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
41 struct fsl_mc_device
*root_mc_bus_dev
;
42 u8 num_translation_ranges
;
43 struct fsl_mc_addr_translation_range
*translation_ranges
;
48 * struct fsl_mc_addr_translation_range - bus to system address translation
50 * @mc_region_type: Type of MC region for the range being translated
51 * @start_mc_offset: Start MC offset of the range being translated
52 * @end_mc_offset: MC offset of the first byte after the range (last MC
53 * offset of the range is end_mc_offset - 1)
54 * @start_phys_addr: system physical address corresponding to start_mc_addr
56 struct fsl_mc_addr_translation_range
{
57 enum dprc_region_type mc_region_type
;
60 phys_addr_t start_phys_addr
;
63 #define FSL_MC_GCR1 0x0
64 #define GCR1_P1_STOP BIT(31)
66 #define FSL_MC_FAPR 0x28
67 #define MC_FAPR_PL BIT(18)
68 #define MC_FAPR_BMT BIT(17)
71 * fsl_mc_bus_match - device to driver matching callback
72 * @dev: the fsl-mc device to match against
73 * @drv: the device driver to search for matching fsl-mc object type
76 * Returns 1 on success, 0 otherwise.
78 static int fsl_mc_bus_match(struct device
*dev
, struct device_driver
*drv
)
80 const struct fsl_mc_device_id
*id
;
81 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
82 struct fsl_mc_driver
*mc_drv
= to_fsl_mc_driver(drv
);
85 /* When driver_override is set, only bind to the matching driver */
86 if (mc_dev
->driver_override
) {
87 found
= !strcmp(mc_dev
->driver_override
, mc_drv
->driver
.name
);
91 if (!mc_drv
->match_id_table
)
95 * If the object is not 'plugged' don't match.
96 * Only exception is the root DPRC, which is a special case.
98 if ((mc_dev
->obj_desc
.state
& FSL_MC_OBJ_STATE_PLUGGED
) == 0 &&
99 !fsl_mc_is_root_dprc(&mc_dev
->dev
))
103 * Traverse the match_id table of the given driver, trying to find
104 * a matching for the given device.
106 for (id
= mc_drv
->match_id_table
; id
->vendor
!= 0x0; id
++) {
107 if (id
->vendor
== mc_dev
->obj_desc
.vendor
&&
108 strcmp(id
->obj_type
, mc_dev
->obj_desc
.type
) == 0) {
116 dev_dbg(dev
, "%smatched\n", found
? "" : "not ");
121 * fsl_mc_bus_uevent - callback invoked when a device is added
123 static int fsl_mc_bus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
125 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
127 if (add_uevent_var(env
, "MODALIAS=fsl-mc:v%08Xd%s",
128 mc_dev
->obj_desc
.vendor
,
129 mc_dev
->obj_desc
.type
))
135 static int fsl_mc_dma_configure(struct device
*dev
)
137 struct device
*dma_dev
= dev
;
138 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
139 u32 input_id
= mc_dev
->icid
;
141 while (dev_is_fsl_mc(dma_dev
))
142 dma_dev
= dma_dev
->parent
;
144 if (dev_of_node(dma_dev
))
145 return of_dma_configure_id(dev
, dma_dev
->of_node
, 0, &input_id
);
147 return acpi_dma_configure_id(dev
, DEV_DMA_COHERENT
, &input_id
);
150 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
153 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
155 return sprintf(buf
, "fsl-mc:v%08Xd%s\n", mc_dev
->obj_desc
.vendor
,
156 mc_dev
->obj_desc
.type
);
158 static DEVICE_ATTR_RO(modalias
);
160 static ssize_t
driver_override_store(struct device
*dev
,
161 struct device_attribute
*attr
,
162 const char *buf
, size_t count
)
164 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
165 char *driver_override
, *old
= mc_dev
->driver_override
;
168 if (WARN_ON(dev
->bus
!= &fsl_mc_bus_type
))
171 if (count
>= (PAGE_SIZE
- 1))
174 driver_override
= kstrndup(buf
, count
, GFP_KERNEL
);
175 if (!driver_override
)
178 cp
= strchr(driver_override
, '\n');
182 if (strlen(driver_override
)) {
183 mc_dev
->driver_override
= driver_override
;
185 kfree(driver_override
);
186 mc_dev
->driver_override
= NULL
;
194 static ssize_t
driver_override_show(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
199 return snprintf(buf
, PAGE_SIZE
, "%s\n", mc_dev
->driver_override
);
201 static DEVICE_ATTR_RW(driver_override
);
203 static struct attribute
*fsl_mc_dev_attrs
[] = {
204 &dev_attr_modalias
.attr
,
205 &dev_attr_driver_override
.attr
,
209 ATTRIBUTE_GROUPS(fsl_mc_dev
);
211 struct bus_type fsl_mc_bus_type
= {
213 .match
= fsl_mc_bus_match
,
214 .uevent
= fsl_mc_bus_uevent
,
215 .dma_configure
= fsl_mc_dma_configure
,
216 .dev_groups
= fsl_mc_dev_groups
,
218 EXPORT_SYMBOL_GPL(fsl_mc_bus_type
);
220 struct device_type fsl_mc_bus_dprc_type
= {
221 .name
= "fsl_mc_bus_dprc"
223 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type
);
225 struct device_type fsl_mc_bus_dpni_type
= {
226 .name
= "fsl_mc_bus_dpni"
228 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type
);
230 struct device_type fsl_mc_bus_dpio_type
= {
231 .name
= "fsl_mc_bus_dpio"
233 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type
);
235 struct device_type fsl_mc_bus_dpsw_type
= {
236 .name
= "fsl_mc_bus_dpsw"
238 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type
);
240 struct device_type fsl_mc_bus_dpbp_type
= {
241 .name
= "fsl_mc_bus_dpbp"
243 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type
);
245 struct device_type fsl_mc_bus_dpcon_type
= {
246 .name
= "fsl_mc_bus_dpcon"
248 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type
);
250 struct device_type fsl_mc_bus_dpmcp_type
= {
251 .name
= "fsl_mc_bus_dpmcp"
253 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type
);
255 struct device_type fsl_mc_bus_dpmac_type
= {
256 .name
= "fsl_mc_bus_dpmac"
258 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type
);
260 struct device_type fsl_mc_bus_dprtc_type
= {
261 .name
= "fsl_mc_bus_dprtc"
263 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type
);
265 struct device_type fsl_mc_bus_dpseci_type
= {
266 .name
= "fsl_mc_bus_dpseci"
268 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type
);
270 struct device_type fsl_mc_bus_dpdmux_type
= {
271 .name
= "fsl_mc_bus_dpdmux"
273 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type
);
275 struct device_type fsl_mc_bus_dpdcei_type
= {
276 .name
= "fsl_mc_bus_dpdcei"
278 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type
);
280 struct device_type fsl_mc_bus_dpaiop_type
= {
281 .name
= "fsl_mc_bus_dpaiop"
283 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type
);
285 struct device_type fsl_mc_bus_dpci_type
= {
286 .name
= "fsl_mc_bus_dpci"
288 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type
);
290 struct device_type fsl_mc_bus_dpdmai_type
= {
291 .name
= "fsl_mc_bus_dpdmai"
293 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type
);
295 static struct device_type
*fsl_mc_get_device_type(const char *type
)
297 static const struct {
298 struct device_type
*dev_type
;
301 { &fsl_mc_bus_dprc_type
, "dprc" },
302 { &fsl_mc_bus_dpni_type
, "dpni" },
303 { &fsl_mc_bus_dpio_type
, "dpio" },
304 { &fsl_mc_bus_dpsw_type
, "dpsw" },
305 { &fsl_mc_bus_dpbp_type
, "dpbp" },
306 { &fsl_mc_bus_dpcon_type
, "dpcon" },
307 { &fsl_mc_bus_dpmcp_type
, "dpmcp" },
308 { &fsl_mc_bus_dpmac_type
, "dpmac" },
309 { &fsl_mc_bus_dprtc_type
, "dprtc" },
310 { &fsl_mc_bus_dpseci_type
, "dpseci" },
311 { &fsl_mc_bus_dpdmux_type
, "dpdmux" },
312 { &fsl_mc_bus_dpdcei_type
, "dpdcei" },
313 { &fsl_mc_bus_dpaiop_type
, "dpaiop" },
314 { &fsl_mc_bus_dpci_type
, "dpci" },
315 { &fsl_mc_bus_dpdmai_type
, "dpdmai" },
320 for (i
= 0; dev_types
[i
].dev_type
; i
++)
321 if (!strcmp(dev_types
[i
].type
, type
))
322 return dev_types
[i
].dev_type
;
327 static int fsl_mc_driver_probe(struct device
*dev
)
329 struct fsl_mc_driver
*mc_drv
;
330 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
333 mc_drv
= to_fsl_mc_driver(dev
->driver
);
335 error
= mc_drv
->probe(mc_dev
);
337 if (error
!= -EPROBE_DEFER
)
338 dev_err(dev
, "%s failed: %d\n", __func__
, error
);
345 static int fsl_mc_driver_remove(struct device
*dev
)
347 struct fsl_mc_driver
*mc_drv
= to_fsl_mc_driver(dev
->driver
);
348 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
351 error
= mc_drv
->remove(mc_dev
);
353 dev_err(dev
, "%s failed: %d\n", __func__
, error
);
360 static void fsl_mc_driver_shutdown(struct device
*dev
)
362 struct fsl_mc_driver
*mc_drv
= to_fsl_mc_driver(dev
->driver
);
363 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
365 mc_drv
->shutdown(mc_dev
);
369 * __fsl_mc_driver_register - registers a child device driver with the
372 * This function is implicitly invoked from the registration function of
373 * fsl_mc device drivers, which is generated by the
374 * module_fsl_mc_driver() macro.
376 int __fsl_mc_driver_register(struct fsl_mc_driver
*mc_driver
,
377 struct module
*owner
)
381 mc_driver
->driver
.owner
= owner
;
382 mc_driver
->driver
.bus
= &fsl_mc_bus_type
;
384 if (mc_driver
->probe
)
385 mc_driver
->driver
.probe
= fsl_mc_driver_probe
;
387 if (mc_driver
->remove
)
388 mc_driver
->driver
.remove
= fsl_mc_driver_remove
;
390 if (mc_driver
->shutdown
)
391 mc_driver
->driver
.shutdown
= fsl_mc_driver_shutdown
;
393 error
= driver_register(&mc_driver
->driver
);
395 pr_err("driver_register() failed for %s: %d\n",
396 mc_driver
->driver
.name
, error
);
402 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register
);
405 * fsl_mc_driver_unregister - unregisters a device driver from the
408 void fsl_mc_driver_unregister(struct fsl_mc_driver
*mc_driver
)
410 driver_unregister(&mc_driver
->driver
);
412 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister
);
415 * mc_get_version() - Retrieves the Management Complex firmware
416 * version information
417 * @mc_io: Pointer to opaque I/O object
418 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
419 * @mc_ver_info: Returned version information structure
421 * Return: '0' on Success; Error code otherwise.
423 static int mc_get_version(struct fsl_mc_io
*mc_io
,
425 struct fsl_mc_version
*mc_ver_info
)
427 struct fsl_mc_command cmd
= { 0 };
428 struct dpmng_rsp_get_version
*rsp_params
;
431 /* prepare command */
432 cmd
.header
= mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION
,
436 /* send command to mc*/
437 err
= mc_send_command(mc_io
, &cmd
);
441 /* retrieve response parameters */
442 rsp_params
= (struct dpmng_rsp_get_version
*)cmd
.params
;
443 mc_ver_info
->revision
= le32_to_cpu(rsp_params
->revision
);
444 mc_ver_info
->major
= le32_to_cpu(rsp_params
->version_major
);
445 mc_ver_info
->minor
= le32_to_cpu(rsp_params
->version_minor
);
451 * fsl_mc_get_version - function to retrieve the MC f/w version information
453 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
455 struct fsl_mc_version
*fsl_mc_get_version(void)
457 if (mc_version
.major
)
462 EXPORT_SYMBOL_GPL(fsl_mc_get_version
);
465 * fsl_mc_get_root_dprc - function to traverse to the root dprc
467 void fsl_mc_get_root_dprc(struct device
*dev
,
468 struct device
**root_dprc_dev
)
471 *root_dprc_dev
= NULL
;
472 } else if (!dev_is_fsl_mc(dev
)) {
473 *root_dprc_dev
= NULL
;
475 *root_dprc_dev
= dev
;
476 while (dev_is_fsl_mc((*root_dprc_dev
)->parent
))
477 *root_dprc_dev
= (*root_dprc_dev
)->parent
;
481 static int get_dprc_attr(struct fsl_mc_io
*mc_io
,
482 int container_id
, struct dprc_attributes
*attr
)
487 error
= dprc_open(mc_io
, 0, container_id
, &dprc_handle
);
489 dev_err(mc_io
->dev
, "dprc_open() failed: %d\n", error
);
493 memset(attr
, 0, sizeof(struct dprc_attributes
));
494 error
= dprc_get_attributes(mc_io
, 0, dprc_handle
, attr
);
496 dev_err(mc_io
->dev
, "dprc_get_attributes() failed: %d\n",
504 (void)dprc_close(mc_io
, 0, dprc_handle
);
508 static int get_dprc_icid(struct fsl_mc_io
*mc_io
,
509 int container_id
, u32
*icid
)
511 struct dprc_attributes attr
;
514 error
= get_dprc_attr(mc_io
, container_id
, &attr
);
521 static int translate_mc_addr(struct fsl_mc_device
*mc_dev
,
522 enum dprc_region_type mc_region_type
,
523 u64 mc_offset
, phys_addr_t
*phys_addr
)
526 struct device
*root_dprc_dev
;
529 fsl_mc_get_root_dprc(&mc_dev
->dev
, &root_dprc_dev
);
530 mc
= dev_get_drvdata(root_dprc_dev
->parent
);
532 if (mc
->num_translation_ranges
== 0) {
534 * Do identity mapping:
536 *phys_addr
= mc_offset
;
540 for (i
= 0; i
< mc
->num_translation_ranges
; i
++) {
541 struct fsl_mc_addr_translation_range
*range
=
542 &mc
->translation_ranges
[i
];
544 if (mc_region_type
== range
->mc_region_type
&&
545 mc_offset
>= range
->start_mc_offset
&&
546 mc_offset
< range
->end_mc_offset
) {
547 *phys_addr
= range
->start_phys_addr
+
548 (mc_offset
- range
->start_mc_offset
);
556 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device
*mc_dev
,
557 struct fsl_mc_device
*mc_bus_dev
)
561 struct resource
*regions
;
562 struct fsl_mc_obj_desc
*obj_desc
= &mc_dev
->obj_desc
;
563 struct device
*parent_dev
= mc_dev
->dev
.parent
;
564 enum dprc_region_type mc_region_type
;
566 if (is_fsl_mc_bus_dprc(mc_dev
) ||
567 is_fsl_mc_bus_dpmcp(mc_dev
)) {
568 mc_region_type
= DPRC_REGION_TYPE_MC_PORTAL
;
569 } else if (is_fsl_mc_bus_dpio(mc_dev
)) {
570 mc_region_type
= DPRC_REGION_TYPE_QBMAN_PORTAL
;
573 * This function should not have been called for this MC object
574 * type, as this object type is not supposed to have MMIO
580 regions
= kmalloc_array(obj_desc
->region_count
,
581 sizeof(regions
[0]), GFP_KERNEL
);
585 for (i
= 0; i
< obj_desc
->region_count
; i
++) {
586 struct dprc_region_desc region_desc
;
588 error
= dprc_get_obj_region(mc_bus_dev
->mc_io
,
590 mc_bus_dev
->mc_handle
,
592 obj_desc
->id
, i
, ®ion_desc
);
595 "dprc_get_obj_region() failed: %d\n", error
);
596 goto error_cleanup_regions
;
599 * Older MC only returned region offset and no base address
600 * If base address is in the region_desc use it otherwise
601 * revert to old mechanism
603 if (region_desc
.base_address
)
604 regions
[i
].start
= region_desc
.base_address
+
605 region_desc
.base_offset
;
607 error
= translate_mc_addr(mc_dev
, mc_region_type
,
608 region_desc
.base_offset
,
613 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
614 region_desc
.base_offset
,
615 obj_desc
->type
, obj_desc
->id
, i
);
616 goto error_cleanup_regions
;
619 regions
[i
].end
= regions
[i
].start
+ region_desc
.size
- 1;
620 regions
[i
].name
= "fsl-mc object MMIO region";
621 regions
[i
].flags
= region_desc
.flags
& IORESOURCE_BITS
;
622 regions
[i
].flags
|= IORESOURCE_MEM
;
625 mc_dev
->regions
= regions
;
628 error_cleanup_regions
:
634 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
636 bool fsl_mc_is_root_dprc(struct device
*dev
)
638 struct device
*root_dprc_dev
;
640 fsl_mc_get_root_dprc(dev
, &root_dprc_dev
);
643 return dev
== root_dprc_dev
;
646 static void fsl_mc_device_release(struct device
*dev
)
648 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
650 kfree(mc_dev
->regions
);
652 if (is_fsl_mc_bus_dprc(mc_dev
))
653 kfree(to_fsl_mc_bus(mc_dev
));
659 * Add a newly discovered fsl-mc device to be visible in Linux
661 int fsl_mc_device_add(struct fsl_mc_obj_desc
*obj_desc
,
662 struct fsl_mc_io
*mc_io
,
663 struct device
*parent_dev
,
664 struct fsl_mc_device
**new_mc_dev
)
667 struct fsl_mc_device
*mc_dev
= NULL
;
668 struct fsl_mc_bus
*mc_bus
= NULL
;
669 struct fsl_mc_device
*parent_mc_dev
;
671 if (dev_is_fsl_mc(parent_dev
))
672 parent_mc_dev
= to_fsl_mc_device(parent_dev
);
674 parent_mc_dev
= NULL
;
676 if (strcmp(obj_desc
->type
, "dprc") == 0) {
678 * Allocate an MC bus device object:
680 mc_bus
= kzalloc(sizeof(*mc_bus
), GFP_KERNEL
);
684 mutex_init(&mc_bus
->scan_mutex
);
685 mc_dev
= &mc_bus
->mc_dev
;
688 * Allocate a regular fsl_mc_device object:
690 mc_dev
= kzalloc(sizeof(*mc_dev
), GFP_KERNEL
);
695 mc_dev
->obj_desc
= *obj_desc
;
696 mc_dev
->mc_io
= mc_io
;
697 device_initialize(&mc_dev
->dev
);
698 mc_dev
->dev
.parent
= parent_dev
;
699 mc_dev
->dev
.bus
= &fsl_mc_bus_type
;
700 mc_dev
->dev
.release
= fsl_mc_device_release
;
701 mc_dev
->dev
.type
= fsl_mc_get_device_type(obj_desc
->type
);
702 if (!mc_dev
->dev
.type
) {
704 dev_err(parent_dev
, "unknown device type %s\n", obj_desc
->type
);
705 goto error_cleanup_dev
;
707 dev_set_name(&mc_dev
->dev
, "%s.%d", obj_desc
->type
, obj_desc
->id
);
709 if (strcmp(obj_desc
->type
, "dprc") == 0) {
710 struct fsl_mc_io
*mc_io2
;
712 mc_dev
->flags
|= FSL_MC_IS_DPRC
;
715 * To get the DPRC's ICID, we need to open the DPRC
716 * in get_dprc_icid(). For child DPRCs, we do so using the
717 * parent DPRC's MC portal instead of the child DPRC's MC
718 * portal, in case the child DPRC is already opened with
719 * its own portal (e.g., the DPRC used by AIOP).
721 * NOTE: There cannot be more than one active open for a
722 * given MC object, using the same MC portal.
726 * device being added is a child DPRC device
728 mc_io2
= parent_mc_dev
->mc_io
;
731 * device being added is the root DPRC device
735 goto error_cleanup_dev
;
741 error
= get_dprc_icid(mc_io2
, obj_desc
->id
, &mc_dev
->icid
);
743 goto error_cleanup_dev
;
746 * A non-DPRC object has to be a child of a DPRC, use the
747 * parent's ICID and interrupt domain.
749 mc_dev
->icid
= parent_mc_dev
->icid
;
750 mc_dev
->dma_mask
= FSL_MC_DEFAULT_DMA_MASK
;
751 mc_dev
->dev
.dma_mask
= &mc_dev
->dma_mask
;
752 mc_dev
->dev
.coherent_dma_mask
= mc_dev
->dma_mask
;
753 dev_set_msi_domain(&mc_dev
->dev
,
754 dev_get_msi_domain(&parent_mc_dev
->dev
));
758 * Get MMIO regions for the device from the MC:
760 * NOTE: the root DPRC is a special case as its MMIO region is
761 * obtained from the device tree
763 if (parent_mc_dev
&& obj_desc
->region_count
!= 0) {
764 error
= fsl_mc_device_get_mmio_regions(mc_dev
,
767 goto error_cleanup_dev
;
771 * The device-specific probe callback will get invoked by device_add()
773 error
= device_add(&mc_dev
->dev
);
776 "device_add() failed for device %s: %d\n",
777 dev_name(&mc_dev
->dev
), error
);
778 goto error_cleanup_dev
;
781 dev_dbg(parent_dev
, "added %s\n", dev_name(&mc_dev
->dev
));
783 *new_mc_dev
= mc_dev
;
787 kfree(mc_dev
->regions
);
793 EXPORT_SYMBOL_GPL(fsl_mc_device_add
);
796 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
799 * @mc_dev: Pointer to an fsl-mc device
801 void fsl_mc_device_remove(struct fsl_mc_device
*mc_dev
)
803 kfree(mc_dev
->driver_override
);
804 mc_dev
->driver_override
= NULL
;
807 * The device-specific remove callback will get invoked by device_del()
809 device_del(&mc_dev
->dev
);
810 put_device(&mc_dev
->dev
);
812 EXPORT_SYMBOL_GPL(fsl_mc_device_remove
);
814 struct fsl_mc_device
*fsl_mc_get_endpoint(struct fsl_mc_device
*mc_dev
)
816 struct fsl_mc_device
*mc_bus_dev
, *endpoint
;
817 struct fsl_mc_obj_desc endpoint_desc
= {{ 0 }};
818 struct dprc_endpoint endpoint1
= {{ 0 }};
819 struct dprc_endpoint endpoint2
= {{ 0 }};
822 mc_bus_dev
= to_fsl_mc_device(mc_dev
->dev
.parent
);
823 strcpy(endpoint1
.type
, mc_dev
->obj_desc
.type
);
824 endpoint1
.id
= mc_dev
->obj_desc
.id
;
826 err
= dprc_get_connection(mc_bus_dev
->mc_io
, 0,
827 mc_bus_dev
->mc_handle
,
828 &endpoint1
, &endpoint2
,
831 if (err
== -ENOTCONN
|| state
== -1)
832 return ERR_PTR(-ENOTCONN
);
835 dev_err(&mc_bus_dev
->dev
, "dprc_get_connection() = %d\n", err
);
839 strcpy(endpoint_desc
.type
, endpoint2
.type
);
840 endpoint_desc
.id
= endpoint2
.id
;
841 endpoint
= fsl_mc_device_lookup(&endpoint_desc
, mc_bus_dev
);
845 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint
);
847 static int parse_mc_ranges(struct device
*dev
,
851 const __be32
**ranges_start
)
854 int range_tuple_cell_count
;
857 struct device_node
*mc_node
= dev
->of_node
;
859 *ranges_start
= of_get_property(mc_node
, "ranges", &ranges_len
);
860 if (!(*ranges_start
) || !ranges_len
) {
862 "missing or empty ranges property for device tree node '%pOFn'\n",
867 *paddr_cells
= of_n_addr_cells(mc_node
);
869 prop
= of_get_property(mc_node
, "#address-cells", NULL
);
871 *mc_addr_cells
= be32_to_cpup(prop
);
873 *mc_addr_cells
= *paddr_cells
;
875 prop
= of_get_property(mc_node
, "#size-cells", NULL
);
877 *mc_size_cells
= be32_to_cpup(prop
);
879 *mc_size_cells
= of_n_size_cells(mc_node
);
881 range_tuple_cell_count
= *paddr_cells
+ *mc_addr_cells
+
884 tuple_len
= range_tuple_cell_count
* sizeof(__be32
);
885 if (ranges_len
% tuple_len
!= 0) {
886 dev_err(dev
, "malformed ranges property '%pOFn'\n", mc_node
);
890 return ranges_len
/ tuple_len
;
893 static int get_mc_addr_translation_ranges(struct device
*dev
,
894 struct fsl_mc_addr_translation_range
903 const __be32
*ranges_start
;
906 ret
= parse_mc_ranges(dev
,
917 * Missing or empty ranges property ("ranges;") for the
918 * 'fsl,qoriq-mc' node. In this case, identity mapping
925 *ranges
= devm_kcalloc(dev
, *num_ranges
,
926 sizeof(struct fsl_mc_addr_translation_range
),
932 for (i
= 0; i
< *num_ranges
; ++i
) {
933 struct fsl_mc_addr_translation_range
*range
= &(*ranges
)[i
];
935 range
->mc_region_type
= of_read_number(cell
, 1);
936 range
->start_mc_offset
= of_read_number(cell
+ 1,
938 cell
+= mc_addr_cells
;
939 range
->start_phys_addr
= of_read_number(cell
, paddr_cells
);
941 range
->end_mc_offset
= range
->start_mc_offset
+
942 of_read_number(cell
, mc_size_cells
);
944 cell
+= mc_size_cells
;
951 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
954 static int fsl_mc_bus_probe(struct platform_device
*pdev
)
956 struct fsl_mc_obj_desc obj_desc
;
959 struct fsl_mc_device
*mc_bus_dev
= NULL
;
960 struct fsl_mc_io
*mc_io
= NULL
;
962 phys_addr_t mc_portal_phys_addr
;
963 u32 mc_portal_size
, mc_stream_id
;
964 struct resource
*plat_res
;
966 mc
= devm_kzalloc(&pdev
->dev
, sizeof(*mc
), GFP_KERNEL
);
970 platform_set_drvdata(pdev
, mc
);
972 plat_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
974 mc
->fsl_mc_regs
= devm_ioremap_resource(&pdev
->dev
, plat_res
);
975 if (IS_ERR(mc
->fsl_mc_regs
))
976 return PTR_ERR(mc
->fsl_mc_regs
);
979 if (mc
->fsl_mc_regs
) {
981 * Some bootloaders pause the MC firmware before booting the
982 * kernel so that MC will not cause faults as soon as the
983 * SMMU probes due to the fact that there's no configuration
985 * At this point MC should have all its SMMU setup done so make
986 * sure it is resumed.
988 writel(readl(mc
->fsl_mc_regs
+ FSL_MC_GCR1
) & (~GCR1_P1_STOP
),
989 mc
->fsl_mc_regs
+ FSL_MC_GCR1
);
991 if (IS_ENABLED(CONFIG_ACPI
) && !dev_of_node(&pdev
->dev
)) {
992 mc_stream_id
= readl(mc
->fsl_mc_regs
+ FSL_MC_FAPR
);
994 * HW ORs the PL and BMT bit, places the result in bit
995 * 14 of the StreamID and ORs in the ICID. Calculate it
998 mc_stream_id
= (mc_stream_id
& 0xffff) |
999 ((mc_stream_id
& (MC_FAPR_PL
| MC_FAPR_BMT
)) ?
1001 error
= acpi_dma_configure_id(&pdev
->dev
,
1005 dev_warn(&pdev
->dev
,
1006 "failed to configure dma: %d.\n",
1012 * Get physical address of MC portal for the root DPRC:
1014 plat_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1015 mc_portal_phys_addr
= plat_res
->start
;
1016 mc_portal_size
= resource_size(plat_res
);
1017 error
= fsl_create_mc_io(&pdev
->dev
, mc_portal_phys_addr
,
1018 mc_portal_size
, NULL
,
1019 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
, &mc_io
);
1023 error
= mc_get_version(mc_io
, 0, &mc_version
);
1026 "mc_get_version() failed with error %d\n", error
);
1027 goto error_cleanup_mc_io
;
1030 dev_info(&pdev
->dev
, "MC firmware version: %u.%u.%u\n",
1031 mc_version
.major
, mc_version
.minor
, mc_version
.revision
);
1033 if (dev_of_node(&pdev
->dev
)) {
1034 error
= get_mc_addr_translation_ranges(&pdev
->dev
,
1035 &mc
->translation_ranges
,
1036 &mc
->num_translation_ranges
);
1038 goto error_cleanup_mc_io
;
1041 error
= dprc_get_container_id(mc_io
, 0, &container_id
);
1044 "dprc_get_container_id() failed: %d\n", error
);
1045 goto error_cleanup_mc_io
;
1048 memset(&obj_desc
, 0, sizeof(struct fsl_mc_obj_desc
));
1049 error
= dprc_get_api_version(mc_io
, 0,
1050 &obj_desc
.ver_major
,
1051 &obj_desc
.ver_minor
);
1053 goto error_cleanup_mc_io
;
1055 obj_desc
.vendor
= FSL_MC_VENDOR_FREESCALE
;
1056 strcpy(obj_desc
.type
, "dprc");
1057 obj_desc
.id
= container_id
;
1058 obj_desc
.irq_count
= 1;
1059 obj_desc
.region_count
= 0;
1061 error
= fsl_mc_device_add(&obj_desc
, mc_io
, &pdev
->dev
, &mc_bus_dev
);
1063 goto error_cleanup_mc_io
;
1065 mc
->root_mc_bus_dev
= mc_bus_dev
;
1066 mc_bus_dev
->dev
.fwnode
= pdev
->dev
.fwnode
;
1069 error_cleanup_mc_io
:
1070 fsl_destroy_mc_io(mc_io
);
1075 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1078 static int fsl_mc_bus_remove(struct platform_device
*pdev
)
1080 struct fsl_mc
*mc
= platform_get_drvdata(pdev
);
1082 if (!fsl_mc_is_root_dprc(&mc
->root_mc_bus_dev
->dev
))
1085 fsl_mc_device_remove(mc
->root_mc_bus_dev
);
1087 fsl_destroy_mc_io(mc
->root_mc_bus_dev
->mc_io
);
1088 mc
->root_mc_bus_dev
->mc_io
= NULL
;
1093 static const struct of_device_id fsl_mc_bus_match_table
[] = {
1094 {.compatible
= "fsl,qoriq-mc",},
1098 MODULE_DEVICE_TABLE(of
, fsl_mc_bus_match_table
);
1100 static const struct acpi_device_id fsl_mc_bus_acpi_match_table
[] = {
1104 MODULE_DEVICE_TABLE(acpi
, fsl_mc_bus_acpi_match_table
);
1106 static struct platform_driver fsl_mc_bus_driver
= {
1108 .name
= "fsl_mc_bus",
1110 .of_match_table
= fsl_mc_bus_match_table
,
1111 .acpi_match_table
= fsl_mc_bus_acpi_match_table
,
1113 .probe
= fsl_mc_bus_probe
,
1114 .remove
= fsl_mc_bus_remove
,
1117 static int __init
fsl_mc_bus_driver_init(void)
1121 error
= bus_register(&fsl_mc_bus_type
);
1123 pr_err("bus type registration failed: %d\n", error
);
1124 goto error_cleanup_cache
;
1127 error
= platform_driver_register(&fsl_mc_bus_driver
);
1129 pr_err("platform_driver_register() failed: %d\n", error
);
1130 goto error_cleanup_bus
;
1133 error
= dprc_driver_init();
1135 goto error_cleanup_driver
;
1137 error
= fsl_mc_allocator_driver_init();
1139 goto error_cleanup_dprc_driver
;
1143 error_cleanup_dprc_driver
:
1146 error_cleanup_driver
:
1147 platform_driver_unregister(&fsl_mc_bus_driver
);
1150 bus_unregister(&fsl_mc_bus_type
);
1152 error_cleanup_cache
:
1155 postcore_initcall(fsl_mc_bus_driver_init
);