Module: Mesa
Branch: master
Commit: 4342dec09a9914f68271e21a9006963ec8a406b9
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=4342dec09a9914f68271e21a9006963ec8a406b9

Author: Samuel Iglesias Gonsálvez <[email protected]>
Date:   Wed Jan 13 16:46:52 2021 +0100

turnip: keep track of memory heap usage, size and flags

Signed-off-by: Samuel Iglesias Gonsálvez <[email protected]>
Reviewed-by: Eric Anholt <[email protected]>
Reviewed-by: Hyunjun Ko <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8524>

---

 src/freedreno/vulkan/tu_device.c  | 26 ++++++++++++++++++++++----
 src/freedreno/vulkan/tu_drm.c     |  4 ++++
 src/freedreno/vulkan/tu_kgsl.c    |  4 ++++
 src/freedreno/vulkan/tu_private.h | 19 +++++++++++++++++++
 4 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/src/freedreno/vulkan/tu_device.c b/src/freedreno/vulkan/tu_device.c
index ee55f811a5a..e41b8bada9a 100644
--- a/src/freedreno/vulkan/tu_device.c
+++ b/src/freedreno/vulkan/tu_device.c
@@ -889,7 +889,7 @@ tu_GetPhysicalDeviceQueueFamilyProperties2(
    }
 }
 
-static uint64_t
+uint64_t
 tu_get_system_heap_size()
 {
    struct sysinfo info;
@@ -913,11 +913,12 @@ void
 tu_GetPhysicalDeviceMemoryProperties2(VkPhysicalDevice pdev,
                                       VkPhysicalDeviceMemoryProperties2 
*props2)
 {
-   VkPhysicalDeviceMemoryProperties *props = &props2->memoryProperties;
+   TU_FROM_HANDLE(tu_physical_device, physical_device, pdev);
 
+   VkPhysicalDeviceMemoryProperties *props = &props2->memoryProperties;
    props->memoryHeapCount = 1;
-   props->memoryHeaps[0].size = tu_get_system_heap_size();
-   props->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
+   props->memoryHeaps[0].size = physical_device->heap.size;
+   props->memoryHeaps[0].flags = physical_device->heap.flags;
 
    props->memoryTypeCount = 1;
    props->memoryTypes[0].propertyFlags =
@@ -1428,6 +1429,11 @@ tu_AllocateMemory(VkDevice _device,
       return VK_SUCCESS;
    }
 
+   struct tu_memory_heap *mem_heap = &device->physical_device->heap;
+   uint64_t mem_heap_used = p_atomic_read(&mem_heap->used);
+   if (mem_heap_used > mem_heap->size)
+      return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
+
    mem = vk_object_alloc(&device->vk, pAllocator, sizeof(*mem),
                          VK_OBJECT_TYPE_DEVICE_MEMORY);
    if (mem == NULL)
@@ -1460,6 +1466,17 @@ tu_AllocateMemory(VkDevice _device,
          tu_bo_init_new(device, &mem->bo, pAllocateInfo->allocationSize, 
false);
    }
 
+
+   if (result == VK_SUCCESS) {
+      mem_heap_used = p_atomic_add_return(&mem_heap->used, mem->bo.size);
+      if (mem_heap_used > mem_heap->size) {
+         p_atomic_add(&mem_heap->used, -mem->bo.size);
+         tu_bo_finish(device, &mem->bo);
+         result = vk_errorf(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY,
+                            "Out of heap memory");
+      }
+   }
+
    if (result != VK_SUCCESS) {
       vk_object_free(&device->vk, pAllocator, mem);
       return result;
@@ -1481,6 +1498,7 @@ tu_FreeMemory(VkDevice _device,
    if (mem == NULL)
       return;
 
+   p_atomic_add(&device->physical_device->heap.used, -mem->bo.size);
    tu_bo_finish(device, &mem->bo);
    vk_object_free(&device->vk, pAllocator, mem);
 }
diff --git a/src/freedreno/vulkan/tu_drm.c b/src/freedreno/vulkan/tu_drm.c
index 28f2c9531e8..dd0d4e9cf52 100644
--- a/src/freedreno/vulkan/tu_drm.c
+++ b/src/freedreno/vulkan/tu_drm.c
@@ -393,6 +393,10 @@ tu_drm_device_init(struct tu_physical_device *device,
       goto fail;
    }
 
+   device->heap.size = tu_get_system_heap_size();
+   device->heap.used = 0u;
+   device->heap.flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
+
    return tu_physical_device_init(device, instance);
 
 fail:
diff --git a/src/freedreno/vulkan/tu_kgsl.c b/src/freedreno/vulkan/tu_kgsl.c
index acd26a5472b..2d3e903e409 100644
--- a/src/freedreno/vulkan/tu_kgsl.c
+++ b/src/freedreno/vulkan/tu_kgsl.c
@@ -242,6 +242,10 @@ tu_enumerate_devices(struct tu_instance *instance)
    device->gmem_size = info.gmem_sizebytes;
    device->gmem_base = gmem_iova;
 
+   device->heap.size = tu_get_system_heap_size();
+   device->heap.used = 0u;
+   device->heap.flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
+
    if (tu_physical_device_init(device, instance) != VK_SUCCESS)
       goto fail;
 
diff --git a/src/freedreno/vulkan/tu_private.h 
b/src/freedreno/vulkan/tu_private.h
index 1b71a8d4074..86014457ded 100644
--- a/src/freedreno/vulkan/tu_private.h
+++ b/src/freedreno/vulkan/tu_private.h
@@ -174,6 +174,23 @@ __tu_finishme(const char *file, int line, const char 
*format, ...)
       tu_finishme("stub %s", __func__);                                      \
    } while (0)
 
+struct tu_memory_heap {
+   /* Standard bits passed on to the client */
+   VkDeviceSize      size;
+   VkMemoryHeapFlags flags;
+
+   /** Copied from ANV:
+    *
+    * Driver-internal book-keeping.
+    *
+    * Align it to 64 bits to make atomic operations faster on 32 bit platforms.
+    */
+   VkDeviceSize      used __attribute__ ((aligned (8)));
+};
+
+uint64_t
+tu_get_system_heap_size(void);
+
 struct tu_physical_device
 {
    struct vk_physical_device vk;
@@ -203,6 +220,8 @@ struct tu_physical_device
     * the pipeline cache defined by apps.
     */
    struct disk_cache *disk_cache;
+
+   struct tu_memory_heap heap;
 };
 
 enum tu_debug_flags

_______________________________________________
mesa-commit mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/mesa-commit

Reply via email to