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