1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Freescale Management Complex (MC) bus driver 4 * 5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc. 6 * Copyright 2019-2020 NXP 7 * Author: German Rivera <German.Rivera@freescale.com> 8 * 9 */ 10 11 #define pr_fmt(fmt) "fsl-mc: " fmt 12 13 #include <linux/module.h> 14 #include <linux/of_device.h> 15 #include <linux/of_address.h> 16 #include <linux/ioport.h> 17 #include <linux/platform_device.h> 18 #include <linux/slab.h> 19 #include <linux/limits.h> 20 #include <linux/bitops.h> 21 #include <linux/dma-mapping.h> 22 #include <linux/acpi.h> 23 #include <linux/iommu.h> 24 #include <linux/dma-map-ops.h> 25 26 #include "fsl-mc-private.h" 27 28 /* 29 * Default DMA mask for devices on a fsl-mc bus 30 */ 31 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL) 32 33 static struct fsl_mc_version mc_version; 34 35 /** 36 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device 37 * @root_mc_bus_dev: fsl-mc device representing the root DPRC 38 * @num_translation_ranges: number of entries in addr_translation_ranges 39 * @translation_ranges: array of bus to system address translation ranges 40 * @fsl_mc_regs: base address of register bank 41 */ 42 struct fsl_mc { 43 struct fsl_mc_device *root_mc_bus_dev; 44 u8 num_translation_ranges; 45 struct fsl_mc_addr_translation_range *translation_ranges; 46 void __iomem *fsl_mc_regs; 47 }; 48 49 /** 50 * struct fsl_mc_addr_translation_range - bus to system address translation 51 * range 52 * @mc_region_type: Type of MC region for the range being translated 53 * @start_mc_offset: Start MC offset of the range being translated 54 * @end_mc_offset: MC offset of the first byte after the range (last MC 55 * offset of the range is end_mc_offset - 1) 56 * @start_phys_addr: system physical address corresponding to start_mc_addr 57 */ 58 struct fsl_mc_addr_translation_range { 59 enum dprc_region_type mc_region_type; 60 u64 start_mc_offset; 61 u64 end_mc_offset; 62 phys_addr_t start_phys_addr; 63 }; 64 65 #define FSL_MC_GCR1 0x0 66 #define GCR1_P1_STOP BIT(31) 67 #define GCR1_P2_STOP BIT(30) 68 69 #define FSL_MC_FAPR 0x28 70 #define MC_FAPR_PL BIT(18) 71 #define MC_FAPR_BMT BIT(17) 72 73 static phys_addr_t mc_portal_base_phys_addr; 74 75 /** 76 * fsl_mc_bus_match - device to driver matching callback 77 * @dev: the fsl-mc device to match against 78 * @drv: the device driver to search for matching fsl-mc object type 79 * structures 80 * 81 * Returns 1 on success, 0 otherwise. 82 */ 83 static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv) 84 { 85 const struct fsl_mc_device_id *id; 86 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 87 const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv); 88 bool found = false; 89 90 /* When driver_override is set, only bind to the matching driver */ 91 if (mc_dev->driver_override) { 92 found = !strcmp(mc_dev->driver_override, mc_drv->driver.name); 93 goto out; 94 } 95 96 if (!mc_drv->match_id_table) 97 goto out; 98 99 /* 100 * If the object is not 'plugged' don't match. 101 * Only exception is the root DPRC, which is a special case. 102 */ 103 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 && 104 !fsl_mc_is_root_dprc(&mc_dev->dev)) 105 goto out; 106 107 /* 108 * Traverse the match_id table of the given driver, trying to find 109 * a matching for the given device. 110 */ 111 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) { 112 if (id->vendor == mc_dev->obj_desc.vendor && 113 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) { 114 found = true; 115 116 break; 117 } 118 } 119 120 out: 121 dev_dbg(dev, "%smatched\n", found ? "" : "not "); 122 return found; 123 } 124 125 /* 126 * fsl_mc_bus_uevent - callback invoked when a device is added 127 */ 128 static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env) 129 { 130 const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 131 132 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s", 133 mc_dev->obj_desc.vendor, 134 mc_dev->obj_desc.type)) 135 return -ENOMEM; 136 137 return 0; 138 } 139 140 static int fsl_mc_probe(struct device *dev) 141 { 142 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 143 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 144 145 if (mc_drv->probe) 146 return mc_drv->probe(mc_dev); 147 148 return 0; 149 } 150 151 static void fsl_mc_remove(struct device *dev) 152 { 153 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 154 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 155 156 if (mc_drv->remove) 157 mc_drv->remove(mc_dev); 158 } 159 160 static void fsl_mc_shutdown(struct device *dev) 161 { 162 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 163 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 164 165 if (dev->driver && mc_drv->shutdown) 166 mc_drv->shutdown(mc_dev); 167 } 168 169 static int fsl_mc_dma_configure(struct device *dev) 170 { 171 const struct device_driver *drv = READ_ONCE(dev->driver); 172 struct device *dma_dev = dev; 173 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 174 u32 input_id = mc_dev->icid; 175 int ret; 176 177 while (dev_is_fsl_mc(dma_dev)) 178 dma_dev = dma_dev->parent; 179 180 if (dev_of_node(dma_dev)) 181 ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id); 182 else 183 ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id); 184 185 /* @drv may not be valid when we're called from the IOMMU layer */ 186 if (!ret && drv && !to_fsl_mc_driver(drv)->driver_managed_dma) { 187 ret = iommu_device_use_default_domain(dev); 188 if (ret) 189 arch_teardown_dma_ops(dev); 190 } 191 192 return ret; 193 } 194 195 static void fsl_mc_dma_cleanup(struct device *dev) 196 { 197 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 198 199 if (!mc_drv->driver_managed_dma) 200 iommu_device_unuse_default_domain(dev); 201 } 202 203 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 204 char *buf) 205 { 206 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 207 208 return sysfs_emit(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor, 209 mc_dev->obj_desc.type); 210 } 211 static DEVICE_ATTR_RO(modalias); 212 213 static ssize_t driver_override_store(struct device *dev, 214 struct device_attribute *attr, 215 const char *buf, size_t count) 216 { 217 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 218 int ret; 219 220 if (WARN_ON(dev->bus != &fsl_mc_bus_type)) 221 return -EINVAL; 222 223 ret = driver_set_override(dev, &mc_dev->driver_override, buf, count); 224 if (ret) 225 return ret; 226 227 return count; 228 } 229 230 static ssize_t driver_override_show(struct device *dev, 231 struct device_attribute *attr, char *buf) 232 { 233 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 234 ssize_t len; 235 236 device_lock(dev); 237 len = sysfs_emit(buf, "%s\n", mc_dev->driver_override); 238 device_unlock(dev); 239 return len; 240 } 241 static DEVICE_ATTR_RW(driver_override); 242 243 static struct attribute *fsl_mc_dev_attrs[] = { 244 &dev_attr_modalias.attr, 245 &dev_attr_driver_override.attr, 246 NULL, 247 }; 248 249 ATTRIBUTE_GROUPS(fsl_mc_dev); 250 251 static int scan_fsl_mc_bus(struct device *dev, void *data) 252 { 253 struct fsl_mc_device *root_mc_dev; 254 struct fsl_mc_bus *root_mc_bus; 255 256 if (!fsl_mc_is_root_dprc(dev)) 257 goto exit; 258 259 root_mc_dev = to_fsl_mc_device(dev); 260 root_mc_bus = to_fsl_mc_bus(root_mc_dev); 261 mutex_lock(&root_mc_bus->scan_mutex); 262 dprc_scan_objects(root_mc_dev, false); 263 mutex_unlock(&root_mc_bus->scan_mutex); 264 265 exit: 266 return 0; 267 } 268 269 static ssize_t rescan_store(const struct bus_type *bus, 270 const char *buf, size_t count) 271 { 272 unsigned long val; 273 274 if (kstrtoul(buf, 0, &val) < 0) 275 return -EINVAL; 276 277 if (val) 278 bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus); 279 280 return count; 281 } 282 static BUS_ATTR_WO(rescan); 283 284 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data) 285 { 286 struct fsl_mc_device *root_mc_dev; 287 unsigned long val; 288 char *buf = data; 289 290 if (!fsl_mc_is_root_dprc(dev)) 291 goto exit; 292 293 root_mc_dev = to_fsl_mc_device(dev); 294 295 if (kstrtoul(buf, 0, &val) < 0) 296 return -EINVAL; 297 298 if (val) 299 enable_dprc_irq(root_mc_dev); 300 else 301 disable_dprc_irq(root_mc_dev); 302 303 exit: 304 return 0; 305 } 306 307 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data) 308 { 309 struct fsl_mc_device *root_mc_dev; 310 char *buf = data; 311 312 if (!fsl_mc_is_root_dprc(dev)) 313 goto exit; 314 315 root_mc_dev = to_fsl_mc_device(dev); 316 317 sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev)); 318 exit: 319 return 0; 320 } 321 322 static ssize_t autorescan_store(const struct bus_type *bus, 323 const char *buf, size_t count) 324 { 325 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan); 326 327 return count; 328 } 329 330 static ssize_t autorescan_show(const struct bus_type *bus, char *buf) 331 { 332 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan); 333 return strlen(buf); 334 } 335 336 static BUS_ATTR_RW(autorescan); 337 338 static struct attribute *fsl_mc_bus_attrs[] = { 339 &bus_attr_rescan.attr, 340 &bus_attr_autorescan.attr, 341 NULL, 342 }; 343 344 ATTRIBUTE_GROUPS(fsl_mc_bus); 345 346 const struct bus_type fsl_mc_bus_type = { 347 .name = "fsl-mc", 348 .match = fsl_mc_bus_match, 349 .uevent = fsl_mc_bus_uevent, 350 .probe = fsl_mc_probe, 351 .remove = fsl_mc_remove, 352 .shutdown = fsl_mc_shutdown, 353 .dma_configure = fsl_mc_dma_configure, 354 .dma_cleanup = fsl_mc_dma_cleanup, 355 .dev_groups = fsl_mc_dev_groups, 356 .bus_groups = fsl_mc_bus_groups, 357 }; 358 EXPORT_SYMBOL_GPL(fsl_mc_bus_type); 359 360 const struct device_type fsl_mc_bus_dprc_type = { 361 .name = "fsl_mc_bus_dprc" 362 }; 363 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type); 364 365 const struct device_type fsl_mc_bus_dpni_type = { 366 .name = "fsl_mc_bus_dpni" 367 }; 368 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type); 369 370 const struct device_type fsl_mc_bus_dpio_type = { 371 .name = "fsl_mc_bus_dpio" 372 }; 373 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type); 374 375 const struct device_type fsl_mc_bus_dpsw_type = { 376 .name = "fsl_mc_bus_dpsw" 377 }; 378 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type); 379 380 const struct device_type fsl_mc_bus_dpbp_type = { 381 .name = "fsl_mc_bus_dpbp" 382 }; 383 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type); 384 385 const struct device_type fsl_mc_bus_dpcon_type = { 386 .name = "fsl_mc_bus_dpcon" 387 }; 388 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type); 389 390 const struct device_type fsl_mc_bus_dpmcp_type = { 391 .name = "fsl_mc_bus_dpmcp" 392 }; 393 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type); 394 395 const struct device_type fsl_mc_bus_dpmac_type = { 396 .name = "fsl_mc_bus_dpmac" 397 }; 398 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type); 399 400 const struct device_type fsl_mc_bus_dprtc_type = { 401 .name = "fsl_mc_bus_dprtc" 402 }; 403 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type); 404 405 const struct device_type fsl_mc_bus_dpseci_type = { 406 .name = "fsl_mc_bus_dpseci" 407 }; 408 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type); 409 410 const struct device_type fsl_mc_bus_dpdmux_type = { 411 .name = "fsl_mc_bus_dpdmux" 412 }; 413 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type); 414 415 const struct device_type fsl_mc_bus_dpdcei_type = { 416 .name = "fsl_mc_bus_dpdcei" 417 }; 418 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type); 419 420 const struct device_type fsl_mc_bus_dpaiop_type = { 421 .name = "fsl_mc_bus_dpaiop" 422 }; 423 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type); 424 425 const struct device_type fsl_mc_bus_dpci_type = { 426 .name = "fsl_mc_bus_dpci" 427 }; 428 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type); 429 430 const struct device_type fsl_mc_bus_dpdmai_type = { 431 .name = "fsl_mc_bus_dpdmai" 432 }; 433 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type); 434 435 static const struct device_type fsl_mc_bus_dpdbg_type = { 436 .name = "fsl_mc_bus_dpdbg" 437 }; 438 439 static const struct device_type *fsl_mc_get_device_type(const char *type) 440 { 441 static const struct { 442 const struct device_type *dev_type; 443 const char *type; 444 } dev_types[] = { 445 { &fsl_mc_bus_dprc_type, "dprc" }, 446 { &fsl_mc_bus_dpni_type, "dpni" }, 447 { &fsl_mc_bus_dpio_type, "dpio" }, 448 { &fsl_mc_bus_dpsw_type, "dpsw" }, 449 { &fsl_mc_bus_dpbp_type, "dpbp" }, 450 { &fsl_mc_bus_dpcon_type, "dpcon" }, 451 { &fsl_mc_bus_dpmcp_type, "dpmcp" }, 452 { &fsl_mc_bus_dpmac_type, "dpmac" }, 453 { &fsl_mc_bus_dprtc_type, "dprtc" }, 454 { &fsl_mc_bus_dpseci_type, "dpseci" }, 455 { &fsl_mc_bus_dpdmux_type, "dpdmux" }, 456 { &fsl_mc_bus_dpdcei_type, "dpdcei" }, 457 { &fsl_mc_bus_dpaiop_type, "dpaiop" }, 458 { &fsl_mc_bus_dpci_type, "dpci" }, 459 { &fsl_mc_bus_dpdmai_type, "dpdmai" }, 460 { &fsl_mc_bus_dpdbg_type, "dpdbg" }, 461 { NULL, NULL } 462 }; 463 int i; 464 465 for (i = 0; dev_types[i].dev_type; i++) 466 if (!strcmp(dev_types[i].type, type)) 467 return dev_types[i].dev_type; 468 469 return NULL; 470 } 471 472 /* 473 * __fsl_mc_driver_register - registers a child device driver with the 474 * MC bus 475 * 476 * This function is implicitly invoked from the registration function of 477 * fsl_mc device drivers, which is generated by the 478 * module_fsl_mc_driver() macro. 479 */ 480 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver, 481 struct module *owner) 482 { 483 int error; 484 485 mc_driver->driver.owner = owner; 486 mc_driver->driver.bus = &fsl_mc_bus_type; 487 488 error = driver_register(&mc_driver->driver); 489 if (error < 0) { 490 pr_err("driver_register() failed for %s: %d\n", 491 mc_driver->driver.name, error); 492 return error; 493 } 494 495 return 0; 496 } 497 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register); 498 499 /* 500 * fsl_mc_driver_unregister - unregisters a device driver from the 501 * MC bus 502 */ 503 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver) 504 { 505 driver_unregister(&mc_driver->driver); 506 } 507 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister); 508 509 /** 510 * mc_get_version() - Retrieves the Management Complex firmware 511 * version information 512 * @mc_io: Pointer to opaque I/O object 513 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_' 514 * @mc_ver_info: Returned version information structure 515 * 516 * Return: '0' on Success; Error code otherwise. 517 */ 518 static int mc_get_version(struct fsl_mc_io *mc_io, 519 u32 cmd_flags, 520 struct fsl_mc_version *mc_ver_info) 521 { 522 struct fsl_mc_command cmd = { 0 }; 523 struct dpmng_rsp_get_version *rsp_params; 524 int err; 525 526 /* prepare command */ 527 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION, 528 cmd_flags, 529 0); 530 531 /* send command to mc*/ 532 err = mc_send_command(mc_io, &cmd); 533 if (err) 534 return err; 535 536 /* retrieve response parameters */ 537 rsp_params = (struct dpmng_rsp_get_version *)cmd.params; 538 mc_ver_info->revision = le32_to_cpu(rsp_params->revision); 539 mc_ver_info->major = le32_to_cpu(rsp_params->version_major); 540 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor); 541 542 return 0; 543 } 544 545 /** 546 * fsl_mc_get_version - function to retrieve the MC f/w version information 547 * 548 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise. 549 */ 550 struct fsl_mc_version *fsl_mc_get_version(void) 551 { 552 if (mc_version.major) 553 return &mc_version; 554 555 return NULL; 556 } 557 EXPORT_SYMBOL_GPL(fsl_mc_get_version); 558 559 /* 560 * fsl_mc_get_root_dprc - function to traverse to the root dprc 561 */ 562 void fsl_mc_get_root_dprc(struct device *dev, 563 struct device **root_dprc_dev) 564 { 565 if (!dev) { 566 *root_dprc_dev = NULL; 567 } else if (!dev_is_fsl_mc(dev)) { 568 *root_dprc_dev = NULL; 569 } else { 570 *root_dprc_dev = dev; 571 while (dev_is_fsl_mc((*root_dprc_dev)->parent)) 572 *root_dprc_dev = (*root_dprc_dev)->parent; 573 } 574 } 575 576 static int get_dprc_attr(struct fsl_mc_io *mc_io, 577 int container_id, struct dprc_attributes *attr) 578 { 579 u16 dprc_handle; 580 int error; 581 582 error = dprc_open(mc_io, 0, container_id, &dprc_handle); 583 if (error < 0) { 584 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error); 585 return error; 586 } 587 588 memset(attr, 0, sizeof(struct dprc_attributes)); 589 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr); 590 if (error < 0) { 591 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n", 592 error); 593 goto common_cleanup; 594 } 595 596 error = 0; 597 598 common_cleanup: 599 (void)dprc_close(mc_io, 0, dprc_handle); 600 return error; 601 } 602 603 static int get_dprc_icid(struct fsl_mc_io *mc_io, 604 int container_id, u32 *icid) 605 { 606 struct dprc_attributes attr; 607 int error; 608 609 error = get_dprc_attr(mc_io, container_id, &attr); 610 if (error == 0) 611 *icid = attr.icid; 612 613 return error; 614 } 615 616 static int translate_mc_addr(struct fsl_mc_device *mc_dev, 617 enum dprc_region_type mc_region_type, 618 u64 mc_offset, phys_addr_t *phys_addr) 619 { 620 int i; 621 struct device *root_dprc_dev; 622 struct fsl_mc *mc; 623 624 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev); 625 mc = dev_get_drvdata(root_dprc_dev->parent); 626 627 if (mc->num_translation_ranges == 0) { 628 /* 629 * Do identity mapping: 630 */ 631 *phys_addr = mc_offset; 632 return 0; 633 } 634 635 for (i = 0; i < mc->num_translation_ranges; i++) { 636 struct fsl_mc_addr_translation_range *range = 637 &mc->translation_ranges[i]; 638 639 if (mc_region_type == range->mc_region_type && 640 mc_offset >= range->start_mc_offset && 641 mc_offset < range->end_mc_offset) { 642 *phys_addr = range->start_phys_addr + 643 (mc_offset - range->start_mc_offset); 644 return 0; 645 } 646 } 647 648 return -EFAULT; 649 } 650 651 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev, 652 struct fsl_mc_device *mc_bus_dev) 653 { 654 int i; 655 int error; 656 struct resource *regions; 657 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc; 658 struct device *parent_dev = mc_dev->dev.parent; 659 enum dprc_region_type mc_region_type; 660 661 if (is_fsl_mc_bus_dprc(mc_dev) || 662 is_fsl_mc_bus_dpmcp(mc_dev)) { 663 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL; 664 } else if (is_fsl_mc_bus_dpio(mc_dev)) { 665 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL; 666 } else { 667 /* 668 * This function should not have been called for this MC object 669 * type, as this object type is not supposed to have MMIO 670 * regions 671 */ 672 return -EINVAL; 673 } 674 675 regions = kmalloc_objs(regions[0], obj_desc->region_count); 676 if (!regions) 677 return -ENOMEM; 678 679 for (i = 0; i < obj_desc->region_count; i++) { 680 struct dprc_region_desc region_desc; 681 682 error = dprc_get_obj_region(mc_bus_dev->mc_io, 683 0, 684 mc_bus_dev->mc_handle, 685 obj_desc->type, 686 obj_desc->id, i, ®ion_desc); 687 if (error < 0) { 688 dev_err(parent_dev, 689 "dprc_get_obj_region() failed: %d\n", error); 690 goto error_cleanup_regions; 691 } 692 /* 693 * Older MC only returned region offset and no base address 694 * If base address is in the region_desc use it otherwise 695 * revert to old mechanism 696 */ 697 if (region_desc.base_address) { 698 regions[i].start = region_desc.base_address + 699 region_desc.base_offset; 700 } else { 701 error = translate_mc_addr(mc_dev, mc_region_type, 702 region_desc.base_offset, 703 ®ions[i].start); 704 705 /* 706 * Some versions of the MC firmware wrongly report 707 * 0 for register base address of the DPMCP associated 708 * with child DPRC objects thus rendering them unusable. 709 * This is particularly troublesome in ACPI boot 710 * scenarios where the legacy way of extracting this 711 * base address from the device tree does not apply. 712 * Given that DPMCPs share the same base address, 713 * workaround this by using the base address extracted 714 * from the root DPRC container. 715 */ 716 if (is_fsl_mc_bus_dprc(mc_dev) && 717 regions[i].start == region_desc.base_offset) 718 regions[i].start += mc_portal_base_phys_addr; 719 } 720 721 if (error < 0) { 722 dev_err(parent_dev, 723 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n", 724 region_desc.base_offset, 725 obj_desc->type, obj_desc->id, i); 726 goto error_cleanup_regions; 727 } 728 729 regions[i].end = regions[i].start + region_desc.size - 1; 730 regions[i].name = "fsl-mc object MMIO region"; 731 regions[i].flags = region_desc.flags & IORESOURCE_BITS; 732 regions[i].flags |= IORESOURCE_MEM; 733 } 734 735 mc_dev->regions = regions; 736 return 0; 737 738 error_cleanup_regions: 739 kfree(regions); 740 return error; 741 } 742 743 /* 744 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc 745 */ 746 bool fsl_mc_is_root_dprc(struct device *dev) 747 { 748 struct device *root_dprc_dev; 749 750 fsl_mc_get_root_dprc(dev, &root_dprc_dev); 751 if (!root_dprc_dev) 752 return false; 753 return dev == root_dprc_dev; 754 } 755 756 static void fsl_mc_device_release(struct device *dev) 757 { 758 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 759 760 kfree(mc_dev->regions); 761 762 if (is_fsl_mc_bus_dprc(mc_dev)) 763 kfree(to_fsl_mc_bus(mc_dev)); 764 else 765 kfree(mc_dev); 766 } 767 768 /* 769 * Add a newly discovered fsl-mc device to be visible in Linux 770 */ 771 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, 772 struct fsl_mc_io *mc_io, 773 struct device *parent_dev, 774 struct fsl_mc_device **new_mc_dev) 775 { 776 int error; 777 struct fsl_mc_device *mc_dev = NULL; 778 struct fsl_mc_bus *mc_bus = NULL; 779 struct fsl_mc_device *parent_mc_dev; 780 781 if (dev_is_fsl_mc(parent_dev)) 782 parent_mc_dev = to_fsl_mc_device(parent_dev); 783 else 784 parent_mc_dev = NULL; 785 786 if (strcmp(obj_desc->type, "dprc") == 0) { 787 /* 788 * Allocate an MC bus device object: 789 */ 790 mc_bus = kzalloc_obj(*mc_bus); 791 if (!mc_bus) 792 return -ENOMEM; 793 794 mutex_init(&mc_bus->scan_mutex); 795 mc_dev = &mc_bus->mc_dev; 796 } else { 797 /* 798 * Allocate a regular fsl_mc_device object: 799 */ 800 mc_dev = kzalloc_obj(*mc_dev); 801 if (!mc_dev) 802 return -ENOMEM; 803 } 804 805 mc_dev->obj_desc = *obj_desc; 806 mc_dev->mc_io = mc_io; 807 device_initialize(&mc_dev->dev); 808 mc_dev->dev.parent = parent_dev; 809 mc_dev->dev.bus = &fsl_mc_bus_type; 810 mc_dev->dev.release = fsl_mc_device_release; 811 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type); 812 if (!mc_dev->dev.type) { 813 error = -ENODEV; 814 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type); 815 goto error_cleanup_dev; 816 } 817 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id); 818 819 if (strcmp(obj_desc->type, "dprc") == 0) { 820 struct fsl_mc_io *mc_io2; 821 822 mc_dev->flags |= FSL_MC_IS_DPRC; 823 824 /* 825 * To get the DPRC's ICID, we need to open the DPRC 826 * in get_dprc_icid(). For child DPRCs, we do so using the 827 * parent DPRC's MC portal instead of the child DPRC's MC 828 * portal, in case the child DPRC is already opened with 829 * its own portal (e.g., the DPRC used by AIOP). 830 * 831 * NOTE: There cannot be more than one active open for a 832 * given MC object, using the same MC portal. 833 */ 834 if (parent_mc_dev) { 835 /* 836 * device being added is a child DPRC device 837 */ 838 mc_io2 = parent_mc_dev->mc_io; 839 } else { 840 /* 841 * device being added is the root DPRC device 842 */ 843 if (!mc_io) { 844 error = -EINVAL; 845 goto error_cleanup_dev; 846 } 847 848 mc_io2 = mc_io; 849 } 850 851 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid); 852 if (error < 0) 853 goto error_cleanup_dev; 854 } else { 855 /* 856 * A non-DPRC object has to be a child of a DPRC, use the 857 * parent's ICID and interrupt domain. 858 */ 859 mc_dev->icid = parent_mc_dev->icid; 860 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK; 861 mc_dev->dev.dma_mask = &mc_dev->dma_mask; 862 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask; 863 dev_set_msi_domain(&mc_dev->dev, 864 dev_get_msi_domain(&parent_mc_dev->dev)); 865 } 866 867 /* 868 * Get MMIO regions for the device from the MC: 869 * 870 * NOTE: the root DPRC is a special case as its MMIO region is 871 * obtained from the device tree 872 */ 873 if (parent_mc_dev && obj_desc->region_count != 0) { 874 error = fsl_mc_device_get_mmio_regions(mc_dev, 875 parent_mc_dev); 876 if (error < 0) 877 goto error_cleanup_dev; 878 } 879 880 /* 881 * The device-specific probe callback will get invoked by device_add() 882 */ 883 error = device_add(&mc_dev->dev); 884 if (error < 0) { 885 dev_err(parent_dev, 886 "device_add() failed for device %s: %d\n", 887 dev_name(&mc_dev->dev), error); 888 goto error_cleanup_dev; 889 } 890 891 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev)); 892 893 *new_mc_dev = mc_dev; 894 return 0; 895 896 error_cleanup_dev: 897 put_device(&mc_dev->dev); 898 899 return error; 900 } 901 EXPORT_SYMBOL_GPL(fsl_mc_device_add); 902 903 static struct notifier_block fsl_mc_nb; 904 905 /** 906 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to 907 * Linux 908 * 909 * @mc_dev: Pointer to an fsl-mc device 910 */ 911 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev) 912 { 913 kfree(mc_dev->driver_override); 914 mc_dev->driver_override = NULL; 915 916 /* 917 * The device-specific remove callback will get invoked by device_del() 918 */ 919 device_del(&mc_dev->dev); 920 put_device(&mc_dev->dev); 921 } 922 EXPORT_SYMBOL_GPL(fsl_mc_device_remove); 923 924 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev, 925 u16 if_id) 926 { 927 struct fsl_mc_device *mc_bus_dev, *endpoint; 928 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }}; 929 struct dprc_endpoint endpoint1 = {{ 0 }}; 930 struct dprc_endpoint endpoint2 = {{ 0 }}; 931 struct fsl_mc_bus *mc_bus; 932 int state, err; 933 934 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent); 935 strcpy(endpoint1.type, mc_dev->obj_desc.type); 936 endpoint1.id = mc_dev->obj_desc.id; 937 endpoint1.if_id = if_id; 938 939 err = dprc_get_connection(mc_bus_dev->mc_io, 0, 940 mc_bus_dev->mc_handle, 941 &endpoint1, &endpoint2, 942 &state); 943 944 if (err == -ENOTCONN || state == -1) 945 return ERR_PTR(-ENOTCONN); 946 947 if (err < 0) { 948 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err); 949 return ERR_PTR(err); 950 } 951 952 strcpy(endpoint_desc.type, endpoint2.type); 953 endpoint_desc.id = endpoint2.id; 954 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev); 955 if (endpoint) 956 return endpoint; 957 958 /* 959 * We know that the device has an endpoint because we verified by 960 * interrogating the firmware. This is the case when the device was not 961 * yet discovered by the fsl-mc bus, thus the lookup returned NULL. 962 * Force a rescan of the devices in this container and retry the lookup. 963 */ 964 mc_bus = to_fsl_mc_bus(mc_bus_dev); 965 if (mutex_trylock(&mc_bus->scan_mutex)) { 966 err = dprc_scan_objects(mc_bus_dev, true); 967 mutex_unlock(&mc_bus->scan_mutex); 968 } 969 if (err < 0) 970 return ERR_PTR(err); 971 972 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev); 973 /* 974 * This means that the endpoint might reside in a different isolation 975 * context (DPRC/container). Not much to do, so return a permssion 976 * error. 977 */ 978 if (!endpoint) 979 return ERR_PTR(-EPERM); 980 981 return endpoint; 982 } 983 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint); 984 985 static int get_mc_addr_translation_ranges(struct device *dev, 986 struct fsl_mc_addr_translation_range 987 **ranges, 988 u8 *num_ranges) 989 { 990 struct fsl_mc_addr_translation_range *r; 991 struct of_range_parser parser; 992 struct of_range range; 993 994 of_range_parser_init(&parser, dev->of_node); 995 *num_ranges = of_range_count(&parser); 996 if (!*num_ranges) { 997 /* 998 * Missing or empty ranges property ("ranges;") for the 999 * 'fsl,qoriq-mc' node. In this case, identity mapping 1000 * will be used. 1001 */ 1002 *ranges = NULL; 1003 return 0; 1004 } 1005 1006 *ranges = devm_kcalloc(dev, *num_ranges, 1007 sizeof(struct fsl_mc_addr_translation_range), 1008 GFP_KERNEL); 1009 if (!(*ranges)) 1010 return -ENOMEM; 1011 1012 r = *ranges; 1013 for_each_of_range(&parser, &range) { 1014 r->mc_region_type = range.flags; 1015 r->start_mc_offset = range.bus_addr; 1016 r->end_mc_offset = range.bus_addr + range.size; 1017 r->start_phys_addr = range.cpu_addr; 1018 r++; 1019 } 1020 1021 return 0; 1022 } 1023 1024 /* 1025 * fsl_mc_bus_probe - callback invoked when the root MC bus is being 1026 * added 1027 */ 1028 static int fsl_mc_bus_probe(struct platform_device *pdev) 1029 { 1030 struct fsl_mc_obj_desc obj_desc; 1031 int error; 1032 struct fsl_mc *mc; 1033 struct fsl_mc_device *mc_bus_dev = NULL; 1034 struct fsl_mc_io *mc_io = NULL; 1035 int container_id; 1036 phys_addr_t mc_portal_phys_addr; 1037 u32 mc_portal_size, mc_stream_id; 1038 struct resource *plat_res; 1039 1040 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL); 1041 if (!mc) 1042 return -ENOMEM; 1043 1044 platform_set_drvdata(pdev, mc); 1045 1046 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1047 if (plat_res) { 1048 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res); 1049 if (IS_ERR(mc->fsl_mc_regs)) 1050 return PTR_ERR(mc->fsl_mc_regs); 1051 } 1052 1053 if (mc->fsl_mc_regs) { 1054 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) { 1055 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR); 1056 /* 1057 * HW ORs the PL and BMT bit, places the result in bit 1058 * 14 of the StreamID and ORs in the ICID. Calculate it 1059 * accordingly. 1060 */ 1061 mc_stream_id = (mc_stream_id & 0xffff) | 1062 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ? 1063 BIT(14) : 0); 1064 error = acpi_dma_configure_id(&pdev->dev, 1065 DEV_DMA_COHERENT, 1066 &mc_stream_id); 1067 if (error == -EPROBE_DEFER) 1068 return error; 1069 if (error) 1070 dev_warn(&pdev->dev, 1071 "failed to configure dma: %d.\n", 1072 error); 1073 } 1074 1075 /* 1076 * Some bootloaders pause the MC firmware before booting the 1077 * kernel so that MC will not cause faults as soon as the 1078 * SMMU probes due to the fact that there's no configuration 1079 * in place for MC. 1080 * At this point MC should have all its SMMU setup done so make 1081 * sure it is resumed. 1082 */ 1083 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) & 1084 (~(GCR1_P1_STOP | GCR1_P2_STOP)), 1085 mc->fsl_mc_regs + FSL_MC_GCR1); 1086 } 1087 1088 /* 1089 * Get physical address of MC portal for the root DPRC: 1090 */ 1091 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1092 if (!plat_res) 1093 return -EINVAL; 1094 1095 mc_portal_phys_addr = plat_res->start; 1096 mc_portal_size = resource_size(plat_res); 1097 mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff; 1098 1099 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr, 1100 mc_portal_size, NULL, 1101 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io); 1102 if (error < 0) 1103 return error; 1104 1105 error = mc_get_version(mc_io, 0, &mc_version); 1106 if (error != 0) { 1107 dev_err(&pdev->dev, 1108 "mc_get_version() failed with error %d\n", error); 1109 goto error_cleanup_mc_io; 1110 } 1111 1112 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n", 1113 mc_version.major, mc_version.minor, mc_version.revision); 1114 1115 if (dev_of_node(&pdev->dev)) { 1116 error = get_mc_addr_translation_ranges(&pdev->dev, 1117 &mc->translation_ranges, 1118 &mc->num_translation_ranges); 1119 if (error < 0) 1120 goto error_cleanup_mc_io; 1121 } 1122 1123 error = dprc_get_container_id(mc_io, 0, &container_id); 1124 if (error < 0) { 1125 dev_err(&pdev->dev, 1126 "dprc_get_container_id() failed: %d\n", error); 1127 goto error_cleanup_mc_io; 1128 } 1129 1130 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc)); 1131 error = dprc_get_api_version(mc_io, 0, 1132 &obj_desc.ver_major, 1133 &obj_desc.ver_minor); 1134 if (error < 0) 1135 goto error_cleanup_mc_io; 1136 1137 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE; 1138 strcpy(obj_desc.type, "dprc"); 1139 obj_desc.id = container_id; 1140 obj_desc.irq_count = 1; 1141 obj_desc.region_count = 0; 1142 1143 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev); 1144 if (error < 0) 1145 goto error_cleanup_mc_io; 1146 1147 mc->root_mc_bus_dev = mc_bus_dev; 1148 mc_bus_dev->dev.fwnode = pdev->dev.fwnode; 1149 return 0; 1150 1151 error_cleanup_mc_io: 1152 fsl_destroy_mc_io(mc_io); 1153 return error; 1154 } 1155 1156 /* 1157 * fsl_mc_bus_remove - callback invoked when the root MC bus is being 1158 * removed 1159 */ 1160 static void fsl_mc_bus_remove(struct platform_device *pdev) 1161 { 1162 struct fsl_mc *mc = platform_get_drvdata(pdev); 1163 struct fsl_mc_io *mc_io; 1164 1165 mc_io = mc->root_mc_bus_dev->mc_io; 1166 fsl_mc_device_remove(mc->root_mc_bus_dev); 1167 fsl_destroy_mc_io(mc_io); 1168 1169 bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb); 1170 1171 if (mc->fsl_mc_regs) { 1172 /* 1173 * Pause the MC firmware so that it doesn't crash in certain 1174 * scenarios, such as kexec. 1175 */ 1176 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) | 1177 (GCR1_P1_STOP | GCR1_P2_STOP), 1178 mc->fsl_mc_regs + FSL_MC_GCR1); 1179 } 1180 } 1181 1182 static const struct of_device_id fsl_mc_bus_match_table[] = { 1183 {.compatible = "fsl,qoriq-mc",}, 1184 {}, 1185 }; 1186 1187 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table); 1188 1189 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = { 1190 {"NXP0008", 0 }, 1191 { } 1192 }; 1193 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table); 1194 1195 static struct platform_driver fsl_mc_bus_driver = { 1196 .driver = { 1197 .name = "fsl_mc_bus", 1198 .pm = NULL, 1199 .of_match_table = fsl_mc_bus_match_table, 1200 .acpi_match_table = fsl_mc_bus_acpi_match_table, 1201 }, 1202 .probe = fsl_mc_bus_probe, 1203 .remove = fsl_mc_bus_remove, 1204 .shutdown = fsl_mc_bus_remove, 1205 }; 1206 1207 static int fsl_mc_bus_notifier(struct notifier_block *nb, 1208 unsigned long action, void *data) 1209 { 1210 struct device *dev = data; 1211 struct resource *res; 1212 void __iomem *fsl_mc_regs; 1213 1214 if (action != BUS_NOTIFY_ADD_DEVICE) 1215 return 0; 1216 1217 if (!of_match_device(fsl_mc_bus_match_table, dev) && 1218 !acpi_match_device(fsl_mc_bus_acpi_match_table, dev)) 1219 return 0; 1220 1221 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1); 1222 if (!res) 1223 return 0; 1224 1225 fsl_mc_regs = ioremap(res->start, resource_size(res)); 1226 if (!fsl_mc_regs) 1227 return 0; 1228 1229 /* 1230 * Make sure that the MC firmware is paused before the IOMMU setup for 1231 * it is done or otherwise the firmware will crash right after the SMMU 1232 * gets probed and enabled. 1233 */ 1234 writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP), 1235 fsl_mc_regs + FSL_MC_GCR1); 1236 iounmap(fsl_mc_regs); 1237 1238 return 0; 1239 } 1240 1241 static struct notifier_block fsl_mc_nb = { 1242 .notifier_call = fsl_mc_bus_notifier, 1243 }; 1244 1245 static int __init fsl_mc_bus_driver_init(void) 1246 { 1247 int error; 1248 1249 error = bus_register(&fsl_mc_bus_type); 1250 if (error < 0) { 1251 pr_err("bus type registration failed: %d\n", error); 1252 goto error_cleanup_cache; 1253 } 1254 1255 error = platform_driver_register(&fsl_mc_bus_driver); 1256 if (error < 0) { 1257 pr_err("platform_driver_register() failed: %d\n", error); 1258 goto error_cleanup_bus; 1259 } 1260 1261 error = dprc_driver_init(); 1262 if (error < 0) 1263 goto error_cleanup_driver; 1264 1265 error = fsl_mc_allocator_driver_init(); 1266 if (error < 0) 1267 goto error_cleanup_dprc_driver; 1268 1269 return bus_register_notifier(&platform_bus_type, &fsl_mc_nb); 1270 1271 error_cleanup_dprc_driver: 1272 dprc_driver_exit(); 1273 1274 error_cleanup_driver: 1275 platform_driver_unregister(&fsl_mc_bus_driver); 1276 1277 error_cleanup_bus: 1278 bus_unregister(&fsl_mc_bus_type); 1279 1280 error_cleanup_cache: 1281 return error; 1282 } 1283 postcore_initcall(fsl_mc_bus_driver_init); 1284