2 * fsl-mc object allocator driver
4 * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
11 #include <linux/module.h>
12 #include <linux/msi.h>
13 #include "../include/mc.h"
15 #include "fsl-mc-private.h"
17 static bool __must_check fsl_mc_is_allocatable(const char *obj_type)
19 return strcmp(obj_type, "dpbp") == 0 ||
20 strcmp(obj_type, "dpmcp") == 0 ||
21 strcmp(obj_type, "dpcon") == 0;
25 * fsl_mc_resource_pool_add_device - add allocatable object to a resource
26 * pool of a given fsl-mc bus
28 * @mc_bus: pointer to the fsl-mc bus
29 * @pool_type: pool type
30 * @mc_dev: pointer to allocatable fsl-mc device
32 static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
39 struct fsl_mc_resource_pool *res_pool;
40 struct fsl_mc_resource *resource;
41 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
44 if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
46 if (WARN_ON(!fsl_mc_is_allocatable(mc_dev->obj_desc.type)))
48 if (WARN_ON(mc_dev->resource))
51 res_pool = &mc_bus->resource_pools[pool_type];
52 if (WARN_ON(res_pool->type != pool_type))
54 if (WARN_ON(res_pool->mc_bus != mc_bus))
57 mutex_lock(&res_pool->mutex);
59 if (WARN_ON(res_pool->max_count < 0))
61 if (WARN_ON(res_pool->free_count < 0 ||
62 res_pool->free_count > res_pool->max_count))
65 resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource),
69 dev_err(&mc_bus_dev->dev,
70 "Failed to allocate memory for fsl_mc_resource\n");
74 resource->type = pool_type;
75 resource->id = mc_dev->obj_desc.id;
76 resource->data = mc_dev;
77 resource->parent_pool = res_pool;
78 INIT_LIST_HEAD(&resource->node);
79 list_add_tail(&resource->node, &res_pool->free_list);
80 mc_dev->resource = resource;
81 res_pool->free_count++;
82 res_pool->max_count++;
85 mutex_unlock(&res_pool->mutex);
91 * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
94 * @mc_dev: pointer to allocatable fsl-mc device
96 * It permanently removes an allocatable fsl-mc device from the resource
97 * pool. It's an error if the device is in use.
99 static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
102 struct fsl_mc_device *mc_bus_dev;
103 struct fsl_mc_bus *mc_bus;
104 struct fsl_mc_resource_pool *res_pool;
105 struct fsl_mc_resource *resource;
108 if (WARN_ON(!fsl_mc_is_allocatable(mc_dev->obj_desc.type)))
111 resource = mc_dev->resource;
112 if (WARN_ON(!resource || resource->data != mc_dev))
115 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
116 mc_bus = to_fsl_mc_bus(mc_bus_dev);
117 res_pool = resource->parent_pool;
118 if (WARN_ON(res_pool != &mc_bus->resource_pools[resource->type]))
121 mutex_lock(&res_pool->mutex);
123 if (WARN_ON(res_pool->max_count <= 0))
125 if (WARN_ON(res_pool->free_count <= 0 ||
126 res_pool->free_count > res_pool->max_count))
130 * If the device is currently allocated, its resource is not
131 * in the free list and thus, the device cannot be removed.
133 if (list_empty(&resource->node)) {
135 dev_err(&mc_bus_dev->dev,
136 "Device %s cannot be removed from resource pool\n",
137 dev_name(&mc_dev->dev));
141 list_del_init(&resource->node);
142 res_pool->free_count--;
143 res_pool->max_count--;
145 devm_kfree(&mc_bus_dev->dev, resource);
146 mc_dev->resource = NULL;
149 mutex_unlock(&res_pool->mutex);
154 static const char *const fsl_mc_pool_type_strings[] = {
155 [FSL_MC_POOL_DPMCP] = "dpmcp",
156 [FSL_MC_POOL_DPBP] = "dpbp",
157 [FSL_MC_POOL_DPCON] = "dpcon",
158 [FSL_MC_POOL_IRQ] = "irq",
161 static int __must_check object_type_to_pool_type(const char *object_type,
162 enum fsl_mc_pool_type
167 for (i = 0; i < ARRAY_SIZE(fsl_mc_pool_type_strings); i++) {
168 if (strcmp(object_type, fsl_mc_pool_type_strings[i]) == 0) {
177 int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
178 enum fsl_mc_pool_type pool_type,
179 struct fsl_mc_resource **new_resource)
181 struct fsl_mc_resource_pool *res_pool;
182 struct fsl_mc_resource *resource;
183 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
186 BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
187 FSL_MC_NUM_POOL_TYPES);
189 *new_resource = NULL;
190 if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
193 res_pool = &mc_bus->resource_pools[pool_type];
194 if (WARN_ON(res_pool->mc_bus != mc_bus))
197 mutex_lock(&res_pool->mutex);
198 resource = list_first_entry_or_null(&res_pool->free_list,
199 struct fsl_mc_resource, node);
202 WARN_ON(res_pool->free_count != 0);
204 dev_err(&mc_bus_dev->dev,
205 "No more resources of type %s left\n",
206 fsl_mc_pool_type_strings[pool_type]);
210 if (WARN_ON(resource->type != pool_type))
212 if (WARN_ON(resource->parent_pool != res_pool))
214 if (WARN_ON(res_pool->free_count <= 0 ||
215 res_pool->free_count > res_pool->max_count))
218 list_del_init(&resource->node);
220 res_pool->free_count--;
223 mutex_unlock(&res_pool->mutex);
224 *new_resource = resource;
228 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
230 void fsl_mc_resource_free(struct fsl_mc_resource *resource)
232 struct fsl_mc_resource_pool *res_pool;
234 res_pool = resource->parent_pool;
235 if (WARN_ON(resource->type != res_pool->type))
238 mutex_lock(&res_pool->mutex);
239 if (WARN_ON(res_pool->free_count < 0 ||
240 res_pool->free_count >= res_pool->max_count))
243 if (WARN_ON(!list_empty(&resource->node)))
246 list_add_tail(&resource->node, &res_pool->free_list);
247 res_pool->free_count++;
249 mutex_unlock(&res_pool->mutex);
251 EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
254 * fsl_mc_object_allocate - Allocates an fsl-mc object of the given
255 * pool type from a given fsl-mc bus instance
257 * @mc_dev: fsl-mc device which is used in conjunction with the
259 * @pool_type: pool type
260 * @new_mc_dev: pointer to area where the pointer to the allocated device
263 * Allocatable objects are always used in conjunction with some functional
264 * device. This function allocates an object of the specified type from
265 * the DPRC containing the functional device.
267 * NOTE: pool_type must be different from FSL_MC_POOL_MCP, since MC
268 * portals are allocated using fsl_mc_portal_allocate(), instead of
271 int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
272 enum fsl_mc_pool_type pool_type,
273 struct fsl_mc_device **new_mc_adev)
275 struct fsl_mc_device *mc_bus_dev;
276 struct fsl_mc_bus *mc_bus;
277 struct fsl_mc_device *mc_adev;
279 struct fsl_mc_resource *resource = NULL;
282 if (WARN_ON(mc_dev->flags & FSL_MC_IS_DPRC))
285 if (WARN_ON(!dev_is_fsl_mc(mc_dev->dev.parent)))
288 if (WARN_ON(pool_type == FSL_MC_POOL_DPMCP))
291 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
292 mc_bus = to_fsl_mc_bus(mc_bus_dev);
293 error = fsl_mc_resource_allocate(mc_bus, pool_type, &resource);
297 mc_adev = resource->data;
298 if (WARN_ON(!mc_adev))
301 *new_mc_adev = mc_adev;
305 fsl_mc_resource_free(resource);
309 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
312 * fsl_mc_object_free - Returns an fsl-mc object to the resource
313 * pool where it came from.
314 * @mc_adev: Pointer to the fsl-mc device
316 void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
318 struct fsl_mc_resource *resource;
320 resource = mc_adev->resource;
321 if (WARN_ON(resource->type == FSL_MC_POOL_DPMCP))
323 if (WARN_ON(resource->data != mc_adev))
326 fsl_mc_resource_free(resource);
328 EXPORT_SYMBOL_GPL(fsl_mc_object_free);
331 * A DPRC and the devices in the DPRC all share the same GIC-ITS device
332 * ID. A block of IRQs is pre-allocated and maintained in a pool
333 * from which devices can allocate them when needed.
337 * Initialize the interrupt pool associated with an fsl-mc bus.
338 * It allocates a block of IRQs from the GIC-ITS.
340 int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
341 unsigned int irq_count)
344 struct msi_desc *msi_desc;
345 struct fsl_mc_device_irq *irq_resources;
346 struct fsl_mc_device_irq *mc_dev_irq;
348 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
349 struct fsl_mc_resource_pool *res_pool =
350 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
352 if (WARN_ON(irq_count == 0 ||
353 irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS))
356 error = fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev->dev, irq_count);
360 irq_resources = devm_kzalloc(&mc_bus_dev->dev,
361 sizeof(*irq_resources) * irq_count,
363 if (!irq_resources) {
365 goto cleanup_msi_irqs;
368 for (i = 0; i < irq_count; i++) {
369 mc_dev_irq = &irq_resources[i];
372 * NOTE: This mc_dev_irq's MSI addr/value pair will be set
373 * by the fsl_mc_msi_write_msg() callback
375 mc_dev_irq->resource.type = res_pool->type;
376 mc_dev_irq->resource.data = mc_dev_irq;
377 mc_dev_irq->resource.parent_pool = res_pool;
378 INIT_LIST_HEAD(&mc_dev_irq->resource.node);
379 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list);
382 for_each_msi_entry(msi_desc, &mc_bus_dev->dev) {
383 mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index];
384 mc_dev_irq->msi_desc = msi_desc;
385 mc_dev_irq->resource.id = msi_desc->irq;
388 res_pool->max_count = irq_count;
389 res_pool->free_count = irq_count;
390 mc_bus->irq_resources = irq_resources;
394 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
397 EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool);
400 * Teardown the interrupt pool associated with an fsl-mc bus.
401 * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
403 void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus)
405 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
406 struct fsl_mc_resource_pool *res_pool =
407 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
409 if (WARN_ON(!mc_bus->irq_resources))
412 if (WARN_ON(res_pool->max_count == 0))
415 if (WARN_ON(res_pool->free_count != res_pool->max_count))
418 INIT_LIST_HEAD(&res_pool->free_list);
419 res_pool->max_count = 0;
420 res_pool->free_count = 0;
421 mc_bus->irq_resources = NULL;
422 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
424 EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool);
427 * Allocate the IRQs required by a given fsl-mc device.
429 int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
433 int res_allocated_count = 0;
435 struct fsl_mc_device_irq **irqs = NULL;
436 struct fsl_mc_bus *mc_bus;
437 struct fsl_mc_resource_pool *res_pool;
439 if (WARN_ON(mc_dev->irqs))
442 irq_count = mc_dev->obj_desc.irq_count;
443 if (WARN_ON(irq_count == 0))
446 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
447 mc_bus = to_fsl_mc_bus(mc_dev);
449 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
451 if (WARN_ON(!mc_bus->irq_resources))
454 res_pool = &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
455 if (res_pool->free_count < irq_count) {
456 dev_err(&mc_dev->dev,
457 "Not able to allocate %u irqs for device\n", irq_count);
461 irqs = devm_kzalloc(&mc_dev->dev, irq_count * sizeof(irqs[0]),
466 for (i = 0; i < irq_count; i++) {
467 struct fsl_mc_resource *resource;
469 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_IRQ,
472 goto error_resource_alloc;
474 irqs[i] = to_fsl_mc_irq(resource);
475 res_allocated_count++;
477 WARN_ON(irqs[i]->mc_dev);
478 irqs[i]->mc_dev = mc_dev;
479 irqs[i]->dev_irq_index = i;
485 error_resource_alloc:
486 for (i = 0; i < res_allocated_count; i++) {
487 irqs[i]->mc_dev = NULL;
488 fsl_mc_resource_free(&irqs[i]->resource);
493 EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs);
496 * Frees the IRQs that were allocated for an fsl-mc device.
498 void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev)
502 struct fsl_mc_bus *mc_bus;
503 struct fsl_mc_device_irq **irqs = mc_dev->irqs;
508 irq_count = mc_dev->obj_desc.irq_count;
510 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
511 mc_bus = to_fsl_mc_bus(mc_dev);
513 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
515 if (WARN_ON(!mc_bus->irq_resources))
518 for (i = 0; i < irq_count; i++) {
519 WARN_ON(!irqs[i]->mc_dev);
520 irqs[i]->mc_dev = NULL;
521 fsl_mc_resource_free(&irqs[i]->resource);
526 EXPORT_SYMBOL_GPL(fsl_mc_free_irqs);
528 void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
531 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
533 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
534 struct fsl_mc_resource_pool *res_pool =
535 &mc_bus->resource_pools[pool_type];
537 res_pool->type = pool_type;
538 res_pool->max_count = 0;
539 res_pool->free_count = 0;
540 res_pool->mc_bus = mc_bus;
541 INIT_LIST_HEAD(&res_pool->free_list);
542 mutex_init(&res_pool->mutex);
546 static void fsl_mc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
547 enum fsl_mc_pool_type pool_type)
549 struct fsl_mc_resource *resource;
550 struct fsl_mc_resource *next;
551 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
552 struct fsl_mc_resource_pool *res_pool =
553 &mc_bus->resource_pools[pool_type];
556 WARN_ON(res_pool->type != pool_type);
557 WARN_ON(res_pool->free_count != res_pool->max_count);
559 list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
561 WARN_ON(resource->type != res_pool->type);
562 WARN_ON(resource->parent_pool != res_pool);
563 devm_kfree(&mc_bus_dev->dev, resource);
566 WARN_ON(free_count != res_pool->free_count);
569 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
573 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
574 fsl_mc_cleanup_resource_pool(mc_bus_dev, pool_type);
578 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
579 * being added to the system
581 static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
583 enum fsl_mc_pool_type pool_type;
584 struct fsl_mc_device *mc_bus_dev;
585 struct fsl_mc_bus *mc_bus;
588 if (WARN_ON(!fsl_mc_is_allocatable(mc_dev->obj_desc.type)))
591 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
592 if (WARN_ON(!dev_is_fsl_mc(&mc_bus_dev->dev)))
595 mc_bus = to_fsl_mc_bus(mc_bus_dev);
596 error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
600 error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
604 dev_dbg(&mc_dev->dev,
605 "Allocatable fsl-mc device bound to fsl_mc_allocator driver");
610 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
611 * being removed from the system
613 static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
617 if (WARN_ON(!fsl_mc_is_allocatable(mc_dev->obj_desc.type)))
620 if (mc_dev->resource) {
621 error = fsl_mc_resource_pool_remove_device(mc_dev);
626 dev_dbg(&mc_dev->dev,
627 "Allocatable fsl-mc device unbound from fsl_mc_allocator driver");
631 static const struct fsl_mc_device_id match_id_table[] = {
633 .vendor = FSL_MC_VENDOR_FREESCALE,
637 .vendor = FSL_MC_VENDOR_FREESCALE,
641 .vendor = FSL_MC_VENDOR_FREESCALE,
647 static struct fsl_mc_driver fsl_mc_allocator_driver = {
649 .name = "fsl_mc_allocator",
652 .match_id_table = match_id_table,
653 .probe = fsl_mc_allocator_probe,
654 .remove = fsl_mc_allocator_remove,
657 int __init fsl_mc_allocator_driver_init(void)
659 return fsl_mc_driver_register(&fsl_mc_allocator_driver);
662 void fsl_mc_allocator_driver_exit(void)
664 fsl_mc_driver_unregister(&fsl_mc_allocator_driver);