Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_resource.c
4565 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122/**23* @file iris_resource.c24*25* Resources are images, buffers, and other objects used by the GPU.26*27* XXX: explain resources28*/2930#include <stdio.h>31#include <errno.h>32#include "pipe/p_defines.h"33#include "pipe/p_state.h"34#include "pipe/p_context.h"35#include "pipe/p_screen.h"36#include "util/os_memory.h"37#include "util/u_cpu_detect.h"38#include "util/u_inlines.h"39#include "util/format/u_format.h"40#include "util/u_memory.h"41#include "util/u_threaded_context.h"42#include "util/u_transfer.h"43#include "util/u_transfer_helper.h"44#include "util/u_upload_mgr.h"45#include "util/ralloc.h"46#include "iris_batch.h"47#include "iris_context.h"48#include "iris_resource.h"49#include "iris_screen.h"50#include "intel/common/intel_aux_map.h"51#include "intel/dev/intel_debug.h"52#include "isl/isl.h"53#include "drm-uapi/drm_fourcc.h"54#include "drm-uapi/i915_drm.h"5556enum modifier_priority {57MODIFIER_PRIORITY_INVALID = 0,58MODIFIER_PRIORITY_LINEAR,59MODIFIER_PRIORITY_X,60MODIFIER_PRIORITY_Y,61MODIFIER_PRIORITY_Y_CCS,62MODIFIER_PRIORITY_Y_GFX12_RC_CCS,63MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC,64};6566static const uint64_t priority_to_modifier[] = {67[MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,68[MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,69[MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,70[MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,71[MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,72[MODIFIER_PRIORITY_Y_GFX12_RC_CCS] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,73[MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,74};7576static bool77modifier_is_supported(const struct intel_device_info *devinfo,78enum pipe_format pfmt, unsigned bind,79uint64_t modifier)80{81/* Check for basic device support. */82switch (modifier) {83case DRM_FORMAT_MOD_LINEAR:84case I915_FORMAT_MOD_X_TILED:85break;86case I915_FORMAT_MOD_Y_TILED:87if (devinfo->ver <= 8 && (bind & PIPE_BIND_SCANOUT))88return false;89break;90case I915_FORMAT_MOD_Y_TILED_CCS:91if (devinfo->ver <= 8 || devinfo->ver >= 12)92return false;93break;94case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:95case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:96case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:97if (devinfo->ver != 12)98return false;99break;100case DRM_FORMAT_MOD_INVALID:101default:102return false;103}104105/* Check remaining requirements. */106switch (modifier) {107case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:108if (pfmt != PIPE_FORMAT_BGRA8888_UNORM &&109pfmt != PIPE_FORMAT_RGBA8888_UNORM &&110pfmt != PIPE_FORMAT_BGRX8888_UNORM &&111pfmt != PIPE_FORMAT_RGBX8888_UNORM &&112pfmt != PIPE_FORMAT_NV12 &&113pfmt != PIPE_FORMAT_P010 &&114pfmt != PIPE_FORMAT_P012 &&115pfmt != PIPE_FORMAT_P016 &&116pfmt != PIPE_FORMAT_YUYV &&117pfmt != PIPE_FORMAT_UYVY) {118return false;119}120break;121case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:122case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:123case I915_FORMAT_MOD_Y_TILED_CCS: {124if (INTEL_DEBUG & DEBUG_NO_RBC)125return false;126127enum isl_format rt_format =128iris_format_for_usage(devinfo, pfmt,129ISL_SURF_USAGE_RENDER_TARGET_BIT).fmt;130131if (rt_format == ISL_FORMAT_UNSUPPORTED ||132!isl_format_supports_ccs_e(devinfo, rt_format))133return false;134break;135}136default:137break;138}139140return true;141}142143static uint64_t144select_best_modifier(struct intel_device_info *devinfo,145const struct pipe_resource *templ,146const uint64_t *modifiers,147int count)148{149enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;150151for (int i = 0; i < count; i++) {152if (!modifier_is_supported(devinfo, templ->format, templ->bind,153modifiers[i]))154continue;155156switch (modifiers[i]) {157case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:158prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC);159break;160case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:161prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS);162break;163case I915_FORMAT_MOD_Y_TILED_CCS:164prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);165break;166case I915_FORMAT_MOD_Y_TILED:167prio = MAX2(prio, MODIFIER_PRIORITY_Y);168break;169case I915_FORMAT_MOD_X_TILED:170prio = MAX2(prio, MODIFIER_PRIORITY_X);171break;172case DRM_FORMAT_MOD_LINEAR:173prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);174break;175case DRM_FORMAT_MOD_INVALID:176default:177break;178}179}180181return priority_to_modifier[prio];182}183184static inline bool is_modifier_external_only(enum pipe_format pfmt,185uint64_t modifier)186{187/* Only allow external usage for the following cases: YUV formats188* and the media-compression modifier. The render engine lacks189* support for rendering to a media-compressed surface if the190* compression ratio is large enough. By requiring external usage191* of media-compressed surfaces, resolves are avoided.192*/193return util_format_is_yuv(pfmt) ||194modifier == I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS;195}196197static void198iris_query_dmabuf_modifiers(struct pipe_screen *pscreen,199enum pipe_format pfmt,200int max,201uint64_t *modifiers,202unsigned int *external_only,203int *count)204{205struct iris_screen *screen = (void *) pscreen;206const struct intel_device_info *devinfo = &screen->devinfo;207208uint64_t all_modifiers[] = {209DRM_FORMAT_MOD_LINEAR,210I915_FORMAT_MOD_X_TILED,211I915_FORMAT_MOD_Y_TILED,212I915_FORMAT_MOD_Y_TILED_CCS,213I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,214I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS,215I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,216};217218int supported_mods = 0;219220for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {221if (!modifier_is_supported(devinfo, pfmt, 0, all_modifiers[i]))222continue;223224if (supported_mods < max) {225if (modifiers)226modifiers[supported_mods] = all_modifiers[i];227228if (external_only) {229external_only[supported_mods] =230is_modifier_external_only(pfmt, all_modifiers[i]);231}232}233234supported_mods++;235}236237*count = supported_mods;238}239240static bool241iris_is_dmabuf_modifier_supported(struct pipe_screen *pscreen,242uint64_t modifier, enum pipe_format pfmt,243bool *external_only)244{245struct iris_screen *screen = (void *) pscreen;246const struct intel_device_info *devinfo = &screen->devinfo;247248if (modifier_is_supported(devinfo, pfmt, 0, modifier)) {249if (external_only)250*external_only = is_modifier_external_only(pfmt, modifier);251252return true;253}254255return false;256}257258static unsigned int259iris_get_dmabuf_modifier_planes(struct pipe_screen *pscreen, uint64_t modifier,260enum pipe_format format)261{262unsigned int planes = util_format_get_num_planes(format);263264switch (modifier) {265case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:266return 3;267case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:268case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:269case I915_FORMAT_MOD_Y_TILED_CCS:270return 2 * planes;271default:272return planes;273}274}275276enum isl_format277iris_image_view_get_format(struct iris_context *ice,278const struct pipe_image_view *img)279{280struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;281const struct intel_device_info *devinfo = &screen->devinfo;282283isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;284enum isl_format isl_fmt =285iris_format_for_usage(devinfo, img->format, usage).fmt;286287if (img->shader_access & PIPE_IMAGE_ACCESS_READ) {288/* On Gfx8, try to use typed surfaces reads (which support a289* limited number of formats), and if not possible, fall back290* to untyped reads.291*/292if (devinfo->ver == 8 &&293!isl_has_matching_typed_storage_image_format(devinfo, isl_fmt))294return ISL_FORMAT_RAW;295else296return isl_lower_storage_image_format(devinfo, isl_fmt);297}298299return isl_fmt;300}301302static struct pipe_memory_object *303iris_memobj_create_from_handle(struct pipe_screen *pscreen,304struct winsys_handle *whandle,305bool dedicated)306{307struct iris_screen *screen = (struct iris_screen *)pscreen;308struct iris_memory_object *memobj = CALLOC_STRUCT(iris_memory_object);309struct iris_bo *bo;310311if (!memobj)312return NULL;313314switch (whandle->type) {315case WINSYS_HANDLE_TYPE_SHARED:316bo = iris_bo_gem_create_from_name(screen->bufmgr, "winsys image",317whandle->handle);318break;319case WINSYS_HANDLE_TYPE_FD:320bo = iris_bo_import_dmabuf(screen->bufmgr, whandle->handle);321break;322default:323unreachable("invalid winsys handle type");324}325326if (!bo) {327free(memobj);328return NULL;329}330331memobj->b.dedicated = dedicated;332memobj->bo = bo;333memobj->format = whandle->format;334memobj->stride = whandle->stride;335336return &memobj->b;337}338339static void340iris_memobj_destroy(struct pipe_screen *pscreen,341struct pipe_memory_object *pmemobj)342{343struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;344345iris_bo_unreference(memobj->bo);346free(memobj);347}348349struct pipe_resource *350iris_resource_get_separate_stencil(struct pipe_resource *p_res)351{352/* For packed depth-stencil, we treat depth as the primary resource353* and store S8 as the "second plane" resource.354*/355if (p_res->next && p_res->next->format == PIPE_FORMAT_S8_UINT)356return p_res->next;357358return NULL;359360}361362static void363iris_resource_set_separate_stencil(struct pipe_resource *p_res,364struct pipe_resource *stencil)365{366assert(util_format_has_depth(util_format_description(p_res->format)));367pipe_resource_reference(&p_res->next, stencil);368}369370void371iris_get_depth_stencil_resources(struct pipe_resource *res,372struct iris_resource **out_z,373struct iris_resource **out_s)374{375if (!res) {376*out_z = NULL;377*out_s = NULL;378return;379}380381if (res->format != PIPE_FORMAT_S8_UINT) {382*out_z = (void *) res;383*out_s = (void *) iris_resource_get_separate_stencil(res);384} else {385*out_z = NULL;386*out_s = (void *) res;387}388}389390void391iris_resource_disable_aux(struct iris_resource *res)392{393iris_bo_unreference(res->aux.bo);394iris_bo_unreference(res->aux.clear_color_bo);395free(res->aux.state);396397res->aux.usage = ISL_AUX_USAGE_NONE;398res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;399res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;400res->aux.surf.size_B = 0;401res->aux.bo = NULL;402res->aux.extra_aux.surf.size_B = 0;403res->aux.clear_color_bo = NULL;404res->aux.state = NULL;405}406407static uint32_t408iris_resource_alloc_flags(const struct iris_screen *screen,409const struct pipe_resource *templ)410{411if (templ->flags & IRIS_RESOURCE_FLAG_DEVICE_MEM)412return 0;413414uint32_t flags = 0;415416switch (templ->usage) {417case PIPE_USAGE_STAGING:418flags |= BO_ALLOC_SMEM | BO_ALLOC_COHERENT;419break;420case PIPE_USAGE_STREAM:421flags |= BO_ALLOC_SMEM;422break;423case PIPE_USAGE_DYNAMIC:424case PIPE_USAGE_DEFAULT:425case PIPE_USAGE_IMMUTABLE:426/* Use LMEM for these if possible */427break;428}429430if (templ->flags & (PIPE_RESOURCE_FLAG_MAP_COHERENT |431PIPE_RESOURCE_FLAG_MAP_PERSISTENT))432flags |= BO_ALLOC_SMEM;433434return flags;435}436437static void438iris_resource_destroy(struct pipe_screen *screen,439struct pipe_resource *p_res)440{441struct iris_resource *res = (struct iris_resource *) p_res;442443if (p_res->target == PIPE_BUFFER)444util_range_destroy(&res->valid_buffer_range);445446iris_resource_disable_aux(res);447448threaded_resource_deinit(p_res);449iris_bo_unreference(res->bo);450iris_pscreen_unref(res->orig_screen);451452free(res);453}454455static struct iris_resource *456iris_alloc_resource(struct pipe_screen *pscreen,457const struct pipe_resource *templ)458{459struct iris_resource *res = calloc(1, sizeof(struct iris_resource));460if (!res)461return NULL;462463res->base.b = *templ;464res->base.b.screen = pscreen;465res->orig_screen = iris_pscreen_ref(pscreen);466pipe_reference_init(&res->base.b.reference, 1);467threaded_resource_init(&res->base.b);468469res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;470res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;471472if (templ->target == PIPE_BUFFER)473util_range_init(&res->valid_buffer_range);474475return res;476}477478unsigned479iris_get_num_logical_layers(const struct iris_resource *res, unsigned level)480{481if (res->surf.dim == ISL_SURF_DIM_3D)482return minify(res->surf.logical_level0_px.depth, level);483else484return res->surf.logical_level0_px.array_len;485}486487static enum isl_aux_state **488create_aux_state_map(struct iris_resource *res, enum isl_aux_state initial)489{490assert(res->aux.state == NULL);491492uint32_t total_slices = 0;493for (uint32_t level = 0; level < res->surf.levels; level++)494total_slices += iris_get_num_logical_layers(res, level);495496const size_t per_level_array_size =497res->surf.levels * sizeof(enum isl_aux_state *);498499/* We're going to allocate a single chunk of data for both the per-level500* reference array and the arrays of aux_state. This makes cleanup501* significantly easier.502*/503const size_t total_size =504per_level_array_size + total_slices * sizeof(enum isl_aux_state);505506void *data = malloc(total_size);507if (!data)508return NULL;509510enum isl_aux_state **per_level_arr = data;511enum isl_aux_state *s = data + per_level_array_size;512for (uint32_t level = 0; level < res->surf.levels; level++) {513per_level_arr[level] = s;514const unsigned level_layers = iris_get_num_logical_layers(res, level);515for (uint32_t a = 0; a < level_layers; a++)516*(s++) = initial;517}518assert((void *)s == data + total_size);519520return per_level_arr;521}522523static unsigned524iris_get_aux_clear_color_state_size(struct iris_screen *screen)525{526const struct intel_device_info *devinfo = &screen->devinfo;527return devinfo->ver >= 10 ? screen->isl_dev.ss.clear_color_state_size : 0;528}529530static void531map_aux_addresses(struct iris_screen *screen, struct iris_resource *res,532enum isl_format format, unsigned plane)533{534const struct intel_device_info *devinfo = &screen->devinfo;535if (devinfo->ver >= 12 && isl_aux_usage_has_ccs(res->aux.usage)) {536void *aux_map_ctx = iris_bufmgr_get_aux_map_context(screen->bufmgr);537assert(aux_map_ctx);538const unsigned aux_offset = res->aux.extra_aux.surf.size_B > 0 ?539res->aux.extra_aux.offset : res->aux.offset;540const uint64_t format_bits =541intel_aux_map_format_bits(res->surf.tiling, format, plane);542intel_aux_map_add_mapping(aux_map_ctx, res->bo->gtt_offset + res->offset,543res->aux.bo->gtt_offset + aux_offset,544res->surf.size_B, format_bits);545res->bo->aux_map_address = res->aux.bo->gtt_offset;546}547}548549static bool550want_ccs_e_for_format(const struct intel_device_info *devinfo,551enum isl_format format)552{553if (!isl_format_supports_ccs_e(devinfo, format))554return false;555556const struct isl_format_layout *fmtl = isl_format_get_layout(format);557558/* CCS_E seems to significantly hurt performance with 32-bit floating559* point formats. For example, Paraview's "Wavelet Volume" case uses560* both R32_FLOAT and R32G32B32A32_FLOAT, and enabling CCS_E for those561* formats causes a 62% FPS drop.562*563* However, many benchmarks seem to use 16-bit float with no issues.564*/565if (fmtl->channels.r.bits == 32 && fmtl->channels.r.type == ISL_SFLOAT)566return false;567568return true;569}570571static enum isl_surf_dim572target_to_isl_surf_dim(enum pipe_texture_target target)573{574switch (target) {575case PIPE_BUFFER:576case PIPE_TEXTURE_1D:577case PIPE_TEXTURE_1D_ARRAY:578return ISL_SURF_DIM_1D;579case PIPE_TEXTURE_2D:580case PIPE_TEXTURE_CUBE:581case PIPE_TEXTURE_RECT:582case PIPE_TEXTURE_2D_ARRAY:583case PIPE_TEXTURE_CUBE_ARRAY:584return ISL_SURF_DIM_2D;585case PIPE_TEXTURE_3D:586return ISL_SURF_DIM_3D;587case PIPE_MAX_TEXTURE_TYPES:588break;589}590unreachable("invalid texture type");591}592593static bool594iris_resource_configure_main(const struct iris_screen *screen,595struct iris_resource *res,596const struct pipe_resource *templ,597uint64_t modifier, uint32_t row_pitch_B)598{599res->mod_info = isl_drm_modifier_get_info(modifier);600601if (modifier != DRM_FORMAT_MOD_INVALID && res->mod_info == NULL)602return false;603604isl_tiling_flags_t tiling_flags = 0;605606if (res->mod_info != NULL) {607tiling_flags = 1 << res->mod_info->tiling;608} else if (templ->usage == PIPE_USAGE_STAGING ||609templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR)) {610tiling_flags = ISL_TILING_LINEAR_BIT;611} else if (templ->bind & PIPE_BIND_SCANOUT) {612tiling_flags = screen->devinfo.has_tiling_uapi ?613ISL_TILING_X_BIT : ISL_TILING_LINEAR_BIT;614} else {615tiling_flags = ISL_TILING_ANY_MASK;616}617618isl_surf_usage_flags_t usage = 0;619620if (templ->usage == PIPE_USAGE_STAGING)621usage |= ISL_SURF_USAGE_STAGING_BIT;622623if (templ->bind & PIPE_BIND_RENDER_TARGET)624usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;625626if (templ->bind & PIPE_BIND_SAMPLER_VIEW)627usage |= ISL_SURF_USAGE_TEXTURE_BIT;628629if (templ->bind & PIPE_BIND_SHADER_IMAGE)630usage |= ISL_SURF_USAGE_STORAGE_BIT;631632if (templ->bind & PIPE_BIND_SCANOUT)633usage |= ISL_SURF_USAGE_DISPLAY_BIT;634635if (templ->target == PIPE_TEXTURE_CUBE ||636templ->target == PIPE_TEXTURE_CUBE_ARRAY) {637usage |= ISL_SURF_USAGE_CUBE_BIT;638}639640if (templ->usage != PIPE_USAGE_STAGING &&641util_format_is_depth_or_stencil(templ->format)) {642643/* Should be handled by u_transfer_helper */644assert(!util_format_is_depth_and_stencil(templ->format));645646usage |= templ->format == PIPE_FORMAT_S8_UINT ?647ISL_SURF_USAGE_STENCIL_BIT : ISL_SURF_USAGE_DEPTH_BIT;648}649650const enum isl_format format =651iris_format_for_usage(&screen->devinfo, templ->format, usage).fmt;652653const struct isl_surf_init_info init_info = {654.dim = target_to_isl_surf_dim(templ->target),655.format = format,656.width = templ->width0,657.height = templ->height0,658.depth = templ->depth0,659.levels = templ->last_level + 1,660.array_len = templ->array_size,661.samples = MAX2(templ->nr_samples, 1),662.min_alignment_B = 0,663.row_pitch_B = row_pitch_B,664.usage = usage,665.tiling_flags = tiling_flags666};667668if (!isl_surf_init_s(&screen->isl_dev, &res->surf, &init_info))669return false;670671res->internal_format = templ->format;672673return true;674}675676static bool677iris_get_ccs_surf(const struct isl_device *dev,678const struct isl_surf *surf,679struct isl_surf *aux_surf,680struct isl_surf *extra_aux_surf,681uint32_t row_pitch_B)682{683assert(extra_aux_surf->size_B == 0);684685struct isl_surf *ccs_surf;686const struct isl_surf *hiz_or_mcs_surf;687if (aux_surf->size_B > 0) {688assert(aux_surf->usage & (ISL_SURF_USAGE_HIZ_BIT |689ISL_SURF_USAGE_MCS_BIT));690hiz_or_mcs_surf = aux_surf;691ccs_surf = extra_aux_surf;692} else {693hiz_or_mcs_surf = NULL;694ccs_surf = aux_surf;695}696697return isl_surf_get_ccs_surf(dev, surf, hiz_or_mcs_surf,698ccs_surf, row_pitch_B);699}700701/**702* Configure aux for the resource, but don't allocate it. For images which703* might be shared with modifiers, we must allocate the image and aux data in704* a single bo.705*706* Returns false on unexpected error (e.g. allocation failed, or invalid707* configuration result).708*/709static bool710iris_resource_configure_aux(struct iris_screen *screen,711struct iris_resource *res, bool imported)712{713const struct intel_device_info *devinfo = &screen->devinfo;714715/* Try to create the auxiliary surfaces allowed by the modifier or by716* the user if no modifier is specified.717*/718assert(!res->mod_info ||719res->mod_info->aux_usage == ISL_AUX_USAGE_NONE ||720res->mod_info->aux_usage == ISL_AUX_USAGE_CCS_E ||721res->mod_info->aux_usage == ISL_AUX_USAGE_GFX12_CCS_E ||722res->mod_info->aux_usage == ISL_AUX_USAGE_MC);723724const bool has_mcs = !res->mod_info &&725isl_surf_get_mcs_surf(&screen->isl_dev, &res->surf, &res->aux.surf);726727const bool has_hiz = !res->mod_info && !(INTEL_DEBUG & DEBUG_NO_HIZ) &&728isl_surf_get_hiz_surf(&screen->isl_dev, &res->surf, &res->aux.surf);729730const bool has_ccs =731((!res->mod_info && !(INTEL_DEBUG & DEBUG_NO_RBC)) ||732(res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE)) &&733iris_get_ccs_surf(&screen->isl_dev, &res->surf, &res->aux.surf,734&res->aux.extra_aux.surf, 0);735736/* Having both HIZ and MCS is impossible. */737assert(!has_mcs || !has_hiz);738739if (res->mod_info && has_ccs) {740/* Only allow a CCS modifier if the aux was created successfully. */741res->aux.possible_usages |= 1 << res->mod_info->aux_usage;742} else if (has_mcs) {743res->aux.possible_usages |=7441 << (has_ccs ? ISL_AUX_USAGE_MCS_CCS : ISL_AUX_USAGE_MCS);745} else if (has_hiz) {746if (!has_ccs) {747res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ;748} else if (res->surf.samples == 1 &&749(res->surf.usage & ISL_SURF_USAGE_TEXTURE_BIT)) {750/* If this resource is single-sampled and will be used as a texture,751* put the HiZ surface in write-through mode so that we can sample752* from it.753*/754res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ_CCS_WT;755} else {756res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ_CCS;757}758} else if (has_ccs && isl_surf_usage_is_stencil(res->surf.usage)) {759res->aux.possible_usages |= 1 << ISL_AUX_USAGE_STC_CCS;760} else if (has_ccs) {761if (want_ccs_e_for_format(devinfo, res->surf.format)) {762res->aux.possible_usages |= devinfo->ver < 12 ?7631 << ISL_AUX_USAGE_CCS_E : 1 << ISL_AUX_USAGE_GFX12_CCS_E;764} else if (isl_format_supports_ccs_d(devinfo, res->surf.format)) {765res->aux.possible_usages |= 1 << ISL_AUX_USAGE_CCS_D;766}767}768769res->aux.usage = util_last_bit(res->aux.possible_usages) - 1;770771if (!has_hiz || iris_sample_with_depth_aux(devinfo, res))772res->aux.sampler_usages = res->aux.possible_usages;773774enum isl_aux_state initial_state;775assert(!res->aux.bo);776777switch (res->aux.usage) {778case ISL_AUX_USAGE_NONE:779/* Update relevant fields to indicate that aux is disabled. */780iris_resource_disable_aux(res);781782/* Having no aux buffer is only okay if there's no modifier with aux. */783return !res->mod_info || res->mod_info->aux_usage == ISL_AUX_USAGE_NONE;784case ISL_AUX_USAGE_HIZ:785case ISL_AUX_USAGE_HIZ_CCS:786case ISL_AUX_USAGE_HIZ_CCS_WT:787initial_state = ISL_AUX_STATE_AUX_INVALID;788break;789case ISL_AUX_USAGE_MCS:790case ISL_AUX_USAGE_MCS_CCS:791/* The Ivybridge PRM, Vol 2 Part 1 p326 says:792*793* "When MCS buffer is enabled and bound to MSRT, it is required794* that it is cleared prior to any rendering."795*796* Since we only use the MCS buffer for rendering, we just clear it797* immediately on allocation. The clear value for MCS buffers is all798* 1's, so we simply memset it to 0xff.799*/800initial_state = ISL_AUX_STATE_CLEAR;801break;802case ISL_AUX_USAGE_CCS_D:803case ISL_AUX_USAGE_CCS_E:804case ISL_AUX_USAGE_GFX12_CCS_E:805case ISL_AUX_USAGE_STC_CCS:806case ISL_AUX_USAGE_MC:807/* When CCS_E is used, we need to ensure that the CCS starts off in808* a valid state. From the Sky Lake PRM, "MCS Buffer for Render809* Target(s)":810*811* "If Software wants to enable Color Compression without Fast812* clear, Software needs to initialize MCS with zeros."813*814* A CCS value of 0 indicates that the corresponding block is in the815* pass-through state which is what we want.816*817* For CCS_D, do the same thing. On Gfx9+, this avoids having any818* undefined bits in the aux buffer.819*/820if (imported) {821assert(res->aux.usage != ISL_AUX_USAGE_STC_CCS);822initial_state =823isl_drm_modifier_get_default_aux_state(res->mod_info->modifier);824} else {825initial_state = ISL_AUX_STATE_PASS_THROUGH;826}827break;828default:829unreachable("Unsupported aux mode");830}831832/* Create the aux_state for the auxiliary buffer. */833res->aux.state = create_aux_state_map(res, initial_state);834if (!res->aux.state)835return false;836837return true;838}839840/**841* Initialize the aux buffer contents.842*843* Returns false on unexpected error (e.g. mapping a BO failed).844*/845static bool846iris_resource_init_aux_buf(struct iris_resource *res,847unsigned clear_color_state_size)848{849void *map = iris_bo_map(NULL, res->aux.bo, MAP_WRITE | MAP_RAW);850851if (!map)852return false;853854if (iris_resource_get_aux_state(res, 0, 0) != ISL_AUX_STATE_AUX_INVALID) {855/* See iris_resource_configure_aux for the memset_value rationale. */856uint8_t memset_value = isl_aux_usage_has_mcs(res->aux.usage) ? 0xFF : 0;857memset((char*)map + res->aux.offset, memset_value,858res->aux.surf.size_B);859}860861memset((char*)map + res->aux.extra_aux.offset,8620, res->aux.extra_aux.surf.size_B);863864/* Zero the indirect clear color to match ::fast_clear_color. */865memset((char *)map + res->aux.clear_color_offset, 0,866clear_color_state_size);867868iris_bo_unmap(res->aux.bo);869870if (clear_color_state_size > 0) {871res->aux.clear_color_bo = res->aux.bo;872iris_bo_reference(res->aux.clear_color_bo);873}874875return true;876}877878static void879import_aux_info(struct iris_resource *res,880const struct iris_resource *aux_res)881{882assert(aux_res->aux.surf.row_pitch_B && aux_res->aux.offset);883assert(res->bo == aux_res->aux.bo);884assert(res->aux.surf.row_pitch_B == aux_res->aux.surf.row_pitch_B);885assert(res->bo->size >= aux_res->aux.offset + res->aux.surf.size_B);886887iris_bo_reference(aux_res->aux.bo);888res->aux.bo = aux_res->aux.bo;889res->aux.offset = aux_res->aux.offset;890}891892void893iris_resource_finish_aux_import(struct pipe_screen *pscreen,894struct iris_resource *res)895{896struct iris_screen *screen = (struct iris_screen *)pscreen;897assert(iris_resource_unfinished_aux_import(res));898899/* Create an array of resources. Combining main and aux planes is easier900* with indexing as opposed to scanning the linked list.901*/902struct iris_resource *r[4] = { NULL, };903unsigned num_planes = 0;904unsigned num_main_planes = 0;905for (struct pipe_resource *p_res = &res->base.b; p_res; p_res = p_res->next) {906r[num_planes] = (struct iris_resource *)p_res;907num_main_planes += r[num_planes++]->bo != NULL;908}909910/* Get an ISL format to use with the aux-map. */911enum isl_format format;912switch (res->external_format) {913case PIPE_FORMAT_NV12: format = ISL_FORMAT_PLANAR_420_8; break;914case PIPE_FORMAT_P010: format = ISL_FORMAT_PLANAR_420_10; break;915case PIPE_FORMAT_P012: format = ISL_FORMAT_PLANAR_420_12; break;916case PIPE_FORMAT_P016: format = ISL_FORMAT_PLANAR_420_16; break;917case PIPE_FORMAT_YUYV: format = ISL_FORMAT_YCRCB_NORMAL; break;918case PIPE_FORMAT_UYVY: format = ISL_FORMAT_YCRCB_SWAPY; break;919default: format = res->surf.format; break;920}921922/* Combine main and aux plane information. */923if (num_main_planes == 1 && num_planes == 2) {924import_aux_info(r[0], r[1]);925map_aux_addresses(screen, r[0], format, 0);926927/* Add on a clear color BO. */928if (iris_get_aux_clear_color_state_size(screen) > 0) {929res->aux.clear_color_bo =930iris_bo_alloc(screen->bufmgr, "clear color_buffer",931iris_get_aux_clear_color_state_size(screen), 1,932IRIS_MEMZONE_OTHER, BO_ALLOC_ZEROED);933}934} else if (num_main_planes == 1 && num_planes == 3) {935import_aux_info(r[0], r[1]);936map_aux_addresses(screen, r[0], format, 0);937938/* Import the clear color BO. */939iris_bo_reference(r[2]->aux.clear_color_bo);940r[0]->aux.clear_color_bo = r[2]->aux.clear_color_bo;941r[0]->aux.clear_color_offset = r[2]->aux.clear_color_offset;942memcpy(res->aux.clear_color.f32,943iris_bo_map(NULL, res->aux.clear_color_bo, MAP_READ|MAP_RAW) +944res->aux.clear_color_offset, sizeof(res->aux.clear_color.f32));945} else if (num_main_planes == 2 && num_planes == 4) {946import_aux_info(r[0], r[2]);947import_aux_info(r[1], r[3]);948map_aux_addresses(screen, r[0], format, 0);949map_aux_addresses(screen, r[1], format, 1);950} else {951/* Gallium has lowered a single main plane into two. */952assert(num_main_planes == 2 && num_planes == 3);953assert(isl_format_is_yuv(format) && !isl_format_is_planar(format));954import_aux_info(r[0], r[2]);955import_aux_info(r[1], r[2]);956map_aux_addresses(screen, r[0], format, 0);957}958}959960static struct pipe_resource *961iris_resource_create_for_buffer(struct pipe_screen *pscreen,962const struct pipe_resource *templ)963{964struct iris_screen *screen = (struct iris_screen *)pscreen;965struct iris_resource *res = iris_alloc_resource(pscreen, templ);966967assert(templ->target == PIPE_BUFFER);968assert(templ->height0 <= 1);969assert(templ->depth0 <= 1);970assert(templ->format == PIPE_FORMAT_NONE ||971util_format_get_blocksize(templ->format) == 1);972973res->internal_format = templ->format;974res->surf.tiling = ISL_TILING_LINEAR;975976enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;977const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";978if (templ->flags & IRIS_RESOURCE_FLAG_SHADER_MEMZONE) {979memzone = IRIS_MEMZONE_SHADER;980name = "shader kernels";981} else if (templ->flags & IRIS_RESOURCE_FLAG_SURFACE_MEMZONE) {982memzone = IRIS_MEMZONE_SURFACE;983name = "surface state";984} else if (templ->flags & IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE) {985memzone = IRIS_MEMZONE_DYNAMIC;986name = "dynamic state";987} else if (templ->flags & IRIS_RESOURCE_FLAG_BINDLESS_MEMZONE) {988memzone = IRIS_MEMZONE_BINDLESS;989name = "bindless surface state";990}991992unsigned flags = iris_resource_alloc_flags(screen, templ);993994res->bo =995iris_bo_alloc(screen->bufmgr, name, templ->width0, 1, memzone, flags);996997if (!res->bo) {998iris_resource_destroy(pscreen, &res->base.b);999return NULL;1000}10011002if (templ->bind & PIPE_BIND_SHARED) {1003iris_bo_mark_exported(res->bo);1004res->base.is_shared = true;1005}10061007return &res->base.b;1008}10091010static struct pipe_resource *1011iris_resource_create_with_modifiers(struct pipe_screen *pscreen,1012const struct pipe_resource *templ,1013const uint64_t *modifiers,1014int modifiers_count)1015{1016struct iris_screen *screen = (struct iris_screen *)pscreen;1017struct intel_device_info *devinfo = &screen->devinfo;1018struct iris_resource *res = iris_alloc_resource(pscreen, templ);10191020if (!res)1021return NULL;10221023uint64_t modifier =1024select_best_modifier(devinfo, templ, modifiers, modifiers_count);10251026if (modifier == DRM_FORMAT_MOD_INVALID && modifiers_count > 0) {1027fprintf(stderr, "Unsupported modifier, resource creation failed.\n");1028goto fail;1029}10301031UNUSED const bool isl_surf_created_successfully =1032iris_resource_configure_main(screen, res, templ, modifier, 0);1033assert(isl_surf_created_successfully);10341035const char *name = "miptree";1036enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;10371038unsigned int flags = iris_resource_alloc_flags(screen, templ);10391040/* These are for u_upload_mgr buffers only */1041assert(!(templ->flags & (IRIS_RESOURCE_FLAG_SHADER_MEMZONE |1042IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |1043IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE |1044IRIS_RESOURCE_FLAG_BINDLESS_MEMZONE)));10451046if (!iris_resource_configure_aux(screen, res, false))1047goto fail;10481049/* Modifiers require the aux data to be in the same buffer as the main1050* surface, but we combine them even when a modifier is not being used.1051*/1052uint64_t bo_size = res->surf.size_B;10531054/* Allocate space for the aux buffer. */1055if (res->aux.surf.size_B > 0) {1056res->aux.offset = ALIGN(bo_size, res->aux.surf.alignment_B);1057bo_size = res->aux.offset + res->aux.surf.size_B;1058}10591060/* Allocate space for the extra aux buffer. */1061if (res->aux.extra_aux.surf.size_B > 0) {1062res->aux.extra_aux.offset =1063ALIGN(bo_size, res->aux.extra_aux.surf.alignment_B);1064bo_size = res->aux.extra_aux.offset + res->aux.extra_aux.surf.size_B;1065}10661067/* Allocate space for the indirect clear color.1068*1069* Also add some padding to make sure the fast clear color state buffer1070* starts at a 4K alignment. We believe that 256B might be enough, but due1071* to lack of testing we will leave this as 4K for now.1072*/1073if (res->aux.surf.size_B > 0) {1074res->aux.clear_color_offset = ALIGN(bo_size, 4096);1075bo_size = res->aux.clear_color_offset +1076iris_get_aux_clear_color_state_size(screen);1077}10781079uint32_t alignment = MAX2(4096, res->surf.alignment_B);1080res->bo =1081iris_bo_alloc(screen->bufmgr, name, bo_size, alignment, memzone, flags);10821083if (!res->bo)1084goto fail;10851086if (res->aux.surf.size_B > 0) {1087res->aux.bo = res->bo;1088iris_bo_reference(res->aux.bo);1089unsigned clear_color_state_size =1090iris_get_aux_clear_color_state_size(screen);1091if (!iris_resource_init_aux_buf(res, clear_color_state_size))1092goto fail;1093map_aux_addresses(screen, res, res->surf.format, 0);1094}10951096if (templ->bind & PIPE_BIND_SHARED) {1097iris_bo_mark_exported(res->bo);1098res->base.is_shared = true;1099}11001101return &res->base.b;11021103fail:1104fprintf(stderr, "XXX: resource creation failed\n");1105iris_resource_destroy(pscreen, &res->base.b);1106return NULL;1107}11081109static struct pipe_resource *1110iris_resource_create(struct pipe_screen *pscreen,1111const struct pipe_resource *templ)1112{1113if (templ->target == PIPE_BUFFER)1114return iris_resource_create_for_buffer(pscreen, templ);1115else1116return iris_resource_create_with_modifiers(pscreen, templ, NULL, 0);1117}11181119static uint64_t1120tiling_to_modifier(uint32_t tiling)1121{1122static const uint64_t map[] = {1123[I915_TILING_NONE] = DRM_FORMAT_MOD_LINEAR,1124[I915_TILING_X] = I915_FORMAT_MOD_X_TILED,1125[I915_TILING_Y] = I915_FORMAT_MOD_Y_TILED,1126};11271128assert(tiling < ARRAY_SIZE(map));11291130return map[tiling];1131}11321133static struct pipe_resource *1134iris_resource_from_user_memory(struct pipe_screen *pscreen,1135const struct pipe_resource *templ,1136void *user_memory)1137{1138struct iris_screen *screen = (struct iris_screen *)pscreen;1139struct iris_bufmgr *bufmgr = screen->bufmgr;1140struct iris_resource *res = iris_alloc_resource(pscreen, templ);1141if (!res)1142return NULL;11431144assert(templ->target == PIPE_BUFFER);11451146res->internal_format = templ->format;1147res->base.is_user_ptr = true;1148res->bo = iris_bo_create_userptr(bufmgr, "user",1149user_memory, templ->width0,1150IRIS_MEMZONE_OTHER);1151if (!res->bo) {1152iris_resource_destroy(pscreen, &res->base.b);1153return NULL;1154}11551156util_range_add(&res->base.b, &res->valid_buffer_range, 0, templ->width0);11571158return &res->base.b;1159}11601161static bool1162mod_plane_is_clear_color(uint64_t modifier, uint32_t plane)1163{1164ASSERTED const struct isl_drm_modifier_info *mod_info =1165isl_drm_modifier_get_info(modifier);1166assert(mod_info);11671168switch (modifier) {1169case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:1170assert(mod_info->supports_clear_color);1171return plane == 2;1172default:1173assert(!mod_info->supports_clear_color);1174return false;1175}1176}11771178static struct pipe_resource *1179iris_resource_from_handle(struct pipe_screen *pscreen,1180const struct pipe_resource *templ,1181struct winsys_handle *whandle,1182unsigned usage)1183{1184assert(templ->target != PIPE_BUFFER);11851186struct iris_screen *screen = (struct iris_screen *)pscreen;1187struct iris_bufmgr *bufmgr = screen->bufmgr;1188struct iris_resource *res = iris_alloc_resource(pscreen, templ);1189if (!res)1190return NULL;11911192switch (whandle->type) {1193case WINSYS_HANDLE_TYPE_FD:1194res->bo = iris_bo_import_dmabuf(bufmgr, whandle->handle);1195break;1196case WINSYS_HANDLE_TYPE_SHARED:1197res->bo = iris_bo_gem_create_from_name(bufmgr, "winsys image",1198whandle->handle);1199break;1200default:1201unreachable("invalid winsys handle type");1202}1203if (!res->bo)1204goto fail;12051206res->offset = whandle->offset;1207res->external_format = whandle->format;12081209/* Create a surface for each plane specified by the external format. */1210if (whandle->plane < util_format_get_num_planes(whandle->format)) {1211uint64_t modifier = whandle->modifier;12121213if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {1214/* We don't have a modifier; match whatever GEM_GET_TILING says */1215uint32_t tiling;1216iris_gem_get_tiling(res->bo, &tiling);1217modifier = tiling_to_modifier(tiling);1218}12191220UNUSED const bool isl_surf_created_successfully =1221iris_resource_configure_main(screen, res, templ, modifier,1222whandle->stride);1223assert(isl_surf_created_successfully);12241225UNUSED const bool ok = iris_resource_configure_aux(screen, res, true);1226assert(ok);1227/* The gallium dri layer will create a separate plane resource for the1228* aux image. iris_resource_finish_aux_import will merge the separate aux1229* parameters back into a single iris_resource.1230*/1231} else if (mod_plane_is_clear_color(whandle->modifier, whandle->plane)) {1232res->aux.clear_color_offset = whandle->offset;1233res->aux.clear_color_bo = res->bo;1234res->bo = NULL;1235} else {1236/* Save modifier import information to reconstruct later. After import,1237* this will be available under a second image accessible from the main1238* image with res->base.next. See iris_resource_finish_aux_import.1239*/1240res->aux.surf.row_pitch_B = whandle->stride;1241res->aux.offset = whandle->offset;1242res->aux.bo = res->bo;1243res->bo = NULL;1244}12451246return &res->base.b;12471248fail:1249iris_resource_destroy(pscreen, &res->base.b);1250return NULL;1251}12521253static struct pipe_resource *1254iris_resource_from_memobj(struct pipe_screen *pscreen,1255const struct pipe_resource *templ,1256struct pipe_memory_object *pmemobj,1257uint64_t offset)1258{1259struct iris_screen *screen = (struct iris_screen *)pscreen;1260struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;1261struct iris_resource *res = iris_alloc_resource(pscreen, templ);12621263if (!res)1264return NULL;12651266if (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) {1267UNUSED const bool isl_surf_created_successfully =1268iris_resource_configure_main(screen, res, templ, DRM_FORMAT_MOD_INVALID, 0);1269assert(isl_surf_created_successfully);1270}12711272res->bo = memobj->bo;1273res->offset = offset;1274res->external_format = memobj->format;12751276iris_bo_reference(memobj->bo);12771278return &res->base.b;1279}12801281static void1282iris_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)1283{1284struct iris_context *ice = (struct iris_context *)ctx;1285struct iris_resource *res = (void *) resource;1286const struct isl_drm_modifier_info *mod = res->mod_info;12871288iris_resource_prepare_access(ice, res,12890, INTEL_REMAINING_LEVELS,12900, INTEL_REMAINING_LAYERS,1291mod ? mod->aux_usage : ISL_AUX_USAGE_NONE,1292mod ? mod->supports_clear_color : false);12931294if (!res->mod_info && res->aux.usage != ISL_AUX_USAGE_NONE) {1295/* flush_resource may be used to prepare an image for sharing external1296* to the driver (e.g. via eglCreateImage). To account for this, make1297* sure to get rid of any compression that a consumer wouldn't know how1298* to handle.1299*/1300for (int i = 0; i < IRIS_BATCH_COUNT; i++) {1301if (iris_batch_references(&ice->batches[i], res->bo))1302iris_batch_flush(&ice->batches[i]);1303}13041305iris_resource_disable_aux(res);1306}1307}13081309static void1310iris_resource_disable_aux_on_first_query(struct pipe_resource *resource,1311unsigned usage)1312{1313struct iris_resource *res = (struct iris_resource *)resource;1314bool mod_with_aux =1315res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;13161317/* Disable aux usage if explicit flush not set and this is the first time1318* we are dealing with this resource and the resource was not created with1319* a modifier with aux.1320*/1321if (!mod_with_aux &&1322(!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0) &&1323p_atomic_read(&resource->reference.count) == 1) {1324iris_resource_disable_aux(res);1325}1326}13271328static bool1329iris_resource_get_param(struct pipe_screen *pscreen,1330struct pipe_context *context,1331struct pipe_resource *resource,1332unsigned plane,1333unsigned layer,1334unsigned level,1335enum pipe_resource_param param,1336unsigned handle_usage,1337uint64_t *value)1338{1339struct iris_screen *screen = (struct iris_screen *)pscreen;1340struct iris_resource *res = (struct iris_resource *)resource;1341bool mod_with_aux =1342res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;1343bool wants_aux = mod_with_aux && plane > 0;1344bool result;1345unsigned handle;13461347if (iris_resource_unfinished_aux_import(res))1348iris_resource_finish_aux_import(pscreen, res);13491350struct iris_bo *bo = wants_aux ? res->aux.bo : res->bo;13511352iris_resource_disable_aux_on_first_query(resource, handle_usage);13531354switch (param) {1355case PIPE_RESOURCE_PARAM_NPLANES:1356if (mod_with_aux) {1357*value = iris_get_dmabuf_modifier_planes(pscreen,1358res->mod_info->modifier,1359res->external_format);1360} else {1361unsigned count = 0;1362for (struct pipe_resource *cur = resource; cur; cur = cur->next)1363count++;1364*value = count;1365}1366return true;1367case PIPE_RESOURCE_PARAM_STRIDE:1368*value = wants_aux ? res->aux.surf.row_pitch_B : res->surf.row_pitch_B;1369return true;1370case PIPE_RESOURCE_PARAM_OFFSET:1371*value = wants_aux ?1372mod_plane_is_clear_color(res->mod_info->modifier, plane) ?1373res->aux.clear_color_offset : res->aux.offset : 0;1374return true;1375case PIPE_RESOURCE_PARAM_MODIFIER:1376*value = res->mod_info ? res->mod_info->modifier :1377tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));1378return true;1379case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:1380if (!wants_aux)1381iris_gem_set_tiling(bo, &res->surf);13821383result = iris_bo_flink(bo, &handle) == 0;1384if (result)1385*value = handle;1386return result;1387case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS: {1388if (!wants_aux)1389iris_gem_set_tiling(bo, &res->surf);13901391/* Because we share the same drm file across multiple iris_screen, when1392* we export a GEM handle we must make sure it is valid in the DRM file1393* descriptor the caller is using (this is the FD given at screen1394* creation).1395*/1396uint32_t handle;1397if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))1398return false;1399*value = handle;1400return true;1401}14021403case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:1404if (!wants_aux)1405iris_gem_set_tiling(bo, &res->surf);14061407result = iris_bo_export_dmabuf(bo, (int *) &handle) == 0;1408if (result)1409*value = handle;1410return result;1411default:1412return false;1413}1414}14151416static bool1417iris_resource_get_handle(struct pipe_screen *pscreen,1418struct pipe_context *unused_ctx,1419struct pipe_resource *resource,1420struct winsys_handle *whandle,1421unsigned usage)1422{1423struct iris_screen *screen = (struct iris_screen *) pscreen;1424struct iris_resource *res = (struct iris_resource *)resource;1425bool mod_with_aux =1426res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;14271428/* if ctx is ever used, do ctx = threaded_context_unwrap_sync(ctx) */14291430iris_resource_disable_aux_on_first_query(resource, usage);14311432struct iris_bo *bo;1433if (res->mod_info &&1434mod_plane_is_clear_color(res->mod_info->modifier, whandle->plane)) {1435bo = res->aux.clear_color_bo;1436whandle->offset = res->aux.clear_color_offset;1437} else if (mod_with_aux && whandle->plane > 0) {1438bo = res->aux.bo;1439whandle->stride = res->aux.surf.row_pitch_B;1440whandle->offset = res->aux.offset;1441} else {1442/* If this is a buffer, stride should be 0 - no need to special case */1443whandle->stride = res->surf.row_pitch_B;1444bo = res->bo;1445}14461447whandle->format = res->external_format;1448whandle->modifier =1449res->mod_info ? res->mod_info->modifier1450: tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));14511452#ifndef NDEBUG1453enum isl_aux_usage allowed_usage =1454usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH ? res->aux.usage :1455res->mod_info ? res->mod_info->aux_usage : ISL_AUX_USAGE_NONE;14561457if (res->aux.usage != allowed_usage) {1458enum isl_aux_state aux_state = iris_resource_get_aux_state(res, 0, 0);1459assert(aux_state == ISL_AUX_STATE_RESOLVED ||1460aux_state == ISL_AUX_STATE_PASS_THROUGH);1461}1462#endif14631464switch (whandle->type) {1465case WINSYS_HANDLE_TYPE_SHARED:1466iris_gem_set_tiling(bo, &res->surf);1467return iris_bo_flink(bo, &whandle->handle) == 0;1468case WINSYS_HANDLE_TYPE_KMS: {1469iris_gem_set_tiling(bo, &res->surf);14701471/* Because we share the same drm file across multiple iris_screen, when1472* we export a GEM handle we must make sure it is valid in the DRM file1473* descriptor the caller is using (this is the FD given at screen1474* creation).1475*/1476uint32_t handle;1477if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))1478return false;1479whandle->handle = handle;1480return true;1481}1482case WINSYS_HANDLE_TYPE_FD:1483iris_gem_set_tiling(bo, &res->surf);1484return iris_bo_export_dmabuf(bo, (int *) &whandle->handle) == 0;1485}14861487return false;1488}14891490static bool1491resource_is_busy(struct iris_context *ice,1492struct iris_resource *res)1493{1494bool busy = iris_bo_busy(res->bo);14951496for (int i = 0; i < IRIS_BATCH_COUNT; i++)1497busy |= iris_batch_references(&ice->batches[i], res->bo);14981499return busy;1500}15011502void1503iris_replace_buffer_storage(struct pipe_context *ctx,1504struct pipe_resource *p_dst,1505struct pipe_resource *p_src,1506unsigned num_rebinds,1507uint32_t rebind_mask,1508uint32_t delete_buffer_id)1509{1510struct iris_screen *screen = (void *) ctx->screen;1511struct iris_context *ice = (void *) ctx;1512struct iris_resource *dst = (void *) p_dst;1513struct iris_resource *src = (void *) p_src;15141515assert(memcmp(&dst->surf, &src->surf, sizeof(dst->surf)) == 0);15161517struct iris_bo *old_bo = dst->bo;15181519/* Swap out the backing storage */1520iris_bo_reference(src->bo);1521dst->bo = src->bo;15221523/* Rebind the buffer, replacing any state referring to the old BO's1524* address, and marking state dirty so it's reemitted.1525*/1526screen->vtbl.rebind_buffer(ice, dst);15271528iris_bo_unreference(old_bo);1529}15301531static void1532iris_invalidate_resource(struct pipe_context *ctx,1533struct pipe_resource *resource)1534{1535struct iris_screen *screen = (void *) ctx->screen;1536struct iris_context *ice = (void *) ctx;1537struct iris_resource *res = (void *) resource;15381539if (resource->target != PIPE_BUFFER)1540return;15411542/* If it's already invalidated, don't bother doing anything. */1543if (res->valid_buffer_range.start > res->valid_buffer_range.end)1544return;15451546if (!resource_is_busy(ice, res)) {1547/* The resource is idle, so just mark that it contains no data and1548* keep using the same underlying buffer object.1549*/1550util_range_set_empty(&res->valid_buffer_range);1551return;1552}15531554/* Otherwise, try and replace the backing storage with a new BO. */15551556/* We can't reallocate memory we didn't allocate in the first place. */1557if (res->bo->userptr)1558return;15591560struct iris_bo *old_bo = res->bo;1561struct iris_bo *new_bo =1562iris_bo_alloc(screen->bufmgr, res->bo->name, resource->width0, 1,1563iris_memzone_for_address(old_bo->gtt_offset), 0);1564if (!new_bo)1565return;15661567/* Swap out the backing storage */1568res->bo = new_bo;15691570/* Rebind the buffer, replacing any state referring to the old BO's1571* address, and marking state dirty so it's reemitted.1572*/1573screen->vtbl.rebind_buffer(ice, res);15741575util_range_set_empty(&res->valid_buffer_range);15761577iris_bo_unreference(old_bo);1578}15791580static void1581iris_flush_staging_region(struct pipe_transfer *xfer,1582const struct pipe_box *flush_box)1583{1584if (!(xfer->usage & PIPE_MAP_WRITE))1585return;15861587struct iris_transfer *map = (void *) xfer;15881589struct pipe_box src_box = *flush_box;15901591/* Account for extra alignment padding in staging buffer */1592if (xfer->resource->target == PIPE_BUFFER)1593src_box.x += xfer->box.x % IRIS_MAP_BUFFER_ALIGNMENT;15941595struct pipe_box dst_box = (struct pipe_box) {1596.x = xfer->box.x + flush_box->x,1597.y = xfer->box.y + flush_box->y,1598.z = xfer->box.z + flush_box->z,1599.width = flush_box->width,1600.height = flush_box->height,1601.depth = flush_box->depth,1602};16031604iris_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,1605dst_box.x, dst_box.y, dst_box.z, map->staging, 0,1606&src_box);1607}16081609static void1610iris_unmap_copy_region(struct iris_transfer *map)1611{1612iris_resource_destroy(map->staging->screen, map->staging);16131614map->ptr = NULL;1615}16161617static void1618iris_map_copy_region(struct iris_transfer *map)1619{1620struct pipe_screen *pscreen = &map->batch->screen->base;1621struct pipe_transfer *xfer = &map->base.b;1622struct pipe_box *box = &xfer->box;1623struct iris_resource *res = (void *) xfer->resource;16241625unsigned extra = xfer->resource->target == PIPE_BUFFER ?1626box->x % IRIS_MAP_BUFFER_ALIGNMENT : 0;16271628struct pipe_resource templ = (struct pipe_resource) {1629.usage = PIPE_USAGE_STAGING,1630.width0 = box->width + extra,1631.height0 = box->height,1632.depth0 = 1,1633.nr_samples = xfer->resource->nr_samples,1634.nr_storage_samples = xfer->resource->nr_storage_samples,1635.array_size = box->depth,1636.format = res->internal_format,1637};16381639if (xfer->resource->target == PIPE_BUFFER)1640templ.target = PIPE_BUFFER;1641else if (templ.array_size > 1)1642templ.target = PIPE_TEXTURE_2D_ARRAY;1643else1644templ.target = PIPE_TEXTURE_2D;16451646map->staging = iris_resource_create(pscreen, &templ);1647assert(map->staging);16481649if (templ.target != PIPE_BUFFER) {1650struct isl_surf *surf = &((struct iris_resource *) map->staging)->surf;1651xfer->stride = isl_surf_get_row_pitch_B(surf);1652xfer->layer_stride = isl_surf_get_array_pitch(surf);1653}16541655if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1656iris_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,1657xfer->resource, xfer->level, box);1658/* Ensure writes to the staging BO land before we map it below. */1659iris_emit_pipe_control_flush(map->batch,1660"transfer read: flush before mapping",1661PIPE_CONTROL_RENDER_TARGET_FLUSH |1662PIPE_CONTROL_TILE_CACHE_FLUSH |1663PIPE_CONTROL_CS_STALL);1664}16651666struct iris_bo *staging_bo = iris_resource_bo(map->staging);16671668if (iris_batch_references(map->batch, staging_bo))1669iris_batch_flush(map->batch);16701671map->ptr =1672iris_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;16731674map->unmap = iris_unmap_copy_region;1675}16761677static void1678get_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,1679unsigned *out_x0_el, unsigned *out_y0_el)1680{1681ASSERTED uint32_t z0_el, a0_el;1682if (surf->dim == ISL_SURF_DIM_3D) {1683isl_surf_get_image_offset_el(surf, level, 0, z,1684out_x0_el, out_y0_el, &z0_el, &a0_el);1685} else {1686isl_surf_get_image_offset_el(surf, level, z, 0,1687out_x0_el, out_y0_el, &z0_el, &a0_el);1688}1689assert(z0_el == 0 && a0_el == 0);1690}16911692/**1693* Get pointer offset into stencil buffer.1694*1695* The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we1696* must decode the tile's layout in software.1697*1698* See1699* - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile1700* Format.1701* - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm1702*1703* Even though the returned offset is always positive, the return type is1704* signed due to1705* commit e8b1c6d6f55f5be3bef25084fdd8b6127517e1371706* mesa: Fix return type of _mesa_get_format_bytes() (#37351)1707*/1708static intptr_t1709s8_offset(uint32_t stride, uint32_t x, uint32_t y)1710{1711uint32_t tile_size = 4096;1712uint32_t tile_width = 64;1713uint32_t tile_height = 64;1714uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */17151716uint32_t tile_x = x / tile_width;1717uint32_t tile_y = y / tile_height;17181719/* The byte's address relative to the tile's base addres. */1720uint32_t byte_x = x % tile_width;1721uint32_t byte_y = y % tile_height;17221723uintptr_t u = tile_y * row_size1724+ tile_x * tile_size1725+ 512 * (byte_x / 8)1726+ 64 * (byte_y / 8)1727+ 32 * ((byte_y / 4) % 2)1728+ 16 * ((byte_x / 4) % 2)1729+ 8 * ((byte_y / 2) % 2)1730+ 4 * ((byte_x / 2) % 2)1731+ 2 * (byte_y % 2)1732+ 1 * (byte_x % 2);17331734return u;1735}17361737static void1738iris_unmap_s8(struct iris_transfer *map)1739{1740struct pipe_transfer *xfer = &map->base.b;1741const struct pipe_box *box = &xfer->box;1742struct iris_resource *res = (struct iris_resource *) xfer->resource;1743struct isl_surf *surf = &res->surf;17441745if (xfer->usage & PIPE_MAP_WRITE) {1746uint8_t *untiled_s8_map = map->ptr;1747uint8_t *tiled_s8_map =1748iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);17491750for (int s = 0; s < box->depth; s++) {1751unsigned x0_el, y0_el;1752get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);17531754for (uint32_t y = 0; y < box->height; y++) {1755for (uint32_t x = 0; x < box->width; x++) {1756ptrdiff_t offset = s8_offset(surf->row_pitch_B,1757x0_el + box->x + x,1758y0_el + box->y + y);1759tiled_s8_map[offset] =1760untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];1761}1762}1763}1764}17651766free(map->buffer);1767}17681769static void1770iris_map_s8(struct iris_transfer *map)1771{1772struct pipe_transfer *xfer = &map->base.b;1773const struct pipe_box *box = &xfer->box;1774struct iris_resource *res = (struct iris_resource *) xfer->resource;1775struct isl_surf *surf = &res->surf;17761777xfer->stride = surf->row_pitch_B;1778xfer->layer_stride = xfer->stride * box->height;17791780/* The tiling and detiling functions require that the linear buffer has1781* a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we1782* over-allocate the linear buffer to get the proper alignment.1783*/1784map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);1785assert(map->buffer);17861787/* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no1788* INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless1789* invalidate is set, since we'll be writing the whole rectangle from our1790* temporary buffer back out.1791*/1792if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1793uint8_t *untiled_s8_map = map->ptr;1794uint8_t *tiled_s8_map =1795iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);17961797for (int s = 0; s < box->depth; s++) {1798unsigned x0_el, y0_el;1799get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);18001801for (uint32_t y = 0; y < box->height; y++) {1802for (uint32_t x = 0; x < box->width; x++) {1803ptrdiff_t offset = s8_offset(surf->row_pitch_B,1804x0_el + box->x + x,1805y0_el + box->y + y);1806untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =1807tiled_s8_map[offset];1808}1809}1810}1811}18121813map->unmap = iris_unmap_s8;1814}18151816/* Compute extent parameters for use with tiled_memcpy functions.1817* xs are in units of bytes and ys are in units of strides.1818*/1819static inline void1820tile_extents(const struct isl_surf *surf,1821const struct pipe_box *box,1822unsigned level, int z,1823unsigned *x1_B, unsigned *x2_B,1824unsigned *y1_el, unsigned *y2_el)1825{1826const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);1827const unsigned cpp = fmtl->bpb / 8;18281829assert(box->x % fmtl->bw == 0);1830assert(box->y % fmtl->bh == 0);18311832unsigned x0_el, y0_el;1833get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);18341835*x1_B = (box->x / fmtl->bw + x0_el) * cpp;1836*y1_el = box->y / fmtl->bh + y0_el;1837*x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;1838*y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;1839}18401841static void1842iris_unmap_tiled_memcpy(struct iris_transfer *map)1843{1844struct pipe_transfer *xfer = &map->base.b;1845const struct pipe_box *box = &xfer->box;1846struct iris_resource *res = (struct iris_resource *) xfer->resource;1847struct isl_surf *surf = &res->surf;18481849const bool has_swizzling = false;18501851if (xfer->usage & PIPE_MAP_WRITE) {1852char *dst =1853iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);18541855for (int s = 0; s < box->depth; s++) {1856unsigned x1, x2, y1, y2;1857tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);18581859void *ptr = map->ptr + s * xfer->layer_stride;18601861isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,1862surf->row_pitch_B, xfer->stride,1863has_swizzling, surf->tiling, ISL_MEMCPY);1864}1865}1866os_free_aligned(map->buffer);1867map->buffer = map->ptr = NULL;1868}18691870static void1871iris_map_tiled_memcpy(struct iris_transfer *map)1872{1873struct pipe_transfer *xfer = &map->base.b;1874const struct pipe_box *box = &xfer->box;1875struct iris_resource *res = (struct iris_resource *) xfer->resource;1876struct isl_surf *surf = &res->surf;18771878xfer->stride = ALIGN(surf->row_pitch_B, 16);1879xfer->layer_stride = xfer->stride * box->height;18801881unsigned x1, x2, y1, y2;1882tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);18831884/* The tiling and detiling functions require that the linear buffer has1885* a 16-byte alignment (that is, its `x0` is 16-byte aligned). Here we1886* over-allocate the linear buffer to get the proper alignment.1887*/1888map->buffer =1889os_malloc_aligned(xfer->layer_stride * box->depth, 16);1890assert(map->buffer);1891map->ptr = (char *)map->buffer + (x1 & 0xf);18921893const bool has_swizzling = false;18941895if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {1896char *src =1897iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);18981899for (int s = 0; s < box->depth; s++) {1900unsigned x1, x2, y1, y2;1901tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);19021903/* Use 's' rather than 'box->z' to rebase the first slice to 0. */1904void *ptr = map->ptr + s * xfer->layer_stride;19051906isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,1907surf->row_pitch_B, has_swizzling,1908surf->tiling, ISL_MEMCPY_STREAMING_LOAD);1909}1910}19111912map->unmap = iris_unmap_tiled_memcpy;1913}19141915static void1916iris_map_direct(struct iris_transfer *map)1917{1918struct pipe_transfer *xfer = &map->base.b;1919struct pipe_box *box = &xfer->box;1920struct iris_resource *res = (struct iris_resource *) xfer->resource;19211922void *ptr = iris_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);19231924if (res->base.b.target == PIPE_BUFFER) {1925xfer->stride = 0;1926xfer->layer_stride = 0;19271928map->ptr = ptr + box->x;1929} else {1930struct isl_surf *surf = &res->surf;1931const struct isl_format_layout *fmtl =1932isl_format_get_layout(surf->format);1933const unsigned cpp = fmtl->bpb / 8;1934unsigned x0_el, y0_el;19351936get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);19371938xfer->stride = isl_surf_get_row_pitch_B(surf);1939xfer->layer_stride = isl_surf_get_array_pitch(surf);19401941map->ptr = ptr + (y0_el + box->y) * xfer->stride + (x0_el + box->x) * cpp;1942}1943}19441945static bool1946can_promote_to_async(const struct iris_resource *res,1947const struct pipe_box *box,1948enum pipe_map_flags usage)1949{1950/* If we're writing to a section of the buffer that hasn't even been1951* initialized with useful data, then we can safely promote this write1952* to be unsynchronized. This helps the common pattern of appending data.1953*/1954return res->base.b.target == PIPE_BUFFER && (usage & PIPE_MAP_WRITE) &&1955!(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&1956!util_ranges_intersect(&res->valid_buffer_range, box->x,1957box->x + box->width);1958}19591960static void *1961iris_transfer_map(struct pipe_context *ctx,1962struct pipe_resource *resource,1963unsigned level,1964enum pipe_map_flags usage,1965const struct pipe_box *box,1966struct pipe_transfer **ptransfer)1967{1968struct iris_context *ice = (struct iris_context *)ctx;1969struct iris_resource *res = (struct iris_resource *)resource;1970struct isl_surf *surf = &res->surf;19711972if (iris_resource_unfinished_aux_import(res))1973iris_resource_finish_aux_import(ctx->screen, res);19741975if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {1976/* Replace the backing storage with a fresh buffer for non-async maps */1977if (!(usage & (PIPE_MAP_UNSYNCHRONIZED |1978TC_TRANSFER_MAP_NO_INVALIDATE)))1979iris_invalidate_resource(ctx, resource);19801981/* If we can discard the whole resource, we can discard the range. */1982usage |= PIPE_MAP_DISCARD_RANGE;1983}19841985if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&1986can_promote_to_async(res, box, usage)) {1987usage |= PIPE_MAP_UNSYNCHRONIZED;1988}19891990/* Avoid using GPU copies for persistent/coherent buffers, as the idea1991* there is to access them simultaneously on the CPU & GPU. This also1992* avoids trying to use GPU copies for our u_upload_mgr buffers which1993* contain state we're constructing for a GPU draw call, which would1994* kill us with infinite stack recursion.1995*/1996if (usage & (PIPE_MAP_PERSISTENT | PIPE_MAP_COHERENT))1997usage |= PIPE_MAP_DIRECTLY;19981999/* We cannot provide a direct mapping of tiled resources, and we2000* may not be able to mmap imported BOs since they may come from2001* other devices that I915_GEM_MMAP cannot work with.2002*/2003if ((usage & PIPE_MAP_DIRECTLY) &&2004(surf->tiling != ISL_TILING_LINEAR || res->bo->imported))2005return NULL;20062007bool map_would_stall = false;20082009if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {2010map_would_stall =2011resource_is_busy(ice, res) ||2012iris_has_invalid_primary(res, level, 1, box->z, box->depth);20132014if (map_would_stall && (usage & PIPE_MAP_DONTBLOCK) &&2015(usage & PIPE_MAP_DIRECTLY))2016return NULL;2017}20182019struct iris_transfer *map;20202021if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)2022map = slab_alloc(&ice->transfer_pool_unsync);2023else2024map = slab_alloc(&ice->transfer_pool);20252026if (!map)2027return NULL;20282029struct pipe_transfer *xfer = &map->base.b;20302031memset(map, 0, sizeof(*map));2032map->dbg = &ice->dbg;20332034pipe_resource_reference(&xfer->resource, resource);2035xfer->level = level;2036xfer->usage = usage;2037xfer->box = *box;2038*ptransfer = xfer;20392040map->dest_had_defined_contents =2041util_ranges_intersect(&res->valid_buffer_range, box->x,2042box->x + box->width);20432044if (usage & PIPE_MAP_WRITE)2045util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);20462047if (!res->bo->imported) {2048/* GPU copies are not useful for buffer reads. Instead of stalling to2049* read from the original buffer, we'd simply copy it to a temporary...2050* then stall (a bit longer) to read from that buffer.2051*2052* Images are less clear-cut. Resolves can be destructive, removing2053* some of the underlying compression, so we'd rather blit the data to2054* a linear temporary and map that, to avoid the resolve.2055*/2056if (!(usage & PIPE_MAP_DISCARD_RANGE) &&2057!iris_has_invalid_primary(res, level, 1, box->z, box->depth)) {2058usage |= PIPE_MAP_DIRECTLY;2059}20602061const struct isl_format_layout *fmtl =2062isl_format_get_layout(surf->format);2063if (fmtl->txc == ISL_TXC_ASTC)2064usage |= PIPE_MAP_DIRECTLY;20652066/* We can map directly if it wouldn't stall, there's no compression,2067* and we aren't doing an uncached read.2068*/2069if (!map_would_stall &&2070!isl_aux_usage_has_compression(res->aux.usage) &&2071!((usage & PIPE_MAP_READ) && res->bo->mmap_mode != IRIS_MMAP_WB)) {2072usage |= PIPE_MAP_DIRECTLY;2073}2074}20752076if (!(usage & PIPE_MAP_DIRECTLY)) {2077/* If we need a synchronous mapping and the resource is busy, or needs2078* resolving, we copy to/from a linear temporary buffer using the GPU.2079*/2080map->batch = &ice->batches[IRIS_BATCH_RENDER];2081map->blorp = &ice->blorp;2082iris_map_copy_region(map);2083} else {2084/* Otherwise we're free to map on the CPU. */20852086if (resource->target != PIPE_BUFFER) {2087iris_resource_access_raw(ice, res, level, box->z, box->depth,2088usage & PIPE_MAP_WRITE);2089}20902091if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {2092for (int i = 0; i < IRIS_BATCH_COUNT; i++) {2093if (iris_batch_references(&ice->batches[i], res->bo))2094iris_batch_flush(&ice->batches[i]);2095}2096}20972098if (surf->tiling == ISL_TILING_W) {2099/* TODO: Teach iris_map_tiled_memcpy about W-tiling... */2100iris_map_s8(map);2101} else if (surf->tiling != ISL_TILING_LINEAR) {2102iris_map_tiled_memcpy(map);2103} else {2104iris_map_direct(map);2105}2106}21072108return map->ptr;2109}21102111static void2112iris_transfer_flush_region(struct pipe_context *ctx,2113struct pipe_transfer *xfer,2114const struct pipe_box *box)2115{2116struct iris_context *ice = (struct iris_context *)ctx;2117struct iris_resource *res = (struct iris_resource *) xfer->resource;2118struct iris_transfer *map = (void *) xfer;21192120if (map->staging)2121iris_flush_staging_region(xfer, box);21222123uint32_t history_flush = 0;21242125if (res->base.b.target == PIPE_BUFFER) {2126if (map->staging)2127history_flush |= PIPE_CONTROL_RENDER_TARGET_FLUSH |2128PIPE_CONTROL_TILE_CACHE_FLUSH;21292130if (map->dest_had_defined_contents)2131history_flush |= iris_flush_bits_for_history(ice, res);21322133util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);2134}21352136if (history_flush & ~PIPE_CONTROL_CS_STALL) {2137for (int i = 0; i < IRIS_BATCH_COUNT; i++) {2138struct iris_batch *batch = &ice->batches[i];2139if (batch->contains_draw || batch->cache.render->entries) {2140iris_batch_maybe_flush(batch, 24);2141iris_emit_pipe_control_flush(batch,2142"cache history: transfer flush",2143history_flush);2144}2145}2146}21472148/* Make sure we flag constants dirty even if there's no need to emit2149* any PIPE_CONTROLs to a batch.2150*/2151iris_dirty_for_history(ice, res);2152}21532154static void2155iris_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)2156{2157struct iris_context *ice = (struct iris_context *)ctx;2158struct iris_transfer *map = (void *) xfer;21592160if (!(xfer->usage & (PIPE_MAP_FLUSH_EXPLICIT |2161PIPE_MAP_COHERENT))) {2162struct pipe_box flush_box = {2163.x = 0, .y = 0, .z = 0,2164.width = xfer->box.width,2165.height = xfer->box.height,2166.depth = xfer->box.depth,2167};2168iris_transfer_flush_region(ctx, xfer, &flush_box);2169}21702171if (map->unmap)2172map->unmap(map);21732174pipe_resource_reference(&xfer->resource, NULL);21752176/* transfer_unmap is always called from the driver thread, so we have to2177* use transfer_pool, not transfer_pool_unsync. Freeing an object into a2178* different pool is allowed, however.2179*/2180slab_free(&ice->transfer_pool, map);2181}21822183/**2184* The pipe->texture_subdata() driver hook.2185*2186* Mesa's state tracker takes this path whenever possible, even with2187* PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER set.2188*/2189static void2190iris_texture_subdata(struct pipe_context *ctx,2191struct pipe_resource *resource,2192unsigned level,2193unsigned usage,2194const struct pipe_box *box,2195const void *data,2196unsigned stride,2197unsigned layer_stride)2198{2199struct iris_context *ice = (struct iris_context *)ctx;2200struct iris_resource *res = (struct iris_resource *)resource;2201const struct isl_surf *surf = &res->surf;22022203assert(resource->target != PIPE_BUFFER);22042205if (iris_resource_unfinished_aux_import(res))2206iris_resource_finish_aux_import(ctx->screen, res);22072208/* Just use the transfer-based path for linear buffers - it will already2209* do a direct mapping, or a simple linear staging buffer.2210*2211* Linear staging buffers appear to be better than tiled ones, too, so2212* take that path if we need the GPU to perform color compression, or2213* stall-avoidance blits.2214*/2215if (surf->tiling == ISL_TILING_LINEAR ||2216isl_aux_usage_has_compression(res->aux.usage) ||2217resource_is_busy(ice, res)) {2218return u_default_texture_subdata(ctx, resource, level, usage, box,2219data, stride, layer_stride);2220}22212222/* No state trackers pass any flags other than PIPE_MAP_WRITE */22232224iris_resource_access_raw(ice, res, level, box->z, box->depth, true);22252226for (int i = 0; i < IRIS_BATCH_COUNT; i++) {2227if (iris_batch_references(&ice->batches[i], res->bo))2228iris_batch_flush(&ice->batches[i]);2229}22302231uint8_t *dst = iris_bo_map(&ice->dbg, res->bo, MAP_WRITE | MAP_RAW);22322233for (int s = 0; s < box->depth; s++) {2234const uint8_t *src = data + s * layer_stride;22352236if (surf->tiling == ISL_TILING_W) {2237unsigned x0_el, y0_el;2238get_image_offset_el(surf, level, box->z + s, &x0_el, &y0_el);22392240for (unsigned y = 0; y < box->height; y++) {2241for (unsigned x = 0; x < box->width; x++) {2242ptrdiff_t offset = s8_offset(surf->row_pitch_B,2243x0_el + box->x + x,2244y0_el + box->y + y);2245dst[offset] = src[y * stride + x];2246}2247}2248} else {2249unsigned x1, x2, y1, y2;22502251tile_extents(surf, box, level, s, &x1, &x2, &y1, &y2);22522253isl_memcpy_linear_to_tiled(x1, x2, y1, y2,2254(void *)dst, (void *)src,2255surf->row_pitch_B, stride,2256false, surf->tiling, ISL_MEMCPY);2257}2258}2259}22602261/**2262* Mark state dirty that needs to be re-emitted when a resource is written.2263*/2264void2265iris_dirty_for_history(struct iris_context *ice,2266struct iris_resource *res)2267{2268uint64_t stage_dirty = 0ull;22692270if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {2271stage_dirty |= ((uint64_t)res->bind_stages)2272<< IRIS_SHIFT_FOR_STAGE_DIRTY_CONSTANTS;2273}22742275ice->state.stage_dirty |= stage_dirty;2276}22772278/**2279* Produce a set of PIPE_CONTROL bits which ensure data written to a2280* resource becomes visible, and any stale read cache data is invalidated.2281*/2282uint32_t2283iris_flush_bits_for_history(struct iris_context *ice,2284struct iris_resource *res)2285{2286struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;22872288uint32_t flush = PIPE_CONTROL_CS_STALL;22892290if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {2291flush |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;2292flush |= screen->compiler->indirect_ubos_use_sampler ?2293PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE :2294PIPE_CONTROL_DATA_CACHE_FLUSH;2295}22962297if (res->bind_history & PIPE_BIND_SAMPLER_VIEW)2298flush |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;22992300if (res->bind_history & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))2301flush |= PIPE_CONTROL_VF_CACHE_INVALIDATE;23022303if (res->bind_history & (PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE))2304flush |= PIPE_CONTROL_DATA_CACHE_FLUSH;23052306return flush;2307}23082309void2310iris_flush_and_dirty_for_history(struct iris_context *ice,2311struct iris_batch *batch,2312struct iris_resource *res,2313uint32_t extra_flags,2314const char *reason)2315{2316if (res->base.b.target != PIPE_BUFFER)2317return;23182319uint32_t flush = iris_flush_bits_for_history(ice, res) | extra_flags;23202321iris_emit_pipe_control_flush(batch, reason, flush);23222323iris_dirty_for_history(ice, res);2324}23252326bool2327iris_resource_set_clear_color(struct iris_context *ice,2328struct iris_resource *res,2329union isl_color_value color)2330{2331if (memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {2332res->aux.clear_color = color;2333return true;2334}23352336return false;2337}23382339union isl_color_value2340iris_resource_get_clear_color(const struct iris_resource *res,2341struct iris_bo **clear_color_bo,2342uint64_t *clear_color_offset)2343{2344assert(res->aux.bo);23452346if (clear_color_bo)2347*clear_color_bo = res->aux.clear_color_bo;2348if (clear_color_offset)2349*clear_color_offset = res->aux.clear_color_offset;2350return res->aux.clear_color;2351}23522353static enum pipe_format2354iris_resource_get_internal_format(struct pipe_resource *p_res)2355{2356struct iris_resource *res = (void *) p_res;2357return res->internal_format;2358}23592360static const struct u_transfer_vtbl transfer_vtbl = {2361.resource_create = iris_resource_create,2362.resource_destroy = iris_resource_destroy,2363.transfer_map = iris_transfer_map,2364.transfer_unmap = iris_transfer_unmap,2365.transfer_flush_region = iris_transfer_flush_region,2366.get_internal_format = iris_resource_get_internal_format,2367.set_stencil = iris_resource_set_separate_stencil,2368.get_stencil = iris_resource_get_separate_stencil,2369};23702371void2372iris_init_screen_resource_functions(struct pipe_screen *pscreen)2373{2374pscreen->query_dmabuf_modifiers = iris_query_dmabuf_modifiers;2375pscreen->is_dmabuf_modifier_supported = iris_is_dmabuf_modifier_supported;2376pscreen->get_dmabuf_modifier_planes = iris_get_dmabuf_modifier_planes;2377pscreen->resource_create_with_modifiers =2378iris_resource_create_with_modifiers;2379pscreen->resource_create = u_transfer_helper_resource_create;2380pscreen->resource_from_user_memory = iris_resource_from_user_memory;2381pscreen->resource_from_handle = iris_resource_from_handle;2382pscreen->resource_from_memobj = iris_resource_from_memobj;2383pscreen->resource_get_handle = iris_resource_get_handle;2384pscreen->resource_get_param = iris_resource_get_param;2385pscreen->resource_destroy = u_transfer_helper_resource_destroy;2386pscreen->memobj_create_from_handle = iris_memobj_create_from_handle;2387pscreen->memobj_destroy = iris_memobj_destroy;2388pscreen->transfer_helper =2389u_transfer_helper_create(&transfer_vtbl, true, true, false, true);2390}23912392void2393iris_init_resource_functions(struct pipe_context *ctx)2394{2395ctx->flush_resource = iris_flush_resource;2396ctx->invalidate_resource = iris_invalidate_resource;2397ctx->buffer_map = u_transfer_helper_transfer_map;2398ctx->texture_map = u_transfer_helper_transfer_map;2399ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;2400ctx->buffer_unmap = u_transfer_helper_transfer_unmap;2401ctx->texture_unmap = u_transfer_helper_transfer_unmap;2402ctx->buffer_subdata = u_default_buffer_subdata;2403ctx->texture_subdata = iris_texture_subdata;2404}240524062407