Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / arm64 / iort.c
blobe11b5da6f828f3bd8fffa23094b1a31e797d7c9c
1 /*
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
12 * more details.
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;
37 phys_addr_t base_addr;
38 u32 translation_id;
41 struct iort_fwnode {
42 struct list_head list;
43 struct acpi_iort_node *iort_node;
44 struct fwnode_handle *fwnode;
46 static LIST_HEAD(iort_fwnode_list);
47 static DEFINE_SPINLOCK(iort_fwnode_lock);
49 /**
50 * iort_set_fwnode() - Create iort_fwnode and use it to register
51 * iommu data in the iort_fwnode_list
53 * @node: IORT table node associated with the IOMMU
54 * @fwnode: fwnode associated with the IORT node
56 * Returns: 0 on success
57 * <0 on failure
59 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
60 struct fwnode_handle *fwnode)
62 struct iort_fwnode *np;
64 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
66 if (WARN_ON(!np))
67 return -ENOMEM;
69 INIT_LIST_HEAD(&np->list);
70 np->iort_node = iort_node;
71 np->fwnode = fwnode;
73 spin_lock(&iort_fwnode_lock);
74 list_add_tail(&np->list, &iort_fwnode_list);
75 spin_unlock(&iort_fwnode_lock);
77 return 0;
80 /**
81 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
83 * @node: IORT table node to be looked-up
85 * Returns: fwnode_handle pointer on success, NULL on failure
87 static inline struct fwnode_handle *iort_get_fwnode(
88 struct acpi_iort_node *node)
90 struct iort_fwnode *curr;
91 struct fwnode_handle *fwnode = NULL;
93 spin_lock(&iort_fwnode_lock);
94 list_for_each_entry(curr, &iort_fwnode_list, list) {
95 if (curr->iort_node == node) {
96 fwnode = curr->fwnode;
97 break;
100 spin_unlock(&iort_fwnode_lock);
102 return fwnode;
106 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
108 * @node: IORT table node associated with fwnode to delete
110 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
112 struct iort_fwnode *curr, *tmp;
114 spin_lock(&iort_fwnode_lock);
115 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
116 if (curr->iort_node == node) {
117 list_del(&curr->list);
118 kfree(curr);
119 break;
122 spin_unlock(&iort_fwnode_lock);
126 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
128 * @fwnode: fwnode associated with device to be looked-up
130 * Returns: iort_node pointer on success, NULL on failure
132 static inline struct acpi_iort_node *iort_get_iort_node(
133 struct fwnode_handle *fwnode)
135 struct iort_fwnode *curr;
136 struct acpi_iort_node *iort_node = NULL;
138 spin_lock(&iort_fwnode_lock);
139 list_for_each_entry(curr, &iort_fwnode_list, list) {
140 if (curr->fwnode == fwnode) {
141 iort_node = curr->iort_node;
142 break;
145 spin_unlock(&iort_fwnode_lock);
147 return iort_node;
150 typedef acpi_status (*iort_find_node_callback)
151 (struct acpi_iort_node *node, void *context);
153 /* Root pointer to the mapped IORT table */
154 static struct acpi_table_header *iort_table;
156 static LIST_HEAD(iort_msi_chip_list);
157 static DEFINE_SPINLOCK(iort_msi_chip_lock);
160 * iort_register_domain_token() - register domain token along with related
161 * ITS ID and base address to the list from where we can get it back later on.
162 * @trans_id: ITS ID.
163 * @base: ITS base address.
164 * @fw_node: Domain token.
166 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
168 int iort_register_domain_token(int trans_id, phys_addr_t base,
169 struct fwnode_handle *fw_node)
171 struct iort_its_msi_chip *its_msi_chip;
173 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
174 if (!its_msi_chip)
175 return -ENOMEM;
177 its_msi_chip->fw_node = fw_node;
178 its_msi_chip->translation_id = trans_id;
179 its_msi_chip->base_addr = base;
181 spin_lock(&iort_msi_chip_lock);
182 list_add(&its_msi_chip->list, &iort_msi_chip_list);
183 spin_unlock(&iort_msi_chip_lock);
185 return 0;
189 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
190 * @trans_id: ITS ID.
192 * Returns: none.
194 void iort_deregister_domain_token(int trans_id)
196 struct iort_its_msi_chip *its_msi_chip, *t;
198 spin_lock(&iort_msi_chip_lock);
199 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
200 if (its_msi_chip->translation_id == trans_id) {
201 list_del(&its_msi_chip->list);
202 kfree(its_msi_chip);
203 break;
206 spin_unlock(&iort_msi_chip_lock);
210 * iort_find_domain_token() - Find domain token based on given ITS ID
211 * @trans_id: ITS ID.
213 * Returns: domain token when find on the list, NULL otherwise
215 struct fwnode_handle *iort_find_domain_token(int trans_id)
217 struct fwnode_handle *fw_node = NULL;
218 struct iort_its_msi_chip *its_msi_chip;
220 spin_lock(&iort_msi_chip_lock);
221 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
222 if (its_msi_chip->translation_id == trans_id) {
223 fw_node = its_msi_chip->fw_node;
224 break;
227 spin_unlock(&iort_msi_chip_lock);
229 return fw_node;
232 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
233 iort_find_node_callback callback,
234 void *context)
236 struct acpi_iort_node *iort_node, *iort_end;
237 struct acpi_table_iort *iort;
238 int i;
240 if (!iort_table)
241 return NULL;
243 /* Get the first IORT node */
244 iort = (struct acpi_table_iort *)iort_table;
245 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
246 iort->node_offset);
247 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
248 iort_table->length);
250 for (i = 0; i < iort->node_count; i++) {
251 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
252 "IORT node pointer overflows, bad table!\n"))
253 return NULL;
255 if (iort_node->type == type &&
256 ACPI_SUCCESS(callback(iort_node, context)))
257 return iort_node;
259 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
260 iort_node->length);
263 return NULL;
266 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
267 void *context)
269 struct device *dev = context;
270 acpi_status status = AE_NOT_FOUND;
272 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
273 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
274 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
275 struct acpi_iort_named_component *ncomp;
277 if (!adev)
278 goto out;
280 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
281 if (ACPI_FAILURE(status)) {
282 dev_warn(dev, "Can't get device full path name\n");
283 goto out;
286 ncomp = (struct acpi_iort_named_component *)node->node_data;
287 status = !strcmp(ncomp->device_name, buf.pointer) ?
288 AE_OK : AE_NOT_FOUND;
289 acpi_os_free(buf.pointer);
290 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
291 struct acpi_iort_root_complex *pci_rc;
292 struct pci_bus *bus;
294 bus = to_pci_bus(dev);
295 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
298 * It is assumed that PCI segment numbers maps one-to-one
299 * with root complexes. Each segment number can represent only
300 * one root complex.
302 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
303 AE_OK : AE_NOT_FOUND;
305 out:
306 return status;
309 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
310 u32 *rid_out)
312 /* Single mapping does not care for input id */
313 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
314 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
315 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
316 *rid_out = map->output_base;
317 return 0;
320 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
321 map, type);
322 return -ENXIO;
325 if (rid_in < map->input_base ||
326 (rid_in >= map->input_base + map->id_count))
327 return -ENXIO;
329 *rid_out = map->output_base + (rid_in - map->input_base);
330 return 0;
333 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
334 u32 *id_out, int index)
336 struct acpi_iort_node *parent;
337 struct acpi_iort_id_mapping *map;
339 if (!node->mapping_offset || !node->mapping_count ||
340 index >= node->mapping_count)
341 return NULL;
343 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
344 node->mapping_offset + index * sizeof(*map));
346 /* Firmware bug! */
347 if (!map->output_reference) {
348 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
349 node, node->type);
350 return NULL;
353 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
354 map->output_reference);
356 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
357 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
358 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
359 node->type == ACPI_IORT_NODE_SMMU_V3) {
360 *id_out = map->output_base;
361 return parent;
365 return NULL;
368 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
370 struct acpi_iort_smmu_v3 *smmu;
372 switch (node->type) {
373 case ACPI_IORT_NODE_SMMU_V3:
375 * SMMUv3 dev ID mapping index was introduced in revision 1
376 * table, not available in revision 0
378 if (node->revision < 1)
379 return -EINVAL;
381 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
383 * ID mapping index is only ignored if all interrupts are
384 * GSIV based
386 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
387 && smmu->sync_gsiv)
388 return -EINVAL;
390 if (smmu->id_mapping_index >= node->mapping_count) {
391 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
392 node, node->type);
393 return -EINVAL;
396 return smmu->id_mapping_index;
397 default:
398 return -EINVAL;
402 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
403 u32 id_in, u32 *id_out,
404 u8 type_mask)
406 u32 id = id_in;
408 /* Parse the ID mapping tree to find specified node type */
409 while (node) {
410 struct acpi_iort_id_mapping *map;
411 int i, index;
413 if (IORT_TYPE_MASK(node->type) & type_mask) {
414 if (id_out)
415 *id_out = id;
416 return node;
419 if (!node->mapping_offset || !node->mapping_count)
420 goto fail_map;
422 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
423 node->mapping_offset);
425 /* Firmware bug! */
426 if (!map->output_reference) {
427 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
428 node, node->type);
429 goto fail_map;
433 * Get the special ID mapping index (if any) and skip its
434 * associated ID map to prevent erroneous multi-stage
435 * IORT ID translations.
437 index = iort_get_id_mapping_index(node);
439 /* Do the ID translation */
440 for (i = 0; i < node->mapping_count; i++, map++) {
441 /* if it is special mapping index, skip it */
442 if (i == index)
443 continue;
445 if (!iort_id_map(map, node->type, id, &id))
446 break;
449 if (i == node->mapping_count)
450 goto fail_map;
452 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
453 map->output_reference);
456 fail_map:
457 /* Map input ID to output ID unchanged on mapping failure */
458 if (id_out)
459 *id_out = id_in;
461 return NULL;
464 static struct acpi_iort_node *iort_node_map_platform_id(
465 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
466 int index)
468 struct acpi_iort_node *parent;
469 u32 id;
471 /* step 1: retrieve the initial dev id */
472 parent = iort_node_get_id(node, &id, index);
473 if (!parent)
474 return NULL;
477 * optional step 2: map the initial dev id if its parent is not
478 * the target type we want, map it again for the use cases such
479 * as NC (named component) -> SMMU -> ITS. If the type is matched,
480 * return the initial dev id and its parent pointer directly.
482 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
483 parent = iort_node_map_id(parent, id, id_out, type_mask);
484 else
485 if (id_out)
486 *id_out = id;
488 return parent;
491 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
493 struct pci_bus *pbus;
495 if (!dev_is_pci(dev)) {
496 struct acpi_iort_node *node;
498 * scan iort_fwnode_list to see if it's an iort platform
499 * device (such as SMMU, PMCG),its iort node already cached
500 * and associated with fwnode when iort platform devices
501 * were initialized.
503 node = iort_get_iort_node(dev->fwnode);
504 if (node)
505 return node;
508 * if not, then it should be a platform device defined in
509 * DSDT/SSDT (with Named Component node in IORT)
511 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
512 iort_match_node_callback, dev);
515 /* Find a PCI root bus */
516 pbus = to_pci_dev(dev)->bus;
517 while (!pci_is_root_bus(pbus))
518 pbus = pbus->parent;
520 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
521 iort_match_node_callback, &pbus->dev);
525 * iort_msi_map_rid() - Map a MSI requester ID for a device
526 * @dev: The device for which the mapping is to be done.
527 * @req_id: The device requester ID.
529 * Returns: mapped MSI RID on success, input requester ID otherwise
531 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
533 struct acpi_iort_node *node;
534 u32 dev_id;
536 node = iort_find_dev_node(dev);
537 if (!node)
538 return req_id;
540 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
541 return dev_id;
545 * iort_pmsi_get_dev_id() - Get the device id for a device
546 * @dev: The device for which the mapping is to be done.
547 * @dev_id: The device ID found.
549 * Returns: 0 for successful find a dev id, -ENODEV on error
551 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
553 int i, index;
554 struct acpi_iort_node *node;
556 node = iort_find_dev_node(dev);
557 if (!node)
558 return -ENODEV;
560 index = iort_get_id_mapping_index(node);
561 /* if there is a valid index, go get the dev_id directly */
562 if (index >= 0) {
563 if (iort_node_get_id(node, dev_id, index))
564 return 0;
565 } else {
566 for (i = 0; i < node->mapping_count; i++) {
567 if (iort_node_map_platform_id(node, dev_id,
568 IORT_MSI_TYPE, i))
569 return 0;
573 return -ENODEV;
576 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
578 struct iort_its_msi_chip *its_msi_chip;
579 int ret = -ENODEV;
581 spin_lock(&iort_msi_chip_lock);
582 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
583 if (its_msi_chip->translation_id == its_id) {
584 *base = its_msi_chip->base_addr;
585 ret = 0;
586 break;
589 spin_unlock(&iort_msi_chip_lock);
591 return ret;
595 * iort_dev_find_its_id() - Find the ITS identifier for a device
596 * @dev: The device.
597 * @req_id: Device's requester ID
598 * @idx: Index of the ITS identifier list.
599 * @its_id: ITS identifier.
601 * Returns: 0 on success, appropriate error value otherwise
603 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
604 unsigned int idx, int *its_id)
606 struct acpi_iort_its_group *its;
607 struct acpi_iort_node *node;
609 node = iort_find_dev_node(dev);
610 if (!node)
611 return -ENXIO;
613 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
614 if (!node)
615 return -ENXIO;
617 /* Move to ITS specific data */
618 its = (struct acpi_iort_its_group *)node->node_data;
619 if (idx >= its->its_count) {
620 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
621 idx, its->its_count);
622 return -ENXIO;
625 *its_id = its->identifiers[idx];
626 return 0;
630 * iort_get_device_domain() - Find MSI domain related to a device
631 * @dev: The device.
632 * @req_id: Requester ID for the device.
634 * Returns: the MSI domain for this device, NULL otherwise
636 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
638 struct fwnode_handle *handle;
639 int its_id;
641 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
642 return NULL;
644 handle = iort_find_domain_token(its_id);
645 if (!handle)
646 return NULL;
648 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
651 static void iort_set_device_domain(struct device *dev,
652 struct acpi_iort_node *node)
654 struct acpi_iort_its_group *its;
655 struct acpi_iort_node *msi_parent;
656 struct acpi_iort_id_mapping *map;
657 struct fwnode_handle *iort_fwnode;
658 struct irq_domain *domain;
659 int index;
661 index = iort_get_id_mapping_index(node);
662 if (index < 0)
663 return;
665 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
666 node->mapping_offset + index * sizeof(*map));
668 /* Firmware bug! */
669 if (!map->output_reference ||
670 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
671 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
672 node, node->type);
673 return;
676 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
677 map->output_reference);
679 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
680 return;
682 /* Move to ITS specific data */
683 its = (struct acpi_iort_its_group *)msi_parent->node_data;
685 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
686 if (!iort_fwnode)
687 return;
689 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
690 if (domain)
691 dev_set_msi_domain(dev, domain);
695 * iort_get_platform_device_domain() - Find MSI domain related to a
696 * platform device
697 * @dev: the dev pointer associated with the platform device
699 * Returns: the MSI domain for this device, NULL otherwise
701 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
703 struct acpi_iort_node *node, *msi_parent = NULL;
704 struct fwnode_handle *iort_fwnode;
705 struct acpi_iort_its_group *its;
706 int i;
708 /* find its associated iort node */
709 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
710 iort_match_node_callback, dev);
711 if (!node)
712 return NULL;
714 /* then find its msi parent node */
715 for (i = 0; i < node->mapping_count; i++) {
716 msi_parent = iort_node_map_platform_id(node, NULL,
717 IORT_MSI_TYPE, i);
718 if (msi_parent)
719 break;
722 if (!msi_parent)
723 return NULL;
725 /* Move to ITS specific data */
726 its = (struct acpi_iort_its_group *)msi_parent->node_data;
728 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
729 if (!iort_fwnode)
730 return NULL;
732 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
735 void acpi_configure_pmsi_domain(struct device *dev)
737 struct irq_domain *msi_domain;
739 msi_domain = iort_get_platform_device_domain(dev);
740 if (msi_domain)
741 dev_set_msi_domain(dev, msi_domain);
744 static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
745 void *data)
747 u32 *rid = data;
749 *rid = alias;
750 return 0;
753 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
754 struct fwnode_handle *fwnode,
755 const struct iommu_ops *ops)
757 int ret = iommu_fwspec_init(dev, fwnode, ops);
759 if (!ret)
760 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
762 return ret;
765 static inline bool iort_iommu_driver_enabled(u8 type)
767 switch (type) {
768 case ACPI_IORT_NODE_SMMU_V3:
769 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
770 case ACPI_IORT_NODE_SMMU:
771 return IS_BUILTIN(CONFIG_ARM_SMMU);
772 default:
773 pr_warn("IORT node type %u does not describe an SMMU\n", type);
774 return false;
778 #ifdef CONFIG_IOMMU_API
779 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
781 struct acpi_iort_node *iommu;
782 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
784 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
786 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
787 struct acpi_iort_smmu_v3 *smmu;
789 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
790 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
791 return iommu;
794 return NULL;
797 static inline const struct iommu_ops *iort_fwspec_iommu_ops(
798 struct iommu_fwspec *fwspec)
800 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
803 static inline int iort_add_device_replay(const struct iommu_ops *ops,
804 struct device *dev)
806 int err = 0;
808 if (ops->add_device && dev->bus && !dev->iommu_group)
809 err = ops->add_device(dev);
811 return err;
815 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
816 * @dev: Device from iommu_get_resv_regions()
817 * @head: Reserved region list from iommu_get_resv_regions()
819 * Returns: Number of msi reserved regions on success (0 if platform
820 * doesn't require the reservation or no associated msi regions),
821 * appropriate error value otherwise. The ITS interrupt translation
822 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
823 * are the msi reserved regions.
825 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
827 struct acpi_iort_its_group *its;
828 struct acpi_iort_node *iommu_node, *its_node = NULL;
829 int i, resv = 0;
831 iommu_node = iort_get_msi_resv_iommu(dev);
832 if (!iommu_node)
833 return 0;
836 * Current logic to reserve ITS regions relies on HW topologies
837 * where a given PCI or named component maps its IDs to only one
838 * ITS group; if a PCI or named component can map its IDs to
839 * different ITS groups through IORT mappings this function has
840 * to be reworked to ensure we reserve regions for all ITS groups
841 * a given PCI or named component may map IDs to.
844 for (i = 0; i < dev->iommu_fwspec->num_ids; i++) {
845 its_node = iort_node_map_id(iommu_node,
846 dev->iommu_fwspec->ids[i],
847 NULL, IORT_MSI_TYPE);
848 if (its_node)
849 break;
852 if (!its_node)
853 return 0;
855 /* Move to ITS specific data */
856 its = (struct acpi_iort_its_group *)its_node->node_data;
858 for (i = 0; i < its->its_count; i++) {
859 phys_addr_t base;
861 if (!iort_find_its_base(its->identifiers[i], &base)) {
862 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
863 struct iommu_resv_region *region;
865 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
866 prot, IOMMU_RESV_MSI);
867 if (region) {
868 list_add_tail(&region->list, head);
869 resv++;
874 return (resv == its->its_count) ? resv : -ENODEV;
876 #else
877 static inline const struct iommu_ops *iort_fwspec_iommu_ops(
878 struct iommu_fwspec *fwspec)
879 { return NULL; }
880 static inline int iort_add_device_replay(const struct iommu_ops *ops,
881 struct device *dev)
882 { return 0; }
883 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
884 { return 0; }
885 #endif
887 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
888 u32 streamid)
890 const struct iommu_ops *ops;
891 struct fwnode_handle *iort_fwnode;
893 if (!node)
894 return -ENODEV;
896 iort_fwnode = iort_get_fwnode(node);
897 if (!iort_fwnode)
898 return -ENODEV;
901 * If the ops look-up fails, this means that either
902 * the SMMU drivers have not been probed yet or that
903 * the SMMU drivers are not built in the kernel;
904 * Depending on whether the SMMU drivers are built-in
905 * in the kernel or not, defer the IOMMU configuration
906 * or just abort it.
908 ops = iommu_ops_from_fwnode(iort_fwnode);
909 if (!ops)
910 return iort_iommu_driver_enabled(node->type) ?
911 -EPROBE_DEFER : -ENODEV;
913 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
916 struct iort_pci_alias_info {
917 struct device *dev;
918 struct acpi_iort_node *node;
921 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
923 struct iort_pci_alias_info *info = data;
924 struct acpi_iort_node *parent;
925 u32 streamid;
927 parent = iort_node_map_id(info->node, alias, &streamid,
928 IORT_IOMMU_TYPE);
929 return iort_iommu_xlate(info->dev, parent, streamid);
932 static int nc_dma_get_range(struct device *dev, u64 *size)
934 struct acpi_iort_node *node;
935 struct acpi_iort_named_component *ncomp;
937 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
938 iort_match_node_callback, dev);
939 if (!node)
940 return -ENODEV;
942 ncomp = (struct acpi_iort_named_component *)node->node_data;
944 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
945 1ULL<<ncomp->memory_address_limit;
947 return 0;
950 static int rc_dma_get_range(struct device *dev, u64 *size)
952 struct acpi_iort_node *node;
953 struct acpi_iort_root_complex *rc;
954 struct pci_bus *pbus = to_pci_dev(dev)->bus;
956 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
957 iort_match_node_callback, &pbus->dev);
958 if (!node || node->revision < 1)
959 return -ENODEV;
961 rc = (struct acpi_iort_root_complex *)node->node_data;
963 *size = rc->memory_address_limit >= 64 ? U64_MAX :
964 1ULL<<rc->memory_address_limit;
966 return 0;
970 * iort_dma_setup() - Set-up device DMA parameters.
972 * @dev: device to configure
973 * @dma_addr: device DMA address result pointer
974 * @size: DMA range size result pointer
976 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
978 u64 mask, dmaaddr = 0, size = 0, offset = 0;
979 int ret, msb;
982 * If @dev is expected to be DMA-capable then the bus code that created
983 * it should have initialised its dma_mask pointer by this point. For
984 * now, we'll continue the legacy behaviour of coercing it to the
985 * coherent mask if not, but we'll no longer do so quietly.
987 if (!dev->dma_mask) {
988 dev_warn(dev, "DMA mask not set\n");
989 dev->dma_mask = &dev->coherent_dma_mask;
992 if (dev->coherent_dma_mask)
993 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
994 else
995 size = 1ULL << 32;
997 if (dev_is_pci(dev)) {
998 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
999 if (ret == -ENODEV)
1000 ret = rc_dma_get_range(dev, &size);
1001 } else {
1002 ret = nc_dma_get_range(dev, &size);
1005 if (!ret) {
1006 msb = fls64(dmaaddr + size - 1);
1008 * Round-up to the power-of-two mask or set
1009 * the mask to the whole 64-bit address space
1010 * in case the DMA region covers the full
1011 * memory window.
1013 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
1015 * Limit coherent and dma mask based on size
1016 * retrieved from firmware.
1018 dev->bus_dma_mask = mask;
1019 dev->coherent_dma_mask = mask;
1020 *dev->dma_mask = mask;
1023 *dma_addr = dmaaddr;
1024 *dma_size = size;
1026 dev->dma_pfn_offset = PFN_DOWN(offset);
1027 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1031 * iort_iommu_configure - Set-up IOMMU configuration for a device.
1033 * @dev: device to configure
1035 * Returns: iommu_ops pointer on configuration success
1036 * NULL on configuration failure
1038 const struct iommu_ops *iort_iommu_configure(struct device *dev)
1040 struct acpi_iort_node *node, *parent;
1041 const struct iommu_ops *ops;
1042 u32 streamid = 0;
1043 int err = -ENODEV;
1046 * If we already translated the fwspec there
1047 * is nothing left to do, return the iommu_ops.
1049 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
1050 if (ops)
1051 return ops;
1053 if (dev_is_pci(dev)) {
1054 struct pci_bus *bus = to_pci_dev(dev)->bus;
1055 struct iort_pci_alias_info info = { .dev = dev };
1057 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1058 iort_match_node_callback, &bus->dev);
1059 if (!node)
1060 return NULL;
1062 info.node = node;
1063 err = pci_for_each_dma_alias(to_pci_dev(dev),
1064 iort_pci_iommu_init, &info);
1065 } else {
1066 int i = 0;
1068 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1069 iort_match_node_callback, dev);
1070 if (!node)
1071 return NULL;
1073 do {
1074 parent = iort_node_map_platform_id(node, &streamid,
1075 IORT_IOMMU_TYPE,
1076 i++);
1078 if (parent)
1079 err = iort_iommu_xlate(dev, parent, streamid);
1080 } while (parent && !err);
1084 * If we have reason to believe the IOMMU driver missed the initial
1085 * add_device callback for dev, replay it to get things in order.
1087 if (!err) {
1088 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
1089 err = iort_add_device_replay(ops, dev);
1092 /* Ignore all other errors apart from EPROBE_DEFER */
1093 if (err == -EPROBE_DEFER) {
1094 ops = ERR_PTR(err);
1095 } else if (err) {
1096 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1097 ops = NULL;
1100 return ops;
1103 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1104 int trigger,
1105 struct resource *res)
1107 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1108 ACPI_ACTIVE_HIGH);
1110 if (irq <= 0) {
1111 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1112 name);
1113 return;
1116 res->start = irq;
1117 res->end = irq;
1118 res->flags = IORESOURCE_IRQ;
1119 res->name = name;
1122 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1124 struct acpi_iort_smmu_v3 *smmu;
1125 /* Always present mem resource */
1126 int num_res = 1;
1128 /* Retrieve SMMUv3 specific data */
1129 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1131 if (smmu->event_gsiv)
1132 num_res++;
1134 if (smmu->pri_gsiv)
1135 num_res++;
1137 if (smmu->gerr_gsiv)
1138 num_res++;
1140 if (smmu->sync_gsiv)
1141 num_res++;
1143 return num_res;
1146 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1149 * Cavium ThunderX2 implementation doesn't not support unique
1150 * irq line. Use single irq line for all the SMMUv3 interrupts.
1152 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1153 return false;
1156 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1157 * SPI numbers here.
1159 return smmu->event_gsiv == smmu->pri_gsiv &&
1160 smmu->event_gsiv == smmu->gerr_gsiv &&
1161 smmu->event_gsiv == smmu->sync_gsiv;
1164 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1167 * Override the size, for Cavium ThunderX2 implementation
1168 * which doesn't support the page 1 SMMU register space.
1170 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1171 return SZ_64K;
1173 return SZ_128K;
1176 static void __init arm_smmu_v3_init_resources(struct resource *res,
1177 struct acpi_iort_node *node)
1179 struct acpi_iort_smmu_v3 *smmu;
1180 int num_res = 0;
1182 /* Retrieve SMMUv3 specific data */
1183 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1185 res[num_res].start = smmu->base_address;
1186 res[num_res].end = smmu->base_address +
1187 arm_smmu_v3_resource_size(smmu) - 1;
1188 res[num_res].flags = IORESOURCE_MEM;
1190 num_res++;
1191 if (arm_smmu_v3_is_combined_irq(smmu)) {
1192 if (smmu->event_gsiv)
1193 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1194 ACPI_EDGE_SENSITIVE,
1195 &res[num_res++]);
1196 } else {
1198 if (smmu->event_gsiv)
1199 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1200 ACPI_EDGE_SENSITIVE,
1201 &res[num_res++]);
1203 if (smmu->pri_gsiv)
1204 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1205 ACPI_EDGE_SENSITIVE,
1206 &res[num_res++]);
1208 if (smmu->gerr_gsiv)
1209 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1210 ACPI_EDGE_SENSITIVE,
1211 &res[num_res++]);
1213 if (smmu->sync_gsiv)
1214 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1215 ACPI_EDGE_SENSITIVE,
1216 &res[num_res++]);
1220 static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
1222 struct acpi_iort_smmu_v3 *smmu;
1224 /* Retrieve SMMUv3 specific data */
1225 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1227 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
1230 #if defined(CONFIG_ACPI_NUMA)
1232 * set numa proximity domain for smmuv3 device
1234 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1235 struct acpi_iort_node *node)
1237 struct acpi_iort_smmu_v3 *smmu;
1239 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1240 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1241 int node = acpi_map_pxm_to_node(smmu->pxm);
1243 if (node != NUMA_NO_NODE && !node_online(node))
1244 return -EINVAL;
1246 set_dev_node(dev, node);
1247 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1248 smmu->base_address,
1249 smmu->pxm);
1251 return 0;
1253 #else
1254 #define arm_smmu_v3_set_proximity NULL
1255 #endif
1257 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1259 struct acpi_iort_smmu *smmu;
1261 /* Retrieve SMMU specific data */
1262 smmu = (struct acpi_iort_smmu *)node->node_data;
1265 * Only consider the global fault interrupt and ignore the
1266 * configuration access interrupt.
1268 * MMIO address and global fault interrupt resources are always
1269 * present so add them to the context interrupt count as a static
1270 * value.
1272 return smmu->context_interrupt_count + 2;
1275 static void __init arm_smmu_init_resources(struct resource *res,
1276 struct acpi_iort_node *node)
1278 struct acpi_iort_smmu *smmu;
1279 int i, hw_irq, trigger, num_res = 0;
1280 u64 *ctx_irq, *glb_irq;
1282 /* Retrieve SMMU specific data */
1283 smmu = (struct acpi_iort_smmu *)node->node_data;
1285 res[num_res].start = smmu->base_address;
1286 res[num_res].end = smmu->base_address + smmu->span - 1;
1287 res[num_res].flags = IORESOURCE_MEM;
1288 num_res++;
1290 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1291 /* Global IRQs */
1292 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1293 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1295 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1296 &res[num_res++]);
1298 /* Context IRQs */
1299 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1300 for (i = 0; i < smmu->context_interrupt_count; i++) {
1301 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1302 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1304 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1305 &res[num_res++]);
1309 static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
1311 struct acpi_iort_smmu *smmu;
1313 /* Retrieve SMMU specific data */
1314 smmu = (struct acpi_iort_smmu *)node->node_data;
1316 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
1319 struct iort_dev_config {
1320 const char *name;
1321 int (*dev_init)(struct acpi_iort_node *node);
1322 bool (*dev_is_coherent)(struct acpi_iort_node *node);
1323 int (*dev_count_resources)(struct acpi_iort_node *node);
1324 void (*dev_init_resources)(struct resource *res,
1325 struct acpi_iort_node *node);
1326 int (*dev_set_proximity)(struct device *dev,
1327 struct acpi_iort_node *node);
1330 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1331 .name = "arm-smmu-v3",
1332 .dev_is_coherent = arm_smmu_v3_is_coherent,
1333 .dev_count_resources = arm_smmu_v3_count_resources,
1334 .dev_init_resources = arm_smmu_v3_init_resources,
1335 .dev_set_proximity = arm_smmu_v3_set_proximity,
1338 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1339 .name = "arm-smmu",
1340 .dev_is_coherent = arm_smmu_is_coherent,
1341 .dev_count_resources = arm_smmu_count_resources,
1342 .dev_init_resources = arm_smmu_init_resources
1345 static __init const struct iort_dev_config *iort_get_dev_cfg(
1346 struct acpi_iort_node *node)
1348 switch (node->type) {
1349 case ACPI_IORT_NODE_SMMU_V3:
1350 return &iort_arm_smmu_v3_cfg;
1351 case ACPI_IORT_NODE_SMMU:
1352 return &iort_arm_smmu_cfg;
1353 default:
1354 return NULL;
1359 * iort_add_platform_device() - Allocate a platform device for IORT node
1360 * @node: Pointer to device ACPI IORT node
1362 * Returns: 0 on success, <0 failure
1364 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1365 const struct iort_dev_config *ops)
1367 struct fwnode_handle *fwnode;
1368 struct platform_device *pdev;
1369 struct resource *r;
1370 enum dev_dma_attr attr;
1371 int ret, count;
1373 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1374 if (!pdev)
1375 return -ENOMEM;
1377 if (ops->dev_set_proximity) {
1378 ret = ops->dev_set_proximity(&pdev->dev, node);
1379 if (ret)
1380 goto dev_put;
1383 count = ops->dev_count_resources(node);
1385 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1386 if (!r) {
1387 ret = -ENOMEM;
1388 goto dev_put;
1391 ops->dev_init_resources(r, node);
1393 ret = platform_device_add_resources(pdev, r, count);
1395 * Resources are duplicated in platform_device_add_resources,
1396 * free their allocated memory
1398 kfree(r);
1400 if (ret)
1401 goto dev_put;
1404 * Add a copy of IORT node pointer to platform_data to
1405 * be used to retrieve IORT data information.
1407 ret = platform_device_add_data(pdev, &node, sizeof(node));
1408 if (ret)
1409 goto dev_put;
1412 * We expect the dma masks to be equivalent for
1413 * all SMMUs set-ups
1415 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1417 fwnode = iort_get_fwnode(node);
1419 if (!fwnode) {
1420 ret = -ENODEV;
1421 goto dev_put;
1424 pdev->dev.fwnode = fwnode;
1426 attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
1427 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1429 /* Configure DMA for the page table walker */
1430 acpi_dma_configure(&pdev->dev, attr);
1432 iort_set_device_domain(&pdev->dev, node);
1434 ret = platform_device_add(pdev);
1435 if (ret)
1436 goto dma_deconfigure;
1438 return 0;
1440 dma_deconfigure:
1441 acpi_dma_deconfigure(&pdev->dev);
1442 dev_put:
1443 platform_device_put(pdev);
1445 return ret;
1448 static bool __init iort_enable_acs(struct acpi_iort_node *iort_node)
1450 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1451 struct acpi_iort_node *parent;
1452 struct acpi_iort_id_mapping *map;
1453 int i;
1455 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1456 iort_node->mapping_offset);
1458 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1459 if (!map->output_reference)
1460 continue;
1462 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1463 iort_table, map->output_reference);
1465 * If we detect a RC->SMMU mapping, make sure
1466 * we enable ACS on the system.
1468 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1469 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1470 pci_request_acs();
1471 return true;
1476 return false;
1479 static void __init iort_init_platform_devices(void)
1481 struct acpi_iort_node *iort_node, *iort_end;
1482 struct acpi_table_iort *iort;
1483 struct fwnode_handle *fwnode;
1484 int i, ret;
1485 bool acs_enabled = false;
1486 const struct iort_dev_config *ops;
1489 * iort_table and iort both point to the start of IORT table, but
1490 * have different struct types
1492 iort = (struct acpi_table_iort *)iort_table;
1494 /* Get the first IORT node */
1495 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1496 iort->node_offset);
1497 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1498 iort_table->length);
1500 for (i = 0; i < iort->node_count; i++) {
1501 if (iort_node >= iort_end) {
1502 pr_err("iort node pointer overflows, bad table\n");
1503 return;
1506 if (!acs_enabled)
1507 acs_enabled = iort_enable_acs(iort_node);
1509 ops = iort_get_dev_cfg(iort_node);
1510 if (ops) {
1511 fwnode = acpi_alloc_fwnode_static();
1512 if (!fwnode)
1513 return;
1515 iort_set_fwnode(iort_node, fwnode);
1517 ret = iort_add_platform_device(iort_node, ops);
1518 if (ret) {
1519 iort_delete_fwnode(iort_node);
1520 acpi_free_fwnode_static(fwnode);
1521 return;
1525 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1526 iort_node->length);
1530 void __init acpi_iort_init(void)
1532 acpi_status status;
1534 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1535 if (ACPI_FAILURE(status)) {
1536 if (status != AE_NOT_FOUND) {
1537 const char *msg = acpi_format_exception(status);
1539 pr_err("Failed to get table, %s\n", msg);
1542 return;
1545 iort_init_platform_devices();