xref: /linux/drivers/bus/fsl-mc/dprc-driver.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Freescale data path resource container (DPRC) 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 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/fsl/mc.h>
15 
16 #include "fsl-mc-private.h"
17 
18 #define FSL_MC_DPRC_DRIVER_NAME    "fsl_mc_dprc"
19 
20 struct fsl_mc_child_objs {
21 	int child_count;
22 	struct fsl_mc_obj_desc *child_array;
23 };
24 
25 static bool fsl_mc_device_match(const struct fsl_mc_device *mc_dev,
26 				const struct fsl_mc_obj_desc *obj_desc)
27 {
28 	return mc_dev->obj_desc.id == obj_desc->id &&
29 	       strcmp(mc_dev->obj_desc.type, obj_desc->type) == 0;
30 }
31 
32 static bool fsl_mc_obj_desc_is_allocatable(struct fsl_mc_obj_desc *obj)
33 {
34 	if (strcmp(obj->type, "dpmcp") == 0 ||
35 	    strcmp(obj->type, "dpcon") == 0 ||
36 	    strcmp(obj->type, "dpbp") == 0)
37 		return true;
38 	else
39 		return false;
40 }
41 
42 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
43 {
44 	int i;
45 	struct fsl_mc_child_objs *objs;
46 	struct fsl_mc_device *mc_dev;
47 
48 	if (!dev_is_fsl_mc(dev))
49 		return 0;
50 
51 	mc_dev = to_fsl_mc_device(dev);
52 	objs = data;
53 
54 	for (i = 0; i < objs->child_count; i++) {
55 		struct fsl_mc_obj_desc *obj_desc = &objs->child_array[i];
56 
57 		if (strlen(obj_desc->type) != 0 &&
58 		    fsl_mc_device_match(mc_dev, obj_desc))
59 			break;
60 	}
61 
62 	if (i == objs->child_count)
63 		fsl_mc_device_remove(mc_dev);
64 
65 	return 0;
66 }
67 
68 static int __fsl_mc_device_remove(struct device *dev, void *data)
69 {
70 	if (!dev_is_fsl_mc(dev))
71 		return 0;
72 
73 	fsl_mc_device_remove(to_fsl_mc_device(dev));
74 	return 0;
75 }
76 
77 /**
78  * dprc_remove_devices - Removes devices for objects removed from a DPRC
79  *
80  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
81  * @obj_desc_array: array of object descriptors for child objects currently
82  * present in the DPRC in the MC.
83  * @num_child_objects_in_mc: number of entries in obj_desc_array
84  *
85  * Synchronizes the state of the Linux bus driver with the actual state of
86  * the MC by removing devices that represent MC objects that have
87  * been dynamically removed in the physical DPRC.
88  */
89 void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
90 			 struct fsl_mc_obj_desc *obj_desc_array,
91 			 int num_child_objects_in_mc)
92 {
93 	if (num_child_objects_in_mc != 0) {
94 		/*
95 		 * Remove child objects that are in the DPRC in Linux,
96 		 * but not in the MC:
97 		 */
98 		struct fsl_mc_child_objs objs;
99 
100 		objs.child_count = num_child_objects_in_mc;
101 		objs.child_array = obj_desc_array;
102 		device_for_each_child(&mc_bus_dev->dev, &objs,
103 				      __fsl_mc_device_remove_if_not_in_mc);
104 	} else {
105 		/*
106 		 * There are no child objects for this DPRC in the MC.
107 		 * So, remove all the child devices from Linux:
108 		 */
109 		device_for_each_child(&mc_bus_dev->dev, NULL,
110 				      __fsl_mc_device_remove);
111 	}
112 }
113 EXPORT_SYMBOL_GPL(dprc_remove_devices);
114 
115 static int __fsl_mc_device_match(struct device *dev, const void *data)
116 {
117 	const struct fsl_mc_obj_desc *obj_desc = data;
118 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
119 
120 	return fsl_mc_device_match(mc_dev, obj_desc);
121 }
122 
123 struct fsl_mc_device *fsl_mc_device_lookup(struct fsl_mc_obj_desc *obj_desc,
124 					   struct fsl_mc_device *mc_bus_dev)
125 {
126 	struct device *dev;
127 
128 	dev = device_find_child(&mc_bus_dev->dev, obj_desc,
129 				__fsl_mc_device_match);
130 
131 	return dev ? to_fsl_mc_device(dev) : NULL;
132 }
133 
134 /**
135  * check_plugged_state_change - Check change in an MC object's plugged state
136  *
137  * @mc_dev: pointer to the fsl-mc device for a given MC object
138  * @obj_desc: pointer to the MC object's descriptor in the MC
139  *
140  * If the plugged state has changed from unplugged to plugged, the fsl-mc
141  * device is bound to the corresponding device driver.
142  * If the plugged state has changed from plugged to unplugged, the fsl-mc
143  * device is unbound from the corresponding device driver.
144  */
145 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
146 				       struct fsl_mc_obj_desc *obj_desc)
147 {
148 	int error;
149 	u32 plugged_flag_at_mc =
150 			obj_desc->state & FSL_MC_OBJ_STATE_PLUGGED;
151 
152 	if (plugged_flag_at_mc !=
153 	    (mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED)) {
154 		if (plugged_flag_at_mc) {
155 			mc_dev->obj_desc.state |= FSL_MC_OBJ_STATE_PLUGGED;
156 			error = device_attach(&mc_dev->dev);
157 			if (error < 0) {
158 				dev_err(&mc_dev->dev,
159 					"device_attach() failed: %d\n",
160 					error);
161 			}
162 		} else {
163 			mc_dev->obj_desc.state &= ~FSL_MC_OBJ_STATE_PLUGGED;
164 			device_release_driver(&mc_dev->dev);
165 		}
166 	}
167 }
168 
169 static void fsl_mc_obj_device_add(struct fsl_mc_device *mc_bus_dev,
170 				  struct fsl_mc_obj_desc *obj_desc)
171 {
172 	int error;
173 	struct fsl_mc_device *child_dev;
174 
175 	/*
176 	 * Check if device is already known to Linux:
177 	 */
178 	child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
179 	if (child_dev) {
180 		check_plugged_state_change(child_dev, obj_desc);
181 		put_device(&child_dev->dev);
182 	} else {
183 		error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
184 					  &child_dev);
185 		if (error < 0)
186 			return;
187 	}
188 }
189 
190 /**
191  * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
192  *
193  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
194  * @obj_desc_array: array of device descriptors for child devices currently
195  * present in the physical DPRC.
196  * @num_child_objects_in_mc: number of entries in obj_desc_array
197  *
198  * Synchronizes the state of the Linux bus driver with the actual
199  * state of the MC by adding objects that have been newly discovered
200  * in the physical DPRC.
201  */
202 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
203 				 struct fsl_mc_obj_desc *obj_desc_array,
204 				 int num_child_objects_in_mc)
205 {
206 	int i;
207 
208 	/* probe the allocable objects first */
209 	for (i = 0; i < num_child_objects_in_mc; i++) {
210 		struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
211 
212 		if (strlen(obj_desc->type) > 0 &&
213 		    fsl_mc_obj_desc_is_allocatable(obj_desc))
214 			fsl_mc_obj_device_add(mc_bus_dev, obj_desc);
215 	}
216 
217 	for (i = 0; i < num_child_objects_in_mc; i++) {
218 		struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
219 
220 		if (strlen(obj_desc->type) > 0 &&
221 		    !fsl_mc_obj_desc_is_allocatable(obj_desc))
222 			fsl_mc_obj_device_add(mc_bus_dev, obj_desc);
223 	}
224 }
225 
226 /**
227  * dprc_scan_objects - Discover objects in a DPRC
228  *
229  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
230  * @alloc_interrupts: if true the function allocates the interrupt pool,
231  * otherwise the interrupt allocation is delayed
232  *
233  * Detects objects added and removed from a DPRC and synchronizes the
234  * state of the Linux bus driver, MC by adding and removing
235  * devices accordingly.
236  * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
237  * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
238  * All allocatable devices needed to be probed before all non-allocatable
239  * devices, to ensure that device drivers for non-allocatable
240  * devices can allocate any type of allocatable devices.
241  * That is, we need to ensure that the corresponding resource pools are
242  * populated before they can get allocation requests from probe callbacks
243  * of the device drivers for the non-allocatable devices.
244  */
245 int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
246 		      bool alloc_interrupts)
247 {
248 	int num_child_objects;
249 	int dprc_get_obj_failures;
250 	int error;
251 	unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
252 	struct fsl_mc_obj_desc *child_obj_desc_array = NULL;
253 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
254 
255 	error = dprc_get_obj_count(mc_bus_dev->mc_io,
256 				   0,
257 				   mc_bus_dev->mc_handle,
258 				   &num_child_objects);
259 	if (error < 0) {
260 		dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
261 			error);
262 		return error;
263 	}
264 
265 	if (num_child_objects != 0) {
266 		int i;
267 
268 		child_obj_desc_array =
269 		    devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
270 				       sizeof(*child_obj_desc_array),
271 				       GFP_KERNEL);
272 		if (!child_obj_desc_array)
273 			return -ENOMEM;
274 
275 		/*
276 		 * Discover objects currently present in the physical DPRC:
277 		 */
278 		dprc_get_obj_failures = 0;
279 		for (i = 0; i < num_child_objects; i++) {
280 			struct fsl_mc_obj_desc *obj_desc =
281 			    &child_obj_desc_array[i];
282 
283 			error = dprc_get_obj(mc_bus_dev->mc_io,
284 					     0,
285 					     mc_bus_dev->mc_handle,
286 					     i, obj_desc);
287 			if (error < 0) {
288 				dev_err(&mc_bus_dev->dev,
289 					"dprc_get_obj(i=%d) failed: %d\n",
290 					i, error);
291 				/*
292 				 * Mark the obj entry as "invalid", by using the
293 				 * empty string as obj type:
294 				 */
295 				obj_desc->type[0] = '\0';
296 				obj_desc->id = error;
297 				dprc_get_obj_failures++;
298 				continue;
299 			}
300 
301 			/*
302 			 * add a quirk for all versions of dpsec < 4.0...none
303 			 * are coherent regardless of what the MC reports.
304 			 */
305 			if ((strcmp(obj_desc->type, "dpseci") == 0) &&
306 			    (obj_desc->ver_major < 4))
307 				obj_desc->flags |=
308 					FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY;
309 
310 			irq_count += obj_desc->irq_count;
311 			dev_dbg(&mc_bus_dev->dev,
312 				"Discovered object: type %s, id %d\n",
313 				obj_desc->type, obj_desc->id);
314 		}
315 
316 		if (dprc_get_obj_failures != 0) {
317 			dev_err(&mc_bus_dev->dev,
318 				"%d out of %d devices could not be retrieved\n",
319 				dprc_get_obj_failures, num_child_objects);
320 		}
321 	}
322 
323 	/*
324 	 * Allocate IRQ's before binding the scanned devices with their
325 	 * respective drivers.
326 	 */
327 	if (dev_get_msi_domain(&mc_bus_dev->dev)) {
328 		if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
329 			dev_warn(&mc_bus_dev->dev,
330 				 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
331 				 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
332 		}
333 
334 		if (alloc_interrupts && !mc_bus->irq_resources) {
335 			error = fsl_mc_populate_irq_pool(mc_bus_dev,
336 					 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
337 			if (error < 0)
338 				return error;
339 		}
340 	}
341 
342 	dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
343 			    num_child_objects);
344 
345 	dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
346 			     num_child_objects);
347 
348 	if (child_obj_desc_array)
349 		devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
350 
351 	return 0;
352 }
353 
354 /**
355  * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
356  *
357  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
358  * @alloc_interrupts: if true the function allocates the interrupt pool,
359  *                    otherwise the interrupt allocation is delayed
360  * Scans the physical DPRC and synchronizes the state of the Linux
361  * bus driver with the actual state of the MC by adding and removing
362  * devices as appropriate.
363  */
364 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev,
365 			bool alloc_interrupts)
366 {
367 	int error = 0;
368 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
369 
370 	fsl_mc_init_all_resource_pools(mc_bus_dev);
371 
372 	/*
373 	 * Discover objects in the DPRC:
374 	 */
375 	mutex_lock(&mc_bus->scan_mutex);
376 	error = dprc_scan_objects(mc_bus_dev, alloc_interrupts);
377 	mutex_unlock(&mc_bus->scan_mutex);
378 
379 	return error;
380 }
381 EXPORT_SYMBOL_GPL(dprc_scan_container);
382 
383 /**
384  * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
385  *
386  * @irq_num: IRQ number of the interrupt being handled
387  * @arg: Pointer to device structure
388  */
389 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
390 {
391 	int error;
392 	u32 status;
393 	struct device *dev = arg;
394 	struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
395 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
396 	struct fsl_mc_io *mc_io = mc_dev->mc_io;
397 	int irq = mc_dev->irqs[0]->virq;
398 
399 	dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
400 		irq_num, smp_processor_id());
401 
402 	if (!(mc_dev->flags & FSL_MC_IS_DPRC))
403 		return IRQ_HANDLED;
404 
405 	mutex_lock(&mc_bus->scan_mutex);
406 	if (irq != (u32)irq_num)
407 		goto out;
408 
409 	status = 0;
410 	error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
411 				    &status);
412 	if (error < 0) {
413 		dev_err(dev,
414 			"dprc_get_irq_status() failed: %d\n", error);
415 		goto out;
416 	}
417 
418 	error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
419 				      status);
420 	if (error < 0) {
421 		dev_err(dev,
422 			"dprc_clear_irq_status() failed: %d\n", error);
423 		goto out;
424 	}
425 
426 	if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
427 		      DPRC_IRQ_EVENT_OBJ_REMOVED |
428 		      DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
429 		      DPRC_IRQ_EVENT_OBJ_DESTROYED |
430 		      DPRC_IRQ_EVENT_OBJ_CREATED)) {
431 
432 		error = dprc_scan_objects(mc_dev, true);
433 		if (error < 0) {
434 			/*
435 			 * If the error is -ENXIO, we ignore it, as it indicates
436 			 * that the object scan was aborted, as we detected that
437 			 * an object was removed from the DPRC in the MC, while
438 			 * we were scanning the DPRC.
439 			 */
440 			if (error != -ENXIO) {
441 				dev_err(dev, "dprc_scan_objects() failed: %d\n",
442 					error);
443 			}
444 
445 			goto out;
446 		}
447 	}
448 
449 out:
450 	mutex_unlock(&mc_bus->scan_mutex);
451 	return IRQ_HANDLED;
452 }
453 
454 /*
455  * Disable and clear interrupt for a given DPRC object
456  */
457 int disable_dprc_irq(struct fsl_mc_device *mc_dev)
458 {
459 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
460 	int error;
461 	struct fsl_mc_io *mc_io = mc_dev->mc_io;
462 
463 	/*
464 	 * Disable generation of interrupt, while we configure it:
465 	 */
466 	error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
467 	if (error < 0) {
468 		dev_err(&mc_dev->dev,
469 			"Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
470 			error);
471 		return error;
472 	}
473 
474 	/*
475 	 * Disable all interrupt causes for the interrupt:
476 	 */
477 	error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
478 	if (error < 0) {
479 		dev_err(&mc_dev->dev,
480 			"Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
481 			error);
482 		return error;
483 	}
484 
485 	/*
486 	 * Clear any leftover interrupts:
487 	 */
488 	error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
489 	if (error < 0) {
490 		dev_err(&mc_dev->dev,
491 			"Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
492 			error);
493 		return error;
494 	}
495 
496 	mc_bus->irq_enabled = 0;
497 
498 	return 0;
499 }
500 
501 int get_dprc_irq_state(struct fsl_mc_device *mc_dev)
502 {
503 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
504 
505 	return mc_bus->irq_enabled;
506 }
507 
508 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
509 {
510 	int error;
511 	struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
512 
513 	/*
514 	 * NOTE: devm_request_threaded_irq() invokes the device-specific
515 	 * function that programs the MSI physically in the device
516 	 */
517 	error = devm_request_threaded_irq(&mc_dev->dev,
518 					  irq->virq,
519 					  NULL,
520 					  dprc_irq0_handler_thread,
521 					  IRQF_NO_SUSPEND | IRQF_ONESHOT,
522 					  dev_name(&mc_dev->dev),
523 					  &mc_dev->dev);
524 	if (error < 0) {
525 		dev_err(&mc_dev->dev,
526 			"devm_request_threaded_irq() failed: %d\n",
527 			error);
528 		return error;
529 	}
530 
531 	return 0;
532 }
533 
534 int enable_dprc_irq(struct fsl_mc_device *mc_dev)
535 {
536 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
537 	int error;
538 
539 	/*
540 	 * Enable all interrupt causes for the interrupt:
541 	 */
542 	error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
543 				  ~0x0u);
544 	if (error < 0) {
545 		dev_err(&mc_dev->dev,
546 			"Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
547 			error);
548 
549 		return error;
550 	}
551 
552 	/*
553 	 * Enable generation of the interrupt:
554 	 */
555 	error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
556 	if (error < 0) {
557 		dev_err(&mc_dev->dev,
558 			"Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
559 			error);
560 
561 		return error;
562 	}
563 
564 	mc_bus->irq_enabled = 1;
565 
566 	return 0;
567 }
568 
569 /*
570  * Setup interrupt for a given DPRC device
571  */
572 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
573 {
574 	int error;
575 
576 	error = fsl_mc_allocate_irqs(mc_dev);
577 	if (error < 0)
578 		return error;
579 
580 	error = disable_dprc_irq(mc_dev);
581 	if (error < 0)
582 		goto error_free_irqs;
583 
584 	error = register_dprc_irq_handler(mc_dev);
585 	if (error < 0)
586 		goto error_free_irqs;
587 
588 	error = enable_dprc_irq(mc_dev);
589 	if (error < 0)
590 		goto error_free_irqs;
591 
592 	return 0;
593 
594 error_free_irqs:
595 	fsl_mc_free_irqs(mc_dev);
596 	return error;
597 }
598 
599 /**
600  * dprc_setup - opens and creates a mc_io for DPRC
601  *
602  * @mc_dev: Pointer to fsl-mc device representing a DPRC
603  *
604  * It opens the physical DPRC in the MC.
605  * It configures the DPRC portal used to communicate with MC
606  */
607 
608 int dprc_setup(struct fsl_mc_device *mc_dev)
609 {
610 	struct device *parent_dev = mc_dev->dev.parent;
611 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
612 	struct irq_domain *mc_msi_domain = NULL;
613 	bool mc_io_created = false;
614 	bool uapi_created = false;
615 	u16 major_ver, minor_ver;
616 	size_t region_size;
617 	int error;
618 
619 	if (!is_fsl_mc_bus_dprc(mc_dev))
620 		return -EINVAL;
621 
622 	if (dev_get_msi_domain(&mc_dev->dev))
623 		return -EINVAL;
624 
625 	if (!mc_dev->mc_io) {
626 		/*
627 		 * This is a child DPRC:
628 		 */
629 		if (!dev_is_fsl_mc(parent_dev))
630 			return -EINVAL;
631 
632 		if (mc_dev->obj_desc.region_count == 0)
633 			return -EINVAL;
634 
635 		region_size = resource_size(mc_dev->regions);
636 
637 		error = fsl_create_mc_io(&mc_dev->dev,
638 					 mc_dev->regions[0].start,
639 					 region_size,
640 					 NULL,
641 					 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
642 					 &mc_dev->mc_io);
643 		if (error < 0)
644 			return error;
645 
646 		mc_io_created = true;
647 	} else {
648 		error = fsl_mc_uapi_create_device_file(mc_bus);
649 		if (error < 0)
650 			return -EPROBE_DEFER;
651 		uapi_created = true;
652 	}
653 
654 	mc_msi_domain = fsl_mc_get_msi_parent(&mc_dev->dev);
655 	if (!mc_msi_domain)
656 		dev_warn(&mc_dev->dev,
657 			 "WARNING: MC bus without interrupt support\n");
658 	else
659 		dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
660 
661 	error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
662 			  &mc_dev->mc_handle);
663 	if (error < 0) {
664 		dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
665 		goto error_cleanup_msi_domain;
666 	}
667 
668 	error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
669 				    &mc_bus->dprc_attr);
670 	if (error < 0) {
671 		dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
672 			error);
673 		goto error_cleanup_open;
674 	}
675 
676 	error = dprc_get_api_version(mc_dev->mc_io, 0,
677 				     &major_ver,
678 				     &minor_ver);
679 	if (error < 0) {
680 		dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
681 			error);
682 		goto error_cleanup_open;
683 	}
684 
685 	if (major_ver < DPRC_MIN_VER_MAJOR) {
686 		dev_err(&mc_dev->dev,
687 			"ERROR: DPRC version %d.%d not supported\n",
688 			major_ver, minor_ver);
689 		error = -ENOTSUPP;
690 		goto error_cleanup_open;
691 	}
692 
693 	return 0;
694 
695 error_cleanup_open:
696 	(void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
697 
698 error_cleanup_msi_domain:
699 	dev_set_msi_domain(&mc_dev->dev, NULL);
700 
701 	if (mc_io_created) {
702 		fsl_destroy_mc_io(mc_dev->mc_io);
703 		mc_dev->mc_io = NULL;
704 	}
705 
706 	if (uapi_created)
707 		fsl_mc_uapi_remove_device_file(mc_bus);
708 
709 	return error;
710 }
711 EXPORT_SYMBOL_GPL(dprc_setup);
712 
713 /**
714  * dprc_probe - callback invoked when a DPRC is being bound to this driver
715  *
716  * @mc_dev: Pointer to fsl-mc device representing a DPRC
717  *
718  * It opens the physical DPRC in the MC.
719  * It scans the DPRC to discover the MC objects contained in it.
720  * It creates the interrupt pool for the MC bus associated with the DPRC.
721  * It configures the interrupts for the DPRC device itself.
722  */
723 static int dprc_probe(struct fsl_mc_device *mc_dev)
724 {
725 	int error;
726 
727 	error = dprc_setup(mc_dev);
728 	if (error < 0)
729 		return error;
730 
731 	/*
732 	 * Discover MC objects in DPRC object:
733 	 */
734 	error = dprc_scan_container(mc_dev, true);
735 	if (error < 0)
736 		goto dprc_cleanup;
737 
738 	/*
739 	 * Configure interrupt for the DPRC object associated with this MC bus:
740 	 */
741 	error = dprc_setup_irq(mc_dev);
742 	if (error < 0)
743 		goto scan_cleanup;
744 
745 	dev_info(&mc_dev->dev, "DPRC device bound to driver");
746 	return 0;
747 
748 scan_cleanup:
749 	device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
750 dprc_cleanup:
751 	dprc_cleanup(mc_dev);
752 	return error;
753 }
754 
755 /*
756  * Tear down interrupt for a given DPRC object
757  */
758 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
759 {
760 	struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
761 
762 	(void)disable_dprc_irq(mc_dev);
763 
764 	devm_free_irq(&mc_dev->dev, irq->virq, &mc_dev->dev);
765 
766 	fsl_mc_free_irqs(mc_dev);
767 }
768 
769 /**
770  * dprc_cleanup - function that cleanups a DPRC
771  *
772  * @mc_dev: Pointer to fsl-mc device representing the DPRC
773  *
774  * It closes the DPRC device in the MC.
775  * It destroys the interrupt pool associated with this MC bus.
776  */
777 
778 int dprc_cleanup(struct fsl_mc_device *mc_dev)
779 {
780 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
781 	int error;
782 
783 	/* this function should be called only for DPRCs, it
784 	 * is an error to call it for regular objects
785 	 */
786 	if (!is_fsl_mc_bus_dprc(mc_dev))
787 		return -EINVAL;
788 
789 	if (dev_get_msi_domain(&mc_dev->dev)) {
790 		fsl_mc_cleanup_irq_pool(mc_dev);
791 		dev_set_msi_domain(&mc_dev->dev, NULL);
792 	}
793 
794 	/* if this step fails we cannot go further with cleanup as there is no way of
795 	 * communicating with the firmware
796 	 */
797 	if (!mc_dev->mc_io) {
798 		dev_err(&mc_dev->dev, "mc_io is NULL, tear down cannot be performed in firmware\n");
799 		return -EINVAL;
800 	}
801 
802 	error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
803 	if (error < 0)
804 		dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
805 
806 	if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
807 		fsl_destroy_mc_io(mc_dev->mc_io);
808 		mc_dev->mc_io = NULL;
809 	} else {
810 		fsl_mc_uapi_remove_device_file(mc_bus);
811 	}
812 
813 	return 0;
814 }
815 EXPORT_SYMBOL_GPL(dprc_cleanup);
816 
817 /**
818  * dprc_remove - callback invoked when a DPRC is being unbound from this driver
819  *
820  * @mc_dev: Pointer to fsl-mc device representing the DPRC
821  *
822  * It removes the DPRC's child objects from Linux (not from the MC) and
823  * closes the DPRC device in the MC.
824  * It tears down the interrupts that were configured for the DPRC device.
825  * It destroys the interrupt pool associated with this MC bus.
826  */
827 static void dprc_remove(struct fsl_mc_device *mc_dev)
828 {
829 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
830 
831 	if (!mc_bus->irq_resources) {
832 		dev_err(&mc_dev->dev, "No irq resources, so unbinding the device failed\n");
833 		return;
834 	}
835 
836 	if (dev_get_msi_domain(&mc_dev->dev))
837 		dprc_teardown_irq(mc_dev);
838 
839 	device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
840 
841 	dprc_cleanup(mc_dev);
842 
843 	dev_info(&mc_dev->dev, "DPRC device unbound from driver");
844 }
845 
846 static const struct fsl_mc_device_id match_id_table[] = {
847 	{
848 	 .vendor = FSL_MC_VENDOR_FREESCALE,
849 	 .obj_type = "dprc"},
850 	{.vendor = 0x0},
851 };
852 
853 static struct fsl_mc_driver dprc_driver = {
854 	.driver = {
855 		   .name = FSL_MC_DPRC_DRIVER_NAME,
856 		   .owner = THIS_MODULE,
857 		   .pm = NULL,
858 		   },
859 	.match_id_table = match_id_table,
860 	.probe = dprc_probe,
861 	.remove = dprc_remove,
862 };
863 
864 int __init dprc_driver_init(void)
865 {
866 	return fsl_mc_driver_register(&dprc_driver);
867 }
868 
869 void dprc_driver_exit(void)
870 {
871 	fsl_mc_driver_unregister(&dprc_driver);
872 }
873