Path: blob/21.2-virgl/src/gallium/drivers/r600/r600_cs.h
4570 views
/*1* Copyright 2013 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*22* Authors: Marek Olšák <[email protected]>23*/2425/**26* This file contains helpers for writing commands to commands streams.27*/2829#ifndef R600_CS_H30#define R600_CS_H3132#include "r600_pipe_common.h"33#include "r600d_common.h"3435/**36* Return true if there is enough memory in VRAM and GTT for the buffers37* added so far.38*39* \param vram VRAM memory size not added to the buffer list yet40* \param gtt GTT memory size not added to the buffer list yet41*/42static inline bool43radeon_cs_memory_below_limit(struct r600_common_screen *screen,44struct radeon_cmdbuf *cs,45uint64_t vram, uint64_t gtt)46{47vram += (uint64_t)cs->used_vram_kb * 1024;48gtt += (uint64_t)cs->used_gart_kb * 1024;4950/* Anything that goes above the VRAM size should go to GTT. */51if (vram > screen->info.vram_size)52gtt += vram - screen->info.vram_size;5354/* Now we just need to check if we have enough GTT. */55return gtt < screen->info.gart_size * 0.7;56}5758/**59* Add a buffer to the buffer list for the given command stream (CS).60*61* All buffers used by a CS must be added to the list. This tells the kernel62* driver which buffers are used by GPU commands. Other buffers can63* be swapped out (not accessible) during execution.64*65* The buffer list becomes empty after every context flush and must be66* rebuilt.67*/68static inline unsigned radeon_add_to_buffer_list(struct r600_common_context *rctx,69struct r600_ring *ring,70struct r600_resource *rbo,71enum radeon_bo_usage usage,72enum radeon_bo_priority priority)73{74assert(usage);75return rctx->ws->cs_add_buffer(76&ring->cs, rbo->buf,77(enum radeon_bo_usage)(usage | RADEON_USAGE_SYNCHRONIZED),78rbo->domains, priority) * 4;79}8081/**82* Same as above, but also checks memory usage and flushes the context83* accordingly.84*85* When this SHOULD NOT be used:86*87* - if r600_context_add_resource_size has been called for the buffer88* followed by *_need_cs_space for checking the memory usage89*90* - if r600_need_dma_space has been called for the buffer91*92* - when emitting state packets and draw packets (because preceding packets93* can't be re-emitted at that point)94*95* - if shader resource "enabled_mask" is not up-to-date or there is96* a different constraint disallowing a context flush97*/98static inline unsigned99radeon_add_to_buffer_list_check_mem(struct r600_common_context *rctx,100struct r600_ring *ring,101struct r600_resource *rbo,102enum radeon_bo_usage usage,103enum radeon_bo_priority priority,104bool check_mem)105{106if (check_mem &&107!radeon_cs_memory_below_limit(rctx->screen, &ring->cs,108rctx->vram + rbo->vram_usage,109rctx->gtt + rbo->gart_usage))110ring->flush(rctx, PIPE_FLUSH_ASYNC, NULL);111112return radeon_add_to_buffer_list(rctx, ring, rbo, usage, priority);113}114115static inline void r600_emit_reloc(struct r600_common_context *rctx,116struct r600_ring *ring, struct r600_resource *rbo,117enum radeon_bo_usage usage,118enum radeon_bo_priority priority)119{120struct radeon_cmdbuf *cs = &ring->cs;121bool has_vm = ((struct r600_common_screen*)rctx->b.screen)->info.r600_has_virtual_memory;122unsigned reloc = radeon_add_to_buffer_list(rctx, ring, rbo, usage, priority);123124if (!has_vm) {125radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));126radeon_emit(cs, reloc);127}128}129130static inline void radeon_set_config_reg_seq(struct radeon_cmdbuf *cs, unsigned reg, unsigned num)131{132assert(reg < R600_CONTEXT_REG_OFFSET);133assert(cs->current.cdw + 2 + num <= cs->current.max_dw);134radeon_emit(cs, PKT3(PKT3_SET_CONFIG_REG, num, 0));135radeon_emit(cs, (reg - R600_CONFIG_REG_OFFSET) >> 2);136}137138static inline void radeon_set_config_reg(struct radeon_cmdbuf *cs, unsigned reg, unsigned value)139{140radeon_set_config_reg_seq(cs, reg, 1);141radeon_emit(cs, value);142}143144static inline void radeon_set_context_reg_seq(struct radeon_cmdbuf *cs, unsigned reg, unsigned num)145{146assert(reg >= R600_CONTEXT_REG_OFFSET);147assert(cs->current.cdw + 2 + num <= cs->current.max_dw);148radeon_emit(cs, PKT3(PKT3_SET_CONTEXT_REG, num, 0));149radeon_emit(cs, (reg - R600_CONTEXT_REG_OFFSET) >> 2);150}151152static inline void radeon_set_context_reg(struct radeon_cmdbuf *cs, unsigned reg, unsigned value)153{154radeon_set_context_reg_seq(cs, reg, 1);155radeon_emit(cs, value);156}157158static inline void radeon_set_context_reg_idx(struct radeon_cmdbuf *cs,159unsigned reg, unsigned idx,160unsigned value)161{162assert(reg >= R600_CONTEXT_REG_OFFSET);163assert(cs->current.cdw + 3 <= cs->current.max_dw);164radeon_emit(cs, PKT3(PKT3_SET_CONTEXT_REG, 1, 0));165radeon_emit(cs, (reg - R600_CONTEXT_REG_OFFSET) >> 2 | (idx << 28));166radeon_emit(cs, value);167}168169static inline void radeon_set_sh_reg_seq(struct radeon_cmdbuf *cs, unsigned reg, unsigned num)170{171assert(reg >= SI_SH_REG_OFFSET && reg < SI_SH_REG_END);172assert(cs->current.cdw + 2 + num <= cs->current.max_dw);173radeon_emit(cs, PKT3(PKT3_SET_SH_REG, num, 0));174radeon_emit(cs, (reg - SI_SH_REG_OFFSET) >> 2);175}176177static inline void radeon_set_sh_reg(struct radeon_cmdbuf *cs, unsigned reg, unsigned value)178{179radeon_set_sh_reg_seq(cs, reg, 1);180radeon_emit(cs, value);181}182183static inline void radeon_set_uconfig_reg_seq(struct radeon_cmdbuf *cs, unsigned reg, unsigned num)184{185assert(reg >= CIK_UCONFIG_REG_OFFSET && reg < CIK_UCONFIG_REG_END);186assert(cs->current.cdw + 2 + num <= cs->current.max_dw);187radeon_emit(cs, PKT3(PKT3_SET_UCONFIG_REG, num, 0));188radeon_emit(cs, (reg - CIK_UCONFIG_REG_OFFSET) >> 2);189}190191static inline void radeon_set_uconfig_reg(struct radeon_cmdbuf *cs, unsigned reg, unsigned value)192{193radeon_set_uconfig_reg_seq(cs, reg, 1);194radeon_emit(cs, value);195}196197static inline void radeon_set_uconfig_reg_idx(struct radeon_cmdbuf *cs,198unsigned reg, unsigned idx,199unsigned value)200{201assert(reg >= CIK_UCONFIG_REG_OFFSET && reg < CIK_UCONFIG_REG_END);202assert(cs->current.cdw + 3 <= cs->current.max_dw);203radeon_emit(cs, PKT3(PKT3_SET_UCONFIG_REG, 1, 0));204radeon_emit(cs, (reg - CIK_UCONFIG_REG_OFFSET) >> 2 | (idx << 28));205radeon_emit(cs, value);206}207208#endif209210211