| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "hk_device_memory.h" |
| 8 |
|
| 9 |
#include "hk_device.h" |
| 10 |
#include "hk_entrypoints.h" |
| 11 |
#include "hk_image.h" |
| 12 |
#include "hk_physical_device.h" |
| 13 |
|
| 14 |
#include "asahi/lib/agx_bo.h" |
| 15 |
#include "util/u_atomic.h" |
| 16 |
|
| 17 |
#include <inttypes.h> |
| 18 |
#include <sys/mman.h> |
| 19 |
|
| 20 |
/* Supports opaque fd only */ |
| 21 |
const VkExternalMemoryProperties hk_opaque_fd_mem_props = { |
| 22 |
.externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | |
| 23 |
VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT, |
| 24 |
.exportFromImportedHandleTypes = |
| 25 |
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, |
| 26 |
.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, |
| 27 |
}; |
| 28 |
|
| 29 |
/* Supports opaque fd and dma_buf. */ |
| 30 |
const VkExternalMemoryProperties hk_dma_buf_mem_props = { |
| 31 |
.externalMemoryFeatures = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | |
| 32 |
VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT, |
| 33 |
.exportFromImportedHandleTypes = |
| 34 |
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT | |
| 35 |
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, |
| 36 |
.compatibleHandleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT | |
| 37 |
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, |
| 38 |
}; |
| 39 |
|
| 40 |
static enum agx_bo_flags |
| 41 |
hk_memory_type_flags(const VkMemoryType *type, |
| 42 |
VkExternalMemoryHandleTypeFlagBits handle_types) |
| 43 |
{ |
| 44 |
unsigned flags = 0; |
| 45 |
|
| 46 |
if (handle_types) |
| 47 |
flags |= AGX_BO_SHARED | AGX_BO_SHAREABLE; |
| 48 |
|
| 49 |
return flags; |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
hk_add_ext_bo_locked(struct hk_device *dev, struct agx_bo *bo) |
| 54 |
{ |
| 55 |
uint32_t id = bo->uapi_handle; |
| 56 |
|
| 57 |
unsigned count = util_dynarray_num_elements(&dev->external_bos.list, |
| 58 |
struct asahi_ccmd_submit_res); |
| 59 |
|
| 60 |
for (unsigned i = 0; i < count; i++) { |
| 61 |
struct asahi_ccmd_submit_res *p = util_dynarray_element( |
| 62 |
&dev->external_bos.list, struct asahi_ccmd_submit_res, i); |
| 63 |
|
| 64 |
if (p->res_id == id) { |
| 65 |
++*util_dynarray_element(&dev->external_bos.counts, unsigned, i); |
| 66 |
return; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
struct asahi_ccmd_submit_res res = { |
| 71 |
.res_id = id, |
| 72 |
.flags = ASAHI_EXTRES_READ | ASAHI_EXTRES_WRITE, |
| 73 |
}; |
| 74 |
util_dynarray_append(&dev->external_bos.list, res); |
| 75 |
util_dynarray_append_typed(&dev->external_bos.counts, unsigned, 1); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
hk_add_ext_bo(struct hk_device *dev, struct agx_bo *bo) |
| 80 |
{ |
| 81 |
if (dev->dev.is_virtio) { |
| 82 |
u_rwlock_wrlock(&dev->external_bos.lock); |
| 83 |
hk_add_ext_bo_locked(dev, bo); |
| 84 |
u_rwlock_wrunlock(&dev->external_bos.lock); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
hk_remove_ext_bo_locked(struct hk_device *dev, struct agx_bo *bo) |
| 90 |
{ |
| 91 |
uint32_t id = bo->uapi_handle; |
| 92 |
unsigned count = util_dynarray_num_elements(&dev->external_bos.list, |
| 93 |
struct asahi_ccmd_submit_res); |
| 94 |
|
| 95 |
for (unsigned i = 0; i < count; i++) { |
| 96 |
struct asahi_ccmd_submit_res *p = util_dynarray_element( |
| 97 |
&dev->external_bos.list, struct asahi_ccmd_submit_res, i); |
| 98 |
|
| 99 |
if (p->res_id == id) { |
| 100 |
unsigned *ctr = |
| 101 |
util_dynarray_element(&dev->external_bos.counts, unsigned, i); |
| 102 |
if (!--*ctr) { |
| 103 |
*ctr = util_dynarray_pop(&dev->external_bos.counts, unsigned); |
| 104 |
*p = util_dynarray_pop(&dev->external_bos.list, |
| 105 |
struct asahi_ccmd_submit_res); |
| 106 |
} |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
UNREACHABLE("BO not found"); |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
hk_remove_ext_bo(struct hk_device *dev, struct agx_bo *bo) |
| 116 |
{ |
| 117 |
if (dev->dev.is_virtio) { |
| 118 |
u_rwlock_wrlock(&dev->external_bos.lock); |
| 119 |
hk_remove_ext_bo_locked(dev, bo); |
| 120 |
u_rwlock_wrunlock(&dev->external_bos.lock); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 125 |
hk_GetMemoryFdPropertiesKHR(VkDevice device, |
| 126 |
VkExternalMemoryHandleTypeFlagBits handleType, |
| 127 |
int fd, |
| 128 |
VkMemoryFdPropertiesKHR *pMemoryFdProperties) |
| 129 |
{ |
| 130 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 131 |
struct hk_physical_device *pdev = hk_device_physical(dev); |
| 132 |
struct agx_bo *bo; |
| 133 |
|
| 134 |
switch (handleType) { |
| 135 |
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: |
| 136 |
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT: |
| 137 |
bo = agx_bo_import(&dev->dev, fd); |
| 138 |
if (bo == NULL) |
| 139 |
return vk_error(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE); |
| 140 |
break; |
| 141 |
default: |
| 142 |
return vk_error(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE); |
| 143 |
} |
| 144 |
|
| 145 |
uint32_t type_bits = 0; |
| 146 |
for (unsigned t = 0; t < ARRAY_SIZE(pdev->mem_types); t++) { |
| 147 |
const unsigned flags = |
| 148 |
hk_memory_type_flags(&pdev->mem_types[t], handleType); |
| 149 |
if (!(flags & ~bo->flags)) |
| 150 |
type_bits |= (1 << t); |
| 151 |
} |
| 152 |
|
| 153 |
pMemoryFdProperties->memoryTypeBits = type_bits; |
| 154 |
|
| 155 |
agx_bo_unreference(&dev->dev, bo); |
| 156 |
|
| 157 |
return VK_SUCCESS; |
| 158 |
} |
| 159 |
|
| 160 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 161 |
hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo, |
| 162 |
const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMem) |
| 163 |
{ |
| 164 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 165 |
struct hk_physical_device *pdev = hk_device_physical(dev); |
| 166 |
struct hk_device_memory *mem; |
| 167 |
VkResult result = VK_SUCCESS; |
| 168 |
|
| 169 |
const VkImportMemoryFdInfoKHR *fd_info = |
| 170 |
vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR); |
| 171 |
const VkExportMemoryAllocateInfo *export_info = |
| 172 |
vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO); |
| 173 |
const VkMemoryType *type = &pdev->mem_types[pAllocateInfo->memoryTypeIndex]; |
| 174 |
|
| 175 |
VkExternalMemoryHandleTypeFlagBits handle_types = 0; |
| 176 |
if (export_info != NULL) |
| 177 |
handle_types |= export_info->handleTypes; |
| 178 |
if (fd_info != NULL) |
| 179 |
handle_types |= fd_info->handleType; |
| 180 |
|
| 181 |
const unsigned flags = hk_memory_type_flags(type, handle_types); |
| 182 |
|
| 183 |
uint32_t alignment = 16384; /* Apple page size */ |
| 184 |
|
| 185 |
struct hk_memory_heap *heap = &pdev->mem_heaps[type->heapIndex]; |
| 186 |
if (p_atomic_read(&heap->used) > heap->size) |
| 187 |
return vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 188 |
|
| 189 |
const uint64_t aligned_size = |
| 190 |
align64(pAllocateInfo->allocationSize, alignment); |
| 191 |
|
| 192 |
mem = vk_device_memory_create(&dev->vk, pAllocateInfo, pAllocator, |
| 193 |
sizeof(*mem)); |
| 194 |
if (!mem) |
| 195 |
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 196 |
|
| 197 |
mem->map = NULL; |
| 198 |
if (fd_info && fd_info->handleType) { |
| 199 |
assert( |
| 200 |
fd_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT || |
| 201 |
fd_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT); |
| 202 |
|
| 203 |
mem->bo = agx_bo_import(&dev->dev, fd_info->fd); |
| 204 |
if (mem->bo == NULL) { |
| 205 |
result = vk_error(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE); |
| 206 |
goto fail_alloc; |
| 207 |
} |
| 208 |
assert(!(flags & ~mem->bo->flags)); |
| 209 |
} else { |
| 210 |
enum agx_bo_flags flags = 0; |
| 211 |
if (handle_types) |
| 212 |
flags |= AGX_BO_SHAREABLE; |
| 213 |
if (type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) |
| 214 |
flags |= AGX_BO_WRITEBACK; |
| 215 |
|
| 216 |
mem->bo = agx_bo_create(&dev->dev, aligned_size, 0, flags, "App memory"); |
| 217 |
if (!mem->bo) { |
| 218 |
result = vk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 219 |
goto fail_alloc; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/* Shadow map in case this is used for a sparse resident buffer */ |
| 224 |
int ret = agx_bo_bind(&dev->dev, mem->bo, |
| 225 |
agx_rw_addr_to_ro(&dev->dev, mem->bo->va->addr), |
| 226 |
mem->bo->size, 0, DRM_ASAHI_BIND_READ); |
| 227 |
if (ret) |
| 228 |
return VK_ERROR_UNKNOWN; |
| 229 |
|
| 230 |
if (mem->bo->flags & (AGX_BO_SHAREABLE | AGX_BO_SHARED)) |
| 231 |
hk_add_ext_bo(dev, mem->bo); |
| 232 |
|
| 233 |
if (fd_info && fd_info->handleType) { |
| 234 |
/* From the Vulkan spec: |
| 235 |
* |
| 236 |
* "Importing memory from a file descriptor transfers ownership of |
| 237 |
* the file descriptor from the application to the Vulkan |
| 238 |
* implementation. The application must not perform any operations on |
| 239 |
* the file descriptor after a successful import." |
| 240 |
* |
| 241 |
* If the import fails, we leave the file descriptor open. |
| 242 |
*/ |
| 243 |
close(fd_info->fd); |
| 244 |
} |
| 245 |
|
| 246 |
uint64_t heap_used = p_atomic_add_return(&heap->used, mem->bo->size); |
| 247 |
if (heap_used > heap->size) { |
| 248 |
hk_FreeMemory(device, hk_device_memory_to_handle(mem), pAllocator); |
| 249 |
return vk_errorf(device, VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 250 |
"Out of heap memory"); |
| 251 |
} |
| 252 |
|
| 253 |
*pMem = hk_device_memory_to_handle(mem); |
| 254 |
|
| 255 |
return VK_SUCCESS; |
| 256 |
|
| 257 |
fail_alloc: |
| 258 |
vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk); |
| 259 |
return result; |
| 260 |
} |
| 261 |
|
| 262 |
VKAPI_ATTR void VKAPI_CALL |
| 263 |
hk_FreeMemory(VkDevice device, VkDeviceMemory _mem, |
| 264 |
const VkAllocationCallbacks *pAllocator) |
| 265 |
{ |
| 266 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 267 |
VK_FROM_HANDLE(hk_device_memory, mem, _mem); |
| 268 |
struct hk_physical_device *pdev = hk_device_physical(dev); |
| 269 |
|
| 270 |
if (!mem) |
| 271 |
return; |
| 272 |
|
| 273 |
const VkMemoryType *type = &pdev->mem_types[mem->vk.memory_type_index]; |
| 274 |
struct hk_memory_heap *heap = &pdev->mem_heaps[type->heapIndex]; |
| 275 |
p_atomic_add(&heap->used, -((int64_t)mem->bo->size)); |
| 276 |
|
| 277 |
if (mem->bo->flags & (AGX_BO_SHAREABLE | AGX_BO_SHARED)) |
| 278 |
hk_remove_ext_bo(dev, mem->bo); |
| 279 |
|
| 280 |
agx_bo_unreference(&dev->dev, mem->bo); |
| 281 |
|
| 282 |
vk_device_memory_destroy(&dev->vk, pAllocator, &mem->vk); |
| 283 |
} |
| 284 |
|
| 285 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 286 |
hk_MapMemory2KHR(VkDevice device, const VkMemoryMapInfoKHR *pMemoryMapInfo, |
| 287 |
void **ppData) |
| 288 |
{ |
| 289 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 290 |
VK_FROM_HANDLE(hk_device_memory, mem, pMemoryMapInfo->memory); |
| 291 |
|
| 292 |
if (mem == NULL) { |
| 293 |
*ppData = NULL; |
| 294 |
return VK_SUCCESS; |
| 295 |
} |
| 296 |
|
| 297 |
const VkDeviceSize offset = pMemoryMapInfo->offset; |
| 298 |
const VkDeviceSize size = vk_device_memory_range( |
| 299 |
&mem->vk, pMemoryMapInfo->offset, pMemoryMapInfo->size); |
| 300 |
|
| 301 |
void *fixed_addr = NULL; |
| 302 |
if (pMemoryMapInfo->flags & VK_MEMORY_MAP_PLACED_BIT_EXT) { |
| 303 |
const VkMemoryMapPlacedInfoEXT *placed_info = vk_find_struct_const( |
| 304 |
pMemoryMapInfo->pNext, MEMORY_MAP_PLACED_INFO_EXT); |
| 305 |
fixed_addr = placed_info->pPlacedAddress; |
| 306 |
} |
| 307 |
|
| 308 |
/* From the Vulkan spec version 1.0.32 docs for MapMemory: |
| 309 |
* |
| 310 |
* * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0 |
| 311 |
* assert(size != 0); |
| 312 |
* * If size is not equal to VK_WHOLE_SIZE, size must be less than or |
| 313 |
* equal to the size of the memory minus offset |
| 314 |
*/ |
| 315 |
assert(size > 0); |
| 316 |
assert(offset + size <= mem->bo->size); |
| 317 |
|
| 318 |
if (size != (size_t)size) { |
| 319 |
return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED, |
| 320 |
"requested size 0x%" PRIx64 " does not fit in %u bits", |
| 321 |
size, (unsigned)(sizeof(size_t) * 8)); |
| 322 |
} |
| 323 |
|
| 324 |
/* From the Vulkan 1.2.194 spec: |
| 325 |
* |
| 326 |
* "memory must not be currently host mapped" |
| 327 |
*/ |
| 328 |
if (mem->map != NULL) { |
| 329 |
return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED, |
| 330 |
"Memory object already mapped."); |
| 331 |
} |
| 332 |
|
| 333 |
mem->map = agx_bo_map_placed(mem->bo, fixed_addr); |
| 334 |
assert(!fixed_addr || mem->map == fixed_addr); |
| 335 |
*ppData = mem->map + offset; |
| 336 |
|
| 337 |
return VK_SUCCESS; |
| 338 |
} |
| 339 |
|
| 340 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 341 |
hk_UnmapMemory2KHR(VkDevice device, |
| 342 |
const VkMemoryUnmapInfoKHR *pMemoryUnmapInfo) |
| 343 |
{ |
| 344 |
VK_FROM_HANDLE(hk_device_memory, mem, pMemoryUnmapInfo->memory); |
| 345 |
|
| 346 |
if (mem == NULL) |
| 347 |
return VK_SUCCESS; |
| 348 |
|
| 349 |
if (pMemoryUnmapInfo->flags & VK_MEMORY_UNMAP_RESERVE_BIT_EXT) { |
| 350 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 351 |
|
| 352 |
void *err = mmap(mem->bo->_map, mem->bo->size, PROT_NONE, |
| 353 |
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); |
| 354 |
if (err == MAP_FAILED) { |
| 355 |
return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED, |
| 356 |
"Failed to map over original mapping"); |
| 357 |
} |
| 358 |
mem->bo->_map = NULL; |
| 359 |
} else { |
| 360 |
if (mem->bo->_map) { |
| 361 |
munmap(mem->bo->_map, mem->bo->size); |
| 362 |
mem->bo->_map = NULL; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
mem->map = NULL; |
| 367 |
|
| 368 |
return VK_SUCCESS; |
| 369 |
} |
| 370 |
|
| 371 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 372 |
hk_FlushMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 373 |
const VkMappedMemoryRange *pMemoryRanges) |
| 374 |
{ |
| 375 |
return VK_SUCCESS; |
| 376 |
} |
| 377 |
|
| 378 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 379 |
hk_InvalidateMappedMemoryRanges(VkDevice device, uint32_t memoryRangeCount, |
| 380 |
const VkMappedMemoryRange *pMemoryRanges) |
| 381 |
{ |
| 382 |
return VK_SUCCESS; |
| 383 |
} |
| 384 |
|
| 385 |
VKAPI_ATTR void VKAPI_CALL |
| 386 |
hk_GetDeviceMemoryCommitment(VkDevice device, VkDeviceMemory _mem, |
| 387 |
VkDeviceSize *pCommittedMemoryInBytes) |
| 388 |
{ |
| 389 |
VK_FROM_HANDLE(hk_device_memory, mem, _mem); |
| 390 |
|
| 391 |
*pCommittedMemoryInBytes = mem->bo->size; |
| 392 |
} |
| 393 |
|
| 394 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 395 |
hk_GetMemoryFdKHR(VkDevice device, const VkMemoryGetFdInfoKHR *pGetFdInfo, |
| 396 |
int *pFD) |
| 397 |
{ |
| 398 |
VK_FROM_HANDLE(hk_device, dev, device); |
| 399 |
VK_FROM_HANDLE(hk_device_memory, memory, pGetFdInfo->memory); |
| 400 |
|
| 401 |
switch (pGetFdInfo->handleType) { |
| 402 |
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: |
| 403 |
case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT: |
| 404 |
*pFD = agx_bo_export(&dev->dev, memory->bo); |
| 405 |
return VK_SUCCESS; |
| 406 |
default: |
| 407 |
assert(!"unsupported handle type"); |
| 408 |
return vk_error(dev, VK_ERROR_FEATURE_NOT_PRESENT); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
VKAPI_ATTR uint64_t VKAPI_CALL |
| 413 |
hk_GetDeviceMemoryOpaqueCaptureAddress( |
| 414 |
UNUSED VkDevice device, |
| 415 |
UNUSED const VkDeviceMemoryOpaqueCaptureAddressInfo *pInfo) |
| 416 |
{ |
| 417 |
/* Addresses are replayed at buffer and image creation, not memory. */ |
| 418 |
return 0; |
| 419 |
} |