Path: blob/21.2-virgl/src/intel/common/intel_urb_config.c
4547 views
/*1* Copyright (c) 2011 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 (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <stdlib.h>24#include <math.h>2526#include "util/macros.h"27#include "main/macros.h"28#include "compiler/shader_enums.h"2930#include "intel_l3_config.h"3132/**33* The following diagram shows how we partition the URB:34*35* 16kb or 32kb Rest of the URB space36* __________-__________ _________________-_________________37* / \ / \38* +-------------------------------------------------------------+39* | VS/HS/DS/GS/FS Push | VS/HS/DS/GS URB |40* | Constants | Entries |41* +-------------------------------------------------------------+42*43* Push constants must be stored at the beginning of the URB space,44* while URB entries can be stored anywhere. We choose to lay them45* out in pipeline order (VS -> HS -> DS -> GS).46*/4748/**49* Decide how to partition the URB among the various stages.50*51* \param[in] push_constant_bytes - space allocate for push constants.52* \param[in] urb_size_bytes - total size of the URB (from L3 config).53* \param[in] tess_present - are tessellation shaders active?54* \param[in] gs_present - are geometry shaders active?55* \param[in] entry_size - the URB entry size (from the shader compiler)56* \param[out] entries - the number of URB entries for each stage57* \param[out] start - the starting offset for each stage58* \param[out] deref_block_size - deref block size for 3DSTATE_SF59* \param[out] constrained - true if we wanted more space than we had60*/61void62intel_get_urb_config(const struct intel_device_info *devinfo,63const struct intel_l3_config *l3_cfg,64bool tess_present, bool gs_present,65const unsigned entry_size[4],66unsigned entries[4], unsigned start[4],67enum intel_urb_deref_block_size *deref_block_size,68bool *constrained)69{70unsigned urb_size_kB = intel_get_l3_config_urb_size(devinfo, l3_cfg);7172/* RCU_MODE register for Gfx12+ in BSpec says:73*74* "HW reserves 4KB of URB space per bank for Compute Engine out of the75* total storage available in L3. SW must consider that 4KB of storage76* per bank will be reduced from what is programmed for the URB space77* in L3 for Render Engine executed workloads.78*79* Example: When URB space programmed is 64KB (per bank) for Render80* Engine, the actual URB space available for operation is only 60KB81* (per bank). Similarly when URB space programmed is 128KB (per bank)82* for render engine, the actual URB space available for operation is83* only 124KB (per bank). More detailed descripton available in "L384* Cache" section of the B-Spec."85*/86if (devinfo->ver >= 12)87urb_size_kB -= 4 * devinfo->l3_banks;8889const unsigned push_constant_kB =90(devinfo->ver >= 8 || (devinfo->is_haswell && devinfo->gt == 3)) ? 32 : 16;9192const bool active[4] = { true, tess_present, tess_present, gs_present };9394/* URB allocations must be done in 8k chunks. */95const unsigned chunk_size_kB = 8;96const unsigned chunk_size_bytes = chunk_size_kB * 1024;9798const unsigned push_constant_chunks = push_constant_kB / chunk_size_kB;99const unsigned urb_chunks = urb_size_kB / chunk_size_kB;100101/* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):102*103* VS Number of URB Entries must be divisible by 8 if the VS URB Entry104* Allocation Size is less than 9 512-bit URB entries.105*106* Similar text exists for HS, DS and GS.107*/108unsigned granularity[4];109for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {110granularity[i] = (entry_size[i] < 9) ? 8 : 1;111}112113unsigned min_entries[4] = {114/* VS has a lower limit on the number of URB entries.115*116* From the Broadwell PRM, 3DSTATE_URB_VS instruction:117* "When tessellation is enabled, the VS Number of URB Entries must be118* greater than or equal to 192."119*/120[MESA_SHADER_VERTEX] = tess_present && devinfo->ver == 8 ?121192 : devinfo->urb.min_entries[MESA_SHADER_VERTEX],122123/* There are two constraints on the minimum amount of URB space we can124* allocate:125*126* (1) We need room for at least 2 URB entries, since we always operate127* the GS in DUAL_OBJECT mode.128*129* (2) We can't allocate less than nr_gs_entries_granularity.130*/131[MESA_SHADER_GEOMETRY] = gs_present ? 2 : 0,132133[MESA_SHADER_TESS_CTRL] = tess_present ? 1 : 0,134135[MESA_SHADER_TESS_EVAL] = tess_present ?136devinfo->urb.min_entries[MESA_SHADER_TESS_EVAL] : 0,137};138139/* Min VS Entries isn't a multiple of 8 on Cherryview/Broxton; round up.140* Round them all up.141*/142for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {143min_entries[i] = ALIGN(min_entries[i], granularity[i]);144}145146unsigned entry_size_bytes[4];147for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {148entry_size_bytes[i] = 64 * entry_size[i];149}150151/* Initially, assign each stage the minimum amount of URB space it needs,152* and make a note of how much additional space it "wants" (the amount of153* additional space it could actually make use of).154*/155unsigned chunks[4];156unsigned wants[4];157unsigned total_needs = push_constant_chunks;158unsigned total_wants = 0;159160for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {161if (active[i]) {162chunks[i] = DIV_ROUND_UP(min_entries[i] * entry_size_bytes[i],163chunk_size_bytes);164165wants[i] =166DIV_ROUND_UP(devinfo->urb.max_entries[i] * entry_size_bytes[i],167chunk_size_bytes) - chunks[i];168} else {169chunks[i] = 0;170wants[i] = 0;171}172173total_needs += chunks[i];174total_wants += wants[i];175}176177assert(total_needs <= urb_chunks);178179*constrained = total_needs + total_wants > urb_chunks;180181/* Mete out remaining space (if any) in proportion to "wants". */182unsigned remaining_space = MIN2(urb_chunks - total_needs, total_wants);183184if (remaining_space > 0) {185for (int i = MESA_SHADER_VERTEX;186total_wants > 0 && i <= MESA_SHADER_TESS_EVAL; i++) {187unsigned additional = (unsigned)188roundf(wants[i] * (((float) remaining_space) / total_wants));189chunks[i] += additional;190remaining_space -= additional;191total_wants -= wants[i];192}193194chunks[MESA_SHADER_GEOMETRY] += remaining_space;195}196197/* Sanity check that we haven't over-allocated. */198unsigned total_chunks = push_constant_chunks;199for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {200total_chunks += chunks[i];201}202assert(total_chunks <= urb_chunks);203204/* Finally, compute the number of entries that can fit in the space205* allocated to each stage.206*/207for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {208entries[i] = chunks[i] * chunk_size_bytes / entry_size_bytes[i];209210/* Since we rounded up when computing wants[], this may be slightly211* more than the maximum allowed amount, so correct for that.212*/213entries[i] = MIN2(entries[i], devinfo->urb.max_entries[i]);214215/* Ensure that we program a multiple of the granularity. */216entries[i] = ROUND_DOWN_TO(entries[i], granularity[i]);217218/* Finally, sanity check to make sure we have at least the minimum219* number of entries needed for each stage.220*/221assert(entries[i] >= min_entries[i]);222}223224/* Lay out the URB in pipeline order: push constants, VS, HS, DS, GS. */225int next = push_constant_chunks;226for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {227if (entries[i]) {228start[i] = next;229next += chunks[i];230} else {231/* Just put disabled stages at the beginning. */232start[i] = 0;233}234}235236if (deref_block_size) {237if (devinfo->ver >= 12) {238/* From the Gfx12 BSpec:239*240* "Deref Block size depends on the last enabled shader and number241* of handles programmed for that shader242*243* 1) For GS last shader enabled cases, the deref block is244* always set to a per poly(within hardware)245*246* If the last enabled shader is VS or DS.247*248* 1) If DS is last enabled shader then if the number of DS249* handles is less than 324, need to set per poly deref.250*251* 2) If VS is last enabled shader then if the number of VS252* handles is less than 192, need to set per poly deref"253*254* The default is 32 so we assume that's the right choice if we're255* not in one of the explicit cases listed above.256*/257if (gs_present) {258*deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;259} else if (tess_present) {260if (entries[MESA_SHADER_TESS_EVAL] < 324)261*deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;262else263*deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;264} else {265if (entries[MESA_SHADER_VERTEX] < 192)266*deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;267else268*deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;269}270} else {271*deref_block_size = 0;272}273}274}275276277