Path: blob/21.2-virgl/src/gallium/drivers/crocus/crocus_context.c
4570 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122#include <stdio.h>23#include <time.h>24#include "pipe/p_defines.h"25#include "pipe/p_state.h"26#include "util/ralloc.h"27#include "util/u_inlines.h"28#include "util/format/u_format.h"29#include "util/u_upload_mgr.h"30#include "drm-uapi/i915_drm.h"31#include "crocus_context.h"32#include "crocus_resource.h"33#include "crocus_screen.h"34#include "common/intel_defines.h"35#include "common/intel_sample_positions.h"3637/**38* The pipe->set_debug_callback() driver hook.39*/40static void41crocus_set_debug_callback(struct pipe_context *ctx,42const struct pipe_debug_callback *cb)43{44struct crocus_context *ice = (struct crocus_context *)ctx;4546if (cb)47ice->dbg = *cb;48else49memset(&ice->dbg, 0, sizeof(ice->dbg));50}5152static bool53crocus_init_identifier_bo(struct crocus_context *ice)54{55void *bo_map;5657bo_map = crocus_bo_map(NULL, ice->workaround_bo, MAP_READ | MAP_WRITE);58if (!bo_map)59return false;6061ice->workaround_bo->kflags |= EXEC_OBJECT_CAPTURE;62ice->workaround_offset = ALIGN(63intel_debug_write_identifiers(bo_map, 4096, "Crocus") + 8, 8);6465crocus_bo_unmap(ice->workaround_bo);6667return true;68}6970/**71* Called from the batch module when it detects a GPU hang.72*73* In this case, we've lost our GEM context, and can't rely on any existing74* state on the GPU. We must mark everything dirty and wipe away any saved75* assumptions about the last known state of the GPU.76*/77void78crocus_lost_context_state(struct crocus_batch *batch)79{80/* The batch module doesn't have an crocus_context, because we want to81* avoid introducing lots of layering violations. Unfortunately, here82* we do need to inform the context of batch catastrophe. We know the83* batch is one of our context's, so hackily claw our way back.84*/85struct crocus_context *ice = batch->ice;86struct crocus_screen *screen = batch->screen;87if (batch->name == CROCUS_BATCH_RENDER) {88screen->vtbl.init_render_context(batch);89} else if (batch->name == CROCUS_BATCH_COMPUTE) {90screen->vtbl.init_compute_context(batch);91} else {92unreachable("unhandled batch reset");93}9495ice->state.dirty = ~0ull;96memset(ice->state.last_grid, 0, sizeof(ice->state.last_grid));97batch->state_base_address_emitted = false;98screen->vtbl.lost_genx_state(ice, batch);99}100101static enum pipe_reset_status102crocus_get_device_reset_status(struct pipe_context *ctx)103{104struct crocus_context *ice = (struct crocus_context *)ctx;105106enum pipe_reset_status worst_reset = PIPE_NO_RESET;107108/* Check the reset status of each batch's hardware context, and take the109* worst status (if one was guilty, proclaim guilt).110*/111for (int i = 0; i < ice->batch_count; i++) {112/* This will also recreate the hardware contexts as necessary, so any113* future queries will show no resets. We only want to report once.114*/115enum pipe_reset_status batch_reset =116crocus_batch_check_for_reset(&ice->batches[i]);117118if (batch_reset == PIPE_NO_RESET)119continue;120121if (worst_reset == PIPE_NO_RESET) {122worst_reset = batch_reset;123} else {124/* GUILTY < INNOCENT < UNKNOWN */125worst_reset = MIN2(worst_reset, batch_reset);126}127}128129if (worst_reset != PIPE_NO_RESET && ice->reset.reset)130ice->reset.reset(ice->reset.data, worst_reset);131132return worst_reset;133}134135static void136crocus_set_device_reset_callback(struct pipe_context *ctx,137const struct pipe_device_reset_callback *cb)138{139struct crocus_context *ice = (struct crocus_context *)ctx;140141if (cb)142ice->reset = *cb;143else144memset(&ice->reset, 0, sizeof(ice->reset));145}146147static void148crocus_get_sample_position(struct pipe_context *ctx,149unsigned sample_count,150unsigned sample_index,151float *out_value)152{153union {154struct {155float x[16];156float y[16];157} a;158struct {159float _0XOffset, _1XOffset, _2XOffset, _3XOffset,160_4XOffset, _5XOffset, _6XOffset, _7XOffset,161_8XOffset, _9XOffset, _10XOffset, _11XOffset,162_12XOffset, _13XOffset, _14XOffset, _15XOffset;163float _0YOffset, _1YOffset, _2YOffset, _3YOffset,164_4YOffset, _5YOffset, _6YOffset, _7YOffset,165_8YOffset, _9YOffset, _10YOffset, _11YOffset,166_12YOffset, _13YOffset, _14YOffset, _15YOffset;167} v;168} u;169switch (sample_count) {170case 1: INTEL_SAMPLE_POS_1X(u.v._); break;171case 2: INTEL_SAMPLE_POS_2X(u.v._); break;172case 4: INTEL_SAMPLE_POS_4X(u.v._); break;173case 8: INTEL_SAMPLE_POS_8X(u.v._); break;174case 16: INTEL_SAMPLE_POS_16X(u.v._); break;175default: unreachable("invalid sample count");176}177178out_value[0] = u.a.x[sample_index];179out_value[1] = u.a.y[sample_index];180}181182/**183* Destroy a context, freeing any associated memory.184*/185static void186crocus_destroy_context(struct pipe_context *ctx)187{188struct crocus_context *ice = (struct crocus_context *)ctx;189struct crocus_screen *screen = (struct crocus_screen *)ctx->screen;190if (ctx->stream_uploader)191u_upload_destroy(ctx->stream_uploader);192193if (ice->blitter)194util_blitter_destroy(ice->blitter);195screen->vtbl.destroy_state(ice);196crocus_destroy_program_cache(ice);197u_upload_destroy(ice->query_buffer_uploader);198199crocus_bo_unreference(ice->workaround_bo);200201slab_destroy_child(&ice->transfer_pool);202slab_destroy_child(&ice->transfer_pool_unsync);203204crocus_batch_free(&ice->batches[CROCUS_BATCH_RENDER]);205if (ice->batches[CROCUS_BATCH_COMPUTE].ice)206crocus_batch_free(&ice->batches[CROCUS_BATCH_COMPUTE]);207208ralloc_free(ice);209}210211#define genX_call(devinfo, func, ...) \212switch ((devinfo)->verx10) { \213case 80: \214gfx8_##func(__VA_ARGS__); \215break; \216case 75: \217gfx75_##func(__VA_ARGS__); \218break; \219case 70: \220gfx7_##func(__VA_ARGS__); \221break; \222case 60: \223gfx6_##func(__VA_ARGS__); \224break; \225case 50: \226gfx5_##func(__VA_ARGS__); \227break; \228case 45: \229gfx45_##func(__VA_ARGS__); \230break; \231case 40: \232gfx4_##func(__VA_ARGS__); \233break; \234default: \235unreachable("Unknown hardware generation"); \236}237238/**239* Create a context.240*241* This is where each context begins.242*/243struct pipe_context *244crocus_create_context(struct pipe_screen *pscreen, void *priv, unsigned flags)245{246struct crocus_screen *screen = (struct crocus_screen*)pscreen;247const struct intel_device_info *devinfo = &screen->devinfo;248struct crocus_context *ice = rzalloc(NULL, struct crocus_context);249250if (!ice)251return NULL;252253struct pipe_context *ctx = &ice->ctx;254255ctx->screen = pscreen;256ctx->priv = priv;257258ctx->stream_uploader = u_upload_create_default(ctx);259if (!ctx->stream_uploader) {260free(ctx);261return NULL;262}263ctx->const_uploader = ctx->stream_uploader;264265ctx->destroy = crocus_destroy_context;266ctx->set_debug_callback = crocus_set_debug_callback;267ctx->set_device_reset_callback = crocus_set_device_reset_callback;268ctx->get_device_reset_status = crocus_get_device_reset_status;269ctx->get_sample_position = crocus_get_sample_position;270271ice->shaders.urb_size = devinfo->urb.size;272273crocus_init_context_fence_functions(ctx);274crocus_init_blit_functions(ctx);275crocus_init_clear_functions(ctx);276crocus_init_program_functions(ctx);277crocus_init_resource_functions(ctx);278crocus_init_flush_functions(ctx);279280crocus_init_program_cache(ice);281282slab_create_child(&ice->transfer_pool, &screen->transfer_pool);283slab_create_child(&ice->transfer_pool_unsync, &screen->transfer_pool);284285ice->query_buffer_uploader =286u_upload_create(ctx, 4096, PIPE_BIND_CUSTOM, PIPE_USAGE_STAGING,2870);288289ice->workaround_bo =290crocus_bo_alloc(screen->bufmgr, "workaround", 4096);291if (!ice->workaround_bo)292return NULL;293294if (!crocus_init_identifier_bo(ice))295return NULL;296297genX_call(devinfo, crocus_init_state, ice);298genX_call(devinfo, crocus_init_blorp, ice);299genX_call(devinfo, crocus_init_query, ice);300301ice->blitter = util_blitter_create(&ice->ctx);302if (ice->blitter == NULL)303return NULL;304int priority = 0;305if (flags & PIPE_CONTEXT_HIGH_PRIORITY)306priority = INTEL_CONTEXT_HIGH_PRIORITY;307if (flags & PIPE_CONTEXT_LOW_PRIORITY)308priority = INTEL_CONTEXT_LOW_PRIORITY;309310ice->batch_count = devinfo->ver >= 7 ? CROCUS_BATCH_COUNT : 1;311for (int i = 0; i < ice->batch_count; i++) {312crocus_init_batch(ice, (enum crocus_batch_name) i,313priority);314}315316ice->urb.size = devinfo->urb.size;317screen->vtbl.init_render_context(&ice->batches[CROCUS_BATCH_RENDER]);318if (ice->batch_count > 1)319screen->vtbl.init_compute_context(&ice->batches[CROCUS_BATCH_COMPUTE]);320321if (!(flags & PIPE_CONTEXT_PREFER_THREADED))322return ctx;323324return threaded_context_create(ctx, &screen->transfer_pool,325crocus_replace_buffer_storage,326NULL, /* TODO: asynchronous flushes? */327NULL,328false,329&ice->thrctx);330}331332bool333crocus_sw_check_cond_render(struct crocus_context *ice)334{335struct crocus_query *q = ice->condition.query;336union pipe_query_result result;337338bool wait = ice->condition.mode == PIPE_RENDER_COND_WAIT ||339ice->condition.mode == PIPE_RENDER_COND_BY_REGION_WAIT;340if (!q)341return true;342343bool ret = ice->ctx.get_query_result(&ice->ctx, (void *)q, wait, &result);344if (!ret)345return true;346347return ice->condition.condition ? result.u64 == 0 : result.u64 != 0;348}349350351