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 vc4_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 <libsync.h>
38 #include <fcntl.h>
39
40 #include "util/os_file.h"
41 #include "util/perf/cpu_trace.h"
42 #include "util/u_inlines.h"
43
44 #include "vc4_screen.h"
45 #include "vc4_context.h"
46 #include "vc4_bufmgr.h"
47
48 struct vc4_fence {
49 struct pipe_reference reference;
50 uint64_t seqno;
51 int fd;
52 };
53
54 static inline struct vc4_fence *
vc4_fence(struct pipe_fence_handle * pfence)55 vc4_fence(struct pipe_fence_handle *pfence)
56 {
57 return (struct vc4_fence *)pfence;
58 }
59
60 static void
vc4_fence_reference(struct pipe_screen * pscreen,struct pipe_fence_handle ** pp,struct pipe_fence_handle * pf)61 vc4_fence_reference(struct pipe_screen *pscreen,
62 struct pipe_fence_handle **pp,
63 struct pipe_fence_handle *pf)
64 {
65 struct vc4_fence **p = (struct vc4_fence **)pp;
66 struct vc4_fence *f = vc4_fence(pf);
67 struct vc4_fence *old = *p;
68
69 if (pipe_reference(old ? &old->reference : NULL,
70 f ? &f->reference : NULL)) {
71 if (old->fd >= 0)
72 close(old->fd);
73 free(old);
74 }
75 *p = f;
76 }
77
78 static bool
vc4_fence_finish(struct pipe_screen * pscreen,struct pipe_context * ctx,struct pipe_fence_handle * pf,uint64_t timeout_ns)79 vc4_fence_finish(struct pipe_screen *pscreen,
80 struct pipe_context *ctx,
81 struct pipe_fence_handle *pf,
82 uint64_t timeout_ns)
83 {
84 struct vc4_screen *screen = vc4_screen(pscreen);
85 struct vc4_fence *f = vc4_fence(pf);
86
87 MESA_TRACE_FUNC();
88
89 if (f->fd >= 0)
90 return sync_wait(f->fd, timeout_ns / 1000000) == 0;
91
92 return vc4_wait_seqno(screen, f->seqno, timeout_ns, "fence wait");
93 }
94
95 struct vc4_fence *
vc4_fence_create(struct vc4_screen * screen,uint64_t seqno,int fd)96 vc4_fence_create(struct vc4_screen *screen, uint64_t seqno, int fd)
97 {
98 struct vc4_fence *f = calloc(1, sizeof(*f));
99
100 if (!f)
101 return NULL;
102
103 pipe_reference_init(&f->reference, 1);
104 f->seqno = seqno;
105 f->fd = fd;
106
107 return f;
108 }
109
110 static void
vc4_fence_create_fd(struct pipe_context * pctx,struct pipe_fence_handle ** pf,int fd,enum pipe_fd_type type)111 vc4_fence_create_fd(struct pipe_context *pctx, struct pipe_fence_handle **pf,
112 int fd, enum pipe_fd_type type)
113 {
114 struct vc4_context *vc4 = vc4_context(pctx);
115 struct vc4_fence **fence = (struct vc4_fence **)pf;
116
117 assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
118 *fence = vc4_fence_create(vc4->screen, vc4->last_emit_seqno,
119 os_dupfd_cloexec(fd));
120 }
121
122 static void
vc4_fence_server_sync(struct pipe_context * pctx,struct pipe_fence_handle * pfence)123 vc4_fence_server_sync(struct pipe_context *pctx,
124 struct pipe_fence_handle *pfence)
125 {
126 struct vc4_context *vc4 = vc4_context(pctx);
127 struct vc4_fence *fence = vc4_fence(pfence);
128
129 MESA_TRACE_FUNC();
130
131 if (fence->fd >= 0)
132 sync_accumulate("vc4", &vc4->in_fence_fd, fence->fd);
133 }
134
135 static int
vc4_fence_get_fd(struct pipe_screen * screen,struct pipe_fence_handle * pfence)136 vc4_fence_get_fd(struct pipe_screen *screen, struct pipe_fence_handle *pfence)
137 {
138 struct vc4_fence *fence = vc4_fence(pfence);
139
140 MESA_TRACE_FUNC();
141
142 return os_dupfd_cloexec(fence->fd);
143 }
144
145 int
vc4_fence_context_init(struct vc4_context * vc4)146 vc4_fence_context_init(struct vc4_context *vc4)
147 {
148 vc4->base.create_fence_fd = vc4_fence_create_fd;
149 vc4->base.fence_server_sync = vc4_fence_server_sync;
150 vc4->in_fence_fd = -1;
151
152 /* Since we initialize the in_fence_fd to -1 (no wait necessary),
153 * we also need to initialize our in_syncobj as signaled.
154 */
155 if (vc4->screen->has_syncobj) {
156 return drmSyncobjCreate(vc4->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
157 &vc4->in_syncobj);
158 } else {
159 return 0;
160 }
161 }
162
163 void
vc4_fence_screen_init(struct vc4_screen * screen)164 vc4_fence_screen_init(struct vc4_screen *screen)
165 {
166 screen->base.fence_reference = vc4_fence_reference;
167 screen->base.fence_finish = vc4_fence_finish;
168 screen->base.fence_get_fd = vc4_fence_get_fd;
169 }
170