Skip to content

Commit 2ccdd9f

Browse files
committed
Merge tag 'iommu-fixes-v5.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu
Pull iommu fixes from Joerg Roedel: - three Intel VT-d fixes to fix address handling on 32bit, fix a NULL pointer dereference bug and serialize a hardware register access as required by the VT-d spec. - two patches for AMD IOMMU to force AMD GPUs into translation mode when memory encryption is active and disallow using IOMMUv2 functionality. This makes the AMDGPU driver work when memory encryption is active. - two more fixes for AMD IOMMU to fix updating the Interrupt Remapping Table Entries. - MAINTAINERS file update for the Qualcom IOMMU driver. * tag 'iommu-fixes-v5.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: iommu/vt-d: Handle 36bit addressing for x86-32 iommu/amd: Do not use IOMMUv2 functionality when SME is active iommu/amd: Do not force direct mapping when SME is active iommu/amd: Use cmpxchg_double() when updating 128-bit IRTE iommu/amd: Restore IRTE.RemapEn bit after programming IRTE iommu/vt-d: Fix NULL pointer dereference in dev_iommu_priv_set() iommu/vt-d: Serialize IOMMU GCMD register modifications MAINTAINERS: Update QUALCOMM IOMMU after Arm SMMU drivers move
2 parents 015b315 + 29aaebb commit 2ccdd9f

File tree

7 files changed

+119
-63
lines changed

7 files changed

+119
-63
lines changed

MAINTAINERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14388,7 +14388,7 @@ M: Rob Clark <[email protected]>
1438814388
1438914389
1439014390
S: Maintained
14391-
F: drivers/iommu/qcom_iommu.c
14391+
F: drivers/iommu/arm/arm-smmu/qcom_iommu.c
1439214392

1439314393
QUALCOMM IPCC MAILBOX DRIVER
1439414394
M: Manivannan Sadhasivam <[email protected]>

drivers/iommu/amd/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ config AMD_IOMMU
1010
select IOMMU_API
1111
select IOMMU_IOVA
1212
select IOMMU_DMA
13-
depends on X86_64 && PCI && ACPI
13+
depends on X86_64 && PCI && ACPI && HAVE_CMPXCHG_DOUBLE
1414
help
1515
With this option you can enable support for AMD IOMMU hardware in
1616
your system. An IOMMU is a hardware component which provides

drivers/iommu/amd/init.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,14 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
15111511
iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
15121512
else
15131513
iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1514-
if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1514+
1515+
/*
1516+
* Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1517+
* GAM also requires GA mode. Therefore, we need to
1518+
* check cmpxchg16b support before enabling it.
1519+
*/
1520+
if (!boot_cpu_has(X86_FEATURE_CX16) ||
1521+
((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
15151522
amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
15161523
break;
15171524
case 0x11:
@@ -1520,8 +1527,18 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
15201527
iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
15211528
else
15221529
iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1523-
if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1530+
1531+
/*
1532+
* Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1533+
* XT, GAM also requires GA mode. Therefore, we need to
1534+
* check cmpxchg16b support before enabling them.
1535+
*/
1536+
if (!boot_cpu_has(X86_FEATURE_CX16) ||
1537+
((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
15241538
amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1539+
break;
1540+
}
1541+
15251542
/*
15261543
* Note: Since iommu_update_intcapxt() leverages
15271544
* the IOMMU MMIO access to MSI capability block registers

drivers/iommu/amd/iommu.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,12 @@ static int amd_iommu_def_domain_type(struct device *dev)
26592659
if (!dev_data)
26602660
return 0;
26612661

2662-
if (dev_data->iommu_v2)
2662+
/*
2663+
* Do not identity map IOMMUv2 capable devices when memory encryption is
2664+
* active, because some of those devices (AMD GPUs) don't have the
2665+
* encryption bit in their DMA-mask and require remapping.
2666+
*/
2667+
if (!mem_encrypt_active() && dev_data->iommu_v2)
26632668
return IOMMU_DOMAIN_IDENTITY;
26642669

26652670
return 0;
@@ -3292,6 +3297,7 @@ static int alloc_irq_index(u16 devid, int count, bool align,
32923297
static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
32933298
struct amd_ir_data *data)
32943299
{
3300+
bool ret;
32953301
struct irq_remap_table *table;
32963302
struct amd_iommu *iommu;
32973303
unsigned long flags;
@@ -3309,10 +3315,18 @@ static int modify_irte_ga(u16 devid, int index, struct irte_ga *irte,
33093315

33103316
entry = (struct irte_ga *)table->table;
33113317
entry = &entry[index];
3312-
entry->lo.fields_remap.valid = 0;
3313-
entry->hi.val = irte->hi.val;
3314-
entry->lo.val = irte->lo.val;
3315-
entry->lo.fields_remap.valid = 1;
3318+
3319+
ret = cmpxchg_double(&entry->lo.val, &entry->hi.val,
3320+
entry->lo.val, entry->hi.val,
3321+
irte->lo.val, irte->hi.val);
3322+
/*
3323+
* We use cmpxchg16 to atomically update the 128-bit IRTE,
3324+
* and it cannot be updated by the hardware or other processors
3325+
* behind us, so the return value of cmpxchg16 should be the
3326+
* same as the old value.
3327+
*/
3328+
WARN_ON(!ret);
3329+
33163330
if (data)
33173331
data->ref = entry;
33183332

@@ -3850,6 +3864,7 @@ int amd_iommu_deactivate_guest_mode(void *data)
38503864
struct amd_ir_data *ir_data = (struct amd_ir_data *)data;
38513865
struct irte_ga *entry = (struct irte_ga *) ir_data->entry;
38523866
struct irq_cfg *cfg = ir_data->cfg;
3867+
u64 valid = entry->lo.fields_remap.valid;
38533868

38543869
if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) ||
38553870
!entry || !entry->lo.fields_vapic.guest_mode)
@@ -3858,6 +3873,7 @@ int amd_iommu_deactivate_guest_mode(void *data)
38583873
entry->lo.val = 0;
38593874
entry->hi.val = 0;
38603875

3876+
entry->lo.fields_remap.valid = valid;
38613877
entry->lo.fields_remap.dm = apic->irq_dest_mode;
38623878
entry->lo.fields_remap.int_type = apic->irq_delivery_mode;
38633879
entry->hi.fields.vector = cfg->vector;

drivers/iommu/amd/iommu_v2.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,13 @@ int amd_iommu_init_device(struct pci_dev *pdev, int pasids)
737737

738738
might_sleep();
739739

740+
/*
741+
* When memory encryption is active the device is likely not in a
742+
* direct-mapped domain. Forbid using IOMMUv2 functionality for now.
743+
*/
744+
if (mem_encrypt_active())
745+
return -ENODEV;
746+
740747
if (!amd_iommu_v2_supported())
741748
return -ENODEV;
742749

drivers/iommu/intel/iommu.c

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,29 @@ static inline unsigned int level_to_offset_bits(int level)
123123
return (level - 1) * LEVEL_STRIDE;
124124
}
125125

126-
static inline int pfn_level_offset(unsigned long pfn, int level)
126+
static inline int pfn_level_offset(u64 pfn, int level)
127127
{
128128
return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
129129
}
130130

131-
static inline unsigned long level_mask(int level)
131+
static inline u64 level_mask(int level)
132132
{
133-
return -1UL << level_to_offset_bits(level);
133+
return -1ULL << level_to_offset_bits(level);
134134
}
135135

136-
static inline unsigned long level_size(int level)
136+
static inline u64 level_size(int level)
137137
{
138-
return 1UL << level_to_offset_bits(level);
138+
return 1ULL << level_to_offset_bits(level);
139139
}
140140

141-
static inline unsigned long align_to_level(unsigned long pfn, int level)
141+
static inline u64 align_to_level(u64 pfn, int level)
142142
{
143143
return (pfn + level_size(level) - 1) & level_mask(level);
144144
}
145145

146146
static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
147147
{
148-
return 1 << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
148+
return 1UL << min_t(int, (lvl - 1) * LEVEL_STRIDE, MAX_AGAW_PFN_WIDTH);
149149
}
150150

151151
/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
@@ -364,7 +364,6 @@ static int iommu_skip_te_disable;
364364
int intel_iommu_gfx_mapped;
365365
EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);
366366

367-
#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
368367
#define DEFER_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-2))
369368
struct device_domain_info *get_domain_info(struct device *dev)
370369
{
@@ -374,8 +373,7 @@ struct device_domain_info *get_domain_info(struct device *dev)
374373
return NULL;
375374

376375
info = dev_iommu_priv_get(dev);
377-
if (unlikely(info == DUMMY_DEVICE_DOMAIN_INFO ||
378-
info == DEFER_DEVICE_DOMAIN_INFO))
376+
if (unlikely(info == DEFER_DEVICE_DOMAIN_INFO))
379377
return NULL;
380378

381379
return info;
@@ -742,11 +740,6 @@ struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus,
742740
return &context[devfn];
743741
}
744742

745-
static int iommu_dummy(struct device *dev)
746-
{
747-
return dev_iommu_priv_get(dev) == DUMMY_DEVICE_DOMAIN_INFO;
748-
}
749-
750743
static bool attach_deferred(struct device *dev)
751744
{
752745
return dev_iommu_priv_get(dev) == DEFER_DEVICE_DOMAIN_INFO;
@@ -779,6 +772,53 @@ is_downstream_to_pci_bridge(struct device *dev, struct device *bridge)
779772
return false;
780773
}
781774

775+
static bool quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
776+
{
777+
struct dmar_drhd_unit *drhd;
778+
u32 vtbar;
779+
int rc;
780+
781+
/* We know that this device on this chipset has its own IOMMU.
782+
* If we find it under a different IOMMU, then the BIOS is lying
783+
* to us. Hope that the IOMMU for this device is actually
784+
* disabled, and it needs no translation...
785+
*/
786+
rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
787+
if (rc) {
788+
/* "can't" happen */
789+
dev_info(&pdev->dev, "failed to run vt-d quirk\n");
790+
return false;
791+
}
792+
vtbar &= 0xffff0000;
793+
794+
/* we know that the this iommu should be at offset 0xa000 from vtbar */
795+
drhd = dmar_find_matched_drhd_unit(pdev);
796+
if (!drhd || drhd->reg_base_addr - vtbar != 0xa000) {
797+
pr_warn_once(FW_BUG "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n");
798+
add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
799+
return true;
800+
}
801+
802+
return false;
803+
}
804+
805+
static bool iommu_is_dummy(struct intel_iommu *iommu, struct device *dev)
806+
{
807+
if (!iommu || iommu->drhd->ignored)
808+
return true;
809+
810+
if (dev_is_pci(dev)) {
811+
struct pci_dev *pdev = to_pci_dev(dev);
812+
813+
if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
814+
pdev->device == PCI_DEVICE_ID_INTEL_IOAT_SNB &&
815+
quirk_ioat_snb_local_iommu(pdev))
816+
return true;
817+
}
818+
819+
return false;
820+
}
821+
782822
struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
783823
{
784824
struct dmar_drhd_unit *drhd = NULL;
@@ -788,7 +828,7 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
788828
u16 segment = 0;
789829
int i;
790830

791-
if (!dev || iommu_dummy(dev))
831+
if (!dev)
792832
return NULL;
793833

794834
if (dev_is_pci(dev)) {
@@ -805,7 +845,7 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
805845
dev = &ACPI_COMPANION(dev)->dev;
806846

807847
rcu_read_lock();
808-
for_each_active_iommu(iommu, drhd) {
848+
for_each_iommu(iommu, drhd) {
809849
if (pdev && segment != drhd->segment)
810850
continue;
811851

@@ -841,6 +881,9 @@ struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
841881
}
842882
iommu = NULL;
843883
out:
884+
if (iommu_is_dummy(iommu, dev))
885+
iommu = NULL;
886+
844887
rcu_read_unlock();
845888

846889
return iommu;
@@ -2447,7 +2490,7 @@ struct dmar_domain *find_domain(struct device *dev)
24472490
{
24482491
struct device_domain_info *info;
24492492

2450-
if (unlikely(attach_deferred(dev) || iommu_dummy(dev)))
2493+
if (unlikely(attach_deferred(dev)))
24512494
return NULL;
24522495

24532496
/* No lock here, assumes no domain exit in normal case */
@@ -3989,35 +4032,6 @@ static void __init iommu_exit_mempool(void)
39894032
iova_cache_put();
39904033
}
39914034

3992-
static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3993-
{
3994-
struct dmar_drhd_unit *drhd;
3995-
u32 vtbar;
3996-
int rc;
3997-
3998-
/* We know that this device on this chipset has its own IOMMU.
3999-
* If we find it under a different IOMMU, then the BIOS is lying
4000-
* to us. Hope that the IOMMU for this device is actually
4001-
* disabled, and it needs no translation...
4002-
*/
4003-
rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
4004-
if (rc) {
4005-
/* "can't" happen */
4006-
dev_info(&pdev->dev, "failed to run vt-d quirk\n");
4007-
return;
4008-
}
4009-
vtbar &= 0xffff0000;
4010-
4011-
/* we know that the this iommu should be at offset 0xa000 from vtbar */
4012-
drhd = dmar_find_matched_drhd_unit(pdev);
4013-
if (!drhd || drhd->reg_base_addr - vtbar != 0xa000) {
4014-
pr_warn_once(FW_BUG "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n");
4015-
add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
4016-
dev_iommu_priv_set(&pdev->dev, DUMMY_DEVICE_DOMAIN_INFO);
4017-
}
4018-
}
4019-
DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
4020-
40214035
static void __init init_no_remapping_devices(void)
40224036
{
40234037
struct dmar_drhd_unit *drhd;
@@ -4049,12 +4063,8 @@ static void __init init_no_remapping_devices(void)
40494063
/* This IOMMU has *only* gfx devices. Either bypass it or
40504064
set the gfx_mapped flag, as appropriate */
40514065
drhd->gfx_dedicated = 1;
4052-
if (!dmar_map_gfx) {
4066+
if (!dmar_map_gfx)
40534067
drhd->ignored = 1;
4054-
for_each_active_dev_scope(drhd->devices,
4055-
drhd->devices_cnt, i, dev)
4056-
dev_iommu_priv_set(dev, DUMMY_DEVICE_DOMAIN_INFO);
4057-
}
40584068
}
40594069
}
40604070

drivers/iommu/intel/irq_remapping.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,18 @@ static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
508508

509509
/* Enable interrupt-remapping */
510510
iommu->gcmd |= DMA_GCMD_IRE;
511-
iommu->gcmd &= ~DMA_GCMD_CFI; /* Block compatibility-format MSIs */
512511
writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
513-
514512
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
515513
readl, (sts & DMA_GSTS_IRES), sts);
516514

515+
/* Block compatibility-format MSIs */
516+
if (sts & DMA_GSTS_CFIS) {
517+
iommu->gcmd &= ~DMA_GCMD_CFI;
518+
writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
519+
IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
520+
readl, !(sts & DMA_GSTS_CFIS), sts);
521+
}
522+
517523
/*
518524
* With CFI clear in the Global Command register, we should be
519525
* protected from dangerous (i.e. compatibility) interrupts

0 commit comments

Comments
 (0)