r600g,radeonsi: add debug option which forces DMA for copy_region and blit
[mesa.git] / src / gallium / drivers / radeon / 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/u_draw_quad.h"
31 #include "util/u_memory.h"
32 #include "util/u_format_s3tc.h"
33 #include "util/u_upload_mgr.h"
34 #include "vl/vl_decoder.h"
35 #include "vl/vl_video_buffer.h"
36 #include "radeon/radeon_video.h"
37 #include <inttypes.h>
38
39 /*
40 * pipe_context
41 */
42
43 void r600_draw_rectangle(struct blitter_context *blitter,
44 int x1, int y1, int x2, int y2, float depth,
45 enum blitter_attrib_type type,
46 const union pipe_color_union *attrib)
47 {
48 struct r600_common_context *rctx =
49 (struct r600_common_context*)util_blitter_get_pipe(blitter);
50 struct pipe_viewport_state viewport;
51 struct pipe_resource *buf = NULL;
52 unsigned offset = 0;
53 float *vb;
54
55 if (type == UTIL_BLITTER_ATTRIB_TEXCOORD) {
56 util_blitter_draw_rectangle(blitter, x1, y1, x2, y2, depth, type, attrib);
57 return;
58 }
59
60 /* Some operations (like color resolve on r6xx) don't work
61 * with the conventional primitive types.
62 * One that works is PT_RECTLIST, which we use here. */
63
64 /* setup viewport */
65 viewport.scale[0] = 1.0f;
66 viewport.scale[1] = 1.0f;
67 viewport.scale[2] = 1.0f;
68 viewport.scale[3] = 1.0f;
69 viewport.translate[0] = 0.0f;
70 viewport.translate[1] = 0.0f;
71 viewport.translate[2] = 0.0f;
72 viewport.translate[3] = 0.0f;
73 rctx->b.set_viewport_states(&rctx->b, 0, 1, &viewport);
74
75 /* Upload vertices. The hw rectangle has only 3 vertices,
76 * I guess the 4th one is derived from the first 3.
77 * The vertex specification should match u_blitter's vertex element state. */
78 u_upload_alloc(rctx->uploader, 0, sizeof(float) * 24, &offset, &buf, (void**)&vb);
79 vb[0] = x1;
80 vb[1] = y1;
81 vb[2] = depth;
82 vb[3] = 1;
83
84 vb[8] = x1;
85 vb[9] = y2;
86 vb[10] = depth;
87 vb[11] = 1;
88
89 vb[16] = x2;
90 vb[17] = y1;
91 vb[18] = depth;
92 vb[19] = 1;
93
94 if (attrib) {
95 memcpy(vb+4, attrib->f, sizeof(float)*4);
96 memcpy(vb+12, attrib->f, sizeof(float)*4);
97 memcpy(vb+20, attrib->f, sizeof(float)*4);
98 }
99
100 /* draw */
101 util_draw_vertex_buffer(&rctx->b, NULL, buf, blitter->vb_slot, offset,
102 R600_PRIM_RECTANGLE_LIST, 3, 2);
103 pipe_resource_reference(&buf, NULL);
104 }
105
106 void r600_need_dma_space(struct r600_common_context *ctx, unsigned num_dw)
107 {
108 /* The number of dwords we already used in the DMA so far. */
109 num_dw += ctx->rings.dma.cs->cdw;
110 /* Flush if there's not enough space. */
111 if (num_dw > RADEON_MAX_CMDBUF_DWORDS) {
112 ctx->rings.dma.flush(ctx, RADEON_FLUSH_ASYNC, NULL);
113 }
114 }
115
116 static void r600_memory_barrier(struct pipe_context *ctx, unsigned flags)
117 {
118 }
119
120 void r600_preflush_suspend_features(struct r600_common_context *ctx)
121 {
122 /* Disable render condition. */
123 ctx->saved_render_cond = NULL;
124 ctx->saved_render_cond_cond = FALSE;
125 ctx->saved_render_cond_mode = 0;
126 if (ctx->current_render_cond) {
127 ctx->saved_render_cond = ctx->current_render_cond;
128 ctx->saved_render_cond_cond = ctx->current_render_cond_cond;
129 ctx->saved_render_cond_mode = ctx->current_render_cond_mode;
130 ctx->b.render_condition(&ctx->b, NULL, FALSE, 0);
131 }
132
133 /* suspend queries */
134 ctx->nontimer_queries_suspended = false;
135 if (ctx->num_cs_dw_nontimer_queries_suspend) {
136 r600_suspend_nontimer_queries(ctx);
137 ctx->nontimer_queries_suspended = true;
138 }
139
140 ctx->streamout.suspended = false;
141 if (ctx->streamout.begin_emitted) {
142 r600_emit_streamout_end(ctx);
143 ctx->streamout.suspended = true;
144 }
145 }
146
147 void r600_postflush_resume_features(struct r600_common_context *ctx)
148 {
149 if (ctx->streamout.suspended) {
150 ctx->streamout.append_bitmask = ctx->streamout.enabled_mask;
151 r600_streamout_buffers_dirty(ctx);
152 }
153
154 /* resume queries */
155 if (ctx->nontimer_queries_suspended) {
156 r600_resume_nontimer_queries(ctx);
157 }
158
159 /* Re-enable render condition. */
160 if (ctx->saved_render_cond) {
161 ctx->b.render_condition(&ctx->b, ctx->saved_render_cond,
162 ctx->saved_render_cond_cond,
163 ctx->saved_render_cond_mode);
164 }
165 }
166
167 static void r600_flush_from_st(struct pipe_context *ctx,
168 struct pipe_fence_handle **fence,
169 unsigned flags)
170 {
171 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
172 unsigned rflags = 0;
173
174 if (flags & PIPE_FLUSH_END_OF_FRAME)
175 rflags |= RADEON_FLUSH_END_OF_FRAME;
176
177 if (rctx->rings.dma.cs) {
178 rctx->rings.dma.flush(rctx, rflags, NULL);
179 }
180 rctx->rings.gfx.flush(rctx, rflags, fence);
181 }
182
183 static void r600_flush_dma_ring(void *ctx, unsigned flags,
184 struct pipe_fence_handle **fence)
185 {
186 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
187 struct radeon_winsys_cs *cs = rctx->rings.dma.cs;
188
189 if (!cs->cdw) {
190 return;
191 }
192
193 rctx->rings.dma.flushing = true;
194 rctx->ws->cs_flush(cs, flags, fence, 0);
195 rctx->rings.dma.flushing = false;
196 }
197
198 bool r600_common_context_init(struct r600_common_context *rctx,
199 struct r600_common_screen *rscreen)
200 {
201 util_slab_create(&rctx->pool_transfers,
202 sizeof(struct r600_transfer), 64,
203 UTIL_SLAB_SINGLETHREADED);
204
205 rctx->screen = rscreen;
206 rctx->ws = rscreen->ws;
207 rctx->family = rscreen->family;
208 rctx->chip_class = rscreen->chip_class;
209
210 if (rscreen->family == CHIP_HAWAII)
211 rctx->max_db = 16;
212 else if (rscreen->chip_class >= EVERGREEN)
213 rctx->max_db = 8;
214 else
215 rctx->max_db = 4;
216
217 rctx->b.transfer_map = u_transfer_map_vtbl;
218 rctx->b.transfer_flush_region = u_default_transfer_flush_region;
219 rctx->b.transfer_unmap = u_transfer_unmap_vtbl;
220 rctx->b.transfer_inline_write = u_default_transfer_inline_write;
221 rctx->b.memory_barrier = r600_memory_barrier;
222 rctx->b.flush = r600_flush_from_st;
223
224 LIST_INITHEAD(&rctx->texture_buffers);
225
226 r600_init_context_texture_functions(rctx);
227 r600_streamout_init(rctx);
228 r600_query_init(rctx);
229 cayman_init_msaa(&rctx->b);
230
231 rctx->allocator_so_filled_size = u_suballocator_create(&rctx->b, 4096, 4,
232 0, PIPE_USAGE_DEFAULT, TRUE);
233 if (!rctx->allocator_so_filled_size)
234 return false;
235
236 rctx->uploader = u_upload_create(&rctx->b, 1024 * 1024, 256,
237 PIPE_BIND_INDEX_BUFFER |
238 PIPE_BIND_CONSTANT_BUFFER);
239 if (!rctx->uploader)
240 return false;
241
242 if (rscreen->info.r600_has_dma && !(rscreen->debug_flags & DBG_NO_ASYNC_DMA)) {
243 rctx->rings.dma.cs = rctx->ws->cs_create(rctx->ws, RING_DMA,
244 r600_flush_dma_ring,
245 rctx, NULL);
246 rctx->rings.dma.flush = r600_flush_dma_ring;
247 }
248
249 return true;
250 }
251
252 void r600_common_context_cleanup(struct r600_common_context *rctx)
253 {
254 if (rctx->rings.gfx.cs) {
255 rctx->ws->cs_destroy(rctx->rings.gfx.cs);
256 }
257 if (rctx->rings.dma.cs) {
258 rctx->ws->cs_destroy(rctx->rings.dma.cs);
259 }
260
261 if (rctx->uploader) {
262 u_upload_destroy(rctx->uploader);
263 }
264
265 util_slab_destroy(&rctx->pool_transfers);
266
267 if (rctx->allocator_so_filled_size) {
268 u_suballocator_destroy(rctx->allocator_so_filled_size);
269 }
270 }
271
272 void r600_context_add_resource_size(struct pipe_context *ctx, struct pipe_resource *r)
273 {
274 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
275 struct r600_resource *rr = (struct r600_resource *)r;
276
277 if (r == NULL) {
278 return;
279 }
280
281 /*
282 * The idea is to compute a gross estimate of memory requirement of
283 * each draw call. After each draw call, memory will be precisely
284 * accounted. So the uncertainty is only on the current draw call.
285 * In practice this gave very good estimate (+/- 10% of the target
286 * memory limit).
287 */
288 if (rr->domains & RADEON_DOMAIN_GTT) {
289 rctx->gtt += rr->buf->size;
290 }
291 if (rr->domains & RADEON_DOMAIN_VRAM) {
292 rctx->vram += rr->buf->size;
293 }
294 }
295
296 /*
297 * pipe_screen
298 */
299
300 static const struct debug_named_value common_debug_options[] = {
301 /* logging */
302 { "tex", DBG_TEX, "Print texture info" },
303 { "texmip", DBG_TEXMIP, "Print texture info (mipmapped only)" },
304 { "compute", DBG_COMPUTE, "Print compute info" },
305 { "vm", DBG_VM, "Print virtual addresses when creating resources" },
306 { "trace_cs", DBG_TRACE_CS, "Trace cs and write rlockup_<csid>.c file with faulty cs" },
307
308 /* shaders */
309 { "fs", DBG_FS, "Print fetch shaders" },
310 { "vs", DBG_VS, "Print vertex shaders" },
311 { "gs", DBG_GS, "Print geometry shaders" },
312 { "ps", DBG_PS, "Print pixel shaders" },
313 { "cs", DBG_CS, "Print compute shaders" },
314
315 /* features */
316 { "nodma", DBG_NO_ASYNC_DMA, "Disable asynchronous DMA" },
317 { "hyperz", DBG_HYPERZ, "Enable Hyper-Z" },
318 /* GL uses the word INVALIDATE, gallium uses the word DISCARD */
319 { "noinvalrange", DBG_NO_DISCARD_RANGE, "Disable handling of INVALIDATE_RANGE map flags" },
320 { "no2d", DBG_NO_2D_TILING, "Disable 2D tiling" },
321 { "notiling", DBG_NO_TILING, "Disable tiling" },
322 { "switch_on_eop", DBG_SWITCH_ON_EOP, "Program WD/IA to switch on end-of-packet." },
323 { "forcedma", DBG_FORCE_DMA, "Use asynchronous DMA for all operations when possible." },
324
325 DEBUG_NAMED_VALUE_END /* must be last */
326 };
327
328 static const char* r600_get_vendor(struct pipe_screen* pscreen)
329 {
330 return "X.Org";
331 }
332
333 static const char* r600_get_name(struct pipe_screen* pscreen)
334 {
335 struct r600_common_screen *rscreen = (struct r600_common_screen*)pscreen;
336
337 switch (rscreen->family) {
338 case CHIP_R600: return "AMD R600";
339 case CHIP_RV610: return "AMD RV610";
340 case CHIP_RV630: return "AMD RV630";
341 case CHIP_RV670: return "AMD RV670";
342 case CHIP_RV620: return "AMD RV620";
343 case CHIP_RV635: return "AMD RV635";
344 case CHIP_RS780: return "AMD RS780";
345 case CHIP_RS880: return "AMD RS880";
346 case CHIP_RV770: return "AMD RV770";
347 case CHIP_RV730: return "AMD RV730";
348 case CHIP_RV710: return "AMD RV710";
349 case CHIP_RV740: return "AMD RV740";
350 case CHIP_CEDAR: return "AMD CEDAR";
351 case CHIP_REDWOOD: return "AMD REDWOOD";
352 case CHIP_JUNIPER: return "AMD JUNIPER";
353 case CHIP_CYPRESS: return "AMD CYPRESS";
354 case CHIP_HEMLOCK: return "AMD HEMLOCK";
355 case CHIP_PALM: return "AMD PALM";
356 case CHIP_SUMO: return "AMD SUMO";
357 case CHIP_SUMO2: return "AMD SUMO2";
358 case CHIP_BARTS: return "AMD BARTS";
359 case CHIP_TURKS: return "AMD TURKS";
360 case CHIP_CAICOS: return "AMD CAICOS";
361 case CHIP_CAYMAN: return "AMD CAYMAN";
362 case CHIP_ARUBA: return "AMD ARUBA";
363 case CHIP_TAHITI: return "AMD TAHITI";
364 case CHIP_PITCAIRN: return "AMD PITCAIRN";
365 case CHIP_VERDE: return "AMD CAPE VERDE";
366 case CHIP_OLAND: return "AMD OLAND";
367 case CHIP_HAINAN: return "AMD HAINAN";
368 case CHIP_BONAIRE: return "AMD BONAIRE";
369 case CHIP_KAVERI: return "AMD KAVERI";
370 case CHIP_KABINI: return "AMD KABINI";
371 case CHIP_HAWAII: return "AMD HAWAII";
372 case CHIP_MULLINS: return "AMD MULLINS";
373 default: return "AMD unknown";
374 }
375 }
376
377 static float r600_get_paramf(struct pipe_screen* pscreen,
378 enum pipe_capf param)
379 {
380 struct r600_common_screen *rscreen = (struct r600_common_screen *)pscreen;
381
382 switch (param) {
383 case PIPE_CAPF_MAX_LINE_WIDTH:
384 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
385 case PIPE_CAPF_MAX_POINT_WIDTH:
386 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
387 if (rscreen->family >= CHIP_CEDAR)
388 return 16384.0f;
389 else
390 return 8192.0f;
391 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
392 return 16.0f;
393 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
394 return 16.0f;
395 case PIPE_CAPF_GUARD_BAND_LEFT:
396 case PIPE_CAPF_GUARD_BAND_TOP:
397 case PIPE_CAPF_GUARD_BAND_RIGHT:
398 case PIPE_CAPF_GUARD_BAND_BOTTOM:
399 return 0.0f;
400 }
401 return 0.0f;
402 }
403
404 static int r600_get_video_param(struct pipe_screen *screen,
405 enum pipe_video_profile profile,
406 enum pipe_video_entrypoint entrypoint,
407 enum pipe_video_cap param)
408 {
409 switch (param) {
410 case PIPE_VIDEO_CAP_SUPPORTED:
411 return vl_profile_supported(screen, profile, entrypoint);
412 case PIPE_VIDEO_CAP_NPOT_TEXTURES:
413 return 1;
414 case PIPE_VIDEO_CAP_MAX_WIDTH:
415 case PIPE_VIDEO_CAP_MAX_HEIGHT:
416 return vl_video_buffer_max_size(screen);
417 case PIPE_VIDEO_CAP_PREFERED_FORMAT:
418 return PIPE_FORMAT_NV12;
419 case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
420 return false;
421 case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
422 return false;
423 case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
424 return true;
425 case PIPE_VIDEO_CAP_MAX_LEVEL:
426 return vl_level_supported(screen, profile);
427 default:
428 return 0;
429 }
430 }
431
432 const char *r600_get_llvm_processor_name(enum radeon_family family)
433 {
434 switch (family) {
435 case CHIP_R600:
436 case CHIP_RV630:
437 case CHIP_RV635:
438 case CHIP_RV670:
439 return "r600";
440 case CHIP_RV610:
441 case CHIP_RV620:
442 case CHIP_RS780:
443 case CHIP_RS880:
444 return "rs880";
445 case CHIP_RV710:
446 return "rv710";
447 case CHIP_RV730:
448 return "rv730";
449 case CHIP_RV740:
450 case CHIP_RV770:
451 return "rv770";
452 case CHIP_PALM:
453 case CHIP_CEDAR:
454 return "cedar";
455 case CHIP_SUMO:
456 case CHIP_SUMO2:
457 return "sumo";
458 case CHIP_REDWOOD:
459 return "redwood";
460 case CHIP_JUNIPER:
461 return "juniper";
462 case CHIP_HEMLOCK:
463 case CHIP_CYPRESS:
464 return "cypress";
465 case CHIP_BARTS:
466 return "barts";
467 case CHIP_TURKS:
468 return "turks";
469 case CHIP_CAICOS:
470 return "caicos";
471 case CHIP_CAYMAN:
472 case CHIP_ARUBA:
473 return "cayman";
474
475 case CHIP_TAHITI: return "tahiti";
476 case CHIP_PITCAIRN: return "pitcairn";
477 case CHIP_VERDE: return "verde";
478 case CHIP_OLAND: return "oland";
479 case CHIP_HAINAN: return "hainan";
480 case CHIP_BONAIRE: return "bonaire";
481 case CHIP_KABINI: return "kabini";
482 case CHIP_KAVERI: return "kaveri";
483 case CHIP_HAWAII: return "hawaii";
484 case CHIP_MULLINS:
485 #if HAVE_LLVM >= 0x0305
486 return "mullins";
487 #else
488 return "kabini";
489 #endif
490 default: return "";
491 }
492 }
493
494 static int r600_get_compute_param(struct pipe_screen *screen,
495 enum pipe_compute_cap param,
496 void *ret)
497 {
498 struct r600_common_screen *rscreen = (struct r600_common_screen *)screen;
499
500 //TODO: select these params by asic
501 switch (param) {
502 case PIPE_COMPUTE_CAP_IR_TARGET: {
503 const char *gpu;
504 switch(rscreen->family) {
505 /* Clang < 3.6 is missing Hainan in its list of
506 * GPUs, so we need to use the name of a similar GPU.
507 */
508 #if HAVE_LLVM < 0x0306
509 case CHIP_HAINAN:
510 gpu = "oland";
511 break;
512 #endif
513 default:
514 gpu = r600_get_llvm_processor_name(rscreen->family);
515 break;
516 }
517 if (ret) {
518 sprintf(ret, "%s-r600--", gpu);
519 }
520 return (8 + strlen(gpu)) * sizeof(char);
521 }
522 case PIPE_COMPUTE_CAP_GRID_DIMENSION:
523 if (ret) {
524 uint64_t *grid_dimension = ret;
525 grid_dimension[0] = 3;
526 }
527 return 1 * sizeof(uint64_t);
528
529 case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
530 if (ret) {
531 uint64_t *grid_size = ret;
532 grid_size[0] = 65535;
533 grid_size[1] = 65535;
534 grid_size[2] = 1;
535 }
536 return 3 * sizeof(uint64_t) ;
537
538 case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
539 if (ret) {
540 uint64_t *block_size = ret;
541 block_size[0] = 256;
542 block_size[1] = 256;
543 block_size[2] = 256;
544 }
545 return 3 * sizeof(uint64_t);
546
547 case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
548 if (ret) {
549 uint64_t *max_threads_per_block = ret;
550 *max_threads_per_block = 256;
551 }
552 return sizeof(uint64_t);
553
554 case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
555 if (ret) {
556 uint64_t *max_global_size = ret;
557 uint64_t max_mem_alloc_size;
558
559 r600_get_compute_param(screen,
560 PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE,
561 &max_mem_alloc_size);
562
563 /* In OpenCL, the MAX_MEM_ALLOC_SIZE must be at least
564 * 1/4 of the MAX_GLOBAL_SIZE. Since the
565 * MAX_MEM_ALLOC_SIZE is fixed for older kernels,
566 * make sure we never report more than
567 * 4 * MAX_MEM_ALLOC_SIZE.
568 */
569 *max_global_size = MIN2(4 * max_mem_alloc_size,
570 rscreen->info.gart_size +
571 rscreen->info.vram_size);
572 }
573 return sizeof(uint64_t);
574
575 case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
576 if (ret) {
577 uint64_t *max_local_size = ret;
578 /* Value reported by the closed source driver. */
579 *max_local_size = 32768;
580 }
581 return sizeof(uint64_t);
582
583 case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
584 if (ret) {
585 uint64_t *max_input_size = ret;
586 /* Value reported by the closed source driver. */
587 *max_input_size = 1024;
588 }
589 return sizeof(uint64_t);
590
591 case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
592 if (ret) {
593 uint64_t max_global_size;
594 uint64_t *max_mem_alloc_size = ret;
595
596 /* XXX: The limit in older kernels is 256 MB. We
597 * should add a query here for newer kernels.
598 */
599 *max_mem_alloc_size = 256 * 1024 * 1024;
600 }
601 return sizeof(uint64_t);
602
603 case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
604 if (ret) {
605 uint32_t *max_clock_frequency = ret;
606 *max_clock_frequency = rscreen->info.max_sclk;
607 }
608 return sizeof(uint32_t);
609
610 case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
611 if (ret) {
612 uint32_t *max_compute_units = ret;
613 *max_compute_units = MAX2(rscreen->info.max_compute_units, 1);
614 }
615 return sizeof(uint32_t);
616
617 case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
618 if (ret) {
619 uint32_t *images_supported = ret;
620 *images_supported = 0;
621 }
622 return sizeof(uint32_t);
623 }
624
625 fprintf(stderr, "unknown PIPE_COMPUTE_CAP %d\n", param);
626 return 0;
627 }
628
629 static uint64_t r600_get_timestamp(struct pipe_screen *screen)
630 {
631 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
632
633 return 1000000 * rscreen->ws->query_value(rscreen->ws, RADEON_TIMESTAMP) /
634 rscreen->info.r600_clock_crystal_freq;
635 }
636
637 static int r600_get_driver_query_info(struct pipe_screen *screen,
638 unsigned index,
639 struct pipe_driver_query_info *info)
640 {
641 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
642 struct pipe_driver_query_info list[] = {
643 {"draw-calls", R600_QUERY_DRAW_CALLS, 0},
644 {"requested-VRAM", R600_QUERY_REQUESTED_VRAM, rscreen->info.vram_size, TRUE},
645 {"requested-GTT", R600_QUERY_REQUESTED_GTT, rscreen->info.gart_size, TRUE},
646 {"buffer-wait-time", R600_QUERY_BUFFER_WAIT_TIME, 0, FALSE},
647 {"num-cs-flushes", R600_QUERY_NUM_CS_FLUSHES, 0, FALSE},
648 {"num-bytes-moved", R600_QUERY_NUM_BYTES_MOVED, 0, TRUE},
649 {"VRAM-usage", R600_QUERY_VRAM_USAGE, rscreen->info.vram_size, TRUE},
650 {"GTT-usage", R600_QUERY_GTT_USAGE, rscreen->info.gart_size, TRUE},
651 };
652
653 if (!info)
654 return Elements(list);
655
656 if (index >= Elements(list))
657 return 0;
658
659 *info = list[index];
660 return 1;
661 }
662
663 static void r600_fence_reference(struct pipe_screen *screen,
664 struct pipe_fence_handle **ptr,
665 struct pipe_fence_handle *fence)
666 {
667 struct radeon_winsys *rws = ((struct r600_common_screen*)screen)->ws;
668
669 rws->fence_reference(ptr, fence);
670 }
671
672 static boolean r600_fence_signalled(struct pipe_screen *screen,
673 struct pipe_fence_handle *fence)
674 {
675 struct radeon_winsys *rws = ((struct r600_common_screen*)screen)->ws;
676
677 return rws->fence_wait(rws, fence, 0);
678 }
679
680 static boolean r600_fence_finish(struct pipe_screen *screen,
681 struct pipe_fence_handle *fence,
682 uint64_t timeout)
683 {
684 struct radeon_winsys *rws = ((struct r600_common_screen*)screen)->ws;
685
686 return rws->fence_wait(rws, fence, timeout);
687 }
688
689 static bool r600_interpret_tiling(struct r600_common_screen *rscreen,
690 uint32_t tiling_config)
691 {
692 switch ((tiling_config & 0xe) >> 1) {
693 case 0:
694 rscreen->tiling_info.num_channels = 1;
695 break;
696 case 1:
697 rscreen->tiling_info.num_channels = 2;
698 break;
699 case 2:
700 rscreen->tiling_info.num_channels = 4;
701 break;
702 case 3:
703 rscreen->tiling_info.num_channels = 8;
704 break;
705 default:
706 return false;
707 }
708
709 switch ((tiling_config & 0x30) >> 4) {
710 case 0:
711 rscreen->tiling_info.num_banks = 4;
712 break;
713 case 1:
714 rscreen->tiling_info.num_banks = 8;
715 break;
716 default:
717 return false;
718
719 }
720 switch ((tiling_config & 0xc0) >> 6) {
721 case 0:
722 rscreen->tiling_info.group_bytes = 256;
723 break;
724 case 1:
725 rscreen->tiling_info.group_bytes = 512;
726 break;
727 default:
728 return false;
729 }
730 return true;
731 }
732
733 static bool evergreen_interpret_tiling(struct r600_common_screen *rscreen,
734 uint32_t tiling_config)
735 {
736 switch (tiling_config & 0xf) {
737 case 0:
738 rscreen->tiling_info.num_channels = 1;
739 break;
740 case 1:
741 rscreen->tiling_info.num_channels = 2;
742 break;
743 case 2:
744 rscreen->tiling_info.num_channels = 4;
745 break;
746 case 3:
747 rscreen->tiling_info.num_channels = 8;
748 break;
749 default:
750 return false;
751 }
752
753 switch ((tiling_config & 0xf0) >> 4) {
754 case 0:
755 rscreen->tiling_info.num_banks = 4;
756 break;
757 case 1:
758 rscreen->tiling_info.num_banks = 8;
759 break;
760 case 2:
761 rscreen->tiling_info.num_banks = 16;
762 break;
763 default:
764 return false;
765 }
766
767 switch ((tiling_config & 0xf00) >> 8) {
768 case 0:
769 rscreen->tiling_info.group_bytes = 256;
770 break;
771 case 1:
772 rscreen->tiling_info.group_bytes = 512;
773 break;
774 default:
775 return false;
776 }
777 return true;
778 }
779
780 static bool r600_init_tiling(struct r600_common_screen *rscreen)
781 {
782 uint32_t tiling_config = rscreen->info.r600_tiling_config;
783
784 /* set default group bytes, overridden by tiling info ioctl */
785 if (rscreen->chip_class <= R700) {
786 rscreen->tiling_info.group_bytes = 256;
787 } else {
788 rscreen->tiling_info.group_bytes = 512;
789 }
790
791 if (!tiling_config)
792 return true;
793
794 if (rscreen->chip_class <= R700) {
795 return r600_interpret_tiling(rscreen, tiling_config);
796 } else {
797 return evergreen_interpret_tiling(rscreen, tiling_config);
798 }
799 }
800
801 struct pipe_resource *r600_resource_create_common(struct pipe_screen *screen,
802 const struct pipe_resource *templ)
803 {
804 if (templ->target == PIPE_BUFFER) {
805 return r600_buffer_create(screen, templ, 4096);
806 } else {
807 return r600_texture_create(screen, templ);
808 }
809 }
810
811 bool r600_common_screen_init(struct r600_common_screen *rscreen,
812 struct radeon_winsys *ws)
813 {
814 ws->query_info(ws, &rscreen->info);
815
816 rscreen->b.get_name = r600_get_name;
817 rscreen->b.get_vendor = r600_get_vendor;
818 rscreen->b.get_compute_param = r600_get_compute_param;
819 rscreen->b.get_paramf = r600_get_paramf;
820 rscreen->b.get_driver_query_info = r600_get_driver_query_info;
821 rscreen->b.get_timestamp = r600_get_timestamp;
822 rscreen->b.fence_finish = r600_fence_finish;
823 rscreen->b.fence_reference = r600_fence_reference;
824 rscreen->b.fence_signalled = r600_fence_signalled;
825 rscreen->b.resource_destroy = u_resource_destroy_vtbl;
826
827 if (rscreen->info.has_uvd) {
828 rscreen->b.get_video_param = rvid_get_video_param;
829 rscreen->b.is_video_format_supported = rvid_is_format_supported;
830 } else {
831 rscreen->b.get_video_param = r600_get_video_param;
832 rscreen->b.is_video_format_supported = vl_video_buffer_is_format_supported;
833 }
834
835 r600_init_screen_texture_functions(rscreen);
836
837 rscreen->ws = ws;
838 rscreen->family = rscreen->info.family;
839 rscreen->chip_class = rscreen->info.chip_class;
840 rscreen->debug_flags = debug_get_flags_option("R600_DEBUG", common_debug_options, 0);
841
842 if (!r600_init_tiling(rscreen)) {
843 return false;
844 }
845 util_format_s3tc_init();
846 pipe_mutex_init(rscreen->aux_context_lock);
847
848 if (rscreen->info.drm_minor >= 28 && (rscreen->debug_flags & DBG_TRACE_CS)) {
849 rscreen->trace_bo = (struct r600_resource*)pipe_buffer_create(&rscreen->b,
850 PIPE_BIND_CUSTOM,
851 PIPE_USAGE_STAGING,
852 4096);
853 if (rscreen->trace_bo) {
854 rscreen->trace_ptr = rscreen->ws->buffer_map(rscreen->trace_bo->cs_buf, NULL,
855 PIPE_TRANSFER_UNSYNCHRONIZED);
856 }
857 }
858
859 return true;
860 }
861
862 void r600_destroy_common_screen(struct r600_common_screen *rscreen)
863 {
864 pipe_mutex_destroy(rscreen->aux_context_lock);
865 rscreen->aux_context->destroy(rscreen->aux_context);
866
867 if (rscreen->trace_bo) {
868 rscreen->ws->buffer_unmap(rscreen->trace_bo->cs_buf);
869 pipe_resource_reference((struct pipe_resource**)&rscreen->trace_bo, NULL);
870 }
871
872 rscreen->ws->destroy(rscreen->ws);
873 FREE(rscreen);
874 }
875
876 static unsigned tgsi_get_processor_type(const struct tgsi_token *tokens)
877 {
878 struct tgsi_parse_context parse;
879
880 if (tgsi_parse_init( &parse, tokens ) != TGSI_PARSE_OK) {
881 debug_printf("tgsi_parse_init() failed in %s:%i!\n", __func__, __LINE__);
882 return ~0;
883 }
884 return parse.FullHeader.Processor.Processor;
885 }
886
887 bool r600_can_dump_shader(struct r600_common_screen *rscreen,
888 const struct tgsi_token *tokens)
889 {
890 /* Compute shader don't have tgsi_tokens */
891 if (!tokens)
892 return (rscreen->debug_flags & DBG_CS) != 0;
893
894 switch (tgsi_get_processor_type(tokens)) {
895 case TGSI_PROCESSOR_VERTEX:
896 return (rscreen->debug_flags & DBG_VS) != 0;
897 case TGSI_PROCESSOR_GEOMETRY:
898 return (rscreen->debug_flags & DBG_GS) != 0;
899 case TGSI_PROCESSOR_FRAGMENT:
900 return (rscreen->debug_flags & DBG_PS) != 0;
901 case TGSI_PROCESSOR_COMPUTE:
902 return (rscreen->debug_flags & DBG_CS) != 0;
903 default:
904 return false;
905 }
906 }
907
908 void r600_screen_clear_buffer(struct r600_common_screen *rscreen, struct pipe_resource *dst,
909 unsigned offset, unsigned size, unsigned value)
910 {
911 struct r600_common_context *rctx = (struct r600_common_context*)rscreen->aux_context;
912
913 pipe_mutex_lock(rscreen->aux_context_lock);
914 rctx->clear_buffer(&rctx->b, dst, offset, size, value);
915 rscreen->aux_context->flush(rscreen->aux_context, NULL, 0);
916 pipe_mutex_unlock(rscreen->aux_context_lock);
917 }