KVM: arm64: Fix order of vcpu_write_sys_reg() arguments
[linux/fpc-iii.git] / drivers / fpga / fpga-region.c
blobedab2a2e03ef883c9bb11b73c39fa9dc476b1329
1 /*
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
14 * more details.
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 *))
37 struct device *dev;
39 dev = class_find_device(fpga_region_class, start, data, match);
40 if (!dev)
41 return NULL;
43 return to_fpga_region(dev);
45 EXPORT_SYMBOL_GPL(fpga_region_class_find);
47 /**
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 = &region->dev;
61 if (!mutex_trylock(&region->mutex)) {
62 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
63 return ERR_PTR(-EBUSY);
66 get_device(dev);
67 if (!try_module_get(dev->parent->driver->owner)) {
68 put_device(dev);
69 mutex_unlock(&region->mutex);
70 return ERR_PTR(-ENODEV);
73 dev_dbg(dev, "get\n");
75 return region;
78 /**
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 = &region->dev;
87 dev_dbg(dev, "put\n");
89 module_put(dev->parent->driver->owner);
90 put_device(dev);
91 mutex_unlock(&region->mutex);
94 /**
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 = &region->dev;
103 struct fpga_image_info *info = region->info;
104 int ret;
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);
113 if (ret) {
114 dev_err(dev, "FPGA manager is busy\n");
115 goto err_put_region;
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);
124 if (ret) {
125 dev_err(dev, "failed to get fpga region bridges\n");
126 goto err_unlock_mgr;
130 ret = fpga_bridges_disable(&region->bridge_list);
131 if (ret) {
132 dev_err(dev, "failed to disable bridges\n");
133 goto err_put_br;
136 ret = fpga_mgr_load(region->mgr, info);
137 if (ret) {
138 dev_err(dev, "failed to load FPGA image\n");
139 goto err_put_br;
142 ret = fpga_bridges_enable(&region->bridge_list);
143 if (ret) {
144 dev_err(dev, "failed to enable region bridges\n");
145 goto err_put_br;
148 fpga_mgr_unlock(region->mgr);
149 fpga_region_put(region);
151 return 0;
153 err_put_br:
154 if (region->get_bridges)
155 fpga_bridges_put(&region->bridge_list);
156 err_unlock_mgr:
157 fpga_mgr_unlock(region->mgr);
158 err_put_region:
159 fpga_region_put(region);
161 return ret;
163 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
165 int fpga_region_register(struct device *dev, struct fpga_region *region)
167 int id, ret = 0;
169 id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
170 if (id < 0)
171 return id;
173 mutex_init(&region->mutex);
174 INIT_LIST_HEAD(&region->bridge_list);
175 device_initialize(&region->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;
180 region->dev.id = id;
181 dev_set_drvdata(dev, region);
183 ret = dev_set_name(&region->dev, "region%d", id);
184 if (ret)
185 goto err_remove;
187 ret = device_add(&region->dev);
188 if (ret)
189 goto err_remove;
191 return 0;
193 err_remove:
194 ida_simple_remove(&fpga_region_ida, id);
195 return ret;
197 EXPORT_SYMBOL_GPL(fpga_region_register);
199 int fpga_region_unregister(struct fpga_region *region)
201 device_unregister(&region->dev);
203 return 0;
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;
226 return 0;
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");
239 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
240 MODULE_LICENSE("GPL v2");