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 <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/msi.h>
16 #include "../include/mc-bus.h"
17 #include "../include/mc-sys.h"
20 #include "fsl-mc-private.h"
22 #define FSL_MC_DPRC_DRIVER_NAME "fsl_mc_dprc"
24 #define FSL_MC_DEVICE_MATCH(_mc_dev, _obj_desc) \
25 (strcmp((_mc_dev)->obj_desc.type, (_obj_desc)->type) == 0 && \
26 (_mc_dev)->obj_desc.id == (_obj_desc)->id)
28 struct dprc_child_objs {
30 struct dprc_obj_desc *child_array;
33 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
36 struct dprc_child_objs *objs;
37 struct fsl_mc_device *mc_dev;
41 mc_dev = to_fsl_mc_device(dev);
44 for (i = 0; i < objs->child_count; i++) {
45 struct dprc_obj_desc *obj_desc = &objs->child_array[i];
47 if (strlen(obj_desc->type) != 0 &&
48 FSL_MC_DEVICE_MATCH(mc_dev, obj_desc))
52 if (i == objs->child_count)
53 fsl_mc_device_remove(mc_dev);
58 static int __fsl_mc_device_remove(struct device *dev, void *data)
62 fsl_mc_device_remove(to_fsl_mc_device(dev));
67 * dprc_remove_devices - Removes devices for objects removed from a DPRC
69 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
70 * @obj_desc_array: array of object descriptors for child objects currently
71 * present in the DPRC in the MC.
72 * @num_child_objects_in_mc: number of entries in obj_desc_array
74 * Synchronizes the state of the Linux bus driver with the actual state of
75 * the MC by removing devices that represent MC objects that have
76 * been dynamically removed in the physical DPRC.
78 static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
79 struct dprc_obj_desc *obj_desc_array,
80 int num_child_objects_in_mc)
82 if (num_child_objects_in_mc != 0) {
84 * Remove child objects that are in the DPRC in Linux,
87 struct dprc_child_objs objs;
89 objs.child_count = num_child_objects_in_mc;
90 objs.child_array = obj_desc_array;
91 device_for_each_child(&mc_bus_dev->dev, &objs,
92 __fsl_mc_device_remove_if_not_in_mc);
95 * There are no child objects for this DPRC in the MC.
96 * So, remove all the child devices from Linux:
98 device_for_each_child(&mc_bus_dev->dev, NULL,
99 __fsl_mc_device_remove);
103 static int __fsl_mc_device_match(struct device *dev, void *data)
105 struct dprc_obj_desc *obj_desc = data;
106 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
108 return FSL_MC_DEVICE_MATCH(mc_dev, obj_desc);
111 static struct fsl_mc_device *fsl_mc_device_lookup(struct dprc_obj_desc
118 dev = device_find_child(&mc_bus_dev->dev, obj_desc,
119 __fsl_mc_device_match);
121 return dev ? to_fsl_mc_device(dev) : NULL;
125 * check_plugged_state_change - Check change in an MC object's plugged state
127 * @mc_dev: pointer to the fsl-mc device for a given MC object
128 * @obj_desc: pointer to the MC object's descriptor in the MC
130 * If the plugged state has changed from unplugged to plugged, the fsl-mc
131 * device is bound to the corresponding device driver.
132 * If the plugged state has changed from plugged to unplugged, the fsl-mc
133 * device is unbound from the corresponding device driver.
135 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
136 struct dprc_obj_desc *obj_desc)
139 u32 plugged_flag_at_mc =
140 obj_desc->state & DPRC_OBJ_STATE_PLUGGED;
142 if (plugged_flag_at_mc !=
143 (mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED)) {
144 if (plugged_flag_at_mc) {
145 mc_dev->obj_desc.state |= DPRC_OBJ_STATE_PLUGGED;
146 error = device_attach(&mc_dev->dev);
148 dev_err(&mc_dev->dev,
149 "device_attach() failed: %d\n",
153 mc_dev->obj_desc.state &= ~DPRC_OBJ_STATE_PLUGGED;
154 device_release_driver(&mc_dev->dev);
160 * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
162 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
163 * @obj_desc_array: array of device descriptors for child devices currently
164 * present in the physical DPRC.
165 * @num_child_objects_in_mc: number of entries in obj_desc_array
167 * Synchronizes the state of the Linux bus driver with the actual
168 * state of the MC by adding objects that have been newly discovered
169 * in the physical DPRC.
171 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
172 struct dprc_obj_desc *obj_desc_array,
173 int num_child_objects_in_mc)
178 for (i = 0; i < num_child_objects_in_mc; i++) {
179 struct fsl_mc_device *child_dev;
180 struct dprc_obj_desc *obj_desc = &obj_desc_array[i];
182 if (strlen(obj_desc->type) == 0)
186 * Check if device is already known to Linux:
188 child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
190 check_plugged_state_change(child_dev, obj_desc);
194 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
202 * dprc_scan_objects - Discover objects in a DPRC
204 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
205 * @total_irq_count: total number of IRQs needed by objects in the DPRC.
207 * Detects objects added and removed from a DPRC and synchronizes the
208 * state of the Linux bus driver, MC by adding and removing
209 * devices accordingly.
210 * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
211 * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
212 * All allocatable devices needed to be probed before all non-allocatable
213 * devices, to ensure that device drivers for non-allocatable
214 * devices can allocate any type of allocatable devices.
215 * That is, we need to ensure that the corresponding resource pools are
216 * populated before they can get allocation requests from probe callbacks
217 * of the device drivers for the non-allocatable devices.
219 int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
220 unsigned int *total_irq_count)
222 int num_child_objects;
223 int dprc_get_obj_failures;
225 unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
226 struct dprc_obj_desc *child_obj_desc_array = NULL;
228 error = dprc_get_obj_count(mc_bus_dev->mc_io,
230 mc_bus_dev->mc_handle,
233 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
238 if (num_child_objects != 0) {
241 child_obj_desc_array =
242 devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
243 sizeof(*child_obj_desc_array),
245 if (!child_obj_desc_array)
249 * Discover objects currently present in the physical DPRC:
251 dprc_get_obj_failures = 0;
252 for (i = 0; i < num_child_objects; i++) {
253 struct dprc_obj_desc *obj_desc =
254 &child_obj_desc_array[i];
256 error = dprc_get_obj(mc_bus_dev->mc_io,
258 mc_bus_dev->mc_handle,
261 dev_err(&mc_bus_dev->dev,
262 "dprc_get_obj(i=%d) failed: %d\n",
265 * Mark the obj entry as "invalid", by using the
266 * empty string as obj type:
268 obj_desc->type[0] = '\0';
269 obj_desc->id = error;
270 dprc_get_obj_failures++;
275 * add a quirk for all versions of dpsec < 4.0...none
276 * are coherent regardless of what the MC reports.
278 if ((strcmp(obj_desc->type, "dpseci") == 0) &&
279 (obj_desc->ver_major < 4))
281 DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY;
283 irq_count += obj_desc->irq_count;
284 dev_dbg(&mc_bus_dev->dev,
285 "Discovered object: type %s, id %d\n",
286 obj_desc->type, obj_desc->id);
289 if (dprc_get_obj_failures != 0) {
290 dev_err(&mc_bus_dev->dev,
291 "%d out of %d devices could not be retrieved\n",
292 dprc_get_obj_failures, num_child_objects);
296 *total_irq_count = irq_count;
297 dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
300 dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
303 if (child_obj_desc_array)
304 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
308 EXPORT_SYMBOL_GPL(dprc_scan_objects);
311 * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
313 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
315 * Scans the physical DPRC and synchronizes the state of the Linux
316 * bus driver with the actual state of the MC by adding and removing
317 * devices as appropriate.
319 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
322 unsigned int irq_count;
323 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
325 fsl_mc_init_all_resource_pools(mc_bus_dev);
328 * Discover objects in the DPRC:
330 mutex_lock(&mc_bus->scan_mutex);
331 error = dprc_scan_objects(mc_bus_dev, &irq_count);
332 mutex_unlock(&mc_bus->scan_mutex);
336 if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
337 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
338 dev_warn(&mc_bus_dev->dev,
339 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
340 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
343 error = fsl_mc_populate_irq_pool(
345 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
352 fsl_mc_cleanup_all_resource_pools(mc_bus_dev);
355 EXPORT_SYMBOL_GPL(dprc_scan_container);
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 (WARN_ON(!(mc_dev->flags & FSL_MC_IS_DPRC)))
390 mutex_lock(&mc_bus->scan_mutex);
391 if (WARN_ON(!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;
454 WARN_ON(mc_dev->obj_desc.irq_count != 1);
457 * Disable generation of interrupt, while we configure it:
459 error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
461 dev_err(&mc_dev->dev,
462 "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
468 * Disable all interrupt causes for the interrupt:
470 error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
472 dev_err(&mc_dev->dev,
473 "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
479 * Clear any leftover interrupts:
481 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
483 dev_err(&mc_dev->dev,
484 "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
492 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
495 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
497 WARN_ON(mc_dev->obj_desc.irq_count != 1);
500 * NOTE: devm_request_threaded_irq() invokes the device-specific
501 * function that programs the MSI physically in the device
503 error = devm_request_threaded_irq(&mc_dev->dev,
506 dprc_irq0_handler_thread,
507 IRQF_NO_SUSPEND | IRQF_ONESHOT,
511 dev_err(&mc_dev->dev,
512 "devm_request_threaded_irq() failed: %d\n",
520 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
525 * Enable all interrupt causes for the interrupt:
527 error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
530 dev_err(&mc_dev->dev,
531 "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
538 * Enable generation of the interrupt:
540 error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
542 dev_err(&mc_dev->dev,
543 "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
553 * Setup interrupt for a given DPRC device
555 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
559 error = fsl_mc_allocate_irqs(mc_dev);
563 error = disable_dprc_irq(mc_dev);
565 goto error_free_irqs;
567 error = register_dprc_irq_handler(mc_dev);
569 goto error_free_irqs;
571 error = enable_dprc_irq(mc_dev);
573 goto error_free_irqs;
578 fsl_mc_free_irqs(mc_dev);
583 * dprc_probe - callback invoked when a DPRC is being bound to this driver
585 * @mc_dev: Pointer to fsl-mc device representing a DPRC
587 * It opens the physical DPRC in the MC.
588 * It scans the DPRC to discover the MC objects contained in it.
589 * It creates the interrupt pool for the MC bus associated with the DPRC.
590 * It configures the interrupts for the DPRC device itself.
592 static int dprc_probe(struct fsl_mc_device *mc_dev)
596 struct device *parent_dev = mc_dev->dev.parent;
597 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
598 bool mc_io_created = false;
599 bool msi_domain_set = false;
601 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
604 if (WARN_ON(dev_get_msi_domain(&mc_dev->dev)))
607 if (!mc_dev->mc_io) {
609 * This is a child DPRC:
611 if (WARN_ON(!dev_is_fsl_mc(parent_dev)))
614 if (WARN_ON(mc_dev->obj_desc.region_count == 0))
617 region_size = mc_dev->regions[0].end -
618 mc_dev->regions[0].start + 1;
620 error = fsl_create_mc_io(&mc_dev->dev,
621 mc_dev->regions[0].start,
624 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
629 mc_io_created = true;
632 * Inherit parent MSI domain:
634 dev_set_msi_domain(&mc_dev->dev,
635 dev_get_msi_domain(parent_dev));
636 msi_domain_set = true;
639 * This is a root DPRC
641 struct irq_domain *mc_msi_domain;
643 if (WARN_ON(dev_is_fsl_mc(parent_dev)))
646 error = fsl_mc_find_msi_domain(parent_dev,
649 dev_warn(&mc_dev->dev,
650 "WARNING: MC bus without interrupt support\n");
652 dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
653 msi_domain_set = true;
657 error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
660 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
661 goto error_cleanup_msi_domain;
664 error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
667 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
669 goto error_cleanup_open;
672 if (mc_bus->dprc_attr.version.major < DPRC_MIN_VER_MAJOR ||
673 (mc_bus->dprc_attr.version.major == DPRC_MIN_VER_MAJOR &&
674 mc_bus->dprc_attr.version.minor < DPRC_MIN_VER_MINOR)) {
675 dev_err(&mc_dev->dev,
676 "ERROR: DPRC version %d.%d not supported\n",
677 mc_bus->dprc_attr.version.major,
678 mc_bus->dprc_attr.version.minor);
680 goto error_cleanup_open;
683 mutex_init(&mc_bus->scan_mutex);
686 * Discover MC objects in DPRC object:
688 error = dprc_scan_container(mc_dev);
690 goto error_cleanup_open;
693 * Configure interrupt for the DPRC object associated with this MC bus:
695 error = dprc_setup_irq(mc_dev);
697 goto error_cleanup_open;
699 dev_info(&mc_dev->dev, "DPRC device bound to driver");
703 (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
705 error_cleanup_msi_domain:
707 dev_set_msi_domain(&mc_dev->dev, NULL);
710 fsl_destroy_mc_io(mc_dev->mc_io);
711 mc_dev->mc_io = NULL;
718 * Tear down interrupt for a given DPRC object
720 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
722 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
724 (void)disable_dprc_irq(mc_dev);
726 devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
728 fsl_mc_free_irqs(mc_dev);
732 * dprc_remove - callback invoked when a DPRC is being unbound from this driver
734 * @mc_dev: Pointer to fsl-mc device representing the DPRC
736 * It removes the DPRC's child objects from Linux (not from the MC) and
737 * closes the DPRC device in the MC.
738 * It tears down the interrupts that were configured for the DPRC device.
739 * It destroys the interrupt pool associated with this MC bus.
741 static int dprc_remove(struct fsl_mc_device *mc_dev)
744 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
746 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
748 if (WARN_ON(!mc_dev->mc_io))
751 if (WARN_ON(!mc_bus->irq_resources))
754 if (dev_get_msi_domain(&mc_dev->dev))
755 dprc_teardown_irq(mc_dev);
757 device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
759 if (dev_get_msi_domain(&mc_dev->dev)) {
760 fsl_mc_cleanup_irq_pool(mc_bus);
761 dev_set_msi_domain(&mc_dev->dev, NULL);
764 fsl_mc_cleanup_all_resource_pools(mc_dev);
766 error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
768 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
770 if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
771 fsl_destroy_mc_io(mc_dev->mc_io);
772 mc_dev->mc_io = NULL;
775 dev_info(&mc_dev->dev, "DPRC device unbound from driver");
779 static const struct fsl_mc_device_id match_id_table[] = {
781 .vendor = FSL_MC_VENDOR_FREESCALE,
786 static struct fsl_mc_driver dprc_driver = {
788 .name = FSL_MC_DPRC_DRIVER_NAME,
789 .owner = THIS_MODULE,
792 .match_id_table = match_id_table,
794 .remove = dprc_remove,
797 int __init dprc_driver_init(void)
799 return fsl_mc_driver_register(&dprc_driver);
802 void dprc_driver_exit(void)
804 fsl_mc_driver_unregister(&dprc_driver);