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 mc_bus_dev
= to_fsl_mc_device(mc_dev
->dev
.parent
);
107 mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
109 resource
= mc_dev
->resource
;
110 if (!resource
|| resource
->data
!= mc_dev
) {
111 dev_err(&mc_bus_dev
->dev
, "resource mismatch\n");
115 res_pool
= resource
->parent_pool
;
116 if (res_pool
!= &mc_bus
->resource_pools
[resource
->type
]) {
117 dev_err(&mc_bus_dev
->dev
, "pool mismatch\n");
121 mutex_lock(&res_pool
->mutex
);
123 if (res_pool
->max_count
<= 0) {
124 dev_err(&mc_bus_dev
->dev
, "max_count underflow\n");
127 if (res_pool
->free_count
<= 0 ||
128 res_pool
->free_count
> res_pool
->max_count
) {
129 dev_err(&mc_bus_dev
->dev
, "free_count mismatch\n");
134 * If the device is currently allocated, its resource is not
135 * in the free list and thus, the device cannot be removed.
137 if (list_empty(&resource
->node
)) {
139 dev_err(&mc_bus_dev
->dev
,
140 "Device %s cannot be removed from resource pool\n",
141 dev_name(&mc_dev
->dev
));
145 list_del_init(&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
;
153 mutex_unlock(&res_pool
->mutex
);
158 static const char *const fsl_mc_pool_type_strings
[] = {
159 [FSL_MC_POOL_DPMCP
] = "dpmcp",
160 [FSL_MC_POOL_DPBP
] = "dpbp",
161 [FSL_MC_POOL_DPCON
] = "dpcon",
162 [FSL_MC_POOL_IRQ
] = "irq",
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
;
190 BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings
) !=
191 FSL_MC_NUM_POOL_TYPES
);
193 *new_resource
= NULL
;
194 if (pool_type
< 0 || pool_type
>= FSL_MC_NUM_POOL_TYPES
)
197 res_pool
= &mc_bus
->resource_pools
[pool_type
];
198 if (res_pool
->mc_bus
!= mc_bus
)
201 mutex_lock(&res_pool
->mutex
);
202 resource
= list_first_entry_or_null(&res_pool
->free_list
,
203 struct fsl_mc_resource
, node
);
207 dev_err(&mc_bus_dev
->dev
,
208 "No more resources of type %s left\n",
209 fsl_mc_pool_type_strings
[pool_type
]);
213 if (resource
->type
!= pool_type
)
215 if (resource
->parent_pool
!= res_pool
)
217 if (res_pool
->free_count
<= 0 ||
218 res_pool
->free_count
> res_pool
->max_count
)
221 list_del_init(&resource
->node
);
223 res_pool
->free_count
--;
226 mutex_unlock(&res_pool
->mutex
);
227 *new_resource
= resource
;
231 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate
);
233 void fsl_mc_resource_free(struct fsl_mc_resource
*resource
)
235 struct fsl_mc_resource_pool
*res_pool
;
237 res_pool
= resource
->parent_pool
;
238 if (resource
->type
!= res_pool
->type
)
241 mutex_lock(&res_pool
->mutex
);
242 if (res_pool
->free_count
< 0 ||
243 res_pool
->free_count
>= res_pool
->max_count
)
246 if (!list_empty(&resource
->node
))
249 list_add_tail(&resource
->node
, &res_pool
->free_list
);
250 res_pool
->free_count
++;
252 mutex_unlock(&res_pool
->mutex
);
254 EXPORT_SYMBOL_GPL(fsl_mc_resource_free
);
257 * fsl_mc_object_allocate - Allocates an fsl-mc object of the given
258 * pool type from a given fsl-mc bus instance
260 * @mc_dev: fsl-mc device which is used in conjunction with the
262 * @pool_type: pool type
263 * @new_mc_adev: pointer to area where the pointer to the allocated device
266 * Allocatable objects are always used in conjunction with some functional
267 * device. This function allocates an object of the specified type from
268 * the DPRC containing the functional device.
270 * NOTE: pool_type must be different from FSL_MC_POOL_MCP, since MC
271 * portals are allocated using fsl_mc_portal_allocate(), instead of
274 int __must_check
fsl_mc_object_allocate(struct fsl_mc_device
*mc_dev
,
275 enum fsl_mc_pool_type pool_type
,
276 struct fsl_mc_device
**new_mc_adev
)
278 struct fsl_mc_device
*mc_bus_dev
;
279 struct fsl_mc_bus
*mc_bus
;
280 struct fsl_mc_device
*mc_adev
;
282 struct fsl_mc_resource
*resource
= NULL
;
285 if (mc_dev
->flags
& FSL_MC_IS_DPRC
)
288 if (!dev_is_fsl_mc(mc_dev
->dev
.parent
))
291 if (pool_type
== FSL_MC_POOL_DPMCP
)
294 mc_bus_dev
= to_fsl_mc_device(mc_dev
->dev
.parent
);
295 mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
296 error
= fsl_mc_resource_allocate(mc_bus
, pool_type
, &resource
);
300 mc_adev
= resource
->data
;
306 mc_adev
->consumer_link
= device_link_add(&mc_dev
->dev
,
308 DL_FLAG_AUTOREMOVE_CONSUMER
);
309 if (!mc_adev
->consumer_link
) {
314 *new_mc_adev
= mc_adev
;
318 fsl_mc_resource_free(resource
);
322 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate
);
325 * fsl_mc_object_free - Returns an fsl-mc object to the resource
326 * pool where it came from.
327 * @mc_adev: Pointer to the fsl-mc device
329 void fsl_mc_object_free(struct fsl_mc_device
*mc_adev
)
331 struct fsl_mc_resource
*resource
;
333 resource
= mc_adev
->resource
;
334 if (resource
->type
== FSL_MC_POOL_DPMCP
)
336 if (resource
->data
!= mc_adev
)
339 fsl_mc_resource_free(resource
);
341 mc_adev
->consumer_link
= NULL
;
343 EXPORT_SYMBOL_GPL(fsl_mc_object_free
);
346 * A DPRC and the devices in the DPRC all share the same GIC-ITS device
347 * ID. A block of IRQs is pre-allocated and maintained in a pool
348 * from which devices can allocate them when needed.
352 * Initialize the interrupt pool associated with an fsl-mc bus.
353 * It allocates a block of IRQs from the GIC-ITS.
355 int fsl_mc_populate_irq_pool(struct fsl_mc_device
*mc_bus_dev
,
356 unsigned int irq_count
)
359 struct fsl_mc_device_irq
*irq_resources
;
360 struct fsl_mc_device_irq
*mc_dev_irq
;
362 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
363 struct fsl_mc_resource_pool
*res_pool
=
364 &mc_bus
->resource_pools
[FSL_MC_POOL_IRQ
];
366 /* do nothing if the IRQ pool is already populated */
367 if (mc_bus
->irq_resources
)
370 if (irq_count
== 0 ||
371 irq_count
> FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS
)
374 error
= fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev
->dev
, irq_count
);
378 irq_resources
= devm_kcalloc(&mc_bus_dev
->dev
,
379 irq_count
, sizeof(*irq_resources
),
381 if (!irq_resources
) {
383 goto cleanup_msi_irqs
;
386 for (i
= 0; i
< irq_count
; i
++) {
387 mc_dev_irq
= &irq_resources
[i
];
390 * NOTE: This mc_dev_irq's MSI addr/value pair will be set
391 * by the fsl_mc_msi_write_msg() callback
393 mc_dev_irq
->resource
.type
= res_pool
->type
;
394 mc_dev_irq
->resource
.data
= mc_dev_irq
;
395 mc_dev_irq
->resource
.parent_pool
= res_pool
;
396 mc_dev_irq
->virq
= msi_get_virq(&mc_bus_dev
->dev
, i
);
397 mc_dev_irq
->resource
.id
= mc_dev_irq
->virq
;
398 INIT_LIST_HEAD(&mc_dev_irq
->resource
.node
);
399 list_add_tail(&mc_dev_irq
->resource
.node
, &res_pool
->free_list
);
402 res_pool
->max_count
= irq_count
;
403 res_pool
->free_count
= irq_count
;
404 mc_bus
->irq_resources
= irq_resources
;
408 fsl_mc_msi_domain_free_irqs(&mc_bus_dev
->dev
);
411 EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool
);
414 * Teardown the interrupt pool associated with an fsl-mc bus.
415 * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
417 void fsl_mc_cleanup_irq_pool(struct fsl_mc_device
*mc_bus_dev
)
419 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
420 struct fsl_mc_resource_pool
*res_pool
=
421 &mc_bus
->resource_pools
[FSL_MC_POOL_IRQ
];
423 if (!mc_bus
->irq_resources
)
426 if (res_pool
->max_count
== 0)
429 if (res_pool
->free_count
!= res_pool
->max_count
)
432 INIT_LIST_HEAD(&res_pool
->free_list
);
433 res_pool
->max_count
= 0;
434 res_pool
->free_count
= 0;
435 mc_bus
->irq_resources
= NULL
;
436 fsl_mc_msi_domain_free_irqs(&mc_bus_dev
->dev
);
438 EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool
);
441 * Allocate the IRQs required by a given fsl-mc device.
443 int __must_check
fsl_mc_allocate_irqs(struct fsl_mc_device
*mc_dev
)
447 int res_allocated_count
= 0;
449 struct fsl_mc_device_irq
**irqs
= NULL
;
450 struct fsl_mc_bus
*mc_bus
;
451 struct fsl_mc_resource_pool
*res_pool
;
456 irq_count
= mc_dev
->obj_desc
.irq_count
;
460 if (is_fsl_mc_bus_dprc(mc_dev
))
461 mc_bus
= to_fsl_mc_bus(mc_dev
);
463 mc_bus
= to_fsl_mc_bus(to_fsl_mc_device(mc_dev
->dev
.parent
));
465 if (!mc_bus
->irq_resources
)
468 res_pool
= &mc_bus
->resource_pools
[FSL_MC_POOL_IRQ
];
469 if (res_pool
->free_count
< irq_count
) {
470 dev_err(&mc_dev
->dev
,
471 "Not able to allocate %u irqs for device\n", irq_count
);
475 irqs
= devm_kcalloc(&mc_dev
->dev
, irq_count
, sizeof(irqs
[0]),
480 for (i
= 0; i
< irq_count
; i
++) {
481 struct fsl_mc_resource
*resource
;
483 error
= fsl_mc_resource_allocate(mc_bus
, FSL_MC_POOL_IRQ
,
486 goto error_resource_alloc
;
488 irqs
[i
] = to_fsl_mc_irq(resource
);
489 res_allocated_count
++;
491 irqs
[i
]->mc_dev
= mc_dev
;
492 irqs
[i
]->dev_irq_index
= i
;
498 error_resource_alloc
:
499 for (i
= 0; i
< res_allocated_count
; i
++) {
500 irqs
[i
]->mc_dev
= NULL
;
501 fsl_mc_resource_free(&irqs
[i
]->resource
);
506 EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs
);
509 * Frees the IRQs that were allocated for an fsl-mc device.
511 void fsl_mc_free_irqs(struct fsl_mc_device
*mc_dev
)
515 struct fsl_mc_bus
*mc_bus
;
516 struct fsl_mc_device_irq
**irqs
= mc_dev
->irqs
;
521 irq_count
= mc_dev
->obj_desc
.irq_count
;
523 if (is_fsl_mc_bus_dprc(mc_dev
))
524 mc_bus
= to_fsl_mc_bus(mc_dev
);
526 mc_bus
= to_fsl_mc_bus(to_fsl_mc_device(mc_dev
->dev
.parent
));
528 if (!mc_bus
->irq_resources
)
531 for (i
= 0; i
< irq_count
; i
++) {
532 irqs
[i
]->mc_dev
= NULL
;
533 fsl_mc_resource_free(&irqs
[i
]->resource
);
538 EXPORT_SYMBOL_GPL(fsl_mc_free_irqs
);
540 void fsl_mc_init_all_resource_pools(struct fsl_mc_device
*mc_bus_dev
)
543 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
545 for (pool_type
= 0; pool_type
< FSL_MC_NUM_POOL_TYPES
; pool_type
++) {
546 struct fsl_mc_resource_pool
*res_pool
=
547 &mc_bus
->resource_pools
[pool_type
];
549 res_pool
->type
= pool_type
;
550 res_pool
->max_count
= 0;
551 res_pool
->free_count
= 0;
552 res_pool
->mc_bus
= mc_bus
;
553 INIT_LIST_HEAD(&res_pool
->free_list
);
554 mutex_init(&res_pool
->mutex
);
558 static void fsl_mc_cleanup_resource_pool(struct fsl_mc_device
*mc_bus_dev
,
559 enum fsl_mc_pool_type pool_type
)
561 struct fsl_mc_resource
*resource
;
562 struct fsl_mc_resource
*next
;
563 struct fsl_mc_bus
*mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
564 struct fsl_mc_resource_pool
*res_pool
=
565 &mc_bus
->resource_pools
[pool_type
];
567 list_for_each_entry_safe(resource
, next
, &res_pool
->free_list
, node
)
568 devm_kfree(&mc_bus_dev
->dev
, resource
);
571 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device
*mc_bus_dev
)
575 for (pool_type
= 0; pool_type
< FSL_MC_NUM_POOL_TYPES
; pool_type
++)
576 fsl_mc_cleanup_resource_pool(mc_bus_dev
, pool_type
);
580 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
581 * being added to the system
583 static int fsl_mc_allocator_probe(struct fsl_mc_device
*mc_dev
)
585 enum fsl_mc_pool_type pool_type
;
586 struct fsl_mc_device
*mc_bus_dev
;
587 struct fsl_mc_bus
*mc_bus
;
590 if (!fsl_mc_is_allocatable(mc_dev
))
593 mc_bus_dev
= to_fsl_mc_device(mc_dev
->dev
.parent
);
594 if (!dev_is_fsl_mc(&mc_bus_dev
->dev
))
597 mc_bus
= to_fsl_mc_bus(mc_bus_dev
);
598 error
= object_type_to_pool_type(mc_dev
->obj_desc
.type
, &pool_type
);
602 error
= fsl_mc_resource_pool_add_device(mc_bus
, pool_type
, mc_dev
);
606 dev_dbg(&mc_dev
->dev
,
607 "Allocatable fsl-mc device bound to fsl_mc_allocator driver");
612 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
613 * being removed from the system
615 static void fsl_mc_allocator_remove(struct fsl_mc_device
*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");
629 static const struct fsl_mc_device_id match_id_table
[] = {
631 .vendor
= FSL_MC_VENDOR_FREESCALE
,
635 .vendor
= FSL_MC_VENDOR_FREESCALE
,
639 .vendor
= FSL_MC_VENDOR_FREESCALE
,
645 static struct fsl_mc_driver fsl_mc_allocator_driver
= {
647 .name
= "fsl_mc_allocator",
650 .match_id_table
= match_id_table
,
651 .probe
= fsl_mc_allocator_probe
,
652 .remove
= fsl_mc_allocator_remove
,
655 int __init
fsl_mc_allocator_driver_init(void)
657 return fsl_mc_driver_register(&fsl_mc_allocator_driver
);
660 void fsl_mc_allocator_driver_exit(void)
662 fsl_mc_driver_unregister(&fsl_mc_allocator_driver
);