v3dv: create a v3dv_bo struct and reference it from v3dv_device_memory

So we have a lower level representation of a buffer object that we can
manipulate that is not tied to a Vulkan representation of memory. This
will be useful as we start allocating driver internal buffers, such as
command lists.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>
diff --git a/src/broadcom/vulkan/meson.build b/src/broadcom/vulkan/meson.build
index 89d032a..d8bf8fb 100644
--- a/src/broadcom/vulkan/meson.build
+++ b/src/broadcom/vulkan/meson.build
@@ -52,6 +52,7 @@
 )
 
 libv3dv_files = files(
+  'v3dv_bo.c',
   'v3dv_cmd_buffer.c',
   'v3dv_debug.c',
   'v3dv_debug.h',
diff --git a/src/broadcom/vulkan/v3dv_bo.c b/src/broadcom/vulkan/v3dv_bo.c
new file mode 100644
index 0000000..95e5695
--- /dev/null
+++ b/src/broadcom/vulkan/v3dv_bo.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright © 2019 Raspberry Pi
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "v3dv_private.h"
+
+#include <errno.h>
+#include <sys/mman.h>
+
+#include "drm-uapi/v3d_drm.h"
+
+bool
+v3dv_bo_alloc(struct v3dv_device *device, uint32_t size, struct v3dv_bo *bo)
+{
+   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 false;
+
+   assert(create.offset % page_align == 0);
+   assert((create.offset & 0xffffffff) == create.offset);
+
+   bo->handle = create.handle;
+   bo->size = size;
+   bo->offset = create.offset;
+   bo->map = NULL;
+   bo->map_size = 0;
+
+   return true;
+}
+
+bool
+v3dv_bo_free(struct v3dv_device *device, struct v3dv_bo *bo)
+{
+   assert(bo);
+
+   if (bo->map)
+      v3dv_bo_unmap(device, bo);
+
+   struct drm_gem_close c;
+   memset(&c, 0, sizeof(c));
+   c.handle = bo->handle;
+   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &c);
+   if (ret != 0)
+      fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
+
+   return ret == 0;
+}
+
+bool
+v3dv_bo_map_unsynchronized(struct v3dv_device *device,
+                           struct v3dv_bo *bo,
+                           uint32_t size)
+{
+   assert(bo != NULL && size <= bo->size);
+
+   struct drm_v3d_mmap_bo map;
+   memset(&map, 0, sizeof(map));
+   map.handle = bo->handle;
+   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_V3D_MMAP_BO, &map);
+   if (ret != 0) {
+      fprintf(stderr, "map ioctl failure\n");
+      return false;
+   }
+
+   bo->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
+                  device->fd, map.offset);
+   if (bo->map == MAP_FAILED) {
+      fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
+              bo->handle, (long long)map.offset, (uint32_t)bo->size);
+      return false;
+   }
+   VG(VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, false));
+
+   bo->map_size = size;
+
+   return true;
+}
+
+bool
+v3dv_bo_wait(struct v3dv_device *device,
+             struct v3dv_bo *bo,
+             uint64_t timeout_ns)
+{
+   struct drm_v3d_wait_bo wait = {
+      .handle = bo->handle,
+      .timeout_ns = timeout_ns,
+   };
+   return v3dv_ioctl(device->fd, DRM_IOCTL_V3D_WAIT_BO, &wait) == 0;
+}
+
+bool
+v3dv_bo_map(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size)
+{
+   assert(bo && size <= bo->size);
+
+   bool ok = v3dv_bo_map_unsynchronized(device, bo, size);
+   if (!ok)
+      return false;
+
+   const uint64_t infinite = 0xffffffffffffffffull;
+   ok = v3dv_bo_wait(device, bo, infinite);
+   if (!ok) {
+      fprintf(stderr, "memory wait for map failed\n");
+      return false;
+   }
+
+   return true;
+}
+
+void
+v3dv_bo_unmap(struct v3dv_device *device, struct v3dv_bo *bo)
+{
+   assert(bo && bo->map && bo->map_size > 0);
+
+   munmap(bo->map, bo->map_size);
+   VG(VALGRIND_FREELIKE_BLOCK(bo->map, 0));
+   bo->map = NULL;
+   bo->map_size = 0;
+}
+
diff --git a/src/broadcom/vulkan/v3dv_bo.h b/src/broadcom/vulkan/v3dv_bo.h
new file mode 100644
index 0000000..d6c9cad
--- /dev/null
+++ b/src/broadcom/vulkan/v3dv_bo.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2019 Raspberry Pi
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef V3DV_BO_H
+#define V3DV_BO_H
+
+struct v3dv_device;
+
+struct v3dv_bo {
+   uint32_t handle;
+   uint32_t size;
+   uint32_t offset;
+
+   uint32_t map_size;
+   void *map;
+};
+
+bool v3dv_bo_alloc(struct v3dv_device *device, uint32_t size, struct v3dv_bo *bo);
+
+bool v3dv_bo_free(struct v3dv_device *device, struct v3dv_bo *bo);
+
+bool v3dv_bo_wait(struct v3dv_device *device, struct v3dv_bo *bo, uint64_t timeout_ns);
+
+bool v3dv_bo_map_unsynchronized(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size);
+
+bool v3dv_bo_map(struct v3dv_device *device, struct v3dv_bo *bo, uint32_t size);
+
+void v3dv_bo_unmap(struct v3dv_device *device, struct v3dv_bo *bo);
+
+#endif /* V3DV_BO_H */
diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c
index a7b3a24..96fe7bc 100644
--- a/src/broadcom/vulkan/v3dv_device.c
+++ b/src/broadcom/vulkan/v3dv_device.c
@@ -1015,96 +1015,18 @@
              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)
+   bool ok = v3dv_bo_alloc(device, size, &mem->bo);
+   if (!ok)
       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;
 }
 
 static void
 device_free(struct v3dv_device *device, struct v3dv_device_memory *mem)
 {
-   struct drm_gem_close c;
-   memset(&c, 0, sizeof(c));
-   c.handle = mem->handle;
-   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &c);
-   if (ret != 0)
-      fprintf(stderr, "close object %d: %s\n", mem->handle, strerror(errno));
-}
-
-static VkResult
-device_map_unsynchronized(struct v3dv_device *device,
-                          struct v3dv_device_memory *mem,
-                          uint32_t size)
-{
-   assert(mem != NULL && size < mem->size);
-
-   /* From the spec:
-    *
-    *   "After a successful call to vkMapMemory the memory object memory is
-    *   considered to be currently host mapped. It is an application error to
-    *   call vkMapMemory on a memory object that is already host mapped."
-    */
-   assert(mem->map = NULL);
-
-   struct drm_v3d_mmap_bo map;
-   memset(&map, 0, sizeof(map));
-   map.handle = mem->handle;
-   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_V3D_MMAP_BO, &map);
-   if (ret != 0) {
-      fprintf(stderr, "map ioctl failure\n");
-      return VK_ERROR_MEMORY_MAP_FAILED;
-   }
-
-   mem->map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
-                   device->fd, map.offset);
-   if (mem->map == MAP_FAILED) {
-      fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
-              mem->handle, (long long)map.offset, (uint32_t)mem->size);
-      return VK_ERROR_MEMORY_MAP_FAILED;
-   }
-   VG(VALGRIND_MALLOCLIKE_BLOCK(mem->map, mem->size, 0, false));
-
-   mem->map_size = size;
-
-   return VK_SUCCESS;
-}
-
-static bool
-device_memory_wait(struct v3dv_device *device,
-                   struct v3dv_device_memory *mem,
-                   uint64_t timeout_ns)
-{
-   struct drm_v3d_wait_bo wait = {
-      .handle = mem->handle,
-      .timeout_ns = timeout_ns,
-   };
-   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_V3D_WAIT_BO, &wait);
-   if (ret == -1)
-      return -errno;
-   else
-      return 0;
+   v3dv_bo_free(device, &mem->bo);
 }
 
 static VkResult
@@ -1112,16 +1034,17 @@
            struct v3dv_device_memory *mem,
            uint32_t size)
 {
-   VkResult result = device_map_unsynchronized(device, mem, size);
-   if (result != VK_SUCCESS)
-      return result;
+   /* From the spec:
+    *
+    *   "After a successful call to vkMapMemory the memory object memory is
+    *   considered to be currently host mapped. It is an application error to
+    *   call vkMapMemory on a memory object that is already host mapped."
+    */
+   assert(mem && mem->bo.map == NULL);
 
-   const uint64_t infinite = 0xffffffffffffffffull;
-   bool ok = device_memory_wait(device, mem, infinite);
-   if (!ok) {
-      fprintf(stderr, "memory wait for map failed\n");
+   bool ok = v3dv_bo_map(device, &mem->bo, size);
+   if (!ok)
       return VK_ERROR_MEMORY_MAP_FAILED;
-   }
 
    return VK_SUCCESS;
 }
@@ -1129,19 +1052,8 @@
 static void
 device_unmap(struct v3dv_device *device, struct v3dv_device_memory *mem)
 {
-   assert(mem->map && mem->map_size > 0);
-
-   munmap(mem->map, mem->map_size);
-   VG(VALGRIND_FREELIKE_BLOCK(mem->map, 0));
-   mem->map = NULL;
-   mem->map_size = 0;
-
-   struct drm_gem_close c;
-   memset(&c, 0, sizeof(c));
-   c.handle = mem->handle;
-   int ret = v3dv_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &c);
-   if (ret != 0)
-      fprintf(stderr, "close object %d: %s\n", mem->handle, strerror(errno));
+   assert(mem && mem->bo.map && mem->bo.map_size > 0);
+   v3dv_bo_unmap(device, &mem->bo);
 }
 
 VkResult
@@ -1184,7 +1096,7 @@
    if (mem == NULL)
       return;
 
-   if (mem->map)
+   if (mem->bo.map)
       v3dv_UnmapMemory(_device, _mem);
 
    device_free(device, mem);
@@ -1208,14 +1120,14 @@
       return VK_SUCCESS;
    }
 
-   assert(offset < mem->size);
+   assert(offset < mem->bo.size);
 
    /* We always map from the beginning of the region, so if our offset
     * is not 0 and we are not mapping the entire region, we need to
     * add the offset to the map size.
     */
    if (size == VK_WHOLE_SIZE)
-      size = mem->size;
+      size = mem->bo.size;
    else if (offset > 0)
       size += offset;
 
@@ -1223,7 +1135,7 @@
    if (result != VK_SUCCESS)
       return vk_error(device->instance, result);
 
-   *ppData = ((uint8_t *) mem->map) + offset;
+   *ppData = ((uint8_t *) mem->bo.map) + offset;
    return VK_SUCCESS;
 }
 
@@ -1292,7 +1204,7 @@
     *    vkGetImageMemoryRequirements with image"
     */
    assert(memoryOffset % image->alignment == 0);
-   assert(memoryOffset < mem->size);
+   assert(memoryOffset < mem->bo.size);
 
    image->mem = mem;
    image->mem_offset = memoryOffset;
@@ -1329,7 +1241,7 @@
     *    vkGetBufferMemoryRequirements with buffer"
     */
    assert(memoryOffset % buffer->alignment == 0);
-   assert(memoryOffset < mem->size);
+   assert(memoryOffset < mem->bo.size);
 
    buffer->mem = mem;
    buffer->mem_offset = memoryOffset;
diff --git a/src/broadcom/vulkan/v3dv_private.h b/src/broadcom/vulkan/v3dv_private.h
index b5b517b..fc7d1b0 100644
--- a/src/broadcom/vulkan/v3dv_private.h
+++ b/src/broadcom/vulkan/v3dv_private.h
@@ -53,6 +53,7 @@
 
 #include "v3dv_entrypoints.h"
 #include "v3dv_extensions.h"
+#include "v3dv_bo.h"
 
 #include "vk_alloc.h"
 #include "simulator/v3d_simulator.h"
@@ -166,14 +167,7 @@
 };
 
 struct v3dv_device_memory {
-   /* FIXME: Can we refactor and resuse v3d_bo here? */
-   uint32_t handle;
-   VkDeviceSize size;
-   VkDeviceSize offset;
-
-   VkDeviceSize map_size;
-   void *map;
-
+   struct v3dv_bo bo;
    const VkMemoryType *type;
 };