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