Clean up VMD DMA Map Ops
Subsystems (5)
v2 cover Clean up VMD DMA Map Ops
v1 Set: https://lore.kernel.org/linux-iommu/[email protected]/T/#t
This revision introduces a new weak function to reference the dma alias, as well as using the struct device rather than the pci_dev. By using the weak function in this manner, we remove the need to add a pointer in struct device or pci_dev. Weak functions are generally frowned upon when it's a single architecture implementation, so I am open to alternatives.
v1 Blurb:
VMD currently works with VT-d enabled by pointing DMA and IOMMU actions at the VMD endpoint. The problem with this approach is that the VMD endpoint's device-specific attributes, such as the dma mask, are used instead.
This set cleans up VMD by removing the override that redirects dma map operations to the VMD endpoint. Instead it introduces a new dma alias mechanism into the existing dma alias infrastructure.
Changes from v1:
Removed 1/5 & 2/5 misc fix patches that were merged
Uses Christoph's staging/cleanup patches
Introduce weak function rather than including pointer in struct device or pci_dev.
Based on Joerg's next:
https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/
Christoph Hellwig (2):
x86/pci: Add a to_pci_sysdata helper
x86/pci: Replace the vmd_domain field with a vmd_dev pointer
Jon Derrick (3):
PCI: Introduce direct dma alias
PCI: vmd: Stop overriding dma_map_ops
x86/pci: Remove X86_DEV_DMA_OPS
arch/x86/Kconfig | 3 - arch/x86/include/asm/device.h | 10 --- arch/x86/include/asm/pci.h | 31 ++++----- arch/x86/pci/common.c | 45 ++---------- drivers/iommu/intel-iommu.c | 17 +++-- drivers/pci/controller/Kconfig | 1 - drivers/pci/controller/vmd.c | 152 +---------------------------------------- drivers/pci/pci.c | 17 ++++- drivers/pci/search.c | 9 +++ include/linux/pci.h | 1 + 10 files changed, 60 insertions(+), 226 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/5 x86/pci: Add a to_pci_sysdata helper
From: Christoph Hellwig <[email protected]>
From: Christoph Hellwig <[email protected]>
Various helpers need the pci_sysdata just to dereference a single field in it. Add a little helper that returns the properly typed sysdata pointer to require a little less boilerplate code.
--- arch/x86/include/asm/pci.h | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
arch/x86/include/asm/pci.h +13 -15
@@ -35,12 +35,15 @@ struct pci_sysdata {#ifdef CONFIG_PCI +static inline struct pci_sysdata *to_pci_sysdata (struct pci_bus *bus) +{ +return bus->sysdata; +} +#ifdef CONFIG_PCI_DOMAINS static inline int pci_domain_nr (struct pci_bus *bus) { -struct pci_sysdata *sd = bus->sysdata; - -return sd->domain; +return to_pci_sysdata (bus)->domain; }static inline int pci_proc_domain (struct pci_bus *bus)
@@ -52,23 +55,20 @@ static inline int pci_proc_domain(struct pci_bus *bus)#ifdef CONFIG_PCI_MSI_IRQ_DOMAIN static inline void *_pci_root_bus_fwnode (struct pci_bus *bus) { -struct pci_sysdata *sd = bus->sysdata; - -return sd->fwnode; +return to_pci_sysdata (bus)->fwnode; }#define pci_root_bus_fwnode _pci_root_bus_fwnode#endif +#if IS_ENABLED (CONFIG_VMD )static inline bool is_vmd (struct pci_bus *bus) { -#if IS_ENABLED (CONFIG_VMD ) -struct pci_sysdata *sd = bus->sysdata; - -return sd->vmd_domain; +return to_pci_sysdata (bus)->vmd_domain; +}#else -return false; -#endif +#define is_vmd (bus) false +#endif /* CONFIG_VMD */ }/* Can be used to override the logic in pci_scan_bus for skipping
@@ -124,9 +124,7 @@ static inline void early_quirks(void) { }/* Returns the node based on pci bus */ static inline int __pcibus_to_node (const struct pci_bus *bus) { -const struct pci_sysdata *sd = bus->sysdata; - -return sd->node; +return to_pci_sysdata ((struct pci_bus *) bus)->node; } static inline const struct cpumask *
Signature · 1 line
1.8.3.1
PATCH v2 2/5 x86/pci: Replace the vmd_domain field with a vmd_dev pointer
In reply to Jon DerrickFrom: Christoph Hellwig <[email protected]>
From: Christoph Hellwig <[email protected]>
Store the actual VMD device in struct pci_sysdata, so that we can later use it directly for DMA mappings.
--- arch/x86/include/asm/pci.h | 5 ++--- drivers/pci/controller/vmd.c | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-)
arch/x86/include/asm/pci.h +2 -3
@@ -25,7 +25,7 @@ struct pci_sysdata {void *fwnode;/* IRQ domain for MSI assignment */ #endif #if IS_ENABLED (CONFIG_VMD ) -bool vmd_domain;/* True if in Intel VMD domain */ +struct device *vmd_dev;/* Main device if in Intel VMD domain */ #endif };
@@ -64,12 +64,11 @@ static inline void *_pci_root_bus_fwnode(struct pci_bus *bus)#if IS_ENABLED (CONFIG_VMD )static inline bool is_vmd (struct pci_bus *bus) { -return to_pci_sysdata (bus)->vmd_domain; +return to_pci_sysdata (bus)->vmd_dev !=NULL ; }#else #define is_vmd (bus) false#endif /* CONFIG_VMD */ -}/* Can be used to override the logic in pci_scan_bus for skipping already-configured bus numbers - to be used for buggy BIOSes
drivers/pci/controller/vmd.c +1 -1
@@ -679,7 +679,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) .parent = res, }; - sd->vmd_domain = true; + sd->vmd_dev = &vmd->dev->dev; sd->domain =vmd_find_free_domain ();if (sd->domain <0 )return sd->domain;
Signature · 1 line
1.8.3.1
PATCH v2 3/5 PCI: Introduce direct dma alias
In reply to Jon DerrickThe current dma alias implementation requires the aliased device be on the same bus as the dma parent. This introduces an arch-specific mechanism to point to an arbitrary struct device when doing mapping and pci alias search.
--- arch/x86/pci/common.c | 7 +++++++ drivers/pci/pci.c | 17 ++++++++++++++++- drivers/pci/search.c | 9 +++++++++ include/linux/pci.h | 1 + 4 files changed, 33 insertions(+), 1 deletion(-)
arch/x86/pci/common.c +7 -0
@@ -736,3 +736,10 @@ int pci_ext_cfg_avail(void)else return 0 ; } + +#if IS_ENABLED (CONFIG_VMD ) +struct device *pci_direct_dma_alias (struct pci_dev *dev) +{ +return to_pci_sysdata (dev->bus)->vmd_dev; +} +#endif
drivers/pci/pci.c +16 -1
@@ -6034,7 +6034,9 @@ bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)return (dev1->dma_alias_mask &&test_bit (dev2->devfn, dev1->dma_alias_mask)) || (dev2->dma_alias_mask && -test_bit (dev1->devfn, dev2->dma_alias_mask)); +test_bit (dev1->devfn, dev2->dma_alias_mask)) || + (pci_direct_dma_alias (dev1) == &dev2->dev) || + (pci_direct_dma_alias (dev2) == &dev1->dev); }bool pci_device_is_present (struct pci_dev *pdev)
@@ -6058,6 +6060,19 @@ void pci_ignore_hotplug(struct pci_dev *dev) }EXPORT_SYMBOL_GPL (pci_ignore_hotplug); +/** +* pci_direct_dma_alias - Get dma alias for pci device +* @dev: the PCI device that may have a dma alias +* +* Permits the platform to provide architecture-specific functionality to +* devices needing to alias dma to another device. This is the default +* implementation. Architecture implementations can override this. +*/ +struct device __weak *pci_direct_dma_alias (struct pci_dev *dev) +{ +return NULL ; +} +resource_size_t __weak pcibios_default_alignment (void ) {return 0 ;
drivers/pci/search.c +9 -0
@@ -32,6 +32,15 @@ int pci_for_each_dma_alias(struct pci_dev *pdev,struct pci_bus *bus;int ret; +if (unlikely (pci_direct_dma_alias (pdev))) { +struct device *dev =pci_direct_dma_alias (pdev); + +if (dev_is_pci (dev)) + pdev =to_pci_dev (dev); +return fn (pdev,PCI_DEVID (pdev->bus->number, pdev->devfn), + data); + } + ret =fn (pdev,pci_dev_id (pdev), data);if (ret)return ret;
include/linux/pci.h +1 -0
@@ -1202,6 +1202,7 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,int pci_select_bars (struct pci_dev *dev,unsigned long flags);bool pci_device_is_present (struct pci_dev *pdev);void pci_ignore_hotplug (struct pci_dev *dev); +struct device *pci_direct_dma_alias (struct pci_dev *dev); int __printf(6, 7) pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler, irq_handler_t thread_fn, void *dev_id,
Signature · 1 line
1.8.3.1
Re: [PATCH v2 3/5] PCI: Introduce direct dma alias
In subject:
s/Introduce direct dma alias/Add pci_direct_dma_alias()/
Quoted 4 lines · Jon Derrick
The current dma alias implementation requires the aliased device be on the same bus as the dma parent. This introduces an arch-specific mechanism to point to an arbitrary struct device when doing mapping and pci alias search.
"arbitrary struct device" is a little weird since an arbitrary device doesn't have to be a PCI device, but these mappings and aliases only make sense in the PCI domain.
Maybe it has something to do with pci_sysdata.vmd_dev being a "struct device *" rather than a "struct pci_dev *"? I don't know why that is, because it looks like every place you use it, you use to_pci_dev() to get the pci_dev pointer back anyway. But I assume you have some good reason for that.
s/dma/DMA/
s/pci/PCI/
(above and also in code comments below)
Quoted 93 lines
Signed-off-by Jon Derrick <[email protected]>--- arch/x86/pci/common.c | 7 +++++++ drivers/pci/pci.c | 17 ++++++++++++++++- drivers/pci/search.c | 9 +++++++++ include/linux/pci.h | 1 + 4 files changed, 33 insertions(+), 1 deletion(-)arch/x86/pci/common.c +7
-0@@ -736,3 +736,10 @@ int pci_ext_cfg_avail(void)else return 0 ; } + +#if IS_ENABLED (CONFIG_VMD ) +struct device *pci_direct_dma_alias (struct pci_dev *dev) +{ +return to_pci_sysdata (dev->bus)->vmd_dev; +} +#endif drivers/pci/pci.c +16
-1@@ -6034,7 +6034,9 @@ bool pci_devs_are_dma_aliases(struct pci_dev *dev1, struct pci_dev *dev2)return (dev1->dma_alias_mask &&test_bit (dev2->devfn, dev1->dma_alias_mask)) || (dev2->dma_alias_mask && -test_bit (dev1->devfn, dev2->dma_alias_mask)); +test_bit (dev1->devfn, dev2->dma_alias_mask)) || + (pci_direct_dma_alias (dev1) == &dev2->dev) || + (pci_direct_dma_alias (dev2) == &dev1->dev); }bool pci_device_is_present (struct pci_dev *pdev)@@ -6058,6 +6060,19 @@ void pci_ignore_hotplug(struct pci_dev *dev) }EXPORT_SYMBOL_GPL (pci_ignore_hotplug); +/** +* pci_direct_dma_alias - Get dma alias for pci device +* @dev: the PCI device that may have a dma alias +* +* Permits the platform to provide architecture-specific functionality to +* devices needing to alias dma to another device. This is the default +* implementation. Architecture implementations can override this. +*/ +struct device __weak *pci_direct_dma_alias (struct pci_dev *dev) +{ +return NULL ; +} +resource_size_t __weak pcibios_default_alignment (void ) {return 0 ;drivers/pci/search.c +9
-0@@ -32,6 +32,15 @@ int pci_for_each_dma_alias(struct pci_dev *pdev,struct pci_bus *bus;int ret; +if (unlikely (pci_direct_dma_alias (pdev))) { +struct device *dev =pci_direct_dma_alias (pdev); + +if (dev_is_pci (dev)) + pdev =to_pci_dev (dev); +return fn (pdev,PCI_DEVID (pdev->bus->number, pdev->devfn), + data); + } + ret =fn (pdev,pci_dev_id (pdev), data);if (ret)return ret;include/linux/pci.h +1
-0@@ -1202,6 +1202,7 @@ u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev,int pci_select_bars (struct pci_dev *dev,unsigned long flags);bool pci_device_is_present (struct pci_dev *pdev);void pci_ignore_hotplug (struct pci_dev *dev); +struct device *pci_direct_dma_alias (struct pci_dev *dev); int __printf(6, 7) pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler, irq_handler_t thread_fn, void *dev_id,Signature · 1 line
1.8.3.1
Re: [PATCH v2 3/5] PCI: Introduce direct dma alias
Quoted 13 lines · Bjorn Helgaas
In subject:
s/Introduce direct dma alias/Add pci_direct_dma_alias()/Quoted 4 lines · Jon Derrick
The current dma alias implementation requires the aliased device be on the same bus as the dma parent. This introduces an arch-specific mechanism to point to an arbitrary struct device when doing mapping and pci alias search.
"arbitrary struct device" is a little weird since an arbitrary device doesn't have to be a PCI device, but these mappings and aliases only make sense in the PCI domain.
Quoted 5 lines
Maybe it has something to do with pci_sysdata.vmd_dev being a "struct device *" rather than a "struct
pci_dev*"? I don't know why that is, because it looks like every place you use it, you useto_pci_dev()to get thepci_devpointer back anyway. But I assume you have some good reason for that.
No particular reason other than to align with the suggestion in the last set to be using the struct device. It does make sense to reference the struct device as that provides the dma context, however as you have pointed out, the implementation here moreso needs the device's pci_dev.
I'll see how it looks for the next set.
Quoted 6 lines
s/dma/DMA/
s/pci/PCI/
(above and also in code comments below)Quoted 1 line
PATCH v2 4/5 PCI: vmd: Stop overriding dma_map_ops
In reply to Jon DerrickDevices on the VMD domain use the VMD endpoint's requester-id and have been relying on the VMD endpoint's dma operations. The downside of this was that VMD domain devices would use the VMD endpoint's attributes when doing DMA and IOMMU mapping. We can be smarter about this by only using the VMD endpoint when mapping and providing the correct child device's attributes during dma operations.
This patch modifies intel-iommu to check for a 'direct dma alias' and refer to it for mapping.
--- drivers/iommu/intel-iommu.c | 17 +++-- drivers/pci/controller/Kconfig | 1 - drivers/pci/controller/vmd.c | 150 ----------------------------------------- 3 files changed, 12 insertions(+), 156 deletions(-)
drivers/iommu/intel-iommu.c +12 -5
@@ -773,14 +773,13 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfif (dev_is_pci (dev)) {struct pci_dev *pf_pdev; +struct device *dma_alias; pdev =to_pci_dev (dev); -#ifdef CONFIG_X86 -/* VMD child devices currently cannot be handled individually */ -if (is_vmd (pdev->bus)) -return NULL ; -#endif + dma_alias =pci_direct_dma_alias (pdev); +if (dma_alias &&dev_is_pci (dma_alias)) + pdev =to_pci_dev (dma_alias);/* VFs aren't listed in scope tables; we need to look up * the PF instead to find the IOMMU. */
@@ -2428,6 +2427,14 @@ static struct dmar_domain *find_domain(struct device *dev) dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO))return NULL ; +if (dev_is_pci (dev)) { +struct pci_dev *pdev =to_pci_dev (dev); +struct device *dma_alias =pci_direct_dma_alias (pdev); + +if (dma_alias) + dev = dma_alias; + } +/* No lock here, assumes no domain exit in normal case */ info = dev->archdata.iommu;if (likely (info))
drivers/pci/controller/Kconfig +0 -1
@@ -239,7 +239,6 @@ config PCIE_TANGO_SMP8759 config VMD depends on PCI_MSI && X86_64 && SRCU - select X86_DEV_DMA_OPS tristate "Intel Volume Management Device Driver" ---help--- Adds support for the Intel Volume Management Device (VMD). VMD is a
drivers/pci/controller/vmd.c +0 -150
174 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 5/5 x86/pci: Remove X86_DEV_DMA_OPS
In reply to Jon DerrickFrom: Christoph Hellwig <[email protected]>
There are no users of X86_DEV_DMA_OPS left, so remove the code.
--- arch/x86/Kconfig | 3 --- arch/x86/include/asm/device.h | 10 ---------- arch/x86/pci/common.c | 38 -------------------------------------- 3 files changed, 51 deletions(-)
arch/x86/Kconfig +0 -3
@@ -2955,9 +2955,6 @@ config HAVE_ATOMIC_IOMAP def_bool y depends on X86_32 -config X86_DEV_DMA_OPS - bool - source "drivers/firmware/Kconfig" source "arch/x86/kvm/Kconfig"
arch/x86/include/asm/device.h +0 -10
@@ -8,16 +8,6 @@ struct dev_archdata {#endif }; -#if defined(CONFIG_X86_DEV_DMA_OPS ) && defined(CONFIG_PCI_DOMAINS ) -struct dma_domain { -struct list_head node; -const struct dma_map_ops *dma_ops; -int domain_nr; -}; -void add_dma_domain (struct dma_domain *domain); -void del_dma_domain (struct dma_domain *domain); -#endif -struct pdev_archdata { };
arch/x86/pci/common.c +0 -38
@@ -625,43 +625,6 @@ unsigned int pcibios_assign_all_busses(void)return (pci_probe &PCI_ASSIGN_ALL_BUSSES ) ?1 :0 ; } -#if defined(CONFIG_X86_DEV_DMA_OPS ) && defined(CONFIG_PCI_DOMAINS ) -static LIST_HEAD (dma_domain_list); -static DEFINE_SPINLOCK (dma_domain_list_lock); - -void add_dma_domain (struct dma_domain *domain) -{ -spin_lock (&dma_domain_list_lock); -list_add (&domain->node, &dma_domain_list); -spin_unlock (&dma_domain_list_lock); -} -EXPORT_SYMBOL_GPL (add_dma_domain); - -void del_dma_domain (struct dma_domain *domain) -{ -spin_lock (&dma_domain_list_lock); -list_del (&domain->node); -spin_unlock (&dma_domain_list_lock); -} -EXPORT_SYMBOL_GPL (del_dma_domain); - -static void set_dma_domain_ops (struct pci_dev *pdev) -{ -struct dma_domain *domain; - -spin_lock (&dma_domain_list_lock); -list_for_each_entry (domain, &dma_domain_list, node) { -if (pci_domain_nr (pdev->bus) == domain->domain_nr) { - pdev->dev.dma_ops = domain->dma_ops; -break ; - } - } -spin_unlock (&dma_domain_list_lock); -} -#else -static void set_dma_domain_ops (struct pci_dev *pdev) {} -#endif -static void set_dev_domain_options (struct pci_dev *pdev) {if (is_vmd (pdev->bus))
@@ -697,7 +660,6 @@ int pcibios_add_device(struct pci_dev *dev) pa_data = data->next;memunmap (data); } -set_dma_domain_ops (dev);set_dev_domain_options (dev);return 0 ; }
Signature · 1 line
1.8.3.1