of-fpga-region.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * FPGA Region - Device Tree support for FPGA programming under Linux
  3. *
  4. * Copyright (C) 2013-2016 Altera Corporation
  5. * Copyright (C) 2017 Intel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/fpga/fpga-bridge.h>
  20. #include <linux/fpga/fpga-mgr.h>
  21. #include <linux/fpga/fpga-region.h>
  22. #include <linux/idr.h>
  23. #include <linux/kernel.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. static const struct of_device_id fpga_region_of_match[] = {
  30. { .compatible = "fpga-region", },
  31. {},
  32. };
  33. MODULE_DEVICE_TABLE(of, fpga_region_of_match);
  34. static int fpga_region_of_node_match(struct device *dev, const void *data)
  35. {
  36. return dev->of_node == data;
  37. }
  38. /**
  39. * of_fpga_region_find - find FPGA region
  40. * @np: device node of FPGA Region
  41. *
  42. * Caller will need to put_device(&region->dev) when done.
  43. *
  44. * Returns FPGA Region struct or NULL
  45. */
  46. static struct fpga_region *of_fpga_region_find(struct device_node *np)
  47. {
  48. return fpga_region_class_find(NULL, np, fpga_region_of_node_match);
  49. }
  50. /**
  51. * of_fpga_region_get_mgr - get reference for FPGA manager
  52. * @np: device node of FPGA region
  53. *
  54. * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
  55. *
  56. * Caller should call fpga_mgr_put() when done with manager.
  57. *
  58. * Return: fpga manager struct or IS_ERR() condition containing error code.
  59. */
  60. static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
  61. {
  62. struct device_node *mgr_node;
  63. struct fpga_manager *mgr;
  64. of_node_get(np);
  65. while (np) {
  66. if (of_device_is_compatible(np, "fpga-region")) {
  67. mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
  68. if (mgr_node) {
  69. mgr = of_fpga_mgr_get(mgr_node);
  70. of_node_put(mgr_node);
  71. of_node_put(np);
  72. return mgr;
  73. }
  74. }
  75. np = of_get_next_parent(np);
  76. }
  77. of_node_put(np);
  78. return ERR_PTR(-EINVAL);
  79. }
  80. /**
  81. * of_fpga_region_get_bridges - create a list of bridges
  82. * @region: FPGA region
  83. *
  84. * Create a list of bridges including the parent bridge and the bridges
  85. * specified by "fpga-bridges" property. Note that the
  86. * fpga_bridges_enable/disable/put functions are all fine with an empty list
  87. * if that happens.
  88. *
  89. * Caller should call fpga_bridges_put(&region->bridge_list) when
  90. * done with the bridges.
  91. *
  92. * Return 0 for success (even if there are no bridges specified)
  93. * or -EBUSY if any of the bridges are in use.
  94. */
  95. static int of_fpga_region_get_bridges(struct fpga_region *region)
  96. {
  97. struct device *dev = &region->dev;
  98. struct device_node *region_np = dev->of_node;
  99. struct fpga_image_info *info = region->info;
  100. struct device_node *br, *np, *parent_br = NULL;
  101. int i, ret;
  102. /* If parent is a bridge, add to list */
  103. ret = of_fpga_bridge_get_to_list(region_np->parent, info,
  104. &region->bridge_list);
  105. /* -EBUSY means parent is a bridge that is under use. Give up. */
  106. if (ret == -EBUSY)
  107. return ret;
  108. /* Zero return code means parent was a bridge and was added to list. */
  109. if (!ret)
  110. parent_br = region_np->parent;
  111. /* If overlay has a list of bridges, use it. */
  112. br = of_parse_phandle(info->overlay, "fpga-bridges", 0);
  113. if (br) {
  114. of_node_put(br);
  115. np = info->overlay;
  116. } else {
  117. np = region_np;
  118. }
  119. for (i = 0; ; i++) {
  120. br = of_parse_phandle(np, "fpga-bridges", i);
  121. if (!br)
  122. break;
  123. /* If parent bridge is in list, skip it. */
  124. if (br == parent_br) {
  125. of_node_put(br);
  126. continue;
  127. }
  128. /* If node is a bridge, get it and add to list */
  129. ret = of_fpga_bridge_get_to_list(br, info,
  130. &region->bridge_list);
  131. of_node_put(br);
  132. /* If any of the bridges are in use, give up */
  133. if (ret == -EBUSY) {
  134. fpga_bridges_put(&region->bridge_list);
  135. return -EBUSY;
  136. }
  137. }
  138. return 0;
  139. }
  140. /**
  141. * child_regions_with_firmware
  142. * @overlay: device node of the overlay
  143. *
  144. * If the overlay adds child FPGA regions, they are not allowed to have
  145. * firmware-name property.
  146. *
  147. * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
  148. */
  149. static int child_regions_with_firmware(struct device_node *overlay)
  150. {
  151. struct device_node *child_region;
  152. const char *child_firmware_name;
  153. int ret = 0;
  154. of_node_get(overlay);
  155. child_region = of_find_matching_node(overlay, fpga_region_of_match);
  156. while (child_region) {
  157. if (!of_property_read_string(child_region, "firmware-name",
  158. &child_firmware_name)) {
  159. ret = -EINVAL;
  160. break;
  161. }
  162. child_region = of_find_matching_node(child_region,
  163. fpga_region_of_match);
  164. }
  165. of_node_put(child_region);
  166. if (ret)
  167. pr_err("firmware-name not allowed in child FPGA region: %pOF",
  168. child_region);
  169. return ret;
  170. }
  171. /**
  172. * of_fpga_region_parse_ov - parse and check overlay applied to region
  173. *
  174. * @region: FPGA region
  175. * @overlay: overlay applied to the FPGA region
  176. *
  177. * Given an overlay applied to a FPGA region, parse the FPGA image specific
  178. * info in the overlay and do some checking.
  179. *
  180. * Returns:
  181. * NULL if overlay doesn't direct us to program the FPGA.
  182. * fpga_image_info struct if there is an image to program.
  183. * error code for invalid overlay.
  184. */
  185. static struct fpga_image_info *of_fpga_region_parse_ov(
  186. struct fpga_region *region,
  187. struct device_node *overlay)
  188. {
  189. struct device *dev = &region->dev;
  190. struct fpga_image_info *info;
  191. const char *firmware_name;
  192. int ret;
  193. if (region->info) {
  194. dev_err(dev, "Region already has overlay applied.\n");
  195. return ERR_PTR(-EINVAL);
  196. }
  197. /*
  198. * Reject overlay if child FPGA Regions added in the overlay have
  199. * firmware-name property (would mean that an FPGA region that has
  200. * not been added to the live tree yet is doing FPGA programming).
  201. */
  202. ret = child_regions_with_firmware(overlay);
  203. if (ret)
  204. return ERR_PTR(ret);
  205. info = fpga_image_info_alloc(dev);
  206. if (!info)
  207. return ERR_PTR(-ENOMEM);
  208. info->overlay = overlay;
  209. /* Read FPGA region properties from the overlay */
  210. if (of_property_read_bool(overlay, "partial-fpga-config"))
  211. info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
  212. if (of_property_read_bool(overlay, "external-fpga-config"))
  213. info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
  214. if (of_property_read_bool(overlay, "encrypted-fpga-config"))
  215. info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
  216. if (!of_property_read_string(overlay, "firmware-name",
  217. &firmware_name)) {
  218. info->firmware_name = devm_kstrdup(dev, firmware_name,
  219. GFP_KERNEL);
  220. if (!info->firmware_name)
  221. return ERR_PTR(-ENOMEM);
  222. }
  223. of_property_read_u32(overlay, "region-unfreeze-timeout-us",
  224. &info->enable_timeout_us);
  225. of_property_read_u32(overlay, "region-freeze-timeout-us",
  226. &info->disable_timeout_us);
  227. of_property_read_u32(overlay, "config-complete-timeout-us",
  228. &info->config_complete_timeout_us);
  229. /* If overlay is not programming the FPGA, don't need FPGA image info */
  230. if (!info->firmware_name) {
  231. ret = 0;
  232. goto ret_no_info;
  233. }
  234. /*
  235. * If overlay informs us FPGA was externally programmed, specifying
  236. * firmware here would be ambiguous.
  237. */
  238. if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
  239. dev_err(dev, "error: specified firmware and external-fpga-config");
  240. ret = -EINVAL;
  241. goto ret_no_info;
  242. }
  243. return info;
  244. ret_no_info:
  245. fpga_image_info_free(info);
  246. return ERR_PTR(ret);
  247. }
  248. /**
  249. * of_fpga_region_notify_pre_apply - pre-apply overlay notification
  250. *
  251. * @region: FPGA region that the overlay was applied to
  252. * @nd: overlay notification data
  253. *
  254. * Called when an overlay targeted to a FPGA Region is about to be applied.
  255. * Parses the overlay for properties that influence how the FPGA will be
  256. * programmed and does some checking. If the checks pass, programs the FPGA.
  257. * If the checks fail, overlay is rejected and does not get added to the
  258. * live tree.
  259. *
  260. * Returns 0 for success or negative error code for failure.
  261. */
  262. static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
  263. struct of_overlay_notify_data *nd)
  264. {
  265. struct device *dev = &region->dev;
  266. struct fpga_image_info *info;
  267. int ret;
  268. info = of_fpga_region_parse_ov(region, nd->overlay);
  269. if (IS_ERR(info))
  270. return PTR_ERR(info);
  271. /* If overlay doesn't program the FPGA, accept it anyway. */
  272. if (!info)
  273. return 0;
  274. if (region->info) {
  275. dev_err(dev, "Region already has overlay applied.\n");
  276. return -EINVAL;
  277. }
  278. region->info = info;
  279. ret = fpga_region_program_fpga(region);
  280. if (ret) {
  281. /* error; reject overlay */
  282. fpga_image_info_free(info);
  283. region->info = NULL;
  284. }
  285. return ret;
  286. }
  287. /**
  288. * of_fpga_region_notify_post_remove - post-remove overlay notification
  289. *
  290. * @region: FPGA region that was targeted by the overlay that was removed
  291. * @nd: overlay notification data
  292. *
  293. * Called after an overlay has been removed if the overlay's target was a
  294. * FPGA region.
  295. */
  296. static void of_fpga_region_notify_post_remove(struct fpga_region *region,
  297. struct of_overlay_notify_data *nd)
  298. {
  299. fpga_bridges_disable(&region->bridge_list);
  300. fpga_bridges_put(&region->bridge_list);
  301. fpga_image_info_free(region->info);
  302. region->info = NULL;
  303. }
  304. /**
  305. * of_fpga_region_notify - reconfig notifier for dynamic DT changes
  306. * @nb: notifier block
  307. * @action: notifier action
  308. * @arg: reconfig data
  309. *
  310. * This notifier handles programming a FPGA when a "firmware-name" property is
  311. * added to a fpga-region.
  312. *
  313. * Returns NOTIFY_OK or error if FPGA programming fails.
  314. */
  315. static int of_fpga_region_notify(struct notifier_block *nb,
  316. unsigned long action, void *arg)
  317. {
  318. struct of_overlay_notify_data *nd = arg;
  319. struct fpga_region *region;
  320. int ret;
  321. switch (action) {
  322. case OF_OVERLAY_PRE_APPLY:
  323. pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
  324. break;
  325. case OF_OVERLAY_POST_APPLY:
  326. pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
  327. return NOTIFY_OK; /* not for us */
  328. case OF_OVERLAY_PRE_REMOVE:
  329. pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
  330. return NOTIFY_OK; /* not for us */
  331. case OF_OVERLAY_POST_REMOVE:
  332. pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
  333. break;
  334. default: /* should not happen */
  335. return NOTIFY_OK;
  336. }
  337. region = of_fpga_region_find(nd->target);
  338. if (!region)
  339. return NOTIFY_OK;
  340. ret = 0;
  341. switch (action) {
  342. case OF_OVERLAY_PRE_APPLY:
  343. ret = of_fpga_region_notify_pre_apply(region, nd);
  344. break;
  345. case OF_OVERLAY_POST_REMOVE:
  346. of_fpga_region_notify_post_remove(region, nd);
  347. break;
  348. }
  349. put_device(&region->dev);
  350. if (ret)
  351. return notifier_from_errno(ret);
  352. return NOTIFY_OK;
  353. }
  354. static struct notifier_block fpga_region_of_nb = {
  355. .notifier_call = of_fpga_region_notify,
  356. };
  357. static int of_fpga_region_probe(struct platform_device *pdev)
  358. {
  359. struct device *dev = &pdev->dev;
  360. struct device_node *np = dev->of_node;
  361. struct fpga_region *region;
  362. struct fpga_manager *mgr;
  363. int ret;
  364. /* Find the FPGA mgr specified by region or parent region. */
  365. mgr = of_fpga_region_get_mgr(np);
  366. if (IS_ERR(mgr))
  367. return -EPROBE_DEFER;
  368. region = fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
  369. if (!region) {
  370. ret = -ENOMEM;
  371. goto eprobe_mgr_put;
  372. }
  373. ret = fpga_region_register(region);
  374. if (ret)
  375. goto eprobe_free;
  376. of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
  377. dev_set_drvdata(dev, region);
  378. dev_info(dev, "FPGA Region probed\n");
  379. return 0;
  380. eprobe_free:
  381. fpga_region_free(region);
  382. eprobe_mgr_put:
  383. fpga_mgr_put(mgr);
  384. return ret;
  385. }
  386. static int of_fpga_region_remove(struct platform_device *pdev)
  387. {
  388. struct fpga_region *region = platform_get_drvdata(pdev);
  389. fpga_region_unregister(region);
  390. fpga_mgr_put(region->mgr);
  391. return 0;
  392. }
  393. static struct platform_driver of_fpga_region_driver = {
  394. .probe = of_fpga_region_probe,
  395. .remove = of_fpga_region_remove,
  396. .driver = {
  397. .name = "of-fpga-region",
  398. .of_match_table = of_match_ptr(fpga_region_of_match),
  399. },
  400. };
  401. /**
  402. * fpga_region_init - init function for fpga_region class
  403. * Creates the fpga_region class and registers a reconfig notifier.
  404. */
  405. static int __init of_fpga_region_init(void)
  406. {
  407. int ret;
  408. ret = of_overlay_notifier_register(&fpga_region_of_nb);
  409. if (ret)
  410. return ret;
  411. ret = platform_driver_register(&of_fpga_region_driver);
  412. if (ret)
  413. goto err_plat;
  414. return 0;
  415. err_plat:
  416. of_overlay_notifier_unregister(&fpga_region_of_nb);
  417. return ret;
  418. }
  419. static void __exit of_fpga_region_exit(void)
  420. {
  421. platform_driver_unregister(&of_fpga_region_driver);
  422. of_overlay_notifier_unregister(&fpga_region_of_nb);
  423. }
  424. subsys_initcall(of_fpga_region_init);
  425. module_exit(of_fpga_region_exit);
  426. MODULE_DESCRIPTION("FPGA Region");
  427. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  428. MODULE_LICENSE("GPL v2");