Path: blob/21.2-virgl/src/gallium/drivers/v3d/v3d_fence.c
4570 views
/*1* Copyright © 2014 Broadcom2*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 (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/** @file v3d_fence.c24*25* Seqno-based fence management.26*27* We have two mechanisms for waiting in our kernel API: You can wait on a BO28* to have all rendering to from any process to be completed, or wait on a29* seqno for that particular seqno to be passed. The fence API we're30* implementing is based on waiting for all rendering in the context to have31* completed (with no reference to what other processes might be doing with32* the same BOs), so we can just use the seqno of the last rendering we'd33* fired off as our fence marker.34*/3536#include "util/u_inlines.h"37#include "util/os_time.h"3839#include "v3d_context.h"40#include "v3d_bufmgr.h"4142struct v3d_fence {43struct pipe_reference reference;44int fd;45};4647static void48v3d_fence_reference(struct pipe_screen *pscreen,49struct pipe_fence_handle **pp,50struct pipe_fence_handle *pf)51{52struct v3d_fence **p = (struct v3d_fence **)pp;53struct v3d_fence *f = (struct v3d_fence *)pf;54struct v3d_fence *old = *p;5556if (pipe_reference(&(*p)->reference, &f->reference)) {57close(old->fd);58free(old);59}60*p = f;61}6263static bool64v3d_fence_finish(struct pipe_screen *pscreen,65struct pipe_context *ctx,66struct pipe_fence_handle *pf,67uint64_t timeout_ns)68{69struct v3d_screen *screen = v3d_screen(pscreen);70struct v3d_fence *f = (struct v3d_fence *)pf;71int ret;7273unsigned syncobj;74ret = drmSyncobjCreate(screen->fd, 0, &syncobj);75if (ret) {76fprintf(stderr, "Failed to create syncobj to wait on: %d\n",77ret);78return false;79}8081ret = drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd);82if (ret) {83fprintf(stderr, "Failed to import fence to syncobj: %d\n", ret);84return false;85}8687uint64_t abs_timeout = os_time_get_absolute_timeout(timeout_ns);88if (abs_timeout == OS_TIMEOUT_INFINITE)89abs_timeout = INT64_MAX;9091ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);9293drmSyncobjDestroy(screen->fd, syncobj);9495return ret >= 0;96}9798struct v3d_fence *99v3d_fence_create(struct v3d_context *v3d)100{101struct v3d_fence *f = calloc(1, sizeof(*f));102if (!f)103return NULL;104105/* Snapshot the last V3D rendering's out fence. We'd rather have106* another syncobj instead of a sync file, but this is all we get.107* (HandleToFD/FDToHandle just gives you another syncobj ID for the108* same syncobj).109*/110drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &f->fd);111if (f->fd == -1) {112fprintf(stderr, "export failed\n");113free(f);114return NULL;115}116117pipe_reference_init(&f->reference, 1);118119return f;120}121122void123v3d_fence_init(struct v3d_screen *screen)124{125screen->base.fence_reference = v3d_fence_reference;126screen->base.fence_finish = v3d_fence_finish;127}128129130