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>
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)
67 #define FSL_MC_FAPR 0x28
68 #define MC_FAPR_PL BIT(18)
69 #define MC_FAPR_BMT BIT(17)
72 * fsl_mc_bus_match - device to driver matching callback
73 * @dev: the fsl-mc device to match against
74 * @drv: the device driver to search for matching fsl-mc object type
77 * Returns 1 on success, 0 otherwise.
79 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
81 const struct fsl_mc_device_id *id;
82 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
83 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
86 /* When driver_override is set, only bind to the matching driver */
87 if (mc_dev->driver_override) {
88 found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
92 if (!mc_drv->match_id_table)
96 * If the object is not 'plugged' don't match.
97 * Only exception is the root DPRC, which is a special case.
99 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
100 !fsl_mc_is_root_dprc(&mc_dev->dev))
104 * Traverse the match_id table of the given driver, trying to find
105 * a matching for the given device.
107 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
108 if (id->vendor == mc_dev->obj_desc.vendor &&
109 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
117 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
122 * fsl_mc_bus_uevent - callback invoked when a device is added
124 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
126 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
128 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
129 mc_dev->obj_desc.vendor,
130 mc_dev->obj_desc.type))
136 static int fsl_mc_dma_configure(struct device *dev)
138 struct device *dma_dev = dev;
139 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
140 u32 input_id = mc_dev->icid;
142 while (dev_is_fsl_mc(dma_dev))
143 dma_dev = dma_dev->parent;
145 if (dev_of_node(dma_dev))
146 return of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
148 return acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
151 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
154 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
156 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
157 mc_dev->obj_desc.type);
159 static DEVICE_ATTR_RO(modalias);
161 static ssize_t driver_override_store(struct device *dev,
162 struct device_attribute *attr,
163 const char *buf, size_t count)
165 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
166 char *driver_override, *old = mc_dev->driver_override;
169 if (WARN_ON(dev->bus != &fsl_mc_bus_type))
172 if (count >= (PAGE_SIZE - 1))
175 driver_override = kstrndup(buf, count, GFP_KERNEL);
176 if (!driver_override)
179 cp = strchr(driver_override, '\n');
183 if (strlen(driver_override)) {
184 mc_dev->driver_override = driver_override;
186 kfree(driver_override);
187 mc_dev->driver_override = NULL;
195 static ssize_t driver_override_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
198 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
200 return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
202 static DEVICE_ATTR_RW(driver_override);
204 static struct attribute *fsl_mc_dev_attrs[] = {
205 &dev_attr_modalias.attr,
206 &dev_attr_driver_override.attr,
210 ATTRIBUTE_GROUPS(fsl_mc_dev);
212 static int scan_fsl_mc_bus(struct device *dev, void *data)
214 struct fsl_mc_device *root_mc_dev;
215 struct fsl_mc_bus *root_mc_bus;
217 if (!fsl_mc_is_root_dprc(dev))
220 root_mc_dev = to_fsl_mc_device(dev);
221 root_mc_bus = to_fsl_mc_bus(root_mc_dev);
222 mutex_lock(&root_mc_bus->scan_mutex);
223 dprc_scan_objects(root_mc_dev, NULL);
224 mutex_unlock(&root_mc_bus->scan_mutex);
230 static ssize_t rescan_store(struct bus_type *bus,
231 const char *buf, size_t count)
235 if (kstrtoul(buf, 0, &val) < 0)
239 bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
243 static BUS_ATTR_WO(rescan);
245 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
247 struct fsl_mc_device *root_mc_dev;
251 if (!fsl_mc_is_root_dprc(dev))
254 root_mc_dev = to_fsl_mc_device(dev);
256 if (kstrtoul(buf, 0, &val) < 0)
260 enable_dprc_irq(root_mc_dev);
262 disable_dprc_irq(root_mc_dev);
268 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
270 struct fsl_mc_device *root_mc_dev;
273 if (!fsl_mc_is_root_dprc(dev))
276 root_mc_dev = to_fsl_mc_device(dev);
278 sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
283 static ssize_t autorescan_store(struct bus_type *bus,
284 const char *buf, size_t count)
286 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
291 static ssize_t autorescan_show(struct bus_type *bus, char *buf)
293 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
297 static BUS_ATTR_RW(autorescan);
299 static struct attribute *fsl_mc_bus_attrs[] = {
300 &bus_attr_rescan.attr,
301 &bus_attr_autorescan.attr,
305 ATTRIBUTE_GROUPS(fsl_mc_bus);
307 struct bus_type fsl_mc_bus_type = {
309 .match = fsl_mc_bus_match,
310 .uevent = fsl_mc_bus_uevent,
311 .dma_configure = fsl_mc_dma_configure,
312 .dev_groups = fsl_mc_dev_groups,
313 .bus_groups = fsl_mc_bus_groups,
315 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
317 struct device_type fsl_mc_bus_dprc_type = {
318 .name = "fsl_mc_bus_dprc"
320 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
322 struct device_type fsl_mc_bus_dpni_type = {
323 .name = "fsl_mc_bus_dpni"
325 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
327 struct device_type fsl_mc_bus_dpio_type = {
328 .name = "fsl_mc_bus_dpio"
330 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
332 struct device_type fsl_mc_bus_dpsw_type = {
333 .name = "fsl_mc_bus_dpsw"
335 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
337 struct device_type fsl_mc_bus_dpbp_type = {
338 .name = "fsl_mc_bus_dpbp"
340 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
342 struct device_type fsl_mc_bus_dpcon_type = {
343 .name = "fsl_mc_bus_dpcon"
345 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
347 struct device_type fsl_mc_bus_dpmcp_type = {
348 .name = "fsl_mc_bus_dpmcp"
350 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
352 struct device_type fsl_mc_bus_dpmac_type = {
353 .name = "fsl_mc_bus_dpmac"
355 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
357 struct device_type fsl_mc_bus_dprtc_type = {
358 .name = "fsl_mc_bus_dprtc"
360 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
362 struct device_type fsl_mc_bus_dpseci_type = {
363 .name = "fsl_mc_bus_dpseci"
365 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
367 struct device_type fsl_mc_bus_dpdmux_type = {
368 .name = "fsl_mc_bus_dpdmux"
370 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
372 struct device_type fsl_mc_bus_dpdcei_type = {
373 .name = "fsl_mc_bus_dpdcei"
375 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
377 struct device_type fsl_mc_bus_dpaiop_type = {
378 .name = "fsl_mc_bus_dpaiop"
380 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
382 struct device_type fsl_mc_bus_dpci_type = {
383 .name = "fsl_mc_bus_dpci"
385 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
387 struct device_type fsl_mc_bus_dpdmai_type = {
388 .name = "fsl_mc_bus_dpdmai"
390 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
392 struct device_type fsl_mc_bus_dpdbg_type = {
393 .name = "fsl_mc_bus_dpdbg"
395 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
397 static struct device_type *fsl_mc_get_device_type(const char *type)
399 static const struct {
400 struct device_type *dev_type;
403 { &fsl_mc_bus_dprc_type, "dprc" },
404 { &fsl_mc_bus_dpni_type, "dpni" },
405 { &fsl_mc_bus_dpio_type, "dpio" },
406 { &fsl_mc_bus_dpsw_type, "dpsw" },
407 { &fsl_mc_bus_dpbp_type, "dpbp" },
408 { &fsl_mc_bus_dpcon_type, "dpcon" },
409 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
410 { &fsl_mc_bus_dpmac_type, "dpmac" },
411 { &fsl_mc_bus_dprtc_type, "dprtc" },
412 { &fsl_mc_bus_dpseci_type, "dpseci" },
413 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
414 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
415 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
416 { &fsl_mc_bus_dpci_type, "dpci" },
417 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
418 { &fsl_mc_bus_dpdbg_type, "dpdbg" },
423 for (i = 0; dev_types[i].dev_type; i++)
424 if (!strcmp(dev_types[i].type, type))
425 return dev_types[i].dev_type;
430 static int fsl_mc_driver_probe(struct device *dev)
432 struct fsl_mc_driver *mc_drv;
433 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
436 mc_drv = to_fsl_mc_driver(dev->driver);
438 error = mc_drv->probe(mc_dev);
440 if (error != -EPROBE_DEFER)
441 dev_err(dev, "%s failed: %d\n", __func__, error);
448 static int fsl_mc_driver_remove(struct device *dev)
450 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
451 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
454 error = mc_drv->remove(mc_dev);
456 dev_err(dev, "%s failed: %d\n", __func__, error);
463 static void fsl_mc_driver_shutdown(struct device *dev)
465 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
466 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
468 mc_drv->shutdown(mc_dev);
472 * __fsl_mc_driver_register - registers a child device driver with the
475 * This function is implicitly invoked from the registration function of
476 * fsl_mc device drivers, which is generated by the
477 * module_fsl_mc_driver() macro.
479 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
480 struct module *owner)
484 mc_driver->driver.owner = owner;
485 mc_driver->driver.bus = &fsl_mc_bus_type;
487 if (mc_driver->probe)
488 mc_driver->driver.probe = fsl_mc_driver_probe;
490 if (mc_driver->remove)
491 mc_driver->driver.remove = fsl_mc_driver_remove;
493 if (mc_driver->shutdown)
494 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
496 error = driver_register(&mc_driver->driver);
498 pr_err("driver_register() failed for %s: %d\n",
499 mc_driver->driver.name, error);
505 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
508 * fsl_mc_driver_unregister - unregisters a device driver from the
511 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
513 driver_unregister(&mc_driver->driver);
515 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
518 * mc_get_version() - Retrieves the Management Complex firmware
519 * version information
520 * @mc_io: Pointer to opaque I/O object
521 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
522 * @mc_ver_info: Returned version information structure
524 * Return: '0' on Success; Error code otherwise.
526 static int mc_get_version(struct fsl_mc_io *mc_io,
528 struct fsl_mc_version *mc_ver_info)
530 struct fsl_mc_command cmd = { 0 };
531 struct dpmng_rsp_get_version *rsp_params;
534 /* prepare command */
535 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
539 /* send command to mc*/
540 err = mc_send_command(mc_io, &cmd);
544 /* retrieve response parameters */
545 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
546 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
547 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
548 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
554 * fsl_mc_get_version - function to retrieve the MC f/w version information
556 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
558 struct fsl_mc_version *fsl_mc_get_version(void)
560 if (mc_version.major)
565 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
568 * fsl_mc_get_root_dprc - function to traverse to the root dprc
570 void fsl_mc_get_root_dprc(struct device *dev,
571 struct device **root_dprc_dev)
574 *root_dprc_dev = NULL;
575 } else if (!dev_is_fsl_mc(dev)) {
576 *root_dprc_dev = NULL;
578 *root_dprc_dev = dev;
579 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
580 *root_dprc_dev = (*root_dprc_dev)->parent;
584 static int get_dprc_attr(struct fsl_mc_io *mc_io,
585 int container_id, struct dprc_attributes *attr)
590 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
592 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
596 memset(attr, 0, sizeof(struct dprc_attributes));
597 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
599 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
607 (void)dprc_close(mc_io, 0, dprc_handle);
611 static int get_dprc_icid(struct fsl_mc_io *mc_io,
612 int container_id, u32 *icid)
614 struct dprc_attributes attr;
617 error = get_dprc_attr(mc_io, container_id, &attr);
624 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
625 enum dprc_region_type mc_region_type,
626 u64 mc_offset, phys_addr_t *phys_addr)
629 struct device *root_dprc_dev;
632 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
633 mc = dev_get_drvdata(root_dprc_dev->parent);
635 if (mc->num_translation_ranges == 0) {
637 * Do identity mapping:
639 *phys_addr = mc_offset;
643 for (i = 0; i < mc->num_translation_ranges; i++) {
644 struct fsl_mc_addr_translation_range *range =
645 &mc->translation_ranges[i];
647 if (mc_region_type == range->mc_region_type &&
648 mc_offset >= range->start_mc_offset &&
649 mc_offset < range->end_mc_offset) {
650 *phys_addr = range->start_phys_addr +
651 (mc_offset - range->start_mc_offset);
659 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
660 struct fsl_mc_device *mc_bus_dev)
664 struct resource *regions;
665 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
666 struct device *parent_dev = mc_dev->dev.parent;
667 enum dprc_region_type mc_region_type;
669 if (is_fsl_mc_bus_dprc(mc_dev) ||
670 is_fsl_mc_bus_dpmcp(mc_dev)) {
671 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
672 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
673 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
676 * This function should not have been called for this MC object
677 * type, as this object type is not supposed to have MMIO
683 regions = kmalloc_array(obj_desc->region_count,
684 sizeof(regions[0]), GFP_KERNEL);
688 for (i = 0; i < obj_desc->region_count; i++) {
689 struct dprc_region_desc region_desc;
691 error = dprc_get_obj_region(mc_bus_dev->mc_io,
693 mc_bus_dev->mc_handle,
695 obj_desc->id, i, ®ion_desc);
698 "dprc_get_obj_region() failed: %d\n", error);
699 goto error_cleanup_regions;
702 * Older MC only returned region offset and no base address
703 * If base address is in the region_desc use it otherwise
704 * revert to old mechanism
706 if (region_desc.base_address)
707 regions[i].start = region_desc.base_address +
708 region_desc.base_offset;
710 error = translate_mc_addr(mc_dev, mc_region_type,
711 region_desc.base_offset,
716 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
717 region_desc.base_offset,
718 obj_desc->type, obj_desc->id, i);
719 goto error_cleanup_regions;
722 regions[i].end = regions[i].start + region_desc.size - 1;
723 regions[i].name = "fsl-mc object MMIO region";
724 regions[i].flags = region_desc.flags & IORESOURCE_BITS;
725 regions[i].flags |= IORESOURCE_MEM;
728 mc_dev->regions = regions;
731 error_cleanup_regions:
737 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
739 bool fsl_mc_is_root_dprc(struct device *dev)
741 struct device *root_dprc_dev;
743 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
746 return dev == root_dprc_dev;
749 static void fsl_mc_device_release(struct device *dev)
751 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
753 kfree(mc_dev->regions);
755 if (is_fsl_mc_bus_dprc(mc_dev))
756 kfree(to_fsl_mc_bus(mc_dev));
762 * Add a newly discovered fsl-mc device to be visible in Linux
764 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
765 struct fsl_mc_io *mc_io,
766 struct device *parent_dev,
767 struct fsl_mc_device **new_mc_dev)
770 struct fsl_mc_device *mc_dev = NULL;
771 struct fsl_mc_bus *mc_bus = NULL;
772 struct fsl_mc_device *parent_mc_dev;
774 if (dev_is_fsl_mc(parent_dev))
775 parent_mc_dev = to_fsl_mc_device(parent_dev);
777 parent_mc_dev = NULL;
779 if (strcmp(obj_desc->type, "dprc") == 0) {
781 * Allocate an MC bus device object:
783 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
787 mutex_init(&mc_bus->scan_mutex);
788 mc_dev = &mc_bus->mc_dev;
791 * Allocate a regular fsl_mc_device object:
793 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
798 mc_dev->obj_desc = *obj_desc;
799 mc_dev->mc_io = mc_io;
800 device_initialize(&mc_dev->dev);
801 mc_dev->dev.parent = parent_dev;
802 mc_dev->dev.bus = &fsl_mc_bus_type;
803 mc_dev->dev.release = fsl_mc_device_release;
804 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
805 if (!mc_dev->dev.type) {
807 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
808 goto error_cleanup_dev;
810 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
812 if (strcmp(obj_desc->type, "dprc") == 0) {
813 struct fsl_mc_io *mc_io2;
815 mc_dev->flags |= FSL_MC_IS_DPRC;
818 * To get the DPRC's ICID, we need to open the DPRC
819 * in get_dprc_icid(). For child DPRCs, we do so using the
820 * parent DPRC's MC portal instead of the child DPRC's MC
821 * portal, in case the child DPRC is already opened with
822 * its own portal (e.g., the DPRC used by AIOP).
824 * NOTE: There cannot be more than one active open for a
825 * given MC object, using the same MC portal.
829 * device being added is a child DPRC device
831 mc_io2 = parent_mc_dev->mc_io;
834 * device being added is the root DPRC device
838 goto error_cleanup_dev;
844 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
846 goto error_cleanup_dev;
849 * A non-DPRC object has to be a child of a DPRC, use the
850 * parent's ICID and interrupt domain.
852 mc_dev->icid = parent_mc_dev->icid;
853 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
854 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
855 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
856 dev_set_msi_domain(&mc_dev->dev,
857 dev_get_msi_domain(&parent_mc_dev->dev));
861 * Get MMIO regions for the device from the MC:
863 * NOTE: the root DPRC is a special case as its MMIO region is
864 * obtained from the device tree
866 if (parent_mc_dev && obj_desc->region_count != 0) {
867 error = fsl_mc_device_get_mmio_regions(mc_dev,
870 goto error_cleanup_dev;
874 * The device-specific probe callback will get invoked by device_add()
876 error = device_add(&mc_dev->dev);
879 "device_add() failed for device %s: %d\n",
880 dev_name(&mc_dev->dev), error);
881 goto error_cleanup_dev;
884 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
886 *new_mc_dev = mc_dev;
890 kfree(mc_dev->regions);
896 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
899 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
902 * @mc_dev: Pointer to an fsl-mc device
904 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
906 kfree(mc_dev->driver_override);
907 mc_dev->driver_override = NULL;
910 * The device-specific remove callback will get invoked by device_del()
912 device_del(&mc_dev->dev);
913 put_device(&mc_dev->dev);
915 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
917 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
919 struct fsl_mc_device *mc_bus_dev, *endpoint;
920 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
921 struct dprc_endpoint endpoint1 = {{ 0 }};
922 struct dprc_endpoint endpoint2 = {{ 0 }};
925 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
926 strcpy(endpoint1.type, mc_dev->obj_desc.type);
927 endpoint1.id = mc_dev->obj_desc.id;
929 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
930 mc_bus_dev->mc_handle,
931 &endpoint1, &endpoint2,
934 if (err == -ENOTCONN || state == -1)
935 return ERR_PTR(-ENOTCONN);
938 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
942 strcpy(endpoint_desc.type, endpoint2.type);
943 endpoint_desc.id = endpoint2.id;
944 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
947 * We know that the device has an endpoint because we verified by
948 * interrogating the firmware. This is the case when the device was not
949 * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
950 * Differentiate this case by returning EPROBE_DEFER.
953 return ERR_PTR(-EPROBE_DEFER);
957 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
959 static int parse_mc_ranges(struct device *dev,
963 const __be32 **ranges_start)
966 int range_tuple_cell_count;
969 struct device_node *mc_node = dev->of_node;
971 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
972 if (!(*ranges_start) || !ranges_len) {
974 "missing or empty ranges property for device tree node '%pOFn'\n",
979 *paddr_cells = of_n_addr_cells(mc_node);
981 prop = of_get_property(mc_node, "#address-cells", NULL);
983 *mc_addr_cells = be32_to_cpup(prop);
985 *mc_addr_cells = *paddr_cells;
987 prop = of_get_property(mc_node, "#size-cells", NULL);
989 *mc_size_cells = be32_to_cpup(prop);
991 *mc_size_cells = of_n_size_cells(mc_node);
993 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
996 tuple_len = range_tuple_cell_count * sizeof(__be32);
997 if (ranges_len % tuple_len != 0) {
998 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
1002 return ranges_len / tuple_len;
1005 static int get_mc_addr_translation_ranges(struct device *dev,
1006 struct fsl_mc_addr_translation_range
1015 const __be32 *ranges_start;
1018 ret = parse_mc_ranges(dev,
1029 * Missing or empty ranges property ("ranges;") for the
1030 * 'fsl,qoriq-mc' node. In this case, identity mapping
1037 *ranges = devm_kcalloc(dev, *num_ranges,
1038 sizeof(struct fsl_mc_addr_translation_range),
1043 cell = ranges_start;
1044 for (i = 0; i < *num_ranges; ++i) {
1045 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
1047 range->mc_region_type = of_read_number(cell, 1);
1048 range->start_mc_offset = of_read_number(cell + 1,
1050 cell += mc_addr_cells;
1051 range->start_phys_addr = of_read_number(cell, paddr_cells);
1052 cell += paddr_cells;
1053 range->end_mc_offset = range->start_mc_offset +
1054 of_read_number(cell, mc_size_cells);
1056 cell += mc_size_cells;
1063 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
1066 static int fsl_mc_bus_probe(struct platform_device *pdev)
1068 struct fsl_mc_obj_desc obj_desc;
1071 struct fsl_mc_device *mc_bus_dev = NULL;
1072 struct fsl_mc_io *mc_io = NULL;
1074 phys_addr_t mc_portal_phys_addr;
1075 u32 mc_portal_size, mc_stream_id;
1076 struct resource *plat_res;
1078 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1082 platform_set_drvdata(pdev, mc);
1084 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1086 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1087 if (IS_ERR(mc->fsl_mc_regs))
1088 return PTR_ERR(mc->fsl_mc_regs);
1091 if (mc->fsl_mc_regs) {
1093 * Some bootloaders pause the MC firmware before booting the
1094 * kernel so that MC will not cause faults as soon as the
1095 * SMMU probes due to the fact that there's no configuration
1097 * At this point MC should have all its SMMU setup done so make
1098 * sure it is resumed.
1100 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) & (~GCR1_P1_STOP),
1101 mc->fsl_mc_regs + FSL_MC_GCR1);
1103 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1104 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1106 * HW ORs the PL and BMT bit, places the result in bit
1107 * 14 of the StreamID and ORs in the ICID. Calculate it
1110 mc_stream_id = (mc_stream_id & 0xffff) |
1111 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1113 error = acpi_dma_configure_id(&pdev->dev,
1117 dev_warn(&pdev->dev,
1118 "failed to configure dma: %d.\n",
1124 * Get physical address of MC portal for the root DPRC:
1126 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1127 mc_portal_phys_addr = plat_res->start;
1128 mc_portal_size = resource_size(plat_res);
1129 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1130 mc_portal_size, NULL,
1131 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1135 error = mc_get_version(mc_io, 0, &mc_version);
1138 "mc_get_version() failed with error %d\n", error);
1139 goto error_cleanup_mc_io;
1142 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1143 mc_version.major, mc_version.minor, mc_version.revision);
1145 if (dev_of_node(&pdev->dev)) {
1146 error = get_mc_addr_translation_ranges(&pdev->dev,
1147 &mc->translation_ranges,
1148 &mc->num_translation_ranges);
1150 goto error_cleanup_mc_io;
1153 error = dprc_get_container_id(mc_io, 0, &container_id);
1156 "dprc_get_container_id() failed: %d\n", error);
1157 goto error_cleanup_mc_io;
1160 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1161 error = dprc_get_api_version(mc_io, 0,
1162 &obj_desc.ver_major,
1163 &obj_desc.ver_minor);
1165 goto error_cleanup_mc_io;
1167 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1168 strcpy(obj_desc.type, "dprc");
1169 obj_desc.id = container_id;
1170 obj_desc.irq_count = 1;
1171 obj_desc.region_count = 0;
1173 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1175 goto error_cleanup_mc_io;
1177 mc->root_mc_bus_dev = mc_bus_dev;
1178 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1181 error_cleanup_mc_io:
1182 fsl_destroy_mc_io(mc_io);
1187 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1190 static int fsl_mc_bus_remove(struct platform_device *pdev)
1192 struct fsl_mc *mc = platform_get_drvdata(pdev);
1194 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1197 fsl_mc_device_remove(mc->root_mc_bus_dev);
1199 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
1200 mc->root_mc_bus_dev->mc_io = NULL;
1205 static const struct of_device_id fsl_mc_bus_match_table[] = {
1206 {.compatible = "fsl,qoriq-mc",},
1210 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1212 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1216 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1218 static struct platform_driver fsl_mc_bus_driver = {
1220 .name = "fsl_mc_bus",
1222 .of_match_table = fsl_mc_bus_match_table,
1223 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1225 .probe = fsl_mc_bus_probe,
1226 .remove = fsl_mc_bus_remove,
1229 static int __init fsl_mc_bus_driver_init(void)
1233 error = bus_register(&fsl_mc_bus_type);
1235 pr_err("bus type registration failed: %d\n", error);
1236 goto error_cleanup_cache;
1239 error = platform_driver_register(&fsl_mc_bus_driver);
1241 pr_err("platform_driver_register() failed: %d\n", error);
1242 goto error_cleanup_bus;
1245 error = dprc_driver_init();
1247 goto error_cleanup_driver;
1249 error = fsl_mc_allocator_driver_init();
1251 goto error_cleanup_dprc_driver;
1255 error_cleanup_dprc_driver:
1258 error_cleanup_driver:
1259 platform_driver_unregister(&fsl_mc_bus_driver);
1262 bus_unregister(&fsl_mc_bus_type);
1264 error_cleanup_cache:
1267 postcore_initcall(fsl_mc_bus_driver_init);