xref: /openbmc/linux/drivers/bus/fsl-mc/fsl-mc-bus.c (revision cc958441)
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/dma-mapping.h>
21 #include <linux/acpi.h>
22 #include <linux/iommu.h>
23 #include <linux/dma-map-ops.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 #define GCR1_P2_STOP	BIT(30)
67 
68 #define FSL_MC_FAPR	0x28
69 #define MC_FAPR_PL	BIT(18)
70 #define MC_FAPR_BMT	BIT(17)
71 
72 static phys_addr_t mc_portal_base_phys_addr;
73 
74 /**
75  * fsl_mc_bus_match - device to driver matching callback
76  * @dev: the fsl-mc device to match against
77  * @drv: the device driver to search for matching fsl-mc object type
78  * structures
79  *
80  * Returns 1 on success, 0 otherwise.
81  */
82 static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
83 {
84 	const struct fsl_mc_device_id *id;
85 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
86 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
87 	bool found = false;
88 
89 	/* When driver_override is set, only bind to the matching driver */
90 	if (mc_dev->driver_override) {
91 		found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
92 		goto out;
93 	}
94 
95 	if (!mc_drv->match_id_table)
96 		goto out;
97 
98 	/*
99 	 * If the object is not 'plugged' don't match.
100 	 * Only exception is the root DPRC, which is a special case.
101 	 */
102 	if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
103 	    !fsl_mc_is_root_dprc(&mc_dev->dev))
104 		goto out;
105 
106 	/*
107 	 * Traverse the match_id table of the given driver, trying to find
108 	 * a matching for the given device.
109 	 */
110 	for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
111 		if (id->vendor == mc_dev->obj_desc.vendor &&
112 		    strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
113 			found = true;
114 
115 			break;
116 		}
117 	}
118 
119 out:
120 	dev_dbg(dev, "%smatched\n", found ? "" : "not ");
121 	return found;
122 }
123 
124 /*
125  * fsl_mc_bus_uevent - callback invoked when a device is added
126  */
127 static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
128 {
129 	const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
130 
131 	if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
132 			   mc_dev->obj_desc.vendor,
133 			   mc_dev->obj_desc.type))
134 		return -ENOMEM;
135 
136 	return 0;
137 }
138 
139 static int fsl_mc_dma_configure(struct device *dev)
140 {
141 	struct device *dma_dev = dev;
142 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
143 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
144 	u32 input_id = mc_dev->icid;
145 	int ret;
146 
147 	while (dev_is_fsl_mc(dma_dev))
148 		dma_dev = dma_dev->parent;
149 
150 	if (dev_of_node(dma_dev))
151 		ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
152 	else
153 		ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
154 
155 	if (!ret && !mc_drv->driver_managed_dma) {
156 		ret = iommu_device_use_default_domain(dev);
157 		if (ret)
158 			arch_teardown_dma_ops(dev);
159 	}
160 
161 	return ret;
162 }
163 
164 static void fsl_mc_dma_cleanup(struct device *dev)
165 {
166 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
167 
168 	if (!mc_drv->driver_managed_dma)
169 		iommu_device_unuse_default_domain(dev);
170 }
171 
172 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
173 			     char *buf)
174 {
175 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
176 
177 	return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
178 		       mc_dev->obj_desc.type);
179 }
180 static DEVICE_ATTR_RO(modalias);
181 
182 static ssize_t driver_override_store(struct device *dev,
183 				     struct device_attribute *attr,
184 				     const char *buf, size_t count)
185 {
186 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
187 	int ret;
188 
189 	if (WARN_ON(dev->bus != &fsl_mc_bus_type))
190 		return -EINVAL;
191 
192 	ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
193 	if (ret)
194 		return ret;
195 
196 	return count;
197 }
198 
199 static ssize_t driver_override_show(struct device *dev,
200 				    struct device_attribute *attr, char *buf)
201 {
202 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
203 
204 	return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
205 }
206 static DEVICE_ATTR_RW(driver_override);
207 
208 static struct attribute *fsl_mc_dev_attrs[] = {
209 	&dev_attr_modalias.attr,
210 	&dev_attr_driver_override.attr,
211 	NULL,
212 };
213 
214 ATTRIBUTE_GROUPS(fsl_mc_dev);
215 
216 static int scan_fsl_mc_bus(struct device *dev, void *data)
217 {
218 	struct fsl_mc_device *root_mc_dev;
219 	struct fsl_mc_bus *root_mc_bus;
220 
221 	if (!fsl_mc_is_root_dprc(dev))
222 		goto exit;
223 
224 	root_mc_dev = to_fsl_mc_device(dev);
225 	root_mc_bus = to_fsl_mc_bus(root_mc_dev);
226 	mutex_lock(&root_mc_bus->scan_mutex);
227 	dprc_scan_objects(root_mc_dev, false);
228 	mutex_unlock(&root_mc_bus->scan_mutex);
229 
230 exit:
231 	return 0;
232 }
233 
234 static ssize_t rescan_store(const struct bus_type *bus,
235 			    const char *buf, size_t count)
236 {
237 	unsigned long val;
238 
239 	if (kstrtoul(buf, 0, &val) < 0)
240 		return -EINVAL;
241 
242 	if (val)
243 		bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
244 
245 	return count;
246 }
247 static BUS_ATTR_WO(rescan);
248 
249 static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
250 {
251 	struct fsl_mc_device *root_mc_dev;
252 	unsigned long val;
253 	char *buf = data;
254 
255 	if (!fsl_mc_is_root_dprc(dev))
256 		goto exit;
257 
258 	root_mc_dev = to_fsl_mc_device(dev);
259 
260 	if (kstrtoul(buf, 0, &val) < 0)
261 		return -EINVAL;
262 
263 	if (val)
264 		enable_dprc_irq(root_mc_dev);
265 	else
266 		disable_dprc_irq(root_mc_dev);
267 
268 exit:
269 	return 0;
270 }
271 
272 static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
273 {
274 	struct fsl_mc_device *root_mc_dev;
275 	char *buf = data;
276 
277 	if (!fsl_mc_is_root_dprc(dev))
278 		goto exit;
279 
280 	root_mc_dev = to_fsl_mc_device(dev);
281 
282 	sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
283 exit:
284 	return 0;
285 }
286 
287 static ssize_t autorescan_store(const struct bus_type *bus,
288 				const char *buf, size_t count)
289 {
290 	bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
291 
292 	return count;
293 }
294 
295 static ssize_t autorescan_show(const struct bus_type *bus, char *buf)
296 {
297 	bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
298 	return strlen(buf);
299 }
300 
301 static BUS_ATTR_RW(autorescan);
302 
303 static struct attribute *fsl_mc_bus_attrs[] = {
304 	&bus_attr_rescan.attr,
305 	&bus_attr_autorescan.attr,
306 	NULL,
307 };
308 
309 ATTRIBUTE_GROUPS(fsl_mc_bus);
310 
311 struct bus_type fsl_mc_bus_type = {
312 	.name = "fsl-mc",
313 	.match = fsl_mc_bus_match,
314 	.uevent = fsl_mc_bus_uevent,
315 	.dma_configure  = fsl_mc_dma_configure,
316 	.dma_cleanup = fsl_mc_dma_cleanup,
317 	.dev_groups = fsl_mc_dev_groups,
318 	.bus_groups = fsl_mc_bus_groups,
319 };
320 EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
321 
322 struct device_type fsl_mc_bus_dprc_type = {
323 	.name = "fsl_mc_bus_dprc"
324 };
325 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
326 
327 struct device_type fsl_mc_bus_dpni_type = {
328 	.name = "fsl_mc_bus_dpni"
329 };
330 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
331 
332 struct device_type fsl_mc_bus_dpio_type = {
333 	.name = "fsl_mc_bus_dpio"
334 };
335 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
336 
337 struct device_type fsl_mc_bus_dpsw_type = {
338 	.name = "fsl_mc_bus_dpsw"
339 };
340 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
341 
342 struct device_type fsl_mc_bus_dpbp_type = {
343 	.name = "fsl_mc_bus_dpbp"
344 };
345 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
346 
347 struct device_type fsl_mc_bus_dpcon_type = {
348 	.name = "fsl_mc_bus_dpcon"
349 };
350 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
351 
352 struct device_type fsl_mc_bus_dpmcp_type = {
353 	.name = "fsl_mc_bus_dpmcp"
354 };
355 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
356 
357 struct device_type fsl_mc_bus_dpmac_type = {
358 	.name = "fsl_mc_bus_dpmac"
359 };
360 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
361 
362 struct device_type fsl_mc_bus_dprtc_type = {
363 	.name = "fsl_mc_bus_dprtc"
364 };
365 EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
366 
367 struct device_type fsl_mc_bus_dpseci_type = {
368 	.name = "fsl_mc_bus_dpseci"
369 };
370 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
371 
372 struct device_type fsl_mc_bus_dpdmux_type = {
373 	.name = "fsl_mc_bus_dpdmux"
374 };
375 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
376 
377 struct device_type fsl_mc_bus_dpdcei_type = {
378 	.name = "fsl_mc_bus_dpdcei"
379 };
380 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
381 
382 struct device_type fsl_mc_bus_dpaiop_type = {
383 	.name = "fsl_mc_bus_dpaiop"
384 };
385 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
386 
387 struct device_type fsl_mc_bus_dpci_type = {
388 	.name = "fsl_mc_bus_dpci"
389 };
390 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
391 
392 struct device_type fsl_mc_bus_dpdmai_type = {
393 	.name = "fsl_mc_bus_dpdmai"
394 };
395 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
396 
397 struct device_type fsl_mc_bus_dpdbg_type = {
398 	.name = "fsl_mc_bus_dpdbg"
399 };
400 EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
401 
402 static struct device_type *fsl_mc_get_device_type(const char *type)
403 {
404 	static const struct {
405 		struct device_type *dev_type;
406 		const char *type;
407 	} dev_types[] = {
408 		{ &fsl_mc_bus_dprc_type, "dprc" },
409 		{ &fsl_mc_bus_dpni_type, "dpni" },
410 		{ &fsl_mc_bus_dpio_type, "dpio" },
411 		{ &fsl_mc_bus_dpsw_type, "dpsw" },
412 		{ &fsl_mc_bus_dpbp_type, "dpbp" },
413 		{ &fsl_mc_bus_dpcon_type, "dpcon" },
414 		{ &fsl_mc_bus_dpmcp_type, "dpmcp" },
415 		{ &fsl_mc_bus_dpmac_type, "dpmac" },
416 		{ &fsl_mc_bus_dprtc_type, "dprtc" },
417 		{ &fsl_mc_bus_dpseci_type, "dpseci" },
418 		{ &fsl_mc_bus_dpdmux_type, "dpdmux" },
419 		{ &fsl_mc_bus_dpdcei_type, "dpdcei" },
420 		{ &fsl_mc_bus_dpaiop_type, "dpaiop" },
421 		{ &fsl_mc_bus_dpci_type, "dpci" },
422 		{ &fsl_mc_bus_dpdmai_type, "dpdmai" },
423 		{ &fsl_mc_bus_dpdbg_type, "dpdbg" },
424 		{ NULL, NULL }
425 	};
426 	int i;
427 
428 	for (i = 0; dev_types[i].dev_type; i++)
429 		if (!strcmp(dev_types[i].type, type))
430 			return dev_types[i].dev_type;
431 
432 	return NULL;
433 }
434 
435 static int fsl_mc_driver_probe(struct device *dev)
436 {
437 	struct fsl_mc_driver *mc_drv;
438 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
439 	int error;
440 
441 	mc_drv = to_fsl_mc_driver(dev->driver);
442 
443 	error = mc_drv->probe(mc_dev);
444 	if (error < 0) {
445 		if (error != -EPROBE_DEFER)
446 			dev_err(dev, "%s failed: %d\n", __func__, error);
447 		return error;
448 	}
449 
450 	return 0;
451 }
452 
453 static int fsl_mc_driver_remove(struct device *dev)
454 {
455 	struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
456 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
457 
458 	mc_drv->remove(mc_dev);
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 			/*
715 			 * Some versions of the MC firmware wrongly report
716 			 * 0 for register base address of the DPMCP associated
717 			 * with child DPRC objects thus rendering them unusable.
718 			 * This is particularly troublesome in ACPI boot
719 			 * scenarios where the legacy way of extracting this
720 			 * base address from the device tree does not apply.
721 			 * Given that DPMCPs share the same base address,
722 			 * workaround this by using the base address extracted
723 			 * from the root DPRC container.
724 			 */
725 			if (is_fsl_mc_bus_dprc(mc_dev) &&
726 			    regions[i].start == region_desc.base_offset)
727 				regions[i].start += mc_portal_base_phys_addr;
728 		}
729 
730 		if (error < 0) {
731 			dev_err(parent_dev,
732 				"Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
733 				region_desc.base_offset,
734 				obj_desc->type, obj_desc->id, i);
735 			goto error_cleanup_regions;
736 		}
737 
738 		regions[i].end = regions[i].start + region_desc.size - 1;
739 		regions[i].name = "fsl-mc object MMIO region";
740 		regions[i].flags = region_desc.flags & IORESOURCE_BITS;
741 		regions[i].flags |= IORESOURCE_MEM;
742 	}
743 
744 	mc_dev->regions = regions;
745 	return 0;
746 
747 error_cleanup_regions:
748 	kfree(regions);
749 	return error;
750 }
751 
752 /*
753  * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
754  */
755 bool fsl_mc_is_root_dprc(struct device *dev)
756 {
757 	struct device *root_dprc_dev;
758 
759 	fsl_mc_get_root_dprc(dev, &root_dprc_dev);
760 	if (!root_dprc_dev)
761 		return false;
762 	return dev == root_dprc_dev;
763 }
764 
765 static void fsl_mc_device_release(struct device *dev)
766 {
767 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
768 
769 	kfree(mc_dev->regions);
770 
771 	if (is_fsl_mc_bus_dprc(mc_dev))
772 		kfree(to_fsl_mc_bus(mc_dev));
773 	else
774 		kfree(mc_dev);
775 }
776 
777 /*
778  * Add a newly discovered fsl-mc device to be visible in Linux
779  */
780 int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
781 		      struct fsl_mc_io *mc_io,
782 		      struct device *parent_dev,
783 		      struct fsl_mc_device **new_mc_dev)
784 {
785 	int error;
786 	struct fsl_mc_device *mc_dev = NULL;
787 	struct fsl_mc_bus *mc_bus = NULL;
788 	struct fsl_mc_device *parent_mc_dev;
789 
790 	if (dev_is_fsl_mc(parent_dev))
791 		parent_mc_dev = to_fsl_mc_device(parent_dev);
792 	else
793 		parent_mc_dev = NULL;
794 
795 	if (strcmp(obj_desc->type, "dprc") == 0) {
796 		/*
797 		 * Allocate an MC bus device object:
798 		 */
799 		mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
800 		if (!mc_bus)
801 			return -ENOMEM;
802 
803 		mutex_init(&mc_bus->scan_mutex);
804 		mc_dev = &mc_bus->mc_dev;
805 	} else {
806 		/*
807 		 * Allocate a regular fsl_mc_device object:
808 		 */
809 		mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
810 		if (!mc_dev)
811 			return -ENOMEM;
812 	}
813 
814 	mc_dev->obj_desc = *obj_desc;
815 	mc_dev->mc_io = mc_io;
816 	device_initialize(&mc_dev->dev);
817 	mc_dev->dev.parent = parent_dev;
818 	mc_dev->dev.bus = &fsl_mc_bus_type;
819 	mc_dev->dev.release = fsl_mc_device_release;
820 	mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
821 	if (!mc_dev->dev.type) {
822 		error = -ENODEV;
823 		dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
824 		goto error_cleanup_dev;
825 	}
826 	dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
827 
828 	if (strcmp(obj_desc->type, "dprc") == 0) {
829 		struct fsl_mc_io *mc_io2;
830 
831 		mc_dev->flags |= FSL_MC_IS_DPRC;
832 
833 		/*
834 		 * To get the DPRC's ICID, we need to open the DPRC
835 		 * in get_dprc_icid(). For child DPRCs, we do so using the
836 		 * parent DPRC's MC portal instead of the child DPRC's MC
837 		 * portal, in case the child DPRC is already opened with
838 		 * its own portal (e.g., the DPRC used by AIOP).
839 		 *
840 		 * NOTE: There cannot be more than one active open for a
841 		 * given MC object, using the same MC portal.
842 		 */
843 		if (parent_mc_dev) {
844 			/*
845 			 * device being added is a child DPRC device
846 			 */
847 			mc_io2 = parent_mc_dev->mc_io;
848 		} else {
849 			/*
850 			 * device being added is the root DPRC device
851 			 */
852 			if (!mc_io) {
853 				error = -EINVAL;
854 				goto error_cleanup_dev;
855 			}
856 
857 			mc_io2 = mc_io;
858 		}
859 
860 		error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
861 		if (error < 0)
862 			goto error_cleanup_dev;
863 	} else {
864 		/*
865 		 * A non-DPRC object has to be a child of a DPRC, use the
866 		 * parent's ICID and interrupt domain.
867 		 */
868 		mc_dev->icid = parent_mc_dev->icid;
869 		mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
870 		mc_dev->dev.dma_mask = &mc_dev->dma_mask;
871 		mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
872 		dev_set_msi_domain(&mc_dev->dev,
873 				   dev_get_msi_domain(&parent_mc_dev->dev));
874 	}
875 
876 	/*
877 	 * Get MMIO regions for the device from the MC:
878 	 *
879 	 * NOTE: the root DPRC is a special case as its MMIO region is
880 	 * obtained from the device tree
881 	 */
882 	if (parent_mc_dev && obj_desc->region_count != 0) {
883 		error = fsl_mc_device_get_mmio_regions(mc_dev,
884 						       parent_mc_dev);
885 		if (error < 0)
886 			goto error_cleanup_dev;
887 	}
888 
889 	/*
890 	 * The device-specific probe callback will get invoked by device_add()
891 	 */
892 	error = device_add(&mc_dev->dev);
893 	if (error < 0) {
894 		dev_err(parent_dev,
895 			"device_add() failed for device %s: %d\n",
896 			dev_name(&mc_dev->dev), error);
897 		goto error_cleanup_dev;
898 	}
899 
900 	dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
901 
902 	*new_mc_dev = mc_dev;
903 	return 0;
904 
905 error_cleanup_dev:
906 	kfree(mc_dev->regions);
907 	kfree(mc_bus);
908 	kfree(mc_dev);
909 
910 	return error;
911 }
912 EXPORT_SYMBOL_GPL(fsl_mc_device_add);
913 
914 static struct notifier_block fsl_mc_nb;
915 
916 /**
917  * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
918  * Linux
919  *
920  * @mc_dev: Pointer to an fsl-mc device
921  */
922 void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
923 {
924 	kfree(mc_dev->driver_override);
925 	mc_dev->driver_override = NULL;
926 
927 	/*
928 	 * The device-specific remove callback will get invoked by device_del()
929 	 */
930 	device_del(&mc_dev->dev);
931 	put_device(&mc_dev->dev);
932 }
933 EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
934 
935 struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
936 					  u16 if_id)
937 {
938 	struct fsl_mc_device *mc_bus_dev, *endpoint;
939 	struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
940 	struct dprc_endpoint endpoint1 = {{ 0 }};
941 	struct dprc_endpoint endpoint2 = {{ 0 }};
942 	int state, err;
943 
944 	mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
945 	strcpy(endpoint1.type, mc_dev->obj_desc.type);
946 	endpoint1.id = mc_dev->obj_desc.id;
947 	endpoint1.if_id = if_id;
948 
949 	err = dprc_get_connection(mc_bus_dev->mc_io, 0,
950 				  mc_bus_dev->mc_handle,
951 				  &endpoint1, &endpoint2,
952 				  &state);
953 
954 	if (err == -ENOTCONN || state == -1)
955 		return ERR_PTR(-ENOTCONN);
956 
957 	if (err < 0) {
958 		dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
959 		return ERR_PTR(err);
960 	}
961 
962 	strcpy(endpoint_desc.type, endpoint2.type);
963 	endpoint_desc.id = endpoint2.id;
964 	endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
965 
966 	/*
967 	 * We know that the device has an endpoint because we verified by
968 	 * interrogating the firmware. This is the case when the device was not
969 	 * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
970 	 * Force a rescan of the devices in this container and retry the lookup.
971 	 */
972 	if (!endpoint) {
973 		struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
974 
975 		if (mutex_trylock(&mc_bus->scan_mutex)) {
976 			err = dprc_scan_objects(mc_bus_dev, true);
977 			mutex_unlock(&mc_bus->scan_mutex);
978 		}
979 
980 		if (err < 0)
981 			return ERR_PTR(err);
982 	}
983 
984 	endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
985 	/*
986 	 * This means that the endpoint might reside in a different isolation
987 	 * context (DPRC/container). Not much to do, so return a permssion
988 	 * error.
989 	 */
990 	if (!endpoint)
991 		return ERR_PTR(-EPERM);
992 
993 	return endpoint;
994 }
995 EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
996 
997 static int parse_mc_ranges(struct device *dev,
998 			   int *paddr_cells,
999 			   int *mc_addr_cells,
1000 			   int *mc_size_cells,
1001 			   const __be32 **ranges_start)
1002 {
1003 	const __be32 *prop;
1004 	int range_tuple_cell_count;
1005 	int ranges_len;
1006 	int tuple_len;
1007 	struct device_node *mc_node = dev->of_node;
1008 
1009 	*ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
1010 	if (!(*ranges_start) || !ranges_len) {
1011 		dev_warn(dev,
1012 			 "missing or empty ranges property for device tree node '%pOFn'\n",
1013 			 mc_node);
1014 		return 0;
1015 	}
1016 
1017 	*paddr_cells = of_n_addr_cells(mc_node);
1018 
1019 	prop = of_get_property(mc_node, "#address-cells", NULL);
1020 	if (prop)
1021 		*mc_addr_cells = be32_to_cpup(prop);
1022 	else
1023 		*mc_addr_cells = *paddr_cells;
1024 
1025 	prop = of_get_property(mc_node, "#size-cells", NULL);
1026 	if (prop)
1027 		*mc_size_cells = be32_to_cpup(prop);
1028 	else
1029 		*mc_size_cells = of_n_size_cells(mc_node);
1030 
1031 	range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
1032 				 *mc_size_cells;
1033 
1034 	tuple_len = range_tuple_cell_count * sizeof(__be32);
1035 	if (ranges_len % tuple_len != 0) {
1036 		dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
1037 		return -EINVAL;
1038 	}
1039 
1040 	return ranges_len / tuple_len;
1041 }
1042 
1043 static int get_mc_addr_translation_ranges(struct device *dev,
1044 					  struct fsl_mc_addr_translation_range
1045 						**ranges,
1046 					  u8 *num_ranges)
1047 {
1048 	int ret;
1049 	int paddr_cells;
1050 	int mc_addr_cells;
1051 	int mc_size_cells;
1052 	int i;
1053 	const __be32 *ranges_start;
1054 	const __be32 *cell;
1055 
1056 	ret = parse_mc_ranges(dev,
1057 			      &paddr_cells,
1058 			      &mc_addr_cells,
1059 			      &mc_size_cells,
1060 			      &ranges_start);
1061 	if (ret < 0)
1062 		return ret;
1063 
1064 	*num_ranges = ret;
1065 	if (!ret) {
1066 		/*
1067 		 * Missing or empty ranges property ("ranges;") for the
1068 		 * 'fsl,qoriq-mc' node. In this case, identity mapping
1069 		 * will be used.
1070 		 */
1071 		*ranges = NULL;
1072 		return 0;
1073 	}
1074 
1075 	*ranges = devm_kcalloc(dev, *num_ranges,
1076 			       sizeof(struct fsl_mc_addr_translation_range),
1077 			       GFP_KERNEL);
1078 	if (!(*ranges))
1079 		return -ENOMEM;
1080 
1081 	cell = ranges_start;
1082 	for (i = 0; i < *num_ranges; ++i) {
1083 		struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
1084 
1085 		range->mc_region_type = of_read_number(cell, 1);
1086 		range->start_mc_offset = of_read_number(cell + 1,
1087 							mc_addr_cells - 1);
1088 		cell += mc_addr_cells;
1089 		range->start_phys_addr = of_read_number(cell, paddr_cells);
1090 		cell += paddr_cells;
1091 		range->end_mc_offset = range->start_mc_offset +
1092 				     of_read_number(cell, mc_size_cells);
1093 
1094 		cell += mc_size_cells;
1095 	}
1096 
1097 	return 0;
1098 }
1099 
1100 /*
1101  * fsl_mc_bus_probe - callback invoked when the root MC bus is being
1102  * added
1103  */
1104 static int fsl_mc_bus_probe(struct platform_device *pdev)
1105 {
1106 	struct fsl_mc_obj_desc obj_desc;
1107 	int error;
1108 	struct fsl_mc *mc;
1109 	struct fsl_mc_device *mc_bus_dev = NULL;
1110 	struct fsl_mc_io *mc_io = NULL;
1111 	int container_id;
1112 	phys_addr_t mc_portal_phys_addr;
1113 	u32 mc_portal_size, mc_stream_id;
1114 	struct resource *plat_res;
1115 
1116 	mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
1117 	if (!mc)
1118 		return -ENOMEM;
1119 
1120 	platform_set_drvdata(pdev, mc);
1121 
1122 	plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1123 	if (plat_res) {
1124 		mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
1125 		if (IS_ERR(mc->fsl_mc_regs))
1126 			return PTR_ERR(mc->fsl_mc_regs);
1127 	}
1128 
1129 	if (mc->fsl_mc_regs) {
1130 		if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
1131 			mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
1132 			/*
1133 			 * HW ORs the PL and BMT bit, places the result in bit
1134 			 * 14 of the StreamID and ORs in the ICID. Calculate it
1135 			 * accordingly.
1136 			 */
1137 			mc_stream_id = (mc_stream_id & 0xffff) |
1138 				((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
1139 					BIT(14) : 0);
1140 			error = acpi_dma_configure_id(&pdev->dev,
1141 						      DEV_DMA_COHERENT,
1142 						      &mc_stream_id);
1143 			if (error == -EPROBE_DEFER)
1144 				return error;
1145 			if (error)
1146 				dev_warn(&pdev->dev,
1147 					 "failed to configure dma: %d.\n",
1148 					 error);
1149 		}
1150 
1151 		/*
1152 		 * Some bootloaders pause the MC firmware before booting the
1153 		 * kernel so that MC will not cause faults as soon as the
1154 		 * SMMU probes due to the fact that there's no configuration
1155 		 * in place for MC.
1156 		 * At this point MC should have all its SMMU setup done so make
1157 		 * sure it is resumed.
1158 		 */
1159 		writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
1160 			     (~(GCR1_P1_STOP | GCR1_P2_STOP)),
1161 		       mc->fsl_mc_regs + FSL_MC_GCR1);
1162 	}
1163 
1164 	/*
1165 	 * Get physical address of MC portal for the root DPRC:
1166 	 */
1167 	plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1168 	mc_portal_phys_addr = plat_res->start;
1169 	mc_portal_size = resource_size(plat_res);
1170 	mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
1171 
1172 	error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
1173 				 mc_portal_size, NULL,
1174 				 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
1175 	if (error < 0)
1176 		return error;
1177 
1178 	error = mc_get_version(mc_io, 0, &mc_version);
1179 	if (error != 0) {
1180 		dev_err(&pdev->dev,
1181 			"mc_get_version() failed with error %d\n", error);
1182 		goto error_cleanup_mc_io;
1183 	}
1184 
1185 	dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
1186 		 mc_version.major, mc_version.minor, mc_version.revision);
1187 
1188 	if (dev_of_node(&pdev->dev)) {
1189 		error = get_mc_addr_translation_ranges(&pdev->dev,
1190 						&mc->translation_ranges,
1191 						&mc->num_translation_ranges);
1192 		if (error < 0)
1193 			goto error_cleanup_mc_io;
1194 	}
1195 
1196 	error = dprc_get_container_id(mc_io, 0, &container_id);
1197 	if (error < 0) {
1198 		dev_err(&pdev->dev,
1199 			"dprc_get_container_id() failed: %d\n", error);
1200 		goto error_cleanup_mc_io;
1201 	}
1202 
1203 	memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
1204 	error = dprc_get_api_version(mc_io, 0,
1205 				     &obj_desc.ver_major,
1206 				     &obj_desc.ver_minor);
1207 	if (error < 0)
1208 		goto error_cleanup_mc_io;
1209 
1210 	obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
1211 	strcpy(obj_desc.type, "dprc");
1212 	obj_desc.id = container_id;
1213 	obj_desc.irq_count = 1;
1214 	obj_desc.region_count = 0;
1215 
1216 	error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
1217 	if (error < 0)
1218 		goto error_cleanup_mc_io;
1219 
1220 	mc->root_mc_bus_dev = mc_bus_dev;
1221 	mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
1222 	return 0;
1223 
1224 error_cleanup_mc_io:
1225 	fsl_destroy_mc_io(mc_io);
1226 	return error;
1227 }
1228 
1229 /*
1230  * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1231  * removed
1232  */
1233 static int fsl_mc_bus_remove(struct platform_device *pdev)
1234 {
1235 	struct fsl_mc *mc = platform_get_drvdata(pdev);
1236 	struct fsl_mc_io *mc_io;
1237 
1238 	if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1239 		return -EINVAL;
1240 
1241 	mc_io = mc->root_mc_bus_dev->mc_io;
1242 	fsl_mc_device_remove(mc->root_mc_bus_dev);
1243 	fsl_destroy_mc_io(mc_io);
1244 
1245 	bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
1246 
1247 	if (mc->fsl_mc_regs) {
1248 		/*
1249 		 * Pause the MC firmware so that it doesn't crash in certain
1250 		 * scenarios, such as kexec.
1251 		 */
1252 		writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
1253 		       (GCR1_P1_STOP | GCR1_P2_STOP),
1254 		       mc->fsl_mc_regs + FSL_MC_GCR1);
1255 	}
1256 
1257 	return 0;
1258 }
1259 
1260 static void fsl_mc_bus_shutdown(struct platform_device *pdev)
1261 {
1262 	fsl_mc_bus_remove(pdev);
1263 }
1264 
1265 static const struct of_device_id fsl_mc_bus_match_table[] = {
1266 	{.compatible = "fsl,qoriq-mc",},
1267 	{},
1268 };
1269 
1270 MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1271 
1272 static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1273 	{"NXP0008", 0 },
1274 	{ }
1275 };
1276 MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1277 
1278 static struct platform_driver fsl_mc_bus_driver = {
1279 	.driver = {
1280 		   .name = "fsl_mc_bus",
1281 		   .pm = NULL,
1282 		   .of_match_table = fsl_mc_bus_match_table,
1283 		   .acpi_match_table = fsl_mc_bus_acpi_match_table,
1284 		   },
1285 	.probe = fsl_mc_bus_probe,
1286 	.remove = fsl_mc_bus_remove,
1287 	.shutdown = fsl_mc_bus_shutdown,
1288 };
1289 
1290 static int fsl_mc_bus_notifier(struct notifier_block *nb,
1291 			       unsigned long action, void *data)
1292 {
1293 	struct device *dev = data;
1294 	struct resource *res;
1295 	void __iomem *fsl_mc_regs;
1296 
1297 	if (action != BUS_NOTIFY_ADD_DEVICE)
1298 		return 0;
1299 
1300 	if (!of_match_device(fsl_mc_bus_match_table, dev) &&
1301 	    !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
1302 		return 0;
1303 
1304 	res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
1305 	if (!res)
1306 		return 0;
1307 
1308 	fsl_mc_regs = ioremap(res->start, resource_size(res));
1309 	if (!fsl_mc_regs)
1310 		return 0;
1311 
1312 	/*
1313 	 * Make sure that the MC firmware is paused before the IOMMU setup for
1314 	 * it is done or otherwise the firmware will crash right after the SMMU
1315 	 * gets probed and enabled.
1316 	 */
1317 	writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
1318 	       fsl_mc_regs + FSL_MC_GCR1);
1319 	iounmap(fsl_mc_regs);
1320 
1321 	return 0;
1322 }
1323 
1324 static struct notifier_block fsl_mc_nb = {
1325 	.notifier_call = fsl_mc_bus_notifier,
1326 };
1327 
1328 static int __init fsl_mc_bus_driver_init(void)
1329 {
1330 	int error;
1331 
1332 	error = bus_register(&fsl_mc_bus_type);
1333 	if (error < 0) {
1334 		pr_err("bus type registration failed: %d\n", error);
1335 		goto error_cleanup_cache;
1336 	}
1337 
1338 	error = platform_driver_register(&fsl_mc_bus_driver);
1339 	if (error < 0) {
1340 		pr_err("platform_driver_register() failed: %d\n", error);
1341 		goto error_cleanup_bus;
1342 	}
1343 
1344 	error = dprc_driver_init();
1345 	if (error < 0)
1346 		goto error_cleanup_driver;
1347 
1348 	error = fsl_mc_allocator_driver_init();
1349 	if (error < 0)
1350 		goto error_cleanup_dprc_driver;
1351 
1352 	return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
1353 
1354 error_cleanup_dprc_driver:
1355 	dprc_driver_exit();
1356 
1357 error_cleanup_driver:
1358 	platform_driver_unregister(&fsl_mc_bus_driver);
1359 
1360 error_cleanup_bus:
1361 	bus_unregister(&fsl_mc_bus_type);
1362 
1363 error_cleanup_cache:
1364 	return error;
1365 }
1366 postcore_initcall(fsl_mc_bus_driver_init);
1367