Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_fence.c
4570 views
/*1* Copyright 2013-2017 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 THE21* SOFTWARE.22*23*/2425#include "si_build_pm4.h"26#include "util/os_time.h"27#include "util/u_memory.h"28#include "util/u_queue.h"29#include "util/u_upload_mgr.h"3031#include <libsync.h>3233struct si_fine_fence {34struct si_resource *buf;35unsigned offset;36};3738struct si_fence {39struct pipe_reference reference;40struct pipe_fence_handle *gfx;41struct tc_unflushed_batch_token *tc_token;42struct util_queue_fence ready;4344/* If the context wasn't flushed at fence creation, this is non-NULL. */45struct {46struct si_context *ctx;47unsigned ib_index;48} gfx_unflushed;4950struct si_fine_fence fine;51};5253/**54* Write an EOP event.55*56* \param event EVENT_TYPE_*57* \param event_flags Optional cache flush flags (TC)58* \param dst_sel MEM or TC_L259* \param int_sel NONE or SEND_DATA_AFTER_WR_CONFIRM60* \param data_sel DISCARD, VALUE_32BIT, TIMESTAMP, or GDS61* \param buf Buffer62* \param va GPU address63* \param old_value Previous fence value (for a bug workaround)64* \param new_value Fence value to write for this event.65*/66void si_cp_release_mem(struct si_context *ctx, struct radeon_cmdbuf *cs, unsigned event,67unsigned event_flags, unsigned dst_sel, unsigned int_sel, unsigned data_sel,68struct si_resource *buf, uint64_t va, uint32_t new_fence,69unsigned query_type)70{71unsigned op = EVENT_TYPE(event) |72EVENT_INDEX(event == V_028A90_CS_DONE || event == V_028A90_PS_DONE ? 6 : 5) |73event_flags;74unsigned sel = EOP_DST_SEL(dst_sel) | EOP_INT_SEL(int_sel) | EOP_DATA_SEL(data_sel);75bool compute_ib = !ctx->has_graphics || cs == &ctx->prim_discard_compute_cs;7677radeon_begin(cs);7879if (ctx->chip_class >= GFX9 || (compute_ib && ctx->chip_class >= GFX7)) {80/* A ZPASS_DONE or PIXEL_STAT_DUMP_EVENT (of the DB occlusion81* counters) must immediately precede every timestamp event to82* prevent a GPU hang on GFX9.83*84* Occlusion queries don't need to do it here, because they85* always do ZPASS_DONE before the timestamp.86*/87if (ctx->chip_class == GFX9 && !compute_ib && query_type != PIPE_QUERY_OCCLUSION_COUNTER &&88query_type != PIPE_QUERY_OCCLUSION_PREDICATE &&89query_type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {90struct si_resource *scratch = unlikely(ctx->ws->cs_is_secure(&ctx->gfx_cs)) ?91ctx->eop_bug_scratch_tmz : ctx->eop_bug_scratch;9293assert(16 * ctx->screen->info.max_render_backends <= scratch->b.b.width0);94radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));95radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));96radeon_emit(cs, scratch->gpu_address);97radeon_emit(cs, scratch->gpu_address >> 32);9899radeon_add_to_buffer_list(ctx, &ctx->gfx_cs, scratch, RADEON_USAGE_WRITE,100RADEON_PRIO_QUERY);101}102103radeon_emit(cs, PKT3(PKT3_RELEASE_MEM, ctx->chip_class >= GFX9 ? 6 : 5, 0));104radeon_emit(cs, op);105radeon_emit(cs, sel);106radeon_emit(cs, va); /* address lo */107radeon_emit(cs, va >> 32); /* address hi */108radeon_emit(cs, new_fence); /* immediate data lo */109radeon_emit(cs, 0); /* immediate data hi */110if (ctx->chip_class >= GFX9)111radeon_emit(cs, 0); /* unused */112} else {113if (ctx->chip_class == GFX7 || ctx->chip_class == GFX8) {114struct si_resource *scratch = ctx->eop_bug_scratch;115uint64_t va = scratch->gpu_address;116117/* Two EOP events are required to make all engines go idle118* (and optional cache flushes executed) before the timestamp119* is written.120*/121radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));122radeon_emit(cs, op);123radeon_emit(cs, va);124radeon_emit(cs, ((va >> 32) & 0xffff) | sel);125radeon_emit(cs, 0); /* immediate data */126radeon_emit(cs, 0); /* unused */127128radeon_add_to_buffer_list(ctx, &ctx->gfx_cs, scratch, RADEON_USAGE_WRITE,129RADEON_PRIO_QUERY);130}131132radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));133radeon_emit(cs, op);134radeon_emit(cs, va);135radeon_emit(cs, ((va >> 32) & 0xffff) | sel);136radeon_emit(cs, new_fence); /* immediate data */137radeon_emit(cs, 0); /* unused */138}139140radeon_end();141142if (buf) {143radeon_add_to_buffer_list(ctx, &ctx->gfx_cs, buf, RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);144}145}146147unsigned si_cp_write_fence_dwords(struct si_screen *screen)148{149unsigned dwords = 6;150151if (screen->info.chip_class == GFX7 || screen->info.chip_class == GFX8)152dwords *= 2;153154return dwords;155}156157void si_cp_wait_mem(struct si_context *ctx, struct radeon_cmdbuf *cs, uint64_t va, uint32_t ref,158uint32_t mask, unsigned flags)159{160radeon_begin(cs);161radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));162radeon_emit(cs, WAIT_REG_MEM_MEM_SPACE(1) | flags);163radeon_emit(cs, va);164radeon_emit(cs, va >> 32);165radeon_emit(cs, ref); /* reference value */166radeon_emit(cs, mask); /* mask */167radeon_emit(cs, 4); /* poll interval */168radeon_end();169}170171static void si_add_fence_dependency(struct si_context *sctx, struct pipe_fence_handle *fence)172{173struct radeon_winsys *ws = sctx->ws;174175ws->cs_add_fence_dependency(&sctx->gfx_cs, fence, 0);176}177178static void si_add_syncobj_signal(struct si_context *sctx, struct pipe_fence_handle *fence)179{180sctx->ws->cs_add_syncobj_signal(&sctx->gfx_cs, fence);181}182183static void si_fence_reference(struct pipe_screen *screen, struct pipe_fence_handle **dst,184struct pipe_fence_handle *src)185{186struct radeon_winsys *ws = ((struct si_screen *)screen)->ws;187struct si_fence **sdst = (struct si_fence **)dst;188struct si_fence *ssrc = (struct si_fence *)src;189190if (pipe_reference(&(*sdst)->reference, &ssrc->reference)) {191ws->fence_reference(&(*sdst)->gfx, NULL);192tc_unflushed_batch_token_reference(&(*sdst)->tc_token, NULL);193si_resource_reference(&(*sdst)->fine.buf, NULL);194FREE(*sdst);195}196*sdst = ssrc;197}198199static struct si_fence *si_create_multi_fence()200{201struct si_fence *fence = CALLOC_STRUCT(si_fence);202if (!fence)203return NULL;204205pipe_reference_init(&fence->reference, 1);206util_queue_fence_init(&fence->ready);207208return fence;209}210211struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,212struct tc_unflushed_batch_token *tc_token)213{214struct si_fence *fence = si_create_multi_fence();215if (!fence)216return NULL;217218util_queue_fence_reset(&fence->ready);219tc_unflushed_batch_token_reference(&fence->tc_token, tc_token);220221return (struct pipe_fence_handle *)fence;222}223224static bool si_fine_fence_signaled(struct radeon_winsys *rws, const struct si_fine_fence *fine)225{226char *map =227rws->buffer_map(rws, fine->buf->buf, NULL, PIPE_MAP_READ | PIPE_MAP_UNSYNCHRONIZED);228if (!map)229return false;230231uint32_t *fence = (uint32_t *)(map + fine->offset);232return *fence != 0;233}234235static void si_fine_fence_set(struct si_context *ctx, struct si_fine_fence *fine, unsigned flags)236{237uint32_t *fence_ptr;238239assert(util_bitcount(flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) == 1);240241/* Use cached system memory for the fence. */242u_upload_alloc(ctx->cached_gtt_allocator, 0, 4, 4, &fine->offset,243(struct pipe_resource **)&fine->buf, (void **)&fence_ptr);244if (!fine->buf)245return;246247*fence_ptr = 0;248249if (flags & PIPE_FLUSH_TOP_OF_PIPE) {250uint32_t value = 0x80000000;251252si_cp_write_data(ctx, fine->buf, fine->offset, 4, V_370_MEM, V_370_PFP, &value);253} else if (flags & PIPE_FLUSH_BOTTOM_OF_PIPE) {254uint64_t fence_va = fine->buf->gpu_address + fine->offset;255256radeon_add_to_buffer_list(ctx, &ctx->gfx_cs, fine->buf, RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);257si_cp_release_mem(ctx, &ctx->gfx_cs, V_028A90_BOTTOM_OF_PIPE_TS, 0, EOP_DST_SEL_MEM,258EOP_INT_SEL_NONE, EOP_DATA_SEL_VALUE_32BIT, NULL, fence_va, 0x80000000,259PIPE_QUERY_GPU_FINISHED);260} else {261assert(false);262}263}264265static bool si_fence_finish(struct pipe_screen *screen, struct pipe_context *ctx,266struct pipe_fence_handle *fence, uint64_t timeout)267{268struct radeon_winsys *rws = ((struct si_screen *)screen)->ws;269struct si_fence *sfence = (struct si_fence *)fence;270struct si_context *sctx;271int64_t abs_timeout = os_time_get_absolute_timeout(timeout);272273ctx = threaded_context_unwrap_sync(ctx);274sctx = (struct si_context *)(ctx ? ctx : NULL);275276if (!util_queue_fence_is_signalled(&sfence->ready)) {277if (sfence->tc_token) {278/* Ensure that si_flush_from_st will be called for279* this fence, but only if we're in the API thread280* where the context is current.281*282* Note that the batch containing the flush may already283* be in flight in the driver thread, so the fence284* may not be ready yet when this call returns.285*/286threaded_context_flush(ctx, sfence->tc_token, timeout == 0);287}288289if (!timeout)290return false;291292if (timeout == PIPE_TIMEOUT_INFINITE) {293util_queue_fence_wait(&sfence->ready);294} else {295if (!util_queue_fence_wait_timeout(&sfence->ready, abs_timeout))296return false;297}298299if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {300int64_t time = os_time_get_nano();301timeout = abs_timeout > time ? abs_timeout - time : 0;302}303}304305if (!sfence->gfx)306return true;307308if (sfence->fine.buf && si_fine_fence_signaled(rws, &sfence->fine)) {309rws->fence_reference(&sfence->gfx, NULL);310si_resource_reference(&sfence->fine.buf, NULL);311return true;312}313314/* Flush the gfx IB if it hasn't been flushed yet. */315if (sctx && sfence->gfx_unflushed.ctx == sctx &&316sfence->gfx_unflushed.ib_index == sctx->num_gfx_cs_flushes) {317/* Section 4.1.2 (Signaling) of the OpenGL 4.6 (Core profile)318* spec says:319*320* "If the sync object being blocked upon will not be321* signaled in finite time (for example, by an associated322* fence command issued previously, but not yet flushed to323* the graphics pipeline), then ClientWaitSync may hang324* forever. To help prevent this behavior, if325* ClientWaitSync is called and all of the following are326* true:327*328* * the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,329* * sync is unsignaled when ClientWaitSync is called,330* * and the calls to ClientWaitSync and FenceSync were331* issued from the same context,332*333* then the GL will behave as if the equivalent of Flush334* were inserted immediately after the creation of sync."335*336* This means we need to flush for such fences even when we're337* not going to wait.338*/339si_flush_gfx_cs(sctx, (timeout ? 0 : PIPE_FLUSH_ASYNC) | RADEON_FLUSH_START_NEXT_GFX_IB_NOW,340NULL);341sfence->gfx_unflushed.ctx = NULL;342343if (!timeout)344return false;345346/* Recompute the timeout after all that. */347if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {348int64_t time = os_time_get_nano();349timeout = abs_timeout > time ? abs_timeout - time : 0;350}351}352353if (rws->fence_wait(rws, sfence->gfx, timeout))354return true;355356/* Re-check in case the GPU is slow or hangs, but the commands before357* the fine-grained fence have completed. */358if (sfence->fine.buf && si_fine_fence_signaled(rws, &sfence->fine))359return true;360361return false;362}363364static void si_create_fence_fd(struct pipe_context *ctx, struct pipe_fence_handle **pfence, int fd,365enum pipe_fd_type type)366{367struct si_screen *sscreen = (struct si_screen *)ctx->screen;368struct radeon_winsys *ws = sscreen->ws;369struct si_fence *sfence;370371*pfence = NULL;372373sfence = si_create_multi_fence();374if (!sfence)375return;376377switch (type) {378case PIPE_FD_TYPE_NATIVE_SYNC:379if (!sscreen->info.has_fence_to_handle)380goto finish;381382sfence->gfx = ws->fence_import_sync_file(ws, fd);383break;384385case PIPE_FD_TYPE_SYNCOBJ:386if (!sscreen->info.has_syncobj)387goto finish;388389sfence->gfx = ws->fence_import_syncobj(ws, fd);390break;391392default:393unreachable("bad fence fd type when importing");394}395396finish:397if (!sfence->gfx) {398FREE(sfence);399return;400}401402*pfence = (struct pipe_fence_handle *)sfence;403}404405static int si_fence_get_fd(struct pipe_screen *screen, struct pipe_fence_handle *fence)406{407struct si_screen *sscreen = (struct si_screen *)screen;408struct radeon_winsys *ws = sscreen->ws;409struct si_fence *sfence = (struct si_fence *)fence;410int gfx_fd = -1;411412if (!sscreen->info.has_fence_to_handle)413return -1;414415util_queue_fence_wait(&sfence->ready);416417/* Deferred fences aren't supported. */418assert(!sfence->gfx_unflushed.ctx);419if (sfence->gfx_unflushed.ctx)420return -1;421422if (sfence->gfx) {423gfx_fd = ws->fence_export_sync_file(ws, sfence->gfx);424if (gfx_fd == -1) {425return -1;426}427}428429/* If we don't have FDs at this point, it means we don't have fences430* either. */431if (gfx_fd == -1)432return ws->export_signalled_sync_file(ws);433434return gfx_fd;435}436437static void si_flush_all_queues(struct pipe_context *ctx,438struct pipe_fence_handle **fence,439unsigned flags, bool force_flush)440{441struct pipe_screen *screen = ctx->screen;442struct si_context *sctx = (struct si_context *)ctx;443struct radeon_winsys *ws = sctx->ws;444struct pipe_fence_handle *gfx_fence = NULL;445bool deferred_fence = false;446struct si_fine_fence fine = {};447unsigned rflags = PIPE_FLUSH_ASYNC;448449if (!(flags & PIPE_FLUSH_DEFERRED)) {450si_flush_implicit_resources(sctx);451}452453if (flags & PIPE_FLUSH_END_OF_FRAME)454rflags |= PIPE_FLUSH_END_OF_FRAME;455456if (flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) {457assert(flags & PIPE_FLUSH_DEFERRED);458assert(fence);459460si_fine_fence_set(sctx, &fine, flags);461}462463if (force_flush) {464sctx->initial_gfx_cs_size = 0;465}466467if (!radeon_emitted(&sctx->gfx_cs, sctx->initial_gfx_cs_size)) {468if (fence)469ws->fence_reference(&gfx_fence, sctx->last_gfx_fence);470if (!(flags & PIPE_FLUSH_DEFERRED))471ws->cs_sync_flush(&sctx->gfx_cs);472473tc_driver_internal_flush_notify(sctx->tc);474} else {475/* Instead of flushing, create a deferred fence. Constraints:476* - the gallium frontend must allow a deferred flush.477* - the gallium frontend must request a fence.478* - fence_get_fd is not allowed.479* Thread safety in fence_finish must be ensured by the gallium frontend.480*/481if (flags & PIPE_FLUSH_DEFERRED && !(flags & PIPE_FLUSH_FENCE_FD) && fence) {482gfx_fence = sctx->ws->cs_get_next_fence(&sctx->gfx_cs);483deferred_fence = true;484} else {485si_flush_gfx_cs(sctx, rflags, fence ? &gfx_fence : NULL);486}487}488489/* Both engines can signal out of order, so we need to keep both fences. */490if (fence) {491struct si_fence *new_fence;492493if (flags & TC_FLUSH_ASYNC) {494new_fence = (struct si_fence *)*fence;495assert(new_fence);496} else {497new_fence = si_create_multi_fence();498if (!new_fence) {499ws->fence_reference(&gfx_fence, NULL);500goto finish;501}502503screen->fence_reference(screen, fence, NULL);504*fence = (struct pipe_fence_handle *)new_fence;505}506507/* If both fences are NULL, fence_finish will always return true. */508new_fence->gfx = gfx_fence;509510if (deferred_fence) {511new_fence->gfx_unflushed.ctx = sctx;512new_fence->gfx_unflushed.ib_index = sctx->num_gfx_cs_flushes;513}514515new_fence->fine = fine;516fine.buf = NULL;517518if (flags & TC_FLUSH_ASYNC) {519util_queue_fence_signal(&new_fence->ready);520tc_unflushed_batch_token_reference(&new_fence->tc_token, NULL);521}522}523assert(!fine.buf);524finish:525if (!(flags & (PIPE_FLUSH_DEFERRED | PIPE_FLUSH_ASYNC))) {526ws->cs_sync_flush(&sctx->gfx_cs);527}528}529530static void si_flush_from_st(struct pipe_context *ctx, struct pipe_fence_handle **fence,531unsigned flags)532{533return si_flush_all_queues(ctx, fence, flags, false);534}535536static void si_fence_server_signal(struct pipe_context *ctx, struct pipe_fence_handle *fence)537{538struct si_context *sctx = (struct si_context *)ctx;539struct si_fence *sfence = (struct si_fence *)fence;540541assert(sfence->gfx);542543if (sfence->gfx)544si_add_syncobj_signal(sctx, sfence->gfx);545546/**547* The spec does not require a flush here. We insert a flush548* because syncobj based signals are not directly placed into549* the command stream. Instead the signal happens when the550* submission associated with the syncobj finishes execution.551*552* Therefore, we must make sure that we flush the pipe to avoid553* new work being emitted and getting executed before the signal554* operation.555*556* Forces a flush even if the GFX CS is empty.557*/558si_flush_all_queues(ctx, NULL, PIPE_FLUSH_ASYNC, true);559}560561static void si_fence_server_sync(struct pipe_context *ctx, struct pipe_fence_handle *fence)562{563struct si_context *sctx = (struct si_context *)ctx;564struct si_fence *sfence = (struct si_fence *)fence;565566util_queue_fence_wait(&sfence->ready);567568/* Unflushed fences from the same context are no-ops. */569if (sfence->gfx_unflushed.ctx && sfence->gfx_unflushed.ctx == sctx)570return;571572/* All unflushed commands will not start execution before this fence573* dependency is signalled. That's fine. Flushing is very expensive574* if we get fence_server_sync after every draw call. (which happens575* with Android/SurfaceFlinger)576*577* In a nutshell, when CPU overhead is greater than GPU overhead,578* or when the time it takes to execute an IB on the GPU is less than579* the time it takes to create and submit that IB, flushing decreases580* performance. Therefore, DO NOT FLUSH.581*/582if (sfence->gfx)583si_add_fence_dependency(sctx, sfence->gfx);584}585586void si_init_fence_functions(struct si_context *ctx)587{588ctx->b.flush = si_flush_from_st;589ctx->b.create_fence_fd = si_create_fence_fd;590ctx->b.fence_server_sync = si_fence_server_sync;591ctx->b.fence_server_signal = si_fence_server_signal;592}593594void si_init_screen_fence_functions(struct si_screen *screen)595{596screen->b.fence_finish = si_fence_finish;597screen->b.fence_reference = si_fence_reference;598screen->b.fence_get_fd = si_fence_get_fd;599}600601602