Similar to the coverage of cache_invalidate_user for iotlb invalidation,
add a device cache and an invalidation op to test IOMMU_VIOMMU_INVALIDATE
ioctl.

Signed-off-by: Nicolin Chen <nicol...@nvidia.com>
---
 drivers/iommu/iommufd/iommufd_test.h | 25 +++++++++++
 drivers/iommu/iommufd/selftest.c     | 63 +++++++++++++++++++++++++++-
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/iommufd/iommufd_test.h 
b/drivers/iommu/iommufd/iommufd_test.h
index 0bb30275a92f..da824b58927f 100644
--- a/drivers/iommu/iommufd/iommufd_test.h
+++ b/drivers/iommu/iommufd/iommufd_test.h
@@ -55,6 +55,11 @@ enum {
        MOCK_NESTED_DOMAIN_IOTLB_NUM = 4,
 };
 
+enum {
+       MOCK_DEV_CACHE_ID_MAX = 3,
+       MOCK_DEV_CACHE_NUM = 4,
+};
+
 struct iommu_test_cmd {
        __u32 size;
        __u32 op;
@@ -156,6 +161,7 @@ struct iommu_test_hw_info {
 /* Should not be equal to any defined value in enum iommu_hwpt_data_type */
 #define IOMMU_HWPT_DATA_SELFTEST 0xdead
 #define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef
+#define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad
 
 /**
  * struct iommu_hwpt_selftest
@@ -184,4 +190,23 @@ struct iommu_hwpt_invalidate_selftest {
        __u32 iotlb_id;
 };
 
+/* Should not be equal to any defined value in enum 
iommu_viommu_invalidate_data_type */
+#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef
+#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef
+
+/**
+ * struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU
+ *                                        
(IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST)
+ * @flags: Invalidate flags
+ * @cache_id: Invalidate cache entry index
+ *
+ * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored
+ */
+struct iommu_viommu_invalidate_selftest {
+#define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0)
+       __u32 flags;
+       __u32 vdev_id;
+       __u32 cache_id;
+};
+
 #endif
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index a165b162c88f..4858c74c67be 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -141,6 +141,7 @@ struct mock_dev {
        struct device dev;
        unsigned long flags;
        int id;
+       u32 cache[MOCK_DEV_CACHE_NUM];
 };
 
 struct selftest_obj {
@@ -560,6 +561,61 @@ static void mock_dev_get_resv_regions(struct device *dev,
        iommu_dma_get_resv_regions(dev, head);
 }
 
+static int mock_viommu_cache_invalidate(struct iommufd_viommu *viommu,
+                                       struct iommu_user_data_array *array)
+{
+       struct iommu_viommu_invalidate_selftest inv;
+       u32 processed = 0;
+       int i = 0, j;
+       int rc = 0;
+
+       if (array->type != IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       for ( ; i < array->entry_num; i++) {
+               struct mock_dev *mdev;
+               struct device *dev;
+
+               rc = iommu_copy_struct_from_user_array(&inv, array,
+                       IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, i, cache_id);
+               if (rc)
+                       break;
+
+               if (inv.flags & ~IOMMU_TEST_INVALIDATE_FLAG_ALL) {
+                       rc = -EOPNOTSUPP;
+                       break;
+               }
+
+               if (inv.cache_id > MOCK_DEV_CACHE_ID_MAX) {
+                       rc = -EINVAL;
+                       break;
+               }
+
+               dev = iommufd_viommu_find_device(viommu, inv.vdev_id);
+               if (!dev) {
+                       rc = -EINVAL;
+                       break;
+               }
+               mdev = container_of(dev, struct mock_dev, dev);
+
+               if (inv.flags & IOMMU_TEST_INVALIDATE_FLAG_ALL) {
+                       /* Invalidate all cache entries and ignore cache_id */
+                       for (j = 0; j < MOCK_DEV_CACHE_NUM; j++)
+                               mdev->cache[j] = 0;
+               } else {
+                       mdev->cache[inv.cache_id] = 0;
+               }
+
+               processed++;
+       }
+
+out:
+       array->entry_num = processed;
+       return rc;
+}
+
 static const struct iommu_ops mock_ops = {
        /*
         * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type()
@@ -587,6 +643,9 @@ static const struct iommu_ops mock_ops = {
                        .map_pages = mock_domain_map_pages,
                        .unmap_pages = mock_domain_unmap_pages,
                        .iova_to_phys = mock_domain_iova_to_phys,
+                       .default_viommu_ops = &(struct iommufd_viommu_ops){
+                               .cache_invalidate = 
mock_viommu_cache_invalidate,
+                       },
                },
 };
 
@@ -722,7 +781,7 @@ static void mock_dev_release(struct device *dev)
 static struct mock_dev *mock_dev_create(unsigned long dev_flags)
 {
        struct mock_dev *mdev;
-       int rc;
+       int rc, i;
 
        if (dev_flags &
            ~(MOCK_FLAGS_DEVICE_NO_DIRTY | MOCK_FLAGS_DEVICE_HUGE_IOVA))
@@ -737,6 +796,8 @@ static struct mock_dev *mock_dev_create(unsigned long 
dev_flags)
        mdev->flags = dev_flags;
        mdev->dev.release = mock_dev_release;
        mdev->dev.bus = &iommufd_mock_bus_type.bus;
+       for (i = 0; i < MOCK_DEV_CACHE_NUM; i++)
+               mdev->cache[i] = IOMMU_TEST_DEV_CACHE_DEFAULT;
 
        rc = ida_alloc(&mock_dev_ida, GFP_KERNEL);
        if (rc < 0)
-- 
2.43.0


Reply via email to