17ec681f3Smrg/*
27ec681f3Smrg * Copyright (c) 2011 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include <stdlib.h>
257ec681f3Smrg#include <math.h>
267ec681f3Smrg
277ec681f3Smrg#include "util/macros.h"
287ec681f3Smrg#include "main/macros.h"
297ec681f3Smrg#include "compiler/shader_enums.h"
307ec681f3Smrg
317ec681f3Smrg#include "intel_l3_config.h"
327ec681f3Smrg
337ec681f3Smrg/**
347ec681f3Smrg * The following diagram shows how we partition the URB:
357ec681f3Smrg *
367ec681f3Smrg *        16kb or 32kb               Rest of the URB space
377ec681f3Smrg *   __________-__________   _________________-_________________
387ec681f3Smrg *  /                     \ /                                   \
397ec681f3Smrg * +-------------------------------------------------------------+
407ec681f3Smrg * |  VS/HS/DS/GS/FS Push  |           VS/HS/DS/GS URB           |
417ec681f3Smrg * |       Constants       |               Entries               |
427ec681f3Smrg * +-------------------------------------------------------------+
437ec681f3Smrg *
447ec681f3Smrg * Push constants must be stored at the beginning of the URB space,
457ec681f3Smrg * while URB entries can be stored anywhere.  We choose to lay them
467ec681f3Smrg * out in pipeline order (VS -> HS -> DS -> GS).
477ec681f3Smrg */
487ec681f3Smrg
497ec681f3Smrg/**
507ec681f3Smrg * Decide how to partition the URB among the various stages.
517ec681f3Smrg *
527ec681f3Smrg * \param[in] push_constant_bytes - space allocate for push constants.
537ec681f3Smrg * \param[in] urb_size_bytes - total size of the URB (from L3 config).
547ec681f3Smrg * \param[in] tess_present - are tessellation shaders active?
557ec681f3Smrg * \param[in] gs_present - are geometry shaders active?
567ec681f3Smrg * \param[in] entry_size - the URB entry size (from the shader compiler)
577ec681f3Smrg * \param[out] entries - the number of URB entries for each stage
587ec681f3Smrg * \param[out] start - the starting offset for each stage
597ec681f3Smrg * \param[out] deref_block_size - deref block size for 3DSTATE_SF
607ec681f3Smrg * \param[out] constrained - true if we wanted more space than we had
617ec681f3Smrg */
627ec681f3Smrgvoid
637ec681f3Smrgintel_get_urb_config(const struct intel_device_info *devinfo,
647ec681f3Smrg                     const struct intel_l3_config *l3_cfg,
657ec681f3Smrg                     bool tess_present, bool gs_present,
667ec681f3Smrg                     const unsigned entry_size[4],
677ec681f3Smrg                     unsigned entries[4], unsigned start[4],
687ec681f3Smrg                     enum intel_urb_deref_block_size *deref_block_size,
697ec681f3Smrg                     bool *constrained)
707ec681f3Smrg{
717ec681f3Smrg   unsigned urb_size_kB = intel_get_l3_config_urb_size(devinfo, l3_cfg);
727ec681f3Smrg
737ec681f3Smrg   /* RCU_MODE register for Gfx12+ in BSpec says:
747ec681f3Smrg    *
757ec681f3Smrg    *    "HW reserves 4KB of URB space per bank for Compute Engine out of the
767ec681f3Smrg    *    total storage available in L3. SW must consider that 4KB of storage
777ec681f3Smrg    *    per bank will be reduced from what is programmed for the URB space
787ec681f3Smrg    *    in L3 for Render Engine executed workloads.
797ec681f3Smrg    *
807ec681f3Smrg    *    Example: When URB space programmed is 64KB (per bank) for Render
817ec681f3Smrg    *    Engine, the actual URB space available for operation is only 60KB
827ec681f3Smrg    *    (per bank). Similarly when URB space programmed is 128KB (per bank)
837ec681f3Smrg    *    for render engine, the actual URB space available for operation is
847ec681f3Smrg    *    only 124KB (per bank). More detailed descripton available in "L3
857ec681f3Smrg    *    Cache" section of the B-Spec."
867ec681f3Smrg    */
877ec681f3Smrg   if (devinfo->ver >= 12)
887ec681f3Smrg      urb_size_kB -= 4 * devinfo->l3_banks;
897ec681f3Smrg
907ec681f3Smrg   const unsigned push_constant_kB = devinfo->max_constant_urb_size_kb;
917ec681f3Smrg
927ec681f3Smrg   const bool active[4] = { true, tess_present, tess_present, gs_present };
937ec681f3Smrg
947ec681f3Smrg   /* URB allocations must be done in 8k chunks. */
957ec681f3Smrg   const unsigned chunk_size_kB = 8;
967ec681f3Smrg   const unsigned chunk_size_bytes = chunk_size_kB * 1024;
977ec681f3Smrg
987ec681f3Smrg   const unsigned push_constant_chunks = push_constant_kB / chunk_size_kB;
997ec681f3Smrg   const unsigned urb_chunks = urb_size_kB / chunk_size_kB;
1007ec681f3Smrg
1017ec681f3Smrg   /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
1027ec681f3Smrg    *
1037ec681f3Smrg    *     VS Number of URB Entries must be divisible by 8 if the VS URB Entry
1047ec681f3Smrg    *     Allocation Size is less than 9 512-bit URB entries.
1057ec681f3Smrg    *
1067ec681f3Smrg    * Similar text exists for HS, DS and GS.
1077ec681f3Smrg    */
1087ec681f3Smrg   unsigned granularity[4];
1097ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1107ec681f3Smrg      granularity[i] = (entry_size[i] < 9) ? 8 : 1;
1117ec681f3Smrg   }
1127ec681f3Smrg
1137ec681f3Smrg   unsigned min_entries[4] = {
1147ec681f3Smrg      /* VS has a lower limit on the number of URB entries.
1157ec681f3Smrg       *
1167ec681f3Smrg       * From the Broadwell PRM, 3DSTATE_URB_VS instruction:
1177ec681f3Smrg       * "When tessellation is enabled, the VS Number of URB Entries must be
1187ec681f3Smrg       *  greater than or equal to 192."
1197ec681f3Smrg       */
1207ec681f3Smrg      [MESA_SHADER_VERTEX] = tess_present && devinfo->ver == 8 ?
1217ec681f3Smrg         192 : devinfo->urb.min_entries[MESA_SHADER_VERTEX],
1227ec681f3Smrg
1237ec681f3Smrg      /* There are two constraints on the minimum amount of URB space we can
1247ec681f3Smrg       * allocate:
1257ec681f3Smrg       *
1267ec681f3Smrg       * (1) We need room for at least 2 URB entries, since we always operate
1277ec681f3Smrg       * the GS in DUAL_OBJECT mode.
1287ec681f3Smrg       *
1297ec681f3Smrg       * (2) We can't allocate less than nr_gs_entries_granularity.
1307ec681f3Smrg       */
1317ec681f3Smrg      [MESA_SHADER_GEOMETRY] = gs_present ? 2 : 0,
1327ec681f3Smrg
1337ec681f3Smrg      [MESA_SHADER_TESS_CTRL] = tess_present ? 1 : 0,
1347ec681f3Smrg
1357ec681f3Smrg      [MESA_SHADER_TESS_EVAL] = tess_present ?
1367ec681f3Smrg         devinfo->urb.min_entries[MESA_SHADER_TESS_EVAL] : 0,
1377ec681f3Smrg   };
1387ec681f3Smrg
1397ec681f3Smrg   /* Min VS Entries isn't a multiple of 8 on Cherryview/Broxton; round up.
1407ec681f3Smrg    * Round them all up.
1417ec681f3Smrg    */
1427ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1437ec681f3Smrg      min_entries[i] = ALIGN(min_entries[i], granularity[i]);
1447ec681f3Smrg   }
1457ec681f3Smrg
1467ec681f3Smrg   unsigned entry_size_bytes[4];
1477ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1487ec681f3Smrg      entry_size_bytes[i] = 64 * entry_size[i];
1497ec681f3Smrg   }
1507ec681f3Smrg
1517ec681f3Smrg   /* Initially, assign each stage the minimum amount of URB space it needs,
1527ec681f3Smrg    * and make a note of how much additional space it "wants" (the amount of
1537ec681f3Smrg    * additional space it could actually make use of).
1547ec681f3Smrg    */
1557ec681f3Smrg   unsigned chunks[4];
1567ec681f3Smrg   unsigned wants[4];
1577ec681f3Smrg   unsigned total_needs = push_constant_chunks;
1587ec681f3Smrg   unsigned total_wants = 0;
1597ec681f3Smrg
1607ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1617ec681f3Smrg      if (active[i]) {
1627ec681f3Smrg         chunks[i] = DIV_ROUND_UP(min_entries[i] * entry_size_bytes[i],
1637ec681f3Smrg                                  chunk_size_bytes);
1647ec681f3Smrg
1657ec681f3Smrg         wants[i] =
1667ec681f3Smrg            DIV_ROUND_UP(devinfo->urb.max_entries[i] * entry_size_bytes[i],
1677ec681f3Smrg                         chunk_size_bytes) - chunks[i];
1687ec681f3Smrg      } else {
1697ec681f3Smrg         chunks[i] = 0;
1707ec681f3Smrg         wants[i] = 0;
1717ec681f3Smrg      }
1727ec681f3Smrg
1737ec681f3Smrg      total_needs += chunks[i];
1747ec681f3Smrg      total_wants += wants[i];
1757ec681f3Smrg   }
1767ec681f3Smrg
1777ec681f3Smrg   assert(total_needs <= urb_chunks);
1787ec681f3Smrg
1797ec681f3Smrg   *constrained = total_needs + total_wants > urb_chunks;
1807ec681f3Smrg
1817ec681f3Smrg   /* Mete out remaining space (if any) in proportion to "wants". */
1827ec681f3Smrg   unsigned remaining_space = MIN2(urb_chunks - total_needs, total_wants);
1837ec681f3Smrg
1847ec681f3Smrg   if (remaining_space > 0) {
1857ec681f3Smrg      for (int i = MESA_SHADER_VERTEX;
1867ec681f3Smrg           total_wants > 0 && i <= MESA_SHADER_TESS_EVAL; i++) {
1877ec681f3Smrg         unsigned additional = (unsigned)
1887ec681f3Smrg            roundf(wants[i] * (((float) remaining_space) / total_wants));
1897ec681f3Smrg         chunks[i] += additional;
1907ec681f3Smrg         remaining_space -= additional;
1917ec681f3Smrg         total_wants -= wants[i];
1927ec681f3Smrg      }
1937ec681f3Smrg
1947ec681f3Smrg      chunks[MESA_SHADER_GEOMETRY] += remaining_space;
1957ec681f3Smrg   }
1967ec681f3Smrg
1977ec681f3Smrg   /* Sanity check that we haven't over-allocated. */
1987ec681f3Smrg   unsigned total_chunks = push_constant_chunks;
1997ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2007ec681f3Smrg      total_chunks += chunks[i];
2017ec681f3Smrg   }
2027ec681f3Smrg   assert(total_chunks <= urb_chunks);
2037ec681f3Smrg
2047ec681f3Smrg   /* Finally, compute the number of entries that can fit in the space
2057ec681f3Smrg    * allocated to each stage.
2067ec681f3Smrg    */
2077ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2087ec681f3Smrg      entries[i] = chunks[i] * chunk_size_bytes / entry_size_bytes[i];
2097ec681f3Smrg
2107ec681f3Smrg      /* Since we rounded up when computing wants[], this may be slightly
2117ec681f3Smrg       * more than the maximum allowed amount, so correct for that.
2127ec681f3Smrg       */
2137ec681f3Smrg      entries[i] = MIN2(entries[i], devinfo->urb.max_entries[i]);
2147ec681f3Smrg
2157ec681f3Smrg      /* Ensure that we program a multiple of the granularity. */
2167ec681f3Smrg      entries[i] = ROUND_DOWN_TO(entries[i], granularity[i]);
2177ec681f3Smrg
2187ec681f3Smrg      /* Finally, sanity check to make sure we have at least the minimum
2197ec681f3Smrg       * number of entries needed for each stage.
2207ec681f3Smrg       */
2217ec681f3Smrg      assert(entries[i] >= min_entries[i]);
2227ec681f3Smrg   }
2237ec681f3Smrg
2247ec681f3Smrg   /* Lay out the URB in pipeline order: push constants, VS, HS, DS, GS. */
2257ec681f3Smrg   int next = push_constant_chunks;
2267ec681f3Smrg   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2277ec681f3Smrg      if (entries[i]) {
2287ec681f3Smrg         start[i] = next;
2297ec681f3Smrg         next += chunks[i];
2307ec681f3Smrg      } else {
2317ec681f3Smrg         /* Just put disabled stages at the beginning. */
2327ec681f3Smrg         start[i] = 0;
2337ec681f3Smrg      }
2347ec681f3Smrg   }
2357ec681f3Smrg
2367ec681f3Smrg   if (deref_block_size) {
2377ec681f3Smrg      if (devinfo->ver >= 12) {
2387ec681f3Smrg         /* From the Gfx12 BSpec:
2397ec681f3Smrg          *
2407ec681f3Smrg          *    "Deref Block size depends on the last enabled shader and number
2417ec681f3Smrg          *    of handles programmed for that shader
2427ec681f3Smrg          *
2437ec681f3Smrg          *       1) For GS last shader enabled cases, the deref block is
2447ec681f3Smrg          *          always set to a per poly(within hardware)
2457ec681f3Smrg          *
2467ec681f3Smrg          *    If the last enabled shader is VS or DS.
2477ec681f3Smrg          *
2487ec681f3Smrg          *       1) If DS is last enabled shader then if the number of DS
2497ec681f3Smrg          *          handles is less than 324, need to set per poly deref.
2507ec681f3Smrg          *
2517ec681f3Smrg          *       2) If VS is last enabled shader then if the number of VS
2527ec681f3Smrg          *          handles is less than 192, need to set per poly deref"
2537ec681f3Smrg          *
2547ec681f3Smrg          * The default is 32 so we assume that's the right choice if we're
2557ec681f3Smrg          * not in one of the explicit cases listed above.
2567ec681f3Smrg          */
2577ec681f3Smrg         if (gs_present) {
2587ec681f3Smrg            *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
2597ec681f3Smrg         } else if (tess_present) {
2607ec681f3Smrg            if (entries[MESA_SHADER_TESS_EVAL] < 324)
2617ec681f3Smrg               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
2627ec681f3Smrg            else
2637ec681f3Smrg               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;
2647ec681f3Smrg         } else {
2657ec681f3Smrg            if (entries[MESA_SHADER_VERTEX] < 192)
2667ec681f3Smrg               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
2677ec681f3Smrg            else
2687ec681f3Smrg               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;
2697ec681f3Smrg         }
2707ec681f3Smrg      } else {
2717ec681f3Smrg         *deref_block_size = 0;
2727ec681f3Smrg      }
2737ec681f3Smrg   }
2747ec681f3Smrg}
275