12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 288ef16d8STomasz Nowicki /* 388ef16d8STomasz Nowicki * Copyright (C) 2016, Semihalf 488ef16d8STomasz Nowicki * Author: Tomasz Nowicki <tn@semihalf.com> 588ef16d8STomasz Nowicki * 688ef16d8STomasz Nowicki * This file implements early detection/parsing of I/O mapping 788ef16d8STomasz Nowicki * reported to OS through firmware via I/O Remapping Table (IORT) 888ef16d8STomasz Nowicki * IORT document number: ARM DEN 0049A 988ef16d8STomasz Nowicki */ 1088ef16d8STomasz Nowicki 1188ef16d8STomasz Nowicki #define pr_fmt(fmt) "ACPI: IORT: " fmt 1288ef16d8STomasz Nowicki 1388ef16d8STomasz Nowicki #include <linux/acpi_iort.h> 14da22565dSJean-Philippe Brucker #include <linux/bitfield.h> 15846f0e9eSLorenzo Pieralisi #include <linux/iommu.h> 1688ef16d8STomasz Nowicki #include <linux/kernel.h> 177936df92SLorenzo Pieralisi #include <linux/list.h> 1888ef16d8STomasz Nowicki #include <linux/pci.h> 19846f0e9eSLorenzo Pieralisi #include <linux/platform_device.h> 207936df92SLorenzo Pieralisi #include <linux/slab.h> 210a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h> 22fcea0ccfSSudeep Holla #include "init.h" 2388ef16d8STomasz Nowicki 24ea50b524SLorenzo Pieralisi #define IORT_TYPE_MASK(type) (1 << (type)) 25ea50b524SLorenzo Pieralisi #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) 26643b8e4dSLorenzo Pieralisi #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ 27643b8e4dSLorenzo Pieralisi (1 << ACPI_IORT_NODE_SMMU_V3)) 28ea50b524SLorenzo Pieralisi 294bf2efd2STomasz Nowicki struct iort_its_msi_chip { 304bf2efd2STomasz Nowicki struct list_head list; 314bf2efd2STomasz Nowicki struct fwnode_handle *fw_node; 328b4282e6SShameer Kolothum phys_addr_t base_addr; 334bf2efd2STomasz Nowicki u32 translation_id; 344bf2efd2STomasz Nowicki }; 354bf2efd2STomasz Nowicki 367936df92SLorenzo Pieralisi struct iort_fwnode { 377936df92SLorenzo Pieralisi struct list_head list; 387936df92SLorenzo Pieralisi struct acpi_iort_node *iort_node; 397936df92SLorenzo Pieralisi struct fwnode_handle *fwnode; 407936df92SLorenzo Pieralisi }; 417936df92SLorenzo Pieralisi static LIST_HEAD(iort_fwnode_list); 427936df92SLorenzo Pieralisi static DEFINE_SPINLOCK(iort_fwnode_lock); 437936df92SLorenzo Pieralisi 447936df92SLorenzo Pieralisi /** 457936df92SLorenzo Pieralisi * iort_set_fwnode() - Create iort_fwnode and use it to register 467936df92SLorenzo Pieralisi * iommu data in the iort_fwnode_list 477936df92SLorenzo Pieralisi * 48774c4a3bSShiju Jose * @iort_node: IORT table node associated with the IOMMU 497936df92SLorenzo Pieralisi * @fwnode: fwnode associated with the IORT node 507936df92SLorenzo Pieralisi * 517936df92SLorenzo Pieralisi * Returns: 0 on success 527936df92SLorenzo Pieralisi * <0 on failure 537936df92SLorenzo Pieralisi */ 547936df92SLorenzo Pieralisi static inline int iort_set_fwnode(struct acpi_iort_node *iort_node, 557936df92SLorenzo Pieralisi struct fwnode_handle *fwnode) 567936df92SLorenzo Pieralisi { 577936df92SLorenzo Pieralisi struct iort_fwnode *np; 587936df92SLorenzo Pieralisi 597936df92SLorenzo Pieralisi np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC); 607936df92SLorenzo Pieralisi 617936df92SLorenzo Pieralisi if (WARN_ON(!np)) 627936df92SLorenzo Pieralisi return -ENOMEM; 637936df92SLorenzo Pieralisi 647936df92SLorenzo Pieralisi INIT_LIST_HEAD(&np->list); 657936df92SLorenzo Pieralisi np->iort_node = iort_node; 667936df92SLorenzo Pieralisi np->fwnode = fwnode; 677936df92SLorenzo Pieralisi 687936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 697936df92SLorenzo Pieralisi list_add_tail(&np->list, &iort_fwnode_list); 707936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 717936df92SLorenzo Pieralisi 727936df92SLorenzo Pieralisi return 0; 737936df92SLorenzo Pieralisi } 747936df92SLorenzo Pieralisi 757936df92SLorenzo Pieralisi /** 767936df92SLorenzo Pieralisi * iort_get_fwnode() - Retrieve fwnode associated with an IORT node 777936df92SLorenzo Pieralisi * 787936df92SLorenzo Pieralisi * @node: IORT table node to be looked-up 797936df92SLorenzo Pieralisi * 807936df92SLorenzo Pieralisi * Returns: fwnode_handle pointer on success, NULL on failure 817936df92SLorenzo Pieralisi */ 82e3d49392SLorenzo Pieralisi static inline struct fwnode_handle *iort_get_fwnode( 83e3d49392SLorenzo Pieralisi struct acpi_iort_node *node) 847936df92SLorenzo Pieralisi { 857936df92SLorenzo Pieralisi struct iort_fwnode *curr; 867936df92SLorenzo Pieralisi struct fwnode_handle *fwnode = NULL; 877936df92SLorenzo Pieralisi 887936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 897936df92SLorenzo Pieralisi list_for_each_entry(curr, &iort_fwnode_list, list) { 907936df92SLorenzo Pieralisi if (curr->iort_node == node) { 917936df92SLorenzo Pieralisi fwnode = curr->fwnode; 927936df92SLorenzo Pieralisi break; 937936df92SLorenzo Pieralisi } 947936df92SLorenzo Pieralisi } 957936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 967936df92SLorenzo Pieralisi 977936df92SLorenzo Pieralisi return fwnode; 987936df92SLorenzo Pieralisi } 997936df92SLorenzo Pieralisi 1007936df92SLorenzo Pieralisi /** 1017936df92SLorenzo Pieralisi * iort_delete_fwnode() - Delete fwnode associated with an IORT node 1027936df92SLorenzo Pieralisi * 1037936df92SLorenzo Pieralisi * @node: IORT table node associated with fwnode to delete 1047936df92SLorenzo Pieralisi */ 1057936df92SLorenzo Pieralisi static inline void iort_delete_fwnode(struct acpi_iort_node *node) 1067936df92SLorenzo Pieralisi { 1077936df92SLorenzo Pieralisi struct iort_fwnode *curr, *tmp; 1087936df92SLorenzo Pieralisi 1097936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 1107936df92SLorenzo Pieralisi list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) { 1117936df92SLorenzo Pieralisi if (curr->iort_node == node) { 1127936df92SLorenzo Pieralisi list_del(&curr->list); 1137936df92SLorenzo Pieralisi kfree(curr); 1147936df92SLorenzo Pieralisi break; 1157936df92SLorenzo Pieralisi } 1167936df92SLorenzo Pieralisi } 1177936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 1187936df92SLorenzo Pieralisi } 1197936df92SLorenzo Pieralisi 1200a71d8b9SHanjun Guo /** 1210a71d8b9SHanjun Guo * iort_get_iort_node() - Retrieve iort_node associated with an fwnode 1220a71d8b9SHanjun Guo * 1230a71d8b9SHanjun Guo * @fwnode: fwnode associated with device to be looked-up 1240a71d8b9SHanjun Guo * 1250a71d8b9SHanjun Guo * Returns: iort_node pointer on success, NULL on failure 1260a71d8b9SHanjun Guo */ 1270a71d8b9SHanjun Guo static inline struct acpi_iort_node *iort_get_iort_node( 1280a71d8b9SHanjun Guo struct fwnode_handle *fwnode) 1290a71d8b9SHanjun Guo { 1300a71d8b9SHanjun Guo struct iort_fwnode *curr; 1310a71d8b9SHanjun Guo struct acpi_iort_node *iort_node = NULL; 1320a71d8b9SHanjun Guo 1330a71d8b9SHanjun Guo spin_lock(&iort_fwnode_lock); 1340a71d8b9SHanjun Guo list_for_each_entry(curr, &iort_fwnode_list, list) { 1350a71d8b9SHanjun Guo if (curr->fwnode == fwnode) { 1360a71d8b9SHanjun Guo iort_node = curr->iort_node; 1370a71d8b9SHanjun Guo break; 1380a71d8b9SHanjun Guo } 1390a71d8b9SHanjun Guo } 1400a71d8b9SHanjun Guo spin_unlock(&iort_fwnode_lock); 1410a71d8b9SHanjun Guo 1420a71d8b9SHanjun Guo return iort_node; 1430a71d8b9SHanjun Guo } 1440a71d8b9SHanjun Guo 14588ef16d8STomasz Nowicki typedef acpi_status (*iort_find_node_callback) 14688ef16d8STomasz Nowicki (struct acpi_iort_node *node, void *context); 14788ef16d8STomasz Nowicki 14888ef16d8STomasz Nowicki /* Root pointer to the mapped IORT table */ 14988ef16d8STomasz Nowicki static struct acpi_table_header *iort_table; 15088ef16d8STomasz Nowicki 15188ef16d8STomasz Nowicki static LIST_HEAD(iort_msi_chip_list); 15288ef16d8STomasz Nowicki static DEFINE_SPINLOCK(iort_msi_chip_lock); 15388ef16d8STomasz Nowicki 1544bf2efd2STomasz Nowicki /** 1558b4282e6SShameer Kolothum * iort_register_domain_token() - register domain token along with related 1568b4282e6SShameer Kolothum * ITS ID and base address to the list from where we can get it back later on. 1574bf2efd2STomasz Nowicki * @trans_id: ITS ID. 1588b4282e6SShameer Kolothum * @base: ITS base address. 1594bf2efd2STomasz Nowicki * @fw_node: Domain token. 1604bf2efd2STomasz Nowicki * 1614bf2efd2STomasz Nowicki * Returns: 0 on success, -ENOMEM if no memory when allocating list element 1624bf2efd2STomasz Nowicki */ 1638b4282e6SShameer Kolothum int iort_register_domain_token(int trans_id, phys_addr_t base, 1648b4282e6SShameer Kolothum struct fwnode_handle *fw_node) 1654bf2efd2STomasz Nowicki { 1664bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip; 1674bf2efd2STomasz Nowicki 1684bf2efd2STomasz Nowicki its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL); 1694bf2efd2STomasz Nowicki if (!its_msi_chip) 1704bf2efd2STomasz Nowicki return -ENOMEM; 1714bf2efd2STomasz Nowicki 1724bf2efd2STomasz Nowicki its_msi_chip->fw_node = fw_node; 1734bf2efd2STomasz Nowicki its_msi_chip->translation_id = trans_id; 1748b4282e6SShameer Kolothum its_msi_chip->base_addr = base; 1754bf2efd2STomasz Nowicki 1764bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 1774bf2efd2STomasz Nowicki list_add(&its_msi_chip->list, &iort_msi_chip_list); 1784bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 1794bf2efd2STomasz Nowicki 1804bf2efd2STomasz Nowicki return 0; 1814bf2efd2STomasz Nowicki } 1824bf2efd2STomasz Nowicki 1834bf2efd2STomasz Nowicki /** 1844bf2efd2STomasz Nowicki * iort_deregister_domain_token() - Deregister domain token based on ITS ID 1854bf2efd2STomasz Nowicki * @trans_id: ITS ID. 1864bf2efd2STomasz Nowicki * 1874bf2efd2STomasz Nowicki * Returns: none. 1884bf2efd2STomasz Nowicki */ 1894bf2efd2STomasz Nowicki void iort_deregister_domain_token(int trans_id) 1904bf2efd2STomasz Nowicki { 1914bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip, *t; 1924bf2efd2STomasz Nowicki 1934bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 1944bf2efd2STomasz Nowicki list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { 1954bf2efd2STomasz Nowicki if (its_msi_chip->translation_id == trans_id) { 1964bf2efd2STomasz Nowicki list_del(&its_msi_chip->list); 1974bf2efd2STomasz Nowicki kfree(its_msi_chip); 1984bf2efd2STomasz Nowicki break; 1994bf2efd2STomasz Nowicki } 2004bf2efd2STomasz Nowicki } 2014bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 2024bf2efd2STomasz Nowicki } 2034bf2efd2STomasz Nowicki 2044bf2efd2STomasz Nowicki /** 2054bf2efd2STomasz Nowicki * iort_find_domain_token() - Find domain token based on given ITS ID 2064bf2efd2STomasz Nowicki * @trans_id: ITS ID. 2074bf2efd2STomasz Nowicki * 2084bf2efd2STomasz Nowicki * Returns: domain token when find on the list, NULL otherwise 2094bf2efd2STomasz Nowicki */ 2104bf2efd2STomasz Nowicki struct fwnode_handle *iort_find_domain_token(int trans_id) 2114bf2efd2STomasz Nowicki { 2124bf2efd2STomasz Nowicki struct fwnode_handle *fw_node = NULL; 2134bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip; 2144bf2efd2STomasz Nowicki 2154bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 2164bf2efd2STomasz Nowicki list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 2174bf2efd2STomasz Nowicki if (its_msi_chip->translation_id == trans_id) { 2184bf2efd2STomasz Nowicki fw_node = its_msi_chip->fw_node; 2194bf2efd2STomasz Nowicki break; 2204bf2efd2STomasz Nowicki } 2214bf2efd2STomasz Nowicki } 2224bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 2234bf2efd2STomasz Nowicki 2244bf2efd2STomasz Nowicki return fw_node; 2254bf2efd2STomasz Nowicki } 2264bf2efd2STomasz Nowicki 22788ef16d8STomasz Nowicki static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, 22888ef16d8STomasz Nowicki iort_find_node_callback callback, 22988ef16d8STomasz Nowicki void *context) 23088ef16d8STomasz Nowicki { 23188ef16d8STomasz Nowicki struct acpi_iort_node *iort_node, *iort_end; 23288ef16d8STomasz Nowicki struct acpi_table_iort *iort; 23388ef16d8STomasz Nowicki int i; 23488ef16d8STomasz Nowicki 23588ef16d8STomasz Nowicki if (!iort_table) 23688ef16d8STomasz Nowicki return NULL; 23788ef16d8STomasz Nowicki 23888ef16d8STomasz Nowicki /* Get the first IORT node */ 23988ef16d8STomasz Nowicki iort = (struct acpi_table_iort *)iort_table; 24088ef16d8STomasz Nowicki iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 24188ef16d8STomasz Nowicki iort->node_offset); 24288ef16d8STomasz Nowicki iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 24388ef16d8STomasz Nowicki iort_table->length); 24488ef16d8STomasz Nowicki 24588ef16d8STomasz Nowicki for (i = 0; i < iort->node_count; i++) { 24688ef16d8STomasz Nowicki if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 24788ef16d8STomasz Nowicki "IORT node pointer overflows, bad table!\n")) 24888ef16d8STomasz Nowicki return NULL; 24988ef16d8STomasz Nowicki 25088ef16d8STomasz Nowicki if (iort_node->type == type && 25188ef16d8STomasz Nowicki ACPI_SUCCESS(callback(iort_node, context))) 25288ef16d8STomasz Nowicki return iort_node; 25388ef16d8STomasz Nowicki 25488ef16d8STomasz Nowicki iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 25588ef16d8STomasz Nowicki iort_node->length); 25688ef16d8STomasz Nowicki } 25788ef16d8STomasz Nowicki 25888ef16d8STomasz Nowicki return NULL; 25988ef16d8STomasz Nowicki } 26088ef16d8STomasz Nowicki 26188ef16d8STomasz Nowicki static acpi_status iort_match_node_callback(struct acpi_iort_node *node, 26288ef16d8STomasz Nowicki void *context) 26388ef16d8STomasz Nowicki { 26488ef16d8STomasz Nowicki struct device *dev = context; 265c92bdfe8SHanjun Guo acpi_status status = AE_NOT_FOUND; 26688ef16d8STomasz Nowicki 26788ef16d8STomasz Nowicki if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { 26888ef16d8STomasz Nowicki struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 26907d2e59fSLorenzo Pieralisi struct acpi_device *adev; 27088ef16d8STomasz Nowicki struct acpi_iort_named_component *ncomp; 27107d2e59fSLorenzo Pieralisi struct device *nc_dev = dev; 27207d2e59fSLorenzo Pieralisi 27307d2e59fSLorenzo Pieralisi /* 27407d2e59fSLorenzo Pieralisi * Walk the device tree to find a device with an 27507d2e59fSLorenzo Pieralisi * ACPI companion; there is no point in scanning 27607d2e59fSLorenzo Pieralisi * IORT for a device matching a named component if 27707d2e59fSLorenzo Pieralisi * the device does not have an ACPI companion to 27807d2e59fSLorenzo Pieralisi * start with. 27907d2e59fSLorenzo Pieralisi */ 28007d2e59fSLorenzo Pieralisi do { 28107d2e59fSLorenzo Pieralisi adev = ACPI_COMPANION(nc_dev); 28207d2e59fSLorenzo Pieralisi if (adev) 28307d2e59fSLorenzo Pieralisi break; 28407d2e59fSLorenzo Pieralisi 28507d2e59fSLorenzo Pieralisi nc_dev = nc_dev->parent; 28607d2e59fSLorenzo Pieralisi } while (nc_dev); 28788ef16d8STomasz Nowicki 288c92bdfe8SHanjun Guo if (!adev) 28988ef16d8STomasz Nowicki goto out; 29088ef16d8STomasz Nowicki 29188ef16d8STomasz Nowicki status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); 29288ef16d8STomasz Nowicki if (ACPI_FAILURE(status)) { 29307d2e59fSLorenzo Pieralisi dev_warn(nc_dev, "Can't get device full path name\n"); 29488ef16d8STomasz Nowicki goto out; 29588ef16d8STomasz Nowicki } 29688ef16d8STomasz Nowicki 29788ef16d8STomasz Nowicki ncomp = (struct acpi_iort_named_component *)node->node_data; 29888ef16d8STomasz Nowicki status = !strcmp(ncomp->device_name, buf.pointer) ? 29988ef16d8STomasz Nowicki AE_OK : AE_NOT_FOUND; 30088ef16d8STomasz Nowicki acpi_os_free(buf.pointer); 30188ef16d8STomasz Nowicki } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 30288ef16d8STomasz Nowicki struct acpi_iort_root_complex *pci_rc; 30388ef16d8STomasz Nowicki struct pci_bus *bus; 30488ef16d8STomasz Nowicki 30588ef16d8STomasz Nowicki bus = to_pci_bus(dev); 30688ef16d8STomasz Nowicki pci_rc = (struct acpi_iort_root_complex *)node->node_data; 30788ef16d8STomasz Nowicki 30888ef16d8STomasz Nowicki /* 30988ef16d8STomasz Nowicki * It is assumed that PCI segment numbers maps one-to-one 31088ef16d8STomasz Nowicki * with root complexes. Each segment number can represent only 31188ef16d8STomasz Nowicki * one root complex. 31288ef16d8STomasz Nowicki */ 31388ef16d8STomasz Nowicki status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? 31488ef16d8STomasz Nowicki AE_OK : AE_NOT_FOUND; 31588ef16d8STomasz Nowicki } 31688ef16d8STomasz Nowicki out: 31788ef16d8STomasz Nowicki return status; 31888ef16d8STomasz Nowicki } 31988ef16d8STomasz Nowicki 32088ef16d8STomasz Nowicki static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, 321539979b6SArd Biesheuvel u32 *rid_out, bool check_overlap) 32288ef16d8STomasz Nowicki { 32388ef16d8STomasz Nowicki /* Single mapping does not care for input id */ 32488ef16d8STomasz Nowicki if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 32588ef16d8STomasz Nowicki if (type == ACPI_IORT_NODE_NAMED_COMPONENT || 32688ef16d8STomasz Nowicki type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 32788ef16d8STomasz Nowicki *rid_out = map->output_base; 32888ef16d8STomasz Nowicki return 0; 32988ef16d8STomasz Nowicki } 33088ef16d8STomasz Nowicki 33188ef16d8STomasz Nowicki pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", 33288ef16d8STomasz Nowicki map, type); 33388ef16d8STomasz Nowicki return -ENXIO; 33488ef16d8STomasz Nowicki } 33588ef16d8STomasz Nowicki 3366d3b29d0SArd Biesheuvel if (rid_in < map->input_base || 337539979b6SArd Biesheuvel (rid_in > map->input_base + map->id_count)) 33888ef16d8STomasz Nowicki return -ENXIO; 33988ef16d8STomasz Nowicki 340539979b6SArd Biesheuvel if (check_overlap) { 341539979b6SArd Biesheuvel /* 342539979b6SArd Biesheuvel * We already found a mapping for this input ID at the end of 343539979b6SArd Biesheuvel * another region. If it coincides with the start of this 344539979b6SArd Biesheuvel * region, we assume the prior match was due to the off-by-1 345539979b6SArd Biesheuvel * issue mentioned below, and allow it to be superseded. 346539979b6SArd Biesheuvel * Otherwise, things are *really* broken, and we just disregard 347539979b6SArd Biesheuvel * duplicate matches entirely to retain compatibility. 348539979b6SArd Biesheuvel */ 349539979b6SArd Biesheuvel pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n", 350539979b6SArd Biesheuvel map, rid_in); 351539979b6SArd Biesheuvel if (rid_in != map->input_base) 352539979b6SArd Biesheuvel return -ENXIO; 35344cdc7b1SHanjun Guo 35444cdc7b1SHanjun Guo pr_err(FW_BUG "applying workaround.\n"); 355539979b6SArd Biesheuvel } 356539979b6SArd Biesheuvel 35788ef16d8STomasz Nowicki *rid_out = map->output_base + (rid_in - map->input_base); 358539979b6SArd Biesheuvel 359539979b6SArd Biesheuvel /* 360539979b6SArd Biesheuvel * Due to confusion regarding the meaning of the id_count field (which 361539979b6SArd Biesheuvel * carries the number of IDs *minus 1*), we may have to disregard this 362539979b6SArd Biesheuvel * match if it is at the end of the range, and overlaps with the start 363539979b6SArd Biesheuvel * of another one. 364539979b6SArd Biesheuvel */ 365539979b6SArd Biesheuvel if (map->id_count > 0 && rid_in == map->input_base + map->id_count) 366539979b6SArd Biesheuvel return -EAGAIN; 36788ef16d8STomasz Nowicki return 0; 36888ef16d8STomasz Nowicki } 36988ef16d8STomasz Nowicki 370e3d49392SLorenzo Pieralisi static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, 3718ca4f1d3SHanjun Guo u32 *id_out, int index) 372618f535aSLorenzo Pieralisi { 373618f535aSLorenzo Pieralisi struct acpi_iort_node *parent; 374618f535aSLorenzo Pieralisi struct acpi_iort_id_mapping *map; 375618f535aSLorenzo Pieralisi 376618f535aSLorenzo Pieralisi if (!node->mapping_offset || !node->mapping_count || 377618f535aSLorenzo Pieralisi index >= node->mapping_count) 378618f535aSLorenzo Pieralisi return NULL; 379618f535aSLorenzo Pieralisi 380618f535aSLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 381030abd8aSLorenzo Pieralisi node->mapping_offset + index * sizeof(*map)); 382618f535aSLorenzo Pieralisi 383618f535aSLorenzo Pieralisi /* Firmware bug! */ 384618f535aSLorenzo Pieralisi if (!map->output_reference) { 385618f535aSLorenzo Pieralisi pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 386618f535aSLorenzo Pieralisi node, node->type); 387618f535aSLorenzo Pieralisi return NULL; 388618f535aSLorenzo Pieralisi } 389618f535aSLorenzo Pieralisi 390618f535aSLorenzo Pieralisi parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 391618f535aSLorenzo Pieralisi map->output_reference); 392618f535aSLorenzo Pieralisi 393030abd8aSLorenzo Pieralisi if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 394618f535aSLorenzo Pieralisi if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || 39586456a3fSHanjun Guo node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || 39624e51604SNeil Leeder node->type == ACPI_IORT_NODE_SMMU_V3 || 39724e51604SNeil Leeder node->type == ACPI_IORT_NODE_PMCG) { 398030abd8aSLorenzo Pieralisi *id_out = map->output_base; 399618f535aSLorenzo Pieralisi return parent; 400618f535aSLorenzo Pieralisi } 401618f535aSLorenzo Pieralisi } 402618f535aSLorenzo Pieralisi 403618f535aSLorenzo Pieralisi return NULL; 404618f535aSLorenzo Pieralisi } 405618f535aSLorenzo Pieralisi 40605da178cSRobin Murphy #ifndef ACPI_IORT_SMMU_V3_DEVICEID_VALID 40705da178cSRobin Murphy #define ACPI_IORT_SMMU_V3_DEVICEID_VALID (1 << 4) 40805da178cSRobin Murphy #endif 40905da178cSRobin Murphy 41086456a3fSHanjun Guo static int iort_get_id_mapping_index(struct acpi_iort_node *node) 41186456a3fSHanjun Guo { 41286456a3fSHanjun Guo struct acpi_iort_smmu_v3 *smmu; 41350c8ab8dSTuan Phan struct acpi_iort_pmcg *pmcg; 41486456a3fSHanjun Guo 41586456a3fSHanjun Guo switch (node->type) { 41686456a3fSHanjun Guo case ACPI_IORT_NODE_SMMU_V3: 41786456a3fSHanjun Guo /* 41886456a3fSHanjun Guo * SMMUv3 dev ID mapping index was introduced in revision 1 41986456a3fSHanjun Guo * table, not available in revision 0 42086456a3fSHanjun Guo */ 42186456a3fSHanjun Guo if (node->revision < 1) 42286456a3fSHanjun Guo return -EINVAL; 42386456a3fSHanjun Guo 42486456a3fSHanjun Guo smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 42586456a3fSHanjun Guo /* 42605da178cSRobin Murphy * Until IORT E.e (node rev. 5), the ID mapping index was 42705da178cSRobin Murphy * defined to be valid unless all interrupts are GSIV-based. 42886456a3fSHanjun Guo */ 42905da178cSRobin Murphy if (node->revision < 5) { 43005da178cSRobin Murphy if (smmu->event_gsiv && smmu->pri_gsiv && 43105da178cSRobin Murphy smmu->gerr_gsiv && smmu->sync_gsiv) 43286456a3fSHanjun Guo return -EINVAL; 43305da178cSRobin Murphy } else if (!(smmu->flags & ACPI_IORT_SMMU_V3_DEVICEID_VALID)) { 43405da178cSRobin Murphy return -EINVAL; 43505da178cSRobin Murphy } 43686456a3fSHanjun Guo 43786456a3fSHanjun Guo if (smmu->id_mapping_index >= node->mapping_count) { 43886456a3fSHanjun Guo pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", 43986456a3fSHanjun Guo node, node->type); 44086456a3fSHanjun Guo return -EINVAL; 44186456a3fSHanjun Guo } 44286456a3fSHanjun Guo 44386456a3fSHanjun Guo return smmu->id_mapping_index; 44424e51604SNeil Leeder case ACPI_IORT_NODE_PMCG: 44550c8ab8dSTuan Phan pmcg = (struct acpi_iort_pmcg *)node->node_data; 44650c8ab8dSTuan Phan if (pmcg->overflow_gsiv || node->mapping_count == 0) 44750c8ab8dSTuan Phan return -EINVAL; 44850c8ab8dSTuan Phan 44924e51604SNeil Leeder return 0; 45086456a3fSHanjun Guo default: 45186456a3fSHanjun Guo return -EINVAL; 45286456a3fSHanjun Guo } 45386456a3fSHanjun Guo } 4548c8df8dcSHanjun Guo 455697f6093SHanjun Guo static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, 456697f6093SHanjun Guo u32 id_in, u32 *id_out, 457ea50b524SLorenzo Pieralisi u8 type_mask) 45888ef16d8STomasz Nowicki { 459697f6093SHanjun Guo u32 id = id_in; 46088ef16d8STomasz Nowicki 46188ef16d8STomasz Nowicki /* Parse the ID mapping tree to find specified node type */ 46288ef16d8STomasz Nowicki while (node) { 46388ef16d8STomasz Nowicki struct acpi_iort_id_mapping *map; 464539979b6SArd Biesheuvel int i, index, rc = 0; 465539979b6SArd Biesheuvel u32 out_ref = 0, map_id = id; 46688ef16d8STomasz Nowicki 467ea50b524SLorenzo Pieralisi if (IORT_TYPE_MASK(node->type) & type_mask) { 468697f6093SHanjun Guo if (id_out) 469697f6093SHanjun Guo *id_out = id; 47088ef16d8STomasz Nowicki return node; 47188ef16d8STomasz Nowicki } 47288ef16d8STomasz Nowicki 47388ef16d8STomasz Nowicki if (!node->mapping_offset || !node->mapping_count) 47488ef16d8STomasz Nowicki goto fail_map; 47588ef16d8STomasz Nowicki 47688ef16d8STomasz Nowicki map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 47788ef16d8STomasz Nowicki node->mapping_offset); 47888ef16d8STomasz Nowicki 47988ef16d8STomasz Nowicki /* Firmware bug! */ 48088ef16d8STomasz Nowicki if (!map->output_reference) { 48188ef16d8STomasz Nowicki pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 48288ef16d8STomasz Nowicki node, node->type); 48388ef16d8STomasz Nowicki goto fail_map; 48488ef16d8STomasz Nowicki } 48588ef16d8STomasz Nowicki 4868c8df8dcSHanjun Guo /* 4878c8df8dcSHanjun Guo * Get the special ID mapping index (if any) and skip its 4888c8df8dcSHanjun Guo * associated ID map to prevent erroneous multi-stage 4898c8df8dcSHanjun Guo * IORT ID translations. 4908c8df8dcSHanjun Guo */ 4918c8df8dcSHanjun Guo index = iort_get_id_mapping_index(node); 4928c8df8dcSHanjun Guo 493697f6093SHanjun Guo /* Do the ID translation */ 49488ef16d8STomasz Nowicki for (i = 0; i < node->mapping_count; i++, map++) { 4958c8df8dcSHanjun Guo /* if it is special mapping index, skip it */ 4968c8df8dcSHanjun Guo if (i == index) 4978c8df8dcSHanjun Guo continue; 4988c8df8dcSHanjun Guo 499539979b6SArd Biesheuvel rc = iort_id_map(map, node->type, map_id, &id, out_ref); 500539979b6SArd Biesheuvel if (!rc) 50188ef16d8STomasz Nowicki break; 502539979b6SArd Biesheuvel if (rc == -EAGAIN) 503539979b6SArd Biesheuvel out_ref = map->output_reference; 50488ef16d8STomasz Nowicki } 50588ef16d8STomasz Nowicki 506539979b6SArd Biesheuvel if (i == node->mapping_count && !out_ref) 50788ef16d8STomasz Nowicki goto fail_map; 50888ef16d8STomasz Nowicki 50988ef16d8STomasz Nowicki node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 510539979b6SArd Biesheuvel rc ? out_ref : map->output_reference); 51188ef16d8STomasz Nowicki } 51288ef16d8STomasz Nowicki 51388ef16d8STomasz Nowicki fail_map: 514697f6093SHanjun Guo /* Map input ID to output ID unchanged on mapping failure */ 515697f6093SHanjun Guo if (id_out) 516697f6093SHanjun Guo *id_out = id_in; 51788ef16d8STomasz Nowicki 51888ef16d8STomasz Nowicki return NULL; 51988ef16d8STomasz Nowicki } 52088ef16d8STomasz Nowicki 521e3d49392SLorenzo Pieralisi static struct acpi_iort_node *iort_node_map_platform_id( 522e3d49392SLorenzo Pieralisi struct acpi_iort_node *node, u32 *id_out, u8 type_mask, 5238ca4f1d3SHanjun Guo int index) 5248ca4f1d3SHanjun Guo { 5258ca4f1d3SHanjun Guo struct acpi_iort_node *parent; 5268ca4f1d3SHanjun Guo u32 id; 5278ca4f1d3SHanjun Guo 5288ca4f1d3SHanjun Guo /* step 1: retrieve the initial dev id */ 5298ca4f1d3SHanjun Guo parent = iort_node_get_id(node, &id, index); 5308ca4f1d3SHanjun Guo if (!parent) 5318ca4f1d3SHanjun Guo return NULL; 5328ca4f1d3SHanjun Guo 5338ca4f1d3SHanjun Guo /* 5348ca4f1d3SHanjun Guo * optional step 2: map the initial dev id if its parent is not 5358ca4f1d3SHanjun Guo * the target type we want, map it again for the use cases such 5368ca4f1d3SHanjun Guo * as NC (named component) -> SMMU -> ITS. If the type is matched, 5378ca4f1d3SHanjun Guo * return the initial dev id and its parent pointer directly. 5388ca4f1d3SHanjun Guo */ 5398ca4f1d3SHanjun Guo if (!(IORT_TYPE_MASK(parent->type) & type_mask)) 5408ca4f1d3SHanjun Guo parent = iort_node_map_id(parent, id, id_out, type_mask); 5418ca4f1d3SHanjun Guo else 5428ca4f1d3SHanjun Guo if (id_out) 5438ca4f1d3SHanjun Guo *id_out = id; 5448ca4f1d3SHanjun Guo 5458ca4f1d3SHanjun Guo return parent; 5468ca4f1d3SHanjun Guo } 5478ca4f1d3SHanjun Guo 54888ef16d8STomasz Nowicki static struct acpi_iort_node *iort_find_dev_node(struct device *dev) 54988ef16d8STomasz Nowicki { 55088ef16d8STomasz Nowicki struct pci_bus *pbus; 55188ef16d8STomasz Nowicki 5520a71d8b9SHanjun Guo if (!dev_is_pci(dev)) { 5530a71d8b9SHanjun Guo struct acpi_iort_node *node; 5540a71d8b9SHanjun Guo /* 5550a71d8b9SHanjun Guo * scan iort_fwnode_list to see if it's an iort platform 5560a71d8b9SHanjun Guo * device (such as SMMU, PMCG),its iort node already cached 5570a71d8b9SHanjun Guo * and associated with fwnode when iort platform devices 5580a71d8b9SHanjun Guo * were initialized. 5590a71d8b9SHanjun Guo */ 5600a71d8b9SHanjun Guo node = iort_get_iort_node(dev->fwnode); 5610a71d8b9SHanjun Guo if (node) 5620a71d8b9SHanjun Guo return node; 5630a71d8b9SHanjun Guo /* 5640a71d8b9SHanjun Guo * if not, then it should be a platform device defined in 5650a71d8b9SHanjun Guo * DSDT/SSDT (with Named Component node in IORT) 5660a71d8b9SHanjun Guo */ 56788ef16d8STomasz Nowicki return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 56888ef16d8STomasz Nowicki iort_match_node_callback, dev); 5690a71d8b9SHanjun Guo } 57088ef16d8STomasz Nowicki 57188ef16d8STomasz Nowicki pbus = to_pci_dev(dev)->bus; 57288ef16d8STomasz Nowicki 57388ef16d8STomasz Nowicki return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 57488ef16d8STomasz Nowicki iort_match_node_callback, &pbus->dev); 57588ef16d8STomasz Nowicki } 57688ef16d8STomasz Nowicki 5774bf2efd2STomasz Nowicki /** 57839c3cf56SLorenzo Pieralisi * iort_msi_map_id() - Map a MSI input ID for a device 5794bf2efd2STomasz Nowicki * @dev: The device for which the mapping is to be done. 58039c3cf56SLorenzo Pieralisi * @input_id: The device input ID. 5814bf2efd2STomasz Nowicki * 58239c3cf56SLorenzo Pieralisi * Returns: mapped MSI ID on success, input ID otherwise 5834bf2efd2STomasz Nowicki */ 58439c3cf56SLorenzo Pieralisi u32 iort_msi_map_id(struct device *dev, u32 input_id) 5854bf2efd2STomasz Nowicki { 5864bf2efd2STomasz Nowicki struct acpi_iort_node *node; 5874bf2efd2STomasz Nowicki u32 dev_id; 5884bf2efd2STomasz Nowicki 5894bf2efd2STomasz Nowicki node = iort_find_dev_node(dev); 5904bf2efd2STomasz Nowicki if (!node) 59139c3cf56SLorenzo Pieralisi return input_id; 5924bf2efd2STomasz Nowicki 59339c3cf56SLorenzo Pieralisi iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); 5944bf2efd2STomasz Nowicki return dev_id; 5954bf2efd2STomasz Nowicki } 5964bf2efd2STomasz Nowicki 5974bf2efd2STomasz Nowicki /** 598ae7c1838SHanjun Guo * iort_pmsi_get_dev_id() - Get the device id for a device 599ae7c1838SHanjun Guo * @dev: The device for which the mapping is to be done. 600ae7c1838SHanjun Guo * @dev_id: The device ID found. 601ae7c1838SHanjun Guo * 602ae7c1838SHanjun Guo * Returns: 0 for successful find a dev id, -ENODEV on error 603ae7c1838SHanjun Guo */ 604ae7c1838SHanjun Guo int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) 605ae7c1838SHanjun Guo { 6068c8df8dcSHanjun Guo int i, index; 607ae7c1838SHanjun Guo struct acpi_iort_node *node; 608ae7c1838SHanjun Guo 609ae7c1838SHanjun Guo node = iort_find_dev_node(dev); 610ae7c1838SHanjun Guo if (!node) 611ae7c1838SHanjun Guo return -ENODEV; 612ae7c1838SHanjun Guo 6138c8df8dcSHanjun Guo index = iort_get_id_mapping_index(node); 6148c8df8dcSHanjun Guo /* if there is a valid index, go get the dev_id directly */ 6158c8df8dcSHanjun Guo if (index >= 0) { 6168c8df8dcSHanjun Guo if (iort_node_get_id(node, dev_id, index)) 617ae7c1838SHanjun Guo return 0; 6188c8df8dcSHanjun Guo } else { 6198c8df8dcSHanjun Guo for (i = 0; i < node->mapping_count; i++) { 6208c8df8dcSHanjun Guo if (iort_node_map_platform_id(node, dev_id, 6218c8df8dcSHanjun Guo IORT_MSI_TYPE, i)) 6228c8df8dcSHanjun Guo return 0; 6238c8df8dcSHanjun Guo } 624ae7c1838SHanjun Guo } 625ae7c1838SHanjun Guo 626ae7c1838SHanjun Guo return -ENODEV; 627ae7c1838SHanjun Guo } 628ae7c1838SHanjun Guo 6298b4282e6SShameer Kolothum static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) 6308b4282e6SShameer Kolothum { 6318b4282e6SShameer Kolothum struct iort_its_msi_chip *its_msi_chip; 6328b4282e6SShameer Kolothum int ret = -ENODEV; 6338b4282e6SShameer Kolothum 6348b4282e6SShameer Kolothum spin_lock(&iort_msi_chip_lock); 6358b4282e6SShameer Kolothum list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 6368b4282e6SShameer Kolothum if (its_msi_chip->translation_id == its_id) { 6378b4282e6SShameer Kolothum *base = its_msi_chip->base_addr; 6388b4282e6SShameer Kolothum ret = 0; 6398b4282e6SShameer Kolothum break; 6408b4282e6SShameer Kolothum } 6418b4282e6SShameer Kolothum } 6428b4282e6SShameer Kolothum spin_unlock(&iort_msi_chip_lock); 6438b4282e6SShameer Kolothum 6448b4282e6SShameer Kolothum return ret; 6458b4282e6SShameer Kolothum } 6468b4282e6SShameer Kolothum 647ae7c1838SHanjun Guo /** 6484bf2efd2STomasz Nowicki * iort_dev_find_its_id() - Find the ITS identifier for a device 6494bf2efd2STomasz Nowicki * @dev: The device. 650d1718a1bSLorenzo Pieralisi * @id: Device's ID 6514bf2efd2STomasz Nowicki * @idx: Index of the ITS identifier list. 6524bf2efd2STomasz Nowicki * @its_id: ITS identifier. 6534bf2efd2STomasz Nowicki * 6544bf2efd2STomasz Nowicki * Returns: 0 on success, appropriate error value otherwise 6554bf2efd2STomasz Nowicki */ 656d1718a1bSLorenzo Pieralisi static int iort_dev_find_its_id(struct device *dev, u32 id, 6574bf2efd2STomasz Nowicki unsigned int idx, int *its_id) 6584bf2efd2STomasz Nowicki { 6594bf2efd2STomasz Nowicki struct acpi_iort_its_group *its; 6604bf2efd2STomasz Nowicki struct acpi_iort_node *node; 6614bf2efd2STomasz Nowicki 6624bf2efd2STomasz Nowicki node = iort_find_dev_node(dev); 6634bf2efd2STomasz Nowicki if (!node) 6644bf2efd2STomasz Nowicki return -ENXIO; 6654bf2efd2STomasz Nowicki 666d1718a1bSLorenzo Pieralisi node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE); 6674bf2efd2STomasz Nowicki if (!node) 6684bf2efd2STomasz Nowicki return -ENXIO; 6694bf2efd2STomasz Nowicki 6704bf2efd2STomasz Nowicki /* Move to ITS specific data */ 6714bf2efd2STomasz Nowicki its = (struct acpi_iort_its_group *)node->node_data; 6725a46d3f7SLorenzo Pieralisi if (idx >= its->its_count) { 6735a46d3f7SLorenzo Pieralisi dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n", 6744bf2efd2STomasz Nowicki idx, its->its_count); 6754bf2efd2STomasz Nowicki return -ENXIO; 6764bf2efd2STomasz Nowicki } 6774bf2efd2STomasz Nowicki 6784bf2efd2STomasz Nowicki *its_id = its->identifiers[idx]; 6794bf2efd2STomasz Nowicki return 0; 6804bf2efd2STomasz Nowicki } 6814bf2efd2STomasz Nowicki 6824bf2efd2STomasz Nowicki /** 6834bf2efd2STomasz Nowicki * iort_get_device_domain() - Find MSI domain related to a device 6844bf2efd2STomasz Nowicki * @dev: The device. 685774c4a3bSShiju Jose * @id: Requester ID for the device. 686774c4a3bSShiju Jose * @bus_token: irq domain bus token. 6874bf2efd2STomasz Nowicki * 6884bf2efd2STomasz Nowicki * Returns: the MSI domain for this device, NULL otherwise 6894bf2efd2STomasz Nowicki */ 690d1718a1bSLorenzo Pieralisi struct irq_domain *iort_get_device_domain(struct device *dev, u32 id, 691d1718a1bSLorenzo Pieralisi enum irq_domain_bus_token bus_token) 6924bf2efd2STomasz Nowicki { 6934bf2efd2STomasz Nowicki struct fwnode_handle *handle; 6944bf2efd2STomasz Nowicki int its_id; 6954bf2efd2STomasz Nowicki 696d1718a1bSLorenzo Pieralisi if (iort_dev_find_its_id(dev, id, 0, &its_id)) 6974bf2efd2STomasz Nowicki return NULL; 6984bf2efd2STomasz Nowicki 6994bf2efd2STomasz Nowicki handle = iort_find_domain_token(its_id); 7004bf2efd2STomasz Nowicki if (!handle) 7014bf2efd2STomasz Nowicki return NULL; 7024bf2efd2STomasz Nowicki 703d1718a1bSLorenzo Pieralisi return irq_find_matching_fwnode(handle, bus_token); 7044bf2efd2STomasz Nowicki } 7054bf2efd2STomasz Nowicki 70665637901SLorenzo Pieralisi static void iort_set_device_domain(struct device *dev, 70765637901SLorenzo Pieralisi struct acpi_iort_node *node) 70865637901SLorenzo Pieralisi { 70965637901SLorenzo Pieralisi struct acpi_iort_its_group *its; 71065637901SLorenzo Pieralisi struct acpi_iort_node *msi_parent; 71165637901SLorenzo Pieralisi struct acpi_iort_id_mapping *map; 71265637901SLorenzo Pieralisi struct fwnode_handle *iort_fwnode; 71365637901SLorenzo Pieralisi struct irq_domain *domain; 71465637901SLorenzo Pieralisi int index; 71565637901SLorenzo Pieralisi 71665637901SLorenzo Pieralisi index = iort_get_id_mapping_index(node); 71765637901SLorenzo Pieralisi if (index < 0) 71865637901SLorenzo Pieralisi return; 71965637901SLorenzo Pieralisi 72065637901SLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 72165637901SLorenzo Pieralisi node->mapping_offset + index * sizeof(*map)); 72265637901SLorenzo Pieralisi 72365637901SLorenzo Pieralisi /* Firmware bug! */ 72465637901SLorenzo Pieralisi if (!map->output_reference || 72565637901SLorenzo Pieralisi !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { 72665637901SLorenzo Pieralisi pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", 72765637901SLorenzo Pieralisi node, node->type); 72865637901SLorenzo Pieralisi return; 72965637901SLorenzo Pieralisi } 73065637901SLorenzo Pieralisi 73165637901SLorenzo Pieralisi msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 73265637901SLorenzo Pieralisi map->output_reference); 73365637901SLorenzo Pieralisi 73465637901SLorenzo Pieralisi if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) 73565637901SLorenzo Pieralisi return; 73665637901SLorenzo Pieralisi 73765637901SLorenzo Pieralisi /* Move to ITS specific data */ 73865637901SLorenzo Pieralisi its = (struct acpi_iort_its_group *)msi_parent->node_data; 73965637901SLorenzo Pieralisi 74065637901SLorenzo Pieralisi iort_fwnode = iort_find_domain_token(its->identifiers[0]); 74165637901SLorenzo Pieralisi if (!iort_fwnode) 74265637901SLorenzo Pieralisi return; 74365637901SLorenzo Pieralisi 74465637901SLorenzo Pieralisi domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 74565637901SLorenzo Pieralisi if (domain) 74665637901SLorenzo Pieralisi dev_set_msi_domain(dev, domain); 74765637901SLorenzo Pieralisi } 74865637901SLorenzo Pieralisi 749d4f54a18SHanjun Guo /** 750d4f54a18SHanjun Guo * iort_get_platform_device_domain() - Find MSI domain related to a 751d4f54a18SHanjun Guo * platform device 752d4f54a18SHanjun Guo * @dev: the dev pointer associated with the platform device 753d4f54a18SHanjun Guo * 754d4f54a18SHanjun Guo * Returns: the MSI domain for this device, NULL otherwise 755d4f54a18SHanjun Guo */ 756d4f54a18SHanjun Guo static struct irq_domain *iort_get_platform_device_domain(struct device *dev) 757d4f54a18SHanjun Guo { 758ea2412dcSLorenzo Pieralisi struct acpi_iort_node *node, *msi_parent = NULL; 759d4f54a18SHanjun Guo struct fwnode_handle *iort_fwnode; 760d4f54a18SHanjun Guo struct acpi_iort_its_group *its; 761d4f54a18SHanjun Guo int i; 762d4f54a18SHanjun Guo 763d4f54a18SHanjun Guo /* find its associated iort node */ 764d4f54a18SHanjun Guo node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 765d4f54a18SHanjun Guo iort_match_node_callback, dev); 766d4f54a18SHanjun Guo if (!node) 767d4f54a18SHanjun Guo return NULL; 768d4f54a18SHanjun Guo 769d4f54a18SHanjun Guo /* then find its msi parent node */ 770d4f54a18SHanjun Guo for (i = 0; i < node->mapping_count; i++) { 771d4f54a18SHanjun Guo msi_parent = iort_node_map_platform_id(node, NULL, 772d4f54a18SHanjun Guo IORT_MSI_TYPE, i); 773d4f54a18SHanjun Guo if (msi_parent) 774d4f54a18SHanjun Guo break; 775d4f54a18SHanjun Guo } 776d4f54a18SHanjun Guo 777d4f54a18SHanjun Guo if (!msi_parent) 778d4f54a18SHanjun Guo return NULL; 779d4f54a18SHanjun Guo 780d4f54a18SHanjun Guo /* Move to ITS specific data */ 781d4f54a18SHanjun Guo its = (struct acpi_iort_its_group *)msi_parent->node_data; 782d4f54a18SHanjun Guo 783d4f54a18SHanjun Guo iort_fwnode = iort_find_domain_token(its->identifiers[0]); 784d4f54a18SHanjun Guo if (!iort_fwnode) 785d4f54a18SHanjun Guo return NULL; 786d4f54a18SHanjun Guo 787d4f54a18SHanjun Guo return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 788d4f54a18SHanjun Guo } 789d4f54a18SHanjun Guo 790d4f54a18SHanjun Guo void acpi_configure_pmsi_domain(struct device *dev) 791d4f54a18SHanjun Guo { 792d4f54a18SHanjun Guo struct irq_domain *msi_domain; 793d4f54a18SHanjun Guo 794d4f54a18SHanjun Guo msi_domain = iort_get_platform_device_domain(dev); 795d4f54a18SHanjun Guo if (msi_domain) 796d4f54a18SHanjun Guo dev_set_msi_domain(dev, msi_domain); 797d4f54a18SHanjun Guo } 798d4f54a18SHanjun Guo 799d49f2dedSLorenzo Pieralisi #ifdef CONFIG_IOMMU_API 800491cf4a6SShameer Kolothum static void iort_rmr_free(struct device *dev, 801491cf4a6SShameer Kolothum struct iommu_resv_region *region) 802491cf4a6SShameer Kolothum { 803491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 804491cf4a6SShameer Kolothum 805491cf4a6SShameer Kolothum rmr_data = container_of(region, struct iommu_iort_rmr_data, rr); 806491cf4a6SShameer Kolothum kfree(rmr_data->sids); 807491cf4a6SShameer Kolothum kfree(rmr_data); 808491cf4a6SShameer Kolothum } 809491cf4a6SShameer Kolothum 810491cf4a6SShameer Kolothum static struct iommu_iort_rmr_data *iort_rmr_alloc( 811491cf4a6SShameer Kolothum struct acpi_iort_rmr_desc *rmr_desc, 812491cf4a6SShameer Kolothum int prot, enum iommu_resv_type type, 813491cf4a6SShameer Kolothum u32 *sids, u32 num_sids) 814491cf4a6SShameer Kolothum { 815491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 816491cf4a6SShameer Kolothum struct iommu_resv_region *region; 817491cf4a6SShameer Kolothum u32 *sids_copy; 818491cf4a6SShameer Kolothum u64 addr = rmr_desc->base_address, size = rmr_desc->length; 819491cf4a6SShameer Kolothum 820491cf4a6SShameer Kolothum rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL); 821491cf4a6SShameer Kolothum if (!rmr_data) 822491cf4a6SShameer Kolothum return NULL; 823491cf4a6SShameer Kolothum 824491cf4a6SShameer Kolothum /* Create a copy of SIDs array to associate with this rmr_data */ 825491cf4a6SShameer Kolothum sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL); 826491cf4a6SShameer Kolothum if (!sids_copy) { 827491cf4a6SShameer Kolothum kfree(rmr_data); 828491cf4a6SShameer Kolothum return NULL; 829491cf4a6SShameer Kolothum } 830491cf4a6SShameer Kolothum rmr_data->sids = sids_copy; 831491cf4a6SShameer Kolothum rmr_data->num_sids = num_sids; 832491cf4a6SShameer Kolothum 833491cf4a6SShameer Kolothum if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) { 834491cf4a6SShameer Kolothum /* PAGE align base addr and size */ 835491cf4a6SShameer Kolothum addr &= PAGE_MASK; 836491cf4a6SShameer Kolothum size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address)); 837491cf4a6SShameer Kolothum 838491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n", 839491cf4a6SShameer Kolothum rmr_desc->base_address, 840491cf4a6SShameer Kolothum rmr_desc->base_address + rmr_desc->length - 1, 841491cf4a6SShameer Kolothum addr, addr + size - 1); 842491cf4a6SShameer Kolothum } 843491cf4a6SShameer Kolothum 844491cf4a6SShameer Kolothum region = &rmr_data->rr; 845491cf4a6SShameer Kolothum INIT_LIST_HEAD(®ion->list); 846491cf4a6SShameer Kolothum region->start = addr; 847491cf4a6SShameer Kolothum region->length = size; 848491cf4a6SShameer Kolothum region->prot = prot; 849491cf4a6SShameer Kolothum region->type = type; 850491cf4a6SShameer Kolothum region->free = iort_rmr_free; 851491cf4a6SShameer Kolothum 852491cf4a6SShameer Kolothum return rmr_data; 853491cf4a6SShameer Kolothum } 854491cf4a6SShameer Kolothum 855491cf4a6SShameer Kolothum static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc, 856491cf4a6SShameer Kolothum u32 count) 857491cf4a6SShameer Kolothum { 858491cf4a6SShameer Kolothum int i, j; 859491cf4a6SShameer Kolothum 860491cf4a6SShameer Kolothum for (i = 0; i < count; i++) { 861491cf4a6SShameer Kolothum u64 end, start = desc[i].base_address, length = desc[i].length; 862491cf4a6SShameer Kolothum 863491cf4a6SShameer Kolothum if (!length) { 864491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n", 865491cf4a6SShameer Kolothum start); 866491cf4a6SShameer Kolothum continue; 867491cf4a6SShameer Kolothum } 868491cf4a6SShameer Kolothum 869491cf4a6SShameer Kolothum end = start + length - 1; 870491cf4a6SShameer Kolothum 871491cf4a6SShameer Kolothum /* Check for address overlap */ 872491cf4a6SShameer Kolothum for (j = i + 1; j < count; j++) { 873491cf4a6SShameer Kolothum u64 e_start = desc[j].base_address; 874491cf4a6SShameer Kolothum u64 e_end = e_start + desc[j].length - 1; 875491cf4a6SShameer Kolothum 876491cf4a6SShameer Kolothum if (start <= e_end && end >= e_start) 877491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n", 878491cf4a6SShameer Kolothum start, end); 879491cf4a6SShameer Kolothum } 880491cf4a6SShameer Kolothum } 881491cf4a6SShameer Kolothum } 882491cf4a6SShameer Kolothum 883491cf4a6SShameer Kolothum /* 884491cf4a6SShameer Kolothum * Please note, we will keep the already allocated RMR reserve 885491cf4a6SShameer Kolothum * regions in case of a memory allocation failure. 886491cf4a6SShameer Kolothum */ 887491cf4a6SShameer Kolothum static void iort_get_rmrs(struct acpi_iort_node *node, 888491cf4a6SShameer Kolothum struct acpi_iort_node *smmu, 889491cf4a6SShameer Kolothum u32 *sids, u32 num_sids, 890491cf4a6SShameer Kolothum struct list_head *head) 891491cf4a6SShameer Kolothum { 892491cf4a6SShameer Kolothum struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data; 893491cf4a6SShameer Kolothum struct acpi_iort_rmr_desc *rmr_desc; 894491cf4a6SShameer Kolothum int i; 895491cf4a6SShameer Kolothum 896491cf4a6SShameer Kolothum rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node, 897491cf4a6SShameer Kolothum rmr->rmr_offset); 898491cf4a6SShameer Kolothum 899491cf4a6SShameer Kolothum iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count); 900491cf4a6SShameer Kolothum 901491cf4a6SShameer Kolothum for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) { 902491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 903491cf4a6SShameer Kolothum enum iommu_resv_type type; 904491cf4a6SShameer Kolothum int prot = IOMMU_READ | IOMMU_WRITE; 905491cf4a6SShameer Kolothum 906491cf4a6SShameer Kolothum if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED) 907491cf4a6SShameer Kolothum type = IOMMU_RESV_DIRECT_RELAXABLE; 908491cf4a6SShameer Kolothum else 909491cf4a6SShameer Kolothum type = IOMMU_RESV_DIRECT; 910491cf4a6SShameer Kolothum 911491cf4a6SShameer Kolothum if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE) 912491cf4a6SShameer Kolothum prot |= IOMMU_PRIV; 913491cf4a6SShameer Kolothum 914491cf4a6SShameer Kolothum /* Attributes 0x00 - 0x03 represents device memory */ 915491cf4a6SShameer Kolothum if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <= 916491cf4a6SShameer Kolothum ACPI_IORT_RMR_ATTR_DEVICE_GRE) 917491cf4a6SShameer Kolothum prot |= IOMMU_MMIO; 918491cf4a6SShameer Kolothum else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) == 919491cf4a6SShameer Kolothum ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB) 920491cf4a6SShameer Kolothum prot |= IOMMU_CACHE; 921491cf4a6SShameer Kolothum 922491cf4a6SShameer Kolothum rmr_data = iort_rmr_alloc(rmr_desc, prot, type, 923491cf4a6SShameer Kolothum sids, num_sids); 924491cf4a6SShameer Kolothum if (!rmr_data) 925491cf4a6SShameer Kolothum return; 926491cf4a6SShameer Kolothum 927491cf4a6SShameer Kolothum list_add_tail(&rmr_data->rr.list, head); 928491cf4a6SShameer Kolothum } 929491cf4a6SShameer Kolothum } 930491cf4a6SShameer Kolothum 931491cf4a6SShameer Kolothum static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start, 932491cf4a6SShameer Kolothum u32 new_count) 933491cf4a6SShameer Kolothum { 934491cf4a6SShameer Kolothum u32 *new_sids; 935491cf4a6SShameer Kolothum u32 total_count = count + new_count; 936491cf4a6SShameer Kolothum int i; 937491cf4a6SShameer Kolothum 938491cf4a6SShameer Kolothum new_sids = krealloc_array(sids, count + new_count, 939491cf4a6SShameer Kolothum sizeof(*new_sids), GFP_KERNEL); 940491cf4a6SShameer Kolothum if (!new_sids) 941491cf4a6SShameer Kolothum return NULL; 942491cf4a6SShameer Kolothum 943491cf4a6SShameer Kolothum for (i = count; i < total_count; i++) 944491cf4a6SShameer Kolothum new_sids[i] = id_start++; 945491cf4a6SShameer Kolothum 946491cf4a6SShameer Kolothum return new_sids; 947491cf4a6SShameer Kolothum } 948491cf4a6SShameer Kolothum 949491cf4a6SShameer Kolothum static bool iort_rmr_has_dev(struct device *dev, u32 id_start, 950491cf4a6SShameer Kolothum u32 id_count) 951491cf4a6SShameer Kolothum { 952491cf4a6SShameer Kolothum int i; 953491cf4a6SShameer Kolothum struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 954491cf4a6SShameer Kolothum 955491cf4a6SShameer Kolothum /* 956491cf4a6SShameer Kolothum * Make sure the kernel has preserved the boot firmware PCIe 957491cf4a6SShameer Kolothum * configuration. This is required to ensure that the RMR PCIe 958491cf4a6SShameer Kolothum * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5). 959491cf4a6SShameer Kolothum */ 960491cf4a6SShameer Kolothum if (dev_is_pci(dev)) { 961491cf4a6SShameer Kolothum struct pci_dev *pdev = to_pci_dev(dev); 962491cf4a6SShameer Kolothum struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus); 963491cf4a6SShameer Kolothum 964491cf4a6SShameer Kolothum if (!host->preserve_config) 965491cf4a6SShameer Kolothum return false; 966491cf4a6SShameer Kolothum } 967491cf4a6SShameer Kolothum 968491cf4a6SShameer Kolothum for (i = 0; i < fwspec->num_ids; i++) { 969491cf4a6SShameer Kolothum if (fwspec->ids[i] >= id_start && 970491cf4a6SShameer Kolothum fwspec->ids[i] <= id_start + id_count) 971491cf4a6SShameer Kolothum return true; 972491cf4a6SShameer Kolothum } 973491cf4a6SShameer Kolothum 974491cf4a6SShameer Kolothum return false; 975491cf4a6SShameer Kolothum } 976491cf4a6SShameer Kolothum 977491cf4a6SShameer Kolothum static void iort_node_get_rmr_info(struct acpi_iort_node *node, 978491cf4a6SShameer Kolothum struct acpi_iort_node *iommu, 979491cf4a6SShameer Kolothum struct device *dev, struct list_head *head) 980491cf4a6SShameer Kolothum { 981491cf4a6SShameer Kolothum struct acpi_iort_node *smmu = NULL; 982491cf4a6SShameer Kolothum struct acpi_iort_rmr *rmr; 983491cf4a6SShameer Kolothum struct acpi_iort_id_mapping *map; 984491cf4a6SShameer Kolothum u32 *sids = NULL; 985491cf4a6SShameer Kolothum u32 num_sids = 0; 986491cf4a6SShameer Kolothum int i; 987491cf4a6SShameer Kolothum 988491cf4a6SShameer Kolothum if (!node->mapping_offset || !node->mapping_count) { 989491cf4a6SShameer Kolothum pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n", 990491cf4a6SShameer Kolothum node); 991491cf4a6SShameer Kolothum return; 992491cf4a6SShameer Kolothum } 993491cf4a6SShameer Kolothum 994491cf4a6SShameer Kolothum rmr = (struct acpi_iort_rmr *)node->node_data; 995491cf4a6SShameer Kolothum if (!rmr->rmr_offset || !rmr->rmr_count) 996491cf4a6SShameer Kolothum return; 997491cf4a6SShameer Kolothum 998491cf4a6SShameer Kolothum map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 999491cf4a6SShameer Kolothum node->mapping_offset); 1000491cf4a6SShameer Kolothum 1001491cf4a6SShameer Kolothum /* 1002491cf4a6SShameer Kolothum * Go through the ID mappings and see if we have a match for SMMU 1003491cf4a6SShameer Kolothum * and dev(if !NULL). If found, get the sids for the Node. 1004491cf4a6SShameer Kolothum * Please note, id_count is equal to the number of IDs in the 1005491cf4a6SShameer Kolothum * range minus one. 1006491cf4a6SShameer Kolothum */ 1007491cf4a6SShameer Kolothum for (i = 0; i < node->mapping_count; i++, map++) { 1008491cf4a6SShameer Kolothum struct acpi_iort_node *parent; 1009491cf4a6SShameer Kolothum 1010491cf4a6SShameer Kolothum parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 1011491cf4a6SShameer Kolothum map->output_reference); 1012491cf4a6SShameer Kolothum if (parent != iommu) 1013491cf4a6SShameer Kolothum continue; 1014491cf4a6SShameer Kolothum 1015491cf4a6SShameer Kolothum /* If dev is valid, check RMR node corresponds to the dev SID */ 1016491cf4a6SShameer Kolothum if (dev && !iort_rmr_has_dev(dev, map->output_base, 1017491cf4a6SShameer Kolothum map->id_count)) 1018491cf4a6SShameer Kolothum continue; 1019491cf4a6SShameer Kolothum 1020491cf4a6SShameer Kolothum /* Retrieve SIDs associated with the Node. */ 1021491cf4a6SShameer Kolothum sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base, 1022491cf4a6SShameer Kolothum map->id_count + 1); 1023491cf4a6SShameer Kolothum if (!sids) 1024491cf4a6SShameer Kolothum return; 1025491cf4a6SShameer Kolothum 1026491cf4a6SShameer Kolothum num_sids += map->id_count + 1; 1027491cf4a6SShameer Kolothum } 1028491cf4a6SShameer Kolothum 1029491cf4a6SShameer Kolothum if (!sids) 1030491cf4a6SShameer Kolothum return; 1031491cf4a6SShameer Kolothum 1032491cf4a6SShameer Kolothum iort_get_rmrs(node, smmu, sids, num_sids, head); 1033491cf4a6SShameer Kolothum kfree(sids); 1034491cf4a6SShameer Kolothum } 1035491cf4a6SShameer Kolothum 1036491cf4a6SShameer Kolothum static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev, 1037491cf4a6SShameer Kolothum struct list_head *head) 1038491cf4a6SShameer Kolothum { 1039491cf4a6SShameer Kolothum struct acpi_table_iort *iort; 1040491cf4a6SShameer Kolothum struct acpi_iort_node *iort_node, *iort_end; 1041491cf4a6SShameer Kolothum int i; 1042491cf4a6SShameer Kolothum 1043491cf4a6SShameer Kolothum /* Only supports ARM DEN 0049E.d onwards */ 1044491cf4a6SShameer Kolothum if (iort_table->revision < 5) 1045491cf4a6SShameer Kolothum return; 1046491cf4a6SShameer Kolothum 1047491cf4a6SShameer Kolothum iort = (struct acpi_table_iort *)iort_table; 1048491cf4a6SShameer Kolothum 1049491cf4a6SShameer Kolothum iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1050491cf4a6SShameer Kolothum iort->node_offset); 1051491cf4a6SShameer Kolothum iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1052491cf4a6SShameer Kolothum iort_table->length); 1053491cf4a6SShameer Kolothum 1054491cf4a6SShameer Kolothum for (i = 0; i < iort->node_count; i++) { 1055491cf4a6SShameer Kolothum if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 1056491cf4a6SShameer Kolothum "IORT node pointer overflows, bad table!\n")) 1057491cf4a6SShameer Kolothum return; 1058491cf4a6SShameer Kolothum 1059491cf4a6SShameer Kolothum if (iort_node->type == ACPI_IORT_NODE_RMR) 1060491cf4a6SShameer Kolothum iort_node_get_rmr_info(iort_node, iommu, dev, head); 1061491cf4a6SShameer Kolothum 1062491cf4a6SShameer Kolothum iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1063491cf4a6SShameer Kolothum iort_node->length); 1064491cf4a6SShameer Kolothum } 1065491cf4a6SShameer Kolothum } 1066491cf4a6SShameer Kolothum 1067491cf4a6SShameer Kolothum /* 1068491cf4a6SShameer Kolothum * Populate the RMR list associated with a given IOMMU and dev(if provided). 1069491cf4a6SShameer Kolothum * If dev is NULL, the function populates all the RMRs associated with the 1070491cf4a6SShameer Kolothum * given IOMMU. 1071491cf4a6SShameer Kolothum */ 1072491cf4a6SShameer Kolothum static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode, 1073491cf4a6SShameer Kolothum struct device *dev, 1074491cf4a6SShameer Kolothum struct list_head *head) 1075491cf4a6SShameer Kolothum { 1076491cf4a6SShameer Kolothum struct acpi_iort_node *iommu; 1077491cf4a6SShameer Kolothum 1078491cf4a6SShameer Kolothum iommu = iort_get_iort_node(iommu_fwnode); 1079491cf4a6SShameer Kolothum if (!iommu) 1080491cf4a6SShameer Kolothum return; 1081491cf4a6SShameer Kolothum 1082491cf4a6SShameer Kolothum iort_find_rmrs(iommu, dev, head); 1083491cf4a6SShameer Kolothum } 1084491cf4a6SShameer Kolothum 10858b4282e6SShameer Kolothum static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) 10868b4282e6SShameer Kolothum { 10878b4282e6SShameer Kolothum struct acpi_iort_node *iommu; 10888097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 10898b4282e6SShameer Kolothum 10908b4282e6SShameer Kolothum iommu = iort_get_iort_node(fwspec->iommu_fwnode); 10918b4282e6SShameer Kolothum 10928b4282e6SShameer Kolothum if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { 10938b4282e6SShameer Kolothum struct acpi_iort_smmu_v3 *smmu; 10948b4282e6SShameer Kolothum 10958b4282e6SShameer Kolothum smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; 10968b4282e6SShameer Kolothum if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) 10978b4282e6SShameer Kolothum return iommu; 10988b4282e6SShameer Kolothum } 10998b4282e6SShameer Kolothum 11008b4282e6SShameer Kolothum return NULL; 11018b4282e6SShameer Kolothum } 11028b4282e6SShameer Kolothum 110355be25b8SShameer Kolothum /* 110455be25b8SShameer Kolothum * Retrieve platform specific HW MSI reserve regions. 11058778b1d4SShameer Kolothum * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K) 11068778b1d4SShameer Kolothum * associated with the device are the HW MSI reserved regions. 11078b4282e6SShameer Kolothum */ 110855be25b8SShameer Kolothum static void iort_iommu_msi_get_resv_regions(struct device *dev, 110955be25b8SShameer Kolothum struct list_head *head) 11108b4282e6SShameer Kolothum { 11118097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 11128b4282e6SShameer Kolothum struct acpi_iort_its_group *its; 11138b4282e6SShameer Kolothum struct acpi_iort_node *iommu_node, *its_node = NULL; 11148778b1d4SShameer Kolothum int i; 11158b4282e6SShameer Kolothum 11168b4282e6SShameer Kolothum iommu_node = iort_get_msi_resv_iommu(dev); 11178b4282e6SShameer Kolothum if (!iommu_node) 11188778b1d4SShameer Kolothum return; 11198b4282e6SShameer Kolothum 11208b4282e6SShameer Kolothum /* 11218b4282e6SShameer Kolothum * Current logic to reserve ITS regions relies on HW topologies 11228b4282e6SShameer Kolothum * where a given PCI or named component maps its IDs to only one 11238b4282e6SShameer Kolothum * ITS group; if a PCI or named component can map its IDs to 11248b4282e6SShameer Kolothum * different ITS groups through IORT mappings this function has 11258b4282e6SShameer Kolothum * to be reworked to ensure we reserve regions for all ITS groups 11268b4282e6SShameer Kolothum * a given PCI or named component may map IDs to. 11278b4282e6SShameer Kolothum */ 11288b4282e6SShameer Kolothum 11298097e53eSJoerg Roedel for (i = 0; i < fwspec->num_ids; i++) { 11308b4282e6SShameer Kolothum its_node = iort_node_map_id(iommu_node, 11318097e53eSJoerg Roedel fwspec->ids[i], 11328b4282e6SShameer Kolothum NULL, IORT_MSI_TYPE); 11338b4282e6SShameer Kolothum if (its_node) 11348b4282e6SShameer Kolothum break; 11358b4282e6SShameer Kolothum } 11368b4282e6SShameer Kolothum 11378b4282e6SShameer Kolothum if (!its_node) 11388778b1d4SShameer Kolothum return; 11398b4282e6SShameer Kolothum 11408b4282e6SShameer Kolothum /* Move to ITS specific data */ 11418b4282e6SShameer Kolothum its = (struct acpi_iort_its_group *)its_node->node_data; 11428b4282e6SShameer Kolothum 11438b4282e6SShameer Kolothum for (i = 0; i < its->its_count; i++) { 11448b4282e6SShameer Kolothum phys_addr_t base; 11458b4282e6SShameer Kolothum 11468b4282e6SShameer Kolothum if (!iort_find_its_base(its->identifiers[i], &base)) { 11478b4282e6SShameer Kolothum int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 11488b4282e6SShameer Kolothum struct iommu_resv_region *region; 11498b4282e6SShameer Kolothum 11508b4282e6SShameer Kolothum region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, 11510251d010SLu Baolu prot, IOMMU_RESV_MSI, 11520251d010SLu Baolu GFP_KERNEL); 11538778b1d4SShameer Kolothum if (region) 11548b4282e6SShameer Kolothum list_add_tail(®ion->list, head); 11558b4282e6SShameer Kolothum } 11568b4282e6SShameer Kolothum } 11578b4282e6SShameer Kolothum } 11588b4282e6SShameer Kolothum 115955be25b8SShameer Kolothum /** 116055be25b8SShameer Kolothum * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions. 116155be25b8SShameer Kolothum * @dev: Device from iommu_get_resv_regions() 116255be25b8SShameer Kolothum * @head: Reserved region list from iommu_get_resv_regions() 116355be25b8SShameer Kolothum */ 116455be25b8SShameer Kolothum void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 116555be25b8SShameer Kolothum { 1166491cf4a6SShameer Kolothum struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1167491cf4a6SShameer Kolothum 116855be25b8SShameer Kolothum iort_iommu_msi_get_resv_regions(dev, head); 1169491cf4a6SShameer Kolothum iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head); 117055be25b8SShameer Kolothum } 117155be25b8SShameer Kolothum 1172de026976SRen Zhijie /** 1173de026976SRen Zhijie * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with 1174de026976SRen Zhijie * associated StreamIDs information. 1175de026976SRen Zhijie * @iommu_fwnode: fwnode associated with IOMMU 1176de026976SRen Zhijie * @head: Resereved region list 1177de026976SRen Zhijie */ 1178de026976SRen Zhijie void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode, 1179de026976SRen Zhijie struct list_head *head) 1180de026976SRen Zhijie { 1181de026976SRen Zhijie iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head); 1182de026976SRen Zhijie } 1183de026976SRen Zhijie EXPORT_SYMBOL_GPL(iort_get_rmr_sids); 1184de026976SRen Zhijie 1185de026976SRen Zhijie /** 1186de026976SRen Zhijie * iort_put_rmr_sids - Free memory allocated for RMR reserved regions. 1187de026976SRen Zhijie * @iommu_fwnode: fwnode associated with IOMMU 1188de026976SRen Zhijie * @head: Resereved region list 1189de026976SRen Zhijie */ 1190de026976SRen Zhijie void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode, 1191de026976SRen Zhijie struct list_head *head) 1192de026976SRen Zhijie { 1193de026976SRen Zhijie struct iommu_resv_region *entry, *next; 1194de026976SRen Zhijie 1195de026976SRen Zhijie list_for_each_entry_safe(entry, next, head, list) 1196de026976SRen Zhijie entry->free(NULL, entry); 1197de026976SRen Zhijie } 1198de026976SRen Zhijie EXPORT_SYMBOL_GPL(iort_put_rmr_sids); 1199de026976SRen Zhijie 120082126886SLorenzo Pieralisi static inline bool iort_iommu_driver_enabled(u8 type) 120182126886SLorenzo Pieralisi { 120282126886SLorenzo Pieralisi switch (type) { 120382126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU_V3: 1204d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU_V3); 120582126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU: 1206d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU); 120782126886SLorenzo Pieralisi default: 120882126886SLorenzo Pieralisi pr_warn("IORT node type %u does not describe an SMMU\n", type); 120982126886SLorenzo Pieralisi return false; 121082126886SLorenzo Pieralisi } 121182126886SLorenzo Pieralisi } 121282126886SLorenzo Pieralisi 121382126886SLorenzo Pieralisi static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) 121482126886SLorenzo Pieralisi { 121582126886SLorenzo Pieralisi struct acpi_iort_root_complex *pci_rc; 121682126886SLorenzo Pieralisi 121782126886SLorenzo Pieralisi pci_rc = (struct acpi_iort_root_complex *)node->node_data; 121882126886SLorenzo Pieralisi return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; 121982126886SLorenzo Pieralisi } 1220d49f2dedSLorenzo Pieralisi 1221bc8648d4SRobin Murphy static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, 1222643b8e4dSLorenzo Pieralisi u32 streamid) 1223643b8e4dSLorenzo Pieralisi { 1224643b8e4dSLorenzo Pieralisi struct fwnode_handle *iort_fwnode; 1225643b8e4dSLorenzo Pieralisi 1226*3f7c3209SRobin Murphy /* If there's no SMMU driver at all, give up now */ 1227*3f7c3209SRobin Murphy if (!node || !iort_iommu_driver_enabled(node->type)) 1228bc8648d4SRobin Murphy return -ENODEV; 1229bc8648d4SRobin Murphy 1230643b8e4dSLorenzo Pieralisi iort_fwnode = iort_get_fwnode(node); 1231643b8e4dSLorenzo Pieralisi if (!iort_fwnode) 1232bc8648d4SRobin Murphy return -ENODEV; 1233643b8e4dSLorenzo Pieralisi 12345a1bb638SSricharan R /* 1235*3f7c3209SRobin Murphy * If the SMMU drivers are enabled but not loaded/probed 1236*3f7c3209SRobin Murphy * yet, this will defer. 12375a1bb638SSricharan R */ 1238*3f7c3209SRobin Murphy return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode); 1239643b8e4dSLorenzo Pieralisi } 1240643b8e4dSLorenzo Pieralisi 1241bc8648d4SRobin Murphy struct iort_pci_alias_info { 1242bc8648d4SRobin Murphy struct device *dev; 1243bc8648d4SRobin Murphy struct acpi_iort_node *node; 1244bc8648d4SRobin Murphy }; 1245bc8648d4SRobin Murphy 1246bc8648d4SRobin Murphy static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 1247bc8648d4SRobin Murphy { 1248bc8648d4SRobin Murphy struct iort_pci_alias_info *info = data; 1249bc8648d4SRobin Murphy struct acpi_iort_node *parent; 1250bc8648d4SRobin Murphy u32 streamid; 1251bc8648d4SRobin Murphy 1252bc8648d4SRobin Murphy parent = iort_node_map_id(info->node, alias, &streamid, 1253bc8648d4SRobin Murphy IORT_IOMMU_TYPE); 1254bc8648d4SRobin Murphy return iort_iommu_xlate(info->dev, parent, streamid); 1255643b8e4dSLorenzo Pieralisi } 1256643b8e4dSLorenzo Pieralisi 1257da22565dSJean-Philippe Brucker static void iort_named_component_init(struct device *dev, 1258da22565dSJean-Philippe Brucker struct acpi_iort_node *node) 1259da22565dSJean-Philippe Brucker { 12606522b1e0SJean-Philippe Brucker struct property_entry props[3] = {}; 1261da22565dSJean-Philippe Brucker struct acpi_iort_named_component *nc; 1262da22565dSJean-Philippe Brucker 1263da22565dSJean-Philippe Brucker nc = (struct acpi_iort_named_component *)node->node_data; 1264434b73e6SJean-Philippe Brucker props[0] = PROPERTY_ENTRY_U32("pasid-num-bits", 1265434b73e6SJean-Philippe Brucker FIELD_GET(ACPI_IORT_NC_PASID_BITS, 1266434b73e6SJean-Philippe Brucker nc->node_flags)); 12676522b1e0SJean-Philippe Brucker if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED) 12686522b1e0SJean-Philippe Brucker props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall"); 1269434b73e6SJean-Philippe Brucker 12700df316b8SHeikki Krogerus if (device_create_managed_software_node(dev, props, NULL)) 1271434b73e6SJean-Philippe Brucker dev_warn(dev, "Could not add device properties\n"); 1272da22565dSJean-Philippe Brucker } 1273da22565dSJean-Philippe Brucker 1274b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node) 1275b8e069a2SLorenzo Pieralisi { 1276b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 1277b8e069a2SLorenzo Pieralisi int err = -ENODEV, i = 0; 1278b8e069a2SLorenzo Pieralisi u32 streamid = 0; 1279b8e069a2SLorenzo Pieralisi 1280b8e069a2SLorenzo Pieralisi do { 1281b8e069a2SLorenzo Pieralisi 1282b8e069a2SLorenzo Pieralisi parent = iort_node_map_platform_id(node, &streamid, 1283b8e069a2SLorenzo Pieralisi IORT_IOMMU_TYPE, 1284b8e069a2SLorenzo Pieralisi i++); 1285b8e069a2SLorenzo Pieralisi 1286b8e069a2SLorenzo Pieralisi if (parent) 1287b8e069a2SLorenzo Pieralisi err = iort_iommu_xlate(dev, parent, streamid); 1288b8e069a2SLorenzo Pieralisi } while (parent && !err); 1289b8e069a2SLorenzo Pieralisi 1290b8e069a2SLorenzo Pieralisi return err; 1291b8e069a2SLorenzo Pieralisi } 1292b8e069a2SLorenzo Pieralisi 1293b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map_id(struct device *dev, 1294b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node, 1295b8e069a2SLorenzo Pieralisi const u32 *in_id) 1296b8e069a2SLorenzo Pieralisi { 1297b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 1298b8e069a2SLorenzo Pieralisi u32 streamid; 1299b8e069a2SLorenzo Pieralisi 1300b8e069a2SLorenzo Pieralisi parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE); 1301b8e069a2SLorenzo Pieralisi if (parent) 1302b8e069a2SLorenzo Pieralisi return iort_iommu_xlate(dev, parent, streamid); 1303b8e069a2SLorenzo Pieralisi 1304b8e069a2SLorenzo Pieralisi return -ENODEV; 1305b8e069a2SLorenzo Pieralisi } 1306b8e069a2SLorenzo Pieralisi 1307b8e069a2SLorenzo Pieralisi 130882126886SLorenzo Pieralisi /** 1309b8e069a2SLorenzo Pieralisi * iort_iommu_configure_id - Set-up IOMMU configuration for a device. 131082126886SLorenzo Pieralisi * 131182126886SLorenzo Pieralisi * @dev: device to configure 1312b8e069a2SLorenzo Pieralisi * @id_in: optional input id const value pointer 131382126886SLorenzo Pieralisi * 131411a8c5e3SJean-Philippe Brucker * Returns: 0 on success, <0 on failure 131582126886SLorenzo Pieralisi */ 131611a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *id_in) 131782126886SLorenzo Pieralisi { 1318b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node; 131982126886SLorenzo Pieralisi int err = -ENODEV; 132082126886SLorenzo Pieralisi 132182126886SLorenzo Pieralisi if (dev_is_pci(dev)) { 13226990ec79SJoerg Roedel struct iommu_fwspec *fwspec; 132382126886SLorenzo Pieralisi struct pci_bus *bus = to_pci_dev(dev)->bus; 132482126886SLorenzo Pieralisi struct iort_pci_alias_info info = { .dev = dev }; 132582126886SLorenzo Pieralisi 132682126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 132782126886SLorenzo Pieralisi iort_match_node_callback, &bus->dev); 132882126886SLorenzo Pieralisi if (!node) 132911a8c5e3SJean-Philippe Brucker return -ENODEV; 133082126886SLorenzo Pieralisi 133182126886SLorenzo Pieralisi info.node = node; 133282126886SLorenzo Pieralisi err = pci_for_each_dma_alias(to_pci_dev(dev), 133382126886SLorenzo Pieralisi iort_pci_iommu_init, &info); 133482126886SLorenzo Pieralisi 13356990ec79SJoerg Roedel fwspec = dev_iommu_fwspec_get(dev); 13366990ec79SJoerg Roedel if (fwspec && iort_pci_rc_supports_ats(node)) 13376990ec79SJoerg Roedel fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; 133882126886SLorenzo Pieralisi } else { 133982126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 134082126886SLorenzo Pieralisi iort_match_node_callback, dev); 134182126886SLorenzo Pieralisi if (!node) 134211a8c5e3SJean-Philippe Brucker return -ENODEV; 134382126886SLorenzo Pieralisi 1344b8e069a2SLorenzo Pieralisi err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) : 1345b8e069a2SLorenzo Pieralisi iort_nc_iommu_map(dev, node); 1346da22565dSJean-Philippe Brucker 1347da22565dSJean-Philippe Brucker if (!err) 1348da22565dSJean-Philippe Brucker iort_named_component_init(dev, node); 134982126886SLorenzo Pieralisi } 135082126886SLorenzo Pieralisi 135111a8c5e3SJean-Philippe Brucker return err; 135282126886SLorenzo Pieralisi } 1353b8e069a2SLorenzo Pieralisi 135482126886SLorenzo Pieralisi #else 135555be25b8SShameer Kolothum void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 13568778b1d4SShameer Kolothum { } 135711a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *input_id) 135811a8c5e3SJean-Philippe Brucker { return -ENODEV; } 135982126886SLorenzo Pieralisi #endif 136082126886SLorenzo Pieralisi 136191cfd679SRobin Murphy static int nc_dma_get_range(struct device *dev, u64 *limit) 136210d8ab2cSLorenzo Pieralisi { 136310d8ab2cSLorenzo Pieralisi struct acpi_iort_node *node; 136410d8ab2cSLorenzo Pieralisi struct acpi_iort_named_component *ncomp; 136510d8ab2cSLorenzo Pieralisi 136610d8ab2cSLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 136710d8ab2cSLorenzo Pieralisi iort_match_node_callback, dev); 136810d8ab2cSLorenzo Pieralisi if (!node) 136910d8ab2cSLorenzo Pieralisi return -ENODEV; 137010d8ab2cSLorenzo Pieralisi 137110d8ab2cSLorenzo Pieralisi ncomp = (struct acpi_iort_named_component *)node->node_data; 137210d8ab2cSLorenzo Pieralisi 1373a1df829eSMoritz Fischer if (!ncomp->memory_address_limit) { 1374a1df829eSMoritz Fischer pr_warn(FW_BUG "Named component missing memory address limit\n"); 1375a1df829eSMoritz Fischer return -EINVAL; 1376a1df829eSMoritz Fischer } 1377a1df829eSMoritz Fischer 137891cfd679SRobin Murphy *limit = ncomp->memory_address_limit >= 64 ? U64_MAX : 137991cfd679SRobin Murphy (1ULL << ncomp->memory_address_limit) - 1; 138010d8ab2cSLorenzo Pieralisi 138110d8ab2cSLorenzo Pieralisi return 0; 138210d8ab2cSLorenzo Pieralisi } 138310d8ab2cSLorenzo Pieralisi 138491cfd679SRobin Murphy static int rc_dma_get_range(struct device *dev, u64 *limit) 13855ac65e8cSRobin Murphy { 13865ac65e8cSRobin Murphy struct acpi_iort_node *node; 13875ac65e8cSRobin Murphy struct acpi_iort_root_complex *rc; 1388c7777236SJean-Philippe Brucker struct pci_bus *pbus = to_pci_dev(dev)->bus; 13895ac65e8cSRobin Murphy 13905ac65e8cSRobin Murphy node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1391c7777236SJean-Philippe Brucker iort_match_node_callback, &pbus->dev); 13925ac65e8cSRobin Murphy if (!node || node->revision < 1) 13935ac65e8cSRobin Murphy return -ENODEV; 13945ac65e8cSRobin Murphy 13955ac65e8cSRobin Murphy rc = (struct acpi_iort_root_complex *)node->node_data; 13965ac65e8cSRobin Murphy 1397a1df829eSMoritz Fischer if (!rc->memory_address_limit) { 1398a1df829eSMoritz Fischer pr_warn(FW_BUG "Root complex missing memory address limit\n"); 1399a1df829eSMoritz Fischer return -EINVAL; 1400a1df829eSMoritz Fischer } 1401a1df829eSMoritz Fischer 140291cfd679SRobin Murphy *limit = rc->memory_address_limit >= 64 ? U64_MAX : 140391cfd679SRobin Murphy (1ULL << rc->memory_address_limit) - 1; 14045ac65e8cSRobin Murphy 14055ac65e8cSRobin Murphy return 0; 14065ac65e8cSRobin Murphy } 14075ac65e8cSRobin Murphy 1408643b8e4dSLorenzo Pieralisi /** 1409db59e1b6SJean-Philippe Brucker * iort_dma_get_ranges() - Look up DMA addressing limit for the device 1410db59e1b6SJean-Philippe Brucker * @dev: device to lookup 141191cfd679SRobin Murphy * @limit: DMA limit result pointer 141218b709beSLorenzo Pieralisi * 1413db59e1b6SJean-Philippe Brucker * Return: 0 on success, an error otherwise. 141418b709beSLorenzo Pieralisi */ 141591cfd679SRobin Murphy int iort_dma_get_ranges(struct device *dev, u64 *limit) 141618b709beSLorenzo Pieralisi { 1417db59e1b6SJean-Philippe Brucker if (dev_is_pci(dev)) 141891cfd679SRobin Murphy return rc_dma_get_range(dev, limit); 14196757cdaeSRobin Murphy else 142091cfd679SRobin Murphy return nc_dma_get_range(dev, limit); 142118b709beSLorenzo Pieralisi } 142218b709beSLorenzo Pieralisi 1423e4dadfa8SLorenzo Pieralisi static void __init acpi_iort_register_irq(int hwirq, const char *name, 1424e4dadfa8SLorenzo Pieralisi int trigger, 1425e4dadfa8SLorenzo Pieralisi struct resource *res) 1426e4dadfa8SLorenzo Pieralisi { 1427e4dadfa8SLorenzo Pieralisi int irq = acpi_register_gsi(NULL, hwirq, trigger, 1428e4dadfa8SLorenzo Pieralisi ACPI_ACTIVE_HIGH); 1429e4dadfa8SLorenzo Pieralisi 1430e4dadfa8SLorenzo Pieralisi if (irq <= 0) { 1431e4dadfa8SLorenzo Pieralisi pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, 1432e4dadfa8SLorenzo Pieralisi name); 1433e4dadfa8SLorenzo Pieralisi return; 1434e4dadfa8SLorenzo Pieralisi } 1435e4dadfa8SLorenzo Pieralisi 1436e4dadfa8SLorenzo Pieralisi res->start = irq; 1437e4dadfa8SLorenzo Pieralisi res->end = irq; 1438e4dadfa8SLorenzo Pieralisi res->flags = IORESOURCE_IRQ; 1439e4dadfa8SLorenzo Pieralisi res->name = name; 1440e4dadfa8SLorenzo Pieralisi } 1441e4dadfa8SLorenzo Pieralisi 1442e4dadfa8SLorenzo Pieralisi static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) 1443e4dadfa8SLorenzo Pieralisi { 1444e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1445e4dadfa8SLorenzo Pieralisi /* Always present mem resource */ 1446e4dadfa8SLorenzo Pieralisi int num_res = 1; 1447e4dadfa8SLorenzo Pieralisi 1448e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1449e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1450e4dadfa8SLorenzo Pieralisi 1451e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1452e4dadfa8SLorenzo Pieralisi num_res++; 1453e4dadfa8SLorenzo Pieralisi 1454e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1455e4dadfa8SLorenzo Pieralisi num_res++; 1456e4dadfa8SLorenzo Pieralisi 1457e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1458e4dadfa8SLorenzo Pieralisi num_res++; 1459e4dadfa8SLorenzo Pieralisi 1460e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1461e4dadfa8SLorenzo Pieralisi num_res++; 1462e4dadfa8SLorenzo Pieralisi 1463e4dadfa8SLorenzo Pieralisi return num_res; 1464e4dadfa8SLorenzo Pieralisi } 1465e4dadfa8SLorenzo Pieralisi 1466f935448aSGeetha Sowjanya static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) 1467f935448aSGeetha Sowjanya { 1468f935448aSGeetha Sowjanya /* 1469f935448aSGeetha Sowjanya * Cavium ThunderX2 implementation doesn't not support unique 1470f935448aSGeetha Sowjanya * irq line. Use single irq line for all the SMMUv3 interrupts. 1471f935448aSGeetha Sowjanya */ 1472f935448aSGeetha Sowjanya if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1473f935448aSGeetha Sowjanya return false; 1474f935448aSGeetha Sowjanya 1475f935448aSGeetha Sowjanya /* 1476f935448aSGeetha Sowjanya * ThunderX2 doesn't support MSIs from the SMMU, so we're checking 1477f935448aSGeetha Sowjanya * SPI numbers here. 1478f935448aSGeetha Sowjanya */ 1479f935448aSGeetha Sowjanya return smmu->event_gsiv == smmu->pri_gsiv && 1480f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->gerr_gsiv && 1481f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->sync_gsiv; 1482f935448aSGeetha Sowjanya } 1483f935448aSGeetha Sowjanya 1484403e8c7cSLinu Cherian static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) 1485403e8c7cSLinu Cherian { 1486403e8c7cSLinu Cherian /* 1487403e8c7cSLinu Cherian * Override the size, for Cavium ThunderX2 implementation 1488403e8c7cSLinu Cherian * which doesn't support the page 1 SMMU register space. 1489403e8c7cSLinu Cherian */ 1490403e8c7cSLinu Cherian if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1491403e8c7cSLinu Cherian return SZ_64K; 1492403e8c7cSLinu Cherian 1493403e8c7cSLinu Cherian return SZ_128K; 1494403e8c7cSLinu Cherian } 1495403e8c7cSLinu Cherian 1496e4dadfa8SLorenzo Pieralisi static void __init arm_smmu_v3_init_resources(struct resource *res, 1497e4dadfa8SLorenzo Pieralisi struct acpi_iort_node *node) 1498e4dadfa8SLorenzo Pieralisi { 1499e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1500e4dadfa8SLorenzo Pieralisi int num_res = 0; 1501e4dadfa8SLorenzo Pieralisi 1502e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1503e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1504e4dadfa8SLorenzo Pieralisi 1505e4dadfa8SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1506403e8c7cSLinu Cherian res[num_res].end = smmu->base_address + 1507403e8c7cSLinu Cherian arm_smmu_v3_resource_size(smmu) - 1; 1508e4dadfa8SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1509e4dadfa8SLorenzo Pieralisi 1510e4dadfa8SLorenzo Pieralisi num_res++; 1511f935448aSGeetha Sowjanya if (arm_smmu_v3_is_combined_irq(smmu)) { 1512f935448aSGeetha Sowjanya if (smmu->event_gsiv) 1513f935448aSGeetha Sowjanya acpi_iort_register_irq(smmu->event_gsiv, "combined", 1514f935448aSGeetha Sowjanya ACPI_EDGE_SENSITIVE, 1515f935448aSGeetha Sowjanya &res[num_res++]); 1516f935448aSGeetha Sowjanya } else { 1517e4dadfa8SLorenzo Pieralisi 1518e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1519e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->event_gsiv, "eventq", 1520e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1521e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1522e4dadfa8SLorenzo Pieralisi 1523e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1524e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->pri_gsiv, "priq", 1525e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1526e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1527e4dadfa8SLorenzo Pieralisi 1528e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1529e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", 1530e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1531e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1532e4dadfa8SLorenzo Pieralisi 1533e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1534e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", 1535e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1536e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1537e4dadfa8SLorenzo Pieralisi } 1538f935448aSGeetha Sowjanya } 1539e4dadfa8SLorenzo Pieralisi 154024e51604SNeil Leeder static void __init arm_smmu_v3_dma_configure(struct device *dev, 154124e51604SNeil Leeder struct acpi_iort_node *node) 1542e4dadfa8SLorenzo Pieralisi { 1543e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 154424e51604SNeil Leeder enum dev_dma_attr attr; 1545e4dadfa8SLorenzo Pieralisi 1546e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1547e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1548e4dadfa8SLorenzo Pieralisi 154924e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? 155024e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 155124e51604SNeil Leeder 155224e51604SNeil Leeder /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ 155324e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 155424e51604SNeil Leeder 155524e51604SNeil Leeder /* Configure DMA for the page table walker */ 155624e51604SNeil Leeder acpi_dma_configure(dev, attr); 1557e4dadfa8SLorenzo Pieralisi } 1558e4dadfa8SLorenzo Pieralisi 155975808131SLorenzo Pieralisi #if defined(CONFIG_ACPI_NUMA) 15605fe0ce3bSGanapatrao Kulkarni /* 15615fe0ce3bSGanapatrao Kulkarni * set numa proximity domain for smmuv3 device 15625fe0ce3bSGanapatrao Kulkarni */ 156336a2ba07SKefeng Wang static int __init arm_smmu_v3_set_proximity(struct device *dev, 15645fe0ce3bSGanapatrao Kulkarni struct acpi_iort_node *node) 15655fe0ce3bSGanapatrao Kulkarni { 15665fe0ce3bSGanapatrao Kulkarni struct acpi_iort_smmu_v3 *smmu; 15675fe0ce3bSGanapatrao Kulkarni 15685fe0ce3bSGanapatrao Kulkarni smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 15695fe0ce3bSGanapatrao Kulkarni if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { 157001feba59SJonathan Cameron int dev_node = pxm_to_node(smmu->pxm); 157136a2ba07SKefeng Wang 15723e77eeb7SLorenzo Pieralisi if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) 157336a2ba07SKefeng Wang return -EINVAL; 157436a2ba07SKefeng Wang 15753e77eeb7SLorenzo Pieralisi set_dev_node(dev, dev_node); 15765fe0ce3bSGanapatrao Kulkarni pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", 15775fe0ce3bSGanapatrao Kulkarni smmu->base_address, 15785fe0ce3bSGanapatrao Kulkarni smmu->pxm); 15795fe0ce3bSGanapatrao Kulkarni } 158036a2ba07SKefeng Wang return 0; 15815fe0ce3bSGanapatrao Kulkarni } 15825fe0ce3bSGanapatrao Kulkarni #else 15835fe0ce3bSGanapatrao Kulkarni #define arm_smmu_v3_set_proximity NULL 15845fe0ce3bSGanapatrao Kulkarni #endif 15855fe0ce3bSGanapatrao Kulkarni 1586d6fcd3b1SLorenzo Pieralisi static int __init arm_smmu_count_resources(struct acpi_iort_node *node) 1587d6fcd3b1SLorenzo Pieralisi { 1588d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1589d6fcd3b1SLorenzo Pieralisi 1590d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1591d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1592d6fcd3b1SLorenzo Pieralisi 1593d6fcd3b1SLorenzo Pieralisi /* 1594d6fcd3b1SLorenzo Pieralisi * Only consider the global fault interrupt and ignore the 1595d6fcd3b1SLorenzo Pieralisi * configuration access interrupt. 1596d6fcd3b1SLorenzo Pieralisi * 1597d6fcd3b1SLorenzo Pieralisi * MMIO address and global fault interrupt resources are always 1598d6fcd3b1SLorenzo Pieralisi * present so add them to the context interrupt count as a static 1599d6fcd3b1SLorenzo Pieralisi * value. 1600d6fcd3b1SLorenzo Pieralisi */ 1601d6fcd3b1SLorenzo Pieralisi return smmu->context_interrupt_count + 2; 1602d6fcd3b1SLorenzo Pieralisi } 1603d6fcd3b1SLorenzo Pieralisi 1604d6fcd3b1SLorenzo Pieralisi static void __init arm_smmu_init_resources(struct resource *res, 1605d6fcd3b1SLorenzo Pieralisi struct acpi_iort_node *node) 1606d6fcd3b1SLorenzo Pieralisi { 1607d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1608d6fcd3b1SLorenzo Pieralisi int i, hw_irq, trigger, num_res = 0; 1609d6fcd3b1SLorenzo Pieralisi u64 *ctx_irq, *glb_irq; 1610d6fcd3b1SLorenzo Pieralisi 1611d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1612d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1613d6fcd3b1SLorenzo Pieralisi 1614d6fcd3b1SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1615d6fcd3b1SLorenzo Pieralisi res[num_res].end = smmu->base_address + smmu->span - 1; 1616d6fcd3b1SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1617d6fcd3b1SLorenzo Pieralisi num_res++; 1618d6fcd3b1SLorenzo Pieralisi 1619d6fcd3b1SLorenzo Pieralisi glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); 1620d6fcd3b1SLorenzo Pieralisi /* Global IRQs */ 1621d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(glb_irq[0]); 1622d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); 1623d6fcd3b1SLorenzo Pieralisi 1624d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, 1625d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1626d6fcd3b1SLorenzo Pieralisi 1627d6fcd3b1SLorenzo Pieralisi /* Context IRQs */ 1628d6fcd3b1SLorenzo Pieralisi ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); 1629d6fcd3b1SLorenzo Pieralisi for (i = 0; i < smmu->context_interrupt_count; i++) { 1630d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(ctx_irq[i]); 1631d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); 1632d6fcd3b1SLorenzo Pieralisi 1633d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, 1634d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1635d6fcd3b1SLorenzo Pieralisi } 1636d6fcd3b1SLorenzo Pieralisi } 1637d6fcd3b1SLorenzo Pieralisi 163824e51604SNeil Leeder static void __init arm_smmu_dma_configure(struct device *dev, 163924e51604SNeil Leeder struct acpi_iort_node *node) 1640d6fcd3b1SLorenzo Pieralisi { 1641d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 164224e51604SNeil Leeder enum dev_dma_attr attr; 1643d6fcd3b1SLorenzo Pieralisi 1644d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1645d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1646d6fcd3b1SLorenzo Pieralisi 164724e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? 164824e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 164924e51604SNeil Leeder 165024e51604SNeil Leeder /* We expect the dma masks to be equivalent for SMMU set-ups */ 165124e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 165224e51604SNeil Leeder 165324e51604SNeil Leeder /* Configure DMA for the page table walker */ 165424e51604SNeil Leeder acpi_dma_configure(dev, attr); 165524e51604SNeil Leeder } 165624e51604SNeil Leeder 165724e51604SNeil Leeder static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) 165824e51604SNeil Leeder { 165924e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 166024e51604SNeil Leeder 166124e51604SNeil Leeder /* Retrieve PMCG specific data */ 166224e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 166324e51604SNeil Leeder 166424e51604SNeil Leeder /* 166524e51604SNeil Leeder * There are always 2 memory resources. 166624e51604SNeil Leeder * If the overflow_gsiv is present then add that for a total of 3. 166724e51604SNeil Leeder */ 166824e51604SNeil Leeder return pmcg->overflow_gsiv ? 3 : 2; 166924e51604SNeil Leeder } 167024e51604SNeil Leeder 167124e51604SNeil Leeder static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, 167224e51604SNeil Leeder struct acpi_iort_node *node) 167324e51604SNeil Leeder { 167424e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 167524e51604SNeil Leeder 167624e51604SNeil Leeder /* Retrieve PMCG specific data */ 167724e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 167824e51604SNeil Leeder 167924e51604SNeil Leeder res[0].start = pmcg->page0_base_address; 168024e51604SNeil Leeder res[0].end = pmcg->page0_base_address + SZ_4K - 1; 168124e51604SNeil Leeder res[0].flags = IORESOURCE_MEM; 1682da5fb9e1SRobin Murphy /* 1683da5fb9e1SRobin Murphy * The initial version in DEN0049C lacked a way to describe register 1684da5fb9e1SRobin Murphy * page 1, which makes it broken for most PMCG implementations; in 1685da5fb9e1SRobin Murphy * that case, just let the driver fail gracefully if it expects to 1686da5fb9e1SRobin Murphy * find a second memory resource. 1687da5fb9e1SRobin Murphy */ 1688da5fb9e1SRobin Murphy if (node->revision > 0) { 168924e51604SNeil Leeder res[1].start = pmcg->page1_base_address; 169024e51604SNeil Leeder res[1].end = pmcg->page1_base_address + SZ_4K - 1; 169124e51604SNeil Leeder res[1].flags = IORESOURCE_MEM; 1692da5fb9e1SRobin Murphy } 169324e51604SNeil Leeder 169424e51604SNeil Leeder if (pmcg->overflow_gsiv) 169524e51604SNeil Leeder acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", 169624e51604SNeil Leeder ACPI_EDGE_SENSITIVE, &res[2]); 169724e51604SNeil Leeder } 169824e51604SNeil Leeder 169924062fe8SShameer Kolothum static struct acpi_platform_list pmcg_plat_info[] __initdata = { 170024062fe8SShameer Kolothum /* HiSilicon Hip08 Platform */ 170124062fe8SShameer Kolothum {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 17020242737dSYicong Yang "Erratum #162001800, Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP08}, 17030242737dSYicong Yang /* HiSilicon Hip09 Platform */ 17040242737dSYicong Yang {"HISI ", "HIP09 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 17050242737dSYicong Yang "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 170624062fe8SShameer Kolothum { } 170724062fe8SShameer Kolothum }; 170824062fe8SShameer Kolothum 170924e51604SNeil Leeder static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) 171024e51604SNeil Leeder { 171124062fe8SShameer Kolothum u32 model; 171224062fe8SShameer Kolothum int idx; 171324062fe8SShameer Kolothum 171424062fe8SShameer Kolothum idx = acpi_match_platform_list(pmcg_plat_info); 171524062fe8SShameer Kolothum if (idx >= 0) 171624062fe8SShameer Kolothum model = pmcg_plat_info[idx].data; 171724062fe8SShameer Kolothum else 171824062fe8SShameer Kolothum model = IORT_SMMU_V3_PMCG_GENERIC; 171924e51604SNeil Leeder 172024e51604SNeil Leeder return platform_device_add_data(pdev, &model, sizeof(model)); 1721d6fcd3b1SLorenzo Pieralisi } 1722d6fcd3b1SLorenzo Pieralisi 1723896dd2c3SLorenzo Pieralisi struct iort_dev_config { 1724846f0e9eSLorenzo Pieralisi const char *name; 1725896dd2c3SLorenzo Pieralisi int (*dev_init)(struct acpi_iort_node *node); 172624e51604SNeil Leeder void (*dev_dma_configure)(struct device *dev, 172724e51604SNeil Leeder struct acpi_iort_node *node); 1728896dd2c3SLorenzo Pieralisi int (*dev_count_resources)(struct acpi_iort_node *node); 1729896dd2c3SLorenzo Pieralisi void (*dev_init_resources)(struct resource *res, 1730846f0e9eSLorenzo Pieralisi struct acpi_iort_node *node); 173136a2ba07SKefeng Wang int (*dev_set_proximity)(struct device *dev, 17325fe0ce3bSGanapatrao Kulkarni struct acpi_iort_node *node); 173324e51604SNeil Leeder int (*dev_add_platdata)(struct platform_device *pdev); 1734846f0e9eSLorenzo Pieralisi }; 1735846f0e9eSLorenzo Pieralisi 1736896dd2c3SLorenzo Pieralisi static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { 1737e4dadfa8SLorenzo Pieralisi .name = "arm-smmu-v3", 173824e51604SNeil Leeder .dev_dma_configure = arm_smmu_v3_dma_configure, 1739896dd2c3SLorenzo Pieralisi .dev_count_resources = arm_smmu_v3_count_resources, 1740896dd2c3SLorenzo Pieralisi .dev_init_resources = arm_smmu_v3_init_resources, 1741896dd2c3SLorenzo Pieralisi .dev_set_proximity = arm_smmu_v3_set_proximity, 1742e4dadfa8SLorenzo Pieralisi }; 1743e4dadfa8SLorenzo Pieralisi 1744896dd2c3SLorenzo Pieralisi static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { 1745d6fcd3b1SLorenzo Pieralisi .name = "arm-smmu", 174624e51604SNeil Leeder .dev_dma_configure = arm_smmu_dma_configure, 1747896dd2c3SLorenzo Pieralisi .dev_count_resources = arm_smmu_count_resources, 174824e51604SNeil Leeder .dev_init_resources = arm_smmu_init_resources, 174924e51604SNeil Leeder }; 175024e51604SNeil Leeder 175124e51604SNeil Leeder static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { 175224e51604SNeil Leeder .name = "arm-smmu-v3-pmcg", 175324e51604SNeil Leeder .dev_count_resources = arm_smmu_v3_pmcg_count_resources, 175424e51604SNeil Leeder .dev_init_resources = arm_smmu_v3_pmcg_init_resources, 175524e51604SNeil Leeder .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, 1756d6fcd3b1SLorenzo Pieralisi }; 1757d6fcd3b1SLorenzo Pieralisi 1758896dd2c3SLorenzo Pieralisi static __init const struct iort_dev_config *iort_get_dev_cfg( 1759e3d49392SLorenzo Pieralisi struct acpi_iort_node *node) 1760846f0e9eSLorenzo Pieralisi { 1761e4dadfa8SLorenzo Pieralisi switch (node->type) { 1762e4dadfa8SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU_V3: 1763e4dadfa8SLorenzo Pieralisi return &iort_arm_smmu_v3_cfg; 1764d6fcd3b1SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU: 1765d6fcd3b1SLorenzo Pieralisi return &iort_arm_smmu_cfg; 176624e51604SNeil Leeder case ACPI_IORT_NODE_PMCG: 176724e51604SNeil Leeder return &iort_arm_smmu_v3_pmcg_cfg; 1768e4dadfa8SLorenzo Pieralisi default: 1769846f0e9eSLorenzo Pieralisi return NULL; 1770846f0e9eSLorenzo Pieralisi } 1771e4dadfa8SLorenzo Pieralisi } 1772846f0e9eSLorenzo Pieralisi 1773846f0e9eSLorenzo Pieralisi /** 1774896dd2c3SLorenzo Pieralisi * iort_add_platform_device() - Allocate a platform device for IORT node 1775896dd2c3SLorenzo Pieralisi * @node: Pointer to device ACPI IORT node 1776774c4a3bSShiju Jose * @ops: Pointer to IORT device config struct 1777846f0e9eSLorenzo Pieralisi * 1778846f0e9eSLorenzo Pieralisi * Returns: 0 on success, <0 failure 1779846f0e9eSLorenzo Pieralisi */ 1780896dd2c3SLorenzo Pieralisi static int __init iort_add_platform_device(struct acpi_iort_node *node, 1781896dd2c3SLorenzo Pieralisi const struct iort_dev_config *ops) 1782846f0e9eSLorenzo Pieralisi { 1783846f0e9eSLorenzo Pieralisi struct fwnode_handle *fwnode; 1784846f0e9eSLorenzo Pieralisi struct platform_device *pdev; 1785846f0e9eSLorenzo Pieralisi struct resource *r; 1786846f0e9eSLorenzo Pieralisi int ret, count; 1787846f0e9eSLorenzo Pieralisi 1788846f0e9eSLorenzo Pieralisi pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); 1789846f0e9eSLorenzo Pieralisi if (!pdev) 17905e5afa6cSDan Carpenter return -ENOMEM; 1791846f0e9eSLorenzo Pieralisi 179236a2ba07SKefeng Wang if (ops->dev_set_proximity) { 179336a2ba07SKefeng Wang ret = ops->dev_set_proximity(&pdev->dev, node); 179436a2ba07SKefeng Wang if (ret) 179536a2ba07SKefeng Wang goto dev_put; 179636a2ba07SKefeng Wang } 17975fe0ce3bSGanapatrao Kulkarni 1798896dd2c3SLorenzo Pieralisi count = ops->dev_count_resources(node); 1799846f0e9eSLorenzo Pieralisi 1800846f0e9eSLorenzo Pieralisi r = kcalloc(count, sizeof(*r), GFP_KERNEL); 1801846f0e9eSLorenzo Pieralisi if (!r) { 1802846f0e9eSLorenzo Pieralisi ret = -ENOMEM; 1803846f0e9eSLorenzo Pieralisi goto dev_put; 1804846f0e9eSLorenzo Pieralisi } 1805846f0e9eSLorenzo Pieralisi 1806896dd2c3SLorenzo Pieralisi ops->dev_init_resources(r, node); 1807846f0e9eSLorenzo Pieralisi 1808846f0e9eSLorenzo Pieralisi ret = platform_device_add_resources(pdev, r, count); 1809846f0e9eSLorenzo Pieralisi /* 1810846f0e9eSLorenzo Pieralisi * Resources are duplicated in platform_device_add_resources, 1811846f0e9eSLorenzo Pieralisi * free their allocated memory 1812846f0e9eSLorenzo Pieralisi */ 1813846f0e9eSLorenzo Pieralisi kfree(r); 1814846f0e9eSLorenzo Pieralisi 1815846f0e9eSLorenzo Pieralisi if (ret) 1816846f0e9eSLorenzo Pieralisi goto dev_put; 1817846f0e9eSLorenzo Pieralisi 1818846f0e9eSLorenzo Pieralisi /* 181924e51604SNeil Leeder * Platform devices based on PMCG nodes uses platform_data to 182024e51604SNeil Leeder * pass the hardware model info to the driver. For others, add 182124e51604SNeil Leeder * a copy of IORT node pointer to platform_data to be used to 182224e51604SNeil Leeder * retrieve IORT data information. 1823846f0e9eSLorenzo Pieralisi */ 182424e51604SNeil Leeder if (ops->dev_add_platdata) 182524e51604SNeil Leeder ret = ops->dev_add_platdata(pdev); 182624e51604SNeil Leeder else 1827846f0e9eSLorenzo Pieralisi ret = platform_device_add_data(pdev, &node, sizeof(node)); 182824e51604SNeil Leeder 1829846f0e9eSLorenzo Pieralisi if (ret) 1830846f0e9eSLorenzo Pieralisi goto dev_put; 1831846f0e9eSLorenzo Pieralisi 1832846f0e9eSLorenzo Pieralisi fwnode = iort_get_fwnode(node); 1833846f0e9eSLorenzo Pieralisi 1834846f0e9eSLorenzo Pieralisi if (!fwnode) { 1835846f0e9eSLorenzo Pieralisi ret = -ENODEV; 1836846f0e9eSLorenzo Pieralisi goto dev_put; 1837846f0e9eSLorenzo Pieralisi } 1838846f0e9eSLorenzo Pieralisi 1839846f0e9eSLorenzo Pieralisi pdev->dev.fwnode = fwnode; 1840846f0e9eSLorenzo Pieralisi 184124e51604SNeil Leeder if (ops->dev_dma_configure) 184224e51604SNeil Leeder ops->dev_dma_configure(&pdev->dev, node); 1843846f0e9eSLorenzo Pieralisi 184465637901SLorenzo Pieralisi iort_set_device_domain(&pdev->dev, node); 184565637901SLorenzo Pieralisi 1846846f0e9eSLorenzo Pieralisi ret = platform_device_add(pdev); 1847846f0e9eSLorenzo Pieralisi if (ret) 1848846f0e9eSLorenzo Pieralisi goto dma_deconfigure; 1849846f0e9eSLorenzo Pieralisi 1850846f0e9eSLorenzo Pieralisi return 0; 1851846f0e9eSLorenzo Pieralisi 1852846f0e9eSLorenzo Pieralisi dma_deconfigure: 1853dc3c0550SChristoph Hellwig arch_teardown_dma_ops(&pdev->dev); 1854846f0e9eSLorenzo Pieralisi dev_put: 1855846f0e9eSLorenzo Pieralisi platform_device_put(pdev); 1856846f0e9eSLorenzo Pieralisi 1857846f0e9eSLorenzo Pieralisi return ret; 1858846f0e9eSLorenzo Pieralisi } 1859846f0e9eSLorenzo Pieralisi 186043554cebSSinan Kaya #ifdef CONFIG_PCI 186143554cebSSinan Kaya static void __init iort_enable_acs(struct acpi_iort_node *iort_node) 186237f6b42eSLorenzo Pieralisi { 186343554cebSSinan Kaya static bool acs_enabled __initdata; 186443554cebSSinan Kaya 186543554cebSSinan Kaya if (acs_enabled) 186643554cebSSinan Kaya return; 186743554cebSSinan Kaya 186837f6b42eSLorenzo Pieralisi if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 186937f6b42eSLorenzo Pieralisi struct acpi_iort_node *parent; 187037f6b42eSLorenzo Pieralisi struct acpi_iort_id_mapping *map; 187137f6b42eSLorenzo Pieralisi int i; 187237f6b42eSLorenzo Pieralisi 187337f6b42eSLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, 187437f6b42eSLorenzo Pieralisi iort_node->mapping_offset); 187537f6b42eSLorenzo Pieralisi 187637f6b42eSLorenzo Pieralisi for (i = 0; i < iort_node->mapping_count; i++, map++) { 187737f6b42eSLorenzo Pieralisi if (!map->output_reference) 187837f6b42eSLorenzo Pieralisi continue; 187937f6b42eSLorenzo Pieralisi 188037f6b42eSLorenzo Pieralisi parent = ACPI_ADD_PTR(struct acpi_iort_node, 188137f6b42eSLorenzo Pieralisi iort_table, map->output_reference); 188237f6b42eSLorenzo Pieralisi /* 188337f6b42eSLorenzo Pieralisi * If we detect a RC->SMMU mapping, make sure 188437f6b42eSLorenzo Pieralisi * we enable ACS on the system. 188537f6b42eSLorenzo Pieralisi */ 188637f6b42eSLorenzo Pieralisi if ((parent->type == ACPI_IORT_NODE_SMMU) || 188737f6b42eSLorenzo Pieralisi (parent->type == ACPI_IORT_NODE_SMMU_V3)) { 188837f6b42eSLorenzo Pieralisi pci_request_acs(); 188943554cebSSinan Kaya acs_enabled = true; 189043554cebSSinan Kaya return; 189137f6b42eSLorenzo Pieralisi } 189237f6b42eSLorenzo Pieralisi } 189337f6b42eSLorenzo Pieralisi } 189437f6b42eSLorenzo Pieralisi } 189543554cebSSinan Kaya #else 189643554cebSSinan Kaya static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } 189743554cebSSinan Kaya #endif 189837f6b42eSLorenzo Pieralisi 1899846f0e9eSLorenzo Pieralisi static void __init iort_init_platform_devices(void) 1900846f0e9eSLorenzo Pieralisi { 1901846f0e9eSLorenzo Pieralisi struct acpi_iort_node *iort_node, *iort_end; 1902846f0e9eSLorenzo Pieralisi struct acpi_table_iort *iort; 1903846f0e9eSLorenzo Pieralisi struct fwnode_handle *fwnode; 1904846f0e9eSLorenzo Pieralisi int i, ret; 1905896dd2c3SLorenzo Pieralisi const struct iort_dev_config *ops; 1906846f0e9eSLorenzo Pieralisi 1907846f0e9eSLorenzo Pieralisi /* 1908846f0e9eSLorenzo Pieralisi * iort_table and iort both point to the start of IORT table, but 1909846f0e9eSLorenzo Pieralisi * have different struct types 1910846f0e9eSLorenzo Pieralisi */ 1911846f0e9eSLorenzo Pieralisi iort = (struct acpi_table_iort *)iort_table; 1912846f0e9eSLorenzo Pieralisi 1913846f0e9eSLorenzo Pieralisi /* Get the first IORT node */ 1914846f0e9eSLorenzo Pieralisi iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1915846f0e9eSLorenzo Pieralisi iort->node_offset); 1916846f0e9eSLorenzo Pieralisi iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1917846f0e9eSLorenzo Pieralisi iort_table->length); 1918846f0e9eSLorenzo Pieralisi 1919846f0e9eSLorenzo Pieralisi for (i = 0; i < iort->node_count; i++) { 1920846f0e9eSLorenzo Pieralisi if (iort_node >= iort_end) { 1921846f0e9eSLorenzo Pieralisi pr_err("iort node pointer overflows, bad table\n"); 1922846f0e9eSLorenzo Pieralisi return; 1923846f0e9eSLorenzo Pieralisi } 1924846f0e9eSLorenzo Pieralisi 192543554cebSSinan Kaya iort_enable_acs(iort_node); 192637f6b42eSLorenzo Pieralisi 1927896dd2c3SLorenzo Pieralisi ops = iort_get_dev_cfg(iort_node); 1928896dd2c3SLorenzo Pieralisi if (ops) { 1929846f0e9eSLorenzo Pieralisi fwnode = acpi_alloc_fwnode_static(); 1930846f0e9eSLorenzo Pieralisi if (!fwnode) 1931846f0e9eSLorenzo Pieralisi return; 1932846f0e9eSLorenzo Pieralisi 1933846f0e9eSLorenzo Pieralisi iort_set_fwnode(iort_node, fwnode); 1934846f0e9eSLorenzo Pieralisi 1935896dd2c3SLorenzo Pieralisi ret = iort_add_platform_device(iort_node, ops); 1936846f0e9eSLorenzo Pieralisi if (ret) { 1937846f0e9eSLorenzo Pieralisi iort_delete_fwnode(iort_node); 1938846f0e9eSLorenzo Pieralisi acpi_free_fwnode_static(fwnode); 1939846f0e9eSLorenzo Pieralisi return; 1940846f0e9eSLorenzo Pieralisi } 1941846f0e9eSLorenzo Pieralisi } 1942846f0e9eSLorenzo Pieralisi 1943846f0e9eSLorenzo Pieralisi iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1944846f0e9eSLorenzo Pieralisi iort_node->length); 1945846f0e9eSLorenzo Pieralisi } 1946846f0e9eSLorenzo Pieralisi } 1947846f0e9eSLorenzo Pieralisi 194888ef16d8STomasz Nowicki void __init acpi_iort_init(void) 194988ef16d8STomasz Nowicki { 195088ef16d8STomasz Nowicki acpi_status status; 195188ef16d8STomasz Nowicki 1952701dafe0SHanjun Guo /* iort_table will be used at runtime after the iort init, 1953701dafe0SHanjun Guo * so we don't need to call acpi_put_table() to release 1954701dafe0SHanjun Guo * the IORT table mapping. 1955701dafe0SHanjun Guo */ 195688ef16d8STomasz Nowicki status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); 195734ceea27SLorenzo Pieralisi if (ACPI_FAILURE(status)) { 195834ceea27SLorenzo Pieralisi if (status != AE_NOT_FOUND) { 195988ef16d8STomasz Nowicki const char *msg = acpi_format_exception(status); 196034ceea27SLorenzo Pieralisi 196188ef16d8STomasz Nowicki pr_err("Failed to get table, %s\n", msg); 196288ef16d8STomasz Nowicki } 196334ceea27SLorenzo Pieralisi 196434ceea27SLorenzo Pieralisi return; 196534ceea27SLorenzo Pieralisi } 196634ceea27SLorenzo Pieralisi 1967846f0e9eSLorenzo Pieralisi iort_init_platform_devices(); 196888ef16d8STomasz Nowicki } 19692b865293SArd Biesheuvel 19702b865293SArd Biesheuvel #ifdef CONFIG_ZONE_DMA 19712b865293SArd Biesheuvel /* 19722b865293SArd Biesheuvel * Extract the highest CPU physical address accessible to all DMA masters in 19732b865293SArd Biesheuvel * the system. PHYS_ADDR_MAX is returned when no constrained device is found. 19742b865293SArd Biesheuvel */ 19752b865293SArd Biesheuvel phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void) 19762b865293SArd Biesheuvel { 19772b865293SArd Biesheuvel phys_addr_t limit = PHYS_ADDR_MAX; 19782b865293SArd Biesheuvel struct acpi_iort_node *node, *end; 19792b865293SArd Biesheuvel struct acpi_table_iort *iort; 19802b865293SArd Biesheuvel acpi_status status; 19812b865293SArd Biesheuvel int i; 19822b865293SArd Biesheuvel 19832b865293SArd Biesheuvel if (acpi_disabled) 19842b865293SArd Biesheuvel return limit; 19852b865293SArd Biesheuvel 19862b865293SArd Biesheuvel status = acpi_get_table(ACPI_SIG_IORT, 0, 19872b865293SArd Biesheuvel (struct acpi_table_header **)&iort); 19882b865293SArd Biesheuvel if (ACPI_FAILURE(status)) 19892b865293SArd Biesheuvel return limit; 19902b865293SArd Biesheuvel 19912b865293SArd Biesheuvel node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset); 19922b865293SArd Biesheuvel end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length); 19932b865293SArd Biesheuvel 19942b865293SArd Biesheuvel for (i = 0; i < iort->node_count; i++) { 19952b865293SArd Biesheuvel if (node >= end) 19962b865293SArd Biesheuvel break; 19972b865293SArd Biesheuvel 19982b865293SArd Biesheuvel switch (node->type) { 19992b865293SArd Biesheuvel struct acpi_iort_named_component *ncomp; 20002b865293SArd Biesheuvel struct acpi_iort_root_complex *rc; 20012b865293SArd Biesheuvel phys_addr_t local_limit; 20022b865293SArd Biesheuvel 20032b865293SArd Biesheuvel case ACPI_IORT_NODE_NAMED_COMPONENT: 20042b865293SArd Biesheuvel ncomp = (struct acpi_iort_named_component *)node->node_data; 20052b865293SArd Biesheuvel local_limit = DMA_BIT_MASK(ncomp->memory_address_limit); 20062b865293SArd Biesheuvel limit = min_not_zero(limit, local_limit); 20072b865293SArd Biesheuvel break; 20082b865293SArd Biesheuvel 20092b865293SArd Biesheuvel case ACPI_IORT_NODE_PCI_ROOT_COMPLEX: 20102b865293SArd Biesheuvel if (node->revision < 1) 20112b865293SArd Biesheuvel break; 20122b865293SArd Biesheuvel 20132b865293SArd Biesheuvel rc = (struct acpi_iort_root_complex *)node->node_data; 20142b865293SArd Biesheuvel local_limit = DMA_BIT_MASK(rc->memory_address_limit); 20152b865293SArd Biesheuvel limit = min_not_zero(limit, local_limit); 20162b865293SArd Biesheuvel break; 20172b865293SArd Biesheuvel } 20182b865293SArd Biesheuvel node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length); 20192b865293SArd Biesheuvel } 20202b865293SArd Biesheuvel acpi_put_table(&iort->header); 20212b865293SArd Biesheuvel return limit; 20222b865293SArd Biesheuvel } 20232b865293SArd Biesheuvel #endif 2024