1 // SPDX-License-Identifier: GPL-2.0
3 * FPGA Region - Device Tree support for FPGA programming under Linux
5 * Copyright (C) 2013-2016 Altera Corporation
6 * Copyright (C) 2017 Intel Corporation
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>
19 static const struct of_device_id fpga_region_of_match[] = {
20 { .compatible = "fpga-region", },
23 MODULE_DEVICE_TABLE(of, fpga_region_of_match);
26 * of_fpga_region_find - find FPGA region
27 * @np: device node of FPGA Region
29 * Caller will need to put_device(®ion->dev) when done.
31 * Returns FPGA Region struct or NULL
33 static struct fpga_region *of_fpga_region_find(struct device_node *np)
35 return fpga_region_class_find(NULL, np, device_match_of_node);
39 * of_fpga_region_get_mgr - get reference for FPGA manager
40 * @np: device node of FPGA region
42 * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
44 * Caller should call fpga_mgr_put() when done with manager.
46 * Return: fpga manager struct or IS_ERR() condition containing error code.
48 static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
50 struct device_node *mgr_node;
51 struct fpga_manager *mgr;
55 if (of_device_is_compatible(np, "fpga-region")) {
56 mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
58 mgr = of_fpga_mgr_get(mgr_node);
59 of_node_put(mgr_node);
64 np = of_get_next_parent(np);
68 return ERR_PTR(-EINVAL);
72 * of_fpga_region_get_bridges - create a list of bridges
73 * @region: FPGA region
75 * Create a list of bridges including the parent bridge and the bridges
76 * specified by "fpga-bridges" property. Note that the
77 * fpga_bridges_enable/disable/put functions are all fine with an empty list
80 * Caller should call fpga_bridges_put(®ion->bridge_list) when
81 * done with the bridges.
83 * Return 0 for success (even if there are no bridges specified)
84 * or -EBUSY if any of the bridges are in use.
86 static int of_fpga_region_get_bridges(struct fpga_region *region)
88 struct device *dev = ®ion->dev;
89 struct device_node *region_np = dev->of_node;
90 struct fpga_image_info *info = region->info;
91 struct device_node *br, *np, *parent_br = NULL;
94 /* If parent is a bridge, add to list */
95 ret = of_fpga_bridge_get_to_list(region_np->parent, info,
96 ®ion->bridge_list);
98 /* -EBUSY means parent is a bridge that is under use. Give up. */
102 /* Zero return code means parent was a bridge and was added to list. */
104 parent_br = region_np->parent;
106 /* If overlay has a list of bridges, use it. */
107 br = of_parse_phandle(info->overlay, "fpga-bridges", 0);
116 br = of_parse_phandle(np, "fpga-bridges", i);
120 /* If parent bridge is in list, skip it. */
121 if (br == parent_br) {
126 /* If node is a bridge, get it and add to list */
127 ret = of_fpga_bridge_get_to_list(br, info,
128 ®ion->bridge_list);
131 /* If any of the bridges are in use, give up */
133 fpga_bridges_put(®ion->bridge_list);
142 * child_regions_with_firmware
143 * @overlay: device node of the overlay
145 * If the overlay adds child FPGA regions, they are not allowed to have
146 * firmware-name property.
148 * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
150 static int child_regions_with_firmware(struct device_node *overlay)
152 struct device_node *child_region;
153 const char *child_firmware_name;
156 of_node_get(overlay);
158 child_region = of_find_matching_node(overlay, fpga_region_of_match);
159 while (child_region) {
160 if (!of_property_read_string(child_region, "firmware-name",
161 &child_firmware_name)) {
165 child_region = of_find_matching_node(child_region,
166 fpga_region_of_match);
169 of_node_put(child_region);
172 pr_err("firmware-name not allowed in child FPGA region: %pOF",
179 * of_fpga_region_parse_ov - parse and check overlay applied to region
181 * @region: FPGA region
182 * @overlay: overlay applied to the FPGA region
184 * Given an overlay applied to an FPGA region, parse the FPGA image specific
185 * info in the overlay and do some checking.
188 * NULL if overlay doesn't direct us to program the FPGA.
189 * fpga_image_info struct if there is an image to program.
190 * error code for invalid overlay.
192 static struct fpga_image_info *of_fpga_region_parse_ov(
193 struct fpga_region *region,
194 struct device_node *overlay)
196 struct device *dev = ®ion->dev;
197 struct fpga_image_info *info;
198 const char *firmware_name;
202 dev_err(dev, "Region already has overlay applied.\n");
203 return ERR_PTR(-EINVAL);
207 * Reject overlay if child FPGA Regions added in the overlay have
208 * firmware-name property (would mean that an FPGA region that has
209 * not been added to the live tree yet is doing FPGA programming).
211 ret = child_regions_with_firmware(overlay);
215 info = fpga_image_info_alloc(dev);
217 return ERR_PTR(-ENOMEM);
219 info->overlay = overlay;
221 /* Read FPGA region properties from the overlay */
222 if (of_property_read_bool(overlay, "partial-fpga-config"))
223 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
225 if (of_property_read_bool(overlay, "external-fpga-config"))
226 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
228 if (of_property_read_bool(overlay, "encrypted-fpga-config"))
229 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
231 if (!of_property_read_string(overlay, "firmware-name",
233 info->firmware_name = devm_kstrdup(dev, firmware_name,
235 if (!info->firmware_name)
236 return ERR_PTR(-ENOMEM);
239 of_property_read_u32(overlay, "region-unfreeze-timeout-us",
240 &info->enable_timeout_us);
242 of_property_read_u32(overlay, "region-freeze-timeout-us",
243 &info->disable_timeout_us);
245 of_property_read_u32(overlay, "config-complete-timeout-us",
246 &info->config_complete_timeout_us);
248 /* If overlay is not programming the FPGA, don't need FPGA image info */
249 if (!info->firmware_name) {
255 * If overlay informs us FPGA was externally programmed, specifying
256 * firmware here would be ambiguous.
258 if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
259 dev_err(dev, "error: specified firmware and external-fpga-config");
266 fpga_image_info_free(info);
271 * of_fpga_region_notify_pre_apply - pre-apply overlay notification
273 * @region: FPGA region that the overlay was applied to
274 * @nd: overlay notification data
276 * Called when an overlay targeted to an FPGA Region is about to be applied.
277 * Parses the overlay for properties that influence how the FPGA will be
278 * programmed and does some checking. If the checks pass, programs the FPGA.
279 * If the checks fail, overlay is rejected and does not get added to the
282 * Returns 0 for success or negative error code for failure.
284 static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
285 struct of_overlay_notify_data *nd)
287 struct device *dev = ®ion->dev;
288 struct fpga_image_info *info;
291 info = of_fpga_region_parse_ov(region, nd->overlay);
293 return PTR_ERR(info);
295 /* If overlay doesn't program the FPGA, accept it anyway. */
300 dev_err(dev, "Region already has overlay applied.\n");
305 ret = fpga_region_program_fpga(region);
307 /* error; reject overlay */
308 fpga_image_info_free(info);
316 * of_fpga_region_notify_post_remove - post-remove overlay notification
318 * @region: FPGA region that was targeted by the overlay that was removed
319 * @nd: overlay notification data
321 * Called after an overlay has been removed if the overlay's target was a
324 static void of_fpga_region_notify_post_remove(struct fpga_region *region,
325 struct of_overlay_notify_data *nd)
327 fpga_bridges_disable(®ion->bridge_list);
328 fpga_bridges_put(®ion->bridge_list);
329 fpga_image_info_free(region->info);
334 * of_fpga_region_notify - reconfig notifier for dynamic DT changes
335 * @nb: notifier block
336 * @action: notifier action
337 * @arg: reconfig data
339 * This notifier handles programming an FPGA when a "firmware-name" property is
340 * added to an fpga-region.
342 * Returns NOTIFY_OK or error if FPGA programming fails.
344 static int of_fpga_region_notify(struct notifier_block *nb,
345 unsigned long action, void *arg)
347 struct of_overlay_notify_data *nd = arg;
348 struct fpga_region *region;
352 case OF_OVERLAY_PRE_APPLY:
353 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
355 case OF_OVERLAY_POST_APPLY:
356 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
357 return NOTIFY_OK; /* not for us */
358 case OF_OVERLAY_PRE_REMOVE:
359 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
360 return NOTIFY_OK; /* not for us */
361 case OF_OVERLAY_POST_REMOVE:
362 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
364 default: /* should not happen */
368 region = of_fpga_region_find(nd->target);
374 case OF_OVERLAY_PRE_APPLY:
375 ret = of_fpga_region_notify_pre_apply(region, nd);
378 case OF_OVERLAY_POST_REMOVE:
379 of_fpga_region_notify_post_remove(region, nd);
383 put_device(®ion->dev);
386 return notifier_from_errno(ret);
391 static struct notifier_block fpga_region_of_nb = {
392 .notifier_call = of_fpga_region_notify,
395 static int of_fpga_region_probe(struct platform_device *pdev)
397 struct device *dev = &pdev->dev;
398 struct device_node *np = dev->of_node;
399 struct fpga_region *region;
400 struct fpga_manager *mgr;
403 /* Find the FPGA mgr specified by region or parent region. */
404 mgr = of_fpga_region_get_mgr(np);
406 return -EPROBE_DEFER;
408 region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
414 ret = fpga_region_register(region);
418 of_platform_populate(np, fpga_region_of_match, NULL, ®ion->dev);
419 platform_set_drvdata(pdev, region);
421 dev_info(dev, "FPGA Region probed\n");
430 static int of_fpga_region_remove(struct platform_device *pdev)
432 struct fpga_region *region = platform_get_drvdata(pdev);
433 struct fpga_manager *mgr = region->mgr;
435 fpga_region_unregister(region);
441 static struct platform_driver of_fpga_region_driver = {
442 .probe = of_fpga_region_probe,
443 .remove = of_fpga_region_remove,
445 .name = "of-fpga-region",
446 .of_match_table = of_match_ptr(fpga_region_of_match),
451 * fpga_region_init - init function for fpga_region class
452 * Creates the fpga_region class and registers a reconfig notifier.
454 static int __init of_fpga_region_init(void)
458 ret = of_overlay_notifier_register(&fpga_region_of_nb);
462 ret = platform_driver_register(&of_fpga_region_driver);
469 of_overlay_notifier_unregister(&fpga_region_of_nb);
473 static void __exit of_fpga_region_exit(void)
475 platform_driver_unregister(&of_fpga_region_driver);
476 of_overlay_notifier_unregister(&fpga_region_of_nb);
479 subsys_initcall(of_fpga_region_init);
480 module_exit(of_fpga_region_exit);
482 MODULE_DESCRIPTION("FPGA Region");
483 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
484 MODULE_LICENSE("GPL v2");