dfl-afu-main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for FPGA Accelerated Function Unit (AFU)
  4. *
  5. * Copyright (C) 2017-2018 Intel Corporation, Inc.
  6. *
  7. * Authors:
  8. * Wu Hao <hao.wu@intel.com>
  9. * Xiao Guangrong <guangrong.xiao@linux.intel.com>
  10. * Joseph Grecco <joe.grecco@intel.com>
  11. * Enno Luebbers <enno.luebbers@intel.com>
  12. * Tim Whisonant <tim.whisonant@intel.com>
  13. * Ananda Ravuri <ananda.ravuri@intel.com>
  14. * Henry Mitchel <henry.mitchel@intel.com>
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/fpga-dfl.h>
  20. #include "dfl-afu.h"
  21. /**
  22. * port_enable - enable a port
  23. * @pdev: port platform device.
  24. *
  25. * Enable Port by clear the port soft reset bit, which is set by default.
  26. * The AFU is unable to respond to any MMIO access while in reset.
  27. * port_enable function should only be used after port_disable function.
  28. */
  29. static void port_enable(struct platform_device *pdev)
  30. {
  31. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  32. void __iomem *base;
  33. u64 v;
  34. WARN_ON(!pdata->disable_count);
  35. if (--pdata->disable_count != 0)
  36. return;
  37. base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
  38. /* Clear port soft reset */
  39. v = readq(base + PORT_HDR_CTRL);
  40. v &= ~PORT_CTRL_SFTRST;
  41. writeq(v, base + PORT_HDR_CTRL);
  42. }
  43. #define RST_POLL_INVL 10 /* us */
  44. #define RST_POLL_TIMEOUT 1000 /* us */
  45. /**
  46. * port_disable - disable a port
  47. * @pdev: port platform device.
  48. *
  49. * Disable Port by setting the port soft reset bit, it puts the port into
  50. * reset.
  51. */
  52. static int port_disable(struct platform_device *pdev)
  53. {
  54. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  55. void __iomem *base;
  56. u64 v;
  57. if (pdata->disable_count++ != 0)
  58. return 0;
  59. base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
  60. /* Set port soft reset */
  61. v = readq(base + PORT_HDR_CTRL);
  62. v |= PORT_CTRL_SFTRST;
  63. writeq(v, base + PORT_HDR_CTRL);
  64. /*
  65. * HW sets ack bit to 1 when all outstanding requests have been drained
  66. * on this port and minimum soft reset pulse width has elapsed.
  67. * Driver polls port_soft_reset_ack to determine if reset done by HW.
  68. */
  69. if (readq_poll_timeout(base + PORT_HDR_CTRL, v, v & PORT_CTRL_SFTRST,
  70. RST_POLL_INVL, RST_POLL_TIMEOUT)) {
  71. dev_err(&pdev->dev, "timeout, fail to reset device\n");
  72. return -ETIMEDOUT;
  73. }
  74. return 0;
  75. }
  76. /*
  77. * This function resets the FPGA Port and its accelerator (AFU) by function
  78. * __port_disable and __port_enable (set port soft reset bit and then clear
  79. * it). Userspace can do Port reset at any time, e.g. during DMA or Partial
  80. * Reconfiguration. But it should never cause any system level issue, only
  81. * functional failure (e.g. DMA or PR operation failure) and be recoverable
  82. * from the failure.
  83. *
  84. * Note: the accelerator (AFU) is not accessible when its port is in reset
  85. * (disabled). Any attempts on MMIO access to AFU while in reset, will
  86. * result errors reported via port error reporting sub feature (if present).
  87. */
  88. static int __port_reset(struct platform_device *pdev)
  89. {
  90. int ret;
  91. ret = port_disable(pdev);
  92. if (!ret)
  93. port_enable(pdev);
  94. return ret;
  95. }
  96. static int port_reset(struct platform_device *pdev)
  97. {
  98. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  99. int ret;
  100. mutex_lock(&pdata->lock);
  101. ret = __port_reset(pdev);
  102. mutex_unlock(&pdata->lock);
  103. return ret;
  104. }
  105. static int port_get_id(struct platform_device *pdev)
  106. {
  107. void __iomem *base;
  108. base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
  109. return FIELD_GET(PORT_CAP_PORT_NUM, readq(base + PORT_HDR_CAP));
  110. }
  111. static ssize_t
  112. id_show(struct device *dev, struct device_attribute *attr, char *buf)
  113. {
  114. int id = port_get_id(to_platform_device(dev));
  115. return scnprintf(buf, PAGE_SIZE, "%d\n", id);
  116. }
  117. static DEVICE_ATTR_RO(id);
  118. static const struct attribute *port_hdr_attrs[] = {
  119. &dev_attr_id.attr,
  120. NULL,
  121. };
  122. static int port_hdr_init(struct platform_device *pdev,
  123. struct dfl_feature *feature)
  124. {
  125. dev_dbg(&pdev->dev, "PORT HDR Init.\n");
  126. port_reset(pdev);
  127. return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
  128. }
  129. static void port_hdr_uinit(struct platform_device *pdev,
  130. struct dfl_feature *feature)
  131. {
  132. dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
  133. sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
  134. }
  135. static long
  136. port_hdr_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
  137. unsigned int cmd, unsigned long arg)
  138. {
  139. long ret;
  140. switch (cmd) {
  141. case DFL_FPGA_PORT_RESET:
  142. if (!arg)
  143. ret = port_reset(pdev);
  144. else
  145. ret = -EINVAL;
  146. break;
  147. default:
  148. dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
  149. ret = -ENODEV;
  150. }
  151. return ret;
  152. }
  153. static const struct dfl_feature_ops port_hdr_ops = {
  154. .init = port_hdr_init,
  155. .uinit = port_hdr_uinit,
  156. .ioctl = port_hdr_ioctl,
  157. };
  158. static ssize_t
  159. afu_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  160. {
  161. struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
  162. void __iomem *base;
  163. u64 guidl, guidh;
  164. base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_AFU);
  165. mutex_lock(&pdata->lock);
  166. if (pdata->disable_count) {
  167. mutex_unlock(&pdata->lock);
  168. return -EBUSY;
  169. }
  170. guidl = readq(base + GUID_L);
  171. guidh = readq(base + GUID_H);
  172. mutex_unlock(&pdata->lock);
  173. return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n", guidh, guidl);
  174. }
  175. static DEVICE_ATTR_RO(afu_id);
  176. static const struct attribute *port_afu_attrs[] = {
  177. &dev_attr_afu_id.attr,
  178. NULL
  179. };
  180. static int port_afu_init(struct platform_device *pdev,
  181. struct dfl_feature *feature)
  182. {
  183. struct resource *res = &pdev->resource[feature->resource_index];
  184. int ret;
  185. dev_dbg(&pdev->dev, "PORT AFU Init.\n");
  186. ret = afu_mmio_region_add(dev_get_platdata(&pdev->dev),
  187. DFL_PORT_REGION_INDEX_AFU, resource_size(res),
  188. res->start, DFL_PORT_REGION_READ |
  189. DFL_PORT_REGION_WRITE | DFL_PORT_REGION_MMAP);
  190. if (ret)
  191. return ret;
  192. return sysfs_create_files(&pdev->dev.kobj, port_afu_attrs);
  193. }
  194. static void port_afu_uinit(struct platform_device *pdev,
  195. struct dfl_feature *feature)
  196. {
  197. dev_dbg(&pdev->dev, "PORT AFU UInit.\n");
  198. sysfs_remove_files(&pdev->dev.kobj, port_afu_attrs);
  199. }
  200. static const struct dfl_feature_ops port_afu_ops = {
  201. .init = port_afu_init,
  202. .uinit = port_afu_uinit,
  203. };
  204. static struct dfl_feature_driver port_feature_drvs[] = {
  205. {
  206. .id = PORT_FEATURE_ID_HEADER,
  207. .ops = &port_hdr_ops,
  208. },
  209. {
  210. .id = PORT_FEATURE_ID_AFU,
  211. .ops = &port_afu_ops,
  212. },
  213. {
  214. .ops = NULL,
  215. }
  216. };
  217. static int afu_open(struct inode *inode, struct file *filp)
  218. {
  219. struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
  220. struct dfl_feature_platform_data *pdata;
  221. int ret;
  222. pdata = dev_get_platdata(&fdev->dev);
  223. if (WARN_ON(!pdata))
  224. return -ENODEV;
  225. ret = dfl_feature_dev_use_begin(pdata);
  226. if (ret)
  227. return ret;
  228. dev_dbg(&fdev->dev, "Device File Open\n");
  229. filp->private_data = fdev;
  230. return 0;
  231. }
  232. static int afu_release(struct inode *inode, struct file *filp)
  233. {
  234. struct platform_device *pdev = filp->private_data;
  235. struct dfl_feature_platform_data *pdata;
  236. dev_dbg(&pdev->dev, "Device File Release\n");
  237. pdata = dev_get_platdata(&pdev->dev);
  238. mutex_lock(&pdata->lock);
  239. __port_reset(pdev);
  240. afu_dma_region_destroy(pdata);
  241. mutex_unlock(&pdata->lock);
  242. dfl_feature_dev_use_end(pdata);
  243. return 0;
  244. }
  245. static long afu_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
  246. unsigned long arg)
  247. {
  248. /* No extension support for now */
  249. return 0;
  250. }
  251. static long
  252. afu_ioctl_get_info(struct dfl_feature_platform_data *pdata, void __user *arg)
  253. {
  254. struct dfl_fpga_port_info info;
  255. struct dfl_afu *afu;
  256. unsigned long minsz;
  257. minsz = offsetofend(struct dfl_fpga_port_info, num_umsgs);
  258. if (copy_from_user(&info, arg, minsz))
  259. return -EFAULT;
  260. if (info.argsz < minsz)
  261. return -EINVAL;
  262. mutex_lock(&pdata->lock);
  263. afu = dfl_fpga_pdata_get_private(pdata);
  264. info.flags = 0;
  265. info.num_regions = afu->num_regions;
  266. info.num_umsgs = afu->num_umsgs;
  267. mutex_unlock(&pdata->lock);
  268. if (copy_to_user(arg, &info, sizeof(info)))
  269. return -EFAULT;
  270. return 0;
  271. }
  272. static long afu_ioctl_get_region_info(struct dfl_feature_platform_data *pdata,
  273. void __user *arg)
  274. {
  275. struct dfl_fpga_port_region_info rinfo;
  276. struct dfl_afu_mmio_region region;
  277. unsigned long minsz;
  278. long ret;
  279. minsz = offsetofend(struct dfl_fpga_port_region_info, offset);
  280. if (copy_from_user(&rinfo, arg, minsz))
  281. return -EFAULT;
  282. if (rinfo.argsz < minsz || rinfo.padding)
  283. return -EINVAL;
  284. ret = afu_mmio_region_get_by_index(pdata, rinfo.index, &region);
  285. if (ret)
  286. return ret;
  287. rinfo.flags = region.flags;
  288. rinfo.size = region.size;
  289. rinfo.offset = region.offset;
  290. if (copy_to_user(arg, &rinfo, sizeof(rinfo)))
  291. return -EFAULT;
  292. return 0;
  293. }
  294. static long
  295. afu_ioctl_dma_map(struct dfl_feature_platform_data *pdata, void __user *arg)
  296. {
  297. struct dfl_fpga_port_dma_map map;
  298. unsigned long minsz;
  299. long ret;
  300. minsz = offsetofend(struct dfl_fpga_port_dma_map, iova);
  301. if (copy_from_user(&map, arg, minsz))
  302. return -EFAULT;
  303. if (map.argsz < minsz || map.flags)
  304. return -EINVAL;
  305. ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
  306. if (ret)
  307. return ret;
  308. if (copy_to_user(arg, &map, sizeof(map))) {
  309. afu_dma_unmap_region(pdata, map.iova);
  310. return -EFAULT;
  311. }
  312. dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
  313. (unsigned long long)map.user_addr,
  314. (unsigned long long)map.length,
  315. (unsigned long long)map.iova);
  316. return 0;
  317. }
  318. static long
  319. afu_ioctl_dma_unmap(struct dfl_feature_platform_data *pdata, void __user *arg)
  320. {
  321. struct dfl_fpga_port_dma_unmap unmap;
  322. unsigned long minsz;
  323. minsz = offsetofend(struct dfl_fpga_port_dma_unmap, iova);
  324. if (copy_from_user(&unmap, arg, minsz))
  325. return -EFAULT;
  326. if (unmap.argsz < minsz || unmap.flags)
  327. return -EINVAL;
  328. return afu_dma_unmap_region(pdata, unmap.iova);
  329. }
  330. static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  331. {
  332. struct platform_device *pdev = filp->private_data;
  333. struct dfl_feature_platform_data *pdata;
  334. struct dfl_feature *f;
  335. long ret;
  336. dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
  337. pdata = dev_get_platdata(&pdev->dev);
  338. switch (cmd) {
  339. case DFL_FPGA_GET_API_VERSION:
  340. return DFL_FPGA_API_VERSION;
  341. case DFL_FPGA_CHECK_EXTENSION:
  342. return afu_ioctl_check_extension(pdata, arg);
  343. case DFL_FPGA_PORT_GET_INFO:
  344. return afu_ioctl_get_info(pdata, (void __user *)arg);
  345. case DFL_FPGA_PORT_GET_REGION_INFO:
  346. return afu_ioctl_get_region_info(pdata, (void __user *)arg);
  347. case DFL_FPGA_PORT_DMA_MAP:
  348. return afu_ioctl_dma_map(pdata, (void __user *)arg);
  349. case DFL_FPGA_PORT_DMA_UNMAP:
  350. return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
  351. default:
  352. /*
  353. * Let sub-feature's ioctl function to handle the cmd
  354. * Sub-feature's ioctl returns -ENODEV when cmd is not
  355. * handled in this sub feature, and returns 0 and other
  356. * error code if cmd is handled.
  357. */
  358. dfl_fpga_dev_for_each_feature(pdata, f)
  359. if (f->ops && f->ops->ioctl) {
  360. ret = f->ops->ioctl(pdev, f, cmd, arg);
  361. if (ret != -ENODEV)
  362. return ret;
  363. }
  364. }
  365. return -EINVAL;
  366. }
  367. static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
  368. {
  369. struct platform_device *pdev = filp->private_data;
  370. struct dfl_feature_platform_data *pdata;
  371. u64 size = vma->vm_end - vma->vm_start;
  372. struct dfl_afu_mmio_region region;
  373. u64 offset;
  374. int ret;
  375. if (!(vma->vm_flags & VM_SHARED))
  376. return -EINVAL;
  377. pdata = dev_get_platdata(&pdev->dev);
  378. offset = vma->vm_pgoff << PAGE_SHIFT;
  379. ret = afu_mmio_region_get_by_offset(pdata, offset, size, &region);
  380. if (ret)
  381. return ret;
  382. if (!(region.flags & DFL_PORT_REGION_MMAP))
  383. return -EINVAL;
  384. if ((vma->vm_flags & VM_READ) && !(region.flags & DFL_PORT_REGION_READ))
  385. return -EPERM;
  386. if ((vma->vm_flags & VM_WRITE) &&
  387. !(region.flags & DFL_PORT_REGION_WRITE))
  388. return -EPERM;
  389. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  390. return remap_pfn_range(vma, vma->vm_start,
  391. (region.phys + (offset - region.offset)) >> PAGE_SHIFT,
  392. size, vma->vm_page_prot);
  393. }
  394. static const struct file_operations afu_fops = {
  395. .owner = THIS_MODULE,
  396. .open = afu_open,
  397. .release = afu_release,
  398. .unlocked_ioctl = afu_ioctl,
  399. .mmap = afu_mmap,
  400. };
  401. static int afu_dev_init(struct platform_device *pdev)
  402. {
  403. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  404. struct dfl_afu *afu;
  405. afu = devm_kzalloc(&pdev->dev, sizeof(*afu), GFP_KERNEL);
  406. if (!afu)
  407. return -ENOMEM;
  408. afu->pdata = pdata;
  409. mutex_lock(&pdata->lock);
  410. dfl_fpga_pdata_set_private(pdata, afu);
  411. afu_mmio_region_init(pdata);
  412. afu_dma_region_init(pdata);
  413. mutex_unlock(&pdata->lock);
  414. return 0;
  415. }
  416. static int afu_dev_destroy(struct platform_device *pdev)
  417. {
  418. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  419. struct dfl_afu *afu;
  420. mutex_lock(&pdata->lock);
  421. afu = dfl_fpga_pdata_get_private(pdata);
  422. afu_mmio_region_destroy(pdata);
  423. afu_dma_region_destroy(pdata);
  424. dfl_fpga_pdata_set_private(pdata, NULL);
  425. mutex_unlock(&pdata->lock);
  426. return 0;
  427. }
  428. static int port_enable_set(struct platform_device *pdev, bool enable)
  429. {
  430. struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
  431. int ret = 0;
  432. mutex_lock(&pdata->lock);
  433. if (enable)
  434. port_enable(pdev);
  435. else
  436. ret = port_disable(pdev);
  437. mutex_unlock(&pdata->lock);
  438. return ret;
  439. }
  440. static struct dfl_fpga_port_ops afu_port_ops = {
  441. .name = DFL_FPGA_FEATURE_DEV_PORT,
  442. .owner = THIS_MODULE,
  443. .get_id = port_get_id,
  444. .enable_set = port_enable_set,
  445. };
  446. static int afu_probe(struct platform_device *pdev)
  447. {
  448. int ret;
  449. dev_dbg(&pdev->dev, "%s\n", __func__);
  450. ret = afu_dev_init(pdev);
  451. if (ret)
  452. goto exit;
  453. ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs);
  454. if (ret)
  455. goto dev_destroy;
  456. ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE);
  457. if (ret) {
  458. dfl_fpga_dev_feature_uinit(pdev);
  459. goto dev_destroy;
  460. }
  461. return 0;
  462. dev_destroy:
  463. afu_dev_destroy(pdev);
  464. exit:
  465. return ret;
  466. }
  467. static int afu_remove(struct platform_device *pdev)
  468. {
  469. dev_dbg(&pdev->dev, "%s\n", __func__);
  470. dfl_fpga_dev_ops_unregister(pdev);
  471. dfl_fpga_dev_feature_uinit(pdev);
  472. afu_dev_destroy(pdev);
  473. return 0;
  474. }
  475. static struct platform_driver afu_driver = {
  476. .driver = {
  477. .name = DFL_FPGA_FEATURE_DEV_PORT,
  478. },
  479. .probe = afu_probe,
  480. .remove = afu_remove,
  481. };
  482. static int __init afu_init(void)
  483. {
  484. int ret;
  485. dfl_fpga_port_ops_add(&afu_port_ops);
  486. ret = platform_driver_register(&afu_driver);
  487. if (ret)
  488. dfl_fpga_port_ops_del(&afu_port_ops);
  489. return ret;
  490. }
  491. static void __exit afu_exit(void)
  492. {
  493. platform_driver_unregister(&afu_driver);
  494. dfl_fpga_port_ops_del(&afu_port_ops);
  495. }
  496. module_init(afu_init);
  497. module_exit(afu_exit);
  498. MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver");
  499. MODULE_AUTHOR("Intel Corporation");
  500. MODULE_LICENSE("GPL v2");
  501. MODULE_ALIAS("platform:dfl-port");