Debian Codegraph @

src/panfrost/vulkan/panvk_device_memory.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "genxml/decode.h"
7
8 #include "vulkan/util/vk_util.h"
9
10 #include "panvk_android.h"
11 #include "panvk_device.h"
12 #include "panvk_device_memory.h"
13 #include "panvk_entrypoints.h"
14
15 #include "pan_props.h"
16
17 #include "util/u_atomic.h"
18 #include "vk_debug_utils.h"
19 #include "vk_log.h"
20
21 static void
22 panvk_memory_emit_report(struct panvk_device *device,
23 const struct panvk_device_memory *mem,
24 const VkMemoryAllocateInfo *alloc_info,
25 VkResult result)
26 {
27 struct panvk_physical_device *pdev =
28 to_panvk_physical_device(device->vk.physical);
29
30 if (likely(!device->vk.memory_reports))
31 return;
32
33 if (result != VK_SUCCESS) {
34 const uint32_t heap_index =
35 pdev->memory.types[alloc_info->memoryTypeIndex].heapIndex;
36 vk_emit_device_memory_report(
37 &device->vk, VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATION_FAILED_EXT,
38 /* mem_obj_id */ 0, alloc_info->allocationSize,
39 VK_OBJECT_TYPE_DEVICE_MEMORY,
40 /* obj_handle */ 0, heap_index);
41 return;
42 }
43
44 VkDeviceMemoryReportEventTypeEXT type;
45 if (alloc_info) {
46 type = mem->vk.import_handle_type
47 ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_IMPORT_EXT
48 : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_ALLOCATE_EXT;
49 } else {
50 type = mem->vk.import_handle_type
51 ? VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_UNIMPORT_EXT
52 : VK_DEVICE_MEMORY_REPORT_EVENT_TYPE_FREE_EXT;
53 }
54
55 const uint32_t heap_index =
56 pdev->memory.types[mem->vk.memory_type_index].heapIndex;
57 vk_emit_device_memory_report(&device->vk, type, mem->bo->handle,
58 mem->bo->size, VK_OBJECT_TYPE_DEVICE_MEMORY,
59 (uintptr_t)(mem), heap_index);
60 }
61
62 VKAPI_ATTR VkResult VKAPI_CALL
63 panvk_AllocateMemory(VkDevice _device,
64 const VkMemoryAllocateInfo *pAllocateInfo,
65 const VkAllocationCallbacks *pAllocator,
66 VkDeviceMemory *pMem)
67 {
68 if (panvk_android_is_ahb_memory(pAllocateInfo)) {
69 return panvk_android_allocate_ahb_memory(_device, pAllocateInfo,
70 pAllocator, pMem);
71 }
72
73 VK_FROM_HANDLE(panvk_device, device, _device);
74 struct panvk_physical_device *physical_device =
75 to_panvk_physical_device(device->vk.physical);
76 struct panvk_device_memory *mem;
77 bool can_be_exported = false;
78 VkResult result;
79
80 assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
81
82 const VkMemoryType *type =
83 &physical_device->memory.types[pAllocateInfo->memoryTypeIndex];
84
85 const VkExportMemoryAllocateInfo *export_info =
86 vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO);
87
88 if (export_info) {
89 if (export_info->handleTypes &
90 ~(VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT |
91 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT))
92 return panvk_error(device, VK_ERROR_INVALID_EXTERNAL_HANDLE);
93 else if (export_info->handleTypes)
94 can_be_exported = true;
95 }
96
97 mem = vk_device_memory_create(&device->vk, pAllocateInfo, pAllocator,
98 sizeof(*mem));
99 if (mem == NULL)
100 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
101
102 const VkImportMemoryFdInfoKHR *fd_info =
103 vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
104
105 if (fd_info && !fd_info->handleType)
106 fd_info = NULL;
107
108 if (fd_info) {
109 assert(
110 fd_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
111 fd_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
112
113 mem->bo = pan_kmod_bo_import(device->kmod.dev, fd_info->fd);
114 if (!mem->bo) {
115 result = panvk_error(device, VK_ERROR_INVALID_EXTERNAL_HANDLE);
116 goto err_destroy_mem;
117 }
118 } else {
119 uint32_t bo_flags = 0;
120
121 /* We don't do cached on exported buffers to keep the pre-WB_MMAP
122 * behavior.
123 */
124 if (!can_be_exported &&
125 (type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT))
126 bo_flags |= PAN_KMOD_BO_FLAG_WB_MMAP;
127
128 bo_flags = panvk_device_adjust_bo_flags(device, bo_flags);
129 mem->bo = pan_kmod_bo_alloc(device->kmod.dev,
130 can_be_exported ? NULL : device->kmod.vm,
131 pAllocateInfo->allocationSize, bo_flags);
132 if (!mem->bo) {
133 result = panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
134 goto err_destroy_mem;
135 }
136 }
137
138 /* Always GPU-map at creation time. */
139 struct pan_kmod_vm_op op = {
140 .type = PAN_KMOD_VM_OP_TYPE_MAP,
141 .va = {
142 .start = PAN_KMOD_VM_MAP_AUTO_VA,
143 .size = pan_kmod_bo_size(mem->bo),
144 },
145 .map = {
146 .bo = mem->bo,
147 .bo_offset = 0,
148 },
149 };
150
151 if (!(device->kmod.vm->flags & PAN_KMOD_VM_FLAG_AUTO_VA)) {
152 if (unlikely(mem->vk.alloc_flags &
153 VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT)) {
154 const VkMemoryOpaqueCaptureAddressAllocateInfo *capture_alloc_info =
155 vk_find_struct_const(pAllocateInfo->pNext,
156 MEMORY_OPAQUE_CAPTURE_ADDRESS_ALLOCATE_INFO);
157 if (capture_alloc_info == NULL ||
158 capture_alloc_info->opaqueCaptureAddress == 0) {
159 op.va.start = panvk_as_alloc(
160 device, &device->as.fixed_heap, op.va.size,
161 pan_choose_gpu_va_alignment(device->kmod.vm, op.va.size));
162 } else {
163 op.va.start = panvk_as_alloc_fixed_address(
164 device, &device->as.fixed_heap,
165 capture_alloc_info->opaqueCaptureAddress, op.va.size);
166 }
167 } else {
168 op.va.start = panvk_as_alloc(
169 device, &device->as.heap, op.va.size,
170 pan_choose_gpu_va_alignment(device->kmod.vm, op.va.size));
171 }
172
173 if (!op.va.start) {
174 result = panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
175 goto err_put_bo;
176 }
177 }
178
179 int ret =
180 pan_kmod_vm_bind(device->kmod.vm, PAN_KMOD_VM_OP_MODE_IMMEDIATE, &op, 1);
181 if (ret) {
182 result = panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
183 goto err_return_va;
184 }
185
186 mem->addr.dev = op.va.start;
187
188 if (fd_info) {
189 /* From the Vulkan spec:
190 *
191 * "Importing memory from a file descriptor transfers ownership of
192 * the file descriptor from the application to the Vulkan
193 * implementation. The application must not perform any operations on
194 * the file descriptor after a successful import."
195 *
196 * If the import fails, we leave the file descriptor open.
197 */
198 close(fd_info->fd);
199 }
200
201 if (device->debug.decode_ctx) {
202 if (PANVK_DEBUG(DUMP) || PANVK_DEBUG(TRACE)) {
203 void *cpu =
204 pan_kmod_bo_mmap(mem->bo, PROT_READ | PROT_WRITE, MAP_SHARED, NULL);
205 if (cpu != MAP_FAILED)
206 mem->debug.host_mapping = cpu;
207 else
208 vk_logw(VK_LOG_OBJS(_device),
209 "failed to map VkMemory for dump or trace.\n");
210 }
211
212 pandecode_inject_mmap(device->debug.decode_ctx, mem->addr.dev,
213 mem->debug.host_mapping, pan_kmod_bo_size(mem->bo),
214 NULL);
215 }
216
217 panvk_memory_emit_report(device, mem, pAllocateInfo, VK_SUCCESS);
218
219 p_atomic_add(&physical_device->memory.heap_used,
220 (uint64_t)pan_kmod_bo_size(mem->bo));
221
222 *pMem = panvk_device_memory_to_handle(mem);
223
224 return VK_SUCCESS;
225
226 err_return_va:
227 if (!(device->kmod.vm->flags & PAN_KMOD_VM_FLAG_AUTO_VA)) {
228 panvk_as_free(device, &device->as.heap, op.va.start, op.va.size);
229 }
230
231 err_put_bo:
232 pan_kmod_bo_put(mem->bo);
233
234 err_destroy_mem:
235 vk_device_memory_destroy(&device->vk, pAllocator, &mem->vk);
236 panvk_memory_emit_report(device, /* mem */ NULL, pAllocateInfo, result);
237
238 return result;
239 }
240
241 VKAPI_ATTR void VKAPI_CALL
242 panvk_FreeMemory(VkDevice _device, VkDeviceMemory _mem,
243 const VkAllocationCallbacks *pAllocator)
244 {
245 VK_FROM_HANDLE(panvk_device, device, _device);
246 VK_FROM_HANDLE(panvk_device_memory, mem, _mem);
247
248 if (mem == NULL)
249 return;
250
251 struct panvk_physical_device *physical_device =
252 to_panvk_physical_device(device->vk.physical);
253
254 p_atomic_add(&physical_device->memory.heap_used,
255 -((int64_t)pan_kmod_bo_size(mem->bo)));
256
257 if (device->debug.decode_ctx) {
258 pandecode_inject_free(device->debug.decode_ctx, mem->addr.dev,
259 pan_kmod_bo_size(mem->bo));
260
261 if (mem->debug.host_mapping)
262 os_munmap(mem->debug.host_mapping, pan_kmod_bo_size(mem->bo));
263 }
264
265 if (mem->addr.host) {
266 ASSERTED int ret = os_munmap(mem->addr.host, pan_kmod_bo_size(mem->bo));
267 assert(!ret);
268 }
269
270 struct pan_kmod_vm_op op = {
271 .type = PAN_KMOD_VM_OP_TYPE_UNMAP,
272 .va = {
273 .start = mem->addr.dev,
274 .size = pan_kmod_bo_size(mem->bo),
275 },
276 };
277
278 ASSERTED int ret =
279 pan_kmod_vm_bind(device->kmod.vm, PAN_KMOD_VM_OP_MODE_IMMEDIATE, &op, 1);
280 assert(!ret);
281
282 if (!(device->kmod.vm->flags & PAN_KMOD_VM_FLAG_AUTO_VA)) {
283 const bool fixed = (mem->vk.alloc_flags &
284 VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT);
285 struct util_vma_heap *heap =
286 fixed ? &device->as.fixed_heap : &device->as.heap;
287 panvk_as_free(device, heap, op.va.start, op.va.size);
288 }
289
290 panvk_memory_emit_report(device, mem, /* alloc_info */ NULL, VK_SUCCESS);
291
292 pan_kmod_bo_put(mem->bo);
293 vk_device_memory_destroy(&device->vk, pAllocator, &mem->vk);
294 }
295
296 VKAPI_ATTR VkResult VKAPI_CALL
297 panvk_MapMemory2KHR(VkDevice _device, const VkMemoryMapInfoKHR *pMemoryMapInfo,
298 void **ppData)
299 {
300 VK_FROM_HANDLE(panvk_device, device, _device);
301 VK_FROM_HANDLE(panvk_device_memory, mem, pMemoryMapInfo->memory);
302
303 if (mem == NULL) {
304 *ppData = NULL;
305 return VK_SUCCESS;
306 }
307
308 const VkDeviceSize offset = pMemoryMapInfo->offset;
309 const VkDeviceSize size = vk_device_memory_range(
310 &mem->vk, pMemoryMapInfo->offset, pMemoryMapInfo->size);
311
312 /* From the Vulkan spec version 1.0.32 docs for MapMemory:
313 *
314 * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
315 * assert(size != 0);
316 * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
317 * equal to the size of the memory minus offset
318 */
319 assert(size > 0);
320 assert(offset + size <= mem->bo->size);
321
322 if (size != (size_t)size) {
323 return panvk_errorf(device, VK_ERROR_MEMORY_MAP_FAILED,
324 "requested size 0x%" PRIx64
325 " does not fit in %u bits",
326 size, (unsigned)(sizeof(size_t) * 8));
327 }
328
329 /* From the Vulkan 1.2.194 spec:
330 *
331 * "memory must not be currently host mapped"
332 */
333 if (mem->addr.host)
334 return panvk_errorf(device, VK_ERROR_MEMORY_MAP_FAILED,
335 "Memory object already mapped.");
336
337 void *host_addr = NULL;
338 int map_flags = MAP_SHARED;
339
340 if (pMemoryMapInfo->flags & VK_MEMORY_MAP_PLACED_BIT_EXT) {
341 const VkMemoryMapPlacedInfoEXT *placed_info = vk_find_struct_const(
342 pMemoryMapInfo->pNext, MEMORY_MAP_PLACED_INFO_EXT);
343 assert(placed_info != NULL);
344 assert(offset == 0 && size == mem->bo->size);
345 host_addr = placed_info->pPlacedAddress;
346 map_flags |= MAP_FIXED;
347 }
348
349 void *addr =
350 pan_kmod_bo_mmap(mem->bo, PROT_READ | PROT_WRITE, map_flags, host_addr);
351 if (addr == MAP_FAILED)
352 return panvk_errorf(device, VK_ERROR_MEMORY_MAP_FAILED,
353 "Memory object couldn't be mapped.");
354
355 assert(!host_addr || addr == host_addr);
356 mem->addr.host = addr;
357 *ppData = (char *)addr + offset;
358
359 return VK_SUCCESS;
360 }
361
362 VKAPI_ATTR VkResult VKAPI_CALL
363 panvk_UnmapMemory2KHR(VkDevice _device,
364 const VkMemoryUnmapInfoKHR *pMemoryUnmapInfo)
365 {
366 VK_FROM_HANDLE(panvk_device, device, _device);
367 VK_FROM_HANDLE(panvk_device_memory, mem, pMemoryUnmapInfo->memory);
368
369 if (pMemoryUnmapInfo->flags & VK_MEMORY_UNMAP_RESERVE_BIT_EXT) {
370 void *reserved =
371 os_mmap(mem->addr.host, pan_kmod_bo_size(mem->bo), PROT_NONE,
372 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
373 if (reserved == MAP_FAILED)
374 return panvk_errorf(device, VK_ERROR_MEMORY_MAP_FAILED,
375 "Failed to reserve VA on unmap.");
376 } else {
377 ASSERTED int ret = os_munmap(mem->addr.host, pan_kmod_bo_size(mem->bo));
378 assert(!ret);
379 }
380
381 mem->addr.host = NULL;
382
383 return VK_SUCCESS;
384 }
385
386 static VkResult
387 sync_memory_ranges(struct panvk_device *device, uint32_t memoryRangeCount,
388 const VkMappedMemoryRange *pMemoryRanges,
389 enum pan_kmod_bo_sync_type type)
390 {
391 for (uint32_t i = 0; i < memoryRangeCount; i++) {
392 const VkMappedMemoryRange *r = &pMemoryRanges[i];
393 if (r->size == 0)
394 continue;
395
396 VK_FROM_HANDLE(panvk_device_memory, memory, r->memory);
397
398 pan_kmod_queue_bo_map_sync(
399 memory->bo, r->offset, (char *)memory->addr.host + r->offset,
400 vk_device_memory_range(&memory->vk, r->offset, r->size), type);
401 }
402
403 return VK_SUCCESS;
404 }
405
406 VKAPI_ATTR VkResult VKAPI_CALL
407 panvk_FlushMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount,
408 const VkMappedMemoryRange *pMemoryRanges)
409 {
410 VK_FROM_HANDLE(panvk_device, device, _device);
411
412 return sync_memory_ranges(device, memoryRangeCount, pMemoryRanges,
413 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH);
414 }
415
416 VKAPI_ATTR VkResult VKAPI_CALL
417 panvk_InvalidateMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount,
418 const VkMappedMemoryRange *pMemoryRanges)
419 {
420 VK_FROM_HANDLE(panvk_device, device, _device);
421 VkResult result =
422 sync_memory_ranges(device, memoryRangeCount, pMemoryRanges,
423 PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE);
424
425 if (result == VK_SUCCESS)
426 pan_kmod_flush_bo_map_syncs(device->kmod.dev);
427
428 return result;
429 }
430
431 VKAPI_ATTR VkResult VKAPI_CALL
432 panvk_GetMemoryFdKHR(VkDevice _device, const VkMemoryGetFdInfoKHR *pGetFdInfo,
433 int *pFd)
434 {
435 VK_FROM_HANDLE(panvk_device, device, _device);
436 VK_FROM_HANDLE(panvk_device_memory, memory, pGetFdInfo->memory);
437
438 assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
439
440 /* At the moment, we support only the below handle types. */
441 assert(
442 pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
443 pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
444
445 int prime_fd = pan_kmod_bo_export(memory->bo);
446 if (prime_fd < 0)
447 return panvk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
448
449 *pFd = prime_fd;
450 return VK_SUCCESS;
451 }
452
453 VKAPI_ATTR VkResult VKAPI_CALL
454 panvk_GetMemoryFdPropertiesKHR(VkDevice _device,
455 VkExternalMemoryHandleTypeFlagBits handleType,
456 int fd,
457 VkMemoryFdPropertiesKHR *pMemoryFdProperties)
458 {
459 VK_FROM_HANDLE(panvk_device, device, _device);
460 const struct panvk_physical_device *phys_dev =
461 to_panvk_physical_device(device->vk.physical);
462
463 assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
464
465 struct pan_kmod_bo *bo = pan_kmod_bo_import(device->kmod.dev, fd);
466 if (!bo)
467 return VK_ERROR_INVALID_EXTERNAL_HANDLE;
468
469 pMemoryFdProperties->memoryTypeBits = 0;
470
471 /* Keep things simple by only allowing host-visible if the BO doesn't require
472 * kernel-side synchronization going through the dma-buf exporter, which is
473 * reflected through the PAN_KMOD_BO_FLAG_FORCE_FULL_KERNEL_SYNC flag.
474 */
475 const bool can_do_host_visible = !(bo->flags & PAN_KMOD_BO_FLAG_NO_MMAP);
476 const bool can_do_host_coherent = !(bo->flags & PAN_KMOD_BO_FLAG_WB_MMAP) ||
477 (bo->flags & PAN_KMOD_BO_FLAG_IO_COHERENT);
478 const bool can_do_host_cached = (bo->flags & PAN_KMOD_BO_FLAG_WB_MMAP);
479
480 pMemoryFdProperties->memoryTypeBits = 0;
481 for (uint32_t i = 0; i < phys_dev->memory.type_count; i++) {
482 if (!can_do_host_visible && (phys_dev->memory.types[i].propertyFlags &
483 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
484 continue;
485 if (!can_do_host_coherent && (phys_dev->memory.types[i].propertyFlags &
486 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
487 continue;
488 if (!can_do_host_cached && (phys_dev->memory.types[i].propertyFlags &
489 VK_MEMORY_PROPERTY_HOST_CACHED_BIT))
490 continue;
491
492 pMemoryFdProperties->memoryTypeBits |= BITFIELD_BIT(i);
493 }
494
495 pan_kmod_bo_put(bo);
496 return VK_SUCCESS;
497 }
498
499 VKAPI_ATTR void VKAPI_CALL
500 panvk_GetDeviceMemoryCommitment(VkDevice device, VkDeviceMemory memory,
501 VkDeviceSize *pCommittedMemoryInBytes)
502 {
503 *pCommittedMemoryInBytes = 0;
504 }
505
506 VKAPI_ATTR uint64_t VKAPI_CALL
507 panvk_GetDeviceMemoryOpaqueCaptureAddress(
508 VkDevice _device, const VkDeviceMemoryOpaqueCaptureAddressInfo *pInfo)
509 {
510 VK_FROM_HANDLE(panvk_device_memory, memory, pInfo->memory);
511
512 return memory->addr.dev;
513 }