2 * Freescale data path resource container (DPRC) driver
4 * Copyright (C) 2014-2016 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.h"
19 #include "fsl-mc-private.h"
21 #define FSL_MC_DPRC_DRIVER_NAME "fsl_mc_dprc"
23 struct fsl_mc_child_objs {
25 struct fsl_mc_obj_desc *child_array;
28 static bool fsl_mc_device_match(struct fsl_mc_device *mc_dev,
29 struct fsl_mc_obj_desc *obj_desc)
31 return mc_dev->obj_desc.id == obj_desc->id &&
32 strcmp(mc_dev->obj_desc.type, obj_desc->type) == 0;
36 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
39 struct fsl_mc_child_objs *objs;
40 struct fsl_mc_device *mc_dev;
44 mc_dev = to_fsl_mc_device(dev);
47 for (i = 0; i < objs->child_count; i++) {
48 struct fsl_mc_obj_desc *obj_desc = &objs->child_array[i];
50 if (strlen(obj_desc->type) != 0 &&
51 fsl_mc_device_match(mc_dev, obj_desc))
55 if (i == objs->child_count)
56 fsl_mc_device_remove(mc_dev);
61 static int __fsl_mc_device_remove(struct device *dev, void *data)
65 fsl_mc_device_remove(to_fsl_mc_device(dev));
70 * dprc_remove_devices - Removes devices for objects removed from a DPRC
72 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
73 * @obj_desc_array: array of object descriptors for child objects currently
74 * present in the DPRC in the MC.
75 * @num_child_objects_in_mc: number of entries in obj_desc_array
77 * Synchronizes the state of the Linux bus driver with the actual state of
78 * the MC by removing devices that represent MC objects that have
79 * been dynamically removed in the physical DPRC.
81 static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
82 struct fsl_mc_obj_desc *obj_desc_array,
83 int num_child_objects_in_mc)
85 if (num_child_objects_in_mc != 0) {
87 * Remove child objects that are in the DPRC in Linux,
90 struct fsl_mc_child_objs objs;
92 objs.child_count = num_child_objects_in_mc;
93 objs.child_array = obj_desc_array;
94 device_for_each_child(&mc_bus_dev->dev, &objs,
95 __fsl_mc_device_remove_if_not_in_mc);
98 * There are no child objects for this DPRC in the MC.
99 * So, remove all the child devices from Linux:
101 device_for_each_child(&mc_bus_dev->dev, NULL,
102 __fsl_mc_device_remove);
106 static int __fsl_mc_device_match(struct device *dev, void *data)
108 struct fsl_mc_obj_desc *obj_desc = data;
109 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
111 return fsl_mc_device_match(mc_dev, obj_desc);
114 static struct fsl_mc_device *fsl_mc_device_lookup(struct fsl_mc_obj_desc
121 dev = device_find_child(&mc_bus_dev->dev, obj_desc,
122 __fsl_mc_device_match);
124 return dev ? to_fsl_mc_device(dev) : NULL;
128 * check_plugged_state_change - Check change in an MC object's plugged state
130 * @mc_dev: pointer to the fsl-mc device for a given MC object
131 * @obj_desc: pointer to the MC object's descriptor in the MC
133 * If the plugged state has changed from unplugged to plugged, the fsl-mc
134 * device is bound to the corresponding device driver.
135 * If the plugged state has changed from plugged to unplugged, the fsl-mc
136 * device is unbound from the corresponding device driver.
138 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
139 struct fsl_mc_obj_desc *obj_desc)
142 u32 plugged_flag_at_mc =
143 obj_desc->state & FSL_MC_OBJ_STATE_PLUGGED;
145 if (plugged_flag_at_mc !=
146 (mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED)) {
147 if (plugged_flag_at_mc) {
148 mc_dev->obj_desc.state |= FSL_MC_OBJ_STATE_PLUGGED;
149 error = device_attach(&mc_dev->dev);
151 dev_err(&mc_dev->dev,
152 "device_attach() failed: %d\n",
156 mc_dev->obj_desc.state &= ~FSL_MC_OBJ_STATE_PLUGGED;
157 device_release_driver(&mc_dev->dev);
163 * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
165 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
166 * @obj_desc_array: array of device descriptors for child devices currently
167 * present in the physical DPRC.
168 * @num_child_objects_in_mc: number of entries in obj_desc_array
170 * Synchronizes the state of the Linux bus driver with the actual
171 * state of the MC by adding objects that have been newly discovered
172 * in the physical DPRC.
174 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
175 struct fsl_mc_obj_desc *obj_desc_array,
176 int num_child_objects_in_mc)
181 for (i = 0; i < num_child_objects_in_mc; i++) {
182 struct fsl_mc_device *child_dev;
183 struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
185 if (strlen(obj_desc->type) == 0)
189 * Check if device is already known to Linux:
191 child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
193 check_plugged_state_change(child_dev, obj_desc);
194 put_device(&child_dev->dev);
198 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
206 * dprc_scan_objects - Discover objects in a DPRC
208 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
209 * @total_irq_count: total number of IRQs needed by objects in the DPRC.
211 * Detects objects added and removed from a DPRC and synchronizes the
212 * state of the Linux bus driver, MC by adding and removing
213 * devices accordingly.
214 * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
215 * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
216 * All allocatable devices needed to be probed before all non-allocatable
217 * devices, to ensure that device drivers for non-allocatable
218 * devices can allocate any type of allocatable devices.
219 * That is, we need to ensure that the corresponding resource pools are
220 * populated before they can get allocation requests from probe callbacks
221 * of the device drivers for the non-allocatable devices.
223 static int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
224 unsigned int *total_irq_count)
226 int num_child_objects;
227 int dprc_get_obj_failures;
229 unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
230 struct fsl_mc_obj_desc *child_obj_desc_array = NULL;
232 error = dprc_get_obj_count(mc_bus_dev->mc_io,
234 mc_bus_dev->mc_handle,
237 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
242 if (num_child_objects != 0) {
245 child_obj_desc_array =
246 devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
247 sizeof(*child_obj_desc_array),
249 if (!child_obj_desc_array)
253 * Discover objects currently present in the physical DPRC:
255 dprc_get_obj_failures = 0;
256 for (i = 0; i < num_child_objects; i++) {
257 struct fsl_mc_obj_desc *obj_desc =
258 &child_obj_desc_array[i];
260 error = dprc_get_obj(mc_bus_dev->mc_io,
262 mc_bus_dev->mc_handle,
265 dev_err(&mc_bus_dev->dev,
266 "dprc_get_obj(i=%d) failed: %d\n",
269 * Mark the obj entry as "invalid", by using the
270 * empty string as obj type:
272 obj_desc->type[0] = '\0';
273 obj_desc->id = error;
274 dprc_get_obj_failures++;
279 * add a quirk for all versions of dpsec < 4.0...none
280 * are coherent regardless of what the MC reports.
282 if ((strcmp(obj_desc->type, "dpseci") == 0) &&
283 (obj_desc->ver_major < 4))
285 FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY;
287 irq_count += obj_desc->irq_count;
288 dev_dbg(&mc_bus_dev->dev,
289 "Discovered object: type %s, id %d\n",
290 obj_desc->type, obj_desc->id);
293 if (dprc_get_obj_failures != 0) {
294 dev_err(&mc_bus_dev->dev,
295 "%d out of %d devices could not be retrieved\n",
296 dprc_get_obj_failures, num_child_objects);
300 *total_irq_count = irq_count;
301 dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
304 dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
307 if (child_obj_desc_array)
308 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
314 * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
316 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
318 * Scans the physical DPRC and synchronizes the state of the Linux
319 * bus driver with the actual state of the MC by adding and removing
320 * devices as appropriate.
322 static int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
325 unsigned int irq_count;
326 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
328 fsl_mc_init_all_resource_pools(mc_bus_dev);
331 * Discover objects in the DPRC:
333 mutex_lock(&mc_bus->scan_mutex);
334 error = dprc_scan_objects(mc_bus_dev, &irq_count);
335 mutex_unlock(&mc_bus->scan_mutex);
339 if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
340 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
341 dev_warn(&mc_bus_dev->dev,
342 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
343 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
346 error = fsl_mc_populate_irq_pool(
348 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
355 fsl_mc_cleanup_all_resource_pools(mc_bus_dev);
360 * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
362 * @irq: IRQ number of the interrupt being handled
363 * @arg: Pointer to device structure
365 static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
367 return IRQ_WAKE_THREAD;
371 * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
373 * @irq: IRQ number of the interrupt being handled
374 * @arg: Pointer to device structure
376 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
380 struct device *dev = arg;
381 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
382 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
383 struct fsl_mc_io *mc_io = mc_dev->mc_io;
384 struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
386 dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
387 irq_num, smp_processor_id());
389 if (WARN_ON(!(mc_dev->flags & FSL_MC_IS_DPRC)))
392 mutex_lock(&mc_bus->scan_mutex);
393 if (WARN_ON(!msi_desc || msi_desc->irq != (u32)irq_num))
397 error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
401 "dprc_get_irq_status() failed: %d\n", error);
405 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
409 "dprc_clear_irq_status() failed: %d\n", error);
413 if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
414 DPRC_IRQ_EVENT_OBJ_REMOVED |
415 DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
416 DPRC_IRQ_EVENT_OBJ_DESTROYED |
417 DPRC_IRQ_EVENT_OBJ_CREATED)) {
418 unsigned int irq_count;
420 error = dprc_scan_objects(mc_dev, &irq_count);
423 * If the error is -ENXIO, we ignore it, as it indicates
424 * that the object scan was aborted, as we detected that
425 * an object was removed from the DPRC in the MC, while
426 * we were scanning the DPRC.
428 if (error != -ENXIO) {
429 dev_err(dev, "dprc_scan_objects() failed: %d\n",
436 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
438 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
439 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
444 mutex_unlock(&mc_bus->scan_mutex);
449 * Disable and clear interrupt for a given DPRC object
451 static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
454 struct fsl_mc_io *mc_io = mc_dev->mc_io;
456 WARN_ON(mc_dev->obj_desc.irq_count != 1);
459 * Disable generation of interrupt, while we configure it:
461 error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
463 dev_err(&mc_dev->dev,
464 "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
470 * Disable all interrupt causes for the interrupt:
472 error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
474 dev_err(&mc_dev->dev,
475 "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
481 * Clear any leftover interrupts:
483 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
485 dev_err(&mc_dev->dev,
486 "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
494 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
497 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
499 WARN_ON(mc_dev->obj_desc.irq_count != 1);
502 * NOTE: devm_request_threaded_irq() invokes the device-specific
503 * function that programs the MSI physically in the device
505 error = devm_request_threaded_irq(&mc_dev->dev,
508 dprc_irq0_handler_thread,
509 IRQF_NO_SUSPEND | IRQF_ONESHOT,
510 dev_name(&mc_dev->dev),
513 dev_err(&mc_dev->dev,
514 "devm_request_threaded_irq() failed: %d\n",
522 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
527 * Enable all interrupt causes for the interrupt:
529 error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
532 dev_err(&mc_dev->dev,
533 "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
540 * Enable generation of the interrupt:
542 error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
544 dev_err(&mc_dev->dev,
545 "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
555 * Setup interrupt for a given DPRC device
557 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
561 error = fsl_mc_allocate_irqs(mc_dev);
565 error = disable_dprc_irq(mc_dev);
567 goto error_free_irqs;
569 error = register_dprc_irq_handler(mc_dev);
571 goto error_free_irqs;
573 error = enable_dprc_irq(mc_dev);
575 goto error_free_irqs;
580 fsl_mc_free_irqs(mc_dev);
585 * dprc_probe - callback invoked when a DPRC is being bound to this driver
587 * @mc_dev: Pointer to fsl-mc device representing a DPRC
589 * It opens the physical DPRC in the MC.
590 * It scans the DPRC to discover the MC objects contained in it.
591 * It creates the interrupt pool for the MC bus associated with the DPRC.
592 * It configures the interrupts for the DPRC device itself.
594 static int dprc_probe(struct fsl_mc_device *mc_dev)
598 struct device *parent_dev = mc_dev->dev.parent;
599 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
600 bool mc_io_created = false;
601 bool msi_domain_set = false;
602 u16 major_ver, minor_ver;
604 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
607 if (WARN_ON(dev_get_msi_domain(&mc_dev->dev)))
610 if (!mc_dev->mc_io) {
612 * This is a child DPRC:
614 if (WARN_ON(!dev_is_fsl_mc(parent_dev)))
617 if (WARN_ON(mc_dev->obj_desc.region_count == 0))
620 region_size = resource_size(mc_dev->regions);
622 error = fsl_create_mc_io(&mc_dev->dev,
623 mc_dev->regions[0].start,
626 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
631 mc_io_created = true;
634 * Inherit parent MSI domain:
636 dev_set_msi_domain(&mc_dev->dev,
637 dev_get_msi_domain(parent_dev));
638 msi_domain_set = true;
641 * This is a root DPRC
643 struct irq_domain *mc_msi_domain;
645 if (WARN_ON(dev_is_fsl_mc(parent_dev)))
648 error = fsl_mc_find_msi_domain(parent_dev,
651 dev_warn(&mc_dev->dev,
652 "WARNING: MC bus without interrupt support\n");
654 dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
655 msi_domain_set = true;
659 error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
662 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
663 goto error_cleanup_msi_domain;
666 error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
669 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
671 goto error_cleanup_open;
674 error = dprc_get_api_version(mc_dev->mc_io, 0,
678 dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
680 goto error_cleanup_open;
683 if (major_ver < DPRC_MIN_VER_MAJOR ||
684 (major_ver == DPRC_MIN_VER_MAJOR &&
685 minor_ver < DPRC_MIN_VER_MINOR)) {
686 dev_err(&mc_dev->dev,
687 "ERROR: DPRC version %d.%d not supported\n",
688 major_ver, minor_ver);
690 goto error_cleanup_open;
693 mutex_init(&mc_bus->scan_mutex);
696 * Discover MC objects in DPRC object:
698 error = dprc_scan_container(mc_dev);
700 goto error_cleanup_open;
703 * Configure interrupt for the DPRC object associated with this MC bus:
705 error = dprc_setup_irq(mc_dev);
707 goto error_cleanup_open;
709 dev_info(&mc_dev->dev, "DPRC device bound to driver");
713 (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
715 error_cleanup_msi_domain:
717 dev_set_msi_domain(&mc_dev->dev, NULL);
720 fsl_destroy_mc_io(mc_dev->mc_io);
721 mc_dev->mc_io = NULL;
728 * Tear down interrupt for a given DPRC object
730 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
732 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
734 (void)disable_dprc_irq(mc_dev);
736 devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
738 fsl_mc_free_irqs(mc_dev);
742 * dprc_remove - callback invoked when a DPRC is being unbound from this driver
744 * @mc_dev: Pointer to fsl-mc device representing the DPRC
746 * It removes the DPRC's child objects from Linux (not from the MC) and
747 * closes the DPRC device in the MC.
748 * It tears down the interrupts that were configured for the DPRC device.
749 * It destroys the interrupt pool associated with this MC bus.
751 static int dprc_remove(struct fsl_mc_device *mc_dev)
754 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
756 if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
758 if (WARN_ON(!mc_dev->mc_io))
761 if (WARN_ON(!mc_bus->irq_resources))
764 if (dev_get_msi_domain(&mc_dev->dev))
765 dprc_teardown_irq(mc_dev);
767 device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
769 if (dev_get_msi_domain(&mc_dev->dev)) {
770 fsl_mc_cleanup_irq_pool(mc_bus);
771 dev_set_msi_domain(&mc_dev->dev, NULL);
774 fsl_mc_cleanup_all_resource_pools(mc_dev);
776 error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
778 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
780 if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
781 fsl_destroy_mc_io(mc_dev->mc_io);
782 mc_dev->mc_io = NULL;
785 dev_info(&mc_dev->dev, "DPRC device unbound from driver");
789 static const struct fsl_mc_device_id match_id_table[] = {
791 .vendor = FSL_MC_VENDOR_FREESCALE,
796 static struct fsl_mc_driver dprc_driver = {
798 .name = FSL_MC_DPRC_DRIVER_NAME,
799 .owner = THIS_MODULE,
802 .match_id_table = match_id_table,
804 .remove = dprc_remove,
807 int __init dprc_driver_init(void)
809 return fsl_mc_driver_register(&dprc_driver);
812 void dprc_driver_exit(void)
814 fsl_mc_driver_unregister(&dprc_driver);