2 * FPGA Region - Device Tree support for FPGA programming under Linux
4 * Copyright (C) 2013-2016 Altera Corporation
5 * Copyright (C) 2017 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/fpga/fpga-bridge.h>
21 #include <linux/fpga/fpga-mgr.h>
22 #include <linux/fpga/fpga-region.h>
23 #include <linux/idr.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
30 static DEFINE_IDA(fpga_region_ida);
31 static struct class *fpga_region_class;
33 struct fpga_region *fpga_region_class_find(
34 struct device *start, const void *data,
35 int (*match)(struct device *, const void *))
39 dev = class_find_device(fpga_region_class, start, data, match);
43 return to_fpga_region(dev);
45 EXPORT_SYMBOL_GPL(fpga_region_class_find);
48 * fpga_region_get - get an exclusive reference to a fpga region
49 * @region: FPGA Region struct
51 * Caller should call fpga_region_put() when done with region.
53 * Return fpga_region struct if successful.
54 * Return -EBUSY if someone already has a reference to the region.
55 * Return -ENODEV if @np is not a FPGA Region.
57 static struct fpga_region *fpga_region_get(struct fpga_region *region)
59 struct device *dev = ®ion->dev;
61 if (!mutex_trylock(®ion->mutex)) {
62 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
63 return ERR_PTR(-EBUSY);
67 if (!try_module_get(dev->parent->driver->owner)) {
69 mutex_unlock(®ion->mutex);
70 return ERR_PTR(-ENODEV);
73 dev_dbg(dev, "get\n");
79 * fpga_region_put - release a reference to a region
81 * @region: FPGA region
83 static void fpga_region_put(struct fpga_region *region)
85 struct device *dev = ®ion->dev;
87 dev_dbg(dev, "put\n");
89 module_put(dev->parent->driver->owner);
91 mutex_unlock(®ion->mutex);
95 * fpga_region_program_fpga - program FPGA
96 * @region: FPGA region
97 * Program an FPGA using fpga image info (region->info).
98 * Return 0 for success or negative error code.
100 int fpga_region_program_fpga(struct fpga_region *region)
102 struct device *dev = ®ion->dev;
103 struct fpga_image_info *info = region->info;
106 region = fpga_region_get(region);
107 if (IS_ERR(region)) {
108 dev_err(dev, "failed to get FPGA region\n");
109 return PTR_ERR(region);
112 ret = fpga_mgr_lock(region->mgr);
114 dev_err(dev, "FPGA manager is busy\n");
119 * In some cases, we already have a list of bridges in the
120 * fpga region struct. Or we don't have any bridges.
122 if (region->get_bridges) {
123 ret = region->get_bridges(region);
125 dev_err(dev, "failed to get fpga region bridges\n");
130 ret = fpga_bridges_disable(®ion->bridge_list);
132 dev_err(dev, "failed to disable bridges\n");
136 ret = fpga_mgr_load(region->mgr, info);
138 dev_err(dev, "failed to load FPGA image\n");
142 ret = fpga_bridges_enable(®ion->bridge_list);
144 dev_err(dev, "failed to enable region bridges\n");
148 fpga_mgr_unlock(region->mgr);
149 fpga_region_put(region);
154 if (region->get_bridges)
155 fpga_bridges_put(®ion->bridge_list);
157 fpga_mgr_unlock(region->mgr);
159 fpga_region_put(region);
163 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
165 int fpga_region_register(struct device *dev, struct fpga_region *region)
169 id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
173 mutex_init(®ion->mutex);
174 INIT_LIST_HEAD(®ion->bridge_list);
175 device_initialize(®ion->dev);
176 region->dev.groups = region->groups;
177 region->dev.class = fpga_region_class;
178 region->dev.parent = dev;
179 region->dev.of_node = dev->of_node;
181 dev_set_drvdata(dev, region);
183 ret = dev_set_name(®ion->dev, "region%d", id);
187 ret = device_add(®ion->dev);
194 ida_simple_remove(&fpga_region_ida, id);
197 EXPORT_SYMBOL_GPL(fpga_region_register);
199 int fpga_region_unregister(struct fpga_region *region)
201 device_unregister(®ion->dev);
205 EXPORT_SYMBOL_GPL(fpga_region_unregister);
207 static void fpga_region_dev_release(struct device *dev)
209 struct fpga_region *region = to_fpga_region(dev);
211 ida_simple_remove(&fpga_region_ida, region->dev.id);
215 * fpga_region_init - init function for fpga_region class
216 * Creates the fpga_region class and registers a reconfig notifier.
218 static int __init fpga_region_init(void)
220 fpga_region_class = class_create(THIS_MODULE, "fpga_region");
221 if (IS_ERR(fpga_region_class))
222 return PTR_ERR(fpga_region_class);
224 fpga_region_class->dev_release = fpga_region_dev_release;
229 static void __exit fpga_region_exit(void)
231 class_destroy(fpga_region_class);
232 ida_destroy(&fpga_region_ida);
235 subsys_initcall(fpga_region_init);
236 module_exit(fpga_region_exit);
238 MODULE_DESCRIPTION("FPGA Region");
240 MODULE_LICENSE("GPL v2");