xref: /linux/drivers/bus/fsl-mc/fsl-mc-bus.c (revision 26fbb4c8c7c3ee9a4c3b4de555a8587b5a19154e)
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 *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  */
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  */
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 
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 
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 
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 
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 
211 struct bus_type fsl_mc_bus_type = {
212 	.name = "fsl-mc",
213 	.match = fsl_mc_bus_match,
214 	.uevent = fsl_mc_bus_uevent,
215 	.dma_configure  = fsl_mc_dma_configure,
216 	.dev_groups = fsl_mc_dev_groups,
217 };
218 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
219 
220 struct device_type fsl_mc_bus_dprc_type = {
221 	.name = "fsl_mc_bus_dprc"
222 };
223 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
224 
225 struct device_type fsl_mc_bus_dpni_type = {
226 	.name = "fsl_mc_bus_dpni"
227 };
228 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
229 
230 struct device_type fsl_mc_bus_dpio_type = {
231 	.name = "fsl_mc_bus_dpio"
232 };
233 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
234 
235 struct device_type fsl_mc_bus_dpsw_type = {
236 	.name = "fsl_mc_bus_dpsw"
237 };
238 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
239 
240 struct device_type fsl_mc_bus_dpbp_type = {
241 	.name = "fsl_mc_bus_dpbp"
242 };
243 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
244 
245 struct device_type fsl_mc_bus_dpcon_type = {
246 	.name = "fsl_mc_bus_dpcon"
247 };
248 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
249 
250 struct device_type fsl_mc_bus_dpmcp_type = {
251 	.name = "fsl_mc_bus_dpmcp"
252 };
253 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
254 
255 struct device_type fsl_mc_bus_dpmac_type = {
256 	.name = "fsl_mc_bus_dpmac"
257 };
258 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
259 
260 struct device_type fsl_mc_bus_dprtc_type = {
261 	.name = "fsl_mc_bus_dprtc"
262 };
263 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
264 
265 struct device_type fsl_mc_bus_dpseci_type = {
266 	.name = "fsl_mc_bus_dpseci"
267 };
268 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
269 
270 struct device_type fsl_mc_bus_dpdmux_type = {
271 	.name = "fsl_mc_bus_dpdmux"
272 };
273 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
274 
275 struct device_type fsl_mc_bus_dpdcei_type = {
276 	.name = "fsl_mc_bus_dpdcei"
277 };
278 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
279 
280 struct device_type fsl_mc_bus_dpaiop_type = {
281 	.name = "fsl_mc_bus_dpaiop"
282 };
283 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
284 
285 struct device_type fsl_mc_bus_dpci_type = {
286 	.name = "fsl_mc_bus_dpci"
287 };
288 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
289 
290 struct device_type fsl_mc_bus_dpdmai_type = {
291 	.name = "fsl_mc_bus_dpdmai"
292 };
293 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
294 
295 static struct device_type *fsl_mc_get_device_type(const char *type)
296 {
297 	static const struct {
298 		struct device_type *dev_type;
299 		const char *type;
300 	} dev_types[] = {
301 		{ &fsl_mc_bus_dprc_type, "dprc" },
302 		{ &fsl_mc_bus_dpni_type, "dpni" },
303 		{ &fsl_mc_bus_dpio_type, "dpio" },
304 		{ &fsl_mc_bus_dpsw_type, "dpsw" },
305 		{ &fsl_mc_bus_dpbp_type, "dpbp" },
306 		{ &fsl_mc_bus_dpcon_type, "dpcon" },
307 		{ &fsl_mc_bus_dpmcp_type, "dpmcp" },
308 		{ &fsl_mc_bus_dpmac_type, "dpmac" },
309 		{ &fsl_mc_bus_dprtc_type, "dprtc" },
310 		{ &fsl_mc_bus_dpseci_type, "dpseci" },
311 		{ &fsl_mc_bus_dpdmux_type, "dpdmux" },
312 		{ &fsl_mc_bus_dpdcei_type, "dpdcei" },
313 		{ &fsl_mc_bus_dpaiop_type, "dpaiop" },
314 		{ &fsl_mc_bus_dpci_type, "dpci" },
315 		{ &fsl_mc_bus_dpdmai_type, "dpdmai" },
316 		{ NULL, NULL }
317 	};
318 	int i;
319 
320 	for (i = 0; dev_types[i].dev_type; i++)
321 		if (!strcmp(dev_types[i].type, type))
322 			return dev_types[i].dev_type;
323 
324 	return NULL;
325 }
326 
327 static int fsl_mc_driver_probe(struct device *dev)
328 {
329 	struct fsl_mc_driver *mc_drv;
330 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
331 	int error;
332 
333 	mc_drv = to_fsl_mc_driver(dev->driver);
334 
335 	error = mc_drv->probe(mc_dev);
336 	if (error < 0) {
337 		if (error != -EPROBE_DEFER)
338 			dev_err(dev, "%s failed: %d\n", __func__, error);
339 		return error;
340 	}
341 
342 	return 0;
343 }
344 
345 static int fsl_mc_driver_remove(struct device *dev)
346 {
347 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
348 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
349 	int error;
350 
351 	error = mc_drv->remove(mc_dev);
352 	if (error < 0) {
353 		dev_err(dev, "%s failed: %d\n", __func__, error);
354 		return error;
355 	}
356 
357 	return 0;
358 }
359 
360 static void fsl_mc_driver_shutdown(struct device *dev)
361 {
362 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
363 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
364 
365 	mc_drv->shutdown(mc_dev);
366 }
367 
368 /**
369  * __fsl_mc_driver_register - registers a child device driver with the
370  * MC bus
371  *
372  * This function is implicitly invoked from the registration function of
373  * fsl_mc device drivers, which is generated by the
374  * module_fsl_mc_driver() macro.
375  */
376 int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
377 			     struct module *owner)
378 {
379 	int error;
380 
381 	mc_driver->driver.owner = owner;
382 	mc_driver->driver.bus = &fsl_mc_bus_type;
383 
384 	if (mc_driver->probe)
385 		mc_driver->driver.probe = fsl_mc_driver_probe;
386 
387 	if (mc_driver->remove)
388 		mc_driver->driver.remove = fsl_mc_driver_remove;
389 
390 	if (mc_driver->shutdown)
391 		mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
392 
393 	error = driver_register(&mc_driver->driver);
394 	if (error < 0) {
395 		pr_err("driver_register() failed for %s: %d\n",
396 		       mc_driver->driver.name, error);
397 		return error;
398 	}
399 
400 	return 0;
401 }
402 EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
403 
404 /**
405  * fsl_mc_driver_unregister - unregisters a device driver from the
406  * MC bus
407  */
408 void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
409 {
410 	driver_unregister(&mc_driver->driver);
411 }
412 EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
413 
414 /**
415  * mc_get_version() - Retrieves the Management Complex firmware
416  *			version information
417  * @mc_io:		Pointer to opaque I/O object
418  * @cmd_flags:		Command flags; one or more of 'MC_CMD_FLAG_'
419  * @mc_ver_info:	Returned version information structure
420  *
421  * Return:	'0' on Success; Error code otherwise.
422  */
423 static int mc_get_version(struct fsl_mc_io *mc_io,
424 			  u32 cmd_flags,
425 			  struct fsl_mc_version *mc_ver_info)
426 {
427 	struct fsl_mc_command cmd = { 0 };
428 	struct dpmng_rsp_get_version *rsp_params;
429 	int err;
430 
431 	/* prepare command */
432 	cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
433 					  cmd_flags,
434 					  0);
435 
436 	/* send command to mc*/
437 	err = mc_send_command(mc_io, &cmd);
438 	if (err)
439 		return err;
440 
441 	/* retrieve response parameters */
442 	rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
443 	mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
444 	mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
445 	mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
446 
447 	return 0;
448 }
449 
450 /**
451  * fsl_mc_get_version - function to retrieve the MC f/w version information
452  *
453  * Return:	mc version when called after fsl-mc-bus probe; NULL otherwise.
454  */
455 struct fsl_mc_version *fsl_mc_get_version(void)
456 {
457 	if (mc_version.major)
458 		return &mc_version;
459 
460 	return NULL;
461 }
462 EXPORT_SYMBOL_GPL(fsl_mc_get_version);
463 
464 /**
465  * fsl_mc_get_root_dprc - function to traverse to the root dprc
466  */
467 void fsl_mc_get_root_dprc(struct device *dev,
468 			 struct device **root_dprc_dev)
469 {
470 	if (!dev) {
471 		*root_dprc_dev = NULL;
472 	} else if (!dev_is_fsl_mc(dev)) {
473 		*root_dprc_dev = NULL;
474 	} else {
475 		*root_dprc_dev = dev;
476 		while (dev_is_fsl_mc((*root_dprc_dev)->parent))
477 			*root_dprc_dev = (*root_dprc_dev)->parent;
478 	}
479 }
480 
481 static int get_dprc_attr(struct fsl_mc_io *mc_io,
482 			 int container_id, struct dprc_attributes *attr)
483 {
484 	u16 dprc_handle;
485 	int error;
486 
487 	error = dprc_open(mc_io, 0, container_id, &dprc_handle);
488 	if (error < 0) {
489 		dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
490 		return error;
491 	}
492 
493 	memset(attr, 0, sizeof(struct dprc_attributes));
494 	error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
495 	if (error < 0) {
496 		dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
497 			error);
498 		goto common_cleanup;
499 	}
500 
501 	error = 0;
502 
503 common_cleanup:
504 	(void)dprc_close(mc_io, 0, dprc_handle);
505 	return error;
506 }
507 
508 static int get_dprc_icid(struct fsl_mc_io *mc_io,
509 			 int container_id, u32 *icid)
510 {
511 	struct dprc_attributes attr;
512 	int error;
513 
514 	error = get_dprc_attr(mc_io, container_id, &attr);
515 	if (error == 0)
516 		*icid = attr.icid;
517 
518 	return error;
519 }
520 
521 static int translate_mc_addr(struct fsl_mc_device *mc_dev,
522 			     enum dprc_region_type mc_region_type,
523 			     u64 mc_offset, phys_addr_t *phys_addr)
524 {
525 	int i;
526 	struct device *root_dprc_dev;
527 	struct fsl_mc *mc;
528 
529 	fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
530 	mc = dev_get_drvdata(root_dprc_dev->parent);
531 
532 	if (mc->num_translation_ranges == 0) {
533 		/*
534 		 * Do identity mapping:
535 		 */
536 		*phys_addr = mc_offset;
537 		return 0;
538 	}
539 
540 	for (i = 0; i < mc->num_translation_ranges; i++) {
541 		struct fsl_mc_addr_translation_range *range =
542 			&mc->translation_ranges[i];
543 
544 		if (mc_region_type == range->mc_region_type &&
545 		    mc_offset >= range->start_mc_offset &&
546 		    mc_offset < range->end_mc_offset) {
547 			*phys_addr = range->start_phys_addr +
548 				     (mc_offset - range->start_mc_offset);
549 			return 0;
550 		}
551 	}
552 
553 	return -EFAULT;
554 }
555 
556 static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
557 					  struct fsl_mc_device *mc_bus_dev)
558 {
559 	int i;
560 	int error;
561 	struct resource *regions;
562 	struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
563 	struct device *parent_dev = mc_dev->dev.parent;
564 	enum dprc_region_type mc_region_type;
565 
566 	if (is_fsl_mc_bus_dprc(mc_dev) ||
567 	    is_fsl_mc_bus_dpmcp(mc_dev)) {
568 		mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
569 	} else if (is_fsl_mc_bus_dpio(mc_dev)) {
570 		mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
571 	} else {
572 		/*
573 		 * This function should not have been called for this MC object
574 		 * type, as this object type is not supposed to have MMIO
575 		 * regions
576 		 */
577 		return -EINVAL;
578 	}
579 
580 	regions = kmalloc_array(obj_desc->region_count,
581 				sizeof(regions[0]), GFP_KERNEL);
582 	if (!regions)
583 		return -ENOMEM;
584 
585 	for (i = 0; i < obj_desc->region_count; i++) {
586 		struct dprc_region_desc region_desc;
587 
588 		error = dprc_get_obj_region(mc_bus_dev->mc_io,
589 					    0,
590 					    mc_bus_dev->mc_handle,
591 					    obj_desc->type,
592 					    obj_desc->id, i, &region_desc);
593 		if (error < 0) {
594 			dev_err(parent_dev,
595 				"dprc_get_obj_region() failed: %d\n", error);
596 			goto error_cleanup_regions;
597 		}
598 		/*
599 		 * Older MC only returned region offset and no base address
600 		 * If base address is in the region_desc use it otherwise
601 		 * revert to old mechanism
602 		 */
603 		if (region_desc.base_address)
604 			regions[i].start = region_desc.base_address +
605 						region_desc.base_offset;
606 		else
607 			error = translate_mc_addr(mc_dev, mc_region_type,
608 					  region_desc.base_offset,
609 					  &regions[i].start);
610 
611 		if (error < 0) {
612 			dev_err(parent_dev,
613 				"Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
614 				region_desc.base_offset,
615 				obj_desc->type, obj_desc->id, i);
616 			goto error_cleanup_regions;
617 		}
618 
619 		regions[i].end = regions[i].start + region_desc.size - 1;
620 		regions[i].name = "fsl-mc object MMIO region";
621 		regions[i].flags = region_desc.flags & IORESOURCE_BITS;
622 		regions[i].flags |= IORESOURCE_MEM;
623 	}
624 
625 	mc_dev->regions = regions;
626 	return 0;
627 
628 error_cleanup_regions:
629 	kfree(regions);
630 	return error;
631 }
632 
633 /**
634  * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
635  */
636 bool fsl_mc_is_root_dprc(struct device *dev)
637 {
638 	struct device *root_dprc_dev;
639 
640 	fsl_mc_get_root_dprc(dev, &root_dprc_dev);
641 	if (!root_dprc_dev)
642 		return false;
643 	return dev == root_dprc_dev;
644 }
645 
646 static void fsl_mc_device_release(struct device *dev)
647 {
648 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
649 
650 	kfree(mc_dev->regions);
651 
652 	if (is_fsl_mc_bus_dprc(mc_dev))
653 		kfree(to_fsl_mc_bus(mc_dev));
654 	else
655 		kfree(mc_dev);
656 }
657 
658 /**
659  * Add a newly discovered fsl-mc device to be visible in Linux
660  */
661 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
662 		      struct fsl_mc_io *mc_io,
663 		      struct device *parent_dev,
664 		      struct fsl_mc_device **new_mc_dev)
665 {
666 	int error;
667 	struct fsl_mc_device *mc_dev = NULL;
668 	struct fsl_mc_bus *mc_bus = NULL;
669 	struct fsl_mc_device *parent_mc_dev;
670 
671 	if (dev_is_fsl_mc(parent_dev))
672 		parent_mc_dev = to_fsl_mc_device(parent_dev);
673 	else
674 		parent_mc_dev = NULL;
675 
676 	if (strcmp(obj_desc->type, "dprc") == 0) {
677 		/*
678 		 * Allocate an MC bus device object:
679 		 */
680 		mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
681 		if (!mc_bus)
682 			return -ENOMEM;
683 
684 		mutex_init(&mc_bus->scan_mutex);
685 		mc_dev = &mc_bus->mc_dev;
686 	} else {
687 		/*
688 		 * Allocate a regular fsl_mc_device object:
689 		 */
690 		mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
691 		if (!mc_dev)
692 			return -ENOMEM;
693 	}
694 
695 	mc_dev->obj_desc = *obj_desc;
696 	mc_dev->mc_io = mc_io;
697 	device_initialize(&mc_dev->dev);
698 	mc_dev->dev.parent = parent_dev;
699 	mc_dev->dev.bus = &fsl_mc_bus_type;
700 	mc_dev->dev.release = fsl_mc_device_release;
701 	mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
702 	if (!mc_dev->dev.type) {
703 		error = -ENODEV;
704 		dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
705 		goto error_cleanup_dev;
706 	}
707 	dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
708 
709 	if (strcmp(obj_desc->type, "dprc") == 0) {
710 		struct fsl_mc_io *mc_io2;
711 
712 		mc_dev->flags |= FSL_MC_IS_DPRC;
713 
714 		/*
715 		 * To get the DPRC's ICID, we need to open the DPRC
716 		 * in get_dprc_icid(). For child DPRCs, we do so using the
717 		 * parent DPRC's MC portal instead of the child DPRC's MC
718 		 * portal, in case the child DPRC is already opened with
719 		 * its own portal (e.g., the DPRC used by AIOP).
720 		 *
721 		 * NOTE: There cannot be more than one active open for a
722 		 * given MC object, using the same MC portal.
723 		 */
724 		if (parent_mc_dev) {
725 			/*
726 			 * device being added is a child DPRC device
727 			 */
728 			mc_io2 = parent_mc_dev->mc_io;
729 		} else {
730 			/*
731 			 * device being added is the root DPRC device
732 			 */
733 			if (!mc_io) {
734 				error = -EINVAL;
735 				goto error_cleanup_dev;
736 			}
737 
738 			mc_io2 = mc_io;
739 		}
740 
741 		error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
742 		if (error < 0)
743 			goto error_cleanup_dev;
744 	} else {
745 		/*
746 		 * A non-DPRC object has to be a child of a DPRC, use the
747 		 * parent's ICID and interrupt domain.
748 		 */
749 		mc_dev->icid = parent_mc_dev->icid;
750 		mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
751 		mc_dev->dev.dma_mask = &mc_dev->dma_mask;
752 		mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
753 		dev_set_msi_domain(&mc_dev->dev,
754 				   dev_get_msi_domain(&parent_mc_dev->dev));
755 	}
756 
757 	/*
758 	 * Get MMIO regions for the device from the MC:
759 	 *
760 	 * NOTE: the root DPRC is a special case as its MMIO region is
761 	 * obtained from the device tree
762 	 */
763 	if (parent_mc_dev && obj_desc->region_count != 0) {
764 		error = fsl_mc_device_get_mmio_regions(mc_dev,
765 						       parent_mc_dev);
766 		if (error < 0)
767 			goto error_cleanup_dev;
768 	}
769 
770 	/*
771 	 * The device-specific probe callback will get invoked by device_add()
772 	 */
773 	error = device_add(&mc_dev->dev);
774 	if (error < 0) {
775 		dev_err(parent_dev,
776 			"device_add() failed for device %s: %d\n",
777 			dev_name(&mc_dev->dev), error);
778 		goto error_cleanup_dev;
779 	}
780 
781 	dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
782 
783 	*new_mc_dev = mc_dev;
784 	return 0;
785 
786 error_cleanup_dev:
787 	kfree(mc_dev->regions);
788 	kfree(mc_bus);
789 	kfree(mc_dev);
790 
791 	return error;
792 }
793 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
794 
795 /**
796  * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
797  * Linux
798  *
799  * @mc_dev: Pointer to an fsl-mc device
800  */
801 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
802 {
803 	kfree(mc_dev->driver_override);
804 	mc_dev->driver_override = NULL;
805 
806 	/*
807 	 * The device-specific remove callback will get invoked by device_del()
808 	 */
809 	device_del(&mc_dev->dev);
810 	put_device(&mc_dev->dev);
811 }
812 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
813 
814 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
815 {
816 	struct fsl_mc_device *mc_bus_dev, *endpoint;
817 	struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
818 	struct dprc_endpoint endpoint1 = {{ 0 }};
819 	struct dprc_endpoint endpoint2 = {{ 0 }};
820 	int state, err;
821 
822 	mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
823 	strcpy(endpoint1.type, mc_dev->obj_desc.type);
824 	endpoint1.id = mc_dev->obj_desc.id;
825 
826 	err = dprc_get_connection(mc_bus_dev->mc_io, 0,
827 				  mc_bus_dev->mc_handle,
828 				  &endpoint1, &endpoint2,
829 				  &state);
830 
831 	if (err == -ENOTCONN || state == -1)
832 		return ERR_PTR(-ENOTCONN);
833 
834 	if (err < 0) {
835 		dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
836 		return ERR_PTR(err);
837 	}
838 
839 	strcpy(endpoint_desc.type, endpoint2.type);
840 	endpoint_desc.id = endpoint2.id;
841 	endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
842 
843 	return endpoint;
844 }
845 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
846 
847 static int parse_mc_ranges(struct device *dev,
848 			   int *paddr_cells,
849 			   int *mc_addr_cells,
850 			   int *mc_size_cells,
851 			   const __be32 **ranges_start)
852 {
853 	const __be32 *prop;
854 	int range_tuple_cell_count;
855 	int ranges_len;
856 	int tuple_len;
857 	struct device_node *mc_node = dev->of_node;
858 
859 	*ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
860 	if (!(*ranges_start) || !ranges_len) {
861 		dev_warn(dev,
862 			 "missing or empty ranges property for device tree node '%pOFn'\n",
863 			 mc_node);
864 		return 0;
865 	}
866 
867 	*paddr_cells = of_n_addr_cells(mc_node);
868 
869 	prop = of_get_property(mc_node, "#address-cells", NULL);
870 	if (prop)
871 		*mc_addr_cells = be32_to_cpup(prop);
872 	else
873 		*mc_addr_cells = *paddr_cells;
874 
875 	prop = of_get_property(mc_node, "#size-cells", NULL);
876 	if (prop)
877 		*mc_size_cells = be32_to_cpup(prop);
878 	else
879 		*mc_size_cells = of_n_size_cells(mc_node);
880 
881 	range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
882 				 *mc_size_cells;
883 
884 	tuple_len = range_tuple_cell_count * sizeof(__be32);
885 	if (ranges_len % tuple_len != 0) {
886 		dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
887 		return -EINVAL;
888 	}
889 
890 	return ranges_len / tuple_len;
891 }
892 
893 static int get_mc_addr_translation_ranges(struct device *dev,
894 					  struct fsl_mc_addr_translation_range
895 						**ranges,
896 					  u8 *num_ranges)
897 {
898 	int ret;
899 	int paddr_cells;
900 	int mc_addr_cells;
901 	int mc_size_cells;
902 	int i;
903 	const __be32 *ranges_start;
904 	const __be32 *cell;
905 
906 	ret = parse_mc_ranges(dev,
907 			      &paddr_cells,
908 			      &mc_addr_cells,
909 			      &mc_size_cells,
910 			      &ranges_start);
911 	if (ret < 0)
912 		return ret;
913 
914 	*num_ranges = ret;
915 	if (!ret) {
916 		/*
917 		 * Missing or empty ranges property ("ranges;") for the
918 		 * 'fsl,qoriq-mc' node. In this case, identity mapping
919 		 * will be used.
920 		 */
921 		*ranges = NULL;
922 		return 0;
923 	}
924 
925 	*ranges = devm_kcalloc(dev, *num_ranges,
926 			       sizeof(struct fsl_mc_addr_translation_range),
927 			       GFP_KERNEL);
928 	if (!(*ranges))
929 		return -ENOMEM;
930 
931 	cell = ranges_start;
932 	for (i = 0; i < *num_ranges; ++i) {
933 		struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
934 
935 		range->mc_region_type = of_read_number(cell, 1);
936 		range->start_mc_offset = of_read_number(cell + 1,
937 							mc_addr_cells - 1);
938 		cell += mc_addr_cells;
939 		range->start_phys_addr = of_read_number(cell, paddr_cells);
940 		cell += paddr_cells;
941 		range->end_mc_offset = range->start_mc_offset +
942 				     of_read_number(cell, mc_size_cells);
943 
944 		cell += mc_size_cells;
945 	}
946 
947 	return 0;
948 }
949 
950 /**
951  * fsl_mc_bus_probe - callback invoked when the root MC bus is being
952  * added
953  */
954 static int fsl_mc_bus_probe(struct platform_device *pdev)
955 {
956 	struct fsl_mc_obj_desc obj_desc;
957 	int error;
958 	struct fsl_mc *mc;
959 	struct fsl_mc_device *mc_bus_dev = NULL;
960 	struct fsl_mc_io *mc_io = NULL;
961 	int container_id;
962 	phys_addr_t mc_portal_phys_addr;
963 	u32 mc_portal_size, mc_stream_id;
964 	struct resource *plat_res;
965 
966 	mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
967 	if (!mc)
968 		return -ENOMEM;
969 
970 	platform_set_drvdata(pdev, mc);
971 
972 	plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
973 	if (plat_res) {
974 		mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
975 		if (IS_ERR(mc->fsl_mc_regs))
976 			return PTR_ERR(mc->fsl_mc_regs);
977 	}
978 
979 	if (mc->fsl_mc_regs) {
980 		/*
981 		 * Some bootloaders pause the MC firmware before booting the
982 		 * kernel so that MC will not cause faults as soon as the
983 		 * SMMU probes due to the fact that there's no configuration
984 		 * in place for MC.
985 		 * At this point MC should have all its SMMU setup done so make
986 		 * sure it is resumed.
987 		 */
988 		writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) & (~GCR1_P1_STOP),
989 		       mc->fsl_mc_regs + FSL_MC_GCR1);
990 
991 		if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
992 			mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
993 			/*
994 			 * HW ORs the PL and BMT bit, places the result in bit
995 			 * 14 of the StreamID and ORs in the ICID. Calculate it
996 			 * accordingly.
997 			 */
998 			mc_stream_id = (mc_stream_id & 0xffff) |
999 				((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1000 					BIT(14) : 0);
1001 			error = acpi_dma_configure_id(&pdev->dev,
1002 						      DEV_DMA_COHERENT,
1003 						      &mc_stream_id);
1004 			if (error)
1005 				dev_warn(&pdev->dev,
1006 					 "failed to configure dma: %d.\n",
1007 					 error);
1008 		}
1009 	}
1010 
1011 	/*
1012 	 * Get physical address of MC portal for the root DPRC:
1013 	 */
1014 	plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1015 	mc_portal_phys_addr = plat_res->start;
1016 	mc_portal_size = resource_size(plat_res);
1017 	error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1018 				 mc_portal_size, NULL,
1019 				 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1020 	if (error < 0)
1021 		return error;
1022 
1023 	error = mc_get_version(mc_io, 0, &mc_version);
1024 	if (error != 0) {
1025 		dev_err(&pdev->dev,
1026 			"mc_get_version() failed with error %d\n", error);
1027 		goto error_cleanup_mc_io;
1028 	}
1029 
1030 	dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1031 		 mc_version.major, mc_version.minor, mc_version.revision);
1032 
1033 	if (dev_of_node(&pdev->dev)) {
1034 		error = get_mc_addr_translation_ranges(&pdev->dev,
1035 						&mc->translation_ranges,
1036 						&mc->num_translation_ranges);
1037 		if (error < 0)
1038 			goto error_cleanup_mc_io;
1039 	}
1040 
1041 	error = dprc_get_container_id(mc_io, 0, &container_id);
1042 	if (error < 0) {
1043 		dev_err(&pdev->dev,
1044 			"dprc_get_container_id() failed: %d\n", error);
1045 		goto error_cleanup_mc_io;
1046 	}
1047 
1048 	memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1049 	error = dprc_get_api_version(mc_io, 0,
1050 				     &obj_desc.ver_major,
1051 				     &obj_desc.ver_minor);
1052 	if (error < 0)
1053 		goto error_cleanup_mc_io;
1054 
1055 	obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1056 	strcpy(obj_desc.type, "dprc");
1057 	obj_desc.id = container_id;
1058 	obj_desc.irq_count = 1;
1059 	obj_desc.region_count = 0;
1060 
1061 	error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1062 	if (error < 0)
1063 		goto error_cleanup_mc_io;
1064 
1065 	mc->root_mc_bus_dev = mc_bus_dev;
1066 	mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1067 	return 0;
1068 
1069 error_cleanup_mc_io:
1070 	fsl_destroy_mc_io(mc_io);
1071 	return error;
1072 }
1073 
1074 /**
1075  * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1076  * removed
1077  */
1078 static int fsl_mc_bus_remove(struct platform_device *pdev)
1079 {
1080 	struct fsl_mc *mc = platform_get_drvdata(pdev);
1081 
1082 	if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1083 		return -EINVAL;
1084 
1085 	fsl_mc_device_remove(mc->root_mc_bus_dev);
1086 
1087 	fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
1088 	mc->root_mc_bus_dev->mc_io = NULL;
1089 
1090 	return 0;
1091 }
1092 
1093 static const struct of_device_id fsl_mc_bus_match_table[] = {
1094 	{.compatible = "fsl,qoriq-mc",},
1095 	{},
1096 };
1097 
1098 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1099 
1100 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1101 	{"NXP0008", 0 },
1102 	{ }
1103 };
1104 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1105 
1106 static struct platform_driver fsl_mc_bus_driver = {
1107 	.driver = {
1108 		   .name = "fsl_mc_bus",
1109 		   .pm = NULL,
1110 		   .of_match_table = fsl_mc_bus_match_table,
1111 		   .acpi_match_table = fsl_mc_bus_acpi_match_table,
1112 		   },
1113 	.probe = fsl_mc_bus_probe,
1114 	.remove = fsl_mc_bus_remove,
1115 };
1116 
1117 static int __init fsl_mc_bus_driver_init(void)
1118 {
1119 	int error;
1120 
1121 	error = bus_register(&fsl_mc_bus_type);
1122 	if (error < 0) {
1123 		pr_err("bus type registration failed: %d\n", error);
1124 		goto error_cleanup_cache;
1125 	}
1126 
1127 	error = platform_driver_register(&fsl_mc_bus_driver);
1128 	if (error < 0) {
1129 		pr_err("platform_driver_register() failed: %d\n", error);
1130 		goto error_cleanup_bus;
1131 	}
1132 
1133 	error = dprc_driver_init();
1134 	if (error < 0)
1135 		goto error_cleanup_driver;
1136 
1137 	error = fsl_mc_allocator_driver_init();
1138 	if (error < 0)
1139 		goto error_cleanup_dprc_driver;
1140 
1141 	return 0;
1142 
1143 error_cleanup_dprc_driver:
1144 	dprc_driver_exit();
1145 
1146 error_cleanup_driver:
1147 	platform_driver_unregister(&fsl_mc_bus_driver);
1148 
1149 error_cleanup_bus:
1150 	bus_unregister(&fsl_mc_bus_type);
1151 
1152 error_cleanup_cache:
1153 	return error;
1154 }
1155 postcore_initcall(fsl_mc_bus_driver_init);
1156