turnip: Move tu_bo functions to tu_drm.c

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5999>
This commit is contained in:
Kristian H. Kristensen 2020-04-07 10:33:27 -07:00 committed by Marge Bot
parent 270599cc1e
commit 59e5f95bdc
3 changed files with 101 additions and 116 deletions

View file

@ -31,7 +31,6 @@
#include <libsync.h> #include <libsync.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <unistd.h> #include <unistd.h>
#include <xf86drm.h> #include <xf86drm.h>
@ -68,100 +67,6 @@ tu_device_get_cache_uuid(uint16_t family, void *uuid)
return 0; 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 VkResult
tu_physical_device_init(struct tu_physical_device *device, tu_physical_device_init(struct tu_physical_device *device,
struct tu_instance *instance) struct tu_instance *instance)

View file

@ -28,6 +28,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdint.h> #include <stdint.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h>
#include <xf86drm.h> #include <xf86drm.h>
#include "drm-uapi/msm_drm.h" #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. * 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) tu_gem_new(const struct tu_device *dev, uint64_t size, uint32_t flags)
{ {
struct drm_msm_gem_new req = { 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; return req.handle;
} }
uint32_t static uint32_t
tu_gem_import_dmabuf(const struct tu_device *dev, int prime_fd, uint64_t size) tu_gem_import_dmabuf(const struct tu_device *dev, int prime_fd, uint64_t size)
{ {
/* lseek() to get the real 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; return gem_handle;
} }
int static int
tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle) tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle)
{ {
int prime_fd; 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; return ret == 0 ? prime_fd : -1;
} }
void static void
tu_gem_close(const struct tu_device *dev, uint32_t gem_handle) tu_gem_close(const struct tu_device *dev, uint32_t gem_handle)
{ {
struct drm_gem_close req = { 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). * 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) tu_gem_info_offset(const struct tu_device *dev, uint32_t gem_handle)
{ {
return tu_gem_info(dev, gem_handle, MSM_INFO_GET_OFFSET); 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). * 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) tu_gem_info_iova(const struct tu_device *dev, uint32_t gem_handle)
{ {
return tu_gem_info(dev, gem_handle, MSM_INFO_GET_IOVA); 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 static VkResult
tu_drm_device_init(struct tu_physical_device *device, tu_drm_device_init(struct tu_physical_device *device,
struct tu_instance *instance, struct tu_instance *instance,

View file

@ -1587,21 +1587,6 @@ tu_drm_submitqueue_new(const struct tu_device *dev,
void void
tu_drm_submitqueue_close(const struct tu_device *dev, uint32_t queue_id); 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) \ #define TU_DEFINE_HANDLE_CASTS(__tu_type, __VkType) \
\ \
static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \ static inline struct __tu_type *__tu_type##_from_handle(__VkType _handle) \