Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / bus / fsl-mc / fsl-mc-bus.c
blobb8e6acdf932e631319111b07d9abc4f4ee51c930
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
9 */
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"
27 /**
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;
34 /**
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
40 struct fsl_mc {
41 struct fsl_mc_device *root_mc_bus_dev;
42 u8 num_translation_ranges;
43 struct fsl_mc_addr_translation_range *translation_ranges;
44 void *fsl_mc_regs;
47 /**
48 * struct fsl_mc_addr_translation_range - bus to system address translation
49 * range
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;
58 u64 start_mc_offset;
59 u64 end_mc_offset;
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)
70 /**
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
74 * structures
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);
83 bool found = false;
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);
88 goto out;
91 if (!mc_drv->match_id_table)
92 goto out;
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))
100 goto out;
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) {
109 found = true;
111 break;
115 out:
116 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
117 return found;
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))
130 return -ENOMEM;
132 return 0;
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,
151 char *buf)
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;
166 char *cp;
168 if (WARN_ON(dev->bus != &fsl_mc_bus_type))
169 return -EINVAL;
171 if (count >= (PAGE_SIZE - 1))
172 return -EINVAL;
174 driver_override = kstrndup(buf, count, GFP_KERNEL);
175 if (!driver_override)
176 return -ENOMEM;
178 cp = strchr(driver_override, '\n');
179 if (cp)
180 *cp = '\0';
182 if (strlen(driver_override)) {
183 mc_dev->driver_override = driver_override;
184 } else {
185 kfree(driver_override);
186 mc_dev->driver_override = NULL;
189 kfree(old);
191 return count;
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,
206 NULL,
209 ATTRIBUTE_GROUPS(fsl_mc_dev);
211 struct bus_type fsl_mc_bus_type = {
212 .name = "fsl-mc",
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;
299 const char *type;
300 } dev_types[] = {
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" },
316 { NULL, NULL }
318 int i;
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;
324 return NULL;
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);
331 int error;
333 mc_drv = to_fsl_mc_driver(dev->driver);
335 error = mc_drv->probe(mc_dev);
336 if (error < 0) {
337 if (error != -EPROBE_DEFER)
338 dev_err(dev, "%s failed: %d\n", __func__, error);
339 return error;
342 return 0;
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);
349 int error;
351 error = mc_drv->remove(mc_dev);
352 if (error < 0) {
353 dev_err(dev, "%s failed: %d\n", __func__, error);
354 return error;
357 return 0;
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
370 * MC bus
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)
379 int error;
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);
394 if (error < 0) {
395 pr_err("driver_register() failed for %s: %d\n",
396 mc_driver->driver.name, error);
397 return error;
400 return 0;
402 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
405 * fsl_mc_driver_unregister - unregisters a device driver from the
406 * MC bus
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,
424 u32 cmd_flags,
425 struct fsl_mc_version *mc_ver_info)
427 struct fsl_mc_command cmd = { 0 };
428 struct dpmng_rsp_get_version *rsp_params;
429 int err;
431 /* prepare command */
432 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
433 cmd_flags,
436 /* send command to mc*/
437 err = mc_send_command(mc_io, &cmd);
438 if (err)
439 return err;
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);
447 return 0;
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)
458 return &mc_version;
460 return NULL;
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)
470 if (!dev) {
471 *root_dprc_dev = NULL;
472 } else if (!dev_is_fsl_mc(dev)) {
473 *root_dprc_dev = NULL;
474 } else {
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)
484 u16 dprc_handle;
485 int error;
487 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
488 if (error < 0) {
489 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
490 return error;
493 memset(attr, 0, sizeof(struct dprc_attributes));
494 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
495 if (error < 0) {
496 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
497 error);
498 goto common_cleanup;
501 error = 0;
503 common_cleanup:
504 (void)dprc_close(mc_io, 0, dprc_handle);
505 return error;
508 static int get_dprc_icid(struct fsl_mc_io *mc_io,
509 int container_id, u32 *icid)
511 struct dprc_attributes attr;
512 int error;
514 error = get_dprc_attr(mc_io, container_id, &attr);
515 if (error == 0)
516 *icid = attr.icid;
518 return error;
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)
525 int i;
526 struct device *root_dprc_dev;
527 struct fsl_mc *mc;
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;
537 return 0;
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);
549 return 0;
553 return -EFAULT;
556 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
557 struct fsl_mc_device *mc_bus_dev)
559 int i;
560 int error;
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;
571 } else {
573 * This function should not have been called for this MC object
574 * type, as this object type is not supposed to have MMIO
575 * regions
577 return -EINVAL;
580 regions = kmalloc_array(obj_desc->region_count,
581 sizeof(regions[0]), GFP_KERNEL);
582 if (!regions)
583 return -ENOMEM;
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,
591 obj_desc->type,
592 obj_desc->id, i, &region_desc);
593 if (error < 0) {
594 dev_err(parent_dev,
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;
606 else
607 error = translate_mc_addr(mc_dev, mc_region_type,
608 region_desc.base_offset,
609 &regions[i].start);
611 if (error < 0) {
612 dev_err(parent_dev,
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;
626 return 0;
628 error_cleanup_regions:
629 kfree(regions);
630 return error;
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);
641 if (!root_dprc_dev)
642 return false;
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));
654 else
655 kfree(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)
666 int error;
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);
673 else
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);
681 if (!mc_bus)
682 return -ENOMEM;
684 mutex_init(&mc_bus->scan_mutex);
685 mc_dev = &mc_bus->mc_dev;
686 } else {
688 * Allocate a regular fsl_mc_device object:
690 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
691 if (!mc_dev)
692 return -ENOMEM;
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) {
703 error = -ENODEV;
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.
724 if (parent_mc_dev) {
726 * device being added is a child DPRC device
728 mc_io2 = parent_mc_dev->mc_io;
729 } else {
731 * device being added is the root DPRC device
733 if (!mc_io) {
734 error = -EINVAL;
735 goto error_cleanup_dev;
738 mc_io2 = mc_io;
741 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
742 if (error < 0)
743 goto error_cleanup_dev;
744 } else {
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,
765 parent_mc_dev);
766 if (error < 0)
767 goto error_cleanup_dev;
771 * The device-specific probe callback will get invoked by device_add()
773 error = device_add(&mc_dev->dev);
774 if (error < 0) {
775 dev_err(parent_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;
784 return 0;
786 error_cleanup_dev:
787 kfree(mc_dev->regions);
788 kfree(mc_bus);
789 kfree(mc_dev);
791 return error;
793 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
796 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
797 * Linux
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 }};
820 int state, err;
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,
829 &state);
831 if (err == -ENOTCONN || state == -1)
832 return ERR_PTR(-ENOTCONN);
834 if (err < 0) {
835 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
836 return ERR_PTR(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);
843 return endpoint;
845 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
847 static int parse_mc_ranges(struct device *dev,
848 int *paddr_cells,
849 int *mc_addr_cells,
850 int *mc_size_cells,
851 const __be32 **ranges_start)
853 const __be32 *prop;
854 int range_tuple_cell_count;
855 int ranges_len;
856 int tuple_len;
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) {
861 dev_warn(dev,
862 "missing or empty ranges property for device tree node '%pOFn'\n",
863 mc_node);
864 return 0;
867 *paddr_cells = of_n_addr_cells(mc_node);
869 prop = of_get_property(mc_node, "#address-cells", NULL);
870 if (prop)
871 *mc_addr_cells = be32_to_cpup(prop);
872 else
873 *mc_addr_cells = *paddr_cells;
875 prop = of_get_property(mc_node, "#size-cells", NULL);
876 if (prop)
877 *mc_size_cells = be32_to_cpup(prop);
878 else
879 *mc_size_cells = of_n_size_cells(mc_node);
881 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
882 *mc_size_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);
887 return -EINVAL;
890 return ranges_len / tuple_len;
893 static int get_mc_addr_translation_ranges(struct device *dev,
894 struct fsl_mc_addr_translation_range
895 **ranges,
896 u8 *num_ranges)
898 int ret;
899 int paddr_cells;
900 int mc_addr_cells;
901 int mc_size_cells;
902 int i;
903 const __be32 *ranges_start;
904 const __be32 *cell;
906 ret = parse_mc_ranges(dev,
907 &paddr_cells,
908 &mc_addr_cells,
909 &mc_size_cells,
910 &ranges_start);
911 if (ret < 0)
912 return ret;
914 *num_ranges = ret;
915 if (!ret) {
917 * Missing or empty ranges property ("ranges;") for the
918 * 'fsl,qoriq-mc' node. In this case, identity mapping
919 * will be used.
921 *ranges = NULL;
922 return 0;
925 *ranges = devm_kcalloc(dev, *num_ranges,
926 sizeof(struct fsl_mc_addr_translation_range),
927 GFP_KERNEL);
928 if (!(*ranges))
929 return -ENOMEM;
931 cell = ranges_start;
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,
937 mc_addr_cells - 1);
938 cell += mc_addr_cells;
939 range->start_phys_addr = of_read_number(cell, paddr_cells);
940 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;
947 return 0;
951 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
952 * added
954 static int fsl_mc_bus_probe(struct platform_device *pdev)
956 struct fsl_mc_obj_desc obj_desc;
957 int error;
958 struct fsl_mc *mc;
959 struct fsl_mc_device *mc_bus_dev = NULL;
960 struct fsl_mc_io *mc_io = NULL;
961 int container_id;
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);
967 if (!mc)
968 return -ENOMEM;
970 platform_set_drvdata(pdev, mc);
972 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
973 if (plat_res) {
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
984 * in place for MC.
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
996 * accordingly.
998 mc_stream_id = (mc_stream_id & 0xffff) |
999 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1000 BIT(14) : 0);
1001 error = acpi_dma_configure_id(&pdev->dev,
1002 DEV_DMA_COHERENT,
1003 &mc_stream_id);
1004 if (error)
1005 dev_warn(&pdev->dev,
1006 "failed to configure dma: %d.\n",
1007 error);
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);
1020 if (error < 0)
1021 return error;
1023 error = mc_get_version(mc_io, 0, &mc_version);
1024 if (error != 0) {
1025 dev_err(&pdev->dev,
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);
1037 if (error < 0)
1038 goto error_cleanup_mc_io;
1041 error = dprc_get_container_id(mc_io, 0, &container_id);
1042 if (error < 0) {
1043 dev_err(&pdev->dev,
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);
1052 if (error < 0)
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);
1062 if (error < 0)
1063 goto error_cleanup_mc_io;
1065 mc->root_mc_bus_dev = mc_bus_dev;
1066 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1067 return 0;
1069 error_cleanup_mc_io:
1070 fsl_destroy_mc_io(mc_io);
1071 return error;
1075 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1076 * removed
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))
1083 return -EINVAL;
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;
1090 return 0;
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[] = {
1101 {"NXP0008", 0 },
1104 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1106 static struct platform_driver fsl_mc_bus_driver = {
1107 .driver = {
1108 .name = "fsl_mc_bus",
1109 .pm = NULL,
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)
1119 int error;
1121 error = bus_register(&fsl_mc_bus_type);
1122 if (error < 0) {
1123 pr_err("bus type registration failed: %d\n", error);
1124 goto error_cleanup_cache;
1127 error = platform_driver_register(&fsl_mc_bus_driver);
1128 if (error < 0) {
1129 pr_err("platform_driver_register() failed: %d\n", error);
1130 goto error_cleanup_bus;
1133 error = dprc_driver_init();
1134 if (error < 0)
1135 goto error_cleanup_driver;
1137 error = fsl_mc_allocator_driver_init();
1138 if (error < 0)
1139 goto error_cleanup_dprc_driver;
1141 return 0;
1143 error_cleanup_dprc_driver:
1144 dprc_driver_exit();
1146 error_cleanup_driver:
1147 platform_driver_unregister(&fsl_mc_bus_driver);
1149 error_cleanup_bus:
1150 bus_unregister(&fsl_mc_bus_type);
1152 error_cleanup_cache:
1153 return error;
1155 postcore_initcall(fsl_mc_bus_driver_init);