Skip to content

Commit 9eb5920

Browse files
yiliu1765jgunthorpe
authored andcommitted
iommufd/selftest: Add set_dev_pasid in mock iommu
The callback is needed to make pasid_attach/detach path complete for mock device. A nop is enough for set_dev_pasid. A MOCK_FLAGS_DEVICE_PASID is added to indicate a pasid-capable mock device for the pasid test cases. Other test cases will still create a non-pasid mock device. While the mock iommu always pretends to be pasid-capable. Link: https://patch.msgid.link/r/[email protected] Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Nicolin Chen <[email protected]> Signed-off-by: Yi Liu <[email protected]> Tested-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent dbc5f37 commit 9eb5920

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

drivers/iommu/iommufd/iommufd_test.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum {
4949
enum {
5050
MOCK_FLAGS_DEVICE_NO_DIRTY = 1 << 0,
5151
MOCK_FLAGS_DEVICE_HUGE_IOVA = 1 << 1,
52+
MOCK_FLAGS_DEVICE_PASID = 1 << 2,
5253
};
5354

5455
enum {
@@ -154,6 +155,9 @@ struct iommu_test_cmd {
154155
};
155156
#define IOMMU_TEST_CMD _IO(IOMMUFD_TYPE, IOMMUFD_CMD_BASE + 32)
156157

158+
/* Mock device/iommu PASID width */
159+
#define MOCK_PASID_WIDTH 20
160+
157161
/* Mock structs for IOMMU_DEVICE_GET_HW_INFO ioctl */
158162
#define IOMMU_HW_INFO_TYPE_SELFTEST 0xfeedbeef
159163
#define IOMMU_HW_INFO_SELFTEST_REGVAL 0xdeadbeef

drivers/iommu/iommufd/selftest.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,16 @@ static int mock_domain_nop_attach(struct iommu_domain *domain,
223223
return 0;
224224
}
225225

226+
static int mock_domain_set_dev_pasid_nop(struct iommu_domain *domain,
227+
struct device *dev, ioasid_t pasid,
228+
struct iommu_domain *old)
229+
{
230+
return 0;
231+
}
232+
226233
static const struct iommu_domain_ops mock_blocking_ops = {
227234
.attach_dev = mock_domain_nop_attach,
235+
.set_dev_pasid = mock_domain_set_dev_pasid_nop
228236
};
229237

230238
static struct iommu_domain mock_blocking_domain = {
@@ -366,7 +374,7 @@ mock_domain_alloc_nested(struct device *dev, struct iommu_domain *parent,
366374
struct mock_iommu_domain_nested *mock_nested;
367375
struct mock_iommu_domain *mock_parent;
368376

369-
if (flags)
377+
if (flags & ~IOMMU_HWPT_ALLOC_PASID)
370378
return ERR_PTR(-EOPNOTSUPP);
371379
if (!parent || parent->ops != mock_ops.default_domain_ops)
372380
return ERR_PTR(-EINVAL);
@@ -388,7 +396,8 @@ mock_domain_alloc_paging_flags(struct device *dev, u32 flags,
388396
{
389397
bool has_dirty_flag = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
390398
const u32 PAGING_FLAGS = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
391-
IOMMU_HWPT_ALLOC_NEST_PARENT;
399+
IOMMU_HWPT_ALLOC_NEST_PARENT |
400+
IOMMU_HWPT_ALLOC_PASID;
392401
struct mock_dev *mdev = to_mock_dev(dev);
393402
bool no_dirty_ops = mdev->flags & MOCK_FLAGS_DEVICE_NO_DIRTY;
394403
struct mock_iommu_domain *mock;
@@ -608,7 +617,7 @@ mock_viommu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
608617
struct mock_viommu *mock_viommu = to_mock_viommu(viommu);
609618
struct mock_iommu_domain_nested *mock_nested;
610619

611-
if (flags)
620+
if (flags & ~IOMMU_HWPT_ALLOC_PASID)
612621
return ERR_PTR(-EOPNOTSUPP);
613622

614623
mock_nested = __mock_domain_alloc_nested(user_data);
@@ -743,6 +752,7 @@ static const struct iommu_ops mock_ops = {
743752
.map_pages = mock_domain_map_pages,
744753
.unmap_pages = mock_domain_unmap_pages,
745754
.iova_to_phys = mock_domain_iova_to_phys,
755+
.set_dev_pasid = mock_domain_set_dev_pasid_nop,
746756
},
747757
};
748758

@@ -803,6 +813,7 @@ static struct iommu_domain_ops domain_nested_ops = {
803813
.free = mock_domain_free_nested,
804814
.attach_dev = mock_domain_nop_attach,
805815
.cache_invalidate_user = mock_domain_cache_invalidate_user,
816+
.set_dev_pasid = mock_domain_set_dev_pasid_nop,
806817
};
807818

808819
static inline struct iommufd_hw_pagetable *
@@ -862,11 +873,17 @@ static void mock_dev_release(struct device *dev)
862873

863874
static struct mock_dev *mock_dev_create(unsigned long dev_flags)
864875
{
876+
struct property_entry prop[] = {
877+
PROPERTY_ENTRY_U32("pasid-num-bits", 0),
878+
{},
879+
};
880+
const u32 valid_flags = MOCK_FLAGS_DEVICE_NO_DIRTY |
881+
MOCK_FLAGS_DEVICE_HUGE_IOVA |
882+
MOCK_FLAGS_DEVICE_PASID;
865883
struct mock_dev *mdev;
866884
int rc, i;
867885

868-
if (dev_flags &
869-
~(MOCK_FLAGS_DEVICE_NO_DIRTY | MOCK_FLAGS_DEVICE_HUGE_IOVA))
886+
if (dev_flags & ~valid_flags)
870887
return ERR_PTR(-EINVAL);
871888

872889
mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
@@ -890,6 +907,15 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags)
890907
if (rc)
891908
goto err_put;
892909

910+
if (dev_flags & MOCK_FLAGS_DEVICE_PASID)
911+
prop[0] = PROPERTY_ENTRY_U32("pasid-num-bits", MOCK_PASID_WIDTH);
912+
913+
rc = device_create_managed_software_node(&mdev->dev, prop, NULL);
914+
if (rc) {
915+
dev_err(&mdev->dev, "add pasid-num-bits property failed, rc: %d", rc);
916+
goto err_put;
917+
}
918+
893919
rc = device_add(&mdev->dev);
894920
if (rc)
895921
goto err_put;
@@ -1778,6 +1804,7 @@ int __init iommufd_test_init(void)
17781804
init_completion(&mock_iommu.complete);
17791805

17801806
mock_iommu_iopf_queue = iopf_queue_alloc("mock-iopfq");
1807+
mock_iommu.iommu_dev.max_pasids = (1 << MOCK_PASID_WIDTH);
17811808

17821809
return 0;
17831810

0 commit comments

Comments
 (0)