1 // SPDX-License-Identifier: GPL-2.0
3 * FPGA Region - Support for FPGA programming under Linux
5 * Copyright (C) 2013-2016 Altera Corporation
6 * Copyright (C) 2017 Intel Corporation
8 #include <linux/fpga/fpga-bridge.h>
9 #include <linux/fpga/fpga-mgr.h>
10 #include <linux/fpga/fpga-region.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
18 static DEFINE_IDA(fpga_region_ida);
19 static struct class *fpga_region_class;
22 fpga_region_class_find(struct device *start, const void *data,
23 int (*match)(struct device *, const void *))
27 dev = class_find_device(fpga_region_class, start, data, match);
31 return to_fpga_region(dev);
33 EXPORT_SYMBOL_GPL(fpga_region_class_find);
36 * fpga_region_get - get an exclusive reference to an fpga region
37 * @region: FPGA Region struct
39 * Caller should call fpga_region_put() when done with region.
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 an FPGA Region.
45 static struct fpga_region *fpga_region_get(struct fpga_region *region)
47 struct device *dev = ®ion->dev;
49 if (!mutex_trylock(®ion->mutex)) {
50 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
51 return ERR_PTR(-EBUSY);
55 if (!try_module_get(dev->parent->driver->owner)) {
57 mutex_unlock(®ion->mutex);
58 return ERR_PTR(-ENODEV);
61 dev_dbg(dev, "get\n");
67 * fpga_region_put - release a reference to a region
69 * @region: FPGA region
71 static void fpga_region_put(struct fpga_region *region)
73 struct device *dev = ®ion->dev;
75 dev_dbg(dev, "put\n");
77 module_put(dev->parent->driver->owner);
79 mutex_unlock(®ion->mutex);
83 * fpga_region_program_fpga - program FPGA
85 * @region: FPGA region
87 * Program an FPGA using fpga image info (region->info).
88 * 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.
94 * Return 0 for success or negative error code.
96 int fpga_region_program_fpga(struct fpga_region *region)
98 struct device *dev = ®ion->dev;
99 struct fpga_image_info *info = region->info;
102 region = fpga_region_get(region);
103 if (IS_ERR(region)) {
104 dev_err(dev, "failed to get FPGA region\n");
105 return PTR_ERR(region);
108 ret = fpga_mgr_lock(region->mgr);
110 dev_err(dev, "FPGA manager is busy\n");
115 * In some cases, we already have a list of bridges in the
116 * fpga region struct. Or we don't have any bridges.
118 if (region->get_bridges) {
119 ret = region->get_bridges(region);
121 dev_err(dev, "failed to get fpga region bridges\n");
126 ret = fpga_bridges_disable(®ion->bridge_list);
128 dev_err(dev, "failed to disable bridges\n");
132 ret = fpga_mgr_load(region->mgr, info);
134 dev_err(dev, "failed to load FPGA image\n");
138 ret = fpga_bridges_enable(®ion->bridge_list);
140 dev_err(dev, "failed to enable region bridges\n");
144 fpga_mgr_unlock(region->mgr);
145 fpga_region_put(region);
150 if (region->get_bridges)
151 fpga_bridges_put(®ion->bridge_list);
153 fpga_mgr_unlock(region->mgr);
155 fpga_region_put(region);
159 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
161 static ssize_t compat_id_show(struct device *dev,
162 struct device_attribute *attr, char *buf)
164 struct fpga_region *region = to_fpga_region(dev);
166 if (!region->compat_id)
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);
174 static DEVICE_ATTR_RO(compat_id);
176 static struct attribute *fpga_region_attrs[] = {
177 &dev_attr_compat_id.attr,
180 ATTRIBUTE_GROUPS(fpga_region);
183 * fpga_region_register_full - create and register an FPGA Region device
184 * @parent: device parent
185 * @info: parameters for FPGA Region
187 * Return: struct fpga_region or ERR_PTR()
190 fpga_region_register_full(struct device *parent, const struct fpga_region_info *info)
192 struct fpga_region *region;
197 "Attempt to register without required info structure\n");
198 return ERR_PTR(-EINVAL);
201 region = kzalloc(sizeof(*region), GFP_KERNEL);
203 return ERR_PTR(-ENOMEM);
205 id = ida_alloc(&fpga_region_ida, GFP_KERNEL);
211 region->mgr = info->mgr;
212 region->compat_id = info->compat_id;
213 region->priv = info->priv;
214 region->get_bridges = info->get_bridges;
216 mutex_init(®ion->mutex);
217 INIT_LIST_HEAD(®ion->bridge_list);
219 region->dev.class = fpga_region_class;
220 region->dev.parent = parent;
221 region->dev.of_node = parent->of_node;
224 ret = dev_set_name(®ion->dev, "region%d", id);
228 ret = device_register(®ion->dev);
230 put_device(®ion->dev);
237 ida_free(&fpga_region_ida, id);
243 EXPORT_SYMBOL_GPL(fpga_region_register_full);
246 * fpga_region_register - create and register an FPGA Region device
247 * @parent: device parent
248 * @mgr: manager that programs this region
249 * @get_bridges: optional function to get bridges to a list
251 * This simple version of the register function should be sufficient for most users.
252 * The fpga_region_register_full() function is available for users that need to
253 * pass additional, optional parameters.
255 * Return: struct fpga_region or ERR_PTR()
258 fpga_region_register(struct device *parent, struct fpga_manager *mgr,
259 int (*get_bridges)(struct fpga_region *))
261 struct fpga_region_info info = { 0 };
264 info.get_bridges = get_bridges;
266 return fpga_region_register_full(parent, &info);
268 EXPORT_SYMBOL_GPL(fpga_region_register);
271 * fpga_region_unregister - unregister an FPGA region
272 * @region: FPGA region
274 * This function is intended for use in an FPGA region driver's remove function.
276 void fpga_region_unregister(struct fpga_region *region)
278 device_unregister(®ion->dev);
280 EXPORT_SYMBOL_GPL(fpga_region_unregister);
282 static void fpga_region_dev_release(struct device *dev)
284 struct fpga_region *region = to_fpga_region(dev);
286 ida_free(&fpga_region_ida, region->dev.id);
291 * fpga_region_init - init function for fpga_region class
292 * Creates the fpga_region class and registers a reconfig notifier.
294 static int __init fpga_region_init(void)
296 fpga_region_class = class_create(THIS_MODULE, "fpga_region");
297 if (IS_ERR(fpga_region_class))
298 return PTR_ERR(fpga_region_class);
300 fpga_region_class->dev_groups = fpga_region_groups;
301 fpga_region_class->dev_release = fpga_region_dev_release;
306 static void __exit fpga_region_exit(void)
308 class_destroy(fpga_region_class);
309 ida_destroy(&fpga_region_ida);
312 subsys_initcall(fpga_region_init);
313 module_exit(fpga_region_exit);
315 MODULE_DESCRIPTION("FPGA Region");
316 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
317 MODULE_LICENSE("GPL v2");