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