1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale data path resource container (DPRC) driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6 * Author: German Rivera <German.Rivera@freescale.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/fsl/mc.h>
16 #include "fsl-mc-private.h"
18 #define FSL_MC_DPRC_DRIVER_NAME "fsl_mc_dprc"
20 struct fsl_mc_child_objs
{
22 struct fsl_mc_obj_desc
*child_array
;
25 static bool fsl_mc_device_match(struct fsl_mc_device
*mc_dev
,
26 struct fsl_mc_obj_desc
*obj_desc
)
28 return mc_dev
->obj_desc
.id
== obj_desc
->id
&&
29 strcmp(mc_dev
->obj_desc
.type
, obj_desc
->type
) == 0;
33 static int __fsl_mc_device_remove_if_not_in_mc(struct device
*dev
, void *data
)
36 struct fsl_mc_child_objs
*objs
;
37 struct fsl_mc_device
*mc_dev
;
39 mc_dev
= to_fsl_mc_device(dev
);
42 for (i
= 0; i
< objs
->child_count
; i
++) {
43 struct fsl_mc_obj_desc
*obj_desc
= &objs
->child_array
[i
];
45 if (strlen(obj_desc
->type
) != 0 &&
46 fsl_mc_device_match(mc_dev
, obj_desc
))
50 if (i
== objs
->child_count
)
51 fsl_mc_device_remove(mc_dev
);
56 static int __fsl_mc_device_remove(struct device
*dev
, void *data
)
58 fsl_mc_device_remove(to_fsl_mc_device(dev
));
63 * dprc_remove_devices - Removes devices for objects removed from a DPRC
65 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
66 * @obj_desc_array: array of object descriptors for child objects currently
67 * present in the DPRC in the MC.
68 * @num_child_objects_in_mc: number of entries in obj_desc_array
70 * Synchronizes the state of the Linux bus driver with the actual state of
71 * the MC by removing devices that represent MC objects that have
72 * been dynamically removed in the physical DPRC.
74 static void dprc_remove_devices(struct fsl_mc_device
*mc_bus_dev
,
75 struct fsl_mc_obj_desc
*obj_desc_array
,
76 int num_child_objects_in_mc
)
78 if (num_child_objects_in_mc
!= 0) {
80 * Remove child objects that are in the DPRC in Linux,
83 struct fsl_mc_child_objs objs
;
85 objs
.child_count
= num_child_objects_in_mc
;
86 objs
.child_array
= obj_desc_array
;
87 device_for_each_child(&mc_bus_dev
->dev
, &objs
,
88 __fsl_mc_device_remove_if_not_in_mc
);
91 * There are no child objects for this DPRC in the MC.
92 * So, remove all the child devices from Linux:
94 device_for_each_child(&mc_bus_dev
->dev
, NULL
,
95 __fsl_mc_device_remove
);
99 static int __fsl_mc_device_match(struct device
*dev
, void *data
)
101 struct fsl_mc_obj_desc
*obj_desc
= data
;
102 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
104 return fsl_mc_device_match(mc_dev
, obj_desc
);
107 static struct fsl_mc_device
*fsl_mc_device_lookup(struct fsl_mc_obj_desc
114 dev
= device_find_child(&mc_bus_dev
->dev
, obj_desc
,
115 __fsl_mc_device_match
);
117 return dev
? to_fsl_mc_device(dev
) : NULL
;
121 * check_plugged_state_change - Check change in an MC object's plugged state
123 * @mc_dev: pointer to the fsl-mc device for a given MC object
124 * @obj_desc: pointer to the MC object's descriptor in the MC
126 * If the plugged state has changed from unplugged to plugged, the fsl-mc
127 * device is bound to the corresponding device driver.
128 * If the plugged state has changed from plugged to unplugged, the fsl-mc
129 * device is unbound from the corresponding device driver.
131 static void check_plugged_state_change(struct fsl_mc_device
*mc_dev
,
132 struct fsl_mc_obj_desc
*obj_desc
)
135 u32 plugged_flag_at_mc
=
136 obj_desc
->state
& FSL_MC_OBJ_STATE_PLUGGED
;
138 if (plugged_flag_at_mc
!=
139 (mc_dev
->obj_desc
.state
& FSL_MC_OBJ_STATE_PLUGGED
)) {
140 if (plugged_flag_at_mc
) {
141 mc_dev
->obj_desc
.state
|= FSL_MC_OBJ_STATE_PLUGGED
;
142 error
= device_attach(&mc_dev
->dev
);
144 dev_err(&mc_dev
->dev
,
145 "device_attach() failed: %d\n",
149 mc_dev
->obj_desc
.state
&= ~FSL_MC_OBJ_STATE_PLUGGED
;
150 device_release_driver(&mc_dev
->dev
);
156 * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
158 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
159 * @obj_desc_array: array of device descriptors for child devices currently
160 * present in the physical DPRC.
161 * @num_child_objects_in_mc: number of entries in obj_desc_array
163 * Synchronizes the state of the Linux bus driver with the actual
164 * state of the MC by adding objects that have been newly discovered
165 * in the physical DPRC.
167 static void dprc_add_new_devices(struct fsl_mc_device
*mc_bus_dev
,
168 struct fsl_mc_obj_desc
*obj_desc_array
,
169 int num_child_objects_in_mc
)
174 for (i
= 0; i
< num_child_objects_in_mc
; i
++) {
175 struct fsl_mc_device
*child_dev
;
176 struct fsl_mc_obj_desc
*obj_desc
= &obj_desc_array
[i
];
178 if (strlen(obj_desc
->type
) == 0)
182 * Check if device is already known to Linux:
184 child_dev
= fsl_mc_device_lookup(obj_desc
, mc_bus_dev
);
186 check_plugged_state_change(child_dev
, obj_desc
);
187 put_device(&child_dev
->dev
);
191 error
= fsl_mc_device_add(obj_desc
, NULL
, &mc_bus_dev
->dev
,
199 * dprc_scan_objects - Discover objects in a DPRC
201 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
202 * @total_irq_count: If argument is provided the function populates the
203 * total number of IRQs created by objects in the DPRC.
205 * Detects objects added and removed from a DPRC and synchronizes the
206 * state of the Linux bus driver, MC by adding and removing
207 * devices accordingly.
208 * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
209 * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
210 * All allocatable devices needed to be probed before all non-allocatable
211 * devices, to ensure that device drivers for non-allocatable
212 * devices can allocate any type of allocatable devices.
213 * That is, we need to ensure that the corresponding resource pools are
214 * populated before they can get allocation requests from probe callbacks
215 * of the device drivers for the non-allocatable devices.
217 static int dprc_scan_objects(struct fsl_mc_device
*mc_bus_dev
,
218 unsigned int *total_irq_count
)
220 int num_child_objects
;
221 int dprc_get_obj_failures
;
223 unsigned int irq_count
= mc_bus_dev
->obj_desc
.irq_count
;
224 struct fsl_mc_obj_desc
*child_obj_desc_array
= NULL
;
225 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
227 error
= dprc_get_obj_count(mc_bus_dev
->mc_io
,
229 mc_bus_dev
->mc_handle
,
232 dev_err(&mc_bus_dev
->dev
, "dprc_get_obj_count() failed: %d\n",
237 if (num_child_objects
!= 0) {
240 child_obj_desc_array
=
241 devm_kmalloc_array(&mc_bus_dev
->dev
, num_child_objects
,
242 sizeof(*child_obj_desc_array
),
244 if (!child_obj_desc_array
)
248 * Discover objects currently present in the physical DPRC:
250 dprc_get_obj_failures
= 0;
251 for (i
= 0; i
< num_child_objects
; i
++) {
252 struct fsl_mc_obj_desc
*obj_desc
=
253 &child_obj_desc_array
[i
];
255 error
= dprc_get_obj(mc_bus_dev
->mc_io
,
257 mc_bus_dev
->mc_handle
,
260 dev_err(&mc_bus_dev
->dev
,
261 "dprc_get_obj(i=%d) failed: %d\n",
264 * Mark the obj entry as "invalid", by using the
265 * empty string as obj type:
267 obj_desc
->type
[0] = '\0';
268 obj_desc
->id
= error
;
269 dprc_get_obj_failures
++;
274 * add a quirk for all versions of dpsec < 4.0...none
275 * are coherent regardless of what the MC reports.
277 if ((strcmp(obj_desc
->type
, "dpseci") == 0) &&
278 (obj_desc
->ver_major
< 4))
280 FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY
;
282 irq_count
+= obj_desc
->irq_count
;
283 dev_dbg(&mc_bus_dev
->dev
,
284 "Discovered object: type %s, id %d\n",
285 obj_desc
->type
, obj_desc
->id
);
288 if (dprc_get_obj_failures
!= 0) {
289 dev_err(&mc_bus_dev
->dev
,
290 "%d out of %d devices could not be retrieved\n",
291 dprc_get_obj_failures
, num_child_objects
);
296 * Allocate IRQ's before binding the scanned devices with their
297 * respective drivers.
299 if (dev_get_msi_domain(&mc_bus_dev
->dev
) && !mc_bus
->irq_resources
) {
300 if (irq_count
> FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
) {
301 dev_warn(&mc_bus_dev
->dev
,
302 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
303 irq_count
, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
);
306 error
= fsl_mc_populate_irq_pool(mc_bus
,
307 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
);
313 *total_irq_count
= irq_count
;
315 dprc_remove_devices(mc_bus_dev
, child_obj_desc_array
,
318 dprc_add_new_devices(mc_bus_dev
, child_obj_desc_array
,
321 if (child_obj_desc_array
)
322 devm_kfree(&mc_bus_dev
->dev
, child_obj_desc_array
);
328 * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
330 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
332 * Scans the physical DPRC and synchronizes the state of the Linux
333 * bus driver with the actual state of the MC by adding and removing
334 * devices as appropriate.
336 static int dprc_scan_container(struct fsl_mc_device
*mc_bus_dev
)
339 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
341 fsl_mc_init_all_resource_pools(mc_bus_dev
);
344 * Discover objects in the DPRC:
346 mutex_lock(&mc_bus
->scan_mutex
);
347 error
= dprc_scan_objects(mc_bus_dev
, NULL
);
348 mutex_unlock(&mc_bus
->scan_mutex
);
350 fsl_mc_cleanup_all_resource_pools(mc_bus_dev
);
358 * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
360 * @irq: IRQ number of the interrupt being handled
361 * @arg: Pointer to device structure
363 static irqreturn_t
dprc_irq0_handler(int irq_num
, void *arg
)
365 return IRQ_WAKE_THREAD
;
369 * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
371 * @irq: IRQ number of the interrupt being handled
372 * @arg: Pointer to device structure
374 static irqreturn_t
dprc_irq0_handler_thread(int irq_num
, void *arg
)
378 struct device
*dev
= arg
;
379 struct fsl_mc_device
*mc_dev
= to_fsl_mc_device(dev
);
380 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_dev
);
381 struct fsl_mc_io
*mc_io
= mc_dev
->mc_io
;
382 struct msi_desc
*msi_desc
= mc_dev
->irqs
[0]->msi_desc
;
384 dev_dbg(dev
, "DPRC IRQ %d triggered on CPU %u\n",
385 irq_num
, smp_processor_id());
387 if (!(mc_dev
->flags
& FSL_MC_IS_DPRC
))
390 mutex_lock(&mc_bus
->scan_mutex
);
391 if (!msi_desc
|| msi_desc
->irq
!= (u32
)irq_num
)
395 error
= dprc_get_irq_status(mc_io
, 0, mc_dev
->mc_handle
, 0,
399 "dprc_get_irq_status() failed: %d\n", error
);
403 error
= dprc_clear_irq_status(mc_io
, 0, mc_dev
->mc_handle
, 0,
407 "dprc_clear_irq_status() failed: %d\n", error
);
411 if (status
& (DPRC_IRQ_EVENT_OBJ_ADDED
|
412 DPRC_IRQ_EVENT_OBJ_REMOVED
|
413 DPRC_IRQ_EVENT_CONTAINER_DESTROYED
|
414 DPRC_IRQ_EVENT_OBJ_DESTROYED
|
415 DPRC_IRQ_EVENT_OBJ_CREATED
)) {
416 unsigned int irq_count
;
418 error
= dprc_scan_objects(mc_dev
, &irq_count
);
421 * If the error is -ENXIO, we ignore it, as it indicates
422 * that the object scan was aborted, as we detected that
423 * an object was removed from the DPRC in the MC, while
424 * we were scanning the DPRC.
426 if (error
!= -ENXIO
) {
427 dev_err(dev
, "dprc_scan_objects() failed: %d\n",
434 if (irq_count
> FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
) {
436 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
437 irq_count
, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
);
442 mutex_unlock(&mc_bus
->scan_mutex
);
447 * Disable and clear interrupt for a given DPRC object
449 static int disable_dprc_irq(struct fsl_mc_device
*mc_dev
)
452 struct fsl_mc_io
*mc_io
= mc_dev
->mc_io
;
455 * Disable generation of interrupt, while we configure it:
457 error
= dprc_set_irq_enable(mc_io
, 0, mc_dev
->mc_handle
, 0, 0);
459 dev_err(&mc_dev
->dev
,
460 "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
466 * Disable all interrupt causes for the interrupt:
468 error
= dprc_set_irq_mask(mc_io
, 0, mc_dev
->mc_handle
, 0, 0x0);
470 dev_err(&mc_dev
->dev
,
471 "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
477 * Clear any leftover interrupts:
479 error
= dprc_clear_irq_status(mc_io
, 0, mc_dev
->mc_handle
, 0, ~0x0U
);
481 dev_err(&mc_dev
->dev
,
482 "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
490 static int register_dprc_irq_handler(struct fsl_mc_device
*mc_dev
)
493 struct fsl_mc_device_irq
*irq
= mc_dev
->irqs
[0];
496 * NOTE: devm_request_threaded_irq() invokes the device-specific
497 * function that programs the MSI physically in the device
499 error
= devm_request_threaded_irq(&mc_dev
->dev
,
502 dprc_irq0_handler_thread
,
503 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
504 dev_name(&mc_dev
->dev
),
507 dev_err(&mc_dev
->dev
,
508 "devm_request_threaded_irq() failed: %d\n",
516 static int enable_dprc_irq(struct fsl_mc_device
*mc_dev
)
521 * Enable all interrupt causes for the interrupt:
523 error
= dprc_set_irq_mask(mc_dev
->mc_io
, 0, mc_dev
->mc_handle
, 0,
526 dev_err(&mc_dev
->dev
,
527 "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
534 * Enable generation of the interrupt:
536 error
= dprc_set_irq_enable(mc_dev
->mc_io
, 0, mc_dev
->mc_handle
, 0, 1);
538 dev_err(&mc_dev
->dev
,
539 "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
549 * Setup interrupt for a given DPRC device
551 static int dprc_setup_irq(struct fsl_mc_device
*mc_dev
)
555 error
= fsl_mc_allocate_irqs(mc_dev
);
559 error
= disable_dprc_irq(mc_dev
);
561 goto error_free_irqs
;
563 error
= register_dprc_irq_handler(mc_dev
);
565 goto error_free_irqs
;
567 error
= enable_dprc_irq(mc_dev
);
569 goto error_free_irqs
;
574 fsl_mc_free_irqs(mc_dev
);
579 * dprc_probe - callback invoked when a DPRC is being bound to this driver
581 * @mc_dev: Pointer to fsl-mc device representing a DPRC
583 * It opens the physical DPRC in the MC.
584 * It scans the DPRC to discover the MC objects contained in it.
585 * It creates the interrupt pool for the MC bus associated with the DPRC.
586 * It configures the interrupts for the DPRC device itself.
588 static int dprc_probe(struct fsl_mc_device
*mc_dev
)
592 struct device
*parent_dev
= mc_dev
->dev
.parent
;
593 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_dev
);
594 bool mc_io_created
= false;
595 bool msi_domain_set
= false;
596 u16 major_ver
, minor_ver
;
598 if (!is_fsl_mc_bus_dprc(mc_dev
))
601 if (dev_get_msi_domain(&mc_dev
->dev
))
604 if (!mc_dev
->mc_io
) {
606 * This is a child DPRC:
608 if (!dev_is_fsl_mc(parent_dev
))
611 if (mc_dev
->obj_desc
.region_count
== 0)
614 region_size
= resource_size(mc_dev
->regions
);
616 error
= fsl_create_mc_io(&mc_dev
->dev
,
617 mc_dev
->regions
[0].start
,
620 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL
,
625 mc_io_created
= true;
628 * Inherit parent MSI domain:
630 dev_set_msi_domain(&mc_dev
->dev
,
631 dev_get_msi_domain(parent_dev
));
632 msi_domain_set
= true;
635 * This is a root DPRC
637 struct irq_domain
*mc_msi_domain
;
639 if (dev_is_fsl_mc(parent_dev
))
642 error
= fsl_mc_find_msi_domain(parent_dev
,
645 dev_warn(&mc_dev
->dev
,
646 "WARNING: MC bus without interrupt support\n");
648 dev_set_msi_domain(&mc_dev
->dev
, mc_msi_domain
);
649 msi_domain_set
= true;
653 error
= dprc_open(mc_dev
->mc_io
, 0, mc_dev
->obj_desc
.id
,
656 dev_err(&mc_dev
->dev
, "dprc_open() failed: %d\n", error
);
657 goto error_cleanup_msi_domain
;
660 error
= dprc_get_attributes(mc_dev
->mc_io
, 0, mc_dev
->mc_handle
,
663 dev_err(&mc_dev
->dev
, "dprc_get_attributes() failed: %d\n",
665 goto error_cleanup_open
;
668 error
= dprc_get_api_version(mc_dev
->mc_io
, 0,
672 dev_err(&mc_dev
->dev
, "dprc_get_api_version() failed: %d\n",
674 goto error_cleanup_open
;
677 if (major_ver
< DPRC_MIN_VER_MAJOR
||
678 (major_ver
== DPRC_MIN_VER_MAJOR
&&
679 minor_ver
< DPRC_MIN_VER_MINOR
)) {
680 dev_err(&mc_dev
->dev
,
681 "ERROR: DPRC version %d.%d not supported\n",
682 major_ver
, minor_ver
);
684 goto error_cleanup_open
;
687 mutex_init(&mc_bus
->scan_mutex
);
690 * Discover MC objects in DPRC object:
692 error
= dprc_scan_container(mc_dev
);
694 goto error_cleanup_open
;
697 * Configure interrupt for the DPRC object associated with this MC bus:
699 error
= dprc_setup_irq(mc_dev
);
701 goto error_cleanup_open
;
703 dev_info(&mc_dev
->dev
, "DPRC device bound to driver");
707 (void)dprc_close(mc_dev
->mc_io
, 0, mc_dev
->mc_handle
);
709 error_cleanup_msi_domain
:
711 dev_set_msi_domain(&mc_dev
->dev
, NULL
);
714 fsl_destroy_mc_io(mc_dev
->mc_io
);
715 mc_dev
->mc_io
= NULL
;
722 * Tear down interrupt for a given DPRC object
724 static void dprc_teardown_irq(struct fsl_mc_device
*mc_dev
)
726 struct fsl_mc_device_irq
*irq
= mc_dev
->irqs
[0];
728 (void)disable_dprc_irq(mc_dev
);
730 devm_free_irq(&mc_dev
->dev
, irq
->msi_desc
->irq
, &mc_dev
->dev
);
732 fsl_mc_free_irqs(mc_dev
);
736 * dprc_remove - callback invoked when a DPRC is being unbound from this driver
738 * @mc_dev: Pointer to fsl-mc device representing the DPRC
740 * It removes the DPRC's child objects from Linux (not from the MC) and
741 * closes the DPRC device in the MC.
742 * It tears down the interrupts that were configured for the DPRC device.
743 * It destroys the interrupt pool associated with this MC bus.
745 static int dprc_remove(struct fsl_mc_device
*mc_dev
)
748 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_dev
);
750 if (!is_fsl_mc_bus_dprc(mc_dev
))
755 if (!mc_bus
->irq_resources
)
758 if (dev_get_msi_domain(&mc_dev
->dev
))
759 dprc_teardown_irq(mc_dev
);
761 device_for_each_child(&mc_dev
->dev
, NULL
, __fsl_mc_device_remove
);
763 if (dev_get_msi_domain(&mc_dev
->dev
)) {
764 fsl_mc_cleanup_irq_pool(mc_bus
);
765 dev_set_msi_domain(&mc_dev
->dev
, NULL
);
768 fsl_mc_cleanup_all_resource_pools(mc_dev
);
770 error
= dprc_close(mc_dev
->mc_io
, 0, mc_dev
->mc_handle
);
772 dev_err(&mc_dev
->dev
, "dprc_close() failed: %d\n", error
);
774 if (!fsl_mc_is_root_dprc(&mc_dev
->dev
)) {
775 fsl_destroy_mc_io(mc_dev
->mc_io
);
776 mc_dev
->mc_io
= NULL
;
779 dev_info(&mc_dev
->dev
, "DPRC device unbound from driver");
783 static const struct fsl_mc_device_id match_id_table
[] = {
785 .vendor
= FSL_MC_VENDOR_FREESCALE
,
790 static struct fsl_mc_driver dprc_driver
= {
792 .name
= FSL_MC_DPRC_DRIVER_NAME
,
793 .owner
= THIS_MODULE
,
796 .match_id_table
= match_id_table
,
798 .remove
= dprc_remove
,
801 int __init
dprc_driver_init(void)
803 return fsl_mc_driver_register(&dprc_driver
);
806 void dprc_driver_exit(void)
808 fsl_mc_driver_unregister(&dprc_driver
);