IPMMU handling for PCIe Passthrough on ARM
v3 cover IPMMU handling for PCIe Passthrough on ARM
This series introduces IPMMU handling for PCIe passthrough on ARM. It includes changes to pci-designware, pci-host-rcar and ipmmu-vmsa drivers to enable configuring BDF->OSID->uTLB translation chain needed to pass different PCIe devices to different domains.
Tested on RCar S4 Spider board.
v2->v3:
* see individual patches
v1->v2:
* see individual patches
Mykyta Poturai (2):
arm/pci: allow designware-based hosts to have private data pci/rcar: implement OSID configuration for Renesas RCar Gen4 PCIe host
Oleksandr Tyshchenko (1):
iommu/ipmmu-vmsa: Implement basic PCIE-IPMMU OSID support
xen/arch/arm/pci/pci-designware.c | 12 ++ xen/arch/arm/pci/pci-designware.h | 4 + xen/arch/arm/pci/pci-host-rcar4.c | 148 +++++++++++++++++++++++ xen/arch/arm/pci/pci-host-rcar4.h | 18 +++ xen/drivers/passthrough/arm/ipmmu-vmsa.c | 138 +++++++++++++++++++-- 5 files changed, 312 insertions(+), 8 deletions(-) create mode 100644 xen/arch/arm/pci/pci-host-rcar4.h
Signature · 1 line
2.34.1
PATCH v3 1/3 arm/pci: allow designware-based hosts to have private data
Introduce an additional private data field in dw_pcie_priv to allow vendors to store custom data without interfering with bridge->priv. Also add get/set pair to make accesing that private data less cumbersome.
---v2->v3:
* no changes
v1->v2:
* add Stefano's AB
--- xen/arch/arm/pci/pci-designware.c | 12 ++++++++++++ xen/arch/arm/pci/pci-designware.h | 4 ++++ 2 files changed, 16 insertions(+)
xen/arch/arm/pci/pci-designware.c +12 -0
@@ -403,3 +403,15 @@ dw_pcie_host_probe(struct dt_device_node *dev, const void *data,return bridge; } + +void *dw_pcie_get_priv (struct pci_host_bridge *bridge) +{ +struct dw_pcie_priv *priv = bridge->priv; +return priv->priv; +} + +void dw_pcie_set_priv (struct pci_host_bridge *bridge,void *other) +{ +struct dw_pcie_priv *priv = bridge->priv; + priv->priv = other; +}
xen/arch/arm/pci/pci-designware.h +4 -0
@@ -66,8 +66,12 @@ struct dw_pcie_priv {bool iatu_unroll_enabled;void __iomem *atu_base;unsigned int version; +void *priv; }; +void *dw_pcie_get_priv (struct pci_host_bridge *bridge); +void dw_pcie_set_priv (struct pci_host_bridge *bridge,void *other); +void dw_pcie_set_version (struct pci_host_bridge *bridge,unsigned int version); void __iomem *dw_pcie_child_map_bus(struct pci_host_bridge *bridge,
Signature · 1 line
2.34.1
PATCH v3 2/3 pci/rcar: implement OSID configuration for Renesas RCar Gen4 PCIe host
In reply to Mykyta PoturaiFor IPMMU to be able to associate a specific PCI device with it's TLB the BDF to OSID mapping needs to be set up in the host bridge. The configured OSID is then emmited as a sideband data on the AXI bus during PCI DMA transactions. OSID configuration registers are located in the "app" region of the host bridge.
Map the "app" region on init and implement methods for setting up BDF->OSID mappings
---v2->v3:
* no changes
v1->v2:
* add Stefano's AB
--- xen/arch/arm/pci/pci-host-rcar4.c | 148 ++++++++++++++++++++++++++++++ xen/arch/arm/pci/pci-host-rcar4.h | 18 ++++ 2 files changed, 166 insertions(+) create mode 100644 xen/arch/arm/pci/pci-host-rcar4.h
xen/arch/arm/pci/pci-host-rcar4.c +148 -0
@@ -16,6 +16,32 @@#define RCAR4_DWC_VERSION 0x520A +/* PCIE BDF-OSID assignment */ +#define CNVID (n) (0x700 + ((n) * 4)) +#define CNVID_CNV_EN (1U << 31) +#define CNVID_OSID_MASK (0x0F << 16) +#define CNVID_OSID_SHIFT 16 +#define CNVID_BDF_MASK (0xFFFF << 0) +#define CNVID_BDF_SHIFT 0 + +#define CNVIDMSK (n) (0x780 + ((n) * 4)) +#define CNVIDMSK_BDF_MSK_MASK (0xFFFF << 0) +#define CNVIDMSK_BDF_MSK_SHIFT 0 + +#define CNVOSIDCTRL 0x800 +#define CNVOSIDCTRL_OSID_MASK (0x0F << 16) +#define CNVOSIDCTRL_OSID_SHIFT 16 + +#define DEFAULT_OSID 0 + +#define NUM_OSID_REGS 16 + +struct rcar4_pcie_priv { +bool init_done; +void __iomem *app_base; + DECLARE_BITMAP(osid_regs, NUM_OSID_REGS); +}; +/* * PCI host bridges often have different ways to access the root and child * bus config spaces:
@@ -65,17 +91,139 @@ static const struct dt_device_match __initconstrel rcar4_pcie_dt_match[] = { {}, }; +static void rcar4_pcie_writel_app (struct rcar4_pcie_priv *pci,uint32_t reg, +uint32_t val) +{ +writel (val, pci->app_base + reg); +} + +static uint32_t rcar4_pcie_readl_app (struct rcar4_pcie_priv *pci,uint32_t reg) +{ +return readl (pci->app_base + reg); +} + +int rcar4_pcie_osid_regs_init (struct pci_host_bridge *bridge) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); +uint32_t val =rcar4_pcie_readl_app (priv,CNVOSIDCTRL ); + +if ( priv->init_done ) +return 0 ; + priv->init_done = true; + + val = (val & ~CNVOSIDCTRL_OSID_MASK ) | + (DEFAULT_OSID <<CNVOSIDCTRL_OSID_SHIFT ); +rcar4_pcie_writel_app (priv,CNVOSIDCTRL , val); +bitmap_zero (priv->osid_regs,NUM_OSID_REGS ); + +printk ("%s: Initialized OSID regs (default OSID %u)\n" , + bridge->dt_node->full_name,DEFAULT_OSID ); + +return 0 ; +} + +int rcar4_pcie_osid_reg_alloc (struct pci_host_bridge *bridge) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); +int ret; + + ret =find_first_zero_bit (priv->osid_regs,NUM_OSID_REGS ); +if ( ret !=NUM_OSID_REGS ) +set_bit (ret, priv->osid_regs); +else + ret = -EBUSY ; + +return ret; +} + +void rcar4_pcie_osid_reg_free (struct pci_host_bridge *bridge, +unsigned int reg_id) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); + +clear_bit (reg_id, priv->osid_regs); +} + +void rcar4_pcie_osid_bdf_set (struct pci_host_bridge *bridge, +unsigned int reg_id,uint32_t osid,uint32_t bdf) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); +uint32_t data =rcar4_pcie_readl_app (priv,CNVID (reg_id)); + + data &= ~(CNVID_OSID_MASK |CNVID_BDF_MASK ); + data |=CNVID_CNV_EN | (osid <<CNVID_OSID_SHIFT ) | + (bdf <<CNVID_BDF_SHIFT ); +rcar4_pcie_writel_app (priv,CNVID (reg_id), data); +} + +void rcar4_pcie_osid_bdf_clear (struct pci_host_bridge *bridge, +unsigned int reg_id) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); +uint32_t data =rcar4_pcie_readl_app (priv,CNVID (reg_id)); + + data &= ~CNVID_CNV_EN ; +rcar4_pcie_writel_app (priv,CNVID (reg_id), data); +} + +void rcar4_pcie_bdf_msk_set (struct pci_host_bridge *bridge,unsigned int reg_id, +uint32_t data) +{ +struct rcar4_pcie_priv *priv =dw_pcie_get_priv (bridge); + +uint32_t val =rcar4_pcie_readl_app (priv,CNVIDMSK (reg_id)); + + val = (val & ~CNVIDMSK_BDF_MSK_MASK ) | (data <<CNVIDMSK_BDF_MSK_SHIFT ); + +rcar4_pcie_writel_app (priv,CNVIDMSK (reg_id), val); +} +static int __init pci_host_rcar4_probe (struct dt_device_node *dev,const void *data) {struct pci_host_bridge *bridge; +paddr_t app_phys_addr; +paddr_t app_size; +int app_idx, ret; + + struct rcar4_pcie_priv *priv = xzalloc(struct rcar4_pcie_priv); +if ( !priv ) +return -ENOMEM ; bridge =dw_pcie_host_probe (dev, data, &rcar4_pcie_ops, &rcar4_pcie_child_ops); + app_idx =dt_property_match_string (dev,"reg-names" ,"app" ); +if ( app_idx <0 ) + { +printk (XENLOG_ERR "Cannot find \"app\" range index in device tree\n" ); + ret = app_idx; + goto err; + } + ret =dt_device_get_address (dev, app_idx, &app_phys_addr, &app_size); +if ( ret ) + { +printk (XENLOG_ERR "Cannot find \"app\" range in device tree\n" ); + goto err; + } + + priv->app_base =ioremap_nocache (app_phys_addr, app_size); +if ( !priv->app_base ) + { +printk (XENLOG_ERR "APP ioremap failed\n" ); + ret = -ENXIO ; + goto err; + } +printk ("APP at [mem 0x%" PRIpaddr"-0x%" PRIpaddr"]\n" , app_phys_addr, + app_phys_addr + app_size -1 ); + +dw_pcie_set_priv (bridge, priv);dw_pcie_set_version (bridge,RCAR4_DWC_VERSION );return 0 ; +err: +xfree (priv); +return ret; }DT_DEVICE_START (pci_gen,"PCI HOST R-CAR GEN4" ,DEVICE_PCI_HOSTBRIDGE )
xen/arch/arm/pci/pci-host-rcar4.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <asm/pci.h> + +#ifndef __PCI_HOST_RCAR4_H__ +#define __PCI_HOST_RCAR4_H__ + +void rcar4_pcie_osid_bdf_set (struct pci_host_bridge *bridge, +unsigned int reg_id,uint32_t osid,uint32_t bdf); +void rcar4_pcie_osid_bdf_clear (struct pci_host_bridge *bridge, +unsigned int reg_id); +void rcar4_pcie_bdf_msk_set (struct pci_host_bridge *bridge,unsigned int reg_id, +uint32_t data); +int rcar4_pcie_osid_reg_alloc (struct pci_host_bridge *bridge); +void rcar4_pcie_osid_reg_free (struct pci_host_bridge *bridge, +unsigned int reg_id); +int rcar4_pcie_osid_regs_init (struct pci_host_bridge *bridge); + +#endif /* __PCI_HOST_RCAR4_H__ */
Signature · 1 line
2.34.1
PATCH v3 3/3 iommu/ipmmu-vmsa: Implement basic PCIE-IPMMU OSID support
In reply to Mykyta PoturaiFrom: Oleksandr Tyshchenko <[email protected]>
Program PCIE BDF-OSID assignment according to the S4_PCIe_IPMMU-OSID when adding PCI device to the IOMMU in ipmmu_add_device callback. This is needed for being able to assign PCI devices to different domains at the same time. Programmed OSID is emmited as sideband data on the AXI bus during PCI DMA transactions and then used by IPMMU to match the transaction to the corresponding uTLB.
---v2->v3:
* fix compilation error with HAS_PCI=n
* add Stefano's AB
v1->v2:
* rework VF handling to be in line with SMMU drivers
* add locking to domain_list manipulations
* remove assignment print
--- xen/drivers/passthrough/arm/ipmmu-vmsa.c | 138 +++++++++++++++++++++-- 1 file changed, 130 insertions(+), 8 deletions(-)
xen/drivers/passthrough/arm/ipmmu-vmsa.c +130 -8
@@ -51,12 +51,27 @@#include <asm/device.h> #include <asm/io.h> #include <asm/iommu_fwspec.h> +#ifdef CONFIG_HAS_PCI +#include "../arch/arm/pci/pci-host-rcar4.h" +#endif #define dev_name (dev) dt_node_full_name(dev_to_dt(dev))/* Device logger functions */ +#ifndef CONFIG_HAS_PCI #define dev_print (dev, lvl, fmt, ...) \ printk(lvl "ipmmu: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#else +#define dev_print (dev, lvl, fmt, ...) ({ \ + if ( !dev_is_pci((dev)) ) \ + printk(lvl "ipmmu: %s: " fmt, dev_name((dev)), ## __VA_ARGS__); \ + else \ + { \ + struct pci_dev *pdev = dev_to_pci((dev)); \ + printk(lvl "ipmmu: %pp: " fmt, &pdev->sbdf, ## __VA_ARGS__); \ + } \ +}) +#endif #define dev_info (dev, fmt, ...) \ dev_print(dev, XENLOG_INFO, fmt, ## __VA_ARGS__)
@@ -1125,6 +1140,8 @@ static void ipmmu_free_root_domain(struct ipmmu_vmsa_domain *domain)xfree (domain); } +static int ipmmu_deassign_device (struct domain *d,struct device *dev); +static int ipmmu_assign_device (struct domain *d,u8 devfn,struct device *dev,uint32_t flag) {
@@ -1138,8 +1155,38 @@ static int ipmmu_assign_device(struct domain *d, u8 devfn, struct device *dev,if ( !to_ipmmu (dev) )return -ENODEV ; -spin_lock (&xen_domain->lock); +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); +struct domain *old_d = pdev->domain; + +/* Ignore calls for phantom functions */ +if ( devfn != pdev->devfn ) +return 0 ; + +write_lock (&d->pci_lock); +list_move (&pdev->domain_list, &d->pdev_list); +write_unlock (&d->pci_lock); + pdev->domain = d; + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) + { +int ret; + +/* +* Try to de-assign: do not return error if it was already +* de-assigned. +*/ + ret =ipmmu_deassign_device (old_d, dev); + +return ret == -ESRCH ?0 : ret; + } + } +#endif +spin_lock (&xen_domain->lock);/* * The IPMMU context for the Xen domain is not allocated beforehand * (at the Xen domain creation time), but on demand only, when the first
@@ -1244,7 +1291,7 @@ static int ipmmu_reassign_device(struct domain *s, struct domain *t,int ret =0 ;/* Don't allow remapping on other domain than hwdom */ -if ( t && !is_hardware_domain (t) ) +if ( t && !is_hardware_domain (t) && (t != dom_io) )return -EPERM ;if ( t == s )
@@ -1292,20 +1339,95 @@ static int ipmmu_dt_xlate(struct device *dev,static int ipmmu_add_device (u8 devfn,struct device *dev) { -struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); +struct iommu_fwspec *fwspec; + +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); +int ret; + +if ( devfn != pdev->devfn ) +return 0 ; + + ret =iommu_add_pci_sideband_ids (pdev); +if ( ret <0 ) +iommu_fwspec_free (dev); + } +#endif + + fwspec =dev_iommu_fwspec_get (dev);/* Only let through devices that have been verified in xlate(). */ if ( !to_ipmmu (dev) )return -ENODEV ; -if (dt_device_is_protected (dev_to_dt (dev)) ) +if ( !dev_is_pci (dev) ) { -dev_err (dev,"Already added to IPMMU\n" ); -return -EEXIST ; +if (dt_device_is_protected (dev_to_dt (dev)) ) + { +dev_err (dev,"Already added to IPMMU\n" ); +return -EEXIST ; + } + +/* Let Xen know that the master device is protected by an IOMMU. */ +dt_device_set_protected (dev_to_dt (dev)); } +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); +unsigned int reg_id, osid; +struct pci_host_bridge *bridge; +struct iommu_fwspec *fwspec_bridge; +unsigned int utlb_osid0 =0 ; +int ret; + + bridge =pci_find_host_bridge (pdev->seg, pdev->bus); +if ( !bridge ) + { +dev_err (dev,"Failed to find host bridge\n" ); +return -ENODEV ; + } + + fwspec_bridge =dev_iommu_fwspec_get (dt_to_dev (bridge->dt_node)); +if ( fwspec_bridge->num_ids <1 ) + { +dev_err (dev,"Failed to find host bridge uTLB\n" ); +return -ENXIO ; + } + +if ( fwspec->num_ids <1 ) + { +dev_err (dev,"Failed to find uTLB" ); +return -ENXIO ; + } + +rcar4_pcie_osid_regs_init (bridge); + + ret =rcar4_pcie_osid_reg_alloc (bridge); +if ( ret <0 ) + { +dev_err (dev,"No unused OSID regs\n" ); +return ret; + } + reg_id = ret; + + osid = fwspec->ids[0 ] - utlb_osid0; +rcar4_pcie_osid_bdf_set (bridge, reg_id, osid, pdev->sbdf.bdf); +rcar4_pcie_bdf_msk_set (bridge, reg_id,0 ); + +dev_info (dev,"Allocated OSID reg %u (OSID %u)\n" , reg_id, osid); -/* Let Xen know that the master device is protected by an IOMMU. */ -dt_device_set_protected (dev_to_dt (dev)); + ret =ipmmu_assign_device (pdev->domain, devfn, dev,0 ); +if ( ret ) + { +rcar4_pcie_osid_bdf_clear (bridge, reg_id); +rcar4_pcie_osid_reg_free (bridge, reg_id); +return ret; + } + } +#endif dev_info (dev,"Added master device (IPMMU %s micro-TLBs %u)\n" ,dev_name (fwspec->iommu_dev), fwspec->num_ids);
Signature · 1 line
2.34.1
Re: [PATCH v3 3/3] iommu/ipmmu-vmsa: Implement basic PCIE-IPMMU OSID support
Quoted 31 lines · Mykyta Poturai
xen/drivers/passthrough/arm/ipmmu-vmsa.c +22
-1@@ -1138,8 +1155,38 @@ static int ipmmu_assign_device(struct domain *d, u8 devfn, struct device *dev,if ( !to_ipmmu (dev) )return -ENODEV ; -spin_lock (&xen_domain->lock); +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); +struct domain *old_d = pdev->domain; + +/* Ignore calls for phantom functions */ +if ( devfn != pdev->devfn ) +return 0 ; + +write_lock (&d->pci_lock); +list_move (&pdev->domain_list, &d->pdev_list); +write_unlock (&d->pci_lock); + pdev->domain = d; + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) + { +int ret; + +/* +* Try to de-assign: do not return error if it was already
xen/drivers/passthrough/arm/ipmmu-vmsa.c static int ipmmu_assign_device(struct domain *d, u8 devfn, struct device *dev, · line 1189
Quoted 10 lines
xen/drivers/passthrough/arm/ipmmu-vmsa.c +9
-0+* de-assigned. +*/ + ret =ipmmu_deassign_device (old_d, dev); + + return ret == -ESRCH ? 0 : ret; + } + } +#endif +spin_lock (&xen_domain->lock);
Minor point. You've removed a newline here, which is why the hunk is not cleanly ahead of the spin_lock() call.
~Andrew
Quoted 13 lines · Mykyta Poturai
This series introduces IPMMU handling for PCIe passthrough on ARM. It includes changes to pci-designware, pci-host-rcar and ipmmu-vmsa drivers to enable configuring
BDF->OSID->uTLBtranslation chain needed to pass different PCIe devices to different domains.Tested on RCar S4 Spider board.
v2->v3:
* see individual patches
v1->v2:
* see individual patches
Quoted 6 lines
Mykyta Poturai (2):
arm/pci: allow designware-based hosts to have private data pci/rcar: implement OSID configuration for Renesas RCar Gen4 PCIe hostOleksandr Tyshchenko (1):
iommu/ipmmu-vmsa: Implement basic PCIE-IPMMU OSID support
These patches went in already, one causing a randconfig issue. What is this re-posting about? Are you implying that the entire series is going to be reverted? (I really expected an incremental patch to deal with the issue CI ran into.)
Jan
Quoted 21 lines · Jan Beulich
Quoted 13 lines · Mykyta Poturai
This series introduces IPMMU handling for PCIe passthrough on ARM. It includes changes to pci-designware, pci-host-rcar and ipmmu-vmsa drivers to enable configuring
BDF->OSID->uTLBtranslation chain needed to pass different PCIe devices to different domains.Tested on RCar S4 Spider board.
v2->v3:
* see individual patches
v1->v2:
* see individual patchesQuoted 6 lines
Mykyta Poturai (2):
arm/pci: allow designware-based hosts to have private data pci/rcar: implement OSID configuration for Renesas RCar Gen4 PCIe hostOleksandr Tyshchenko (1):
iommu/ipmmu-vmsa: Implement basic PCIE-IPMMU OSID support
Quoted 6 lines
These patches went in already, one causing a randconfig issue. What is this re-posting about? Are you implying that the entire series is going to be reverted? (I really expected an incremental patch to deal with the issue CI ran into.)
Jan
Oops sorry, I've got an idea that it was a pre-commit CI job (as the patches are not yet in the master branch). Please ignore this series, I will send an incremental patch shortly.
Signature · 1 line
Mykyta