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 * Copyright 2019-2020 NXP
7 * Author: German Rivera <German.Rivera@freescale.com>
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/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/limits.h>
20 #include <linux/bitops.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/acpi.h>
23 #include <linux/iommu.h>
24 #include <linux/dma-map-ops.h>
25
26 #include "fsl-mc-private.h"
27
28 /*
29 * Default DMA mask for devices on a fsl-mc bus
30 */
31 #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
32
33 static struct fsl_mc_version mc_version;
34
35 /**
36 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
37 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
38 * @num_translation_ranges: number of entries in addr_translation_ranges
39 * @translation_ranges: array of bus to system address translation ranges
40 * @fsl_mc_regs: base address of register bank
41 */
42 struct fsl_mc {
43 struct fsl_mc_device *root_mc_bus_dev;
44 u8 num_translation_ranges;
45 struct fsl_mc_addr_translation_range *translation_ranges;
46 void __iomem *fsl_mc_regs;
47 };
48
49 /**
50 * struct fsl_mc_addr_translation_range - bus to system address translation
51 * range
52 * @mc_region_type: Type of MC region for the range being translated
53 * @start_mc_offset: Start MC offset of the range being translated
54 * @end_mc_offset: MC offset of the first byte after the range (last MC
55 * offset of the range is end_mc_offset - 1)
56 * @start_phys_addr: system physical address corresponding to start_mc_addr
57 */
58 struct fsl_mc_addr_translation_range {
59 enum dprc_region_type mc_region_type;
60 u64 start_mc_offset;
61 u64 end_mc_offset;
62 phys_addr_t start_phys_addr;
63 };
64
65 #define FSL_MC_GCR1 0x0
66 #define GCR1_P1_STOP BIT(31)
67 #define GCR1_P2_STOP BIT(30)
68
69 #define FSL_MC_FAPR 0x28
70 #define MC_FAPR_PL BIT(18)
71 #define MC_FAPR_BMT BIT(17)
72
73 static phys_addr_t mc_portal_base_phys_addr;
74
75 /**
76 * fsl_mc_bus_match - device to driver matching callback
77 * @dev: the fsl-mc device to match against
78 * @drv: the device driver to search for matching fsl-mc object type
79 * structures
80 *
81 * Returns 1 on success, 0 otherwise.
82 */
fsl_mc_bus_match(struct device * dev,const struct device_driver * drv)83 static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
84 {
85 const struct fsl_mc_device_id *id;
86 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
87 const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
88 bool found = false;
89 int ret;
90
91 /* When driver_override is set, only bind to the matching driver */
92 ret = device_match_driver_override(dev, drv);
93 if (ret > 0) {
94 found = true;
95 goto out;
96 }
97 if (ret == 0)
98 goto out;
99
100 if (!mc_drv->match_id_table)
101 goto out;
102
103 /*
104 * If the object is not 'plugged' don't match.
105 * Only exception is the root DPRC, which is a special case.
106 */
107 if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
108 !fsl_mc_is_root_dprc(&mc_dev->dev))
109 goto out;
110
111 /*
112 * Traverse the match_id table of the given driver, trying to find
113 * a matching for the given device.
114 */
115 for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
116 if (id->vendor == mc_dev->obj_desc.vendor &&
117 strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
118 found = true;
119
120 break;
121 }
122 }
123
124 out:
125 dev_dbg(dev, "%smatched\n", found ? "" : "not ");
126 return found;
127 }
128
129 /*
130 * fsl_mc_bus_uevent - callback invoked when a device is added
131 */
fsl_mc_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)132 static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
133 {
134 const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
135
136 if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
137 mc_dev->obj_desc.vendor,
138 mc_dev->obj_desc.type))
139 return -ENOMEM;
140
141 return 0;
142 }
143
fsl_mc_probe(struct device * dev)144 static int fsl_mc_probe(struct device *dev)
145 {
146 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
147 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
148
149 if (mc_drv->probe)
150 return mc_drv->probe(mc_dev);
151
152 return 0;
153 }
154
fsl_mc_remove(struct device * dev)155 static void fsl_mc_remove(struct device *dev)
156 {
157 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
158 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
159
160 if (mc_drv->remove)
161 mc_drv->remove(mc_dev);
162 }
163
fsl_mc_shutdown(struct device * dev)164 static void fsl_mc_shutdown(struct device *dev)
165 {
166 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
167 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
168
169 if (dev->driver && mc_drv->shutdown)
170 mc_drv->shutdown(mc_dev);
171 }
172
fsl_mc_dma_configure(struct device * dev)173 static int fsl_mc_dma_configure(struct device *dev)
174 {
175 const struct device_driver *drv = READ_ONCE(dev->driver);
176 struct device *dma_dev = dev;
177 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
178 u32 input_id = mc_dev->icid;
179 int ret;
180
181 while (dev_is_fsl_mc(dma_dev))
182 dma_dev = dma_dev->parent;
183
184 if (dev_of_node(dma_dev))
185 ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
186 else
187 ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
188
189 /* @drv may not be valid when we're called from the IOMMU layer */
190 if (!ret && drv && !to_fsl_mc_driver(drv)->driver_managed_dma) {
191 ret = iommu_device_use_default_domain(dev);
192 if (ret)
193 arch_teardown_dma_ops(dev);
194 }
195
196 return ret;
197 }
198
fsl_mc_dma_cleanup(struct device * dev)199 static void fsl_mc_dma_cleanup(struct device *dev)
200 {
201 struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
202
203 if (!mc_drv->driver_managed_dma)
204 iommu_device_unuse_default_domain(dev);
205 }
206
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)207 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
208 char *buf)
209 {
210 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
211
212 return sysfs_emit(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
213 mc_dev->obj_desc.type);
214 }
215 static DEVICE_ATTR_RO(modalias);
216
217 static struct attribute *fsl_mc_dev_attrs[] = {
218 &dev_attr_modalias.attr,
219 NULL,
220 };
221
222 ATTRIBUTE_GROUPS(fsl_mc_dev);
223
scan_fsl_mc_bus(struct device * dev,void * data)224 static int scan_fsl_mc_bus(struct device *dev, void *data)
225 {
226 struct fsl_mc_device *root_mc_dev;
227 struct fsl_mc_bus *root_mc_bus;
228
229 if (!fsl_mc_is_root_dprc(dev))
230 goto exit;
231
232 root_mc_dev = to_fsl_mc_device(dev);
233 root_mc_bus = to_fsl_mc_bus(root_mc_dev);
234 mutex_lock(&root_mc_bus->scan_mutex);
235 dprc_scan_objects(root_mc_dev, false);
236 mutex_unlock(&root_mc_bus->scan_mutex);
237
238 exit:
239 return 0;
240 }
241
rescan_store(const struct bus_type * bus,const char * buf,size_t count)242 static ssize_t rescan_store(const struct bus_type *bus,
243 const char *buf, size_t count)
244 {
245 unsigned long val;
246
247 if (kstrtoul(buf, 0, &val) < 0)
248 return -EINVAL;
249
250 if (val)
251 bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
252
253 return count;
254 }
255 static BUS_ATTR_WO(rescan);
256
fsl_mc_bus_set_autorescan(struct device * dev,void * data)257 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
258 {
259 struct fsl_mc_device *root_mc_dev;
260 unsigned long val;
261 char *buf = data;
262
263 if (!fsl_mc_is_root_dprc(dev))
264 goto exit;
265
266 root_mc_dev = to_fsl_mc_device(dev);
267
268 if (kstrtoul(buf, 0, &val) < 0)
269 return -EINVAL;
270
271 if (val)
272 enable_dprc_irq(root_mc_dev);
273 else
274 disable_dprc_irq(root_mc_dev);
275
276 exit:
277 return 0;
278 }
279
fsl_mc_bus_get_autorescan(struct device * dev,void * data)280 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
281 {
282 struct fsl_mc_device *root_mc_dev;
283 char *buf = data;
284
285 if (!fsl_mc_is_root_dprc(dev))
286 goto exit;
287
288 root_mc_dev = to_fsl_mc_device(dev);
289
290 sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
291 exit:
292 return 0;
293 }
294
autorescan_store(const struct bus_type * bus,const char * buf,size_t count)295 static ssize_t autorescan_store(const struct bus_type *bus,
296 const char *buf, size_t count)
297 {
298 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
299
300 return count;
301 }
302
autorescan_show(const struct bus_type * bus,char * buf)303 static ssize_t autorescan_show(const struct bus_type *bus, char *buf)
304 {
305 bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
306 return strlen(buf);
307 }
308
309 static BUS_ATTR_RW(autorescan);
310
311 static struct attribute *fsl_mc_bus_attrs[] = {
312 &bus_attr_rescan.attr,
313 &bus_attr_autorescan.attr,
314 NULL,
315 };
316
317 ATTRIBUTE_GROUPS(fsl_mc_bus);
318
319 const struct bus_type fsl_mc_bus_type = {
320 .name = "fsl-mc",
321 .driver_override = true,
322 .match = fsl_mc_bus_match,
323 .uevent = fsl_mc_bus_uevent,
324 .probe = fsl_mc_probe,
325 .remove = fsl_mc_remove,
326 .shutdown = fsl_mc_shutdown,
327 .dma_configure = fsl_mc_dma_configure,
328 .dma_cleanup = fsl_mc_dma_cleanup,
329 .dev_groups = fsl_mc_dev_groups,
330 .bus_groups = fsl_mc_bus_groups,
331 };
332 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
333
334 const struct device_type fsl_mc_bus_dprc_type = {
335 .name = "fsl_mc_bus_dprc"
336 };
337 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
338
339 const struct device_type fsl_mc_bus_dpni_type = {
340 .name = "fsl_mc_bus_dpni"
341 };
342 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
343
344 const struct device_type fsl_mc_bus_dpio_type = {
345 .name = "fsl_mc_bus_dpio"
346 };
347 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
348
349 const struct device_type fsl_mc_bus_dpsw_type = {
350 .name = "fsl_mc_bus_dpsw"
351 };
352 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
353
354 const struct device_type fsl_mc_bus_dpbp_type = {
355 .name = "fsl_mc_bus_dpbp"
356 };
357 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
358
359 const struct device_type fsl_mc_bus_dpcon_type = {
360 .name = "fsl_mc_bus_dpcon"
361 };
362 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
363
364 const struct device_type fsl_mc_bus_dpmcp_type = {
365 .name = "fsl_mc_bus_dpmcp"
366 };
367 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
368
369 const struct device_type fsl_mc_bus_dpmac_type = {
370 .name = "fsl_mc_bus_dpmac"
371 };
372 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
373
374 const struct device_type fsl_mc_bus_dprtc_type = {
375 .name = "fsl_mc_bus_dprtc"
376 };
377 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
378
379 const struct device_type fsl_mc_bus_dpseci_type = {
380 .name = "fsl_mc_bus_dpseci"
381 };
382 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
383
384 const struct device_type fsl_mc_bus_dpdmux_type = {
385 .name = "fsl_mc_bus_dpdmux"
386 };
387 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
388
389 const struct device_type fsl_mc_bus_dpdcei_type = {
390 .name = "fsl_mc_bus_dpdcei"
391 };
392 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
393
394 const struct device_type fsl_mc_bus_dpaiop_type = {
395 .name = "fsl_mc_bus_dpaiop"
396 };
397 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
398
399 const struct device_type fsl_mc_bus_dpci_type = {
400 .name = "fsl_mc_bus_dpci"
401 };
402 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
403
404 const struct device_type fsl_mc_bus_dpdmai_type = {
405 .name = "fsl_mc_bus_dpdmai"
406 };
407 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
408
409 static const struct device_type fsl_mc_bus_dpdbg_type = {
410 .name = "fsl_mc_bus_dpdbg"
411 };
412
fsl_mc_get_device_type(const char * type)413 static const struct device_type *fsl_mc_get_device_type(const char *type)
414 {
415 static const struct {
416 const struct device_type *dev_type;
417 const char *type;
418 } dev_types[] = {
419 { &fsl_mc_bus_dprc_type, "dprc" },
420 { &fsl_mc_bus_dpni_type, "dpni" },
421 { &fsl_mc_bus_dpio_type, "dpio" },
422 { &fsl_mc_bus_dpsw_type, "dpsw" },
423 { &fsl_mc_bus_dpbp_type, "dpbp" },
424 { &fsl_mc_bus_dpcon_type, "dpcon" },
425 { &fsl_mc_bus_dpmcp_type, "dpmcp" },
426 { &fsl_mc_bus_dpmac_type, "dpmac" },
427 { &fsl_mc_bus_dprtc_type, "dprtc" },
428 { &fsl_mc_bus_dpseci_type, "dpseci" },
429 { &fsl_mc_bus_dpdmux_type, "dpdmux" },
430 { &fsl_mc_bus_dpdcei_type, "dpdcei" },
431 { &fsl_mc_bus_dpaiop_type, "dpaiop" },
432 { &fsl_mc_bus_dpci_type, "dpci" },
433 { &fsl_mc_bus_dpdmai_type, "dpdmai" },
434 { &fsl_mc_bus_dpdbg_type, "dpdbg" },
435 { NULL, NULL }
436 };
437 int i;
438
439 for (i = 0; dev_types[i].dev_type; i++)
440 if (!strcmp(dev_types[i].type, type))
441 return dev_types[i].dev_type;
442
443 return NULL;
444 }
445
446 /*
447 * __fsl_mc_driver_register - registers a child device driver with the
448 * MC bus
449 *
450 * This function is implicitly invoked from the registration function of
451 * fsl_mc device drivers, which is generated by the
452 * module_fsl_mc_driver() macro.
453 */
__fsl_mc_driver_register(struct fsl_mc_driver * mc_driver,struct module * owner)454 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
455 struct module *owner)
456 {
457 int error;
458
459 mc_driver->driver.owner = owner;
460 mc_driver->driver.bus = &fsl_mc_bus_type;
461
462 error = driver_register(&mc_driver->driver);
463 if (error < 0) {
464 pr_err("driver_register() failed for %s: %d\n",
465 mc_driver->driver.name, error);
466 return error;
467 }
468
469 return 0;
470 }
471 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
472
473 /*
474 * fsl_mc_driver_unregister - unregisters a device driver from the
475 * MC bus
476 */
fsl_mc_driver_unregister(struct fsl_mc_driver * mc_driver)477 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
478 {
479 driver_unregister(&mc_driver->driver);
480 }
481 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
482
483 /**
484 * mc_get_version() - Retrieves the Management Complex firmware
485 * version information
486 * @mc_io: Pointer to opaque I/O object
487 * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
488 * @mc_ver_info: Returned version information structure
489 *
490 * Return: '0' on Success; Error code otherwise.
491 */
mc_get_version(struct fsl_mc_io * mc_io,u32 cmd_flags,struct fsl_mc_version * mc_ver_info)492 static int mc_get_version(struct fsl_mc_io *mc_io,
493 u32 cmd_flags,
494 struct fsl_mc_version *mc_ver_info)
495 {
496 struct fsl_mc_command cmd = { 0 };
497 struct dpmng_rsp_get_version *rsp_params;
498 int err;
499
500 /* prepare command */
501 cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
502 cmd_flags,
503 0);
504
505 /* send command to mc*/
506 err = mc_send_command(mc_io, &cmd);
507 if (err)
508 return err;
509
510 /* retrieve response parameters */
511 rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
512 mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
513 mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
514 mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
515
516 return 0;
517 }
518
519 /**
520 * fsl_mc_get_version - function to retrieve the MC f/w version information
521 *
522 * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
523 */
fsl_mc_get_version(void)524 struct fsl_mc_version *fsl_mc_get_version(void)
525 {
526 if (mc_version.major)
527 return &mc_version;
528
529 return NULL;
530 }
531 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
532
533 /*
534 * fsl_mc_get_root_dprc - function to traverse to the root dprc
535 */
fsl_mc_get_root_dprc(struct device * dev,struct device ** root_dprc_dev)536 void fsl_mc_get_root_dprc(struct device *dev,
537 struct device **root_dprc_dev)
538 {
539 if (!dev) {
540 *root_dprc_dev = NULL;
541 } else if (!dev_is_fsl_mc(dev)) {
542 *root_dprc_dev = NULL;
543 } else {
544 *root_dprc_dev = dev;
545 while (dev_is_fsl_mc((*root_dprc_dev)->parent))
546 *root_dprc_dev = (*root_dprc_dev)->parent;
547 }
548 }
549
get_dprc_attr(struct fsl_mc_io * mc_io,int container_id,struct dprc_attributes * attr)550 static int get_dprc_attr(struct fsl_mc_io *mc_io,
551 int container_id, struct dprc_attributes *attr)
552 {
553 u16 dprc_handle;
554 int error;
555
556 error = dprc_open(mc_io, 0, container_id, &dprc_handle);
557 if (error < 0) {
558 dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
559 return error;
560 }
561
562 memset(attr, 0, sizeof(struct dprc_attributes));
563 error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
564 if (error < 0) {
565 dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
566 error);
567 goto common_cleanup;
568 }
569
570 error = 0;
571
572 common_cleanup:
573 (void)dprc_close(mc_io, 0, dprc_handle);
574 return error;
575 }
576
get_dprc_icid(struct fsl_mc_io * mc_io,int container_id,u32 * icid)577 static int get_dprc_icid(struct fsl_mc_io *mc_io,
578 int container_id, u32 *icid)
579 {
580 struct dprc_attributes attr;
581 int error;
582
583 error = get_dprc_attr(mc_io, container_id, &attr);
584 if (error == 0)
585 *icid = attr.icid;
586
587 return error;
588 }
589
translate_mc_addr(struct fsl_mc_device * mc_dev,enum dprc_region_type mc_region_type,u64 mc_offset,phys_addr_t * phys_addr)590 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
591 enum dprc_region_type mc_region_type,
592 u64 mc_offset, phys_addr_t *phys_addr)
593 {
594 int i;
595 struct device *root_dprc_dev;
596 struct fsl_mc *mc;
597
598 fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
599 mc = dev_get_drvdata(root_dprc_dev->parent);
600
601 if (mc->num_translation_ranges == 0) {
602 /*
603 * Do identity mapping:
604 */
605 *phys_addr = mc_offset;
606 return 0;
607 }
608
609 for (i = 0; i < mc->num_translation_ranges; i++) {
610 struct fsl_mc_addr_translation_range *range =
611 &mc->translation_ranges[i];
612
613 if (mc_region_type == range->mc_region_type &&
614 mc_offset >= range->start_mc_offset &&
615 mc_offset < range->end_mc_offset) {
616 *phys_addr = range->start_phys_addr +
617 (mc_offset - range->start_mc_offset);
618 return 0;
619 }
620 }
621
622 return -EFAULT;
623 }
624
fsl_mc_device_get_mmio_regions(struct fsl_mc_device * mc_dev,struct fsl_mc_device * mc_bus_dev)625 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
626 struct fsl_mc_device *mc_bus_dev)
627 {
628 int i;
629 int error;
630 struct resource *regions;
631 struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
632 struct device *parent_dev = mc_dev->dev.parent;
633 enum dprc_region_type mc_region_type;
634
635 if (is_fsl_mc_bus_dprc(mc_dev) ||
636 is_fsl_mc_bus_dpmcp(mc_dev)) {
637 mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
638 } else if (is_fsl_mc_bus_dpio(mc_dev)) {
639 mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
640 } else {
641 /*
642 * This function should not have been called for this MC object
643 * type, as this object type is not supposed to have MMIO
644 * regions
645 */
646 return -EINVAL;
647 }
648
649 regions = kmalloc_objs(regions[0], obj_desc->region_count);
650 if (!regions)
651 return -ENOMEM;
652
653 for (i = 0; i < obj_desc->region_count; i++) {
654 struct dprc_region_desc region_desc;
655
656 error = dprc_get_obj_region(mc_bus_dev->mc_io,
657 0,
658 mc_bus_dev->mc_handle,
659 obj_desc->type,
660 obj_desc->id, i, ®ion_desc);
661 if (error < 0) {
662 dev_err(parent_dev,
663 "dprc_get_obj_region() failed: %d\n", error);
664 goto error_cleanup_regions;
665 }
666 /*
667 * Older MC only returned region offset and no base address
668 * If base address is in the region_desc use it otherwise
669 * revert to old mechanism
670 */
671 if (region_desc.base_address) {
672 regions[i].start = region_desc.base_address +
673 region_desc.base_offset;
674 } else {
675 error = translate_mc_addr(mc_dev, mc_region_type,
676 region_desc.base_offset,
677 ®ions[i].start);
678
679 /*
680 * Some versions of the MC firmware wrongly report
681 * 0 for register base address of the DPMCP associated
682 * with child DPRC objects thus rendering them unusable.
683 * This is particularly troublesome in ACPI boot
684 * scenarios where the legacy way of extracting this
685 * base address from the device tree does not apply.
686 * Given that DPMCPs share the same base address,
687 * workaround this by using the base address extracted
688 * from the root DPRC container.
689 */
690 if (is_fsl_mc_bus_dprc(mc_dev) &&
691 regions[i].start == region_desc.base_offset)
692 regions[i].start += mc_portal_base_phys_addr;
693 }
694
695 if (error < 0) {
696 dev_err(parent_dev,
697 "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
698 region_desc.base_offset,
699 obj_desc->type, obj_desc->id, i);
700 goto error_cleanup_regions;
701 }
702
703 regions[i].end = regions[i].start + region_desc.size - 1;
704 regions[i].name = "fsl-mc object MMIO region";
705 regions[i].flags = region_desc.flags & IORESOURCE_BITS;
706 regions[i].flags |= IORESOURCE_MEM;
707 }
708
709 mc_dev->regions = regions;
710 return 0;
711
712 error_cleanup_regions:
713 kfree(regions);
714 return error;
715 }
716
717 /*
718 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
719 */
fsl_mc_is_root_dprc(struct device * dev)720 bool fsl_mc_is_root_dprc(struct device *dev)
721 {
722 struct device *root_dprc_dev;
723
724 fsl_mc_get_root_dprc(dev, &root_dprc_dev);
725 if (!root_dprc_dev)
726 return false;
727 return dev == root_dprc_dev;
728 }
729
fsl_mc_device_release(struct device * dev)730 static void fsl_mc_device_release(struct device *dev)
731 {
732 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
733
734 kfree(mc_dev->regions);
735
736 if (is_fsl_mc_bus_dprc(mc_dev))
737 kfree(to_fsl_mc_bus(mc_dev));
738 else
739 kfree(mc_dev);
740 }
741
742 /*
743 * Add a newly discovered fsl-mc device to be visible in Linux
744 */
fsl_mc_device_add(struct fsl_mc_obj_desc * obj_desc,struct fsl_mc_io * mc_io,struct device * parent_dev,struct fsl_mc_device ** new_mc_dev)745 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
746 struct fsl_mc_io *mc_io,
747 struct device *parent_dev,
748 struct fsl_mc_device **new_mc_dev)
749 {
750 int error;
751 struct fsl_mc_device *mc_dev = NULL;
752 struct fsl_mc_bus *mc_bus = NULL;
753 struct fsl_mc_device *parent_mc_dev;
754
755 if (dev_is_fsl_mc(parent_dev))
756 parent_mc_dev = to_fsl_mc_device(parent_dev);
757 else
758 parent_mc_dev = NULL;
759
760 if (strcmp(obj_desc->type, "dprc") == 0) {
761 /*
762 * Allocate an MC bus device object:
763 */
764 mc_bus = kzalloc_obj(*mc_bus);
765 if (!mc_bus)
766 return -ENOMEM;
767
768 mutex_init(&mc_bus->scan_mutex);
769 mc_dev = &mc_bus->mc_dev;
770 } else {
771 /*
772 * Allocate a regular fsl_mc_device object:
773 */
774 mc_dev = kzalloc_obj(*mc_dev);
775 if (!mc_dev)
776 return -ENOMEM;
777 }
778
779 mc_dev->obj_desc = *obj_desc;
780 mc_dev->mc_io = mc_io;
781 device_initialize(&mc_dev->dev);
782 mc_dev->dev.parent = parent_dev;
783 mc_dev->dev.bus = &fsl_mc_bus_type;
784 mc_dev->dev.release = fsl_mc_device_release;
785 mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
786 if (!mc_dev->dev.type) {
787 error = -ENODEV;
788 dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
789 goto error_cleanup_dev;
790 }
791 dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
792
793 if (strcmp(obj_desc->type, "dprc") == 0) {
794 struct fsl_mc_io *mc_io2;
795
796 mc_dev->flags |= FSL_MC_IS_DPRC;
797
798 /*
799 * To get the DPRC's ICID, we need to open the DPRC
800 * in get_dprc_icid(). For child DPRCs, we do so using the
801 * parent DPRC's MC portal instead of the child DPRC's MC
802 * portal, in case the child DPRC is already opened with
803 * its own portal (e.g., the DPRC used by AIOP).
804 *
805 * NOTE: There cannot be more than one active open for a
806 * given MC object, using the same MC portal.
807 */
808 if (parent_mc_dev) {
809 /*
810 * device being added is a child DPRC device
811 */
812 mc_io2 = parent_mc_dev->mc_io;
813 } else {
814 /*
815 * device being added is the root DPRC device
816 */
817 if (!mc_io) {
818 error = -EINVAL;
819 goto error_cleanup_dev;
820 }
821
822 mc_io2 = mc_io;
823 }
824
825 error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
826 if (error < 0)
827 goto error_cleanup_dev;
828 } else {
829 /*
830 * A non-DPRC object has to be a child of a DPRC, use the
831 * parent's ICID and interrupt domain.
832 */
833 mc_dev->icid = parent_mc_dev->icid;
834 mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
835 mc_dev->dev.dma_mask = &mc_dev->dma_mask;
836 mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
837 dev_set_msi_domain(&mc_dev->dev,
838 dev_get_msi_domain(&parent_mc_dev->dev));
839 }
840
841 /*
842 * Get MMIO regions for the device from the MC:
843 *
844 * NOTE: the root DPRC is a special case as its MMIO region is
845 * obtained from the device tree
846 */
847 if (parent_mc_dev && obj_desc->region_count != 0) {
848 error = fsl_mc_device_get_mmio_regions(mc_dev,
849 parent_mc_dev);
850 if (error < 0)
851 goto error_cleanup_dev;
852 }
853
854 /*
855 * The device-specific probe callback will get invoked by device_add()
856 */
857 error = device_add(&mc_dev->dev);
858 if (error < 0) {
859 dev_err(parent_dev,
860 "device_add() failed for device %s: %d\n",
861 dev_name(&mc_dev->dev), error);
862 goto error_cleanup_dev;
863 }
864
865 dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
866
867 *new_mc_dev = mc_dev;
868 return 0;
869
870 error_cleanup_dev:
871 put_device(&mc_dev->dev);
872
873 return error;
874 }
875 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
876
877 static struct notifier_block fsl_mc_nb;
878
879 /**
880 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
881 * Linux
882 *
883 * @mc_dev: Pointer to an fsl-mc device
884 */
fsl_mc_device_remove(struct fsl_mc_device * mc_dev)885 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
886 {
887 /*
888 * The device-specific remove callback will get invoked by device_del()
889 */
890 device_del(&mc_dev->dev);
891 put_device(&mc_dev->dev);
892 }
893 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
894
fsl_mc_get_endpoint(struct fsl_mc_device * mc_dev,u16 if_id)895 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
896 u16 if_id)
897 {
898 struct fsl_mc_device *mc_bus_dev, *endpoint;
899 struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
900 struct dprc_endpoint endpoint1 = {{ 0 }};
901 struct dprc_endpoint endpoint2 = {{ 0 }};
902 struct fsl_mc_bus *mc_bus;
903 int state, err;
904
905 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
906 strcpy(endpoint1.type, mc_dev->obj_desc.type);
907 endpoint1.id = mc_dev->obj_desc.id;
908 endpoint1.if_id = if_id;
909
910 err = dprc_get_connection(mc_bus_dev->mc_io, 0,
911 mc_bus_dev->mc_handle,
912 &endpoint1, &endpoint2,
913 &state);
914
915 if (err == -ENOTCONN || state == -1)
916 return ERR_PTR(-ENOTCONN);
917
918 if (err < 0) {
919 dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
920 return ERR_PTR(err);
921 }
922
923 strcpy(endpoint_desc.type, endpoint2.type);
924 endpoint_desc.id = endpoint2.id;
925 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
926 if (endpoint)
927 return endpoint;
928
929 /*
930 * We know that the device has an endpoint because we verified by
931 * interrogating the firmware. This is the case when the device was not
932 * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
933 * Force a rescan of the devices in this container and retry the lookup.
934 */
935 mc_bus = to_fsl_mc_bus(mc_bus_dev);
936 if (mutex_trylock(&mc_bus->scan_mutex)) {
937 err = dprc_scan_objects(mc_bus_dev, true);
938 mutex_unlock(&mc_bus->scan_mutex);
939 }
940 if (err < 0)
941 return ERR_PTR(err);
942
943 endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
944 /*
945 * This means that the endpoint might reside in a different isolation
946 * context (DPRC/container). Not much to do, so return a permssion
947 * error.
948 */
949 if (!endpoint)
950 return ERR_PTR(-EPERM);
951
952 return endpoint;
953 }
954 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
955
get_mc_addr_translation_ranges(struct device * dev,struct fsl_mc_addr_translation_range ** ranges,u8 * num_ranges)956 static int get_mc_addr_translation_ranges(struct device *dev,
957 struct fsl_mc_addr_translation_range
958 **ranges,
959 u8 *num_ranges)
960 {
961 struct fsl_mc_addr_translation_range *r;
962 struct of_range_parser parser;
963 struct of_range range;
964
965 of_range_parser_init(&parser, dev->of_node);
966 *num_ranges = of_range_count(&parser);
967 if (!*num_ranges) {
968 /*
969 * Missing or empty ranges property ("ranges;") for the
970 * 'fsl,qoriq-mc' node. In this case, identity mapping
971 * will be used.
972 */
973 *ranges = NULL;
974 return 0;
975 }
976
977 *ranges = devm_kcalloc(dev, *num_ranges,
978 sizeof(struct fsl_mc_addr_translation_range),
979 GFP_KERNEL);
980 if (!(*ranges))
981 return -ENOMEM;
982
983 r = *ranges;
984 for_each_of_range(&parser, &range) {
985 r->mc_region_type = range.flags;
986 r->start_mc_offset = range.bus_addr;
987 r->end_mc_offset = range.bus_addr + range.size;
988 r->start_phys_addr = range.cpu_addr;
989 r++;
990 }
991
992 return 0;
993 }
994
995 /*
996 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
997 * added
998 */
fsl_mc_bus_probe(struct platform_device * pdev)999 static int fsl_mc_bus_probe(struct platform_device *pdev)
1000 {
1001 struct fsl_mc_obj_desc obj_desc;
1002 int error;
1003 struct fsl_mc *mc;
1004 struct fsl_mc_device *mc_bus_dev = NULL;
1005 struct fsl_mc_io *mc_io = NULL;
1006 int container_id;
1007 phys_addr_t mc_portal_phys_addr;
1008 u32 mc_portal_size, mc_stream_id;
1009 struct resource *plat_res;
1010
1011 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1012 if (!mc)
1013 return -ENOMEM;
1014
1015 platform_set_drvdata(pdev, mc);
1016
1017 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1018 if (plat_res) {
1019 mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1020 if (IS_ERR(mc->fsl_mc_regs))
1021 return PTR_ERR(mc->fsl_mc_regs);
1022 }
1023
1024 if (mc->fsl_mc_regs) {
1025 if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1026 mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1027 /*
1028 * HW ORs the PL and BMT bit, places the result in bit
1029 * 14 of the StreamID and ORs in the ICID. Calculate it
1030 * accordingly.
1031 */
1032 mc_stream_id = (mc_stream_id & 0xffff) |
1033 ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1034 BIT(14) : 0);
1035 error = acpi_dma_configure_id(&pdev->dev,
1036 DEV_DMA_COHERENT,
1037 &mc_stream_id);
1038 if (error == -EPROBE_DEFER)
1039 return error;
1040 if (error)
1041 dev_warn(&pdev->dev,
1042 "failed to configure dma: %d.\n",
1043 error);
1044 }
1045
1046 /*
1047 * Some bootloaders pause the MC firmware before booting the
1048 * kernel so that MC will not cause faults as soon as the
1049 * SMMU probes due to the fact that there's no configuration
1050 * in place for MC.
1051 * At this point MC should have all its SMMU setup done so make
1052 * sure it is resumed.
1053 */
1054 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
1055 (~(GCR1_P1_STOP | GCR1_P2_STOP)),
1056 mc->fsl_mc_regs + FSL_MC_GCR1);
1057 }
1058
1059 /*
1060 * Get physical address of MC portal for the root DPRC:
1061 */
1062 plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1063 if (!plat_res)
1064 return -EINVAL;
1065
1066 mc_portal_phys_addr = plat_res->start;
1067 mc_portal_size = resource_size(plat_res);
1068 mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
1069
1070 error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1071 mc_portal_size, NULL,
1072 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1073 if (error < 0)
1074 return error;
1075
1076 error = mc_get_version(mc_io, 0, &mc_version);
1077 if (error != 0) {
1078 dev_err(&pdev->dev,
1079 "mc_get_version() failed with error %d\n", error);
1080 goto error_cleanup_mc_io;
1081 }
1082
1083 dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1084 mc_version.major, mc_version.minor, mc_version.revision);
1085
1086 if (dev_of_node(&pdev->dev)) {
1087 error = get_mc_addr_translation_ranges(&pdev->dev,
1088 &mc->translation_ranges,
1089 &mc->num_translation_ranges);
1090 if (error < 0)
1091 goto error_cleanup_mc_io;
1092 }
1093
1094 error = dprc_get_container_id(mc_io, 0, &container_id);
1095 if (error < 0) {
1096 dev_err(&pdev->dev,
1097 "dprc_get_container_id() failed: %d\n", error);
1098 goto error_cleanup_mc_io;
1099 }
1100
1101 memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1102 error = dprc_get_api_version(mc_io, 0,
1103 &obj_desc.ver_major,
1104 &obj_desc.ver_minor);
1105 if (error < 0)
1106 goto error_cleanup_mc_io;
1107
1108 obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1109 strcpy(obj_desc.type, "dprc");
1110 obj_desc.id = container_id;
1111 obj_desc.irq_count = 1;
1112 obj_desc.region_count = 0;
1113
1114 error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1115 if (error < 0)
1116 goto error_cleanup_mc_io;
1117
1118 mc->root_mc_bus_dev = mc_bus_dev;
1119 mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1120 return 0;
1121
1122 error_cleanup_mc_io:
1123 fsl_destroy_mc_io(mc_io);
1124 return error;
1125 }
1126
1127 /*
1128 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1129 * removed
1130 */
fsl_mc_bus_remove(struct platform_device * pdev)1131 static void fsl_mc_bus_remove(struct platform_device *pdev)
1132 {
1133 struct fsl_mc *mc = platform_get_drvdata(pdev);
1134 struct fsl_mc_io *mc_io;
1135
1136 mc_io = mc->root_mc_bus_dev->mc_io;
1137 fsl_mc_device_remove(mc->root_mc_bus_dev);
1138 fsl_destroy_mc_io(mc_io);
1139
1140 bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
1141
1142 if (mc->fsl_mc_regs) {
1143 /*
1144 * Pause the MC firmware so that it doesn't crash in certain
1145 * scenarios, such as kexec.
1146 */
1147 writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
1148 (GCR1_P1_STOP | GCR1_P2_STOP),
1149 mc->fsl_mc_regs + FSL_MC_GCR1);
1150 }
1151 }
1152
1153 static const struct of_device_id fsl_mc_bus_match_table[] = {
1154 {.compatible = "fsl,qoriq-mc",},
1155 {},
1156 };
1157
1158 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1159
1160 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1161 {"NXP0008", 0 },
1162 { }
1163 };
1164 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1165
1166 static struct platform_driver fsl_mc_bus_driver = {
1167 .driver = {
1168 .name = "fsl_mc_bus",
1169 .pm = NULL,
1170 .of_match_table = fsl_mc_bus_match_table,
1171 .acpi_match_table = fsl_mc_bus_acpi_match_table,
1172 },
1173 .probe = fsl_mc_bus_probe,
1174 .remove = fsl_mc_bus_remove,
1175 .shutdown = fsl_mc_bus_remove,
1176 };
1177
fsl_mc_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1178 static int fsl_mc_bus_notifier(struct notifier_block *nb,
1179 unsigned long action, void *data)
1180 {
1181 struct device *dev = data;
1182 struct resource *res;
1183 void __iomem *fsl_mc_regs;
1184
1185 if (action != BUS_NOTIFY_ADD_DEVICE)
1186 return 0;
1187
1188 if (!of_match_device(fsl_mc_bus_match_table, dev) &&
1189 !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
1190 return 0;
1191
1192 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
1193 if (!res)
1194 return 0;
1195
1196 fsl_mc_regs = ioremap(res->start, resource_size(res));
1197 if (!fsl_mc_regs)
1198 return 0;
1199
1200 /*
1201 * Make sure that the MC firmware is paused before the IOMMU setup for
1202 * it is done or otherwise the firmware will crash right after the SMMU
1203 * gets probed and enabled.
1204 */
1205 writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
1206 fsl_mc_regs + FSL_MC_GCR1);
1207 iounmap(fsl_mc_regs);
1208
1209 return 0;
1210 }
1211
1212 static struct notifier_block fsl_mc_nb = {
1213 .notifier_call = fsl_mc_bus_notifier,
1214 };
1215
fsl_mc_bus_driver_init(void)1216 static int __init fsl_mc_bus_driver_init(void)
1217 {
1218 int error;
1219
1220 error = bus_register(&fsl_mc_bus_type);
1221 if (error < 0) {
1222 pr_err("bus type registration failed: %d\n", error);
1223 goto error_cleanup_cache;
1224 }
1225
1226 error = platform_driver_register(&fsl_mc_bus_driver);
1227 if (error < 0) {
1228 pr_err("platform_driver_register() failed: %d\n", error);
1229 goto error_cleanup_bus;
1230 }
1231
1232 error = dprc_driver_init();
1233 if (error < 0)
1234 goto error_cleanup_driver;
1235
1236 error = fsl_mc_allocator_driver_init();
1237 if (error < 0)
1238 goto error_cleanup_dprc_driver;
1239
1240 return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
1241
1242 error_cleanup_dprc_driver:
1243 dprc_driver_exit();
1244
1245 error_cleanup_driver:
1246 platform_driver_unregister(&fsl_mc_bus_driver);
1247
1248 error_cleanup_bus:
1249 bus_unregister(&fsl_mc_bus_type);
1250
1251 error_cleanup_cache:
1252 return error;
1253 }
1254 postcore_initcall(fsl_mc_bus_driver_init);
1255