1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale Management Complex (MC) bus driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
10 #define pr_fmt(fmt) "fsl-mc: " fmt
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_address.h>
15 #include <linux/ioport.h>
16 #include <linux/slab.h>
17 #include <linux/limits.h>
18 #include <linux/bitops.h>
19 #include <linux/msi.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/acpi.h>
22 #include <linux/iommu.h>
24 #include "fsl-mc-private.h"
27 * Default DMA mask for devices on a fsl-mc bus
29 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
31 static struct fsl_mc_version mc_version;
34 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
35 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
36 * @num_translation_ranges: number of entries in addr_translation_ranges
37 * @translation_ranges: array of bus to system address translation ranges
40 struct fsl_mc_device *root_mc_bus_dev;
41 u8 num_translation_ranges;
42 struct fsl_mc_addr_translation_range *translation_ranges;
47 * struct fsl_mc_addr_translation_range - bus to system address translation
49 * @mc_region_type: Type of MC region for the range being translated
50 * @start_mc_offset: Start MC offset of the range being translated
51 * @end_mc_offset: MC offset of the first byte after the range (last MC
52 * offset of the range is end_mc_offset - 1)
53 * @start_phys_addr: system physical address corresponding to start_mc_addr
55 struct fsl_mc_addr_translation_range {
56 enum dprc_region_type mc_region_type;
59 phys_addr_t start_phys_addr;
62 #define FSL_MC_FAPR 0x28
63 #define MC_FAPR_PL BIT(18)
64 #define MC_FAPR_BMT BIT(17)
67 * fsl_mc_bus_match - device to driver matching callback
68 * @dev: the fsl-mc device to match against
69 * @drv: the device driver to search for matching fsl-mc object type
72 * Returns 1 on success, 0 otherwise.
74 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
76 const struct fsl_mc_device_id *id;
77 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
78 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
81 if (!mc_drv->match_id_table)
85 * If the object is not 'plugged' don't match.
86 * Only exception is the root DPRC, which is a special case.
88 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
89 !fsl_mc_is_root_dprc(&mc_dev->dev))
93 * Traverse the match_id table of the given driver, trying to find
94 * a matching for the given device.
96 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
97 if (id->vendor == mc_dev->obj_desc.vendor &&
98 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
106 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
111 * fsl_mc_bus_uevent - callback invoked when a device is added
113 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
115 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
117 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
118 mc_dev->obj_desc.vendor,
119 mc_dev->obj_desc.type))
125 static int fsl_mc_dma_configure(struct device *dev)
127 struct device *dma_dev = dev;
128 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
129 u32 input_id = mc_dev->icid;
131 while (dev_is_fsl_mc(dma_dev))
132 dma_dev = dma_dev->parent;
134 if (dev_of_node(dma_dev))
135 return of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
137 return acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
140 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
143 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
145 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
146 mc_dev->obj_desc.type);
148 static DEVICE_ATTR_RO(modalias);
150 static struct attribute *fsl_mc_dev_attrs[] = {
151 &dev_attr_modalias.attr,
155 ATTRIBUTE_GROUPS(fsl_mc_dev);
157 struct bus_type fsl_mc_bus_type = {
159 .match = fsl_mc_bus_match,
160 .uevent = fsl_mc_bus_uevent,
161 .dma_configure = fsl_mc_dma_configure,
162 .dev_groups = fsl_mc_dev_groups,
164 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
166 struct device_type fsl_mc_bus_dprc_type = {
167 .name = "fsl_mc_bus_dprc"
169 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
171 struct device_type fsl_mc_bus_dpni_type = {
172 .name = "fsl_mc_bus_dpni"
174 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
176 struct device_type fsl_mc_bus_dpio_type = {
177 .name = "fsl_mc_bus_dpio"
179 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
181 struct device_type fsl_mc_bus_dpsw_type = {
182 .name = "fsl_mc_bus_dpsw"
184 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
186 struct device_type fsl_mc_bus_dpbp_type = {
187 .name = "fsl_mc_bus_dpbp"
189 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
191 struct device_type fsl_mc_bus_dpcon_type = {
192 .name = "fsl_mc_bus_dpcon"
194 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
196 struct device_type fsl_mc_bus_dpmcp_type = {
197 .name = "fsl_mc_bus_dpmcp"
199 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
201 struct device_type fsl_mc_bus_dpmac_type = {
202 .name = "fsl_mc_bus_dpmac"
204 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
206 struct device_type fsl_mc_bus_dprtc_type = {
207 .name = "fsl_mc_bus_dprtc"
209 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
211 struct device_type fsl_mc_bus_dpseci_type = {
212 .name = "fsl_mc_bus_dpseci"
214 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
216 struct device_type fsl_mc_bus_dpdmux_type = {
217 .name = "fsl_mc_bus_dpdmux"
219 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
221 struct device_type fsl_mc_bus_dpdcei_type = {
222 .name = "fsl_mc_bus_dpdcei"
224 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
226 struct device_type fsl_mc_bus_dpaiop_type = {
227 .name = "fsl_mc_bus_dpaiop"
229 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
231 struct device_type fsl_mc_bus_dpci_type = {
232 .name = "fsl_mc_bus_dpci"
234 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
236 struct device_type fsl_mc_bus_dpdmai_type = {
237 .name = "fsl_mc_bus_dpdmai"
239 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
241 static struct device_type *fsl_mc_get_device_type(const char *type)
243 static const struct {
244 struct device_type *dev_type;
247 { &fsl_mc_bus_dprc_type, "dprc" },
248 { &fsl_mc_bus_dpni_type, "dpni" },
249 { &fsl_mc_bus_dpio_type, "dpio" },
250 { &fsl_mc_bus_dpsw_type, "dpsw" },
251 { &fsl_mc_bus_dpbp_type, "dpbp" },
252 { &fsl_mc_bus_dpcon_type, "dpcon" },
253 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
254 { &fsl_mc_bus_dpmac_type, "dpmac" },
255 { &fsl_mc_bus_dprtc_type, "dprtc" },
256 { &fsl_mc_bus_dpseci_type, "dpseci" },
257 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
258 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
259 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
260 { &fsl_mc_bus_dpci_type, "dpci" },
261 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
266 for (i = 0; dev_types[i].dev_type; i++)
267 if (!strcmp(dev_types[i].type, type))
268 return dev_types[i].dev_type;
273 static int fsl_mc_driver_probe(struct device *dev)
275 struct fsl_mc_driver *mc_drv;
276 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
279 mc_drv = to_fsl_mc_driver(dev->driver);
281 error = mc_drv->probe(mc_dev);
283 if (error != -EPROBE_DEFER)
284 dev_err(dev, "%s failed: %d\n", __func__, error);
291 static int fsl_mc_driver_remove(struct device *dev)
293 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
294 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
297 error = mc_drv->remove(mc_dev);
299 dev_err(dev, "%s failed: %d\n", __func__, error);
306 static void fsl_mc_driver_shutdown(struct device *dev)
308 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
309 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
311 mc_drv->shutdown(mc_dev);
315 * __fsl_mc_driver_register - registers a child device driver with the
318 * This function is implicitly invoked from the registration function of
319 * fsl_mc device drivers, which is generated by the
320 * module_fsl_mc_driver() macro.
322 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
323 struct module *owner)
327 mc_driver->driver.owner = owner;
328 mc_driver->driver.bus = &fsl_mc_bus_type;
330 if (mc_driver->probe)
331 mc_driver->driver.probe = fsl_mc_driver_probe;
333 if (mc_driver->remove)
334 mc_driver->driver.remove = fsl_mc_driver_remove;
336 if (mc_driver->shutdown)
337 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
339 error = driver_register(&mc_driver->driver);
341 pr_err("driver_register() failed for %s: %d\n",
342 mc_driver->driver.name, error);
348 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
351 * fsl_mc_driver_unregister - unregisters a device driver from the
354 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
356 driver_unregister(&mc_driver->driver);
358 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
361 * mc_get_version() - Retrieves the Management Complex firmware
362 * version information
363 * @mc_io: Pointer to opaque I/O object
364 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
365 * @mc_ver_info: Returned version information structure
367 * Return: '0' on Success; Error code otherwise.
369 static int mc_get_version(struct fsl_mc_io *mc_io,
371 struct fsl_mc_version *mc_ver_info)
373 struct fsl_mc_command cmd = { 0 };
374 struct dpmng_rsp_get_version *rsp_params;
377 /* prepare command */
378 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
382 /* send command to mc*/
383 err = mc_send_command(mc_io, &cmd);
387 /* retrieve response parameters */
388 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
389 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
390 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
391 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
397 * fsl_mc_get_version - function to retrieve the MC f/w version information
399 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
401 struct fsl_mc_version *fsl_mc_get_version(void)
403 if (mc_version.major)
408 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
411 * fsl_mc_get_root_dprc - function to traverse to the root dprc
413 void fsl_mc_get_root_dprc(struct device *dev,
414 struct device **root_dprc_dev)
417 *root_dprc_dev = NULL;
418 } else if (!dev_is_fsl_mc(dev)) {
419 *root_dprc_dev = NULL;
421 *root_dprc_dev = dev;
422 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
423 *root_dprc_dev = (*root_dprc_dev)->parent;
427 static int get_dprc_attr(struct fsl_mc_io *mc_io,
428 int container_id, struct dprc_attributes *attr)
433 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
435 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
439 memset(attr, 0, sizeof(struct dprc_attributes));
440 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
442 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
450 (void)dprc_close(mc_io, 0, dprc_handle);
454 static int get_dprc_icid(struct fsl_mc_io *mc_io,
455 int container_id, u16 *icid)
457 struct dprc_attributes attr;
460 error = get_dprc_attr(mc_io, container_id, &attr);
467 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
468 enum dprc_region_type mc_region_type,
469 u64 mc_offset, phys_addr_t *phys_addr)
472 struct device *root_dprc_dev;
475 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
476 mc = dev_get_drvdata(root_dprc_dev->parent);
478 if (mc->num_translation_ranges == 0) {
480 * Do identity mapping:
482 *phys_addr = mc_offset;
486 for (i = 0; i < mc->num_translation_ranges; i++) {
487 struct fsl_mc_addr_translation_range *range =
488 &mc->translation_ranges[i];
490 if (mc_region_type == range->mc_region_type &&
491 mc_offset >= range->start_mc_offset &&
492 mc_offset < range->end_mc_offset) {
493 *phys_addr = range->start_phys_addr +
494 (mc_offset - range->start_mc_offset);
502 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
503 struct fsl_mc_device *mc_bus_dev)
507 struct resource *regions;
508 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
509 struct device *parent_dev = mc_dev->dev.parent;
510 enum dprc_region_type mc_region_type;
512 if (is_fsl_mc_bus_dprc(mc_dev) ||
513 is_fsl_mc_bus_dpmcp(mc_dev)) {
514 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
515 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
516 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
519 * This function should not have been called for this MC object
520 * type, as this object type is not supposed to have MMIO
526 regions = kmalloc_array(obj_desc->region_count,
527 sizeof(regions[0]), GFP_KERNEL);
531 for (i = 0; i < obj_desc->region_count; i++) {
532 struct dprc_region_desc region_desc;
534 error = dprc_get_obj_region(mc_bus_dev->mc_io,
536 mc_bus_dev->mc_handle,
538 obj_desc->id, i, ®ion_desc);
541 "dprc_get_obj_region() failed: %d\n", error);
542 goto error_cleanup_regions;
545 * Older MC only returned region offset and no base address
546 * If base address is in the region_desc use it otherwise
547 * revert to old mechanism
549 if (region_desc.base_address)
550 regions[i].start = region_desc.base_address +
551 region_desc.base_offset;
553 error = translate_mc_addr(mc_dev, mc_region_type,
554 region_desc.base_offset,
559 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
560 region_desc.base_offset,
561 obj_desc->type, obj_desc->id, i);
562 goto error_cleanup_regions;
565 regions[i].end = regions[i].start + region_desc.size - 1;
566 regions[i].name = "fsl-mc object MMIO region";
567 regions[i].flags = IORESOURCE_IO;
568 if (region_desc.flags & DPRC_REGION_CACHEABLE)
569 regions[i].flags |= IORESOURCE_CACHEABLE;
570 if (region_desc.flags & DPRC_REGION_SHAREABLE)
571 regions[i].flags |= IORESOURCE_MEM;
574 mc_dev->regions = regions;
577 error_cleanup_regions:
583 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
585 bool fsl_mc_is_root_dprc(struct device *dev)
587 struct device *root_dprc_dev;
589 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
592 return dev == root_dprc_dev;
595 static void fsl_mc_device_release(struct device *dev)
597 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
599 kfree(mc_dev->regions);
601 if (is_fsl_mc_bus_dprc(mc_dev))
602 kfree(to_fsl_mc_bus(mc_dev));
608 * Add a newly discovered fsl-mc device to be visible in Linux
610 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
611 struct fsl_mc_io *mc_io,
612 struct device *parent_dev,
613 struct fsl_mc_device **new_mc_dev)
616 struct fsl_mc_device *mc_dev = NULL;
617 struct fsl_mc_bus *mc_bus = NULL;
618 struct fsl_mc_device *parent_mc_dev;
620 if (dev_is_fsl_mc(parent_dev))
621 parent_mc_dev = to_fsl_mc_device(parent_dev);
623 parent_mc_dev = NULL;
625 if (strcmp(obj_desc->type, "dprc") == 0) {
627 * Allocate an MC bus device object:
629 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
633 mc_dev = &mc_bus->mc_dev;
636 * Allocate a regular fsl_mc_device object:
638 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
643 mc_dev->obj_desc = *obj_desc;
644 mc_dev->mc_io = mc_io;
645 device_initialize(&mc_dev->dev);
646 mc_dev->dev.parent = parent_dev;
647 mc_dev->dev.bus = &fsl_mc_bus_type;
648 mc_dev->dev.release = fsl_mc_device_release;
649 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
650 if (!mc_dev->dev.type) {
652 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
653 goto error_cleanup_dev;
655 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
657 if (strcmp(obj_desc->type, "dprc") == 0) {
658 struct fsl_mc_io *mc_io2;
660 mc_dev->flags |= FSL_MC_IS_DPRC;
663 * To get the DPRC's ICID, we need to open the DPRC
664 * in get_dprc_icid(). For child DPRCs, we do so using the
665 * parent DPRC's MC portal instead of the child DPRC's MC
666 * portal, in case the child DPRC is already opened with
667 * its own portal (e.g., the DPRC used by AIOP).
669 * NOTE: There cannot be more than one active open for a
670 * given MC object, using the same MC portal.
674 * device being added is a child DPRC device
676 mc_io2 = parent_mc_dev->mc_io;
679 * device being added is the root DPRC device
683 goto error_cleanup_dev;
689 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
691 goto error_cleanup_dev;
694 * A non-DPRC object has to be a child of a DPRC, use the
695 * parent's ICID and interrupt domain.
697 mc_dev->icid = parent_mc_dev->icid;
698 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
699 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
700 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
701 dev_set_msi_domain(&mc_dev->dev,
702 dev_get_msi_domain(&parent_mc_dev->dev));
706 * Get MMIO regions for the device from the MC:
708 * NOTE: the root DPRC is a special case as its MMIO region is
709 * obtained from the device tree
711 if (parent_mc_dev && obj_desc->region_count != 0) {
712 error = fsl_mc_device_get_mmio_regions(mc_dev,
715 goto error_cleanup_dev;
719 * The device-specific probe callback will get invoked by device_add()
721 error = device_add(&mc_dev->dev);
724 "device_add() failed for device %s: %d\n",
725 dev_name(&mc_dev->dev), error);
726 goto error_cleanup_dev;
729 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
731 *new_mc_dev = mc_dev;
735 kfree(mc_dev->regions);
741 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
744 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
747 * @mc_dev: Pointer to an fsl-mc device
749 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
752 * The device-specific remove callback will get invoked by device_del()
754 device_del(&mc_dev->dev);
755 put_device(&mc_dev->dev);
757 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
759 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
761 struct fsl_mc_device *mc_bus_dev, *endpoint;
762 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
763 struct dprc_endpoint endpoint1 = {{ 0 }};
764 struct dprc_endpoint endpoint2 = {{ 0 }};
767 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
768 strcpy(endpoint1.type, mc_dev->obj_desc.type);
769 endpoint1.id = mc_dev->obj_desc.id;
771 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
772 mc_bus_dev->mc_handle,
773 &endpoint1, &endpoint2,
776 if (err == -ENOTCONN || state == -1)
777 return ERR_PTR(-ENOTCONN);
780 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
784 strcpy(endpoint_desc.type, endpoint2.type);
785 endpoint_desc.id = endpoint2.id;
786 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
790 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
792 static int parse_mc_ranges(struct device *dev,
796 const __be32 **ranges_start)
799 int range_tuple_cell_count;
802 struct device_node *mc_node = dev->of_node;
804 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
805 if (!(*ranges_start) || !ranges_len) {
807 "missing or empty ranges property for device tree node '%pOFn'\n",
812 *paddr_cells = of_n_addr_cells(mc_node);
814 prop = of_get_property(mc_node, "#address-cells", NULL);
816 *mc_addr_cells = be32_to_cpup(prop);
818 *mc_addr_cells = *paddr_cells;
820 prop = of_get_property(mc_node, "#size-cells", NULL);
822 *mc_size_cells = be32_to_cpup(prop);
824 *mc_size_cells = of_n_size_cells(mc_node);
826 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
829 tuple_len = range_tuple_cell_count * sizeof(__be32);
830 if (ranges_len % tuple_len != 0) {
831 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
835 return ranges_len / tuple_len;
838 static int get_mc_addr_translation_ranges(struct device *dev,
839 struct fsl_mc_addr_translation_range
848 const __be32 *ranges_start;
851 ret = parse_mc_ranges(dev,
862 * Missing or empty ranges property ("ranges;") for the
863 * 'fsl,qoriq-mc' node. In this case, identity mapping
870 *ranges = devm_kcalloc(dev, *num_ranges,
871 sizeof(struct fsl_mc_addr_translation_range),
877 for (i = 0; i < *num_ranges; ++i) {
878 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
880 range->mc_region_type = of_read_number(cell, 1);
881 range->start_mc_offset = of_read_number(cell + 1,
883 cell += mc_addr_cells;
884 range->start_phys_addr = of_read_number(cell, paddr_cells);
886 range->end_mc_offset = range->start_mc_offset +
887 of_read_number(cell, mc_size_cells);
889 cell += mc_size_cells;
896 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
899 static int fsl_mc_bus_probe(struct platform_device *pdev)
901 struct fsl_mc_obj_desc obj_desc;
904 struct fsl_mc_device *mc_bus_dev = NULL;
905 struct fsl_mc_io *mc_io = NULL;
907 phys_addr_t mc_portal_phys_addr;
908 u32 mc_portal_size, mc_stream_id;
909 struct resource *plat_res;
911 if (!iommu_present(&fsl_mc_bus_type))
912 return -EPROBE_DEFER;
914 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
918 platform_set_drvdata(pdev, mc);
920 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
921 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
922 if (IS_ERR(mc->fsl_mc_regs))
923 return PTR_ERR(mc->fsl_mc_regs);
925 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
926 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
928 * HW ORs the PL and BMT bit, places the result in bit 15 of
929 * the StreamID and ORs in the ICID. Calculate it accordingly.
931 mc_stream_id = (mc_stream_id & 0xffff) |
932 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
934 error = acpi_dma_configure_id(&pdev->dev, DEV_DMA_COHERENT,
937 dev_warn(&pdev->dev, "failed to configure dma: %d.\n",
942 * Get physical address of MC portal for the root DPRC:
944 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
945 mc_portal_phys_addr = plat_res->start;
946 mc_portal_size = resource_size(plat_res);
947 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
948 mc_portal_size, NULL,
949 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
953 error = mc_get_version(mc_io, 0, &mc_version);
956 "mc_get_version() failed with error %d\n", error);
957 goto error_cleanup_mc_io;
960 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
961 mc_version.major, mc_version.minor, mc_version.revision);
963 if (dev_of_node(&pdev->dev)) {
964 error = get_mc_addr_translation_ranges(&pdev->dev,
965 &mc->translation_ranges,
966 &mc->num_translation_ranges);
968 goto error_cleanup_mc_io;
971 error = dprc_get_container_id(mc_io, 0, &container_id);
974 "dprc_get_container_id() failed: %d\n", error);
975 goto error_cleanup_mc_io;
978 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
979 error = dprc_get_api_version(mc_io, 0,
981 &obj_desc.ver_minor);
983 goto error_cleanup_mc_io;
985 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
986 strcpy(obj_desc.type, "dprc");
987 obj_desc.id = container_id;
988 obj_desc.irq_count = 1;
989 obj_desc.region_count = 0;
991 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
993 goto error_cleanup_mc_io;
995 mc->root_mc_bus_dev = mc_bus_dev;
996 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1000 fsl_destroy_mc_io(mc_io);
1005 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1008 static int fsl_mc_bus_remove(struct platform_device *pdev)
1010 struct fsl_mc *mc = platform_get_drvdata(pdev);
1012 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1015 fsl_mc_device_remove(mc->root_mc_bus_dev);
1017 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
1018 mc->root_mc_bus_dev->mc_io = NULL;
1023 static const struct of_device_id fsl_mc_bus_match_table[] = {
1024 {.compatible = "fsl,qoriq-mc",},
1028 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1030 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1034 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1036 static struct platform_driver fsl_mc_bus_driver = {
1038 .name = "fsl_mc_bus",
1040 .of_match_table = fsl_mc_bus_match_table,
1041 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1043 .probe = fsl_mc_bus_probe,
1044 .remove = fsl_mc_bus_remove,
1047 static int __init fsl_mc_bus_driver_init(void)
1051 error = bus_register(&fsl_mc_bus_type);
1053 pr_err("bus type registration failed: %d\n", error);
1054 goto error_cleanup_cache;
1057 error = platform_driver_register(&fsl_mc_bus_driver);
1059 pr_err("platform_driver_register() failed: %d\n", error);
1060 goto error_cleanup_bus;
1063 error = dprc_driver_init();
1065 goto error_cleanup_driver;
1067 error = fsl_mc_allocator_driver_init();
1069 goto error_cleanup_dprc_driver;
1073 error_cleanup_dprc_driver:
1076 error_cleanup_driver:
1077 platform_driver_unregister(&fsl_mc_bus_driver);
1080 bus_unregister(&fsl_mc_bus_type);
1082 error_cleanup_cache:
1085 postcore_initcall(fsl_mc_bus_driver_init);