xref: /openbmc/linux/drivers/fpga/fpga-region.c (revision 49cb93cb090a1515deaef7dcafe905ab25b2809e)
1 /*
2  * FPGA Region - Device Tree support for FPGA programming under Linux
3  *
4  *  Copyright (C) 2013-2016 Altera Corporation
5  *  Copyright (C) 2017 Intel Corporation
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 
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>
29 
30 static DEFINE_IDA(fpga_region_ida);
31 static struct class *fpga_region_class;
32 
33 struct fpga_region *fpga_region_class_find(
34 	struct device *start, const void *data,
35 	int (*match)(struct device *, const void *))
36 {
37 	struct device *dev;
38 
39 	dev = class_find_device(fpga_region_class, start, data, match);
40 	if (!dev)
41 		return NULL;
42 
43 	return to_fpga_region(dev);
44 }
45 EXPORT_SYMBOL_GPL(fpga_region_class_find);
46 
47 /**
48  * fpga_region_get - get an exclusive reference to a fpga region
49  * @region: FPGA Region struct
50  *
51  * Caller should call fpga_region_put() when done with region.
52  *
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.
56  */
57 static struct fpga_region *fpga_region_get(struct fpga_region *region)
58 {
59 	struct device *dev = &region->dev;
60 
61 	if (!mutex_trylock(&region->mutex)) {
62 		dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
63 		return ERR_PTR(-EBUSY);
64 	}
65 
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);
71 	}
72 
73 	dev_dbg(dev, "get\n");
74 
75 	return region;
76 }
77 
78 /**
79  * fpga_region_put - release a reference to a region
80  *
81  * @region: FPGA region
82  */
83 static void fpga_region_put(struct fpga_region *region)
84 {
85 	struct device *dev = &region->dev;
86 
87 	dev_dbg(dev, "put\n");
88 
89 	module_put(dev->parent->driver->owner);
90 	put_device(dev);
91 	mutex_unlock(&region->mutex);
92 }
93 
94 /**
95  * fpga_region_program_fpga - program FPGA
96  * @region: FPGA region
97  * Program an FPGA using fpga image info (region->info).
98  * If the region has a get_bridges function, the exclusive reference for the
99  * bridges will be held if programming succeeds.  This is intended to prevent
100  * reprogramming the region until the caller considers it safe to do so.
101  * The caller will need to call fpga_bridges_put() before attempting to
102  * reprogram the region.
103  * Return 0 for success or negative error code.
104  */
105 int fpga_region_program_fpga(struct fpga_region *region)
106 {
107 	struct device *dev = &region->dev;
108 	struct fpga_image_info *info = region->info;
109 	int ret;
110 
111 	region = fpga_region_get(region);
112 	if (IS_ERR(region)) {
113 		dev_err(dev, "failed to get FPGA region\n");
114 		return PTR_ERR(region);
115 	}
116 
117 	ret = fpga_mgr_lock(region->mgr);
118 	if (ret) {
119 		dev_err(dev, "FPGA manager is busy\n");
120 		goto err_put_region;
121 	}
122 
123 	/*
124 	 * In some cases, we already have a list of bridges in the
125 	 * fpga region struct.  Or we don't have any bridges.
126 	 */
127 	if (region->get_bridges) {
128 		ret = region->get_bridges(region);
129 		if (ret) {
130 			dev_err(dev, "failed to get fpga region bridges\n");
131 			goto err_unlock_mgr;
132 		}
133 	}
134 
135 	ret = fpga_bridges_disable(&region->bridge_list);
136 	if (ret) {
137 		dev_err(dev, "failed to disable bridges\n");
138 		goto err_put_br;
139 	}
140 
141 	ret = fpga_mgr_load(region->mgr, info);
142 	if (ret) {
143 		dev_err(dev, "failed to load FPGA image\n");
144 		goto err_put_br;
145 	}
146 
147 	ret = fpga_bridges_enable(&region->bridge_list);
148 	if (ret) {
149 		dev_err(dev, "failed to enable region bridges\n");
150 		goto err_put_br;
151 	}
152 
153 	fpga_mgr_unlock(region->mgr);
154 	fpga_region_put(region);
155 
156 	return 0;
157 
158 err_put_br:
159 	if (region->get_bridges)
160 		fpga_bridges_put(&region->bridge_list);
161 err_unlock_mgr:
162 	fpga_mgr_unlock(region->mgr);
163 err_put_region:
164 	fpga_region_put(region);
165 
166 	return ret;
167 }
168 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
169 
170 int fpga_region_register(struct device *dev, struct fpga_region *region)
171 {
172 	int id, ret = 0;
173 
174 	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
175 	if (id < 0)
176 		return id;
177 
178 	mutex_init(&region->mutex);
179 	INIT_LIST_HEAD(&region->bridge_list);
180 	device_initialize(&region->dev);
181 	region->dev.groups = region->groups;
182 	region->dev.class = fpga_region_class;
183 	region->dev.parent = dev;
184 	region->dev.of_node = dev->of_node;
185 	region->dev.id = id;
186 	dev_set_drvdata(dev, region);
187 
188 	ret = dev_set_name(&region->dev, "region%d", id);
189 	if (ret)
190 		goto err_remove;
191 
192 	ret = device_add(&region->dev);
193 	if (ret)
194 		goto err_remove;
195 
196 	return 0;
197 
198 err_remove:
199 	ida_simple_remove(&fpga_region_ida, id);
200 	return ret;
201 }
202 EXPORT_SYMBOL_GPL(fpga_region_register);
203 
204 int fpga_region_unregister(struct fpga_region *region)
205 {
206 	device_unregister(&region->dev);
207 
208 	return 0;
209 }
210 EXPORT_SYMBOL_GPL(fpga_region_unregister);
211 
212 static void fpga_region_dev_release(struct device *dev)
213 {
214 	struct fpga_region *region = to_fpga_region(dev);
215 
216 	ida_simple_remove(&fpga_region_ida, region->dev.id);
217 }
218 
219 /**
220  * fpga_region_init - init function for fpga_region class
221  * Creates the fpga_region class and registers a reconfig notifier.
222  */
223 static int __init fpga_region_init(void)
224 {
225 	fpga_region_class = class_create(THIS_MODULE, "fpga_region");
226 	if (IS_ERR(fpga_region_class))
227 		return PTR_ERR(fpga_region_class);
228 
229 	fpga_region_class->dev_release = fpga_region_dev_release;
230 
231 	return 0;
232 }
233 
234 static void __exit fpga_region_exit(void)
235 {
236 	class_destroy(fpga_region_class);
237 	ida_destroy(&fpga_region_ida);
238 }
239 
240 subsys_initcall(fpga_region_init);
241 module_exit(fpga_region_exit);
242 
243 MODULE_DESCRIPTION("FPGA Region");
244 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
245 MODULE_LICENSE("GPL v2");
246