Debian Codegraph @

src/gallium/winsys/radeon/drm/radeon_surface.c

raw
1 /*
2 * Copyright © 2011 Red Hat All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
14 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
16 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 */
25 /*
26 * Authors:
27 * Jérôme Glisse <[email protected]>
28 */
29
30 #include "radeon_surface.h"
31 #include "util/compiler.h"
32 #include "util/u_math.h"
33 #include <errno.h>
34 #include <xf86drm.h>
35 #include "drm-uapi/radeon_drm.h"
36
37 #define CIK_TILE_MODE_COLOR_2D 14
38 #define CIK_TILE_MODE_COLOR_2D_SCANOUT 10
39 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64 0
40 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128 1
41 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256 2
42 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_512 3
43 #define CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_ROW_SIZE 4
44
45
46 /* keep this private */
47 enum radeon_family {
48 CHIP_UNKNOWN,
49 CHIP_R600,
50 CHIP_RV610,
51 CHIP_RV630,
52 CHIP_RV670,
53 CHIP_RV620,
54 CHIP_RV635,
55 CHIP_RS780,
56 CHIP_RS880,
57 CHIP_RV770,
58 CHIP_RV730,
59 CHIP_RV710,
60 CHIP_RV740,
61 CHIP_CEDAR,
62 CHIP_REDWOOD,
63 CHIP_JUNIPER,
64 CHIP_CYPRESS,
65 CHIP_HEMLOCK,
66 CHIP_PALM,
67 CHIP_SUMO,
68 CHIP_SUMO2,
69 CHIP_BARTS,
70 CHIP_TURKS,
71 CHIP_CAICOS,
72 CHIP_CAYMAN,
73 CHIP_ARUBA,
74 CHIP_TAHITI,
75 CHIP_PITCAIRN,
76 CHIP_VERDE,
77 CHIP_OLAND,
78 CHIP_HAINAN,
79 CHIP_BONAIRE,
80 CHIP_KAVERI,
81 CHIP_KABINI,
82 CHIP_HAWAII,
83 CHIP_MULLINS,
84 CHIP_LAST,
85 };
86
87 typedef int (*hw_init_surface_t)(struct radeon_surface_manager *surf_man,
88 struct radeon_surface *surf);
89 typedef int (*hw_best_surface_t)(struct radeon_surface_manager *surf_man,
90 struct radeon_surface *surf);
91
92 struct radeon_hw_info {
93 /* apply to r6, eg */
94 uint32_t group_bytes;
95 uint32_t num_banks;
96 uint32_t num_pipes;
97 /* apply to eg */
98 uint32_t row_size;
99 unsigned allow_2d;
100 /* apply to si */
101 uint32_t tile_mode_array[32];
102 /* apply to cik */
103 uint32_t macrotile_mode_array[16];
104 };
105
106 struct radeon_surface_manager {
107 int fd;
108 uint32_t device_id;
109 struct radeon_hw_info hw_info;
110 unsigned family;
111 hw_init_surface_t surface_init;
112 hw_best_surface_t surface_best;
113 };
114
115 /* helper */
116 static int radeon_get_value(int fd, unsigned req, uint32_t *value)
117 {
118 struct drm_radeon_info info = {};
119 int r;
120
121 *value = 0;
122 info.request = req;
123 info.value = (uintptr_t)value;
124 r = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info,
125 sizeof(struct drm_radeon_info));
126 return r;
127 }
128
129 static int radeon_get_family(struct radeon_surface_manager *surf_man)
130 {
131 switch (surf_man->device_id) {
132 #define CHIPSET(pci_id, name, fam) case pci_id: surf_man->family = CHIP_##fam; break;
133 #include "pci_ids/r600_pci_ids.h"
134 #undef CHIPSET
135 #define CHIPSET(pci_id, fam) case pci_id: surf_man->family = CHIP_##fam; break;
136 #include "pci_ids/radeonsi_pci_ids.h"
137 #undef CHIPSET
138 default:
139 return -EINVAL;
140 }
141 return 0;
142 }
143
144 static unsigned next_power_of_two(unsigned x)
145 {
146 if (x <= 1)
147 return 1;
148
149 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
150 }
151
152 static unsigned mip_minify(unsigned size, unsigned level)
153 {
154 unsigned val;
155
156 val = MAX2(1, size >> level);
157 if (level > 0)
158 val = next_power_of_two(val);
159 return val;
160 }
161
162 static void surf_minify(struct radeon_surface *surf,
163 struct radeon_surface_level *surflevel,
164 unsigned bpe, unsigned level,
165 uint32_t xalign, uint32_t yalign, uint32_t zalign,
166 uint64_t offset)
167 {
168 surflevel->npix_x = mip_minify(surf->npix_x, level);
169 surflevel->npix_y = mip_minify(surf->npix_y, level);
170 surflevel->npix_z = mip_minify(surf->npix_z, level);
171 surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w;
172 surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h;
173 surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d;
174 if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D &&
175 !(surf->flags & RADEON_SURF_FMASK)) {
176 if (surflevel->nblk_x < xalign || surflevel->nblk_y < yalign) {
177 surflevel->mode = RADEON_SURF_MODE_1D;
178 return;
179 }
180 }
181 surflevel->nblk_x = align(surflevel->nblk_x, xalign);
182 surflevel->nblk_y = align(surflevel->nblk_y, yalign);
183 surflevel->nblk_z = align(surflevel->nblk_z, zalign);
184
185 surflevel->offset = offset;
186 surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples;
187 surflevel->slice_size = (uint64_t)surflevel->pitch_bytes * surflevel->nblk_y;
188
189 surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size;
190 }
191
192 /* ===========================================================================
193 * r600/r700 family
194 */
195 static int r6_init_hw_info(struct radeon_surface_manager *surf_man)
196 {
197 uint32_t tiling_config;
198 drmVersionPtr version;
199 int r;
200
201 r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG,
202 &tiling_config);
203 if (r) {
204 return r;
205 }
206
207 surf_man->hw_info.allow_2d = 0;
208 version = drmGetVersion(surf_man->fd);
209 if (version && version->version_minor >= 14) {
210 surf_man->hw_info.allow_2d = 1;
211 }
212 drmFreeVersion(version);
213
214 switch ((tiling_config & 0xe) >> 1) {
215 case 0:
216 surf_man->hw_info.num_pipes = 1;
217 break;
218 case 1:
219 surf_man->hw_info.num_pipes = 2;
220 break;
221 case 2:
222 surf_man->hw_info.num_pipes = 4;
223 break;
224 case 3:
225 surf_man->hw_info.num_pipes = 8;
226 break;
227 default:
228 surf_man->hw_info.num_pipes = 8;
229 surf_man->hw_info.allow_2d = 0;
230 break;
231 }
232
233 switch ((tiling_config & 0x30) >> 4) {
234 case 0:
235 surf_man->hw_info.num_banks = 4;
236 break;
237 case 1:
238 surf_man->hw_info.num_banks = 8;
239 break;
240 default:
241 surf_man->hw_info.num_banks = 8;
242 surf_man->hw_info.allow_2d = 0;
243 break;
244 }
245
246 switch ((tiling_config & 0xc0) >> 6) {
247 case 0:
248 surf_man->hw_info.group_bytes = 256;
249 break;
250 case 1:
251 surf_man->hw_info.group_bytes = 512;
252 break;
253 default:
254 surf_man->hw_info.group_bytes = 256;
255 surf_man->hw_info.allow_2d = 0;
256 break;
257 }
258 return 0;
259 }
260
261 static int r6_surface_init_linear(struct radeon_surface_manager *surf_man,
262 struct radeon_surface *surf,
263 uint64_t offset, unsigned start_level)
264 {
265 uint32_t xalign, yalign, zalign;
266 unsigned i;
267
268 /* compute alignment */
269 if (!start_level) {
270 surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes);
271 }
272 /* the 32 alignment is for scanout, cb or db but to allow texture to be
273 * easily bound as such we force this alignment to all surface
274 */
275 xalign = MAX2(1, surf_man->hw_info.group_bytes / surf->bpe);
276 yalign = 1;
277 zalign = 1;
278 if (surf->flags & RADEON_SURF_SCANOUT) {
279 xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign);
280 }
281
282 /* build mipmap tree */
283 for (i = start_level; i <= surf->last_level; i++) {
284 surf->level[i].mode = RADEON_SURF_MODE_LINEAR;
285 surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset);
286 /* level0 and first mipmap need to have alignment */
287 offset = surf->bo_size;
288 if (i == 0) {
289 offset = align64(offset, surf->bo_alignment);
290 }
291 }
292 return 0;
293 }
294
295 static int r6_surface_init_linear_aligned(struct radeon_surface_manager *surf_man,
296 struct radeon_surface *surf,
297 uint64_t offset, unsigned start_level)
298 {
299 uint32_t xalign, yalign, zalign;
300 unsigned i;
301
302 /* compute alignment */
303 if (!start_level) {
304 surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes);
305 }
306 xalign = MAX2(64, surf_man->hw_info.group_bytes / surf->bpe);
307 yalign = 1;
308 zalign = 1;
309
310 /* build mipmap tree */
311 for (i = start_level; i <= surf->last_level; i++) {
312 surf->level[i].mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
313 surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset);
314 /* level0 and first mipmap need to have alignment */
315 offset = surf->bo_size;
316 if (i == 0) {
317 offset = align64(offset, surf->bo_alignment);
318 }
319 }
320 return 0;
321 }
322
323 static int r6_surface_init_1d(struct radeon_surface_manager *surf_man,
324 struct radeon_surface *surf,
325 uint64_t offset, unsigned start_level)
326 {
327 uint32_t xalign, yalign, zalign, tilew;
328 unsigned i;
329
330 /* compute alignment */
331 tilew = 8;
332 xalign = surf_man->hw_info.group_bytes / (tilew * surf->bpe * surf->nsamples);
333 xalign = MAX2(tilew, xalign);
334 yalign = tilew;
335 zalign = 1;
336 if (surf->flags & RADEON_SURF_SCANOUT) {
337 xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign);
338 }
339 if (!start_level) {
340 surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes);
341 }
342
343 /* build mipmap tree */
344 for (i = start_level; i <= surf->last_level; i++) {
345 surf->level[i].mode = RADEON_SURF_MODE_1D;
346 surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset);
347 /* level0 and first mipmap need to have alignment */
348 offset = surf->bo_size;
349 if (i == 0) {
350 offset = align64(offset, surf->bo_alignment);
351 }
352 }
353 return 0;
354 }
355
356 static int r6_surface_init_2d(struct radeon_surface_manager *surf_man,
357 struct radeon_surface *surf,
358 uint64_t offset, unsigned start_level)
359 {
360 uint32_t xalign, yalign, zalign, tilew;
361 unsigned i;
362
363 /* compute alignment */
364 tilew = 8;
365 zalign = 1;
366 xalign = (surf_man->hw_info.group_bytes * surf_man->hw_info.num_banks) /
367 (tilew * surf->bpe * surf->nsamples);
368 xalign = MAX2(tilew * surf_man->hw_info.num_banks, xalign);
369 if (surf->flags & RADEON_SURF_FMASK)
370 xalign = MAX2(128, xalign);
371 yalign = tilew * surf_man->hw_info.num_pipes;
372 if (surf->flags & RADEON_SURF_SCANOUT) {
373 xalign = MAX2((surf->bpe == 1) ? 64 : 32, xalign);
374 }
375 if (!start_level) {
376 surf->bo_alignment =
377 MAX2(surf_man->hw_info.num_pipes *
378 surf_man->hw_info.num_banks *
379 surf->nsamples * surf->bpe * 64,
380 xalign * yalign * surf->nsamples * surf->bpe);
381 }
382
383 /* build mipmap tree */
384 for (i = start_level; i <= surf->last_level; i++) {
385 surf->level[i].mode = RADEON_SURF_MODE_2D;
386 surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, offset);
387 if (surf->level[i].mode == RADEON_SURF_MODE_1D) {
388 return r6_surface_init_1d(surf_man, surf, offset, i);
389 }
390 /* level0 and first mipmap need to have alignment */
391 offset = surf->bo_size;
392 if (i == 0) {
393 offset = align64(offset, surf->bo_alignment);
394 }
395 }
396 return 0;
397 }
398
399 static int r6_surface_init(struct radeon_surface_manager *surf_man,
400 struct radeon_surface *surf)
401 {
402 unsigned mode;
403 int r;
404
405 /* MSAA surfaces support the 2D mode only. */
406 if (surf->nsamples > 1) {
407 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
408 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
409 }
410
411 /* tiling mode */
412 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
413
414 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) {
415 /* zbuffer only support 1D or 2D tiled surface */
416 switch (mode) {
417 case RADEON_SURF_MODE_1D:
418 case RADEON_SURF_MODE_2D:
419 break;
420 default:
421 mode = RADEON_SURF_MODE_1D;
422 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
423 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
424 break;
425 }
426 }
427
428 /* force 1d on kernel that can't do 2d */
429 if (!surf_man->hw_info.allow_2d && mode > RADEON_SURF_MODE_1D) {
430 if (surf->nsamples > 1) {
431 fprintf(stderr, "radeon: Cannot use 2D tiling for an MSAA surface (%i).\n", __LINE__);
432 return -EFAULT;
433 }
434 mode = RADEON_SURF_MODE_1D;
435 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
436 surf->flags |= RADEON_SURF_SET(mode, MODE);
437 }
438
439 /* check surface dimension */
440 if (surf->npix_x > 8192 || surf->npix_y > 8192 || surf->npix_z > 8192) {
441 return -EINVAL;
442 }
443
444 /* check mipmap last_level */
445 if (surf->last_level > 14) {
446 return -EINVAL;
447 }
448
449 /* check tiling mode */
450 switch (mode) {
451 case RADEON_SURF_MODE_LINEAR:
452 r = r6_surface_init_linear(surf_man, surf, 0, 0);
453 break;
454 case RADEON_SURF_MODE_LINEAR_ALIGNED:
455 r = r6_surface_init_linear_aligned(surf_man, surf, 0, 0);
456 break;
457 case RADEON_SURF_MODE_1D:
458 r = r6_surface_init_1d(surf_man, surf, 0, 0);
459 break;
460 case RADEON_SURF_MODE_2D:
461 r = r6_surface_init_2d(surf_man, surf, 0, 0);
462 break;
463 default:
464 return -EINVAL;
465 }
466 return r;
467 }
468
469 static int r6_surface_best(struct radeon_surface_manager *surf_man,
470 struct radeon_surface *surf)
471 {
472 /* no value to optimize for r6xx/r7xx */
473 return 0;
474 }
475
476
477 /* ===========================================================================
478 * evergreen family
479 */
480 static int eg_init_hw_info(struct radeon_surface_manager *surf_man)
481 {
482 uint32_t tiling_config;
483 drmVersionPtr version;
484 int r;
485
486 r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG,
487 &tiling_config);
488 if (r) {
489 return r;
490 }
491
492 surf_man->hw_info.allow_2d = 0;
493 version = drmGetVersion(surf_man->fd);
494 if (version && version->version_minor >= 16) {
495 surf_man->hw_info.allow_2d = 1;
496 }
497 drmFreeVersion(version);
498
499 switch (tiling_config & 0xf) {
500 case 0:
501 surf_man->hw_info.num_pipes = 1;
502 break;
503 case 1:
504 surf_man->hw_info.num_pipes = 2;
505 break;
506 case 2:
507 surf_man->hw_info.num_pipes = 4;
508 break;
509 case 3:
510 surf_man->hw_info.num_pipes = 8;
511 break;
512 default:
513 surf_man->hw_info.num_pipes = 8;
514 surf_man->hw_info.allow_2d = 0;
515 break;
516 }
517
518 switch ((tiling_config & 0xf0) >> 4) {
519 case 0:
520 surf_man->hw_info.num_banks = 4;
521 break;
522 case 1:
523 surf_man->hw_info.num_banks = 8;
524 break;
525 case 2:
526 surf_man->hw_info.num_banks = 16;
527 break;
528 default:
529 surf_man->hw_info.num_banks = 8;
530 surf_man->hw_info.allow_2d = 0;
531 break;
532 }
533
534 switch ((tiling_config & 0xf00) >> 8) {
535 case 0:
536 surf_man->hw_info.group_bytes = 256;
537 break;
538 case 1:
539 surf_man->hw_info.group_bytes = 512;
540 break;
541 default:
542 surf_man->hw_info.group_bytes = 256;
543 surf_man->hw_info.allow_2d = 0;
544 break;
545 }
546
547 switch ((tiling_config & 0xf000) >> 12) {
548 case 0:
549 surf_man->hw_info.row_size = 1024;
550 break;
551 case 1:
552 surf_man->hw_info.row_size = 2048;
553 break;
554 case 2:
555 surf_man->hw_info.row_size = 4096;
556 break;
557 default:
558 surf_man->hw_info.row_size = 4096;
559 surf_man->hw_info.allow_2d = 0;
560 break;
561 }
562 return 0;
563 }
564
565 static void eg_surf_minify(struct radeon_surface *surf,
566 struct radeon_surface_level *surflevel,
567 unsigned bpe,
568 unsigned level,
569 unsigned slice_pt,
570 unsigned mtilew,
571 unsigned mtileh,
572 unsigned mtileb,
573 uint64_t offset)
574 {
575 unsigned mtile_pr, mtile_ps;
576
577 surflevel->npix_x = mip_minify(surf->npix_x, level);
578 surflevel->npix_y = mip_minify(surf->npix_y, level);
579 surflevel->npix_z = mip_minify(surf->npix_z, level);
580 surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w;
581 surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h;
582 surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d;
583 if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D &&
584 !(surf->flags & RADEON_SURF_FMASK)) {
585 if (surflevel->nblk_x < mtilew || surflevel->nblk_y < mtileh) {
586 surflevel->mode = RADEON_SURF_MODE_1D;
587 return;
588 }
589 }
590 surflevel->nblk_x = align(surflevel->nblk_x, mtilew);
591 surflevel->nblk_y = align(surflevel->nblk_y, mtileh);
592 surflevel->nblk_z = align(surflevel->nblk_z, 1);
593
594 /* macro tile per row */
595 mtile_pr = surflevel->nblk_x / mtilew;
596 /* macro tile per slice */
597 mtile_ps = (mtile_pr * surflevel->nblk_y) / mtileh;
598
599 surflevel->offset = offset;
600 surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples;
601 surflevel->slice_size = (uint64_t)mtile_ps * mtileb * slice_pt;
602
603 surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size;
604 }
605
606 static int eg_surface_init_1d(struct radeon_surface_manager *surf_man,
607 struct radeon_surface *surf,
608 struct radeon_surface_level *level,
609 unsigned bpe,
610 unsigned align_maginify,
611 unsigned align_mask,
612 uint64_t offset, unsigned start_level)
613 {
614 uint32_t xalign, yalign, zalign, tilew, xalign_masked;
615 unsigned i;
616
617 /* compute alignment */
618 tilew = 8;
619 xalign = surf_man->hw_info.group_bytes / (tilew * bpe * surf->nsamples);
620 xalign_masked = MAX2(tilew, xalign);
621 xalign = MAX2(tilew, xalign * align_maginify);
622 yalign = tilew;
623 zalign = 1;
624 if (surf->flags & RADEON_SURF_SCANOUT) {
625 xalign = MAX2((bpe == 1) ? 64 : 32, xalign);
626 xalign_masked = MAX2((bpe == 1) ? 64 : 32, xalign_masked);
627 }
628
629 if (!start_level) {
630 unsigned alignment = MAX2(256, surf_man->hw_info.group_bytes);
631 surf->bo_alignment = MAX2(surf->bo_alignment, alignment);
632
633 if (offset) {
634 offset = align64(offset, alignment);
635 }
636 }
637
638 /* build mipmap tree */
639 for (i = start_level; i <= surf->last_level; i++) {
640 level[i].mode = RADEON_SURF_MODE_1D;
641 surf_minify(surf, level+i, bpe, i,
642 align_mask & (1U<<i) ? xalign : xalign_masked,
643 yalign, zalign, offset);
644 /* level0 and first mipmap need to have alignment */
645 offset = surf->bo_size;
646 if (i == 0) {
647 offset = align64(offset, surf->bo_alignment);
648 }
649 }
650 return 0;
651 }
652
653 static int eg_surface_init_2d(struct radeon_surface_manager *surf_man,
654 struct radeon_surface *surf,
655 struct radeon_surface_level *level,
656 unsigned bpe, unsigned align_magnify,
657 unsigned tile_split,
658 uint64_t offset, unsigned start_level)
659 {
660 unsigned tilew, tileh, tileb;
661 unsigned mtilew, mtileh, mtileb;
662 unsigned slice_pt;
663 unsigned i;
664
665 /* compute tile values */
666 tilew = 8;
667 tileh = 8;
668 tileb = tilew * tileh * bpe * surf->nsamples;
669 /* slices per tile */
670 slice_pt = 1;
671 if (tileb > tile_split && tile_split) {
672 slice_pt = tileb / tile_split;
673 }
674 tileb = tileb / slice_pt;
675
676 /* macro tile width & height */
677 mtilew = (tilew * surf->bankw * surf_man->hw_info.num_pipes) * surf->mtilea;
678 mtileh = (tileh * surf->bankh * surf_man->hw_info.num_banks) / surf->mtilea;
679 /* macro tile bytes */
680 mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb;
681
682 if (!start_level) {
683 unsigned alignment = MAX2(256, mtileb);
684 surf->bo_alignment = MAX2(surf->bo_alignment, alignment);
685
686 if (offset) {
687 offset = align64(offset, alignment);
688 }
689 }
690
691 /* build mipmap tree */
692 for (i = start_level; i <= surf->last_level; i++) {
693 level[i].mode = RADEON_SURF_MODE_2D;
694 eg_surf_minify(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, mtileb, offset);
695 if (level[i].mode == RADEON_SURF_MODE_1D) {
696 return eg_surface_init_1d(surf_man, surf, level, bpe, align_magnify, ~0, offset, i);
697 }
698 /* level0 and first mipmap need to have alignment */
699 offset = surf->bo_size;
700 if (i == 0) {
701 offset = align64(offset, surf->bo_alignment);
702 }
703 }
704 return 0;
705 }
706
707 static int eg_surface_sanity(struct radeon_surface_manager *surf_man,
708 struct radeon_surface *surf,
709 unsigned mode)
710 {
711 unsigned tileb;
712
713 /* check surface dimension */
714 if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) {
715 return -EINVAL;
716 }
717
718 /* check mipmap last_level */
719 if (surf->last_level > 15) {
720 return -EINVAL;
721 }
722
723 /* force 1d on kernel that can't do 2d */
724 if (!surf_man->hw_info.allow_2d && mode > RADEON_SURF_MODE_1D) {
725 if (surf->nsamples > 1) {
726 fprintf(stderr, "radeon: Cannot use 2D tiling for an MSAA surface (%i).\n", __LINE__);
727 return -EFAULT;
728 }
729 mode = RADEON_SURF_MODE_1D;
730 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
731 surf->flags |= RADEON_SURF_SET(mode, MODE);
732 }
733
734 /* check tile split */
735 if (mode == RADEON_SURF_MODE_2D) {
736 switch (surf->tile_split) {
737 case 64:
738 case 128:
739 case 256:
740 case 512:
741 case 1024:
742 case 2048:
743 case 4096:
744 break;
745 default:
746 return -EINVAL;
747 }
748 switch (surf->mtilea) {
749 case 1:
750 case 2:
751 case 4:
752 case 8:
753 break;
754 default:
755 return -EINVAL;
756 }
757 /* check aspect ratio */
758 if (surf_man->hw_info.num_banks < surf->mtilea) {
759 return -EINVAL;
760 }
761 /* check bank width */
762 switch (surf->bankw) {
763 case 1:
764 case 2:
765 case 4:
766 case 8:
767 break;
768 default:
769 return -EINVAL;
770 }
771 /* check bank height */
772 switch (surf->bankh) {
773 case 1:
774 case 2:
775 case 4:
776 case 8:
777 break;
778 default:
779 return -EINVAL;
780 }
781 tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples);
782 if ((tileb * surf->bankh * surf->bankw) < surf_man->hw_info.group_bytes) {
783 return -EINVAL;
784 }
785 }
786
787 return 0;
788 }
789
790 static int eg_surface_init_1d_miptrees(struct radeon_surface_manager *surf_man,
791 struct radeon_surface *surf)
792 {
793 unsigned zs_flags = RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER;
794 int r, is_depth_stencil = (surf->flags & zs_flags) == zs_flags;
795 /* Old libdrm_macros.headers didn't have stencil_level in it. This prevents crashes. */
796 struct radeon_surface_level tmp[RADEON_SURF_MAX_LEVEL];
797 struct radeon_surface_level *stencil_level =
798 (surf->flags & RADEON_SURF_HAS_SBUFFER_MIPTREE) ? surf->stencil_level : tmp;
799
800 /* With certain sizes the depth and the stencil texture end up being of
801 * different block sizes which later results in wrong rendering for npot
802 * textures. Inflate the alignment requirement for the depth surface by
803 * its bpe in order to make it allocate a texture that is of the same
804 * block size like the stencil texture (The rules used here are deducted by
805 * running dEQP tests and piglits) */
806 int magnify_align = is_depth_stencil &&
807 ((surf->npix_x < 32) ||
808 (!util_is_power_of_two_or_zero(surf->npix_x) &&
809 !surf->last_level));
810
811 r = eg_surface_init_1d(surf_man, surf, surf->level, surf->bpe,
812 magnify_align ? surf->bpe : 1,
813 magnify_align && surf->npix_x >= 4 && surf->npix_x < 32 ? 1 : ~0,
814 0, 0);
815 if (r)
816 return r;
817
818 if (is_depth_stencil) {
819 r = eg_surface_init_1d(surf_man, surf, stencil_level, 1, 1,
820 ~0,
821 surf->bo_size, 0);
822 surf->stencil_offset = stencil_level[0].offset;
823 }
824 return r;
825 }
826
827 static int eg_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man,
828 struct radeon_surface *surf)
829 {
830 unsigned zs_flags = RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER;
831 int r, is_depth_stencil = (surf->flags & zs_flags) == zs_flags;
832 /* Old libdrm_macros.headers didn't have stencil_level in it. This prevents crashes. */
833 struct radeon_surface_level tmp[RADEON_SURF_MAX_LEVEL];
834 struct radeon_surface_level *stencil_level =
835 (surf->flags & RADEON_SURF_HAS_SBUFFER_MIPTREE) ? surf->stencil_level : tmp;
836
837 /* Inflate the alignment requirement for npot textures to insure that
838 * stencil and depth texture have the same block size. Use this only in
839 * the 1d code path that uses the non-specific minify. */
840 int magnify_align = is_depth_stencil &&
841 ((surf->npix_x < 16) ||
842 (!util_is_power_of_two_or_zero(surf->npix_x) &&
843 !surf->last_level));
844
845 r = eg_surface_init_2d(surf_man, surf, surf->level, surf->bpe,
846 magnify_align ? surf->bpe : 1,
847 surf->tile_split, 0, 0);
848 if (r)
849 return r;
850
851 if (is_depth_stencil) {
852 r = eg_surface_init_2d(surf_man, surf, stencil_level, 1, 1,
853 surf->stencil_tile_split, surf->bo_size, 0);
854 surf->stencil_offset = stencil_level[0].offset;
855
856 }
857 return r;
858 }
859
860 static int eg_surface_init(struct radeon_surface_manager *surf_man,
861 struct radeon_surface *surf)
862 {
863 unsigned mode;
864 int r;
865
866 /* MSAA surfaces support the 2D mode only. */
867 if (surf->nsamples > 1) {
868 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
869 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
870 }
871
872 /* tiling mode */
873 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
874
875 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) {
876 /* zbuffer only support 1D or 2D tiled surface */
877 switch (mode) {
878 case RADEON_SURF_MODE_1D:
879 case RADEON_SURF_MODE_2D:
880 break;
881 default:
882 mode = RADEON_SURF_MODE_1D;
883 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
884 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
885 break;
886 }
887 }
888
889 r = eg_surface_sanity(surf_man, surf, mode);
890 if (r) {
891 return r;
892 }
893
894 surf->stencil_offset = 0;
895 surf->bo_alignment = 0;
896
897 /* check tiling mode */
898 switch (mode) {
899 case RADEON_SURF_MODE_LINEAR:
900 r = r6_surface_init_linear(surf_man, surf, 0, 0);
901 break;
902 case RADEON_SURF_MODE_LINEAR_ALIGNED:
903 r = r6_surface_init_linear_aligned(surf_man, surf, 0, 0);
904 break;
905 case RADEON_SURF_MODE_1D:
906 r = eg_surface_init_1d_miptrees(surf_man, surf);
907 break;
908 case RADEON_SURF_MODE_2D:
909 r = eg_surface_init_2d_miptrees(surf_man, surf);
910 break;
911 default:
912 return -EINVAL;
913 }
914 return r;
915 }
916
917 static unsigned log2_int(unsigned x)
918 {
919 unsigned l;
920
921 if (x < 2) {
922 return 0;
923 }
924 for (l = 2; ; l++) {
925 if ((unsigned)(1 << l) > x) {
926 return l - 1;
927 }
928 }
929 return 0;
930 }
931
932 /* compute best tile_split, bankw, bankh, mtilea
933 * depending on surface
934 */
935 static int eg_surface_best(struct radeon_surface_manager *surf_man,
936 struct radeon_surface *surf)
937 {
938 unsigned mode, tileb, h_over_w;
939 int r;
940
941 /* tiling mode */
942 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
943
944 /* set some default value to avoid sanity check choking on them */
945 surf->tile_split = 1024;
946 surf->bankw = 1;
947 surf->bankh = 1;
948 surf->mtilea = surf_man->hw_info.num_banks;
949 tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples);
950 for (; surf->bankh <= 8; surf->bankh *= 2) {
951 if ((tileb * surf->bankh * surf->bankw) >= surf_man->hw_info.group_bytes) {
952 break;
953 }
954 }
955 if (surf->mtilea > 8) {
956 surf->mtilea = 8;
957 }
958
959 r = eg_surface_sanity(surf_man, surf, mode);
960 if (r) {
961 return r;
962 }
963
964 if (mode != RADEON_SURF_MODE_2D) {
965 /* nothing to do for non 2D tiled surface */
966 return 0;
967 }
968
969 /* Tweak TILE_SPLIT for performance here. */
970 if (surf->nsamples > 1) {
971 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) {
972 switch (surf->nsamples) {
973 case 2:
974 surf->tile_split = 128;
975 break;
976 case 4:
977 surf->tile_split = 128;
978 break;
979 case 8:
980 surf->tile_split = 256;
981 break;
982 case 16: /* cayman only */
983 surf->tile_split = 512;
984 break;
985 default:
986 fprintf(stderr, "radeon: Wrong number of samples %i (%i)\n",
987 surf->nsamples, __LINE__);
988 return -EINVAL;
989 }
990 surf->stencil_tile_split = 64;
991 } else {
992 /* tile split must be >= 256 for colorbuffer surfaces,
993 * SAMPLE_SPLIT = tile_split / (bpe * 64), the optimal value is 2
994 */
995 surf->tile_split = MAX2(2 * surf->bpe * 64, 256);
996 if (surf->tile_split > 4096)
997 surf->tile_split = 4096;
998 }
999 } else {
1000 /* set tile split to row size */
1001 surf->tile_split = surf_man->hw_info.row_size;
1002 surf->stencil_tile_split = surf_man->hw_info.row_size / 2;
1003 }
1004
1005 /* bankw or bankh greater than 1 increase alignment requirement, not
1006 * sure if it's worth using smaller bankw & bankh to stick with 2D
1007 * tiling on small surface rather than falling back to 1D tiling.
1008 * Use recommended value based on tile size for now.
1009 *
1010 * fmask buffer has different optimal value figure them out once we
1011 * use it.
1012 */
1013 if (surf->flags & RADEON_SURF_SBUFFER) {
1014 /* assume 1 bytes for stencil, we optimize for stencil as stencil
1015 * and depth shares surface values
1016 */
1017 tileb = MIN2(surf->tile_split, 64 * surf->nsamples);
1018 } else {
1019 tileb = MIN2(surf->tile_split, 64 * surf->bpe * surf->nsamples);
1020 }
1021
1022 /* use bankw of 1 to minimize width alignment, might be interesting to
1023 * increase it for large surface
1024 */
1025 surf->bankw = 1;
1026 switch (tileb) {
1027 case 64:
1028 surf->bankh = 4;
1029 break;
1030 case 128:
1031 case 256:
1032 surf->bankh = 2;
1033 break;
1034 default:
1035 surf->bankh = 1;
1036 break;
1037 }
1038 /* double check the constraint */
1039 for (; surf->bankh <= 8; surf->bankh *= 2) {
1040 if ((tileb * surf->bankh * surf->bankw) >= surf_man->hw_info.group_bytes) {
1041 break;
1042 }
1043 }
1044
1045 h_over_w = (((surf->bankh * surf_man->hw_info.num_banks) << 16) /
1046 (surf->bankw * surf_man->hw_info.num_pipes)) >> 16;
1047 surf->mtilea = 1 << (log2_int(h_over_w) >> 1);
1048
1049 return 0;
1050 }
1051
1052
1053 /* ===========================================================================
1054 * Southern Islands family
1055 */
1056 #define SI__GB_TILE_MODE__PIPE_CONFIG(x) (((x) >> 6) & 0x1f)
1057 #define SI__PIPE_CONFIG__ADDR_SURF_P2 0
1058 #define SI__PIPE_CONFIG__ADDR_SURF_P4_8x16 4
1059 #define SI__PIPE_CONFIG__ADDR_SURF_P4_16x16 5
1060 #define SI__PIPE_CONFIG__ADDR_SURF_P4_16x32 6
1061 #define SI__PIPE_CONFIG__ADDR_SURF_P4_32x32 7
1062 #define SI__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16 8
1063 #define SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16 9
1064 #define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16 10
1065 #define SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16 11
1066 #define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16 12
1067 #define SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32 13
1068 #define SI__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32 14
1069 #define SI__GB_TILE_MODE__TILE_SPLIT(x) (((x) >> 11) & 0x7)
1070 #define SI__TILE_SPLIT__64B 0
1071 #define SI__TILE_SPLIT__128B 1
1072 #define SI__TILE_SPLIT__256B 2
1073 #define SI__TILE_SPLIT__512B 3
1074 #define SI__TILE_SPLIT__1024B 4
1075 #define SI__TILE_SPLIT__2048B 5
1076 #define SI__TILE_SPLIT__4096B 6
1077 #define SI__GB_TILE_MODE__BANK_WIDTH(x) (((x) >> 14) & 0x3)
1078 #define SI__BANK_WIDTH__1 0
1079 #define SI__BANK_WIDTH__2 1
1080 #define SI__BANK_WIDTH__4 2
1081 #define SI__BANK_WIDTH__8 3
1082 #define SI__GB_TILE_MODE__BANK_HEIGHT(x) (((x) >> 16) & 0x3)
1083 #define SI__BANK_HEIGHT__1 0
1084 #define SI__BANK_HEIGHT__2 1
1085 #define SI__BANK_HEIGHT__4 2
1086 #define SI__BANK_HEIGHT__8 3
1087 #define SI__GB_TILE_MODE__MACRO_TILE_ASPECT(x) (((x) >> 18) & 0x3)
1088 #define SI__MACRO_TILE_ASPECT__1 0
1089 #define SI__MACRO_TILE_ASPECT__2 1
1090 #define SI__MACRO_TILE_ASPECT__4 2
1091 #define SI__MACRO_TILE_ASPECT__8 3
1092 #define SI__GB_TILE_MODE__NUM_BANKS(x) (((x) >> 20) & 0x3)
1093 #define SI__NUM_BANKS__2_BANK 0
1094 #define SI__NUM_BANKS__4_BANK 1
1095 #define SI__NUM_BANKS__8_BANK 2
1096 #define SI__NUM_BANKS__16_BANK 3
1097
1098
1099 static void si_gb_tile_mode(uint32_t gb_tile_mode,
1100 unsigned *num_pipes,
1101 unsigned *num_banks,
1102 uint32_t *macro_tile_aspect,
1103 uint32_t *bank_w,
1104 uint32_t *bank_h,
1105 uint32_t *tile_split)
1106 {
1107 if (num_pipes) {
1108 switch (SI__GB_TILE_MODE__PIPE_CONFIG(gb_tile_mode)) {
1109 case SI__PIPE_CONFIG__ADDR_SURF_P2:
1110 default:
1111 *num_pipes = 2;
1112 break;
1113 case SI__PIPE_CONFIG__ADDR_SURF_P4_8x16:
1114 case SI__PIPE_CONFIG__ADDR_SURF_P4_16x16:
1115 case SI__PIPE_CONFIG__ADDR_SURF_P4_16x32:
1116 case SI__PIPE_CONFIG__ADDR_SURF_P4_32x32:
1117 *num_pipes = 4;
1118 break;
1119 case SI__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16:
1120 case SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16:
1121 case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16:
1122 case SI__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16:
1123 case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16:
1124 case SI__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32:
1125 case SI__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32:
1126 *num_pipes = 8;
1127 break;
1128 }
1129 }
1130 if (num_banks) {
1131 switch (SI__GB_TILE_MODE__NUM_BANKS(gb_tile_mode)) {
1132 default:
1133 case SI__NUM_BANKS__2_BANK:
1134 *num_banks = 2;
1135 break;
1136 case SI__NUM_BANKS__4_BANK:
1137 *num_banks = 4;
1138 break;
1139 case SI__NUM_BANKS__8_BANK:
1140 *num_banks = 8;
1141 break;
1142 case SI__NUM_BANKS__16_BANK:
1143 *num_banks = 16;
1144 break;
1145 }
1146 }
1147 if (macro_tile_aspect) {
1148 switch (SI__GB_TILE_MODE__MACRO_TILE_ASPECT(gb_tile_mode)) {
1149 default:
1150 case SI__MACRO_TILE_ASPECT__1:
1151 *macro_tile_aspect = 1;
1152 break;
1153 case SI__MACRO_TILE_ASPECT__2:
1154 *macro_tile_aspect = 2;
1155 break;
1156 case SI__MACRO_TILE_ASPECT__4:
1157 *macro_tile_aspect = 4;
1158 break;
1159 case SI__MACRO_TILE_ASPECT__8:
1160 *macro_tile_aspect = 8;
1161 break;
1162 }
1163 }
1164 if (bank_w) {
1165 switch (SI__GB_TILE_MODE__BANK_WIDTH(gb_tile_mode)) {
1166 default:
1167 case SI__BANK_WIDTH__1:
1168 *bank_w = 1;
1169 break;
1170 case SI__BANK_WIDTH__2:
1171 *bank_w = 2;
1172 break;
1173 case SI__BANK_WIDTH__4:
1174 *bank_w = 4;
1175 break;
1176 case SI__BANK_WIDTH__8:
1177 *bank_w = 8;
1178 break;
1179 }
1180 }
1181 if (bank_h) {
1182 switch (SI__GB_TILE_MODE__BANK_HEIGHT(gb_tile_mode)) {
1183 default:
1184 case SI__BANK_HEIGHT__1:
1185 *bank_h = 1;
1186 break;
1187 case SI__BANK_HEIGHT__2:
1188 *bank_h = 2;
1189 break;
1190 case SI__BANK_HEIGHT__4:
1191 *bank_h = 4;
1192 break;
1193 case SI__BANK_HEIGHT__8:
1194 *bank_h = 8;
1195 break;
1196 }
1197 }
1198 if (tile_split) {
1199 switch (SI__GB_TILE_MODE__TILE_SPLIT(gb_tile_mode)) {
1200 default:
1201 case SI__TILE_SPLIT__64B:
1202 *tile_split = 64;
1203 break;
1204 case SI__TILE_SPLIT__128B:
1205 *tile_split = 128;
1206 break;
1207 case SI__TILE_SPLIT__256B:
1208 *tile_split = 256;
1209 break;
1210 case SI__TILE_SPLIT__512B:
1211 *tile_split = 512;
1212 break;
1213 case SI__TILE_SPLIT__1024B:
1214 *tile_split = 1024;
1215 break;
1216 case SI__TILE_SPLIT__2048B:
1217 *tile_split = 2048;
1218 break;
1219 case SI__TILE_SPLIT__4096B:
1220 *tile_split = 4096;
1221 break;
1222 }
1223 }
1224 }
1225
1226 static int si_init_hw_info(struct radeon_surface_manager *surf_man)
1227 {
1228 uint32_t tiling_config;
1229 drmVersionPtr version;
1230 int r;
1231
1232 r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG,
1233 &tiling_config);
1234 if (r) {
1235 return r;
1236 }
1237
1238 surf_man->hw_info.allow_2d = 0;
1239 version = drmGetVersion(surf_man->fd);
1240 if (version && version->version_minor >= 33) {
1241 if (!radeon_get_value(surf_man->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, surf_man->hw_info.tile_mode_array)) {
1242 surf_man->hw_info.allow_2d = 1;
1243 }
1244 }
1245 drmFreeVersion(version);
1246
1247 switch (tiling_config & 0xf) {
1248 case 0:
1249 surf_man->hw_info.num_pipes = 1;
1250 break;
1251 case 1:
1252 surf_man->hw_info.num_pipes = 2;
1253 break;
1254 case 2:
1255 surf_man->hw_info.num_pipes = 4;
1256 break;
1257 case 3:
1258 surf_man->hw_info.num_pipes = 8;
1259 break;
1260 default:
1261 surf_man->hw_info.num_pipes = 8;
1262 surf_man->hw_info.allow_2d = 0;
1263 break;
1264 }
1265
1266 switch ((tiling_config & 0xf0) >> 4) {
1267 case 0:
1268 surf_man->hw_info.num_banks = 4;
1269 break;
1270 case 1:
1271 surf_man->hw_info.num_banks = 8;
1272 break;
1273 case 2:
1274 surf_man->hw_info.num_banks = 16;
1275 break;
1276 default:
1277 surf_man->hw_info.num_banks = 8;
1278 surf_man->hw_info.allow_2d = 0;
1279 break;
1280 }
1281
1282 switch ((tiling_config & 0xf00) >> 8) {
1283 case 0:
1284 surf_man->hw_info.group_bytes = 256;
1285 break;
1286 case 1:
1287 surf_man->hw_info.group_bytes = 512;
1288 break;
1289 default:
1290 surf_man->hw_info.group_bytes = 256;
1291 surf_man->hw_info.allow_2d = 0;
1292 break;
1293 }
1294
1295 switch ((tiling_config & 0xf000) >> 12) {
1296 case 0:
1297 surf_man->hw_info.row_size = 1024;
1298 break;
1299 case 1:
1300 surf_man->hw_info.row_size = 2048;
1301 break;
1302 case 2:
1303 surf_man->hw_info.row_size = 4096;
1304 break;
1305 default:
1306 surf_man->hw_info.row_size = 4096;
1307 surf_man->hw_info.allow_2d = 0;
1308 break;
1309 }
1310 return 0;
1311 }
1312
1313 static int si_surface_sanity(struct radeon_surface_manager *surf_man,
1314 struct radeon_surface *surf,
1315 unsigned mode, unsigned *tile_mode, unsigned *stencil_tile_mode)
1316 {
1317 uint32_t gb_tile_mode;
1318
1319 /* check surface dimension */
1320 if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) {
1321 return -EINVAL;
1322 }
1323
1324 /* check mipmap last_level */
1325 if (surf->last_level > 15) {
1326 return -EINVAL;
1327 }
1328
1329 /* force 1d on kernel that can't do 2d */
1330 if (mode > RADEON_SURF_MODE_1D &&
1331 (!surf_man->hw_info.allow_2d || !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX))) {
1332 if (surf->nsamples > 1) {
1333 fprintf(stderr, "radeon: Cannot use 1D tiling for an MSAA surface (%i).\n", __LINE__);
1334 return -EFAULT;
1335 }
1336 mode = RADEON_SURF_MODE_1D;
1337 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
1338 surf->flags |= RADEON_SURF_SET(mode, MODE);
1339 }
1340
1341 if (surf->nsamples > 1 && mode != RADEON_SURF_MODE_2D) {
1342 return -EINVAL;
1343 }
1344
1345 if (!surf->tile_split) {
1346 /* default value */
1347 surf->mtilea = 1;
1348 surf->bankw = 1;
1349 surf->bankh = 1;
1350 surf->tile_split = 64;
1351 surf->stencil_tile_split = 64;
1352 }
1353
1354 switch (mode) {
1355 case RADEON_SURF_MODE_2D:
1356 if (surf->flags & RADEON_SURF_SBUFFER) {
1357 switch (surf->nsamples) {
1358 case 1:
1359 *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D;
1360 break;
1361 case 2:
1362 *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_2AA;
1363 break;
1364 case 4:
1365 *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_4AA;
1366 break;
1367 case 8:
1368 *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_8AA;
1369 break;
1370 default:
1371 return -EINVAL;
1372 }
1373 /* retrieve tiling mode value */
1374 gb_tile_mode = surf_man->hw_info.tile_mode_array[*stencil_tile_mode];
1375 si_gb_tile_mode(gb_tile_mode, NULL, NULL, NULL, NULL, NULL, &surf->stencil_tile_split);
1376 }
1377 if (surf->flags & RADEON_SURF_ZBUFFER) {
1378 switch (surf->nsamples) {
1379 case 1:
1380 *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D;
1381 break;
1382 case 2:
1383 *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_2AA;
1384 break;
1385 case 4:
1386 *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_4AA;
1387 break;
1388 case 8:
1389 *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_2D_8AA;
1390 break;
1391 default:
1392 return -EINVAL;
1393 }
1394 } else if (surf->flags & RADEON_SURF_SCANOUT) {
1395 switch (surf->bpe) {
1396 case 2:
1397 *tile_mode = SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP;
1398 break;
1399 case 4:
1400 *tile_mode = SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP;
1401 break;
1402 default:
1403 return -EINVAL;
1404 }
1405 } else {
1406 switch (surf->bpe) {
1407 case 1:
1408 *tile_mode = SI_TILE_MODE_COLOR_2D_8BPP;
1409 break;
1410 case 2:
1411 *tile_mode = SI_TILE_MODE_COLOR_2D_16BPP;
1412 break;
1413 case 4:
1414 *tile_mode = SI_TILE_MODE_COLOR_2D_32BPP;
1415 break;
1416 case 8:
1417 case 16:
1418 *tile_mode = SI_TILE_MODE_COLOR_2D_64BPP;
1419 break;
1420 default:
1421 return -EINVAL;
1422 }
1423 }
1424 /* retrieve tiling mode value */
1425 gb_tile_mode = surf_man->hw_info.tile_mode_array[*tile_mode];
1426 si_gb_tile_mode(gb_tile_mode, NULL, NULL, &surf->mtilea, &surf->bankw, &surf->bankh, &surf->tile_split);
1427 break;
1428 case RADEON_SURF_MODE_1D:
1429 if (surf->flags & RADEON_SURF_SBUFFER) {
1430 *stencil_tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D;
1431 }
1432 if (surf->flags & RADEON_SURF_ZBUFFER) {
1433 *tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D;
1434 } else if (surf->flags & RADEON_SURF_SCANOUT) {
1435 *tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT;
1436 } else {
1437 *tile_mode = SI_TILE_MODE_COLOR_1D;
1438 }
1439 break;
1440 case RADEON_SURF_MODE_LINEAR_ALIGNED:
1441 default:
1442 *tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED;
1443 }
1444
1445 return 0;
1446 }
1447
1448 static void si_surf_minify(struct radeon_surface *surf,
1449 struct radeon_surface_level *surflevel,
1450 unsigned bpe, unsigned level,
1451 uint32_t xalign, uint32_t yalign, uint32_t zalign,
1452 uint32_t slice_align, uint64_t offset)
1453 {
1454 if (level == 0) {
1455 surflevel->npix_x = surf->npix_x;
1456 } else {
1457 surflevel->npix_x = mip_minify(next_power_of_two(surf->npix_x), level);
1458 }
1459 surflevel->npix_y = mip_minify(surf->npix_y, level);
1460 surflevel->npix_z = mip_minify(surf->npix_z, level);
1461
1462 if (level == 0 && surf->last_level > 0) {
1463 surflevel->nblk_x = (next_power_of_two(surflevel->npix_x) + surf->blk_w - 1) / surf->blk_w;
1464 surflevel->nblk_y = (next_power_of_two(surflevel->npix_y) + surf->blk_h - 1) / surf->blk_h;
1465 surflevel->nblk_z = (next_power_of_two(surflevel->npix_z) + surf->blk_d - 1) / surf->blk_d;
1466 } else {
1467 surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w;
1468 surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h;
1469 surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d;
1470 }
1471
1472 surflevel->nblk_y = align(surflevel->nblk_y, yalign);
1473
1474 /* XXX: Texture sampling uses unexpectedly large pitches in some cases,
1475 * these are just guesses for the rules behind those
1476 */
1477 if (level == 0 && surf->last_level == 0)
1478 /* Non-mipmap pitch padded to slice alignment */
1479 /* Using just bpe here breaks stencil blitting; surf->bpe works. */
1480 xalign = MAX2(xalign, slice_align / surf->bpe);
1481 else if (surflevel->mode == RADEON_SURF_MODE_LINEAR_ALIGNED)
1482 /* Small rows evenly distributed across slice */
1483 xalign = MAX2(xalign, slice_align / bpe / surflevel->nblk_y);
1484
1485 surflevel->nblk_x = align(surflevel->nblk_x, xalign);
1486 surflevel->nblk_z = align(surflevel->nblk_z, zalign);
1487
1488 surflevel->offset = offset;
1489 surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples;
1490 surflevel->slice_size = align64((uint64_t)surflevel->pitch_bytes * surflevel->nblk_y,
1491 (uint64_t)slice_align);
1492
1493 surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size;
1494 }
1495
1496 static void si_surf_minify_2d(struct radeon_surface *surf,
1497 struct radeon_surface_level *surflevel,
1498 unsigned bpe, unsigned level, unsigned slice_pt,
1499 uint32_t xalign, uint32_t yalign, uint32_t zalign,
1500 unsigned mtileb, uint64_t offset)
1501 {
1502 unsigned mtile_pr, mtile_ps;
1503
1504 if (level == 0) {
1505 surflevel->npix_x = surf->npix_x;
1506 } else {
1507 surflevel->npix_x = mip_minify(next_power_of_two(surf->npix_x), level);
1508 }
1509 surflevel->npix_y = mip_minify(surf->npix_y, level);
1510 surflevel->npix_z = mip_minify(surf->npix_z, level);
1511
1512 if (level == 0 && surf->last_level > 0) {
1513 surflevel->nblk_x = (next_power_of_two(surflevel->npix_x) + surf->blk_w - 1) / surf->blk_w;
1514 surflevel->nblk_y = (next_power_of_two(surflevel->npix_y) + surf->blk_h - 1) / surf->blk_h;
1515 surflevel->nblk_z = (next_power_of_two(surflevel->npix_z) + surf->blk_d - 1) / surf->blk_d;
1516 } else {
1517 surflevel->nblk_x = (surflevel->npix_x + surf->blk_w - 1) / surf->blk_w;
1518 surflevel->nblk_y = (surflevel->npix_y + surf->blk_h - 1) / surf->blk_h;
1519 surflevel->nblk_z = (surflevel->npix_z + surf->blk_d - 1) / surf->blk_d;
1520 }
1521
1522 if (surf->nsamples == 1 && surflevel->mode == RADEON_SURF_MODE_2D &&
1523 !(surf->flags & RADEON_SURF_FMASK)) {
1524 if (surflevel->nblk_x < xalign || surflevel->nblk_y < yalign) {
1525 surflevel->mode = RADEON_SURF_MODE_1D;
1526 return;
1527 }
1528 }
1529 surflevel->nblk_x = align(surflevel->nblk_x, xalign);
1530 surflevel->nblk_y = align(surflevel->nblk_y, yalign);
1531 surflevel->nblk_z = align(surflevel->nblk_z, zalign);
1532
1533 /* macro tile per row */
1534 mtile_pr = surflevel->nblk_x / xalign;
1535 /* macro tile per slice */
1536 mtile_ps = (mtile_pr * surflevel->nblk_y) / yalign;
1537 surflevel->offset = offset;
1538 surflevel->pitch_bytes = surflevel->nblk_x * bpe * surf->nsamples;
1539 surflevel->slice_size = (uint64_t)mtile_ps * mtileb * slice_pt;
1540
1541 surf->bo_size = offset + surflevel->slice_size * surflevel->nblk_z * surf->array_size;
1542 }
1543
1544 static int si_surface_init_linear_aligned(struct radeon_surface_manager *surf_man,
1545 struct radeon_surface *surf,
1546 unsigned tile_mode,
1547 uint64_t offset, unsigned start_level)
1548 {
1549 uint32_t xalign, yalign, zalign, slice_align;
1550 unsigned i;
1551
1552 /* compute alignment */
1553 if (!start_level) {
1554 surf->bo_alignment = MAX2(256, surf_man->hw_info.group_bytes);
1555 }
1556 xalign = MAX2(8, 64 / surf->bpe);
1557 yalign = 1;
1558 zalign = 1;
1559 slice_align = MAX2(64 * surf->bpe, surf_man->hw_info.group_bytes);
1560
1561 /* build mipmap tree */
1562 for (i = start_level; i <= surf->last_level; i++) {
1563 surf->level[i].mode = RADEON_SURF_MODE_LINEAR_ALIGNED;
1564 si_surf_minify(surf, surf->level+i, surf->bpe, i, xalign, yalign, zalign, slice_align, offset);
1565 /* level0 and first mipmap need to have alignment */
1566 offset = surf->bo_size;
1567 if (i == 0) {
1568 offset = align64(offset, surf->bo_alignment);
1569 }
1570 if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) {
1571 surf->tiling_index[i] = tile_mode;
1572 }
1573 }
1574 return 0;
1575 }
1576
1577 static int si_surface_init_1d(struct radeon_surface_manager *surf_man,
1578 struct radeon_surface *surf,
1579 struct radeon_surface_level *level,
1580 unsigned bpe, unsigned tile_mode,
1581 uint64_t offset, unsigned start_level)
1582 {
1583 uint32_t xalign, yalign, zalign, slice_align;
1584 unsigned alignment = MAX2(256, surf_man->hw_info.group_bytes);
1585 unsigned i;
1586
1587 /* compute alignment */
1588 xalign = 8;
1589 yalign = 8;
1590 zalign = 1;
1591 slice_align = surf_man->hw_info.group_bytes;
1592 if (surf->flags & RADEON_SURF_SCANOUT) {
1593 xalign = MAX2((bpe == 1) ? 64 : 32, xalign);
1594 }
1595
1596 if (start_level <= 1) {
1597 surf->bo_alignment = MAX2(surf->bo_alignment, alignment);
1598
1599 if (offset) {
1600 offset = align64(offset, alignment);
1601 }
1602 }
1603
1604 /* build mipmap tree */
1605 for (i = start_level; i <= surf->last_level; i++) {
1606 level[i].mode = RADEON_SURF_MODE_1D;
1607 si_surf_minify(surf, level+i, bpe, i, xalign, yalign, zalign, slice_align, offset);
1608 /* level0 and first mipmap need to have alignment */
1609 offset = surf->bo_size;
1610 if (i == 0) {
1611 offset = align64(offset, alignment);
1612 }
1613 if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) {
1614 if (surf->level == level) {
1615 surf->tiling_index[i] = tile_mode;
1616 /* it's ok because stencil is done after */
1617 surf->stencil_tiling_index[i] = tile_mode;
1618 } else {
1619 surf->stencil_tiling_index[i] = tile_mode;
1620 }
1621 }
1622 }
1623 return 0;
1624 }
1625
1626 static int si_surface_init_1d_miptrees(struct radeon_surface_manager *surf_man,
1627 struct radeon_surface *surf,
1628 unsigned tile_mode, unsigned stencil_tile_mode)
1629 {
1630 int r;
1631
1632 r = si_surface_init_1d(surf_man, surf, surf->level, surf->bpe, tile_mode, 0, 0);
1633 if (r) {
1634 return r;
1635 }
1636
1637 if (surf->flags & RADEON_SURF_SBUFFER) {
1638 r = si_surface_init_1d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode, surf->bo_size, 0);
1639 surf->stencil_offset = surf->stencil_level[0].offset;
1640 }
1641 return r;
1642 }
1643
1644 static int si_surface_init_2d(struct radeon_surface_manager *surf_man,
1645 struct radeon_surface *surf,
1646 struct radeon_surface_level *level,
1647 unsigned bpe, unsigned tile_mode,
1648 unsigned num_pipes, unsigned num_banks,
1649 unsigned tile_split,
1650 uint64_t offset,
1651 unsigned start_level)
1652 {
1653 uint64_t aligned_offset = offset;
1654 unsigned tilew, tileh, tileb;
1655 unsigned mtilew, mtileh, mtileb;
1656 unsigned slice_pt;
1657 unsigned i;
1658
1659 /* compute tile values */
1660 tilew = 8;
1661 tileh = 8;
1662 tileb = tilew * tileh * bpe * surf->nsamples;
1663 /* slices per tile */
1664 slice_pt = 1;
1665 if (tileb > tile_split && tile_split) {
1666 slice_pt = tileb / tile_split;
1667 }
1668 tileb = tileb / slice_pt;
1669
1670 /* macro tile width & height */
1671 mtilew = (tilew * surf->bankw * num_pipes) * surf->mtilea;
1672 mtileh = (tileh * surf->bankh * num_banks) / surf->mtilea;
1673
1674 /* macro tile bytes */
1675 mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb;
1676
1677 if (start_level <= 1) {
1678 unsigned alignment = MAX2(256, mtileb);
1679 surf->bo_alignment = MAX2(surf->bo_alignment, alignment);
1680
1681 if (aligned_offset) {
1682 aligned_offset = align64(aligned_offset, alignment);
1683 }
1684 }
1685
1686 /* build mipmap tree */
1687 for (i = start_level; i <= surf->last_level; i++) {
1688 level[i].mode = RADEON_SURF_MODE_2D;
1689 si_surf_minify_2d(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, 1, mtileb, aligned_offset);
1690 if (level[i].mode == RADEON_SURF_MODE_1D) {
1691 switch (tile_mode) {
1692 case SI_TILE_MODE_COLOR_2D_8BPP:
1693 case SI_TILE_MODE_COLOR_2D_16BPP:
1694 case SI_TILE_MODE_COLOR_2D_32BPP:
1695 case SI_TILE_MODE_COLOR_2D_64BPP:
1696 tile_mode = SI_TILE_MODE_COLOR_1D;
1697 break;
1698 case SI_TILE_MODE_COLOR_2D_SCANOUT_16BPP:
1699 case SI_TILE_MODE_COLOR_2D_SCANOUT_32BPP:
1700 tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT;
1701 break;
1702 case SI_TILE_MODE_DEPTH_STENCIL_2D:
1703 tile_mode = SI_TILE_MODE_DEPTH_STENCIL_1D;
1704 break;
1705 default:
1706 return -EINVAL;
1707 }
1708 return si_surface_init_1d(surf_man, surf, level, bpe, tile_mode, offset, i);
1709 }
1710 /* level0 and first mipmap need to have alignment */
1711 aligned_offset = offset = surf->bo_size;
1712 if (i == 0) {
1713 aligned_offset = align64(aligned_offset, surf->bo_alignment);
1714 }
1715 if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) {
1716 if (surf->level == level) {
1717 surf->tiling_index[i] = tile_mode;
1718 /* it's ok because stencil is done after */
1719 surf->stencil_tiling_index[i] = tile_mode;
1720 } else {
1721 surf->stencil_tiling_index[i] = tile_mode;
1722 }
1723 }
1724 }
1725 return 0;
1726 }
1727
1728 static int si_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man,
1729 struct radeon_surface *surf,
1730 unsigned tile_mode, unsigned stencil_tile_mode)
1731 {
1732 unsigned num_pipes, num_banks;
1733 uint32_t gb_tile_mode;
1734 int r;
1735
1736 /* retrieve tiling mode value */
1737 gb_tile_mode = surf_man->hw_info.tile_mode_array[tile_mode];
1738 si_gb_tile_mode(gb_tile_mode, &num_pipes, &num_banks, NULL, NULL, NULL, NULL);
1739
1740 r = si_surface_init_2d(surf_man, surf, surf->level, surf->bpe, tile_mode, num_pipes, num_banks, surf->tile_split, 0, 0);
1741 if (r) {
1742 return r;
1743 }
1744
1745 if (surf->flags & RADEON_SURF_SBUFFER) {
1746 r = si_surface_init_2d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode, num_pipes, num_banks, surf->stencil_tile_split, surf->bo_size, 0);
1747 surf->stencil_offset = surf->stencil_level[0].offset;
1748 }
1749 return r;
1750 }
1751
1752 static int si_surface_init(struct radeon_surface_manager *surf_man,
1753 struct radeon_surface *surf)
1754 {
1755 unsigned mode, tile_mode, stencil_tile_mode = 0;
1756 int r;
1757
1758 /* MSAA surfaces support the 2D mode only. */
1759 if (surf->nsamples > 1) {
1760 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
1761 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
1762 }
1763
1764 /* tiling mode */
1765 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
1766
1767 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) {
1768 /* zbuffer only support 1D or 2D tiled surface */
1769 switch (mode) {
1770 case RADEON_SURF_MODE_1D:
1771 case RADEON_SURF_MODE_2D:
1772 break;
1773 default:
1774 mode = RADEON_SURF_MODE_1D;
1775 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
1776 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
1777 break;
1778 }
1779 }
1780
1781 r = si_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode);
1782 if (r) {
1783 return r;
1784 }
1785
1786 surf->stencil_offset = 0;
1787 surf->bo_alignment = 0;
1788
1789 /* check tiling mode */
1790 switch (mode) {
1791 case RADEON_SURF_MODE_LINEAR:
1792 r = r6_surface_init_linear(surf_man, surf, 0, 0);
1793 break;
1794 case RADEON_SURF_MODE_LINEAR_ALIGNED:
1795 r = si_surface_init_linear_aligned(surf_man, surf, tile_mode, 0, 0);
1796 break;
1797 case RADEON_SURF_MODE_1D:
1798 r = si_surface_init_1d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode);
1799 break;
1800 case RADEON_SURF_MODE_2D:
1801 r = si_surface_init_2d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode);
1802 break;
1803 default:
1804 return -EINVAL;
1805 }
1806 return r;
1807 }
1808
1809 /*
1810 * depending on surface
1811 */
1812 static int si_surface_best(struct radeon_surface_manager *surf_man,
1813 struct radeon_surface *surf)
1814 {
1815 unsigned mode, tile_mode, stencil_tile_mode;
1816
1817 /* tiling mode */
1818 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
1819
1820 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER) &&
1821 !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX)) {
1822 /* depth/stencil force 1d tiling for old mesa */
1823 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
1824 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
1825 }
1826
1827 return si_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode);
1828 }
1829
1830
1831 /* ===========================================================================
1832 * Sea Islands family
1833 */
1834 #define CIK__GB_TILE_MODE__PIPE_CONFIG(x) (((x) >> 6) & 0x1f)
1835 #define CIK__PIPE_CONFIG__ADDR_SURF_P2 0
1836 #define CIK__PIPE_CONFIG__ADDR_SURF_P4_8x16 4
1837 #define CIK__PIPE_CONFIG__ADDR_SURF_P4_16x16 5
1838 #define CIK__PIPE_CONFIG__ADDR_SURF_P4_16x32 6
1839 #define CIK__PIPE_CONFIG__ADDR_SURF_P4_32x32 7
1840 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16 8
1841 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16 9
1842 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16 10
1843 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16 11
1844 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16 12
1845 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32 13
1846 #define CIK__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32 14
1847 #define CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_8X16 16
1848 #define CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_16X16 17
1849 #define CIK__GB_TILE_MODE__TILE_SPLIT(x) (((x) >> 11) & 0x7)
1850 #define CIK__TILE_SPLIT__64B 0
1851 #define CIK__TILE_SPLIT__128B 1
1852 #define CIK__TILE_SPLIT__256B 2
1853 #define CIK__TILE_SPLIT__512B 3
1854 #define CIK__TILE_SPLIT__1024B 4
1855 #define CIK__TILE_SPLIT__2048B 5
1856 #define CIK__TILE_SPLIT__4096B 6
1857 #define CIK__GB_TILE_MODE__SAMPLE_SPLIT(x) (((x) >> 25) & 0x3)
1858 #define CIK__SAMPLE_SPLIT__1 0
1859 #define CIK__SAMPLE_SPLIT__2 1
1860 #define CIK__SAMPLE_SPLIT__4 2
1861 #define CIK__SAMPLE_SPLIT__8 3
1862 #define CIK__GB_MACROTILE_MODE__BANK_WIDTH(x) ((x) & 0x3)
1863 #define CIK__BANK_WIDTH__1 0
1864 #define CIK__BANK_WIDTH__2 1
1865 #define CIK__BANK_WIDTH__4 2
1866 #define CIK__BANK_WIDTH__8 3
1867 #define CIK__GB_MACROTILE_MODE__BANK_HEIGHT(x) (((x) >> 2) & 0x3)
1868 #define CIK__BANK_HEIGHT__1 0
1869 #define CIK__BANK_HEIGHT__2 1
1870 #define CIK__BANK_HEIGHT__4 2
1871 #define CIK__BANK_HEIGHT__8 3
1872 #define CIK__GB_MACROTILE_MODE__MACRO_TILE_ASPECT(x) (((x) >> 4) & 0x3)
1873 #define CIK__MACRO_TILE_ASPECT__1 0
1874 #define CIK__MACRO_TILE_ASPECT__2 1
1875 #define CIK__MACRO_TILE_ASPECT__4 2
1876 #define CIK__MACRO_TILE_ASPECT__8 3
1877 #define CIK__GB_MACROTILE_MODE__NUM_BANKS(x) (((x) >> 6) & 0x3)
1878 #define CIK__NUM_BANKS__2_BANK 0
1879 #define CIK__NUM_BANKS__4_BANK 1
1880 #define CIK__NUM_BANKS__8_BANK 2
1881 #define CIK__NUM_BANKS__16_BANK 3
1882
1883
1884 static void cik_get_2d_params(struct radeon_surface_manager *surf_man,
1885 unsigned bpe, unsigned nsamples, bool is_color,
1886 unsigned tile_mode,
1887 uint32_t *num_pipes,
1888 uint32_t *tile_split_ptr,
1889 uint32_t *num_banks,
1890 uint32_t *macro_tile_aspect,
1891 uint32_t *bank_w,
1892 uint32_t *bank_h)
1893 {
1894 uint32_t gb_tile_mode = surf_man->hw_info.tile_mode_array[tile_mode];
1895 unsigned tileb_1x, tileb;
1896 unsigned gb_macrotile_mode;
1897 unsigned macrotile_index;
1898 unsigned tile_split, sample_split;
1899
1900 if (num_pipes) {
1901 switch (CIK__GB_TILE_MODE__PIPE_CONFIG(gb_tile_mode)) {
1902 case CIK__PIPE_CONFIG__ADDR_SURF_P2:
1903 default:
1904 *num_pipes = 2;
1905 break;
1906 case CIK__PIPE_CONFIG__ADDR_SURF_P4_8x16:
1907 case CIK__PIPE_CONFIG__ADDR_SURF_P4_16x16:
1908 case CIK__PIPE_CONFIG__ADDR_SURF_P4_16x32:
1909 case CIK__PIPE_CONFIG__ADDR_SURF_P4_32x32:
1910 *num_pipes = 4;
1911 break;
1912 case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x16_8x16:
1913 case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_8x16:
1914 case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_8x16:
1915 case CIK__PIPE_CONFIG__ADDR_SURF_P8_16x32_16x16:
1916 case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x16:
1917 case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x32_16x32:
1918 case CIK__PIPE_CONFIG__ADDR_SURF_P8_32x64_32x32:
1919 *num_pipes = 8;
1920 break;
1921 case CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_8X16:
1922 case CIK__PIPE_CONFIG__ADDR_SURF_P16_32X32_16X16:
1923 *num_pipes = 16;
1924 break;
1925 }
1926 }
1927 switch (CIK__GB_TILE_MODE__TILE_SPLIT(gb_tile_mode)) {
1928 default:
1929 case CIK__TILE_SPLIT__64B:
1930 tile_split = 64;
1931 break;
1932 case CIK__TILE_SPLIT__128B:
1933 tile_split = 128;
1934 break;
1935 case CIK__TILE_SPLIT__256B:
1936 tile_split = 256;
1937 break;
1938 case CIK__TILE_SPLIT__512B:
1939 tile_split = 512;
1940 break;
1941 case CIK__TILE_SPLIT__1024B:
1942 tile_split = 1024;
1943 break;
1944 case CIK__TILE_SPLIT__2048B:
1945 tile_split = 2048;
1946 break;
1947 case CIK__TILE_SPLIT__4096B:
1948 tile_split = 4096;
1949 break;
1950 }
1951 switch (CIK__GB_TILE_MODE__SAMPLE_SPLIT(gb_tile_mode)) {
1952 default:
1953 case CIK__SAMPLE_SPLIT__1:
1954 sample_split = 1;
1955 break;
1956 case CIK__SAMPLE_SPLIT__2:
1957 sample_split = 2;
1958 break;
1959 case CIK__SAMPLE_SPLIT__4:
1960 sample_split = 4;
1961 break;
1962 case CIK__SAMPLE_SPLIT__8:
1963 sample_split = 8;
1964 break;
1965 }
1966
1967 /* Adjust the tile split. */
1968 tileb_1x = 8 * 8 * bpe;
1969 if (is_color) {
1970 tile_split = MAX2(256, sample_split * tileb_1x);
1971 }
1972 tile_split = MIN2(surf_man->hw_info.row_size, tile_split);
1973
1974 /* Determine the macrotile index. */
1975 tileb = MIN2(tile_split, nsamples * tileb_1x);
1976
1977 for (macrotile_index = 0; tileb > 64; macrotile_index++) {
1978 tileb >>= 1;
1979 }
1980 gb_macrotile_mode = surf_man->hw_info.macrotile_mode_array[macrotile_index];
1981
1982 if (tile_split_ptr) {
1983 *tile_split_ptr = tile_split;
1984 }
1985 if (num_banks) {
1986 switch (CIK__GB_MACROTILE_MODE__NUM_BANKS(gb_macrotile_mode)) {
1987 default:
1988 case CIK__NUM_BANKS__2_BANK:
1989 *num_banks = 2;
1990 break;
1991 case CIK__NUM_BANKS__4_BANK:
1992 *num_banks = 4;
1993 break;
1994 case CIK__NUM_BANKS__8_BANK:
1995 *num_banks = 8;
1996 break;
1997 case CIK__NUM_BANKS__16_BANK:
1998 *num_banks = 16;
1999 break;
2000 }
2001 }
2002 if (macro_tile_aspect) {
2003 switch (CIK__GB_MACROTILE_MODE__MACRO_TILE_ASPECT(gb_macrotile_mode)) {
2004 default:
2005 case CIK__MACRO_TILE_ASPECT__1:
2006 *macro_tile_aspect = 1;
2007 break;
2008 case CIK__MACRO_TILE_ASPECT__2:
2009 *macro_tile_aspect = 2;
2010 break;
2011 case CIK__MACRO_TILE_ASPECT__4:
2012 *macro_tile_aspect = 4;
2013 break;
2014 case CIK__MACRO_TILE_ASPECT__8:
2015 *macro_tile_aspect = 8;
2016 break;
2017 }
2018 }
2019 if (bank_w) {
2020 switch (CIK__GB_MACROTILE_MODE__BANK_WIDTH(gb_macrotile_mode)) {
2021 default:
2022 case CIK__BANK_WIDTH__1:
2023 *bank_w = 1;
2024 break;
2025 case CIK__BANK_WIDTH__2:
2026 *bank_w = 2;
2027 break;
2028 case CIK__BANK_WIDTH__4:
2029 *bank_w = 4;
2030 break;
2031 case CIK__BANK_WIDTH__8:
2032 *bank_w = 8;
2033 break;
2034 }
2035 }
2036 if (bank_h) {
2037 switch (CIK__GB_MACROTILE_MODE__BANK_HEIGHT(gb_macrotile_mode)) {
2038 default:
2039 case CIK__BANK_HEIGHT__1:
2040 *bank_h = 1;
2041 break;
2042 case CIK__BANK_HEIGHT__2:
2043 *bank_h = 2;
2044 break;
2045 case CIK__BANK_HEIGHT__4:
2046 *bank_h = 4;
2047 break;
2048 case CIK__BANK_HEIGHT__8:
2049 *bank_h = 8;
2050 break;
2051 }
2052 }
2053 }
2054
2055 static int cik_init_hw_info(struct radeon_surface_manager *surf_man)
2056 {
2057 uint32_t tiling_config;
2058 drmVersionPtr version;
2059 int r;
2060
2061 r = radeon_get_value(surf_man->fd, RADEON_INFO_TILING_CONFIG,
2062 &tiling_config);
2063 if (r) {
2064 return r;
2065 }
2066
2067 surf_man->hw_info.allow_2d = 0;
2068 version = drmGetVersion(surf_man->fd);
2069 if (version && version->version_minor >= 35) {
2070 if (!radeon_get_value(surf_man->fd, RADEON_INFO_SI_TILE_MODE_ARRAY, surf_man->hw_info.tile_mode_array) &&
2071 !radeon_get_value(surf_man->fd, RADEON_INFO_CIK_MACROTILE_MODE_ARRAY, surf_man->hw_info.macrotile_mode_array)) {
2072 surf_man->hw_info.allow_2d = 1;
2073 }
2074 }
2075 drmFreeVersion(version);
2076
2077 switch (tiling_config & 0xf) {
2078 case 0:
2079 surf_man->hw_info.num_pipes = 1;
2080 break;
2081 case 1:
2082 surf_man->hw_info.num_pipes = 2;
2083 break;
2084 case 2:
2085 surf_man->hw_info.num_pipes = 4;
2086 break;
2087 case 3:
2088 surf_man->hw_info.num_pipes = 8;
2089 break;
2090 default:
2091 surf_man->hw_info.num_pipes = 8;
2092 surf_man->hw_info.allow_2d = 0;
2093 break;
2094 }
2095
2096 switch ((tiling_config & 0xf0) >> 4) {
2097 case 0:
2098 surf_man->hw_info.num_banks = 4;
2099 break;
2100 case 1:
2101 surf_man->hw_info.num_banks = 8;
2102 break;
2103 case 2:
2104 surf_man->hw_info.num_banks = 16;
2105 break;
2106 default:
2107 surf_man->hw_info.num_banks = 8;
2108 surf_man->hw_info.allow_2d = 0;
2109 break;
2110 }
2111
2112 switch ((tiling_config & 0xf00) >> 8) {
2113 case 0:
2114 surf_man->hw_info.group_bytes = 256;
2115 break;
2116 case 1:
2117 surf_man->hw_info.group_bytes = 512;
2118 break;
2119 default:
2120 surf_man->hw_info.group_bytes = 256;
2121 surf_man->hw_info.allow_2d = 0;
2122 break;
2123 }
2124
2125 switch ((tiling_config & 0xf000) >> 12) {
2126 case 0:
2127 surf_man->hw_info.row_size = 1024;
2128 break;
2129 case 1:
2130 surf_man->hw_info.row_size = 2048;
2131 break;
2132 case 2:
2133 surf_man->hw_info.row_size = 4096;
2134 break;
2135 default:
2136 surf_man->hw_info.row_size = 4096;
2137 surf_man->hw_info.allow_2d = 0;
2138 break;
2139 }
2140 return 0;
2141 }
2142
2143 static int cik_surface_sanity(struct radeon_surface_manager *surf_man,
2144 struct radeon_surface *surf,
2145 unsigned mode, unsigned *tile_mode, unsigned *stencil_tile_mode)
2146 {
2147 /* check surface dimension */
2148 if (surf->npix_x > 16384 || surf->npix_y > 16384 || surf->npix_z > 16384) {
2149 return -EINVAL;
2150 }
2151
2152 /* check mipmap last_level */
2153 if (surf->last_level > 15) {
2154 return -EINVAL;
2155 }
2156
2157 /* force 1d on kernel that can't do 2d */
2158 if (mode > RADEON_SURF_MODE_1D &&
2159 (!surf_man->hw_info.allow_2d || !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX))) {
2160 if (surf->nsamples > 1) {
2161 fprintf(stderr, "radeon: Cannot use 1D tiling for an MSAA surface (%i).\n", __LINE__);
2162 return -EFAULT;
2163 }
2164 mode = RADEON_SURF_MODE_1D;
2165 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
2166 surf->flags |= RADEON_SURF_SET(mode, MODE);
2167 }
2168
2169 if (surf->nsamples > 1 && mode != RADEON_SURF_MODE_2D) {
2170 return -EINVAL;
2171 }
2172
2173 if (!surf->tile_split) {
2174 /* default value */
2175 surf->mtilea = 1;
2176 surf->bankw = 1;
2177 surf->bankh = 1;
2178 surf->tile_split = 64;
2179 surf->stencil_tile_split = 64;
2180 }
2181
2182 switch (mode) {
2183 case RADEON_SURF_MODE_2D: {
2184 if (surf->flags & RADEON_SURF_Z_OR_SBUFFER) {
2185 switch (surf->nsamples) {
2186 case 1:
2187 *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64;
2188 break;
2189 case 2:
2190 case 4:
2191 *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128;
2192 break;
2193 case 8:
2194 *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256;
2195 break;
2196 default:
2197 return -EINVAL;
2198 }
2199
2200 if (surf->flags & RADEON_SURF_SBUFFER) {
2201 *stencil_tile_mode = *tile_mode;
2202
2203 cik_get_2d_params(surf_man, 1, surf->nsamples, false,
2204 *stencil_tile_mode, NULL,
2205 &surf->stencil_tile_split,
2206 NULL, NULL, NULL, NULL);
2207 }
2208 } else if (surf->flags & RADEON_SURF_SCANOUT) {
2209 *tile_mode = CIK_TILE_MODE_COLOR_2D_SCANOUT;
2210 } else {
2211 *tile_mode = CIK_TILE_MODE_COLOR_2D;
2212 }
2213
2214 /* retrieve tiling mode values */
2215 cik_get_2d_params(surf_man, surf->bpe, surf->nsamples,
2216 !(surf->flags & RADEON_SURF_Z_OR_SBUFFER), *tile_mode,
2217 NULL, &surf->tile_split, NULL, &surf->mtilea,
2218 &surf->bankw, &surf->bankh);
2219 break;
2220 }
2221 case RADEON_SURF_MODE_1D:
2222 if (surf->flags & RADEON_SURF_SBUFFER) {
2223 *stencil_tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D;
2224 }
2225 if (surf->flags & RADEON_SURF_ZBUFFER) {
2226 *tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D;
2227 } else if (surf->flags & RADEON_SURF_SCANOUT) {
2228 *tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT;
2229 } else {
2230 *tile_mode = SI_TILE_MODE_COLOR_1D;
2231 }
2232 break;
2233 case RADEON_SURF_MODE_LINEAR_ALIGNED:
2234 default:
2235 *stencil_tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED;
2236 *tile_mode = SI_TILE_MODE_COLOR_LINEAR_ALIGNED;
2237 }
2238
2239 return 0;
2240 }
2241
2242 static int cik_surface_init_2d(struct radeon_surface_manager *surf_man,
2243 struct radeon_surface *surf,
2244 struct radeon_surface_level *level,
2245 unsigned bpe, unsigned tile_mode,
2246 unsigned tile_split,
2247 unsigned num_pipes, unsigned num_banks,
2248 uint64_t offset,
2249 unsigned start_level)
2250 {
2251 uint64_t aligned_offset = offset;
2252 unsigned tilew, tileh, tileb_1x, tileb;
2253 unsigned mtilew, mtileh, mtileb;
2254 unsigned slice_pt;
2255 unsigned i;
2256
2257 /* compute tile values */
2258 tilew = 8;
2259 tileh = 8;
2260 tileb_1x = tilew * tileh * bpe;
2261
2262 tile_split = MIN2(surf_man->hw_info.row_size, tile_split);
2263
2264 tileb = surf->nsamples * tileb_1x;
2265
2266 /* slices per tile */
2267 slice_pt = 1;
2268 if (tileb > tile_split && tile_split) {
2269 slice_pt = tileb / tile_split;
2270 tileb = tileb / slice_pt;
2271 }
2272
2273 /* macro tile width & height */
2274 mtilew = (tilew * surf->bankw * num_pipes) * surf->mtilea;
2275 mtileh = (tileh * surf->bankh * num_banks) / surf->mtilea;
2276
2277 /* macro tile bytes */
2278 mtileb = (mtilew / tilew) * (mtileh / tileh) * tileb;
2279
2280 if (start_level <= 1) {
2281 unsigned alignment = MAX2(256, mtileb);
2282 surf->bo_alignment = MAX2(surf->bo_alignment, alignment);
2283
2284 if (aligned_offset) {
2285 aligned_offset = align64(aligned_offset, alignment);
2286 }
2287 }
2288
2289 /* build mipmap tree */
2290 for (i = start_level; i <= surf->last_level; i++) {
2291 level[i].mode = RADEON_SURF_MODE_2D;
2292 si_surf_minify_2d(surf, level+i, bpe, i, slice_pt, mtilew, mtileh, 1, mtileb, aligned_offset);
2293 if (level[i].mode == RADEON_SURF_MODE_1D) {
2294 switch (tile_mode) {
2295 case CIK_TILE_MODE_COLOR_2D:
2296 tile_mode = SI_TILE_MODE_COLOR_1D;
2297 break;
2298 case CIK_TILE_MODE_COLOR_2D_SCANOUT:
2299 tile_mode = SI_TILE_MODE_COLOR_1D_SCANOUT;
2300 break;
2301 case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_64:
2302 case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_128:
2303 case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_256:
2304 case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_512:
2305 case CIK_TILE_MODE_DEPTH_STENCIL_2D_TILESPLIT_ROW_SIZE:
2306 tile_mode = CIK_TILE_MODE_DEPTH_STENCIL_1D;
2307 break;
2308 default:
2309 return -EINVAL;
2310 }
2311 return si_surface_init_1d(surf_man, surf, level, bpe, tile_mode, offset, i);
2312 }
2313 /* level0 and first mipmap need to have alignment */
2314 aligned_offset = offset = surf->bo_size;
2315 if (i == 0) {
2316 aligned_offset = align64(aligned_offset, surf->bo_alignment);
2317 }
2318 if (surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX) {
2319 if (surf->level == level) {
2320 surf->tiling_index[i] = tile_mode;
2321 /* it's ok because stencil is done after */
2322 surf->stencil_tiling_index[i] = tile_mode;
2323 } else {
2324 surf->stencil_tiling_index[i] = tile_mode;
2325 }
2326 }
2327 }
2328 return 0;
2329 }
2330
2331 static int cik_surface_init_2d_miptrees(struct radeon_surface_manager *surf_man,
2332 struct radeon_surface *surf,
2333 unsigned tile_mode, unsigned stencil_tile_mode)
2334 {
2335 int r;
2336 uint32_t num_pipes, num_banks;
2337
2338 cik_get_2d_params(surf_man, surf->bpe, surf->nsamples,
2339 !(surf->flags & RADEON_SURF_Z_OR_SBUFFER), tile_mode,
2340 &num_pipes, NULL, &num_banks, NULL, NULL, NULL);
2341
2342 r = cik_surface_init_2d(surf_man, surf, surf->level, surf->bpe, tile_mode,
2343 surf->tile_split, num_pipes, num_banks, 0, 0);
2344 if (r) {
2345 return r;
2346 }
2347
2348 if (surf->flags & RADEON_SURF_SBUFFER) {
2349 r = cik_surface_init_2d(surf_man, surf, surf->stencil_level, 1, stencil_tile_mode,
2350 surf->stencil_tile_split, num_pipes, num_banks,
2351 surf->bo_size, 0);
2352 surf->stencil_offset = surf->stencil_level[0].offset;
2353 }
2354 return r;
2355 }
2356
2357 static int cik_surface_init(struct radeon_surface_manager *surf_man,
2358 struct radeon_surface *surf)
2359 {
2360 unsigned mode, tile_mode, stencil_tile_mode = 0;
2361 int r;
2362
2363 /* MSAA surfaces support the 2D mode only. */
2364 if (surf->nsamples > 1) {
2365 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
2366 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_2D, MODE);
2367 }
2368
2369 /* tiling mode */
2370 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
2371
2372 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER)) {
2373 /* zbuffer only support 1D or 2D tiled surface */
2374 switch (mode) {
2375 case RADEON_SURF_MODE_1D:
2376 case RADEON_SURF_MODE_2D:
2377 break;
2378 default:
2379 mode = RADEON_SURF_MODE_1D;
2380 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
2381 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
2382 break;
2383 }
2384 }
2385
2386 r = cik_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode);
2387 if (r) {
2388 return r;
2389 }
2390
2391 surf->stencil_offset = 0;
2392 surf->bo_alignment = 0;
2393
2394 /* check tiling mode */
2395 switch (mode) {
2396 case RADEON_SURF_MODE_LINEAR:
2397 r = r6_surface_init_linear(surf_man, surf, 0, 0);
2398 break;
2399 case RADEON_SURF_MODE_LINEAR_ALIGNED:
2400 r = si_surface_init_linear_aligned(surf_man, surf, tile_mode, 0, 0);
2401 break;
2402 case RADEON_SURF_MODE_1D:
2403 r = si_surface_init_1d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode);
2404 break;
2405 case RADEON_SURF_MODE_2D:
2406 r = cik_surface_init_2d_miptrees(surf_man, surf, tile_mode, stencil_tile_mode);
2407 break;
2408 default:
2409 return -EINVAL;
2410 }
2411 return r;
2412 }
2413
2414 /*
2415 * depending on surface
2416 */
2417 static int cik_surface_best(struct radeon_surface_manager *surf_man,
2418 struct radeon_surface *surf)
2419 {
2420 unsigned mode, tile_mode, stencil_tile_mode;
2421
2422 /* tiling mode */
2423 mode = (surf->flags >> RADEON_SURF_MODE_SHIFT) & RADEON_SURF_MODE_MASK;
2424
2425 if (surf->flags & (RADEON_SURF_ZBUFFER | RADEON_SURF_SBUFFER) &&
2426 !(surf->flags & RADEON_SURF_HAS_TILE_MODE_INDEX)) {
2427 /* depth/stencil force 1d tiling for old mesa */
2428 surf->flags = RADEON_SURF_CLR(surf->flags, MODE);
2429 surf->flags |= RADEON_SURF_SET(RADEON_SURF_MODE_1D, MODE);
2430 }
2431
2432 return cik_surface_sanity(surf_man, surf, mode, &tile_mode, &stencil_tile_mode);
2433 }
2434
2435
2436 /* ===========================================================================
2437 * public API
2438 */
2439 struct radeon_surface_manager *
2440 radeon_surface_manager_new(int fd)
2441 {
2442 struct radeon_surface_manager *surf_man;
2443
2444 surf_man = calloc(1, sizeof(struct radeon_surface_manager));
2445 if (surf_man == NULL) {
2446 return NULL;
2447 }
2448 surf_man->fd = fd;
2449 if (radeon_get_value(fd, RADEON_INFO_DEVICE_ID, &surf_man->device_id)) {
2450 goto out_err;
2451 }
2452 if (radeon_get_family(surf_man)) {
2453 goto out_err;
2454 }
2455
2456 if (surf_man->family <= CHIP_RV740) {
2457 if (r6_init_hw_info(surf_man)) {
2458 goto out_err;
2459 }
2460 surf_man->surface_init = &r6_surface_init;
2461 surf_man->surface_best = &r6_surface_best;
2462 } else if (surf_man->family <= CHIP_ARUBA) {
2463 if (eg_init_hw_info(surf_man)) {
2464 goto out_err;
2465 }
2466 surf_man->surface_init = &eg_surface_init;
2467 surf_man->surface_best = &eg_surface_best;
2468 } else if (surf_man->family < CHIP_BONAIRE) {
2469 if (si_init_hw_info(surf_man)) {
2470 goto out_err;
2471 }
2472 surf_man->surface_init = &si_surface_init;
2473 surf_man->surface_best = &si_surface_best;
2474 } else {
2475 if (cik_init_hw_info(surf_man)) {
2476 goto out_err;
2477 }
2478 surf_man->surface_init = &cik_surface_init;
2479 surf_man->surface_best = &cik_surface_best;
2480 }
2481
2482 return surf_man;
2483 out_err:
2484 free(surf_man);
2485 return NULL;
2486 }
2487
2488 void
2489 radeon_surface_manager_free(struct radeon_surface_manager *surf_man)
2490 {
2491 free(surf_man);
2492 }
2493
2494 static int radeon_surface_sanity(struct radeon_surface_manager *surf_man,
2495 struct radeon_surface *surf,
2496 unsigned type,
2497 unsigned mode)
2498 {
2499 if (surf_man == NULL || surf_man->surface_init == NULL || surf == NULL) {
2500 return -EINVAL;
2501 }
2502
2503 /* all dimension must be at least 1 ! */
2504 if (!surf->npix_x || !surf->npix_y || !surf->npix_z) {
2505 return -EINVAL;
2506 }
2507 if (!surf->blk_w || !surf->blk_h || !surf->blk_d) {
2508 return -EINVAL;
2509 }
2510 if (!surf->array_size) {
2511 return -EINVAL;
2512 }
2513 /* array size must be a power of 2 */
2514 surf->array_size = next_power_of_two(surf->array_size);
2515
2516 switch (surf->nsamples) {
2517 case 1:
2518 case 2:
2519 case 4:
2520 case 8:
2521 break;
2522 default:
2523 return -EINVAL;
2524 }
2525 /* check type */
2526 switch (type) {
2527 case RADEON_SURF_TYPE_1D:
2528 if (surf->npix_y > 1) {
2529 return -EINVAL;
2530 }
2531 FALLTHROUGH;
2532 case RADEON_SURF_TYPE_2D:
2533 if (surf->npix_z > 1) {
2534 return -EINVAL;
2535 }
2536 break;
2537 case RADEON_SURF_TYPE_CUBEMAP:
2538 if (surf->npix_z > 1) {
2539 return -EINVAL;
2540 }
2541 /* deal with cubemap as they were texture array */
2542 if (surf_man->family >= CHIP_RV770) {
2543 surf->array_size = 8;
2544 } else {
2545 surf->array_size = 6;
2546 }
2547 break;
2548 case RADEON_SURF_TYPE_3D:
2549 break;
2550 case RADEON_SURF_TYPE_1D_ARRAY:
2551 if (surf->npix_y > 1) {
2552 return -EINVAL;
2553 }
2554 FALLTHROUGH;
2555 case RADEON_SURF_TYPE_2D_ARRAY:
2556 break;
2557 default:
2558 return -EINVAL;
2559 }
2560 return 0;
2561 }
2562
2563 int
2564 radeon_surface_init(struct radeon_surface_manager *surf_man,
2565 struct radeon_surface *surf)
2566 {
2567 unsigned mode, type;
2568 int r;
2569
2570 type = RADEON_SURF_GET(surf->flags, TYPE);
2571 mode = RADEON_SURF_GET(surf->flags, MODE);
2572
2573 r = radeon_surface_sanity(surf_man, surf, type, mode);
2574 if (r) {
2575 return r;
2576 }
2577 return surf_man->surface_init(surf_man, surf);
2578 }
2579
2580 int
2581 radeon_surface_best(struct radeon_surface_manager *surf_man,
2582 struct radeon_surface *surf)
2583 {
2584 unsigned mode, type;
2585 int r;
2586
2587 type = RADEON_SURF_GET(surf->flags, TYPE);
2588 mode = RADEON_SURF_GET(surf->flags, MODE);
2589
2590 r = radeon_surface_sanity(surf_man, surf, type, mode);
2591 if (r) {
2592 return r;
2593 }
2594 return surf_man->surface_best(surf_man, surf);
2595 }