From 59e5f95bdccf70219dd67adabd8b7f09b62710bf Mon Sep 17 00:00:00 2001 From: "Kristian H. Kristensen" Date: Tue, 7 Apr 2020 10:33:27 -0700 Subject: [PATCH] turnip: Move tu_bo functions to tu_drm.c Part-of: --- src/freedreno/vulkan/tu_device.c | 95 -------------------------- src/freedreno/vulkan/tu_drm.c | 107 ++++++++++++++++++++++++++++-- src/freedreno/vulkan/tu_private.h | 15 ----- 3 files changed, 101 insertions(+), 116 deletions(-) diff --git a/src/freedreno/vulkan/tu_device.c b/src/freedreno/vulkan/tu_device.c index 9f2881724cf..b4ae1268d7e 100644 --- a/src/freedreno/vulkan/tu_device.c +++ b/src/freedreno/vulkan/tu_device.c @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -68,100 +67,6 @@ tu_device_get_cache_uuid(uint16_t family, void *uuid) return 0; } -static VkResult -tu_bo_init(struct tu_device *dev, - struct tu_bo *bo, - uint32_t gem_handle, - uint64_t size) -{ - uint64_t iova = tu_gem_info_iova(dev, gem_handle); - if (!iova) - return VK_ERROR_OUT_OF_DEVICE_MEMORY; - - *bo = (struct tu_bo) { - .gem_handle = gem_handle, - .size = size, - .iova = iova, - }; - - return VK_SUCCESS; -} - -VkResult -tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size) -{ - /* TODO: Choose better flags. As of 2018-11-12, freedreno/drm/msm_bo.c - * always sets `flags = MSM_BO_WC`, and we copy that behavior here. - */ - uint32_t gem_handle = tu_gem_new(dev, size, MSM_BO_WC); - if (!gem_handle) - return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY); - - VkResult result = tu_bo_init(dev, bo, gem_handle, size); - if (result != VK_SUCCESS) { - tu_gem_close(dev, gem_handle); - return vk_error(dev->instance, result); - } - - return VK_SUCCESS; -} - -VkResult -tu_bo_init_dmabuf(struct tu_device *dev, - struct tu_bo *bo, - uint64_t size, - int fd) -{ - uint32_t gem_handle = tu_gem_import_dmabuf(dev, fd, size); - if (!gem_handle) - return vk_error(dev->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE); - - VkResult result = tu_bo_init(dev, bo, gem_handle, size); - if (result != VK_SUCCESS) { - tu_gem_close(dev, gem_handle); - return vk_error(dev->instance, result); - } - - return VK_SUCCESS; -} - -int -tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo) -{ - return tu_gem_export_dmabuf(dev, bo->gem_handle); -} - -VkResult -tu_bo_map(struct tu_device *dev, struct tu_bo *bo) -{ - if (bo->map) - return VK_SUCCESS; - - uint64_t offset = tu_gem_info_offset(dev, bo->gem_handle); - if (!offset) - return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY); - - /* TODO: Should we use the wrapper os_mmap() like Freedreno does? */ - void *map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, - dev->physical_device->local_fd, offset); - if (map == MAP_FAILED) - return vk_error(dev->instance, VK_ERROR_MEMORY_MAP_FAILED); - - bo->map = map; - return VK_SUCCESS; -} - -void -tu_bo_finish(struct tu_device *dev, struct tu_bo *bo) -{ - assert(bo->gem_handle); - - if (bo->map) - munmap(bo->map, bo->size); - - tu_gem_close(dev, bo->gem_handle); -} - VkResult tu_physical_device_init(struct tu_physical_device *device, struct tu_instance *instance) diff --git a/src/freedreno/vulkan/tu_drm.c b/src/freedreno/vulkan/tu_drm.c index c8f10566f0e..99d81f33c1c 100644 --- a/src/freedreno/vulkan/tu_drm.c +++ b/src/freedreno/vulkan/tu_drm.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "drm-uapi/msm_drm.h" @@ -114,7 +115,7 @@ tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id) /** * Return gem handle on success. Return 0 on failure. */ -uint32_t +static uint32_t tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags) { struct drm_msm_gem_new req = { @@ -130,7 +131,7 @@ tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags) return req.handle; } -uint32_t +static uint32_t tu_gem_import_dmabuf(const struct tu_device *dev, int prime_fd, uint64_t size) { /* lseek() to get the real size */ @@ -148,7 +149,7 @@ tu_gem_import_dmabuf(const struct tu_device *dev, int prime_fd, uint64_t size) return gem_handle; } -int +static int tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle) { int prime_fd; @@ -158,7 +159,7 @@ tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle) return ret == 0 ? prime_fd : -1; } -void +static void tu_gem_close(const struct tu_device *dev, uint32_t gem_handle) { struct drm_gem_close req = { @@ -189,7 +190,7 @@ tu_gem_info(const struct tu_device *dev, uint32_t gem_handle, uint32_t info) * * Returns 0 on error (an invalid mmap offset in the DRM UBI). */ -uint64_t +static uint64_t tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle) { return tu_gem_info(dev, gem_handle, MSM_INFO_GET_OFFSET); @@ -199,12 +200,106 @@ tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle) * * Returns 0 on error (an invalid iova in the MSM DRM UABI). */ -uint64_t +static uint64_t tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle) { return tu_gem_info(dev, gem_handle, MSM_INFO_GET_IOVA); } +static VkResult +tu_bo_init(struct tu_device *dev, + struct tu_bo *bo, + uint32_t gem_handle, + uint64_t size) +{ + uint64_t iova = tu_gem_info_iova(dev, gem_handle); + if (!iova) + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + + *bo = (struct tu_bo) { + .gem_handle = gem_handle, + .size = size, + .iova = iova, + }; + + return VK_SUCCESS; +} + +VkResult +tu_bo_init_new(struct tu_device *dev, struct tu_bo *bo, uint64_t size) +{ + /* TODO: Choose better flags. As of 2018-11-12, freedreno/drm/msm_bo.c + * always sets `flags = MSM_BO_WC`, and we copy that behavior here. + */ + uint32_t gem_handle = tu_gem_new(dev, size, MSM_BO_WC); + if (!gem_handle) + return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY); + + VkResult result = tu_bo_init(dev, bo, gem_handle, size); + if (result != VK_SUCCESS) { + tu_gem_close(dev, gem_handle); + return vk_error(dev->instance, result); + } + + return VK_SUCCESS; +} + +VkResult +tu_bo_init_dmabuf(struct tu_device *dev, + struct tu_bo *bo, + uint64_t size, + int fd) +{ + uint32_t gem_handle = tu_gem_import_dmabuf(dev, fd, size); + if (!gem_handle) + return vk_error(dev->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE); + + VkResult result = tu_bo_init(dev, bo, gem_handle, size); + if (result != VK_SUCCESS) { + tu_gem_close(dev, gem_handle); + return vk_error(dev->instance, result); + } + + return VK_SUCCESS; +} + +int +tu_bo_export_dmabuf(struct tu_device *dev, struct tu_bo *bo) +{ + return tu_gem_export_dmabuf(dev, bo->gem_handle); +} + +VkResult +tu_bo_map(struct tu_device *dev, struct tu_bo *bo) +{ + if (bo->map) + return VK_SUCCESS; + + uint64_t offset = tu_gem_info_offset(dev, bo->gem_handle); + if (!offset) + return vk_error(dev->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY); + + /* TODO: Should we use the wrapper os_mmap() like Freedreno does? */ + void *map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, + dev->physical_device->local_fd, offset); + if (map == MAP_FAILED) + return vk_error(dev->instance, VK_ERROR_MEMORY_MAP_FAILED); + + bo->map = map; + return VK_SUCCESS; +} + +void +tu_bo_finish(struct tu_device *dev, struct tu_bo *bo) +{ + assert(bo->gem_handle); + + if (bo->map) + munmap(bo->map, bo->size); + + tu_gem_close(dev, bo->gem_handle); +} + static VkResult tu_drm_device_init(struct tu_physical_device *device, struct tu_instance *instance, diff --git a/src/freedreno/vulkan/tu_private.h b/src/freedreno/vulkan/tu_private.h index 93852bd0d57..40c2fb092a3 100644 --- a/src/freedreno/vulkan/tu_private.h +++ b/src/freedreno/vulkan/tu_private.h @@ -1587,21 +1587,6 @@ tu_drm_submitqueue_new(const struct tu_device *dev, void tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id); -uint32_t -tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags); -uint32_t -tu_gem_import_dmabuf(const struct tu_device *dev, - int prime_fd, - uint64_t size); -int -tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle); -void -tu_gem_close(const struct tu_device *dev, uint32_t gem_handle); -uint64_t -tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle); -uint64_t -tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle); - #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \ \ static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \ -- 2.30.2