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