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