fpga-region.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/slab.h>
  16. #include <linux/spinlock.h>
  17. static DEFINE_IDA(fpga_region_ida);
  18. static struct class *fpga_region_class;
  19. struct fpga_region *fpga_region_class_find(
  20. struct device *start, const void *data,
  21. int (*match)(struct device *, const void *))
  22. {
  23. struct device *dev;
  24. dev = class_find_device(fpga_region_class, start, data, match);
  25. if (!dev)
  26. return NULL;
  27. return to_fpga_region(dev);
  28. }
  29. EXPORT_SYMBOL_GPL(fpga_region_class_find);
  30. /**
  31. * fpga_region_get - get an exclusive reference to a fpga region
  32. * @region: FPGA Region struct
  33. *
  34. * Caller should call fpga_region_put() when done with region.
  35. *
  36. * Return fpga_region struct if successful.
  37. * Return -EBUSY if someone already has a reference to the region.
  38. * Return -ENODEV if @np is not a FPGA Region.
  39. */
  40. static struct fpga_region *fpga_region_get(struct fpga_region *region)
  41. {
  42. struct device *dev = &region->dev;
  43. if (!mutex_trylock(&region->mutex)) {
  44. dev_dbg(dev, "%s: FPGA Region already in use\n", __func__);
  45. return ERR_PTR(-EBUSY);
  46. }
  47. get_device(dev);
  48. if (!try_module_get(dev->parent->driver->owner)) {
  49. put_device(dev);
  50. mutex_unlock(&region->mutex);
  51. return ERR_PTR(-ENODEV);
  52. }
  53. dev_dbg(dev, "get\n");
  54. return region;
  55. }
  56. /**
  57. * fpga_region_put - release a reference to a region
  58. *
  59. * @region: FPGA region
  60. */
  61. static void fpga_region_put(struct fpga_region *region)
  62. {
  63. struct device *dev = &region->dev;
  64. dev_dbg(dev, "put\n");
  65. module_put(dev->parent->driver->owner);
  66. put_device(dev);
  67. mutex_unlock(&region->mutex);
  68. }
  69. /**
  70. * fpga_region_program_fpga - program FPGA
  71. *
  72. * @region: FPGA region
  73. *
  74. * Program an FPGA using fpga image info (region->info).
  75. * If the region has a get_bridges function, the exclusive reference for the
  76. * bridges will be held if programming succeeds. This is intended to prevent
  77. * reprogramming the region until the caller considers it safe to do so.
  78. * The caller will need to call fpga_bridges_put() before attempting to
  79. * reprogram the region.
  80. *
  81. * Return 0 for success or negative error code.
  82. */
  83. int fpga_region_program_fpga(struct fpga_region *region)
  84. {
  85. struct device *dev = &region->dev;
  86. struct fpga_image_info *info = region->info;
  87. int ret;
  88. region = fpga_region_get(region);
  89. if (IS_ERR(region)) {
  90. dev_err(dev, "failed to get FPGA region\n");
  91. return PTR_ERR(region);
  92. }
  93. ret = fpga_mgr_lock(region->mgr);
  94. if (ret) {
  95. dev_err(dev, "FPGA manager is busy\n");
  96. goto err_put_region;
  97. }
  98. /*
  99. * In some cases, we already have a list of bridges in the
  100. * fpga region struct. Or we don't have any bridges.
  101. */
  102. if (region->get_bridges) {
  103. ret = region->get_bridges(region);
  104. if (ret) {
  105. dev_err(dev, "failed to get fpga region bridges\n");
  106. goto err_unlock_mgr;
  107. }
  108. }
  109. ret = fpga_bridges_disable(&region->bridge_list);
  110. if (ret) {
  111. dev_err(dev, "failed to disable bridges\n");
  112. goto err_put_br;
  113. }
  114. ret = fpga_mgr_load(region->mgr, info);
  115. if (ret) {
  116. dev_err(dev, "failed to load FPGA image\n");
  117. goto err_put_br;
  118. }
  119. ret = fpga_bridges_enable(&region->bridge_list);
  120. if (ret) {
  121. dev_err(dev, "failed to enable region bridges\n");
  122. goto err_put_br;
  123. }
  124. fpga_mgr_unlock(region->mgr);
  125. fpga_region_put(region);
  126. return 0;
  127. err_put_br:
  128. if (region->get_bridges)
  129. fpga_bridges_put(&region->bridge_list);
  130. err_unlock_mgr:
  131. fpga_mgr_unlock(region->mgr);
  132. err_put_region:
  133. fpga_region_put(region);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
  137. /**
  138. * fpga_region_create - alloc and init a struct fpga_region
  139. * @dev: device parent
  140. * @mgr: manager that programs this region
  141. * @get_bridges: optional function to get bridges to a list
  142. *
  143. * Return: struct fpga_region or NULL
  144. */
  145. struct fpga_region
  146. *fpga_region_create(struct device *dev,
  147. struct fpga_manager *mgr,
  148. int (*get_bridges)(struct fpga_region *))
  149. {
  150. struct fpga_region *region;
  151. int id, ret = 0;
  152. region = kzalloc(sizeof(*region), GFP_KERNEL);
  153. if (!region)
  154. return NULL;
  155. id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
  156. if (id < 0)
  157. goto err_free;
  158. region->mgr = mgr;
  159. region->get_bridges = get_bridges;
  160. mutex_init(&region->mutex);
  161. INIT_LIST_HEAD(&region->bridge_list);
  162. device_initialize(&region->dev);
  163. region->dev.class = fpga_region_class;
  164. region->dev.parent = dev;
  165. region->dev.of_node = dev->of_node;
  166. region->dev.id = id;
  167. ret = dev_set_name(&region->dev, "region%d", id);
  168. if (ret)
  169. goto err_remove;
  170. return region;
  171. err_remove:
  172. ida_simple_remove(&fpga_region_ida, id);
  173. err_free:
  174. kfree(region);
  175. return NULL;
  176. }
  177. EXPORT_SYMBOL_GPL(fpga_region_create);
  178. /**
  179. * fpga_region_free - free a struct fpga_region
  180. * @region: FPGA region created by fpga_region_create
  181. */
  182. void fpga_region_free(struct fpga_region *region)
  183. {
  184. ida_simple_remove(&fpga_region_ida, region->dev.id);
  185. kfree(region);
  186. }
  187. EXPORT_SYMBOL_GPL(fpga_region_free);
  188. /**
  189. * fpga_region_register - register a FPGA region
  190. * @region: FPGA region created by fpga_region_create
  191. * Return: 0 or -errno
  192. */
  193. int fpga_region_register(struct fpga_region *region)
  194. {
  195. return device_add(&region->dev);
  196. }
  197. EXPORT_SYMBOL_GPL(fpga_region_register);
  198. /**
  199. * fpga_region_unregister - unregister and free a FPGA region
  200. * @region: FPGA region
  201. */
  202. void fpga_region_unregister(struct fpga_region *region)
  203. {
  204. device_unregister(&region->dev);
  205. }
  206. EXPORT_SYMBOL_GPL(fpga_region_unregister);
  207. static void fpga_region_dev_release(struct device *dev)
  208. {
  209. struct fpga_region *region = to_fpga_region(dev);
  210. fpga_region_free(region);
  211. }
  212. /**
  213. * fpga_region_init - init function for fpga_region class
  214. * Creates the fpga_region class and registers a reconfig notifier.
  215. */
  216. static int __init fpga_region_init(void)
  217. {
  218. fpga_region_class = class_create(THIS_MODULE, "fpga_region");
  219. if (IS_ERR(fpga_region_class))
  220. return PTR_ERR(fpga_region_class);
  221. fpga_region_class->dev_release = fpga_region_dev_release;
  222. return 0;
  223. }
  224. static void __exit fpga_region_exit(void)
  225. {
  226. class_destroy(fpga_region_class);
  227. ida_destroy(&fpga_region_ida);
  228. }
  229. subsys_initcall(fpga_region_init);
  230. module_exit(fpga_region_exit);
  231. MODULE_DESCRIPTION("FPGA Region");
  232. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  233. MODULE_LICENSE("GPL v2");