GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / bus / fsl-mc / fsl-mc-allocator.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fsl-mc object allocator driver
4  *
5  * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
6  *
7  */
8
9 #include <linux/module.h>
10 #include <linux/msi.h>
11 #include <linux/fsl/mc.h>
12
13 #include "fsl-mc-private.h"
14
15 static bool __must_check fsl_mc_is_allocatable(struct fsl_mc_device *mc_dev)
16 {
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);
20 }
21
22 /**
23  * fsl_mc_resource_pool_add_device - add allocatable object to a resource
24  * pool of a given fsl-mc bus
25  *
26  * @mc_bus: pointer to the fsl-mc bus
27  * @pool_type: pool type
28  * @mc_dev: pointer to allocatable fsl-mc device
29  */
30 static int __must_check fsl_mc_resource_pool_add_device(struct fsl_mc_bus
31                                                                 *mc_bus,
32                                                         enum fsl_mc_pool_type
33                                                                 pool_type,
34                                                         struct fsl_mc_device
35                                                                 *mc_dev)
36 {
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;
40         int error = -EINVAL;
41
42         if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
43                 goto out;
44         if (!fsl_mc_is_allocatable(mc_dev))
45                 goto out;
46         if (mc_dev->resource)
47                 goto out;
48
49         res_pool = &mc_bus->resource_pools[pool_type];
50         if (res_pool->type != pool_type)
51                 goto out;
52         if (res_pool->mc_bus != mc_bus)
53                 goto out;
54
55         mutex_lock(&res_pool->mutex);
56
57         if (res_pool->max_count < 0)
58                 goto out_unlock;
59         if (res_pool->free_count < 0 ||
60             res_pool->free_count > res_pool->max_count)
61                 goto out_unlock;
62
63         resource = devm_kzalloc(&mc_bus_dev->dev, sizeof(*resource),
64                                 GFP_KERNEL);
65         if (!resource) {
66                 error = -ENOMEM;
67                 dev_err(&mc_bus_dev->dev,
68                         "Failed to allocate memory for fsl_mc_resource\n");
69                 goto out_unlock;
70         }
71
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++;
81         error = 0;
82 out_unlock:
83         mutex_unlock(&res_pool->mutex);
84 out:
85         return error;
86 }
87
88 /**
89  * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
90  * resource pool
91  *
92  * @mc_dev: pointer to allocatable fsl-mc device
93  *
94  * It permanently removes an allocatable fsl-mc device from the resource
95  * pool. It's an error if the device is in use.
96  */
97 static int __must_check fsl_mc_resource_pool_remove_device(struct fsl_mc_device
98                                                                    *mc_dev)
99 {
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;
104         int error = -EINVAL;
105
106         if (!fsl_mc_is_allocatable(mc_dev))
107                 goto out;
108
109         resource = mc_dev->resource;
110         if (!resource || resource->data != mc_dev)
111                 goto out;
112
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])
117                 goto out;
118
119         mutex_lock(&res_pool->mutex);
120
121         if (res_pool->max_count <= 0)
122                 goto out_unlock;
123         if (res_pool->free_count <= 0 ||
124             res_pool->free_count > res_pool->max_count)
125                 goto out_unlock;
126
127         /*
128          * If the device is currently allocated, its resource is not
129          * in the free list and thus, the device cannot be removed.
130          */
131         if (list_empty(&resource->node)) {
132                 error = -EBUSY;
133                 dev_err(&mc_bus_dev->dev,
134                         "Device %s cannot be removed from resource pool\n",
135                         dev_name(&mc_dev->dev));
136                 goto out_unlock;
137         }
138
139         list_del_init(&resource->node);
140         res_pool->free_count--;
141         res_pool->max_count--;
142
143         devm_kfree(&mc_bus_dev->dev, resource);
144         mc_dev->resource = NULL;
145         error = 0;
146 out_unlock:
147         mutex_unlock(&res_pool->mutex);
148 out:
149         return error;
150 }
151
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",
157 };
158
159 static int __must_check object_type_to_pool_type(const char *object_type,
160                                                  enum fsl_mc_pool_type
161                                                                 *pool_type)
162 {
163         unsigned int i;
164
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) {
167                         *pool_type = i;
168                         return 0;
169                 }
170         }
171
172         return -EINVAL;
173 }
174
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)
178 {
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;
182         int error = -EINVAL;
183
184         BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
185                      FSL_MC_NUM_POOL_TYPES);
186
187         *new_resource = NULL;
188         if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
189                 goto out;
190
191         res_pool = &mc_bus->resource_pools[pool_type];
192         if (res_pool->mc_bus != mc_bus)
193                 goto out;
194
195         mutex_lock(&res_pool->mutex);
196         resource = list_first_entry_or_null(&res_pool->free_list,
197                                             struct fsl_mc_resource, node);
198
199         if (!resource) {
200                 error = -ENXIO;
201                 dev_err(&mc_bus_dev->dev,
202                         "No more resources of type %s left\n",
203                         fsl_mc_pool_type_strings[pool_type]);
204                 goto out_unlock;
205         }
206
207         if (resource->type != pool_type)
208                 goto out_unlock;
209         if (resource->parent_pool != res_pool)
210                 goto out_unlock;
211         if (res_pool->free_count <= 0 ||
212             res_pool->free_count > res_pool->max_count)
213                 goto out_unlock;
214
215         list_del_init(&resource->node);
216
217         res_pool->free_count--;
218         error = 0;
219 out_unlock:
220         mutex_unlock(&res_pool->mutex);
221         *new_resource = resource;
222 out:
223         return error;
224 }
225 EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
226
227 void fsl_mc_resource_free(struct fsl_mc_resource *resource)
228 {
229         struct fsl_mc_resource_pool *res_pool;
230
231         res_pool = resource->parent_pool;
232         if (resource->type != res_pool->type)
233                 return;
234
235         mutex_lock(&res_pool->mutex);
236         if (res_pool->free_count < 0 ||
237             res_pool->free_count >= res_pool->max_count)
238                 goto out_unlock;
239
240         if (!list_empty(&resource->node))
241                 goto out_unlock;
242
243         list_add_tail(&resource->node, &res_pool->free_list);
244         res_pool->free_count++;
245 out_unlock:
246         mutex_unlock(&res_pool->mutex);
247 }
248 EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
249
250 /**
251  * fsl_mc_object_allocate - Allocates an fsl-mc object of the given
252  * pool type from a given fsl-mc bus instance
253  *
254  * @mc_dev: fsl-mc device which is used in conjunction with the
255  * allocated object
256  * @pool_type: pool type
257  * @new_mc_dev: pointer to area where the pointer to the allocated device
258  * is to be returned
259  *
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.
263  *
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
266  * this function.
267  */
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)
271 {
272         struct fsl_mc_device *mc_bus_dev;
273         struct fsl_mc_bus *mc_bus;
274         struct fsl_mc_device *mc_adev;
275         int error = -EINVAL;
276         struct fsl_mc_resource *resource = NULL;
277
278         *new_mc_adev = NULL;
279         if (mc_dev->flags & FSL_MC_IS_DPRC)
280                 goto error;
281
282         if (!dev_is_fsl_mc(mc_dev->dev.parent))
283                 goto error;
284
285         if (pool_type == FSL_MC_POOL_DPMCP)
286                 goto error;
287
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);
291         if (error < 0)
292                 goto error;
293
294         mc_adev = resource->data;
295         if (!mc_adev) {
296                 error = -EINVAL;
297                 goto error;
298         }
299
300         *new_mc_adev = mc_adev;
301         return 0;
302 error:
303         if (resource)
304                 fsl_mc_resource_free(resource);
305
306         return error;
307 }
308 EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
309
310 /**
311  * fsl_mc_object_free - Returns an fsl-mc object to the resource
312  * pool where it came from.
313  * @mc_adev: Pointer to the fsl-mc device
314  */
315 void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
316 {
317         struct fsl_mc_resource *resource;
318
319         resource = mc_adev->resource;
320         if (resource->type == FSL_MC_POOL_DPMCP)
321                 return;
322         if (resource->data != mc_adev)
323                 return;
324
325         fsl_mc_resource_free(resource);
326 }
327 EXPORT_SYMBOL_GPL(fsl_mc_object_free);
328
329 /*
330  * A DPRC and the devices in the DPRC all share the same GIC-ITS device
331  * ID.  A block of IRQs is pre-allocated and maintained in a pool
332  * from which devices can allocate them when needed.
333  */
334
335 /*
336  * Initialize the interrupt pool associated with an fsl-mc bus.
337  * It allocates a block of IRQs from the GIC-ITS.
338  */
339 int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
340                              unsigned int irq_count)
341 {
342         unsigned int i;
343         struct msi_desc *msi_desc;
344         struct fsl_mc_device_irq *irq_resources;
345         struct fsl_mc_device_irq *mc_dev_irq;
346         int error;
347         struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
348         struct fsl_mc_resource_pool *res_pool =
349                         &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
350
351         if (irq_count == 0 ||
352             irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS)
353                 return -EINVAL;
354
355         error = fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev->dev, irq_count);
356         if (error < 0)
357                 return error;
358
359         irq_resources = devm_kcalloc(&mc_bus_dev->dev,
360                                      irq_count, sizeof(*irq_resources),
361                                      GFP_KERNEL);
362         if (!irq_resources) {
363                 error = -ENOMEM;
364                 goto cleanup_msi_irqs;
365         }
366
367         for (i = 0; i < irq_count; i++) {
368                 mc_dev_irq = &irq_resources[i];
369
370                 /*
371                  * NOTE: This mc_dev_irq's MSI addr/value pair will be set
372                  * by the fsl_mc_msi_write_msg() callback
373                  */
374                 mc_dev_irq->resource.type = res_pool->type;
375                 mc_dev_irq->resource.data = mc_dev_irq;
376                 mc_dev_irq->resource.parent_pool = res_pool;
377                 INIT_LIST_HEAD(&mc_dev_irq->resource.node);
378                 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list);
379         }
380
381         for_each_msi_entry(msi_desc, &mc_bus_dev->dev) {
382                 mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index];
383                 mc_dev_irq->msi_desc = msi_desc;
384                 mc_dev_irq->resource.id = msi_desc->irq;
385         }
386
387         res_pool->max_count = irq_count;
388         res_pool->free_count = irq_count;
389         mc_bus->irq_resources = irq_resources;
390         return 0;
391
392 cleanup_msi_irqs:
393         fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
394         return error;
395 }
396 EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool);
397
398 /**
399  * Teardown the interrupt pool associated with an fsl-mc bus.
400  * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
401  */
402 void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus)
403 {
404         struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
405         struct fsl_mc_resource_pool *res_pool =
406                         &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
407
408         if (!mc_bus->irq_resources)
409                 return;
410
411         if (res_pool->max_count == 0)
412                 return;
413
414         if (res_pool->free_count != res_pool->max_count)
415                 return;
416
417         INIT_LIST_HEAD(&res_pool->free_list);
418         res_pool->max_count = 0;
419         res_pool->free_count = 0;
420         mc_bus->irq_resources = NULL;
421         fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
422 }
423 EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool);
424
425 /**
426  * Allocate the IRQs required by a given fsl-mc device.
427  */
428 int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
429 {
430         int i;
431         int irq_count;
432         int res_allocated_count = 0;
433         int error = -EINVAL;
434         struct fsl_mc_device_irq **irqs = NULL;
435         struct fsl_mc_bus *mc_bus;
436         struct fsl_mc_resource_pool *res_pool;
437
438         if (mc_dev->irqs)
439                 return -EINVAL;
440
441         irq_count = mc_dev->obj_desc.irq_count;
442         if (irq_count == 0)
443                 return -EINVAL;
444
445         if (is_fsl_mc_bus_dprc(mc_dev))
446                 mc_bus = to_fsl_mc_bus(mc_dev);
447         else
448                 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
449
450         if (!mc_bus->irq_resources)
451                 return -EINVAL;
452
453         res_pool = &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
454         if (res_pool->free_count < irq_count) {
455                 dev_err(&mc_dev->dev,
456                         "Not able to allocate %u irqs for device\n", irq_count);
457                 return -ENOSPC;
458         }
459
460         irqs = devm_kcalloc(&mc_dev->dev, irq_count, sizeof(irqs[0]),
461                             GFP_KERNEL);
462         if (!irqs)
463                 return -ENOMEM;
464
465         for (i = 0; i < irq_count; i++) {
466                 struct fsl_mc_resource *resource;
467
468                 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_IRQ,
469                                                  &resource);
470                 if (error < 0)
471                         goto error_resource_alloc;
472
473                 irqs[i] = to_fsl_mc_irq(resource);
474                 res_allocated_count++;
475
476                 irqs[i]->mc_dev = mc_dev;
477                 irqs[i]->dev_irq_index = i;
478         }
479
480         mc_dev->irqs = irqs;
481         return 0;
482
483 error_resource_alloc:
484         for (i = 0; i < res_allocated_count; i++) {
485                 irqs[i]->mc_dev = NULL;
486                 fsl_mc_resource_free(&irqs[i]->resource);
487         }
488
489         return error;
490 }
491 EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs);
492
493 /*
494  * Frees the IRQs that were allocated for an fsl-mc device.
495  */
496 void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev)
497 {
498         int i;
499         int irq_count;
500         struct fsl_mc_bus *mc_bus;
501         struct fsl_mc_device_irq **irqs = mc_dev->irqs;
502
503         if (!irqs)
504                 return;
505
506         irq_count = mc_dev->obj_desc.irq_count;
507
508         if (is_fsl_mc_bus_dprc(mc_dev))
509                 mc_bus = to_fsl_mc_bus(mc_dev);
510         else
511                 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
512
513         if (!mc_bus->irq_resources)
514                 return;
515
516         for (i = 0; i < irq_count; i++) {
517                 irqs[i]->mc_dev = NULL;
518                 fsl_mc_resource_free(&irqs[i]->resource);
519         }
520
521         mc_dev->irqs = NULL;
522 }
523 EXPORT_SYMBOL_GPL(fsl_mc_free_irqs);
524
525 void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
526 {
527         int pool_type;
528         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
529
530         for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
531                 struct fsl_mc_resource_pool *res_pool =
532                     &mc_bus->resource_pools[pool_type];
533
534                 res_pool->type = pool_type;
535                 res_pool->max_count = 0;
536                 res_pool->free_count = 0;
537                 res_pool->mc_bus = mc_bus;
538                 INIT_LIST_HEAD(&res_pool->free_list);
539                 mutex_init(&res_pool->mutex);
540         }
541 }
542
543 static void fsl_mc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
544                                          enum fsl_mc_pool_type pool_type)
545 {
546         struct fsl_mc_resource *resource;
547         struct fsl_mc_resource *next;
548         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
549         struct fsl_mc_resource_pool *res_pool =
550                                         &mc_bus->resource_pools[pool_type];
551         int free_count = 0;
552
553         list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
554                 free_count++;
555                 devm_kfree(&mc_bus_dev->dev, resource);
556         }
557 }
558
559 void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
560 {
561         int pool_type;
562
563         for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
564                 fsl_mc_cleanup_resource_pool(mc_bus_dev, pool_type);
565 }
566
567 /**
568  * fsl_mc_allocator_probe - callback invoked when an allocatable device is
569  * being added to the system
570  */
571 static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
572 {
573         enum fsl_mc_pool_type pool_type;
574         struct fsl_mc_device *mc_bus_dev;
575         struct fsl_mc_bus *mc_bus;
576         int error;
577
578         if (!fsl_mc_is_allocatable(mc_dev))
579                 return -EINVAL;
580
581         mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
582         if (!dev_is_fsl_mc(&mc_bus_dev->dev))
583                 return -EINVAL;
584
585         mc_bus = to_fsl_mc_bus(mc_bus_dev);
586         error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
587         if (error < 0)
588                 return error;
589
590         error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
591         if (error < 0)
592                 return error;
593
594         dev_dbg(&mc_dev->dev,
595                 "Allocatable fsl-mc device bound to fsl_mc_allocator driver");
596         return 0;
597 }
598
599 /**
600  * fsl_mc_allocator_remove - callback invoked when an allocatable device is
601  * being removed from the system
602  */
603 static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
604 {
605         int error;
606
607         if (!fsl_mc_is_allocatable(mc_dev))
608                 return -EINVAL;
609
610         if (mc_dev->resource) {
611                 error = fsl_mc_resource_pool_remove_device(mc_dev);
612                 if (error < 0)
613                         return error;
614         }
615
616         dev_dbg(&mc_dev->dev,
617                 "Allocatable fsl-mc device unbound from fsl_mc_allocator driver");
618         return 0;
619 }
620
621 static const struct fsl_mc_device_id match_id_table[] = {
622         {
623          .vendor = FSL_MC_VENDOR_FREESCALE,
624          .obj_type = "dpbp",
625         },
626         {
627          .vendor = FSL_MC_VENDOR_FREESCALE,
628          .obj_type = "dpmcp",
629         },
630         {
631          .vendor = FSL_MC_VENDOR_FREESCALE,
632          .obj_type = "dpcon",
633         },
634         {.vendor = 0x0},
635 };
636
637 static struct fsl_mc_driver fsl_mc_allocator_driver = {
638         .driver = {
639                    .name = "fsl_mc_allocator",
640                    .pm = NULL,
641                    },
642         .match_id_table = match_id_table,
643         .probe = fsl_mc_allocator_probe,
644         .remove = fsl_mc_allocator_remove,
645 };
646
647 int __init fsl_mc_allocator_driver_init(void)
648 {
649         return fsl_mc_driver_register(&fsl_mc_allocator_driver);
650 }
651
652 void fsl_mc_allocator_driver_exit(void)
653 {
654         fsl_mc_driver_unregister(&fsl_mc_allocator_driver);
655 }