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> 2288ef16d8STomasz Nowicki 23ea50b524SLorenzo Pieralisi #define IORT_TYPE_MASK(type) (1 << (type)) 24ea50b524SLorenzo Pieralisi #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) 25643b8e4dSLorenzo Pieralisi #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ 26643b8e4dSLorenzo Pieralisi (1 << ACPI_IORT_NODE_SMMU_V3)) 27ea50b524SLorenzo Pieralisi 284bf2efd2STomasz Nowicki struct iort_its_msi_chip { 294bf2efd2STomasz Nowicki struct list_head list; 304bf2efd2STomasz Nowicki struct fwnode_handle *fw_node; 318b4282e6SShameer Kolothum phys_addr_t base_addr; 324bf2efd2STomasz Nowicki u32 translation_id; 334bf2efd2STomasz Nowicki }; 344bf2efd2STomasz Nowicki 357936df92SLorenzo Pieralisi struct iort_fwnode { 367936df92SLorenzo Pieralisi struct list_head list; 377936df92SLorenzo Pieralisi struct acpi_iort_node *iort_node; 387936df92SLorenzo Pieralisi struct fwnode_handle *fwnode; 397936df92SLorenzo Pieralisi }; 407936df92SLorenzo Pieralisi static LIST_HEAD(iort_fwnode_list); 417936df92SLorenzo Pieralisi static DEFINE_SPINLOCK(iort_fwnode_lock); 427936df92SLorenzo Pieralisi 437936df92SLorenzo Pieralisi /** 447936df92SLorenzo Pieralisi * iort_set_fwnode() - Create iort_fwnode and use it to register 457936df92SLorenzo Pieralisi * iommu data in the iort_fwnode_list 467936df92SLorenzo Pieralisi * 47774c4a3bSShiju Jose * @iort_node: IORT table node associated with the IOMMU 487936df92SLorenzo Pieralisi * @fwnode: fwnode associated with the IORT node 497936df92SLorenzo Pieralisi * 507936df92SLorenzo Pieralisi * Returns: 0 on success 517936df92SLorenzo Pieralisi * <0 on failure 527936df92SLorenzo Pieralisi */ 537936df92SLorenzo Pieralisi static inline int iort_set_fwnode(struct acpi_iort_node *iort_node, 547936df92SLorenzo Pieralisi struct fwnode_handle *fwnode) 557936df92SLorenzo Pieralisi { 567936df92SLorenzo Pieralisi struct iort_fwnode *np; 577936df92SLorenzo Pieralisi 587936df92SLorenzo Pieralisi np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC); 597936df92SLorenzo Pieralisi 607936df92SLorenzo Pieralisi if (WARN_ON(!np)) 617936df92SLorenzo Pieralisi return -ENOMEM; 627936df92SLorenzo Pieralisi 637936df92SLorenzo Pieralisi INIT_LIST_HEAD(&np->list); 647936df92SLorenzo Pieralisi np->iort_node = iort_node; 657936df92SLorenzo Pieralisi np->fwnode = fwnode; 667936df92SLorenzo Pieralisi 677936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 687936df92SLorenzo Pieralisi list_add_tail(&np->list, &iort_fwnode_list); 697936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 707936df92SLorenzo Pieralisi 717936df92SLorenzo Pieralisi return 0; 727936df92SLorenzo Pieralisi } 737936df92SLorenzo Pieralisi 747936df92SLorenzo Pieralisi /** 757936df92SLorenzo Pieralisi * iort_get_fwnode() - Retrieve fwnode associated with an IORT node 767936df92SLorenzo Pieralisi * 777936df92SLorenzo Pieralisi * @node: IORT table node to be looked-up 787936df92SLorenzo Pieralisi * 797936df92SLorenzo Pieralisi * Returns: fwnode_handle pointer on success, NULL on failure 807936df92SLorenzo Pieralisi */ 81e3d49392SLorenzo Pieralisi static inline struct fwnode_handle *iort_get_fwnode( 82e3d49392SLorenzo Pieralisi struct acpi_iort_node *node) 837936df92SLorenzo Pieralisi { 847936df92SLorenzo Pieralisi struct iort_fwnode *curr; 857936df92SLorenzo Pieralisi struct fwnode_handle *fwnode = NULL; 867936df92SLorenzo Pieralisi 877936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 887936df92SLorenzo Pieralisi list_for_each_entry(curr, &iort_fwnode_list, list) { 897936df92SLorenzo Pieralisi if (curr->iort_node == node) { 907936df92SLorenzo Pieralisi fwnode = curr->fwnode; 917936df92SLorenzo Pieralisi break; 927936df92SLorenzo Pieralisi } 937936df92SLorenzo Pieralisi } 947936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 957936df92SLorenzo Pieralisi 967936df92SLorenzo Pieralisi return fwnode; 977936df92SLorenzo Pieralisi } 987936df92SLorenzo Pieralisi 997936df92SLorenzo Pieralisi /** 1007936df92SLorenzo Pieralisi * iort_delete_fwnode() - Delete fwnode associated with an IORT node 1017936df92SLorenzo Pieralisi * 1027936df92SLorenzo Pieralisi * @node: IORT table node associated with fwnode to delete 1037936df92SLorenzo Pieralisi */ 1047936df92SLorenzo Pieralisi static inline void iort_delete_fwnode(struct acpi_iort_node *node) 1057936df92SLorenzo Pieralisi { 1067936df92SLorenzo Pieralisi struct iort_fwnode *curr, *tmp; 1077936df92SLorenzo Pieralisi 1087936df92SLorenzo Pieralisi spin_lock(&iort_fwnode_lock); 1097936df92SLorenzo Pieralisi list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) { 1107936df92SLorenzo Pieralisi if (curr->iort_node == node) { 1117936df92SLorenzo Pieralisi list_del(&curr->list); 1127936df92SLorenzo Pieralisi kfree(curr); 1137936df92SLorenzo Pieralisi break; 1147936df92SLorenzo Pieralisi } 1157936df92SLorenzo Pieralisi } 1167936df92SLorenzo Pieralisi spin_unlock(&iort_fwnode_lock); 1177936df92SLorenzo Pieralisi } 1187936df92SLorenzo Pieralisi 1190a71d8b9SHanjun Guo /** 1200a71d8b9SHanjun Guo * iort_get_iort_node() - Retrieve iort_node associated with an fwnode 1210a71d8b9SHanjun Guo * 1220a71d8b9SHanjun Guo * @fwnode: fwnode associated with device to be looked-up 1230a71d8b9SHanjun Guo * 1240a71d8b9SHanjun Guo * Returns: iort_node pointer on success, NULL on failure 1250a71d8b9SHanjun Guo */ 1260a71d8b9SHanjun Guo static inline struct acpi_iort_node *iort_get_iort_node( 1270a71d8b9SHanjun Guo struct fwnode_handle *fwnode) 1280a71d8b9SHanjun Guo { 1290a71d8b9SHanjun Guo struct iort_fwnode *curr; 1300a71d8b9SHanjun Guo struct acpi_iort_node *iort_node = NULL; 1310a71d8b9SHanjun Guo 1320a71d8b9SHanjun Guo spin_lock(&iort_fwnode_lock); 1330a71d8b9SHanjun Guo list_for_each_entry(curr, &iort_fwnode_list, list) { 1340a71d8b9SHanjun Guo if (curr->fwnode == fwnode) { 1350a71d8b9SHanjun Guo iort_node = curr->iort_node; 1360a71d8b9SHanjun Guo break; 1370a71d8b9SHanjun Guo } 1380a71d8b9SHanjun Guo } 1390a71d8b9SHanjun Guo spin_unlock(&iort_fwnode_lock); 1400a71d8b9SHanjun Guo 1410a71d8b9SHanjun Guo return iort_node; 1420a71d8b9SHanjun Guo } 1430a71d8b9SHanjun Guo 14488ef16d8STomasz Nowicki typedef acpi_status (*iort_find_node_callback) 14588ef16d8STomasz Nowicki (struct acpi_iort_node *node, void *context); 14688ef16d8STomasz Nowicki 14788ef16d8STomasz Nowicki /* Root pointer to the mapped IORT table */ 14888ef16d8STomasz Nowicki static struct acpi_table_header *iort_table; 14988ef16d8STomasz Nowicki 15088ef16d8STomasz Nowicki static LIST_HEAD(iort_msi_chip_list); 15188ef16d8STomasz Nowicki static DEFINE_SPINLOCK(iort_msi_chip_lock); 15288ef16d8STomasz Nowicki 1534bf2efd2STomasz Nowicki /** 1548b4282e6SShameer Kolothum * iort_register_domain_token() - register domain token along with related 1558b4282e6SShameer Kolothum * ITS ID and base address to the list from where we can get it back later on. 1564bf2efd2STomasz Nowicki * @trans_id: ITS ID. 1578b4282e6SShameer Kolothum * @base: ITS base address. 1584bf2efd2STomasz Nowicki * @fw_node: Domain token. 1594bf2efd2STomasz Nowicki * 1604bf2efd2STomasz Nowicki * Returns: 0 on success, -ENOMEM if no memory when allocating list element 1614bf2efd2STomasz Nowicki */ 1628b4282e6SShameer Kolothum int iort_register_domain_token(int trans_id, phys_addr_t base, 1638b4282e6SShameer Kolothum struct fwnode_handle *fw_node) 1644bf2efd2STomasz Nowicki { 1654bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip; 1664bf2efd2STomasz Nowicki 1674bf2efd2STomasz Nowicki its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL); 1684bf2efd2STomasz Nowicki if (!its_msi_chip) 1694bf2efd2STomasz Nowicki return -ENOMEM; 1704bf2efd2STomasz Nowicki 1714bf2efd2STomasz Nowicki its_msi_chip->fw_node = fw_node; 1724bf2efd2STomasz Nowicki its_msi_chip->translation_id = trans_id; 1738b4282e6SShameer Kolothum its_msi_chip->base_addr = base; 1744bf2efd2STomasz Nowicki 1754bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 1764bf2efd2STomasz Nowicki list_add(&its_msi_chip->list, &iort_msi_chip_list); 1774bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 1784bf2efd2STomasz Nowicki 1794bf2efd2STomasz Nowicki return 0; 1804bf2efd2STomasz Nowicki } 1814bf2efd2STomasz Nowicki 1824bf2efd2STomasz Nowicki /** 1834bf2efd2STomasz Nowicki * iort_deregister_domain_token() - Deregister domain token based on ITS ID 1844bf2efd2STomasz Nowicki * @trans_id: ITS ID. 1854bf2efd2STomasz Nowicki * 1864bf2efd2STomasz Nowicki * Returns: none. 1874bf2efd2STomasz Nowicki */ 1884bf2efd2STomasz Nowicki void iort_deregister_domain_token(int trans_id) 1894bf2efd2STomasz Nowicki { 1904bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip, *t; 1914bf2efd2STomasz Nowicki 1924bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 1934bf2efd2STomasz Nowicki list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { 1944bf2efd2STomasz Nowicki if (its_msi_chip->translation_id == trans_id) { 1954bf2efd2STomasz Nowicki list_del(&its_msi_chip->list); 1964bf2efd2STomasz Nowicki kfree(its_msi_chip); 1974bf2efd2STomasz Nowicki break; 1984bf2efd2STomasz Nowicki } 1994bf2efd2STomasz Nowicki } 2004bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 2014bf2efd2STomasz Nowicki } 2024bf2efd2STomasz Nowicki 2034bf2efd2STomasz Nowicki /** 2044bf2efd2STomasz Nowicki * iort_find_domain_token() - Find domain token based on given ITS ID 2054bf2efd2STomasz Nowicki * @trans_id: ITS ID. 2064bf2efd2STomasz Nowicki * 2074bf2efd2STomasz Nowicki * Returns: domain token when find on the list, NULL otherwise 2084bf2efd2STomasz Nowicki */ 2094bf2efd2STomasz Nowicki struct fwnode_handle *iort_find_domain_token(int trans_id) 2104bf2efd2STomasz Nowicki { 2114bf2efd2STomasz Nowicki struct fwnode_handle *fw_node = NULL; 2124bf2efd2STomasz Nowicki struct iort_its_msi_chip *its_msi_chip; 2134bf2efd2STomasz Nowicki 2144bf2efd2STomasz Nowicki spin_lock(&iort_msi_chip_lock); 2154bf2efd2STomasz Nowicki list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 2164bf2efd2STomasz Nowicki if (its_msi_chip->translation_id == trans_id) { 2174bf2efd2STomasz Nowicki fw_node = its_msi_chip->fw_node; 2184bf2efd2STomasz Nowicki break; 2194bf2efd2STomasz Nowicki } 2204bf2efd2STomasz Nowicki } 2214bf2efd2STomasz Nowicki spin_unlock(&iort_msi_chip_lock); 2224bf2efd2STomasz Nowicki 2234bf2efd2STomasz Nowicki return fw_node; 2244bf2efd2STomasz Nowicki } 2254bf2efd2STomasz Nowicki 22688ef16d8STomasz Nowicki static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, 22788ef16d8STomasz Nowicki iort_find_node_callback callback, 22888ef16d8STomasz Nowicki void *context) 22988ef16d8STomasz Nowicki { 23088ef16d8STomasz Nowicki struct acpi_iort_node *iort_node, *iort_end; 23188ef16d8STomasz Nowicki struct acpi_table_iort *iort; 23288ef16d8STomasz Nowicki int i; 23388ef16d8STomasz Nowicki 23488ef16d8STomasz Nowicki if (!iort_table) 23588ef16d8STomasz Nowicki return NULL; 23688ef16d8STomasz Nowicki 23788ef16d8STomasz Nowicki /* Get the first IORT node */ 23888ef16d8STomasz Nowicki iort = (struct acpi_table_iort *)iort_table; 23988ef16d8STomasz Nowicki iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 24088ef16d8STomasz Nowicki iort->node_offset); 24188ef16d8STomasz Nowicki iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 24288ef16d8STomasz Nowicki iort_table->length); 24388ef16d8STomasz Nowicki 24488ef16d8STomasz Nowicki for (i = 0; i < iort->node_count; i++) { 24588ef16d8STomasz Nowicki if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 24688ef16d8STomasz Nowicki "IORT node pointer overflows, bad table!\n")) 24788ef16d8STomasz Nowicki return NULL; 24888ef16d8STomasz Nowicki 24988ef16d8STomasz Nowicki if (iort_node->type == type && 25088ef16d8STomasz Nowicki ACPI_SUCCESS(callback(iort_node, context))) 25188ef16d8STomasz Nowicki return iort_node; 25288ef16d8STomasz Nowicki 25388ef16d8STomasz Nowicki iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 25488ef16d8STomasz Nowicki iort_node->length); 25588ef16d8STomasz Nowicki } 25688ef16d8STomasz Nowicki 25788ef16d8STomasz Nowicki return NULL; 25888ef16d8STomasz Nowicki } 25988ef16d8STomasz Nowicki 26088ef16d8STomasz Nowicki static acpi_status iort_match_node_callback(struct acpi_iort_node *node, 26188ef16d8STomasz Nowicki void *context) 26288ef16d8STomasz Nowicki { 26388ef16d8STomasz Nowicki struct device *dev = context; 264c92bdfe8SHanjun Guo acpi_status status = AE_NOT_FOUND; 26588ef16d8STomasz Nowicki 26688ef16d8STomasz Nowicki if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { 26788ef16d8STomasz Nowicki struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 26807d2e59fSLorenzo Pieralisi struct acpi_device *adev; 26988ef16d8STomasz Nowicki struct acpi_iort_named_component *ncomp; 27007d2e59fSLorenzo Pieralisi struct device *nc_dev = dev; 27107d2e59fSLorenzo Pieralisi 27207d2e59fSLorenzo Pieralisi /* 27307d2e59fSLorenzo Pieralisi * Walk the device tree to find a device with an 27407d2e59fSLorenzo Pieralisi * ACPI companion; there is no point in scanning 27507d2e59fSLorenzo Pieralisi * IORT for a device matching a named component if 27607d2e59fSLorenzo Pieralisi * the device does not have an ACPI companion to 27707d2e59fSLorenzo Pieralisi * start with. 27807d2e59fSLorenzo Pieralisi */ 27907d2e59fSLorenzo Pieralisi do { 28007d2e59fSLorenzo Pieralisi adev = ACPI_COMPANION(nc_dev); 28107d2e59fSLorenzo Pieralisi if (adev) 28207d2e59fSLorenzo Pieralisi break; 28307d2e59fSLorenzo Pieralisi 28407d2e59fSLorenzo Pieralisi nc_dev = nc_dev->parent; 28507d2e59fSLorenzo Pieralisi } while (nc_dev); 28688ef16d8STomasz Nowicki 287c92bdfe8SHanjun Guo if (!adev) 28888ef16d8STomasz Nowicki goto out; 28988ef16d8STomasz Nowicki 29088ef16d8STomasz Nowicki status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); 29188ef16d8STomasz Nowicki if (ACPI_FAILURE(status)) { 29207d2e59fSLorenzo Pieralisi dev_warn(nc_dev, "Can't get device full path name\n"); 29388ef16d8STomasz Nowicki goto out; 29488ef16d8STomasz Nowicki } 29588ef16d8STomasz Nowicki 29688ef16d8STomasz Nowicki ncomp = (struct acpi_iort_named_component *)node->node_data; 29788ef16d8STomasz Nowicki status = !strcmp(ncomp->device_name, buf.pointer) ? 29888ef16d8STomasz Nowicki AE_OK : AE_NOT_FOUND; 29988ef16d8STomasz Nowicki acpi_os_free(buf.pointer); 30088ef16d8STomasz Nowicki } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 30188ef16d8STomasz Nowicki struct acpi_iort_root_complex *pci_rc; 30288ef16d8STomasz Nowicki struct pci_bus *bus; 30388ef16d8STomasz Nowicki 30488ef16d8STomasz Nowicki bus = to_pci_bus(dev); 30588ef16d8STomasz Nowicki pci_rc = (struct acpi_iort_root_complex *)node->node_data; 30688ef16d8STomasz Nowicki 30788ef16d8STomasz Nowicki /* 30888ef16d8STomasz Nowicki * It is assumed that PCI segment numbers maps one-to-one 30988ef16d8STomasz Nowicki * with root complexes. Each segment number can represent only 31088ef16d8STomasz Nowicki * one root complex. 31188ef16d8STomasz Nowicki */ 31288ef16d8STomasz Nowicki status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? 31388ef16d8STomasz Nowicki AE_OK : AE_NOT_FOUND; 31488ef16d8STomasz Nowicki } 31588ef16d8STomasz Nowicki out: 31688ef16d8STomasz Nowicki return status; 31788ef16d8STomasz Nowicki } 31888ef16d8STomasz Nowicki 31988ef16d8STomasz Nowicki static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, 320539979b6SArd Biesheuvel u32 *rid_out, bool check_overlap) 32188ef16d8STomasz Nowicki { 32288ef16d8STomasz Nowicki /* Single mapping does not care for input id */ 32388ef16d8STomasz Nowicki if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 32488ef16d8STomasz Nowicki if (type == ACPI_IORT_NODE_NAMED_COMPONENT || 32588ef16d8STomasz Nowicki type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 32688ef16d8STomasz Nowicki *rid_out = map->output_base; 32788ef16d8STomasz Nowicki return 0; 32888ef16d8STomasz Nowicki } 32988ef16d8STomasz Nowicki 33088ef16d8STomasz Nowicki pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", 33188ef16d8STomasz Nowicki map, type); 33288ef16d8STomasz Nowicki return -ENXIO; 33388ef16d8STomasz Nowicki } 33488ef16d8STomasz Nowicki 3356d3b29d0SArd Biesheuvel if (rid_in < map->input_base || 336539979b6SArd Biesheuvel (rid_in > map->input_base + map->id_count)) 33788ef16d8STomasz Nowicki return -ENXIO; 33888ef16d8STomasz Nowicki 339539979b6SArd Biesheuvel if (check_overlap) { 340539979b6SArd Biesheuvel /* 341539979b6SArd Biesheuvel * We already found a mapping for this input ID at the end of 342539979b6SArd Biesheuvel * another region. If it coincides with the start of this 343539979b6SArd Biesheuvel * region, we assume the prior match was due to the off-by-1 344539979b6SArd Biesheuvel * issue mentioned below, and allow it to be superseded. 345539979b6SArd Biesheuvel * Otherwise, things are *really* broken, and we just disregard 346539979b6SArd Biesheuvel * duplicate matches entirely to retain compatibility. 347539979b6SArd Biesheuvel */ 348539979b6SArd Biesheuvel pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n", 349539979b6SArd Biesheuvel map, rid_in); 350539979b6SArd Biesheuvel if (rid_in != map->input_base) 351539979b6SArd Biesheuvel return -ENXIO; 35244cdc7b1SHanjun Guo 35344cdc7b1SHanjun Guo pr_err(FW_BUG "applying workaround.\n"); 354539979b6SArd Biesheuvel } 355539979b6SArd Biesheuvel 35688ef16d8STomasz Nowicki *rid_out = map->output_base + (rid_in - map->input_base); 357539979b6SArd Biesheuvel 358539979b6SArd Biesheuvel /* 359539979b6SArd Biesheuvel * Due to confusion regarding the meaning of the id_count field (which 360539979b6SArd Biesheuvel * carries the number of IDs *minus 1*), we may have to disregard this 361539979b6SArd Biesheuvel * match if it is at the end of the range, and overlaps with the start 362539979b6SArd Biesheuvel * of another one. 363539979b6SArd Biesheuvel */ 364539979b6SArd Biesheuvel if (map->id_count > 0 && rid_in == map->input_base + map->id_count) 365539979b6SArd Biesheuvel return -EAGAIN; 36688ef16d8STomasz Nowicki return 0; 36788ef16d8STomasz Nowicki } 36888ef16d8STomasz Nowicki 369e3d49392SLorenzo Pieralisi static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, 3708ca4f1d3SHanjun Guo u32 *id_out, int index) 371618f535aSLorenzo Pieralisi { 372618f535aSLorenzo Pieralisi struct acpi_iort_node *parent; 373618f535aSLorenzo Pieralisi struct acpi_iort_id_mapping *map; 374618f535aSLorenzo Pieralisi 375618f535aSLorenzo Pieralisi if (!node->mapping_offset || !node->mapping_count || 376618f535aSLorenzo Pieralisi index >= node->mapping_count) 377618f535aSLorenzo Pieralisi return NULL; 378618f535aSLorenzo Pieralisi 379618f535aSLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 380030abd8aSLorenzo Pieralisi node->mapping_offset + index * sizeof(*map)); 381618f535aSLorenzo Pieralisi 382618f535aSLorenzo Pieralisi /* Firmware bug! */ 383618f535aSLorenzo Pieralisi if (!map->output_reference) { 384618f535aSLorenzo Pieralisi pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 385618f535aSLorenzo Pieralisi node, node->type); 386618f535aSLorenzo Pieralisi return NULL; 387618f535aSLorenzo Pieralisi } 388618f535aSLorenzo Pieralisi 389618f535aSLorenzo Pieralisi parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 390618f535aSLorenzo Pieralisi map->output_reference); 391618f535aSLorenzo Pieralisi 392030abd8aSLorenzo Pieralisi if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 393618f535aSLorenzo Pieralisi if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || 39486456a3fSHanjun Guo node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || 39524e51604SNeil Leeder node->type == ACPI_IORT_NODE_SMMU_V3 || 39624e51604SNeil Leeder node->type == ACPI_IORT_NODE_PMCG) { 397030abd8aSLorenzo Pieralisi *id_out = map->output_base; 398618f535aSLorenzo Pieralisi return parent; 399618f535aSLorenzo Pieralisi } 400618f535aSLorenzo Pieralisi } 401618f535aSLorenzo Pieralisi 402618f535aSLorenzo Pieralisi return NULL; 403618f535aSLorenzo Pieralisi } 404618f535aSLorenzo Pieralisi 40586456a3fSHanjun Guo static int iort_get_id_mapping_index(struct acpi_iort_node *node) 40686456a3fSHanjun Guo { 40786456a3fSHanjun Guo struct acpi_iort_smmu_v3 *smmu; 40850c8ab8dSTuan Phan struct acpi_iort_pmcg *pmcg; 40986456a3fSHanjun Guo 41086456a3fSHanjun Guo switch (node->type) { 41186456a3fSHanjun Guo case ACPI_IORT_NODE_SMMU_V3: 41286456a3fSHanjun Guo /* 41386456a3fSHanjun Guo * SMMUv3 dev ID mapping index was introduced in revision 1 41486456a3fSHanjun Guo * table, not available in revision 0 41586456a3fSHanjun Guo */ 41686456a3fSHanjun Guo if (node->revision < 1) 41786456a3fSHanjun Guo return -EINVAL; 41886456a3fSHanjun Guo 41986456a3fSHanjun Guo smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 42086456a3fSHanjun Guo /* 42186456a3fSHanjun Guo * ID mapping index is only ignored if all interrupts are 42286456a3fSHanjun Guo * GSIV based 42386456a3fSHanjun Guo */ 42486456a3fSHanjun Guo if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv 42586456a3fSHanjun Guo && smmu->sync_gsiv) 42686456a3fSHanjun Guo return -EINVAL; 42786456a3fSHanjun Guo 42886456a3fSHanjun Guo if (smmu->id_mapping_index >= node->mapping_count) { 42986456a3fSHanjun Guo pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", 43086456a3fSHanjun Guo node, node->type); 43186456a3fSHanjun Guo return -EINVAL; 43286456a3fSHanjun Guo } 43386456a3fSHanjun Guo 43486456a3fSHanjun Guo return smmu->id_mapping_index; 43524e51604SNeil Leeder case ACPI_IORT_NODE_PMCG: 43650c8ab8dSTuan Phan pmcg = (struct acpi_iort_pmcg *)node->node_data; 43750c8ab8dSTuan Phan if (pmcg->overflow_gsiv || node->mapping_count == 0) 43850c8ab8dSTuan Phan return -EINVAL; 43950c8ab8dSTuan Phan 44024e51604SNeil Leeder return 0; 44186456a3fSHanjun Guo default: 44286456a3fSHanjun Guo return -EINVAL; 44386456a3fSHanjun Guo } 44486456a3fSHanjun Guo } 4458c8df8dcSHanjun Guo 446697f6093SHanjun Guo static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, 447697f6093SHanjun Guo u32 id_in, u32 *id_out, 448ea50b524SLorenzo Pieralisi u8 type_mask) 44988ef16d8STomasz Nowicki { 450697f6093SHanjun Guo u32 id = id_in; 45188ef16d8STomasz Nowicki 45288ef16d8STomasz Nowicki /* Parse the ID mapping tree to find specified node type */ 45388ef16d8STomasz Nowicki while (node) { 45488ef16d8STomasz Nowicki struct acpi_iort_id_mapping *map; 455539979b6SArd Biesheuvel int i, index, rc = 0; 456539979b6SArd Biesheuvel u32 out_ref = 0, map_id = id; 45788ef16d8STomasz Nowicki 458ea50b524SLorenzo Pieralisi if (IORT_TYPE_MASK(node->type) & type_mask) { 459697f6093SHanjun Guo if (id_out) 460697f6093SHanjun Guo *id_out = id; 46188ef16d8STomasz Nowicki return node; 46288ef16d8STomasz Nowicki } 46388ef16d8STomasz Nowicki 46488ef16d8STomasz Nowicki if (!node->mapping_offset || !node->mapping_count) 46588ef16d8STomasz Nowicki goto fail_map; 46688ef16d8STomasz Nowicki 46788ef16d8STomasz Nowicki map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 46888ef16d8STomasz Nowicki node->mapping_offset); 46988ef16d8STomasz Nowicki 47088ef16d8STomasz Nowicki /* Firmware bug! */ 47188ef16d8STomasz Nowicki if (!map->output_reference) { 47288ef16d8STomasz Nowicki pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 47388ef16d8STomasz Nowicki node, node->type); 47488ef16d8STomasz Nowicki goto fail_map; 47588ef16d8STomasz Nowicki } 47688ef16d8STomasz Nowicki 4778c8df8dcSHanjun Guo /* 4788c8df8dcSHanjun Guo * Get the special ID mapping index (if any) and skip its 4798c8df8dcSHanjun Guo * associated ID map to prevent erroneous multi-stage 4808c8df8dcSHanjun Guo * IORT ID translations. 4818c8df8dcSHanjun Guo */ 4828c8df8dcSHanjun Guo index = iort_get_id_mapping_index(node); 4838c8df8dcSHanjun Guo 484697f6093SHanjun Guo /* Do the ID translation */ 48588ef16d8STomasz Nowicki for (i = 0; i < node->mapping_count; i++, map++) { 4868c8df8dcSHanjun Guo /* if it is special mapping index, skip it */ 4878c8df8dcSHanjun Guo if (i == index) 4888c8df8dcSHanjun Guo continue; 4898c8df8dcSHanjun Guo 490539979b6SArd Biesheuvel rc = iort_id_map(map, node->type, map_id, &id, out_ref); 491539979b6SArd Biesheuvel if (!rc) 49288ef16d8STomasz Nowicki break; 493539979b6SArd Biesheuvel if (rc == -EAGAIN) 494539979b6SArd Biesheuvel out_ref = map->output_reference; 49588ef16d8STomasz Nowicki } 49688ef16d8STomasz Nowicki 497539979b6SArd Biesheuvel if (i == node->mapping_count && !out_ref) 49888ef16d8STomasz Nowicki goto fail_map; 49988ef16d8STomasz Nowicki 50088ef16d8STomasz Nowicki node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 501539979b6SArd Biesheuvel rc ? out_ref : map->output_reference); 50288ef16d8STomasz Nowicki } 50388ef16d8STomasz Nowicki 50488ef16d8STomasz Nowicki fail_map: 505697f6093SHanjun Guo /* Map input ID to output ID unchanged on mapping failure */ 506697f6093SHanjun Guo if (id_out) 507697f6093SHanjun Guo *id_out = id_in; 50888ef16d8STomasz Nowicki 50988ef16d8STomasz Nowicki return NULL; 51088ef16d8STomasz Nowicki } 51188ef16d8STomasz Nowicki 512e3d49392SLorenzo Pieralisi static struct acpi_iort_node *iort_node_map_platform_id( 513e3d49392SLorenzo Pieralisi struct acpi_iort_node *node, u32 *id_out, u8 type_mask, 5148ca4f1d3SHanjun Guo int index) 5158ca4f1d3SHanjun Guo { 5168ca4f1d3SHanjun Guo struct acpi_iort_node *parent; 5178ca4f1d3SHanjun Guo u32 id; 5188ca4f1d3SHanjun Guo 5198ca4f1d3SHanjun Guo /* step 1: retrieve the initial dev id */ 5208ca4f1d3SHanjun Guo parent = iort_node_get_id(node, &id, index); 5218ca4f1d3SHanjun Guo if (!parent) 5228ca4f1d3SHanjun Guo return NULL; 5238ca4f1d3SHanjun Guo 5248ca4f1d3SHanjun Guo /* 5258ca4f1d3SHanjun Guo * optional step 2: map the initial dev id if its parent is not 5268ca4f1d3SHanjun Guo * the target type we want, map it again for the use cases such 5278ca4f1d3SHanjun Guo * as NC (named component) -> SMMU -> ITS. If the type is matched, 5288ca4f1d3SHanjun Guo * return the initial dev id and its parent pointer directly. 5298ca4f1d3SHanjun Guo */ 5308ca4f1d3SHanjun Guo if (!(IORT_TYPE_MASK(parent->type) & type_mask)) 5318ca4f1d3SHanjun Guo parent = iort_node_map_id(parent, id, id_out, type_mask); 5328ca4f1d3SHanjun Guo else 5338ca4f1d3SHanjun Guo if (id_out) 5348ca4f1d3SHanjun Guo *id_out = id; 5358ca4f1d3SHanjun Guo 5368ca4f1d3SHanjun Guo return parent; 5378ca4f1d3SHanjun Guo } 5388ca4f1d3SHanjun Guo 53988ef16d8STomasz Nowicki static struct acpi_iort_node *iort_find_dev_node(struct device *dev) 54088ef16d8STomasz Nowicki { 54188ef16d8STomasz Nowicki struct pci_bus *pbus; 54288ef16d8STomasz Nowicki 5430a71d8b9SHanjun Guo if (!dev_is_pci(dev)) { 5440a71d8b9SHanjun Guo struct acpi_iort_node *node; 5450a71d8b9SHanjun Guo /* 5460a71d8b9SHanjun Guo * scan iort_fwnode_list to see if it's an iort platform 5470a71d8b9SHanjun Guo * device (such as SMMU, PMCG),its iort node already cached 5480a71d8b9SHanjun Guo * and associated with fwnode when iort platform devices 5490a71d8b9SHanjun Guo * were initialized. 5500a71d8b9SHanjun Guo */ 5510a71d8b9SHanjun Guo node = iort_get_iort_node(dev->fwnode); 5520a71d8b9SHanjun Guo if (node) 5530a71d8b9SHanjun Guo return node; 5540a71d8b9SHanjun Guo /* 5550a71d8b9SHanjun Guo * if not, then it should be a platform device defined in 5560a71d8b9SHanjun Guo * DSDT/SSDT (with Named Component node in IORT) 5570a71d8b9SHanjun Guo */ 55888ef16d8STomasz Nowicki return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 55988ef16d8STomasz Nowicki iort_match_node_callback, dev); 5600a71d8b9SHanjun Guo } 56188ef16d8STomasz Nowicki 56288ef16d8STomasz Nowicki pbus = to_pci_dev(dev)->bus; 56388ef16d8STomasz Nowicki 56488ef16d8STomasz Nowicki return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 56588ef16d8STomasz Nowicki iort_match_node_callback, &pbus->dev); 56688ef16d8STomasz Nowicki } 56788ef16d8STomasz Nowicki 5684bf2efd2STomasz Nowicki /** 56939c3cf56SLorenzo Pieralisi * iort_msi_map_id() - Map a MSI input ID for a device 5704bf2efd2STomasz Nowicki * @dev: The device for which the mapping is to be done. 57139c3cf56SLorenzo Pieralisi * @input_id: The device input ID. 5724bf2efd2STomasz Nowicki * 57339c3cf56SLorenzo Pieralisi * Returns: mapped MSI ID on success, input ID otherwise 5744bf2efd2STomasz Nowicki */ 57539c3cf56SLorenzo Pieralisi u32 iort_msi_map_id(struct device *dev, u32 input_id) 5764bf2efd2STomasz Nowicki { 5774bf2efd2STomasz Nowicki struct acpi_iort_node *node; 5784bf2efd2STomasz Nowicki u32 dev_id; 5794bf2efd2STomasz Nowicki 5804bf2efd2STomasz Nowicki node = iort_find_dev_node(dev); 5814bf2efd2STomasz Nowicki if (!node) 58239c3cf56SLorenzo Pieralisi return input_id; 5834bf2efd2STomasz Nowicki 58439c3cf56SLorenzo Pieralisi iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); 5854bf2efd2STomasz Nowicki return dev_id; 5864bf2efd2STomasz Nowicki } 5874bf2efd2STomasz Nowicki 5884bf2efd2STomasz Nowicki /** 589ae7c1838SHanjun Guo * iort_pmsi_get_dev_id() - Get the device id for a device 590ae7c1838SHanjun Guo * @dev: The device for which the mapping is to be done. 591ae7c1838SHanjun Guo * @dev_id: The device ID found. 592ae7c1838SHanjun Guo * 593ae7c1838SHanjun Guo * Returns: 0 for successful find a dev id, -ENODEV on error 594ae7c1838SHanjun Guo */ 595ae7c1838SHanjun Guo int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) 596ae7c1838SHanjun Guo { 5978c8df8dcSHanjun Guo int i, index; 598ae7c1838SHanjun Guo struct acpi_iort_node *node; 599ae7c1838SHanjun Guo 600ae7c1838SHanjun Guo node = iort_find_dev_node(dev); 601ae7c1838SHanjun Guo if (!node) 602ae7c1838SHanjun Guo return -ENODEV; 603ae7c1838SHanjun Guo 6048c8df8dcSHanjun Guo index = iort_get_id_mapping_index(node); 6058c8df8dcSHanjun Guo /* if there is a valid index, go get the dev_id directly */ 6068c8df8dcSHanjun Guo if (index >= 0) { 6078c8df8dcSHanjun Guo if (iort_node_get_id(node, dev_id, index)) 608ae7c1838SHanjun Guo return 0; 6098c8df8dcSHanjun Guo } else { 6108c8df8dcSHanjun Guo for (i = 0; i < node->mapping_count; i++) { 6118c8df8dcSHanjun Guo if (iort_node_map_platform_id(node, dev_id, 6128c8df8dcSHanjun Guo IORT_MSI_TYPE, i)) 6138c8df8dcSHanjun Guo return 0; 6148c8df8dcSHanjun Guo } 615ae7c1838SHanjun Guo } 616ae7c1838SHanjun Guo 617ae7c1838SHanjun Guo return -ENODEV; 618ae7c1838SHanjun Guo } 619ae7c1838SHanjun Guo 6208b4282e6SShameer Kolothum static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) 6218b4282e6SShameer Kolothum { 6228b4282e6SShameer Kolothum struct iort_its_msi_chip *its_msi_chip; 6238b4282e6SShameer Kolothum int ret = -ENODEV; 6248b4282e6SShameer Kolothum 6258b4282e6SShameer Kolothum spin_lock(&iort_msi_chip_lock); 6268b4282e6SShameer Kolothum list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 6278b4282e6SShameer Kolothum if (its_msi_chip->translation_id == its_id) { 6288b4282e6SShameer Kolothum *base = its_msi_chip->base_addr; 6298b4282e6SShameer Kolothum ret = 0; 6308b4282e6SShameer Kolothum break; 6318b4282e6SShameer Kolothum } 6328b4282e6SShameer Kolothum } 6338b4282e6SShameer Kolothum spin_unlock(&iort_msi_chip_lock); 6348b4282e6SShameer Kolothum 6358b4282e6SShameer Kolothum return ret; 6368b4282e6SShameer Kolothum } 6378b4282e6SShameer Kolothum 638ae7c1838SHanjun Guo /** 6394bf2efd2STomasz Nowicki * iort_dev_find_its_id() - Find the ITS identifier for a device 6404bf2efd2STomasz Nowicki * @dev: The device. 641d1718a1bSLorenzo Pieralisi * @id: Device's ID 6424bf2efd2STomasz Nowicki * @idx: Index of the ITS identifier list. 6434bf2efd2STomasz Nowicki * @its_id: ITS identifier. 6444bf2efd2STomasz Nowicki * 6454bf2efd2STomasz Nowicki * Returns: 0 on success, appropriate error value otherwise 6464bf2efd2STomasz Nowicki */ 647d1718a1bSLorenzo Pieralisi static int iort_dev_find_its_id(struct device *dev, u32 id, 6484bf2efd2STomasz Nowicki unsigned int idx, int *its_id) 6494bf2efd2STomasz Nowicki { 6504bf2efd2STomasz Nowicki struct acpi_iort_its_group *its; 6514bf2efd2STomasz Nowicki struct acpi_iort_node *node; 6524bf2efd2STomasz Nowicki 6534bf2efd2STomasz Nowicki node = iort_find_dev_node(dev); 6544bf2efd2STomasz Nowicki if (!node) 6554bf2efd2STomasz Nowicki return -ENXIO; 6564bf2efd2STomasz Nowicki 657d1718a1bSLorenzo Pieralisi node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE); 6584bf2efd2STomasz Nowicki if (!node) 6594bf2efd2STomasz Nowicki return -ENXIO; 6604bf2efd2STomasz Nowicki 6614bf2efd2STomasz Nowicki /* Move to ITS specific data */ 6624bf2efd2STomasz Nowicki its = (struct acpi_iort_its_group *)node->node_data; 6635a46d3f7SLorenzo Pieralisi if (idx >= its->its_count) { 6645a46d3f7SLorenzo Pieralisi dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n", 6654bf2efd2STomasz Nowicki idx, its->its_count); 6664bf2efd2STomasz Nowicki return -ENXIO; 6674bf2efd2STomasz Nowicki } 6684bf2efd2STomasz Nowicki 6694bf2efd2STomasz Nowicki *its_id = its->identifiers[idx]; 6704bf2efd2STomasz Nowicki return 0; 6714bf2efd2STomasz Nowicki } 6724bf2efd2STomasz Nowicki 6734bf2efd2STomasz Nowicki /** 6744bf2efd2STomasz Nowicki * iort_get_device_domain() - Find MSI domain related to a device 6754bf2efd2STomasz Nowicki * @dev: The device. 676774c4a3bSShiju Jose * @id: Requester ID for the device. 677774c4a3bSShiju Jose * @bus_token: irq domain bus token. 6784bf2efd2STomasz Nowicki * 6794bf2efd2STomasz Nowicki * Returns: the MSI domain for this device, NULL otherwise 6804bf2efd2STomasz Nowicki */ 681d1718a1bSLorenzo Pieralisi struct irq_domain *iort_get_device_domain(struct device *dev, u32 id, 682d1718a1bSLorenzo Pieralisi enum irq_domain_bus_token bus_token) 6834bf2efd2STomasz Nowicki { 6844bf2efd2STomasz Nowicki struct fwnode_handle *handle; 6854bf2efd2STomasz Nowicki int its_id; 6864bf2efd2STomasz Nowicki 687d1718a1bSLorenzo Pieralisi if (iort_dev_find_its_id(dev, id, 0, &its_id)) 6884bf2efd2STomasz Nowicki return NULL; 6894bf2efd2STomasz Nowicki 6904bf2efd2STomasz Nowicki handle = iort_find_domain_token(its_id); 6914bf2efd2STomasz Nowicki if (!handle) 6924bf2efd2STomasz Nowicki return NULL; 6934bf2efd2STomasz Nowicki 694d1718a1bSLorenzo Pieralisi return irq_find_matching_fwnode(handle, bus_token); 6954bf2efd2STomasz Nowicki } 6964bf2efd2STomasz Nowicki 69765637901SLorenzo Pieralisi static void iort_set_device_domain(struct device *dev, 69865637901SLorenzo Pieralisi struct acpi_iort_node *node) 69965637901SLorenzo Pieralisi { 70065637901SLorenzo Pieralisi struct acpi_iort_its_group *its; 70165637901SLorenzo Pieralisi struct acpi_iort_node *msi_parent; 70265637901SLorenzo Pieralisi struct acpi_iort_id_mapping *map; 70365637901SLorenzo Pieralisi struct fwnode_handle *iort_fwnode; 70465637901SLorenzo Pieralisi struct irq_domain *domain; 70565637901SLorenzo Pieralisi int index; 70665637901SLorenzo Pieralisi 70765637901SLorenzo Pieralisi index = iort_get_id_mapping_index(node); 70865637901SLorenzo Pieralisi if (index < 0) 70965637901SLorenzo Pieralisi return; 71065637901SLorenzo Pieralisi 71165637901SLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 71265637901SLorenzo Pieralisi node->mapping_offset + index * sizeof(*map)); 71365637901SLorenzo Pieralisi 71465637901SLorenzo Pieralisi /* Firmware bug! */ 71565637901SLorenzo Pieralisi if (!map->output_reference || 71665637901SLorenzo Pieralisi !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { 71765637901SLorenzo Pieralisi pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", 71865637901SLorenzo Pieralisi node, node->type); 71965637901SLorenzo Pieralisi return; 72065637901SLorenzo Pieralisi } 72165637901SLorenzo Pieralisi 72265637901SLorenzo Pieralisi msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 72365637901SLorenzo Pieralisi map->output_reference); 72465637901SLorenzo Pieralisi 72565637901SLorenzo Pieralisi if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) 72665637901SLorenzo Pieralisi return; 72765637901SLorenzo Pieralisi 72865637901SLorenzo Pieralisi /* Move to ITS specific data */ 72965637901SLorenzo Pieralisi its = (struct acpi_iort_its_group *)msi_parent->node_data; 73065637901SLorenzo Pieralisi 73165637901SLorenzo Pieralisi iort_fwnode = iort_find_domain_token(its->identifiers[0]); 73265637901SLorenzo Pieralisi if (!iort_fwnode) 73365637901SLorenzo Pieralisi return; 73465637901SLorenzo Pieralisi 73565637901SLorenzo Pieralisi domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 73665637901SLorenzo Pieralisi if (domain) 73765637901SLorenzo Pieralisi dev_set_msi_domain(dev, domain); 73865637901SLorenzo Pieralisi } 73965637901SLorenzo Pieralisi 740d4f54a18SHanjun Guo /** 741d4f54a18SHanjun Guo * iort_get_platform_device_domain() - Find MSI domain related to a 742d4f54a18SHanjun Guo * platform device 743d4f54a18SHanjun Guo * @dev: the dev pointer associated with the platform device 744d4f54a18SHanjun Guo * 745d4f54a18SHanjun Guo * Returns: the MSI domain for this device, NULL otherwise 746d4f54a18SHanjun Guo */ 747d4f54a18SHanjun Guo static struct irq_domain *iort_get_platform_device_domain(struct device *dev) 748d4f54a18SHanjun Guo { 749ea2412dcSLorenzo Pieralisi struct acpi_iort_node *node, *msi_parent = NULL; 750d4f54a18SHanjun Guo struct fwnode_handle *iort_fwnode; 751d4f54a18SHanjun Guo struct acpi_iort_its_group *its; 752d4f54a18SHanjun Guo int i; 753d4f54a18SHanjun Guo 754d4f54a18SHanjun Guo /* find its associated iort node */ 755d4f54a18SHanjun Guo node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 756d4f54a18SHanjun Guo iort_match_node_callback, dev); 757d4f54a18SHanjun Guo if (!node) 758d4f54a18SHanjun Guo return NULL; 759d4f54a18SHanjun Guo 760d4f54a18SHanjun Guo /* then find its msi parent node */ 761d4f54a18SHanjun Guo for (i = 0; i < node->mapping_count; i++) { 762d4f54a18SHanjun Guo msi_parent = iort_node_map_platform_id(node, NULL, 763d4f54a18SHanjun Guo IORT_MSI_TYPE, i); 764d4f54a18SHanjun Guo if (msi_parent) 765d4f54a18SHanjun Guo break; 766d4f54a18SHanjun Guo } 767d4f54a18SHanjun Guo 768d4f54a18SHanjun Guo if (!msi_parent) 769d4f54a18SHanjun Guo return NULL; 770d4f54a18SHanjun Guo 771d4f54a18SHanjun Guo /* Move to ITS specific data */ 772d4f54a18SHanjun Guo its = (struct acpi_iort_its_group *)msi_parent->node_data; 773d4f54a18SHanjun Guo 774d4f54a18SHanjun Guo iort_fwnode = iort_find_domain_token(its->identifiers[0]); 775d4f54a18SHanjun Guo if (!iort_fwnode) 776d4f54a18SHanjun Guo return NULL; 777d4f54a18SHanjun Guo 778d4f54a18SHanjun Guo return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 779d4f54a18SHanjun Guo } 780d4f54a18SHanjun Guo 781d4f54a18SHanjun Guo void acpi_configure_pmsi_domain(struct device *dev) 782d4f54a18SHanjun Guo { 783d4f54a18SHanjun Guo struct irq_domain *msi_domain; 784d4f54a18SHanjun Guo 785d4f54a18SHanjun Guo msi_domain = iort_get_platform_device_domain(dev); 786d4f54a18SHanjun Guo if (msi_domain) 787d4f54a18SHanjun Guo dev_set_msi_domain(dev, msi_domain); 788d4f54a18SHanjun Guo } 789d4f54a18SHanjun Guo 790d49f2dedSLorenzo Pieralisi #ifdef CONFIG_IOMMU_API 7918b4282e6SShameer Kolothum static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) 7928b4282e6SShameer Kolothum { 7938b4282e6SShameer Kolothum struct acpi_iort_node *iommu; 7948097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 7958b4282e6SShameer Kolothum 7968b4282e6SShameer Kolothum iommu = iort_get_iort_node(fwspec->iommu_fwnode); 7978b4282e6SShameer Kolothum 7988b4282e6SShameer Kolothum if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { 7998b4282e6SShameer Kolothum struct acpi_iort_smmu_v3 *smmu; 8008b4282e6SShameer Kolothum 8018b4282e6SShameer Kolothum smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; 8028b4282e6SShameer Kolothum if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) 8038b4282e6SShameer Kolothum return iommu; 8048b4282e6SShameer Kolothum } 8058b4282e6SShameer Kolothum 8068b4282e6SShameer Kolothum return NULL; 8078b4282e6SShameer Kolothum } 8088b4282e6SShameer Kolothum 8098b4282e6SShameer Kolothum /** 8108b4282e6SShameer Kolothum * iort_iommu_msi_get_resv_regions - Reserved region driver helper 8118b4282e6SShameer Kolothum * @dev: Device from iommu_get_resv_regions() 8128b4282e6SShameer Kolothum * @head: Reserved region list from iommu_get_resv_regions() 8138b4282e6SShameer Kolothum * 8148b4282e6SShameer Kolothum * Returns: Number of msi reserved regions on success (0 if platform 8158b4282e6SShameer Kolothum * doesn't require the reservation or no associated msi regions), 8168b4282e6SShameer Kolothum * appropriate error value otherwise. The ITS interrupt translation 8178b4282e6SShameer Kolothum * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device 8188b4282e6SShameer Kolothum * are the msi reserved regions. 8198b4282e6SShameer Kolothum */ 8208b4282e6SShameer Kolothum int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) 8218b4282e6SShameer Kolothum { 8228097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 8238b4282e6SShameer Kolothum struct acpi_iort_its_group *its; 8248b4282e6SShameer Kolothum struct acpi_iort_node *iommu_node, *its_node = NULL; 8258b4282e6SShameer Kolothum int i, resv = 0; 8268b4282e6SShameer Kolothum 8278b4282e6SShameer Kolothum iommu_node = iort_get_msi_resv_iommu(dev); 8288b4282e6SShameer Kolothum if (!iommu_node) 8298b4282e6SShameer Kolothum return 0; 8308b4282e6SShameer Kolothum 8318b4282e6SShameer Kolothum /* 8328b4282e6SShameer Kolothum * Current logic to reserve ITS regions relies on HW topologies 8338b4282e6SShameer Kolothum * where a given PCI or named component maps its IDs to only one 8348b4282e6SShameer Kolothum * ITS group; if a PCI or named component can map its IDs to 8358b4282e6SShameer Kolothum * different ITS groups through IORT mappings this function has 8368b4282e6SShameer Kolothum * to be reworked to ensure we reserve regions for all ITS groups 8378b4282e6SShameer Kolothum * a given PCI or named component may map IDs to. 8388b4282e6SShameer Kolothum */ 8398b4282e6SShameer Kolothum 8408097e53eSJoerg Roedel for (i = 0; i < fwspec->num_ids; i++) { 8418b4282e6SShameer Kolothum its_node = iort_node_map_id(iommu_node, 8428097e53eSJoerg Roedel fwspec->ids[i], 8438b4282e6SShameer Kolothum NULL, IORT_MSI_TYPE); 8448b4282e6SShameer Kolothum if (its_node) 8458b4282e6SShameer Kolothum break; 8468b4282e6SShameer Kolothum } 8478b4282e6SShameer Kolothum 8488b4282e6SShameer Kolothum if (!its_node) 8498b4282e6SShameer Kolothum return 0; 8508b4282e6SShameer Kolothum 8518b4282e6SShameer Kolothum /* Move to ITS specific data */ 8528b4282e6SShameer Kolothum its = (struct acpi_iort_its_group *)its_node->node_data; 8538b4282e6SShameer Kolothum 8548b4282e6SShameer Kolothum for (i = 0; i < its->its_count; i++) { 8558b4282e6SShameer Kolothum phys_addr_t base; 8568b4282e6SShameer Kolothum 8578b4282e6SShameer Kolothum if (!iort_find_its_base(its->identifiers[i], &base)) { 8588b4282e6SShameer Kolothum int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 8598b4282e6SShameer Kolothum struct iommu_resv_region *region; 8608b4282e6SShameer Kolothum 8618b4282e6SShameer Kolothum region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, 8628b4282e6SShameer Kolothum prot, IOMMU_RESV_MSI); 8638b4282e6SShameer Kolothum if (region) { 8648b4282e6SShameer Kolothum list_add_tail(®ion->list, head); 8658b4282e6SShameer Kolothum resv++; 8668b4282e6SShameer Kolothum } 8678b4282e6SShameer Kolothum } 8688b4282e6SShameer Kolothum } 8698b4282e6SShameer Kolothum 8708b4282e6SShameer Kolothum return (resv == its->its_count) ? resv : -ENODEV; 8718b4282e6SShameer Kolothum } 87282126886SLorenzo Pieralisi 87382126886SLorenzo Pieralisi static inline bool iort_iommu_driver_enabled(u8 type) 87482126886SLorenzo Pieralisi { 87582126886SLorenzo Pieralisi switch (type) { 87682126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU_V3: 877d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU_V3); 87882126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU: 879d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU); 88082126886SLorenzo Pieralisi default: 88182126886SLorenzo Pieralisi pr_warn("IORT node type %u does not describe an SMMU\n", type); 88282126886SLorenzo Pieralisi return false; 88382126886SLorenzo Pieralisi } 88482126886SLorenzo Pieralisi } 88582126886SLorenzo Pieralisi 88682126886SLorenzo Pieralisi static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) 88782126886SLorenzo Pieralisi { 88882126886SLorenzo Pieralisi struct acpi_iort_root_complex *pci_rc; 88982126886SLorenzo Pieralisi 89082126886SLorenzo Pieralisi pci_rc = (struct acpi_iort_root_complex *)node->node_data; 89182126886SLorenzo Pieralisi return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; 89282126886SLorenzo Pieralisi } 893d49f2dedSLorenzo Pieralisi 894bc8648d4SRobin Murphy static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, 895643b8e4dSLorenzo Pieralisi u32 streamid) 896643b8e4dSLorenzo Pieralisi { 897bc8648d4SRobin Murphy const struct iommu_ops *ops; 898643b8e4dSLorenzo Pieralisi struct fwnode_handle *iort_fwnode; 899643b8e4dSLorenzo Pieralisi 900bc8648d4SRobin Murphy if (!node) 901bc8648d4SRobin Murphy return -ENODEV; 902bc8648d4SRobin Murphy 903643b8e4dSLorenzo Pieralisi iort_fwnode = iort_get_fwnode(node); 904643b8e4dSLorenzo Pieralisi if (!iort_fwnode) 905bc8648d4SRobin Murphy return -ENODEV; 906643b8e4dSLorenzo Pieralisi 9075a1bb638SSricharan R /* 9085a1bb638SSricharan R * If the ops look-up fails, this means that either 9095a1bb638SSricharan R * the SMMU drivers have not been probed yet or that 9105a1bb638SSricharan R * the SMMU drivers are not built in the kernel; 9115a1bb638SSricharan R * Depending on whether the SMMU drivers are built-in 9125a1bb638SSricharan R * in the kernel or not, defer the IOMMU configuration 9135a1bb638SSricharan R * or just abort it. 9145a1bb638SSricharan R */ 915bc8648d4SRobin Murphy ops = iommu_ops_from_fwnode(iort_fwnode); 916643b8e4dSLorenzo Pieralisi if (!ops) 9175a1bb638SSricharan R return iort_iommu_driver_enabled(node->type) ? 918bc8648d4SRobin Murphy -EPROBE_DEFER : -ENODEV; 919643b8e4dSLorenzo Pieralisi 920*11a8c5e3SJean-Philippe Brucker return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops); 921643b8e4dSLorenzo Pieralisi } 922643b8e4dSLorenzo Pieralisi 923bc8648d4SRobin Murphy struct iort_pci_alias_info { 924bc8648d4SRobin Murphy struct device *dev; 925bc8648d4SRobin Murphy struct acpi_iort_node *node; 926bc8648d4SRobin Murphy }; 927bc8648d4SRobin Murphy 928bc8648d4SRobin Murphy static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 929bc8648d4SRobin Murphy { 930bc8648d4SRobin Murphy struct iort_pci_alias_info *info = data; 931bc8648d4SRobin Murphy struct acpi_iort_node *parent; 932bc8648d4SRobin Murphy u32 streamid; 933bc8648d4SRobin Murphy 934bc8648d4SRobin Murphy parent = iort_node_map_id(info->node, alias, &streamid, 935bc8648d4SRobin Murphy IORT_IOMMU_TYPE); 936bc8648d4SRobin Murphy return iort_iommu_xlate(info->dev, parent, streamid); 937643b8e4dSLorenzo Pieralisi } 938643b8e4dSLorenzo Pieralisi 939da22565dSJean-Philippe Brucker static void iort_named_component_init(struct device *dev, 940da22565dSJean-Philippe Brucker struct acpi_iort_node *node) 941da22565dSJean-Philippe Brucker { 942434b73e6SJean-Philippe Brucker struct property_entry props[2] = {}; 943da22565dSJean-Philippe Brucker struct acpi_iort_named_component *nc; 944da22565dSJean-Philippe Brucker 945da22565dSJean-Philippe Brucker nc = (struct acpi_iort_named_component *)node->node_data; 946434b73e6SJean-Philippe Brucker props[0] = PROPERTY_ENTRY_U32("pasid-num-bits", 947434b73e6SJean-Philippe Brucker FIELD_GET(ACPI_IORT_NC_PASID_BITS, 948434b73e6SJean-Philippe Brucker nc->node_flags)); 949434b73e6SJean-Philippe Brucker 950434b73e6SJean-Philippe Brucker if (device_add_properties(dev, props)) 951434b73e6SJean-Philippe Brucker dev_warn(dev, "Could not add device properties\n"); 952da22565dSJean-Philippe Brucker } 953da22565dSJean-Philippe Brucker 954b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node) 955b8e069a2SLorenzo Pieralisi { 956b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 957b8e069a2SLorenzo Pieralisi int err = -ENODEV, i = 0; 958b8e069a2SLorenzo Pieralisi u32 streamid = 0; 959b8e069a2SLorenzo Pieralisi 960b8e069a2SLorenzo Pieralisi do { 961b8e069a2SLorenzo Pieralisi 962b8e069a2SLorenzo Pieralisi parent = iort_node_map_platform_id(node, &streamid, 963b8e069a2SLorenzo Pieralisi IORT_IOMMU_TYPE, 964b8e069a2SLorenzo Pieralisi i++); 965b8e069a2SLorenzo Pieralisi 966b8e069a2SLorenzo Pieralisi if (parent) 967b8e069a2SLorenzo Pieralisi err = iort_iommu_xlate(dev, parent, streamid); 968b8e069a2SLorenzo Pieralisi } while (parent && !err); 969b8e069a2SLorenzo Pieralisi 970b8e069a2SLorenzo Pieralisi return err; 971b8e069a2SLorenzo Pieralisi } 972b8e069a2SLorenzo Pieralisi 973b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map_id(struct device *dev, 974b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node, 975b8e069a2SLorenzo Pieralisi const u32 *in_id) 976b8e069a2SLorenzo Pieralisi { 977b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 978b8e069a2SLorenzo Pieralisi u32 streamid; 979b8e069a2SLorenzo Pieralisi 980b8e069a2SLorenzo Pieralisi parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE); 981b8e069a2SLorenzo Pieralisi if (parent) 982b8e069a2SLorenzo Pieralisi return iort_iommu_xlate(dev, parent, streamid); 983b8e069a2SLorenzo Pieralisi 984b8e069a2SLorenzo Pieralisi return -ENODEV; 985b8e069a2SLorenzo Pieralisi } 986b8e069a2SLorenzo Pieralisi 987b8e069a2SLorenzo Pieralisi 98882126886SLorenzo Pieralisi /** 989b8e069a2SLorenzo Pieralisi * iort_iommu_configure_id - Set-up IOMMU configuration for a device. 99082126886SLorenzo Pieralisi * 99182126886SLorenzo Pieralisi * @dev: device to configure 992b8e069a2SLorenzo Pieralisi * @id_in: optional input id const value pointer 99382126886SLorenzo Pieralisi * 994*11a8c5e3SJean-Philippe Brucker * Returns: 0 on success, <0 on failure 99582126886SLorenzo Pieralisi */ 996*11a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *id_in) 99782126886SLorenzo Pieralisi { 998b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node; 99982126886SLorenzo Pieralisi int err = -ENODEV; 100082126886SLorenzo Pieralisi 100182126886SLorenzo Pieralisi if (dev_is_pci(dev)) { 10026990ec79SJoerg Roedel struct iommu_fwspec *fwspec; 100382126886SLorenzo Pieralisi struct pci_bus *bus = to_pci_dev(dev)->bus; 100482126886SLorenzo Pieralisi struct iort_pci_alias_info info = { .dev = dev }; 100582126886SLorenzo Pieralisi 100682126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 100782126886SLorenzo Pieralisi iort_match_node_callback, &bus->dev); 100882126886SLorenzo Pieralisi if (!node) 1009*11a8c5e3SJean-Philippe Brucker return -ENODEV; 101082126886SLorenzo Pieralisi 101182126886SLorenzo Pieralisi info.node = node; 101282126886SLorenzo Pieralisi err = pci_for_each_dma_alias(to_pci_dev(dev), 101382126886SLorenzo Pieralisi iort_pci_iommu_init, &info); 101482126886SLorenzo Pieralisi 10156990ec79SJoerg Roedel fwspec = dev_iommu_fwspec_get(dev); 10166990ec79SJoerg Roedel if (fwspec && iort_pci_rc_supports_ats(node)) 10176990ec79SJoerg Roedel fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; 101882126886SLorenzo Pieralisi } else { 101982126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 102082126886SLorenzo Pieralisi iort_match_node_callback, dev); 102182126886SLorenzo Pieralisi if (!node) 1022*11a8c5e3SJean-Philippe Brucker return -ENODEV; 102382126886SLorenzo Pieralisi 1024b8e069a2SLorenzo Pieralisi err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) : 1025b8e069a2SLorenzo Pieralisi iort_nc_iommu_map(dev, node); 1026da22565dSJean-Philippe Brucker 1027da22565dSJean-Philippe Brucker if (!err) 1028da22565dSJean-Philippe Brucker iort_named_component_init(dev, node); 102982126886SLorenzo Pieralisi } 103082126886SLorenzo Pieralisi 1031*11a8c5e3SJean-Philippe Brucker return err; 103282126886SLorenzo Pieralisi } 1033b8e069a2SLorenzo Pieralisi 103482126886SLorenzo Pieralisi #else 103582126886SLorenzo Pieralisi int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) 103682126886SLorenzo Pieralisi { return 0; } 1037*11a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *input_id) 1038*11a8c5e3SJean-Philippe Brucker { return -ENODEV; } 103982126886SLorenzo Pieralisi #endif 104082126886SLorenzo Pieralisi 104110d8ab2cSLorenzo Pieralisi static int nc_dma_get_range(struct device *dev, u64 *size) 104210d8ab2cSLorenzo Pieralisi { 104310d8ab2cSLorenzo Pieralisi struct acpi_iort_node *node; 104410d8ab2cSLorenzo Pieralisi struct acpi_iort_named_component *ncomp; 104510d8ab2cSLorenzo Pieralisi 104610d8ab2cSLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 104710d8ab2cSLorenzo Pieralisi iort_match_node_callback, dev); 104810d8ab2cSLorenzo Pieralisi if (!node) 104910d8ab2cSLorenzo Pieralisi return -ENODEV; 105010d8ab2cSLorenzo Pieralisi 105110d8ab2cSLorenzo Pieralisi ncomp = (struct acpi_iort_named_component *)node->node_data; 105210d8ab2cSLorenzo Pieralisi 1053a1df829eSMoritz Fischer if (!ncomp->memory_address_limit) { 1054a1df829eSMoritz Fischer pr_warn(FW_BUG "Named component missing memory address limit\n"); 1055a1df829eSMoritz Fischer return -EINVAL; 1056a1df829eSMoritz Fischer } 1057a1df829eSMoritz Fischer 105810d8ab2cSLorenzo Pieralisi *size = ncomp->memory_address_limit >= 64 ? U64_MAX : 105910d8ab2cSLorenzo Pieralisi 1ULL<<ncomp->memory_address_limit; 106010d8ab2cSLorenzo Pieralisi 106110d8ab2cSLorenzo Pieralisi return 0; 106210d8ab2cSLorenzo Pieralisi } 106310d8ab2cSLorenzo Pieralisi 10645ac65e8cSRobin Murphy static int rc_dma_get_range(struct device *dev, u64 *size) 10655ac65e8cSRobin Murphy { 10665ac65e8cSRobin Murphy struct acpi_iort_node *node; 10675ac65e8cSRobin Murphy struct acpi_iort_root_complex *rc; 1068c7777236SJean-Philippe Brucker struct pci_bus *pbus = to_pci_dev(dev)->bus; 10695ac65e8cSRobin Murphy 10705ac65e8cSRobin Murphy node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1071c7777236SJean-Philippe Brucker iort_match_node_callback, &pbus->dev); 10725ac65e8cSRobin Murphy if (!node || node->revision < 1) 10735ac65e8cSRobin Murphy return -ENODEV; 10745ac65e8cSRobin Murphy 10755ac65e8cSRobin Murphy rc = (struct acpi_iort_root_complex *)node->node_data; 10765ac65e8cSRobin Murphy 1077a1df829eSMoritz Fischer if (!rc->memory_address_limit) { 1078a1df829eSMoritz Fischer pr_warn(FW_BUG "Root complex missing memory address limit\n"); 1079a1df829eSMoritz Fischer return -EINVAL; 1080a1df829eSMoritz Fischer } 1081a1df829eSMoritz Fischer 10825ac65e8cSRobin Murphy *size = rc->memory_address_limit >= 64 ? U64_MAX : 10835ac65e8cSRobin Murphy 1ULL<<rc->memory_address_limit; 10845ac65e8cSRobin Murphy 10855ac65e8cSRobin Murphy return 0; 10865ac65e8cSRobin Murphy } 10875ac65e8cSRobin Murphy 1088643b8e4dSLorenzo Pieralisi /** 1089db59e1b6SJean-Philippe Brucker * iort_dma_get_ranges() - Look up DMA addressing limit for the device 1090db59e1b6SJean-Philippe Brucker * @dev: device to lookup 1091db59e1b6SJean-Philippe Brucker * @size: DMA range size result pointer 109218b709beSLorenzo Pieralisi * 1093db59e1b6SJean-Philippe Brucker * Return: 0 on success, an error otherwise. 109418b709beSLorenzo Pieralisi */ 1095db59e1b6SJean-Philippe Brucker int iort_dma_get_ranges(struct device *dev, u64 *size) 109618b709beSLorenzo Pieralisi { 1097db59e1b6SJean-Philippe Brucker if (dev_is_pci(dev)) 1098db59e1b6SJean-Philippe Brucker return rc_dma_get_range(dev, size); 10996757cdaeSRobin Murphy else 1100db59e1b6SJean-Philippe Brucker return nc_dma_get_range(dev, size); 110118b709beSLorenzo Pieralisi } 110218b709beSLorenzo Pieralisi 1103e4dadfa8SLorenzo Pieralisi static void __init acpi_iort_register_irq(int hwirq, const char *name, 1104e4dadfa8SLorenzo Pieralisi int trigger, 1105e4dadfa8SLorenzo Pieralisi struct resource *res) 1106e4dadfa8SLorenzo Pieralisi { 1107e4dadfa8SLorenzo Pieralisi int irq = acpi_register_gsi(NULL, hwirq, trigger, 1108e4dadfa8SLorenzo Pieralisi ACPI_ACTIVE_HIGH); 1109e4dadfa8SLorenzo Pieralisi 1110e4dadfa8SLorenzo Pieralisi if (irq <= 0) { 1111e4dadfa8SLorenzo Pieralisi pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, 1112e4dadfa8SLorenzo Pieralisi name); 1113e4dadfa8SLorenzo Pieralisi return; 1114e4dadfa8SLorenzo Pieralisi } 1115e4dadfa8SLorenzo Pieralisi 1116e4dadfa8SLorenzo Pieralisi res->start = irq; 1117e4dadfa8SLorenzo Pieralisi res->end = irq; 1118e4dadfa8SLorenzo Pieralisi res->flags = IORESOURCE_IRQ; 1119e4dadfa8SLorenzo Pieralisi res->name = name; 1120e4dadfa8SLorenzo Pieralisi } 1121e4dadfa8SLorenzo Pieralisi 1122e4dadfa8SLorenzo Pieralisi static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) 1123e4dadfa8SLorenzo Pieralisi { 1124e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1125e4dadfa8SLorenzo Pieralisi /* Always present mem resource */ 1126e4dadfa8SLorenzo Pieralisi int num_res = 1; 1127e4dadfa8SLorenzo Pieralisi 1128e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1129e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1130e4dadfa8SLorenzo Pieralisi 1131e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1132e4dadfa8SLorenzo Pieralisi num_res++; 1133e4dadfa8SLorenzo Pieralisi 1134e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1135e4dadfa8SLorenzo Pieralisi num_res++; 1136e4dadfa8SLorenzo Pieralisi 1137e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1138e4dadfa8SLorenzo Pieralisi num_res++; 1139e4dadfa8SLorenzo Pieralisi 1140e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1141e4dadfa8SLorenzo Pieralisi num_res++; 1142e4dadfa8SLorenzo Pieralisi 1143e4dadfa8SLorenzo Pieralisi return num_res; 1144e4dadfa8SLorenzo Pieralisi } 1145e4dadfa8SLorenzo Pieralisi 1146f935448aSGeetha Sowjanya static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) 1147f935448aSGeetha Sowjanya { 1148f935448aSGeetha Sowjanya /* 1149f935448aSGeetha Sowjanya * Cavium ThunderX2 implementation doesn't not support unique 1150f935448aSGeetha Sowjanya * irq line. Use single irq line for all the SMMUv3 interrupts. 1151f935448aSGeetha Sowjanya */ 1152f935448aSGeetha Sowjanya if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1153f935448aSGeetha Sowjanya return false; 1154f935448aSGeetha Sowjanya 1155f935448aSGeetha Sowjanya /* 1156f935448aSGeetha Sowjanya * ThunderX2 doesn't support MSIs from the SMMU, so we're checking 1157f935448aSGeetha Sowjanya * SPI numbers here. 1158f935448aSGeetha Sowjanya */ 1159f935448aSGeetha Sowjanya return smmu->event_gsiv == smmu->pri_gsiv && 1160f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->gerr_gsiv && 1161f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->sync_gsiv; 1162f935448aSGeetha Sowjanya } 1163f935448aSGeetha Sowjanya 1164403e8c7cSLinu Cherian static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) 1165403e8c7cSLinu Cherian { 1166403e8c7cSLinu Cherian /* 1167403e8c7cSLinu Cherian * Override the size, for Cavium ThunderX2 implementation 1168403e8c7cSLinu Cherian * which doesn't support the page 1 SMMU register space. 1169403e8c7cSLinu Cherian */ 1170403e8c7cSLinu Cherian if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1171403e8c7cSLinu Cherian return SZ_64K; 1172403e8c7cSLinu Cherian 1173403e8c7cSLinu Cherian return SZ_128K; 1174403e8c7cSLinu Cherian } 1175403e8c7cSLinu Cherian 1176e4dadfa8SLorenzo Pieralisi static void __init arm_smmu_v3_init_resources(struct resource *res, 1177e4dadfa8SLorenzo Pieralisi struct acpi_iort_node *node) 1178e4dadfa8SLorenzo Pieralisi { 1179e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1180e4dadfa8SLorenzo Pieralisi int num_res = 0; 1181e4dadfa8SLorenzo Pieralisi 1182e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1183e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1184e4dadfa8SLorenzo Pieralisi 1185e4dadfa8SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1186403e8c7cSLinu Cherian res[num_res].end = smmu->base_address + 1187403e8c7cSLinu Cherian arm_smmu_v3_resource_size(smmu) - 1; 1188e4dadfa8SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1189e4dadfa8SLorenzo Pieralisi 1190e4dadfa8SLorenzo Pieralisi num_res++; 1191f935448aSGeetha Sowjanya if (arm_smmu_v3_is_combined_irq(smmu)) { 1192f935448aSGeetha Sowjanya if (smmu->event_gsiv) 1193f935448aSGeetha Sowjanya acpi_iort_register_irq(smmu->event_gsiv, "combined", 1194f935448aSGeetha Sowjanya ACPI_EDGE_SENSITIVE, 1195f935448aSGeetha Sowjanya &res[num_res++]); 1196f935448aSGeetha Sowjanya } else { 1197e4dadfa8SLorenzo Pieralisi 1198e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1199e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->event_gsiv, "eventq", 1200e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1201e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1202e4dadfa8SLorenzo Pieralisi 1203e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1204e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->pri_gsiv, "priq", 1205e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1206e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1207e4dadfa8SLorenzo Pieralisi 1208e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1209e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", 1210e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1211e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1212e4dadfa8SLorenzo Pieralisi 1213e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1214e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", 1215e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1216e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1217e4dadfa8SLorenzo Pieralisi } 1218f935448aSGeetha Sowjanya } 1219e4dadfa8SLorenzo Pieralisi 122024e51604SNeil Leeder static void __init arm_smmu_v3_dma_configure(struct device *dev, 122124e51604SNeil Leeder struct acpi_iort_node *node) 1222e4dadfa8SLorenzo Pieralisi { 1223e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 122424e51604SNeil Leeder enum dev_dma_attr attr; 1225e4dadfa8SLorenzo Pieralisi 1226e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1227e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1228e4dadfa8SLorenzo Pieralisi 122924e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? 123024e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 123124e51604SNeil Leeder 123224e51604SNeil Leeder /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ 123324e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 123424e51604SNeil Leeder 123524e51604SNeil Leeder /* Configure DMA for the page table walker */ 123624e51604SNeil Leeder acpi_dma_configure(dev, attr); 1237e4dadfa8SLorenzo Pieralisi } 1238e4dadfa8SLorenzo Pieralisi 123975808131SLorenzo Pieralisi #if defined(CONFIG_ACPI_NUMA) 12405fe0ce3bSGanapatrao Kulkarni /* 12415fe0ce3bSGanapatrao Kulkarni * set numa proximity domain for smmuv3 device 12425fe0ce3bSGanapatrao Kulkarni */ 124336a2ba07SKefeng Wang static int __init arm_smmu_v3_set_proximity(struct device *dev, 12445fe0ce3bSGanapatrao Kulkarni struct acpi_iort_node *node) 12455fe0ce3bSGanapatrao Kulkarni { 12465fe0ce3bSGanapatrao Kulkarni struct acpi_iort_smmu_v3 *smmu; 12475fe0ce3bSGanapatrao Kulkarni 12485fe0ce3bSGanapatrao Kulkarni smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 12495fe0ce3bSGanapatrao Kulkarni if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { 125001feba59SJonathan Cameron int dev_node = pxm_to_node(smmu->pxm); 125136a2ba07SKefeng Wang 12523e77eeb7SLorenzo Pieralisi if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) 125336a2ba07SKefeng Wang return -EINVAL; 125436a2ba07SKefeng Wang 12553e77eeb7SLorenzo Pieralisi set_dev_node(dev, dev_node); 12565fe0ce3bSGanapatrao Kulkarni pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", 12575fe0ce3bSGanapatrao Kulkarni smmu->base_address, 12585fe0ce3bSGanapatrao Kulkarni smmu->pxm); 12595fe0ce3bSGanapatrao Kulkarni } 126036a2ba07SKefeng Wang return 0; 12615fe0ce3bSGanapatrao Kulkarni } 12625fe0ce3bSGanapatrao Kulkarni #else 12635fe0ce3bSGanapatrao Kulkarni #define arm_smmu_v3_set_proximity NULL 12645fe0ce3bSGanapatrao Kulkarni #endif 12655fe0ce3bSGanapatrao Kulkarni 1266d6fcd3b1SLorenzo Pieralisi static int __init arm_smmu_count_resources(struct acpi_iort_node *node) 1267d6fcd3b1SLorenzo Pieralisi { 1268d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1269d6fcd3b1SLorenzo Pieralisi 1270d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1271d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1272d6fcd3b1SLorenzo Pieralisi 1273d6fcd3b1SLorenzo Pieralisi /* 1274d6fcd3b1SLorenzo Pieralisi * Only consider the global fault interrupt and ignore the 1275d6fcd3b1SLorenzo Pieralisi * configuration access interrupt. 1276d6fcd3b1SLorenzo Pieralisi * 1277d6fcd3b1SLorenzo Pieralisi * MMIO address and global fault interrupt resources are always 1278d6fcd3b1SLorenzo Pieralisi * present so add them to the context interrupt count as a static 1279d6fcd3b1SLorenzo Pieralisi * value. 1280d6fcd3b1SLorenzo Pieralisi */ 1281d6fcd3b1SLorenzo Pieralisi return smmu->context_interrupt_count + 2; 1282d6fcd3b1SLorenzo Pieralisi } 1283d6fcd3b1SLorenzo Pieralisi 1284d6fcd3b1SLorenzo Pieralisi static void __init arm_smmu_init_resources(struct resource *res, 1285d6fcd3b1SLorenzo Pieralisi struct acpi_iort_node *node) 1286d6fcd3b1SLorenzo Pieralisi { 1287d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1288d6fcd3b1SLorenzo Pieralisi int i, hw_irq, trigger, num_res = 0; 1289d6fcd3b1SLorenzo Pieralisi u64 *ctx_irq, *glb_irq; 1290d6fcd3b1SLorenzo Pieralisi 1291d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1292d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1293d6fcd3b1SLorenzo Pieralisi 1294d6fcd3b1SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1295d6fcd3b1SLorenzo Pieralisi res[num_res].end = smmu->base_address + smmu->span - 1; 1296d6fcd3b1SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1297d6fcd3b1SLorenzo Pieralisi num_res++; 1298d6fcd3b1SLorenzo Pieralisi 1299d6fcd3b1SLorenzo Pieralisi glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); 1300d6fcd3b1SLorenzo Pieralisi /* Global IRQs */ 1301d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(glb_irq[0]); 1302d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); 1303d6fcd3b1SLorenzo Pieralisi 1304d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, 1305d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1306d6fcd3b1SLorenzo Pieralisi 1307d6fcd3b1SLorenzo Pieralisi /* Context IRQs */ 1308d6fcd3b1SLorenzo Pieralisi ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); 1309d6fcd3b1SLorenzo Pieralisi for (i = 0; i < smmu->context_interrupt_count; i++) { 1310d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(ctx_irq[i]); 1311d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); 1312d6fcd3b1SLorenzo Pieralisi 1313d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, 1314d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1315d6fcd3b1SLorenzo Pieralisi } 1316d6fcd3b1SLorenzo Pieralisi } 1317d6fcd3b1SLorenzo Pieralisi 131824e51604SNeil Leeder static void __init arm_smmu_dma_configure(struct device *dev, 131924e51604SNeil Leeder struct acpi_iort_node *node) 1320d6fcd3b1SLorenzo Pieralisi { 1321d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 132224e51604SNeil Leeder enum dev_dma_attr attr; 1323d6fcd3b1SLorenzo Pieralisi 1324d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1325d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1326d6fcd3b1SLorenzo Pieralisi 132724e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? 132824e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 132924e51604SNeil Leeder 133024e51604SNeil Leeder /* We expect the dma masks to be equivalent for SMMU set-ups */ 133124e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 133224e51604SNeil Leeder 133324e51604SNeil Leeder /* Configure DMA for the page table walker */ 133424e51604SNeil Leeder acpi_dma_configure(dev, attr); 133524e51604SNeil Leeder } 133624e51604SNeil Leeder 133724e51604SNeil Leeder static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) 133824e51604SNeil Leeder { 133924e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 134024e51604SNeil Leeder 134124e51604SNeil Leeder /* Retrieve PMCG specific data */ 134224e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 134324e51604SNeil Leeder 134424e51604SNeil Leeder /* 134524e51604SNeil Leeder * There are always 2 memory resources. 134624e51604SNeil Leeder * If the overflow_gsiv is present then add that for a total of 3. 134724e51604SNeil Leeder */ 134824e51604SNeil Leeder return pmcg->overflow_gsiv ? 3 : 2; 134924e51604SNeil Leeder } 135024e51604SNeil Leeder 135124e51604SNeil Leeder static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, 135224e51604SNeil Leeder struct acpi_iort_node *node) 135324e51604SNeil Leeder { 135424e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 135524e51604SNeil Leeder 135624e51604SNeil Leeder /* Retrieve PMCG specific data */ 135724e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 135824e51604SNeil Leeder 135924e51604SNeil Leeder res[0].start = pmcg->page0_base_address; 136024e51604SNeil Leeder res[0].end = pmcg->page0_base_address + SZ_4K - 1; 136124e51604SNeil Leeder res[0].flags = IORESOURCE_MEM; 136224e51604SNeil Leeder res[1].start = pmcg->page1_base_address; 136324e51604SNeil Leeder res[1].end = pmcg->page1_base_address + SZ_4K - 1; 136424e51604SNeil Leeder res[1].flags = IORESOURCE_MEM; 136524e51604SNeil Leeder 136624e51604SNeil Leeder if (pmcg->overflow_gsiv) 136724e51604SNeil Leeder acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", 136824e51604SNeil Leeder ACPI_EDGE_SENSITIVE, &res[2]); 136924e51604SNeil Leeder } 137024e51604SNeil Leeder 137124062fe8SShameer Kolothum static struct acpi_platform_list pmcg_plat_info[] __initdata = { 137224062fe8SShameer Kolothum /* HiSilicon Hip08 Platform */ 137324062fe8SShameer Kolothum {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 137424062fe8SShameer Kolothum "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08}, 137524062fe8SShameer Kolothum { } 137624062fe8SShameer Kolothum }; 137724062fe8SShameer Kolothum 137824e51604SNeil Leeder static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) 137924e51604SNeil Leeder { 138024062fe8SShameer Kolothum u32 model; 138124062fe8SShameer Kolothum int idx; 138224062fe8SShameer Kolothum 138324062fe8SShameer Kolothum idx = acpi_match_platform_list(pmcg_plat_info); 138424062fe8SShameer Kolothum if (idx >= 0) 138524062fe8SShameer Kolothum model = pmcg_plat_info[idx].data; 138624062fe8SShameer Kolothum else 138724062fe8SShameer Kolothum model = IORT_SMMU_V3_PMCG_GENERIC; 138824e51604SNeil Leeder 138924e51604SNeil Leeder return platform_device_add_data(pdev, &model, sizeof(model)); 1390d6fcd3b1SLorenzo Pieralisi } 1391d6fcd3b1SLorenzo Pieralisi 1392896dd2c3SLorenzo Pieralisi struct iort_dev_config { 1393846f0e9eSLorenzo Pieralisi const char *name; 1394896dd2c3SLorenzo Pieralisi int (*dev_init)(struct acpi_iort_node *node); 139524e51604SNeil Leeder void (*dev_dma_configure)(struct device *dev, 139624e51604SNeil Leeder struct acpi_iort_node *node); 1397896dd2c3SLorenzo Pieralisi int (*dev_count_resources)(struct acpi_iort_node *node); 1398896dd2c3SLorenzo Pieralisi void (*dev_init_resources)(struct resource *res, 1399846f0e9eSLorenzo Pieralisi struct acpi_iort_node *node); 140036a2ba07SKefeng Wang int (*dev_set_proximity)(struct device *dev, 14015fe0ce3bSGanapatrao Kulkarni struct acpi_iort_node *node); 140224e51604SNeil Leeder int (*dev_add_platdata)(struct platform_device *pdev); 1403846f0e9eSLorenzo Pieralisi }; 1404846f0e9eSLorenzo Pieralisi 1405896dd2c3SLorenzo Pieralisi static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { 1406e4dadfa8SLorenzo Pieralisi .name = "arm-smmu-v3", 140724e51604SNeil Leeder .dev_dma_configure = arm_smmu_v3_dma_configure, 1408896dd2c3SLorenzo Pieralisi .dev_count_resources = arm_smmu_v3_count_resources, 1409896dd2c3SLorenzo Pieralisi .dev_init_resources = arm_smmu_v3_init_resources, 1410896dd2c3SLorenzo Pieralisi .dev_set_proximity = arm_smmu_v3_set_proximity, 1411e4dadfa8SLorenzo Pieralisi }; 1412e4dadfa8SLorenzo Pieralisi 1413896dd2c3SLorenzo Pieralisi static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { 1414d6fcd3b1SLorenzo Pieralisi .name = "arm-smmu", 141524e51604SNeil Leeder .dev_dma_configure = arm_smmu_dma_configure, 1416896dd2c3SLorenzo Pieralisi .dev_count_resources = arm_smmu_count_resources, 141724e51604SNeil Leeder .dev_init_resources = arm_smmu_init_resources, 141824e51604SNeil Leeder }; 141924e51604SNeil Leeder 142024e51604SNeil Leeder static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { 142124e51604SNeil Leeder .name = "arm-smmu-v3-pmcg", 142224e51604SNeil Leeder .dev_count_resources = arm_smmu_v3_pmcg_count_resources, 142324e51604SNeil Leeder .dev_init_resources = arm_smmu_v3_pmcg_init_resources, 142424e51604SNeil Leeder .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, 1425d6fcd3b1SLorenzo Pieralisi }; 1426d6fcd3b1SLorenzo Pieralisi 1427896dd2c3SLorenzo Pieralisi static __init const struct iort_dev_config *iort_get_dev_cfg( 1428e3d49392SLorenzo Pieralisi struct acpi_iort_node *node) 1429846f0e9eSLorenzo Pieralisi { 1430e4dadfa8SLorenzo Pieralisi switch (node->type) { 1431e4dadfa8SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU_V3: 1432e4dadfa8SLorenzo Pieralisi return &iort_arm_smmu_v3_cfg; 1433d6fcd3b1SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU: 1434d6fcd3b1SLorenzo Pieralisi return &iort_arm_smmu_cfg; 143524e51604SNeil Leeder case ACPI_IORT_NODE_PMCG: 143624e51604SNeil Leeder return &iort_arm_smmu_v3_pmcg_cfg; 1437e4dadfa8SLorenzo Pieralisi default: 1438846f0e9eSLorenzo Pieralisi return NULL; 1439846f0e9eSLorenzo Pieralisi } 1440e4dadfa8SLorenzo Pieralisi } 1441846f0e9eSLorenzo Pieralisi 1442846f0e9eSLorenzo Pieralisi /** 1443896dd2c3SLorenzo Pieralisi * iort_add_platform_device() - Allocate a platform device for IORT node 1444896dd2c3SLorenzo Pieralisi * @node: Pointer to device ACPI IORT node 1445774c4a3bSShiju Jose * @ops: Pointer to IORT device config struct 1446846f0e9eSLorenzo Pieralisi * 1447846f0e9eSLorenzo Pieralisi * Returns: 0 on success, <0 failure 1448846f0e9eSLorenzo Pieralisi */ 1449896dd2c3SLorenzo Pieralisi static int __init iort_add_platform_device(struct acpi_iort_node *node, 1450896dd2c3SLorenzo Pieralisi const struct iort_dev_config *ops) 1451846f0e9eSLorenzo Pieralisi { 1452846f0e9eSLorenzo Pieralisi struct fwnode_handle *fwnode; 1453846f0e9eSLorenzo Pieralisi struct platform_device *pdev; 1454846f0e9eSLorenzo Pieralisi struct resource *r; 1455846f0e9eSLorenzo Pieralisi int ret, count; 1456846f0e9eSLorenzo Pieralisi 1457846f0e9eSLorenzo Pieralisi pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); 1458846f0e9eSLorenzo Pieralisi if (!pdev) 14595e5afa6cSDan Carpenter return -ENOMEM; 1460846f0e9eSLorenzo Pieralisi 146136a2ba07SKefeng Wang if (ops->dev_set_proximity) { 146236a2ba07SKefeng Wang ret = ops->dev_set_proximity(&pdev->dev, node); 146336a2ba07SKefeng Wang if (ret) 146436a2ba07SKefeng Wang goto dev_put; 146536a2ba07SKefeng Wang } 14665fe0ce3bSGanapatrao Kulkarni 1467896dd2c3SLorenzo Pieralisi count = ops->dev_count_resources(node); 1468846f0e9eSLorenzo Pieralisi 1469846f0e9eSLorenzo Pieralisi r = kcalloc(count, sizeof(*r), GFP_KERNEL); 1470846f0e9eSLorenzo Pieralisi if (!r) { 1471846f0e9eSLorenzo Pieralisi ret = -ENOMEM; 1472846f0e9eSLorenzo Pieralisi goto dev_put; 1473846f0e9eSLorenzo Pieralisi } 1474846f0e9eSLorenzo Pieralisi 1475896dd2c3SLorenzo Pieralisi ops->dev_init_resources(r, node); 1476846f0e9eSLorenzo Pieralisi 1477846f0e9eSLorenzo Pieralisi ret = platform_device_add_resources(pdev, r, count); 1478846f0e9eSLorenzo Pieralisi /* 1479846f0e9eSLorenzo Pieralisi * Resources are duplicated in platform_device_add_resources, 1480846f0e9eSLorenzo Pieralisi * free their allocated memory 1481846f0e9eSLorenzo Pieralisi */ 1482846f0e9eSLorenzo Pieralisi kfree(r); 1483846f0e9eSLorenzo Pieralisi 1484846f0e9eSLorenzo Pieralisi if (ret) 1485846f0e9eSLorenzo Pieralisi goto dev_put; 1486846f0e9eSLorenzo Pieralisi 1487846f0e9eSLorenzo Pieralisi /* 148824e51604SNeil Leeder * Platform devices based on PMCG nodes uses platform_data to 148924e51604SNeil Leeder * pass the hardware model info to the driver. For others, add 149024e51604SNeil Leeder * a copy of IORT node pointer to platform_data to be used to 149124e51604SNeil Leeder * retrieve IORT data information. 1492846f0e9eSLorenzo Pieralisi */ 149324e51604SNeil Leeder if (ops->dev_add_platdata) 149424e51604SNeil Leeder ret = ops->dev_add_platdata(pdev); 149524e51604SNeil Leeder else 1496846f0e9eSLorenzo Pieralisi ret = platform_device_add_data(pdev, &node, sizeof(node)); 149724e51604SNeil Leeder 1498846f0e9eSLorenzo Pieralisi if (ret) 1499846f0e9eSLorenzo Pieralisi goto dev_put; 1500846f0e9eSLorenzo Pieralisi 1501846f0e9eSLorenzo Pieralisi fwnode = iort_get_fwnode(node); 1502846f0e9eSLorenzo Pieralisi 1503846f0e9eSLorenzo Pieralisi if (!fwnode) { 1504846f0e9eSLorenzo Pieralisi ret = -ENODEV; 1505846f0e9eSLorenzo Pieralisi goto dev_put; 1506846f0e9eSLorenzo Pieralisi } 1507846f0e9eSLorenzo Pieralisi 1508846f0e9eSLorenzo Pieralisi pdev->dev.fwnode = fwnode; 1509846f0e9eSLorenzo Pieralisi 151024e51604SNeil Leeder if (ops->dev_dma_configure) 151124e51604SNeil Leeder ops->dev_dma_configure(&pdev->dev, node); 1512846f0e9eSLorenzo Pieralisi 151365637901SLorenzo Pieralisi iort_set_device_domain(&pdev->dev, node); 151465637901SLorenzo Pieralisi 1515846f0e9eSLorenzo Pieralisi ret = platform_device_add(pdev); 1516846f0e9eSLorenzo Pieralisi if (ret) 1517846f0e9eSLorenzo Pieralisi goto dma_deconfigure; 1518846f0e9eSLorenzo Pieralisi 1519846f0e9eSLorenzo Pieralisi return 0; 1520846f0e9eSLorenzo Pieralisi 1521846f0e9eSLorenzo Pieralisi dma_deconfigure: 1522dc3c0550SChristoph Hellwig arch_teardown_dma_ops(&pdev->dev); 1523846f0e9eSLorenzo Pieralisi dev_put: 1524846f0e9eSLorenzo Pieralisi platform_device_put(pdev); 1525846f0e9eSLorenzo Pieralisi 1526846f0e9eSLorenzo Pieralisi return ret; 1527846f0e9eSLorenzo Pieralisi } 1528846f0e9eSLorenzo Pieralisi 152943554cebSSinan Kaya #ifdef CONFIG_PCI 153043554cebSSinan Kaya static void __init iort_enable_acs(struct acpi_iort_node *iort_node) 153137f6b42eSLorenzo Pieralisi { 153243554cebSSinan Kaya static bool acs_enabled __initdata; 153343554cebSSinan Kaya 153443554cebSSinan Kaya if (acs_enabled) 153543554cebSSinan Kaya return; 153643554cebSSinan Kaya 153737f6b42eSLorenzo Pieralisi if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 153837f6b42eSLorenzo Pieralisi struct acpi_iort_node *parent; 153937f6b42eSLorenzo Pieralisi struct acpi_iort_id_mapping *map; 154037f6b42eSLorenzo Pieralisi int i; 154137f6b42eSLorenzo Pieralisi 154237f6b42eSLorenzo Pieralisi map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, 154337f6b42eSLorenzo Pieralisi iort_node->mapping_offset); 154437f6b42eSLorenzo Pieralisi 154537f6b42eSLorenzo Pieralisi for (i = 0; i < iort_node->mapping_count; i++, map++) { 154637f6b42eSLorenzo Pieralisi if (!map->output_reference) 154737f6b42eSLorenzo Pieralisi continue; 154837f6b42eSLorenzo Pieralisi 154937f6b42eSLorenzo Pieralisi parent = ACPI_ADD_PTR(struct acpi_iort_node, 155037f6b42eSLorenzo Pieralisi iort_table, map->output_reference); 155137f6b42eSLorenzo Pieralisi /* 155237f6b42eSLorenzo Pieralisi * If we detect a RC->SMMU mapping, make sure 155337f6b42eSLorenzo Pieralisi * we enable ACS on the system. 155437f6b42eSLorenzo Pieralisi */ 155537f6b42eSLorenzo Pieralisi if ((parent->type == ACPI_IORT_NODE_SMMU) || 155637f6b42eSLorenzo Pieralisi (parent->type == ACPI_IORT_NODE_SMMU_V3)) { 155737f6b42eSLorenzo Pieralisi pci_request_acs(); 155843554cebSSinan Kaya acs_enabled = true; 155943554cebSSinan Kaya return; 156037f6b42eSLorenzo Pieralisi } 156137f6b42eSLorenzo Pieralisi } 156237f6b42eSLorenzo Pieralisi } 156337f6b42eSLorenzo Pieralisi } 156443554cebSSinan Kaya #else 156543554cebSSinan Kaya static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } 156643554cebSSinan Kaya #endif 156737f6b42eSLorenzo Pieralisi 1568846f0e9eSLorenzo Pieralisi static void __init iort_init_platform_devices(void) 1569846f0e9eSLorenzo Pieralisi { 1570846f0e9eSLorenzo Pieralisi struct acpi_iort_node *iort_node, *iort_end; 1571846f0e9eSLorenzo Pieralisi struct acpi_table_iort *iort; 1572846f0e9eSLorenzo Pieralisi struct fwnode_handle *fwnode; 1573846f0e9eSLorenzo Pieralisi int i, ret; 1574896dd2c3SLorenzo Pieralisi const struct iort_dev_config *ops; 1575846f0e9eSLorenzo Pieralisi 1576846f0e9eSLorenzo Pieralisi /* 1577846f0e9eSLorenzo Pieralisi * iort_table and iort both point to the start of IORT table, but 1578846f0e9eSLorenzo Pieralisi * have different struct types 1579846f0e9eSLorenzo Pieralisi */ 1580846f0e9eSLorenzo Pieralisi iort = (struct acpi_table_iort *)iort_table; 1581846f0e9eSLorenzo Pieralisi 1582846f0e9eSLorenzo Pieralisi /* Get the first IORT node */ 1583846f0e9eSLorenzo Pieralisi iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1584846f0e9eSLorenzo Pieralisi iort->node_offset); 1585846f0e9eSLorenzo Pieralisi iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1586846f0e9eSLorenzo Pieralisi iort_table->length); 1587846f0e9eSLorenzo Pieralisi 1588846f0e9eSLorenzo Pieralisi for (i = 0; i < iort->node_count; i++) { 1589846f0e9eSLorenzo Pieralisi if (iort_node >= iort_end) { 1590846f0e9eSLorenzo Pieralisi pr_err("iort node pointer overflows, bad table\n"); 1591846f0e9eSLorenzo Pieralisi return; 1592846f0e9eSLorenzo Pieralisi } 1593846f0e9eSLorenzo Pieralisi 159443554cebSSinan Kaya iort_enable_acs(iort_node); 159537f6b42eSLorenzo Pieralisi 1596896dd2c3SLorenzo Pieralisi ops = iort_get_dev_cfg(iort_node); 1597896dd2c3SLorenzo Pieralisi if (ops) { 1598846f0e9eSLorenzo Pieralisi fwnode = acpi_alloc_fwnode_static(); 1599846f0e9eSLorenzo Pieralisi if (!fwnode) 1600846f0e9eSLorenzo Pieralisi return; 1601846f0e9eSLorenzo Pieralisi 1602846f0e9eSLorenzo Pieralisi iort_set_fwnode(iort_node, fwnode); 1603846f0e9eSLorenzo Pieralisi 1604896dd2c3SLorenzo Pieralisi ret = iort_add_platform_device(iort_node, ops); 1605846f0e9eSLorenzo Pieralisi if (ret) { 1606846f0e9eSLorenzo Pieralisi iort_delete_fwnode(iort_node); 1607846f0e9eSLorenzo Pieralisi acpi_free_fwnode_static(fwnode); 1608846f0e9eSLorenzo Pieralisi return; 1609846f0e9eSLorenzo Pieralisi } 1610846f0e9eSLorenzo Pieralisi } 1611846f0e9eSLorenzo Pieralisi 1612846f0e9eSLorenzo Pieralisi iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1613846f0e9eSLorenzo Pieralisi iort_node->length); 1614846f0e9eSLorenzo Pieralisi } 1615846f0e9eSLorenzo Pieralisi } 1616846f0e9eSLorenzo Pieralisi 161788ef16d8STomasz Nowicki void __init acpi_iort_init(void) 161888ef16d8STomasz Nowicki { 161988ef16d8STomasz Nowicki acpi_status status; 162088ef16d8STomasz Nowicki 1621701dafe0SHanjun Guo /* iort_table will be used at runtime after the iort init, 1622701dafe0SHanjun Guo * so we don't need to call acpi_put_table() to release 1623701dafe0SHanjun Guo * the IORT table mapping. 1624701dafe0SHanjun Guo */ 162588ef16d8STomasz Nowicki status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); 162634ceea27SLorenzo Pieralisi if (ACPI_FAILURE(status)) { 162734ceea27SLorenzo Pieralisi if (status != AE_NOT_FOUND) { 162888ef16d8STomasz Nowicki const char *msg = acpi_format_exception(status); 162934ceea27SLorenzo Pieralisi 163088ef16d8STomasz Nowicki pr_err("Failed to get table, %s\n", msg); 163188ef16d8STomasz Nowicki } 163234ceea27SLorenzo Pieralisi 163334ceea27SLorenzo Pieralisi return; 163434ceea27SLorenzo Pieralisi } 163534ceea27SLorenzo Pieralisi 1636846f0e9eSLorenzo Pieralisi iort_init_platform_devices(); 163788ef16d8STomasz Nowicki } 16382b865293SArd Biesheuvel 16392b865293SArd Biesheuvel #ifdef CONFIG_ZONE_DMA 16402b865293SArd Biesheuvel /* 16412b865293SArd Biesheuvel * Extract the highest CPU physical address accessible to all DMA masters in 16422b865293SArd Biesheuvel * the system. PHYS_ADDR_MAX is returned when no constrained device is found. 16432b865293SArd Biesheuvel */ 16442b865293SArd Biesheuvel phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void) 16452b865293SArd Biesheuvel { 16462b865293SArd Biesheuvel phys_addr_t limit = PHYS_ADDR_MAX; 16472b865293SArd Biesheuvel struct acpi_iort_node *node, *end; 16482b865293SArd Biesheuvel struct acpi_table_iort *iort; 16492b865293SArd Biesheuvel acpi_status status; 16502b865293SArd Biesheuvel int i; 16512b865293SArd Biesheuvel 16522b865293SArd Biesheuvel if (acpi_disabled) 16532b865293SArd Biesheuvel return limit; 16542b865293SArd Biesheuvel 16552b865293SArd Biesheuvel status = acpi_get_table(ACPI_SIG_IORT, 0, 16562b865293SArd Biesheuvel (struct acpi_table_header **)&iort); 16572b865293SArd Biesheuvel if (ACPI_FAILURE(status)) 16582b865293SArd Biesheuvel return limit; 16592b865293SArd Biesheuvel 16602b865293SArd Biesheuvel node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset); 16612b865293SArd Biesheuvel end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length); 16622b865293SArd Biesheuvel 16632b865293SArd Biesheuvel for (i = 0; i < iort->node_count; i++) { 16642b865293SArd Biesheuvel if (node >= end) 16652b865293SArd Biesheuvel break; 16662b865293SArd Biesheuvel 16672b865293SArd Biesheuvel switch (node->type) { 16682b865293SArd Biesheuvel struct acpi_iort_named_component *ncomp; 16692b865293SArd Biesheuvel struct acpi_iort_root_complex *rc; 16702b865293SArd Biesheuvel phys_addr_t local_limit; 16712b865293SArd Biesheuvel 16722b865293SArd Biesheuvel case ACPI_IORT_NODE_NAMED_COMPONENT: 16732b865293SArd Biesheuvel ncomp = (struct acpi_iort_named_component *)node->node_data; 16742b865293SArd Biesheuvel local_limit = DMA_BIT_MASK(ncomp->memory_address_limit); 16752b865293SArd Biesheuvel limit = min_not_zero(limit, local_limit); 16762b865293SArd Biesheuvel break; 16772b865293SArd Biesheuvel 16782b865293SArd Biesheuvel case ACPI_IORT_NODE_PCI_ROOT_COMPLEX: 16792b865293SArd Biesheuvel if (node->revision < 1) 16802b865293SArd Biesheuvel break; 16812b865293SArd Biesheuvel 16822b865293SArd Biesheuvel rc = (struct acpi_iort_root_complex *)node->node_data; 16832b865293SArd Biesheuvel local_limit = DMA_BIT_MASK(rc->memory_address_limit); 16842b865293SArd Biesheuvel limit = min_not_zero(limit, local_limit); 16852b865293SArd Biesheuvel break; 16862b865293SArd Biesheuvel } 16872b865293SArd Biesheuvel node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length); 16882b865293SArd Biesheuvel } 16892b865293SArd Biesheuvel acpi_put_table(&iort->header); 16902b865293SArd Biesheuvel return limit; 16912b865293SArd Biesheuvel } 16922b865293SArd Biesheuvel #endif 1693