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>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
21 static const struct of_device_id fpga_region_of_match[] = {
22 { .compatible = "fpga-region", },
25 MODULE_DEVICE_TABLE(of, fpga_region_of_match);
28 * of_fpga_region_find - find FPGA region
29 * @np: device node of FPGA Region
31 * Caller will need to put_device(®ion->dev) when done.
33 * Return: FPGA Region struct or NULL
35 static struct fpga_region *of_fpga_region_find(struct device_node *np)
37 return fpga_region_class_find(NULL, np, device_match_of_node);
41 * of_fpga_region_get_mgr - get reference for FPGA manager
42 * @np: device node of FPGA region
44 * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
46 * Caller should call fpga_mgr_put() when done with manager.
48 * Return: fpga manager struct or IS_ERR() condition containing error code.
50 static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
52 struct device_node *mgr_node;
53 struct fpga_manager *mgr;
57 if (of_device_is_compatible(np, "fpga-region")) {
58 mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
60 mgr = of_fpga_mgr_get(mgr_node);
61 of_node_put(mgr_node);
66 np = of_get_next_parent(np);
70 return ERR_PTR(-EINVAL);
74 * of_fpga_region_get_bridges - create a list of bridges
75 * @region: FPGA region
77 * Create a list of bridges including the parent bridge and the bridges
78 * specified by "fpga-bridges" property. Note that the
79 * fpga_bridges_enable/disable/put functions are all fine with an empty list
82 * Caller should call fpga_bridges_put(®ion->bridge_list) when
83 * done with the bridges.
85 * Return: 0 for success (even if there are no bridges specified)
86 * or -EBUSY if any of the bridges are in use.
88 static int of_fpga_region_get_bridges(struct fpga_region *region)
90 struct device *dev = ®ion->dev;
91 struct device_node *region_np = dev->of_node;
92 struct fpga_image_info *info = region->info;
93 struct device_node *br, *np, *parent_br = NULL;
96 /* If parent is a bridge, add to list */
97 ret = of_fpga_bridge_get_to_list(region_np->parent, info,
98 ®ion->bridge_list);
100 /* -EBUSY means parent is a bridge that is under use. Give up. */
104 /* Zero return code means parent was a bridge and was added to list. */
106 parent_br = region_np->parent;
108 /* If overlay has a list of bridges, use it. */
109 br = of_parse_phandle(info->overlay, "fpga-bridges", 0);
118 br = of_parse_phandle(np, "fpga-bridges", i);
122 /* If parent bridge is in list, skip it. */
123 if (br == parent_br) {
128 /* If node is a bridge, get it and add to list */
129 ret = of_fpga_bridge_get_to_list(br, info,
130 ®ion->bridge_list);
133 /* If any of the bridges are in use, give up */
135 fpga_bridges_put(®ion->bridge_list);
144 * child_regions_with_firmware - Used to check the child region info.
145 * @overlay: device node of the overlay
147 * If the overlay adds child FPGA regions, they are not allowed to have
148 * firmware-name property.
150 * Return: 0 for OK or -EINVAL if child FPGA region adds firmware-name.
152 static int child_regions_with_firmware(struct device_node *overlay)
154 struct device_node *child_region;
155 const char *child_firmware_name;
158 of_node_get(overlay);
160 child_region = of_find_matching_node(overlay, fpga_region_of_match);
161 while (child_region) {
162 if (!of_property_read_string(child_region, "firmware-name",
163 &child_firmware_name)) {
167 child_region = of_find_matching_node(child_region,
168 fpga_region_of_match);
171 of_node_put(child_region);
174 pr_err("firmware-name not allowed in child FPGA region: %pOF",
181 * of_fpga_region_parse_ov - parse and check overlay applied to region
183 * @region: FPGA region
184 * @overlay: overlay applied to the FPGA region
186 * Given an overlay applied to an FPGA region, parse the FPGA image specific
187 * info in the overlay and do some checking.
190 * NULL if overlay doesn't direct us to program the FPGA.
191 * fpga_image_info struct if there is an image to program.
192 * error code for invalid overlay.
194 static struct fpga_image_info *
195 of_fpga_region_parse_ov(struct fpga_region *region,
196 struct device_node *overlay)
198 struct device *dev = ®ion->dev;
199 struct fpga_image_info *info;
200 const char *firmware_name;
204 dev_err(dev, "Region already has overlay applied.\n");
205 return ERR_PTR(-EINVAL);
209 * Reject overlay if child FPGA Regions added in the overlay have
210 * firmware-name property (would mean that an FPGA region that has
211 * not been added to the live tree yet is doing FPGA programming).
213 ret = child_regions_with_firmware(overlay);
217 info = fpga_image_info_alloc(dev);
219 return ERR_PTR(-ENOMEM);
221 info->overlay = overlay;
223 /* Read FPGA region properties from the overlay */
224 if (of_property_read_bool(overlay, "partial-fpga-config"))
225 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
227 if (of_property_read_bool(overlay, "external-fpga-config"))
228 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
230 if (of_property_read_bool(overlay, "encrypted-fpga-config"))
231 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
233 if (!of_property_read_string(overlay, "firmware-name",
235 info->firmware_name = devm_kstrdup(dev, firmware_name,
237 if (!info->firmware_name)
238 return ERR_PTR(-ENOMEM);
241 of_property_read_u32(overlay, "region-unfreeze-timeout-us",
242 &info->enable_timeout_us);
244 of_property_read_u32(overlay, "region-freeze-timeout-us",
245 &info->disable_timeout_us);
247 of_property_read_u32(overlay, "config-complete-timeout-us",
248 &info->config_complete_timeout_us);
250 /* If overlay is not programming the FPGA, don't need FPGA image info */
251 if (!info->firmware_name) {
257 * If overlay informs us FPGA was externally programmed, specifying
258 * firmware here would be ambiguous.
260 if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
261 dev_err(dev, "error: specified firmware and external-fpga-config");
268 fpga_image_info_free(info);
273 * of_fpga_region_notify_pre_apply - pre-apply overlay notification
275 * @region: FPGA region that the overlay was applied to
276 * @nd: overlay notification data
278 * Called when an overlay targeted to an FPGA Region is about to be applied.
279 * Parses the overlay for properties that influence how the FPGA will be
280 * programmed and does some checking. If the checks pass, programs the FPGA.
281 * If the checks fail, overlay is rejected and does not get added to the
284 * Return: 0 for success or negative error code for failure.
286 static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
287 struct of_overlay_notify_data *nd)
289 struct device *dev = ®ion->dev;
290 struct fpga_image_info *info;
293 info = of_fpga_region_parse_ov(region, nd->overlay);
295 return PTR_ERR(info);
297 /* If overlay doesn't program the FPGA, accept it anyway. */
302 dev_err(dev, "Region already has overlay applied.\n");
307 ret = fpga_region_program_fpga(region);
309 /* error; reject overlay */
310 fpga_image_info_free(info);
318 * of_fpga_region_notify_post_remove - post-remove overlay notification
320 * @region: FPGA region that was targeted by the overlay that was removed
321 * @nd: overlay notification data
323 * Called after an overlay has been removed if the overlay's target was a
326 static void of_fpga_region_notify_post_remove(struct fpga_region *region,
327 struct of_overlay_notify_data *nd)
329 fpga_bridges_disable(®ion->bridge_list);
330 fpga_bridges_put(®ion->bridge_list);
331 fpga_image_info_free(region->info);
336 * of_fpga_region_notify - reconfig notifier for dynamic DT changes
337 * @nb: notifier block
338 * @action: notifier action
339 * @arg: reconfig data
341 * This notifier handles programming an FPGA when a "firmware-name" property is
342 * added to an fpga-region.
344 * Return: NOTIFY_OK or error if FPGA programming fails.
346 static int of_fpga_region_notify(struct notifier_block *nb,
347 unsigned long action, void *arg)
349 struct of_overlay_notify_data *nd = arg;
350 struct fpga_region *region;
354 case OF_OVERLAY_PRE_APPLY:
355 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
357 case OF_OVERLAY_POST_APPLY:
358 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
359 return NOTIFY_OK; /* not for us */
360 case OF_OVERLAY_PRE_REMOVE:
361 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
362 return NOTIFY_OK; /* not for us */
363 case OF_OVERLAY_POST_REMOVE:
364 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
366 default: /* should not happen */
370 region = of_fpga_region_find(nd->target);
376 case OF_OVERLAY_PRE_APPLY:
377 ret = of_fpga_region_notify_pre_apply(region, nd);
380 case OF_OVERLAY_POST_REMOVE:
381 of_fpga_region_notify_post_remove(region, nd);
385 put_device(®ion->dev);
388 return notifier_from_errno(ret);
393 static struct notifier_block fpga_region_of_nb = {
394 .notifier_call = of_fpga_region_notify,
397 static int of_fpga_region_probe(struct platform_device *pdev)
399 struct device *dev = &pdev->dev;
400 struct device_node *np = dev->of_node;
401 struct fpga_region *region;
402 struct fpga_manager *mgr;
405 /* Find the FPGA mgr specified by region or parent region. */
406 mgr = of_fpga_region_get_mgr(np);
408 return -EPROBE_DEFER;
410 region = fpga_region_register(dev, mgr, of_fpga_region_get_bridges);
411 if (IS_ERR(region)) {
412 ret = PTR_ERR(region);
416 of_platform_populate(np, fpga_region_of_match, NULL, ®ion->dev);
417 platform_set_drvdata(pdev, region);
419 dev_info(dev, "FPGA Region probed\n");
428 static int of_fpga_region_remove(struct platform_device *pdev)
430 struct fpga_region *region = platform_get_drvdata(pdev);
431 struct fpga_manager *mgr = region->mgr;
433 fpga_region_unregister(region);
439 static struct platform_driver of_fpga_region_driver = {
440 .probe = of_fpga_region_probe,
441 .remove = of_fpga_region_remove,
443 .name = "of-fpga-region",
444 .of_match_table = of_match_ptr(fpga_region_of_match),
449 * of_fpga_region_init - init function for fpga_region class
450 * Creates the fpga_region class and registers a reconfig notifier.
452 * Return: 0 on success, negative error code otherwise.
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");