Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_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 vc4_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 <libsync.h>37#include <fcntl.h>3839#include "util/os_file.h"40#include "util/u_inlines.h"4142#include "vc4_screen.h"43#include "vc4_context.h"44#include "vc4_bufmgr.h"4546struct vc4_fence {47struct pipe_reference reference;48uint64_t seqno;49int fd;50};5152static inline struct vc4_fence *53vc4_fence(struct pipe_fence_handle *pfence)54{55return (struct vc4_fence *)pfence;56}5758static void59vc4_fence_reference(struct pipe_screen *pscreen,60struct pipe_fence_handle **pp,61struct pipe_fence_handle *pf)62{63struct vc4_fence **p = (struct vc4_fence **)pp;64struct vc4_fence *f = vc4_fence(pf);65struct vc4_fence *old = *p;6667if (pipe_reference(&(*p)->reference, &f->reference)) {68if (old->fd >= 0)69close(old->fd);70free(old);71}72*p = f;73}7475static bool76vc4_fence_finish(struct pipe_screen *pscreen,77struct pipe_context *ctx,78struct pipe_fence_handle *pf,79uint64_t timeout_ns)80{81struct vc4_screen *screen = vc4_screen(pscreen);82struct vc4_fence *f = vc4_fence(pf);8384if (f->fd >= 0)85return sync_wait(f->fd, timeout_ns / 1000000) == 0;8687return vc4_wait_seqno(screen, f->seqno, timeout_ns, "fence wait");88}8990struct vc4_fence *91vc4_fence_create(struct vc4_screen *screen, uint64_t seqno, int fd)92{93struct vc4_fence *f = calloc(1, sizeof(*f));9495if (!f)96return NULL;9798pipe_reference_init(&f->reference, 1);99f->seqno = seqno;100f->fd = fd;101102return f;103}104105static void106vc4_fence_create_fd(struct pipe_context *pctx, struct pipe_fence_handle **pf,107int fd, enum pipe_fd_type type)108{109struct vc4_context *vc4 = vc4_context(pctx);110struct vc4_fence **fence = (struct vc4_fence **)pf;111112assert(type == PIPE_FD_TYPE_NATIVE_SYNC);113*fence = vc4_fence_create(vc4->screen, vc4->last_emit_seqno,114os_dupfd_cloexec(fd));115}116117static void118vc4_fence_server_sync(struct pipe_context *pctx,119struct pipe_fence_handle *pfence)120{121struct vc4_context *vc4 = vc4_context(pctx);122struct vc4_fence *fence = vc4_fence(pfence);123124if (fence->fd >= 0)125sync_accumulate("vc4", &vc4->in_fence_fd, fence->fd);126}127128static int129vc4_fence_get_fd(struct pipe_screen *screen, struct pipe_fence_handle *pfence)130{131struct vc4_fence *fence = vc4_fence(pfence);132133return os_dupfd_cloexec(fence->fd);134}135136int137vc4_fence_context_init(struct vc4_context *vc4)138{139vc4->base.create_fence_fd = vc4_fence_create_fd;140vc4->base.fence_server_sync = vc4_fence_server_sync;141vc4->in_fence_fd = -1;142143/* Since we initialize the in_fence_fd to -1 (no wait necessary),144* we also need to initialize our in_syncobj as signaled.145*/146if (vc4->screen->has_syncobj) {147return drmSyncobjCreate(vc4->fd, DRM_SYNCOBJ_CREATE_SIGNALED,148&vc4->in_syncobj);149} else {150return 0;151}152}153154void155vc4_fence_screen_init(struct vc4_screen *screen)156{157screen->base.fence_reference = vc4_fence_reference;158screen->base.fence_finish = vc4_fence_finish;159screen->base.fence_get_fd = vc4_fence_get_fd;160}161162163