Debian Codegraph @

src/panfrost/vulkan/panvk_vX_device.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 * Copyright © 2024 Arm Ltd.
4 *
5 * Derived from tu_image.c which is:
6 * Copyright © 2016 Red Hat.
7 * Copyright © 2016 Bas Nieuwenhuizen
8 * Copyright © 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "drm-uapi/panthor_drm.h"
14
15 #include "vk_cmd_enqueue_entrypoints.h"
16 #include "vk_common_entrypoints.h"
17 #include "vk_drm_syncobj.h"
18
19 #include "panvk_buffer.h"
20 #include "panvk_cmd_alloc.h"
21 #include "panvk_cmd_buffer.h"
22 #include "panvk_device.h"
23 #include "panvk_cmd_draw.h"
24 #include "panvk_entrypoints.h"
25 #include "panvk_instance.h"
26 #include "panvk_macros.h"
27 #include "panvk_physical_device.h"
28 #include "panvk_precomp_cache.h"
29 #include "panvk_priv_bo.h"
30 #include "panvk_queue.h"
31 #include "panvk_utrace.h"
32 #include "panvk_utrace_perfetto.h"
33
34 #include "genxml/decode.h"
35 #include "genxml/gen_macros.h"
36
37 #include "kmod/pan_kmod.h"
38 #include "util/os_file.h"
39 #include "util/u_printf.h"
40 #include "pan_props.h"
41 #include "pan_samples.h"
42
43 static void *
44 panvk_kmod_zalloc(const struct pan_kmod_allocator *allocator, size_t size,
45 bool transient)
46 {
47 const VkAllocationCallbacks *vkalloc = allocator->priv;
48
49 void *obj = vk_zalloc(vkalloc, size, 8,
50 transient ? VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
51 : VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
52
53 /* We force errno to -ENOMEM on host allocation failures so we can properly
54 * report it back as VK_ERROR_OUT_OF_HOST_MEMORY. */
55 if (!obj)
56 errno = -ENOMEM;
57
58 return obj;
59 }
60
61 static void
62 panvk_kmod_free(const struct pan_kmod_allocator *allocator, void *data)
63 {
64 const VkAllocationCallbacks *vkalloc = allocator->priv;
65
66 return vk_free(vkalloc, data);
67 }
68
69 static void
70 panvk_device_init_mempools(struct panvk_device *dev)
71 {
72 struct panvk_pool_properties rw_pool_props = {
73 .create_flags =
74 panvk_device_adjust_bo_flags(dev, PAN_KMOD_BO_FLAG_WB_MMAP),
75 .slab_size = 16 * 1024,
76 .label = "Device RW cached memory pool",
77 .owns_bos = false,
78 .needs_locking = true,
79 .prealloc = false,
80 };
81
82 panvk_pool_init(&dev->mempools.rw, dev, NULL, NULL, &rw_pool_props);
83
84 struct panvk_pool_properties rw_nc_pool_props = {
85 .create_flags =
86 panvk_device_adjust_bo_flags(dev, PAN_KMOD_BO_FLAG_GPU_UNCACHED),
87 .slab_size = 16 * 1024,
88 .label = "Device RW uncached memory pool",
89 .owns_bos = false,
90 .needs_locking = true,
91 .prealloc = false,
92 };
93
94 panvk_pool_init(&dev->mempools.rw_nc, dev, NULL, NULL, &rw_nc_pool_props);
95
96 struct panvk_pool_properties exec_pool_props = {
97 .create_flags = panvk_device_adjust_bo_flags(
98 dev, PAN_KMOD_BO_FLAG_EXECUTABLE | PAN_KMOD_BO_FLAG_WB_MMAP),
99 .slab_size = 16 * 1024,
100 .label = "Device executable memory pool (shaders)",
101 .owns_bos = false,
102 .needs_locking = true,
103 .prealloc = false,
104 };
105
106 panvk_pool_init(&dev->mempools.exec, dev, NULL, NULL, &exec_pool_props);
107 }
108
109 static void
110 panvk_device_cleanup_mempools(struct panvk_device *dev)
111 {
112 panvk_pool_cleanup(&dev->mempools.rw);
113 panvk_pool_cleanup(&dev->mempools.rw_nc);
114 panvk_pool_cleanup(&dev->mempools.exec);
115 }
116
117 static VkResult
118 panvk_meta_cmd_bind_map_buffer(struct vk_command_buffer *cmd,
119 struct vk_meta_device *meta, VkBuffer buf,
120 void **map_out)
121 {
122 VK_FROM_HANDLE(panvk_buffer, buffer, buf);
123 struct panvk_cmd_buffer *cmdbuf =
124 container_of(cmd, struct panvk_cmd_buffer, vk);
125 struct pan_ptr mem =
126 panvk_cmd_alloc_dev_mem(cmdbuf, desc, buffer->vk.size, 64);
127
128 if (!mem.gpu)
129 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
130
131 assert(buffer->vk.device_address == 0);
132 buffer->vk.device_address = mem.gpu;
133 *map_out = mem.cpu;
134 return VK_SUCCESS;
135 }
136
137 static VkResult
138 panvk_meta_init(struct panvk_device *device)
139 {
140 const struct vk_physical_device *pdev = device->vk.physical;
141
142 VkResult result = vk_meta_device_init(&device->vk, &device->meta);
143 if (result != VK_SUCCESS)
144 return result;
145
146 device->meta.use_stencil_export = true;
147 device->meta.use_rect_list_pipeline = true;
148 device->meta.max_bind_map_buffer_size_B = 64 * 1024;
149 device->meta.cmd_bind_map_buffer = panvk_meta_cmd_bind_map_buffer;
150
151 /* Assume a maximum of 1024 bytes per worgroup and choose the workgroup size
152 * accordingly. */
153 for (uint32_t i = 0;
154 i < ARRAY_SIZE(device->meta.buffer_access.optimal_wg_size); i++) {
155 device->meta.buffer_access.optimal_wg_size[i] =
156 MIN2(1024 >> i, pdev->properties.maxComputeWorkGroupSize[0]);
157 }
158
159 return VK_SUCCESS;
160 }
161
162 static void
163 panvk_meta_cleanup(struct panvk_device *device)
164 {
165 vk_meta_device_finish(&device->vk, &device->meta);
166 }
167
168 static VkResult
169 panvk_precomp_init(struct panvk_device *device)
170 {
171 device->precomp_cache = panvk_per_arch(precomp_cache_init)(device);
172
173 if (device->precomp_cache == NULL)
174 return VK_ERROR_OUT_OF_HOST_MEMORY;
175
176 return VK_SUCCESS;
177 }
178
179 static void
180 panvk_precomp_cleanup(struct panvk_device *device)
181 {
182 panvk_per_arch(precomp_cache_cleanup)(device->precomp_cache);
183 }
184
185 /* Always reserve the lower 32MB. */
186 #define PANVK_VA_RESERVE_BOTTOM 0x2000000ull
187
188 static enum pan_kmod_group_allow_priority_flags
189 global_priority_to_group_allow_priority_flag(
190 VkQueueGlobalPriorityKHR priority)
191 {
192 switch (priority) {
193 case VK_QUEUE_GLOBAL_PRIORITY_LOW_KHR:
194 return PAN_KMOD_GROUP_ALLOW_PRIORITY_LOW;
195 case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR:
196 return PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM;
197 case VK_QUEUE_GLOBAL_PRIORITY_HIGH_KHR:
198 return PAN_KMOD_GROUP_ALLOW_PRIORITY_HIGH;
199 case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_KHR:
200 return PAN_KMOD_GROUP_ALLOW_PRIORITY_REALTIME;
201 default:
202 UNREACHABLE("Invalid global priority");
203 }
204 }
205
206 static VkResult
207 check_global_priority(const struct panvk_physical_device *phys_dev,
208 const VkDeviceQueueCreateInfo *create_info)
209 {
210 const unsigned arch = pan_arch(phys_dev->kmod.dev->props.gpu_id);
211 const VkDeviceQueueGlobalPriorityCreateInfoKHR *priority_info =
212 vk_find_struct_const(create_info->pNext,
213 DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_KHR);
214 const VkQueueGlobalPriorityKHR priority =
215 priority_info ? priority_info->globalPriority
216 : VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_KHR;
217
218 switch (create_info->queueFamilyIndex) {
219 case PANVK_QUEUE_FAMILY_GPU: {
220 enum pan_kmod_group_allow_priority_flags requested_prio =
221 global_priority_to_group_allow_priority_flag(priority);
222 enum pan_kmod_group_allow_priority_flags allowed_prio_mask =
223 phys_dev->kmod.dev->props.allowed_group_priorities_mask;
224
225 /* Non-medium priority context is not hooked-up in the JM backend, even
226 * though the panfrost kmod advertize it. Manually filter non-medium
227 * priority for now.
228 */
229 if (arch < 10)
230 allowed_prio_mask &= PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM;
231
232 if (requested_prio & allowed_prio_mask)
233 return VK_SUCCESS;
234
235 return VK_ERROR_NOT_PERMITTED_KHR;
236 }
237
238 case PANVK_QUEUE_FAMILY_BIND: {
239 if (priority == VK_QUEUE_GLOBAL_PRIORITY_MEDIUM)
240 return VK_SUCCESS;
241
242 return VK_ERROR_NOT_PERMITTED_KHR;
243 }
244
245 default:
246 UNREACHABLE("Unknown queue family");
247 }
248 }
249
250 static VkResult
251 panvk_queue_check_status(struct vk_queue *queue)
252 {
253 switch (queue->queue_family_index) {
254 case PANVK_QUEUE_FAMILY_GPU:
255 return panvk_per_arch(gpu_queue_check_status)(queue);
256 case PANVK_QUEUE_FAMILY_BIND:
257 return panvk_per_arch(bind_queue_check_status)(queue);
258 default:
259 UNREACHABLE("Unknown queue family");
260 }
261 }
262
263 static VkResult
264 panvk_device_check_status(struct vk_device *vk_dev)
265 {
266 struct panvk_device *dev = to_panvk_device(vk_dev);
267 VkResult result = vk_check_printf_status(&dev->vk, &dev->printf.ctx);
268
269 for (uint32_t qfi = 0; qfi < PANVK_QUEUE_FAMILY_COUNT; qfi++) {
270 struct panvk_device_queue_family *qf = &dev->queue_families[qfi];
271
272 for (uint32_t q = 0; q < qf->queue_count; q++) {
273 struct vk_queue *queue = qf->queues[q];
274
275 if (panvk_queue_check_status(queue) != VK_SUCCESS)
276 result = VK_ERROR_DEVICE_LOST;
277 }
278 }
279
280 if (pan_kmod_vm_query_state(dev->kmod.vm) != PAN_KMOD_VM_USABLE) {
281 vk_device_set_lost(&dev->vk, "vm state: not usable");
282 result = VK_ERROR_DEVICE_LOST;
283 }
284
285 return result;
286 }
287
288 static VkResult
289 panvk_device_get_timestamp(struct vk_device *vk_dev, uint64_t *timestamp)
290 {
291 struct panvk_physical_device *pdev =
292 to_panvk_physical_device(vk_dev->physical);
293
294 *timestamp = pan_kmod_query_timestamp(pdev->kmod.dev);
295
296 return VK_SUCCESS;
297 }
298
299 static VkResult
300 panvk_queue_create(struct panvk_device *dev,
301 const VkDeviceQueueCreateInfo *create_info,
302 uint32_t queue_idx,
303 struct vk_queue **out_queue)
304 {
305 switch (create_info->queueFamilyIndex) {
306 case PANVK_QUEUE_FAMILY_GPU:
307 return panvk_per_arch(create_gpu_queue)(
308 dev, create_info, queue_idx, out_queue);
309 case PANVK_QUEUE_FAMILY_BIND:
310 return panvk_per_arch(create_bind_queue)(
311 dev, create_info, queue_idx, out_queue);
312 default:
313 return panvk_error(dev, VK_ERROR_INITIALIZATION_FAILED);
314 }
315 }
316
317 static void
318 panvk_queue_destroy(struct vk_queue *queue)
319 {
320 switch (queue->queue_family_index) {
321 case PANVK_QUEUE_FAMILY_GPU:
322 panvk_per_arch(destroy_gpu_queue)(queue);
323 break;
324 case PANVK_QUEUE_FAMILY_BIND:
325 panvk_per_arch(destroy_bind_queue)(queue);
326 break;
327 default:
328 UNREACHABLE("Unknown queue family");
329 }
330 }
331
332 VkResult
333 panvk_per_arch(create_device)(struct panvk_physical_device *physical_device,
334 const VkDeviceCreateInfo *pCreateInfo,
335 const VkAllocationCallbacks *pAllocator,
336 VkDevice *pDevice)
337 {
338 struct panvk_instance *instance =
339 to_panvk_instance(physical_device->vk.instance);
340 VkResult result;
341 struct panvk_device *device;
342
343 device = vk_zalloc2(&instance->vk.alloc, pAllocator, sizeof(*device), 8,
344 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
345 if (!device)
346 return panvk_error(physical_device, VK_ERROR_OUT_OF_HOST_MEMORY);
347
348 struct vk_device_dispatch_table dispatch_table;
349
350
351
352 if (PAN_ARCH <= 9) {
353 /* For secondary command buffer support, overwrite any command entrypoints
354 * in the main device-level dispatch table with
355 * vk_cmd_enqueue_unless_primary_Cmd*.
356 */
357 vk_device_dispatch_table_from_entrypoints(
358 &dispatch_table, &vk_cmd_enqueue_unless_primary_device_entrypoints, true);
359
360 /* Populate our primary cmd_dispatch table. */
361 vk_device_dispatch_table_from_entrypoints(
362 &device->cmd_dispatch, &panvk_per_arch(device_entrypoints), true);
363 vk_device_dispatch_table_from_entrypoints(&device->cmd_dispatch,
364 &panvk_device_entrypoints,
365 false);
366 vk_device_dispatch_table_from_entrypoints(&device->cmd_dispatch,
367 &vk_common_device_entrypoints,
368 false);
369 }
370
371 vk_device_dispatch_table_from_entrypoints(
372 &dispatch_table, &panvk_per_arch(device_entrypoints), PAN_ARCH > 9);
373 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
374 &panvk_device_entrypoints, false);
375 vk_device_dispatch_table_from_entrypoints(&dispatch_table,
376 &wsi_device_entrypoints, false);
377
378 result = vk_device_init(&device->vk, &physical_device->vk, &dispatch_table,
379 pCreateInfo, pAllocator);
380 if (result != VK_SUCCESS)
381 goto err_free_dev;
382
383 /* Must be done after vk_device_init() because this function memset(0) the
384 * whole struct.
385 */
386 device->vk.command_dispatch_table = &device->cmd_dispatch;
387 device->vk.command_buffer_ops = &panvk_per_arch(cmd_buffer_ops);
388 device->vk.shader_ops = &panvk_per_arch(device_shader_ops);
389 device->vk.check_status = panvk_device_check_status;
390 device->vk.get_timestamp = panvk_device_get_timestamp;
391 device->vk.copy_sync_payloads = vk_drm_syncobj_copy_payloads;
392
393 device->kmod.allocator = (struct pan_kmod_allocator){
394 .zalloc = panvk_kmod_zalloc,
395 .free = panvk_kmod_free,
396 .priv = &device->vk.alloc,
397 };
398 device->kmod.dev = pan_kmod_dev_create(
399 os_dupfd_cloexec(physical_device->kmod.dev->fd),
400 physical_device->kmod.dev->flags, &device->kmod.allocator);
401
402 if (!device->kmod.dev) {
403 result = panvk_errorf(instance, VK_ERROR_OUT_OF_HOST_MEMORY,
404 "cannot create device");
405 goto err_finish_dev;
406 }
407
408 if (PANVK_DEBUG(TRACE) || PANVK_DEBUG(SYNC) || PANVK_DEBUG(DUMP))
409 device->debug.decode_ctx = pandecode_create_context(false);
410
411 /* 48bit address space clamped by the physical device limits, with the lower
412 * 32MB reserved. */
413 uint64_t user_va_start = pan_clamp_to_usable_va_range(
414 device->kmod.dev, PANVK_VA_RESERVE_BOTTOM);
415 uint64_t user_va_end = physical_device->memory.max_supported_va;
416 uint32_t vm_flags = PAN_ARCH < 10 ? PAN_KMOD_VM_FLAG_AUTO_VA : 0;
417
418 device->kmod.vm =
419 pan_kmod_vm_create(device->kmod.dev, vm_flags,
420 user_va_start, user_va_end - user_va_start);
421
422 if (!device->kmod.vm) {
423 result = panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
424 goto err_destroy_kdev;
425 }
426
427 #if PAN_ARCH >= 10
428 const struct drm_panthor_csif_info *csif_info =
429 panthor_kmod_get_csif_props(device->kmod.dev);
430
431 assert(csif_info->scoreboard_slot_count <= 16);
432 device->csf.sb.count = csif_info->scoreboard_slot_count;
433 device->csf.sb.all_mask = (uint16_t)BITFIELD_MASK(csif_info->scoreboard_slot_count);
434
435 assert(device->csf.sb.count > PANVK_SB_ITER_START);
436 device->csf.sb.iter_count = device->csf.sb.count - PANVK_SB_ITER_START;
437
438 #if PAN_ARCH == 10
439 device->csf.sb.iter_count =
440 MIN2(device->csf.sb.iter_count, PANVK_SB_ITER_COUNT);
441 #endif
442
443 device->csf.sb.all_iters_mask =
444 BITFIELD_RANGE(PANVK_SB_ITER_START, device->csf.sb.iter_count);
445 #endif
446
447 simple_mtx_init(&device->as.lock, mtx_plain);
448
449 /* capture/replay requires a separate AS for fixed allocations. */
450 if (device->vk.enabled_features.bufferDeviceAddressCaptureReplay) {
451 const uint64_t split_point = user_va_end / 2;
452 util_vma_heap_init(&device->as.fixed_heap, split_point,
453 user_va_end - split_point);
454 device->as.split_heap = true;
455 /* shift the start of the non-fixed heap below the fixed one */
456 user_va_end = split_point;
457 }
458
459 const uint64_t low_va_end = 1ull << 32;
460 if (user_va_end <= low_va_end) {
461 /* if user_va_end overlaps with the low 32bits, share the AS for both. */
462 util_vma_heap_init(&device->as.heap, user_va_start,
463 user_va_end - user_va_start);
464 device->as.priv_heap = &device->as.heap;
465 device->as.extended_range = false;
466 } else {
467 util_vma_heap_init(&device->as.heap, low_va_end,
468 user_va_end - low_va_end);
469 device->as.priv_heap = malloc(sizeof(*device->as.priv_heap));
470 if (device->as.priv_heap == NULL) {
471 result = panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
472 goto err_free_heaps;
473 }
474 util_vma_heap_init(device->as.priv_heap, user_va_start,
475 low_va_end - user_va_start);
476 device->as.extended_range = true;
477 }
478
479 panvk_device_init_mempools(device);
480
481 #if PAN_ARCH >= 10
482 /* The only reason this is a heap allocation is that PANVK_SUBQUEUE_COUNT
483 * isn't available in the header */
484 device->dump_region_size =
485 vk_zalloc(&device->vk.alloc, PANVK_SUBQUEUE_COUNT * sizeof(uint32_t),
486 alignof(uint32_t), VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
487 if (!device->dump_region_size) {
488 result = panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
489 goto err_free_priv_bos;
490 }
491 #endif
492
493 #if PAN_ARCH <= 9
494 result = panvk_priv_bo_create(
495 device, 128 * 1024 * 1024,
496 PAN_KMOD_BO_FLAG_NO_MMAP | PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT,
497 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, &device->tiler_heap);
498 if (result != VK_SUCCESS)
499 goto err_free_priv_bos;
500
501 result = panvk_priv_bo_create(
502 device,
503 PANVK_JM_MAX_VERTICES_INDIRECT * PANVK_JM_MAX_PER_VTX_ATTRIBUTES_INDIRECT_SIZE,
504 PAN_KMOD_BO_FLAG_NO_MMAP | PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT,
505 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, &device->indirect_varying_buffer);
506 if (result != VK_SUCCESS)
507 goto err_free_priv_bos;
508 #endif
509
510 result = panvk_priv_bo_create(
511 device, pan_sample_positions_buffer_size(),
512 panvk_device_adjust_bo_flags(device, PAN_KMOD_BO_FLAG_WB_MMAP),
513 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, &device->sample_positions);
514 if (result != VK_SUCCESS)
515 goto err_free_priv_bos;
516
517 pan_upload_sample_positions(device->sample_positions->addr.host);
518 panvk_priv_bo_flush(device->sample_positions, 0,
519 pan_sample_positions_buffer_size());
520
521 #if PAN_ARCH >= 10
522 result = panvk_per_arch(init_tiler_oom)(device);
523 if (result != VK_SUCCESS)
524 goto err_free_priv_bos;
525 #endif
526
527 result = panvk_priv_bo_create(device, PAN_PRINTF_BUFFER_SIZE, 0,
528 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE,
529 &device->printf.bo);
530 if (result != VK_SUCCESS)
531 goto err_free_priv_bos;
532
533 u_printf_init(&device->printf.ctx, device->printf.bo,
534 device->printf.bo->addr.host);
535
536 device->drm_fd = device->kmod.dev->fd;
537 vk_device_set_drm_fd(&device->vk, device->kmod.dev->fd);
538
539
540 result = panvk_precomp_init(device);
541 if (result != VK_SUCCESS)
542 goto err_free_priv_bos;
543
544 struct vk_pipeline_cache_create_info cache_info = {
545 .weak_ref = true,
546 };
547 device->vk.mem_cache = vk_pipeline_cache_create(&device->vk, &cache_info, NULL);
548 if (device->vk.mem_cache == NULL) {
549 result = VK_ERROR_OUT_OF_HOST_MEMORY;
550 goto err_free_precomp;
551 }
552
553 #if PAN_ARCH >= 10
554 result = panvk_per_arch(device_draw_context_init)(device);
555 if (result != VK_SUCCESS)
556 goto err_free_mem_cache;
557 #endif
558
559 result = panvk_meta_init(device);
560 if (result != VK_SUCCESS)
561 goto err_free_draw_ctx;
562
563 for (unsigned i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
564 const VkDeviceQueueCreateInfo *queue_create =
565 &pCreateInfo->pQueueCreateInfos[i];
566
567 result = check_global_priority(physical_device, queue_create);
568 if (result != VK_SUCCESS)
569 goto err_finish_queues;
570
571 uint32_t qfi = queue_create->queueFamilyIndex;
572 struct panvk_device_queue_family *qf = &device->queue_families[qfi];
573
574 qf->queues =
575 vk_zalloc(&device->vk.alloc,
576 queue_create->queueCount * sizeof(qf->queues[0]), 8,
577 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
578 if (!qf->queues) {
579 result = panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
580 goto err_finish_queues;
581 }
582
583 for (unsigned q = 0; q < queue_create->queueCount; q++) {
584 result = panvk_queue_create(device, queue_create, q, &qf->queues[q]);
585 if (result != VK_SUCCESS)
586 goto err_finish_queues;
587
588 qf->queue_count++;
589 }
590 }
591
592 result = panvk_per_arch(utrace_context_init)(device);
593 if (result != VK_SUCCESS)
594 goto err_finish_queues;
595
596 #if PAN_ARCH >= 10
597 panvk_utrace_perfetto_init(device, PANVK_SUBQUEUE_COUNT);
598 #else
599 panvk_utrace_perfetto_init(device, 2);
600 #endif
601
602 *pDevice = panvk_device_to_handle(device);
603 return VK_SUCCESS;
604
605 err_finish_queues:
606 for (unsigned i = 0; i < PANVK_QUEUE_FAMILY_COUNT; i++) {
607 struct panvk_device_queue_family *qf = &device->queue_families[i];
608
609 for (unsigned q = 0; q < qf->queue_count; q++)
610 panvk_queue_destroy(qf->queues[q]);
611
612 if (qf->queues)
613 vk_free(&device->vk.alloc, qf->queues);
614 }
615
616 panvk_meta_cleanup(device);
617
618 err_free_draw_ctx:
619 #if PAN_ARCH >= 10
620 panvk_per_arch(device_draw_context_cleanup)(device);
621 err_free_mem_cache:
622 #endif
623 vk_pipeline_cache_destroy(device->vk.mem_cache, NULL);
624 err_free_precomp:
625 panvk_precomp_cleanup(device);
626 err_free_priv_bos:
627 if (device->printf.bo)
628 u_printf_destroy(&device->printf.ctx);
629 panvk_priv_bo_unref(device->printf.bo);
630 panvk_priv_bo_unref(device->tiler_oom.handlers_bo);
631 panvk_priv_bo_unref(device->sample_positions);
632 panvk_priv_bo_unref(device->indirect_varying_buffer);
633 panvk_priv_bo_unref(device->tiler_heap);
634 panvk_device_cleanup_mempools(device);
635 vk_free(&device->vk.alloc, device->dump_region_size);
636 err_free_heaps:
637 pan_kmod_vm_destroy(device->kmod.vm);
638 util_vma_heap_finish(&device->as.heap);
639 if (device->as.extended_range) {
640 util_vma_heap_finish(device->as.priv_heap);
641 free(device->as.priv_heap);
642 device->as.priv_heap = NULL;
643 }
644 if (device->as.split_heap)
645 util_vma_heap_finish(&device->as.fixed_heap);
646 simple_mtx_destroy(&device->as.lock);
647
648 err_destroy_kdev:
649 if (device->debug.decode_ctx)
650 pandecode_destroy_context(device->debug.decode_ctx);
651
652 pan_kmod_dev_destroy(device->kmod.dev);
653
654 err_finish_dev:
655 vk_device_finish(&device->vk);
656
657 err_free_dev:
658 vk_free(&device->vk.alloc, device);
659 return result;
660 }
661
662 void
663 panvk_per_arch(destroy_device)(struct panvk_device *device,
664 const VkAllocationCallbacks *pAllocator)
665 {
666 if (!device)
667 return;
668
669 panvk_per_arch(utrace_context_fini)(device);
670
671 for (unsigned i = 0; i < PANVK_QUEUE_FAMILY_COUNT; i++) {
672 struct panvk_device_queue_family *qf = &device->queue_families[i];
673
674 for (unsigned q = 0; q < qf->queue_count; q++)
675 panvk_queue_destroy(qf->queues[q]);
676
677 if (qf->queues)
678 vk_free(&device->vk.alloc, qf->queues);
679 }
680
681 panvk_precomp_cleanup(device);
682 #if PAN_ARCH >= 10
683 panvk_per_arch(device_draw_context_cleanup)(device);
684 #endif
685 panvk_meta_cleanup(device);
686 vk_pipeline_cache_destroy(device->vk.mem_cache, NULL);
687 pan_kmod_bo_put(device->sparse_mem.blackhole);
688 u_printf_destroy(&device->printf.ctx);
689 panvk_priv_bo_unref(device->printf.bo);
690 panvk_priv_bo_unref(device->tiler_oom.handlers_bo);
691 panvk_priv_bo_unref(device->indirect_varying_buffer);
692 panvk_priv_bo_unref(device->tiler_heap);
693 panvk_priv_bo_unref(device->sample_positions);
694 panvk_device_cleanup_mempools(device);
695 vk_free(&device->vk.alloc, device->dump_region_size);
696 pan_kmod_vm_destroy(device->kmod.vm);
697 util_vma_heap_finish(&device->as.heap);
698 if (device->as.extended_range && (device->as.priv_heap != NULL)) {
699 util_vma_heap_finish(device->as.priv_heap);
700 free(device->as.priv_heap);
701 device->as.priv_heap = NULL;
702 }
703 if (device->as.split_heap)
704 util_vma_heap_finish(&device->as.fixed_heap);
705 simple_mtx_destroy(&device->as.lock);
706
707 if (device->debug.decode_ctx)
708 pandecode_destroy_context(device->debug.decode_ctx);
709
710 pan_kmod_dev_destroy(device->kmod.dev);
711 vk_device_finish(&device->vk);
712 vk_free(&device->vk.alloc, device);
713 }
714
715 VKAPI_ATTR void VKAPI_CALL
716 panvk_per_arch(GetRenderAreaGranularity)(VkDevice device,
717 VkRenderPass renderPass,
718 VkExtent2D *pGranularity)
719 {
720 *pGranularity = (VkExtent2D){32, 32};
721 }
722
723 VKAPI_ATTR void VKAPI_CALL
724 panvk_per_arch(GetRenderingAreaGranularityKHR)(
725 VkDevice _device, const VkRenderingAreaInfoKHR *pRenderingAreaInfo,
726 VkExtent2D *pGranularity)
727 {
728 *pGranularity = (VkExtent2D){32, 32};
729 }