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/iommu.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/pci.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
21 #define IORT_TYPE_MASK(type) (1 << (type))
22 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
23 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
24 (1 << ACPI_IORT_NODE_SMMU_V3))
26 struct iort_its_msi_chip {
27 struct list_head list;
28 struct fwnode_handle *fw_node;
29 phys_addr_t base_addr;
34 struct list_head list;
35 struct acpi_iort_node *iort_node;
36 struct fwnode_handle *fwnode;
38 static LIST_HEAD(iort_fwnode_list);
39 static DEFINE_SPINLOCK(iort_fwnode_lock);
42 * iort_set_fwnode() - Create iort_fwnode and use it to register
43 * iommu data in the iort_fwnode_list
45 * @node: IORT table node associated with the IOMMU
46 * @fwnode: fwnode associated with the IORT node
48 * Returns: 0 on success
51 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
52 struct fwnode_handle *fwnode)
54 struct iort_fwnode *np;
56 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
61 INIT_LIST_HEAD(&np->list);
62 np->iort_node = iort_node;
65 spin_lock(&iort_fwnode_lock);
66 list_add_tail(&np->list, &iort_fwnode_list);
67 spin_unlock(&iort_fwnode_lock);
73 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
75 * @node: IORT table node to be looked-up
77 * Returns: fwnode_handle pointer on success, NULL on failure
79 static inline struct fwnode_handle *iort_get_fwnode(
80 struct acpi_iort_node *node)
82 struct iort_fwnode *curr;
83 struct fwnode_handle *fwnode = NULL;
85 spin_lock(&iort_fwnode_lock);
86 list_for_each_entry(curr, &iort_fwnode_list, list) {
87 if (curr->iort_node == node) {
88 fwnode = curr->fwnode;
92 spin_unlock(&iort_fwnode_lock);
98 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
100 * @node: IORT table node associated with fwnode to delete
102 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
104 struct iort_fwnode *curr, *tmp;
106 spin_lock(&iort_fwnode_lock);
107 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
108 if (curr->iort_node == node) {
109 list_del(&curr->list);
114 spin_unlock(&iort_fwnode_lock);
118 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
120 * @fwnode: fwnode associated with device to be looked-up
122 * Returns: iort_node pointer on success, NULL on failure
124 static inline struct acpi_iort_node *iort_get_iort_node(
125 struct fwnode_handle *fwnode)
127 struct iort_fwnode *curr;
128 struct acpi_iort_node *iort_node = NULL;
130 spin_lock(&iort_fwnode_lock);
131 list_for_each_entry(curr, &iort_fwnode_list, list) {
132 if (curr->fwnode == fwnode) {
133 iort_node = curr->iort_node;
137 spin_unlock(&iort_fwnode_lock);
142 typedef acpi_status (*iort_find_node_callback)
143 (struct acpi_iort_node *node, void *context);
145 /* Root pointer to the mapped IORT table */
146 static struct acpi_table_header *iort_table;
148 static LIST_HEAD(iort_msi_chip_list);
149 static DEFINE_SPINLOCK(iort_msi_chip_lock);
152 * iort_register_domain_token() - register domain token along with related
153 * ITS ID and base address to the list from where we can get it back later on.
155 * @base: ITS base address.
156 * @fw_node: Domain token.
158 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
160 int iort_register_domain_token(int trans_id, phys_addr_t base,
161 struct fwnode_handle *fw_node)
163 struct iort_its_msi_chip *its_msi_chip;
165 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
169 its_msi_chip->fw_node = fw_node;
170 its_msi_chip->translation_id = trans_id;
171 its_msi_chip->base_addr = base;
173 spin_lock(&iort_msi_chip_lock);
174 list_add(&its_msi_chip->list, &iort_msi_chip_list);
175 spin_unlock(&iort_msi_chip_lock);
181 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
186 void iort_deregister_domain_token(int trans_id)
188 struct iort_its_msi_chip *its_msi_chip, *t;
190 spin_lock(&iort_msi_chip_lock);
191 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
192 if (its_msi_chip->translation_id == trans_id) {
193 list_del(&its_msi_chip->list);
198 spin_unlock(&iort_msi_chip_lock);
202 * iort_find_domain_token() - Find domain token based on given ITS ID
205 * Returns: domain token when find on the list, NULL otherwise
207 struct fwnode_handle *iort_find_domain_token(int trans_id)
209 struct fwnode_handle *fw_node = NULL;
210 struct iort_its_msi_chip *its_msi_chip;
212 spin_lock(&iort_msi_chip_lock);
213 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
214 if (its_msi_chip->translation_id == trans_id) {
215 fw_node = its_msi_chip->fw_node;
219 spin_unlock(&iort_msi_chip_lock);
224 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
225 iort_find_node_callback callback,
228 struct acpi_iort_node *iort_node, *iort_end;
229 struct acpi_table_iort *iort;
235 /* Get the first IORT node */
236 iort = (struct acpi_table_iort *)iort_table;
237 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
239 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
242 for (i = 0; i < iort->node_count; i++) {
243 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
244 "IORT node pointer overflows, bad table!\n"))
247 if (iort_node->type == type &&
248 ACPI_SUCCESS(callback(iort_node, context)))
251 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
258 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
261 struct device *dev = context;
262 acpi_status status = AE_NOT_FOUND;
264 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
265 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
266 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
267 struct acpi_iort_named_component *ncomp;
272 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
273 if (ACPI_FAILURE(status)) {
274 dev_warn(dev, "Can't get device full path name\n");
278 ncomp = (struct acpi_iort_named_component *)node->node_data;
279 status = !strcmp(ncomp->device_name, buf.pointer) ?
280 AE_OK : AE_NOT_FOUND;
281 acpi_os_free(buf.pointer);
282 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
283 struct acpi_iort_root_complex *pci_rc;
286 bus = to_pci_bus(dev);
287 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
290 * It is assumed that PCI segment numbers maps one-to-one
291 * with root complexes. Each segment number can represent only
294 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
295 AE_OK : AE_NOT_FOUND;
301 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
304 /* Single mapping does not care for input id */
305 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
306 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
307 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
308 *rid_out = map->output_base;
312 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
317 if (rid_in < map->input_base ||
318 (rid_in >= map->input_base + map->id_count))
321 *rid_out = map->output_base + (rid_in - map->input_base);
325 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
326 u32 *id_out, int index)
328 struct acpi_iort_node *parent;
329 struct acpi_iort_id_mapping *map;
331 if (!node->mapping_offset || !node->mapping_count ||
332 index >= node->mapping_count)
335 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
336 node->mapping_offset + index * sizeof(*map));
339 if (!map->output_reference) {
340 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
345 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
346 map->output_reference);
348 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
349 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
350 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
351 node->type == ACPI_IORT_NODE_SMMU_V3 ||
352 node->type == ACPI_IORT_NODE_PMCG) {
353 *id_out = map->output_base;
361 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
363 struct acpi_iort_smmu_v3 *smmu;
365 switch (node->type) {
366 case ACPI_IORT_NODE_SMMU_V3:
368 * SMMUv3 dev ID mapping index was introduced in revision 1
369 * table, not available in revision 0
371 if (node->revision < 1)
374 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
376 * ID mapping index is only ignored if all interrupts are
379 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
383 if (smmu->id_mapping_index >= node->mapping_count) {
384 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
389 return smmu->id_mapping_index;
390 case ACPI_IORT_NODE_PMCG:
397 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
398 u32 id_in, u32 *id_out,
403 /* Parse the ID mapping tree to find specified node type */
405 struct acpi_iort_id_mapping *map;
408 if (IORT_TYPE_MASK(node->type) & type_mask) {
414 if (!node->mapping_offset || !node->mapping_count)
417 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
418 node->mapping_offset);
421 if (!map->output_reference) {
422 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
428 * Get the special ID mapping index (if any) and skip its
429 * associated ID map to prevent erroneous multi-stage
430 * IORT ID translations.
432 index = iort_get_id_mapping_index(node);
434 /* Do the ID translation */
435 for (i = 0; i < node->mapping_count; i++, map++) {
436 /* if it is special mapping index, skip it */
440 if (!iort_id_map(map, node->type, id, &id))
444 if (i == node->mapping_count)
447 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
448 map->output_reference);
452 /* Map input ID to output ID unchanged on mapping failure */
459 static struct acpi_iort_node *iort_node_map_platform_id(
460 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
463 struct acpi_iort_node *parent;
466 /* step 1: retrieve the initial dev id */
467 parent = iort_node_get_id(node, &id, index);
472 * optional step 2: map the initial dev id if its parent is not
473 * the target type we want, map it again for the use cases such
474 * as NC (named component) -> SMMU -> ITS. If the type is matched,
475 * return the initial dev id and its parent pointer directly.
477 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
478 parent = iort_node_map_id(parent, id, id_out, type_mask);
486 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
488 struct pci_bus *pbus;
490 if (!dev_is_pci(dev)) {
491 struct acpi_iort_node *node;
493 * scan iort_fwnode_list to see if it's an iort platform
494 * device (such as SMMU, PMCG),its iort node already cached
495 * and associated with fwnode when iort platform devices
498 node = iort_get_iort_node(dev->fwnode);
503 * if not, then it should be a platform device defined in
504 * DSDT/SSDT (with Named Component node in IORT)
506 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
507 iort_match_node_callback, dev);
510 /* Find a PCI root bus */
511 pbus = to_pci_dev(dev)->bus;
512 while (!pci_is_root_bus(pbus))
515 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
516 iort_match_node_callback, &pbus->dev);
520 * iort_msi_map_rid() - Map a MSI requester ID for a device
521 * @dev: The device for which the mapping is to be done.
522 * @req_id: The device requester ID.
524 * Returns: mapped MSI RID on success, input requester ID otherwise
526 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
528 struct acpi_iort_node *node;
531 node = iort_find_dev_node(dev);
535 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
540 * iort_pmsi_get_dev_id() - Get the device id for a device
541 * @dev: The device for which the mapping is to be done.
542 * @dev_id: The device ID found.
544 * Returns: 0 for successful find a dev id, -ENODEV on error
546 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
549 struct acpi_iort_node *node;
551 node = iort_find_dev_node(dev);
555 index = iort_get_id_mapping_index(node);
556 /* if there is a valid index, go get the dev_id directly */
558 if (iort_node_get_id(node, dev_id, index))
561 for (i = 0; i < node->mapping_count; i++) {
562 if (iort_node_map_platform_id(node, dev_id,
571 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
573 struct iort_its_msi_chip *its_msi_chip;
576 spin_lock(&iort_msi_chip_lock);
577 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
578 if (its_msi_chip->translation_id == its_id) {
579 *base = its_msi_chip->base_addr;
584 spin_unlock(&iort_msi_chip_lock);
590 * iort_dev_find_its_id() - Find the ITS identifier for a device
592 * @req_id: Device's requester ID
593 * @idx: Index of the ITS identifier list.
594 * @its_id: ITS identifier.
596 * Returns: 0 on success, appropriate error value otherwise
598 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
599 unsigned int idx, int *its_id)
601 struct acpi_iort_its_group *its;
602 struct acpi_iort_node *node;
604 node = iort_find_dev_node(dev);
608 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
612 /* Move to ITS specific data */
613 its = (struct acpi_iort_its_group *)node->node_data;
614 if (idx >= its->its_count) {
615 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
616 idx, its->its_count);
620 *its_id = its->identifiers[idx];
625 * iort_get_device_domain() - Find MSI domain related to a device
627 * @req_id: Requester ID for the device.
629 * Returns: the MSI domain for this device, NULL otherwise
631 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
633 struct fwnode_handle *handle;
636 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
639 handle = iort_find_domain_token(its_id);
643 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
646 static void iort_set_device_domain(struct device *dev,
647 struct acpi_iort_node *node)
649 struct acpi_iort_its_group *its;
650 struct acpi_iort_node *msi_parent;
651 struct acpi_iort_id_mapping *map;
652 struct fwnode_handle *iort_fwnode;
653 struct irq_domain *domain;
656 index = iort_get_id_mapping_index(node);
660 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
661 node->mapping_offset + index * sizeof(*map));
664 if (!map->output_reference ||
665 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
666 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
671 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
672 map->output_reference);
674 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
677 /* Move to ITS specific data */
678 its = (struct acpi_iort_its_group *)msi_parent->node_data;
680 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
684 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
686 dev_set_msi_domain(dev, domain);
690 * iort_get_platform_device_domain() - Find MSI domain related to a
692 * @dev: the dev pointer associated with the platform device
694 * Returns: the MSI domain for this device, NULL otherwise
696 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
698 struct acpi_iort_node *node, *msi_parent = NULL;
699 struct fwnode_handle *iort_fwnode;
700 struct acpi_iort_its_group *its;
703 /* find its associated iort node */
704 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
705 iort_match_node_callback, dev);
709 /* then find its msi parent node */
710 for (i = 0; i < node->mapping_count; i++) {
711 msi_parent = iort_node_map_platform_id(node, NULL,
720 /* Move to ITS specific data */
721 its = (struct acpi_iort_its_group *)msi_parent->node_data;
723 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
727 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
730 void acpi_configure_pmsi_domain(struct device *dev)
732 struct irq_domain *msi_domain;
734 msi_domain = iort_get_platform_device_domain(dev);
736 dev_set_msi_domain(dev, msi_domain);
739 static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
748 #ifdef CONFIG_IOMMU_API
749 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
751 struct acpi_iort_node *iommu;
752 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
754 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
756 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
757 struct acpi_iort_smmu_v3 *smmu;
759 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
760 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
767 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
769 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
771 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
774 static inline int iort_add_device_replay(const struct iommu_ops *ops,
779 if (dev->bus && !device_iommu_mapped(dev))
780 err = iommu_probe_device(dev);
786 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
787 * @dev: Device from iommu_get_resv_regions()
788 * @head: Reserved region list from iommu_get_resv_regions()
790 * Returns: Number of msi reserved regions on success (0 if platform
791 * doesn't require the reservation or no associated msi regions),
792 * appropriate error value otherwise. The ITS interrupt translation
793 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
794 * are the msi reserved regions.
796 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
798 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
799 struct acpi_iort_its_group *its;
800 struct acpi_iort_node *iommu_node, *its_node = NULL;
803 iommu_node = iort_get_msi_resv_iommu(dev);
808 * Current logic to reserve ITS regions relies on HW topologies
809 * where a given PCI or named component maps its IDs to only one
810 * ITS group; if a PCI or named component can map its IDs to
811 * different ITS groups through IORT mappings this function has
812 * to be reworked to ensure we reserve regions for all ITS groups
813 * a given PCI or named component may map IDs to.
816 for (i = 0; i < fwspec->num_ids; i++) {
817 its_node = iort_node_map_id(iommu_node,
819 NULL, IORT_MSI_TYPE);
827 /* Move to ITS specific data */
828 its = (struct acpi_iort_its_group *)its_node->node_data;
830 for (i = 0; i < its->its_count; i++) {
833 if (!iort_find_its_base(its->identifiers[i], &base)) {
834 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
835 struct iommu_resv_region *region;
837 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
838 prot, IOMMU_RESV_MSI);
840 list_add_tail(®ion->list, head);
846 return (resv == its->its_count) ? resv : -ENODEV;
849 static inline bool iort_iommu_driver_enabled(u8 type)
852 case ACPI_IORT_NODE_SMMU_V3:
853 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
854 case ACPI_IORT_NODE_SMMU:
855 return IS_BUILTIN(CONFIG_ARM_SMMU);
857 pr_warn("IORT node type %u does not describe an SMMU\n", type);
862 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
863 struct fwnode_handle *fwnode,
864 const struct iommu_ops *ops)
866 int ret = iommu_fwspec_init(dev, fwnode, ops);
869 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
874 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
876 struct acpi_iort_root_complex *pci_rc;
878 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
879 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
882 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
885 const struct iommu_ops *ops;
886 struct fwnode_handle *iort_fwnode;
891 iort_fwnode = iort_get_fwnode(node);
896 * If the ops look-up fails, this means that either
897 * the SMMU drivers have not been probed yet or that
898 * the SMMU drivers are not built in the kernel;
899 * Depending on whether the SMMU drivers are built-in
900 * in the kernel or not, defer the IOMMU configuration
903 ops = iommu_ops_from_fwnode(iort_fwnode);
905 return iort_iommu_driver_enabled(node->type) ?
906 -EPROBE_DEFER : -ENODEV;
908 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
911 struct iort_pci_alias_info {
913 struct acpi_iort_node *node;
916 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
918 struct iort_pci_alias_info *info = data;
919 struct acpi_iort_node *parent;
922 parent = iort_node_map_id(info->node, alias, &streamid,
924 return iort_iommu_xlate(info->dev, parent, streamid);
928 * iort_iommu_configure - Set-up IOMMU configuration for a device.
930 * @dev: device to configure
932 * Returns: iommu_ops pointer on configuration success
933 * NULL on configuration failure
935 const struct iommu_ops *iort_iommu_configure(struct device *dev)
937 struct acpi_iort_node *node, *parent;
938 const struct iommu_ops *ops;
943 * If we already translated the fwspec there
944 * is nothing left to do, return the iommu_ops.
946 ops = iort_fwspec_iommu_ops(dev);
950 if (dev_is_pci(dev)) {
951 struct pci_bus *bus = to_pci_dev(dev)->bus;
952 struct iort_pci_alias_info info = { .dev = dev };
954 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
955 iort_match_node_callback, &bus->dev);
960 err = pci_for_each_dma_alias(to_pci_dev(dev),
961 iort_pci_iommu_init, &info);
963 if (!err && iort_pci_rc_supports_ats(node))
964 dev->iommu_fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
968 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
969 iort_match_node_callback, dev);
974 parent = iort_node_map_platform_id(node, &streamid,
979 err = iort_iommu_xlate(dev, parent, streamid);
980 } while (parent && !err);
984 * If we have reason to believe the IOMMU driver missed the initial
985 * add_device callback for dev, replay it to get things in order.
988 ops = iort_fwspec_iommu_ops(dev);
989 err = iort_add_device_replay(ops, dev);
992 /* Ignore all other errors apart from EPROBE_DEFER */
993 if (err == -EPROBE_DEFER) {
996 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1003 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1005 static inline int iort_add_device_replay(const struct iommu_ops *ops,
1008 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1010 const struct iommu_ops *iort_iommu_configure(struct device *dev)
1014 static int nc_dma_get_range(struct device *dev, u64 *size)
1016 struct acpi_iort_node *node;
1017 struct acpi_iort_named_component *ncomp;
1019 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1020 iort_match_node_callback, dev);
1024 ncomp = (struct acpi_iort_named_component *)node->node_data;
1026 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1027 1ULL<<ncomp->memory_address_limit;
1032 static int rc_dma_get_range(struct device *dev, u64 *size)
1034 struct acpi_iort_node *node;
1035 struct acpi_iort_root_complex *rc;
1036 struct pci_bus *pbus = to_pci_dev(dev)->bus;
1038 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1039 iort_match_node_callback, &pbus->dev);
1040 if (!node || node->revision < 1)
1043 rc = (struct acpi_iort_root_complex *)node->node_data;
1045 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1046 1ULL<<rc->memory_address_limit;
1052 * iort_dma_setup() - Set-up device DMA parameters.
1054 * @dev: device to configure
1055 * @dma_addr: device DMA address result pointer
1056 * @size: DMA range size result pointer
1058 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
1060 u64 mask, dmaaddr = 0, size = 0, offset = 0;
1064 * If @dev is expected to be DMA-capable then the bus code that created
1065 * it should have initialised its dma_mask pointer by this point. For
1066 * now, we'll continue the legacy behaviour of coercing it to the
1067 * coherent mask if not, but we'll no longer do so quietly.
1069 if (!dev->dma_mask) {
1070 dev_warn(dev, "DMA mask not set\n");
1071 dev->dma_mask = &dev->coherent_dma_mask;
1074 if (dev->coherent_dma_mask)
1075 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1079 if (dev_is_pci(dev)) {
1080 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1082 ret = rc_dma_get_range(dev, &size);
1084 ret = nc_dma_get_range(dev, &size);
1088 msb = fls64(dmaaddr + size - 1);
1090 * Round-up to the power-of-two mask or set
1091 * the mask to the whole 64-bit address space
1092 * in case the DMA region covers the full
1095 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
1097 * Limit coherent and dma mask based on size
1098 * retrieved from firmware.
1100 dev->bus_dma_mask = mask;
1101 dev->coherent_dma_mask = mask;
1102 *dev->dma_mask = mask;
1105 *dma_addr = dmaaddr;
1108 dev->dma_pfn_offset = PFN_DOWN(offset);
1109 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1112 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1114 struct resource *res)
1116 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1120 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1127 res->flags = IORESOURCE_IRQ;
1131 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1133 struct acpi_iort_smmu_v3 *smmu;
1134 /* Always present mem resource */
1137 /* Retrieve SMMUv3 specific data */
1138 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1140 if (smmu->event_gsiv)
1146 if (smmu->gerr_gsiv)
1149 if (smmu->sync_gsiv)
1155 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1158 * Cavium ThunderX2 implementation doesn't not support unique
1159 * irq line. Use single irq line for all the SMMUv3 interrupts.
1161 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1165 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1168 return smmu->event_gsiv == smmu->pri_gsiv &&
1169 smmu->event_gsiv == smmu->gerr_gsiv &&
1170 smmu->event_gsiv == smmu->sync_gsiv;
1173 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1176 * Override the size, for Cavium ThunderX2 implementation
1177 * which doesn't support the page 1 SMMU register space.
1179 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1185 static void __init arm_smmu_v3_init_resources(struct resource *res,
1186 struct acpi_iort_node *node)
1188 struct acpi_iort_smmu_v3 *smmu;
1191 /* Retrieve SMMUv3 specific data */
1192 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1194 res[num_res].start = smmu->base_address;
1195 res[num_res].end = smmu->base_address +
1196 arm_smmu_v3_resource_size(smmu) - 1;
1197 res[num_res].flags = IORESOURCE_MEM;
1200 if (arm_smmu_v3_is_combined_irq(smmu)) {
1201 if (smmu->event_gsiv)
1202 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1203 ACPI_EDGE_SENSITIVE,
1207 if (smmu->event_gsiv)
1208 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1209 ACPI_EDGE_SENSITIVE,
1213 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1214 ACPI_EDGE_SENSITIVE,
1217 if (smmu->gerr_gsiv)
1218 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1219 ACPI_EDGE_SENSITIVE,
1222 if (smmu->sync_gsiv)
1223 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1224 ACPI_EDGE_SENSITIVE,
1229 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1230 struct acpi_iort_node *node)
1232 struct acpi_iort_smmu_v3 *smmu;
1233 enum dev_dma_attr attr;
1235 /* Retrieve SMMUv3 specific data */
1236 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1238 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1239 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1241 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1242 dev->dma_mask = &dev->coherent_dma_mask;
1244 /* Configure DMA for the page table walker */
1245 acpi_dma_configure(dev, attr);
1248 #if defined(CONFIG_ACPI_NUMA)
1250 * set numa proximity domain for smmuv3 device
1252 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1253 struct acpi_iort_node *node)
1255 struct acpi_iort_smmu_v3 *smmu;
1257 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1258 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1259 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
1261 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1264 set_dev_node(dev, dev_node);
1265 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1272 #define arm_smmu_v3_set_proximity NULL
1275 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1277 struct acpi_iort_smmu *smmu;
1279 /* Retrieve SMMU specific data */
1280 smmu = (struct acpi_iort_smmu *)node->node_data;
1283 * Only consider the global fault interrupt and ignore the
1284 * configuration access interrupt.
1286 * MMIO address and global fault interrupt resources are always
1287 * present so add them to the context interrupt count as a static
1290 return smmu->context_interrupt_count + 2;
1293 static void __init arm_smmu_init_resources(struct resource *res,
1294 struct acpi_iort_node *node)
1296 struct acpi_iort_smmu *smmu;
1297 int i, hw_irq, trigger, num_res = 0;
1298 u64 *ctx_irq, *glb_irq;
1300 /* Retrieve SMMU specific data */
1301 smmu = (struct acpi_iort_smmu *)node->node_data;
1303 res[num_res].start = smmu->base_address;
1304 res[num_res].end = smmu->base_address + smmu->span - 1;
1305 res[num_res].flags = IORESOURCE_MEM;
1308 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1310 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1311 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1313 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1317 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1318 for (i = 0; i < smmu->context_interrupt_count; i++) {
1319 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1320 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1322 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1327 static void __init arm_smmu_dma_configure(struct device *dev,
1328 struct acpi_iort_node *node)
1330 struct acpi_iort_smmu *smmu;
1331 enum dev_dma_attr attr;
1333 /* Retrieve SMMU specific data */
1334 smmu = (struct acpi_iort_smmu *)node->node_data;
1336 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1337 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1339 /* We expect the dma masks to be equivalent for SMMU set-ups */
1340 dev->dma_mask = &dev->coherent_dma_mask;
1342 /* Configure DMA for the page table walker */
1343 acpi_dma_configure(dev, attr);
1346 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1348 struct acpi_iort_pmcg *pmcg;
1350 /* Retrieve PMCG specific data */
1351 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1354 * There are always 2 memory resources.
1355 * If the overflow_gsiv is present then add that for a total of 3.
1357 return pmcg->overflow_gsiv ? 3 : 2;
1360 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1361 struct acpi_iort_node *node)
1363 struct acpi_iort_pmcg *pmcg;
1365 /* Retrieve PMCG specific data */
1366 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1368 res[0].start = pmcg->page0_base_address;
1369 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1370 res[0].flags = IORESOURCE_MEM;
1371 res[1].start = pmcg->page1_base_address;
1372 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1373 res[1].flags = IORESOURCE_MEM;
1375 if (pmcg->overflow_gsiv)
1376 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1377 ACPI_EDGE_SENSITIVE, &res[2]);
1380 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1381 /* HiSilicon Hip08 Platform */
1382 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1383 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1387 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1392 idx = acpi_match_platform_list(pmcg_plat_info);
1394 model = pmcg_plat_info[idx].data;
1396 model = IORT_SMMU_V3_PMCG_GENERIC;
1398 return platform_device_add_data(pdev, &model, sizeof(model));
1401 struct iort_dev_config {
1403 int (*dev_init)(struct acpi_iort_node *node);
1404 void (*dev_dma_configure)(struct device *dev,
1405 struct acpi_iort_node *node);
1406 int (*dev_count_resources)(struct acpi_iort_node *node);
1407 void (*dev_init_resources)(struct resource *res,
1408 struct acpi_iort_node *node);
1409 int (*dev_set_proximity)(struct device *dev,
1410 struct acpi_iort_node *node);
1411 int (*dev_add_platdata)(struct platform_device *pdev);
1414 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1415 .name = "arm-smmu-v3",
1416 .dev_dma_configure = arm_smmu_v3_dma_configure,
1417 .dev_count_resources = arm_smmu_v3_count_resources,
1418 .dev_init_resources = arm_smmu_v3_init_resources,
1419 .dev_set_proximity = arm_smmu_v3_set_proximity,
1422 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1424 .dev_dma_configure = arm_smmu_dma_configure,
1425 .dev_count_resources = arm_smmu_count_resources,
1426 .dev_init_resources = arm_smmu_init_resources,
1429 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1430 .name = "arm-smmu-v3-pmcg",
1431 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1432 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1433 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1436 static __init const struct iort_dev_config *iort_get_dev_cfg(
1437 struct acpi_iort_node *node)
1439 switch (node->type) {
1440 case ACPI_IORT_NODE_SMMU_V3:
1441 return &iort_arm_smmu_v3_cfg;
1442 case ACPI_IORT_NODE_SMMU:
1443 return &iort_arm_smmu_cfg;
1444 case ACPI_IORT_NODE_PMCG:
1445 return &iort_arm_smmu_v3_pmcg_cfg;
1452 * iort_add_platform_device() - Allocate a platform device for IORT node
1453 * @node: Pointer to device ACPI IORT node
1455 * Returns: 0 on success, <0 failure
1457 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1458 const struct iort_dev_config *ops)
1460 struct fwnode_handle *fwnode;
1461 struct platform_device *pdev;
1465 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1469 if (ops->dev_set_proximity) {
1470 ret = ops->dev_set_proximity(&pdev->dev, node);
1475 count = ops->dev_count_resources(node);
1477 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1483 ops->dev_init_resources(r, node);
1485 ret = platform_device_add_resources(pdev, r, count);
1487 * Resources are duplicated in platform_device_add_resources,
1488 * free their allocated memory
1496 * Platform devices based on PMCG nodes uses platform_data to
1497 * pass the hardware model info to the driver. For others, add
1498 * a copy of IORT node pointer to platform_data to be used to
1499 * retrieve IORT data information.
1501 if (ops->dev_add_platdata)
1502 ret = ops->dev_add_platdata(pdev);
1504 ret = platform_device_add_data(pdev, &node, sizeof(node));
1509 fwnode = iort_get_fwnode(node);
1516 pdev->dev.fwnode = fwnode;
1518 if (ops->dev_dma_configure)
1519 ops->dev_dma_configure(&pdev->dev, node);
1521 iort_set_device_domain(&pdev->dev, node);
1523 ret = platform_device_add(pdev);
1525 goto dma_deconfigure;
1530 arch_teardown_dma_ops(&pdev->dev);
1532 platform_device_put(pdev);
1538 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1540 static bool acs_enabled __initdata;
1545 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1546 struct acpi_iort_node *parent;
1547 struct acpi_iort_id_mapping *map;
1550 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1551 iort_node->mapping_offset);
1553 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1554 if (!map->output_reference)
1557 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1558 iort_table, map->output_reference);
1560 * If we detect a RC->SMMU mapping, make sure
1561 * we enable ACS on the system.
1563 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1564 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1573 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1576 static void __init iort_init_platform_devices(void)
1578 struct acpi_iort_node *iort_node, *iort_end;
1579 struct acpi_table_iort *iort;
1580 struct fwnode_handle *fwnode;
1582 const struct iort_dev_config *ops;
1585 * iort_table and iort both point to the start of IORT table, but
1586 * have different struct types
1588 iort = (struct acpi_table_iort *)iort_table;
1590 /* Get the first IORT node */
1591 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1593 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1594 iort_table->length);
1596 for (i = 0; i < iort->node_count; i++) {
1597 if (iort_node >= iort_end) {
1598 pr_err("iort node pointer overflows, bad table\n");
1602 iort_enable_acs(iort_node);
1604 ops = iort_get_dev_cfg(iort_node);
1606 fwnode = acpi_alloc_fwnode_static();
1610 iort_set_fwnode(iort_node, fwnode);
1612 ret = iort_add_platform_device(iort_node, ops);
1614 iort_delete_fwnode(iort_node);
1615 acpi_free_fwnode_static(fwnode);
1620 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1625 void __init acpi_iort_init(void)
1629 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1630 if (ACPI_FAILURE(status)) {
1631 if (status != AE_NOT_FOUND) {
1632 const char *msg = acpi_format_exception(status);
1634 pr_err("Failed to get table, %s\n", msg);
1640 iort_init_platform_devices();