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/idr.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/of_platform.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
30 * struct fpga_region - FPGA Region structure
31 * @dev: FPGA Region device
32 * @mutex: enforces exclusive reference to region
33 * @bridge_list: list of FPGA bridges specified in region
35 * @info: fpga image specific information
39 struct mutex mutex; /* for exclusive reference to region */
40 struct list_head bridge_list;
41 struct fpga_manager *mgr;
42 struct fpga_image_info *info;
45 #define to_fpga_region(d) container_of(d, struct fpga_region, dev)
47 static DEFINE_IDA(fpga_region_ida);
48 static struct class *fpga_region_class;
50 static const struct of_device_id fpga_region_of_match[] = {
51 { .compatible = "fpga-region", },
54 MODULE_DEVICE_TABLE(of, fpga_region_of_match);
56 static int fpga_region_of_node_match(struct device *dev, const void *data)
58 return dev->of_node == data;
62 * fpga_region_find - find FPGA region
63 * @np: device node of FPGA Region
64 * Caller will need to put_device(®ion->dev) when done.
65 * Returns FPGA Region struct or NULL
67 static struct fpga_region *fpga_region_find(struct device_node *np)
71 dev = class_find_device(fpga_region_class, NULL, np,
72 fpga_region_of_node_match);
76 return to_fpga_region(dev);
80 * fpga_region_get - get an exclusive reference to a fpga region
81 * @region: FPGA Region struct
83 * Caller should call fpga_region_put() when done with region.
85 * Return fpga_region struct if successful.
86 * Return -EBUSY if someone already has a reference to the region.
87 * Return -ENODEV if @np is not a FPGA Region.
89 static struct fpga_region *fpga_region_get(struct fpga_region *region)
91 struct device *dev = ®ion->dev;
93 if (!mutex_trylock(®ion->mutex)) {
94 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
95 return ERR_PTR(-EBUSY);
99 if (!try_module_get(dev->parent->driver->owner)) {
101 mutex_unlock(®ion->mutex);
102 return ERR_PTR(-ENODEV);
105 dev_dbg(dev, "get\n");
111 * fpga_region_put - release a reference to a region
113 * @region: FPGA region
115 static void fpga_region_put(struct fpga_region *region)
117 struct device *dev = ®ion->dev;
119 dev_dbg(dev, "put\n");
121 module_put(dev->parent->driver->owner);
123 mutex_unlock(®ion->mutex);
127 * fpga_region_get_manager - get reference for FPGA manager
128 * @np: device node of FPGA region
130 * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
132 * Caller should call fpga_mgr_put() when done with manager.
134 * Return: fpga manager struct or IS_ERR() condition containing error code.
136 static struct fpga_manager *fpga_region_get_manager(struct device_node *np)
138 struct device_node *mgr_node;
139 struct fpga_manager *mgr;
143 if (of_device_is_compatible(np, "fpga-region")) {
144 mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
146 mgr = of_fpga_mgr_get(mgr_node);
151 np = of_get_next_parent(np);
155 return ERR_PTR(-EINVAL);
159 * fpga_region_get_bridges - create a list of bridges
160 * @region: FPGA region
161 * @overlay: device node of the overlay
163 * Create a list of bridges including the parent bridge and the bridges
164 * specified by "fpga-bridges" property. Note that the
165 * fpga_bridges_enable/disable/put functions are all fine with an empty list
168 * Caller should call fpga_bridges_put(®ion->bridge_list) when
169 * done with the bridges.
171 * Return 0 for success (even if there are no bridges specified)
172 * or -EBUSY if any of the bridges are in use.
174 static int fpga_region_get_bridges(struct fpga_region *region,
175 struct device_node *overlay)
177 struct device *dev = ®ion->dev;
178 struct device_node *region_np = dev->of_node;
179 struct device_node *br, *np, *parent_br = NULL;
182 /* If parent is a bridge, add to list */
183 ret = of_fpga_bridge_get_to_list(region_np->parent, region->info,
184 ®ion->bridge_list);
186 /* -EBUSY means parent is a bridge that is under use. Give up. */
190 /* Zero return code means parent was a bridge and was added to list. */
192 parent_br = region_np->parent;
194 /* If overlay has a list of bridges, use it. */
195 if (of_parse_phandle(overlay, "fpga-bridges", 0))
201 br = of_parse_phandle(np, "fpga-bridges", i);
205 /* If parent bridge is in list, skip it. */
209 /* If node is a bridge, get it and add to list */
210 ret = of_fpga_bridge_get_to_list(br, region->info,
211 ®ion->bridge_list);
213 /* If any of the bridges are in use, give up */
215 fpga_bridges_put(®ion->bridge_list);
224 * fpga_region_program_fpga - program FPGA
225 * @region: FPGA region
226 * @overlay: device node of the overlay
227 * Program an FPGA using information in the region's fpga image info.
228 * Return 0 for success or negative error code.
230 static int fpga_region_program_fpga(struct fpga_region *region,
231 struct device_node *overlay)
233 struct device *dev = ®ion->dev;
236 region = fpga_region_get(region);
237 if (IS_ERR(region)) {
238 dev_err(dev, "failed to get FPGA region\n");
239 return PTR_ERR(region);
242 ret = fpga_mgr_lock(region->mgr);
244 dev_err(dev, "FPGA manager is busy\n");
248 ret = fpga_region_get_bridges(region, overlay);
250 dev_err(dev, "failed to get FPGA bridges\n");
254 ret = fpga_bridges_disable(®ion->bridge_list);
256 dev_err(dev, "failed to disable bridges\n");
260 ret = fpga_mgr_load(region->mgr, region->info);
262 dev_err(dev, "failed to load FPGA image\n");
266 ret = fpga_bridges_enable(®ion->bridge_list);
268 dev_err(dev, "failed to enable region bridges\n");
272 fpga_mgr_unlock(region->mgr);
273 fpga_region_put(region);
278 fpga_bridges_put(®ion->bridge_list);
280 fpga_mgr_unlock(region->mgr);
282 fpga_region_put(region);
288 * child_regions_with_firmware
289 * @overlay: device node of the overlay
291 * If the overlay adds child FPGA regions, they are not allowed to have
292 * firmware-name property.
294 * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
296 static int child_regions_with_firmware(struct device_node *overlay)
298 struct device_node *child_region;
299 const char *child_firmware_name;
302 of_node_get(overlay);
304 child_region = of_find_matching_node(overlay, fpga_region_of_match);
305 while (child_region) {
306 if (!of_property_read_string(child_region, "firmware-name",
307 &child_firmware_name)) {
311 child_region = of_find_matching_node(child_region,
312 fpga_region_of_match);
315 of_node_put(child_region);
318 pr_err("firmware-name not allowed in child FPGA region: %pOF",
325 * fpga_region_notify_pre_apply - pre-apply overlay notification
327 * @region: FPGA region that the overlay was applied to
328 * @nd: overlay notification data
330 * Called after when an overlay targeted to a FPGA Region is about to be
331 * applied. Function will check the properties that will be added to the FPGA
332 * region. If the checks pass, it will program the FPGA.
335 * The overlay must add either firmware-name or external-fpga-config property
336 * to the FPGA Region.
338 * firmware-name : program the FPGA
339 * external-fpga-config : FPGA is already programmed
340 * encrypted-fpga-config : FPGA bitstream is encrypted
342 * The overlay can add other FPGA regions, but child FPGA regions cannot have a
343 * firmware-name property since those regions don't exist yet.
345 * If the overlay that breaks the rules, notifier returns an error and the
346 * overlay is rejected before it goes into the main tree.
348 * Returns 0 for success or negative error code for failure.
350 static int fpga_region_notify_pre_apply(struct fpga_region *region,
351 struct of_overlay_notify_data *nd)
353 struct device *dev = ®ion->dev;
354 struct fpga_image_info *info;
355 const char *firmware_name;
359 dev_err(dev, "Region already has overlay applied.\n");
364 * Reject overlay if child FPGA Regions added in the overlay have
365 * firmware-name property (would mean that an FPGA region that has
366 * not been added to the live tree yet is doing FPGA programming).
368 ret = child_regions_with_firmware(nd->overlay);
372 info = fpga_image_info_alloc(dev);
376 /* Read FPGA region properties from the overlay */
377 if (of_property_read_bool(nd->overlay, "partial-fpga-config"))
378 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
380 if (of_property_read_bool(nd->overlay, "external-fpga-config"))
381 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
383 if (of_property_read_bool(nd->overlay, "encrypted-fpga-config"))
384 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
386 if (!of_property_read_string(nd->overlay, "firmware-name",
388 info->firmware_name = devm_kstrdup(dev, firmware_name,
390 if (!info->firmware_name)
394 of_property_read_u32(nd->overlay, "region-unfreeze-timeout-us",
395 &info->enable_timeout_us);
397 of_property_read_u32(nd->overlay, "region-freeze-timeout-us",
398 &info->disable_timeout_us);
400 of_property_read_u32(nd->overlay, "config-complete-timeout-us",
401 &info->config_complete_timeout_us);
403 /* If FPGA was externally programmed, don't specify firmware */
404 if ((info->flags & FPGA_MGR_EXTERNAL_CONFIG) && info->firmware_name) {
405 dev_err(dev, "error: specified firmware and external-fpga-config");
406 fpga_image_info_free(info);
410 /* FPGA is already configured externally. We're done. */
411 if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
412 fpga_image_info_free(info);
416 /* If we got this far, we should be programming the FPGA */
417 if (!info->firmware_name) {
418 dev_err(dev, "should specify firmware-name or external-fpga-config\n");
419 fpga_image_info_free(info);
424 ret = fpga_region_program_fpga(region, nd->overlay);
426 fpga_image_info_free(info);
434 * fpga_region_notify_post_remove - post-remove overlay notification
436 * @region: FPGA region that was targeted by the overlay that was removed
437 * @nd: overlay notification data
439 * Called after an overlay has been removed if the overlay's target was a
442 static void fpga_region_notify_post_remove(struct fpga_region *region,
443 struct of_overlay_notify_data *nd)
445 fpga_bridges_disable(®ion->bridge_list);
446 fpga_bridges_put(®ion->bridge_list);
447 fpga_image_info_free(region->info);
452 * of_fpga_region_notify - reconfig notifier for dynamic DT changes
453 * @nb: notifier block
454 * @action: notifier action
455 * @arg: reconfig data
457 * This notifier handles programming a FPGA when a "firmware-name" property is
458 * added to a fpga-region.
460 * Returns NOTIFY_OK or error if FPGA programming fails.
462 static int of_fpga_region_notify(struct notifier_block *nb,
463 unsigned long action, void *arg)
465 struct of_overlay_notify_data *nd = arg;
466 struct fpga_region *region;
470 case OF_OVERLAY_PRE_APPLY:
471 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
473 case OF_OVERLAY_POST_APPLY:
474 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
475 return NOTIFY_OK; /* not for us */
476 case OF_OVERLAY_PRE_REMOVE:
477 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
478 return NOTIFY_OK; /* not for us */
479 case OF_OVERLAY_POST_REMOVE:
480 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
482 default: /* should not happen */
486 region = fpga_region_find(nd->target);
492 case OF_OVERLAY_PRE_APPLY:
493 ret = fpga_region_notify_pre_apply(region, nd);
496 case OF_OVERLAY_POST_REMOVE:
497 fpga_region_notify_post_remove(region, nd);
501 put_device(®ion->dev);
504 return notifier_from_errno(ret);
509 static struct notifier_block fpga_region_of_nb = {
510 .notifier_call = of_fpga_region_notify,
513 static int fpga_region_probe(struct platform_device *pdev)
515 struct device *dev = &pdev->dev;
516 struct device_node *np = dev->of_node;
517 struct fpga_region *region;
518 struct fpga_manager *mgr;
521 mgr = fpga_region_get_manager(np);
523 return -EPROBE_DEFER;
525 region = kzalloc(sizeof(*region), GFP_KERNEL);
533 id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
539 mutex_init(®ion->mutex);
540 INIT_LIST_HEAD(®ion->bridge_list);
542 device_initialize(®ion->dev);
543 region->dev.class = fpga_region_class;
544 region->dev.parent = dev;
545 region->dev.of_node = np;
547 dev_set_drvdata(dev, region);
549 ret = dev_set_name(®ion->dev, "region%d", id);
553 ret = device_add(®ion->dev);
557 of_platform_populate(np, fpga_region_of_match, NULL, ®ion->dev);
559 dev_info(dev, "FPGA Region probed\n");
564 ida_simple_remove(&fpga_region_ida, id);
573 static int fpga_region_remove(struct platform_device *pdev)
575 struct fpga_region *region = platform_get_drvdata(pdev);
577 device_unregister(®ion->dev);
578 fpga_mgr_put(region->mgr);
583 static struct platform_driver fpga_region_driver = {
584 .probe = fpga_region_probe,
585 .remove = fpga_region_remove,
587 .name = "fpga-region",
588 .of_match_table = of_match_ptr(fpga_region_of_match),
592 static void fpga_region_dev_release(struct device *dev)
594 struct fpga_region *region = to_fpga_region(dev);
596 ida_simple_remove(&fpga_region_ida, region->dev.id);
601 * fpga_region_init - init function for fpga_region class
602 * Creates the fpga_region class and registers a reconfig notifier.
604 static int __init fpga_region_init(void)
608 fpga_region_class = class_create(THIS_MODULE, "fpga_region");
609 if (IS_ERR(fpga_region_class))
610 return PTR_ERR(fpga_region_class);
612 fpga_region_class->dev_release = fpga_region_dev_release;
614 ret = of_overlay_notifier_register(&fpga_region_of_nb);
618 ret = platform_driver_register(&fpga_region_driver);
625 of_overlay_notifier_unregister(&fpga_region_of_nb);
627 class_destroy(fpga_region_class);
628 ida_destroy(&fpga_region_ida);
632 static void __exit fpga_region_exit(void)
634 platform_driver_unregister(&fpga_region_driver);
635 of_overlay_notifier_unregister(&fpga_region_of_nb);
636 class_destroy(fpga_region_class);
637 ida_destroy(&fpga_region_ida);
640 subsys_initcall(fpga_region_init);
641 module_exit(fpga_region_exit);
643 MODULE_DESCRIPTION("FPGA Region");
645 MODULE_LICENSE("GPL v2");