2 * Copyright (C) 2016, Semihalf
3 * Author: Tomasz Nowicki <tn@semihalf.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * This file implements early detection/parsing of I/O mapping
15 * reported to OS through firmware via I/O Remapping Table (IORT)
16 * IORT document number: ARM DEN 0049A
19 #define pr_fmt(fmt) "ACPI: IORT: " fmt
21 #include <linux/acpi_iort.h>
22 #include <linux/iommu.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 #define IORT_TYPE_MASK(type) (1 << (type))
30 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
31 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
32 (1 << ACPI_IORT_NODE_SMMU_V3))
34 /* Until ACPICA headers cover IORT rev. C */
35 #ifndef ACPI_IORT_SMMU_V3_CAVIUM_CN99XX
36 #define ACPI_IORT_SMMU_V3_CAVIUM_CN99XX 0x2
39 struct iort_its_msi_chip {
40 struct list_head list;
41 struct fwnode_handle *fw_node;
46 struct list_head list;
47 struct acpi_iort_node *iort_node;
48 struct fwnode_handle *fwnode;
50 static LIST_HEAD(iort_fwnode_list);
51 static DEFINE_SPINLOCK(iort_fwnode_lock);
54 * iort_set_fwnode() - Create iort_fwnode and use it to register
55 * iommu data in the iort_fwnode_list
57 * @node: IORT table node associated with the IOMMU
58 * @fwnode: fwnode associated with the IORT node
60 * Returns: 0 on success
63 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
64 struct fwnode_handle *fwnode)
66 struct iort_fwnode *np;
68 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
73 INIT_LIST_HEAD(&np->list);
74 np->iort_node = iort_node;
77 spin_lock(&iort_fwnode_lock);
78 list_add_tail(&np->list, &iort_fwnode_list);
79 spin_unlock(&iort_fwnode_lock);
85 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
87 * @node: IORT table node to be looked-up
89 * Returns: fwnode_handle pointer on success, NULL on failure
92 struct fwnode_handle *iort_get_fwnode(struct acpi_iort_node *node)
94 struct iort_fwnode *curr;
95 struct fwnode_handle *fwnode = NULL;
97 spin_lock(&iort_fwnode_lock);
98 list_for_each_entry(curr, &iort_fwnode_list, list) {
99 if (curr->iort_node == node) {
100 fwnode = curr->fwnode;
104 spin_unlock(&iort_fwnode_lock);
110 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
112 * @node: IORT table node associated with fwnode to delete
114 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
116 struct iort_fwnode *curr, *tmp;
118 spin_lock(&iort_fwnode_lock);
119 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
120 if (curr->iort_node == node) {
121 list_del(&curr->list);
126 spin_unlock(&iort_fwnode_lock);
129 typedef acpi_status (*iort_find_node_callback)
130 (struct acpi_iort_node *node, void *context);
132 /* Root pointer to the mapped IORT table */
133 static struct acpi_table_header *iort_table;
135 static LIST_HEAD(iort_msi_chip_list);
136 static DEFINE_SPINLOCK(iort_msi_chip_lock);
139 * iort_register_domain_token() - register domain token and related ITS ID
140 * to the list from where we can get it back later on.
142 * @fw_node: Domain token.
144 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
146 int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
148 struct iort_its_msi_chip *its_msi_chip;
150 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
154 its_msi_chip->fw_node = fw_node;
155 its_msi_chip->translation_id = trans_id;
157 spin_lock(&iort_msi_chip_lock);
158 list_add(&its_msi_chip->list, &iort_msi_chip_list);
159 spin_unlock(&iort_msi_chip_lock);
165 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
170 void iort_deregister_domain_token(int trans_id)
172 struct iort_its_msi_chip *its_msi_chip, *t;
174 spin_lock(&iort_msi_chip_lock);
175 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
176 if (its_msi_chip->translation_id == trans_id) {
177 list_del(&its_msi_chip->list);
182 spin_unlock(&iort_msi_chip_lock);
186 * iort_find_domain_token() - Find domain token based on given ITS ID
189 * Returns: domain token when find on the list, NULL otherwise
191 struct fwnode_handle *iort_find_domain_token(int trans_id)
193 struct fwnode_handle *fw_node = NULL;
194 struct iort_its_msi_chip *its_msi_chip;
196 spin_lock(&iort_msi_chip_lock);
197 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
198 if (its_msi_chip->translation_id == trans_id) {
199 fw_node = its_msi_chip->fw_node;
203 spin_unlock(&iort_msi_chip_lock);
208 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
209 iort_find_node_callback callback,
212 struct acpi_iort_node *iort_node, *iort_end;
213 struct acpi_table_iort *iort;
219 /* Get the first IORT node */
220 iort = (struct acpi_table_iort *)iort_table;
221 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
223 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
226 for (i = 0; i < iort->node_count; i++) {
227 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
228 "IORT node pointer overflows, bad table!\n"))
231 if (iort_node->type == type &&
232 ACPI_SUCCESS(callback(iort_node, context)))
235 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
242 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
245 struct device *dev = context;
246 acpi_status status = AE_NOT_FOUND;
248 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
249 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
250 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
251 struct acpi_iort_named_component *ncomp;
256 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
257 if (ACPI_FAILURE(status)) {
258 dev_warn(dev, "Can't get device full path name\n");
262 ncomp = (struct acpi_iort_named_component *)node->node_data;
263 status = !strcmp(ncomp->device_name, buf.pointer) ?
264 AE_OK : AE_NOT_FOUND;
265 acpi_os_free(buf.pointer);
266 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
267 struct acpi_iort_root_complex *pci_rc;
270 bus = to_pci_bus(dev);
271 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
274 * It is assumed that PCI segment numbers maps one-to-one
275 * with root complexes. Each segment number can represent only
278 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
279 AE_OK : AE_NOT_FOUND;
285 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
288 /* Single mapping does not care for input id */
289 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
290 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
291 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
292 *rid_out = map->output_base;
296 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
301 if (rid_in < map->input_base ||
302 (rid_in >= map->input_base + map->id_count))
305 *rid_out = map->output_base + (rid_in - map->input_base);
310 struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
311 u32 *id_out, int index)
313 struct acpi_iort_node *parent;
314 struct acpi_iort_id_mapping *map;
316 if (!node->mapping_offset || !node->mapping_count ||
317 index >= node->mapping_count)
320 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
321 node->mapping_offset + index * sizeof(*map));
324 if (!map->output_reference) {
325 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
330 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
331 map->output_reference);
333 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
334 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
335 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
336 *id_out = map->output_base;
344 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
345 u32 id_in, u32 *id_out,
350 /* Parse the ID mapping tree to find specified node type */
352 struct acpi_iort_id_mapping *map;
355 if (IORT_TYPE_MASK(node->type) & type_mask) {
361 if (!node->mapping_offset || !node->mapping_count)
364 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
365 node->mapping_offset);
368 if (!map->output_reference) {
369 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
374 /* Do the ID translation */
375 for (i = 0; i < node->mapping_count; i++, map++) {
376 if (!iort_id_map(map, node->type, id, &id))
380 if (i == node->mapping_count)
383 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
384 map->output_reference);
388 /* Map input ID to output ID unchanged on mapping failure */
396 struct acpi_iort_node *iort_node_map_platform_id(struct acpi_iort_node *node,
397 u32 *id_out, u8 type_mask,
400 struct acpi_iort_node *parent;
403 /* step 1: retrieve the initial dev id */
404 parent = iort_node_get_id(node, &id, index);
409 * optional step 2: map the initial dev id if its parent is not
410 * the target type we want, map it again for the use cases such
411 * as NC (named component) -> SMMU -> ITS. If the type is matched,
412 * return the initial dev id and its parent pointer directly.
414 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
415 parent = iort_node_map_id(parent, id, id_out, type_mask);
423 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
425 struct pci_bus *pbus;
427 if (!dev_is_pci(dev))
428 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
429 iort_match_node_callback, dev);
431 /* Find a PCI root bus */
432 pbus = to_pci_dev(dev)->bus;
433 while (!pci_is_root_bus(pbus))
436 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
437 iort_match_node_callback, &pbus->dev);
441 * iort_msi_map_rid() - Map a MSI requester ID for a device
442 * @dev: The device for which the mapping is to be done.
443 * @req_id: The device requester ID.
445 * Returns: mapped MSI RID on success, input requester ID otherwise
447 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
449 struct acpi_iort_node *node;
452 node = iort_find_dev_node(dev);
456 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
461 * iort_pmsi_get_dev_id() - Get the device id for a device
462 * @dev: The device for which the mapping is to be done.
463 * @dev_id: The device ID found.
465 * Returns: 0 for successful find a dev id, -ENODEV on error
467 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
470 struct acpi_iort_node *node;
472 node = iort_find_dev_node(dev);
476 for (i = 0; i < node->mapping_count; i++) {
477 if (iort_node_map_platform_id(node, dev_id, IORT_MSI_TYPE, i))
485 * iort_dev_find_its_id() - Find the ITS identifier for a device
487 * @req_id: Device's requester ID
488 * @idx: Index of the ITS identifier list.
489 * @its_id: ITS identifier.
491 * Returns: 0 on success, appropriate error value otherwise
493 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
494 unsigned int idx, int *its_id)
496 struct acpi_iort_its_group *its;
497 struct acpi_iort_node *node;
499 node = iort_find_dev_node(dev);
503 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
507 /* Move to ITS specific data */
508 its = (struct acpi_iort_its_group *)node->node_data;
509 if (idx > its->its_count) {
510 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
511 idx, its->its_count);
515 *its_id = its->identifiers[idx];
520 * iort_get_device_domain() - Find MSI domain related to a device
522 * @req_id: Requester ID for the device.
524 * Returns: the MSI domain for this device, NULL otherwise
526 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
528 struct fwnode_handle *handle;
531 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
534 handle = iort_find_domain_token(its_id);
538 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
542 * iort_get_platform_device_domain() - Find MSI domain related to a
544 * @dev: the dev pointer associated with the platform device
546 * Returns: the MSI domain for this device, NULL otherwise
548 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
550 struct acpi_iort_node *node, *msi_parent;
551 struct fwnode_handle *iort_fwnode;
552 struct acpi_iort_its_group *its;
555 /* find its associated iort node */
556 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
557 iort_match_node_callback, dev);
561 /* then find its msi parent node */
562 for (i = 0; i < node->mapping_count; i++) {
563 msi_parent = iort_node_map_platform_id(node, NULL,
572 /* Move to ITS specific data */
573 its = (struct acpi_iort_its_group *)msi_parent->node_data;
575 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
579 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
582 void acpi_configure_pmsi_domain(struct device *dev)
584 struct irq_domain *msi_domain;
586 msi_domain = iort_get_platform_device_domain(dev);
588 dev_set_msi_domain(dev, msi_domain);
591 static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
599 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
600 struct fwnode_handle *fwnode,
601 const struct iommu_ops *ops)
603 int ret = iommu_fwspec_init(dev, fwnode, ops);
606 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
611 static inline bool iort_iommu_driver_enabled(u8 type)
614 case ACPI_IORT_NODE_SMMU_V3:
615 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
616 case ACPI_IORT_NODE_SMMU:
617 return IS_BUILTIN(CONFIG_ARM_SMMU);
619 pr_warn("IORT node type %u does not describe an SMMU\n", type);
624 #ifdef CONFIG_IOMMU_API
626 const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec)
628 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
632 int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev)
636 if (!IS_ERR_OR_NULL(ops) && ops->add_device && dev->bus &&
638 err = ops->add_device(dev);
644 const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec)
647 int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev)
651 static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
652 struct acpi_iort_node *node,
655 const struct iommu_ops *ops = NULL;
657 struct fwnode_handle *iort_fwnode;
660 iort_fwnode = iort_get_fwnode(node);
664 ops = iommu_ops_from_fwnode(iort_fwnode);
666 * If the ops look-up fails, this means that either
667 * the SMMU drivers have not been probed yet or that
668 * the SMMU drivers are not built in the kernel;
669 * Depending on whether the SMMU drivers are built-in
670 * in the kernel or not, defer the IOMMU configuration
674 return iort_iommu_driver_enabled(node->type) ?
675 ERR_PTR(-EPROBE_DEFER) : NULL;
677 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
680 return ret ? NULL : ops;
684 * iort_set_dma_mask - Set-up dma mask for a device.
686 * @dev: device to configure
688 void iort_set_dma_mask(struct device *dev)
691 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
692 * setup the correct supported mask.
694 if (!dev->coherent_dma_mask)
695 dev->coherent_dma_mask = DMA_BIT_MASK(32);
698 * Set it to coherent_dma_mask by default if the architecture
699 * code has not set it.
702 dev->dma_mask = &dev->coherent_dma_mask;
706 * iort_iommu_configure - Set-up IOMMU configuration for a device.
708 * @dev: device to configure
710 * Returns: iommu_ops pointer on configuration success
711 * NULL on configuration failure
713 const struct iommu_ops *iort_iommu_configure(struct device *dev)
715 struct acpi_iort_node *node, *parent;
716 const struct iommu_ops *ops = NULL;
721 * If we already translated the fwspec there
722 * is nothing left to do, return the iommu_ops.
724 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
728 if (dev_is_pci(dev)) {
729 struct pci_bus *bus = to_pci_dev(dev)->bus;
732 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
735 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
736 iort_match_node_callback, &bus->dev);
740 parent = iort_node_map_id(node, rid, &streamid,
743 ops = iort_iommu_xlate(dev, parent, streamid);
748 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
749 iort_match_node_callback, dev);
753 parent = iort_node_map_platform_id(node, &streamid,
754 IORT_IOMMU_TYPE, i++);
757 ops = iort_iommu_xlate(dev, parent, streamid);
758 if (IS_ERR_OR_NULL(ops))
761 parent = iort_node_map_platform_id(node, &streamid,
768 * If we have reason to believe the IOMMU driver missed the initial
769 * add_device callback for dev, replay it to get things in order.
771 err = iort_add_device_replay(ops, dev);
775 /* Ignore all other errors apart from EPROBE_DEFER */
776 if (IS_ERR(ops) && (PTR_ERR(ops) != -EPROBE_DEFER)) {
777 dev_dbg(dev, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops));
784 static void __init acpi_iort_register_irq(int hwirq, const char *name,
786 struct resource *res)
788 int irq = acpi_register_gsi(NULL, hwirq, trigger,
792 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
799 res->flags = IORESOURCE_IRQ;
803 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
805 struct acpi_iort_smmu_v3 *smmu;
806 /* Always present mem resource */
809 /* Retrieve SMMUv3 specific data */
810 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
812 if (smmu->event_gsiv)
827 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
830 * Cavium ThunderX2 implementation doesn't not support unique
831 * irq line. Use single irq line for all the SMMUv3 interrupts.
833 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
837 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
840 return smmu->event_gsiv == smmu->pri_gsiv &&
841 smmu->event_gsiv == smmu->gerr_gsiv &&
842 smmu->event_gsiv == smmu->sync_gsiv;
845 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
848 * Override the size, for Cavium ThunderX2 implementation
849 * which doesn't support the page 1 SMMU register space.
851 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
857 static void __init arm_smmu_v3_init_resources(struct resource *res,
858 struct acpi_iort_node *node)
860 struct acpi_iort_smmu_v3 *smmu;
863 /* Retrieve SMMUv3 specific data */
864 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
866 res[num_res].start = smmu->base_address;
867 res[num_res].end = smmu->base_address +
868 arm_smmu_v3_resource_size(smmu) - 1;
869 res[num_res].flags = IORESOURCE_MEM;
872 if (arm_smmu_v3_is_combined_irq(smmu)) {
873 if (smmu->event_gsiv)
874 acpi_iort_register_irq(smmu->event_gsiv, "combined",
879 if (smmu->event_gsiv)
880 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
885 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
890 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
895 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
901 static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
903 struct acpi_iort_smmu_v3 *smmu;
905 /* Retrieve SMMUv3 specific data */
906 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
908 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
911 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
913 struct acpi_iort_smmu *smmu;
915 /* Retrieve SMMU specific data */
916 smmu = (struct acpi_iort_smmu *)node->node_data;
919 * Only consider the global fault interrupt and ignore the
920 * configuration access interrupt.
922 * MMIO address and global fault interrupt resources are always
923 * present so add them to the context interrupt count as a static
926 return smmu->context_interrupt_count + 2;
929 static void __init arm_smmu_init_resources(struct resource *res,
930 struct acpi_iort_node *node)
932 struct acpi_iort_smmu *smmu;
933 int i, hw_irq, trigger, num_res = 0;
934 u64 *ctx_irq, *glb_irq;
936 /* Retrieve SMMU specific data */
937 smmu = (struct acpi_iort_smmu *)node->node_data;
939 res[num_res].start = smmu->base_address;
940 res[num_res].end = smmu->base_address + smmu->span - 1;
941 res[num_res].flags = IORESOURCE_MEM;
944 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
946 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
947 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
949 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
953 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
954 for (i = 0; i < smmu->context_interrupt_count; i++) {
955 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
956 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
958 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
963 static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
965 struct acpi_iort_smmu *smmu;
967 /* Retrieve SMMU specific data */
968 smmu = (struct acpi_iort_smmu *)node->node_data;
970 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
973 struct iort_iommu_config {
975 int (*iommu_init)(struct acpi_iort_node *node);
976 bool (*iommu_is_coherent)(struct acpi_iort_node *node);
977 int (*iommu_count_resources)(struct acpi_iort_node *node);
978 void (*iommu_init_resources)(struct resource *res,
979 struct acpi_iort_node *node);
982 static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
983 .name = "arm-smmu-v3",
984 .iommu_is_coherent = arm_smmu_v3_is_coherent,
985 .iommu_count_resources = arm_smmu_v3_count_resources,
986 .iommu_init_resources = arm_smmu_v3_init_resources
989 static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
991 .iommu_is_coherent = arm_smmu_is_coherent,
992 .iommu_count_resources = arm_smmu_count_resources,
993 .iommu_init_resources = arm_smmu_init_resources
997 const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
999 switch (node->type) {
1000 case ACPI_IORT_NODE_SMMU_V3:
1001 return &iort_arm_smmu_v3_cfg;
1002 case ACPI_IORT_NODE_SMMU:
1003 return &iort_arm_smmu_cfg;
1010 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
1011 * @node: Pointer to SMMU ACPI IORT node
1013 * Returns: 0 on success, <0 failure
1015 static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
1017 struct fwnode_handle *fwnode;
1018 struct platform_device *pdev;
1020 enum dev_dma_attr attr;
1022 const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
1027 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1031 count = ops->iommu_count_resources(node);
1033 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1039 ops->iommu_init_resources(r, node);
1041 ret = platform_device_add_resources(pdev, r, count);
1043 * Resources are duplicated in platform_device_add_resources,
1044 * free their allocated memory
1052 * Add a copy of IORT node pointer to platform_data to
1053 * be used to retrieve IORT data information.
1055 ret = platform_device_add_data(pdev, &node, sizeof(node));
1060 * We expect the dma masks to be equivalent for
1063 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1065 fwnode = iort_get_fwnode(node);
1072 pdev->dev.fwnode = fwnode;
1074 attr = ops->iommu_is_coherent(node) ?
1075 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1077 /* Configure DMA for the page table walker */
1078 acpi_dma_configure(&pdev->dev, attr);
1080 ret = platform_device_add(pdev);
1082 goto dma_deconfigure;
1087 acpi_dma_deconfigure(&pdev->dev);
1089 platform_device_put(pdev);
1094 static void __init iort_init_platform_devices(void)
1096 struct acpi_iort_node *iort_node, *iort_end;
1097 struct acpi_table_iort *iort;
1098 struct fwnode_handle *fwnode;
1102 * iort_table and iort both point to the start of IORT table, but
1103 * have different struct types
1105 iort = (struct acpi_table_iort *)iort_table;
1107 /* Get the first IORT node */
1108 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1110 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1111 iort_table->length);
1113 for (i = 0; i < iort->node_count; i++) {
1114 if (iort_node >= iort_end) {
1115 pr_err("iort node pointer overflows, bad table\n");
1119 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
1120 (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
1122 fwnode = acpi_alloc_fwnode_static();
1126 iort_set_fwnode(iort_node, fwnode);
1128 ret = iort_add_smmu_platform_device(iort_node);
1130 iort_delete_fwnode(iort_node);
1131 acpi_free_fwnode_static(fwnode);
1136 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1141 void __init acpi_iort_init(void)
1145 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1146 if (ACPI_FAILURE(status)) {
1147 if (status != AE_NOT_FOUND) {
1148 const char *msg = acpi_format_exception(status);
1150 pr_err("Failed to get table, %s\n", msg);
1156 iort_init_platform_devices();