Skip to content

Commit a44092e

Browse files
ssuthiku-amdjoergroedel
authored andcommitted
iommu/amd: Use IVHD EFR for early initialization of IOMMU features
IOMMU Extended Feature Register (EFR) is used to communicate the supported features for each IOMMU to the IOMMU driver. This is normally read from the PCI MMIO register offset 0x30, and used by the iommu_feature() helper function. However, there are certain scenarios where the information is needed prior to PCI initialization, and the iommu_feature() function is used prematurely w/o warning. This has caused incorrect initialization of IOMMU. This is the case for the commit 6d39bde ("iommu/amd: Enforce 4k mapping for certain IOMMU data structures") Since, the EFR is also available in the IVHD header, and is available to the driver prior to PCI initialization. Therefore, default to using the IVHD EFR instead. Fixes: 6d39bde ("iommu/amd: Enforce 4k mapping for certain IOMMU data structures") Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Tested-by: Brijesh Singh <brijesh.singh@amd.com> Reviewed-by: Robert Richter <rrichter@amd.com> Link: https://lore.kernel.org/r/20210120135002.2682-1-suravee.suthikulpanit@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 6ee1d74 commit a44092e

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎drivers/iommu/amd/amd_iommu.h‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ static inline bool is_rd890_iommu(struct pci_dev *pdev)
8484
(pdev->device == PCI_DEVICE_ID_RD890_IOMMU);
8585
}
8686

87-
static inline bool iommu_feature(struct amd_iommu *iommu, u64 f)
87+
static inline bool iommu_feature(struct amd_iommu *iommu, u64 mask)
8888
{
89-
if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
90-
return false;
91-
92-
return !!(iommu->features & f);
89+
return !!(iommu->features & mask);
9390
}
9491

9592
static inline u64 iommu_virt_to_phys(void *vaddr)

‎drivers/iommu/amd/amd_iommu_types.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@
387387
#define IOMMU_CAP_NPCACHE 26
388388
#define IOMMU_CAP_EFR 27
389389

390+
/* IOMMU IVINFO */
391+
#define IOMMU_IVINFO_OFFSET 36
392+
#define IOMMU_IVINFO_EFRSUP BIT(0)
393+
390394
/* IOMMU Feature Reporting Field (for IVHD type 10h */
391395
#define IOMMU_FEAT_GASUP_SHIFT 6
392396

‎drivers/iommu/amd/init.c‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ static void init_device_table_dma(void);
257257

258258
static bool amd_iommu_pre_enabled = true;
259259

260+
static u32 amd_iommu_ivinfo __initdata;
261+
260262
bool translation_pre_enabled(struct amd_iommu *iommu)
261263
{
262264
return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
@@ -296,6 +298,18 @@ int amd_iommu_get_num_iommus(void)
296298
return amd_iommus_present;
297299
}
298300

301+
/*
302+
* For IVHD type 0x11/0x40, EFR is also available via IVHD.
303+
* Default to IVHD EFR since it is available sooner
304+
* (i.e. before PCI init).
305+
*/
306+
static void __init early_iommu_features_init(struct amd_iommu *iommu,
307+
struct ivhd_header *h)
308+
{
309+
if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP)
310+
iommu->features = h->efr_reg;
311+
}
312+
299313
/* Access to l1 and l2 indexed register spaces */
300314

301315
static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
@@ -1577,6 +1591,9 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
15771591

15781592
if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
15791593
amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1594+
1595+
early_iommu_features_init(iommu, h);
1596+
15801597
break;
15811598
default:
15821599
return -EINVAL;
@@ -1770,6 +1787,35 @@ static const struct attribute_group *amd_iommu_groups[] = {
17701787
NULL,
17711788
};
17721789

1790+
/*
1791+
* Note: IVHD 0x11 and 0x40 also contains exact copy
1792+
* of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1793+
* Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1794+
*/
1795+
static void __init late_iommu_features_init(struct amd_iommu *iommu)
1796+
{
1797+
u64 features;
1798+
1799+
if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1800+
return;
1801+
1802+
/* read extended feature bits */
1803+
features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1804+
1805+
if (!iommu->features) {
1806+
iommu->features = features;
1807+
return;
1808+
}
1809+
1810+
/*
1811+
* Sanity check and warn if EFR values from
1812+
* IVHD and MMIO conflict.
1813+
*/
1814+
if (features != iommu->features)
1815+
pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx\n).",
1816+
features, iommu->features);
1817+
}
1818+
17731819
static int __init iommu_init_pci(struct amd_iommu *iommu)
17741820
{
17751821
int cap_ptr = iommu->cap_ptr;
@@ -1789,8 +1835,7 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)
17891835
if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
17901836
amd_iommu_iotlb_sup = false;
17911837

1792-
/* read extended feature bits */
1793-
iommu->features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1838+
late_iommu_features_init(iommu);
17941839

17951840
if (iommu_feature(iommu, FEATURE_GT)) {
17961841
int glxval;
@@ -2607,6 +2652,11 @@ static void __init free_dma_resources(void)
26072652
free_unity_maps();
26082653
}
26092654

2655+
static void __init ivinfo_init(void *ivrs)
2656+
{
2657+
amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2658+
}
2659+
26102660
/*
26112661
* This is the hardware init function for AMD IOMMU in the system.
26122662
* This function is called either from amd_iommu_init or from the interrupt
@@ -2661,6 +2711,8 @@ static int __init early_amd_iommu_init(void)
26612711
if (ret)
26622712
goto out;
26632713

2714+
ivinfo_init(ivrs_base);
2715+
26642716
amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
26652717
DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
26662718

0 commit comments

Comments
 (0)