2 * Freescale Management Complex (MC) bus driver
4 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
5 * Author: German Rivera <German.Rivera@freescale.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #define pr_fmt(fmt) "fsl-mc: " fmt
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/of_address.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/limits.h>
20 #include <linux/bitops.h>
21 #include <linux/msi.h>
22 #include <linux/dma-mapping.h>
23 #include "../include/mc-bus.h"
24 #include "../include/dpmng.h"
25 #include "../include/mc-sys.h"
27 #include "fsl-mc-private.h"
30 static struct kmem_cache *mc_dev_cache;
33 * Default DMA mask for devices on a fsl-mc bus
35 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
38 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
39 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
40 * @num_translation_ranges: number of entries in addr_translation_ranges
41 * @translation_ranges: array of bus to system address translation ranges
44 struct fsl_mc_device *root_mc_bus_dev;
45 u8 num_translation_ranges;
46 struct fsl_mc_addr_translation_range *translation_ranges;
50 * struct fsl_mc_addr_translation_range - bus to system address translation
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
58 struct fsl_mc_addr_translation_range {
59 enum dprc_region_type mc_region_type;
62 phys_addr_t start_phys_addr;
66 * fsl_mc_bus_match - device to driver matching callback
67 * @dev: the fsl-mc device to match against
68 * @drv: the device driver to search for matching fsl-mc object type
71 * Returns 1 on success, 0 otherwise.
73 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
75 const struct fsl_mc_device_id *id;
76 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
77 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
80 if (WARN_ON(!fsl_mc_bus_exists()))
83 if (!mc_drv->match_id_table)
87 * If the object is not 'plugged' don't match.
88 * Only exception is the root DPRC, which is a special case.
90 if ((mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED) == 0 &&
91 !fsl_mc_is_root_dprc(&mc_dev->dev))
95 * Traverse the match_id table of the given driver, trying to find
96 * a matching for the given device.
98 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
99 if (id->vendor == mc_dev->obj_desc.vendor &&
100 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
108 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
113 * fsl_mc_bus_uevent - callback invoked when a device is added
115 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
117 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
119 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
120 mc_dev->obj_desc.vendor,
121 mc_dev->obj_desc.type))
127 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
130 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
132 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
133 mc_dev->obj_desc.type);
135 static DEVICE_ATTR_RO(modalias);
137 static struct attribute *fsl_mc_dev_attrs[] = {
138 &dev_attr_modalias.attr,
142 ATTRIBUTE_GROUPS(fsl_mc_dev);
144 struct bus_type fsl_mc_bus_type = {
146 .match = fsl_mc_bus_match,
147 .uevent = fsl_mc_bus_uevent,
148 .dev_groups = fsl_mc_dev_groups,
150 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
152 static atomic_t root_dprc_count = ATOMIC_INIT(0);
154 static int fsl_mc_driver_probe(struct device *dev)
156 struct fsl_mc_driver *mc_drv;
157 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
160 if (WARN_ON(!dev->driver))
163 mc_drv = to_fsl_mc_driver(dev->driver);
164 if (WARN_ON(!mc_drv->probe))
167 error = mc_drv->probe(mc_dev);
169 dev_err(dev, "%s failed: %d\n", __func__, error);
176 static int fsl_mc_driver_remove(struct device *dev)
178 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
179 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
182 if (WARN_ON(!dev->driver))
185 error = mc_drv->remove(mc_dev);
187 dev_err(dev, "%s failed: %d\n", __func__, error);
194 static void fsl_mc_driver_shutdown(struct device *dev)
196 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
197 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
199 mc_drv->shutdown(mc_dev);
203 * __fsl_mc_driver_register - registers a child device driver with the
206 * This function is implicitly invoked from the registration function of
207 * fsl_mc device drivers, which is generated by the
208 * module_fsl_mc_driver() macro.
210 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
211 struct module *owner)
215 mc_driver->driver.owner = owner;
216 mc_driver->driver.bus = &fsl_mc_bus_type;
218 if (mc_driver->probe)
219 mc_driver->driver.probe = fsl_mc_driver_probe;
221 if (mc_driver->remove)
222 mc_driver->driver.remove = fsl_mc_driver_remove;
224 if (mc_driver->shutdown)
225 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
227 error = driver_register(&mc_driver->driver);
229 pr_err("driver_register() failed for %s: %d\n",
230 mc_driver->driver.name, error);
234 pr_info("driver %s registered\n", mc_driver->driver.name);
237 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
240 * fsl_mc_driver_unregister - unregisters a device driver from the
243 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
245 driver_unregister(&mc_driver->driver);
247 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
250 * fsl_mc_bus_exists - check if a root dprc exists
252 bool fsl_mc_bus_exists(void)
254 return atomic_read(&root_dprc_count) > 0;
256 EXPORT_SYMBOL_GPL(fsl_mc_bus_exists);
259 * fsl_mc_get_root_dprc - function to traverse to the root dprc
261 void fsl_mc_get_root_dprc(struct device *dev,
262 struct device **root_dprc_dev)
265 *root_dprc_dev = NULL;
266 } else if (WARN_ON(!dev_is_fsl_mc(dev))) {
267 *root_dprc_dev = NULL;
269 *root_dprc_dev = dev;
270 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
271 *root_dprc_dev = (*root_dprc_dev)->parent;
274 EXPORT_SYMBOL_GPL(fsl_mc_get_root_dprc);
276 static int get_dprc_attr(struct fsl_mc_io *mc_io,
277 int container_id, struct dprc_attributes *attr)
282 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
284 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
288 memset(attr, 0, sizeof(struct dprc_attributes));
289 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
291 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
299 (void)dprc_close(mc_io, 0, dprc_handle);
303 static int get_dprc_icid(struct fsl_mc_io *mc_io,
304 int container_id, u16 *icid)
306 struct dprc_attributes attr;
309 error = get_dprc_attr(mc_io, container_id, &attr);
316 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
317 enum dprc_region_type mc_region_type,
318 u64 mc_offset, phys_addr_t *phys_addr)
321 struct device *root_dprc_dev;
324 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
325 if (WARN_ON(!root_dprc_dev))
327 mc = dev_get_drvdata(root_dprc_dev->parent);
329 if (mc->num_translation_ranges == 0) {
331 * Do identity mapping:
333 *phys_addr = mc_offset;
337 for (i = 0; i < mc->num_translation_ranges; i++) {
338 struct fsl_mc_addr_translation_range *range =
339 &mc->translation_ranges[i];
341 if (mc_region_type == range->mc_region_type &&
342 mc_offset >= range->start_mc_offset &&
343 mc_offset < range->end_mc_offset) {
344 *phys_addr = range->start_phys_addr +
345 (mc_offset - range->start_mc_offset);
353 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
354 struct fsl_mc_device *mc_bus_dev)
358 struct resource *regions;
359 struct dprc_obj_desc *obj_desc = &mc_dev->obj_desc;
360 struct device *parent_dev = mc_dev->dev.parent;
361 enum dprc_region_type mc_region_type;
363 if (strcmp(obj_desc->type, "dprc") == 0 ||
364 strcmp(obj_desc->type, "dpmcp") == 0) {
365 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
366 } else if (strcmp(obj_desc->type, "dpio") == 0) {
367 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
370 * This function should not have been called for this MC object
371 * type, as this object type is not supposed to have MMIO
378 regions = kmalloc_array(obj_desc->region_count,
379 sizeof(regions[0]), GFP_KERNEL);
383 for (i = 0; i < obj_desc->region_count; i++) {
384 struct dprc_region_desc region_desc;
386 error = dprc_get_obj_region(mc_bus_dev->mc_io,
388 mc_bus_dev->mc_handle,
390 obj_desc->id, i, ®ion_desc);
393 "dprc_get_obj_region() failed: %d\n", error);
394 goto error_cleanup_regions;
397 WARN_ON(region_desc.size == 0);
398 error = translate_mc_addr(mc_dev, mc_region_type,
399 region_desc.base_offset,
403 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
404 region_desc.base_offset,
405 obj_desc->type, obj_desc->id, i);
406 goto error_cleanup_regions;
409 regions[i].end = regions[i].start + region_desc.size - 1;
410 regions[i].name = "fsl-mc object MMIO region";
411 regions[i].flags = IORESOURCE_IO;
412 if (region_desc.flags & DPRC_REGION_CACHEABLE)
413 regions[i].flags |= IORESOURCE_CACHEABLE;
416 mc_dev->regions = regions;
419 error_cleanup_regions:
425 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
427 bool fsl_mc_is_root_dprc(struct device *dev)
429 struct device *root_dprc_dev;
431 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
434 return dev == root_dprc_dev;
438 * Add a newly discovered fsl-mc device to be visible in Linux
440 int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
441 struct fsl_mc_io *mc_io,
442 struct device *parent_dev,
443 struct fsl_mc_device **new_mc_dev)
446 struct fsl_mc_device *mc_dev = NULL;
447 struct fsl_mc_bus *mc_bus = NULL;
448 struct fsl_mc_device *parent_mc_dev;
450 if (dev_is_fsl_mc(parent_dev))
451 parent_mc_dev = to_fsl_mc_device(parent_dev);
453 parent_mc_dev = NULL;
455 if (strcmp(obj_desc->type, "dprc") == 0) {
457 * Allocate an MC bus device object:
459 mc_bus = devm_kzalloc(parent_dev, sizeof(*mc_bus), GFP_KERNEL);
463 mc_dev = &mc_bus->mc_dev;
466 * Allocate a regular fsl_mc_device object:
468 mc_dev = kmem_cache_zalloc(mc_dev_cache, GFP_KERNEL);
473 mc_dev->obj_desc = *obj_desc;
474 mc_dev->mc_io = mc_io;
475 device_initialize(&mc_dev->dev);
476 mc_dev->dev.parent = parent_dev;
477 mc_dev->dev.bus = &fsl_mc_bus_type;
478 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
480 if (strcmp(obj_desc->type, "dprc") == 0) {
481 struct fsl_mc_io *mc_io2;
483 mc_dev->flags |= FSL_MC_IS_DPRC;
486 * To get the DPRC's ICID, we need to open the DPRC
487 * in get_dprc_icid(). For child DPRCs, we do so using the
488 * parent DPRC's MC portal instead of the child DPRC's MC
489 * portal, in case the child DPRC is already opened with
490 * its own portal (e.g., the DPRC used by AIOP).
492 * NOTE: There cannot be more than one active open for a
493 * given MC object, using the same MC portal.
497 * device being added is a child DPRC device
499 mc_io2 = parent_mc_dev->mc_io;
502 * device being added is the root DPRC device
504 if (WARN_ON(!mc_io)) {
506 goto error_cleanup_dev;
511 atomic_inc(&root_dprc_count);
514 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
516 goto error_cleanup_dev;
519 * A non-DPRC object has to be a child of a DPRC, use the
520 * parent's ICID and interrupt domain.
522 mc_dev->icid = parent_mc_dev->icid;
523 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
524 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
525 dev_set_msi_domain(&mc_dev->dev,
526 dev_get_msi_domain(&parent_mc_dev->dev));
530 * Get MMIO regions for the device from the MC:
532 * NOTE: the root DPRC is a special case as its MMIO region is
533 * obtained from the device tree
535 if (parent_mc_dev && obj_desc->region_count != 0) {
536 error = fsl_mc_device_get_mmio_regions(mc_dev,
539 goto error_cleanup_dev;
542 /* Objects are coherent, unless 'no shareability' flag set. */
543 if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
544 arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
547 * The device-specific probe callback will get invoked by device_add()
549 error = device_add(&mc_dev->dev);
552 "device_add() failed for device %s: %d\n",
553 dev_name(&mc_dev->dev), error);
554 goto error_cleanup_dev;
557 (void)get_device(&mc_dev->dev);
558 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
560 *new_mc_dev = mc_dev;
564 kfree(mc_dev->regions);
566 devm_kfree(parent_dev, mc_bus);
568 kmem_cache_free(mc_dev_cache, mc_dev);
572 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
575 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
578 * @mc_dev: Pointer to an fsl-mc device
580 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
582 struct fsl_mc_bus *mc_bus = NULL;
584 kfree(mc_dev->regions);
587 * The device-specific remove callback will get invoked by device_del()
589 device_del(&mc_dev->dev);
590 put_device(&mc_dev->dev);
592 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) {
593 mc_bus = to_fsl_mc_bus(mc_dev);
595 if (fsl_mc_is_root_dprc(&mc_dev->dev)) {
596 if (atomic_read(&root_dprc_count) > 0)
597 atomic_dec(&root_dprc_count);
604 devm_kfree(mc_dev->dev.parent, mc_bus);
606 kmem_cache_free(mc_dev_cache, mc_dev);
608 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
610 static int parse_mc_ranges(struct device *dev,
614 const __be32 **ranges_start,
618 int range_tuple_cell_count;
621 struct device_node *mc_node = dev->of_node;
623 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
624 if (!(*ranges_start) || !ranges_len) {
626 "missing or empty ranges property for device tree node '%s'\n",
633 *paddr_cells = of_n_addr_cells(mc_node);
635 prop = of_get_property(mc_node, "#address-cells", NULL);
637 *mc_addr_cells = be32_to_cpup(prop);
639 *mc_addr_cells = *paddr_cells;
641 prop = of_get_property(mc_node, "#size-cells", NULL);
643 *mc_size_cells = be32_to_cpup(prop);
645 *mc_size_cells = of_n_size_cells(mc_node);
647 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
650 tuple_len = range_tuple_cell_count * sizeof(__be32);
651 if (ranges_len % tuple_len != 0) {
652 dev_err(dev, "malformed ranges property '%s'\n", mc_node->name);
656 *num_ranges = ranges_len / tuple_len;
660 static int get_mc_addr_translation_ranges(struct device *dev,
661 struct fsl_mc_addr_translation_range
670 const __be32 *ranges_start;
673 error = parse_mc_ranges(dev,
682 if (!(*num_ranges)) {
684 * Missing or empty ranges property ("ranges;") for the
685 * 'fsl,qoriq-mc' node. In this case, identity mapping
692 *ranges = devm_kcalloc(dev, *num_ranges,
693 sizeof(struct fsl_mc_addr_translation_range),
699 for (i = 0; i < *num_ranges; ++i) {
700 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
702 range->mc_region_type = of_read_number(cell, 1);
703 range->start_mc_offset = of_read_number(cell + 1,
705 cell += mc_addr_cells;
706 range->start_phys_addr = of_read_number(cell, paddr_cells);
708 range->end_mc_offset = range->start_mc_offset +
709 of_read_number(cell, mc_size_cells);
711 cell += mc_size_cells;
718 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
721 static int fsl_mc_bus_probe(struct platform_device *pdev)
723 struct dprc_obj_desc obj_desc;
726 struct fsl_mc_device *mc_bus_dev = NULL;
727 struct fsl_mc_io *mc_io = NULL;
729 phys_addr_t mc_portal_phys_addr;
731 struct mc_version mc_version;
734 dev_info(&pdev->dev, "root DPRC probed");
736 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
740 platform_set_drvdata(pdev, mc);
743 * Get physical address of MC portal for the root DPRC:
745 error = of_address_to_resource(pdev->dev.of_node, 0, &res);
748 "of_address_to_resource() failed for %s\n",
749 pdev->dev.of_node->full_name);
753 mc_portal_phys_addr = res.start;
754 mc_portal_size = resource_size(&res);
755 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
756 mc_portal_size, NULL,
757 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
761 error = mc_get_version(mc_io, 0, &mc_version);
764 "mc_get_version() failed with error %d\n", error);
765 goto error_cleanup_mc_io;
768 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
769 mc_version.major, mc_version.minor, mc_version.revision);
771 error = get_mc_addr_translation_ranges(&pdev->dev,
772 &mc->translation_ranges,
773 &mc->num_translation_ranges);
775 goto error_cleanup_mc_io;
777 error = dprc_get_container_id(mc_io, 0, &container_id);
780 "dpmng_get_container_id() failed: %d\n", error);
781 goto error_cleanup_mc_io;
784 memset(&obj_desc, 0, sizeof(struct dprc_obj_desc));
785 error = dprc_get_api_version(mc_io, 0,
787 &obj_desc.ver_minor);
789 goto error_cleanup_mc_io;
791 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
792 strcpy(obj_desc.type, "dprc");
793 obj_desc.id = container_id;
794 obj_desc.irq_count = 1;
795 obj_desc.region_count = 0;
797 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
799 goto error_cleanup_mc_io;
801 mc->root_mc_bus_dev = mc_bus_dev;
805 fsl_destroy_mc_io(mc_io);
810 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
813 static int fsl_mc_bus_remove(struct platform_device *pdev)
815 struct fsl_mc *mc = platform_get_drvdata(pdev);
817 if (WARN_ON(!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev)))
820 fsl_mc_device_remove(mc->root_mc_bus_dev);
822 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
823 mc->root_mc_bus_dev->mc_io = NULL;
825 dev_info(&pdev->dev, "root DPRC removed");
829 static const struct of_device_id fsl_mc_bus_match_table[] = {
830 {.compatible = "fsl,qoriq-mc",},
834 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
836 static struct platform_driver fsl_mc_bus_driver = {
838 .name = "fsl_mc_bus",
840 .of_match_table = fsl_mc_bus_match_table,
842 .probe = fsl_mc_bus_probe,
843 .remove = fsl_mc_bus_remove,
846 static int __init fsl_mc_bus_driver_init(void)
850 mc_dev_cache = kmem_cache_create("fsl_mc_device",
851 sizeof(struct fsl_mc_device), 0, 0,
854 pr_err("Could not create fsl_mc_device cache\n");
858 error = bus_register(&fsl_mc_bus_type);
860 pr_err("bus type registration failed: %d\n", error);
861 goto error_cleanup_cache;
864 pr_info("bus type registered\n");
866 error = platform_driver_register(&fsl_mc_bus_driver);
868 pr_err("platform_driver_register() failed: %d\n", error);
869 goto error_cleanup_bus;
872 error = dprc_driver_init();
874 goto error_cleanup_driver;
876 error = fsl_mc_allocator_driver_init();
878 goto error_cleanup_dprc_driver;
880 error = its_fsl_mc_msi_init();
882 goto error_cleanup_mc_allocator;
886 error_cleanup_mc_allocator:
887 fsl_mc_allocator_driver_exit();
889 error_cleanup_dprc_driver:
892 error_cleanup_driver:
893 platform_driver_unregister(&fsl_mc_bus_driver);
896 bus_unregister(&fsl_mc_bus_type);
899 kmem_cache_destroy(mc_dev_cache);
902 postcore_initcall(fsl_mc_bus_driver_init);