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

Author: Lionel Landwerlin <[email protected]>
Date:   Fri Dec  4 13:44:37 2020 +0200

anv: handle protected memory allocation

v2: Add assert on VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT in vkMapMemory

Signed-off-by: Lionel Landwerlin <[email protected]>
Reviewed-by: José Roberto de Souza <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8064>

---

 src/intel/vulkan/anv_allocator.c        |  1 +
 src/intel/vulkan/anv_device.c           | 18 +++++++++++++++++-
 src/intel/vulkan/anv_image.c            | 15 ++++++++++++---
 src/intel/vulkan/anv_private.h          |  3 +++
 src/intel/vulkan/i915/anv_device.c      | 12 ++++++++++++
 src/intel/vulkan/i915/anv_kmd_backend.c | 13 +++++++++++--
 src/intel/vulkan/xe/anv_kmd_backend.c   |  3 +++
 7 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/src/intel/vulkan/anv_allocator.c b/src/intel/vulkan/anv_allocator.c
index 469955fe81d..29f60ab96aa 100644
--- a/src/intel/vulkan/anv_allocator.c
+++ b/src/intel/vulkan/anv_allocator.c
@@ -1592,6 +1592,7 @@ anv_device_import_bo_from_host_ptr(struct anv_device 
*device,
    assert(!(alloc_flags & (ANV_BO_ALLOC_MAPPED |
                            ANV_BO_ALLOC_SNOOPED |
                            ANV_BO_ALLOC_DEDICATED |
+                           ANV_BO_ALLOC_PROTECTED |
                            ANV_BO_ALLOC_FIXED_ADDRESS)));
    assert(alloc_flags & ANV_BO_ALLOC_EXTERNAL);
 
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index c5ab8b7b60d..514f29649e9 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -4029,6 +4029,9 @@ VkResult anv_AllocateMemory(
    if (mem->vk.alloc_flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT)
       alloc_flags |= ANV_BO_ALLOC_CLIENT_VISIBLE_ADDRESS;
 
+   if (mem->vk.alloc_flags & VK_MEMORY_PROPERTY_PROTECTED_BIT)
+      alloc_flags |= ANV_BO_ALLOC_PROTECTED;
+
    /* Anything imported or exported is EXTERNAL. Apply implicit sync to be
     * compatible with clients relying on implicit fencing. This matches the
     * behavior in iris i915_batch_submit. An example client is VA-API.
@@ -4541,6 +4544,7 @@ VkResult anv_ResetEvent(
 
 static void
 anv_get_buffer_memory_requirements(struct anv_device *device,
+                                   VkBufferCreateFlags flags,
                                    VkDeviceSize size,
                                    VkBufferUsageFlags usage,
                                    bool is_sparse,
@@ -4553,7 +4557,18 @@ anv_get_buffer_memory_requirements(struct anv_device 
*device,
     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
     *    structure for the physical device is supported.
     */
-   uint32_t memory_types = (1ull << device->physical->memory.type_count) - 1;
+   uint32_t memory_types = 0;
+   for (uint32_t i = 0; i < device->physical->memory.type_count; i++) {
+      /* Have the protected buffer bit match only the memory types with the
+       * equivalent bit.
+       */
+      if (!!(flags & VK_BUFFER_CREATE_PROTECTED_BIT) !=
+          !!(device->physical->memory.types[i].propertyFlags &
+             VK_MEMORY_PROPERTY_PROTECTED_BIT))
+         continue;
+
+      memory_types |= 1ull << i;
+   }
 
    /* The GPU appears to write back to main memory in cachelines. Writes to a
     * buffers should not clobber with writes to another buffers so make sure
@@ -4622,6 +4637,7 @@ void anv_GetDeviceBufferMemoryRequirementsKHR(
               __LINE__, pInfo->pCreateInfo->flags);
 
    anv_get_buffer_memory_requirements(device,
+                                      pInfo->pCreateInfo->flags,
                                       pInfo->pCreateInfo->size,
                                       pInfo->pCreateInfo->usage,
                                       is_sparse,
diff --git a/src/intel/vulkan/anv_image.c b/src/intel/vulkan/anv_image.c
index 6ffe7cbd5c0..aaab1a70c54 100644
--- a/src/intel/vulkan/anv_image.c
+++ b/src/intel/vulkan/anv_image.c
@@ -1890,10 +1890,19 @@ anv_image_get_memory_requirements(struct anv_device 
*device,
     *    supported memory type for the resource. The bit `1<<i` is set if and
     *    only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
     *    structure for the physical device is supported.
-    *
-    * All types are currently supported for images.
     */
-   uint32_t memory_types = (1ull << device->physical->memory.type_count) - 1;
+   uint32_t memory_types = 0;
+   for (uint32_t i = 0; i < device->physical->memory.type_count; i++) {
+      /* Have the protected image bit match only the memory types with the
+       * equivalent bit.
+       */
+      if (!!(image->vk.create_flags & VK_IMAGE_CREATE_PROTECTED_BIT) !=
+          !!(device->physical->memory.types[i].propertyFlags &
+             VK_MEMORY_PROPERTY_PROTECTED_BIT))
+         continue;
+
+      memory_types |= 1ull << i;
+   }
 
    vk_foreach_struct(ext, pMemoryRequirements->pNext) {
       switch (ext->sType) {
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 2841f3dcfa7..b3ae01e117e 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -406,6 +406,9 @@ enum anv_bo_alloc_flags {
     * Not for buffers used as the TR-TT page tables.
     */
    ANV_BO_ALLOC_TRTT = (1 << 14),
+
+   /** Protected buffer */
+   ANV_BO_ALLOC_PROTECTED = (1 << 15),
 };
 
 struct anv_bo {
diff --git a/src/intel/vulkan/i915/anv_device.c 
b/src/intel/vulkan/i915/anv_device.c
index 392d9eeb688..9524eaef55d 100644
--- a/src/intel/vulkan/i915/anv_device.c
+++ b/src/intel/vulkan/i915/anv_device.c
@@ -206,6 +206,18 @@ anv_i915_physical_device_init_memory_types(struct 
anv_physical_device *device)
       };
    }
 
+   if (device->has_protected_contexts) {
+      /* Add a memory type for protected buffers, local and not host
+       * visible.
+       */
+      device->memory.types[device->memory.type_count++] =
+         (struct anv_memory_type) {
+            .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
+                             VK_MEMORY_PROPERTY_PROTECTED_BIT,
+            .heapIndex = 0,
+      };
+   }
+
    return VK_SUCCESS;
 }
 
diff --git a/src/intel/vulkan/i915/anv_kmd_backend.c 
b/src/intel/vulkan/i915/anv_kmd_backend.c
index 1d0cb178abc..fe9be942ca8 100644
--- a/src/intel/vulkan/i915/anv_kmd_backend.c
+++ b/src/intel/vulkan/i915/anv_kmd_backend.c
@@ -78,16 +78,18 @@ i915_gem_create(struct anv_device *device,
          flags |= I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS;
 
    struct drm_i915_gem_create_ext_memory_regions ext_regions = {
-      .base = { .name = I915_GEM_CREATE_EXT_MEMORY_REGIONS },
       .num_regions = num_regions,
       .regions = (uintptr_t)i915_regions,
    };
    struct drm_i915_gem_create_ext gem_create = {
       .size = size,
-      .extensions = (uintptr_t) &ext_regions,
       .flags = flags,
    };
 
+   intel_i915_gem_add_ext(&gem_create.extensions,
+                          I915_GEM_CREATE_EXT_MEMORY_REGIONS,
+                          &ext_regions.base);
+
    struct drm_i915_gem_create_ext_set_pat set_pat_param = { 0 };
    if (device->info->has_set_pat_uapi) {
       /* Set PAT param */
@@ -97,6 +99,13 @@ i915_gem_create(struct anv_device *device,
                              &set_pat_param.base);
    }
 
+   struct drm_i915_gem_create_ext_protected_content protected_param = { 0 };
+   if (alloc_flags & ANV_BO_ALLOC_PROTECTED) {
+      intel_i915_gem_add_ext(&gem_create.extensions,
+                             I915_GEM_CREATE_EXT_PROTECTED_CONTENT,
+                             &protected_param.base);
+   }
+
    if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &gem_create))
       return 0;
 
diff --git a/src/intel/vulkan/xe/anv_kmd_backend.c 
b/src/intel/vulkan/xe/anv_kmd_backend.c
index a3883c8e61f..5063b19ec2c 100644
--- a/src/intel/vulkan/xe/anv_kmd_backend.c
+++ b/src/intel/vulkan/xe/anv_kmd_backend.c
@@ -40,6 +40,9 @@ xe_gem_create(struct anv_device *device,
               enum anv_bo_alloc_flags alloc_flags,
               uint64_t *actual_size)
 {
+   /* TODO: protected content */
+   assert((alloc_flags & ANV_BO_ALLOC_PROTECTED) == 0);
+
    uint32_t flags = 0;
    if (alloc_flags & ANV_BO_ALLOC_SCANOUT)
       flags |= XE_GEM_CREATE_FLAG_SCANOUT;

Reply via email to