1b8e80941Smrg/*
2b8e80941Smrg * Copyright (c) 2018-2019 Lima Project
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, sub license,
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
12b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
13b8e80941Smrg * of the 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 NON-INFRINGEMENT. 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
21b8e80941Smrg * DEALINGS IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#include <fcntl.h>
26b8e80941Smrg#include <libsync.h>
27b8e80941Smrg
28b8e80941Smrg#include <util/u_memory.h>
29b8e80941Smrg#include <util/u_inlines.h>
30b8e80941Smrg
31b8e80941Smrg#include "drm-uapi/lima_drm.h"
32b8e80941Smrg
33b8e80941Smrg#include "lima_screen.h"
34b8e80941Smrg#include "lima_context.h"
35b8e80941Smrg#include "lima_fence.h"
36b8e80941Smrg#include "lima_submit.h"
37b8e80941Smrg
38b8e80941Smrgstruct pipe_fence_handle {
39b8e80941Smrg   struct pipe_reference reference;
40b8e80941Smrg   int fd;
41b8e80941Smrg};
42b8e80941Smrg
43b8e80941Smrgstatic void
44b8e80941Smrglima_create_fence_fd(struct pipe_context *pctx,
45b8e80941Smrg                     struct pipe_fence_handle **fence,
46b8e80941Smrg                     int fd, enum pipe_fd_type type)
47b8e80941Smrg{
48b8e80941Smrg   assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
49b8e80941Smrg   *fence = lima_fence_create(fcntl(fd, F_DUPFD_CLOEXEC, 3));
50b8e80941Smrg}
51b8e80941Smrg
52b8e80941Smrgstatic void
53b8e80941Smrglima_fence_server_sync(struct pipe_context *pctx,
54b8e80941Smrg                       struct pipe_fence_handle *fence)
55b8e80941Smrg{
56b8e80941Smrg   struct lima_context *ctx = lima_context(pctx);
57b8e80941Smrg
58b8e80941Smrg   lima_submit_add_in_sync(ctx->gp_submit, fence->fd);
59b8e80941Smrg}
60b8e80941Smrg
61b8e80941Smrgvoid lima_fence_context_init(struct lima_context *ctx)
62b8e80941Smrg{
63b8e80941Smrg   ctx->base.create_fence_fd = lima_create_fence_fd;
64b8e80941Smrg   ctx->base.fence_server_sync = lima_fence_server_sync;
65b8e80941Smrg}
66b8e80941Smrg
67b8e80941Smrgstruct pipe_fence_handle *
68b8e80941Smrglima_fence_create(int fd)
69b8e80941Smrg{
70b8e80941Smrg   struct pipe_fence_handle *fence;
71b8e80941Smrg
72b8e80941Smrg   fence = CALLOC_STRUCT(pipe_fence_handle);
73b8e80941Smrg   if (!fence)
74b8e80941Smrg      return NULL;
75b8e80941Smrg
76b8e80941Smrg   pipe_reference_init(&fence->reference, 1);
77b8e80941Smrg   fence->fd = fd;
78b8e80941Smrg
79b8e80941Smrg   return fence;
80b8e80941Smrg}
81b8e80941Smrg
82b8e80941Smrgstatic int
83b8e80941Smrglima_fence_get_fd(struct pipe_screen *pscreen,
84b8e80941Smrg                  struct pipe_fence_handle *fence)
85b8e80941Smrg{
86b8e80941Smrg   return fcntl(fence->fd, F_DUPFD_CLOEXEC, 3);
87b8e80941Smrg}
88b8e80941Smrg
89b8e80941Smrgstatic void
90b8e80941Smrglima_fence_destroy(struct pipe_fence_handle *fence)
91b8e80941Smrg{
92b8e80941Smrg   if (fence->fd >= 0)
93b8e80941Smrg      close(fence->fd);
94b8e80941Smrg   FREE(fence);
95b8e80941Smrg}
96b8e80941Smrg
97b8e80941Smrgstatic void
98b8e80941Smrglima_fence_reference(struct pipe_screen *pscreen,
99b8e80941Smrg                     struct pipe_fence_handle **ptr,
100b8e80941Smrg                     struct pipe_fence_handle *fence)
101b8e80941Smrg{
102b8e80941Smrg   if (pipe_reference(&(*ptr)->reference, &fence->reference))
103b8e80941Smrg      lima_fence_destroy(*ptr);
104b8e80941Smrg   *ptr = fence;
105b8e80941Smrg}
106b8e80941Smrg
107b8e80941Smrgstatic boolean
108b8e80941Smrglima_fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
109b8e80941Smrg                  struct pipe_fence_handle *fence, uint64_t timeout)
110b8e80941Smrg{
111b8e80941Smrg   return !sync_wait(fence->fd, timeout / 1000000);
112b8e80941Smrg}
113b8e80941Smrg
114b8e80941Smrgvoid
115b8e80941Smrglima_fence_screen_init(struct lima_screen *screen)
116b8e80941Smrg{
117b8e80941Smrg   screen->base.fence_reference = lima_fence_reference;
118b8e80941Smrg   screen->base.fence_finish = lima_fence_finish;
119b8e80941Smrg   screen->base.fence_get_fd = lima_fence_get_fd;
120b8e80941Smrg}
121