Skip to contentSkip to conversation

ACPI: VIOT: Fix ACS setup

PATCH Superseded Eric Auger 3 participants 3 messages Jun 27, 2022 – Jun 29, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

PATCH 1/1 ACPI: VIOT: Fix ACS setup

Currently acpi_viot_init() gets called after the pci device has been scanned and pci_enable_acs() has been called. So pci_request_acs() fails to be taken into account leading to wrong single iommu group topologies when dealing with multi-function root ports for instance.

We cannot simply move the acpi_viot_init() earlier, similarly as the IORT init because the VIOT parsing relies on the pci scan. However we can detect VIOT is present earlier and in such a case, request ACS. Introduce a new acpi_viot_early_init() routine that allows to call pci_request_acs() before the scan.

Fixes 3cf485540e7b ("ACPI: Add driver for the VIOT table")
---
 drivers/acpi/bus.c        |  1 +
 drivers/acpi/viot.c       | 23 +++++++++++++++++------
 include/linux/acpi_viot.h |  2 ++
 3 files changed, 20 insertions(+), 6 deletions(-)
drivers/acpi/bus.c +1 -0
index 86fa61a21826..906ad8153fd9 100644
@@ -1400,6 +1400,7 @@ static int __init acpi_init(void)
 
 	pci_mmcfg_late_init();
 	acpi_iort_init();
+	acpi_viot_early_init();
 	acpi_hest_init();
 	acpi_ghes_init();
 	acpi_scan_init();
drivers/acpi/viot.c +17 -6
index d2256326c73a..3c1be123e4d6 100644
@@ -248,6 +248,23 @@ static int __init viot_parse_node(const struct acpi_viot_header *hdr)
 	return ret;
 }
 
+/**
+ * acpi_viot_early_init - Test the presence of VIOT and enable ACS
+ *
+ * If the VIOT does exist, ACS must be enabled. This cannot be
+ * done in acpi_viot_init() which is called after the bus scan
+ */
+void __init acpi_viot_early_init(void)
+{
+	acpi_status status;
+	struct acpi_table_header *hdr;
+
+	status = acpi_get_table(ACPI_SIG_VIOT, 0, &hdr);
+	if (!ACPI_FAILURE(status))
+		pci_request_acs();
+	acpi_put_table(hdr);
+}
+
 /**
  * acpi_viot_init - Parse the VIOT table
  *
@@ -319,12 +336,6 @@ static int viot_pci_dev_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
 			epid = ((domain_nr - ep->segment_start) << 16) +
 				dev_id - ep->bdf_start + ep->endpoint_id;
 
-			/*
-			 * If we found a PCI range managed by the viommu, we're
-			 * the one that has to request ACS.
-			 */
-			pci_request_acs();
-
 			return viot_dev_iommu_init(&pdev->dev, ep->viommu,
 						   epid);
 		}
include/linux/acpi_viot.h +2 -0
index 1eb8ee5b0e5f..e58d60f8ff2e 100644
@@ -6,10 +6,12 @@
 #include <linux/acpi.h>
 
 #ifdef CONFIG_ACPI_VIOT
+void __init acpi_viot_early_init(void);
 void __init acpi_viot_init(void);
 int viot_iommu_configure(struct device *dev);
 #else
 static inline void acpi_viot_init(void) {}
+static inline void acpi_viot_early_init(void) {}
 static inline int viot_iommu_configure(struct device *dev)
 {
 	return -ENODEV;
Signature · 1 line
2.35.3
#1 lore raw

Hi Eric,

Quoted 12 lines · Eric Auger

Currently acpi_viot_init() gets called after the pci device has been scanned and pci_enable_acs() has been called. So pci_request_acs() fails to be taken into account leading to wrong single iommu group topologies when dealing with multi-function root ports for instance.

We cannot simply move the acpi_viot_init() earlier, similarly as the IORT init because the VIOT parsing relies on the pci scan. However we can detect VIOT is present earlier and in such a case, request ACS. Introduce a new acpi_viot_early_init() routine that allows to call pci_request_acs() before the scan.

Quoted 3 lines
Fixes 3cf485540e7b ("ACPI: Add driver for the VIOT table")

Thanks for the fix, the patch makes sense and fixes the issue.

I wondered whether we should keep the logic where we only request ACS if an IOMMU is found to manage a PCI range, but I can't see any harm in requesting it regardless (plus there is a precedent with AMD IOMMU). I could imagine some VMM wanting to only put an IOMMU in front of its MMIO devices and leave PCI to roam free, but that seems like a stretch.

There is another issue with the existing code, though: we can't call pci_request_acs() when CONFIG_PCI is disabled because no stub is defined. Could you wrap the call in an #ifdef?

Quoted 31 lines
---
 drivers/acpi/bus.c        |  1 +
 drivers/acpi/viot.c       | 23 +++++++++++++++++------
 include/linux/acpi_viot.h |  2 ++
 3 files changed, 20 insertions(+), 6 deletions(-)
drivers/acpi/bus.c +1 -0
index 86fa61a21826..906ad8153fd9 100644
@@ -1400,6 +1400,7 @@ static int __init acpi_init(void)
 
 	pci_mmcfg_late_init();
 	acpi_iort_init();
+	acpi_viot_early_init();
 	acpi_hest_init();
 	acpi_ghes_init();
 	acpi_scan_init();
drivers/acpi/viot.c +5 -0
index d2256326c73a..3c1be123e4d6 100644
@@ -248,6 +248,23 @@ static int __init viot_parse_node(const struct acpi_viot_header *hdr)
 	return ret;
 }
 
+/**
+ * acpi_viot_early_init - Test the presence of VIOT and enable ACS
+ *
+ * If the VIOT does exist, ACS must be enabled. This cannot be
+ * done in acpi_viot_init() which is called after the bus scan

drivers/acpi/viot.c static int __init viot_parse_node(const struct acpi_viot_header *hdr) · line 265

Quoted 10 lines
drivers/acpi/viot.c +10 -0
+ */
+void __init acpi_viot_early_init(void)
+{
+	acpi_status status;
+	struct acpi_table_header *hdr;
+
+	status = acpi_get_table(ACPI_SIG_VIOT, 0, &hdr);
+	if (!ACPI_FAILURE(status))
+		pci_request_acs();
+	acpi_put_table(hdr);

I'd rather not call acpi_put_table() in case of failure. I know it is handled but it looks fragile and I couldn't find any other user of acpi_get_table() doing this.

Quoted 23 lines
drivers/acpi/viot.c +2 -6
+}
+
 /**
  * acpi_viot_init - Parse the VIOT table
  *
@@ -319,12 +336,6 @@ static int viot_pci_dev_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
 			epid = ((domain_nr - ep->segment_start) << 16) +
 				dev_id - ep->bdf_start + ep->endpoint_id;
 
-			/*
-			 * If we found a PCI range managed by the viommu, we're
-			 * the one that has to request ACS.
-			 */
-			pci_request_acs();
-
 			return viot_dev_iommu_init(&pdev->dev, ep->viommu,
 						   epid);
 		}
include/linux/acpi_viot.h +0 -0
index 1eb8ee5b0e5f..e58d60f8ff2e 100644

include/linux/acpi_viot.h line 14

Quoted 9 lines
include/linux/acpi_viot.h +2 -0
@@ -6,10 +6,12 @@
 #include <linux/acpi.h>
 
 #ifdef CONFIG_ACPI_VIOT
+void __init acpi_viot_early_init(void);
 void __init acpi_viot_init(void);
 int viot_iommu_configure(struct device *dev);
 #else
 static inline void acpi_viot_init(void) {}
+static inline void acpi_viot_early_init(void) {}

nit: different declaration order

Thanks,
Jean

Quoted 6 lines
include/linux/acpi_viot.h +0 -0
 static inline int viot_iommu_configure(struct device *dev)
 {
 	return -ENODEV;
Signature · 1 line
2.35.3
#2 lore raw

Hi Jean

Quoted 19 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 12 lines · Eric Auger

Currently acpi_viot_init() gets called after the pci device has been scanned and pci_enable_acs() has been called. So pci_request_acs() fails to be taken into account leading to wrong single iommu group topologies when dealing with multi-function root ports for instance.

We cannot simply move the acpi_viot_init() earlier, similarly as the IORT init because the VIOT parsing relies on the pci scan. However we can detect VIOT is present earlier and in such a case, request ACS. Introduce a new acpi_viot_early_init() routine that allows to call pci_request_acs() before the scan.

Quoted 3 lines
Fixes 3cf485540e7b ("ACPI: Add driver for the VIOT table")
Quoted 5 lines

Thanks for the fix, the patch makes sense and fixes the issue.

I wondered whether we should keep the logic where we only request ACS if an IOMMU is found to manage a PCI range, but I can't see any harm in requesting it regardless (plus there is a precedent with AMD IOMMU).

Yes that's what I saw too

Quoted 6 lines

I could imagine some VMM wanting to only put an IOMMU in front of its MMIO devices and leave PCI to roam free, but that seems like a stretch.

There is another issue with the existing code, though: we can't call pci_request_acs() when CONFIG_PCI is disabled because no stub is defined. Could you wrap the call in an #ifdef?

sure

Quoted 38 lines
Quoted 38 lines
---
 drivers/acpi/bus.c        |  1 +
 drivers/acpi/viot.c       | 23 +++++++++++++++++------
 include/linux/acpi_viot.h |  2 ++
 3 files changed, 20 insertions(+), 6 deletions(-)
drivers/acpi/bus.c +1 -0
index 86fa61a21826..906ad8153fd9 100644
@@ -1400,6 +1400,7 @@ static int __init acpi_init(void)
 
 	pci_mmcfg_late_init();
 	acpi_iort_init();
+	acpi_viot_early_init();
 	acpi_hest_init();
 	acpi_ghes_init();
 	acpi_scan_init();
drivers/acpi/viot.c +11 -0
index d2256326c73a..3c1be123e4d6 100644
@@ -248,6 +248,23 @@ static int __init viot_parse_node(const struct acpi_viot_header *hdr)
 	return ret;
 }
 
+/**
+ * acpi_viot_early_init - Test the presence of VIOT and enable ACS
+ *
+ * If the VIOT does exist, ACS must be enabled. This cannot be
+ * done in acpi_viot_init() which is called after the bus scan
+ */
+void __init acpi_viot_early_init(void)
+{
+	acpi_status status;
+	struct acpi_table_header *hdr;
+
Quoted 8 lines

drivers/acpi/viot.c static int __init viot_parse_node(const struct acpi_viot_header *hdr) · line 265

Quoted 4 lines
drivers/acpi/viot.c +4 -0
+	status = acpi_get_table(ACPI_SIG_VIOT, 0, &hdr);
+	if (!ACPI_FAILURE(status))
+		pci_request_acs();
+	acpi_put_table(hdr);

I'd rather not call acpi_put_table() in case of failure. I know it is handled but it looks fragile and I couldn't find any other user of acpi_get_table() doing this.

OK

Quoted 26 lines
Quoted 26 lines
drivers/acpi/viot.c +2 -6
+}
+
 /**
  * acpi_viot_init - Parse the VIOT table
  *
@@ -319,12 +336,6 @@ static int viot_pci_dev_iommu_init(struct pci_dev *pdev, u16 dev_id, void *data)
 			epid = ((domain_nr - ep->segment_start) << 16) +
 				dev_id - ep->bdf_start + ep->endpoint_id;
 
-			/*
-			 * If we found a PCI range managed by the viommu, we're
-			 * the one that has to request ACS.
-			 */
-			pci_request_acs();
-
 			return viot_dev_iommu_init(&pdev->dev, ep->viommu,
 						   epid);
 		}
include/linux/acpi_viot.h +0 -0
index 1eb8ee5b0e5f..e58d60f8ff2e 100644
@@ -6,10 +6,12 @@
 #include <linux/acpi.h>
 
Quoted 9 lines

include/linux/acpi_viot.h line 14

Quoted 7 lines
include/linux/acpi_viot.h +2 -0
 #ifdef CONFIG_ACPI_VIOT
+void __init acpi_viot_early_init(void);
 void __init acpi_viot_init(void);
 int viot_iommu_configure(struct device *dev);
 #else
 static inline void acpi_viot_init(void) {}
+static inline void acpi_viot_early_init(void) {}

nit: different declaration order

OK

Thanks

Eric

Quoted 11 lines

Thanks,
Jean

Quoted 6 lines
include/linux/acpi_viot.h +0 -0
 static inline int viot_iommu_configure(struct device *dev)
 {
 	return -ENODEV;
Signature · 1 line
2.35.3