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