fpga-region.c 8.46 KB
Newer Older
Alan Tull's avatar
Alan Tull committed
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5
/*
 * FPGA Region - Device Tree support for FPGA programming under Linux
 *
 *  Copyright (C) 2013-2016 Altera Corporation
6
 *  Copyright (C) 2017 Intel Corporation
7 8 9
 */
#include <linux/fpga/fpga-bridge.h>
#include <linux/fpga/fpga-mgr.h>
10
#include <linux/fpga/fpga-region.h>
11 12 13 14 15 16 17 18 19 20
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spinlock.h>

static DEFINE_IDA(fpga_region_ida);
static struct class *fpga_region_class;

21 22 23 24 25 26 27 28 29 30 31 32 33 34
struct fpga_region *fpga_region_class_find(
	struct device *start, const void *data,
	int (*match)(struct device *, const void *))
{
	struct device *dev;

	dev = class_find_device(fpga_region_class, start, data, match);
	if (!dev)
		return NULL;

	return to_fpga_region(dev);
}
EXPORT_SYMBOL_GPL(fpga_region_class_find);

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/**
 * fpga_region_get - get an exclusive reference to a fpga region
 * @region: FPGA Region struct
 *
 * Caller should call fpga_region_put() when done with region.
 *
 * Return fpga_region struct if successful.
 * Return -EBUSY if someone already has a reference to the region.
 * Return -ENODEV if @np is not a FPGA Region.
 */
static struct fpga_region *fpga_region_get(struct fpga_region *region)
{
	struct device *dev = &region->dev;

	if (!mutex_trylock(&region->mutex)) {
		dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
		return ERR_PTR(-EBUSY);
	}

	get_device(dev);
	if (!try_module_get(dev->parent->driver->owner)) {
		put_device(dev);
		mutex_unlock(&region->mutex);
		return ERR_PTR(-ENODEV);
	}

61
	dev_dbg(dev, "get\n");
62 63 64 65 66 67 68 69 70 71 72 73 74

	return region;
}

/**
 * fpga_region_put - release a reference to a region
 *
 * @region: FPGA region
 */
static void fpga_region_put(struct fpga_region *region)
{
	struct device *dev = &region->dev;

75
	dev_dbg(dev, "put\n");
76 77 78 79 80 81 82 83

	module_put(dev->parent->driver->owner);
	put_device(dev);
	mutex_unlock(&region->mutex);
}

/**
 * fpga_region_program_fpga - program FPGA
Alan Tull's avatar
Alan Tull committed
84
 *
85
 * @region: FPGA region
Alan Tull's avatar
Alan Tull committed
86
 *
87
 * Program an FPGA using fpga image info (region->info).
88 89 90 91 92
 * If the region has a get_bridges function, the exclusive reference for the
 * bridges will be held if programming succeeds.  This is intended to prevent
 * reprogramming the region until the caller considers it safe to do so.
 * The caller will need to call fpga_bridges_put() before attempting to
 * reprogram the region.
Alan Tull's avatar
Alan Tull committed
93
 *
94 95
 * Return 0 for success or negative error code.
 */
96
int fpga_region_program_fpga(struct fpga_region *region)
97
{
98
	struct device *dev = &region->dev;
99
	struct fpga_image_info *info = region->info;
100 101 102 103
	int ret;

	region = fpga_region_get(region);
	if (IS_ERR(region)) {
104
		dev_err(dev, "failed to get FPGA region\n");
105 106 107
		return PTR_ERR(region);
	}

Alan Tull's avatar
Alan Tull committed
108
	ret = fpga_mgr_lock(region->mgr);
109 110
	if (ret) {
		dev_err(dev, "FPGA manager is busy\n");
Alan Tull's avatar
Alan Tull committed
111
		goto err_put_region;
112 113
	}

114 115 116 117 118 119 120 121 122 123
	/*
	 * In some cases, we already have a list of bridges in the
	 * fpga region struct.  Or we don't have any bridges.
	 */
	if (region->get_bridges) {
		ret = region->get_bridges(region);
		if (ret) {
			dev_err(dev, "failed to get fpga region bridges\n");
			goto err_unlock_mgr;
		}
124 125 126 127
	}

	ret = fpga_bridges_disable(&region->bridge_list);
	if (ret) {
128
		dev_err(dev, "failed to disable bridges\n");
129 130 131
		goto err_put_br;
	}

132
	ret = fpga_mgr_load(region->mgr, info);
133
	if (ret) {
134
		dev_err(dev, "failed to load FPGA image\n");
135 136 137 138 139
		goto err_put_br;
	}

	ret = fpga_bridges_enable(&region->bridge_list);
	if (ret) {
140
		dev_err(dev, "failed to enable region bridges\n");
141 142 143
		goto err_put_br;
	}

Alan Tull's avatar
Alan Tull committed
144
	fpga_mgr_unlock(region->mgr);
145 146 147 148 149
	fpga_region_put(region);

	return 0;

err_put_br:
150 151
	if (region->get_bridges)
		fpga_bridges_put(&region->bridge_list);
152
err_unlock_mgr:
Alan Tull's avatar
Alan Tull committed
153
	fpga_mgr_unlock(region->mgr);
154
err_put_region:
155 156 157 158
	fpga_region_put(region);

	return ret;
}
159
EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
160

Wu Hao's avatar
Wu Hao committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static ssize_t compat_id_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct fpga_region *region = to_fpga_region(dev);

	if (!region->compat_id)
		return -ENOENT;

	return sprintf(buf, "%016llx%016llx\n",
		       (unsigned long long)region->compat_id->id_h,
		       (unsigned long long)region->compat_id->id_l);
}

static DEVICE_ATTR_RO(compat_id);

static struct attribute *fpga_region_attrs[] = {
	&dev_attr_compat_id.attr,
	NULL,
};
ATTRIBUTE_GROUPS(fpga_region);

182 183 184 185 186 187
/**
 * fpga_region_create - alloc and init a struct fpga_region
 * @dev: device parent
 * @mgr: manager that programs this region
 * @get_bridges: optional function to get bridges to a list
 *
188 189 190 191
 * The caller of this function is responsible for freeing the resulting region
 * struct with fpga_region_free().  Using devm_fpga_region_create() instead is
 * recommended.
 *
192 193 194 195 196 197
 * Return: struct fpga_region or NULL
 */
struct fpga_region
*fpga_region_create(struct device *dev,
		    struct fpga_manager *mgr,
		    int (*get_bridges)(struct fpga_region *))
198
{
199
	struct fpga_region *region;
200 201
	int id, ret = 0;

202 203 204 205
	region = kzalloc(sizeof(*region), GFP_KERNEL);
	if (!region)
		return NULL;

206
	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
207
	if (id < 0)
208
		goto err_free;
209

210 211
	region->mgr = mgr;
	region->get_bridges = get_bridges;
212 213
	mutex_init(&region->mutex);
	INIT_LIST_HEAD(&region->bridge_list);
214

215 216 217
	device_initialize(&region->dev);
	region->dev.class = fpga_region_class;
	region->dev.parent = dev;
218
	region->dev.of_node = dev->of_node;
219 220 221 222 223 224
	region->dev.id = id;

	ret = dev_set_name(&region->dev, "region%d", id);
	if (ret)
		goto err_remove;

225
	return region;
226 227 228

err_remove:
	ida_simple_remove(&fpga_region_ida, id);
229 230 231 232 233 234 235 236
err_free:
	kfree(region);

	return NULL;
}
EXPORT_SYMBOL_GPL(fpga_region_create);

/**
237 238
 * fpga_region_free - free a FPGA region created by fpga_region_create()
 * @region: FPGA region
239 240 241 242 243 244 245 246
 */
void fpga_region_free(struct fpga_region *region)
{
	ida_simple_remove(&fpga_region_ida, region->dev.id);
	kfree(region);
}
EXPORT_SYMBOL_GPL(fpga_region_free);

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static void devm_fpga_region_release(struct device *dev, void *res)
{
	struct fpga_region *region = *(struct fpga_region **)res;

	fpga_region_free(region);
}

/**
 * devm_fpga_region_create - create and initialize a managed FPGA region struct
 * @dev: device parent
 * @mgr: manager that programs this region
 * @get_bridges: optional function to get bridges to a list
 *
 * This function is intended for use in a FPGA region driver's probe function.
 * After the region driver creates the region struct with
 * devm_fpga_region_create(), it should register it with fpga_region_register().
 * The region driver's remove function should call fpga_region_unregister().
 * The region struct allocated with this function will be freed automatically on
 * driver detach.  This includes the case of a probe function returning error
 * before calling fpga_region_register(), the struct will still get cleaned up.
 *
 * Return: struct fpga_region or NULL
 */
struct fpga_region
*devm_fpga_region_create(struct device *dev,
			 struct fpga_manager *mgr,
			 int (*get_bridges)(struct fpga_region *))
{
	struct fpga_region **ptr, *region;

	ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return NULL;

	region = fpga_region_create(dev, mgr, get_bridges);
	if (!region) {
		devres_free(ptr);
	} else {
		*ptr = region;
		devres_add(dev, ptr);
	}

	return region;
}
EXPORT_SYMBOL_GPL(devm_fpga_region_create);

Alan Tull's avatar
Alan Tull committed
293
/**
294
 * fpga_region_register - register a FPGA region
295 296
 * @region: FPGA region
 *
297 298 299 300 301
 * Return: 0 or -errno
 */
int fpga_region_register(struct fpga_region *region)
{
	return device_add(&region->dev);
302 303 304
}
EXPORT_SYMBOL_GPL(fpga_region_register);

Alan Tull's avatar
Alan Tull committed
305
/**
306
 * fpga_region_unregister - unregister a FPGA region
307
 * @region: FPGA region
308 309
 *
 * This function is intended for use in a FPGA region driver's remove function.
310 311
 */
void fpga_region_unregister(struct fpga_region *region)
312 313 314 315 316
{
	device_unregister(&region->dev);
}
EXPORT_SYMBOL_GPL(fpga_region_unregister);

317 318 319 320 321 322 323 324 325 326 327 328 329 330
static void fpga_region_dev_release(struct device *dev)
{
}

/**
 * fpga_region_init - init function for fpga_region class
 * Creates the fpga_region class and registers a reconfig notifier.
 */
static int __init fpga_region_init(void)
{
	fpga_region_class = class_create(THIS_MODULE, "fpga_region");
	if (IS_ERR(fpga_region_class))
		return PTR_ERR(fpga_region_class);

Wu Hao's avatar
Wu Hao committed
331
	fpga_region_class->dev_groups = fpga_region_groups;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	fpga_region_class->dev_release = fpga_region_dev_release;

	return 0;
}

static void __exit fpga_region_exit(void)
{
	class_destroy(fpga_region_class);
	ida_destroy(&fpga_region_ida);
}

subsys_initcall(fpga_region_init);
module_exit(fpga_region_exit);

MODULE_DESCRIPTION("FPGA Region");
347
MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
348
MODULE_LICENSE("GPL v2");