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 23 #define IORT_TYPE_MASK(type) (1 << (type)) 24 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) 25 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ 26 (1 << ACPI_IORT_NODE_SMMU_V3)) 27 28 struct iort_its_msi_chip { 29 struct list_head list; 30 struct fwnode_handle *fw_node; 31 phys_addr_t base_addr; 32 u32 translation_id; 33 }; 34 35 struct iort_fwnode { 36 struct list_head list; 37 struct acpi_iort_node *iort_node; 38 struct fwnode_handle *fwnode; 39 }; 40 static LIST_HEAD(iort_fwnode_list); 41 static DEFINE_SPINLOCK(iort_fwnode_lock); 42 43 /** 44 * iort_set_fwnode() - Create iort_fwnode and use it to register 45 * iommu data in the iort_fwnode_list 46 * 47 * @node: IORT table node associated with the IOMMU 48 * @fwnode: fwnode associated with the IORT node 49 * 50 * Returns: 0 on success 51 * <0 on failure 52 */ 53 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node, 54 struct fwnode_handle *fwnode) 55 { 56 struct iort_fwnode *np; 57 58 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC); 59 60 if (WARN_ON(!np)) 61 return -ENOMEM; 62 63 INIT_LIST_HEAD(&np->list); 64 np->iort_node = iort_node; 65 np->fwnode = fwnode; 66 67 spin_lock(&iort_fwnode_lock); 68 list_add_tail(&np->list, &iort_fwnode_list); 69 spin_unlock(&iort_fwnode_lock); 70 71 return 0; 72 } 73 74 /** 75 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node 76 * 77 * @node: IORT table node to be looked-up 78 * 79 * Returns: fwnode_handle pointer on success, NULL on failure 80 */ 81 static inline struct fwnode_handle *iort_get_fwnode( 82 struct acpi_iort_node *node) 83 { 84 struct iort_fwnode *curr; 85 struct fwnode_handle *fwnode = NULL; 86 87 spin_lock(&iort_fwnode_lock); 88 list_for_each_entry(curr, &iort_fwnode_list, list) { 89 if (curr->iort_node == node) { 90 fwnode = curr->fwnode; 91 break; 92 } 93 } 94 spin_unlock(&iort_fwnode_lock); 95 96 return fwnode; 97 } 98 99 /** 100 * iort_delete_fwnode() - Delete fwnode associated with an IORT node 101 * 102 * @node: IORT table node associated with fwnode to delete 103 */ 104 static inline void iort_delete_fwnode(struct acpi_iort_node *node) 105 { 106 struct iort_fwnode *curr, *tmp; 107 108 spin_lock(&iort_fwnode_lock); 109 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) { 110 if (curr->iort_node == node) { 111 list_del(&curr->list); 112 kfree(curr); 113 break; 114 } 115 } 116 spin_unlock(&iort_fwnode_lock); 117 } 118 119 /** 120 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode 121 * 122 * @fwnode: fwnode associated with device to be looked-up 123 * 124 * Returns: iort_node pointer on success, NULL on failure 125 */ 126 static inline struct acpi_iort_node *iort_get_iort_node( 127 struct fwnode_handle *fwnode) 128 { 129 struct iort_fwnode *curr; 130 struct acpi_iort_node *iort_node = NULL; 131 132 spin_lock(&iort_fwnode_lock); 133 list_for_each_entry(curr, &iort_fwnode_list, list) { 134 if (curr->fwnode == fwnode) { 135 iort_node = curr->iort_node; 136 break; 137 } 138 } 139 spin_unlock(&iort_fwnode_lock); 140 141 return iort_node; 142 } 143 144 typedef acpi_status (*iort_find_node_callback) 145 (struct acpi_iort_node *node, void *context); 146 147 /* Root pointer to the mapped IORT table */ 148 static struct acpi_table_header *iort_table; 149 150 static LIST_HEAD(iort_msi_chip_list); 151 static DEFINE_SPINLOCK(iort_msi_chip_lock); 152 153 /** 154 * iort_register_domain_token() - register domain token along with related 155 * ITS ID and base address to the list from where we can get it back later on. 156 * @trans_id: ITS ID. 157 * @base: ITS base address. 158 * @fw_node: Domain token. 159 * 160 * Returns: 0 on success, -ENOMEM if no memory when allocating list element 161 */ 162 int iort_register_domain_token(int trans_id, phys_addr_t base, 163 struct fwnode_handle *fw_node) 164 { 165 struct iort_its_msi_chip *its_msi_chip; 166 167 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL); 168 if (!its_msi_chip) 169 return -ENOMEM; 170 171 its_msi_chip->fw_node = fw_node; 172 its_msi_chip->translation_id = trans_id; 173 its_msi_chip->base_addr = base; 174 175 spin_lock(&iort_msi_chip_lock); 176 list_add(&its_msi_chip->list, &iort_msi_chip_list); 177 spin_unlock(&iort_msi_chip_lock); 178 179 return 0; 180 } 181 182 /** 183 * iort_deregister_domain_token() - Deregister domain token based on ITS ID 184 * @trans_id: ITS ID. 185 * 186 * Returns: none. 187 */ 188 void iort_deregister_domain_token(int trans_id) 189 { 190 struct iort_its_msi_chip *its_msi_chip, *t; 191 192 spin_lock(&iort_msi_chip_lock); 193 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { 194 if (its_msi_chip->translation_id == trans_id) { 195 list_del(&its_msi_chip->list); 196 kfree(its_msi_chip); 197 break; 198 } 199 } 200 spin_unlock(&iort_msi_chip_lock); 201 } 202 203 /** 204 * iort_find_domain_token() - Find domain token based on given ITS ID 205 * @trans_id: ITS ID. 206 * 207 * Returns: domain token when find on the list, NULL otherwise 208 */ 209 struct fwnode_handle *iort_find_domain_token(int trans_id) 210 { 211 struct fwnode_handle *fw_node = NULL; 212 struct iort_its_msi_chip *its_msi_chip; 213 214 spin_lock(&iort_msi_chip_lock); 215 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 216 if (its_msi_chip->translation_id == trans_id) { 217 fw_node = its_msi_chip->fw_node; 218 break; 219 } 220 } 221 spin_unlock(&iort_msi_chip_lock); 222 223 return fw_node; 224 } 225 226 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, 227 iort_find_node_callback callback, 228 void *context) 229 { 230 struct acpi_iort_node *iort_node, *iort_end; 231 struct acpi_table_iort *iort; 232 int i; 233 234 if (!iort_table) 235 return NULL; 236 237 /* Get the first IORT node */ 238 iort = (struct acpi_table_iort *)iort_table; 239 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 240 iort->node_offset); 241 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 242 iort_table->length); 243 244 for (i = 0; i < iort->node_count; i++) { 245 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, 246 "IORT node pointer overflows, bad table!\n")) 247 return NULL; 248 249 if (iort_node->type == type && 250 ACPI_SUCCESS(callback(iort_node, context))) 251 return iort_node; 252 253 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 254 iort_node->length); 255 } 256 257 return NULL; 258 } 259 260 static acpi_status iort_match_node_callback(struct acpi_iort_node *node, 261 void *context) 262 { 263 struct device *dev = context; 264 acpi_status status = AE_NOT_FOUND; 265 266 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { 267 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; 268 struct acpi_device *adev; 269 struct acpi_iort_named_component *ncomp; 270 struct device *nc_dev = dev; 271 272 /* 273 * Walk the device tree to find a device with an 274 * ACPI companion; there is no point in scanning 275 * IORT for a device matching a named component if 276 * the device does not have an ACPI companion to 277 * start with. 278 */ 279 do { 280 adev = ACPI_COMPANION(nc_dev); 281 if (adev) 282 break; 283 284 nc_dev = nc_dev->parent; 285 } while (nc_dev); 286 287 if (!adev) 288 goto out; 289 290 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); 291 if (ACPI_FAILURE(status)) { 292 dev_warn(nc_dev, "Can't get device full path name\n"); 293 goto out; 294 } 295 296 ncomp = (struct acpi_iort_named_component *)node->node_data; 297 status = !strcmp(ncomp->device_name, buf.pointer) ? 298 AE_OK : AE_NOT_FOUND; 299 acpi_os_free(buf.pointer); 300 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 301 struct acpi_iort_root_complex *pci_rc; 302 struct pci_bus *bus; 303 304 bus = to_pci_bus(dev); 305 pci_rc = (struct acpi_iort_root_complex *)node->node_data; 306 307 /* 308 * It is assumed that PCI segment numbers maps one-to-one 309 * with root complexes. Each segment number can represent only 310 * one root complex. 311 */ 312 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? 313 AE_OK : AE_NOT_FOUND; 314 } 315 out: 316 return status; 317 } 318 319 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, 320 u32 *rid_out, bool check_overlap) 321 { 322 /* Single mapping does not care for input id */ 323 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 324 if (type == ACPI_IORT_NODE_NAMED_COMPONENT || 325 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 326 *rid_out = map->output_base; 327 return 0; 328 } 329 330 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", 331 map, type); 332 return -ENXIO; 333 } 334 335 if (rid_in < map->input_base || 336 (rid_in > map->input_base + map->id_count)) 337 return -ENXIO; 338 339 if (check_overlap) { 340 /* 341 * We already found a mapping for this input ID at the end of 342 * another region. If it coincides with the start of this 343 * region, we assume the prior match was due to the off-by-1 344 * issue mentioned below, and allow it to be superseded. 345 * Otherwise, things are *really* broken, and we just disregard 346 * duplicate matches entirely to retain compatibility. 347 */ 348 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n", 349 map, rid_in); 350 if (rid_in != map->input_base) 351 return -ENXIO; 352 353 pr_err(FW_BUG "applying workaround.\n"); 354 } 355 356 *rid_out = map->output_base + (rid_in - map->input_base); 357 358 /* 359 * Due to confusion regarding the meaning of the id_count field (which 360 * carries the number of IDs *minus 1*), we may have to disregard this 361 * match if it is at the end of the range, and overlaps with the start 362 * of another one. 363 */ 364 if (map->id_count > 0 && rid_in == map->input_base + map->id_count) 365 return -EAGAIN; 366 return 0; 367 } 368 369 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, 370 u32 *id_out, int index) 371 { 372 struct acpi_iort_node *parent; 373 struct acpi_iort_id_mapping *map; 374 375 if (!node->mapping_offset || !node->mapping_count || 376 index >= node->mapping_count) 377 return NULL; 378 379 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 380 node->mapping_offset + index * sizeof(*map)); 381 382 /* Firmware bug! */ 383 if (!map->output_reference) { 384 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 385 node, node->type); 386 return NULL; 387 } 388 389 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 390 map->output_reference); 391 392 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { 393 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || 394 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || 395 node->type == ACPI_IORT_NODE_SMMU_V3 || 396 node->type == ACPI_IORT_NODE_PMCG) { 397 *id_out = map->output_base; 398 return parent; 399 } 400 } 401 402 return NULL; 403 } 404 405 static int iort_get_id_mapping_index(struct acpi_iort_node *node) 406 { 407 struct acpi_iort_smmu_v3 *smmu; 408 struct acpi_iort_pmcg *pmcg; 409 410 switch (node->type) { 411 case ACPI_IORT_NODE_SMMU_V3: 412 /* 413 * SMMUv3 dev ID mapping index was introduced in revision 1 414 * table, not available in revision 0 415 */ 416 if (node->revision < 1) 417 return -EINVAL; 418 419 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 420 /* 421 * ID mapping index is only ignored if all interrupts are 422 * GSIV based 423 */ 424 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv 425 && smmu->sync_gsiv) 426 return -EINVAL; 427 428 if (smmu->id_mapping_index >= node->mapping_count) { 429 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", 430 node, node->type); 431 return -EINVAL; 432 } 433 434 return smmu->id_mapping_index; 435 case ACPI_IORT_NODE_PMCG: 436 pmcg = (struct acpi_iort_pmcg *)node->node_data; 437 if (pmcg->overflow_gsiv || node->mapping_count == 0) 438 return -EINVAL; 439 440 return 0; 441 default: 442 return -EINVAL; 443 } 444 } 445 446 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, 447 u32 id_in, u32 *id_out, 448 u8 type_mask) 449 { 450 u32 id = id_in; 451 452 /* Parse the ID mapping tree to find specified node type */ 453 while (node) { 454 struct acpi_iort_id_mapping *map; 455 int i, index, rc = 0; 456 u32 out_ref = 0, map_id = id; 457 458 if (IORT_TYPE_MASK(node->type) & type_mask) { 459 if (id_out) 460 *id_out = id; 461 return node; 462 } 463 464 if (!node->mapping_offset || !node->mapping_count) 465 goto fail_map; 466 467 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 468 node->mapping_offset); 469 470 /* Firmware bug! */ 471 if (!map->output_reference) { 472 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", 473 node, node->type); 474 goto fail_map; 475 } 476 477 /* 478 * Get the special ID mapping index (if any) and skip its 479 * associated ID map to prevent erroneous multi-stage 480 * IORT ID translations. 481 */ 482 index = iort_get_id_mapping_index(node); 483 484 /* Do the ID translation */ 485 for (i = 0; i < node->mapping_count; i++, map++) { 486 /* if it is special mapping index, skip it */ 487 if (i == index) 488 continue; 489 490 rc = iort_id_map(map, node->type, map_id, &id, out_ref); 491 if (!rc) 492 break; 493 if (rc == -EAGAIN) 494 out_ref = map->output_reference; 495 } 496 497 if (i == node->mapping_count && !out_ref) 498 goto fail_map; 499 500 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 501 rc ? out_ref : map->output_reference); 502 } 503 504 fail_map: 505 /* Map input ID to output ID unchanged on mapping failure */ 506 if (id_out) 507 *id_out = id_in; 508 509 return NULL; 510 } 511 512 static struct acpi_iort_node *iort_node_map_platform_id( 513 struct acpi_iort_node *node, u32 *id_out, u8 type_mask, 514 int index) 515 { 516 struct acpi_iort_node *parent; 517 u32 id; 518 519 /* step 1: retrieve the initial dev id */ 520 parent = iort_node_get_id(node, &id, index); 521 if (!parent) 522 return NULL; 523 524 /* 525 * optional step 2: map the initial dev id if its parent is not 526 * the target type we want, map it again for the use cases such 527 * as NC (named component) -> SMMU -> ITS. If the type is matched, 528 * return the initial dev id and its parent pointer directly. 529 */ 530 if (!(IORT_TYPE_MASK(parent->type) & type_mask)) 531 parent = iort_node_map_id(parent, id, id_out, type_mask); 532 else 533 if (id_out) 534 *id_out = id; 535 536 return parent; 537 } 538 539 static struct acpi_iort_node *iort_find_dev_node(struct device *dev) 540 { 541 struct pci_bus *pbus; 542 543 if (!dev_is_pci(dev)) { 544 struct acpi_iort_node *node; 545 /* 546 * scan iort_fwnode_list to see if it's an iort platform 547 * device (such as SMMU, PMCG),its iort node already cached 548 * and associated with fwnode when iort platform devices 549 * were initialized. 550 */ 551 node = iort_get_iort_node(dev->fwnode); 552 if (node) 553 return node; 554 /* 555 * if not, then it should be a platform device defined in 556 * DSDT/SSDT (with Named Component node in IORT) 557 */ 558 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 559 iort_match_node_callback, dev); 560 } 561 562 pbus = to_pci_dev(dev)->bus; 563 564 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 565 iort_match_node_callback, &pbus->dev); 566 } 567 568 /** 569 * iort_msi_map_id() - Map a MSI input ID for a device 570 * @dev: The device for which the mapping is to be done. 571 * @input_id: The device input ID. 572 * 573 * Returns: mapped MSI ID on success, input ID otherwise 574 */ 575 u32 iort_msi_map_id(struct device *dev, u32 input_id) 576 { 577 struct acpi_iort_node *node; 578 u32 dev_id; 579 580 node = iort_find_dev_node(dev); 581 if (!node) 582 return input_id; 583 584 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); 585 return dev_id; 586 } 587 588 /** 589 * iort_pmsi_get_dev_id() - Get the device id for a device 590 * @dev: The device for which the mapping is to be done. 591 * @dev_id: The device ID found. 592 * 593 * Returns: 0 for successful find a dev id, -ENODEV on error 594 */ 595 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) 596 { 597 int i, index; 598 struct acpi_iort_node *node; 599 600 node = iort_find_dev_node(dev); 601 if (!node) 602 return -ENODEV; 603 604 index = iort_get_id_mapping_index(node); 605 /* if there is a valid index, go get the dev_id directly */ 606 if (index >= 0) { 607 if (iort_node_get_id(node, dev_id, index)) 608 return 0; 609 } else { 610 for (i = 0; i < node->mapping_count; i++) { 611 if (iort_node_map_platform_id(node, dev_id, 612 IORT_MSI_TYPE, i)) 613 return 0; 614 } 615 } 616 617 return -ENODEV; 618 } 619 620 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) 621 { 622 struct iort_its_msi_chip *its_msi_chip; 623 int ret = -ENODEV; 624 625 spin_lock(&iort_msi_chip_lock); 626 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { 627 if (its_msi_chip->translation_id == its_id) { 628 *base = its_msi_chip->base_addr; 629 ret = 0; 630 break; 631 } 632 } 633 spin_unlock(&iort_msi_chip_lock); 634 635 return ret; 636 } 637 638 /** 639 * iort_dev_find_its_id() - Find the ITS identifier for a device 640 * @dev: The device. 641 * @id: Device's ID 642 * @idx: Index of the ITS identifier list. 643 * @its_id: ITS identifier. 644 * 645 * Returns: 0 on success, appropriate error value otherwise 646 */ 647 static int iort_dev_find_its_id(struct device *dev, u32 id, 648 unsigned int idx, int *its_id) 649 { 650 struct acpi_iort_its_group *its; 651 struct acpi_iort_node *node; 652 653 node = iort_find_dev_node(dev); 654 if (!node) 655 return -ENXIO; 656 657 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE); 658 if (!node) 659 return -ENXIO; 660 661 /* Move to ITS specific data */ 662 its = (struct acpi_iort_its_group *)node->node_data; 663 if (idx >= its->its_count) { 664 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n", 665 idx, its->its_count); 666 return -ENXIO; 667 } 668 669 *its_id = its->identifiers[idx]; 670 return 0; 671 } 672 673 /** 674 * iort_get_device_domain() - Find MSI domain related to a device 675 * @dev: The device. 676 * @req_id: Requester ID for the device. 677 * 678 * Returns: the MSI domain for this device, NULL otherwise 679 */ 680 struct irq_domain *iort_get_device_domain(struct device *dev, u32 id, 681 enum irq_domain_bus_token bus_token) 682 { 683 struct fwnode_handle *handle; 684 int its_id; 685 686 if (iort_dev_find_its_id(dev, id, 0, &its_id)) 687 return NULL; 688 689 handle = iort_find_domain_token(its_id); 690 if (!handle) 691 return NULL; 692 693 return irq_find_matching_fwnode(handle, bus_token); 694 } 695 696 static void iort_set_device_domain(struct device *dev, 697 struct acpi_iort_node *node) 698 { 699 struct acpi_iort_its_group *its; 700 struct acpi_iort_node *msi_parent; 701 struct acpi_iort_id_mapping *map; 702 struct fwnode_handle *iort_fwnode; 703 struct irq_domain *domain; 704 int index; 705 706 index = iort_get_id_mapping_index(node); 707 if (index < 0) 708 return; 709 710 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, 711 node->mapping_offset + index * sizeof(*map)); 712 713 /* Firmware bug! */ 714 if (!map->output_reference || 715 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { 716 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", 717 node, node->type); 718 return; 719 } 720 721 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, 722 map->output_reference); 723 724 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) 725 return; 726 727 /* Move to ITS specific data */ 728 its = (struct acpi_iort_its_group *)msi_parent->node_data; 729 730 iort_fwnode = iort_find_domain_token(its->identifiers[0]); 731 if (!iort_fwnode) 732 return; 733 734 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 735 if (domain) 736 dev_set_msi_domain(dev, domain); 737 } 738 739 /** 740 * iort_get_platform_device_domain() - Find MSI domain related to a 741 * platform device 742 * @dev: the dev pointer associated with the platform device 743 * 744 * Returns: the MSI domain for this device, NULL otherwise 745 */ 746 static struct irq_domain *iort_get_platform_device_domain(struct device *dev) 747 { 748 struct acpi_iort_node *node, *msi_parent = NULL; 749 struct fwnode_handle *iort_fwnode; 750 struct acpi_iort_its_group *its; 751 int i; 752 753 /* find its associated iort node */ 754 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 755 iort_match_node_callback, dev); 756 if (!node) 757 return NULL; 758 759 /* then find its msi parent node */ 760 for (i = 0; i < node->mapping_count; i++) { 761 msi_parent = iort_node_map_platform_id(node, NULL, 762 IORT_MSI_TYPE, i); 763 if (msi_parent) 764 break; 765 } 766 767 if (!msi_parent) 768 return NULL; 769 770 /* Move to ITS specific data */ 771 its = (struct acpi_iort_its_group *)msi_parent->node_data; 772 773 iort_fwnode = iort_find_domain_token(its->identifiers[0]); 774 if (!iort_fwnode) 775 return NULL; 776 777 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); 778 } 779 780 void acpi_configure_pmsi_domain(struct device *dev) 781 { 782 struct irq_domain *msi_domain; 783 784 msi_domain = iort_get_platform_device_domain(dev); 785 if (msi_domain) 786 dev_set_msi_domain(dev, msi_domain); 787 } 788 789 #ifdef CONFIG_IOMMU_API 790 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) 791 { 792 struct acpi_iort_node *iommu; 793 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 794 795 iommu = iort_get_iort_node(fwspec->iommu_fwnode); 796 797 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { 798 struct acpi_iort_smmu_v3 *smmu; 799 800 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; 801 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) 802 return iommu; 803 } 804 805 return NULL; 806 } 807 808 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev) 809 { 810 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 811 812 return (fwspec && fwspec->ops) ? fwspec->ops : NULL; 813 } 814 815 static inline int iort_add_device_replay(struct device *dev) 816 { 817 int err = 0; 818 819 if (dev->bus && !device_iommu_mapped(dev)) 820 err = iommu_probe_device(dev); 821 822 return err; 823 } 824 825 /** 826 * iort_iommu_msi_get_resv_regions - Reserved region driver helper 827 * @dev: Device from iommu_get_resv_regions() 828 * @head: Reserved region list from iommu_get_resv_regions() 829 * 830 * Returns: Number of msi reserved regions on success (0 if platform 831 * doesn't require the reservation or no associated msi regions), 832 * appropriate error value otherwise. The ITS interrupt translation 833 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device 834 * are the msi reserved regions. 835 */ 836 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) 837 { 838 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 839 struct acpi_iort_its_group *its; 840 struct acpi_iort_node *iommu_node, *its_node = NULL; 841 int i, resv = 0; 842 843 iommu_node = iort_get_msi_resv_iommu(dev); 844 if (!iommu_node) 845 return 0; 846 847 /* 848 * Current logic to reserve ITS regions relies on HW topologies 849 * where a given PCI or named component maps its IDs to only one 850 * ITS group; if a PCI or named component can map its IDs to 851 * different ITS groups through IORT mappings this function has 852 * to be reworked to ensure we reserve regions for all ITS groups 853 * a given PCI or named component may map IDs to. 854 */ 855 856 for (i = 0; i < fwspec->num_ids; i++) { 857 its_node = iort_node_map_id(iommu_node, 858 fwspec->ids[i], 859 NULL, IORT_MSI_TYPE); 860 if (its_node) 861 break; 862 } 863 864 if (!its_node) 865 return 0; 866 867 /* Move to ITS specific data */ 868 its = (struct acpi_iort_its_group *)its_node->node_data; 869 870 for (i = 0; i < its->its_count; i++) { 871 phys_addr_t base; 872 873 if (!iort_find_its_base(its->identifiers[i], &base)) { 874 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; 875 struct iommu_resv_region *region; 876 877 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, 878 prot, IOMMU_RESV_MSI); 879 if (region) { 880 list_add_tail(®ion->list, head); 881 resv++; 882 } 883 } 884 } 885 886 return (resv == its->its_count) ? resv : -ENODEV; 887 } 888 889 static inline bool iort_iommu_driver_enabled(u8 type) 890 { 891 switch (type) { 892 case ACPI_IORT_NODE_SMMU_V3: 893 return IS_ENABLED(CONFIG_ARM_SMMU_V3); 894 case ACPI_IORT_NODE_SMMU: 895 return IS_ENABLED(CONFIG_ARM_SMMU); 896 default: 897 pr_warn("IORT node type %u does not describe an SMMU\n", type); 898 return false; 899 } 900 } 901 902 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid, 903 struct fwnode_handle *fwnode, 904 const struct iommu_ops *ops) 905 { 906 int ret = iommu_fwspec_init(dev, fwnode, ops); 907 908 if (!ret) 909 ret = iommu_fwspec_add_ids(dev, &streamid, 1); 910 911 return ret; 912 } 913 914 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) 915 { 916 struct acpi_iort_root_complex *pci_rc; 917 918 pci_rc = (struct acpi_iort_root_complex *)node->node_data; 919 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; 920 } 921 922 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, 923 u32 streamid) 924 { 925 const struct iommu_ops *ops; 926 struct fwnode_handle *iort_fwnode; 927 928 if (!node) 929 return -ENODEV; 930 931 iort_fwnode = iort_get_fwnode(node); 932 if (!iort_fwnode) 933 return -ENODEV; 934 935 /* 936 * If the ops look-up fails, this means that either 937 * the SMMU drivers have not been probed yet or that 938 * the SMMU drivers are not built in the kernel; 939 * Depending on whether the SMMU drivers are built-in 940 * in the kernel or not, defer the IOMMU configuration 941 * or just abort it. 942 */ 943 ops = iommu_ops_from_fwnode(iort_fwnode); 944 if (!ops) 945 return iort_iommu_driver_enabled(node->type) ? 946 -EPROBE_DEFER : -ENODEV; 947 948 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops); 949 } 950 951 struct iort_pci_alias_info { 952 struct device *dev; 953 struct acpi_iort_node *node; 954 }; 955 956 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 957 { 958 struct iort_pci_alias_info *info = data; 959 struct acpi_iort_node *parent; 960 u32 streamid; 961 962 parent = iort_node_map_id(info->node, alias, &streamid, 963 IORT_IOMMU_TYPE); 964 return iort_iommu_xlate(info->dev, parent, streamid); 965 } 966 967 static void iort_named_component_init(struct device *dev, 968 struct acpi_iort_node *node) 969 { 970 struct acpi_iort_named_component *nc; 971 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 972 973 if (!fwspec) 974 return; 975 976 nc = (struct acpi_iort_named_component *)node->node_data; 977 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS, 978 nc->node_flags); 979 } 980 981 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node) 982 { 983 struct acpi_iort_node *parent; 984 int err = -ENODEV, i = 0; 985 u32 streamid = 0; 986 987 do { 988 989 parent = iort_node_map_platform_id(node, &streamid, 990 IORT_IOMMU_TYPE, 991 i++); 992 993 if (parent) 994 err = iort_iommu_xlate(dev, parent, streamid); 995 } while (parent && !err); 996 997 return err; 998 } 999 1000 static int iort_nc_iommu_map_id(struct device *dev, 1001 struct acpi_iort_node *node, 1002 const u32 *in_id) 1003 { 1004 struct acpi_iort_node *parent; 1005 u32 streamid; 1006 1007 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE); 1008 if (parent) 1009 return iort_iommu_xlate(dev, parent, streamid); 1010 1011 return -ENODEV; 1012 } 1013 1014 1015 /** 1016 * iort_iommu_configure_id - Set-up IOMMU configuration for a device. 1017 * 1018 * @dev: device to configure 1019 * @id_in: optional input id const value pointer 1020 * 1021 * Returns: iommu_ops pointer on configuration success 1022 * NULL on configuration failure 1023 */ 1024 const struct iommu_ops *iort_iommu_configure_id(struct device *dev, 1025 const u32 *id_in) 1026 { 1027 struct acpi_iort_node *node; 1028 const struct iommu_ops *ops; 1029 int err = -ENODEV; 1030 1031 /* 1032 * If we already translated the fwspec there 1033 * is nothing left to do, return the iommu_ops. 1034 */ 1035 ops = iort_fwspec_iommu_ops(dev); 1036 if (ops) 1037 return ops; 1038 1039 if (dev_is_pci(dev)) { 1040 struct iommu_fwspec *fwspec; 1041 struct pci_bus *bus = to_pci_dev(dev)->bus; 1042 struct iort_pci_alias_info info = { .dev = dev }; 1043 1044 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1045 iort_match_node_callback, &bus->dev); 1046 if (!node) 1047 return NULL; 1048 1049 info.node = node; 1050 err = pci_for_each_dma_alias(to_pci_dev(dev), 1051 iort_pci_iommu_init, &info); 1052 1053 fwspec = dev_iommu_fwspec_get(dev); 1054 if (fwspec && iort_pci_rc_supports_ats(node)) 1055 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; 1056 } else { 1057 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 1058 iort_match_node_callback, dev); 1059 if (!node) 1060 return NULL; 1061 1062 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) : 1063 iort_nc_iommu_map(dev, node); 1064 1065 if (!err) 1066 iort_named_component_init(dev, node); 1067 } 1068 1069 /* 1070 * If we have reason to believe the IOMMU driver missed the initial 1071 * add_device callback for dev, replay it to get things in order. 1072 */ 1073 if (!err) { 1074 ops = iort_fwspec_iommu_ops(dev); 1075 err = iort_add_device_replay(dev); 1076 } 1077 1078 /* Ignore all other errors apart from EPROBE_DEFER */ 1079 if (err == -EPROBE_DEFER) { 1080 ops = ERR_PTR(err); 1081 } else if (err) { 1082 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 1083 ops = NULL; 1084 } 1085 1086 return ops; 1087 } 1088 1089 #else 1090 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) 1091 { return 0; } 1092 const struct iommu_ops *iort_iommu_configure_id(struct device *dev, 1093 const u32 *input_id) 1094 { return NULL; } 1095 #endif 1096 1097 static int nc_dma_get_range(struct device *dev, u64 *size) 1098 { 1099 struct acpi_iort_node *node; 1100 struct acpi_iort_named_component *ncomp; 1101 1102 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, 1103 iort_match_node_callback, dev); 1104 if (!node) 1105 return -ENODEV; 1106 1107 ncomp = (struct acpi_iort_named_component *)node->node_data; 1108 1109 *size = ncomp->memory_address_limit >= 64 ? U64_MAX : 1110 1ULL<<ncomp->memory_address_limit; 1111 1112 return 0; 1113 } 1114 1115 static int rc_dma_get_range(struct device *dev, u64 *size) 1116 { 1117 struct acpi_iort_node *node; 1118 struct acpi_iort_root_complex *rc; 1119 struct pci_bus *pbus = to_pci_dev(dev)->bus; 1120 1121 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, 1122 iort_match_node_callback, &pbus->dev); 1123 if (!node || node->revision < 1) 1124 return -ENODEV; 1125 1126 rc = (struct acpi_iort_root_complex *)node->node_data; 1127 1128 *size = rc->memory_address_limit >= 64 ? U64_MAX : 1129 1ULL<<rc->memory_address_limit; 1130 1131 return 0; 1132 } 1133 1134 /** 1135 * iort_dma_setup() - Set-up device DMA parameters. 1136 * 1137 * @dev: device to configure 1138 * @dma_addr: device DMA address result pointer 1139 * @size: DMA range size result pointer 1140 */ 1141 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size) 1142 { 1143 u64 end, mask, dmaaddr = 0, size = 0, offset = 0; 1144 int ret; 1145 1146 /* 1147 * If @dev is expected to be DMA-capable then the bus code that created 1148 * it should have initialised its dma_mask pointer by this point. For 1149 * now, we'll continue the legacy behaviour of coercing it to the 1150 * coherent mask if not, but we'll no longer do so quietly. 1151 */ 1152 if (!dev->dma_mask) { 1153 dev_warn(dev, "DMA mask not set\n"); 1154 dev->dma_mask = &dev->coherent_dma_mask; 1155 } 1156 1157 if (dev->coherent_dma_mask) 1158 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); 1159 else 1160 size = 1ULL << 32; 1161 1162 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size); 1163 if (ret == -ENODEV) 1164 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size) 1165 : nc_dma_get_range(dev, &size); 1166 1167 if (!ret) { 1168 /* 1169 * Limit coherent and dma mask based on size retrieved from 1170 * firmware. 1171 */ 1172 end = dmaaddr + size - 1; 1173 mask = DMA_BIT_MASK(ilog2(end) + 1); 1174 dev->bus_dma_limit = end; 1175 dev->coherent_dma_mask = mask; 1176 *dev->dma_mask = mask; 1177 } 1178 1179 *dma_addr = dmaaddr; 1180 *dma_size = size; 1181 1182 ret = dma_direct_set_offset(dev, dmaaddr + offset, dmaaddr, size); 1183 1184 dev_dbg(dev, "dma_offset(%#08llx)%s\n", offset, ret ? " failed!" : ""); 1185 } 1186 1187 static void __init acpi_iort_register_irq(int hwirq, const char *name, 1188 int trigger, 1189 struct resource *res) 1190 { 1191 int irq = acpi_register_gsi(NULL, hwirq, trigger, 1192 ACPI_ACTIVE_HIGH); 1193 1194 if (irq <= 0) { 1195 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, 1196 name); 1197 return; 1198 } 1199 1200 res->start = irq; 1201 res->end = irq; 1202 res->flags = IORESOURCE_IRQ; 1203 res->name = name; 1204 } 1205 1206 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) 1207 { 1208 struct acpi_iort_smmu_v3 *smmu; 1209 /* Always present mem resource */ 1210 int num_res = 1; 1211 1212 /* Retrieve SMMUv3 specific data */ 1213 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1214 1215 if (smmu->event_gsiv) 1216 num_res++; 1217 1218 if (smmu->pri_gsiv) 1219 num_res++; 1220 1221 if (smmu->gerr_gsiv) 1222 num_res++; 1223 1224 if (smmu->sync_gsiv) 1225 num_res++; 1226 1227 return num_res; 1228 } 1229 1230 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) 1231 { 1232 /* 1233 * Cavium ThunderX2 implementation doesn't not support unique 1234 * irq line. Use single irq line for all the SMMUv3 interrupts. 1235 */ 1236 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1237 return false; 1238 1239 /* 1240 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking 1241 * SPI numbers here. 1242 */ 1243 return smmu->event_gsiv == smmu->pri_gsiv && 1244 smmu->event_gsiv == smmu->gerr_gsiv && 1245 smmu->event_gsiv == smmu->sync_gsiv; 1246 } 1247 1248 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) 1249 { 1250 /* 1251 * Override the size, for Cavium ThunderX2 implementation 1252 * which doesn't support the page 1 SMMU register space. 1253 */ 1254 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) 1255 return SZ_64K; 1256 1257 return SZ_128K; 1258 } 1259 1260 static void __init arm_smmu_v3_init_resources(struct resource *res, 1261 struct acpi_iort_node *node) 1262 { 1263 struct acpi_iort_smmu_v3 *smmu; 1264 int num_res = 0; 1265 1266 /* Retrieve SMMUv3 specific data */ 1267 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1268 1269 res[num_res].start = smmu->base_address; 1270 res[num_res].end = smmu->base_address + 1271 arm_smmu_v3_resource_size(smmu) - 1; 1272 res[num_res].flags = IORESOURCE_MEM; 1273 1274 num_res++; 1275 if (arm_smmu_v3_is_combined_irq(smmu)) { 1276 if (smmu->event_gsiv) 1277 acpi_iort_register_irq(smmu->event_gsiv, "combined", 1278 ACPI_EDGE_SENSITIVE, 1279 &res[num_res++]); 1280 } else { 1281 1282 if (smmu->event_gsiv) 1283 acpi_iort_register_irq(smmu->event_gsiv, "eventq", 1284 ACPI_EDGE_SENSITIVE, 1285 &res[num_res++]); 1286 1287 if (smmu->pri_gsiv) 1288 acpi_iort_register_irq(smmu->pri_gsiv, "priq", 1289 ACPI_EDGE_SENSITIVE, 1290 &res[num_res++]); 1291 1292 if (smmu->gerr_gsiv) 1293 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", 1294 ACPI_EDGE_SENSITIVE, 1295 &res[num_res++]); 1296 1297 if (smmu->sync_gsiv) 1298 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", 1299 ACPI_EDGE_SENSITIVE, 1300 &res[num_res++]); 1301 } 1302 } 1303 1304 static void __init arm_smmu_v3_dma_configure(struct device *dev, 1305 struct acpi_iort_node *node) 1306 { 1307 struct acpi_iort_smmu_v3 *smmu; 1308 enum dev_dma_attr attr; 1309 1310 /* Retrieve SMMUv3 specific data */ 1311 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1312 1313 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? 1314 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 1315 1316 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ 1317 dev->dma_mask = &dev->coherent_dma_mask; 1318 1319 /* Configure DMA for the page table walker */ 1320 acpi_dma_configure(dev, attr); 1321 } 1322 1323 #if defined(CONFIG_ACPI_NUMA) 1324 /* 1325 * set numa proximity domain for smmuv3 device 1326 */ 1327 static int __init arm_smmu_v3_set_proximity(struct device *dev, 1328 struct acpi_iort_node *node) 1329 { 1330 struct acpi_iort_smmu_v3 *smmu; 1331 1332 smmu = (struct acpi_iort_smmu_v3 *)node->node_data; 1333 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { 1334 int dev_node = pxm_to_node(smmu->pxm); 1335 1336 if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) 1337 return -EINVAL; 1338 1339 set_dev_node(dev, dev_node); 1340 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", 1341 smmu->base_address, 1342 smmu->pxm); 1343 } 1344 return 0; 1345 } 1346 #else 1347 #define arm_smmu_v3_set_proximity NULL 1348 #endif 1349 1350 static int __init arm_smmu_count_resources(struct acpi_iort_node *node) 1351 { 1352 struct acpi_iort_smmu *smmu; 1353 1354 /* Retrieve SMMU specific data */ 1355 smmu = (struct acpi_iort_smmu *)node->node_data; 1356 1357 /* 1358 * Only consider the global fault interrupt and ignore the 1359 * configuration access interrupt. 1360 * 1361 * MMIO address and global fault interrupt resources are always 1362 * present so add them to the context interrupt count as a static 1363 * value. 1364 */ 1365 return smmu->context_interrupt_count + 2; 1366 } 1367 1368 static void __init arm_smmu_init_resources(struct resource *res, 1369 struct acpi_iort_node *node) 1370 { 1371 struct acpi_iort_smmu *smmu; 1372 int i, hw_irq, trigger, num_res = 0; 1373 u64 *ctx_irq, *glb_irq; 1374 1375 /* Retrieve SMMU specific data */ 1376 smmu = (struct acpi_iort_smmu *)node->node_data; 1377 1378 res[num_res].start = smmu->base_address; 1379 res[num_res].end = smmu->base_address + smmu->span - 1; 1380 res[num_res].flags = IORESOURCE_MEM; 1381 num_res++; 1382 1383 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); 1384 /* Global IRQs */ 1385 hw_irq = IORT_IRQ_MASK(glb_irq[0]); 1386 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); 1387 1388 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, 1389 &res[num_res++]); 1390 1391 /* Context IRQs */ 1392 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); 1393 for (i = 0; i < smmu->context_interrupt_count; i++) { 1394 hw_irq = IORT_IRQ_MASK(ctx_irq[i]); 1395 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); 1396 1397 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, 1398 &res[num_res++]); 1399 } 1400 } 1401 1402 static void __init arm_smmu_dma_configure(struct device *dev, 1403 struct acpi_iort_node *node) 1404 { 1405 struct acpi_iort_smmu *smmu; 1406 enum dev_dma_attr attr; 1407 1408 /* Retrieve SMMU specific data */ 1409 smmu = (struct acpi_iort_smmu *)node->node_data; 1410 1411 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? 1412 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; 1413 1414 /* We expect the dma masks to be equivalent for SMMU set-ups */ 1415 dev->dma_mask = &dev->coherent_dma_mask; 1416 1417 /* Configure DMA for the page table walker */ 1418 acpi_dma_configure(dev, attr); 1419 } 1420 1421 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) 1422 { 1423 struct acpi_iort_pmcg *pmcg; 1424 1425 /* Retrieve PMCG specific data */ 1426 pmcg = (struct acpi_iort_pmcg *)node->node_data; 1427 1428 /* 1429 * There are always 2 memory resources. 1430 * If the overflow_gsiv is present then add that for a total of 3. 1431 */ 1432 return pmcg->overflow_gsiv ? 3 : 2; 1433 } 1434 1435 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, 1436 struct acpi_iort_node *node) 1437 { 1438 struct acpi_iort_pmcg *pmcg; 1439 1440 /* Retrieve PMCG specific data */ 1441 pmcg = (struct acpi_iort_pmcg *)node->node_data; 1442 1443 res[0].start = pmcg->page0_base_address; 1444 res[0].end = pmcg->page0_base_address + SZ_4K - 1; 1445 res[0].flags = IORESOURCE_MEM; 1446 res[1].start = pmcg->page1_base_address; 1447 res[1].end = pmcg->page1_base_address + SZ_4K - 1; 1448 res[1].flags = IORESOURCE_MEM; 1449 1450 if (pmcg->overflow_gsiv) 1451 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", 1452 ACPI_EDGE_SENSITIVE, &res[2]); 1453 } 1454 1455 static struct acpi_platform_list pmcg_plat_info[] __initdata = { 1456 /* HiSilicon Hip08 Platform */ 1457 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, 1458 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08}, 1459 { } 1460 }; 1461 1462 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) 1463 { 1464 u32 model; 1465 int idx; 1466 1467 idx = acpi_match_platform_list(pmcg_plat_info); 1468 if (idx >= 0) 1469 model = pmcg_plat_info[idx].data; 1470 else 1471 model = IORT_SMMU_V3_PMCG_GENERIC; 1472 1473 return platform_device_add_data(pdev, &model, sizeof(model)); 1474 } 1475 1476 struct iort_dev_config { 1477 const char *name; 1478 int (*dev_init)(struct acpi_iort_node *node); 1479 void (*dev_dma_configure)(struct device *dev, 1480 struct acpi_iort_node *node); 1481 int (*dev_count_resources)(struct acpi_iort_node *node); 1482 void (*dev_init_resources)(struct resource *res, 1483 struct acpi_iort_node *node); 1484 int (*dev_set_proximity)(struct device *dev, 1485 struct acpi_iort_node *node); 1486 int (*dev_add_platdata)(struct platform_device *pdev); 1487 }; 1488 1489 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { 1490 .name = "arm-smmu-v3", 1491 .dev_dma_configure = arm_smmu_v3_dma_configure, 1492 .dev_count_resources = arm_smmu_v3_count_resources, 1493 .dev_init_resources = arm_smmu_v3_init_resources, 1494 .dev_set_proximity = arm_smmu_v3_set_proximity, 1495 }; 1496 1497 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { 1498 .name = "arm-smmu", 1499 .dev_dma_configure = arm_smmu_dma_configure, 1500 .dev_count_resources = arm_smmu_count_resources, 1501 .dev_init_resources = arm_smmu_init_resources, 1502 }; 1503 1504 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { 1505 .name = "arm-smmu-v3-pmcg", 1506 .dev_count_resources = arm_smmu_v3_pmcg_count_resources, 1507 .dev_init_resources = arm_smmu_v3_pmcg_init_resources, 1508 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, 1509 }; 1510 1511 static __init const struct iort_dev_config *iort_get_dev_cfg( 1512 struct acpi_iort_node *node) 1513 { 1514 switch (node->type) { 1515 case ACPI_IORT_NODE_SMMU_V3: 1516 return &iort_arm_smmu_v3_cfg; 1517 case ACPI_IORT_NODE_SMMU: 1518 return &iort_arm_smmu_cfg; 1519 case ACPI_IORT_NODE_PMCG: 1520 return &iort_arm_smmu_v3_pmcg_cfg; 1521 default: 1522 return NULL; 1523 } 1524 } 1525 1526 /** 1527 * iort_add_platform_device() - Allocate a platform device for IORT node 1528 * @node: Pointer to device ACPI IORT node 1529 * 1530 * Returns: 0 on success, <0 failure 1531 */ 1532 static int __init iort_add_platform_device(struct acpi_iort_node *node, 1533 const struct iort_dev_config *ops) 1534 { 1535 struct fwnode_handle *fwnode; 1536 struct platform_device *pdev; 1537 struct resource *r; 1538 int ret, count; 1539 1540 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); 1541 if (!pdev) 1542 return -ENOMEM; 1543 1544 if (ops->dev_set_proximity) { 1545 ret = ops->dev_set_proximity(&pdev->dev, node); 1546 if (ret) 1547 goto dev_put; 1548 } 1549 1550 count = ops->dev_count_resources(node); 1551 1552 r = kcalloc(count, sizeof(*r), GFP_KERNEL); 1553 if (!r) { 1554 ret = -ENOMEM; 1555 goto dev_put; 1556 } 1557 1558 ops->dev_init_resources(r, node); 1559 1560 ret = platform_device_add_resources(pdev, r, count); 1561 /* 1562 * Resources are duplicated in platform_device_add_resources, 1563 * free their allocated memory 1564 */ 1565 kfree(r); 1566 1567 if (ret) 1568 goto dev_put; 1569 1570 /* 1571 * Platform devices based on PMCG nodes uses platform_data to 1572 * pass the hardware model info to the driver. For others, add 1573 * a copy of IORT node pointer to platform_data to be used to 1574 * retrieve IORT data information. 1575 */ 1576 if (ops->dev_add_platdata) 1577 ret = ops->dev_add_platdata(pdev); 1578 else 1579 ret = platform_device_add_data(pdev, &node, sizeof(node)); 1580 1581 if (ret) 1582 goto dev_put; 1583 1584 fwnode = iort_get_fwnode(node); 1585 1586 if (!fwnode) { 1587 ret = -ENODEV; 1588 goto dev_put; 1589 } 1590 1591 pdev->dev.fwnode = fwnode; 1592 1593 if (ops->dev_dma_configure) 1594 ops->dev_dma_configure(&pdev->dev, node); 1595 1596 iort_set_device_domain(&pdev->dev, node); 1597 1598 ret = platform_device_add(pdev); 1599 if (ret) 1600 goto dma_deconfigure; 1601 1602 return 0; 1603 1604 dma_deconfigure: 1605 arch_teardown_dma_ops(&pdev->dev); 1606 dev_put: 1607 platform_device_put(pdev); 1608 1609 return ret; 1610 } 1611 1612 #ifdef CONFIG_PCI 1613 static void __init iort_enable_acs(struct acpi_iort_node *iort_node) 1614 { 1615 static bool acs_enabled __initdata; 1616 1617 if (acs_enabled) 1618 return; 1619 1620 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { 1621 struct acpi_iort_node *parent; 1622 struct acpi_iort_id_mapping *map; 1623 int i; 1624 1625 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, 1626 iort_node->mapping_offset); 1627 1628 for (i = 0; i < iort_node->mapping_count; i++, map++) { 1629 if (!map->output_reference) 1630 continue; 1631 1632 parent = ACPI_ADD_PTR(struct acpi_iort_node, 1633 iort_table, map->output_reference); 1634 /* 1635 * If we detect a RC->SMMU mapping, make sure 1636 * we enable ACS on the system. 1637 */ 1638 if ((parent->type == ACPI_IORT_NODE_SMMU) || 1639 (parent->type == ACPI_IORT_NODE_SMMU_V3)) { 1640 pci_request_acs(); 1641 acs_enabled = true; 1642 return; 1643 } 1644 } 1645 } 1646 } 1647 #else 1648 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } 1649 #endif 1650 1651 static void __init iort_init_platform_devices(void) 1652 { 1653 struct acpi_iort_node *iort_node, *iort_end; 1654 struct acpi_table_iort *iort; 1655 struct fwnode_handle *fwnode; 1656 int i, ret; 1657 const struct iort_dev_config *ops; 1658 1659 /* 1660 * iort_table and iort both point to the start of IORT table, but 1661 * have different struct types 1662 */ 1663 iort = (struct acpi_table_iort *)iort_table; 1664 1665 /* Get the first IORT node */ 1666 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1667 iort->node_offset); 1668 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, 1669 iort_table->length); 1670 1671 for (i = 0; i < iort->node_count; i++) { 1672 if (iort_node >= iort_end) { 1673 pr_err("iort node pointer overflows, bad table\n"); 1674 return; 1675 } 1676 1677 iort_enable_acs(iort_node); 1678 1679 ops = iort_get_dev_cfg(iort_node); 1680 if (ops) { 1681 fwnode = acpi_alloc_fwnode_static(); 1682 if (!fwnode) 1683 return; 1684 1685 iort_set_fwnode(iort_node, fwnode); 1686 1687 ret = iort_add_platform_device(iort_node, ops); 1688 if (ret) { 1689 iort_delete_fwnode(iort_node); 1690 acpi_free_fwnode_static(fwnode); 1691 return; 1692 } 1693 } 1694 1695 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, 1696 iort_node->length); 1697 } 1698 } 1699 1700 void __init acpi_iort_init(void) 1701 { 1702 acpi_status status; 1703 1704 /* iort_table will be used at runtime after the iort init, 1705 * so we don't need to call acpi_put_table() to release 1706 * the IORT table mapping. 1707 */ 1708 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); 1709 if (ACPI_FAILURE(status)) { 1710 if (status != AE_NOT_FOUND) { 1711 const char *msg = acpi_format_exception(status); 1712 1713 pr_err("Failed to get table, %s\n", msg); 1714 } 1715 1716 return; 1717 } 1718 1719 iort_init_platform_devices(); 1720 } 1721