radeonsi: add support for Vega12
[mesa.git] / src / gallium / drivers / radeonsi / si_pipe.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_pipe.h"
25 #include "si_public.h"
26 #include "si_shader_internal.h"
27 #include "sid.h"
28
29 #include "radeon/radeon_uvd.h"
30 #include "util/hash_table.h"
31 #include "util/u_log.h"
32 #include "util/u_memory.h"
33 #include "util/u_suballoc.h"
34 #include "util/u_tests.h"
35 #include "util/xmlconfig.h"
36 #include "vl/vl_decoder.h"
37 #include "../ddebug/dd_util.h"
38
39 static const struct debug_named_value debug_options[] = {
40 /* Shader logging options: */
41 { "vs", DBG(VS), "Print vertex shaders" },
42 { "ps", DBG(PS), "Print pixel shaders" },
43 { "gs", DBG(GS), "Print geometry shaders" },
44 { "tcs", DBG(TCS), "Print tessellation control shaders" },
45 { "tes", DBG(TES), "Print tessellation evaluation shaders" },
46 { "cs", DBG(CS), "Print compute shaders" },
47 { "noir", DBG(NO_IR), "Don't print the LLVM IR"},
48 { "notgsi", DBG(NO_TGSI), "Don't print the TGSI"},
49 { "noasm", DBG(NO_ASM), "Don't print disassembled shaders"},
50 { "preoptir", DBG(PREOPT_IR), "Print the LLVM IR before initial optimizations" },
51
52 /* Shader compiler options the shader cache should be aware of: */
53 { "unsafemath", DBG(UNSAFE_MATH), "Enable unsafe math shader optimizations" },
54 { "sisched", DBG(SI_SCHED), "Enable LLVM SI Machine Instruction Scheduler." },
55
56 /* Shader compiler options (with no effect on the shader cache): */
57 { "checkir", DBG(CHECK_IR), "Enable additional sanity checks on shader IR" },
58 { "nir", DBG(NIR), "Enable experimental NIR shaders" },
59 { "mono", DBG(MONOLITHIC_SHADERS), "Use old-style monolithic shaders compiled on demand" },
60 { "nooptvariant", DBG(NO_OPT_VARIANT), "Disable compiling optimized shader variants." },
61
62 /* Information logging options: */
63 { "info", DBG(INFO), "Print driver information" },
64 { "tex", DBG(TEX), "Print texture info" },
65 { "compute", DBG(COMPUTE), "Print compute info" },
66 { "vm", DBG(VM), "Print virtual addresses when creating resources" },
67
68 /* Driver options: */
69 { "forcedma", DBG(FORCE_DMA), "Use asynchronous DMA for all operations when possible." },
70 { "nodma", DBG(NO_ASYNC_DMA), "Disable asynchronous DMA" },
71 { "nowc", DBG(NO_WC), "Disable GTT write combining" },
72 { "check_vm", DBG(CHECK_VM), "Check VM faults and dump debug info." },
73 { "reserve_vmid", DBG(RESERVE_VMID), "Force VMID reservation per context." },
74
75 /* 3D engine options: */
76 { "switch_on_eop", DBG(SWITCH_ON_EOP), "Program WD/IA to switch on end-of-packet." },
77 { "nooutoforder", DBG(NO_OUT_OF_ORDER), "Disable out-of-order rasterization" },
78 { "nodpbb", DBG(NO_DPBB), "Disable DPBB." },
79 { "nodfsm", DBG(NO_DFSM), "Disable DFSM." },
80 { "dpbb", DBG(DPBB), "Enable DPBB." },
81 { "dfsm", DBG(DFSM), "Enable DFSM." },
82 { "nohyperz", DBG(NO_HYPERZ), "Disable Hyper-Z" },
83 { "norbplus", DBG(NO_RB_PLUS), "Disable RB+." },
84 { "no2d", DBG(NO_2D_TILING), "Disable 2D tiling" },
85 { "notiling", DBG(NO_TILING), "Disable tiling" },
86 { "nodcc", DBG(NO_DCC), "Disable DCC." },
87 { "nodccclear", DBG(NO_DCC_CLEAR), "Disable DCC fast clear." },
88 { "nodccfb", DBG(NO_DCC_FB), "Disable separate DCC on the main framebuffer" },
89 { "nodccmsaa", DBG(NO_DCC_MSAA), "Disable DCC for MSAA" },
90 { "dccmsaa", DBG(DCC_MSAA), "Enable DCC for MSAA" },
91
92 /* Tests: */
93 { "testdma", DBG(TEST_DMA), "Invoke SDMA tests and exit." },
94 { "testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit." },
95 { "testvmfaultsdma", DBG(TEST_VMFAULT_SDMA), "Invoke a SDMA VM fault test and exit." },
96 { "testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit." },
97
98 DEBUG_NAMED_VALUE_END /* must be last */
99 };
100
101 /*
102 * pipe_context
103 */
104 static void si_destroy_context(struct pipe_context *context)
105 {
106 struct si_context *sctx = (struct si_context *)context;
107 int i;
108
109 /* Unreference the framebuffer normally to disable related logic
110 * properly.
111 */
112 struct pipe_framebuffer_state fb = {};
113 if (context->set_framebuffer_state)
114 context->set_framebuffer_state(context, &fb);
115
116 si_release_all_descriptors(sctx);
117
118 pipe_resource_reference(&sctx->esgs_ring, NULL);
119 pipe_resource_reference(&sctx->gsvs_ring, NULL);
120 pipe_resource_reference(&sctx->tess_rings, NULL);
121 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
122 r600_resource_reference(&sctx->border_color_buffer, NULL);
123 free(sctx->border_color_table);
124 r600_resource_reference(&sctx->scratch_buffer, NULL);
125 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
126 r600_resource_reference(&sctx->wait_mem_scratch, NULL);
127
128 si_pm4_free_state(sctx, sctx->init_config, ~0);
129 if (sctx->init_config_gs_rings)
130 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
131 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
132 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
133
134 if (sctx->fixed_func_tcs_shader.cso)
135 sctx->b.b.delete_tcs_state(&sctx->b.b, sctx->fixed_func_tcs_shader.cso);
136 if (sctx->custom_dsa_flush)
137 sctx->b.b.delete_depth_stencil_alpha_state(&sctx->b.b, sctx->custom_dsa_flush);
138 if (sctx->custom_blend_resolve)
139 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_resolve);
140 if (sctx->custom_blend_fmask_decompress)
141 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_fmask_decompress);
142 if (sctx->custom_blend_eliminate_fastclear)
143 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_eliminate_fastclear);
144 if (sctx->custom_blend_dcc_decompress)
145 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_dcc_decompress);
146 if (sctx->vs_blit_pos)
147 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos);
148 if (sctx->vs_blit_pos_layered)
149 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos_layered);
150 if (sctx->vs_blit_color)
151 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color);
152 if (sctx->vs_blit_color_layered)
153 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color_layered);
154 if (sctx->vs_blit_texcoord)
155 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_texcoord);
156
157 if (sctx->blitter)
158 util_blitter_destroy(sctx->blitter);
159
160 si_common_context_cleanup(&sctx->b);
161
162 LLVMDisposeTargetMachine(sctx->tm);
163
164 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
165
166 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
167 _mesa_hash_table_destroy(sctx->img_handles, NULL);
168
169 util_dynarray_fini(&sctx->resident_tex_handles);
170 util_dynarray_fini(&sctx->resident_img_handles);
171 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
172 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
173 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
174 FREE(sctx);
175 }
176
177 static enum pipe_reset_status
178 si_amdgpu_get_reset_status(struct pipe_context *ctx)
179 {
180 struct si_context *sctx = (struct si_context *)ctx;
181
182 return sctx->b.ws->ctx_query_reset_status(sctx->b.ctx);
183 }
184
185 /* Apitrace profiling:
186 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
187 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
188 * and remember its number.
189 * 3) In Mesa, enable queries and performance counters around that draw
190 * call and print the results.
191 * 4) glretrace --benchmark --markers ..
192 */
193 static void si_emit_string_marker(struct pipe_context *ctx,
194 const char *string, int len)
195 {
196 struct si_context *sctx = (struct si_context *)ctx;
197
198 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
199
200 if (sctx->b.log)
201 u_log_printf(sctx->b.log, "\nString marker: %*s\n", len, string);
202 }
203
204 static LLVMTargetMachineRef
205 si_create_llvm_target_machine(struct si_screen *sscreen)
206 {
207 enum ac_target_machine_options tm_options =
208 (sscreen->debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
209 (sscreen->info.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
210 (sscreen->info.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
211 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
212
213 return ac_create_target_machine(sscreen->info.family, tm_options);
214 }
215
216 static void si_set_debug_callback(struct pipe_context *ctx,
217 const struct pipe_debug_callback *cb)
218 {
219 struct si_context *sctx = (struct si_context *)ctx;
220 struct si_screen *screen = sctx->screen;
221
222 util_queue_finish(&screen->shader_compiler_queue);
223 util_queue_finish(&screen->shader_compiler_queue_low_priority);
224
225 if (cb)
226 sctx->debug = *cb;
227 else
228 memset(&sctx->debug, 0, sizeof(sctx->debug));
229 }
230
231 static void si_set_log_context(struct pipe_context *ctx,
232 struct u_log_context *log)
233 {
234 struct si_context *sctx = (struct si_context *)ctx;
235 sctx->b.log = log;
236
237 if (log)
238 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
239 }
240
241 static struct pipe_context *si_create_context(struct pipe_screen *screen,
242 unsigned flags)
243 {
244 struct si_context *sctx = CALLOC_STRUCT(si_context);
245 struct si_screen* sscreen = (struct si_screen *)screen;
246 struct radeon_winsys *ws = sscreen->ws;
247 int shader, i;
248
249 if (!sctx)
250 return NULL;
251
252 if (flags & PIPE_CONTEXT_DEBUG)
253 sscreen->record_llvm_ir = true; /* racy but not critical */
254
255 sctx->b.b.screen = screen; /* this must be set first */
256 sctx->b.b.priv = NULL;
257 sctx->b.b.destroy = si_destroy_context;
258 sctx->b.b.emit_string_marker = si_emit_string_marker;
259 sctx->b.b.set_debug_callback = si_set_debug_callback;
260 sctx->b.b.set_log_context = si_set_log_context;
261 sctx->b.set_atom_dirty = (void *)si_set_atom_dirty;
262 sctx->screen = sscreen; /* Easy accessing of screen/winsys. */
263 sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;
264
265 if (!si_common_context_init(&sctx->b, sscreen, flags))
266 goto fail;
267
268 if (sscreen->info.drm_major == 3)
269 sctx->b.b.get_device_reset_status = si_amdgpu_get_reset_status;
270
271 si_init_buffer_functions(sctx);
272 si_init_clear_functions(sctx);
273 si_init_blit_functions(sctx);
274 si_init_compute_functions(sctx);
275 si_init_cp_dma_functions(sctx);
276 si_init_debug_functions(sctx);
277 si_init_msaa_functions(sctx);
278 si_init_streamout_functions(sctx);
279
280 if (sscreen->info.has_hw_decode) {
281 sctx->b.b.create_video_codec = si_uvd_create_decoder;
282 sctx->b.b.create_video_buffer = si_video_buffer_create;
283 } else {
284 sctx->b.b.create_video_codec = vl_create_decoder;
285 sctx->b.b.create_video_buffer = vl_video_buffer_create;
286 }
287
288 sctx->b.gfx.cs = ws->cs_create(sctx->b.ctx, RING_GFX,
289 si_context_gfx_flush, sctx);
290 sctx->b.gfx.flush = si_context_gfx_flush;
291
292 /* Border colors. */
293 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS *
294 sizeof(*sctx->border_color_table));
295 if (!sctx->border_color_table)
296 goto fail;
297
298 sctx->border_color_buffer = (struct r600_resource*)
299 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT,
300 SI_MAX_BORDER_COLORS *
301 sizeof(*sctx->border_color_table));
302 if (!sctx->border_color_buffer)
303 goto fail;
304
305 sctx->border_color_map =
306 ws->buffer_map(sctx->border_color_buffer->buf,
307 NULL, PIPE_TRANSFER_WRITE);
308 if (!sctx->border_color_map)
309 goto fail;
310
311 si_init_all_descriptors(sctx);
312 si_init_fence_functions(sctx);
313 si_init_state_functions(sctx);
314 si_init_shader_functions(sctx);
315 si_init_viewport_functions(sctx);
316 si_init_ia_multi_vgt_param_table(sctx);
317
318 if (sctx->b.chip_class >= CIK)
319 cik_init_sdma_functions(sctx);
320 else
321 si_init_dma_functions(sctx);
322
323 if (sscreen->debug_flags & DBG(FORCE_DMA))
324 sctx->b.b.resource_copy_region = sctx->b.dma_copy;
325
326 sctx->blitter = util_blitter_create(&sctx->b.b);
327 if (sctx->blitter == NULL)
328 goto fail;
329 sctx->blitter->draw_rectangle = si_draw_rectangle;
330 sctx->blitter->skip_viewport_restore = true;
331
332 sctx->sample_mask.sample_mask = 0xffff;
333
334 /* these must be last */
335 si_begin_new_cs(sctx);
336
337 if (sctx->b.chip_class >= GFX9) {
338 sctx->wait_mem_scratch = (struct r600_resource*)
339 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT, 4);
340 if (!sctx->wait_mem_scratch)
341 goto fail;
342
343 /* Initialize the memory. */
344 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
345 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
346 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
347 S_370_WR_CONFIRM(1) |
348 S_370_ENGINE_SEL(V_370_ME));
349 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address);
350 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address >> 32);
351 radeon_emit(cs, sctx->wait_mem_number);
352 }
353
354 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
355 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
356 if (sctx->b.chip_class == CIK) {
357 sctx->null_const_buf.buffer =
358 si_aligned_buffer_create(screen,
359 R600_RESOURCE_FLAG_32BIT,
360 PIPE_USAGE_DEFAULT, 16,
361 sctx->screen->info.tcc_cache_line_size);
362 if (!sctx->null_const_buf.buffer)
363 goto fail;
364 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
365
366 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
367 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
368 sctx->b.b.set_constant_buffer(&sctx->b.b, shader, i,
369 &sctx->null_const_buf);
370 }
371 }
372
373 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS,
374 &sctx->null_const_buf);
375 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS,
376 &sctx->null_const_buf);
377 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES,
378 &sctx->null_const_buf);
379 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE,
380 &sctx->null_const_buf);
381 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS,
382 &sctx->null_const_buf);
383
384 /* Clear the NULL constant buffer, because loads should return zeros. */
385 si_clear_buffer(&sctx->b.b, sctx->null_const_buf.buffer, 0,
386 sctx->null_const_buf.buffer->width0, 0,
387 R600_COHERENCY_SHADER);
388 }
389
390 uint64_t max_threads_per_block;
391 screen->get_compute_param(screen, PIPE_SHADER_IR_TGSI,
392 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
393 &max_threads_per_block);
394
395 /* The maximum number of scratch waves. Scratch space isn't divided
396 * evenly between CUs. The number is only a function of the number of CUs.
397 * We can decrease the constant to decrease the scratch buffer size.
398 *
399 * sctx->scratch_waves must be >= the maximum posible size of
400 * 1 threadgroup, so that the hw doesn't hang from being unable
401 * to start any.
402 *
403 * The recommended value is 4 per CU at most. Higher numbers don't
404 * bring much benefit, but they still occupy chip resources (think
405 * async compute). I've seen ~2% performance difference between 4 and 32.
406 */
407 sctx->scratch_waves = MAX2(32 * sscreen->info.num_good_compute_units,
408 max_threads_per_block / 64);
409
410 sctx->tm = si_create_llvm_target_machine(sscreen);
411
412 /* Bindless handles. */
413 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
414 _mesa_key_pointer_equal);
415 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
416 _mesa_key_pointer_equal);
417
418 util_dynarray_init(&sctx->resident_tex_handles, NULL);
419 util_dynarray_init(&sctx->resident_img_handles, NULL);
420 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
421 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
422 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
423
424 return &sctx->b.b;
425 fail:
426 fprintf(stderr, "radeonsi: Failed to create a context.\n");
427 si_destroy_context(&sctx->b.b);
428 return NULL;
429 }
430
431 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen,
432 void *priv, unsigned flags)
433 {
434 struct si_screen *sscreen = (struct si_screen *)screen;
435 struct pipe_context *ctx;
436
437 if (sscreen->debug_flags & DBG(CHECK_VM))
438 flags |= PIPE_CONTEXT_DEBUG;
439
440 ctx = si_create_context(screen, flags);
441
442 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
443 return ctx;
444
445 /* Clover (compute-only) is unsupported. */
446 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
447 return ctx;
448
449 /* When shaders are logged to stderr, asynchronous compilation is
450 * disabled too. */
451 if (sscreen->debug_flags & DBG_ALL_SHADERS)
452 return ctx;
453
454 /* Use asynchronous flushes only on amdgpu, since the radeon
455 * implementation for fence_server_sync is incomplete. */
456 return threaded_context_create(ctx, &sscreen->pool_transfers,
457 si_replace_buffer_storage,
458 sscreen->info.drm_major >= 3 ? si_create_fence : NULL,
459 &((struct si_context*)ctx)->b.tc);
460 }
461
462 /*
463 * pipe_screen
464 */
465 static void si_destroy_screen(struct pipe_screen* pscreen)
466 {
467 struct si_screen *sscreen = (struct si_screen *)pscreen;
468 struct si_shader_part *parts[] = {
469 sscreen->vs_prologs,
470 sscreen->tcs_epilogs,
471 sscreen->gs_prologs,
472 sscreen->ps_prologs,
473 sscreen->ps_epilogs
474 };
475 unsigned i;
476
477 if (!sscreen->ws->unref(sscreen->ws))
478 return;
479
480 util_queue_destroy(&sscreen->shader_compiler_queue);
481 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
482
483 for (i = 0; i < ARRAY_SIZE(sscreen->tm); i++)
484 if (sscreen->tm[i])
485 LLVMDisposeTargetMachine(sscreen->tm[i]);
486
487 for (i = 0; i < ARRAY_SIZE(sscreen->tm_low_priority); i++)
488 if (sscreen->tm_low_priority[i])
489 LLVMDisposeTargetMachine(sscreen->tm_low_priority[i]);
490
491 /* Free shader parts. */
492 for (i = 0; i < ARRAY_SIZE(parts); i++) {
493 while (parts[i]) {
494 struct si_shader_part *part = parts[i];
495
496 parts[i] = part->next;
497 ac_shader_binary_clean(&part->binary);
498 FREE(part);
499 }
500 }
501 mtx_destroy(&sscreen->shader_parts_mutex);
502 si_destroy_shader_cache(sscreen);
503
504 si_perfcounters_destroy(sscreen);
505 si_gpu_load_kill_thread(sscreen);
506
507 mtx_destroy(&sscreen->gpu_load_mutex);
508 mtx_destroy(&sscreen->aux_context_lock);
509 sscreen->aux_context->destroy(sscreen->aux_context);
510
511 slab_destroy_parent(&sscreen->pool_transfers);
512
513 disk_cache_destroy(sscreen->disk_shader_cache);
514 sscreen->ws->destroy(sscreen->ws);
515 FREE(sscreen);
516 }
517
518 static bool si_init_gs_info(struct si_screen *sscreen)
519 {
520 /* gs_table_depth is not used by GFX9 */
521 if (sscreen->info.chip_class >= GFX9)
522 return true;
523
524 switch (sscreen->info.family) {
525 case CHIP_OLAND:
526 case CHIP_HAINAN:
527 case CHIP_KAVERI:
528 case CHIP_KABINI:
529 case CHIP_MULLINS:
530 case CHIP_ICELAND:
531 case CHIP_CARRIZO:
532 case CHIP_STONEY:
533 sscreen->gs_table_depth = 16;
534 return true;
535 case CHIP_TAHITI:
536 case CHIP_PITCAIRN:
537 case CHIP_VERDE:
538 case CHIP_BONAIRE:
539 case CHIP_HAWAII:
540 case CHIP_TONGA:
541 case CHIP_FIJI:
542 case CHIP_POLARIS10:
543 case CHIP_POLARIS11:
544 case CHIP_POLARIS12:
545 sscreen->gs_table_depth = 32;
546 return true;
547 default:
548 return false;
549 }
550 }
551
552 static void si_handle_env_var_force_family(struct si_screen *sscreen)
553 {
554 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
555 unsigned i;
556
557 if (!family)
558 return;
559
560 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
561 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
562 /* Override family and chip_class. */
563 sscreen->info.family = i;
564
565 if (i >= CHIP_VEGA10)
566 sscreen->info.chip_class = GFX9;
567 else if (i >= CHIP_TONGA)
568 sscreen->info.chip_class = VI;
569 else if (i >= CHIP_BONAIRE)
570 sscreen->info.chip_class = CIK;
571 else
572 sscreen->info.chip_class = SI;
573
574 /* Don't submit any IBs. */
575 setenv("RADEON_NOOP", "1", 1);
576 return;
577 }
578 }
579
580 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
581 exit(1);
582 }
583
584 static void si_test_vmfault(struct si_screen *sscreen)
585 {
586 struct pipe_context *ctx = sscreen->aux_context;
587 struct si_context *sctx = (struct si_context *)ctx;
588 struct pipe_resource *buf =
589 pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64);
590
591 if (!buf) {
592 puts("Buffer allocation failed.");
593 exit(1);
594 }
595
596 r600_resource(buf)->gpu_address = 0; /* cause a VM fault */
597
598 if (sscreen->debug_flags & DBG(TEST_VMFAULT_CP)) {
599 si_copy_buffer(sctx, buf, buf, 0, 4, 4, 0);
600 ctx->flush(ctx, NULL, 0);
601 puts("VM fault test: CP - done.");
602 }
603 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SDMA)) {
604 sctx->b.dma_clear_buffer(ctx, buf, 0, 4, 0);
605 ctx->flush(ctx, NULL, 0);
606 puts("VM fault test: SDMA - done.");
607 }
608 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SHADER)) {
609 util_test_constant_buffer(ctx, buf);
610 puts("VM fault test: Shader - done.");
611 }
612 exit(0);
613 }
614
615 static void si_disk_cache_create(struct si_screen *sscreen)
616 {
617 /* Don't use the cache if shader dumping is enabled. */
618 if (sscreen->debug_flags & DBG_ALL_SHADERS)
619 return;
620
621 uint32_t mesa_timestamp;
622 if (disk_cache_get_function_timestamp(si_disk_cache_create,
623 &mesa_timestamp)) {
624 char *timestamp_str;
625 int res = -1;
626 uint32_t llvm_timestamp;
627
628 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo,
629 &llvm_timestamp)) {
630 res = asprintf(&timestamp_str, "%u_%u",
631 mesa_timestamp, llvm_timestamp);
632 }
633
634 if (res != -1) {
635 /* These flags affect shader compilation. */
636 #define ALL_FLAGS (DBG(FS_CORRECT_DERIVS_AFTER_KILL) | \
637 DBG(SI_SCHED) | \
638 DBG(UNSAFE_MATH) | \
639 DBG(NIR))
640 uint64_t shader_debug_flags = sscreen->debug_flags &
641 ALL_FLAGS;
642
643 /* Add the high bits of 32-bit addresses, which affects
644 * how 32-bit addresses are expanded to 64 bits.
645 */
646 STATIC_ASSERT(ALL_FLAGS <= UINT_MAX);
647 shader_debug_flags |= (uint64_t)sscreen->info.address32_hi << 32;
648
649 sscreen->disk_shader_cache =
650 disk_cache_create(si_get_family_name(sscreen),
651 timestamp_str,
652 shader_debug_flags);
653 free(timestamp_str);
654 }
655 }
656 }
657
658 struct pipe_screen *radeonsi_screen_create(struct radeon_winsys *ws,
659 const struct pipe_screen_config *config)
660 {
661 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
662 unsigned num_threads, num_compiler_threads, num_compiler_threads_lowprio, i;
663
664 if (!sscreen) {
665 return NULL;
666 }
667
668 sscreen->ws = ws;
669 ws->query_info(ws, &sscreen->info);
670
671 sscreen->debug_flags = debug_get_flags_option("R600_DEBUG",
672 debug_options, 0);
673
674 /* Set functions first. */
675 sscreen->b.context_create = si_pipe_create_context;
676 sscreen->b.destroy = si_destroy_screen;
677
678 si_init_screen_get_functions(sscreen);
679 si_init_screen_buffer_functions(sscreen);
680 si_init_screen_fence_functions(sscreen);
681 si_init_screen_state_functions(sscreen);
682 si_init_screen_texture_functions(sscreen);
683 si_init_screen_query_functions(sscreen);
684
685 /* Set these flags in debug_flags early, so that the shader cache takes
686 * them into account.
687 */
688 if (driQueryOptionb(config->options,
689 "glsl_correct_derivatives_after_discard"))
690 sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
691 if (driQueryOptionb(config->options, "radeonsi_enable_sisched"))
692 sscreen->debug_flags |= DBG(SI_SCHED);
693
694
695 if (sscreen->debug_flags & DBG(INFO))
696 ac_print_gpu_info(&sscreen->info);
697
698 slab_create_parent(&sscreen->pool_transfers,
699 sizeof(struct r600_transfer), 64);
700
701 sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
702 if (sscreen->force_aniso >= 0) {
703 printf("radeonsi: Forcing anisotropy filter to %ix\n",
704 /* round down to a power of two */
705 1 << util_logbase2(sscreen->force_aniso));
706 }
707
708 (void) mtx_init(&sscreen->aux_context_lock, mtx_plain);
709 (void) mtx_init(&sscreen->gpu_load_mutex, mtx_plain);
710
711 if (!si_init_gs_info(sscreen) ||
712 !si_init_shader_cache(sscreen)) {
713 FREE(sscreen);
714 return NULL;
715 }
716
717 si_disk_cache_create(sscreen);
718
719 /* Only enable as many threads as we have target machines, but at most
720 * the number of CPUs - 1 if there is more than one.
721 */
722 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
723 num_threads = MAX2(1, num_threads - 1);
724 num_compiler_threads = MIN2(num_threads, ARRAY_SIZE(sscreen->tm));
725 num_compiler_threads_lowprio =
726 MIN2(num_threads, ARRAY_SIZE(sscreen->tm_low_priority));
727
728 if (!util_queue_init(&sscreen->shader_compiler_queue, "si_shader",
729 32, num_compiler_threads,
730 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
731 si_destroy_shader_cache(sscreen);
732 FREE(sscreen);
733 return NULL;
734 }
735
736 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority,
737 "si_shader_low",
738 32, num_compiler_threads_lowprio,
739 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
740 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
741 si_destroy_shader_cache(sscreen);
742 FREE(sscreen);
743 return NULL;
744 }
745
746 si_handle_env_var_force_family(sscreen);
747
748 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
749 si_init_perfcounters(sscreen);
750
751 /* Determine tessellation ring info. */
752 bool double_offchip_buffers = sscreen->info.chip_class >= CIK &&
753 sscreen->info.family != CHIP_CARRIZO &&
754 sscreen->info.family != CHIP_STONEY;
755 /* This must be one less than the maximum number due to a hw limitation.
756 * Various hardware bugs in SI, CIK, and GFX9 need this.
757 */
758 unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 127 : 63;
759 unsigned max_offchip_buffers = max_offchip_buffers_per_se *
760 sscreen->info.max_se;
761 unsigned offchip_granularity;
762
763 /* Hawaii has a bug with offchip buffers > 256 that can be worked
764 * around by setting 4K granularity.
765 */
766 if (sscreen->info.family == CHIP_HAWAII) {
767 sscreen->tess_offchip_block_dw_size = 4096;
768 offchip_granularity = V_03093C_X_4K_DWORDS;
769 } else {
770 sscreen->tess_offchip_block_dw_size = 8192;
771 offchip_granularity = V_03093C_X_8K_DWORDS;
772 }
773
774 sscreen->tess_factor_ring_size = 32768 * sscreen->info.max_se;
775 assert(((sscreen->tess_factor_ring_size / 4) & C_030938_SIZE) == 0);
776 sscreen->tess_offchip_ring_size = max_offchip_buffers *
777 sscreen->tess_offchip_block_dw_size * 4;
778
779 if (sscreen->info.chip_class >= CIK) {
780 if (sscreen->info.chip_class >= VI)
781 --max_offchip_buffers;
782 sscreen->vgt_hs_offchip_param =
783 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
784 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
785 } else {
786 assert(offchip_granularity == V_03093C_X_8K_DWORDS);
787 sscreen->vgt_hs_offchip_param =
788 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
789 }
790
791 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
792 * on SI. */
793 sscreen->has_clear_state = sscreen->info.chip_class >= CIK;
794
795 sscreen->has_distributed_tess =
796 sscreen->info.chip_class >= VI &&
797 sscreen->info.max_se >= 2;
798
799 sscreen->has_draw_indirect_multi =
800 (sscreen->info.family >= CHIP_POLARIS10) ||
801 (sscreen->info.chip_class == VI &&
802 sscreen->info.pfp_fw_version >= 121 &&
803 sscreen->info.me_fw_version >= 87) ||
804 (sscreen->info.chip_class == CIK &&
805 sscreen->info.pfp_fw_version >= 211 &&
806 sscreen->info.me_fw_version >= 173) ||
807 (sscreen->info.chip_class == SI &&
808 sscreen->info.pfp_fw_version >= 79 &&
809 sscreen->info.me_fw_version >= 142);
810
811 sscreen->has_out_of_order_rast = sscreen->info.chip_class >= VI &&
812 sscreen->info.max_se >= 2 &&
813 !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER));
814 sscreen->assume_no_z_fights =
815 driQueryOptionb(config->options, "radeonsi_assume_no_z_fights");
816 sscreen->commutative_blend_add =
817 driQueryOptionb(config->options, "radeonsi_commutative_blend_add");
818 sscreen->clear_db_cache_before_clear =
819 driQueryOptionb(config->options, "radeonsi_clear_db_cache_before_clear");
820 sscreen->has_msaa_sample_loc_bug = (sscreen->info.family >= CHIP_POLARIS10 &&
821 sscreen->info.family <= CHIP_POLARIS12) ||
822 sscreen->info.family == CHIP_VEGA10 ||
823 sscreen->info.family == CHIP_RAVEN;
824 sscreen->has_ls_vgpr_init_bug = sscreen->info.family == CHIP_VEGA10 ||
825 sscreen->info.family == CHIP_RAVEN;
826
827 if (sscreen->debug_flags & DBG(DPBB)) {
828 sscreen->dpbb_allowed = true;
829 } else {
830 /* Only enable primitive binning on Raven by default. */
831 /* TODO: Investigate if binning is profitable on Vega12. */
832 sscreen->dpbb_allowed = sscreen->info.family == CHIP_RAVEN &&
833 !(sscreen->debug_flags & DBG(NO_DPBB));
834 }
835
836 if (sscreen->debug_flags & DBG(DFSM)) {
837 sscreen->dfsm_allowed = sscreen->dpbb_allowed;
838 } else {
839 sscreen->dfsm_allowed = sscreen->dpbb_allowed &&
840 !(sscreen->debug_flags & DBG(NO_DFSM));
841 }
842
843 /* While it would be nice not to have this flag, we are constrained
844 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
845 * on GFX9.
846 */
847 sscreen->llvm_has_working_vgpr_indexing = sscreen->info.chip_class <= VI;
848
849 /* Some chips have RB+ registers, but don't support RB+. Those must
850 * always disable it.
851 */
852 if (sscreen->info.family == CHIP_STONEY ||
853 sscreen->info.chip_class >= GFX9) {
854 sscreen->has_rbplus = true;
855
856 sscreen->rbplus_allowed =
857 !(sscreen->debug_flags & DBG(NO_RB_PLUS)) &&
858 (sscreen->info.family == CHIP_STONEY ||
859 sscreen->info.family == CHIP_VEGA12 ||
860 sscreen->info.family == CHIP_RAVEN);
861 }
862
863 sscreen->dcc_msaa_allowed =
864 !(sscreen->debug_flags & DBG(NO_DCC_MSAA)) &&
865 (sscreen->debug_flags & DBG(DCC_MSAA) ||
866 sscreen->info.chip_class == VI);
867
868 sscreen->cpdma_prefetch_writes_memory = sscreen->info.chip_class <= VI;
869
870 (void) mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
871 sscreen->use_monolithic_shaders =
872 (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
873
874 sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SMEM_L1 |
875 SI_CONTEXT_INV_VMEM_L1;
876 if (sscreen->info.chip_class <= VI) {
877 sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_GLOBAL_L2;
878 sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
879 }
880
881 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
882 sscreen->debug_flags |= DBG_ALL_SHADERS;
883
884 for (i = 0; i < num_compiler_threads; i++)
885 sscreen->tm[i] = si_create_llvm_target_machine(sscreen);
886 for (i = 0; i < num_compiler_threads_lowprio; i++)
887 sscreen->tm_low_priority[i] = si_create_llvm_target_machine(sscreen);
888
889 /* Create the auxiliary context. This must be done last. */
890 sscreen->aux_context = si_create_context(&sscreen->b, 0);
891
892 if (sscreen->debug_flags & DBG(TEST_DMA))
893 si_test_dma(sscreen);
894
895 if (sscreen->debug_flags & (DBG(TEST_VMFAULT_CP) |
896 DBG(TEST_VMFAULT_SDMA) |
897 DBG(TEST_VMFAULT_SHADER)))
898 si_test_vmfault(sscreen);
899
900 return &sscreen->b;
901 }