Merge tag 'usb-ci-v5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/peter...
[linux-2.6-microblaze.git] / drivers / bus / fsl-mc / fsl-mc-bus.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Freescale Management Complex (MC) bus driver
4  *
5  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6  * Author: German Rivera <German.Rivera@freescale.com>
7  *
8  */
9
10 #define pr_fmt(fmt) "fsl-mc: " fmt
11
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
22 #include "fsl-mc-private.h"
23
24 /**
25  * Default DMA mask for devices on a fsl-mc bus
26  */
27 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
28
29 static struct fsl_mc_version mc_version;
30
31 /**
32  * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
33  * @root_mc_bus_dev: fsl-mc device representing the root DPRC
34  * @num_translation_ranges: number of entries in addr_translation_ranges
35  * @translation_ranges: array of bus to system address translation ranges
36  */
37 struct fsl_mc {
38         struct fsl_mc_device *root_mc_bus_dev;
39         u8 num_translation_ranges;
40         struct fsl_mc_addr_translation_range *translation_ranges;
41 };
42
43 /**
44  * struct fsl_mc_addr_translation_range - bus to system address translation
45  * range
46  * @mc_region_type: Type of MC region for the range being translated
47  * @start_mc_offset: Start MC offset of the range being translated
48  * @end_mc_offset: MC offset of the first byte after the range (last MC
49  * offset of the range is end_mc_offset - 1)
50  * @start_phys_addr: system physical address corresponding to start_mc_addr
51  */
52 struct fsl_mc_addr_translation_range {
53         enum dprc_region_type mc_region_type;
54         u64 start_mc_offset;
55         u64 end_mc_offset;
56         phys_addr_t start_phys_addr;
57 };
58
59 /**
60  * fsl_mc_bus_match - device to driver matching callback
61  * @dev: the fsl-mc device to match against
62  * @drv: the device driver to search for matching fsl-mc object type
63  * structures
64  *
65  * Returns 1 on success, 0 otherwise.
66  */
67 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
68 {
69         const struct fsl_mc_device_id *id;
70         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
71         struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
72         bool found = false;
73
74         if (!mc_drv->match_id_table)
75                 goto out;
76
77         /*
78          * If the object is not 'plugged' don't match.
79          * Only exception is the root DPRC, which is a special case.
80          */
81         if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
82             !fsl_mc_is_root_dprc(&mc_dev->dev))
83                 goto out;
84
85         /*
86          * Traverse the match_id table of the given driver, trying to find
87          * a matching for the given device.
88          */
89         for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
90                 if (id->vendor == mc_dev->obj_desc.vendor &&
91                     strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
92                         found = true;
93
94                         break;
95                 }
96         }
97
98 out:
99         dev_dbg(dev, "%smatched\n", found ? "" : "not ");
100         return found;
101 }
102
103 /**
104  * fsl_mc_bus_uevent - callback invoked when a device is added
105  */
106 static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
107 {
108         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
109
110         if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
111                            mc_dev->obj_desc.vendor,
112                            mc_dev->obj_desc.type))
113                 return -ENOMEM;
114
115         return 0;
116 }
117
118 static int fsl_mc_dma_configure(struct device *dev)
119 {
120         struct device *dma_dev = dev;
121
122         while (dev_is_fsl_mc(dma_dev))
123                 dma_dev = dma_dev->parent;
124
125         return of_dma_configure(dev, dma_dev->of_node, 0);
126 }
127
128 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
129                              char *buf)
130 {
131         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
132
133         return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
134                        mc_dev->obj_desc.type);
135 }
136 static DEVICE_ATTR_RO(modalias);
137
138 static struct attribute *fsl_mc_dev_attrs[] = {
139         &dev_attr_modalias.attr,
140         NULL,
141 };
142
143 ATTRIBUTE_GROUPS(fsl_mc_dev);
144
145 struct bus_type fsl_mc_bus_type = {
146         .name = "fsl-mc",
147         .match = fsl_mc_bus_match,
148         .uevent = fsl_mc_bus_uevent,
149         .dma_configure  = fsl_mc_dma_configure,
150         .dev_groups = fsl_mc_dev_groups,
151 };
152 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
153
154 struct device_type fsl_mc_bus_dprc_type = {
155         .name = "fsl_mc_bus_dprc"
156 };
157 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
158
159 struct device_type fsl_mc_bus_dpni_type = {
160         .name = "fsl_mc_bus_dpni"
161 };
162 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
163
164 struct device_type fsl_mc_bus_dpio_type = {
165         .name = "fsl_mc_bus_dpio"
166 };
167 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
168
169 struct device_type fsl_mc_bus_dpsw_type = {
170         .name = "fsl_mc_bus_dpsw"
171 };
172 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
173
174 struct device_type fsl_mc_bus_dpbp_type = {
175         .name = "fsl_mc_bus_dpbp"
176 };
177 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
178
179 struct device_type fsl_mc_bus_dpcon_type = {
180         .name = "fsl_mc_bus_dpcon"
181 };
182 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
183
184 struct device_type fsl_mc_bus_dpmcp_type = {
185         .name = "fsl_mc_bus_dpmcp"
186 };
187 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
188
189 struct device_type fsl_mc_bus_dpmac_type = {
190         .name = "fsl_mc_bus_dpmac"
191 };
192 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
193
194 struct device_type fsl_mc_bus_dprtc_type = {
195         .name = "fsl_mc_bus_dprtc"
196 };
197 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
198
199 struct device_type fsl_mc_bus_dpseci_type = {
200         .name = "fsl_mc_bus_dpseci"
201 };
202 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
203
204 static struct device_type *fsl_mc_get_device_type(const char *type)
205 {
206         static const struct {
207                 struct device_type *dev_type;
208                 const char *type;
209         } dev_types[] = {
210                 { &fsl_mc_bus_dprc_type, "dprc" },
211                 { &fsl_mc_bus_dpni_type, "dpni" },
212                 { &fsl_mc_bus_dpio_type, "dpio" },
213                 { &fsl_mc_bus_dpsw_type, "dpsw" },
214                 { &fsl_mc_bus_dpbp_type, "dpbp" },
215                 { &fsl_mc_bus_dpcon_type, "dpcon" },
216                 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
217                 { &fsl_mc_bus_dpmac_type, "dpmac" },
218                 { &fsl_mc_bus_dprtc_type, "dprtc" },
219                 { &fsl_mc_bus_dpseci_type, "dpseci" },
220                 { NULL, NULL }
221         };
222         int i;
223
224         for (i = 0; dev_types[i].dev_type; i++)
225                 if (!strcmp(dev_types[i].type, type))
226                         return dev_types[i].dev_type;
227
228         return NULL;
229 }
230
231 static int fsl_mc_driver_probe(struct device *dev)
232 {
233         struct fsl_mc_driver *mc_drv;
234         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
235         int error;
236
237         mc_drv = to_fsl_mc_driver(dev->driver);
238
239         error = mc_drv->probe(mc_dev);
240         if (error < 0) {
241                 if (error != -EPROBE_DEFER)
242                         dev_err(dev, "%s failed: %d\n", __func__, error);
243                 return error;
244         }
245
246         return 0;
247 }
248
249 static int fsl_mc_driver_remove(struct device *dev)
250 {
251         struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
252         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
253         int error;
254
255         error = mc_drv->remove(mc_dev);
256         if (error < 0) {
257                 dev_err(dev, "%s failed: %d\n", __func__, error);
258                 return error;
259         }
260
261         return 0;
262 }
263
264 static void fsl_mc_driver_shutdown(struct device *dev)
265 {
266         struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
267         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
268
269         mc_drv->shutdown(mc_dev);
270 }
271
272 /**
273  * __fsl_mc_driver_register - registers a child device driver with the
274  * MC bus
275  *
276  * This function is implicitly invoked from the registration function of
277  * fsl_mc device drivers, which is generated by the
278  * module_fsl_mc_driver() macro.
279  */
280 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
281                              struct module *owner)
282 {
283         int error;
284
285         mc_driver->driver.owner = owner;
286         mc_driver->driver.bus = &fsl_mc_bus_type;
287
288         if (mc_driver->probe)
289                 mc_driver->driver.probe = fsl_mc_driver_probe;
290
291         if (mc_driver->remove)
292                 mc_driver->driver.remove = fsl_mc_driver_remove;
293
294         if (mc_driver->shutdown)
295                 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
296
297         error = driver_register(&mc_driver->driver);
298         if (error < 0) {
299                 pr_err("driver_register() failed for %s: %d\n",
300                        mc_driver->driver.name, error);
301                 return error;
302         }
303
304         return 0;
305 }
306 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
307
308 /**
309  * fsl_mc_driver_unregister - unregisters a device driver from the
310  * MC bus
311  */
312 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
313 {
314         driver_unregister(&mc_driver->driver);
315 }
316 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
317
318 /**
319  * mc_get_version() - Retrieves the Management Complex firmware
320  *                      version information
321  * @mc_io:              Pointer to opaque I/O object
322  * @cmd_flags:          Command flags; one or more of 'MC_CMD_FLAG_'
323  * @mc_ver_info:        Returned version information structure
324  *
325  * Return:      '0' on Success; Error code otherwise.
326  */
327 static int mc_get_version(struct fsl_mc_io *mc_io,
328                           u32 cmd_flags,
329                           struct fsl_mc_version *mc_ver_info)
330 {
331         struct fsl_mc_command cmd = { 0 };
332         struct dpmng_rsp_get_version *rsp_params;
333         int err;
334
335         /* prepare command */
336         cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
337                                           cmd_flags,
338                                           0);
339
340         /* send command to mc*/
341         err = mc_send_command(mc_io, &cmd);
342         if (err)
343                 return err;
344
345         /* retrieve response parameters */
346         rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
347         mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
348         mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
349         mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
350
351         return 0;
352 }
353
354 /**
355  * fsl_mc_get_version - function to retrieve the MC f/w version information
356  *
357  * Return:      mc version when called after fsl-mc-bus probe; NULL otherwise.
358  */
359 struct fsl_mc_version *fsl_mc_get_version(void)
360 {
361         if (mc_version.major)
362                 return &mc_version;
363
364         return NULL;
365 }
366 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
367
368 /**
369  * fsl_mc_get_root_dprc - function to traverse to the root dprc
370  */
371 static void fsl_mc_get_root_dprc(struct device *dev,
372                                  struct device **root_dprc_dev)
373 {
374         if (!dev) {
375                 *root_dprc_dev = NULL;
376         } else if (!dev_is_fsl_mc(dev)) {
377                 *root_dprc_dev = NULL;
378         } else {
379                 *root_dprc_dev = dev;
380                 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
381                         *root_dprc_dev = (*root_dprc_dev)->parent;
382         }
383 }
384
385 static int get_dprc_attr(struct fsl_mc_io *mc_io,
386                          int container_id, struct dprc_attributes *attr)
387 {
388         u16 dprc_handle;
389         int error;
390
391         error = dprc_open(mc_io, 0, container_id, &dprc_handle);
392         if (error < 0) {
393                 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
394                 return error;
395         }
396
397         memset(attr, 0, sizeof(struct dprc_attributes));
398         error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
399         if (error < 0) {
400                 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
401                         error);
402                 goto common_cleanup;
403         }
404
405         error = 0;
406
407 common_cleanup:
408         (void)dprc_close(mc_io, 0, dprc_handle);
409         return error;
410 }
411
412 static int get_dprc_icid(struct fsl_mc_io *mc_io,
413                          int container_id, u16 *icid)
414 {
415         struct dprc_attributes attr;
416         int error;
417
418         error = get_dprc_attr(mc_io, container_id, &attr);
419         if (error == 0)
420                 *icid = attr.icid;
421
422         return error;
423 }
424
425 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
426                              enum dprc_region_type mc_region_type,
427                              u64 mc_offset, phys_addr_t *phys_addr)
428 {
429         int i;
430         struct device *root_dprc_dev;
431         struct fsl_mc *mc;
432
433         fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
434         mc = dev_get_drvdata(root_dprc_dev->parent);
435
436         if (mc->num_translation_ranges == 0) {
437                 /*
438                  * Do identity mapping:
439                  */
440                 *phys_addr = mc_offset;
441                 return 0;
442         }
443
444         for (i = 0; i < mc->num_translation_ranges; i++) {
445                 struct fsl_mc_addr_translation_range *range =
446                         &mc->translation_ranges[i];
447
448                 if (mc_region_type == range->mc_region_type &&
449                     mc_offset >= range->start_mc_offset &&
450                     mc_offset < range->end_mc_offset) {
451                         *phys_addr = range->start_phys_addr +
452                                      (mc_offset - range->start_mc_offset);
453                         return 0;
454                 }
455         }
456
457         return -EFAULT;
458 }
459
460 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
461                                           struct fsl_mc_device *mc_bus_dev)
462 {
463         int i;
464         int error;
465         struct resource *regions;
466         struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
467         struct device *parent_dev = mc_dev->dev.parent;
468         enum dprc_region_type mc_region_type;
469
470         if (is_fsl_mc_bus_dprc(mc_dev) ||
471             is_fsl_mc_bus_dpmcp(mc_dev)) {
472                 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
473         } else if (is_fsl_mc_bus_dpio(mc_dev)) {
474                 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
475         } else {
476                 /*
477                  * This function should not have been called for this MC object
478                  * type, as this object type is not supposed to have MMIO
479                  * regions
480                  */
481                 return -EINVAL;
482         }
483
484         regions = kmalloc_array(obj_desc->region_count,
485                                 sizeof(regions[0]), GFP_KERNEL);
486         if (!regions)
487                 return -ENOMEM;
488
489         for (i = 0; i < obj_desc->region_count; i++) {
490                 struct dprc_region_desc region_desc;
491
492                 error = dprc_get_obj_region(mc_bus_dev->mc_io,
493                                             0,
494                                             mc_bus_dev->mc_handle,
495                                             obj_desc->type,
496                                             obj_desc->id, i, &region_desc);
497                 if (error < 0) {
498                         dev_err(parent_dev,
499                                 "dprc_get_obj_region() failed: %d\n", error);
500                         goto error_cleanup_regions;
501                 }
502                 /*
503                  * Older MC only returned region offset and no base address
504                  * If base address is in the region_desc use it otherwise
505                  * revert to old mechanism
506                  */
507                 if (region_desc.base_address)
508                         regions[i].start = region_desc.base_address +
509                                                 region_desc.base_offset;
510                 else
511                         error = translate_mc_addr(mc_dev, mc_region_type,
512                                           region_desc.base_offset,
513                                           &regions[i].start);
514
515                 if (error < 0) {
516                         dev_err(parent_dev,
517                                 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
518                                 region_desc.base_offset,
519                                 obj_desc->type, obj_desc->id, i);
520                         goto error_cleanup_regions;
521                 }
522
523                 regions[i].end = regions[i].start + region_desc.size - 1;
524                 regions[i].name = "fsl-mc object MMIO region";
525                 regions[i].flags = IORESOURCE_IO;
526                 if (region_desc.flags & DPRC_REGION_CACHEABLE)
527                         regions[i].flags |= IORESOURCE_CACHEABLE;
528                 if (region_desc.flags & DPRC_REGION_SHAREABLE)
529                         regions[i].flags |= IORESOURCE_MEM;
530         }
531
532         mc_dev->regions = regions;
533         return 0;
534
535 error_cleanup_regions:
536         kfree(regions);
537         return error;
538 }
539
540 /**
541  * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
542  */
543 bool fsl_mc_is_root_dprc(struct device *dev)
544 {
545         struct device *root_dprc_dev;
546
547         fsl_mc_get_root_dprc(dev, &root_dprc_dev);
548         if (!root_dprc_dev)
549                 return false;
550         return dev == root_dprc_dev;
551 }
552
553 static void fsl_mc_device_release(struct device *dev)
554 {
555         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
556
557         kfree(mc_dev->regions);
558
559         if (is_fsl_mc_bus_dprc(mc_dev))
560                 kfree(to_fsl_mc_bus(mc_dev));
561         else
562                 kfree(mc_dev);
563 }
564
565 /**
566  * Add a newly discovered fsl-mc device to be visible in Linux
567  */
568 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
569                       struct fsl_mc_io *mc_io,
570                       struct device *parent_dev,
571                       struct fsl_mc_device **new_mc_dev)
572 {
573         int error;
574         struct fsl_mc_device *mc_dev = NULL;
575         struct fsl_mc_bus *mc_bus = NULL;
576         struct fsl_mc_device *parent_mc_dev;
577
578         if (dev_is_fsl_mc(parent_dev))
579                 parent_mc_dev = to_fsl_mc_device(parent_dev);
580         else
581                 parent_mc_dev = NULL;
582
583         if (strcmp(obj_desc->type, "dprc") == 0) {
584                 /*
585                  * Allocate an MC bus device object:
586                  */
587                 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
588                 if (!mc_bus)
589                         return -ENOMEM;
590
591                 mc_dev = &mc_bus->mc_dev;
592         } else {
593                 /*
594                  * Allocate a regular fsl_mc_device object:
595                  */
596                 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
597                 if (!mc_dev)
598                         return -ENOMEM;
599         }
600
601         mc_dev->obj_desc = *obj_desc;
602         mc_dev->mc_io = mc_io;
603         device_initialize(&mc_dev->dev);
604         mc_dev->dev.parent = parent_dev;
605         mc_dev->dev.bus = &fsl_mc_bus_type;
606         mc_dev->dev.release = fsl_mc_device_release;
607         mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
608         if (!mc_dev->dev.type) {
609                 error = -ENODEV;
610                 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
611                 goto error_cleanup_dev;
612         }
613         dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
614
615         if (strcmp(obj_desc->type, "dprc") == 0) {
616                 struct fsl_mc_io *mc_io2;
617
618                 mc_dev->flags |= FSL_MC_IS_DPRC;
619
620                 /*
621                  * To get the DPRC's ICID, we need to open the DPRC
622                  * in get_dprc_icid(). For child DPRCs, we do so using the
623                  * parent DPRC's MC portal instead of the child DPRC's MC
624                  * portal, in case the child DPRC is already opened with
625                  * its own portal (e.g., the DPRC used by AIOP).
626                  *
627                  * NOTE: There cannot be more than one active open for a
628                  * given MC object, using the same MC portal.
629                  */
630                 if (parent_mc_dev) {
631                         /*
632                          * device being added is a child DPRC device
633                          */
634                         mc_io2 = parent_mc_dev->mc_io;
635                 } else {
636                         /*
637                          * device being added is the root DPRC device
638                          */
639                         if (!mc_io) {
640                                 error = -EINVAL;
641                                 goto error_cleanup_dev;
642                         }
643
644                         mc_io2 = mc_io;
645                 }
646
647                 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
648                 if (error < 0)
649                         goto error_cleanup_dev;
650         } else {
651                 /*
652                  * A non-DPRC object has to be a child of a DPRC, use the
653                  * parent's ICID and interrupt domain.
654                  */
655                 mc_dev->icid = parent_mc_dev->icid;
656                 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
657                 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
658                 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
659                 dev_set_msi_domain(&mc_dev->dev,
660                                    dev_get_msi_domain(&parent_mc_dev->dev));
661         }
662
663         /*
664          * Get MMIO regions for the device from the MC:
665          *
666          * NOTE: the root DPRC is a special case as its MMIO region is
667          * obtained from the device tree
668          */
669         if (parent_mc_dev && obj_desc->region_count != 0) {
670                 error = fsl_mc_device_get_mmio_regions(mc_dev,
671                                                        parent_mc_dev);
672                 if (error < 0)
673                         goto error_cleanup_dev;
674         }
675
676         /*
677          * The device-specific probe callback will get invoked by device_add()
678          */
679         error = device_add(&mc_dev->dev);
680         if (error < 0) {
681                 dev_err(parent_dev,
682                         "device_add() failed for device %s: %d\n",
683                         dev_name(&mc_dev->dev), error);
684                 goto error_cleanup_dev;
685         }
686
687         dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
688
689         *new_mc_dev = mc_dev;
690         return 0;
691
692 error_cleanup_dev:
693         kfree(mc_dev->regions);
694         kfree(mc_bus);
695         kfree(mc_dev);
696
697         return error;
698 }
699 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
700
701 /**
702  * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
703  * Linux
704  *
705  * @mc_dev: Pointer to an fsl-mc device
706  */
707 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
708 {
709         /*
710          * The device-specific remove callback will get invoked by device_del()
711          */
712         device_del(&mc_dev->dev);
713         put_device(&mc_dev->dev);
714 }
715 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
716
717 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
718 {
719         struct fsl_mc_device *mc_bus_dev, *endpoint;
720         struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
721         struct dprc_endpoint endpoint1 = {{ 0 }};
722         struct dprc_endpoint endpoint2 = {{ 0 }};
723         int state, err;
724
725         mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
726         strcpy(endpoint1.type, mc_dev->obj_desc.type);
727         endpoint1.id = mc_dev->obj_desc.id;
728
729         err = dprc_get_connection(mc_bus_dev->mc_io, 0,
730                                   mc_bus_dev->mc_handle,
731                                   &endpoint1, &endpoint2,
732                                   &state);
733
734         if (err == -ENOTCONN || state == -1)
735                 return ERR_PTR(-ENOTCONN);
736
737         if (err < 0) {
738                 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
739                 return ERR_PTR(err);
740         }
741
742         strcpy(endpoint_desc.type, endpoint2.type);
743         endpoint_desc.id = endpoint2.id;
744         endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
745
746         return endpoint;
747 }
748 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
749
750 static int parse_mc_ranges(struct device *dev,
751                            int *paddr_cells,
752                            int *mc_addr_cells,
753                            int *mc_size_cells,
754                            const __be32 **ranges_start)
755 {
756         const __be32 *prop;
757         int range_tuple_cell_count;
758         int ranges_len;
759         int tuple_len;
760         struct device_node *mc_node = dev->of_node;
761
762         *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
763         if (!(*ranges_start) || !ranges_len) {
764                 dev_warn(dev,
765                          "missing or empty ranges property for device tree node '%pOFn'\n",
766                          mc_node);
767                 return 0;
768         }
769
770         *paddr_cells = of_n_addr_cells(mc_node);
771
772         prop = of_get_property(mc_node, "#address-cells", NULL);
773         if (prop)
774                 *mc_addr_cells = be32_to_cpup(prop);
775         else
776                 *mc_addr_cells = *paddr_cells;
777
778         prop = of_get_property(mc_node, "#size-cells", NULL);
779         if (prop)
780                 *mc_size_cells = be32_to_cpup(prop);
781         else
782                 *mc_size_cells = of_n_size_cells(mc_node);
783
784         range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
785                                  *mc_size_cells;
786
787         tuple_len = range_tuple_cell_count * sizeof(__be32);
788         if (ranges_len % tuple_len != 0) {
789                 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
790                 return -EINVAL;
791         }
792
793         return ranges_len / tuple_len;
794 }
795
796 static int get_mc_addr_translation_ranges(struct device *dev,
797                                           struct fsl_mc_addr_translation_range
798                                                 **ranges,
799                                           u8 *num_ranges)
800 {
801         int ret;
802         int paddr_cells;
803         int mc_addr_cells;
804         int mc_size_cells;
805         int i;
806         const __be32 *ranges_start;
807         const __be32 *cell;
808
809         ret = parse_mc_ranges(dev,
810                               &paddr_cells,
811                               &mc_addr_cells,
812                               &mc_size_cells,
813                               &ranges_start);
814         if (ret < 0)
815                 return ret;
816
817         *num_ranges = ret;
818         if (!ret) {
819                 /*
820                  * Missing or empty ranges property ("ranges;") for the
821                  * 'fsl,qoriq-mc' node. In this case, identity mapping
822                  * will be used.
823                  */
824                 *ranges = NULL;
825                 return 0;
826         }
827
828         *ranges = devm_kcalloc(dev, *num_ranges,
829                                sizeof(struct fsl_mc_addr_translation_range),
830                                GFP_KERNEL);
831         if (!(*ranges))
832                 return -ENOMEM;
833
834         cell = ranges_start;
835         for (i = 0; i < *num_ranges; ++i) {
836                 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
837
838                 range->mc_region_type = of_read_number(cell, 1);
839                 range->start_mc_offset = of_read_number(cell + 1,
840                                                         mc_addr_cells - 1);
841                 cell += mc_addr_cells;
842                 range->start_phys_addr = of_read_number(cell, paddr_cells);
843                 cell += paddr_cells;
844                 range->end_mc_offset = range->start_mc_offset +
845                                      of_read_number(cell, mc_size_cells);
846
847                 cell += mc_size_cells;
848         }
849
850         return 0;
851 }
852
853 /**
854  * fsl_mc_bus_probe - callback invoked when the root MC bus is being
855  * added
856  */
857 static int fsl_mc_bus_probe(struct platform_device *pdev)
858 {
859         struct fsl_mc_obj_desc obj_desc;
860         int error;
861         struct fsl_mc *mc;
862         struct fsl_mc_device *mc_bus_dev = NULL;
863         struct fsl_mc_io *mc_io = NULL;
864         int container_id;
865         phys_addr_t mc_portal_phys_addr;
866         u32 mc_portal_size;
867         struct resource res;
868
869         mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
870         if (!mc)
871                 return -ENOMEM;
872
873         platform_set_drvdata(pdev, mc);
874
875         /*
876          * Get physical address of MC portal for the root DPRC:
877          */
878         error = of_address_to_resource(pdev->dev.of_node, 0, &res);
879         if (error < 0) {
880                 dev_err(&pdev->dev,
881                         "of_address_to_resource() failed for %pOF\n",
882                         pdev->dev.of_node);
883                 return error;
884         }
885
886         mc_portal_phys_addr = res.start;
887         mc_portal_size = resource_size(&res);
888         error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
889                                  mc_portal_size, NULL,
890                                  FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
891         if (error < 0)
892                 return error;
893
894         error = mc_get_version(mc_io, 0, &mc_version);
895         if (error != 0) {
896                 dev_err(&pdev->dev,
897                         "mc_get_version() failed with error %d\n", error);
898                 goto error_cleanup_mc_io;
899         }
900
901         dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
902                  mc_version.major, mc_version.minor, mc_version.revision);
903
904         error = get_mc_addr_translation_ranges(&pdev->dev,
905                                                &mc->translation_ranges,
906                                                &mc->num_translation_ranges);
907         if (error < 0)
908                 goto error_cleanup_mc_io;
909
910         error = dprc_get_container_id(mc_io, 0, &container_id);
911         if (error < 0) {
912                 dev_err(&pdev->dev,
913                         "dprc_get_container_id() failed: %d\n", error);
914                 goto error_cleanup_mc_io;
915         }
916
917         memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
918         error = dprc_get_api_version(mc_io, 0,
919                                      &obj_desc.ver_major,
920                                      &obj_desc.ver_minor);
921         if (error < 0)
922                 goto error_cleanup_mc_io;
923
924         obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
925         strcpy(obj_desc.type, "dprc");
926         obj_desc.id = container_id;
927         obj_desc.irq_count = 1;
928         obj_desc.region_count = 0;
929
930         error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
931         if (error < 0)
932                 goto error_cleanup_mc_io;
933
934         mc->root_mc_bus_dev = mc_bus_dev;
935         return 0;
936
937 error_cleanup_mc_io:
938         fsl_destroy_mc_io(mc_io);
939         return error;
940 }
941
942 /**
943  * fsl_mc_bus_remove - callback invoked when the root MC bus is being
944  * removed
945  */
946 static int fsl_mc_bus_remove(struct platform_device *pdev)
947 {
948         struct fsl_mc *mc = platform_get_drvdata(pdev);
949
950         if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
951                 return -EINVAL;
952
953         fsl_mc_device_remove(mc->root_mc_bus_dev);
954
955         fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
956         mc->root_mc_bus_dev->mc_io = NULL;
957
958         return 0;
959 }
960
961 static const struct of_device_id fsl_mc_bus_match_table[] = {
962         {.compatible = "fsl,qoriq-mc",},
963         {},
964 };
965
966 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
967
968 static struct platform_driver fsl_mc_bus_driver = {
969         .driver = {
970                    .name = "fsl_mc_bus",
971                    .pm = NULL,
972                    .of_match_table = fsl_mc_bus_match_table,
973                    },
974         .probe = fsl_mc_bus_probe,
975         .remove = fsl_mc_bus_remove,
976 };
977
978 static int __init fsl_mc_bus_driver_init(void)
979 {
980         int error;
981
982         error = bus_register(&fsl_mc_bus_type);
983         if (error < 0) {
984                 pr_err("bus type registration failed: %d\n", error);
985                 goto error_cleanup_cache;
986         }
987
988         error = platform_driver_register(&fsl_mc_bus_driver);
989         if (error < 0) {
990                 pr_err("platform_driver_register() failed: %d\n", error);
991                 goto error_cleanup_bus;
992         }
993
994         error = dprc_driver_init();
995         if (error < 0)
996                 goto error_cleanup_driver;
997
998         error = fsl_mc_allocator_driver_init();
999         if (error < 0)
1000                 goto error_cleanup_dprc_driver;
1001
1002         return 0;
1003
1004 error_cleanup_dprc_driver:
1005         dprc_driver_exit();
1006
1007 error_cleanup_driver:
1008         platform_driver_unregister(&fsl_mc_bus_driver);
1009
1010 error_cleanup_bus:
1011         bus_unregister(&fsl_mc_bus_type);
1012
1013 error_cleanup_cache:
1014         return error;
1015 }
1016 postcore_initcall(fsl_mc_bus_driver_init);