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/u_debug.h"
28 #include "util/ralloc.h"
29 #include "util/u_inlines.h"
30 #include "util/format/u_format.h"
31 #include "util/u_upload_mgr.h"
32 #include "iris_context.h"
33 #include "iris_perf.h"
34 #include "iris_resource.h"
35 #include "iris_screen.h"
36 #include "iris_utrace.h"
37 #include "common/intel_sample_positions.h"
38
39 /**
40 * The pipe->set_debug_callback() driver hook.
41 */
42 static void
iris_set_debug_callback(struct pipe_context * ctx,const struct util_debug_callback * cb)43 iris_set_debug_callback(struct pipe_context *ctx,
44 const struct util_debug_callback *cb)
45 {
46 struct iris_context *ice = (struct iris_context *)ctx;
47 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
48
49 util_queue_finish(&screen->shader_compiler_queue);
50
51 if (cb)
52 ice->dbg = *cb;
53 else
54 memset(&ice->dbg, 0, sizeof(ice->dbg));
55 }
56
57 /**
58 * Called from the batch module when it detects a GPU hang.
59 *
60 * In this case, we've lost our GEM context, and can't rely on any existing
61 * state on the GPU. We must mark everything dirty and wipe away any saved
62 * assumptions about the last known state of the GPU.
63 */
64 void
iris_lost_context_state(struct iris_batch * batch)65 iris_lost_context_state(struct iris_batch *batch)
66 {
67 struct iris_context *ice = batch->ice;
68
69 if (batch->name == IRIS_BATCH_RENDER) {
70 batch->screen->vtbl.init_render_context(batch);
71 } else if (batch->name == IRIS_BATCH_COMPUTE) {
72 batch->screen->vtbl.init_compute_context(batch);
73 } else if (batch->name == IRIS_BATCH_BLITTER) {
74 /* No state to set up */
75 } else {
76 unreachable("unhandled batch reset");
77 }
78
79 ice->state.dirty = ~0ull;
80 ice->state.stage_dirty = ~0ull;
81 ice->state.current_hash_scale = 0;
82 memset(&ice->shaders.urb, 0, sizeof(ice->shaders.urb));
83 memset(ice->state.last_block, 0, sizeof(ice->state.last_block));
84 memset(ice->state.last_grid, 0, sizeof(ice->state.last_grid));
85 ice->state.last_grid_dim = 0;
86 batch->last_binder_address = ~0ull;
87 batch->last_aux_map_state = 0;
88 batch->screen->vtbl.lost_genx_state(ice, batch);
89 }
90
91 static enum pipe_reset_status
iris_get_device_reset_status(struct pipe_context * ctx)92 iris_get_device_reset_status(struct pipe_context *ctx)
93 {
94 struct iris_context *ice = (struct iris_context *)ctx;
95
96 enum pipe_reset_status worst_reset = PIPE_NO_RESET;
97
98 /* Check the reset status of each batch's hardware context, and take the
99 * worst status (if one was guilty, proclaim guilt).
100 */
101 iris_foreach_batch(ice, batch) {
102 enum pipe_reset_status batch_reset =
103 iris_batch_check_for_reset(batch);
104
105 if (batch_reset == PIPE_NO_RESET)
106 continue;
107
108 if (worst_reset == PIPE_NO_RESET) {
109 worst_reset = batch_reset;
110 } else {
111 /* GUILTY < INNOCENT < UNKNOWN */
112 worst_reset = MIN2(worst_reset, batch_reset);
113 }
114 }
115
116 if (worst_reset != PIPE_NO_RESET && ice->reset.reset)
117 ice->reset.reset(ice->reset.data, worst_reset);
118
119 return worst_reset;
120 }
121
122 static void
iris_set_device_reset_callback(struct pipe_context * ctx,const struct pipe_device_reset_callback * cb)123 iris_set_device_reset_callback(struct pipe_context *ctx,
124 const struct pipe_device_reset_callback *cb)
125 {
126 struct iris_context *ice = (struct iris_context *)ctx;
127
128 if (cb)
129 ice->reset = *cb;
130 else
131 memset(&ice->reset, 0, sizeof(ice->reset));
132 }
133
134 static void
iris_get_sample_position(struct pipe_context * ctx,unsigned sample_count,unsigned sample_index,float * out_value)135 iris_get_sample_position(struct pipe_context *ctx,
136 unsigned sample_count,
137 unsigned sample_index,
138 float *out_value)
139 {
140 union {
141 struct {
142 float x[16];
143 float y[16];
144 } a;
145 struct {
146 float _0XOffset, _1XOffset, _2XOffset, _3XOffset,
147 _4XOffset, _5XOffset, _6XOffset, _7XOffset,
148 _8XOffset, _9XOffset, _10XOffset, _11XOffset,
149 _12XOffset, _13XOffset, _14XOffset, _15XOffset;
150 float _0YOffset, _1YOffset, _2YOffset, _3YOffset,
151 _4YOffset, _5YOffset, _6YOffset, _7YOffset,
152 _8YOffset, _9YOffset, _10YOffset, _11YOffset,
153 _12YOffset, _13YOffset, _14YOffset, _15YOffset;
154 } v;
155 } u;
156 switch (sample_count) {
157 case 1: INTEL_SAMPLE_POS_1X(u.v._); break;
158 case 2: INTEL_SAMPLE_POS_2X(u.v._); break;
159 case 4: INTEL_SAMPLE_POS_4X(u.v._); break;
160 case 8: INTEL_SAMPLE_POS_8X(u.v._); break;
161 case 16: INTEL_SAMPLE_POS_16X(u.v._); break;
162 default: unreachable("invalid sample count");
163 }
164
165 out_value[0] = u.a.x[sample_index];
166 out_value[1] = u.a.y[sample_index];
167 }
168
169 static bool
create_dirty_dmabuf_set(struct iris_context * ice)170 create_dirty_dmabuf_set(struct iris_context *ice)
171 {
172 assert(ice->dirty_dmabufs == NULL);
173
174 ice->dirty_dmabufs = _mesa_pointer_set_create(ice);
175 return ice->dirty_dmabufs != NULL;
176 }
177
178 void
iris_mark_dirty_dmabuf(struct iris_context * ice,struct pipe_resource * res)179 iris_mark_dirty_dmabuf(struct iris_context *ice,
180 struct pipe_resource *res)
181 {
182 if (!_mesa_set_search(ice->dirty_dmabufs, res)) {
183 _mesa_set_add(ice->dirty_dmabufs, res);
184 pipe_reference(NULL, &res->reference);
185 }
186 }
187
188 static void
clear_dirty_dmabuf_set(struct iris_context * ice)189 clear_dirty_dmabuf_set(struct iris_context *ice)
190 {
191 set_foreach(ice->dirty_dmabufs, entry) {
192 struct pipe_resource *res = (struct pipe_resource *)entry->key;
193 if (pipe_reference(&res->reference, NULL))
194 res->screen->resource_destroy(res->screen, res);
195 }
196
197 _mesa_set_clear(ice->dirty_dmabufs, NULL);
198 }
199
200 void
iris_flush_dirty_dmabufs(struct iris_context * ice)201 iris_flush_dirty_dmabufs(struct iris_context *ice)
202 {
203 set_foreach(ice->dirty_dmabufs, entry) {
204 struct pipe_resource *res = (struct pipe_resource *)entry->key;
205 ice->ctx.flush_resource(&ice->ctx, res);
206 }
207
208 clear_dirty_dmabuf_set(ice);
209 }
210
211 /**
212 * Destroy a context, freeing any associated memory.
213 */
214 void
iris_destroy_context(struct pipe_context * ctx)215 iris_destroy_context(struct pipe_context *ctx)
216 {
217 struct iris_context *ice = (struct iris_context *)ctx;
218 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
219
220 blorp_finish(&ice->blorp);
221
222 intel_perf_free_context(ice->perf_ctx);
223 if (ctx->stream_uploader)
224 u_upload_destroy(ctx->stream_uploader);
225 if (ctx->const_uploader)
226 u_upload_destroy(ctx->const_uploader);
227
228 clear_dirty_dmabuf_set(ice);
229
230 screen->vtbl.destroy_state(ice);
231
232 for (unsigned i = 0; i < ARRAY_SIZE(ice->shaders.scratch_surfs); i++)
233 pipe_resource_reference(&ice->shaders.scratch_surfs[i].res, NULL);
234
235 for (unsigned i = 0; i < ARRAY_SIZE(ice->shaders.scratch_bos); i++) {
236 for (unsigned j = 0; j < ARRAY_SIZE(ice->shaders.scratch_bos[i]); j++)
237 iris_bo_unreference(ice->shaders.scratch_bos[i][j]);
238 }
239
240 iris_destroy_program_cache(ice);
241 if (screen->measure.config)
242 iris_destroy_ctx_measure(ice);
243
244 u_upload_destroy(ice->state.surface_uploader);
245 u_upload_destroy(ice->state.scratch_surface_uploader);
246 u_upload_destroy(ice->state.dynamic_uploader);
247 u_upload_destroy(ice->query_buffer_uploader);
248
249 iris_destroy_batches(ice);
250 iris_destroy_binder(&ice->state.binder);
251 iris_bo_unreference(ice->draw.generation.ring_bo);
252
253 iris_utrace_fini(ice);
254
255 slab_destroy_child(&ice->transfer_pool);
256 slab_destroy_child(&ice->transfer_pool_unsync);
257
258 ralloc_free(ice);
259 }
260
261 #define genX_call(devinfo, func, ...) \
262 switch ((devinfo)->verx10) { \
263 case 300: \
264 gfx30_##func(__VA_ARGS__); \
265 break; \
266 case 200: \
267 gfx20_##func(__VA_ARGS__); \
268 break; \
269 case 125: \
270 gfx125_##func(__VA_ARGS__); \
271 break; \
272 case 120: \
273 gfx12_##func(__VA_ARGS__); \
274 break; \
275 case 110: \
276 gfx11_##func(__VA_ARGS__); \
277 break; \
278 case 90: \
279 gfx9_##func(__VA_ARGS__); \
280 break; \
281 case 80: \
282 gfx8_##func(__VA_ARGS__); \
283 break; \
284 default: \
285 unreachable("Unknown hardware generation"); \
286 }
287
288 /**
289 * Create a context.
290 *
291 * This is where each context begins.
292 */
293 struct pipe_context *
iris_create_context(struct pipe_screen * pscreen,void * priv,unsigned flags)294 iris_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)
295 {
296 struct iris_screen *screen = (struct iris_screen*)pscreen;
297 const struct intel_device_info *devinfo = screen->devinfo;
298 struct iris_context *ice = rzalloc(NULL, struct iris_context);
299
300 if (!ice)
301 return NULL;
302
303 struct pipe_context *ctx = &ice->ctx;
304
305 ctx->screen = pscreen;
306 ctx->priv = priv;
307
308 ctx->stream_uploader = u_upload_create(ctx, 1024 * 1024 * 2,
309 PIPE_BIND_VERTEX_BUFFER |
310 PIPE_BIND_INDEX_BUFFER |
311 PIPE_BIND_CONSTANT_BUFFER,
312 PIPE_USAGE_STREAM, 0);
313 if (!ctx->stream_uploader) {
314 ralloc_free(ice);
315 return NULL;
316 }
317 ctx->const_uploader = u_upload_create(ctx, 1024 * 1024,
318 PIPE_BIND_CONSTANT_BUFFER,
319 PIPE_USAGE_IMMUTABLE,
320 IRIS_RESOURCE_FLAG_DEVICE_MEM);
321 if (!ctx->const_uploader) {
322 u_upload_destroy(ctx->stream_uploader);
323 ralloc_free(ice);
324 return NULL;
325 }
326
327 if (!create_dirty_dmabuf_set(ice)) {
328 ralloc_free(ice);
329 return NULL;
330 }
331
332 ctx->destroy = iris_destroy_context;
333 ctx->set_debug_callback = iris_set_debug_callback;
334 ctx->set_device_reset_callback = iris_set_device_reset_callback;
335 ctx->get_device_reset_status = iris_get_device_reset_status;
336 ctx->get_sample_position = iris_get_sample_position;
337
338 iris_init_context_fence_functions(ctx);
339 iris_init_blit_functions(ctx);
340 iris_init_clear_functions(ctx);
341 iris_init_program_functions(ctx);
342 iris_init_resource_functions(ctx);
343 iris_init_flush_functions(ctx);
344 iris_init_perfquery_functions(ctx);
345
346 iris_init_program_cache(ice);
347 iris_init_binder(ice);
348
349 slab_create_child(&ice->transfer_pool, &screen->transfer_pool);
350 slab_create_child(&ice->transfer_pool_unsync, &screen->transfer_pool);
351
352 ice->state.surface_uploader =
353 u_upload_create(ctx, 64 * 1024, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
354 IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |
355 IRIS_RESOURCE_FLAG_DEVICE_MEM);
356 ice->state.scratch_surface_uploader =
357 u_upload_create(ctx, 64 * 1024, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
358 IRIS_RESOURCE_FLAG_SCRATCH_MEMZONE |
359 IRIS_RESOURCE_FLAG_DEVICE_MEM);
360 ice->state.dynamic_uploader =
361 u_upload_create(ctx, 64 * 1024, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
362 IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE |
363 IRIS_RESOURCE_FLAG_DEVICE_MEM);
364
365 ice->query_buffer_uploader =
366 u_upload_create(ctx, 16 * 1024, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING,
367 0);
368
369 genX_call(devinfo, init_state, ice);
370 genX_call(devinfo, init_blorp, ice);
371 genX_call(devinfo, init_query, ice);
372
373 if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
374 ice->priority = IRIS_CONTEXT_HIGH_PRIORITY;
375 if (flags & PIPE_CONTEXT_LOW_PRIORITY)
376 ice->priority = IRIS_CONTEXT_LOW_PRIORITY;
377 if (flags & PIPE_CONTEXT_PROTECTED)
378 ice->protected = true;
379
380 if (INTEL_DEBUG(DEBUG_BATCH))
381 ice->state.sizes = _mesa_hash_table_u64_create(ice);
382
383 /* Do this before initializing the batches */
384 iris_utrace_init(ice);
385
386 iris_init_batches(ice);
387
388 screen->vtbl.init_render_context(&ice->batches[IRIS_BATCH_RENDER]);
389 screen->vtbl.init_compute_context(&ice->batches[IRIS_BATCH_COMPUTE]);
390 screen->vtbl.init_copy_context(&ice->batches[IRIS_BATCH_BLITTER]);
391
392 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
393 return ctx;
394
395 /* Clover doesn't support u_threaded_context */
396 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
397 return ctx;
398
399 return threaded_context_create(ctx, &screen->transfer_pool,
400 iris_replace_buffer_storage,
401 &(struct threaded_context_options){
402 .unsynchronized_get_device_reset_status = true,
403 },
404 &ice->thrctx);
405 }
406