Linux 4.19.133
[linux/fpc-iii.git] / drivers / fpga / dfl-afu-main.c
blob02baa6a227c0c3317039d7d5ad3203188c7f7e13
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for FPGA Accelerated Function Unit (AFU)
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
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>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/uaccess.h>
20 #include <linux/fpga-dfl.h>
22 #include "dfl-afu.h"
24 /**
25 * port_enable - enable a port
26 * @pdev: port platform device.
28 * Enable Port by clear the port soft reset bit, which is set by default.
29 * The AFU is unable to respond to any MMIO access while in reset.
30 * port_enable function should only be used after port_disable function.
32 static void port_enable(struct platform_device *pdev)
34 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
35 void __iomem *base;
36 u64 v;
38 WARN_ON(!pdata->disable_count);
40 if (--pdata->disable_count != 0)
41 return;
43 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
45 /* Clear port soft reset */
46 v = readq(base + PORT_HDR_CTRL);
47 v &= ~PORT_CTRL_SFTRST;
48 writeq(v, base + PORT_HDR_CTRL);
51 #define RST_POLL_INVL 10 /* us */
52 #define RST_POLL_TIMEOUT 1000 /* us */
54 /**
55 * port_disable - disable a port
56 * @pdev: port platform device.
58 * Disable Port by setting the port soft reset bit, it puts the port into
59 * reset.
61 static int port_disable(struct platform_device *pdev)
63 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
64 void __iomem *base;
65 u64 v;
67 if (pdata->disable_count++ != 0)
68 return 0;
70 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
72 /* Set port soft reset */
73 v = readq(base + PORT_HDR_CTRL);
74 v |= PORT_CTRL_SFTRST;
75 writeq(v, base + PORT_HDR_CTRL);
78 * HW sets ack bit to 1 when all outstanding requests have been drained
79 * on this port and minimum soft reset pulse width has elapsed.
80 * Driver polls port_soft_reset_ack to determine if reset done by HW.
82 if (readq_poll_timeout(base + PORT_HDR_CTRL, v, v & PORT_CTRL_SFTRST,
83 RST_POLL_INVL, RST_POLL_TIMEOUT)) {
84 dev_err(&pdev->dev, "timeout, fail to reset device\n");
85 return -ETIMEDOUT;
88 return 0;
92 * This function resets the FPGA Port and its accelerator (AFU) by function
93 * __port_disable and __port_enable (set port soft reset bit and then clear
94 * it). Userspace can do Port reset at any time, e.g. during DMA or Partial
95 * Reconfiguration. But it should never cause any system level issue, only
96 * functional failure (e.g. DMA or PR operation failure) and be recoverable
97 * from the failure.
99 * Note: the accelerator (AFU) is not accessible when its port is in reset
100 * (disabled). Any attempts on MMIO access to AFU while in reset, will
101 * result errors reported via port error reporting sub feature (if present).
103 static int __port_reset(struct platform_device *pdev)
105 int ret;
107 ret = port_disable(pdev);
108 if (!ret)
109 port_enable(pdev);
111 return ret;
114 static int port_reset(struct platform_device *pdev)
116 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
117 int ret;
119 mutex_lock(&pdata->lock);
120 ret = __port_reset(pdev);
121 mutex_unlock(&pdata->lock);
123 return ret;
126 static int port_get_id(struct platform_device *pdev)
128 void __iomem *base;
130 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
132 return FIELD_GET(PORT_CAP_PORT_NUM, readq(base + PORT_HDR_CAP));
135 static ssize_t
136 id_show(struct device *dev, struct device_attribute *attr, char *buf)
138 int id = port_get_id(to_platform_device(dev));
140 return scnprintf(buf, PAGE_SIZE, "%d\n", id);
142 static DEVICE_ATTR_RO(id);
144 static const struct attribute *port_hdr_attrs[] = {
145 &dev_attr_id.attr,
146 NULL,
149 static int port_hdr_init(struct platform_device *pdev,
150 struct dfl_feature *feature)
152 dev_dbg(&pdev->dev, "PORT HDR Init.\n");
154 port_reset(pdev);
156 return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
159 static void port_hdr_uinit(struct platform_device *pdev,
160 struct dfl_feature *feature)
162 dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
164 sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
167 static long
168 port_hdr_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
169 unsigned int cmd, unsigned long arg)
171 long ret;
173 switch (cmd) {
174 case DFL_FPGA_PORT_RESET:
175 if (!arg)
176 ret = port_reset(pdev);
177 else
178 ret = -EINVAL;
179 break;
180 default:
181 dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
182 ret = -ENODEV;
185 return ret;
188 static const struct dfl_feature_ops port_hdr_ops = {
189 .init = port_hdr_init,
190 .uinit = port_hdr_uinit,
191 .ioctl = port_hdr_ioctl,
194 static ssize_t
195 afu_id_show(struct device *dev, struct device_attribute *attr, char *buf)
197 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
198 void __iomem *base;
199 u64 guidl, guidh;
201 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_AFU);
203 mutex_lock(&pdata->lock);
204 if (pdata->disable_count) {
205 mutex_unlock(&pdata->lock);
206 return -EBUSY;
209 guidl = readq(base + GUID_L);
210 guidh = readq(base + GUID_H);
211 mutex_unlock(&pdata->lock);
213 return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n", guidh, guidl);
215 static DEVICE_ATTR_RO(afu_id);
217 static const struct attribute *port_afu_attrs[] = {
218 &dev_attr_afu_id.attr,
219 NULL
222 static int port_afu_init(struct platform_device *pdev,
223 struct dfl_feature *feature)
225 struct resource *res = &pdev->resource[feature->resource_index];
226 int ret;
228 dev_dbg(&pdev->dev, "PORT AFU Init.\n");
230 ret = afu_mmio_region_add(dev_get_platdata(&pdev->dev),
231 DFL_PORT_REGION_INDEX_AFU, resource_size(res),
232 res->start, DFL_PORT_REGION_READ |
233 DFL_PORT_REGION_WRITE | DFL_PORT_REGION_MMAP);
234 if (ret)
235 return ret;
237 return sysfs_create_files(&pdev->dev.kobj, port_afu_attrs);
240 static void port_afu_uinit(struct platform_device *pdev,
241 struct dfl_feature *feature)
243 dev_dbg(&pdev->dev, "PORT AFU UInit.\n");
245 sysfs_remove_files(&pdev->dev.kobj, port_afu_attrs);
248 static const struct dfl_feature_ops port_afu_ops = {
249 .init = port_afu_init,
250 .uinit = port_afu_uinit,
253 static struct dfl_feature_driver port_feature_drvs[] = {
255 .id = PORT_FEATURE_ID_HEADER,
256 .ops = &port_hdr_ops,
259 .id = PORT_FEATURE_ID_AFU,
260 .ops = &port_afu_ops,
263 .ops = NULL,
267 static int afu_open(struct inode *inode, struct file *filp)
269 struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
270 struct dfl_feature_platform_data *pdata;
271 int ret;
273 pdata = dev_get_platdata(&fdev->dev);
274 if (WARN_ON(!pdata))
275 return -ENODEV;
277 ret = dfl_feature_dev_use_begin(pdata);
278 if (ret)
279 return ret;
281 dev_dbg(&fdev->dev, "Device File Open\n");
282 filp->private_data = fdev;
284 return 0;
287 static int afu_release(struct inode *inode, struct file *filp)
289 struct platform_device *pdev = filp->private_data;
290 struct dfl_feature_platform_data *pdata;
292 dev_dbg(&pdev->dev, "Device File Release\n");
294 pdata = dev_get_platdata(&pdev->dev);
296 mutex_lock(&pdata->lock);
297 __port_reset(pdev);
298 afu_dma_region_destroy(pdata);
299 mutex_unlock(&pdata->lock);
301 dfl_feature_dev_use_end(pdata);
303 return 0;
306 static long afu_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
307 unsigned long arg)
309 /* No extension support for now */
310 return 0;
313 static long
314 afu_ioctl_get_info(struct dfl_feature_platform_data *pdata, void __user *arg)
316 struct dfl_fpga_port_info info;
317 struct dfl_afu *afu;
318 unsigned long minsz;
320 minsz = offsetofend(struct dfl_fpga_port_info, num_umsgs);
322 if (copy_from_user(&info, arg, minsz))
323 return -EFAULT;
325 if (info.argsz < minsz)
326 return -EINVAL;
328 mutex_lock(&pdata->lock);
329 afu = dfl_fpga_pdata_get_private(pdata);
330 info.flags = 0;
331 info.num_regions = afu->num_regions;
332 info.num_umsgs = afu->num_umsgs;
333 mutex_unlock(&pdata->lock);
335 if (copy_to_user(arg, &info, sizeof(info)))
336 return -EFAULT;
338 return 0;
341 static long afu_ioctl_get_region_info(struct dfl_feature_platform_data *pdata,
342 void __user *arg)
344 struct dfl_fpga_port_region_info rinfo;
345 struct dfl_afu_mmio_region region;
346 unsigned long minsz;
347 long ret;
349 minsz = offsetofend(struct dfl_fpga_port_region_info, offset);
351 if (copy_from_user(&rinfo, arg, minsz))
352 return -EFAULT;
354 if (rinfo.argsz < minsz || rinfo.padding)
355 return -EINVAL;
357 ret = afu_mmio_region_get_by_index(pdata, rinfo.index, &region);
358 if (ret)
359 return ret;
361 rinfo.flags = region.flags;
362 rinfo.size = region.size;
363 rinfo.offset = region.offset;
365 if (copy_to_user(arg, &rinfo, sizeof(rinfo)))
366 return -EFAULT;
368 return 0;
371 static long
372 afu_ioctl_dma_map(struct dfl_feature_platform_data *pdata, void __user *arg)
374 struct dfl_fpga_port_dma_map map;
375 unsigned long minsz;
376 long ret;
378 minsz = offsetofend(struct dfl_fpga_port_dma_map, iova);
380 if (copy_from_user(&map, arg, minsz))
381 return -EFAULT;
383 if (map.argsz < minsz || map.flags)
384 return -EINVAL;
386 ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
387 if (ret)
388 return ret;
390 if (copy_to_user(arg, &map, sizeof(map))) {
391 afu_dma_unmap_region(pdata, map.iova);
392 return -EFAULT;
395 dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
396 (unsigned long long)map.user_addr,
397 (unsigned long long)map.length,
398 (unsigned long long)map.iova);
400 return 0;
403 static long
404 afu_ioctl_dma_unmap(struct dfl_feature_platform_data *pdata, void __user *arg)
406 struct dfl_fpga_port_dma_unmap unmap;
407 unsigned long minsz;
409 minsz = offsetofend(struct dfl_fpga_port_dma_unmap, iova);
411 if (copy_from_user(&unmap, arg, minsz))
412 return -EFAULT;
414 if (unmap.argsz < minsz || unmap.flags)
415 return -EINVAL;
417 return afu_dma_unmap_region(pdata, unmap.iova);
420 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
422 struct platform_device *pdev = filp->private_data;
423 struct dfl_feature_platform_data *pdata;
424 struct dfl_feature *f;
425 long ret;
427 dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
429 pdata = dev_get_platdata(&pdev->dev);
431 switch (cmd) {
432 case DFL_FPGA_GET_API_VERSION:
433 return DFL_FPGA_API_VERSION;
434 case DFL_FPGA_CHECK_EXTENSION:
435 return afu_ioctl_check_extension(pdata, arg);
436 case DFL_FPGA_PORT_GET_INFO:
437 return afu_ioctl_get_info(pdata, (void __user *)arg);
438 case DFL_FPGA_PORT_GET_REGION_INFO:
439 return afu_ioctl_get_region_info(pdata, (void __user *)arg);
440 case DFL_FPGA_PORT_DMA_MAP:
441 return afu_ioctl_dma_map(pdata, (void __user *)arg);
442 case DFL_FPGA_PORT_DMA_UNMAP:
443 return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
444 default:
446 * Let sub-feature's ioctl function to handle the cmd
447 * Sub-feature's ioctl returns -ENODEV when cmd is not
448 * handled in this sub feature, and returns 0 and other
449 * error code if cmd is handled.
451 dfl_fpga_dev_for_each_feature(pdata, f)
452 if (f->ops && f->ops->ioctl) {
453 ret = f->ops->ioctl(pdev, f, cmd, arg);
454 if (ret != -ENODEV)
455 return ret;
459 return -EINVAL;
462 static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
464 struct platform_device *pdev = filp->private_data;
465 struct dfl_feature_platform_data *pdata;
466 u64 size = vma->vm_end - vma->vm_start;
467 struct dfl_afu_mmio_region region;
468 u64 offset;
469 int ret;
471 if (!(vma->vm_flags & VM_SHARED))
472 return -EINVAL;
474 pdata = dev_get_platdata(&pdev->dev);
476 offset = vma->vm_pgoff << PAGE_SHIFT;
477 ret = afu_mmio_region_get_by_offset(pdata, offset, size, &region);
478 if (ret)
479 return ret;
481 if (!(region.flags & DFL_PORT_REGION_MMAP))
482 return -EINVAL;
484 if ((vma->vm_flags & VM_READ) && !(region.flags & DFL_PORT_REGION_READ))
485 return -EPERM;
487 if ((vma->vm_flags & VM_WRITE) &&
488 !(region.flags & DFL_PORT_REGION_WRITE))
489 return -EPERM;
491 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
493 return remap_pfn_range(vma, vma->vm_start,
494 (region.phys + (offset - region.offset)) >> PAGE_SHIFT,
495 size, vma->vm_page_prot);
498 static const struct file_operations afu_fops = {
499 .owner = THIS_MODULE,
500 .open = afu_open,
501 .release = afu_release,
502 .unlocked_ioctl = afu_ioctl,
503 .mmap = afu_mmap,
506 static int afu_dev_init(struct platform_device *pdev)
508 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
509 struct dfl_afu *afu;
511 afu = devm_kzalloc(&pdev->dev, sizeof(*afu), GFP_KERNEL);
512 if (!afu)
513 return -ENOMEM;
515 afu->pdata = pdata;
517 mutex_lock(&pdata->lock);
518 dfl_fpga_pdata_set_private(pdata, afu);
519 afu_mmio_region_init(pdata);
520 afu_dma_region_init(pdata);
521 mutex_unlock(&pdata->lock);
523 return 0;
526 static int afu_dev_destroy(struct platform_device *pdev)
528 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
529 struct dfl_afu *afu;
531 mutex_lock(&pdata->lock);
532 afu = dfl_fpga_pdata_get_private(pdata);
533 afu_mmio_region_destroy(pdata);
534 afu_dma_region_destroy(pdata);
535 dfl_fpga_pdata_set_private(pdata, NULL);
536 mutex_unlock(&pdata->lock);
538 return 0;
541 static int port_enable_set(struct platform_device *pdev, bool enable)
543 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
544 int ret = 0;
546 mutex_lock(&pdata->lock);
547 if (enable)
548 port_enable(pdev);
549 else
550 ret = port_disable(pdev);
551 mutex_unlock(&pdata->lock);
553 return ret;
556 static struct dfl_fpga_port_ops afu_port_ops = {
557 .name = DFL_FPGA_FEATURE_DEV_PORT,
558 .owner = THIS_MODULE,
559 .get_id = port_get_id,
560 .enable_set = port_enable_set,
563 static int afu_probe(struct platform_device *pdev)
565 int ret;
567 dev_dbg(&pdev->dev, "%s\n", __func__);
569 ret = afu_dev_init(pdev);
570 if (ret)
571 goto exit;
573 ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs);
574 if (ret)
575 goto dev_destroy;
577 ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE);
578 if (ret) {
579 dfl_fpga_dev_feature_uinit(pdev);
580 goto dev_destroy;
583 return 0;
585 dev_destroy:
586 afu_dev_destroy(pdev);
587 exit:
588 return ret;
591 static int afu_remove(struct platform_device *pdev)
593 dev_dbg(&pdev->dev, "%s\n", __func__);
595 dfl_fpga_dev_ops_unregister(pdev);
596 dfl_fpga_dev_feature_uinit(pdev);
597 afu_dev_destroy(pdev);
599 return 0;
602 static struct platform_driver afu_driver = {
603 .driver = {
604 .name = DFL_FPGA_FEATURE_DEV_PORT,
606 .probe = afu_probe,
607 .remove = afu_remove,
610 static int __init afu_init(void)
612 int ret;
614 dfl_fpga_port_ops_add(&afu_port_ops);
616 ret = platform_driver_register(&afu_driver);
617 if (ret)
618 dfl_fpga_port_ops_del(&afu_port_ops);
620 return ret;
623 static void __exit afu_exit(void)
625 platform_driver_unregister(&afu_driver);
627 dfl_fpga_port_ops_del(&afu_port_ops);
630 module_init(afu_init);
631 module_exit(afu_exit);
633 MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver");
634 MODULE_AUTHOR("Intel Corporation");
635 MODULE_LICENSE("GPL v2");
636 MODULE_ALIAS("platform:dfl-port");