PCI: vmd: Add feature to scan BIOS enumerated devices.
Subsystems (3)
Nirmal Patel
maintainerPATCH v5 1/1 PCI: vmd: Add feature to scan BIOS enumerated devices.
Newer VMD with device ID 0x28c1 has unique settings compared to its predecessor where BIOS enumerates the entire VMD device tree and assigns respective configurations.
VMD configuration BAR0 carries over from GNR legacy VMD as the mechanism to access the configuration space of the devices owned by VMD. The size of this window is fixed at 256 MB, where each function consumes 4 KB and every bus consumes 1 MB.
The shadow and scratchpad registers have been relocated from the VMD configuration space to the VMD MMIO space in VMD BAR4/BAR5, otherwise refers to as MEMBAR2 or MSI-X bar.
VMD MSI-X remapping enable/disable is no longer supported.
All the VMD driver code needs to do is to obtain bus hide range along with shadow register values set by BIOS and perform a bus scan.
The commit also involves small refactoring of vmd_enable_domain function.
---
v5 : Fixing configbar resource end and some spacing issue. v4 : Updating vmd_set_msi_remapping for supported devices only. v3 : Hard code configbar .end to 0xff same as probe.c; Adjust membar2
offset to accommodate more registers in 28C1. Remove redundant IORESOURCE_MEM check in vmd_prepare_offsets_and_bus.
v2 : Using PCI features flag instead of devic ID and fixing corner cases
for vmd_remove_irq.
---
--- drivers/pci/controller/vmd.c | 155 ++++++++++++++++++++++++++++------- include/linux/pci_ids.h | 1 + 2 files changed, 128 insertions(+), 28 deletions(-)
drivers/pci/controller/vmd.c +127 -28
@@ -37,6 +37,12 @@#define MB2_SHADOW_OFFSET 0x2000#define MB2_SHADOW_SIZE 16 +/* DMR BAR4 register offsets */ +#define SHADOW_MEMBAR1_28C1 0x2818/* MEMBAR1 physical address */ +#define SHADOW_MEMBAR2_28C1 0x2820/* MEMBAR2 physical address */ +#define BASE_ID_REG_28C1 0x2840 +#define MEMBAR2_OFFSET_28C1 0x30d0 +enum vmd_features {/* * Device may contain registers which hint the physical location of the
@@ -77,6 +83,15 @@ enum vmd_features {* proper power management of the SoC. */ VMD_FEAT_BIOS_PM_QUIRK = (1 <<5 ), + +/* +* Newer VMD with device ID 0x28c1 has unique settings compared to its +* predecessor where BIOS enumerates the entire VMD device tree and +* stores respective configurations including bus start range and +* shadow registers in VMD MMIO space in VMD BAR4/BAR5, otherwise refers +* to as MEMBAR2 or MSI-X bar. +*/ + VMD_FEAT_USE_BIOS_INFO = (1 << 6), };#define VMD_BIOS_PM_QUIRK_LTR 0x1003/* 3145728 ns */
@@ -142,6 +157,7 @@ struct vmd_dev {u8 first_vec;char *name;int instance; +unsigned long features; };static inline struct vmd_dev *vmd_from_bus (struct pci_bus *bus)
@@ -366,6 +382,9 @@ static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable) {u16 reg; +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) +return ; +pci_read_config_word (vmd->dev,PCI_REG_VMCONFIG , ®); reg = enable ? (reg & ~VMCONFIG_MSI_REMAP ) : (reg |VMCONFIG_MSI_REMAP );
@@ -374,6 +393,9 @@ static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)static void vmd_remove_irq_domain (struct vmd_dev *vmd) { +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) +return ; +/* * Some production BIOS won't enable remapping between soft reboots. * Ensure remapping is restored before unloading the driver.
@@ -393,7 +415,12 @@ static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, unsigned int devfn, int reg, int len) { unsigned int busnr_ecam = bus->number - vmd->busn_start; -u32 offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg); +u32 offset; + +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) + busnr_ecam = bus->number; + + offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg);if (offset + len >=resource_size (&vmd->dev->resource[VMD_CFGBAR ]))return NULL ;
@@ -661,6 +688,46 @@ static int vmd_get_bus_number_start(struct vmd_dev *vmd)return 0 ; } +static int vmd_get_bus_info_from_bar4 (struct vmd_dev *vmd, +resource_size_t *offset1, +resource_size_t *offset2) +{ +u64 phys1, phys2, bar4_2840; +void __iomem *bar4; +u32 base_id; +u8 base_bus; + + bar4 =pci_ioremap_bar (vmd->dev,4 ); +if (!bar4) +return -ENOMEM ; + +/* Read shadow registers for MEMBAR1 and MEMBAR2 physical addresses. */ + phys1 =readq (bar4 +SHADOW_MEMBAR1_28C1 ); + phys2 =readq (bar4 +SHADOW_MEMBAR2_28C1 ); + +/* +* Read and set bus start number from Base ID register. 24-bit Base ID +* register is part of 64-bit shadowed reqid hide range register and +* holds segment, bus, device and function. +*/ + bar4_2840 =readq (bar4 +BASE_ID_REG_28C1 ); + base_id = bar4_2840 &0xFFFFFF ; + base_bus = base_id >>8 ; + vmd->busn_start = base_bus; + +/* Calculate offsets like vmd_get_phys_offsets() does. */ +if (phys1) + *offset1 = vmd->dev->resource[VMD_MEMBAR1 ].start - + (phys1 &PCI_BASE_ADDRESS_MEM_MASK ); +if (phys2) + *offset2 = vmd->dev->resource[VMD_MEMBAR2 ].start - + (phys2 &PCI_BASE_ADDRESS_MEM_MASK ); + +pci_iounmap (vmd->dev, bar4); + +return 0 ; +} +static irqreturn_t vmd_irq (int irq,void *data) {struct vmd_irq_list *irqs = data;
@@ -711,6 +778,52 @@ static int vmd_alloc_irqs(struct vmd_dev *vmd)return 0 ; } +static int vmd_prepare_offsets_and_bus (struct vmd_dev *vmd, +unsigned long features, +resource_size_t *membar2_offset, +resource_size_t *offset1, +resource_size_t *offset2) +{ +int ret; + +/* +* Shadow registers may exist in certain VMD device ids which allow +* guests to correctly assign host physical addresses to the root ports +* and child devices. These registers will either return the host value +* or 0, depending on an enable bit in the VMD device. +* +* For certain VMD devices (i.e. 0x28C1), BIOS places device info +* in BAR4 shadow registers to determine the base bus number and memory +* offsets. +*/ +if (features &VMD_FEAT_USE_BIOS_INFO ) { + *membar2_offset =MEMBAR2_OFFSET_28C1 ; + ret =vmd_get_bus_info_from_bar4 (vmd, offset1, offset2); +if (ret) +return ret; + }else if (features &VMD_FEAT_HAS_MEMBAR_SHADOW ) { + *membar2_offset =MB2_SHADOW_OFFSET +MB2_SHADOW_SIZE ; + ret =vmd_get_phys_offsets (vmd, true, offset1, offset2); +if (ret) +return ret; + }else if (features &VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP ) { + ret =vmd_get_phys_offsets (vmd, false, offset1, offset2); +if (ret) +return ret; + } + +/* +* Certain VMD devices may have a root port configuration option which +* limits the bus range to between 0-127, 128-255, or 224-255. +*/ +if (features &VMD_FEAT_HAS_BUS_RESTRICTIONS ) { + ret =vmd_get_bus_number_start (vmd); +if (ret) +return ret; + } +return 0 ; +} +/* * Since VMD is an aperture to regular PCIe root ports, only allow it to * control features that the OS is allowed to control on the physical PCI bus.
@@ -780,42 +893,24 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)LIST_HEAD (resources);resource_size_t offset[2 ] = {0 };resource_size_t membar2_offset =0x2000 ; +resource_size_t busn_end;struct pci_bus *child;struct pci_dev *dev;int ret; -/* -* Shadow registers may exist in certain VMD device ids which allow -* guests to correctly assign host physical addresses to the root ports -* and child devices. These registers will either return the host value -* or 0, depending on an enable bit in the VMD device. -*/ -if (features &VMD_FEAT_HAS_MEMBAR_SHADOW ) { - membar2_offset =MB2_SHADOW_OFFSET +MB2_SHADOW_SIZE ; - ret =vmd_get_phys_offsets (vmd, true, &offset[0 ], &offset[1 ]); -if (ret) -return ret; - }else if (features &VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP ) { - ret =vmd_get_phys_offsets (vmd, false, &offset[0 ], &offset[1 ]); -if (ret) -return ret; - } - -/* -* Certain VMD devices may have a root port configuration option which -* limits the bus range to between 0-127, 128-255, or 224-255 -*/ -if (features &VMD_FEAT_HAS_BUS_RESTRICTIONS ) { - ret =vmd_get_bus_number_start (vmd); -if (ret) -return ret; - } + ret =vmd_prepare_offsets_and_bus (vmd, features, &membar2_offset, + &offset[0 ], &offset[1 ]); +if (ret) +return ret; +/* Do not let resource[0] end go out of bound.*/ res = &vmd->dev->resource[VMD_CFGBAR ]; + busn_end = vmd->busn_start + (resource_size (res) >>20 ) -1 ; + busn_end =min_t (resource_size_t, busn_end,0xff ); vmd->resources[0 ] = (struct resource ) { .name ="VMD CFGBAR" , .start = vmd->busn_start, - .end = vmd->busn_start + (resource_size (res) >>20 ) -1 , + .end = busn_end, .flags =IORESOURCE_BUS |IORESOURCE_PCI_FIXED , };
@@ -998,6 +1093,7 @@ static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id) vmd->dev = dev; vmd->sysdata.domain =PCI_DOMAIN_NR_NOT_SET ; + vmd->features = features; vmd->instance =ida_alloc (&vmd_instance_ida,GFP_KERNEL );if (vmd->instance <0 )return vmd->instance;
@@ -1114,6 +1210,9 @@ static const struct pci_device_id vmd_ids[] = { .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW | VMD_FEAT_HAS_BUS_RESTRICTIONS | VMD_FEAT_CAN_BYPASS_MSI_REMAP,}, + {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_28C1), + .driver_data =VMD_FEAT_HAS_MEMBAR_SHADOW | + VMD_FEAT_USE_BIOS_INFO,}, {PCI_VDEVICE(INTEL, 0x467f), .driver_data = VMD_FEATS_CLIENT,}, {PCI_VDEVICE(INTEL, 0x4c3d),
include/linux/pci_ids.h +1 -0
@@ -2889,6 +2889,7 @@#define PCI_DEVICE_ID_INTEL_HDA_ICH8 0x284b#define PCI_DEVICE_ID_INTEL_ICH8_6 0x2850#define PCI_DEVICE_ID_INTEL_VMD_28C0 0x28c0 +#define PCI_DEVICE_ID_INTEL_VMD_28C1 0x28c1#define PCI_DEVICE_ID_INTEL_ICH9_0 0x2910#define PCI_DEVICE_ID_INTEL_ICH9_2 0x2912#define PCI_DEVICE_ID_INTEL_ICH9_3 0x2913
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] vmd_remove_irq_domain() returns early for devices with VMD_FEAT_USE_BIOS_INFO, permanently…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] vmd_remove_irq_domain() returns early for devices with VMD_FEAT_USE_BIOS_INFO, permanently leaking the IRQ domain and its fwnode, and causing an exploitable Use-After-Free.
- [High] vmd_domain_reset() accesses incorrect ECAM offsets when VMD_FEAT_USE_BIOS_INFO is set, failing to reset actual VMD bridges and corrupting unrelated MMIO configuration space.
- [Medium] The new vmd_get_bus_info_from_bar4() function accesses MMIO regions in BAR4 at hardcoded offsets without validating that the mapped BAR is sufficiently large. --
commit 371d26de7e81b3dcfea77481a03dd53acb14c51c
Author: Nirmal Patel <[email protected]>
PCI: vmd: Add feature to scan BIOS enumerated devices.
Newer VMD devices with ID 0x28c1 use BIOS enumeration for the device tree. This commit updates the VMD driver to read bus hide ranges and shadow registers from VMD BAR4 to perform bus scans for these devices.
Quoted 4 lines
drivers/pci/controller/vmd.c +0
-0
[ ... ]
drivers/pci/controller/vmd.c static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable) · line 398
Quoted 7 lines
drivers/pci/controller/vmd.c +3
-0@@ -374,6 +393,9 @@ static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)static void vmd_remove_irq_domain (struct vmd_dev *vmd) { +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) +return ; +
[Severity: Critical]
Does this early return bypass irq_domain_remove() and irq_domain_free_fwnode(), permanently leaking the IRQ domain?
In vmd_enable_domain(), vmd_create_irq_domain() is successfully called for these devices because VMD_FEAT_CAN_BYPASS_MSI_REMAP is not set.
If the IRQ domain remains globally registered, could it cause a use-after-free when its struct vmd_dev host_data pointer is automatically freed by devres during device removal?
Quoted 3 lines
drivers/pci/controller/vmd.c +0
-0/* * Some production BIOS won't enable remapping between soft reboots. * Ensure remapping is restored before unloading the driver.
[ ... ]
drivers/pci/controller/vmd.c static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, · line 423
Quoted 11 lines
drivers/pci/controller/vmd.c +6
-1@@ -393,7 +415,12 @@ static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, unsigned int devfn, int reg, int len) { unsigned int busnr_ecam = bus->number - vmd->busn_start; -u32 offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg); +u32 offset; + +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) + busnr_ecam = bus->number; + + offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg);
[Severity: High]
Since the ECAM offset calculation now uses the absolute bus number for VMD_FEAT_USE_BIOS_INFO devices, does vmd_domain_reset() also need to be updated?
Looking at vmd_domain_reset(), it iterates bus from 0 to max_buses and calculates the MMIO address like this:
drivers/pci/controller/vmd.c:vmd_domain_reset() {
...
for (bus = 0; bus < max_buses; bus++) {
for (dev = 0; dev < 32; dev++) {
base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
PCI_DEVFN(dev, 0), 0);
...
}If the VMD buses start at a higher number (e.g., 128), could this hardcoded loop cause vmd_domain_reset() to miss the actual VMD bridges and perform MMIO reads/writes to incorrect offsets in the lower half of the BAR?
Quoted 2 lines
drivers/pci/controller/vmd.c +0
-0if (offset + len >=resource_size (&vmd->dev->resource[VMD_CFGBAR ]))return NULL ;
[ ... ]
Quoted 10 lines
drivers/pci/controller/vmd.c +6
-0@@ -661,6 +688,46 @@ static int vmd_get_bus_number_start(struct vmd_dev *vmd)return 0 ; } +static int vmd_get_bus_info_from_bar4 (struct vmd_dev *vmd, +resource_size_t *offset1, +resource_size_t *offset2) +{ +u64 phys1, phys2, bar4_2840; +void __iomem *bar4;
drivers/pci/controller/vmd.c static int vmd_get_bus_number_start(struct vmd_dev *vmd) · line 706
Quoted 10 lines
drivers/pci/controller/vmd.c +10
-0+u32 base_id; +u8 base_bus; + + bar4 =pci_ioremap_bar (vmd->dev,4 ); +if (!bar4) +return -ENOMEM ; + +/* Read shadow registers for MEMBAR1 and MEMBAR2 physical addresses. */ + phys1 =readq (bar4 +SHADOW_MEMBAR1_28C1 ); + phys2 =readq (bar4 +SHADOW_MEMBAR2_28C1 );
[Severity: Medium]
Could these reads access unmapped kernel memory if the device's BAR4 is smaller than expected?
Since SHADOW_MEMBAR1_28C1 and BASE_ID_REG_28C1 access offsets like 0x2818 and 0x2840, should we verify that pci_resource_len(vmd->dev, 4) is large enough to cover these offsets before performing the readq() calls?
Quoted 5 lines
drivers/pci/controller/vmd.c +5
-0+/* +* Read and set bus start number from Base ID register. 24-bit Base ID +* register is part of 64-bit shadowed reqid hide range register and +* holds segment, bus, device and function. +*/
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Nirmal Patel
maintainerQuoted 40 lines · On Mon, 22 Jun 2026 15:56:31 +0000 [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical]vmd_remove_irq_domain()returns early for devices withVMD_FEAT_USE_BIOS_INFO, permanently leaking the IRQ domain and its fwnode, and causing an exploitable Use-After-Free.
- [High]vmd_domain_reset()accesses incorrect ECAM offsets whenVMD_FEAT_USE_BIOS_INFOis set, failing to reset actual VMD bridges and corrupting unrelated MMIO configuration space.
- [Medium] The newvmd_get_bus_info_from_bar4()function accesses MMIO regions in BAR4 at hardcoded offsets without validating that the mapped BAR is sufficiently large. --commit
371d26de7e81b3dcfea77481a03dd53acb14c51c
Author: Nirmal Patel <[email protected]>PCI: vmd: Add feature to scan BIOS enumerated devices.
Newer VMD devices with ID
0x28c1use BIOS enumeration for the device tree. This commit updates the VMD driver to read bus hide ranges and shadow registers from VMD BAR4 to perform bus scans for these devices.Quoted 4 lines
(quoted diff) +0
-0
b/drivers/pci/controller/vmd.cindexd4ae250d4bc6d..a4e03f10a54d6100644 ---a/drivers/pci/controller/vmd.c
+++b/drivers/pci/controller/vmd.c[ ... ]
drivers/pci/controller/vmd.c static void vmd_set_msi_remapping(struct · line 398
Quoted 7 lines
drivers/pci/controller/vmd.c +3
-0@@ -374,6 +393,9 @@ static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)static void vmd_remove_irq_domain (struct vmd_dev *vmd) { +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) +return ; +[Severity: Critical]
Does this early return bypassirq_domain_remove()andirq_domain_free_fwnode(), permanently leaking the IRQ domain?
Quoted 6 lines
In
vmd_enable_domain(),vmd_create_irq_domain()is successfully called for these devices becauseVMD_FEAT_CAN_BYPASS_MSI_REMAPis not set.If the IRQ domain remains globally registered, could it cause a use-after-free when its struct
vmd_devhost_datapointer is automatically freed by devres during device removal?
Let me fix it.
Quoted 29 lines
Quoted 6 lines
(quoted diff) +0
-0/* * Some production BIOS won't enable remapping betweensoft reboots.
* Ensure remapping is restored before unloading the
driver.[ ... ]
drivers/pci/controller/vmd.c static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, · line 423
Quoted 11 lines
(quoted diff) +0
-0@@ -393,7 +415,12 @@ static void __iomem *vmd_cfg_addr(struct
vmd_dev*vmd, structpci_bus*bus, unsigned int devfn, int reg, int len) {drivers/pci/controller/vmd.c +6
-1unsigned int busnr_ecam = bus->number - vmd->busn_start; -u32 offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg); +u32 offset; + +if (!!(vmd->features &VMD_FEAT_USE_BIOS_INFO )) + busnr_ecam = bus->number; + + offset =PCIE_ECAM_OFFSET (busnr_ecam, devfn, reg);[Severity: High]
Since the ECAM offset calculation now uses the absolute bus number forVMD_FEAT_USE_BIOS_INFOdevices, doesvmd_domain_reset()also need to be updated?Looking at
vmd_domain_reset(), it iterates bus from 0 tomax_busesand calculates the MMIO address like this:
Quoted 8 lines
drivers/pci/controller/vmd.c:vmd_domain_reset() {
...
for (bus = 0; bus < max_buses; bus++) {
for (dev = 0; dev < 32; dev++) {
base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
PCI_DEVFN(dev, 0), 0);
...
}Fair point, i need to change vmd_cfg_addr to avoid reading out of bound. I will also udpate ECAM calculation.
Quoted 50 lines
If the VMD buses start at a higher number (e.g., 128), could this hardcoded loop cause
vmd_domain_reset()to miss the actual VMD bridges and perform MMIO reads/writes to incorrect offsets in the lower half of the BAR?Quoted 2 lines
if (offset + len >=
resource_size(&vmd->dev->resource[VMD_CFGBAR])) return NULL;[ ... ]
Quoted 19 lines
drivers/pci/controller/vmd.c +14
-0@@ -661,6 +688,46 @@ static int vmd_get_bus_number_start(struct vmd_dev *vmd) return 0; } +static int vmd_get_bus_info_from_bar4 (struct vmd_dev *vmd, +resource_size_t *offset1, +resource_size_t *offset2) +{ +u64 phys1, phys2, bar4_2840; +void __iomem *bar4; +u32 base_id; +u8 base_bus; + + bar4 =pci_ioremap_bar (vmd->dev,4 ); +if (!bar4) +return -ENOMEM ; + +/* Read shadow registers for MEMBAR1 and MEMBAR2 physical addresses. */
drivers/pci/controller/vmd.c static int vmd_get_bus_number_start(struct vmd_dev *vmd) · line 706
Quoted 2 lines
drivers/pci/controller/vmd.c +2
-0+ phys1 =readq (bar4 +SHADOW_MEMBAR1_28C1 ); + phys2 =readq (bar4 +SHADOW_MEMBAR2_28C1 );[Severity: Medium]
Could these reads access unmapped kernel memory if the device's BAR4 is smaller than expected?Since
SHADOW_MEMBAR1_28C1andBASE_ID_REG_28C1access offsets like0x2818and0x2840, should we verify that pci_resource_len(vmd->dev,
4) is large enough to cover these offsets before performing thereadq()calls?Quoted 7 lines
(quoted diff) +2
-0+ /* + * Read and set bus start number from Base ID register.24-bit Base ID
(quoted diff) +1
-0+ * register is part of 64-bit shadowed reqid hide rangeregister and
drivers/pci/controller/vmd.c +2
-0+* holds segment, bus, device and function. +*/