SMMU handling for PCIe Passthrough on ARM
v6 cover SMMU handling for PCIe Passthrough on ARM
This series introduces SMMU handling for PCIe passthrough on ARM. These patches should be able to be upstreamed independently from the vPCI series [1]. See [2] for notes about test cases.
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-10/msg00660.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-06/msg01135.html
v5->v6:
* don't revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: enable dom0 to use PCI devices with pci-passthrough=no")
v4->v5:
* drop ("xen/arm: Improve readability of check for registered devices")
* drop ("xen/arm: Move is_protected flag to struct device")
* add ("xen/arm: don't pass iommu properties to hwdom for iommu-map")
* add ("xen/arm: Fix mapping for PCI bridge mmio region")
* revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: Map ITS doorbell register to IOMMU page tables.")
* fix test case #1 with PCI device in dom0
v3->v4:
* split a change from ("xen/arm: Move is_protected flag to struct device") into a new separate patch
* see individual patches for further details
v2->v3:
* drop "pci/arm: Use iommu_add_dt_pci_device()"
* drop "RFC: pci/arm: don't do iommu call for phantom functions"
* move invocation of sideband ID mapping function to add_device() platform_ops/iommu_ops hook
v1->v2:
* phantom device handling
* shuffle around iommu_add_dt_pci_device()
Oleksandr Andrushchenko (1):
xen/arm: smmuv2: Add PCI devices support for SMMUv2
Oleksandr Tyshchenko (2):
iommu/arm: Add iommu_dt_xlate()
iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API
Rahul Singh (3):
xen/arm: smmuv3: Add PCI devices support for SMMUv3 xen/arm: Fix mapping for PCI bridge mmio region xen/arm: Map ITS doorbell register to IOMMU page tables
Stewart Hildebrand (3):
xen/arm: don't pass iommu properties to hwdom for iommu-map iommu/arm: iommu_add_dt_pci_sideband_ids phantom handling xen/arm: enable dom0 to use PCI devices with pci-passthrough=no
xen/arch/arm/device.c | 2 +- xen/arch/arm/domain_build.c | 2 + xen/arch/arm/pci/pci.c | 3 +- xen/arch/arm/vgic-v3-its.c | 15 ++ xen/common/device_tree.c | 91 ++++++++++++ xen/drivers/passthrough/arm/smmu-v3.c | 131 +++++++++++++++-- xen/drivers/passthrough/arm/smmu.c | 199 ++++++++++++++++++++++---- xen/drivers/passthrough/device_tree.c | 97 ++++++++++--- xen/drivers/pci/physdev.c | 6 - xen/include/xen/device_tree.h | 23 +++ xen/include/xen/iommu.h | 26 +++- 11 files changed, 528 insertions(+), 67 deletions(-)
base-commit: bede1c7e3b790b63f1ff3ea3ee4e476b012fdf2c
Signature · 1 line
2.42.0
PATCH v6 1/9 xen/arm: don't pass iommu properties to hwdom for iommu-map
A device tree node for a PCIe root controller may have an iommu-map property [1] with a phandle reference to the SMMU node, but not necessarily an iommus property. In this case, we want to treat it the same as we currently handle devices with an iommus property: don't pass the iommu related properties to hwdom.
[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
---v5->v6:
* add Julien's A-b
v4->v5:
* new patch
--- xen/arch/arm/domain_build.c | 2 ++ 1 file changed, 2 insertions(+)
xen/arch/arm/domain_build.c +2 -0
@@ -1135,6 +1135,8 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,* should be skipped. */ iommu_node =dt_parse_phandle (node,"iommus" ,0 ); +if ( !iommu_node ) + iommu_node =dt_parse_phandle (node,"iommu-map" ,1 );if ( iommu_node &&device_get_class (iommu_node) !=DEVICE_IOMMU ) iommu_node =NULL ;
Signature · 1 line
2.42.0
Re: [PATCH v6 1/9] xen/arm: don't pass iommu properties to hwdom for iommu-map
Hi,
Quoted 11 lines · Stewart Hildebrand
A device tree node for a PCIe root controller may have an iommu-map property [1] with a phandle reference to the SMMU node, but not necessarily an iommus property. In this case, we want to treat it the same as we currently handle devices with an iommus property: don't pass the iommu related properties to hwdom.
[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
Reported-by Michal Orzel <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>Acked-by Julien Grall <[email protected]>
I was committed this patch. The rest doesn't seem to be reviewed or have some dependencies.
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 2/9 iommu/arm: Add iommu_dt_xlate()
In reply to Stewart HildebrandFrom: Oleksandr Tyshchenko <[email protected]>
Move code for processing DT IOMMU specifier to a separate helper. This helper will be re-used for adding PCI devices by the subsequent patches as we will need exact the same actions for processing DT PCI-IOMMU specifier.
While at it introduce NO_IOMMU to avoid magic "1".
---v5->v6:
* pass ops parameter to iommu_dt_xlate()
* add Julien's R-b
v4->v5:
* rebase on top of "dynamic node programming using overlay dtbo" series
* move #define NO_IOMMU 1 to header
* s/these/this/ inside comment
v3->v4:
* make dt_phandle_args *iommu_spec const
* move !ops->add_device check to helper
v2->v3:
* no change
v1->v2:
* no change
downstream->v1:
* trivial rebase
* s/dt_iommu_xlate/iommu_dt_xlate/
(cherry picked from commit c26bab0415ca303df86aba1d06ef8edc713734d3 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)
--- xen/drivers/passthrough/device_tree.c | 48 +++++++++++++++++---------- xen/include/xen/iommu.h | 2 ++ 2 files changed, 32 insertions(+), 18 deletions(-)
xen/drivers/passthrough/device_tree.c +30 -18
@@ -137,6 +137,30 @@ int iommu_release_dt_devices(struct domain *d)return 0 ; } +static int iommu_dt_xlate (struct device *dev, +const struct dt_phandle_args *iommu_spec, +const struct iommu_ops *ops) +{ +int rc; + +if ( !ops->dt_xlate ) +return -EINVAL ; + +if ( !dt_device_is_available (iommu_spec->np) ) +return NO_IOMMU ; + + rc =iommu_fwspec_init (dev, &iommu_spec->np->dev); +if ( rc ) +return rc; + +/* +* Provide DT IOMMU specifier which describes the IOMMU master +* interfaces of that device (device IDs, etc) to the driver. +* The driver is responsible to decide how to interpret them. +*/ +return ops->dt_xlate (dev, iommu_spec); +} +int iommu_remove_dt_device (struct dt_device_node *np) {const struct iommu_ops *ops =iommu_get_ops ();
@@ -146,7 +170,7 @@ int iommu_remove_dt_device(struct dt_device_node *np)ASSERT (rw_is_locked (&dt_host_lock));if ( !iommu_enabled ) -return 1 ; +return NO_IOMMU ;if ( !ops )return -EOPNOTSUPP ;
@@ -187,12 +211,12 @@ int iommu_add_dt_device(struct dt_device_node *np)const struct iommu_ops *ops =iommu_get_ops ();struct dt_phandle_args iommu_spec;struct device *dev =dt_to_dev (np); -int rc =1 , index =0 ; +int rc =NO_IOMMU , index =0 ;ASSERT (system_state < SYS_STATE_active ||rw_is_locked (&dt_host_lock));if ( !iommu_enabled ) -return 1 ; +return NO_IOMMU ;if ( !ops )return -EINVAL ;
@@ -215,27 +239,15 @@ int iommu_add_dt_device(struct dt_device_node *np) {/* * The driver which supports generic IOMMU DT bindings must have -* these callback implemented. +* this callback implemented. */ -if ( !ops->add_device || !ops->dt_xlate ) +if ( !ops->add_device ) { rc = -EINVAL ; goto fail; } -if ( !dt_device_is_available (iommu_spec.np) ) -break ; - - rc =iommu_fwspec_init (dev, &iommu_spec.np->dev); -if ( rc ) -break ; - -/* -* Provide DT IOMMU specifier which describes the IOMMU master -* interfaces of that device (device IDs, etc) to the driver. -* The driver is responsible to decide how to interpret them. -*/ - rc = ops->dt_xlate (dev, &iommu_spec); + rc =iommu_dt_xlate (dev, &iommu_spec, ops);if ( rc )break ;
xen/include/xen/iommu.h +2 -0
@@ -245,6 +245,8 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,*/ int iommu_remove_dt_device (struct dt_device_node *np); +#define NO_IOMMU 1 +#endif /* HAS_DEVICE_TREE */ struct page_info ;
Signature · 1 line
2.42.0
Re: [PATCH v6 2/9] iommu/arm: Add iommu_dt_xlate()
xen/include/xen/iommu.h int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d, · line 250
Quoted 9 lines · Stewart Hildebrand
xen/include/xen/iommu.h +2
-0@@ -245,6 +245,8 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,*/ int iommu_remove_dt_device (struct dt_device_node *np); +#define NO_IOMMU 1 +#endif /* HAS_DEVICE_TREE */
When looking just here (i.e. once that was committed, when looking just at the header) it is entirely unclear what this #define's purpose is. My first guess was a bogus attempt to escape using some CONFIG_* setting. Looking at the use sites, this looks to be a kind-of-error-code. That wants saying here in a comment, alongside the scope of where this may be validly used.
Jan
PATCH v6 3/9 iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API
In reply to Stewart HildebrandFrom: Oleksandr Tyshchenko <[email protected]>
The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.
This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.
The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.
[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
---v5->v6:
* pass ops to iommu_dt_xlate()
v4->v5:
* style: add newlines after variable declarations and before return in iommu.h
* drop device_is_protected() check in iommu_add_dt_pci_sideband_ids()
* rebase on top of ("dynamic node programming using overlay dtbo") series
* fix typo in commit message
* remove #ifdef around dt_map_id() prototype
* move dt_map_id() to xen/common/device_tree.c
* add function name in error prints
* use dprintk for debug prints
* use GENMASK and #include <xen/bitops.h>
* fix typo in comment
* remove unnecessary (int) cast in loop condition
* assign *id_out and return success in case of no translation in dt_map_id()
* don't initialize local variable unnecessarily
* return error in case of ACPI/no DT in iommu_add_{dt_}pci_sideband_ids()
v3->v4:
* wrap #include <asm/acpi.h> and if ( acpi_disabled ) in #ifdef CONFIG_ACPI
* fix Michal's remarks about style, parenthesis, and print formats
* remove !ops->dt_xlate check since it is already in iommu_dt_xlate helper
* rename s/iommu_dt_pci_map_id/dt_map_id/ because it is generic, not specific to iommu
* update commit description
v2->v3:
* new patch title (was: iommu/arm: Introduce iommu_add_dt_pci_device API)
* renamed function
from: iommu_add_dt_pci_device
to: iommu_add_dt_pci_sideband_ids
* removed stale ops->add_device check
* iommu.h: add empty stub iommu_add_dt_pci_sideband_ids for !HAS_DEVICE_TREE
* iommu.h: add iommu_add_pci_sideband_ids helper
* iommu.h: don't wrap prototype in #ifdef CONFIG_HAS_PCI
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/
v1->v2:
* remove extra devfn parameter since pdev fully describes the device
* remove ops->add_device() call from iommu_add_dt_pci_device(). Instead, rely on the existing iommu call in iommu_add_device().
* move the ops->add_device and ops->dt_xlate checks earlier
downstream->v1:
* rebase
* add const qualifier to struct dt_device_node *np arg in dt_map_id()
* add const qualifier to struct dt_device_node *np declaration in iommu_add_pci_device()
* use stdint.h types instead of u8/u32/etc...
* rename functions:
s/dt_iommu_xlate/iommu_dt_xlate/
s/dt_map_id/iommu_dt_pci_map_id/ s/iommu_add_pci_device/iommu_add_dt_pci_device/
* add device_is_protected check in iommu_add_dt_pci_device
* wrap prototypes in CONFIG_HAS_PCI
(cherry picked from commit 734e3bf6ee77e7947667ab8fa96c25b349c2e1da from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)
--- xen/common/device_tree.c | 91 +++++++++++++++++++++++++++ xen/drivers/passthrough/device_tree.c | 42 +++++++++++++ xen/include/xen/device_tree.h | 23 +++++++ xen/include/xen/iommu.h | 24 ++++++- 4 files changed, 179 insertions(+), 1 deletion(-)
xen/common/device_tree.c +91 -0
@@ -10,6 +10,7 @@* published by the Free Software Foundation. */ +#include <xen/bitops.h> #include <xen/types.h> #include <xen/init.h> #include <xen/guest_access.h>
@@ -2243,6 +2244,96 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)return (u16 )domain; } +int dt_map_id (const struct dt_device_node *np,uint32_t id, +const char *map_name,const char *map_mask_name, +struct dt_device_node **target,uint32_t *id_out) +{ +uint32_t map_mask, masked_id, map_len; +const __be32 *map =NULL ; + +if ( !np || !map_name || (!target && !id_out) ) +return -EINVAL ; + + map =dt_get_property (np, map_name, &map_len); +if ( !map ) + { +if ( target ) +return -ENODEV ; + +/* Otherwise, no map implies no translation */ + *id_out = id; +return 0 ; + } + +if ( !map_len || (map_len % (4 *sizeof (*map))) ) + { +printk (XENLOG_ERR "%s(): %s: Error: Bad %s length: %u\n" , __func__, + np->full_name, map_name, map_len); +return -EINVAL ; + } + +/* The default is to select all bits. */ + map_mask =GENMASK (31 ,0 ); + +/* +* Can be overridden by "{iommu,msi}-map-mask" property. +* If dt_property_read_u32() fails, the default is used. +*/ +if ( map_mask_name ) +dt_property_read_u32 (np, map_mask_name, &map_mask); + + masked_id = map_mask & id; +for ( ; map_len >0 ; map_len -=4 *sizeof (*map), map +=4 ) + { +struct dt_device_node *phandle_node; +uint32_t id_base =be32_to_cpup (map +0 ); +uint32_t phandle =be32_to_cpup (map +1 ); +uint32_t out_base =be32_to_cpup (map +2 ); +uint32_t id_len =be32_to_cpup (map +3 ); + +if ( id_base & ~map_mask ) + { +printk (XENLOG_ERR "%s(): %s: Invalid %s translation - %s-mask (0x%" PRIx32") ignores id-base (0x%" PRIx32")\n" , + __func__, np->full_name, map_name, map_name, map_mask, + id_base); +return -EFAULT ; + } + +if ( (masked_id < id_base) || (masked_id >= (id_base + id_len)) ) +continue ; + + phandle_node =dt_find_node_by_phandle (phandle); +if ( !phandle_node ) +return -ENODEV ; + +if ( target ) + { +if ( !*target ) + *target = phandle_node; + +if ( *target != phandle_node ) +continue ; + } + +if ( id_out ) + *id_out = masked_id - id_base + out_base; + +dprintk (XENLOG_DEBUG ,"%s: %s, using mask %08" PRIx32", id-base: %08" PRIx32", out-base: %08" PRIx32", length: %08" PRIx32", id: %08" PRIx32" -> %08" PRIx32"\n" , + np->full_name, map_name, map_mask, id_base, out_base, id_len, id, + masked_id - id_base + out_base); +return 0 ; + } + +dprintk (XENLOG_DEBUG ,"%s: no %s translation for id 0x%" PRIx32" on %s\n" , + np->full_name, map_name, id, + (target && *target) ? (*target)->full_name :NULL ); + +if ( id_out ) + *id_out = id; + +return 0 ; +} +/* * Local variables: * mode: C
xen/drivers/passthrough/device_tree.c +42 -0
48 lines not on this page · raw message
xen/include/xen/device_tree.h +23 -0
@@ -939,6 +939,29 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,*/ int dt_get_pci_domain_nr (struct dt_device_node *node); +/** +* dt_map_id - Translate an ID through a downstream mapping. +* @np: root complex device node. +* @id: device ID to map. +* @map_name: property name of the map to use. +* @map_mask_name: optional property name of the mask to use. +* @target: optional pointer to a target device node. +* @id_out: optional pointer to receive the translated ID. +* +* Given a device ID, look up the appropriate implementation-defined +* platform ID and/or the target device which receives transactions on that +* ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or +* @id_out may be NULL if only the other is required. If @target points to +* a non-NULL device node pointer, only entries targeting that node will be +* matched; if it points to a NULL value, it will receive the device node of +* the first matching target phandle, with a reference held. +* +* Return: 0 on success or a standard error code on failure. +*/ +int dt_map_id (const struct dt_device_node *np,uint32_t id, +const char *map_name,const char *map_mask_name, +struct dt_device_node **target,uint32_t *id_out); +struct dt_device_node *dt_find_node_by_phandle (dt_phandle handle);#ifdef CONFIG_DEVICE_TREE_DEBUG
xen/include/xen/iommu.h +23 -1
@@ -27,6 +27,9 @@#include <xen/errno.h> #include <public/domctl.h> #include <public/hvm/ioreq.h> +#ifdef CONFIG_ACPI +#include <asm/acpi.h> +#endif #include <asm/device.h> TYPE_SAFE (uint64_t, dfn);
@@ -222,7 +225,8 @@ int iommu_dt_domain_init(struct domain *d);int iommu_release_dt_devices (struct domain *d);/* -* Helper to add master device to the IOMMU using generic IOMMU DT bindings. +* Helpers to add master device to the IOMMU using generic (PCI-)IOMMU +* DT bindings. * * Return values: * 0 : device is protected by an IOMMU
@@ -231,6 +235,7 @@ int iommu_release_dt_devices(struct domain *d);* (IOMMU is not enabled/present or device is not connected to it). */ int iommu_add_dt_device (struct dt_device_node *np); +int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev);int iommu_do_dt_domctl (struct xen_domctl *domctl,struct domain *d,XEN_GUEST_HANDLE_PARAM (xen_domctl_t) u_domctl);
@@ -247,8 +252,25 @@ int iommu_remove_dt_device(struct dt_device_node *np);#define NO_IOMMU 1 +#else /* !HAS_DEVICE_TREE */ +static inline int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev) +{ +return -ENOSYS ; +}#endif /* HAS_DEVICE_TREE */ +static inline int iommu_add_pci_sideband_ids (struct pci_dev *pdev) +{ +int ret = -ENOSYS ; + +#ifdef CONFIG_ACPI + if ( acpi_disabled ) +#endif + ret =iommu_add_dt_pci_sideband_ids (pdev); + +return ret; +} +struct page_info ;/*
Signature · 1 line
2.42.0
Re: [PATCH v6 3/9] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API
Quoted 306 lines · Stewart Hildebrand
From: Oleksandr Tyshchenko <[email protected]>
The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.
This behaves similarly to the existing
iommu_add_dt_deviceAPI, except it handles PCI devices, and it is to be invoked from theadd_devicehook in the SMMU driver.The function
dt_map_idto translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
Signed-off-by Oleksandr Tyshchenko <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>---
v5->v6:
* pass ops toiommu_dt_xlate()
v4->v5:
* style: add newlines after variable declarations and before return iniommu.h
* dropdevice_is_protected()check iniommu_add_dt_pci_sideband_ids()
* rebase on top of ("dynamic node programming using overlay dtbo") series
* fix typo in commit message
* remove #ifdef arounddt_map_id()prototype
* movedt_map_id()toxen/common/device_tree.c
* add function name in error prints
* use dprintk for debug prints
* use GENMASK and #include <xen/bitops.h>
* fix typo in comment
* remove unnecessary (int) cast in loop condition
* assign*id_outand return success in case of no translation indt_map_id()
* don't initialize local variable unnecessarily
* return error in case of ACPI/no DT in iommu_add_{dt_}pci_sideband_ids()
v3->v4:
* wrap #include <asm/acpi.h> and if (acpi_disabled) in #ifdefCONFIG_ACPI
* fix Michal's remarks about style, parenthesis, and print formats
* remove !ops->dt_xlate check since it is already iniommu_dt_xlatehelper
* rename s/iommu_dt_pci_map_id/dt_map_id/ because it is generic, not specific to iommu
* update commit description
v2->v3:
* new patch title (was: iommu/arm: Introduceiommu_add_dt_pci_deviceAPI)
* renamed function
from:iommu_add_dt_pci_device
to:iommu_add_dt_pci_sideband_ids
* removed staleops->add_devicecheck
*iommu.h: add empty stubiommu_add_dt_pci_sideband_idsfor !HAS_DEVICE_TREE
*iommu.h: addiommu_add_pci_sideband_idshelper
*iommu.h: don't wrap prototype in #ifdefCONFIG_HAS_PCI
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/
v1->v2:
* remove extra devfn parameter since pdev fully describes the device
* removeops->add_device()call fromiommu_add_dt_pci_device(). Instead, rely on the existing iommu call iniommu_add_device().
* move theops->add_deviceandops->dt_xlatechecks earlier
downstream->v1:
* rebase
* add const qualifier to structdt_device_node*np arg indt_map_id()
* add const qualifier to structdt_device_node*np declaration iniommu_add_pci_device()
* usestdint.htypes instead of u8/u32/etc...
* rename functions:
s/dt_iommu_xlate/iommu_dt_xlate/
s/dt_map_id/iommu_dt_pci_map_id/ s/iommu_add_pci_device/iommu_add_dt_pci_device/
* adddevice_is_protectedcheck iniommu_add_dt_pci_device
* wrap prototypes inCONFIG_HAS_PCI(cherry picked from commit
734e3bf6ee77e7947667ab8fa96c25b349c2e1dafrom
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)--- xen/common/device_tree.c | 91 +++++++++++++++++++++++++++ xen/drivers/passthrough/device_tree.c | 42 +++++++++++++ xen/include/xen/device_tree.h | 23 +++++++ xen/include/xen/iommu.h | 24 ++++++- 4 files changed, 179 insertions(+), 1 deletion(-)xen/common/device_tree.c +91
-0@@ -10,6 +10,7 @@* published by the Free Software Foundation. */ +#include <xen/bitops.h> #include <xen/types.h> #include <xen/init.h> #include <xen/guest_access.h> @@ -2243,6 +2244,96 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)return (u16 )domain; } +int dt_map_id (const struct dt_device_node *np,uint32_t id, +const char *map_name,const char *map_mask_name, +struct dt_device_node **target,uint32_t *id_out) +{ +uint32_t map_mask, masked_id, map_len; +const __be32 *map =NULL ; + +if ( !np || !map_name || (!target && !id_out) ) +return -EINVAL ; + + map =dt_get_property (np, map_name, &map_len); +if ( !map ) + { +if ( target ) +return -ENODEV ; + +/* Otherwise, no map implies no translation */ + *id_out = id; +return 0 ; + } + +if ( !map_len || (map_len % (4 *sizeof (*map))) ) + { +printk (XENLOG_ERR "%s(): %s: Error: Bad %s length: %u\n" , __func__, + np->full_name, map_name, map_len); +return -EINVAL ; + } + +/* The default is to select all bits. */ + map_mask =GENMASK (31 ,0 ); + +/* +* Can be overridden by "{iommu,msi}-map-mask" property. +* If dt_property_read_u32() fails, the default is used. +*/ +if ( map_mask_name ) +dt_property_read_u32 (np, map_mask_name, &map_mask); + + masked_id = map_mask & id; +for ( ; map_len >0 ; map_len -=4 *sizeof (*map), map +=4 ) + { +struct dt_device_node *phandle_node; +uint32_t id_base =be32_to_cpup (map +0 ); +uint32_t phandle =be32_to_cpup (map +1 ); +uint32_t out_base =be32_to_cpup (map +2 ); +uint32_t id_len =be32_to_cpup (map +3 ); + +if ( id_base & ~map_mask ) + { +printk (XENLOG_ERR "%s(): %s: Invalid %s translation - %s-mask (0x%" PRIx32") ignores id-base (0x%" PRIx32")\n" , + __func__, np->full_name, map_name, map_name, map_mask, + id_base); +return -EFAULT ; + } + +if ( (masked_id < id_base) || (masked_id >= (id_base + id_len)) ) +continue ; + + phandle_node =dt_find_node_by_phandle (phandle); +if ( !phandle_node ) +return -ENODEV ; + +if ( target ) + { +if ( !*target ) + *target = phandle_node; + +if ( *target != phandle_node ) +continue ; + } + +if ( id_out ) + *id_out = masked_id - id_base + out_base; + +dprintk (XENLOG_DEBUG ,"%s: %s, using mask %08" PRIx32", id-base: %08" PRIx32", out-base: %08" PRIx32", length: %08" PRIx32", id: %08" PRIx32" -> %08" PRIx32"\n" , + np->full_name, map_name, map_mask, id_base, out_base, id_len, id, + masked_id - id_base + out_base); +return 0 ; + } + +dprintk (XENLOG_DEBUG ,"%s: no %s translation for id 0x%" PRIx32" on %s\n" , + np->full_name, map_name, id, + (target && *target) ? (*target)->full_name :NULL ); + +if ( id_out ) + *id_out = id; + +return 0 ; +} +/* * Local variables: * mode: C xen/drivers/passthrough/device_tree.c +42
-0@@ -161,6 +161,48 @@ static int iommu_dt_xlate(struct device *dev,return ops->dt_xlate (dev, iommu_spec); } +#ifdef CONFIG_HAS_PCI +int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev) +{ +const struct iommu_ops *ops =iommu_get_ops (); +struct dt_phandle_args iommu_spec = { .args_count =1 }; +struct device *dev =pci_to_dev (pdev); +const struct dt_device_node *np; +int rc; + +if ( !iommu_enabled ) +return NO_IOMMU ; + +if ( !ops ) +return -EINVAL ; + +if (dev_iommu_fwspec_get (dev) ) +return -EEXIST ; + + np =pci_find_host_bridge_node (pdev); +if ( !np ) +return -ENODEV ; + +/* +* According to the Documentation/devicetree/bindings/pci/pci-iommu.txt +* from Linux. +*/ + rc =dt_map_id (np,PCI_BDF (pdev->bus, pdev->devfn),"iommu-map" , +"iommu-map-mask" , &iommu_spec.np, iommu_spec.args); +if ( rc ) +return (rc == -ENODEV ) ?NO_IOMMU : rc; + + rc =iommu_dt_xlate (dev, &iommu_spec, ops); +if ( rc <0 ) + { +iommu_fwspec_free (dev); +return -EINVAL ; + } + +return rc; +} +#endif /* CONFIG_HAS_PCI */ +int iommu_remove_dt_device (struct dt_device_node *np) {const struct iommu_ops *ops =iommu_get_ops ();xen/include/xen/device_tree.h +23
-0@@ -939,6 +939,29 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,*/ int dt_get_pci_domain_nr (struct dt_device_node *node); +/** +* dt_map_id - Translate an ID through a downstream mapping. +* @np: root complex device node. +* @id: device ID to map. +* @map_name: property name of the map to use. +* @map_mask_name: optional property name of the mask to use. +* @target: optional pointer to a target device node. +* @id_out: optional pointer to receive the translated ID. +* +* Given a device ID, look up the appropriate implementation-defined +* platform ID and/or the target device which receives transactions on that +* ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or +* @id_out may be NULL if only the other is required. If @target points to +* a non-NULL device node pointer, only entries targeting that node will be +* matched; if it points to a NULL value, it will receive the device node of +* the first matching target phandle, with a reference held. +* +* Return: 0 on success or a standard error code on failure. +*/ +int dt_map_id (const struct dt_device_node *np,uint32_t id, +const char *map_name,const char *map_mask_name, +struct dt_device_node **target,uint32_t *id_out); +struct dt_device_node *dt_find_node_by_phandle (dt_phandle handle);#ifdef CONFIG_DEVICE_TREE_DEBUG xen/include/xen/iommu.h +5
-1@@ -27,6 +27,9 @@#include <xen/errno.h> #include <public/domctl.h> #include <public/hvm/ioreq.h> +#ifdef CONFIG_ACPI +#include <asm/acpi.h> +#endif #include <asm/device.h> TYPE_SAFE (uint64_t, dfn);@@ -222,7 +225,8 @@ int iommu_dt_domain_init(struct domain *d);int iommu_release_dt_devices (struct domain *d);/* -* Helper to add master device to the IOMMU using generic IOMMU DT bindings. +* Helpers to add master device to the IOMMU using generic (PCI-)IOMMU +* DT bindings. * * Return values: * 0 : device is protected by an IOMMU
xen/include/xen/iommu.h int iommu_release_dt_devices(struct domain *d); · line 238
Quoted 4 lines
xen/include/xen/iommu.h +1
-0@@ -231,6 +235,7 @@ int iommu_release_dt_devices(struct domain *d);* (IOMMU is not enabled/present or device is not connected to it). */ int iommu_add_dt_device (struct dt_device_node *np); +int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev);
This doesn't look quite right without #ifdef CONFIG_HAS_PCI around it, but I realize we already have another use of struct pci_dev here in a function declaration without forward-declaring the struct. IOW it looks okay now, but will likely bite us later on.
xen/include/xen/iommu.h int iommu_remove_dt_device(struct dt_device_node *np); · line 260
Quoted 10 lines
xen/include/xen/iommu.h +5
-0@@ -247,8 +252,25 @@ int iommu_remove_dt_device(struct dt_device_node *np);#define NO_IOMMU 1 +#else /* !HAS_DEVICE_TREE */ +static inline int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev) +{ +return -ENOSYS ; +}#endif /* HAS_DEVICE_TREE */
This wants blank lines added, to be symmetric with the tail of the #if part.
xen/include/xen/iommu.h int iommu_remove_dt_device(struct dt_device_node *np); · line 272
Quoted 11 lines
xen/include/xen/iommu.h +11
-0+static inline int iommu_add_pci_sideband_ids (struct pci_dev *pdev) +{ +int ret = -ENOSYS ; + +#ifdef CONFIG_ACPI + if ( acpi_disabled ) +#endif + ret =iommu_add_dt_pci_sideband_ids (pdev); + +return ret; +}
Independent of the earlier remark, this surely wants enclosing in #ifdef CONFIG_HAS_PCI. At which point the inclusion of acpi.h also wants to be (further) restricted to just that case. (That said, acpi.h could of course do with itself dealing with CONFIG_ACPI and supplying acpi_disabled as a #define when that setting is off.)
I also continue to disagree with uses of ENOSYS in situations like this.
Jan
Re: [PATCH v6 3/9] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API
In reply to Stewart HildebrandHi Stewart,
Quoted 98 lines · Stewart Hildebrand
From: Oleksandr Tyshchenko <[email protected]>
The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.
This behaves similarly to the existing
iommu_add_dt_deviceAPI, except it handles PCI devices, and it is to be invoked from theadd_devicehook in the SMMU driver.The function
dt_map_idto translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
Signed-off-by Oleksandr Tyshchenko <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>---
v5->v6:
* pass ops toiommu_dt_xlate()
v4->v5:
* style: add newlines after variable declarations and before return iniommu.h
* dropdevice_is_protected()check iniommu_add_dt_pci_sideband_ids()
* rebase on top of ("dynamic node programming using overlay dtbo") series
* fix typo in commit message
* remove #ifdef arounddt_map_id()prototype
* movedt_map_id()toxen/common/device_tree.c
* add function name in error prints
* use dprintk for debug prints
* use GENMASK and #include <xen/bitops.h>
* fix typo in comment
* remove unnecessary (int) cast in loop condition
* assign*id_outand return success in case of no translation indt_map_id()
* don't initialize local variable unnecessarily
* return error in case of ACPI/no DT in iommu_add_{dt_}pci_sideband_ids()
v3->v4:
* wrap #include <asm/acpi.h> and if (acpi_disabled) in #ifdefCONFIG_ACPI
* fix Michal's remarks about style, parenthesis, and print formats
* remove !ops->dt_xlate check since it is already iniommu_dt_xlatehelper
* rename s/iommu_dt_pci_map_id/dt_map_id/ because it is generic, not specific to iommu
* update commit description
v2->v3:
* new patch title (was: iommu/arm: Introduceiommu_add_dt_pci_deviceAPI)
* renamed function
from:iommu_add_dt_pci_device
to:iommu_add_dt_pci_sideband_ids
* removed staleops->add_devicecheck
*iommu.h: add empty stubiommu_add_dt_pci_sideband_idsfor !HAS_DEVICE_TREE
*iommu.h: addiommu_add_pci_sideband_idshelper
*iommu.h: don't wrap prototype in #ifdefCONFIG_HAS_PCI
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/
v1->v2:
* remove extra devfn parameter since pdev fully describes the device
* removeops->add_device()call fromiommu_add_dt_pci_device(). Instead, rely on the existing iommu call iniommu_add_device().
* move theops->add_deviceandops->dt_xlatechecks earlier
downstream->v1:
* rebase
* add const qualifier to structdt_device_node*np arg indt_map_id()
* add const qualifier to structdt_device_node*np declaration iniommu_add_pci_device()
* usestdint.htypes instead of u8/u32/etc...
* rename functions:s/dt_iommu_xlate/iommu_dt_xlate/ s/dt_map_id/iommu_dt_pci_map_id/ s/iommu_add_pci_device/iommu_add_dt_pci_device/* add
device_is_protectedcheck iniommu_add_dt_pci_device
* wrap prototypes inCONFIG_HAS_PCI(cherry picked from commit
734e3bf6ee77e7947667ab8fa96c25b349c2e1dafrom
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)--- xen/common/device_tree.c | 91 +++++++++++++++++++++++++++ xen/drivers/passthrough/device_tree.c | 42 +++++++++++++ xen/include/xen/device_tree.h | 23 +++++++ xen/include/xen/iommu.h | 24 ++++++- 4 files changed, 179 insertions(+), 1 deletion(-)xen/common/device_tree.c +1
-0@@ -10,6 +10,7 @@* published by the Free Software Foundation. */ +#include <xen/bitops.h> #include <xen/types.h> #include <xen/init.h> #include <xen/guest_access.h>
xen/common/device_tree.c int dt_get_pci_domain_nr(struct dt_device_node *node) · line 2247
Quoted 4 lines
xen/common/device_tree.c +1
-0@@ -2243,6 +2244,96 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)return (u16 )domain; } +int dt_map_id(const struct dt_device_node *np, uint32_t id,
OOI (no changes requested), compare to Linux, why did you rename 'rid' to 'id'? Are we going to pass something different than a requester ID?
Quoted 118 lines
xen/common/device_tree.c +89
-0+ const char *map_name, const char *map_mask_name, + struct dt_device_node **target, uint32_t *id_out) +{ + uint32_t map_mask, masked_id, map_len; +const __be32 *map =NULL ; + +if ( !np || !map_name || (!target && !id_out) ) +return -EINVAL ; + + map =dt_get_property (np, map_name, &map_len); +if ( !map ) + { +if ( target ) +return -ENODEV ; + +/* Otherwise, no map implies no translation */ + *id_out = id; +return 0 ; + } + +if ( !map_len || (map_len % (4 *sizeof (*map))) ) + { +printk (XENLOG_ERR "%s(): %s: Error: Bad %s length: %u\n" , __func__, + np->full_name, map_name, map_len); +return -EINVAL ; + } + +/* The default is to select all bits. */ + map_mask =GENMASK (31 ,0 ); + +/* +* Can be overridden by "{iommu,msi}-map-mask" property. +* If dt_property_read_u32() fails, the default is used. +*/ +if ( map_mask_name ) +dt_property_read_u32 (np, map_mask_name, &map_mask); + + masked_id = map_mask & id; +for ( ; map_len >0 ; map_len -=4 *sizeof (*map), map +=4 ) + { +struct dt_device_node *phandle_node; +uint32_t id_base =be32_to_cpup (map +0 ); +uint32_t phandle =be32_to_cpup (map +1 ); +uint32_t out_base =be32_to_cpup (map +2 ); +uint32_t id_len =be32_to_cpup (map +3 ); + +if ( id_base & ~map_mask ) + { +printk (XENLOG_ERR "%s(): %s: Invalid %s translation - %s-mask (0x%" PRIx32") ignores id-base (0x%" PRIx32")\n" , + __func__, np->full_name, map_name, map_name, map_mask, + id_base); +return -EFAULT ; + } + +if ( (masked_id < id_base) || (masked_id >= (id_base + id_len)) ) +continue ; + + phandle_node =dt_find_node_by_phandle (phandle); +if ( !phandle_node ) +return -ENODEV ; + +if ( target ) + { +if ( !*target ) + *target = phandle_node; + +if ( *target != phandle_node ) +continue ; + } + +if ( id_out ) + *id_out = masked_id - id_base + out_base; + +dprintk (XENLOG_DEBUG ,"%s: %s, using mask %08" PRIx32", id-base: %08" PRIx32", out-base: %08" PRIx32", length: %08" PRIx32", id: %08" PRIx32" -> %08" PRIx32"\n" , + np->full_name, map_name, map_mask, id_base, out_base, id_len, id, + masked_id - id_base + out_base); +return 0 ; + } + +dprintk (XENLOG_DEBUG ,"%s: no %s translation for id 0x%" PRIx32" on %s\n" , + np->full_name, map_name, id, + (target && *target) ? (*target)->full_name :NULL ); + +if ( id_out ) + *id_out = id; + +return 0 ; +} +/* * Local variables: * mode: C xen/drivers/passthrough/device_tree.c +18
-0@@ -161,6 +161,48 @@ static int iommu_dt_xlate(struct device *dev,return ops->dt_xlate (dev, iommu_spec); } +#ifdef CONFIG_HAS_PCI +int iommu_add_dt_pci_sideband_ids (struct pci_dev *pdev) +{ +const struct iommu_ops *ops =iommu_get_ops (); +struct dt_phandle_args iommu_spec = { .args_count =1 }; +struct device *dev =pci_to_dev (pdev); +const struct dt_device_node *np; +int rc; + +if ( !iommu_enabled ) +return NO_IOMMU ; + +if ( !ops ) +return -EINVAL ; + +if (dev_iommu_fwspec_get (dev) ) +return -EEXIST ; +
xen/drivers/passthrough/device_tree.c static int iommu_dt_xlate(struct device *dev, · line 191
Quoted 10 lines
xen/drivers/passthrough/device_tree.c +10
-0+ np =pci_find_host_bridge_node (pdev); +if ( !np ) +return -ENODEV ; + +/* +* According to the Documentation/devicetree/bindings/pci/pci-iommu.txt +* from Linux. +*/ + rc =dt_map_id (np,PCI_BDF (pdev->bus, pdev->devfn),"iommu-map" , +"iommu-map-mask" , &iommu_spec.np, iommu_spec.args);
It is not fully clear why in Xen we directly call dt_map_id() (aka of_map_rid() in Linux) whereas Linux will may map multiple RID via pci_for_each_dma_alias(). Can you clarify?
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 4/9 iommu/arm: iommu_add_dt_pci_sideband_ids phantom handling
In reply to Stewart HildebrandHandle phantom functions in iommu_add_dt_pci_sideband_ids(). Each phantom function will have a unique requestor ID (RID)/BDF. On ARM, we need to map/translate the RID/BDF to an AXI stream ID for each phantom function according to the pci-iommu device tree mapping [1]. The RID/BDF -> AXI stream ID mapping in DT could allow phantom devices (i.e. devices with phantom functions) to use different AXI stream IDs based on the (phantom) function.
[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt
---v5->v6:
* no change
v4->v5:
* no change
v3->v4:
* s/iommu_dt_pci_map_id/dt_map_id/
v2->v3:
* new patch title (was: iommu/arm: iommu_add_dt_pci_device phantom handling)
* rework loop to reduce duplication
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/
v1->v2:
* new patch
--- xen/drivers/passthrough/device_tree.c | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-)
xen/drivers/passthrough/device_tree.c +20 -13
@@ -169,6 +169,7 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)struct device *dev =pci_to_dev (pdev);const struct dt_device_node *np;int rc; +unsigned int devfn = pdev->devfn;if ( !iommu_enabled )return NO_IOMMU ;
@@ -183,21 +184,27 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)if ( !np )return -ENODEV ; -/* -* According to the Documentation/devicetree/bindings/pci/pci-iommu.txt -* from Linux. -*/ - rc =dt_map_id (np,PCI_BDF (pdev->bus, pdev->devfn),"iommu-map" , -"iommu-map-mask" , &iommu_spec.np, iommu_spec.args); -if ( rc ) -return (rc == -ENODEV ) ?NO_IOMMU : rc; +do { +/* +* According to the Documentation/devicetree/bindings/pci/pci-iommu.txt +* from Linux. +*/ + rc =dt_map_id (np,PCI_BDF (pdev->bus, devfn),"iommu-map" , +"iommu-map-mask" , &iommu_spec.np, iommu_spec.args); +if ( rc ) +return (rc == -ENODEV ) ?NO_IOMMU : rc; - rc =iommu_dt_xlate (dev, &iommu_spec, ops); -if ( rc <0 ) - { -iommu_fwspec_free (dev); -return -EINVAL ; + rc =iommu_dt_xlate (dev, &iommu_spec, ops); +if ( rc <0 ) + { +iommu_fwspec_free (dev); +return -EINVAL ; + } + + devfn += pdev->phantom_stride; } +while ( (devfn != pdev->devfn) && + (PCI_SLOT (devfn) ==PCI_SLOT (pdev->devfn)) );return rc; }
Signature · 1 line
2.42.0
PATCH v6 5/9 xen/arm: smmuv2: Add PCI devices support for SMMUv2
In reply to Stewart HildebrandFrom: Oleksandr Andrushchenko <[email protected]>
---v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()
v4->v5:
* assign device to pdev->domain (usually dom0) by default in add_device() hook
* deassign from hwdom
* rebase on top of ("dynamic node programming using overlay dtbo") series
* remove TODO in comment about device prints
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")
v3->v4:
* add new device_is_protected check in add_device hook to match SMMUv3 and IPMMU-VMSA drivers
v2->v3:
* invoke iommu_add_pci_sideband_ids() from add_device hook
v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)
downstream->v1:
* wrap unused function in #ifdef 0
* remove the remove_device() stub since it was submitted separately to the list [XEN][PATCH v6 12/19] xen/smmu: Add remove_device callback for smmu_iommu ops
https://lists.xenproject.org/archives/html/xen-devel/2023-05/msg00204.html
* arm_smmu_(de)assign_dev: return error instead of crashing system
* update condition in arm_smmu_reassign_dev
* style fixup
* add && !is_hardware_domain(d) into condition in arm_smmu_assign_dev()
(cherry picked from commit 0c11a7f65f044c26d87d1e27ac6283ef1f9cfb7a from
the downstream branch spider-master from
https://github.com/xen-troops/xen.git)
--- xen/drivers/passthrough/arm/smmu.c | 199 ++++++++++++++++++++++++----- 1 file changed, 169 insertions(+), 30 deletions(-)
xen/drivers/passthrough/arm/smmu.c +169 -30
@@ -131,11 +131,21 @@ enum irqreturn {typedef enum irqreturn irqreturn_t ; -/* Device logger functions -* TODO: Handle PCI -*/ -#define dev_print (dev, lvl, fmt, ...) \ - printk(lvl "smmu: %s: " fmt, dt_node_full_name(dev_to_dt(dev)), ## __VA_ARGS__) +/* Device logger functions */ +#ifndef CONFIG_HAS_PCI +#define dev_print (dev, lvl, fmt, ...) \ + printk(lvl "smmu: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#else +#define dev_print (dev, lvl, fmt, ...) ({ \ + if ( !dev_is_pci((dev)) ) \ + printk(lvl "smmu: %s: " fmt, dev_name((dev)), ## __VA_ARGS__); \ + else \ + { \ + struct pci_dev *pdev = dev_to_pci((dev)); \ + printk(lvl "smmu: %pp: " fmt, &pdev->sbdf, ## __VA_ARGS__); \ + } \ +}) +#endif #define dev_dbg (dev, fmt, ...) dev_print(dev, XENLOG_DEBUG, fmt, ## __VA_ARGS__)#define dev_notice (dev, fmt, ...) dev_print(dev, XENLOG_INFO, fmt, ## __VA_ARGS__)
@@ -187,6 +197,7 @@ static void __iomem *devm_ioremap_resource(struct device *dev,* Xen: PCI functions * TODO: It should be implemented when PCI will be supported */ +#if 0 /* unused */ #define to_pci_dev (dev) (NULL) static inline int pci_for_each_dma_alias(struct pci_dev *pdev, int (*fn) (struct pci_dev *pdev,
@@ -196,6 +207,7 @@ static inline int pci_for_each_dma_alias(struct pci_dev *pdev,BUG (); return 0; } +#endif /* Xen: misc */ #define PHYS_MASK_SHIFT PADDR_BITS
@@ -631,7 +643,7 @@ struct arm_smmu_master_cfg {for (i =0 ; idx = cfg->smendx[i], i < num; ++i) struct arm_smmu_master { -struct device_node *of_node; +struct device *dev;struct rb_node node;struct arm_smmu_master_cfg cfg; };
@@ -723,7 +735,7 @@ arm_smmu_get_fwspec(struct arm_smmu_master_cfg *cfg) {struct arm_smmu_master *master =container_of (cfg, struct arm_smmu_master, cfg); -return dev_iommu_fwspec_get (&master->of_node->dev); +return dev_iommu_fwspec_get (master->dev); }static void parse_driver_options (struct arm_smmu_device *smmu)
@@ -756,7 +768,7 @@ static struct device_node *dev_get_dev_node(struct device *dev) }static struct arm_smmu_master *find_smmu_master (struct arm_smmu_device *smmu, -struct device_node *dev_node) +struct device *dev) {struct rb_node *node = smmu->masters.rb_node;
@@ -765,9 +777,9 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, master = container_of(node, struct arm_smmu_master, node); -if (dev_node < master->of_node) +if (dev < master->dev) node = node->rb_left; -else if (dev_node > master->of_node) +else if (dev > master->dev) node = node->rb_right;else return master;
@@ -802,9 +814,9 @@ static int insert_smmu_master(struct arm_smmu_device *smmu, = container_of(*new, struct arm_smmu_master, node); parent = *new; -if (master->of_node < this->of_node) +if (master->dev < this->dev) new = &((*new)->rb_left); -else if (master->of_node > this->of_node) +else if (master->dev > this->dev) new = &((*new)->rb_right);else return -EEXIST ;
@@ -836,28 +848,37 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,struct arm_smmu_master *master;struct device_node *dev_node =dev_get_dev_node (dev); - master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master) {dev_err (dev,"rejecting multiple registrations for master device %s\n" , - dev_node->name); + dev_node ? dev_node->name :"" );return -EBUSY ; } master =devm_kzalloc (dev,sizeof (*master),GFP_KERNEL );if (!master)return -ENOMEM ; - master->of_node = dev_node; + master->dev = dev; -/* Xen: Let Xen know that the device is protected by an SMMU */ -dt_device_set_protected (dev_node); +if ( !dev_is_pci (dev) ) + { +if (dt_device_is_protected (dev_node) ) + { +dev_err (dev,"Already added to SMMU\n" ); +return -EEXIST ; + } + +/* Xen: Let Xen know that the device is protected by an SMMU */ +dt_device_set_protected (dev_node); + }for (i =0 ; i < fwspec->num_ids; ++i) {if (!(smmu->features &ARM_SMMU_FEAT_STREAM_MATCH ) && (fwspec->ids[i] >= smmu->num_mapping_groups)) {dev_err (dev,"stream ID for master device %s greater than maximum allowed (%d)\n" , - dev_node->name, smmu->num_mapping_groups); + dev_node ? dev_node->name :"" , smmu->num_mapping_groups);return -ERANGE ; } master->cfg.smendx[i] =INVALID_SMENDX ;
@@ -872,7 +893,7 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,struct device_node *dev_node =dev_get_dev_node (dev);int ret; - master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master ==NULL ) { dev_err(dev, "No registrations found for master device %s\n",
@@ -884,8 +905,9 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,if (ret)return ret; -/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */ - dev_node->is_protected = false; +if ( !dev_is_pci (dev) ) +/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */ + dev_node->is_protected = false;kfree (master);return 0 ;
@@ -914,6 +936,12 @@ static int register_smmu_master(struct arm_smmu_device *smmu, fwspec); } +/* Forward declaration */ +static int arm_smmu_assign_dev (struct domain *d,u8 devfn, +struct device *dev,u32 flag); +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn, +struct device *dev); +/* * The driver which supports generic IOMMU DT bindings must have this * callback implemented.
@@ -938,6 +966,22 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) {struct arm_smmu_device *smmu;struct iommu_fwspec *fwspec; +int ret; + +#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);if (fwspec ==NULL )
@@ -947,7 +991,24 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)if (smmu ==NULL )return -ENXIO ; -return arm_smmu_dt_add_device_legacy (smmu, dev, fwspec); + ret =arm_smmu_dt_add_device_legacy (smmu, dev, fwspec); +if ( ret ) +return ret; + +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +/* +* During PHYSDEVOP_pci_device_add, Xen does not assign the +* device, so we must do it here. +*/ + ret =arm_smmu_assign_dev (pdev->domain, devfn, dev,0 ); + } +#endif + +return ret; } static int arm_smmu_dt_xlate_generic(struct device *dev,
@@ -970,11 +1031,10 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev) {struct arm_smmu_device *smmu;struct arm_smmu_master *master =NULL ; -struct device_node *dev_node =dev_get_dev_node (dev);spin_lock (&arm_smmu_devices_lock);list_for_each_entry (smmu, &arm_smmu_devices, list) { - master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master)break ; }
@@ -2066,6 +2126,7 @@ static bool arm_smmu_capable(enum iommu_cap cap) }#endif +#if 0 /* Not used */ static int __arm_smmu_get_pci_sid (struct pci_dev *pdev,u16 alias,void *data) { *((u16 *)data) = alias;
@@ -2076,6 +2137,7 @@ static void __arm_smmu_release_pci_iommudata(void *data) {kfree (data); } +#endif static int arm_smmu_add_device (struct device *dev) {
@@ -2083,12 +2145,13 @@ static int arm_smmu_add_device(struct device *dev)struct arm_smmu_master_cfg *cfg;struct iommu_group *group;void (*releasefn)(void *data) =NULL ; -int ret; smmu =find_smmu_for_device (dev);if (!smmu)return -ENODEV ; +/* There is no need to distinguish here, thanks to PCI-IOMMU DT bindings */ +#if 0 if (dev_is_pci (dev)) {struct pci_dev *pdev =to_pci_dev (dev);struct iommu_fwspec *fwspec;
@@ -2113,10 +2176,12 @@ static int arm_smmu_add_device(struct device *dev) &fwspec->ids[0]); releasefn = __arm_smmu_release_pci_iommudata; cfg->smmu = smmu; - }else { + } else +#endif + {struct arm_smmu_master *master; - master =find_smmu_master (smmu, dev->of_node); + master =find_smmu_master (smmu, dev);if (!master) {return -ENODEV ; }
@@ -2784,6 +2849,61 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,return -ENOMEM ; } +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) && !is_hardware_domain (d) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Assigning device %04x:%02x:%02x.%u to dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn),PCI_FUNC (devfn), + d->domain_id); + +if ( devfn != pdev->devfn || pdev->domain == d ) +return 0 ; + +ASSERT (pcidevs_locked ()); + +/* TODO: acquire pci_lock */ +#if 0 +write_lock (&pdev->domain->pci_lock); +#endif +list_del (&pdev->domain_list); +#if 0 +write_unlock (&pdev->domain->pci_lock); +#endif + + pdev->domain = d; + +#if 0 +write_lock (&d->pci_lock); +#endif +list_add (&pdev->domain_list, &d->pdev_list); +#if 0 +write_unlock (&d->pci_lock); +#endif + +if ( hardware_domain ) + { + domain =dev_iommu_domain (dev); + +/* +* Xen may not deassign the device from hwdom before +* assigning it elsewhere. +*/ +if ( domain &&is_hardware_domain (domain->priv->cfg.domain) ) + { + ret =arm_smmu_deassign_dev (hardware_domain, devfn, dev); +if ( ret ) +return ret; + } + } + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) +return 0 ; + } +#endif +if (!dev_iommu_group (dev)) { ret =arm_smmu_add_device (dev);if (ret)
@@ -2833,11 +2953,30 @@ out:return ret; } -static int arm_smmu_deassign_dev (struct domain *d,struct device *dev) +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn, +struct device *dev) {struct iommu_domain *domain =dev_iommu_domain (dev);struct arm_smmu_xen_domain *xen_domain; +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Deassigning device %04x:%02x:%02x.%u from dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn),PCI_FUNC (devfn), + d->domain_id); + +if ( devfn != pdev->devfn ) +return 0 ; + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) +return 0 ; + } +#endif + xen_domain =dom_iommu (d)->arch.priv;if (!domain || domain->priv->cfg.domain != d) {
@@ -2865,13 +3004,13 @@ static int arm_smmu_reassign_dev(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)return 0 ; - ret =arm_smmu_deassign_dev (s, dev); + ret =arm_smmu_deassign_dev (s, devfn, dev);if (ret)return ret;
Signature · 1 line
2.42.0
Re: [PATCH v6 5/9] xen/arm: smmuv2: Add PCI devices support for SMMUv2
Hi Stewart,
Quoted 1 line · Stewart Hildebrand
From: Oleksandr Andrushchenko <[email protected]>
In general, we should avoid blank commit. In this case, I think some details would be useful about the implementation. Some of the details I am interested in is how the logic works and why we don't handle the same quarantine options as x86?
Quoted 144 lines
Signed-off-by Oleksandr Tyshchenko <[email protected]>Signed-off-by Oleksandr Andrushchenko <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>---
v5->v6:
* check forhardware_domain== NULL (dom0less test case)
* locking: assignpdev->domainbeforelist_add()
v4->v5:
* assign device topdev->domain(usually dom0) by default inadd_device()hook
* deassign from hwdom
* rebase on top of ("dynamic node programming using overlay dtbo") series
* remove TODO in comment about device prints
* add TODO regarding locking
* fixup after dropping ("xen/arm: Moveis_protectedflag to struct device")
v3->v4:
* add newdevice_is_protectedcheck inadd_devicehook to match SMMUv3 and IPMMU-VMSA drivers
v2->v3:
* invokeiommu_add_pci_sideband_ids()fromadd_devicehook
v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn !=pdev->devfn)
downstream->v1:
* wrap unused function in #ifdef 0
* remove theremove_device()stub since it was submitted separately to the list [XEN][PATCH v6 12/19] xen/smmu: Addremove_devicecallback forsmmu_iommuops
https://lists.xenproject.org/archives/html/xen-devel/2023-05/msg00204.html
* arm_smmu_(de)assign_dev: return error instead of crashing system
* update condition inarm_smmu_reassign_dev
* style fixup
* add && !is_hardware_domain(d) into condition inarm_smmu_assign_dev()(cherry picked from commit
0c11a7f65f044c26d87d1e27ac6283ef1f9cfb7afrom
the downstream branch spider-master from
https://github.com/xen-troops/xen.git)--- xen/drivers/passthrough/arm/smmu.c | 199 ++++++++++++++++++++++++----- 1 file changed, 169 insertions(+), 30 deletions(-)xen/drivers/passthrough/arm/smmu.c +24
-12@@ -131,11 +131,21 @@ enum irqreturn {typedef enum irqreturn irqreturn_t ; -/* Device logger functions -* TODO: Handle PCI -*/ -#define dev_print (dev, lvl, fmt, ...) \ - printk(lvl "smmu: %s: " fmt, dt_node_full_name(dev_to_dt(dev)), ## __VA_ARGS__) +/* Device logger functions */ +#ifndef CONFIG_HAS_PCI +#define dev_print (dev, lvl, fmt, ...) \ + printk(lvl "smmu: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#else +#define dev_print (dev, lvl, fmt, ...) ({ \ + if ( !dev_is_pci((dev)) ) \ + printk(lvl "smmu: %s: " fmt, dev_name((dev)), ## __VA_ARGS__); \ + else \ + { \ + struct pci_dev *pdev = dev_to_pci((dev)); \ + printk(lvl "smmu: %pp: " fmt, &pdev->sbdf, ## __VA_ARGS__); \ + } \ +}) +#endif #define dev_dbg (dev, fmt, ...) dev_print(dev, XENLOG_DEBUG, fmt, ## __VA_ARGS__)#define dev_notice (dev, fmt, ...) dev_print(dev, XENLOG_INFO, fmt, ## __VA_ARGS__)@@ -187,6 +197,7 @@ static void __iomem *devm_ioremap_resource(struct device *dev,* Xen: PCI functions * TODO: It should be implemented when PCI will be supported */ +#if 0 /* unused */ #define to_pci_dev (dev) (NULL) static inline int pci_for_each_dma_alias(struct pci_dev *pdev, int (*fn) (struct pci_dev *pdev,@@ -196,6 +207,7 @@ static inline int pci_for_each_dma_alias(struct pci_dev *pdev,BUG (); return 0; } +#endif /* Xen: misc */ #define PHYS_MASK_SHIFT PADDR_BITS@@ -631,7 +643,7 @@ struct arm_smmu_master_cfg {for (i =0 ; idx = cfg->smendx[i], i < num; ++i) struct arm_smmu_master { -struct device_node *of_node; +struct device *dev;struct rb_node node;struct arm_smmu_master_cfg cfg; };@@ -723,7 +735,7 @@ arm_smmu_get_fwspec(struct arm_smmu_master_cfg *cfg) {struct arm_smmu_master *master =container_of (cfg, struct arm_smmu_master, cfg); -return dev_iommu_fwspec_get (&master->of_node->dev); +return dev_iommu_fwspec_get (master->dev); }static void parse_driver_options (struct arm_smmu_device *smmu)@@ -756,7 +768,7 @@ static struct device_node *dev_get_dev_node(struct device *dev) }static struct arm_smmu_master *find_smmu_master (struct arm_smmu_device *smmu, -struct device_node *dev_node) +struct device *dev) {struct rb_node *node = smmu->masters.rb_node;@@ -765,9 +777,9 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu, master = container_of(node, struct arm_smmu_master, node); -if (dev_node < master->of_node) +if (dev < master->dev) node = node->rb_left; -else if (dev_node > master->of_node) +else if (dev > master->dev) node = node->rb_right;else return master;@@ -802,9 +814,9 @@ static int insert_smmu_master(struct arm_smmu_device *smmu, = container_of(*new, struct arm_smmu_master, node); parent = *new; -if (master->of_node < this->of_node) +if (master->dev < this->dev) new = &((*new)->rb_left); -else if (master->of_node > this->of_node) +else if (master->dev > this->dev) new = &((*new)->rb_right);else return -EEXIST ;
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 849
Quoted 2 lines
xen/drivers/passthrough/arm/smmu.c +0
-0@@ -836,28 +848,37 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,struct arm_smmu_master *master;struct device_node *dev_node =dev_get_dev_node (dev);
Looking at this call, there is a Todo "Add support of PCI". If this function is now only meant to be used for platform device, then should we remove the TODO in it?
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 855
Quoted 8 lines
xen/drivers/passthrough/arm/smmu.c +2
-2- master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master) {dev_err (dev,"rejecting multiple registrations for master device %s\n" , - dev_node->name); + dev_node ? dev_node->name :"" );
Printing "" looks a bit odd. But I wonder if this is actually redundant with the content printed by dev_err()?
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 864
Quoted 12 lines
xen/drivers/passthrough/arm/smmu.c +2
-3return -EBUSY ; } master =devm_kzalloc (dev,sizeof (*master),GFP_KERNEL );if (!master)return -ENOMEM ; - master->of_node = dev_node; + master->dev = dev; -/* Xen: Let Xen know that the device is protected by an SMMU */ -dt_device_set_protected (dev_node); +if ( !dev_is_pci (dev) )
I think a comment should stay to explain how someone known that the PCI device will be protected by an IOMMU.
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 870
Quoted 6 lines
xen/drivers/passthrough/arm/smmu.c +6
-0+ { +if (dt_device_is_protected (dev_node) ) + { +dev_err (dev,"Already added to SMMU\n" ); +return -EEXIST ; + }
This checks seems to be unrelated with the rest of the patch. Can you explain?
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 881
Quoted 12 lines
xen/drivers/passthrough/arm/smmu.c +5
-1+ +/* Xen: Let Xen know that the device is protected by an SMMU */ +dt_device_set_protected (dev_node); + }for (i =0 ; i < fwspec->num_ids; ++i) {if (!(smmu->features &ARM_SMMU_FEAT_STREAM_MATCH ) && (fwspec->ids[i] >= smmu->num_mapping_groups)) {dev_err (dev,"stream ID for master device %s greater than maximum allowed (%d)\n" , - dev_node->name, smmu->num_mapping_groups); + dev_node ? dev_node->name :"" , smmu->num_mapping_groups);
Same remark as above for "".
Quoted 61 lines
xen/drivers/passthrough/arm/smmu.c +26
-3return -ERANGE ; } master->cfg.smendx[i] =INVALID_SMENDX ;@@ -872,7 +893,7 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,struct device_node *dev_node =dev_get_dev_node (dev);int ret; - master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master ==NULL ) { dev_err(dev, "No registrations found for master device %s\n",@@ -884,8 +905,9 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,if (ret)return ret; -/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */ - dev_node->is_protected = false; +if ( !dev_is_pci (dev) ) +/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */ + dev_node->is_protected = false;kfree (master);return 0 ;@@ -914,6 +936,12 @@ static int register_smmu_master(struct arm_smmu_device *smmu, fwspec); } +/* Forward declaration */ +static int arm_smmu_assign_dev (struct domain *d,u8 devfn, +struct device *dev,u32 flag); +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn, +struct device *dev); +/* * The driver which supports generic IOMMU DT bindings must have this * callback implemented. @@ -938,6 +966,22 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) {struct arm_smmu_device *smmu;struct iommu_fwspec *fwspec; +int ret; + +#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);if (fwspec ==NULL )
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) · line 995
Quoted 6 lines
xen/drivers/passthrough/arm/smmu.c +2
-1@@ -947,7 +991,24 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)if (smmu ==NULL )return -ENXIO ; -return arm_smmu_dt_add_device_legacy (smmu, dev, fwspec); + ret =arm_smmu_dt_add_device_legacy (smmu, dev, fwspec); +if ( ret )
axc> + return ret;
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) · line 1006
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +10
-0+ +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +/* +* During PHYSDEVOP_pci_device_add, Xen does not assign the +* device, so we must do it here. +*/
In the context of dom0less, the hardware domain may not exist at all. And even if it exists, you will first assign the device to the hardware domain and then re-assign to the guest just after.
This looks wrong.
Quoted 69 lines
xen/drivers/passthrough/arm/smmu.c +14
-5+ ret =arm_smmu_assign_dev (pdev->domain, devfn, dev,0 ); + } +#endif + +return ret; } static int arm_smmu_dt_xlate_generic(struct device *dev,@@ -970,11 +1031,10 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev) {struct arm_smmu_device *smmu;struct arm_smmu_master *master =NULL ; -struct device_node *dev_node =dev_get_dev_node (dev);spin_lock (&arm_smmu_devices_lock);list_for_each_entry (smmu, &arm_smmu_devices, list) { - master =find_smmu_master (smmu, dev_node); + master =find_smmu_master (smmu, dev);if (master)break ; }@@ -2066,6 +2126,7 @@ static bool arm_smmu_capable(enum iommu_cap cap) }#endif +#if 0 /* Not used */ static int __arm_smmu_get_pci_sid (struct pci_dev *pdev,u16 alias,void *data) { *((u16 *)data) = alias;@@ -2076,6 +2137,7 @@ static void __arm_smmu_release_pci_iommudata(void *data) {kfree (data); } +#endif static int arm_smmu_add_device (struct device *dev) {@@ -2083,12 +2145,13 @@ static int arm_smmu_add_device(struct device *dev)struct arm_smmu_master_cfg *cfg;struct iommu_group *group;void (*releasefn)(void *data) =NULL ; -int ret; smmu =find_smmu_for_device (dev);if (!smmu)return -ENODEV ; +/* There is no need to distinguish here, thanks to PCI-IOMMU DT bindings */ +#if 0 if (dev_is_pci (dev)) {struct pci_dev *pdev =to_pci_dev (dev);struct iommu_fwspec *fwspec;@@ -2113,10 +2176,12 @@ static int arm_smmu_add_device(struct device *dev) &fwspec->ids[0]); releasefn = __arm_smmu_release_pci_iommudata; cfg->smmu = smmu; - }else { + } else +#endif + {struct arm_smmu_master *master; - master =find_smmu_master (smmu, dev->of_node); + master =find_smmu_master (smmu, dev);if (!master) {return -ENODEV ; }@@ -2784,6 +2849,61 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,return -ENOMEM ;
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2859
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +8
-0} +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) && !is_hardware_domain (d) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Assigning device %04x:%02x:%02x.%u to dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn),PCI_FUNC (devfn), + d->domain_id);
Is this a left-over debug? If not, shouldn't this happen in a caller instead because this is quite generic?
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2866
Quoted 7 lines
xen/drivers/passthrough/arm/smmu.c +7
-0+ +if ( devfn != pdev->devfn || pdev->domain == d ) +return 0 ; + +ASSERT (pcidevs_locked ()); + +/* TODO: acquire pci_lock */
What's the plan for this TODO? (The commit message is blank).
Quoted 16 lines
xen/drivers/passthrough/arm/smmu.c +16
-0+#if 0 +write_lock (&pdev->domain->pci_lock); +#endif +list_del (&pdev->domain_list); +#if 0 +write_unlock (&pdev->domain->pci_lock); +#endif + + pdev->domain = d; + +#if 0 +write_lock (&d->pci_lock); +#endif +list_add (&pdev->domain_list, &d->pdev_list); +#if 0 +write_unlock (&d->pci_lock);
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2892
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +10
-0+#endif + +if ( hardware_domain ) + { + domain =dev_iommu_domain (dev); + +/* +* Xen may not deassign the device from hwdom before +* assigning it elsewhere. +*/
This comment leave more question than it really answer. Why can't Xen call reassign? IOW, it feels wrong to reimplement re-assign within assign. At mimimum, this should be consolidated.
Quoted 26 lines
xen/drivers/passthrough/arm/smmu.c +16
-1+if ( domain &&is_hardware_domain (domain->priv->cfg.domain) ) + { + ret =arm_smmu_deassign_dev (hardware_domain, devfn, dev); +if ( ret ) +return ret; + } + } + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) +return 0 ; + } +#endif +if (!dev_iommu_group (dev)) { ret =arm_smmu_add_device (dev);if (ret)@@ -2833,11 +2953,30 @@ out:return ret; } -static int arm_smmu_deassign_dev (struct domain *d,struct device *dev) +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn, +struct device *dev) {struct iommu_domain *domain =dev_iommu_domain (dev);
xen/drivers/passthrough/arm/smmu.c out: · line 2969
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +8
-0struct arm_smmu_xen_domain *xen_domain; +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Deassigning device %04x:%02x:%02x.%u from dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn),PCI_FUNC (devfn), + d->domain_id);
Same questions as above for the printk.
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) · line 978
Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +3
-0+ +if ( devfn != pdev->devfn ) +return 0 ;
I don't understand this check. Are you sanity checking pdev->devfn, and if so shouldn't not this return an error?
Quoted 11 lines
xen/drivers/passthrough/arm/smmu.c +7
-0+ +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) + return 0; + } +#endif + xen_domain =dom_iommu (d)->arch.priv;if (!domain || domain->priv->cfg.domain != d) {
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_reassign_dev(struct domain *s, struct domain *t, · line 3006
Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +0
-0@@ -2865,13 +3004,13 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,int ret =0 ;/* Don't allow remapping on other domain than hwdom */
This comment now ...
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_reassign_dev(struct domain *s, struct domain *t, · line 3007
Quoted 2 lines
xen/drivers/passthrough/arm/smmu.c +1
-1-if ( t && !is_hardware_domain (t) ) +if ( t && !is_hardware_domain (t) && t != dom_io )
... now doesn't match the check. You are also relaxing for everyone but don't really explain why.
xen/drivers/passthrough/arm/smmu.c static int arm_smmu_reassign_dev(struct domain *s, struct domain *t, · line 3016
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +1
-1return -EPERM ;if (t == s)return 0 ; - ret =arm_smmu_deassign_dev (s, dev); + ret =arm_smmu_deassign_dev (s, devfn, dev);if (ret)return ret;
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 6/9 xen/arm: smmuv3: Add PCI devices support for SMMUv3
In reply to Stewart HildebrandFrom: Rahul Singh <[email protected]>
---v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()
v4->v5:
* deassign from hwdom
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")
v3->v4:
* no change
v2->v3:
* rebase
* invoke iommu_add_pci_sideband_ids() from add_device hook
v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)
downstream->v1:
* rebase
* move 2 replacements of s/dt_device_set_protected(dev_to_dt(dev))/device_set_protected(dev)/ from this commit to ("xen/arm: Move is_protected flag to struct device")
so as to not break ability to bisect
* adjust patch title (remove stray space)
* arm_smmu_(de)assign_dev: return error instead of crashing system
* remove arm_smmu_remove_device() stub
* update condition in arm_smmu_reassign_dev
* style fixup
(cherry picked from commit 7ed6c3ab250d899fe6e893a514278e406a2893e8 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)
--- xen/drivers/passthrough/arm/smmu-v3.c | 131 ++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 10 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.c +121 -10
@@ -1469,14 +1469,34 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) }/* Forward declaration */ static struct arm_smmu_device *arm_smmu_get_by_dev (const struct device *dev); +static int arm_smmu_assign_dev (struct domain *d,u8 devfn,struct device *dev, +u32 flag); +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn, +struct device *dev);static int arm_smmu_add_device (u8 devfn,struct device *dev) {int i, ret;struct arm_smmu_device *smmu;struct arm_smmu_master *master; -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);if (!fwspec)return -ENODEV ;
@@ -1521,17 +1541,35 @@ static int arm_smmu_add_device(u8 devfn, struct device *dev)*/ arm_smmu_enable_pasid (master); -if (dt_device_is_protected (dev_to_dt (dev))) { -dev_err (dev,"Already added to SMMUv3\n" ); -return -EEXIST ; - } +if ( !dev_is_pci (dev) ) + { +if (dt_device_is_protected (dev_to_dt (dev))) { +dev_err (dev,"Already added to SMMUv3\n" ); +return -EEXIST ; + } -/* Let Xen know that the master device is protected by an IOMMU. */ -dt_device_set_protected (dev_to_dt (dev)); +/* Let Xen know that the master device is protected by an IOMMU. */ +dt_device_set_protected (dev_to_dt (dev)); + }dev_info (dev,"Added master device (SMMUv3 %s StreamIds %u)\n" ,dev_name (fwspec->iommu_dev), fwspec->num_ids); +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +/* +* During PHYSDEVOP_pci_device_add, Xen does not assign the +* device, so we must do it here. +*/ + ret =arm_smmu_assign_dev (pdev->domain, devfn, dev,0 ); +if (ret) + goto err_free_master; + } +#endif +return 0 ; err_free_master:
@@ -2621,6 +2659,61 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,struct arm_smmu_domain *smmu_domain;struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) && !is_hardware_domain (d) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Assigning device %04x:%02x:%02x.%u to dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn), +PCI_FUNC (devfn), d->domain_id); + +if ( devfn != pdev->devfn || pdev->domain == d ) +return 0 ; + +ASSERT (pcidevs_locked ()); + +/* TODO: acquire pci_lock */ +#if 0 +write_lock (&pdev->domain->pci_lock); +#endif +list_del (&pdev->domain_list); +#if 0 +write_unlock (&pdev->domain->pci_lock); +#endif + + pdev->domain = d; + +#if 0 +write_lock (&d->pci_lock); +#endif +list_add (&pdev->domain_list, &d->pdev_list); +#if 0 +write_unlock (&d->pci_lock); +#endif + +if ( hardware_domain ) + { + io_domain =arm_smmu_get_domain (hardware_domain, dev); + +/* +* Xen may not deassign the device from hwdom before +* assigning it elsewhere. +*/ +if ( io_domain ) + { + ret =arm_smmu_deassign_dev (hardware_domain, devfn, dev); +if ( ret ) +return ret; + } + } + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) +return 0 ; + } +#endif +spin_lock (&xen_domain->lock);/*
@@ -2654,7 +2747,7 @@ out:return ret; } -static int arm_smmu_deassign_dev (struct domain *d,struct device *dev) +static int arm_smmu_deassign_dev (struct domain *d,uint8_t devfn,struct device *dev) {struct iommu_domain *io_domain =arm_smmu_get_domain (d, dev);struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv;
@@ -2666,6 +2759,24 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)return -ESRCH ; } +#ifdef CONFIG_HAS_PCI +if (dev_is_pci (dev) ) + { +struct pci_dev *pdev =dev_to_pci (dev); + +printk (XENLOG_INFO "Deassigning device %04x:%02x:%02x.%u from dom%d\n" , + pdev->seg, pdev->bus,PCI_SLOT (devfn), +PCI_FUNC (devfn), d->domain_id); + +if ( devfn != pdev->devfn ) +return 0 ; + +/* dom_io is used as a sentinel for quarantined devices */ +if ( d == dom_io ) +return 0 ; + } +#endif +spin_lock (&xen_domain->lock);arm_smmu_detach_dev (master);
@@ -2685,13 +2796,13 @@ static int arm_smmu_reassign_dev(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)return 0 ; - ret =arm_smmu_deassign_dev (s, dev); + ret =arm_smmu_deassign_dev (s, devfn, dev);if (ret)return ret;
Signature · 1 line
2.42.0
Re: [PATCH v6 6/9] xen/arm: smmuv3: Add PCI devices support for SMMUv3
Hi,
Quoted 2 lines · Stewart Hildebrand
From: Rahul Singh <[email protected]>
Looking at the code, most of my comments on SMMUv2 apply here too. So I will not repeat them.
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 7/9 xen/arm: Fix mapping for PCI bridge mmio region
In reply to Stewart HildebrandFrom: Rahul Singh <[email protected]>
Current code skip the mapping for PCI bridge MMIO region to dom0 when pci_passthrough_enabled flag is set. Mapping should be skip when has_vpci(d) is enabled for the domain, as we need to skip the mapping only when VPCI handler are registered for ECAM.
---
This patch was originally picked up from [1]
v5->v6:
* drop unrelated change in xen/arch/arm/domain_build.c:handle_linux_pci_domain()
v4->v5:
* new patch
changes since picking up from [1]:
* rebase on top of "dynamic node programming using overlay dtbo" series
* replace !is_pci_passthrough_enabled() check with !IS_ENABLED(CONFIG_HAS_PCI) instead of removing
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
--- xen/arch/arm/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/device.c +1 -1
@@ -330,7 +330,7 @@ int handle_device(struct domain *d, struct dt_device_node *dev, p2m_type_t p2mt, .d = d, .p2mt = p2mt, .skip_mapping = !own_device || - (is_pci_passthrough_enabled () && + (has_vpci (d) && (device_get_class(dev) == DEVICE_PCI_HOSTBRIDGE)), .iomem_ranges = iomem_ranges, .irq_ranges = irq_ranges
Signature · 1 line
2.42.0
Re: [PATCH v6 7/9] xen/arm: Fix mapping for PCI bridge mmio region
Hi Stewart,
Quoted 9 lines · Stewart Hildebrand
From: Rahul Singh <[email protected]>
Current code skip the mapping for PCI bridge MMIO region to dom0 when
pci_passthrough_enabledflag is set. Mapping should be skip whenhas_vpci(d)is enabled for the domain, as we need to skip the mapping only when VPCI handler are registered for ECAM.Signed-off-by Rahul Singh <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 8/9 xen/arm: enable dom0 to use PCI devices with pci-passthrough=no
In reply to Stewart HildebrandEnable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.
Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.
---v5->v6:
* new patch - this effectively replaces
("Revert "xen/arm: Add cmdline boot option "pci-passthrough = <boolean>""")
--- xen/arch/arm/pci/pci.c | 3 ++- xen/drivers/pci/physdev.c | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-)
xen/arch/arm/pci/pci.c +2 -1
@@ -16,6 +16,7 @@#include <xen/device_tree.h> #include <xen/errno.h> #include <xen/init.h> +#include <xen/iommu.h> #include <xen/param.h> #include <xen/pci.h>
@@ -85,7 +86,7 @@ static int __init pci_init(void)* Enable PCI passthrough when has been enabled explicitly * (pci-passthrough=on). */ -if ( !pci_passthrough_enabled ) +if ( !is_pci_passthrough_enabled () && !iommu_enabled )return 0 ;pci_segments_init ();
xen/drivers/pci/physdev.c +0 -6
@@ -18,9 +18,6 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)struct pci_dev_info pdev_info;nodeid_t node =NUMA_NO_NODE ; -if ( !is_pci_passthrough_enabled () ) -return -EOPNOTSUPP ; - ret = -EFAULT ;if (copy_from_guest (&add, arg,1 ) !=0 )break ;
@@ -56,9 +53,6 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)case PHYSDEVOP_pci_device_remove: {struct physdev_pci_device dev; -if ( !is_pci_passthrough_enabled () ) -return -EOPNOTSUPP ; - ret = -EFAULT ;if (copy_from_guest (&dev, arg,1 ) !=0 )break ;
Signature · 1 line
2.42.0
Re: [PATCH v6 8/9] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no
Quoted 13 lines · Stewart Hildebrand
xen/drivers/pci/physdev.c +0
-3@@ -18,9 +18,6 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)struct pci_dev_info pdev_info;nodeid_t node =NUMA_NO_NODE ; -if ( !is_pci_passthrough_enabled () ) -return -EOPNOTSUPP ; - ret = -EFAULT ;if (copy_from_guest (&add, arg,1 ) !=0 )break ;
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 58
Quoted 9 lines
xen/drivers/pci/physdev.c +0
-3@@ -56,9 +53,6 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)case PHYSDEVOP_pci_device_remove: {struct physdev_pci_device dev; -if ( !is_pci_passthrough_enabled () ) -return -EOPNOTSUPP ; - ret = -EFAULT ;if (copy_from_guest (&dev, arg,1 ) !=0 )break ;
This renders is_pci_passthrough_enabled() and Arm-only construct. IOW the x86 definition of the function should then also be removed, for ending up dead otherwise.
Jan
Re: [PATCH v6 8/9] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no
In reply to Stewart HildebrandHi Stewart,
Quoted 30 lines · Stewart Hildebrand
Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a
PHYSDEVOP_pci_device_addcall to add each device to SMMU.Enable
pci_init()for initializing Xen's internal PCI subsystem, and allowPHYSDEVOP_pci_device_addwhen pci-passthrough is disabled.Signed-off-by Stewart Hildebrand <[email protected]>---
v5->v6:
* new patch - this effectively replaces
("Revert "xen/arm: Add cmdline boot option "pci-passthrough = <boolean>""")--- xen/arch/arm/pci/pci.c | 3 ++- xen/drivers/pci/physdev.c | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-)xen/arch/arm/pci/pci.c +1
-0@@ -16,6 +16,7 @@#include <xen/device_tree.h> #include <xen/errno.h> #include <xen/init.h> +#include <xen/iommu.h> #include <xen/param.h> #include <xen/pci.h>
xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 88
Quoted 3 lines
xen/arch/arm/pci/pci.c +0
-0@@ -85,7 +86,7 @@ static int __init pci_init(void)* Enable PCI passthrough when has been enabled explicitly * (pci-passthrough=on). */
The comment needs to be updated after ...
xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 89
Quoted 2 lines
xen/arch/arm/pci/pci.c +1
-1-if ( !pci_passthrough_enabled ) +if ( !is_pci_passthrough_enabled () && !iommu_enabled )
... this change.
Quoted 8 lines
xen/arch/arm/pci/pci.c +0
-0return 0 ;pci_segments_init ();xen/drivers/pci/physdev.c +0
-0
xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23
Quoted 6 lines
xen/drivers/pci/physdev.c +0
-3@@ -18,9 +18,6 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)struct pci_dev_info pdev_info;nodeid_t node =NUMA_NO_NODE ; -if ( !is_pci_passthrough_enabled () ) -return -EOPNOTSUPP ; -
AFAIU, this means that pci_add_device() can now be reached even if pci_segments_init() is not called (this can happen when iommu=false). What error will now be returned and how dom0 will behave?
Cheers,
Signature · 1 line
Julien Grall
PATCH v6 9/9 xen/arm: Map ITS doorbell register to IOMMU page tables
In reply to Stewart HildebrandFrom: Rahul Singh <[email protected]>
When ITS is enabled and PCI devices that are behind an SMMU generate an MSI interrupt, SMMU fault will be observed as there is currently no mapping in p2m table for the ITS translation register (GITS_TRANSLATER).
A mapping is required in the p2m page tables so that the device can generate the MSI interrupt writing to the GITS_TRANSLATER register.
The GITS_TRANSLATER register is a 32-bit register, so map a single page.
---
This patch was originally picked up from [1], and commit description loosely borrowed from [2].
Example SMMUv3 fault (qemu-system-aarch64 virt model), ITS base 0x8080000:
(XEN) SMMUv3: /smmuv3@9050000: event 0x10 received:
(XEN) SMMUv3: /smmuv3@9050000: 0x0000000800000010 (XEN) SMMUv3: /smmuv3@9050000: 0x0000008000000000 (XEN) SMMUv3: /smmuv3@9050000: 0x0000000008090040 (XEN) SMMUv3: /smmuv3@9050000: 0x0000000000000000
Example SMMUv2 fault (AMD/Xilinx Versal), ITS base 0xf9020000:
(XEN) smmu: /axi/smmu@fd800000: Unhandled context fault: fsr=0x402, iova=0xf9030040, fsynr=0x12, cb=0
v5->v6:
* switch to iommu_map() interface
* fix page_count argument
* style fixup
* use gprintk instead of printk
* add my Signed-off-by
* move to vgic_v3_its_init_virtual()
v4->v5:
* new patch
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
[2] https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc/-/commit/6232a0d53377009bb7fbc3c3ab81d0153734be6b
--- xen/arch/arm/vgic-v3-its.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
xen/arch/arm/vgic-v3-its.c +15 -0
@@ -1477,6 +1477,21 @@ static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr,register_mmio_handler (d, &vgic_its_mmio_handler, guest_addr,SZ_64K , its); +if (is_iommu_enabled (its->d) ) + { +mfn_t mfn =maddr_to_mfn (its->doorbell_address); +unsigned int flush_flags =0 ; +int ret =iommu_map (its->d,_dfn (mfn_x (mfn)), mfn,1 , IOMMUF_writable, + &flush_flags); +if ( ret <0 ) + { +gprintk (XENLOG_ERR , +"GICv3: Map ITS translation register %pd failed.\n" , + its->d); +return ret; + } + } +/* Register the virtual ITS to be able to clean it up later. */ list_add_tail (&its->vits_list, &d->arch.vgic.vits_list);
Signature · 1 line
2.42.0
Re: [PATCH v6 9/9] xen/arm: Map ITS doorbell register to IOMMU page tables
Hi,
Quoted 7 lines · Stewart Hildebrand
From: Rahul Singh <[email protected]>
When ITS is enabled and PCI devices that are behind an SMMU generate an MSI interrupt, SMMU fault will be observed as there is currently no mapping in p2m table for the ITS translation register (
GITS_TRANSLATER).A mapping is required in the p2m page tables so that the device can
Not quite. The mapping is required in the device page-table. But not the P2M. So this needs to be updated to match what the code is (correctly) doing.
Quoted 3 lines
generate the MSI interrupt writing to the
GITS_TRANSLATERregister.The
GITS_TRANSLATERregister is a 32-bit register, so map a single page.
and there is nothing else critical in the page.
Quoted 41 lines
Signed-off-by Rahul Singh <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>---
This patch was originally picked up from [1], and commit description loosely borrowed from [2].Example SMMUv3 fault (qemu-system-aarch64 virt model), ITS base
0x8080000:(XEN) SMMUv3: /smmuv3@9050000: event
0x10received:(XEN) SMMUv3: /smmuv3@9050000: 0x0000000800000010 (XEN) SMMUv3: /smmuv3@9050000: 0x0000008000000000 (XEN) SMMUv3: /smmuv3@9050000: 0x0000000008090040 (XEN) SMMUv3: /smmuv3@9050000: 0x0000000000000000Example SMMUv2 fault (AMD/Xilinx Versal), ITS base
0xf9020000:(XEN) smmu: /axi/smmu@fd800000: Unhandled context fault: fsr=0x402, iova=0xf9030040, fsynr=0x12, cb=0
v5->v6:
* switch toiommu_map()interface
* fixpage_countargument
* style fixup
* use gprintk instead of printk
* add my Signed-off-by
* move tovgic_v3_its_init_virtual()
v4->v5:
* new patch[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
[2] https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc/-/commit/6232a0d53377009bb7fbc3c3ab81d0153734be6b--- xen/arch/arm/vgic-v3-its.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)xen/arch/arm/vgic-v3-its.c +0
-0
xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1485
Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +6
-0@@ -1477,6 +1477,21 @@ static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr,register_mmio_handler (d, &vgic_its_mmio_handler, guest_addr,SZ_64K , its); +if (is_iommu_enabled (its->d) ) + { +mfn_t mfn =maddr_to_mfn (its->doorbell_address); +unsigned int flush_flags =0 ; +int ret =iommu_map (its->d,_dfn (mfn_x (mfn)), mfn,1 , IOMMUF_writable, + &flush_flags);
From a generic perspective, iommu_map() can set flush_flags. Yet you are not doing anything with it. Should not we call iommu_iotlb_flush_all()?
Also, coding style: Newline.
xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1488
Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3
-0+if ( ret <0 ) + { + gprintk(XENLOG_ERR,
NIT: gprintk() will print the current domain (e.g. toolstack domain). I would consider to use XENLOG_G_ERR instead to avoid unnecessary information.
xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1489
Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1
-0+ "GICv3: Map ITS translation register %pd failed.\n",
Did you miss "for" before "%pd"?
xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1497
Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +5
-0+ its->d); +return ret; + } + } +/* Register the virtual ITS to be able to clean it up later. */ list_add_tail (&its->vits_list, &d->arch.vgic.vits_list);
Cheers,
Signature · 1 line
Julien Grall
Quoted 66 lines · Stewart Hildebrand
This series introduces SMMU handling for PCIe passthrough on ARM. These patches should be able to be upstreamed independently from the vPCI series [1]. See [2] for notes about test cases.
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-10/msg00660.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-06/msg01135.html
v5->v6:
* don't revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: enable dom0 to use PCI devices with pci-passthrough=no")
v4->v5:
* drop ("xen/arm: Improve readability of check for registered devices")
* drop ("xen/arm: Moveis_protectedflag to struct device")
* add ("xen/arm: don't pass iommu properties to hwdom for iommu-map")
* add ("xen/arm: Fix mapping for PCI bridge mmio region")
* revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: Map ITS doorbell register to IOMMU page tables.")
* fix test case #1 with PCI device in dom0
v3->v4:
* split a change from ("xen/arm: Moveis_protectedflag to struct device") into a new separate patch
* see individual patches for further details
v2->v3:
* drop "pci/arm: Useiommu_add_dt_pci_device()"
* drop "RFC: pci/arm: don't do iommu call for phantom functions"
* move invocation of sideband ID mapping function toadd_device()platform_ops/iommu_ops hook
v1->v2:
* phantom device handling
* shuffle aroundiommu_add_dt_pci_device()Oleksandr Andrushchenko (1):
xen/arm: smmuv2: Add PCI devices support for SMMUv2Oleksandr Tyshchenko (2):
iommu/arm: Addiommu_dt_xlate()
iommu/arm: Introduceiommu_add_dt_pci_sideband_idsAPIRahul Singh (3):
xen/arm: smmuv3: Add PCI devices support for SMMUv3 xen/arm: Fix mapping for PCI bridge mmio region xen/arm: Map ITS doorbell register to IOMMU page tablesStewart Hildebrand (3):
xen/arm: don't pass iommu properties to hwdom for iommu-map iommu/arm:iommu_add_dt_pci_sideband_idsphantom handling xen/arm: enable dom0 to use PCI devices with pci-passthrough=noxen/arch/arm/device.c | 2 +- xen/arch/arm/domain_build.c | 2 + xen/arch/arm/pci/pci.c | 3 +- xen/arch/arm/vgic-v3-its.c | 15 ++ xen/common/device_tree.c | 91 ++++++++++++ xen/drivers/passthrough/arm/smmu-v3.c | 131 +++++++++++++++-- xen/drivers/passthrough/arm/smmu.c | 199 ++++++++++++++++++++++---- xen/drivers/passthrough/device_tree.c | 97 ++++++++++--- xen/drivers/pci/physdev.c | 6 - xen/include/xen/device_tree.h | 23 +++ xen/include/xen/iommu.h | 26 +++- 11 files changed, 528 insertions(+), 67 deletions(-)
Quoted 1 line
base-commit:
bede1c7e3b790b63f1ff3ea3ee4e476b012fdf2c
Hi Stewart,
I noticed there was no activity for this series for some time. Are you still working on this or would it be okay if I take this over and start preparing the V7?
Signature · 1 line
Mykyta
Quoted 67 lines · Mykyta Poturai
Quoted 66 lines · Stewart Hildebrand
This series introduces SMMU handling for PCIe passthrough on ARM. These patches should be able to be upstreamed independently from the vPCI series [1]. See [2] for notes about test cases.
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-10/msg00660.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-06/msg01135.html
v5->v6:
* don't revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: enable dom0 to use PCI devices with pci-passthrough=no")
v4->v5:
* drop ("xen/arm: Improve readability of check for registered devices")
* drop ("xen/arm: Moveis_protectedflag to struct device")
* add ("xen/arm: don't pass iommu properties to hwdom for iommu-map")
* add ("xen/arm: Fix mapping for PCI bridge mmio region")
* revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: Map ITS doorbell register to IOMMU page tables.")
* fix test case #1 with PCI device in dom0
v3->v4:
* split a change from ("xen/arm: Moveis_protectedflag to struct device") into a new separate patch
* see individual patches for further details
v2->v3:
* drop "pci/arm: Useiommu_add_dt_pci_device()"
* drop "RFC: pci/arm: don't do iommu call for phantom functions"
* move invocation of sideband ID mapping function toadd_device()platform_ops/iommu_ops hook
v1->v2:
* phantom device handling
* shuffle aroundiommu_add_dt_pci_device()Oleksandr Andrushchenko (1):
xen/arm: smmuv2: Add PCI devices support for SMMUv2Oleksandr Tyshchenko (2):
iommu/arm: Addiommu_dt_xlate()
iommu/arm: Introduceiommu_add_dt_pci_sideband_idsAPIRahul Singh (3):
xen/arm: smmuv3: Add PCI devices support for SMMUv3 xen/arm: Fix mapping for PCI bridge mmio region xen/arm: Map ITS doorbell register to IOMMU page tablesStewart Hildebrand (3):
xen/arm: don't pass iommu properties to hwdom for iommu-map iommu/arm:iommu_add_dt_pci_sideband_idsphantom handling xen/arm: enable dom0 to use PCI devices with pci-passthrough=noxen/arch/arm/device.c | 2 +- xen/arch/arm/domain_build.c | 2 + xen/arch/arm/pci/pci.c | 3 +- xen/arch/arm/vgic-v3-its.c | 15 ++ xen/common/device_tree.c | 91 ++++++++++++ xen/drivers/passthrough/arm/smmu-v3.c | 131 +++++++++++++++-- xen/drivers/passthrough/arm/smmu.c | 199 ++++++++++++++++++++++---- xen/drivers/passthrough/device_tree.c | 97 ++++++++++--- xen/drivers/pci/physdev.c | 6 - xen/include/xen/device_tree.h | 23 +++ xen/include/xen/iommu.h | 26 +++- 11 files changed, 528 insertions(+), 67 deletions(-)
Quoted 7 lines
Quoted 1 line
base-commit:
bede1c7e3b790b63f1ff3ea3ee4e476b012fdf2cHi Stewart,
I noticed there was no activity for this series for some time. Are you still working on this or would it be okay if I take this over and start preparing the V7?
Hi Mykyta,
Go ahead, please feel free to take it over. Your help is very much appreciated. I made a couple of updates, but the feedback still needs to be addressed. Please see my latest working branch here:
https://github.com/stewdk/xen/tree/pcie-passthrough-smmu-v7