Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/asahi/lib/agx_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ agx_cleanup_partial_device(struct agx_device *dev, unsigned initialized)
util_sparse_array_finish(&dev->bo_map);
simple_mtx_destroy(&dev->bo_cache.lock);
simple_mtx_destroy(&dev->bo_map_lock);
simple_mtx_destroy(&dev->bo_mmap_lock);
agxdecode_destroy_context(dev->agxdecode);
}
if (initialized & AGX_INIT_VIRTIO) {
Expand Down Expand Up @@ -1346,6 +1347,7 @@ agx_open_device(void *memctx, struct agx_device *dev)

util_sparse_array_init(&dev->bo_map, sizeof(struct agx_bo), 512);
simple_mtx_init(&dev->bo_map_lock, mtx_plain);
simple_mtx_init(&dev->bo_mmap_lock, mtx_plain);

simple_mtx_init(&dev->bo_cache.lock, mtx_plain);
list_inithead(&dev->bo_cache.lru);
Expand Down
64 changes: 48 additions & 16 deletions src/asahi/lib/agx_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ struct agx_device {

simple_mtx_t bo_map_lock;
struct util_sparse_array bo_map;
/* Held while agx_bo_map_placed() maps a BO or moves its mapping. */
simple_mtx_t bo_mmap_lock;
/* Exclusive upper bound for every loop over bo_map. */
uint32_t max_handle;

Expand Down Expand Up @@ -387,24 +389,54 @@ agx_bo_map_placed(struct agx_bo *bo, void *fixed_addr, size_t length)
if (bo->flags & AGX_BO_HOST_PTR)
return fixed_addr && fixed_addr != bo->_map ? NULL : bo->_map;

/* The BO cache deliberately preserves CPU mappings. A Vulkan allocation
* may therefore inherit a mapping from an earlier allocation even though
* the new VkDeviceMemory object has never been mapped. A placed map must
* move that stale process mapping to the application's reserved address;
* returning it would violate VK_EXT_map_memory_placed.
*
* Keep ordinary mapping reuse unchanged. Only the explicit placed-map
* contract is allowed to relocate an existing BO mapping. */
if (fixed_addr && bo->_map && bo->_map != fixed_addr) {
bo->dev->ops.bo_munmap(bo->dev, bo);
bo->_map = NULL;
bo->_map_size = 0;
void *map = p_atomic_read(&bo->_map);
if (map && (!fixed_addr || map == fixed_addr))
return map;

/* Threads that share a BO can map it for the first time at once (two
* contexts mapping one resource), and each would install its own mapping
* over the other's and leak it. So the mapping is made, or moved, under
* the device's lock and checked again there. On a backend with
* bo_mmap_independent it is made into a copy of the BO and published
* after its size, so a thread that finds it without the lock above finds
* it whole, never a failed mapping. A backend without it is only allowed
* to map the BO itself, so it maps the BO in place under the lock. */
simple_mtx_lock(&bo->dev->bo_mmap_lock);
map = bo->_map;
if (!map || (fixed_addr && map != fixed_addr)) {
/* The BO cache deliberately preserves CPU mappings. A Vulkan
* allocation may therefore inherit a mapping from an earlier allocation
* even though the new VkDeviceMemory object has never been mapped. A
* placed map must move that stale process mapping to the application's
* reserved address; returning it would violate
* VK_EXT_map_memory_placed.
*
* Keep ordinary mapping reuse unchanged. Only the explicit placed-map
* contract is allowed to relocate an existing BO mapping. */
if (map) {
bo->dev->ops.bo_munmap(bo->dev, bo);
p_atomic_set(&bo->_map, NULL);
bo->_map_size = 0;
}

if (bo->dev->ops.bo_mmap_independent) {
struct agx_bo copy = *bo;
copy._map = NULL;
copy._map_size = 0;
bo->dev->ops.bo_mmap(bo->dev, &copy, fixed_addr,
fixed_addr ? length : 0);
map = copy._map;
bo->_map_size = copy._map_size;
p_atomic_set(&bo->_map, map);
} else {
bo->dev->ops.bo_mmap(bo->dev, bo, fixed_addr,
fixed_addr ? length : 0);
map = bo->_map;
}
}
simple_mtx_unlock(&bo->dev->bo_mmap_lock);

if (!bo->_map)
bo->dev->ops.bo_mmap(bo->dev, bo, fixed_addr, fixed_addr ? length : 0);

return bo->_map;
return map;
}

static inline void *
Expand Down
57 changes: 57 additions & 0 deletions src/asahi/vulkan/hk_device_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
result = vk_error(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE);
goto fail_alloc;
}

/* Every byte of allocationSize must be backed by the imported object.
* An image bound to the memory is checked against the BO
* (hk_image_plane_bind()), but a buffer may use the whole of
* allocationSize, which would then run past the BO. */
if (mem->bo->size < pAllocateInfo->allocationSize) {
result = vk_errorf(dev, VK_ERROR_INVALID_EXTERNAL_HANDLE,
"allocationSize %" PRIu64 " exceeds the imported "
"object (%" PRIu64 " bytes)",
(uint64_t)pAllocateInfo->allocationSize,
(uint64_t)mem->bo->size);
agx_bo_unreference(&dev->dev, mem->bo);
mem->bo = NULL;
goto fail_alloc;
}
assert(!(flags & ~mem->bo->flags));
mem->alias_import = true;
} else if (host_info && host_info->handleType) {
Expand Down Expand Up @@ -663,6 +678,46 @@ hk_AllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
return result;
}

/* The CPU mapping that host image copies of this memory go through.
*
* It is this object's own mapping of the whole BO (agx_bo_map_owned()), made
* at the first copy and kept until vkFreeMemory. The application's
* vkMapMemory2 and vkUnmapMemory2 of the memory, which may run on other
* threads while copies do, map, move and unmap only the BO's shared mapping
* or their own, so they never touch the one a copy is using.
*
* Host image copies need no external synchronization on the memory, so two
* threads can make the mapping at once: the first one installed is kept and
* the other is unmapped. Host memory stays where the application put it, and
* a backend that cannot map a BO twice has only the shared mapping; copies
* use the BO's shared mapping there. NULL if the BO cannot be mapped.
*/
void *
hk_device_memory_copy_map(struct hk_device_memory *mem)
{
void *map = p_atomic_read(&mem->copy_map);
if (map)
return map;

if (!mem->bo->dev->ops.bo_mmap_independent ||
(mem->bo->flags & AGX_BO_HOST_PTR)) {
map = agx_bo_map(mem->bo);
} else {
size_t size;
map = agx_bo_map_owned(mem->bo, NULL, 0, &size);
if (map == NULL)
return NULL;

void *installed = p_atomic_cmpxchg(&mem->copy_map, NULL, map);
if (installed != NULL) {
agx_bo_unmap_owned(mem->bo, map, size);
return installed;
}
}

return map;
}

VKAPI_ATTR void VKAPI_CALL
hk_FreeMemory(VkDevice device, VkDeviceMemory _mem,
const VkAllocationCallbacks *pAllocator)
Expand All @@ -687,6 +742,8 @@ hk_FreeMemory(VkDevice device, VkDeviceMemory _mem,
* the BO; an owned one belongs to this object alone. */
if (mem->map_owned)
agx_bo_unmap_owned(mem->bo, mem->map, mem->map_size);
if (mem->copy_map)
agx_bo_unmap_owned(mem->bo, mem->copy_map, mem->bo->size);

/* The BO may outlive this object in the BO cache. */
hk_set_memory_priority(dev, mem, 0.5f);
Expand Down
7 changes: 7 additions & 0 deletions src/asahi/vulkan/hk_device_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ struct hk_device_memory {
size_t map_size;
bool map_owned;

/* The mapping host image copies go through (hk_device_memory_copy_map()),
* NULL until the first copy. It covers the whole BO and lives until
* vkFreeMemory. */
void *copy_map;

/* Residency preference the kernel accepted (memory priority 1.0). */
bool keep_resident;

Expand All @@ -63,6 +68,8 @@ hk_device_memory_addr(const struct hk_device_memory *mem)
return mem->replay_va ? mem->replay_va->addr : mem->bo->va->addr;
}

void *hk_device_memory_copy_map(struct hk_device_memory *mem);

extern const VkExternalMemoryProperties hk_opaque_fd_mem_props;
extern const VkExternalMemoryProperties hk_dma_buf_mem_props;
extern const VkExternalMemoryProperties hk_host_ptr_mem_props;
78 changes: 60 additions & 18 deletions src/asahi/vulkan/hk_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -2263,7 +2263,8 @@ hk_image_plane_bind(struct hk_device *dev, struct hk_image_plane *plane,
&bo_offset_B))
return false;

plane->map = agx_bo_map(mem->bo) + bo_offset_B;
plane->mem = mem;
plane->bo_offset_B = bo_offset_B;
plane->rem = mem_size_B - bo_offset_B;
}

Expand Down Expand Up @@ -2337,7 +2338,19 @@ hk_plane_index(VkFormat format, VkImageAspectFlags aspect_mask)
}
}

static void
/* The CPU address of a bound plane for host image copies, NULL if the BO
* cannot be mapped. The copies go through the memory's own copy mapping
* rather than the BO mapping vkMapMemory2 gives the application, which
* vkUnmapMemory2 removes and a placed vkMapMemory2 moves, on any thread.
*/
static char *
hk_image_plane_map(const struct hk_image_plane *plane)
{
char *map = hk_device_memory_copy_map(plane->mem);
return map ? map + plane->bo_offset_B : NULL;
}

static VkResult
hk_copy_memory_to_image(struct hk_device *device, struct hk_image *dst_image,
const VkMemoryToImageCopyEXT *info, bool copy_memcpy)
{
Expand Down Expand Up @@ -2376,8 +2389,12 @@ hk_copy_memory_to_image(struct hk_device *device, struct hk_image *dst_image,
bool tiled = ail_is_level_twiddled_uncompressed(
layout, info->imageSubresource.mipLevel);

char *dst = hk_image_plane_map(&dst_image->planes[plane]);
if (!dst)
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);

const char *src = (const char *)info->pHostPointer;
char *dst = (char *)dst_image->planes[plane].map + image_offset;
dst += image_offset;
for (unsigned layer = 0; layer < layers;
layer++, src += src_layer_stride, dst += dst_layer_stride) {
if (copy_memcpy) {
Expand All @@ -2394,6 +2411,8 @@ hk_copy_memory_to_image(struct hk_device *device, struct hk_image *dst_image,
offset.y, extent.width, extent.height);
}
}

return VK_SUCCESS;
}

VKAPI_ATTR VkResult VKAPI_CALL
Expand All @@ -2404,14 +2423,17 @@ hk_CopyMemoryToImageEXT(VkDevice _device,
VK_FROM_HANDLE(hk_image, dst_image, info->dstImage);

for (unsigned i = 0; i < info->regionCount; i++) {
hk_copy_memory_to_image(device, dst_image, &info->pRegions[i],
info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
VkResult result =
hk_copy_memory_to_image(device, dst_image, &info->pRegions[i],
info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
if (result != VK_SUCCESS)
return result;
}

return VK_SUCCESS;
}

static void
static VkResult
hk_copy_image_to_memory(struct hk_device *device, struct hk_image *src_image,
const VkImageToMemoryCopyEXT *info, bool copy_memcpy)
{
Expand Down Expand Up @@ -2456,7 +2478,11 @@ hk_copy_image_to_memory(struct hk_device *device, struct hk_image *src_image,
bool tiled = ail_is_level_twiddled_uncompressed(
layout, info->imageSubresource.mipLevel);

const char *src = (const char *)src_image->planes[plane].map + image_offset;
const char *src = hk_image_plane_map(&src_image->planes[plane]);
if (!src)
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);

src += image_offset;
char *dst = (char *)info->pHostPointer;
for (unsigned layer = 0; layer < layers;
layer++, src += src_layer_stride, dst += dst_layer_stride) {
Expand All @@ -2476,6 +2502,8 @@ hk_copy_image_to_memory(struct hk_device *device, struct hk_image *src_image,
dst_pitch, offset.x, offset.y, extent.width, extent.height);
}
}

return VK_SUCCESS;
}

VKAPI_ATTR VkResult VKAPI_CALL
Expand All @@ -2486,14 +2514,17 @@ hk_CopyImageToMemoryEXT(VkDevice _device,
VK_FROM_HANDLE(hk_image, image, info->srcImage);

for (unsigned i = 0; i < info->regionCount; i++) {
hk_copy_image_to_memory(device, image, &info->pRegions[i],
info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
VkResult result =
hk_copy_image_to_memory(device, image, &info->pRegions[i],
info->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT);
if (result != VK_SUCCESS)
return result;
}

return VK_SUCCESS;
}

static void
static VkResult
hk_copy_image_to_image_cpu(struct hk_device *device, struct hk_image *src_image,
struct hk_image *dst_image, const VkImageCopy2 *info,
bool copy_memcpy)
Expand Down Expand Up @@ -2542,9 +2573,13 @@ hk_copy_image_to_image_cpu(struct hk_device *device, struct hk_image *src_image,
bool dst_tiled = ail_is_level_twiddled_uncompressed(
dst_layout, info->dstSubresource.mipLevel);

const char *src =
(const char *)src_image->planes[src_plane].map + src_image_offset;
char *dst = (char *)dst_image->planes[dst_plane].map + dst_image_offset;
const char *src = hk_image_plane_map(&src_image->planes[src_plane]);
char *dst = hk_image_plane_map(&dst_image->planes[dst_plane]);
if (!src || !dst)
return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED);

src += src_image_offset;
dst += dst_image_offset;
for (unsigned layer = 0; layer < layers_to_copy;
layer++, src += src_layer_stride, dst += dst_layer_stride) {

Expand Down Expand Up @@ -2635,6 +2670,8 @@ hk_copy_image_to_image_cpu(struct hk_device *device, struct hk_image *src_image,
free(temp_tile);
}
}

return VK_SUCCESS;
}

VKAPI_ATTR VkResult VKAPI_CALL
Expand All @@ -2648,20 +2685,25 @@ hk_CopyImageToImageEXT(VkDevice _device,
pCopyImageToImageInfo->flags & VK_HOST_IMAGE_COPY_MEMCPY_EXT;

for (uint32_t i = 0; i < pCopyImageToImageInfo->regionCount; ++i) {
VkResult result;
if (hk_split_depth_stencil(src_image->vk.format)) {
VkImageCopy2 info = pCopyImageToImageInfo->pRegions[i];
u_foreach_bit(b, info.dstSubresource.aspectMask) {
info.srcSubresource.aspectMask = BITFIELD_BIT(b);
info.dstSubresource.aspectMask = BITFIELD_BIT(b);
hk_copy_image_to_image_cpu(device, src_image, dst_image, &info,
copy_memcpy);
result = hk_copy_image_to_image_cpu(device, src_image, dst_image,
&info, copy_memcpy);
if (result != VK_SUCCESS)
return result;
}
continue;
}

hk_copy_image_to_image_cpu(device, src_image, dst_image,
pCopyImageToImageInfo->pRegions + i,
copy_memcpy);
result = hk_copy_image_to_image_cpu(device, src_image, dst_image,
pCopyImageToImageInfo->pRegions + i,
copy_memcpy);
if (result != VK_SUCCESS)
return result;
}

return VK_SUCCESS;
Expand Down
8 changes: 6 additions & 2 deletions src/asahi/vulkan/hk_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ struct hk_image_plane {
/* Bytes of that VA counted in hk_device::sparse_scratch_B. */
uint64_t sparse_scratch_B;

/* For host image copy */
void *map;
/* For host image copy: the memory bound to this plane and the plane's
* offset in its BO. The copies go through the memory's copy mapping
* (hk_image_plane_map()).
*/
struct hk_device_memory *mem;
uint64_t bo_offset_B;
uint32_t rem;

/* If the image has sparse residency, its residency is tracked in this
Expand Down
Loading