radeonsi: move/remove ac_shader_binary helpers
[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 /*
40 * pipe_context
41 */
42 static void si_destroy_context(struct pipe_context *context)
43 {
44 struct si_context *sctx = (struct si_context *)context;
45 int i;
46
47 /* Unreference the framebuffer normally to disable related logic
48 * properly.
49 */
50 struct pipe_framebuffer_state fb = {};
51 if (context->set_framebuffer_state)
52 context->set_framebuffer_state(context, &fb);
53
54 si_release_all_descriptors(sctx);
55
56 pipe_resource_reference(&sctx->esgs_ring, NULL);
57 pipe_resource_reference(&sctx->gsvs_ring, NULL);
58 pipe_resource_reference(&sctx->tf_ring, NULL);
59 pipe_resource_reference(&sctx->tess_offchip_ring, NULL);
60 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
61 r600_resource_reference(&sctx->border_color_buffer, NULL);
62 free(sctx->border_color_table);
63 r600_resource_reference(&sctx->scratch_buffer, NULL);
64 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
65 r600_resource_reference(&sctx->wait_mem_scratch, NULL);
66
67 si_pm4_free_state(sctx, sctx->init_config, ~0);
68 if (sctx->init_config_gs_rings)
69 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
70 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
71 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
72
73 if (sctx->fixed_func_tcs_shader.cso)
74 sctx->b.b.delete_tcs_state(&sctx->b.b, sctx->fixed_func_tcs_shader.cso);
75 if (sctx->custom_dsa_flush)
76 sctx->b.b.delete_depth_stencil_alpha_state(&sctx->b.b, sctx->custom_dsa_flush);
77 if (sctx->custom_blend_resolve)
78 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_resolve);
79 if (sctx->custom_blend_fmask_decompress)
80 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_fmask_decompress);
81 if (sctx->custom_blend_eliminate_fastclear)
82 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_eliminate_fastclear);
83 if (sctx->custom_blend_dcc_decompress)
84 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_dcc_decompress);
85 if (sctx->vs_blit_pos)
86 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos);
87 if (sctx->vs_blit_pos_layered)
88 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos_layered);
89 if (sctx->vs_blit_color)
90 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color);
91 if (sctx->vs_blit_color_layered)
92 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color_layered);
93 if (sctx->vs_blit_texcoord)
94 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_texcoord);
95
96 if (sctx->blitter)
97 util_blitter_destroy(sctx->blitter);
98
99 si_common_context_cleanup(&sctx->b);
100
101 LLVMDisposeTargetMachine(sctx->tm);
102
103 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
104
105 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
106 _mesa_hash_table_destroy(sctx->img_handles, NULL);
107
108 util_dynarray_fini(&sctx->resident_tex_handles);
109 util_dynarray_fini(&sctx->resident_img_handles);
110 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
111 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
112 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
113 FREE(sctx);
114 }
115
116 static enum pipe_reset_status
117 si_amdgpu_get_reset_status(struct pipe_context *ctx)
118 {
119 struct si_context *sctx = (struct si_context *)ctx;
120
121 return sctx->b.ws->ctx_query_reset_status(sctx->b.ctx);
122 }
123
124 /* Apitrace profiling:
125 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
126 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
127 * and remember its number.
128 * 3) In Mesa, enable queries and performance counters around that draw
129 * call and print the results.
130 * 4) glretrace --benchmark --markers ..
131 */
132 static void si_emit_string_marker(struct pipe_context *ctx,
133 const char *string, int len)
134 {
135 struct si_context *sctx = (struct si_context *)ctx;
136
137 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
138
139 if (sctx->b.log)
140 u_log_printf(sctx->b.log, "\nString marker: %*s\n", len, string);
141 }
142
143 static LLVMTargetMachineRef
144 si_create_llvm_target_machine(struct si_screen *sscreen)
145 {
146 enum ac_target_machine_options tm_options =
147 (sscreen->b.debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
148 (sscreen->b.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
149 (sscreen->b.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
150 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
151
152 return ac_create_target_machine(sscreen->b.family, tm_options);
153 }
154
155 static void si_set_debug_callback(struct pipe_context *ctx,
156 const struct pipe_debug_callback *cb)
157 {
158 struct si_context *sctx = (struct si_context *)ctx;
159 struct si_screen *screen = sctx->screen;
160
161 util_queue_finish(&screen->shader_compiler_queue);
162 util_queue_finish(&screen->shader_compiler_queue_low_priority);
163
164 if (cb)
165 sctx->debug = *cb;
166 else
167 memset(&sctx->debug, 0, sizeof(sctx->debug));
168 }
169
170 static void si_set_log_context(struct pipe_context *ctx,
171 struct u_log_context *log)
172 {
173 struct si_context *sctx = (struct si_context *)ctx;
174 sctx->b.log = log;
175
176 if (log)
177 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
178 }
179
180 static struct pipe_context *si_create_context(struct pipe_screen *screen,
181 unsigned flags)
182 {
183 struct si_context *sctx = CALLOC_STRUCT(si_context);
184 struct si_screen* sscreen = (struct si_screen *)screen;
185 struct radeon_winsys *ws = sscreen->b.ws;
186 int shader, i;
187
188 if (!sctx)
189 return NULL;
190
191 if (flags & PIPE_CONTEXT_DEBUG)
192 sscreen->record_llvm_ir = true; /* racy but not critical */
193
194 sctx->b.b.screen = screen; /* this must be set first */
195 sctx->b.b.priv = NULL;
196 sctx->b.b.destroy = si_destroy_context;
197 sctx->b.b.emit_string_marker = si_emit_string_marker;
198 sctx->b.b.set_debug_callback = si_set_debug_callback;
199 sctx->b.b.set_log_context = si_set_log_context;
200 sctx->b.set_atom_dirty = (void *)si_set_atom_dirty;
201 sctx->screen = sscreen; /* Easy accessing of screen/winsys. */
202 sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;
203
204 if (!si_common_context_init(&sctx->b, &sscreen->b, flags))
205 goto fail;
206
207 if (sscreen->b.info.drm_major == 3)
208 sctx->b.b.get_device_reset_status = si_amdgpu_get_reset_status;
209
210 si_init_clear_functions(sctx);
211 si_init_blit_functions(sctx);
212 si_init_compute_functions(sctx);
213 si_init_cp_dma_functions(sctx);
214 si_init_debug_functions(sctx);
215 si_init_msaa_functions(sctx);
216 si_init_streamout_functions(sctx);
217
218 if (sscreen->b.info.has_hw_decode) {
219 sctx->b.b.create_video_codec = si_uvd_create_decoder;
220 sctx->b.b.create_video_buffer = si_video_buffer_create;
221 } else {
222 sctx->b.b.create_video_codec = vl_create_decoder;
223 sctx->b.b.create_video_buffer = vl_video_buffer_create;
224 }
225
226 sctx->b.gfx.cs = ws->cs_create(sctx->b.ctx, RING_GFX,
227 si_context_gfx_flush, sctx);
228 sctx->b.gfx.flush = si_context_gfx_flush;
229
230 /* Border colors. */
231 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS *
232 sizeof(*sctx->border_color_table));
233 if (!sctx->border_color_table)
234 goto fail;
235
236 sctx->border_color_buffer = (struct r600_resource*)
237 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT,
238 SI_MAX_BORDER_COLORS *
239 sizeof(*sctx->border_color_table));
240 if (!sctx->border_color_buffer)
241 goto fail;
242
243 sctx->border_color_map =
244 ws->buffer_map(sctx->border_color_buffer->buf,
245 NULL, PIPE_TRANSFER_WRITE);
246 if (!sctx->border_color_map)
247 goto fail;
248
249 si_init_all_descriptors(sctx);
250 si_init_fence_functions(sctx);
251 si_init_state_functions(sctx);
252 si_init_shader_functions(sctx);
253 si_init_viewport_functions(sctx);
254 si_init_ia_multi_vgt_param_table(sctx);
255
256 if (sctx->b.chip_class >= CIK)
257 cik_init_sdma_functions(sctx);
258 else
259 si_init_dma_functions(sctx);
260
261 if (sscreen->b.debug_flags & DBG(FORCE_DMA))
262 sctx->b.b.resource_copy_region = sctx->b.dma_copy;
263
264 sctx->blitter = util_blitter_create(&sctx->b.b);
265 if (sctx->blitter == NULL)
266 goto fail;
267 sctx->blitter->draw_rectangle = si_draw_rectangle;
268 sctx->blitter->skip_viewport_restore = true;
269
270 sctx->sample_mask.sample_mask = 0xffff;
271
272 /* these must be last */
273 si_begin_new_cs(sctx);
274
275 if (sctx->b.chip_class >= GFX9) {
276 sctx->wait_mem_scratch = (struct r600_resource*)
277 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT, 4);
278 if (!sctx->wait_mem_scratch)
279 goto fail;
280
281 /* Initialize the memory. */
282 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
283 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
284 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
285 S_370_WR_CONFIRM(1) |
286 S_370_ENGINE_SEL(V_370_ME));
287 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address);
288 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address >> 32);
289 radeon_emit(cs, sctx->wait_mem_number);
290 }
291
292 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
293 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
294 if (sctx->b.chip_class == CIK) {
295 sctx->null_const_buf.buffer =
296 si_aligned_buffer_create(screen,
297 R600_RESOURCE_FLAG_UNMAPPABLE,
298 PIPE_USAGE_DEFAULT, 16,
299 sctx->screen->b.info.tcc_cache_line_size);
300 if (!sctx->null_const_buf.buffer)
301 goto fail;
302 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
303
304 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
305 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
306 sctx->b.b.set_constant_buffer(&sctx->b.b, shader, i,
307 &sctx->null_const_buf);
308 }
309 }
310
311 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS,
312 &sctx->null_const_buf);
313 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS,
314 &sctx->null_const_buf);
315 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES,
316 &sctx->null_const_buf);
317 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE,
318 &sctx->null_const_buf);
319 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS,
320 &sctx->null_const_buf);
321
322 /* Clear the NULL constant buffer, because loads should return zeros. */
323 si_clear_buffer(&sctx->b.b, sctx->null_const_buf.buffer, 0,
324 sctx->null_const_buf.buffer->width0, 0,
325 R600_COHERENCY_SHADER);
326 }
327
328 uint64_t max_threads_per_block;
329 screen->get_compute_param(screen, PIPE_SHADER_IR_TGSI,
330 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
331 &max_threads_per_block);
332
333 /* The maximum number of scratch waves. Scratch space isn't divided
334 * evenly between CUs. The number is only a function of the number of CUs.
335 * We can decrease the constant to decrease the scratch buffer size.
336 *
337 * sctx->scratch_waves must be >= the maximum posible size of
338 * 1 threadgroup, so that the hw doesn't hang from being unable
339 * to start any.
340 *
341 * The recommended value is 4 per CU at most. Higher numbers don't
342 * bring much benefit, but they still occupy chip resources (think
343 * async compute). I've seen ~2% performance difference between 4 and 32.
344 */
345 sctx->scratch_waves = MAX2(32 * sscreen->b.info.num_good_compute_units,
346 max_threads_per_block / 64);
347
348 sctx->tm = si_create_llvm_target_machine(sscreen);
349
350 /* Bindless handles. */
351 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
352 _mesa_key_pointer_equal);
353 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
354 _mesa_key_pointer_equal);
355
356 util_dynarray_init(&sctx->resident_tex_handles, NULL);
357 util_dynarray_init(&sctx->resident_img_handles, NULL);
358 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
359 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
360 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
361
362 return &sctx->b.b;
363 fail:
364 fprintf(stderr, "radeonsi: Failed to create a context.\n");
365 si_destroy_context(&sctx->b.b);
366 return NULL;
367 }
368
369 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen,
370 void *priv, unsigned flags)
371 {
372 struct si_screen *sscreen = (struct si_screen *)screen;
373 struct pipe_context *ctx;
374
375 if (sscreen->b.debug_flags & DBG(CHECK_VM))
376 flags |= PIPE_CONTEXT_DEBUG;
377
378 ctx = si_create_context(screen, flags);
379
380 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
381 return ctx;
382
383 /* Clover (compute-only) is unsupported. */
384 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
385 return ctx;
386
387 /* When shaders are logged to stderr, asynchronous compilation is
388 * disabled too. */
389 if (sscreen->b.debug_flags & DBG_ALL_SHADERS)
390 return ctx;
391
392 /* Use asynchronous flushes only on amdgpu, since the radeon
393 * implementation for fence_server_sync is incomplete. */
394 return threaded_context_create(ctx, &sscreen->b.pool_transfers,
395 si_replace_buffer_storage,
396 sscreen->b.info.drm_major >= 3 ? si_create_fence : NULL,
397 &((struct si_context*)ctx)->b.tc);
398 }
399
400 /*
401 * pipe_screen
402 */
403
404 static void si_destroy_screen(struct pipe_screen* pscreen)
405 {
406 struct si_screen *sscreen = (struct si_screen *)pscreen;
407 struct si_shader_part *parts[] = {
408 sscreen->vs_prologs,
409 sscreen->tcs_epilogs,
410 sscreen->gs_prologs,
411 sscreen->ps_prologs,
412 sscreen->ps_epilogs
413 };
414 unsigned i;
415
416 if (!sscreen->b.ws->unref(sscreen->b.ws))
417 return;
418
419 util_queue_destroy(&sscreen->shader_compiler_queue);
420 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
421
422 for (i = 0; i < ARRAY_SIZE(sscreen->tm); i++)
423 if (sscreen->tm[i])
424 LLVMDisposeTargetMachine(sscreen->tm[i]);
425
426 for (i = 0; i < ARRAY_SIZE(sscreen->tm_low_priority); i++)
427 if (sscreen->tm_low_priority[i])
428 LLVMDisposeTargetMachine(sscreen->tm_low_priority[i]);
429
430 /* Free shader parts. */
431 for (i = 0; i < ARRAY_SIZE(parts); i++) {
432 while (parts[i]) {
433 struct si_shader_part *part = parts[i];
434
435 parts[i] = part->next;
436 ac_shader_binary_clean(&part->binary);
437 FREE(part);
438 }
439 }
440 mtx_destroy(&sscreen->shader_parts_mutex);
441 si_destroy_shader_cache(sscreen);
442 si_destroy_common_screen(&sscreen->b);
443 }
444
445 static bool si_init_gs_info(struct si_screen *sscreen)
446 {
447 /* gs_table_depth is not used by GFX9 */
448 if (sscreen->b.chip_class >= GFX9)
449 return true;
450
451 switch (sscreen->b.family) {
452 case CHIP_OLAND:
453 case CHIP_HAINAN:
454 case CHIP_KAVERI:
455 case CHIP_KABINI:
456 case CHIP_MULLINS:
457 case CHIP_ICELAND:
458 case CHIP_CARRIZO:
459 case CHIP_STONEY:
460 sscreen->gs_table_depth = 16;
461 return true;
462 case CHIP_TAHITI:
463 case CHIP_PITCAIRN:
464 case CHIP_VERDE:
465 case CHIP_BONAIRE:
466 case CHIP_HAWAII:
467 case CHIP_TONGA:
468 case CHIP_FIJI:
469 case CHIP_POLARIS10:
470 case CHIP_POLARIS11:
471 case CHIP_POLARIS12:
472 sscreen->gs_table_depth = 32;
473 return true;
474 default:
475 return false;
476 }
477 }
478
479 static void si_handle_env_var_force_family(struct si_screen *sscreen)
480 {
481 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
482 unsigned i;
483
484 if (!family)
485 return;
486
487 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
488 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
489 /* Override family and chip_class. */
490 sscreen->b.family = sscreen->b.info.family = i;
491
492 if (i >= CHIP_VEGA10)
493 sscreen->b.chip_class = sscreen->b.info.chip_class = GFX9;
494 else if (i >= CHIP_TONGA)
495 sscreen->b.chip_class = sscreen->b.info.chip_class = VI;
496 else if (i >= CHIP_BONAIRE)
497 sscreen->b.chip_class = sscreen->b.info.chip_class = CIK;
498 else
499 sscreen->b.chip_class = sscreen->b.info.chip_class = SI;
500
501 /* Don't submit any IBs. */
502 setenv("RADEON_NOOP", "1", 1);
503 return;
504 }
505 }
506
507 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
508 exit(1);
509 }
510
511 static void si_test_vmfault(struct si_screen *sscreen)
512 {
513 struct pipe_context *ctx = sscreen->b.aux_context;
514 struct si_context *sctx = (struct si_context *)ctx;
515 struct pipe_resource *buf =
516 pipe_buffer_create(&sscreen->b.b, 0, PIPE_USAGE_DEFAULT, 64);
517
518 if (!buf) {
519 puts("Buffer allocation failed.");
520 exit(1);
521 }
522
523 r600_resource(buf)->gpu_address = 0; /* cause a VM fault */
524
525 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_CP)) {
526 si_copy_buffer(sctx, buf, buf, 0, 4, 4, 0);
527 ctx->flush(ctx, NULL, 0);
528 puts("VM fault test: CP - done.");
529 }
530 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_SDMA)) {
531 sctx->b.dma_clear_buffer(ctx, buf, 0, 4, 0);
532 ctx->flush(ctx, NULL, 0);
533 puts("VM fault test: SDMA - done.");
534 }
535 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_SHADER)) {
536 util_test_constant_buffer(ctx, buf);
537 puts("VM fault test: Shader - done.");
538 }
539 exit(0);
540 }
541
542 static void si_disk_cache_create(struct si_screen *sscreen)
543 {
544 /* Don't use the cache if shader dumping is enabled. */
545 if (sscreen->b.debug_flags & DBG_ALL_SHADERS)
546 return;
547
548 /* TODO: remove this once gallium supports a nir cache */
549 if (sscreen->b.debug_flags & DBG(NIR))
550 return;
551
552 uint32_t mesa_timestamp;
553 if (disk_cache_get_function_timestamp(si_disk_cache_create,
554 &mesa_timestamp)) {
555 char *timestamp_str;
556 int res = -1;
557 uint32_t llvm_timestamp;
558
559 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo,
560 &llvm_timestamp)) {
561 res = asprintf(&timestamp_str, "%u_%u",
562 mesa_timestamp, llvm_timestamp);
563 }
564
565 if (res != -1) {
566 /* These flags affect shader compilation. */
567 uint64_t shader_debug_flags =
568 sscreen->b.debug_flags &
569 (DBG(FS_CORRECT_DERIVS_AFTER_KILL) |
570 DBG(SI_SCHED) |
571 DBG(UNSAFE_MATH));
572
573 sscreen->b.disk_shader_cache =
574 disk_cache_create(si_get_family_name(sscreen),
575 timestamp_str,
576 shader_debug_flags);
577 free(timestamp_str);
578 }
579 }
580 }
581
582 struct pipe_screen *radeonsi_screen_create(struct radeon_winsys *ws,
583 const struct pipe_screen_config *config)
584 {
585 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
586 unsigned num_threads, num_compiler_threads, num_compiler_threads_lowprio, i;
587
588 if (!sscreen) {
589 return NULL;
590 }
591
592 sscreen->b.ws = ws;
593 ws->query_info(ws, &sscreen->b.info);
594
595 sscreen->b.family = sscreen->b.info.family;
596 sscreen->b.chip_class = sscreen->b.info.chip_class;
597
598 /* Set functions first. */
599 sscreen->b.b.context_create = si_pipe_create_context;
600 sscreen->b.b.destroy = si_destroy_screen;
601 sscreen->b.b.resource_create = si_resource_create_common;
602
603 si_init_screen_get_functions(sscreen);
604 si_init_screen_fence_functions(sscreen);
605 si_init_screen_state_functions(sscreen);
606
607 /* Set these flags in debug_flags early, so that the shader cache takes
608 * them into account.
609 */
610 if (driQueryOptionb(config->options,
611 "glsl_correct_derivatives_after_discard"))
612 sscreen->b.debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
613 if (driQueryOptionb(config->options, "radeonsi_enable_sisched"))
614 sscreen->b.debug_flags |= DBG(SI_SCHED);
615
616 if (!si_common_screen_init(&sscreen->b, ws) ||
617 !si_init_gs_info(sscreen) ||
618 !si_init_shader_cache(sscreen)) {
619 FREE(sscreen);
620 return NULL;
621 }
622
623 si_disk_cache_create(sscreen);
624
625 /* Only enable as many threads as we have target machines, but at most
626 * the number of CPUs - 1 if there is more than one.
627 */
628 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
629 num_threads = MAX2(1, num_threads - 1);
630 num_compiler_threads = MIN2(num_threads, ARRAY_SIZE(sscreen->tm));
631 num_compiler_threads_lowprio =
632 MIN2(num_threads, ARRAY_SIZE(sscreen->tm_low_priority));
633
634 if (!util_queue_init(&sscreen->shader_compiler_queue, "si_shader",
635 32, num_compiler_threads,
636 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
637 si_destroy_shader_cache(sscreen);
638 FREE(sscreen);
639 return NULL;
640 }
641
642 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority,
643 "si_shader_low",
644 32, num_compiler_threads_lowprio,
645 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
646 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
647 si_destroy_shader_cache(sscreen);
648 FREE(sscreen);
649 return NULL;
650 }
651
652 si_handle_env_var_force_family(sscreen);
653
654 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
655 si_init_perfcounters(sscreen);
656
657 /* Hawaii has a bug with offchip buffers > 256 that can be worked
658 * around by setting 4K granularity.
659 */
660 sscreen->tess_offchip_block_dw_size =
661 sscreen->b.family == CHIP_HAWAII ? 4096 : 8192;
662
663 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
664 * on SI. */
665 sscreen->has_clear_state = sscreen->b.chip_class >= CIK;
666
667 sscreen->has_distributed_tess =
668 sscreen->b.chip_class >= VI &&
669 sscreen->b.info.max_se >= 2;
670
671 sscreen->has_draw_indirect_multi =
672 (sscreen->b.family >= CHIP_POLARIS10) ||
673 (sscreen->b.chip_class == VI &&
674 sscreen->b.info.pfp_fw_version >= 121 &&
675 sscreen->b.info.me_fw_version >= 87) ||
676 (sscreen->b.chip_class == CIK &&
677 sscreen->b.info.pfp_fw_version >= 211 &&
678 sscreen->b.info.me_fw_version >= 173) ||
679 (sscreen->b.chip_class == SI &&
680 sscreen->b.info.pfp_fw_version >= 79 &&
681 sscreen->b.info.me_fw_version >= 142);
682
683 sscreen->has_out_of_order_rast = sscreen->b.chip_class >= VI &&
684 sscreen->b.info.max_se >= 2 &&
685 !(sscreen->b.debug_flags & DBG(NO_OUT_OF_ORDER));
686 sscreen->assume_no_z_fights =
687 driQueryOptionb(config->options, "radeonsi_assume_no_z_fights");
688 sscreen->commutative_blend_add =
689 driQueryOptionb(config->options, "radeonsi_commutative_blend_add");
690 sscreen->clear_db_cache_before_clear =
691 driQueryOptionb(config->options, "radeonsi_clear_db_cache_before_clear");
692 sscreen->has_msaa_sample_loc_bug = (sscreen->b.family >= CHIP_POLARIS10 &&
693 sscreen->b.family <= CHIP_POLARIS12) ||
694 sscreen->b.family == CHIP_VEGA10 ||
695 sscreen->b.family == CHIP_RAVEN;
696 sscreen->has_ls_vgpr_init_bug = sscreen->b.family == CHIP_VEGA10 ||
697 sscreen->b.family == CHIP_RAVEN;
698
699 if (sscreen->b.debug_flags & DBG(DPBB)) {
700 sscreen->dpbb_allowed = true;
701 } else {
702 /* Only enable primitive binning on Raven by default. */
703 sscreen->dpbb_allowed = sscreen->b.family == CHIP_RAVEN &&
704 !(sscreen->b.debug_flags & DBG(NO_DPBB));
705 }
706
707 if (sscreen->b.debug_flags & DBG(DFSM)) {
708 sscreen->dfsm_allowed = sscreen->dpbb_allowed;
709 } else {
710 sscreen->dfsm_allowed = sscreen->dpbb_allowed &&
711 !(sscreen->b.debug_flags & DBG(NO_DFSM));
712 }
713
714 /* While it would be nice not to have this flag, we are constrained
715 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
716 * on GFX9.
717 */
718 sscreen->llvm_has_working_vgpr_indexing = sscreen->b.chip_class <= VI;
719
720 /* Some chips have RB+ registers, but don't support RB+. Those must
721 * always disable it.
722 */
723 if (sscreen->b.family == CHIP_STONEY ||
724 sscreen->b.chip_class >= GFX9) {
725 sscreen->b.has_rbplus = true;
726
727 sscreen->b.rbplus_allowed =
728 !(sscreen->b.debug_flags & DBG(NO_RB_PLUS)) &&
729 (sscreen->b.family == CHIP_STONEY ||
730 sscreen->b.family == CHIP_RAVEN);
731 }
732
733 sscreen->b.dcc_msaa_allowed =
734 !(sscreen->b.debug_flags & DBG(NO_DCC_MSAA)) &&
735 (sscreen->b.debug_flags & DBG(DCC_MSAA) ||
736 sscreen->b.chip_class == VI);
737
738 (void) mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
739 sscreen->use_monolithic_shaders =
740 (sscreen->b.debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
741
742 sscreen->b.barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SMEM_L1 |
743 SI_CONTEXT_INV_VMEM_L1;
744 if (sscreen->b.chip_class <= VI) {
745 sscreen->b.barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_GLOBAL_L2;
746 sscreen->b.barrier_flags.L2_to_cp |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
747 }
748
749 sscreen->b.barrier_flags.compute_to_L2 = SI_CONTEXT_CS_PARTIAL_FLUSH;
750
751 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
752 sscreen->b.debug_flags |= DBG_ALL_SHADERS;
753
754 for (i = 0; i < num_compiler_threads; i++)
755 sscreen->tm[i] = si_create_llvm_target_machine(sscreen);
756 for (i = 0; i < num_compiler_threads_lowprio; i++)
757 sscreen->tm_low_priority[i] = si_create_llvm_target_machine(sscreen);
758
759 /* Create the auxiliary context. This must be done last. */
760 sscreen->b.aux_context = si_create_context(&sscreen->b.b, 0);
761
762 if (sscreen->b.debug_flags & DBG(TEST_DMA))
763 si_test_dma(sscreen);
764
765 if (sscreen->b.debug_flags & (DBG(TEST_VMFAULT_CP) |
766 DBG(TEST_VMFAULT_SDMA) |
767 DBG(TEST_VMFAULT_SHADER)))
768 si_test_vmfault(sscreen);
769
770 return &sscreen->b.b;
771 }