1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci */ 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_ci#include <stdio.h> 24bf215546Sopenharmony_ci#include <time.h> 25bf215546Sopenharmony_ci#include "pipe/p_defines.h" 26bf215546Sopenharmony_ci#include "pipe/p_state.h" 27bf215546Sopenharmony_ci#include "util/ralloc.h" 28bf215546Sopenharmony_ci#include "util/u_inlines.h" 29bf215546Sopenharmony_ci#include "util/format/u_format.h" 30bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 31bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h" 32bf215546Sopenharmony_ci#include "crocus_context.h" 33bf215546Sopenharmony_ci#include "crocus_resource.h" 34bf215546Sopenharmony_ci#include "crocus_screen.h" 35bf215546Sopenharmony_ci#include "common/intel_defines.h" 36bf215546Sopenharmony_ci#include "common/intel_sample_positions.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci/** 39bf215546Sopenharmony_ci * The pipe->set_debug_callback() driver hook. 40bf215546Sopenharmony_ci */ 41bf215546Sopenharmony_cistatic void 42bf215546Sopenharmony_cicrocus_set_debug_callback(struct pipe_context *ctx, 43bf215546Sopenharmony_ci const struct util_debug_callback *cb) 44bf215546Sopenharmony_ci{ 45bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci if (cb) 48bf215546Sopenharmony_ci ice->dbg = *cb; 49bf215546Sopenharmony_ci else 50bf215546Sopenharmony_ci memset(&ice->dbg, 0, sizeof(ice->dbg)); 51bf215546Sopenharmony_ci} 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_cistatic bool 54bf215546Sopenharmony_cicrocus_init_identifier_bo(struct crocus_context *ice) 55bf215546Sopenharmony_ci{ 56bf215546Sopenharmony_ci void *bo_map; 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci bo_map = crocus_bo_map(NULL, ice->workaround_bo, MAP_READ | MAP_WRITE); 59bf215546Sopenharmony_ci if (!bo_map) 60bf215546Sopenharmony_ci return false; 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_ci ice->workaround_bo->kflags |= EXEC_OBJECT_CAPTURE; 63bf215546Sopenharmony_ci ice->workaround_offset = ALIGN( 64bf215546Sopenharmony_ci intel_debug_write_identifiers(bo_map, 4096, "Crocus") + 8, 8); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci crocus_bo_unmap(ice->workaround_bo); 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci return true; 69bf215546Sopenharmony_ci} 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci/** 72bf215546Sopenharmony_ci * Called from the batch module when it detects a GPU hang. 73bf215546Sopenharmony_ci * 74bf215546Sopenharmony_ci * In this case, we've lost our GEM context, and can't rely on any existing 75bf215546Sopenharmony_ci * state on the GPU. We must mark everything dirty and wipe away any saved 76bf215546Sopenharmony_ci * assumptions about the last known state of the GPU. 77bf215546Sopenharmony_ci */ 78bf215546Sopenharmony_civoid 79bf215546Sopenharmony_cicrocus_lost_context_state(struct crocus_batch *batch) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci /* The batch module doesn't have an crocus_context, because we want to 82bf215546Sopenharmony_ci * avoid introducing lots of layering violations. Unfortunately, here 83bf215546Sopenharmony_ci * we do need to inform the context of batch catastrophe. We know the 84bf215546Sopenharmony_ci * batch is one of our context's, so hackily claw our way back. 85bf215546Sopenharmony_ci */ 86bf215546Sopenharmony_ci struct crocus_context *ice = batch->ice; 87bf215546Sopenharmony_ci struct crocus_screen *screen = batch->screen; 88bf215546Sopenharmony_ci if (batch->name == CROCUS_BATCH_RENDER) { 89bf215546Sopenharmony_ci screen->vtbl.init_render_context(batch); 90bf215546Sopenharmony_ci } else if (batch->name == CROCUS_BATCH_COMPUTE) { 91bf215546Sopenharmony_ci screen->vtbl.init_compute_context(batch); 92bf215546Sopenharmony_ci } else { 93bf215546Sopenharmony_ci unreachable("unhandled batch reset"); 94bf215546Sopenharmony_ci } 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci ice->state.dirty = ~0ull; 97bf215546Sopenharmony_ci memset(ice->state.last_grid, 0, sizeof(ice->state.last_grid)); 98bf215546Sopenharmony_ci batch->state_base_address_emitted = false; 99bf215546Sopenharmony_ci screen->vtbl.lost_genx_state(ice, batch); 100bf215546Sopenharmony_ci} 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_cistatic enum pipe_reset_status 103bf215546Sopenharmony_cicrocus_get_device_reset_status(struct pipe_context *ctx) 104bf215546Sopenharmony_ci{ 105bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci enum pipe_reset_status worst_reset = PIPE_NO_RESET; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci /* Check the reset status of each batch's hardware context, and take the 110bf215546Sopenharmony_ci * worst status (if one was guilty, proclaim guilt). 111bf215546Sopenharmony_ci */ 112bf215546Sopenharmony_ci for (int i = 0; i < ice->batch_count; i++) { 113bf215546Sopenharmony_ci /* This will also recreate the hardware contexts as necessary, so any 114bf215546Sopenharmony_ci * future queries will show no resets. We only want to report once. 115bf215546Sopenharmony_ci */ 116bf215546Sopenharmony_ci enum pipe_reset_status batch_reset = 117bf215546Sopenharmony_ci crocus_batch_check_for_reset(&ice->batches[i]); 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci if (batch_reset == PIPE_NO_RESET) 120bf215546Sopenharmony_ci continue; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci if (worst_reset == PIPE_NO_RESET) { 123bf215546Sopenharmony_ci worst_reset = batch_reset; 124bf215546Sopenharmony_ci } else { 125bf215546Sopenharmony_ci /* GUILTY < INNOCENT < UNKNOWN */ 126bf215546Sopenharmony_ci worst_reset = MIN2(worst_reset, batch_reset); 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci } 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci if (worst_reset != PIPE_NO_RESET && ice->reset.reset) 131bf215546Sopenharmony_ci ice->reset.reset(ice->reset.data, worst_reset); 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci return worst_reset; 134bf215546Sopenharmony_ci} 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_cistatic void 137bf215546Sopenharmony_cicrocus_set_device_reset_callback(struct pipe_context *ctx, 138bf215546Sopenharmony_ci const struct pipe_device_reset_callback *cb) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci if (cb) 143bf215546Sopenharmony_ci ice->reset = *cb; 144bf215546Sopenharmony_ci else 145bf215546Sopenharmony_ci memset(&ice->reset, 0, sizeof(ice->reset)); 146bf215546Sopenharmony_ci} 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_cistatic void 149bf215546Sopenharmony_cicrocus_get_sample_position(struct pipe_context *ctx, 150bf215546Sopenharmony_ci unsigned sample_count, 151bf215546Sopenharmony_ci unsigned sample_index, 152bf215546Sopenharmony_ci float *out_value) 153bf215546Sopenharmony_ci{ 154bf215546Sopenharmony_ci union { 155bf215546Sopenharmony_ci struct { 156bf215546Sopenharmony_ci float x[16]; 157bf215546Sopenharmony_ci float y[16]; 158bf215546Sopenharmony_ci } a; 159bf215546Sopenharmony_ci struct { 160bf215546Sopenharmony_ci float _0XOffset, _1XOffset, _2XOffset, _3XOffset, 161bf215546Sopenharmony_ci _4XOffset, _5XOffset, _6XOffset, _7XOffset, 162bf215546Sopenharmony_ci _8XOffset, _9XOffset, _10XOffset, _11XOffset, 163bf215546Sopenharmony_ci _12XOffset, _13XOffset, _14XOffset, _15XOffset; 164bf215546Sopenharmony_ci float _0YOffset, _1YOffset, _2YOffset, _3YOffset, 165bf215546Sopenharmony_ci _4YOffset, _5YOffset, _6YOffset, _7YOffset, 166bf215546Sopenharmony_ci _8YOffset, _9YOffset, _10YOffset, _11YOffset, 167bf215546Sopenharmony_ci _12YOffset, _13YOffset, _14YOffset, _15YOffset; 168bf215546Sopenharmony_ci } v; 169bf215546Sopenharmony_ci } u; 170bf215546Sopenharmony_ci switch (sample_count) { 171bf215546Sopenharmony_ci case 1: INTEL_SAMPLE_POS_1X(u.v._); break; 172bf215546Sopenharmony_ci case 2: INTEL_SAMPLE_POS_2X(u.v._); break; 173bf215546Sopenharmony_ci case 4: INTEL_SAMPLE_POS_4X(u.v._); break; 174bf215546Sopenharmony_ci case 8: INTEL_SAMPLE_POS_8X(u.v._); break; 175bf215546Sopenharmony_ci case 16: INTEL_SAMPLE_POS_16X(u.v._); break; 176bf215546Sopenharmony_ci default: unreachable("invalid sample count"); 177bf215546Sopenharmony_ci } 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci out_value[0] = u.a.x[sample_index]; 180bf215546Sopenharmony_ci out_value[1] = u.a.y[sample_index]; 181bf215546Sopenharmony_ci} 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_ci/** 184bf215546Sopenharmony_ci * Destroy a context, freeing any associated memory. 185bf215546Sopenharmony_ci */ 186bf215546Sopenharmony_cistatic void 187bf215546Sopenharmony_cicrocus_destroy_context(struct pipe_context *ctx) 188bf215546Sopenharmony_ci{ 189bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 190bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)ctx->screen; 191bf215546Sopenharmony_ci if (ctx->stream_uploader) 192bf215546Sopenharmony_ci u_upload_destroy(ctx->stream_uploader); 193bf215546Sopenharmony_ci 194bf215546Sopenharmony_ci if (ice->blitter) 195bf215546Sopenharmony_ci util_blitter_destroy(ice->blitter); 196bf215546Sopenharmony_ci screen->vtbl.destroy_state(ice); 197bf215546Sopenharmony_ci crocus_destroy_program_cache(ice); 198bf215546Sopenharmony_ci u_upload_destroy(ice->query_buffer_uploader); 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci crocus_bo_unreference(ice->workaround_bo); 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci slab_destroy_child(&ice->transfer_pool); 203bf215546Sopenharmony_ci slab_destroy_child(&ice->transfer_pool_unsync); 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci crocus_batch_free(&ice->batches[CROCUS_BATCH_RENDER]); 206bf215546Sopenharmony_ci if (ice->batches[CROCUS_BATCH_COMPUTE].ice) 207bf215546Sopenharmony_ci crocus_batch_free(&ice->batches[CROCUS_BATCH_COMPUTE]); 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci ralloc_free(ice); 210bf215546Sopenharmony_ci} 211bf215546Sopenharmony_ci 212bf215546Sopenharmony_ci#define genX_call(devinfo, func, ...) \ 213bf215546Sopenharmony_ci switch ((devinfo)->verx10) { \ 214bf215546Sopenharmony_ci case 80: \ 215bf215546Sopenharmony_ci gfx8_##func(__VA_ARGS__); \ 216bf215546Sopenharmony_ci break; \ 217bf215546Sopenharmony_ci case 75: \ 218bf215546Sopenharmony_ci gfx75_##func(__VA_ARGS__); \ 219bf215546Sopenharmony_ci break; \ 220bf215546Sopenharmony_ci case 70: \ 221bf215546Sopenharmony_ci gfx7_##func(__VA_ARGS__); \ 222bf215546Sopenharmony_ci break; \ 223bf215546Sopenharmony_ci case 60: \ 224bf215546Sopenharmony_ci gfx6_##func(__VA_ARGS__); \ 225bf215546Sopenharmony_ci break; \ 226bf215546Sopenharmony_ci case 50: \ 227bf215546Sopenharmony_ci gfx5_##func(__VA_ARGS__); \ 228bf215546Sopenharmony_ci break; \ 229bf215546Sopenharmony_ci case 45: \ 230bf215546Sopenharmony_ci gfx45_##func(__VA_ARGS__); \ 231bf215546Sopenharmony_ci break; \ 232bf215546Sopenharmony_ci case 40: \ 233bf215546Sopenharmony_ci gfx4_##func(__VA_ARGS__); \ 234bf215546Sopenharmony_ci break; \ 235bf215546Sopenharmony_ci default: \ 236bf215546Sopenharmony_ci unreachable("Unknown hardware generation"); \ 237bf215546Sopenharmony_ci } 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci/** 240bf215546Sopenharmony_ci * Create a context. 241bf215546Sopenharmony_ci * 242bf215546Sopenharmony_ci * This is where each context begins. 243bf215546Sopenharmony_ci */ 244bf215546Sopenharmony_cistruct pipe_context * 245bf215546Sopenharmony_cicrocus_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags) 246bf215546Sopenharmony_ci{ 247bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen*)pscreen; 248bf215546Sopenharmony_ci const struct intel_device_info *devinfo = &screen->devinfo; 249bf215546Sopenharmony_ci struct crocus_context *ice = rzalloc(NULL, struct crocus_context); 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_ci if (!ice) 252bf215546Sopenharmony_ci return NULL; 253bf215546Sopenharmony_ci 254bf215546Sopenharmony_ci struct pipe_context *ctx = &ice->ctx; 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci ctx->screen = pscreen; 257bf215546Sopenharmony_ci ctx->priv = priv; 258bf215546Sopenharmony_ci 259bf215546Sopenharmony_ci ctx->stream_uploader = u_upload_create_default(ctx); 260bf215546Sopenharmony_ci if (!ctx->stream_uploader) { 261bf215546Sopenharmony_ci free(ctx); 262bf215546Sopenharmony_ci return NULL; 263bf215546Sopenharmony_ci } 264bf215546Sopenharmony_ci ctx->const_uploader = ctx->stream_uploader; 265bf215546Sopenharmony_ci 266bf215546Sopenharmony_ci ctx->destroy = crocus_destroy_context; 267bf215546Sopenharmony_ci ctx->set_debug_callback = crocus_set_debug_callback; 268bf215546Sopenharmony_ci ctx->set_device_reset_callback = crocus_set_device_reset_callback; 269bf215546Sopenharmony_ci ctx->get_device_reset_status = crocus_get_device_reset_status; 270bf215546Sopenharmony_ci ctx->get_sample_position = crocus_get_sample_position; 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci ice->shaders.urb_size = devinfo->urb.size; 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ci crocus_init_context_fence_functions(ctx); 275bf215546Sopenharmony_ci crocus_init_blit_functions(ctx); 276bf215546Sopenharmony_ci crocus_init_clear_functions(ctx); 277bf215546Sopenharmony_ci crocus_init_program_functions(ctx); 278bf215546Sopenharmony_ci crocus_init_resource_functions(ctx); 279bf215546Sopenharmony_ci crocus_init_flush_functions(ctx); 280bf215546Sopenharmony_ci 281bf215546Sopenharmony_ci crocus_init_program_cache(ice); 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci slab_create_child(&ice->transfer_pool, &screen->transfer_pool); 284bf215546Sopenharmony_ci slab_create_child(&ice->transfer_pool_unsync, &screen->transfer_pool); 285bf215546Sopenharmony_ci 286bf215546Sopenharmony_ci ice->query_buffer_uploader = 287bf215546Sopenharmony_ci u_upload_create(ctx, 4096, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING, 288bf215546Sopenharmony_ci 0); 289bf215546Sopenharmony_ci 290bf215546Sopenharmony_ci ice->workaround_bo = 291bf215546Sopenharmony_ci crocus_bo_alloc(screen->bufmgr, "workaround", 4096); 292bf215546Sopenharmony_ci if (!ice->workaround_bo) 293bf215546Sopenharmony_ci return NULL; 294bf215546Sopenharmony_ci 295bf215546Sopenharmony_ci if (!crocus_init_identifier_bo(ice)) 296bf215546Sopenharmony_ci return NULL; 297bf215546Sopenharmony_ci 298bf215546Sopenharmony_ci genX_call(devinfo, crocus_init_state, ice); 299bf215546Sopenharmony_ci genX_call(devinfo, crocus_init_blorp, ice); 300bf215546Sopenharmony_ci genX_call(devinfo, crocus_init_query, ice); 301bf215546Sopenharmony_ci 302bf215546Sopenharmony_ci ice->blitter = util_blitter_create(&ice->ctx); 303bf215546Sopenharmony_ci if (ice->blitter == NULL) 304bf215546Sopenharmony_ci return NULL; 305bf215546Sopenharmony_ci int priority = 0; 306bf215546Sopenharmony_ci if (flags & PIPE_CONTEXT_HIGH_PRIORITY) 307bf215546Sopenharmony_ci priority = INTEL_CONTEXT_HIGH_PRIORITY; 308bf215546Sopenharmony_ci if (flags & PIPE_CONTEXT_LOW_PRIORITY) 309bf215546Sopenharmony_ci priority = INTEL_CONTEXT_LOW_PRIORITY; 310bf215546Sopenharmony_ci 311bf215546Sopenharmony_ci ice->batch_count = devinfo->ver >= 7 ? CROCUS_BATCH_COUNT : 1; 312bf215546Sopenharmony_ci for (int i = 0; i < ice->batch_count; i++) { 313bf215546Sopenharmony_ci crocus_init_batch(ice, (enum crocus_batch_name) i, 314bf215546Sopenharmony_ci priority); 315bf215546Sopenharmony_ci } 316bf215546Sopenharmony_ci 317bf215546Sopenharmony_ci ice->urb.size = devinfo->urb.size; 318bf215546Sopenharmony_ci screen->vtbl.init_render_context(&ice->batches[CROCUS_BATCH_RENDER]); 319bf215546Sopenharmony_ci if (ice->batch_count > 1) 320bf215546Sopenharmony_ci screen->vtbl.init_compute_context(&ice->batches[CROCUS_BATCH_COMPUTE]); 321bf215546Sopenharmony_ci 322bf215546Sopenharmony_ci if (!(flags & PIPE_CONTEXT_PREFER_THREADED)) 323bf215546Sopenharmony_ci return ctx; 324bf215546Sopenharmony_ci 325bf215546Sopenharmony_ci return threaded_context_create(ctx, &screen->transfer_pool, 326bf215546Sopenharmony_ci crocus_replace_buffer_storage, 327bf215546Sopenharmony_ci NULL, /* TODO: asynchronous flushes? */ 328bf215546Sopenharmony_ci &ice->thrctx); 329bf215546Sopenharmony_ci} 330bf215546Sopenharmony_ci 331bf215546Sopenharmony_cibool 332bf215546Sopenharmony_cicrocus_sw_check_cond_render(struct crocus_context *ice) 333bf215546Sopenharmony_ci{ 334bf215546Sopenharmony_ci struct crocus_query *q = ice->condition.query; 335bf215546Sopenharmony_ci union pipe_query_result result; 336bf215546Sopenharmony_ci 337bf215546Sopenharmony_ci bool wait = ice->condition.mode == PIPE_RENDER_COND_WAIT || 338bf215546Sopenharmony_ci ice->condition.mode == PIPE_RENDER_COND_BY_REGION_WAIT; 339bf215546Sopenharmony_ci if (!q) 340bf215546Sopenharmony_ci return true; 341bf215546Sopenharmony_ci 342bf215546Sopenharmony_ci bool ret = ice->ctx.get_query_result(&ice->ctx, (void *)q, wait, &result); 343bf215546Sopenharmony_ci if (!ret) 344bf215546Sopenharmony_ci return true; 345bf215546Sopenharmony_ci 346bf215546Sopenharmony_ci return ice->condition.condition ? result.u64 == 0 : result.u64 != 0; 347bf215546Sopenharmony_ci} 348