• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <laanwj@gmail.com>
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       etna_resource_level_mark_unflushed(&rsc->levels[level]);
574    }
575 }
576 
577 static void
etna_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)578 etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
579 {
580    struct etna_resource *rsc = etna_resource(prsc);
581 
582    if (rsc->bo)
583       etna_bo_del(rsc->bo);
584 
585    if (rsc->ts_bo)
586       etna_bo_del(rsc->ts_bo);
587 
588    if (rsc->scanout)
589       renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro);
590 
591    if (rsc->ts_scanout)
592       renderonly_scanout_destroy(rsc->ts_scanout, etna_screen(pscreen)->ro);
593 
594    util_range_destroy(&rsc->valid_buffer_range);
595 
596    pipe_resource_reference(&rsc->texture, NULL);
597    pipe_resource_reference(&rsc->render, NULL);
598 
599    for (unsigned i = 0; i < ETNA_NUM_LOD; i++)
600       FREE(rsc->levels[i].patch_offsets);
601 
602    FREE(rsc);
603 }
604 
etna_resource_finish_ts_import(struct etna_screen * screen,struct etna_resource * rsc)605 static void etna_resource_finish_ts_import(struct etna_screen *screen,
606                                            struct etna_resource *rsc)
607 {
608    struct etna_resource *ts_rsc = etna_resource(rsc->base.next);
609    uint64_t ts_modifier = rsc->modifier & VIVANTE_MOD_TS_MASK;
610    struct etna_resource_level *lvl = &rsc->levels[0];
611    uint8_t ts_mode = 0;
612 
613    if (ts_rsc->bo == rsc->bo)
614       fprintf(stderr, "etnaviv: application bug: importing shared TS resource "
615               "with TS BO matching color BO, expect rendering corruption!\n");
616 
617    if (ts_modifier == VIVANTE_MOD_TS_256_4)
618       ts_mode = TS_MODE_256B;
619 
620    rsc->ts_bo = etna_bo_ref(ts_rsc->bo);
621 
622    rsc->ts_scanout = ts_rsc->scanout;
623    ts_rsc->scanout = NULL;
624 
625    /* Get TS layout/usage information from the SW meta. FIXME: clear color may
626     * change over the lifetime of the resource, so might need to look this up
627     * at a few other places. For now it's not an issue, as buffers with shared
628     * TS get re-imported all the time. */
629    lvl->ts_meta = etna_bo_map(rsc->ts_bo) + ts_rsc->levels[0].offset;
630    lvl->ts_compress_fmt = drmfourcc_to_ts_format(lvl->ts_meta->v0.comp_format);
631    lvl->ts_offset = ts_rsc->levels[0].offset + lvl->ts_meta->v0.data_offset;
632    lvl->ts_layer_stride = lvl->ts_meta->v0.layer_stride;
633    lvl->clear_value = lvl->ts_meta->v0.clear_value;
634    lvl->ts_size = lvl->ts_meta->v0.data_size;
635    lvl->ts_mode = ts_mode;
636 
637    etna_resource_destroy(&screen->base, rsc->base.next);
638    rsc->base.next = NULL;
639 }
640 
641 static struct pipe_resource *
etna_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * handle,unsigned usage)642 etna_resource_from_handle(struct pipe_screen *pscreen,
643                           const struct pipe_resource *tmpl,
644                           struct winsys_handle *handle, unsigned usage)
645 {
646    struct etna_screen *screen = etna_screen(pscreen);
647    struct etna_resource *rsc;
648    struct etna_resource_level *level;
649    struct pipe_resource *prsc;
650    uint64_t modifier = handle->modifier;
651 
652    DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
653        "nr_samples=%u, usage=%u, bind=%x, flags=%x",
654        tmpl->target, util_format_name(tmpl->format), tmpl->width0,
655        tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level,
656        tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags);
657 
658    rsc = CALLOC_STRUCT(etna_resource);
659    if (!rsc)
660       return NULL;
661 
662    level = &rsc->levels[0];
663    prsc = &rsc->base;
664 
665    *prsc = *tmpl;
666 
667    pipe_reference_init(&prsc->reference, 1);
668    util_range_init(&rsc->valid_buffer_range);
669    prsc->screen = pscreen;
670 
671    rsc->bo = etna_screen_bo_from_handle(pscreen, handle);
672    if (!rsc->bo)
673       goto fail;
674 
675    if (modifier == DRM_FORMAT_MOD_INVALID)
676       modifier = DRM_FORMAT_MOD_LINEAR;
677 
678    rsc->layout = modifier_to_layout(modifier);
679    rsc->modifier = modifier;
680 
681    rsc->shared = true;
682    if (usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH)
683       rsc->explicit_flush = true;
684 
685    level->width = tmpl->width0;
686    level->height = tmpl->height0;
687    level->depth = tmpl->depth0;
688    level->stride = handle->stride;
689    level->offset = handle->offset;
690    level->seqno = 1;
691 
692    /* Determine padding of the imported resource. */
693    unsigned paddingX, paddingY;
694    etna_layout_multiple(screen, tmpl, rsc->layout,
695                         &paddingX, &paddingY, &rsc->halign);
696 
697    level->padded_width = align(level->width, paddingX);
698    level->padded_height = align(level->height, paddingY);
699 
700    level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format,
701                                                                   level->padded_height);
702    level->size = level->layer_stride;
703 
704    if (screen->ro)
705       rsc->scanout = renderonly_create_gpu_import_for_resource(prsc, screen->ro,
706                                                                NULL);
707 
708    /* If the buffer is for a TS plane, skip the RS compatible checks */
709    if (handle->plane >= util_format_get_num_planes(prsc->format))
710       return prsc;
711 
712    /* The DDX must give us a BO which conforms to our padding size.
713     * The stride of the BO must be greater or equal to our padded
714     * stride. The size of the BO must accomodate the padded height. */
715    if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) {
716       BUG("BO stride %u is too small for RS engine width padding (%u, format %s)",
717           level->stride, util_format_get_stride(tmpl->format, level->padded_width),
718           util_format_name(tmpl->format));
719       goto fail;
720    }
721    if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) {
722       BUG("BO size %u is too small for RS engine height padding (%u, format %s)",
723           etna_bo_size(rsc->bo), level->stride * level->padded_height,
724           util_format_name(tmpl->format));
725       goto fail;
726    }
727 
728    if (handle->plane == 0 && etna_resource_ext_ts(rsc))
729       etna_resource_finish_ts_import(screen, rsc);
730 
731    return prsc;
732 
733 fail:
734    etna_resource_destroy(pscreen, prsc);
735 
736    return NULL;
737 }
738 
739 static bool
etna_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * handle,unsigned usage)740 etna_resource_get_handle(struct pipe_screen *pscreen,
741                          struct pipe_context *pctx,
742                          struct pipe_resource *prsc,
743                          struct winsys_handle *handle, unsigned usage)
744 {
745    struct etna_screen *screen = etna_screen(pscreen);
746    struct etna_resource *rsc = etna_resource(prsc);
747    bool wants_ts = etna_resource_ext_ts(rsc) &&
748                       handle->plane >= util_format_get_num_planes(prsc->format);
749    struct renderonly_scanout *scanout;
750    struct etna_resource_level *lvl;
751    struct etna_bo *bo;
752 
753    if (handle->plane && !wants_ts) {
754       struct pipe_resource *cur = prsc;
755 
756       for (int i = 0; i < handle->plane; i++) {
757          cur = cur->next;
758          if (!cur)
759             return false;
760       }
761       rsc = etna_resource(cur);
762    }
763 
764    /* Scanout is always attached to the base resource */
765    scanout = rsc->scanout;
766 
767    lvl = &rsc->levels[0];
768 
769    if (wants_ts) {
770       handle->stride = DIV_ROUND_UP(lvl->stride,
771                           etna_screen_get_tile_size(screen, lvl->ts_mode, false)
772                           * 8 / screen->specs.bits_per_tile);
773       handle->offset = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
774       scanout = rsc->ts_scanout;
775       bo = rsc->ts_bo;
776    } else {
777       handle->stride = lvl->stride;
778       handle->offset = lvl->offset;
779       scanout = rsc->scanout;
780       bo = rsc->bo;
781    }
782    handle->modifier = etna_resource_modifier(rsc);
783 
784    rsc->shared = true;
785    if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH))
786       rsc->explicit_flush = false;
787 
788    if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
789       return etna_bo_get_name(bo, &handle->handle) == 0;
790    } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
791       if (screen->ro) {
792          return renderonly_get_handle(scanout, handle);
793       } else {
794          handle->handle = etna_bo_handle(bo);
795          return true;
796       }
797    } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
798       handle->handle = etna_bo_dmabuf(bo);
799       return true;
800    } else {
801       return false;
802    }
803 }
804 
805 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)806 etna_resource_get_param(struct pipe_screen *pscreen,
807                         struct pipe_context *pctx, struct pipe_resource *prsc,
808                         unsigned plane, unsigned layer, unsigned level,
809                         enum pipe_resource_param param,
810                         unsigned usage, uint64_t *value)
811 {
812    struct etna_screen *screen = etna_screen(pscreen);
813    struct etna_resource *rsc = etna_resource(prsc);
814    bool wants_ts = etna_resource_ext_ts(rsc) &&
815                       plane >= util_format_get_num_planes(prsc->format);
816    struct etna_resource_level *lvl;
817 
818    if (param == PIPE_RESOURCE_PARAM_NPLANES) {
819       if (etna_resource_ext_ts(rsc)) {
820                *value = 2;
821       } else {
822          unsigned count = 0;
823 
824          for (struct pipe_resource *cur = prsc; cur; cur = cur->next)
825             count++;
826          *value = count;
827       }
828       return true;
829    }
830 
831    if (!wants_ts) {
832       struct pipe_resource *cur = prsc;
833       for (int i = 0; i < plane; i++) {
834          cur = cur->next;
835          if (!cur)
836             return false;
837       }
838       rsc = etna_resource(cur);
839    }
840 
841    lvl = &rsc->levels[0];
842 
843    switch (param) {
844    case PIPE_RESOURCE_PARAM_STRIDE:
845       if (wants_ts) {
846          *value = DIV_ROUND_UP(lvl->stride,
847                                etna_screen_get_tile_size(screen, lvl->ts_mode,
848                                                          prsc->nr_samples > 1)
849                                * 8 / screen->specs.bits_per_tile);
850       } else {
851          *value = lvl->stride;
852       }
853       return true;
854    case PIPE_RESOURCE_PARAM_OFFSET:
855       if (wants_ts)
856          *value = lvl->ts_offset - lvl->ts_meta->v0.data_offset;
857       else
858          *value = lvl->offset;
859       return true;
860    case PIPE_RESOURCE_PARAM_MODIFIER:
861       *value = etna_resource_modifier(rsc);
862       return true;
863    default:
864       return false;
865    }
866 }
867 
868 void
etna_resource_used(struct etna_context * ctx,struct pipe_resource * prsc,enum etna_resource_status status)869 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
870                    enum etna_resource_status status)
871 {
872    struct etna_resource *rsc;
873    struct hash_entry *entry;
874    uint32_t hash;
875 
876    if (!prsc)
877       return;
878 
879    rsc = etna_resource(prsc);
880    hash = _mesa_hash_pointer(rsc);
881    entry = _mesa_hash_table_search_pre_hashed(ctx->pending_resources,
882                                               hash, rsc);
883 
884    if (entry) {
885       enum etna_resource_status tmp = (uintptr_t)entry->data;
886       tmp |= status;
887       entry->data = (void *)(uintptr_t)tmp;
888    } else {
889       _mesa_hash_table_insert_pre_hashed(ctx->pending_resources, hash, rsc,
890                                          (void *)(uintptr_t)status);
891    }
892 }
893 
894 enum etna_resource_status
etna_resource_status(struct etna_context * ctx,struct etna_resource * res)895 etna_resource_status(struct etna_context *ctx, struct etna_resource *res)
896 {
897    struct hash_entry *entry = _mesa_hash_table_search(ctx->pending_resources, res);
898 
899    if (entry)
900       return (enum etna_resource_status)(uintptr_t)entry->data;
901    else
902       return 0;
903 }
904 
905 bool
etna_resource_needs_flush(struct etna_resource * rsc)906 etna_resource_needs_flush(struct etna_resource *rsc)
907 {
908    if (!rsc->ts_bo)
909       return false;
910 
911    for (int level = 0; level <= rsc->base.last_level; level++)
912       if (etna_resource_level_needs_flush(&rsc->levels[level]))
913          return true;
914 
915    return false;
916 }
917 
918 void
etna_resource_screen_init(struct pipe_screen * pscreen)919 etna_resource_screen_init(struct pipe_screen *pscreen)
920 {
921    pscreen->can_create_resource = etna_screen_can_create_resource;
922    pscreen->resource_create = etna_resource_create;
923    pscreen->resource_create_with_modifiers = etna_resource_create_modifiers;
924    pscreen->resource_from_handle = etna_resource_from_handle;
925    pscreen->resource_get_handle = etna_resource_get_handle;
926    pscreen->resource_get_param = etna_resource_get_param;
927    pscreen->resource_changed = etna_resource_changed;
928    pscreen->resource_destroy = etna_resource_destroy;
929 }
930