xref: /linux/drivers/bus/fsl-mc/dprc-driver.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 		return error;
526 
527 	return 0;
528 }
529 
530 int enable_dprc_irq(struct fsl_mc_device *mc_dev)
531 {
532 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
533 	int error;
534 
535 	/*
536 	 * Enable all interrupt causes for the interrupt:
537 	 */
538 	error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
539 				  ~0x0u);
540 	if (error < 0) {
541 		dev_err(&mc_dev->dev,
542 			"Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
543 			error);
544 
545 		return error;
546 	}
547 
548 	/*
549 	 * Enable generation of the interrupt:
550 	 */
551 	error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
552 	if (error < 0) {
553 		dev_err(&mc_dev->dev,
554 			"Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
555 			error);
556 
557 		return error;
558 	}
559 
560 	mc_bus->irq_enabled = 1;
561 
562 	return 0;
563 }
564 
565 /*
566  * Setup interrupt for a given DPRC device
567  */
568 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
569 {
570 	int error;
571 
572 	error = fsl_mc_allocate_irqs(mc_dev);
573 	if (error < 0)
574 		return error;
575 
576 	error = disable_dprc_irq(mc_dev);
577 	if (error < 0)
578 		goto error_free_irqs;
579 
580 	error = register_dprc_irq_handler(mc_dev);
581 	if (error < 0)
582 		goto error_free_irqs;
583 
584 	error = enable_dprc_irq(mc_dev);
585 	if (error < 0)
586 		goto error_free_irqs;
587 
588 	return 0;
589 
590 error_free_irqs:
591 	fsl_mc_free_irqs(mc_dev);
592 	return error;
593 }
594 
595 /**
596  * dprc_setup - opens and creates a mc_io for DPRC
597  *
598  * @mc_dev: Pointer to fsl-mc device representing a DPRC
599  *
600  * It opens the physical DPRC in the MC.
601  * It configures the DPRC portal used to communicate with MC
602  */
603 
604 int dprc_setup(struct fsl_mc_device *mc_dev)
605 {
606 	struct device *parent_dev = mc_dev->dev.parent;
607 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
608 	struct irq_domain *mc_msi_domain = NULL;
609 	bool mc_io_created = false;
610 	bool uapi_created = false;
611 	u16 major_ver, minor_ver;
612 	size_t region_size;
613 	int error;
614 
615 	if (!is_fsl_mc_bus_dprc(mc_dev))
616 		return -EINVAL;
617 
618 	if (dev_get_msi_domain(&mc_dev->dev))
619 		return -EINVAL;
620 
621 	if (!mc_dev->mc_io) {
622 		/*
623 		 * This is a child DPRC:
624 		 */
625 		if (!dev_is_fsl_mc(parent_dev))
626 			return -EINVAL;
627 
628 		if (mc_dev->obj_desc.region_count == 0)
629 			return -EINVAL;
630 
631 		region_size = resource_size(mc_dev->regions);
632 
633 		error = fsl_create_mc_io(&mc_dev->dev,
634 					 mc_dev->regions[0].start,
635 					 region_size,
636 					 NULL,
637 					 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
638 					 &mc_dev->mc_io);
639 		if (error < 0)
640 			return error;
641 
642 		mc_io_created = true;
643 	} else {
644 		error = fsl_mc_uapi_create_device_file(mc_bus);
645 		if (error < 0)
646 			return -EPROBE_DEFER;
647 		uapi_created = true;
648 	}
649 
650 	mc_msi_domain = fsl_mc_get_msi_parent(&mc_dev->dev);
651 	if (!mc_msi_domain)
652 		dev_warn(&mc_dev->dev,
653 			 "WARNING: MC bus without interrupt support\n");
654 	else
655 		dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
656 
657 	error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
658 			  &mc_dev->mc_handle);
659 	if (error < 0) {
660 		dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
661 		goto error_cleanup_msi_domain;
662 	}
663 
664 	error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
665 				    &mc_bus->dprc_attr);
666 	if (error < 0) {
667 		dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
668 			error);
669 		goto error_cleanup_open;
670 	}
671 
672 	error = dprc_get_api_version(mc_dev->mc_io, 0,
673 				     &major_ver,
674 				     &minor_ver);
675 	if (error < 0) {
676 		dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
677 			error);
678 		goto error_cleanup_open;
679 	}
680 
681 	if (major_ver < DPRC_MIN_VER_MAJOR) {
682 		dev_err(&mc_dev->dev,
683 			"ERROR: DPRC version %d.%d not supported\n",
684 			major_ver, minor_ver);
685 		error = -ENOTSUPP;
686 		goto error_cleanup_open;
687 	}
688 
689 	return 0;
690 
691 error_cleanup_open:
692 	(void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
693 
694 error_cleanup_msi_domain:
695 	dev_set_msi_domain(&mc_dev->dev, NULL);
696 
697 	if (mc_io_created) {
698 		fsl_destroy_mc_io(mc_dev->mc_io);
699 		mc_dev->mc_io = NULL;
700 	}
701 
702 	if (uapi_created)
703 		fsl_mc_uapi_remove_device_file(mc_bus);
704 
705 	return error;
706 }
707 EXPORT_SYMBOL_GPL(dprc_setup);
708 
709 /**
710  * dprc_probe - callback invoked when a DPRC is being bound to this driver
711  *
712  * @mc_dev: Pointer to fsl-mc device representing a DPRC
713  *
714  * It opens the physical DPRC in the MC.
715  * It scans the DPRC to discover the MC objects contained in it.
716  * It creates the interrupt pool for the MC bus associated with the DPRC.
717  * It configures the interrupts for the DPRC device itself.
718  */
719 static int dprc_probe(struct fsl_mc_device *mc_dev)
720 {
721 	int error;
722 
723 	error = dprc_setup(mc_dev);
724 	if (error < 0)
725 		return error;
726 
727 	/*
728 	 * Discover MC objects in DPRC object:
729 	 */
730 	error = dprc_scan_container(mc_dev, true);
731 	if (error < 0)
732 		goto dprc_cleanup;
733 
734 	/*
735 	 * Configure interrupt for the DPRC object associated with this MC bus:
736 	 */
737 	error = dprc_setup_irq(mc_dev);
738 	if (error < 0)
739 		goto scan_cleanup;
740 
741 	dev_info(&mc_dev->dev, "DPRC device bound to driver");
742 	return 0;
743 
744 scan_cleanup:
745 	device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
746 dprc_cleanup:
747 	dprc_cleanup(mc_dev);
748 	return error;
749 }
750 
751 /*
752  * Tear down interrupt for a given DPRC object
753  */
754 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
755 {
756 	struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
757 
758 	(void)disable_dprc_irq(mc_dev);
759 
760 	devm_free_irq(&mc_dev->dev, irq->virq, &mc_dev->dev);
761 
762 	fsl_mc_free_irqs(mc_dev);
763 }
764 
765 /**
766  * dprc_cleanup - function that cleanups a DPRC
767  *
768  * @mc_dev: Pointer to fsl-mc device representing the DPRC
769  *
770  * It closes the DPRC device in the MC.
771  * It destroys the interrupt pool associated with this MC bus.
772  */
773 
774 int dprc_cleanup(struct fsl_mc_device *mc_dev)
775 {
776 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
777 	int error;
778 
779 	/* this function should be called only for DPRCs, it
780 	 * is an error to call it for regular objects
781 	 */
782 	if (!is_fsl_mc_bus_dprc(mc_dev))
783 		return -EINVAL;
784 
785 	if (dev_get_msi_domain(&mc_dev->dev)) {
786 		fsl_mc_cleanup_irq_pool(mc_dev);
787 		dev_set_msi_domain(&mc_dev->dev, NULL);
788 	}
789 
790 	/* if this step fails we cannot go further with cleanup as there is no way of
791 	 * communicating with the firmware
792 	 */
793 	if (!mc_dev->mc_io) {
794 		dev_err(&mc_dev->dev, "mc_io is NULL, tear down cannot be performed in firmware\n");
795 		return -EINVAL;
796 	}
797 
798 	error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
799 	if (error < 0)
800 		dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
801 
802 	if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
803 		fsl_destroy_mc_io(mc_dev->mc_io);
804 		mc_dev->mc_io = NULL;
805 	} else {
806 		fsl_mc_uapi_remove_device_file(mc_bus);
807 	}
808 
809 	return 0;
810 }
811 EXPORT_SYMBOL_GPL(dprc_cleanup);
812 
813 /**
814  * dprc_remove - callback invoked when a DPRC is being unbound from this driver
815  *
816  * @mc_dev: Pointer to fsl-mc device representing the DPRC
817  *
818  * It removes the DPRC's child objects from Linux (not from the MC) and
819  * closes the DPRC device in the MC.
820  * It tears down the interrupts that were configured for the DPRC device.
821  * It destroys the interrupt pool associated with this MC bus.
822  */
823 static void dprc_remove(struct fsl_mc_device *mc_dev)
824 {
825 	struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
826 
827 	if (!mc_bus->irq_resources) {
828 		dev_err(&mc_dev->dev, "No irq resources, so unbinding the device failed\n");
829 		return;
830 	}
831 
832 	if (dev_get_msi_domain(&mc_dev->dev))
833 		dprc_teardown_irq(mc_dev);
834 
835 	device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
836 
837 	dprc_cleanup(mc_dev);
838 
839 	dev_info(&mc_dev->dev, "DPRC device unbound from driver");
840 }
841 
842 static const struct fsl_mc_device_id match_id_table[] = {
843 	{
844 	 .vendor = FSL_MC_VENDOR_FREESCALE,
845 	 .obj_type = "dprc"},
846 	{.vendor = 0x0},
847 };
848 
849 static struct fsl_mc_driver dprc_driver = {
850 	.driver = {
851 		   .name = FSL_MC_DPRC_DRIVER_NAME,
852 		   .owner = THIS_MODULE,
853 		   .pm = NULL,
854 		   },
855 	.match_id_table = match_id_table,
856 	.probe = dprc_probe,
857 	.remove = dprc_remove,
858 };
859 
860 int __init dprc_driver_init(void)
861 {
862 	return fsl_mc_driver_register(&dprc_driver);
863 }
864 
865 void dprc_driver_exit(void)
866 {
867 	fsl_mc_driver_unregister(&dprc_driver);
868 }
869