Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_pipe.c
4570 views
/*1* Copyright 2010 Jerome Glisse <[email protected]>2* Copyright 2018 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* on the rights to use, copy, modify, merge, publish, distribute, sub9* license, and/or sell copies of the Software, and to permit persons to whom10* the Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice (including the next13* paragraph) shall be included in all copies or substantial portions of the14* Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL19* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,20* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR21* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE22* USE OR OTHER DEALINGS IN THE SOFTWARE.23*/2425#include "si_pipe.h"2627#include "driver_ddebug/dd_util.h"28#include "gallium/winsys/amdgpu/drm/amdgpu_public.h"29#include "gallium/winsys/radeon/drm/radeon_drm_public.h"30#include "radeon/radeon_uvd.h"31#include "si_compute.h"32#include "si_public.h"33#include "si_shader_internal.h"34#include "sid.h"35#include "ac_shadowed_regs.h"36#include "util/disk_cache.h"37#include "util/u_cpu_detect.h"38#include "util/u_log.h"39#include "util/u_memory.h"40#include "util/u_suballoc.h"41#include "util/u_tests.h"42#include "util/u_upload_mgr.h"43#include "util/xmlconfig.h"44#include "vl/vl_decoder.h"4546#include <xf86drm.h>4748static struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags);4950static const struct debug_named_value radeonsi_debug_options[] = {51/* Shader logging options: */52{"vs", DBG(VS), "Print vertex shaders"},53{"ps", DBG(PS), "Print pixel shaders"},54{"gs", DBG(GS), "Print geometry shaders"},55{"tcs", DBG(TCS), "Print tessellation control shaders"},56{"tes", DBG(TES), "Print tessellation evaluation shaders"},57{"cs", DBG(CS), "Print compute shaders"},58{"noir", DBG(NO_IR), "Don't print the LLVM IR"},59{"nonir", DBG(NO_NIR), "Don't print NIR when printing shaders"},60{"noasm", DBG(NO_ASM), "Don't print disassembled shaders"},61{"preoptir", DBG(PREOPT_IR), "Print the LLVM IR before initial optimizations"},6263/* Shader compiler options the shader cache should be aware of: */64{"gisel", DBG(GISEL), "Enable LLVM global instruction selector."},65{"w32ge", DBG(W32_GE), "Use Wave32 for vertex, tessellation, and geometry shaders."},66{"w32ps", DBG(W32_PS), "Use Wave32 for pixel shaders."},67{"w32cs", DBG(W32_CS), "Use Wave32 for computes shaders."},68{"w64ge", DBG(W64_GE), "Use Wave64 for vertex, tessellation, and geometry shaders."},69{"w64ps", DBG(W64_PS), "Use Wave64 for pixel shaders."},70{"w64cs", DBG(W64_CS), "Use Wave64 for computes shaders."},7172/* Shader compiler options (with no effect on the shader cache): */73{"checkir", DBG(CHECK_IR), "Enable additional sanity checks on shader IR"},74{"mono", DBG(MONOLITHIC_SHADERS), "Use old-style monolithic shaders compiled on demand"},75{"nooptvariant", DBG(NO_OPT_VARIANT), "Disable compiling optimized shader variants."},7677/* Information logging options: */78{"info", DBG(INFO), "Print driver information"},79{"tex", DBG(TEX), "Print texture info"},80{"compute", DBG(COMPUTE), "Print compute info"},81{"vm", DBG(VM), "Print virtual addresses when creating resources"},82{"cache_stats", DBG(CACHE_STATS), "Print shader cache statistics."},8384/* Driver options: */85{"nowc", DBG(NO_WC), "Disable GTT write combining"},86{"check_vm", DBG(CHECK_VM), "Check VM faults and dump debug info."},87{"reserve_vmid", DBG(RESERVE_VMID), "Force VMID reservation per context."},88{"shadowregs", DBG(SHADOW_REGS), "Enable CP register shadowing."},8990/* 3D engine options: */91{"nogfx", DBG(NO_GFX), "Disable graphics. Only multimedia compute paths can be used."},92{"nongg", DBG(NO_NGG), "Disable NGG and use the legacy pipeline."},93{"nofastlaunch", DBG(NO_FAST_LAUNCH), "Disable NGG GS fast launch."},94{"nggc", DBG(ALWAYS_NGG_CULLING_ALL), "Always use NGG culling even when it can hurt."},95{"nggctess", DBG(ALWAYS_NGG_CULLING_TESS), "Always use NGG culling for tessellation."},96{"nonggc", DBG(NO_NGG_CULLING), "Disable NGG culling."},97{"alwayspd", DBG(ALWAYS_PD), "Always enable the primitive discard compute shader."},98{"pd", DBG(PD), "Enable the primitive discard compute shader for large draw calls."},99{"nopd", DBG(NO_PD), "Disable the primitive discard compute shader."},100{"switch_on_eop", DBG(SWITCH_ON_EOP), "Program WD/IA to switch on end-of-packet."},101{"nooutoforder", DBG(NO_OUT_OF_ORDER), "Disable out-of-order rasterization"},102{"nodpbb", DBG(NO_DPBB), "Disable DPBB."},103{"dpbb", DBG(DPBB), "Enable DPBB."},104{"nohyperz", DBG(NO_HYPERZ), "Disable Hyper-Z"},105{"no2d", DBG(NO_2D_TILING), "Disable 2D tiling"},106{"notiling", DBG(NO_TILING), "Disable tiling"},107{"nodisplaytiling", DBG(NO_DISPLAY_TILING), "Disable display tiling"},108{"nodisplaydcc", DBG(NO_DISPLAY_DCC), "Disable display DCC"},109{"nodcc", DBG(NO_DCC), "Disable DCC."},110{"nodccclear", DBG(NO_DCC_CLEAR), "Disable DCC fast clear."},111{"nodccfb", DBG(NO_DCC_FB), "Disable separate DCC on the main framebuffer"},112{"nodccmsaa", DBG(NO_DCC_MSAA), "Disable DCC for MSAA"},113{"nofmask", DBG(NO_FMASK), "Disable MSAA compression"},114115{"tmz", DBG(TMZ), "Force allocation of scanout/depth/stencil buffer as encrypted"},116{"sqtt", DBG(SQTT), "Enable SQTT"},117118DEBUG_NAMED_VALUE_END /* must be last */119};120121static const struct debug_named_value test_options[] = {122/* Tests: */123{"blit", DBG(TEST_BLIT), "Invoke blit tests and exit."},124{"testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit."},125{"testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit."},126{"testdmaperf", DBG(TEST_DMA_PERF), "Test DMA performance"},127{"testgds", DBG(TEST_GDS), "Test GDS."},128{"testgdsmm", DBG(TEST_GDS_MM), "Test GDS memory management."},129{"testgdsoamm", DBG(TEST_GDS_OA_MM), "Test GDS OA memory management."},130131DEBUG_NAMED_VALUE_END /* must be last */132};133134void si_init_compiler(struct si_screen *sscreen, struct ac_llvm_compiler *compiler)135{136/* Only create the less-optimizing version of the compiler on APUs137* predating Ryzen (Raven). */138bool create_low_opt_compiler =139!sscreen->info.has_dedicated_vram && sscreen->info.chip_class <= GFX8;140141enum ac_target_machine_options tm_options =142(sscreen->debug_flags & DBG(GISEL) ? AC_TM_ENABLE_GLOBAL_ISEL : 0) |143(sscreen->debug_flags & DBG(CHECK_IR) ? AC_TM_CHECK_IR : 0) |144(create_low_opt_compiler ? AC_TM_CREATE_LOW_OPT : 0);145146ac_init_llvm_once();147ac_init_llvm_compiler(compiler, sscreen->info.family, tm_options);148compiler->passes = ac_create_llvm_passes(compiler->tm);149150if (compiler->low_opt_tm)151compiler->low_opt_passes = ac_create_llvm_passes(compiler->low_opt_tm);152}153154static void si_destroy_compiler(struct ac_llvm_compiler *compiler)155{156ac_destroy_llvm_compiler(compiler);157}158159160static void decref_implicit_resource(struct hash_entry *entry)161{162pipe_resource_reference((struct pipe_resource**)&entry->data, NULL);163}164165/*166* pipe_context167*/168static void si_destroy_context(struct pipe_context *context)169{170struct si_context *sctx = (struct si_context *)context;171int i;172173/* Unreference the framebuffer normally to disable related logic174* properly.175*/176struct pipe_framebuffer_state fb = {};177if (context->set_framebuffer_state)178context->set_framebuffer_state(context, &fb);179180si_release_all_descriptors(sctx);181182if (sctx->chip_class >= GFX10 && sctx->has_graphics)183gfx10_destroy_query(sctx);184185if (sctx->thread_trace)186si_destroy_thread_trace(sctx);187188pipe_resource_reference(&sctx->esgs_ring, NULL);189pipe_resource_reference(&sctx->gsvs_ring, NULL);190pipe_resource_reference(&sctx->tess_rings, NULL);191pipe_resource_reference(&sctx->tess_rings_tmz, NULL);192pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);193pipe_resource_reference(&sctx->sample_pos_buffer, NULL);194si_resource_reference(&sctx->border_color_buffer, NULL);195free(sctx->border_color_table);196si_resource_reference(&sctx->scratch_buffer, NULL);197si_resource_reference(&sctx->compute_scratch_buffer, NULL);198si_resource_reference(&sctx->wait_mem_scratch, NULL);199si_resource_reference(&sctx->wait_mem_scratch_tmz, NULL);200si_resource_reference(&sctx->small_prim_cull_info_buf, NULL);201202if (sctx->cs_preamble_state)203si_pm4_free_state(sctx, sctx->cs_preamble_state, ~0);204if (sctx->cs_preamble_tess_rings)205si_pm4_free_state(sctx, sctx->cs_preamble_tess_rings, ~0);206if (sctx->cs_preamble_tess_rings_tmz)207si_pm4_free_state(sctx, sctx->cs_preamble_tess_rings_tmz, ~0);208if (sctx->cs_preamble_gs_rings)209si_pm4_free_state(sctx, sctx->cs_preamble_gs_rings, ~0);210for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)211si_pm4_free_state(sctx, sctx->vgt_shader_config[i], SI_STATE_IDX(vgt_shader_config));212213if (sctx->fixed_func_tcs_shader.cso)214sctx->b.delete_tcs_state(&sctx->b, sctx->fixed_func_tcs_shader.cso);215if (sctx->custom_dsa_flush)216sctx->b.delete_depth_stencil_alpha_state(&sctx->b, sctx->custom_dsa_flush);217if (sctx->custom_blend_resolve)218sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_resolve);219if (sctx->custom_blend_fmask_decompress)220sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_fmask_decompress);221if (sctx->custom_blend_eliminate_fastclear)222sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_eliminate_fastclear);223if (sctx->custom_blend_dcc_decompress)224sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_dcc_decompress);225if (sctx->vs_blit_pos)226sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos);227if (sctx->vs_blit_pos_layered)228sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos_layered);229if (sctx->vs_blit_color)230sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color);231if (sctx->vs_blit_color_layered)232sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color_layered);233if (sctx->vs_blit_texcoord)234sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_texcoord);235if (sctx->cs_clear_buffer)236sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_buffer);237if (sctx->cs_clear_buffer_rmw)238sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_buffer_rmw);239if (sctx->cs_copy_buffer)240sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_buffer);241if (sctx->cs_copy_image)242sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_image);243if (sctx->cs_copy_image_1d_array)244sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_image_1d_array);245if (sctx->cs_clear_render_target)246sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target);247if (sctx->cs_clear_render_target_1d_array)248sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target_1d_array);249if (sctx->cs_clear_12bytes_buffer)250sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_12bytes_buffer);251if (sctx->cs_dcc_decompress)252sctx->b.delete_compute_state(&sctx->b, sctx->cs_dcc_decompress);253if (sctx->cs_dcc_retile)254sctx->b.delete_compute_state(&sctx->b, sctx->cs_dcc_retile);255if (sctx->no_velems_state)256sctx->b.delete_vertex_elements_state(&sctx->b, sctx->no_velems_state);257258for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_fmask_expand); i++) {259for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_fmask_expand[i]); j++) {260if (sctx->cs_fmask_expand[i][j]) {261sctx->b.delete_compute_state(&sctx->b, sctx->cs_fmask_expand[i][j]);262}263}264}265266for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_clear_dcc_msaa); i++) {267for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i]); j++) {268for (unsigned k = 0; k < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j]); k++) {269for (unsigned l = 0; l < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j][k]); l++) {270for (unsigned m = 0; m < ARRAY_SIZE(sctx->cs_clear_dcc_msaa[i][j][k][l]); m++) {271if (sctx->cs_clear_dcc_msaa[i][j][k][l][m])272sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_dcc_msaa[i][j][k][l][m]);273}274}275}276}277}278279if (sctx->blitter)280util_blitter_destroy(sctx->blitter);281282if (sctx->query_result_shader)283sctx->b.delete_compute_state(&sctx->b, sctx->query_result_shader);284if (sctx->sh_query_result_shader)285sctx->b.delete_compute_state(&sctx->b, sctx->sh_query_result_shader);286287sctx->ws->cs_destroy(&sctx->gfx_cs);288if (sctx->ctx)289sctx->ws->ctx_destroy(sctx->ctx);290291if (sctx->dirty_implicit_resources)292_mesa_hash_table_destroy(sctx->dirty_implicit_resources,293decref_implicit_resource);294295if (sctx->b.stream_uploader)296u_upload_destroy(sctx->b.stream_uploader);297if (sctx->b.const_uploader && sctx->b.const_uploader != sctx->b.stream_uploader)298u_upload_destroy(sctx->b.const_uploader);299if (sctx->cached_gtt_allocator)300u_upload_destroy(sctx->cached_gtt_allocator);301302slab_destroy_child(&sctx->pool_transfers);303slab_destroy_child(&sctx->pool_transfers_unsync);304305u_suballocator_destroy(&sctx->allocator_zeroed_memory);306307sctx->ws->fence_reference(&sctx->last_gfx_fence, NULL);308sctx->ws->fence_reference(&sctx->last_ib_barrier_fence, NULL);309si_resource_reference(&sctx->eop_bug_scratch, NULL);310si_resource_reference(&sctx->eop_bug_scratch_tmz, NULL);311si_resource_reference(&sctx->index_ring, NULL);312si_resource_reference(&sctx->barrier_buf, NULL);313si_resource_reference(&sctx->last_ib_barrier_buf, NULL);314si_resource_reference(&sctx->shadowed_regs, NULL);315radeon_bo_reference(sctx->screen->ws, &sctx->gds, NULL);316radeon_bo_reference(sctx->screen->ws, &sctx->gds_oa, NULL);317318si_destroy_compiler(&sctx->compiler);319320si_saved_cs_reference(&sctx->current_saved_cs, NULL);321322_mesa_hash_table_destroy(sctx->tex_handles, NULL);323_mesa_hash_table_destroy(sctx->img_handles, NULL);324325util_dynarray_fini(&sctx->resident_tex_handles);326util_dynarray_fini(&sctx->resident_img_handles);327util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);328util_dynarray_fini(&sctx->resident_img_needs_color_decompress);329util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);330331if (!(sctx->context_flags & SI_CONTEXT_FLAG_AUX))332p_atomic_dec(&context->screen->num_contexts);333334FREE(sctx);335}336337static enum pipe_reset_status si_get_reset_status(struct pipe_context *ctx)338{339struct si_context *sctx = (struct si_context *)ctx;340if (sctx->context_flags & SI_CONTEXT_FLAG_AUX)341return PIPE_NO_RESET;342343bool needs_reset;344enum pipe_reset_status status = sctx->ws->ctx_query_reset_status(sctx->ctx, false, &needs_reset);345346if (status != PIPE_NO_RESET && needs_reset && !(sctx->context_flags & SI_CONTEXT_FLAG_AUX)) {347/* Call the gallium frontend to set a no-op API dispatch. */348if (sctx->device_reset_callback.reset) {349sctx->device_reset_callback.reset(sctx->device_reset_callback.data, status);350}351}352return status;353}354355static void si_set_device_reset_callback(struct pipe_context *ctx,356const struct pipe_device_reset_callback *cb)357{358struct si_context *sctx = (struct si_context *)ctx;359360if (cb)361sctx->device_reset_callback = *cb;362else363memset(&sctx->device_reset_callback, 0, sizeof(sctx->device_reset_callback));364}365366/* Apitrace profiling:367* 1) qapitrace : Tools -> Profile: Measure CPU & GPU times368* 2) In the middle panel, zoom in (mouse wheel) on some bad draw call369* and remember its number.370* 3) In Mesa, enable queries and performance counters around that draw371* call and print the results.372* 4) glretrace --benchmark --markers ..373*/374static void si_emit_string_marker(struct pipe_context *ctx, const char *string, int len)375{376struct si_context *sctx = (struct si_context *)ctx;377378dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);379380if (sctx->thread_trace_enabled)381si_write_user_event(sctx, &sctx->gfx_cs, UserEventTrigger, string, len);382383if (sctx->log)384u_log_printf(sctx->log, "\nString marker: %*s\n", len, string);385}386387static void si_set_debug_callback(struct pipe_context *ctx, const struct pipe_debug_callback *cb)388{389struct si_context *sctx = (struct si_context *)ctx;390struct si_screen *screen = sctx->screen;391392util_queue_finish(&screen->shader_compiler_queue);393util_queue_finish(&screen->shader_compiler_queue_low_priority);394395if (cb)396sctx->debug = *cb;397else398memset(&sctx->debug, 0, sizeof(sctx->debug));399}400401static void si_set_log_context(struct pipe_context *ctx, struct u_log_context *log)402{403struct si_context *sctx = (struct si_context *)ctx;404sctx->log = log;405406if (log)407u_log_add_auto_logger(log, si_auto_log_cs, sctx);408}409410static void si_set_context_param(struct pipe_context *ctx, enum pipe_context_param param,411unsigned value)412{413struct radeon_winsys *ws = ((struct si_context *)ctx)->ws;414415switch (param) {416case PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE:417ws->pin_threads_to_L3_cache(ws, value);418break;419default:;420}421}422423static void si_set_frontend_noop(struct pipe_context *ctx, bool enable)424{425struct si_context *sctx = (struct si_context *)ctx;426427ctx->flush(ctx, NULL, PIPE_FLUSH_ASYNC);428sctx->is_noop = enable;429}430431static struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags)432{433struct si_screen *sscreen = (struct si_screen *)screen;434STATIC_ASSERT(DBG_COUNT <= 64);435436/* Don't create a context if it's not compute-only and hw is compute-only. */437if (!sscreen->info.has_graphics && !(flags & PIPE_CONTEXT_COMPUTE_ONLY))438return NULL;439440struct si_context *sctx = CALLOC_STRUCT(si_context);441struct radeon_winsys *ws = sscreen->ws;442int shader, i;443bool stop_exec_on_failure = (flags & PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET) != 0;444445if (!sctx)446return NULL;447448sctx->has_graphics = sscreen->info.chip_class == GFX6 || !(flags & PIPE_CONTEXT_COMPUTE_ONLY);449450if (flags & PIPE_CONTEXT_DEBUG)451sscreen->record_llvm_ir = true; /* racy but not critical */452453sctx->b.screen = screen; /* this must be set first */454sctx->b.priv = NULL;455sctx->b.destroy = si_destroy_context;456sctx->screen = sscreen; /* Easy accessing of screen/winsys. */457sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;458sctx->context_flags = flags;459460slab_create_child(&sctx->pool_transfers, &sscreen->pool_transfers);461slab_create_child(&sctx->pool_transfers_unsync, &sscreen->pool_transfers);462463sctx->ws = sscreen->ws;464sctx->family = sscreen->info.family;465sctx->chip_class = sscreen->info.chip_class;466467if (sctx->chip_class == GFX7 || sctx->chip_class == GFX8 || sctx->chip_class == GFX9) {468sctx->eop_bug_scratch = si_aligned_buffer_create(469&sscreen->b, SI_RESOURCE_FLAG_DRIVER_INTERNAL,470PIPE_USAGE_DEFAULT, 16 * sscreen->info.max_render_backends, 256);471if (sctx->screen->info.has_tmz_support)472sctx->eop_bug_scratch_tmz = si_aligned_buffer_create(473&sscreen->b, PIPE_RESOURCE_FLAG_ENCRYPTED | SI_RESOURCE_FLAG_DRIVER_INTERNAL,474PIPE_USAGE_DEFAULT, 16 * sscreen->info.max_render_backends, 256);475if (!sctx->eop_bug_scratch)476goto fail;477}478479/* Initialize the context handle and the command stream. */480sctx->ctx = sctx->ws->ctx_create(sctx->ws);481if (!sctx->ctx)482goto fail;483484ws->cs_create(&sctx->gfx_cs, sctx->ctx, sctx->has_graphics ? RING_GFX : RING_COMPUTE,485(void *)si_flush_gfx_cs, sctx, stop_exec_on_failure);486487/* Initialize private allocators. */488u_suballocator_init(&sctx->allocator_zeroed_memory, &sctx->b, 128 * 1024, 0,489PIPE_USAGE_DEFAULT,490SI_RESOURCE_FLAG_CLEAR | SI_RESOURCE_FLAG_32BIT, false);491492sctx->cached_gtt_allocator = u_upload_create(&sctx->b, 16 * 1024, 0, PIPE_USAGE_STAGING, 0);493if (!sctx->cached_gtt_allocator)494goto fail;495496/* Initialize public allocators. */497/* Unify uploaders as follows:498* - dGPUs with Smart Access Memory: there is only one uploader instance writing to VRAM.499* - APUs: There is only one uploader instance writing to RAM. VRAM has the same perf on APUs.500* - Other chips: The const uploader writes to VRAM and the stream uploader writes to RAM.501*/502bool smart_access_memory = sscreen->info.smart_access_memory;503bool is_apu = !sscreen->info.has_dedicated_vram;504sctx->b.stream_uploader =505u_upload_create(&sctx->b, 1024 * 1024, 0,506smart_access_memory && !is_apu ? PIPE_USAGE_DEFAULT : PIPE_USAGE_STREAM,507SI_RESOURCE_FLAG_32BIT); /* same flags as const_uploader */508if (!sctx->b.stream_uploader)509goto fail;510511if (smart_access_memory || is_apu) {512sctx->b.const_uploader = sctx->b.stream_uploader;513} else {514sctx->b.const_uploader =515u_upload_create(&sctx->b, 256 * 1024, 0, PIPE_USAGE_DEFAULT,516SI_RESOURCE_FLAG_32BIT);517if (!sctx->b.const_uploader)518goto fail;519}520521/* Border colors. */522if (sscreen->info.has_3d_cube_border_color_mipmap) {523sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table));524if (!sctx->border_color_table)525goto fail;526527sctx->border_color_buffer = si_resource(pipe_buffer_create(528screen, 0, PIPE_USAGE_DEFAULT, SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table)));529if (!sctx->border_color_buffer)530goto fail;531532sctx->border_color_map =533ws->buffer_map(ws, sctx->border_color_buffer->buf, NULL, PIPE_MAP_WRITE);534if (!sctx->border_color_map)535goto fail;536}537538sctx->ngg = sscreen->use_ngg;539540/* Initialize context functions used by graphics and compute. */541if (sctx->chip_class >= GFX10)542sctx->emit_cache_flush = gfx10_emit_cache_flush;543else544sctx->emit_cache_flush = si_emit_cache_flush;545546sctx->b.emit_string_marker = si_emit_string_marker;547sctx->b.set_debug_callback = si_set_debug_callback;548sctx->b.set_log_context = si_set_log_context;549sctx->b.set_context_param = si_set_context_param;550sctx->b.get_device_reset_status = si_get_reset_status;551sctx->b.set_device_reset_callback = si_set_device_reset_callback;552sctx->b.set_frontend_noop = si_set_frontend_noop;553554si_init_all_descriptors(sctx);555si_init_buffer_functions(sctx);556si_init_clear_functions(sctx);557si_init_blit_functions(sctx);558si_init_compute_functions(sctx);559si_init_compute_blit_functions(sctx);560si_init_debug_functions(sctx);561si_init_fence_functions(sctx);562si_init_query_functions(sctx);563si_init_state_compute_functions(sctx);564si_init_context_texture_functions(sctx);565566/* Initialize graphics-only context functions. */567if (sctx->has_graphics) {568if (sctx->chip_class >= GFX10)569gfx10_init_query(sctx);570si_init_msaa_functions(sctx);571si_init_shader_functions(sctx);572si_init_state_functions(sctx);573si_init_streamout_functions(sctx);574si_init_viewport_functions(sctx);575576sctx->blitter = util_blitter_create(&sctx->b);577if (sctx->blitter == NULL)578goto fail;579sctx->blitter->skip_viewport_restore = true;580581/* Some states are expected to be always non-NULL. */582sctx->noop_blend = util_blitter_get_noop_blend_state(sctx->blitter);583sctx->queued.named.blend = sctx->noop_blend;584585sctx->noop_dsa = util_blitter_get_noop_dsa_state(sctx->blitter);586sctx->queued.named.dsa = sctx->noop_dsa;587588sctx->no_velems_state = sctx->b.create_vertex_elements_state(&sctx->b, 0, NULL);589sctx->vertex_elements = sctx->no_velems_state;590591sctx->discard_rasterizer_state = util_blitter_get_discard_rasterizer_state(sctx->blitter);592sctx->queued.named.rasterizer = sctx->discard_rasterizer_state;593594switch (sctx->chip_class) {595case GFX6:596si_init_draw_functions_GFX6(sctx);597break;598case GFX7:599si_init_draw_functions_GFX7(sctx);600break;601case GFX8:602si_init_draw_functions_GFX8(sctx);603break;604case GFX9:605si_init_draw_functions_GFX9(sctx);606break;607case GFX10:608si_init_draw_functions_GFX10(sctx);609break;610case GFX10_3:611si_init_draw_functions_GFX10_3(sctx);612break;613default:614unreachable("unhandled chip class");615}616617si_initialize_prim_discard_tunables(sscreen, flags & SI_CONTEXT_FLAG_AUX,618&sctx->prim_discard_vertex_count_threshold,619&sctx->index_ring_size_per_ib);620} else {621sctx->prim_discard_vertex_count_threshold = UINT_MAX;622}623624sctx->sample_mask = 0xffff;625626/* Initialize multimedia functions. */627if (sscreen->info.has_video_hw.uvd_decode || sscreen->info.has_video_hw.vcn_decode ||628sscreen->info.has_video_hw.jpeg_decode || sscreen->info.has_video_hw.vce_encode ||629sscreen->info.has_video_hw.uvd_encode || sscreen->info.has_video_hw.vcn_encode) {630sctx->b.create_video_codec = si_uvd_create_decoder;631sctx->b.create_video_buffer = si_video_buffer_create;632if (screen->resource_create_with_modifiers)633sctx->b.create_video_buffer_with_modifiers = si_video_buffer_create_with_modifiers;634} else {635sctx->b.create_video_codec = vl_create_decoder;636sctx->b.create_video_buffer = vl_video_buffer_create;637}638639if (sctx->chip_class >= GFX9 || si_compute_prim_discard_enabled(sctx)) {640sctx->wait_mem_scratch =641si_aligned_buffer_create(screen,642SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL,643PIPE_USAGE_DEFAULT, 8,644sscreen->info.tcc_cache_line_size);645if (!sctx->wait_mem_scratch)646goto fail;647648if (sscreen->info.has_tmz_support) {649sctx->wait_mem_scratch_tmz =650si_aligned_buffer_create(screen,651SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL |652PIPE_RESOURCE_FLAG_ENCRYPTED,653PIPE_USAGE_DEFAULT, 8,654sscreen->info.tcc_cache_line_size);655if (!sctx->wait_mem_scratch_tmz)656goto fail;657}658}659660/* GFX7 cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads661* if NUM_RECORDS == 0). We need to use a dummy buffer instead. */662if (sctx->chip_class == GFX7) {663sctx->null_const_buf.buffer =664pipe_aligned_buffer_create(screen,665SI_RESOURCE_FLAG_32BIT | SI_RESOURCE_FLAG_DRIVER_INTERNAL,666PIPE_USAGE_DEFAULT, 16,667sctx->screen->info.tcc_cache_line_size);668if (!sctx->null_const_buf.buffer)669goto fail;670sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;671672unsigned start_shader = sctx->has_graphics ? 0 : PIPE_SHADER_COMPUTE;673for (shader = start_shader; shader < SI_NUM_SHADERS; shader++) {674for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {675sctx->b.set_constant_buffer(&sctx->b, shader, i, false, &sctx->null_const_buf);676}677}678679si_set_internal_const_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &sctx->null_const_buf);680si_set_internal_const_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &sctx->null_const_buf);681si_set_internal_const_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &sctx->null_const_buf);682si_set_internal_const_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &sctx->null_const_buf);683si_set_internal_const_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &sctx->null_const_buf);684}685686uint64_t max_threads_per_block;687screen->get_compute_param(screen, PIPE_SHADER_IR_NIR, PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,688&max_threads_per_block);689690/* The maximum number of scratch waves. Scratch space isn't divided691* evenly between CUs. The number is only a function of the number of CUs.692* We can decrease the constant to decrease the scratch buffer size.693*694* sctx->scratch_waves must be >= the maximum possible size of695* 1 threadgroup, so that the hw doesn't hang from being unable696* to start any.697*698* The recommended value is 4 per CU at most. Higher numbers don't699* bring much benefit, but they still occupy chip resources (think700* async compute). I've seen ~2% performance difference between 4 and 32.701*/702sctx->scratch_waves =703MAX2(32 * sscreen->info.num_good_compute_units, max_threads_per_block / 64);704705/* Bindless handles. */706sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);707sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);708709util_dynarray_init(&sctx->resident_tex_handles, NULL);710util_dynarray_init(&sctx->resident_img_handles, NULL);711util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);712util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);713util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);714715sctx->dirty_implicit_resources = _mesa_pointer_hash_table_create(NULL);716if (!sctx->dirty_implicit_resources)717goto fail;718719/* The remainder of this function initializes the gfx CS and must be last. */720assert(sctx->gfx_cs.current.cdw == 0);721722if (sctx->has_graphics) {723si_init_cp_reg_shadowing(sctx);724}725726si_begin_new_gfx_cs(sctx, true);727assert(sctx->gfx_cs.current.cdw == sctx->initial_gfx_cs_size);728729/* Initialize per-context buffers. */730if (sctx->wait_mem_scratch)731si_cp_write_data(sctx, sctx->wait_mem_scratch, 0, 4, V_370_MEM, V_370_ME,732&sctx->wait_mem_number);733if (sctx->wait_mem_scratch_tmz)734si_cp_write_data(sctx, sctx->wait_mem_scratch_tmz, 0, 4, V_370_MEM, V_370_ME,735&sctx->wait_mem_number);736737if (sctx->chip_class == GFX7) {738/* Clear the NULL constant buffer, because loads should return zeros.739* Note that this forces CP DMA to be used, because clover deadlocks740* for some reason when the compute codepath is used.741*/742uint32_t clear_value = 0;743si_clear_buffer(sctx, sctx->null_const_buf.buffer, 0, sctx->null_const_buf.buffer->width0,744&clear_value, 4, SI_OP_SYNC_AFTER, SI_COHERENCY_SHADER,745SI_CP_DMA_CLEAR_METHOD);746}747748if (!(flags & SI_CONTEXT_FLAG_AUX)) {749p_atomic_inc(&screen->num_contexts);750751/* Check if the aux_context needs to be recreated */752struct si_context *saux = (struct si_context *)sscreen->aux_context;753754simple_mtx_lock(&sscreen->aux_context_lock);755enum pipe_reset_status status = sctx->ws->ctx_query_reset_status(756saux->ctx, true, NULL);757if (status != PIPE_NO_RESET) {758/* We lost the aux_context, create a new one */759struct u_log_context *aux_log = (saux)->log;760sscreen->aux_context->set_log_context(sscreen->aux_context, NULL);761sscreen->aux_context->destroy(sscreen->aux_context);762763sscreen->aux_context = si_create_context(764&sscreen->b, SI_CONTEXT_FLAG_AUX |765(sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) |766(sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY));767sscreen->aux_context->set_log_context(sscreen->aux_context, aux_log);768}769simple_mtx_unlock(&sscreen->aux_context_lock);770}771772sctx->initial_gfx_cs_size = sctx->gfx_cs.current.cdw;773return &sctx->b;774fail:775fprintf(stderr, "radeonsi: Failed to create a context.\n");776si_destroy_context(&sctx->b);777return NULL;778}779780static bool si_is_resource_busy(struct pipe_screen *screen, struct pipe_resource *resource,781unsigned usage)782{783struct radeon_winsys *ws = ((struct si_screen *)screen)->ws;784785return !ws->buffer_wait(ws, si_resource(resource)->buf, 0,786/* If mapping for write, we need to wait for all reads and writes.787* If mapping for read, we only need to wait for writes.788*/789usage & PIPE_MAP_WRITE ? RADEON_USAGE_READWRITE : RADEON_USAGE_WRITE);790}791792static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen, void *priv,793unsigned flags)794{795struct si_screen *sscreen = (struct si_screen *)screen;796struct pipe_context *ctx;797uint64_t total_ram;798799if (sscreen->debug_flags & DBG(CHECK_VM))800flags |= PIPE_CONTEXT_DEBUG;801802ctx = si_create_context(screen, flags);803804if (ctx && sscreen->info.chip_class >= GFX9 && sscreen->debug_flags & DBG(SQTT)) {805if (!si_init_thread_trace((struct si_context *)ctx)) {806FREE(ctx);807return NULL;808}809}810811if (!(flags & PIPE_CONTEXT_PREFER_THREADED))812return ctx;813814/* Clover (compute-only) is unsupported. */815if (flags & PIPE_CONTEXT_COMPUTE_ONLY)816return ctx;817818/* When shaders are logged to stderr, asynchronous compilation is819* disabled too. */820if (sscreen->debug_flags & DBG_ALL_SHADERS)821return ctx;822823/* Use asynchronous flushes only on amdgpu, since the radeon824* implementation for fence_server_sync is incomplete. */825struct pipe_context *tc =826threaded_context_create(ctx, &sscreen->pool_transfers,827si_replace_buffer_storage,828sscreen->info.is_amdgpu ? si_create_fence : NULL,829si_is_resource_busy,830true,831&((struct si_context *)ctx)->tc);832833if (tc && tc != ctx && os_get_total_physical_memory(&total_ram)) {834((struct threaded_context *) tc)->bytes_mapped_limit = total_ram / 4;835}836837return tc;838}839840/*841* pipe_screen842*/843static void si_destroy_screen(struct pipe_screen *pscreen)844{845struct si_screen *sscreen = (struct si_screen *)pscreen;846struct si_shader_part *parts[] = {sscreen->vs_prologs, sscreen->tcs_epilogs, sscreen->gs_prologs,847sscreen->ps_prologs, sscreen->ps_epilogs};848unsigned i;849850if (!sscreen->ws->unref(sscreen->ws))851return;852853if (sscreen->debug_flags & DBG(CACHE_STATS)) {854printf("live shader cache: hits = %u, misses = %u\n", sscreen->live_shader_cache.hits,855sscreen->live_shader_cache.misses);856printf("memory shader cache: hits = %u, misses = %u\n", sscreen->num_memory_shader_cache_hits,857sscreen->num_memory_shader_cache_misses);858printf("disk shader cache: hits = %u, misses = %u\n", sscreen->num_disk_shader_cache_hits,859sscreen->num_disk_shader_cache_misses);860}861862simple_mtx_destroy(&sscreen->aux_context_lock);863864if (sscreen->aux_context) {865struct u_log_context *aux_log = ((struct si_context *)sscreen->aux_context)->log;866if (aux_log) {867sscreen->aux_context->set_log_context(sscreen->aux_context, NULL);868u_log_context_destroy(aux_log);869FREE(aux_log);870}871872sscreen->aux_context->destroy(sscreen->aux_context);873}874875util_queue_destroy(&sscreen->shader_compiler_queue);876util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);877878/* Release the reference on glsl types of the compiler threads. */879glsl_type_singleton_decref();880881for (i = 0; i < ARRAY_SIZE(sscreen->compiler); i++)882si_destroy_compiler(&sscreen->compiler[i]);883884for (i = 0; i < ARRAY_SIZE(sscreen->compiler_lowp); i++)885si_destroy_compiler(&sscreen->compiler_lowp[i]);886887/* Free shader parts. */888for (i = 0; i < ARRAY_SIZE(parts); i++) {889while (parts[i]) {890struct si_shader_part *part = parts[i];891892parts[i] = part->next;893si_shader_binary_clean(&part->binary);894FREE(part);895}896}897simple_mtx_destroy(&sscreen->shader_parts_mutex);898si_destroy_shader_cache(sscreen);899900si_destroy_perfcounters(sscreen);901si_gpu_load_kill_thread(sscreen);902903simple_mtx_destroy(&sscreen->gpu_load_mutex);904905slab_destroy_parent(&sscreen->pool_transfers);906907disk_cache_destroy(sscreen->disk_shader_cache);908util_live_shader_cache_deinit(&sscreen->live_shader_cache);909util_idalloc_mt_fini(&sscreen->buffer_ids);910911sscreen->ws->destroy(sscreen->ws);912FREE(sscreen);913}914915static void si_init_gs_info(struct si_screen *sscreen)916{917sscreen->gs_table_depth = ac_get_gs_table_depth(sscreen->info.chip_class, sscreen->info.family);918}919920static void si_test_vmfault(struct si_screen *sscreen, uint64_t test_flags)921{922struct pipe_context *ctx = sscreen->aux_context;923struct si_context *sctx = (struct si_context *)ctx;924struct pipe_resource *buf = pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64);925926if (!buf) {927puts("Buffer allocation failed.");928exit(1);929}930931si_resource(buf)->gpu_address = 0; /* cause a VM fault */932933if (test_flags & DBG(TEST_VMFAULT_CP)) {934si_cp_dma_copy_buffer(sctx, buf, buf, 0, 4, 4, SI_OP_SYNC_BEFORE_AFTER,935SI_COHERENCY_NONE, L2_BYPASS);936ctx->flush(ctx, NULL, 0);937puts("VM fault test: CP - done.");938}939if (test_flags & DBG(TEST_VMFAULT_SHADER)) {940util_test_constant_buffer(ctx, buf);941puts("VM fault test: Shader - done.");942}943exit(0);944}945946static void si_test_gds_memory_management(struct si_context *sctx, unsigned alloc_size,947unsigned alignment, enum radeon_bo_domain domain)948{949struct radeon_winsys *ws = sctx->ws;950struct radeon_cmdbuf cs[8];951struct pb_buffer *gds_bo[ARRAY_SIZE(cs)];952953for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) {954ws->cs_create(&cs[i], sctx->ctx, RING_COMPUTE, NULL, NULL, false);955gds_bo[i] = ws->buffer_create(ws, alloc_size, alignment, domain, 0);956assert(gds_bo[i]);957}958959for (unsigned iterations = 0; iterations < 20000; iterations++) {960for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) {961/* This clears GDS with CP DMA.962*963* We don't care if GDS is present. Just add some packet964* to make the GPU busy for a moment.965*/966si_cp_dma_clear_buffer(967sctx, &cs[i], NULL, 0, alloc_size, 0,968SI_OP_CPDMA_SKIP_CHECK_CS_SPACE, 0,9690);970971ws->cs_add_buffer(&cs[i], gds_bo[i], RADEON_USAGE_READWRITE, domain, 0);972ws->cs_flush(&cs[i], PIPE_FLUSH_ASYNC, NULL);973}974}975exit(0);976}977978static void si_disk_cache_create(struct si_screen *sscreen)979{980/* Don't use the cache if shader dumping is enabled. */981if (sscreen->debug_flags & DBG_ALL_SHADERS)982return;983984struct mesa_sha1 ctx;985unsigned char sha1[20];986char cache_id[20 * 2 + 1];987988_mesa_sha1_init(&ctx);989990if (!disk_cache_get_function_identifier(si_disk_cache_create, &ctx) ||991!disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx))992return;993994_mesa_sha1_final(&ctx, sha1);995disk_cache_format_hex_id(cache_id, sha1, 20 * 2);996997sscreen->disk_shader_cache = disk_cache_create(sscreen->info.name, cache_id,998sscreen->info.address32_hi);999}10001001static void si_set_max_shader_compiler_threads(struct pipe_screen *screen, unsigned max_threads)1002{1003struct si_screen *sscreen = (struct si_screen *)screen;10041005/* This function doesn't allow a greater number of threads than1006* the queue had at its creation. */1007util_queue_adjust_num_threads(&sscreen->shader_compiler_queue, max_threads);1008/* Don't change the number of threads on the low priority queue. */1009}10101011static bool si_is_parallel_shader_compilation_finished(struct pipe_screen *screen, void *shader,1012enum pipe_shader_type shader_type)1013{1014struct si_shader_selector *sel = (struct si_shader_selector *)shader;10151016return util_queue_fence_is_signalled(&sel->ready);1017}10181019static struct pipe_screen *radeonsi_screen_create_impl(struct radeon_winsys *ws,1020const struct pipe_screen_config *config)1021{1022struct si_screen *sscreen = CALLOC_STRUCT(si_screen);1023unsigned hw_threads, num_comp_hi_threads, num_comp_lo_threads;1024uint64_t test_flags;10251026if (!sscreen) {1027return NULL;1028}10291030{1031#define OPT_BOOL(name, dflt, description) \1032sscreen->options.name = driQueryOptionb(config->options, "radeonsi_" #name);1033#include "si_debug_options.h"1034}10351036sscreen->ws = ws;1037ws->query_info(ws, &sscreen->info,1038sscreen->options.enable_sam,1039sscreen->options.disable_sam);10401041if (sscreen->info.chip_class >= GFX9) {1042sscreen->se_tile_repeat = 32 * sscreen->info.max_se;1043} else {1044ac_get_raster_config(&sscreen->info, &sscreen->pa_sc_raster_config,1045&sscreen->pa_sc_raster_config_1, &sscreen->se_tile_repeat);1046}10471048sscreen->debug_flags = debug_get_flags_option("R600_DEBUG", radeonsi_debug_options, 0);1049sscreen->debug_flags |= debug_get_flags_option("AMD_DEBUG", radeonsi_debug_options, 0);1050test_flags = debug_get_flags_option("AMD_TEST", test_options, 0);10511052if (sscreen->debug_flags & DBG(NO_GFX))1053sscreen->info.has_graphics = false;10541055if ((sscreen->debug_flags & DBG(TMZ)) &&1056!sscreen->info.has_tmz_support) {1057fprintf(stderr, "radeonsi: requesting TMZ features but TMZ is not supported\n");1058FREE(sscreen);1059return NULL;1060}10611062util_idalloc_mt_init_tc(&sscreen->buffer_ids);10631064/* Set functions first. */1065sscreen->b.context_create = si_pipe_create_context;1066sscreen->b.destroy = si_destroy_screen;1067sscreen->b.set_max_shader_compiler_threads = si_set_max_shader_compiler_threads;1068sscreen->b.is_parallel_shader_compilation_finished = si_is_parallel_shader_compilation_finished;1069sscreen->b.finalize_nir = si_finalize_nir;10701071si_init_screen_get_functions(sscreen);1072si_init_screen_buffer_functions(sscreen);1073si_init_screen_fence_functions(sscreen);1074si_init_screen_state_functions(sscreen);1075si_init_screen_texture_functions(sscreen);1076si_init_screen_query_functions(sscreen);1077si_init_screen_live_shader_cache(sscreen);10781079/* Set these flags in debug_flags early, so that the shader cache takes1080* them into account.1081*1082* Enable FS_CORRECT_DERIVS_AFTER_KILL by default if LLVM is >= 13. This makes1083* nir_opt_move_discards_to_top more effective.1084*/1085if (driQueryOptionb(config->options, "glsl_correct_derivatives_after_discard") ||1086LLVM_VERSION_MAJOR >= 13)1087sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);10881089if (sscreen->debug_flags & DBG(INFO))1090ac_print_gpu_info(&sscreen->info, stdout);10911092slab_create_parent(&sscreen->pool_transfers, sizeof(struct si_transfer), 64);10931094sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));1095if (sscreen->force_aniso == -1) {1096sscreen->force_aniso = MIN2(16, debug_get_num_option("AMD_TEX_ANISO", -1));1097}10981099if (sscreen->force_aniso >= 0) {1100printf("radeonsi: Forcing anisotropy filter to %ix\n",1101/* round down to a power of two */11021 << util_logbase2(sscreen->force_aniso));1103}11041105(void)simple_mtx_init(&sscreen->aux_context_lock, mtx_plain);1106(void)simple_mtx_init(&sscreen->gpu_load_mutex, mtx_plain);11071108si_init_gs_info(sscreen);1109if (!si_init_shader_cache(sscreen)) {1110FREE(sscreen);1111return NULL;1112}11131114if (sscreen->info.chip_class < GFX10_3)1115sscreen->options.vrs2x2 = false;11161117si_disk_cache_create(sscreen);11181119/* Determine the number of shader compiler threads. */1120const struct util_cpu_caps_t *caps = util_get_cpu_caps();1121hw_threads = caps->nr_cpus;11221123if (hw_threads >= 12) {1124num_comp_hi_threads = hw_threads * 3 / 4;1125num_comp_lo_threads = hw_threads / 3;1126} else if (hw_threads >= 6) {1127num_comp_hi_threads = hw_threads - 2;1128num_comp_lo_threads = hw_threads / 2;1129} else if (hw_threads >= 2) {1130num_comp_hi_threads = hw_threads - 1;1131num_comp_lo_threads = hw_threads / 2;1132} else {1133num_comp_hi_threads = 1;1134num_comp_lo_threads = 1;1135}11361137num_comp_hi_threads = MIN2(num_comp_hi_threads, ARRAY_SIZE(sscreen->compiler));1138num_comp_lo_threads = MIN2(num_comp_lo_threads, ARRAY_SIZE(sscreen->compiler_lowp));11391140/* Take a reference on the glsl types for the compiler threads. */1141glsl_type_singleton_init_or_ref();11421143if (!util_queue_init(1144&sscreen->shader_compiler_queue, "sh", 64, num_comp_hi_threads,1145UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY, NULL)) {1146si_destroy_shader_cache(sscreen);1147FREE(sscreen);1148glsl_type_singleton_decref();1149return NULL;1150}11511152if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority, "shlo", 64,1153num_comp_lo_threads,1154UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY |1155UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY, NULL)) {1156si_destroy_shader_cache(sscreen);1157FREE(sscreen);1158glsl_type_singleton_decref();1159return NULL;1160}11611162if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))1163si_init_perfcounters(sscreen);11641165unsigned prim_discard_vertex_count_threshold, tmp;1166si_initialize_prim_discard_tunables(sscreen, false, &prim_discard_vertex_count_threshold, &tmp);1167/* Compute-shader-based culling doesn't support VBOs in user SGPRs. */1168if (prim_discard_vertex_count_threshold == UINT_MAX) {1169/* This decreases CPU overhead if all descriptors are in user SGPRs because we don't1170* have to allocate and count references for the upload buffer.1171*/1172sscreen->num_vbos_in_user_sgprs = sscreen->info.chip_class >= GFX9 ? 5 : 1;1173}11741175/* Determine tessellation ring info. */1176bool double_offchip_buffers = sscreen->info.chip_class >= GFX7 &&1177sscreen->info.family != CHIP_CARRIZO &&1178sscreen->info.family != CHIP_STONEY;1179/* This must be one less than the maximum number due to a hw limitation.1180* Various hardware bugs need this.1181*/1182unsigned max_offchip_buffers_per_se;11831184if (sscreen->info.chip_class >= GFX10)1185max_offchip_buffers_per_se = 128;1186/* Only certain chips can use the maximum value. */1187else if (sscreen->info.family == CHIP_VEGA12 || sscreen->info.family == CHIP_VEGA20)1188max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;1189else1190max_offchip_buffers_per_se = double_offchip_buffers ? 127 : 63;11911192unsigned max_offchip_buffers = max_offchip_buffers_per_se * sscreen->info.max_se;1193unsigned offchip_granularity;11941195/* Hawaii has a bug with offchip buffers > 256 that can be worked1196* around by setting 4K granularity.1197*/1198if (sscreen->info.family == CHIP_HAWAII) {1199sscreen->tess_offchip_block_dw_size = 4096;1200offchip_granularity = V_03093C_X_4K_DWORDS;1201} else {1202sscreen->tess_offchip_block_dw_size = 8192;1203offchip_granularity = V_03093C_X_8K_DWORDS;1204}12051206sscreen->tess_factor_ring_size = 32768 * sscreen->info.max_se;1207sscreen->tess_offchip_ring_size = max_offchip_buffers * sscreen->tess_offchip_block_dw_size * 4;12081209if (sscreen->info.chip_class >= GFX10_3) {1210sscreen->vgt_hs_offchip_param =1211S_03093C_OFFCHIP_BUFFERING_GFX103(max_offchip_buffers - 1) |1212S_03093C_OFFCHIP_GRANULARITY_GFX103(offchip_granularity);1213} else if (sscreen->info.chip_class >= GFX7) {1214if (sscreen->info.chip_class >= GFX8)1215--max_offchip_buffers;1216sscreen->vgt_hs_offchip_param = S_03093C_OFFCHIP_BUFFERING_GFX7(max_offchip_buffers) |1217S_03093C_OFFCHIP_GRANULARITY_GFX7(offchip_granularity);1218} else {1219assert(offchip_granularity == V_03093C_X_8K_DWORDS);1220sscreen->vgt_hs_offchip_param = S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);1221}12221223sscreen->has_draw_indirect_multi =1224(sscreen->info.family >= CHIP_POLARIS10) ||1225(sscreen->info.chip_class == GFX8 && sscreen->info.pfp_fw_version >= 121 &&1226sscreen->info.me_fw_version >= 87) ||1227(sscreen->info.chip_class == GFX7 && sscreen->info.pfp_fw_version >= 211 &&1228sscreen->info.me_fw_version >= 173) ||1229(sscreen->info.chip_class == GFX6 && sscreen->info.pfp_fw_version >= 79 &&1230sscreen->info.me_fw_version >= 142);12311232sscreen->has_out_of_order_rast =1233sscreen->info.has_out_of_order_rast && !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER));1234sscreen->assume_no_z_fights = driQueryOptionb(config->options, "radeonsi_assume_no_z_fights") ||1235driQueryOptionb(config->options, "allow_draw_out_of_order");1236sscreen->commutative_blend_add =1237driQueryOptionb(config->options, "radeonsi_commutative_blend_add") ||1238driQueryOptionb(config->options, "allow_draw_out_of_order");1239sscreen->allow_draw_out_of_order = driQueryOptionb(config->options, "allow_draw_out_of_order");12401241sscreen->use_ngg = !(sscreen->debug_flags & DBG(NO_NGG)) &&1242sscreen->info.chip_class >= GFX10 &&1243(sscreen->info.family != CHIP_NAVI14 ||1244sscreen->info.is_pro_graphics);1245sscreen->use_ngg_culling = sscreen->use_ngg &&1246sscreen->info.max_render_backends >= 2 &&1247!((sscreen->debug_flags & DBG(NO_NGG_CULLING)) ||1248LLVM_VERSION_MAJOR <= 11 /* hangs on 11, see #4874 */);1249sscreen->use_ngg_streamout = false;12501251/* Only set this for the cases that are known to work, which are:1252* - GFX9 if bpp >= 4 (in bytes)1253*/1254if (sscreen->info.chip_class == GFX9) {1255for (unsigned bpp_log2 = util_logbase2(4); bpp_log2 <= util_logbase2(16); bpp_log2++)1256sscreen->allow_dcc_msaa_clear_to_reg_for_bpp[bpp_log2] = true;1257}12581259sscreen->dpbb_allowed = !(sscreen->debug_flags & DBG(NO_DPBB)) &&1260(sscreen->info.chip_class >= GFX10 ||1261/* Only enable primitive binning on gfx9 APUs by default. */1262(sscreen->info.chip_class == GFX9 && !sscreen->info.has_dedicated_vram) ||1263sscreen->debug_flags & DBG(DPBB));12641265if (sscreen->dpbb_allowed) {1266if (sscreen->info.has_dedicated_vram) {1267if (sscreen->info.max_render_backends > 4) {1268sscreen->pbb_context_states_per_bin = 1;1269sscreen->pbb_persistent_states_per_bin = 1;1270} else {1271sscreen->pbb_context_states_per_bin = 3;1272sscreen->pbb_persistent_states_per_bin = 8;1273}1274} else {1275/* This is a workaround for:1276* https://bugs.freedesktop.org/show_bug.cgi?id=1102141277* (an alternative is to insert manual BATCH_BREAK event when1278* a context_roll is detected). */1279sscreen->pbb_context_states_per_bin = sscreen->info.has_gfx9_scissor_bug ? 1 : 6;1280/* Using 32 here can cause GPU hangs on RAVEN1 */1281sscreen->pbb_persistent_states_per_bin = 16;1282}12831284assert(sscreen->pbb_context_states_per_bin >= 1 &&1285sscreen->pbb_context_states_per_bin <= 6);1286assert(sscreen->pbb_persistent_states_per_bin >= 1 &&1287sscreen->pbb_persistent_states_per_bin <= 32);1288}12891290(void)simple_mtx_init(&sscreen->shader_parts_mutex, mtx_plain);1291sscreen->use_monolithic_shaders = (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;12921293sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE;1294if (sscreen->info.chip_class <= GFX8) {1295sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_L2;1296sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WB_L2;1297}12981299if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))1300sscreen->debug_flags |= DBG_ALL_SHADERS;13011302/* Syntax:1303* EQAA=s,z,c1304* Example:1305* EQAA=8,4,213061307* That means 8 coverage samples, 4 Z/S samples, and 2 color samples.1308* Constraints:1309* s >= z >= c (ignoring this only wastes memory)1310* s = [2..16]1311* z = [2..8]1312* c = [2..8]1313*1314* Only MSAA color and depth buffers are overriden.1315*/1316if (sscreen->info.has_eqaa_surface_allocator) {1317const char *eqaa = debug_get_option("EQAA", NULL);1318unsigned s, z, f;13191320if (eqaa && sscanf(eqaa, "%u,%u,%u", &s, &z, &f) == 3 && s && z && f) {1321sscreen->eqaa_force_coverage_samples = s;1322sscreen->eqaa_force_z_samples = z;1323sscreen->eqaa_force_color_samples = f;1324}1325}13261327sscreen->ngg_subgroup_size = 128;1328sscreen->ge_wave_size = 64;1329sscreen->ps_wave_size = 64;1330sscreen->compute_wave_size = 64;13311332if (sscreen->info.chip_class >= GFX10) {1333/* Pixel shaders: Wave64 is always fastest.1334* Vertex shaders: Wave64 is probably better, because:1335* - greater chance of L0 cache hits, because more threads are assigned1336* to the same CU1337* - scalar instructions are only executed once for 64 threads instead of twice1338* - VGPR allocation granularity is half of Wave32, so 1 Wave64 can1339* sometimes use fewer VGPRs than 2 Wave321340* - TessMark X64 with NGG culling is faster with Wave641341*/1342if (sscreen->debug_flags & DBG(W32_GE))1343sscreen->ge_wave_size = 32;1344if (sscreen->debug_flags & DBG(W32_PS))1345sscreen->ps_wave_size = 32;1346if (sscreen->debug_flags & DBG(W32_CS))1347sscreen->compute_wave_size = 32;13481349if (sscreen->debug_flags & DBG(W64_GE))1350sscreen->ge_wave_size = 64;1351if (sscreen->debug_flags & DBG(W64_PS))1352sscreen->ps_wave_size = 64;1353if (sscreen->debug_flags & DBG(W64_CS))1354sscreen->compute_wave_size = 64;1355}13561357/* Create the auxiliary context. This must be done last. */1358sscreen->aux_context = si_create_context(1359&sscreen->b,1360SI_CONTEXT_FLAG_AUX |1361(sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) |1362(sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY));13631364if (sscreen->options.aux_debug) {1365struct u_log_context *log = CALLOC_STRUCT(u_log_context);1366u_log_context_init(log);1367sscreen->aux_context->set_log_context(sscreen->aux_context, log);1368}13691370if (test_flags & DBG(TEST_BLIT))1371si_test_blit(sscreen);13721373if (test_flags & DBG(TEST_DMA_PERF)) {1374si_test_dma_perf(sscreen);1375}13761377if (test_flags & (DBG(TEST_VMFAULT_CP) | DBG(TEST_VMFAULT_SHADER)))1378si_test_vmfault(sscreen, test_flags);13791380if (test_flags & DBG(TEST_GDS))1381si_test_gds((struct si_context *)sscreen->aux_context);13821383if (test_flags & DBG(TEST_GDS_MM)) {1384si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 32 * 1024, 4,1385RADEON_DOMAIN_GDS);1386}1387if (test_flags & DBG(TEST_GDS_OA_MM)) {1388si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 4, 1,1389RADEON_DOMAIN_OA);1390}13911392ac_print_shadowed_regs(&sscreen->info);13931394STATIC_ASSERT(sizeof(union si_vgt_stages_key) == 1);1395return &sscreen->b;1396}13971398struct pipe_screen *radeonsi_screen_create(int fd, const struct pipe_screen_config *config)1399{1400drmVersionPtr version = drmGetVersion(fd);1401struct radeon_winsys *rw = NULL;14021403switch (version->version_major) {1404case 2:1405rw = radeon_drm_winsys_create(fd, config, radeonsi_screen_create_impl);1406break;1407case 3:1408rw = amdgpu_winsys_create(fd, config, radeonsi_screen_create_impl);1409break;1410}14111412drmFreeVersion(version);1413return rw ? rw->screen : NULL;1414}141514161417