1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (c) 2018-2019 Lima Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sub license,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
12bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
13bf215546Sopenharmony_ci * of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include <fcntl.h>
26bf215546Sopenharmony_ci#include <libsync.h>
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/os_file.h"
29bf215546Sopenharmony_ci#include <util/u_memory.h>
30bf215546Sopenharmony_ci#include <util/u_inlines.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "drm-uapi/lima_drm.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "lima_screen.h"
35bf215546Sopenharmony_ci#include "lima_context.h"
36bf215546Sopenharmony_ci#include "lima_fence.h"
37bf215546Sopenharmony_ci#include "lima_job.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistruct pipe_fence_handle {
40bf215546Sopenharmony_ci   struct pipe_reference reference;
41bf215546Sopenharmony_ci   int fd;
42bf215546Sopenharmony_ci};
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cistatic void
45bf215546Sopenharmony_cilima_create_fence_fd(struct pipe_context *pctx,
46bf215546Sopenharmony_ci                     struct pipe_fence_handle **fence,
47bf215546Sopenharmony_ci                     int fd, enum pipe_fd_type type)
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   assert(type == PIPE_FD_TYPE_NATIVE_SYNC);
50bf215546Sopenharmony_ci   *fence = lima_fence_create(os_dupfd_cloexec(fd));
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_cilima_fence_server_sync(struct pipe_context *pctx,
55bf215546Sopenharmony_ci                       struct pipe_fence_handle *fence)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   struct lima_context *ctx = lima_context(pctx);
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   sync_accumulate("lima", &ctx->in_sync_fd, fence->fd);
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_civoid lima_fence_context_init(struct lima_context *ctx)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   ctx->base.create_fence_fd = lima_create_fence_fd;
65bf215546Sopenharmony_ci   ctx->base.fence_server_sync = lima_fence_server_sync;
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_cistruct pipe_fence_handle *
69bf215546Sopenharmony_cilima_fence_create(int fd)
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   struct pipe_fence_handle *fence;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   fence = CALLOC_STRUCT(pipe_fence_handle);
74bf215546Sopenharmony_ci   if (!fence)
75bf215546Sopenharmony_ci      return NULL;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   pipe_reference_init(&fence->reference, 1);
78bf215546Sopenharmony_ci   fence->fd = fd;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   return fence;
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic int
84bf215546Sopenharmony_cilima_fence_get_fd(struct pipe_screen *pscreen,
85bf215546Sopenharmony_ci                  struct pipe_fence_handle *fence)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   return os_dupfd_cloexec(fence->fd);
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistatic void
91bf215546Sopenharmony_cilima_fence_destroy(struct pipe_fence_handle *fence)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   if (fence->fd >= 0)
94bf215546Sopenharmony_ci      close(fence->fd);
95bf215546Sopenharmony_ci   FREE(fence);
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_cistatic void
99bf215546Sopenharmony_cilima_fence_reference(struct pipe_screen *pscreen,
100bf215546Sopenharmony_ci                     struct pipe_fence_handle **ptr,
101bf215546Sopenharmony_ci                     struct pipe_fence_handle *fence)
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   if (pipe_reference(&(*ptr)->reference, &fence->reference))
104bf215546Sopenharmony_ci      lima_fence_destroy(*ptr);
105bf215546Sopenharmony_ci   *ptr = fence;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_cistatic bool
109bf215546Sopenharmony_cilima_fence_finish(struct pipe_screen *pscreen, struct pipe_context *pctx,
110bf215546Sopenharmony_ci                  struct pipe_fence_handle *fence, uint64_t timeout)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   return !sync_wait(fence->fd, timeout / 1000000);
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_civoid
116bf215546Sopenharmony_cilima_fence_screen_init(struct lima_screen *screen)
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci   screen->base.fence_reference = lima_fence_reference;
119bf215546Sopenharmony_ci   screen->base.fence_finish = lima_fence_finish;
120bf215546Sopenharmony_ci   screen->base.fence_get_fd = lima_fence_get_fd;
121bf215546Sopenharmony_ci}
122