Debian Codegraph @

src/asahi/vulkan/hk_queue.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * Copyright 2024 Valve Corporation
6 * Copyright 2024 Alyssa Rosenzweig
7 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
8 * Copyright © 2016 Red Hat.
9 * Copyright © 2016 Bas Nieuwenhuizen
10 *
11 * based in part on anv driver which is:
12 * Copyright © 2015 Intel Corporation
13 * SPDX-License-Identifier: MIT
14 */
15 #include "hk_queue.h"
16 #include "hk_buffer.h"
17
18 #include "agx_bg_eot.h"
19 #include "agx_bo.h"
20 #include "agx_device.h"
21 #include "agx_pack.h"
22 #include "decode.h"
23 #include "hk_cmd_buffer.h"
24 #include "hk_device.h"
25 #include "hk_image.h"
26 #include "hk_physical_device.h"
27
28 #include <xf86drm.h>
29 #include "util/list.h"
30 #include "util/macros.h"
31 #include "util/u_dynarray.h"
32 #include "vulkan/vulkan_core.h"
33
34 #include "hk_private.h"
35 #include "layout.h"
36 #include "vk_drm_syncobj.h"
37 #include "vk_sync.h"
38
39 /*
40 * We need to specially handle submits with no control streams. The kernel
41 * can't accept empty submits, but we can end up here in Vulkan for
42 * synchronization purposes only. Rather than submit a no-op job (slow),
43 * we simply tie the fences together.
44 */
45 static VkResult
46 queue_submit_empty(struct hk_device *dev, struct hk_queue *queue,
47 struct vk_queue_submit *submit)
48 {
49 int fd = dev->dev.fd;
50
51 /* Transfer the waits into the queue timeline. */
52 for (unsigned i = 0; i < submit->wait_count; ++i) {
53 struct vk_sync_wait *wait = &submit->waits[i];
54
55 assert(vk_sync_type_is_drm_syncobj(wait->sync->type));
56 const struct vk_drm_syncobj *syncobj = vk_sync_as_drm_syncobj(wait->sync);
57
58 drmSyncobjTransfer(fd, queue->drm.syncobj, ++queue->drm.timeline_value,
59 syncobj->syncobj, wait->wait_value, 0);
60 }
61
62 /* Transfer the queue timeline into each out fence. They will all be
63 * signalled when we reach this point.
64 */
65 for (unsigned i = 0; i < submit->signal_count; ++i) {
66 struct vk_sync_signal *signal = &submit->signals[i];
67
68 assert(vk_sync_type_is_drm_syncobj(signal->sync->type));
69 const struct vk_drm_syncobj *syncobj =
70 vk_sync_as_drm_syncobj(signal->sync);
71
72 drmSyncobjTransfer(fd, syncobj->syncobj, signal->signal_value,
73 queue->drm.syncobj, queue->drm.timeline_value, 0);
74 }
75
76 return VK_SUCCESS;
77 }
78
79 static void
80 asahi_fill_cdm_command(struct hk_device *dev, struct hk_cs *cs,
81 struct drm_asahi_cmd_compute *cmd)
82 {
83 size_t len = cs->stream_linked ? 65536 /* XXX */ : (cs->current - cs->start);
84
85 *cmd = (struct drm_asahi_cmd_compute){
86 .cdm_ctrl_stream_base = cs->addr,
87 .cdm_ctrl_stream_end = cs->addr + len,
88
89 .ts.end.handle = cs->timestamp.end.handle,
90 .ts.end.offset = cs->timestamp.end.offset_B,
91 };
92
93 if (cs->uses_sampler_heap) {
94 cmd->sampler_heap = dev->samplers.table.bo->va->addr;
95 cmd->sampler_count = dev->samplers.table.alloc;
96 }
97
98 if (cs->scratch.cs.main || cs->scratch.cs.preamble) {
99 cmd->helper.data = dev->scratch.cs.buf->va->addr;
100 cmd->helper.cfg = cs->scratch.cs.preamble ? (1 << 16) : 0;
101 cmd->helper.binary = agx_helper_program(&dev->bg_eot);
102 }
103 }
104
105 static void
106 asahi_fill_vdm_command(struct hk_device *dev, struct hk_cs *cs,
107 struct drm_asahi_cmd_render *c)
108 {
109 memset(c, 0, sizeof(*c));
110
111 c->vdm_ctrl_stream_base = cs->addr;
112
113 agx_pack(&c->ppp_ctrl, CR_PPP_CONTROL, cfg) {
114 cfg.enable_w_clamp = true;
115 cfg.fixed_point_format = 1;
116 }
117
118 c->width_px = cs->cr.width;
119 c->height_px = cs->cr.height;
120
121 c->isp_bgobjdepth = cs->cr.isp_bgobjdepth;
122 c->isp_bgobjvals = cs->cr.isp_bgobjvals;
123
124 static_assert(sizeof(c->zls_ctrl) == sizeof(cs->cr.zls_control));
125 memcpy(&c->zls_ctrl, &cs->cr.zls_control, sizeof(cs->cr.zls_control));
126
127 agx_pack(&c->isp_zls_pixels, CR_ISP_ZLS_PIXELS, cfg) {
128 cfg.x = cs->cr.zls_width;
129 cfg.y = cs->cr.zls_height;
130 }
131
132 c->depth.base = cs->cr.depth.buffer;
133 c->depth.stride = cs->cr.depth.stride;
134 c->depth.comp_base = cs->cr.depth.meta;
135 c->depth.comp_stride = cs->cr.depth.meta_stride;
136 c->stencil.base = cs->cr.stencil.buffer;
137 c->stencil.stride = cs->cr.stencil.stride;
138 c->stencil.comp_base = cs->cr.stencil.meta;
139 c->stencil.comp_stride = cs->cr.stencil.meta_stride;
140
141 if (cs->cr.dbias_is_int == U_TRISTATE_YES) {
142 c->flags |= DRM_ASAHI_RENDER_DBIAS_IS_INT;
143 }
144
145 if (dev->dev.debug & AGX_DBG_NOCLUSTER) {
146 c->flags |= DRM_ASAHI_RENDER_NO_VERTEX_CLUSTERING;
147 }
148
149 agx_tilebuffer_set_drm_cmd(c, &cs->tib);
150
151 /* Can be 0 for attachmentless rendering with no draws */
152 c->samples = MAX2(cs->tib.nr_samples, 1);
153 c->layers = cs->cr.layers;
154
155 /* Drawing max size will OOM and fail submission. But vkd3d-proton does this
156 * for emulating no-attachment rendering. Clamp to something reasonable and
157 * hope this is good enough in practice. This only affects a case that would
158 * otherwise be guaranteed broken.
159 *
160 * XXX: Hack for vkd3d-proton.
161 */
162 if (c->layers == 2048 && c->width_px == 16384 && c->height_px == 16384) {
163 mesa_log(MESA_LOG_WARN, MESA_LOG_TAG, "Clamping massive framebuffer");
164 c->layers = 32;
165 }
166
167 c->ppp_multisamplectl = cs->ppp_multisamplectl;
168 c->sample_size_B = cs->tib.sample_size_B;
169
170 float tan_60 = 1.732051f;
171 c->isp_merge_upper_x = fui(tan_60 / cs->cr.width);
172 c->isp_merge_upper_y = fui(tan_60 / cs->cr.height);
173
174 c->bg.usc = cs->cr.bg.main.usc | 4;
175 c->eot.usc = cs->cr.eot.main.usc | 4;
176 c->partial_bg.usc = cs->cr.bg.partial.usc | 4;
177 c->partial_eot.usc = cs->cr.eot.partial.usc | 4;
178
179 memcpy(&c->bg.rsrc_spec, &cs->cr.bg.main.counts,
180 sizeof(struct agx_counts_packed));
181
182 memcpy(&c->eot.rsrc_spec, &cs->cr.eot.main.counts,
183 sizeof(struct agx_counts_packed));
184
185 memcpy(&c->partial_bg.rsrc_spec, &cs->cr.bg.partial.counts,
186 sizeof(struct agx_counts_packed));
187
188 memcpy(&c->partial_eot.rsrc_spec, &cs->cr.eot.partial.counts,
189 sizeof(struct agx_counts_packed));
190
191 c->isp_scissor_base = cs->uploaded_scissor;
192 c->isp_dbias_base = cs->uploaded_zbias;
193
194 if (cs->uses_sampler_heap) {
195 c->sampler_heap = dev->samplers.table.bo->va->addr;
196 c->sampler_count = dev->samplers.table.alloc;
197 }
198
199 c->isp_oclqry_base = dev->occlusion_queries.bo->va->addr;
200
201 if (cs->cr.process_empty_tiles)
202 c->flags |= DRM_ASAHI_RENDER_PROCESS_EMPTY_TILES;
203
204 if (cs->scratch.vs.main || cs->scratch.vs.preamble) {
205 c->flags |= DRM_ASAHI_RENDER_VERTEX_SCRATCH;
206 c->vertex_helper.data = dev->scratch.vs.buf->va->addr;
207 c->vertex_helper.cfg = cs->scratch.vs.preamble ? (1 << 16) : 0;
208 c->vertex_helper.binary = agx_helper_program(&dev->bg_eot);
209 }
210
211 if (cs->scratch.fs.main || cs->scratch.fs.preamble) {
212 c->fragment_helper.data = dev->scratch.fs.buf->va->addr;
213 c->fragment_helper.cfg = cs->scratch.fs.preamble ? (1 << 16) : 0;
214 c->fragment_helper.binary = agx_helper_program(&dev->bg_eot);
215 }
216
217 if (cs->timestamp.end.handle) {
218 c->ts_frag.end.handle = cs->timestamp.end.handle;
219 c->ts_frag.end.offset = cs->timestamp.end.offset_B;
220 }
221 }
222
223 static void
224 asahi_fill_sync(struct drm_asahi_sync *sync, struct vk_sync *vk_sync,
225 uint64_t value)
226 {
227 if (unlikely(!vk_sync_type_is_drm_syncobj(vk_sync->type))) {
228 UNREACHABLE("Unsupported sync type");
229 return;
230 }
231
232 const struct vk_drm_syncobj *syncobj = vk_sync_as_drm_syncobj(vk_sync);
233 *sync = (struct drm_asahi_sync){.handle = syncobj->syncobj};
234
235 if (vk_sync->flags & VK_SYNC_IS_TIMELINE) {
236 sync->sync_type = DRM_ASAHI_SYNC_TIMELINE_SYNCOBJ;
237 sync->timeline_value = value;
238 } else {
239 sync->sync_type = DRM_ASAHI_SYNC_SYNCOBJ;
240 }
241 }
242
243 union drm_asahi_cmd {
244 struct drm_asahi_cmd_compute compute;
245 struct drm_asahi_cmd_render render;
246 };
247
248 /* XXX: Batching multiple commands per submission is causing rare (7ppm) flakes
249 * on the CTS once lossless compression is enabled. This needs to be
250 * investigated before we can reenable this mechanism. We are likely missing a
251 * cache flush or barrier somewhere.
252 */
253 static inline unsigned
254 max_commands_per_submit(struct hk_device *dev)
255 {
256 return HK_PERF(dev, BATCH) ? 64 : 1;
257 }
258
259 static VkResult
260 queue_submit_single(struct hk_device *dev, struct drm_asahi_submit *submit,
261 unsigned ring_idx)
262 {
263 struct agx_submit_virt virt = {.ring_idx = ring_idx};
264
265 if (dev->dev.is_virtio) {
266 u_rwlock_rdlock(&dev->external_bos.lock);
267 virt.extres_count = util_dynarray_num_elements(
268 &dev->external_bos.list, struct asahi_ccmd_submit_res);
269 virt.extres = util_dynarray_begin(&dev->external_bos.list);
270 }
271
272 int ret = dev->dev.ops.submit(&dev->dev, submit, &virt);
273
274 if (dev->dev.is_virtio)
275 u_rwlock_rdunlock(&dev->external_bos.lock);
276
277 /* XXX: don't trap */
278 if (ret) {
279 fprintf(stderr, "DRM_IOCTL_ASAHI_SUBMIT failed: %m\n");
280 assert(0);
281 }
282
283 return VK_SUCCESS;
284 }
285
286 /*
287 * The kernel/firmware jointly impose a limit on commands per submit ioctl, but
288 * we can build up arbitrarily large command buffers. We handle this here by
289 * looping the ioctl, submitting slices of the command buffers that are within
290 * bounds.
291 */
292 static VkResult
293 queue_submit_looped(struct hk_device *dev, struct drm_asahi_submit *submit,
294 unsigned command_count, unsigned ring_idx)
295 {
296 uint8_t *cmdbuf = (uint8_t *)(uintptr_t)submit->cmdbuf;
297 uint32_t offs = 0;
298 unsigned submitted_vdm = 0, submitted_cdm = 0;
299 unsigned commands_remaining = command_count;
300
301 uint64_t out_syncs =
302 submit->syncs + sizeof(struct drm_asahi_sync) * submit->in_sync_count;
303
304 while (commands_remaining) {
305 bool first = commands_remaining == command_count;
306 bool last = commands_remaining <= max_commands_per_submit(dev);
307
308 unsigned count = MIN2(commands_remaining, max_commands_per_submit(dev));
309 commands_remaining -= count;
310
311 assert(!last || commands_remaining == 0);
312 assert(count > 0);
313
314 unsigned base_offs = offs;
315 unsigned cdm_count = 0, vdm_count = 0;
316
317 /* We need to fix up the barriers since barriers are ioctl-relative */
318 for (unsigned i = 0; i < count; ++i) {
319 struct drm_asahi_cmd_header *cmd = (void *)(cmdbuf + offs);
320 offs += sizeof(*cmd) + cmd->size;
321
322 if (cmd->cmd_type == DRM_ASAHI_CMD_RENDER)
323 vdm_count++;
324 else if (cmd->cmd_type == DRM_ASAHI_CMD_COMPUTE)
325 cdm_count++;
326
327 if (cmd->vdm_barrier != DRM_ASAHI_BARRIER_NONE) {
328 assert(cmd->vdm_barrier >= submitted_vdm);
329 cmd->vdm_barrier -= submitted_vdm;
330 }
331
332 if (cmd->cdm_barrier != DRM_ASAHI_BARRIER_NONE) {
333 assert(cmd->cdm_barrier >= submitted_cdm);
334 cmd->cdm_barrier -= submitted_cdm;
335 }
336 }
337
338 /* We can't signal the out-syncobjs until all prior work finishes. Since
339 * only the last ioctl will signal, make sure it waits on prior ioctls.
340 *
341 * TODO: there might be a more performant way to do this.
342 */
343 if (last && !first) {
344 struct drm_asahi_cmd_header *cmd = (void *)(cmdbuf + base_offs);
345
346 if (cmd->vdm_barrier == DRM_ASAHI_BARRIER_NONE)
347 cmd->vdm_barrier = 0;
348
349 if (cmd->cdm_barrier == DRM_ASAHI_BARRIER_NONE)
350 cmd->cdm_barrier = 0;
351 }
352
353 bool has_in_syncs = first;
354 bool has_out_syncs = last;
355
356 struct drm_asahi_submit submit_ioctl = {
357 .flags = submit->flags,
358 .queue_id = submit->queue_id,
359 .cmdbuf = submit->cmdbuf + base_offs,
360 .cmdbuf_size = offs - base_offs,
361
362 .syncs = has_in_syncs ? submit->syncs : out_syncs,
363 .in_sync_count = has_in_syncs ? submit->in_sync_count : 0,
364 .out_sync_count = has_out_syncs ? submit->out_sync_count : 0,
365 };
366
367 VkResult result = queue_submit_single(dev, &submit_ioctl, ring_idx);
368 if (result != VK_SUCCESS)
369 return result;
370
371 submitted_cdm += cdm_count;
372 submitted_vdm += vdm_count;
373 }
374
375 return VK_SUCCESS;
376 }
377
378 struct hk_bind_builder {
379 /* Initialized */
380 struct hk_device *dev;
381 struct vk_object_base *obj_base;
382 struct agx_va *va;
383 struct hk_image *image;
384
385 /* State */
386 struct hk_device_memory *mem;
387 VkDeviceSize resourceOffset;
388 VkDeviceSize size;
389 VkDeviceSize memoryOffset;
390 VkResult result;
391
392 /* Array of drm_asahi_gem_bind_op's */
393 struct util_dynarray binds;
394 };
395
396 static inline struct hk_bind_builder
397 hk_bind_builder(struct hk_device *dev, struct vk_object_base *obj_base,
398 struct agx_va *va, struct hk_image *image)
399 {
400 struct hk_bind_builder b = {
401 .dev = dev,
402 .obj_base = obj_base,
403 .va = va,
404 .image = image,
405 };
406
407 b.binds = UTIL_DYNARRAY_INIT;
408 return b;
409 }
410
411 static VkResult
412 hk_flush_bind(struct hk_bind_builder *b)
413 {
414 if (b->result != VK_SUCCESS || b->size == 0) {
415 return b->result;
416 }
417
418 perf_debug(b->dev, "Sparse bind");
419
420 uint64_t va_addr = b->va->addr + b->resourceOffset;
421
422 /* If we have an image with sparse residency, we have a userspace-managed
423 * sparse page table map, which we need to keep in sync with the real
424 * kernel-managed page table. This ensures textures get strict residency
425 * semantics, using the hardware sparse support.
426 */
427 if (b->image && b->image->planes[0].sparse_map != NULL) {
428 assert(b->image->plane_count == 1 && "multiplane sparse not supported");
429
430 uint32_t *map = agx_bo_map(b->image->planes[0].sparse_map);
431 uint64_t size_page = ail_bytes_to_pages(b->size);
432
433 struct ail_layout *layout = &b->image->planes[0].layout;
434 uint64_t layer_stride_page = ail_bytes_to_pages(layout->layer_stride_B);
435
436 for (unsigned offs_page = 0; offs_page < size_page; offs_page++) {
437 /* Determine the target page to bind */
438 uint64_t target_page =
439 ail_bytes_to_pages(b->resourceOffset) + offs_page;
440
441 /* The page table is per-layer. Fortunately, layers are page-aligned,
442 * so we can divide to find the layer & the page relative to the start
443 * of the layer, which give us the index into the sparse map.
444 *
445 * Note that we can end up out-of-bounds since the hardware page size
446 * (16k) is smaller than the Vulkan standard sparse block size (65k).
447 * Just clamp out-of-bounds maps - there is sufficient VA space for
448 * them but not sufficient sparse map space for them.
449 */
450 uint64_t z = target_page / layer_stride_page;
451 if (z >= layout->depth_px)
452 break;
453
454 uint64_t page_in_layer = target_page % layer_stride_page;
455 unsigned idx = ail_page_to_sparse_index_el(layout, z, page_in_layer);
456
457 agx_pack(map + idx, SPARSE_BLOCK, cfg) {
458 cfg.enabled = b->mem != NULL;
459 cfg.unknown = cfg.enabled;
460
461 if (cfg.enabled) {
462 cfg.address = va_addr + (offs_page * AIL_PAGESIZE);
463 }
464 }
465 }
466 }
467
468 /* When the app wants to unbind, replace the bound pages with scratch pages
469 * so we don't leave a gap.
470 */
471 struct drm_asahi_gem_bind_op op;
472 if (!b->mem) {
473 op = (struct drm_asahi_gem_bind_op){
474 .handle = b->dev->dev.scratch_bo->uapi_handle,
475 .flags = DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_WRITE |
476 DRM_ASAHI_BIND_SINGLE_PAGE,
477 .addr = b->va->addr + b->resourceOffset,
478 .range = b->size,
479 };
480 } else {
481 op = (struct drm_asahi_gem_bind_op){
482 .handle = b->mem->bo->uapi_handle,
483 .flags = DRM_ASAHI_BIND_READ | DRM_ASAHI_BIND_WRITE,
484 .addr = va_addr,
485 .offset = b->memoryOffset,
486 .range = b->size,
487 };
488 }
489
490 util_dynarray_append(&b->binds, op);
491
492 /* Shadow a read-only mapping to the upper half */
493 op.flags &= ~DRM_ASAHI_BIND_WRITE;
494 op.addr = agx_rw_addr_to_ro(&b->dev->dev, op.addr);
495
496 if (!b->mem) {
497 op.handle = b->dev->dev.zero_bo->uapi_handle;
498 }
499
500 util_dynarray_append(&b->binds, op);
501
502 return VK_SUCCESS;
503 }
504
505 static int
506 hk_bind_builder_finish(struct hk_bind_builder *b)
507 {
508 hk_flush_bind(b);
509
510 /* Submit everything to the kernel at once */
511 if (b->binds.size > 0) {
512 b->dev->dev.ops.bo_bind(
513 &b->dev->dev, b->binds.data,
514 util_dynarray_num_elements(&b->binds, struct drm_asahi_gem_bind_op));
515 }
516
517 util_dynarray_fini(&b->binds);
518 return b->result;
519 }
520
521 static void
522 hk_add_bind(struct hk_bind_builder *b, struct hk_device_memory *mem,
523 VkDeviceSize resourceOffset, VkDeviceSize size,
524 VkDeviceSize memoryOffset)
525 {
526 /* Discard trivial binds to simplify the below logic. */
527 if (size == 0)
528 return;
529
530 /* Try to merge with the previous bind */
531 if (b->size && b->mem == mem &&
532 resourceOffset == b->resourceOffset + b->size &&
533 (!mem || memoryOffset == b->memoryOffset + b->size)) {
534
535 b->size += size;
536 return;
537 }
538
539 /* Otherwise, flush the previous bind and replace with the new one */
540 hk_flush_bind(b);
541 b->mem = mem;
542 b->resourceOffset = resourceOffset;
543 b->size = size;
544 b->memoryOffset = memoryOffset;
545 }
546
547 static VkResult
548 hk_sparse_buffer_bind_memory(struct hk_device *device,
549 const VkSparseBufferMemoryBindInfo *bind)
550 {
551 VK_FROM_HANDLE(hk_buffer, buffer, bind->buffer);
552
553 struct hk_bind_builder b =
554 hk_bind_builder(device, &buffer->vk.base, buffer->va, NULL);
555
556 for (uint32_t i = 0; i < bind->bindCount; ++i) {
557 struct hk_device_memory *cur_mem = NULL;
558
559 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
560 cur_mem = hk_device_memory_from_handle(bind->pBinds[i].memory);
561
562 hk_add_bind(&b, cur_mem, bind->pBinds[i].resourceOffset,
563 bind->pBinds[i].size, bind->pBinds[i].memoryOffset);
564 }
565
566 return hk_bind_builder_finish(&b);
567 }
568
569 static VkResult
570 hk_sparse_image_opaque_bind_memory(
571 struct hk_device *device, const VkSparseImageOpaqueMemoryBindInfo *bind)
572 {
573 VK_FROM_HANDLE(hk_image, image, bind->image);
574
575 struct hk_bind_builder b =
576 hk_bind_builder(device, &image->vk.base, image->planes[0].va, image);
577
578 for (uint32_t i = 0; i < bind->bindCount; ++i) {
579 struct hk_device_memory *mem = NULL;
580 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
581 mem = hk_device_memory_from_handle(bind->pBinds[i].memory);
582
583 VkDeviceSize resourceOffset = bind->pBinds[i].resourceOffset;
584
585 /* Conceptually, the miptail is a single region at the end of the image,
586 * possibly layered. However, due to alignment requirements we need to
587 * use a non-layered miptail and internally fan out to each of the layers.
588 * This is facilitated by the HK_MIP_TAIL_START_OFFSET magic offset, see
589 * the comment where that is defined for more detail.
590 */
591 if (resourceOffset >= HK_MIP_TAIL_START_OFFSET) {
592 assert(resourceOffset == HK_MIP_TAIL_START_OFFSET &&
593 "must bind whole miptail... maybe...");
594
595 const struct ail_layout *layout = &image->planes[0].layout;
596 unsigned tail_offset_B =
597 layout->level_offsets_B[layout->mip_tail_first_lod];
598
599 for (unsigned z = 0; z < layout->depth_px; ++z) {
600 uint64_t image_offs = tail_offset_B + (z * layout->layer_stride_B);
601 uint64_t mem_offs =
602 bind->pBinds[i].memoryOffset + (z * layout->mip_tail_stride);
603
604 hk_add_bind(&b, mem, image_offs, layout->mip_tail_stride, mem_offs);
605 }
606 } else {
607 hk_add_bind(&b, mem, bind->pBinds[i].resourceOffset,
608 bind->pBinds[i].size, bind->pBinds[i].memoryOffset);
609 }
610 }
611
612 return hk_bind_builder_finish(&b);
613 }
614
615 static void
616 bind_hw_tile(struct hk_bind_builder *b, struct hk_device_memory *mem,
617 struct ail_layout *layout, unsigned layer, unsigned level,
618 VkOffset3D offset, VkExtent3D extent, struct ail_tile std_size_el,
619 unsigned mem_offset, unsigned x, unsigned y, unsigned z)
620 {
621 uint64_t bo_offset_B = ail_get_twiddled_block_B(
622 layout, level, offset.x + x, offset.y + y, layer + offset.z + z);
623
624 /* Consider the standard tiles in the bound memory to be in raster order, and
625 * address accordingly in standard tiles.
626 */
627 unsigned mem_x_stl = x / std_size_el.width_el;
628 unsigned mem_y_stl = y / std_size_el.height_el;
629 unsigned extent_w_stl = DIV_ROUND_UP(extent.width, std_size_el.width_el);
630 unsigned extent_y_stl = DIV_ROUND_UP(extent.height, std_size_el.height_el);
631 unsigned mem_offs_stl = (extent_y_stl * extent_w_stl * z) +
632 (extent_w_stl * mem_y_stl) + mem_x_stl;
633
634 /* There are 4 hardware tiles per standard tile, so offset
635 * accordingly for each hardware tile.
636 */
637 unsigned mem_offset_B = mem_offset + (mem_offs_stl * 4 * AIL_PAGESIZE);
638
639 if (x % std_size_el.width_el)
640 mem_offset_B += AIL_PAGESIZE;
641
642 if (y % std_size_el.height_el)
643 mem_offset_B += (2 * AIL_PAGESIZE);
644
645 hk_add_bind(b, mem, bo_offset_B, AIL_PAGESIZE, mem_offset_B);
646 }
647
648 static VkResult
649 hk_sparse_image_bind_memory(struct hk_device *device,
650 const VkSparseImageMemoryBindInfo *bind)
651 {
652 VK_FROM_HANDLE(hk_image, image, bind->image);
653 struct ail_layout *layout = &image->planes[0].layout;
654
655 struct hk_bind_builder b =
656 hk_bind_builder(device, &image->vk.base, image->planes[0].va, image);
657
658 for (uint32_t i = 0; i < bind->bindCount; ++i) {
659 struct hk_device_memory *mem = NULL;
660 if (bind->pBinds[i].memory != VK_NULL_HANDLE)
661 mem = hk_device_memory_from_handle(bind->pBinds[i].memory);
662
663 uint64_t mem_offset = bind->pBinds[i].memoryOffset;
664 const uint32_t layer = bind->pBinds[i].subresource.arrayLayer;
665 const uint32_t level = bind->pBinds[i].subresource.mipLevel;
666
667 VkExtent3D bind_extent = bind->pBinds[i].extent;
668 bind_extent.width = DIV_ROUND_UP(
669 bind_extent.width, vk_format_get_blockwidth(image->vk.format));
670 bind_extent.height = DIV_ROUND_UP(
671 bind_extent.height, vk_format_get_blockheight(image->vk.format));
672
673 VkOffset3D bind_offset = bind->pBinds[i].offset;
674 bind_offset.x /= vk_format_get_blockwidth(image->vk.format);
675 bind_offset.y /= vk_format_get_blockheight(image->vk.format);
676
677 /* Hardware tiles are exactly one page (16K) */
678 struct ail_tile tilesize_el = layout->tilesize_el[level];
679 unsigned size_B = tilesize_el.width_el * tilesize_el.height_el *
680 ail_get_blocksize_B(layout);
681
682 assert(size_B == AIL_PAGESIZE && "fundamental to AGX");
683
684 /* Standard tiles are exactly 4 pages (65K), consisting of a 2x2 grid of
685 * hardware tiles.
686 */
687 struct ail_tile std_size_el = tilesize_el;
688 std_size_el.width_el *= 2;
689 std_size_el.height_el *= 2;
690
691 for (unsigned z = 0; z < bind_extent.depth; z += 1) {
692 for (unsigned y = 0; y < bind_extent.height;
693 y += tilesize_el.height_el) {
694 for (unsigned x = 0; x < bind_extent.width;
695 x += tilesize_el.width_el) {
696 bind_hw_tile(&b, mem, layout, layer, level, bind_offset,
697 bind_extent, std_size_el, mem_offset, x, y, z);
698 }
699 }
700 }
701 }
702
703 return hk_bind_builder_finish(&b);
704 }
705
706 static VkResult
707 hk_queue_submit_bind_sparse_memory(struct hk_device *device,
708 struct vk_queue_submit *submission)
709 {
710 assert(submission->command_buffer_count == 0);
711
712 for (uint32_t i = 0; i < submission->buffer_bind_count; ++i) {
713 VkResult result =
714 hk_sparse_buffer_bind_memory(device, submission->buffer_binds + i);
715 if (result != VK_SUCCESS)
716 return result;
717 }
718
719 for (uint32_t i = 0; i < submission->image_opaque_bind_count; ++i) {
720 VkResult result = hk_sparse_image_opaque_bind_memory(
721 device, submission->image_opaque_binds + i);
722 if (result != VK_SUCCESS)
723 return result;
724 }
725
726 for (uint32_t i = 0; i < submission->image_bind_count; ++i) {
727 VkResult result =
728 hk_sparse_image_bind_memory(device, submission->image_binds + i);
729 if (result != VK_SUCCESS)
730 return result;
731 }
732
733 return VK_SUCCESS;
734 }
735
736 static VkResult
737 queue_submit(struct hk_device *dev, struct hk_queue *queue,
738 struct vk_queue_submit *submit)
739 {
740 /* TODO: Support asynchronous sparse queue? */
741 if (submit->buffer_bind_count || submit->image_bind_count ||
742 submit->image_opaque_bind_count) {
743
744 VkResult result = hk_queue_submit_bind_sparse_memory(dev, submit);
745 if (result != VK_SUCCESS)
746 return result;
747 }
748
749 unsigned command_count = 0;
750
751 /* Gather the number of individual commands to submit up front */
752 for (unsigned i = 0; i < submit->command_buffer_count; ++i) {
753 struct hk_cmd_buffer *cmdbuf =
754 (struct hk_cmd_buffer *)submit->command_buffers[i];
755
756 command_count += list_length(&cmdbuf->control_streams);
757 }
758
759 perf_debug_dev(&dev->dev,
760 "Submitting %u control streams (%u command buffers)",
761 command_count, submit->command_buffer_count);
762
763 if (command_count == 0)
764 return queue_submit_empty(dev, queue, submit);
765
766 unsigned wait_count = 0;
767 struct drm_asahi_sync *syncs =
768 alloca((submit->wait_count + submit->signal_count + 1) *
769 sizeof(struct drm_asahi_sync));
770
771 for (unsigned i = 0; i < submit->wait_count; ++i) {
772 /* The kernel rejects the submission if we try to wait on the same
773 * timeline semaphore at multiple points.
774 *
775 * TODO: Can we relax the UAPI?
776 *
777 * XXX: This is quadratic time.
778 */
779 bool skip = false;
780 if (submit->waits[i].sync->flags & VK_SYNC_IS_TIMELINE) {
781 uint32_t v1 = submit->waits[i].wait_value;
782 for (unsigned j = 0; j < submit->wait_count; ++j) {
783 uint32_t v2 = submit->waits[j].wait_value;
784 if (i != j && submit->waits[i].sync == submit->waits[j].sync &&
785 (v1 < v2 || (v1 == v2 && i < j))) {
786 skip = true;
787 break;
788 }
789 }
790
791 if (skip)
792 continue;
793 }
794
795 asahi_fill_sync(&syncs[wait_count++], submit->waits[i].sync,
796 submit->waits[i].wait_value);
797 }
798
799 for (unsigned i = 0; i < submit->signal_count; ++i) {
800 asahi_fill_sync(&syncs[wait_count + i], submit->signals[i].sync,
801 submit->signals[i].signal_value);
802 }
803
804 /* Signal progress on the queue itself */
805 syncs[wait_count + submit->signal_count] = (struct drm_asahi_sync){
806 .sync_type = DRM_ASAHI_SYNC_TIMELINE_SYNCOBJ,
807 .handle = queue->drm.syncobj,
808 .timeline_value = ++queue->drm.timeline_value,
809 };
810
811 /* Now setup the command structs */
812 struct util_dynarray payload = UTIL_DYNARRAY_INIT;
813
814 unsigned nr_vdm = 0, nr_cdm = 0;
815
816 for (unsigned i = 0; i < submit->command_buffer_count; ++i) {
817 struct hk_cmd_buffer *cmdbuf =
818 (struct hk_cmd_buffer *)submit->command_buffers[i];
819
820 list_for_each_entry(struct hk_cs, cs, &cmdbuf->control_streams, node) {
821 /* Barrier on previous command */
822 struct drm_asahi_cmd_header header =
823 agx_cmd_header(cs->type == HK_CS_CDM, nr_vdm, nr_cdm);
824
825 util_dynarray_append(&payload, header);
826
827 if (cs->type == HK_CS_CDM) {
828 perf_debug(
829 cmdbuf,
830 "%u: Submitting CDM with %u API calls, %u dispatches, %u flushes, %u merged",
831 i, cs->stats.calls, cs->stats.cmds, cs->stats.flushes,
832 cs->stats.merged);
833
834 assert(cs->stats.cmds > 0 || cs->stats.flushes > 0 ||
835 cs->timestamp.end.handle);
836
837 struct drm_asahi_cmd_compute cmd;
838 asahi_fill_cdm_command(dev, cs, &cmd);
839 util_dynarray_append(&payload, cmd);
840 nr_cdm++;
841 } else {
842 assert(cs->type == HK_CS_VDM);
843 perf_debug(cmdbuf, "%u: Submitting VDM with %u API draws, %u draws",
844 i, cs->stats.calls, cs->stats.cmds);
845 assert(cs->stats.cmds > 0 || cs->cr.process_empty_tiles ||
846 cs->timestamp.end.handle);
847
848 struct drm_asahi_cmd_render cmd;
849 asahi_fill_vdm_command(dev, cs, &cmd);
850 util_dynarray_append(&payload, cmd);
851 nr_vdm++;
852 }
853 }
854 }
855
856 if (dev->dev.debug & AGX_DBG_TRACE) {
857 agxdecode_drm_cmdbuf(dev->dev.agxdecode, &dev->dev.params, &payload,
858 true);
859
860 agxdecode_next_frame();
861 }
862
863 struct drm_asahi_submit submit_ioctl = {
864 .flags = 0,
865 .queue_id = queue->drm.id,
866 .in_sync_count = wait_count,
867 .out_sync_count = submit->signal_count + 1,
868 .cmdbuf_size = payload.size,
869 .syncs = (uint64_t)(uintptr_t)(syncs),
870 .cmdbuf = (uint64_t)(uintptr_t)(payload.data),
871 };
872
873 VkResult result;
874 if (command_count <= max_commands_per_submit(dev)) {
875 result =
876 queue_submit_single(dev, &submit_ioctl, queue->drm.virt_ring_idx);
877 } else {
878 result = queue_submit_looped(dev, &submit_ioctl, command_count,
879 queue->drm.virt_ring_idx);
880 }
881
882 util_dynarray_fini(&payload);
883 return result;
884 }
885
886 static VkResult
887 hk_queue_submit(struct vk_queue *vk_queue, struct vk_queue_submit *submit)
888 {
889 struct hk_queue *queue = container_of(vk_queue, struct hk_queue, vk);
890 struct hk_device *dev = hk_queue_device(queue);
891
892 if (vk_queue_is_lost(&queue->vk))
893 return VK_ERROR_DEVICE_LOST;
894
895 VkResult result = queue_submit(dev, queue, submit);
896 if (result != VK_SUCCESS)
897 result = vk_queue_set_lost(&queue->vk, "Submit failed");
898
899 if (dev->dev.debug & AGX_DBG_SYNC) {
900 /* Wait for completion */
901 int err = drmSyncobjTimelineWait(
902 dev->dev.fd, &queue->drm.syncobj, &queue->drm.timeline_value, 1,
903 INT64_MAX, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, NULL);
904
905 if (err) {
906 result = vk_queue_set_lost(&queue->vk, "Wait failed");
907 } else {
908 VkResult res = dev->vk.check_status(&dev->vk);
909 if (result == VK_SUCCESS)
910 result = res;
911 }
912 }
913
914 return result;
915 }
916
917 static enum drm_asahi_priority
918 translate_priority(VkQueueGlobalPriorityKHR prio)
919 {
920 switch (prio) {
921 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_KHR:
922 return DRM_ASAHI_PRIORITY_REALTIME;
923
924 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_KHR:
925 return DRM_ASAHI_PRIORITY_HIGH;
926
927 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR:
928 return DRM_ASAHI_PRIORITY_MEDIUM;
929
930 case VK_QUEUE_GLOBAL_PRIORITY_LOW_KHR:
931 return DRM_ASAHI_PRIORITY_LOW;
932
933 default:
934 UNREACHABLE("Invalid VkQueueGlobalPriorityKHR");
935 }
936 }
937
938 VkResult
939 hk_queue_init(struct hk_device *dev, const VkDeviceQueueCreateInfo *pCreateInfo,
940 uint32_t index_in_family)
941 {
942 struct hk_physical_device *pdev = hk_device_physical(dev);
943 VkResult result;
944
945 assert(pCreateInfo->queueFamilyIndex < pdev->queue_family_count);
946
947 struct hk_queue *queue = vk_zalloc(&dev->vk.alloc, sizeof(struct hk_queue),
948 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
949 if (!queue)
950 return VK_ERROR_OUT_OF_HOST_MEMORY;
951
952 const VkDeviceQueueGlobalPriorityCreateInfoKHR *priority_info =
953 vk_find_struct_const(pCreateInfo->pNext,
954 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_KHR);
955 const VkQueueGlobalPriorityKHR priority =
956 priority_info ? priority_info->globalPriority
957 : VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR;
958
959 /* TODO: Lift when kernel side is ready and we can handle failures in
960 * create_command_queue.
961 */
962 enum drm_asahi_priority drm_priority = translate_priority(priority);
963 if (drm_priority >= DRM_ASAHI_PRIORITY_HIGH) {
964 return VK_ERROR_NOT_PERMITTED_EXT;
965 }
966
967 result = vk_queue_init(&queue->vk, &dev->vk, pCreateInfo, index_in_family);
968 if (result != VK_SUCCESS)
969 return result;
970
971 queue->vk.driver_submit = hk_queue_submit;
972
973 queue->drm.id = agx_create_command_queue(&dev->dev, drm_priority);
974 queue->drm.virt_ring_idx = drm_priority + 1;
975
976 if (drmSyncobjCreate(dev->dev.fd, 0, &queue->drm.syncobj)) {
977 mesa_loge("drmSyncobjCreate() failed %d\n", errno);
978 agx_destroy_command_queue(&dev->dev, queue->drm.id);
979 vk_queue_finish(&queue->vk);
980
981 return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY,
982 "DRM_IOCTL_SYNCOBJ_CREATE failed: %m");
983 }
984
985 uint64_t initial_value = 1;
986 if (drmSyncobjTimelineSignal(dev->dev.fd, &queue->drm.syncobj,
987 &initial_value, 1)) {
988 hk_queue_finish(dev, queue);
989 return vk_errorf(dev, VK_ERROR_OUT_OF_HOST_MEMORY,
990 "DRM_IOCTL_TIMELINE_SYNCOBJ_SIGNAL failed: %m");
991 }
992
993 return VK_SUCCESS;
994 }
995
996 void
997 hk_queue_finish(struct hk_device *dev, struct hk_queue *queue)
998 {
999 drmSyncobjDestroy(dev->dev.fd, queue->drm.syncobj);
1000 agx_destroy_command_queue(&dev->dev, queue->drm.id);
1001 vk_queue_finish(&queue->vk);
1002 vk_free(&dev->vk.alloc, queue);
1003 }