1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2014 Broadcom 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg/** @file v3d_fence.c 25b8e80941Smrg * 26b8e80941Smrg * Seqno-based fence management. 27b8e80941Smrg * 28b8e80941Smrg * We have two mechanisms for waiting in our kernel API: You can wait on a BO 29b8e80941Smrg * to have all rendering to from any process to be completed, or wait on a 30b8e80941Smrg * seqno for that particular seqno to be passed. The fence API we're 31b8e80941Smrg * implementing is based on waiting for all rendering in the context to have 32b8e80941Smrg * completed (with no reference to what other processes might be doing with 33b8e80941Smrg * the same BOs), so we can just use the seqno of the last rendering we'd 34b8e80941Smrg * fired off as our fence marker. 35b8e80941Smrg */ 36b8e80941Smrg 37b8e80941Smrg#include "util/u_inlines.h" 38b8e80941Smrg#include "util/os_time.h" 39b8e80941Smrg 40b8e80941Smrg#include "v3d_context.h" 41b8e80941Smrg#include "v3d_bufmgr.h" 42b8e80941Smrg 43b8e80941Smrgstruct v3d_fence { 44b8e80941Smrg struct pipe_reference reference; 45b8e80941Smrg int fd; 46b8e80941Smrg}; 47b8e80941Smrg 48b8e80941Smrgstatic void 49b8e80941Smrgv3d_fence_reference(struct pipe_screen *pscreen, 50b8e80941Smrg struct pipe_fence_handle **pp, 51b8e80941Smrg struct pipe_fence_handle *pf) 52b8e80941Smrg{ 53b8e80941Smrg struct v3d_fence **p = (struct v3d_fence **)pp; 54b8e80941Smrg struct v3d_fence *f = (struct v3d_fence *)pf; 55b8e80941Smrg struct v3d_fence *old = *p; 56b8e80941Smrg 57b8e80941Smrg if (pipe_reference(&(*p)->reference, &f->reference)) { 58b8e80941Smrg close(old->fd); 59b8e80941Smrg free(old); 60b8e80941Smrg } 61b8e80941Smrg *p = f; 62b8e80941Smrg} 63b8e80941Smrg 64b8e80941Smrgstatic boolean 65b8e80941Smrgv3d_fence_finish(struct pipe_screen *pscreen, 66b8e80941Smrg struct pipe_context *ctx, 67b8e80941Smrg struct pipe_fence_handle *pf, 68b8e80941Smrg uint64_t timeout_ns) 69b8e80941Smrg{ 70b8e80941Smrg struct v3d_screen *screen = v3d_screen(pscreen); 71b8e80941Smrg struct v3d_fence *f = (struct v3d_fence *)pf; 72b8e80941Smrg int ret; 73b8e80941Smrg 74b8e80941Smrg unsigned syncobj; 75b8e80941Smrg ret = drmSyncobjCreate(screen->fd, 0, &syncobj); 76b8e80941Smrg if (ret) { 77b8e80941Smrg fprintf(stderr, "Failed to create syncobj to wait on: %d\n", 78b8e80941Smrg ret); 79b8e80941Smrg return false; 80b8e80941Smrg } 81b8e80941Smrg 82b8e80941Smrg drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd); 83b8e80941Smrg if (ret) { 84b8e80941Smrg fprintf(stderr, "Failed to import fence to syncobj: %d\n", ret); 85b8e80941Smrg return false; 86b8e80941Smrg } 87b8e80941Smrg 88b8e80941Smrg uint64_t abs_timeout = os_time_get_absolute_timeout(timeout_ns); 89b8e80941Smrg if (abs_timeout == OS_TIMEOUT_INFINITE) 90b8e80941Smrg abs_timeout = INT64_MAX; 91b8e80941Smrg 92b8e80941Smrg ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL); 93b8e80941Smrg 94b8e80941Smrg drmSyncobjDestroy(screen->fd, syncobj); 95b8e80941Smrg 96b8e80941Smrg return ret >= 0; 97b8e80941Smrg} 98b8e80941Smrg 99b8e80941Smrgstruct v3d_fence * 100b8e80941Smrgv3d_fence_create(struct v3d_context *v3d) 101b8e80941Smrg{ 102b8e80941Smrg struct v3d_fence *f = calloc(1, sizeof(*f)); 103b8e80941Smrg if (!f) 104b8e80941Smrg return NULL; 105b8e80941Smrg 106b8e80941Smrg /* Snapshot the last V3D rendering's out fence. We'd rather have 107b8e80941Smrg * another syncobj instead of a sync file, but this is all we get. 108b8e80941Smrg * (HandleToFD/FDToHandle just gives you another syncobj ID for the 109b8e80941Smrg * same syncobj). 110b8e80941Smrg */ 111b8e80941Smrg drmSyncobjExportSyncFile(v3d->fd, v3d->out_sync, &f->fd); 112b8e80941Smrg if (f->fd == -1) { 113b8e80941Smrg fprintf(stderr, "export failed\n"); 114b8e80941Smrg free(f); 115b8e80941Smrg return NULL; 116b8e80941Smrg } 117b8e80941Smrg 118b8e80941Smrg pipe_reference_init(&f->reference, 1); 119b8e80941Smrg 120b8e80941Smrg return f; 121b8e80941Smrg} 122b8e80941Smrg 123b8e80941Smrgvoid 124b8e80941Smrgv3d_fence_init(struct v3d_screen *screen) 125b8e80941Smrg{ 126b8e80941Smrg screen->base.fence_reference = v3d_fence_reference; 127b8e80941Smrg screen->base.fence_finish = v3d_fence_finish; 128b8e80941Smrg} 129