dfl-fme-pr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for FPGA Management Engine (FME) Partial Reconfiguration
  4. *
  5. * Copyright (C) 2017-2018 Intel Corporation, Inc.
  6. *
  7. * Authors:
  8. * Kang Luwei <luwei.kang@intel.com>
  9. * Xiao Guangrong <guangrong.xiao@linux.intel.com>
  10. * Wu Hao <hao.wu@intel.com>
  11. * Joseph Grecco <joe.grecco@intel.com>
  12. * Enno Luebbers <enno.luebbers@intel.com>
  13. * Tim Whisonant <tim.whisonant@intel.com>
  14. * Ananda Ravuri <ananda.ravuri@intel.com>
  15. * Christopher Rauer <christopher.rauer@intel.com>
  16. * Henry Mitchel <henry.mitchel@intel.com>
  17. */
  18. #include <linux/types.h>
  19. #include <linux/device.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/fpga/fpga-mgr.h>
  23. #include <linux/fpga/fpga-bridge.h>
  24. #include <linux/fpga/fpga-region.h>
  25. #include <linux/fpga-dfl.h>
  26. #include "dfl.h"
  27. #include "dfl-fme.h"
  28. #include "dfl-fme-pr.h"
  29. static struct dfl_fme_region *
  30. dfl_fme_region_find_by_port_id(struct dfl_fme *fme, int port_id)
  31. {
  32. struct dfl_fme_region *fme_region;
  33. list_for_each_entry(fme_region, &fme->region_list, node)
  34. if (fme_region->port_id == port_id)
  35. return fme_region;
  36. return NULL;
  37. }
  38. static int dfl_fme_region_match(struct device *dev, const void *data)
  39. {
  40. return dev->parent == data;
  41. }
  42. static struct fpga_region *dfl_fme_region_find(struct dfl_fme *fme, int port_id)
  43. {
  44. struct dfl_fme_region *fme_region;
  45. struct fpga_region *region;
  46. fme_region = dfl_fme_region_find_by_port_id(fme, port_id);
  47. if (!fme_region)
  48. return NULL;
  49. region = fpga_region_class_find(NULL, &fme_region->region->dev,
  50. dfl_fme_region_match);
  51. if (!region)
  52. return NULL;
  53. return region;
  54. }
  55. static int fme_pr(struct platform_device *pdev, unsigned long arg)
  56. {
  57. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  58. void __user *argp = (void __user *)arg;
  59. struct dfl_fpga_fme_port_pr port_pr;
  60. struct fpga_image_info *info;
  61. struct fpga_region *region;
  62. void __iomem *fme_hdr;
  63. struct dfl_fme *fme;
  64. unsigned long minsz;
  65. void *buf = NULL;
  66. int ret = 0;
  67. u64 v;
  68. minsz = offsetofend(struct dfl_fpga_fme_port_pr, buffer_address);
  69. if (copy_from_user(&port_pr, argp, minsz))
  70. return -EFAULT;
  71. if (port_pr.argsz < minsz || port_pr.flags)
  72. return -EINVAL;
  73. if (!IS_ALIGNED(port_pr.buffer_size, 4))
  74. return -EINVAL;
  75. /* get fme header region */
  76. fme_hdr = dfl_get_feature_ioaddr_by_id(&pdev->dev,
  77. FME_FEATURE_ID_HEADER);
  78. /* check port id */
  79. v = readq(fme_hdr + FME_HDR_CAP);
  80. if (port_pr.port_id >= FIELD_GET(FME_CAP_NUM_PORTS, v)) {
  81. dev_dbg(&pdev->dev, "port number more than maximum\n");
  82. return -EINVAL;
  83. }
  84. if (!access_ok(VERIFY_READ,
  85. (void __user *)(unsigned long)port_pr.buffer_address,
  86. port_pr.buffer_size))
  87. return -EFAULT;
  88. buf = vmalloc(port_pr.buffer_size);
  89. if (!buf)
  90. return -ENOMEM;
  91. if (copy_from_user(buf,
  92. (void __user *)(unsigned long)port_pr.buffer_address,
  93. port_pr.buffer_size)) {
  94. ret = -EFAULT;
  95. goto free_exit;
  96. }
  97. /* prepare fpga_image_info for PR */
  98. info = fpga_image_info_alloc(&pdev->dev);
  99. if (!info) {
  100. ret = -ENOMEM;
  101. goto free_exit;
  102. }
  103. info->flags |= FPGA_MGR_PARTIAL_RECONFIG;
  104. mutex_lock(&pdata->lock);
  105. fme = dfl_fpga_pdata_get_private(pdata);
  106. /* fme device has been unregistered. */
  107. if (!fme) {
  108. ret = -EINVAL;
  109. goto unlock_exit;
  110. }
  111. region = dfl_fme_region_find(fme, port_pr.port_id);
  112. if (!region) {
  113. ret = -EINVAL;
  114. goto unlock_exit;
  115. }
  116. fpga_image_info_free(region->info);
  117. info->buf = buf;
  118. info->count = port_pr.buffer_size;
  119. info->region_id = port_pr.port_id;
  120. region->info = info;
  121. ret = fpga_region_program_fpga(region);
  122. /*
  123. * it allows userspace to reset the PR region's logic by disabling and
  124. * reenabling the bridge to clear things out between accleration runs.
  125. * so no need to hold the bridges after partial reconfiguration.
  126. */
  127. if (region->get_bridges)
  128. fpga_bridges_put(&region->bridge_list);
  129. put_device(&region->dev);
  130. unlock_exit:
  131. mutex_unlock(&pdata->lock);
  132. free_exit:
  133. vfree(buf);
  134. if (copy_to_user((void __user *)arg, &port_pr, minsz))
  135. return -EFAULT;
  136. return ret;
  137. }
  138. /**
  139. * dfl_fme_create_mgr - create fpga mgr platform device as child device
  140. *
  141. * @pdata: fme platform_device's pdata
  142. *
  143. * Return: mgr platform device if successful, and error code otherwise.
  144. */
  145. static struct platform_device *
  146. dfl_fme_create_mgr(struct dfl_feature_platform_data *pdata,
  147. struct dfl_feature *feature)
  148. {
  149. struct platform_device *mgr, *fme = pdata->dev;
  150. struct dfl_fme_mgr_pdata mgr_pdata;
  151. int ret = -ENOMEM;
  152. if (!feature->ioaddr)
  153. return ERR_PTR(-ENODEV);
  154. mgr_pdata.ioaddr = feature->ioaddr;
  155. /*
  156. * Each FME has only one fpga-mgr, so allocate platform device using
  157. * the same FME platform device id.
  158. */
  159. mgr = platform_device_alloc(DFL_FPGA_FME_MGR, fme->id);
  160. if (!mgr)
  161. return ERR_PTR(ret);
  162. mgr->dev.parent = &fme->dev;
  163. ret = platform_device_add_data(mgr, &mgr_pdata, sizeof(mgr_pdata));
  164. if (ret)
  165. goto create_mgr_err;
  166. ret = platform_device_add(mgr);
  167. if (ret)
  168. goto create_mgr_err;
  169. return mgr;
  170. create_mgr_err:
  171. platform_device_put(mgr);
  172. return ERR_PTR(ret);
  173. }
  174. /**
  175. * dfl_fme_destroy_mgr - destroy fpga mgr platform device
  176. * @pdata: fme platform device's pdata
  177. */
  178. static void dfl_fme_destroy_mgr(struct dfl_feature_platform_data *pdata)
  179. {
  180. struct dfl_fme *priv = dfl_fpga_pdata_get_private(pdata);
  181. platform_device_unregister(priv->mgr);
  182. }
  183. /**
  184. * dfl_fme_create_bridge - create fme fpga bridge platform device as child
  185. *
  186. * @pdata: fme platform device's pdata
  187. * @port_id: port id for the bridge to be created.
  188. *
  189. * Return: bridge platform device if successful, and error code otherwise.
  190. */
  191. static struct dfl_fme_bridge *
  192. dfl_fme_create_bridge(struct dfl_feature_platform_data *pdata, int port_id)
  193. {
  194. struct device *dev = &pdata->dev->dev;
  195. struct dfl_fme_br_pdata br_pdata;
  196. struct dfl_fme_bridge *fme_br;
  197. int ret = -ENOMEM;
  198. fme_br = devm_kzalloc(dev, sizeof(*fme_br), GFP_KERNEL);
  199. if (!fme_br)
  200. return ERR_PTR(ret);
  201. br_pdata.cdev = pdata->dfl_cdev;
  202. br_pdata.port_id = port_id;
  203. fme_br->br = platform_device_alloc(DFL_FPGA_FME_BRIDGE,
  204. PLATFORM_DEVID_AUTO);
  205. if (!fme_br->br)
  206. return ERR_PTR(ret);
  207. fme_br->br->dev.parent = dev;
  208. ret = platform_device_add_data(fme_br->br, &br_pdata, sizeof(br_pdata));
  209. if (ret)
  210. goto create_br_err;
  211. ret = platform_device_add(fme_br->br);
  212. if (ret)
  213. goto create_br_err;
  214. return fme_br;
  215. create_br_err:
  216. platform_device_put(fme_br->br);
  217. return ERR_PTR(ret);
  218. }
  219. /**
  220. * dfl_fme_destroy_bridge - destroy fpga bridge platform device
  221. * @fme_br: fme bridge to destroy
  222. */
  223. static void dfl_fme_destroy_bridge(struct dfl_fme_bridge *fme_br)
  224. {
  225. platform_device_unregister(fme_br->br);
  226. }
  227. /**
  228. * dfl_fme_destroy_bridge - destroy all fpga bridge platform device
  229. * @pdata: fme platform device's pdata
  230. */
  231. static void dfl_fme_destroy_bridges(struct dfl_feature_platform_data *pdata)
  232. {
  233. struct dfl_fme *priv = dfl_fpga_pdata_get_private(pdata);
  234. struct dfl_fme_bridge *fbridge, *tmp;
  235. list_for_each_entry_safe(fbridge, tmp, &priv->bridge_list, node) {
  236. list_del(&fbridge->node);
  237. dfl_fme_destroy_bridge(fbridge);
  238. }
  239. }
  240. /**
  241. * dfl_fme_create_region - create fpga region platform device as child
  242. *
  243. * @pdata: fme platform device's pdata
  244. * @mgr: mgr platform device needed for region
  245. * @br: br platform device needed for region
  246. * @port_id: port id
  247. *
  248. * Return: fme region if successful, and error code otherwise.
  249. */
  250. static struct dfl_fme_region *
  251. dfl_fme_create_region(struct dfl_feature_platform_data *pdata,
  252. struct platform_device *mgr,
  253. struct platform_device *br, int port_id)
  254. {
  255. struct dfl_fme_region_pdata region_pdata;
  256. struct device *dev = &pdata->dev->dev;
  257. struct dfl_fme_region *fme_region;
  258. int ret = -ENOMEM;
  259. fme_region = devm_kzalloc(dev, sizeof(*fme_region), GFP_KERNEL);
  260. if (!fme_region)
  261. return ERR_PTR(ret);
  262. region_pdata.mgr = mgr;
  263. region_pdata.br = br;
  264. /*
  265. * Each FPGA device may have more than one port, so allocate platform
  266. * device using the same port platform device id.
  267. */
  268. fme_region->region = platform_device_alloc(DFL_FPGA_FME_REGION, br->id);
  269. if (!fme_region->region)
  270. return ERR_PTR(ret);
  271. fme_region->region->dev.parent = dev;
  272. ret = platform_device_add_data(fme_region->region, &region_pdata,
  273. sizeof(region_pdata));
  274. if (ret)
  275. goto create_region_err;
  276. ret = platform_device_add(fme_region->region);
  277. if (ret)
  278. goto create_region_err;
  279. fme_region->port_id = port_id;
  280. return fme_region;
  281. create_region_err:
  282. platform_device_put(fme_region->region);
  283. return ERR_PTR(ret);
  284. }
  285. /**
  286. * dfl_fme_destroy_region - destroy fme region
  287. * @fme_region: fme region to destroy
  288. */
  289. static void dfl_fme_destroy_region(struct dfl_fme_region *fme_region)
  290. {
  291. platform_device_unregister(fme_region->region);
  292. }
  293. /**
  294. * dfl_fme_destroy_regions - destroy all fme regions
  295. * @pdata: fme platform device's pdata
  296. */
  297. static void dfl_fme_destroy_regions(struct dfl_feature_platform_data *pdata)
  298. {
  299. struct dfl_fme *priv = dfl_fpga_pdata_get_private(pdata);
  300. struct dfl_fme_region *fme_region, *tmp;
  301. list_for_each_entry_safe(fme_region, tmp, &priv->region_list, node) {
  302. list_del(&fme_region->node);
  303. dfl_fme_destroy_region(fme_region);
  304. }
  305. }
  306. static int pr_mgmt_init(struct platform_device *pdev,
  307. struct dfl_feature *feature)
  308. {
  309. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  310. struct dfl_fme_region *fme_region;
  311. struct dfl_fme_bridge *fme_br;
  312. struct platform_device *mgr;
  313. struct dfl_fme *priv;
  314. void __iomem *fme_hdr;
  315. int ret = -ENODEV, i = 0;
  316. u64 fme_cap, port_offset;
  317. fme_hdr = dfl_get_feature_ioaddr_by_id(&pdev->dev,
  318. FME_FEATURE_ID_HEADER);
  319. mutex_lock(&pdata->lock);
  320. priv = dfl_fpga_pdata_get_private(pdata);
  321. /* Initialize the region and bridge sub device list */
  322. INIT_LIST_HEAD(&priv->region_list);
  323. INIT_LIST_HEAD(&priv->bridge_list);
  324. /* Create fpga mgr platform device */
  325. mgr = dfl_fme_create_mgr(pdata, feature);
  326. if (IS_ERR(mgr)) {
  327. dev_err(&pdev->dev, "fail to create fpga mgr pdev\n");
  328. goto unlock;
  329. }
  330. priv->mgr = mgr;
  331. /* Read capability register to check number of regions and bridges */
  332. fme_cap = readq(fme_hdr + FME_HDR_CAP);
  333. for (; i < FIELD_GET(FME_CAP_NUM_PORTS, fme_cap); i++) {
  334. port_offset = readq(fme_hdr + FME_HDR_PORT_OFST(i));
  335. if (!(port_offset & FME_PORT_OFST_IMP))
  336. continue;
  337. /* Create bridge for each port */
  338. fme_br = dfl_fme_create_bridge(pdata, i);
  339. if (IS_ERR(fme_br)) {
  340. ret = PTR_ERR(fme_br);
  341. goto destroy_region;
  342. }
  343. list_add(&fme_br->node, &priv->bridge_list);
  344. /* Create region for each port */
  345. fme_region = dfl_fme_create_region(pdata, mgr,
  346. fme_br->br, i);
  347. if (IS_ERR(fme_region)) {
  348. ret = PTR_ERR(fme_region);
  349. goto destroy_region;
  350. }
  351. list_add(&fme_region->node, &priv->region_list);
  352. }
  353. mutex_unlock(&pdata->lock);
  354. return 0;
  355. destroy_region:
  356. dfl_fme_destroy_regions(pdata);
  357. dfl_fme_destroy_bridges(pdata);
  358. dfl_fme_destroy_mgr(pdata);
  359. unlock:
  360. mutex_unlock(&pdata->lock);
  361. return ret;
  362. }
  363. static void pr_mgmt_uinit(struct platform_device *pdev,
  364. struct dfl_feature *feature)
  365. {
  366. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  367. struct dfl_fme *priv;
  368. mutex_lock(&pdata->lock);
  369. priv = dfl_fpga_pdata_get_private(pdata);
  370. dfl_fme_destroy_regions(pdata);
  371. dfl_fme_destroy_bridges(pdata);
  372. dfl_fme_destroy_mgr(pdata);
  373. mutex_unlock(&pdata->lock);
  374. }
  375. static long fme_pr_ioctl(struct platform_device *pdev,
  376. struct dfl_feature *feature,
  377. unsigned int cmd, unsigned long arg)
  378. {
  379. long ret;
  380. switch (cmd) {
  381. case DFL_FPGA_FME_PORT_PR:
  382. ret = fme_pr(pdev, arg);
  383. break;
  384. default:
  385. ret = -ENODEV;
  386. }
  387. return ret;
  388. }
  389. const struct dfl_feature_ops pr_mgmt_ops = {
  390. .init = pr_mgmt_init,
  391. .uinit = pr_mgmt_uinit,
  392. .ioctl = fme_pr_ioctl,
  393. };