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