blob: 0d65220d5ec5b48fdc46186b2043c3797147cd9b [file] [log] [blame]
Alan Tull473f01f2018-05-16 18:49:58 -05001// SPDX-License-Identifier: GPL-2.0
Alan Tull0fa20cd2016-11-01 14:14:29 -05002/*
3 * FPGA Region - Device Tree support for FPGA programming under Linux
4 *
5 * Copyright (C) 2013-2016 Altera Corporation
Alan Tullef3acdd2017-11-15 14:20:25 -06006 * Copyright (C) 2017 Intel Corporation
Alan Tull0fa20cd2016-11-01 14:14:29 -05007 */
Alan Tull0fa20cd2016-11-01 14:14:29 -05008#include <linux/fpga/fpga-bridge.h>
9#include <linux/fpga/fpga-mgr.h>
Alan Tull59460a92017-11-15 14:20:21 -060010#include <linux/fpga/fpga-region.h>
Alan Tull0fa20cd2016-11-01 14:14:29 -050011#include <linux/idr.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
14#include <linux/module.h>
Alan Tull0fa20cd2016-11-01 14:14:29 -050015#include <linux/slab.h>
16#include <linux/spinlock.h>
17
Alan Tull0fa20cd2016-11-01 14:14:29 -050018static DEFINE_IDA(fpga_region_ida);
19static struct class *fpga_region_class;
20
Alan Tull503d4b72017-11-15 14:20:24 -060021struct fpga_region *fpga_region_class_find(
22 struct device *start, const void *data,
23 int (*match)(struct device *, const void *))
24{
25 struct device *dev;
26
27 dev = class_find_device(fpga_region_class, start, data, match);
28 if (!dev)
29 return NULL;
30
31 return to_fpga_region(dev);
32}
33EXPORT_SYMBOL_GPL(fpga_region_class_find);
34
Alan Tull0fa20cd2016-11-01 14:14:29 -050035/**
36 * fpga_region_get - get an exclusive reference to a fpga region
37 * @region: FPGA Region struct
38 *
39 * Caller should call fpga_region_put() when done with region.
40 *
41 * Return fpga_region struct if successful.
42 * Return -EBUSY if someone already has a reference to the region.
43 * Return -ENODEV if @np is not a FPGA Region.
44 */
45static struct fpga_region *fpga_region_get(struct fpga_region *region)
46{
47 struct device *dev = &region->dev;
48
49 if (!mutex_trylock(&region->mutex)) {
50 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
51 return ERR_PTR(-EBUSY);
52 }
53
54 get_device(dev);
Alan Tull0fa20cd2016-11-01 14:14:29 -050055 if (!try_module_get(dev->parent->driver->owner)) {
Alan Tull0fa20cd2016-11-01 14:14:29 -050056 put_device(dev);
57 mutex_unlock(&region->mutex);
58 return ERR_PTR(-ENODEV);
59 }
60
Alan Tullc3d971a2017-11-15 14:20:14 -060061 dev_dbg(dev, "get\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -050062
63 return region;
64}
65
66/**
67 * fpga_region_put - release a reference to a region
68 *
69 * @region: FPGA region
70 */
71static void fpga_region_put(struct fpga_region *region)
72{
73 struct device *dev = &region->dev;
74
Alan Tullc3d971a2017-11-15 14:20:14 -060075 dev_dbg(dev, "put\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -050076
77 module_put(dev->parent->driver->owner);
Alan Tull0fa20cd2016-11-01 14:14:29 -050078 put_device(dev);
79 mutex_unlock(&region->mutex);
80}
81
82/**
Alan Tull0fa20cd2016-11-01 14:14:29 -050083 * fpga_region_program_fpga - program FPGA
Alan Tull917a4302018-05-16 18:50:01 -050084 *
Alan Tull0fa20cd2016-11-01 14:14:29 -050085 * @region: FPGA region
Alan Tull917a4302018-05-16 18:50:01 -050086 *
Alan Tull61c32102017-11-15 14:20:19 -060087 * Program an FPGA using fpga image info (region->info).
Alan Tull093a89d2018-04-16 20:43:37 -070088 * If the region has a get_bridges function, the exclusive reference for the
89 * bridges will be held if programming succeeds. This is intended to prevent
90 * reprogramming the region until the caller considers it safe to do so.
91 * The caller will need to call fpga_bridges_put() before attempting to
92 * reprogram the region.
Alan Tull917a4302018-05-16 18:50:01 -050093 *
Alan Tull0fa20cd2016-11-01 14:14:29 -050094 * Return 0 for success or negative error code.
95 */
Alan Tull59460a92017-11-15 14:20:21 -060096int fpga_region_program_fpga(struct fpga_region *region)
Alan Tull0fa20cd2016-11-01 14:14:29 -050097{
Alan Tullebf877a52017-11-15 14:20:13 -060098 struct device *dev = &region->dev;
Alan Tull61c32102017-11-15 14:20:19 -060099 struct fpga_image_info *info = region->info;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500100 int ret;
101
102 region = fpga_region_get(region);
103 if (IS_ERR(region)) {
Alan Tullc3d971a2017-11-15 14:20:14 -0600104 dev_err(dev, "failed to get FPGA region\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -0500105 return PTR_ERR(region);
106 }
107
Alan Tull1df2dd72017-11-15 14:20:16 -0600108 ret = fpga_mgr_lock(region->mgr);
Alan Tullebf877a52017-11-15 14:20:13 -0600109 if (ret) {
110 dev_err(dev, "FPGA manager is busy\n");
Alan Tull1df2dd72017-11-15 14:20:16 -0600111 goto err_put_region;
Alan Tullebf877a52017-11-15 14:20:13 -0600112 }
113
Alan Tull52a3a7c2017-11-15 14:20:23 -0600114 /*
115 * In some cases, we already have a list of bridges in the
116 * fpga region struct. Or we don't have any bridges.
117 */
118 if (region->get_bridges) {
119 ret = region->get_bridges(region);
120 if (ret) {
121 dev_err(dev, "failed to get fpga region bridges\n");
122 goto err_unlock_mgr;
123 }
Alan Tull0fa20cd2016-11-01 14:14:29 -0500124 }
125
126 ret = fpga_bridges_disable(&region->bridge_list);
127 if (ret) {
Alan Tullc3d971a2017-11-15 14:20:14 -0600128 dev_err(dev, "failed to disable bridges\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -0500129 goto err_put_br;
130 }
131
Alan Tull61c32102017-11-15 14:20:19 -0600132 ret = fpga_mgr_load(region->mgr, info);
Alan Tull0fa20cd2016-11-01 14:14:29 -0500133 if (ret) {
Alan Tullc3d971a2017-11-15 14:20:14 -0600134 dev_err(dev, "failed to load FPGA image\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -0500135 goto err_put_br;
136 }
137
138 ret = fpga_bridges_enable(&region->bridge_list);
139 if (ret) {
Alan Tullc3d971a2017-11-15 14:20:14 -0600140 dev_err(dev, "failed to enable region bridges\n");
Alan Tull0fa20cd2016-11-01 14:14:29 -0500141 goto err_put_br;
142 }
143
Alan Tull1df2dd72017-11-15 14:20:16 -0600144 fpga_mgr_unlock(region->mgr);
Alan Tull0fa20cd2016-11-01 14:14:29 -0500145 fpga_region_put(region);
146
147 return 0;
148
149err_put_br:
Alan Tull52a3a7c2017-11-15 14:20:23 -0600150 if (region->get_bridges)
151 fpga_bridges_put(&region->bridge_list);
Alan Tullebf877a52017-11-15 14:20:13 -0600152err_unlock_mgr:
Alan Tull1df2dd72017-11-15 14:20:16 -0600153 fpga_mgr_unlock(region->mgr);
Tobias Klausere73bbf62017-04-24 16:34:21 -0500154err_put_region:
Alan Tull0fa20cd2016-11-01 14:14:29 -0500155 fpga_region_put(region);
156
157 return ret;
158}
Alan Tull59460a92017-11-15 14:20:21 -0600159EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
Alan Tull0fa20cd2016-11-01 14:14:29 -0500160
Wu Hao41a8b2c2018-06-30 08:53:12 +0800161static ssize_t compat_id_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
163{
164 struct fpga_region *region = to_fpga_region(dev);
165
166 if (!region->compat_id)
167 return -ENOENT;
168
169 return sprintf(buf, "%016llx%016llx\n",
170 (unsigned long long)region->compat_id->id_h,
171 (unsigned long long)region->compat_id->id_l);
172}
173
174static DEVICE_ATTR_RO(compat_id);
175
176static struct attribute *fpga_region_attrs[] = {
177 &dev_attr_compat_id.attr,
178 NULL,
179};
180ATTRIBUTE_GROUPS(fpga_region);
181
Alan Tull9f368972018-05-16 18:49:57 -0500182/**
183 * fpga_region_create - alloc and init a struct fpga_region
184 * @dev: device parent
185 * @mgr: manager that programs this region
186 * @get_bridges: optional function to get bridges to a list
187 *
188 * Return: struct fpga_region or NULL
189 */
190struct fpga_region
191*fpga_region_create(struct device *dev,
192 struct fpga_manager *mgr,
193 int (*get_bridges)(struct fpga_region *))
Alan Tull0fa20cd2016-11-01 14:14:29 -0500194{
Alan Tull9f368972018-05-16 18:49:57 -0500195 struct fpga_region *region;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500196 int id, ret = 0;
197
Alan Tull9f368972018-05-16 18:49:57 -0500198 region = kzalloc(sizeof(*region), GFP_KERNEL);
199 if (!region)
200 return NULL;
201
Alan Tull0fa20cd2016-11-01 14:14:29 -0500202 id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
Alan Tull52a3a7c2017-11-15 14:20:23 -0600203 if (id < 0)
Alan Tull9f368972018-05-16 18:49:57 -0500204 goto err_free;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500205
Alan Tull9f368972018-05-16 18:49:57 -0500206 region->mgr = mgr;
207 region->get_bridges = get_bridges;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500208 mutex_init(&region->mutex);
209 INIT_LIST_HEAD(&region->bridge_list);
Alan Tull9f368972018-05-16 18:49:57 -0500210
Alan Tull0fa20cd2016-11-01 14:14:29 -0500211 device_initialize(&region->dev);
212 region->dev.class = fpga_region_class;
213 region->dev.parent = dev;
Alan Tull52a3a7c2017-11-15 14:20:23 -0600214 region->dev.of_node = dev->of_node;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500215 region->dev.id = id;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500216
217 ret = dev_set_name(&region->dev, "region%d", id);
218 if (ret)
219 goto err_remove;
220
Alan Tull9f368972018-05-16 18:49:57 -0500221 return region;
Alan Tull52a3a7c2017-11-15 14:20:23 -0600222
223err_remove:
224 ida_simple_remove(&fpga_region_ida, id);
Alan Tull9f368972018-05-16 18:49:57 -0500225err_free:
226 kfree(region);
227
228 return NULL;
229}
230EXPORT_SYMBOL_GPL(fpga_region_create);
231
232/**
233 * fpga_region_free - free a struct fpga_region
234 * @region: FPGA region created by fpga_region_create
235 */
236void fpga_region_free(struct fpga_region *region)
237{
238 ida_simple_remove(&fpga_region_ida, region->dev.id);
239 kfree(region);
240}
241EXPORT_SYMBOL_GPL(fpga_region_free);
242
Alan Tull917a4302018-05-16 18:50:01 -0500243/**
Alan Tull9f368972018-05-16 18:49:57 -0500244 * fpga_region_register - register a FPGA region
245 * @region: FPGA region created by fpga_region_create
246 * Return: 0 or -errno
247 */
248int fpga_region_register(struct fpga_region *region)
249{
250 return device_add(&region->dev);
251
Alan Tull52a3a7c2017-11-15 14:20:23 -0600252}
253EXPORT_SYMBOL_GPL(fpga_region_register);
254
Alan Tull917a4302018-05-16 18:50:01 -0500255/**
Alan Tullfdff4052018-05-16 18:50:06 -0500256 * fpga_region_unregister - unregister and free a FPGA region
Alan Tull9f368972018-05-16 18:49:57 -0500257 * @region: FPGA region
258 */
259void fpga_region_unregister(struct fpga_region *region)
Alan Tull52a3a7c2017-11-15 14:20:23 -0600260{
261 device_unregister(&region->dev);
Alan Tull52a3a7c2017-11-15 14:20:23 -0600262}
263EXPORT_SYMBOL_GPL(fpga_region_unregister);
264
Alan Tull0fa20cd2016-11-01 14:14:29 -0500265static void fpga_region_dev_release(struct device *dev)
266{
267 struct fpga_region *region = to_fpga_region(dev);
268
Alan Tull9f368972018-05-16 18:49:57 -0500269 fpga_region_free(region);
Alan Tull0fa20cd2016-11-01 14:14:29 -0500270}
271
272/**
273 * fpga_region_init - init function for fpga_region class
274 * Creates the fpga_region class and registers a reconfig notifier.
275 */
276static int __init fpga_region_init(void)
277{
Alan Tull0fa20cd2016-11-01 14:14:29 -0500278 fpga_region_class = class_create(THIS_MODULE, "fpga_region");
279 if (IS_ERR(fpga_region_class))
280 return PTR_ERR(fpga_region_class);
281
Wu Hao41a8b2c2018-06-30 08:53:12 +0800282 fpga_region_class->dev_groups = fpga_region_groups;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500283 fpga_region_class->dev_release = fpga_region_dev_release;
284
Alan Tull0fa20cd2016-11-01 14:14:29 -0500285 return 0;
Alan Tull0fa20cd2016-11-01 14:14:29 -0500286}
287
288static void __exit fpga_region_exit(void)
289{
Alan Tull0fa20cd2016-11-01 14:14:29 -0500290 class_destroy(fpga_region_class);
291 ida_destroy(&fpga_region_ida);
292}
293
294subsys_initcall(fpga_region_init);
295module_exit(fpga_region_exit);
296
297MODULE_DESCRIPTION("FPGA Region");
Alan Tull59460a92017-11-15 14:20:21 -0600298MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
Alan Tull0fa20cd2016-11-01 14:14:29 -0500299MODULE_LICENSE("GPL v2");