Skip to content

Commit 1c38718

Browse files
ashok-rajdwmw2
authored andcommitted
iommu/vt-d: Fix IOMMU lookup for SR-IOV Virtual Functions
The VT-d specification (§8.3.3) says: ‘Virtual Functions’ of a ‘Physical Function’ are under the scope of the same remapping unit as the ‘Physical Function’. The BIOS is not required to list all the possible VFs in the scope tables, and arguably *shouldn't* make any attempt to do so, since there could be a huge number of them. This has been broken basically for ever — the VF is never going to match against a specific unit's scope, so it ends up being assigned to the INCLUDE_ALL IOMMU. Which was always actually correct by coincidence, but now we're looking at Root-Complex integrated devices with SR-IOV support it's going to start being wrong. Fix it to simply use pci_physfn() before doing the lookup for PCI devices. Cc: [email protected] Signed-off-by: Sainath Grandhi <[email protected]> Signed-off-by: Ashok Raj <[email protected]> Signed-off-by: David Woodhouse <[email protected]>
1 parent a909d3e commit 1c38718

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

drivers/iommu/dmar.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ static int dmar_pci_bus_notifier(struct notifier_block *nb,
338338
struct pci_dev *pdev = to_pci_dev(data);
339339
struct dmar_pci_notify_info *info;
340340

341-
/* Only care about add/remove events for physical functions */
341+
/* Only care about add/remove events for physical functions.
342+
* For VFs we actually do the lookup based on the corresponding
343+
* PF in device_to_iommu() anyway. */
342344
if (pdev->is_virtfn)
343345
return NOTIFY_DONE;
344346
if (action != BUS_NOTIFY_ADD_DEVICE &&

drivers/iommu/intel-iommu.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,13 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
892892
return NULL;
893893

894894
if (dev_is_pci(dev)) {
895+
struct pci_dev *pf_pdev;
896+
895897
pdev = to_pci_dev(dev);
898+
/* VFs aren't listed in scope tables; we need to look up
899+
* the PF instead to find the IOMMU. */
900+
pf_pdev = pci_physfn(pdev);
901+
dev = &pf_pdev->dev;
896902
segment = pci_domain_nr(pdev->bus);
897903
} else if (has_acpi_companion(dev))
898904
dev = &ACPI_COMPANION(dev)->dev;
@@ -905,6 +911,13 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
905911
for_each_active_dev_scope(drhd->devices,
906912
drhd->devices_cnt, i, tmp) {
907913
if (tmp == dev) {
914+
/* For a VF use its original BDF# not that of the PF
915+
* which we used for the IOMMU lookup. Strictly speaking
916+
* we could do this for all PCI devices; we only need to
917+
* get the BDF# from the scope table for ACPI matches. */
918+
if (pdev->is_virtfn)
919+
goto got_pdev;
920+
908921
*bus = drhd->devices[i].bus;
909922
*devfn = drhd->devices[i].devfn;
910923
goto out;

0 commit comments

Comments
 (0)