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_program.h" 32#include "freedreno_resource.h" 33#include "freedreno_texture.h" 34#include "freedreno_state.h" 35#include "freedreno_gmem.h" 36#include "freedreno_query.h" 37#include "freedreno_query_hw.h" 38#include "freedreno_util.h" 39#include "util/u_upload_mgr.h" 40 41static void 42fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fencep, 43 unsigned flags) 44{ 45 struct fd_context *ctx = fd_context(pctx); 46 struct pipe_fence_handle *fence = NULL; 47 // TODO we want to lookup batch if it exists, but not create one if not. 48 struct fd_batch *batch = fd_context_batch(ctx); 49 50 DBG("%p: flush: flags=%x\n", ctx->batch, flags); 51 52 /* if no rendering since last flush, ie. app just decided it needed 53 * a fence, re-use the last one: 54 */ 55 if (ctx->last_fence) { 56 fd_fence_ref(pctx->screen, &fence, ctx->last_fence); 57 goto out; 58 } 59 60 if (!batch) 61 return; 62 63 /* Take a ref to the batch's fence (batch can be unref'd when flushed: */ 64 fd_fence_ref(pctx->screen, &fence, batch->fence); 65 66 /* TODO is it worth trying to figure out if app is using fence-fd's, to 67 * avoid requesting one every batch? 68 */ 69 batch->needs_out_fence_fd = true; 70 71 if (!ctx->screen->reorder) { 72 fd_batch_flush(batch, true, false); 73 } else if (flags & PIPE_FLUSH_DEFERRED) { 74 fd_bc_flush_deferred(&ctx->screen->batch_cache, ctx); 75 } else { 76 fd_bc_flush(&ctx->screen->batch_cache, ctx); 77 } 78 79out: 80 if (fencep) 81 fd_fence_ref(pctx->screen, fencep, fence); 82 83 fd_fence_ref(pctx->screen, &ctx->last_fence, fence); 84 85 fd_fence_ref(pctx->screen, &fence, NULL); 86} 87 88static void 89fd_texture_barrier(struct pipe_context *pctx, unsigned flags) 90{ 91 if (flags == PIPE_TEXTURE_BARRIER_FRAMEBUFFER) { 92 struct fd_context *ctx = fd_context(pctx); 93 94 if (ctx->framebuffer_barrier) { 95 ctx->framebuffer_barrier(ctx); 96 return; 97 } 98 } 99 100 /* On devices that could sample from GMEM we could possibly do better. 101 * Or if we knew that we were doing GMEM bypass we could just emit a 102 * cache flush, perhaps? But we don't know if future draws would cause 103 * us to use GMEM, and a flush in bypass isn't the end of the world. 104 */ 105 fd_context_flush(pctx, NULL, 0); 106} 107 108static void 109fd_memory_barrier(struct pipe_context *pctx, unsigned flags) 110{ 111 if (!(flags & ~PIPE_BARRIER_UPDATE)) 112 return; 113 114 fd_context_flush(pctx, NULL, 0); 115 /* TODO do we need to check for persistently mapped buffers and fd_bo_cpu_prep()?? */ 116} 117 118/** 119 * emit marker string as payload of a no-op packet, which can be 120 * decoded by cffdump. 121 */ 122static void 123fd_emit_string_marker(struct pipe_context *pctx, const char *string, int len) 124{ 125 struct fd_context *ctx = fd_context(pctx); 126 struct fd_ringbuffer *ring; 127 const uint32_t *buf = (const void *)string; 128 129 if (!ctx->batch) 130 return; 131 132 ctx->batch->needs_flush = true; 133 134 ring = ctx->batch->draw; 135 136 /* max packet size is 0x3fff dwords: */ 137 len = MIN2(len, 0x3fff * 4); 138 139 if (ctx->screen->gpu_id >= 500) 140 OUT_PKT7(ring, CP_NOP, align(len, 4) / 4); 141 else 142 OUT_PKT3(ring, CP_NOP, align(len, 4) / 4); 143 while (len >= 4) { 144 OUT_RING(ring, *buf); 145 buf++; 146 len -= 4; 147 } 148 149 /* copy remainder bytes without reading past end of input string: */ 150 if (len > 0) { 151 uint32_t w = 0; 152 memcpy(&w, buf, len); 153 OUT_RING(ring, w); 154 } 155} 156 157void 158fd_context_destroy(struct pipe_context *pctx) 159{ 160 struct fd_context *ctx = fd_context(pctx); 161 unsigned i; 162 163 DBG(""); 164 165 fd_fence_ref(pctx->screen, &ctx->last_fence, NULL); 166 167 if (ctx->screen->reorder && util_queue_is_initialized(&ctx->flush_queue)) 168 util_queue_destroy(&ctx->flush_queue); 169 170 util_copy_framebuffer_state(&ctx->framebuffer, NULL); 171 fd_batch_reference(&ctx->batch, NULL); /* unref current batch */ 172 fd_bc_invalidate_context(ctx); 173 174 fd_prog_fini(pctx); 175 176 if (ctx->blitter) 177 util_blitter_destroy(ctx->blitter); 178 179 if (pctx->stream_uploader) 180 u_upload_destroy(pctx->stream_uploader); 181 182 if (ctx->clear_rs_state) 183 pctx->delete_rasterizer_state(pctx, ctx->clear_rs_state); 184 185 if (ctx->primconvert) 186 util_primconvert_destroy(ctx->primconvert); 187 188 slab_destroy_child(&ctx->transfer_pool); 189 190 for (i = 0; i < ARRAY_SIZE(ctx->vsc_pipe); i++) { 191 struct fd_vsc_pipe *pipe = &ctx->vsc_pipe[i]; 192 if (!pipe->bo) 193 break; 194 fd_bo_del(pipe->bo); 195 } 196 197 fd_device_del(ctx->dev); 198 fd_pipe_del(ctx->pipe); 199 200 if (fd_mesa_debug & (FD_DBG_BSTAT | FD_DBG_MSGS)) { 201 printf("batch_total=%u, batch_sysmem=%u, batch_gmem=%u, batch_nondraw=%u, batch_restore=%u\n", 202 (uint32_t)ctx->stats.batch_total, (uint32_t)ctx->stats.batch_sysmem, 203 (uint32_t)ctx->stats.batch_gmem, (uint32_t)ctx->stats.batch_nondraw, 204 (uint32_t)ctx->stats.batch_restore); 205 } 206} 207 208static void 209fd_set_debug_callback(struct pipe_context *pctx, 210 const struct pipe_debug_callback *cb) 211{ 212 struct fd_context *ctx = fd_context(pctx); 213 214 if (cb) 215 ctx->debug = *cb; 216 else 217 memset(&ctx->debug, 0, sizeof(ctx->debug)); 218} 219 220static uint32_t 221fd_get_reset_count(struct fd_context *ctx, bool per_context) 222{ 223 uint64_t val; 224 enum fd_param_id param = 225 per_context ? FD_CTX_FAULTS : FD_GLOBAL_FAULTS; 226 int ret = fd_pipe_get_param(ctx->pipe, param, &val); 227 debug_assert(!ret); 228 return val; 229} 230 231static enum pipe_reset_status 232fd_get_device_reset_status(struct pipe_context *pctx) 233{ 234 struct fd_context *ctx = fd_context(pctx); 235 int context_faults = fd_get_reset_count(ctx, true); 236 int global_faults = fd_get_reset_count(ctx, false); 237 enum pipe_reset_status status; 238 239 if (context_faults != ctx->context_reset_count) { 240 status = PIPE_GUILTY_CONTEXT_RESET; 241 } else if (global_faults != ctx->global_reset_count) { 242 status = PIPE_INNOCENT_CONTEXT_RESET; 243 } else { 244 status = PIPE_NO_RESET; 245 } 246 247 ctx->context_reset_count = context_faults; 248 ctx->global_reset_count = global_faults; 249 250 return status; 251} 252 253/* TODO we could combine a few of these small buffers (solid_vbuf, 254 * blit_texcoord_vbuf, and vsc_size_mem, into a single buffer and 255 * save a tiny bit of memory 256 */ 257 258static struct pipe_resource * 259create_solid_vertexbuf(struct pipe_context *pctx) 260{ 261 static const float init_shader_const[] = { 262 -1.000000, +1.000000, +1.000000, 263 +1.000000, -1.000000, +1.000000, 264 }; 265 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen, 266 PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE, sizeof(init_shader_const)); 267 pipe_buffer_write(pctx, prsc, 0, 268 sizeof(init_shader_const), init_shader_const); 269 return prsc; 270} 271 272static struct pipe_resource * 273create_blit_texcoord_vertexbuf(struct pipe_context *pctx) 274{ 275 struct pipe_resource *prsc = pipe_buffer_create(pctx->screen, 276 PIPE_BIND_CUSTOM, PIPE_USAGE_DYNAMIC, 16); 277 return prsc; 278} 279 280void 281fd_context_setup_common_vbos(struct fd_context *ctx) 282{ 283 struct pipe_context *pctx = &ctx->base; 284 285 ctx->solid_vbuf = create_solid_vertexbuf(pctx); 286 ctx->blit_texcoord_vbuf = create_blit_texcoord_vertexbuf(pctx); 287 288 /* setup solid_vbuf_state: */ 289 ctx->solid_vbuf_state.vtx = pctx->create_vertex_elements_state( 290 pctx, 1, (struct pipe_vertex_element[]){{ 291 .vertex_buffer_index = 0, 292 .src_offset = 0, 293 .src_format = PIPE_FORMAT_R32G32B32_FLOAT, 294 }}); 295 ctx->solid_vbuf_state.vertexbuf.count = 1; 296 ctx->solid_vbuf_state.vertexbuf.vb[0].stride = 12; 297 ctx->solid_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->solid_vbuf; 298 299 /* setup blit_vbuf_state: */ 300 ctx->blit_vbuf_state.vtx = pctx->create_vertex_elements_state( 301 pctx, 2, (struct pipe_vertex_element[]){{ 302 .vertex_buffer_index = 0, 303 .src_offset = 0, 304 .src_format = PIPE_FORMAT_R32G32_FLOAT, 305 }, { 306 .vertex_buffer_index = 1, 307 .src_offset = 0, 308 .src_format = PIPE_FORMAT_R32G32B32_FLOAT, 309 }}); 310 ctx->blit_vbuf_state.vertexbuf.count = 2; 311 ctx->blit_vbuf_state.vertexbuf.vb[0].stride = 8; 312 ctx->blit_vbuf_state.vertexbuf.vb[0].buffer.resource = ctx->blit_texcoord_vbuf; 313 ctx->blit_vbuf_state.vertexbuf.vb[1].stride = 12; 314 ctx->blit_vbuf_state.vertexbuf.vb[1].buffer.resource = ctx->solid_vbuf; 315} 316 317void 318fd_context_cleanup_common_vbos(struct fd_context *ctx) 319{ 320 struct pipe_context *pctx = &ctx->base; 321 322 pctx->delete_vertex_elements_state(pctx, ctx->solid_vbuf_state.vtx); 323 pctx->delete_vertex_elements_state(pctx, ctx->blit_vbuf_state.vtx); 324 325 pipe_resource_reference(&ctx->solid_vbuf, NULL); 326 pipe_resource_reference(&ctx->blit_texcoord_vbuf, NULL); 327} 328 329struct pipe_context * 330fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen, 331 const uint8_t *primtypes, void *priv, unsigned flags) 332{ 333 struct fd_screen *screen = fd_screen(pscreen); 334 struct pipe_context *pctx; 335 unsigned prio = 1; 336 int i; 337 338 /* lower numerical value == higher priority: */ 339 if (fd_mesa_debug & FD_DBG_HIPRIO) 340 prio = 0; 341 else if (flags & PIPE_CONTEXT_HIGH_PRIORITY) 342 prio = 0; 343 else if (flags & PIPE_CONTEXT_LOW_PRIORITY) 344 prio = 2; 345 346 ctx->screen = screen; 347 ctx->pipe = fd_pipe_new2(screen->dev, FD_PIPE_3D, prio); 348 349 if (fd_device_version(screen->dev) >= FD_VERSION_ROBUSTNESS) { 350 ctx->context_reset_count = fd_get_reset_count(ctx, true); 351 ctx->global_reset_count = fd_get_reset_count(ctx, false); 352 } 353 354 ctx->primtypes = primtypes; 355 ctx->primtype_mask = 0; 356 for (i = 0; i < PIPE_PRIM_MAX; i++) 357 if (primtypes[i]) 358 ctx->primtype_mask |= (1 << i); 359 360 /* need some sane default in case state tracker doesn't 361 * set some state: 362 */ 363 ctx->sample_mask = 0xffff; 364 365 pctx = &ctx->base; 366 pctx->screen = pscreen; 367 pctx->priv = priv; 368 pctx->flush = fd_context_flush; 369 pctx->emit_string_marker = fd_emit_string_marker; 370 pctx->set_debug_callback = fd_set_debug_callback; 371 pctx->get_device_reset_status = fd_get_device_reset_status; 372 pctx->create_fence_fd = fd_create_fence_fd; 373 pctx->fence_server_sync = fd_fence_server_sync; 374 pctx->texture_barrier = fd_texture_barrier; 375 pctx->memory_barrier = fd_memory_barrier; 376 377 pctx->stream_uploader = u_upload_create_default(pctx); 378 if (!pctx->stream_uploader) 379 goto fail; 380 pctx->const_uploader = pctx->stream_uploader; 381 382 if (!ctx->screen->reorder) 383 ctx->batch = fd_bc_alloc_batch(&screen->batch_cache, ctx, false); 384 385 slab_create_child(&ctx->transfer_pool, &screen->transfer_pool); 386 387 fd_draw_init(pctx); 388 fd_resource_context_init(pctx); 389 fd_query_context_init(pctx); 390 fd_texture_init(pctx); 391 fd_state_init(pctx); 392 393 ctx->blitter = util_blitter_create(pctx); 394 if (!ctx->blitter) 395 goto fail; 396 397 ctx->primconvert = util_primconvert_create(pctx, ctx->primtype_mask); 398 if (!ctx->primconvert) 399 goto fail; 400 401 list_inithead(&ctx->hw_active_queries); 402 list_inithead(&ctx->acc_active_queries); 403 404 return pctx; 405 406fail: 407 pctx->destroy(pctx); 408 return NULL; 409} 410