Debian Codegraph @

src/panfrost/vulkan/csf/panvk_vX_bind_queue.c

raw
1 /*
2 * Copyright © 2024 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "drm-uapi/panthor_drm.h"
7
8 #include "genxml/cs_builder.h"
9 #include "genxml/decode.h"
10
11 #include "panvk_buffer.h"
12 #include "panvk_cmd_buffer.h"
13 #include "panvk_device_memory.h"
14 #include "panvk_macros.h"
15 #include "panvk_queue.h"
16 #include "panvk_utrace.h"
17 #include "panvk_sparse.h"
18 #include "pan_layout.h"
19
20 #include "util/bitscan.h"
21 #include "vk_drm_syncobj.h"
22 #include "vk_log.h"
23
24 struct panvk_bind_queue_submit_sync_ops {
25 struct drm_panthor_sync_op *all;
26 size_t all_count;
27 struct drm_panthor_sync_op *waits;
28 size_t wait_count;
29 struct drm_panthor_sync_op *signals;
30 size_t signal_count;
31
32 struct drm_panthor_sync_op small_storage[4];
33 };
34
35 static void
36 panvk_bind_queue_submit_sync_ops_init(
37 struct panvk_bind_queue_submit_sync_ops *sync_ops,
38 const struct vk_queue_submit *vk_submit,
39 const struct drm_panthor_sync_op *extra_signal)
40 {
41 size_t signal_count = vk_submit->signal_count;
42 if (extra_signal)
43 signal_count++;
44
45 size_t all_count = vk_submit->wait_count + signal_count;
46
47 sync_ops->all = all_count <= ARRAY_SIZE(sync_ops->small_storage)
48 ? sync_ops->small_storage
49 : malloc(sizeof(*sync_ops->all) * all_count);
50 sync_ops->all_count = all_count;
51
52 sync_ops->waits = sync_ops->all;
53 sync_ops->wait_count = vk_submit->wait_count;
54
55 sync_ops->signals = sync_ops->all + vk_submit->wait_count;
56 sync_ops->signal_count = signal_count;
57
58 for (uint32_t i = 0; i < vk_submit->wait_count; i++) {
59 const struct vk_sync_wait *wait = &vk_submit->waits[i];
60 const struct vk_drm_syncobj *syncobj = vk_sync_as_drm_syncobj(wait->sync);
61 assert(syncobj);
62
63 sync_ops->waits[i] = (struct drm_panthor_sync_op){
64 .flags = (syncobj->base.flags & VK_SYNC_IS_TIMELINE
65 ? DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ
66 : DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ) |
67 DRM_PANTHOR_SYNC_OP_WAIT,
68 .handle = syncobj->syncobj,
69 .timeline_value = wait->wait_value,
70 };
71 }
72
73 uint32_t signal_idx = 0;
74 for (uint32_t i = 0; i < vk_submit->signal_count; i++) {
75 const struct vk_sync_signal *signal = &vk_submit->signals[i];
76 const struct vk_drm_syncobj *syncobj =
77 vk_sync_as_drm_syncobj(signal->sync);
78 assert(syncobj);
79
80 sync_ops->signals[signal_idx++] = (struct drm_panthor_sync_op){
81 .flags = (syncobj->base.flags & VK_SYNC_IS_TIMELINE
82 ? DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ
83 : DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ) |
84 DRM_PANTHOR_SYNC_OP_SIGNAL,
85 .handle = syncobj->syncobj,
86 .timeline_value = signal->signal_value,
87 };
88 }
89 if (extra_signal)
90 sync_ops->signals[signal_idx++] = *extra_signal;
91 assert(signal_idx == signal_count);
92 }
93
94 static void
95 panvk_bind_queue_submit_sync_ops_cleanup(
96 struct panvk_bind_queue_submit_sync_ops *sync_ops)
97 {
98 if (sync_ops->all != sync_ops->small_storage)
99 free(sync_ops->all);
100 }
101
102 struct panvk_bind_queue_submit {
103 struct panvk_bind_queue *queue;
104
105 bool force_sync;
106
107 struct panvk_bind_queue_submit_sync_ops sync_ops;
108
109 struct drm_panthor_vm_bind_op *bind_ops;
110 size_t bind_op_count; /* number of bind ops buffered */
111 size_t bind_op_cap; /* size of the bind op buffer */
112
113 struct drm_panthor_vm_bind_op bind_op_small_storage[16];
114 };
115
116 static void
117 panvk_bind_queue_submit_init(struct panvk_bind_queue_submit *submit,
118 struct vk_queue *vk_queue,
119 struct vk_queue_submit *vk_submit)
120 {
121 struct panvk_bind_queue *queue =
122 container_of(vk_queue, struct panvk_bind_queue, vk);
123
124 const bool force_sync = PANVK_DEBUG(SYNC);
125
126 *submit = (struct panvk_bind_queue_submit){
127 .queue = queue,
128 .force_sync = force_sync,
129 };
130
131 struct drm_panthor_sync_op syncobj_signal = {
132 .flags =
133 DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_SYNCOBJ | DRM_PANTHOR_SYNC_OP_SIGNAL,
134 .handle = queue->syncobj_handle,
135 };
136
137 panvk_bind_queue_submit_sync_ops_init(&submit->sync_ops, vk_submit,
138 force_sync ? &syncobj_signal : NULL);
139
140 /* TODO: guess how many bind ops we'll need and allocate that much, unless
141 * it's too much
142 */
143 uint32_t bind_op_cap = ARRAY_SIZE(submit->bind_op_small_storage);
144
145 submit->bind_ops = bind_op_cap <= ARRAY_SIZE(submit->bind_op_small_storage)
146 ? submit->bind_op_small_storage
147 : malloc(sizeof(*submit->bind_ops) * bind_op_cap);
148 submit->bind_op_cap = bind_op_cap;
149 }
150
151 static void
152 panvk_bind_queue_submit_cleanup(struct panvk_bind_queue_submit *submit)
153 {
154 panvk_bind_queue_submit_sync_ops_cleanup(&submit->sync_ops);
155
156 if (submit->bind_ops != submit->bind_op_small_storage)
157 free(submit->bind_ops);
158 }
159
160 static int
161 panvk_bind_queue_submit_flush(struct panvk_bind_queue_submit *submit)
162 {
163 struct panvk_device *dev = to_panvk_device(submit->queue->vk.base.device);
164
165 if (submit->bind_op_count == 0)
166 return 0;
167
168 struct drm_panthor_vm_bind req = {
169 .vm_id = dev->kmod.vm->handle,
170 .flags = DRM_PANTHOR_VM_BIND_ASYNC,
171 .ops = DRM_PANTHOR_OBJ_ARRAY(submit->bind_op_count, submit->bind_ops),
172 };
173 int ret = pan_kmod_ioctl(dev->drm_fd, DRM_IOCTL_PANTHOR_VM_BIND, &req);
174 submit->bind_op_count = 0;
175 return ret;
176 }
177
178 static bool
179 panvk_try_merge_vm_bind_ops(struct drm_panthor_vm_bind_op *a,
180 const struct drm_panthor_vm_bind_op *b)
181 {
182 if (a->flags != b->flags)
183 return false;
184
185 /* See panvk_bind_queue_submit_vm_bind */
186 assert(b->syncs.count == 0);
187
188 enum drm_panthor_vm_bind_op_flags op_type = a->flags & DRM_PANTHOR_VM_BIND_OP_TYPE_MASK;
189 if (op_type != DRM_PANTHOR_VM_BIND_OP_TYPE_MAP &&
190 op_type != DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP)
191 return false;
192
193 if (a->va + a->size != b->va)
194 return false;
195
196 if (op_type == DRM_PANTHOR_VM_BIND_OP_TYPE_MAP &&
197 (a->bo_handle != b->bo_handle ||
198 a->bo_offset + a->size != b->bo_offset))
199 return false;
200
201 a->size += b->size;
202 return true;
203 }
204
205 static int
206 panvk_bind_queue_submit_vm_bind(
207 struct panvk_bind_queue_submit *submit,
208 const struct drm_panthor_vm_bind_op *op)
209 {
210 /* We handle all of the syncs here and in
211 * panvk_bind_queue_submit_process_signals */
212 assert(op->syncs.count == 0);
213
214 if (submit->bind_op_count > 0) {
215 struct drm_panthor_vm_bind_op *prev = &submit->bind_ops[submit->bind_op_count - 1];
216 if (panvk_try_merge_vm_bind_ops(prev, op))
217 return 0;
218 }
219
220 if (submit->bind_op_count == submit->bind_op_cap) {
221 int ret = panvk_bind_queue_submit_flush(submit);
222 if (ret)
223 return ret;
224 }
225
226 struct drm_panthor_vm_bind_op tmp = *op;
227 if (submit->sync_ops.wait_count > 0) {
228 tmp.syncs = (struct drm_panthor_obj_array)DRM_PANTHOR_OBJ_ARRAY(
229 submit->sync_ops.wait_count, submit->sync_ops.waits);
230 submit->sync_ops.wait_count = 0;
231 }
232
233 assert(submit->bind_op_count < submit->bind_op_cap);
234 submit->bind_ops[submit->bind_op_count++] = tmp;
235
236 return 0;
237 }
238
239 static int
240 panvk_bind_queue_submit_process_signals(struct panvk_bind_queue_submit *submit)
241 {
242 struct panvk_bind_queue *queue = submit->queue;
243 struct panvk_device *device =
244 to_panvk_device(queue->vk.base.device);
245
246 struct drm_panthor_vm_bind_op tmp = {
247 .flags = DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY,
248 };
249
250 struct drm_panthor_vm_bind_op *op = submit->bind_op_count > 0
251 ? &submit->bind_ops[submit->bind_op_count - 1]
252 : &tmp;
253
254 assert(!op->syncs.array ||
255 (op->syncs.array == (__u64)(uintptr_t)submit->sync_ops.waits &&
256 op->syncs.count > 0));
257
258 /* If we
259 * - have not processed waits (i.e. have not done any bind ops at all), or
260 * - the op we're about to make do signals already waits
261 *
262 * the op will be made to do all the sync ops.
263 */
264 op->syncs = submit->sync_ops.wait_count > 0 || op->syncs.array
265 ? (struct drm_panthor_obj_array)DRM_PANTHOR_OBJ_ARRAY(
266 submit->sync_ops.all_count, submit->sync_ops.all)
267 : (struct drm_panthor_obj_array)DRM_PANTHOR_OBJ_ARRAY(
268 submit->sync_ops.signal_count, submit->sync_ops.signals);
269
270 if (op == &tmp && op->syncs.count > 0) {
271 /* If we were filling out tmp, it wasn't in the bind op buffer. Now is a
272 * good time to plop it into there.
273 */
274 assert(submit->bind_op_count < submit->bind_op_cap);
275 submit->bind_ops[submit->bind_op_count++] = tmp;
276 }
277
278 int ret = panvk_bind_queue_submit_flush(submit);
279 if (ret)
280 return ret;
281
282 if (submit->force_sync) {
283 ASSERTED int ret = drmSyncobjWait(device->drm_fd,
284 &queue->syncobj_handle, 1, INT64_MAX,
285 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
286 assert(!ret);
287
288 drmSyncobjReset(device->drm_fd, &queue->syncobj_handle, 1);
289 }
290
291 return 0;
292 }
293
294 static int
295 panvk_bind_queue_submit_map_to_blackhole(
296 struct panvk_bind_queue_submit *submit,
297 uint64_t base_va, uint64_t size)
298 {
299 struct panvk_device *dev = to_panvk_device(submit->queue->vk.base.device);
300
301 struct pan_kmod_bo *blackhole = panvk_get_blackhole(dev);
302 uint64_t blackhole_size = blackhole->size;
303
304 uint64_t off = 0;
305 while (off < size) {
306 uint64_t bo_offset = base_va & (blackhole_size - 1);
307 uint64_t va = base_va + off;
308 uint64_t va_range = MIN2(blackhole_size - bo_offset, size - off);
309
310 struct drm_panthor_vm_bind_op op = {
311 .flags = DRM_PANTHOR_VM_BIND_OP_TYPE_MAP,
312 .bo_handle = blackhole->handle,
313 .bo_offset = bo_offset,
314 .va = va,
315 .size = va_range,
316 };
317 int ret = panvk_bind_queue_submit_vm_bind(submit, &op);
318 if (!ret)
319 return ret;
320
321 off += va_range;
322 }
323
324 return 0;
325 }
326
327 static int
328 panvk_bind_queue_submit_sparse_memory_bind(
329 struct panvk_bind_queue_submit *submit,
330 VkDeviceAddress resource_va, const VkSparseMemoryBind *in)
331 {
332 VK_FROM_HANDLE(panvk_device_memory, mem, in->memory);
333
334 if (mem) {
335 struct drm_panthor_vm_bind_op op = {
336 .flags = DRM_PANTHOR_VM_BIND_OP_TYPE_MAP,
337 .bo_handle = mem->bo->handle,
338 .bo_offset = in->memoryOffset,
339 .va = resource_va + in->resourceOffset,
340 .size = in->size,
341 };
342 return panvk_bind_queue_submit_vm_bind(submit, &op);
343 } else {
344 return panvk_bind_queue_submit_map_to_blackhole(submit, resource_va, in->size);
345 }
346 }
347
348 struct panvk_sparse_block_memory_bind {
349 uint32_t plane_index;
350 uint32_t layer;
351 uint32_t level;
352 VkOffset3D offset; /* must be a multiple of extent */
353 VkExtent3D extent;
354 VkDeviceMemory mem;
355 VkDeviceSize mem_offset;
356 };
357
358 static int
359 panvk_bind_queue_submit_sparse_block_memory_bind(
360 struct panvk_bind_queue_submit *submit,
361 const struct panvk_image *image,
362 const struct panvk_sparse_block_memory_bind *in)
363 {
364 uint64_t resource_va = image->sparse.device_address;
365 const struct panvk_image_plane *plane = &image->planes[in->plane_index];
366
367 /* Previously, sparse residency was implemented using block U-interleaved
368 * (https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37483).
369 * Interleaved 64k offers better map and unmap performance (at most one bind
370 * op per tile, as opposed to 4 to 16 with U-interleaved), and no worse
371 * access performance.
372 *
373 * Note that interleaved 64k is not a thing on pre-v10 hardware. If at any
374 * point there's a desire for sparse residency on pre-v10 hardware, resurrect
375 * the code linked above.
376 */
377 assert(image->vk.drm_format_mod == DRM_FORMAT_MOD_ARM_INTERLEAVED_64K);
378
379 struct pan_image_block_size tile_extent_el =
380 pan_interleaved_64k_tile_size_el(vk_format_to_pipe_format(image->vk.format));
381 struct pan_image_block_size tile_extent_px = {
382 tile_extent_el.width * vk_format_get_blockwidth(image->vk.format),
383 tile_extent_el.height * vk_format_get_blockheight(image->vk.format),
384 };
385 VkOffset3D offset_tiles = {
386 in->offset.x / tile_extent_px.width,
387 in->offset.y / tile_extent_px.height,
388 in->offset.z,
389 };
390 assert(in->extent.width == tile_extent_px.width &&
391 in->extent.height == tile_extent_px.height &&
392 in->extent.depth == 1);
393 uint32_t tile_size_B = 65536;
394
395 assert(image->vk.image_type != VK_IMAGE_TYPE_3D || in->layer == 0);
396
397 const struct pan_image_slice_layout *slayout = &plane->plane.layout.slices[in->level];
398 VkSparseMemoryBind bind = {
399 .resourceOffset =
400 in->layer * plane->plane.layout.array_stride_B +
401 slayout->offset_B +
402 offset_tiles.z * slayout->tiled_or_linear.surface_stride_B +
403 offset_tiles.y * slayout->tiled_or_linear.row_stride_B +
404 offset_tiles.x * tile_size_B,
405 .size = tile_size_B,
406 .memory = in->mem,
407 .memoryOffset = in->mem_offset,
408 };
409 return panvk_bind_queue_submit_sparse_memory_bind(submit, resource_va, &bind);
410 }
411
412 static int
413 panvk_bind_queue_submit_do(struct panvk_bind_queue_submit *submit,
414 const struct vk_queue_submit *vk_submit)
415 {
416 int ret;
417
418 for (uint32_t i = 0; i < vk_submit->buffer_bind_count; i++) {
419 VK_FROM_HANDLE(panvk_buffer, buf, vk_submit->buffer_binds[i].buffer);
420 assert(buf->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT);
421
422 uint64_t resource_va = buf->vk.device_address;
423
424 for (uint32_t j = 0; j < vk_submit->buffer_binds[i].bindCount; j++) {
425 const VkSparseMemoryBind *in = &vk_submit->buffer_binds[i].pBinds[j];
426
427 ret = panvk_bind_queue_submit_sparse_memory_bind(submit, resource_va, in);
428 if (ret)
429 return ret;
430 }
431 }
432 for (uint32_t i = 0; i < vk_submit->image_opaque_bind_count; i++) {
433 VK_FROM_HANDLE(panvk_image, image,
434 vk_submit->image_opaque_binds[i].image);
435 assert(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT);
436
437 uint64_t resource_va = image->sparse.device_address;
438
439 for (uint32_t j = 0; j < vk_submit->image_opaque_binds[i].bindCount;
440 j++) {
441 const VkSparseMemoryBind *in =
442 &vk_submit->image_opaque_binds[i].pBinds[j];
443
444 ret = panvk_bind_queue_submit_sparse_memory_bind(submit, resource_va, in);
445 if (ret)
446 return ret;
447 }
448 }
449 for (uint32_t i = 0; i < vk_submit->image_bind_count; i++) {
450 VK_FROM_HANDLE(panvk_image, image, vk_submit->image_binds[i].image);
451 assert(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT);
452 assert(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT);
453
454 struct panvk_sparse_block_desc sblock = panvk_get_sparse_block_desc(image->vk.image_type, image->vk.format);
455
456 for (uint32_t j = 0; j < vk_submit->image_binds[i].bindCount; j++) {
457 const VkSparseImageMemoryBind *in =
458 &vk_submit->image_binds[i].pBinds[j];
459
460 VkOffset3D max = {
461 in->offset.x + in->extent.width,
462 in->offset.y + in->extent.height,
463 in->offset.z + in->extent.depth,
464 };
465 struct panvk_sparse_block_memory_bind bind = {
466 /* We only support single-plane images right now. See
467 * https://gitlab.freedesktop.org/panfrost/mesa/-/issues/243 details. */
468 .plane_index = 0,
469 .level = in->subresource.mipLevel,
470 .layer = in->subresource.arrayLayer,
471 .offset = {},
472 .extent = sblock.extent,
473 .mem = in->memory,
474 .mem_offset = in->memoryOffset,
475 };
476 for (bind.offset.z = in->offset.z;
477 bind.offset.z < max.z; bind.offset.z += sblock.extent.depth) {
478 for (bind.offset.y = in->offset.y;
479 bind.offset.y < max.y; bind.offset.y += sblock.extent.height) {
480 for (bind.offset.x = in->offset.x;
481 bind.offset.x < max.x; bind.offset.x += sblock.extent.width) {
482 ret = panvk_bind_queue_submit_sparse_block_memory_bind(submit, image, &bind);
483 if (ret)
484 return ret;
485 bind.mem_offset += sblock.size_B;
486 }
487 }
488 }
489 }
490 }
491
492 return panvk_bind_queue_submit_process_signals(submit);
493 }
494
495 VkResult
496 panvk_per_arch(bind_queue_submit)(struct vk_queue *vk_queue,
497 struct vk_queue_submit *vk_submit)
498 {
499 struct panvk_bind_queue_submit submit;
500 VkResult result = VK_SUCCESS;
501
502 if (vk_queue_is_lost(vk_queue))
503 return VK_ERROR_DEVICE_LOST;
504
505 panvk_bind_queue_submit_init(&submit, vk_queue, vk_submit);
506
507 int ret = panvk_bind_queue_submit_do(&submit, vk_submit);
508 if (ret)
509 result = vk_queue_set_lost(vk_queue, "GROUP_SUBMIT: %m");
510
511 panvk_bind_queue_submit_cleanup(&submit);
512
513 return result;
514 }
515
516 VkResult
517 panvk_per_arch(create_bind_queue)(struct panvk_device *dev,
518 const VkDeviceQueueCreateInfo *create_info,
519 uint32_t queue_idx,
520 struct vk_queue **out_queue)
521 {
522 struct panvk_bind_queue *queue = vk_zalloc(
523 &dev->vk.alloc, sizeof(*queue), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
524 if (!queue)
525 return panvk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
526
527 VkResult result =
528 vk_queue_init(&queue->vk, &dev->vk, create_info, queue_idx);
529 if (result != VK_SUCCESS)
530 goto err_free_queue;
531
532 int ret = drmSyncobjCreate(dev->drm_fd, 0, &queue->syncobj_handle);
533 if (ret) {
534 result = panvk_errorf(dev, VK_ERROR_INITIALIZATION_FAILED,
535 "Failed to create our internal sync object");
536 goto err_finish_queue;
537 }
538
539 queue->vk.driver_submit = panvk_per_arch(bind_queue_submit);
540 *out_queue = &queue->vk;
541 return VK_SUCCESS;
542
543 err_finish_queue:
544 vk_queue_finish(&queue->vk);
545
546 err_free_queue:
547 vk_free(&dev->vk.alloc, queue);
548 return result;
549 }
550
551 void
552 panvk_per_arch(destroy_bind_queue)(struct vk_queue *vk_queue)
553 {
554 struct panvk_bind_queue *queue =
555 container_of(vk_queue, struct panvk_bind_queue, vk);
556 struct panvk_device *dev = to_panvk_device(queue->vk.base.device);
557
558 drmSyncobjDestroy(dev->drm_fd, queue->syncobj_handle);
559 vk_queue_finish(&queue->vk);
560 vk_free(&dev->vk.alloc, queue);
561 }
562
563 VkResult
564 panvk_per_arch(bind_queue_check_status)(struct vk_queue *vk_queue)
565 {
566 return VK_SUCCESS;
567 }