fpga-region.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. #include <linux/fpga/fpga-bridge.h>
  19. #include <linux/fpga/fpga-mgr.h>
  20. #include <linux/fpga/fpga-region.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. static DEFINE_IDA(fpga_region_ida);
  29. static struct class *fpga_region_class;
  30. static const struct of_device_id fpga_region_of_match[] = {
  31. { .compatible = "fpga-region", },
  32. {},
  33. };
  34. MODULE_DEVICE_TABLE(of, fpga_region_of_match);
  35. static int fpga_region_of_node_match(struct device *dev, const void *data)
  36. {
  37. return dev->of_node == data;
  38. }
  39. /**
  40. * of_fpga_region_find - find FPGA region
  41. * @np: device node of FPGA Region
  42. *
  43. * Caller will need to put_device(&region->dev) when done.
  44. *
  45. * Returns FPGA Region struct or NULL
  46. */
  47. static struct fpga_region *of_fpga_region_find(struct device_node *np)
  48. {
  49. struct device *dev;
  50. dev = class_find_device(fpga_region_class, NULL, np,
  51. fpga_region_of_node_match);
  52. if (!dev)
  53. return NULL;
  54. return to_fpga_region(dev);
  55. }
  56. /**
  57. * fpga_region_get - get an exclusive reference to a fpga region
  58. * @region: FPGA Region struct
  59. *
  60. * Caller should call fpga_region_put() when done with region.
  61. *
  62. * Return fpga_region struct if successful.
  63. * Return -EBUSY if someone already has a reference to the region.
  64. * Return -ENODEV if @np is not a FPGA Region.
  65. */
  66. static struct fpga_region *fpga_region_get(struct fpga_region *region)
  67. {
  68. struct device *dev = &region->dev;
  69. if (!mutex_trylock(&region->mutex)) {
  70. dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
  71. return ERR_PTR(-EBUSY);
  72. }
  73. get_device(dev);
  74. if (!try_module_get(dev->parent->driver->owner)) {
  75. put_device(dev);
  76. mutex_unlock(&region->mutex);
  77. return ERR_PTR(-ENODEV);
  78. }
  79. dev_dbg(dev, "get\n");
  80. return region;
  81. }
  82. /**
  83. * fpga_region_put - release a reference to a region
  84. *
  85. * @region: FPGA region
  86. */
  87. static void fpga_region_put(struct fpga_region *region)
  88. {
  89. struct device *dev = &region->dev;
  90. dev_dbg(dev, "put\n");
  91. module_put(dev->parent->driver->owner);
  92. put_device(dev);
  93. mutex_unlock(&region->mutex);
  94. }
  95. /**
  96. * of_fpga_region_get_mgr - get reference for FPGA manager
  97. * @np: device node of FPGA region
  98. *
  99. * Get FPGA Manager from "fpga-mgr" property or from ancestor region.
  100. *
  101. * Caller should call fpga_mgr_put() when done with manager.
  102. *
  103. * Return: fpga manager struct or IS_ERR() condition containing error code.
  104. */
  105. static struct fpga_manager *of_fpga_region_get_mgr(struct device_node *np)
  106. {
  107. struct device_node *mgr_node;
  108. struct fpga_manager *mgr;
  109. of_node_get(np);
  110. while (np) {
  111. if (of_device_is_compatible(np, "fpga-region")) {
  112. mgr_node = of_parse_phandle(np, "fpga-mgr", 0);
  113. if (mgr_node) {
  114. mgr = of_fpga_mgr_get(mgr_node);
  115. of_node_put(np);
  116. return mgr;
  117. }
  118. }
  119. np = of_get_next_parent(np);
  120. }
  121. of_node_put(np);
  122. return ERR_PTR(-EINVAL);
  123. }
  124. /**
  125. * of_fpga_region_get_bridges - create a list of bridges
  126. * @region: FPGA region
  127. * @info: FPGA image info
  128. *
  129. * Create a list of bridges including the parent bridge and the bridges
  130. * specified by "fpga-bridges" property. Note that the
  131. * fpga_bridges_enable/disable/put functions are all fine with an empty list
  132. * if that happens.
  133. *
  134. * Caller should call fpga_bridges_put(&region->bridge_list) when
  135. * done with the bridges.
  136. *
  137. * Return 0 for success (even if there are no bridges specified)
  138. * or -EBUSY if any of the bridges are in use.
  139. */
  140. static int of_fpga_region_get_bridges(struct fpga_region *region,
  141. struct fpga_image_info *info)
  142. {
  143. struct device *dev = &region->dev;
  144. struct device_node *region_np = dev->of_node;
  145. struct device_node *br, *np, *parent_br = NULL;
  146. int i, ret;
  147. /* If parent is a bridge, add to list */
  148. ret = of_fpga_bridge_get_to_list(region_np->parent, info,
  149. &region->bridge_list);
  150. /* -EBUSY means parent is a bridge that is under use. Give up. */
  151. if (ret == -EBUSY)
  152. return ret;
  153. /* Zero return code means parent was a bridge and was added to list. */
  154. if (!ret)
  155. parent_br = region_np->parent;
  156. /* If overlay has a list of bridges, use it. */
  157. if (of_parse_phandle(info->overlay, "fpga-bridges", 0))
  158. np = info->overlay;
  159. else
  160. np = region_np;
  161. for (i = 0; ; i++) {
  162. br = of_parse_phandle(np, "fpga-bridges", i);
  163. if (!br)
  164. break;
  165. /* If parent bridge is in list, skip it. */
  166. if (br == parent_br)
  167. continue;
  168. /* If node is a bridge, get it and add to list */
  169. ret = of_fpga_bridge_get_to_list(br, region->info,
  170. &region->bridge_list);
  171. /* If any of the bridges are in use, give up */
  172. if (ret == -EBUSY) {
  173. fpga_bridges_put(&region->bridge_list);
  174. return -EBUSY;
  175. }
  176. }
  177. return 0;
  178. }
  179. /**
  180. * fpga_region_program_fpga - program FPGA
  181. * @region: FPGA region
  182. * Program an FPGA using fpga image info (region->info).
  183. * Return 0 for success or negative error code.
  184. */
  185. int fpga_region_program_fpga(struct fpga_region *region)
  186. {
  187. struct device *dev = &region->dev;
  188. struct fpga_image_info *info = region->info;
  189. int ret;
  190. region = fpga_region_get(region);
  191. if (IS_ERR(region)) {
  192. dev_err(dev, "failed to get FPGA region\n");
  193. return PTR_ERR(region);
  194. }
  195. ret = fpga_mgr_lock(region->mgr);
  196. if (ret) {
  197. dev_err(dev, "FPGA manager is busy\n");
  198. goto err_put_region;
  199. }
  200. ret = of_fpga_region_get_bridges(region, info);
  201. if (ret) {
  202. dev_err(dev, "failed to get FPGA bridges\n");
  203. goto err_unlock_mgr;
  204. }
  205. ret = fpga_bridges_disable(&region->bridge_list);
  206. if (ret) {
  207. dev_err(dev, "failed to disable bridges\n");
  208. goto err_put_br;
  209. }
  210. ret = fpga_mgr_load(region->mgr, info);
  211. if (ret) {
  212. dev_err(dev, "failed to load FPGA image\n");
  213. goto err_put_br;
  214. }
  215. ret = fpga_bridges_enable(&region->bridge_list);
  216. if (ret) {
  217. dev_err(dev, "failed to enable region bridges\n");
  218. goto err_put_br;
  219. }
  220. fpga_mgr_unlock(region->mgr);
  221. fpga_region_put(region);
  222. return 0;
  223. err_put_br:
  224. fpga_bridges_put(&region->bridge_list);
  225. err_unlock_mgr:
  226. fpga_mgr_unlock(region->mgr);
  227. err_put_region:
  228. fpga_region_put(region);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
  232. /**
  233. * child_regions_with_firmware
  234. * @overlay: device node of the overlay
  235. *
  236. * If the overlay adds child FPGA regions, they are not allowed to have
  237. * firmware-name property.
  238. *
  239. * Return 0 for OK or -EINVAL if child FPGA region adds firmware-name.
  240. */
  241. static int child_regions_with_firmware(struct device_node *overlay)
  242. {
  243. struct device_node *child_region;
  244. const char *child_firmware_name;
  245. int ret = 0;
  246. of_node_get(overlay);
  247. child_region = of_find_matching_node(overlay, fpga_region_of_match);
  248. while (child_region) {
  249. if (!of_property_read_string(child_region, "firmware-name",
  250. &child_firmware_name)) {
  251. ret = -EINVAL;
  252. break;
  253. }
  254. child_region = of_find_matching_node(child_region,
  255. fpga_region_of_match);
  256. }
  257. of_node_put(child_region);
  258. if (ret)
  259. pr_err("firmware-name not allowed in child FPGA region: %pOF",
  260. child_region);
  261. return ret;
  262. }
  263. /**
  264. * of_fpga_region_parse_ov - parse and check overlay applied to region
  265. *
  266. * @region: FPGA region
  267. * @overlay: overlay applied to the FPGA region
  268. *
  269. * Given an overlay applied to a FPGA region, parse the FPGA image specific
  270. * info in the overlay and do some checking.
  271. *
  272. * Returns:
  273. * NULL if overlay doesn't direct us to program the FPGA.
  274. * fpga_image_info struct if there is an image to program.
  275. * error code for invalid overlay.
  276. */
  277. static struct fpga_image_info *of_fpga_region_parse_ov(
  278. struct fpga_region *region,
  279. struct device_node *overlay)
  280. {
  281. struct device *dev = &region->dev;
  282. struct fpga_image_info *info;
  283. const char *firmware_name;
  284. int ret;
  285. if (region->info) {
  286. dev_err(dev, "Region already has overlay applied.\n");
  287. return ERR_PTR(-EINVAL);
  288. }
  289. /*
  290. * Reject overlay if child FPGA Regions added in the overlay have
  291. * firmware-name property (would mean that an FPGA region that has
  292. * not been added to the live tree yet is doing FPGA programming).
  293. */
  294. ret = child_regions_with_firmware(overlay);
  295. if (ret)
  296. return ERR_PTR(ret);
  297. info = fpga_image_info_alloc(dev);
  298. if (!info)
  299. return ERR_PTR(-ENOMEM);
  300. info->overlay = overlay;
  301. /* Read FPGA region properties from the overlay */
  302. if (of_property_read_bool(overlay, "partial-fpga-config"))
  303. info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
  304. if (of_property_read_bool(overlay, "external-fpga-config"))
  305. info->flags |= FPGA_MGR_EXTERNAL_CONFIG;
  306. if (of_property_read_bool(overlay, "encrypted-fpga-config"))
  307. info->flags |= FPGA_MGR_ENCRYPTED_BITSTREAM;
  308. if (!of_property_read_string(overlay, "firmware-name",
  309. &firmware_name)) {
  310. info->firmware_name = devm_kstrdup(dev, firmware_name,
  311. GFP_KERNEL);
  312. if (!info->firmware_name)
  313. return ERR_PTR(-ENOMEM);
  314. }
  315. of_property_read_u32(overlay, "region-unfreeze-timeout-us",
  316. &info->enable_timeout_us);
  317. of_property_read_u32(overlay, "region-freeze-timeout-us",
  318. &info->disable_timeout_us);
  319. of_property_read_u32(overlay, "config-complete-timeout-us",
  320. &info->config_complete_timeout_us);
  321. /* If overlay is not programming the FPGA, don't need FPGA image info */
  322. if (!info->firmware_name) {
  323. ret = 0;
  324. goto ret_no_info;
  325. }
  326. /*
  327. * If overlay informs us FPGA was externally programmed, specifying
  328. * firmware here would be ambiguous.
  329. */
  330. if (info->flags & FPGA_MGR_EXTERNAL_CONFIG) {
  331. dev_err(dev, "error: specified firmware and external-fpga-config");
  332. ret = -EINVAL;
  333. goto ret_no_info;
  334. }
  335. return info;
  336. ret_no_info:
  337. fpga_image_info_free(info);
  338. return ERR_PTR(ret);
  339. }
  340. /**
  341. * of_fpga_region_notify_pre_apply - pre-apply overlay notification
  342. *
  343. * @region: FPGA region that the overlay was applied to
  344. * @nd: overlay notification data
  345. *
  346. * Called when an overlay targeted to a FPGA Region is about to be applied.
  347. * Parses the overlay for properties that influence how the FPGA will be
  348. * programmed and does some checking. If the checks pass, programs the FPGA.
  349. * If the checks fail, overlay is rejected and does not get added to the
  350. * live tree.
  351. *
  352. * Returns 0 for success or negative error code for failure.
  353. */
  354. static int of_fpga_region_notify_pre_apply(struct fpga_region *region,
  355. struct of_overlay_notify_data *nd)
  356. {
  357. struct device *dev = &region->dev;
  358. struct fpga_image_info *info;
  359. int ret;
  360. if (region->info) {
  361. dev_err(dev, "Region already has overlay applied.\n");
  362. return -EINVAL;
  363. }
  364. info = of_fpga_region_parse_ov(region, nd->overlay);
  365. if (IS_ERR(info))
  366. return PTR_ERR(info);
  367. if (!info)
  368. return 0;
  369. region->info = info;
  370. ret = fpga_region_program_fpga(region);
  371. if (ret) {
  372. /* error; reject overlay */
  373. fpga_image_info_free(info);
  374. region->info = NULL;
  375. }
  376. return ret;
  377. }
  378. /**
  379. * of_fpga_region_notify_post_remove - post-remove overlay notification
  380. *
  381. * @region: FPGA region that was targeted by the overlay that was removed
  382. * @nd: overlay notification data
  383. *
  384. * Called after an overlay has been removed if the overlay's target was a
  385. * FPGA region.
  386. */
  387. static void of_fpga_region_notify_post_remove(struct fpga_region *region,
  388. struct of_overlay_notify_data *nd)
  389. {
  390. fpga_bridges_disable(&region->bridge_list);
  391. fpga_bridges_put(&region->bridge_list);
  392. fpga_image_info_free(region->info);
  393. region->info = NULL;
  394. }
  395. /**
  396. * of_fpga_region_notify - reconfig notifier for dynamic DT changes
  397. * @nb: notifier block
  398. * @action: notifier action
  399. * @arg: reconfig data
  400. *
  401. * This notifier handles programming a FPGA when a "firmware-name" property is
  402. * added to a fpga-region.
  403. *
  404. * Returns NOTIFY_OK or error if FPGA programming fails.
  405. */
  406. static int of_fpga_region_notify(struct notifier_block *nb,
  407. unsigned long action, void *arg)
  408. {
  409. struct of_overlay_notify_data *nd = arg;
  410. struct fpga_region *region;
  411. int ret;
  412. switch (action) {
  413. case OF_OVERLAY_PRE_APPLY:
  414. pr_debug("%s OF_OVERLAY_PRE_APPLY\n", __func__);
  415. break;
  416. case OF_OVERLAY_POST_APPLY:
  417. pr_debug("%s OF_OVERLAY_POST_APPLY\n", __func__);
  418. return NOTIFY_OK; /* not for us */
  419. case OF_OVERLAY_PRE_REMOVE:
  420. pr_debug("%s OF_OVERLAY_PRE_REMOVE\n", __func__);
  421. return NOTIFY_OK; /* not for us */
  422. case OF_OVERLAY_POST_REMOVE:
  423. pr_debug("%s OF_OVERLAY_POST_REMOVE\n", __func__);
  424. break;
  425. default: /* should not happen */
  426. return NOTIFY_OK;
  427. }
  428. region = of_fpga_region_find(nd->target);
  429. if (!region)
  430. return NOTIFY_OK;
  431. ret = 0;
  432. switch (action) {
  433. case OF_OVERLAY_PRE_APPLY:
  434. ret = of_fpga_region_notify_pre_apply(region, nd);
  435. break;
  436. case OF_OVERLAY_POST_REMOVE:
  437. of_fpga_region_notify_post_remove(region, nd);
  438. break;
  439. }
  440. put_device(&region->dev);
  441. if (ret)
  442. return notifier_from_errno(ret);
  443. return NOTIFY_OK;
  444. }
  445. static struct notifier_block fpga_region_of_nb = {
  446. .notifier_call = of_fpga_region_notify,
  447. };
  448. static int of_fpga_region_probe(struct platform_device *pdev)
  449. {
  450. struct device *dev = &pdev->dev;
  451. struct device_node *np = dev->of_node;
  452. struct fpga_region *region;
  453. struct fpga_manager *mgr;
  454. int id, ret = 0;
  455. mgr = of_fpga_region_get_mgr(np);
  456. if (IS_ERR(mgr))
  457. return -EPROBE_DEFER;
  458. region = kzalloc(sizeof(*region), GFP_KERNEL);
  459. if (!region) {
  460. ret = -ENOMEM;
  461. goto err_put_mgr;
  462. }
  463. region->mgr = mgr;
  464. id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
  465. if (id < 0) {
  466. ret = id;
  467. goto err_kfree;
  468. }
  469. mutex_init(&region->mutex);
  470. INIT_LIST_HEAD(&region->bridge_list);
  471. device_initialize(&region->dev);
  472. region->dev.class = fpga_region_class;
  473. region->dev.parent = dev;
  474. region->dev.of_node = np;
  475. region->dev.id = id;
  476. dev_set_drvdata(dev, region);
  477. ret = dev_set_name(&region->dev, "region%d", id);
  478. if (ret)
  479. goto err_remove;
  480. ret = device_add(&region->dev);
  481. if (ret)
  482. goto err_remove;
  483. of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
  484. dev_info(dev, "FPGA Region probed\n");
  485. return 0;
  486. err_remove:
  487. ida_simple_remove(&fpga_region_ida, id);
  488. err_kfree:
  489. kfree(region);
  490. err_put_mgr:
  491. fpga_mgr_put(mgr);
  492. return ret;
  493. }
  494. static int of_fpga_region_remove(struct platform_device *pdev)
  495. {
  496. struct fpga_region *region = platform_get_drvdata(pdev);
  497. device_unregister(&region->dev);
  498. fpga_mgr_put(region->mgr);
  499. return 0;
  500. }
  501. static struct platform_driver of_fpga_region_driver = {
  502. .probe = of_fpga_region_probe,
  503. .remove = of_fpga_region_remove,
  504. .driver = {
  505. .name = "fpga-region",
  506. .of_match_table = of_match_ptr(fpga_region_of_match),
  507. },
  508. };
  509. static void fpga_region_dev_release(struct device *dev)
  510. {
  511. struct fpga_region *region = to_fpga_region(dev);
  512. ida_simple_remove(&fpga_region_ida, region->dev.id);
  513. kfree(region);
  514. }
  515. /**
  516. * fpga_region_init - init function for fpga_region class
  517. * Creates the fpga_region class and registers a reconfig notifier.
  518. */
  519. static int __init fpga_region_init(void)
  520. {
  521. int ret;
  522. fpga_region_class = class_create(THIS_MODULE, "fpga_region");
  523. if (IS_ERR(fpga_region_class))
  524. return PTR_ERR(fpga_region_class);
  525. fpga_region_class->dev_release = fpga_region_dev_release;
  526. ret = of_overlay_notifier_register(&fpga_region_of_nb);
  527. if (ret)
  528. goto err_class;
  529. ret = platform_driver_register(&of_fpga_region_driver);
  530. if (ret)
  531. goto err_plat;
  532. return 0;
  533. err_plat:
  534. of_overlay_notifier_unregister(&fpga_region_of_nb);
  535. err_class:
  536. class_destroy(fpga_region_class);
  537. ida_destroy(&fpga_region_ida);
  538. return ret;
  539. }
  540. static void __exit fpga_region_exit(void)
  541. {
  542. platform_driver_unregister(&of_fpga_region_driver);
  543. of_overlay_notifier_unregister(&fpga_region_of_nb);
  544. class_destroy(fpga_region_class);
  545. ida_destroy(&fpga_region_ida);
  546. }
  547. subsys_initcall(fpga_region_init);
  548. module_exit(fpga_region_exit);
  549. MODULE_DESCRIPTION("FPGA Region");
  550. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  551. MODULE_LICENSE("GPL v2");