Module: Mesa
Branch: main
Commit: d491742d1964e17949bfc45aeb4116deef2a11d2
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=d491742d1964e17949bfc45aeb4116deef2a11d2

Author: José Roberto de Souza <jose.so...@intel.com>
Date:   Wed Sep 13 08:16:49 2023 -0700

anv: Add support all possible cached and coherent memory types

This changes allow us to support HOST_COHERENT, HOST_CACHED and
HOST_COHERENT + HOST_CACHED memory types for platforms that has
the PAT uAPI.

Be aware that Xe KMD will not be able to support cached only memory
types, anv_xe_physical_device_init_memory_types() will reflect that
but internal usage should not allocate
VK_MEMORY_PROPERTY_HOST_CACHED_BIT only memory, hence the assert
added.

Signed-off-by: José Roberto de Souza <jose.so...@intel.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwer...@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25462>

---

 src/intel/vulkan/anv_allocator.c      |  4 ++++
 src/intel/vulkan/anv_device.c         | 13 +++++++++----
 src/intel/vulkan/anv_private.h        |  7 +++++++
 src/intel/vulkan/xe/anv_kmd_backend.c |  2 ++
 4 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c
index 87f4136e019..eb588750920 100644
--- a/src/intel/vulkan/anv_allocator.c
+++ b/src/intel/vulkan/anv_allocator.c
@@ -1463,6 +1463,10 @@ anv_device_alloc_bo(struct anv_device *device,
                     uint64_t explicit_address,
                     struct anv_bo **bo_out)
 {
+   /* bo can only be one: cached+coherent, cached(incoherent) or coherent(no 
flags) */
+   assert(!(!!(alloc_flags & ANV_BO_ALLOC_HOST_CACHED_COHERENT) &&
+            !!(alloc_flags & ANV_BO_ALLOC_HOST_CACHED)));
+
    const uint32_t bo_flags =
          device->kmd_backend->bo_alloc_flags_to_bo_flags(device, alloc_flags);
 
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index bfd07818daa..1be4c158996 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -4146,10 +4146,13 @@ VkResult anv_AllocateMemory(
    if (mem->vk.export_handle_types || mem->vk.import_handle_type)
       alloc_flags |= (ANV_BO_ALLOC_EXTERNAL | ANV_BO_ALLOC_IMPLICIT_SYNC);
 
-   if ((mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) &&
-       (mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) &&
-       (alloc_flags & (ANV_BO_ALLOC_EXTERNAL | ANV_BO_ALLOC_SCANOUT)) == 0)
-      alloc_flags |= ANV_BO_ALLOC_HOST_CACHED_COHERENT;
+   if ((alloc_flags & (ANV_BO_ALLOC_EXTERNAL | ANV_BO_ALLOC_SCANOUT)) == 0) {
+      if ((mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) &&
+          (mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT))
+         alloc_flags |= ANV_BO_ALLOC_HOST_CACHED_COHERENT;
+      else if (mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
+         alloc_flags |= ANV_BO_ALLOC_HOST_CACHED;
+   }
 
    if (mem->vk.ahardware_buffer) {
       result = anv_import_ahw_memory(_device, mem);
@@ -5188,6 +5191,8 @@ anv_device_get_pat_entry(struct anv_device *device,
       return &device->info->pat.cached_coherent;
    else if (alloc_flags & (ANV_BO_ALLOC_EXTERNAL | ANV_BO_ALLOC_SCANOUT))
       return &device->info->pat.scanout;
+   else if (alloc_flags & ANV_BO_ALLOC_HOST_CACHED)
+      return &device->info->pat.writeback_incoherent;
    else
       return &device->info->pat.writecombining;
 }
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index d1cabc49088..b54400c7014 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -425,6 +425,13 @@ enum anv_bo_alloc_flags {
 
    /** Protected buffer */
    ANV_BO_ALLOC_PROTECTED =               (1 << 15),
+
+   /** Specifies that the BO should be cached and incoherent.
+    *
+    * If ANV_BO_ALLOC_HOST_CACHED or ANV_BO_ALLOC_HOST_CACHED_COHERENT are not
+    * set it will allocate a coherent BO.
+    **/
+   ANV_BO_ALLOC_HOST_CACHED =             (1 << 16),
 };
 
 struct anv_bo {
diff --git a/src/intel/vulkan/xe/anv_kmd_backend.c 
b/src/intel/vulkan/xe/anv_kmd_backend.c
index 0ead46e563b..2d48e291ea4 100644
--- a/src/intel/vulkan/xe/anv_kmd_backend.c
+++ b/src/intel/vulkan/xe/anv_kmd_backend.c
@@ -42,6 +42,8 @@ xe_gem_create(struct anv_device *device,
 {
    /* TODO: protected content */
    assert((alloc_flags & ANV_BO_ALLOC_PROTECTED) == 0);
+   /* WB+0 way coherent not supported by Xe KMD */
+   assert((alloc_flags & ANV_BO_ALLOC_HOST_CACHED) == 0);
 
    uint32_t flags = 0;
    if (alloc_flags & ANV_BO_ALLOC_SCANOUT)

Reply via email to