Add support for ACPI VIOT
Subsystems (11)
v5 cover Add support for ACPI VIOT
Add a driver for the ACPI VIOT table, which provides topology information for para-virtual IOMMUs. Enable virtio-iommu on non-devicetree platforms, including x86.
Since v4 [1]:
* Fixes (comments, wrong argument, unused variable)
* Removed patch 5 that wrongly moved set_dma_ops(dev, NULL) into dma-iommu. The simplification of limit parameters for x86 callers is now in patch 4.
* Release ACPI table after parsing
* Added review and tested tags, thanks for all the feedback!
You can find a QEMU implementation at [2], with extra support for testing all VIOT nodes including MMIO-based endpoints and IOMMU. This series is at [3].
[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://jpbrucker.net/git/qemu/log/?h=virtio-iommu/acpi
[3] https://jpbrucker.net/git/linux/log/?h=virtio-iommu/acpi
Jean-Philippe Brucker (5):
ACPI: arm64: Move DMA setup operations out of IORT
ACPI: Move IOMMU setup code out of IORT
ACPI: Add driver for the VIOT table iommu/dma: Pass address limit rather than size to
iommu_setup_dma_ops()
iommu/virtio: Enable x86 support
drivers/acpi/Kconfig | 3 + drivers/iommu/Kconfig | 4 +- drivers/acpi/Makefile | 2 + drivers/acpi/arm64/Makefile | 1 + include/acpi/acpi_bus.h | 3 + include/linux/acpi.h | 3 + include/linux/acpi_iort.h | 14 +- include/linux/acpi_viot.h | 19 ++ include/linux/dma-iommu.h | 4 +- arch/arm64/mm/dma-mapping.c | 2 +- drivers/acpi/arm64/dma.c | 50 +++++ drivers/acpi/arm64/iort.c | 128 ++---------- drivers/acpi/bus.c | 2 + drivers/acpi/scan.c | 78 +++++++- drivers/acpi/viot.c | 366 +++++++++++++++++++++++++++++++++++ drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/dma-iommu.c | 13 +- drivers/iommu/intel/iommu.c | 5 +- drivers/iommu/virtio-iommu.c | 11 ++ MAINTAINERS | 8 + 20 files changed, 581 insertions(+), 137 deletions(-) create mode 100644 include/linux/acpi_viot.h create mode 100644 drivers/acpi/arm64/dma.c create mode 100644 drivers/acpi/viot.c
Signature · 6 lines
2.32.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v5 1/5 ACPI: arm64: Move DMA setup operations out of IORT
Extract generic DMA setup code out of IORT, so it can be reused by VIOT. Keep it in drivers/acpi/arm64 for now, since it could break x86 platforms that haven't run this code so far, if they have invalid tables.
--- drivers/acpi/arm64/Makefile | 1 + include/linux/acpi.h | 3 +++ include/linux/acpi_iort.h | 6 ++--- drivers/acpi/arm64/dma.c | 50 ++++++++++++++++++++++++++++++++++ drivers/acpi/arm64/iort.c | 54 ++++++------------------------------- drivers/acpi/scan.c | 2 +- 6 files changed, 66 insertions(+), 50 deletions(-) create mode 100644 drivers/acpi/arm64/dma.c
drivers/acpi/arm64/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_ACPI_IORT) += iort.o obj-$(CONFIG_ACPI_GTDT) += gtdt.o +obj-y += dma.o
include/linux/acpi.h +3 -0
@@ -259,9 +259,12 @@ void acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa);#ifdef CONFIG_ARM64 void acpi_numa_gicc_affinity_init (struct acpi_srat_gicc_affinity *pa); +void acpi_arch_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size);#else static inline void acpi_numa_gicc_affinity_init (struct acpi_srat_gicc_affinity *pa) { } +static inline void +acpi_arch_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size) { }#endif int acpi_numa_memory_affinity_init (struct acpi_srat_mem_affinity *ma);
include/linux/acpi_iort.h +3 -3
@@ -34,7 +34,7 @@ struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,void acpi_configure_pmsi_domain (struct device *dev);int iort_pmsi_get_dev_id (struct device *dev,u32 *dev_id);/* IOMMU interface */ -void iort_dma_setup (struct device *dev,u64 *dma_addr,u64 *size); +int iort_dma_get_ranges (struct device *dev,u64 *size);const struct iommu_ops *iort_iommu_configure_id (struct device *dev,const u32 *id_in);int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head);
@@ -48,8 +48,8 @@ static inline struct irq_domain *iort_get_device_domain( {return NULL ; }static inline void acpi_configure_pmsi_domain (struct device *dev) { }/* IOMMU interface */ -static inline void iort_dma_setup (struct device *dev,u64 *dma_addr, -u64 *size) { } +static inline int iort_dma_get_ranges (struct device *dev,u64 *size) +{return -ENODEV ; }static inline const struct iommu_ops *iort_iommu_configure_id (struct device *dev,const u32 *id_in) {return NULL ; }
drivers/acpi/arm64/dma.c new file +50 -0
50 lines not on this page · raw message
drivers/acpi/arm64/iort.c +8 -46
64 lines not on this page · raw message
drivers/acpi/scan.c +1 -1
@@ -1537,7 +1537,7 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,return 0 ; } -iort_dma_setup (dev, &dma_addr, &size); +acpi_arch_dma_setup (dev, &dma_addr, &size); iommu =iort_iommu_configure_id (dev, input_id);if (PTR_ERR (iommu) == -EPROBE_DEFER )
Signature · 6 lines
2.32.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v5 1/5] ACPI: arm64: Move DMA setup operations out of IORT
Quoted 4 lines · Jean-Philippe Brucker
Extract generic DMA setup code out of IORT, so it can be reused by VIOT. Keep it in
drivers/acpi/arm64for now, since it could break x86 platforms that haven't run this code so far, if they have invalid tables.
Quoted 201 lines
Reviewed-by Eric Auger <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>--- drivers/acpi/arm64/Makefile | 1 + include/linux/acpi.h | 3 +++ include/linux/acpi_iort.h | 6 ++--- drivers/acpi/arm64/dma.c | 50 ++++++++++++++++++++++++++++++++++ drivers/acpi/arm64/iort.c | 54 ++++++------------------------------- drivers/acpi/scan.c | 2 +- 6 files changed, 66 insertions(+), 50 deletions(-)create mode 100644
drivers/acpi/arm64/dma.cdrivers/acpi/arm64/Makefile +1
-0@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_ACPI_IORT) += iort.o obj-$(CONFIG_ACPI_GTDT) += gtdt.o +obj-y += dma.oinclude/linux/acpi.h +3
-0@@ -259,9 +259,12 @@ void acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa);#ifdef CONFIG_ARM64 void acpi_numa_gicc_affinity_init (struct acpi_srat_gicc_affinity *pa); +void acpi_arch_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size);#else static inline void acpi_numa_gicc_affinity_init (struct acpi_srat_gicc_affinity *pa) { } +static inline void +acpi_arch_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size) { }#endif int acpi_numa_memory_affinity_init (struct acpi_srat_mem_affinity *ma);include/linux/acpi_iort.h +3
-3@@ -34,7 +34,7 @@ struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,void acpi_configure_pmsi_domain (struct device *dev);int iort_pmsi_get_dev_id (struct device *dev,u32 *dev_id);/* IOMMU interface */ -void iort_dma_setup (struct device *dev,u64 *dma_addr,u64 *size); +int iort_dma_get_ranges (struct device *dev,u64 *size);const struct iommu_ops *iort_iommu_configure_id (struct device *dev,const u32 *id_in);int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head);@@ -48,8 +48,8 @@ static inline struct irq_domain *iort_get_device_domain( {return NULL ; }static inline void acpi_configure_pmsi_domain (struct device *dev) { }/* IOMMU interface */ -static inline void iort_dma_setup (struct device *dev,u64 *dma_addr, -u64 *size) { } +static inline int iort_dma_get_ranges (struct device *dev,u64 *size) +{return -ENODEV ; }static inline const struct iommu_ops *iort_iommu_configure_id (struct device *dev,const u32 *id_in) {return NULL ; }drivers/acpi/arm64/dma.c new file +50
-0@@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/acpi.h> +#include <linux/acpi_iort.h> +#include <linux/device.h> +#include <linux/dma-direct.h> + +void acpi_arch_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size) +{ +int ret; +u64 end, mask; +u64 dmaaddr =0 , size =0 , offset =0 ; + +/* +* If @dev is expected to be DMA-capable then the bus code that created +* it should have initialised its dma_mask pointer by this point. For +* now, we'll continue the legacy behaviour of coercing it to the +* coherent mask if not, but we'll no longer do so quietly. +*/ +if (!dev->dma_mask) { +dev_warn (dev,"DMA mask not set\n" ); + dev->dma_mask = &dev->coherent_dma_mask; + } + +if (dev->coherent_dma_mask) + size =max (dev->coherent_dma_mask, dev->coherent_dma_mask +1 ); +else + size =1ULL <<32 ; + + ret =acpi_dma_get_range (dev, &dmaaddr, &offset, &size); +if (ret == -ENODEV ) + ret =iort_dma_get_ranges (dev, &size); +if (!ret) { +/* +* Limit coherent and dma mask based on size retrieved from +* firmware. +*/ + end = dmaaddr + size -1 ; + mask =DMA_BIT_MASK (ilog2 (end) +1 ); + dev->bus_dma_limit = end; + dev->coherent_dma_mask =min (dev->coherent_dma_mask, mask); + *dev->dma_mask =min (*dev->dma_mask, mask); + } + + *dma_addr = dmaaddr; + *dma_size = size; + + ret =dma_direct_set_offset (dev, dmaaddr + offset, dmaaddr, size); + +dev_dbg (dev,"dma_offset(%#08llx)%s\n" , offset, ret ?" failed!" :"" ); +}drivers/acpi/arm64/iort.c +8
-46@@ -1144,56 +1144,18 @@ static int rc_dma_get_range(struct device *dev, u64 *size) }/** -* iort_dma_setup() - Set-up device DMA parameters. +* iort_dma_get_ranges() - Look up DMA addressing limit for the device +* @dev: device to lookup +* @size: DMA range size result pointer * -* @dev: device to configure -* @dma_addr: device DMA address result pointer -* @dma_size: DMA range size result pointer +* Return: 0 on success, an error otherwise. */ -void iort_dma_setup (struct device *dev,u64 *dma_addr,u64 *dma_size) +int iort_dma_get_ranges (struct device *dev,u64 *size) { -u64 end, mask, dmaaddr =0 , size =0 , offset =0 ; -int ret; - -/* -* If @dev is expected to be DMA-capable then the bus code that created -* it should have initialised its dma_mask pointer by this point. For -* now, we'll continue the legacy behaviour of coercing it to the -* coherent mask if not, but we'll no longer do so quietly. -*/ -if (!dev->dma_mask) { -dev_warn (dev,"DMA mask not set\n" ); - dev->dma_mask = &dev->coherent_dma_mask; - } - -if (dev->coherent_dma_mask) - size =max (dev->coherent_dma_mask, dev->coherent_dma_mask +1 ); +if (dev_is_pci (dev)) +return rc_dma_get_range (dev, size);else - size =1ULL <<32 ; - - ret =acpi_dma_get_range (dev, &dmaaddr, &offset, &size); -if (ret == -ENODEV ) - ret =dev_is_pci (dev) ?rc_dma_get_range (dev, &size) - :nc_dma_get_range (dev, &size); - -if (!ret) { -/* -* Limit coherent and dma mask based on size retrieved from -* firmware. -*/ - end = dmaaddr + size -1 ; - mask =DMA_BIT_MASK (ilog2 (end) +1 ); - dev->bus_dma_limit = end; - dev->coherent_dma_mask =min (dev->coherent_dma_mask, mask); - *dev->dma_mask =min (*dev->dma_mask, mask); - } - - *dma_addr = dmaaddr; - *dma_size = size; - - ret =dma_direct_set_offset (dev, dmaaddr + offset, dmaaddr, size); - -dev_dbg (dev,"dma_offset(%#08llx)%s\n" , offset, ret ?" failed!" :"" ); +return nc_dma_get_range (dev, size); } static void __init acpi_iort_register_irq(int hwirq, const char *name,drivers/acpi/scan.c +1
-1@@ -1537,7 +1537,7 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,return 0 ; } -iort_dma_setup (dev, &dma_addr, &size); +acpi_arch_dma_setup (dev, &dma_addr, &size); iommu =iort_iommu_configure_id (dev, input_id);if (PTR_ERR (iommu) == -EPROBE_DEFER )
PATCH v5 2/5 ACPI: Move IOMMU setup code out of IORT
In reply to Jean-Philippe BruckerExtract the code that sets up the IOMMU infrastructure from IORT, since it can be reused by VIOT. Move it one level up into a new acpi_iommu_configure_id() function, which calls the IORT parsing function which in turn calls the acpi_iommu_fwspec_init() helper.
--- include/acpi/acpi_bus.h | 3 ++ include/linux/acpi_iort.h | 8 ++--- drivers/acpi/arm64/iort.c | 74 +++++---------------------------------- drivers/acpi/scan.c | 73 +++++++++++++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 72 deletions(-)
include/acpi/acpi_bus.h +3 -0
@@ -588,6 +588,9 @@ struct acpi_pci_root {bool acpi_dma_supported (struct acpi_device *adev);enum dev_dma_attr acpi_get_dma_attr (struct acpi_device *adev); +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops);int acpi_dma_get_range (struct device *dev,u64 *dma_addr,u64 *offset,u64 *size); int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
include/linux/acpi_iort.h +3 -5
@@ -35,8 +35,7 @@ void acpi_configure_pmsi_domain(struct device *dev);int iort_pmsi_get_dev_id (struct device *dev,u32 *dev_id);/* IOMMU interface */ int iort_dma_get_ranges (struct device *dev,u64 *size); -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *id_in); +int iort_iommu_configure_id (struct device *dev,const u32 *id_in);int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head);phys_addr_t acpi_iort_dma_get_max_cpu_address (void );#else
@@ -50,9 +49,8 @@ static inline void acpi_configure_pmsi_domain(struct device *dev) { }/* IOMMU interface */ static inline int iort_dma_get_ranges (struct device *dev,u64 *size) {return -ENODEV ; } -static inline const struct iommu_ops *iort_iommu_configure_id ( -struct device *dev,const u32 *id_in) -{return NULL ; } +static inline int iort_iommu_configure_id (struct device *dev,const u32 *id_in) +{return -ENODEV ; }static inline int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head) {return 0 ; }
drivers/acpi/arm64/iort.c +8 -66
126 lines not on this page · raw message
drivers/acpi/scan.c +72 -1
91 lines not on this page · raw message
Signature · 6 lines
2.32.0 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH v5 2/5] ACPI: Move IOMMU setup code out of IORT
Quoted 4 lines · Jean-Philippe Brucker
Extract the code that sets up the IOMMU infrastructure from IORT, since it can be reused by VIOT. Move it one level up into a new
acpi_iommu_configure_id()function, which calls the IORT parsing function which in turn calls theacpi_iommu_fwspec_init()helper.
Quoted 284 lines
Signed-off-by Jean-Philippe Brucker <[email protected]>--- include/acpi/acpi_bus.h | 3 ++ include/linux/acpi_iort.h | 8 ++--- drivers/acpi/arm64/iort.c | 74 +++++---------------------------------- drivers/acpi/scan.c | 73 +++++++++++++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 72 deletions(-)include/acpi/acpi_bus.h +3
-0@@ -588,6 +588,9 @@ struct acpi_pci_root {bool acpi_dma_supported (struct acpi_device *adev);enum dev_dma_attr acpi_get_dma_attr (struct acpi_device *adev); +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops);int acpi_dma_get_range (struct device *dev,u64 *dma_addr,u64 *offset,u64 *size); int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,include/linux/acpi_iort.h +3
-5@@ -35,8 +35,7 @@ void acpi_configure_pmsi_domain(struct device *dev);int iort_pmsi_get_dev_id (struct device *dev,u32 *dev_id);/* IOMMU interface */ int iort_dma_get_ranges (struct device *dev,u64 *size); -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *id_in); +int iort_iommu_configure_id (struct device *dev,const u32 *id_in);int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head);phys_addr_t acpi_iort_dma_get_max_cpu_address (void );#else @@ -50,9 +49,8 @@ static inline void acpi_configure_pmsi_domain(struct device *dev) { }/* IOMMU interface */ static inline int iort_dma_get_ranges (struct device *dev,u64 *size) {return -ENODEV ; } -static inline const struct iommu_ops *iort_iommu_configure_id ( -struct device *dev,const u32 *id_in) -{return NULL ; } +static inline int iort_iommu_configure_id (struct device *dev,const u32 *id_in) +{return -ENODEV ; }static inline int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head) {return 0 ; }drivers/acpi/arm64/iort.c +8
-66@@ -806,23 +806,6 @@ static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)return NULL ; } -static inline const struct iommu_ops *iort_fwspec_iommu_ops (struct device *dev) -{ -struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); - -return (fwspec && fwspec->ops) ? fwspec->ops :NULL ; -} - -static inline int iort_add_device_replay (struct device *dev) -{ -int err =0 ; - -if (dev->bus && !device_iommu_mapped (dev)) - err =iommu_probe_device (dev); - -return err; -} -/** * iort_iommu_msi_get_resv_regions - Reserved region driver helper * @dev: Device from iommu_get_resv_regions() @@ -900,18 +883,6 @@ static inline bool iort_iommu_driver_enabled(u8 type) } } -static int arm_smmu_iort_xlate (struct device *dev,u32 streamid, -struct fwnode_handle *fwnode, -const struct iommu_ops *ops) -{ -int ret =iommu_fwspec_init (dev, fwnode, ops); - -if (!ret) - ret =iommu_fwspec_add_ids (dev, &streamid,1 ); - -return ret; -} -static bool iort_pci_rc_supports_ats (struct acpi_iort_node *node) {struct acpi_iort_root_complex *pci_rc;@@ -946,7 +917,7 @@ static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,return iort_iommu_driver_enabled (node->type) ? -EPROBE_DEFER : -ENODEV ; -return arm_smmu_iort_xlate (dev, streamid, iort_fwnode, ops); +return acpi_iommu_fwspec_init (dev, streamid, iort_fwnode, ops); }struct iort_pci_alias_info {@@ -1020,24 +991,13 @@ static int iort_nc_iommu_map_id(struct device *dev,* @dev: device to configure * @id_in: optional input id const value pointer * -* Returns: iommu_ops pointer on configuration success -* NULL on configuration failure +* Returns: 0 on success, <0 on failure */ -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *id_in) +int iort_iommu_configure_id (struct device *dev,const u32 *id_in) {struct acpi_iort_node *node; -const struct iommu_ops *ops;int err = -ENODEV ; -/* -* If we already translated the fwspec there -* is nothing left to do, return the iommu_ops. -*/ - ops =iort_fwspec_iommu_ops (dev); -if (ops) -return ops; -if (dev_is_pci (dev)) {struct iommu_fwspec *fwspec;struct pci_bus *bus =to_pci_dev (dev)->bus;@@ -1046,7 +1006,7 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, node =iort_scan_node (ACPI_IORT_NODE_PCI_ROOT_COMPLEX , iort_match_node_callback, &bus->dev);if (!node) -return NULL ; +return -ENODEV ; info.node = node; err = pci_for_each_dma_alias(to_pci_dev(dev),@@ -1059,7 +1019,7 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, node =iort_scan_node (ACPI_IORT_NODE_NAMED_COMPONENT , iort_match_node_callback, dev);if (!node) -return NULL ; +return -ENODEV ; err = id_in ?iort_nc_iommu_map_id (dev, node, id_in) :iort_nc_iommu_map (dev, node);@@ -1068,32 +1028,14 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, iort_named_component_init(dev, node); } -/* -* If we have reason to believe the IOMMU driver missed the initial -* add_device callback for dev, replay it to get things in order. -*/ -if (!err) { - ops =iort_fwspec_iommu_ops (dev); - err =iort_add_device_replay (dev); - } - -/* Ignore all other errors apart from EPROBE_DEFER */ -if (err == -EPROBE_DEFER ) { - ops =ERR_PTR (err); - }else if (err) { -dev_dbg (dev,"Adding to IOMMU failed: %d\n" , err); - ops =NULL ; - } - -return ops; +return err; }#else int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head) {return 0 ; } -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *input_id) -{return NULL ; } +int iort_iommu_configure_id (struct device *dev,const u32 *input_id) +{return -ENODEV ; }#endif static int nc_dma_get_range (struct device *dev,u64 *size)drivers/acpi/scan.c +72
-1@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/iommu.h> #include <linux/signal.h> #include <linux/kthread.h> #include <linux/dmi.h> @@ -1520,6 +1521,76 @@ int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,return ret >=0 ?0 : ret; } +#ifdef CONFIG_IOMMU_API +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops) +{ +int ret =iommu_fwspec_init (dev, fwnode, ops); + +if (!ret) + ret =iommu_fwspec_add_ids (dev, &id,1 ); + +return ret; +} + +static inline const struct iommu_ops *acpi_iommu_fwspec_ops (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec ? fwspec->ops :NULL ; +} + +static const struct iommu_ops *acpi_iommu_configure_id (struct device *dev, +const u32 *id_in) +{ +int err; +const struct iommu_ops *ops; + +/* +* If we already translated the fwspec there is nothing left to do, +* return the iommu_ops. +*/ + ops =acpi_iommu_fwspec_ops (dev); +if (ops) +return ops; + + err =iort_iommu_configure_id (dev, id_in); + +/* +* If we have reason to believe the IOMMU driver missed the initial +* iommu_probe_device() call for dev, replay it to get things in order. +*/ +if (!err && dev->bus && !device_iommu_mapped (dev)) + err =iommu_probe_device (dev); + +/* Ignore all other errors apart from EPROBE_DEFER */ +if (err == -EPROBE_DEFER ) { +return ERR_PTR (err); + }else if (err) { +dev_dbg (dev,"Adding to IOMMU failed: %d\n" , err); +return NULL ; + } +return acpi_iommu_fwspec_ops (dev); +} + +#else /* !CONFIG_IOMMU_API */ + +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops) +{ +return -ENODEV ; +} + +static const struct iommu_ops *acpi_iommu_configure_id (struct device *dev, +const u32 *id_in) +{ +return NULL ; +} + +#endif /* !CONFIG_IOMMU_API */ +/** * acpi_dma_configure_id - Set-up DMA configuration for the device. * @dev: The pointer to the device @@ -1539,7 +1610,7 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,acpi_arch_dma_setup (dev, &dma_addr, &size); - iommu =iort_iommu_configure_id (dev, input_id); + iommu =acpi_iommu_configure_id (dev, input_id);if (PTR_ERR (iommu) == -EPROBE_DEFER )return -EPROBE_DEFER ;
Re: [PATCH v5 2/5] ACPI: Move IOMMU setup code out of IORT
In reply to Jean-Philippe BruckerHi Jean,
Quoted 6 lines · Jean-Philippe Brucker
Extract the code that sets up the IOMMU infrastructure from IORT, since it can be reused by VIOT. Move it one level up into a new
acpi_iommu_configure_id()function, which calls the IORT parsing function which in turn calls theacpi_iommu_fwspec_init()helper.Signed-off-by Jean-Philippe Brucker <[email protected]>
Thanks
Eric
Quoted 282 lines
--- include/acpi/acpi_bus.h | 3 ++ include/linux/acpi_iort.h | 8 ++--- drivers/acpi/arm64/iort.c | 74 +++++---------------------------------- drivers/acpi/scan.c | 73 +++++++++++++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 72 deletions(-)include/acpi/acpi_bus.h +3
-0@@ -588,6 +588,9 @@ struct acpi_pci_root {bool acpi_dma_supported (struct acpi_device *adev);enum dev_dma_attr acpi_get_dma_attr (struct acpi_device *adev); +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops);int acpi_dma_get_range (struct device *dev,u64 *dma_addr,u64 *offset,u64 *size); int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,include/linux/acpi_iort.h +3
-5@@ -35,8 +35,7 @@ void acpi_configure_pmsi_domain(struct device *dev);int iort_pmsi_get_dev_id (struct device *dev,u32 *dev_id);/* IOMMU interface */ int iort_dma_get_ranges (struct device *dev,u64 *size); -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *id_in); +int iort_iommu_configure_id (struct device *dev,const u32 *id_in);int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head);phys_addr_t acpi_iort_dma_get_max_cpu_address (void );#else @@ -50,9 +49,8 @@ static inline void acpi_configure_pmsi_domain(struct device *dev) { }/* IOMMU interface */ static inline int iort_dma_get_ranges (struct device *dev,u64 *size) {return -ENODEV ; } -static inline const struct iommu_ops *iort_iommu_configure_id ( -struct device *dev,const u32 *id_in) -{return NULL ; } +static inline int iort_iommu_configure_id (struct device *dev,const u32 *id_in) +{return -ENODEV ; }static inline int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head) {return 0 ; }drivers/acpi/arm64/iort.c +8
-66@@ -806,23 +806,6 @@ static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)return NULL ; } -static inline const struct iommu_ops *iort_fwspec_iommu_ops (struct device *dev) -{ -struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); - -return (fwspec && fwspec->ops) ? fwspec->ops :NULL ; -} - -static inline int iort_add_device_replay (struct device *dev) -{ -int err =0 ; - -if (dev->bus && !device_iommu_mapped (dev)) - err =iommu_probe_device (dev); - -return err; -} -/** * iort_iommu_msi_get_resv_regions - Reserved region driver helper * @dev: Device from iommu_get_resv_regions() @@ -900,18 +883,6 @@ static inline bool iort_iommu_driver_enabled(u8 type) } } -static int arm_smmu_iort_xlate (struct device *dev,u32 streamid, -struct fwnode_handle *fwnode, -const struct iommu_ops *ops) -{ -int ret =iommu_fwspec_init (dev, fwnode, ops); - -if (!ret) - ret =iommu_fwspec_add_ids (dev, &streamid,1 ); - -return ret; -} -static bool iort_pci_rc_supports_ats (struct acpi_iort_node *node) {struct acpi_iort_root_complex *pci_rc;@@ -946,7 +917,7 @@ static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,return iort_iommu_driver_enabled (node->type) ? -EPROBE_DEFER : -ENODEV ; -return arm_smmu_iort_xlate (dev, streamid, iort_fwnode, ops); +return acpi_iommu_fwspec_init (dev, streamid, iort_fwnode, ops); }struct iort_pci_alias_info {@@ -1020,24 +991,13 @@ static int iort_nc_iommu_map_id(struct device *dev,* @dev: device to configure * @id_in: optional input id const value pointer * -* Returns: iommu_ops pointer on configuration success -* NULL on configuration failure +* Returns: 0 on success, <0 on failure */ -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *id_in) +int iort_iommu_configure_id (struct device *dev,const u32 *id_in) {struct acpi_iort_node *node; -const struct iommu_ops *ops;int err = -ENODEV ; -/* -* If we already translated the fwspec there -* is nothing left to do, return the iommu_ops. -*/ - ops =iort_fwspec_iommu_ops (dev); -if (ops) -return ops; -if (dev_is_pci (dev)) {struct iommu_fwspec *fwspec;struct pci_bus *bus =to_pci_dev (dev)->bus;@@ -1046,7 +1006,7 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, node =iort_scan_node (ACPI_IORT_NODE_PCI_ROOT_COMPLEX , iort_match_node_callback, &bus->dev);if (!node) -return NULL ; +return -ENODEV ; info.node = node; err = pci_for_each_dma_alias(to_pci_dev(dev),@@ -1059,7 +1019,7 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, node =iort_scan_node (ACPI_IORT_NODE_NAMED_COMPONENT , iort_match_node_callback, dev);if (!node) -return NULL ; +return -ENODEV ; err = id_in ?iort_nc_iommu_map_id (dev, node, id_in) :iort_nc_iommu_map (dev, node);@@ -1068,32 +1028,14 @@ const struct iommu_ops *iort_iommu_configure_id(struct device *dev, iort_named_component_init(dev, node); } -/* -* If we have reason to believe the IOMMU driver missed the initial -* add_device callback for dev, replay it to get things in order. -*/ -if (!err) { - ops =iort_fwspec_iommu_ops (dev); - err =iort_add_device_replay (dev); - } - -/* Ignore all other errors apart from EPROBE_DEFER */ -if (err == -EPROBE_DEFER ) { - ops =ERR_PTR (err); - }else if (err) { -dev_dbg (dev,"Adding to IOMMU failed: %d\n" , err); - ops =NULL ; - } - -return ops; +return err; }#else int iort_iommu_msi_get_resv_regions (struct device *dev,struct list_head *head) {return 0 ; } -const struct iommu_ops *iort_iommu_configure_id (struct device *dev, -const u32 *input_id) -{return NULL ; } +int iort_iommu_configure_id (struct device *dev,const u32 *input_id) +{return -ENODEV ; }#endif static int nc_dma_get_range (struct device *dev,u64 *size)drivers/acpi/scan.c +72
-1@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/iommu.h> #include <linux/signal.h> #include <linux/kthread.h> #include <linux/dmi.h> @@ -1520,6 +1521,76 @@ int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,return ret >=0 ?0 : ret; } +#ifdef CONFIG_IOMMU_API +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops) +{ +int ret =iommu_fwspec_init (dev, fwnode, ops); + +if (!ret) + ret =iommu_fwspec_add_ids (dev, &id,1 ); + +return ret; +} + +static inline const struct iommu_ops *acpi_iommu_fwspec_ops (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec ? fwspec->ops :NULL ; +} + +static const struct iommu_ops *acpi_iommu_configure_id (struct device *dev, +const u32 *id_in) +{ +int err; +const struct iommu_ops *ops; + +/* +* If we already translated the fwspec there is nothing left to do, +* return the iommu_ops. +*/ + ops =acpi_iommu_fwspec_ops (dev); +if (ops) +return ops; + + err =iort_iommu_configure_id (dev, id_in); + +/* +* If we have reason to believe the IOMMU driver missed the initial +* iommu_probe_device() call for dev, replay it to get things in order. +*/ +if (!err && dev->bus && !device_iommu_mapped (dev)) + err =iommu_probe_device (dev); + +/* Ignore all other errors apart from EPROBE_DEFER */ +if (err == -EPROBE_DEFER ) { +return ERR_PTR (err); + }else if (err) { +dev_dbg (dev,"Adding to IOMMU failed: %d\n" , err); +return NULL ; + } +return acpi_iommu_fwspec_ops (dev); +} + +#else /* !CONFIG_IOMMU_API */ + +int acpi_iommu_fwspec_init (struct device *dev,u32 id, +struct fwnode_handle *fwnode, +const struct iommu_ops *ops) +{ +return -ENODEV ; +} + +static const struct iommu_ops *acpi_iommu_configure_id (struct device *dev, +const u32 *id_in) +{ +return NULL ; +} + +#endif /* !CONFIG_IOMMU_API */ +/** * acpi_dma_configure_id - Set-up DMA configuration for the device. * @dev: The pointer to the device @@ -1539,7 +1610,7 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,acpi_arch_dma_setup (dev, &dma_addr, &size); - iommu =iort_iommu_configure_id (dev, input_id); + iommu =acpi_iommu_configure_id (dev, input_id);if (PTR_ERR (iommu) == -EPROBE_DEFER )return -EPROBE_DEFER ;
PATCH v5 3/5 ACPI: Add driver for the VIOT table
In reply to Jean-Philippe BruckerThe ACPI Virtual I/O Translation Table describes topology of para-virtual platforms, similarly to vendor tables DMAR, IVRS and IORT. For now it describes the relation between virtio-iommu and the endpoints it manages.
Three steps are needed to configure DMA of endpoints:
(1) acpi_viot_init(): parse the VIOT table, find or create the fwnode associated to each vIOMMU device. This needs to happen after acpi_scan_init(), because it relies on the struct device and their fwnode to be available.
(2) When probing the vIOMMU device, the driver registers its IOMMU ops within the IOMMU subsystem. This step doesn't require any intervention from the VIOT driver.
(3) viot_iommu_configure(): before binding the endpoint to a driver, find the associated IOMMU ops. Register them, along with the endpoint ID, into the device's iommu_fwspec.
If step (3) happens before step (2), it is deferred until the IOMMU is initialized, then retried.
--- drivers/acpi/Kconfig | 3 + drivers/iommu/Kconfig | 1 + drivers/acpi/Makefile | 2 + include/linux/acpi_viot.h | 19 ++ drivers/acpi/bus.c | 2 + drivers/acpi/scan.c | 3 + drivers/acpi/viot.c | 366 ++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 8 + 8 files changed, 404 insertions(+) create mode 100644 include/linux/acpi_viot.h create mode 100644 drivers/acpi/viot.c
drivers/acpi/Kconfig +3 -0
@@ -526,6 +526,9 @@ endif source "drivers/acpi/pmic/Kconfig" +config ACPI_VIOT + bool + endif # ACPI config X86_PM_TIMER
drivers/iommu/Kconfig +1 -0
@@ -403,6 +403,7 @@ config VIRTIO_IOMMU depends on ARM64 select IOMMU_API select INTERVAL_TREE + select ACPI_VIOT if ACPI help Para-virtualised IOMMU driver with virtio.
drivers/acpi/Makefile +2 -0
@@ -118,3 +118,5 @@ video-objs += acpi_video.o video_detect.o obj-y += dptf/ obj-$(CONFIG_ARM64) += arm64/ + +obj-$(CONFIG_ACPI_VIOT) += viot.o
include/linux/acpi_viot.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ACPI_VIOT_H__ +#define __ACPI_VIOT_H__ + +#include <linux/acpi.h> + +#ifdef CONFIG_ACPI_VIOT +void __init acpi_viot_init (void ); +int viot_iommu_configure (struct device *dev); +#else +static inline void acpi_viot_init (void ) {} +static inline int viot_iommu_configure (struct device *dev) +{ +return -ENODEV ; +} +#endif + +#endif /* __ACPI_VIOT_H__ */
drivers/acpi/bus.c +2 -0
@@ -27,6 +27,7 @@#include <linux/dmi.h> #endif #include <linux/acpi_iort.h> +#include <linux/acpi_viot.h> #include <linux/pci.h> #include <acpi/apei.h> #include <linux/suspend.h>
@@ -1334,6 +1335,7 @@ static int __init acpi_init(void)acpi_wakeup_device_init ();acpi_debugger_init ();acpi_setup_sb_notify_handler (); +acpi_viot_init ();return 0 ; }
drivers/acpi/scan.c +3 -0
@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/acpi_viot.h> #include <linux/iommu.h> #include <linux/signal.h> #include <linux/kthread.h>
@@ -1556,6 +1557,8 @@ static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,return ops; err =iort_iommu_configure_id (dev, id_in); +if (err && err != -EPROBE_DEFER ) + err =viot_iommu_configure (dev);/* * If we have reason to believe the IOMMU driver missed the initial
drivers/acpi/viot.c new file +366 -0
366 lines not on this page · raw message
MAINTAINERS +8 -0
@@ -431,6 +431,14 @@ W: https://01.org/linux-acpi B: https://bugzilla.kernel.org F: drivers/acpi/acpi_video.c +ACPI VIOT DRIVER +M: Jean-Philippe Brucker <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/acpi/viot.c +F: include/linux/acpi_viot.h + ACPI WMI DRIVER L: [email protected] S: Orphan
Signature · 6 lines
2.32.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v5 3/5] ACPI: Add driver for the VIOT table
Quoted 21 lines · Jean-Philippe Brucker
The ACPI Virtual I/O Translation Table describes topology of para-virtual platforms, similarly to vendor tables DMAR, IVRS and IORT. For now it describes the relation between virtio-iommu and the endpoints it manages.
Three steps are needed to configure DMA of endpoints:
(1)
acpi_viot_init(): parse the VIOT table, find or create the fwnode associated to each vIOMMU device. This needs to happen afteracpi_scan_init(), because it relies on the struct device and their fwnode to be available.(2) When probing the vIOMMU device, the driver registers its IOMMU ops within the IOMMU subsystem. This step doesn't require any intervention from the VIOT driver.
(3)
viot_iommu_configure(): before binding the endpoint to a driver, find the associated IOMMU ops. Register them, along with the endpoint ID, into the device'siommu_fwspec.
Quoted 6 lines
If step (3) happens before step (2), it is deferred until the IOMMU is initialized, then retried.
Tested-by Eric Auger <[email protected]>Reviewed-by Eric Auger <[email protected]>Signed-off-by Jean-Philippe Brucker <[email protected]>
From the general ACPI perspective
and I'm assuming that it will be routed through a different tree.
Quoted 509 lines
--- drivers/acpi/Kconfig | 3 + drivers/iommu/Kconfig | 1 + drivers/acpi/Makefile | 2 + include/linux/acpi_viot.h | 19 ++ drivers/acpi/bus.c | 2 + drivers/acpi/scan.c | 3 + drivers/acpi/viot.c | 366 ++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 8 + 8 files changed, 404 insertions(+) create mode 100644 include/linux/acpi_viot.h create mode 100644 drivers/acpi/viot.cdrivers/acpi/Kconfig +3
-0@@ -526,6 +526,9 @@ endif source "drivers/acpi/pmic/Kconfig" +config ACPI_VIOT + bool + endif # ACPI config X86_PM_TIMERdrivers/iommu/Kconfig +1
-0@@ -403,6 +403,7 @@ config VIRTIO_IOMMU depends on ARM64 select IOMMU_API select INTERVAL_TREE + select ACPI_VIOT if ACPI help Para-virtualised IOMMU driver with virtio.drivers/acpi/Makefile +2
-0@@ -118,3 +118,5 @@ video-objs += acpi_video.o video_detect.o obj-y += dptf/ obj-$(CONFIG_ARM64) += arm64/ + +obj-$(CONFIG_ACPI_VIOT) += viot.oinclude/linux/acpi_viot.h new file +19
-0@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ACPI_VIOT_H__ +#define __ACPI_VIOT_H__ + +#include <linux/acpi.h> + +#ifdef CONFIG_ACPI_VIOT +void __init acpi_viot_init (void ); +int viot_iommu_configure (struct device *dev); +#else +static inline void acpi_viot_init (void ) {} +static inline int viot_iommu_configure (struct device *dev) +{ +return -ENODEV ; +} +#endif + +#endif /* __ACPI_VIOT_H__ */ drivers/acpi/bus.c +2
-0@@ -27,6 +27,7 @@#include <linux/dmi.h> #endif #include <linux/acpi_iort.h> +#include <linux/acpi_viot.h> #include <linux/pci.h> #include <acpi/apei.h> #include <linux/suspend.h> @@ -1334,6 +1335,7 @@ static int __init acpi_init(void)acpi_wakeup_device_init ();acpi_debugger_init ();acpi_setup_sb_notify_handler (); +acpi_viot_init ();return 0 ; }drivers/acpi/scan.c +3
-0@@ -9,6 +9,7 @@#include <linux/kernel.h> #include <linux/acpi.h> #include <linux/acpi_iort.h> +#include <linux/acpi_viot.h> #include <linux/iommu.h> #include <linux/signal.h> #include <linux/kthread.h> @@ -1556,6 +1557,8 @@ static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,return ops; err =iort_iommu_configure_id (dev, id_in); +if (err && err != -EPROBE_DEFER ) + err =viot_iommu_configure (dev);/* * If we have reason to believe the IOMMU driver missed the initial drivers/acpi/viot.c new file +366
-0@@ -0,0 +1,366 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Virtual I/O topology +* +* The Virtual I/O Translation Table (VIOT) describes the topology of +* para-virtual IOMMUs and the endpoints they manage. The OS uses it to +* initialize devices in the right order, preventing endpoints from issuing DMA +* before their IOMMU is ready. +* +* When binding a driver to a device, before calling the device driver's probe() +* method, the driver infrastructure calls dma_configure(). At that point the +* VIOT driver looks for an IOMMU associated to the device in the VIOT table. +* If an IOMMU exists and has been initialized, the VIOT driver initializes the +* device's IOMMU fwspec, allowing the DMA infrastructure to invoke the IOMMU +* ops when the device driver configures DMA mappings. If an IOMMU exists and +* hasn't yet been initialized, VIOT returns -EPROBE_DEFER to postpone probing +* the device until the IOMMU is available. +*/ +#define pr_fmt (fmt) "ACPI: VIOT: " fmt + +#include <linux/acpi_viot.h> +#include <linux/dma-iommu.h> +#include <linux/fwnode.h> +#include <linux/iommu.h> +#include <linux/list.h> +#include <linux/pci.h> +#include <linux/platform_device.h> + +struct viot_iommu { +/* Node offset within the table */ +unsigned int offset; +struct fwnode_handle *fwnode; +struct list_head list; +}; + +struct viot_endpoint { +union { +/* PCI range */ +struct { +u16 segment_start; +u16 segment_end; +u16 bdf_start; +u16 bdf_end; + }; +/* MMIO */ +u64 address; + }; +u32 endpoint_id; +struct viot_iommu *viommu; +struct list_head list; +}; + +static struct acpi_table_viot *viot; +static LIST_HEAD (viot_iommus); +static LIST_HEAD (viot_pci_ranges); +static LIST_HEAD (viot_mmio_endpoints); + +static int __init viot_check_bounds (const struct acpi_viot_header *hdr) +{ +struct acpi_viot_header *start, *end, *hdr_end; + + start = ACPI_ADD_PTR(struct acpi_viot_header, viot, +max_t (size_t,sizeof (*viot), viot->node_offset)); + end = ACPI_ADD_PTR(struct acpi_viot_header, viot, viot->header.length); + hdr_end = ACPI_ADD_PTR(struct acpi_viot_header, hdr, sizeof(*hdr)); + +if (hdr < start || hdr_end > end) { +pr_err (FW_BUG "Node pointer overflows\n" ); +return -EOVERFLOW ; + } +if (hdr->length <sizeof (*hdr)) { +pr_err (FW_BUG "Empty node\n" ); +return -EINVAL ; + } +return 0 ; +} + +static int __init viot_get_pci_iommu_fwnode (struct viot_iommu *viommu, +u16 segment,u16 bdf) +{ +struct pci_dev *pdev; +struct fwnode_handle *fwnode; + + pdev =pci_get_domain_bus_and_slot (segment,PCI_BUS_NUM (bdf), + bdf &0xff ); +if (!pdev) { +pr_err ("Could not find PCI IOMMU\n" ); +return -ENODEV ; + } + + fwnode = pdev->dev.fwnode; +if (!fwnode) { +/* +* PCI devices aren't necessarily described by ACPI. Create a +* fwnode so the IOMMU subsystem can identify this device. +*/ + fwnode =acpi_alloc_fwnode_static (); +if (!fwnode) { +pci_dev_put (pdev); +return -ENOMEM ; + } +set_primary_fwnode (&pdev->dev, fwnode); + } + viommu->fwnode = pdev->dev.fwnode; +pci_dev_put (pdev); +return 0 ; +} + +static int __init viot_get_mmio_iommu_fwnode (struct viot_iommu *viommu, +u64 address) +{ +struct acpi_device *adev; +struct resource res = { + .start = address, + .end = address, + .flags =IORESOURCE_MEM , + }; + + adev =acpi_resource_consumer (&res); +if (!adev) { +pr_err ("Could not find MMIO IOMMU\n" ); +return -EINVAL ; + } + viommu->fwnode = &adev->fwnode; +return 0 ; +} + +static struct viot_iommu *__init viot_get_iommu (unsigned int offset) +{ +int ret; +struct viot_iommu *viommu; + struct acpi_viot_header *hdr = ACPI_ADD_PTR(struct acpi_viot_header, + viot, offset); +union { +struct acpi_viot_virtio_iommu_pci pci; +struct acpi_viot_virtio_iommu_mmio mmio; + } *node = (void *)hdr; + +list_for_each_entry (viommu, &viot_iommus, list) +if (viommu->offset == offset) +return viommu; + +if (viot_check_bounds (hdr)) +return NULL ; + + viommu =kzalloc (sizeof (*viommu),GFP_KERNEL ); +if (!viommu) +return NULL ; + + viommu->offset = offset; +switch (hdr->type) { +case ACPI_VIOT_NODE_VIRTIO_IOMMU_PCI : +if (hdr->length <sizeof (node->pci)) + goto err_free; + + ret =viot_get_pci_iommu_fwnode (viommu, node->pci.segment, + node->pci.bdf); +break ; +case ACPI_VIOT_NODE_VIRTIO_IOMMU_MMIO : +if (hdr->length <sizeof (node->mmio)) + goto err_free; + + ret =viot_get_mmio_iommu_fwnode (viommu, + node->mmio.base_address); +break ; +default : + ret = -EINVAL ; + } +if (ret) + goto err_free; + +list_add (&viommu->list, &viot_iommus); +return viommu; + +err_free: +kfree (viommu); +return NULL ; +} + +static int __init viot_parse_node (const struct acpi_viot_header *hdr) +{ +int ret = -EINVAL ; +struct list_head *list; +struct viot_endpoint *ep; +union { +struct acpi_viot_mmio mmio; +struct acpi_viot_pci_range pci; + } *node = (void *)hdr; + +if (viot_check_bounds (hdr)) +return -EINVAL ; + +if (hdr->type ==ACPI_VIOT_NODE_VIRTIO_IOMMU_PCI || + hdr->type ==ACPI_VIOT_NODE_VIRTIO_IOMMU_MMIO ) +return 0 ; + + ep =kzalloc (sizeof (*ep),GFP_KERNEL ); +if (!ep) +return -ENOMEM ; + +switch (hdr->type) { +case ACPI_VIOT_NODE_PCI_RANGE : +if (hdr->length <sizeof (node->pci)) { +pr_err (FW_BUG "Invalid PCI node size\n" ); + goto err_free; + } + + ep->segment_start = node->pci.segment_start; + ep->segment_end = node->pci.segment_end; + ep->bdf_start = node->pci.bdf_start; + ep->bdf_end = node->pci.bdf_end; + ep->endpoint_id = node->pci.endpoint_start; + ep->viommu =viot_get_iommu (node->pci.output_node); + list = &viot_pci_ranges; +break ; +case ACPI_VIOT_NODE_MMIO : +if (hdr->length <sizeof (node->mmio)) { +pr_err (FW_BUG "Invalid MMIO node size\n" ); + goto err_free; + } + + ep->address = node->mmio.base_address; + ep->endpoint_id = node->mmio.endpoint; + ep->viommu =viot_get_iommu (node->mmio.output_node); + list = &viot_mmio_endpoints; +break ; +default : +pr_warn ("Unsupported node %x\n" , hdr->type); + ret =0 ; + goto err_free; + } + +if (!ep->viommu) { +pr_warn ("No IOMMU node found\n" ); +/* +* A future version of the table may use the node for other +* purposes. Keep parsing. +*/ + ret =0 ; + goto err_free; + } + +list_add (&ep->list, list); +return 0 ; + +err_free: +kfree (ep); +return ret; +} + +/** +* acpi_viot_init - Parse the VIOT table +* +* Parse the VIOT table, prepare the list of endpoints to be used during DMA +* setup of devices. +*/ +void __init acpi_viot_init (void ) +{ +int i; +acpi_status status; +struct acpi_table_header *hdr; +struct acpi_viot_header *node; + + status =acpi_get_table (ACPI_SIG_VIOT ,0 , &hdr); +if (ACPI_FAILURE (status)) { +if (status !=AE_NOT_FOUND ) { +const char *msg =acpi_format_exception (status); + +pr_err ("Failed to get table, %s\n" , msg); + } +return ; + } + + viot = (void *)hdr; + + node = ACPI_ADD_PTR(struct acpi_viot_header, viot, viot->node_offset); +for (i =0 ; i < viot->node_count; i++) { +if (viot_parse_node (node)) +return ; + + node = ACPI_ADD_PTR(struct acpi_viot_header, node, + node->length); + } + +acpi_put_table (hdr); +} + +static int viot_dev_iommu_init (struct device *dev,struct viot_iommu *viommu, +u32 epid) +{ +const struct iommu_ops *ops; + +if (!viommu) +return -ENODEV ; + +/* We're not translating ourself */ +if (viommu->fwnode == dev->fwnode) +return -EINVAL ; + + ops =iommu_ops_from_fwnode (viommu->fwnode); +if (!ops) +return IS_ENABLED (CONFIG_VIRTIO_IOMMU ) ? + -EPROBE_DEFER : -ENODEV ; + +return acpi_iommu_fwspec_init (dev, epid, viommu->fwnode, ops); +} + +static int viot_pci_dev_iommu_init (struct pci_dev *pdev,u16 dev_id,void *data) +{ +u32 epid; +struct viot_endpoint *ep; +u32 domain_nr =pci_domain_nr (pdev->bus); + +list_for_each_entry (ep, &viot_pci_ranges, list) { +if (domain_nr >= ep->segment_start && + domain_nr <= ep->segment_end && + dev_id >= ep->bdf_start && + dev_id <= ep->bdf_end) { + 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); + } + } +return -ENODEV ; +} + +static int viot_mmio_dev_iommu_init (struct platform_device *pdev) +{ +struct resource *mem; +struct viot_endpoint *ep; + + mem =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!mem) +return -ENODEV ; + +list_for_each_entry (ep, &viot_mmio_endpoints, list) { +if (ep->address == mem->start) +return viot_dev_iommu_init (&pdev->dev, ep->viommu, + ep->endpoint_id); + } +return -ENODEV ; +} + +/** +* viot_iommu_configure - Setup IOMMU ops for an endpoint described by VIOT +* @dev: the endpoint +* +* Return: 0 on success, <0 on failure +*/ +int viot_iommu_configure (struct device *dev) +{ +if (dev_is_pci (dev)) +return pci_for_each_dma_alias (to_pci_dev (dev), + viot_pci_dev_iommu_init,NULL ); +else if (dev_is_platform (dev)) +return viot_mmio_dev_iommu_init (to_platform_device (dev)); +return -ENODEV ; +}MAINTAINERS +8
-1@@ -431,6 +431,14 @@ W: https://01.org/linux-acpi B: https://bugzilla.kernel.org F: drivers/acpi/acpi_video.c +ACPI VIOT DRIVER +M: Jean-Philippe Brucker <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/acpi/viot.c +F: include/linux/acpi_viot.h + ACPI WMI DRIVER L: [email protected] S: Orphan --2.32.0
PATCH v5 4/5 iommu/dma: Pass address limit rather than size to iommu_setup_dma_ops()
In reply to Jean-Philippe BruckerPassing a 64-bit address width to iommu_setup_dma_ops() is valid on virtual platforms, but isn't currently possible. The overflow check in iommu_dma_init_domain() prevents this even when @dma_base isn't 0. Pass a limit address instead of a size, so callers don't have to fake a size to work around the check.
The base and limit parameters are being phased out, because:
* they are redundant for x86 callers. dma-iommu already reserves the first page, and the upper limit is already in domain->geometry.
* they can now be obtained from dev->dma_range_map on Arm. But removing them on Arm isn't completely straightforward so is left for future work. As an intermediate step, simplify the x86 callers by passing dummy limits.
--- include/linux/dma-iommu.h | 4 ++-- arch/arm64/mm/dma-mapping.c | 2 +- drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/dma-iommu.c | 12 ++++++------ drivers/iommu/intel/iommu.c | 5 +---- 5 files changed, 11 insertions(+), 14 deletions(-)
include/linux/dma-iommu.h +2 -2
@@ -19,7 +19,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);void iommu_put_dma_cookie (struct iommu_domain *domain);/* Setup call for arch DMA mapping code */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size); +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit);/* The DMA API isn't _quite_ the whole story, though... */ /*
@@ -50,7 +50,7 @@ struct msi_msg;struct device ;static inline void iommu_setup_dma_ops (struct device *dev,u64 dma_base, -u64 size) +u64 dma_limit) { }
arch/arm64/mm/dma-mapping.c +1 -1
@@ -50,7 +50,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, dev->dma_coherent = coherent;if (iommu) -iommu_setup_dma_ops (dev, dma_base, size); +iommu_setup_dma_ops (dev, dma_base, dma_base + size -1 );#ifdef CONFIG_XEN if (xen_swiotlb_detect ())
drivers/iommu/amd/iommu.c +1 -1
@@ -1713,7 +1713,7 @@ static void amd_iommu_probe_finalize(struct device *dev)/* Domains are initialized for this device - have a look what we ended up with */ domain =iommu_get_domain_for_dev (dev);if (domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev,IOVA_START_PFN <<PAGE_SHIFT ,0 ); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }
drivers/iommu/dma-iommu.c +6 -6
@@ -319,16 +319,16 @@ static bool dev_is_untrusted(struct device *dev)* iommu_dma_init_domain - Initialise a DMA mapping domain * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() * @base: IOVA at which the mappable address space starts -* @size: Size of IOVA space +* @limit: Last address of the IOVA space * @dev: Device the domain is being initialised for * -* @base and @size should be exact multiples of IOMMU page granularity to +* @base and @limit + 1 should be exact multiples of IOMMU page granularity to * avoid rounding surprises. If necessary, we reserve the page at address 0 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but * any change which could make prior IOVAs invalid will fail. */ static int iommu_dma_init_domain (struct iommu_domain *domain,dma_addr_t base, -u64 size,struct device *dev) +dma_addr_t limit,struct device *dev) {struct iommu_dma_cookie *cookie = domain->iova_cookie;unsigned long order, base_pfn;
@@ -346,7 +346,7 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,/* Check the domain allows at least some access to the device... */ if (domain->geometry.force_aperture) {if (base > domain->geometry.aperture_end || - base + size <= domain->geometry.aperture_start) { + limit < domain->geometry.aperture_start) {pr_warn ("specified DMA range outside IOMMU capability\n" );return -EFAULT ; }
@@ -1308,7 +1308,7 @@ static const struct dma_map_ops iommu_dma_ops = {* The IOMMU core code allocates the default DMA domain, which the underlying * IOMMU driver needs to support via the dma-iommu layer. */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size) +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit) {struct iommu_domain *domain =iommu_get_domain_for_dev (dev);
@@ -1320,7 +1320,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)* underlying IOMMU driver needs to support via the dma-iommu layer. */ if (domain->type ==IOMMU_DOMAIN_DMA ) { -if (iommu_dma_init_domain (domain, dma_base, size, dev)) +if (iommu_dma_init_domain (domain, dma_base, dma_limit, dev)) goto out_err; dev->dma_ops = &iommu_dma_ops; }
drivers/iommu/intel/iommu.c +1 -4
@@ -5165,13 +5165,10 @@ static void intel_iommu_release_device(struct device *dev)static void intel_iommu_probe_finalize (struct device *dev) { -dma_addr_t base =IOVA_START_PFN <<VTD_PAGE_SHIFT ;struct iommu_domain *domain =iommu_get_domain_for_dev (dev); -struct dmar_domain *dmar_domain =to_dmar_domain (domain);if (domain && domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev, base, -__DOMAIN_MAX_ADDR (dmar_domain->gaw) - base); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }
Signature · 6 lines
2.32.0 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH v5 4/5] iommu/dma: Pass address limit rather than size to iommu_setup_dma_ops()
Quoted 6 lines · Jean-Philippe Brucker
Passing a 64-bit address width to
iommu_setup_dma_ops()is valid on virtual platforms, but isn't currently possible. The overflow check iniommu_dma_init_domain()prevents this even when @dma_base isn't 0. Pass a limit address instead of a size, so callers don't have to fake a size to work around the check.
Quoted 7 lines
The base and limit parameters are being phased out, because:
* they are redundant for x86 callers. dma-iommu already reserves the first page, and the upper limit is already indomain->geometry.
* they can now be obtained fromdev->dma_range_mapon Arm. But removing them on Arm isn't completely straightforward so is left for future work. As an intermediate step, simplify the x86 callers by passing dummy limits.
Quoted 128 lines
Signed-off-by Jean-Philippe Brucker <[email protected]>--- include/linux/dma-iommu.h | 4 ++-- arch/arm64/mm/dma-mapping.c | 2 +- drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/dma-iommu.c | 12 ++++++------ drivers/iommu/intel/iommu.c | 5 +---- 5 files changed, 11 insertions(+), 14 deletions(-)include/linux/dma-iommu.h +2
-2@@ -19,7 +19,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);void iommu_put_dma_cookie (struct iommu_domain *domain);/* Setup call for arch DMA mapping code */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size); +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit);/* The DMA API isn't _quite_ the whole story, though... */ /* @@ -50,7 +50,7 @@ struct msi_msg;struct device ;static inline void iommu_setup_dma_ops (struct device *dev,u64 dma_base, -u64 size) +u64 dma_limit) { }arch/arm64/mm/dma-mapping.c +1
-1@@ -50,7 +50,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, dev->dma_coherent = coherent;if (iommu) -iommu_setup_dma_ops (dev, dma_base, size); +iommu_setup_dma_ops (dev, dma_base, dma_base + size -1 );#ifdef CONFIG_XEN if (xen_swiotlb_detect ())drivers/iommu/amd/iommu.c +1
-1@@ -1713,7 +1713,7 @@ static void amd_iommu_probe_finalize(struct device *dev)/* Domains are initialized for this device - have a look what we ended up with */ domain =iommu_get_domain_for_dev (dev);if (domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev,IOVA_START_PFN <<PAGE_SHIFT ,0 ); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }drivers/iommu/dma-iommu.c +6
-6@@ -319,16 +319,16 @@ static bool dev_is_untrusted(struct device *dev)* iommu_dma_init_domain - Initialise a DMA mapping domain * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() * @base: IOVA at which the mappable address space starts -* @size: Size of IOVA space +* @limit: Last address of the IOVA space * @dev: Device the domain is being initialised for * -* @base and @size should be exact multiples of IOMMU page granularity to +* @base and @limit + 1 should be exact multiples of IOMMU page granularity to * avoid rounding surprises. If necessary, we reserve the page at address 0 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but * any change which could make prior IOVAs invalid will fail. */ static int iommu_dma_init_domain (struct iommu_domain *domain,dma_addr_t base, -u64 size,struct device *dev) +dma_addr_t limit,struct device *dev) {struct iommu_dma_cookie *cookie = domain->iova_cookie;unsigned long order, base_pfn;@@ -346,7 +346,7 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,/* Check the domain allows at least some access to the device... */ if (domain->geometry.force_aperture) {if (base > domain->geometry.aperture_end || - base + size <= domain->geometry.aperture_start) { + limit < domain->geometry.aperture_start) {pr_warn ("specified DMA range outside IOMMU capability\n" );return -EFAULT ; }@@ -1308,7 +1308,7 @@ static const struct dma_map_ops iommu_dma_ops = {* The IOMMU core code allocates the default DMA domain, which the underlying * IOMMU driver needs to support via the dma-iommu layer. */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size) +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit) {struct iommu_domain *domain =iommu_get_domain_for_dev (dev);@@ -1320,7 +1320,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)* underlying IOMMU driver needs to support via the dma-iommu layer. */ if (domain->type ==IOMMU_DOMAIN_DMA ) { -if (iommu_dma_init_domain (domain, dma_base, size, dev)) +if (iommu_dma_init_domain (domain, dma_base, dma_limit, dev)) goto out_err; dev->dma_ops = &iommu_dma_ops; }drivers/iommu/intel/iommu.c +1
-4@@ -5165,13 +5165,10 @@ static void intel_iommu_release_device(struct device *dev)static void intel_iommu_probe_finalize (struct device *dev) { -dma_addr_t base =IOVA_START_PFN <<VTD_PAGE_SHIFT ;struct iommu_domain *domain =iommu_get_domain_for_dev (dev); -struct dmar_domain *dmar_domain =to_dmar_domain (domain);if (domain && domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev, base, -__DOMAIN_MAX_ADDR (dmar_domain->gaw) - base); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }
Re: [PATCH v5 4/5] iommu/dma: Pass address limit rather than size to iommu_setup_dma_ops()
In reply to Jean-Philippe BruckerHi Jean,
Quoted 14 lines · Jean-Philippe Brucker
Passing a 64-bit address width to
iommu_setup_dma_ops()is valid on virtual platforms, but isn't currently possible. The overflow check iniommu_dma_init_domain()prevents this even when @dma_base isn't 0. Pass a limit address instead of a size, so callers don't have to fake a size to work around the check.The base and limit parameters are being phased out, because:
* they are redundant for x86 callers. dma-iommu already reserves the first page, and the upper limit is already indomain->geometry.
* they can now be obtained fromdev->dma_range_mapon Arm. But removing them on Arm isn't completely straightforward so is left for future work. As an intermediate step, simplify the x86 callers by passing dummy limits.
Quoted 1 line
Signed-off-by Jean-Philippe Brucker <[email protected]>
Eric
Quoted 126 lines
--- include/linux/dma-iommu.h | 4 ++-- arch/arm64/mm/dma-mapping.c | 2 +- drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/dma-iommu.c | 12 ++++++------ drivers/iommu/intel/iommu.c | 5 +---- 5 files changed, 11 insertions(+), 14 deletions(-)include/linux/dma-iommu.h +2
-2@@ -19,7 +19,7 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base);void iommu_put_dma_cookie (struct iommu_domain *domain);/* Setup call for arch DMA mapping code */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size); +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit);/* The DMA API isn't _quite_ the whole story, though... */ /* @@ -50,7 +50,7 @@ struct msi_msg;struct device ;static inline void iommu_setup_dma_ops (struct device *dev,u64 dma_base, -u64 size) +u64 dma_limit) { }arch/arm64/mm/dma-mapping.c +1
-1@@ -50,7 +50,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, dev->dma_coherent = coherent;if (iommu) -iommu_setup_dma_ops (dev, dma_base, size); +iommu_setup_dma_ops (dev, dma_base, dma_base + size -1 );#ifdef CONFIG_XEN if (xen_swiotlb_detect ())drivers/iommu/amd/iommu.c +1
-1@@ -1713,7 +1713,7 @@ static void amd_iommu_probe_finalize(struct device *dev)/* Domains are initialized for this device - have a look what we ended up with */ domain =iommu_get_domain_for_dev (dev);if (domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev,IOVA_START_PFN <<PAGE_SHIFT ,0 ); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }drivers/iommu/dma-iommu.c +6
-6@@ -319,16 +319,16 @@ static bool dev_is_untrusted(struct device *dev)* iommu_dma_init_domain - Initialise a DMA mapping domain * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() * @base: IOVA at which the mappable address space starts -* @size: Size of IOVA space +* @limit: Last address of the IOVA space * @dev: Device the domain is being initialised for * -* @base and @size should be exact multiples of IOMMU page granularity to +* @base and @limit + 1 should be exact multiples of IOMMU page granularity to * avoid rounding surprises. If necessary, we reserve the page at address 0 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but * any change which could make prior IOVAs invalid will fail. */ static int iommu_dma_init_domain (struct iommu_domain *domain,dma_addr_t base, -u64 size,struct device *dev) +dma_addr_t limit,struct device *dev) {struct iommu_dma_cookie *cookie = domain->iova_cookie;unsigned long order, base_pfn;@@ -346,7 +346,7 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,/* Check the domain allows at least some access to the device... */ if (domain->geometry.force_aperture) {if (base > domain->geometry.aperture_end || - base + size <= domain->geometry.aperture_start) { + limit < domain->geometry.aperture_start) {pr_warn ("specified DMA range outside IOMMU capability\n" );return -EFAULT ; }@@ -1308,7 +1308,7 @@ static const struct dma_map_ops iommu_dma_ops = {* The IOMMU core code allocates the default DMA domain, which the underlying * IOMMU driver needs to support via the dma-iommu layer. */ -void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 size) +void iommu_setup_dma_ops (struct device *dev,u64 dma_base,u64 dma_limit) {struct iommu_domain *domain =iommu_get_domain_for_dev (dev);@@ -1320,7 +1320,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)* underlying IOMMU driver needs to support via the dma-iommu layer. */ if (domain->type ==IOMMU_DOMAIN_DMA ) { -if (iommu_dma_init_domain (domain, dma_base, size, dev)) +if (iommu_dma_init_domain (domain, dma_base, dma_limit, dev)) goto out_err; dev->dma_ops = &iommu_dma_ops; }drivers/iommu/intel/iommu.c +1
-4@@ -5165,13 +5165,10 @@ static void intel_iommu_release_device(struct device *dev)static void intel_iommu_probe_finalize (struct device *dev) { -dma_addr_t base =IOVA_START_PFN <<VTD_PAGE_SHIFT ;struct iommu_domain *domain =iommu_get_domain_for_dev (dev); -struct dmar_domain *dmar_domain =to_dmar_domain (domain);if (domain && domain->type ==IOMMU_DOMAIN_DMA ) -iommu_setup_dma_ops (dev, base, -__DOMAIN_MAX_ADDR (dmar_domain->gaw) - base); +iommu_setup_dma_ops (dev,0 ,U64_MAX );else set_dma_ops (dev,NULL ); }
PATCH v5 5/5 iommu/virtio: Enable x86 support
In reply to Jean-Philippe BruckerWith the VIOT support in place, x86 platforms can now use the virtio-iommu.
Because the other x86 IOMMU drivers aren't yet ready to use the acpi_dma_setup() path, x86 doesn't implement arch_setup_dma_ops() at the moment. Similarly to Vt-d and AMD IOMMU, clear the DMA ops and call iommu_setup_dma_ops() from probe_finalize().
--- drivers/iommu/Kconfig | 3 ++- drivers/iommu/dma-iommu.c | 1 + drivers/iommu/virtio-iommu.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-)
drivers/iommu/Kconfig +2 -1
@@ -400,8 +400,9 @@ config HYPERV_IOMMU config VIRTIO_IOMMU tristate "Virtio IOMMU driver" depends on VIRTIO - depends on ARM64 + depends on (ARM64 || X86) select IOMMU_API + select IOMMU_DMA select INTERVAL_TREE select ACPI_VIOT if ACPI help
drivers/iommu/dma-iommu.c +1 -0
@@ -1330,6 +1330,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)pr_warn ("Failed to set up IOMMU for device %s; retaining platform DMA ops\n" ,dev_name (dev)); } +EXPORT_SYMBOL_GPL (iommu_setup_dma_ops);static struct iommu_dma_msi_page *iommu_dma_get_msi_page (struct device *dev,phys_addr_t msi_addr,struct iommu_domain *domain)
drivers/iommu/virtio-iommu.c +11 -0
@@ -10,6 +10,7 @@#include <linux/amba/bus.h> #include <linux/delay.h> #include <linux/dma-iommu.h> +#include <linux/dma-map-ops.h> #include <linux/freezer.h> #include <linux/interval_tree.h> #include <linux/iommu.h>
@@ -904,6 +905,15 @@ static struct iommu_device *viommu_probe_device(struct device *dev)return ERR_PTR (ret); } +static void viommu_probe_finalize (struct device *dev) +{ +#ifndef CONFIG_ARCH_HAS_SETUP_DMA_OPS +/* First clear the DMA ops in case we're switching from a DMA domain */ +set_dma_ops (dev,NULL ); +iommu_setup_dma_ops (dev,0 ,U64_MAX ); +#endif +} +static void viommu_release_device (struct device *dev) {struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev);
@@ -940,6 +950,7 @@ static struct iommu_ops viommu_ops = { .iova_to_phys = viommu_iova_to_phys, .iotlb_sync = viommu_iotlb_sync, .probe_device = viommu_probe_device, + .probe_finalize = viommu_probe_finalize, .release_device = viommu_release_device, .device_group = viommu_device_group, .get_resv_regions = viommu_get_resv_regions,
Signature · 6 lines
2.32.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 7 lines · Jean-Philippe Brucker
Jean-Philippe Brucker (5):
ACPI: arm64: Move DMA setup operations out of IORT
ACPI: Move IOMMU setup code out of IORT
ACPI: Add driver for the VIOT table iommu/dma: Pass address limit rather than size to
iommu_setup_dma_ops()
iommu/virtio: Enable x86 support
Applied all for v5.14, thanks Jean-Philippe for driving this effort and the patience to do it right!