xref: /openbmc/linux/drivers/fpga/fpga-region.c (revision ba61bb17)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * FPGA Region - Device Tree support for FPGA programming under Linux
4  *
5  *  Copyright (C) 2013-2016 Altera Corporation
6  *  Copyright (C) 2017 Intel Corporation
7  */
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>
17 
18 static DEFINE_IDA(fpga_region_ida);
19 static struct class *fpga_region_class;
20 
21 struct 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 }
33 EXPORT_SYMBOL_GPL(fpga_region_class_find);
34 
35 /**
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  */
45 static 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);
55 	if (!try_module_get(dev->parent->driver->owner)) {
56 		put_device(dev);
57 		mutex_unlock(&region->mutex);
58 		return ERR_PTR(-ENODEV);
59 	}
60 
61 	dev_dbg(dev, "get\n");
62 
63 	return region;
64 }
65 
66 /**
67  * fpga_region_put - release a reference to a region
68  *
69  * @region: FPGA region
70  */
71 static void fpga_region_put(struct fpga_region *region)
72 {
73 	struct device *dev = &region->dev;
74 
75 	dev_dbg(dev, "put\n");
76 
77 	module_put(dev->parent->driver->owner);
78 	put_device(dev);
79 	mutex_unlock(&region->mutex);
80 }
81 
82 /**
83  * fpga_region_program_fpga - program FPGA
84  *
85  * @region: FPGA region
86  *
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.
93  *
94  * Return 0 for success or negative error code.
95  */
96 int fpga_region_program_fpga(struct fpga_region *region)
97 {
98 	struct device *dev = &region->dev;
99 	struct fpga_image_info *info = region->info;
100 	int ret;
101 
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);
106 	}
107 
108 	ret = fpga_mgr_lock(region->mgr);
109 	if (ret) {
110 		dev_err(dev, "FPGA manager is busy\n");
111 		goto err_put_region;
112 	}
113 
114 	/*
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 		}
124 	}
125 
126 	ret = fpga_bridges_disable(&region->bridge_list);
127 	if (ret) {
128 		dev_err(dev, "failed to disable bridges\n");
129 		goto err_put_br;
130 	}
131 
132 	ret = fpga_mgr_load(region->mgr, info);
133 	if (ret) {
134 		dev_err(dev, "failed to load FPGA image\n");
135 		goto err_put_br;
136 	}
137 
138 	ret = fpga_bridges_enable(&region->bridge_list);
139 	if (ret) {
140 		dev_err(dev, "failed to enable region bridges\n");
141 		goto err_put_br;
142 	}
143 
144 	fpga_mgr_unlock(region->mgr);
145 	fpga_region_put(region);
146 
147 	return 0;
148 
149 err_put_br:
150 	if (region->get_bridges)
151 		fpga_bridges_put(&region->bridge_list);
152 err_unlock_mgr:
153 	fpga_mgr_unlock(region->mgr);
154 err_put_region:
155 	fpga_region_put(region);
156 
157 	return ret;
158 }
159 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
160 
161 /**
162  * fpga_region_create - alloc and init a struct fpga_region
163  * @dev: device parent
164  * @mgr: manager that programs this region
165  * @get_bridges: optional function to get bridges to a list
166  *
167  * Return: struct fpga_region or NULL
168  */
169 struct fpga_region
170 *fpga_region_create(struct device *dev,
171 		    struct fpga_manager *mgr,
172 		    int (*get_bridges)(struct fpga_region *))
173 {
174 	struct fpga_region *region;
175 	int id, ret = 0;
176 
177 	region = kzalloc(sizeof(*region), GFP_KERNEL);
178 	if (!region)
179 		return NULL;
180 
181 	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
182 	if (id < 0)
183 		goto err_free;
184 
185 	region->mgr = mgr;
186 	region->get_bridges = get_bridges;
187 	mutex_init(&region->mutex);
188 	INIT_LIST_HEAD(&region->bridge_list);
189 
190 	device_initialize(&region->dev);
191 	region->dev.class = fpga_region_class;
192 	region->dev.parent = dev;
193 	region->dev.of_node = dev->of_node;
194 	region->dev.id = id;
195 
196 	ret = dev_set_name(&region->dev, "region%d", id);
197 	if (ret)
198 		goto err_remove;
199 
200 	return region;
201 
202 err_remove:
203 	ida_simple_remove(&fpga_region_ida, id);
204 err_free:
205 	kfree(region);
206 
207 	return NULL;
208 }
209 EXPORT_SYMBOL_GPL(fpga_region_create);
210 
211 /**
212  * fpga_region_free - free a struct fpga_region
213  * @region: FPGA region created by fpga_region_create
214  */
215 void fpga_region_free(struct fpga_region *region)
216 {
217 	ida_simple_remove(&fpga_region_ida, region->dev.id);
218 	kfree(region);
219 }
220 EXPORT_SYMBOL_GPL(fpga_region_free);
221 
222 /**
223  * fpga_region_register - register a FPGA region
224  * @region: FPGA region created by fpga_region_create
225  * Return: 0 or -errno
226  */
227 int fpga_region_register(struct fpga_region *region)
228 {
229 	return device_add(&region->dev);
230 
231 }
232 EXPORT_SYMBOL_GPL(fpga_region_register);
233 
234 /**
235  * fpga_region_unregister - unregister and free a FPGA region
236  * @region: FPGA region
237  */
238 void fpga_region_unregister(struct fpga_region *region)
239 {
240 	device_unregister(&region->dev);
241 }
242 EXPORT_SYMBOL_GPL(fpga_region_unregister);
243 
244 static void fpga_region_dev_release(struct device *dev)
245 {
246 	struct fpga_region *region = to_fpga_region(dev);
247 
248 	fpga_region_free(region);
249 }
250 
251 /**
252  * fpga_region_init - init function for fpga_region class
253  * Creates the fpga_region class and registers a reconfig notifier.
254  */
255 static int __init fpga_region_init(void)
256 {
257 	fpga_region_class = class_create(THIS_MODULE, "fpga_region");
258 	if (IS_ERR(fpga_region_class))
259 		return PTR_ERR(fpga_region_class);
260 
261 	fpga_region_class->dev_release = fpga_region_dev_release;
262 
263 	return 0;
264 }
265 
266 static void __exit fpga_region_exit(void)
267 {
268 	class_destroy(fpga_region_class);
269 	ida_destroy(&fpga_region_ida);
270 }
271 
272 subsys_initcall(fpga_region_init);
273 module_exit(fpga_region_exit);
274 
275 MODULE_DESCRIPTION("FPGA Region");
276 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
277 MODULE_LICENSE("GPL v2");
278