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/of_platform.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18
19static const struct of_device_id fpga_region_of_match[] = {
20 { .compatible = "fpga-region", },
21 {},
22};
23MODULE_DEVICE_TABLE(of, fpga_region_of_match);
24
25static int fpga_region_of_node_match(struct device *dev, const void *data)
26{
27 return dev->of_node == data;
28}
29
30/**
31 * of_fpga_region_find - find FPGA region
32 * @np: device node of FPGA Region
33 *
34 * Caller will need to put_device(&region->dev) when done.
35 *
36 * Returns FPGA Region struct or NULL
37 */
38static struct fpga_region *of_fpga_region_find(struct device_node *np)
39{
40 return fpga_region_class_find(NULL, np, fpga_region_of_node_match);
41}
42
43/**
44 * of_fpga_region_get_mgr - get reference for FPGA manager
45 * @np: device node of FPGA region
46 *
47 * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
48 *
49 * Caller should call fpga_mgr_put() when done with manager.
50 *
51 * Return: fpga manager struct or IS_ERR() condition containing error code.
52 */
53static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
54{
55 struct device_node *mgr_node;
56 struct fpga_manager *mgr;
57
58 of_node_get(np);
59 while (np) {
60 if (of_device_is_compatible(np, "fpga-region")) {
61 mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
62 if (mgr_node) {
63 mgr = of_fpga_mgr_get(mgr_node);
64 of_node_put(mgr_node);
65 of_node_put(np);
66 return mgr;
67 }
68 }
69 np = of_get_next_parent(np);
70 }
71 of_node_put(np);
72
73 return ERR_PTR(-EINVAL);
74}
75
76/**
77 * of_fpga_region_get_bridges - create a list of bridges
78 * @region: FPGA region
79 *
80 * Create a list of bridges including the parent bridge and the bridges
81 * specified by "fpga-bridges" property. Note that the
82 * fpga_bridges_enable/disable/put functions are all fine with an empty list
83 * if that happens.
84 *
85 * Caller should call fpga_bridges_put(&region->bridge_list) when
86 * done with the bridges.
87 *
88 * Return 0 for success (even if there are no bridges specified)
89 * or -EBUSY if any of the bridges are in use.
90 */
91static int of_fpga_region_get_bridges(struct fpga_region *region)
92{
93 struct device *dev = &region->dev;
94 struct device_node *region_np = dev->of_node;
95 struct fpga_image_info *info = region->info;
96 struct device_node *br, *np, *parent_br = NULL;
97 int i, ret;
98
99 /* If parent is a bridge, add to list */
100 ret = of_fpga_bridge_get_to_list(region_np->parent, info,
101 &region->bridge_list);
102
103 /* -EBUSY means parent is a bridge that is under use. Give up. */
104 if (ret == -EBUSY)
105 return ret;
106
107 /* Zero return code means parent was a bridge and was added to list. */
108 if (!ret)
109 parent_br = region_np->parent;
110
111 /* If overlay has a list of bridges, use it. */
112 br = of_parse_phandle(info->overlay, "fpga-bridges", 0);
113 if (br) {
114 of_node_put(br);
115 np = info->overlay;
116 } else {
117 np = region_np;
118 }
119
120 for (i = 0; ; i++) {
121 br = of_parse_phandle(np, "fpga-bridges", i);
122 if (!br)
123 break;
124
125 /* If parent bridge is in list, skip it. */
126 if (br == parent_br) {
127 of_node_put(br);
128 continue;
129 }
130
131 /* If node is a bridge, get it and add to list */
132 ret = of_fpga_bridge_get_to_list(br, info,
133 &region->bridge_list);
134 of_node_put(br);
135
136 /* If any of the bridges are in use, give up */
137 if (ret == -EBUSY) {
138 fpga_bridges_put(&region->bridge_list);
139 return -EBUSY;
140 }
141 }
142
143 return 0;
144}
145
146/**
147 * child_regions_with_firmware
148 * @overlay: device node of the overlay
149 *
150 * If the overlay adds child FPGA regions, they are not allowed to have
151 * firmware-name property.
152 *
153 * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
154 */
155static int child_regions_with_firmware(struct device_node *overlay)
156{
157 struct device_node *child_region;
158 const char *child_firmware_name;
159 int ret = 0;
160
161 of_node_get(overlay);
162
163 child_region = of_find_matching_node(overlay, fpga_region_of_match);
164 while (child_region) {
165 if (!of_property_read_string(child_region, "firmware-name",
166 &child_firmware_name)) {
167 ret = -EINVAL;
168 break;
169 }
170 child_region = of_find_matching_node(child_region,
171 fpga_region_of_match);
172 }
173
174 of_node_put(child_region);
175
176 if (ret)
177 pr_err("firmware-name not allowed in child FPGA region: %pOF",
178 child_region);
179
180 return ret;
181}
182
183/**
184 * of_fpga_region_parse_ov - parse and check overlay applied to region
185 *
186 * @region: FPGA region
187 * @overlay: overlay applied to the FPGA region
188 *
189 * Given an overlay applied to a FPGA region, parse the FPGA image specific
190 * info in the overlay and do some checking.
191 *
192 * Returns:
193 * NULL if overlay doesn't direct us to program the FPGA.
194 * fpga_image_info struct if there is an image to program.
195 * error code for invalid overlay.
196 */
197static struct fpga_image_info *of_fpga_region_parse_ov(
198 struct fpga_region *region,
199 struct device_node *overlay)
200{
201 struct device *dev = &region->dev;
202 struct fpga_image_info *info;
203 const char *firmware_name;
204 int ret;
205
206 if (region->info) {
207 dev_err(dev, "Region already has overlay applied.\n");
208 return ERR_PTR(-EINVAL);
209 }
210
211 /*
212 * Reject overlay if child FPGA Regions added in the overlay have
213 * firmware-name property (would mean that an FPGA region that has
214 * not been added to the live tree yet is doing FPGA programming).
215 */
216 ret = child_regions_with_firmware(overlay);
217 if (ret)
218 return ERR_PTR(ret);
219
220 info = fpga_image_info_alloc(dev);
221 if (!info)
222 return ERR_PTR(-ENOMEM);
223
224 info->overlay = overlay;
225
226 /* Read FPGA region properties from the overlay */
227 if (of_property_read_bool(overlay, "partial-fpga-config"))
228 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
229
230 if (of_property_read_bool(overlay, "external-fpga-config"))
231 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
232
233 if (of_property_read_bool(overlay, "encrypted-fpga-config"))
234 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
235
236 if (!of_property_read_string(overlay, "firmware-name",
237 &firmware_name)) {
238 info->firmware_name = devm_kstrdup(dev, firmware_name,
239 GFP_KERNEL);
240 if (!info->firmware_name)
241 return ERR_PTR(-ENOMEM);
242 }
243
244 of_property_read_u32(overlay, "region-unfreeze-timeout-us",
245 &info->enable_timeout_us);
246
247 of_property_read_u32(overlay, "region-freeze-timeout-us",
248 &info->disable_timeout_us);
249
250 of_property_read_u32(overlay, "config-complete-timeout-us",
251 &info->config_complete_timeout_us);
252
253 /* If overlay is not programming the FPGA, don't need FPGA image info */
254 if (!info->firmware_name) {
255 ret = 0;
256 goto ret_no_info;
257 }
258
259 /*
260 * If overlay informs us FPGA was externally programmed, specifying
261 * firmware here would be ambiguous.
262 */
263 if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
264 dev_err(dev, "error: specified firmware and external-fpga-config");
265 ret = -EINVAL;
266 goto ret_no_info;
267 }
268
269 return info;
270ret_no_info:
271 fpga_image_info_free(info);
272 return ERR_PTR(ret);
273}
274
275/**
276 * of_fpga_region_notify_pre_apply - pre-apply overlay notification
277 *
278 * @region: FPGA region that the overlay was applied to
279 * @nd: overlay notification data
280 *
281 * Called when an overlay targeted to a FPGA Region is about to be applied.
282 * Parses the overlay for properties that influence how the FPGA will be
283 * programmed and does some checking. If the checks pass, programs the FPGA.
284 * If the checks fail, overlay is rejected and does not get added to the
285 * live tree.
286 *
287 * Returns 0 for success or negative error code for failure.
288 */
289static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
290 struct of_overlay_notify_data *nd)
291{
292 struct device *dev = &region->dev;
293 struct fpga_image_info *info;
294 int ret;
295
296 info = of_fpga_region_parse_ov(region, nd->overlay);
297 if (IS_ERR(info))
298 return PTR_ERR(info);
299
300 /* If overlay doesn't program the FPGA, accept it anyway. */
301 if (!info)
302 return 0;
303
304 if (region->info) {
305 dev_err(dev, "Region already has overlay applied.\n");
306 return -EINVAL;
307 }
308
309 region->info = info;
310 ret = fpga_region_program_fpga(region);
311 if (ret) {
312 /* error; reject overlay */
313 fpga_image_info_free(info);
314 region->info = NULL;
315 }
316
317 return ret;
318}
319
320/**
321 * of_fpga_region_notify_post_remove - post-remove overlay notification
322 *
323 * @region: FPGA region that was targeted by the overlay that was removed
324 * @nd: overlay notification data
325 *
326 * Called after an overlay has been removed if the overlay's target was a
327 * FPGA region.
328 */
329static void of_fpga_region_notify_post_remove(struct fpga_region *region,
330 struct of_overlay_notify_data *nd)
331{
332 fpga_bridges_disable(&region->bridge_list);
333 fpga_bridges_put(&region->bridge_list);
334 fpga_image_info_free(region->info);
335 region->info = NULL;
336}
337
338/**
339 * of_fpga_region_notify - reconfig notifier for dynamic DT changes
340 * @nb: notifier block
341 * @action: notifier action
342 * @arg: reconfig data
343 *
344 * This notifier handles programming a FPGA when a "firmware-name" property is
345 * added to a fpga-region.
346 *
347 * Returns NOTIFY_OK or error if FPGA programming fails.
348 */
349static int of_fpga_region_notify(struct notifier_block *nb,
350 unsigned long action, void *arg)
351{
352 struct of_overlay_notify_data *nd = arg;
353 struct fpga_region *region;
354 int ret;
355
356 switch (action) {
357 case OF_OVERLAY_PRE_APPLY:
358 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
359 break;
360 case OF_OVERLAY_POST_APPLY:
361 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
362 return NOTIFY_OK; /* not for us */
363 case OF_OVERLAY_PRE_REMOVE:
364 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
365 return NOTIFY_OK; /* not for us */
366 case OF_OVERLAY_POST_REMOVE:
367 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
368 break;
369 default: /* should not happen */
370 return NOTIFY_OK;
371 }
372
373 region = of_fpga_region_find(nd->target);
374 if (!region)
375 return NOTIFY_OK;
376
377 ret = 0;
378 switch (action) {
379 case OF_OVERLAY_PRE_APPLY:
380 ret = of_fpga_region_notify_pre_apply(region, nd);
381 break;
382
383 case OF_OVERLAY_POST_REMOVE:
384 of_fpga_region_notify_post_remove(region, nd);
385 break;
386 }
387
388 put_device(&region->dev);
389
390 if (ret)
391 return notifier_from_errno(ret);
392
393 return NOTIFY_OK;
394}
395
396static struct notifier_block fpga_region_of_nb = {
397 .notifier_call = of_fpga_region_notify,
398};
399
400static int of_fpga_region_probe(struct platform_device *pdev)
401{
402 struct device *dev = &pdev->dev;
403 struct device_node *np = dev->of_node;
404 struct fpga_region *region;
405 struct fpga_manager *mgr;
406 int ret;
407
408 /* Find the FPGA mgr specified by region or parent region. */
409 mgr = of_fpga_region_get_mgr(np);
410 if (IS_ERR(mgr))
411 return -EPROBE_DEFER;
412
413 region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
414 if (!region) {
415 ret = -ENOMEM;
416 goto eprobe_mgr_put;
417 }
418
419 ret = fpga_region_register(region);
420 if (ret)
421 goto eprobe_mgr_put;
422
423 of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
424 platform_set_drvdata(pdev, region);
425
426 dev_info(dev, "FPGA Region probed\n");
427
428 return 0;
429
430eprobe_mgr_put:
431 fpga_mgr_put(mgr);
432 return ret;
433}
434
435static int of_fpga_region_remove(struct platform_device *pdev)
436{
437 struct fpga_region *region = platform_get_drvdata(pdev);
438 struct fpga_manager *mgr = region->mgr;
439
440 fpga_region_unregister(region);
441 fpga_mgr_put(mgr);
442
443 return 0;
444}
445
446static struct platform_driver of_fpga_region_driver = {
447 .probe = of_fpga_region_probe,
448 .remove = of_fpga_region_remove,
449 .driver = {
450 .name = "of-fpga-region",
451 .of_match_table = of_match_ptr(fpga_region_of_match),
452 },
453};
454
455/**
456 * fpga_region_init - init function for fpga_region class
457 * Creates the fpga_region class and registers a reconfig notifier.
458 */
459static int __init of_fpga_region_init(void)
460{
461 int ret;
462
463 ret = of_overlay_notifier_register(&fpga_region_of_nb);
464 if (ret)
465 return ret;
466
467 ret = platform_driver_register(&of_fpga_region_driver);
468 if (ret)
469 goto err_plat;
470
471 return 0;
472
473err_plat:
474 of_overlay_notifier_unregister(&fpga_region_of_nb);
475 return ret;
476}
477
478static void __exit of_fpga_region_exit(void)
479{
480 platform_driver_unregister(&of_fpga_region_driver);
481 of_overlay_notifier_unregister(&fpga_region_of_nb);
482}
483
484subsys_initcall(of_fpga_region_init);
485module_exit(of_fpga_region_exit);
486
487MODULE_DESCRIPTION("FPGA Region");
488MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
489MODULE_LICENSE("GPL v2");
490