History

iommu: Add APIs for multiple domains per device

Sharing a physical PCI device in a finer-granularity way is becoming a consensus in the industry. IOMMU vendors are also engaging efforts to support such sharing as well as possible. Among the efforts, the capability of support finer-granularity DMA isolation is a common requirement due to the security consideration. With finer-granularity DMA isolation, subsets of a PCI function can be isolated from each others by the IOMMU. As a result, there is a request in software to attach multiple domains to a physical PCI device. One example of such use model is the Intel Scalable IOV [1] [2]. The Intel vt-d 3.0 spec [3] introduces the scalable mode which enables PASID granularity DMA isolation. This adds the APIs to support multiple domains per device. In order to ease the discussions, we call it 'a domain in auxiliary mode' or simply 'auxiliary domain' when multiple domains are attached to a physical device. The APIs include: * iommu_dev_has_feature(dev, IOMMU_DEV_FEAT_AUX) - Detect both IOMMU and PCI endpoint devices supporting the feature (aux-domain here) without the host driver dependency. * iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) - Check the enabling status of the feature (aux-domain here). The aux-domain interfaces are available only if this returns true. * iommu_dev_enable/disable_feature(dev, IOMMU_DEV_FEAT_AUX) - Enable/disable device specific aux-domain feature. * iommu_aux_attach_device(domain, dev) - Attaches @domain to @dev in the auxiliary mode. Multiple domains could be attached to a single device in the auxiliary mode with each domain representing an isolated address space for an assignable subset of the device. * iommu_aux_detach_device(domain, dev) - Detach @domain which has been attached to @dev in the auxiliary mode. * iommu_aux_get_pasid(domain, dev) - Return ID used for finer-granularity DMA translation. For the Intel Scalable IOV usage model, this will be a PASID. The device which supports Scalable IOV needs to write this ID to the device register so that DMA requests could be tagged with a right PASID prefix. This has been updated with the latest proposal from Joerg posted here [5]. Many people involved in discussions of this design. Kevin Tian <kevin.tian@intel.com> Liu Yi L <yi.l.liu@intel.com> Ashok Raj <ashok.raj@intel.com> Sanjay Kumar <sanjay.k.kumar@intel.com> Jacob Pan <jacob.jun.pan@linux.intel.com> Alex Williamson <alex.williamson@redhat.com> Jean-Philippe Brucker <jean-philippe.brucker@arm.com> Joerg Roedel <joro@8bytes.org> and some discussions can be found here [4] [5]. [1] https://software.intel.com/en-us/download/intel-scalable-io-virtualization-technical-specification [2] https://schd.ws/hosted_files/lc32018/00/LC3-SIOV-final.pdf [3] https://software.intel.com/en-us/download/intel-virtualization-technology-for-directed-io-architecture-specification [4] https://lkml.org/lkml/2018/7/26/4 [5] https://www.spinics.net/lists/iommu/msg31874.html

Lu Baolu committed 7 years ago

Commit a3a19591 parent 15ade5d2 files · +166 −0Browse files

Unified · Split

2 files
  1. drivers/iommu/iommu.c +96 −0
  2. include/linux/iommu.h +70 −0
drivers/iommu/iommu.c+96−0
@@ -2039,3 +2039,99 @@ int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) · 2038 unmodified lines
20392039 return 0;
20402040 }
20412041 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2042+
2043+/*
2044+ * Per device IOMMU features.
2045+ */
2046+bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2047+{
2048+ const struct iommu_ops *ops = dev->bus->iommu_ops;
2049+
2050+ if (ops && ops->dev_has_feat)
2051+ return ops->dev_has_feat(dev, feat);
2052+
2053+ return false;
2054+}
2055+EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2056+
2057+int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2058+{
2059+ const struct iommu_ops *ops = dev->bus->iommu_ops;
2060+
2061+ if (ops && ops->dev_enable_feat)
2062+ return ops->dev_enable_feat(dev, feat);
2063+
2064+ return -ENODEV;
2065+}
2066+EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2067+
2068+/*
2069+ * The device drivers should do the necessary cleanups before calling this.
2070+ * For example, before disabling the aux-domain feature, the device driver
2071+ * should detach all aux-domains. Otherwise, this will return -EBUSY.
2072+ */
2073+int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2074+{
2075+ const struct iommu_ops *ops = dev->bus->iommu_ops;
2076+
2077+ if (ops && ops->dev_disable_feat)
2078+ return ops->dev_disable_feat(dev, feat);
2079+
2080+ return -EBUSY;
2081+}
2082+EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2083+
2084+bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2085+{
2086+ const struct iommu_ops *ops = dev->bus->iommu_ops;
2087+
2088+ if (ops && ops->dev_feat_enabled)
2089+ return ops->dev_feat_enabled(dev, feat);
2090+
2091+ return false;
2092+}
2093+EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2094+
2095+/*
2096+ * Aux-domain specific attach/detach.
2097+ *
2098+ * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2099+ * true. Also, as long as domains are attached to a device through this
2100+ * interface, any tries to call iommu_attach_device() should fail
2101+ * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2102+ * This should make us safe against a device being attached to a guest as a
2103+ * whole while there are still pasid users on it (aux and sva).
2104+ */
2105+int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2106+{
2107+ int ret = -ENODEV;
2108+
2109+ if (domain->ops->aux_attach_dev)
2110+ ret = domain->ops->aux_attach_dev(domain, dev);
2111+
2112+ if (!ret)
2113+ trace_attach_device_to_domain(dev);
2114+
2115+ return ret;
2116+}
2117+EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2118+
2119+void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2120+{
2121+ if (domain->ops->aux_detach_dev) {
2122+ domain->ops->aux_detach_dev(domain, dev);
2123+ trace_detach_device_from_domain(dev);
2124+ }
2125+}
2126+EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2127+
2128+int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2129+{
2130+ int ret = -ENODEV;
2131+
2132+ if (domain->ops->aux_get_pasid)
2133+ ret = domain->ops->aux_get_pasid(domain, dev);
2134+
2135+ return ret;
2136+}
2137+EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
include/linux/iommu.h+70−0
@@ -156,6 +156,11 @@ struct iommu_resv_region { · 155 unmodified lines
156156 enum iommu_resv_type type;
157157 };
158158
159+/* Per device IOMMU features */
160+enum iommu_dev_features {
161+ IOMMU_DEV_FEAT_AUX, /* Aux-domain feature */
162+};
163+
159164 #ifdef CONFIG_IOMMU_API
160165
161166 /**
@@ -186,6 +191,11 @@ struct iommu_resv_region { · 24 unmodified lines
186191 * @of_xlate: add OF master IDs to iommu grouping
187192 * @is_attach_deferred: Check if domain attach should be deferred from iommu
188193 * driver init to device driver init (default no)
194+ * @dev_has/enable/disable_feat: per device entries to check/enable/disable
195+ * iommu specific features.
196+ * @dev_feat_enabled: check enabled feature
197+ * @aux_attach/detach_dev: aux-domain specific attach/detach entries.
198+ * @aux_get_pasid: get the pasid given an aux-domain
189199 * @pgsize_bitmap: bitmap of all possible supported page sizes
190200 */
191201 struct iommu_ops {
@@ -230,6 +240,17 @@ struct iommu_ops { · 38 unmodified lines
230240 int (*of_xlate)(struct device *dev, struct of_phandle_args *args);
231241 bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev);
232242
243+ /* Per device IOMMU features */
244+ bool (*dev_has_feat)(struct device *dev, enum iommu_dev_features f);
245+ bool (*dev_feat_enabled)(struct device *dev, enum iommu_dev_features f);
246+ int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f);
247+ int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f);
248+
249+ /* Aux-domain specific attach/detach entries */
250+ int (*aux_attach_dev)(struct iommu_domain *domain, struct device *dev);
251+ void (*aux_detach_dev)(struct iommu_domain *domain, struct device *dev);
252+ int (*aux_get_pasid)(struct iommu_domain *domain, struct device *dev);
253+
233254 unsigned long pgsize_bitmap;
234255 };
235256
@@ -416,6 +437,14 @@ static inline void dev_iommu_fwspec_set(struct device *dev, · 180 unmodified lines
416437 int iommu_probe_device(struct device *dev);
417438 void iommu_release_device(struct device *dev);
418439
440+bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features f);
441+int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features f);
442+int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features f);
443+bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features f);
444+int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev);
445+void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev);
446+int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev);
447+
419448 #else /* CONFIG_IOMMU_API */
420449
421450 struct iommu_ops {};
@@ -700,6 +729,47 @@ const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) · 278 unmodified lines
700729 return NULL;
701730 }
702731
732+static inline bool
733+iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
734+{
735+ return false;
736+}
737+
738+static inline bool
739+iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
740+{
741+ return false;
742+}
743+
744+static inline int
745+iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
746+{
747+ return -ENODEV;
748+}
749+
750+static inline int
751+iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
752+{
753+ return -ENODEV;
754+}
755+
756+static inline int
757+iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
758+{
759+ return -ENODEV;
760+}
761+
762+static inline void
763+iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
764+{
765+}
766+
767+static inline int
768+iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
769+{
770+ return -ENODEV;
771+}
772+
703773 #endif /* CONFIG_IOMMU_API */
704774
705775 #ifdef CONFIG_IOMMU_DEBUGFS

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t