2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
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:
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
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.
27 #include "si_public.h"
28 #include "si_shader_internal.h"
31 #include "radeon/radeon_uvd.h"
32 #include "util/hash_table.h"
33 #include "util/u_log.h"
34 #include "util/u_memory.h"
35 #include "util/u_suballoc.h"
36 #include "util/u_tests.h"
37 #include "util/u_upload_mgr.h"
38 #include "util/xmlconfig.h"
39 #include "vl/vl_decoder.h"
40 #include "driver_ddebug/dd_util.h"
42 static const struct debug_named_value debug_options
[] = {
43 /* Shader logging options: */
44 { "vs", DBG(VS
), "Print vertex shaders" },
45 { "ps", DBG(PS
), "Print pixel shaders" },
46 { "gs", DBG(GS
), "Print geometry shaders" },
47 { "tcs", DBG(TCS
), "Print tessellation control shaders" },
48 { "tes", DBG(TES
), "Print tessellation evaluation shaders" },
49 { "cs", DBG(CS
), "Print compute shaders" },
50 { "noir", DBG(NO_IR
), "Don't print the LLVM IR"},
51 { "notgsi", DBG(NO_TGSI
), "Don't print the TGSI"},
52 { "noasm", DBG(NO_ASM
), "Don't print disassembled shaders"},
53 { "preoptir", DBG(PREOPT_IR
), "Print the LLVM IR before initial optimizations" },
55 /* Shader compiler options the shader cache should be aware of: */
56 { "unsafemath", DBG(UNSAFE_MATH
), "Enable unsafe math shader optimizations" },
57 { "sisched", DBG(SI_SCHED
), "Enable LLVM SI Machine Instruction Scheduler." },
59 /* Shader compiler options (with no effect on the shader cache): */
60 { "checkir", DBG(CHECK_IR
), "Enable additional sanity checks on shader IR" },
61 { "nir", DBG(NIR
), "Enable experimental NIR shaders" },
62 { "mono", DBG(MONOLITHIC_SHADERS
), "Use old-style monolithic shaders compiled on demand" },
63 { "nooptvariant", DBG(NO_OPT_VARIANT
), "Disable compiling optimized shader variants." },
65 /* Information logging options: */
66 { "info", DBG(INFO
), "Print driver information" },
67 { "tex", DBG(TEX
), "Print texture info" },
68 { "compute", DBG(COMPUTE
), "Print compute info" },
69 { "vm", DBG(VM
), "Print virtual addresses when creating resources" },
72 { "forcedma", DBG(FORCE_DMA
), "Use asynchronous DMA for all operations when possible." },
73 { "nodma", DBG(NO_ASYNC_DMA
), "Disable asynchronous DMA" },
74 { "nowc", DBG(NO_WC
), "Disable GTT write combining" },
75 { "check_vm", DBG(CHECK_VM
), "Check VM faults and dump debug info." },
76 { "reserve_vmid", DBG(RESERVE_VMID
), "Force VMID reservation per context." },
78 /* 3D engine options: */
79 { "switch_on_eop", DBG(SWITCH_ON_EOP
), "Program WD/IA to switch on end-of-packet." },
80 { "nooutoforder", DBG(NO_OUT_OF_ORDER
), "Disable out-of-order rasterization" },
81 { "nodpbb", DBG(NO_DPBB
), "Disable DPBB." },
82 { "nodfsm", DBG(NO_DFSM
), "Disable DFSM." },
83 { "dpbb", DBG(DPBB
), "Enable DPBB." },
84 { "dfsm", DBG(DFSM
), "Enable DFSM." },
85 { "nohyperz", DBG(NO_HYPERZ
), "Disable Hyper-Z" },
86 { "norbplus", DBG(NO_RB_PLUS
), "Disable RB+." },
87 { "no2d", DBG(NO_2D_TILING
), "Disable 2D tiling" },
88 { "notiling", DBG(NO_TILING
), "Disable tiling" },
89 { "nodcc", DBG(NO_DCC
), "Disable DCC." },
90 { "nodccclear", DBG(NO_DCC_CLEAR
), "Disable DCC fast clear." },
91 { "nodccfb", DBG(NO_DCC_FB
), "Disable separate DCC on the main framebuffer" },
92 { "nodccmsaa", DBG(NO_DCC_MSAA
), "Disable DCC for MSAA" },
93 { "nofmask", DBG(NO_FMASK
), "Disable MSAA compression" },
96 { "testdma", DBG(TEST_DMA
), "Invoke SDMA tests and exit." },
97 { "testvmfaultcp", DBG(TEST_VMFAULT_CP
), "Invoke a CP VM fault test and exit." },
98 { "testvmfaultsdma", DBG(TEST_VMFAULT_SDMA
), "Invoke a SDMA VM fault test and exit." },
99 { "testvmfaultshader", DBG(TEST_VMFAULT_SHADER
), "Invoke a shader VM fault test and exit." },
101 DEBUG_NAMED_VALUE_END
/* must be last */
107 static void si_destroy_context(struct pipe_context
*context
)
109 struct si_context
*sctx
= (struct si_context
*)context
;
112 /* Unreference the framebuffer normally to disable related logic
115 struct pipe_framebuffer_state fb
= {};
116 if (context
->set_framebuffer_state
)
117 context
->set_framebuffer_state(context
, &fb
);
119 si_release_all_descriptors(sctx
);
121 pipe_resource_reference(&sctx
->esgs_ring
, NULL
);
122 pipe_resource_reference(&sctx
->gsvs_ring
, NULL
);
123 pipe_resource_reference(&sctx
->tess_rings
, NULL
);
124 pipe_resource_reference(&sctx
->null_const_buf
.buffer
, NULL
);
125 r600_resource_reference(&sctx
->border_color_buffer
, NULL
);
126 free(sctx
->border_color_table
);
127 r600_resource_reference(&sctx
->scratch_buffer
, NULL
);
128 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
129 r600_resource_reference(&sctx
->wait_mem_scratch
, NULL
);
131 si_pm4_free_state(sctx
, sctx
->init_config
, ~0);
132 if (sctx
->init_config_gs_rings
)
133 si_pm4_free_state(sctx
, sctx
->init_config_gs_rings
, ~0);
134 for (i
= 0; i
< ARRAY_SIZE(sctx
->vgt_shader_config
); i
++)
135 si_pm4_delete_state(sctx
, vgt_shader_config
, sctx
->vgt_shader_config
[i
]);
137 if (sctx
->fixed_func_tcs_shader
.cso
)
138 sctx
->b
.delete_tcs_state(&sctx
->b
, sctx
->fixed_func_tcs_shader
.cso
);
139 if (sctx
->custom_dsa_flush
)
140 sctx
->b
.delete_depth_stencil_alpha_state(&sctx
->b
, sctx
->custom_dsa_flush
);
141 if (sctx
->custom_blend_resolve
)
142 sctx
->b
.delete_blend_state(&sctx
->b
, sctx
->custom_blend_resolve
);
143 if (sctx
->custom_blend_fmask_decompress
)
144 sctx
->b
.delete_blend_state(&sctx
->b
, sctx
->custom_blend_fmask_decompress
);
145 if (sctx
->custom_blend_eliminate_fastclear
)
146 sctx
->b
.delete_blend_state(&sctx
->b
, sctx
->custom_blend_eliminate_fastclear
);
147 if (sctx
->custom_blend_dcc_decompress
)
148 sctx
->b
.delete_blend_state(&sctx
->b
, sctx
->custom_blend_dcc_decompress
);
149 if (sctx
->vs_blit_pos
)
150 sctx
->b
.delete_vs_state(&sctx
->b
, sctx
->vs_blit_pos
);
151 if (sctx
->vs_blit_pos_layered
)
152 sctx
->b
.delete_vs_state(&sctx
->b
, sctx
->vs_blit_pos_layered
);
153 if (sctx
->vs_blit_color
)
154 sctx
->b
.delete_vs_state(&sctx
->b
, sctx
->vs_blit_color
);
155 if (sctx
->vs_blit_color_layered
)
156 sctx
->b
.delete_vs_state(&sctx
->b
, sctx
->vs_blit_color_layered
);
157 if (sctx
->vs_blit_texcoord
)
158 sctx
->b
.delete_vs_state(&sctx
->b
, sctx
->vs_blit_texcoord
);
161 util_blitter_destroy(sctx
->blitter
);
163 /* Release DCC stats. */
164 for (int i
= 0; i
< ARRAY_SIZE(sctx
->dcc_stats
); i
++) {
165 assert(!sctx
->dcc_stats
[i
].query_active
);
167 for (int j
= 0; j
< ARRAY_SIZE(sctx
->dcc_stats
[i
].ps_stats
); j
++)
168 if (sctx
->dcc_stats
[i
].ps_stats
[j
])
169 sctx
->b
.destroy_query(&sctx
->b
,
170 sctx
->dcc_stats
[i
].ps_stats
[j
]);
172 r600_texture_reference(&sctx
->dcc_stats
[i
].tex
, NULL
);
175 if (sctx
->query_result_shader
)
176 sctx
->b
.delete_compute_state(&sctx
->b
, sctx
->query_result_shader
);
179 sctx
->ws
->cs_destroy(sctx
->gfx_cs
);
181 sctx
->ws
->cs_destroy(sctx
->dma_cs
);
183 sctx
->ws
->ctx_destroy(sctx
->ctx
);
185 if (sctx
->b
.stream_uploader
)
186 u_upload_destroy(sctx
->b
.stream_uploader
);
187 if (sctx
->b
.const_uploader
)
188 u_upload_destroy(sctx
->b
.const_uploader
);
189 if (sctx
->cached_gtt_allocator
)
190 u_upload_destroy(sctx
->cached_gtt_allocator
);
192 slab_destroy_child(&sctx
->pool_transfers
);
193 slab_destroy_child(&sctx
->pool_transfers_unsync
);
195 if (sctx
->allocator_zeroed_memory
)
196 u_suballocator_destroy(sctx
->allocator_zeroed_memory
);
198 sctx
->ws
->fence_reference(&sctx
->last_gfx_fence
, NULL
);
199 sctx
->ws
->fence_reference(&sctx
->last_sdma_fence
, NULL
);
200 r600_resource_reference(&sctx
->eop_bug_scratch
, NULL
);
202 LLVMDisposeTargetMachine(sctx
->tm
);
204 si_saved_cs_reference(&sctx
->current_saved_cs
, NULL
);
206 _mesa_hash_table_destroy(sctx
->tex_handles
, NULL
);
207 _mesa_hash_table_destroy(sctx
->img_handles
, NULL
);
209 util_dynarray_fini(&sctx
->resident_tex_handles
);
210 util_dynarray_fini(&sctx
->resident_img_handles
);
211 util_dynarray_fini(&sctx
->resident_tex_needs_color_decompress
);
212 util_dynarray_fini(&sctx
->resident_img_needs_color_decompress
);
213 util_dynarray_fini(&sctx
->resident_tex_needs_depth_decompress
);
217 static enum pipe_reset_status
218 si_amdgpu_get_reset_status(struct pipe_context
*ctx
)
220 struct si_context
*sctx
= (struct si_context
*)ctx
;
222 return sctx
->ws
->ctx_query_reset_status(sctx
->ctx
);
225 static enum pipe_reset_status
si_get_reset_status(struct pipe_context
*ctx
)
227 struct si_context
*sctx
= (struct si_context
*)ctx
;
228 unsigned latest
= sctx
->ws
->query_value(sctx
->ws
,
229 RADEON_GPU_RESET_COUNTER
);
231 if (sctx
->gpu_reset_counter
== latest
)
232 return PIPE_NO_RESET
;
234 sctx
->gpu_reset_counter
= latest
;
235 return PIPE_UNKNOWN_CONTEXT_RESET
;
238 static void si_set_device_reset_callback(struct pipe_context
*ctx
,
239 const struct pipe_device_reset_callback
*cb
)
241 struct si_context
*sctx
= (struct si_context
*)ctx
;
244 sctx
->device_reset_callback
= *cb
;
246 memset(&sctx
->device_reset_callback
, 0,
247 sizeof(sctx
->device_reset_callback
));
250 bool si_check_device_reset(struct si_context
*sctx
)
252 enum pipe_reset_status status
;
254 if (!sctx
->device_reset_callback
.reset
)
257 if (!sctx
->b
.get_device_reset_status
)
260 status
= sctx
->b
.get_device_reset_status(&sctx
->b
);
261 if (status
== PIPE_NO_RESET
)
264 sctx
->device_reset_callback
.reset(sctx
->device_reset_callback
.data
, status
);
268 /* Apitrace profiling:
269 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
270 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
271 * and remember its number.
272 * 3) In Mesa, enable queries and performance counters around that draw
273 * call and print the results.
274 * 4) glretrace --benchmark --markers ..
276 static void si_emit_string_marker(struct pipe_context
*ctx
,
277 const char *string
, int len
)
279 struct si_context
*sctx
= (struct si_context
*)ctx
;
281 dd_parse_apitrace_marker(string
, len
, &sctx
->apitrace_call_number
);
284 u_log_printf(sctx
->log
, "\nString marker: %*s\n", len
, string
);
287 static LLVMTargetMachineRef
288 si_create_llvm_target_machine(struct si_screen
*sscreen
)
290 enum ac_target_machine_options tm_options
=
291 (sscreen
->debug_flags
& DBG(SI_SCHED
) ? AC_TM_SISCHED
: 0) |
292 (sscreen
->info
.chip_class
>= GFX9
? AC_TM_FORCE_ENABLE_XNACK
: 0) |
293 (sscreen
->info
.chip_class
< GFX9
? AC_TM_FORCE_DISABLE_XNACK
: 0) |
294 (!sscreen
->llvm_has_working_vgpr_indexing
? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
: 0);
296 return ac_create_target_machine(sscreen
->info
.family
, tm_options
);
299 static void si_set_debug_callback(struct pipe_context
*ctx
,
300 const struct pipe_debug_callback
*cb
)
302 struct si_context
*sctx
= (struct si_context
*)ctx
;
303 struct si_screen
*screen
= sctx
->screen
;
305 util_queue_finish(&screen
->shader_compiler_queue
);
306 util_queue_finish(&screen
->shader_compiler_queue_low_priority
);
311 memset(&sctx
->debug
, 0, sizeof(sctx
->debug
));
314 static void si_set_log_context(struct pipe_context
*ctx
,
315 struct u_log_context
*log
)
317 struct si_context
*sctx
= (struct si_context
*)ctx
;
321 u_log_add_auto_logger(log
, si_auto_log_cs
, sctx
);
324 static struct pipe_context
*si_create_context(struct pipe_screen
*screen
,
327 struct si_context
*sctx
= CALLOC_STRUCT(si_context
);
328 struct si_screen
* sscreen
= (struct si_screen
*)screen
;
329 struct radeon_winsys
*ws
= sscreen
->ws
;
335 if (flags
& PIPE_CONTEXT_DEBUG
)
336 sscreen
->record_llvm_ir
= true; /* racy but not critical */
338 sctx
->b
.screen
= screen
; /* this must be set first */
340 sctx
->b
.destroy
= si_destroy_context
;
341 sctx
->b
.emit_string_marker
= si_emit_string_marker
;
342 sctx
->b
.set_debug_callback
= si_set_debug_callback
;
343 sctx
->b
.set_log_context
= si_set_log_context
;
344 sctx
->screen
= sscreen
; /* Easy accessing of screen/winsys. */
345 sctx
->is_debug
= (flags
& PIPE_CONTEXT_DEBUG
) != 0;
347 slab_create_child(&sctx
->pool_transfers
, &sscreen
->pool_transfers
);
348 slab_create_child(&sctx
->pool_transfers_unsync
, &sscreen
->pool_transfers
);
350 sctx
->ws
= sscreen
->ws
;
351 sctx
->family
= sscreen
->info
.family
;
352 sctx
->chip_class
= sscreen
->info
.chip_class
;
354 if (sscreen
->info
.drm_major
== 2 && sscreen
->info
.drm_minor
>= 43) {
355 sctx
->b
.get_device_reset_status
= si_get_reset_status
;
356 sctx
->gpu_reset_counter
=
357 sctx
->ws
->query_value(sctx
->ws
,
358 RADEON_GPU_RESET_COUNTER
);
361 sctx
->b
.set_device_reset_callback
= si_set_device_reset_callback
;
363 si_init_context_texture_functions(sctx
);
364 si_init_query_functions(sctx
);
366 if (sctx
->chip_class
== CIK
||
367 sctx
->chip_class
== VI
||
368 sctx
->chip_class
== GFX9
) {
369 sctx
->eop_bug_scratch
= (struct r600_resource
*)
370 pipe_buffer_create(&sscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
371 16 * sscreen
->info
.num_render_backends
);
372 if (!sctx
->eop_bug_scratch
)
376 sctx
->allocator_zeroed_memory
=
377 u_suballocator_create(&sctx
->b
, sscreen
->info
.gart_page_size
,
378 0, PIPE_USAGE_DEFAULT
, 0, true);
379 if (!sctx
->allocator_zeroed_memory
)
382 sctx
->b
.stream_uploader
= u_upload_create(&sctx
->b
, 1024 * 1024,
383 0, PIPE_USAGE_STREAM
,
384 SI_RESOURCE_FLAG_READ_ONLY
);
385 if (!sctx
->b
.stream_uploader
)
388 sctx
->b
.const_uploader
= u_upload_create(&sctx
->b
, 128 * 1024,
389 0, PIPE_USAGE_DEFAULT
,
390 SI_RESOURCE_FLAG_32BIT
|
391 (sscreen
->cpdma_prefetch_writes_memory
?
392 0 : SI_RESOURCE_FLAG_READ_ONLY
));
393 if (!sctx
->b
.const_uploader
)
396 sctx
->cached_gtt_allocator
= u_upload_create(&sctx
->b
, 16 * 1024,
397 0, PIPE_USAGE_STAGING
, 0);
398 if (!sctx
->cached_gtt_allocator
)
401 sctx
->ctx
= sctx
->ws
->ctx_create(sctx
->ws
);
405 if (sscreen
->info
.num_sdma_rings
&& !(sscreen
->debug_flags
& DBG(NO_ASYNC_DMA
))) {
406 sctx
->dma_cs
= sctx
->ws
->cs_create(sctx
->ctx
, RING_DMA
,
407 (void*)si_flush_dma_cs
,
411 if (sscreen
->info
.drm_major
== 3)
412 sctx
->b
.get_device_reset_status
= si_amdgpu_get_reset_status
;
414 si_init_buffer_functions(sctx
);
415 si_init_clear_functions(sctx
);
416 si_init_blit_functions(sctx
);
417 si_init_compute_functions(sctx
);
418 si_init_cp_dma_functions(sctx
);
419 si_init_debug_functions(sctx
);
420 si_init_msaa_functions(sctx
);
421 si_init_streamout_functions(sctx
);
423 if (sscreen
->info
.has_hw_decode
) {
424 sctx
->b
.create_video_codec
= si_uvd_create_decoder
;
425 sctx
->b
.create_video_buffer
= si_video_buffer_create
;
427 sctx
->b
.create_video_codec
= vl_create_decoder
;
428 sctx
->b
.create_video_buffer
= vl_video_buffer_create
;
431 sctx
->gfx_cs
= ws
->cs_create(sctx
->ctx
, RING_GFX
,
432 (void*)si_flush_gfx_cs
, sctx
);
435 sctx
->border_color_table
= malloc(SI_MAX_BORDER_COLORS
*
436 sizeof(*sctx
->border_color_table
));
437 if (!sctx
->border_color_table
)
440 sctx
->border_color_buffer
= (struct r600_resource
*)
441 pipe_buffer_create(screen
, 0, PIPE_USAGE_DEFAULT
,
442 SI_MAX_BORDER_COLORS
*
443 sizeof(*sctx
->border_color_table
));
444 if (!sctx
->border_color_buffer
)
447 sctx
->border_color_map
=
448 ws
->buffer_map(sctx
->border_color_buffer
->buf
,
449 NULL
, PIPE_TRANSFER_WRITE
);
450 if (!sctx
->border_color_map
)
453 si_init_all_descriptors(sctx
);
454 si_init_fence_functions(sctx
);
455 si_init_state_functions(sctx
);
456 si_init_shader_functions(sctx
);
457 si_init_viewport_functions(sctx
);
458 si_init_ia_multi_vgt_param_table(sctx
);
460 if (sctx
->chip_class
>= CIK
)
461 cik_init_sdma_functions(sctx
);
463 si_init_dma_functions(sctx
);
465 if (sscreen
->debug_flags
& DBG(FORCE_DMA
))
466 sctx
->b
.resource_copy_region
= sctx
->dma_copy
;
468 sctx
->blitter
= util_blitter_create(&sctx
->b
);
469 if (sctx
->blitter
== NULL
)
471 sctx
->blitter
->draw_rectangle
= si_draw_rectangle
;
472 sctx
->blitter
->skip_viewport_restore
= true;
474 sctx
->sample_mask
.sample_mask
= 0xffff;
476 if (sctx
->chip_class
>= GFX9
) {
477 sctx
->wait_mem_scratch
= (struct r600_resource
*)
478 pipe_buffer_create(screen
, 0, PIPE_USAGE_DEFAULT
, 4);
479 if (!sctx
->wait_mem_scratch
)
482 /* Initialize the memory. */
483 struct radeon_winsys_cs
*cs
= sctx
->gfx_cs
;
484 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 3, 0));
485 radeon_emit(cs
, S_370_DST_SEL(V_370_MEMORY_SYNC
) |
486 S_370_WR_CONFIRM(1) |
487 S_370_ENGINE_SEL(V_370_ME
));
488 radeon_emit(cs
, sctx
->wait_mem_scratch
->gpu_address
);
489 radeon_emit(cs
, sctx
->wait_mem_scratch
->gpu_address
>> 32);
490 radeon_emit(cs
, sctx
->wait_mem_number
);
491 radeon_add_to_buffer_list(sctx
, cs
, sctx
->wait_mem_scratch
,
492 RADEON_USAGE_WRITE
, RADEON_PRIO_FENCE
);
495 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
496 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
497 if (sctx
->chip_class
== CIK
) {
498 sctx
->null_const_buf
.buffer
=
499 si_aligned_buffer_create(screen
,
500 SI_RESOURCE_FLAG_32BIT
,
501 PIPE_USAGE_DEFAULT
, 16,
502 sctx
->screen
->info
.tcc_cache_line_size
);
503 if (!sctx
->null_const_buf
.buffer
)
505 sctx
->null_const_buf
.buffer_size
= sctx
->null_const_buf
.buffer
->width0
;
507 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
508 for (i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++) {
509 sctx
->b
.set_constant_buffer(&sctx
->b
, shader
, i
,
510 &sctx
->null_const_buf
);
514 si_set_rw_buffer(sctx
, SI_HS_CONST_DEFAULT_TESS_LEVELS
,
515 &sctx
->null_const_buf
);
516 si_set_rw_buffer(sctx
, SI_VS_CONST_INSTANCE_DIVISORS
,
517 &sctx
->null_const_buf
);
518 si_set_rw_buffer(sctx
, SI_VS_CONST_CLIP_PLANES
,
519 &sctx
->null_const_buf
);
520 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
,
521 &sctx
->null_const_buf
);
522 si_set_rw_buffer(sctx
, SI_PS_CONST_SAMPLE_POSITIONS
,
523 &sctx
->null_const_buf
);
525 /* Clear the NULL constant buffer, because loads should return zeros. */
526 si_clear_buffer(sctx
, sctx
->null_const_buf
.buffer
, 0,
527 sctx
->null_const_buf
.buffer
->width0
, 0,
528 SI_COHERENCY_SHADER
);
531 uint64_t max_threads_per_block
;
532 screen
->get_compute_param(screen
, PIPE_SHADER_IR_TGSI
,
533 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK
,
534 &max_threads_per_block
);
536 /* The maximum number of scratch waves. Scratch space isn't divided
537 * evenly between CUs. The number is only a function of the number of CUs.
538 * We can decrease the constant to decrease the scratch buffer size.
540 * sctx->scratch_waves must be >= the maximum posible size of
541 * 1 threadgroup, so that the hw doesn't hang from being unable
544 * The recommended value is 4 per CU at most. Higher numbers don't
545 * bring much benefit, but they still occupy chip resources (think
546 * async compute). I've seen ~2% performance difference between 4 and 32.
548 sctx
->scratch_waves
= MAX2(32 * sscreen
->info
.num_good_compute_units
,
549 max_threads_per_block
/ 64);
551 sctx
->tm
= si_create_llvm_target_machine(sscreen
);
553 /* Bindless handles. */
554 sctx
->tex_handles
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
555 _mesa_key_pointer_equal
);
556 sctx
->img_handles
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
557 _mesa_key_pointer_equal
);
559 util_dynarray_init(&sctx
->resident_tex_handles
, NULL
);
560 util_dynarray_init(&sctx
->resident_img_handles
, NULL
);
561 util_dynarray_init(&sctx
->resident_tex_needs_color_decompress
, NULL
);
562 util_dynarray_init(&sctx
->resident_img_needs_color_decompress
, NULL
);
563 util_dynarray_init(&sctx
->resident_tex_needs_depth_decompress
, NULL
);
565 /* this must be last */
566 si_begin_new_gfx_cs(sctx
);
569 fprintf(stderr
, "radeonsi: Failed to create a context.\n");
570 si_destroy_context(&sctx
->b
);
574 static struct pipe_context
*si_pipe_create_context(struct pipe_screen
*screen
,
575 void *priv
, unsigned flags
)
577 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
578 struct pipe_context
*ctx
;
580 if (sscreen
->debug_flags
& DBG(CHECK_VM
))
581 flags
|= PIPE_CONTEXT_DEBUG
;
583 ctx
= si_create_context(screen
, flags
);
585 if (!(flags
& PIPE_CONTEXT_PREFER_THREADED
))
588 /* Clover (compute-only) is unsupported. */
589 if (flags
& PIPE_CONTEXT_COMPUTE_ONLY
)
592 /* When shaders are logged to stderr, asynchronous compilation is
594 if (sscreen
->debug_flags
& DBG_ALL_SHADERS
)
597 /* Use asynchronous flushes only on amdgpu, since the radeon
598 * implementation for fence_server_sync is incomplete. */
599 return threaded_context_create(ctx
, &sscreen
->pool_transfers
,
600 si_replace_buffer_storage
,
601 sscreen
->info
.drm_major
>= 3 ? si_create_fence
: NULL
,
602 &((struct si_context
*)ctx
)->tc
);
608 static void si_destroy_screen(struct pipe_screen
* pscreen
)
610 struct si_screen
*sscreen
= (struct si_screen
*)pscreen
;
611 struct si_shader_part
*parts
[] = {
613 sscreen
->tcs_epilogs
,
620 if (!sscreen
->ws
->unref(sscreen
->ws
))
623 util_queue_destroy(&sscreen
->shader_compiler_queue
);
624 util_queue_destroy(&sscreen
->shader_compiler_queue_low_priority
);
626 for (i
= 0; i
< ARRAY_SIZE(sscreen
->tm
); i
++)
628 LLVMDisposeTargetMachine(sscreen
->tm
[i
]);
630 for (i
= 0; i
< ARRAY_SIZE(sscreen
->tm_low_priority
); i
++)
631 if (sscreen
->tm_low_priority
[i
])
632 LLVMDisposeTargetMachine(sscreen
->tm_low_priority
[i
]);
634 /* Free shader parts. */
635 for (i
= 0; i
< ARRAY_SIZE(parts
); i
++) {
637 struct si_shader_part
*part
= parts
[i
];
639 parts
[i
] = part
->next
;
640 ac_shader_binary_clean(&part
->binary
);
644 mtx_destroy(&sscreen
->shader_parts_mutex
);
645 si_destroy_shader_cache(sscreen
);
647 si_perfcounters_destroy(sscreen
);
648 si_gpu_load_kill_thread(sscreen
);
650 mtx_destroy(&sscreen
->gpu_load_mutex
);
651 mtx_destroy(&sscreen
->aux_context_lock
);
652 sscreen
->aux_context
->destroy(sscreen
->aux_context
);
654 slab_destroy_parent(&sscreen
->pool_transfers
);
656 disk_cache_destroy(sscreen
->disk_shader_cache
);
657 sscreen
->ws
->destroy(sscreen
->ws
);
661 static void si_init_gs_info(struct si_screen
*sscreen
)
663 sscreen
->gs_table_depth
= ac_get_gs_table_depth(sscreen
->info
.chip_class
,
664 sscreen
->info
.family
);
667 static void si_handle_env_var_force_family(struct si_screen
*sscreen
)
669 const char *family
= debug_get_option("SI_FORCE_FAMILY", NULL
);
675 for (i
= CHIP_TAHITI
; i
< CHIP_LAST
; i
++) {
676 if (!strcmp(family
, ac_get_llvm_processor_name(i
))) {
677 /* Override family and chip_class. */
678 sscreen
->info
.family
= i
;
680 if (i
>= CHIP_VEGA10
)
681 sscreen
->info
.chip_class
= GFX9
;
682 else if (i
>= CHIP_TONGA
)
683 sscreen
->info
.chip_class
= VI
;
684 else if (i
>= CHIP_BONAIRE
)
685 sscreen
->info
.chip_class
= CIK
;
687 sscreen
->info
.chip_class
= SI
;
689 /* Don't submit any IBs. */
690 setenv("RADEON_NOOP", "1", 1);
695 fprintf(stderr
, "radeonsi: Unknown family: %s\n", family
);
699 static void si_test_vmfault(struct si_screen
*sscreen
)
701 struct pipe_context
*ctx
= sscreen
->aux_context
;
702 struct si_context
*sctx
= (struct si_context
*)ctx
;
703 struct pipe_resource
*buf
=
704 pipe_buffer_create_const0(&sscreen
->b
, 0, PIPE_USAGE_DEFAULT
, 64);
707 puts("Buffer allocation failed.");
711 r600_resource(buf
)->gpu_address
= 0; /* cause a VM fault */
713 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_CP
)) {
714 si_copy_buffer(sctx
, buf
, buf
, 0, 4, 4, 0);
715 ctx
->flush(ctx
, NULL
, 0);
716 puts("VM fault test: CP - done.");
718 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_SDMA
)) {
719 sctx
->dma_clear_buffer(sctx
, buf
, 0, 4, 0);
720 ctx
->flush(ctx
, NULL
, 0);
721 puts("VM fault test: SDMA - done.");
723 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_SHADER
)) {
724 util_test_constant_buffer(ctx
, buf
);
725 puts("VM fault test: Shader - done.");
730 static void si_disk_cache_create(struct si_screen
*sscreen
)
732 /* Don't use the cache if shader dumping is enabled. */
733 if (sscreen
->debug_flags
& DBG_ALL_SHADERS
)
736 uint32_t mesa_timestamp
;
737 if (disk_cache_get_function_timestamp(si_disk_cache_create
,
741 uint32_t llvm_timestamp
;
743 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo
,
745 res
= asprintf(×tamp_str
, "%u_%u",
746 mesa_timestamp
, llvm_timestamp
);
750 /* These flags affect shader compilation. */
751 #define ALL_FLAGS (DBG(FS_CORRECT_DERIVS_AFTER_KILL) | \
755 uint64_t shader_debug_flags
= sscreen
->debug_flags
&
758 /* Add the high bits of 32-bit addresses, which affects
759 * how 32-bit addresses are expanded to 64 bits.
761 STATIC_ASSERT(ALL_FLAGS
<= UINT_MAX
);
762 shader_debug_flags
|= (uint64_t)sscreen
->info
.address32_hi
<< 32;
764 sscreen
->disk_shader_cache
=
765 disk_cache_create(si_get_family_name(sscreen
),
773 struct pipe_screen
*radeonsi_screen_create(struct radeon_winsys
*ws
,
774 const struct pipe_screen_config
*config
)
776 struct si_screen
*sscreen
= CALLOC_STRUCT(si_screen
);
777 unsigned num_threads
, num_compiler_threads
, num_compiler_threads_lowprio
, i
;
784 ws
->query_info(ws
, &sscreen
->info
);
786 sscreen
->debug_flags
= debug_get_flags_option("R600_DEBUG",
789 /* Set functions first. */
790 sscreen
->b
.context_create
= si_pipe_create_context
;
791 sscreen
->b
.destroy
= si_destroy_screen
;
793 si_init_screen_get_functions(sscreen
);
794 si_init_screen_buffer_functions(sscreen
);
795 si_init_screen_fence_functions(sscreen
);
796 si_init_screen_state_functions(sscreen
);
797 si_init_screen_texture_functions(sscreen
);
798 si_init_screen_query_functions(sscreen
);
800 /* Set these flags in debug_flags early, so that the shader cache takes
803 if (driQueryOptionb(config
->options
,
804 "glsl_correct_derivatives_after_discard"))
805 sscreen
->debug_flags
|= DBG(FS_CORRECT_DERIVS_AFTER_KILL
);
806 if (driQueryOptionb(config
->options
, "radeonsi_enable_sisched"))
807 sscreen
->debug_flags
|= DBG(SI_SCHED
);
810 if (sscreen
->debug_flags
& DBG(INFO
))
811 ac_print_gpu_info(&sscreen
->info
);
813 slab_create_parent(&sscreen
->pool_transfers
,
814 sizeof(struct r600_transfer
), 64);
816 sscreen
->force_aniso
= MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
817 if (sscreen
->force_aniso
>= 0) {
818 printf("radeonsi: Forcing anisotropy filter to %ix\n",
819 /* round down to a power of two */
820 1 << util_logbase2(sscreen
->force_aniso
));
823 (void) mtx_init(&sscreen
->aux_context_lock
, mtx_plain
);
824 (void) mtx_init(&sscreen
->gpu_load_mutex
, mtx_plain
);
826 si_init_gs_info(sscreen
);
827 if (!si_init_shader_cache(sscreen
)) {
832 si_disk_cache_create(sscreen
);
834 /* Only enable as many threads as we have target machines, but at most
835 * the number of CPUs - 1 if there is more than one.
837 num_threads
= sysconf(_SC_NPROCESSORS_ONLN
);
838 num_threads
= MAX2(1, num_threads
- 1);
839 num_compiler_threads
= MIN2(num_threads
, ARRAY_SIZE(sscreen
->tm
));
840 num_compiler_threads_lowprio
=
841 MIN2(num_threads
, ARRAY_SIZE(sscreen
->tm_low_priority
));
843 if (!util_queue_init(&sscreen
->shader_compiler_queue
, "si_shader",
844 32, num_compiler_threads
,
845 UTIL_QUEUE_INIT_RESIZE_IF_FULL
)) {
846 si_destroy_shader_cache(sscreen
);
851 if (!util_queue_init(&sscreen
->shader_compiler_queue_low_priority
,
853 32, num_compiler_threads_lowprio
,
854 UTIL_QUEUE_INIT_RESIZE_IF_FULL
|
855 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
)) {
856 si_destroy_shader_cache(sscreen
);
861 si_handle_env_var_force_family(sscreen
);
863 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
864 si_init_perfcounters(sscreen
);
866 /* Determine tessellation ring info. */
867 bool double_offchip_buffers
= sscreen
->info
.chip_class
>= CIK
&&
868 sscreen
->info
.family
!= CHIP_CARRIZO
&&
869 sscreen
->info
.family
!= CHIP_STONEY
;
870 /* This must be one less than the maximum number due to a hw limitation.
871 * Various hardware bugs in SI, CIK, and GFX9 need this.
873 unsigned max_offchip_buffers_per_se
;
875 /* Only certain chips can use the maximum value. */
876 if (sscreen
->info
.family
== CHIP_VEGA12
)
877 max_offchip_buffers_per_se
= double_offchip_buffers
? 128 : 64;
879 max_offchip_buffers_per_se
= double_offchip_buffers
? 127 : 63;
881 unsigned max_offchip_buffers
= max_offchip_buffers_per_se
*
882 sscreen
->info
.max_se
;
883 unsigned offchip_granularity
;
885 /* Hawaii has a bug with offchip buffers > 256 that can be worked
886 * around by setting 4K granularity.
888 if (sscreen
->info
.family
== CHIP_HAWAII
) {
889 sscreen
->tess_offchip_block_dw_size
= 4096;
890 offchip_granularity
= V_03093C_X_4K_DWORDS
;
892 sscreen
->tess_offchip_block_dw_size
= 8192;
893 offchip_granularity
= V_03093C_X_8K_DWORDS
;
896 sscreen
->tess_factor_ring_size
= 32768 * sscreen
->info
.max_se
;
897 assert(((sscreen
->tess_factor_ring_size
/ 4) & C_030938_SIZE
) == 0);
898 sscreen
->tess_offchip_ring_size
= max_offchip_buffers
*
899 sscreen
->tess_offchip_block_dw_size
* 4;
901 if (sscreen
->info
.chip_class
>= CIK
) {
902 if (sscreen
->info
.chip_class
>= VI
)
903 --max_offchip_buffers
;
904 sscreen
->vgt_hs_offchip_param
=
905 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers
) |
906 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity
);
908 assert(offchip_granularity
== V_03093C_X_8K_DWORDS
);
909 sscreen
->vgt_hs_offchip_param
=
910 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers
);
913 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
915 sscreen
->has_clear_state
= sscreen
->info
.chip_class
>= CIK
;
917 sscreen
->has_distributed_tess
=
918 sscreen
->info
.chip_class
>= VI
&&
919 sscreen
->info
.max_se
>= 2;
921 sscreen
->has_draw_indirect_multi
=
922 (sscreen
->info
.family
>= CHIP_POLARIS10
) ||
923 (sscreen
->info
.chip_class
== VI
&&
924 sscreen
->info
.pfp_fw_version
>= 121 &&
925 sscreen
->info
.me_fw_version
>= 87) ||
926 (sscreen
->info
.chip_class
== CIK
&&
927 sscreen
->info
.pfp_fw_version
>= 211 &&
928 sscreen
->info
.me_fw_version
>= 173) ||
929 (sscreen
->info
.chip_class
== SI
&&
930 sscreen
->info
.pfp_fw_version
>= 79 &&
931 sscreen
->info
.me_fw_version
>= 142);
933 sscreen
->has_out_of_order_rast
= sscreen
->info
.chip_class
>= VI
&&
934 sscreen
->info
.max_se
>= 2 &&
935 !(sscreen
->debug_flags
& DBG(NO_OUT_OF_ORDER
));
936 sscreen
->assume_no_z_fights
=
937 driQueryOptionb(config
->options
, "radeonsi_assume_no_z_fights");
938 sscreen
->commutative_blend_add
=
939 driQueryOptionb(config
->options
, "radeonsi_commutative_blend_add");
940 sscreen
->clear_db_cache_before_clear
=
941 driQueryOptionb(config
->options
, "radeonsi_clear_db_cache_before_clear");
942 sscreen
->has_msaa_sample_loc_bug
= (sscreen
->info
.family
>= CHIP_POLARIS10
&&
943 sscreen
->info
.family
<= CHIP_POLARIS12
) ||
944 sscreen
->info
.family
== CHIP_VEGA10
||
945 sscreen
->info
.family
== CHIP_RAVEN
;
946 sscreen
->has_ls_vgpr_init_bug
= sscreen
->info
.family
== CHIP_VEGA10
||
947 sscreen
->info
.family
== CHIP_RAVEN
;
949 if (sscreen
->debug_flags
& DBG(DPBB
)) {
950 sscreen
->dpbb_allowed
= true;
952 /* Only enable primitive binning on Raven by default. */
953 /* TODO: Investigate if binning is profitable on Vega12. */
954 sscreen
->dpbb_allowed
= sscreen
->info
.family
== CHIP_RAVEN
&&
955 !(sscreen
->debug_flags
& DBG(NO_DPBB
));
958 if (sscreen
->debug_flags
& DBG(DFSM
)) {
959 sscreen
->dfsm_allowed
= sscreen
->dpbb_allowed
;
961 sscreen
->dfsm_allowed
= sscreen
->dpbb_allowed
&&
962 !(sscreen
->debug_flags
& DBG(NO_DFSM
));
965 /* While it would be nice not to have this flag, we are constrained
966 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
969 sscreen
->llvm_has_working_vgpr_indexing
= sscreen
->info
.chip_class
<= VI
;
971 /* Some chips have RB+ registers, but don't support RB+. Those must
974 if (sscreen
->info
.family
== CHIP_STONEY
||
975 sscreen
->info
.chip_class
>= GFX9
) {
976 sscreen
->has_rbplus
= true;
978 sscreen
->rbplus_allowed
=
979 !(sscreen
->debug_flags
& DBG(NO_RB_PLUS
)) &&
980 (sscreen
->info
.family
== CHIP_STONEY
||
981 sscreen
->info
.family
== CHIP_VEGA12
||
982 sscreen
->info
.family
== CHIP_RAVEN
);
985 sscreen
->dcc_msaa_allowed
=
986 !(sscreen
->debug_flags
& DBG(NO_DCC_MSAA
));
988 sscreen
->cpdma_prefetch_writes_memory
= sscreen
->info
.chip_class
<= VI
;
990 (void) mtx_init(&sscreen
->shader_parts_mutex
, mtx_plain
);
991 sscreen
->use_monolithic_shaders
=
992 (sscreen
->debug_flags
& DBG(MONOLITHIC_SHADERS
)) != 0;
994 sscreen
->barrier_flags
.cp_to_L2
= SI_CONTEXT_INV_SMEM_L1
|
995 SI_CONTEXT_INV_VMEM_L1
;
996 if (sscreen
->info
.chip_class
<= VI
) {
997 sscreen
->barrier_flags
.cp_to_L2
|= SI_CONTEXT_INV_GLOBAL_L2
;
998 sscreen
->barrier_flags
.L2_to_cp
|= SI_CONTEXT_WRITEBACK_GLOBAL_L2
;
1001 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
1002 sscreen
->debug_flags
|= DBG_ALL_SHADERS
;
1004 for (i
= 0; i
< num_compiler_threads
; i
++)
1005 sscreen
->tm
[i
] = si_create_llvm_target_machine(sscreen
);
1006 for (i
= 0; i
< num_compiler_threads_lowprio
; i
++)
1007 sscreen
->tm_low_priority
[i
] = si_create_llvm_target_machine(sscreen
);
1009 /* Create the auxiliary context. This must be done last. */
1010 sscreen
->aux_context
= si_create_context(&sscreen
->b
, 0);
1012 if (sscreen
->debug_flags
& DBG(TEST_DMA
))
1013 si_test_dma(sscreen
);
1015 if (sscreen
->debug_flags
& (DBG(TEST_VMFAULT_CP
) |
1016 DBG(TEST_VMFAULT_SDMA
) |
1017 DBG(TEST_VMFAULT_SHADER
)))
1018 si_test_vmfault(sscreen
);