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