]> Git Repo - linux.git/blob - drivers/fpga/fpga-region.c
fpga: region: use image info as parameter for programming region
[linux.git] / drivers / fpga / fpga-region.c
1 /*
2  * FPGA Region - Device Tree support for FPGA programming under Linux
3  *
4  *  Copyright (C) 2013-2016 Altera Corporation
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
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>
28
29 /**
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
34  * @mgr: FPGA manager
35  * @info: fpga image specific information
36  */
37 struct fpga_region {
38         struct device dev;
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;
43 };
44
45 #define to_fpga_region(d) container_of(d, struct fpga_region, dev)
46
47 static DEFINE_IDA(fpga_region_ida);
48 static struct class *fpga_region_class;
49
50 static const struct of_device_id fpga_region_of_match[] = {
51         { .compatible = "fpga-region", },
52         {},
53 };
54 MODULE_DEVICE_TABLE(of, fpga_region_of_match);
55
56 static int fpga_region_of_node_match(struct device *dev, const void *data)
57 {
58         return dev->of_node == data;
59 }
60
61 /**
62  * fpga_region_find - find FPGA region
63  * @np: device node of FPGA Region
64  * Caller will need to put_device(&region->dev) when done.
65  * Returns FPGA Region struct or NULL
66  */
67 static struct fpga_region *fpga_region_find(struct device_node *np)
68 {
69         struct device *dev;
70
71         dev = class_find_device(fpga_region_class, NULL, np,
72                                 fpga_region_of_node_match);
73         if (!dev)
74                 return NULL;
75
76         return to_fpga_region(dev);
77 }
78
79 /**
80  * fpga_region_get - get an exclusive reference to a fpga region
81  * @region: FPGA Region struct
82  *
83  * Caller should call fpga_region_put() when done with region.
84  *
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.
88  */
89 static struct fpga_region *fpga_region_get(struct fpga_region *region)
90 {
91         struct device *dev = &region->dev;
92
93         if (!mutex_trylock(&region->mutex)) {
94                 dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
95                 return ERR_PTR(-EBUSY);
96         }
97
98         get_device(dev);
99         if (!try_module_get(dev->parent->driver->owner)) {
100                 put_device(dev);
101                 mutex_unlock(&region->mutex);
102                 return ERR_PTR(-ENODEV);
103         }
104
105         dev_dbg(dev, "get\n");
106
107         return region;
108 }
109
110 /**
111  * fpga_region_put - release a reference to a region
112  *
113  * @region: FPGA region
114  */
115 static void fpga_region_put(struct fpga_region *region)
116 {
117         struct device *dev = &region->dev;
118
119         dev_dbg(dev, "put\n");
120
121         module_put(dev->parent->driver->owner);
122         put_device(dev);
123         mutex_unlock(&region->mutex);
124 }
125
126 /**
127  * fpga_region_get_manager - get reference for FPGA manager
128  * @np: device node of FPGA region
129  *
130  * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
131  *
132  * Caller should call fpga_mgr_put() when done with manager.
133  *
134  * Return: fpga manager struct or IS_ERR() condition containing error code.
135  */
136 static struct fpga_manager *fpga_region_get_manager(struct device_node *np)
137 {
138         struct device_node  *mgr_node;
139         struct fpga_manager *mgr;
140
141         of_node_get(np);
142         while (np) {
143                 if (of_device_is_compatible(np, "fpga-region")) {
144                         mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
145                         if (mgr_node) {
146                                 mgr = of_fpga_mgr_get(mgr_node);
147                                 of_node_put(np);
148                                 return mgr;
149                         }
150                 }
151                 np = of_get_next_parent(np);
152         }
153         of_node_put(np);
154
155         return ERR_PTR(-EINVAL);
156 }
157
158 /**
159  * fpga_region_get_bridges - create a list of bridges
160  * @region: FPGA region
161  * @overlay: device node of the overlay
162  *
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
166  * if that happens.
167  *
168  * Caller should call fpga_bridges_put(&region->bridge_list) when
169  * done with the bridges.
170  *
171  * Return 0 for success (even if there are no bridges specified)
172  * or -EBUSY if any of the bridges are in use.
173  */
174 static int fpga_region_get_bridges(struct fpga_region *region,
175                                    struct device_node *overlay)
176 {
177         struct device *dev = &region->dev;
178         struct device_node *region_np = dev->of_node;
179         struct device_node *br, *np, *parent_br = NULL;
180         int i, ret;
181
182         /* If parent is a bridge, add to list */
183         ret = of_fpga_bridge_get_to_list(region_np->parent, region->info,
184                                          &region->bridge_list);
185
186         /* -EBUSY means parent is a bridge that is under use. Give up. */
187         if (ret == -EBUSY)
188                 return ret;
189
190         /* Zero return code means parent was a bridge and was added to list. */
191         if (!ret)
192                 parent_br = region_np->parent;
193
194         /* If overlay has a list of bridges, use it. */
195         if (of_parse_phandle(overlay, "fpga-bridges", 0))
196                 np = overlay;
197         else
198                 np = region_np;
199
200         for (i = 0; ; i++) {
201                 br = of_parse_phandle(np, "fpga-bridges", i);
202                 if (!br)
203                         break;
204
205                 /* If parent bridge is in list, skip it. */
206                 if (br == parent_br)
207                         continue;
208
209                 /* If node is a bridge, get it and add to list */
210                 ret = of_fpga_bridge_get_to_list(br, region->info,
211                                                  &region->bridge_list);
212
213                 /* If any of the bridges are in use, give up */
214                 if (ret == -EBUSY) {
215                         fpga_bridges_put(&region->bridge_list);
216                         return -EBUSY;
217                 }
218         }
219
220         return 0;
221 }
222
223 /**
224  * fpga_region_program_fpga - program FPGA
225  * @region: FPGA region
226  * Program an FPGA using fpga image info (region->info).
227  * Return 0 for success or negative error code.
228  */
229 static int fpga_region_program_fpga(struct fpga_region *region)
230 {
231         struct device *dev = &region->dev;
232         struct fpga_image_info *info = region->info;
233         int ret;
234
235         region = fpga_region_get(region);
236         if (IS_ERR(region)) {
237                 dev_err(dev, "failed to get FPGA region\n");
238                 return PTR_ERR(region);
239         }
240
241         ret = fpga_mgr_lock(region->mgr);
242         if (ret) {
243                 dev_err(dev, "FPGA manager is busy\n");
244                 goto err_put_region;
245         }
246
247         ret = fpga_region_get_bridges(region, info->overlay);
248         if (ret) {
249                 dev_err(dev, "failed to get FPGA bridges\n");
250                 goto err_unlock_mgr;
251         }
252
253         ret = fpga_bridges_disable(&region->bridge_list);
254         if (ret) {
255                 dev_err(dev, "failed to disable bridges\n");
256                 goto err_put_br;
257         }
258
259         ret = fpga_mgr_load(region->mgr, info);
260         if (ret) {
261                 dev_err(dev, "failed to load FPGA image\n");
262                 goto err_put_br;
263         }
264
265         ret = fpga_bridges_enable(&region->bridge_list);
266         if (ret) {
267                 dev_err(dev, "failed to enable region bridges\n");
268                 goto err_put_br;
269         }
270
271         fpga_mgr_unlock(region->mgr);
272         fpga_region_put(region);
273
274         return 0;
275
276 err_put_br:
277         fpga_bridges_put(&region->bridge_list);
278 err_unlock_mgr:
279         fpga_mgr_unlock(region->mgr);
280 err_put_region:
281         fpga_region_put(region);
282
283         return ret;
284 }
285
286 /**
287  * child_regions_with_firmware
288  * @overlay: device node of the overlay
289  *
290  * If the overlay adds child FPGA regions, they are not allowed to have
291  * firmware-name property.
292  *
293  * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
294  */
295 static int child_regions_with_firmware(struct device_node *overlay)
296 {
297         struct device_node *child_region;
298         const char *child_firmware_name;
299         int ret = 0;
300
301         of_node_get(overlay);
302
303         child_region = of_find_matching_node(overlay, fpga_region_of_match);
304         while (child_region) {
305                 if (!of_property_read_string(child_region, "firmware-name",
306                                              &child_firmware_name)) {
307                         ret = -EINVAL;
308                         break;
309                 }
310                 child_region = of_find_matching_node(child_region,
311                                                      fpga_region_of_match);
312         }
313
314         of_node_put(child_region);
315
316         if (ret)
317                 pr_err("firmware-name not allowed in child FPGA region: %pOF",
318                        child_region);
319
320         return ret;
321 }
322
323 /**
324  * fpga_region_notify_pre_apply - pre-apply overlay notification
325  *
326  * @region: FPGA region that the overlay was applied to
327  * @nd: overlay notification data
328  *
329  * Called after when an overlay targeted to a FPGA Region is about to be
330  * applied.  Function will check the properties that will be added to the FPGA
331  * region.  If the checks pass, it will program the FPGA.
332  *
333  * The checks are:
334  * The overlay must add either firmware-name or external-fpga-config property
335  * to the FPGA Region.
336  *
337  *   firmware-name         : program the FPGA
338  *   external-fpga-config  : FPGA is already programmed
339  *   encrypted-fpga-config : FPGA bitstream is encrypted
340  *
341  * The overlay can add other FPGA regions, but child FPGA regions cannot have a
342  * firmware-name property since those regions don't exist yet.
343  *
344  * If the overlay that breaks the rules, notifier returns an error and the
345  * overlay is rejected before it goes into the main tree.
346  *
347  * Returns 0 for success or negative error code for failure.
348  */
349 static int fpga_region_notify_pre_apply(struct fpga_region *region,
350                                         struct of_overlay_notify_data *nd)
351 {
352         struct device *dev = &region->dev;
353         struct fpga_image_info *info;
354         const char *firmware_name;
355         int ret;
356
357         if (region->info) {
358                 dev_err(dev, "Region already has overlay applied.\n");
359                 return -EINVAL;
360         }
361
362         /*
363          * Reject overlay if child FPGA Regions added in the overlay have
364          * firmware-name property (would mean that an FPGA region that has
365          * not been added to the live tree yet is doing FPGA programming).
366          */
367         ret = child_regions_with_firmware(nd->overlay);
368         if (ret)
369                 return ret;
370
371         info = fpga_image_info_alloc(dev);
372         if (!info)
373                 return -ENOMEM;
374
375         info->overlay = nd->overlay;
376
377         /* Read FPGA region properties from the overlay */
378         if (of_property_read_bool(nd->overlay, "partial-fpga-config"))
379                 info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
380
381         if (of_property_read_bool(nd->overlay, "external-fpga-config"))
382                 info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
383
384         if (of_property_read_bool(nd->overlay, "encrypted-fpga-config"))
385                 info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
386
387         if (!of_property_read_string(nd->overlay, "firmware-name",
388                                      &firmware_name)) {
389                 info->firmware_name = devm_kstrdup(dev, firmware_name,
390                                                    GFP_KERNEL);
391                 if (!info->firmware_name)
392                         return -ENOMEM;
393         }
394
395         of_property_read_u32(nd->overlay, "region-unfreeze-timeout-us",
396                              &info->enable_timeout_us);
397
398         of_property_read_u32(nd->overlay, "region-freeze-timeout-us",
399                              &info->disable_timeout_us);
400
401         of_property_read_u32(nd->overlay, "config-complete-timeout-us",
402                              &info->config_complete_timeout_us);
403
404         /* If FPGA was externally programmed, don't specify firmware */
405         if ((info->flags & FPGA_MGR_EXTERNAL_CONFIG) && info->firmware_name) {
406                 dev_err(dev, "error: specified firmware and external-fpga-config");
407                 fpga_image_info_free(info);
408                 return -EINVAL;
409         }
410
411         /* FPGA is already configured externally.  We're done. */
412         if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
413                 fpga_image_info_free(info);
414                 return 0;
415         }
416
417         /* If we got this far, we should be programming the FPGA */
418         if (!info->firmware_name) {
419                 dev_err(dev, "should specify firmware-name or external-fpga-config\n");
420                 fpga_image_info_free(info);
421                 return -EINVAL;
422         }
423
424         region->info = info;
425
426         ret = fpga_region_program_fpga(region);
427         if (ret) {
428                 fpga_image_info_free(info);
429                 region->info = NULL;
430         }
431
432         return ret;
433 }
434
435 /**
436  * fpga_region_notify_post_remove - post-remove overlay notification
437  *
438  * @region: FPGA region that was targeted by the overlay that was removed
439  * @nd: overlay notification data
440  *
441  * Called after an overlay has been removed if the overlay's target was a
442  * FPGA region.
443  */
444 static void fpga_region_notify_post_remove(struct fpga_region *region,
445                                            struct of_overlay_notify_data *nd)
446 {
447         fpga_bridges_disable(&region->bridge_list);
448         fpga_bridges_put(&region->bridge_list);
449         fpga_image_info_free(region->info);
450         region->info = NULL;
451 }
452
453 /**
454  * of_fpga_region_notify - reconfig notifier for dynamic DT changes
455  * @nb:         notifier block
456  * @action:     notifier action
457  * @arg:        reconfig data
458  *
459  * This notifier handles programming a FPGA when a "firmware-name" property is
460  * added to a fpga-region.
461  *
462  * Returns NOTIFY_OK or error if FPGA programming fails.
463  */
464 static int of_fpga_region_notify(struct notifier_block *nb,
465                                  unsigned long action, void *arg)
466 {
467         struct of_overlay_notify_data *nd = arg;
468         struct fpga_region *region;
469         int ret;
470
471         switch (action) {
472         case OF_OVERLAY_PRE_APPLY:
473                 pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
474                 break;
475         case OF_OVERLAY_POST_APPLY:
476                 pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
477                 return NOTIFY_OK;       /* not for us */
478         case OF_OVERLAY_PRE_REMOVE:
479                 pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
480                 return NOTIFY_OK;       /* not for us */
481         case OF_OVERLAY_POST_REMOVE:
482                 pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
483                 break;
484         default:                        /* should not happen */
485                 return NOTIFY_OK;
486         }
487
488         region = fpga_region_find(nd->target);
489         if (!region)
490                 return NOTIFY_OK;
491
492         ret = 0;
493         switch (action) {
494         case OF_OVERLAY_PRE_APPLY:
495                 ret = fpga_region_notify_pre_apply(region, nd);
496                 break;
497
498         case OF_OVERLAY_POST_REMOVE:
499                 fpga_region_notify_post_remove(region, nd);
500                 break;
501         }
502
503         put_device(&region->dev);
504
505         if (ret)
506                 return notifier_from_errno(ret);
507
508         return NOTIFY_OK;
509 }
510
511 static struct notifier_block fpga_region_of_nb = {
512         .notifier_call = of_fpga_region_notify,
513 };
514
515 static int fpga_region_probe(struct platform_device *pdev)
516 {
517         struct device *dev = &pdev->dev;
518         struct device_node *np = dev->of_node;
519         struct fpga_region *region;
520         struct fpga_manager *mgr;
521         int id, ret = 0;
522
523         mgr = fpga_region_get_manager(np);
524         if (IS_ERR(mgr))
525                 return -EPROBE_DEFER;
526
527         region = kzalloc(sizeof(*region), GFP_KERNEL);
528         if (!region) {
529                 ret = -ENOMEM;
530                 goto err_put_mgr;
531         }
532
533         region->mgr = mgr;
534
535         id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
536         if (id < 0) {
537                 ret = id;
538                 goto err_kfree;
539         }
540
541         mutex_init(&region->mutex);
542         INIT_LIST_HEAD(&region->bridge_list);
543
544         device_initialize(&region->dev);
545         region->dev.class = fpga_region_class;
546         region->dev.parent = dev;
547         region->dev.of_node = np;
548         region->dev.id = id;
549         dev_set_drvdata(dev, region);
550
551         ret = dev_set_name(&region->dev, "region%d", id);
552         if (ret)
553                 goto err_remove;
554
555         ret = device_add(&region->dev);
556         if (ret)
557                 goto err_remove;
558
559         of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
560
561         dev_info(dev, "FPGA Region probed\n");
562
563         return 0;
564
565 err_remove:
566         ida_simple_remove(&fpga_region_ida, id);
567 err_kfree:
568         kfree(region);
569 err_put_mgr:
570         fpga_mgr_put(mgr);
571
572         return ret;
573 }
574
575 static int fpga_region_remove(struct platform_device *pdev)
576 {
577         struct fpga_region *region = platform_get_drvdata(pdev);
578
579         device_unregister(&region->dev);
580         fpga_mgr_put(region->mgr);
581
582         return 0;
583 }
584
585 static struct platform_driver fpga_region_driver = {
586         .probe = fpga_region_probe,
587         .remove = fpga_region_remove,
588         .driver = {
589                 .name   = "fpga-region",
590                 .of_match_table = of_match_ptr(fpga_region_of_match),
591         },
592 };
593
594 static void fpga_region_dev_release(struct device *dev)
595 {
596         struct fpga_region *region = to_fpga_region(dev);
597
598         ida_simple_remove(&fpga_region_ida, region->dev.id);
599         kfree(region);
600 }
601
602 /**
603  * fpga_region_init - init function for fpga_region class
604  * Creates the fpga_region class and registers a reconfig notifier.
605  */
606 static int __init fpga_region_init(void)
607 {
608         int ret;
609
610         fpga_region_class = class_create(THIS_MODULE, "fpga_region");
611         if (IS_ERR(fpga_region_class))
612                 return PTR_ERR(fpga_region_class);
613
614         fpga_region_class->dev_release = fpga_region_dev_release;
615
616         ret = of_overlay_notifier_register(&fpga_region_of_nb);
617         if (ret)
618                 goto err_class;
619
620         ret = platform_driver_register(&fpga_region_driver);
621         if (ret)
622                 goto err_plat;
623
624         return 0;
625
626 err_plat:
627         of_overlay_notifier_unregister(&fpga_region_of_nb);
628 err_class:
629         class_destroy(fpga_region_class);
630         ida_destroy(&fpga_region_ida);
631         return ret;
632 }
633
634 static void __exit fpga_region_exit(void)
635 {
636         platform_driver_unregister(&fpga_region_driver);
637         of_overlay_notifier_unregister(&fpga_region_of_nb);
638         class_destroy(fpga_region_class);
639         ida_destroy(&fpga_region_ida);
640 }
641
642 subsys_initcall(fpga_region_init);
643 module_exit(fpga_region_exit);
644
645 MODULE_DESCRIPTION("FPGA Region");
646 MODULE_AUTHOR("Alan Tull <[email protected]>");
647 MODULE_LICENSE("GPL v2");
This page took 0.071417 seconds and 4 git commands to generate.