| 1 |
/* |
| 2 |
* Copyright © 2021 Collabora Ltd. |
| 3 |
* |
| 4 |
* Derived from tu_device.c which is: |
| 5 |
* Copyright © 2016 Red Hat. |
| 6 |
* Copyright © 2016 Bas Nieuwenhuizen |
| 7 |
* Copyright © 2015 Intel Corporation |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: MIT |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "genxml/gen_macros.h" |
| 13 |
|
| 14 |
#include "decode.h" |
| 15 |
|
| 16 |
#include "panvk_cmd_buffer.h" |
| 17 |
#include "panvk_device.h" |
| 18 |
#include "panvk_entrypoints.h" |
| 19 |
#include "panvk_event.h" |
| 20 |
#include "panvk_image.h" |
| 21 |
#include "panvk_image_view.h" |
| 22 |
#include "panvk_instance.h" |
| 23 |
#include "panvk_physical_device.h" |
| 24 |
#include "panvk_priv_bo.h" |
| 25 |
#include "panvk_queue.h" |
| 26 |
|
| 27 |
#include "vk_drm_syncobj.h" |
| 28 |
#include "vk_framebuffer.h" |
| 29 |
|
| 30 |
#include "drm-uapi/panfrost_drm.h" |
| 31 |
|
| 32 |
static void |
| 33 |
panvk_queue_submit_batch(struct panvk_gpu_queue *queue, |
| 34 |
struct panvk_cmd_buffer *cmdbuf, |
| 35 |
struct panvk_batch *batch, uint32_t *bos, |
| 36 |
unsigned nr_bos, uint32_t *in_fences, |
| 37 |
unsigned nr_in_fences) |
| 38 |
{ |
| 39 |
struct panvk_device *dev = to_panvk_device(queue->vk.base.device); |
| 40 |
struct panvk_physical_device *phys_dev = |
| 41 |
to_panvk_physical_device(dev->vk.physical); |
| 42 |
ASSERTED int ret; |
| 43 |
|
| 44 |
/* Reset the batch if it's already been issued */ |
| 45 |
if (batch->issued) { |
| 46 |
util_dynarray_foreach(&batch->jobs, void *, job) |
| 47 |
memset((*job), 0, 4 * 4); |
| 48 |
|
| 49 |
/* Reset the tiler before re-issuing the batch */ |
| 50 |
if (batch->tiler.ctx_descs.cpu) { |
| 51 |
memcpy(batch->tiler.heap_desc.cpu, &batch->tiler.heap_templ, |
| 52 |
sizeof(batch->tiler.heap_templ)); |
| 53 |
|
| 54 |
struct mali_tiler_context_packed *ctxs = batch->tiler.ctx_descs.cpu; |
| 55 |
|
| 56 |
for (uint32_t i = 0; i < batch->fb.layer_count; i++) |
| 57 |
memcpy(&ctxs[i], &batch->tiler.ctx_templ, sizeof(*ctxs)); |
| 58 |
} |
| 59 |
|
| 60 |
/* We don't keep track of BO <-> job relationship, so let's just flush the |
| 61 |
* whole desc pool for now. */ |
| 62 |
panvk_pool_flush_maps(&cmdbuf->desc_pool); |
| 63 |
} |
| 64 |
|
| 65 |
/* Flush pending synchronization requests before submitting the job, to |
| 66 |
* make sure things are GPU-visible. */ |
| 67 |
pan_kmod_flush_bo_map_syncs(dev->kmod.dev); |
| 68 |
|
| 69 |
if (batch->vtc_jc.first_job) { |
| 70 |
struct drm_panfrost_submit submit = { |
| 71 |
.bo_handles = (uintptr_t)bos, |
| 72 |
.bo_handle_count = nr_bos, |
| 73 |
.in_syncs = (uintptr_t)in_fences, |
| 74 |
.in_sync_count = nr_in_fences, |
| 75 |
.out_sync = queue->sync, |
| 76 |
.jc = batch->vtc_jc.first_job, |
| 77 |
}; |
| 78 |
|
| 79 |
ret = pan_kmod_ioctl(dev->drm_fd, DRM_IOCTL_PANFROST_SUBMIT, &submit); |
| 80 |
assert(!ret); |
| 81 |
|
| 82 |
if (PANVK_DEBUG(TRACE) || PANVK_DEBUG(SYNC)) { |
| 83 |
ret = drmSyncobjWait(dev->drm_fd, &submit.out_sync, 1, INT64_MAX, 0, |
| 84 |
NULL); |
| 85 |
assert(!ret); |
| 86 |
|
| 87 |
/* If we want to read the descriptors back, we need to invalidate the |
| 88 |
* whole desc pool, otherwise we might end up with stale data. */ |
| 89 |
panvk_pool_invalidate_maps(&cmdbuf->desc_pool); |
| 90 |
pan_kmod_flush_bo_map_syncs(dev->kmod.dev); |
| 91 |
} |
| 92 |
|
| 93 |
if (PANVK_DEBUG(TRACE)) { |
| 94 |
pandecode_jc(dev->debug.decode_ctx, batch->vtc_jc.first_job, |
| 95 |
phys_dev->kmod.dev->props.gpu_id); |
| 96 |
} |
| 97 |
|
| 98 |
if (PANVK_DEBUG(DUMP)) |
| 99 |
pandecode_dump_mappings(dev->debug.decode_ctx); |
| 100 |
|
| 101 |
if (PANVK_DEBUG(SYNC)) |
| 102 |
pandecode_abort_on_fault(dev->debug.decode_ctx, submit.jc, |
| 103 |
phys_dev->kmod.dev->props.gpu_id); |
| 104 |
} |
| 105 |
|
| 106 |
if (batch->frag_jc.first_job) { |
| 107 |
struct drm_panfrost_submit submit = { |
| 108 |
.bo_handles = (uintptr_t)bos, |
| 109 |
.bo_handle_count = nr_bos, |
| 110 |
.out_sync = queue->sync, |
| 111 |
.jc = batch->frag_jc.first_job, |
| 112 |
.requirements = PANFROST_JD_REQ_FS, |
| 113 |
}; |
| 114 |
|
| 115 |
if (batch->vtc_jc.first_job) { |
| 116 |
submit.in_syncs = (uintptr_t)(&queue->sync); |
| 117 |
submit.in_sync_count = 1; |
| 118 |
} else { |
| 119 |
submit.in_syncs = (uintptr_t)in_fences; |
| 120 |
submit.in_sync_count = nr_in_fences; |
| 121 |
} |
| 122 |
|
| 123 |
ret = pan_kmod_ioctl(dev->drm_fd, DRM_IOCTL_PANFROST_SUBMIT, &submit); |
| 124 |
assert(!ret); |
| 125 |
if (PANVK_DEBUG(TRACE) || PANVK_DEBUG(SYNC)) { |
| 126 |
ret = drmSyncobjWait(dev->drm_fd, &submit.out_sync, 1, INT64_MAX, 0, |
| 127 |
NULL); |
| 128 |
assert(!ret); |
| 129 |
|
| 130 |
/* If we want to read the descriptors back, we need to invalidate the |
| 131 |
* whole desc pool, otherwise we might end up with stale data. */ |
| 132 |
panvk_pool_invalidate_maps(&cmdbuf->desc_pool); |
| 133 |
pan_kmod_flush_bo_map_syncs(dev->kmod.dev); |
| 134 |
} |
| 135 |
|
| 136 |
if (PANVK_DEBUG(TRACE)) |
| 137 |
pandecode_jc(dev->debug.decode_ctx, batch->frag_jc.first_job, |
| 138 |
phys_dev->kmod.dev->props.gpu_id); |
| 139 |
|
| 140 |
if (PANVK_DEBUG(DUMP)) |
| 141 |
pandecode_dump_mappings(dev->debug.decode_ctx); |
| 142 |
|
| 143 |
if (PANVK_DEBUG(SYNC)) |
| 144 |
pandecode_abort_on_fault(dev->debug.decode_ctx, submit.jc, |
| 145 |
phys_dev->kmod.dev->props.gpu_id); |
| 146 |
} |
| 147 |
|
| 148 |
if (PANVK_DEBUG(TRACE)) |
| 149 |
pandecode_next_frame(dev->debug.decode_ctx); |
| 150 |
|
| 151 |
batch->issued = true; |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
panvk_queue_transfer_sync(struct panvk_gpu_queue *queue, uint32_t syncobj) |
| 156 |
{ |
| 157 |
struct panvk_device *dev = to_panvk_device(queue->vk.base.device); |
| 158 |
ASSERTED int ret; |
| 159 |
|
| 160 |
struct drm_syncobj_handle handle = { |
| 161 |
.handle = queue->sync, |
| 162 |
.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE, |
| 163 |
.fd = -1, |
| 164 |
}; |
| 165 |
|
| 166 |
ret = pan_kmod_ioctl(dev->drm_fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &handle); |
| 167 |
assert(!ret); |
| 168 |
assert(handle.fd >= 0); |
| 169 |
|
| 170 |
handle.handle = syncobj; |
| 171 |
ret = pan_kmod_ioctl(dev->drm_fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &handle); |
| 172 |
assert(!ret); |
| 173 |
|
| 174 |
close(handle.fd); |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
panvk_add_wait_event_syncobjs(struct panvk_batch *batch, uint32_t *in_fences, |
| 179 |
unsigned *nr_in_fences) |
| 180 |
{ |
| 181 |
util_dynarray_foreach(&batch->event_ops, struct panvk_cmd_event_op, op) { |
| 182 |
switch (op->type) { |
| 183 |
case PANVK_EVENT_OP_SET: |
| 184 |
/* Nothing to do yet */ |
| 185 |
break; |
| 186 |
case PANVK_EVENT_OP_RESET: |
| 187 |
/* Nothing to do yet */ |
| 188 |
break; |
| 189 |
case PANVK_EVENT_OP_WAIT: |
| 190 |
in_fences[(*nr_in_fences)++] = op->event->syncobj; |
| 191 |
break; |
| 192 |
default: |
| 193 |
UNREACHABLE("bad panvk_cmd_event_op type\n"); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
panvk_signal_event_syncobjs(struct panvk_gpu_queue *queue, |
| 200 |
struct panvk_batch *batch) |
| 201 |
{ |
| 202 |
struct panvk_device *dev = to_panvk_device(queue->vk.base.device); |
| 203 |
|
| 204 |
util_dynarray_foreach(&batch->event_ops, struct panvk_cmd_event_op, op) { |
| 205 |
switch (op->type) { |
| 206 |
case PANVK_EVENT_OP_SET: { |
| 207 |
panvk_queue_transfer_sync(queue, op->event->syncobj); |
| 208 |
break; |
| 209 |
} |
| 210 |
case PANVK_EVENT_OP_RESET: { |
| 211 |
struct panvk_event *event = op->event; |
| 212 |
|
| 213 |
struct drm_syncobj_array objs = { |
| 214 |
.handles = (uint64_t)(uintptr_t)&event->syncobj, |
| 215 |
.count_handles = 1}; |
| 216 |
|
| 217 |
ASSERTED int ret = pan_kmod_ioctl(dev->drm_fd, |
| 218 |
DRM_IOCTL_SYNCOBJ_RESET, &objs); |
| 219 |
assert(!ret); |
| 220 |
break; |
| 221 |
} |
| 222 |
case PANVK_EVENT_OP_WAIT: |
| 223 |
/* Nothing left to do */ |
| 224 |
break; |
| 225 |
default: |
| 226 |
UNREACHABLE("bad panvk_cmd_event_op type\n"); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
VkResult |
| 232 |
panvk_per_arch(gpu_queue_submit)(struct vk_queue *vk_queue, struct vk_queue_submit *submit) |
| 233 |
{ |
| 234 |
struct panvk_gpu_queue *queue = container_of(vk_queue, struct panvk_gpu_queue, vk); |
| 235 |
struct panvk_device *dev = to_panvk_device(queue->vk.base.device); |
| 236 |
|
| 237 |
unsigned nr_semaphores = submit->wait_count + 1; |
| 238 |
uint32_t semaphores[nr_semaphores]; |
| 239 |
|
| 240 |
semaphores[0] = queue->sync; |
| 241 |
for (unsigned i = 0; i < submit->wait_count; i++) { |
| 242 |
assert(vk_sync_type_is_drm_syncobj(submit->waits[i].sync->type)); |
| 243 |
struct vk_drm_syncobj *syncobj = |
| 244 |
vk_sync_as_drm_syncobj(submit->waits[i].sync); |
| 245 |
|
| 246 |
semaphores[i + 1] = syncobj->syncobj; |
| 247 |
} |
| 248 |
|
| 249 |
for (uint32_t j = 0; j < submit->command_buffer_count; ++j) { |
| 250 |
struct panvk_cmd_buffer *cmdbuf = |
| 251 |
container_of(submit->command_buffers[j], struct panvk_cmd_buffer, vk); |
| 252 |
|
| 253 |
list_for_each_entry(struct panvk_batch, batch, &cmdbuf->batches, node) { |
| 254 |
/* FIXME: should be done at the batch level */ |
| 255 |
unsigned nr_bos = panvk_pool_num_bos(&cmdbuf->desc_pool) + |
| 256 |
panvk_pool_num_bos(&cmdbuf->varying_pool) + |
| 257 |
panvk_pool_num_bos(&cmdbuf->tls_pool) + |
| 258 |
batch->fb.bo_count + (batch->blit.src ? 1 : 0) + |
| 259 |
(batch->blit.dst ? 1 : 0) + |
| 260 |
(batch->vtc_jc.first_tiler ? 1 : 0) + 1; |
| 261 |
unsigned bo_idx = 0; |
| 262 |
uint32_t bos[nr_bos]; |
| 263 |
|
| 264 |
panvk_pool_get_bo_handles(&cmdbuf->desc_pool, &bos[bo_idx]); |
| 265 |
bo_idx += panvk_pool_num_bos(&cmdbuf->desc_pool); |
| 266 |
|
| 267 |
panvk_pool_get_bo_handles(&cmdbuf->varying_pool, &bos[bo_idx]); |
| 268 |
bo_idx += panvk_pool_num_bos(&cmdbuf->varying_pool); |
| 269 |
|
| 270 |
panvk_pool_get_bo_handles(&cmdbuf->tls_pool, &bos[bo_idx]); |
| 271 |
bo_idx += panvk_pool_num_bos(&cmdbuf->tls_pool); |
| 272 |
|
| 273 |
for (unsigned i = 0; i < batch->fb.bo_count; i++) |
| 274 |
bos[bo_idx++] = pan_kmod_bo_handle(batch->fb.bos[i]); |
| 275 |
|
| 276 |
if (batch->blit.src) |
| 277 |
bos[bo_idx++] = pan_kmod_bo_handle(batch->blit.src); |
| 278 |
|
| 279 |
if (batch->blit.dst) |
| 280 |
bos[bo_idx++] = pan_kmod_bo_handle(batch->blit.dst); |
| 281 |
|
| 282 |
if (batch->vtc_jc.first_tiler) |
| 283 |
bos[bo_idx++] = pan_kmod_bo_handle(dev->tiler_heap->bo); |
| 284 |
|
| 285 |
bos[bo_idx++] = pan_kmod_bo_handle(dev->sample_positions->bo); |
| 286 |
assert(bo_idx == nr_bos); |
| 287 |
|
| 288 |
/* Merge identical BO entries. */ |
| 289 |
for (unsigned x = 0; x < nr_bos; x++) { |
| 290 |
for (unsigned y = x + 1; y < nr_bos;) { |
| 291 |
if (bos[x] == bos[y]) |
| 292 |
bos[y] = bos[--nr_bos]; |
| 293 |
else |
| 294 |
y++; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
unsigned nr_in_fences = 0; |
| 299 |
unsigned max_wait_event_syncobjs = util_dynarray_num_elements( |
| 300 |
&batch->event_ops, struct panvk_cmd_event_op); |
| 301 |
uint32_t in_fences[nr_semaphores + max_wait_event_syncobjs]; |
| 302 |
memcpy(in_fences, semaphores, nr_semaphores * sizeof(*in_fences)); |
| 303 |
nr_in_fences += nr_semaphores; |
| 304 |
|
| 305 |
panvk_add_wait_event_syncobjs(batch, in_fences, &nr_in_fences); |
| 306 |
|
| 307 |
panvk_queue_submit_batch(queue, cmdbuf, batch, bos, nr_bos, in_fences, |
| 308 |
nr_in_fences); |
| 309 |
|
| 310 |
panvk_signal_event_syncobjs(queue, batch); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/* Transfer the out fence to signal semaphores */ |
| 315 |
for (unsigned i = 0; i < submit->signal_count; i++) { |
| 316 |
assert(vk_sync_type_is_drm_syncobj(submit->signals[i].sync->type)); |
| 317 |
struct vk_drm_syncobj *syncobj = |
| 318 |
vk_sync_as_drm_syncobj(submit->signals[i].sync); |
| 319 |
|
| 320 |
panvk_queue_transfer_sync(queue, syncobj->syncobj); |
| 321 |
} |
| 322 |
|
| 323 |
return VK_SUCCESS; |
| 324 |
} |
| 325 |
|
| 326 |
VkResult |
| 327 |
panvk_per_arch(create_gpu_queue)(struct panvk_device *device, |
| 328 |
const VkDeviceQueueCreateInfo *create_info, |
| 329 |
uint32_t queue_idx, |
| 330 |
struct vk_queue **out_queue) |
| 331 |
{ |
| 332 |
ASSERTED const VkDeviceQueueGlobalPriorityCreateInfoKHR *priority_info = |
| 333 |
vk_find_struct_const(create_info->pNext, |
| 334 |
DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_KHR); |
| 335 |
ASSERTED const VkQueueGlobalPriorityKHR priority = |
| 336 |
priority_info ? priority_info->globalPriority |
| 337 |
: VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR; |
| 338 |
|
| 339 |
/* XXX: Panfrost kernel module doesn't support priorities so far */ |
| 340 |
assert(priority == VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR); |
| 341 |
|
| 342 |
struct panvk_gpu_queue *queue = vk_zalloc(&device->vk.alloc, sizeof(*queue), 8, |
| 343 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 344 |
if (!queue) |
| 345 |
return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 346 |
|
| 347 |
VkResult result = |
| 348 |
vk_queue_init(&queue->vk, &device->vk, create_info, queue_idx); |
| 349 |
if (result != VK_SUCCESS) |
| 350 |
goto err_free_queue; |
| 351 |
|
| 352 |
int ret = drmSyncobjCreate(device->drm_fd, DRM_SYNCOBJ_CREATE_SIGNALED, |
| 353 |
&queue->sync); |
| 354 |
if (ret) { |
| 355 |
result = panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 356 |
goto err_finish_queue; |
| 357 |
} |
| 358 |
|
| 359 |
queue->vk.driver_submit = panvk_per_arch(gpu_queue_submit); |
| 360 |
*out_queue = &queue->vk; |
| 361 |
return VK_SUCCESS; |
| 362 |
|
| 363 |
err_finish_queue: |
| 364 |
vk_queue_finish(&queue->vk); |
| 365 |
|
| 366 |
err_free_queue: |
| 367 |
vk_free(&device->vk.alloc, queue); |
| 368 |
return result; |
| 369 |
} |
| 370 |
|
| 371 |
void panvk_per_arch(destroy_gpu_queue)(struct vk_queue *vk_queue) |
| 372 |
{ |
| 373 |
struct panvk_gpu_queue *queue = container_of(vk_queue, struct panvk_gpu_queue, vk); |
| 374 |
struct panvk_device *dev = to_panvk_device(vk_queue->base.device); |
| 375 |
|
| 376 |
vk_queue_finish(&queue->vk); |
| 377 |
drmSyncobjDestroy(dev->drm_fd, queue->sync); |
| 378 |
vk_free(&dev->vk.alloc, queue); |
| 379 |
} |
| 380 |
|
| 381 |
VkResult |
| 382 |
panvk_per_arch(gpu_queue_check_status)(struct vk_queue *vk_queue) |
| 383 |
{ |
| 384 |
return VK_SUCCESS; |
| 385 |
} |
| 386 |
|
| 387 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 388 |
panvk_per_arch(QueueWaitIdle)(VkQueue _queue) |
| 389 |
{ |
| 390 |
VK_FROM_HANDLE(panvk_gpu_queue, queue, _queue); |
| 391 |
struct panvk_device *dev = to_panvk_device(queue->vk.base.device); |
| 392 |
|
| 393 |
/* we need to use vk_common_QueueWaitIdle if we ever go threaded */ |
| 394 |
assert(queue->vk.submit.mode != VK_QUEUE_SUBMIT_MODE_THREADED); |
| 395 |
|
| 396 |
if (vk_device_is_lost(&dev->vk)) { |
| 397 |
/* Check printf buffer one more time before exiting */ |
| 398 |
u_printf_with_ctx(stdout, &dev->printf.ctx); |
| 399 |
return VK_ERROR_DEVICE_LOST; |
| 400 |
} |
| 401 |
|
| 402 |
ASSERTED int ret = drmSyncobjWait(dev->drm_fd, &queue->sync, 1, |
| 403 |
INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, |
| 404 |
NULL); |
| 405 |
assert(!ret); |
| 406 |
|
| 407 |
return VK_SUCCESS; |
| 408 |
} |