1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016, Semihalf
6 * This file implements early detection/parsing of I/O mapping
7 * reported to OS through firmware via I/O Remapping Table (IORT)
8 * IORT document number: ARM DEN 0049A
11 #define pr_fmt(fmt) "ACPI: IORT: " fmt
13 #include <linux/acpi_iort.h>
14 #include <linux/bitfield.h>
15 #include <linux/iommu.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/pci.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
22 #define IORT_TYPE_MASK(type) (1 << (type))
23 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
24 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
25 (1 << ACPI_IORT_NODE_SMMU_V3))
27 struct iort_its_msi_chip {
28 struct list_head list;
29 struct fwnode_handle *fw_node;
30 phys_addr_t base_addr;
35 struct list_head list;
36 struct acpi_iort_node *iort_node;
37 struct fwnode_handle *fwnode;
39 static LIST_HEAD(iort_fwnode_list);
40 static DEFINE_SPINLOCK(iort_fwnode_lock);
43 * iort_set_fwnode() - Create iort_fwnode and use it to register
44 * iommu data in the iort_fwnode_list
46 * @node: IORT table node associated with the IOMMU
47 * @fwnode: fwnode associated with the IORT node
49 * Returns: 0 on success
52 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
53 struct fwnode_handle *fwnode)
55 struct iort_fwnode *np;
57 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
62 INIT_LIST_HEAD(&np->list);
63 np->iort_node = iort_node;
66 spin_lock(&iort_fwnode_lock);
67 list_add_tail(&np->list, &iort_fwnode_list);
68 spin_unlock(&iort_fwnode_lock);
74 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
76 * @node: IORT table node to be looked-up
78 * Returns: fwnode_handle pointer on success, NULL on failure
80 static inline struct fwnode_handle *iort_get_fwnode(
81 struct acpi_iort_node *node)
83 struct iort_fwnode *curr;
84 struct fwnode_handle *fwnode = NULL;
86 spin_lock(&iort_fwnode_lock);
87 list_for_each_entry(curr, &iort_fwnode_list, list) {
88 if (curr->iort_node == node) {
89 fwnode = curr->fwnode;
93 spin_unlock(&iort_fwnode_lock);
99 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
101 * @node: IORT table node associated with fwnode to delete
103 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
105 struct iort_fwnode *curr, *tmp;
107 spin_lock(&iort_fwnode_lock);
108 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
109 if (curr->iort_node == node) {
110 list_del(&curr->list);
115 spin_unlock(&iort_fwnode_lock);
119 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
121 * @fwnode: fwnode associated with device to be looked-up
123 * Returns: iort_node pointer on success, NULL on failure
125 static inline struct acpi_iort_node *iort_get_iort_node(
126 struct fwnode_handle *fwnode)
128 struct iort_fwnode *curr;
129 struct acpi_iort_node *iort_node = NULL;
131 spin_lock(&iort_fwnode_lock);
132 list_for_each_entry(curr, &iort_fwnode_list, list) {
133 if (curr->fwnode == fwnode) {
134 iort_node = curr->iort_node;
138 spin_unlock(&iort_fwnode_lock);
143 typedef acpi_status (*iort_find_node_callback)
144 (struct acpi_iort_node *node, void *context);
146 /* Root pointer to the mapped IORT table */
147 static struct acpi_table_header *iort_table;
149 static LIST_HEAD(iort_msi_chip_list);
150 static DEFINE_SPINLOCK(iort_msi_chip_lock);
153 * iort_register_domain_token() - register domain token along with related
154 * ITS ID and base address to the list from where we can get it back later on.
156 * @base: ITS base address.
157 * @fw_node: Domain token.
159 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
161 int iort_register_domain_token(int trans_id, phys_addr_t base,
162 struct fwnode_handle *fw_node)
164 struct iort_its_msi_chip *its_msi_chip;
166 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
170 its_msi_chip->fw_node = fw_node;
171 its_msi_chip->translation_id = trans_id;
172 its_msi_chip->base_addr = base;
174 spin_lock(&iort_msi_chip_lock);
175 list_add(&its_msi_chip->list, &iort_msi_chip_list);
176 spin_unlock(&iort_msi_chip_lock);
182 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
187 void iort_deregister_domain_token(int trans_id)
189 struct iort_its_msi_chip *its_msi_chip, *t;
191 spin_lock(&iort_msi_chip_lock);
192 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
193 if (its_msi_chip->translation_id == trans_id) {
194 list_del(&its_msi_chip->list);
199 spin_unlock(&iort_msi_chip_lock);
203 * iort_find_domain_token() - Find domain token based on given ITS ID
206 * Returns: domain token when find on the list, NULL otherwise
208 struct fwnode_handle *iort_find_domain_token(int trans_id)
210 struct fwnode_handle *fw_node = NULL;
211 struct iort_its_msi_chip *its_msi_chip;
213 spin_lock(&iort_msi_chip_lock);
214 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
215 if (its_msi_chip->translation_id == trans_id) {
216 fw_node = its_msi_chip->fw_node;
220 spin_unlock(&iort_msi_chip_lock);
225 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
226 iort_find_node_callback callback,
229 struct acpi_iort_node *iort_node, *iort_end;
230 struct acpi_table_iort *iort;
236 /* Get the first IORT node */
237 iort = (struct acpi_table_iort *)iort_table;
238 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
240 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
243 for (i = 0; i < iort->node_count; i++) {
244 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
245 "IORT node pointer overflows, bad table!\n"))
248 if (iort_node->type == type &&
249 ACPI_SUCCESS(callback(iort_node, context)))
252 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
259 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
262 struct device *dev = context;
263 acpi_status status = AE_NOT_FOUND;
265 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
266 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
267 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
268 struct acpi_iort_named_component *ncomp;
273 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
274 if (ACPI_FAILURE(status)) {
275 dev_warn(dev, "Can't get device full path name\n");
279 ncomp = (struct acpi_iort_named_component *)node->node_data;
280 status = !strcmp(ncomp->device_name, buf.pointer) ?
281 AE_OK : AE_NOT_FOUND;
282 acpi_os_free(buf.pointer);
283 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
284 struct acpi_iort_root_complex *pci_rc;
287 bus = to_pci_bus(dev);
288 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
291 * It is assumed that PCI segment numbers maps one-to-one
292 * with root complexes. Each segment number can represent only
295 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
296 AE_OK : AE_NOT_FOUND;
302 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
303 u32 *rid_out, bool check_overlap)
305 /* Single mapping does not care for input id */
306 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
307 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
308 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
309 *rid_out = map->output_base;
313 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
318 if (rid_in < map->input_base ||
319 (rid_in > map->input_base + map->id_count))
324 * We already found a mapping for this input ID at the end of
325 * another region. If it coincides with the start of this
326 * region, we assume the prior match was due to the off-by-1
327 * issue mentioned below, and allow it to be superseded.
328 * Otherwise, things are *really* broken, and we just disregard
329 * duplicate matches entirely to retain compatibility.
331 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
333 if (rid_in != map->input_base)
336 pr_err(FW_BUG "applying workaround.\n");
339 *rid_out = map->output_base + (rid_in - map->input_base);
342 * Due to confusion regarding the meaning of the id_count field (which
343 * carries the number of IDs *minus 1*), we may have to disregard this
344 * match if it is at the end of the range, and overlaps with the start
347 if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
352 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
353 u32 *id_out, int index)
355 struct acpi_iort_node *parent;
356 struct acpi_iort_id_mapping *map;
358 if (!node->mapping_offset || !node->mapping_count ||
359 index >= node->mapping_count)
362 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
363 node->mapping_offset + index * sizeof(*map));
366 if (!map->output_reference) {
367 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
372 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
373 map->output_reference);
375 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
376 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
377 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
378 node->type == ACPI_IORT_NODE_SMMU_V3 ||
379 node->type == ACPI_IORT_NODE_PMCG) {
380 *id_out = map->output_base;
388 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
390 struct acpi_iort_smmu_v3 *smmu;
391 struct acpi_iort_pmcg *pmcg;
393 switch (node->type) {
394 case ACPI_IORT_NODE_SMMU_V3:
396 * SMMUv3 dev ID mapping index was introduced in revision 1
397 * table, not available in revision 0
399 if (node->revision < 1)
402 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
404 * ID mapping index is only ignored if all interrupts are
407 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
411 if (smmu->id_mapping_index >= node->mapping_count) {
412 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
417 return smmu->id_mapping_index;
418 case ACPI_IORT_NODE_PMCG:
419 pmcg = (struct acpi_iort_pmcg *)node->node_data;
420 if (pmcg->overflow_gsiv || node->mapping_count == 0)
429 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
430 u32 id_in, u32 *id_out,
435 /* Parse the ID mapping tree to find specified node type */
437 struct acpi_iort_id_mapping *map;
438 int i, index, rc = 0;
439 u32 out_ref = 0, map_id = id;
441 if (IORT_TYPE_MASK(node->type) & type_mask) {
447 if (!node->mapping_offset || !node->mapping_count)
450 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
451 node->mapping_offset);
454 if (!map->output_reference) {
455 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
461 * Get the special ID mapping index (if any) and skip its
462 * associated ID map to prevent erroneous multi-stage
463 * IORT ID translations.
465 index = iort_get_id_mapping_index(node);
467 /* Do the ID translation */
468 for (i = 0; i < node->mapping_count; i++, map++) {
469 /* if it is special mapping index, skip it */
473 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
477 out_ref = map->output_reference;
480 if (i == node->mapping_count && !out_ref)
483 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
484 rc ? out_ref : map->output_reference);
488 /* Map input ID to output ID unchanged on mapping failure */
495 static struct acpi_iort_node *iort_node_map_platform_id(
496 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
499 struct acpi_iort_node *parent;
502 /* step 1: retrieve the initial dev id */
503 parent = iort_node_get_id(node, &id, index);
508 * optional step 2: map the initial dev id if its parent is not
509 * the target type we want, map it again for the use cases such
510 * as NC (named component) -> SMMU -> ITS. If the type is matched,
511 * return the initial dev id and its parent pointer directly.
513 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
514 parent = iort_node_map_id(parent, id, id_out, type_mask);
522 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
524 struct pci_bus *pbus;
526 if (!dev_is_pci(dev)) {
527 struct acpi_iort_node *node;
529 * scan iort_fwnode_list to see if it's an iort platform
530 * device (such as SMMU, PMCG),its iort node already cached
531 * and associated with fwnode when iort platform devices
534 node = iort_get_iort_node(dev->fwnode);
539 * if not, then it should be a platform device defined in
540 * DSDT/SSDT (with Named Component node in IORT)
542 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
543 iort_match_node_callback, dev);
546 /* Find a PCI root bus */
547 pbus = to_pci_dev(dev)->bus;
548 while (!pci_is_root_bus(pbus))
551 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
552 iort_match_node_callback, &pbus->dev);
556 * iort_msi_map_rid() - Map a MSI requester ID for a device
557 * @dev: The device for which the mapping is to be done.
558 * @req_id: The device requester ID.
560 * Returns: mapped MSI RID on success, input requester ID otherwise
562 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
564 struct acpi_iort_node *node;
567 node = iort_find_dev_node(dev);
571 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
576 * iort_pmsi_get_dev_id() - Get the device id for a device
577 * @dev: The device for which the mapping is to be done.
578 * @dev_id: The device ID found.
580 * Returns: 0 for successful find a dev id, -ENODEV on error
582 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
585 struct acpi_iort_node *node;
587 node = iort_find_dev_node(dev);
591 index = iort_get_id_mapping_index(node);
592 /* if there is a valid index, go get the dev_id directly */
594 if (iort_node_get_id(node, dev_id, index))
597 for (i = 0; i < node->mapping_count; i++) {
598 if (iort_node_map_platform_id(node, dev_id,
607 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
609 struct iort_its_msi_chip *its_msi_chip;
612 spin_lock(&iort_msi_chip_lock);
613 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
614 if (its_msi_chip->translation_id == its_id) {
615 *base = its_msi_chip->base_addr;
620 spin_unlock(&iort_msi_chip_lock);
626 * iort_dev_find_its_id() - Find the ITS identifier for a device
628 * @req_id: Device's requester ID
629 * @idx: Index of the ITS identifier list.
630 * @its_id: ITS identifier.
632 * Returns: 0 on success, appropriate error value otherwise
634 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
635 unsigned int idx, int *its_id)
637 struct acpi_iort_its_group *its;
638 struct acpi_iort_node *node;
640 node = iort_find_dev_node(dev);
644 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
648 /* Move to ITS specific data */
649 its = (struct acpi_iort_its_group *)node->node_data;
650 if (idx >= its->its_count) {
651 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
652 idx, its->its_count);
656 *its_id = its->identifiers[idx];
661 * iort_get_device_domain() - Find MSI domain related to a device
663 * @req_id: Requester ID for the device.
665 * Returns: the MSI domain for this device, NULL otherwise
667 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
669 struct fwnode_handle *handle;
672 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
675 handle = iort_find_domain_token(its_id);
679 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
682 static void iort_set_device_domain(struct device *dev,
683 struct acpi_iort_node *node)
685 struct acpi_iort_its_group *its;
686 struct acpi_iort_node *msi_parent;
687 struct acpi_iort_id_mapping *map;
688 struct fwnode_handle *iort_fwnode;
689 struct irq_domain *domain;
692 index = iort_get_id_mapping_index(node);
696 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
697 node->mapping_offset + index * sizeof(*map));
700 if (!map->output_reference ||
701 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
702 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
707 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
708 map->output_reference);
710 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
713 /* Move to ITS specific data */
714 its = (struct acpi_iort_its_group *)msi_parent->node_data;
716 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
720 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
722 dev_set_msi_domain(dev, domain);
726 * iort_get_platform_device_domain() - Find MSI domain related to a
728 * @dev: the dev pointer associated with the platform device
730 * Returns: the MSI domain for this device, NULL otherwise
732 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
734 struct acpi_iort_node *node, *msi_parent = NULL;
735 struct fwnode_handle *iort_fwnode;
736 struct acpi_iort_its_group *its;
739 /* find its associated iort node */
740 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
741 iort_match_node_callback, dev);
745 /* then find its msi parent node */
746 for (i = 0; i < node->mapping_count; i++) {
747 msi_parent = iort_node_map_platform_id(node, NULL,
756 /* Move to ITS specific data */
757 its = (struct acpi_iort_its_group *)msi_parent->node_data;
759 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
763 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
766 void acpi_configure_pmsi_domain(struct device *dev)
768 struct irq_domain *msi_domain;
770 msi_domain = iort_get_platform_device_domain(dev);
772 dev_set_msi_domain(dev, msi_domain);
775 #ifdef CONFIG_IOMMU_API
776 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
778 struct acpi_iort_node *iommu;
779 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
781 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
783 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
784 struct acpi_iort_smmu_v3 *smmu;
786 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
787 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
794 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
796 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
798 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
801 static inline int iort_add_device_replay(const struct iommu_ops *ops,
806 if (dev->bus && !device_iommu_mapped(dev))
807 err = iommu_probe_device(dev);
813 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
814 * @dev: Device from iommu_get_resv_regions()
815 * @head: Reserved region list from iommu_get_resv_regions()
817 * Returns: Number of msi reserved regions on success (0 if platform
818 * doesn't require the reservation or no associated msi regions),
819 * appropriate error value otherwise. The ITS interrupt translation
820 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
821 * are the msi reserved regions.
823 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
825 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
826 struct acpi_iort_its_group *its;
827 struct acpi_iort_node *iommu_node, *its_node = NULL;
830 iommu_node = iort_get_msi_resv_iommu(dev);
835 * Current logic to reserve ITS regions relies on HW topologies
836 * where a given PCI or named component maps its IDs to only one
837 * ITS group; if a PCI or named component can map its IDs to
838 * different ITS groups through IORT mappings this function has
839 * to be reworked to ensure we reserve regions for all ITS groups
840 * a given PCI or named component may map IDs to.
843 for (i = 0; i < fwspec->num_ids; i++) {
844 its_node = iort_node_map_id(iommu_node,
846 NULL, IORT_MSI_TYPE);
854 /* Move to ITS specific data */
855 its = (struct acpi_iort_its_group *)its_node->node_data;
857 for (i = 0; i < its->its_count; i++) {
860 if (!iort_find_its_base(its->identifiers[i], &base)) {
861 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
862 struct iommu_resv_region *region;
864 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
865 prot, IOMMU_RESV_MSI);
867 list_add_tail(®ion->list, head);
873 return (resv == its->its_count) ? resv : -ENODEV;
876 static inline bool iort_iommu_driver_enabled(u8 type)
879 case ACPI_IORT_NODE_SMMU_V3:
880 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
881 case ACPI_IORT_NODE_SMMU:
882 return IS_ENABLED(CONFIG_ARM_SMMU);
884 pr_warn("IORT node type %u does not describe an SMMU\n", type);
889 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
890 struct fwnode_handle *fwnode,
891 const struct iommu_ops *ops)
893 int ret = iommu_fwspec_init(dev, fwnode, ops);
896 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
901 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
903 struct acpi_iort_root_complex *pci_rc;
905 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
906 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
909 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
912 const struct iommu_ops *ops;
913 struct fwnode_handle *iort_fwnode;
918 iort_fwnode = iort_get_fwnode(node);
923 * If the ops look-up fails, this means that either
924 * the SMMU drivers have not been probed yet or that
925 * the SMMU drivers are not built in the kernel;
926 * Depending on whether the SMMU drivers are built-in
927 * in the kernel or not, defer the IOMMU configuration
930 ops = iommu_ops_from_fwnode(iort_fwnode);
932 return iort_iommu_driver_enabled(node->type) ?
933 -EPROBE_DEFER : -ENODEV;
935 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
938 struct iort_pci_alias_info {
940 struct acpi_iort_node *node;
943 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
945 struct iort_pci_alias_info *info = data;
946 struct acpi_iort_node *parent;
949 parent = iort_node_map_id(info->node, alias, &streamid,
951 return iort_iommu_xlate(info->dev, parent, streamid);
954 static void iort_named_component_init(struct device *dev,
955 struct acpi_iort_node *node)
957 struct acpi_iort_named_component *nc;
958 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
963 nc = (struct acpi_iort_named_component *)node->node_data;
964 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
969 * iort_iommu_configure - Set-up IOMMU configuration for a device.
971 * @dev: device to configure
973 * Returns: iommu_ops pointer on configuration success
974 * NULL on configuration failure
976 const struct iommu_ops *iort_iommu_configure(struct device *dev)
978 struct acpi_iort_node *node, *parent;
979 const struct iommu_ops *ops;
984 * If we already translated the fwspec there
985 * is nothing left to do, return the iommu_ops.
987 ops = iort_fwspec_iommu_ops(dev);
991 if (dev_is_pci(dev)) {
992 struct iommu_fwspec *fwspec;
993 struct pci_bus *bus = to_pci_dev(dev)->bus;
994 struct iort_pci_alias_info info = { .dev = dev };
996 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
997 iort_match_node_callback, &bus->dev);
1002 err = pci_for_each_dma_alias(to_pci_dev(dev),
1003 iort_pci_iommu_init, &info);
1005 fwspec = dev_iommu_fwspec_get(dev);
1006 if (fwspec && iort_pci_rc_supports_ats(node))
1007 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1011 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1012 iort_match_node_callback, dev);
1017 parent = iort_node_map_platform_id(node, &streamid,
1022 err = iort_iommu_xlate(dev, parent, streamid);
1023 } while (parent && !err);
1026 iort_named_component_init(dev, node);
1030 * If we have reason to believe the IOMMU driver missed the initial
1031 * add_device callback for dev, replay it to get things in order.
1034 ops = iort_fwspec_iommu_ops(dev);
1035 err = iort_add_device_replay(ops, dev);
1038 /* Ignore all other errors apart from EPROBE_DEFER */
1039 if (err == -EPROBE_DEFER) {
1042 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1049 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1051 static inline int iort_add_device_replay(const struct iommu_ops *ops,
1054 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1056 const struct iommu_ops *iort_iommu_configure(struct device *dev)
1060 static int nc_dma_get_range(struct device *dev, u64 *size)
1062 struct acpi_iort_node *node;
1063 struct acpi_iort_named_component *ncomp;
1065 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1066 iort_match_node_callback, dev);
1070 ncomp = (struct acpi_iort_named_component *)node->node_data;
1072 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1073 1ULL<<ncomp->memory_address_limit;
1078 static int rc_dma_get_range(struct device *dev, u64 *size)
1080 struct acpi_iort_node *node;
1081 struct acpi_iort_root_complex *rc;
1082 struct pci_bus *pbus = to_pci_dev(dev)->bus;
1084 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1085 iort_match_node_callback, &pbus->dev);
1086 if (!node || node->revision < 1)
1089 rc = (struct acpi_iort_root_complex *)node->node_data;
1091 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1092 1ULL<<rc->memory_address_limit;
1098 * iort_dma_setup() - Set-up device DMA parameters.
1100 * @dev: device to configure
1101 * @dma_addr: device DMA address result pointer
1102 * @size: DMA range size result pointer
1104 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
1106 u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1110 * If @dev is expected to be DMA-capable then the bus code that created
1111 * it should have initialised its dma_mask pointer by this point. For
1112 * now, we'll continue the legacy behaviour of coercing it to the
1113 * coherent mask if not, but we'll no longer do so quietly.
1115 if (!dev->dma_mask) {
1116 dev_warn(dev, "DMA mask not set\n");
1117 dev->dma_mask = &dev->coherent_dma_mask;
1120 if (dev->coherent_dma_mask)
1121 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1125 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1127 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1128 : nc_dma_get_range(dev, &size);
1132 * Limit coherent and dma mask based on size retrieved from
1135 end = dmaaddr + size - 1;
1136 mask = DMA_BIT_MASK(ilog2(end) + 1);
1137 dev->bus_dma_limit = end;
1138 dev->coherent_dma_mask = mask;
1139 *dev->dma_mask = mask;
1142 *dma_addr = dmaaddr;
1145 dev->dma_pfn_offset = PFN_DOWN(offset);
1146 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1149 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1151 struct resource *res)
1153 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1157 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1164 res->flags = IORESOURCE_IRQ;
1168 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1170 struct acpi_iort_smmu_v3 *smmu;
1171 /* Always present mem resource */
1174 /* Retrieve SMMUv3 specific data */
1175 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1177 if (smmu->event_gsiv)
1183 if (smmu->gerr_gsiv)
1186 if (smmu->sync_gsiv)
1192 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1195 * Cavium ThunderX2 implementation doesn't not support unique
1196 * irq line. Use single irq line for all the SMMUv3 interrupts.
1198 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1202 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1205 return smmu->event_gsiv == smmu->pri_gsiv &&
1206 smmu->event_gsiv == smmu->gerr_gsiv &&
1207 smmu->event_gsiv == smmu->sync_gsiv;
1210 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1213 * Override the size, for Cavium ThunderX2 implementation
1214 * which doesn't support the page 1 SMMU register space.
1216 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1222 static void __init arm_smmu_v3_init_resources(struct resource *res,
1223 struct acpi_iort_node *node)
1225 struct acpi_iort_smmu_v3 *smmu;
1228 /* Retrieve SMMUv3 specific data */
1229 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1231 res[num_res].start = smmu->base_address;
1232 res[num_res].end = smmu->base_address +
1233 arm_smmu_v3_resource_size(smmu) - 1;
1234 res[num_res].flags = IORESOURCE_MEM;
1237 if (arm_smmu_v3_is_combined_irq(smmu)) {
1238 if (smmu->event_gsiv)
1239 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1240 ACPI_EDGE_SENSITIVE,
1244 if (smmu->event_gsiv)
1245 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1246 ACPI_EDGE_SENSITIVE,
1250 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1251 ACPI_EDGE_SENSITIVE,
1254 if (smmu->gerr_gsiv)
1255 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1256 ACPI_EDGE_SENSITIVE,
1259 if (smmu->sync_gsiv)
1260 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1261 ACPI_EDGE_SENSITIVE,
1266 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1267 struct acpi_iort_node *node)
1269 struct acpi_iort_smmu_v3 *smmu;
1270 enum dev_dma_attr attr;
1272 /* Retrieve SMMUv3 specific data */
1273 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1275 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1276 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1278 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1279 dev->dma_mask = &dev->coherent_dma_mask;
1281 /* Configure DMA for the page table walker */
1282 acpi_dma_configure(dev, attr);
1285 #if defined(CONFIG_ACPI_NUMA)
1287 * set numa proximity domain for smmuv3 device
1289 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1290 struct acpi_iort_node *node)
1292 struct acpi_iort_smmu_v3 *smmu;
1294 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1295 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1296 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
1298 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1301 set_dev_node(dev, dev_node);
1302 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1309 #define arm_smmu_v3_set_proximity NULL
1312 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1314 struct acpi_iort_smmu *smmu;
1316 /* Retrieve SMMU specific data */
1317 smmu = (struct acpi_iort_smmu *)node->node_data;
1320 * Only consider the global fault interrupt and ignore the
1321 * configuration access interrupt.
1323 * MMIO address and global fault interrupt resources are always
1324 * present so add them to the context interrupt count as a static
1327 return smmu->context_interrupt_count + 2;
1330 static void __init arm_smmu_init_resources(struct resource *res,
1331 struct acpi_iort_node *node)
1333 struct acpi_iort_smmu *smmu;
1334 int i, hw_irq, trigger, num_res = 0;
1335 u64 *ctx_irq, *glb_irq;
1337 /* Retrieve SMMU specific data */
1338 smmu = (struct acpi_iort_smmu *)node->node_data;
1340 res[num_res].start = smmu->base_address;
1341 res[num_res].end = smmu->base_address + smmu->span - 1;
1342 res[num_res].flags = IORESOURCE_MEM;
1345 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1347 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1348 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1350 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1354 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1355 for (i = 0; i < smmu->context_interrupt_count; i++) {
1356 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1357 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1359 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1364 static void __init arm_smmu_dma_configure(struct device *dev,
1365 struct acpi_iort_node *node)
1367 struct acpi_iort_smmu *smmu;
1368 enum dev_dma_attr attr;
1370 /* Retrieve SMMU specific data */
1371 smmu = (struct acpi_iort_smmu *)node->node_data;
1373 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1374 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1376 /* We expect the dma masks to be equivalent for SMMU set-ups */
1377 dev->dma_mask = &dev->coherent_dma_mask;
1379 /* Configure DMA for the page table walker */
1380 acpi_dma_configure(dev, attr);
1383 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1385 struct acpi_iort_pmcg *pmcg;
1387 /* Retrieve PMCG specific data */
1388 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1391 * There are always 2 memory resources.
1392 * If the overflow_gsiv is present then add that for a total of 3.
1394 return pmcg->overflow_gsiv ? 3 : 2;
1397 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1398 struct acpi_iort_node *node)
1400 struct acpi_iort_pmcg *pmcg;
1402 /* Retrieve PMCG specific data */
1403 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1405 res[0].start = pmcg->page0_base_address;
1406 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1407 res[0].flags = IORESOURCE_MEM;
1408 res[1].start = pmcg->page1_base_address;
1409 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1410 res[1].flags = IORESOURCE_MEM;
1412 if (pmcg->overflow_gsiv)
1413 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1414 ACPI_EDGE_SENSITIVE, &res[2]);
1417 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1418 /* HiSilicon Hip08 Platform */
1419 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1420 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1424 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1429 idx = acpi_match_platform_list(pmcg_plat_info);
1431 model = pmcg_plat_info[idx].data;
1433 model = IORT_SMMU_V3_PMCG_GENERIC;
1435 return platform_device_add_data(pdev, &model, sizeof(model));
1438 struct iort_dev_config {
1440 int (*dev_init)(struct acpi_iort_node *node);
1441 void (*dev_dma_configure)(struct device *dev,
1442 struct acpi_iort_node *node);
1443 int (*dev_count_resources)(struct acpi_iort_node *node);
1444 void (*dev_init_resources)(struct resource *res,
1445 struct acpi_iort_node *node);
1446 int (*dev_set_proximity)(struct device *dev,
1447 struct acpi_iort_node *node);
1448 int (*dev_add_platdata)(struct platform_device *pdev);
1451 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1452 .name = "arm-smmu-v3",
1453 .dev_dma_configure = arm_smmu_v3_dma_configure,
1454 .dev_count_resources = arm_smmu_v3_count_resources,
1455 .dev_init_resources = arm_smmu_v3_init_resources,
1456 .dev_set_proximity = arm_smmu_v3_set_proximity,
1459 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1461 .dev_dma_configure = arm_smmu_dma_configure,
1462 .dev_count_resources = arm_smmu_count_resources,
1463 .dev_init_resources = arm_smmu_init_resources,
1466 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1467 .name = "arm-smmu-v3-pmcg",
1468 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1469 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1470 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1473 static __init const struct iort_dev_config *iort_get_dev_cfg(
1474 struct acpi_iort_node *node)
1476 switch (node->type) {
1477 case ACPI_IORT_NODE_SMMU_V3:
1478 return &iort_arm_smmu_v3_cfg;
1479 case ACPI_IORT_NODE_SMMU:
1480 return &iort_arm_smmu_cfg;
1481 case ACPI_IORT_NODE_PMCG:
1482 return &iort_arm_smmu_v3_pmcg_cfg;
1489 * iort_add_platform_device() - Allocate a platform device for IORT node
1490 * @node: Pointer to device ACPI IORT node
1492 * Returns: 0 on success, <0 failure
1494 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1495 const struct iort_dev_config *ops)
1497 struct fwnode_handle *fwnode;
1498 struct platform_device *pdev;
1502 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1506 if (ops->dev_set_proximity) {
1507 ret = ops->dev_set_proximity(&pdev->dev, node);
1512 count = ops->dev_count_resources(node);
1514 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1520 ops->dev_init_resources(r, node);
1522 ret = platform_device_add_resources(pdev, r, count);
1524 * Resources are duplicated in platform_device_add_resources,
1525 * free their allocated memory
1533 * Platform devices based on PMCG nodes uses platform_data to
1534 * pass the hardware model info to the driver. For others, add
1535 * a copy of IORT node pointer to platform_data to be used to
1536 * retrieve IORT data information.
1538 if (ops->dev_add_platdata)
1539 ret = ops->dev_add_platdata(pdev);
1541 ret = platform_device_add_data(pdev, &node, sizeof(node));
1546 fwnode = iort_get_fwnode(node);
1553 pdev->dev.fwnode = fwnode;
1555 if (ops->dev_dma_configure)
1556 ops->dev_dma_configure(&pdev->dev, node);
1558 iort_set_device_domain(&pdev->dev, node);
1560 ret = platform_device_add(pdev);
1562 goto dma_deconfigure;
1567 arch_teardown_dma_ops(&pdev->dev);
1569 platform_device_put(pdev);
1575 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1577 static bool acs_enabled __initdata;
1582 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1583 struct acpi_iort_node *parent;
1584 struct acpi_iort_id_mapping *map;
1587 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1588 iort_node->mapping_offset);
1590 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1591 if (!map->output_reference)
1594 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1595 iort_table, map->output_reference);
1597 * If we detect a RC->SMMU mapping, make sure
1598 * we enable ACS on the system.
1600 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1601 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1610 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1613 static void __init iort_init_platform_devices(void)
1615 struct acpi_iort_node *iort_node, *iort_end;
1616 struct acpi_table_iort *iort;
1617 struct fwnode_handle *fwnode;
1619 const struct iort_dev_config *ops;
1622 * iort_table and iort both point to the start of IORT table, but
1623 * have different struct types
1625 iort = (struct acpi_table_iort *)iort_table;
1627 /* Get the first IORT node */
1628 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1630 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1631 iort_table->length);
1633 for (i = 0; i < iort->node_count; i++) {
1634 if (iort_node >= iort_end) {
1635 pr_err("iort node pointer overflows, bad table\n");
1639 iort_enable_acs(iort_node);
1641 ops = iort_get_dev_cfg(iort_node);
1643 fwnode = acpi_alloc_fwnode_static();
1647 iort_set_fwnode(iort_node, fwnode);
1649 ret = iort_add_platform_device(iort_node, ops);
1651 iort_delete_fwnode(iort_node);
1652 acpi_free_fwnode_static(fwnode);
1657 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1662 void __init acpi_iort_init(void)
1666 /* iort_table will be used at runtime after the iort init,
1667 * so we don't need to call acpi_put_table() to release
1668 * the IORT table mapping.
1670 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1671 if (ACPI_FAILURE(status)) {
1672 if (status != AE_NOT_FOUND) {
1673 const char *msg = acpi_format_exception(status);
1675 pr_err("Failed to get table, %s\n", msg);
1681 iort_init_platform_devices();