2 * Freescale data path resource container (DPRC) driver
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 * Author: German Rivera <German.Rivera@freescale.com>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include "../include/mc-private.h"
13 #include "../include/mc-sys.h"
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/msi.h>
20 struct dprc_child_objs {
22 struct dprc_obj_desc *child_array;
25 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
28 struct dprc_child_objs *objs;
29 struct fsl_mc_device *mc_dev;
33 mc_dev = to_fsl_mc_device(dev);
36 for (i = 0; i < objs->child_count; i++) {
37 struct dprc_obj_desc *obj_desc = &objs->child_array[i];
39 if (strlen(obj_desc->type) != 0 &&
40 FSL_MC_DEVICE_MATCH(mc_dev, obj_desc))
44 if (i == objs->child_count)
45 fsl_mc_device_remove(mc_dev);
50 static int __fsl_mc_device_remove(struct device *dev, void *data)
54 fsl_mc_device_remove(to_fsl_mc_device(dev));
59 * dprc_remove_devices - Removes devices for objects removed from a DPRC
61 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
62 * @obj_desc_array: array of object descriptors for child objects currently
63 * present in the DPRC in the MC.
64 * @num_child_objects_in_mc: number of entries in obj_desc_array
66 * Synchronizes the state of the Linux bus driver with the actual state of
67 * the MC by removing devices that represent MC objects that have
68 * been dynamically removed in the physical DPRC.
70 static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
71 struct dprc_obj_desc *obj_desc_array,
72 int num_child_objects_in_mc)
74 if (num_child_objects_in_mc != 0) {
76 * Remove child objects that are in the DPRC in Linux,
79 struct dprc_child_objs objs;
81 objs.child_count = num_child_objects_in_mc;
82 objs.child_array = obj_desc_array;
83 device_for_each_child(&mc_bus_dev->dev, &objs,
84 __fsl_mc_device_remove_if_not_in_mc);
87 * There are no child objects for this DPRC in the MC.
88 * So, remove all the child devices from Linux:
90 device_for_each_child(&mc_bus_dev->dev, NULL,
91 __fsl_mc_device_remove);
95 static int __fsl_mc_device_match(struct device *dev, void *data)
97 struct dprc_obj_desc *obj_desc = data;
98 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
100 return FSL_MC_DEVICE_MATCH(mc_dev, obj_desc);
103 static struct fsl_mc_device *fsl_mc_device_lookup(struct dprc_obj_desc
110 dev = device_find_child(&mc_bus_dev->dev, obj_desc,
111 __fsl_mc_device_match);
113 return dev ? to_fsl_mc_device(dev) : NULL;
117 * check_plugged_state_change - Check change in an MC object's plugged state
119 * @mc_dev: pointer to the fsl-mc device for a given MC object
120 * @obj_desc: pointer to the MC object's descriptor in the MC
122 * If the plugged state has changed from unplugged to plugged, the fsl-mc
123 * device is bound to the corresponding device driver.
124 * If the plugged state has changed from plugged to unplugged, the fsl-mc
125 * device is unbound from the corresponding device driver.
127 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
128 struct dprc_obj_desc *obj_desc)
131 u32 plugged_flag_at_mc =
132 obj_desc->state & DPRC_OBJ_STATE_PLUGGED;
134 if (plugged_flag_at_mc !=
135 (mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED)) {
136 if (plugged_flag_at_mc) {
137 mc_dev->obj_desc.state |= DPRC_OBJ_STATE_PLUGGED;
138 error = device_attach(&mc_dev->dev);
140 dev_err(&mc_dev->dev,
141 "device_attach() failed: %d\n",
145 mc_dev->obj_desc.state &= ~DPRC_OBJ_STATE_PLUGGED;
146 device_release_driver(&mc_dev->dev);
152 * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
154 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
155 * @obj_desc_array: array of device descriptors for child devices currently
156 * present in the physical DPRC.
157 * @num_child_objects_in_mc: number of entries in obj_desc_array
159 * Synchronizes the state of the Linux bus driver with the actual
160 * state of the MC by adding objects that have been newly discovered
161 * in the physical DPRC.
163 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
164 struct dprc_obj_desc *obj_desc_array,
165 int num_child_objects_in_mc)
170 for (i = 0; i < num_child_objects_in_mc; i++) {
171 struct fsl_mc_device *child_dev;
172 struct dprc_obj_desc *obj_desc = &obj_desc_array[i];
174 if (strlen(obj_desc->type) == 0)
178 * Check if device is already known to Linux:
180 child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
182 check_plugged_state_change(child_dev, obj_desc);
186 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
193 static void dprc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
196 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
198 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
199 struct fsl_mc_resource_pool *res_pool =
200 &mc_bus->resource_pools[pool_type];
202 res_pool->type = pool_type;
203 res_pool->max_count = 0;
204 res_pool->free_count = 0;
205 res_pool->mc_bus = mc_bus;
206 INIT_LIST_HEAD(&res_pool->free_list);
207 mutex_init(&res_pool->mutex);
211 static void dprc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
212 enum fsl_mc_pool_type pool_type)
214 struct fsl_mc_resource *resource;
215 struct fsl_mc_resource *next;
216 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
217 struct fsl_mc_resource_pool *res_pool =
218 &mc_bus->resource_pools[pool_type];
221 WARN_ON(res_pool->type != pool_type);
222 WARN_ON(res_pool->free_count != res_pool->max_count);
224 list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
226 WARN_ON(resource->type != res_pool->type);
227 WARN_ON(resource->parent_pool != res_pool);
228 devm_kfree(&mc_bus_dev->dev, resource);
231 WARN_ON(free_count != res_pool->free_count);
234 static void dprc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
238 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
239 dprc_cleanup_resource_pool(mc_bus_dev, pool_type);
243 * dprc_scan_objects - Discover objects in a DPRC
245 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
246 * @total_irq_count: total number of IRQs needed by objects in the DPRC.
248 * Detects objects added and removed from a DPRC and synchronizes the
249 * state of the Linux bus driver, MC by adding and removing
250 * devices accordingly.
251 * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
252 * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
253 * All allocatable devices needed to be probed before all non-allocatable
254 * devices, to ensure that device drivers for non-allocatable
255 * devices can allocate any type of allocatable devices.
256 * That is, we need to ensure that the corresponding resource pools are
257 * populated before they can get allocation requests from probe callbacks
258 * of the device drivers for the non-allocatable devices.
260 int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
261 unsigned int *total_irq_count)
263 int num_child_objects;
264 int dprc_get_obj_failures;
266 unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
267 struct dprc_obj_desc *child_obj_desc_array = NULL;
269 error = dprc_get_obj_count(mc_bus_dev->mc_io,
271 mc_bus_dev->mc_handle,
274 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
279 if (num_child_objects != 0) {
282 child_obj_desc_array =
283 devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
284 sizeof(*child_obj_desc_array),
286 if (!child_obj_desc_array)
290 * Discover objects currently present in the physical DPRC:
292 dprc_get_obj_failures = 0;
293 for (i = 0; i < num_child_objects; i++) {
294 struct dprc_obj_desc *obj_desc =
295 &child_obj_desc_array[i];
297 error = dprc_get_obj(mc_bus_dev->mc_io,
299 mc_bus_dev->mc_handle,
302 dev_err(&mc_bus_dev->dev,
303 "dprc_get_obj(i=%d) failed: %d\n",
306 * Mark the obj entry as "invalid", by using the
307 * empty string as obj type:
309 obj_desc->type[0] = '\0';
310 obj_desc->id = error;
311 dprc_get_obj_failures++;
316 * add a quirk for all versions of dpsec < 4.0...none
317 * are coherent regardless of what the MC reports.
319 if ((strcmp(obj_desc->type, "dpseci") == 0) &&
320 (obj_desc->ver_major < 4))
322 DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY;
324 irq_count += obj_desc->irq_count;
325 dev_dbg(&mc_bus_dev->dev,
326 "Discovered object: type %s, id %d\n",
327 obj_desc->type, obj_desc->id);
330 if (dprc_get_obj_failures != 0) {
331 dev_err(&mc_bus_dev->dev,
332 "%d out of %d devices could not be retrieved\n",
333 dprc_get_obj_failures, num_child_objects);
337 *total_irq_count = irq_count;
338 dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
341 dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
344 if (child_obj_desc_array)
345 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
349 EXPORT_SYMBOL_GPL(dprc_scan_objects);
352 * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
354 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
356 * Scans the physical DPRC and synchronizes the state of the Linux
357 * bus driver with the actual state of the MC by adding and removing
358 * devices as appropriate.
360 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
363 unsigned int irq_count;
364 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
366 dprc_init_all_resource_pools(mc_bus_dev);
369 * Discover objects in the DPRC:
371 mutex_lock(&mc_bus->scan_mutex);
372 error = dprc_scan_objects(mc_bus_dev, &irq_count);
373 mutex_unlock(&mc_bus->scan_mutex);
377 if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
378 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
379 dev_warn(&mc_bus_dev->dev,
380 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
381 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
384 error = fsl_mc_populate_irq_pool(
386 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
393 dprc_cleanup_all_resource_pools(mc_bus_dev);
396 EXPORT_SYMBOL_GPL(dprc_scan_container);
399 * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
401 * @irq: IRQ number of the interrupt being handled
402 * @arg: Pointer to device structure
404 static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
406 return IRQ_WAKE_THREAD;
410 * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
412 * @irq: IRQ number of the interrupt being handled
413 * @arg: Pointer to device structure
415 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
419 struct device *dev = arg;
420 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
421 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
422 struct fsl_mc_io *mc_io = mc_dev->mc_io;
423 struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
425 dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
426 irq_num, smp_processor_id());
428 if (WARN_ON(!(mc_dev->flags & FSL_MC_IS_DPRC)))
431 mutex_lock(&mc_bus->scan_mutex);
432 if (WARN_ON(!msi_desc || msi_desc->irq != (u32)irq_num))
436 error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
440 "dprc_get_irq_status() failed: %d\n", error);
444 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
448 "dprc_clear_irq_status() failed: %d\n", error);
452 if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
453 DPRC_IRQ_EVENT_OBJ_REMOVED |
454 DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
455 DPRC_IRQ_EVENT_OBJ_DESTROYED |
456 DPRC_IRQ_EVENT_OBJ_CREATED)) {
457 unsigned int irq_count;
459 error = dprc_scan_objects(mc_dev, &irq_count);
462 * If the error is -ENXIO, we ignore it, as it indicates
463 * that the object scan was aborted, as we detected that
464 * an object was removed from the DPRC in the MC, while
465 * we were scanning the DPRC.
467 if (error != -ENXIO) {
468 dev_err(dev, "dprc_scan_objects() failed: %d\n",
475 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
477 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
478 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
483 mutex_unlock(&mc_bus->scan_mutex);
488 * Disable and clear interrupt for a given DPRC object
490 static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
493 struct fsl_mc_io *mc_io = mc_dev->mc_io;
495 WARN_ON(mc_dev->obj_desc.irq_count != 1);
498 * Disable generation of interrupt, while we configure it:
500 error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
502 dev_err(&mc_dev->dev,
503 "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
509 * Disable all interrupt causes for the interrupt:
511 error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
513 dev_err(&mc_dev->dev,
514 "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
520 * Clear any leftover interrupts:
522 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
524 dev_err(&mc_dev->dev,
525 "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
533 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
536 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
538 WARN_ON(mc_dev->obj_desc.irq_count != 1);
541 * NOTE: devm_request_threaded_irq() invokes the device-specific
542 * function that programs the MSI physically in the device
544 error = devm_request_threaded_irq(&mc_dev->dev,
547 dprc_irq0_handler_thread,
548 IRQF_NO_SUSPEND | IRQF_ONESHOT,
552 dev_err(&mc_dev->dev,
553 "devm_request_threaded_irq() failed: %d\n",
561 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
566 * Enable all interrupt causes for the interrupt:
568 error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
571 dev_err(&mc_dev->dev,
572 "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
579 * Enable generation of the interrupt:
581 error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
583 dev_err(&mc_dev->dev,
584 "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
594 * Setup interrupt for a given DPRC device
596 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
600 error = fsl_mc_allocate_irqs(mc_dev);
604 error = disable_dprc_irq(mc_dev);
606 goto error_free_irqs;
608 error = register_dprc_irq_handler(mc_dev);
610 goto error_free_irqs;
612 error = enable_dprc_irq(mc_dev);
614 goto error_free_irqs;
619 fsl_mc_free_irqs(mc_dev);
624 * dprc_probe - callback invoked when a DPRC is being bound to this driver
626 * @mc_dev: Pointer to fsl-mc device representing a DPRC
628 * It opens the physical DPRC in the MC.
629 * It scans the DPRC to discover the MC objects contained in it.
630 * It creates the interrupt pool for the MC bus associated with the DPRC.
631 * It configures the interrupts for the DPRC device itself.
633 static int dprc_probe(struct fsl_mc_device *mc_dev)
637 struct device *parent_dev = mc_dev->dev.parent;
638 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
639 bool mc_io_created = false;
640 bool msi_domain_set = false;
642 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
645 if (WARN_ON(dev_get_msi_domain(&mc_dev->dev)))
648 if (!mc_dev->mc_io) {
650 * This is a child DPRC:
652 if (WARN_ON(parent_dev->bus != &fsl_mc_bus_type))
655 if (WARN_ON(mc_dev->obj_desc.region_count == 0))
658 region_size = mc_dev->regions[0].end -
659 mc_dev->regions[0].start + 1;
661 error = fsl_create_mc_io(&mc_dev->dev,
662 mc_dev->regions[0].start,
665 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
670 mc_io_created = true;
673 * Inherit parent MSI domain:
675 dev_set_msi_domain(&mc_dev->dev,
676 dev_get_msi_domain(parent_dev));
677 msi_domain_set = true;
680 * This is a root DPRC
682 struct irq_domain *mc_msi_domain;
684 if (WARN_ON(parent_dev->bus == &fsl_mc_bus_type))
687 error = fsl_mc_find_msi_domain(parent_dev,
690 dev_warn(&mc_dev->dev,
691 "WARNING: MC bus without interrupt support\n");
693 dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
694 msi_domain_set = true;
698 error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
701 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
702 goto error_cleanup_msi_domain;
705 error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
708 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
710 goto error_cleanup_open;
713 if (mc_bus->dprc_attr.version.major < DPRC_MIN_VER_MAJOR ||
714 (mc_bus->dprc_attr.version.major == DPRC_MIN_VER_MAJOR &&
715 mc_bus->dprc_attr.version.minor < DPRC_MIN_VER_MINOR)) {
716 dev_err(&mc_dev->dev,
717 "ERROR: DPRC version %d.%d not supported\n",
718 mc_bus->dprc_attr.version.major,
719 mc_bus->dprc_attr.version.minor);
721 goto error_cleanup_open;
724 mutex_init(&mc_bus->scan_mutex);
727 * Discover MC objects in DPRC object:
729 error = dprc_scan_container(mc_dev);
731 goto error_cleanup_open;
734 * Configure interrupt for the DPRC object associated with this MC bus:
736 error = dprc_setup_irq(mc_dev);
738 goto error_cleanup_open;
740 dev_info(&mc_dev->dev, "DPRC device bound to driver");
744 (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
746 error_cleanup_msi_domain:
748 dev_set_msi_domain(&mc_dev->dev, NULL);
751 fsl_destroy_mc_io(mc_dev->mc_io);
752 mc_dev->mc_io = NULL;
759 * Tear down interrupt for a given DPRC object
761 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
763 (void)disable_dprc_irq(mc_dev);
764 fsl_mc_free_irqs(mc_dev);
768 * dprc_remove - callback invoked when a DPRC is being unbound from this driver
770 * @mc_dev: Pointer to fsl-mc device representing the DPRC
772 * It removes the DPRC's child objects from Linux (not from the MC) and
773 * closes the DPRC device in the MC.
774 * It tears down the interrupts that were configured for the DPRC device.
775 * It destroys the interrupt pool associated with this MC bus.
777 static int dprc_remove(struct fsl_mc_device *mc_dev)
780 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
782 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
784 if (WARN_ON(!mc_dev->mc_io))
787 if (WARN_ON(!mc_bus->irq_resources))
790 if (dev_get_msi_domain(&mc_dev->dev))
791 dprc_teardown_irq(mc_dev);
793 device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
794 dprc_cleanup_all_resource_pools(mc_dev);
795 error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
797 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
799 if (dev_get_msi_domain(&mc_dev->dev)) {
800 fsl_mc_cleanup_irq_pool(mc_bus);
801 dev_set_msi_domain(&mc_dev->dev, NULL);
804 dev_info(&mc_dev->dev, "DPRC device unbound from driver");
808 static const struct fsl_mc_device_match_id match_id_table[] = {
810 .vendor = FSL_MC_VENDOR_FREESCALE,
815 static struct fsl_mc_driver dprc_driver = {
817 .name = FSL_MC_DPRC_DRIVER_NAME,
818 .owner = THIS_MODULE,
821 .match_id_table = match_id_table,
823 .remove = dprc_remove,
826 int __init dprc_driver_init(void)
828 return fsl_mc_driver_register(&dprc_driver);
831 void dprc_driver_exit(void)
833 fsl_mc_driver_unregister(&dprc_driver);