101e04c3fSmrg/* 201e04c3fSmrg * Copyright (c) 2012-2015 Etnaviv Project 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sub license, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the 1201e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions 1301e04c3fSmrg * of the Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2101e04c3fSmrg * DEALINGS IN THE SOFTWARE. 2201e04c3fSmrg * 2301e04c3fSmrg * Authors: 2401e04c3fSmrg * Wladimir J. van der Laan <laanwj@gmail.com> 2501e04c3fSmrg */ 2601e04c3fSmrg 2701e04c3fSmrg#include "etnaviv_resource.h" 2801e04c3fSmrg 2901e04c3fSmrg#include "hw/common.xml.h" 3001e04c3fSmrg 3101e04c3fSmrg#include "etnaviv_context.h" 3201e04c3fSmrg#include "etnaviv_debug.h" 3301e04c3fSmrg#include "etnaviv_screen.h" 3401e04c3fSmrg#include "etnaviv_translate.h" 3501e04c3fSmrg 369f464c52Smaya#include "util/hash_table.h" 3701e04c3fSmrg#include "util/u_inlines.h" 3801e04c3fSmrg#include "util/u_memory.h" 3901e04c3fSmrg 409f464c52Smaya#include "drm-uapi/drm_fourcc.h" 4101e04c3fSmrg 4201e04c3fSmrgstatic enum etna_surface_layout modifier_to_layout(uint64_t modifier) 4301e04c3fSmrg{ 4401e04c3fSmrg switch (modifier) { 4501e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_TILED: 4601e04c3fSmrg return ETNA_LAYOUT_TILED; 4701e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED: 4801e04c3fSmrg return ETNA_LAYOUT_SUPER_TILED; 4901e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED: 5001e04c3fSmrg return ETNA_LAYOUT_MULTI_TILED; 5101e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED: 5201e04c3fSmrg return ETNA_LAYOUT_MULTI_SUPERTILED; 5301e04c3fSmrg case DRM_FORMAT_MOD_LINEAR: 5401e04c3fSmrg default: 5501e04c3fSmrg return ETNA_LAYOUT_LINEAR; 5601e04c3fSmrg } 5701e04c3fSmrg} 5801e04c3fSmrg 5901e04c3fSmrgstatic uint64_t layout_to_modifier(enum etna_surface_layout layout) 6001e04c3fSmrg{ 6101e04c3fSmrg switch (layout) { 6201e04c3fSmrg case ETNA_LAYOUT_TILED: 6301e04c3fSmrg return DRM_FORMAT_MOD_VIVANTE_TILED; 6401e04c3fSmrg case ETNA_LAYOUT_SUPER_TILED: 6501e04c3fSmrg return DRM_FORMAT_MOD_VIVANTE_SUPER_TILED; 6601e04c3fSmrg case ETNA_LAYOUT_MULTI_TILED: 6701e04c3fSmrg return DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED; 6801e04c3fSmrg case ETNA_LAYOUT_MULTI_SUPERTILED: 6901e04c3fSmrg return DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED; 7001e04c3fSmrg case ETNA_LAYOUT_LINEAR: 7101e04c3fSmrg return DRM_FORMAT_MOD_LINEAR; 7201e04c3fSmrg default: 7301e04c3fSmrg return DRM_FORMAT_MOD_INVALID; 7401e04c3fSmrg } 7501e04c3fSmrg} 7601e04c3fSmrg 7701e04c3fSmrg/* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status. 7801e04c3fSmrg * So, in a buffer of N pixels, there are N / (4 * 4) tiles. 7901e04c3fSmrg * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or 8001e04c3fSmrg * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes. 8101e04c3fSmrg */ 8201e04c3fSmrgbool 8301e04c3fSmrgetna_screen_resource_alloc_ts(struct pipe_screen *pscreen, 8401e04c3fSmrg struct etna_resource *rsc) 8501e04c3fSmrg{ 8601e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 877ec681f3Smrg size_t rt_ts_size, ts_layer_stride; 887ec681f3Smrg size_t ts_bits_per_tile, bytes_per_tile; 897ec681f3Smrg uint8_t ts_mode = TS_MODE_128B; /* only used by halti5 */ 907ec681f3Smrg int8_t ts_compress_fmt; 9101e04c3fSmrg 9201e04c3fSmrg assert(!rsc->ts_bo); 9301e04c3fSmrg 947ec681f3Smrg /* pre-v4 compression is largely useless, so disable it when not wanted for MSAA 957ec681f3Smrg * v4 compression can be enabled everywhere without any known drawback, 967ec681f3Smrg * except that in-place resolve must go through a slower path 977ec681f3Smrg */ 987ec681f3Smrg ts_compress_fmt = (screen->specs.v4_compression || rsc->base.nr_samples > 1) ? 997ec681f3Smrg translate_ts_format(rsc->base.format) : -1; 1007ec681f3Smrg 1017ec681f3Smrg if (screen->specs.halti >= 5) { 1027ec681f3Smrg /* enable 256B ts mode with compression, as it improves performance 1037ec681f3Smrg * the size of the resource might also determine if we want to use it or not 1047ec681f3Smrg */ 1057ec681f3Smrg if (ts_compress_fmt >= 0) 1067ec681f3Smrg ts_mode = TS_MODE_256B; 1077ec681f3Smrg 1087ec681f3Smrg ts_bits_per_tile = 4; 1097ec681f3Smrg bytes_per_tile = ts_mode == TS_MODE_256B ? 256 : 128; 1107ec681f3Smrg } else { 1117ec681f3Smrg ts_bits_per_tile = screen->specs.bits_per_tile; 1127ec681f3Smrg bytes_per_tile = 64; 1137ec681f3Smrg } 1147ec681f3Smrg 1157ec681f3Smrg ts_layer_stride = align(DIV_ROUND_UP(rsc->levels[0].layer_stride, 1167ec681f3Smrg bytes_per_tile * 8 / ts_bits_per_tile), 11701e04c3fSmrg 0x100 * screen->specs.pixel_pipes); 11801e04c3fSmrg rt_ts_size = ts_layer_stride * rsc->base.array_size; 11901e04c3fSmrg if (rt_ts_size == 0) 12001e04c3fSmrg return true; 12101e04c3fSmrg 12201e04c3fSmrg DBG_F(ETNA_DBG_RESOURCE_MSGS, "%p: Allocating tile status of size %zu", 12301e04c3fSmrg rsc, rt_ts_size); 12401e04c3fSmrg 12501e04c3fSmrg struct etna_bo *rt_ts; 12601e04c3fSmrg rt_ts = etna_bo_new(screen->dev, rt_ts_size, DRM_ETNA_GEM_CACHE_WC); 12701e04c3fSmrg 12801e04c3fSmrg if (unlikely(!rt_ts)) { 12901e04c3fSmrg BUG("Problem allocating tile status for resource"); 13001e04c3fSmrg return false; 13101e04c3fSmrg } 13201e04c3fSmrg 13301e04c3fSmrg rsc->ts_bo = rt_ts; 13401e04c3fSmrg rsc->levels[0].ts_offset = 0; 13501e04c3fSmrg rsc->levels[0].ts_layer_stride = ts_layer_stride; 13601e04c3fSmrg rsc->levels[0].ts_size = rt_ts_size; 1377ec681f3Smrg rsc->levels[0].ts_mode = ts_mode; 1387ec681f3Smrg rsc->levels[0].ts_compress_fmt = ts_compress_fmt; 13901e04c3fSmrg 14001e04c3fSmrg return true; 14101e04c3fSmrg} 14201e04c3fSmrg 1437ec681f3Smrgstatic bool 14401e04c3fSmrgetna_screen_can_create_resource(struct pipe_screen *pscreen, 14501e04c3fSmrg const struct pipe_resource *templat) 14601e04c3fSmrg{ 14701e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 1487ec681f3Smrg if (!translate_samples_to_xyscale(templat->nr_samples, NULL, NULL)) 14901e04c3fSmrg return false; 15001e04c3fSmrg 15101e04c3fSmrg /* templat->bind is not set here, so we must use the minimum sizes */ 15201e04c3fSmrg uint max_size = 15301e04c3fSmrg MIN2(screen->specs.max_rendertarget_size, screen->specs.max_texture_size); 15401e04c3fSmrg 15501e04c3fSmrg if (templat->width0 > max_size || templat->height0 > max_size) 15601e04c3fSmrg return false; 15701e04c3fSmrg 15801e04c3fSmrg return true; 15901e04c3fSmrg} 16001e04c3fSmrg 16101e04c3fSmrgstatic unsigned 16201e04c3fSmrgsetup_miptree(struct etna_resource *rsc, unsigned paddingX, unsigned paddingY, 16301e04c3fSmrg unsigned msaa_xscale, unsigned msaa_yscale) 16401e04c3fSmrg{ 16501e04c3fSmrg struct pipe_resource *prsc = &rsc->base; 16601e04c3fSmrg unsigned level, size = 0; 16701e04c3fSmrg unsigned width = prsc->width0; 16801e04c3fSmrg unsigned height = prsc->height0; 16901e04c3fSmrg unsigned depth = prsc->depth0; 17001e04c3fSmrg 17101e04c3fSmrg for (level = 0; level <= prsc->last_level; level++) { 17201e04c3fSmrg struct etna_resource_level *mip = &rsc->levels[level]; 17301e04c3fSmrg 17401e04c3fSmrg mip->width = width; 17501e04c3fSmrg mip->height = height; 1767ec681f3Smrg mip->depth = depth; 17701e04c3fSmrg mip->padded_width = align(width * msaa_xscale, paddingX); 17801e04c3fSmrg mip->padded_height = align(height * msaa_yscale, paddingY); 17901e04c3fSmrg mip->stride = util_format_get_stride(prsc->format, mip->padded_width); 18001e04c3fSmrg mip->offset = size; 18101e04c3fSmrg mip->layer_stride = mip->stride * util_format_get_nblocksy(prsc->format, mip->padded_height); 18201e04c3fSmrg mip->size = prsc->array_size * mip->layer_stride; 18301e04c3fSmrg 18401e04c3fSmrg /* align levels to 64 bytes to be able to render to them */ 18501e04c3fSmrg size += align(mip->size, ETNA_PE_ALIGNMENT) * depth; 18601e04c3fSmrg 18701e04c3fSmrg width = u_minify(width, 1); 18801e04c3fSmrg height = u_minify(height, 1); 18901e04c3fSmrg depth = u_minify(depth, 1); 19001e04c3fSmrg } 19101e04c3fSmrg 19201e04c3fSmrg return size; 19301e04c3fSmrg} 19401e04c3fSmrg 1959f464c52Smaya/* Is rs alignment needed? */ 1969f464c52Smayastatic bool is_rs_align(struct etna_screen *screen, 1979f464c52Smaya const struct pipe_resource *tmpl) 1989f464c52Smaya{ 1999f464c52Smaya return screen->specs.use_blt ? false : ( 2009f464c52Smaya VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN) || 2019f464c52Smaya !etna_resource_sampler_only(tmpl)); 2029f464c52Smaya} 2039f464c52Smaya 20401e04c3fSmrg/* Create a new resource object, using the given template info */ 20501e04c3fSmrgstruct pipe_resource * 20601e04c3fSmrgetna_resource_alloc(struct pipe_screen *pscreen, unsigned layout, 2077ec681f3Smrg uint64_t modifier, const struct pipe_resource *templat) 20801e04c3fSmrg{ 20901e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 21001e04c3fSmrg struct etna_resource *rsc; 21101e04c3fSmrg unsigned size; 21201e04c3fSmrg 21301e04c3fSmrg DBG_F(ETNA_DBG_RESOURCE_MSGS, 21401e04c3fSmrg "target=%d, format=%s, %ux%ux%u, array_size=%u, " 21501e04c3fSmrg "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x", 21601e04c3fSmrg templat->target, util_format_name(templat->format), templat->width0, 21701e04c3fSmrg templat->height0, templat->depth0, templat->array_size, 21801e04c3fSmrg templat->last_level, templat->nr_samples, templat->usage, 21901e04c3fSmrg templat->bind, templat->flags); 22001e04c3fSmrg 22101e04c3fSmrg /* Determine scaling for antialiasing, allow override using debug flag */ 22201e04c3fSmrg int nr_samples = templat->nr_samples; 22301e04c3fSmrg if ((templat->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) && 22401e04c3fSmrg !(templat->bind & PIPE_BIND_SAMPLER_VIEW)) { 22501e04c3fSmrg if (DBG_ENABLED(ETNA_DBG_MSAA_2X)) 22601e04c3fSmrg nr_samples = 2; 22701e04c3fSmrg if (DBG_ENABLED(ETNA_DBG_MSAA_4X)) 22801e04c3fSmrg nr_samples = 4; 22901e04c3fSmrg } 23001e04c3fSmrg 23101e04c3fSmrg int msaa_xscale = 1, msaa_yscale = 1; 2327ec681f3Smrg if (!translate_samples_to_xyscale(nr_samples, &msaa_xscale, &msaa_yscale)) { 23301e04c3fSmrg /* Number of samples not supported */ 23401e04c3fSmrg return NULL; 23501e04c3fSmrg } 23601e04c3fSmrg 23701e04c3fSmrg /* Determine needed padding (alignment of height/width) */ 23801e04c3fSmrg unsigned paddingX = 0, paddingY = 0; 23901e04c3fSmrg unsigned halign = TEXTURE_HALIGN_FOUR; 24001e04c3fSmrg if (!util_format_is_compressed(templat->format)) { 24101e04c3fSmrg /* If we have the TEXTURE_HALIGN feature, we can always align to the 24201e04c3fSmrg * resolve engine's width. If not, we must not align resources used 24301e04c3fSmrg * only for textures. If this GPU uses the BLT engine, never do RS align. 24401e04c3fSmrg */ 2459f464c52Smaya etna_layout_multiple(layout, screen->specs.pixel_pipes, 2469f464c52Smaya is_rs_align (screen, templat), 2479f464c52Smaya &paddingX, &paddingY, &halign); 24801e04c3fSmrg assert(paddingX && paddingY); 24901e04c3fSmrg } else { 25001e04c3fSmrg /* Compressed textures are padded to their block size, but we don't have 25101e04c3fSmrg * to do anything special for that. */ 25201e04c3fSmrg paddingX = 1; 25301e04c3fSmrg paddingY = 1; 25401e04c3fSmrg } 25501e04c3fSmrg 2567ec681f3Smrg if (!screen->specs.use_blt && templat->target != PIPE_BUFFER && layout == ETNA_LAYOUT_LINEAR) 2577ec681f3Smrg paddingY = align(paddingY, ETNA_RS_HEIGHT_MASK + 1); 25801e04c3fSmrg 25901e04c3fSmrg rsc = CALLOC_STRUCT(etna_resource); 26001e04c3fSmrg if (!rsc) 26101e04c3fSmrg return NULL; 26201e04c3fSmrg 26301e04c3fSmrg rsc->base = *templat; 26401e04c3fSmrg rsc->base.screen = pscreen; 26501e04c3fSmrg rsc->base.nr_samples = nr_samples; 26601e04c3fSmrg rsc->layout = layout; 26701e04c3fSmrg rsc->halign = halign; 2687ec681f3Smrg rsc->explicit_flush = true; 26901e04c3fSmrg 27001e04c3fSmrg pipe_reference_init(&rsc->base.reference, 1); 2717ec681f3Smrg util_range_init(&rsc->valid_buffer_range); 27201e04c3fSmrg 27301e04c3fSmrg size = setup_miptree(rsc, paddingX, paddingY, msaa_xscale, msaa_yscale); 27401e04c3fSmrg 2757ec681f3Smrg if (unlikely(templat->bind & PIPE_BIND_SCANOUT) && screen->ro) { 2767ec681f3Smrg struct pipe_resource scanout_templat = *templat; 2777ec681f3Smrg struct winsys_handle handle; 2787ec681f3Smrg 2797ec681f3Smrg scanout_templat.width0 = align(scanout_templat.width0, paddingX); 2807ec681f3Smrg scanout_templat.height0 = align(scanout_templat.height0, paddingY); 2817ec681f3Smrg 2827ec681f3Smrg rsc->scanout = renderonly_scanout_for_resource(&scanout_templat, 2837ec681f3Smrg screen->ro, &handle); 2847ec681f3Smrg if (!rsc->scanout) { 2857ec681f3Smrg BUG("Problem allocating kms memory for resource"); 2867ec681f3Smrg goto free_rsc; 2877ec681f3Smrg } 28801e04c3fSmrg 2897ec681f3Smrg assert(handle.type == WINSYS_HANDLE_TYPE_FD); 2907ec681f3Smrg rsc->levels[0].stride = handle.stride; 2917ec681f3Smrg rsc->bo = etna_screen_bo_from_handle(pscreen, &handle); 2927ec681f3Smrg close(handle.handle); 2937ec681f3Smrg if (unlikely(!rsc->bo)) 2947ec681f3Smrg goto free_rsc; 2957ec681f3Smrg } else { 2967ec681f3Smrg uint32_t flags = DRM_ETNA_GEM_CACHE_WC; 2977ec681f3Smrg 2987ec681f3Smrg if (templat->bind & PIPE_BIND_VERTEX_BUFFER) 2997ec681f3Smrg flags |= DRM_ETNA_GEM_FORCE_MMU; 3007ec681f3Smrg 3017ec681f3Smrg rsc->bo = etna_bo_new(screen->dev, size, flags); 3027ec681f3Smrg if (unlikely(!rsc->bo)) { 3037ec681f3Smrg BUG("Problem allocating video memory for resource"); 3047ec681f3Smrg goto free_rsc; 3057ec681f3Smrg } 3067ec681f3Smrg } 30701e04c3fSmrg 30801e04c3fSmrg if (DBG_ENABLED(ETNA_DBG_ZERO)) { 3097ec681f3Smrg void *map = etna_bo_map(rsc->bo); 3107ec681f3Smrg etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE); 31101e04c3fSmrg memset(map, 0, size); 3127ec681f3Smrg etna_bo_cpu_fini(rsc->bo); 31301e04c3fSmrg } 31401e04c3fSmrg 3157ec681f3Smrg mtx_init(&rsc->lock, mtx_recursive); 3169f464c52Smaya rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer, 3179f464c52Smaya _mesa_key_pointer_equal); 3189f464c52Smaya if (!rsc->pending_ctx) 3199f464c52Smaya goto free_rsc; 3209f464c52Smaya 32101e04c3fSmrg return &rsc->base; 32201e04c3fSmrg 32301e04c3fSmrgfree_rsc: 32401e04c3fSmrg FREE(rsc); 32501e04c3fSmrg return NULL; 32601e04c3fSmrg} 32701e04c3fSmrg 32801e04c3fSmrgstatic struct pipe_resource * 32901e04c3fSmrgetna_resource_create(struct pipe_screen *pscreen, 33001e04c3fSmrg const struct pipe_resource *templat) 33101e04c3fSmrg{ 33201e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 3337ec681f3Smrg unsigned layout = ETNA_LAYOUT_TILED; 3347ec681f3Smrg 3357ec681f3Smrg /* At this point we don't know if the resource will be used as a texture, 3367ec681f3Smrg * render target, or both, because gallium sets the bits whenever possible 3377ec681f3Smrg * This matters because on some GPUs (GC2000) there is no tiling that is 3387ec681f3Smrg * compatible with both TE and PE. 3397ec681f3Smrg * 3407ec681f3Smrg * We expect that depth/stencil buffers will always be used by PE (rendering), 3417ec681f3Smrg * and any other non-scanout resource will be used as a texture at some point, 3427ec681f3Smrg * So allocate a render-compatible base buffer for scanout/depthstencil buffers, 3437ec681f3Smrg * and a texture-compatible base buffer in other cases 3447ec681f3Smrg * 34501e04c3fSmrg */ 3467ec681f3Smrg if (templat->bind & PIPE_BIND_DEPTH_STENCIL) { 3477ec681f3Smrg if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) 34801e04c3fSmrg layout |= ETNA_LAYOUT_BIT_MULTI; 3497ec681f3Smrg if (screen->specs.can_supertile) 35001e04c3fSmrg layout |= ETNA_LAYOUT_BIT_SUPER; 3517ec681f3Smrg } else if (VIV_FEATURE(screen, chipMinorFeatures2, SUPERTILED_TEXTURE) && 3527ec681f3Smrg etna_resource_hw_tileable(screen->specs.use_blt, templat)) { 3537ec681f3Smrg layout |= ETNA_LAYOUT_BIT_SUPER; 35401e04c3fSmrg } 35501e04c3fSmrg 3567ec681f3Smrg if (/* linear base or scanout without modifier requested */ 3577ec681f3Smrg (templat->bind & (PIPE_BIND_LINEAR | PIPE_BIND_SCANOUT)) || 3587ec681f3Smrg templat->target == PIPE_BUFFER || /* buffer always linear */ 3597ec681f3Smrg /* compressed textures don't use tiling, they have their own "tiles" */ 3607ec681f3Smrg util_format_is_compressed(templat->format)) { 36101e04c3fSmrg layout = ETNA_LAYOUT_LINEAR; 3627ec681f3Smrg } 36301e04c3fSmrg 36401e04c3fSmrg /* modifier is only used for scanout surfaces, so safe to use LINEAR here */ 3657ec681f3Smrg return etna_resource_alloc(pscreen, layout, DRM_FORMAT_MOD_LINEAR, templat); 36601e04c3fSmrg} 36701e04c3fSmrg 36801e04c3fSmrgenum modifier_priority { 36901e04c3fSmrg MODIFIER_PRIORITY_INVALID = 0, 37001e04c3fSmrg MODIFIER_PRIORITY_LINEAR, 37101e04c3fSmrg MODIFIER_PRIORITY_SPLIT_TILED, 37201e04c3fSmrg MODIFIER_PRIORITY_SPLIT_SUPER_TILED, 37301e04c3fSmrg MODIFIER_PRIORITY_TILED, 37401e04c3fSmrg MODIFIER_PRIORITY_SUPER_TILED, 37501e04c3fSmrg}; 37601e04c3fSmrg 3777ec681f3Smrgstatic const uint64_t priority_to_modifier[] = { 37801e04c3fSmrg [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID, 37901e04c3fSmrg [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR, 38001e04c3fSmrg [MODIFIER_PRIORITY_SPLIT_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED, 38101e04c3fSmrg [MODIFIER_PRIORITY_SPLIT_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED, 38201e04c3fSmrg [MODIFIER_PRIORITY_TILED] = DRM_FORMAT_MOD_VIVANTE_TILED, 38301e04c3fSmrg [MODIFIER_PRIORITY_SUPER_TILED] = DRM_FORMAT_MOD_VIVANTE_SUPER_TILED, 38401e04c3fSmrg}; 38501e04c3fSmrg 38601e04c3fSmrgstatic uint64_t 38701e04c3fSmrgselect_best_modifier(const struct etna_screen * screen, 38801e04c3fSmrg const uint64_t *modifiers, const unsigned count) 38901e04c3fSmrg{ 39001e04c3fSmrg enum modifier_priority prio = MODIFIER_PRIORITY_INVALID; 39101e04c3fSmrg 39201e04c3fSmrg for (int i = 0; i < count; i++) { 39301e04c3fSmrg switch (modifiers[i]) { 39401e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED: 39501e04c3fSmrg if ((screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) || 39601e04c3fSmrg !screen->specs.can_supertile) 39701e04c3fSmrg break; 39801e04c3fSmrg prio = MAX2(prio, MODIFIER_PRIORITY_SUPER_TILED); 39901e04c3fSmrg break; 40001e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_TILED: 40101e04c3fSmrg if (screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer) 40201e04c3fSmrg break; 40301e04c3fSmrg prio = MAX2(prio, MODIFIER_PRIORITY_TILED); 40401e04c3fSmrg break; 40501e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED: 40601e04c3fSmrg if ((screen->specs.pixel_pipes < 2) || !screen->specs.can_supertile) 40701e04c3fSmrg break; 40801e04c3fSmrg prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_SUPER_TILED); 40901e04c3fSmrg break; 41001e04c3fSmrg case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED: 41101e04c3fSmrg if (screen->specs.pixel_pipes < 2) 41201e04c3fSmrg break; 41301e04c3fSmrg prio = MAX2(prio, MODIFIER_PRIORITY_SPLIT_TILED); 41401e04c3fSmrg break; 41501e04c3fSmrg case DRM_FORMAT_MOD_LINEAR: 41601e04c3fSmrg prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR); 41701e04c3fSmrg break; 41801e04c3fSmrg case DRM_FORMAT_MOD_INVALID: 41901e04c3fSmrg default: 42001e04c3fSmrg break; 42101e04c3fSmrg } 42201e04c3fSmrg } 42301e04c3fSmrg 42401e04c3fSmrg return priority_to_modifier[prio]; 42501e04c3fSmrg} 42601e04c3fSmrg 42701e04c3fSmrgstatic struct pipe_resource * 42801e04c3fSmrgetna_resource_create_modifiers(struct pipe_screen *pscreen, 42901e04c3fSmrg const struct pipe_resource *templat, 43001e04c3fSmrg const uint64_t *modifiers, int count) 43101e04c3fSmrg{ 43201e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 43301e04c3fSmrg struct pipe_resource tmpl = *templat; 43401e04c3fSmrg uint64_t modifier = select_best_modifier(screen, modifiers, count); 43501e04c3fSmrg 43601e04c3fSmrg if (modifier == DRM_FORMAT_MOD_INVALID) 43701e04c3fSmrg return NULL; 43801e04c3fSmrg 43901e04c3fSmrg /* 44001e04c3fSmrg * We currently assume that all buffers allocated through this interface 44101e04c3fSmrg * should be scanout enabled. 44201e04c3fSmrg */ 44301e04c3fSmrg tmpl.bind |= PIPE_BIND_SCANOUT; 44401e04c3fSmrg 4457ec681f3Smrg return etna_resource_alloc(pscreen, modifier_to_layout(modifier), modifier, &tmpl); 44601e04c3fSmrg} 44701e04c3fSmrg 44801e04c3fSmrgstatic void 44901e04c3fSmrgetna_resource_changed(struct pipe_screen *pscreen, struct pipe_resource *prsc) 45001e04c3fSmrg{ 4517ec681f3Smrg etna_resource(prsc)->seqno++; 45201e04c3fSmrg} 45301e04c3fSmrg 45401e04c3fSmrgstatic void 45501e04c3fSmrgetna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc) 45601e04c3fSmrg{ 45701e04c3fSmrg struct etna_resource *rsc = etna_resource(prsc); 45801e04c3fSmrg 4597ec681f3Smrg mtx_lock(&rsc->lock); 4607ec681f3Smrg assert(!_mesa_set_next_entry(rsc->pending_ctx, NULL)); 4619f464c52Smaya _mesa_set_destroy(rsc->pending_ctx, NULL); 4627ec681f3Smrg mtx_unlock(&rsc->lock); 4639f464c52Smaya 46401e04c3fSmrg if (rsc->bo) 46501e04c3fSmrg etna_bo_del(rsc->bo); 46601e04c3fSmrg 46701e04c3fSmrg if (rsc->ts_bo) 46801e04c3fSmrg etna_bo_del(rsc->ts_bo); 46901e04c3fSmrg 47001e04c3fSmrg if (rsc->scanout) 47101e04c3fSmrg renderonly_scanout_destroy(rsc->scanout, etna_screen(pscreen)->ro); 47201e04c3fSmrg 4737ec681f3Smrg util_range_destroy(&rsc->valid_buffer_range); 4747ec681f3Smrg 47501e04c3fSmrg pipe_resource_reference(&rsc->texture, NULL); 4767ec681f3Smrg pipe_resource_reference(&rsc->render, NULL); 47701e04c3fSmrg 4789f464c52Smaya for (unsigned i = 0; i < ETNA_NUM_LOD; i++) 4799f464c52Smaya FREE(rsc->levels[i].patch_offsets); 4809f464c52Smaya 4817ec681f3Smrg mtx_destroy(&rsc->lock); 4827ec681f3Smrg 48301e04c3fSmrg FREE(rsc); 48401e04c3fSmrg} 48501e04c3fSmrg 48601e04c3fSmrgstatic struct pipe_resource * 48701e04c3fSmrgetna_resource_from_handle(struct pipe_screen *pscreen, 48801e04c3fSmrg const struct pipe_resource *tmpl, 48901e04c3fSmrg struct winsys_handle *handle, unsigned usage) 49001e04c3fSmrg{ 49101e04c3fSmrg struct etna_screen *screen = etna_screen(pscreen); 49201e04c3fSmrg struct etna_resource *rsc; 49301e04c3fSmrg struct etna_resource_level *level; 49401e04c3fSmrg struct pipe_resource *prsc; 49501e04c3fSmrg 49601e04c3fSmrg DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, " 49701e04c3fSmrg "nr_samples=%u, usage=%u, bind=%x, flags=%x", 49801e04c3fSmrg tmpl->target, util_format_name(tmpl->format), tmpl->width0, 49901e04c3fSmrg tmpl->height0, tmpl->depth0, tmpl->array_size, tmpl->last_level, 50001e04c3fSmrg tmpl->nr_samples, tmpl->usage, tmpl->bind, tmpl->flags); 50101e04c3fSmrg 50201e04c3fSmrg rsc = CALLOC_STRUCT(etna_resource); 50301e04c3fSmrg if (!rsc) 50401e04c3fSmrg return NULL; 50501e04c3fSmrg 50601e04c3fSmrg level = &rsc->levels[0]; 50701e04c3fSmrg prsc = &rsc->base; 50801e04c3fSmrg 50901e04c3fSmrg *prsc = *tmpl; 51001e04c3fSmrg 51101e04c3fSmrg pipe_reference_init(&prsc->reference, 1); 5127ec681f3Smrg util_range_init(&rsc->valid_buffer_range); 51301e04c3fSmrg prsc->screen = pscreen; 51401e04c3fSmrg 5157ec681f3Smrg rsc->bo = etna_screen_bo_from_handle(pscreen, handle); 51601e04c3fSmrg if (!rsc->bo) 51701e04c3fSmrg goto fail; 51801e04c3fSmrg 51901e04c3fSmrg rsc->seqno = 1; 52001e04c3fSmrg rsc->layout = modifier_to_layout(handle->modifier); 52101e04c3fSmrg rsc->halign = TEXTURE_HALIGN_FOUR; 52201e04c3fSmrg 5237ec681f3Smrg if (usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) 5247ec681f3Smrg rsc->explicit_flush = true; 52501e04c3fSmrg 52601e04c3fSmrg level->width = tmpl->width0; 52701e04c3fSmrg level->height = tmpl->height0; 5287ec681f3Smrg level->depth = tmpl->depth0; 5297ec681f3Smrg level->stride = handle->stride; 5307ec681f3Smrg level->offset = handle->offset; 53101e04c3fSmrg 53201e04c3fSmrg /* Determine padding of the imported resource. */ 53301e04c3fSmrg unsigned paddingX = 0, paddingY = 0; 53401e04c3fSmrg etna_layout_multiple(rsc->layout, screen->specs.pixel_pipes, 5359f464c52Smaya is_rs_align(screen, tmpl), 53601e04c3fSmrg &paddingX, &paddingY, &rsc->halign); 53701e04c3fSmrg 5387ec681f3Smrg if (!screen->specs.use_blt && rsc->layout == ETNA_LAYOUT_LINEAR) 5397ec681f3Smrg paddingY = align(paddingY, ETNA_RS_HEIGHT_MASK + 1); 54001e04c3fSmrg level->padded_width = align(level->width, paddingX); 54101e04c3fSmrg level->padded_height = align(level->height, paddingY); 54201e04c3fSmrg 54301e04c3fSmrg level->layer_stride = level->stride * util_format_get_nblocksy(prsc->format, 54401e04c3fSmrg level->padded_height); 54501e04c3fSmrg level->size = level->layer_stride; 54601e04c3fSmrg 54701e04c3fSmrg /* The DDX must give us a BO which conforms to our padding size. 54801e04c3fSmrg * The stride of the BO must be greater or equal to our padded 54901e04c3fSmrg * stride. The size of the BO must accomodate the padded height. */ 55001e04c3fSmrg if (level->stride < util_format_get_stride(tmpl->format, level->padded_width)) { 55101e04c3fSmrg BUG("BO stride %u is too small for RS engine width padding (%zu, format %s)", 55201e04c3fSmrg level->stride, util_format_get_stride(tmpl->format, level->padded_width), 55301e04c3fSmrg util_format_name(tmpl->format)); 55401e04c3fSmrg goto fail; 55501e04c3fSmrg } 55601e04c3fSmrg if (etna_bo_size(rsc->bo) < level->stride * level->padded_height) { 55701e04c3fSmrg BUG("BO size %u is too small for RS engine height padding (%u, format %s)", 55801e04c3fSmrg etna_bo_size(rsc->bo), level->stride * level->padded_height, 55901e04c3fSmrg util_format_name(tmpl->format)); 56001e04c3fSmrg goto fail; 56101e04c3fSmrg } 56201e04c3fSmrg 5637ec681f3Smrg mtx_init(&rsc->lock, mtx_recursive); 5649f464c52Smaya rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer, 5659f464c52Smaya _mesa_key_pointer_equal); 5669f464c52Smaya if (!rsc->pending_ctx) 5679f464c52Smaya goto fail; 5689f464c52Smaya 5697ec681f3Smrg if (screen->ro) { 5707ec681f3Smrg struct pipe_resource *imp_prsc = prsc; 5717ec681f3Smrg do { 5727ec681f3Smrg etna_resource(imp_prsc)->scanout = 5737ec681f3Smrg renderonly_create_gpu_import_for_resource(imp_prsc, screen->ro, 5747ec681f3Smrg NULL); 5757ec681f3Smrg /* failure is expected for scanout incompatible buffers */ 5767ec681f3Smrg } while ((imp_prsc = imp_prsc->next)); 57701e04c3fSmrg } 57801e04c3fSmrg 57901e04c3fSmrg return prsc; 58001e04c3fSmrg 58101e04c3fSmrgfail: 58201e04c3fSmrg etna_resource_destroy(pscreen, prsc); 58301e04c3fSmrg 58401e04c3fSmrg return NULL; 58501e04c3fSmrg} 58601e04c3fSmrg 5877ec681f3Smrgstatic bool 58801e04c3fSmrgetna_resource_get_handle(struct pipe_screen *pscreen, 58901e04c3fSmrg struct pipe_context *pctx, 59001e04c3fSmrg struct pipe_resource *prsc, 59101e04c3fSmrg struct winsys_handle *handle, unsigned usage) 59201e04c3fSmrg{ 5937ec681f3Smrg struct etna_screen *screen = etna_screen(pscreen); 59401e04c3fSmrg struct etna_resource *rsc = etna_resource(prsc); 5957ec681f3Smrg struct renderonly_scanout *scanout; 59601e04c3fSmrg 5977ec681f3Smrg if (handle->plane) { 5987ec681f3Smrg struct pipe_resource *cur = prsc; 5997ec681f3Smrg 6007ec681f3Smrg for (int i = 0; i < handle->plane; i++) { 6017ec681f3Smrg cur = cur->next; 6027ec681f3Smrg if (!cur) 6037ec681f3Smrg return false; 6047ec681f3Smrg } 6057ec681f3Smrg rsc = etna_resource(cur); 6067ec681f3Smrg } 6077ec681f3Smrg 6087ec681f3Smrg /* Scanout is always attached to the base resource */ 6097ec681f3Smrg scanout = rsc->scanout; 61001e04c3fSmrg 61101e04c3fSmrg handle->stride = rsc->levels[0].stride; 6129f464c52Smaya handle->offset = rsc->levels[0].offset; 61301e04c3fSmrg handle->modifier = layout_to_modifier(rsc->layout); 61401e04c3fSmrg 6157ec681f3Smrg if (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH)) 6167ec681f3Smrg rsc->explicit_flush = false; 6177ec681f3Smrg 61801e04c3fSmrg if (handle->type == WINSYS_HANDLE_TYPE_SHARED) { 61901e04c3fSmrg return etna_bo_get_name(rsc->bo, &handle->handle) == 0; 62001e04c3fSmrg } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) { 6217ec681f3Smrg if (screen->ro) { 6227ec681f3Smrg return renderonly_get_handle(scanout, handle); 62301e04c3fSmrg } else { 62401e04c3fSmrg handle->handle = etna_bo_handle(rsc->bo); 6257ec681f3Smrg return true; 62601e04c3fSmrg } 62701e04c3fSmrg } else if (handle->type == WINSYS_HANDLE_TYPE_FD) { 62801e04c3fSmrg handle->handle = etna_bo_dmabuf(rsc->bo); 6297ec681f3Smrg return true; 63001e04c3fSmrg } else { 6317ec681f3Smrg return false; 6327ec681f3Smrg } 6337ec681f3Smrg} 6347ec681f3Smrg 6357ec681f3Smrgstatic bool 6367ec681f3Smrgetna_resource_get_param(struct pipe_screen *pscreen, 6377ec681f3Smrg struct pipe_context *pctx, struct pipe_resource *prsc, 6387ec681f3Smrg unsigned plane, unsigned layer, unsigned level, 6397ec681f3Smrg enum pipe_resource_param param, 6407ec681f3Smrg unsigned usage, uint64_t *value) 6417ec681f3Smrg{ 6427ec681f3Smrg if (param == PIPE_RESOURCE_PARAM_NPLANES) { 6437ec681f3Smrg unsigned count = 0; 6447ec681f3Smrg 6457ec681f3Smrg for (struct pipe_resource *cur = prsc; cur; cur = cur->next) 6467ec681f3Smrg count++; 6477ec681f3Smrg *value = count; 6487ec681f3Smrg return true; 6497ec681f3Smrg } 6507ec681f3Smrg 6517ec681f3Smrg struct pipe_resource *cur = prsc; 6527ec681f3Smrg for (int i = 0; i < plane; i++) { 6537ec681f3Smrg cur = cur->next; 6547ec681f3Smrg if (!cur) 6557ec681f3Smrg return false; 6567ec681f3Smrg } 6577ec681f3Smrg struct etna_resource *rsc = etna_resource(cur); 6587ec681f3Smrg 6597ec681f3Smrg switch (param) { 6607ec681f3Smrg case PIPE_RESOURCE_PARAM_STRIDE: 6617ec681f3Smrg *value = rsc->levels[level].stride; 6627ec681f3Smrg return true; 6637ec681f3Smrg case PIPE_RESOURCE_PARAM_OFFSET: 6647ec681f3Smrg *value = rsc->levels[level].offset; 6657ec681f3Smrg return true; 6667ec681f3Smrg case PIPE_RESOURCE_PARAM_MODIFIER: 6677ec681f3Smrg *value = layout_to_modifier(rsc->layout); 6687ec681f3Smrg return true; 6697ec681f3Smrg default: 6707ec681f3Smrg return false; 67101e04c3fSmrg } 67201e04c3fSmrg} 67301e04c3fSmrg 67401e04c3fSmrgvoid 67501e04c3fSmrgetna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc, 67601e04c3fSmrg enum etna_resource_status status) 67701e04c3fSmrg{ 6787ec681f3Smrg struct pipe_resource *referenced = NULL; 67901e04c3fSmrg struct etna_resource *rsc; 68001e04c3fSmrg 68101e04c3fSmrg if (!prsc) 68201e04c3fSmrg return; 68301e04c3fSmrg 6847ec681f3Smrg mtx_lock(&ctx->lock); 6857ec681f3Smrg 68601e04c3fSmrg rsc = etna_resource(prsc); 6877ec681f3Smrgagain: 6887ec681f3Smrg mtx_lock(&rsc->lock); 6897ec681f3Smrg 6907ec681f3Smrg set_foreach(rsc->pending_ctx, entry) { 6917ec681f3Smrg struct etna_context *extctx = (struct etna_context *)entry->key; 6927ec681f3Smrg struct pipe_context *pctx = &extctx->base; 6937ec681f3Smrg bool need_flush = false; 6947ec681f3Smrg 6957ec681f3Smrg if (mtx_trylock(&extctx->lock) != thrd_success) { 6967ec681f3Smrg /* 6977ec681f3Smrg * The other context could be locked in etna_flush() and 6987ec681f3Smrg * stuck waiting for the resource lock, so release the 6997ec681f3Smrg * resource lock here, let etna_flush() finish, and try 7007ec681f3Smrg * again. 7017ec681f3Smrg */ 7027ec681f3Smrg mtx_unlock(&rsc->lock); 7037ec681f3Smrg thrd_yield(); 7047ec681f3Smrg goto again; 7057ec681f3Smrg } 7069f464c52Smaya 7077ec681f3Smrg set_foreach(extctx->used_resources_read, entry2) { 7087ec681f3Smrg struct etna_resource *rsc2 = (struct etna_resource *)entry2->key; 7097ec681f3Smrg if (ctx == extctx || rsc2 != rsc) 7107ec681f3Smrg continue; 7119f464c52Smaya 7127ec681f3Smrg if (status & ETNA_PENDING_WRITE) { 7137ec681f3Smrg need_flush = true; 7147ec681f3Smrg break; 7157ec681f3Smrg } 7167ec681f3Smrg } 7177ec681f3Smrg 7187ec681f3Smrg if (need_flush) { 7197ec681f3Smrg pctx->flush(pctx, NULL, 0); 7207ec681f3Smrg mtx_unlock(&extctx->lock); 7217ec681f3Smrg continue; 7227ec681f3Smrg } 7239f464c52Smaya 7247ec681f3Smrg set_foreach(extctx->used_resources_write, entry2) { 7257ec681f3Smrg struct etna_resource *rsc2 = (struct etna_resource *)entry2->key; 7267ec681f3Smrg if (ctx == extctx || rsc2 != rsc) 7279f464c52Smaya continue; 7289f464c52Smaya 7297ec681f3Smrg need_flush = true; 7307ec681f3Smrg break; 7319f464c52Smaya } 7327ec681f3Smrg 7337ec681f3Smrg if (need_flush) 7347ec681f3Smrg pctx->flush(pctx, NULL, 0); 7357ec681f3Smrg 7367ec681f3Smrg mtx_unlock(&extctx->lock); 7379f464c52Smaya } 7389f464c52Smaya 7397ec681f3Smrg rsc->status = status; 74001e04c3fSmrg 7417ec681f3Smrg if (!_mesa_set_search(rsc->pending_ctx, ctx)) { 7427ec681f3Smrg pipe_resource_reference(&referenced, prsc); 7437ec681f3Smrg _mesa_set_add((status & ETNA_PENDING_READ) ? 7447ec681f3Smrg ctx->used_resources_read : ctx->used_resources_write, rsc); 7457ec681f3Smrg _mesa_set_add(rsc->pending_ctx, ctx); 7467ec681f3Smrg } 7479f464c52Smaya 7487ec681f3Smrg mtx_unlock(&rsc->lock); 7497ec681f3Smrg mtx_unlock(&ctx->lock); 75001e04c3fSmrg} 75101e04c3fSmrg 75201e04c3fSmrgbool 75301e04c3fSmrgetna_resource_has_valid_ts(struct etna_resource *rsc) 75401e04c3fSmrg{ 75501e04c3fSmrg if (!rsc->ts_bo) 75601e04c3fSmrg return false; 75701e04c3fSmrg 75801e04c3fSmrg for (int level = 0; level <= rsc->base.last_level; level++) 75901e04c3fSmrg if (rsc->levels[level].ts_valid) 76001e04c3fSmrg return true; 76101e04c3fSmrg 76201e04c3fSmrg return false; 76301e04c3fSmrg} 76401e04c3fSmrg 76501e04c3fSmrgvoid 76601e04c3fSmrgetna_resource_screen_init(struct pipe_screen *pscreen) 76701e04c3fSmrg{ 76801e04c3fSmrg pscreen->can_create_resource = etna_screen_can_create_resource; 76901e04c3fSmrg pscreen->resource_create = etna_resource_create; 77001e04c3fSmrg pscreen->resource_create_with_modifiers = etna_resource_create_modifiers; 77101e04c3fSmrg pscreen->resource_from_handle = etna_resource_from_handle; 77201e04c3fSmrg pscreen->resource_get_handle = etna_resource_get_handle; 7737ec681f3Smrg pscreen->resource_get_param = etna_resource_get_param; 77401e04c3fSmrg pscreen->resource_changed = etna_resource_changed; 77501e04c3fSmrg pscreen->resource_destroy = etna_resource_destroy; 77601e04c3fSmrg} 777