xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/etnaviv/etnaviv_resource.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2012-2015 Etnaviv Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sub license,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Wladimir J. van der Laan <[email protected]>
25  */
26 
27 #include "etnaviv_resource.h"
28 
29 #include "etnaviv_context.h"
30 #include "etnaviv_debug.h"
31 #include "etnaviv_screen.h"
32 #include "etnaviv_translate.h"
33 
34 #include "util/hash_table.h"
35 #include "util/u_inlines.h"
36 #include "util/u_memory.h"
37 
modifier_to_layout(uint64_t modifier)38 static enum etna_surface_layout modifier_to_layout(uint64_t modifier)
39 {
40    switch (modifier & ~VIVANTE_MOD_EXT_MASK) {
41    case DRM_FORMAT_MOD_VIVANTE_TILED:
42       return ETNA_LAYOUT_TILED;
43    case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
44       return ETNA_LAYOUT_SUPER_TILED;
45    case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
46       return ETNA_LAYOUT_MULTI_TILED;
47    case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
48       return ETNA_LAYOUT_MULTI_SUPERTILED;
49    case DRM_FORMAT_MOD_LINEAR:
50       return ETNA_LAYOUT_LINEAR;
51    default:
52       unreachable("unhandled modifier");
53    }
54 }
55 
layout_to_modifier(enum etna_surface_layout layout)56 static uint64_t layout_to_modifier(enum etna_surface_layout layout)
57 {
58    switch (layout) {
59    case ETNA_LAYOUT_TILED:
60       return DRM_FORMAT_MOD_VIVANTE_TILED;
61    case ETNA_LAYOUT_SUPER_TILED:
62       return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED;
63    case ETNA_LAYOUT_MULTI_TILED:
64       return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED;
65    case ETNA_LAYOUT_MULTI_SUPERTILED:
66       return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED;
67    case ETNA_LAYOUT_LINEAR:
68       return DRM_FORMAT_MOD_LINEAR;
69    default:
70       return DRM_FORMAT_MOD_INVALID;
71    }
72 }
73 
etna_resource_modifier(struct etna_resource * rsc)74 static uint64_t etna_resource_modifier(struct etna_resource *rsc)
75 {
76    if (etna_resource_ext_ts(rsc))
77       return rsc->modifier;
78 
79    return layout_to_modifier(rsc->layout);
80 }
81 
82 /* A tile is either 64 bytes or, when the GPU has the CACHE128B256BPERLINE
83  * feature, 128/256 bytes of color/depth data, tracked by
84  * 'screen->specs.bits_per_tile' bits of tile status.
85  */
86 bool
etna_screen_resource_alloc_ts(struct pipe_screen * pscreen,struct etna_resource * rsc,uint64_t modifier)87 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
88                               struct etna_resource *rsc, uint64_t modifier)
89 {
90    struct etna_screen *screen = etna_screen(pscreen);
91    struct etna_resource_level *lvl = &rsc->levels[0];
92    struct pipe_resource *prsc = &rsc->base;
93    size_t tile_size, ts_size, ts_bo_size, ts_layer_stride, ts_data_offset = 0;
94    uint8_t ts_mode = TS_MODE_128B;
95    int8_t ts_compress_fmt = -1;
96    unsigned layers;
97 
98    assert(!rsc->ts_bo);
99 
100    /* pre-v4 compression is largely useless, so disable it when not wanted for MSAA
101     * v4 compression can be enabled everywhere without any known drawback,
102     * except that in-place resolve must go through a slower path
103     */
104    if ((screen->specs.v4_compression &&
105         (!modifier || (modifier & VIVANTE_MOD_COMP_DEC400))) ||
106        (!modifier && prsc->nr_samples > 1))
107       ts_compress_fmt = translate_ts_format(prsc->format);
108 
109    /* enable 256B ts mode with compression, as it improves performance
110     * the size of the resource might also determine if we want to use it or not
111     */
112    if (VIV_FEATURE(screen, ETNA_FEATURE_CACHE128B256BPERLINE)) {
113       if ((modifier & VIVANTE_MOD_TS_MASK) == VIVANTE_MOD_TS_128_4)
114          ts_mode = TS_MODE_128B;
115       else if ((modifier & VIVANTE_MOD_TS_MASK) == VIVANTE_MOD_TS_256_4)
116          ts_mode = TS_MODE_256B;
117       else {
118          /* Without a TS modifier TS is only internal, so we can choose the
119           * mode to use freely. Enable 256B ts mode with compression, as it
120           * improves performance. The size of the resource might also determine
121           * if we want to use it or not.
122           */
123          if (ts_compress_fmt >= 0 &&
124              (rsc->layout != ETNA_LAYOUT_LINEAR || lvl->stride % 256 == 0))
125             ts_mode = TS_MODE_256B;
126          else
127             ts_mode = TS_MODE_128B;
128       }
129    }
130 
131    tile_size = etna_screen_get_tile_size(screen, ts_mode, prsc->nr_samples > 1);
132    layers = prsc->target == PIPE_TEXTURE_3D ? prsc->depth0 : prsc->array_size;
133    ts_layer_stride = align(DIV_ROUND_UP(lvl->layer_stride,
134                                         tile_size * 8 / screen->specs.bits_per_tile),
135                            0x100 * screen->specs.pixel_pipes);
136    ts_size = ts_bo_size = ts_layer_stride * layers;
137    if (ts_size == 0)
138       return true;
139 
140    /* add space for the software meta */
141    if (modifier & VIVANTE_MOD_TS_MASK) {
142       /* The offset is a educated guess for a safe value based on the experience
143        * that various object pointers on the GPU need to be 64B aligned. Maybe
144        * some GPU needs even more alignment, or we could drop this to 32B. 64B
145        * has worked well across various GPU generations so far.
146        */
147       ts_data_offset = 64;
148       assert(ts_data_offset >= sizeof(struct etna_ts_sw_meta));
149       ts_bo_size += ts_data_offset;
150    }
151 
152    DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu",
153          rsc, ts_bo_size);
154 
155    if ((rsc->base.bind & PIPE_BIND_SCANOUT) && screen->ro) {
156       struct pipe_resource scanout_templat;
157       struct winsys_handle handle;
158 
159       scanout_templat.format = PIPE_FORMAT_R8_UNORM;
160       scanout_templat.width0 = align(ts_bo_size, 4096);
161       scanout_templat.height0 = 1;
162 
163       rsc->ts_scanout = renderonly_scanout_for_resource(&scanout_templat,
164                                                      screen->ro, &handle);
165       if (!rsc->ts_scanout) {
166          BUG("Problem allocating kms memory for TS resource");
167          return false;
168       }
169 
170       assert(handle.type == WINSYS_HANDLE_TYPE_FD);
171       rsc->ts_bo = etna_screen_bo_from_handle(pscreen, &handle);
172       close(handle.handle);
173    } else {
174       rsc->ts_bo = etna_bo_new(screen->dev, ts_bo_size, DRM_ETNA_GEM_CACHE_WC);
175    }
176 
177    if (unlikely(!rsc->ts_bo)) {
178       BUG("Problem allocating tile status for resource");
179       return false;
180    }
181 
182    lvl->ts_offset = ts_data_offset;
183    lvl->ts_layer_stride = ts_layer_stride;
184    lvl->ts_size = ts_size;
185    lvl->ts_mode = ts_mode;
186    lvl->ts_compress_fmt = ts_compress_fmt;
187 
188    /* fill software meta */
189    if (modifier & VIVANTE_MOD_TS_MASK) {
190       lvl->ts_meta = etna_bo_map(rsc->ts_bo);
191       memset(lvl->ts_meta, 0, sizeof(struct etna_ts_sw_meta));
192       lvl->ts_meta->version = 0;
193       lvl->ts_meta->v0.data_size = ts_size;
194       lvl->ts_meta->v0.data_offset = ts_data_offset;
195       lvl->ts_meta->v0.layer_stride = ts_layer_stride;
196       lvl->ts_meta->v0.comp_format = ts_format_to_drmfourcc(ts_compress_fmt);
197    }
198    return true;
199 }
200 
201 static bool
etna_screen_can_create_resource(struct pipe_screen * pscreen,const struct pipe_resource * templat)202 etna_screen_can_create_resource(struct pipe_screen *pscreen,
203                                 const struct pipe_resource *templat)
204 {
205    struct etna_screen *screen = etna_screen(pscreen);
206    if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL))
207       return false;
208 
209    /* templat->bind is not set here, so we must use the minimum sizes */
210    uint max_size =
211       MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size);
212 
213    if (templat->width0 > max_size || templat->height0 > max_size)
214       return false;
215 
216    return true;
217 }
218 
219 static unsigned
setup_miptree(struct etna_resource * rsc,unsigned paddingX,unsigned paddingY,unsigned msaa_xscale,unsigned msaa_yscale)220 setup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY,
221               unsigned msaa_xscale, unsigned msaa_yscale)
222 {
223    struct pipe_resource *prsc = &rsc->base;
224    unsigned level, size = 0;
225    unsigned width = prsc->width0;
226    unsigned height = prsc->height0;
227    unsigned depth = prsc->depth0;
228 
229    for (level = 0; level <= prsc->last_level; level++) {
230       struct etna_resource_level *mip = &rsc->levels[level];
231 
232       mip->width = width;
233       mip->height = height;
234       mip->depth = depth;
235       mip->padded_width = align(width * msaa_xscale, paddingX);
236       mip->padded_height = align(height * msaa_yscale, paddingY);
237       mip->stride = util_format_get_stride(prsc->format, mip->padded_width);
238       mip->offset = size;
239       mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height);
240       mip->size = prsc->array_size * mip->layer_stride;
241 
242       /* align levels to 64 bytes to be able to render to them */
243       size += align(mip->size, ETNA_PE_ALIGNMENT) * depth;
244 
245       width = u_minify(width, 1);
246       height = u_minify(height, 1);
247       depth = u_minify(depth, 1);
248    }
249 
250    return size;
251 }
252 
253 /* Compute the slice/miplevel alignment (in pixels) and the texture sampler
254  * HALIGN parameter from the resource parameters and the target layout.
255  */
256 static void
etna_layout_multiple(const struct etna_screen * screen,const struct pipe_resource * templat,unsigned layout,unsigned * paddingX,unsigned * paddingY,unsigned * halign)257 etna_layout_multiple(const struct etna_screen *screen,
258                      const struct pipe_resource *templat, unsigned layout,
259                      unsigned *paddingX, unsigned *paddingY, unsigned *halign)
260 {
261    const struct etna_specs *specs = &screen->specs;
262    /* If we have the TEXTURE_HALIGN feature, we can always align to the resolve
263     * engine's width.  If not, we must not align resources used only for
264     * textures. If this GPU uses the BLT engine, never do RS align.
265     */
266    bool rs_align = !specs->use_blt && (!etna_resource_sampler_only(templat) ||
267                    VIV_FEATURE(screen, ETNA_FEATURE_TEXTURE_HALIGN));
268    int msaa_xscale = 1, msaa_yscale = 1;
269 
270    /* Compressed textures are padded to their block size, but we don't have
271     * to do anything special for that.
272     */
273    if (unlikely(util_format_is_compressed(templat->format))) {
274       assert(layout == ETNA_LAYOUT_LINEAR);
275       *paddingX = 1;
276       *paddingY = 1;
277       *halign = TEXTURE_HALIGN_FOUR;
278       return;
279    }
280 
281    translate_samples_to_xyscale(templat->nr_samples, &msaa_xscale, &msaa_yscale);
282 
283    switch (layout) {
284    case ETNA_LAYOUT_LINEAR:
285       *paddingX = rs_align ? 16 : 4;
286       *paddingY = !specs->use_blt && templat->target != PIPE_BUFFER ? 4 : 1;
287       *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
288       break;
289    case ETNA_LAYOUT_TILED:
290       *paddingX = rs_align ? 16 * msaa_xscale : 4;
291       *paddingY = 4 * msaa_yscale;
292       *halign = rs_align ? TEXTURE_HALIGN_SIXTEEN : TEXTURE_HALIGN_FOUR;
293       break;
294    case ETNA_LAYOUT_SUPER_TILED:
295       *paddingX = 64;
296       *paddingY = 64;
297       *halign = TEXTURE_HALIGN_SUPER_TILED;
298       break;
299    case ETNA_LAYOUT_MULTI_TILED:
300       *paddingX = 16 * msaa_xscale;
301       *paddingY = 4 * msaa_yscale * specs->pixel_pipes;
302       *halign = TEXTURE_HALIGN_SPLIT_TILED;
303       break;
304    case ETNA_LAYOUT_MULTI_SUPERTILED:
305       *paddingX = 64;
306       *paddingY = 64 * specs->pixel_pipes;
307       *halign = TEXTURE_HALIGN_SPLIT_SUPER_TILED;
308       break;
309    default:
310       unreachable("Unhandled layout");
311    }
312 }
313 
314 /* Create a new resource object, using the given template info */
315 struct pipe_resource *
etna_resource_alloc(struct pipe_screen * pscreen,unsigned layout,uint64_t modifier,const struct pipe_resource * templat)316 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
317                     uint64_t modifier, const struct pipe_resource *templat)
318 {
319    struct etna_screen *screen = etna_screen(pscreen);
320    struct etna_resource *rsc;
321    unsigned size;
322 
323    DBG_F(ETNA_DBG_RESOURCE_MSGS,
324          "target=%d, format=%s, %ux%ux%u, array_size=%u, "
325          "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
326          templat->target, util_format_name(templat->format), templat->width0,
327          templat->height0, templat->depth0, templat->array_size,
328          templat->last_level, templat->nr_samples, templat->usage,
329          templat->bind, templat->flags);
330 
331    /* Determine scaling for antialiasing */
332    int msaa_xscale = 1, msaa_yscale = 1;
333    if (!translate_samples_to_xyscale(templat->nr_samples, &msaa_xscale, &msaa_yscale)) {
334       /* Number of samples not supported */
335       return NULL;
336    }
337 
338    /* Determine needed padding (alignment of height/width) */
339    unsigned paddingX, paddingY, halign;
340    etna_layout_multiple(screen, templat, layout, &paddingX, &paddingY, &halign);
341 
342    rsc = CALLOC_STRUCT(etna_resource);
343    if (!rsc)
344       return NULL;
345 
346    rsc->base = *templat;
347    rsc->base.screen = pscreen;
348    rsc->base.nr_samples = templat->nr_samples;
349    rsc->layout = layout;
350    rsc->modifier = modifier;
351    rsc->halign = halign;
352    rsc->explicit_flush = true;
353 
354    pipe_reference_init(&rsc->base.reference, 1);
355    util_range_init(&rsc->valid_buffer_range);
356 
357    size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale);
358 
359    if (unlikely(templat->bind & PIPE_BIND_SCANOUT) && screen->ro) {
360       struct pipe_resource scanout_templat = *templat;
361       struct winsys_handle handle;
362 
363       scanout_templat.width0 = align(scanout_templat.width0, paddingX);
364       scanout_templat.height0 = align(scanout_templat.height0, paddingY);
365 
366       rsc->scanout = renderonly_scanout_for_resource(&scanout_templat,
367                                                      screen->ro, &handle);
368       if (!rsc->scanout) {
369          BUG("Problem allocating kms memory for resource");
370          goto free_rsc;
371       }
372 
373       assert(handle.type == WINSYS_HANDLE_TYPE_FD);
374       rsc->levels[0].stride = handle.stride;
375       rsc->bo = etna_screen_bo_from_handle(pscreen, &handle);
376       close(handle.handle);
377       if (unlikely(!rsc->bo))
378          goto free_rsc;
379    } else {
380       uint32_t flags = DRM_ETNA_GEM_CACHE_WC;
381 
382       if (templat->bind & PIPE_BIND_VERTEX_BUFFER)
383          flags |= DRM_ETNA_GEM_FORCE_MMU;
384 
385       rsc->bo = etna_bo_new(screen->dev, size, flags);
386       if (unlikely(!rsc->bo)) {
387          BUG("Problem allocating video memory for resource");
388          goto free_rsc;
389       }
390    }
391 
392    /* If TS is externally visible set it up now, so it can be exported before
393     * the first rendering to a surface.
394     */
395    if (etna_resource_ext_ts(rsc))
396       etna_screen_resource_alloc_ts(pscreen, rsc, modifier);
397 
398    if (DBG_ENABLED(ETNA_DBG_ZERO)) {
399       void *map = etna_bo_map(rsc->bo);
400       etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE);
401       memset(map, 0, size);
402       etna_bo_cpu_fini(rsc->bo);
403    }
404 
405    return &rsc->base;
406 
407 free_rsc:
408    FREE(rsc);
409    return NULL;
410 }
411 
412 static struct pipe_resource *
etna_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * templat)413 etna_resource_create(struct pipe_screen *pscreen,
414                      const struct pipe_resource *templat)
415 {
416    struct etna_screen *screen = etna_screen(pscreen);
417    unsigned layout = ETNA_LAYOUT_TILED;
418 
419    /* At this point we don't know if the resource will be used as a texture,
420     * render target, or both, because gallium sets the bits whenever possible
421     * This matters because on some GPUs (GC2000) there is no tiling that is
422     * compatible with both TE and PE.
423     *
424     * We expect that depth/stencil buffers will always be used by PE (rendering),
425     * and any other non-scanout resource will be used as a texture at some point,
426     * So allocate a render-compatible base buffer for scanout/depthstencil buffers,
427     * and a texture-compatible base buffer in other cases
428     *
429     */
430    if (templat->bind & PIPE_BIND_DEPTH_STENCIL) {
431       if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
432          layout |= ETNA_LAYOUT_BIT_MULTI;
433       if (screen->specs.can_supertile)
434          layout |= ETNA_LAYOUT_BIT_SUPER;
435    } else if (screen->specs.can_supertile &&
436               VIV_FEATURE(screen, ETNA_FEATURE_SUPERTILED_TEXTURE) &&
437               etna_resource_hw_tileable(screen->specs.use_blt, templat)) {
438       layout |= ETNA_LAYOUT_BIT_SUPER;
439    }
440 
441    if (/* MSAA render target */
442        (templat->nr_samples > 1) &&
443        (templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL))) {
444       if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
445          layout |= ETNA_LAYOUT_BIT_MULTI;
446       if (screen->specs.can_supertile)
447          layout |= ETNA_LAYOUT_BIT_SUPER;
448    }
449 
450    if (/* linear base or scanout without modifier requested */
451        (templat->bind & (PIPE_BIND_LINEAR | PIPE_BIND_SCANOUT)) ||
452        templat->target == PIPE_BUFFER || /* buffer always linear */
453        /* compressed textures don't use tiling, they have their own "tiles" */
454        util_format_is_compressed(templat->format)) {
455       layout = ETNA_LAYOUT_LINEAR;
456    }
457 
458    /* modifier is only used for scanout surfaces, so safe to use LINEAR here */
459    return etna_resource_alloc(pscreen, layout, DRM_FORMAT_MOD_LINEAR, templat);
460 }
461 
462 enum modifier_priority {
463    MODIFIER_PRIORITY_INVALID = 0,
464    MODIFIER_PRIORITY_LINEAR,
465    MODIFIER_PRIORITY_SPLIT_TILED,
466    MODIFIER_PRIORITY_SPLIT_SUPER_TILED,
467    MODIFIER_PRIORITY_TILED,
468    MODIFIER_PRIORITY_SUPER_TILED,
469 };
470 
471 static const uint64_t priority_to_modifier[] = {
472    [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
473    [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
474    [MODIFIER_PRIORITY_SPLIT_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED,
475    [MODIFIER_PRIORITY_SPLIT_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED,
476    [MODIFIER_PRIORITY_TILED] = DRM_FORMAT_MOD_VIVANTE_TILED,
477    [MODIFIER_PRIORITY_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED,
478 };
479 
480 static uint64_t
select_best_modifier(const struct etna_screen * screen,const uint64_t * modifiers,const unsigned count)481 select_best_modifier(const struct etna_screen * screen,
482                      const uint64_t *modifiers, const unsigned count)
483 {
484    enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
485    uint64_t best_modifier, base_modifier;
486 
487    for (int i = 0; i < count; i++) {
488       switch (modifiers[i] & ~VIVANTE_MOD_EXT_MASK) {
489       case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
490          if ((screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) ||
491              !screen->specs.can_supertile)
492             break;
493          prio = MAX2(prio, MODIFIER_PRIORITY_SUPER_TILED);
494          break;
495       case DRM_FORMAT_MOD_VIVANTE_TILED:
496          if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer)
497             break;
498          prio = MAX2(prio, MODIFIER_PRIORITY_TILED);
499          break;
500       case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
501          if ((screen->specs.pixel_pipes < 2) || !screen->specs.can_supertile)
502             break;
503          prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_SUPER_TILED);
504          break;
505       case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
506          if (screen->specs.pixel_pipes < 2)
507             break;
508          prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_TILED);
509          break;
510       case DRM_FORMAT_MOD_LINEAR:
511          prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
512          break;
513       case DRM_FORMAT_MOD_INVALID:
514       default:
515          break;
516       }
517    }
518 
519    best_modifier = base_modifier = priority_to_modifier[prio];
520 
521    if (!DBG_ENABLED(ETNA_DBG_SHARED_TS) ||
522        !VIV_FEATURE(screen, ETNA_FEATURE_FAST_CLEAR))
523       return best_modifier;
524 
525    /* Make a second pass to try and find the best TS modifier if any. */
526    for (int i = 0; i < count; i++) {
527       if ((modifiers[i] & ~VIVANTE_MOD_EXT_MASK) == base_modifier)
528          if ((modifiers[i] & VIVANTE_MOD_TS_MASK) >
529              (best_modifier & VIVANTE_MOD_TS_MASK))
530             best_modifier = modifiers[i];
531    }
532 
533    /* If no modifier with TS was found, there is no point in looking further,
534     * as compression depends on TS.
535     */
536    if (best_modifier == base_modifier)
537       return best_modifier;
538 
539    /* Make a third pass to find a modifier allowing compression. */
540    base_modifier = best_modifier;
541    for (int i = 0; i < count; i++) {
542       if ((modifiers[i] & ~VIVANTE_MOD_COMP_MASK) == base_modifier)
543          if ((modifiers[i] & VIVANTE_MOD_COMP_MASK) >
544              (best_modifier & VIVANTE_MOD_COMP_MASK))
545             best_modifier = modifiers[i];
546    }
547 
548    return best_modifier;
549 }
550 
551 static struct pipe_resource *
etna_resource_create_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * templat,const uint64_t * modifiers,int count)552 etna_resource_create_modifiers(struct pipe_screen *pscreen,
553                                const struct pipe_resource *templat,
554                                const uint64_t *modifiers, int count)
555 {
556    struct etna_screen *screen = etna_screen(pscreen);
557    struct pipe_resource tmpl = *templat;
558    uint64_t modifier = select_best_modifier(screen, modifiers, count);
559 
560    if (modifier == DRM_FORMAT_MOD_INVALID)
561       return NULL;
562 
563    return etna_resource_alloc(pscreen, modifier_to_layout(modifier), modifier, &tmpl);
564 }
565 
566 static void
etna_resource_changed(struct pipe_screen * pscreen,struct pipe_resource * prsc)567 etna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc)
568 {
569    struct etna_resource *rsc = etna_resource(prsc);
570 
571    for (int level = 0; level <= prsc->last_level; level++)
572       etna_resource_level_mark_changed(&rsc->levels[level]);
573 }
574 
575 static void
etna_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)576 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
577 {
578    struct etna_resource *rsc = etna_resource(prsc);
579 
580    if (rsc->bo)
581       etna_bo_del(rsc->bo);
582 
583    if (rsc->ts_bo)
584       etna_bo_del(rsc->ts_bo);
585 
586    if (rsc->scanout)
587       renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
588 
589    if (rsc->ts_scanout)
590       renderonly_scanout_destroy(rsc->ts_scanout, etna_screen(pscreen)->ro);
591 
592    util_range_destroy(&rsc->valid_buffer_range);
593 
594    pipe_resource_reference(&rsc->texture, NULL);
595    pipe_resource_reference(&rsc->render, NULL);
596 
597    for (unsigned i = 0; i < ETNA_NUM_LOD; i++)
598       FREE(rsc->levels[i].patch_offsets);
599 
600    FREE(rsc);
601 }
602 
etna_resource_finish_ts_import(struct etna_screen * screen,struct etna_resource * rsc)603 static void etna_resource_finish_ts_import(struct etna_screen *screen,
604                                            struct etna_resource *rsc)
605 {
606    struct etna_resource *ts_rsc = etna_resource(rsc->base.next);
607    uint64_t ts_modifier = rsc->modifier & VIVANTE_MOD_TS_MASK;
608    struct etna_resource_level *lvl = &rsc->levels[0];
609    uint8_t ts_mode = 0;
610 
611    if (ts_rsc->bo == rsc->bo)
612       fprintf(stderr, "etnaviv: application bug: importing shared TS resource "
613               "with TS BO matching color BO, expect rendering corruption!\n");
614 
615    if (ts_modifier == VIVANTE_MOD_TS_256_4)
616       ts_mode = TS_MODE_256B;
617 
618    rsc->ts_bo = etna_bo_ref(ts_rsc->bo);
619 
620    rsc->ts_scanout = ts_rsc->scanout;
621    ts_rsc->scanout = NULL;
622 
623    /* Get TS layout/usage information from the SW meta. FIXME: clear color may
624     * change over the lifetime of the resource, so might need to look this up
625     * at a few other places. For now it's not an issue, as buffers with shared
626     * TS get re-imported all the time. */
627    lvl->ts_meta = etna_bo_map(rsc->ts_bo) + ts_rsc->levels[0].offset;
628    lvl->ts_compress_fmt = drmfourcc_to_ts_format(lvl->ts_meta->v0.comp_format);
629    lvl->ts_offset = ts_rsc->levels[0].offset + lvl->ts_meta->v0.data_offset;
630    lvl->ts_layer_stride = lvl->ts_meta->v0.layer_stride;
631    lvl->clear_value = lvl->ts_meta->v0.clear_value;
632    lvl->ts_size = lvl->ts_meta->v0.data_size;
633    lvl->ts_mode = ts_mode;
634 
635    etna_resource_destroy(&screen->base, rsc->base.next);
636    rsc->base.next = NULL;
637 }
638 
639 static struct pipe_resource *
etna_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * handle,unsigned usage)640 etna_resource_from_handle(struct pipe_screen *pscreen,
641                           const struct pipe_resource *tmpl,
642                           struct winsys_handle *handle, unsigned usage)
643 {
644    struct etna_screen *screen = etna_screen(pscreen);
645    struct etna_resource *rsc;
646    struct etna_resource_level *level;
647    struct pipe_resource *prsc;
648    uint64_t modifier = handle->modifier;
649 
650    DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
651        "nr_samples=%u, usage=%u, bind=%x, flags=%x",
652        tmpl->target, util_format_name(tmpl->format), tmpl->width0,
653        tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
654        tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
655 
656    rsc = CALLOC_STRUCT(etna_resource);
657    if (!rsc)
658       return NULL;
659 
660    level = &rsc->levels[0];
661    prsc = &rsc->base;
662 
663    *prsc = *tmpl;
664 
665    pipe_reference_init(&prsc->reference, 1);
666    util_range_init(&rsc->valid_buffer_range);
667    prsc->screen = pscreen;
668 
669    rsc->bo = etna_screen_bo_from_handle(pscreen, handle);
670    if (!rsc->bo)
671       goto fail;
672 
673    if (modifier == DRM_FORMAT_MOD_INVALID)
674       modifier = DRM_FORMAT_MOD_LINEAR;
675 
676    rsc->layout = modifier_to_layout(modifier);
677    rsc->modifier = modifier;
678 
679    rsc->shared = true;
680    if (usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH)
681       rsc->explicit_flush = true;
682 
683    level->width = tmpl->width0;
684    level->height = tmpl->height0;
685    level->depth = tmpl->depth0;
686    level->stride = handle->stride;
687    level->offset = handle->offset;
688    level->seqno = 1;
689 
690    /* Determine padding of the imported resource. */
691    unsigned paddingX, paddingY;
692    etna_layout_multiple(screen, tmpl, rsc->layout,
693                         &paddingX, &paddingY, &rsc->halign);
694 
695    level->padded_width = align(level->width, paddingX);
696    level->padded_height = align(level->height, paddingY);
697 
698    level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
699                                                                   level->padded_height);
700    level->size = level->layer_stride;
701 
702    if (screen->ro)
703       rsc->scanout = renderonly_create_gpu_import_for_resource(prsc, screen->ro,
704                                                                NULL);
705 
706    /* If the buffer is for a TS plane, skip the RS compatible checks */
707    if (handle->plane >= util_format_get_num_planes(prsc->format))
708       return prsc;
709 
710    /* The DDX must give us a BO which conforms to our padding size.
711     * The stride of the BO must be greater or equal to our padded
712     * stride. The size of the BO must accomodate the padded height. */
713    if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
714       BUG("BO stride %u is too small for RS engine width padding (%u, format %s)",
715           level->stride, util_format_get_stride(tmpl->format, level->padded_width),
716           util_format_name(tmpl->format));
717       goto fail;
718    }
719    if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
720       BUG("BO size %u is too small for RS engine height padding (%u, format %s)",
721           etna_bo_size(rsc->bo), level->stride * level->padded_height,
722           util_format_name(tmpl->format));
723       goto fail;
724    }
725 
726    if (handle->plane == 0 && etna_resource_ext_ts(rsc))
727       etna_resource_finish_ts_import(screen, rsc);
728 
729    return prsc;
730 
731 fail:
732    etna_resource_destroy(pscreen, prsc);
733 
734    return NULL;
735 }
736 
737 static bool
etna_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * handle,unsigned usage)738 etna_resource_get_handle(struct pipe_screen *pscreen,
739                          struct pipe_context *pctx,
740                          struct pipe_resource *prsc,
741                          struct winsys_handle *handle, unsigned usage)
742 {
743    struct etna_screen *screen = etna_screen(pscreen);
744    struct etna_resource *rsc = etna_resource(prsc);
745    bool wants_ts = etna_resource_ext_ts(rsc) &&
746                       handle->plane >= util_format_get_num_planes(prsc->format);
747    struct renderonly_scanout *scanout;
748    struct etna_resource_level *lvl;
749    struct etna_bo *bo;
750 
751    if (handle->plane && !wants_ts) {
752       struct pipe_resource *cur = prsc;
753 
754       for (int i = 0; i < handle->plane; i++) {
755          cur = cur->next;
756          if (!cur)
757             return false;
758       }
759       rsc = etna_resource(cur);
760    }
761 
762    /* Scanout is always attached to the base resource */
763    scanout = rsc->scanout;
764 
765    lvl = &rsc->levels[0];
766 
767    if (wants_ts) {
768       handle->stride = DIV_ROUND_UP(lvl->stride,
769                           etna_screen_get_tile_size(screen, lvl->ts_mode, false)
770                           * 8 / screen->specs.bits_per_tile);
771       handle->offset = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
772       scanout = rsc->ts_scanout;
773       bo = rsc->ts_bo;
774    } else {
775       handle->stride = lvl->stride;
776       handle->offset = lvl->offset;
777       scanout = rsc->scanout;
778       bo = rsc->bo;
779    }
780    handle->modifier = etna_resource_modifier(rsc);
781 
782    rsc->shared = true;
783    if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
784       rsc->explicit_flush = false;
785 
786    if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
787       return etna_bo_get_name(bo, &handle->handle) == 0;
788    } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
789       if (screen->ro) {
790          return renderonly_get_handle(scanout, handle);
791       } else {
792          handle->handle = etna_bo_handle(bo);
793          return true;
794       }
795    } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
796       handle->handle = etna_bo_dmabuf(bo);
797       return true;
798    } else {
799       return false;
800    }
801 }
802 
803 static bool
etna_resource_get_param(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,unsigned plane,unsigned layer,unsigned level,enum pipe_resource_param param,unsigned usage,uint64_t * value)804 etna_resource_get_param(struct pipe_screen *pscreen,
805                         struct pipe_context *pctx, struct pipe_resource *prsc,
806                         unsigned plane, unsigned layer, unsigned level,
807                         enum pipe_resource_param param,
808                         unsigned usage, uint64_t *value)
809 {
810    struct etna_screen *screen = etna_screen(pscreen);
811    struct etna_resource *rsc = etna_resource(prsc);
812    bool wants_ts = etna_resource_ext_ts(rsc) &&
813                       plane >= util_format_get_num_planes(prsc->format);
814    struct etna_resource_level *lvl;
815 
816    if (param == PIPE_RESOURCE_PARAM_NPLANES) {
817       if (etna_resource_ext_ts(rsc)) {
818                *value = 2;
819       } else {
820          unsigned count = 0;
821 
822          for (struct pipe_resource *cur = prsc; cur; cur = cur->next)
823             count++;
824          *value = count;
825       }
826       return true;
827    }
828 
829    if (!wants_ts) {
830       struct pipe_resource *cur = prsc;
831       for (int i = 0; i < plane; i++) {
832          cur = cur->next;
833          if (!cur)
834             return false;
835       }
836       rsc = etna_resource(cur);
837    }
838 
839    lvl = &rsc->levels[0];
840 
841    switch (param) {
842    case PIPE_RESOURCE_PARAM_STRIDE:
843       if (wants_ts) {
844          *value = DIV_ROUND_UP(lvl->stride,
845                                etna_screen_get_tile_size(screen, lvl->ts_mode,
846                                                          prsc->nr_samples > 1)
847                                * 8 / screen->specs.bits_per_tile);
848       } else {
849          *value = lvl->stride;
850       }
851       return true;
852    case PIPE_RESOURCE_PARAM_OFFSET:
853       if (wants_ts)
854          *value = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
855       else
856          *value = lvl->offset;
857       return true;
858    case PIPE_RESOURCE_PARAM_MODIFIER:
859       *value = etna_resource_modifier(rsc);
860       return true;
861    default:
862       return false;
863    }
864 }
865 
866 void
etna_resource_used(struct etna_context * ctx,struct pipe_resource * prsc,enum etna_resource_status status)867 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
868                    enum etna_resource_status status)
869 {
870    struct etna_resource *rsc;
871    struct hash_entry *entry;
872    uint32_t hash;
873 
874    if (!prsc)
875       return;
876 
877    rsc = etna_resource(prsc);
878    hash = _mesa_hash_pointer(rsc);
879    entry = _mesa_hash_table_search_pre_hashed(ctx->pending_resources,
880                                               hash, rsc);
881 
882    if (entry) {
883       enum etna_resource_status tmp = (uintptr_t)entry->data;
884       tmp |= status;
885       entry->data = (void *)(uintptr_t)tmp;
886    } else {
887       _mesa_hash_table_insert_pre_hashed(ctx->pending_resources, hash, rsc,
888                                          (void *)(uintptr_t)status);
889    }
890 }
891 
892 enum etna_resource_status
etna_resource_status(struct etna_context * ctx,struct etna_resource * res)893 etna_resource_status(struct etna_context *ctx, struct etna_resource *res)
894 {
895    struct hash_entry *entry = _mesa_hash_table_search(ctx->pending_resources, res);
896 
897    if (entry)
898       return (enum etna_resource_status)(uintptr_t)entry->data;
899    else
900       return 0;
901 }
902 
903 bool
etna_resource_needs_flush(struct etna_resource * rsc)904 etna_resource_needs_flush(struct etna_resource *rsc)
905 {
906    if (!rsc->ts_bo)
907       return false;
908 
909    for (int level = 0; level <= rsc->base.last_level; level++)
910       if (etna_resource_level_needs_flush(&rsc->levels[level]))
911          return true;
912 
913    return false;
914 }
915 
916 void
etna_resource_screen_init(struct pipe_screen * pscreen)917 etna_resource_screen_init(struct pipe_screen *pscreen)
918 {
919    pscreen->can_create_resource = etna_screen_can_create_resource;
920    pscreen->resource_create = etna_resource_create;
921    pscreen->resource_create_with_modifiers = etna_resource_create_modifiers;
922    pscreen->resource_from_handle = etna_resource_from_handle;
923    pscreen->resource_get_handle = etna_resource_get_handle;
924    pscreen->resource_get_param = etna_resource_get_param;
925    pscreen->resource_changed = etna_resource_changed;
926    pscreen->resource_destroy = etna_resource_destroy;
927 }
928