blob: e906ecfe23dd82025cdfc9b412f309a7f04fdfdf [file] [log] [blame]
Bogdan Purcareata203e2de2018-01-17 18:36:44 +02001// SPDX-License-Identifier: GPL-2.0
J. German Rivera197f4d62015-03-05 19:35:25 -06002/*
Stuart Yoder58caaac2016-10-26 11:20:32 -05003 * fsl-mc object allocator driver
J. German Rivera197f4d62015-03-05 19:35:25 -06004 *
Stuart Yoder6466dac2016-10-26 11:20:33 -05005 * Copyright (C) 2013-2016 Freescale Semiconductor, Inc.
J. German Rivera197f4d62015-03-05 19:35:25 -06006 *
J. German Rivera197f4d62015-03-05 19:35:25 -06007 */
8
Stuart Yoderd4e75132016-08-23 17:14:23 -05009#include <linux/module.h>
10#include <linux/msi.h>
Bogdan Purcareata6bd067c2018-02-05 08:07:42 -060011#include <linux/fsl/mc.h>
Stuart Yoderd4e75132016-08-23 17:14:23 -050012
Stuart Yoder243444f2016-08-23 17:13:30 -050013#include "fsl-mc-private.h"
J. German Rivera197f4d62015-03-05 19:35:25 -060014
Laurentiu Tudor19e2be72017-11-17 15:38:34 +020015static bool __must_check fsl_mc_is_allocatable(struct fsl_mc_device *mc_dev)
Laurentiu Tudor410ab9b2017-06-22 16:35:47 +030016{
Laurentiu Tudor19e2be72017-11-17 15:38:34 +020017 return is_fsl_mc_bus_dpbp(mc_dev) ||
18 is_fsl_mc_bus_dpmcp(mc_dev) ||
19 is_fsl_mc_bus_dpcon(mc_dev);
Laurentiu Tudor410ab9b2017-06-22 16:35:47 +030020}
Stuart Yodere267ddd2016-08-23 17:13:17 -050021
J. German Rivera197f4d62015-03-05 19:35:25 -060022/**
Stuart Yoder58caaac2016-10-26 11:20:32 -050023 * fsl_mc_resource_pool_add_device - add allocatable object to a resource
24 * pool of a given fsl-mc bus
J. German Rivera197f4d62015-03-05 19:35:25 -060025 *
Stuart Yoder58caaac2016-10-26 11:20:32 -050026 * @mc_bus: pointer to the fsl-mc bus
27 * @pool_type: pool type
28 * @mc_dev: pointer to allocatable fsl-mc device
J. German Rivera197f4d62015-03-05 19:35:25 -060029 */
30static 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;
J. German Rivera197f4d62015-03-05 19:35:25 -060041
Laurentiu Tudora385dd72017-11-17 15:38:32 +020042 if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
J. German Rivera197f4d62015-03-05 19:35:25 -060043 goto out;
Laurentiu Tudor19e2be72017-11-17 15:38:34 +020044 if (!fsl_mc_is_allocatable(mc_dev))
J. German Rivera197f4d62015-03-05 19:35:25 -060045 goto out;
Laurentiu Tudora385dd72017-11-17 15:38:32 +020046 if (mc_dev->resource)
J. German Rivera197f4d62015-03-05 19:35:25 -060047 goto out;
48
49 res_pool = &mc_bus->resource_pools[pool_type];
Laurentiu Tudora385dd72017-11-17 15:38:32 +020050 if (res_pool->type != pool_type)
J. German Rivera197f4d62015-03-05 19:35:25 -060051 goto out;
Laurentiu Tudora385dd72017-11-17 15:38:32 +020052 if (res_pool->mc_bus != mc_bus)
J. German Rivera197f4d62015-03-05 19:35:25 -060053 goto out;
54
55 mutex_lock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -060056
Laurentiu Tudora385dd72017-11-17 15:38:32 +020057 if (res_pool->max_count < 0)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +030058 goto out_unlock;
Laurentiu Tudora385dd72017-11-17 15:38:32 +020059 if (res_pool->free_count < 0 ||
60 res_pool->free_count > res_pool->max_count)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +030061 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -060062
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");
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +030069 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -060070 }
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;
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +030082out_unlock:
83 mutex_unlock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -060084out:
J. German Rivera197f4d62015-03-05 19:35:25 -060085 return error;
86}
87
88/**
89 * fsl_mc_resource_pool_remove_device - remove an allocatable device from a
90 * resource pool
91 *
Stuart Yoder58caaac2016-10-26 11:20:32 -050092 * @mc_dev: pointer to allocatable fsl-mc device
J. German Rivera197f4d62015-03-05 19:35:25 -060093 *
Stuart Yoder58caaac2016-10-26 11:20:32 -050094 * It permanently removes an allocatable fsl-mc device from the resource
95 * pool. It's an error if the device is in use.
J. German Rivera197f4d62015-03-05 19:35:25 -060096 */
97static 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;
J. German Rivera197f4d62015-03-05 19:35:25 -0600105
Laurentiu Tudor19e2be72017-11-17 15:38:34 +0200106 if (!fsl_mc_is_allocatable(mc_dev))
J. German Rivera197f4d62015-03-05 19:35:25 -0600107 goto out;
108
109 resource = mc_dev->resource;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200110 if (!resource || resource->data != mc_dev)
J. German Rivera197f4d62015-03-05 19:35:25 -0600111 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;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200116 if (res_pool != &mc_bus->resource_pools[resource->type])
J. German Rivera197f4d62015-03-05 19:35:25 -0600117 goto out;
118
119 mutex_lock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -0600120
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200121 if (res_pool->max_count <= 0)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300122 goto out_unlock;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200123 if (res_pool->free_count <= 0 ||
124 res_pool->free_count > res_pool->max_count)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300125 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600126
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));
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300136 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600137 }
138
Wei Yongjun9f4feef2016-09-15 02:27:04 +0000139 list_del_init(&resource->node);
J. German Rivera197f4d62015-03-05 19:35:25 -0600140 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;
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300146out_unlock:
147 mutex_unlock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -0600148out:
J. German Rivera197f4d62015-03-05 19:35:25 -0600149 return error;
150}
151
152static 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",
J. German Rivera89f067df2016-01-06 16:03:23 -0600156 [FSL_MC_POOL_IRQ] = "irq",
J. German Rivera197f4d62015-03-05 19:35:25 -0600157};
158
159static 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
175int __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;
J. German Rivera197f4d62015-03-05 19:35:25 -0600183
184 BUILD_BUG_ON(ARRAY_SIZE(fsl_mc_pool_type_strings) !=
185 FSL_MC_NUM_POOL_TYPES);
186
187 *new_resource = NULL;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200188 if (pool_type < 0 || pool_type >= FSL_MC_NUM_POOL_TYPES)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300189 goto out;
J. German Rivera197f4d62015-03-05 19:35:25 -0600190
191 res_pool = &mc_bus->resource_pools[pool_type];
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200192 if (res_pool->mc_bus != mc_bus)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300193 goto out;
J. German Rivera197f4d62015-03-05 19:35:25 -0600194
195 mutex_lock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -0600196 resource = list_first_entry_or_null(&res_pool->free_list,
197 struct fsl_mc_resource, node);
198
199 if (!resource) {
J. German Rivera197f4d62015-03-05 19:35:25 -0600200 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]);
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300204 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600205 }
206
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200207 if (resource->type != pool_type)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300208 goto out_unlock;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200209 if (resource->parent_pool != res_pool)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300210 goto out_unlock;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200211 if (res_pool->free_count <= 0 ||
212 res_pool->free_count > res_pool->max_count)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300213 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600214
Wei Yongjun9f4feef2016-09-15 02:27:04 +0000215 list_del_init(&resource->node);
J. German Rivera197f4d62015-03-05 19:35:25 -0600216
217 res_pool->free_count--;
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300218 error = 0;
219out_unlock:
J. German Rivera197f4d62015-03-05 19:35:25 -0600220 mutex_unlock(&res_pool->mutex);
221 *new_resource = resource;
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300222out:
J. German Rivera197f4d62015-03-05 19:35:25 -0600223 return error;
224}
225EXPORT_SYMBOL_GPL(fsl_mc_resource_allocate);
226
227void fsl_mc_resource_free(struct fsl_mc_resource *resource)
228{
229 struct fsl_mc_resource_pool *res_pool;
J. German Rivera197f4d62015-03-05 19:35:25 -0600230
231 res_pool = resource->parent_pool;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200232 if (resource->type != res_pool->type)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300233 return;
J. German Rivera197f4d62015-03-05 19:35:25 -0600234
235 mutex_lock(&res_pool->mutex);
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200236 if (res_pool->free_count < 0 ||
237 res_pool->free_count >= res_pool->max_count)
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300238 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600239
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200240 if (!list_empty(&resource->node))
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300241 goto out_unlock;
J. German Rivera197f4d62015-03-05 19:35:25 -0600242
243 list_add_tail(&resource->node, &res_pool->free_list);
244 res_pool->free_count++;
Cihangir Akturk6ffdc7b2016-04-09 21:45:18 +0300245out_unlock:
246 mutex_unlock(&res_pool->mutex);
J. German Rivera197f4d62015-03-05 19:35:25 -0600247}
248EXPORT_SYMBOL_GPL(fsl_mc_resource_free);
249
250/**
Stuart Yoder58caaac2016-10-26 11:20:32 -0500251 * fsl_mc_object_allocate - Allocates an fsl-mc object of the given
252 * pool type from a given fsl-mc bus instance
J. German Rivera197f4d62015-03-05 19:35:25 -0600253 *
Stuart Yoder58caaac2016-10-26 11:20:32 -0500254 * @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
J. German Rivera197f4d62015-03-05 19:35:25 -0600259 *
Stuart Yoder58caaac2016-10-26 11:20:32 -0500260 * 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.
J. German Rivera197f4d62015-03-05 19:35:25 -0600263 *
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 */
268int __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;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200279 if (mc_dev->flags & FSL_MC_IS_DPRC)
J. German Rivera197f4d62015-03-05 19:35:25 -0600280 goto error;
281
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200282 if (!dev_is_fsl_mc(mc_dev->dev.parent))
J. German Rivera197f4d62015-03-05 19:35:25 -0600283 goto error;
284
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200285 if (pool_type == FSL_MC_POOL_DPMCP)
J. German Rivera197f4d62015-03-05 19:35:25 -0600286 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;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200295 if (!mc_adev)
J. German Rivera197f4d62015-03-05 19:35:25 -0600296 goto error;
297
298 *new_mc_adev = mc_adev;
299 return 0;
300error:
301 if (resource)
302 fsl_mc_resource_free(resource);
303
304 return error;
305}
306EXPORT_SYMBOL_GPL(fsl_mc_object_allocate);
307
308/**
Stuart Yoder58caaac2016-10-26 11:20:32 -0500309 * fsl_mc_object_free - Returns an fsl-mc object to the resource
310 * pool where it came from.
311 * @mc_adev: Pointer to the fsl-mc device
J. German Rivera197f4d62015-03-05 19:35:25 -0600312 */
313void fsl_mc_object_free(struct fsl_mc_device *mc_adev)
314{
315 struct fsl_mc_resource *resource;
316
317 resource = mc_adev->resource;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200318 if (resource->type == FSL_MC_POOL_DPMCP)
J. German Rivera197f4d62015-03-05 19:35:25 -0600319 return;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200320 if (resource->data != mc_adev)
J. German Rivera197f4d62015-03-05 19:35:25 -0600321 return;
322
323 fsl_mc_resource_free(resource);
324}
325EXPORT_SYMBOL_GPL(fsl_mc_object_free);
326
J. German Rivera89f067df2016-01-06 16:03:23 -0600327/*
Stuart Yoder58caaac2016-10-26 11:20:32 -0500328 * A DPRC and the devices in the DPRC all share the same GIC-ITS device
329 * ID. A block of IRQs is pre-allocated and maintained in a pool
330 * from which devices can allocate them when needed.
331 */
332
333/*
334 * Initialize the interrupt pool associated with an fsl-mc bus.
335 * It allocates a block of IRQs from the GIC-ITS.
J. German Rivera89f067df2016-01-06 16:03:23 -0600336 */
337int fsl_mc_populate_irq_pool(struct fsl_mc_bus *mc_bus,
338 unsigned int irq_count)
339{
340 unsigned int i;
341 struct msi_desc *msi_desc;
342 struct fsl_mc_device_irq *irq_resources;
343 struct fsl_mc_device_irq *mc_dev_irq;
344 int error;
345 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
346 struct fsl_mc_resource_pool *res_pool =
347 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
348
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200349 if (irq_count == 0 ||
350 irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS)
J. German Rivera89f067df2016-01-06 16:03:23 -0600351 return -EINVAL;
352
353 error = fsl_mc_msi_domain_alloc_irqs(&mc_bus_dev->dev, irq_count);
354 if (error < 0)
355 return error;
356
Kees Cooka86854d2018-06-12 14:07:58 -0700357 irq_resources = devm_kcalloc(&mc_bus_dev->dev,
358 irq_count, sizeof(*irq_resources),
J. German Rivera89f067df2016-01-06 16:03:23 -0600359 GFP_KERNEL);
360 if (!irq_resources) {
361 error = -ENOMEM;
362 goto cleanup_msi_irqs;
363 }
364
365 for (i = 0; i < irq_count; i++) {
366 mc_dev_irq = &irq_resources[i];
367
368 /*
369 * NOTE: This mc_dev_irq's MSI addr/value pair will be set
370 * by the fsl_mc_msi_write_msg() callback
371 */
372 mc_dev_irq->resource.type = res_pool->type;
373 mc_dev_irq->resource.data = mc_dev_irq;
374 mc_dev_irq->resource.parent_pool = res_pool;
375 INIT_LIST_HEAD(&mc_dev_irq->resource.node);
376 list_add_tail(&mc_dev_irq->resource.node, &res_pool->free_list);
377 }
378
379 for_each_msi_entry(msi_desc, &mc_bus_dev->dev) {
380 mc_dev_irq = &irq_resources[msi_desc->fsl_mc.msi_index];
381 mc_dev_irq->msi_desc = msi_desc;
382 mc_dev_irq->resource.id = msi_desc->irq;
383 }
384
385 res_pool->max_count = irq_count;
386 res_pool->free_count = irq_count;
387 mc_bus->irq_resources = irq_resources;
388 return 0;
389
390cleanup_msi_irqs:
391 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
392 return error;
393}
394EXPORT_SYMBOL_GPL(fsl_mc_populate_irq_pool);
395
396/**
Stuart Yoder58caaac2016-10-26 11:20:32 -0500397 * Teardown the interrupt pool associated with an fsl-mc bus.
J. German Rivera89f067df2016-01-06 16:03:23 -0600398 * It frees the IRQs that were allocated to the pool, back to the GIC-ITS.
399 */
400void fsl_mc_cleanup_irq_pool(struct fsl_mc_bus *mc_bus)
401{
402 struct fsl_mc_device *mc_bus_dev = &mc_bus->mc_dev;
403 struct fsl_mc_resource_pool *res_pool =
404 &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
405
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200406 if (!mc_bus->irq_resources)
J. German Rivera89f067df2016-01-06 16:03:23 -0600407 return;
408
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200409 if (res_pool->max_count == 0)
J. German Rivera89f067df2016-01-06 16:03:23 -0600410 return;
411
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200412 if (res_pool->free_count != res_pool->max_count)
J. German Rivera89f067df2016-01-06 16:03:23 -0600413 return;
414
415 INIT_LIST_HEAD(&res_pool->free_list);
416 res_pool->max_count = 0;
417 res_pool->free_count = 0;
418 mc_bus->irq_resources = NULL;
419 fsl_mc_msi_domain_free_irqs(&mc_bus_dev->dev);
420}
421EXPORT_SYMBOL_GPL(fsl_mc_cleanup_irq_pool);
422
423/**
Stuart Yoder58caaac2016-10-26 11:20:32 -0500424 * Allocate the IRQs required by a given fsl-mc device.
J. German Rivera89f067df2016-01-06 16:03:23 -0600425 */
426int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev)
427{
428 int i;
429 int irq_count;
430 int res_allocated_count = 0;
431 int error = -EINVAL;
432 struct fsl_mc_device_irq **irqs = NULL;
433 struct fsl_mc_bus *mc_bus;
434 struct fsl_mc_resource_pool *res_pool;
435
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200436 if (mc_dev->irqs)
J. German Rivera89f067df2016-01-06 16:03:23 -0600437 return -EINVAL;
438
439 irq_count = mc_dev->obj_desc.irq_count;
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200440 if (irq_count == 0)
J. German Rivera89f067df2016-01-06 16:03:23 -0600441 return -EINVAL;
442
Laurentiu Tudor19e2be72017-11-17 15:38:34 +0200443 if (is_fsl_mc_bus_dprc(mc_dev))
J. German Rivera89f067df2016-01-06 16:03:23 -0600444 mc_bus = to_fsl_mc_bus(mc_dev);
445 else
446 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
447
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200448 if (!mc_bus->irq_resources)
J. German Rivera89f067df2016-01-06 16:03:23 -0600449 return -EINVAL;
450
451 res_pool = &mc_bus->resource_pools[FSL_MC_POOL_IRQ];
452 if (res_pool->free_count < irq_count) {
453 dev_err(&mc_dev->dev,
454 "Not able to allocate %u irqs for device\n", irq_count);
455 return -ENOSPC;
456 }
457
Kees Cooka86854d2018-06-12 14:07:58 -0700458 irqs = devm_kcalloc(&mc_dev->dev, irq_count, sizeof(irqs[0]),
J. German Rivera89f067df2016-01-06 16:03:23 -0600459 GFP_KERNEL);
460 if (!irqs)
461 return -ENOMEM;
462
463 for (i = 0; i < irq_count; i++) {
464 struct fsl_mc_resource *resource;
465
466 error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_IRQ,
467 &resource);
468 if (error < 0)
469 goto error_resource_alloc;
470
471 irqs[i] = to_fsl_mc_irq(resource);
472 res_allocated_count++;
473
J. German Rivera89f067df2016-01-06 16:03:23 -0600474 irqs[i]->mc_dev = mc_dev;
475 irqs[i]->dev_irq_index = i;
476 }
477
478 mc_dev->irqs = irqs;
479 return 0;
480
481error_resource_alloc:
482 for (i = 0; i < res_allocated_count; i++) {
483 irqs[i]->mc_dev = NULL;
484 fsl_mc_resource_free(&irqs[i]->resource);
485 }
486
487 return error;
488}
489EXPORT_SYMBOL_GPL(fsl_mc_allocate_irqs);
490
491/*
Stuart Yoder58caaac2016-10-26 11:20:32 -0500492 * Frees the IRQs that were allocated for an fsl-mc device.
J. German Rivera89f067df2016-01-06 16:03:23 -0600493 */
494void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev)
495{
496 int i;
497 int irq_count;
498 struct fsl_mc_bus *mc_bus;
499 struct fsl_mc_device_irq **irqs = mc_dev->irqs;
500
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200501 if (!irqs)
J. German Rivera89f067df2016-01-06 16:03:23 -0600502 return;
503
504 irq_count = mc_dev->obj_desc.irq_count;
505
Laurentiu Tudor19e2be72017-11-17 15:38:34 +0200506 if (is_fsl_mc_bus_dprc(mc_dev))
J. German Rivera89f067df2016-01-06 16:03:23 -0600507 mc_bus = to_fsl_mc_bus(mc_dev);
508 else
509 mc_bus = to_fsl_mc_bus(to_fsl_mc_device(mc_dev->dev.parent));
510
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200511 if (!mc_bus->irq_resources)
J. German Rivera89f067df2016-01-06 16:03:23 -0600512 return;
513
514 for (i = 0; i < irq_count; i++) {
J. German Rivera89f067df2016-01-06 16:03:23 -0600515 irqs[i]->mc_dev = NULL;
516 fsl_mc_resource_free(&irqs[i]->resource);
517 }
518
519 mc_dev->irqs = NULL;
520}
521EXPORT_SYMBOL_GPL(fsl_mc_free_irqs);
522
Stuart Yoder36406952016-08-23 17:13:24 -0500523void fsl_mc_init_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
524{
525 int pool_type;
526 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
527
528 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++) {
529 struct fsl_mc_resource_pool *res_pool =
530 &mc_bus->resource_pools[pool_type];
531
532 res_pool->type = pool_type;
533 res_pool->max_count = 0;
534 res_pool->free_count = 0;
535 res_pool->mc_bus = mc_bus;
536 INIT_LIST_HEAD(&res_pool->free_list);
537 mutex_init(&res_pool->mutex);
538 }
539}
540
541static void fsl_mc_cleanup_resource_pool(struct fsl_mc_device *mc_bus_dev,
542 enum fsl_mc_pool_type pool_type)
543{
544 struct fsl_mc_resource *resource;
545 struct fsl_mc_resource *next;
546 struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
547 struct fsl_mc_resource_pool *res_pool =
548 &mc_bus->resource_pools[pool_type];
549 int free_count = 0;
550
Stuart Yoder36406952016-08-23 17:13:24 -0500551 list_for_each_entry_safe(resource, next, &res_pool->free_list, node) {
552 free_count++;
Stuart Yoder36406952016-08-23 17:13:24 -0500553 devm_kfree(&mc_bus_dev->dev, resource);
554 }
Stuart Yoder36406952016-08-23 17:13:24 -0500555}
556
557void fsl_mc_cleanup_all_resource_pools(struct fsl_mc_device *mc_bus_dev)
558{
559 int pool_type;
560
561 for (pool_type = 0; pool_type < FSL_MC_NUM_POOL_TYPES; pool_type++)
562 fsl_mc_cleanup_resource_pool(mc_bus_dev, pool_type);
563}
564
J. German Rivera197f4d62015-03-05 19:35:25 -0600565/**
566 * fsl_mc_allocator_probe - callback invoked when an allocatable device is
567 * being added to the system
568 */
569static int fsl_mc_allocator_probe(struct fsl_mc_device *mc_dev)
570{
571 enum fsl_mc_pool_type pool_type;
572 struct fsl_mc_device *mc_bus_dev;
573 struct fsl_mc_bus *mc_bus;
J. German Rivera09a38ab2015-10-17 11:18:21 -0500574 int error;
J. German Rivera197f4d62015-03-05 19:35:25 -0600575
Laurentiu Tudor19e2be72017-11-17 15:38:34 +0200576 if (!fsl_mc_is_allocatable(mc_dev))
J. German Rivera09a38ab2015-10-17 11:18:21 -0500577 return -EINVAL;
J. German Rivera197f4d62015-03-05 19:35:25 -0600578
579 mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
Laurentiu Tudora385dd72017-11-17 15:38:32 +0200580 if (!dev_is_fsl_mc(&mc_bus_dev->dev))
J. German Rivera09a38ab2015-10-17 11:18:21 -0500581 return -EINVAL;
J. German Rivera197f4d62015-03-05 19:35:25 -0600582
583 mc_bus = to_fsl_mc_bus(mc_bus_dev);
584 error = object_type_to_pool_type(mc_dev->obj_desc.type, &pool_type);
585 if (error < 0)
J. German Rivera09a38ab2015-10-17 11:18:21 -0500586 return error;
J. German Rivera197f4d62015-03-05 19:35:25 -0600587
588 error = fsl_mc_resource_pool_add_device(mc_bus, pool_type, mc_dev);
589 if (error < 0)
J. German Rivera09a38ab2015-10-17 11:18:21 -0500590 return error;
J. German Rivera197f4d62015-03-05 19:35:25 -0600591
J. German Rivera6998d6b2015-10-17 11:18:16 -0500592 dev_dbg(&mc_dev->dev,
Stuart Yodere3494af2016-10-26 11:20:31 -0500593 "Allocatable fsl-mc device bound to fsl_mc_allocator driver");
J. German Rivera197f4d62015-03-05 19:35:25 -0600594 return 0;
J. German Rivera197f4d62015-03-05 19:35:25 -0600595}
596
597/**
598 * fsl_mc_allocator_remove - callback invoked when an allocatable device is
599 * being removed from the system
600 */
601static int fsl_mc_allocator_remove(struct fsl_mc_device *mc_dev)
602{
J. German Rivera09a38ab2015-10-17 11:18:21 -0500603 int error;
J. German Rivera197f4d62015-03-05 19:35:25 -0600604
Laurentiu Tudor19e2be72017-11-17 15:38:34 +0200605 if (!fsl_mc_is_allocatable(mc_dev))
J. German Rivera09a38ab2015-10-17 11:18:21 -0500606 return -EINVAL;
J. German Rivera197f4d62015-03-05 19:35:25 -0600607
J. German Rivera6958cd42015-10-17 11:18:20 -0500608 if (mc_dev->resource) {
609 error = fsl_mc_resource_pool_remove_device(mc_dev);
610 if (error < 0)
J. German Rivera09a38ab2015-10-17 11:18:21 -0500611 return error;
J. German Rivera6958cd42015-10-17 11:18:20 -0500612 }
J. German Rivera197f4d62015-03-05 19:35:25 -0600613
J. German Rivera6998d6b2015-10-17 11:18:16 -0500614 dev_dbg(&mc_dev->dev,
Stuart Yodere3494af2016-10-26 11:20:31 -0500615 "Allocatable fsl-mc device unbound from fsl_mc_allocator driver");
J. German Rivera09a38ab2015-10-17 11:18:21 -0500616 return 0;
J. German Rivera197f4d62015-03-05 19:35:25 -0600617}
618
Stuart Yoder57538af2016-06-22 16:40:44 -0500619static const struct fsl_mc_device_id match_id_table[] = {
J. German Rivera197f4d62015-03-05 19:35:25 -0600620 {
621 .vendor = FSL_MC_VENDOR_FREESCALE,
622 .obj_type = "dpbp",
J. German Rivera197f4d62015-03-05 19:35:25 -0600623 },
624 {
625 .vendor = FSL_MC_VENDOR_FREESCALE,
626 .obj_type = "dpmcp",
J. German Rivera197f4d62015-03-05 19:35:25 -0600627 },
628 {
629 .vendor = FSL_MC_VENDOR_FREESCALE,
630 .obj_type = "dpcon",
J. German Rivera197f4d62015-03-05 19:35:25 -0600631 },
632 {.vendor = 0x0},
633};
634
635static struct fsl_mc_driver fsl_mc_allocator_driver = {
636 .driver = {
637 .name = "fsl_mc_allocator",
J. German Rivera197f4d62015-03-05 19:35:25 -0600638 .pm = NULL,
639 },
640 .match_id_table = match_id_table,
641 .probe = fsl_mc_allocator_probe,
642 .remove = fsl_mc_allocator_remove,
643};
644
J. German Riverae91ffa92015-03-27 16:01:08 -0500645int __init fsl_mc_allocator_driver_init(void)
646{
647 return fsl_mc_driver_register(&fsl_mc_allocator_driver);
648}
Ioana Ciornei1e8ac832018-03-15 12:05:33 -0500649
650void fsl_mc_allocator_driver_exit(void)
651{
652 fsl_mc_driver_unregister(&fsl_mc_allocator_driver);
653}