anv: Use the BO cache for DeviceMemory allocations

Change-Id: I2aef65783172e9f2a985f5cf8c915354f3c46f88
Reviewed-by: Chad Versace <chadversary@chromium.org>
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index c2db014..4b6ee1e 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -1241,10 +1241,14 @@
 
    anv_bo_pool_init(&device->batch_bo_pool, device);
 
+   result = anv_bo_cache_init(&device->bo_cache);
+   if (result != VK_SUCCESS)
+      goto fail_batch_bo_pool;
+
    result = anv_block_pool_init(&device->dynamic_state_block_pool, device,
                                 16384);
    if (result != VK_SUCCESS)
-      goto fail_batch_bo_pool;
+      goto fail_bo_cache;
 
    anv_state_pool_init(&device->dynamic_state_pool,
                        &device->dynamic_state_block_pool);
@@ -1316,6 +1320,8 @@
  fail_dynamic_state_pool:
    anv_state_pool_finish(&device->dynamic_state_pool);
    anv_block_pool_finish(&device->dynamic_state_block_pool);
+ fail_bo_cache:
+   anv_bo_cache_finish(&device->bo_cache);
  fail_batch_bo_pool:
    anv_bo_pool_finish(&device->batch_bo_pool);
    pthread_cond_destroy(&device->queue_submit);
@@ -1363,6 +1369,8 @@
    anv_state_pool_finish(&device->dynamic_state_pool);
    anv_block_pool_finish(&device->dynamic_state_block_pool);
 
+   anv_bo_cache_finish(&device->bo_cache);
+
    anv_bo_pool_finish(&device->batch_bo_pool);
 
    pthread_cond_destroy(&device->queue_submit);
@@ -1780,7 +1788,8 @@
    /* The kernel is going to give us whole pages anyway */
    uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
 
-   result = anv_bo_init_new(&mem->bo, device, alloc_size);
+   result = anv_bo_cache_alloc(device, &device->bo_cache,
+                               alloc_size, &mem->bo);
    if (result != VK_SUCCESS)
       goto fail;
 
@@ -1792,10 +1801,10 @@
 
    assert(mem->type->heapIndex < pdevice->memory.heap_count);
    if (pdevice->memory.heaps[mem->type->heapIndex].supports_48bit_addresses)
-      mem->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+      mem->bo->flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
 
    if (pdevice->has_exec_async)
-      mem->bo.flags |= EXEC_OBJECT_ASYNC;
+      mem->bo->flags |= EXEC_OBJECT_ASYNC;
 
    *pMem = anv_device_memory_to_handle(mem);
 
@@ -1821,11 +1830,7 @@
    if (mem->map)
       anv_UnmapMemory(_device, _mem);
 
-   if (mem->bo.map)
-      anv_gem_munmap(device, mem->bo.gem_handle, mem->bo.map, mem->bo.size);
-
-   if (mem->bo.gem_handle != 0)
-      anv_gem_close(device, mem->bo.gem_handle);
+   anv_bo_cache_release(device, &device->bo_cache, mem->bo);
 
    vk_free2(&device->alloc, pAllocator, mem);
 }
@@ -1847,7 +1852,7 @@
    }
 
    if (size == VK_WHOLE_SIZE)
-      size = mem->bo.size - offset;
+      size = mem->bo->size - offset;
 
    /* From the Vulkan spec version 1.0.32 docs for MapMemory:
     *
@@ -1857,7 +1862,7 @@
     *    equal to the size of the memory minus offset
     */
    assert(size > 0);
-   assert(offset + size <= mem->bo.size);
+   assert(offset + size <= mem->bo->size);
 
    /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
     * takes a VkDeviceMemory pointer, it seems like only one map of the memory
@@ -1879,7 +1884,7 @@
    /* Let's map whole pages */
    map_size = align_u64(map_size, 4096);
 
-   void *map = anv_gem_mmap(device, mem->bo.gem_handle,
+   void *map = anv_gem_mmap(device, mem->bo->gem_handle,
                             map_offset, map_size, gem_flags);
    if (map == MAP_FAILED)
       return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
@@ -1902,7 +1907,7 @@
    if (mem == NULL)
       return;
 
-   anv_gem_munmap(device, mem->bo.gem_handle, mem->map, mem->map_size);
+   anv_gem_munmap(device, mem->bo->gem_handle, mem->map, mem->map_size);
 
    mem->map = NULL;
    mem->map_size = 0;
@@ -2039,8 +2044,7 @@
    ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
 
    if (mem) {
-      assert((buffer->usage & mem->type->valid_buffer_usage) == buffer->usage);
-      buffer->bo = &mem->bo;
+      buffer->bo = mem->bo;
       buffer->offset = memoryOffset;
    } else {
       buffer->bo = NULL;
diff --git a/src/intel/vulkan/anv_image.c b/src/intel/vulkan/anv_image.c
index 6daf356..168b003 100644
--- a/src/intel/vulkan/anv_image.c
+++ b/src/intel/vulkan/anv_image.c
@@ -353,7 +353,7 @@
       return VK_SUCCESS;
    }
 
-   image->bo = &mem->bo;
+   image->bo = mem->bo;
    image->offset = memoryOffset;
 
    return VK_SUCCESS;
diff --git a/src/intel/vulkan/anv_intel.c b/src/intel/vulkan/anv_intel.c
index eda474e..991a935 100644
--- a/src/intel/vulkan/anv_intel.c
+++ b/src/intel/vulkan/anv_intel.c
@@ -49,18 +49,15 @@
    if (mem == NULL)
       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
 
-   uint32_t gem_handle = anv_gem_fd_to_handle(device, pCreateInfo->fd);
-   if (!gem_handle) {
-      result = vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
-      goto fail;
-   }
-
    uint64_t size = (uint64_t)pCreateInfo->strideInBytes * pCreateInfo->extent.height;
 
-   anv_bo_init(&mem->bo, gem_handle, size);
+   result = anv_bo_cache_import(device, &device->bo_cache,
+                                pCreateInfo->fd, size, &mem->bo);
+   if (result != VK_SUCCESS)
+      goto fail;
 
    if (device->instance->physicalDevice.supports_48bit_addresses)
-      mem->bo.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+      mem->bo->flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
 
    anv_image_create(_device,
       &(struct anv_image_create_info) {
@@ -83,7 +80,7 @@
       pAllocator, &image_h);
 
    image = anv_image_from_handle(image_h);
-   image->bo = &mem->bo;
+   image->bo = mem->bo;
    image->offset = 0;
 
    assert(image->extent.width > 0);
diff --git a/src/intel/vulkan/anv_magma.cc b/src/intel/vulkan/anv_magma.cc
index 972a4ce..65b1e2a 100644
--- a/src/intel/vulkan/anv_magma.cc
+++ b/src/intel/vulkan/anv_magma.cc
@@ -296,7 +296,7 @@
    ANV_FROM_HANDLE(anv_device, device, _device);
    ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
 
-   auto result = magma_export(magma_connection(device), mem->bo.gem_handle, pHandle);
+   auto result = magma_export(magma_connection(device), mem->bo->gem_handle, pHandle);
    DASSERT(result == MAGMA_STATUS_OK);
 
    return VK_SUCCESS;
@@ -328,7 +328,7 @@
 
    DASSERT(result == MAGMA_STATUS_OK);
 
-   anv_bo_init(&mem->bo, magma_buffer, magma_get_buffer_size(magma_buffer));
+   anv_bo_init(mem->bo, magma_buffer, magma_get_buffer_size(magma_buffer));
 
    struct anv_physical_device *pdevice = &device->instance->physicalDevice;
    mem->type = &pdevice->memory.types[0];
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 2604e65..665690b 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -757,6 +757,8 @@
 
     struct anv_bo_pool                          batch_bo_pool;
 
+    struct anv_bo_cache                         bo_cache;
+
     struct anv_block_pool                       dynamic_state_block_pool;
     struct anv_state_pool                       dynamic_state_pool;
 
@@ -1053,7 +1055,7 @@
    }
 
 struct anv_device_memory {
-   struct anv_bo                                bo;
+   struct anv_bo *                              bo;
    struct anv_memory_type *                     type;
    VkDeviceSize                                 map_size;
    void *                                       map;
diff --git a/src/intel/vulkan/anv_wsi.c b/src/intel/vulkan/anv_wsi.c
index fac8db7..d04c5c0 100644
--- a/src/intel/vulkan/anv_wsi.c
+++ b/src/intel/vulkan/anv_wsi.c
@@ -231,8 +231,8 @@
     * know we're writing to them and synchronize uses on other rings (eg if
     * the display server uses the blitter ring).
     */
-   memory->bo.flags &= ~EXEC_OBJECT_ASYNC;
-   memory->bo.flags |= EXEC_OBJECT_WRITE;
+   memory->bo->flags &= ~EXEC_OBJECT_ASYNC;
+   memory->bo->flags |= EXEC_OBJECT_WRITE;
 
    anv_BindImageMemory(device_h, image_h, memory_h, 0);
 
@@ -240,7 +240,7 @@
    assert(surface->isl.tiling == ISL_TILING_X);
 
    *row_pitch = surface->isl.row_pitch;
-   int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
+   int ret = anv_gem_set_tiling(device, memory->bo->gem_handle,
                                 surface->isl.row_pitch, I915_TILING_X);
    if (ret) {
       /* FINISHME: Choose a better error. */
@@ -249,7 +249,7 @@
       goto fail_alloc_memory;
    }
 
-   int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
+   int fd = anv_gem_handle_to_fd(device, memory->bo->gem_handle);
    if (fd == -1) {
       /* FINISHME: Choose a better error. */
       result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,