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_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"
45
46 #include <xf86drm.h>
47
48 static struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags);
49
50 static const struct debug_named_value 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"},
62
63 /* 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."},
71
72 /* 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."},
76
77 /* 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."},
83
84 /* Driver options: */
85 {"forcedma", DBG(FORCE_SDMA), "Use SDMA for all operations when possible."},
86 {"nodma", DBG(NO_SDMA), "Disable SDMA"},
87 {"nodmaclear", DBG(NO_SDMA_CLEARS), "Disable SDMA clears"},
88 {"nodmacopyimage", DBG(NO_SDMA_COPY_IMAGE), "Disable SDMA image copies"},
89 {"nowc", DBG(NO_WC), "Disable GTT write combining"},
90 {"check_vm", DBG(CHECK_VM), "Check VM faults and dump debug info."},
91 {"reserve_vmid", DBG(RESERVE_VMID), "Force VMID reservation per context."},
92 {"zerovram", DBG(ZERO_VRAM), "Clear VRAM allocations."},
93 {"shadowregs", DBG(SHADOW_REGS), "Enable CP register shadowing."},
94
95 /* 3D engine options: */
96 {"nogfx", DBG(NO_GFX), "Disable graphics. Only multimedia compute paths can be used."},
97 {"nongg", DBG(NO_NGG), "Disable NGG and use the legacy pipeline."},
98 {"nggc", DBG(ALWAYS_NGG_CULLING_ALL), "Always use NGG culling even when it can hurt."},
99 {"nggctess", DBG(ALWAYS_NGG_CULLING_TESS), "Always use NGG culling for tessellation."},
100 {"nonggc", DBG(NO_NGG_CULLING), "Disable NGG culling."},
101 {"alwayspd", DBG(ALWAYS_PD), "Always enable the primitive discard compute shader."},
102 {"pd", DBG(PD), "Enable the primitive discard compute shader for large draw calls."},
103 {"nopd", DBG(NO_PD), "Disable the primitive discard compute shader."},
104 {"switch_on_eop", DBG(SWITCH_ON_EOP), "Program WD/IA to switch on end-of-packet."},
105 {"nooutoforder", DBG(NO_OUT_OF_ORDER), "Disable out-of-order rasterization"},
106 {"nodpbb", DBG(NO_DPBB), "Disable DPBB."},
107 {"nodfsm", DBG(NO_DFSM), "Disable DFSM."},
108 {"dpbb", DBG(DPBB), "Enable DPBB."},
109 {"dfsm", DBG(DFSM), "Enable DFSM."},
110 {"nohyperz", DBG(NO_HYPERZ), "Disable Hyper-Z"},
111 {"norbplus", DBG(NO_RB_PLUS), "Disable RB+."},
112 {"no2d", DBG(NO_2D_TILING), "Disable 2D tiling"},
113 {"notiling", DBG(NO_TILING), "Disable tiling"},
114 {"nodcc", DBG(NO_DCC), "Disable DCC."},
115 {"nodccclear", DBG(NO_DCC_CLEAR), "Disable DCC fast clear."},
116 {"nodccfb", DBG(NO_DCC_FB), "Disable separate DCC on the main framebuffer"},
117 {"nodccmsaa", DBG(NO_DCC_MSAA), "Disable DCC for MSAA"},
118 {"nofmask", DBG(NO_FMASK), "Disable MSAA compression"},
119
120 {"tmz", DBG(TMZ), "Force allocation of scanout/depth/stencil buffer as encrypted"},
121
122 DEBUG_NAMED_VALUE_END /* must be last */
123 };
124
125 static const struct debug_named_value test_options[] = {
126 /* Tests: */
127 {"testdma", DBG(TEST_DMA), "Invoke SDMA tests and exit."},
128 {"testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit."},
129 {"testvmfaultsdma", DBG(TEST_VMFAULT_SDMA), "Invoke a SDMA VM fault test and exit."},
130 {"testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit."},
131 {"testdmaperf", DBG(TEST_DMA_PERF), "Test DMA performance"},
132 {"testgds", DBG(TEST_GDS), "Test GDS."},
133 {"testgdsmm", DBG(TEST_GDS_MM), "Test GDS memory management."},
134 {"testgdsoamm", DBG(TEST_GDS_OA_MM), "Test GDS OA memory management."},
135
136 DEBUG_NAMED_VALUE_END /* must be last */
137 };
138
si_init_compiler(struct si_screen * sscreen,struct ac_llvm_compiler * compiler)139 void si_init_compiler(struct si_screen *sscreen, struct ac_llvm_compiler *compiler)
140 {
141 /* Only create the less-optimizing version of the compiler on APUs
142 * predating Ryzen (Raven). */
143 bool create_low_opt_compiler =
144 !sscreen->info.has_dedicated_vram && sscreen->info.chip_class <= GFX8;
145
146 enum ac_target_machine_options tm_options =
147 (sscreen->debug_flags & DBG(GISEL) ? AC_TM_ENABLE_GLOBAL_ISEL : 0) |
148 (sscreen->info.chip_class <= GFX8 ? AC_TM_FORCE_DISABLE_XNACK :
149 sscreen->info.chip_class <= GFX10 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
150 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0) |
151 (sscreen->debug_flags & DBG(CHECK_IR) ? AC_TM_CHECK_IR : 0) |
152 (create_low_opt_compiler ? AC_TM_CREATE_LOW_OPT : 0);
153
154 ac_init_llvm_once();
155 ac_init_llvm_compiler(compiler, sscreen->info.family, tm_options);
156 compiler->passes = ac_create_llvm_passes(compiler->tm);
157
158 if (compiler->tm_wave32)
159 compiler->passes_wave32 = ac_create_llvm_passes(compiler->tm_wave32);
160 if (compiler->low_opt_tm)
161 compiler->low_opt_passes = ac_create_llvm_passes(compiler->low_opt_tm);
162 }
163
si_destroy_compiler(struct ac_llvm_compiler * compiler)164 static void si_destroy_compiler(struct ac_llvm_compiler *compiler)
165 {
166 ac_destroy_llvm_compiler(compiler);
167 }
168
169 /*
170 * pipe_context
171 */
si_destroy_context(struct pipe_context * context)172 static void si_destroy_context(struct pipe_context *context)
173 {
174 struct si_context *sctx = (struct si_context *)context;
175 int i;
176
177 /* Unreference the framebuffer normally to disable related logic
178 * properly.
179 */
180 struct pipe_framebuffer_state fb = {};
181 if (context->set_framebuffer_state)
182 context->set_framebuffer_state(context, &fb);
183
184 si_release_all_descriptors(sctx);
185
186 if (sctx->chip_class >= GFX10 && sctx->has_graphics)
187 gfx10_destroy_query(sctx);
188
189 pipe_resource_reference(&sctx->esgs_ring, NULL);
190 pipe_resource_reference(&sctx->gsvs_ring, NULL);
191 pipe_resource_reference(&sctx->tess_rings, NULL);
192 pipe_resource_reference(&sctx->tess_rings_tmz, NULL);
193 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
194 pipe_resource_reference(&sctx->sample_pos_buffer, NULL);
195 si_resource_reference(&sctx->border_color_buffer, NULL);
196 free(sctx->border_color_table);
197 si_resource_reference(&sctx->scratch_buffer, NULL);
198 si_resource_reference(&sctx->compute_scratch_buffer, NULL);
199 si_resource_reference(&sctx->wait_mem_scratch, NULL);
200 si_resource_reference(&sctx->wait_mem_scratch_tmz, NULL);
201 si_resource_reference(&sctx->small_prim_cull_info_buf, NULL);
202
203 if (sctx->cs_preamble_state)
204 si_pm4_free_state(sctx, sctx->cs_preamble_state, ~0);
205 if (sctx->cs_preamble_tess_rings)
206 si_pm4_free_state(sctx, sctx->cs_preamble_tess_rings, ~0);
207 if (sctx->cs_preamble_tess_rings_tmz)
208 si_pm4_free_state(sctx, sctx->cs_preamble_tess_rings_tmz, ~0);
209 if (sctx->cs_preamble_gs_rings)
210 si_pm4_free_state(sctx, sctx->cs_preamble_gs_rings, ~0);
211 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
212 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
213
214 if (sctx->fixed_func_tcs_shader.cso)
215 sctx->b.delete_tcs_state(&sctx->b, sctx->fixed_func_tcs_shader.cso);
216 if (sctx->custom_dsa_flush)
217 sctx->b.delete_depth_stencil_alpha_state(&sctx->b, sctx->custom_dsa_flush);
218 if (sctx->custom_blend_resolve)
219 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_resolve);
220 if (sctx->custom_blend_fmask_decompress)
221 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_fmask_decompress);
222 if (sctx->custom_blend_eliminate_fastclear)
223 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_eliminate_fastclear);
224 if (sctx->custom_blend_dcc_decompress)
225 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_dcc_decompress);
226 if (sctx->vs_blit_pos)
227 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos);
228 if (sctx->vs_blit_pos_layered)
229 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos_layered);
230 if (sctx->vs_blit_color)
231 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color);
232 if (sctx->vs_blit_color_layered)
233 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color_layered);
234 if (sctx->vs_blit_texcoord)
235 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_texcoord);
236 if (sctx->cs_clear_buffer)
237 sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_buffer);
238 if (sctx->cs_copy_buffer)
239 sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_buffer);
240 if (sctx->cs_copy_image)
241 sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_image);
242 if (sctx->cs_copy_image_1d_array)
243 sctx->b.delete_compute_state(&sctx->b, sctx->cs_copy_image_1d_array);
244 if (sctx->cs_clear_render_target)
245 sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target);
246 if (sctx->cs_clear_render_target_1d_array)
247 sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_render_target_1d_array);
248 if (sctx->cs_clear_12bytes_buffer)
249 sctx->b.delete_compute_state(&sctx->b, sctx->cs_clear_12bytes_buffer);
250 if (sctx->cs_dcc_decompress)
251 sctx->b.delete_compute_state(&sctx->b, sctx->cs_dcc_decompress);
252 if (sctx->cs_dcc_retile)
253 sctx->b.delete_compute_state(&sctx->b, sctx->cs_dcc_retile);
254
255 for (unsigned i = 0; i < ARRAY_SIZE(sctx->cs_fmask_expand); i++) {
256 for (unsigned j = 0; j < ARRAY_SIZE(sctx->cs_fmask_expand[i]); j++) {
257 if (sctx->cs_fmask_expand[i][j]) {
258 sctx->b.delete_compute_state(&sctx->b, sctx->cs_fmask_expand[i][j]);
259 }
260 }
261 }
262
263 if (sctx->blitter)
264 util_blitter_destroy(sctx->blitter);
265
266 /* Release DCC stats. */
267 for (int i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++) {
268 assert(!sctx->dcc_stats[i].query_active);
269
270 for (int j = 0; j < ARRAY_SIZE(sctx->dcc_stats[i].ps_stats); j++)
271 if (sctx->dcc_stats[i].ps_stats[j])
272 sctx->b.destroy_query(&sctx->b, sctx->dcc_stats[i].ps_stats[j]);
273
274 si_texture_reference(&sctx->dcc_stats[i].tex, NULL);
275 }
276
277 if (sctx->query_result_shader)
278 sctx->b.delete_compute_state(&sctx->b, sctx->query_result_shader);
279 if (sctx->sh_query_result_shader)
280 sctx->b.delete_compute_state(&sctx->b, sctx->sh_query_result_shader);
281
282 if (sctx->gfx_cs)
283 sctx->ws->cs_destroy(sctx->gfx_cs);
284 if (sctx->sdma_cs)
285 sctx->ws->cs_destroy(sctx->sdma_cs);
286 if (sctx->ctx)
287 sctx->ws->ctx_destroy(sctx->ctx);
288
289 if (sctx->b.stream_uploader)
290 u_upload_destroy(sctx->b.stream_uploader);
291 if (sctx->b.const_uploader)
292 u_upload_destroy(sctx->b.const_uploader);
293 if (sctx->cached_gtt_allocator)
294 u_upload_destroy(sctx->cached_gtt_allocator);
295
296 slab_destroy_child(&sctx->pool_transfers);
297 slab_destroy_child(&sctx->pool_transfers_unsync);
298
299 if (sctx->allocator_zeroed_memory)
300 u_suballocator_destroy(sctx->allocator_zeroed_memory);
301
302 sctx->ws->fence_reference(&sctx->last_gfx_fence, NULL);
303 sctx->ws->fence_reference(&sctx->last_sdma_fence, NULL);
304 sctx->ws->fence_reference(&sctx->last_ib_barrier_fence, NULL);
305 si_resource_reference(&sctx->eop_bug_scratch, NULL);
306 si_resource_reference(&sctx->eop_bug_scratch_tmz, NULL);
307 si_resource_reference(&sctx->index_ring, NULL);
308 si_resource_reference(&sctx->barrier_buf, NULL);
309 si_resource_reference(&sctx->last_ib_barrier_buf, NULL);
310 si_resource_reference(&sctx->shadowed_regs, NULL);
311 pb_reference(&sctx->gds, NULL);
312 pb_reference(&sctx->gds_oa, NULL);
313
314 si_destroy_compiler(&sctx->compiler);
315
316 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
317
318 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
319 _mesa_hash_table_destroy(sctx->img_handles, NULL);
320
321 util_dynarray_fini(&sctx->resident_tex_handles);
322 util_dynarray_fini(&sctx->resident_img_handles);
323 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
324 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
325 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
326 si_unref_sdma_uploads(sctx);
327 free(sctx->sdma_uploads);
328 FREE(sctx);
329 }
330
si_get_reset_status(struct pipe_context * ctx)331 static enum pipe_reset_status si_get_reset_status(struct pipe_context *ctx)
332 {
333 struct si_context *sctx = (struct si_context *)ctx;
334 struct si_screen *sscreen = sctx->screen;
335 enum pipe_reset_status status = sctx->ws->ctx_query_reset_status(sctx->ctx);
336
337 if (status != PIPE_NO_RESET) {
338 /* Call the gallium frontend to set a no-op API dispatch. */
339 if (sctx->device_reset_callback.reset) {
340 sctx->device_reset_callback.reset(sctx->device_reset_callback.data, status);
341 }
342
343 /* Re-create the auxiliary context, because it won't submit
344 * any new IBs due to a GPU reset.
345 */
346 simple_mtx_lock(&sscreen->aux_context_lock);
347
348 struct u_log_context *aux_log = ((struct si_context *)sscreen->aux_context)->log;
349 sscreen->aux_context->set_log_context(sscreen->aux_context, NULL);
350 sscreen->aux_context->destroy(sscreen->aux_context);
351
352 sscreen->aux_context = si_create_context(
353 &sscreen->b, (sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) |
354 (sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY));
355 sscreen->aux_context->set_log_context(sscreen->aux_context, aux_log);
356 simple_mtx_unlock(&sscreen->aux_context_lock);
357 }
358 return status;
359 }
360
si_set_device_reset_callback(struct pipe_context * ctx,const struct pipe_device_reset_callback * cb)361 static void si_set_device_reset_callback(struct pipe_context *ctx,
362 const struct pipe_device_reset_callback *cb)
363 {
364 struct si_context *sctx = (struct si_context *)ctx;
365
366 if (cb)
367 sctx->device_reset_callback = *cb;
368 else
369 memset(&sctx->device_reset_callback, 0, sizeof(sctx->device_reset_callback));
370 }
371
372 /* Apitrace profiling:
373 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
374 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
375 * and remember its number.
376 * 3) In Mesa, enable queries and performance counters around that draw
377 * call and print the results.
378 * 4) glretrace --benchmark --markers ..
379 */
si_emit_string_marker(struct pipe_context * ctx,const char * string,int len)380 static void si_emit_string_marker(struct pipe_context *ctx, const char *string, int len)
381 {
382 struct si_context *sctx = (struct si_context *)ctx;
383
384 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
385
386 if (sctx->log)
387 u_log_printf(sctx->log, "\nString marker: %*s\n", len, string);
388 }
389
si_set_debug_callback(struct pipe_context * ctx,const struct pipe_debug_callback * cb)390 static void si_set_debug_callback(struct pipe_context *ctx, const struct pipe_debug_callback *cb)
391 {
392 struct si_context *sctx = (struct si_context *)ctx;
393 struct si_screen *screen = sctx->screen;
394
395 util_queue_finish(&screen->shader_compiler_queue);
396 util_queue_finish(&screen->shader_compiler_queue_low_priority);
397
398 if (cb)
399 sctx->debug = *cb;
400 else
401 memset(&sctx->debug, 0, sizeof(sctx->debug));
402 }
403
si_set_log_context(struct pipe_context * ctx,struct u_log_context * log)404 static void si_set_log_context(struct pipe_context *ctx, struct u_log_context *log)
405 {
406 struct si_context *sctx = (struct si_context *)ctx;
407 sctx->log = log;
408
409 if (log)
410 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
411 }
412
si_set_context_param(struct pipe_context * ctx,enum pipe_context_param param,unsigned value)413 static void si_set_context_param(struct pipe_context *ctx, enum pipe_context_param param,
414 unsigned value)
415 {
416 struct radeon_winsys *ws = ((struct si_context *)ctx)->ws;
417
418 switch (param) {
419 case PIPE_CONTEXT_PARAM_PIN_THREADS_TO_L3_CACHE:
420 ws->pin_threads_to_L3_cache(ws, value);
421 break;
422 default:;
423 }
424 }
425
si_set_frontend_noop(struct pipe_context * ctx,bool enable)426 static void si_set_frontend_noop(struct pipe_context *ctx, bool enable)
427 {
428 struct si_context *sctx = (struct si_context *)ctx;
429
430 ctx->flush(ctx, NULL, PIPE_FLUSH_ASYNC);
431 sctx->is_noop = enable;
432 }
433
si_create_context(struct pipe_screen * screen,unsigned flags)434 static struct pipe_context *si_create_context(struct pipe_screen *screen, unsigned flags)
435 {
436 struct si_screen *sscreen = (struct si_screen *)screen;
437 STATIC_ASSERT(DBG_COUNT <= 64);
438
439 /* Don't create a context if it's not compute-only and hw is compute-only. */
440 if (!sscreen->info.has_graphics && !(flags & PIPE_CONTEXT_COMPUTE_ONLY))
441 return NULL;
442
443 struct si_context *sctx = CALLOC_STRUCT(si_context);
444 struct radeon_winsys *ws = sscreen->ws;
445 int shader, i;
446 bool stop_exec_on_failure = (flags & PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET) != 0;
447
448 if (!sctx)
449 return NULL;
450
451 sctx->has_graphics = sscreen->info.chip_class == GFX6 || !(flags & PIPE_CONTEXT_COMPUTE_ONLY);
452
453 if (flags & PIPE_CONTEXT_DEBUG)
454 sscreen->record_llvm_ir = true; /* racy but not critical */
455
456 sctx->b.screen = screen; /* this must be set first */
457 sctx->b.priv = NULL;
458 sctx->b.destroy = si_destroy_context;
459 sctx->screen = sscreen; /* Easy accessing of screen/winsys. */
460 sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;
461
462 slab_create_child(&sctx->pool_transfers, &sscreen->pool_transfers);
463 slab_create_child(&sctx->pool_transfers_unsync, &sscreen->pool_transfers);
464
465 sctx->ws = sscreen->ws;
466 sctx->family = sscreen->info.family;
467 sctx->chip_class = sscreen->info.chip_class;
468
469 if (sctx->chip_class == GFX7 || sctx->chip_class == GFX8 || sctx->chip_class == GFX9) {
470 sctx->eop_bug_scratch = si_aligned_buffer_create(
471 &sscreen->b, SI_RESOURCE_FLAG_DRIVER_INTERNAL,
472 PIPE_USAGE_DEFAULT, 16 * sscreen->info.num_render_backends, 256);
473 if (sctx->screen->info.has_tmz_support)
474 sctx->eop_bug_scratch_tmz = si_aligned_buffer_create(
475 &sscreen->b, PIPE_RESOURCE_FLAG_ENCRYPTED | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
476 PIPE_USAGE_DEFAULT, 16 * sscreen->info.num_render_backends, 256);
477 if (!sctx->eop_bug_scratch)
478 goto fail;
479 }
480
481 /* Initialize context allocators. */
482 sctx->allocator_zeroed_memory =
483 u_suballocator_create(&sctx->b, 128 * 1024, 0, PIPE_USAGE_DEFAULT,
484 SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_CLEAR, false);
485 if (!sctx->allocator_zeroed_memory)
486 goto fail;
487
488 sctx->b.stream_uploader =
489 u_upload_create(&sctx->b, 1024 * 1024, 0, PIPE_USAGE_STREAM, SI_RESOURCE_FLAG_READ_ONLY);
490 if (!sctx->b.stream_uploader)
491 goto fail;
492
493 sctx->cached_gtt_allocator = u_upload_create(&sctx->b, 16 * 1024, 0, PIPE_USAGE_STAGING, 0);
494 if (!sctx->cached_gtt_allocator)
495 goto fail;
496
497 sctx->ctx = sctx->ws->ctx_create(sctx->ws);
498 if (!sctx->ctx)
499 goto fail;
500
501 /* SDMA causes corruption on: :
502 * - RX 580: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1399, 1889
503 * - gfx9 APUs: https://gitlab.freedesktop.org/mesa/mesa/-/issues/2814
504 * - gfx10: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1907,
505 https://gitlab.freedesktop.org/drm/amd/issues/892
506 *
507 * While we could keep buffer copies and clears enabled, let's disable
508 * everything because SDMA decreases CPU performance because of its
509 * command submission overhead.
510 *
511 * And SDMA is disabled on all chips (instead of just the ones listed above),
512 * because it doesn't make sense to keep it enabled on old chips only
513 * that are not tested as often as newer chips.
514 */
515 if (sscreen->info.num_rings[RING_DMA] && !(sscreen->debug_flags & DBG(NO_SDMA)) &&
516 sscreen->debug_flags & DBG(FORCE_SDMA)) {
517 sctx->sdma_cs = sctx->ws->cs_create(sctx->ctx, RING_DMA, (void *)si_flush_dma_cs, sctx,
518 stop_exec_on_failure);
519 }
520
521 bool use_sdma_upload = sscreen->info.has_dedicated_vram && sctx->sdma_cs;
522 sctx->b.const_uploader =
523 u_upload_create(&sctx->b, 256 * 1024, 0, PIPE_USAGE_DEFAULT,
524 SI_RESOURCE_FLAG_32BIT |
525 (use_sdma_upload ? SI_RESOURCE_FLAG_UPLOAD_FLUSH_EXPLICIT_VIA_SDMA : 0));
526 if (!sctx->b.const_uploader)
527 goto fail;
528
529 if (use_sdma_upload)
530 u_upload_enable_flush_explicit(sctx->b.const_uploader);
531
532 sctx->gfx_cs = ws->cs_create(sctx->ctx, sctx->has_graphics ? RING_GFX : RING_COMPUTE,
533 (void *)si_flush_gfx_cs, sctx, stop_exec_on_failure);
534
535 /* Border colors. */
536 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table));
537 if (!sctx->border_color_table)
538 goto fail;
539
540 sctx->border_color_buffer = si_resource(pipe_buffer_create(
541 screen, 0, PIPE_USAGE_DEFAULT, SI_MAX_BORDER_COLORS * sizeof(*sctx->border_color_table)));
542 if (!sctx->border_color_buffer)
543 goto fail;
544
545 sctx->border_color_map =
546 ws->buffer_map(sctx->border_color_buffer->buf, NULL, PIPE_MAP_WRITE);
547 if (!sctx->border_color_map)
548 goto fail;
549
550 sctx->ngg = sscreen->use_ngg;
551
552 /* Initialize context functions used by graphics and compute. */
553 if (sctx->chip_class >= GFX10)
554 sctx->emit_cache_flush = gfx10_emit_cache_flush;
555 else
556 sctx->emit_cache_flush = si_emit_cache_flush;
557
558 sctx->b.emit_string_marker = si_emit_string_marker;
559 sctx->b.set_debug_callback = si_set_debug_callback;
560 sctx->b.set_log_context = si_set_log_context;
561 sctx->b.set_context_param = si_set_context_param;
562 sctx->b.get_device_reset_status = si_get_reset_status;
563 sctx->b.set_device_reset_callback = si_set_device_reset_callback;
564 sctx->b.set_frontend_noop = si_set_frontend_noop;
565
566 si_init_all_descriptors(sctx);
567 si_init_buffer_functions(sctx);
568 si_init_clear_functions(sctx);
569 si_init_blit_functions(sctx);
570 si_init_compute_functions(sctx);
571 si_init_compute_blit_functions(sctx);
572 si_init_debug_functions(sctx);
573 si_init_fence_functions(sctx);
574 si_init_query_functions(sctx);
575 si_init_state_compute_functions(sctx);
576 si_init_context_texture_functions(sctx);
577
578 /* Initialize graphics-only context functions. */
579 if (sctx->has_graphics) {
580 if (sctx->chip_class >= GFX10)
581 gfx10_init_query(sctx);
582 si_init_msaa_functions(sctx);
583 si_init_shader_functions(sctx);
584 si_init_state_functions(sctx);
585 si_init_streamout_functions(sctx);
586 si_init_viewport_functions(sctx);
587
588 sctx->blitter = util_blitter_create(&sctx->b);
589 if (sctx->blitter == NULL)
590 goto fail;
591 sctx->blitter->skip_viewport_restore = true;
592
593 /* Some states are expected to be always non-NULL. */
594 sctx->noop_blend = util_blitter_get_noop_blend_state(sctx->blitter);
595 sctx->queued.named.blend = sctx->noop_blend;
596
597 sctx->noop_dsa = util_blitter_get_noop_dsa_state(sctx->blitter);
598 sctx->queued.named.dsa = sctx->noop_dsa;
599
600 sctx->discard_rasterizer_state = util_blitter_get_discard_rasterizer_state(sctx->blitter);
601 sctx->queued.named.rasterizer = sctx->discard_rasterizer_state;
602
603 si_init_draw_functions(sctx);
604
605 /* If aux_context == NULL, we are initializing aux_context right now. */
606 bool is_aux_context = !sscreen->aux_context;
607 si_initialize_prim_discard_tunables(sscreen, is_aux_context,
608 &sctx->prim_discard_vertex_count_threshold,
609 &sctx->index_ring_size_per_ib);
610 } else {
611 sctx->prim_discard_vertex_count_threshold = UINT_MAX;
612 }
613
614 /* Initialize SDMA functions. */
615 if (sctx->chip_class >= GFX7)
616 cik_init_sdma_functions(sctx);
617 else
618 sctx->dma_copy = si_resource_copy_region;
619
620 if (sscreen->debug_flags & DBG(FORCE_SDMA))
621 sctx->b.resource_copy_region = sctx->dma_copy;
622
623 sctx->sample_mask = 0xffff;
624
625 /* Initialize multimedia functions. */
626 if (sscreen->info.has_hw_decode) {
627 sctx->b.create_video_codec = si_uvd_create_decoder;
628 sctx->b.create_video_buffer = si_video_buffer_create;
629 } else {
630 sctx->b.create_video_codec = vl_create_decoder;
631 sctx->b.create_video_buffer = vl_video_buffer_create;
632 }
633
634 if (sctx->chip_class >= GFX9 || si_compute_prim_discard_enabled(sctx)) {
635 sctx->wait_mem_scratch =
636 si_aligned_buffer_create(screen,
637 SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
638 PIPE_USAGE_DEFAULT, 8,
639 sscreen->info.tcc_cache_line_size);
640 if (!sctx->wait_mem_scratch)
641 goto fail;
642
643 if (sscreen->info.has_tmz_support) {
644 sctx->wait_mem_scratch_tmz =
645 si_aligned_buffer_create(screen,
646 SI_RESOURCE_FLAG_UNMAPPABLE | SI_RESOURCE_FLAG_DRIVER_INTERNAL |
647 PIPE_RESOURCE_FLAG_ENCRYPTED,
648 PIPE_USAGE_DEFAULT, 8,
649 sscreen->info.tcc_cache_line_size);
650 if (!sctx->wait_mem_scratch_tmz)
651 goto fail;
652 }
653 }
654
655 /* GFX7 cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
656 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
657 if (sctx->chip_class == GFX7) {
658 sctx->null_const_buf.buffer =
659 pipe_aligned_buffer_create(screen,
660 SI_RESOURCE_FLAG_32BIT | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
661 PIPE_USAGE_DEFAULT, 16,
662 sctx->screen->info.tcc_cache_line_size);
663 if (!sctx->null_const_buf.buffer)
664 goto fail;
665 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
666
667 unsigned start_shader = sctx->has_graphics ? 0 : PIPE_SHADER_COMPUTE;
668 for (shader = start_shader; shader < SI_NUM_SHADERS; shader++) {
669 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
670 sctx->b.set_constant_buffer(&sctx->b, shader, i, &sctx->null_const_buf);
671 }
672 }
673
674 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &sctx->null_const_buf);
675 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &sctx->null_const_buf);
676 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &sctx->null_const_buf);
677 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &sctx->null_const_buf);
678 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &sctx->null_const_buf);
679 }
680
681 uint64_t max_threads_per_block;
682 screen->get_compute_param(screen, PIPE_SHADER_IR_NIR, PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
683 &max_threads_per_block);
684
685 /* The maximum number of scratch waves. Scratch space isn't divided
686 * evenly between CUs. The number is only a function of the number of CUs.
687 * We can decrease the constant to decrease the scratch buffer size.
688 *
689 * sctx->scratch_waves must be >= the maximum posible size of
690 * 1 threadgroup, so that the hw doesn't hang from being unable
691 * to start any.
692 *
693 * The recommended value is 4 per CU at most. Higher numbers don't
694 * bring much benefit, but they still occupy chip resources (think
695 * async compute). I've seen ~2% performance difference between 4 and 32.
696 */
697 sctx->scratch_waves =
698 MAX2(32 * sscreen->info.num_good_compute_units, max_threads_per_block / 64);
699
700 /* Bindless handles. */
701 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
702 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
703
704 util_dynarray_init(&sctx->resident_tex_handles, NULL);
705 util_dynarray_init(&sctx->resident_img_handles, NULL);
706 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
707 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
708 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
709
710 sctx->sample_pos_buffer =
711 pipe_buffer_create(sctx->b.screen, 0, PIPE_USAGE_DEFAULT, sizeof(sctx->sample_positions));
712 pipe_buffer_write(&sctx->b, sctx->sample_pos_buffer, 0, sizeof(sctx->sample_positions),
713 &sctx->sample_positions);
714
715 /* The remainder of this function initializes the gfx CS and must be last. */
716 assert(sctx->gfx_cs->current.cdw == 0);
717
718 if (sctx->has_graphics) {
719 si_init_cp_reg_shadowing(sctx);
720 }
721
722 si_begin_new_gfx_cs(sctx, true);
723 assert(sctx->gfx_cs->current.cdw == sctx->initial_gfx_cs_size);
724
725 /* Initialize per-context buffers. */
726 if (sctx->wait_mem_scratch)
727 si_cp_write_data(sctx, sctx->wait_mem_scratch, 0, 4, V_370_MEM, V_370_ME,
728 &sctx->wait_mem_number);
729 if (sctx->wait_mem_scratch_tmz)
730 si_cp_write_data(sctx, sctx->wait_mem_scratch_tmz, 0, 4, V_370_MEM, V_370_ME,
731 &sctx->wait_mem_number);
732
733 if (sctx->chip_class == GFX7) {
734 /* Clear the NULL constant buffer, because loads should return zeros.
735 * Note that this forces CP DMA to be used, because clover deadlocks
736 * for some reason when the compute codepath is used.
737 */
738 uint32_t clear_value = 0;
739 si_clear_buffer(sctx, sctx->null_const_buf.buffer, 0, sctx->null_const_buf.buffer->width0,
740 &clear_value, 4, SI_COHERENCY_SHADER, true);
741 }
742
743 sctx->initial_gfx_cs_size = sctx->gfx_cs->current.cdw;
744 return &sctx->b;
745 fail:
746 fprintf(stderr, "radeonsi: Failed to create a context.\n");
747 si_destroy_context(&sctx->b);
748 return NULL;
749 }
750
si_pipe_create_context(struct pipe_screen * screen,void * priv,unsigned flags)751 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen, void *priv,
752 unsigned flags)
753 {
754 struct si_screen *sscreen = (struct si_screen *)screen;
755 struct pipe_context *ctx;
756 uint64_t total_ram;
757
758 if (sscreen->debug_flags & DBG(CHECK_VM))
759 flags |= PIPE_CONTEXT_DEBUG;
760
761 ctx = si_create_context(screen, flags);
762
763 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
764 return ctx;
765
766 /* Clover (compute-only) is unsupported. */
767 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
768 return ctx;
769
770 /* When shaders are logged to stderr, asynchronous compilation is
771 * disabled too. */
772 if (sscreen->debug_flags & DBG_ALL_SHADERS)
773 return ctx;
774
775 /* Use asynchronous flushes only on amdgpu, since the radeon
776 * implementation for fence_server_sync is incomplete. */
777 struct pipe_context * tc = threaded_context_create(
778 ctx, &sscreen->pool_transfers, si_replace_buffer_storage,
779 sscreen->info.is_amdgpu ? si_create_fence : NULL,
780 &((struct si_context *)ctx)->tc);
781
782 if (tc && tc != ctx && os_get_total_physical_memory(&total_ram)) {
783 ((struct threaded_context *) tc)->bytes_mapped_limit = total_ram / 4;
784 }
785
786 return tc;
787 }
788
789 /*
790 * pipe_screen
791 */
si_destroy_screen(struct pipe_screen * pscreen)792 static void si_destroy_screen(struct pipe_screen *pscreen)
793 {
794 struct si_screen *sscreen = (struct si_screen *)pscreen;
795 struct si_shader_part *parts[] = {sscreen->vs_prologs, sscreen->tcs_epilogs, sscreen->gs_prologs,
796 sscreen->ps_prologs, sscreen->ps_epilogs};
797 unsigned i;
798
799 if (!sscreen->ws->unref(sscreen->ws))
800 return;
801
802 if (sscreen->debug_flags & DBG(CACHE_STATS)) {
803 printf("live shader cache: hits = %u, misses = %u\n", sscreen->live_shader_cache.hits,
804 sscreen->live_shader_cache.misses);
805 printf("memory shader cache: hits = %u, misses = %u\n", sscreen->num_memory_shader_cache_hits,
806 sscreen->num_memory_shader_cache_misses);
807 printf("disk shader cache: hits = %u, misses = %u\n", sscreen->num_disk_shader_cache_hits,
808 sscreen->num_disk_shader_cache_misses);
809 }
810
811 simple_mtx_destroy(&sscreen->aux_context_lock);
812
813 struct u_log_context *aux_log = ((struct si_context *)sscreen->aux_context)->log;
814 if (aux_log) {
815 sscreen->aux_context->set_log_context(sscreen->aux_context, NULL);
816 u_log_context_destroy(aux_log);
817 FREE(aux_log);
818 }
819
820 sscreen->aux_context->destroy(sscreen->aux_context);
821
822 util_queue_destroy(&sscreen->shader_compiler_queue);
823 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
824
825 /* Release the reference on glsl types of the compiler threads. */
826 glsl_type_singleton_decref();
827
828 for (i = 0; i < ARRAY_SIZE(sscreen->compiler); i++)
829 si_destroy_compiler(&sscreen->compiler[i]);
830
831 for (i = 0; i < ARRAY_SIZE(sscreen->compiler_lowp); i++)
832 si_destroy_compiler(&sscreen->compiler_lowp[i]);
833
834 /* Free shader parts. */
835 for (i = 0; i < ARRAY_SIZE(parts); i++) {
836 while (parts[i]) {
837 struct si_shader_part *part = parts[i];
838
839 parts[i] = part->next;
840 si_shader_binary_clean(&part->binary);
841 FREE(part);
842 }
843 }
844 simple_mtx_destroy(&sscreen->shader_parts_mutex);
845 si_destroy_shader_cache(sscreen);
846
847 si_destroy_perfcounters(sscreen);
848 si_gpu_load_kill_thread(sscreen);
849
850 simple_mtx_destroy(&sscreen->gpu_load_mutex);
851
852 slab_destroy_parent(&sscreen->pool_transfers);
853
854 disk_cache_destroy(sscreen->disk_shader_cache);
855 util_live_shader_cache_deinit(&sscreen->live_shader_cache);
856 sscreen->ws->destroy(sscreen->ws);
857 FREE(sscreen);
858 }
859
si_init_gs_info(struct si_screen * sscreen)860 static void si_init_gs_info(struct si_screen *sscreen)
861 {
862 sscreen->gs_table_depth = ac_get_gs_table_depth(sscreen->info.chip_class, sscreen->info.family);
863 }
864
si_test_vmfault(struct si_screen * sscreen,uint64_t test_flags)865 static void si_test_vmfault(struct si_screen *sscreen, uint64_t test_flags)
866 {
867 struct pipe_context *ctx = sscreen->aux_context;
868 struct si_context *sctx = (struct si_context *)ctx;
869 struct pipe_resource *buf = pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64);
870
871 if (!buf) {
872 puts("Buffer allocation failed.");
873 exit(1);
874 }
875
876 si_resource(buf)->gpu_address = 0; /* cause a VM fault */
877
878 if (test_flags & DBG(TEST_VMFAULT_CP)) {
879 si_cp_dma_copy_buffer(sctx, buf, buf, 0, 4, 4, 0, SI_COHERENCY_NONE, L2_BYPASS);
880 ctx->flush(ctx, NULL, 0);
881 puts("VM fault test: CP - done.");
882 }
883 if (test_flags & DBG(TEST_VMFAULT_SDMA)) {
884 si_sdma_clear_buffer(sctx, buf, 0, 4, 0);
885 ctx->flush(ctx, NULL, 0);
886 puts("VM fault test: SDMA - done.");
887 }
888 if (test_flags & DBG(TEST_VMFAULT_SHADER)) {
889 util_test_constant_buffer(ctx, buf);
890 puts("VM fault test: Shader - done.");
891 }
892 exit(0);
893 }
894
si_test_gds_memory_management(struct si_context * sctx,unsigned alloc_size,unsigned alignment,enum radeon_bo_domain domain)895 static void si_test_gds_memory_management(struct si_context *sctx, unsigned alloc_size,
896 unsigned alignment, enum radeon_bo_domain domain)
897 {
898 struct radeon_winsys *ws = sctx->ws;
899 struct radeon_cmdbuf *cs[8];
900 struct pb_buffer *gds_bo[ARRAY_SIZE(cs)];
901
902 for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) {
903 cs[i] = ws->cs_create(sctx->ctx, RING_COMPUTE, NULL, NULL, false);
904 gds_bo[i] = ws->buffer_create(ws, alloc_size, alignment, domain, 0);
905 assert(gds_bo[i]);
906 }
907
908 for (unsigned iterations = 0; iterations < 20000; iterations++) {
909 for (unsigned i = 0; i < ARRAY_SIZE(cs); i++) {
910 /* This clears GDS with CP DMA.
911 *
912 * We don't care if GDS is present. Just add some packet
913 * to make the GPU busy for a moment.
914 */
915 si_cp_dma_clear_buffer(
916 sctx, cs[i], NULL, 0, alloc_size, 0,
917 SI_CPDMA_SKIP_BO_LIST_UPDATE | SI_CPDMA_SKIP_CHECK_CS_SPACE | SI_CPDMA_SKIP_GFX_SYNC, 0,
918 0);
919
920 ws->cs_add_buffer(cs[i], gds_bo[i], RADEON_USAGE_READWRITE, domain, 0);
921 ws->cs_flush(cs[i], PIPE_FLUSH_ASYNC, NULL);
922 }
923 }
924 exit(0);
925 }
926
si_disk_cache_create(struct si_screen * sscreen)927 static void si_disk_cache_create(struct si_screen *sscreen)
928 {
929 /* Don't use the cache if shader dumping is enabled. */
930 if (sscreen->debug_flags & DBG_ALL_SHADERS)
931 return;
932
933 struct mesa_sha1 ctx;
934 unsigned char sha1[20];
935 char cache_id[20 * 2 + 1];
936
937 _mesa_sha1_init(&ctx);
938
939 if (!disk_cache_get_function_identifier(si_disk_cache_create, &ctx) ||
940 !disk_cache_get_function_identifier(LLVMInitializeAMDGPUTargetInfo, &ctx))
941 return;
942
943 _mesa_sha1_final(&ctx, sha1);
944 disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
945
946 sscreen->disk_shader_cache = disk_cache_create(sscreen->info.name, cache_id,
947 sscreen->info.address32_hi);
948 }
949
si_set_max_shader_compiler_threads(struct pipe_screen * screen,unsigned max_threads)950 static void si_set_max_shader_compiler_threads(struct pipe_screen *screen, unsigned max_threads)
951 {
952 struct si_screen *sscreen = (struct si_screen *)screen;
953
954 /* This function doesn't allow a greater number of threads than
955 * the queue had at its creation. */
956 util_queue_adjust_num_threads(&sscreen->shader_compiler_queue, max_threads);
957 /* Don't change the number of threads on the low priority queue. */
958 }
959
si_is_parallel_shader_compilation_finished(struct pipe_screen * screen,void * shader,enum pipe_shader_type shader_type)960 static bool si_is_parallel_shader_compilation_finished(struct pipe_screen *screen, void *shader,
961 enum pipe_shader_type shader_type)
962 {
963 struct si_shader_selector *sel = (struct si_shader_selector *)shader;
964
965 return util_queue_fence_is_signalled(&sel->ready);
966 }
967
radeonsi_screen_create_impl(struct radeon_winsys * ws,const struct pipe_screen_config * config)968 static struct pipe_screen *radeonsi_screen_create_impl(struct radeon_winsys *ws,
969 const struct pipe_screen_config *config)
970 {
971 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
972 unsigned hw_threads, num_comp_hi_threads, num_comp_lo_threads;
973 uint64_t test_flags;
974
975 if (!sscreen) {
976 return NULL;
977 }
978
979 sscreen->ws = ws;
980 ws->query_info(ws, &sscreen->info);
981
982 /* Older LLVM have buggy v_pk_* instructions. */
983 sscreen->info.has_packed_math_16bit &= LLVM_VERSION_MAJOR >= 11;
984
985 if (sscreen->info.chip_class == GFX10_3 && LLVM_VERSION_MAJOR < 11) {
986 fprintf(stderr, "radeonsi: GFX 10.3 requires LLVM 11 or higher\n");
987 FREE(sscreen);
988 return NULL;
989 }
990
991 if (sscreen->info.chip_class == GFX10 && LLVM_VERSION_MAJOR < 9) {
992 fprintf(stderr, "radeonsi: Navi family support requires LLVM 9 or higher\n");
993 FREE(sscreen);
994 return NULL;
995 }
996
997 if (sscreen->info.chip_class >= GFX9) {
998 sscreen->se_tile_repeat = 32 * sscreen->info.max_se;
999 } else {
1000 ac_get_raster_config(&sscreen->info, &sscreen->pa_sc_raster_config,
1001 &sscreen->pa_sc_raster_config_1, &sscreen->se_tile_repeat);
1002 }
1003
1004 sscreen->debug_flags = debug_get_flags_option("R600_DEBUG", debug_options, 0);
1005 sscreen->debug_flags |= debug_get_flags_option("AMD_DEBUG", debug_options, 0);
1006 test_flags = debug_get_flags_option("AMD_TEST", test_options, 0);
1007
1008 if (sscreen->debug_flags & DBG(NO_GFX))
1009 sscreen->info.has_graphics = false;
1010
1011 if ((sscreen->debug_flags & DBG(TMZ)) &&
1012 !sscreen->info.has_tmz_support) {
1013 fprintf(stderr, "radeonsi: requesting TMZ features but TMZ is not supported\n");
1014 FREE(sscreen);
1015 return NULL;
1016 }
1017
1018
1019 /* Set functions first. */
1020 sscreen->b.context_create = si_pipe_create_context;
1021 sscreen->b.destroy = si_destroy_screen;
1022 sscreen->b.set_max_shader_compiler_threads = si_set_max_shader_compiler_threads;
1023 sscreen->b.is_parallel_shader_compilation_finished = si_is_parallel_shader_compilation_finished;
1024 sscreen->b.finalize_nir = si_finalize_nir;
1025
1026 si_init_screen_get_functions(sscreen);
1027 si_init_screen_buffer_functions(sscreen);
1028 si_init_screen_fence_functions(sscreen);
1029 si_init_screen_state_functions(sscreen);
1030 si_init_screen_texture_functions(sscreen);
1031 si_init_screen_query_functions(sscreen);
1032 si_init_screen_live_shader_cache(sscreen);
1033
1034 /* Set these flags in debug_flags early, so that the shader cache takes
1035 * them into account.
1036 */
1037 if (driQueryOptionb(config->options, "glsl_correct_derivatives_after_discard"))
1038 sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
1039
1040 if (sscreen->debug_flags & DBG(INFO))
1041 ac_print_gpu_info(&sscreen->info, stdout);
1042
1043 slab_create_parent(&sscreen->pool_transfers, sizeof(struct si_transfer), 64);
1044
1045 sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
1046 if (sscreen->force_aniso == -1) {
1047 sscreen->force_aniso = MIN2(16, debug_get_num_option("AMD_TEX_ANISO", -1));
1048 }
1049
1050 if (sscreen->force_aniso >= 0) {
1051 printf("radeonsi: Forcing anisotropy filter to %ix\n",
1052 /* round down to a power of two */
1053 1 << util_logbase2(sscreen->force_aniso));
1054 }
1055
1056 (void)simple_mtx_init(&sscreen->aux_context_lock, mtx_plain);
1057 (void)simple_mtx_init(&sscreen->gpu_load_mutex, mtx_plain);
1058
1059 si_init_gs_info(sscreen);
1060 if (!si_init_shader_cache(sscreen)) {
1061 FREE(sscreen);
1062 return NULL;
1063 }
1064
1065 {
1066 #define OPT_BOOL(name, dflt, description) \
1067 sscreen->options.name = driQueryOptionb(config->options, "radeonsi_" #name);
1068 #include "si_debug_options.h"
1069 }
1070
1071 si_disk_cache_create(sscreen);
1072
1073 /* Determine the number of shader compiler threads. */
1074 hw_threads = sysconf(_SC_NPROCESSORS_ONLN);
1075
1076 if (hw_threads >= 12) {
1077 num_comp_hi_threads = hw_threads * 3 / 4;
1078 num_comp_lo_threads = hw_threads / 3;
1079 } else if (hw_threads >= 6) {
1080 num_comp_hi_threads = hw_threads - 2;
1081 num_comp_lo_threads = hw_threads / 2;
1082 } else if (hw_threads >= 2) {
1083 num_comp_hi_threads = hw_threads - 1;
1084 num_comp_lo_threads = hw_threads / 2;
1085 } else {
1086 num_comp_hi_threads = 1;
1087 num_comp_lo_threads = 1;
1088 }
1089
1090 num_comp_hi_threads = MIN2(num_comp_hi_threads, ARRAY_SIZE(sscreen->compiler));
1091 num_comp_lo_threads = MIN2(num_comp_lo_threads, ARRAY_SIZE(sscreen->compiler_lowp));
1092
1093 /* Take a reference on the glsl types for the compiler threads. */
1094 glsl_type_singleton_init_or_ref();
1095
1096 if (!util_queue_init(
1097 &sscreen->shader_compiler_queue, "sh", 64, num_comp_hi_threads,
1098 UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY)) {
1099 si_destroy_shader_cache(sscreen);
1100 FREE(sscreen);
1101 glsl_type_singleton_decref();
1102 return NULL;
1103 }
1104
1105 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority, "shlo", 64,
1106 num_comp_lo_threads,
1107 UTIL_QUEUE_INIT_RESIZE_IF_FULL | UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY |
1108 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
1109 si_destroy_shader_cache(sscreen);
1110 FREE(sscreen);
1111 glsl_type_singleton_decref();
1112 return NULL;
1113 }
1114
1115 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
1116 si_init_perfcounters(sscreen);
1117
1118 unsigned prim_discard_vertex_count_threshold, tmp;
1119 si_initialize_prim_discard_tunables(sscreen, false, &prim_discard_vertex_count_threshold, &tmp);
1120 /* Compute-shader-based culling doesn't support VBOs in user SGPRs. */
1121 if (prim_discard_vertex_count_threshold == UINT_MAX)
1122 sscreen->num_vbos_in_user_sgprs = sscreen->info.chip_class >= GFX9 ? 5 : 1;
1123
1124 /* Determine tessellation ring info. */
1125 bool double_offchip_buffers = sscreen->info.chip_class >= GFX7 &&
1126 sscreen->info.family != CHIP_CARRIZO &&
1127 sscreen->info.family != CHIP_STONEY;
1128 /* This must be one less than the maximum number due to a hw limitation.
1129 * Various hardware bugs need this.
1130 */
1131 unsigned max_offchip_buffers_per_se;
1132
1133 if (sscreen->info.chip_class >= GFX10)
1134 max_offchip_buffers_per_se = 128;
1135 /* Only certain chips can use the maximum value. */
1136 else if (sscreen->info.family == CHIP_VEGA12 || sscreen->info.family == CHIP_VEGA20)
1137 max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
1138 else
1139 max_offchip_buffers_per_se = double_offchip_buffers ? 127 : 63;
1140
1141 unsigned max_offchip_buffers = max_offchip_buffers_per_se * sscreen->info.max_se;
1142 unsigned offchip_granularity;
1143
1144 /* Hawaii has a bug with offchip buffers > 256 that can be worked
1145 * around by setting 4K granularity.
1146 */
1147 if (sscreen->info.family == CHIP_HAWAII) {
1148 sscreen->tess_offchip_block_dw_size = 4096;
1149 offchip_granularity = V_03093C_X_4K_DWORDS;
1150 } else {
1151 sscreen->tess_offchip_block_dw_size = 8192;
1152 offchip_granularity = V_03093C_X_8K_DWORDS;
1153 }
1154
1155 sscreen->tess_factor_ring_size = 32768 * sscreen->info.max_se;
1156 sscreen->tess_offchip_ring_size = max_offchip_buffers * sscreen->tess_offchip_block_dw_size * 4;
1157
1158 if (sscreen->info.chip_class >= GFX10_3) {
1159 sscreen->vgt_hs_offchip_param =
1160 S_03093C_OFFCHIP_BUFFERING_GFX103(max_offchip_buffers - 1) |
1161 S_03093C_OFFCHIP_GRANULARITY_GFX103(offchip_granularity);
1162 } else if (sscreen->info.chip_class >= GFX7) {
1163 if (sscreen->info.chip_class >= GFX8)
1164 --max_offchip_buffers;
1165 sscreen->vgt_hs_offchip_param = S_03093C_OFFCHIP_BUFFERING_GFX7(max_offchip_buffers) |
1166 S_03093C_OFFCHIP_GRANULARITY_GFX7(offchip_granularity);
1167 } else {
1168 assert(offchip_granularity == V_03093C_X_8K_DWORDS);
1169 sscreen->vgt_hs_offchip_param = S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
1170 }
1171
1172 sscreen->has_draw_indirect_multi =
1173 (sscreen->info.family >= CHIP_POLARIS10) ||
1174 (sscreen->info.chip_class == GFX8 && sscreen->info.pfp_fw_version >= 121 &&
1175 sscreen->info.me_fw_version >= 87) ||
1176 (sscreen->info.chip_class == GFX7 && sscreen->info.pfp_fw_version >= 211 &&
1177 sscreen->info.me_fw_version >= 173) ||
1178 (sscreen->info.chip_class == GFX6 && sscreen->info.pfp_fw_version >= 79 &&
1179 sscreen->info.me_fw_version >= 142);
1180
1181 sscreen->has_out_of_order_rast =
1182 sscreen->info.has_out_of_order_rast && !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER));
1183 sscreen->assume_no_z_fights = driQueryOptionb(config->options, "radeonsi_assume_no_z_fights") ||
1184 driQueryOptionb(config->options, "allow_draw_out_of_order");
1185 sscreen->commutative_blend_add =
1186 driQueryOptionb(config->options, "radeonsi_commutative_blend_add") ||
1187 driQueryOptionb(config->options, "allow_draw_out_of_order");
1188
1189 sscreen->use_ngg = !(sscreen->debug_flags & DBG(NO_NGG)) &&
1190 sscreen->info.chip_class >= GFX10 &&
1191 (sscreen->info.family != CHIP_NAVI14 ||
1192 sscreen->info.is_pro_graphics) &&
1193 sscreen->info.has_dedicated_vram;
1194 sscreen->use_ngg_culling = sscreen->use_ngg && !(sscreen->debug_flags & DBG(NO_NGG_CULLING));
1195 sscreen->use_ngg_streamout = false;
1196
1197 /* Only enable primitive binning on APUs by default. */
1198 if (sscreen->info.chip_class >= GFX10) {
1199 sscreen->dpbb_allowed = true;
1200 /* DFSM is not supported on GFX 10.3 and not beneficial on Navi1x. */
1201 } else if (sscreen->info.chip_class == GFX9) {
1202 sscreen->dpbb_allowed = !sscreen->info.has_dedicated_vram;
1203 sscreen->dfsm_allowed = !sscreen->info.has_dedicated_vram;
1204 }
1205
1206 /* Process DPBB enable flags. */
1207 if (sscreen->debug_flags & DBG(DPBB)) {
1208 sscreen->dpbb_allowed = true;
1209 if (sscreen->debug_flags & DBG(DFSM))
1210 sscreen->dfsm_allowed = true;
1211 }
1212
1213 /* Process DPBB disable flags. */
1214 if (sscreen->debug_flags & DBG(NO_DPBB)) {
1215 sscreen->dpbb_allowed = false;
1216 sscreen->dfsm_allowed = false;
1217 } else if (sscreen->debug_flags & DBG(NO_DFSM)) {
1218 sscreen->dfsm_allowed = false;
1219 }
1220
1221 if (sscreen->dpbb_allowed) {
1222 if (sscreen->info.has_dedicated_vram) {
1223 if (sscreen->info.num_render_backends > 4) {
1224 sscreen->pbb_context_states_per_bin = 1;
1225 sscreen->pbb_persistent_states_per_bin = 1;
1226 } else {
1227 sscreen->pbb_context_states_per_bin = 3;
1228 sscreen->pbb_persistent_states_per_bin = 8;
1229 }
1230 } else {
1231 /* This is a workaround for:
1232 * https://bugs.freedesktop.org/show_bug.cgi?id=110214
1233 * (an alternative is to insert manual BATCH_BREAK event when
1234 * a context_roll is detected). */
1235 sscreen->pbb_context_states_per_bin = sscreen->info.has_gfx9_scissor_bug ? 1 : 6;
1236 /* Using 32 here can cause GPU hangs on RAVEN1 */
1237 sscreen->pbb_persistent_states_per_bin = 16;
1238 }
1239
1240 assert(sscreen->pbb_context_states_per_bin >= 1 &&
1241 sscreen->pbb_context_states_per_bin <= 6);
1242 assert(sscreen->pbb_persistent_states_per_bin >= 1 &&
1243 sscreen->pbb_persistent_states_per_bin <= 32);
1244 }
1245
1246 /* While it would be nice not to have this flag, we are constrained
1247 * by the reality that LLVM 9.0 has buggy VGPR indexing on GFX9.
1248 */
1249 sscreen->llvm_has_working_vgpr_indexing = sscreen->info.chip_class != GFX9;
1250
1251 sscreen->dcc_msaa_allowed = !(sscreen->debug_flags & DBG(NO_DCC_MSAA));
1252
1253 (void)simple_mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
1254 sscreen->use_monolithic_shaders = (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
1255
1256 sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SCACHE | SI_CONTEXT_INV_VCACHE;
1257 if (sscreen->info.chip_class <= GFX8) {
1258 sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_L2;
1259 sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WB_L2;
1260 }
1261
1262 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
1263 sscreen->debug_flags |= DBG_ALL_SHADERS;
1264
1265 /* Syntax:
1266 * EQAA=s,z,c
1267 * Example:
1268 * EQAA=8,4,2
1269
1270 * That means 8 coverage samples, 4 Z/S samples, and 2 color samples.
1271 * Constraints:
1272 * s >= z >= c (ignoring this only wastes memory)
1273 * s = [2..16]
1274 * z = [2..8]
1275 * c = [2..8]
1276 *
1277 * Only MSAA color and depth buffers are overriden.
1278 */
1279 if (sscreen->info.has_eqaa_surface_allocator) {
1280 const char *eqaa = debug_get_option("EQAA", NULL);
1281 unsigned s, z, f;
1282
1283 if (eqaa && sscanf(eqaa, "%u,%u,%u", &s, &z, &f) == 3 && s && z && f) {
1284 sscreen->eqaa_force_coverage_samples = s;
1285 sscreen->eqaa_force_z_samples = z;
1286 sscreen->eqaa_force_color_samples = f;
1287 }
1288 }
1289
1290 sscreen->ge_wave_size = 64;
1291 sscreen->ps_wave_size = 64;
1292 sscreen->compute_wave_size = 64;
1293
1294 if (sscreen->info.chip_class >= GFX10) {
1295 /* Pixel shaders: Wave64 is always fastest.
1296 * Vertex shaders: Wave64 is probably better, because:
1297 * - greater chance of L0 cache hits, because more threads are assigned
1298 * to the same CU
1299 * - scalar instructions are only executed once for 64 threads instead of twice
1300 * - VGPR allocation granularity is half of Wave32, so 1 Wave64 can
1301 * sometimes use fewer VGPRs than 2 Wave32
1302 * - TessMark X64 with NGG culling is faster with Wave64
1303 */
1304 if (sscreen->debug_flags & DBG(W32_GE))
1305 sscreen->ge_wave_size = 32;
1306 if (sscreen->debug_flags & DBG(W32_PS))
1307 sscreen->ps_wave_size = 32;
1308 if (sscreen->debug_flags & DBG(W32_CS))
1309 sscreen->compute_wave_size = 32;
1310
1311 if (sscreen->debug_flags & DBG(W64_GE))
1312 sscreen->ge_wave_size = 64;
1313 if (sscreen->debug_flags & DBG(W64_PS))
1314 sscreen->ps_wave_size = 64;
1315 if (sscreen->debug_flags & DBG(W64_CS))
1316 sscreen->compute_wave_size = 64;
1317 }
1318
1319 /* Create the auxiliary context. This must be done last. */
1320 sscreen->aux_context = si_create_context(
1321 &sscreen->b, (sscreen->options.aux_debug ? PIPE_CONTEXT_DEBUG : 0) |
1322 (sscreen->info.has_graphics ? 0 : PIPE_CONTEXT_COMPUTE_ONLY));
1323 if (sscreen->options.aux_debug) {
1324 struct u_log_context *log = CALLOC_STRUCT(u_log_context);
1325 u_log_context_init(log);
1326 sscreen->aux_context->set_log_context(sscreen->aux_context, log);
1327 }
1328
1329 if (test_flags & DBG(TEST_DMA))
1330 si_test_dma(sscreen);
1331
1332 if (test_flags & DBG(TEST_DMA_PERF)) {
1333 si_test_dma_perf(sscreen);
1334 }
1335
1336 if (test_flags & (DBG(TEST_VMFAULT_CP) | DBG(TEST_VMFAULT_SDMA) | DBG(TEST_VMFAULT_SHADER)))
1337 si_test_vmfault(sscreen, test_flags);
1338
1339 if (test_flags & DBG(TEST_GDS))
1340 si_test_gds((struct si_context *)sscreen->aux_context);
1341
1342 if (test_flags & DBG(TEST_GDS_MM)) {
1343 si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 32 * 1024, 4,
1344 RADEON_DOMAIN_GDS);
1345 }
1346 if (test_flags & DBG(TEST_GDS_OA_MM)) {
1347 si_test_gds_memory_management((struct si_context *)sscreen->aux_context, 4, 1,
1348 RADEON_DOMAIN_OA);
1349 }
1350
1351 ac_print_shadowed_regs(&sscreen->info);
1352
1353 STATIC_ASSERT(sizeof(union si_vgt_stages_key) == 4);
1354 return &sscreen->b;
1355 }
1356
radeonsi_screen_create(int fd,const struct pipe_screen_config * config)1357 struct pipe_screen *radeonsi_screen_create(int fd, const struct pipe_screen_config *config)
1358 {
1359 drmVersionPtr version = drmGetVersion(fd);
1360 struct radeon_winsys *rw = NULL;
1361
1362 switch (version->version_major) {
1363 case 2:
1364 rw = radeon_drm_winsys_create(fd, config, radeonsi_screen_create_impl);
1365 break;
1366 case 3:
1367 rw = amdgpu_winsys_create(fd, config, radeonsi_screen_create_impl);
1368 break;
1369 }
1370
1371 drmFreeVersion(version);
1372 return rw ? rw->screen : NULL;
1373 }
1374