tu: Handle VkDeviceMemory BO unmapping in VkUnmapMemory

Unmapping the BO associated with a VkDeviceMemory object was
previously handled when destroying the object, this behavior
isn't problematic when the mapping is driver-controlled but
with VK_EXT_map_memory_placed, the user may want control over
the allocation and reuse the mapping after calling unmap.

Signed-off-by: Mark Collins <mark@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28928>
This commit is contained in:
Mark Collins 2024-04-25 11:37:15 +00:00
parent 854640ea26
commit 6d2de5b5b0
3 changed files with 36 additions and 2 deletions

View file

@ -2889,8 +2889,13 @@ tu_MapMemory2KHR(VkDevice _device, const VkMemoryMapInfoKHR *pMemoryMapInfo, voi
VKAPI_ATTR VkResult VKAPI_CALL
tu_UnmapMemory2KHR(VkDevice _device, const VkMemoryUnmapInfoKHR *pMemoryUnmapInfo)
{
/* TODO: unmap here instead of waiting for FreeMemory */
return VK_SUCCESS;
VK_FROM_HANDLE(tu_device, device, _device);
VK_FROM_HANDLE(tu_device_memory, mem, pMemoryUnmapInfo->memory);
if (mem == NULL)
return VK_SUCCESS;
return tu_bo_unmap(device, mem->bo, false);
}
static void

View file

@ -16,10 +16,13 @@
#include <sys/sysmacros.h>
#endif
#include <sys/mman.h>
#include "util/libdrm.h"
#include "tu_device.h"
#include "tu_knl.h"
#include "tu_rmv.h"
VkResult
@ -60,6 +63,29 @@ tu_bo_map(struct tu_device *dev, struct tu_bo *bo)
return dev->instance->knl->bo_map(dev, bo);
}
VkResult
tu_bo_unmap(struct tu_device *dev, struct tu_bo *bo, bool reserve)
{
if (!bo->map)
return VK_SUCCESS;
TU_RMV(bo_unmap, dev, bo);
if (reserve) {
void *map = mmap(bo->map, bo->size, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (map == MAP_FAILED)
return vk_errorf(dev, VK_ERROR_MEMORY_MAP_FAILED,
"Failed to replace mapping with reserved memory");
} else {
munmap(bo->map, bo->size);
}
bo->map = NULL;
return VK_SUCCESS;
}
void tu_bo_allow_dump(struct tu_device *dev, struct tu_bo *bo)
{
dev->instance->knl->bo_allow_dump(dev, bo);

View file

@ -142,6 +142,9 @@ tu_bo_finish(struct tu_device *dev, struct tu_bo *bo);
VkResult
tu_bo_map(struct tu_device *dev, struct tu_bo *bo);
VkResult
tu_bo_unmap(struct tu_device *dev, struct tu_bo *bo, bool reserve);
void tu_bo_allow_dump(struct tu_device *dev, struct tu_bo *bo);
void tu_bo_set_metadata(struct tu_device *dev, struct tu_bo *bo,