of-fpga-region.c 12 KB

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