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 * Author: German Rivera <German.Rivera@freescale.com> 7 * 8 */ 9 10 #define pr_fmt(fmt) "fsl-mc: " fmt 11 12 #include <linux/module.h> 13 #include <linux/of_device.h> 14 #include <linux/of_address.h> 15 #include <linux/ioport.h> 16 #include <linux/slab.h> 17 #include <linux/limits.h> 18 #include <linux/bitops.h> 19 #include <linux/msi.h> 20 #include <linux/dma-mapping.h> 21 22 #include "fsl-mc-private.h" 23 24 /** 25 * Default DMA mask for devices on a fsl-mc bus 26 */ 27 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL) 28 29 /** 30 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device 31 * @root_mc_bus_dev: fsl-mc device representing the root DPRC 32 * @num_translation_ranges: number of entries in addr_translation_ranges 33 * @translation_ranges: array of bus to system address translation ranges 34 */ 35 struct fsl_mc { 36 struct fsl_mc_device *root_mc_bus_dev; 37 u8 num_translation_ranges; 38 struct fsl_mc_addr_translation_range *translation_ranges; 39 }; 40 41 /** 42 * struct fsl_mc_addr_translation_range - bus to system address translation 43 * range 44 * @mc_region_type: Type of MC region for the range being translated 45 * @start_mc_offset: Start MC offset of the range being translated 46 * @end_mc_offset: MC offset of the first byte after the range (last MC 47 * offset of the range is end_mc_offset - 1) 48 * @start_phys_addr: system physical address corresponding to start_mc_addr 49 */ 50 struct fsl_mc_addr_translation_range { 51 enum dprc_region_type mc_region_type; 52 u64 start_mc_offset; 53 u64 end_mc_offset; 54 phys_addr_t start_phys_addr; 55 }; 56 57 /** 58 * struct mc_version 59 * @major: Major version number: incremented on API compatibility changes 60 * @minor: Minor version number: incremented on API additions (that are 61 * backward compatible); reset when major version is incremented 62 * @revision: Internal revision number: incremented on implementation changes 63 * and/or bug fixes that have no impact on API 64 */ 65 struct mc_version { 66 u32 major; 67 u32 minor; 68 u32 revision; 69 }; 70 71 /** 72 * fsl_mc_bus_match - device to driver matching callback 73 * @dev: the fsl-mc device to match against 74 * @drv: the device driver to search for matching fsl-mc object type 75 * structures 76 * 77 * Returns 1 on success, 0 otherwise. 78 */ 79 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv) 80 { 81 const struct fsl_mc_device_id *id; 82 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 83 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv); 84 bool found = false; 85 86 if (!mc_drv->match_id_table) 87 goto out; 88 89 /* 90 * If the object is not 'plugged' don't match. 91 * Only exception is the root DPRC, which is a special case. 92 */ 93 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 && 94 !fsl_mc_is_root_dprc(&mc_dev->dev)) 95 goto out; 96 97 /* 98 * Traverse the match_id table of the given driver, trying to find 99 * a matching for the given device. 100 */ 101 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) { 102 if (id->vendor == mc_dev->obj_desc.vendor && 103 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) { 104 found = true; 105 106 break; 107 } 108 } 109 110 out: 111 dev_dbg(dev, "%smatched\n", found ? "" : "not "); 112 return found; 113 } 114 115 /** 116 * fsl_mc_bus_uevent - callback invoked when a device is added 117 */ 118 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 119 { 120 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 121 122 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s", 123 mc_dev->obj_desc.vendor, 124 mc_dev->obj_desc.type)) 125 return -ENOMEM; 126 127 return 0; 128 } 129 130 static int fsl_mc_dma_configure(struct device *dev) 131 { 132 struct device *dma_dev = dev; 133 134 while (dev_is_fsl_mc(dma_dev)) 135 dma_dev = dma_dev->parent; 136 137 return of_dma_configure(dev, dma_dev->of_node, 0); 138 } 139 140 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 141 char *buf) 142 { 143 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 144 145 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor, 146 mc_dev->obj_desc.type); 147 } 148 static DEVICE_ATTR_RO(modalias); 149 150 static struct attribute *fsl_mc_dev_attrs[] = { 151 &dev_attr_modalias.attr, 152 NULL, 153 }; 154 155 ATTRIBUTE_GROUPS(fsl_mc_dev); 156 157 struct bus_type fsl_mc_bus_type = { 158 .name = "fsl-mc", 159 .match = fsl_mc_bus_match, 160 .uevent = fsl_mc_bus_uevent, 161 .dma_configure = fsl_mc_dma_configure, 162 .dev_groups = fsl_mc_dev_groups, 163 }; 164 EXPORT_SYMBOL_GPL(fsl_mc_bus_type); 165 166 struct device_type fsl_mc_bus_dprc_type = { 167 .name = "fsl_mc_bus_dprc" 168 }; 169 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type); 170 171 struct device_type fsl_mc_bus_dpni_type = { 172 .name = "fsl_mc_bus_dpni" 173 }; 174 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type); 175 176 struct device_type fsl_mc_bus_dpio_type = { 177 .name = "fsl_mc_bus_dpio" 178 }; 179 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type); 180 181 struct device_type fsl_mc_bus_dpsw_type = { 182 .name = "fsl_mc_bus_dpsw" 183 }; 184 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type); 185 186 struct device_type fsl_mc_bus_dpbp_type = { 187 .name = "fsl_mc_bus_dpbp" 188 }; 189 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type); 190 191 struct device_type fsl_mc_bus_dpcon_type = { 192 .name = "fsl_mc_bus_dpcon" 193 }; 194 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type); 195 196 struct device_type fsl_mc_bus_dpmcp_type = { 197 .name = "fsl_mc_bus_dpmcp" 198 }; 199 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type); 200 201 struct device_type fsl_mc_bus_dpmac_type = { 202 .name = "fsl_mc_bus_dpmac" 203 }; 204 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type); 205 206 struct device_type fsl_mc_bus_dprtc_type = { 207 .name = "fsl_mc_bus_dprtc" 208 }; 209 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type); 210 211 struct device_type fsl_mc_bus_dpseci_type = { 212 .name = "fsl_mc_bus_dpseci" 213 }; 214 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type); 215 216 static struct device_type *fsl_mc_get_device_type(const char *type) 217 { 218 static const struct { 219 struct device_type *dev_type; 220 const char *type; 221 } dev_types[] = { 222 { &fsl_mc_bus_dprc_type, "dprc" }, 223 { &fsl_mc_bus_dpni_type, "dpni" }, 224 { &fsl_mc_bus_dpio_type, "dpio" }, 225 { &fsl_mc_bus_dpsw_type, "dpsw" }, 226 { &fsl_mc_bus_dpbp_type, "dpbp" }, 227 { &fsl_mc_bus_dpcon_type, "dpcon" }, 228 { &fsl_mc_bus_dpmcp_type, "dpmcp" }, 229 { &fsl_mc_bus_dpmac_type, "dpmac" }, 230 { &fsl_mc_bus_dprtc_type, "dprtc" }, 231 { &fsl_mc_bus_dpseci_type, "dpseci" }, 232 { NULL, NULL } 233 }; 234 int i; 235 236 for (i = 0; dev_types[i].dev_type; i++) 237 if (!strcmp(dev_types[i].type, type)) 238 return dev_types[i].dev_type; 239 240 return NULL; 241 } 242 243 static int fsl_mc_driver_probe(struct device *dev) 244 { 245 struct fsl_mc_driver *mc_drv; 246 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 247 int error; 248 249 mc_drv = to_fsl_mc_driver(dev->driver); 250 251 error = mc_drv->probe(mc_dev); 252 if (error < 0) { 253 if (error != -EPROBE_DEFER) 254 dev_err(dev, "%s failed: %d\n", __func__, error); 255 return error; 256 } 257 258 return 0; 259 } 260 261 static int fsl_mc_driver_remove(struct device *dev) 262 { 263 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 264 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 265 int error; 266 267 error = mc_drv->remove(mc_dev); 268 if (error < 0) { 269 dev_err(dev, "%s failed: %d\n", __func__, error); 270 return error; 271 } 272 273 return 0; 274 } 275 276 static void fsl_mc_driver_shutdown(struct device *dev) 277 { 278 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver); 279 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 280 281 mc_drv->shutdown(mc_dev); 282 } 283 284 /** 285 * __fsl_mc_driver_register - registers a child device driver with the 286 * MC bus 287 * 288 * This function is implicitly invoked from the registration function of 289 * fsl_mc device drivers, which is generated by the 290 * module_fsl_mc_driver() macro. 291 */ 292 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver, 293 struct module *owner) 294 { 295 int error; 296 297 mc_driver->driver.owner = owner; 298 mc_driver->driver.bus = &fsl_mc_bus_type; 299 300 if (mc_driver->probe) 301 mc_driver->driver.probe = fsl_mc_driver_probe; 302 303 if (mc_driver->remove) 304 mc_driver->driver.remove = fsl_mc_driver_remove; 305 306 if (mc_driver->shutdown) 307 mc_driver->driver.shutdown = fsl_mc_driver_shutdown; 308 309 error = driver_register(&mc_driver->driver); 310 if (error < 0) { 311 pr_err("driver_register() failed for %s: %d\n", 312 mc_driver->driver.name, error); 313 return error; 314 } 315 316 return 0; 317 } 318 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register); 319 320 /** 321 * fsl_mc_driver_unregister - unregisters a device driver from the 322 * MC bus 323 */ 324 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver) 325 { 326 driver_unregister(&mc_driver->driver); 327 } 328 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister); 329 330 /** 331 * mc_get_version() - Retrieves the Management Complex firmware 332 * version information 333 * @mc_io: Pointer to opaque I/O object 334 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_' 335 * @mc_ver_info: Returned version information structure 336 * 337 * Return: '0' on Success; Error code otherwise. 338 */ 339 static int mc_get_version(struct fsl_mc_io *mc_io, 340 u32 cmd_flags, 341 struct mc_version *mc_ver_info) 342 { 343 struct fsl_mc_command cmd = { 0 }; 344 struct dpmng_rsp_get_version *rsp_params; 345 int err; 346 347 /* prepare command */ 348 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION, 349 cmd_flags, 350 0); 351 352 /* send command to mc*/ 353 err = mc_send_command(mc_io, &cmd); 354 if (err) 355 return err; 356 357 /* retrieve response parameters */ 358 rsp_params = (struct dpmng_rsp_get_version *)cmd.params; 359 mc_ver_info->revision = le32_to_cpu(rsp_params->revision); 360 mc_ver_info->major = le32_to_cpu(rsp_params->version_major); 361 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor); 362 363 return 0; 364 } 365 366 /** 367 * fsl_mc_get_root_dprc - function to traverse to the root dprc 368 */ 369 static void fsl_mc_get_root_dprc(struct device *dev, 370 struct device **root_dprc_dev) 371 { 372 if (!dev) { 373 *root_dprc_dev = NULL; 374 } else if (!dev_is_fsl_mc(dev)) { 375 *root_dprc_dev = NULL; 376 } else { 377 *root_dprc_dev = dev; 378 while (dev_is_fsl_mc((*root_dprc_dev)->parent)) 379 *root_dprc_dev = (*root_dprc_dev)->parent; 380 } 381 } 382 383 static int get_dprc_attr(struct fsl_mc_io *mc_io, 384 int container_id, struct dprc_attributes *attr) 385 { 386 u16 dprc_handle; 387 int error; 388 389 error = dprc_open(mc_io, 0, container_id, &dprc_handle); 390 if (error < 0) { 391 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error); 392 return error; 393 } 394 395 memset(attr, 0, sizeof(struct dprc_attributes)); 396 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr); 397 if (error < 0) { 398 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n", 399 error); 400 goto common_cleanup; 401 } 402 403 error = 0; 404 405 common_cleanup: 406 (void)dprc_close(mc_io, 0, dprc_handle); 407 return error; 408 } 409 410 static int get_dprc_icid(struct fsl_mc_io *mc_io, 411 int container_id, u16 *icid) 412 { 413 struct dprc_attributes attr; 414 int error; 415 416 error = get_dprc_attr(mc_io, container_id, &attr); 417 if (error == 0) 418 *icid = attr.icid; 419 420 return error; 421 } 422 423 static int translate_mc_addr(struct fsl_mc_device *mc_dev, 424 enum dprc_region_type mc_region_type, 425 u64 mc_offset, phys_addr_t *phys_addr) 426 { 427 int i; 428 struct device *root_dprc_dev; 429 struct fsl_mc *mc; 430 431 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev); 432 mc = dev_get_drvdata(root_dprc_dev->parent); 433 434 if (mc->num_translation_ranges == 0) { 435 /* 436 * Do identity mapping: 437 */ 438 *phys_addr = mc_offset; 439 return 0; 440 } 441 442 for (i = 0; i < mc->num_translation_ranges; i++) { 443 struct fsl_mc_addr_translation_range *range = 444 &mc->translation_ranges[i]; 445 446 if (mc_region_type == range->mc_region_type && 447 mc_offset >= range->start_mc_offset && 448 mc_offset < range->end_mc_offset) { 449 *phys_addr = range->start_phys_addr + 450 (mc_offset - range->start_mc_offset); 451 return 0; 452 } 453 } 454 455 return -EFAULT; 456 } 457 458 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev, 459 struct fsl_mc_device *mc_bus_dev) 460 { 461 int i; 462 int error; 463 struct resource *regions; 464 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc; 465 struct device *parent_dev = mc_dev->dev.parent; 466 enum dprc_region_type mc_region_type; 467 468 if (is_fsl_mc_bus_dprc(mc_dev) || 469 is_fsl_mc_bus_dpmcp(mc_dev)) { 470 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL; 471 } else if (is_fsl_mc_bus_dpio(mc_dev)) { 472 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL; 473 } else { 474 /* 475 * This function should not have been called for this MC object 476 * type, as this object type is not supposed to have MMIO 477 * regions 478 */ 479 return -EINVAL; 480 } 481 482 regions = kmalloc_array(obj_desc->region_count, 483 sizeof(regions[0]), GFP_KERNEL); 484 if (!regions) 485 return -ENOMEM; 486 487 for (i = 0; i < obj_desc->region_count; i++) { 488 struct dprc_region_desc region_desc; 489 490 error = dprc_get_obj_region(mc_bus_dev->mc_io, 491 0, 492 mc_bus_dev->mc_handle, 493 obj_desc->type, 494 obj_desc->id, i, ®ion_desc); 495 if (error < 0) { 496 dev_err(parent_dev, 497 "dprc_get_obj_region() failed: %d\n", error); 498 goto error_cleanup_regions; 499 } 500 /* 501 * Older MC only returned region offset and no base address 502 * If base address is in the region_desc use it otherwise 503 * revert to old mechanism 504 */ 505 if (region_desc.base_address) 506 regions[i].start = region_desc.base_address + 507 region_desc.base_offset; 508 else 509 error = translate_mc_addr(mc_dev, mc_region_type, 510 region_desc.base_offset, 511 ®ions[i].start); 512 513 if (error < 0) { 514 dev_err(parent_dev, 515 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n", 516 region_desc.base_offset, 517 obj_desc->type, obj_desc->id, i); 518 goto error_cleanup_regions; 519 } 520 521 regions[i].end = regions[i].start + region_desc.size - 1; 522 regions[i].name = "fsl-mc object MMIO region"; 523 regions[i].flags = IORESOURCE_IO; 524 if (region_desc.flags & DPRC_REGION_CACHEABLE) 525 regions[i].flags |= IORESOURCE_CACHEABLE; 526 if (region_desc.flags & DPRC_REGION_SHAREABLE) 527 regions[i].flags |= IORESOURCE_MEM; 528 } 529 530 mc_dev->regions = regions; 531 return 0; 532 533 error_cleanup_regions: 534 kfree(regions); 535 return error; 536 } 537 538 /** 539 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc 540 */ 541 bool fsl_mc_is_root_dprc(struct device *dev) 542 { 543 struct device *root_dprc_dev; 544 545 fsl_mc_get_root_dprc(dev, &root_dprc_dev); 546 if (!root_dprc_dev) 547 return false; 548 return dev == root_dprc_dev; 549 } 550 551 static void fsl_mc_device_release(struct device *dev) 552 { 553 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev); 554 555 kfree(mc_dev->regions); 556 557 if (is_fsl_mc_bus_dprc(mc_dev)) 558 kfree(to_fsl_mc_bus(mc_dev)); 559 else 560 kfree(mc_dev); 561 } 562 563 /** 564 * Add a newly discovered fsl-mc device to be visible in Linux 565 */ 566 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc, 567 struct fsl_mc_io *mc_io, 568 struct device *parent_dev, 569 struct fsl_mc_device **new_mc_dev) 570 { 571 int error; 572 struct fsl_mc_device *mc_dev = NULL; 573 struct fsl_mc_bus *mc_bus = NULL; 574 struct fsl_mc_device *parent_mc_dev; 575 576 if (dev_is_fsl_mc(parent_dev)) 577 parent_mc_dev = to_fsl_mc_device(parent_dev); 578 else 579 parent_mc_dev = NULL; 580 581 if (strcmp(obj_desc->type, "dprc") == 0) { 582 /* 583 * Allocate an MC bus device object: 584 */ 585 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL); 586 if (!mc_bus) 587 return -ENOMEM; 588 589 mc_dev = &mc_bus->mc_dev; 590 } else { 591 /* 592 * Allocate a regular fsl_mc_device object: 593 */ 594 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL); 595 if (!mc_dev) 596 return -ENOMEM; 597 } 598 599 mc_dev->obj_desc = *obj_desc; 600 mc_dev->mc_io = mc_io; 601 device_initialize(&mc_dev->dev); 602 mc_dev->dev.parent = parent_dev; 603 mc_dev->dev.bus = &fsl_mc_bus_type; 604 mc_dev->dev.release = fsl_mc_device_release; 605 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type); 606 if (!mc_dev->dev.type) { 607 error = -ENODEV; 608 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type); 609 goto error_cleanup_dev; 610 } 611 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id); 612 613 if (strcmp(obj_desc->type, "dprc") == 0) { 614 struct fsl_mc_io *mc_io2; 615 616 mc_dev->flags |= FSL_MC_IS_DPRC; 617 618 /* 619 * To get the DPRC's ICID, we need to open the DPRC 620 * in get_dprc_icid(). For child DPRCs, we do so using the 621 * parent DPRC's MC portal instead of the child DPRC's MC 622 * portal, in case the child DPRC is already opened with 623 * its own portal (e.g., the DPRC used by AIOP). 624 * 625 * NOTE: There cannot be more than one active open for a 626 * given MC object, using the same MC portal. 627 */ 628 if (parent_mc_dev) { 629 /* 630 * device being added is a child DPRC device 631 */ 632 mc_io2 = parent_mc_dev->mc_io; 633 } else { 634 /* 635 * device being added is the root DPRC device 636 */ 637 if (!mc_io) { 638 error = -EINVAL; 639 goto error_cleanup_dev; 640 } 641 642 mc_io2 = mc_io; 643 } 644 645 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid); 646 if (error < 0) 647 goto error_cleanup_dev; 648 } else { 649 /* 650 * A non-DPRC object has to be a child of a DPRC, use the 651 * parent's ICID and interrupt domain. 652 */ 653 mc_dev->icid = parent_mc_dev->icid; 654 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK; 655 mc_dev->dev.dma_mask = &mc_dev->dma_mask; 656 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask; 657 dev_set_msi_domain(&mc_dev->dev, 658 dev_get_msi_domain(&parent_mc_dev->dev)); 659 } 660 661 /* 662 * Get MMIO regions for the device from the MC: 663 * 664 * NOTE: the root DPRC is a special case as its MMIO region is 665 * obtained from the device tree 666 */ 667 if (parent_mc_dev && obj_desc->region_count != 0) { 668 error = fsl_mc_device_get_mmio_regions(mc_dev, 669 parent_mc_dev); 670 if (error < 0) 671 goto error_cleanup_dev; 672 } 673 674 /* 675 * The device-specific probe callback will get invoked by device_add() 676 */ 677 error = device_add(&mc_dev->dev); 678 if (error < 0) { 679 dev_err(parent_dev, 680 "device_add() failed for device %s: %d\n", 681 dev_name(&mc_dev->dev), error); 682 goto error_cleanup_dev; 683 } 684 685 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev)); 686 687 *new_mc_dev = mc_dev; 688 return 0; 689 690 error_cleanup_dev: 691 kfree(mc_dev->regions); 692 kfree(mc_bus); 693 kfree(mc_dev); 694 695 return error; 696 } 697 EXPORT_SYMBOL_GPL(fsl_mc_device_add); 698 699 /** 700 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to 701 * Linux 702 * 703 * @mc_dev: Pointer to an fsl-mc device 704 */ 705 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev) 706 { 707 /* 708 * The device-specific remove callback will get invoked by device_del() 709 */ 710 device_del(&mc_dev->dev); 711 put_device(&mc_dev->dev); 712 } 713 EXPORT_SYMBOL_GPL(fsl_mc_device_remove); 714 715 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev) 716 { 717 struct fsl_mc_device *mc_bus_dev, *endpoint; 718 struct fsl_mc_obj_desc endpoint_desc = { 0 }; 719 struct dprc_endpoint endpoint1 = { 0 }; 720 struct dprc_endpoint endpoint2 = { 0 }; 721 int state, err; 722 723 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent); 724 strcpy(endpoint1.type, mc_dev->obj_desc.type); 725 endpoint1.id = mc_dev->obj_desc.id; 726 727 err = dprc_get_connection(mc_bus_dev->mc_io, 0, 728 mc_bus_dev->mc_handle, 729 &endpoint1, &endpoint2, 730 &state); 731 732 if (err == -ENOTCONN || state == -1) 733 return ERR_PTR(-ENOTCONN); 734 735 if (err < 0) { 736 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err); 737 return ERR_PTR(err); 738 } 739 740 strcpy(endpoint_desc.type, endpoint2.type); 741 endpoint_desc.id = endpoint2.id; 742 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev); 743 744 return endpoint; 745 } 746 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint); 747 748 static int parse_mc_ranges(struct device *dev, 749 int *paddr_cells, 750 int *mc_addr_cells, 751 int *mc_size_cells, 752 const __be32 **ranges_start) 753 { 754 const __be32 *prop; 755 int range_tuple_cell_count; 756 int ranges_len; 757 int tuple_len; 758 struct device_node *mc_node = dev->of_node; 759 760 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len); 761 if (!(*ranges_start) || !ranges_len) { 762 dev_warn(dev, 763 "missing or empty ranges property for device tree node '%pOFn'\n", 764 mc_node); 765 return 0; 766 } 767 768 *paddr_cells = of_n_addr_cells(mc_node); 769 770 prop = of_get_property(mc_node, "#address-cells", NULL); 771 if (prop) 772 *mc_addr_cells = be32_to_cpup(prop); 773 else 774 *mc_addr_cells = *paddr_cells; 775 776 prop = of_get_property(mc_node, "#size-cells", NULL); 777 if (prop) 778 *mc_size_cells = be32_to_cpup(prop); 779 else 780 *mc_size_cells = of_n_size_cells(mc_node); 781 782 range_tuple_cell_count = *paddr_cells + *mc_addr_cells + 783 *mc_size_cells; 784 785 tuple_len = range_tuple_cell_count * sizeof(__be32); 786 if (ranges_len % tuple_len != 0) { 787 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node); 788 return -EINVAL; 789 } 790 791 return ranges_len / tuple_len; 792 } 793 794 static int get_mc_addr_translation_ranges(struct device *dev, 795 struct fsl_mc_addr_translation_range 796 **ranges, 797 u8 *num_ranges) 798 { 799 int ret; 800 int paddr_cells; 801 int mc_addr_cells; 802 int mc_size_cells; 803 int i; 804 const __be32 *ranges_start; 805 const __be32 *cell; 806 807 ret = parse_mc_ranges(dev, 808 &paddr_cells, 809 &mc_addr_cells, 810 &mc_size_cells, 811 &ranges_start); 812 if (ret < 0) 813 return ret; 814 815 *num_ranges = ret; 816 if (!ret) { 817 /* 818 * Missing or empty ranges property ("ranges;") for the 819 * 'fsl,qoriq-mc' node. In this case, identity mapping 820 * will be used. 821 */ 822 *ranges = NULL; 823 return 0; 824 } 825 826 *ranges = devm_kcalloc(dev, *num_ranges, 827 sizeof(struct fsl_mc_addr_translation_range), 828 GFP_KERNEL); 829 if (!(*ranges)) 830 return -ENOMEM; 831 832 cell = ranges_start; 833 for (i = 0; i < *num_ranges; ++i) { 834 struct fsl_mc_addr_translation_range *range = &(*ranges)[i]; 835 836 range->mc_region_type = of_read_number(cell, 1); 837 range->start_mc_offset = of_read_number(cell + 1, 838 mc_addr_cells - 1); 839 cell += mc_addr_cells; 840 range->start_phys_addr = of_read_number(cell, paddr_cells); 841 cell += paddr_cells; 842 range->end_mc_offset = range->start_mc_offset + 843 of_read_number(cell, mc_size_cells); 844 845 cell += mc_size_cells; 846 } 847 848 return 0; 849 } 850 851 /** 852 * fsl_mc_bus_probe - callback invoked when the root MC bus is being 853 * added 854 */ 855 static int fsl_mc_bus_probe(struct platform_device *pdev) 856 { 857 struct fsl_mc_obj_desc obj_desc; 858 int error; 859 struct fsl_mc *mc; 860 struct fsl_mc_device *mc_bus_dev = NULL; 861 struct fsl_mc_io *mc_io = NULL; 862 int container_id; 863 phys_addr_t mc_portal_phys_addr; 864 u32 mc_portal_size; 865 struct mc_version mc_version; 866 struct resource res; 867 868 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL); 869 if (!mc) 870 return -ENOMEM; 871 872 platform_set_drvdata(pdev, mc); 873 874 /* 875 * Get physical address of MC portal for the root DPRC: 876 */ 877 error = of_address_to_resource(pdev->dev.of_node, 0, &res); 878 if (error < 0) { 879 dev_err(&pdev->dev, 880 "of_address_to_resource() failed for %pOF\n", 881 pdev->dev.of_node); 882 return error; 883 } 884 885 mc_portal_phys_addr = res.start; 886 mc_portal_size = resource_size(&res); 887 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr, 888 mc_portal_size, NULL, 889 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io); 890 if (error < 0) 891 return error; 892 893 error = mc_get_version(mc_io, 0, &mc_version); 894 if (error != 0) { 895 dev_err(&pdev->dev, 896 "mc_get_version() failed with error %d\n", error); 897 goto error_cleanup_mc_io; 898 } 899 900 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n", 901 mc_version.major, mc_version.minor, mc_version.revision); 902 903 error = get_mc_addr_translation_ranges(&pdev->dev, 904 &mc->translation_ranges, 905 &mc->num_translation_ranges); 906 if (error < 0) 907 goto error_cleanup_mc_io; 908 909 error = dprc_get_container_id(mc_io, 0, &container_id); 910 if (error < 0) { 911 dev_err(&pdev->dev, 912 "dprc_get_container_id() failed: %d\n", error); 913 goto error_cleanup_mc_io; 914 } 915 916 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc)); 917 error = dprc_get_api_version(mc_io, 0, 918 &obj_desc.ver_major, 919 &obj_desc.ver_minor); 920 if (error < 0) 921 goto error_cleanup_mc_io; 922 923 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE; 924 strcpy(obj_desc.type, "dprc"); 925 obj_desc.id = container_id; 926 obj_desc.irq_count = 1; 927 obj_desc.region_count = 0; 928 929 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev); 930 if (error < 0) 931 goto error_cleanup_mc_io; 932 933 mc->root_mc_bus_dev = mc_bus_dev; 934 return 0; 935 936 error_cleanup_mc_io: 937 fsl_destroy_mc_io(mc_io); 938 return error; 939 } 940 941 /** 942 * fsl_mc_bus_remove - callback invoked when the root MC bus is being 943 * removed 944 */ 945 static int fsl_mc_bus_remove(struct platform_device *pdev) 946 { 947 struct fsl_mc *mc = platform_get_drvdata(pdev); 948 949 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev)) 950 return -EINVAL; 951 952 fsl_mc_device_remove(mc->root_mc_bus_dev); 953 954 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io); 955 mc->root_mc_bus_dev->mc_io = NULL; 956 957 return 0; 958 } 959 960 static const struct of_device_id fsl_mc_bus_match_table[] = { 961 {.compatible = "fsl,qoriq-mc",}, 962 {}, 963 }; 964 965 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table); 966 967 static struct platform_driver fsl_mc_bus_driver = { 968 .driver = { 969 .name = "fsl_mc_bus", 970 .pm = NULL, 971 .of_match_table = fsl_mc_bus_match_table, 972 }, 973 .probe = fsl_mc_bus_probe, 974 .remove = fsl_mc_bus_remove, 975 }; 976 977 static int __init fsl_mc_bus_driver_init(void) 978 { 979 int error; 980 981 error = bus_register(&fsl_mc_bus_type); 982 if (error < 0) { 983 pr_err("bus type registration failed: %d\n", error); 984 goto error_cleanup_cache; 985 } 986 987 error = platform_driver_register(&fsl_mc_bus_driver); 988 if (error < 0) { 989 pr_err("platform_driver_register() failed: %d\n", error); 990 goto error_cleanup_bus; 991 } 992 993 error = dprc_driver_init(); 994 if (error < 0) 995 goto error_cleanup_driver; 996 997 error = fsl_mc_allocator_driver_init(); 998 if (error < 0) 999 goto error_cleanup_dprc_driver; 1000 1001 return 0; 1002 1003 error_cleanup_dprc_driver: 1004 dprc_driver_exit(); 1005 1006 error_cleanup_driver: 1007 platform_driver_unregister(&fsl_mc_bus_driver); 1008 1009 error_cleanup_bus: 1010 bus_unregister(&fsl_mc_bus_type); 1011 1012 error_cleanup_cache: 1013 return error; 1014 } 1015 postcore_initcall(fsl_mc_bus_driver_init); 1016