Add xe_bo_mempolicy_modes(), which returns the
DRM_XE_GEM_CREATE_MEMPOLICY_MODE_* values the device can apply. TTM only
applies NUMA placement when the pool does not use DMA allocation, so
report no modes in that case.

Use it to answer DRM_XE_DEVICE_QUERY_MEMPOLICY, and to reject a
GEM_CREATE mempolicy whose mode is not supported with -EOPNOTSUPP,
matching the query, instead of letting BO creation succeed and the
populate fail later.

Signed-off-by: Himal Prasad Ghimiray <[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c    | 49 ++++++++++++++++++++++++++---------
 drivers/gpu/drm/xe/xe_bo.h    |  1 +
 drivers/gpu/drm/xe/xe_query.c | 23 ++++++++++++++++
 3 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index a831dd4d7cad..58ec0c8a3bd0 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -3662,6 +3662,36 @@ static int gem_create_user_ext_set_property(struct 
xe_device *xe,
        return gem_create_set_property_funcs[idx](xe, data, ext.value);
 }
 
+/* Modes left as TTM_NUMA_POLICY_NONE are not supported */
+static const enum ttm_numa_policy xe_mempolicy_to_ttm[] = {
+       [DRM_XE_GEM_CREATE_MEMPOLICY_MODE_PREFERRED] = 
TTM_NUMA_POLICY_PREFERRED,
+       [DRM_XE_GEM_CREATE_MEMPOLICY_MODE_BIND] = TTM_NUMA_POLICY_BIND,
+       [DRM_XE_GEM_CREATE_MEMPOLICY_MODE_INTERLEAVE] = 
TTM_NUMA_POLICY_INTERLEAVE,
+};
+
+/**
+ * xe_bo_mempolicy_modes() - Host NUMA mempolicy modes supported by the device
+ * @xe: The xe device.
+ *
+ * Return: Bitmask of BIT(%DRM_XE_GEM_CREATE_MEMPOLICY_MODE_*) values that
+ * %DRM_XE_GEM_CREATE_EXTENSION_SET_MEMPOLICY accepts.
+ */
+u32 xe_bo_mempolicy_modes(struct xe_device *xe)
+{
+       u32 modes = 0;
+       int i;
+
+       /* TTM cannot apply NUMA placement to DMA-allocated pools */
+       if (xe->ttm.pool.alloc_flags & TTM_ALLOCATION_POOL_USE_DMA_ALLOC)
+               return 0;
+
+       for (i = 0; i < ARRAY_SIZE(xe_mempolicy_to_ttm); i++)
+               if (xe_mempolicy_to_ttm[i] != TTM_NUMA_POLICY_NONE)
+                       modes |= BIT(i);
+
+       return modes;
+}
+
 static int gem_create_set_mempolicy(struct xe_device *xe,
                                    struct xe_gem_create_ext_data *data,
                                    u64 extension)
@@ -3681,19 +3711,14 @@ static int gem_create_set_mempolicy(struct xe_device 
*xe,
        if (XE_IOCTL_DBG(xe, ext.flags) || XE_IOCTL_DBG(xe, ext.pad))
                return -EINVAL;
 
-       switch (ext.mode) {
-       case DRM_XE_GEM_CREATE_MEMPOLICY_MODE_PREFERRED:
-               mode = TTM_NUMA_POLICY_PREFERRED;
-               break;
-       case DRM_XE_GEM_CREATE_MEMPOLICY_MODE_BIND:
-               mode = TTM_NUMA_POLICY_BIND;
-               break;
-       case DRM_XE_GEM_CREATE_MEMPOLICY_MODE_INTERLEAVE:
-               mode = TTM_NUMA_POLICY_INTERLEAVE;
-               break;
-       default:
+       if (XE_IOCTL_DBG(xe, ext.mode >= ARRAY_SIZE(xe_mempolicy_to_ttm)))
                return -EINVAL;
-       }
+
+       if (XE_IOCTL_DBG(xe, !(xe_bo_mempolicy_modes(xe) & BIT(ext.mode))))
+               return -EOPNOTSUPP;
+
+       mode = xe_mempolicy_to_ttm[array_index_nospec(ext.mode,
+                                                     
ARRAY_SIZE(xe_mempolicy_to_ttm))];
 
        if (XE_IOCTL_DBG(xe, !ext.maxnode || ext.maxnode > MAX_NUMNODES))
                return -EINVAL;
diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
index 4b2c17bb0f9c..e46a5c861465 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -454,6 +454,7 @@ extern const char *const xe_mem_type_to_name[];
 
 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
                        struct drm_file *file);
+u32 xe_bo_mempolicy_modes(struct xe_device *xe);
 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
                             struct drm_file *file);
 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
diff --git a/drivers/gpu/drm/xe/xe_query.c b/drivers/gpu/drm/xe/xe_query.c
index 9cc35fc83954..f635ffa14e7b 100644
--- a/drivers/gpu/drm/xe/xe_query.c
+++ b/drivers/gpu/drm/xe/xe_query.c
@@ -788,6 +788,28 @@ static int query_eu_stall(struct xe_device *xe,
        return ret ? -EFAULT : 0;
 }
 
+static int query_mempolicy(struct xe_device *xe,
+                          struct drm_xe_device_query *query)
+{
+       struct drm_xe_query_mempolicy __user *query_ptr = 
u64_to_user_ptr(query->data);
+       size_t size = sizeof(struct drm_xe_query_mempolicy);
+       struct drm_xe_query_mempolicy resp = {};
+
+       if (query->size == 0) {
+               query->size = size;
+               return 0;
+       } else if (XE_IOCTL_DBG(xe, query->size != size)) {
+               return -EINVAL;
+       }
+
+       resp.supported_modes = xe_bo_mempolicy_modes(xe);
+
+       if (copy_to_user(query_ptr, &resp, size))
+               return -EFAULT;
+
+       return 0;
+}
+
 static int (* const xe_query_funcs[])(struct xe_device *xe,
                                      struct drm_xe_device_query *query) = {
        query_engines,
@@ -801,6 +823,7 @@ static int (* const xe_query_funcs[])(struct xe_device *xe,
        query_oa_units,
        query_pxp_status,
        query_eu_stall,
+       query_mempolicy,
 };
 
 int xe_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
-- 
2.43.0

Reply via email to