Skip to content

Commit 7b7563a

Browse files
ssuthiku-amdjoergroedel
authored andcommitted
iommu/amd: Consolidate feature detection and reporting logic
Currently, IOMMU driver assumes capabilities on all IOMMU instances to be homogeneous. During early_amd_iommu_init(), the driver probes all IVHD blocks and do sanity check to make sure that only features common among all IOMMU instances are supported. This is tracked in the global amd_iommu_efr and amd_iommu_efr2, which should be used whenever the driver need to check hardware capabilities. Therefore, introduce check_feature() and check_feature2(), and modify the driver to adopt the new helper functions. In addition, clean up the print_iommu_info() to avoid reporting redundant EFR/EFR2 for each IOMMU instance. Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com> Link: https://lore.kernel.org/r/20230921092147.5930-9-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 45677ab commit 7b7563a

File tree

4 files changed

+54
-60
lines changed

4 files changed

+54
-60
lines changed

drivers/iommu/amd/amd_iommu.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,19 @@ static inline bool is_rd890_iommu(struct pci_dev *pdev)
8787
(pdev->device == PCI_DEVICE_ID_RD890_IOMMU);
8888
}
8989

90-
static inline bool iommu_feature(struct amd_iommu *iommu, u64 mask)
90+
static inline bool check_feature(u64 mask)
9191
{
92-
return !!(iommu->features & mask);
92+
return (amd_iommu_efr & mask);
93+
}
94+
95+
static inline bool check_feature2(u64 mask)
96+
{
97+
return (amd_iommu_efr2 & mask);
98+
}
99+
100+
static inline int check_feature_gpt_level(void)
101+
{
102+
return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK);
93103
}
94104

95105
static inline u64 iommu_virt_to_phys(void *vaddr)
@@ -145,8 +155,5 @@ void amd_iommu_domain_set_pgtable(struct protection_domain *domain,
145155
u64 *root, int mode);
146156
struct dev_table_entry *get_dev_table(struct amd_iommu *iommu);
147157

148-
extern u64 amd_iommu_efr;
149-
extern u64 amd_iommu_efr2;
150-
151158
extern bool amd_iommu_snp_en;
152159
#endif

drivers/iommu/amd/amd_iommu_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ extern bool amd_iommu_force_isolation;
897897
/* Max levels of glxval supported */
898898
extern int amd_iommu_max_glx_val;
899899

900+
/* Global EFR and EFR2 registers */
901+
extern u64 amd_iommu_efr;
902+
extern u64 amd_iommu_efr2;
903+
900904
/*
901905
* This function flushes all internal caches of
902906
* the IOMMU used by this driver.

drivers/iommu/amd/init.c

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ int amd_iommu_get_num_iommus(void)
270270
* Iterate through all the IOMMUs to get common EFR
271271
* masks among all IOMMUs and warn if found inconsistency.
272272
*/
273-
static void get_global_efr(void)
273+
static __init void get_global_efr(void)
274274
{
275275
struct amd_iommu *iommu;
276276

@@ -302,16 +302,6 @@ static void get_global_efr(void)
302302
pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
303303
}
304304

305-
static bool check_feature_on_all_iommus(u64 mask)
306-
{
307-
return !!(amd_iommu_efr & mask);
308-
}
309-
310-
static inline int check_feature_gpt_level(void)
311-
{
312-
return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK);
313-
}
314-
315305
/*
316306
* For IVHD type 0x11/0x40, EFR is also available via IVHD.
317307
* Default to IVHD EFR since it is available sooner
@@ -397,7 +387,7 @@ static void iommu_set_cwwb_range(struct amd_iommu *iommu)
397387
u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
398388
u64 entry = start & PM_ADDR_MASK;
399389

400-
if (!check_feature_on_all_iommus(FEATURE_SNP))
390+
if (!check_feature(FEATURE_SNP))
401391
return;
402392

403393
/* Note:
@@ -867,7 +857,7 @@ static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
867857
void *buf = (void *)__get_free_pages(gfp, order);
868858

869859
if (buf &&
870-
check_feature_on_all_iommus(FEATURE_SNP) &&
860+
check_feature(FEATURE_SNP) &&
871861
set_memory_4k((unsigned long)buf, (1 << order))) {
872862
free_pages((unsigned long)buf, order);
873863
buf = NULL;
@@ -1046,7 +1036,7 @@ static void iommu_enable_xt(struct amd_iommu *iommu)
10461036

10471037
static void iommu_enable_gt(struct amd_iommu *iommu)
10481038
{
1049-
if (!iommu_feature(iommu, FEATURE_GT))
1039+
if (!check_feature(FEATURE_GT))
10501040
return;
10511041

10521042
iommu_feature_enable(iommu, CONTROL_GT_EN);
@@ -1985,7 +1975,7 @@ static void init_iommu_perf_ctr(struct amd_iommu *iommu)
19851975
u64 val;
19861976
struct pci_dev *pdev = iommu->dev;
19871977

1988-
if (!iommu_feature(iommu, FEATURE_PC))
1978+
if (!check_feature(FEATURE_PC))
19891979
return;
19901980

19911981
amd_iommu_pc_present = true;
@@ -2012,8 +2002,7 @@ static ssize_t amd_iommu_show_features(struct device *dev,
20122002
struct device_attribute *attr,
20132003
char *buf)
20142004
{
2015-
struct amd_iommu *iommu = dev_to_amd_iommu(dev);
2016-
return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);
2005+
return sysfs_emit(buf, "%llx:%llx\n", amd_iommu_efr, amd_iommu_efr2);
20172006
}
20182007
static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
20192008

@@ -2049,22 +2038,22 @@ static void __init late_iommu_features_init(struct amd_iommu *iommu)
20492038
features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
20502039
features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
20512040

2052-
if (!iommu->features) {
2053-
iommu->features = features;
2054-
iommu->features2 = features2;
2041+
if (!amd_iommu_efr) {
2042+
amd_iommu_efr = features;
2043+
amd_iommu_efr2 = features2;
20552044
return;
20562045
}
20572046

20582047
/*
20592048
* Sanity check and warn if EFR values from
20602049
* IVHD and MMIO conflict.
20612050
*/
2062-
if (features != iommu->features ||
2063-
features2 != iommu->features2) {
2051+
if (features != amd_iommu_efr ||
2052+
features2 != amd_iommu_efr2) {
20642053
pr_warn(FW_WARN
20652054
"EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
2066-
features, iommu->features,
2067-
features2, iommu->features2);
2055+
features, amd_iommu_efr,
2056+
features2, amd_iommu_efr2);
20682057
}
20692058
}
20702059

@@ -2090,20 +2079,20 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
20902079

20912080
late_iommu_features_init(iommu);
20922081

2093-
if (iommu_feature(iommu, FEATURE_GT)) {
2082+
if (check_feature(FEATURE_GT)) {
20942083
int glxval;
20952084
u32 max_pasid;
20962085
u64 pasmax;
20972086

2098-
pasmax = iommu->features & FEATURE_PASID_MASK;
2087+
pasmax = amd_iommu_efr & FEATURE_PASID_MASK;
20992088
pasmax >>= FEATURE_PASID_SHIFT;
21002089
max_pasid = (1 << (pasmax + 1)) - 1;
21012090

21022091
amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
21032092

21042093
BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
21052094

2106-
glxval = iommu->features & FEATURE_GLXVAL_MASK;
2095+
glxval = amd_iommu_efr & FEATURE_GLXVAL_MASK;
21072096
glxval >>= FEATURE_GLXVAL_SHIFT;
21082097

21092098
if (amd_iommu_max_glx_val == -1)
@@ -2112,13 +2101,13 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
21122101
amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
21132102
}
21142103

2115-
if (iommu_feature(iommu, FEATURE_GT) &&
2116-
iommu_feature(iommu, FEATURE_PPR)) {
2104+
if (check_feature(FEATURE_GT) &&
2105+
check_feature(FEATURE_PPR)) {
21172106
iommu->is_iommu_v2 = true;
21182107
amd_iommu_v2_present = true;
21192108
}
21202109

2121-
if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
2110+
if (check_feature(FEATURE_PPR) && alloc_ppr_log(iommu))
21222111
return -ENOMEM;
21232112

21242113
if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
@@ -2130,8 +2119,8 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
21302119
init_iommu_perf_ctr(iommu);
21312120

21322121
if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2133-
if (!iommu_feature(iommu, FEATURE_GIOSUP) ||
2134-
!iommu_feature(iommu, FEATURE_GT)) {
2122+
if (!check_feature(FEATURE_GIOSUP) ||
2123+
!check_feature(FEATURE_GT)) {
21352124
pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
21362125
amd_iommu_pgtable = AMD_IOMMU_V1;
21372126
}
@@ -2181,35 +2170,29 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
21812170

21822171
static void print_iommu_info(void)
21832172
{
2173+
int i;
21842174
static const char * const feat_str[] = {
21852175
"PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
21862176
"IA", "GA", "HE", "PC"
21872177
};
2188-
struct amd_iommu *iommu;
2189-
2190-
for_each_iommu(iommu) {
2191-
struct pci_dev *pdev = iommu->dev;
2192-
int i;
2193-
2194-
pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
21952178

2196-
if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
2197-
pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);
2179+
if (amd_iommu_efr) {
2180+
pr_info("Extended features (%#llx, %#llx):", amd_iommu_efr, amd_iommu_efr2);
21982181

2199-
for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
2200-
if (iommu_feature(iommu, (1ULL << i)))
2201-
pr_cont(" %s", feat_str[i]);
2202-
}
2182+
for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
2183+
if (check_feature(1ULL << i))
2184+
pr_cont(" %s", feat_str[i]);
2185+
}
22032186

2204-
if (iommu->features & FEATURE_GAM_VAPIC)
2205-
pr_cont(" GA_vAPIC");
2187+
if (check_feature(FEATURE_GAM_VAPIC))
2188+
pr_cont(" GA_vAPIC");
22062189

2207-
if (iommu->features & FEATURE_SNP)
2208-
pr_cont(" SNP");
2190+
if (check_feature(FEATURE_SNP))
2191+
pr_cont(" SNP");
22092192

2210-
pr_cont("\n");
2211-
}
2193+
pr_cont("\n");
22122194
}
2195+
22132196
if (irq_remapping_enabled) {
22142197
pr_info("Interrupt remapping enabled\n");
22152198
if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
@@ -2907,7 +2890,7 @@ static void enable_iommus_vapic(void)
29072890
}
29082891

29092892
if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2910-
!check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {
2893+
!check_feature(FEATURE_GAM_VAPIC)) {
29112894
amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
29122895
return;
29132896
}
@@ -3819,7 +3802,7 @@ int amd_iommu_snp_enable(void)
38193802
return -EINVAL;
38203803
}
38213804

3822-
amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);
3805+
amd_iommu_snp_en = check_feature(FEATURE_SNP);
38233806
if (!amd_iommu_snp_en)
38243807
return -EINVAL;
38253808

drivers/iommu/amd/iommu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ static void amd_iommu_flush_irt_all(struct amd_iommu *iommu)
12951295

12961296
void iommu_flush_all_caches(struct amd_iommu *iommu)
12971297
{
1298-
if (iommu_feature(iommu, FEATURE_IA)) {
1298+
if (check_feature(FEATURE_IA)) {
12991299
amd_iommu_flush_all(iommu);
13001300
} else {
13011301
amd_iommu_flush_dte_all(iommu);
@@ -1639,7 +1639,7 @@ static void set_dte_entry(struct amd_iommu *iommu, u16 devid,
16391639
flags |= DTE_FLAG_IOTLB;
16401640

16411641
if (ppr) {
1642-
if (iommu_feature(iommu, FEATURE_EPHSUP))
1642+
if (check_feature(FEATURE_EPHSUP))
16431643
pte_root |= 1ULL << DEV_ENTRY_PPR;
16441644
}
16451645

0 commit comments

Comments
 (0)