[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [xen staging] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API
commit 7f661b7b401a2869f750a3086ac93c2449b40d47 Author: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx> AuthorDate: Wed Jul 2 07:10:00 2025 +0000 Commit: Stefano Stabellini <stefano.stabellini@xxxxxxx> CommitDate: Thu Jul 3 14:45:45 2025 -0700 iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API 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 Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@xxxxxxxx> Signed-off-by: Stewart Hildebrand <stewart.hildebrand@xxxxxxx> Signed-off-by: Mykyta Poturai <mykyta_poturai@xxxxxxxx> Reviewed-by: Stefano Stabellini <sstabellini@xxxxxxxxxx> Acked-by: Jan Beulich <jbeulich@xxxxxxxx> --- xen/arch/arm/include/asm/iommu.h | 15 ++++++ xen/common/device-tree/device-tree.c | 91 +++++++++++++++++++++++++++++++++++ xen/drivers/passthrough/arm/iommu.c | 13 +++++ xen/drivers/passthrough/device_tree.c | 42 ++++++++++++++++ xen/include/xen/device_tree.h | 23 +++++++++ xen/include/xen/iommu.h | 21 +++++++- 6 files changed, 204 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/include/asm/iommu.h b/xen/arch/arm/include/asm/iommu.h index d57bd8a38c..ad15477e24 100644 --- a/xen/arch/arm/include/asm/iommu.h +++ b/xen/arch/arm/include/asm/iommu.h @@ -34,6 +34,21 @@ int __must_check arm_iommu_unmap_page(struct domain *d, dfn_t dfn, unsigned int order, unsigned int *flush_flags); +/* + * This function is not strictly ARM-specific, but it is only used by ARM + * as of now. So put it here to avoid creating dead code on other + * architectures. When usage is extended to other architectures, it should + * be moved to the generic header. + * + * + * Fills out the device's IOMMU fwspec with IOMMU ids. + * + * Return values: + * 0 : iommu_fwspec is filled out successfully. + * <0 : error while filling out the iommu_fwspec. + * >0 : IOMMU is not enabled/present or device is not connected to it. + */ +int iommu_add_pci_sideband_ids(struct pci_dev *pdev); #endif /* __ARCH_ARM_IOMMU_H__ */ /* diff --git a/xen/common/device-tree/device-tree.c b/xen/common/device-tree/device-tree.c index 886e6c7712..7bede20fa6 100644 --- a/xen/common/device-tree/device-tree.c +++ b/xen/common/device-tree/device-tree.c @@ -7,6 +7,7 @@ * benh@xxxxxxxxxxxxxxxxxxx */ +#include <xen/bitops.h> #include <xen/types.h> #include <xen/init.h> #include <xen/guest_access.h> @@ -2221,6 +2222,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_cpu(*(map + 0)); + uint32_t phandle = be32_to_cpu(*(map + 1)); + uint32_t out_base = be32_to_cpu(*(map + 2)); + uint32_t id_len = be32_to_cpu(*(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 diff --git a/xen/drivers/passthrough/arm/iommu.c b/xen/drivers/passthrough/arm/iommu.c index fc453180f0..100545e23f 100644 --- a/xen/drivers/passthrough/arm/iommu.c +++ b/xen/drivers/passthrough/arm/iommu.c @@ -15,6 +15,7 @@ * GNU General Public License for more details. */ +#include <xen/acpi.h> #include <xen/device_tree.h> #include <xen/iommu.h> #include <xen/lib.h> @@ -151,3 +152,15 @@ bool arch_iommu_use_permitted(const struct domain *d) { return true; } + +int iommu_add_pci_sideband_ids(struct pci_dev *pdev) +{ + int ret = -EOPNOTSUPP; + +#ifdef CONFIG_HAS_PCI + if ( acpi_disabled ) + ret = iommu_add_dt_pci_sideband_ids(pdev); +#endif + + return ret; +} diff --git a/xen/drivers/passthrough/device_tree.c b/xen/drivers/passthrough/device_tree.c index 4a1971c3fc..37e1437b65 100644 --- a/xen/drivers/passthrough/device_tree.c +++ b/xen/drivers/passthrough/device_tree.c @@ -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 1; + + 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) ? 1 : 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(); diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h index d2de7c3a13..a7cc092d05 100644 --- a/xen/include/xen/device_tree.h +++ b/xen/include/xen/device_tree.h @@ -959,6 +959,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); void device_tree_get_reg(const __be32 **cell, uint32_t address_cells, diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index 832775754b..ebfada1d88 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -241,7 +241,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 @@ -251,6 +252,19 @@ int iommu_release_dt_devices(struct domain *d); */ int iommu_add_dt_device(struct dt_device_node *np); +/* + * Fills out the device's IOMMU fwspec with IOMMU ids from the DT. + * Ids are specified in the iommu-map property in the host bridge node. + * More information on the iommu-map property format can be found in + * Documentation/devicetree/bindings/pci/pci-iommu.txt from Linux. + * + * Return values: + * 0 : iommu_fwspec is filled out successfully. + * <0 : error while filling out the iommu_fwspec. + * >0 : IOMMU is not enabled/present or device is not connected to it. + */ +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); @@ -286,6 +300,11 @@ static inline int iommu_release_dt_devices(struct domain *d) return 0; } +static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev) +{ + return -EOPNOTSUPP; +} + #endif /* HAS_PASSTHROUGH */ #endif /* HAS_DEVICE_TREE */ -- generated by git-patchbot for /home/xen/git/xen.git#staging
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |