1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale Management Complex (MC) bus driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6 * Copyright 2019-2020 NXP
7 * Author: German Rivera <German.Rivera@freescale.com>
11 #define pr_fmt(fmt) "fsl-mc: " fmt
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 <linux/dma-mapping.h>
22 #include <linux/acpi.h>
23 #include <linux/iommu.h>
24 #include <linux/dma-map-ops.h>
26 #include "fsl-mc-private.h"
29 * Default DMA mask for devices on a fsl-mc bus
31 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
33 static struct fsl_mc_version mc_version;
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
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;
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;
65 #define FSL_MC_GCR1 0x0
66 #define GCR1_P1_STOP BIT(31)
67 #define GCR1_P2_STOP BIT(30)
69 #define FSL_MC_FAPR 0x28
70 #define MC_FAPR_PL BIT(18)
71 #define MC_FAPR_BMT BIT(17)
73 static phys_addr_t mc_portal_base_phys_addr;
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
81 * Returns 1 on success, 0 otherwise.
83 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
85 const struct fsl_mc_device_id *id;
86 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
87 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
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);
96 if (!mc_drv->match_id_table)
100 * If the object is not 'plugged' don't match.
101 * Only exception is the root DPRC, which is a special case.
103 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
104 !fsl_mc_is_root_dprc(&mc_dev->dev))
108 * Traverse the match_id table of the given driver, trying to find
109 * a matching for the given device.
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) {
121 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
126 * fsl_mc_bus_uevent - callback invoked when a device is added
128 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
130 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
132 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
133 mc_dev->obj_desc.vendor,
134 mc_dev->obj_desc.type))
140 static int fsl_mc_dma_configure(struct device *dev)
142 struct device *dma_dev = dev;
143 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
144 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
145 u32 input_id = mc_dev->icid;
148 while (dev_is_fsl_mc(dma_dev))
149 dma_dev = dma_dev->parent;
151 if (dev_of_node(dma_dev))
152 ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
154 ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
156 if (!ret && !mc_drv->driver_managed_dma) {
157 ret = iommu_device_use_default_domain(dev);
159 arch_teardown_dma_ops(dev);
165 static void fsl_mc_dma_cleanup(struct device *dev)
167 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
169 if (!mc_drv->driver_managed_dma)
170 iommu_device_unuse_default_domain(dev);
173 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
176 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
178 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
179 mc_dev->obj_desc.type);
181 static DEVICE_ATTR_RO(modalias);
183 static ssize_t driver_override_store(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
187 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
190 if (WARN_ON(dev->bus != &fsl_mc_bus_type))
193 ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
200 static ssize_t driver_override_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
203 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
205 return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
207 static DEVICE_ATTR_RW(driver_override);
209 static struct attribute *fsl_mc_dev_attrs[] = {
210 &dev_attr_modalias.attr,
211 &dev_attr_driver_override.attr,
215 ATTRIBUTE_GROUPS(fsl_mc_dev);
217 static int scan_fsl_mc_bus(struct device *dev, void *data)
219 struct fsl_mc_device *root_mc_dev;
220 struct fsl_mc_bus *root_mc_bus;
222 if (!fsl_mc_is_root_dprc(dev))
225 root_mc_dev = to_fsl_mc_device(dev);
226 root_mc_bus = to_fsl_mc_bus(root_mc_dev);
227 mutex_lock(&root_mc_bus->scan_mutex);
228 dprc_scan_objects(root_mc_dev, false);
229 mutex_unlock(&root_mc_bus->scan_mutex);
235 static ssize_t rescan_store(struct bus_type *bus,
236 const char *buf, size_t count)
240 if (kstrtoul(buf, 0, &val) < 0)
244 bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
248 static BUS_ATTR_WO(rescan);
250 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
252 struct fsl_mc_device *root_mc_dev;
256 if (!fsl_mc_is_root_dprc(dev))
259 root_mc_dev = to_fsl_mc_device(dev);
261 if (kstrtoul(buf, 0, &val) < 0)
265 enable_dprc_irq(root_mc_dev);
267 disable_dprc_irq(root_mc_dev);
273 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
275 struct fsl_mc_device *root_mc_dev;
278 if (!fsl_mc_is_root_dprc(dev))
281 root_mc_dev = to_fsl_mc_device(dev);
283 sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
288 static ssize_t autorescan_store(struct bus_type *bus,
289 const char *buf, size_t count)
291 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
296 static ssize_t autorescan_show(struct bus_type *bus, char *buf)
298 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
302 static BUS_ATTR_RW(autorescan);
304 static struct attribute *fsl_mc_bus_attrs[] = {
305 &bus_attr_rescan.attr,
306 &bus_attr_autorescan.attr,
310 ATTRIBUTE_GROUPS(fsl_mc_bus);
312 struct bus_type fsl_mc_bus_type = {
314 .match = fsl_mc_bus_match,
315 .uevent = fsl_mc_bus_uevent,
316 .dma_configure = fsl_mc_dma_configure,
317 .dma_cleanup = fsl_mc_dma_cleanup,
318 .dev_groups = fsl_mc_dev_groups,
319 .bus_groups = fsl_mc_bus_groups,
321 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
323 struct device_type fsl_mc_bus_dprc_type = {
324 .name = "fsl_mc_bus_dprc"
326 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
328 struct device_type fsl_mc_bus_dpni_type = {
329 .name = "fsl_mc_bus_dpni"
331 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
333 struct device_type fsl_mc_bus_dpio_type = {
334 .name = "fsl_mc_bus_dpio"
336 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
338 struct device_type fsl_mc_bus_dpsw_type = {
339 .name = "fsl_mc_bus_dpsw"
341 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
343 struct device_type fsl_mc_bus_dpbp_type = {
344 .name = "fsl_mc_bus_dpbp"
346 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
348 struct device_type fsl_mc_bus_dpcon_type = {
349 .name = "fsl_mc_bus_dpcon"
351 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
353 struct device_type fsl_mc_bus_dpmcp_type = {
354 .name = "fsl_mc_bus_dpmcp"
356 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
358 struct device_type fsl_mc_bus_dpmac_type = {
359 .name = "fsl_mc_bus_dpmac"
361 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
363 struct device_type fsl_mc_bus_dprtc_type = {
364 .name = "fsl_mc_bus_dprtc"
366 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
368 struct device_type fsl_mc_bus_dpseci_type = {
369 .name = "fsl_mc_bus_dpseci"
371 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
373 struct device_type fsl_mc_bus_dpdmux_type = {
374 .name = "fsl_mc_bus_dpdmux"
376 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
378 struct device_type fsl_mc_bus_dpdcei_type = {
379 .name = "fsl_mc_bus_dpdcei"
381 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
383 struct device_type fsl_mc_bus_dpaiop_type = {
384 .name = "fsl_mc_bus_dpaiop"
386 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
388 struct device_type fsl_mc_bus_dpci_type = {
389 .name = "fsl_mc_bus_dpci"
391 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
393 struct device_type fsl_mc_bus_dpdmai_type = {
394 .name = "fsl_mc_bus_dpdmai"
396 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
398 struct device_type fsl_mc_bus_dpdbg_type = {
399 .name = "fsl_mc_bus_dpdbg"
401 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
403 static struct device_type *fsl_mc_get_device_type(const char *type)
405 static const struct {
406 struct device_type *dev_type;
409 { &fsl_mc_bus_dprc_type, "dprc" },
410 { &fsl_mc_bus_dpni_type, "dpni" },
411 { &fsl_mc_bus_dpio_type, "dpio" },
412 { &fsl_mc_bus_dpsw_type, "dpsw" },
413 { &fsl_mc_bus_dpbp_type, "dpbp" },
414 { &fsl_mc_bus_dpcon_type, "dpcon" },
415 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
416 { &fsl_mc_bus_dpmac_type, "dpmac" },
417 { &fsl_mc_bus_dprtc_type, "dprtc" },
418 { &fsl_mc_bus_dpseci_type, "dpseci" },
419 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
420 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
421 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
422 { &fsl_mc_bus_dpci_type, "dpci" },
423 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
424 { &fsl_mc_bus_dpdbg_type, "dpdbg" },
429 for (i = 0; dev_types[i].dev_type; i++)
430 if (!strcmp(dev_types[i].type, type))
431 return dev_types[i].dev_type;
436 static int fsl_mc_driver_probe(struct device *dev)
438 struct fsl_mc_driver *mc_drv;
439 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
442 mc_drv = to_fsl_mc_driver(dev->driver);
444 error = mc_drv->probe(mc_dev);
446 if (error != -EPROBE_DEFER)
447 dev_err(dev, "%s failed: %d\n", __func__, error);
454 static int fsl_mc_driver_remove(struct device *dev)
456 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
457 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
460 error = mc_drv->remove(mc_dev);
462 dev_err(dev, "%s failed: %d\n", __func__, error);
469 static void fsl_mc_driver_shutdown(struct device *dev)
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);
474 mc_drv->shutdown(mc_dev);
478 * __fsl_mc_driver_register - registers a child device driver with the
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.
485 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
486 struct module *owner)
490 mc_driver->driver.owner = owner;
491 mc_driver->driver.bus = &fsl_mc_bus_type;
493 if (mc_driver->probe)
494 mc_driver->driver.probe = fsl_mc_driver_probe;
496 if (mc_driver->remove)
497 mc_driver->driver.remove = fsl_mc_driver_remove;
499 if (mc_driver->shutdown)
500 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
502 error = driver_register(&mc_driver->driver);
504 pr_err("driver_register() failed for %s: %d\n",
505 mc_driver->driver.name, error);
511 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
514 * fsl_mc_driver_unregister - unregisters a device driver from the
517 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
519 driver_unregister(&mc_driver->driver);
521 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
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
530 * Return: '0' on Success; Error code otherwise.
532 static int mc_get_version(struct fsl_mc_io *mc_io,
534 struct fsl_mc_version *mc_ver_info)
536 struct fsl_mc_command cmd = { 0 };
537 struct dpmng_rsp_get_version *rsp_params;
540 /* prepare command */
541 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
545 /* send command to mc*/
546 err = mc_send_command(mc_io, &cmd);
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);
560 * fsl_mc_get_version - function to retrieve the MC f/w version information
562 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
564 struct fsl_mc_version *fsl_mc_get_version(void)
566 if (mc_version.major)
571 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
574 * fsl_mc_get_root_dprc - function to traverse to the root dprc
576 void fsl_mc_get_root_dprc(struct device *dev,
577 struct device **root_dprc_dev)
580 *root_dprc_dev = NULL;
581 } else if (!dev_is_fsl_mc(dev)) {
582 *root_dprc_dev = NULL;
584 *root_dprc_dev = dev;
585 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
586 *root_dprc_dev = (*root_dprc_dev)->parent;
590 static int get_dprc_attr(struct fsl_mc_io *mc_io,
591 int container_id, struct dprc_attributes *attr)
596 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
598 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
602 memset(attr, 0, sizeof(struct dprc_attributes));
603 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
605 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
613 (void)dprc_close(mc_io, 0, dprc_handle);
617 static int get_dprc_icid(struct fsl_mc_io *mc_io,
618 int container_id, u32 *icid)
620 struct dprc_attributes attr;
623 error = get_dprc_attr(mc_io, container_id, &attr);
630 static 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)
635 struct device *root_dprc_dev;
638 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
639 mc = dev_get_drvdata(root_dprc_dev->parent);
641 if (mc->num_translation_ranges == 0) {
643 * Do identity mapping:
645 *phys_addr = mc_offset;
649 for (i = 0; i < mc->num_translation_ranges; i++) {
650 struct fsl_mc_addr_translation_range *range =
651 &mc->translation_ranges[i];
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);
665 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
666 struct fsl_mc_device *mc_bus_dev)
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;
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;
682 * This function should not have been called for this MC object
683 * type, as this object type is not supposed to have MMIO
689 regions = kmalloc_array(obj_desc->region_count,
690 sizeof(regions[0]), GFP_KERNEL);
694 for (i = 0; i < obj_desc->region_count; i++) {
695 struct dprc_region_desc region_desc;
697 error = dprc_get_obj_region(mc_bus_dev->mc_io,
699 mc_bus_dev->mc_handle,
701 obj_desc->id, i, ®ion_desc);
704 "dprc_get_obj_region() failed: %d\n", error);
705 goto error_cleanup_regions;
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
712 if (region_desc.base_address) {
713 regions[i].start = region_desc.base_address +
714 region_desc.base_offset;
716 error = translate_mc_addr(mc_dev, mc_region_type,
717 region_desc.base_offset,
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.
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;
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;
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;
750 mc_dev->regions = regions;
753 error_cleanup_regions:
759 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
761 bool fsl_mc_is_root_dprc(struct device *dev)
763 struct device *root_dprc_dev;
765 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
768 return dev == root_dprc_dev;
771 static void fsl_mc_device_release(struct device *dev)
773 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
775 kfree(mc_dev->regions);
777 if (is_fsl_mc_bus_dprc(mc_dev))
778 kfree(to_fsl_mc_bus(mc_dev));
784 * Add a newly discovered fsl-mc device to be visible in Linux
786 int 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)
792 struct fsl_mc_device *mc_dev = NULL;
793 struct fsl_mc_bus *mc_bus = NULL;
794 struct fsl_mc_device *parent_mc_dev;
796 if (dev_is_fsl_mc(parent_dev))
797 parent_mc_dev = to_fsl_mc_device(parent_dev);
799 parent_mc_dev = NULL;
801 if (strcmp(obj_desc->type, "dprc") == 0) {
803 * Allocate an MC bus device object:
805 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
809 mutex_init(&mc_bus->scan_mutex);
810 mc_dev = &mc_bus->mc_dev;
813 * Allocate a regular fsl_mc_device object:
815 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
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) {
829 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
830 goto error_cleanup_dev;
832 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
834 if (strcmp(obj_desc->type, "dprc") == 0) {
835 struct fsl_mc_io *mc_io2;
837 mc_dev->flags |= FSL_MC_IS_DPRC;
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).
846 * NOTE: There cannot be more than one active open for a
847 * given MC object, using the same MC portal.
851 * device being added is a child DPRC device
853 mc_io2 = parent_mc_dev->mc_io;
856 * device being added is the root DPRC device
860 goto error_cleanup_dev;
866 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
868 goto error_cleanup_dev;
871 * A non-DPRC object has to be a child of a DPRC, use the
872 * parent's ICID and interrupt domain.
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));
883 * Get MMIO regions for the device from the MC:
885 * NOTE: the root DPRC is a special case as its MMIO region is
886 * obtained from the device tree
888 if (parent_mc_dev && obj_desc->region_count != 0) {
889 error = fsl_mc_device_get_mmio_regions(mc_dev,
892 goto error_cleanup_dev;
896 * The device-specific probe callback will get invoked by device_add()
898 error = device_add(&mc_dev->dev);
901 "device_add() failed for device %s: %d\n",
902 dev_name(&mc_dev->dev), error);
903 goto error_cleanup_dev;
906 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
908 *new_mc_dev = mc_dev;
912 kfree(mc_dev->regions);
918 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
920 static struct notifier_block fsl_mc_nb;
923 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
926 * @mc_dev: Pointer to an fsl-mc device
928 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
930 kfree(mc_dev->driver_override);
931 mc_dev->driver_override = NULL;
934 * The device-specific remove callback will get invoked by device_del()
936 device_del(&mc_dev->dev);
937 put_device(&mc_dev->dev);
939 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
941 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
944 struct fsl_mc_device *mc_bus_dev, *endpoint;
945 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
946 struct dprc_endpoint endpoint1 = {{ 0 }};
947 struct dprc_endpoint endpoint2 = {{ 0 }};
950 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
951 strcpy(endpoint1.type, mc_dev->obj_desc.type);
952 endpoint1.id = mc_dev->obj_desc.id;
953 endpoint1.if_id = if_id;
955 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
956 mc_bus_dev->mc_handle,
957 &endpoint1, &endpoint2,
960 if (err == -ENOTCONN || state == -1)
961 return ERR_PTR(-ENOTCONN);
964 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
968 strcpy(endpoint_desc.type, endpoint2.type);
969 endpoint_desc.id = endpoint2.id;
970 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
973 * We know that the device has an endpoint because we verified by
974 * interrogating the firmware. This is the case when the device was not
975 * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
976 * Force a rescan of the devices in this container and retry the lookup.
979 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
981 if (mutex_trylock(&mc_bus->scan_mutex)) {
982 err = dprc_scan_objects(mc_bus_dev, true);
983 mutex_unlock(&mc_bus->scan_mutex);
990 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
992 * This means that the endpoint might reside in a different isolation
993 * context (DPRC/container). Not much to do, so return a permssion
997 return ERR_PTR(-EPERM);
1001 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
1003 static int parse_mc_ranges(struct device *dev,
1007 const __be32 **ranges_start)
1010 int range_tuple_cell_count;
1013 struct device_node *mc_node = dev->of_node;
1015 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
1016 if (!(*ranges_start) || !ranges_len) {
1018 "missing or empty ranges property for device tree node '%pOFn'\n",
1023 *paddr_cells = of_n_addr_cells(mc_node);
1025 prop = of_get_property(mc_node, "#address-cells", NULL);
1027 *mc_addr_cells = be32_to_cpup(prop);
1029 *mc_addr_cells = *paddr_cells;
1031 prop = of_get_property(mc_node, "#size-cells", NULL);
1033 *mc_size_cells = be32_to_cpup(prop);
1035 *mc_size_cells = of_n_size_cells(mc_node);
1037 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
1040 tuple_len = range_tuple_cell_count * sizeof(__be32);
1041 if (ranges_len % tuple_len != 0) {
1042 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
1046 return ranges_len / tuple_len;
1049 static int get_mc_addr_translation_ranges(struct device *dev,
1050 struct fsl_mc_addr_translation_range
1059 const __be32 *ranges_start;
1062 ret = parse_mc_ranges(dev,
1073 * Missing or empty ranges property ("ranges;") for the
1074 * 'fsl,qoriq-mc' node. In this case, identity mapping
1081 *ranges = devm_kcalloc(dev, *num_ranges,
1082 sizeof(struct fsl_mc_addr_translation_range),
1087 cell = ranges_start;
1088 for (i = 0; i < *num_ranges; ++i) {
1089 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
1091 range->mc_region_type = of_read_number(cell, 1);
1092 range->start_mc_offset = of_read_number(cell + 1,
1094 cell += mc_addr_cells;
1095 range->start_phys_addr = of_read_number(cell, paddr_cells);
1096 cell += paddr_cells;
1097 range->end_mc_offset = range->start_mc_offset +
1098 of_read_number(cell, mc_size_cells);
1100 cell += mc_size_cells;
1107 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
1110 static int fsl_mc_bus_probe(struct platform_device *pdev)
1112 struct fsl_mc_obj_desc obj_desc;
1115 struct fsl_mc_device *mc_bus_dev = NULL;
1116 struct fsl_mc_io *mc_io = NULL;
1118 phys_addr_t mc_portal_phys_addr;
1119 u32 mc_portal_size, mc_stream_id;
1120 struct resource *plat_res;
1122 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1126 platform_set_drvdata(pdev, mc);
1128 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1130 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1131 if (IS_ERR(mc->fsl_mc_regs))
1132 return PTR_ERR(mc->fsl_mc_regs);
1135 if (mc->fsl_mc_regs) {
1136 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1137 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1139 * HW ORs the PL and BMT bit, places the result in bit
1140 * 14 of the StreamID and ORs in the ICID. Calculate it
1143 mc_stream_id = (mc_stream_id & 0xffff) |
1144 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1146 error = acpi_dma_configure_id(&pdev->dev,
1149 if (error == -EPROBE_DEFER)
1152 dev_warn(&pdev->dev,
1153 "failed to configure dma: %d.\n",
1158 * Some bootloaders pause the MC firmware before booting the
1159 * kernel so that MC will not cause faults as soon as the
1160 * SMMU probes due to the fact that there's no configuration
1162 * At this point MC should have all its SMMU setup done so make
1163 * sure it is resumed.
1165 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
1166 (~(GCR1_P1_STOP | GCR1_P2_STOP)),
1167 mc->fsl_mc_regs + FSL_MC_GCR1);
1171 * Get physical address of MC portal for the root DPRC:
1173 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1174 mc_portal_phys_addr = plat_res->start;
1175 mc_portal_size = resource_size(plat_res);
1176 mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
1178 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1179 mc_portal_size, NULL,
1180 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1184 error = mc_get_version(mc_io, 0, &mc_version);
1187 "mc_get_version() failed with error %d\n", error);
1188 goto error_cleanup_mc_io;
1191 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1192 mc_version.major, mc_version.minor, mc_version.revision);
1194 if (dev_of_node(&pdev->dev)) {
1195 error = get_mc_addr_translation_ranges(&pdev->dev,
1196 &mc->translation_ranges,
1197 &mc->num_translation_ranges);
1199 goto error_cleanup_mc_io;
1202 error = dprc_get_container_id(mc_io, 0, &container_id);
1205 "dprc_get_container_id() failed: %d\n", error);
1206 goto error_cleanup_mc_io;
1209 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1210 error = dprc_get_api_version(mc_io, 0,
1211 &obj_desc.ver_major,
1212 &obj_desc.ver_minor);
1214 goto error_cleanup_mc_io;
1216 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1217 strcpy(obj_desc.type, "dprc");
1218 obj_desc.id = container_id;
1219 obj_desc.irq_count = 1;
1220 obj_desc.region_count = 0;
1222 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1224 goto error_cleanup_mc_io;
1226 mc->root_mc_bus_dev = mc_bus_dev;
1227 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1230 error_cleanup_mc_io:
1231 fsl_destroy_mc_io(mc_io);
1236 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1239 static int fsl_mc_bus_remove(struct platform_device *pdev)
1241 struct fsl_mc *mc = platform_get_drvdata(pdev);
1242 struct fsl_mc_io *mc_io;
1244 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1247 mc_io = mc->root_mc_bus_dev->mc_io;
1248 fsl_mc_device_remove(mc->root_mc_bus_dev);
1249 fsl_destroy_mc_io(mc_io);
1251 bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
1253 if (mc->fsl_mc_regs) {
1255 * Pause the MC firmware so that it doesn't crash in certain
1256 * scenarios, such as kexec.
1258 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
1259 (GCR1_P1_STOP | GCR1_P2_STOP),
1260 mc->fsl_mc_regs + FSL_MC_GCR1);
1266 static void fsl_mc_bus_shutdown(struct platform_device *pdev)
1268 fsl_mc_bus_remove(pdev);
1271 static const struct of_device_id fsl_mc_bus_match_table[] = {
1272 {.compatible = "fsl,qoriq-mc",},
1276 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1278 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1282 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1284 static struct platform_driver fsl_mc_bus_driver = {
1286 .name = "fsl_mc_bus",
1288 .of_match_table = fsl_mc_bus_match_table,
1289 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1291 .probe = fsl_mc_bus_probe,
1292 .remove = fsl_mc_bus_remove,
1293 .shutdown = fsl_mc_bus_shutdown,
1296 static int fsl_mc_bus_notifier(struct notifier_block *nb,
1297 unsigned long action, void *data)
1299 struct device *dev = data;
1300 struct resource *res;
1301 void __iomem *fsl_mc_regs;
1303 if (action != BUS_NOTIFY_ADD_DEVICE)
1306 if (!of_match_device(fsl_mc_bus_match_table, dev) &&
1307 !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
1310 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
1314 fsl_mc_regs = ioremap(res->start, resource_size(res));
1319 * Make sure that the MC firmware is paused before the IOMMU setup for
1320 * it is done or otherwise the firmware will crash right after the SMMU
1321 * gets probed and enabled.
1323 writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
1324 fsl_mc_regs + FSL_MC_GCR1);
1325 iounmap(fsl_mc_regs);
1330 static struct notifier_block fsl_mc_nb = {
1331 .notifier_call = fsl_mc_bus_notifier,
1334 static int __init fsl_mc_bus_driver_init(void)
1338 error = bus_register(&fsl_mc_bus_type);
1340 pr_err("bus type registration failed: %d\n", error);
1341 goto error_cleanup_cache;
1344 error = platform_driver_register(&fsl_mc_bus_driver);
1346 pr_err("platform_driver_register() failed: %d\n", error);
1347 goto error_cleanup_bus;
1350 error = dprc_driver_init();
1352 goto error_cleanup_driver;
1354 error = fsl_mc_allocator_driver_init();
1356 goto error_cleanup_dprc_driver;
1358 return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
1360 error_cleanup_dprc_driver:
1363 error_cleanup_driver:
1364 platform_driver_unregister(&fsl_mc_bus_driver);
1367 bus_unregister(&fsl_mc_bus_type);
1369 error_cleanup_cache:
1372 postcore_initcall(fsl_mc_bus_driver_init);