Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_cp_reg_shadowing.c
4570 views
/*1* Copyright 2020 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*/2324#include "si_build_pm4.h"25#include "ac_debug.h"26#include "ac_shadowed_regs.h"27#include "util/u_memory.h"2829static void si_build_load_reg(struct si_screen *sscreen, struct si_pm4_state *pm4,30enum ac_reg_range_type type,31struct si_resource *shadow_regs)32{33uint64_t gpu_address = shadow_regs->gpu_address;34unsigned packet, num_ranges, offset;35const struct ac_reg_range *ranges;3637ac_get_reg_ranges(sscreen->info.chip_class, sscreen->info.family,38type, &num_ranges, &ranges);3940switch (type) {41case SI_REG_RANGE_UCONFIG:42gpu_address += SI_SHADOWED_UCONFIG_REG_OFFSET;43offset = CIK_UCONFIG_REG_OFFSET;44packet = PKT3_LOAD_UCONFIG_REG;45break;46case SI_REG_RANGE_CONTEXT:47gpu_address += SI_SHADOWED_CONTEXT_REG_OFFSET;48offset = SI_CONTEXT_REG_OFFSET;49packet = PKT3_LOAD_CONTEXT_REG;50break;51default:52gpu_address += SI_SHADOWED_SH_REG_OFFSET;53offset = SI_SH_REG_OFFSET;54packet = PKT3_LOAD_SH_REG;55break;56}5758si_pm4_cmd_add(pm4, PKT3(packet, 1 + num_ranges * 2, 0));59si_pm4_cmd_add(pm4, gpu_address);60si_pm4_cmd_add(pm4, gpu_address >> 32);61for (unsigned i = 0; i < num_ranges; i++) {62si_pm4_cmd_add(pm4, (ranges[i].offset - offset) / 4);63si_pm4_cmd_add(pm4, ranges[i].size / 4);64}65}6667static struct si_pm4_state *68si_create_shadowing_ib_preamble(struct si_context *sctx)69{70struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);7172if (sctx->chip_class == GFX10) {73/* SQ_NON_EVENT must be emitted before GE_PC_ALLOC is written. */74si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0));75si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_SQ_NON_EVENT) | EVENT_INDEX(0));76}7778if (sctx->screen->dpbb_allowed) {79si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0));80si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));81}8283/* Wait for idle, because we'll update VGT ring pointers. */84si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0));85si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));8687/* VGT_FLUSH is required even if VGT is idle. It resets VGT pointers. */88si_pm4_cmd_add(pm4, PKT3(PKT3_EVENT_WRITE, 0, 0));89si_pm4_cmd_add(pm4, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));9091if (sctx->chip_class >= GFX10) {92unsigned gcr_cntl = S_586_GL2_INV(1) | S_586_GL2_WB(1) |93S_586_GLM_INV(1) | S_586_GLM_WB(1) |94S_586_GL1_INV(1) | S_586_GLV_INV(1) |95S_586_GLK_INV(1) | S_586_GLI_INV(V_586_GLI_ALL);9697si_pm4_cmd_add(pm4, PKT3(PKT3_ACQUIRE_MEM, 6, 0));98si_pm4_cmd_add(pm4, 0); /* CP_COHER_CNTL */99si_pm4_cmd_add(pm4, 0xffffffff); /* CP_COHER_SIZE */100si_pm4_cmd_add(pm4, 0xffffff); /* CP_COHER_SIZE_HI */101si_pm4_cmd_add(pm4, 0); /* CP_COHER_BASE */102si_pm4_cmd_add(pm4, 0); /* CP_COHER_BASE_HI */103si_pm4_cmd_add(pm4, 0x0000000A); /* POLL_INTERVAL */104si_pm4_cmd_add(pm4, gcr_cntl); /* GCR_CNTL */105} else if (sctx->chip_class == GFX9) {106unsigned cp_coher_cntl = S_0301F0_SH_ICACHE_ACTION_ENA(1) |107S_0301F0_SH_KCACHE_ACTION_ENA(1) |108S_0301F0_TC_ACTION_ENA(1) |109S_0301F0_TCL1_ACTION_ENA(1) |110S_0301F0_TC_WB_ACTION_ENA(1);111112si_pm4_cmd_add(pm4, PKT3(PKT3_ACQUIRE_MEM, 5, 0));113si_pm4_cmd_add(pm4, cp_coher_cntl); /* CP_COHER_CNTL */114si_pm4_cmd_add(pm4, 0xffffffff); /* CP_COHER_SIZE */115si_pm4_cmd_add(pm4, 0xffffff); /* CP_COHER_SIZE_HI */116si_pm4_cmd_add(pm4, 0); /* CP_COHER_BASE */117si_pm4_cmd_add(pm4, 0); /* CP_COHER_BASE_HI */118si_pm4_cmd_add(pm4, 0x0000000A); /* POLL_INTERVAL */119} else {120unreachable("invalid chip");121}122123si_pm4_cmd_add(pm4, PKT3(PKT3_PFP_SYNC_ME, 0, 0));124si_pm4_cmd_add(pm4, 0);125126si_pm4_cmd_add(pm4, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));127si_pm4_cmd_add(pm4,128CC0_UPDATE_LOAD_ENABLES(1) |129CC0_LOAD_PER_CONTEXT_STATE(1) |130CC0_LOAD_CS_SH_REGS(1) |131CC0_LOAD_GFX_SH_REGS(1) |132CC0_LOAD_GLOBAL_UCONFIG(1));133si_pm4_cmd_add(pm4,134CC1_UPDATE_SHADOW_ENABLES(1) |135CC1_SHADOW_PER_CONTEXT_STATE(1) |136CC1_SHADOW_CS_SH_REGS(1) |137CC1_SHADOW_GFX_SH_REGS(1) |138CC1_SHADOW_GLOBAL_UCONFIG(1));139140for (unsigned i = 0; i < SI_NUM_SHADOWED_REG_RANGES; i++)141si_build_load_reg(sctx->screen, pm4, i, sctx->shadowed_regs);142143return pm4;144}145146static void si_set_context_reg_array(struct radeon_cmdbuf *cs, unsigned reg, unsigned num,147const uint32_t *values)148{149radeon_begin(cs);150radeon_set_context_reg_seq(cs, reg, num);151radeon_emit_array(cs, values, num);152radeon_end();153}154155void si_init_cp_reg_shadowing(struct si_context *sctx)156{157if (sctx->screen->info.mid_command_buffer_preemption_enabled ||158sctx->screen->debug_flags & DBG(SHADOW_REGS)) {159sctx->shadowed_regs =160si_aligned_buffer_create(sctx->b.screen,161SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL,162PIPE_USAGE_DEFAULT,163SI_SHADOWED_REG_BUFFER_SIZE,1644096);165if (!sctx->shadowed_regs)166fprintf(stderr, "radeonsi: cannot create a shadowed_regs buffer\n");167}168169si_init_cs_preamble_state(sctx, sctx->shadowed_regs != NULL);170171if (sctx->shadowed_regs) {172/* We need to clear the shadowed reg buffer. */173si_cp_dma_clear_buffer(sctx, &sctx->gfx_cs, &sctx->shadowed_regs->b.b,1740, sctx->shadowed_regs->bo_size, 0, SI_OP_SYNC_AFTER,175SI_COHERENCY_CP, L2_BYPASS);176177/* Create the shadowing preamble. */178struct si_pm4_state *shadowing_preamble =179si_create_shadowing_ib_preamble(sctx);180181/* Initialize shadowed registers as follows. */182radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, sctx->shadowed_regs,183RADEON_USAGE_READWRITE, RADEON_PRIO_DESCRIPTORS);184si_pm4_emit(sctx, shadowing_preamble);185ac_emulate_clear_state(&sctx->screen->info, &sctx->gfx_cs, si_set_context_reg_array);186si_pm4_emit(sctx, sctx->cs_preamble_state);187188/* The register values are shadowed, so we won't need to set them again. */189si_pm4_free_state(sctx, sctx->cs_preamble_state, ~0);190sctx->cs_preamble_state = NULL;191192si_set_tracked_regs_to_clear_state(sctx);193194/* Setup preemption. The shadowing preamble will be executed as a preamble IB,195* which will load register values from memory on a context switch.196*/197sctx->ws->cs_setup_preemption(&sctx->gfx_cs, shadowing_preamble->pm4,198shadowing_preamble->ndw);199si_pm4_free_state(sctx, shadowing_preamble, ~0);200}201}202203204