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/dma-mapping.h>
21 #include <linux/acpi.h>
22 #include <linux/iommu.h>
23 #include <linux/dma-map-ops.h>
25 #include "fsl-mc-private.h"
28 * Default DMA mask for devices on a fsl-mc bus
30 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
32 static struct fsl_mc_version mc_version;
35 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
36 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
37 * @num_translation_ranges: number of entries in addr_translation_ranges
38 * @translation_ranges: array of bus to system address translation ranges
39 * @fsl_mc_regs: base address of register bank
42 struct fsl_mc_device *root_mc_bus_dev;
43 u8 num_translation_ranges;
44 struct fsl_mc_addr_translation_range *translation_ranges;
45 void __iomem *fsl_mc_regs;
49 * struct fsl_mc_addr_translation_range - bus to system address translation
51 * @mc_region_type: Type of MC region for the range being translated
52 * @start_mc_offset: Start MC offset of the range being translated
53 * @end_mc_offset: MC offset of the first byte after the range (last MC
54 * offset of the range is end_mc_offset - 1)
55 * @start_phys_addr: system physical address corresponding to start_mc_addr
57 struct fsl_mc_addr_translation_range {
58 enum dprc_region_type mc_region_type;
61 phys_addr_t start_phys_addr;
64 #define FSL_MC_GCR1 0x0
65 #define GCR1_P1_STOP BIT(31)
66 #define GCR1_P2_STOP BIT(30)
68 #define FSL_MC_FAPR 0x28
69 #define MC_FAPR_PL BIT(18)
70 #define MC_FAPR_BMT BIT(17)
72 static phys_addr_t mc_portal_base_phys_addr;
75 * fsl_mc_bus_match - device to driver matching callback
76 * @dev: the fsl-mc device to match against
77 * @drv: the device driver to search for matching fsl-mc object type
80 * Returns 1 on success, 0 otherwise.
82 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
84 const struct fsl_mc_device_id *id;
85 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
86 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
89 /* When driver_override is set, only bind to the matching driver */
90 if (mc_dev->driver_override) {
91 found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
95 if (!mc_drv->match_id_table)
99 * If the object is not 'plugged' don't match.
100 * Only exception is the root DPRC, which is a special case.
102 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
103 !fsl_mc_is_root_dprc(&mc_dev->dev))
107 * Traverse the match_id table of the given driver, trying to find
108 * a matching for the given device.
110 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
111 if (id->vendor == mc_dev->obj_desc.vendor &&
112 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
120 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
125 * fsl_mc_bus_uevent - callback invoked when a device is added
127 static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
129 const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
131 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
132 mc_dev->obj_desc.vendor,
133 mc_dev->obj_desc.type))
139 static int fsl_mc_dma_configure(struct device *dev)
141 struct device *dma_dev = dev;
142 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
143 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
144 u32 input_id = mc_dev->icid;
147 while (dev_is_fsl_mc(dma_dev))
148 dma_dev = dma_dev->parent;
150 if (dev_of_node(dma_dev))
151 ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
153 ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
155 if (!ret && !mc_drv->driver_managed_dma) {
156 ret = iommu_device_use_default_domain(dev);
158 arch_teardown_dma_ops(dev);
164 static void fsl_mc_dma_cleanup(struct device *dev)
166 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
168 if (!mc_drv->driver_managed_dma)
169 iommu_device_unuse_default_domain(dev);
172 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
175 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
177 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
178 mc_dev->obj_desc.type);
180 static DEVICE_ATTR_RO(modalias);
182 static ssize_t driver_override_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
186 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
189 if (WARN_ON(dev->bus != &fsl_mc_bus_type))
192 ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
199 static ssize_t driver_override_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
202 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
204 return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
206 static DEVICE_ATTR_RW(driver_override);
208 static struct attribute *fsl_mc_dev_attrs[] = {
209 &dev_attr_modalias.attr,
210 &dev_attr_driver_override.attr,
214 ATTRIBUTE_GROUPS(fsl_mc_dev);
216 static int scan_fsl_mc_bus(struct device *dev, void *data)
218 struct fsl_mc_device *root_mc_dev;
219 struct fsl_mc_bus *root_mc_bus;
221 if (!fsl_mc_is_root_dprc(dev))
224 root_mc_dev = to_fsl_mc_device(dev);
225 root_mc_bus = to_fsl_mc_bus(root_mc_dev);
226 mutex_lock(&root_mc_bus->scan_mutex);
227 dprc_scan_objects(root_mc_dev, false);
228 mutex_unlock(&root_mc_bus->scan_mutex);
234 static ssize_t rescan_store(struct bus_type *bus,
235 const char *buf, size_t count)
239 if (kstrtoul(buf, 0, &val) < 0)
243 bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
247 static BUS_ATTR_WO(rescan);
249 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
251 struct fsl_mc_device *root_mc_dev;
255 if (!fsl_mc_is_root_dprc(dev))
258 root_mc_dev = to_fsl_mc_device(dev);
260 if (kstrtoul(buf, 0, &val) < 0)
264 enable_dprc_irq(root_mc_dev);
266 disable_dprc_irq(root_mc_dev);
272 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
274 struct fsl_mc_device *root_mc_dev;
277 if (!fsl_mc_is_root_dprc(dev))
280 root_mc_dev = to_fsl_mc_device(dev);
282 sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
287 static ssize_t autorescan_store(struct bus_type *bus,
288 const char *buf, size_t count)
290 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
295 static ssize_t autorescan_show(struct bus_type *bus, char *buf)
297 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
301 static BUS_ATTR_RW(autorescan);
303 static struct attribute *fsl_mc_bus_attrs[] = {
304 &bus_attr_rescan.attr,
305 &bus_attr_autorescan.attr,
309 ATTRIBUTE_GROUPS(fsl_mc_bus);
311 struct bus_type fsl_mc_bus_type = {
313 .match = fsl_mc_bus_match,
314 .uevent = fsl_mc_bus_uevent,
315 .dma_configure = fsl_mc_dma_configure,
316 .dma_cleanup = fsl_mc_dma_cleanup,
317 .dev_groups = fsl_mc_dev_groups,
318 .bus_groups = fsl_mc_bus_groups,
320 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
322 struct device_type fsl_mc_bus_dprc_type = {
323 .name = "fsl_mc_bus_dprc"
325 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
327 struct device_type fsl_mc_bus_dpni_type = {
328 .name = "fsl_mc_bus_dpni"
330 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
332 struct device_type fsl_mc_bus_dpio_type = {
333 .name = "fsl_mc_bus_dpio"
335 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
337 struct device_type fsl_mc_bus_dpsw_type = {
338 .name = "fsl_mc_bus_dpsw"
340 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
342 struct device_type fsl_mc_bus_dpbp_type = {
343 .name = "fsl_mc_bus_dpbp"
345 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
347 struct device_type fsl_mc_bus_dpcon_type = {
348 .name = "fsl_mc_bus_dpcon"
350 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
352 struct device_type fsl_mc_bus_dpmcp_type = {
353 .name = "fsl_mc_bus_dpmcp"
355 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
357 struct device_type fsl_mc_bus_dpmac_type = {
358 .name = "fsl_mc_bus_dpmac"
360 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
362 struct device_type fsl_mc_bus_dprtc_type = {
363 .name = "fsl_mc_bus_dprtc"
365 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
367 struct device_type fsl_mc_bus_dpseci_type = {
368 .name = "fsl_mc_bus_dpseci"
370 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
372 struct device_type fsl_mc_bus_dpdmux_type = {
373 .name = "fsl_mc_bus_dpdmux"
375 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
377 struct device_type fsl_mc_bus_dpdcei_type = {
378 .name = "fsl_mc_bus_dpdcei"
380 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
382 struct device_type fsl_mc_bus_dpaiop_type = {
383 .name = "fsl_mc_bus_dpaiop"
385 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
387 struct device_type fsl_mc_bus_dpci_type = {
388 .name = "fsl_mc_bus_dpci"
390 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
392 struct device_type fsl_mc_bus_dpdmai_type = {
393 .name = "fsl_mc_bus_dpdmai"
395 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
397 struct device_type fsl_mc_bus_dpdbg_type = {
398 .name = "fsl_mc_bus_dpdbg"
400 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
402 static struct device_type *fsl_mc_get_device_type(const char *type)
404 static const struct {
405 struct device_type *dev_type;
408 { &fsl_mc_bus_dprc_type, "dprc" },
409 { &fsl_mc_bus_dpni_type, "dpni" },
410 { &fsl_mc_bus_dpio_type, "dpio" },
411 { &fsl_mc_bus_dpsw_type, "dpsw" },
412 { &fsl_mc_bus_dpbp_type, "dpbp" },
413 { &fsl_mc_bus_dpcon_type, "dpcon" },
414 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
415 { &fsl_mc_bus_dpmac_type, "dpmac" },
416 { &fsl_mc_bus_dprtc_type, "dprtc" },
417 { &fsl_mc_bus_dpseci_type, "dpseci" },
418 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
419 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
420 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
421 { &fsl_mc_bus_dpci_type, "dpci" },
422 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
423 { &fsl_mc_bus_dpdbg_type, "dpdbg" },
428 for (i = 0; dev_types[i].dev_type; i++)
429 if (!strcmp(dev_types[i].type, type))
430 return dev_types[i].dev_type;
435 static int fsl_mc_driver_probe(struct device *dev)
437 struct fsl_mc_driver *mc_drv;
438 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
441 mc_drv = to_fsl_mc_driver(dev->driver);
443 error = mc_drv->probe(mc_dev);
445 if (error != -EPROBE_DEFER)
446 dev_err(dev, "%s failed: %d\n", __func__, error);
453 static int fsl_mc_driver_remove(struct device *dev)
455 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
456 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
459 error = mc_drv->remove(mc_dev);
461 dev_err(dev, "%s failed: %d\n", __func__, error);
468 static void fsl_mc_driver_shutdown(struct device *dev)
470 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
471 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
473 mc_drv->shutdown(mc_dev);
477 * __fsl_mc_driver_register - registers a child device driver with the
480 * This function is implicitly invoked from the registration function of
481 * fsl_mc device drivers, which is generated by the
482 * module_fsl_mc_driver() macro.
484 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
485 struct module *owner)
489 mc_driver->driver.owner = owner;
490 mc_driver->driver.bus = &fsl_mc_bus_type;
492 if (mc_driver->probe)
493 mc_driver->driver.probe = fsl_mc_driver_probe;
495 if (mc_driver->remove)
496 mc_driver->driver.remove = fsl_mc_driver_remove;
498 if (mc_driver->shutdown)
499 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
501 error = driver_register(&mc_driver->driver);
503 pr_err("driver_register() failed for %s: %d\n",
504 mc_driver->driver.name, error);
510 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
513 * fsl_mc_driver_unregister - unregisters a device driver from the
516 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
518 driver_unregister(&mc_driver->driver);
520 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
523 * mc_get_version() - Retrieves the Management Complex firmware
524 * version information
525 * @mc_io: Pointer to opaque I/O object
526 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
527 * @mc_ver_info: Returned version information structure
529 * Return: '0' on Success; Error code otherwise.
531 static int mc_get_version(struct fsl_mc_io *mc_io,
533 struct fsl_mc_version *mc_ver_info)
535 struct fsl_mc_command cmd = { 0 };
536 struct dpmng_rsp_get_version *rsp_params;
539 /* prepare command */
540 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
544 /* send command to mc*/
545 err = mc_send_command(mc_io, &cmd);
549 /* retrieve response parameters */
550 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
551 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
552 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
553 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
559 * fsl_mc_get_version - function to retrieve the MC f/w version information
561 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
563 struct fsl_mc_version *fsl_mc_get_version(void)
565 if (mc_version.major)
570 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
573 * fsl_mc_get_root_dprc - function to traverse to the root dprc
575 void fsl_mc_get_root_dprc(struct device *dev,
576 struct device **root_dprc_dev)
579 *root_dprc_dev = NULL;
580 } else if (!dev_is_fsl_mc(dev)) {
581 *root_dprc_dev = NULL;
583 *root_dprc_dev = dev;
584 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
585 *root_dprc_dev = (*root_dprc_dev)->parent;
589 static int get_dprc_attr(struct fsl_mc_io *mc_io,
590 int container_id, struct dprc_attributes *attr)
595 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
597 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
601 memset(attr, 0, sizeof(struct dprc_attributes));
602 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
604 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
612 (void)dprc_close(mc_io, 0, dprc_handle);
616 static int get_dprc_icid(struct fsl_mc_io *mc_io,
617 int container_id, u32 *icid)
619 struct dprc_attributes attr;
622 error = get_dprc_attr(mc_io, container_id, &attr);
629 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
630 enum dprc_region_type mc_region_type,
631 u64 mc_offset, phys_addr_t *phys_addr)
634 struct device *root_dprc_dev;
637 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
638 mc = dev_get_drvdata(root_dprc_dev->parent);
640 if (mc->num_translation_ranges == 0) {
642 * Do identity mapping:
644 *phys_addr = mc_offset;
648 for (i = 0; i < mc->num_translation_ranges; i++) {
649 struct fsl_mc_addr_translation_range *range =
650 &mc->translation_ranges[i];
652 if (mc_region_type == range->mc_region_type &&
653 mc_offset >= range->start_mc_offset &&
654 mc_offset < range->end_mc_offset) {
655 *phys_addr = range->start_phys_addr +
656 (mc_offset - range->start_mc_offset);
664 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
665 struct fsl_mc_device *mc_bus_dev)
669 struct resource *regions;
670 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
671 struct device *parent_dev = mc_dev->dev.parent;
672 enum dprc_region_type mc_region_type;
674 if (is_fsl_mc_bus_dprc(mc_dev) ||
675 is_fsl_mc_bus_dpmcp(mc_dev)) {
676 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
677 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
678 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
681 * This function should not have been called for this MC object
682 * type, as this object type is not supposed to have MMIO
688 regions = kmalloc_array(obj_desc->region_count,
689 sizeof(regions[0]), GFP_KERNEL);
693 for (i = 0; i < obj_desc->region_count; i++) {
694 struct dprc_region_desc region_desc;
696 error = dprc_get_obj_region(mc_bus_dev->mc_io,
698 mc_bus_dev->mc_handle,
700 obj_desc->id, i, ®ion_desc);
703 "dprc_get_obj_region() failed: %d\n", error);
704 goto error_cleanup_regions;
707 * Older MC only returned region offset and no base address
708 * If base address is in the region_desc use it otherwise
709 * revert to old mechanism
711 if (region_desc.base_address) {
712 regions[i].start = region_desc.base_address +
713 region_desc.base_offset;
715 error = translate_mc_addr(mc_dev, mc_region_type,
716 region_desc.base_offset,
720 * Some versions of the MC firmware wrongly report
721 * 0 for register base address of the DPMCP associated
722 * with child DPRC objects thus rendering them unusable.
723 * This is particularly troublesome in ACPI boot
724 * scenarios where the legacy way of extracting this
725 * base address from the device tree does not apply.
726 * Given that DPMCPs share the same base address,
727 * workaround this by using the base address extracted
728 * from the root DPRC container.
730 if (is_fsl_mc_bus_dprc(mc_dev) &&
731 regions[i].start == region_desc.base_offset)
732 regions[i].start += mc_portal_base_phys_addr;
737 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
738 region_desc.base_offset,
739 obj_desc->type, obj_desc->id, i);
740 goto error_cleanup_regions;
743 regions[i].end = regions[i].start + region_desc.size - 1;
744 regions[i].name = "fsl-mc object MMIO region";
745 regions[i].flags = region_desc.flags & IORESOURCE_BITS;
746 regions[i].flags |= IORESOURCE_MEM;
749 mc_dev->regions = regions;
752 error_cleanup_regions:
758 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
760 bool fsl_mc_is_root_dprc(struct device *dev)
762 struct device *root_dprc_dev;
764 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
767 return dev == root_dprc_dev;
770 static void fsl_mc_device_release(struct device *dev)
772 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
774 kfree(mc_dev->regions);
776 if (is_fsl_mc_bus_dprc(mc_dev))
777 kfree(to_fsl_mc_bus(mc_dev));
783 * Add a newly discovered fsl-mc device to be visible in Linux
785 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
786 struct fsl_mc_io *mc_io,
787 struct device *parent_dev,
788 struct fsl_mc_device **new_mc_dev)
791 struct fsl_mc_device *mc_dev = NULL;
792 struct fsl_mc_bus *mc_bus = NULL;
793 struct fsl_mc_device *parent_mc_dev;
795 if (dev_is_fsl_mc(parent_dev))
796 parent_mc_dev = to_fsl_mc_device(parent_dev);
798 parent_mc_dev = NULL;
800 if (strcmp(obj_desc->type, "dprc") == 0) {
802 * Allocate an MC bus device object:
804 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
808 mutex_init(&mc_bus->scan_mutex);
809 mc_dev = &mc_bus->mc_dev;
812 * Allocate a regular fsl_mc_device object:
814 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
819 mc_dev->obj_desc = *obj_desc;
820 mc_dev->mc_io = mc_io;
821 device_initialize(&mc_dev->dev);
822 mc_dev->dev.parent = parent_dev;
823 mc_dev->dev.bus = &fsl_mc_bus_type;
824 mc_dev->dev.release = fsl_mc_device_release;
825 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
826 if (!mc_dev->dev.type) {
828 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
829 goto error_cleanup_dev;
831 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
833 if (strcmp(obj_desc->type, "dprc") == 0) {
834 struct fsl_mc_io *mc_io2;
836 mc_dev->flags |= FSL_MC_IS_DPRC;
839 * To get the DPRC's ICID, we need to open the DPRC
840 * in get_dprc_icid(). For child DPRCs, we do so using the
841 * parent DPRC's MC portal instead of the child DPRC's MC
842 * portal, in case the child DPRC is already opened with
843 * its own portal (e.g., the DPRC used by AIOP).
845 * NOTE: There cannot be more than one active open for a
846 * given MC object, using the same MC portal.
850 * device being added is a child DPRC device
852 mc_io2 = parent_mc_dev->mc_io;
855 * device being added is the root DPRC device
859 goto error_cleanup_dev;
865 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
867 goto error_cleanup_dev;
870 * A non-DPRC object has to be a child of a DPRC, use the
871 * parent's ICID and interrupt domain.
873 mc_dev->icid = parent_mc_dev->icid;
874 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
875 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
876 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
877 dev_set_msi_domain(&mc_dev->dev,
878 dev_get_msi_domain(&parent_mc_dev->dev));
882 * Get MMIO regions for the device from the MC:
884 * NOTE: the root DPRC is a special case as its MMIO region is
885 * obtained from the device tree
887 if (parent_mc_dev && obj_desc->region_count != 0) {
888 error = fsl_mc_device_get_mmio_regions(mc_dev,
891 goto error_cleanup_dev;
895 * The device-specific probe callback will get invoked by device_add()
897 error = device_add(&mc_dev->dev);
900 "device_add() failed for device %s: %d\n",
901 dev_name(&mc_dev->dev), error);
902 goto error_cleanup_dev;
905 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
907 *new_mc_dev = mc_dev;
911 kfree(mc_dev->regions);
917 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
919 static struct notifier_block fsl_mc_nb;
922 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
925 * @mc_dev: Pointer to an fsl-mc device
927 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
929 kfree(mc_dev->driver_override);
930 mc_dev->driver_override = NULL;
933 * The device-specific remove callback will get invoked by device_del()
935 device_del(&mc_dev->dev);
936 put_device(&mc_dev->dev);
938 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
940 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
943 struct fsl_mc_device *mc_bus_dev, *endpoint;
944 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
945 struct dprc_endpoint endpoint1 = {{ 0 }};
946 struct dprc_endpoint endpoint2 = {{ 0 }};
949 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
950 strcpy(endpoint1.type, mc_dev->obj_desc.type);
951 endpoint1.id = mc_dev->obj_desc.id;
952 endpoint1.if_id = if_id;
954 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
955 mc_bus_dev->mc_handle,
956 &endpoint1, &endpoint2,
959 if (err == -ENOTCONN || state == -1)
960 return ERR_PTR(-ENOTCONN);
963 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
967 strcpy(endpoint_desc.type, endpoint2.type);
968 endpoint_desc.id = endpoint2.id;
969 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
972 * We know that the device has an endpoint because we verified by
973 * interrogating the firmware. This is the case when the device was not
974 * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
975 * Force a rescan of the devices in this container and retry the lookup.
978 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
980 if (mutex_trylock(&mc_bus->scan_mutex)) {
981 err = dprc_scan_objects(mc_bus_dev, true);
982 mutex_unlock(&mc_bus->scan_mutex);
989 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
991 * This means that the endpoint might reside in a different isolation
992 * context (DPRC/container). Not much to do, so return a permssion
996 return ERR_PTR(-EPERM);
1000 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
1002 static int parse_mc_ranges(struct device *dev,
1006 const __be32 **ranges_start)
1009 int range_tuple_cell_count;
1012 struct device_node *mc_node = dev->of_node;
1014 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
1015 if (!(*ranges_start) || !ranges_len) {
1017 "missing or empty ranges property for device tree node '%pOFn'\n",
1022 *paddr_cells = of_n_addr_cells(mc_node);
1024 prop = of_get_property(mc_node, "#address-cells", NULL);
1026 *mc_addr_cells = be32_to_cpup(prop);
1028 *mc_addr_cells = *paddr_cells;
1030 prop = of_get_property(mc_node, "#size-cells", NULL);
1032 *mc_size_cells = be32_to_cpup(prop);
1034 *mc_size_cells = of_n_size_cells(mc_node);
1036 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
1039 tuple_len = range_tuple_cell_count * sizeof(__be32);
1040 if (ranges_len % tuple_len != 0) {
1041 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
1045 return ranges_len / tuple_len;
1048 static int get_mc_addr_translation_ranges(struct device *dev,
1049 struct fsl_mc_addr_translation_range
1058 const __be32 *ranges_start;
1061 ret = parse_mc_ranges(dev,
1072 * Missing or empty ranges property ("ranges;") for the
1073 * 'fsl,qoriq-mc' node. In this case, identity mapping
1080 *ranges = devm_kcalloc(dev, *num_ranges,
1081 sizeof(struct fsl_mc_addr_translation_range),
1086 cell = ranges_start;
1087 for (i = 0; i < *num_ranges; ++i) {
1088 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
1090 range->mc_region_type = of_read_number(cell, 1);
1091 range->start_mc_offset = of_read_number(cell + 1,
1093 cell += mc_addr_cells;
1094 range->start_phys_addr = of_read_number(cell, paddr_cells);
1095 cell += paddr_cells;
1096 range->end_mc_offset = range->start_mc_offset +
1097 of_read_number(cell, mc_size_cells);
1099 cell += mc_size_cells;
1106 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
1109 static int fsl_mc_bus_probe(struct platform_device *pdev)
1111 struct fsl_mc_obj_desc obj_desc;
1114 struct fsl_mc_device *mc_bus_dev = NULL;
1115 struct fsl_mc_io *mc_io = NULL;
1117 phys_addr_t mc_portal_phys_addr;
1118 u32 mc_portal_size, mc_stream_id;
1119 struct resource *plat_res;
1121 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1125 platform_set_drvdata(pdev, mc);
1127 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1129 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1130 if (IS_ERR(mc->fsl_mc_regs))
1131 return PTR_ERR(mc->fsl_mc_regs);
1134 if (mc->fsl_mc_regs) {
1135 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1136 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1138 * HW ORs the PL and BMT bit, places the result in bit
1139 * 14 of the StreamID and ORs in the ICID. Calculate it
1142 mc_stream_id = (mc_stream_id & 0xffff) |
1143 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1145 error = acpi_dma_configure_id(&pdev->dev,
1148 if (error == -EPROBE_DEFER)
1151 dev_warn(&pdev->dev,
1152 "failed to configure dma: %d.\n",
1157 * Some bootloaders pause the MC firmware before booting the
1158 * kernel so that MC will not cause faults as soon as the
1159 * SMMU probes due to the fact that there's no configuration
1161 * At this point MC should have all its SMMU setup done so make
1162 * sure it is resumed.
1164 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
1165 (~(GCR1_P1_STOP | GCR1_P2_STOP)),
1166 mc->fsl_mc_regs + FSL_MC_GCR1);
1170 * Get physical address of MC portal for the root DPRC:
1172 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1173 mc_portal_phys_addr = plat_res->start;
1174 mc_portal_size = resource_size(plat_res);
1175 mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
1177 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1178 mc_portal_size, NULL,
1179 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1183 error = mc_get_version(mc_io, 0, &mc_version);
1186 "mc_get_version() failed with error %d\n", error);
1187 goto error_cleanup_mc_io;
1190 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1191 mc_version.major, mc_version.minor, mc_version.revision);
1193 if (dev_of_node(&pdev->dev)) {
1194 error = get_mc_addr_translation_ranges(&pdev->dev,
1195 &mc->translation_ranges,
1196 &mc->num_translation_ranges);
1198 goto error_cleanup_mc_io;
1201 error = dprc_get_container_id(mc_io, 0, &container_id);
1204 "dprc_get_container_id() failed: %d\n", error);
1205 goto error_cleanup_mc_io;
1208 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1209 error = dprc_get_api_version(mc_io, 0,
1210 &obj_desc.ver_major,
1211 &obj_desc.ver_minor);
1213 goto error_cleanup_mc_io;
1215 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1216 strcpy(obj_desc.type, "dprc");
1217 obj_desc.id = container_id;
1218 obj_desc.irq_count = 1;
1219 obj_desc.region_count = 0;
1221 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1223 goto error_cleanup_mc_io;
1225 mc->root_mc_bus_dev = mc_bus_dev;
1226 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1229 error_cleanup_mc_io:
1230 fsl_destroy_mc_io(mc_io);
1235 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1238 static int fsl_mc_bus_remove(struct platform_device *pdev)
1240 struct fsl_mc *mc = platform_get_drvdata(pdev);
1241 struct fsl_mc_io *mc_io;
1243 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1246 mc_io = mc->root_mc_bus_dev->mc_io;
1247 fsl_mc_device_remove(mc->root_mc_bus_dev);
1248 fsl_destroy_mc_io(mc_io);
1250 bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
1252 if (mc->fsl_mc_regs) {
1254 * Pause the MC firmware so that it doesn't crash in certain
1255 * scenarios, such as kexec.
1257 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
1258 (GCR1_P1_STOP | GCR1_P2_STOP),
1259 mc->fsl_mc_regs + FSL_MC_GCR1);
1265 static void fsl_mc_bus_shutdown(struct platform_device *pdev)
1267 fsl_mc_bus_remove(pdev);
1270 static const struct of_device_id fsl_mc_bus_match_table[] = {
1271 {.compatible = "fsl,qoriq-mc",},
1275 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1277 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1281 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1283 static struct platform_driver fsl_mc_bus_driver = {
1285 .name = "fsl_mc_bus",
1287 .of_match_table = fsl_mc_bus_match_table,
1288 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1290 .probe = fsl_mc_bus_probe,
1291 .remove = fsl_mc_bus_remove,
1292 .shutdown = fsl_mc_bus_shutdown,
1295 static int fsl_mc_bus_notifier(struct notifier_block *nb,
1296 unsigned long action, void *data)
1298 struct device *dev = data;
1299 struct resource *res;
1300 void __iomem *fsl_mc_regs;
1302 if (action != BUS_NOTIFY_ADD_DEVICE)
1305 if (!of_match_device(fsl_mc_bus_match_table, dev) &&
1306 !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
1309 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
1313 fsl_mc_regs = ioremap(res->start, resource_size(res));
1318 * Make sure that the MC firmware is paused before the IOMMU setup for
1319 * it is done or otherwise the firmware will crash right after the SMMU
1320 * gets probed and enabled.
1322 writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
1323 fsl_mc_regs + FSL_MC_GCR1);
1324 iounmap(fsl_mc_regs);
1329 static struct notifier_block fsl_mc_nb = {
1330 .notifier_call = fsl_mc_bus_notifier,
1333 static int __init fsl_mc_bus_driver_init(void)
1337 error = bus_register(&fsl_mc_bus_type);
1339 pr_err("bus type registration failed: %d\n", error);
1340 goto error_cleanup_cache;
1343 error = platform_driver_register(&fsl_mc_bus_driver);
1345 pr_err("platform_driver_register() failed: %d\n", error);
1346 goto error_cleanup_bus;
1349 error = dprc_driver_init();
1351 goto error_cleanup_driver;
1353 error = fsl_mc_allocator_driver_init();
1355 goto error_cleanup_dprc_driver;
1357 return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
1359 error_cleanup_dprc_driver:
1362 error_cleanup_driver:
1363 platform_driver_unregister(&fsl_mc_bus_driver);
1366 bus_unregister(&fsl_mc_bus_type);
1368 error_cleanup_cache:
1371 postcore_initcall(fsl_mc_bus_driver_init);