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