Blame SOURCES/0001-bus-pci-forbid-IOVA-mode-if-IOMMU-address-width-too-.patch

e49aad
From 781bb36add9e43907a16a1303a13808ae53cfa31 Mon Sep 17 00:00:00 2001
e49aad
From: Maxime Coquelin <maxime.coquelin@redhat.com>
e49aad
Date: Fri, 12 Jan 2018 11:22:20 +0100
e49aad
Subject: [PATCH] bus/pci: forbid IOVA mode if IOMMU address width too small
e49aad
e49aad
[ upstream commit 54a328f552ff2e0098c3f96f9e32302675f2bcf4 ]
e49aad
e49aad
Intel VT-d supports different address widths for the IOVAs, from
e49aad
39 bits to 56 bits.
e49aad
e49aad
While recent processors support at least 48 bits, VT-d emulation
e49aad
currently only supports 39 bits. It makes DMA mapping to fail in this
e49aad
case when using VA as IOVA mode, as user-space virtual addresses uses
e49aad
up to 47 bits (see kernel's Documentation/x86/x86_64/mm.txt).
e49aad
e49aad
This patch parses VT-d CAP register value available in sysfs, and
e49aad
forbid VA as IOVA mode if the GAW is 39 bits or unknown.
e49aad
e49aad
Fixes: f37dfab21c98 ("drivers/net: enable IOVA mode for Intel PMDs")
e49aad
e49aad
Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
e49aad
Tested-by: Chas Williams <chas3@att.com>
e49aad
---
e49aad
 drivers/bus/pci/linux/pci.c | 90 ++++++++++++++++++++++++++++++++++++++++-----
e49aad
 1 file changed, 81 insertions(+), 9 deletions(-)
e49aad
e49aad
diff --git a/drivers/bus/pci/linux/pci.c b/drivers/bus/pci/linux/pci.c
e49aad
index ec31216..74deef3 100644
e49aad
--- a/drivers/bus/pci/linux/pci.c
e49aad
+++ b/drivers/bus/pci/linux/pci.c
e49aad
@@ -577,4 +577,80 @@
e49aad
 }
e49aad
 
e49aad
+#if defined(RTE_ARCH_X86)
e49aad
+static bool
e49aad
+pci_one_device_iommu_support_va(struct rte_pci_device *dev)
e49aad
+{
e49aad
+#define VTD_CAP_MGAW_SHIFT	16
e49aad
+#define VTD_CAP_MGAW_MASK	(0x3fULL << VTD_CAP_MGAW_SHIFT)
e49aad
+#define X86_VA_WIDTH 47 /* From Documentation/x86/x86_64/mm.txt */
e49aad
+	struct rte_pci_addr *addr = &dev->addr;
e49aad
+	char filename[PATH_MAX];
e49aad
+	FILE *fp;
e49aad
+	uint64_t mgaw, vtd_cap_reg = 0;
e49aad
+
e49aad
+	snprintf(filename, sizeof(filename),
e49aad
+		 "%s/" PCI_PRI_FMT "/iommu/intel-iommu/cap",
e49aad
+		 rte_pci_get_sysfs_path(), addr->domain, addr->bus, addr->devid,
e49aad
+		 addr->function);
e49aad
+	if (access(filename, F_OK) == -1) {
e49aad
+		/* We don't have an Intel IOMMU, assume VA supported*/
e49aad
+		return true;
e49aad
+	}
e49aad
+
e49aad
+	/* We have an intel IOMMU */
e49aad
+	fp = fopen(filename, "r");
e49aad
+	if (fp == NULL) {
e49aad
+		RTE_LOG(ERR, EAL, "%s(): can't open %s\n", __func__, filename);
e49aad
+		return false;
e49aad
+	}
e49aad
+
e49aad
+	if (fscanf(fp, "%" PRIx64, &vtd_cap_reg) != 1) {
e49aad
+		RTE_LOG(ERR, EAL, "%s(): can't read %s\n", __func__, filename);
e49aad
+		fclose(fp);
e49aad
+		return false;
e49aad
+	}
e49aad
+
e49aad
+	fclose(fp);
e49aad
+
e49aad
+	mgaw = ((vtd_cap_reg & VTD_CAP_MGAW_MASK) >> VTD_CAP_MGAW_SHIFT) + 1;
e49aad
+	if (mgaw < X86_VA_WIDTH)
e49aad
+		return false;
e49aad
+
e49aad
+	return true;
e49aad
+}
e49aad
+#elif defined(RTE_ARCH_PPC_64)
e49aad
+static bool
e49aad
+pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
e49aad
+{
e49aad
+	return false;
e49aad
+}
e49aad
+#else
e49aad
+static bool
e49aad
+pci_one_device_iommu_support_va(__rte_unused struct rte_pci_device *dev)
e49aad
+{
e49aad
+	return true;
e49aad
+}
e49aad
+#endif
e49aad
+
e49aad
+/*
e49aad
+ * All devices IOMMUs support VA as IOVA
e49aad
+ */
e49aad
+static bool
e49aad
+pci_devices_iommu_support_va(void)
e49aad
+{
e49aad
+	struct rte_pci_device *dev = NULL;
e49aad
+	struct rte_pci_driver *drv = NULL;
e49aad
+
e49aad
+	FOREACH_DRIVER_ON_PCIBUS(drv) {
e49aad
+		FOREACH_DEVICE_ON_PCIBUS(dev) {
e49aad
+			if (!rte_pci_match(drv, dev))
e49aad
+				continue;
e49aad
+			if (!pci_one_device_iommu_support_va(dev))
e49aad
+				return false;
e49aad
+		}
e49aad
+	}
e49aad
+	return true;
e49aad
+}
e49aad
+
e49aad
 /*
e49aad
  * Get iommu class of PCI devices on the bus.
e49aad
@@ -587,10 +663,5 @@ enum rte_iova_mode
e49aad
 	bool has_iova_va;
e49aad
 	bool is_bound_uio;
e49aad
-	bool spapr_iommu =
e49aad
-#if defined(RTE_ARCH_PPC_64)
e49aad
-		true;
e49aad
-#else
e49aad
-		false;
e49aad
-#endif
e49aad
+	bool iommu_no_va;
e49aad
 
e49aad
 	is_bound = pci_one_device_is_bound();
e49aad
@@ -600,4 +671,5 @@ enum rte_iova_mode
e49aad
 	has_iova_va = pci_one_device_has_iova_va();
e49aad
 	is_bound_uio = pci_one_device_bound_uio();
e49aad
+	iommu_no_va = !pci_devices_iommu_support_va();
e49aad
 #ifdef VFIO_PRESENT
e49aad
 	is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
e49aad
@@ -606,5 +678,5 @@ enum rte_iova_mode
e49aad
 
e49aad
 	if (has_iova_va && !is_bound_uio && !is_vfio_noiommu_enabled &&
e49aad
-			!spapr_iommu)
e49aad
+			!iommu_no_va)
e49aad
 		return RTE_IOVA_VA;
e49aad
 
e49aad
@@ -615,6 +687,6 @@ enum rte_iova_mode
e49aad
 		if (is_bound_uio)
e49aad
 			RTE_LOG(WARNING, EAL, "few device bound to UIO\n");
e49aad
-		if (spapr_iommu)
e49aad
-			RTE_LOG(WARNING, EAL, "sPAPR IOMMU does not support IOVA as VA\n");
e49aad
+		if (iommu_no_va)
e49aad
+			RTE_LOG(WARNING, EAL, "IOMMU does not support IOVA as VA\n");
e49aad
 	}
e49aad
 
e49aad
-- 
e49aad
1.8.3.1
e49aad