| 1 |
/* |
| 2 |
* Copyright © 2022 Imagination Technologies Ltd. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to deal |
| 6 |
* in the Software without restriction, including without limitation the rights |
| 7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
* copies of the Software, and to permit persons to whom the Software is |
| 9 |
* 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 THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <stdbool.h> |
| 25 |
#include <stddef.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <poll.h> |
| 29 |
#include <vulkan/vulkan.h> |
| 30 |
|
| 31 |
#include "pvr_device.h" |
| 32 |
#include "pvr_srv.h" |
| 33 |
#include "pvr_srv_sync.h" |
| 34 |
#include "util/libsync.h" |
| 35 |
#include "util/macros.h" |
| 36 |
#include "util/os_file.h" |
| 37 |
#include "util/timespec.h" |
| 38 |
#include "vk_alloc.h" |
| 39 |
#include "vk_log.h" |
| 40 |
#include "vk_sync.h" |
| 41 |
#include "vk_util.h" |
| 42 |
|
| 43 |
static VkResult pvr_srv_sync_init(struct vk_device *device, |
| 44 |
struct vk_sync *sync, |
| 45 |
uint64_t initial_value) |
| 46 |
{ |
| 47 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 48 |
|
| 49 |
srv_sync->signaled = initial_value ? true : false; |
| 50 |
srv_sync->fd = -1; |
| 51 |
|
| 52 |
return VK_SUCCESS; |
| 53 |
} |
| 54 |
|
| 55 |
void pvr_srv_sync_finish(struct vk_device *device, struct vk_sync *sync) |
| 56 |
{ |
| 57 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 58 |
|
| 59 |
if (srv_sync->fd != -1) |
| 60 |
close(srv_sync->fd); |
| 61 |
} |
| 62 |
|
| 63 |
/* Note: function closes the fd. */ |
| 64 |
static void pvr_set_sync_state(struct pvr_srv_sync *srv_sync, bool signaled) |
| 65 |
{ |
| 66 |
if (srv_sync->fd != -1) { |
| 67 |
close(srv_sync->fd); |
| 68 |
srv_sync->fd = -1; |
| 69 |
} |
| 70 |
|
| 71 |
srv_sync->signaled = signaled; |
| 72 |
} |
| 73 |
|
| 74 |
void pvr_srv_set_sync_payload(struct pvr_srv_sync *srv_sync, int payload) |
| 75 |
{ |
| 76 |
if (srv_sync->fd != -1) |
| 77 |
close(srv_sync->fd); |
| 78 |
|
| 79 |
srv_sync->fd = payload; |
| 80 |
srv_sync->signaled = (payload == -1); |
| 81 |
} |
| 82 |
|
| 83 |
static VkResult pvr_srv_sync_signal(struct vk_device *device, |
| 84 |
struct vk_sync *sync, |
| 85 |
UNUSED uint64_t value) |
| 86 |
{ |
| 87 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 88 |
|
| 89 |
pvr_set_sync_state(srv_sync, true); |
| 90 |
|
| 91 |
return VK_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
static VkResult pvr_srv_sync_reset(struct vk_device *device, |
| 95 |
struct vk_sync *sync) |
| 96 |
{ |
| 97 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 98 |
|
| 99 |
pvr_set_sync_state(srv_sync, false); |
| 100 |
|
| 101 |
return VK_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
static inline int pvr_get_relative_time_ms(uint64_t abs_timeout_ns) |
| 105 |
{ |
| 106 |
uint64_t cur_time_ms; |
| 107 |
uint64_t abs_timeout_ms; |
| 108 |
|
| 109 |
if (abs_timeout_ns >= INT64_MAX) { |
| 110 |
/* This is treated as an infinite wait */ |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
cur_time_ms = os_time_get_nano() / 1000000; |
| 115 |
abs_timeout_ms = abs_timeout_ns / 1000000; |
| 116 |
|
| 117 |
if (abs_timeout_ms <= cur_time_ms) |
| 118 |
return 0; |
| 119 |
|
| 120 |
return MIN2(abs_timeout_ms - cur_time_ms, INT_MAX); |
| 121 |
} |
| 122 |
|
| 123 |
/* pvr_srv_sync can have pending state, which means they would need spin waits |
| 124 |
*/ |
| 125 |
static VkResult pvr_srv_sync_get_status(struct vk_sync *wait, |
| 126 |
uint64_t abs_timeout_ns) |
| 127 |
{ |
| 128 |
struct pvr_srv_sync *srv_sync = to_srv_sync(wait); |
| 129 |
|
| 130 |
if (srv_sync->signaled) { |
| 131 |
assert(srv_sync->fd == -1); |
| 132 |
return VK_SUCCESS; |
| 133 |
} |
| 134 |
|
| 135 |
/* If fd is -1 and this is not signaled, the fence is in pending mode */ |
| 136 |
if (srv_sync->fd == -1) |
| 137 |
return VK_TIMEOUT; |
| 138 |
|
| 139 |
if (sync_wait(srv_sync->fd, pvr_get_relative_time_ms(abs_timeout_ns))) { |
| 140 |
if (errno == ETIME) |
| 141 |
return VK_TIMEOUT; |
| 142 |
else if (errno == ENOMEM) |
| 143 |
return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 144 |
else |
| 145 |
return vk_error(NULL, VK_ERROR_DEVICE_LOST); |
| 146 |
} |
| 147 |
|
| 148 |
pvr_set_sync_state(srv_sync, true); |
| 149 |
|
| 150 |
return VK_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
/* abs_timeout_ns == 0 -> Get status without waiting. |
| 154 |
* abs_timeout_ns == ~0 -> Wait infinitely. |
| 155 |
* else wait for the given abs_timeout_ns in nanoseconds. */ |
| 156 |
static VkResult pvr_srv_sync_wait_many(struct vk_device *device, |
| 157 |
uint32_t wait_count, |
| 158 |
const struct vk_sync_wait *waits, |
| 159 |
enum vk_sync_wait_flags wait_flags, |
| 160 |
uint64_t abs_timeout_ns) |
| 161 |
{ |
| 162 |
bool wait_any = !!(wait_flags & VK_SYNC_WAIT_ANY); |
| 163 |
|
| 164 |
while (true) { |
| 165 |
bool have_unsignaled = false; |
| 166 |
|
| 167 |
for (uint32_t i = 0; i < wait_count; i++) { |
| 168 |
VkResult result = |
| 169 |
pvr_srv_sync_get_status(waits[i].sync, |
| 170 |
wait_any ? 0 : abs_timeout_ns); |
| 171 |
|
| 172 |
if (result != VK_TIMEOUT && (wait_any || result != VK_SUCCESS)) |
| 173 |
return result; |
| 174 |
else if (result == VK_TIMEOUT) |
| 175 |
have_unsignaled = true; |
| 176 |
} |
| 177 |
|
| 178 |
if (!have_unsignaled) |
| 179 |
break; |
| 180 |
|
| 181 |
if (os_time_get_nano() >= abs_timeout_ns) |
| 182 |
return VK_TIMEOUT; |
| 183 |
|
| 184 |
/* TODO: Use pvrsrvkm global events to stop busy waiting and for a bonus |
| 185 |
* catch device loss. |
| 186 |
*/ |
| 187 |
sched_yield(); |
| 188 |
} |
| 189 |
|
| 190 |
return VK_SUCCESS; |
| 191 |
} |
| 192 |
|
| 193 |
static VkResult pvr_srv_sync_move(struct vk_device *device, |
| 194 |
struct vk_sync *dst, |
| 195 |
struct vk_sync *src) |
| 196 |
{ |
| 197 |
struct pvr_srv_sync *srv_dst_sync = to_srv_sync(dst); |
| 198 |
struct pvr_srv_sync *srv_src_sync = to_srv_sync(src); |
| 199 |
|
| 200 |
if (!(dst->flags & VK_SYNC_IS_SHARED) && !(src->flags & VK_SYNC_IS_SHARED)) { |
| 201 |
pvr_srv_set_sync_payload(srv_dst_sync, srv_src_sync->fd); |
| 202 |
srv_src_sync->fd = -1; |
| 203 |
pvr_srv_sync_reset(device, src); |
| 204 |
return VK_SUCCESS; |
| 205 |
} |
| 206 |
|
| 207 |
UNREACHABLE("srv_sync doesn't support move for shared sync objects."); |
| 208 |
return VK_ERROR_UNKNOWN; |
| 209 |
} |
| 210 |
|
| 211 |
static VkResult pvr_srv_sync_import_sync_file(struct vk_device *device, |
| 212 |
struct vk_sync *sync, |
| 213 |
int sync_file) |
| 214 |
{ |
| 215 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 216 |
int fd = -1; |
| 217 |
|
| 218 |
if (sync_file >= 0) { |
| 219 |
fd = os_dupfd_cloexec(sync_file); |
| 220 |
if (fd < 0) |
| 221 |
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 222 |
} |
| 223 |
|
| 224 |
pvr_srv_set_sync_payload(srv_sync, fd); |
| 225 |
|
| 226 |
return VK_SUCCESS; |
| 227 |
} |
| 228 |
|
| 229 |
static VkResult pvr_srv_sync_export_sync_file(struct vk_device *device, |
| 230 |
struct vk_sync *sync, |
| 231 |
int *sync_file) |
| 232 |
{ |
| 233 |
struct pvr_srv_sync *srv_sync = to_srv_sync(sync); |
| 234 |
VkResult result; |
| 235 |
int fd; |
| 236 |
|
| 237 |
if (srv_sync->fd < 0) { |
| 238 |
struct pvr_device *driver_device = |
| 239 |
container_of(device, struct pvr_device, vk); |
| 240 |
|
| 241 |
result = pvr_srv_sync_get_presignaled_sync(driver_device, &srv_sync); |
| 242 |
if (result != VK_SUCCESS) |
| 243 |
return result; |
| 244 |
} |
| 245 |
|
| 246 |
assert(srv_sync->fd >= 0); |
| 247 |
|
| 248 |
fd = os_dupfd_cloexec(srv_sync->fd); |
| 249 |
if (fd < 0) |
| 250 |
return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 251 |
|
| 252 |
*sync_file = fd; |
| 253 |
|
| 254 |
return VK_SUCCESS; |
| 255 |
} |
| 256 |
|
| 257 |
const struct vk_sync_type pvr_srv_sync_type = { |
| 258 |
.size = sizeof(struct pvr_srv_sync), |
| 259 |
/* clang-format off */ |
| 260 |
.features = VK_SYNC_FEATURE_BINARY | |
| 261 |
VK_SYNC_FEATURE_GPU_WAIT | |
| 262 |
VK_SYNC_FEATURE_GPU_MULTI_WAIT | |
| 263 |
VK_SYNC_FEATURE_CPU_WAIT | |
| 264 |
VK_SYNC_FEATURE_CPU_RESET | |
| 265 |
VK_SYNC_FEATURE_CPU_SIGNAL | |
| 266 |
VK_SYNC_FEATURE_WAIT_ANY, |
| 267 |
/* clang-format on */ |
| 268 |
.init = pvr_srv_sync_init, |
| 269 |
.finish = pvr_srv_sync_finish, |
| 270 |
.signal = pvr_srv_sync_signal, |
| 271 |
.reset = pvr_srv_sync_reset, |
| 272 |
.wait_many = pvr_srv_sync_wait_many, |
| 273 |
.move = pvr_srv_sync_move, |
| 274 |
.import_sync_file = pvr_srv_sync_import_sync_file, |
| 275 |
.export_sync_file = pvr_srv_sync_export_sync_file, |
| 276 |
}; |