Commit 56e1a4cc authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Will Deacon

iommu/arm-smmu-v3: Add unit tests for arm_smmu_write_entry

Add tests for some of the more common STE update operations that we expect
to see, as well as some artificial STE updates to test the edges of
arm_smmu_write_entry. These also serve as a record of which common
operation is expected to be hitless, and how many syncs they require.

arm_smmu_write_entry implements a generic algorithm that updates an STE/CD
to any other abritrary STE/CD configuration. The update requires a
sequence of write+sync operations with some invariants that must be held
true after each sync. arm_smmu_write_entry lends itself well to
unit-testing since the function's interaction with the STE/CD is already
abstracted by input callbacks that we can hook to introspect into the
sequence of operations. We can use these hooks to guarantee that
invariants are held throughout the entire update operation.

Link: https://lore.kernel.org/r/20240106083617.1173871-3-mshavit@google.comTested-by: default avatarNicolin Chen <nicolinc@nvidia.com>
Signed-off-by: default avatarMichael Shavit <mshavit@google.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/9-v9-5040dc602008+177d7-smmuv3_newapi_p2_jgg@nvidia.comSigned-off-by: default avatarWill Deacon <will@kernel.org>
parent 04905c17
...@@ -401,9 +401,9 @@ config ARM_SMMU_V3 ...@@ -401,9 +401,9 @@ config ARM_SMMU_V3
Say Y here if your system includes an IOMMU device implementing Say Y here if your system includes an IOMMU device implementing
the ARM SMMUv3 architecture. the ARM SMMUv3 architecture.
if ARM_SMMU_V3
config ARM_SMMU_V3_SVA config ARM_SMMU_V3_SVA
bool "Shared Virtual Addressing support for the ARM SMMUv3" bool "Shared Virtual Addressing support for the ARM SMMUv3"
depends on ARM_SMMU_V3
select IOMMU_SVA select IOMMU_SVA
select IOMMU_IOPF select IOMMU_IOPF
select MMU_NOTIFIER select MMU_NOTIFIER
...@@ -414,6 +414,17 @@ config ARM_SMMU_V3_SVA ...@@ -414,6 +414,17 @@ config ARM_SMMU_V3_SVA
Say Y here if your system supports SVA extensions such as PCIe PASID Say Y here if your system supports SVA extensions such as PCIe PASID
and PRI. and PRI.
config ARM_SMMU_V3_KUNIT_TEST
bool "KUnit tests for arm-smmu-v3 driver" if !KUNIT_ALL_TESTS
depends on KUNIT
depends on ARM_SMMU_V3_SVA
default KUNIT_ALL_TESTS
help
Enable this option to unit-test arm-smmu-v3 driver functions.
If unsure, say N.
endif
config S390_IOMMU config S390_IOMMU
def_bool y if S390 && PCI def_bool y if S390 && PCI
depends on S390 && PCI depends on S390 && PCI
......
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o obj-$(CONFIG_ARM_SMMU_V3) += arm_smmu_v3.o
arm_smmu_v3-objs-y += arm-smmu-v3.o arm_smmu_v3-objs-y += arm-smmu-v3.o
arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_SVA) += arm-smmu-v3-sva.o
arm_smmu_v3-objs-$(CONFIG_ARM_SMMU_V3_KUNIT_TEST) += arm-smmu-v3-test.o
arm_smmu_v3-objs := $(arm_smmu_v3-objs-y) arm_smmu_v3-objs := $(arm_smmu_v3-objs-y)
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <linux/mmu_notifier.h> #include <linux/mmu_notifier.h>
#include <linux/sched/mm.h> #include <linux/sched/mm.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <kunit/visibility.h>
#include "arm-smmu-v3.h" #include "arm-smmu-v3.h"
#include "../../io-pgtable-arm.h" #include "../../io-pgtable-arm.h"
...@@ -120,9 +121,10 @@ static u64 page_size_to_cd(void) ...@@ -120,9 +121,10 @@ static u64 page_size_to_cd(void)
return ARM_LPAE_TCR_TG0_4K; return ARM_LPAE_TCR_TG0_4K;
} }
static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, VISIBLE_IF_KUNIT
struct arm_smmu_master *master, void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
struct mm_struct *mm, u16 asid) struct arm_smmu_master *master, struct mm_struct *mm,
u16 asid)
{ {
u64 par; u64 par;
......
This diff is collapsed.
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <linux/pci.h> #include <linux/pci.h>
#include <linux/pci-ats.h> #include <linux/pci-ats.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <kunit/visibility.h>
#include "arm-smmu-v3.h" #include "arm-smmu-v3.h"
#include "../../dma-iommu.h" #include "../../dma-iommu.h"
...@@ -42,17 +43,6 @@ enum arm_smmu_msi_index { ...@@ -42,17 +43,6 @@ enum arm_smmu_msi_index {
ARM_SMMU_MAX_MSIS, ARM_SMMU_MAX_MSIS,
}; };
struct arm_smmu_entry_writer_ops;
struct arm_smmu_entry_writer {
const struct arm_smmu_entry_writer_ops *ops;
struct arm_smmu_master *master;
};
struct arm_smmu_entry_writer_ops {
void (*get_used)(const __le64 *entry, __le64 *used);
void (*sync)(struct arm_smmu_entry_writer *writer);
};
#define NUM_ENTRY_QWORDS 8 #define NUM_ENTRY_QWORDS 8
static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64)); static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64));
static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64)); static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64));
...@@ -979,7 +969,8 @@ void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid) ...@@ -979,7 +969,8 @@ void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid)
* would be nice if this was complete according to the spec, but minimally it * would be nice if this was complete according to the spec, but minimally it
* has to capture the bits this driver uses. * has to capture the bits this driver uses.
*/ */
static void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits) VISIBLE_IF_KUNIT
void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits)
{ {
unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0])); unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0]));
...@@ -1101,8 +1092,9 @@ static bool entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry, ...@@ -1101,8 +1092,9 @@ static bool entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry,
* V=0 process. This relies on the IGNORED behavior described in the * V=0 process. This relies on the IGNORED behavior described in the
* specification. * specification.
*/ */
static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, VISIBLE_IF_KUNIT
__le64 *entry, const __le64 *target) void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry,
const __le64 *target)
{ {
__le64 unused_update[NUM_ENTRY_QWORDS]; __le64 unused_update[NUM_ENTRY_QWORDS];
u8 used_qword_diff; u8 used_qword_diff;
...@@ -1256,7 +1248,8 @@ struct arm_smmu_cd_writer { ...@@ -1256,7 +1248,8 @@ struct arm_smmu_cd_writer {
unsigned int ssid; unsigned int ssid;
}; };
static void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) VISIBLE_IF_KUNIT
void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits)
{ {
used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V); used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V);
if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V))) if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V)))
...@@ -1514,7 +1507,8 @@ static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid, ...@@ -1514,7 +1507,8 @@ static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid,
} }
} }
static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) VISIBLE_IF_KUNIT
void arm_smmu_make_abort_ste(struct arm_smmu_ste *target)
{ {
memset(target, 0, sizeof(*target)); memset(target, 0, sizeof(*target));
target->data[0] = cpu_to_le64( target->data[0] = cpu_to_le64(
...@@ -1522,7 +1516,8 @@ static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target) ...@@ -1522,7 +1516,8 @@ static void arm_smmu_make_abort_ste(struct arm_smmu_ste *target)
FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT)); FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT));
} }
static void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, VISIBLE_IF_KUNIT
void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
struct arm_smmu_ste *target) struct arm_smmu_ste *target)
{ {
memset(target, 0, sizeof(*target)); memset(target, 0, sizeof(*target));
...@@ -1535,7 +1530,8 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu, ...@@ -1535,7 +1530,8 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
STRTAB_STE_1_SHCFG_INCOMING)); STRTAB_STE_1_SHCFG_INCOMING));
} }
static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, VISIBLE_IF_KUNIT
void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
struct arm_smmu_master *master) struct arm_smmu_master *master)
{ {
struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
...@@ -1585,7 +1581,8 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, ...@@ -1585,7 +1581,8 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
} }
} }
static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, VISIBLE_IF_KUNIT
void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
struct arm_smmu_master *master, struct arm_smmu_master *master,
struct arm_smmu_domain *smmu_domain) struct arm_smmu_domain *smmu_domain)
{ {
......
...@@ -736,6 +736,36 @@ struct arm_smmu_domain { ...@@ -736,6 +736,36 @@ struct arm_smmu_domain {
struct list_head mmu_notifiers; struct list_head mmu_notifiers;
}; };
/* The following are exposed for testing purposes. */
struct arm_smmu_entry_writer_ops;
struct arm_smmu_entry_writer {
const struct arm_smmu_entry_writer_ops *ops;
struct arm_smmu_master *master;
};
struct arm_smmu_entry_writer_ops {
void (*get_used)(const __le64 *entry, __le64 *used);
void (*sync)(struct arm_smmu_entry_writer *writer);
};
#if IS_ENABLED(CONFIG_KUNIT)
void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits);
void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
const __le64 *target);
void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits);
void arm_smmu_make_abort_ste(struct arm_smmu_ste *target);
void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
struct arm_smmu_ste *target);
void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
struct arm_smmu_master *master);
void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
struct arm_smmu_master *master,
struct arm_smmu_domain *smmu_domain);
void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
struct arm_smmu_master *master, struct mm_struct *mm,
u16 asid);
#endif
static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
{ {
return container_of(dom, struct arm_smmu_domain, domain); return container_of(dom, struct arm_smmu_domain, domain);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment