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