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