2 * Copyright (c) 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include "util/macros.h"
28 #include "main/macros.h"
30 #include "gen_l3_config.h"
33 * The following diagram shows how we partition the URB:
35 * 16kb or 32kb Rest of the URB space
36 * __________-__________ _________________-_________________
38 * +-------------------------------------------------------------+
39 * | VS/HS/DS/GS/FS Push | VS/HS/DS/GS URB |
40 * | Constants | Entries |
41 * +-------------------------------------------------------------+
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 them
45 * out in pipeline order (VS -> HS -> DS -> GS).
49 * Decide how to partition the URB among the various stages.
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 stage
57 * \param[out] start - the starting offset for each stage
60 gen_get_urb_config(const struct gen_device_info
*devinfo
,
61 unsigned push_constant_bytes
, unsigned urb_size_bytes
,
62 bool tess_present
, bool gs_present
,
63 const unsigned entry_size
[4],
64 unsigned entries
[4], unsigned start
[4])
66 const bool active
[4] = { true, tess_present
, tess_present
, gs_present
};
68 /* URB allocations must be done in 8k chunks. */
69 const unsigned chunk_size_bytes
= 8192;
71 const unsigned push_constant_chunks
=
72 push_constant_bytes
/ chunk_size_bytes
;
73 const unsigned urb_chunks
= urb_size_bytes
/ chunk_size_bytes
;
75 /* From p35 of the Ivy Bridge PRM (section 1.7.1: 3DSTATE_URB_GS):
77 * VS Number of URB Entries must be divisible by 8 if the VS URB Entry
78 * Allocation Size is less than 9 512-bit URB entries.
80 * Similar text exists for HS, DS and GS.
82 unsigned granularity
[4];
83 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
84 granularity
[i
] = (entry_size
[i
] < 9) ? 8 : 1;
87 unsigned min_entries
[4] = {
88 /* VS has a lower limit on the number of URB entries.
90 * From the Broadwell PRM, 3DSTATE_URB_VS instruction:
91 * "When tessellation is enabled, the VS Number of URB Entries must be
92 * greater than or equal to 192."
94 [MESA_SHADER_VERTEX
] = tess_present
&& devinfo
->gen
== 8 ?
95 192 : devinfo
->urb
.min_entries
[MESA_SHADER_VERTEX
],
97 /* There are two constraints on the minimum amount of URB space we can
100 * (1) We need room for at least 2 URB entries, since we always operate
101 * the GS in DUAL_OBJECT mode.
103 * (2) We can't allocate less than nr_gs_entries_granularity.
105 [MESA_SHADER_GEOMETRY
] = gs_present
? 2 : 0,
107 [MESA_SHADER_TESS_CTRL
] = tess_present
? 1 : 0,
109 [MESA_SHADER_TESS_EVAL
] = tess_present
?
110 devinfo
->urb
.min_entries
[MESA_SHADER_TESS_EVAL
] : 0,
113 /* Min VS Entries isn't a multiple of 8 on Cherryview/Broxton; round up.
116 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
117 min_entries
[i
] = ALIGN(min_entries
[i
], granularity
[i
]);
120 unsigned entry_size_bytes
[4];
121 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
122 entry_size_bytes
[i
] = 64 * entry_size
[i
];
125 /* Initially, assign each stage the minimum amount of URB space it needs,
126 * and make a note of how much additional space it "wants" (the amount of
127 * additional space it could actually make use of).
131 unsigned total_needs
= push_constant_chunks
;
132 unsigned total_wants
= 0;
134 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
136 chunks
[i
] = DIV_ROUND_UP(min_entries
[i
] * entry_size_bytes
[i
],
140 DIV_ROUND_UP(devinfo
->urb
.max_entries
[i
] * entry_size_bytes
[i
],
141 chunk_size_bytes
) - chunks
[i
];
147 total_needs
+= chunks
[i
];
148 total_wants
+= wants
[i
];
151 assert(total_needs
<= urb_chunks
);
153 /* Mete out remaining space (if any) in proportion to "wants". */
154 unsigned remaining_space
= MIN2(urb_chunks
- total_needs
, total_wants
);
156 if (remaining_space
> 0) {
157 for (int i
= MESA_SHADER_VERTEX
;
158 total_wants
> 0 && i
<= MESA_SHADER_TESS_EVAL
; i
++) {
159 unsigned additional
= (unsigned)
160 roundf(wants
[i
] * (((float) remaining_space
) / total_wants
));
161 chunks
[i
] += additional
;
162 remaining_space
-= additional
;
163 total_wants
-= wants
[i
];
166 chunks
[MESA_SHADER_GEOMETRY
] += remaining_space
;
169 /* Sanity check that we haven't over-allocated. */
170 unsigned total_chunks
= push_constant_chunks
;
171 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
172 total_chunks
+= chunks
[i
];
174 assert(total_chunks
<= urb_chunks
);
176 /* Finally, compute the number of entries that can fit in the space
177 * allocated to each stage.
179 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
180 entries
[i
] = chunks
[i
] * chunk_size_bytes
/ entry_size_bytes
[i
];
182 /* Since we rounded up when computing wants[], this may be slightly
183 * more than the maximum allowed amount, so correct for that.
185 entries
[i
] = MIN2(entries
[i
], devinfo
->urb
.max_entries
[i
]);
187 /* Ensure that we program a multiple of the granularity. */
188 entries
[i
] = ROUND_DOWN_TO(entries
[i
], granularity
[i
]);
190 /* Finally, sanity check to make sure we have at least the minimum
191 * number of entries needed for each stage.
193 assert(entries
[i
] >= min_entries
[i
]);
196 /* Lay out the URB in pipeline order: push constants, VS, HS, DS, GS. */
197 start
[0] = push_constant_chunks
;
198 for (int i
= MESA_SHADER_TESS_CTRL
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
199 start
[i
] = start
[i
- 1] + chunks
[i
- 1];