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