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