fpga-bridge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * FPGA Bridge Framework Driver
  3. *
  4. * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
  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/idr.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. static DEFINE_IDA(fpga_bridge_ida);
  27. static struct class *fpga_bridge_class;
  28. /* Lock for adding/removing bridges to linked lists*/
  29. static spinlock_t bridge_list_lock;
  30. static int fpga_bridge_of_node_match(struct device *dev, const void *data)
  31. {
  32. return dev->of_node == data;
  33. }
  34. /**
  35. * fpga_bridge_enable - Enable transactions on the bridge
  36. *
  37. * @bridge: FPGA bridge
  38. *
  39. * Return: 0 for success, error code otherwise.
  40. */
  41. int fpga_bridge_enable(struct fpga_bridge *bridge)
  42. {
  43. dev_dbg(&bridge->dev, "enable\n");
  44. if (bridge->br_ops && bridge->br_ops->enable_set)
  45. return bridge->br_ops->enable_set(bridge, 1);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL_GPL(fpga_bridge_enable);
  49. /**
  50. * fpga_bridge_disable - Disable transactions on the bridge
  51. *
  52. * @bridge: FPGA bridge
  53. *
  54. * Return: 0 for success, error code otherwise.
  55. */
  56. int fpga_bridge_disable(struct fpga_bridge *bridge)
  57. {
  58. dev_dbg(&bridge->dev, "disable\n");
  59. if (bridge->br_ops && bridge->br_ops->enable_set)
  60. return bridge->br_ops->enable_set(bridge, 0);
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(fpga_bridge_disable);
  64. static struct fpga_bridge *__fpga_bridge_get(struct device *dev,
  65. struct fpga_image_info *info)
  66. {
  67. struct fpga_bridge *bridge;
  68. int ret = -ENODEV;
  69. bridge = to_fpga_bridge(dev);
  70. if (!bridge)
  71. goto err_dev;
  72. bridge->info = info;
  73. if (!mutex_trylock(&bridge->mutex)) {
  74. ret = -EBUSY;
  75. goto err_dev;
  76. }
  77. if (!try_module_get(dev->parent->driver->owner))
  78. goto err_ll_mod;
  79. dev_dbg(&bridge->dev, "get\n");
  80. return bridge;
  81. err_ll_mod:
  82. mutex_unlock(&bridge->mutex);
  83. err_dev:
  84. put_device(dev);
  85. return ERR_PTR(ret);
  86. }
  87. /**
  88. * of_fpga_bridge_get - get an exclusive reference to a fpga bridge
  89. *
  90. * @np: node pointer of a FPGA bridge
  91. * @info: fpga image specific information
  92. *
  93. * Return fpga_bridge struct if successful.
  94. * Return -EBUSY if someone already has a reference to the bridge.
  95. * Return -ENODEV if @np is not a FPGA Bridge.
  96. */
  97. struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
  98. struct fpga_image_info *info)
  99. {
  100. struct device *dev;
  101. dev = class_find_device(fpga_bridge_class, NULL, np,
  102. fpga_bridge_of_node_match);
  103. if (!dev)
  104. return ERR_PTR(-ENODEV);
  105. return __fpga_bridge_get(dev, info);
  106. }
  107. EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
  108. static int fpga_bridge_dev_match(struct device *dev, const void *data)
  109. {
  110. return dev->parent == data;
  111. }
  112. /**
  113. * fpga_bridge_get - get an exclusive reference to a fpga bridge
  114. * @dev: parent device that fpga bridge was registered with
  115. *
  116. * Given a device, get an exclusive reference to a fpga bridge.
  117. *
  118. * Return: fpga manager struct or IS_ERR() condition containing error code.
  119. */
  120. struct fpga_bridge *fpga_bridge_get(struct device *dev,
  121. struct fpga_image_info *info)
  122. {
  123. struct device *bridge_dev;
  124. bridge_dev = class_find_device(fpga_bridge_class, NULL, dev,
  125. fpga_bridge_dev_match);
  126. if (!bridge_dev)
  127. return ERR_PTR(-ENODEV);
  128. return __fpga_bridge_get(bridge_dev, info);
  129. }
  130. EXPORT_SYMBOL_GPL(fpga_bridge_get);
  131. /**
  132. * fpga_bridge_put - release a reference to a bridge
  133. *
  134. * @bridge: FPGA bridge
  135. */
  136. void fpga_bridge_put(struct fpga_bridge *bridge)
  137. {
  138. dev_dbg(&bridge->dev, "put\n");
  139. bridge->info = NULL;
  140. module_put(bridge->dev.parent->driver->owner);
  141. mutex_unlock(&bridge->mutex);
  142. put_device(&bridge->dev);
  143. }
  144. EXPORT_SYMBOL_GPL(fpga_bridge_put);
  145. /**
  146. * fpga_bridges_enable - enable bridges in a list
  147. * @bridge_list: list of FPGA bridges
  148. *
  149. * Enable each bridge in the list. If list is empty, do nothing.
  150. *
  151. * Return 0 for success or empty bridge list; return error code otherwise.
  152. */
  153. int fpga_bridges_enable(struct list_head *bridge_list)
  154. {
  155. struct fpga_bridge *bridge;
  156. int ret;
  157. list_for_each_entry(bridge, bridge_list, node) {
  158. ret = fpga_bridge_enable(bridge);
  159. if (ret)
  160. return ret;
  161. }
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(fpga_bridges_enable);
  165. /**
  166. * fpga_bridges_disable - disable bridges in a list
  167. *
  168. * @bridge_list: list of FPGA bridges
  169. *
  170. * Disable each bridge in the list. If list is empty, do nothing.
  171. *
  172. * Return 0 for success or empty bridge list; return error code otherwise.
  173. */
  174. int fpga_bridges_disable(struct list_head *bridge_list)
  175. {
  176. struct fpga_bridge *bridge;
  177. int ret;
  178. list_for_each_entry(bridge, bridge_list, node) {
  179. ret = fpga_bridge_disable(bridge);
  180. if (ret)
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(fpga_bridges_disable);
  186. /**
  187. * fpga_bridges_put - put bridges
  188. *
  189. * @bridge_list: list of FPGA bridges
  190. *
  191. * For each bridge in the list, put the bridge and remove it from the list.
  192. * If list is empty, do nothing.
  193. */
  194. void fpga_bridges_put(struct list_head *bridge_list)
  195. {
  196. struct fpga_bridge *bridge, *next;
  197. unsigned long flags;
  198. list_for_each_entry_safe(bridge, next, bridge_list, node) {
  199. fpga_bridge_put(bridge);
  200. spin_lock_irqsave(&bridge_list_lock, flags);
  201. list_del(&bridge->node);
  202. spin_unlock_irqrestore(&bridge_list_lock, flags);
  203. }
  204. }
  205. EXPORT_SYMBOL_GPL(fpga_bridges_put);
  206. /**
  207. * of_fpga_bridge_get_to_list - get a bridge, add it to a list
  208. *
  209. * @np: node pointer of a FPGA bridge
  210. * @info: fpga image specific information
  211. * @bridge_list: list of FPGA bridges
  212. *
  213. * Get an exclusive reference to the bridge and and it to the list.
  214. *
  215. * Return 0 for success, error code from of_fpga_bridge_get() othewise.
  216. */
  217. int of_fpga_bridge_get_to_list(struct device_node *np,
  218. struct fpga_image_info *info,
  219. struct list_head *bridge_list)
  220. {
  221. struct fpga_bridge *bridge;
  222. unsigned long flags;
  223. bridge = of_fpga_bridge_get(np, info);
  224. if (IS_ERR(bridge))
  225. return PTR_ERR(bridge);
  226. spin_lock_irqsave(&bridge_list_lock, flags);
  227. list_add(&bridge->node, bridge_list);
  228. spin_unlock_irqrestore(&bridge_list_lock, flags);
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
  232. /**
  233. * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
  234. *
  235. * @dev: FPGA bridge device
  236. * @info: fpga image specific information
  237. * @bridge_list: list of FPGA bridges
  238. *
  239. * Get an exclusive reference to the bridge and and it to the list.
  240. *
  241. * Return 0 for success, error code from fpga_bridge_get() othewise.
  242. */
  243. int fpga_bridge_get_to_list(struct device *dev,
  244. struct fpga_image_info *info,
  245. struct list_head *bridge_list)
  246. {
  247. struct fpga_bridge *bridge;
  248. unsigned long flags;
  249. bridge = fpga_bridge_get(dev, info);
  250. if (IS_ERR(bridge))
  251. return PTR_ERR(bridge);
  252. spin_lock_irqsave(&bridge_list_lock, flags);
  253. list_add(&bridge->node, bridge_list);
  254. spin_unlock_irqrestore(&bridge_list_lock, flags);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
  258. static ssize_t name_show(struct device *dev,
  259. struct device_attribute *attr, char *buf)
  260. {
  261. struct fpga_bridge *bridge = to_fpga_bridge(dev);
  262. return sprintf(buf, "%s\n", bridge->name);
  263. }
  264. static ssize_t state_show(struct device *dev,
  265. struct device_attribute *attr, char *buf)
  266. {
  267. struct fpga_bridge *bridge = to_fpga_bridge(dev);
  268. int enable = 1;
  269. if (bridge->br_ops && bridge->br_ops->enable_show)
  270. enable = bridge->br_ops->enable_show(bridge);
  271. return sprintf(buf, "%s\n", enable ? "enabled" : "disabled");
  272. }
  273. static DEVICE_ATTR_RO(name);
  274. static DEVICE_ATTR_RO(state);
  275. static struct attribute *fpga_bridge_attrs[] = {
  276. &dev_attr_name.attr,
  277. &dev_attr_state.attr,
  278. NULL,
  279. };
  280. ATTRIBUTE_GROUPS(fpga_bridge);
  281. /**
  282. * fpga_bridge_register - register a fpga bridge driver
  283. * @dev: FPGA bridge device from pdev
  284. * @name: FPGA bridge name
  285. * @br_ops: pointer to structure of fpga bridge ops
  286. * @priv: FPGA bridge private data
  287. *
  288. * Return: 0 for success, error code otherwise.
  289. */
  290. int fpga_bridge_register(struct device *dev, const char *name,
  291. const struct fpga_bridge_ops *br_ops, void *priv)
  292. {
  293. struct fpga_bridge *bridge;
  294. int id, ret = 0;
  295. if (!name || !strlen(name)) {
  296. dev_err(dev, "Attempt to register with no name!\n");
  297. return -EINVAL;
  298. }
  299. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  300. if (!bridge)
  301. return -ENOMEM;
  302. id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
  303. if (id < 0) {
  304. ret = id;
  305. goto error_kfree;
  306. }
  307. mutex_init(&bridge->mutex);
  308. INIT_LIST_HEAD(&bridge->node);
  309. bridge->name = name;
  310. bridge->br_ops = br_ops;
  311. bridge->priv = priv;
  312. device_initialize(&bridge->dev);
  313. bridge->dev.class = fpga_bridge_class;
  314. bridge->dev.parent = dev;
  315. bridge->dev.of_node = dev->of_node;
  316. bridge->dev.id = id;
  317. dev_set_drvdata(dev, bridge);
  318. ret = dev_set_name(&bridge->dev, "br%d", id);
  319. if (ret)
  320. goto error_device;
  321. ret = device_add(&bridge->dev);
  322. if (ret)
  323. goto error_device;
  324. of_platform_populate(dev->of_node, NULL, NULL, dev);
  325. dev_info(bridge->dev.parent, "fpga bridge [%s] registered\n",
  326. bridge->name);
  327. return 0;
  328. error_device:
  329. ida_simple_remove(&fpga_bridge_ida, id);
  330. error_kfree:
  331. kfree(bridge);
  332. return ret;
  333. }
  334. EXPORT_SYMBOL_GPL(fpga_bridge_register);
  335. /**
  336. * fpga_bridge_unregister - unregister a fpga bridge driver
  337. * @dev: FPGA bridge device from pdev
  338. */
  339. void fpga_bridge_unregister(struct device *dev)
  340. {
  341. struct fpga_bridge *bridge = dev_get_drvdata(dev);
  342. /*
  343. * If the low level driver provides a method for putting bridge into
  344. * a desired state upon unregister, do it.
  345. */
  346. if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
  347. bridge->br_ops->fpga_bridge_remove(bridge);
  348. device_unregister(&bridge->dev);
  349. }
  350. EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
  351. static void fpga_bridge_dev_release(struct device *dev)
  352. {
  353. struct fpga_bridge *bridge = to_fpga_bridge(dev);
  354. ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
  355. kfree(bridge);
  356. }
  357. static int __init fpga_bridge_dev_init(void)
  358. {
  359. spin_lock_init(&bridge_list_lock);
  360. fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
  361. if (IS_ERR(fpga_bridge_class))
  362. return PTR_ERR(fpga_bridge_class);
  363. fpga_bridge_class->dev_groups = fpga_bridge_groups;
  364. fpga_bridge_class->dev_release = fpga_bridge_dev_release;
  365. return 0;
  366. }
  367. static void __exit fpga_bridge_dev_exit(void)
  368. {
  369. class_destroy(fpga_bridge_class);
  370. ida_destroy(&fpga_bridge_ida);
  371. }
  372. MODULE_DESCRIPTION("FPGA Bridge Driver");
  373. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  374. MODULE_LICENSE("GPL v2");
  375. subsys_initcall(fpga_bridge_dev_init);
  376. module_exit(fpga_bridge_dev_exit);