1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2010 Jerome Glisse <glisse@freedesktop.org> 3bf215546Sopenharmony_ci * Copyright 2018 Advanced Micro Devices, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub 10bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom 11bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 14bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 15bf215546Sopenharmony_ci * Software. 16bf215546Sopenharmony_ci * 17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 20bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 21bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 22bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 23bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 24bf215546Sopenharmony_ci */ 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include "si_pipe.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "driver_ddebug/dd_util.h" 29bf215546Sopenharmony_ci#include "radeon_uvd.h" 30bf215546Sopenharmony_ci#include "si_compute.h" 31bf215546Sopenharmony_ci#include "si_public.h" 32bf215546Sopenharmony_ci#include "si_shader_internal.h" 33bf215546Sopenharmony_ci#include "sid.h" 34bf215546Sopenharmony_ci#include "ac_shadowed_regs.h" 35bf215546Sopenharmony_ci#include "compiler/nir/nir.h" 36bf215546Sopenharmony_ci#include "util/disk_cache.h" 37bf215546Sopenharmony_ci#include "util/u_cpu_detect.h" 38bf215546Sopenharmony_ci#include "util/u_log.h" 39bf215546Sopenharmony_ci#include "util/u_memory.h" 40bf215546Sopenharmony_ci#include "util/u_suballoc.h" 41bf215546Sopenharmony_ci#include "util/u_tests.h" 42bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 43bf215546Sopenharmony_ci#include "util/xmlconfig.h" 44bf215546Sopenharmony_ci#include "vl/vl_decoder.h" 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci#include <xf86drm.h> 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_cistatic struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags); 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_cistatic const struct debug_named_value radeonsi_debug_options[] = { 51bf215546Sopenharmony_ci /* Shader logging options: */ 52bf215546Sopenharmony_ci {"vs", DBG(VS), "Print vertex shaders"}, 53bf215546Sopenharmony_ci {"ps", DBG(PS), "Print pixel shaders"}, 54bf215546Sopenharmony_ci {"gs", DBG(GS), "Print geometry shaders"}, 55bf215546Sopenharmony_ci {"tcs", DBG(TCS), "Print tessellation control shaders"}, 56bf215546Sopenharmony_ci {"tes", DBG(TES), "Print tessellation evaluation shaders"}, 57bf215546Sopenharmony_ci {"cs", DBG(CS), "Print compute shaders"}, 58bf215546Sopenharmony_ci {"noir", DBG(NO_IR), "Don't print the LLVM IR"}, 59bf215546Sopenharmony_ci {"nonir", DBG(NO_NIR), "Don't print NIR when printing shaders"}, 60bf215546Sopenharmony_ci {"noasm", DBG(NO_ASM), "Don't print disassembled shaders"}, 61bf215546Sopenharmony_ci {"preoptir", DBG(PREOPT_IR), "Print the LLVM IR before initial optimizations"}, 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci /* Shader compiler options the shader cache should be aware of: */ 64bf215546Sopenharmony_ci {"w32ge", DBG(W32_GE), "Use Wave32 for vertex, tessellation, and geometry shaders."}, 65bf215546Sopenharmony_ci {"w32ps", DBG(W32_PS), "Use Wave32 for pixel shaders."}, 66bf215546Sopenharmony_ci {"w32psdiscard", DBG(W32_PS_DISCARD), "Use Wave32 for pixel shaders even if they contain discard and LLVM is buggy."}, 67bf215546Sopenharmony_ci {"w32cs", DBG(W32_CS), "Use Wave32 for computes shaders."}, 68bf215546Sopenharmony_ci {"w64ge", DBG(W64_GE), "Use Wave64 for vertex, tessellation, and geometry shaders."}, 69bf215546Sopenharmony_ci {"w64ps", DBG(W64_PS), "Use Wave64 for pixel shaders."}, 70bf215546Sopenharmony_ci {"w64cs", DBG(W64_CS), "Use Wave64 for computes shaders."}, 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci /* Shader compiler options (with no effect on the shader cache): */ 73bf215546Sopenharmony_ci {"checkir", DBG(CHECK_IR), "Enable additional sanity checks on shader IR"}, 74bf215546Sopenharmony_ci {"mono", DBG(MONOLITHIC_SHADERS), "Use old-style monolithic shaders compiled on demand"}, 75bf215546Sopenharmony_ci {"nooptvariant", DBG(NO_OPT_VARIANT), "Disable compiling optimized shader variants."}, 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci /* Information logging options: */ 78bf215546Sopenharmony_ci {"info", DBG(INFO), "Print driver information"}, 79bf215546Sopenharmony_ci {"tex", DBG(TEX), "Print texture info"}, 80bf215546Sopenharmony_ci {"compute", DBG(COMPUTE), "Print compute info"}, 81bf215546Sopenharmony_ci {"vm", DBG(VM), "Print virtual addresses when creating resources"}, 82bf215546Sopenharmony_ci {"cache_stats", DBG(CACHE_STATS), "Print shader cache statistics."}, 83bf215546Sopenharmony_ci {"ib", DBG(IB), "Print command buffers."}, 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci /* Driver options: */ 86bf215546Sopenharmony_ci {"nowc", DBG(NO_WC), "Disable GTT write combining"}, 87bf215546Sopenharmony_ci {"check_vm", DBG(CHECK_VM), "Check VM faults and dump debug info."}, 88bf215546Sopenharmony_ci {"reserve_vmid", DBG(RESERVE_VMID), "Force VMID reservation per context."}, 89bf215546Sopenharmony_ci {"shadowregs", DBG(SHADOW_REGS), "Enable CP register shadowing."}, 90bf215546Sopenharmony_ci {"nofastdlist", DBG(NO_FAST_DISPLAY_LIST), "Disable fast display lists"}, 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci /* Multimedia options: */ 93bf215546Sopenharmony_ci { "noefc", DBG(NO_EFC), "Disable hardware based encoder colour format conversion."}, 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci /* 3D engine options: */ 96bf215546Sopenharmony_ci {"nogfx", DBG(NO_GFX), "Disable graphics. Only multimedia compute paths can be used."}, 97bf215546Sopenharmony_ci {"nongg", DBG(NO_NGG), "Disable NGG and use the legacy pipeline."}, 98bf215546Sopenharmony_ci {"nggc", DBG(ALWAYS_NGG_CULLING_ALL), "Always use NGG culling even when it can hurt."}, 99bf215546Sopenharmony_ci {"nonggc", DBG(NO_NGG_CULLING), "Disable NGG culling."}, 100bf215546Sopenharmony_ci {"switch_on_eop", DBG(SWITCH_ON_EOP), "Program WD/IA to switch on end-of-packet."}, 101bf215546Sopenharmony_ci {"nooutoforder", DBG(NO_OUT_OF_ORDER), "Disable out-of-order rasterization"}, 102bf215546Sopenharmony_ci {"nodpbb", DBG(NO_DPBB), "Disable DPBB."}, 103bf215546Sopenharmony_ci {"dpbb", DBG(DPBB), "Enable DPBB."}, 104bf215546Sopenharmony_ci {"nohyperz", DBG(NO_HYPERZ), "Disable Hyper-Z"}, 105bf215546Sopenharmony_ci {"no2d", DBG(NO_2D_TILING), "Disable 2D tiling"}, 106bf215546Sopenharmony_ci {"notiling", DBG(NO_TILING), "Disable tiling"}, 107bf215546Sopenharmony_ci {"nodisplaytiling", DBG(NO_DISPLAY_TILING), "Disable display tiling"}, 108bf215546Sopenharmony_ci {"nodisplaydcc", DBG(NO_DISPLAY_DCC), "Disable display DCC"}, 109bf215546Sopenharmony_ci {"noexporteddcc", DBG(NO_EXPORTED_DCC), "Disable DCC for all exported buffers (via DMABUF, etc.)"}, 110bf215546Sopenharmony_ci {"nodcc", DBG(NO_DCC), "Disable DCC."}, 111bf215546Sopenharmony_ci {"nodccclear", DBG(NO_DCC_CLEAR), "Disable DCC fast clear."}, 112bf215546Sopenharmony_ci {"nodccstore", DBG(NO_DCC_STORE), "Disable DCC stores"}, 113bf215546Sopenharmony_ci {"dccstore", DBG(DCC_STORE), "Enable DCC stores"}, 114bf215546Sopenharmony_ci {"nodccmsaa", DBG(NO_DCC_MSAA), "Disable DCC for MSAA"}, 115bf215546Sopenharmony_ci {"nofmask", DBG(NO_FMASK), "Disable MSAA compression"}, 116bf215546Sopenharmony_ci {"nodma", DBG(NO_DMA), "Disable SDMA-copy for DRI_PRIME"}, 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ci {"tmz", DBG(TMZ), "Force allocation of scanout/depth/stencil buffer as encrypted"}, 119bf215546Sopenharmony_ci {"sqtt", DBG(SQTT), "Enable SQTT"}, 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci DEBUG_NAMED_VALUE_END /* must be last */ 122bf215546Sopenharmony_ci}; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_cistatic const struct debug_named_value test_options[] = { 125bf215546Sopenharmony_ci /* Tests: */ 126bf215546Sopenharmony_ci {"imagecopy", DBG(TEST_IMAGE_COPY), "Invoke resource_copy_region tests with images and exit."}, 127bf215546Sopenharmony_ci {"cbresolve", DBG(TEST_CB_RESOLVE), "Invoke MSAA resolve tests and exit."}, 128bf215546Sopenharmony_ci {"computeblit", DBG(TEST_COMPUTE_BLIT), "Invoke blits tests and exit."}, 129bf215546Sopenharmony_ci {"testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit."}, 130bf215546Sopenharmony_ci {"testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit."}, 131bf215546Sopenharmony_ci {"testdmaperf", DBG(TEST_DMA_PERF), "Test DMA performance"}, 132bf215546Sopenharmony_ci {"testgds", DBG(TEST_GDS), "Test GDS."}, 133bf215546Sopenharmony_ci {"testgdsmm", DBG(TEST_GDS_MM), "Test GDS memory management."}, 134bf215546Sopenharmony_ci {"testgdsoamm", DBG(TEST_GDS_OA_MM), "Test GDS OA memory management."}, 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci DEBUG_NAMED_VALUE_END /* must be last */ 137bf215546Sopenharmony_ci}; 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_cibool si_init_compiler(struct si_screen *sscreen, struct ac_llvm_compiler *compiler) 140bf215546Sopenharmony_ci{ 141bf215546Sopenharmony_ci /* Only create the less-optimizing version of the compiler on APUs 142bf215546Sopenharmony_ci * predating Ryzen (Raven). */ 143bf215546Sopenharmony_ci bool create_low_opt_compiler = 144bf215546Sopenharmony_ci !sscreen->info.has_dedicated_vram && sscreen->info.gfx_level <= GFX8; 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci enum ac_target_machine_options tm_options = 147bf215546Sopenharmony_ci (sscreen->debug_flags & DBG(CHECK_IR) ? AC_TM_CHECK_IR : 0) | 148bf215546Sopenharmony_ci (create_low_opt_compiler ? AC_TM_CREATE_LOW_OPT : 0); 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci ac_init_llvm_once(); 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci if (!ac_init_llvm_compiler(compiler, sscreen->info.family, tm_options)) 153bf215546Sopenharmony_ci return false; 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci compiler->passes = ac_create_llvm_passes(compiler->tm); 156bf215546Sopenharmony_ci if (compiler->low_opt_tm) 157bf215546Sopenharmony_ci compiler->low_opt_passes = ac_create_llvm_passes(compiler->low_opt_tm); 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_ci return true; 160bf215546Sopenharmony_ci} 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_civoid si_init_aux_async_compute_ctx(struct si_screen *sscreen) 163bf215546Sopenharmony_ci{ 164bf215546Sopenharmony_ci assert(!sscreen->async_compute_context); 165bf215546Sopenharmony_ci sscreen->async_compute_context = si_create_context( 166bf215546Sopenharmony_ci &sscreen->b, 167bf215546Sopenharmony_ci SI_CONTEXT_FLAG_AUX | 168bf215546Sopenharmony_ci (sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) | 169bf215546Sopenharmony_ci PIPE_CONTEXT_COMPUTE_ONLY); 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci /* Limit the numbers of waves allocated for this context. */ 172bf215546Sopenharmony_ci if (sscreen->async_compute_context) 173bf215546Sopenharmony_ci ((struct si_context*)sscreen->async_compute_context)->cs_max_waves_per_sh = 2; 174bf215546Sopenharmony_ci} 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_cistatic void si_destroy_compiler(struct ac_llvm_compiler *compiler) 177bf215546Sopenharmony_ci{ 178bf215546Sopenharmony_ci ac_destroy_llvm_compiler(compiler); 179bf215546Sopenharmony_ci} 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_cistatic void decref_implicit_resource(struct hash_entry *entry) 183bf215546Sopenharmony_ci{ 184bf215546Sopenharmony_ci pipe_resource_reference((struct pipe_resource**)&entry->data, NULL); 185bf215546Sopenharmony_ci} 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci/* 188bf215546Sopenharmony_ci * pipe_context 189bf215546Sopenharmony_ci */ 190bf215546Sopenharmony_cistatic void si_destroy_context(struct pipe_context *context) 191bf215546Sopenharmony_ci{ 192bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)context; 193bf215546Sopenharmony_ci int i; 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci /* Unreference the framebuffer normally to disable related logic 196bf215546Sopenharmony_ci * properly. 197bf215546Sopenharmony_ci */ 198bf215546Sopenharmony_ci struct pipe_framebuffer_state fb = {}; 199bf215546Sopenharmony_ci if (context->set_framebuffer_state) 200bf215546Sopenharmony_ci context->set_framebuffer_state(context, &fb); 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci si_release_all_descriptors(sctx); 203bf215546Sopenharmony_ci 204bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10 && sctx->has_graphics) 205bf215546Sopenharmony_ci gfx10_destroy_query(sctx); 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci if (sctx->thread_trace) 208bf215546Sopenharmony_ci si_destroy_thread_trace(sctx); 209bf215546Sopenharmony_ci 210bf215546Sopenharmony_ci pipe_resource_reference(&sctx->esgs_ring, NULL); 211bf215546Sopenharmony_ci pipe_resource_reference(&sctx->gsvs_ring, NULL); 212bf215546Sopenharmony_ci pipe_resource_reference(&sctx->tess_rings, NULL); 213bf215546Sopenharmony_ci pipe_resource_reference(&sctx->tess_rings_tmz, NULL); 214bf215546Sopenharmony_ci pipe_resource_reference(&sctx->null_const_buf.buffer, NULL); 215bf215546Sopenharmony_ci pipe_resource_reference(&sctx->sample_pos_buffer, NULL); 216bf215546Sopenharmony_ci si_resource_reference(&sctx->border_color_buffer, NULL); 217bf215546Sopenharmony_ci free(sctx->border_color_table); 218bf215546Sopenharmony_ci si_resource_reference(&sctx->scratch_buffer, NULL); 219bf215546Sopenharmony_ci si_resource_reference(&sctx->compute_scratch_buffer, NULL); 220bf215546Sopenharmony_ci si_resource_reference(&sctx->wait_mem_scratch, NULL); 221bf215546Sopenharmony_ci si_resource_reference(&sctx->wait_mem_scratch_tmz, NULL); 222bf215546Sopenharmony_ci si_resource_reference(&sctx->small_prim_cull_info_buf, NULL); 223bf215546Sopenharmony_ci si_resource_reference(&sctx->pipeline_stats_query_buf, NULL); 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci if (sctx->cs_preamble_state) 226bf215546Sopenharmony_ci si_pm4_free_state(sctx, sctx->cs_preamble_state, ~0); 227bf215546Sopenharmony_ci if (sctx->cs_preamble_state_tmz) 228bf215546Sopenharmony_ci si_pm4_free_state(sctx, sctx->cs_preamble_state_tmz, ~0); 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++) 231bf215546Sopenharmony_ci si_pm4_free_state(sctx, sctx->vgt_shader_config[i], SI_STATE_IDX(vgt_shader_config)); 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ci if (sctx->fixed_func_tcs_shader_cache) { 234bf215546Sopenharmony_ci hash_table_foreach(sctx->fixed_func_tcs_shader_cache, entry) { 235bf215546Sopenharmony_ci sctx->b.delete_tcs_state(&sctx->b, entry->data); 236bf215546Sopenharmony_ci } 237bf215546Sopenharmony_ci _mesa_hash_table_destroy(sctx->fixed_func_tcs_shader_cache, NULL); 238bf215546Sopenharmony_ci } 239bf215546Sopenharmony_ci 240bf215546Sopenharmony_ci if (sctx->custom_dsa_flush) 241bf215546Sopenharmony_ci sctx->b.delete_depth_stencil_alpha_state(&sctx->b, sctx->custom_dsa_flush); 242bf215546Sopenharmony_ci if (sctx->custom_blend_resolve) 243bf215546Sopenharmony_ci sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_resolve); 244bf215546Sopenharmony_ci if (sctx->custom_blend_fmask_decompress) 245bf215546Sopenharmony_ci sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_fmask_decompress); 246bf215546Sopenharmony_ci if (sctx->custom_blend_eliminate_fastclear) 247bf215546Sopenharmony_ci sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_eliminate_fastclear); 248bf215546Sopenharmony_ci if (sctx->custom_blend_dcc_decompress) 249bf215546Sopenharmony_ci sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_dcc_decompress); 250bf215546Sopenharmony_ci if (sctx->vs_blit_pos) 251bf215546Sopenharmony_ci sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos); 252bf215546Sopenharmony_ci if (sctx->vs_blit_pos_layered) 253bf215546Sopenharmony_ci sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos_layered); 254bf215546Sopenharmony_ci if (sctx->vs_blit_color) 255bf215546Sopenharmony_ci sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color); 256bf215546Sopenharmony_ci if (sctx->vs_blit_color_layered) 257bf215546Sopenharmony_ci sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color_layered); 258bf215546Sopenharmony_ci if (sctx->vs_blit_texcoord) 259bf215546Sopenharmony_ci sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_texcoord); 260bf215546Sopenharmony_ci if (sctx->cs_clear_buffer) 261bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_buffer); 262bf215546Sopenharmony_ci if (sctx->cs_clear_buffer_rmw) 263bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_buffer_rmw); 264bf215546Sopenharmony_ci if (sctx->cs_copy_buffer) 265bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_buffer); 266bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_copy_image); i++) { 267bf215546Sopenharmony_ci for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_copy_image[i]); j++) { 268bf215546Sopenharmony_ci if (sctx->cs_copy_image[i][j]) 269bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_image[i][j]); 270bf215546Sopenharmony_ci } 271bf215546Sopenharmony_ci } 272bf215546Sopenharmony_ci if (sctx->cs_clear_render_target) 273bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target); 274bf215546Sopenharmony_ci if (sctx->cs_clear_render_target_1d_array) 275bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target_1d_array); 276bf215546Sopenharmony_ci if (sctx->cs_clear_12bytes_buffer) 277bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_12bytes_buffer); 278bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_dcc_retile); i++) { 279bf215546Sopenharmony_ci if (sctx->cs_dcc_retile[i]) 280bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_dcc_retile[i]); 281bf215546Sopenharmony_ci } 282bf215546Sopenharmony_ci if (sctx->no_velems_state) 283bf215546Sopenharmony_ci sctx->b.delete_vertex_elements_state(&sctx->b, sctx->no_velems_state); 284bf215546Sopenharmony_ci 285bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_fmask_expand); i++) { 286bf215546Sopenharmony_ci for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_fmask_expand[i]); j++) { 287bf215546Sopenharmony_ci if (sctx->cs_fmask_expand[i][j]) { 288bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_fmask_expand[i][j]); 289bf215546Sopenharmony_ci } 290bf215546Sopenharmony_ci } 291bf215546Sopenharmony_ci } 292bf215546Sopenharmony_ci 293bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_clear_dcc_msaa); i++) { 294bf215546Sopenharmony_ci for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i]); j++) { 295bf215546Sopenharmony_ci for (unsigned k = 0; k < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j]); k++) { 296bf215546Sopenharmony_ci for (unsigned l = 0; l < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j][k]); l++) { 297bf215546Sopenharmony_ci for (unsigned m = 0; m < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j][k][l]); m++) { 298bf215546Sopenharmony_ci if (sctx->cs_clear_dcc_msaa[i][j][k][l][m]) 299bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_dcc_msaa[i][j][k][l][m]); 300bf215546Sopenharmony_ci } 301bf215546Sopenharmony_ci } 302bf215546Sopenharmony_ci } 303bf215546Sopenharmony_ci } 304bf215546Sopenharmony_ci } 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_ci if (sctx->blitter) 307bf215546Sopenharmony_ci util_blitter_destroy(sctx->blitter); 308bf215546Sopenharmony_ci 309bf215546Sopenharmony_ci if (sctx->query_result_shader) 310bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->query_result_shader); 311bf215546Sopenharmony_ci if (sctx->sh_query_result_shader) 312bf215546Sopenharmony_ci sctx->b.delete_compute_state(&sctx->b, sctx->sh_query_result_shader); 313bf215546Sopenharmony_ci 314bf215546Sopenharmony_ci sctx->ws->cs_destroy(&sctx->gfx_cs); 315bf215546Sopenharmony_ci if (sctx->ctx) 316bf215546Sopenharmony_ci sctx->ws->ctx_destroy(sctx->ctx); 317bf215546Sopenharmony_ci if (sctx->sdma_cs) { 318bf215546Sopenharmony_ci sctx->ws->cs_destroy(sctx->sdma_cs); 319bf215546Sopenharmony_ci free(sctx->sdma_cs); 320bf215546Sopenharmony_ci } 321bf215546Sopenharmony_ci 322bf215546Sopenharmony_ci if (sctx->dirty_implicit_resources) 323bf215546Sopenharmony_ci _mesa_hash_table_destroy(sctx->dirty_implicit_resources, 324bf215546Sopenharmony_ci decref_implicit_resource); 325bf215546Sopenharmony_ci 326bf215546Sopenharmony_ci if (sctx->b.stream_uploader) 327bf215546Sopenharmony_ci u_upload_destroy(sctx->b.stream_uploader); 328bf215546Sopenharmony_ci if (sctx->b.const_uploader && sctx->b.const_uploader != sctx->b.stream_uploader) 329bf215546Sopenharmony_ci u_upload_destroy(sctx->b.const_uploader); 330bf215546Sopenharmony_ci if (sctx->cached_gtt_allocator) 331bf215546Sopenharmony_ci u_upload_destroy(sctx->cached_gtt_allocator); 332bf215546Sopenharmony_ci 333bf215546Sopenharmony_ci slab_destroy_child(&sctx->pool_transfers); 334bf215546Sopenharmony_ci slab_destroy_child(&sctx->pool_transfers_unsync); 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci u_suballocator_destroy(&sctx->allocator_zeroed_memory); 337bf215546Sopenharmony_ci 338bf215546Sopenharmony_ci sctx->ws->fence_reference(&sctx->last_gfx_fence, NULL); 339bf215546Sopenharmony_ci si_resource_reference(&sctx->eop_bug_scratch, NULL); 340bf215546Sopenharmony_ci si_resource_reference(&sctx->eop_bug_scratch_tmz, NULL); 341bf215546Sopenharmony_ci si_resource_reference(&sctx->shadowed_regs, NULL); 342bf215546Sopenharmony_ci 343bf215546Sopenharmony_ci si_destroy_compiler(&sctx->compiler); 344bf215546Sopenharmony_ci 345bf215546Sopenharmony_ci si_saved_cs_reference(&sctx->current_saved_cs, NULL); 346bf215546Sopenharmony_ci 347bf215546Sopenharmony_ci _mesa_hash_table_destroy(sctx->tex_handles, NULL); 348bf215546Sopenharmony_ci _mesa_hash_table_destroy(sctx->img_handles, NULL); 349bf215546Sopenharmony_ci 350bf215546Sopenharmony_ci util_dynarray_fini(&sctx->resident_tex_handles); 351bf215546Sopenharmony_ci util_dynarray_fini(&sctx->resident_img_handles); 352bf215546Sopenharmony_ci util_dynarray_fini(&sctx->resident_tex_needs_color_decompress); 353bf215546Sopenharmony_ci util_dynarray_fini(&sctx->resident_img_needs_color_decompress); 354bf215546Sopenharmony_ci util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress); 355bf215546Sopenharmony_ci 356bf215546Sopenharmony_ci if (!(sctx->context_flags & SI_CONTEXT_FLAG_AUX)) 357bf215546Sopenharmony_ci p_atomic_dec(&context->screen->num_contexts); 358bf215546Sopenharmony_ci 359bf215546Sopenharmony_ci FREE(sctx); 360bf215546Sopenharmony_ci} 361bf215546Sopenharmony_ci 362bf215546Sopenharmony_cistatic enum pipe_reset_status si_get_reset_status(struct pipe_context *ctx) 363bf215546Sopenharmony_ci{ 364bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 365bf215546Sopenharmony_ci if (sctx->context_flags & SI_CONTEXT_FLAG_AUX) 366bf215546Sopenharmony_ci return PIPE_NO_RESET; 367bf215546Sopenharmony_ci 368bf215546Sopenharmony_ci bool needs_reset; 369bf215546Sopenharmony_ci enum pipe_reset_status status = sctx->ws->ctx_query_reset_status(sctx->ctx, false, &needs_reset); 370bf215546Sopenharmony_ci 371bf215546Sopenharmony_ci if (status != PIPE_NO_RESET && needs_reset && !(sctx->context_flags & SI_CONTEXT_FLAG_AUX)) { 372bf215546Sopenharmony_ci /* Call the gallium frontend to set a no-op API dispatch. */ 373bf215546Sopenharmony_ci if (sctx->device_reset_callback.reset) { 374bf215546Sopenharmony_ci sctx->device_reset_callback.reset(sctx->device_reset_callback.data, status); 375bf215546Sopenharmony_ci } 376bf215546Sopenharmony_ci } 377bf215546Sopenharmony_ci return status; 378bf215546Sopenharmony_ci} 379bf215546Sopenharmony_ci 380bf215546Sopenharmony_cistatic void si_set_device_reset_callback(struct pipe_context *ctx, 381bf215546Sopenharmony_ci const struct pipe_device_reset_callback *cb) 382bf215546Sopenharmony_ci{ 383bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 384bf215546Sopenharmony_ci 385bf215546Sopenharmony_ci if (cb) 386bf215546Sopenharmony_ci sctx->device_reset_callback = *cb; 387bf215546Sopenharmony_ci else 388bf215546Sopenharmony_ci memset(&sctx->device_reset_callback, 0, sizeof(sctx->device_reset_callback)); 389bf215546Sopenharmony_ci} 390bf215546Sopenharmony_ci 391bf215546Sopenharmony_ci/* Apitrace profiling: 392bf215546Sopenharmony_ci * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times 393bf215546Sopenharmony_ci * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call 394bf215546Sopenharmony_ci * and remember its number. 395bf215546Sopenharmony_ci * 3) In Mesa, enable queries and performance counters around that draw 396bf215546Sopenharmony_ci * call and print the results. 397bf215546Sopenharmony_ci * 4) glretrace --benchmark --markers .. 398bf215546Sopenharmony_ci */ 399bf215546Sopenharmony_cistatic void si_emit_string_marker(struct pipe_context *ctx, const char *string, int len) 400bf215546Sopenharmony_ci{ 401bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 402bf215546Sopenharmony_ci 403bf215546Sopenharmony_ci dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number); 404bf215546Sopenharmony_ci 405bf215546Sopenharmony_ci if (sctx->thread_trace_enabled) 406bf215546Sopenharmony_ci si_write_user_event(sctx, &sctx->gfx_cs, UserEventTrigger, string, len); 407bf215546Sopenharmony_ci 408bf215546Sopenharmony_ci if (sctx->log) 409bf215546Sopenharmony_ci u_log_printf(sctx->log, "\nString marker: %*s\n", len, string); 410bf215546Sopenharmony_ci} 411bf215546Sopenharmony_ci 412bf215546Sopenharmony_cistatic void si_set_debug_callback(struct pipe_context *ctx, const struct util_debug_callback *cb) 413bf215546Sopenharmony_ci{ 414bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 415bf215546Sopenharmony_ci struct si_screen *screen = sctx->screen; 416bf215546Sopenharmony_ci 417bf215546Sopenharmony_ci util_queue_finish(&screen->shader_compiler_queue); 418bf215546Sopenharmony_ci util_queue_finish(&screen->shader_compiler_queue_low_priority); 419bf215546Sopenharmony_ci 420bf215546Sopenharmony_ci if (cb) 421bf215546Sopenharmony_ci sctx->debug = *cb; 422bf215546Sopenharmony_ci else 423bf215546Sopenharmony_ci memset(&sctx->debug, 0, sizeof(sctx->debug)); 424bf215546Sopenharmony_ci} 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_cistatic void si_set_log_context(struct pipe_context *ctx, struct u_log_context *log) 427bf215546Sopenharmony_ci{ 428bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 429bf215546Sopenharmony_ci sctx->log = log; 430bf215546Sopenharmony_ci 431bf215546Sopenharmony_ci if (log) 432bf215546Sopenharmony_ci u_log_add_auto_logger(log, si_auto_log_cs, sctx); 433bf215546Sopenharmony_ci} 434bf215546Sopenharmony_ci 435bf215546Sopenharmony_cistatic void si_set_context_param(struct pipe_context *ctx, enum pipe_context_param param, 436bf215546Sopenharmony_ci unsigned value) 437bf215546Sopenharmony_ci{ 438bf215546Sopenharmony_ci struct radeon_winsys *ws = ((struct si_context *)ctx)->ws; 439bf215546Sopenharmony_ci 440bf215546Sopenharmony_ci switch (param) { 441bf215546Sopenharmony_ci case PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE: 442bf215546Sopenharmony_ci ws->pin_threads_to_L3_cache(ws, value); 443bf215546Sopenharmony_ci break; 444bf215546Sopenharmony_ci default:; 445bf215546Sopenharmony_ci } 446bf215546Sopenharmony_ci} 447bf215546Sopenharmony_ci 448bf215546Sopenharmony_cistatic void si_set_frontend_noop(struct pipe_context *ctx, bool enable) 449bf215546Sopenharmony_ci{ 450bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 451bf215546Sopenharmony_ci 452bf215546Sopenharmony_ci ctx->flush(ctx, NULL, PIPE_FLUSH_ASYNC); 453bf215546Sopenharmony_ci sctx->is_noop = enable; 454bf215546Sopenharmony_ci} 455bf215546Sopenharmony_ci 456bf215546Sopenharmony_cistatic struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags) 457bf215546Sopenharmony_ci{ 458bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 459bf215546Sopenharmony_ci STATIC_ASSERT(DBG_COUNT <= 64); 460bf215546Sopenharmony_ci 461bf215546Sopenharmony_ci /* Don't create a context if it's not compute-only and hw is compute-only. */ 462bf215546Sopenharmony_ci if (!sscreen->info.has_graphics && !(flags & PIPE_CONTEXT_COMPUTE_ONLY)) { 463bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create a graphics context on a compute chip\n"); 464bf215546Sopenharmony_ci return NULL; 465bf215546Sopenharmony_ci } 466bf215546Sopenharmony_ci 467bf215546Sopenharmony_ci struct si_context *sctx = CALLOC_STRUCT(si_context); 468bf215546Sopenharmony_ci struct radeon_winsys *ws = sscreen->ws; 469bf215546Sopenharmony_ci int shader, i; 470bf215546Sopenharmony_ci bool stop_exec_on_failure = (flags & PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET) != 0; 471bf215546Sopenharmony_ci enum radeon_ctx_priority priority; 472bf215546Sopenharmony_ci 473bf215546Sopenharmony_ci if (!sctx) { 474bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't allocate a context\n"); 475bf215546Sopenharmony_ci return NULL; 476bf215546Sopenharmony_ci } 477bf215546Sopenharmony_ci 478bf215546Sopenharmony_ci sctx->has_graphics = sscreen->info.gfx_level == GFX6 || !(flags & PIPE_CONTEXT_COMPUTE_ONLY); 479bf215546Sopenharmony_ci 480bf215546Sopenharmony_ci if (flags & PIPE_CONTEXT_DEBUG) 481bf215546Sopenharmony_ci sscreen->record_llvm_ir = true; /* racy but not critical */ 482bf215546Sopenharmony_ci 483bf215546Sopenharmony_ci sctx->b.screen = screen; /* this must be set first */ 484bf215546Sopenharmony_ci sctx->b.priv = NULL; 485bf215546Sopenharmony_ci sctx->b.destroy = si_destroy_context; 486bf215546Sopenharmony_ci sctx->screen = sscreen; /* Easy accessing of screen/winsys. */ 487bf215546Sopenharmony_ci sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0; 488bf215546Sopenharmony_ci sctx->context_flags = flags; 489bf215546Sopenharmony_ci 490bf215546Sopenharmony_ci slab_create_child(&sctx->pool_transfers, &sscreen->pool_transfers); 491bf215546Sopenharmony_ci slab_create_child(&sctx->pool_transfers_unsync, &sscreen->pool_transfers); 492bf215546Sopenharmony_ci 493bf215546Sopenharmony_ci sctx->ws = sscreen->ws; 494bf215546Sopenharmony_ci sctx->family = sscreen->info.family; 495bf215546Sopenharmony_ci sctx->gfx_level = sscreen->info.gfx_level; 496bf215546Sopenharmony_ci 497bf215546Sopenharmony_ci if (sctx->gfx_level == GFX7 || sctx->gfx_level == GFX8 || sctx->gfx_level == GFX9) { 498bf215546Sopenharmony_ci sctx->eop_bug_scratch = si_aligned_buffer_create( 499bf215546Sopenharmony_ci &sscreen->b, PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL, 500bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 16 * sscreen->info.max_render_backends, 256); 501bf215546Sopenharmony_ci if (!sctx->eop_bug_scratch) { 502bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create eop_bug_scratch\n"); 503bf215546Sopenharmony_ci goto fail; 504bf215546Sopenharmony_ci } 505bf215546Sopenharmony_ci } 506bf215546Sopenharmony_ci 507bf215546Sopenharmony_ci if (flags & PIPE_CONTEXT_HIGH_PRIORITY) { 508bf215546Sopenharmony_ci priority = RADEON_CTX_PRIORITY_HIGH; 509bf215546Sopenharmony_ci } else if (flags & PIPE_CONTEXT_LOW_PRIORITY) { 510bf215546Sopenharmony_ci priority = RADEON_CTX_PRIORITY_LOW; 511bf215546Sopenharmony_ci } else { 512bf215546Sopenharmony_ci priority = RADEON_CTX_PRIORITY_MEDIUM; 513bf215546Sopenharmony_ci } 514bf215546Sopenharmony_ci 515bf215546Sopenharmony_ci /* Initialize the context handle and the command stream. */ 516bf215546Sopenharmony_ci sctx->ctx = sctx->ws->ctx_create(sctx->ws, priority); 517bf215546Sopenharmony_ci if (!sctx->ctx && priority != RADEON_CTX_PRIORITY_MEDIUM) { 518bf215546Sopenharmony_ci /* Context priority should be treated as a hint. If context creation 519bf215546Sopenharmony_ci * fails with the requested priority, for example because the caller 520bf215546Sopenharmony_ci * lacks CAP_SYS_NICE capability or other system resource constraints, 521bf215546Sopenharmony_ci * fallback to normal priority. 522bf215546Sopenharmony_ci */ 523bf215546Sopenharmony_ci priority = RADEON_CTX_PRIORITY_MEDIUM; 524bf215546Sopenharmony_ci sctx->ctx = sctx->ws->ctx_create(sctx->ws, priority); 525bf215546Sopenharmony_ci } 526bf215546Sopenharmony_ci if (!sctx->ctx) { 527bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create radeon_winsys_ctx\n"); 528bf215546Sopenharmony_ci goto fail; 529bf215546Sopenharmony_ci } 530bf215546Sopenharmony_ci 531bf215546Sopenharmony_ci ws->cs_create(&sctx->gfx_cs, sctx->ctx, sctx->has_graphics ? AMD_IP_GFX : AMD_IP_COMPUTE, 532bf215546Sopenharmony_ci (void *)si_flush_gfx_cs, sctx, stop_exec_on_failure); 533bf215546Sopenharmony_ci 534bf215546Sopenharmony_ci /* Initialize private allocators. */ 535bf215546Sopenharmony_ci u_suballocator_init(&sctx->allocator_zeroed_memory, &sctx->b, 128 * 1024, 0, 536bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 537bf215546Sopenharmony_ci SI_RESOURCE_FLAG_CLEAR | SI_RESOURCE_FLAG_32BIT, false); 538bf215546Sopenharmony_ci 539bf215546Sopenharmony_ci sctx->cached_gtt_allocator = u_upload_create(&sctx->b, 16 * 1024, 0, PIPE_USAGE_STAGING, 0); 540bf215546Sopenharmony_ci if (!sctx->cached_gtt_allocator) { 541bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create cached_gtt_allocator\n"); 542bf215546Sopenharmony_ci goto fail; 543bf215546Sopenharmony_ci } 544bf215546Sopenharmony_ci 545bf215546Sopenharmony_ci /* Initialize public allocators. */ 546bf215546Sopenharmony_ci /* Unify uploaders as follows: 547bf215546Sopenharmony_ci * - dGPUs with Smart Access Memory: there is only one uploader instance writing to VRAM. 548bf215546Sopenharmony_ci * - APUs: There is only one uploader instance writing to RAM. VRAM has the same perf on APUs. 549bf215546Sopenharmony_ci * - Other chips: The const uploader writes to VRAM and the stream uploader writes to RAM. 550bf215546Sopenharmony_ci */ 551bf215546Sopenharmony_ci bool smart_access_memory = sscreen->info.smart_access_memory; 552bf215546Sopenharmony_ci bool is_apu = !sscreen->info.has_dedicated_vram; 553bf215546Sopenharmony_ci sctx->b.stream_uploader = 554bf215546Sopenharmony_ci u_upload_create(&sctx->b, 1024 * 1024, 0, 555bf215546Sopenharmony_ci smart_access_memory && !is_apu ? PIPE_USAGE_DEFAULT : PIPE_USAGE_STREAM, 556bf215546Sopenharmony_ci SI_RESOURCE_FLAG_32BIT); /* same flags as const_uploader */ 557bf215546Sopenharmony_ci if (!sctx->b.stream_uploader) { 558bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create stream_uploader\n"); 559bf215546Sopenharmony_ci goto fail; 560bf215546Sopenharmony_ci } 561bf215546Sopenharmony_ci 562bf215546Sopenharmony_ci if (smart_access_memory || is_apu) { 563bf215546Sopenharmony_ci sctx->b.const_uploader = sctx->b.stream_uploader; 564bf215546Sopenharmony_ci } else { 565bf215546Sopenharmony_ci sctx->b.const_uploader = 566bf215546Sopenharmony_ci u_upload_create(&sctx->b, 256 * 1024, 0, PIPE_USAGE_DEFAULT, 567bf215546Sopenharmony_ci SI_RESOURCE_FLAG_32BIT); 568bf215546Sopenharmony_ci if (!sctx->b.const_uploader) { 569bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create const_uploader\n"); 570bf215546Sopenharmony_ci goto fail; 571bf215546Sopenharmony_ci } 572bf215546Sopenharmony_ci } 573bf215546Sopenharmony_ci 574bf215546Sopenharmony_ci /* Border colors. */ 575bf215546Sopenharmony_ci if (sscreen->info.has_3d_cube_border_color_mipmap) { 576bf215546Sopenharmony_ci sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table)); 577bf215546Sopenharmony_ci if (!sctx->border_color_table) { 578bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create border_color_table\n"); 579bf215546Sopenharmony_ci goto fail; 580bf215546Sopenharmony_ci } 581bf215546Sopenharmony_ci 582bf215546Sopenharmony_ci sctx->border_color_buffer = si_resource(pipe_buffer_create( 583bf215546Sopenharmony_ci screen, 0, PIPE_USAGE_DEFAULT, SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table))); 584bf215546Sopenharmony_ci if (!sctx->border_color_buffer) { 585bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create border_color_buffer\n"); 586bf215546Sopenharmony_ci goto fail; 587bf215546Sopenharmony_ci } 588bf215546Sopenharmony_ci 589bf215546Sopenharmony_ci sctx->border_color_map = 590bf215546Sopenharmony_ci ws->buffer_map(ws, sctx->border_color_buffer->buf, NULL, PIPE_MAP_WRITE); 591bf215546Sopenharmony_ci if (!sctx->border_color_map) { 592bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't map border_color_buffer\n"); 593bf215546Sopenharmony_ci goto fail; 594bf215546Sopenharmony_ci } 595bf215546Sopenharmony_ci } 596bf215546Sopenharmony_ci 597bf215546Sopenharmony_ci sctx->ngg = sscreen->use_ngg; 598bf215546Sopenharmony_ci si_shader_change_notify(sctx); 599bf215546Sopenharmony_ci 600bf215546Sopenharmony_ci /* Initialize context functions used by graphics and compute. */ 601bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) 602bf215546Sopenharmony_ci sctx->emit_cache_flush = gfx10_emit_cache_flush; 603bf215546Sopenharmony_ci else 604bf215546Sopenharmony_ci sctx->emit_cache_flush = si_emit_cache_flush; 605bf215546Sopenharmony_ci 606bf215546Sopenharmony_ci sctx->b.emit_string_marker = si_emit_string_marker; 607bf215546Sopenharmony_ci sctx->b.set_debug_callback = si_set_debug_callback; 608bf215546Sopenharmony_ci sctx->b.set_log_context = si_set_log_context; 609bf215546Sopenharmony_ci sctx->b.set_context_param = si_set_context_param; 610bf215546Sopenharmony_ci sctx->b.get_device_reset_status = si_get_reset_status; 611bf215546Sopenharmony_ci sctx->b.set_device_reset_callback = si_set_device_reset_callback; 612bf215546Sopenharmony_ci sctx->b.set_frontend_noop = si_set_frontend_noop; 613bf215546Sopenharmony_ci 614bf215546Sopenharmony_ci si_init_all_descriptors(sctx); 615bf215546Sopenharmony_ci si_init_buffer_functions(sctx); 616bf215546Sopenharmony_ci si_init_clear_functions(sctx); 617bf215546Sopenharmony_ci si_init_blit_functions(sctx); 618bf215546Sopenharmony_ci si_init_compute_functions(sctx); 619bf215546Sopenharmony_ci si_init_compute_blit_functions(sctx); 620bf215546Sopenharmony_ci si_init_debug_functions(sctx); 621bf215546Sopenharmony_ci si_init_fence_functions(sctx); 622bf215546Sopenharmony_ci si_init_query_functions(sctx); 623bf215546Sopenharmony_ci si_init_state_compute_functions(sctx); 624bf215546Sopenharmony_ci si_init_context_texture_functions(sctx); 625bf215546Sopenharmony_ci 626bf215546Sopenharmony_ci /* Initialize graphics-only context functions. */ 627bf215546Sopenharmony_ci if (sctx->has_graphics) { 628bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX10) 629bf215546Sopenharmony_ci gfx10_init_query(sctx); 630bf215546Sopenharmony_ci si_init_msaa_functions(sctx); 631bf215546Sopenharmony_ci si_init_shader_functions(sctx); 632bf215546Sopenharmony_ci si_init_state_functions(sctx); 633bf215546Sopenharmony_ci si_init_streamout_functions(sctx); 634bf215546Sopenharmony_ci si_init_viewport_functions(sctx); 635bf215546Sopenharmony_ci si_init_spi_map_functions(sctx); 636bf215546Sopenharmony_ci 637bf215546Sopenharmony_ci sctx->blitter = util_blitter_create(&sctx->b); 638bf215546Sopenharmony_ci if (sctx->blitter == NULL) { 639bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create blitter\n"); 640bf215546Sopenharmony_ci goto fail; 641bf215546Sopenharmony_ci } 642bf215546Sopenharmony_ci sctx->blitter->skip_viewport_restore = true; 643bf215546Sopenharmony_ci 644bf215546Sopenharmony_ci /* Some states are expected to be always non-NULL. */ 645bf215546Sopenharmony_ci sctx->noop_blend = util_blitter_get_noop_blend_state(sctx->blitter); 646bf215546Sopenharmony_ci sctx->queued.named.blend = sctx->noop_blend; 647bf215546Sopenharmony_ci 648bf215546Sopenharmony_ci sctx->noop_dsa = util_blitter_get_noop_dsa_state(sctx->blitter); 649bf215546Sopenharmony_ci sctx->queued.named.dsa = sctx->noop_dsa; 650bf215546Sopenharmony_ci 651bf215546Sopenharmony_ci sctx->no_velems_state = sctx->b.create_vertex_elements_state(&sctx->b, 0, NULL); 652bf215546Sopenharmony_ci sctx->vertex_elements = sctx->no_velems_state; 653bf215546Sopenharmony_ci 654bf215546Sopenharmony_ci sctx->discard_rasterizer_state = util_blitter_get_discard_rasterizer_state(sctx->blitter); 655bf215546Sopenharmony_ci sctx->queued.named.rasterizer = sctx->discard_rasterizer_state; 656bf215546Sopenharmony_ci 657bf215546Sopenharmony_ci switch (sctx->gfx_level) { 658bf215546Sopenharmony_ci case GFX6: 659bf215546Sopenharmony_ci si_init_draw_functions_GFX6(sctx); 660bf215546Sopenharmony_ci break; 661bf215546Sopenharmony_ci case GFX7: 662bf215546Sopenharmony_ci si_init_draw_functions_GFX7(sctx); 663bf215546Sopenharmony_ci break; 664bf215546Sopenharmony_ci case GFX8: 665bf215546Sopenharmony_ci si_init_draw_functions_GFX8(sctx); 666bf215546Sopenharmony_ci break; 667bf215546Sopenharmony_ci case GFX9: 668bf215546Sopenharmony_ci si_init_draw_functions_GFX9(sctx); 669bf215546Sopenharmony_ci break; 670bf215546Sopenharmony_ci case GFX10: 671bf215546Sopenharmony_ci si_init_draw_functions_GFX10(sctx); 672bf215546Sopenharmony_ci break; 673bf215546Sopenharmony_ci case GFX10_3: 674bf215546Sopenharmony_ci si_init_draw_functions_GFX10_3(sctx); 675bf215546Sopenharmony_ci break; 676bf215546Sopenharmony_ci case GFX11: 677bf215546Sopenharmony_ci si_init_draw_functions_GFX11(sctx); 678bf215546Sopenharmony_ci break; 679bf215546Sopenharmony_ci default: 680bf215546Sopenharmony_ci unreachable("unhandled gfx level"); 681bf215546Sopenharmony_ci } 682bf215546Sopenharmony_ci } 683bf215546Sopenharmony_ci 684bf215546Sopenharmony_ci sctx->sample_mask = 0xffff; 685bf215546Sopenharmony_ci 686bf215546Sopenharmony_ci /* Initialize multimedia functions. */ 687bf215546Sopenharmony_ci if (sscreen->info.ip[AMD_IP_UVD].num_queues || sscreen->info.has_video_hw.vcn_decode || 688bf215546Sopenharmony_ci sscreen->info.ip[AMD_IP_VCN_JPEG].num_queues || sscreen->info.ip[AMD_IP_VCE].num_queues || 689bf215546Sopenharmony_ci sscreen->info.ip[AMD_IP_UVD_ENC].num_queues || sscreen->info.ip[AMD_IP_VCN_ENC].num_queues) { 690bf215546Sopenharmony_ci sctx->b.create_video_codec = si_uvd_create_decoder; 691bf215546Sopenharmony_ci sctx->b.create_video_buffer = si_video_buffer_create; 692bf215546Sopenharmony_ci if (screen->resource_create_with_modifiers) 693bf215546Sopenharmony_ci sctx->b.create_video_buffer_with_modifiers = si_video_buffer_create_with_modifiers; 694bf215546Sopenharmony_ci } else { 695bf215546Sopenharmony_ci sctx->b.create_video_codec = vl_create_decoder; 696bf215546Sopenharmony_ci sctx->b.create_video_buffer = vl_video_buffer_create; 697bf215546Sopenharmony_ci } 698bf215546Sopenharmony_ci 699bf215546Sopenharmony_ci /* GFX7 cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads 700bf215546Sopenharmony_ci * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */ 701bf215546Sopenharmony_ci if (sctx->gfx_level == GFX7) { 702bf215546Sopenharmony_ci sctx->null_const_buf.buffer = 703bf215546Sopenharmony_ci pipe_aligned_buffer_create(screen, 704bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_32BIT | 705bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DRIVER_INTERNAL, 706bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 16, 707bf215546Sopenharmony_ci sctx->screen->info.tcc_cache_line_size); 708bf215546Sopenharmony_ci if (!sctx->null_const_buf.buffer) { 709bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create null_const_buf\n"); 710bf215546Sopenharmony_ci goto fail; 711bf215546Sopenharmony_ci } 712bf215546Sopenharmony_ci sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0; 713bf215546Sopenharmony_ci 714bf215546Sopenharmony_ci unsigned start_shader = sctx->has_graphics ? 0 : PIPE_SHADER_COMPUTE; 715bf215546Sopenharmony_ci for (shader = start_shader; shader < SI_NUM_SHADERS; shader++) { 716bf215546Sopenharmony_ci for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) { 717bf215546Sopenharmony_ci sctx->b.set_constant_buffer(&sctx->b, shader, i, false, &sctx->null_const_buf); 718bf215546Sopenharmony_ci } 719bf215546Sopenharmony_ci } 720bf215546Sopenharmony_ci 721bf215546Sopenharmony_ci si_set_internal_const_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &sctx->null_const_buf); 722bf215546Sopenharmony_ci si_set_internal_const_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &sctx->null_const_buf); 723bf215546Sopenharmony_ci si_set_internal_const_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &sctx->null_const_buf); 724bf215546Sopenharmony_ci si_set_internal_const_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &sctx->null_const_buf); 725bf215546Sopenharmony_ci si_set_internal_const_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &sctx->null_const_buf); 726bf215546Sopenharmony_ci } 727bf215546Sopenharmony_ci 728bf215546Sopenharmony_ci /* Bindless handles. */ 729bf215546Sopenharmony_ci sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); 730bf215546Sopenharmony_ci sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); 731bf215546Sopenharmony_ci 732bf215546Sopenharmony_ci util_dynarray_init(&sctx->resident_tex_handles, NULL); 733bf215546Sopenharmony_ci util_dynarray_init(&sctx->resident_img_handles, NULL); 734bf215546Sopenharmony_ci util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL); 735bf215546Sopenharmony_ci util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL); 736bf215546Sopenharmony_ci util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL); 737bf215546Sopenharmony_ci 738bf215546Sopenharmony_ci sctx->dirty_implicit_resources = _mesa_pointer_hash_table_create(NULL); 739bf215546Sopenharmony_ci if (!sctx->dirty_implicit_resources) { 740bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create dirty_implicit_resources\n"); 741bf215546Sopenharmony_ci goto fail; 742bf215546Sopenharmony_ci } 743bf215546Sopenharmony_ci 744bf215546Sopenharmony_ci /* The remainder of this function initializes the gfx CS and must be last. */ 745bf215546Sopenharmony_ci assert(sctx->gfx_cs.current.cdw == 0); 746bf215546Sopenharmony_ci 747bf215546Sopenharmony_ci if (sctx->has_graphics) { 748bf215546Sopenharmony_ci si_init_cp_reg_shadowing(sctx); 749bf215546Sopenharmony_ci } 750bf215546Sopenharmony_ci 751bf215546Sopenharmony_ci /* Set immutable fields of shader keys. */ 752bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX9) { 753bf215546Sopenharmony_ci /* The LS output / HS input layout can be communicated 754bf215546Sopenharmony_ci * directly instead of via user SGPRs for merged LS-HS. 755bf215546Sopenharmony_ci * This also enables jumping over the VS prolog for HS-only waves. 756bf215546Sopenharmony_ci * 757bf215546Sopenharmony_ci * When the LS VGPR fix is needed, monolithic shaders can: 758bf215546Sopenharmony_ci * - avoid initializing EXEC in both the LS prolog 759bf215546Sopenharmony_ci * and the LS main part when !vs_needs_prolog 760bf215546Sopenharmony_ci * - remove the fixup for unused input VGPRs 761bf215546Sopenharmony_ci */ 762bf215546Sopenharmony_ci sctx->shader.tcs.key.ge.opt.prefer_mono = 1; 763bf215546Sopenharmony_ci 764bf215546Sopenharmony_ci /* This enables jumping over the VS prolog for GS-only waves. */ 765bf215546Sopenharmony_ci sctx->shader.gs.key.ge.opt.prefer_mono = 1; 766bf215546Sopenharmony_ci } 767bf215546Sopenharmony_ci 768bf215546Sopenharmony_ci si_begin_new_gfx_cs(sctx, true); 769bf215546Sopenharmony_ci assert(sctx->gfx_cs.current.cdw == sctx->initial_gfx_cs_size); 770bf215546Sopenharmony_ci 771bf215546Sopenharmony_ci if (sctx->gfx_level >= GFX9 && sctx->gfx_level < GFX11) { 772bf215546Sopenharmony_ci sctx->wait_mem_scratch = 773bf215546Sopenharmony_ci si_aligned_buffer_create(screen, 774bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | 775bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DRIVER_INTERNAL, 776bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 4, 777bf215546Sopenharmony_ci sscreen->info.tcc_cache_line_size); 778bf215546Sopenharmony_ci if (!sctx->wait_mem_scratch) { 779bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: can't create wait_mem_scratch\n"); 780bf215546Sopenharmony_ci goto fail; 781bf215546Sopenharmony_ci } 782bf215546Sopenharmony_ci 783bf215546Sopenharmony_ci si_cp_write_data(sctx, sctx->wait_mem_scratch, 0, 4, V_370_MEM, V_370_ME, 784bf215546Sopenharmony_ci &sctx->wait_mem_number); 785bf215546Sopenharmony_ci } 786bf215546Sopenharmony_ci 787bf215546Sopenharmony_ci if (sctx->gfx_level == GFX7) { 788bf215546Sopenharmony_ci /* Clear the NULL constant buffer, because loads should return zeros. 789bf215546Sopenharmony_ci * Note that this forces CP DMA to be used, because clover deadlocks 790bf215546Sopenharmony_ci * for some reason when the compute codepath is used. 791bf215546Sopenharmony_ci */ 792bf215546Sopenharmony_ci uint32_t clear_value = 0; 793bf215546Sopenharmony_ci si_clear_buffer(sctx, sctx->null_const_buf.buffer, 0, sctx->null_const_buf.buffer->width0, 794bf215546Sopenharmony_ci &clear_value, 4, SI_OP_SYNC_AFTER, SI_COHERENCY_SHADER, 795bf215546Sopenharmony_ci SI_CP_DMA_CLEAR_METHOD); 796bf215546Sopenharmony_ci } 797bf215546Sopenharmony_ci 798bf215546Sopenharmony_ci if (!(flags & SI_CONTEXT_FLAG_AUX)) { 799bf215546Sopenharmony_ci p_atomic_inc(&screen->num_contexts); 800bf215546Sopenharmony_ci 801bf215546Sopenharmony_ci /* Check if the aux_context needs to be recreated */ 802bf215546Sopenharmony_ci struct si_context *saux = si_get_aux_context(sscreen); 803bf215546Sopenharmony_ci 804bf215546Sopenharmony_ci enum pipe_reset_status status = sctx->ws->ctx_query_reset_status( 805bf215546Sopenharmony_ci saux->ctx, true, NULL); 806bf215546Sopenharmony_ci if (status != PIPE_NO_RESET) { 807bf215546Sopenharmony_ci /* We lost the aux_context, create a new one */ 808bf215546Sopenharmony_ci struct u_log_context *aux_log = (saux)->log; 809bf215546Sopenharmony_ci saux->b.set_log_context(&saux->b, NULL); 810bf215546Sopenharmony_ci saux->b.destroy(&saux->b); 811bf215546Sopenharmony_ci 812bf215546Sopenharmony_ci saux = (struct si_context *)si_create_context( 813bf215546Sopenharmony_ci &sscreen->b, SI_CONTEXT_FLAG_AUX | 814bf215546Sopenharmony_ci (sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) | 815bf215546Sopenharmony_ci (sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY)); 816bf215546Sopenharmony_ci saux->b.set_log_context(&saux->b, aux_log); 817bf215546Sopenharmony_ci sscreen->aux_context = saux; 818bf215546Sopenharmony_ci } 819bf215546Sopenharmony_ci si_put_aux_context_flush(sscreen); 820bf215546Sopenharmony_ci 821bf215546Sopenharmony_ci simple_mtx_lock(&sscreen->async_compute_context_lock); 822bf215546Sopenharmony_ci if (status != PIPE_NO_RESET && sscreen->async_compute_context) { 823bf215546Sopenharmony_ci sscreen->async_compute_context->destroy(sscreen->async_compute_context); 824bf215546Sopenharmony_ci sscreen->async_compute_context = NULL; 825bf215546Sopenharmony_ci } 826bf215546Sopenharmony_ci simple_mtx_unlock(&sscreen->async_compute_context_lock); 827bf215546Sopenharmony_ci } 828bf215546Sopenharmony_ci 829bf215546Sopenharmony_ci sctx->initial_gfx_cs_size = sctx->gfx_cs.current.cdw; 830bf215546Sopenharmony_ci return &sctx->b; 831bf215546Sopenharmony_cifail: 832bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: Failed to create a context.\n"); 833bf215546Sopenharmony_ci si_destroy_context(&sctx->b); 834bf215546Sopenharmony_ci return NULL; 835bf215546Sopenharmony_ci} 836bf215546Sopenharmony_ci 837bf215546Sopenharmony_cistatic bool si_is_resource_busy(struct pipe_screen *screen, struct pipe_resource *resource, 838bf215546Sopenharmony_ci unsigned usage) 839bf215546Sopenharmony_ci{ 840bf215546Sopenharmony_ci struct radeon_winsys *ws = ((struct si_screen *)screen)->ws; 841bf215546Sopenharmony_ci 842bf215546Sopenharmony_ci return !ws->buffer_wait(ws, si_resource(resource)->buf, 0, 843bf215546Sopenharmony_ci /* If mapping for write, we need to wait for all reads and writes. 844bf215546Sopenharmony_ci * If mapping for read, we only need to wait for writes. 845bf215546Sopenharmony_ci */ 846bf215546Sopenharmony_ci usage & PIPE_MAP_WRITE ? RADEON_USAGE_READWRITE : RADEON_USAGE_WRITE); 847bf215546Sopenharmony_ci} 848bf215546Sopenharmony_ci 849bf215546Sopenharmony_cistatic struct pipe_context *si_pipe_create_context(struct pipe_screen *screen, void *priv, 850bf215546Sopenharmony_ci unsigned flags) 851bf215546Sopenharmony_ci{ 852bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 853bf215546Sopenharmony_ci struct pipe_context *ctx; 854bf215546Sopenharmony_ci 855bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(CHECK_VM)) 856bf215546Sopenharmony_ci flags |= PIPE_CONTEXT_DEBUG; 857bf215546Sopenharmony_ci 858bf215546Sopenharmony_ci ctx = si_create_context(screen, flags); 859bf215546Sopenharmony_ci 860bf215546Sopenharmony_ci if (ctx && sscreen->info.gfx_level >= GFX9 && sscreen->debug_flags & DBG(SQTT)) { 861bf215546Sopenharmony_ci if (ac_check_profile_state(&sscreen->info)) { 862bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: Canceling RGP trace request as a hang condition has been " 863bf215546Sopenharmony_ci "detected. Force the GPU into a profiling mode with e.g. " 864bf215546Sopenharmony_ci "\"echo profile_peak > " 865bf215546Sopenharmony_ci "/sys/class/drm/card0/device/power_dpm_force_performance_level\"\n"); 866bf215546Sopenharmony_ci } else if (!si_init_thread_trace((struct si_context *)ctx)) { 867bf215546Sopenharmony_ci FREE(ctx); 868bf215546Sopenharmony_ci return NULL; 869bf215546Sopenharmony_ci } 870bf215546Sopenharmony_ci } 871bf215546Sopenharmony_ci 872bf215546Sopenharmony_ci if (!(flags & PIPE_CONTEXT_PREFER_THREADED)) 873bf215546Sopenharmony_ci return ctx; 874bf215546Sopenharmony_ci 875bf215546Sopenharmony_ci /* Clover (compute-only) is unsupported. */ 876bf215546Sopenharmony_ci if (flags & PIPE_CONTEXT_COMPUTE_ONLY) 877bf215546Sopenharmony_ci return ctx; 878bf215546Sopenharmony_ci 879bf215546Sopenharmony_ci /* When shaders are logged to stderr, asynchronous compilation is 880bf215546Sopenharmony_ci * disabled too. */ 881bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG_ALL_SHADERS) 882bf215546Sopenharmony_ci return ctx; 883bf215546Sopenharmony_ci 884bf215546Sopenharmony_ci /* Use asynchronous flushes only on amdgpu, since the radeon 885bf215546Sopenharmony_ci * implementation for fence_server_sync is incomplete. */ 886bf215546Sopenharmony_ci struct pipe_context *tc = 887bf215546Sopenharmony_ci threaded_context_create(ctx, &sscreen->pool_transfers, 888bf215546Sopenharmony_ci si_replace_buffer_storage, 889bf215546Sopenharmony_ci &(struct threaded_context_options){ 890bf215546Sopenharmony_ci .create_fence = sscreen->info.is_amdgpu ? 891bf215546Sopenharmony_ci si_create_fence : NULL, 892bf215546Sopenharmony_ci .is_resource_busy = si_is_resource_busy, 893bf215546Sopenharmony_ci .driver_calls_flush_notify = true, 894bf215546Sopenharmony_ci }, 895bf215546Sopenharmony_ci &((struct si_context *)ctx)->tc); 896bf215546Sopenharmony_ci 897bf215546Sopenharmony_ci if (tc && tc != ctx) 898bf215546Sopenharmony_ci threaded_context_init_bytes_mapped_limit((struct threaded_context *)tc, 4); 899bf215546Sopenharmony_ci 900bf215546Sopenharmony_ci return tc; 901bf215546Sopenharmony_ci} 902bf215546Sopenharmony_ci 903bf215546Sopenharmony_ci/* 904bf215546Sopenharmony_ci * pipe_screen 905bf215546Sopenharmony_ci */ 906bf215546Sopenharmony_cistatic void si_destroy_screen(struct pipe_screen *pscreen) 907bf215546Sopenharmony_ci{ 908bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)pscreen; 909bf215546Sopenharmony_ci struct si_shader_part *parts[] = {sscreen->vs_prologs, sscreen->tcs_epilogs, 910bf215546Sopenharmony_ci sscreen->ps_prologs, sscreen->ps_epilogs}; 911bf215546Sopenharmony_ci unsigned i; 912bf215546Sopenharmony_ci 913bf215546Sopenharmony_ci if (!sscreen->ws->unref(sscreen->ws)) 914bf215546Sopenharmony_ci return; 915bf215546Sopenharmony_ci 916bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(CACHE_STATS)) { 917bf215546Sopenharmony_ci printf("live shader cache: hits = %u, misses = %u\n", sscreen->live_shader_cache.hits, 918bf215546Sopenharmony_ci sscreen->live_shader_cache.misses); 919bf215546Sopenharmony_ci printf("memory shader cache: hits = %u, misses = %u\n", sscreen->num_memory_shader_cache_hits, 920bf215546Sopenharmony_ci sscreen->num_memory_shader_cache_misses); 921bf215546Sopenharmony_ci printf("disk shader cache: hits = %u, misses = %u\n", sscreen->num_disk_shader_cache_hits, 922bf215546Sopenharmony_ci sscreen->num_disk_shader_cache_misses); 923bf215546Sopenharmony_ci } 924bf215546Sopenharmony_ci 925bf215546Sopenharmony_ci si_resource_reference(&sscreen->attribute_ring, NULL); 926bf215546Sopenharmony_ci 927bf215546Sopenharmony_ci if (sscreen->aux_context) { 928bf215546Sopenharmony_ci struct si_context *saux = si_get_aux_context(sscreen); 929bf215546Sopenharmony_ci struct u_log_context *aux_log = saux->log; 930bf215546Sopenharmony_ci if (aux_log) { 931bf215546Sopenharmony_ci saux->b.set_log_context(&saux->b, NULL); 932bf215546Sopenharmony_ci u_log_context_destroy(aux_log); 933bf215546Sopenharmony_ci FREE(aux_log); 934bf215546Sopenharmony_ci } 935bf215546Sopenharmony_ci 936bf215546Sopenharmony_ci saux->b.destroy(&saux->b); 937bf215546Sopenharmony_ci mtx_unlock(&sscreen->aux_context_lock); 938bf215546Sopenharmony_ci } 939bf215546Sopenharmony_ci mtx_destroy(&sscreen->aux_context_lock); 940bf215546Sopenharmony_ci 941bf215546Sopenharmony_ci simple_mtx_destroy(&sscreen->async_compute_context_lock); 942bf215546Sopenharmony_ci if (sscreen->async_compute_context) { 943bf215546Sopenharmony_ci sscreen->async_compute_context->destroy(sscreen->async_compute_context); 944bf215546Sopenharmony_ci } 945bf215546Sopenharmony_ci 946bf215546Sopenharmony_ci util_queue_destroy(&sscreen->shader_compiler_queue); 947bf215546Sopenharmony_ci util_queue_destroy(&sscreen->shader_compiler_queue_low_priority); 948bf215546Sopenharmony_ci 949bf215546Sopenharmony_ci /* Release the reference on glsl types of the compiler threads. */ 950bf215546Sopenharmony_ci glsl_type_singleton_decref(); 951bf215546Sopenharmony_ci 952bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sscreen->compiler); i++) 953bf215546Sopenharmony_ci si_destroy_compiler(&sscreen->compiler[i]); 954bf215546Sopenharmony_ci 955bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(sscreen->compiler_lowp); i++) 956bf215546Sopenharmony_ci si_destroy_compiler(&sscreen->compiler_lowp[i]); 957bf215546Sopenharmony_ci 958bf215546Sopenharmony_ci /* Free shader parts. */ 959bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(parts); i++) { 960bf215546Sopenharmony_ci while (parts[i]) { 961bf215546Sopenharmony_ci struct si_shader_part *part = parts[i]; 962bf215546Sopenharmony_ci 963bf215546Sopenharmony_ci parts[i] = part->next; 964bf215546Sopenharmony_ci si_shader_binary_clean(&part->binary); 965bf215546Sopenharmony_ci FREE(part); 966bf215546Sopenharmony_ci } 967bf215546Sopenharmony_ci } 968bf215546Sopenharmony_ci simple_mtx_destroy(&sscreen->shader_parts_mutex); 969bf215546Sopenharmony_ci si_destroy_shader_cache(sscreen); 970bf215546Sopenharmony_ci 971bf215546Sopenharmony_ci si_destroy_perfcounters(sscreen); 972bf215546Sopenharmony_ci si_gpu_load_kill_thread(sscreen); 973bf215546Sopenharmony_ci 974bf215546Sopenharmony_ci simple_mtx_destroy(&sscreen->gpu_load_mutex); 975bf215546Sopenharmony_ci simple_mtx_destroy(&sscreen->gds_mutex); 976bf215546Sopenharmony_ci 977bf215546Sopenharmony_ci radeon_bo_reference(sscreen->ws, &sscreen->gds, NULL); 978bf215546Sopenharmony_ci radeon_bo_reference(sscreen->ws, &sscreen->gds_oa, NULL); 979bf215546Sopenharmony_ci 980bf215546Sopenharmony_ci slab_destroy_parent(&sscreen->pool_transfers); 981bf215546Sopenharmony_ci 982bf215546Sopenharmony_ci disk_cache_destroy(sscreen->disk_shader_cache); 983bf215546Sopenharmony_ci util_live_shader_cache_deinit(&sscreen->live_shader_cache); 984bf215546Sopenharmony_ci util_idalloc_mt_fini(&sscreen->buffer_ids); 985bf215546Sopenharmony_ci util_vertex_state_cache_deinit(&sscreen->vertex_state_cache); 986bf215546Sopenharmony_ci 987bf215546Sopenharmony_ci sscreen->ws->destroy(sscreen->ws); 988bf215546Sopenharmony_ci FREE(sscreen); 989bf215546Sopenharmony_ci} 990bf215546Sopenharmony_ci 991bf215546Sopenharmony_cistatic void si_init_gs_info(struct si_screen *sscreen) 992bf215546Sopenharmony_ci{ 993bf215546Sopenharmony_ci sscreen->gs_table_depth = ac_get_gs_table_depth(sscreen->info.gfx_level, sscreen->info.family); 994bf215546Sopenharmony_ci} 995bf215546Sopenharmony_ci 996bf215546Sopenharmony_cistatic void si_test_vmfault(struct si_screen *sscreen, uint64_t test_flags) 997bf215546Sopenharmony_ci{ 998bf215546Sopenharmony_ci struct pipe_context *ctx = sscreen->aux_context; 999bf215546Sopenharmony_ci struct si_context *sctx = (struct si_context *)ctx; 1000bf215546Sopenharmony_ci struct pipe_resource *buf = pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64); 1001bf215546Sopenharmony_ci 1002bf215546Sopenharmony_ci if (!buf) { 1003bf215546Sopenharmony_ci puts("Buffer allocation failed."); 1004bf215546Sopenharmony_ci exit(1); 1005bf215546Sopenharmony_ci } 1006bf215546Sopenharmony_ci 1007bf215546Sopenharmony_ci si_resource(buf)->gpu_address = 0; /* cause a VM fault */ 1008bf215546Sopenharmony_ci 1009bf215546Sopenharmony_ci if (test_flags & DBG(TEST_VMFAULT_CP)) { 1010bf215546Sopenharmony_ci si_cp_dma_copy_buffer(sctx, buf, buf, 0, 4, 4, SI_OP_SYNC_BEFORE_AFTER, 1011bf215546Sopenharmony_ci SI_COHERENCY_NONE, L2_BYPASS); 1012bf215546Sopenharmony_ci ctx->flush(ctx, NULL, 0); 1013bf215546Sopenharmony_ci puts("VM fault test: CP - done."); 1014bf215546Sopenharmony_ci } 1015bf215546Sopenharmony_ci if (test_flags & DBG(TEST_VMFAULT_SHADER)) { 1016bf215546Sopenharmony_ci util_test_constant_buffer(ctx, buf); 1017bf215546Sopenharmony_ci puts("VM fault test: Shader - done."); 1018bf215546Sopenharmony_ci } 1019bf215546Sopenharmony_ci exit(0); 1020bf215546Sopenharmony_ci} 1021bf215546Sopenharmony_ci 1022bf215546Sopenharmony_cistatic void si_test_gds_memory_management(struct si_context *sctx, unsigned alloc_size, 1023bf215546Sopenharmony_ci unsigned alignment, enum radeon_bo_domain domain) 1024bf215546Sopenharmony_ci{ 1025bf215546Sopenharmony_ci struct radeon_winsys *ws = sctx->ws; 1026bf215546Sopenharmony_ci struct radeon_cmdbuf cs[8]; 1027bf215546Sopenharmony_ci struct pb_buffer *gds_bo[ARRAY_SIZE(cs)]; 1028bf215546Sopenharmony_ci 1029bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) { 1030bf215546Sopenharmony_ci ws->cs_create(&cs[i], sctx->ctx, AMD_IP_COMPUTE, NULL, NULL, false); 1031bf215546Sopenharmony_ci gds_bo[i] = ws->buffer_create(ws, alloc_size, alignment, domain, 0); 1032bf215546Sopenharmony_ci assert(gds_bo[i]); 1033bf215546Sopenharmony_ci } 1034bf215546Sopenharmony_ci 1035bf215546Sopenharmony_ci for (unsigned iterations = 0; iterations < 20000; iterations++) { 1036bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) { 1037bf215546Sopenharmony_ci /* This clears GDS with CP DMA. 1038bf215546Sopenharmony_ci * 1039bf215546Sopenharmony_ci * We don't care if GDS is present. Just add some packet 1040bf215546Sopenharmony_ci * to make the GPU busy for a moment. 1041bf215546Sopenharmony_ci */ 1042bf215546Sopenharmony_ci si_cp_dma_clear_buffer( 1043bf215546Sopenharmony_ci sctx, &cs[i], NULL, 0, alloc_size, 0, 1044bf215546Sopenharmony_ci SI_OP_CPDMA_SKIP_CHECK_CS_SPACE, 0, 1045bf215546Sopenharmony_ci 0); 1046bf215546Sopenharmony_ci 1047bf215546Sopenharmony_ci ws->cs_add_buffer(&cs[i], gds_bo[i], RADEON_USAGE_READWRITE, domain); 1048bf215546Sopenharmony_ci ws->cs_flush(&cs[i], PIPE_FLUSH_ASYNC, NULL); 1049bf215546Sopenharmony_ci } 1050bf215546Sopenharmony_ci } 1051bf215546Sopenharmony_ci exit(0); 1052bf215546Sopenharmony_ci} 1053bf215546Sopenharmony_ci 1054bf215546Sopenharmony_cistatic void si_disk_cache_create(struct si_screen *sscreen) 1055bf215546Sopenharmony_ci{ 1056bf215546Sopenharmony_ci /* Don't use the cache if shader dumping is enabled. */ 1057bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG_ALL_SHADERS) 1058bf215546Sopenharmony_ci return; 1059bf215546Sopenharmony_ci 1060bf215546Sopenharmony_ci struct mesa_sha1 ctx; 1061bf215546Sopenharmony_ci unsigned char sha1[20]; 1062bf215546Sopenharmony_ci char cache_id[20 * 2 + 1]; 1063bf215546Sopenharmony_ci 1064bf215546Sopenharmony_ci _mesa_sha1_init(&ctx); 1065bf215546Sopenharmony_ci 1066bf215546Sopenharmony_ci if (!disk_cache_get_function_identifier(si_disk_cache_create, &ctx) || 1067bf215546Sopenharmony_ci !disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx)) 1068bf215546Sopenharmony_ci return; 1069bf215546Sopenharmony_ci 1070bf215546Sopenharmony_ci _mesa_sha1_final(&ctx, sha1); 1071bf215546Sopenharmony_ci disk_cache_format_hex_id(cache_id, sha1, 20 * 2); 1072bf215546Sopenharmony_ci 1073bf215546Sopenharmony_ci sscreen->disk_shader_cache = disk_cache_create(sscreen->info.name, cache_id, 1074bf215546Sopenharmony_ci sscreen->info.address32_hi); 1075bf215546Sopenharmony_ci} 1076bf215546Sopenharmony_ci 1077bf215546Sopenharmony_cistatic void si_set_max_shader_compiler_threads(struct pipe_screen *screen, unsigned max_threads) 1078bf215546Sopenharmony_ci{ 1079bf215546Sopenharmony_ci struct si_screen *sscreen = (struct si_screen *)screen; 1080bf215546Sopenharmony_ci 1081bf215546Sopenharmony_ci /* This function doesn't allow a greater number of threads than 1082bf215546Sopenharmony_ci * the queue had at its creation. */ 1083bf215546Sopenharmony_ci util_queue_adjust_num_threads(&sscreen->shader_compiler_queue, max_threads); 1084bf215546Sopenharmony_ci /* Don't change the number of threads on the low priority queue. */ 1085bf215546Sopenharmony_ci} 1086bf215546Sopenharmony_ci 1087bf215546Sopenharmony_cistatic bool si_is_parallel_shader_compilation_finished(struct pipe_screen *screen, void *shader, 1088bf215546Sopenharmony_ci enum pipe_shader_type shader_type) 1089bf215546Sopenharmony_ci{ 1090bf215546Sopenharmony_ci struct si_shader_selector *sel = (struct si_shader_selector *)shader; 1091bf215546Sopenharmony_ci 1092bf215546Sopenharmony_ci return util_queue_fence_is_signalled(&sel->ready); 1093bf215546Sopenharmony_ci} 1094bf215546Sopenharmony_ci 1095bf215546Sopenharmony_cistatic struct pipe_screen *radeonsi_screen_create_impl(struct radeon_winsys *ws, 1096bf215546Sopenharmony_ci const struct pipe_screen_config *config) 1097bf215546Sopenharmony_ci{ 1098bf215546Sopenharmony_ci struct si_screen *sscreen = CALLOC_STRUCT(si_screen); 1099bf215546Sopenharmony_ci unsigned hw_threads, num_comp_hi_threads, num_comp_lo_threads; 1100bf215546Sopenharmony_ci uint64_t test_flags; 1101bf215546Sopenharmony_ci 1102bf215546Sopenharmony_ci if (!sscreen) { 1103bf215546Sopenharmony_ci return NULL; 1104bf215546Sopenharmony_ci } 1105bf215546Sopenharmony_ci 1106bf215546Sopenharmony_ci { 1107bf215546Sopenharmony_ci#define OPT_BOOL(name, dflt, description) \ 1108bf215546Sopenharmony_ci sscreen->options.name = driQueryOptionb(config->options, "radeonsi_" #name); 1109bf215546Sopenharmony_ci#define OPT_INT(name, dflt, description) \ 1110bf215546Sopenharmony_ci sscreen->options.name = driQueryOptioni(config->options, "radeonsi_" #name); 1111bf215546Sopenharmony_ci#include "si_debug_options.h" 1112bf215546Sopenharmony_ci } 1113bf215546Sopenharmony_ci 1114bf215546Sopenharmony_ci sscreen->ws = ws; 1115bf215546Sopenharmony_ci ws->query_info(ws, &sscreen->info, 1116bf215546Sopenharmony_ci sscreen->options.enable_sam, 1117bf215546Sopenharmony_ci sscreen->options.disable_sam); 1118bf215546Sopenharmony_ci 1119bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX9) { 1120bf215546Sopenharmony_ci sscreen->se_tile_repeat = 32 * sscreen->info.max_se; 1121bf215546Sopenharmony_ci } else { 1122bf215546Sopenharmony_ci ac_get_raster_config(&sscreen->info, &sscreen->pa_sc_raster_config, 1123bf215546Sopenharmony_ci &sscreen->pa_sc_raster_config_1, &sscreen->se_tile_repeat); 1124bf215546Sopenharmony_ci } 1125bf215546Sopenharmony_ci 1126bf215546Sopenharmony_ci sscreen->debug_flags = debug_get_flags_option("R600_DEBUG", radeonsi_debug_options, 0); 1127bf215546Sopenharmony_ci sscreen->debug_flags |= debug_get_flags_option("AMD_DEBUG", radeonsi_debug_options, 0); 1128bf215546Sopenharmony_ci test_flags = debug_get_flags_option("AMD_TEST", test_options, 0); 1129bf215546Sopenharmony_ci 1130bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(NO_GFX)) 1131bf215546Sopenharmony_ci sscreen->info.has_graphics = false; 1132bf215546Sopenharmony_ci 1133bf215546Sopenharmony_ci if ((sscreen->debug_flags & DBG(TMZ)) && 1134bf215546Sopenharmony_ci !sscreen->info.has_tmz_support) { 1135bf215546Sopenharmony_ci fprintf(stderr, "radeonsi: requesting TMZ features but TMZ is not supported\n"); 1136bf215546Sopenharmony_ci FREE(sscreen); 1137bf215546Sopenharmony_ci return NULL; 1138bf215546Sopenharmony_ci } 1139bf215546Sopenharmony_ci 1140bf215546Sopenharmony_ci /* Initialize just one compiler instance to check for errors. The other compiler instances are 1141bf215546Sopenharmony_ci * initialized on demand. 1142bf215546Sopenharmony_ci */ 1143bf215546Sopenharmony_ci if (!si_init_compiler(sscreen, &sscreen->compiler[0])) { 1144bf215546Sopenharmony_ci /* The callee prints the error message. */ 1145bf215546Sopenharmony_ci FREE(sscreen); 1146bf215546Sopenharmony_ci return NULL; 1147bf215546Sopenharmony_ci } 1148bf215546Sopenharmony_ci 1149bf215546Sopenharmony_ci util_idalloc_mt_init_tc(&sscreen->buffer_ids); 1150bf215546Sopenharmony_ci 1151bf215546Sopenharmony_ci /* Set functions first. */ 1152bf215546Sopenharmony_ci sscreen->b.context_create = si_pipe_create_context; 1153bf215546Sopenharmony_ci sscreen->b.destroy = si_destroy_screen; 1154bf215546Sopenharmony_ci sscreen->b.set_max_shader_compiler_threads = si_set_max_shader_compiler_threads; 1155bf215546Sopenharmony_ci sscreen->b.is_parallel_shader_compilation_finished = si_is_parallel_shader_compilation_finished; 1156bf215546Sopenharmony_ci sscreen->b.finalize_nir = si_finalize_nir; 1157bf215546Sopenharmony_ci 1158bf215546Sopenharmony_ci si_init_screen_get_functions(sscreen); 1159bf215546Sopenharmony_ci si_init_screen_buffer_functions(sscreen); 1160bf215546Sopenharmony_ci si_init_screen_fence_functions(sscreen); 1161bf215546Sopenharmony_ci si_init_screen_state_functions(sscreen); 1162bf215546Sopenharmony_ci si_init_screen_texture_functions(sscreen); 1163bf215546Sopenharmony_ci si_init_screen_query_functions(sscreen); 1164bf215546Sopenharmony_ci si_init_screen_live_shader_cache(sscreen); 1165bf215546Sopenharmony_ci 1166bf215546Sopenharmony_ci sscreen->max_texel_buffer_elements = sscreen->b.get_param( 1167bf215546Sopenharmony_ci &sscreen->b, PIPE_CAP_MAX_TEXEL_BUFFER_ELEMENTS_UINT); 1168bf215546Sopenharmony_ci 1169bf215546Sopenharmony_ci /* Set these flags in debug_flags early, so that the shader cache takes 1170bf215546Sopenharmony_ci * them into account. 1171bf215546Sopenharmony_ci * 1172bf215546Sopenharmony_ci * Enable FS_CORRECT_DERIVS_AFTER_KILL by default if LLVM is >= 13. This makes 1173bf215546Sopenharmony_ci * nir_opt_move_discards_to_top more effective. 1174bf215546Sopenharmony_ci */ 1175bf215546Sopenharmony_ci if (driQueryOptionb(config->options, "glsl_correct_derivatives_after_discard") || 1176bf215546Sopenharmony_ci LLVM_VERSION_MAJOR >= 13) 1177bf215546Sopenharmony_ci sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL); 1178bf215546Sopenharmony_ci 1179bf215546Sopenharmony_ci if (sscreen->debug_flags & DBG(INFO)) 1180bf215546Sopenharmony_ci ac_print_gpu_info(&sscreen->info, stdout); 1181bf215546Sopenharmony_ci 1182bf215546Sopenharmony_ci slab_create_parent(&sscreen->pool_transfers, sizeof(struct si_transfer), 64); 1183bf215546Sopenharmony_ci 1184bf215546Sopenharmony_ci sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1)); 1185bf215546Sopenharmony_ci if (sscreen->force_aniso == -1) { 1186bf215546Sopenharmony_ci sscreen->force_aniso = MIN2(16, debug_get_num_option("AMD_TEX_ANISO", -1)); 1187bf215546Sopenharmony_ci } 1188bf215546Sopenharmony_ci 1189bf215546Sopenharmony_ci if (sscreen->force_aniso >= 0) { 1190bf215546Sopenharmony_ci printf("radeonsi: Forcing anisotropy filter to %ix\n", 1191bf215546Sopenharmony_ci /* round down to a power of two */ 1192bf215546Sopenharmony_ci 1 << util_logbase2(sscreen->force_aniso)); 1193bf215546Sopenharmony_ci } 1194bf215546Sopenharmony_ci 1195bf215546Sopenharmony_ci (void)mtx_init(&sscreen->aux_context_lock, mtx_recursive); 1196bf215546Sopenharmony_ci (void)simple_mtx_init(&sscreen->async_compute_context_lock, mtx_plain); 1197bf215546Sopenharmony_ci (void)simple_mtx_init(&sscreen->gpu_load_mutex, mtx_plain); 1198bf215546Sopenharmony_ci (void)simple_mtx_init(&sscreen->gds_mutex, mtx_plain); 1199bf215546Sopenharmony_ci 1200bf215546Sopenharmony_ci si_init_gs_info(sscreen); 1201bf215546Sopenharmony_ci if (!si_init_shader_cache(sscreen)) { 1202bf215546Sopenharmony_ci FREE(sscreen); 1203bf215546Sopenharmony_ci return NULL; 1204bf215546Sopenharmony_ci } 1205bf215546Sopenharmony_ci 1206bf215546Sopenharmony_ci if (sscreen->info.gfx_level < GFX10_3) 1207bf215546Sopenharmony_ci sscreen->options.vrs2x2 = false; 1208bf215546Sopenharmony_ci 1209bf215546Sopenharmony_ci si_disk_cache_create(sscreen); 1210bf215546Sopenharmony_ci 1211bf215546Sopenharmony_ci /* Determine the number of shader compiler threads. */ 1212bf215546Sopenharmony_ci const struct util_cpu_caps_t *caps = util_get_cpu_caps(); 1213bf215546Sopenharmony_ci hw_threads = caps->nr_cpus; 1214bf215546Sopenharmony_ci 1215bf215546Sopenharmony_ci if (hw_threads >= 12) { 1216bf215546Sopenharmony_ci num_comp_hi_threads = hw_threads * 3 / 4; 1217bf215546Sopenharmony_ci num_comp_lo_threads = hw_threads / 3; 1218bf215546Sopenharmony_ci } else if (hw_threads >= 6) { 1219bf215546Sopenharmony_ci num_comp_hi_threads = hw_threads - 2; 1220bf215546Sopenharmony_ci num_comp_lo_threads = hw_threads / 2; 1221bf215546Sopenharmony_ci } else if (hw_threads >= 2) { 1222bf215546Sopenharmony_ci num_comp_hi_threads = hw_threads - 1; 1223bf215546Sopenharmony_ci num_comp_lo_threads = hw_threads / 2; 1224bf215546Sopenharmony_ci } else { 1225bf215546Sopenharmony_ci num_comp_hi_threads = 1; 1226bf215546Sopenharmony_ci num_comp_lo_threads = 1; 1227bf215546Sopenharmony_ci } 1228bf215546Sopenharmony_ci 1229bf215546Sopenharmony_ci#ifndef NDEBUG 1230bf215546Sopenharmony_ci nir_process_debug_variable(); 1231bf215546Sopenharmony_ci 1232bf215546Sopenharmony_ci /* Use a single compilation thread if NIR printing is enabled to avoid 1233bf215546Sopenharmony_ci * multiple shaders being printed at the same time. 1234bf215546Sopenharmony_ci */ 1235bf215546Sopenharmony_ci if (NIR_DEBUG(PRINT)) { 1236bf215546Sopenharmony_ci num_comp_hi_threads = 1; 1237bf215546Sopenharmony_ci num_comp_lo_threads = 1; 1238bf215546Sopenharmony_ci } 1239bf215546Sopenharmony_ci#endif 1240bf215546Sopenharmony_ci 1241bf215546Sopenharmony_ci num_comp_hi_threads = MIN2(num_comp_hi_threads, ARRAY_SIZE(sscreen->compiler)); 1242bf215546Sopenharmony_ci num_comp_lo_threads = MIN2(num_comp_lo_threads, ARRAY_SIZE(sscreen->compiler_lowp)); 1243bf215546Sopenharmony_ci 1244bf215546Sopenharmony_ci /* Take a reference on the glsl types for the compiler threads. */ 1245bf215546Sopenharmony_ci glsl_type_singleton_init_or_ref(); 1246bf215546Sopenharmony_ci 1247bf215546Sopenharmony_ci /* Start with a single thread and a single slot. 1248bf215546Sopenharmony_ci * Each time we'll hit the "all slots are in use" case, the number of threads and 1249bf215546Sopenharmony_ci * slots will be increased. 1250bf215546Sopenharmony_ci */ 1251bf215546Sopenharmony_ci int num_slots = num_comp_hi_threads == 1 ? 64 : 1; 1252bf215546Sopenharmony_ci if (!util_queue_init(&sscreen->shader_compiler_queue, "sh", num_slots, 1253bf215546Sopenharmony_ci num_comp_hi_threads, 1254bf215546Sopenharmony_ci UTIL_QUEUE_INIT_RESIZE_IF_FULL | 1255bf215546Sopenharmony_ci UTIL_QUEUE_INIT_SCALE_THREADS | 1256bf215546Sopenharmony_ci UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, NULL)) { 1257bf215546Sopenharmony_ci si_destroy_shader_cache(sscreen); 1258bf215546Sopenharmony_ci FREE(sscreen); 1259bf215546Sopenharmony_ci glsl_type_singleton_decref(); 1260bf215546Sopenharmony_ci return NULL; 1261bf215546Sopenharmony_ci } 1262bf215546Sopenharmony_ci 1263bf215546Sopenharmony_ci if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority, "shlo", num_slots, 1264bf215546Sopenharmony_ci num_comp_lo_threads, 1265bf215546Sopenharmony_ci UTIL_QUEUE_INIT_RESIZE_IF_FULL | 1266bf215546Sopenharmony_ci UTIL_QUEUE_INIT_SCALE_THREADS | 1267bf215546Sopenharmony_ci UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY | 1268bf215546Sopenharmony_ci UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY, NULL)) { 1269bf215546Sopenharmony_ci si_destroy_shader_cache(sscreen); 1270bf215546Sopenharmony_ci FREE(sscreen); 1271bf215546Sopenharmony_ci glsl_type_singleton_decref(); 1272bf215546Sopenharmony_ci return NULL; 1273bf215546Sopenharmony_ci } 1274bf215546Sopenharmony_ci 1275bf215546Sopenharmony_ci if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false)) 1276bf215546Sopenharmony_ci si_init_perfcounters(sscreen); 1277bf215546Sopenharmony_ci 1278bf215546Sopenharmony_ci sscreen->max_memory_usage_kb = sscreen->info.vram_size_kb + sscreen->info.gart_size_kb / 4 * 3; 1279bf215546Sopenharmony_ci 1280bf215546Sopenharmony_ci ac_get_hs_info(&sscreen->info, &sscreen->hs); 1281bf215546Sopenharmony_ci 1282bf215546Sopenharmony_ci sscreen->has_draw_indirect_multi = 1283bf215546Sopenharmony_ci (sscreen->info.family >= CHIP_POLARIS10) || 1284bf215546Sopenharmony_ci (sscreen->info.gfx_level == GFX8 && sscreen->info.pfp_fw_version >= 121 && 1285bf215546Sopenharmony_ci sscreen->info.me_fw_version >= 87) || 1286bf215546Sopenharmony_ci (sscreen->info.gfx_level == GFX7 && sscreen->info.pfp_fw_version >= 211 && 1287bf215546Sopenharmony_ci sscreen->info.me_fw_version >= 173) || 1288bf215546Sopenharmony_ci (sscreen->info.gfx_level == GFX6 && sscreen->info.pfp_fw_version >= 79 && 1289bf215546Sopenharmony_ci sscreen->info.me_fw_version >= 142); 1290bf215546Sopenharmony_ci 1291bf215546Sopenharmony_ci sscreen->has_out_of_order_rast = 1292bf215546Sopenharmony_ci sscreen->info.has_out_of_order_rast && !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER)); 1293bf215546Sopenharmony_ci 1294bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 1295bf215546Sopenharmony_ci sscreen->use_ngg = true; 1296bf215546Sopenharmony_ci sscreen->use_ngg_streamout = true; 1297bf215546Sopenharmony_ci /* TODO: Disable for now. Investigate if it helps. */ 1298bf215546Sopenharmony_ci sscreen->use_ngg_culling = (sscreen->debug_flags & DBG(ALWAYS_NGG_CULLING_ALL)) && 1299bf215546Sopenharmony_ci !(sscreen->debug_flags & DBG(NO_NGG_CULLING)); 1300bf215546Sopenharmony_ci } else { 1301bf215546Sopenharmony_ci sscreen->use_ngg = !(sscreen->debug_flags & DBG(NO_NGG)) && 1302bf215546Sopenharmony_ci sscreen->info.gfx_level >= GFX10 && 1303bf215546Sopenharmony_ci (sscreen->info.family != CHIP_NAVI14 || 1304bf215546Sopenharmony_ci sscreen->info.is_pro_graphics); 1305bf215546Sopenharmony_ci sscreen->use_ngg_streamout = false; 1306bf215546Sopenharmony_ci sscreen->use_ngg_culling = sscreen->use_ngg && 1307bf215546Sopenharmony_ci sscreen->info.max_render_backends >= 2 && 1308bf215546Sopenharmony_ci !(sscreen->debug_flags & DBG(NO_NGG_CULLING)) && 1309bf215546Sopenharmony_ci LLVM_VERSION_MAJOR >= 12; /* hangs on 11, see #4874 */ 1310bf215546Sopenharmony_ci } 1311bf215546Sopenharmony_ci 1312bf215546Sopenharmony_ci /* Only set this for the cases that are known to work, which are: 1313bf215546Sopenharmony_ci * - GFX9 if bpp >= 4 (in bytes) 1314bf215546Sopenharmony_ci */ 1315bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX10) { 1316bf215546Sopenharmony_ci memset(sscreen->allow_dcc_msaa_clear_to_reg_for_bpp, true, 1317bf215546Sopenharmony_ci sizeof(sscreen->allow_dcc_msaa_clear_to_reg_for_bpp)); 1318bf215546Sopenharmony_ci } else if (sscreen->info.gfx_level == GFX9) { 1319bf215546Sopenharmony_ci for (unsigned bpp_log2 = util_logbase2(1); bpp_log2 <= util_logbase2(16); bpp_log2++) 1320bf215546Sopenharmony_ci sscreen->allow_dcc_msaa_clear_to_reg_for_bpp[bpp_log2] = true; 1321bf215546Sopenharmony_ci } 1322bf215546Sopenharmony_ci 1323bf215546Sopenharmony_ci /* DCC stores have 50% performance of uncompressed stores and sometimes 1324bf215546Sopenharmony_ci * even less than that. It's risky to enable on dGPUs. 1325bf215546Sopenharmony_ci */ 1326bf215546Sopenharmony_ci sscreen->always_allow_dcc_stores = !(sscreen->debug_flags & DBG(NO_DCC_STORE)) && 1327bf215546Sopenharmony_ci (sscreen->debug_flags & DBG(DCC_STORE) || 1328bf215546Sopenharmony_ci sscreen->info.gfx_level >= GFX11 || /* always enabled on gfx11 */ 1329bf215546Sopenharmony_ci (sscreen->info.gfx_level >= GFX10_3 && 1330bf215546Sopenharmony_ci !sscreen->info.has_dedicated_vram)); 1331bf215546Sopenharmony_ci 1332bf215546Sopenharmony_ci sscreen->dpbb_allowed = !(sscreen->debug_flags & DBG(NO_DPBB)) && 1333bf215546Sopenharmony_ci (sscreen->info.gfx_level >= GFX10 || 1334bf215546Sopenharmony_ci /* Only enable primitive binning on gfx9 APUs by default. */ 1335bf215546Sopenharmony_ci (sscreen->info.gfx_level == GFX9 && !sscreen->info.has_dedicated_vram) || 1336bf215546Sopenharmony_ci sscreen->debug_flags & DBG(DPBB)); 1337bf215546Sopenharmony_ci 1338bf215546Sopenharmony_ci if (sscreen->dpbb_allowed) { 1339bf215546Sopenharmony_ci if (sscreen->info.has_dedicated_vram) { 1340bf215546Sopenharmony_ci if (sscreen->info.max_render_backends > 4) { 1341bf215546Sopenharmony_ci sscreen->pbb_context_states_per_bin = 1; 1342bf215546Sopenharmony_ci sscreen->pbb_persistent_states_per_bin = 1; 1343bf215546Sopenharmony_ci } else { 1344bf215546Sopenharmony_ci sscreen->pbb_context_states_per_bin = 3; 1345bf215546Sopenharmony_ci sscreen->pbb_persistent_states_per_bin = 8; 1346bf215546Sopenharmony_ci } 1347bf215546Sopenharmony_ci } else { 1348bf215546Sopenharmony_ci /* This is a workaround for: 1349bf215546Sopenharmony_ci * https://bugs.freedesktop.org/show_bug.cgi?id=110214 1350bf215546Sopenharmony_ci * (an alternative is to insert manual BATCH_BREAK event when 1351bf215546Sopenharmony_ci * a context_roll is detected). */ 1352bf215546Sopenharmony_ci sscreen->pbb_context_states_per_bin = sscreen->info.has_gfx9_scissor_bug ? 1 : 6; 1353bf215546Sopenharmony_ci /* Using 32 here can cause GPU hangs on RAVEN1 */ 1354bf215546Sopenharmony_ci sscreen->pbb_persistent_states_per_bin = 16; 1355bf215546Sopenharmony_ci } 1356bf215546Sopenharmony_ci 1357bf215546Sopenharmony_ci assert(sscreen->pbb_context_states_per_bin >= 1 && 1358bf215546Sopenharmony_ci sscreen->pbb_context_states_per_bin <= 6); 1359bf215546Sopenharmony_ci assert(sscreen->pbb_persistent_states_per_bin >= 1 && 1360bf215546Sopenharmony_ci sscreen->pbb_persistent_states_per_bin <= 32); 1361bf215546Sopenharmony_ci } 1362bf215546Sopenharmony_ci 1363bf215546Sopenharmony_ci (void)simple_mtx_init(&sscreen->shader_parts_mutex, mtx_plain); 1364bf215546Sopenharmony_ci sscreen->use_monolithic_shaders = (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0; 1365bf215546Sopenharmony_ci 1366bf215546Sopenharmony_ci sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE; 1367bf215546Sopenharmony_ci if (sscreen->info.gfx_level <= GFX8) { 1368bf215546Sopenharmony_ci sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_L2; 1369bf215546Sopenharmony_ci sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WB_L2; 1370bf215546Sopenharmony_ci } 1371bf215546Sopenharmony_ci 1372bf215546Sopenharmony_ci if (debug_get_bool_option("RADEON_DUMP_SHADERS", false)) 1373bf215546Sopenharmony_ci sscreen->debug_flags |= DBG_ALL_SHADERS; 1374bf215546Sopenharmony_ci 1375bf215546Sopenharmony_ci /* Syntax: 1376bf215546Sopenharmony_ci * EQAA=s,z,c 1377bf215546Sopenharmony_ci * Example: 1378bf215546Sopenharmony_ci * EQAA=8,4,2 1379bf215546Sopenharmony_ci 1380bf215546Sopenharmony_ci * That means 8 coverage samples, 4 Z/S samples, and 2 color samples. 1381bf215546Sopenharmony_ci * Constraints: 1382bf215546Sopenharmony_ci * s >= z >= c (ignoring this only wastes memory) 1383bf215546Sopenharmony_ci * s = [2..16] 1384bf215546Sopenharmony_ci * z = [2..8] 1385bf215546Sopenharmony_ci * c = [2..8] 1386bf215546Sopenharmony_ci * 1387bf215546Sopenharmony_ci * Only MSAA color and depth buffers are overriden. 1388bf215546Sopenharmony_ci */ 1389bf215546Sopenharmony_ci if (sscreen->info.has_eqaa_surface_allocator) { 1390bf215546Sopenharmony_ci const char *eqaa = debug_get_option("EQAA", NULL); 1391bf215546Sopenharmony_ci unsigned s, z, f; 1392bf215546Sopenharmony_ci 1393bf215546Sopenharmony_ci if (eqaa && sscanf(eqaa, "%u,%u,%u", &s, &z, &f) == 3 && s && z && f) { 1394bf215546Sopenharmony_ci sscreen->eqaa_force_coverage_samples = s; 1395bf215546Sopenharmony_ci sscreen->eqaa_force_z_samples = z; 1396bf215546Sopenharmony_ci sscreen->eqaa_force_color_samples = f; 1397bf215546Sopenharmony_ci } 1398bf215546Sopenharmony_ci } 1399bf215546Sopenharmony_ci 1400bf215546Sopenharmony_ci sscreen->ngg_subgroup_size = 128; 1401bf215546Sopenharmony_ci 1402bf215546Sopenharmony_ci if (sscreen->info.gfx_level >= GFX11) { 1403bf215546Sopenharmony_ci /* TODO: tweak this */ 1404bf215546Sopenharmony_ci unsigned attr_ring_size_per_se = align(1400000, 64 * 1024); 1405bf215546Sopenharmony_ci unsigned attr_ring_size = attr_ring_size_per_se * sscreen->info.max_se; 1406bf215546Sopenharmony_ci assert(attr_ring_size <= 16 * 1024 * 1024); /* maximum size */ 1407bf215546Sopenharmony_ci sscreen->attribute_ring = si_aligned_buffer_create(&sscreen->b, 1408bf215546Sopenharmony_ci PIPE_RESOURCE_FLAG_UNMAPPABLE | 1409bf215546Sopenharmony_ci SI_RESOURCE_FLAG_32BIT | 1410bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DRIVER_INTERNAL | 1411bf215546Sopenharmony_ci SI_RESOURCE_FLAG_DISCARDABLE, 1412bf215546Sopenharmony_ci PIPE_USAGE_DEFAULT, 1413bf215546Sopenharmony_ci attr_ring_size, 2 * 1024 * 1024); 1414bf215546Sopenharmony_ci } 1415bf215546Sopenharmony_ci 1416bf215546Sopenharmony_ci /* Create the auxiliary context. This must be done last. */ 1417bf215546Sopenharmony_ci sscreen->aux_context = si_create_context( 1418bf215546Sopenharmony_ci &sscreen->b, 1419bf215546Sopenharmony_ci SI_CONTEXT_FLAG_AUX | 1420bf215546Sopenharmony_ci (sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) | 1421bf215546Sopenharmony_ci (sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY)); 1422bf215546Sopenharmony_ci 1423bf215546Sopenharmony_ci if (sscreen->options.aux_debug) { 1424bf215546Sopenharmony_ci struct u_log_context *log = CALLOC_STRUCT(u_log_context); 1425bf215546Sopenharmony_ci u_log_context_init(log); 1426bf215546Sopenharmony_ci si_get_aux_context(sscreen)->b.set_log_context(sscreen->aux_context, log); 1427bf215546Sopenharmony_ci si_put_aux_context_flush(sscreen); 1428bf215546Sopenharmony_ci } 1429bf215546Sopenharmony_ci 1430bf215546Sopenharmony_ci if (test_flags & DBG(TEST_IMAGE_COPY)) 1431bf215546Sopenharmony_ci si_test_image_copy_region(sscreen); 1432bf215546Sopenharmony_ci 1433bf215546Sopenharmony_ci if (test_flags & (DBG(TEST_CB_RESOLVE) | DBG(TEST_COMPUTE_BLIT))) 1434bf215546Sopenharmony_ci si_test_blit(sscreen, test_flags); 1435bf215546Sopenharmony_ci 1436bf215546Sopenharmony_ci if (test_flags & DBG(TEST_DMA_PERF)) { 1437bf215546Sopenharmony_ci si_test_dma_perf(sscreen); 1438bf215546Sopenharmony_ci } 1439bf215546Sopenharmony_ci 1440bf215546Sopenharmony_ci if (test_flags & (DBG(TEST_VMFAULT_CP) | DBG(TEST_VMFAULT_SHADER))) 1441bf215546Sopenharmony_ci si_test_vmfault(sscreen, test_flags); 1442bf215546Sopenharmony_ci 1443bf215546Sopenharmony_ci if (test_flags & DBG(TEST_GDS)) 1444bf215546Sopenharmony_ci si_test_gds((struct si_context *)sscreen->aux_context); 1445bf215546Sopenharmony_ci 1446bf215546Sopenharmony_ci if (test_flags & DBG(TEST_GDS_MM)) { 1447bf215546Sopenharmony_ci si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 32 * 1024, 4, 1448bf215546Sopenharmony_ci RADEON_DOMAIN_GDS); 1449bf215546Sopenharmony_ci } 1450bf215546Sopenharmony_ci if (test_flags & DBG(TEST_GDS_OA_MM)) { 1451bf215546Sopenharmony_ci si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 4, 1, 1452bf215546Sopenharmony_ci RADEON_DOMAIN_OA); 1453bf215546Sopenharmony_ci } 1454bf215546Sopenharmony_ci 1455bf215546Sopenharmony_ci ac_print_shadowed_regs(&sscreen->info); 1456bf215546Sopenharmony_ci 1457bf215546Sopenharmony_ci STATIC_ASSERT(sizeof(union si_vgt_stages_key) == 1); 1458bf215546Sopenharmony_ci return &sscreen->b; 1459bf215546Sopenharmony_ci} 1460bf215546Sopenharmony_ci 1461bf215546Sopenharmony_cistruct pipe_screen *radeonsi_screen_create(int fd, const struct pipe_screen_config *config) 1462bf215546Sopenharmony_ci{ 1463bf215546Sopenharmony_ci drmVersionPtr version = drmGetVersion(fd); 1464bf215546Sopenharmony_ci struct radeon_winsys *rw = NULL; 1465bf215546Sopenharmony_ci 1466bf215546Sopenharmony_ci driParseConfigFiles(config->options, config->options_info, 0, "radeonsi", 1467bf215546Sopenharmony_ci NULL, NULL, NULL, 0, NULL, 0); 1468bf215546Sopenharmony_ci 1469bf215546Sopenharmony_ci switch (version->version_major) { 1470bf215546Sopenharmony_ci case 2: 1471bf215546Sopenharmony_ci rw = radeon_drm_winsys_create(fd, config, radeonsi_screen_create_impl); 1472bf215546Sopenharmony_ci break; 1473bf215546Sopenharmony_ci case 3: 1474bf215546Sopenharmony_ci rw = amdgpu_winsys_create(fd, config, radeonsi_screen_create_impl); 1475bf215546Sopenharmony_ci break; 1476bf215546Sopenharmony_ci } 1477bf215546Sopenharmony_ci 1478bf215546Sopenharmony_ci drmFreeVersion(version); 1479bf215546Sopenharmony_ci return rw ? rw->screen : NULL; 1480bf215546Sopenharmony_ci} 1481bf215546Sopenharmony_ci 1482bf215546Sopenharmony_cistruct si_context* si_get_aux_context(struct si_screen *sscreen) 1483bf215546Sopenharmony_ci{ 1484bf215546Sopenharmony_ci mtx_lock(&sscreen->aux_context_lock); 1485bf215546Sopenharmony_ci return (struct si_context*)sscreen->aux_context; 1486bf215546Sopenharmony_ci} 1487bf215546Sopenharmony_ci 1488bf215546Sopenharmony_civoid si_put_aux_context_flush(struct si_screen *sscreen) 1489bf215546Sopenharmony_ci{ 1490bf215546Sopenharmony_ci struct pipe_context *c = &((struct si_context*)sscreen->aux_context)->b; 1491bf215546Sopenharmony_ci c->flush(c, NULL, 0); 1492bf215546Sopenharmony_ci mtx_unlock(&sscreen->aux_context_lock); 1493bf215546Sopenharmony_ci} 1494