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