iris: Enable fast clears on other miplevels and layers than 0.
[mesa.git] / src / gallium / drivers / iris / iris_context.c
1 /*
2 * Copyright © 2017 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <time.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "util/ralloc.h"
28 #include "util/u_inlines.h"
29 #include "util/u_format.h"
30 #include "util/u_upload_mgr.h"
31 #include "drm-uapi/i915_drm.h"
32 #include "iris_context.h"
33 #include "iris_resource.h"
34 #include "iris_screen.h"
35 #include "common/gen_defines.h"
36 #include "common/gen_sample_positions.h"
37
38 /**
39 * For debugging purposes, this returns a time in seconds.
40 */
41 double
42 get_time(void)
43 {
44 struct timespec tp;
45
46 clock_gettime(CLOCK_MONOTONIC, &tp);
47
48 return tp.tv_sec + tp.tv_nsec / 1000000000.0;
49 }
50
51 /**
52 * The pipe->set_debug_callback() driver hook.
53 */
54 static void
55 iris_set_debug_callback(struct pipe_context *ctx,
56 const struct pipe_debug_callback *cb)
57 {
58 struct iris_context *ice = (struct iris_context *)ctx;
59
60 if (cb)
61 ice->dbg = *cb;
62 else
63 memset(&ice->dbg, 0, sizeof(ice->dbg));
64 }
65
66 /**
67 * Called from the batch module when it detects a GPU hang.
68 *
69 * In this case, we've lost our GEM context, and can't rely on any existing
70 * state on the GPU. We must mark everything dirty and wipe away any saved
71 * assumptions about the last known state of the GPU.
72 */
73 void
74 iris_lost_context_state(struct iris_batch *batch)
75 {
76 /* The batch module doesn't have an iris_context, because we want to
77 * avoid introducing lots of layering violations. Unfortunately, here
78 * we do need to inform the context of batch catastrophe. We know the
79 * batch is one of our context's, so hackily claw our way back.
80 */
81 struct iris_context *ice = NULL;
82 struct iris_screen *screen;
83
84 if (batch->name == IRIS_BATCH_RENDER) {
85 ice = container_of(batch, ice, batches[IRIS_BATCH_RENDER]);
86 assert(&ice->batches[IRIS_BATCH_RENDER] == batch);
87 screen = (void *) ice->ctx.screen;
88
89 ice->vtbl.init_render_context(screen, batch, &ice->vtbl, &ice->dbg);
90 } else if (batch->name == IRIS_BATCH_COMPUTE) {
91 ice = container_of(batch, ice, batches[IRIS_BATCH_COMPUTE]);
92 assert(&ice->batches[IRIS_BATCH_COMPUTE] == batch);
93 screen = (void *) ice->ctx.screen;
94
95 ice->vtbl.init_compute_context(screen, batch, &ice->vtbl, &ice->dbg);
96 } else {
97 unreachable("unhandled batch reset");
98 }
99
100 ice->state.dirty = ~0ull;
101 memset(ice->state.last_grid, 0, sizeof(ice->state.last_grid));
102 batch->last_surface_base_address = ~0ull;
103 }
104
105 static enum pipe_reset_status
106 iris_get_device_reset_status(struct pipe_context *ctx)
107 {
108 struct iris_context *ice = (struct iris_context *)ctx;
109
110 enum pipe_reset_status worst_reset = PIPE_NO_RESET;
111
112 /* Check the reset status of each batch's hardware context, and take the
113 * worst status (if one was guilty, proclaim guilt).
114 */
115 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
116 /* This will also recreate the hardware contexts as necessary, so any
117 * future queries will show no resets. We only want to report once.
118 */
119 enum pipe_reset_status batch_reset =
120 iris_batch_check_for_reset(&ice->batches[i]);
121
122 if (batch_reset == PIPE_NO_RESET)
123 continue;
124
125 if (worst_reset == PIPE_NO_RESET) {
126 worst_reset = batch_reset;
127 } else {
128 /* GUILTY < INNOCENT < UNKNOWN */
129 worst_reset = MIN2(worst_reset, batch_reset);
130 }
131 }
132
133 if (worst_reset != PIPE_NO_RESET && ice->reset.reset)
134 ice->reset.reset(ice->reset.data, worst_reset);
135
136 return worst_reset;
137 }
138
139 static void
140 iris_set_device_reset_callback(struct pipe_context *ctx,
141 const struct pipe_device_reset_callback *cb)
142 {
143 struct iris_context *ice = (struct iris_context *)ctx;
144
145 if (cb)
146 ice->reset = *cb;
147 else
148 memset(&ice->reset, 0, sizeof(ice->reset));
149 }
150
151 static void
152 iris_get_sample_position(struct pipe_context *ctx,
153 unsigned sample_count,
154 unsigned sample_index,
155 float *out_value)
156 {
157 union {
158 struct {
159 float x[16];
160 float y[16];
161 } a;
162 struct {
163 float _0XOffset, _1XOffset, _2XOffset, _3XOffset,
164 _4XOffset, _5XOffset, _6XOffset, _7XOffset,
165 _8XOffset, _9XOffset, _10XOffset, _11XOffset,
166 _12XOffset, _13XOffset, _14XOffset, _15XOffset;
167 float _0YOffset, _1YOffset, _2YOffset, _3YOffset,
168 _4YOffset, _5YOffset, _6YOffset, _7YOffset,
169 _8YOffset, _9YOffset, _10YOffset, _11YOffset,
170 _12YOffset, _13YOffset, _14YOffset, _15YOffset;
171 } v;
172 } u;
173 switch (sample_count) {
174 case 1: GEN_SAMPLE_POS_1X(u.v._); break;
175 case 2: GEN_SAMPLE_POS_2X(u.v._); break;
176 case 4: GEN_SAMPLE_POS_4X(u.v._); break;
177 case 8: GEN_SAMPLE_POS_8X(u.v._); break;
178 case 16: GEN_SAMPLE_POS_16X(u.v._); break;
179 default: unreachable("invalid sample count");
180 }
181
182 out_value[0] = u.a.x[sample_index];
183 out_value[1] = u.a.y[sample_index];
184 }
185
186 /**
187 * Destroy a context, freeing any associated memory.
188 */
189 static void
190 iris_destroy_context(struct pipe_context *ctx)
191 {
192 struct iris_context *ice = (struct iris_context *)ctx;
193
194 if (ctx->stream_uploader)
195 u_upload_destroy(ctx->stream_uploader);
196
197 ice->vtbl.destroy_state(ice);
198 iris_destroy_program_cache(ice);
199 iris_destroy_border_color_pool(ice);
200 u_upload_destroy(ice->state.surface_uploader);
201 u_upload_destroy(ice->state.dynamic_uploader);
202 u_upload_destroy(ice->query_buffer_uploader);
203
204 slab_destroy_child(&ice->transfer_pool);
205
206 iris_batch_free(&ice->batches[IRIS_BATCH_RENDER]);
207 iris_batch_free(&ice->batches[IRIS_BATCH_COMPUTE]);
208 iris_destroy_binder(&ice->state.binder);
209
210 ralloc_free(ice);
211 }
212
213 #define genX_call(devinfo, func, ...) \
214 switch (devinfo->gen) { \
215 case 11: \
216 gen11_##func(__VA_ARGS__); \
217 break; \
218 case 10: \
219 gen10_##func(__VA_ARGS__); \
220 break; \
221 case 9: \
222 gen9_##func(__VA_ARGS__); \
223 break; \
224 case 8: \
225 gen8_##func(__VA_ARGS__); \
226 break; \
227 default: \
228 unreachable("Unknown hardware generation"); \
229 }
230
231 /**
232 * Create a context.
233 *
234 * This is where each context begins.
235 */
236 struct pipe_context *
237 iris_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)
238 {
239 struct iris_screen *screen = (struct iris_screen*)pscreen;
240 const struct gen_device_info *devinfo = &screen->devinfo;
241 struct iris_context *ice = rzalloc(NULL, struct iris_context);
242
243 if (!ice)
244 return NULL;
245
246 struct pipe_context *ctx = &ice->ctx;
247
248 ctx->screen = pscreen;
249 ctx->priv = priv;
250
251 ctx->stream_uploader = u_upload_create_default(ctx);
252 if (!ctx->stream_uploader) {
253 free(ctx);
254 return NULL;
255 }
256 ctx->const_uploader = ctx->stream_uploader;
257
258 ctx->destroy = iris_destroy_context;
259 ctx->set_debug_callback = iris_set_debug_callback;
260 ctx->set_device_reset_callback = iris_set_device_reset_callback;
261 ctx->get_device_reset_status = iris_get_device_reset_status;
262 ctx->get_sample_position = iris_get_sample_position;
263
264 ice->shaders.urb_size = devinfo->urb.size;
265
266 iris_init_context_fence_functions(ctx);
267 iris_init_blit_functions(ctx);
268 iris_init_clear_functions(ctx);
269 iris_init_program_functions(ctx);
270 iris_init_resource_functions(ctx);
271 iris_init_query_functions(ctx);
272 iris_init_flush_functions(ctx);
273
274 iris_init_program_cache(ice);
275 iris_init_border_color_pool(ice);
276 iris_init_binder(ice);
277
278 slab_create_child(&ice->transfer_pool, &screen->transfer_pool);
279
280 ice->state.surface_uploader =
281 u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
282 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE);
283 ice->state.dynamic_uploader =
284 u_upload_create(ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
285 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE);
286
287 ice->query_buffer_uploader =
288 u_upload_create(ctx, 4096, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING,
289 0);
290
291 genX_call(devinfo, init_state, ice);
292 genX_call(devinfo, init_blorp, ice);
293
294 int priority = 0;
295 if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
296 priority = GEN_CONTEXT_HIGH_PRIORITY;
297 if (flags & PIPE_CONTEXT_LOW_PRIORITY)
298 priority = GEN_CONTEXT_LOW_PRIORITY;
299
300 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
301 ice->state.sizes = _mesa_hash_table_u64_create(ice);
302
303 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
304 iris_init_batch(&ice->batches[i], screen, &ice->vtbl, &ice->dbg,
305 &ice->reset, ice->state.sizes,
306 ice->batches, (enum iris_batch_name) i,
307 I915_EXEC_RENDER, priority);
308 }
309
310 ice->vtbl.init_render_context(screen, &ice->batches[IRIS_BATCH_RENDER],
311 &ice->vtbl, &ice->dbg);
312 ice->vtbl.init_compute_context(screen, &ice->batches[IRIS_BATCH_COMPUTE],
313 &ice->vtbl, &ice->dbg);
314
315 return ctx;
316 }