fpga-bridge.c 11 KB

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