nir: Stop passing an options arg to nir_lower_int64()
[mesa.git] / src / gallium / drivers / freedreno / freedreno_context.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "freedreno_context.h"
28 #include "freedreno_blitter.h"
29 #include "freedreno_draw.h"
30 #include "freedreno_fence.h"
31 #include "freedreno_log.h"
32 #include "freedreno_program.h"
33 #include "freedreno_resource.h"
34 #include "freedreno_texture.h"
35 #include "freedreno_state.h"
36 #include "freedreno_gmem.h"
37 #include "freedreno_query.h"
38 #include "freedreno_query_hw.h"
39 #include "freedreno_util.h"
40 #include "util/u_upload_mgr.h"
41
42 #if DETECT_OS_ANDROID
43 #include "util/u_process.h"
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #endif
47
48 static void
49 fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep,
50 unsigned flags)
51 {
52 struct fd_context *ctx = fd_context(pctx);
53 struct pipe_fence_handle *fence = NULL;
54 // TODO we want to lookup batch if it exists, but not create one if not.
55 struct fd_batch *batch = fd_context_batch(ctx);
56
57 DBG("%p: flush: flags=%x\n", ctx->batch, flags);
58
59 /* In some sequence of events, we can end up with a last_fence that is
60 * not an "fd" fence, which results in eglDupNativeFenceFDANDROID()
61 * errors.
62 */
63 if (flags & PIPE_FLUSH_FENCE_FD)
64 fd_fence_ref(&ctx->last_fence, NULL);
65
66 /* if no rendering since last flush, ie. app just decided it needed
67 * a fence, re-use the last one:
68 */
69 if (ctx->last_fence) {
70 fd_fence_ref(&fence, ctx->last_fence);
71 fd_bc_dump(ctx->screen, "%p: reuse last_fence, remaining:\n", ctx);
72 goto out;
73 }
74
75 if (!batch) {
76 fd_bc_dump(ctx->screen, "%p: NULL batch, remaining:\n", ctx);
77 return;
78 }
79
80 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */
81 fd_fence_ref(&fence, batch->fence);
82
83 if (flags & PIPE_FLUSH_FENCE_FD)
84 batch->needs_out_fence_fd = true;
85
86 fd_bc_dump(ctx->screen, "%p: flushing %p<%u>, flags=0x%x, pending:\n",
87 ctx, batch, batch->seqno, flags);
88
89 if (!ctx->screen->reorder) {
90 fd_batch_flush(batch);
91 } else if (flags & PIPE_FLUSH_DEFERRED) {
92 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx);
93 } else {
94 fd_bc_flush(&ctx->screen->batch_cache, ctx);
95 }
96
97 fd_bc_dump(ctx->screen, "%p: remaining:\n", ctx);
98
99 out:
100 if (fencep)
101 fd_fence_ref(fencep, fence);
102
103 fd_fence_ref(&ctx->last_fence, fence);
104
105 fd_fence_ref(&fence, NULL);
106
107 if (flags & PIPE_FLUSH_END_OF_FRAME)
108 fd_log_eof(ctx);
109 }
110
111 static void
112 fd_texture_barrier(struct pipe_context *pctx, unsigned flags)
113 {
114 if (flags == PIPE_TEXTURE_BARRIER_FRAMEBUFFER) {
115 struct fd_context *ctx = fd_context(pctx);
116
117 if (ctx->framebuffer_barrier) {
118 ctx->framebuffer_barrier(ctx);
119 return;
120 }
121 }
122
123 /* On devices that could sample from GMEM we could possibly do better.
124 * Or if we knew that we were doing GMEM bypass we could just emit a
125 * cache flush, perhaps? But we don't know if future draws would cause
126 * us to use GMEM, and a flush in bypass isn't the end of the world.
127 */
128 fd_context_flush(pctx, NULL, 0);
129 }
130
131 static void
132 fd_memory_barrier(struct pipe_context *pctx, unsigned flags)
133 {
134 if (!(flags & ~PIPE_BARRIER_UPDATE))
135 return;
136
137 fd_context_flush(pctx, NULL, 0);
138 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */
139 }
140
141 static void
142 emit_string_tail(struct fd_ringbuffer *ring, const char *string, int len)
143 {
144 const uint32_t *buf = (const void *)string;
145
146 while (len >= 4) {
147 OUT_RING(ring, *buf);
148 buf++;
149 len -= 4;
150 }
151
152 /* copy remainder bytes without reading past end of input string: */
153 if (len > 0) {
154 uint32_t w = 0;
155 memcpy(&w, buf, len);
156 OUT_RING(ring, w);
157 }
158 }
159
160 /* for prior to a5xx: */
161 void
162 fd_emit_string(struct fd_ringbuffer *ring,
163 const char *string, int len)
164 {
165 /* max packet size is 0x3fff+1 dwords: */
166 len = MIN2(len, 0x4000 * 4);
167
168 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4);
169 emit_string_tail(ring, string, len);
170 }
171
172 /* for a5xx+ */
173 void
174 fd_emit_string5(struct fd_ringbuffer *ring,
175 const char *string, int len)
176 {
177 /* max packet size is 0x3fff dwords: */
178 len = MIN2(len, 0x3fff * 4);
179
180 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4);
181 emit_string_tail(ring, string, len);
182 }
183
184 /**
185 * emit marker string as payload of a no-op packet, which can be
186 * decoded by cffdump.
187 */
188 static void
189 fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len)
190 {
191 struct fd_context *ctx = fd_context(pctx);
192
193 if (!ctx->batch)
194 return;
195
196 ctx->batch->needs_flush = true;
197
198 if (ctx->screen->gpu_id >= 500) {
199 fd_emit_string5(ctx->batch->draw, string, len);
200 } else {
201 fd_emit_string(ctx->batch->draw, string, len);
202 }
203 }
204
205 void
206 fd_context_destroy(struct pipe_context *pctx)
207 {
208 struct fd_context *ctx = fd_context(pctx);
209 unsigned i;
210
211 DBG("");
212
213 fd_screen_lock(ctx->screen);
214 list_del(&ctx->node);
215 fd_screen_unlock(ctx->screen);
216
217 fd_log_process(ctx, true);
218 assert(list_is_empty(&ctx->log_chunks));
219
220 fd_fence_ref(&ctx->last_fence, NULL);
221
222 util_copy_framebuffer_state(&ctx->framebuffer, NULL);
223 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */
224 fd_bc_invalidate_context(ctx);
225
226 fd_prog_fini(pctx);
227
228 if (ctx->blitter)
229 util_blitter_destroy(ctx->blitter);
230
231 if (pctx->stream_uploader)
232 u_upload_destroy(pctx->stream_uploader);
233
234 if (ctx->clear_rs_state)
235 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state);
236
237 if (ctx->primconvert)
238 util_primconvert_destroy(ctx->primconvert);
239
240 slab_destroy_child(&ctx->transfer_pool);
241
242 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe_bo); i++) {
243 if (!ctx->vsc_pipe_bo[i])
244 break;
245 fd_bo_del(ctx->vsc_pipe_bo[i]);
246 }
247
248 fd_device_del(ctx->dev);
249 fd_pipe_del(ctx->pipe);
250
251 mtx_destroy(&ctx->gmem_lock);
252
253 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) {
254 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n",
255 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem,
256 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw,
257 (uint32_t)ctx->stats.batch_restore);
258 }
259 }
260
261 static void
262 fd_set_debug_callback(struct pipe_context *pctx,
263 const struct pipe_debug_callback *cb)
264 {
265 struct fd_context *ctx = fd_context(pctx);
266
267 if (cb)
268 ctx->debug = *cb;
269 else
270 memset(&ctx->debug, 0, sizeof(ctx->debug));
271 }
272
273 static uint32_t
274 fd_get_reset_count(struct fd_context *ctx, bool per_context)
275 {
276 uint64_t val;
277 enum fd_param_id param =
278 per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS;
279 int ret = fd_pipe_get_param(ctx->pipe, param, &val);
280 debug_assert(!ret);
281 return val;
282 }
283
284 static enum pipe_reset_status
285 fd_get_device_reset_status(struct pipe_context *pctx)
286 {
287 struct fd_context *ctx = fd_context(pctx);
288 int context_faults = fd_get_reset_count(ctx, true);
289 int global_faults = fd_get_reset_count(ctx, false);
290 enum pipe_reset_status status;
291
292 if (context_faults != ctx->context_reset_count) {
293 status = PIPE_GUILTY_CONTEXT_RESET;
294 } else if (global_faults != ctx->global_reset_count) {
295 status = PIPE_INNOCENT_CONTEXT_RESET;
296 } else {
297 status = PIPE_NO_RESET;
298 }
299
300 ctx->context_reset_count = context_faults;
301 ctx->global_reset_count = global_faults;
302
303 return status;
304 }
305
306 /* TODO we could combine a few of these small buffers (solid_vbuf,
307 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and
308 * save a tiny bit of memory
309 */
310
311 static struct pipe_resource *
312 create_solid_vertexbuf(struct pipe_context *pctx)
313 {
314 static const float init_shader_const[] = {
315 -1.000000, +1.000000, +1.000000,
316 +1.000000, -1.000000, +1.000000,
317 };
318 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
319 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const));
320 pipe_buffer_write(pctx, prsc, 0,
321 sizeof(init_shader_const), init_shader_const);
322 return prsc;
323 }
324
325 static struct pipe_resource *
326 create_blit_texcoord_vertexbuf(struct pipe_context *pctx)
327 {
328 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen,
329 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16);
330 return prsc;
331 }
332
333 void
334 fd_context_setup_common_vbos(struct fd_context *ctx)
335 {
336 struct pipe_context *pctx = &ctx->base;
337
338 ctx->solid_vbuf = create_solid_vertexbuf(pctx);
339 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx);
340
341 /* setup solid_vbuf_state: */
342 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state(
343 pctx, 1, (struct pipe_vertex_element[]){{
344 .vertex_buffer_index = 0,
345 .src_offset = 0,
346 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
347 }});
348 ctx->solid_vbuf_state.vertexbuf.count = 1;
349 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12;
350 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf;
351
352 /* setup blit_vbuf_state: */
353 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state(
354 pctx, 2, (struct pipe_vertex_element[]){{
355 .vertex_buffer_index = 0,
356 .src_offset = 0,
357 .src_format = PIPE_FORMAT_R32G32_FLOAT,
358 }, {
359 .vertex_buffer_index = 1,
360 .src_offset = 0,
361 .src_format = PIPE_FORMAT_R32G32B32_FLOAT,
362 }});
363 ctx->blit_vbuf_state.vertexbuf.count = 2;
364 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8;
365 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf;
366 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12;
367 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf;
368 }
369
370 void
371 fd_context_cleanup_common_vbos(struct fd_context *ctx)
372 {
373 struct pipe_context *pctx = &ctx->base;
374
375 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx);
376 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx);
377
378 pipe_resource_reference(&ctx->solid_vbuf, NULL);
379 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL);
380 }
381
382 struct pipe_context *
383 fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
384 const uint8_t *primtypes, void *priv, unsigned flags)
385 {
386 struct fd_screen *screen = fd_screen(pscreen);
387 struct pipe_context *pctx;
388 unsigned prio = 1;
389 int i;
390
391 /* lower numerical value == higher priority: */
392 if (fd_mesa_debug & FD_DBG_HIPRIO)
393 prio = 0;
394 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY)
395 prio = 0;
396 else if (flags & PIPE_CONTEXT_LOW_PRIORITY)
397 prio = 2;
398
399 ctx->screen = screen;
400 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio);
401
402 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) {
403 ctx->context_reset_count = fd_get_reset_count(ctx, true);
404 ctx->global_reset_count = fd_get_reset_count(ctx, false);
405 }
406
407 ctx->primtypes = primtypes;
408 ctx->primtype_mask = 0;
409 for (i = 0; i < PIPE_PRIM_MAX; i++)
410 if (primtypes[i])
411 ctx->primtype_mask |= (1 << i);
412
413 (void) mtx_init(&ctx->gmem_lock, mtx_plain);
414
415 /* need some sane default in case gallium frontends don't
416 * set some state:
417 */
418 ctx->sample_mask = 0xffff;
419 ctx->active_queries = true;
420
421 pctx = &ctx->base;
422 pctx->screen = pscreen;
423 pctx->priv = priv;
424 pctx->flush = fd_context_flush;
425 pctx->emit_string_marker = fd_emit_string_marker;
426 pctx->set_debug_callback = fd_set_debug_callback;
427 pctx->get_device_reset_status = fd_get_device_reset_status;
428 pctx->create_fence_fd = fd_create_fence_fd;
429 pctx->fence_server_sync = fd_fence_server_sync;
430 pctx->texture_barrier = fd_texture_barrier;
431 pctx->memory_barrier = fd_memory_barrier;
432
433 pctx->stream_uploader = u_upload_create_default(pctx);
434 if (!pctx->stream_uploader)
435 goto fail;
436 pctx->const_uploader = pctx->stream_uploader;
437
438 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool);
439
440 fd_draw_init(pctx);
441 fd_resource_context_init(pctx);
442 fd_query_context_init(pctx);
443 fd_texture_init(pctx);
444 fd_state_init(pctx);
445
446 ctx->blitter = util_blitter_create(pctx);
447 if (!ctx->blitter)
448 goto fail;
449
450 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask);
451 if (!ctx->primconvert)
452 goto fail;
453
454 list_inithead(&ctx->hw_active_queries);
455 list_inithead(&ctx->acc_active_queries);
456 list_inithead(&ctx->log_chunks);
457
458 fd_screen_lock(ctx->screen);
459 list_add(&ctx->node, &ctx->screen->context_list);
460 fd_screen_unlock(ctx->screen);
461
462 ctx->current_scissor = &ctx->disabled_scissor;
463
464 ctx->log_out = stdout;
465
466 if ((fd_mesa_debug & FD_DBG_LOG) &&
467 !(ctx->record_timestamp && ctx->ts_to_ns)) {
468 printf("logging not supported!\n");
469 fd_mesa_debug &= ~FD_DBG_LOG;
470 }
471
472 #if DETECT_OS_ANDROID
473 if (fd_mesa_debug & FD_DBG_LOG) {
474 static unsigned idx = 0;
475 char *p;
476 asprintf(&p, "/data/fdlog/%s-%d.log", util_get_process_name(), idx++);
477
478 FILE *f = fopen(p, "w");
479 if (f)
480 ctx->log_out = f;
481 }
482 #endif
483
484 return pctx;
485
486 fail:
487 pctx->destroy(pctx);
488 return NULL;
489 }