1 // SPDX-License-Identifier: GPL-2.0
3 * Freescale data path resource container (DPRC) driver
5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
6 * Copyright 2019-2020 NXP
7 * Author: German Rivera <German.Rivera@freescale.com>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/msi.h>
15 #include <linux/fsl/mc.h>
17 #include "fsl-mc-private.h"
19 #define FSL_MC_DPRC_DRIVER_NAME "fsl_mc_dprc"
21 struct fsl_mc_child_objs {
23 struct fsl_mc_obj_desc *child_array;
26 static bool fsl_mc_device_match(struct fsl_mc_device *mc_dev,
27 struct fsl_mc_obj_desc *obj_desc)
29 return mc_dev->obj_desc.id == obj_desc->id &&
30 strcmp(mc_dev->obj_desc.type, obj_desc->type) == 0;
33 static bool fsl_mc_obj_desc_is_allocatable(struct fsl_mc_obj_desc *obj)
35 if (strcmp(obj->type, "dpmcp") == 0 ||
36 strcmp(obj->type, "dpcon") == 0 ||
37 strcmp(obj->type, "dpbp") == 0)
43 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
46 struct fsl_mc_child_objs *objs;
47 struct fsl_mc_device *mc_dev;
49 mc_dev = to_fsl_mc_device(dev);
52 for (i = 0; i < objs->child_count; i++) {
53 struct fsl_mc_obj_desc *obj_desc = &objs->child_array[i];
55 if (strlen(obj_desc->type) != 0 &&
56 fsl_mc_device_match(mc_dev, obj_desc))
60 if (i == objs->child_count)
61 fsl_mc_device_remove(mc_dev);
66 static int __fsl_mc_device_remove(struct device *dev, void *data)
68 fsl_mc_device_remove(to_fsl_mc_device(dev));
73 * dprc_remove_devices - Removes devices for objects removed from a DPRC
75 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
76 * @obj_desc_array: array of object descriptors for child objects currently
77 * present in the DPRC in the MC.
78 * @num_child_objects_in_mc: number of entries in obj_desc_array
80 * Synchronizes the state of the Linux bus driver with the actual state of
81 * the MC by removing devices that represent MC objects that have
82 * been dynamically removed in the physical DPRC.
84 void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
85 struct fsl_mc_obj_desc *obj_desc_array,
86 int num_child_objects_in_mc)
88 if (num_child_objects_in_mc != 0) {
90 * Remove child objects that are in the DPRC in Linux,
93 struct fsl_mc_child_objs objs;
95 objs.child_count = num_child_objects_in_mc;
96 objs.child_array = obj_desc_array;
97 device_for_each_child(&mc_bus_dev->dev, &objs,
98 __fsl_mc_device_remove_if_not_in_mc);
101 * There are no child objects for this DPRC in the MC.
102 * So, remove all the child devices from Linux:
104 device_for_each_child(&mc_bus_dev->dev, NULL,
105 __fsl_mc_device_remove);
108 EXPORT_SYMBOL_GPL(dprc_remove_devices);
110 static int __fsl_mc_device_match(struct device *dev, void *data)
112 struct fsl_mc_obj_desc *obj_desc = data;
113 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
115 return fsl_mc_device_match(mc_dev, obj_desc);
118 struct fsl_mc_device *fsl_mc_device_lookup(struct fsl_mc_obj_desc *obj_desc,
119 struct fsl_mc_device *mc_bus_dev)
123 dev = device_find_child(&mc_bus_dev->dev, obj_desc,
124 __fsl_mc_device_match);
126 return dev ? to_fsl_mc_device(dev) : NULL;
130 * check_plugged_state_change - Check change in an MC object's plugged state
132 * @mc_dev: pointer to the fsl-mc device for a given MC object
133 * @obj_desc: pointer to the MC object's descriptor in the MC
135 * If the plugged state has changed from unplugged to plugged, the fsl-mc
136 * device is bound to the corresponding device driver.
137 * If the plugged state has changed from plugged to unplugged, the fsl-mc
138 * device is unbound from the corresponding device driver.
140 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
141 struct fsl_mc_obj_desc *obj_desc)
144 u32 plugged_flag_at_mc =
145 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 if (plugged_flag_at_mc) {
150 mc_dev->obj_desc.state |= FSL_MC_OBJ_STATE_PLUGGED;
151 error = device_attach(&mc_dev->dev);
153 dev_err(&mc_dev->dev,
154 "device_attach() failed: %d\n",
158 mc_dev->obj_desc.state &= ~FSL_MC_OBJ_STATE_PLUGGED;
159 device_release_driver(&mc_dev->dev);
164 static void fsl_mc_obj_device_add(struct fsl_mc_device *mc_bus_dev,
165 struct fsl_mc_obj_desc *obj_desc)
168 struct fsl_mc_device *child_dev;
171 * Check if device is already known to Linux:
173 child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
175 check_plugged_state_change(child_dev, obj_desc);
176 put_device(&child_dev->dev);
178 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
186 * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
188 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
189 * @obj_desc_array: array of device descriptors for child devices currently
190 * present in the physical DPRC.
191 * @num_child_objects_in_mc: number of entries in obj_desc_array
193 * Synchronizes the state of the Linux bus driver with the actual
194 * state of the MC by adding objects that have been newly discovered
195 * in the physical DPRC.
197 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
198 struct fsl_mc_obj_desc *obj_desc_array,
199 int num_child_objects_in_mc)
203 /* probe the allocable objects first */
204 for (i = 0; i < num_child_objects_in_mc; i++) {
205 struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
207 if (strlen(obj_desc->type) > 0 &&
208 fsl_mc_obj_desc_is_allocatable(obj_desc))
209 fsl_mc_obj_device_add(mc_bus_dev, obj_desc);
212 for (i = 0; i < num_child_objects_in_mc; i++) {
213 struct fsl_mc_obj_desc *obj_desc = &obj_desc_array[i];
215 if (strlen(obj_desc->type) > 0 &&
216 !fsl_mc_obj_desc_is_allocatable(obj_desc))
217 fsl_mc_obj_device_add(mc_bus_dev, obj_desc);
222 * dprc_scan_objects - Discover objects in a DPRC
224 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
225 * @alloc_interrupts: if true the function allocates the interrupt pool,
226 * otherwise the interrupt allocation is delayed
228 * Detects objects added and removed from a DPRC and synchronizes the
229 * state of the Linux bus driver, MC by adding and removing
230 * devices accordingly.
231 * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
232 * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
233 * All allocatable devices needed to be probed before all non-allocatable
234 * devices, to ensure that device drivers for non-allocatable
235 * devices can allocate any type of allocatable devices.
236 * That is, we need to ensure that the corresponding resource pools are
237 * populated before they can get allocation requests from probe callbacks
238 * of the device drivers for the non-allocatable devices.
240 static int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
241 bool alloc_interrupts)
243 int num_child_objects;
244 int dprc_get_obj_failures;
246 unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
247 struct fsl_mc_obj_desc *child_obj_desc_array = NULL;
248 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
250 error = dprc_get_obj_count(mc_bus_dev->mc_io,
252 mc_bus_dev->mc_handle,
255 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
260 if (num_child_objects != 0) {
263 child_obj_desc_array =
264 devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
265 sizeof(*child_obj_desc_array),
267 if (!child_obj_desc_array)
271 * Discover objects currently present in the physical DPRC:
273 dprc_get_obj_failures = 0;
274 for (i = 0; i < num_child_objects; i++) {
275 struct fsl_mc_obj_desc *obj_desc =
276 &child_obj_desc_array[i];
278 error = dprc_get_obj(mc_bus_dev->mc_io,
280 mc_bus_dev->mc_handle,
283 dev_err(&mc_bus_dev->dev,
284 "dprc_get_obj(i=%d) failed: %d\n",
287 * Mark the obj entry as "invalid", by using the
288 * empty string as obj type:
290 obj_desc->type[0] = '\0';
291 obj_desc->id = error;
292 dprc_get_obj_failures++;
297 * add a quirk for all versions of dpsec < 4.0...none
298 * are coherent regardless of what the MC reports.
300 if ((strcmp(obj_desc->type, "dpseci") == 0) &&
301 (obj_desc->ver_major < 4))
303 FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY;
305 irq_count += obj_desc->irq_count;
306 dev_dbg(&mc_bus_dev->dev,
307 "Discovered object: type %s, id %d\n",
308 obj_desc->type, obj_desc->id);
311 if (dprc_get_obj_failures != 0) {
312 dev_err(&mc_bus_dev->dev,
313 "%d out of %d devices could not be retrieved\n",
314 dprc_get_obj_failures, num_child_objects);
319 * Allocate IRQ's before binding the scanned devices with their
320 * respective drivers.
322 if (dev_get_msi_domain(&mc_bus_dev->dev)) {
323 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
324 dev_warn(&mc_bus_dev->dev,
325 "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
326 irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
329 if (alloc_interrupts && !mc_bus->irq_resources) {
330 error = fsl_mc_populate_irq_pool(mc_bus_dev,
331 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
337 dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
340 dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
343 if (child_obj_desc_array)
344 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
350 * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
352 * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
354 * Scans the physical DPRC and synchronizes the state of the Linux
355 * bus driver with the actual state of the MC by adding and removing
356 * devices as appropriate.
358 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev,
359 bool alloc_interrupts)
362 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
364 fsl_mc_init_all_resource_pools(mc_bus_dev);
367 * Discover objects in the DPRC:
369 mutex_lock(&mc_bus->scan_mutex);
370 error = dprc_scan_objects(mc_bus_dev, alloc_interrupts);
371 mutex_unlock(&mc_bus->scan_mutex);
375 EXPORT_SYMBOL_GPL(dprc_scan_container);
377 * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
379 * @irq: IRQ number of the interrupt being handled
380 * @arg: Pointer to device structure
382 static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
384 return IRQ_WAKE_THREAD;
388 * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
390 * @irq: IRQ number of the interrupt being handled
391 * @arg: Pointer to device structure
393 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
397 struct device *dev = arg;
398 struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
399 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
400 struct fsl_mc_io *mc_io = mc_dev->mc_io;
401 struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
403 dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
404 irq_num, smp_processor_id());
406 if (!(mc_dev->flags & FSL_MC_IS_DPRC))
409 mutex_lock(&mc_bus->scan_mutex);
410 if (!msi_desc || msi_desc->irq != (u32)irq_num)
414 error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
418 "dprc_get_irq_status() failed: %d\n", error);
422 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
426 "dprc_clear_irq_status() failed: %d\n", error);
430 if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
431 DPRC_IRQ_EVENT_OBJ_REMOVED |
432 DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
433 DPRC_IRQ_EVENT_OBJ_DESTROYED |
434 DPRC_IRQ_EVENT_OBJ_CREATED)) {
436 error = dprc_scan_objects(mc_dev, true);
439 * If the error is -ENXIO, we ignore it, as it indicates
440 * that the object scan was aborted, as we detected that
441 * an object was removed from the DPRC in the MC, while
442 * we were scanning the DPRC.
444 if (error != -ENXIO) {
445 dev_err(dev, "dprc_scan_objects() failed: %d\n",
454 mutex_unlock(&mc_bus->scan_mutex);
459 * Disable and clear interrupt for a given DPRC object
461 static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
464 struct fsl_mc_io *mc_io = mc_dev->mc_io;
467 * Disable generation of interrupt, while we configure it:
469 error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
471 dev_err(&mc_dev->dev,
472 "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
478 * Disable all interrupt causes for the interrupt:
480 error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
482 dev_err(&mc_dev->dev,
483 "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
489 * Clear any leftover interrupts:
491 error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
493 dev_err(&mc_dev->dev,
494 "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
502 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
505 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
508 * NOTE: devm_request_threaded_irq() invokes the device-specific
509 * function that programs the MSI physically in the device
511 error = devm_request_threaded_irq(&mc_dev->dev,
514 dprc_irq0_handler_thread,
515 IRQF_NO_SUSPEND | IRQF_ONESHOT,
516 dev_name(&mc_dev->dev),
519 dev_err(&mc_dev->dev,
520 "devm_request_threaded_irq() failed: %d\n",
528 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
533 * Enable all interrupt causes for the interrupt:
535 error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
538 dev_err(&mc_dev->dev,
539 "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
546 * Enable generation of the interrupt:
548 error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
550 dev_err(&mc_dev->dev,
551 "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
561 * Setup interrupt for a given DPRC device
563 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
567 error = fsl_mc_allocate_irqs(mc_dev);
571 error = disable_dprc_irq(mc_dev);
573 goto error_free_irqs;
575 error = register_dprc_irq_handler(mc_dev);
577 goto error_free_irqs;
579 error = enable_dprc_irq(mc_dev);
581 goto error_free_irqs;
586 fsl_mc_free_irqs(mc_dev);
591 * dprc_setup - opens and creates a mc_io for DPRC
593 * @mc_dev: Pointer to fsl-mc device representing a DPRC
595 * It opens the physical DPRC in the MC.
596 * It configures the DPRC portal used to communicate with MC
599 int dprc_setup(struct fsl_mc_device *mc_dev)
601 struct device *parent_dev = mc_dev->dev.parent;
602 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
603 struct irq_domain *mc_msi_domain;
604 bool mc_io_created = false;
605 bool msi_domain_set = false;
606 u16 major_ver, minor_ver;
610 if (!is_fsl_mc_bus_dprc(mc_dev))
613 if (dev_get_msi_domain(&mc_dev->dev))
616 if (!mc_dev->mc_io) {
618 * This is a child DPRC:
620 if (!dev_is_fsl_mc(parent_dev))
623 if (mc_dev->obj_desc.region_count == 0)
626 region_size = resource_size(mc_dev->regions);
628 error = fsl_create_mc_io(&mc_dev->dev,
629 mc_dev->regions[0].start,
632 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
637 mc_io_created = true;
640 mc_msi_domain = fsl_mc_find_msi_domain(&mc_dev->dev);
641 if (!mc_msi_domain) {
642 dev_warn(&mc_dev->dev,
643 "WARNING: MC bus without interrupt support\n");
645 dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
646 msi_domain_set = true;
649 error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
652 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
653 goto error_cleanup_msi_domain;
656 error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
659 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
661 goto error_cleanup_open;
664 error = dprc_get_api_version(mc_dev->mc_io, 0,
668 dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
670 goto error_cleanup_open;
673 if (major_ver < DPRC_MIN_VER_MAJOR ||
674 (major_ver == DPRC_MIN_VER_MAJOR &&
675 minor_ver < DPRC_MIN_VER_MINOR)) {
676 dev_err(&mc_dev->dev,
677 "ERROR: DPRC version %d.%d not supported\n",
678 major_ver, minor_ver);
680 goto error_cleanup_open;
686 (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
688 error_cleanup_msi_domain:
690 dev_set_msi_domain(&mc_dev->dev, NULL);
693 fsl_destroy_mc_io(mc_dev->mc_io);
694 mc_dev->mc_io = NULL;
699 EXPORT_SYMBOL_GPL(dprc_setup);
702 * dprc_probe - callback invoked when a DPRC is being bound to this driver
704 * @mc_dev: Pointer to fsl-mc device representing a DPRC
706 * It opens the physical DPRC in the MC.
707 * It scans the DPRC to discover the MC objects contained in it.
708 * It creates the interrupt pool for the MC bus associated with the DPRC.
709 * It configures the interrupts for the DPRC device itself.
711 static int dprc_probe(struct fsl_mc_device *mc_dev)
715 error = dprc_setup(mc_dev);
720 * Discover MC objects in DPRC object:
722 error = dprc_scan_container(mc_dev, true);
727 * Configure interrupt for the DPRC object associated with this MC bus:
729 error = dprc_setup_irq(mc_dev);
733 dev_info(&mc_dev->dev, "DPRC device bound to driver");
737 device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
739 dprc_cleanup(mc_dev);
744 * Tear down interrupt for a given DPRC object
746 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
748 struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
750 (void)disable_dprc_irq(mc_dev);
752 devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
754 fsl_mc_free_irqs(mc_dev);
758 * dprc_cleanup - function that cleanups a DPRC
760 * @mc_dev: Pointer to fsl-mc device representing the DPRC
762 * It closes the DPRC device in the MC.
763 * It destroys the interrupt pool associated with this MC bus.
766 int dprc_cleanup(struct fsl_mc_device *mc_dev)
770 /* this function should be called only for DPRCs, it
771 * is an error to call it for regular objects
773 if (!is_fsl_mc_bus_dprc(mc_dev))
776 if (dev_get_msi_domain(&mc_dev->dev)) {
777 fsl_mc_cleanup_irq_pool(mc_dev);
778 dev_set_msi_domain(&mc_dev->dev, NULL);
781 fsl_mc_cleanup_all_resource_pools(mc_dev);
783 /* if this step fails we cannot go further with cleanup as there is no way of
784 * communicating with the firmware
786 if (!mc_dev->mc_io) {
787 dev_err(&mc_dev->dev, "mc_io is NULL, tear down cannot be performed in firmware\n");
791 error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
793 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
795 if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
796 fsl_destroy_mc_io(mc_dev->mc_io);
797 mc_dev->mc_io = NULL;
802 EXPORT_SYMBOL_GPL(dprc_cleanup);
805 * dprc_remove - callback invoked when a DPRC is being unbound from this driver
807 * @mc_dev: Pointer to fsl-mc device representing the DPRC
809 * It removes the DPRC's child objects from Linux (not from the MC) and
810 * closes the DPRC device in the MC.
811 * It tears down the interrupts that were configured for the DPRC device.
812 * It destroys the interrupt pool associated with this MC bus.
814 static int dprc_remove(struct fsl_mc_device *mc_dev)
816 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
818 if (!is_fsl_mc_bus_dprc(mc_dev))
821 if (!mc_bus->irq_resources)
824 if (dev_get_msi_domain(&mc_dev->dev))
825 dprc_teardown_irq(mc_dev);
827 device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
829 dprc_cleanup(mc_dev);
831 dev_info(&mc_dev->dev, "DPRC device unbound from driver");
835 static const struct fsl_mc_device_id match_id_table[] = {
837 .vendor = FSL_MC_VENDOR_FREESCALE,
842 static struct fsl_mc_driver dprc_driver = {
844 .name = FSL_MC_DPRC_DRIVER_NAME,
845 .owner = THIS_MODULE,
848 .match_id_table = match_id_table,
850 .remove = dprc_remove,
853 int __init dprc_driver_init(void)
855 return fsl_mc_driver_register(&dprc_driver);
858 void dprc_driver_exit(void)
860 fsl_mc_driver_unregister(&dprc_driver);