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