1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016, Semihalf 4 * Author: Tomasz Nowicki <tn@semihalf.com> 5 * 6 * This file implements early detection/parsing of I/O mapping 7 * reported to OS through firmware via I/O Remapping Table (IORT) 8 * IORT document number: ARM DEN 0049A 9 */ 10 11 #define pr_fmt(fmt) "ACPI: IORT: " fmt 12 13 #include <linux/acpi_iort.h> 14 #include <linux/bitfield.h> 15 #include <linux/iommu.h> 16 #include <linux/kernel.h> 17 #include <linux/list.h> 18 #include <linux/pci.h> 19 #include <linux/platform_device.h> 20 #include <linux/slab.h> 21 #include <linux/dma-map-ops.h> 22 #include "init.h" 23 24 #define IORT_TYPE_MASK(type) (1 << (type)) 25 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) 26 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ 27 (1 << ACPI_IORT_NODE_SMMU_V3)) 28 29 struct iort_its_msi_chip { 30 struct list_head list; 31 struct fwnode_handle *fw_node; 32 phys_addr_t base_addr; 33 u32 translation_id; 34 }; 35 36 struct iort_fwnode { 37 struct list_head list; 38 struct acpi_iort_node *iort_node; 39 struct fwnode_handle *fwnode; 40 }; 41 static LIST_HEAD(iort_fwnode_list); 42 static DEFINE_SPINLOCK(iort_fwnode_lock); 43 44 /** 45 * iort_set_fwnode() - Create iort_fwnode and use it to register 46 * iommu data in the iort_fwnode_list 47 * 48 * @iort_node: IORT table node associated with the IOMMU 49 * @fwnode: fwnode associated with the IORT node 50 * 51 * Returns: 0 on success 52 * <0 on failure 53 */ 54 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node, 55 struct fwnode_handle *fwnode) 56 { 57 struct iort_fwnode *np; 58 59 np = kzalloc_obj(struct iort_fwnode, GFP_ATOMIC); 60 61 if (WARN_ON(!np)) 62 return -ENOMEM; 63 64 INIT_LIST_HEAD(&np->list); 65 np->iort_node = iort_node; 66 np->fwnode = fwnode; 67 68 spin_lock(&iort_fwnode_lock); 69 list_add_tail(&np->list, &iort_fwnode_list); 70 spin_unlock(&iort_fwnode_lock); 71 72 return 0; 73 } 74 75 /** 76 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node 77 * 78 * @node: IORT table node to be looked-up 79 * 80 * Returns: fwnode_handle pointer on success, NULL on failure 81 */ 82 static inline struct fwnode_handle *iort_get_fwnode( 83 struct acpi_iort_node *node) 84 { 85 struct iort_fwnode *curr; 86 struct fwnode_handle *fwnode = NULL; 87 88 spin_lock(&iort_fwnode_lock); 89 list_for_each_entry(curr, &iort_fwnode_list, list) { 90 if (curr->iort_node == node) { 91 fwnode = curr->fwnode; 92 break; 93 } 94 } 95 spin_unlock(&iort_fwnode_lock); 96 97 return fwnode; 98 } 99 100 /** 101 * iort_delete_fwnode() - Delete fwnode associated with an IORT node 102 * 103 * @node: IORT table node associated with fwnode to delete 104 */ 105 static inline void iort_delete_fwnode(struct acpi_iort_node *node) 106 { 107 struct iort_fwnode *curr, *tmp; 108 109 spin_lock(&iort_fwnode_lock); 110 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) { 111 if (curr->iort_node == node) { 112 list_del(&curr->list); 113 kfree(curr); 114 break; 115 } 116 } 117 spin_unlock(&iort_fwnode_lock); 118 } 119 120 /** 121 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode 122 * 123 * @fwnode: fwnode associated with device to be looked-up 124 * 125 * Returns: iort_node pointer on success, NULL on failure 126 */ 127 static inline struct acpi_iort_node *iort_get_iort_node( 128 struct fwnode_handle *fwnode) 129 { 130 struct iort_fwnode *curr; 131 struct acpi_iort_node *iort_node = NULL; 132 133 spin_lock(&iort_fwnode_lock); 134 list_for_each_entry(curr, &iort_fwnode_list, list) { 135 if (curr->fwnode == fwnode) { 136 iort_node = curr->iort_node; 137 break; 138 } 139 } 140 spin_unlock(&iort_fwnode_lock); 141 142 return iort_node; 143 } 144 145 typedef acpi_status (*iort_find_node_callback) 146 (struct acpi_iort_node *node, void *context); 147 148 /* Root pointer to the mapped IORT table */ 149 static struct acpi_table_header *iort_table; 150 151 static LIST_HEAD(iort_msi_chip_list); 152 static DEFINE_SPINLOCK(iort_msi_chip_lock); 153 154 /** 155 * iort_register_domain_token() - register domain token along with related 156 * ITS ID and base address to the list from where we can get it back later on. 157 * @trans_id: ITS ID. 158 * @base: ITS base address. 159 * @fw_node: Domain token. 160 * 161 * Returns: 0 on success, -ENOMEM if no memory when allocating list element 162 */ 163 int iort_register_domain_token(int trans_id, phys_addr_t base, 164 struct fwnode_handle *fw_node) 165 { 166 struct iort_its_msi_chip *its_msi_chip; 167 168 its_msi_chip = kzalloc_obj(*its_msi_chip); 169 if (!its_msi_chip) 170 return -ENOMEM; 171 172 its_msi_chip->fw_node = fw_node; 173 its_msi_chip->translation_id = trans_id; 174 its_msi_chip->base_addr = base; 175 176 spin_lock(&iort_msi_chip_lock); 177 list_add(&its_msi_chip->list, &iort_msi_chip_list); 178 spin_unlock(&iort_msi_chip_lock); 179 180 return 0; 181 } 182 183 /** 184 * iort_deregister_domain_token() - Deregister domain token based on ITS ID 185 * @trans_id: ITS ID. 186 * 187 * Returns: none. 188 */ 189 void iort_deregister_domain_token(int trans_id) 190 { 191 struct iort_its_msi_chip *its_msi_chip, *t; 192 193 spin_lock(&iort_msi_chip_lock); 194 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { 195 if (its_msi_chip->translation_id == trans_id) { 196 list_del(&its_msi_chip->list); 197 kfree(its_msi_chip); 198 break; 199 } 200 } 201 spin_unlock(&iort_msi_chip_lock); 202 } 203 204 /** 205 * iort_find_domain_token() - Find domain token based on given ITS ID 206 * @trans_id: ITS ID. 207 * 208 * Returns: domain token when find on the list, NULL otherwise 209 */ 210 struct fwnode_handle *iort_find_domain_token(int trans_id) 211 { 212 struct fwnode_handle *fw_node = NULL; 213 struct iort_its_msi_chip *its_msi_chip; 214 215 spin_lock(&iort_msi_chip_lock); 216 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 217 if (its_msi_chip->translation_id == trans_id) { 218 fw_node = its_msi_chip->fw_node; 219 break; 220 } 221 } 222 spin_unlock(&iort_msi_chip_lock); 223 224 return fw_node; 225 } 226 227 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, 228 iort_find_node_callback callback, 229 void *context) 230 { 231 struct acpi_iort_node *iort_node, *iort_end; 232 struct acpi_table_iort *iort; 233 int i; 234 235 if (!iort_table) 236 return NULL; 237 238 /* Get the first IORT node */ 239 iort = (struct acpi_table_iort *)iort_table; 240 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 241 iort->node_offset); 242 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 243 iort_table->length); 244 245 for (i = 0; i < iort->node_count; i++) { 246 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 247 "IORT node pointer overflows, bad table!\n")) 248 return NULL; 249 250 if (iort_node->type == type && 251 ACPI_SUCCESS(callback(iort_node, context))) 252 return iort_node; 253 254 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 255 iort_node->length); 256 } 257 258 return NULL; 259 } 260 261 static acpi_status iort_match_node_callback(struct acpi_iort_node *node, 262 void *context) 263 { 264 struct device *dev = context; 265 acpi_status status = AE_NOT_FOUND; 266 267 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || 268 node->type == ACPI_IORT_NODE_IWB) { 269 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 270 struct acpi_iort_named_component *ncomp; 271 struct acpi_iort_iwb *iwb; 272 struct device *cdev = dev; 273 struct acpi_device *adev; 274 const char *device_name; 275 276 /* 277 * Walk the device tree to find a device with an 278 * ACPI companion; there is no point in scanning 279 * IORT for a device matching a named component or IWB if 280 * the device does not have an ACPI companion to 281 * start with. 282 */ 283 do { 284 adev = ACPI_COMPANION(cdev); 285 if (adev) 286 break; 287 288 cdev = cdev->parent; 289 } while (cdev); 290 291 if (!adev) 292 goto out; 293 294 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); 295 if (ACPI_FAILURE(status)) { 296 dev_warn(cdev, "Can't get device full path name\n"); 297 goto out; 298 } 299 300 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { 301 ncomp = (struct acpi_iort_named_component *)node->node_data; 302 device_name = ncomp->device_name; 303 } else { 304 iwb = (struct acpi_iort_iwb *)node->node_data; 305 device_name = iwb->device_name; 306 } 307 status = !strcmp(device_name, buf.pointer) ? AE_OK : AE_NOT_FOUND; 308 acpi_os_free(buf.pointer); 309 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 310 struct acpi_iort_root_complex *pci_rc; 311 struct pci_bus *bus; 312 313 bus = to_pci_bus(dev); 314 pci_rc = (struct acpi_iort_root_complex *)node->node_data; 315 316 /* 317 * It is assumed that PCI segment numbers maps one-to-one 318 * with root complexes. Each segment number can represent only 319 * one root complex. 320 */ 321 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? 322 AE_OK : AE_NOT_FOUND; 323 } 324 out: 325 return status; 326 } 327 328 static acpi_status iort_match_iwb_callback(struct acpi_iort_node *node, void *context) 329 { 330 struct acpi_iort_iwb *iwb; 331 u32 *id = context; 332 333 if (node->type != ACPI_IORT_NODE_IWB) 334 return AE_NOT_FOUND; 335 336 iwb = (struct acpi_iort_iwb *)node->node_data; 337 if (iwb->iwb_index != *id) 338 return AE_NOT_FOUND; 339 340 return AE_OK; 341 } 342 343 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, 344 u32 *rid_out, bool check_overlap) 345 { 346 /* Single mapping does not care for input id */ 347 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 348 if (type == ACPI_IORT_NODE_NAMED_COMPONENT || 349 type == ACPI_IORT_NODE_IWB || 350 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 351 *rid_out = map->output_base; 352 return 0; 353 } 354 355 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", 356 map, type); 357 return -ENXIO; 358 } 359 360 if (rid_in < map->input_base || 361 (rid_in > map->input_base + map->id_count)) 362 return -ENXIO; 363 364 if (check_overlap) { 365 /* 366 * We already found a mapping for this input ID at the end of 367 * another region. If it coincides with the start of this 368 * region, we assume the prior match was due to the off-by-1 369 * issue mentioned below, and allow it to be superseded. 370 * Otherwise, things are *really* broken, and we just disregard 371 * duplicate matches entirely to retain compatibility. 372 */ 373 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n", 374 map, rid_in); 375 if (rid_in != map->input_base) 376 return -ENXIO; 377 378 pr_err(FW_BUG "applying workaround.\n"); 379 } 380 381 *rid_out = map->output_base + (rid_in - map->input_base); 382 383 /* 384 * Due to confusion regarding the meaning of the id_count field (which 385 * carries the number of IDs *minus 1*), we may have to disregard this 386 * match if it is at the end of the range, and overlaps with the start 387 * of another one. 388 */ 389 if (map->id_count > 0 && rid_in == map->input_base + map->id_count) 390 return -EAGAIN; 391 return 0; 392 } 393 394 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, 395 u32 *id_out, int index) 396 { 397 struct acpi_iort_node *parent; 398 struct acpi_iort_id_mapping *map; 399 400 if (!node->mapping_offset || !node->mapping_count || 401 index >= node->mapping_count) 402 return NULL; 403 404 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 405 node->mapping_offset + index * sizeof(*map)); 406 407 /* Firmware bug! */ 408 if (!map->output_reference) { 409 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 410 node, node->type); 411 return NULL; 412 } 413 414 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 415 map->output_reference); 416 417 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 418 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || 419 node->type == ACPI_IORT_NODE_IWB || 420 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || 421 node->type == ACPI_IORT_NODE_SMMU_V3 || 422 node->type == ACPI_IORT_NODE_PMCG) { 423 *id_out = map->output_base; 424 return parent; 425 } 426 } 427 428 return NULL; 429 } 430 431 #ifndef ACPI_IORT_SMMU_V3_DEVICEID_VALID 432 #define ACPI_IORT_SMMU_V3_DEVICEID_VALID (1 << 4) 433 #endif 434 435 static int iort_get_id_mapping_index(struct acpi_iort_node *node) 436 { 437 struct acpi_iort_smmu_v3 *smmu; 438 struct acpi_iort_pmcg *pmcg; 439 440 switch (node->type) { 441 case ACPI_IORT_NODE_SMMU_V3: 442 /* 443 * SMMUv3 dev ID mapping index was introduced in revision 1 444 * table, not available in revision 0 445 */ 446 if (node->revision < 1) 447 return -EINVAL; 448 449 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 450 /* 451 * Until IORT E.e (node rev. 5), the ID mapping index was 452 * defined to be valid unless all interrupts are GSIV-based. 453 */ 454 if (node->revision < 5) { 455 if (smmu->event_gsiv && smmu->pri_gsiv && 456 smmu->gerr_gsiv && smmu->sync_gsiv) 457 return -EINVAL; 458 } else if (!(smmu->flags & ACPI_IORT_SMMU_V3_DEVICEID_VALID)) { 459 return -EINVAL; 460 } 461 462 if (smmu->id_mapping_index >= node->mapping_count) { 463 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", 464 node, node->type); 465 return -EINVAL; 466 } 467 468 return smmu->id_mapping_index; 469 case ACPI_IORT_NODE_PMCG: 470 pmcg = (struct acpi_iort_pmcg *)node->node_data; 471 if (pmcg->overflow_gsiv || node->mapping_count == 0) 472 return -EINVAL; 473 474 return 0; 475 default: 476 return -EINVAL; 477 } 478 } 479 480 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, 481 u32 id_in, u32 *id_out, 482 u8 type_mask) 483 { 484 u32 id = id_in; 485 486 /* Parse the ID mapping tree to find specified node type */ 487 while (node) { 488 struct acpi_iort_id_mapping *map; 489 int i, index, rc = 0; 490 u32 out_ref = 0, map_id = id; 491 492 if (IORT_TYPE_MASK(node->type) & type_mask) { 493 if (id_out) 494 *id_out = id; 495 return node; 496 } 497 498 if (!node->mapping_offset || !node->mapping_count) 499 goto fail_map; 500 501 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 502 node->mapping_offset); 503 504 /* Firmware bug! */ 505 if (!map->output_reference) { 506 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 507 node, node->type); 508 goto fail_map; 509 } 510 511 /* 512 * Get the special ID mapping index (if any) and skip its 513 * associated ID map to prevent erroneous multi-stage 514 * IORT ID translations. 515 */ 516 index = iort_get_id_mapping_index(node); 517 518 /* Do the ID translation */ 519 for (i = 0; i < node->mapping_count; i++, map++) { 520 /* if it is special mapping index, skip it */ 521 if (i == index) 522 continue; 523 524 rc = iort_id_map(map, node->type, map_id, &id, out_ref); 525 if (!rc) 526 break; 527 if (rc == -EAGAIN) 528 out_ref = map->output_reference; 529 } 530 531 if (i == node->mapping_count && !out_ref) 532 goto fail_map; 533 534 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 535 rc ? out_ref : map->output_reference); 536 } 537 538 fail_map: 539 /* Map input ID to output ID unchanged on mapping failure */ 540 if (id_out) 541 *id_out = id_in; 542 543 return NULL; 544 } 545 546 static struct acpi_iort_node *iort_node_map_platform_id( 547 struct acpi_iort_node *node, u32 *id_out, u8 type_mask, 548 int index) 549 { 550 struct acpi_iort_node *parent; 551 u32 id; 552 553 /* step 1: retrieve the initial dev id */ 554 parent = iort_node_get_id(node, &id, index); 555 if (!parent) 556 return NULL; 557 558 /* 559 * optional step 2: map the initial dev id if its parent is not 560 * the target type we want, map it again for the use cases such 561 * as NC (named component) -> SMMU -> ITS. If the type is matched, 562 * return the initial dev id and its parent pointer directly. 563 */ 564 if (!(IORT_TYPE_MASK(parent->type) & type_mask)) 565 parent = iort_node_map_id(parent, id, id_out, type_mask); 566 else 567 if (id_out) 568 *id_out = id; 569 570 return parent; 571 } 572 573 static struct acpi_iort_node *iort_find_dev_node(struct device *dev) 574 { 575 struct pci_bus *pbus; 576 577 if (!dev_is_pci(dev)) { 578 struct acpi_iort_node *node; 579 /* 580 * scan iort_fwnode_list to see if it's an iort platform 581 * device (such as SMMU, PMCG),its iort node already cached 582 * and associated with fwnode when iort platform devices 583 * were initialized. 584 */ 585 node = iort_get_iort_node(dev->fwnode); 586 if (node) 587 return node; 588 /* 589 * if not, then it should be a platform device defined in 590 * DSDT/SSDT (with Named Component node in IORT) or an 591 * IWB device in the DSDT/SSDT. 592 */ 593 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 594 iort_match_node_callback, dev); 595 if (node) 596 return node; 597 return iort_scan_node(ACPI_IORT_NODE_IWB, 598 iort_match_node_callback, dev); 599 } 600 601 pbus = to_pci_dev(dev)->bus; 602 603 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 604 iort_match_node_callback, &pbus->dev); 605 } 606 607 /** 608 * iort_msi_map_id() - Map a MSI input ID for a device 609 * @dev: The device for which the mapping is to be done. 610 * @input_id: The device input ID. 611 * 612 * Returns: mapped MSI ID on success, input ID otherwise 613 */ 614 u32 iort_msi_map_id(struct device *dev, u32 input_id) 615 { 616 struct acpi_iort_node *node; 617 u32 dev_id; 618 619 node = iort_find_dev_node(dev); 620 if (!node) 621 return input_id; 622 623 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); 624 return dev_id; 625 } 626 627 /** 628 * iort_msi_xlate() - Map a MSI input ID for a device 629 * @dev: The device for which the mapping is to be done. 630 * @input_id: The device input ID. 631 * @fwnode: Pointer to store the fwnode. 632 * 633 * Returns: mapped MSI ID on success, input ID otherwise 634 * On success, the fwnode pointer is initialized to the MSI 635 * controller fwnode handle. 636 */ 637 u32 iort_msi_xlate(struct device *dev, u32 input_id, struct fwnode_handle **fwnode) 638 { 639 struct acpi_iort_its_group *its; 640 struct acpi_iort_node *node; 641 u32 dev_id; 642 643 node = iort_find_dev_node(dev); 644 if (!node) 645 return input_id; 646 647 node = iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); 648 if (!node) 649 return input_id; 650 651 /* Move to ITS specific data */ 652 its = (struct acpi_iort_its_group *)node->node_data; 653 654 *fwnode = iort_find_domain_token(its->identifiers[0]); 655 656 return dev_id; 657 } 658 659 int iort_its_translate_pa(struct fwnode_handle *node, phys_addr_t *base) 660 { 661 struct iort_its_msi_chip *its_msi_chip; 662 int ret = -ENODEV; 663 664 spin_lock(&iort_msi_chip_lock); 665 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 666 if (its_msi_chip->fw_node == node) { 667 *base = its_msi_chip->base_addr; 668 ret = 0; 669 break; 670 } 671 } 672 spin_unlock(&iort_msi_chip_lock); 673 674 return ret; 675 } 676 677 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) 678 { 679 struct fwnode_handle *fwnode = iort_find_domain_token(its_id); 680 681 if (!fwnode) 682 return -ENODEV; 683 684 return iort_its_translate_pa(fwnode, base); 685 } 686 687 /** 688 * iort_pmsi_get_msi_info() - Get the device id and translate frame PA for a device 689 * @dev: The device for which the mapping is to be done. 690 * @dev_id: The device ID found. 691 * @pa: optional pointer to store translate frame address. 692 * 693 * Returns: 0 for successful devid and pa retrieval, -ENODEV on error 694 */ 695 int iort_pmsi_get_msi_info(struct device *dev, u32 *dev_id, phys_addr_t *pa) 696 { 697 struct acpi_iort_node *node, *parent = NULL; 698 struct acpi_iort_its_group *its; 699 int i, index; 700 701 node = iort_find_dev_node(dev); 702 if (!node) 703 return -ENODEV; 704 705 index = iort_get_id_mapping_index(node); 706 /* if there is a valid index, go get the dev_id directly */ 707 if (index >= 0) { 708 parent = iort_node_get_id(node, dev_id, index); 709 } else { 710 for (i = 0; i < node->mapping_count; i++) { 711 parent = iort_node_map_platform_id(node, dev_id, 712 IORT_MSI_TYPE, i); 713 if (parent) 714 break; 715 } 716 } 717 718 if (!parent) 719 return -ENODEV; 720 721 if (pa) { 722 int ret; 723 724 its = (struct acpi_iort_its_group *)node->node_data; 725 ret = iort_find_its_base(its->identifiers[0], pa); 726 if (ret) 727 return ret; 728 } 729 730 return 0; 731 } 732 733 /** 734 * iort_dev_find_its_id() - Find the ITS identifier for a device 735 * @dev: The device. 736 * @id: Device's ID 737 * @idx: Index of the ITS identifier list. 738 * @its_id: ITS identifier. 739 * 740 * Returns: 0 on success, appropriate error value otherwise 741 */ 742 static int iort_dev_find_its_id(struct device *dev, u32 id, 743 unsigned int idx, int *its_id) 744 { 745 struct acpi_iort_its_group *its; 746 struct acpi_iort_node *node; 747 748 node = iort_find_dev_node(dev); 749 if (!node) 750 return -ENXIO; 751 752 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE); 753 if (!node) 754 return -ENXIO; 755 756 /* Move to ITS specific data */ 757 its = (struct acpi_iort_its_group *)node->node_data; 758 if (idx >= its->its_count) { 759 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n", 760 idx, its->its_count); 761 return -ENXIO; 762 } 763 764 *its_id = its->identifiers[idx]; 765 return 0; 766 } 767 768 /** 769 * iort_get_device_domain() - Find MSI domain related to a device 770 * @dev: The device. 771 * @id: Requester ID for the device. 772 * @bus_token: irq domain bus token. 773 * 774 * Returns: the MSI domain for this device, NULL otherwise 775 */ 776 struct irq_domain *iort_get_device_domain(struct device *dev, u32 id, 777 enum irq_domain_bus_token bus_token) 778 { 779 struct fwnode_handle *handle; 780 int its_id; 781 782 if (iort_dev_find_its_id(dev, id, 0, &its_id)) 783 return NULL; 784 785 handle = iort_find_domain_token(its_id); 786 if (!handle) 787 return NULL; 788 789 return irq_find_matching_fwnode(handle, bus_token); 790 } 791 792 acpi_handle iort_iwb_handle(u32 iwb_id) 793 { 794 struct acpi_iort_node *node; 795 struct acpi_iort_iwb *iwb; 796 acpi_status status; 797 acpi_handle handle; 798 799 /* find its associated IWB node */ 800 node = iort_scan_node(ACPI_IORT_NODE_IWB, iort_match_iwb_callback, &iwb_id); 801 if (!node) 802 return NULL; 803 804 iwb = (struct acpi_iort_iwb *)node->node_data; 805 status = acpi_get_handle(NULL, iwb->device_name, &handle); 806 if (ACPI_FAILURE(status)) 807 return NULL; 808 809 return handle; 810 } 811 812 struct fwnode_handle *iort_iwb_handle_fwnode(u32 iwb_id) 813 { 814 struct fwnode_handle *fwnode; 815 struct acpi_device *device; 816 acpi_handle handle; 817 818 handle = iort_iwb_handle(iwb_id); 819 if (!handle) 820 return NULL; 821 822 device = acpi_get_acpi_dev(handle); 823 if (!device) 824 return NULL; 825 826 fwnode = acpi_fwnode_handle(device); 827 acpi_put_acpi_dev(device); 828 829 return fwnode; 830 } 831 832 static void iort_set_device_domain(struct device *dev, 833 struct acpi_iort_node *node) 834 { 835 struct acpi_iort_its_group *its; 836 struct acpi_iort_node *msi_parent; 837 struct acpi_iort_id_mapping *map; 838 struct fwnode_handle *iort_fwnode; 839 struct irq_domain *domain; 840 int index; 841 842 index = iort_get_id_mapping_index(node); 843 if (index < 0) 844 return; 845 846 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 847 node->mapping_offset + index * sizeof(*map)); 848 849 /* Firmware bug! */ 850 if (!map->output_reference || 851 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { 852 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", 853 node, node->type); 854 return; 855 } 856 857 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 858 map->output_reference); 859 860 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) 861 return; 862 863 /* Move to ITS specific data */ 864 its = (struct acpi_iort_its_group *)msi_parent->node_data; 865 866 iort_fwnode = iort_find_domain_token(its->identifiers[0]); 867 if (!iort_fwnode) 868 return; 869 870 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 871 if (domain) 872 dev_set_msi_domain(dev, domain); 873 } 874 875 /** 876 * iort_get_platform_device_domain() - Find MSI domain related to a 877 * platform device 878 * @dev: the dev pointer associated with the platform device 879 * 880 * Returns: the MSI domain for this device, NULL otherwise 881 */ 882 static struct irq_domain *iort_get_platform_device_domain(struct device *dev) 883 { 884 struct acpi_iort_node *node, *msi_parent = NULL; 885 struct fwnode_handle *iort_fwnode; 886 struct acpi_iort_its_group *its; 887 int i; 888 889 /* find its associated iort node */ 890 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 891 iort_match_node_callback, dev); 892 if (!node) { 893 /* find its associated iort node */ 894 node = iort_scan_node(ACPI_IORT_NODE_IWB, 895 iort_match_node_callback, dev); 896 897 if (!node) 898 return NULL; 899 } 900 901 /* then find its msi parent node */ 902 for (i = 0; i < node->mapping_count; i++) { 903 msi_parent = iort_node_map_platform_id(node, NULL, 904 IORT_MSI_TYPE, i); 905 if (msi_parent) 906 break; 907 } 908 909 if (!msi_parent) 910 return NULL; 911 912 /* Move to ITS specific data */ 913 its = (struct acpi_iort_its_group *)msi_parent->node_data; 914 915 iort_fwnode = iort_find_domain_token(its->identifiers[0]); 916 if (!iort_fwnode) 917 return NULL; 918 919 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 920 } 921 922 void acpi_configure_pmsi_domain(struct device *dev) 923 { 924 struct irq_domain *msi_domain; 925 926 msi_domain = iort_get_platform_device_domain(dev); 927 if (msi_domain) 928 dev_set_msi_domain(dev, msi_domain); 929 } 930 931 #ifdef CONFIG_IOMMU_API 932 static void iort_rmr_free(struct device *dev, 933 struct iommu_resv_region *region) 934 { 935 struct iommu_iort_rmr_data *rmr_data; 936 937 rmr_data = container_of(region, struct iommu_iort_rmr_data, rr); 938 kfree(rmr_data->sids); 939 kfree(rmr_data); 940 } 941 942 static struct iommu_iort_rmr_data *iort_rmr_alloc( 943 struct acpi_iort_rmr_desc *rmr_desc, 944 int prot, enum iommu_resv_type type, 945 u32 *sids, u32 num_sids) 946 { 947 struct iommu_iort_rmr_data *rmr_data; 948 struct iommu_resv_region *region; 949 u32 *sids_copy; 950 u64 addr = rmr_desc->base_address, size = rmr_desc->length; 951 952 rmr_data = kmalloc_obj(*rmr_data); 953 if (!rmr_data) 954 return NULL; 955 956 /* Create a copy of SIDs array to associate with this rmr_data */ 957 sids_copy = kmemdup_array(sids, num_sids, sizeof(*sids), GFP_KERNEL); 958 if (!sids_copy) { 959 kfree(rmr_data); 960 return NULL; 961 } 962 rmr_data->sids = sids_copy; 963 rmr_data->num_sids = num_sids; 964 965 if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) { 966 /* PAGE align base addr and size */ 967 addr &= PAGE_MASK; 968 size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address)); 969 970 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n", 971 rmr_desc->base_address, 972 rmr_desc->base_address + rmr_desc->length - 1, 973 addr, addr + size - 1); 974 } 975 976 region = &rmr_data->rr; 977 INIT_LIST_HEAD(®ion->list); 978 region->start = addr; 979 region->length = size; 980 region->prot = prot; 981 region->type = type; 982 region->free = iort_rmr_free; 983 984 return rmr_data; 985 } 986 987 static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc, 988 u32 count) 989 { 990 int i, j; 991 992 for (i = 0; i < count; i++) { 993 u64 end, start = desc[i].base_address, length = desc[i].length; 994 995 if (!length) { 996 pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n", 997 start); 998 continue; 999 } 1000 1001 end = start + length - 1; 1002 1003 /* Check for address overlap */ 1004 for (j = i + 1; j < count; j++) { 1005 u64 e_start = desc[j].base_address; 1006 u64 e_end = e_start + desc[j].length - 1; 1007 1008 if (start <= e_end && end >= e_start) 1009 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n", 1010 start, end); 1011 } 1012 } 1013 } 1014 1015 /* 1016 * Please note, we will keep the already allocated RMR reserve 1017 * regions in case of a memory allocation failure. 1018 */ 1019 static void iort_get_rmrs(struct acpi_iort_node *node, 1020 struct acpi_iort_node *smmu, 1021 u32 *sids, u32 num_sids, 1022 struct list_head *head) 1023 { 1024 struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data; 1025 struct acpi_iort_rmr_desc *rmr_desc; 1026 int i; 1027 1028 rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node, 1029 rmr->rmr_offset); 1030 1031 iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count); 1032 1033 for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) { 1034 struct iommu_iort_rmr_data *rmr_data; 1035 enum iommu_resv_type type; 1036 int prot = IOMMU_READ | IOMMU_WRITE; 1037 1038 if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED) 1039 type = IOMMU_RESV_DIRECT_RELAXABLE; 1040 else 1041 type = IOMMU_RESV_DIRECT; 1042 1043 if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE) 1044 prot |= IOMMU_PRIV; 1045 1046 /* Attributes 0x00 - 0x03 represents device memory */ 1047 if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <= 1048 ACPI_IORT_RMR_ATTR_DEVICE_GRE) 1049 prot |= IOMMU_MMIO; 1050 else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) == 1051 ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB) 1052 prot |= IOMMU_CACHE; 1053 1054 rmr_data = iort_rmr_alloc(rmr_desc, prot, type, 1055 sids, num_sids); 1056 if (!rmr_data) 1057 return; 1058 1059 list_add_tail(&rmr_data->rr.list, head); 1060 } 1061 } 1062 1063 static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start, 1064 u32 new_count) 1065 { 1066 u32 *new_sids; 1067 u32 total_count = count + new_count; 1068 int i; 1069 1070 new_sids = krealloc_array(sids, count + new_count, 1071 sizeof(*new_sids), GFP_KERNEL); 1072 if (!new_sids) { 1073 kfree(sids); 1074 return NULL; 1075 } 1076 1077 for (i = count; i < total_count; i++) 1078 new_sids[i] = id_start++; 1079 1080 return new_sids; 1081 } 1082 1083 static bool iort_rmr_has_dev(struct device *dev, u32 id_start, 1084 u32 id_count) 1085 { 1086 int i; 1087 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1088 1089 /* 1090 * Make sure the kernel has preserved the boot firmware PCIe 1091 * configuration. This is required to ensure that the RMR PCIe 1092 * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5). 1093 */ 1094 if (dev_is_pci(dev)) { 1095 struct pci_dev *pdev = to_pci_dev(dev); 1096 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus); 1097 1098 if (!host->preserve_config) 1099 return false; 1100 } 1101 1102 for (i = 0; i < fwspec->num_ids; i++) { 1103 if (fwspec->ids[i] >= id_start && 1104 fwspec->ids[i] <= id_start + id_count) 1105 return true; 1106 } 1107 1108 return false; 1109 } 1110 1111 static void iort_node_get_rmr_info(struct acpi_iort_node *node, 1112 struct acpi_iort_node *iommu, 1113 struct device *dev, struct list_head *head) 1114 { 1115 struct acpi_iort_node *smmu = NULL; 1116 struct acpi_iort_rmr *rmr; 1117 struct acpi_iort_id_mapping *map; 1118 u32 *sids = NULL; 1119 u32 num_sids = 0; 1120 int i; 1121 1122 if (!node->mapping_offset || !node->mapping_count) { 1123 pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n", 1124 node); 1125 return; 1126 } 1127 1128 rmr = (struct acpi_iort_rmr *)node->node_data; 1129 if (!rmr->rmr_offset || !rmr->rmr_count) 1130 return; 1131 1132 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 1133 node->mapping_offset); 1134 1135 /* 1136 * Go through the ID mappings and see if we have a match for SMMU 1137 * and dev(if !NULL). If found, get the sids for the Node. 1138 * Please note, id_count is equal to the number of IDs in the 1139 * range minus one. 1140 */ 1141 for (i = 0; i < node->mapping_count; i++, map++) { 1142 struct acpi_iort_node *parent; 1143 1144 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 1145 map->output_reference); 1146 if (parent != iommu) 1147 continue; 1148 1149 /* If dev is valid, check RMR node corresponds to the dev SID */ 1150 if (dev && !iort_rmr_has_dev(dev, map->output_base, 1151 map->id_count)) 1152 continue; 1153 1154 /* Retrieve SIDs associated with the Node. */ 1155 sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base, 1156 map->id_count + 1); 1157 if (!sids) 1158 return; 1159 1160 num_sids += map->id_count + 1; 1161 } 1162 1163 if (!sids) 1164 return; 1165 1166 iort_get_rmrs(node, smmu, sids, num_sids, head); 1167 kfree(sids); 1168 } 1169 1170 static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev, 1171 struct list_head *head) 1172 { 1173 struct acpi_table_iort *iort; 1174 struct acpi_iort_node *iort_node, *iort_end; 1175 int i; 1176 1177 /* Only supports ARM DEN 0049E.d onwards */ 1178 if (iort_table->revision < 5) 1179 return; 1180 1181 iort = (struct acpi_table_iort *)iort_table; 1182 1183 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1184 iort->node_offset); 1185 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1186 iort_table->length); 1187 1188 for (i = 0; i < iort->node_count; i++) { 1189 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 1190 "IORT node pointer overflows, bad table!\n")) 1191 return; 1192 1193 if (iort_node->type == ACPI_IORT_NODE_RMR) 1194 iort_node_get_rmr_info(iort_node, iommu, dev, head); 1195 1196 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1197 iort_node->length); 1198 } 1199 } 1200 1201 /* 1202 * Populate the RMR list associated with a given IOMMU and dev(if provided). 1203 * If dev is NULL, the function populates all the RMRs associated with the 1204 * given IOMMU. 1205 */ 1206 static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode, 1207 struct device *dev, 1208 struct list_head *head) 1209 { 1210 struct acpi_iort_node *iommu; 1211 1212 iommu = iort_get_iort_node(iommu_fwnode); 1213 if (!iommu) 1214 return; 1215 1216 iort_find_rmrs(iommu, dev, head); 1217 } 1218 1219 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) 1220 { 1221 struct acpi_iort_node *iommu; 1222 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1223 1224 iommu = iort_get_iort_node(fwspec->iommu_fwnode); 1225 1226 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { 1227 struct acpi_iort_smmu_v3 *smmu; 1228 1229 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; 1230 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) 1231 return iommu; 1232 } 1233 1234 return NULL; 1235 } 1236 1237 /* 1238 * Retrieve platform specific HW MSI reserve regions. 1239 * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K) 1240 * associated with the device are the HW MSI reserved regions. 1241 */ 1242 static void iort_iommu_msi_get_resv_regions(struct device *dev, 1243 struct list_head *head) 1244 { 1245 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1246 struct acpi_iort_its_group *its; 1247 struct acpi_iort_node *iommu_node, *its_node = NULL; 1248 int i; 1249 1250 iommu_node = iort_get_msi_resv_iommu(dev); 1251 if (!iommu_node) 1252 return; 1253 1254 /* 1255 * Current logic to reserve ITS regions relies on HW topologies 1256 * where a given PCI or named component maps its IDs to only one 1257 * ITS group; if a PCI or named component can map its IDs to 1258 * different ITS groups through IORT mappings this function has 1259 * to be reworked to ensure we reserve regions for all ITS groups 1260 * a given PCI or named component may map IDs to. 1261 */ 1262 1263 for (i = 0; i < fwspec->num_ids; i++) { 1264 its_node = iort_node_map_id(iommu_node, 1265 fwspec->ids[i], 1266 NULL, IORT_MSI_TYPE); 1267 if (its_node) 1268 break; 1269 } 1270 1271 if (!its_node) 1272 return; 1273 1274 /* Move to ITS specific data */ 1275 its = (struct acpi_iort_its_group *)its_node->node_data; 1276 1277 for (i = 0; i < its->its_count; i++) { 1278 phys_addr_t base; 1279 1280 if (!iort_find_its_base(its->identifiers[i], &base)) { 1281 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 1282 struct iommu_resv_region *region; 1283 1284 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, 1285 prot, IOMMU_RESV_MSI, 1286 GFP_KERNEL); 1287 if (region) 1288 list_add_tail(®ion->list, head); 1289 } 1290 } 1291 } 1292 1293 /** 1294 * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions. 1295 * @dev: Device from iommu_get_resv_regions() 1296 * @head: Reserved region list from iommu_get_resv_regions() 1297 */ 1298 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 1299 { 1300 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 1301 1302 iort_iommu_msi_get_resv_regions(dev, head); 1303 iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head); 1304 } 1305 1306 /** 1307 * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with 1308 * associated StreamIDs information. 1309 * @iommu_fwnode: fwnode associated with IOMMU 1310 * @head: Resereved region list 1311 */ 1312 void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode, 1313 struct list_head *head) 1314 { 1315 iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head); 1316 } 1317 EXPORT_SYMBOL_GPL(iort_get_rmr_sids); 1318 1319 /** 1320 * iort_put_rmr_sids - Free memory allocated for RMR reserved regions. 1321 * @iommu_fwnode: fwnode associated with IOMMU 1322 * @head: Resereved region list 1323 */ 1324 void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode, 1325 struct list_head *head) 1326 { 1327 struct iommu_resv_region *entry, *next; 1328 1329 list_for_each_entry_safe(entry, next, head, list) 1330 entry->free(NULL, entry); 1331 } 1332 EXPORT_SYMBOL_GPL(iort_put_rmr_sids); 1333 1334 static inline bool iort_iommu_driver_enabled(u8 type) 1335 { 1336 switch (type) { 1337 case ACPI_IORT_NODE_SMMU_V3: 1338 return IS_ENABLED(CONFIG_ARM_SMMU_V3); 1339 case ACPI_IORT_NODE_SMMU: 1340 return IS_ENABLED(CONFIG_ARM_SMMU); 1341 default: 1342 pr_warn("IORT node type %u does not describe an SMMU\n", type); 1343 return false; 1344 } 1345 } 1346 1347 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) 1348 { 1349 struct acpi_iort_root_complex *pci_rc; 1350 1351 pci_rc = (struct acpi_iort_root_complex *)node->node_data; 1352 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; 1353 } 1354 1355 static bool iort_pci_rc_supports_canwbs(struct acpi_iort_node *node) 1356 { 1357 struct acpi_iort_memory_access *memory_access; 1358 struct acpi_iort_root_complex *pci_rc; 1359 1360 pci_rc = (struct acpi_iort_root_complex *)node->node_data; 1361 memory_access = 1362 (struct acpi_iort_memory_access *)&pci_rc->memory_properties; 1363 return memory_access->memory_flags & ACPI_IORT_MF_CANWBS; 1364 } 1365 1366 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, 1367 u32 streamid) 1368 { 1369 struct fwnode_handle *iort_fwnode; 1370 1371 /* If there's no SMMU driver at all, give up now */ 1372 if (!node || !iort_iommu_driver_enabled(node->type)) 1373 return -ENODEV; 1374 1375 iort_fwnode = iort_get_fwnode(node); 1376 if (!iort_fwnode) 1377 return -ENODEV; 1378 1379 /* 1380 * If the SMMU drivers are enabled but not loaded/probed 1381 * yet, this will defer. 1382 */ 1383 return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode); 1384 } 1385 1386 struct iort_pci_alias_info { 1387 struct device *dev; 1388 struct acpi_iort_node *node; 1389 }; 1390 1391 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 1392 { 1393 struct iort_pci_alias_info *info = data; 1394 struct acpi_iort_node *parent; 1395 u32 streamid; 1396 1397 parent = iort_node_map_id(info->node, alias, &streamid, 1398 IORT_IOMMU_TYPE); 1399 return iort_iommu_xlate(info->dev, parent, streamid); 1400 } 1401 1402 static void iort_named_component_init(struct device *dev, 1403 struct acpi_iort_node *node) 1404 { 1405 struct property_entry props[3] = {}; 1406 struct acpi_iort_named_component *nc; 1407 1408 nc = (struct acpi_iort_named_component *)node->node_data; 1409 props[0] = PROPERTY_ENTRY_U32("pasid-num-bits", 1410 FIELD_GET(ACPI_IORT_NC_PASID_BITS, 1411 nc->node_flags)); 1412 if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED) 1413 props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall"); 1414 1415 if (device_create_managed_software_node(dev, props, NULL)) 1416 dev_warn(dev, "Could not add device properties\n"); 1417 } 1418 1419 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node) 1420 { 1421 struct acpi_iort_node *parent; 1422 int err = -ENODEV, i = 0; 1423 u32 streamid = 0; 1424 1425 do { 1426 1427 parent = iort_node_map_platform_id(node, &streamid, 1428 IORT_IOMMU_TYPE, 1429 i++); 1430 1431 if (parent) 1432 err = iort_iommu_xlate(dev, parent, streamid); 1433 } while (parent && !err); 1434 1435 return err; 1436 } 1437 1438 static int iort_nc_iommu_map_id(struct device *dev, 1439 struct acpi_iort_node *node, 1440 const u32 *in_id) 1441 { 1442 struct acpi_iort_node *parent; 1443 u32 streamid; 1444 1445 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE); 1446 if (parent) 1447 return iort_iommu_xlate(dev, parent, streamid); 1448 1449 return -ENODEV; 1450 } 1451 1452 1453 /** 1454 * iort_iommu_configure_id - Set-up IOMMU configuration for a device. 1455 * 1456 * @dev: device to configure 1457 * @id_in: optional input id const value pointer 1458 * 1459 * Returns: 0 on success, <0 on failure 1460 */ 1461 int iort_iommu_configure_id(struct device *dev, const u32 *id_in) 1462 { 1463 struct acpi_iort_node *node; 1464 int err = -ENODEV; 1465 1466 if (dev_is_pci(dev)) { 1467 struct iommu_fwspec *fwspec; 1468 struct pci_bus *bus = to_pci_dev(dev)->bus; 1469 struct iort_pci_alias_info info = { .dev = dev }; 1470 1471 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1472 iort_match_node_callback, &bus->dev); 1473 if (!node) 1474 return -ENODEV; 1475 1476 info.node = node; 1477 err = pci_for_each_dma_alias(to_pci_dev(dev), 1478 iort_pci_iommu_init, &info); 1479 1480 fwspec = dev_iommu_fwspec_get(dev); 1481 if (fwspec && iort_pci_rc_supports_ats(node)) 1482 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; 1483 if (fwspec && iort_pci_rc_supports_canwbs(node)) 1484 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_CANWBS; 1485 } else { 1486 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 1487 iort_match_node_callback, dev); 1488 if (!node) 1489 return -ENODEV; 1490 1491 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) : 1492 iort_nc_iommu_map(dev, node); 1493 1494 if (!err) 1495 iort_named_component_init(dev, node); 1496 } 1497 1498 return err; 1499 } 1500 1501 #else 1502 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head) 1503 { } 1504 int iort_iommu_configure_id(struct device *dev, const u32 *input_id) 1505 { return -ENODEV; } 1506 #endif 1507 1508 static int nc_dma_get_range(struct device *dev, u64 *limit) 1509 { 1510 struct acpi_iort_node *node; 1511 struct acpi_iort_named_component *ncomp; 1512 1513 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 1514 iort_match_node_callback, dev); 1515 if (!node) 1516 return -ENODEV; 1517 1518 ncomp = (struct acpi_iort_named_component *)node->node_data; 1519 1520 if (!ncomp->memory_address_limit) { 1521 pr_warn(FW_BUG "Named component missing memory address limit\n"); 1522 return -EINVAL; 1523 } 1524 1525 *limit = ncomp->memory_address_limit >= 64 ? U64_MAX : 1526 (1ULL << ncomp->memory_address_limit) - 1; 1527 1528 return 0; 1529 } 1530 1531 static int rc_dma_get_range(struct device *dev, u64 *limit) 1532 { 1533 struct acpi_iort_node *node; 1534 struct acpi_iort_root_complex *rc; 1535 struct pci_bus *pbus = to_pci_dev(dev)->bus; 1536 1537 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1538 iort_match_node_callback, &pbus->dev); 1539 if (!node || node->revision < 1) 1540 return -ENODEV; 1541 1542 rc = (struct acpi_iort_root_complex *)node->node_data; 1543 1544 if (!rc->memory_address_limit) { 1545 pr_warn(FW_BUG "Root complex missing memory address limit\n"); 1546 return -EINVAL; 1547 } 1548 1549 *limit = rc->memory_address_limit >= 64 ? U64_MAX : 1550 (1ULL << rc->memory_address_limit) - 1; 1551 1552 return 0; 1553 } 1554 1555 /** 1556 * iort_dma_get_ranges() - Look up DMA addressing limit for the device 1557 * @dev: device to lookup 1558 * @limit: DMA limit result pointer 1559 * 1560 * Return: 0 on success, an error otherwise. 1561 */ 1562 int iort_dma_get_ranges(struct device *dev, u64 *limit) 1563 { 1564 if (dev_is_pci(dev)) 1565 return rc_dma_get_range(dev, limit); 1566 else 1567 return nc_dma_get_range(dev, limit); 1568 } 1569 1570 static void __init acpi_iort_register_irq(int hwirq, const char *name, 1571 int trigger, 1572 struct resource *res) 1573 { 1574 int irq = acpi_register_gsi(NULL, hwirq, trigger, 1575 ACPI_ACTIVE_HIGH); 1576 1577 if (irq <= 0) { 1578 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, 1579 name); 1580 return; 1581 } 1582 1583 res->start = irq; 1584 res->end = irq; 1585 res->flags = IORESOURCE_IRQ; 1586 res->name = name; 1587 } 1588 1589 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) 1590 { 1591 struct acpi_iort_smmu_v3 *smmu; 1592 /* Always present mem resource */ 1593 int num_res = 1; 1594 1595 /* Retrieve SMMUv3 specific data */ 1596 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1597 1598 if (smmu->event_gsiv) 1599 num_res++; 1600 1601 if (smmu->pri_gsiv) 1602 num_res++; 1603 1604 if (smmu->gerr_gsiv) 1605 num_res++; 1606 1607 if (smmu->sync_gsiv) 1608 num_res++; 1609 1610 return num_res; 1611 } 1612 1613 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) 1614 { 1615 /* 1616 * Cavium ThunderX2 implementation doesn't not support unique 1617 * irq line. Use single irq line for all the SMMUv3 interrupts. 1618 */ 1619 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1620 return false; 1621 1622 /* 1623 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking 1624 * SPI numbers here. 1625 */ 1626 return smmu->event_gsiv == smmu->pri_gsiv && 1627 smmu->event_gsiv == smmu->gerr_gsiv && 1628 smmu->event_gsiv == smmu->sync_gsiv; 1629 } 1630 1631 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) 1632 { 1633 /* 1634 * Override the size, for Cavium ThunderX2 implementation 1635 * which doesn't support the page 1 SMMU register space. 1636 */ 1637 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1638 return SZ_64K; 1639 1640 return SZ_128K; 1641 } 1642 1643 static void __init arm_smmu_v3_init_resources(struct resource *res, 1644 struct acpi_iort_node *node) 1645 { 1646 struct acpi_iort_smmu_v3 *smmu; 1647 int num_res = 0; 1648 1649 /* Retrieve SMMUv3 specific data */ 1650 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1651 1652 res[num_res].start = smmu->base_address; 1653 res[num_res].end = smmu->base_address + 1654 arm_smmu_v3_resource_size(smmu) - 1; 1655 res[num_res].flags = IORESOURCE_MEM; 1656 1657 num_res++; 1658 if (arm_smmu_v3_is_combined_irq(smmu)) { 1659 if (smmu->event_gsiv) 1660 acpi_iort_register_irq(smmu->event_gsiv, "combined", 1661 ACPI_EDGE_SENSITIVE, 1662 &res[num_res++]); 1663 } else { 1664 1665 if (smmu->event_gsiv) 1666 acpi_iort_register_irq(smmu->event_gsiv, "eventq", 1667 ACPI_EDGE_SENSITIVE, 1668 &res[num_res++]); 1669 1670 if (smmu->pri_gsiv) 1671 acpi_iort_register_irq(smmu->pri_gsiv, "priq", 1672 ACPI_EDGE_SENSITIVE, 1673 &res[num_res++]); 1674 1675 if (smmu->gerr_gsiv) 1676 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", 1677 ACPI_EDGE_SENSITIVE, 1678 &res[num_res++]); 1679 1680 if (smmu->sync_gsiv) 1681 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", 1682 ACPI_EDGE_SENSITIVE, 1683 &res[num_res++]); 1684 } 1685 } 1686 1687 static void __init arm_smmu_v3_dma_configure(struct device *dev, 1688 struct acpi_iort_node *node) 1689 { 1690 struct acpi_iort_smmu_v3 *smmu; 1691 enum dev_dma_attr attr; 1692 1693 /* Retrieve SMMUv3 specific data */ 1694 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1695 1696 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? 1697 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 1698 1699 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ 1700 dev->dma_mask = &dev->coherent_dma_mask; 1701 1702 /* Configure DMA for the page table walker */ 1703 acpi_dma_configure(dev, attr); 1704 } 1705 1706 #if defined(CONFIG_ACPI_NUMA) 1707 /* 1708 * set numa proximity domain for smmuv3 device 1709 */ 1710 static int __init arm_smmu_v3_set_proximity(struct device *dev, 1711 struct acpi_iort_node *node) 1712 { 1713 struct acpi_iort_smmu_v3 *smmu; 1714 1715 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1716 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { 1717 int dev_node = pxm_to_node(smmu->pxm); 1718 1719 if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) 1720 return -EINVAL; 1721 1722 set_dev_node(dev, dev_node); 1723 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", 1724 smmu->base_address, 1725 smmu->pxm); 1726 } 1727 return 0; 1728 } 1729 #else 1730 #define arm_smmu_v3_set_proximity NULL 1731 #endif 1732 1733 static int __init arm_smmu_count_resources(struct acpi_iort_node *node) 1734 { 1735 struct acpi_iort_smmu *smmu; 1736 1737 /* Retrieve SMMU specific data */ 1738 smmu = (struct acpi_iort_smmu *)node->node_data; 1739 1740 /* 1741 * Only consider the global fault interrupt and ignore the 1742 * configuration access interrupt. 1743 * 1744 * MMIO address and global fault interrupt resources are always 1745 * present so add them to the context interrupt count as a static 1746 * value. 1747 */ 1748 return smmu->context_interrupt_count + 2; 1749 } 1750 1751 static void __init arm_smmu_init_resources(struct resource *res, 1752 struct acpi_iort_node *node) 1753 { 1754 struct acpi_iort_smmu *smmu; 1755 int i, hw_irq, trigger, num_res = 0; 1756 u64 *ctx_irq, *glb_irq; 1757 1758 /* Retrieve SMMU specific data */ 1759 smmu = (struct acpi_iort_smmu *)node->node_data; 1760 1761 res[num_res].start = smmu->base_address; 1762 res[num_res].end = smmu->base_address + smmu->span - 1; 1763 res[num_res].flags = IORESOURCE_MEM; 1764 num_res++; 1765 1766 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); 1767 /* Global IRQs */ 1768 hw_irq = IORT_IRQ_MASK(glb_irq[0]); 1769 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); 1770 1771 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, 1772 &res[num_res++]); 1773 1774 /* Context IRQs */ 1775 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); 1776 for (i = 0; i < smmu->context_interrupt_count; i++) { 1777 hw_irq = IORT_IRQ_MASK(ctx_irq[i]); 1778 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); 1779 1780 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, 1781 &res[num_res++]); 1782 } 1783 } 1784 1785 static void __init arm_smmu_dma_configure(struct device *dev, 1786 struct acpi_iort_node *node) 1787 { 1788 struct acpi_iort_smmu *smmu; 1789 enum dev_dma_attr attr; 1790 1791 /* Retrieve SMMU specific data */ 1792 smmu = (struct acpi_iort_smmu *)node->node_data; 1793 1794 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? 1795 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 1796 1797 /* We expect the dma masks to be equivalent for SMMU set-ups */ 1798 dev->dma_mask = &dev->coherent_dma_mask; 1799 1800 /* Configure DMA for the page table walker */ 1801 acpi_dma_configure(dev, attr); 1802 } 1803 1804 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) 1805 { 1806 struct acpi_iort_pmcg *pmcg; 1807 1808 /* Retrieve PMCG specific data */ 1809 pmcg = (struct acpi_iort_pmcg *)node->node_data; 1810 1811 /* 1812 * There are always 2 memory resources. 1813 * If the overflow_gsiv is present then add that for a total of 3. 1814 */ 1815 return pmcg->overflow_gsiv ? 3 : 2; 1816 } 1817 1818 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, 1819 struct acpi_iort_node *node) 1820 { 1821 struct acpi_iort_pmcg *pmcg; 1822 1823 /* Retrieve PMCG specific data */ 1824 pmcg = (struct acpi_iort_pmcg *)node->node_data; 1825 1826 res[0].start = pmcg->page0_base_address; 1827 res[0].end = pmcg->page0_base_address + SZ_4K - 1; 1828 res[0].flags = IORESOURCE_MEM; 1829 /* 1830 * The initial version in DEN0049C lacked a way to describe register 1831 * page 1, which makes it broken for most PMCG implementations; in 1832 * that case, just let the driver fail gracefully if it expects to 1833 * find a second memory resource. 1834 */ 1835 if (node->revision > 0) { 1836 res[1].start = pmcg->page1_base_address; 1837 res[1].end = pmcg->page1_base_address + SZ_4K - 1; 1838 res[1].flags = IORESOURCE_MEM; 1839 } 1840 1841 if (pmcg->overflow_gsiv) 1842 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", 1843 ACPI_EDGE_SENSITIVE, &res[2]); 1844 } 1845 1846 static struct acpi_platform_list pmcg_plat_info[] __initdata = { 1847 /* HiSilicon Hip08 Platform */ 1848 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1849 "Erratum #162001800, Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP08}, 1850 /* HiSilicon Hip09 Platform */ 1851 {"HISI ", "HIP09 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1852 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 1853 {"HISI ", "HIP09A ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1854 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 1855 /* HiSilicon Hip10/11 Platform uses the same SMMU IP with Hip09 */ 1856 {"HISI ", "HIP10 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1857 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 1858 {"HISI ", "HIP10C ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1859 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 1860 {"HISI ", "HIP11 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1861 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09}, 1862 { } 1863 }; 1864 1865 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) 1866 { 1867 u32 model; 1868 int idx; 1869 1870 idx = acpi_match_platform_list(pmcg_plat_info); 1871 if (idx >= 0) 1872 model = pmcg_plat_info[idx].data; 1873 else 1874 model = IORT_SMMU_V3_PMCG_GENERIC; 1875 1876 return platform_device_add_data(pdev, &model, sizeof(model)); 1877 } 1878 1879 struct iort_dev_config { 1880 const char *name; 1881 int (*dev_init)(struct acpi_iort_node *node); 1882 void (*dev_dma_configure)(struct device *dev, 1883 struct acpi_iort_node *node); 1884 int (*dev_count_resources)(struct acpi_iort_node *node); 1885 void (*dev_init_resources)(struct resource *res, 1886 struct acpi_iort_node *node); 1887 int (*dev_set_proximity)(struct device *dev, 1888 struct acpi_iort_node *node); 1889 int (*dev_add_platdata)(struct platform_device *pdev); 1890 }; 1891 1892 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { 1893 .name = "arm-smmu-v3", 1894 .dev_dma_configure = arm_smmu_v3_dma_configure, 1895 .dev_count_resources = arm_smmu_v3_count_resources, 1896 .dev_init_resources = arm_smmu_v3_init_resources, 1897 .dev_set_proximity = arm_smmu_v3_set_proximity, 1898 }; 1899 1900 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { 1901 .name = "arm-smmu", 1902 .dev_dma_configure = arm_smmu_dma_configure, 1903 .dev_count_resources = arm_smmu_count_resources, 1904 .dev_init_resources = arm_smmu_init_resources, 1905 }; 1906 1907 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { 1908 .name = "arm-smmu-v3-pmcg", 1909 .dev_count_resources = arm_smmu_v3_pmcg_count_resources, 1910 .dev_init_resources = arm_smmu_v3_pmcg_init_resources, 1911 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, 1912 }; 1913 1914 static __init const struct iort_dev_config *iort_get_dev_cfg( 1915 struct acpi_iort_node *node) 1916 { 1917 switch (node->type) { 1918 case ACPI_IORT_NODE_SMMU_V3: 1919 return &iort_arm_smmu_v3_cfg; 1920 case ACPI_IORT_NODE_SMMU: 1921 return &iort_arm_smmu_cfg; 1922 case ACPI_IORT_NODE_PMCG: 1923 return &iort_arm_smmu_v3_pmcg_cfg; 1924 default: 1925 return NULL; 1926 } 1927 } 1928 1929 /** 1930 * iort_add_platform_device() - Allocate a platform device for IORT node 1931 * @node: Pointer to device ACPI IORT node 1932 * @ops: Pointer to IORT device config struct 1933 * 1934 * Returns: 0 on success, <0 failure 1935 */ 1936 static int __init iort_add_platform_device(struct acpi_iort_node *node, 1937 const struct iort_dev_config *ops) 1938 { 1939 struct fwnode_handle *fwnode; 1940 struct platform_device *pdev; 1941 struct resource *r; 1942 int ret, count; 1943 1944 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); 1945 if (!pdev) 1946 return -ENOMEM; 1947 1948 if (ops->dev_set_proximity) { 1949 ret = ops->dev_set_proximity(&pdev->dev, node); 1950 if (ret) 1951 goto dev_put; 1952 } 1953 1954 count = ops->dev_count_resources(node); 1955 1956 r = kzalloc_objs(*r, count); 1957 if (!r) { 1958 ret = -ENOMEM; 1959 goto dev_put; 1960 } 1961 1962 ops->dev_init_resources(r, node); 1963 1964 ret = platform_device_add_resources(pdev, r, count); 1965 /* 1966 * Resources are duplicated in platform_device_add_resources, 1967 * free their allocated memory 1968 */ 1969 kfree(r); 1970 1971 if (ret) 1972 goto dev_put; 1973 1974 /* 1975 * Platform devices based on PMCG nodes uses platform_data to 1976 * pass the hardware model info to the driver. For others, add 1977 * a copy of IORT node pointer to platform_data to be used to 1978 * retrieve IORT data information. 1979 */ 1980 if (ops->dev_add_platdata) 1981 ret = ops->dev_add_platdata(pdev); 1982 else 1983 ret = platform_device_add_data(pdev, &node, sizeof(node)); 1984 1985 if (ret) 1986 goto dev_put; 1987 1988 fwnode = iort_get_fwnode(node); 1989 1990 if (!fwnode) { 1991 ret = -ENODEV; 1992 goto dev_put; 1993 } 1994 1995 platform_device_set_fwnode(pdev, fwnode); 1996 1997 if (ops->dev_dma_configure) 1998 ops->dev_dma_configure(&pdev->dev, node); 1999 2000 iort_set_device_domain(&pdev->dev, node); 2001 2002 ret = platform_device_add(pdev); 2003 if (ret) 2004 goto dma_deconfigure; 2005 2006 return 0; 2007 2008 dma_deconfigure: 2009 arch_teardown_dma_ops(&pdev->dev); 2010 dev_put: 2011 platform_device_put(pdev); 2012 2013 return ret; 2014 } 2015 2016 #ifdef CONFIG_PCI 2017 static void __init iort_enable_acs(struct acpi_iort_node *iort_node) 2018 { 2019 static bool acs_enabled __initdata; 2020 2021 if (acs_enabled) 2022 return; 2023 2024 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 2025 struct acpi_iort_node *parent; 2026 struct acpi_iort_id_mapping *map; 2027 int i; 2028 2029 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, 2030 iort_node->mapping_offset); 2031 2032 for (i = 0; i < iort_node->mapping_count; i++, map++) { 2033 if (!map->output_reference) 2034 continue; 2035 2036 parent = ACPI_ADD_PTR(struct acpi_iort_node, 2037 iort_table, map->output_reference); 2038 /* 2039 * If we detect a RC->SMMU mapping, make sure 2040 * we enable ACS on the system. 2041 */ 2042 if ((parent->type == ACPI_IORT_NODE_SMMU) || 2043 (parent->type == ACPI_IORT_NODE_SMMU_V3)) { 2044 pci_request_acs(); 2045 acs_enabled = true; 2046 return; 2047 } 2048 } 2049 } 2050 } 2051 #else 2052 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } 2053 #endif 2054 2055 static void __init iort_init_platform_devices(void) 2056 { 2057 struct acpi_iort_node *iort_node, *iort_end; 2058 struct acpi_table_iort *iort; 2059 struct fwnode_handle *fwnode; 2060 int i, ret; 2061 const struct iort_dev_config *ops; 2062 2063 /* 2064 * iort_table and iort both point to the start of IORT table, but 2065 * have different struct types 2066 */ 2067 iort = (struct acpi_table_iort *)iort_table; 2068 2069 /* Get the first IORT node */ 2070 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 2071 iort->node_offset); 2072 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 2073 iort_table->length); 2074 2075 for (i = 0; i < iort->node_count; i++) { 2076 if (iort_node >= iort_end) { 2077 pr_err("iort node pointer overflows, bad table\n"); 2078 return; 2079 } 2080 2081 iort_enable_acs(iort_node); 2082 2083 ops = iort_get_dev_cfg(iort_node); 2084 if (ops) { 2085 fwnode = acpi_alloc_fwnode_static(); 2086 if (!fwnode) 2087 return; 2088 2089 iort_set_fwnode(iort_node, fwnode); 2090 2091 ret = iort_add_platform_device(iort_node, ops); 2092 if (ret) { 2093 iort_delete_fwnode(iort_node); 2094 acpi_free_fwnode_static(fwnode); 2095 return; 2096 } 2097 } 2098 2099 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 2100 iort_node->length); 2101 } 2102 } 2103 2104 u32 arch_acpi_add_auto_dep(acpi_handle handle) 2105 { 2106 return acpi_irq_add_auto_dep(handle); 2107 } 2108 2109 void __init acpi_iort_init(void) 2110 { 2111 acpi_status status; 2112 2113 /* iort_table will be used at runtime after the iort init, 2114 * so we don't need to call acpi_put_table() to release 2115 * the IORT table mapping. 2116 */ 2117 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); 2118 if (ACPI_FAILURE(status)) { 2119 if (status != AE_NOT_FOUND) { 2120 const char *msg = acpi_format_exception(status); 2121 2122 pr_err("Failed to get table, %s\n", msg); 2123 } 2124 2125 return; 2126 } 2127 2128 iort_init_platform_devices(); 2129 } 2130 2131 #ifdef CONFIG_ZONE_DMA 2132 /* 2133 * Extract the highest CPU physical address accessible to all DMA masters in 2134 * the system. PHYS_ADDR_MAX is returned when no constrained device is found. 2135 */ 2136 phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void) 2137 { 2138 phys_addr_t limit = PHYS_ADDR_MAX; 2139 struct acpi_iort_node *node, *end; 2140 struct acpi_table_iort *iort; 2141 acpi_status status; 2142 int i; 2143 2144 if (acpi_disabled) 2145 return limit; 2146 2147 status = acpi_get_table(ACPI_SIG_IORT, 0, 2148 (struct acpi_table_header **)&iort); 2149 if (ACPI_FAILURE(status)) 2150 return limit; 2151 2152 node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset); 2153 end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length); 2154 2155 for (i = 0; i < iort->node_count; i++) { 2156 if (node >= end) 2157 break; 2158 2159 switch (node->type) { 2160 struct acpi_iort_named_component *ncomp; 2161 struct acpi_iort_root_complex *rc; 2162 phys_addr_t local_limit; 2163 2164 case ACPI_IORT_NODE_NAMED_COMPONENT: 2165 ncomp = (struct acpi_iort_named_component *)node->node_data; 2166 local_limit = DMA_BIT_MASK(ncomp->memory_address_limit); 2167 limit = min_not_zero(limit, local_limit); 2168 break; 2169 2170 case ACPI_IORT_NODE_PCI_ROOT_COMPLEX: 2171 if (node->revision < 1) 2172 break; 2173 2174 rc = (struct acpi_iort_root_complex *)node->node_data; 2175 local_limit = DMA_BIT_MASK(rc->memory_address_limit); 2176 limit = min_not_zero(limit, local_limit); 2177 break; 2178 } 2179 node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length); 2180 } 2181 acpi_put_table(&iort->header); 2182 return limit; 2183 } 2184 #endif 2185