fpga-bridge.c 11 KB

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