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"
20 * fsl_mc_resource_pool_add_device - add allocatable device to a resource
21 * pool of a given MC bus
23 * @mc_bus: pointer to the MC bus
24 * @pool_type: MC bus pool type
25 * @mc_dev: Pointer to allocatable MC object device
27 * It adds an allocatable MC object device to a container's resource pool of
28 * the given resource type
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;
41 bool mutex_locked = false;
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);
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++;
86 mutex_unlock(&res_pool->mutex);
92 * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
95 * @mc_dev: Pointer to allocatable MC object device
97 * It permanently removes an allocatable MC object device from the resource
98 * pool, the device is currently in, as long as it is in the pool's free list.
100 static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
103 struct fsl_mc_device *mc_bus_dev;
104 struct fsl_mc_bus *mc_bus;
105 struct fsl_mc_resource_pool *res_pool;
106 struct fsl_mc_resource *resource;
108 bool mutex_locked = false;
110 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
113 resource = mc_dev->resource;
114 if (WARN_ON(resource->data != mc_dev))
117 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
118 mc_bus = to_fsl_mc_bus(mc_bus_dev);
119 res_pool = resource->parent_pool;
120 if (WARN_ON(res_pool != &mc_bus->resource_pools[resource->type]))
123 mutex_lock(&res_pool->mutex);
126 if (WARN_ON(res_pool->max_count <= 0))
128 if (WARN_ON(res_pool->free_count <= 0 ||
129 res_pool->free_count > res_pool->max_count))
133 * If the device is currently allocated, its resource is not
134 * in the free list and thus, the device cannot be removed.
136 if (list_empty(&resource->node)) {
138 dev_err(&mc_bus_dev->dev,
139 "Device %s cannot be removed from resource pool\n",
140 dev_name(&mc_dev->dev));
144 list_del(&resource->node);
145 INIT_LIST_HEAD(&resource->node);
146 res_pool->free_count--;
147 res_pool->max_count--;
149 devm_kfree(&mc_bus_dev->dev, resource);
150 mc_dev->resource = NULL;
154 mutex_unlock(&res_pool->mutex);
159 static const char *const fsl_mc_pool_type_strings[] = {
160 [FSL_MC_POOL_DPMCP] = "dpmcp",
161 [FSL_MC_POOL_DPBP] = "dpbp",
162 [FSL_MC_POOL_DPCON] = "dpcon",
165 static int __must_check object_type_to_pool_type(const char *object_type,
166 enum fsl_mc_pool_type
171 for (i = 0; i < ARRAY_SIZE(fsl_mc_pool_type_strings); i++) {
172 if (strcmp(object_type, fsl_mc_pool_type_strings[i]) == 0) {
181 int __must_check fsl_mc_resource_allocate(struct fsl_mc_bus *mc_bus,
182 enum fsl_mc_pool_type pool_type,
183 struct fsl_mc_resource **new_resource)
185 struct fsl_mc_resource_pool *res_pool;
186 struct fsl_mc_resource *resource;
187 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
189 bool mutex_locked = false;
191 BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
192 FSL_MC_NUM_POOL_TYPES);
194 *new_resource = NULL;
195 if (WARN_ON(pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES))
198 res_pool = &mc_bus->resource_pools[pool_type];
199 if (WARN_ON(res_pool->mc_bus != mc_bus))
202 mutex_lock(&res_pool->mutex);
204 resource = list_first_entry_or_null(&res_pool->free_list,
205 struct fsl_mc_resource, node);
208 WARN_ON(res_pool->free_count != 0);
210 dev_err(&mc_bus_dev->dev,
211 "No more resources of type %s left\n",
212 fsl_mc_pool_type_strings[pool_type]);
216 if (WARN_ON(resource->type != pool_type))
218 if (WARN_ON(resource->parent_pool != res_pool))
220 if (WARN_ON(res_pool->free_count <= 0 ||
221 res_pool->free_count > res_pool->max_count))
224 list_del(&resource->node);
225 INIT_LIST_HEAD(&resource->node);
227 res_pool->free_count--;
228 mutex_unlock(&res_pool->mutex);
229 *new_resource = resource;
233 mutex_unlock(&res_pool->mutex);
237 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
239 void fsl_mc_resource_free(struct fsl_mc_resource *resource)
241 struct fsl_mc_resource_pool *res_pool;
242 bool mutex_locked = false;
244 res_pool = resource->parent_pool;
245 if (WARN_ON(resource->type != res_pool->type))
248 mutex_lock(&res_pool->mutex);
250 if (WARN_ON(res_pool->free_count < 0 ||
251 res_pool->free_count >= res_pool->max_count))
254 if (WARN_ON(!list_empty(&resource->node)))
257 list_add_tail(&resource->node, &res_pool->free_list);
258 res_pool->free_count++;
261 mutex_unlock(&res_pool->mutex);
263 EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
266 * fsl_mc_portal_allocate - Allocates an MC portal
268 * @mc_dev: MC device for which the MC portal is to be allocated
269 * @mc_io_flags: Flags for the fsl_mc_io object that wraps the allocated
271 * @new_mc_io: Pointer to area where the pointer to the fsl_mc_io object
272 * that wraps the allocated MC portal is to be returned
274 * This function allocates an MC portal from the device's parent DPRC,
275 * from the corresponding MC bus' pool of MC portals and wraps
276 * it in a new fsl_mc_io object. If 'mc_dev' is a DPRC itself, the
277 * portal is allocated from its own MC bus.
279 int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
280 uint16_t mc_io_flags,
281 struct fsl_mc_io **new_mc_io)
283 struct fsl_mc_device *mc_bus_dev;
284 struct fsl_mc_bus *mc_bus;
285 phys_addr_t mc_portal_phys_addr;
286 size_t mc_portal_size;
287 struct fsl_mc_device *mc_adev;
289 struct fsl_mc_resource *resource = NULL;
290 struct fsl_mc_io *mc_io = NULL;
292 if (mc_dev->flags & FSL_MC_IS_DPRC) {
295 if (WARN_ON(mc_dev->dev.parent->bus != &fsl_mc_bus_type))
298 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
301 mc_bus = to_fsl_mc_bus(mc_bus_dev);
303 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_DPMCP, &resource);
307 mc_adev = resource->data;
308 if (WARN_ON(!mc_adev))
309 goto error_cleanup_resource;
311 if (WARN_ON(mc_adev->obj_desc.region_count == 0))
312 goto error_cleanup_resource;
314 mc_portal_phys_addr = mc_adev->regions[0].start;
315 mc_portal_size = mc_adev->regions[0].end -
316 mc_adev->regions[0].start + 1;
318 if (WARN_ON(mc_portal_size != mc_bus_dev->mc_io->portal_size))
319 goto error_cleanup_resource;
321 error = fsl_create_mc_io(&mc_bus_dev->dev,
323 mc_portal_size, resource,
324 mc_io_flags, &mc_io);
326 goto error_cleanup_resource;
331 error_cleanup_resource:
332 fsl_mc_resource_free(resource);
335 EXPORT_SYMBOL_GPL(fsl_mc_portal_allocate);
338 * fsl_mc_portal_free - Returns an MC portal to the pool of free MC portals
341 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
343 void fsl_mc_portal_free(struct fsl_mc_io *mc_io)
345 struct fsl_mc_resource *resource;
347 resource = mc_io->resource;
348 if (WARN_ON(resource->type != FSL_MC_POOL_DPMCP))
350 if (WARN_ON(!resource->data))
353 fsl_destroy_mc_io(mc_io);
354 fsl_mc_resource_free(resource);
356 EXPORT_SYMBOL_GPL(fsl_mc_portal_free);
359 * fsl_mc_portal_reset - Resets the dpmcp object for a given fsl_mc_io object
361 * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
363 int fsl_mc_portal_reset(struct fsl_mc_io *mc_io)
367 struct fsl_mc_resource *resource = mc_io->resource;
368 struct fsl_mc_device *mc_dev = resource->data;
370 if (WARN_ON(resource->type != FSL_MC_POOL_DPMCP))
373 if (WARN_ON(!mc_dev))
376 error = dpmcp_open(mc_io, mc_dev->obj_desc.id, &token);
378 dev_err(&mc_dev->dev, "dpmcp_open() failed: %d\n", error);
382 error = dpmcp_reset(mc_io, token);
384 dev_err(&mc_dev->dev, "dpmcp_reset() failed: %d\n", error);
388 error = dpmcp_close(mc_io, token);
390 dev_err(&mc_dev->dev, "dpmcp_close() failed: %d\n", error);
396 EXPORT_SYMBOL_GPL(fsl_mc_portal_reset);
399 * fsl_mc_object_allocate - Allocates a MC object device of the given
400 * pool type from a given MC bus
402 * @mc_dev: MC device for which the MC object device is to be allocated
403 * @pool_type: MC bus resource pool type
404 * @new_mc_dev: Pointer to area where the pointer to the allocated
405 * MC object device is to be returned
407 * This function allocates a MC object device from the device's parent DPRC,
408 * from the corresponding MC bus' pool of allocatable MC object devices of
409 * the given resource type. mc_dev cannot be a DPRC itself.
411 * NOTE: pool_type must be different from FSL_MC_POOL_MCP, since MC
412 * portals are allocated using fsl_mc_portal_allocate(), instead of
415 int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
416 enum fsl_mc_pool_type pool_type,
417 struct fsl_mc_device **new_mc_adev)
419 struct fsl_mc_device *mc_bus_dev;
420 struct fsl_mc_bus *mc_bus;
421 struct fsl_mc_device *mc_adev;
423 struct fsl_mc_resource *resource = NULL;
426 if (WARN_ON(mc_dev->flags & FSL_MC_IS_DPRC))
429 if (WARN_ON(mc_dev->dev.parent->bus != &fsl_mc_bus_type))
432 if (WARN_ON(pool_type == FSL_MC_POOL_DPMCP))
435 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
436 mc_bus = to_fsl_mc_bus(mc_bus_dev);
437 error = fsl_mc_resource_allocate(mc_bus, pool_type, &resource);
441 mc_adev = resource->data;
442 if (WARN_ON(!mc_adev))
445 *new_mc_adev = mc_adev;
449 fsl_mc_resource_free(resource);
453 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
456 * fsl_mc_object_free - Returns an allocatable MC object device to the
457 * corresponding resource pool of a given MC bus.
459 * @mc_adev: Pointer to the MC object device
461 void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
463 struct fsl_mc_resource *resource;
465 resource = mc_adev->resource;
466 if (WARN_ON(resource->type == FSL_MC_POOL_DPMCP))
468 if (WARN_ON(resource->data != mc_adev))
471 fsl_mc_resource_free(resource);
473 EXPORT_SYMBOL_GPL(fsl_mc_object_free);
476 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
477 * being added to the system
479 static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
481 enum fsl_mc_pool_type pool_type;
482 struct fsl_mc_device *mc_bus_dev;
483 struct fsl_mc_bus *mc_bus;
486 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
489 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
490 if (WARN_ON(mc_bus_dev->dev.bus != &fsl_mc_bus_type))
493 mc_bus = to_fsl_mc_bus(mc_bus_dev);
494 error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
498 error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
502 dev_info(&mc_dev->dev,
503 "Allocatable MC object device bound to fsl_mc_allocator driver");
511 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
512 * being removed from the system
514 static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
518 if (WARN_ON(!FSL_MC_IS_ALLOCATABLE(mc_dev->obj_desc.type)))
521 error = fsl_mc_resource_pool_remove_device(mc_dev);
525 dev_info(&mc_dev->dev,
526 "Allocatable MC object device unbound from fsl_mc_allocator driver");
532 static const struct fsl_mc_device_match_id match_id_table[] = {
534 .vendor = FSL_MC_VENDOR_FREESCALE,
536 .ver_major = DPBP_VER_MAJOR,
537 .ver_minor = DPBP_VER_MINOR
540 .vendor = FSL_MC_VENDOR_FREESCALE,
542 .ver_major = DPMCP_VER_MAJOR,
543 .ver_minor = DPMCP_VER_MINOR
546 .vendor = FSL_MC_VENDOR_FREESCALE,
548 .ver_major = DPCON_VER_MAJOR,
549 .ver_minor = DPCON_VER_MINOR
554 static struct fsl_mc_driver fsl_mc_allocator_driver = {
556 .name = "fsl_mc_allocator",
557 .owner = THIS_MODULE,
560 .match_id_table = match_id_table,
561 .probe = fsl_mc_allocator_probe,
562 .remove = fsl_mc_allocator_remove,
565 int __init fsl_mc_allocator_driver_init(void)
567 return fsl_mc_driver_register(&fsl_mc_allocator_driver);
570 void __exit fsl_mc_allocator_driver_exit(void)
572 fsl_mc_driver_unregister(&fsl_mc_allocator_driver);