#include <libsync.h>
#include <stdbool.h>
#include <string.h>
-#include <sys/mman.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <xf86drm.h>
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)
#include <fcntl.h>
#include <stdint.h>
#include <sys/ioctl.h>
+#include <sys/mman.h>
#include <xf86drm.h>
#include "drm-uapi/msm_drm.h"
/**
* 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 = {
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 */
return gem_handle;
}
-int
+static int
tu_gem_export_dmabuf(const struct tu_device *dev, uint32_t gem_handle)
{
int prime_fd;
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 = {
*
* 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);
*
* 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,