Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_binder.c
4565 views
/*1* Copyright © 2018 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_binder.c24*25* Shader programs refer to most resources via integer handles. These are26* indexes (BTIs) into a "Binding Table", which is simply a list of pointers27* to SURFACE_STATE entries. Each shader stage has its own binding table,28* set by the 3DSTATE_BINDING_TABLE_POINTERS_* commands. We stream out29* binding tables dynamically, storing them in special BOs we call "binders."30*31* Unfortunately, the hardware designers made 3DSTATE_BINDING_TABLE_POINTERS32* only accept a 16-bit pointer. This means that all binding tables have to33* live within the 64kB range starting at Surface State Base Address. (The34* actual SURFACE_STATE entries can live anywhere in the 4GB zone, as the35* binding table entries are full 32-bit pointers.)36*37* To handle this, we split a 4GB region of VMA into two memory zones.38* IRIS_MEMZONE_BINDER is a small region at the bottom able to hold a few39* binder BOs. IRIS_MEMZONE_SURFACE contains the rest of the 4GB, and is40* always at a higher address than the binders. This allows us to program41* Surface State Base Address to the binder BO's address, and offset the42* values in the binding table to account for the base not starting at the43* beginning of the 4GB region.44*45* This does mean that we have to emit STATE_BASE_ADDRESS and stall when46* we run out of space in the binder, which hopefully won't happen too often.47*/4849#include <stdlib.h>50#include "util/u_math.h"51#include "iris_binder.h"52#include "iris_bufmgr.h"53#include "iris_context.h"5455#define BTP_ALIGNMENT 325657/* Avoid using offset 0, tools consider it NULL */58#define INIT_INSERT_POINT BTP_ALIGNMENT5960static bool61binder_has_space(struct iris_binder *binder, unsigned size)62{63return binder->insert_point + size <= IRIS_BINDER_SIZE;64}6566static void67binder_realloc(struct iris_context *ice)68{69struct iris_screen *screen = (void *) ice->ctx.screen;70struct iris_bufmgr *bufmgr = screen->bufmgr;71struct iris_binder *binder = &ice->state.binder;7273uint64_t next_address = IRIS_MEMZONE_BINDER_START;7475if (binder->bo) {76/* Place the new binder just after the old binder, unless we've hit the77* end of the memory zone...then wrap around to the start again.78*/79next_address = binder->bo->gtt_offset + IRIS_BINDER_SIZE;80if (next_address >= IRIS_MEMZONE_BINDLESS_START)81next_address = IRIS_MEMZONE_BINDER_START;8283iris_bo_unreference(binder->bo);84}858687binder->bo = iris_bo_alloc(bufmgr, "binder", IRIS_BINDER_SIZE, 1,88IRIS_MEMZONE_BINDER, 0);89binder->bo->gtt_offset = next_address;90binder->map = iris_bo_map(NULL, binder->bo, MAP_WRITE);91binder->insert_point = INIT_INSERT_POINT;9293/* Allocating a new binder requires changing Surface State Base Address,94* which also invalidates all our previous binding tables - each entry95* in those tables is an offset from the old base.96*97* We do this here so that iris_binder_reserve_3d correctly gets a new98* larger total_size when making the updated reservation.99*/100ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;101ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;102}103104static uint32_t105binder_insert(struct iris_binder *binder, unsigned size)106{107uint32_t offset = binder->insert_point;108109binder->insert_point = align(binder->insert_point + size, BTP_ALIGNMENT);110111return offset;112}113114/**115* Reserve a block of space in the binder, given the raw size in bytes.116*/117uint32_t118iris_binder_reserve(struct iris_context *ice,119unsigned size)120{121struct iris_binder *binder = &ice->state.binder;122123if (!binder_has_space(binder, size))124binder_realloc(ice);125126assert(size > 0);127return binder_insert(binder, size);128}129130/**131* Reserve and record binder space for 3D pipeline shader stages.132*133* Note that you must actually populate the new binding tables after134* calling this command - the new area is uninitialized.135*/136void137iris_binder_reserve_3d(struct iris_context *ice)138{139struct iris_compiled_shader **shaders = ice->shaders.prog;140struct iris_binder *binder = &ice->state.binder;141unsigned sizes[MESA_SHADER_STAGES] = {};142unsigned total_size;143144/* If nothing is dirty, skip all this. */145if (!(ice->state.dirty & IRIS_DIRTY_RENDER_BUFFER) &&146!(ice->state.stage_dirty & IRIS_ALL_STAGE_DIRTY_BINDINGS_FOR_RENDER))147return;148149/* Get the binding table sizes for each stage */150for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {151if (!shaders[stage])152continue;153154/* Round up the size so our next table has an aligned starting offset */155sizes[stage] = align(shaders[stage]->bt.size_bytes, BTP_ALIGNMENT);156}157158/* Make space for the new binding tables...this may take two tries. */159while (true) {160total_size = 0;161for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {162if (ice->state.stage_dirty & (IRIS_STAGE_DIRTY_BINDINGS_VS << stage))163total_size += sizes[stage];164}165166assert(total_size < IRIS_BINDER_SIZE);167168if (total_size == 0)169return;170171if (binder_has_space(binder, total_size))172break;173174/* It didn't fit. Allocate a new buffer and try again. Note that175* this will flag all bindings dirty, which may increase total_size176* on the next iteration.177*/178binder_realloc(ice);179}180181/* Assign space and record the new binding table offsets. */182uint32_t offset = binder_insert(binder, total_size);183184for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {185if (ice->state.stage_dirty & (IRIS_STAGE_DIRTY_BINDINGS_VS << stage)) {186binder->bt_offset[stage] = sizes[stage] > 0 ? offset : 0;187iris_record_state_size(ice->state.sizes,188binder->bo->gtt_offset + offset, sizes[stage]);189offset += sizes[stage];190}191}192}193194void195iris_binder_reserve_compute(struct iris_context *ice)196{197if (!(ice->state.stage_dirty & IRIS_STAGE_DIRTY_BINDINGS_CS))198return;199200struct iris_binder *binder = &ice->state.binder;201struct iris_compiled_shader *shader =202ice->shaders.prog[MESA_SHADER_COMPUTE];203204unsigned size = shader->bt.size_bytes;205206if (size == 0)207return;208209binder->bt_offset[MESA_SHADER_COMPUTE] = iris_binder_reserve(ice, size);210}211212void213iris_init_binder(struct iris_context *ice)214{215memset(&ice->state.binder, 0, sizeof(struct iris_binder));216binder_realloc(ice);217}218219void220iris_destroy_binder(struct iris_binder *binder)221{222iris_bo_unreference(binder->bo);223}224225226