2 * Copyright (C) 2016, Semihalf
3 * Author: Tomasz Nowicki <tn@semihalf.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * This file implements early detection/parsing of I/O mapping
15 * reported to OS through firmware via I/O Remapping Table (IORT)
16 * IORT document number: ARM DEN 0049A
19 #define pr_fmt(fmt) "ACPI: IORT: " fmt
21 #include <linux/acpi_iort.h>
22 #include <linux/iommu.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 #define IORT_TYPE_MASK(type) (1 << (type))
30 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
31 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
32 (1 << ACPI_IORT_NODE_SMMU_V3))
34 struct iort_its_msi_chip
{
35 struct list_head list
;
36 struct fwnode_handle
*fw_node
;
41 struct list_head list
;
42 struct acpi_iort_node
*iort_node
;
43 struct fwnode_handle
*fwnode
;
45 static LIST_HEAD(iort_fwnode_list
);
46 static DEFINE_SPINLOCK(iort_fwnode_lock
);
49 * iort_set_fwnode() - Create iort_fwnode and use it to register
50 * iommu data in the iort_fwnode_list
52 * @node: IORT table node associated with the IOMMU
53 * @fwnode: fwnode associated with the IORT node
55 * Returns: 0 on success
58 static inline int iort_set_fwnode(struct acpi_iort_node
*iort_node
,
59 struct fwnode_handle
*fwnode
)
61 struct iort_fwnode
*np
;
63 np
= kzalloc(sizeof(struct iort_fwnode
), GFP_ATOMIC
);
68 INIT_LIST_HEAD(&np
->list
);
69 np
->iort_node
= iort_node
;
72 spin_lock(&iort_fwnode_lock
);
73 list_add_tail(&np
->list
, &iort_fwnode_list
);
74 spin_unlock(&iort_fwnode_lock
);
80 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
82 * @node: IORT table node to be looked-up
84 * Returns: fwnode_handle pointer on success, NULL on failure
87 struct fwnode_handle
*iort_get_fwnode(struct acpi_iort_node
*node
)
89 struct iort_fwnode
*curr
;
90 struct fwnode_handle
*fwnode
= NULL
;
92 spin_lock(&iort_fwnode_lock
);
93 list_for_each_entry(curr
, &iort_fwnode_list
, list
) {
94 if (curr
->iort_node
== node
) {
95 fwnode
= curr
->fwnode
;
99 spin_unlock(&iort_fwnode_lock
);
105 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
107 * @node: IORT table node associated with fwnode to delete
109 static inline void iort_delete_fwnode(struct acpi_iort_node
*node
)
111 struct iort_fwnode
*curr
, *tmp
;
113 spin_lock(&iort_fwnode_lock
);
114 list_for_each_entry_safe(curr
, tmp
, &iort_fwnode_list
, list
) {
115 if (curr
->iort_node
== node
) {
116 list_del(&curr
->list
);
121 spin_unlock(&iort_fwnode_lock
);
124 typedef acpi_status (*iort_find_node_callback
)
125 (struct acpi_iort_node
*node
, void *context
);
127 /* Root pointer to the mapped IORT table */
128 static struct acpi_table_header
*iort_table
;
130 static LIST_HEAD(iort_msi_chip_list
);
131 static DEFINE_SPINLOCK(iort_msi_chip_lock
);
134 * iort_register_domain_token() - register domain token and related ITS ID
135 * to the list from where we can get it back later on.
137 * @fw_node: Domain token.
139 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
141 int iort_register_domain_token(int trans_id
, struct fwnode_handle
*fw_node
)
143 struct iort_its_msi_chip
*its_msi_chip
;
145 its_msi_chip
= kzalloc(sizeof(*its_msi_chip
), GFP_KERNEL
);
149 its_msi_chip
->fw_node
= fw_node
;
150 its_msi_chip
->translation_id
= trans_id
;
152 spin_lock(&iort_msi_chip_lock
);
153 list_add(&its_msi_chip
->list
, &iort_msi_chip_list
);
154 spin_unlock(&iort_msi_chip_lock
);
160 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
165 void iort_deregister_domain_token(int trans_id
)
167 struct iort_its_msi_chip
*its_msi_chip
, *t
;
169 spin_lock(&iort_msi_chip_lock
);
170 list_for_each_entry_safe(its_msi_chip
, t
, &iort_msi_chip_list
, list
) {
171 if (its_msi_chip
->translation_id
== trans_id
) {
172 list_del(&its_msi_chip
->list
);
177 spin_unlock(&iort_msi_chip_lock
);
181 * iort_find_domain_token() - Find domain token based on given ITS ID
184 * Returns: domain token when find on the list, NULL otherwise
186 struct fwnode_handle
*iort_find_domain_token(int trans_id
)
188 struct fwnode_handle
*fw_node
= NULL
;
189 struct iort_its_msi_chip
*its_msi_chip
;
191 spin_lock(&iort_msi_chip_lock
);
192 list_for_each_entry(its_msi_chip
, &iort_msi_chip_list
, list
) {
193 if (its_msi_chip
->translation_id
== trans_id
) {
194 fw_node
= its_msi_chip
->fw_node
;
198 spin_unlock(&iort_msi_chip_lock
);
203 static struct acpi_iort_node
*iort_scan_node(enum acpi_iort_node_type type
,
204 iort_find_node_callback callback
,
207 struct acpi_iort_node
*iort_node
, *iort_end
;
208 struct acpi_table_iort
*iort
;
214 /* Get the first IORT node */
215 iort
= (struct acpi_table_iort
*)iort_table
;
216 iort_node
= ACPI_ADD_PTR(struct acpi_iort_node
, iort
,
218 iort_end
= ACPI_ADD_PTR(struct acpi_iort_node
, iort_table
,
221 for (i
= 0; i
< iort
->node_count
; i
++) {
222 if (WARN_TAINT(iort_node
>= iort_end
, TAINT_FIRMWARE_WORKAROUND
,
223 "IORT node pointer overflows, bad table!\n"))
226 if (iort_node
->type
== type
&&
227 ACPI_SUCCESS(callback(iort_node
, context
)))
230 iort_node
= ACPI_ADD_PTR(struct acpi_iort_node
, iort_node
,
238 iort_match_type_callback(struct acpi_iort_node
*node
, void *context
)
243 bool iort_node_match(u8 type
)
245 struct acpi_iort_node
*node
;
247 node
= iort_scan_node(type
, iort_match_type_callback
, NULL
);
252 static acpi_status
iort_match_node_callback(struct acpi_iort_node
*node
,
255 struct device
*dev
= context
;
256 acpi_status status
= AE_NOT_FOUND
;
258 if (node
->type
== ACPI_IORT_NODE_NAMED_COMPONENT
) {
259 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
260 struct acpi_device
*adev
= to_acpi_device_node(dev
->fwnode
);
261 struct acpi_iort_named_component
*ncomp
;
266 status
= acpi_get_name(adev
->handle
, ACPI_FULL_PATHNAME
, &buf
);
267 if (ACPI_FAILURE(status
)) {
268 dev_warn(dev
, "Can't get device full path name\n");
272 ncomp
= (struct acpi_iort_named_component
*)node
->node_data
;
273 status
= !strcmp(ncomp
->device_name
, buf
.pointer
) ?
274 AE_OK
: AE_NOT_FOUND
;
275 acpi_os_free(buf
.pointer
);
276 } else if (node
->type
== ACPI_IORT_NODE_PCI_ROOT_COMPLEX
) {
277 struct acpi_iort_root_complex
*pci_rc
;
280 bus
= to_pci_bus(dev
);
281 pci_rc
= (struct acpi_iort_root_complex
*)node
->node_data
;
284 * It is assumed that PCI segment numbers maps one-to-one
285 * with root complexes. Each segment number can represent only
288 status
= pci_rc
->pci_segment_number
== pci_domain_nr(bus
) ?
289 AE_OK
: AE_NOT_FOUND
;
295 static int iort_id_map(struct acpi_iort_id_mapping
*map
, u8 type
, u32 rid_in
,
298 /* Single mapping does not care for input id */
299 if (map
->flags
& ACPI_IORT_ID_SINGLE_MAPPING
) {
300 if (type
== ACPI_IORT_NODE_NAMED_COMPONENT
||
301 type
== ACPI_IORT_NODE_PCI_ROOT_COMPLEX
) {
302 *rid_out
= map
->output_base
;
306 pr_warn(FW_BUG
"[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
311 if (rid_in
< map
->input_base
||
312 (rid_in
>= map
->input_base
+ map
->id_count
))
315 *rid_out
= map
->output_base
+ (rid_in
- map
->input_base
);
320 struct acpi_iort_node
*iort_node_get_id(struct acpi_iort_node
*node
,
321 u32
*id_out
, int index
)
323 struct acpi_iort_node
*parent
;
324 struct acpi_iort_id_mapping
*map
;
326 if (!node
->mapping_offset
|| !node
->mapping_count
||
327 index
>= node
->mapping_count
)
330 map
= ACPI_ADD_PTR(struct acpi_iort_id_mapping
, node
,
331 node
->mapping_offset
+ index
* sizeof(*map
));
334 if (!map
->output_reference
) {
335 pr_err(FW_BUG
"[node %p type %d] ID map has NULL parent reference\n",
340 parent
= ACPI_ADD_PTR(struct acpi_iort_node
, iort_table
,
341 map
->output_reference
);
343 if (map
->flags
& ACPI_IORT_ID_SINGLE_MAPPING
) {
344 if (node
->type
== ACPI_IORT_NODE_NAMED_COMPONENT
||
345 node
->type
== ACPI_IORT_NODE_PCI_ROOT_COMPLEX
) {
346 *id_out
= map
->output_base
;
354 static struct acpi_iort_node
*iort_node_map_id(struct acpi_iort_node
*node
,
355 u32 id_in
, u32
*id_out
,
360 /* Parse the ID mapping tree to find specified node type */
362 struct acpi_iort_id_mapping
*map
;
365 if (IORT_TYPE_MASK(node
->type
) & type_mask
) {
371 if (!node
->mapping_offset
|| !node
->mapping_count
)
374 map
= ACPI_ADD_PTR(struct acpi_iort_id_mapping
, node
,
375 node
->mapping_offset
);
378 if (!map
->output_reference
) {
379 pr_err(FW_BUG
"[node %p type %d] ID map has NULL parent reference\n",
384 /* Do the ID translation */
385 for (i
= 0; i
< node
->mapping_count
; i
++, map
++) {
386 if (!iort_id_map(map
, node
->type
, id
, &id
))
390 if (i
== node
->mapping_count
)
393 node
= ACPI_ADD_PTR(struct acpi_iort_node
, iort_table
,
394 map
->output_reference
);
398 /* Map input ID to output ID unchanged on mapping failure */
406 struct acpi_iort_node
*iort_node_map_platform_id(struct acpi_iort_node
*node
,
407 u32
*id_out
, u8 type_mask
,
410 struct acpi_iort_node
*parent
;
413 /* step 1: retrieve the initial dev id */
414 parent
= iort_node_get_id(node
, &id
, index
);
419 * optional step 2: map the initial dev id if its parent is not
420 * the target type we want, map it again for the use cases such
421 * as NC (named component) -> SMMU -> ITS. If the type is matched,
422 * return the initial dev id and its parent pointer directly.
424 if (!(IORT_TYPE_MASK(parent
->type
) & type_mask
))
425 parent
= iort_node_map_id(parent
, id
, id_out
, type_mask
);
433 static struct acpi_iort_node
*iort_find_dev_node(struct device
*dev
)
435 struct pci_bus
*pbus
;
437 if (!dev_is_pci(dev
))
438 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT
,
439 iort_match_node_callback
, dev
);
441 /* Find a PCI root bus */
442 pbus
= to_pci_dev(dev
)->bus
;
443 while (!pci_is_root_bus(pbus
))
446 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX
,
447 iort_match_node_callback
, &pbus
->dev
);
451 * iort_msi_map_rid() - Map a MSI requester ID for a device
452 * @dev: The device for which the mapping is to be done.
453 * @req_id: The device requester ID.
455 * Returns: mapped MSI RID on success, input requester ID otherwise
457 u32
iort_msi_map_rid(struct device
*dev
, u32 req_id
)
459 struct acpi_iort_node
*node
;
462 node
= iort_find_dev_node(dev
);
466 iort_node_map_id(node
, req_id
, &dev_id
, IORT_MSI_TYPE
);
471 * iort_pmsi_get_dev_id() - Get the device id for a device
472 * @dev: The device for which the mapping is to be done.
473 * @dev_id: The device ID found.
475 * Returns: 0 for successful find a dev id, -ENODEV on error
477 int iort_pmsi_get_dev_id(struct device
*dev
, u32
*dev_id
)
480 struct acpi_iort_node
*node
;
482 node
= iort_find_dev_node(dev
);
486 for (i
= 0; i
< node
->mapping_count
; i
++) {
487 if (iort_node_map_platform_id(node
, dev_id
, IORT_MSI_TYPE
, i
))
495 * iort_dev_find_its_id() - Find the ITS identifier for a device
497 * @req_id: Device's requester ID
498 * @idx: Index of the ITS identifier list.
499 * @its_id: ITS identifier.
501 * Returns: 0 on success, appropriate error value otherwise
503 static int iort_dev_find_its_id(struct device
*dev
, u32 req_id
,
504 unsigned int idx
, int *its_id
)
506 struct acpi_iort_its_group
*its
;
507 struct acpi_iort_node
*node
;
509 node
= iort_find_dev_node(dev
);
513 node
= iort_node_map_id(node
, req_id
, NULL
, IORT_MSI_TYPE
);
517 /* Move to ITS specific data */
518 its
= (struct acpi_iort_its_group
*)node
->node_data
;
519 if (idx
> its
->its_count
) {
520 dev_err(dev
, "requested ITS ID index [%d] is greater than available [%d]\n",
521 idx
, its
->its_count
);
525 *its_id
= its
->identifiers
[idx
];
530 * iort_get_device_domain() - Find MSI domain related to a device
532 * @req_id: Requester ID for the device.
534 * Returns: the MSI domain for this device, NULL otherwise
536 struct irq_domain
*iort_get_device_domain(struct device
*dev
, u32 req_id
)
538 struct fwnode_handle
*handle
;
541 if (iort_dev_find_its_id(dev
, req_id
, 0, &its_id
))
544 handle
= iort_find_domain_token(its_id
);
548 return irq_find_matching_fwnode(handle
, DOMAIN_BUS_PCI_MSI
);
552 * iort_get_platform_device_domain() - Find MSI domain related to a
554 * @dev: the dev pointer associated with the platform device
556 * Returns: the MSI domain for this device, NULL otherwise
558 static struct irq_domain
*iort_get_platform_device_domain(struct device
*dev
)
560 struct acpi_iort_node
*node
, *msi_parent
;
561 struct fwnode_handle
*iort_fwnode
;
562 struct acpi_iort_its_group
*its
;
565 /* find its associated iort node */
566 node
= iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT
,
567 iort_match_node_callback
, dev
);
571 /* then find its msi parent node */
572 for (i
= 0; i
< node
->mapping_count
; i
++) {
573 msi_parent
= iort_node_map_platform_id(node
, NULL
,
582 /* Move to ITS specific data */
583 its
= (struct acpi_iort_its_group
*)msi_parent
->node_data
;
585 iort_fwnode
= iort_find_domain_token(its
->identifiers
[0]);
589 return irq_find_matching_fwnode(iort_fwnode
, DOMAIN_BUS_PLATFORM_MSI
);
592 void acpi_configure_pmsi_domain(struct device
*dev
)
594 struct irq_domain
*msi_domain
;
596 msi_domain
= iort_get_platform_device_domain(dev
);
598 dev_set_msi_domain(dev
, msi_domain
);
601 static int __get_pci_rid(struct pci_dev
*pdev
, u16 alias
, void *data
)
609 static int arm_smmu_iort_xlate(struct device
*dev
, u32 streamid
,
610 struct fwnode_handle
*fwnode
,
611 const struct iommu_ops
*ops
)
613 int ret
= iommu_fwspec_init(dev
, fwnode
, ops
);
616 ret
= iommu_fwspec_add_ids(dev
, &streamid
, 1);
621 static inline bool iort_iommu_driver_enabled(u8 type
)
624 case ACPI_IORT_NODE_SMMU_V3
:
625 return IS_BUILTIN(CONFIG_ARM_SMMU_V3
);
626 case ACPI_IORT_NODE_SMMU
:
627 return IS_BUILTIN(CONFIG_ARM_SMMU
);
629 pr_warn("IORT node type %u does not describe an SMMU\n", type
);
634 #ifdef CONFIG_IOMMU_API
636 const struct iommu_ops
*iort_fwspec_iommu_ops(struct iommu_fwspec
*fwspec
)
638 return (fwspec
&& fwspec
->ops
) ? fwspec
->ops
: NULL
;
642 int iort_add_device_replay(const struct iommu_ops
*ops
, struct device
*dev
)
646 if (!IS_ERR_OR_NULL(ops
) && ops
->add_device
&& dev
->bus
&&
648 err
= ops
->add_device(dev
);
654 const struct iommu_ops
*iort_fwspec_iommu_ops(struct iommu_fwspec
*fwspec
)
657 int iort_add_device_replay(const struct iommu_ops
*ops
, struct device
*dev
)
661 static const struct iommu_ops
*iort_iommu_xlate(struct device
*dev
,
662 struct acpi_iort_node
*node
,
665 const struct iommu_ops
*ops
= NULL
;
667 struct fwnode_handle
*iort_fwnode
;
670 iort_fwnode
= iort_get_fwnode(node
);
674 ops
= iommu_ops_from_fwnode(iort_fwnode
);
676 * If the ops look-up fails, this means that either
677 * the SMMU drivers have not been probed yet or that
678 * the SMMU drivers are not built in the kernel;
679 * Depending on whether the SMMU drivers are built-in
680 * in the kernel or not, defer the IOMMU configuration
684 return iort_iommu_driver_enabled(node
->type
) ?
685 ERR_PTR(-EPROBE_DEFER
) : NULL
;
687 ret
= arm_smmu_iort_xlate(dev
, streamid
, iort_fwnode
, ops
);
690 return ret
? NULL
: ops
;
694 * iort_set_dma_mask - Set-up dma mask for a device.
696 * @dev: device to configure
698 void iort_set_dma_mask(struct device
*dev
)
701 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
702 * setup the correct supported mask.
704 if (!dev
->coherent_dma_mask
)
705 dev
->coherent_dma_mask
= DMA_BIT_MASK(32);
708 * Set it to coherent_dma_mask by default if the architecture
709 * code has not set it.
712 dev
->dma_mask
= &dev
->coherent_dma_mask
;
716 * iort_iommu_configure - Set-up IOMMU configuration for a device.
718 * @dev: device to configure
720 * Returns: iommu_ops pointer on configuration success
721 * NULL on configuration failure
723 const struct iommu_ops
*iort_iommu_configure(struct device
*dev
)
725 struct acpi_iort_node
*node
, *parent
;
726 const struct iommu_ops
*ops
= NULL
;
731 * If we already translated the fwspec there
732 * is nothing left to do, return the iommu_ops.
734 ops
= iort_fwspec_iommu_ops(dev
->iommu_fwspec
);
738 if (dev_is_pci(dev
)) {
739 struct pci_bus
*bus
= to_pci_dev(dev
)->bus
;
742 pci_for_each_dma_alias(to_pci_dev(dev
), __get_pci_rid
,
745 node
= iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX
,
746 iort_match_node_callback
, &bus
->dev
);
750 parent
= iort_node_map_id(node
, rid
, &streamid
,
753 ops
= iort_iommu_xlate(dev
, parent
, streamid
);
758 node
= iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT
,
759 iort_match_node_callback
, dev
);
763 parent
= iort_node_map_platform_id(node
, &streamid
,
764 IORT_IOMMU_TYPE
, i
++);
767 ops
= iort_iommu_xlate(dev
, parent
, streamid
);
768 if (IS_ERR_OR_NULL(ops
))
771 parent
= iort_node_map_platform_id(node
, &streamid
,
778 * If we have reason to believe the IOMMU driver missed the initial
779 * add_device callback for dev, replay it to get things in order.
781 err
= iort_add_device_replay(ops
, dev
);
785 /* Ignore all other errors apart from EPROBE_DEFER */
786 if (IS_ERR(ops
) && (PTR_ERR(ops
) != -EPROBE_DEFER
)) {
787 dev_dbg(dev
, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops
));
794 static void __init
acpi_iort_register_irq(int hwirq
, const char *name
,
796 struct resource
*res
)
798 int irq
= acpi_register_gsi(NULL
, hwirq
, trigger
,
802 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq
,
809 res
->flags
= IORESOURCE_IRQ
;
813 static int __init
arm_smmu_v3_count_resources(struct acpi_iort_node
*node
)
815 struct acpi_iort_smmu_v3
*smmu
;
816 /* Always present mem resource */
819 /* Retrieve SMMUv3 specific data */
820 smmu
= (struct acpi_iort_smmu_v3
*)node
->node_data
;
822 if (smmu
->event_gsiv
)
837 static void __init
arm_smmu_v3_init_resources(struct resource
*res
,
838 struct acpi_iort_node
*node
)
840 struct acpi_iort_smmu_v3
*smmu
;
843 /* Retrieve SMMUv3 specific data */
844 smmu
= (struct acpi_iort_smmu_v3
*)node
->node_data
;
846 res
[num_res
].start
= smmu
->base_address
;
847 res
[num_res
].end
= smmu
->base_address
+ SZ_128K
- 1;
848 res
[num_res
].flags
= IORESOURCE_MEM
;
852 if (smmu
->event_gsiv
)
853 acpi_iort_register_irq(smmu
->event_gsiv
, "eventq",
858 acpi_iort_register_irq(smmu
->pri_gsiv
, "priq",
863 acpi_iort_register_irq(smmu
->gerr_gsiv
, "gerror",
868 acpi_iort_register_irq(smmu
->sync_gsiv
, "cmdq-sync",
873 static bool __init
arm_smmu_v3_is_coherent(struct acpi_iort_node
*node
)
875 struct acpi_iort_smmu_v3
*smmu
;
877 /* Retrieve SMMUv3 specific data */
878 smmu
= (struct acpi_iort_smmu_v3
*)node
->node_data
;
880 return smmu
->flags
& ACPI_IORT_SMMU_V3_COHACC_OVERRIDE
;
883 static int __init
arm_smmu_count_resources(struct acpi_iort_node
*node
)
885 struct acpi_iort_smmu
*smmu
;
887 /* Retrieve SMMU specific data */
888 smmu
= (struct acpi_iort_smmu
*)node
->node_data
;
891 * Only consider the global fault interrupt and ignore the
892 * configuration access interrupt.
894 * MMIO address and global fault interrupt resources are always
895 * present so add them to the context interrupt count as a static
898 return smmu
->context_interrupt_count
+ 2;
901 static void __init
arm_smmu_init_resources(struct resource
*res
,
902 struct acpi_iort_node
*node
)
904 struct acpi_iort_smmu
*smmu
;
905 int i
, hw_irq
, trigger
, num_res
= 0;
906 u64
*ctx_irq
, *glb_irq
;
908 /* Retrieve SMMU specific data */
909 smmu
= (struct acpi_iort_smmu
*)node
->node_data
;
911 res
[num_res
].start
= smmu
->base_address
;
912 res
[num_res
].end
= smmu
->base_address
+ smmu
->span
- 1;
913 res
[num_res
].flags
= IORESOURCE_MEM
;
916 glb_irq
= ACPI_ADD_PTR(u64
, node
, smmu
->global_interrupt_offset
);
918 hw_irq
= IORT_IRQ_MASK(glb_irq
[0]);
919 trigger
= IORT_IRQ_TRIGGER_MASK(glb_irq
[0]);
921 acpi_iort_register_irq(hw_irq
, "arm-smmu-global", trigger
,
925 ctx_irq
= ACPI_ADD_PTR(u64
, node
, smmu
->context_interrupt_offset
);
926 for (i
= 0; i
< smmu
->context_interrupt_count
; i
++) {
927 hw_irq
= IORT_IRQ_MASK(ctx_irq
[i
]);
928 trigger
= IORT_IRQ_TRIGGER_MASK(ctx_irq
[i
]);
930 acpi_iort_register_irq(hw_irq
, "arm-smmu-context", trigger
,
935 static bool __init
arm_smmu_is_coherent(struct acpi_iort_node
*node
)
937 struct acpi_iort_smmu
*smmu
;
939 /* Retrieve SMMU specific data */
940 smmu
= (struct acpi_iort_smmu
*)node
->node_data
;
942 return smmu
->flags
& ACPI_IORT_SMMU_COHERENT_WALK
;
945 struct iort_iommu_config
{
947 int (*iommu_init
)(struct acpi_iort_node
*node
);
948 bool (*iommu_is_coherent
)(struct acpi_iort_node
*node
);
949 int (*iommu_count_resources
)(struct acpi_iort_node
*node
);
950 void (*iommu_init_resources
)(struct resource
*res
,
951 struct acpi_iort_node
*node
);
954 static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst
= {
955 .name
= "arm-smmu-v3",
956 .iommu_is_coherent
= arm_smmu_v3_is_coherent
,
957 .iommu_count_resources
= arm_smmu_v3_count_resources
,
958 .iommu_init_resources
= arm_smmu_v3_init_resources
961 static const struct iort_iommu_config iort_arm_smmu_cfg __initconst
= {
963 .iommu_is_coherent
= arm_smmu_is_coherent
,
964 .iommu_count_resources
= arm_smmu_count_resources
,
965 .iommu_init_resources
= arm_smmu_init_resources
969 const struct iort_iommu_config
*iort_get_iommu_cfg(struct acpi_iort_node
*node
)
971 switch (node
->type
) {
972 case ACPI_IORT_NODE_SMMU_V3
:
973 return &iort_arm_smmu_v3_cfg
;
974 case ACPI_IORT_NODE_SMMU
:
975 return &iort_arm_smmu_cfg
;
982 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
983 * @node: Pointer to SMMU ACPI IORT node
985 * Returns: 0 on success, <0 failure
987 static int __init
iort_add_smmu_platform_device(struct acpi_iort_node
*node
)
989 struct fwnode_handle
*fwnode
;
990 struct platform_device
*pdev
;
992 enum dev_dma_attr attr
;
994 const struct iort_iommu_config
*ops
= iort_get_iommu_cfg(node
);
999 pdev
= platform_device_alloc(ops
->name
, PLATFORM_DEVID_AUTO
);
1003 count
= ops
->iommu_count_resources(node
);
1005 r
= kcalloc(count
, sizeof(*r
), GFP_KERNEL
);
1011 ops
->iommu_init_resources(r
, node
);
1013 ret
= platform_device_add_resources(pdev
, r
, count
);
1015 * Resources are duplicated in platform_device_add_resources,
1016 * free their allocated memory
1024 * Add a copy of IORT node pointer to platform_data to
1025 * be used to retrieve IORT data information.
1027 ret
= platform_device_add_data(pdev
, &node
, sizeof(node
));
1032 * We expect the dma masks to be equivalent for
1035 pdev
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
1037 fwnode
= iort_get_fwnode(node
);
1044 pdev
->dev
.fwnode
= fwnode
;
1046 attr
= ops
->iommu_is_coherent(node
) ?
1047 DEV_DMA_COHERENT
: DEV_DMA_NON_COHERENT
;
1049 /* Configure DMA for the page table walker */
1050 acpi_dma_configure(&pdev
->dev
, attr
);
1052 ret
= platform_device_add(pdev
);
1054 goto dma_deconfigure
;
1059 acpi_dma_deconfigure(&pdev
->dev
);
1061 platform_device_put(pdev
);
1066 static void __init
iort_init_platform_devices(void)
1068 struct acpi_iort_node
*iort_node
, *iort_end
;
1069 struct acpi_table_iort
*iort
;
1070 struct fwnode_handle
*fwnode
;
1074 * iort_table and iort both point to the start of IORT table, but
1075 * have different struct types
1077 iort
= (struct acpi_table_iort
*)iort_table
;
1079 /* Get the first IORT node */
1080 iort_node
= ACPI_ADD_PTR(struct acpi_iort_node
, iort
,
1082 iort_end
= ACPI_ADD_PTR(struct acpi_iort_node
, iort
,
1083 iort_table
->length
);
1085 for (i
= 0; i
< iort
->node_count
; i
++) {
1086 if (iort_node
>= iort_end
) {
1087 pr_err("iort node pointer overflows, bad table\n");
1091 if ((iort_node
->type
== ACPI_IORT_NODE_SMMU
) ||
1092 (iort_node
->type
== ACPI_IORT_NODE_SMMU_V3
)) {
1094 fwnode
= acpi_alloc_fwnode_static();
1098 iort_set_fwnode(iort_node
, fwnode
);
1100 ret
= iort_add_smmu_platform_device(iort_node
);
1102 iort_delete_fwnode(iort_node
);
1103 acpi_free_fwnode_static(fwnode
);
1108 iort_node
= ACPI_ADD_PTR(struct acpi_iort_node
, iort_node
,
1113 void __init
acpi_iort_init(void)
1117 status
= acpi_get_table(ACPI_SIG_IORT
, 0, &iort_table
);
1118 if (ACPI_FAILURE(status
)) {
1119 if (status
!= AE_NOT_FOUND
) {
1120 const char *msg
= acpi_format_exception(status
);
1122 pr_err("Failed to get table, %s\n", msg
);
1128 iort_init_platform_devices();