1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2011 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdlib.h>
25bf215546Sopenharmony_ci#include <math.h>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/debug.h"
28bf215546Sopenharmony_ci#include "util/macros.h"
29bf215546Sopenharmony_ci#include "util/u_math.h"
30bf215546Sopenharmony_ci#include "compiler/shader_enums.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "intel_l3_config.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/**
35bf215546Sopenharmony_ci * The following diagram shows how we partition the URB:
36bf215546Sopenharmony_ci *
37bf215546Sopenharmony_ci *        16kb or 32kb               Rest of the URB space
38bf215546Sopenharmony_ci *   __________-__________   _________________-_________________
39bf215546Sopenharmony_ci *  /                     \ /                                   \
40bf215546Sopenharmony_ci * +-------------------------------------------------------------+
41bf215546Sopenharmony_ci * |  VS/HS/DS/GS/FS Push  |           VS/HS/DS/GS URB           |
42bf215546Sopenharmony_ci * |       Constants       |               Entries               |
43bf215546Sopenharmony_ci * +-------------------------------------------------------------+
44bf215546Sopenharmony_ci *
45bf215546Sopenharmony_ci * Push constants must be stored at the beginning of the URB space,
46bf215546Sopenharmony_ci * while URB entries can be stored anywhere.  We choose to lay them
47bf215546Sopenharmony_ci * out in pipeline order (VS -> HS -> DS -> GS).
48bf215546Sopenharmony_ci */
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci/**
51bf215546Sopenharmony_ci * Decide how to partition the URB among the various stages.
52bf215546Sopenharmony_ci *
53bf215546Sopenharmony_ci * \param[in] push_constant_bytes - space allocate for push constants.
54bf215546Sopenharmony_ci * \param[in] urb_size_bytes - total size of the URB (from L3 config).
55bf215546Sopenharmony_ci * \param[in] tess_present - are tessellation shaders active?
56bf215546Sopenharmony_ci * \param[in] gs_present - are geometry shaders active?
57bf215546Sopenharmony_ci * \param[in] entry_size - the URB entry size (from the shader compiler)
58bf215546Sopenharmony_ci * \param[out] entries - the number of URB entries for each stage
59bf215546Sopenharmony_ci * \param[out] start - the starting offset for each stage
60bf215546Sopenharmony_ci * \param[out] deref_block_size - deref block size for 3DSTATE_SF
61bf215546Sopenharmony_ci * \param[out] constrained - true if we wanted more space than we had
62bf215546Sopenharmony_ci */
63bf215546Sopenharmony_civoid
64bf215546Sopenharmony_ciintel_get_urb_config(const struct intel_device_info *devinfo,
65bf215546Sopenharmony_ci                     const struct intel_l3_config *l3_cfg,
66bf215546Sopenharmony_ci                     bool tess_present, bool gs_present,
67bf215546Sopenharmony_ci                     const unsigned entry_size[4],
68bf215546Sopenharmony_ci                     unsigned entries[4], unsigned start[4],
69bf215546Sopenharmony_ci                     enum intel_urb_deref_block_size *deref_block_size,
70bf215546Sopenharmony_ci                     bool *constrained)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   unsigned urb_size_kB = intel_get_l3_config_urb_size(devinfo, l3_cfg);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* RCU_MODE register for Gfx12LP in BSpec says:
75bf215546Sopenharmony_ci    *
76bf215546Sopenharmony_ci    *    "HW reserves 4KB of URB space per bank for Compute Engine out of the
77bf215546Sopenharmony_ci    *    total storage available in L3. SW must consider that 4KB of storage
78bf215546Sopenharmony_ci    *    per bank will be reduced from what is programmed for the URB space
79bf215546Sopenharmony_ci    *    in L3 for Render Engine executed workloads.
80bf215546Sopenharmony_ci    *
81bf215546Sopenharmony_ci    *    Example: When URB space programmed is 64KB (per bank) for Render
82bf215546Sopenharmony_ci    *    Engine, the actual URB space available for operation is only 60KB
83bf215546Sopenharmony_ci    *    (per bank). Similarly when URB space programmed is 128KB (per bank)
84bf215546Sopenharmony_ci    *    for render engine, the actual URB space available for operation is
85bf215546Sopenharmony_ci    *    only 124KB (per bank). More detailed description available in "L3
86bf215546Sopenharmony_ci    *    Cache" section of the B-Spec."
87bf215546Sopenharmony_ci    */
88bf215546Sopenharmony_ci   if (devinfo->verx10 == 120) {
89bf215546Sopenharmony_ci      assert(devinfo->num_slices == 1);
90bf215546Sopenharmony_ci      urb_size_kB -= 4 * devinfo->l3_banks;
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   const unsigned push_constant_kB = devinfo->max_constant_urb_size_kb;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   const bool active[4] = { true, tess_present, tess_present, gs_present };
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   /* URB allocations must be done in 8k chunks. */
98bf215546Sopenharmony_ci   const unsigned chunk_size_kB = 8;
99bf215546Sopenharmony_ci   const unsigned chunk_size_bytes = chunk_size_kB * 1024;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   const unsigned push_constant_chunks = push_constant_kB / chunk_size_kB;
102bf215546Sopenharmony_ci   const unsigned urb_chunks = urb_size_kB / chunk_size_kB;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
105bf215546Sopenharmony_ci    *
106bf215546Sopenharmony_ci    *     VS Number of URB Entries must be divisible by 8 if the VS URB Entry
107bf215546Sopenharmony_ci    *     Allocation Size is less than 9 512-bit URB entries.
108bf215546Sopenharmony_ci    *
109bf215546Sopenharmony_ci    * Similar text exists for HS, DS and GS.
110bf215546Sopenharmony_ci    */
111bf215546Sopenharmony_ci   unsigned granularity[4];
112bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
113bf215546Sopenharmony_ci      granularity[i] = (entry_size[i] < 9) ? 8 : 1;
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   unsigned min_entries[4] = {
117bf215546Sopenharmony_ci      /* VS has a lower limit on the number of URB entries.
118bf215546Sopenharmony_ci       *
119bf215546Sopenharmony_ci       * From the Broadwell PRM, 3DSTATE_URB_VS instruction:
120bf215546Sopenharmony_ci       * "When tessellation is enabled, the VS Number of URB Entries must be
121bf215546Sopenharmony_ci       *  greater than or equal to 192."
122bf215546Sopenharmony_ci       */
123bf215546Sopenharmony_ci      [MESA_SHADER_VERTEX] = tess_present && devinfo->ver == 8 ?
124bf215546Sopenharmony_ci         192 : devinfo->urb.min_entries[MESA_SHADER_VERTEX],
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      /* There are two constraints on the minimum amount of URB space we can
127bf215546Sopenharmony_ci       * allocate:
128bf215546Sopenharmony_ci       *
129bf215546Sopenharmony_ci       * (1) We need room for at least 2 URB entries, since we always operate
130bf215546Sopenharmony_ci       * the GS in DUAL_OBJECT mode.
131bf215546Sopenharmony_ci       *
132bf215546Sopenharmony_ci       * (2) We can't allocate less than nr_gs_entries_granularity.
133bf215546Sopenharmony_ci       */
134bf215546Sopenharmony_ci      [MESA_SHADER_GEOMETRY] = gs_present ? 2 : 0,
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci      [MESA_SHADER_TESS_CTRL] = tess_present ? 1 : 0,
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci      [MESA_SHADER_TESS_EVAL] = tess_present ?
139bf215546Sopenharmony_ci         devinfo->urb.min_entries[MESA_SHADER_TESS_EVAL] : 0,
140bf215546Sopenharmony_ci   };
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   /* Min VS Entries isn't a multiple of 8 on Cherryview/Broxton; round up.
143bf215546Sopenharmony_ci    * Round them all up.
144bf215546Sopenharmony_ci    */
145bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
146bf215546Sopenharmony_ci      min_entries[i] = ALIGN(min_entries[i], granularity[i]);
147bf215546Sopenharmony_ci   }
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   unsigned entry_size_bytes[4];
150bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
151bf215546Sopenharmony_ci      entry_size_bytes[i] = 64 * entry_size[i];
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   /* Initially, assign each stage the minimum amount of URB space it needs,
155bf215546Sopenharmony_ci    * and make a note of how much additional space it "wants" (the amount of
156bf215546Sopenharmony_ci    * additional space it could actually make use of).
157bf215546Sopenharmony_ci    */
158bf215546Sopenharmony_ci   unsigned chunks[4];
159bf215546Sopenharmony_ci   unsigned wants[4];
160bf215546Sopenharmony_ci   unsigned total_needs = push_constant_chunks;
161bf215546Sopenharmony_ci   unsigned total_wants = 0;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
164bf215546Sopenharmony_ci      if (active[i]) {
165bf215546Sopenharmony_ci         chunks[i] = DIV_ROUND_UP(min_entries[i] * entry_size_bytes[i],
166bf215546Sopenharmony_ci                                  chunk_size_bytes);
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci         wants[i] =
169bf215546Sopenharmony_ci            DIV_ROUND_UP(devinfo->urb.max_entries[i] * entry_size_bytes[i],
170bf215546Sopenharmony_ci                         chunk_size_bytes) - chunks[i];
171bf215546Sopenharmony_ci      } else {
172bf215546Sopenharmony_ci         chunks[i] = 0;
173bf215546Sopenharmony_ci         wants[i] = 0;
174bf215546Sopenharmony_ci      }
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci      total_needs += chunks[i];
177bf215546Sopenharmony_ci      total_wants += wants[i];
178bf215546Sopenharmony_ci   }
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   assert(total_needs <= urb_chunks);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   *constrained = total_needs + total_wants > urb_chunks;
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   /* Mete out remaining space (if any) in proportion to "wants". */
185bf215546Sopenharmony_ci   unsigned remaining_space = MIN2(urb_chunks - total_needs, total_wants);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   if (remaining_space > 0) {
188bf215546Sopenharmony_ci      for (int i = MESA_SHADER_VERTEX;
189bf215546Sopenharmony_ci           total_wants > 0 && i <= MESA_SHADER_TESS_EVAL; i++) {
190bf215546Sopenharmony_ci         unsigned additional = (unsigned)
191bf215546Sopenharmony_ci            roundf(wants[i] * (((float) remaining_space) / total_wants));
192bf215546Sopenharmony_ci         chunks[i] += additional;
193bf215546Sopenharmony_ci         remaining_space -= additional;
194bf215546Sopenharmony_ci         total_wants -= wants[i];
195bf215546Sopenharmony_ci      }
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci      chunks[MESA_SHADER_GEOMETRY] += remaining_space;
198bf215546Sopenharmony_ci   }
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   /* Sanity check that we haven't over-allocated. */
201bf215546Sopenharmony_ci   unsigned total_chunks = push_constant_chunks;
202bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
203bf215546Sopenharmony_ci      total_chunks += chunks[i];
204bf215546Sopenharmony_ci   }
205bf215546Sopenharmony_ci   assert(total_chunks <= urb_chunks);
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   /* Finally, compute the number of entries that can fit in the space
208bf215546Sopenharmony_ci    * allocated to each stage.
209bf215546Sopenharmony_ci    */
210bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
211bf215546Sopenharmony_ci      entries[i] = chunks[i] * chunk_size_bytes / entry_size_bytes[i];
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci      /* Since we rounded up when computing wants[], this may be slightly
214bf215546Sopenharmony_ci       * more than the maximum allowed amount, so correct for that.
215bf215546Sopenharmony_ci       */
216bf215546Sopenharmony_ci      entries[i] = MIN2(entries[i], devinfo->urb.max_entries[i]);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci      /* Ensure that we program a multiple of the granularity. */
219bf215546Sopenharmony_ci      entries[i] = ROUND_DOWN_TO(entries[i], granularity[i]);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci      /* Finally, sanity check to make sure we have at least the minimum
222bf215546Sopenharmony_ci       * number of entries needed for each stage.
223bf215546Sopenharmony_ci       */
224bf215546Sopenharmony_ci      assert(entries[i] >= min_entries[i]);
225bf215546Sopenharmony_ci   }
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   /* Lay out the URB in pipeline order: push constants, VS, HS, DS, GS. */
228bf215546Sopenharmony_ci   int first_urb = push_constant_chunks;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   /* From the BDW PRM: for 3DSTATE_URB_*: VS URB Starting Address
231bf215546Sopenharmony_ci    *
232bf215546Sopenharmony_ci    *    "Value: [4,48] Device [SliceCount] GT 1"
233bf215546Sopenharmony_ci    *
234bf215546Sopenharmony_ci    * From the ICL PRMs and above :
235bf215546Sopenharmony_ci    *
236bf215546Sopenharmony_ci    *    "If CTXT_SR_CTL::POSH_Enable is clear and Push Constants are required
237bf215546Sopenharmony_ci    *     or Device[SliceCount] GT 1, the lower limit is 4."
238bf215546Sopenharmony_ci    *
239bf215546Sopenharmony_ci    *    "If Push Constants are not required andDevice[SliceCount] == 1, the
240bf215546Sopenharmony_ci    *     lower limit is 0."
241bf215546Sopenharmony_ci    */
242bf215546Sopenharmony_ci   if ((devinfo->ver == 8 && devinfo->num_slices == 1) ||
243bf215546Sopenharmony_ci       (devinfo->ver >= 11 && push_constant_chunks > 0 && devinfo->num_slices == 1))
244bf215546Sopenharmony_ci      first_urb = MAX2(first_urb, 4);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci   int next_urb = first_urb;
247bf215546Sopenharmony_ci   for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
248bf215546Sopenharmony_ci      if (entries[i]) {
249bf215546Sopenharmony_ci         start[i] = next_urb;
250bf215546Sopenharmony_ci         next_urb += chunks[i];
251bf215546Sopenharmony_ci      } else {
252bf215546Sopenharmony_ci         /* Put disabled stages at the beginning of the valid range */
253bf215546Sopenharmony_ci         start[i] = first_urb;
254bf215546Sopenharmony_ci      }
255bf215546Sopenharmony_ci   }
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci   if (deref_block_size) {
258bf215546Sopenharmony_ci      if (devinfo->ver >= 12) {
259bf215546Sopenharmony_ci         /* From the Gfx12 BSpec:
260bf215546Sopenharmony_ci          *
261bf215546Sopenharmony_ci          *    "Deref Block size depends on the last enabled shader and number
262bf215546Sopenharmony_ci          *    of handles programmed for that shader
263bf215546Sopenharmony_ci          *
264bf215546Sopenharmony_ci          *       1) For GS last shader enabled cases, the deref block is
265bf215546Sopenharmony_ci          *          always set to a per poly(within hardware)
266bf215546Sopenharmony_ci          *
267bf215546Sopenharmony_ci          *    If the last enabled shader is VS or DS.
268bf215546Sopenharmony_ci          *
269bf215546Sopenharmony_ci          *       1) If DS is last enabled shader then if the number of DS
270bf215546Sopenharmony_ci          *          handles is less than 324, need to set per poly deref.
271bf215546Sopenharmony_ci          *
272bf215546Sopenharmony_ci          *       2) If VS is last enabled shader then if the number of VS
273bf215546Sopenharmony_ci          *          handles is less than 192, need to set per poly deref"
274bf215546Sopenharmony_ci          *
275bf215546Sopenharmony_ci          * The default is 32 so we assume that's the right choice if we're
276bf215546Sopenharmony_ci          * not in one of the explicit cases listed above.
277bf215546Sopenharmony_ci          */
278bf215546Sopenharmony_ci         if (gs_present) {
279bf215546Sopenharmony_ci            *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
280bf215546Sopenharmony_ci         } else if (tess_present) {
281bf215546Sopenharmony_ci            if (entries[MESA_SHADER_TESS_EVAL] < 324)
282bf215546Sopenharmony_ci               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
283bf215546Sopenharmony_ci            else
284bf215546Sopenharmony_ci               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;
285bf215546Sopenharmony_ci         } else {
286bf215546Sopenharmony_ci            if (entries[MESA_SHADER_VERTEX] < 192)
287bf215546Sopenharmony_ci               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
288bf215546Sopenharmony_ci            else
289bf215546Sopenharmony_ci               *deref_block_size = INTEL_URB_DEREF_BLOCK_SIZE_32;
290bf215546Sopenharmony_ci         }
291bf215546Sopenharmony_ci      } else {
292bf215546Sopenharmony_ci         *deref_block_size = 0;
293bf215546Sopenharmony_ci      }
294bf215546Sopenharmony_ci   }
295bf215546Sopenharmony_ci}
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_cistruct intel_mesh_urb_allocation
298bf215546Sopenharmony_ciintel_get_mesh_urb_config(const struct intel_device_info *devinfo,
299bf215546Sopenharmony_ci                          const struct intel_l3_config *l3_cfg,
300bf215546Sopenharmony_ci                          unsigned tue_size_dw, unsigned mue_size_dw)
301bf215546Sopenharmony_ci{
302bf215546Sopenharmony_ci   struct intel_mesh_urb_allocation r = {0};
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   /* Allocation Size must be aligned to 64B. */
305bf215546Sopenharmony_ci   r.task_entry_size_64b = DIV_ROUND_UP(tue_size_dw * 4, 64);
306bf215546Sopenharmony_ci   r.mesh_entry_size_64b = DIV_ROUND_UP(mue_size_dw * 4, 64);
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   assert(r.task_entry_size_64b <= 1024);
309bf215546Sopenharmony_ci   assert(r.mesh_entry_size_64b <= 1024);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   /* Per-slice URB size. */
312bf215546Sopenharmony_ci   unsigned total_urb_kb = intel_get_l3_config_urb_size(devinfo, l3_cfg);
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   /* Programming Note in bspec requires all the slice to have the same number
315bf215546Sopenharmony_ci    * of entries, so we need to discount the space for constants for all of
316bf215546Sopenharmony_ci    * them.  See 3DSTATE_URB_ALLOC_MESH and 3DSTATE_URB_ALLOC_TASK.
317bf215546Sopenharmony_ci    */
318bf215546Sopenharmony_ci   const unsigned push_constant_kb = devinfo->max_constant_urb_size_kb;
319bf215546Sopenharmony_ci   total_urb_kb -= push_constant_kb;
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci   /* TODO(mesh): Take push constant size as parameter instead of considering always
322bf215546Sopenharmony_ci    * the max? */
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci   float task_urb_share = 0.0f;
325bf215546Sopenharmony_ci   if (r.task_entry_size_64b > 0) {
326bf215546Sopenharmony_ci      /* By default, assign 10% to TASK and 90% to MESH, since we expect MESH
327bf215546Sopenharmony_ci       * to use larger URB entries since it contains all the vertex and
328bf215546Sopenharmony_ci       * primitive data.  Environment variable allow us to tweak it.
329bf215546Sopenharmony_ci       *
330bf215546Sopenharmony_ci       * TODO(mesh): Re-evaluate if this is a good default once there are more
331bf215546Sopenharmony_ci       * workloads.
332bf215546Sopenharmony_ci       */
333bf215546Sopenharmony_ci      static int task_urb_share_percentage = -1;
334bf215546Sopenharmony_ci      if (task_urb_share_percentage < 0) {
335bf215546Sopenharmony_ci         task_urb_share_percentage =
336bf215546Sopenharmony_ci            MIN2(env_var_as_unsigned("INTEL_MESH_TASK_URB_SHARE", 10), 100);
337bf215546Sopenharmony_ci      }
338bf215546Sopenharmony_ci      task_urb_share = task_urb_share_percentage / 100.0f;
339bf215546Sopenharmony_ci   }
340bf215546Sopenharmony_ci
341bf215546Sopenharmony_ci   const unsigned one_task_urb_kb = ALIGN(r.task_entry_size_64b * 64, 1024) / 1024;
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci   const unsigned task_urb_kb = ALIGN(MAX2(total_urb_kb * task_urb_share, one_task_urb_kb), 8);
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ci   const unsigned mesh_urb_kb = total_urb_kb - task_urb_kb;
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   /* TODO(mesh): Could we avoid allocating URB for Mesh if rasterization is
348bf215546Sopenharmony_ci    * disabled? */
349bf215546Sopenharmony_ci
350bf215546Sopenharmony_ci   unsigned next_address_8kb = DIV_ROUND_UP(push_constant_kb, 8);
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci   if (r.task_entry_size_64b > 0) {
353bf215546Sopenharmony_ci      r.task_entries = MIN2((task_urb_kb * 16) / r.task_entry_size_64b, 1548);
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci      /* 3DSTATE_URB_ALLOC_TASK_BODY says
356bf215546Sopenharmony_ci       *
357bf215546Sopenharmony_ci       *   TASK Number of URB Entries must be divisible by 8 if the TASK URB
358bf215546Sopenharmony_ci       *   Entry Allocation Size is less than 9 512-bit URB entries.
359bf215546Sopenharmony_ci       */
360bf215546Sopenharmony_ci      if (r.task_entry_size_64b < 9)
361bf215546Sopenharmony_ci         r.task_entries = ROUND_DOWN_TO(r.task_entries, 8);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci      r.task_starting_address_8kb = next_address_8kb;
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci      assert(task_urb_kb % 8 == 0);
366bf215546Sopenharmony_ci      next_address_8kb += task_urb_kb / 8;
367bf215546Sopenharmony_ci   }
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   r.mesh_entries = MIN2((mesh_urb_kb * 16) / r.mesh_entry_size_64b, 1548);
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   /* Similar restriction to TASK. */
372bf215546Sopenharmony_ci   if (r.mesh_entry_size_64b < 9)
373bf215546Sopenharmony_ci      r.mesh_entries = ROUND_DOWN_TO(r.mesh_entries, 8);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci   r.mesh_starting_address_8kb = next_address_8kb;
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci   r.deref_block_size = r.mesh_entries > 32 ?
378bf215546Sopenharmony_ci      INTEL_URB_DEREF_BLOCK_SIZE_MESH :
379bf215546Sopenharmony_ci      INTEL_URB_DEREF_BLOCK_SIZE_PER_POLY;
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci   return r;
382bf215546Sopenharmony_ci}
383