2 * Freescale MC object device allocator driver
4 * Copyright (C) 2013 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 "../include/mc-private.h"
12 #include "../include/mc-sys.h"
13 #include <linux/module.h>
14 #include "../include/dpbp-cmd.h"
15 #include "../include/dpcon-cmd.h"
16 #include "dpmcp-cmd.h"
18 #include <linux/msi.h>
21 * fsl_mc_resource_pool_add_device - add allocatable device to a resource
22 * pool of a given MC bus
24 * @mc_bus: pointer to the MC bus
25 * @pool_type: MC bus pool type
26 * @mc_dev: Pointer to allocatable MC object device
28 * It adds an allocatable MC object device to a container's resource pool of
29 * the given resource type
31 static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
38 struct fsl_mc_resource_pool *res_pool;
39 struct fsl_mc_resource *resource;
40 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
43 if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
45 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
47 if (WARN_ON(mc_dev->resource))
50 res_pool = &mc_bus->resource_pools[pool_type];
51 if (WARN_ON(res_pool->type != pool_type))
53 if (WARN_ON(res_pool->mc_bus != mc_bus))
56 mutex_lock(&res_pool->mutex);
58 if (WARN_ON(res_pool->max_count < 0))
60 if (WARN_ON(res_pool->free_count < 0 ||
61 res_pool->free_count > res_pool->max_count))
64 resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource),
68 dev_err(&mc_bus_dev->dev,
69 "Failed to allocate memory for fsl_mc_resource\n");
73 resource->type = pool_type;
74 resource->id = mc_dev->obj_desc.id;
75 resource->data = mc_dev;
76 resource->parent_pool = res_pool;
77 INIT_LIST_HEAD(&resource->node);
78 list_add_tail(&resource->node, &res_pool->free_list);
79 mc_dev->resource = resource;
80 res_pool->free_count++;
81 res_pool->max_count++;
84 mutex_unlock(&res_pool->mutex);
90 * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
93 * @mc_dev: Pointer to allocatable MC object device
95 * It permanently removes an allocatable MC object device from the resource
96 * pool, the device is currently in, as long as it is in the pool's free list.
98 static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
101 struct fsl_mc_device *mc_bus_dev;
102 struct fsl_mc_bus *mc_bus;
103 struct fsl_mc_resource_pool *res_pool;
104 struct fsl_mc_resource *resource;
107 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
110 resource = mc_dev->resource;
111 if (WARN_ON(!resource || resource->data != mc_dev))
114 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
115 mc_bus = to_fsl_mc_bus(mc_bus_dev);
116 res_pool = resource->parent_pool;
117 if (WARN_ON(res_pool != &mc_bus->resource_pools[resource->type]))
120 mutex_lock(&res_pool->mutex);
122 if (WARN_ON(res_pool->max_count <= 0))
124 if (WARN_ON(res_pool->free_count <= 0 ||
125 res_pool->free_count > res_pool->max_count))
129 * If the device is currently allocated, its resource is not
130 * in the free list and thus, the device cannot be removed.
132 if (list_empty(&resource->node)) {
134 dev_err(&mc_bus_dev->dev,
135 "Device %s cannot be removed from resource pool\n",
136 dev_name(&mc_dev->dev));
140 list_del(&resource->node);
141 INIT_LIST_HEAD(&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(&resource->node);
219 INIT_LIST_HEAD(&resource->node);
221 res_pool->free_count--;
224 mutex_unlock(&res_pool->mutex);
225 *new_resource = resource;
229 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
231 void fsl_mc_resource_free(struct fsl_mc_resource *resource)
233 struct fsl_mc_resource_pool *res_pool;
235 res_pool = resource->parent_pool;
236 if (WARN_ON(resource->type != res_pool->type))
239 mutex_lock(&res_pool->mutex);
240 if (WARN_ON(res_pool->free_count < 0 ||
241 res_pool->free_count >= res_pool->max_count))
244 if (WARN_ON(!list_empty(&resource->node)))
247 list_add_tail(&resource->node, &res_pool->free_list);
248 res_pool->free_count++;
250 mutex_unlock(&res_pool->mutex);
252 EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
255 * fsl_mc_portal_allocate - Allocates an MC portal
257 * @mc_dev: MC device for which the MC portal is to be allocated
258 * @mc_io_flags: Flags for the fsl_mc_io object that wraps the allocated
260 * @new_mc_io: Pointer to area where the pointer to the fsl_mc_io object
261 * that wraps the allocated MC portal is to be returned
263 * This function allocates an MC portal from the device's parent DPRC,
264 * from the corresponding MC bus' pool of MC portals and wraps
265 * it in a new fsl_mc_io object. If 'mc_dev' is a DPRC itself, the
266 * portal is allocated from its own MC bus.
268 int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
270 struct fsl_mc_io **new_mc_io)
272 struct fsl_mc_device *mc_bus_dev;
273 struct fsl_mc_bus *mc_bus;
274 phys_addr_t mc_portal_phys_addr;
275 size_t mc_portal_size;
276 struct fsl_mc_device *dpmcp_dev;
278 struct fsl_mc_resource *resource = NULL;
279 struct fsl_mc_io *mc_io = NULL;
281 if (mc_dev->flags & FSL_MC_IS_DPRC) {
284 if (WARN_ON(mc_dev->dev.parent->bus != &fsl_mc_bus_type))
287 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
290 mc_bus = to_fsl_mc_bus(mc_bus_dev);
292 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_DPMCP, &resource);
297 dpmcp_dev = resource->data;
298 if (WARN_ON(!dpmcp_dev))
299 goto error_cleanup_resource;
301 if (dpmcp_dev->obj_desc.ver_major < DPMCP_MIN_VER_MAJOR ||
302 (dpmcp_dev->obj_desc.ver_major == DPMCP_MIN_VER_MAJOR &&
303 dpmcp_dev->obj_desc.ver_minor < DPMCP_MIN_VER_MINOR)) {
304 dev_err(&dpmcp_dev->dev,
305 "ERROR: Version %d.%d of DPMCP not supported.\n",
306 dpmcp_dev->obj_desc.ver_major,
307 dpmcp_dev->obj_desc.ver_minor);
309 goto error_cleanup_resource;
312 if (WARN_ON(dpmcp_dev->obj_desc.region_count == 0))
313 goto error_cleanup_resource;
315 mc_portal_phys_addr = dpmcp_dev->regions[0].start;
316 mc_portal_size = dpmcp_dev->regions[0].end -
317 dpmcp_dev->regions[0].start + 1;
319 if (WARN_ON(mc_portal_size != mc_bus_dev->mc_io->portal_size))
320 goto error_cleanup_resource;
322 error = fsl_create_mc_io(&mc_bus_dev->dev,
324 mc_portal_size, dpmcp_dev,
325 mc_io_flags, &mc_io);
327 goto error_cleanup_resource;
332 error_cleanup_resource:
333 fsl_mc_resource_free(resource);
336 EXPORT_SYMBOL_GPL(fsl_mc_portal_allocate);
339 * fsl_mc_portal_free - Returns an MC portal to the pool of free MC portals
342 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
344 void fsl_mc_portal_free(struct fsl_mc_io *mc_io)
346 struct fsl_mc_device *dpmcp_dev;
347 struct fsl_mc_resource *resource;
350 * Every mc_io obtained by calling fsl_mc_portal_allocate() is supposed
351 * to have a DPMCP object associated with.
353 dpmcp_dev = mc_io->dpmcp_dev;
354 if (WARN_ON(!dpmcp_dev))
357 resource = dpmcp_dev->resource;
358 if (WARN_ON(!resource || resource->type != FSL_MC_POOL_DPMCP))
361 if (WARN_ON(resource->data != dpmcp_dev))
364 fsl_destroy_mc_io(mc_io);
365 fsl_mc_resource_free(resource);
367 EXPORT_SYMBOL_GPL(fsl_mc_portal_free);
370 * fsl_mc_portal_reset - Resets the dpmcp object for a given fsl_mc_io object
372 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
374 int fsl_mc_portal_reset(struct fsl_mc_io *mc_io)
377 struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
379 if (WARN_ON(!dpmcp_dev))
382 error = dpmcp_reset(mc_io, 0, dpmcp_dev->mc_handle);
384 dev_err(&dpmcp_dev->dev, "dpmcp_reset() failed: %d\n", error);
390 EXPORT_SYMBOL_GPL(fsl_mc_portal_reset);
393 * fsl_mc_object_allocate - Allocates a MC object device of the given
394 * pool type from a given MC bus
396 * @mc_dev: MC device for which the MC object device is to be allocated
397 * @pool_type: MC bus resource pool type
398 * @new_mc_dev: Pointer to area where the pointer to the allocated
399 * MC object device is to be returned
401 * This function allocates a MC object device from the device's parent DPRC,
402 * from the corresponding MC bus' pool of allocatable MC object devices of
403 * the given resource type. mc_dev cannot be a DPRC itself.
405 * NOTE: pool_type must be different from FSL_MC_POOL_MCP, since MC
406 * portals are allocated using fsl_mc_portal_allocate(), instead of
409 int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
410 enum fsl_mc_pool_type pool_type,
411 struct fsl_mc_device **new_mc_adev)
413 struct fsl_mc_device *mc_bus_dev;
414 struct fsl_mc_bus *mc_bus;
415 struct fsl_mc_device *mc_adev;
417 struct fsl_mc_resource *resource = NULL;
420 if (WARN_ON(mc_dev->flags & FSL_MC_IS_DPRC))
423 if (WARN_ON(mc_dev->dev.parent->bus != &fsl_mc_bus_type))
426 if (WARN_ON(pool_type == FSL_MC_POOL_DPMCP))
429 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
430 mc_bus = to_fsl_mc_bus(mc_bus_dev);
431 error = fsl_mc_resource_allocate(mc_bus, pool_type, &resource);
435 mc_adev = resource->data;
436 if (WARN_ON(!mc_adev))
439 *new_mc_adev = mc_adev;
443 fsl_mc_resource_free(resource);
447 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
450 * fsl_mc_object_free - Returns an allocatable MC object device to the
451 * corresponding resource pool of a given MC bus.
453 * @mc_adev: Pointer to the MC object device
455 void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
457 struct fsl_mc_resource *resource;
459 resource = mc_adev->resource;
460 if (WARN_ON(resource->type == FSL_MC_POOL_DPMCP))
462 if (WARN_ON(resource->data != mc_adev))
465 fsl_mc_resource_free(resource);
467 EXPORT_SYMBOL_GPL(fsl_mc_object_free);
470 * Initialize the interrupt pool associated with a MC bus.
471 * It allocates a block of IRQs from the GIC-ITS
473 int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
474 unsigned int irq_count)
477 struct msi_desc *msi_desc;
478 struct fsl_mc_device_irq *irq_resources;
479 struct fsl_mc_device_irq *mc_dev_irq;
481 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
482 struct fsl_mc_resource_pool *res_pool =
483 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
485 if (WARN_ON(irq_count == 0 ||
486 irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS))
489 error = fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev->dev, irq_count);
493 irq_resources = devm_kzalloc(&mc_bus_dev->dev,
494 sizeof(*irq_resources) * irq_count,
496 if (!irq_resources) {
498 goto cleanup_msi_irqs;
501 for (i = 0; i < irq_count; i++) {
502 mc_dev_irq = &irq_resources[i];
505 * NOTE: This mc_dev_irq's MSI addr/value pair will be set
506 * by the fsl_mc_msi_write_msg() callback
508 mc_dev_irq->resource.type = res_pool->type;
509 mc_dev_irq->resource.data = mc_dev_irq;
510 mc_dev_irq->resource.parent_pool = res_pool;
511 INIT_LIST_HEAD(&mc_dev_irq->resource.node);
512 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list);
515 for_each_msi_entry(msi_desc, &mc_bus_dev->dev) {
516 mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index];
517 mc_dev_irq->msi_desc = msi_desc;
518 mc_dev_irq->resource.id = msi_desc->irq;
521 res_pool->max_count = irq_count;
522 res_pool->free_count = irq_count;
523 mc_bus->irq_resources = irq_resources;
527 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
530 EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool);
533 * Teardown the interrupt pool associated with an MC bus.
534 * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
536 void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus)
538 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
539 struct fsl_mc_resource_pool *res_pool =
540 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
542 if (WARN_ON(!mc_bus->irq_resources))
545 if (WARN_ON(res_pool->max_count == 0))
548 if (WARN_ON(res_pool->free_count != res_pool->max_count))
551 INIT_LIST_HEAD(&res_pool->free_list);
552 res_pool->max_count = 0;
553 res_pool->free_count = 0;
554 mc_bus->irq_resources = NULL;
555 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
557 EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool);
560 * It allocates the IRQs required by a given MC object device. The
561 * IRQs are allocated from the interrupt pool associated with the
562 * MC bus that contains the device, if the device is not a DPRC device.
563 * Otherwise, the IRQs are allocated from the interrupt pool associated
564 * with the MC bus that represents the DPRC device itself.
566 int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
570 int res_allocated_count = 0;
572 struct fsl_mc_device_irq **irqs = NULL;
573 struct fsl_mc_bus *mc_bus;
574 struct fsl_mc_resource_pool *res_pool;
576 if (WARN_ON(mc_dev->irqs))
579 irq_count = mc_dev->obj_desc.irq_count;
580 if (WARN_ON(irq_count == 0))
583 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
584 mc_bus = to_fsl_mc_bus(mc_dev);
586 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
588 if (WARN_ON(!mc_bus->irq_resources))
591 res_pool = &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
592 if (res_pool->free_count < irq_count) {
593 dev_err(&mc_dev->dev,
594 "Not able to allocate %u irqs for device\n", irq_count);
598 irqs = devm_kzalloc(&mc_dev->dev, irq_count * sizeof(irqs[0]),
603 for (i = 0; i < irq_count; i++) {
604 struct fsl_mc_resource *resource;
606 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_IRQ,
609 goto error_resource_alloc;
611 irqs[i] = to_fsl_mc_irq(resource);
612 res_allocated_count++;
614 WARN_ON(irqs[i]->mc_dev);
615 irqs[i]->mc_dev = mc_dev;
616 irqs[i]->dev_irq_index = i;
622 error_resource_alloc:
623 for (i = 0; i < res_allocated_count; i++) {
624 irqs[i]->mc_dev = NULL;
625 fsl_mc_resource_free(&irqs[i]->resource);
630 EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs);
633 * It frees the IRQs that were allocated for a MC object device, by
634 * returning them to the corresponding interrupt pool.
636 void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev)
640 struct fsl_mc_bus *mc_bus;
641 struct fsl_mc_device_irq **irqs = mc_dev->irqs;
646 irq_count = mc_dev->obj_desc.irq_count;
648 if (strcmp(mc_dev->obj_desc.type, "dprc") == 0)
649 mc_bus = to_fsl_mc_bus(mc_dev);
651 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
653 if (WARN_ON(!mc_bus->irq_resources))
656 for (i = 0; i < irq_count; i++) {
657 WARN_ON(!irqs[i]->mc_dev);
658 irqs[i]->mc_dev = NULL;
659 fsl_mc_resource_free(&irqs[i]->resource);
664 EXPORT_SYMBOL_GPL(fsl_mc_free_irqs);
667 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
668 * being added to the system
670 static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
672 enum fsl_mc_pool_type pool_type;
673 struct fsl_mc_device *mc_bus_dev;
674 struct fsl_mc_bus *mc_bus;
677 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
680 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
681 if (WARN_ON(mc_bus_dev->dev.bus != &fsl_mc_bus_type))
684 mc_bus = to_fsl_mc_bus(mc_bus_dev);
685 error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
689 error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
693 dev_dbg(&mc_dev->dev,
694 "Allocatable MC object device bound to fsl_mc_allocator driver");
699 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
700 * being removed from the system
702 static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
706 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
709 if (mc_dev->resource) {
710 error = fsl_mc_resource_pool_remove_device(mc_dev);
715 dev_dbg(&mc_dev->dev,
716 "Allocatable MC object device unbound from fsl_mc_allocator driver");
720 static const struct fsl_mc_device_id match_id_table[] = {
722 .vendor = FSL_MC_VENDOR_FREESCALE,
726 .vendor = FSL_MC_VENDOR_FREESCALE,
730 .vendor = FSL_MC_VENDOR_FREESCALE,
736 static struct fsl_mc_driver fsl_mc_allocator_driver = {
738 .name = "fsl_mc_allocator",
739 .owner = THIS_MODULE,
742 .match_id_table = match_id_table,
743 .probe = fsl_mc_allocator_probe,
744 .remove = fsl_mc_allocator_remove,
747 int __init fsl_mc_allocator_driver_init(void)
749 return fsl_mc_driver_register(&fsl_mc_allocator_driver);
752 void fsl_mc_allocator_driver_exit(void)
754 fsl_mc_driver_unregister(&fsl_mc_allocator_driver);