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