2 * FPGA Region - Device Tree support for FPGA programming under Linux
4 * Copyright (C) 2013-2016 Altera Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/fpga/fpga-bridge.h>
20 #include <linux/fpga/fpga-mgr.h>
21 #include <linux/fpga/fpga-region.h>
22 #include <linux/idr.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/of_platform.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);
47 static const struct of_device_id fpga_region_of_match[] = {
48 { .compatible = "fpga-region", },
51 MODULE_DEVICE_TABLE(of, fpga_region_of_match);
53 static int fpga_region_of_node_match(struct device *dev, const void *data)
55 return dev->of_node == data;
59 * of_fpga_region_find - find FPGA region
60 * @np: device node of FPGA Region
62 * Caller will need to put_device(®ion->dev) when done.
64 * Returns FPGA Region struct or NULL
66 static struct fpga_region *of_fpga_region_find(struct device_node *np)
68 return fpga_region_class_find(NULL, np, fpga_region_of_node_match);
72 * fpga_region_get - get an exclusive reference to a fpga region
73 * @region: FPGA Region struct
75 * Caller should call fpga_region_put() when done with region.
77 * Return fpga_region struct if successful.
78 * Return -EBUSY if someone already has a reference to the region.
79 * Return -ENODEV if @np is not a FPGA Region.
81 static struct fpga_region *fpga_region_get(struct fpga_region *region)
83 struct device *dev = ®ion->dev;
85 if (!mutex_trylock(®ion->mutex)) {
86 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
87 return ERR_PTR(-EBUSY);
91 if (!try_module_get(dev->parent->driver->owner)) {
93 mutex_unlock(®ion->mutex);
94 return ERR_PTR(-ENODEV);
97 dev_dbg(dev, "get\n");
103 * fpga_region_put - release a reference to a region
105 * @region: FPGA region
107 static void fpga_region_put(struct fpga_region *region)
109 struct device *dev = ®ion->dev;
111 dev_dbg(dev, "put\n");
113 module_put(dev->parent->driver->owner);
115 mutex_unlock(®ion->mutex);
119 * of_fpga_region_get_mgr - get reference for FPGA manager
120 * @np: device node of FPGA region
122 * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
124 * Caller should call fpga_mgr_put() when done with manager.
126 * Return: fpga manager struct or IS_ERR() condition containing error code.
128 static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
130 struct device_node *mgr_node;
131 struct fpga_manager *mgr;
135 if (of_device_is_compatible(np, "fpga-region")) {
136 mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
138 mgr = of_fpga_mgr_get(mgr_node);
143 np = of_get_next_parent(np);
147 return ERR_PTR(-EINVAL);
151 * of_fpga_region_get_bridges - create a list of bridges
152 * @region: FPGA region
154 * Create a list of bridges including the parent bridge and the bridges
155 * specified by "fpga-bridges" property. Note that the
156 * fpga_bridges_enable/disable/put functions are all fine with an empty list
159 * Caller should call fpga_bridges_put(®ion->bridge_list) when
160 * done with the bridges.
162 * Return 0 for success (even if there are no bridges specified)
163 * or -EBUSY if any of the bridges are in use.
165 static int of_fpga_region_get_bridges(struct fpga_region *region)
167 struct device *dev = ®ion->dev;
168 struct device_node *region_np = dev->of_node;
169 struct fpga_image_info *info = region->info;
170 struct device_node *br, *np, *parent_br = NULL;
173 /* If parent is a bridge, add to list */
174 ret = of_fpga_bridge_get_to_list(region_np->parent, info,
175 ®ion->bridge_list);
177 /* -EBUSY means parent is a bridge that is under use. Give up. */
181 /* Zero return code means parent was a bridge and was added to list. */
183 parent_br = region_np->parent;
185 /* If overlay has a list of bridges, use it. */
186 if (of_parse_phandle(info->overlay, "fpga-bridges", 0))
192 br = of_parse_phandle(np, "fpga-bridges", i);
196 /* If parent bridge is in list, skip it. */
200 /* If node is a bridge, get it and add to list */
201 ret = of_fpga_bridge_get_to_list(br, info,
202 ®ion->bridge_list);
204 /* If any of the bridges are in use, give up */
206 fpga_bridges_put(®ion->bridge_list);
215 * fpga_region_program_fpga - program FPGA
216 * @region: FPGA region
217 * Program an FPGA using fpga image info (region->info).
218 * Return 0 for success or negative error code.
220 int fpga_region_program_fpga(struct fpga_region *region)
222 struct device *dev = ®ion->dev;
223 struct fpga_image_info *info = region->info;
226 region = fpga_region_get(region);
227 if (IS_ERR(region)) {
228 dev_err(dev, "failed to get FPGA region\n");
229 return PTR_ERR(region);
232 ret = fpga_mgr_lock(region->mgr);
234 dev_err(dev, "FPGA manager is busy\n");
239 * In some cases, we already have a list of bridges in the
240 * fpga region struct. Or we don't have any bridges.
242 if (region->get_bridges) {
243 ret = region->get_bridges(region);
245 dev_err(dev, "failed to get fpga region bridges\n");
250 ret = fpga_bridges_disable(®ion->bridge_list);
252 dev_err(dev, "failed to disable bridges\n");
256 ret = fpga_mgr_load(region->mgr, info);
258 dev_err(dev, "failed to load FPGA image\n");
262 ret = fpga_bridges_enable(®ion->bridge_list);
264 dev_err(dev, "failed to enable region bridges\n");
268 fpga_mgr_unlock(region->mgr);
269 fpga_region_put(region);
274 if (region->get_bridges)
275 fpga_bridges_put(®ion->bridge_list);
277 fpga_mgr_unlock(region->mgr);
279 fpga_region_put(region);
283 EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
286 * child_regions_with_firmware
287 * @overlay: device node of the overlay
289 * If the overlay adds child FPGA regions, they are not allowed to have
290 * firmware-name property.
292 * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
294 static int child_regions_with_firmware(struct device_node *overlay)
296 struct device_node *child_region;
297 const char *child_firmware_name;
300 of_node_get(overlay);
302 child_region = of_find_matching_node(overlay, fpga_region_of_match);
303 while (child_region) {
304 if (!of_property_read_string(child_region, "firmware-name",
305 &child_firmware_name)) {
309 child_region = of_find_matching_node(child_region,
310 fpga_region_of_match);
313 of_node_put(child_region);
316 pr_err("firmware-name not allowed in child FPGA region: %pOF",
323 * of_fpga_region_parse_ov - parse and check overlay applied to region
325 * @region: FPGA region
326 * @overlay: overlay applied to the FPGA region
328 * Given an overlay applied to a FPGA region, parse the FPGA image specific
329 * info in the overlay and do some checking.
332 * NULL if overlay doesn't direct us to program the FPGA.
333 * fpga_image_info struct if there is an image to program.
334 * error code for invalid overlay.
336 static struct fpga_image_info *of_fpga_region_parse_ov(
337 struct fpga_region *region,
338 struct device_node *overlay)
340 struct device *dev = ®ion->dev;
341 struct fpga_image_info *info;
342 const char *firmware_name;
346 dev_err(dev, "Region already has overlay applied.\n");
347 return ERR_PTR(-EINVAL);
351 * Reject overlay if child FPGA Regions added in the overlay have
352 * firmware-name property (would mean that an FPGA region that has
353 * not been added to the live tree yet is doing FPGA programming).
355 ret = child_regions_with_firmware(overlay);
359 info = fpga_image_info_alloc(dev);
361 return ERR_PTR(-ENOMEM);
363 info->overlay = overlay;
365 /* Read FPGA region properties from the overlay */
366 if (of_property_read_bool(overlay, "partial-fpga-config"))
367 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
369 if (of_property_read_bool(overlay, "external-fpga-config"))
370 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
372 if (of_property_read_bool(overlay, "encrypted-fpga-config"))
373 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
375 if (!of_property_read_string(overlay, "firmware-name",
377 info->firmware_name = devm_kstrdup(dev, firmware_name,
379 if (!info->firmware_name)
380 return ERR_PTR(-ENOMEM);
383 of_property_read_u32(overlay, "region-unfreeze-timeout-us",
384 &info->enable_timeout_us);
386 of_property_read_u32(overlay, "region-freeze-timeout-us",
387 &info->disable_timeout_us);
389 of_property_read_u32(overlay, "config-complete-timeout-us",
390 &info->config_complete_timeout_us);
392 /* If overlay is not programming the FPGA, don't need FPGA image info */
393 if (!info->firmware_name) {
399 * If overlay informs us FPGA was externally programmed, specifying
400 * firmware here would be ambiguous.
402 if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
403 dev_err(dev, "error: specified firmware and external-fpga-config");
410 fpga_image_info_free(info);
415 * of_fpga_region_notify_pre_apply - pre-apply overlay notification
417 * @region: FPGA region that the overlay was applied to
418 * @nd: overlay notification data
420 * Called when an overlay targeted to a FPGA Region is about to be applied.
421 * Parses the overlay for properties that influence how the FPGA will be
422 * programmed and does some checking. If the checks pass, programs the FPGA.
423 * If the checks fail, overlay is rejected and does not get added to the
426 * Returns 0 for success or negative error code for failure.
428 static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
429 struct of_overlay_notify_data *nd)
431 struct device *dev = ®ion->dev;
432 struct fpga_image_info *info;
436 dev_err(dev, "Region already has overlay applied.\n");
440 info = of_fpga_region_parse_ov(region, nd->overlay);
442 return PTR_ERR(info);
448 ret = fpga_region_program_fpga(region);
450 /* error; reject overlay */
451 fpga_image_info_free(info);
459 * of_fpga_region_notify_post_remove - post-remove overlay notification
461 * @region: FPGA region that was targeted by the overlay that was removed
462 * @nd: overlay notification data
464 * Called after an overlay has been removed if the overlay's target was a
467 static void of_fpga_region_notify_post_remove(struct fpga_region *region,
468 struct of_overlay_notify_data *nd)
470 fpga_bridges_disable(®ion->bridge_list);
471 fpga_bridges_put(®ion->bridge_list);
472 fpga_image_info_free(region->info);
477 * of_fpga_region_notify - reconfig notifier for dynamic DT changes
478 * @nb: notifier block
479 * @action: notifier action
480 * @arg: reconfig data
482 * This notifier handles programming a FPGA when a "firmware-name" property is
483 * added to a fpga-region.
485 * Returns NOTIFY_OK or error if FPGA programming fails.
487 static int of_fpga_region_notify(struct notifier_block *nb,
488 unsigned long action, void *arg)
490 struct of_overlay_notify_data *nd = arg;
491 struct fpga_region *region;
495 case OF_OVERLAY_PRE_APPLY:
496 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
498 case OF_OVERLAY_POST_APPLY:
499 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
500 return NOTIFY_OK; /* not for us */
501 case OF_OVERLAY_PRE_REMOVE:
502 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
503 return NOTIFY_OK; /* not for us */
504 case OF_OVERLAY_POST_REMOVE:
505 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
507 default: /* should not happen */
511 region = of_fpga_region_find(nd->target);
517 case OF_OVERLAY_PRE_APPLY:
518 ret = of_fpga_region_notify_pre_apply(region, nd);
521 case OF_OVERLAY_POST_REMOVE:
522 of_fpga_region_notify_post_remove(region, nd);
526 put_device(®ion->dev);
529 return notifier_from_errno(ret);
534 static struct notifier_block fpga_region_of_nb = {
535 .notifier_call = of_fpga_region_notify,
538 int fpga_region_register(struct device *dev, struct fpga_region *region)
542 id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
546 mutex_init(®ion->mutex);
547 INIT_LIST_HEAD(®ion->bridge_list);
548 device_initialize(®ion->dev);
549 region->dev.class = fpga_region_class;
550 region->dev.parent = dev;
551 region->dev.of_node = dev->of_node;
553 dev_set_drvdata(dev, region);
555 ret = dev_set_name(®ion->dev, "region%d", id);
559 ret = device_add(®ion->dev);
566 ida_simple_remove(&fpga_region_ida, id);
569 EXPORT_SYMBOL_GPL(fpga_region_register);
571 int fpga_region_unregister(struct fpga_region *region)
573 device_unregister(®ion->dev);
577 EXPORT_SYMBOL_GPL(fpga_region_unregister);
579 static int of_fpga_region_probe(struct platform_device *pdev)
581 struct device *dev = &pdev->dev;
582 struct device_node *np = dev->of_node;
583 struct fpga_region *region;
584 struct fpga_manager *mgr;
587 /* Find the FPGA mgr specified by region or parent region. */
588 mgr = of_fpga_region_get_mgr(np);
590 return -EPROBE_DEFER;
592 region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
600 /* Specify how to get bridges for this type of region. */
601 region->get_bridges = of_fpga_region_get_bridges;
603 ret = fpga_region_register(dev, region);
607 of_platform_populate(np, fpga_region_of_match, NULL, ®ion->dev);
609 dev_info(dev, "FPGA Region probed\n");
618 static int of_fpga_region_remove(struct platform_device *pdev)
620 struct fpga_region *region = platform_get_drvdata(pdev);
622 fpga_region_unregister(region);
623 fpga_mgr_put(region->mgr);
628 static struct platform_driver of_fpga_region_driver = {
629 .probe = of_fpga_region_probe,
630 .remove = of_fpga_region_remove,
632 .name = "fpga-region",
633 .of_match_table = of_match_ptr(fpga_region_of_match),
637 static void fpga_region_dev_release(struct device *dev)
639 struct fpga_region *region = to_fpga_region(dev);
641 ida_simple_remove(&fpga_region_ida, region->dev.id);
645 * fpga_region_init - init function for fpga_region class
646 * Creates the fpga_region class and registers a reconfig notifier.
648 static int __init fpga_region_init(void)
652 fpga_region_class = class_create(THIS_MODULE, "fpga_region");
653 if (IS_ERR(fpga_region_class))
654 return PTR_ERR(fpga_region_class);
656 fpga_region_class->dev_release = fpga_region_dev_release;
658 ret = of_overlay_notifier_register(&fpga_region_of_nb);
662 ret = platform_driver_register(&of_fpga_region_driver);
669 of_overlay_notifier_unregister(&fpga_region_of_nb);
671 class_destroy(fpga_region_class);
672 ida_destroy(&fpga_region_ida);
676 static void __exit fpga_region_exit(void)
678 platform_driver_unregister(&of_fpga_region_driver);
679 of_overlay_notifier_unregister(&fpga_region_of_nb);
680 class_destroy(fpga_region_class);
681 ida_destroy(&fpga_region_ida);
684 subsys_initcall(fpga_region_init);
685 module_exit(fpga_region_exit);
687 MODULE_DESCRIPTION("FPGA Region");
689 MODULE_LICENSE("GPL v2");