Debian Codegraph @

src/panfrost/lib/pan_mod.c

raw
1 /*
2 * Copyright (C) 2019-2025 Collabora, Ltd.
3 * Copyright (C) 2018-2019 Alyssa Rosenzweig
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "pan_mod.h"
8 #include "pan_afbc.h"
9 #include "pan_afrc.h"
10 #include "pan_desc.h"
11 #include "pan_format.h"
12 #include "pan_image.h"
13 #include "pan_layout.h"
14 #include "pan_props.h"
15 #include "pan_texture.h"
16
17 #include "util/format/u_format.h"
18
19 #if PAN_ARCH <= 10
20 #define MAX_SIZE_B u_uintN_max(32)
21 #define MAX_SLICE_STRIDE_B u_uintN_max(32)
22 #else
23 #define MAX_SIZE_B u_uintN_max(48)
24 #define MAX_SLICE_STRIDE_B u_uintN_max(37)
25 #endif
26
27 static bool
28 pan_mod_afbc_match(uint64_t mod)
29 {
30 return drm_is_afbc(mod);
31 }
32
33 static uint32_t
34 pan_mod_afbc_get_wsi_row_pitch(const struct pan_image *image,
35 unsigned plane_idx, unsigned mip_level)
36 {
37 const struct pan_image_props *props = &image->props;
38 const struct pan_image_layout *layout = &image->planes[plane_idx]->layout;
39 const unsigned header_row_stride_B =
40 layout->slices[mip_level].afbc.header.row_stride_B;
41 const struct pan_image_block_size tile_extent_el =
42 pan_afbc_superblock_size_el(props->format, props->modifier);
43 const unsigned tile_payload_size_B =
44 tile_extent_el.width * tile_extent_el.height *
45 pan_format_get_plane_blocksize(props->format, plane_idx);
46 const unsigned tile_row_payload_size_B =
47 pan_afbc_stride_blocks(props->format,
48 props->modifier, header_row_stride_B) *
49 tile_payload_size_B;
50
51 return tile_row_payload_size_B / pan_afbc_superblock_height(props->modifier);
52 }
53
54 static bool
55 pan_mod_afbc_init_plane_layout(const struct pan_image_props *props,
56 unsigned plane_idx,
57 struct pan_image_layout *plane_layout)
58 {
59 enum pan_afbc_mode mode =
60 pan_afbc_format(PAN_ARCH, props->format, plane_idx);
61 if (mode == PAN_AFBC_MODE_INVALID)
62 return false;
63
64 plane_layout->afbc.mode = mode;
65 return true;
66 }
67
68 static bool
69 pan_mod_afbc_init_slice_layout(
70 const struct pan_image_props *props, unsigned plane_idx,
71 struct pan_image_extent mip_extent_px,
72 const struct pan_image_layout_constraints *layout_constraints,
73 struct pan_image_slice_layout *slayout)
74 {
75 /* Use explicit layout only when wsi_row_pitch_B is non-zero */
76 const bool use_explicit_layout =
77 layout_constraints && layout_constraints->wsi_row_pitch_B;
78 struct pan_image_block_size afbc_tile_extent_px =
79 pan_afbc_superblock_size(props->modifier);
80 unsigned offset_align_mask =
81 pan_afbc_header_align(PAN_ARCH, props->modifier) - 1;
82 unsigned row_align_mask = pan_afbc_header_row_stride_align(
83 PAN_ARCH, props->format, props->modifier) -
84 1;
85 struct pan_image_block_size afbc_tile_extent_el =
86 pan_afbc_superblock_size_el(props->format, props->modifier);
87 unsigned afbc_tile_payload_size_B =
88 afbc_tile_extent_el.width * afbc_tile_extent_el.height *
89 pan_format_get_plane_blocksize(props->format, plane_idx);
90
91 struct pan_image_block_size align_px =
92 pan_afbc_renderblock_size(props->modifier);
93
94 /* If superblock tiling is used, align on a superblock tile. */
95 if (props->modifier & AFBC_FORMAT_MOD_TILED) {
96 align_px.width =
97 ALIGN_POT(align_px.width, afbc_tile_extent_px.width *
98 pan_afbc_tile_size(props->format, props->modifier));
99 align_px.height =
100 ALIGN_POT(align_px.height, afbc_tile_extent_px.height *
101 pan_afbc_tile_size(props->format, props->modifier));
102 }
103
104 struct pan_image_extent aligned_extent_px = {
105 .width = ALIGN_POT(mip_extent_px.width, align_px.width),
106 .height = ALIGN_POT(mip_extent_px.height, align_px.height),
107 .depth = mip_extent_px.depth,
108 };
109
110 if (use_explicit_layout) {
111 unsigned afbc_tile_payload_row_stride_B =
112 layout_constraints->wsi_row_pitch_B *
113 pan_afbc_superblock_height(props->modifier);
114
115 /* For quite some time, we've been accepting WSI row pitch that
116 * didn't match exactly the image size and have been assuming tightly
117 * packed tile rows instead of using the explicit stride in that case.
118 * This is something we can't change without risking breaking existing
119 * users, so we enforce this explicit tile alignment only if we were
120 * asked to. */
121 if (layout_constraints->strict &&
122 (afbc_tile_payload_row_stride_B % afbc_tile_payload_size_B)) {
123 mesa_loge("WSI pitch is not aligned on an AFBC tile");
124 return false;
125 }
126
127 unsigned width_from_wsi_row_stride =
128 (afbc_tile_payload_row_stride_B / afbc_tile_payload_size_B) *
129 pan_afbc_superblock_width(props->modifier);
130
131 if (width_from_wsi_row_stride < mip_extent_px.width) {
132 mesa_loge("WSI pitch too small");
133 return false;
134 }
135
136 slayout->afbc.header.row_stride_B =
137 pan_afbc_row_stride(props->format, props->modifier, width_from_wsi_row_stride);
138 if (slayout->afbc.header.row_stride_B & row_align_mask) {
139 mesa_loge("WSI pitch not properly aligned");
140 return false;
141 }
142
143 slayout->offset_B = layout_constraints->offset_B;
144 if (slayout->offset_B & offset_align_mask) {
145 mesa_loge("WSI offset not properly aligned");
146 return false;
147 }
148
149 /* If this is not a strict import, ignore the WSI row pitch and use
150 * the resource width to get the size. */
151 if (!layout_constraints->strict) {
152 slayout->afbc.header.row_stride_B = ALIGN_POT(
153 pan_afbc_row_stride(props->format, props->modifier, aligned_extent_px.width),
154 row_align_mask + 1);
155 }
156 } else {
157 slayout->offset_B =
158 ALIGN_POT(layout_constraints ? layout_constraints->offset_B : 0,
159 offset_align_mask + 1);
160 slayout->afbc.header.row_stride_B = ALIGN_POT(
161 pan_afbc_row_stride(props->format, props->modifier, aligned_extent_px.width),
162 row_align_mask + 1);
163 }
164
165 const unsigned row_stride_sb = pan_afbc_stride_blocks(props->format,
166 props->modifier,
167 slayout->afbc.header.row_stride_B);
168 const unsigned surface_stride_sb =
169 row_stride_sb * (aligned_extent_px.height / afbc_tile_extent_px.height);
170
171 uint64_t hdr_surf_size_B =
172 (uint64_t)surface_stride_sb * AFBC_HEADER_BYTES_PER_TILE;
173 uint64_t body_offset_B =
174 pan_afbc_body_offset(PAN_ARCH, props->modifier, hdr_surf_size_B);
175 uint64_t surf_stride_B =
176 body_offset_B + ((uint64_t)surface_stride_sb * afbc_tile_payload_size_B);
177
178 slayout->afbc.header.surface_size_B = hdr_surf_size_B;
179 slayout->afbc.surface_stride_B = surf_stride_B;
180 slayout->size_B = surf_stride_B * mip_extent_px.depth;
181
182 if (hdr_surf_size_B > UINT32_MAX || surf_stride_B > MAX_SLICE_STRIDE_B ||
183 slayout->size_B > MAX_SIZE_B)
184 return false;
185
186 return true;
187 }
188
189 static enum pan_mod_support
190 pan_mod_afbc_test_props(const struct pan_kmod_dev_props *dprops,
191 const struct pan_image_props *iprops,
192 const struct pan_image_usage *iusage)
193 {
194 /* No image store. */
195 if (iusage && iusage->bind & PAN_BIND_STORAGE_IMAGE)
196 return PAN_MOD_NOT_SUPPORTED;
197
198 /* We don't implement mapping individual tiles with AFBC. */
199 if (iusage && iusage->standard_sparse_mapping_granularity)
200 return PAN_MOD_NOT_SUPPORTED;
201
202 /* AFBC not supported. */
203 if (!pan_query_afbc(dprops))
204 return PAN_MOD_NOT_SUPPORTED;
205
206 unsigned plane_count = util_format_get_num_planes(iprops->format);
207 const struct util_format_description *fdesc =
208 util_format_description(iprops->format);
209
210 /* Check if the format is supported first. */
211 enum pan_afbc_mode plane_modes[3];
212 for (unsigned p = 0; p < plane_count; p++) {
213 plane_modes[p] = pan_afbc_format(PAN_ARCH, iprops->format, p);
214 if (plane_modes[p] == PAN_AFBC_MODE_INVALID)
215 return PAN_MOD_NOT_SUPPORTED;
216 }
217
218 /* AFBC can't do multisampling. */
219 if (iprops->nr_samples > 1)
220 return PAN_MOD_NOT_SUPPORTED;
221
222 /* AFBC(2D) or AFBC(3D) on v7+ only. */
223 if ((iprops->dim == MALI_TEXTURE_DIMENSION_3D && PAN_ARCH < 7) ||
224 iprops->dim != MALI_TEXTURE_DIMENSION_2D)
225 return PAN_MOD_NOT_SUPPORTED;
226
227 /* ZS buffer descriptors can't pass split/wide/YTR modifiers. */
228 if (iusage && (iusage->bind & PAN_BIND_DEPTH_STENCIL) &&
229 (pan_afbc_superblock_width(iprops->modifier) != 16 ||
230 (iprops->modifier & (AFBC_FORMAT_MOD_SPLIT | AFBC_FORMAT_MOD_YTR))))
231 return PAN_MOD_NOT_SUPPORTED;
232
233 /* YTR is only useful on RGB formats. */
234 if ((iprops->modifier & AFBC_FORMAT_MOD_YTR) &&
235 (pan_format_is_yuv(iprops->format) || fdesc->nr_channels < 3))
236 return PAN_MOD_NOT_SUPPORTED;
237
238 /* Make sure all planes support split mode. */
239 if ((iprops->modifier & AFBC_FORMAT_MOD_SPLIT)) {
240 for (unsigned p = 0; p < plane_count; p++) {
241 if (!pan_afbc_can_split(PAN_ARCH, plane_modes[p], iprops->modifier))
242 return PAN_MOD_NOT_SUPPORTED;
243 }
244 }
245
246 struct pan_image_block_size superblock_extent_px = pan_afbc_superblock_size(iprops->modifier);
247
248 if (iprops->modifier & AFBC_FORMAT_MOD_TILED) {
249 /* Make sure tiled mode is supported. */
250 if (!pan_afbc_can_tile(PAN_ARCH))
251 return PAN_MOD_NOT_SUPPORTED;
252
253 struct pan_image_block_size tile_extent_px = {
254 superblock_extent_px.width * pan_afbc_tile_size(iprops->format, iprops->modifier),
255 superblock_extent_px.height * pan_afbc_tile_size(iprops->format, iprops->modifier),
256 };
257
258 /* Tiled mode has some overhead we don't want to pay if the image size is
259 * too thin. */
260 if (iprops->extent_px.width < tile_extent_px.width / 2 ||
261 iprops->extent_px.height < tile_extent_px.height / 2)
262 return PAN_MOD_NOT_OPTIMAL;
263 } else {
264 /* For images thinner than a superblock, fall through to U-interleaved. */
265 if (iprops->extent_px.width < superblock_extent_px.width ||
266 iprops->extent_px.height < superblock_extent_px.height)
267 return PAN_MOD_NOT_OPTIMAL;
268 }
269
270 /* Only use 32x8 superblock for WSI images. */
271 if (iusage && !iusage->wsi &&
272 pan_afbc_superblock_width(iprops->modifier) != 16)
273 return PAN_MOD_NOT_OPTIMAL;
274
275 /* Prefer YTR when available. */
276 if (pan_afbc_can_ytr(iprops->format) &&
277 !(iprops->modifier & AFBC_FORMAT_MOD_YTR))
278 return PAN_MOD_NOT_OPTIMAL;
279
280 /* Packing/unpacking AFBC payload requires a COMPUTE job which we'd rather
281 * avoid.
282 */
283 if (iusage &&
284 (iusage->bind & (PAN_BIND_DEPTH_STENCIL | PAN_BIND_RENDER_TARGET)) &&
285 !(iprops->modifier & AFBC_FORMAT_MOD_SPARSE))
286 return PAN_MOD_NOT_OPTIMAL;
287
288 return PAN_MOD_OPTIMAL;
289 }
290
291 #define pan_mod_afbc_emit_tex_payload_entry \
292 GENX(pan_tex_emit_afbc_payload_entry)
293 #define pan_mod_afbc_emit_color_attachment GENX(pan_emit_afbc_color_attachment)
294 #define pan_mod_afbc_emit_zs_attachment GENX(pan_emit_afbc_zs_attachment)
295 #define pan_mod_afbc_emit_s_attachment GENX(pan_emit_afbc_s_attachment)
296
297 #if PAN_ARCH >= 10
298 static bool
299 pan_mod_afrc_match(uint64_t mod)
300 {
301 return drm_is_afrc(mod);
302 }
303
304 static enum pan_mod_support
305 pan_mod_afrc_test_props(const struct pan_kmod_dev_props *dprops,
306 const struct pan_image_props *iprops,
307 const struct pan_image_usage *iusage)
308 {
309 /* AFRC not supported. */
310 if (!pan_query_afrc(dprops))
311 return PAN_MOD_NOT_SUPPORTED;
312
313 /* Format not AFRC-able. */
314 if (!pan_afrc_supports_format(iprops->format))
315 return PAN_MOD_NOT_SUPPORTED;
316
317 /* AFRC does not support layered multisampling. */
318 if (iprops->nr_samples > 1)
319 return PAN_MOD_NOT_SUPPORTED;
320
321 /* No image store. */
322 if (iusage && iusage->bind & PAN_BIND_STORAGE_IMAGE)
323 return PAN_MOD_NOT_SUPPORTED;
324
325 /* We don't implement mapping individual tiles with AFRC. */
326 if (iusage && iusage->standard_sparse_mapping_granularity)
327 return PAN_MOD_NOT_SUPPORTED;
328
329 /* We can't write to an AFRC resource directly. */
330 if (iusage && iusage->host_copy)
331 return PAN_MOD_NOT_SUPPORTED;
332
333 /* Host updates require an extra blit which we would rather avoid. */
334 if (iusage && iusage->frequent_host_updates)
335 return PAN_MOD_NOT_OPTIMAL;
336
337 /* There's nothing preventing 1D AFRC, but it's pointless. */
338 if (iprops->dim == MALI_TEXTURE_DIMENSION_1D)
339 return PAN_MOD_NOT_OPTIMAL;
340
341 return PAN_MOD_OPTIMAL;
342 }
343
344 static uint32_t
345 pan_mod_afrc_get_wsi_row_pitch(const struct pan_image *image,
346 unsigned plane_idx, unsigned mip_level)
347 {
348 const struct pan_image_props *props = &image->props;
349 const struct pan_image_layout *layout = &image->planes[plane_idx]->layout;
350 const struct pan_image_block_size tile_extent_px =
351 pan_afrc_tile_size(props->format, props->modifier);
352
353 return layout->slices[mip_level].tiled_or_linear.row_stride_B /
354 tile_extent_px.height;
355 }
356
357 #define pan_mod_afrc_init_plane_layout NULL
358
359 static bool
360 pan_mod_afrc_init_slice_layout(
361 const struct pan_image_props *props, unsigned plane_idx,
362 struct pan_image_extent mip_extent_px,
363 const struct pan_image_layout_constraints *layout_constraints,
364 struct pan_image_slice_layout *slayout)
365 {
366 /* Use explicit layout only when wsi_row_pitch_B is non-zero */
367 const bool use_explicit_layout =
368 layout_constraints && layout_constraints->wsi_row_pitch_B;
369 const unsigned align_mask =
370 pan_afrc_buffer_alignment_from_modifier(props->modifier) - 1;
371 struct pan_image_block_size tile_extent_px =
372 pan_afrc_tile_size(props->format, props->modifier);
373 struct pan_image_extent aligned_extent_px = {
374 .width = ALIGN_POT(mip_extent_px.width, tile_extent_px.width),
375 .height = ALIGN_POT(mip_extent_px.height, tile_extent_px.height),
376 .depth = mip_extent_px.depth,
377 };
378
379 if (use_explicit_layout) {
380 slayout->tiled_or_linear.row_stride_B =
381 layout_constraints->wsi_row_pitch_B * tile_extent_px.height;
382 if (slayout->tiled_or_linear.row_stride_B & align_mask) {
383 mesa_loge("WSI pitch not properly aligned");
384 return false;
385 }
386
387 slayout->offset_B = layout_constraints->offset_B;
388 if (slayout->offset_B & align_mask) {
389 mesa_loge("WSI offset not properly aligned");
390 return false;
391 }
392
393 unsigned afrc_blk_size_B =
394 pan_afrc_block_size_from_modifier(props->modifier) *
395 AFRC_CLUMPS_PER_TILE;
396 unsigned width_from_wsi_row_stride =
397 (slayout->tiled_or_linear.row_stride_B / afrc_blk_size_B) *
398 tile_extent_px.width;
399
400 if (width_from_wsi_row_stride < mip_extent_px.width) {
401 mesa_loge("WSI pitch too small");
402 return false;
403 }
404
405 /* If this is not a strict import, ignore the WSI row pitch and use
406 * the resource width to get the size. */
407 if (!layout_constraints->strict) {
408 slayout->tiled_or_linear.row_stride_B = pan_afrc_row_stride(
409 props->format, props->modifier, mip_extent_px.width);
410 }
411 } else {
412 /* Align levels to cache-line as a performance improvement for
413 * linear/tiled and as a requirement for AFBC */
414 slayout->offset_B = ALIGN_POT(
415 layout_constraints ? layout_constraints->offset_B : 0, align_mask + 1);
416 slayout->tiled_or_linear.row_stride_B =
417 ALIGN_POT(pan_afrc_row_stride(props->format, props->modifier,
418 mip_extent_px.width),
419 align_mask + 1);
420 }
421
422 uint64_t surf_stride_B =
423 (uint64_t)slayout->tiled_or_linear.row_stride_B *
424 (aligned_extent_px.height / tile_extent_px.height);
425
426 slayout->tiled_or_linear.surface_stride_B = surf_stride_B;
427 slayout->size_B =
428 surf_stride_B * aligned_extent_px.depth * props->nr_samples;
429
430 /* Make sure the stride/size fits in the descriptor fields. */
431 if (slayout->size_B > MAX_SIZE_B ||
432 slayout->tiled_or_linear.surface_stride_B > MAX_SLICE_STRIDE_B)
433 return false;
434
435 return true;
436 }
437
438 #define pan_mod_afrc_emit_tex_payload_entry \
439 GENX(pan_tex_emit_afrc_payload_entry)
440 #define pan_mod_afrc_emit_color_attachment GENX(pan_emit_afrc_color_attachment)
441 #define pan_mod_afrc_emit_zs_attachment NULL
442 #define pan_mod_afrc_emit_s_attachment NULL
443 #endif
444
445 static bool
446 pan_mod_u_tiled_match(uint64_t mod)
447 {
448 return mod == DRM_FORMAT_MOD_ARM_16X16_BLOCK_U_INTERLEAVED;
449 }
450
451 static enum pan_mod_support
452 pan_mod_u_tiled_test_props(const struct pan_kmod_dev_props *dprops,
453 const struct pan_image_props *iprops,
454 const struct pan_image_usage *iusage)
455 {
456 assert(GENX(pan_format_from_pipe_format)(iprops->format)->hw);
457
458 /* YUV not supported. */
459 if (pan_format_is_yuv(iprops->format))
460 return PAN_MOD_NOT_SUPPORTED;
461
462 /* We don't implement mapping individual tiles in this layout. */
463 if (iusage && iusage->standard_sparse_mapping_granularity)
464 return PAN_MOD_NOT_SUPPORTED;
465
466 /* The purpose of tiling is improving locality in both X- and
467 * Y-directions. If there is only a single pixel in either direction,
468 * tiling does not make sense; using a linear layout instead is optimal
469 * for both memory usage and performance.
470 */
471 if (MIN2(iprops->extent_px.width, iprops->extent_px.height) < 2)
472 return PAN_MOD_NOT_OPTIMAL;
473
474 return PAN_MOD_OPTIMAL;
475 }
476
477 static uint32_t
478 pan_mod_u_tiled_get_wsi_row_pitch(const struct pan_image *image,
479 unsigned plane_idx, unsigned mip_level)
480 {
481 const struct pan_image_props *props = &image->props;
482 const struct pan_image_layout *layout = &image->planes[plane_idx]->layout;
483
484 return layout->slices[mip_level].tiled_or_linear.row_stride_B /
485 pan_u_interleaved_tile_size_el(props->format).height;
486 }
487
488 #define pan_mod_u_tiled_init_plane_layout NULL
489
490 static bool
491 pan_mod_u_tiled_init_slice_layout(
492 const struct pan_image_props *props, unsigned plane_idx,
493 struct pan_image_extent mip_extent_px,
494 const struct pan_image_layout_constraints *layout_constraints,
495 struct pan_image_slice_layout *slayout)
496 {
497 /* Use explicit layout only when wsi_row_pitch_B is non-zero */
498 const bool use_explicit_layout =
499 layout_constraints && layout_constraints->wsi_row_pitch_B;
500 unsigned align_mask =
501 pan_linear_or_tiled_row_align_req(PAN_ARCH, props->format, plane_idx) - 1;
502 struct pan_image_block_size tile_extent_el =
503 pan_u_interleaved_tile_size_el(props->format);
504 struct pan_image_extent mip_extent_el;
505 unsigned tile_size_B;
506
507 if (util_format_is_compressed(props->format)) {
508 assert(util_format_get_num_planes(props->format) == 1);
509 mip_extent_el.width = DIV_ROUND_UP(
510 mip_extent_px.width, util_format_get_blockwidth(props->format));
511 mip_extent_el.height = DIV_ROUND_UP(
512 mip_extent_px.height, util_format_get_blockheight(props->format));
513 mip_extent_el.depth = DIV_ROUND_UP(
514 mip_extent_px.depth, util_format_get_blockdepth(props->format));
515 tile_size_B = tile_extent_el.width * tile_extent_el.height *
516 pan_format_get_plane_blocksize(props->format, plane_idx);
517 } else {
518 /* Block-based YUV needs special care, because the U-tile extent
519 * is in pixels, not blocks in that case. */
520 assert(tile_extent_el.width % util_format_get_blockwidth(props->format) ==
521 0);
522 assert(tile_extent_el.height %
523 util_format_get_blockheight(props->format) ==
524 0);
525 mip_extent_el = mip_extent_px;
526 tile_size_B =
527 (tile_extent_el.width / util_format_get_blockwidth(props->format)) *
528 (tile_extent_el.height / util_format_get_blockheight(props->format)) *
529 pan_format_get_plane_blocksize(props->format, plane_idx);
530 }
531
532 if (use_explicit_layout) {
533 slayout->tiled_or_linear.row_stride_B =
534 layout_constraints->wsi_row_pitch_B * tile_extent_el.height;
535 if (slayout->tiled_or_linear.row_stride_B & align_mask) {
536 mesa_loge("WSI pitch not properly aligned");
537 return false;
538 }
539
540 const unsigned width_from_wsi_row_stride =
541 (slayout->tiled_or_linear.row_stride_B / tile_size_B) *
542 tile_extent_el.width;
543
544 if (width_from_wsi_row_stride < mip_extent_el.width) {
545 mesa_loge("WSI pitch too small");
546 return false;
547 }
548
549 slayout->offset_B = layout_constraints->offset_B;
550 if (slayout->offset_B & align_mask) {
551 mesa_loge("WSI offset not properly aligned");
552 return false;
553 }
554 } else {
555 /* When we can decide of the layout, we want things aligned on at least a
556 * cacheline for performance reasons. */
557 align_mask = MAX2(align_mask, 63);
558 slayout->offset_B = ALIGN_POT(
559 layout_constraints ? layout_constraints->offset_B : 0,
560 MAX2(align_mask + 1, pan_image_slice_align(props->modifier)));
561 slayout->tiled_or_linear.row_stride_B = ALIGN_POT(
562 tile_size_B * DIV_ROUND_UP(mip_extent_el.width, tile_extent_el.width),
563 align_mask + 1);
564 }
565
566 uint64_t surf_stride_B =
567 (uint64_t)slayout->tiled_or_linear.row_stride_B *
568 DIV_ROUND_UP(mip_extent_el.height, tile_extent_el.height);
569 surf_stride_B = ALIGN_POT(surf_stride_B, (uint64_t)align_mask + 1);
570
571 slayout->tiled_or_linear.surface_stride_B = surf_stride_B;
572 slayout->size_B = surf_stride_B * mip_extent_el.depth * props->nr_samples;
573
574 /* Make sure the stride/size fits in the descriptor fields. */
575 if (slayout->size_B > MAX_SIZE_B ||
576 slayout->tiled_or_linear.surface_stride_B > MAX_SLICE_STRIDE_B)
577 return false;
578
579 return true;
580 }
581
582 #define pan_mod_u_tiled_emit_tex_payload_entry \
583 GENX(pan_tex_emit_u_tiled_payload_entry)
584 #define pan_mod_u_tiled_emit_color_attachment \
585 GENX(pan_emit_u_tiled_color_attachment)
586 #define pan_mod_u_tiled_emit_zs_attachment GENX(pan_emit_u_tiled_zs_attachment)
587 #define pan_mod_u_tiled_emit_s_attachment GENX(pan_emit_u_tiled_s_attachment)
588
589 #if PAN_ARCH >= 10
590 #define pan_mod_interleaved_64k_init_plane_layout NULL
591
592 static bool
593 pan_mod_interleaved_64k_init_slice_layout(
594 const struct pan_image_props *props, unsigned plane_idx,
595 struct pan_image_extent mip_extent_px,
596 const struct pan_image_layout_constraints *layout_constraints,
597 struct pan_image_slice_layout *slayout)
598 {
599 assert(!(layout_constraints && layout_constraints->wsi_row_pitch_B));
600
601 struct pan_image_block_size tile_extent_el =
602 pan_interleaved_64k_tile_size_el(props->format);
603 struct pan_image_block_size tile_extent_px = {
604 tile_extent_el.width * util_format_get_blockwidth(props->format),
605 tile_extent_el.height * util_format_get_blockheight(props->format),
606 };
607 uint64_t tile_size_B = 65536;
608
609 struct pan_image_extent mip_extent_tiles = {
610 DIV_ROUND_UP(mip_extent_px.width, tile_extent_px.width),
611 DIV_ROUND_UP(mip_extent_px.height, tile_extent_px.height),
612 mip_extent_px.depth,
613 };
614
615 uint64_t row_stride_B = mip_extent_tiles.width * tile_size_B;
616 uint64_t surf_stride_B = mip_extent_tiles.height * row_stride_B;
617
618 slayout->offset_B = layout_constraints ? layout_constraints->offset_B : 0;
619 slayout->size_B = mip_extent_tiles.depth * surf_stride_B;
620 slayout->tiled_or_linear.row_stride_B = row_stride_B;
621 slayout->tiled_or_linear.surface_stride_B = surf_stride_B;
622
623 if (slayout->size_B > MAX_SIZE_B ||
624 slayout->tiled_or_linear.surface_stride_B > MAX_SLICE_STRIDE_B)
625 return false;
626
627 return true;
628 }
629
630 static uint32_t
631 pan_mod_interleaved_64k_get_wsi_row_pitch(const struct pan_image *image,
632 unsigned plane_idx, unsigned mip_level)
633 {
634 UNREACHABLE("interleaved 64k cannot be used for wsi");
635 }
636
637 static bool
638 pan_mod_interleaved_64k_match(uint64_t mod)
639 {
640 return mod == DRM_FORMAT_MOD_ARM_INTERLEAVED_64K;
641 }
642
643 static enum pan_mod_support
644 pan_mod_interleaved_64k_test_props(const struct pan_kmod_dev_props *dprops,
645 const struct pan_image_props *iprops,
646 const struct pan_image_usage *iusage)
647 {
648 assert(GENX(pan_format_from_pipe_format)(iprops->format)->hw);
649
650 /* YUV not supported. */
651 if (pan_format_is_yuv(iprops->format))
652 return PAN_MOD_NOT_SUPPORTED;
653
654 /* Non-po2 byte texel blocks not supported. */
655 if (!util_is_power_of_two_nonzero(util_format_get_blocksize(iprops->format)))
656 return PAN_MOD_NOT_SUPPORTED;
657
658 /* We don't implement multisampling with this layout. */
659 if (iprops->nr_samples > 1)
660 return PAN_MOD_NOT_SUPPORTED;
661
662 /* We don't implement tiling/detiling of this layout on host. */
663 if (iusage->host_copy)
664 return PAN_MOD_NOT_SUPPORTED;
665
666 /* We don't respect wsi_row_pitch_B so this layout is not usable for WSI. */
667 if (iusage->wsi)
668 return PAN_MOD_NOT_SUPPORTED;
669
670 struct pan_image_block_size tile_extent_el =
671 pan_interleaved_64k_tile_size_el(iprops->format);
672 struct pan_image_block_size tile_extent_px = {
673 tile_extent_el.width * util_format_get_blockwidth(iprops->format),
674 tile_extent_el.height * util_format_get_blockheight(iprops->format),
675 };
676
677 /* If our image is too thin it might make more sense to use U-interleaved or
678 * even linear */
679 if (iprops->extent_px.width < tile_extent_px.width / 2 ||
680 iprops->extent_px.height < tile_extent_px.height / 2)
681 return PAN_MOD_NOT_OPTIMAL;
682
683 return PAN_MOD_OPTIMAL;
684 }
685
686 #define pan_mod_interleaved_64k_emit_tex_payload_entry \
687 GENX(pan_tex_emit_interleaved_64k_payload_entry)
688 #define pan_mod_interleaved_64k_emit_color_attachment \
689 GENX(pan_emit_interleaved_64k_color_attachment)
690 #define pan_mod_interleaved_64k_emit_zs_attachment GENX(pan_emit_interleaved_64k_zs_attachment)
691 #define pan_mod_interleaved_64k_emit_s_attachment GENX(pan_emit_interleaved_64k_s_attachment)
692 #endif
693
694 static bool
695 pan_mod_linear_match(uint64_t mod)
696 {
697 return mod == DRM_FORMAT_MOD_LINEAR;
698 }
699
700 static enum pan_mod_support
701 pan_mod_linear_test_props(const struct pan_kmod_dev_props *dprops,
702 const struct pan_image_props *iprops,
703 const struct pan_image_usage *iusage)
704 {
705 assert(GENX(pan_format_from_pipe_format)(iprops->format)->hw);
706
707 /* We can't implement mapping of tiles at standard sparse granularity using
708 * this layout. */
709 if (iusage && iusage->standard_sparse_mapping_granularity)
710 return PAN_MOD_NOT_SUPPORTED;
711
712 switch (iprops->format) {
713 /* AFBC-only formats. */
714 case PIPE_FORMAT_R8G8B8_420_UNORM_PACKED:
715 case PIPE_FORMAT_R10G10B10_420_UNORM_PACKED:
716 case PIPE_FORMAT_X6R10X6G10_X6R10X6B10_422_UNORM:
717 return PAN_MOD_NOT_SUPPORTED;
718 default:
719 /* We assume that all "better" mods have been tested before linear, and
720 * declare it as optimal so it's always picked when tested, unless it's
721 * not supported.
722 */
723 return PAN_MOD_OPTIMAL;
724 }
725 }
726
727 static uint32_t
728 pan_mod_linear_get_wsi_row_pitch(const struct pan_image *image,
729 unsigned plane_idx, unsigned mip_level)
730 {
731 const struct pan_image_layout *layout = &image->planes[plane_idx]->layout;
732
733 return layout->slices[mip_level].tiled_or_linear.row_stride_B;
734 }
735
736 #define pan_mod_linear_init_plane_layout NULL
737
738 static bool
739 pan_mod_linear_init_slice_layout(
740 const struct pan_image_props *props, unsigned plane_idx,
741 struct pan_image_extent mip_extent_px,
742 const struct pan_image_layout_constraints *layout_constraints,
743 struct pan_image_slice_layout *slayout)
744 {
745 /* Use explicit layout only when wsi_row_pitch_B is non-zero */
746 const bool use_explicit_layout =
747 layout_constraints && layout_constraints->wsi_row_pitch_B;
748 unsigned align_mask =
749 pan_linear_or_tiled_row_align_req(PAN_ARCH, props->format, plane_idx) - 1;
750 const unsigned fmt_blksize_B =
751 pan_format_get_plane_blocksize(props->format, plane_idx);
752 struct pan_image_extent mip_extent_el;
753
754 if (util_format_is_compressed(props->format)) {
755 assert(util_format_get_num_planes(props->format) == 1);
756 mip_extent_el.width = DIV_ROUND_UP(
757 mip_extent_px.width, util_format_get_blockwidth(props->format));
758 mip_extent_el.height = DIV_ROUND_UP(
759 mip_extent_px.height, util_format_get_blockheight(props->format));
760 mip_extent_el.depth = DIV_ROUND_UP(
761 mip_extent_px.depth, util_format_get_blockdepth(props->format));
762 } else {
763 mip_extent_el = mip_extent_px;
764 }
765
766 if (use_explicit_layout) {
767 unsigned width_from_wsi_row_stride =
768 layout_constraints->wsi_row_pitch_B / fmt_blksize_B;
769
770 if (!util_format_is_compressed(props->format))
771 width_from_wsi_row_stride *= util_format_get_blockwidth(props->format);
772
773 if (width_from_wsi_row_stride < mip_extent_el.width) {
774 mesa_loge("WSI pitch too small");
775 return false;
776 }
777
778 slayout->tiled_or_linear.row_stride_B =
779 layout_constraints->wsi_row_pitch_B;
780 if (slayout->tiled_or_linear.row_stride_B & align_mask) {
781 mesa_loge("WSI pitch not properly aligned");
782 return false;
783 }
784
785 slayout->offset_B = layout_constraints->offset_B;
786 if (slayout->offset_B & align_mask) {
787 mesa_loge("WSI offset not properly aligned");
788 return false;
789 }
790 } else {
791 /* When we can decide of the layout, we want things aligned on at least a
792 * cacheline for performance reasons. */
793 align_mask = MAX2(align_mask, 63);
794 slayout->offset_B = ALIGN_POT(
795 layout_constraints ? layout_constraints->offset_B : 0,
796 MAX2(align_mask + 1, pan_image_slice_align(props->modifier)));
797 slayout->tiled_or_linear.row_stride_B =
798 ALIGN_POT(mip_extent_el.width * fmt_blksize_B, align_mask + 1);
799 }
800
801 uint64_t surf_stride_B =
802 (uint64_t)slayout->tiled_or_linear.row_stride_B * mip_extent_el.height;
803 surf_stride_B = ALIGN_POT(surf_stride_B, (uint64_t)align_mask + 1);
804
805 /* Surface stride is passed as a 32-bit unsigned integer to RT/ZS and texture
806 * descriptors, make sure it fits. */
807 if (surf_stride_B > MAX_SLICE_STRIDE_B)
808 return false;
809
810 slayout->tiled_or_linear.surface_stride_B = surf_stride_B;
811 slayout->size_B = surf_stride_B * mip_extent_el.depth * props->nr_samples;
812 return true;
813 }
814
815 #define pan_mod_linear_emit_tex_payload_entry \
816 GENX(pan_tex_emit_linear_payload_entry)
817 #define pan_mod_linear_emit_color_attachment \
818 GENX(pan_emit_linear_color_attachment)
819 #define pan_mod_linear_emit_zs_attachment GENX(pan_emit_linear_zs_attachment)
820 #define pan_mod_linear_emit_s_attachment GENX(pan_emit_linear_s_attachment)
821
822 #if PAN_ARCH >= 5
823 #define EMIT_ATT(__name) \
824 .emit_color_attachment = pan_mod_##__name##_emit_color_attachment, \
825 .emit_zs_attachment = pan_mod_##__name##_emit_zs_attachment, \
826 .emit_s_attachment = pan_mod_##__name##_emit_s_attachment
827 #else
828 #define EMIT_ATT(__name) \
829 .emit_color_attachment = NULL, .emit_zs_attachment = NULL, \
830 .emit_s_attachment = NULL
831 #endif
832
833 #define PAN_MOD_DEF(__name) \
834 { \
835 .match = pan_mod_##__name##_match, \
836 .test_props = pan_mod_##__name##_test_props, \
837 .get_wsi_row_pitch = pan_mod_##__name##_get_wsi_row_pitch, \
838 .init_plane_layout = pan_mod_##__name##_init_plane_layout, \
839 .init_slice_layout = pan_mod_##__name##_init_slice_layout, \
840 .emit_tex_payload_entry = pan_mod_##__name##_emit_tex_payload_entry, \
841 EMIT_ATT(__name), \
842 }
843
844 static const struct pan_mod_handler pan_mod_handlers[] = {
845 PAN_MOD_DEF(afbc),
846 PAN_MOD_DEF(u_tiled),
847 PAN_MOD_DEF(linear),
848 #if PAN_ARCH >= 10
849 PAN_MOD_DEF(interleaved_64k),
850 PAN_MOD_DEF(afrc),
851 #endif
852 };
853
854 const struct pan_mod_handler *
855 GENX(pan_mod_get_handler)(uint64_t modifier)
856 {
857 for (unsigned i = 0; i < ARRAY_SIZE(pan_mod_handlers); i++) {
858 if (pan_mod_handlers[i].match(modifier))
859 return &pan_mod_handlers[i];
860 }
861
862 return NULL;
863 }