Patchwork [RFC,1/5] iommu: Add iommu_device_group callback and iommu_group sysfs entry

login
register
mail settings
Submitter Alex Williamson
Date Sept. 1, 2011, 7:50 p.m.
Message ID <20110901195030.2391.79018.stgit@s20.home>
Download mbox | patch
Permalink /patch/112969/
State New
Headers show

Comments

Alex Williamson - Sept. 1, 2011, 7:50 p.m.
An IOMMU group is a set of devices for which the IOMMU cannot
distinguish transactions.  For PCI devices, a group often occurs
when a PCI bridge is involved.  Transactions from any device
behind the bridge appear to be sourced from the bridge itself.
We leave it to the IOMMU driver to define the grouping restraints
for their platform.

Using this new interface, the group for a device can be retrieved
using the iommu_device_group() callback.  Users will compare the
value returned against the value returned for other devices to
determine whether they are part of the same group.  Devices with
no group are not translated by the IOMMU.  There should be no
expectations about the group numbers as they may be arbitrarily
assigned by the IOMMU driver and may not be persistent across boots.

We also provide a sysfs interface to the group numbers here so
that user space can understand IOMMU dependencies between devices
for managing safe, user space drivers.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---

 drivers/base/iommu.c  |   51 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/iommu.h |    6 ++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

Patch

diff --git a/drivers/base/iommu.c b/drivers/base/iommu.c
index 6e6b6a1..566aa17 100644
--- a/drivers/base/iommu.c
+++ b/drivers/base/iommu.c
@@ -17,20 +17,63 @@ 
  */
 
 #include <linux/bug.h>
+#include <linux/device.h>
 #include <linux/types.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/iommu.h>
+#include <linux/pci.h>
 
 static struct iommu_ops *iommu_ops;
 
+static ssize_t show_iommu_group(struct device *dev,
+				struct device_attribute *attr, char *buf)
+{
+	unsigned int groupid;
+
+	if (iommu_device_group(dev, &groupid))
+		return 0;
+
+	return sprintf(buf, "%u", groupid);
+}
+static DEVICE_ATTR(iommu_group, S_IRUGO, show_iommu_group, NULL);
+
+static int add_iommu_group(struct device *dev, void *unused)
+{
+	unsigned int groupid;
+
+	if (iommu_device_group(dev, &groupid) == 0)
+		return device_create_file(dev, &dev_attr_iommu_group);
+
+	return 0;
+}
+
+static int device_notifier(struct notifier_block *nb,
+			   unsigned long action, void *data)
+{
+	struct device *dev = data;
+
+	if (action == BUS_NOTIFY_ADD_DEVICE)
+		return add_iommu_group(dev, NULL);
+
+	return 0;
+}
+
+static struct notifier_block device_nb = {
+	.notifier_call = device_notifier,
+};
+
 void register_iommu(struct iommu_ops *ops)
 {
 	if (iommu_ops)
 		BUG();
 
 	iommu_ops = ops;
+
+	/* FIXME - non-PCI, really want for_each_bus() */
+	bus_register_notifier(&pci_bus_type, &device_nb);
+	bus_for_each_dev(&pci_bus_type, NULL, NULL, add_iommu_group);
 }
 
 bool iommu_found(void)
@@ -94,6 +137,14 @@  int iommu_domain_has_cap(struct iommu_domain *domain,
 }
 EXPORT_SYMBOL_GPL(iommu_domain_has_cap);
 
+int iommu_device_group(struct device *dev, unsigned int *groupid)
+{
+	if (iommu_ops->device_group)
+		return iommu_ops->device_group(dev, groupid);
+	return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(iommu_device_group);
+
 int iommu_map(struct iommu_domain *domain, unsigned long iova,
 	      phys_addr_t paddr, int gfp_order, int prot)
 {
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 0a2ba40..e3a53ed 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -45,6 +45,7 @@  struct iommu_ops {
 				    unsigned long iova);
 	int (*domain_has_cap)(struct iommu_domain *domain,
 			      unsigned long cap);
+	int (*device_group)(struct device *dev, unsigned int *groupid);
 };
 
 #ifdef CONFIG_IOMMU_API
@@ -65,6 +66,7 @@  extern phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain,
 				      unsigned long iova);
 extern int iommu_domain_has_cap(struct iommu_domain *domain,
 				unsigned long cap);
+extern int iommu_device_group(struct device *dev, unsigned int *groupid);
 
 #else /* CONFIG_IOMMU_API */
 
@@ -121,6 +123,10 @@  static inline int domain_has_cap(struct iommu_domain *domain,
 	return 0;
 }
 
+static inline int iommu_device_group(struct device *dev, unsigned int *groupid);
+{
+	return -ENODEV;
+}
 #endif /* CONFIG_IOMMU_API */
 
 #endif /* __LINUX_IOMMU_H */