radeonsi: switch radeon_add_to_buffer_list parameter to si_context
[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 { "nofmask", DBG(NO_FMASK), "Disable MSAA compression" },
92
93 /* Tests: */
94 { "testdma", DBG(TEST_DMA), "Invoke SDMA tests and exit." },
95 { "testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit." },
96 { "testvmfaultsdma", DBG(TEST_VMFAULT_SDMA), "Invoke a SDMA VM fault test and exit." },
97 { "testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit." },
98
99 DEBUG_NAMED_VALUE_END /* must be last */
100 };
101
102 /*
103 * pipe_context
104 */
105 static void si_destroy_context(struct pipe_context *context)
106 {
107 struct si_context *sctx = (struct si_context *)context;
108 int i;
109
110 /* Unreference the framebuffer normally to disable related logic
111 * properly.
112 */
113 struct pipe_framebuffer_state fb = {};
114 if (context->set_framebuffer_state)
115 context->set_framebuffer_state(context, &fb);
116
117 si_release_all_descriptors(sctx);
118
119 pipe_resource_reference(&sctx->esgs_ring, NULL);
120 pipe_resource_reference(&sctx->gsvs_ring, NULL);
121 pipe_resource_reference(&sctx->tess_rings, NULL);
122 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
123 r600_resource_reference(&sctx->border_color_buffer, NULL);
124 free(sctx->border_color_table);
125 r600_resource_reference(&sctx->scratch_buffer, NULL);
126 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
127 r600_resource_reference(&sctx->wait_mem_scratch, NULL);
128
129 si_pm4_free_state(sctx, sctx->init_config, ~0);
130 if (sctx->init_config_gs_rings)
131 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
132 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
133 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
134
135 if (sctx->fixed_func_tcs_shader.cso)
136 sctx->b.b.delete_tcs_state(&sctx->b.b, sctx->fixed_func_tcs_shader.cso);
137 if (sctx->custom_dsa_flush)
138 sctx->b.b.delete_depth_stencil_alpha_state(&sctx->b.b, sctx->custom_dsa_flush);
139 if (sctx->custom_blend_resolve)
140 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_resolve);
141 if (sctx->custom_blend_fmask_decompress)
142 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_fmask_decompress);
143 if (sctx->custom_blend_eliminate_fastclear)
144 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_eliminate_fastclear);
145 if (sctx->custom_blend_dcc_decompress)
146 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_dcc_decompress);
147 if (sctx->vs_blit_pos)
148 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos);
149 if (sctx->vs_blit_pos_layered)
150 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos_layered);
151 if (sctx->vs_blit_color)
152 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color);
153 if (sctx->vs_blit_color_layered)
154 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color_layered);
155 if (sctx->vs_blit_texcoord)
156 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_texcoord);
157
158 if (sctx->blitter)
159 util_blitter_destroy(sctx->blitter);
160
161 si_common_context_cleanup(&sctx->b);
162
163 LLVMDisposeTargetMachine(sctx->tm);
164
165 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
166
167 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
168 _mesa_hash_table_destroy(sctx->img_handles, NULL);
169
170 util_dynarray_fini(&sctx->resident_tex_handles);
171 util_dynarray_fini(&sctx->resident_img_handles);
172 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
173 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
174 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
175 FREE(sctx);
176 }
177
178 static enum pipe_reset_status
179 si_amdgpu_get_reset_status(struct pipe_context *ctx)
180 {
181 struct si_context *sctx = (struct si_context *)ctx;
182
183 return sctx->b.ws->ctx_query_reset_status(sctx->b.ctx);
184 }
185
186 /* Apitrace profiling:
187 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
188 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
189 * and remember its number.
190 * 3) In Mesa, enable queries and performance counters around that draw
191 * call and print the results.
192 * 4) glretrace --benchmark --markers ..
193 */
194 static void si_emit_string_marker(struct pipe_context *ctx,
195 const char *string, int len)
196 {
197 struct si_context *sctx = (struct si_context *)ctx;
198
199 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
200
201 if (sctx->b.log)
202 u_log_printf(sctx->b.log, "\nString marker: %*s\n", len, string);
203 }
204
205 static LLVMTargetMachineRef
206 si_create_llvm_target_machine(struct si_screen *sscreen)
207 {
208 enum ac_target_machine_options tm_options =
209 (sscreen->debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
210 (sscreen->info.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
211 (sscreen->info.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
212 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
213
214 return ac_create_target_machine(sscreen->info.family, tm_options);
215 }
216
217 static void si_set_debug_callback(struct pipe_context *ctx,
218 const struct pipe_debug_callback *cb)
219 {
220 struct si_context *sctx = (struct si_context *)ctx;
221 struct si_screen *screen = sctx->screen;
222
223 util_queue_finish(&screen->shader_compiler_queue);
224 util_queue_finish(&screen->shader_compiler_queue_low_priority);
225
226 if (cb)
227 sctx->debug = *cb;
228 else
229 memset(&sctx->debug, 0, sizeof(sctx->debug));
230 }
231
232 static void si_set_log_context(struct pipe_context *ctx,
233 struct u_log_context *log)
234 {
235 struct si_context *sctx = (struct si_context *)ctx;
236 sctx->b.log = log;
237
238 if (log)
239 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
240 }
241
242 static struct pipe_context *si_create_context(struct pipe_screen *screen,
243 unsigned flags)
244 {
245 struct si_context *sctx = CALLOC_STRUCT(si_context);
246 struct si_screen* sscreen = (struct si_screen *)screen;
247 struct radeon_winsys *ws = sscreen->ws;
248 int shader, i;
249
250 if (!sctx)
251 return NULL;
252
253 if (flags & PIPE_CONTEXT_DEBUG)
254 sscreen->record_llvm_ir = true; /* racy but not critical */
255
256 sctx->b.b.screen = screen; /* this must be set first */
257 sctx->b.b.priv = NULL;
258 sctx->b.b.destroy = si_destroy_context;
259 sctx->b.b.emit_string_marker = si_emit_string_marker;
260 sctx->b.b.set_debug_callback = si_set_debug_callback;
261 sctx->b.b.set_log_context = si_set_log_context;
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 (void*)si_flush_gfx_cs, sctx);
290
291 /* Border colors. */
292 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS *
293 sizeof(*sctx->border_color_table));
294 if (!sctx->border_color_table)
295 goto fail;
296
297 sctx->border_color_buffer = (struct r600_resource*)
298 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT,
299 SI_MAX_BORDER_COLORS *
300 sizeof(*sctx->border_color_table));
301 if (!sctx->border_color_buffer)
302 goto fail;
303
304 sctx->border_color_map =
305 ws->buffer_map(sctx->border_color_buffer->buf,
306 NULL, PIPE_TRANSFER_WRITE);
307 if (!sctx->border_color_map)
308 goto fail;
309
310 si_init_all_descriptors(sctx);
311 si_init_fence_functions(sctx);
312 si_init_state_functions(sctx);
313 si_init_shader_functions(sctx);
314 si_init_viewport_functions(sctx);
315 si_init_ia_multi_vgt_param_table(sctx);
316
317 if (sctx->b.chip_class >= CIK)
318 cik_init_sdma_functions(sctx);
319 else
320 si_init_dma_functions(sctx);
321
322 if (sscreen->debug_flags & DBG(FORCE_DMA))
323 sctx->b.b.resource_copy_region = sctx->b.dma_copy;
324
325 sctx->blitter = util_blitter_create(&sctx->b.b);
326 if (sctx->blitter == NULL)
327 goto fail;
328 sctx->blitter->draw_rectangle = si_draw_rectangle;
329 sctx->blitter->skip_viewport_restore = true;
330
331 sctx->sample_mask.sample_mask = 0xffff;
332
333 /* these must be last */
334 si_begin_new_gfx_cs(sctx);
335
336 if (sctx->b.chip_class >= GFX9) {
337 sctx->wait_mem_scratch = (struct r600_resource*)
338 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT, 4);
339 if (!sctx->wait_mem_scratch)
340 goto fail;
341
342 /* Initialize the memory. */
343 struct radeon_winsys_cs *cs = sctx->b.gfx_cs;
344 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
345 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
346 S_370_WR_CONFIRM(1) |
347 S_370_ENGINE_SEL(V_370_ME));
348 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address);
349 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address >> 32);
350 radeon_emit(cs, sctx->wait_mem_number);
351 }
352
353 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
354 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
355 if (sctx->b.chip_class == CIK) {
356 sctx->null_const_buf.buffer =
357 si_aligned_buffer_create(screen,
358 R600_RESOURCE_FLAG_32BIT,
359 PIPE_USAGE_DEFAULT, 16,
360 sctx->screen->info.tcc_cache_line_size);
361 if (!sctx->null_const_buf.buffer)
362 goto fail;
363 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
364
365 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
366 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
367 sctx->b.b.set_constant_buffer(&sctx->b.b, shader, i,
368 &sctx->null_const_buf);
369 }
370 }
371
372 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS,
373 &sctx->null_const_buf);
374 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS,
375 &sctx->null_const_buf);
376 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES,
377 &sctx->null_const_buf);
378 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE,
379 &sctx->null_const_buf);
380 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS,
381 &sctx->null_const_buf);
382
383 /* Clear the NULL constant buffer, because loads should return zeros. */
384 si_clear_buffer(&sctx->b.b, sctx->null_const_buf.buffer, 0,
385 sctx->null_const_buf.buffer->width0, 0,
386 R600_COHERENCY_SHADER);
387 }
388
389 uint64_t max_threads_per_block;
390 screen->get_compute_param(screen, PIPE_SHADER_IR_TGSI,
391 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
392 &max_threads_per_block);
393
394 /* The maximum number of scratch waves. Scratch space isn't divided
395 * evenly between CUs. The number is only a function of the number of CUs.
396 * We can decrease the constant to decrease the scratch buffer size.
397 *
398 * sctx->scratch_waves must be >= the maximum posible size of
399 * 1 threadgroup, so that the hw doesn't hang from being unable
400 * to start any.
401 *
402 * The recommended value is 4 per CU at most. Higher numbers don't
403 * bring much benefit, but they still occupy chip resources (think
404 * async compute). I've seen ~2% performance difference between 4 and 32.
405 */
406 sctx->scratch_waves = MAX2(32 * sscreen->info.num_good_compute_units,
407 max_threads_per_block / 64);
408
409 sctx->tm = si_create_llvm_target_machine(sscreen);
410
411 /* Bindless handles. */
412 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
413 _mesa_key_pointer_equal);
414 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
415 _mesa_key_pointer_equal);
416
417 util_dynarray_init(&sctx->resident_tex_handles, NULL);
418 util_dynarray_init(&sctx->resident_img_handles, NULL);
419 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
420 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
421 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
422
423 return &sctx->b.b;
424 fail:
425 fprintf(stderr, "radeonsi: Failed to create a context.\n");
426 si_destroy_context(&sctx->b.b);
427 return NULL;
428 }
429
430 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen,
431 void *priv, unsigned flags)
432 {
433 struct si_screen *sscreen = (struct si_screen *)screen;
434 struct pipe_context *ctx;
435
436 if (sscreen->debug_flags & DBG(CHECK_VM))
437 flags |= PIPE_CONTEXT_DEBUG;
438
439 ctx = si_create_context(screen, flags);
440
441 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
442 return ctx;
443
444 /* Clover (compute-only) is unsupported. */
445 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
446 return ctx;
447
448 /* When shaders are logged to stderr, asynchronous compilation is
449 * disabled too. */
450 if (sscreen->debug_flags & DBG_ALL_SHADERS)
451 return ctx;
452
453 /* Use asynchronous flushes only on amdgpu, since the radeon
454 * implementation for fence_server_sync is incomplete. */
455 return threaded_context_create(ctx, &sscreen->pool_transfers,
456 si_replace_buffer_storage,
457 sscreen->info.drm_major >= 3 ? si_create_fence : NULL,
458 &((struct si_context*)ctx)->b.tc);
459 }
460
461 /*
462 * pipe_screen
463 */
464 static void si_destroy_screen(struct pipe_screen* pscreen)
465 {
466 struct si_screen *sscreen = (struct si_screen *)pscreen;
467 struct si_shader_part *parts[] = {
468 sscreen->vs_prologs,
469 sscreen->tcs_epilogs,
470 sscreen->gs_prologs,
471 sscreen->ps_prologs,
472 sscreen->ps_epilogs
473 };
474 unsigned i;
475
476 if (!sscreen->ws->unref(sscreen->ws))
477 return;
478
479 util_queue_destroy(&sscreen->shader_compiler_queue);
480 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
481
482 for (i = 0; i < ARRAY_SIZE(sscreen->tm); i++)
483 if (sscreen->tm[i])
484 LLVMDisposeTargetMachine(sscreen->tm[i]);
485
486 for (i = 0; i < ARRAY_SIZE(sscreen->tm_low_priority); i++)
487 if (sscreen->tm_low_priority[i])
488 LLVMDisposeTargetMachine(sscreen->tm_low_priority[i]);
489
490 /* Free shader parts. */
491 for (i = 0; i < ARRAY_SIZE(parts); i++) {
492 while (parts[i]) {
493 struct si_shader_part *part = parts[i];
494
495 parts[i] = part->next;
496 ac_shader_binary_clean(&part->binary);
497 FREE(part);
498 }
499 }
500 mtx_destroy(&sscreen->shader_parts_mutex);
501 si_destroy_shader_cache(sscreen);
502
503 si_perfcounters_destroy(sscreen);
504 si_gpu_load_kill_thread(sscreen);
505
506 mtx_destroy(&sscreen->gpu_load_mutex);
507 mtx_destroy(&sscreen->aux_context_lock);
508 sscreen->aux_context->destroy(sscreen->aux_context);
509
510 slab_destroy_parent(&sscreen->pool_transfers);
511
512 disk_cache_destroy(sscreen->disk_shader_cache);
513 sscreen->ws->destroy(sscreen->ws);
514 FREE(sscreen);
515 }
516
517 static bool si_init_gs_info(struct si_screen *sscreen)
518 {
519 /* gs_table_depth is not used by GFX9 */
520 if (sscreen->info.chip_class >= GFX9)
521 return true;
522
523 switch (sscreen->info.family) {
524 case CHIP_OLAND:
525 case CHIP_HAINAN:
526 case CHIP_KAVERI:
527 case CHIP_KABINI:
528 case CHIP_MULLINS:
529 case CHIP_ICELAND:
530 case CHIP_CARRIZO:
531 case CHIP_STONEY:
532 sscreen->gs_table_depth = 16;
533 return true;
534 case CHIP_TAHITI:
535 case CHIP_PITCAIRN:
536 case CHIP_VERDE:
537 case CHIP_BONAIRE:
538 case CHIP_HAWAII:
539 case CHIP_TONGA:
540 case CHIP_FIJI:
541 case CHIP_POLARIS10:
542 case CHIP_POLARIS11:
543 case CHIP_POLARIS12:
544 sscreen->gs_table_depth = 32;
545 return true;
546 default:
547 return false;
548 }
549 }
550
551 static void si_handle_env_var_force_family(struct si_screen *sscreen)
552 {
553 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
554 unsigned i;
555
556 if (!family)
557 return;
558
559 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
560 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
561 /* Override family and chip_class. */
562 sscreen->info.family = i;
563
564 if (i >= CHIP_VEGA10)
565 sscreen->info.chip_class = GFX9;
566 else if (i >= CHIP_TONGA)
567 sscreen->info.chip_class = VI;
568 else if (i >= CHIP_BONAIRE)
569 sscreen->info.chip_class = CIK;
570 else
571 sscreen->info.chip_class = SI;
572
573 /* Don't submit any IBs. */
574 setenv("RADEON_NOOP", "1", 1);
575 return;
576 }
577 }
578
579 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
580 exit(1);
581 }
582
583 static void si_test_vmfault(struct si_screen *sscreen)
584 {
585 struct pipe_context *ctx = sscreen->aux_context;
586 struct si_context *sctx = (struct si_context *)ctx;
587 struct pipe_resource *buf =
588 pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64);
589
590 if (!buf) {
591 puts("Buffer allocation failed.");
592 exit(1);
593 }
594
595 r600_resource(buf)->gpu_address = 0; /* cause a VM fault */
596
597 if (sscreen->debug_flags & DBG(TEST_VMFAULT_CP)) {
598 si_copy_buffer(sctx, buf, buf, 0, 4, 4, 0);
599 ctx->flush(ctx, NULL, 0);
600 puts("VM fault test: CP - done.");
601 }
602 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SDMA)) {
603 sctx->b.dma_clear_buffer(ctx, buf, 0, 4, 0);
604 ctx->flush(ctx, NULL, 0);
605 puts("VM fault test: SDMA - done.");
606 }
607 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SHADER)) {
608 util_test_constant_buffer(ctx, buf);
609 puts("VM fault test: Shader - done.");
610 }
611 exit(0);
612 }
613
614 static void si_disk_cache_create(struct si_screen *sscreen)
615 {
616 /* Don't use the cache if shader dumping is enabled. */
617 if (sscreen->debug_flags & DBG_ALL_SHADERS)
618 return;
619
620 uint32_t mesa_timestamp;
621 if (disk_cache_get_function_timestamp(si_disk_cache_create,
622 &mesa_timestamp)) {
623 char *timestamp_str;
624 int res = -1;
625 uint32_t llvm_timestamp;
626
627 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo,
628 &llvm_timestamp)) {
629 res = asprintf(&timestamp_str, "%u_%u",
630 mesa_timestamp, llvm_timestamp);
631 }
632
633 if (res != -1) {
634 /* These flags affect shader compilation. */
635 #define ALL_FLAGS (DBG(FS_CORRECT_DERIVS_AFTER_KILL) | \
636 DBG(SI_SCHED) | \
637 DBG(UNSAFE_MATH) | \
638 DBG(NIR))
639 uint64_t shader_debug_flags = sscreen->debug_flags &
640 ALL_FLAGS;
641
642 /* Add the high bits of 32-bit addresses, which affects
643 * how 32-bit addresses are expanded to 64 bits.
644 */
645 STATIC_ASSERT(ALL_FLAGS <= UINT_MAX);
646 shader_debug_flags |= (uint64_t)sscreen->info.address32_hi << 32;
647
648 sscreen->disk_shader_cache =
649 disk_cache_create(si_get_family_name(sscreen),
650 timestamp_str,
651 shader_debug_flags);
652 free(timestamp_str);
653 }
654 }
655 }
656
657 struct pipe_screen *radeonsi_screen_create(struct radeon_winsys *ws,
658 const struct pipe_screen_config *config)
659 {
660 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
661 unsigned num_threads, num_compiler_threads, num_compiler_threads_lowprio, i;
662
663 if (!sscreen) {
664 return NULL;
665 }
666
667 sscreen->ws = ws;
668 ws->query_info(ws, &sscreen->info);
669
670 sscreen->debug_flags = debug_get_flags_option("R600_DEBUG",
671 debug_options, 0);
672
673 /* Set functions first. */
674 sscreen->b.context_create = si_pipe_create_context;
675 sscreen->b.destroy = si_destroy_screen;
676
677 si_init_screen_get_functions(sscreen);
678 si_init_screen_buffer_functions(sscreen);
679 si_init_screen_fence_functions(sscreen);
680 si_init_screen_state_functions(sscreen);
681 si_init_screen_texture_functions(sscreen);
682 si_init_screen_query_functions(sscreen);
683
684 /* Set these flags in debug_flags early, so that the shader cache takes
685 * them into account.
686 */
687 if (driQueryOptionb(config->options,
688 "glsl_correct_derivatives_after_discard"))
689 sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
690 if (driQueryOptionb(config->options, "radeonsi_enable_sisched"))
691 sscreen->debug_flags |= DBG(SI_SCHED);
692
693
694 if (sscreen->debug_flags & DBG(INFO))
695 ac_print_gpu_info(&sscreen->info);
696
697 slab_create_parent(&sscreen->pool_transfers,
698 sizeof(struct r600_transfer), 64);
699
700 sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
701 if (sscreen->force_aniso >= 0) {
702 printf("radeonsi: Forcing anisotropy filter to %ix\n",
703 /* round down to a power of two */
704 1 << util_logbase2(sscreen->force_aniso));
705 }
706
707 (void) mtx_init(&sscreen->aux_context_lock, mtx_plain);
708 (void) mtx_init(&sscreen->gpu_load_mutex, mtx_plain);
709
710 if (!si_init_gs_info(sscreen) ||
711 !si_init_shader_cache(sscreen)) {
712 FREE(sscreen);
713 return NULL;
714 }
715
716 si_disk_cache_create(sscreen);
717
718 /* Only enable as many threads as we have target machines, but at most
719 * the number of CPUs - 1 if there is more than one.
720 */
721 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
722 num_threads = MAX2(1, num_threads - 1);
723 num_compiler_threads = MIN2(num_threads, ARRAY_SIZE(sscreen->tm));
724 num_compiler_threads_lowprio =
725 MIN2(num_threads, ARRAY_SIZE(sscreen->tm_low_priority));
726
727 if (!util_queue_init(&sscreen->shader_compiler_queue, "si_shader",
728 32, num_compiler_threads,
729 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
730 si_destroy_shader_cache(sscreen);
731 FREE(sscreen);
732 return NULL;
733 }
734
735 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority,
736 "si_shader_low",
737 32, num_compiler_threads_lowprio,
738 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
739 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
740 si_destroy_shader_cache(sscreen);
741 FREE(sscreen);
742 return NULL;
743 }
744
745 si_handle_env_var_force_family(sscreen);
746
747 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
748 si_init_perfcounters(sscreen);
749
750 /* Determine tessellation ring info. */
751 bool double_offchip_buffers = sscreen->info.chip_class >= CIK &&
752 sscreen->info.family != CHIP_CARRIZO &&
753 sscreen->info.family != CHIP_STONEY;
754 /* This must be one less than the maximum number due to a hw limitation.
755 * Various hardware bugs in SI, CIK, and GFX9 need this.
756 */
757 unsigned max_offchip_buffers_per_se;
758
759 /* Only certain chips can use the maximum value. */
760 if (sscreen->info.family == CHIP_VEGA12)
761 max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
762 else
763 max_offchip_buffers_per_se = double_offchip_buffers ? 127 : 63;
764
765 unsigned max_offchip_buffers = max_offchip_buffers_per_se *
766 sscreen->info.max_se;
767 unsigned offchip_granularity;
768
769 /* Hawaii has a bug with offchip buffers > 256 that can be worked
770 * around by setting 4K granularity.
771 */
772 if (sscreen->info.family == CHIP_HAWAII) {
773 sscreen->tess_offchip_block_dw_size = 4096;
774 offchip_granularity = V_03093C_X_4K_DWORDS;
775 } else {
776 sscreen->tess_offchip_block_dw_size = 8192;
777 offchip_granularity = V_03093C_X_8K_DWORDS;
778 }
779
780 sscreen->tess_factor_ring_size = 32768 * sscreen->info.max_se;
781 assert(((sscreen->tess_factor_ring_size / 4) & C_030938_SIZE) == 0);
782 sscreen->tess_offchip_ring_size = max_offchip_buffers *
783 sscreen->tess_offchip_block_dw_size * 4;
784
785 if (sscreen->info.chip_class >= CIK) {
786 if (sscreen->info.chip_class >= VI)
787 --max_offchip_buffers;
788 sscreen->vgt_hs_offchip_param =
789 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
790 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
791 } else {
792 assert(offchip_granularity == V_03093C_X_8K_DWORDS);
793 sscreen->vgt_hs_offchip_param =
794 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
795 }
796
797 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
798 * on SI. */
799 sscreen->has_clear_state = sscreen->info.chip_class >= CIK;
800
801 sscreen->has_distributed_tess =
802 sscreen->info.chip_class >= VI &&
803 sscreen->info.max_se >= 2;
804
805 sscreen->has_draw_indirect_multi =
806 (sscreen->info.family >= CHIP_POLARIS10) ||
807 (sscreen->info.chip_class == VI &&
808 sscreen->info.pfp_fw_version >= 121 &&
809 sscreen->info.me_fw_version >= 87) ||
810 (sscreen->info.chip_class == CIK &&
811 sscreen->info.pfp_fw_version >= 211 &&
812 sscreen->info.me_fw_version >= 173) ||
813 (sscreen->info.chip_class == SI &&
814 sscreen->info.pfp_fw_version >= 79 &&
815 sscreen->info.me_fw_version >= 142);
816
817 sscreen->has_out_of_order_rast = sscreen->info.chip_class >= VI &&
818 sscreen->info.max_se >= 2 &&
819 !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER));
820 sscreen->assume_no_z_fights =
821 driQueryOptionb(config->options, "radeonsi_assume_no_z_fights");
822 sscreen->commutative_blend_add =
823 driQueryOptionb(config->options, "radeonsi_commutative_blend_add");
824 sscreen->clear_db_cache_before_clear =
825 driQueryOptionb(config->options, "radeonsi_clear_db_cache_before_clear");
826 sscreen->has_msaa_sample_loc_bug = (sscreen->info.family >= CHIP_POLARIS10 &&
827 sscreen->info.family <= CHIP_POLARIS12) ||
828 sscreen->info.family == CHIP_VEGA10 ||
829 sscreen->info.family == CHIP_RAVEN;
830 sscreen->has_ls_vgpr_init_bug = sscreen->info.family == CHIP_VEGA10 ||
831 sscreen->info.family == CHIP_RAVEN;
832
833 if (sscreen->debug_flags & DBG(DPBB)) {
834 sscreen->dpbb_allowed = true;
835 } else {
836 /* Only enable primitive binning on Raven by default. */
837 /* TODO: Investigate if binning is profitable on Vega12. */
838 sscreen->dpbb_allowed = sscreen->info.family == CHIP_RAVEN &&
839 !(sscreen->debug_flags & DBG(NO_DPBB));
840 }
841
842 if (sscreen->debug_flags & DBG(DFSM)) {
843 sscreen->dfsm_allowed = sscreen->dpbb_allowed;
844 } else {
845 sscreen->dfsm_allowed = sscreen->dpbb_allowed &&
846 !(sscreen->debug_flags & DBG(NO_DFSM));
847 }
848
849 /* While it would be nice not to have this flag, we are constrained
850 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
851 * on GFX9.
852 */
853 sscreen->llvm_has_working_vgpr_indexing = sscreen->info.chip_class <= VI;
854
855 /* Some chips have RB+ registers, but don't support RB+. Those must
856 * always disable it.
857 */
858 if (sscreen->info.family == CHIP_STONEY ||
859 sscreen->info.chip_class >= GFX9) {
860 sscreen->has_rbplus = true;
861
862 sscreen->rbplus_allowed =
863 !(sscreen->debug_flags & DBG(NO_RB_PLUS)) &&
864 (sscreen->info.family == CHIP_STONEY ||
865 sscreen->info.family == CHIP_VEGA12 ||
866 sscreen->info.family == CHIP_RAVEN);
867 }
868
869 sscreen->dcc_msaa_allowed =
870 !(sscreen->debug_flags & DBG(NO_DCC_MSAA)) &&
871 (sscreen->debug_flags & DBG(DCC_MSAA) ||
872 sscreen->info.chip_class == VI);
873
874 sscreen->cpdma_prefetch_writes_memory = sscreen->info.chip_class <= VI;
875
876 (void) mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
877 sscreen->use_monolithic_shaders =
878 (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
879
880 sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SMEM_L1 |
881 SI_CONTEXT_INV_VMEM_L1;
882 if (sscreen->info.chip_class <= VI) {
883 sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_GLOBAL_L2;
884 sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
885 }
886
887 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
888 sscreen->debug_flags |= DBG_ALL_SHADERS;
889
890 for (i = 0; i < num_compiler_threads; i++)
891 sscreen->tm[i] = si_create_llvm_target_machine(sscreen);
892 for (i = 0; i < num_compiler_threads_lowprio; i++)
893 sscreen->tm_low_priority[i] = si_create_llvm_target_machine(sscreen);
894
895 /* Create the auxiliary context. This must be done last. */
896 sscreen->aux_context = si_create_context(&sscreen->b, 0);
897
898 if (sscreen->debug_flags & DBG(TEST_DMA))
899 si_test_dma(sscreen);
900
901 if (sscreen->debug_flags & (DBG(TEST_VMFAULT_CP) |
902 DBG(TEST_VMFAULT_SDMA) |
903 DBG(TEST_VMFAULT_SHADER)))
904 si_test_vmfault(sscreen);
905
906 return &sscreen->b;
907 }