@@ -151,6 +151,146 @@ static int iommu_dt_xlate(struct device *dev,
return ops->dt_xlate(dev, iommu_spec);
}
+#ifdef CONFIG_HAS_PCI
+int iommu_dt_pci_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 "%pOF: Error: Bad %s length: %d\n", np,
+ map_name, map_len);
+ return -EINVAL;
+ }
+
+ /* The default is to select all bits. */
+ map_mask = 0xffffffff;
+
+ /*
+ * Can be overridden by "{iommu,msi}-map-mask" property.
+ * If of_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 ( ; (int)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 "%pOF: Invalid %s translation - %s-mask (0x%x) ignores id-base (0x%x)\n",
+ np, 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;
+
+ printk(XENLOG_DEBUG "%pOF: %s, using mask %08x, id-base: %08x, out-base: %08x, length: %08x, id: %08x -> %08x\n",
+ np, map_name, map_mask, id_base, out_base, id_len, id,
+ masked_id - id_base + out_base);
+ return 0;
+ }
+
+ printk(XENLOG_ERR "%pOF: no %s translation for id 0x%x on %pOF\n",
+ np, map_name, id, target && *target ? *target : NULL);
+
+ /*
+ * NOTE: Linux bypasses translation without returning an error here,
+ * but should we behave in the same way on Xen? Restrict for now.
+ */
+ return -EFAULT;
+}
+
+int iommu_add_dt_pci_device(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 = NO_IOMMU;
+
+ if ( !iommu_enabled )
+ return NO_IOMMU;
+
+ if ( !ops )
+ return -EINVAL;
+
+ if ( device_is_protected(dev) )
+ return 0;
+
+ if ( dev_iommu_fwspec_get(dev) )
+ return -EEXIST;
+
+ np = pci_find_host_bridge_node(pdev);
+ if ( !np )
+ return -ENODEV;
+
+ /*
+ * The driver which supports generic PCI-IOMMU DT bindings must have
+ * these callback implemented.
+ */
+ if ( !ops->add_device || !ops->dt_xlate )
+ return -EINVAL;
+
+ /*
+ * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt
+ * from Linux.
+ */
+ rc = iommu_dt_pci_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);
+ if ( rc < 0 )
+ {
+ iommu_fwspec_free(pci_to_dev(pdev));
+ return -EINVAL;
+ }
+
+ return rc;
+}
+#endif /* CONFIG_HAS_PCI */
+
int iommu_add_dt_device(struct dt_device_node *np)
{
const struct iommu_ops *ops = iommu_get_ops();
@@ -852,6 +852,31 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,
*/
int dt_get_pci_domain_nr(struct dt_device_node *node);
+#ifdef CONFIG_HAS_PCI
+/**
+ * iommu_dt_pci_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 iommu_dt_pci_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);
+#endif /* CONFIG_HAS_PCI */
+
struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle);
#ifdef CONFIG_DEVICE_TREE_DEBUG
@@ -219,7 +219,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
@@ -228,6 +229,9 @@ 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);
+#ifdef CONFIG_HAS_PCI
+int iommu_add_dt_pci_device(struct pci_dev *pdev);
+#endif
int iommu_do_dt_domctl(struct xen_domctl *, struct domain *,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t));