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