1 /*
2 * Copyright © 2021 Intel Corporation
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 #ifndef VK_SYNC_TIMELINE_H
24 #define VK_SYNC_TIMELINE_H
25
26 #include "c11/threads.h"
27 #include "util/list.h"
28 #include "util/macros.h"
29
30 #include "vk_sync.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 struct vk_sync_timeline_type {
37 struct vk_sync_type sync;
38
39 /* Type of each individual time point */
40 const struct vk_sync_type *point_sync_type;
41 };
42
43 struct vk_sync_timeline_type
44 vk_sync_timeline_get_type(const struct vk_sync_type *point_sync_type);
45
46 struct vk_sync_timeline_point {
47 struct vk_sync_timeline *timeline;
48
49 struct list_head link;
50
51 uint64_t value;
52
53 int refcount;
54 bool pending;
55
56 struct vk_sync sync;
57 };
58
59 /** Implements a timeline vk_sync type on top of a binary vk_sync
60 *
61 * This is used for emulating VK_KHR_timeline_semaphores for implementations
62 * whose kernel driver do not yet support timeline syncobj. Since it's a
63 * requirement for Vulkan 1.2, it's useful to have an emulation like this.
64 *
65 * The driver should never see a vk_sync_timeline object. Instead, converting
66 * from vk_sync_timeline to a binary vk_sync for a particular time point is
67 * handled by common code. All a driver needs to do is declare its preferred
68 * binary vk_sync_type for emulation as follows:
69 *
70 * const struct vk_sync_type anv_bo_sync_type = {
71 * ...
72 * };
73 * VK_DECL_TIMELINE_TYPE(anv_bo_timeline_sync_type, &anv_bo_sync_type);
74 *
75 * and then anv_bo_timeline_sync_type.sync can be used as a sync type to
76 * provide timelines.
77 */
78 struct vk_sync_timeline {
79 struct vk_sync sync;
80
81 mtx_t mutex;
82 cnd_t cond;
83
84 uint64_t highest_past;
85 uint64_t highest_pending;
86
87 struct list_head pending_points;
88 struct list_head free_points;
89 };
90
91 VkResult vk_sync_timeline_init(struct vk_device *device,
92 struct vk_sync *sync,
93 uint64_t initial_value);
94
95 VkResult vk_sync_timeline_alloc_point(struct vk_device *device,
96 struct vk_sync_timeline *timeline,
97 uint64_t value,
98 struct vk_sync_timeline_point **point_out);
99
100 void vk_sync_timeline_point_free(struct vk_device *device,
101 struct vk_sync_timeline_point *point);
102
103 VkResult vk_sync_timeline_point_install(struct vk_device *device,
104 struct vk_sync_timeline_point *point);
105
106 VkResult vk_sync_timeline_get_point(struct vk_device *device,
107 struct vk_sync_timeline *timeline,
108 uint64_t wait_value,
109 struct vk_sync_timeline_point **point_out);
110
111 void vk_sync_timeline_point_release(struct vk_device *device,
112 struct vk_sync_timeline_point *point);
113
114 static inline bool
vk_sync_type_is_vk_sync_timeline(const struct vk_sync_type * type)115 vk_sync_type_is_vk_sync_timeline(const struct vk_sync_type *type)
116 {
117 return type->init == vk_sync_timeline_init;
118 }
119
120 static inline struct vk_sync_timeline *
vk_sync_as_timeline(struct vk_sync * sync)121 vk_sync_as_timeline(struct vk_sync *sync)
122 {
123 if (!vk_sync_type_is_vk_sync_timeline(sync->type))
124 return NULL;
125
126 return container_of(sync, struct vk_sync_timeline, sync);
127 }
128
129 #ifdef __cplusplus
130 }
131 #endif
132
133 #endif /* VK_SYNC_TIMELINE_H */
134