1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale Management Complex (MC) bus driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6 * Author: German Rivera <German.Rivera@freescale.com>
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 static struct device_type *fsl_mc_get_device_type(const char *type)
218 static const struct {
219 struct device_type *dev_type;
222 { &fsl_mc_bus_dprc_type, "dprc" },
223 { &fsl_mc_bus_dpni_type, "dpni" },
224 { &fsl_mc_bus_dpio_type, "dpio" },
225 { &fsl_mc_bus_dpsw_type, "dpsw" },
226 { &fsl_mc_bus_dpbp_type, "dpbp" },
227 { &fsl_mc_bus_dpcon_type, "dpcon" },
228 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
229 { &fsl_mc_bus_dpmac_type, "dpmac" },
230 { &fsl_mc_bus_dprtc_type, "dprtc" },
231 { &fsl_mc_bus_dpseci_type, "dpseci" },
236 for (i = 0; dev_types[i].dev_type; i++)
237 if (!strcmp(dev_types[i].type, type))
238 return dev_types[i].dev_type;
243 static int fsl_mc_driver_probe(struct device *dev)
245 struct fsl_mc_driver *mc_drv;
246 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
249 mc_drv = to_fsl_mc_driver(dev->driver);
251 error = mc_drv->probe(mc_dev);
253 if (error != -EPROBE_DEFER)
254 dev_err(dev, "%s failed: %d\n", __func__, error);
261 static int fsl_mc_driver_remove(struct device *dev)
263 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
264 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
267 error = mc_drv->remove(mc_dev);
269 dev_err(dev, "%s failed: %d\n", __func__, error);
276 static void fsl_mc_driver_shutdown(struct device *dev)
278 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
279 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
281 mc_drv->shutdown(mc_dev);
285 * __fsl_mc_driver_register - registers a child device driver with the
288 * This function is implicitly invoked from the registration function of
289 * fsl_mc device drivers, which is generated by the
290 * module_fsl_mc_driver() macro.
292 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
293 struct module *owner)
297 mc_driver->driver.owner = owner;
298 mc_driver->driver.bus = &fsl_mc_bus_type;
300 if (mc_driver->probe)
301 mc_driver->driver.probe = fsl_mc_driver_probe;
303 if (mc_driver->remove)
304 mc_driver->driver.remove = fsl_mc_driver_remove;
306 if (mc_driver->shutdown)
307 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
309 error = driver_register(&mc_driver->driver);
311 pr_err("driver_register() failed for %s: %d\n",
312 mc_driver->driver.name, error);
318 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
321 * fsl_mc_driver_unregister - unregisters a device driver from the
324 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
326 driver_unregister(&mc_driver->driver);
328 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
331 * mc_get_version() - Retrieves the Management Complex firmware
332 * version information
333 * @mc_io: Pointer to opaque I/O object
334 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
335 * @mc_ver_info: Returned version information structure
337 * Return: '0' on Success; Error code otherwise.
339 static int mc_get_version(struct fsl_mc_io *mc_io,
341 struct fsl_mc_version *mc_ver_info)
343 struct fsl_mc_command cmd = { 0 };
344 struct dpmng_rsp_get_version *rsp_params;
347 /* prepare command */
348 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
352 /* send command to mc*/
353 err = mc_send_command(mc_io, &cmd);
357 /* retrieve response parameters */
358 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
359 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
360 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
361 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
367 * fsl_mc_get_version - function to retrieve the MC f/w version information
369 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
371 struct fsl_mc_version *fsl_mc_get_version(void)
373 if (mc_version.major)
378 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
381 * fsl_mc_get_root_dprc - function to traverse to the root dprc
383 void fsl_mc_get_root_dprc(struct device *dev,
384 struct device **root_dprc_dev)
387 *root_dprc_dev = NULL;
388 } else if (!dev_is_fsl_mc(dev)) {
389 *root_dprc_dev = NULL;
391 *root_dprc_dev = dev;
392 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
393 *root_dprc_dev = (*root_dprc_dev)->parent;
397 static int get_dprc_attr(struct fsl_mc_io *mc_io,
398 int container_id, struct dprc_attributes *attr)
403 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
405 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
409 memset(attr, 0, sizeof(struct dprc_attributes));
410 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
412 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
420 (void)dprc_close(mc_io, 0, dprc_handle);
424 static int get_dprc_icid(struct fsl_mc_io *mc_io,
425 int container_id, u16 *icid)
427 struct dprc_attributes attr;
430 error = get_dprc_attr(mc_io, container_id, &attr);
437 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
438 enum dprc_region_type mc_region_type,
439 u64 mc_offset, phys_addr_t *phys_addr)
442 struct device *root_dprc_dev;
445 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
446 mc = dev_get_drvdata(root_dprc_dev->parent);
448 if (mc->num_translation_ranges == 0) {
450 * Do identity mapping:
452 *phys_addr = mc_offset;
456 for (i = 0; i < mc->num_translation_ranges; i++) {
457 struct fsl_mc_addr_translation_range *range =
458 &mc->translation_ranges[i];
460 if (mc_region_type == range->mc_region_type &&
461 mc_offset >= range->start_mc_offset &&
462 mc_offset < range->end_mc_offset) {
463 *phys_addr = range->start_phys_addr +
464 (mc_offset - range->start_mc_offset);
472 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
473 struct fsl_mc_device *mc_bus_dev)
477 struct resource *regions;
478 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
479 struct device *parent_dev = mc_dev->dev.parent;
480 enum dprc_region_type mc_region_type;
482 if (is_fsl_mc_bus_dprc(mc_dev) ||
483 is_fsl_mc_bus_dpmcp(mc_dev)) {
484 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
485 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
486 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
489 * This function should not have been called for this MC object
490 * type, as this object type is not supposed to have MMIO
496 regions = kmalloc_array(obj_desc->region_count,
497 sizeof(regions[0]), GFP_KERNEL);
501 for (i = 0; i < obj_desc->region_count; i++) {
502 struct dprc_region_desc region_desc;
504 error = dprc_get_obj_region(mc_bus_dev->mc_io,
506 mc_bus_dev->mc_handle,
508 obj_desc->id, i, ®ion_desc);
511 "dprc_get_obj_region() failed: %d\n", error);
512 goto error_cleanup_regions;
515 * Older MC only returned region offset and no base address
516 * If base address is in the region_desc use it otherwise
517 * revert to old mechanism
519 if (region_desc.base_address)
520 regions[i].start = region_desc.base_address +
521 region_desc.base_offset;
523 error = translate_mc_addr(mc_dev, mc_region_type,
524 region_desc.base_offset,
529 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
530 region_desc.base_offset,
531 obj_desc->type, obj_desc->id, i);
532 goto error_cleanup_regions;
535 regions[i].end = regions[i].start + region_desc.size - 1;
536 regions[i].name = "fsl-mc object MMIO region";
537 regions[i].flags = IORESOURCE_IO;
538 if (region_desc.flags & DPRC_REGION_CACHEABLE)
539 regions[i].flags |= IORESOURCE_CACHEABLE;
540 if (region_desc.flags & DPRC_REGION_SHAREABLE)
541 regions[i].flags |= IORESOURCE_MEM;
544 mc_dev->regions = regions;
547 error_cleanup_regions:
553 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
555 bool fsl_mc_is_root_dprc(struct device *dev)
557 struct device *root_dprc_dev;
559 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
562 return dev == root_dprc_dev;
565 static void fsl_mc_device_release(struct device *dev)
567 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
569 kfree(mc_dev->regions);
571 if (is_fsl_mc_bus_dprc(mc_dev))
572 kfree(to_fsl_mc_bus(mc_dev));
578 * Add a newly discovered fsl-mc device to be visible in Linux
580 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
581 struct fsl_mc_io *mc_io,
582 struct device *parent_dev,
583 struct fsl_mc_device **new_mc_dev)
586 struct fsl_mc_device *mc_dev = NULL;
587 struct fsl_mc_bus *mc_bus = NULL;
588 struct fsl_mc_device *parent_mc_dev;
590 if (dev_is_fsl_mc(parent_dev))
591 parent_mc_dev = to_fsl_mc_device(parent_dev);
593 parent_mc_dev = NULL;
595 if (strcmp(obj_desc->type, "dprc") == 0) {
597 * Allocate an MC bus device object:
599 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
603 mc_dev = &mc_bus->mc_dev;
606 * Allocate a regular fsl_mc_device object:
608 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
613 mc_dev->obj_desc = *obj_desc;
614 mc_dev->mc_io = mc_io;
615 device_initialize(&mc_dev->dev);
616 mc_dev->dev.parent = parent_dev;
617 mc_dev->dev.bus = &fsl_mc_bus_type;
618 mc_dev->dev.release = fsl_mc_device_release;
619 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
620 if (!mc_dev->dev.type) {
622 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
623 goto error_cleanup_dev;
625 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
627 if (strcmp(obj_desc->type, "dprc") == 0) {
628 struct fsl_mc_io *mc_io2;
630 mc_dev->flags |= FSL_MC_IS_DPRC;
633 * To get the DPRC's ICID, we need to open the DPRC
634 * in get_dprc_icid(). For child DPRCs, we do so using the
635 * parent DPRC's MC portal instead of the child DPRC's MC
636 * portal, in case the child DPRC is already opened with
637 * its own portal (e.g., the DPRC used by AIOP).
639 * NOTE: There cannot be more than one active open for a
640 * given MC object, using the same MC portal.
644 * device being added is a child DPRC device
646 mc_io2 = parent_mc_dev->mc_io;
649 * device being added is the root DPRC device
653 goto error_cleanup_dev;
659 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
661 goto error_cleanup_dev;
664 * A non-DPRC object has to be a child of a DPRC, use the
665 * parent's ICID and interrupt domain.
667 mc_dev->icid = parent_mc_dev->icid;
668 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
669 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
670 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
671 dev_set_msi_domain(&mc_dev->dev,
672 dev_get_msi_domain(&parent_mc_dev->dev));
676 * Get MMIO regions for the device from the MC:
678 * NOTE: the root DPRC is a special case as its MMIO region is
679 * obtained from the device tree
681 if (parent_mc_dev && obj_desc->region_count != 0) {
682 error = fsl_mc_device_get_mmio_regions(mc_dev,
685 goto error_cleanup_dev;
689 * The device-specific probe callback will get invoked by device_add()
691 error = device_add(&mc_dev->dev);
694 "device_add() failed for device %s: %d\n",
695 dev_name(&mc_dev->dev), error);
696 goto error_cleanup_dev;
699 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
701 *new_mc_dev = mc_dev;
705 kfree(mc_dev->regions);
711 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
714 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
717 * @mc_dev: Pointer to an fsl-mc device
719 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
722 * The device-specific remove callback will get invoked by device_del()
724 device_del(&mc_dev->dev);
725 put_device(&mc_dev->dev);
727 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
729 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
731 struct fsl_mc_device *mc_bus_dev, *endpoint;
732 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
733 struct dprc_endpoint endpoint1 = {{ 0 }};
734 struct dprc_endpoint endpoint2 = {{ 0 }};
737 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
738 strcpy(endpoint1.type, mc_dev->obj_desc.type);
739 endpoint1.id = mc_dev->obj_desc.id;
741 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
742 mc_bus_dev->mc_handle,
743 &endpoint1, &endpoint2,
746 if (err == -ENOTCONN || state == -1)
747 return ERR_PTR(-ENOTCONN);
750 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
754 strcpy(endpoint_desc.type, endpoint2.type);
755 endpoint_desc.id = endpoint2.id;
756 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
760 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
762 static int parse_mc_ranges(struct device *dev,
766 const __be32 **ranges_start)
769 int range_tuple_cell_count;
772 struct device_node *mc_node = dev->of_node;
774 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
775 if (!(*ranges_start) || !ranges_len) {
777 "missing or empty ranges property for device tree node '%pOFn'\n",
782 *paddr_cells = of_n_addr_cells(mc_node);
784 prop = of_get_property(mc_node, "#address-cells", NULL);
786 *mc_addr_cells = be32_to_cpup(prop);
788 *mc_addr_cells = *paddr_cells;
790 prop = of_get_property(mc_node, "#size-cells", NULL);
792 *mc_size_cells = be32_to_cpup(prop);
794 *mc_size_cells = of_n_size_cells(mc_node);
796 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
799 tuple_len = range_tuple_cell_count * sizeof(__be32);
800 if (ranges_len % tuple_len != 0) {
801 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
805 return ranges_len / tuple_len;
808 static int get_mc_addr_translation_ranges(struct device *dev,
809 struct fsl_mc_addr_translation_range
818 const __be32 *ranges_start;
821 ret = parse_mc_ranges(dev,
832 * Missing or empty ranges property ("ranges;") for the
833 * 'fsl,qoriq-mc' node. In this case, identity mapping
840 *ranges = devm_kcalloc(dev, *num_ranges,
841 sizeof(struct fsl_mc_addr_translation_range),
847 for (i = 0; i < *num_ranges; ++i) {
848 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
850 range->mc_region_type = of_read_number(cell, 1);
851 range->start_mc_offset = of_read_number(cell + 1,
853 cell += mc_addr_cells;
854 range->start_phys_addr = of_read_number(cell, paddr_cells);
856 range->end_mc_offset = range->start_mc_offset +
857 of_read_number(cell, mc_size_cells);
859 cell += mc_size_cells;
866 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
869 static int fsl_mc_bus_probe(struct platform_device *pdev)
871 struct fsl_mc_obj_desc obj_desc;
874 struct fsl_mc_device *mc_bus_dev = NULL;
875 struct fsl_mc_io *mc_io = NULL;
877 phys_addr_t mc_portal_phys_addr;
878 u32 mc_portal_size, mc_stream_id;
879 struct resource *plat_res;
881 if (!iommu_present(&fsl_mc_bus_type))
882 return -EPROBE_DEFER;
884 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
888 platform_set_drvdata(pdev, mc);
890 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
891 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
892 if (IS_ERR(mc->fsl_mc_regs))
893 return PTR_ERR(mc->fsl_mc_regs);
895 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
896 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
898 * HW ORs the PL and BMT bit, places the result in bit 15 of
899 * the StreamID and ORs in the ICID. Calculate it accordingly.
901 mc_stream_id = (mc_stream_id & 0xffff) |
902 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
904 error = acpi_dma_configure_id(&pdev->dev, DEV_DMA_COHERENT,
907 dev_warn(&pdev->dev, "failed to configure dma: %d.\n",
912 * Get physical address of MC portal for the root DPRC:
914 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
915 mc_portal_phys_addr = plat_res->start;
916 mc_portal_size = resource_size(plat_res);
917 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
918 mc_portal_size, NULL,
919 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
923 error = mc_get_version(mc_io, 0, &mc_version);
926 "mc_get_version() failed with error %d\n", error);
927 goto error_cleanup_mc_io;
930 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
931 mc_version.major, mc_version.minor, mc_version.revision);
933 if (dev_of_node(&pdev->dev)) {
934 error = get_mc_addr_translation_ranges(&pdev->dev,
935 &mc->translation_ranges,
936 &mc->num_translation_ranges);
938 goto error_cleanup_mc_io;
941 error = dprc_get_container_id(mc_io, 0, &container_id);
944 "dprc_get_container_id() failed: %d\n", error);
945 goto error_cleanup_mc_io;
948 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
949 error = dprc_get_api_version(mc_io, 0,
951 &obj_desc.ver_minor);
953 goto error_cleanup_mc_io;
955 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
956 strcpy(obj_desc.type, "dprc");
957 obj_desc.id = container_id;
958 obj_desc.irq_count = 1;
959 obj_desc.region_count = 0;
961 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
963 goto error_cleanup_mc_io;
965 mc->root_mc_bus_dev = mc_bus_dev;
966 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
970 fsl_destroy_mc_io(mc_io);
975 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
978 static int fsl_mc_bus_remove(struct platform_device *pdev)
980 struct fsl_mc *mc = platform_get_drvdata(pdev);
982 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
985 fsl_mc_device_remove(mc->root_mc_bus_dev);
987 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
988 mc->root_mc_bus_dev->mc_io = NULL;
993 static const struct of_device_id fsl_mc_bus_match_table[] = {
994 {.compatible = "fsl,qoriq-mc",},
998 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1000 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1004 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1006 static struct platform_driver fsl_mc_bus_driver = {
1008 .name = "fsl_mc_bus",
1010 .of_match_table = fsl_mc_bus_match_table,
1011 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1013 .probe = fsl_mc_bus_probe,
1014 .remove = fsl_mc_bus_remove,
1017 static int __init fsl_mc_bus_driver_init(void)
1021 error = bus_register(&fsl_mc_bus_type);
1023 pr_err("bus type registration failed: %d\n", error);
1024 goto error_cleanup_cache;
1027 error = platform_driver_register(&fsl_mc_bus_driver);
1029 pr_err("platform_driver_register() failed: %d\n", error);
1030 goto error_cleanup_bus;
1033 error = dprc_driver_init();
1035 goto error_cleanup_driver;
1037 error = fsl_mc_allocator_driver_init();
1039 goto error_cleanup_dprc_driver;
1043 error_cleanup_dprc_driver:
1046 error_cleanup_driver:
1047 platform_driver_unregister(&fsl_mc_bus_driver);
1050 bus_unregister(&fsl_mc_bus_type);
1052 error_cleanup_cache:
1055 postcore_initcall(fsl_mc_bus_driver_init);