1
2
3
4
5
6
7
8
9
10
11#define pr_fmt(fmt) "fsl-mc: " fmt
12
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>
24
25#include "fsl-mc-private.h"
26
27
28
29
30#define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
31
32static struct fsl_mc_version mc_version;
33
34
35
36
37
38
39
40struct 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;
45};
46
47
48
49
50
51
52
53
54
55
56struct 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;
61};
62
63#define FSL_MC_FAPR 0x28
64#define MC_FAPR_PL BIT(18)
65#define MC_FAPR_BMT BIT(17)
66
67
68
69
70
71
72
73
74
75static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
76{
77 const struct fsl_mc_device_id *id;
78 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
79 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
80 bool found = false;
81
82
83 if (mc_dev->driver_override) {
84 found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
85 goto out;
86 }
87
88 if (!mc_drv->match_id_table)
89 goto out;
90
91
92
93
94
95 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
96 !fsl_mc_is_root_dprc(&mc_dev->dev))
97 goto out;
98
99
100
101
102
103 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
104 if (id->vendor == mc_dev->obj_desc.vendor &&
105 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
106 found = true;
107
108 break;
109 }
110 }
111
112out:
113 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
114 return found;
115}
116
117
118
119
120static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
121{
122 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
123
124 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
125 mc_dev->obj_desc.vendor,
126 mc_dev->obj_desc.type))
127 return -ENOMEM;
128
129 return 0;
130}
131
132static int fsl_mc_dma_configure(struct device *dev)
133{
134 struct device *dma_dev = dev;
135 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
136 u32 input_id = mc_dev->icid;
137
138 while (dev_is_fsl_mc(dma_dev))
139 dma_dev = dma_dev->parent;
140
141 if (dev_of_node(dma_dev))
142 return of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
143
144 return acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
145}
146
147static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
148 char *buf)
149{
150 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
151
152 return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
153 mc_dev->obj_desc.type);
154}
155static DEVICE_ATTR_RO(modalias);
156
157static ssize_t driver_override_store(struct device *dev,
158 struct device_attribute *attr,
159 const char *buf, size_t count)
160{
161 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
162 char *driver_override, *old = mc_dev->driver_override;
163 char *cp;
164
165 if (WARN_ON(dev->bus != &fsl_mc_bus_type))
166 return -EINVAL;
167
168 if (count >= (PAGE_SIZE - 1))
169 return -EINVAL;
170
171 driver_override = kstrndup(buf, count, GFP_KERNEL);
172 if (!driver_override)
173 return -ENOMEM;
174
175 cp = strchr(driver_override, '\n');
176 if (cp)
177 *cp = '\0';
178
179 if (strlen(driver_override)) {
180 mc_dev->driver_override = driver_override;
181 } else {
182 kfree(driver_override);
183 mc_dev->driver_override = NULL;
184 }
185
186 kfree(old);
187
188 return count;
189}
190
191static ssize_t driver_override_show(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
195
196 return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
197}
198static DEVICE_ATTR_RW(driver_override);
199
200static struct attribute *fsl_mc_dev_attrs[] = {
201 &dev_attr_modalias.attr,
202 &dev_attr_driver_override.attr,
203 NULL,
204};
205
206ATTRIBUTE_GROUPS(fsl_mc_dev);
207
208struct bus_type fsl_mc_bus_type = {
209 .name = "fsl-mc",
210 .match = fsl_mc_bus_match,
211 .uevent = fsl_mc_bus_uevent,
212 .dma_configure = fsl_mc_dma_configure,
213 .dev_groups = fsl_mc_dev_groups,
214};
215EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
216
217struct device_type fsl_mc_bus_dprc_type = {
218 .name = "fsl_mc_bus_dprc"
219};
220EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
221
222struct device_type fsl_mc_bus_dpni_type = {
223 .name = "fsl_mc_bus_dpni"
224};
225EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
226
227struct device_type fsl_mc_bus_dpio_type = {
228 .name = "fsl_mc_bus_dpio"
229};
230EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
231
232struct device_type fsl_mc_bus_dpsw_type = {
233 .name = "fsl_mc_bus_dpsw"
234};
235EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
236
237struct device_type fsl_mc_bus_dpbp_type = {
238 .name = "fsl_mc_bus_dpbp"
239};
240EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
241
242struct device_type fsl_mc_bus_dpcon_type = {
243 .name = "fsl_mc_bus_dpcon"
244};
245EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
246
247struct device_type fsl_mc_bus_dpmcp_type = {
248 .name = "fsl_mc_bus_dpmcp"
249};
250EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
251
252struct device_type fsl_mc_bus_dpmac_type = {
253 .name = "fsl_mc_bus_dpmac"
254};
255EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
256
257struct device_type fsl_mc_bus_dprtc_type = {
258 .name = "fsl_mc_bus_dprtc"
259};
260EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
261
262struct device_type fsl_mc_bus_dpseci_type = {
263 .name = "fsl_mc_bus_dpseci"
264};
265EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
266
267struct device_type fsl_mc_bus_dpdmux_type = {
268 .name = "fsl_mc_bus_dpdmux"
269};
270EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
271
272struct device_type fsl_mc_bus_dpdcei_type = {
273 .name = "fsl_mc_bus_dpdcei"
274};
275EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
276
277struct device_type fsl_mc_bus_dpaiop_type = {
278 .name = "fsl_mc_bus_dpaiop"
279};
280EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
281
282struct device_type fsl_mc_bus_dpci_type = {
283 .name = "fsl_mc_bus_dpci"
284};
285EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
286
287struct device_type fsl_mc_bus_dpdmai_type = {
288 .name = "fsl_mc_bus_dpdmai"
289};
290EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
291
292static struct device_type *fsl_mc_get_device_type(const char *type)
293{
294 static const struct {
295 struct device_type *dev_type;
296 const char *type;
297 } dev_types[] = {
298 { &fsl_mc_bus_dprc_type, "dprc" },
299 { &fsl_mc_bus_dpni_type, "dpni" },
300 { &fsl_mc_bus_dpio_type, "dpio" },
301 { &fsl_mc_bus_dpsw_type, "dpsw" },
302 { &fsl_mc_bus_dpbp_type, "dpbp" },
303 { &fsl_mc_bus_dpcon_type, "dpcon" },
304 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
305 { &fsl_mc_bus_dpmac_type, "dpmac" },
306 { &fsl_mc_bus_dprtc_type, "dprtc" },
307 { &fsl_mc_bus_dpseci_type, "dpseci" },
308 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
309 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
310 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
311 { &fsl_mc_bus_dpci_type, "dpci" },
312 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
313 { NULL, NULL }
314 };
315 int i;
316
317 for (i = 0; dev_types[i].dev_type; i++)
318 if (!strcmp(dev_types[i].type, type))
319 return dev_types[i].dev_type;
320
321 return NULL;
322}
323
324static int fsl_mc_driver_probe(struct device *dev)
325{
326 struct fsl_mc_driver *mc_drv;
327 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
328 int error;
329
330 mc_drv = to_fsl_mc_driver(dev->driver);
331
332 error = mc_drv->probe(mc_dev);
333 if (error < 0) {
334 if (error != -EPROBE_DEFER)
335 dev_err(dev, "%s failed: %d\n", __func__, error);
336 return error;
337 }
338
339 return 0;
340}
341
342static int fsl_mc_driver_remove(struct device *dev)
343{
344 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
345 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
346 int error;
347
348 error = mc_drv->remove(mc_dev);
349 if (error < 0) {
350 dev_err(dev, "%s failed: %d\n", __func__, error);
351 return error;
352 }
353
354 return 0;
355}
356
357static void fsl_mc_driver_shutdown(struct device *dev)
358{
359 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
360 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
361
362 mc_drv->shutdown(mc_dev);
363}
364
365
366
367
368
369
370
371
372
373int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
374 struct module *owner)
375{
376 int error;
377
378 mc_driver->driver.owner = owner;
379 mc_driver->driver.bus = &fsl_mc_bus_type;
380
381 if (mc_driver->probe)
382 mc_driver->driver.probe = fsl_mc_driver_probe;
383
384 if (mc_driver->remove)
385 mc_driver->driver.remove = fsl_mc_driver_remove;
386
387 if (mc_driver->shutdown)
388 mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
389
390 error = driver_register(&mc_driver->driver);
391 if (error < 0) {
392 pr_err("driver_register() failed for %s: %d\n",
393 mc_driver->driver.name, error);
394 return error;
395 }
396
397 return 0;
398}
399EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
400
401
402
403
404
405void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
406{
407 driver_unregister(&mc_driver->driver);
408}
409EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
410
411
412
413
414
415
416
417
418
419
420static int mc_get_version(struct fsl_mc_io *mc_io,
421 u32 cmd_flags,
422 struct fsl_mc_version *mc_ver_info)
423{
424 struct fsl_mc_command cmd = { 0 };
425 struct dpmng_rsp_get_version *rsp_params;
426 int err;
427
428
429 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
430 cmd_flags,
431 0);
432
433
434 err = mc_send_command(mc_io, &cmd);
435 if (err)
436 return err;
437
438
439 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
440 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
441 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
442 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
443
444 return 0;
445}
446
447
448
449
450
451
452struct fsl_mc_version *fsl_mc_get_version(void)
453{
454 if (mc_version.major)
455 return &mc_version;
456
457 return NULL;
458}
459EXPORT_SYMBOL_GPL(fsl_mc_get_version);
460
461
462
463
464void fsl_mc_get_root_dprc(struct device *dev,
465 struct device **root_dprc_dev)
466{
467 if (!dev) {
468 *root_dprc_dev = NULL;
469 } else if (!dev_is_fsl_mc(dev)) {
470 *root_dprc_dev = NULL;
471 } else {
472 *root_dprc_dev = dev;
473 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
474 *root_dprc_dev = (*root_dprc_dev)->parent;
475 }
476}
477
478static int get_dprc_attr(struct fsl_mc_io *mc_io,
479 int container_id, struct dprc_attributes *attr)
480{
481 u16 dprc_handle;
482 int error;
483
484 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
485 if (error < 0) {
486 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
487 return error;
488 }
489
490 memset(attr, 0, sizeof(struct dprc_attributes));
491 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
492 if (error < 0) {
493 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
494 error);
495 goto common_cleanup;
496 }
497
498 error = 0;
499
500common_cleanup:
501 (void)dprc_close(mc_io, 0, dprc_handle);
502 return error;
503}
504
505static int get_dprc_icid(struct fsl_mc_io *mc_io,
506 int container_id, u32 *icid)
507{
508 struct dprc_attributes attr;
509 int error;
510
511 error = get_dprc_attr(mc_io, container_id, &attr);
512 if (error == 0)
513 *icid = attr.icid;
514
515 return error;
516}
517
518static int translate_mc_addr(struct fsl_mc_device *mc_dev,
519 enum dprc_region_type mc_region_type,
520 u64 mc_offset, phys_addr_t *phys_addr)
521{
522 int i;
523 struct device *root_dprc_dev;
524 struct fsl_mc *mc;
525
526 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
527 mc = dev_get_drvdata(root_dprc_dev->parent);
528
529 if (mc->num_translation_ranges == 0) {
530
531
532
533 *phys_addr = mc_offset;
534 return 0;
535 }
536
537 for (i = 0; i < mc->num_translation_ranges; i++) {
538 struct fsl_mc_addr_translation_range *range =
539 &mc->translation_ranges[i];
540
541 if (mc_region_type == range->mc_region_type &&
542 mc_offset >= range->start_mc_offset &&
543 mc_offset < range->end_mc_offset) {
544 *phys_addr = range->start_phys_addr +
545 (mc_offset - range->start_mc_offset);
546 return 0;
547 }
548 }
549
550 return -EFAULT;
551}
552
553static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
554 struct fsl_mc_device *mc_bus_dev)
555{
556 int i;
557 int error;
558 struct resource *regions;
559 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
560 struct device *parent_dev = mc_dev->dev.parent;
561 enum dprc_region_type mc_region_type;
562
563 if (is_fsl_mc_bus_dprc(mc_dev) ||
564 is_fsl_mc_bus_dpmcp(mc_dev)) {
565 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
566 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
567 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
568 } else {
569
570
571
572
573
574 return -EINVAL;
575 }
576
577 regions = kmalloc_array(obj_desc->region_count,
578 sizeof(regions[0]), GFP_KERNEL);
579 if (!regions)
580 return -ENOMEM;
581
582 for (i = 0; i < obj_desc->region_count; i++) {
583 struct dprc_region_desc region_desc;
584
585 error = dprc_get_obj_region(mc_bus_dev->mc_io,
586 0,
587 mc_bus_dev->mc_handle,
588 obj_desc->type,
589 obj_desc->id, i, ®ion_desc);
590 if (error < 0) {
591 dev_err(parent_dev,
592 "dprc_get_obj_region() failed: %d\n", error);
593 goto error_cleanup_regions;
594 }
595
596
597
598
599
600 if (region_desc.base_address)
601 regions[i].start = region_desc.base_address +
602 region_desc.base_offset;
603 else
604 error = translate_mc_addr(mc_dev, mc_region_type,
605 region_desc.base_offset,
606 ®ions[i].start);
607
608 if (error < 0) {
609 dev_err(parent_dev,
610 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
611 region_desc.base_offset,
612 obj_desc->type, obj_desc->id, i);
613 goto error_cleanup_regions;
614 }
615
616 regions[i].end = regions[i].start + region_desc.size - 1;
617 regions[i].name = "fsl-mc object MMIO region";
618 regions[i].flags = region_desc.flags & IORESOURCE_BITS;
619 regions[i].flags |= IORESOURCE_MEM;
620 }
621
622 mc_dev->regions = regions;
623 return 0;
624
625error_cleanup_regions:
626 kfree(regions);
627 return error;
628}
629
630
631
632
633bool fsl_mc_is_root_dprc(struct device *dev)
634{
635 struct device *root_dprc_dev;
636
637 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
638 if (!root_dprc_dev)
639 return false;
640 return dev == root_dprc_dev;
641}
642
643static void fsl_mc_device_release(struct device *dev)
644{
645 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
646
647 kfree(mc_dev->regions);
648
649 if (is_fsl_mc_bus_dprc(mc_dev))
650 kfree(to_fsl_mc_bus(mc_dev));
651 else
652 kfree(mc_dev);
653}
654
655
656
657
658int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
659 struct fsl_mc_io *mc_io,
660 struct device *parent_dev,
661 struct fsl_mc_device **new_mc_dev)
662{
663 int error;
664 struct fsl_mc_device *mc_dev = NULL;
665 struct fsl_mc_bus *mc_bus = NULL;
666 struct fsl_mc_device *parent_mc_dev;
667
668 if (dev_is_fsl_mc(parent_dev))
669 parent_mc_dev = to_fsl_mc_device(parent_dev);
670 else
671 parent_mc_dev = NULL;
672
673 if (strcmp(obj_desc->type, "dprc") == 0) {
674
675
676
677 mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
678 if (!mc_bus)
679 return -ENOMEM;
680
681 mutex_init(&mc_bus->scan_mutex);
682 mc_dev = &mc_bus->mc_dev;
683 } else {
684
685
686
687 mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
688 if (!mc_dev)
689 return -ENOMEM;
690 }
691
692 mc_dev->obj_desc = *obj_desc;
693 mc_dev->mc_io = mc_io;
694 device_initialize(&mc_dev->dev);
695 mc_dev->dev.parent = parent_dev;
696 mc_dev->dev.bus = &fsl_mc_bus_type;
697 mc_dev->dev.release = fsl_mc_device_release;
698 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
699 if (!mc_dev->dev.type) {
700 error = -ENODEV;
701 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
702 goto error_cleanup_dev;
703 }
704 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
705
706 if (strcmp(obj_desc->type, "dprc") == 0) {
707 struct fsl_mc_io *mc_io2;
708
709 mc_dev->flags |= FSL_MC_IS_DPRC;
710
711
712
713
714
715
716
717
718
719
720
721 if (parent_mc_dev) {
722
723
724
725 mc_io2 = parent_mc_dev->mc_io;
726 } else {
727
728
729
730 if (!mc_io) {
731 error = -EINVAL;
732 goto error_cleanup_dev;
733 }
734
735 mc_io2 = mc_io;
736 }
737
738 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
739 if (error < 0)
740 goto error_cleanup_dev;
741 } else {
742
743
744
745
746 mc_dev->icid = parent_mc_dev->icid;
747 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
748 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
749 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
750 dev_set_msi_domain(&mc_dev->dev,
751 dev_get_msi_domain(&parent_mc_dev->dev));
752 }
753
754
755
756
757
758
759
760 if (parent_mc_dev && obj_desc->region_count != 0) {
761 error = fsl_mc_device_get_mmio_regions(mc_dev,
762 parent_mc_dev);
763 if (error < 0)
764 goto error_cleanup_dev;
765 }
766
767
768
769
770 error = device_add(&mc_dev->dev);
771 if (error < 0) {
772 dev_err(parent_dev,
773 "device_add() failed for device %s: %d\n",
774 dev_name(&mc_dev->dev), error);
775 goto error_cleanup_dev;
776 }
777
778 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
779
780 *new_mc_dev = mc_dev;
781 return 0;
782
783error_cleanup_dev:
784 kfree(mc_dev->regions);
785 kfree(mc_bus);
786 kfree(mc_dev);
787
788 return error;
789}
790EXPORT_SYMBOL_GPL(fsl_mc_device_add);
791
792
793
794
795
796
797
798void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
799{
800 kfree(mc_dev->driver_override);
801 mc_dev->driver_override = NULL;
802
803
804
805
806 device_del(&mc_dev->dev);
807 put_device(&mc_dev->dev);
808}
809EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
810
811struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
812{
813 struct fsl_mc_device *mc_bus_dev, *endpoint;
814 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
815 struct dprc_endpoint endpoint1 = {{ 0 }};
816 struct dprc_endpoint endpoint2 = {{ 0 }};
817 int state, err;
818
819 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
820 strcpy(endpoint1.type, mc_dev->obj_desc.type);
821 endpoint1.id = mc_dev->obj_desc.id;
822
823 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
824 mc_bus_dev->mc_handle,
825 &endpoint1, &endpoint2,
826 &state);
827
828 if (err == -ENOTCONN || state == -1)
829 return ERR_PTR(-ENOTCONN);
830
831 if (err < 0) {
832 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
833 return ERR_PTR(err);
834 }
835
836 strcpy(endpoint_desc.type, endpoint2.type);
837 endpoint_desc.id = endpoint2.id;
838 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
839
840 return endpoint;
841}
842EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
843
844static int parse_mc_ranges(struct device *dev,
845 int *paddr_cells,
846 int *mc_addr_cells,
847 int *mc_size_cells,
848 const __be32 **ranges_start)
849{
850 const __be32 *prop;
851 int range_tuple_cell_count;
852 int ranges_len;
853 int tuple_len;
854 struct device_node *mc_node = dev->of_node;
855
856 *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
857 if (!(*ranges_start) || !ranges_len) {
858 dev_warn(dev,
859 "missing or empty ranges property for device tree node '%pOFn'\n",
860 mc_node);
861 return 0;
862 }
863
864 *paddr_cells = of_n_addr_cells(mc_node);
865
866 prop = of_get_property(mc_node, "#address-cells", NULL);
867 if (prop)
868 *mc_addr_cells = be32_to_cpup(prop);
869 else
870 *mc_addr_cells = *paddr_cells;
871
872 prop = of_get_property(mc_node, "#size-cells", NULL);
873 if (prop)
874 *mc_size_cells = be32_to_cpup(prop);
875 else
876 *mc_size_cells = of_n_size_cells(mc_node);
877
878 range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
879 *mc_size_cells;
880
881 tuple_len = range_tuple_cell_count * sizeof(__be32);
882 if (ranges_len % tuple_len != 0) {
883 dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
884 return -EINVAL;
885 }
886
887 return ranges_len / tuple_len;
888}
889
890static int get_mc_addr_translation_ranges(struct device *dev,
891 struct fsl_mc_addr_translation_range
892 **ranges,
893 u8 *num_ranges)
894{
895 int ret;
896 int paddr_cells;
897 int mc_addr_cells;
898 int mc_size_cells;
899 int i;
900 const __be32 *ranges_start;
901 const __be32 *cell;
902
903 ret = parse_mc_ranges(dev,
904 &paddr_cells,
905 &mc_addr_cells,
906 &mc_size_cells,
907 &ranges_start);
908 if (ret < 0)
909 return ret;
910
911 *num_ranges = ret;
912 if (!ret) {
913
914
915
916
917
918 *ranges = NULL;
919 return 0;
920 }
921
922 *ranges = devm_kcalloc(dev, *num_ranges,
923 sizeof(struct fsl_mc_addr_translation_range),
924 GFP_KERNEL);
925 if (!(*ranges))
926 return -ENOMEM;
927
928 cell = ranges_start;
929 for (i = 0; i < *num_ranges; ++i) {
930 struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
931
932 range->mc_region_type = of_read_number(cell, 1);
933 range->start_mc_offset = of_read_number(cell + 1,
934 mc_addr_cells - 1);
935 cell += mc_addr_cells;
936 range->start_phys_addr = of_read_number(cell, paddr_cells);
937 cell += paddr_cells;
938 range->end_mc_offset = range->start_mc_offset +
939 of_read_number(cell, mc_size_cells);
940
941 cell += mc_size_cells;
942 }
943
944 return 0;
945}
946
947
948
949
950
951static int fsl_mc_bus_probe(struct platform_device *pdev)
952{
953 struct fsl_mc_obj_desc obj_desc;
954 int error;
955 struct fsl_mc *mc;
956 struct fsl_mc_device *mc_bus_dev = NULL;
957 struct fsl_mc_io *mc_io = NULL;
958 int container_id;
959 phys_addr_t mc_portal_phys_addr;
960 u32 mc_portal_size, mc_stream_id;
961 struct resource *plat_res;
962
963 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
964 if (!mc)
965 return -ENOMEM;
966
967 platform_set_drvdata(pdev, mc);
968
969 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
970 if (plat_res)
971 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
972
973 if (mc->fsl_mc_regs && IS_ENABLED(CONFIG_ACPI) &&
974 !dev_of_node(&pdev->dev)) {
975 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
976
977
978
979
980 mc_stream_id = (mc_stream_id & 0xffff) |
981 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
982 0x4000 : 0);
983 error = acpi_dma_configure_id(&pdev->dev, DEV_DMA_COHERENT,
984 &mc_stream_id);
985 if (error)
986 dev_warn(&pdev->dev, "failed to configure dma: %d.\n",
987 error);
988 }
989
990
991
992
993 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
994 mc_portal_phys_addr = plat_res->start;
995 mc_portal_size = resource_size(plat_res);
996 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
997 mc_portal_size, NULL,
998 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
999 if (error < 0)
1000 return error;
1001
1002 error = mc_get_version(mc_io, 0, &mc_version);
1003 if (error != 0) {
1004 dev_err(&pdev->dev,
1005 "mc_get_version() failed with error %d\n", error);
1006 goto error_cleanup_mc_io;
1007 }
1008
1009 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1010 mc_version.major, mc_version.minor, mc_version.revision);
1011
1012 if (dev_of_node(&pdev->dev)) {
1013 error = get_mc_addr_translation_ranges(&pdev->dev,
1014 &mc->translation_ranges,
1015 &mc->num_translation_ranges);
1016 if (error < 0)
1017 goto error_cleanup_mc_io;
1018 }
1019
1020 error = dprc_get_container_id(mc_io, 0, &container_id);
1021 if (error < 0) {
1022 dev_err(&pdev->dev,
1023 "dprc_get_container_id() failed: %d\n", error);
1024 goto error_cleanup_mc_io;
1025 }
1026
1027 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1028 error = dprc_get_api_version(mc_io, 0,
1029 &obj_desc.ver_major,
1030 &obj_desc.ver_minor);
1031 if (error < 0)
1032 goto error_cleanup_mc_io;
1033
1034 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1035 strcpy(obj_desc.type, "dprc");
1036 obj_desc.id = container_id;
1037 obj_desc.irq_count = 1;
1038 obj_desc.region_count = 0;
1039
1040 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1041 if (error < 0)
1042 goto error_cleanup_mc_io;
1043
1044 mc->root_mc_bus_dev = mc_bus_dev;
1045 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1046 return 0;
1047
1048error_cleanup_mc_io:
1049 fsl_destroy_mc_io(mc_io);
1050 return error;
1051}
1052
1053
1054
1055
1056
1057static int fsl_mc_bus_remove(struct platform_device *pdev)
1058{
1059 struct fsl_mc *mc = platform_get_drvdata(pdev);
1060
1061 if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1062 return -EINVAL;
1063
1064 fsl_mc_device_remove(mc->root_mc_bus_dev);
1065
1066 fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
1067 mc->root_mc_bus_dev->mc_io = NULL;
1068
1069 return 0;
1070}
1071
1072static const struct of_device_id fsl_mc_bus_match_table[] = {
1073 {.compatible = "fsl,qoriq-mc",},
1074 {},
1075};
1076
1077MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1078
1079static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1080 {"NXP0008", 0 },
1081 { }
1082};
1083MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1084
1085static struct platform_driver fsl_mc_bus_driver = {
1086 .driver = {
1087 .name = "fsl_mc_bus",
1088 .pm = NULL,
1089 .of_match_table = fsl_mc_bus_match_table,
1090 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1091 },
1092 .probe = fsl_mc_bus_probe,
1093 .remove = fsl_mc_bus_remove,
1094};
1095
1096static int __init fsl_mc_bus_driver_init(void)
1097{
1098 int error;
1099
1100 error = bus_register(&fsl_mc_bus_type);
1101 if (error < 0) {
1102 pr_err("bus type registration failed: %d\n", error);
1103 goto error_cleanup_cache;
1104 }
1105
1106 error = platform_driver_register(&fsl_mc_bus_driver);
1107 if (error < 0) {
1108 pr_err("platform_driver_register() failed: %d\n", error);
1109 goto error_cleanup_bus;
1110 }
1111
1112 error = dprc_driver_init();
1113 if (error < 0)
1114 goto error_cleanup_driver;
1115
1116 error = fsl_mc_allocator_driver_init();
1117 if (error < 0)
1118 goto error_cleanup_dprc_driver;
1119
1120 return 0;
1121
1122error_cleanup_dprc_driver:
1123 dprc_driver_exit();
1124
1125error_cleanup_driver:
1126 platform_driver_unregister(&fsl_mc_bus_driver);
1127
1128error_cleanup_bus:
1129 bus_unregister(&fsl_mc_bus_type);
1130
1131error_cleanup_cache:
1132 return error;
1133}
1134postcore_initcall(fsl_mc_bus_driver_init);
1135