fpga-region.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * @region: FPGA region
  72. * Program an FPGA using fpga image info (region->info).
  73. * If the region has a get_bridges function, the exclusive reference for the
  74. * bridges will be held if programming succeeds. This is intended to prevent
  75. * reprogramming the region until the caller considers it safe to do so.
  76. * The caller will need to call fpga_bridges_put() before attempting to
  77. * reprogram the region.
  78. * Return 0 for success or negative error code.
  79. */
  80. int fpga_region_program_fpga(struct fpga_region *region)
  81. {
  82. struct device *dev = &region->dev;
  83. struct fpga_image_info *info = region->info;
  84. int ret;
  85. region = fpga_region_get(region);
  86. if (IS_ERR(region)) {
  87. dev_err(dev, "failed to get FPGA region\n");
  88. return PTR_ERR(region);
  89. }
  90. ret = fpga_mgr_lock(region->mgr);
  91. if (ret) {
  92. dev_err(dev, "FPGA manager is busy\n");
  93. goto err_put_region;
  94. }
  95. /*
  96. * In some cases, we already have a list of bridges in the
  97. * fpga region struct. Or we don't have any bridges.
  98. */
  99. if (region->get_bridges) {
  100. ret = region->get_bridges(region);
  101. if (ret) {
  102. dev_err(dev, "failed to get fpga region bridges\n");
  103. goto err_unlock_mgr;
  104. }
  105. }
  106. ret = fpga_bridges_disable(&region->bridge_list);
  107. if (ret) {
  108. dev_err(dev, "failed to disable bridges\n");
  109. goto err_put_br;
  110. }
  111. ret = fpga_mgr_load(region->mgr, info);
  112. if (ret) {
  113. dev_err(dev, "failed to load FPGA image\n");
  114. goto err_put_br;
  115. }
  116. ret = fpga_bridges_enable(&region->bridge_list);
  117. if (ret) {
  118. dev_err(dev, "failed to enable region bridges\n");
  119. goto err_put_br;
  120. }
  121. fpga_mgr_unlock(region->mgr);
  122. fpga_region_put(region);
  123. return 0;
  124. err_put_br:
  125. if (region->get_bridges)
  126. fpga_bridges_put(&region->bridge_list);
  127. err_unlock_mgr:
  128. fpga_mgr_unlock(region->mgr);
  129. err_put_region:
  130. fpga_region_put(region);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(fpga_region_program_fpga);
  134. /**
  135. * fpga_region_create - alloc and init a struct fpga_region
  136. * @dev: device parent
  137. * @mgr: manager that programs this region
  138. * @get_bridges: optional function to get bridges to a list
  139. *
  140. * Return: struct fpga_region or NULL
  141. */
  142. struct fpga_region
  143. *fpga_region_create(struct device *dev,
  144. struct fpga_manager *mgr,
  145. int (*get_bridges)(struct fpga_region *))
  146. {
  147. struct fpga_region *region;
  148. int id, ret = 0;
  149. region = kzalloc(sizeof(*region), GFP_KERNEL);
  150. if (!region)
  151. return NULL;
  152. id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
  153. if (id < 0)
  154. goto err_free;
  155. region->mgr = mgr;
  156. region->get_bridges = get_bridges;
  157. mutex_init(&region->mutex);
  158. INIT_LIST_HEAD(&region->bridge_list);
  159. device_initialize(&region->dev);
  160. region->dev.class = fpga_region_class;
  161. region->dev.parent = dev;
  162. region->dev.of_node = dev->of_node;
  163. region->dev.id = id;
  164. ret = dev_set_name(&region->dev, "region%d", id);
  165. if (ret)
  166. goto err_remove;
  167. return region;
  168. err_remove:
  169. ida_simple_remove(&fpga_region_ida, id);
  170. err_free:
  171. kfree(region);
  172. return NULL;
  173. }
  174. EXPORT_SYMBOL_GPL(fpga_region_create);
  175. /**
  176. * fpga_region_free - free a struct fpga_region
  177. * @region: FPGA region created by fpga_region_create
  178. */
  179. void fpga_region_free(struct fpga_region *region)
  180. {
  181. ida_simple_remove(&fpga_region_ida, region->dev.id);
  182. kfree(region);
  183. }
  184. EXPORT_SYMBOL_GPL(fpga_region_free);
  185. /*
  186. * fpga_region_register - register a FPGA region
  187. * @region: FPGA region created by fpga_region_create
  188. * Return: 0 or -errno
  189. */
  190. int fpga_region_register(struct fpga_region *region)
  191. {
  192. return device_add(&region->dev);
  193. }
  194. EXPORT_SYMBOL_GPL(fpga_region_register);
  195. /*
  196. * fpga_region_unregister - unregister a FPGA region
  197. * @region: FPGA region
  198. */
  199. void fpga_region_unregister(struct fpga_region *region)
  200. {
  201. device_unregister(&region->dev);
  202. }
  203. EXPORT_SYMBOL_GPL(fpga_region_unregister);
  204. static void fpga_region_dev_release(struct device *dev)
  205. {
  206. struct fpga_region *region = to_fpga_region(dev);
  207. fpga_region_free(region);
  208. }
  209. /**
  210. * fpga_region_init - init function for fpga_region class
  211. * Creates the fpga_region class and registers a reconfig notifier.
  212. */
  213. static int __init fpga_region_init(void)
  214. {
  215. fpga_region_class = class_create(THIS_MODULE, "fpga_region");
  216. if (IS_ERR(fpga_region_class))
  217. return PTR_ERR(fpga_region_class);
  218. fpga_region_class->dev_release = fpga_region_dev_release;
  219. return 0;
  220. }
  221. static void __exit fpga_region_exit(void)
  222. {
  223. class_destroy(fpga_region_class);
  224. ida_destroy(&fpga_region_ida);
  225. }
  226. subsys_initcall(fpga_region_init);
  227. module_exit(fpga_region_exit);
  228. MODULE_DESCRIPTION("FPGA Region");
  229. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  230. MODULE_LICENSE("GPL v2");