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