v3dv: implement vkAllocateMemory
authorIago Toral Quiroga <itoral@igalia.com>
Wed, 4 Dec 2019 09:39:01 +0000 (10:39 +0100)
committerMarge Bot <eric+marge@anholt.net>
Tue, 13 Oct 2020 21:21:25 +0000 (21:21 +0000)
Removed MAX_MEMORY_ALLOCATION_SIZE definition because that comes from
VK_KHR_maintenance3 which we don't support yet. We can care about that
when we iplement it.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>

src/broadcom/vulkan/v3dv_device.c
src/broadcom/vulkan/v3dv_private.h

index cf834b8..60e1a53 100644 (file)
 #include <unistd.h>
 #include <xf86drm.h>
 
-#include "common/v3d_debug.h"
-
 #include "v3dv_private.h"
-#include "vk_util.h"
 
+#include "common/v3d_debug.h"
 #include "compiler/glsl_types.h"
+#include "drm-uapi/v3d_drm.h"
+#include "vk_util.h"
 
 static void *
 default_alloc_func(void *pUserData, size_t size, size_t align,
@@ -1010,6 +1010,38 @@ v3dv_DestroyDebugReportCallbackEXT(VkInstance _instance,
                                     _callback, pAllocator, &instance->alloc);
 }
 
+static VkResult
+device_alloc(struct v3dv_device *device,
+             struct v3dv_device_memory *mem,
+             VkDeviceSize size)
+{
+   /* FIXME: implement a BO cache like in v3d */
+
+   /* Our kernel interface is 32-bit */
+   assert((size & 0xffffffff) == size);
+
+   const uint32_t page_align = 4096; /* Always allocate full pages */
+   size = align(size, page_align);
+   struct drm_v3d_create_bo create = {
+      .size = size
+   };
+
+   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_V3D_CREATE_BO, &create);
+   if (ret != 0)
+      return VK_ERROR_OUT_OF_DEVICE_MEMORY;
+
+   assert(create.offset % page_align == 0);
+   assert((create.offset & 0xffffffff) == create.offset);
+
+   mem->handle = create.handle;
+   mem->size = size;
+   mem->offset = create.offset;
+   mem->map = NULL;
+   mem->map_size = 0;
+
+   return VK_SUCCESS;
+}
+
 VkResult
 v3dv_AllocateMemory(VkDevice _device,
                     const VkMemoryAllocateInfo *pAllocateInfo,
@@ -1018,29 +1050,25 @@ v3dv_AllocateMemory(VkDevice _device,
 {
    V3DV_FROM_HANDLE(v3dv_device, device, _device);
    struct v3dv_device_memory *mem;
-   /* struct v3dv_physical_device *pdevice = &device->instance->physicalDevice; */
+   struct v3dv_physical_device *pdevice = &device->instance->physicalDevice;
 
    assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
 
    /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
    assert(pAllocateInfo->allocationSize > 0);
 
-   if (pAllocateInfo->allocationSize > MAX_MEMORY_ALLOCATION_SIZE)
-      return VK_ERROR_OUT_OF_DEVICE_MEMORY;
-
    mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
                    VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
    if (mem == NULL)
       return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
 
-   /* FIXME: assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count); */
-   /* FIXME: mem->type = &pdevice->memory.types[pAllocateInfo->memoryTypeIndex]; */
-   mem->map = NULL;
-   mem->map_size = 0;
+   assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.memoryTypeCount);
+   mem->type = &pdevice->memory.memoryTypes[pAllocateInfo->memoryTypeIndex];
 
-   /* FIXME: stub */
+   VkResult result = device_alloc(device, mem, pAllocateInfo->allocationSize);
 
-   return VK_SUCCESS;
+   *pMem = v3dv_device_memory_to_handle(mem);
+   return result;
 }
 
 void
index 0dd3fbc..57a02fe 100644 (file)
@@ -82,14 +82,6 @@ pack_emit_reloc(void *cl, const void *reloc) {}
 #define __gen_emit_reloc pack_emit_reloc
 #define __gen_address_offset(reloc) (0)
 
-/*
- * FIXME: confirm value
- *
- * FIXME: seems like a good idea having something like this, as anv, but both
- * tu/radv doesn't check for this issue. Need to revisit.
- */
-#define MAX_MEMORY_ALLOCATION_SIZE (1ull << 31)
-
 struct v3dv_instance;
 
 #ifdef USE_V3D_SIMULATOR
@@ -182,11 +174,15 @@ struct v3dv_cmd_buffer {
 };
 
 struct v3dv_device_memory {
-   /* FIXME: stub */
-   /* FIXME: likely would include links to structures similar to v3d_bo
-    * (perhaps we should refactor existing v3d_bo?) */
+   /* FIXME: Can we refactor and resuse v3d_bo here? */
+   uint32_t handle;
+   VkDeviceSize size;
+   VkDeviceSize offset;
+
    VkDeviceSize map_size;
    void *map;
+
+   const VkMemoryType *type;
 };
 
 #define V3D_OUTPUT_IMAGE_FORMAT_NO 255