1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016, Semihalf
4 * Author: Tomasz Nowicki <tn@semihalf.com>
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;
268 struct acpi_iort_named_component *ncomp;
269 struct device *nc_dev = dev;
272 * Walk the device tree to find a device with an
273 * ACPI companion; there is no point in scanning
274 * IORT for a device matching a named component if
275 * the device does not have an ACPI companion to
279 adev = ACPI_COMPANION(nc_dev);
283 nc_dev = nc_dev->parent;
289 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
290 if (ACPI_FAILURE(status)) {
291 dev_warn(nc_dev, "Can't get device full path name\n");
295 ncomp = (struct acpi_iort_named_component *)node->node_data;
296 status = !strcmp(ncomp->device_name, buf.pointer) ?
297 AE_OK : AE_NOT_FOUND;
298 acpi_os_free(buf.pointer);
299 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
300 struct acpi_iort_root_complex *pci_rc;
303 bus = to_pci_bus(dev);
304 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
307 * It is assumed that PCI segment numbers maps one-to-one
308 * with root complexes. Each segment number can represent only
311 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
312 AE_OK : AE_NOT_FOUND;
318 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
319 u32 *rid_out, bool check_overlap)
321 /* Single mapping does not care for input id */
322 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
323 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
324 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
325 *rid_out = map->output_base;
329 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
334 if (rid_in < map->input_base ||
335 (rid_in > map->input_base + map->id_count))
340 * We already found a mapping for this input ID at the end of
341 * another region. If it coincides with the start of this
342 * region, we assume the prior match was due to the off-by-1
343 * issue mentioned below, and allow it to be superseded.
344 * Otherwise, things are *really* broken, and we just disregard
345 * duplicate matches entirely to retain compatibility.
347 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
349 if (rid_in != map->input_base)
352 pr_err(FW_BUG "applying workaround.\n");
355 *rid_out = map->output_base + (rid_in - map->input_base);
358 * Due to confusion regarding the meaning of the id_count field (which
359 * carries the number of IDs *minus 1*), we may have to disregard this
360 * match if it is at the end of the range, and overlaps with the start
363 if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
368 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
369 u32 *id_out, int index)
371 struct acpi_iort_node *parent;
372 struct acpi_iort_id_mapping *map;
374 if (!node->mapping_offset || !node->mapping_count ||
375 index >= node->mapping_count)
378 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
379 node->mapping_offset + index * sizeof(*map));
382 if (!map->output_reference) {
383 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
388 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
389 map->output_reference);
391 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
392 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
393 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
394 node->type == ACPI_IORT_NODE_SMMU_V3 ||
395 node->type == ACPI_IORT_NODE_PMCG) {
396 *id_out = map->output_base;
404 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
406 struct acpi_iort_smmu_v3 *smmu;
407 struct acpi_iort_pmcg *pmcg;
409 switch (node->type) {
410 case ACPI_IORT_NODE_SMMU_V3:
412 * SMMUv3 dev ID mapping index was introduced in revision 1
413 * table, not available in revision 0
415 if (node->revision < 1)
418 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
420 * ID mapping index is only ignored if all interrupts are
423 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
427 if (smmu->id_mapping_index >= node->mapping_count) {
428 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
433 return smmu->id_mapping_index;
434 case ACPI_IORT_NODE_PMCG:
435 pmcg = (struct acpi_iort_pmcg *)node->node_data;
436 if (pmcg->overflow_gsiv || node->mapping_count == 0)
445 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
446 u32 id_in, u32 *id_out,
451 /* Parse the ID mapping tree to find specified node type */
453 struct acpi_iort_id_mapping *map;
454 int i, index, rc = 0;
455 u32 out_ref = 0, map_id = id;
457 if (IORT_TYPE_MASK(node->type) & type_mask) {
463 if (!node->mapping_offset || !node->mapping_count)
466 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
467 node->mapping_offset);
470 if (!map->output_reference) {
471 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
477 * Get the special ID mapping index (if any) and skip its
478 * associated ID map to prevent erroneous multi-stage
479 * IORT ID translations.
481 index = iort_get_id_mapping_index(node);
483 /* Do the ID translation */
484 for (i = 0; i < node->mapping_count; i++, map++) {
485 /* if it is special mapping index, skip it */
489 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
493 out_ref = map->output_reference;
496 if (i == node->mapping_count && !out_ref)
499 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
500 rc ? out_ref : map->output_reference);
504 /* Map input ID to output ID unchanged on mapping failure */
511 static struct acpi_iort_node *iort_node_map_platform_id(
512 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
515 struct acpi_iort_node *parent;
518 /* step 1: retrieve the initial dev id */
519 parent = iort_node_get_id(node, &id, index);
524 * optional step 2: map the initial dev id if its parent is not
525 * the target type we want, map it again for the use cases such
526 * as NC (named component) -> SMMU -> ITS. If the type is matched,
527 * return the initial dev id and its parent pointer directly.
529 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
530 parent = iort_node_map_id(parent, id, id_out, type_mask);
538 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
540 struct pci_bus *pbus;
542 if (!dev_is_pci(dev)) {
543 struct acpi_iort_node *node;
545 * scan iort_fwnode_list to see if it's an iort platform
546 * device (such as SMMU, PMCG),its iort node already cached
547 * and associated with fwnode when iort platform devices
550 node = iort_get_iort_node(dev->fwnode);
554 * if not, then it should be a platform device defined in
555 * DSDT/SSDT (with Named Component node in IORT)
557 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
558 iort_match_node_callback, dev);
561 pbus = to_pci_dev(dev)->bus;
563 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
564 iort_match_node_callback, &pbus->dev);
568 * iort_msi_map_id() - Map a MSI input ID for a device
569 * @dev: The device for which the mapping is to be done.
570 * @input_id: The device input ID.
572 * Returns: mapped MSI ID on success, input ID otherwise
574 u32 iort_msi_map_id(struct device *dev, u32 input_id)
576 struct acpi_iort_node *node;
579 node = iort_find_dev_node(dev);
583 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
588 * iort_pmsi_get_dev_id() - Get the device id for a device
589 * @dev: The device for which the mapping is to be done.
590 * @dev_id: The device ID found.
592 * Returns: 0 for successful find a dev id, -ENODEV on error
594 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
597 struct acpi_iort_node *node;
599 node = iort_find_dev_node(dev);
603 index = iort_get_id_mapping_index(node);
604 /* if there is a valid index, go get the dev_id directly */
606 if (iort_node_get_id(node, dev_id, index))
609 for (i = 0; i < node->mapping_count; i++) {
610 if (iort_node_map_platform_id(node, dev_id,
619 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
621 struct iort_its_msi_chip *its_msi_chip;
624 spin_lock(&iort_msi_chip_lock);
625 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
626 if (its_msi_chip->translation_id == its_id) {
627 *base = its_msi_chip->base_addr;
632 spin_unlock(&iort_msi_chip_lock);
638 * iort_dev_find_its_id() - Find the ITS identifier for a device
641 * @idx: Index of the ITS identifier list.
642 * @its_id: ITS identifier.
644 * Returns: 0 on success, appropriate error value otherwise
646 static int iort_dev_find_its_id(struct device *dev, u32 id,
647 unsigned int idx, int *its_id)
649 struct acpi_iort_its_group *its;
650 struct acpi_iort_node *node;
652 node = iort_find_dev_node(dev);
656 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
660 /* Move to ITS specific data */
661 its = (struct acpi_iort_its_group *)node->node_data;
662 if (idx >= its->its_count) {
663 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
664 idx, its->its_count);
668 *its_id = its->identifiers[idx];
673 * iort_get_device_domain() - Find MSI domain related to a device
675 * @req_id: Requester ID for the device.
677 * Returns: the MSI domain for this device, NULL otherwise
679 struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
680 enum irq_domain_bus_token bus_token)
682 struct fwnode_handle *handle;
685 if (iort_dev_find_its_id(dev, id, 0, &its_id))
688 handle = iort_find_domain_token(its_id);
692 return irq_find_matching_fwnode(handle, bus_token);
695 static void iort_set_device_domain(struct device *dev,
696 struct acpi_iort_node *node)
698 struct acpi_iort_its_group *its;
699 struct acpi_iort_node *msi_parent;
700 struct acpi_iort_id_mapping *map;
701 struct fwnode_handle *iort_fwnode;
702 struct irq_domain *domain;
705 index = iort_get_id_mapping_index(node);
709 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
710 node->mapping_offset + index * sizeof(*map));
713 if (!map->output_reference ||
714 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
715 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
720 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
721 map->output_reference);
723 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
726 /* Move to ITS specific data */
727 its = (struct acpi_iort_its_group *)msi_parent->node_data;
729 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
733 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
735 dev_set_msi_domain(dev, domain);
739 * iort_get_platform_device_domain() - Find MSI domain related to a
741 * @dev: the dev pointer associated with the platform device
743 * Returns: the MSI domain for this device, NULL otherwise
745 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
747 struct acpi_iort_node *node, *msi_parent = NULL;
748 struct fwnode_handle *iort_fwnode;
749 struct acpi_iort_its_group *its;
752 /* find its associated iort node */
753 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
754 iort_match_node_callback, dev);
758 /* then find its msi parent node */
759 for (i = 0; i < node->mapping_count; i++) {
760 msi_parent = iort_node_map_platform_id(node, NULL,
769 /* Move to ITS specific data */
770 its = (struct acpi_iort_its_group *)msi_parent->node_data;
772 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
776 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
779 void acpi_configure_pmsi_domain(struct device *dev)
781 struct irq_domain *msi_domain;
783 msi_domain = iort_get_platform_device_domain(dev);
785 dev_set_msi_domain(dev, msi_domain);
788 #ifdef CONFIG_IOMMU_API
789 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
791 struct acpi_iort_node *iommu;
792 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
794 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
796 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
797 struct acpi_iort_smmu_v3 *smmu;
799 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
800 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
807 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
809 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
811 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
814 static inline int iort_add_device_replay(const struct iommu_ops *ops,
819 if (dev->bus && !device_iommu_mapped(dev))
820 err = iommu_probe_device(dev);
826 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
827 * @dev: Device from iommu_get_resv_regions()
828 * @head: Reserved region list from iommu_get_resv_regions()
830 * Returns: Number of msi reserved regions on success (0 if platform
831 * doesn't require the reservation or no associated msi regions),
832 * appropriate error value otherwise. The ITS interrupt translation
833 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
834 * are the msi reserved regions.
836 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
838 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
839 struct acpi_iort_its_group *its;
840 struct acpi_iort_node *iommu_node, *its_node = NULL;
843 iommu_node = iort_get_msi_resv_iommu(dev);
848 * Current logic to reserve ITS regions relies on HW topologies
849 * where a given PCI or named component maps its IDs to only one
850 * ITS group; if a PCI or named component can map its IDs to
851 * different ITS groups through IORT mappings this function has
852 * to be reworked to ensure we reserve regions for all ITS groups
853 * a given PCI or named component may map IDs to.
856 for (i = 0; i < fwspec->num_ids; i++) {
857 its_node = iort_node_map_id(iommu_node,
859 NULL, IORT_MSI_TYPE);
867 /* Move to ITS specific data */
868 its = (struct acpi_iort_its_group *)its_node->node_data;
870 for (i = 0; i < its->its_count; i++) {
873 if (!iort_find_its_base(its->identifiers[i], &base)) {
874 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
875 struct iommu_resv_region *region;
877 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
878 prot, IOMMU_RESV_MSI);
880 list_add_tail(®ion->list, head);
886 return (resv == its->its_count) ? resv : -ENODEV;
889 static inline bool iort_iommu_driver_enabled(u8 type)
892 case ACPI_IORT_NODE_SMMU_V3:
893 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
894 case ACPI_IORT_NODE_SMMU:
895 return IS_ENABLED(CONFIG_ARM_SMMU);
897 pr_warn("IORT node type %u does not describe an SMMU\n", type);
902 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
903 struct fwnode_handle *fwnode,
904 const struct iommu_ops *ops)
906 int ret = iommu_fwspec_init(dev, fwnode, ops);
909 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
914 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
916 struct acpi_iort_root_complex *pci_rc;
918 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
919 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
922 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
925 const struct iommu_ops *ops;
926 struct fwnode_handle *iort_fwnode;
931 iort_fwnode = iort_get_fwnode(node);
936 * If the ops look-up fails, this means that either
937 * the SMMU drivers have not been probed yet or that
938 * the SMMU drivers are not built in the kernel;
939 * Depending on whether the SMMU drivers are built-in
940 * in the kernel or not, defer the IOMMU configuration
943 ops = iommu_ops_from_fwnode(iort_fwnode);
945 return iort_iommu_driver_enabled(node->type) ?
946 -EPROBE_DEFER : -ENODEV;
948 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
951 struct iort_pci_alias_info {
953 struct acpi_iort_node *node;
956 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
958 struct iort_pci_alias_info *info = data;
959 struct acpi_iort_node *parent;
962 parent = iort_node_map_id(info->node, alias, &streamid,
964 return iort_iommu_xlate(info->dev, parent, streamid);
967 static void iort_named_component_init(struct device *dev,
968 struct acpi_iort_node *node)
970 struct acpi_iort_named_component *nc;
971 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
976 nc = (struct acpi_iort_named_component *)node->node_data;
977 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
981 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
983 struct acpi_iort_node *parent;
984 int err = -ENODEV, i = 0;
989 parent = iort_node_map_platform_id(node, &streamid,
994 err = iort_iommu_xlate(dev, parent, streamid);
995 } while (parent && !err);
1000 static int iort_nc_iommu_map_id(struct device *dev,
1001 struct acpi_iort_node *node,
1004 struct acpi_iort_node *parent;
1007 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
1009 return iort_iommu_xlate(dev, parent, streamid);
1016 * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
1018 * @dev: device to configure
1019 * @id_in: optional input id const value pointer
1021 * Returns: iommu_ops pointer on configuration success
1022 * NULL on configuration failure
1024 const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1027 struct acpi_iort_node *node;
1028 const struct iommu_ops *ops;
1032 * If we already translated the fwspec there
1033 * is nothing left to do, return the iommu_ops.
1035 ops = iort_fwspec_iommu_ops(dev);
1039 if (dev_is_pci(dev)) {
1040 struct iommu_fwspec *fwspec;
1041 struct pci_bus *bus = to_pci_dev(dev)->bus;
1042 struct iort_pci_alias_info info = { .dev = dev };
1044 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1045 iort_match_node_callback, &bus->dev);
1050 err = pci_for_each_dma_alias(to_pci_dev(dev),
1051 iort_pci_iommu_init, &info);
1053 fwspec = dev_iommu_fwspec_get(dev);
1054 if (fwspec && iort_pci_rc_supports_ats(node))
1055 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1057 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1058 iort_match_node_callback, dev);
1062 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
1063 iort_nc_iommu_map(dev, node);
1066 iort_named_component_init(dev, node);
1070 * If we have reason to believe the IOMMU driver missed the initial
1071 * add_device callback for dev, replay it to get things in order.
1074 ops = iort_fwspec_iommu_ops(dev);
1075 err = iort_add_device_replay(ops, dev);
1078 /* Ignore all other errors apart from EPROBE_DEFER */
1079 if (err == -EPROBE_DEFER) {
1082 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1090 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1092 static inline int iort_add_device_replay(const struct iommu_ops *ops,
1095 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1097 const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1098 const u32 *input_id)
1102 static int nc_dma_get_range(struct device *dev, u64 *size)
1104 struct acpi_iort_node *node;
1105 struct acpi_iort_named_component *ncomp;
1107 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1108 iort_match_node_callback, dev);
1112 ncomp = (struct acpi_iort_named_component *)node->node_data;
1114 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1115 1ULL<<ncomp->memory_address_limit;
1120 static int rc_dma_get_range(struct device *dev, u64 *size)
1122 struct acpi_iort_node *node;
1123 struct acpi_iort_root_complex *rc;
1124 struct pci_bus *pbus = to_pci_dev(dev)->bus;
1126 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1127 iort_match_node_callback, &pbus->dev);
1128 if (!node || node->revision < 1)
1131 rc = (struct acpi_iort_root_complex *)node->node_data;
1133 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1134 1ULL<<rc->memory_address_limit;
1140 * iort_dma_setup() - Set-up device DMA parameters.
1142 * @dev: device to configure
1143 * @dma_addr: device DMA address result pointer
1144 * @size: DMA range size result pointer
1146 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
1148 u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1152 * If @dev is expected to be DMA-capable then the bus code that created
1153 * it should have initialised its dma_mask pointer by this point. For
1154 * now, we'll continue the legacy behaviour of coercing it to the
1155 * coherent mask if not, but we'll no longer do so quietly.
1157 if (!dev->dma_mask) {
1158 dev_warn(dev, "DMA mask not set\n");
1159 dev->dma_mask = &dev->coherent_dma_mask;
1162 if (dev->coherent_dma_mask)
1163 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1167 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1169 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1170 : nc_dma_get_range(dev, &size);
1174 * Limit coherent and dma mask based on size retrieved from
1177 end = dmaaddr + size - 1;
1178 mask = DMA_BIT_MASK(ilog2(end) + 1);
1179 dev->bus_dma_limit = end;
1180 dev->coherent_dma_mask = mask;
1181 *dev->dma_mask = mask;
1184 *dma_addr = dmaaddr;
1187 dev->dma_pfn_offset = PFN_DOWN(offset);
1188 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1191 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1193 struct resource *res)
1195 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1199 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1206 res->flags = IORESOURCE_IRQ;
1210 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1212 struct acpi_iort_smmu_v3 *smmu;
1213 /* Always present mem resource */
1216 /* Retrieve SMMUv3 specific data */
1217 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1219 if (smmu->event_gsiv)
1225 if (smmu->gerr_gsiv)
1228 if (smmu->sync_gsiv)
1234 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1237 * Cavium ThunderX2 implementation doesn't not support unique
1238 * irq line. Use single irq line for all the SMMUv3 interrupts.
1240 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1244 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1247 return smmu->event_gsiv == smmu->pri_gsiv &&
1248 smmu->event_gsiv == smmu->gerr_gsiv &&
1249 smmu->event_gsiv == smmu->sync_gsiv;
1252 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1255 * Override the size, for Cavium ThunderX2 implementation
1256 * which doesn't support the page 1 SMMU register space.
1258 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1264 static void __init arm_smmu_v3_init_resources(struct resource *res,
1265 struct acpi_iort_node *node)
1267 struct acpi_iort_smmu_v3 *smmu;
1270 /* Retrieve SMMUv3 specific data */
1271 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1273 res[num_res].start = smmu->base_address;
1274 res[num_res].end = smmu->base_address +
1275 arm_smmu_v3_resource_size(smmu) - 1;
1276 res[num_res].flags = IORESOURCE_MEM;
1279 if (arm_smmu_v3_is_combined_irq(smmu)) {
1280 if (smmu->event_gsiv)
1281 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1282 ACPI_EDGE_SENSITIVE,
1286 if (smmu->event_gsiv)
1287 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1288 ACPI_EDGE_SENSITIVE,
1292 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1293 ACPI_EDGE_SENSITIVE,
1296 if (smmu->gerr_gsiv)
1297 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1298 ACPI_EDGE_SENSITIVE,
1301 if (smmu->sync_gsiv)
1302 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1303 ACPI_EDGE_SENSITIVE,
1308 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1309 struct acpi_iort_node *node)
1311 struct acpi_iort_smmu_v3 *smmu;
1312 enum dev_dma_attr attr;
1314 /* Retrieve SMMUv3 specific data */
1315 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1317 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1318 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1320 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1321 dev->dma_mask = &dev->coherent_dma_mask;
1323 /* Configure DMA for the page table walker */
1324 acpi_dma_configure(dev, attr);
1327 #if defined(CONFIG_ACPI_NUMA)
1329 * set numa proximity domain for smmuv3 device
1331 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1332 struct acpi_iort_node *node)
1334 struct acpi_iort_smmu_v3 *smmu;
1336 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1337 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1338 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
1340 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1343 set_dev_node(dev, dev_node);
1344 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1351 #define arm_smmu_v3_set_proximity NULL
1354 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1356 struct acpi_iort_smmu *smmu;
1358 /* Retrieve SMMU specific data */
1359 smmu = (struct acpi_iort_smmu *)node->node_data;
1362 * Only consider the global fault interrupt and ignore the
1363 * configuration access interrupt.
1365 * MMIO address and global fault interrupt resources are always
1366 * present so add them to the context interrupt count as a static
1369 return smmu->context_interrupt_count + 2;
1372 static void __init arm_smmu_init_resources(struct resource *res,
1373 struct acpi_iort_node *node)
1375 struct acpi_iort_smmu *smmu;
1376 int i, hw_irq, trigger, num_res = 0;
1377 u64 *ctx_irq, *glb_irq;
1379 /* Retrieve SMMU specific data */
1380 smmu = (struct acpi_iort_smmu *)node->node_data;
1382 res[num_res].start = smmu->base_address;
1383 res[num_res].end = smmu->base_address + smmu->span - 1;
1384 res[num_res].flags = IORESOURCE_MEM;
1387 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1389 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1390 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1392 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1396 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1397 for (i = 0; i < smmu->context_interrupt_count; i++) {
1398 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1399 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1401 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1406 static void __init arm_smmu_dma_configure(struct device *dev,
1407 struct acpi_iort_node *node)
1409 struct acpi_iort_smmu *smmu;
1410 enum dev_dma_attr attr;
1412 /* Retrieve SMMU specific data */
1413 smmu = (struct acpi_iort_smmu *)node->node_data;
1415 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1416 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1418 /* We expect the dma masks to be equivalent for SMMU set-ups */
1419 dev->dma_mask = &dev->coherent_dma_mask;
1421 /* Configure DMA for the page table walker */
1422 acpi_dma_configure(dev, attr);
1425 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1427 struct acpi_iort_pmcg *pmcg;
1429 /* Retrieve PMCG specific data */
1430 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1433 * There are always 2 memory resources.
1434 * If the overflow_gsiv is present then add that for a total of 3.
1436 return pmcg->overflow_gsiv ? 3 : 2;
1439 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1440 struct acpi_iort_node *node)
1442 struct acpi_iort_pmcg *pmcg;
1444 /* Retrieve PMCG specific data */
1445 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1447 res[0].start = pmcg->page0_base_address;
1448 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1449 res[0].flags = IORESOURCE_MEM;
1450 res[1].start = pmcg->page1_base_address;
1451 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1452 res[1].flags = IORESOURCE_MEM;
1454 if (pmcg->overflow_gsiv)
1455 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1456 ACPI_EDGE_SENSITIVE, &res[2]);
1459 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1460 /* HiSilicon Hip08 Platform */
1461 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1462 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1466 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1471 idx = acpi_match_platform_list(pmcg_plat_info);
1473 model = pmcg_plat_info[idx].data;
1475 model = IORT_SMMU_V3_PMCG_GENERIC;
1477 return platform_device_add_data(pdev, &model, sizeof(model));
1480 struct iort_dev_config {
1482 int (*dev_init)(struct acpi_iort_node *node);
1483 void (*dev_dma_configure)(struct device *dev,
1484 struct acpi_iort_node *node);
1485 int (*dev_count_resources)(struct acpi_iort_node *node);
1486 void (*dev_init_resources)(struct resource *res,
1487 struct acpi_iort_node *node);
1488 int (*dev_set_proximity)(struct device *dev,
1489 struct acpi_iort_node *node);
1490 int (*dev_add_platdata)(struct platform_device *pdev);
1493 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1494 .name = "arm-smmu-v3",
1495 .dev_dma_configure = arm_smmu_v3_dma_configure,
1496 .dev_count_resources = arm_smmu_v3_count_resources,
1497 .dev_init_resources = arm_smmu_v3_init_resources,
1498 .dev_set_proximity = arm_smmu_v3_set_proximity,
1501 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1503 .dev_dma_configure = arm_smmu_dma_configure,
1504 .dev_count_resources = arm_smmu_count_resources,
1505 .dev_init_resources = arm_smmu_init_resources,
1508 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1509 .name = "arm-smmu-v3-pmcg",
1510 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1511 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1512 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1515 static __init const struct iort_dev_config *iort_get_dev_cfg(
1516 struct acpi_iort_node *node)
1518 switch (node->type) {
1519 case ACPI_IORT_NODE_SMMU_V3:
1520 return &iort_arm_smmu_v3_cfg;
1521 case ACPI_IORT_NODE_SMMU:
1522 return &iort_arm_smmu_cfg;
1523 case ACPI_IORT_NODE_PMCG:
1524 return &iort_arm_smmu_v3_pmcg_cfg;
1531 * iort_add_platform_device() - Allocate a platform device for IORT node
1532 * @node: Pointer to device ACPI IORT node
1534 * Returns: 0 on success, <0 failure
1536 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1537 const struct iort_dev_config *ops)
1539 struct fwnode_handle *fwnode;
1540 struct platform_device *pdev;
1544 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1548 if (ops->dev_set_proximity) {
1549 ret = ops->dev_set_proximity(&pdev->dev, node);
1554 count = ops->dev_count_resources(node);
1556 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1562 ops->dev_init_resources(r, node);
1564 ret = platform_device_add_resources(pdev, r, count);
1566 * Resources are duplicated in platform_device_add_resources,
1567 * free their allocated memory
1575 * Platform devices based on PMCG nodes uses platform_data to
1576 * pass the hardware model info to the driver. For others, add
1577 * a copy of IORT node pointer to platform_data to be used to
1578 * retrieve IORT data information.
1580 if (ops->dev_add_platdata)
1581 ret = ops->dev_add_platdata(pdev);
1583 ret = platform_device_add_data(pdev, &node, sizeof(node));
1588 fwnode = iort_get_fwnode(node);
1595 pdev->dev.fwnode = fwnode;
1597 if (ops->dev_dma_configure)
1598 ops->dev_dma_configure(&pdev->dev, node);
1600 iort_set_device_domain(&pdev->dev, node);
1602 ret = platform_device_add(pdev);
1604 goto dma_deconfigure;
1609 arch_teardown_dma_ops(&pdev->dev);
1611 platform_device_put(pdev);
1617 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1619 static bool acs_enabled __initdata;
1624 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1625 struct acpi_iort_node *parent;
1626 struct acpi_iort_id_mapping *map;
1629 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1630 iort_node->mapping_offset);
1632 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1633 if (!map->output_reference)
1636 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1637 iort_table, map->output_reference);
1639 * If we detect a RC->SMMU mapping, make sure
1640 * we enable ACS on the system.
1642 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1643 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1652 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1655 static void __init iort_init_platform_devices(void)
1657 struct acpi_iort_node *iort_node, *iort_end;
1658 struct acpi_table_iort *iort;
1659 struct fwnode_handle *fwnode;
1661 const struct iort_dev_config *ops;
1664 * iort_table and iort both point to the start of IORT table, but
1665 * have different struct types
1667 iort = (struct acpi_table_iort *)iort_table;
1669 /* Get the first IORT node */
1670 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1672 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1673 iort_table->length);
1675 for (i = 0; i < iort->node_count; i++) {
1676 if (iort_node >= iort_end) {
1677 pr_err("iort node pointer overflows, bad table\n");
1681 iort_enable_acs(iort_node);
1683 ops = iort_get_dev_cfg(iort_node);
1685 fwnode = acpi_alloc_fwnode_static();
1689 iort_set_fwnode(iort_node, fwnode);
1691 ret = iort_add_platform_device(iort_node, ops);
1693 iort_delete_fwnode(iort_node);
1694 acpi_free_fwnode_static(fwnode);
1699 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1704 void __init acpi_iort_init(void)
1708 /* iort_table will be used at runtime after the iort init,
1709 * so we don't need to call acpi_put_table() to release
1710 * the IORT table mapping.
1712 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1713 if (ACPI_FAILURE(status)) {
1714 if (status != AE_NOT_FOUND) {
1715 const char *msg = acpi_format_exception(status);
1717 pr_err("Failed to get table, %s\n", msg);
1723 iort_init_platform_devices();