Re: [PATCH v4 2/3] iommu/amd: Do not convert IVMD exclusion range to unity range

From: Vasant Hegde

Date: Fri Nov 20 2026 - 11:52:06 EST




On 11/17/2026 12:36 PM, Sairaj Kodilkar wrote:
> From: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
>
> Original problem was there could be multiple IVMD exclusion entries
> conflicting over a single exclusion range supported in older Linux
> kernel. The fix was first introduced in commit 387caf0b759a ("iommu/amd:
> Treat per-device exclusion ranges as r/w unity-mapped regions") and in
> commit 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
> , where IVMD exclusions are converted to separate unity_map_entry objects
> in pci_seg->unity_map so amd_iommu_get_resv_regions() treated every such
> entry as IOMMU_RESV_DIRECT with R/W.
>
> The driver no longer programs the exclusion base/limit registers and
> relies on the kernel to keep the IOVA ranges named by exclusion IVMDs
> unmapped. Stop forcing exclusion entries to RW-unity. Store the raw
> IVMD flags in struct unity_map_entry instead of a pre-shifted
> protection value, and derive the reserved region from those flags.
> Exclusion entries are now reported as IOMMU_RESV_RESERVED with no
> read/write permission.
>
> Reject unity entries that grant neither IR nor IW with a FW_BUG
> message, and warn about invalid flag combinations and unknown flag bits
>
> Fixes: 0bbe4ced53e3 ("iommu/amd: Fix the overwritten field in IVMD header")
> Cc: Baoquan He <bhe@xxxxxxxxxx>
> Cc: Adrian Huang <ahuang12@xxxxxxxxxx>
> Cc: Jerry Snitselaar <jsnitsel@xxxxxxxxxx>
> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@xxxxxxx>
> Signed-off-by: Sairaj Kodilkar <sarunkod@xxxxxxx>
> Reviewed-by: Adrian Huang <ahuang12@xxxxxxxxxx>
> ---
> drivers/iommu/amd/amd_iommu_types.h | 12 ++++++-----
> drivers/iommu/amd/init.c | 31 ++++++++++++++---------------
> drivers/iommu/amd/iommu.c | 12 +++++++++--
> 3 files changed, 32 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
> index 678ceddd711c..122eac81d4e3 100644
> --- a/drivers/iommu/amd/amd_iommu_types.h
> +++ b/drivers/iommu/amd/amd_iommu_types.h
> @@ -368,9 +368,11 @@
> /* DTE[128:179] | DTE[184:191] */
> #define DTE_DATA2_INTR_MASK ~GENMASK_ULL(55, 52)
>
> -#define IOMMU_PROT_MASK 0x03
> -#define IOMMU_PROT_IR 0x01
> -#define IOMMU_PROT_IW 0x02
> +#define IVMD_FLAG_UNITY_MAP BIT(0)
> +#define IVMD_FLAG_IR BIT(1)
> +#define IVMD_FLAG_IW BIT(2)
> +#define IVMD_FLAG_EXCL_RANGE BIT(3)
> +#define IVMD_FLAG_MASK GENMASK_U32(3, 0)
>
> /* IOMMU capabilities */
> #define IOMMU_CAP_IOTLB 24
> @@ -880,8 +882,8 @@ struct unity_map_entry {
> /* end address to unity map (including) */
> u64 address_end;
>
> - /* required protection */
> - int prot;
> + /* IVMD flags for the entry */
> + u8 flags;
> };
>
> /*
> diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
> index 13c96feed64f..de8e4ba2ccd0 100644
> --- a/drivers/iommu/amd/init.c
> +++ b/drivers/iommu/amd/init.c
> @@ -70,11 +70,6 @@
> #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
> #define IVHD_FLAG_ISOC_EN_MASK 0x08
>
> -#define IVMD_FLAG_EXCL_RANGE 0x08
> -#define IVMD_FLAG_IW 0x04
> -#define IVMD_FLAG_IR 0x02
> -#define IVMD_FLAG_UNITY_MAP 0x01
> -
> #define ACPI_DEVFLAG_INITPASS 0x01
> #define ACPI_DEVFLAG_EXTINT 0x02
> #define ACPI_DEVFLAG_NMI 0x04
> @@ -2636,6 +2631,20 @@ static int __init init_unity_map_range(struct ivmd_header *m,
> struct amd_iommu_pci_seg *pci_seg;
> char *s;
>
> + if ((m->flags & IVMD_FLAG_UNITY_MAP) &&
> + !(m->flags & (IVMD_FLAG_IR | IVMD_FLAG_IW))) {
> + pr_err(FW_BUG "IVMD type %#x devid %#x range %#llx+%#llx: Unity set without IR/IW, ignoring\n",
> + m->type, m->devid, m->range_start, m->range_length);
> + return -EINVAL;
> + }
> +
> + if ((m->flags & IVMD_FLAG_EXCL_RANGE) &&
> + (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_IR | IVMD_FLAG_IW))) {

Spec says IR, IW, and Unity are ignored when ExclusionRange is
set, not that the entry is invalid.


> + pr_err(FW_BUG "IVMD type %#x devid %#x range %#llx+%#llx: Invalid IOMMU flags %#x, ignoring\n",
> + m->type, m->devid, m->range_start, m->range_length, m->flags);
> + return -EINVAL;
> + }
> +
> pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
> if (pci_seg == NULL)
> return -ENOMEM;
> @@ -2665,17 +2674,7 @@ static int __init init_unity_map_range(struct ivmd_header *m,
> }
> e->address_start = PAGE_ALIGN(m->range_start);
> e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
> - e->prot = m->flags >> 1;
> -
> - /*
> - * Treat per-device exclusion ranges as r/w unity-mapped regions
> - * since some buggy BIOSes might lead to the overwritten exclusion
> - * range (exclusion_start and exclusion_length members). This
> - * happens when there are multiple exclusion ranges (IVMD entries)
> - * defined in ACPI table.

I'd say update above comment and keep it. So that we know why we removed
exclusion_range register and used this method.

> - */
> - if (m->flags & IVMD_FLAG_EXCL_RANGE)
> - e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
> + e->flags = m->flags;
>
> DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
> "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
> diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
> index 56262f6b1f70..7edaf99a1ecb 100644
> --- a/drivers/iommu/amd/iommu.c
> +++ b/drivers/iommu/amd/iommu.c
> @@ -3120,12 +3120,20 @@ static void amd_iommu_get_resv_regions(struct device *dev,
> if (devid < entry->devid_start || devid > entry->devid_end)
> continue;
>
> + if (entry->flags & ~IVMD_FLAG_MASK)
> + pr_err("%s: Unknown flags: %#02x for sbdf=%#x\n",
> + __func__, entry->flags & ~IVMD_FLAG_MASK, sbdf);

init_unity_map_range() is the right place to add this check.


-Vasant