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>
21 #include <linux/dma-map-ops.h>
23 #define IORT_TYPE_MASK(type) (1 << (type))
24 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
25 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
26 (1 << ACPI_IORT_NODE_SMMU_V3))
28 struct iort_its_msi_chip {
29 struct list_head list;
30 struct fwnode_handle *fw_node;
31 phys_addr_t base_addr;
36 struct list_head list;
37 struct acpi_iort_node *iort_node;
38 struct fwnode_handle *fwnode;
40 static LIST_HEAD(iort_fwnode_list);
41 static DEFINE_SPINLOCK(iort_fwnode_lock);
44 * iort_set_fwnode() - Create iort_fwnode and use it to register
45 * iommu data in the iort_fwnode_list
47 * @iort_node: IORT table node associated with the IOMMU
48 * @fwnode: fwnode associated with the IORT node
50 * Returns: 0 on success
53 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
54 struct fwnode_handle *fwnode)
56 struct iort_fwnode *np;
58 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
63 INIT_LIST_HEAD(&np->list);
64 np->iort_node = iort_node;
67 spin_lock(&iort_fwnode_lock);
68 list_add_tail(&np->list, &iort_fwnode_list);
69 spin_unlock(&iort_fwnode_lock);
75 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
77 * @node: IORT table node to be looked-up
79 * Returns: fwnode_handle pointer on success, NULL on failure
81 static inline struct fwnode_handle *iort_get_fwnode(
82 struct acpi_iort_node *node)
84 struct iort_fwnode *curr;
85 struct fwnode_handle *fwnode = NULL;
87 spin_lock(&iort_fwnode_lock);
88 list_for_each_entry(curr, &iort_fwnode_list, list) {
89 if (curr->iort_node == node) {
90 fwnode = curr->fwnode;
94 spin_unlock(&iort_fwnode_lock);
100 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
102 * @node: IORT table node associated with fwnode to delete
104 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
106 struct iort_fwnode *curr, *tmp;
108 spin_lock(&iort_fwnode_lock);
109 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
110 if (curr->iort_node == node) {
111 list_del(&curr->list);
116 spin_unlock(&iort_fwnode_lock);
120 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
122 * @fwnode: fwnode associated with device to be looked-up
124 * Returns: iort_node pointer on success, NULL on failure
126 static inline struct acpi_iort_node *iort_get_iort_node(
127 struct fwnode_handle *fwnode)
129 struct iort_fwnode *curr;
130 struct acpi_iort_node *iort_node = NULL;
132 spin_lock(&iort_fwnode_lock);
133 list_for_each_entry(curr, &iort_fwnode_list, list) {
134 if (curr->fwnode == fwnode) {
135 iort_node = curr->iort_node;
139 spin_unlock(&iort_fwnode_lock);
144 typedef acpi_status (*iort_find_node_callback)
145 (struct acpi_iort_node *node, void *context);
147 /* Root pointer to the mapped IORT table */
148 static struct acpi_table_header *iort_table;
150 static LIST_HEAD(iort_msi_chip_list);
151 static DEFINE_SPINLOCK(iort_msi_chip_lock);
154 * iort_register_domain_token() - register domain token along with related
155 * ITS ID and base address to the list from where we can get it back later on.
157 * @base: ITS base address.
158 * @fw_node: Domain token.
160 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
162 int iort_register_domain_token(int trans_id, phys_addr_t base,
163 struct fwnode_handle *fw_node)
165 struct iort_its_msi_chip *its_msi_chip;
167 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
171 its_msi_chip->fw_node = fw_node;
172 its_msi_chip->translation_id = trans_id;
173 its_msi_chip->base_addr = base;
175 spin_lock(&iort_msi_chip_lock);
176 list_add(&its_msi_chip->list, &iort_msi_chip_list);
177 spin_unlock(&iort_msi_chip_lock);
183 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
188 void iort_deregister_domain_token(int trans_id)
190 struct iort_its_msi_chip *its_msi_chip, *t;
192 spin_lock(&iort_msi_chip_lock);
193 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
194 if (its_msi_chip->translation_id == trans_id) {
195 list_del(&its_msi_chip->list);
200 spin_unlock(&iort_msi_chip_lock);
204 * iort_find_domain_token() - Find domain token based on given ITS ID
207 * Returns: domain token when find on the list, NULL otherwise
209 struct fwnode_handle *iort_find_domain_token(int trans_id)
211 struct fwnode_handle *fw_node = NULL;
212 struct iort_its_msi_chip *its_msi_chip;
214 spin_lock(&iort_msi_chip_lock);
215 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
216 if (its_msi_chip->translation_id == trans_id) {
217 fw_node = its_msi_chip->fw_node;
221 spin_unlock(&iort_msi_chip_lock);
226 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
227 iort_find_node_callback callback,
230 struct acpi_iort_node *iort_node, *iort_end;
231 struct acpi_table_iort *iort;
237 /* Get the first IORT node */
238 iort = (struct acpi_table_iort *)iort_table;
239 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
241 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
244 for (i = 0; i < iort->node_count; i++) {
245 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
246 "IORT node pointer overflows, bad table!\n"))
249 if (iort_node->type == type &&
250 ACPI_SUCCESS(callback(iort_node, context)))
253 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
260 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
263 struct device *dev = context;
264 acpi_status status = AE_NOT_FOUND;
266 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
267 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
268 struct acpi_device *adev;
269 struct acpi_iort_named_component *ncomp;
270 struct device *nc_dev = dev;
273 * Walk the device tree to find a device with an
274 * ACPI companion; there is no point in scanning
275 * IORT for a device matching a named component if
276 * the device does not have an ACPI companion to
280 adev = ACPI_COMPANION(nc_dev);
284 nc_dev = nc_dev->parent;
290 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
291 if (ACPI_FAILURE(status)) {
292 dev_warn(nc_dev, "Can't get device full path name\n");
296 ncomp = (struct acpi_iort_named_component *)node->node_data;
297 status = !strcmp(ncomp->device_name, buf.pointer) ?
298 AE_OK : AE_NOT_FOUND;
299 acpi_os_free(buf.pointer);
300 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
301 struct acpi_iort_root_complex *pci_rc;
304 bus = to_pci_bus(dev);
305 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
308 * It is assumed that PCI segment numbers maps one-to-one
309 * with root complexes. Each segment number can represent only
312 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
313 AE_OK : AE_NOT_FOUND;
319 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
320 u32 *rid_out, bool check_overlap)
322 /* Single mapping does not care for input id */
323 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
324 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
325 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
326 *rid_out = map->output_base;
330 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
335 if (rid_in < map->input_base ||
336 (rid_in > map->input_base + map->id_count))
341 * We already found a mapping for this input ID at the end of
342 * another region. If it coincides with the start of this
343 * region, we assume the prior match was due to the off-by-1
344 * issue mentioned below, and allow it to be superseded.
345 * Otherwise, things are *really* broken, and we just disregard
346 * duplicate matches entirely to retain compatibility.
348 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
350 if (rid_in != map->input_base)
353 pr_err(FW_BUG "applying workaround.\n");
356 *rid_out = map->output_base + (rid_in - map->input_base);
359 * Due to confusion regarding the meaning of the id_count field (which
360 * carries the number of IDs *minus 1*), we may have to disregard this
361 * match if it is at the end of the range, and overlaps with the start
364 if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
369 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
370 u32 *id_out, int index)
372 struct acpi_iort_node *parent;
373 struct acpi_iort_id_mapping *map;
375 if (!node->mapping_offset || !node->mapping_count ||
376 index >= node->mapping_count)
379 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
380 node->mapping_offset + index * sizeof(*map));
383 if (!map->output_reference) {
384 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
389 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
390 map->output_reference);
392 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
393 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
394 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
395 node->type == ACPI_IORT_NODE_SMMU_V3 ||
396 node->type == ACPI_IORT_NODE_PMCG) {
397 *id_out = map->output_base;
405 #ifndef ACPI_IORT_SMMU_V3_DEVICEID_VALID
406 #define ACPI_IORT_SMMU_V3_DEVICEID_VALID (1 << 4)
409 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
411 struct acpi_iort_smmu_v3 *smmu;
412 struct acpi_iort_pmcg *pmcg;
414 switch (node->type) {
415 case ACPI_IORT_NODE_SMMU_V3:
417 * SMMUv3 dev ID mapping index was introduced in revision 1
418 * table, not available in revision 0
420 if (node->revision < 1)
423 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
425 * Until IORT E.e (node rev. 5), the ID mapping index was
426 * defined to be valid unless all interrupts are GSIV-based.
428 if (node->revision < 5) {
429 if (smmu->event_gsiv && smmu->pri_gsiv &&
430 smmu->gerr_gsiv && smmu->sync_gsiv)
432 } else if (!(smmu->flags & ACPI_IORT_SMMU_V3_DEVICEID_VALID)) {
436 if (smmu->id_mapping_index >= node->mapping_count) {
437 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
442 return smmu->id_mapping_index;
443 case ACPI_IORT_NODE_PMCG:
444 pmcg = (struct acpi_iort_pmcg *)node->node_data;
445 if (pmcg->overflow_gsiv || node->mapping_count == 0)
454 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
455 u32 id_in, u32 *id_out,
460 /* Parse the ID mapping tree to find specified node type */
462 struct acpi_iort_id_mapping *map;
463 int i, index, rc = 0;
464 u32 out_ref = 0, map_id = id;
466 if (IORT_TYPE_MASK(node->type) & type_mask) {
472 if (!node->mapping_offset || !node->mapping_count)
475 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
476 node->mapping_offset);
479 if (!map->output_reference) {
480 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
486 * Get the special ID mapping index (if any) and skip its
487 * associated ID map to prevent erroneous multi-stage
488 * IORT ID translations.
490 index = iort_get_id_mapping_index(node);
492 /* Do the ID translation */
493 for (i = 0; i < node->mapping_count; i++, map++) {
494 /* if it is special mapping index, skip it */
498 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
502 out_ref = map->output_reference;
505 if (i == node->mapping_count && !out_ref)
508 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
509 rc ? out_ref : map->output_reference);
513 /* Map input ID to output ID unchanged on mapping failure */
520 static struct acpi_iort_node *iort_node_map_platform_id(
521 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
524 struct acpi_iort_node *parent;
527 /* step 1: retrieve the initial dev id */
528 parent = iort_node_get_id(node, &id, index);
533 * optional step 2: map the initial dev id if its parent is not
534 * the target type we want, map it again for the use cases such
535 * as NC (named component) -> SMMU -> ITS. If the type is matched,
536 * return the initial dev id and its parent pointer directly.
538 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
539 parent = iort_node_map_id(parent, id, id_out, type_mask);
547 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
549 struct pci_bus *pbus;
551 if (!dev_is_pci(dev)) {
552 struct acpi_iort_node *node;
554 * scan iort_fwnode_list to see if it's an iort platform
555 * device (such as SMMU, PMCG),its iort node already cached
556 * and associated with fwnode when iort platform devices
559 node = iort_get_iort_node(dev->fwnode);
563 * if not, then it should be a platform device defined in
564 * DSDT/SSDT (with Named Component node in IORT)
566 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
567 iort_match_node_callback, dev);
570 pbus = to_pci_dev(dev)->bus;
572 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
573 iort_match_node_callback, &pbus->dev);
577 * iort_msi_map_id() - Map a MSI input ID for a device
578 * @dev: The device for which the mapping is to be done.
579 * @input_id: The device input ID.
581 * Returns: mapped MSI ID on success, input ID otherwise
583 u32 iort_msi_map_id(struct device *dev, u32 input_id)
585 struct acpi_iort_node *node;
588 node = iort_find_dev_node(dev);
592 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
597 * iort_pmsi_get_dev_id() - Get the device id for a device
598 * @dev: The device for which the mapping is to be done.
599 * @dev_id: The device ID found.
601 * Returns: 0 for successful find a dev id, -ENODEV on error
603 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
606 struct acpi_iort_node *node;
608 node = iort_find_dev_node(dev);
612 index = iort_get_id_mapping_index(node);
613 /* if there is a valid index, go get the dev_id directly */
615 if (iort_node_get_id(node, dev_id, index))
618 for (i = 0; i < node->mapping_count; i++) {
619 if (iort_node_map_platform_id(node, dev_id,
628 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
630 struct iort_its_msi_chip *its_msi_chip;
633 spin_lock(&iort_msi_chip_lock);
634 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
635 if (its_msi_chip->translation_id == its_id) {
636 *base = its_msi_chip->base_addr;
641 spin_unlock(&iort_msi_chip_lock);
647 * iort_dev_find_its_id() - Find the ITS identifier for a device
650 * @idx: Index of the ITS identifier list.
651 * @its_id: ITS identifier.
653 * Returns: 0 on success, appropriate error value otherwise
655 static int iort_dev_find_its_id(struct device *dev, u32 id,
656 unsigned int idx, int *its_id)
658 struct acpi_iort_its_group *its;
659 struct acpi_iort_node *node;
661 node = iort_find_dev_node(dev);
665 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
669 /* Move to ITS specific data */
670 its = (struct acpi_iort_its_group *)node->node_data;
671 if (idx >= its->its_count) {
672 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
673 idx, its->its_count);
677 *its_id = its->identifiers[idx];
682 * iort_get_device_domain() - Find MSI domain related to a device
684 * @id: Requester ID for the device.
685 * @bus_token: irq domain bus token.
687 * Returns: the MSI domain for this device, NULL otherwise
689 struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
690 enum irq_domain_bus_token bus_token)
692 struct fwnode_handle *handle;
695 if (iort_dev_find_its_id(dev, id, 0, &its_id))
698 handle = iort_find_domain_token(its_id);
702 return irq_find_matching_fwnode(handle, bus_token);
705 static void iort_set_device_domain(struct device *dev,
706 struct acpi_iort_node *node)
708 struct acpi_iort_its_group *its;
709 struct acpi_iort_node *msi_parent;
710 struct acpi_iort_id_mapping *map;
711 struct fwnode_handle *iort_fwnode;
712 struct irq_domain *domain;
715 index = iort_get_id_mapping_index(node);
719 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
720 node->mapping_offset + index * sizeof(*map));
723 if (!map->output_reference ||
724 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
725 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
730 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
731 map->output_reference);
733 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
736 /* Move to ITS specific data */
737 its = (struct acpi_iort_its_group *)msi_parent->node_data;
739 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
743 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
745 dev_set_msi_domain(dev, domain);
749 * iort_get_platform_device_domain() - Find MSI domain related to a
751 * @dev: the dev pointer associated with the platform device
753 * Returns: the MSI domain for this device, NULL otherwise
755 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
757 struct acpi_iort_node *node, *msi_parent = NULL;
758 struct fwnode_handle *iort_fwnode;
759 struct acpi_iort_its_group *its;
762 /* find its associated iort node */
763 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
764 iort_match_node_callback, dev);
768 /* then find its msi parent node */
769 for (i = 0; i < node->mapping_count; i++) {
770 msi_parent = iort_node_map_platform_id(node, NULL,
779 /* Move to ITS specific data */
780 its = (struct acpi_iort_its_group *)msi_parent->node_data;
782 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
786 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
789 void acpi_configure_pmsi_domain(struct device *dev)
791 struct irq_domain *msi_domain;
793 msi_domain = iort_get_platform_device_domain(dev);
795 dev_set_msi_domain(dev, msi_domain);
798 #ifdef CONFIG_IOMMU_API
799 static void iort_rmr_free(struct device *dev,
800 struct iommu_resv_region *region)
802 struct iommu_iort_rmr_data *rmr_data;
804 rmr_data = container_of(region, struct iommu_iort_rmr_data, rr);
805 kfree(rmr_data->sids);
809 static struct iommu_iort_rmr_data *iort_rmr_alloc(
810 struct acpi_iort_rmr_desc *rmr_desc,
811 int prot, enum iommu_resv_type type,
812 u32 *sids, u32 num_sids)
814 struct iommu_iort_rmr_data *rmr_data;
815 struct iommu_resv_region *region;
817 u64 addr = rmr_desc->base_address, size = rmr_desc->length;
819 rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL);
823 /* Create a copy of SIDs array to associate with this rmr_data */
824 sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL);
829 rmr_data->sids = sids_copy;
830 rmr_data->num_sids = num_sids;
832 if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) {
833 /* PAGE align base addr and size */
835 size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address));
837 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n",
838 rmr_desc->base_address,
839 rmr_desc->base_address + rmr_desc->length - 1,
840 addr, addr + size - 1);
843 region = &rmr_data->rr;
844 INIT_LIST_HEAD(®ion->list);
845 region->start = addr;
846 region->length = size;
849 region->free = iort_rmr_free;
854 static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc,
859 for (i = 0; i < count; i++) {
860 u64 end, start = desc[i].base_address, length = desc[i].length;
863 pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n",
868 end = start + length - 1;
870 /* Check for address overlap */
871 for (j = i + 1; j < count; j++) {
872 u64 e_start = desc[j].base_address;
873 u64 e_end = e_start + desc[j].length - 1;
875 if (start <= e_end && end >= e_start)
876 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n",
883 * Please note, we will keep the already allocated RMR reserve
884 * regions in case of a memory allocation failure.
886 static void iort_get_rmrs(struct acpi_iort_node *node,
887 struct acpi_iort_node *smmu,
888 u32 *sids, u32 num_sids,
889 struct list_head *head)
891 struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data;
892 struct acpi_iort_rmr_desc *rmr_desc;
895 rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node,
898 iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count);
900 for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) {
901 struct iommu_iort_rmr_data *rmr_data;
902 enum iommu_resv_type type;
903 int prot = IOMMU_READ | IOMMU_WRITE;
905 if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED)
906 type = IOMMU_RESV_DIRECT_RELAXABLE;
908 type = IOMMU_RESV_DIRECT;
910 if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE)
913 /* Attributes 0x00 - 0x03 represents device memory */
914 if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <=
915 ACPI_IORT_RMR_ATTR_DEVICE_GRE)
917 else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) ==
918 ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB)
921 rmr_data = iort_rmr_alloc(rmr_desc, prot, type,
926 list_add_tail(&rmr_data->rr.list, head);
930 static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,
934 u32 total_count = count + new_count;
937 new_sids = krealloc_array(sids, count + new_count,
938 sizeof(*new_sids), GFP_KERNEL);
942 for (i = count; i < total_count; i++)
943 new_sids[i] = id_start++;
948 static bool iort_rmr_has_dev(struct device *dev, u32 id_start,
952 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
955 * Make sure the kernel has preserved the boot firmware PCIe
956 * configuration. This is required to ensure that the RMR PCIe
957 * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5).
959 if (dev_is_pci(dev)) {
960 struct pci_dev *pdev = to_pci_dev(dev);
961 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
963 if (!host->preserve_config)
967 for (i = 0; i < fwspec->num_ids; i++) {
968 if (fwspec->ids[i] >= id_start &&
969 fwspec->ids[i] <= id_start + id_count)
976 static void iort_node_get_rmr_info(struct acpi_iort_node *node,
977 struct acpi_iort_node *iommu,
978 struct device *dev, struct list_head *head)
980 struct acpi_iort_node *smmu = NULL;
981 struct acpi_iort_rmr *rmr;
982 struct acpi_iort_id_mapping *map;
987 if (!node->mapping_offset || !node->mapping_count) {
988 pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n",
993 rmr = (struct acpi_iort_rmr *)node->node_data;
994 if (!rmr->rmr_offset || !rmr->rmr_count)
997 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
998 node->mapping_offset);
1001 * Go through the ID mappings and see if we have a match for SMMU
1002 * and dev(if !NULL). If found, get the sids for the Node.
1003 * Please note, id_count is equal to the number of IDs in the
1006 for (i = 0; i < node->mapping_count; i++, map++) {
1007 struct acpi_iort_node *parent;
1012 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
1013 map->output_reference);
1014 if (parent != iommu)
1017 /* If dev is valid, check RMR node corresponds to the dev SID */
1018 if (dev && !iort_rmr_has_dev(dev, map->output_base,
1022 /* Retrieve SIDs associated with the Node. */
1023 sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base,
1028 num_sids += map->id_count + 1;
1034 iort_get_rmrs(node, smmu, sids, num_sids, head);
1038 static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev,
1039 struct list_head *head)
1041 struct acpi_table_iort *iort;
1042 struct acpi_iort_node *iort_node, *iort_end;
1045 /* Only supports ARM DEN 0049E.d onwards */
1046 if (iort_table->revision < 5)
1049 iort = (struct acpi_table_iort *)iort_table;
1051 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1053 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1054 iort_table->length);
1056 for (i = 0; i < iort->node_count; i++) {
1057 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
1058 "IORT node pointer overflows, bad table!\n"))
1061 if (iort_node->type == ACPI_IORT_NODE_RMR)
1062 iort_node_get_rmr_info(iort_node, iommu, dev, head);
1064 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1070 * Populate the RMR list associated with a given IOMMU and dev(if provided).
1071 * If dev is NULL, the function populates all the RMRs associated with the
1074 static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode,
1076 struct list_head *head)
1078 struct acpi_iort_node *iommu;
1080 iommu = iort_get_iort_node(iommu_fwnode);
1084 iort_find_rmrs(iommu, dev, head);
1087 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
1089 struct acpi_iort_node *iommu;
1090 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1092 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
1094 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
1095 struct acpi_iort_smmu_v3 *smmu;
1097 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
1098 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
1106 * Retrieve platform specific HW MSI reserve regions.
1107 * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K)
1108 * associated with the device are the HW MSI reserved regions.
1110 static void iort_iommu_msi_get_resv_regions(struct device *dev,
1111 struct list_head *head)
1113 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1114 struct acpi_iort_its_group *its;
1115 struct acpi_iort_node *iommu_node, *its_node = NULL;
1118 iommu_node = iort_get_msi_resv_iommu(dev);
1123 * Current logic to reserve ITS regions relies on HW topologies
1124 * where a given PCI or named component maps its IDs to only one
1125 * ITS group; if a PCI or named component can map its IDs to
1126 * different ITS groups through IORT mappings this function has
1127 * to be reworked to ensure we reserve regions for all ITS groups
1128 * a given PCI or named component may map IDs to.
1131 for (i = 0; i < fwspec->num_ids; i++) {
1132 its_node = iort_node_map_id(iommu_node,
1134 NULL, IORT_MSI_TYPE);
1142 /* Move to ITS specific data */
1143 its = (struct acpi_iort_its_group *)its_node->node_data;
1145 for (i = 0; i < its->its_count; i++) {
1148 if (!iort_find_its_base(its->identifiers[i], &base)) {
1149 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1150 struct iommu_resv_region *region;
1152 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
1153 prot, IOMMU_RESV_MSI,
1156 list_add_tail(®ion->list, head);
1162 * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions.
1163 * @dev: Device from iommu_get_resv_regions()
1164 * @head: Reserved region list from iommu_get_resv_regions()
1166 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
1168 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1170 iort_iommu_msi_get_resv_regions(dev, head);
1171 iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head);
1175 * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with
1176 * associated StreamIDs information.
1177 * @iommu_fwnode: fwnode associated with IOMMU
1178 * @head: Resereved region list
1180 void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode,
1181 struct list_head *head)
1183 iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head);
1185 EXPORT_SYMBOL_GPL(iort_get_rmr_sids);
1188 * iort_put_rmr_sids - Free memory allocated for RMR reserved regions.
1189 * @iommu_fwnode: fwnode associated with IOMMU
1190 * @head: Resereved region list
1192 void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode,
1193 struct list_head *head)
1195 struct iommu_resv_region *entry, *next;
1197 list_for_each_entry_safe(entry, next, head, list)
1198 entry->free(NULL, entry);
1200 EXPORT_SYMBOL_GPL(iort_put_rmr_sids);
1202 static inline bool iort_iommu_driver_enabled(u8 type)
1205 case ACPI_IORT_NODE_SMMU_V3:
1206 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
1207 case ACPI_IORT_NODE_SMMU:
1208 return IS_ENABLED(CONFIG_ARM_SMMU);
1210 pr_warn("IORT node type %u does not describe an SMMU\n", type);
1215 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
1217 struct acpi_iort_root_complex *pci_rc;
1219 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
1220 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
1223 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
1226 const struct iommu_ops *ops;
1227 struct fwnode_handle *iort_fwnode;
1232 iort_fwnode = iort_get_fwnode(node);
1237 * If the ops look-up fails, this means that either
1238 * the SMMU drivers have not been probed yet or that
1239 * the SMMU drivers are not built in the kernel;
1240 * Depending on whether the SMMU drivers are built-in
1241 * in the kernel or not, defer the IOMMU configuration
1244 ops = iommu_ops_from_fwnode(iort_fwnode);
1246 return iort_iommu_driver_enabled(node->type) ?
1247 -EPROBE_DEFER : -ENODEV;
1249 return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops);
1252 struct iort_pci_alias_info {
1254 struct acpi_iort_node *node;
1257 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
1259 struct iort_pci_alias_info *info = data;
1260 struct acpi_iort_node *parent;
1263 parent = iort_node_map_id(info->node, alias, &streamid,
1265 return iort_iommu_xlate(info->dev, parent, streamid);
1268 static void iort_named_component_init(struct device *dev,
1269 struct acpi_iort_node *node)
1271 struct property_entry props[3] = {};
1272 struct acpi_iort_named_component *nc;
1274 nc = (struct acpi_iort_named_component *)node->node_data;
1275 props[0] = PROPERTY_ENTRY_U32("pasid-num-bits",
1276 FIELD_GET(ACPI_IORT_NC_PASID_BITS,
1278 if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED)
1279 props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall");
1281 if (device_create_managed_software_node(dev, props, NULL))
1282 dev_warn(dev, "Could not add device properties\n");
1285 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
1287 struct acpi_iort_node *parent;
1288 int err = -ENODEV, i = 0;
1293 parent = iort_node_map_platform_id(node, &streamid,
1298 err = iort_iommu_xlate(dev, parent, streamid);
1299 } while (parent && !err);
1304 static int iort_nc_iommu_map_id(struct device *dev,
1305 struct acpi_iort_node *node,
1308 struct acpi_iort_node *parent;
1311 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
1313 return iort_iommu_xlate(dev, parent, streamid);
1320 * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
1322 * @dev: device to configure
1323 * @id_in: optional input id const value pointer
1325 * Returns: 0 on success, <0 on failure
1327 int iort_iommu_configure_id(struct device *dev, const u32 *id_in)
1329 struct acpi_iort_node *node;
1332 if (dev_is_pci(dev)) {
1333 struct iommu_fwspec *fwspec;
1334 struct pci_bus *bus = to_pci_dev(dev)->bus;
1335 struct iort_pci_alias_info info = { .dev = dev };
1337 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1338 iort_match_node_callback, &bus->dev);
1343 err = pci_for_each_dma_alias(to_pci_dev(dev),
1344 iort_pci_iommu_init, &info);
1346 fwspec = dev_iommu_fwspec_get(dev);
1347 if (fwspec && iort_pci_rc_supports_ats(node))
1348 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1350 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1351 iort_match_node_callback, dev);
1355 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
1356 iort_nc_iommu_map(dev, node);
1359 iort_named_component_init(dev, node);
1366 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
1368 int iort_iommu_configure_id(struct device *dev, const u32 *input_id)
1372 static int nc_dma_get_range(struct device *dev, u64 *size)
1374 struct acpi_iort_node *node;
1375 struct acpi_iort_named_component *ncomp;
1377 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1378 iort_match_node_callback, dev);
1382 ncomp = (struct acpi_iort_named_component *)node->node_data;
1384 if (!ncomp->memory_address_limit) {
1385 pr_warn(FW_BUG "Named component missing memory address limit\n");
1389 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1390 1ULL<<ncomp->memory_address_limit;
1395 static int rc_dma_get_range(struct device *dev, u64 *size)
1397 struct acpi_iort_node *node;
1398 struct acpi_iort_root_complex *rc;
1399 struct pci_bus *pbus = to_pci_dev(dev)->bus;
1401 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1402 iort_match_node_callback, &pbus->dev);
1403 if (!node || node->revision < 1)
1406 rc = (struct acpi_iort_root_complex *)node->node_data;
1408 if (!rc->memory_address_limit) {
1409 pr_warn(FW_BUG "Root complex missing memory address limit\n");
1413 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1414 1ULL<<rc->memory_address_limit;
1420 * iort_dma_get_ranges() - Look up DMA addressing limit for the device
1421 * @dev: device to lookup
1422 * @size: DMA range size result pointer
1424 * Return: 0 on success, an error otherwise.
1426 int iort_dma_get_ranges(struct device *dev, u64 *size)
1428 if (dev_is_pci(dev))
1429 return rc_dma_get_range(dev, size);
1431 return nc_dma_get_range(dev, size);
1434 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1436 struct resource *res)
1438 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1442 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1449 res->flags = IORESOURCE_IRQ;
1453 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1455 struct acpi_iort_smmu_v3 *smmu;
1456 /* Always present mem resource */
1459 /* Retrieve SMMUv3 specific data */
1460 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1462 if (smmu->event_gsiv)
1468 if (smmu->gerr_gsiv)
1471 if (smmu->sync_gsiv)
1477 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1480 * Cavium ThunderX2 implementation doesn't not support unique
1481 * irq line. Use single irq line for all the SMMUv3 interrupts.
1483 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1487 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1490 return smmu->event_gsiv == smmu->pri_gsiv &&
1491 smmu->event_gsiv == smmu->gerr_gsiv &&
1492 smmu->event_gsiv == smmu->sync_gsiv;
1495 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1498 * Override the size, for Cavium ThunderX2 implementation
1499 * which doesn't support the page 1 SMMU register space.
1501 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1507 static void __init arm_smmu_v3_init_resources(struct resource *res,
1508 struct acpi_iort_node *node)
1510 struct acpi_iort_smmu_v3 *smmu;
1513 /* Retrieve SMMUv3 specific data */
1514 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1516 res[num_res].start = smmu->base_address;
1517 res[num_res].end = smmu->base_address +
1518 arm_smmu_v3_resource_size(smmu) - 1;
1519 res[num_res].flags = IORESOURCE_MEM;
1522 if (arm_smmu_v3_is_combined_irq(smmu)) {
1523 if (smmu->event_gsiv)
1524 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1525 ACPI_EDGE_SENSITIVE,
1529 if (smmu->event_gsiv)
1530 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1531 ACPI_EDGE_SENSITIVE,
1535 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1536 ACPI_EDGE_SENSITIVE,
1539 if (smmu->gerr_gsiv)
1540 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1541 ACPI_EDGE_SENSITIVE,
1544 if (smmu->sync_gsiv)
1545 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1546 ACPI_EDGE_SENSITIVE,
1551 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1552 struct acpi_iort_node *node)
1554 struct acpi_iort_smmu_v3 *smmu;
1555 enum dev_dma_attr attr;
1557 /* Retrieve SMMUv3 specific data */
1558 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1560 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1561 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1563 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1564 dev->dma_mask = &dev->coherent_dma_mask;
1566 /* Configure DMA for the page table walker */
1567 acpi_dma_configure(dev, attr);
1570 #if defined(CONFIG_ACPI_NUMA)
1572 * set numa proximity domain for smmuv3 device
1574 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1575 struct acpi_iort_node *node)
1577 struct acpi_iort_smmu_v3 *smmu;
1579 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1580 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1581 int dev_node = pxm_to_node(smmu->pxm);
1583 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1586 set_dev_node(dev, dev_node);
1587 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1594 #define arm_smmu_v3_set_proximity NULL
1597 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1599 struct acpi_iort_smmu *smmu;
1601 /* Retrieve SMMU specific data */
1602 smmu = (struct acpi_iort_smmu *)node->node_data;
1605 * Only consider the global fault interrupt and ignore the
1606 * configuration access interrupt.
1608 * MMIO address and global fault interrupt resources are always
1609 * present so add them to the context interrupt count as a static
1612 return smmu->context_interrupt_count + 2;
1615 static void __init arm_smmu_init_resources(struct resource *res,
1616 struct acpi_iort_node *node)
1618 struct acpi_iort_smmu *smmu;
1619 int i, hw_irq, trigger, num_res = 0;
1620 u64 *ctx_irq, *glb_irq;
1622 /* Retrieve SMMU specific data */
1623 smmu = (struct acpi_iort_smmu *)node->node_data;
1625 res[num_res].start = smmu->base_address;
1626 res[num_res].end = smmu->base_address + smmu->span - 1;
1627 res[num_res].flags = IORESOURCE_MEM;
1630 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1632 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1633 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1635 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1639 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1640 for (i = 0; i < smmu->context_interrupt_count; i++) {
1641 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1642 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1644 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1649 static void __init arm_smmu_dma_configure(struct device *dev,
1650 struct acpi_iort_node *node)
1652 struct acpi_iort_smmu *smmu;
1653 enum dev_dma_attr attr;
1655 /* Retrieve SMMU specific data */
1656 smmu = (struct acpi_iort_smmu *)node->node_data;
1658 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1659 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1661 /* We expect the dma masks to be equivalent for SMMU set-ups */
1662 dev->dma_mask = &dev->coherent_dma_mask;
1664 /* Configure DMA for the page table walker */
1665 acpi_dma_configure(dev, attr);
1668 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1670 struct acpi_iort_pmcg *pmcg;
1672 /* Retrieve PMCG specific data */
1673 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1676 * There are always 2 memory resources.
1677 * If the overflow_gsiv is present then add that for a total of 3.
1679 return pmcg->overflow_gsiv ? 3 : 2;
1682 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1683 struct acpi_iort_node *node)
1685 struct acpi_iort_pmcg *pmcg;
1687 /* Retrieve PMCG specific data */
1688 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1690 res[0].start = pmcg->page0_base_address;
1691 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1692 res[0].flags = IORESOURCE_MEM;
1694 * The initial version in DEN0049C lacked a way to describe register
1695 * page 1, which makes it broken for most PMCG implementations; in
1696 * that case, just let the driver fail gracefully if it expects to
1697 * find a second memory resource.
1699 if (node->revision > 0) {
1700 res[1].start = pmcg->page1_base_address;
1701 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1702 res[1].flags = IORESOURCE_MEM;
1705 if (pmcg->overflow_gsiv)
1706 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1707 ACPI_EDGE_SENSITIVE, &res[2]);
1710 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1711 /* HiSilicon Hip08 Platform */
1712 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1713 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1717 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1722 idx = acpi_match_platform_list(pmcg_plat_info);
1724 model = pmcg_plat_info[idx].data;
1726 model = IORT_SMMU_V3_PMCG_GENERIC;
1728 return platform_device_add_data(pdev, &model, sizeof(model));
1731 struct iort_dev_config {
1733 int (*dev_init)(struct acpi_iort_node *node);
1734 void (*dev_dma_configure)(struct device *dev,
1735 struct acpi_iort_node *node);
1736 int (*dev_count_resources)(struct acpi_iort_node *node);
1737 void (*dev_init_resources)(struct resource *res,
1738 struct acpi_iort_node *node);
1739 int (*dev_set_proximity)(struct device *dev,
1740 struct acpi_iort_node *node);
1741 int (*dev_add_platdata)(struct platform_device *pdev);
1744 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1745 .name = "arm-smmu-v3",
1746 .dev_dma_configure = arm_smmu_v3_dma_configure,
1747 .dev_count_resources = arm_smmu_v3_count_resources,
1748 .dev_init_resources = arm_smmu_v3_init_resources,
1749 .dev_set_proximity = arm_smmu_v3_set_proximity,
1752 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1754 .dev_dma_configure = arm_smmu_dma_configure,
1755 .dev_count_resources = arm_smmu_count_resources,
1756 .dev_init_resources = arm_smmu_init_resources,
1759 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1760 .name = "arm-smmu-v3-pmcg",
1761 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1762 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1763 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1766 static __init const struct iort_dev_config *iort_get_dev_cfg(
1767 struct acpi_iort_node *node)
1769 switch (node->type) {
1770 case ACPI_IORT_NODE_SMMU_V3:
1771 return &iort_arm_smmu_v3_cfg;
1772 case ACPI_IORT_NODE_SMMU:
1773 return &iort_arm_smmu_cfg;
1774 case ACPI_IORT_NODE_PMCG:
1775 return &iort_arm_smmu_v3_pmcg_cfg;
1782 * iort_add_platform_device() - Allocate a platform device for IORT node
1783 * @node: Pointer to device ACPI IORT node
1784 * @ops: Pointer to IORT device config struct
1786 * Returns: 0 on success, <0 failure
1788 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1789 const struct iort_dev_config *ops)
1791 struct fwnode_handle *fwnode;
1792 struct platform_device *pdev;
1796 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1800 if (ops->dev_set_proximity) {
1801 ret = ops->dev_set_proximity(&pdev->dev, node);
1806 count = ops->dev_count_resources(node);
1808 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1814 ops->dev_init_resources(r, node);
1816 ret = platform_device_add_resources(pdev, r, count);
1818 * Resources are duplicated in platform_device_add_resources,
1819 * free their allocated memory
1827 * Platform devices based on PMCG nodes uses platform_data to
1828 * pass the hardware model info to the driver. For others, add
1829 * a copy of IORT node pointer to platform_data to be used to
1830 * retrieve IORT data information.
1832 if (ops->dev_add_platdata)
1833 ret = ops->dev_add_platdata(pdev);
1835 ret = platform_device_add_data(pdev, &node, sizeof(node));
1840 fwnode = iort_get_fwnode(node);
1847 pdev->dev.fwnode = fwnode;
1849 if (ops->dev_dma_configure)
1850 ops->dev_dma_configure(&pdev->dev, node);
1852 iort_set_device_domain(&pdev->dev, node);
1854 ret = platform_device_add(pdev);
1856 goto dma_deconfigure;
1861 arch_teardown_dma_ops(&pdev->dev);
1863 platform_device_put(pdev);
1869 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1871 static bool acs_enabled __initdata;
1876 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1877 struct acpi_iort_node *parent;
1878 struct acpi_iort_id_mapping *map;
1881 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1882 iort_node->mapping_offset);
1884 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1885 if (!map->output_reference)
1888 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1889 iort_table, map->output_reference);
1891 * If we detect a RC->SMMU mapping, make sure
1892 * we enable ACS on the system.
1894 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1895 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1904 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1907 static void __init iort_init_platform_devices(void)
1909 struct acpi_iort_node *iort_node, *iort_end;
1910 struct acpi_table_iort *iort;
1911 struct fwnode_handle *fwnode;
1913 const struct iort_dev_config *ops;
1916 * iort_table and iort both point to the start of IORT table, but
1917 * have different struct types
1919 iort = (struct acpi_table_iort *)iort_table;
1921 /* Get the first IORT node */
1922 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1924 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1925 iort_table->length);
1927 for (i = 0; i < iort->node_count; i++) {
1928 if (iort_node >= iort_end) {
1929 pr_err("iort node pointer overflows, bad table\n");
1933 iort_enable_acs(iort_node);
1935 ops = iort_get_dev_cfg(iort_node);
1937 fwnode = acpi_alloc_fwnode_static();
1941 iort_set_fwnode(iort_node, fwnode);
1943 ret = iort_add_platform_device(iort_node, ops);
1945 iort_delete_fwnode(iort_node);
1946 acpi_free_fwnode_static(fwnode);
1951 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1956 void __init acpi_iort_init(void)
1960 /* iort_table will be used at runtime after the iort init,
1961 * so we don't need to call acpi_put_table() to release
1962 * the IORT table mapping.
1964 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1965 if (ACPI_FAILURE(status)) {
1966 if (status != AE_NOT_FOUND) {
1967 const char *msg = acpi_format_exception(status);
1969 pr_err("Failed to get table, %s\n", msg);
1975 iort_init_platform_devices();
1978 #ifdef CONFIG_ZONE_DMA
1980 * Extract the highest CPU physical address accessible to all DMA masters in
1981 * the system. PHYS_ADDR_MAX is returned when no constrained device is found.
1983 phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
1985 phys_addr_t limit = PHYS_ADDR_MAX;
1986 struct acpi_iort_node *node, *end;
1987 struct acpi_table_iort *iort;
1994 status = acpi_get_table(ACPI_SIG_IORT, 0,
1995 (struct acpi_table_header **)&iort);
1996 if (ACPI_FAILURE(status))
1999 node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset);
2000 end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length);
2002 for (i = 0; i < iort->node_count; i++) {
2006 switch (node->type) {
2007 struct acpi_iort_named_component *ncomp;
2008 struct acpi_iort_root_complex *rc;
2009 phys_addr_t local_limit;
2011 case ACPI_IORT_NODE_NAMED_COMPONENT:
2012 ncomp = (struct acpi_iort_named_component *)node->node_data;
2013 local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
2014 limit = min_not_zero(limit, local_limit);
2017 case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
2018 if (node->revision < 1)
2021 rc = (struct acpi_iort_root_complex *)node->node_data;
2022 local_limit = DMA_BIT_MASK(rc->memory_address_limit);
2023 limit = min_not_zero(limit, local_limit);
2026 node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length);
2028 acpi_put_table(&iort->header);