i965: Flush around state base address
[mesa.git] / src / mesa / drivers / dri / i965 / genX_blorp_exec.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25
26 #include "intel_batchbuffer.h"
27 #include "intel_mipmap_tree.h"
28 #include "intel_fbo.h"
29
30 #include "brw_context.h"
31 #include "brw_state.h"
32
33 #include "blorp/blorp_genX_exec.h"
34
35 #if GEN_GEN <= 5
36 #include "gen4_blorp_exec.h"
37 #endif
38
39 #include "brw_blorp.h"
40
41 static void *
42 blorp_emit_dwords(struct blorp_batch *batch, unsigned n)
43 {
44 assert(batch->blorp->driver_ctx == batch->driver_batch);
45 struct brw_context *brw = batch->driver_batch;
46
47 intel_batchbuffer_begin(brw, n, RENDER_RING);
48 uint32_t *map = brw->batch.map_next;
49 brw->batch.map_next += n;
50 intel_batchbuffer_advance(brw);
51 return map;
52 }
53
54 static uint64_t
55 blorp_emit_reloc(struct blorp_batch *batch,
56 void *location, struct blorp_address address, uint32_t delta)
57 {
58 assert(batch->blorp->driver_ctx == batch->driver_batch);
59 struct brw_context *brw = batch->driver_batch;
60
61 uint32_t offset = (char *)location - (char *)brw->batch.map;
62 return brw_emit_reloc(&brw->batch, offset,
63 address.buffer, address.offset + delta,
64 address.read_domains,
65 address.write_domain);
66 }
67
68 static void
69 blorp_surface_reloc(struct blorp_batch *batch, uint32_t ss_offset,
70 struct blorp_address address, uint32_t delta)
71 {
72 assert(batch->blorp->driver_ctx == batch->driver_batch);
73 struct brw_context *brw = batch->driver_batch;
74 struct brw_bo *bo = address.buffer;
75
76 brw_emit_reloc(&brw->batch, ss_offset, bo, address.offset + delta,
77 address.read_domains, address.write_domain);
78
79 uint64_t reloc_val = bo->offset64 + address.offset + delta;
80 void *reloc_ptr = (void *)brw->batch.map + ss_offset;
81 #if GEN_GEN >= 8
82 *(uint64_t *)reloc_ptr = reloc_val;
83 #else
84 *(uint32_t *)reloc_ptr = reloc_val;
85 #endif
86 }
87
88 static void *
89 blorp_alloc_dynamic_state(struct blorp_batch *batch,
90 uint32_t size,
91 uint32_t alignment,
92 uint32_t *offset)
93 {
94 assert(batch->blorp->driver_ctx == batch->driver_batch);
95 struct brw_context *brw = batch->driver_batch;
96
97 return brw_state_batch(brw, size, alignment, offset);
98 }
99
100 static void
101 blorp_alloc_binding_table(struct blorp_batch *batch, unsigned num_entries,
102 unsigned state_size, unsigned state_alignment,
103 uint32_t *bt_offset, uint32_t *surface_offsets,
104 void **surface_maps)
105 {
106 assert(batch->blorp->driver_ctx == batch->driver_batch);
107 struct brw_context *brw = batch->driver_batch;
108
109 uint32_t *bt_map = brw_state_batch(brw,
110 num_entries * sizeof(uint32_t), 32,
111 bt_offset);
112
113 for (unsigned i = 0; i < num_entries; i++) {
114 surface_maps[i] = brw_state_batch(brw,
115 state_size, state_alignment,
116 &(surface_offsets)[i]);
117 bt_map[i] = surface_offsets[i];
118 }
119 }
120
121 static void *
122 blorp_alloc_vertex_buffer(struct blorp_batch *batch, uint32_t size,
123 struct blorp_address *addr)
124 {
125 assert(batch->blorp->driver_ctx == batch->driver_batch);
126 struct brw_context *brw = batch->driver_batch;
127
128 /* From the Skylake PRM, 3DSTATE_VERTEX_BUFFERS:
129 *
130 * "The VF cache needs to be invalidated before binding and then using
131 * Vertex Buffers that overlap with any previously bound Vertex Buffer
132 * (at a 64B granularity) since the last invalidation. A VF cache
133 * invalidate is performed by setting the "VF Cache Invalidation Enable"
134 * bit in PIPE_CONTROL."
135 *
136 * This restriction first appears in the Skylake PRM but the internal docs
137 * also list it as being an issue on Broadwell. In order to avoid this
138 * problem, we align all vertex buffer allocations to 64 bytes.
139 */
140 uint32_t offset;
141 void *data = brw_state_batch(brw, size, 64, &offset);
142
143 *addr = (struct blorp_address) {
144 .buffer = brw->batch.bo,
145 .read_domains = I915_GEM_DOMAIN_VERTEX,
146 .write_domain = 0,
147 .offset = offset,
148 };
149
150 return data;
151 }
152
153 #if GEN_GEN >= 8
154 static struct blorp_address
155 blorp_get_workaround_page(struct blorp_batch *batch)
156 {
157 assert(batch->blorp->driver_ctx == batch->driver_batch);
158 struct brw_context *brw = batch->driver_batch;
159
160 return (struct blorp_address) {
161 .buffer = brw->workaround_bo,
162 };
163 }
164 #endif
165
166 static void
167 blorp_flush_range(struct blorp_batch *batch, void *start, size_t size)
168 {
169 /* All allocated states come from the batch which we will flush before we
170 * submit it. There's nothing for us to do here.
171 */
172 }
173
174 static void
175 blorp_emit_urb_config(struct blorp_batch *batch,
176 unsigned vs_entry_size, unsigned sf_entry_size)
177 {
178 assert(batch->blorp->driver_ctx == batch->driver_batch);
179 struct brw_context *brw = batch->driver_batch;
180
181 #if GEN_GEN >= 7
182 if (!(brw->ctx.NewDriverState & (BRW_NEW_CONTEXT | BRW_NEW_URB_SIZE)) &&
183 brw->urb.vsize >= vs_entry_size)
184 return;
185
186 brw->ctx.NewDriverState |= BRW_NEW_URB_SIZE;
187
188 gen7_upload_urb(brw, vs_entry_size, false, false);
189 #elif GEN_GEN == 6
190 gen6_upload_urb(brw, vs_entry_size, false, 0);
191 #else
192 /* We calculate it now and emit later. */
193 brw_calculate_urb_fence(brw, 0, vs_entry_size, sf_entry_size);
194 #endif
195 }
196
197 void
198 genX(blorp_exec)(struct blorp_batch *batch,
199 const struct blorp_params *params)
200 {
201 assert(batch->blorp->driver_ctx == batch->driver_batch);
202 struct brw_context *brw = batch->driver_batch;
203 struct gl_context *ctx = &brw->ctx;
204 const uint32_t estimated_max_batch_usage = GEN_GEN >= 8 ? 1920 : 1700;
205 bool check_aperture_failed_once = false;
206
207 /* Flush the sampler and render caches. We definitely need to flush the
208 * sampler cache so that we get updated contents from the render cache for
209 * the glBlitFramebuffer() source. Also, we are sometimes warned in the
210 * docs to flush the cache between reinterpretations of the same surface
211 * data with different formats, which blorp does for stencil and depth
212 * data.
213 */
214 if (params->src.enabled)
215 brw_render_cache_set_check_flush(brw, params->src.addr.buffer);
216 brw_render_cache_set_check_flush(brw, params->dst.addr.buffer);
217
218 brw_select_pipeline(brw, BRW_RENDER_PIPELINE);
219
220 retry:
221 intel_batchbuffer_require_space(brw, estimated_max_batch_usage, RENDER_RING);
222 intel_batchbuffer_save_state(brw);
223 struct brw_bo *saved_bo = brw->batch.bo;
224 uint32_t saved_used = USED_BATCH(brw->batch);
225 uint32_t saved_state_batch_offset = brw->batch.state_batch_offset;
226
227 #if GEN_GEN == 6
228 /* Emit workaround flushes when we switch from drawing to blorping. */
229 brw_emit_post_sync_nonzero_flush(brw);
230 #endif
231
232 brw_upload_state_base_address(brw);
233
234 #if GEN_GEN >= 8
235 gen7_l3_state.emit(brw);
236 #endif
237
238 #if GEN_GEN >= 6
239 brw_emit_depth_stall_flushes(brw);
240 #endif
241
242 #if GEN_GEN == 8
243 gen8_write_pma_stall_bits(brw, 0);
244 #endif
245
246 blorp_emit(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
247 rect.ClippedDrawingRectangleXMax = MAX2(params->x1, params->x0) - 1;
248 rect.ClippedDrawingRectangleYMax = MAX2(params->y1, params->y0) - 1;
249 }
250
251 blorp_exec(batch, params);
252
253 /* Make sure we didn't wrap the batch unintentionally, and make sure we
254 * reserved enough space that a wrap will never happen.
255 */
256 assert(brw->batch.bo == saved_bo);
257 assert((USED_BATCH(brw->batch) - saved_used) * 4 +
258 (saved_state_batch_offset - brw->batch.state_batch_offset) <
259 estimated_max_batch_usage);
260 /* Shut up compiler warnings on release build */
261 (void)saved_bo;
262 (void)saved_used;
263 (void)saved_state_batch_offset;
264
265 /* Check if the blorp op we just did would make our batch likely to fail to
266 * map all the BOs into the GPU at batch exec time later. If so, flush the
267 * batch and try again with nothing else in the batch.
268 */
269 if (!brw_batch_has_aperture_space(brw, 0)) {
270 if (!check_aperture_failed_once) {
271 check_aperture_failed_once = true;
272 intel_batchbuffer_reset_to_saved(brw);
273 intel_batchbuffer_flush(brw);
274 goto retry;
275 } else {
276 int ret = intel_batchbuffer_flush(brw);
277 WARN_ONCE(ret == -ENOSPC,
278 "i965: blorp emit exceeded available aperture space\n");
279 }
280 }
281
282 if (unlikely(brw->always_flush_batch))
283 intel_batchbuffer_flush(brw);
284
285 /* We've smashed all state compared to what the normal 3D pipeline
286 * rendering tracks for GL.
287 */
288 brw->ctx.NewDriverState |= BRW_NEW_BLORP;
289 brw->no_depth_or_stencil = false;
290 brw->ib.index_size = -1;
291
292 if (params->dst.enabled)
293 brw_render_cache_set_add_bo(brw, params->dst.addr.buffer);
294 if (params->depth.enabled)
295 brw_render_cache_set_add_bo(brw, params->depth.addr.buffer);
296 if (params->stencil.enabled)
297 brw_render_cache_set_add_bo(brw, params->stencil.addr.buffer);
298 }