fpga-bridge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. * The caller of this function is responsible for freeing the bridge with
  277. * fpga_bridge_free(). Using devm_fpga_bridge_create() instead is recommended.
  278. *
  279. * Return: struct fpga_bridge or NULL
  280. */
  281. struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
  282. const struct fpga_bridge_ops *br_ops,
  283. void *priv)
  284. {
  285. struct fpga_bridge *bridge;
  286. int id, ret = 0;
  287. if (!name || !strlen(name)) {
  288. dev_err(dev, "Attempt to register with no name!\n");
  289. return NULL;
  290. }
  291. bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
  292. if (!bridge)
  293. return NULL;
  294. id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
  295. if (id < 0) {
  296. ret = id;
  297. goto error_kfree;
  298. }
  299. mutex_init(&bridge->mutex);
  300. INIT_LIST_HEAD(&bridge->node);
  301. bridge->name = name;
  302. bridge->br_ops = br_ops;
  303. bridge->priv = priv;
  304. device_initialize(&bridge->dev);
  305. bridge->dev.groups = br_ops->groups;
  306. bridge->dev.class = fpga_bridge_class;
  307. bridge->dev.parent = dev;
  308. bridge->dev.of_node = dev->of_node;
  309. bridge->dev.id = id;
  310. ret = dev_set_name(&bridge->dev, "br%d", id);
  311. if (ret)
  312. goto error_device;
  313. return bridge;
  314. error_device:
  315. ida_simple_remove(&fpga_bridge_ida, id);
  316. error_kfree:
  317. kfree(bridge);
  318. return NULL;
  319. }
  320. EXPORT_SYMBOL_GPL(fpga_bridge_create);
  321. /**
  322. * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
  323. * @bridge: FPGA bridge struct
  324. */
  325. void fpga_bridge_free(struct fpga_bridge *bridge)
  326. {
  327. ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
  328. kfree(bridge);
  329. }
  330. EXPORT_SYMBOL_GPL(fpga_bridge_free);
  331. static void devm_fpga_bridge_release(struct device *dev, void *res)
  332. {
  333. struct fpga_bridge *bridge = *(struct fpga_bridge **)res;
  334. fpga_bridge_free(bridge);
  335. }
  336. /**
  337. * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
  338. * @dev: FPGA bridge device from pdev
  339. * @name: FPGA bridge name
  340. * @br_ops: pointer to structure of fpga bridge ops
  341. * @priv: FPGA bridge private data
  342. *
  343. * This function is intended for use in a FPGA bridge driver's probe function.
  344. * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
  345. * should register the bridge with fpga_bridge_register(). The bridge driver's
  346. * remove function should call fpga_bridge_unregister(). The bridge struct
  347. * allocated with this function will be freed automatically on driver detach.
  348. * This includes the case of a probe function returning error before calling
  349. * fpga_bridge_register(), the struct will still get cleaned up.
  350. *
  351. * Return: struct fpga_bridge or NULL
  352. */
  353. struct fpga_bridge
  354. *devm_fpga_bridge_create(struct device *dev, const char *name,
  355. const struct fpga_bridge_ops *br_ops, void *priv)
  356. {
  357. struct fpga_bridge **ptr, *bridge;
  358. ptr = devres_alloc(devm_fpga_bridge_release, sizeof(*ptr), GFP_KERNEL);
  359. if (!ptr)
  360. return NULL;
  361. bridge = fpga_bridge_create(dev, name, br_ops, priv);
  362. if (!bridge) {
  363. devres_free(ptr);
  364. } else {
  365. *ptr = bridge;
  366. devres_add(dev, ptr);
  367. }
  368. return bridge;
  369. }
  370. EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
  371. /**
  372. * fpga_bridge_register - register a FPGA bridge
  373. *
  374. * @bridge: FPGA bridge struct
  375. *
  376. * Return: 0 for success, error code otherwise.
  377. */
  378. int fpga_bridge_register(struct fpga_bridge *bridge)
  379. {
  380. struct device *dev = &bridge->dev;
  381. int ret;
  382. ret = device_add(dev);
  383. if (ret)
  384. return ret;
  385. of_platform_populate(dev->of_node, NULL, NULL, dev);
  386. dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(fpga_bridge_register);
  390. /**
  391. * fpga_bridge_unregister - unregister a FPGA bridge
  392. *
  393. * @bridge: FPGA bridge struct
  394. *
  395. * This function is intended for use in a FPGA bridge driver's remove function.
  396. */
  397. void fpga_bridge_unregister(struct fpga_bridge *bridge)
  398. {
  399. /*
  400. * If the low level driver provides a method for putting bridge into
  401. * a desired state upon unregister, do it.
  402. */
  403. if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
  404. bridge->br_ops->fpga_bridge_remove(bridge);
  405. device_unregister(&bridge->dev);
  406. }
  407. EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
  408. static void fpga_bridge_dev_release(struct device *dev)
  409. {
  410. }
  411. static int __init fpga_bridge_dev_init(void)
  412. {
  413. spin_lock_init(&bridge_list_lock);
  414. fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
  415. if (IS_ERR(fpga_bridge_class))
  416. return PTR_ERR(fpga_bridge_class);
  417. fpga_bridge_class->dev_groups = fpga_bridge_groups;
  418. fpga_bridge_class->dev_release = fpga_bridge_dev_release;
  419. return 0;
  420. }
  421. static void __exit fpga_bridge_dev_exit(void)
  422. {
  423. class_destroy(fpga_bridge_class);
  424. ida_destroy(&fpga_bridge_ida);
  425. }
  426. MODULE_DESCRIPTION("FPGA Bridge Driver");
  427. MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
  428. MODULE_LICENSE("GPL v2");
  429. subsys_initcall(fpga_bridge_dev_init);
  430. module_exit(fpga_bridge_dev_exit);