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