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