2 * Freescale Management Complex (MC) bus driver
4 * Copyright (C) 2014 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 #include "../include/mc-private.h"
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/slab.h>
18 #include <linux/limits.h>
19 #include <linux/bitops.h>
20 #include <linux/msi.h>
21 #include "../include/dpmng.h"
22 #include "../include/mc-sys.h"
25 static struct kmem_cache *mc_dev_cache;
27 static bool fsl_mc_is_root_dprc(struct device *dev);
30 * fsl_mc_bus_match - device to driver matching callback
31 * @dev: the MC object device structure to match against
32 * @drv: the device driver to search for matching MC object device id
35 * Returns 1 on success, 0 otherwise.
37 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
39 const struct fsl_mc_device_match_id *id;
40 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
41 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
44 if (WARN_ON(!fsl_mc_bus_exists()))
47 if (!mc_drv->match_id_table)
51 * If the object is not 'plugged' don't match.
52 * Only exception is the root DPRC, which is a special case.
54 if ((mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED) == 0 &&
55 !fsl_mc_is_root_dprc(&mc_dev->dev))
59 * Traverse the match_id table of the given driver, trying to find
60 * a matching for the given MC object device.
62 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
63 if (id->vendor == mc_dev->obj_desc.vendor &&
64 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
72 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
77 * fsl_mc_bus_uevent - callback invoked when a device is added
79 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
81 pr_debug("%s invoked\n", __func__);
85 struct bus_type fsl_mc_bus_type = {
87 .match = fsl_mc_bus_match,
88 .uevent = fsl_mc_bus_uevent,
90 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
92 static atomic_t root_dprc_count = ATOMIC_INIT(0);
94 static int fsl_mc_driver_probe(struct device *dev)
96 struct fsl_mc_driver *mc_drv;
97 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
100 if (WARN_ON(!dev->driver))
103 mc_drv = to_fsl_mc_driver(dev->driver);
104 if (WARN_ON(!mc_drv->probe))
107 error = mc_drv->probe(mc_dev);
109 dev_err(dev, "MC object device probe callback failed: %d\n",
117 static int fsl_mc_driver_remove(struct device *dev)
119 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
120 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
123 if (WARN_ON(!dev->driver))
126 error = mc_drv->remove(mc_dev);
129 "MC object device remove callback failed: %d\n",
137 static void fsl_mc_driver_shutdown(struct device *dev)
139 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
140 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
142 mc_drv->shutdown(mc_dev);
146 * __fsl_mc_driver_register - registers a child device driver with the
149 * This function is implicitly invoked from the registration function of
150 * fsl_mc device drivers, which is generated by the
151 * module_fsl_mc_driver() macro.
153 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
154 struct module *owner)
158 mc_driver->driver.owner = owner;
159 mc_driver->driver.bus = &fsl_mc_bus_type;
161 if (mc_driver->probe)
162 mc_driver->driver.probe = fsl_mc_driver_probe;
164 if (mc_driver->remove)
165 mc_driver->driver.remove = fsl_mc_driver_remove;
167 if (mc_driver->shutdown)
168 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
170 error = driver_register(&mc_driver->driver);
172 pr_err("driver_register() failed for %s: %d\n",
173 mc_driver->driver.name, error);
177 pr_info("MC object device driver %s registered\n",
178 mc_driver->driver.name);
181 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
184 * fsl_mc_driver_unregister - unregisters a device driver from the
187 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
189 driver_unregister(&mc_driver->driver);
191 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
194 * fsl_mc_bus_exists - check if a root dprc exists
196 bool fsl_mc_bus_exists(void)
198 return atomic_read(&root_dprc_count) > 0;
200 EXPORT_SYMBOL_GPL(fsl_mc_bus_exists);
203 * fsl_mc_get_root_dprc - function to traverse to the root dprc
205 static void fsl_mc_get_root_dprc(struct device *dev,
206 struct device **root_dprc_dev)
209 *root_dprc_dev = NULL;
210 } else if (WARN_ON(dev->bus != &fsl_mc_bus_type)) {
211 *root_dprc_dev = NULL;
213 *root_dprc_dev = dev;
214 while ((*root_dprc_dev)->parent->bus == &fsl_mc_bus_type)
215 *root_dprc_dev = (*root_dprc_dev)->parent;
220 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
222 static bool fsl_mc_is_root_dprc(struct device *dev)
224 struct device *root_dprc_dev;
226 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
229 return dev == root_dprc_dev;
232 static int get_dprc_attr(struct fsl_mc_io *mc_io,
233 int container_id, struct dprc_attributes *attr)
238 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
240 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
244 memset(attr, 0, sizeof(struct dprc_attributes));
245 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
247 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
255 (void)dprc_close(mc_io, 0, dprc_handle);
259 static int get_dprc_icid(struct fsl_mc_io *mc_io,
260 int container_id, u16 *icid)
262 struct dprc_attributes attr;
265 error = get_dprc_attr(mc_io, container_id, &attr);
272 static int get_dprc_version(struct fsl_mc_io *mc_io,
273 int container_id, u16 *major, u16 *minor)
275 struct dprc_attributes attr;
278 error = get_dprc_attr(mc_io, container_id, &attr);
280 *major = attr.version.major;
281 *minor = attr.version.minor;
287 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
288 enum dprc_region_type mc_region_type,
289 u64 mc_offset, phys_addr_t *phys_addr)
292 struct device *root_dprc_dev;
295 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
296 if (WARN_ON(!root_dprc_dev))
298 mc = dev_get_drvdata(root_dprc_dev->parent);
300 if (mc->num_translation_ranges == 0) {
302 * Do identity mapping:
304 *phys_addr = mc_offset;
308 for (i = 0; i < mc->num_translation_ranges; i++) {
309 struct fsl_mc_addr_translation_range *range =
310 &mc->translation_ranges[i];
312 if (mc_region_type == range->mc_region_type &&
313 mc_offset >= range->start_mc_offset &&
314 mc_offset < range->end_mc_offset) {
315 *phys_addr = range->start_phys_addr +
316 (mc_offset - range->start_mc_offset);
324 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
325 struct fsl_mc_device *mc_bus_dev)
329 struct resource *regions;
330 struct dprc_obj_desc *obj_desc = &mc_dev->obj_desc;
331 struct device *parent_dev = mc_dev->dev.parent;
332 enum dprc_region_type mc_region_type;
334 if (strcmp(obj_desc->type, "dprc") == 0 ||
335 strcmp(obj_desc->type, "dpmcp") == 0) {
336 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
337 } else if (strcmp(obj_desc->type, "dpio") == 0) {
338 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
341 * This function should not have been called for this MC object
342 * type, as this object type is not supposed to have MMIO
349 regions = kmalloc_array(obj_desc->region_count,
350 sizeof(regions[0]), GFP_KERNEL);
354 for (i = 0; i < obj_desc->region_count; i++) {
355 struct dprc_region_desc region_desc;
357 error = dprc_get_obj_region(mc_bus_dev->mc_io,
359 mc_bus_dev->mc_handle,
361 obj_desc->id, i, ®ion_desc);
364 "dprc_get_obj_region() failed: %d\n", error);
365 goto error_cleanup_regions;
368 WARN_ON(region_desc.size == 0);
369 error = translate_mc_addr(mc_dev, mc_region_type,
370 region_desc.base_offset,
374 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
375 region_desc.base_offset,
376 obj_desc->type, obj_desc->id, i);
377 goto error_cleanup_regions;
380 regions[i].end = regions[i].start + region_desc.size - 1;
381 regions[i].name = "fsl-mc object MMIO region";
382 regions[i].flags = IORESOURCE_IO;
383 if (region_desc.flags & DPRC_REGION_CACHEABLE)
384 regions[i].flags |= IORESOURCE_CACHEABLE;
387 mc_dev->regions = regions;
390 error_cleanup_regions:
396 * Add a newly discovered MC object device to be visible in Linux
398 int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
399 struct fsl_mc_io *mc_io,
400 struct device *parent_dev,
401 struct fsl_mc_device **new_mc_dev)
404 struct fsl_mc_device *mc_dev = NULL;
405 struct fsl_mc_bus *mc_bus = NULL;
406 struct fsl_mc_device *parent_mc_dev;
408 if (parent_dev->bus == &fsl_mc_bus_type)
409 parent_mc_dev = to_fsl_mc_device(parent_dev);
411 parent_mc_dev = NULL;
413 if (strcmp(obj_desc->type, "dprc") == 0) {
415 * Allocate an MC bus device object:
417 mc_bus = devm_kzalloc(parent_dev, sizeof(*mc_bus), GFP_KERNEL);
421 mc_dev = &mc_bus->mc_dev;
424 * Allocate a regular fsl_mc_device object:
426 mc_dev = kmem_cache_zalloc(mc_dev_cache, GFP_KERNEL);
431 mc_dev->obj_desc = *obj_desc;
432 mc_dev->mc_io = mc_io;
433 device_initialize(&mc_dev->dev);
434 mc_dev->dev.parent = parent_dev;
435 mc_dev->dev.bus = &fsl_mc_bus_type;
436 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
438 if (strcmp(obj_desc->type, "dprc") == 0) {
439 struct fsl_mc_io *mc_io2;
441 mc_dev->flags |= FSL_MC_IS_DPRC;
444 * To get the DPRC's ICID, we need to open the DPRC
445 * in get_dprc_icid(). For child DPRCs, we do so using the
446 * parent DPRC's MC portal instead of the child DPRC's MC
447 * portal, in case the child DPRC is already opened with
448 * its own portal (e.g., the DPRC used by AIOP).
450 * NOTE: There cannot be more than one active open for a
451 * given MC object, using the same MC portal.
455 * device being added is a child DPRC device
457 mc_io2 = parent_mc_dev->mc_io;
460 * device being added is the root DPRC device
462 if (WARN_ON(!mc_io)) {
464 goto error_cleanup_dev;
469 atomic_inc(&root_dprc_count);
472 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
474 goto error_cleanup_dev;
477 * A non-DPRC MC object device has to be a child of another
478 * MC object (specifically a DPRC object)
480 mc_dev->icid = parent_mc_dev->icid;
481 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
482 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
483 dev_set_msi_domain(&mc_dev->dev,
484 dev_get_msi_domain(&parent_mc_dev->dev));
488 * Get MMIO regions for the device from the MC:
490 * NOTE: the root DPRC is a special case as its MMIO region is
491 * obtained from the device tree
493 if (parent_mc_dev && obj_desc->region_count != 0) {
494 error = fsl_mc_device_get_mmio_regions(mc_dev,
497 goto error_cleanup_dev;
500 /* Objects are coherent, unless 'no shareability' flag set. */
501 if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
502 arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
505 * The device-specific probe callback will get invoked by device_add()
507 error = device_add(&mc_dev->dev);
510 "device_add() failed for device %s: %d\n",
511 dev_name(&mc_dev->dev), error);
512 goto error_cleanup_dev;
515 (void)get_device(&mc_dev->dev);
516 dev_dbg(parent_dev, "Added MC object device %s\n",
517 dev_name(&mc_dev->dev));
519 *new_mc_dev = mc_dev;
523 kfree(mc_dev->regions);
525 devm_kfree(parent_dev, mc_bus);
527 kmem_cache_free(mc_dev_cache, mc_dev);
531 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
534 * fsl_mc_device_remove - Remove a MC object device from being visible to
537 * @mc_dev: Pointer to a MC object device object
539 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
541 struct fsl_mc_bus *mc_bus = NULL;
543 kfree(mc_dev->regions);
546 * The device-specific remove callback will get invoked by device_del()
548 device_del(&mc_dev->dev);
549 put_device(&mc_dev->dev);
551 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0) {
552 mc_bus = to_fsl_mc_bus(mc_dev);
554 fsl_destroy_mc_io(mc_dev->mc_io);
555 mc_dev->mc_io = NULL;
558 if (fsl_mc_is_root_dprc(&mc_dev->dev)) {
559 if (atomic_read(&root_dprc_count) > 0)
560 atomic_dec(&root_dprc_count);
567 devm_kfree(mc_dev->dev.parent, mc_bus);
569 kmem_cache_free(mc_dev_cache, mc_dev);
571 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
573 static int parse_mc_ranges(struct device *dev,
577 const __be32 **ranges_start,
581 int range_tuple_cell_count;
584 struct device_node *mc_node = dev->of_node;
586 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
587 if (!(*ranges_start) || !ranges_len) {
589 "missing or empty ranges property for device tree node '%s'\n",
596 *paddr_cells = of_n_addr_cells(mc_node);
598 prop = of_get_property(mc_node, "#address-cells", NULL);
600 *mc_addr_cells = be32_to_cpup(prop);
602 *mc_addr_cells = *paddr_cells;
604 prop = of_get_property(mc_node, "#size-cells", NULL);
606 *mc_size_cells = be32_to_cpup(prop);
608 *mc_size_cells = of_n_size_cells(mc_node);
610 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
613 tuple_len = range_tuple_cell_count * sizeof(__be32);
614 if (ranges_len % tuple_len != 0) {
615 dev_err(dev, "malformed ranges property '%s'\n", mc_node->name);
619 *num_ranges = ranges_len / tuple_len;
623 static int get_mc_addr_translation_ranges(struct device *dev,
624 struct fsl_mc_addr_translation_range
633 const __be32 *ranges_start;
636 error = parse_mc_ranges(dev,
645 if (!(*num_ranges)) {
647 * Missing or empty ranges property ("ranges;") for the
648 * 'fsl,qoriq-mc' node. In this case, identity mapping
655 *ranges = devm_kcalloc(dev, *num_ranges,
656 sizeof(struct fsl_mc_addr_translation_range),
662 for (i = 0; i < *num_ranges; ++i) {
663 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
665 range->mc_region_type = of_read_number(cell, 1);
666 range->start_mc_offset = of_read_number(cell + 1,
668 cell += mc_addr_cells;
669 range->start_phys_addr = of_read_number(cell, paddr_cells);
671 range->end_mc_offset = range->start_mc_offset +
672 of_read_number(cell, mc_size_cells);
674 cell += mc_size_cells;
681 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
684 static int fsl_mc_bus_probe(struct platform_device *pdev)
686 struct dprc_obj_desc obj_desc;
689 struct fsl_mc_device *mc_bus_dev = NULL;
690 struct fsl_mc_io *mc_io = NULL;
692 phys_addr_t mc_portal_phys_addr;
694 struct mc_version mc_version;
697 dev_info(&pdev->dev, "Root MC bus device probed");
699 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
703 platform_set_drvdata(pdev, mc);
706 * Get physical address of MC portal for the root DPRC:
708 error = of_address_to_resource(pdev->dev.of_node, 0, &res);
711 "of_address_to_resource() failed for %s\n",
712 pdev->dev.of_node->full_name);
716 mc_portal_phys_addr = res.start;
717 mc_portal_size = resource_size(&res);
718 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
719 mc_portal_size, NULL,
720 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
724 error = mc_get_version(mc_io, 0, &mc_version);
727 "mc_get_version() failed with error %d\n", error);
728 goto error_cleanup_mc_io;
732 "Freescale Management Complex Firmware version: %u.%u.%u\n",
733 mc_version.major, mc_version.minor, mc_version.revision);
735 error = get_mc_addr_translation_ranges(&pdev->dev,
736 &mc->translation_ranges,
737 &mc->num_translation_ranges);
739 goto error_cleanup_mc_io;
741 error = dpmng_get_container_id(mc_io, 0, &container_id);
744 "dpmng_get_container_id() failed: %d\n", error);
745 goto error_cleanup_mc_io;
748 memset(&obj_desc, 0, sizeof(struct dprc_obj_desc));
749 error = get_dprc_version(mc_io, container_id,
750 &obj_desc.ver_major, &obj_desc.ver_minor);
752 goto error_cleanup_mc_io;
754 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
755 strcpy(obj_desc.type, "dprc");
756 obj_desc.id = container_id;
757 obj_desc.irq_count = 1;
758 obj_desc.region_count = 0;
760 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
762 goto error_cleanup_mc_io;
764 mc->root_mc_bus_dev = mc_bus_dev;
768 fsl_destroy_mc_io(mc_io);
773 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
776 static int fsl_mc_bus_remove(struct platform_device *pdev)
778 struct fsl_mc *mc = platform_get_drvdata(pdev);
780 if (WARN_ON(!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev)))
783 fsl_mc_device_remove(mc->root_mc_bus_dev);
784 dev_info(&pdev->dev, "Root MC bus device removed");
788 static const struct of_device_id fsl_mc_bus_match_table[] = {
789 {.compatible = "fsl,qoriq-mc",},
793 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
795 static struct platform_driver fsl_mc_bus_driver = {
797 .name = "fsl_mc_bus",
799 .of_match_table = fsl_mc_bus_match_table,
801 .probe = fsl_mc_bus_probe,
802 .remove = fsl_mc_bus_remove,
805 static int __init fsl_mc_bus_driver_init(void)
809 mc_dev_cache = kmem_cache_create("fsl_mc_device",
810 sizeof(struct fsl_mc_device), 0, 0,
813 pr_err("Could not create fsl_mc_device cache\n");
817 error = bus_register(&fsl_mc_bus_type);
819 pr_err("fsl-mc bus type registration failed: %d\n", error);
820 goto error_cleanup_cache;
823 pr_info("fsl-mc bus type registered\n");
825 error = platform_driver_register(&fsl_mc_bus_driver);
827 pr_err("platform_driver_register() failed: %d\n", error);
828 goto error_cleanup_bus;
831 error = dprc_driver_init();
833 goto error_cleanup_driver;
835 error = fsl_mc_allocator_driver_init();
837 goto error_cleanup_dprc_driver;
839 error = its_fsl_mc_msi_init();
841 goto error_cleanup_mc_allocator;
845 error_cleanup_mc_allocator:
846 fsl_mc_allocator_driver_exit();
848 error_cleanup_dprc_driver:
851 error_cleanup_driver:
852 platform_driver_unregister(&fsl_mc_bus_driver);
855 bus_unregister(&fsl_mc_bus_type);
858 kmem_cache_destroy(mc_dev_cache);
862 postcore_initcall(fsl_mc_bus_driver_init);
864 static void __exit fsl_mc_bus_driver_exit(void)
866 if (WARN_ON(!mc_dev_cache))
869 its_fsl_mc_msi_cleanup();
870 fsl_mc_allocator_driver_exit();
872 platform_driver_unregister(&fsl_mc_bus_driver);
873 bus_unregister(&fsl_mc_bus_type);
874 kmem_cache_destroy(mc_dev_cache);
875 pr_info("MC bus unregistered\n");
878 module_exit(fsl_mc_bus_driver_exit);
880 MODULE_AUTHOR("Freescale Semiconductor Inc.");
881 MODULE_DESCRIPTION("Freescale Management Complex (MC) bus driver");
882 MODULE_LICENSE("GPL");