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 791491cf4a6SShameer Kolothum static void iort_rmr_free(struct device *dev, 792491cf4a6SShameer Kolothum struct iommu_resv_region *region) 793491cf4a6SShameer Kolothum { 794491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 795491cf4a6SShameer Kolothum 796491cf4a6SShameer Kolothum rmr_data = container_of(region, struct iommu_iort_rmr_data, rr); 797491cf4a6SShameer Kolothum kfree(rmr_data->sids); 798491cf4a6SShameer Kolothum kfree(rmr_data); 799491cf4a6SShameer Kolothum } 800491cf4a6SShameer Kolothum 801491cf4a6SShameer Kolothum static struct iommu_iort_rmr_data *iort_rmr_alloc( 802491cf4a6SShameer Kolothum struct acpi_iort_rmr_desc *rmr_desc, 803491cf4a6SShameer Kolothum int prot, enum iommu_resv_type type, 804491cf4a6SShameer Kolothum u32 *sids, u32 num_sids) 805491cf4a6SShameer Kolothum { 806491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 807491cf4a6SShameer Kolothum struct iommu_resv_region *region; 808491cf4a6SShameer Kolothum u32 *sids_copy; 809491cf4a6SShameer Kolothum u64 addr = rmr_desc->base_address, size = rmr_desc->length; 810491cf4a6SShameer Kolothum 811491cf4a6SShameer Kolothum rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL); 812491cf4a6SShameer Kolothum if (!rmr_data) 813491cf4a6SShameer Kolothum return NULL; 814491cf4a6SShameer Kolothum 815491cf4a6SShameer Kolothum /* Create a copy of SIDs array to associate with this rmr_data */ 816491cf4a6SShameer Kolothum sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL); 817491cf4a6SShameer Kolothum if (!sids_copy) { 818491cf4a6SShameer Kolothum kfree(rmr_data); 819491cf4a6SShameer Kolothum return NULL; 820491cf4a6SShameer Kolothum } 821491cf4a6SShameer Kolothum rmr_data->sids = sids_copy; 822491cf4a6SShameer Kolothum rmr_data->num_sids = num_sids; 823491cf4a6SShameer Kolothum 824491cf4a6SShameer Kolothum if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) { 825491cf4a6SShameer Kolothum /* PAGE align base addr and size */ 826491cf4a6SShameer Kolothum addr &= PAGE_MASK; 827491cf4a6SShameer Kolothum size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address)); 828491cf4a6SShameer Kolothum 829491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n", 830491cf4a6SShameer Kolothum rmr_desc->base_address, 831491cf4a6SShameer Kolothum rmr_desc->base_address + rmr_desc->length - 1, 832491cf4a6SShameer Kolothum addr, addr + size - 1); 833491cf4a6SShameer Kolothum } 834491cf4a6SShameer Kolothum 835491cf4a6SShameer Kolothum region = &rmr_data->rr; 836491cf4a6SShameer Kolothum INIT_LIST_HEAD(®ion->list); 837491cf4a6SShameer Kolothum region->start = addr; 838491cf4a6SShameer Kolothum region->length = size; 839491cf4a6SShameer Kolothum region->prot = prot; 840491cf4a6SShameer Kolothum region->type = type; 841491cf4a6SShameer Kolothum region->free = iort_rmr_free; 842491cf4a6SShameer Kolothum 843491cf4a6SShameer Kolothum return rmr_data; 844491cf4a6SShameer Kolothum } 845491cf4a6SShameer Kolothum 846491cf4a6SShameer Kolothum static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc, 847491cf4a6SShameer Kolothum u32 count) 848491cf4a6SShameer Kolothum { 849491cf4a6SShameer Kolothum int i, j; 850491cf4a6SShameer Kolothum 851491cf4a6SShameer Kolothum for (i = 0; i < count; i++) { 852491cf4a6SShameer Kolothum u64 end, start = desc[i].base_address, length = desc[i].length; 853491cf4a6SShameer Kolothum 854491cf4a6SShameer Kolothum if (!length) { 855491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n", 856491cf4a6SShameer Kolothum start); 857491cf4a6SShameer Kolothum continue; 858491cf4a6SShameer Kolothum } 859491cf4a6SShameer Kolothum 860491cf4a6SShameer Kolothum end = start + length - 1; 861491cf4a6SShameer Kolothum 862491cf4a6SShameer Kolothum /* Check for address overlap */ 863491cf4a6SShameer Kolothum for (j = i + 1; j < count; j++) { 864491cf4a6SShameer Kolothum u64 e_start = desc[j].base_address; 865491cf4a6SShameer Kolothum u64 e_end = e_start + desc[j].length - 1; 866491cf4a6SShameer Kolothum 867491cf4a6SShameer Kolothum if (start <= e_end && end >= e_start) 868491cf4a6SShameer Kolothum pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n", 869491cf4a6SShameer Kolothum start, end); 870491cf4a6SShameer Kolothum } 871491cf4a6SShameer Kolothum } 872491cf4a6SShameer Kolothum } 873491cf4a6SShameer Kolothum 874491cf4a6SShameer Kolothum /* 875491cf4a6SShameer Kolothum * Please note, we will keep the already allocated RMR reserve 876491cf4a6SShameer Kolothum * regions in case of a memory allocation failure. 877491cf4a6SShameer Kolothum */ 878491cf4a6SShameer Kolothum static void iort_get_rmrs(struct acpi_iort_node *node, 879491cf4a6SShameer Kolothum struct acpi_iort_node *smmu, 880491cf4a6SShameer Kolothum u32 *sids, u32 num_sids, 881491cf4a6SShameer Kolothum struct list_head *head) 882491cf4a6SShameer Kolothum { 883491cf4a6SShameer Kolothum struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data; 884491cf4a6SShameer Kolothum struct acpi_iort_rmr_desc *rmr_desc; 885491cf4a6SShameer Kolothum int i; 886491cf4a6SShameer Kolothum 887491cf4a6SShameer Kolothum rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node, 888491cf4a6SShameer Kolothum rmr->rmr_offset); 889491cf4a6SShameer Kolothum 890491cf4a6SShameer Kolothum iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count); 891491cf4a6SShameer Kolothum 892491cf4a6SShameer Kolothum for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) { 893491cf4a6SShameer Kolothum struct iommu_iort_rmr_data *rmr_data; 894491cf4a6SShameer Kolothum enum iommu_resv_type type; 895491cf4a6SShameer Kolothum int prot = IOMMU_READ | IOMMU_WRITE; 896491cf4a6SShameer Kolothum 897491cf4a6SShameer Kolothum if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED) 898491cf4a6SShameer Kolothum type = IOMMU_RESV_DIRECT_RELAXABLE; 899491cf4a6SShameer Kolothum else 900491cf4a6SShameer Kolothum type = IOMMU_RESV_DIRECT; 901491cf4a6SShameer Kolothum 902491cf4a6SShameer Kolothum if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE) 903491cf4a6SShameer Kolothum prot |= IOMMU_PRIV; 904491cf4a6SShameer Kolothum 905491cf4a6SShameer Kolothum /* Attributes 0x00 - 0x03 represents device memory */ 906491cf4a6SShameer Kolothum if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <= 907491cf4a6SShameer Kolothum ACPI_IORT_RMR_ATTR_DEVICE_GRE) 908491cf4a6SShameer Kolothum prot |= IOMMU_MMIO; 909491cf4a6SShameer Kolothum else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) == 910491cf4a6SShameer Kolothum ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB) 911491cf4a6SShameer Kolothum prot |= IOMMU_CACHE; 912491cf4a6SShameer Kolothum 913491cf4a6SShameer Kolothum rmr_data = iort_rmr_alloc(rmr_desc, prot, type, 914491cf4a6SShameer Kolothum sids, num_sids); 915491cf4a6SShameer Kolothum if (!rmr_data) 916491cf4a6SShameer Kolothum return; 917491cf4a6SShameer Kolothum 918491cf4a6SShameer Kolothum list_add_tail(&rmr_data->rr.list, head); 919491cf4a6SShameer Kolothum } 920491cf4a6SShameer Kolothum } 921491cf4a6SShameer Kolothum 922491cf4a6SShameer Kolothum static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start, 923491cf4a6SShameer Kolothum u32 new_count) 924491cf4a6SShameer Kolothum { 925491cf4a6SShameer Kolothum u32 *new_sids; 926491cf4a6SShameer Kolothum u32 total_count = count + new_count; 927491cf4a6SShameer Kolothum int i; 928491cf4a6SShameer Kolothum 929491cf4a6SShameer Kolothum new_sids = krealloc_array(sids, count + new_count, 930491cf4a6SShameer Kolothum sizeof(*new_sids), GFP_KERNEL); 931491cf4a6SShameer Kolothum if (!new_sids) 932491cf4a6SShameer Kolothum return NULL; 933491cf4a6SShameer Kolothum 934491cf4a6SShameer Kolothum for (i = count; i < total_count; i++) 935491cf4a6SShameer Kolothum new_sids[i] = id_start++; 936491cf4a6SShameer Kolothum 937491cf4a6SShameer Kolothum return new_sids; 938491cf4a6SShameer Kolothum } 939491cf4a6SShameer Kolothum 940491cf4a6SShameer Kolothum static bool iort_rmr_has_dev(struct device *dev, u32 id_start, 941491cf4a6SShameer Kolothum u32 id_count) 942491cf4a6SShameer Kolothum { 943491cf4a6SShameer Kolothum int i; 944491cf4a6SShameer Kolothum struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 945491cf4a6SShameer Kolothum 946491cf4a6SShameer Kolothum /* 947491cf4a6SShameer Kolothum * Make sure the kernel has preserved the boot firmware PCIe 948491cf4a6SShameer Kolothum * configuration. This is required to ensure that the RMR PCIe 949491cf4a6SShameer Kolothum * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5). 950491cf4a6SShameer Kolothum */ 951491cf4a6SShameer Kolothum if (dev_is_pci(dev)) { 952491cf4a6SShameer Kolothum struct pci_dev *pdev = to_pci_dev(dev); 953491cf4a6SShameer Kolothum struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus); 954491cf4a6SShameer Kolothum 955491cf4a6SShameer Kolothum if (!host->preserve_config) 956491cf4a6SShameer Kolothum return false; 957491cf4a6SShameer Kolothum } 958491cf4a6SShameer Kolothum 959491cf4a6SShameer Kolothum for (i = 0; i < fwspec->num_ids; i++) { 960491cf4a6SShameer Kolothum if (fwspec->ids[i] >= id_start && 961491cf4a6SShameer Kolothum fwspec->ids[i] <= id_start + id_count) 962491cf4a6SShameer Kolothum return true; 963491cf4a6SShameer Kolothum } 964491cf4a6SShameer Kolothum 965491cf4a6SShameer Kolothum return false; 966491cf4a6SShameer Kolothum } 967491cf4a6SShameer Kolothum 968491cf4a6SShameer Kolothum static void iort_node_get_rmr_info(struct acpi_iort_node *node, 969491cf4a6SShameer Kolothum struct acpi_iort_node *iommu, 970491cf4a6SShameer Kolothum struct device *dev, struct list_head *head) 971491cf4a6SShameer Kolothum { 972491cf4a6SShameer Kolothum struct acpi_iort_node *smmu = NULL; 973491cf4a6SShameer Kolothum struct acpi_iort_rmr *rmr; 974491cf4a6SShameer Kolothum struct acpi_iort_id_mapping *map; 975491cf4a6SShameer Kolothum u32 *sids = NULL; 976491cf4a6SShameer Kolothum u32 num_sids = 0; 977491cf4a6SShameer Kolothum int i; 978491cf4a6SShameer Kolothum 979491cf4a6SShameer Kolothum if (!node->mapping_offset || !node->mapping_count) { 980491cf4a6SShameer Kolothum pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n", 981491cf4a6SShameer Kolothum node); 982491cf4a6SShameer Kolothum return; 983491cf4a6SShameer Kolothum } 984491cf4a6SShameer Kolothum 985491cf4a6SShameer Kolothum rmr = (struct acpi_iort_rmr *)node->node_data; 986491cf4a6SShameer Kolothum if (!rmr->rmr_offset || !rmr->rmr_count) 987491cf4a6SShameer Kolothum return; 988491cf4a6SShameer Kolothum 989491cf4a6SShameer Kolothum map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 990491cf4a6SShameer Kolothum node->mapping_offset); 991491cf4a6SShameer Kolothum 992491cf4a6SShameer Kolothum /* 993491cf4a6SShameer Kolothum * Go through the ID mappings and see if we have a match for SMMU 994491cf4a6SShameer Kolothum * and dev(if !NULL). If found, get the sids for the Node. 995491cf4a6SShameer Kolothum * Please note, id_count is equal to the number of IDs in the 996491cf4a6SShameer Kolothum * range minus one. 997491cf4a6SShameer Kolothum */ 998491cf4a6SShameer Kolothum for (i = 0; i < node->mapping_count; i++, map++) { 999491cf4a6SShameer Kolothum struct acpi_iort_node *parent; 1000491cf4a6SShameer Kolothum 1001491cf4a6SShameer Kolothum if (!map->id_count) 1002491cf4a6SShameer Kolothum continue; 1003491cf4a6SShameer Kolothum 1004491cf4a6SShameer Kolothum parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 1005491cf4a6SShameer Kolothum map->output_reference); 1006491cf4a6SShameer Kolothum if (parent != iommu) 1007491cf4a6SShameer Kolothum continue; 1008491cf4a6SShameer Kolothum 1009491cf4a6SShameer Kolothum /* If dev is valid, check RMR node corresponds to the dev SID */ 1010491cf4a6SShameer Kolothum if (dev && !iort_rmr_has_dev(dev, map->output_base, 1011491cf4a6SShameer Kolothum map->id_count)) 1012491cf4a6SShameer Kolothum continue; 1013491cf4a6SShameer Kolothum 1014491cf4a6SShameer Kolothum /* Retrieve SIDs associated with the Node. */ 1015491cf4a6SShameer Kolothum sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base, 1016491cf4a6SShameer Kolothum map->id_count + 1); 1017491cf4a6SShameer Kolothum if (!sids) 1018491cf4a6SShameer Kolothum return; 1019491cf4a6SShameer Kolothum 1020491cf4a6SShameer Kolothum num_sids += map->id_count + 1; 1021491cf4a6SShameer Kolothum } 1022491cf4a6SShameer Kolothum 1023491cf4a6SShameer Kolothum if (!sids) 1024491cf4a6SShameer Kolothum return; 1025491cf4a6SShameer Kolothum 1026491cf4a6SShameer Kolothum iort_get_rmrs(node, smmu, sids, num_sids, head); 1027491cf4a6SShameer Kolothum kfree(sids); 1028491cf4a6SShameer Kolothum } 1029491cf4a6SShameer Kolothum 1030491cf4a6SShameer Kolothum static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev, 1031491cf4a6SShameer Kolothum struct list_head *head) 1032491cf4a6SShameer Kolothum { 1033491cf4a6SShameer Kolothum struct acpi_table_iort *iort; 1034491cf4a6SShameer Kolothum struct acpi_iort_node *iort_node, *iort_end; 1035491cf4a6SShameer Kolothum int i; 1036491cf4a6SShameer Kolothum 1037491cf4a6SShameer Kolothum /* Only supports ARM DEN 0049E.d onwards */ 1038491cf4a6SShameer Kolothum if (iort_table->revision < 5) 1039491cf4a6SShameer Kolothum return; 1040491cf4a6SShameer Kolothum 1041491cf4a6SShameer Kolothum iort = (struct acpi_table_iort *)iort_table; 1042491cf4a6SShameer Kolothum 1043491cf4a6SShameer Kolothum iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1044491cf4a6SShameer Kolothum iort->node_offset); 1045491cf4a6SShameer Kolothum iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1046491cf4a6SShameer Kolothum iort_table->length); 1047491cf4a6SShameer Kolothum 1048491cf4a6SShameer Kolothum for (i = 0; i < iort->node_count; i++) { 1049491cf4a6SShameer Kolothum if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 1050491cf4a6SShameer Kolothum "IORT node pointer overflows, bad table!\n")) 1051491cf4a6SShameer Kolothum return; 1052491cf4a6SShameer Kolothum 1053491cf4a6SShameer Kolothum if (iort_node->type == ACPI_IORT_NODE_RMR) 1054491cf4a6SShameer Kolothum iort_node_get_rmr_info(iort_node, iommu, dev, head); 1055491cf4a6SShameer Kolothum 1056491cf4a6SShameer Kolothum iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1057491cf4a6SShameer Kolothum iort_node->length); 1058491cf4a6SShameer Kolothum } 1059491cf4a6SShameer Kolothum } 1060491cf4a6SShameer Kolothum 1061491cf4a6SShameer Kolothum /* 1062491cf4a6SShameer Kolothum * Populate the RMR list associated with a given IOMMU and dev(if provided). 1063491cf4a6SShameer Kolothum * If dev is NULL, the function populates all the RMRs associated with the 1064491cf4a6SShameer Kolothum * given IOMMU. 1065491cf4a6SShameer Kolothum */ 1066491cf4a6SShameer Kolothum static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode, 1067491cf4a6SShameer Kolothum struct device *dev, 1068491cf4a6SShameer Kolothum struct list_head *head) 1069491cf4a6SShameer Kolothum { 1070491cf4a6SShameer Kolothum struct acpi_iort_node *iommu; 1071491cf4a6SShameer Kolothum 1072491cf4a6SShameer Kolothum iommu = iort_get_iort_node(iommu_fwnode); 1073491cf4a6SShameer Kolothum if (!iommu) 1074491cf4a6SShameer Kolothum return; 1075491cf4a6SShameer Kolothum 1076491cf4a6SShameer Kolothum iort_find_rmrs(iommu, dev, head); 1077491cf4a6SShameer Kolothum } 1078491cf4a6SShameer Kolothum 10798b4282e6SShameer Kolothum static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) 10808b4282e6SShameer Kolothum { 10818b4282e6SShameer Kolothum struct acpi_iort_node *iommu; 10828097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 10838b4282e6SShameer Kolothum 10848b4282e6SShameer Kolothum iommu = iort_get_iort_node(fwspec->iommu_fwnode); 10858b4282e6SShameer Kolothum 10868b4282e6SShameer Kolothum if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { 10878b4282e6SShameer Kolothum struct acpi_iort_smmu_v3 *smmu; 10888b4282e6SShameer Kolothum 10898b4282e6SShameer Kolothum smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; 10908b4282e6SShameer Kolothum if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) 10918b4282e6SShameer Kolothum return iommu; 10928b4282e6SShameer Kolothum } 10938b4282e6SShameer Kolothum 10948b4282e6SShameer Kolothum return NULL; 10958b4282e6SShameer Kolothum } 10968b4282e6SShameer Kolothum 109755be25b8SShameer Kolothum /* 109855be25b8SShameer Kolothum * Retrieve platform specific HW MSI reserve regions. 10998778b1d4SShameer Kolothum * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K) 11008778b1d4SShameer Kolothum * associated with the device are the HW MSI reserved regions. 11018b4282e6SShameer Kolothum */ 110255be25b8SShameer Kolothum static void iort_iommu_msi_get_resv_regions(struct device *dev, 110355be25b8SShameer Kolothum struct list_head *head) 11048b4282e6SShameer Kolothum { 11058097e53eSJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 11068b4282e6SShameer Kolothum struct acpi_iort_its_group *its; 11078b4282e6SShameer Kolothum struct acpi_iort_node *iommu_node, *its_node = NULL; 11088778b1d4SShameer Kolothum int i; 11098b4282e6SShameer Kolothum 11108b4282e6SShameer Kolothum iommu_node = iort_get_msi_resv_iommu(dev); 11118b4282e6SShameer Kolothum if (!iommu_node) 11128778b1d4SShameer Kolothum return; 11138b4282e6SShameer Kolothum 11148b4282e6SShameer Kolothum /* 11158b4282e6SShameer Kolothum * Current logic to reserve ITS regions relies on HW topologies 11168b4282e6SShameer Kolothum * where a given PCI or named component maps its IDs to only one 11178b4282e6SShameer Kolothum * ITS group; if a PCI or named component can map its IDs to 11188b4282e6SShameer Kolothum * different ITS groups through IORT mappings this function has 11198b4282e6SShameer Kolothum * to be reworked to ensure we reserve regions for all ITS groups 11208b4282e6SShameer Kolothum * a given PCI or named component may map IDs to. 11218b4282e6SShameer Kolothum */ 11228b4282e6SShameer Kolothum 11238097e53eSJoerg Roedel for (i = 0; i < fwspec->num_ids; i++) { 11248b4282e6SShameer Kolothum its_node = iort_node_map_id(iommu_node, 11258097e53eSJoerg Roedel fwspec->ids[i], 11268b4282e6SShameer Kolothum NULL, IORT_MSI_TYPE); 11278b4282e6SShameer Kolothum if (its_node) 11288b4282e6SShameer Kolothum break; 11298b4282e6SShameer Kolothum } 11308b4282e6SShameer Kolothum 11318b4282e6SShameer Kolothum if (!its_node) 11328778b1d4SShameer Kolothum return; 11338b4282e6SShameer Kolothum 11348b4282e6SShameer Kolothum /* Move to ITS specific data */ 11358b4282e6SShameer Kolothum its = (struct acpi_iort_its_group *)its_node->node_data; 11368b4282e6SShameer Kolothum 11378b4282e6SShameer Kolothum for (i = 0; i < its->its_count; i++) { 11388b4282e6SShameer Kolothum phys_addr_t base; 11398b4282e6SShameer Kolothum 11408b4282e6SShameer Kolothum if (!iort_find_its_base(its->identifiers[i], &base)) { 11418b4282e6SShameer Kolothum int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 11428b4282e6SShameer Kolothum struct iommu_resv_region *region; 11438b4282e6SShameer Kolothum 11448b4282e6SShameer Kolothum region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, 1145*0251d010SLu Baolu prot, IOMMU_RESV_MSI, 1146*0251d010SLu Baolu GFP_KERNEL); 11478778b1d4SShameer Kolothum if (region) 11488b4282e6SShameer Kolothum list_add_tail(®ion->list, head); 11498b4282e6SShameer Kolothum } 11508b4282e6SShameer Kolothum } 11518b4282e6SShameer Kolothum } 11528b4282e6SShameer Kolothum 115355be25b8SShameer Kolothum /** 115455be25b8SShameer Kolothum * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions. 115555be25b8SShameer Kolothum * @dev: Device from iommu_get_resv_regions() 115655be25b8SShameer Kolothum * @head: Reserved region list from iommu_get_resv_regions() 115755be25b8SShameer Kolothum */ 115855be25b8SShameer Kolothum void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 115955be25b8SShameer Kolothum { 1160491cf4a6SShameer Kolothum struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1161491cf4a6SShameer Kolothum 116255be25b8SShameer Kolothum iort_iommu_msi_get_resv_regions(dev, head); 1163491cf4a6SShameer Kolothum iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head); 116455be25b8SShameer Kolothum } 116555be25b8SShameer Kolothum 1166de026976SRen Zhijie /** 1167de026976SRen Zhijie * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with 1168de026976SRen Zhijie * associated StreamIDs information. 1169de026976SRen Zhijie * @iommu_fwnode: fwnode associated with IOMMU 1170de026976SRen Zhijie * @head: Resereved region list 1171de026976SRen Zhijie */ 1172de026976SRen Zhijie void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode, 1173de026976SRen Zhijie struct list_head *head) 1174de026976SRen Zhijie { 1175de026976SRen Zhijie iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head); 1176de026976SRen Zhijie } 1177de026976SRen Zhijie EXPORT_SYMBOL_GPL(iort_get_rmr_sids); 1178de026976SRen Zhijie 1179de026976SRen Zhijie /** 1180de026976SRen Zhijie * iort_put_rmr_sids - Free memory allocated for RMR reserved regions. 1181de026976SRen Zhijie * @iommu_fwnode: fwnode associated with IOMMU 1182de026976SRen Zhijie * @head: Resereved region list 1183de026976SRen Zhijie */ 1184de026976SRen Zhijie void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode, 1185de026976SRen Zhijie struct list_head *head) 1186de026976SRen Zhijie { 1187de026976SRen Zhijie struct iommu_resv_region *entry, *next; 1188de026976SRen Zhijie 1189de026976SRen Zhijie list_for_each_entry_safe(entry, next, head, list) 1190de026976SRen Zhijie entry->free(NULL, entry); 1191de026976SRen Zhijie } 1192de026976SRen Zhijie EXPORT_SYMBOL_GPL(iort_put_rmr_sids); 1193de026976SRen Zhijie 119482126886SLorenzo Pieralisi static inline bool iort_iommu_driver_enabled(u8 type) 119582126886SLorenzo Pieralisi { 119682126886SLorenzo Pieralisi switch (type) { 119782126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU_V3: 1198d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU_V3); 119982126886SLorenzo Pieralisi case ACPI_IORT_NODE_SMMU: 1200d3daf666SArd Biesheuvel return IS_ENABLED(CONFIG_ARM_SMMU); 120182126886SLorenzo Pieralisi default: 120282126886SLorenzo Pieralisi pr_warn("IORT node type %u does not describe an SMMU\n", type); 120382126886SLorenzo Pieralisi return false; 120482126886SLorenzo Pieralisi } 120582126886SLorenzo Pieralisi } 120682126886SLorenzo Pieralisi 120782126886SLorenzo Pieralisi static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) 120882126886SLorenzo Pieralisi { 120982126886SLorenzo Pieralisi struct acpi_iort_root_complex *pci_rc; 121082126886SLorenzo Pieralisi 121182126886SLorenzo Pieralisi pci_rc = (struct acpi_iort_root_complex *)node->node_data; 121282126886SLorenzo Pieralisi return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; 121382126886SLorenzo Pieralisi } 1214d49f2dedSLorenzo Pieralisi 1215bc8648d4SRobin Murphy static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, 1216643b8e4dSLorenzo Pieralisi u32 streamid) 1217643b8e4dSLorenzo Pieralisi { 1218bc8648d4SRobin Murphy const struct iommu_ops *ops; 1219643b8e4dSLorenzo Pieralisi struct fwnode_handle *iort_fwnode; 1220643b8e4dSLorenzo Pieralisi 1221bc8648d4SRobin Murphy if (!node) 1222bc8648d4SRobin Murphy return -ENODEV; 1223bc8648d4SRobin Murphy 1224643b8e4dSLorenzo Pieralisi iort_fwnode = iort_get_fwnode(node); 1225643b8e4dSLorenzo Pieralisi if (!iort_fwnode) 1226bc8648d4SRobin Murphy return -ENODEV; 1227643b8e4dSLorenzo Pieralisi 12285a1bb638SSricharan R /* 12295a1bb638SSricharan R * If the ops look-up fails, this means that either 12305a1bb638SSricharan R * the SMMU drivers have not been probed yet or that 12315a1bb638SSricharan R * the SMMU drivers are not built in the kernel; 12325a1bb638SSricharan R * Depending on whether the SMMU drivers are built-in 12335a1bb638SSricharan R * in the kernel or not, defer the IOMMU configuration 12345a1bb638SSricharan R * or just abort it. 12355a1bb638SSricharan R */ 1236bc8648d4SRobin Murphy ops = iommu_ops_from_fwnode(iort_fwnode); 1237643b8e4dSLorenzo Pieralisi if (!ops) 12385a1bb638SSricharan R return iort_iommu_driver_enabled(node->type) ? 1239bc8648d4SRobin Murphy -EPROBE_DEFER : -ENODEV; 1240643b8e4dSLorenzo Pieralisi 124111a8c5e3SJean-Philippe Brucker return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops); 1242643b8e4dSLorenzo Pieralisi } 1243643b8e4dSLorenzo Pieralisi 1244bc8648d4SRobin Murphy struct iort_pci_alias_info { 1245bc8648d4SRobin Murphy struct device *dev; 1246bc8648d4SRobin Murphy struct acpi_iort_node *node; 1247bc8648d4SRobin Murphy }; 1248bc8648d4SRobin Murphy 1249bc8648d4SRobin Murphy static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 1250bc8648d4SRobin Murphy { 1251bc8648d4SRobin Murphy struct iort_pci_alias_info *info = data; 1252bc8648d4SRobin Murphy struct acpi_iort_node *parent; 1253bc8648d4SRobin Murphy u32 streamid; 1254bc8648d4SRobin Murphy 1255bc8648d4SRobin Murphy parent = iort_node_map_id(info->node, alias, &streamid, 1256bc8648d4SRobin Murphy IORT_IOMMU_TYPE); 1257bc8648d4SRobin Murphy return iort_iommu_xlate(info->dev, parent, streamid); 1258643b8e4dSLorenzo Pieralisi } 1259643b8e4dSLorenzo Pieralisi 1260da22565dSJean-Philippe Brucker static void iort_named_component_init(struct device *dev, 1261da22565dSJean-Philippe Brucker struct acpi_iort_node *node) 1262da22565dSJean-Philippe Brucker { 12636522b1e0SJean-Philippe Brucker struct property_entry props[3] = {}; 1264da22565dSJean-Philippe Brucker struct acpi_iort_named_component *nc; 1265da22565dSJean-Philippe Brucker 1266da22565dSJean-Philippe Brucker nc = (struct acpi_iort_named_component *)node->node_data; 1267434b73e6SJean-Philippe Brucker props[0] = PROPERTY_ENTRY_U32("pasid-num-bits", 1268434b73e6SJean-Philippe Brucker FIELD_GET(ACPI_IORT_NC_PASID_BITS, 1269434b73e6SJean-Philippe Brucker nc->node_flags)); 12706522b1e0SJean-Philippe Brucker if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED) 12716522b1e0SJean-Philippe Brucker props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall"); 1272434b73e6SJean-Philippe Brucker 12730df316b8SHeikki Krogerus if (device_create_managed_software_node(dev, props, NULL)) 1274434b73e6SJean-Philippe Brucker dev_warn(dev, "Could not add device properties\n"); 1275da22565dSJean-Philippe Brucker } 1276da22565dSJean-Philippe Brucker 1277b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node) 1278b8e069a2SLorenzo Pieralisi { 1279b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 1280b8e069a2SLorenzo Pieralisi int err = -ENODEV, i = 0; 1281b8e069a2SLorenzo Pieralisi u32 streamid = 0; 1282b8e069a2SLorenzo Pieralisi 1283b8e069a2SLorenzo Pieralisi do { 1284b8e069a2SLorenzo Pieralisi 1285b8e069a2SLorenzo Pieralisi parent = iort_node_map_platform_id(node, &streamid, 1286b8e069a2SLorenzo Pieralisi IORT_IOMMU_TYPE, 1287b8e069a2SLorenzo Pieralisi i++); 1288b8e069a2SLorenzo Pieralisi 1289b8e069a2SLorenzo Pieralisi if (parent) 1290b8e069a2SLorenzo Pieralisi err = iort_iommu_xlate(dev, parent, streamid); 1291b8e069a2SLorenzo Pieralisi } while (parent && !err); 1292b8e069a2SLorenzo Pieralisi 1293b8e069a2SLorenzo Pieralisi return err; 1294b8e069a2SLorenzo Pieralisi } 1295b8e069a2SLorenzo Pieralisi 1296b8e069a2SLorenzo Pieralisi static int iort_nc_iommu_map_id(struct device *dev, 1297b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node, 1298b8e069a2SLorenzo Pieralisi const u32 *in_id) 1299b8e069a2SLorenzo Pieralisi { 1300b8e069a2SLorenzo Pieralisi struct acpi_iort_node *parent; 1301b8e069a2SLorenzo Pieralisi u32 streamid; 1302b8e069a2SLorenzo Pieralisi 1303b8e069a2SLorenzo Pieralisi parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE); 1304b8e069a2SLorenzo Pieralisi if (parent) 1305b8e069a2SLorenzo Pieralisi return iort_iommu_xlate(dev, parent, streamid); 1306b8e069a2SLorenzo Pieralisi 1307b8e069a2SLorenzo Pieralisi return -ENODEV; 1308b8e069a2SLorenzo Pieralisi } 1309b8e069a2SLorenzo Pieralisi 1310b8e069a2SLorenzo Pieralisi 131182126886SLorenzo Pieralisi /** 1312b8e069a2SLorenzo Pieralisi * iort_iommu_configure_id - Set-up IOMMU configuration for a device. 131382126886SLorenzo Pieralisi * 131482126886SLorenzo Pieralisi * @dev: device to configure 1315b8e069a2SLorenzo Pieralisi * @id_in: optional input id const value pointer 131682126886SLorenzo Pieralisi * 131711a8c5e3SJean-Philippe Brucker * Returns: 0 on success, <0 on failure 131882126886SLorenzo Pieralisi */ 131911a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *id_in) 132082126886SLorenzo Pieralisi { 1321b8e069a2SLorenzo Pieralisi struct acpi_iort_node *node; 132282126886SLorenzo Pieralisi int err = -ENODEV; 132382126886SLorenzo Pieralisi 132482126886SLorenzo Pieralisi if (dev_is_pci(dev)) { 13256990ec79SJoerg Roedel struct iommu_fwspec *fwspec; 132682126886SLorenzo Pieralisi struct pci_bus *bus = to_pci_dev(dev)->bus; 132782126886SLorenzo Pieralisi struct iort_pci_alias_info info = { .dev = dev }; 132882126886SLorenzo Pieralisi 132982126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 133082126886SLorenzo Pieralisi iort_match_node_callback, &bus->dev); 133182126886SLorenzo Pieralisi if (!node) 133211a8c5e3SJean-Philippe Brucker return -ENODEV; 133382126886SLorenzo Pieralisi 133482126886SLorenzo Pieralisi info.node = node; 133582126886SLorenzo Pieralisi err = pci_for_each_dma_alias(to_pci_dev(dev), 133682126886SLorenzo Pieralisi iort_pci_iommu_init, &info); 133782126886SLorenzo Pieralisi 13386990ec79SJoerg Roedel fwspec = dev_iommu_fwspec_get(dev); 13396990ec79SJoerg Roedel if (fwspec && iort_pci_rc_supports_ats(node)) 13406990ec79SJoerg Roedel fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; 134182126886SLorenzo Pieralisi } else { 134282126886SLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 134382126886SLorenzo Pieralisi iort_match_node_callback, dev); 134482126886SLorenzo Pieralisi if (!node) 134511a8c5e3SJean-Philippe Brucker return -ENODEV; 134682126886SLorenzo Pieralisi 1347b8e069a2SLorenzo Pieralisi err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) : 1348b8e069a2SLorenzo Pieralisi iort_nc_iommu_map(dev, node); 1349da22565dSJean-Philippe Brucker 1350da22565dSJean-Philippe Brucker if (!err) 1351da22565dSJean-Philippe Brucker iort_named_component_init(dev, node); 135282126886SLorenzo Pieralisi } 135382126886SLorenzo Pieralisi 135411a8c5e3SJean-Philippe Brucker return err; 135582126886SLorenzo Pieralisi } 1356b8e069a2SLorenzo Pieralisi 135782126886SLorenzo Pieralisi #else 135855be25b8SShameer Kolothum void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 13598778b1d4SShameer Kolothum { } 136011a8c5e3SJean-Philippe Brucker int iort_iommu_configure_id(struct device *dev, const u32 *input_id) 136111a8c5e3SJean-Philippe Brucker { return -ENODEV; } 136282126886SLorenzo Pieralisi #endif 136382126886SLorenzo Pieralisi 136410d8ab2cSLorenzo Pieralisi static int nc_dma_get_range(struct device *dev, u64 *size) 136510d8ab2cSLorenzo Pieralisi { 136610d8ab2cSLorenzo Pieralisi struct acpi_iort_node *node; 136710d8ab2cSLorenzo Pieralisi struct acpi_iort_named_component *ncomp; 136810d8ab2cSLorenzo Pieralisi 136910d8ab2cSLorenzo Pieralisi node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 137010d8ab2cSLorenzo Pieralisi iort_match_node_callback, dev); 137110d8ab2cSLorenzo Pieralisi if (!node) 137210d8ab2cSLorenzo Pieralisi return -ENODEV; 137310d8ab2cSLorenzo Pieralisi 137410d8ab2cSLorenzo Pieralisi ncomp = (struct acpi_iort_named_component *)node->node_data; 137510d8ab2cSLorenzo Pieralisi 1376a1df829eSMoritz Fischer if (!ncomp->memory_address_limit) { 1377a1df829eSMoritz Fischer pr_warn(FW_BUG "Named component missing memory address limit\n"); 1378a1df829eSMoritz Fischer return -EINVAL; 1379a1df829eSMoritz Fischer } 1380a1df829eSMoritz Fischer 138110d8ab2cSLorenzo Pieralisi *size = ncomp->memory_address_limit >= 64 ? U64_MAX : 138210d8ab2cSLorenzo Pieralisi 1ULL<<ncomp->memory_address_limit; 138310d8ab2cSLorenzo Pieralisi 138410d8ab2cSLorenzo Pieralisi return 0; 138510d8ab2cSLorenzo Pieralisi } 138610d8ab2cSLorenzo Pieralisi 13875ac65e8cSRobin Murphy static int rc_dma_get_range(struct device *dev, u64 *size) 13885ac65e8cSRobin Murphy { 13895ac65e8cSRobin Murphy struct acpi_iort_node *node; 13905ac65e8cSRobin Murphy struct acpi_iort_root_complex *rc; 1391c7777236SJean-Philippe Brucker struct pci_bus *pbus = to_pci_dev(dev)->bus; 13925ac65e8cSRobin Murphy 13935ac65e8cSRobin Murphy node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1394c7777236SJean-Philippe Brucker iort_match_node_callback, &pbus->dev); 13955ac65e8cSRobin Murphy if (!node || node->revision < 1) 13965ac65e8cSRobin Murphy return -ENODEV; 13975ac65e8cSRobin Murphy 13985ac65e8cSRobin Murphy rc = (struct acpi_iort_root_complex *)node->node_data; 13995ac65e8cSRobin Murphy 1400a1df829eSMoritz Fischer if (!rc->memory_address_limit) { 1401a1df829eSMoritz Fischer pr_warn(FW_BUG "Root complex missing memory address limit\n"); 1402a1df829eSMoritz Fischer return -EINVAL; 1403a1df829eSMoritz Fischer } 1404a1df829eSMoritz Fischer 14055ac65e8cSRobin Murphy *size = rc->memory_address_limit >= 64 ? U64_MAX : 14065ac65e8cSRobin Murphy 1ULL<<rc->memory_address_limit; 14075ac65e8cSRobin Murphy 14085ac65e8cSRobin Murphy return 0; 14095ac65e8cSRobin Murphy } 14105ac65e8cSRobin Murphy 1411643b8e4dSLorenzo Pieralisi /** 1412db59e1b6SJean-Philippe Brucker * iort_dma_get_ranges() - Look up DMA addressing limit for the device 1413db59e1b6SJean-Philippe Brucker * @dev: device to lookup 1414db59e1b6SJean-Philippe Brucker * @size: DMA range size result pointer 141518b709beSLorenzo Pieralisi * 1416db59e1b6SJean-Philippe Brucker * Return: 0 on success, an error otherwise. 141718b709beSLorenzo Pieralisi */ 1418db59e1b6SJean-Philippe Brucker int iort_dma_get_ranges(struct device *dev, u64 *size) 141918b709beSLorenzo Pieralisi { 1420db59e1b6SJean-Philippe Brucker if (dev_is_pci(dev)) 1421db59e1b6SJean-Philippe Brucker return rc_dma_get_range(dev, size); 14226757cdaeSRobin Murphy else 1423db59e1b6SJean-Philippe Brucker return nc_dma_get_range(dev, size); 142418b709beSLorenzo Pieralisi } 142518b709beSLorenzo Pieralisi 1426e4dadfa8SLorenzo Pieralisi static void __init acpi_iort_register_irq(int hwirq, const char *name, 1427e4dadfa8SLorenzo Pieralisi int trigger, 1428e4dadfa8SLorenzo Pieralisi struct resource *res) 1429e4dadfa8SLorenzo Pieralisi { 1430e4dadfa8SLorenzo Pieralisi int irq = acpi_register_gsi(NULL, hwirq, trigger, 1431e4dadfa8SLorenzo Pieralisi ACPI_ACTIVE_HIGH); 1432e4dadfa8SLorenzo Pieralisi 1433e4dadfa8SLorenzo Pieralisi if (irq <= 0) { 1434e4dadfa8SLorenzo Pieralisi pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, 1435e4dadfa8SLorenzo Pieralisi name); 1436e4dadfa8SLorenzo Pieralisi return; 1437e4dadfa8SLorenzo Pieralisi } 1438e4dadfa8SLorenzo Pieralisi 1439e4dadfa8SLorenzo Pieralisi res->start = irq; 1440e4dadfa8SLorenzo Pieralisi res->end = irq; 1441e4dadfa8SLorenzo Pieralisi res->flags = IORESOURCE_IRQ; 1442e4dadfa8SLorenzo Pieralisi res->name = name; 1443e4dadfa8SLorenzo Pieralisi } 1444e4dadfa8SLorenzo Pieralisi 1445e4dadfa8SLorenzo Pieralisi static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) 1446e4dadfa8SLorenzo Pieralisi { 1447e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1448e4dadfa8SLorenzo Pieralisi /* Always present mem resource */ 1449e4dadfa8SLorenzo Pieralisi int num_res = 1; 1450e4dadfa8SLorenzo Pieralisi 1451e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1452e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1453e4dadfa8SLorenzo Pieralisi 1454e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1455e4dadfa8SLorenzo Pieralisi num_res++; 1456e4dadfa8SLorenzo Pieralisi 1457e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1458e4dadfa8SLorenzo Pieralisi num_res++; 1459e4dadfa8SLorenzo Pieralisi 1460e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1461e4dadfa8SLorenzo Pieralisi num_res++; 1462e4dadfa8SLorenzo Pieralisi 1463e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1464e4dadfa8SLorenzo Pieralisi num_res++; 1465e4dadfa8SLorenzo Pieralisi 1466e4dadfa8SLorenzo Pieralisi return num_res; 1467e4dadfa8SLorenzo Pieralisi } 1468e4dadfa8SLorenzo Pieralisi 1469f935448aSGeetha Sowjanya static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) 1470f935448aSGeetha Sowjanya { 1471f935448aSGeetha Sowjanya /* 1472f935448aSGeetha Sowjanya * Cavium ThunderX2 implementation doesn't not support unique 1473f935448aSGeetha Sowjanya * irq line. Use single irq line for all the SMMUv3 interrupts. 1474f935448aSGeetha Sowjanya */ 1475f935448aSGeetha Sowjanya if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1476f935448aSGeetha Sowjanya return false; 1477f935448aSGeetha Sowjanya 1478f935448aSGeetha Sowjanya /* 1479f935448aSGeetha Sowjanya * ThunderX2 doesn't support MSIs from the SMMU, so we're checking 1480f935448aSGeetha Sowjanya * SPI numbers here. 1481f935448aSGeetha Sowjanya */ 1482f935448aSGeetha Sowjanya return smmu->event_gsiv == smmu->pri_gsiv && 1483f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->gerr_gsiv && 1484f935448aSGeetha Sowjanya smmu->event_gsiv == smmu->sync_gsiv; 1485f935448aSGeetha Sowjanya } 1486f935448aSGeetha Sowjanya 1487403e8c7cSLinu Cherian static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) 1488403e8c7cSLinu Cherian { 1489403e8c7cSLinu Cherian /* 1490403e8c7cSLinu Cherian * Override the size, for Cavium ThunderX2 implementation 1491403e8c7cSLinu Cherian * which doesn't support the page 1 SMMU register space. 1492403e8c7cSLinu Cherian */ 1493403e8c7cSLinu Cherian if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1494403e8c7cSLinu Cherian return SZ_64K; 1495403e8c7cSLinu Cherian 1496403e8c7cSLinu Cherian return SZ_128K; 1497403e8c7cSLinu Cherian } 1498403e8c7cSLinu Cherian 1499e4dadfa8SLorenzo Pieralisi static void __init arm_smmu_v3_init_resources(struct resource *res, 1500e4dadfa8SLorenzo Pieralisi struct acpi_iort_node *node) 1501e4dadfa8SLorenzo Pieralisi { 1502e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 1503e4dadfa8SLorenzo Pieralisi int num_res = 0; 1504e4dadfa8SLorenzo Pieralisi 1505e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1506e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1507e4dadfa8SLorenzo Pieralisi 1508e4dadfa8SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1509403e8c7cSLinu Cherian res[num_res].end = smmu->base_address + 1510403e8c7cSLinu Cherian arm_smmu_v3_resource_size(smmu) - 1; 1511e4dadfa8SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1512e4dadfa8SLorenzo Pieralisi 1513e4dadfa8SLorenzo Pieralisi num_res++; 1514f935448aSGeetha Sowjanya if (arm_smmu_v3_is_combined_irq(smmu)) { 1515f935448aSGeetha Sowjanya if (smmu->event_gsiv) 1516f935448aSGeetha Sowjanya acpi_iort_register_irq(smmu->event_gsiv, "combined", 1517f935448aSGeetha Sowjanya ACPI_EDGE_SENSITIVE, 1518f935448aSGeetha Sowjanya &res[num_res++]); 1519f935448aSGeetha Sowjanya } else { 1520e4dadfa8SLorenzo Pieralisi 1521e4dadfa8SLorenzo Pieralisi if (smmu->event_gsiv) 1522e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->event_gsiv, "eventq", 1523e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1524e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1525e4dadfa8SLorenzo Pieralisi 1526e4dadfa8SLorenzo Pieralisi if (smmu->pri_gsiv) 1527e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->pri_gsiv, "priq", 1528e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1529e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1530e4dadfa8SLorenzo Pieralisi 1531e4dadfa8SLorenzo Pieralisi if (smmu->gerr_gsiv) 1532e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", 1533e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1534e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1535e4dadfa8SLorenzo Pieralisi 1536e4dadfa8SLorenzo Pieralisi if (smmu->sync_gsiv) 1537e4dadfa8SLorenzo Pieralisi acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", 1538e4dadfa8SLorenzo Pieralisi ACPI_EDGE_SENSITIVE, 1539e4dadfa8SLorenzo Pieralisi &res[num_res++]); 1540e4dadfa8SLorenzo Pieralisi } 1541f935448aSGeetha Sowjanya } 1542e4dadfa8SLorenzo Pieralisi 154324e51604SNeil Leeder static void __init arm_smmu_v3_dma_configure(struct device *dev, 154424e51604SNeil Leeder struct acpi_iort_node *node) 1545e4dadfa8SLorenzo Pieralisi { 1546e4dadfa8SLorenzo Pieralisi struct acpi_iort_smmu_v3 *smmu; 154724e51604SNeil Leeder enum dev_dma_attr attr; 1548e4dadfa8SLorenzo Pieralisi 1549e4dadfa8SLorenzo Pieralisi /* Retrieve SMMUv3 specific data */ 1550e4dadfa8SLorenzo Pieralisi smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1551e4dadfa8SLorenzo Pieralisi 155224e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? 155324e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 155424e51604SNeil Leeder 155524e51604SNeil Leeder /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ 155624e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 155724e51604SNeil Leeder 155824e51604SNeil Leeder /* Configure DMA for the page table walker */ 155924e51604SNeil Leeder acpi_dma_configure(dev, attr); 1560e4dadfa8SLorenzo Pieralisi } 1561e4dadfa8SLorenzo Pieralisi 156275808131SLorenzo Pieralisi #if defined(CONFIG_ACPI_NUMA) 15635fe0ce3bSGanapatrao Kulkarni /* 15645fe0ce3bSGanapatrao Kulkarni * set numa proximity domain for smmuv3 device 15655fe0ce3bSGanapatrao Kulkarni */ 156636a2ba07SKefeng Wang static int __init arm_smmu_v3_set_proximity(struct device *dev, 15675fe0ce3bSGanapatrao Kulkarni struct acpi_iort_node *node) 15685fe0ce3bSGanapatrao Kulkarni { 15695fe0ce3bSGanapatrao Kulkarni struct acpi_iort_smmu_v3 *smmu; 15705fe0ce3bSGanapatrao Kulkarni 15715fe0ce3bSGanapatrao Kulkarni smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 15725fe0ce3bSGanapatrao Kulkarni if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { 157301feba59SJonathan Cameron int dev_node = pxm_to_node(smmu->pxm); 157436a2ba07SKefeng Wang 15753e77eeb7SLorenzo Pieralisi if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) 157636a2ba07SKefeng Wang return -EINVAL; 157736a2ba07SKefeng Wang 15783e77eeb7SLorenzo Pieralisi set_dev_node(dev, dev_node); 15795fe0ce3bSGanapatrao Kulkarni pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", 15805fe0ce3bSGanapatrao Kulkarni smmu->base_address, 15815fe0ce3bSGanapatrao Kulkarni smmu->pxm); 15825fe0ce3bSGanapatrao Kulkarni } 158336a2ba07SKefeng Wang return 0; 15845fe0ce3bSGanapatrao Kulkarni } 15855fe0ce3bSGanapatrao Kulkarni #else 15865fe0ce3bSGanapatrao Kulkarni #define arm_smmu_v3_set_proximity NULL 15875fe0ce3bSGanapatrao Kulkarni #endif 15885fe0ce3bSGanapatrao Kulkarni 1589d6fcd3b1SLorenzo Pieralisi static int __init arm_smmu_count_resources(struct acpi_iort_node *node) 1590d6fcd3b1SLorenzo Pieralisi { 1591d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1592d6fcd3b1SLorenzo Pieralisi 1593d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1594d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1595d6fcd3b1SLorenzo Pieralisi 1596d6fcd3b1SLorenzo Pieralisi /* 1597d6fcd3b1SLorenzo Pieralisi * Only consider the global fault interrupt and ignore the 1598d6fcd3b1SLorenzo Pieralisi * configuration access interrupt. 1599d6fcd3b1SLorenzo Pieralisi * 1600d6fcd3b1SLorenzo Pieralisi * MMIO address and global fault interrupt resources are always 1601d6fcd3b1SLorenzo Pieralisi * present so add them to the context interrupt count as a static 1602d6fcd3b1SLorenzo Pieralisi * value. 1603d6fcd3b1SLorenzo Pieralisi */ 1604d6fcd3b1SLorenzo Pieralisi return smmu->context_interrupt_count + 2; 1605d6fcd3b1SLorenzo Pieralisi } 1606d6fcd3b1SLorenzo Pieralisi 1607d6fcd3b1SLorenzo Pieralisi static void __init arm_smmu_init_resources(struct resource *res, 1608d6fcd3b1SLorenzo Pieralisi struct acpi_iort_node *node) 1609d6fcd3b1SLorenzo Pieralisi { 1610d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 1611d6fcd3b1SLorenzo Pieralisi int i, hw_irq, trigger, num_res = 0; 1612d6fcd3b1SLorenzo Pieralisi u64 *ctx_irq, *glb_irq; 1613d6fcd3b1SLorenzo Pieralisi 1614d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1615d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1616d6fcd3b1SLorenzo Pieralisi 1617d6fcd3b1SLorenzo Pieralisi res[num_res].start = smmu->base_address; 1618d6fcd3b1SLorenzo Pieralisi res[num_res].end = smmu->base_address + smmu->span - 1; 1619d6fcd3b1SLorenzo Pieralisi res[num_res].flags = IORESOURCE_MEM; 1620d6fcd3b1SLorenzo Pieralisi num_res++; 1621d6fcd3b1SLorenzo Pieralisi 1622d6fcd3b1SLorenzo Pieralisi glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); 1623d6fcd3b1SLorenzo Pieralisi /* Global IRQs */ 1624d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(glb_irq[0]); 1625d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); 1626d6fcd3b1SLorenzo Pieralisi 1627d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, 1628d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1629d6fcd3b1SLorenzo Pieralisi 1630d6fcd3b1SLorenzo Pieralisi /* Context IRQs */ 1631d6fcd3b1SLorenzo Pieralisi ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); 1632d6fcd3b1SLorenzo Pieralisi for (i = 0; i < smmu->context_interrupt_count; i++) { 1633d6fcd3b1SLorenzo Pieralisi hw_irq = IORT_IRQ_MASK(ctx_irq[i]); 1634d6fcd3b1SLorenzo Pieralisi trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); 1635d6fcd3b1SLorenzo Pieralisi 1636d6fcd3b1SLorenzo Pieralisi acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, 1637d6fcd3b1SLorenzo Pieralisi &res[num_res++]); 1638d6fcd3b1SLorenzo Pieralisi } 1639d6fcd3b1SLorenzo Pieralisi } 1640d6fcd3b1SLorenzo Pieralisi 164124e51604SNeil Leeder static void __init arm_smmu_dma_configure(struct device *dev, 164224e51604SNeil Leeder struct acpi_iort_node *node) 1643d6fcd3b1SLorenzo Pieralisi { 1644d6fcd3b1SLorenzo Pieralisi struct acpi_iort_smmu *smmu; 164524e51604SNeil Leeder enum dev_dma_attr attr; 1646d6fcd3b1SLorenzo Pieralisi 1647d6fcd3b1SLorenzo Pieralisi /* Retrieve SMMU specific data */ 1648d6fcd3b1SLorenzo Pieralisi smmu = (struct acpi_iort_smmu *)node->node_data; 1649d6fcd3b1SLorenzo Pieralisi 165024e51604SNeil Leeder attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? 165124e51604SNeil Leeder DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 165224e51604SNeil Leeder 165324e51604SNeil Leeder /* We expect the dma masks to be equivalent for SMMU set-ups */ 165424e51604SNeil Leeder dev->dma_mask = &dev->coherent_dma_mask; 165524e51604SNeil Leeder 165624e51604SNeil Leeder /* Configure DMA for the page table walker */ 165724e51604SNeil Leeder acpi_dma_configure(dev, attr); 165824e51604SNeil Leeder } 165924e51604SNeil Leeder 166024e51604SNeil Leeder static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) 166124e51604SNeil Leeder { 166224e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 166324e51604SNeil Leeder 166424e51604SNeil Leeder /* Retrieve PMCG specific data */ 166524e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 166624e51604SNeil Leeder 166724e51604SNeil Leeder /* 166824e51604SNeil Leeder * There are always 2 memory resources. 166924e51604SNeil Leeder * If the overflow_gsiv is present then add that for a total of 3. 167024e51604SNeil Leeder */ 167124e51604SNeil Leeder return pmcg->overflow_gsiv ? 3 : 2; 167224e51604SNeil Leeder } 167324e51604SNeil Leeder 167424e51604SNeil Leeder static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, 167524e51604SNeil Leeder struct acpi_iort_node *node) 167624e51604SNeil Leeder { 167724e51604SNeil Leeder struct acpi_iort_pmcg *pmcg; 167824e51604SNeil Leeder 167924e51604SNeil Leeder /* Retrieve PMCG specific data */ 168024e51604SNeil Leeder pmcg = (struct acpi_iort_pmcg *)node->node_data; 168124e51604SNeil Leeder 168224e51604SNeil Leeder res[0].start = pmcg->page0_base_address; 168324e51604SNeil Leeder res[0].end = pmcg->page0_base_address + SZ_4K - 1; 168424e51604SNeil Leeder res[0].flags = IORESOURCE_MEM; 1685da5fb9e1SRobin Murphy /* 1686da5fb9e1SRobin Murphy * The initial version in DEN0049C lacked a way to describe register 1687da5fb9e1SRobin Murphy * page 1, which makes it broken for most PMCG implementations; in 1688da5fb9e1SRobin Murphy * that case, just let the driver fail gracefully if it expects to 1689da5fb9e1SRobin Murphy * find a second memory resource. 1690da5fb9e1SRobin Murphy */ 1691da5fb9e1SRobin Murphy if (node->revision > 0) { 169224e51604SNeil Leeder res[1].start = pmcg->page1_base_address; 169324e51604SNeil Leeder res[1].end = pmcg->page1_base_address + SZ_4K - 1; 169424e51604SNeil Leeder res[1].flags = IORESOURCE_MEM; 1695da5fb9e1SRobin Murphy } 169624e51604SNeil Leeder 169724e51604SNeil Leeder if (pmcg->overflow_gsiv) 169824e51604SNeil Leeder acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", 169924e51604SNeil Leeder ACPI_EDGE_SENSITIVE, &res[2]); 170024e51604SNeil Leeder } 170124e51604SNeil Leeder 170224062fe8SShameer Kolothum static struct acpi_platform_list pmcg_plat_info[] __initdata = { 170324062fe8SShameer Kolothum /* HiSilicon Hip08 Platform */ 170424062fe8SShameer Kolothum {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 170524062fe8SShameer Kolothum "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08}, 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