1 // SPDX-License-Identifier: GPL-2.0
3 * fsl-mc object allocator driver
5 * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
9 #include <linux/module.h>
10 #include <linux/msi.h>
11 #include <linux/fsl/mc.h>
13 #include "fsl-mc-private.h"
15 static bool __must_check fsl_mc_is_allocatable(struct fsl_mc_device *mc_dev)
17 return is_fsl_mc_bus_dpbp(mc_dev) ||
18 is_fsl_mc_bus_dpmcp(mc_dev) ||
19 is_fsl_mc_bus_dpcon(mc_dev);
23 * fsl_mc_resource_pool_add_device - add allocatable object to a resource
24 * pool of a given fsl-mc bus
26 * @mc_bus: pointer to the fsl-mc bus
27 * @pool_type: pool type
28 * @mc_dev: pointer to allocatable fsl-mc device
30 static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
37 struct fsl_mc_resource_pool *res_pool;
38 struct fsl_mc_resource *resource;
39 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
42 if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
44 if (!fsl_mc_is_allocatable(mc_dev))
49 res_pool = &mc_bus->resource_pools[pool_type];
50 if (res_pool->type != pool_type)
52 if (res_pool->mc_bus != mc_bus)
55 mutex_lock(&res_pool->mutex);
57 if (res_pool->max_count < 0)
59 if (res_pool->free_count < 0 ||
60 res_pool->free_count > res_pool->max_count)
63 resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource),
67 dev_err(&mc_bus_dev->dev,
68 "Failed to allocate memory for fsl_mc_resource\n");
72 resource->type = pool_type;
73 resource->id = mc_dev->obj_desc.id;
74 resource->data = mc_dev;
75 resource->parent_pool = res_pool;
76 INIT_LIST_HEAD(&resource->node);
77 list_add_tail(&resource->node, &res_pool->free_list);
78 mc_dev->resource = resource;
79 res_pool->free_count++;
80 res_pool->max_count++;
83 mutex_unlock(&res_pool->mutex);
89 * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
92 * @mc_dev: pointer to allocatable fsl-mc device
94 * It permanently removes an allocatable fsl-mc device from the resource
95 * pool. It's an error if the device is in use.
97 static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
100 struct fsl_mc_device *mc_bus_dev;
101 struct fsl_mc_bus *mc_bus;
102 struct fsl_mc_resource_pool *res_pool;
103 struct fsl_mc_resource *resource;
106 if (!fsl_mc_is_allocatable(mc_dev))
109 resource = mc_dev->resource;
110 if (!resource || resource->data != mc_dev)
113 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
114 mc_bus = to_fsl_mc_bus(mc_bus_dev);
115 res_pool = resource->parent_pool;
116 if (res_pool != &mc_bus->resource_pools[resource->type])
119 mutex_lock(&res_pool->mutex);
121 if (res_pool->max_count <= 0)
123 if (res_pool->free_count <= 0 ||
124 res_pool->free_count > res_pool->max_count)
128 * If the device is currently allocated, its resource is not
129 * in the free list and thus, the device cannot be removed.
131 if (list_empty(&resource->node)) {
133 dev_err(&mc_bus_dev->dev,
134 "Device %s cannot be removed from resource pool\n",
135 dev_name(&mc_dev->dev));
139 list_del_init(&resource->node);
140 res_pool->free_count--;
141 res_pool->max_count--;
143 devm_kfree(&mc_bus_dev->dev, resource);
144 mc_dev->resource = NULL;
147 mutex_unlock(&res_pool->mutex);
152 static const char *const fsl_mc_pool_type_strings[] = {
153 [FSL_MC_POOL_DPMCP] = "dpmcp",
154 [FSL_MC_POOL_DPBP] = "dpbp",
155 [FSL_MC_POOL_DPCON] = "dpcon",
156 [FSL_MC_POOL_IRQ] = "irq",
159 static int __must_check object_type_to_pool_type(const char *object_type,
160 enum fsl_mc_pool_type
165 for (i = 0; i < ARRAY_SIZE(fsl_mc_pool_type_strings); i++) {
166 if (strcmp(object_type, fsl_mc_pool_type_strings[i]) == 0) {
175 int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
176 enum fsl_mc_pool_type pool_type,
177 struct fsl_mc_resource **new_resource)
179 struct fsl_mc_resource_pool *res_pool;
180 struct fsl_mc_resource *resource;
181 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
184 BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
185 FSL_MC_NUM_POOL_TYPES);
187 *new_resource = NULL;
188 if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
191 res_pool = &mc_bus->resource_pools[pool_type];
192 if (res_pool->mc_bus != mc_bus)
195 mutex_lock(&res_pool->mutex);
196 resource = list_first_entry_or_null(&res_pool->free_list,
197 struct fsl_mc_resource, node);
201 dev_err(&mc_bus_dev->dev,
202 "No more resources of type %s left\n",
203 fsl_mc_pool_type_strings[pool_type]);
207 if (resource->type != pool_type)
209 if (resource->parent_pool != res_pool)
211 if (res_pool->free_count <= 0 ||
212 res_pool->free_count > res_pool->max_count)
215 list_del_init(&resource->node);
217 res_pool->free_count--;
220 mutex_unlock(&res_pool->mutex);
221 *new_resource = resource;
225 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
227 void fsl_mc_resource_free(struct fsl_mc_resource *resource)
229 struct fsl_mc_resource_pool *res_pool;
231 res_pool = resource->parent_pool;
232 if (resource->type != res_pool->type)
235 mutex_lock(&res_pool->mutex);
236 if (res_pool->free_count < 0 ||
237 res_pool->free_count >= res_pool->max_count)
240 if (!list_empty(&resource->node))
243 list_add_tail(&resource->node, &res_pool->free_list);
244 res_pool->free_count++;
246 mutex_unlock(&res_pool->mutex);
248 EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
251 * fsl_mc_object_allocate - Allocates an fsl-mc object of the given
252 * pool type from a given fsl-mc bus instance
254 * @mc_dev: fsl-mc device which is used in conjunction with the
256 * @pool_type: pool type
257 * @new_mc_dev: pointer to area where the pointer to the allocated device
260 * Allocatable objects are always used in conjunction with some functional
261 * device. This function allocates an object of the specified type from
262 * the DPRC containing the functional device.
264 * NOTE: pool_type must be different from FSL_MC_POOL_MCP, since MC
265 * portals are allocated using fsl_mc_portal_allocate(), instead of
268 int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
269 enum fsl_mc_pool_type pool_type,
270 struct fsl_mc_device **new_mc_adev)
272 struct fsl_mc_device *mc_bus_dev;
273 struct fsl_mc_bus *mc_bus;
274 struct fsl_mc_device *mc_adev;
276 struct fsl_mc_resource *resource = NULL;
279 if (mc_dev->flags & FSL_MC_IS_DPRC)
282 if (!dev_is_fsl_mc(mc_dev->dev.parent))
285 if (pool_type == FSL_MC_POOL_DPMCP)
288 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
289 mc_bus = to_fsl_mc_bus(mc_bus_dev);
290 error = fsl_mc_resource_allocate(mc_bus, pool_type, &resource);
294 mc_adev = resource->data;
298 mc_adev->consumer_link = device_link_add(&mc_dev->dev,
300 DL_FLAG_AUTOREMOVE_CONSUMER);
301 if (!mc_adev->consumer_link) {
306 *new_mc_adev = mc_adev;
310 fsl_mc_resource_free(resource);
314 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
317 * fsl_mc_object_free - Returns an fsl-mc object to the resource
318 * pool where it came from.
319 * @mc_adev: Pointer to the fsl-mc device
321 void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
323 struct fsl_mc_resource *resource;
325 resource = mc_adev->resource;
326 if (resource->type == FSL_MC_POOL_DPMCP)
328 if (resource->data != mc_adev)
331 fsl_mc_resource_free(resource);
333 device_link_del(mc_adev->consumer_link);
334 mc_adev->consumer_link = NULL;
336 EXPORT_SYMBOL_GPL(fsl_mc_object_free);
339 * A DPRC and the devices in the DPRC all share the same GIC-ITS device
340 * ID. A block of IRQs is pre-allocated and maintained in a pool
341 * from which devices can allocate them when needed.
345 * Initialize the interrupt pool associated with an fsl-mc bus.
346 * It allocates a block of IRQs from the GIC-ITS.
348 int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
349 unsigned int irq_count)
352 struct msi_desc *msi_desc;
353 struct fsl_mc_device_irq *irq_resources;
354 struct fsl_mc_device_irq *mc_dev_irq;
356 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
357 struct fsl_mc_resource_pool *res_pool =
358 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
360 if (irq_count == 0 ||
361 irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS)
364 error = fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev->dev, irq_count);
368 irq_resources = devm_kcalloc(&mc_bus_dev->dev,
369 irq_count, sizeof(*irq_resources),
371 if (!irq_resources) {
373 goto cleanup_msi_irqs;
376 for (i = 0; i < irq_count; i++) {
377 mc_dev_irq = &irq_resources[i];
380 * NOTE: This mc_dev_irq's MSI addr/value pair will be set
381 * by the fsl_mc_msi_write_msg() callback
383 mc_dev_irq->resource.type = res_pool->type;
384 mc_dev_irq->resource.data = mc_dev_irq;
385 mc_dev_irq->resource.parent_pool = res_pool;
386 INIT_LIST_HEAD(&mc_dev_irq->resource.node);
387 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list);
390 for_each_msi_entry(msi_desc, &mc_bus_dev->dev) {
391 mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index];
392 mc_dev_irq->msi_desc = msi_desc;
393 mc_dev_irq->resource.id = msi_desc->irq;
396 res_pool->max_count = irq_count;
397 res_pool->free_count = irq_count;
398 mc_bus->irq_resources = irq_resources;
402 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
405 EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool);
408 * Teardown the interrupt pool associated with an fsl-mc bus.
409 * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
411 void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus)
413 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
414 struct fsl_mc_resource_pool *res_pool =
415 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
417 if (!mc_bus->irq_resources)
420 if (res_pool->max_count == 0)
423 if (res_pool->free_count != res_pool->max_count)
426 INIT_LIST_HEAD(&res_pool->free_list);
427 res_pool->max_count = 0;
428 res_pool->free_count = 0;
429 mc_bus->irq_resources = NULL;
430 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
432 EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool);
435 * Allocate the IRQs required by a given fsl-mc device.
437 int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
441 int res_allocated_count = 0;
443 struct fsl_mc_device_irq **irqs = NULL;
444 struct fsl_mc_bus *mc_bus;
445 struct fsl_mc_resource_pool *res_pool;
450 irq_count = mc_dev->obj_desc.irq_count;
454 if (is_fsl_mc_bus_dprc(mc_dev))
455 mc_bus = to_fsl_mc_bus(mc_dev);
457 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
459 if (!mc_bus->irq_resources)
462 res_pool = &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
463 if (res_pool->free_count < irq_count) {
464 dev_err(&mc_dev->dev,
465 "Not able to allocate %u irqs for device\n", irq_count);
469 irqs = devm_kcalloc(&mc_dev->dev, irq_count, sizeof(irqs[0]),
474 for (i = 0; i < irq_count; i++) {
475 struct fsl_mc_resource *resource;
477 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_IRQ,
480 goto error_resource_alloc;
482 irqs[i] = to_fsl_mc_irq(resource);
483 res_allocated_count++;
485 irqs[i]->mc_dev = mc_dev;
486 irqs[i]->dev_irq_index = i;
492 error_resource_alloc:
493 for (i = 0; i < res_allocated_count; i++) {
494 irqs[i]->mc_dev = NULL;
495 fsl_mc_resource_free(&irqs[i]->resource);
500 EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs);
503 * Frees the IRQs that were allocated for an fsl-mc device.
505 void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev)
509 struct fsl_mc_bus *mc_bus;
510 struct fsl_mc_device_irq **irqs = mc_dev->irqs;
515 irq_count = mc_dev->obj_desc.irq_count;
517 if (is_fsl_mc_bus_dprc(mc_dev))
518 mc_bus = to_fsl_mc_bus(mc_dev);
520 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
522 if (!mc_bus->irq_resources)
525 for (i = 0; i < irq_count; i++) {
526 irqs[i]->mc_dev = NULL;
527 fsl_mc_resource_free(&irqs[i]->resource);
532 EXPORT_SYMBOL_GPL(fsl_mc_free_irqs);
534 void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
537 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
539 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
540 struct fsl_mc_resource_pool *res_pool =
541 &mc_bus->resource_pools[pool_type];
543 res_pool->type = pool_type;
544 res_pool->max_count = 0;
545 res_pool->free_count = 0;
546 res_pool->mc_bus = mc_bus;
547 INIT_LIST_HEAD(&res_pool->free_list);
548 mutex_init(&res_pool->mutex);
552 static void fsl_mc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
553 enum fsl_mc_pool_type pool_type)
555 struct fsl_mc_resource *resource;
556 struct fsl_mc_resource *next;
557 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
558 struct fsl_mc_resource_pool *res_pool =
559 &mc_bus->resource_pools[pool_type];
562 list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
564 devm_kfree(&mc_bus_dev->dev, resource);
568 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
572 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
573 fsl_mc_cleanup_resource_pool(mc_bus_dev, pool_type);
577 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
578 * being added to the system
580 static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
582 enum fsl_mc_pool_type pool_type;
583 struct fsl_mc_device *mc_bus_dev;
584 struct fsl_mc_bus *mc_bus;
587 if (!fsl_mc_is_allocatable(mc_dev))
590 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
591 if (!dev_is_fsl_mc(&mc_bus_dev->dev))
594 mc_bus = to_fsl_mc_bus(mc_bus_dev);
595 error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
599 error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
603 dev_dbg(&mc_dev->dev,
604 "Allocatable fsl-mc device bound to fsl_mc_allocator driver");
609 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
610 * being removed from the system
612 static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
616 if (!fsl_mc_is_allocatable(mc_dev))
619 if (mc_dev->resource) {
620 error = fsl_mc_resource_pool_remove_device(mc_dev);
625 dev_dbg(&mc_dev->dev,
626 "Allocatable fsl-mc device unbound from fsl_mc_allocator driver");
630 static const struct fsl_mc_device_id match_id_table[] = {
632 .vendor = FSL_MC_VENDOR_FREESCALE,
636 .vendor = FSL_MC_VENDOR_FREESCALE,
640 .vendor = FSL_MC_VENDOR_FREESCALE,
646 static struct fsl_mc_driver fsl_mc_allocator_driver = {
648 .name = "fsl_mc_allocator",
651 .match_id_table = match_id_table,
652 .probe = fsl_mc_allocator_probe,
653 .remove = fsl_mc_allocator_remove,
656 int __init fsl_mc_allocator_driver_init(void)
658 return fsl_mc_driver_register(&fsl_mc_allocator_driver);
661 void fsl_mc_allocator_driver_exit(void)
663 fsl_mc_driver_unregister(&fsl_mc_allocator_driver);