r600: Add NIR compiler options
[mesa.git] / src / gallium / drivers / r600 / r600_pipe_common.c
1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors: Marek Olšák <maraeo@gmail.com>
24 *
25 */
26
27 #include "r600_pipe_common.h"
28 #include "r600_cs.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "compiler/nir/nir.h"
31 #include "util/list.h"
32 #include "util/u_draw_quad.h"
33 #include "util/u_memory.h"
34 #include "util/format/u_format_s3tc.h"
35 #include "util/u_upload_mgr.h"
36 #include "util/os_time.h"
37 #include "vl/vl_decoder.h"
38 #include "vl/vl_video_buffer.h"
39 #include "radeon_video.h"
40 #include <inttypes.h>
41 #include <sys/utsname.h>
42
43 #ifdef LLVM_AVAILABLE
44 #include <llvm-c/TargetMachine.h>
45 #endif
46
47 struct r600_multi_fence {
48 struct pipe_reference reference;
49 struct pipe_fence_handle *gfx;
50 struct pipe_fence_handle *sdma;
51
52 /* If the context wasn't flushed at fence creation, this is non-NULL. */
53 struct {
54 struct r600_common_context *ctx;
55 unsigned ib_index;
56 } gfx_unflushed;
57 };
58
59 /*
60 * pipe_context
61 */
62
63 /**
64 * Write an EOP event.
65 *
66 * \param event EVENT_TYPE_*
67 * \param event_flags Optional cache flush flags (TC)
68 * \param data_sel 1 = fence, 3 = timestamp
69 * \param buf Buffer
70 * \param va GPU address
71 * \param old_value Previous fence value (for a bug workaround)
72 * \param new_value Fence value to write for this event.
73 */
74 void r600_gfx_write_event_eop(struct r600_common_context *ctx,
75 unsigned event, unsigned event_flags,
76 unsigned data_sel,
77 struct r600_resource *buf, uint64_t va,
78 uint32_t new_fence, unsigned query_type)
79 {
80 struct radeon_cmdbuf *cs = ctx->gfx.cs;
81 unsigned op = EVENT_TYPE(event) |
82 EVENT_INDEX(5) |
83 event_flags;
84 unsigned sel = EOP_DATA_SEL(data_sel);
85
86 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
87 radeon_emit(cs, op);
88 radeon_emit(cs, va);
89 radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
90 radeon_emit(cs, new_fence); /* immediate data */
91 radeon_emit(cs, 0); /* unused */
92
93 if (buf)
94 r600_emit_reloc(ctx, &ctx->gfx, buf, RADEON_USAGE_WRITE,
95 RADEON_PRIO_QUERY);
96 }
97
98 unsigned r600_gfx_write_fence_dwords(struct r600_common_screen *screen)
99 {
100 unsigned dwords = 6;
101
102 if (!screen->info.r600_has_virtual_memory)
103 dwords += 2;
104
105 return dwords;
106 }
107
108 void r600_gfx_wait_fence(struct r600_common_context *ctx,
109 struct r600_resource *buf,
110 uint64_t va, uint32_t ref, uint32_t mask)
111 {
112 struct radeon_cmdbuf *cs = ctx->gfx.cs;
113
114 radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
115 radeon_emit(cs, WAIT_REG_MEM_EQUAL | WAIT_REG_MEM_MEM_SPACE(1));
116 radeon_emit(cs, va);
117 radeon_emit(cs, va >> 32);
118 radeon_emit(cs, ref); /* reference value */
119 radeon_emit(cs, mask); /* mask */
120 radeon_emit(cs, 4); /* poll interval */
121
122 if (buf)
123 r600_emit_reloc(ctx, &ctx->gfx, buf, RADEON_USAGE_READ,
124 RADEON_PRIO_QUERY);
125 }
126
127 void r600_draw_rectangle(struct blitter_context *blitter,
128 void *vertex_elements_cso,
129 blitter_get_vs_func get_vs,
130 int x1, int y1, int x2, int y2,
131 float depth, unsigned num_instances,
132 enum blitter_attrib_type type,
133 const union blitter_attrib *attrib)
134 {
135 struct r600_common_context *rctx =
136 (struct r600_common_context*)util_blitter_get_pipe(blitter);
137 struct pipe_viewport_state viewport;
138 struct pipe_resource *buf = NULL;
139 unsigned offset = 0;
140 float *vb;
141
142 rctx->b.bind_vertex_elements_state(&rctx->b, vertex_elements_cso);
143 rctx->b.bind_vs_state(&rctx->b, get_vs(blitter));
144
145 /* Some operations (like color resolve on r6xx) don't work
146 * with the conventional primitive types.
147 * One that works is PT_RECTLIST, which we use here. */
148
149 /* setup viewport */
150 viewport.scale[0] = 1.0f;
151 viewport.scale[1] = 1.0f;
152 viewport.scale[2] = 1.0f;
153 viewport.translate[0] = 0.0f;
154 viewport.translate[1] = 0.0f;
155 viewport.translate[2] = 0.0f;
156 rctx->b.set_viewport_states(&rctx->b, 0, 1, &viewport);
157
158 /* Upload vertices. The hw rectangle has only 3 vertices,
159 * The 4th one is derived from the first 3.
160 * The vertex specification should match u_blitter's vertex element state. */
161 u_upload_alloc(rctx->b.stream_uploader, 0, sizeof(float) * 24,
162 rctx->screen->info.tcc_cache_line_size,
163 &offset, &buf, (void**)&vb);
164 if (!buf)
165 return;
166
167 vb[0] = x1;
168 vb[1] = y1;
169 vb[2] = depth;
170 vb[3] = 1;
171
172 vb[8] = x1;
173 vb[9] = y2;
174 vb[10] = depth;
175 vb[11] = 1;
176
177 vb[16] = x2;
178 vb[17] = y1;
179 vb[18] = depth;
180 vb[19] = 1;
181
182 switch (type) {
183 case UTIL_BLITTER_ATTRIB_COLOR:
184 memcpy(vb+4, attrib->color, sizeof(float)*4);
185 memcpy(vb+12, attrib->color, sizeof(float)*4);
186 memcpy(vb+20, attrib->color, sizeof(float)*4);
187 break;
188 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
189 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
190 vb[6] = vb[14] = vb[22] = attrib->texcoord.z;
191 vb[7] = vb[15] = vb[23] = attrib->texcoord.w;
192 /* fall through */
193 vb[4] = attrib->texcoord.x1;
194 vb[5] = attrib->texcoord.y1;
195 vb[12] = attrib->texcoord.x1;
196 vb[13] = attrib->texcoord.y2;
197 vb[20] = attrib->texcoord.x2;
198 vb[21] = attrib->texcoord.y1;
199 break;
200 default:; /* Nothing to do. */
201 }
202
203 /* draw */
204 struct pipe_vertex_buffer vbuffer = {};
205 vbuffer.buffer.resource = buf;
206 vbuffer.stride = 2 * 4 * sizeof(float); /* vertex size */
207 vbuffer.buffer_offset = offset;
208
209 rctx->b.set_vertex_buffers(&rctx->b, blitter->vb_slot, 1, &vbuffer);
210 util_draw_arrays_instanced(&rctx->b, R600_PRIM_RECTANGLE_LIST, 0, 3,
211 0, num_instances);
212 pipe_resource_reference(&buf, NULL);
213 }
214
215 static void r600_dma_emit_wait_idle(struct r600_common_context *rctx)
216 {
217 struct radeon_cmdbuf *cs = rctx->dma.cs;
218
219 if (rctx->chip_class >= EVERGREEN)
220 radeon_emit(cs, 0xf0000000); /* NOP */
221 else {
222 /* TODO: R600-R700 should use the FENCE packet.
223 * CS checker support is required. */
224 }
225 }
226
227 void r600_need_dma_space(struct r600_common_context *ctx, unsigned num_dw,
228 struct r600_resource *dst, struct r600_resource *src)
229 {
230 uint64_t vram = ctx->dma.cs->used_vram;
231 uint64_t gtt = ctx->dma.cs->used_gart;
232
233 if (dst) {
234 vram += dst->vram_usage;
235 gtt += dst->gart_usage;
236 }
237 if (src) {
238 vram += src->vram_usage;
239 gtt += src->gart_usage;
240 }
241
242 /* Flush the GFX IB if DMA depends on it. */
243 if (radeon_emitted(ctx->gfx.cs, ctx->initial_gfx_cs_size) &&
244 ((dst &&
245 ctx->ws->cs_is_buffer_referenced(ctx->gfx.cs, dst->buf,
246 RADEON_USAGE_READWRITE)) ||
247 (src &&
248 ctx->ws->cs_is_buffer_referenced(ctx->gfx.cs, src->buf,
249 RADEON_USAGE_WRITE))))
250 ctx->gfx.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
251
252 /* Flush if there's not enough space, or if the memory usage per IB
253 * is too large.
254 *
255 * IBs using too little memory are limited by the IB submission overhead.
256 * IBs using too much memory are limited by the kernel/TTM overhead.
257 * Too long IBs create CPU-GPU pipeline bubbles and add latency.
258 *
259 * This heuristic makes sure that DMA requests are executed
260 * very soon after the call is made and lowers memory usage.
261 * It improves texture upload performance by keeping the DMA
262 * engine busy while uploads are being submitted.
263 */
264 num_dw++; /* for emit_wait_idle below */
265 if (!ctx->ws->cs_check_space(ctx->dma.cs, num_dw, false) ||
266 ctx->dma.cs->used_vram + ctx->dma.cs->used_gart > 64 * 1024 * 1024 ||
267 !radeon_cs_memory_below_limit(ctx->screen, ctx->dma.cs, vram, gtt)) {
268 ctx->dma.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
269 assert((num_dw + ctx->dma.cs->current.cdw) <= ctx->dma.cs->current.max_dw);
270 }
271
272 /* Wait for idle if either buffer has been used in the IB before to
273 * prevent read-after-write hazards.
274 */
275 if ((dst &&
276 ctx->ws->cs_is_buffer_referenced(ctx->dma.cs, dst->buf,
277 RADEON_USAGE_READWRITE)) ||
278 (src &&
279 ctx->ws->cs_is_buffer_referenced(ctx->dma.cs, src->buf,
280 RADEON_USAGE_WRITE)))
281 r600_dma_emit_wait_idle(ctx);
282
283 /* If GPUVM is not supported, the CS checker needs 2 entries
284 * in the buffer list per packet, which has to be done manually.
285 */
286 if (ctx->screen->info.r600_has_virtual_memory) {
287 if (dst)
288 radeon_add_to_buffer_list(ctx, &ctx->dma, dst,
289 RADEON_USAGE_WRITE, 0);
290 if (src)
291 radeon_add_to_buffer_list(ctx, &ctx->dma, src,
292 RADEON_USAGE_READ, 0);
293 }
294
295 /* this function is called before all DMA calls, so increment this. */
296 ctx->num_dma_calls++;
297 }
298
299 void r600_preflush_suspend_features(struct r600_common_context *ctx)
300 {
301 /* suspend queries */
302 if (!list_is_empty(&ctx->active_queries))
303 r600_suspend_queries(ctx);
304
305 ctx->streamout.suspended = false;
306 if (ctx->streamout.begin_emitted) {
307 r600_emit_streamout_end(ctx);
308 ctx->streamout.suspended = true;
309 }
310 }
311
312 void r600_postflush_resume_features(struct r600_common_context *ctx)
313 {
314 if (ctx->streamout.suspended) {
315 ctx->streamout.append_bitmask = ctx->streamout.enabled_mask;
316 r600_streamout_buffers_dirty(ctx);
317 }
318
319 /* resume queries */
320 if (!list_is_empty(&ctx->active_queries))
321 r600_resume_queries(ctx);
322 }
323
324 static void r600_fence_server_sync(struct pipe_context *ctx,
325 struct pipe_fence_handle *fence)
326 {
327 /* radeon synchronizes all rings by default and will not implement
328 * fence imports.
329 */
330 }
331
332 static void r600_flush_from_st(struct pipe_context *ctx,
333 struct pipe_fence_handle **fence,
334 unsigned flags)
335 {
336 struct pipe_screen *screen = ctx->screen;
337 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
338 struct radeon_winsys *ws = rctx->ws;
339 struct pipe_fence_handle *gfx_fence = NULL;
340 struct pipe_fence_handle *sdma_fence = NULL;
341 bool deferred_fence = false;
342 unsigned rflags = PIPE_FLUSH_ASYNC;
343
344 if (flags & PIPE_FLUSH_END_OF_FRAME)
345 rflags |= PIPE_FLUSH_END_OF_FRAME;
346
347 /* DMA IBs are preambles to gfx IBs, therefore must be flushed first. */
348 if (rctx->dma.cs)
349 rctx->dma.flush(rctx, rflags, fence ? &sdma_fence : NULL);
350
351 if (!radeon_emitted(rctx->gfx.cs, rctx->initial_gfx_cs_size)) {
352 if (fence)
353 ws->fence_reference(&gfx_fence, rctx->last_gfx_fence);
354 if (!(flags & PIPE_FLUSH_DEFERRED))
355 ws->cs_sync_flush(rctx->gfx.cs);
356 } else {
357 /* Instead of flushing, create a deferred fence. Constraints:
358 * - The state tracker must allow a deferred flush.
359 * - The state tracker must request a fence.
360 * Thread safety in fence_finish must be ensured by the state tracker.
361 */
362 if (flags & PIPE_FLUSH_DEFERRED && fence) {
363 gfx_fence = rctx->ws->cs_get_next_fence(rctx->gfx.cs);
364 deferred_fence = true;
365 } else {
366 rctx->gfx.flush(rctx, rflags, fence ? &gfx_fence : NULL);
367 }
368 }
369
370 /* Both engines can signal out of order, so we need to keep both fences. */
371 if (fence) {
372 struct r600_multi_fence *multi_fence =
373 CALLOC_STRUCT(r600_multi_fence);
374 if (!multi_fence) {
375 ws->fence_reference(&sdma_fence, NULL);
376 ws->fence_reference(&gfx_fence, NULL);
377 goto finish;
378 }
379
380 multi_fence->reference.count = 1;
381 /* If both fences are NULL, fence_finish will always return true. */
382 multi_fence->gfx = gfx_fence;
383 multi_fence->sdma = sdma_fence;
384
385 if (deferred_fence) {
386 multi_fence->gfx_unflushed.ctx = rctx;
387 multi_fence->gfx_unflushed.ib_index = rctx->num_gfx_cs_flushes;
388 }
389
390 screen->fence_reference(screen, fence, NULL);
391 *fence = (struct pipe_fence_handle*)multi_fence;
392 }
393 finish:
394 if (!(flags & PIPE_FLUSH_DEFERRED)) {
395 if (rctx->dma.cs)
396 ws->cs_sync_flush(rctx->dma.cs);
397 ws->cs_sync_flush(rctx->gfx.cs);
398 }
399 }
400
401 static void r600_flush_dma_ring(void *ctx, unsigned flags,
402 struct pipe_fence_handle **fence)
403 {
404 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
405 struct radeon_cmdbuf *cs = rctx->dma.cs;
406 struct radeon_saved_cs saved;
407 bool check_vm =
408 (rctx->screen->debug_flags & DBG_CHECK_VM) &&
409 rctx->check_vm_faults;
410
411 if (!radeon_emitted(cs, 0)) {
412 if (fence)
413 rctx->ws->fence_reference(fence, rctx->last_sdma_fence);
414 return;
415 }
416
417 if (check_vm)
418 radeon_save_cs(rctx->ws, cs, &saved, true);
419
420 rctx->ws->cs_flush(cs, flags, &rctx->last_sdma_fence);
421 if (fence)
422 rctx->ws->fence_reference(fence, rctx->last_sdma_fence);
423
424 if (check_vm) {
425 /* Use conservative timeout 800ms, after which we won't wait any
426 * longer and assume the GPU is hung.
427 */
428 rctx->ws->fence_wait(rctx->ws, rctx->last_sdma_fence, 800*1000*1000);
429
430 rctx->check_vm_faults(rctx, &saved, RING_DMA);
431 radeon_clear_saved_cs(&saved);
432 }
433 }
434
435 /**
436 * Store a linearized copy of all chunks of \p cs together with the buffer
437 * list in \p saved.
438 */
439 void radeon_save_cs(struct radeon_winsys *ws, struct radeon_cmdbuf *cs,
440 struct radeon_saved_cs *saved, bool get_buffer_list)
441 {
442 uint32_t *buf;
443 unsigned i;
444
445 /* Save the IB chunks. */
446 saved->num_dw = cs->prev_dw + cs->current.cdw;
447 saved->ib = MALLOC(4 * saved->num_dw);
448 if (!saved->ib)
449 goto oom;
450
451 buf = saved->ib;
452 for (i = 0; i < cs->num_prev; ++i) {
453 memcpy(buf, cs->prev[i].buf, cs->prev[i].cdw * 4);
454 buf += cs->prev[i].cdw;
455 }
456 memcpy(buf, cs->current.buf, cs->current.cdw * 4);
457
458 if (!get_buffer_list)
459 return;
460
461 /* Save the buffer list. */
462 saved->bo_count = ws->cs_get_buffer_list(cs, NULL);
463 saved->bo_list = CALLOC(saved->bo_count,
464 sizeof(saved->bo_list[0]));
465 if (!saved->bo_list) {
466 FREE(saved->ib);
467 goto oom;
468 }
469 ws->cs_get_buffer_list(cs, saved->bo_list);
470
471 return;
472
473 oom:
474 fprintf(stderr, "%s: out of memory\n", __func__);
475 memset(saved, 0, sizeof(*saved));
476 }
477
478 void radeon_clear_saved_cs(struct radeon_saved_cs *saved)
479 {
480 FREE(saved->ib);
481 FREE(saved->bo_list);
482
483 memset(saved, 0, sizeof(*saved));
484 }
485
486 static enum pipe_reset_status r600_get_reset_status(struct pipe_context *ctx)
487 {
488 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
489
490 return rctx->ws->ctx_query_reset_status(rctx->ctx);
491 }
492
493 static void r600_set_debug_callback(struct pipe_context *ctx,
494 const struct pipe_debug_callback *cb)
495 {
496 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
497
498 if (cb)
499 rctx->debug = *cb;
500 else
501 memset(&rctx->debug, 0, sizeof(rctx->debug));
502 }
503
504 static void r600_set_device_reset_callback(struct pipe_context *ctx,
505 const struct pipe_device_reset_callback *cb)
506 {
507 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
508
509 if (cb)
510 rctx->device_reset_callback = *cb;
511 else
512 memset(&rctx->device_reset_callback, 0,
513 sizeof(rctx->device_reset_callback));
514 }
515
516 bool r600_check_device_reset(struct r600_common_context *rctx)
517 {
518 enum pipe_reset_status status;
519
520 if (!rctx->device_reset_callback.reset)
521 return false;
522
523 if (!rctx->b.get_device_reset_status)
524 return false;
525
526 status = rctx->b.get_device_reset_status(&rctx->b);
527 if (status == PIPE_NO_RESET)
528 return false;
529
530 rctx->device_reset_callback.reset(rctx->device_reset_callback.data, status);
531 return true;
532 }
533
534 static void r600_dma_clear_buffer_fallback(struct pipe_context *ctx,
535 struct pipe_resource *dst,
536 uint64_t offset, uint64_t size,
537 unsigned value)
538 {
539 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
540
541 rctx->clear_buffer(ctx, dst, offset, size, value, R600_COHERENCY_NONE);
542 }
543
544 static bool r600_resource_commit(struct pipe_context *pctx,
545 struct pipe_resource *resource,
546 unsigned level, struct pipe_box *box,
547 bool commit)
548 {
549 struct r600_common_context *ctx = (struct r600_common_context *)pctx;
550 struct r600_resource *res = r600_resource(resource);
551
552 /*
553 * Since buffer commitment changes cannot be pipelined, we need to
554 * (a) flush any pending commands that refer to the buffer we're about
555 * to change, and
556 * (b) wait for threaded submit to finish, including those that were
557 * triggered by some other, earlier operation.
558 */
559 if (radeon_emitted(ctx->gfx.cs, ctx->initial_gfx_cs_size) &&
560 ctx->ws->cs_is_buffer_referenced(ctx->gfx.cs,
561 res->buf, RADEON_USAGE_READWRITE)) {
562 ctx->gfx.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
563 }
564 if (radeon_emitted(ctx->dma.cs, 0) &&
565 ctx->ws->cs_is_buffer_referenced(ctx->dma.cs,
566 res->buf, RADEON_USAGE_READWRITE)) {
567 ctx->dma.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
568 }
569
570 ctx->ws->cs_sync_flush(ctx->dma.cs);
571 ctx->ws->cs_sync_flush(ctx->gfx.cs);
572
573 assert(resource->target == PIPE_BUFFER);
574
575 return ctx->ws->buffer_commit(res->buf, box->x, box->width, commit);
576 }
577
578 bool r600_common_context_init(struct r600_common_context *rctx,
579 struct r600_common_screen *rscreen,
580 unsigned context_flags)
581 {
582 slab_create_child(&rctx->pool_transfers, &rscreen->pool_transfers);
583 slab_create_child(&rctx->pool_transfers_unsync, &rscreen->pool_transfers);
584
585 rctx->screen = rscreen;
586 rctx->ws = rscreen->ws;
587 rctx->family = rscreen->family;
588 rctx->chip_class = rscreen->chip_class;
589
590 rctx->b.invalidate_resource = r600_invalidate_resource;
591 rctx->b.resource_commit = r600_resource_commit;
592 rctx->b.transfer_map = u_transfer_map_vtbl;
593 rctx->b.transfer_flush_region = u_transfer_flush_region_vtbl;
594 rctx->b.transfer_unmap = u_transfer_unmap_vtbl;
595 rctx->b.texture_subdata = u_default_texture_subdata;
596 rctx->b.flush = r600_flush_from_st;
597 rctx->b.set_debug_callback = r600_set_debug_callback;
598 rctx->b.fence_server_sync = r600_fence_server_sync;
599 rctx->dma_clear_buffer = r600_dma_clear_buffer_fallback;
600
601 /* evergreen_compute.c has a special codepath for global buffers.
602 * Everything else can use the direct path.
603 */
604 if ((rscreen->chip_class == EVERGREEN || rscreen->chip_class == CAYMAN) &&
605 (context_flags & PIPE_CONTEXT_COMPUTE_ONLY))
606 rctx->b.buffer_subdata = u_default_buffer_subdata;
607 else
608 rctx->b.buffer_subdata = r600_buffer_subdata;
609
610 rctx->b.get_device_reset_status = r600_get_reset_status;
611 rctx->b.set_device_reset_callback = r600_set_device_reset_callback;
612
613 r600_init_context_texture_functions(rctx);
614 r600_init_viewport_functions(rctx);
615 r600_streamout_init(rctx);
616 r600_query_init(rctx);
617 cayman_init_msaa(&rctx->b);
618
619 rctx->allocator_zeroed_memory =
620 u_suballocator_create(&rctx->b, rscreen->info.gart_page_size,
621 0, PIPE_USAGE_DEFAULT, 0, true);
622 if (!rctx->allocator_zeroed_memory)
623 return false;
624
625 rctx->b.stream_uploader = u_upload_create(&rctx->b, 1024 * 1024,
626 0, PIPE_USAGE_STREAM, 0);
627 if (!rctx->b.stream_uploader)
628 return false;
629
630 rctx->b.const_uploader = u_upload_create(&rctx->b, 128 * 1024,
631 0, PIPE_USAGE_DEFAULT, 0);
632 if (!rctx->b.const_uploader)
633 return false;
634
635 rctx->ctx = rctx->ws->ctx_create(rctx->ws);
636 if (!rctx->ctx)
637 return false;
638
639 if (rscreen->info.num_rings[RING_DMA] && !(rscreen->debug_flags & DBG_NO_ASYNC_DMA)) {
640 rctx->dma.cs = rctx->ws->cs_create(rctx->ctx, RING_DMA,
641 r600_flush_dma_ring,
642 rctx, false);
643 rctx->dma.flush = r600_flush_dma_ring;
644 }
645
646 return true;
647 }
648
649 void r600_common_context_cleanup(struct r600_common_context *rctx)
650 {
651 if (rctx->query_result_shader)
652 rctx->b.delete_compute_state(&rctx->b, rctx->query_result_shader);
653
654 if (rctx->gfx.cs)
655 rctx->ws->cs_destroy(rctx->gfx.cs);
656 if (rctx->dma.cs)
657 rctx->ws->cs_destroy(rctx->dma.cs);
658 if (rctx->ctx)
659 rctx->ws->ctx_destroy(rctx->ctx);
660
661 if (rctx->b.stream_uploader)
662 u_upload_destroy(rctx->b.stream_uploader);
663 if (rctx->b.const_uploader)
664 u_upload_destroy(rctx->b.const_uploader);
665
666 slab_destroy_child(&rctx->pool_transfers);
667 slab_destroy_child(&rctx->pool_transfers_unsync);
668
669 if (rctx->allocator_zeroed_memory) {
670 u_suballocator_destroy(rctx->allocator_zeroed_memory);
671 }
672 rctx->ws->fence_reference(&rctx->last_gfx_fence, NULL);
673 rctx->ws->fence_reference(&rctx->last_sdma_fence, NULL);
674 r600_resource_reference(&rctx->eop_bug_scratch, NULL);
675 }
676
677 /*
678 * pipe_screen
679 */
680
681 static const struct debug_named_value common_debug_options[] = {
682 /* logging */
683 { "tex", DBG_TEX, "Print texture info" },
684 { "nir", DBG_NIR, "Enable experimental NIR shaders" },
685 { "compute", DBG_COMPUTE, "Print compute info" },
686 { "vm", DBG_VM, "Print virtual addresses when creating resources" },
687 { "info", DBG_INFO, "Print driver information" },
688
689 /* shaders */
690 { "fs", DBG_FS, "Print fetch shaders" },
691 { "vs", DBG_VS, "Print vertex shaders" },
692 { "gs", DBG_GS, "Print geometry shaders" },
693 { "ps", DBG_PS, "Print pixel shaders" },
694 { "cs", DBG_CS, "Print compute shaders" },
695 { "tcs", DBG_TCS, "Print tessellation control shaders" },
696 { "tes", DBG_TES, "Print tessellation evaluation shaders" },
697 { "noir", DBG_NO_IR, "Don't print the LLVM IR"},
698 { "notgsi", DBG_NO_TGSI, "Don't print the TGSI"},
699 { "noasm", DBG_NO_ASM, "Don't print disassembled shaders"},
700 { "preoptir", DBG_PREOPT_IR, "Print the LLVM IR before initial optimizations" },
701 { "checkir", DBG_CHECK_IR, "Enable additional sanity checks on shader IR" },
702 { "nooptvariant", DBG_NO_OPT_VARIANT, "Disable compiling optimized shader variants." },
703
704 { "testdma", DBG_TEST_DMA, "Invoke SDMA tests and exit." },
705 { "testvmfaultcp", DBG_TEST_VMFAULT_CP, "Invoke a CP VM fault test and exit." },
706 { "testvmfaultsdma", DBG_TEST_VMFAULT_SDMA, "Invoke a SDMA VM fault test and exit." },
707 { "testvmfaultshader", DBG_TEST_VMFAULT_SHADER, "Invoke a shader VM fault test and exit." },
708
709 /* features */
710 { "nodma", DBG_NO_ASYNC_DMA, "Disable asynchronous DMA" },
711 { "nohyperz", DBG_NO_HYPERZ, "Disable Hyper-Z" },
712 /* GL uses the word INVALIDATE, gallium uses the word DISCARD */
713 { "noinvalrange", DBG_NO_DISCARD_RANGE, "Disable handling of INVALIDATE_RANGE map flags" },
714 { "no2d", DBG_NO_2D_TILING, "Disable 2D tiling" },
715 { "notiling", DBG_NO_TILING, "Disable tiling" },
716 { "switch_on_eop", DBG_SWITCH_ON_EOP, "Program WD/IA to switch on end-of-packet." },
717 { "forcedma", DBG_FORCE_DMA, "Use asynchronous DMA for all operations when possible." },
718 { "precompile", DBG_PRECOMPILE, "Compile one shader variant at shader creation." },
719 { "nowc", DBG_NO_WC, "Disable GTT write combining" },
720 { "check_vm", DBG_CHECK_VM, "Check VM faults and dump debug info." },
721 { "unsafemath", DBG_UNSAFE_MATH, "Enable unsafe math shader optimizations" },
722
723 DEBUG_NAMED_VALUE_END /* must be last */
724 };
725
726 static const char* r600_get_vendor(struct pipe_screen* pscreen)
727 {
728 return "X.Org";
729 }
730
731 static const char* r600_get_device_vendor(struct pipe_screen* pscreen)
732 {
733 return "AMD";
734 }
735
736 static const char *r600_get_family_name(const struct r600_common_screen *rscreen)
737 {
738 switch (rscreen->info.family) {
739 case CHIP_R600: return "AMD R600";
740 case CHIP_RV610: return "AMD RV610";
741 case CHIP_RV630: return "AMD RV630";
742 case CHIP_RV670: return "AMD RV670";
743 case CHIP_RV620: return "AMD RV620";
744 case CHIP_RV635: return "AMD RV635";
745 case CHIP_RS780: return "AMD RS780";
746 case CHIP_RS880: return "AMD RS880";
747 case CHIP_RV770: return "AMD RV770";
748 case CHIP_RV730: return "AMD RV730";
749 case CHIP_RV710: return "AMD RV710";
750 case CHIP_RV740: return "AMD RV740";
751 case CHIP_CEDAR: return "AMD CEDAR";
752 case CHIP_REDWOOD: return "AMD REDWOOD";
753 case CHIP_JUNIPER: return "AMD JUNIPER";
754 case CHIP_CYPRESS: return "AMD CYPRESS";
755 case CHIP_HEMLOCK: return "AMD HEMLOCK";
756 case CHIP_PALM: return "AMD PALM";
757 case CHIP_SUMO: return "AMD SUMO";
758 case CHIP_SUMO2: return "AMD SUMO2";
759 case CHIP_BARTS: return "AMD BARTS";
760 case CHIP_TURKS: return "AMD TURKS";
761 case CHIP_CAICOS: return "AMD CAICOS";
762 case CHIP_CAYMAN: return "AMD CAYMAN";
763 case CHIP_ARUBA: return "AMD ARUBA";
764 default: return "AMD unknown";
765 }
766 }
767
768 static void r600_disk_cache_create(struct r600_common_screen *rscreen)
769 {
770 /* Don't use the cache if shader dumping is enabled. */
771 if (rscreen->debug_flags & DBG_ALL_SHADERS)
772 return;
773
774 struct mesa_sha1 ctx;
775 unsigned char sha1[20];
776 char cache_id[20 * 2 + 1];
777
778 _mesa_sha1_init(&ctx);
779 if (!disk_cache_get_function_identifier(r600_disk_cache_create,
780 &ctx))
781 return;
782
783 _mesa_sha1_final(&ctx, sha1);
784 disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
785
786 /* These flags affect shader compilation. */
787 uint64_t shader_debug_flags =
788 rscreen->debug_flags &
789 (DBG_FS_CORRECT_DERIVS_AFTER_KILL |
790 DBG_UNSAFE_MATH);
791
792 rscreen->disk_shader_cache =
793 disk_cache_create(r600_get_family_name(rscreen),
794 cache_id,
795 shader_debug_flags);
796 }
797
798 static struct disk_cache *r600_get_disk_shader_cache(struct pipe_screen *pscreen)
799 {
800 struct r600_common_screen *rscreen = (struct r600_common_screen*)pscreen;
801 return rscreen->disk_shader_cache;
802 }
803
804 static const char* r600_get_name(struct pipe_screen* pscreen)
805 {
806 struct r600_common_screen *rscreen = (struct r600_common_screen*)pscreen;
807
808 return rscreen->renderer_string;
809 }
810
811 static float r600_get_paramf(struct pipe_screen* pscreen,
812 enum pipe_capf param)
813 {
814 struct r600_common_screen *rscreen = (struct r600_common_screen *)pscreen;
815
816 switch (param) {
817 case PIPE_CAPF_MAX_LINE_WIDTH:
818 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
819 case PIPE_CAPF_MAX_POINT_WIDTH:
820 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
821 return 8191.0f;
822 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
823 return 16.0f;
824 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
825 return 16.0f;
826 case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
827 case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
828 case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
829 return 0.0f;
830 }
831 return 0.0f;
832 }
833
834 static int r600_get_video_param(struct pipe_screen *screen,
835 enum pipe_video_profile profile,
836 enum pipe_video_entrypoint entrypoint,
837 enum pipe_video_cap param)
838 {
839 switch (param) {
840 case PIPE_VIDEO_CAP_SUPPORTED:
841 return vl_profile_supported(screen, profile, entrypoint);
842 case PIPE_VIDEO_CAP_NPOT_TEXTURES:
843 return 1;
844 case PIPE_VIDEO_CAP_MAX_WIDTH:
845 case PIPE_VIDEO_CAP_MAX_HEIGHT:
846 return vl_video_buffer_max_size(screen);
847 case PIPE_VIDEO_CAP_PREFERED_FORMAT:
848 return PIPE_FORMAT_NV12;
849 case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
850 return false;
851 case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
852 return false;
853 case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
854 return true;
855 case PIPE_VIDEO_CAP_MAX_LEVEL:
856 return vl_level_supported(screen, profile);
857 default:
858 return 0;
859 }
860 }
861
862 const char *r600_get_llvm_processor_name(enum radeon_family family)
863 {
864 switch (family) {
865 case CHIP_R600:
866 case CHIP_RV630:
867 case CHIP_RV635:
868 case CHIP_RV670:
869 return "r600";
870 case CHIP_RV610:
871 case CHIP_RV620:
872 case CHIP_RS780:
873 case CHIP_RS880:
874 return "rs880";
875 case CHIP_RV710:
876 return "rv710";
877 case CHIP_RV730:
878 return "rv730";
879 case CHIP_RV740:
880 case CHIP_RV770:
881 return "rv770";
882 case CHIP_PALM:
883 case CHIP_CEDAR:
884 return "cedar";
885 case CHIP_SUMO:
886 case CHIP_SUMO2:
887 return "sumo";
888 case CHIP_REDWOOD:
889 return "redwood";
890 case CHIP_JUNIPER:
891 return "juniper";
892 case CHIP_HEMLOCK:
893 case CHIP_CYPRESS:
894 return "cypress";
895 case CHIP_BARTS:
896 return "barts";
897 case CHIP_TURKS:
898 return "turks";
899 case CHIP_CAICOS:
900 return "caicos";
901 case CHIP_CAYMAN:
902 case CHIP_ARUBA:
903 return "cayman";
904
905 default:
906 return "";
907 }
908 }
909
910 static unsigned get_max_threads_per_block(struct r600_common_screen *screen,
911 enum pipe_shader_ir ir_type)
912 {
913 if (ir_type != PIPE_SHADER_IR_TGSI)
914 return 256;
915 if (screen->chip_class >= EVERGREEN)
916 return 1024;
917 return 256;
918 }
919
920 static int r600_get_compute_param(struct pipe_screen *screen,
921 enum pipe_shader_ir ir_type,
922 enum pipe_compute_cap param,
923 void *ret)
924 {
925 struct r600_common_screen *rscreen = (struct r600_common_screen *)screen;
926
927 //TODO: select these params by asic
928 switch (param) {
929 case PIPE_COMPUTE_CAP_IR_TARGET: {
930 const char *gpu;
931 const char *triple = "r600--";
932 gpu = r600_get_llvm_processor_name(rscreen->family);
933 if (ret) {
934 sprintf(ret, "%s-%s", gpu, triple);
935 }
936 /* +2 for dash and terminating NIL byte */
937 return (strlen(triple) + strlen(gpu) + 2) * sizeof(char);
938 }
939 case PIPE_COMPUTE_CAP_GRID_DIMENSION:
940 if (ret) {
941 uint64_t *grid_dimension = ret;
942 grid_dimension[0] = 3;
943 }
944 return 1 * sizeof(uint64_t);
945
946 case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
947 if (ret) {
948 uint64_t *grid_size = ret;
949 grid_size[0] = 65535;
950 grid_size[1] = 65535;
951 grid_size[2] = 65535;
952 }
953 return 3 * sizeof(uint64_t) ;
954
955 case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
956 if (ret) {
957 uint64_t *block_size = ret;
958 unsigned threads_per_block = get_max_threads_per_block(rscreen, ir_type);
959 block_size[0] = threads_per_block;
960 block_size[1] = threads_per_block;
961 block_size[2] = threads_per_block;
962 }
963 return 3 * sizeof(uint64_t);
964
965 case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
966 if (ret) {
967 uint64_t *max_threads_per_block = ret;
968 *max_threads_per_block = get_max_threads_per_block(rscreen, ir_type);
969 }
970 return sizeof(uint64_t);
971 case PIPE_COMPUTE_CAP_ADDRESS_BITS:
972 if (ret) {
973 uint32_t *address_bits = ret;
974 address_bits[0] = 32;
975 }
976 return 1 * sizeof(uint32_t);
977
978 case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
979 if (ret) {
980 uint64_t *max_global_size = ret;
981 uint64_t max_mem_alloc_size;
982
983 r600_get_compute_param(screen, ir_type,
984 PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE,
985 &max_mem_alloc_size);
986
987 /* In OpenCL, the MAX_MEM_ALLOC_SIZE must be at least
988 * 1/4 of the MAX_GLOBAL_SIZE. Since the
989 * MAX_MEM_ALLOC_SIZE is fixed for older kernels,
990 * make sure we never report more than
991 * 4 * MAX_MEM_ALLOC_SIZE.
992 */
993 *max_global_size = MIN2(4 * max_mem_alloc_size,
994 MAX2(rscreen->info.gart_size,
995 rscreen->info.vram_size));
996 }
997 return sizeof(uint64_t);
998
999 case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
1000 if (ret) {
1001 uint64_t *max_local_size = ret;
1002 /* Value reported by the closed source driver. */
1003 *max_local_size = 32768;
1004 }
1005 return sizeof(uint64_t);
1006
1007 case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
1008 if (ret) {
1009 uint64_t *max_input_size = ret;
1010 /* Value reported by the closed source driver. */
1011 *max_input_size = 1024;
1012 }
1013 return sizeof(uint64_t);
1014
1015 case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
1016 if (ret) {
1017 uint64_t *max_mem_alloc_size = ret;
1018
1019 *max_mem_alloc_size = rscreen->info.max_alloc_size;
1020 }
1021 return sizeof(uint64_t);
1022
1023 case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
1024 if (ret) {
1025 uint32_t *max_clock_frequency = ret;
1026 *max_clock_frequency = rscreen->info.max_shader_clock;
1027 }
1028 return sizeof(uint32_t);
1029
1030 case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
1031 if (ret) {
1032 uint32_t *max_compute_units = ret;
1033 *max_compute_units = rscreen->info.num_good_compute_units;
1034 }
1035 return sizeof(uint32_t);
1036
1037 case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
1038 if (ret) {
1039 uint32_t *images_supported = ret;
1040 *images_supported = 0;
1041 }
1042 return sizeof(uint32_t);
1043 case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
1044 break; /* unused */
1045 case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
1046 if (ret) {
1047 uint32_t *subgroup_size = ret;
1048 *subgroup_size = r600_wavefront_size(rscreen->family);
1049 }
1050 return sizeof(uint32_t);
1051 case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
1052 if (ret) {
1053 uint64_t *max_variable_threads_per_block = ret;
1054 *max_variable_threads_per_block = 0;
1055 }
1056 return sizeof(uint64_t);
1057 }
1058
1059 fprintf(stderr, "unknown PIPE_COMPUTE_CAP %d\n", param);
1060 return 0;
1061 }
1062
1063 static uint64_t r600_get_timestamp(struct pipe_screen *screen)
1064 {
1065 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1066
1067 return 1000000 * rscreen->ws->query_value(rscreen->ws, RADEON_TIMESTAMP) /
1068 rscreen->info.clock_crystal_freq;
1069 }
1070
1071 static void r600_fence_reference(struct pipe_screen *screen,
1072 struct pipe_fence_handle **dst,
1073 struct pipe_fence_handle *src)
1074 {
1075 struct radeon_winsys *ws = ((struct r600_common_screen*)screen)->ws;
1076 struct r600_multi_fence **rdst = (struct r600_multi_fence **)dst;
1077 struct r600_multi_fence *rsrc = (struct r600_multi_fence *)src;
1078
1079 if (pipe_reference(&(*rdst)->reference, &rsrc->reference)) {
1080 ws->fence_reference(&(*rdst)->gfx, NULL);
1081 ws->fence_reference(&(*rdst)->sdma, NULL);
1082 FREE(*rdst);
1083 }
1084 *rdst = rsrc;
1085 }
1086
1087 static bool r600_fence_finish(struct pipe_screen *screen,
1088 struct pipe_context *ctx,
1089 struct pipe_fence_handle *fence,
1090 uint64_t timeout)
1091 {
1092 struct radeon_winsys *rws = ((struct r600_common_screen*)screen)->ws;
1093 struct r600_multi_fence *rfence = (struct r600_multi_fence *)fence;
1094 struct r600_common_context *rctx;
1095 int64_t abs_timeout = os_time_get_absolute_timeout(timeout);
1096
1097 ctx = threaded_context_unwrap_sync(ctx);
1098 rctx = ctx ? (struct r600_common_context*)ctx : NULL;
1099
1100 if (rfence->sdma) {
1101 if (!rws->fence_wait(rws, rfence->sdma, timeout))
1102 return false;
1103
1104 /* Recompute the timeout after waiting. */
1105 if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
1106 int64_t time = os_time_get_nano();
1107 timeout = abs_timeout > time ? abs_timeout - time : 0;
1108 }
1109 }
1110
1111 if (!rfence->gfx)
1112 return true;
1113
1114 /* Flush the gfx IB if it hasn't been flushed yet. */
1115 if (rctx &&
1116 rfence->gfx_unflushed.ctx == rctx &&
1117 rfence->gfx_unflushed.ib_index == rctx->num_gfx_cs_flushes) {
1118 rctx->gfx.flush(rctx, timeout ? 0 : PIPE_FLUSH_ASYNC, NULL);
1119 rfence->gfx_unflushed.ctx = NULL;
1120
1121 if (!timeout)
1122 return false;
1123
1124 /* Recompute the timeout after all that. */
1125 if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
1126 int64_t time = os_time_get_nano();
1127 timeout = abs_timeout > time ? abs_timeout - time : 0;
1128 }
1129 }
1130
1131 return rws->fence_wait(rws, rfence->gfx, timeout);
1132 }
1133
1134 static void r600_query_memory_info(struct pipe_screen *screen,
1135 struct pipe_memory_info *info)
1136 {
1137 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
1138 struct radeon_winsys *ws = rscreen->ws;
1139 unsigned vram_usage, gtt_usage;
1140
1141 info->total_device_memory = rscreen->info.vram_size / 1024;
1142 info->total_staging_memory = rscreen->info.gart_size / 1024;
1143
1144 /* The real TTM memory usage is somewhat random, because:
1145 *
1146 * 1) TTM delays freeing memory, because it can only free it after
1147 * fences expire.
1148 *
1149 * 2) The memory usage can be really low if big VRAM evictions are
1150 * taking place, but the real usage is well above the size of VRAM.
1151 *
1152 * Instead, return statistics of this process.
1153 */
1154 vram_usage = ws->query_value(ws, RADEON_REQUESTED_VRAM_MEMORY) / 1024;
1155 gtt_usage = ws->query_value(ws, RADEON_REQUESTED_GTT_MEMORY) / 1024;
1156
1157 info->avail_device_memory =
1158 vram_usage <= info->total_device_memory ?
1159 info->total_device_memory - vram_usage : 0;
1160 info->avail_staging_memory =
1161 gtt_usage <= info->total_staging_memory ?
1162 info->total_staging_memory - gtt_usage : 0;
1163
1164 info->device_memory_evicted =
1165 ws->query_value(ws, RADEON_NUM_BYTES_MOVED) / 1024;
1166
1167 /* Just return the number of evicted 64KB pages. */
1168 info->nr_device_memory_evictions = info->device_memory_evicted / 64;
1169 }
1170
1171 struct pipe_resource *r600_resource_create_common(struct pipe_screen *screen,
1172 const struct pipe_resource *templ)
1173 {
1174 if (templ->target == PIPE_BUFFER) {
1175 return r600_buffer_create(screen, templ, 256);
1176 } else {
1177 return r600_texture_create(screen, templ);
1178 }
1179 }
1180
1181 const struct nir_shader_compiler_options r600_nir_options = {
1182 .fuse_ffma = true,
1183 .lower_scmp = true,
1184 .lower_flrp32 = true,
1185 .lower_flrp64 = true,
1186 .lower_fpow = true,
1187 .lower_fdiv = true,
1188 .lower_idiv = true,
1189 .lower_fmod = true,
1190 .lower_doubles_options = nir_lower_fp64_full_software,
1191 .lower_int64_options = 0,
1192 .lower_extract_byte = true,
1193 .lower_extract_word = true,
1194 .max_unroll_iterations = 32,
1195 .lower_all_io_to_temps = true,
1196 .vectorize_io = true
1197 };
1198
1199 static const void *
1200 r600_get_compiler_options(struct pipe_screen *screen,
1201 enum pipe_shader_ir ir,
1202 enum pipe_shader_type shader)
1203 {
1204 assert(ir == PIPE_SHADER_IR_NIR);
1205 return &r600_nir_options;
1206 }
1207
1208 bool r600_common_screen_init(struct r600_common_screen *rscreen,
1209 struct radeon_winsys *ws)
1210 {
1211 char family_name[32] = {}, kernel_version[128] = {};
1212 struct utsname uname_data;
1213 const char *chip_name;
1214
1215 ws->query_info(ws, &rscreen->info);
1216 rscreen->ws = ws;
1217
1218 chip_name = r600_get_family_name(rscreen);
1219
1220 if (uname(&uname_data) == 0)
1221 snprintf(kernel_version, sizeof(kernel_version),
1222 " / %s", uname_data.release);
1223
1224 snprintf(rscreen->renderer_string, sizeof(rscreen->renderer_string),
1225 "%s (%sDRM %i.%i.%i%s"
1226 #ifdef LLVM_AVAILABLE
1227 ", LLVM " MESA_LLVM_VERSION_STRING
1228 #endif
1229 ")",
1230 chip_name, family_name, rscreen->info.drm_major,
1231 rscreen->info.drm_minor, rscreen->info.drm_patchlevel,
1232 kernel_version);
1233
1234 rscreen->b.get_name = r600_get_name;
1235 rscreen->b.get_vendor = r600_get_vendor;
1236 rscreen->b.get_device_vendor = r600_get_device_vendor;
1237 rscreen->b.get_disk_shader_cache = r600_get_disk_shader_cache;
1238 rscreen->b.get_compute_param = r600_get_compute_param;
1239 rscreen->b.get_paramf = r600_get_paramf;
1240 rscreen->b.get_timestamp = r600_get_timestamp;
1241 rscreen->b.get_compiler_options = r600_get_compiler_options;
1242 rscreen->b.fence_finish = r600_fence_finish;
1243 rscreen->b.fence_reference = r600_fence_reference;
1244 rscreen->b.resource_destroy = u_resource_destroy_vtbl;
1245 rscreen->b.resource_from_user_memory = r600_buffer_from_user_memory;
1246 rscreen->b.query_memory_info = r600_query_memory_info;
1247
1248 if (rscreen->info.has_hw_decode) {
1249 rscreen->b.get_video_param = rvid_get_video_param;
1250 rscreen->b.is_video_format_supported = rvid_is_format_supported;
1251 } else {
1252 rscreen->b.get_video_param = r600_get_video_param;
1253 rscreen->b.is_video_format_supported = vl_video_buffer_is_format_supported;
1254 }
1255
1256 r600_init_screen_texture_functions(rscreen);
1257 r600_init_screen_query_functions(rscreen);
1258
1259 rscreen->family = rscreen->info.family;
1260 rscreen->chip_class = rscreen->info.chip_class;
1261 rscreen->debug_flags |= debug_get_flags_option("R600_DEBUG", common_debug_options, 0);
1262
1263 r600_disk_cache_create(rscreen);
1264
1265 slab_create_parent(&rscreen->pool_transfers, sizeof(struct r600_transfer), 64);
1266
1267 rscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
1268 if (rscreen->force_aniso >= 0) {
1269 printf("radeon: Forcing anisotropy filter to %ix\n",
1270 /* round down to a power of two */
1271 1 << util_logbase2(rscreen->force_aniso));
1272 }
1273
1274 (void) mtx_init(&rscreen->aux_context_lock, mtx_plain);
1275 (void) mtx_init(&rscreen->gpu_load_mutex, mtx_plain);
1276
1277 if (rscreen->debug_flags & DBG_INFO) {
1278 printf("pci (domain:bus:dev.func): %04x:%02x:%02x.%x\n",
1279 rscreen->info.pci_domain, rscreen->info.pci_bus,
1280 rscreen->info.pci_dev, rscreen->info.pci_func);
1281 printf("pci_id = 0x%x\n", rscreen->info.pci_id);
1282 printf("family = %i (%s)\n", rscreen->info.family,
1283 r600_get_family_name(rscreen));
1284 printf("chip_class = %i\n", rscreen->info.chip_class);
1285 printf("pte_fragment_size = %u\n", rscreen->info.pte_fragment_size);
1286 printf("gart_page_size = %u\n", rscreen->info.gart_page_size);
1287 printf("gart_size = %i MB\n", (int)DIV_ROUND_UP(rscreen->info.gart_size, 1024*1024));
1288 printf("vram_size = %i MB\n", (int)DIV_ROUND_UP(rscreen->info.vram_size, 1024*1024));
1289 printf("vram_vis_size = %i MB\n", (int)DIV_ROUND_UP(rscreen->info.vram_vis_size, 1024*1024));
1290 printf("max_alloc_size = %i MB\n",
1291 (int)DIV_ROUND_UP(rscreen->info.max_alloc_size, 1024*1024));
1292 printf("min_alloc_size = %u\n", rscreen->info.min_alloc_size);
1293 printf("has_dedicated_vram = %u\n", rscreen->info.has_dedicated_vram);
1294 printf("r600_has_virtual_memory = %i\n", rscreen->info.r600_has_virtual_memory);
1295 printf("gfx_ib_pad_with_type2 = %i\n", rscreen->info.gfx_ib_pad_with_type2);
1296 printf("has_hw_decode = %u\n", rscreen->info.has_hw_decode);
1297 printf("num_rings[RING_DMA] = %i\n", rscreen->info.num_rings[RING_DMA]);
1298 printf("num_rings[RING_COMPUTE] = %u\n", rscreen->info.num_rings[RING_COMPUTE]);
1299 printf("uvd_fw_version = %u\n", rscreen->info.uvd_fw_version);
1300 printf("vce_fw_version = %u\n", rscreen->info.vce_fw_version);
1301 printf("me_fw_version = %i\n", rscreen->info.me_fw_version);
1302 printf("pfp_fw_version = %i\n", rscreen->info.pfp_fw_version);
1303 printf("ce_fw_version = %i\n", rscreen->info.ce_fw_version);
1304 printf("vce_harvest_config = %i\n", rscreen->info.vce_harvest_config);
1305 printf("clock_crystal_freq = %i\n", rscreen->info.clock_crystal_freq);
1306 printf("tcc_cache_line_size = %u\n", rscreen->info.tcc_cache_line_size);
1307 printf("drm = %i.%i.%i\n", rscreen->info.drm_major,
1308 rscreen->info.drm_minor, rscreen->info.drm_patchlevel);
1309 printf("has_userptr = %i\n", rscreen->info.has_userptr);
1310 printf("has_syncobj = %u\n", rscreen->info.has_syncobj);
1311
1312 printf("r600_max_quad_pipes = %i\n", rscreen->info.r600_max_quad_pipes);
1313 printf("max_shader_clock = %i\n", rscreen->info.max_shader_clock);
1314 printf("num_good_compute_units = %i\n", rscreen->info.num_good_compute_units);
1315 printf("max_se = %i\n", rscreen->info.max_se);
1316 printf("max_sh_per_se = %i\n", rscreen->info.max_sh_per_se);
1317
1318 printf("r600_gb_backend_map = %i\n", rscreen->info.r600_gb_backend_map);
1319 printf("r600_gb_backend_map_valid = %i\n", rscreen->info.r600_gb_backend_map_valid);
1320 printf("r600_num_banks = %i\n", rscreen->info.r600_num_banks);
1321 printf("num_render_backends = %i\n", rscreen->info.num_render_backends);
1322 printf("num_tile_pipes = %i\n", rscreen->info.num_tile_pipes);
1323 printf("pipe_interleave_bytes = %i\n", rscreen->info.pipe_interleave_bytes);
1324 printf("enabled_rb_mask = 0x%x\n", rscreen->info.enabled_rb_mask);
1325 printf("max_alignment = %u\n", (unsigned)rscreen->info.max_alignment);
1326 }
1327 return true;
1328 }
1329
1330 void r600_destroy_common_screen(struct r600_common_screen *rscreen)
1331 {
1332 r600_perfcounters_destroy(rscreen);
1333 r600_gpu_load_kill_thread(rscreen);
1334
1335 mtx_destroy(&rscreen->gpu_load_mutex);
1336 mtx_destroy(&rscreen->aux_context_lock);
1337 rscreen->aux_context->destroy(rscreen->aux_context);
1338
1339 slab_destroy_parent(&rscreen->pool_transfers);
1340
1341 disk_cache_destroy(rscreen->disk_shader_cache);
1342 rscreen->ws->destroy(rscreen->ws);
1343 FREE(rscreen);
1344 }
1345
1346 bool r600_can_dump_shader(struct r600_common_screen *rscreen,
1347 unsigned processor)
1348 {
1349 return rscreen->debug_flags & (1 << processor);
1350 }
1351
1352 bool r600_extra_shader_checks(struct r600_common_screen *rscreen, unsigned processor)
1353 {
1354 return (rscreen->debug_flags & DBG_CHECK_IR) ||
1355 r600_can_dump_shader(rscreen, processor);
1356 }
1357
1358 void r600_screen_clear_buffer(struct r600_common_screen *rscreen, struct pipe_resource *dst,
1359 uint64_t offset, uint64_t size, unsigned value)
1360 {
1361 struct r600_common_context *rctx = (struct r600_common_context*)rscreen->aux_context;
1362
1363 mtx_lock(&rscreen->aux_context_lock);
1364 rctx->dma_clear_buffer(&rctx->b, dst, offset, size, value);
1365 rscreen->aux_context->flush(rscreen->aux_context, NULL, 0);
1366 mtx_unlock(&rscreen->aux_context_lock);
1367 }