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