treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / fpga / dfl-afu-main.c
blob65437b6a68424b1dd6f10633f4f51e0cddf746a4
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 * __afu_port_enable - enable a port by clear reset
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 * __afu_port_enable function should only be used after __afu_port_disable
31 * function.
33 * The caller needs to hold lock for protection.
35 void __afu_port_enable(struct platform_device *pdev)
37 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
38 void __iomem *base;
39 u64 v;
41 WARN_ON(!pdata->disable_count);
43 if (--pdata->disable_count != 0)
44 return;
46 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
48 /* Clear port soft reset */
49 v = readq(base + PORT_HDR_CTRL);
50 v &= ~PORT_CTRL_SFTRST;
51 writeq(v, base + PORT_HDR_CTRL);
54 #define RST_POLL_INVL 10 /* us */
55 #define RST_POLL_TIMEOUT 1000 /* us */
57 /**
58 * __afu_port_disable - disable a port by hold reset
59 * @pdev: port platform device.
61 * Disable Port by setting the port soft reset bit, it puts the port into reset.
63 * The caller needs to hold lock for protection.
65 int __afu_port_disable(struct platform_device *pdev)
67 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
68 void __iomem *base;
69 u64 v;
71 if (pdata->disable_count++ != 0)
72 return 0;
74 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
76 /* Set port soft reset */
77 v = readq(base + PORT_HDR_CTRL);
78 v |= PORT_CTRL_SFTRST;
79 writeq(v, base + PORT_HDR_CTRL);
82 * HW sets ack bit to 1 when all outstanding requests have been drained
83 * on this port and minimum soft reset pulse width has elapsed.
84 * Driver polls port_soft_reset_ack to determine if reset done by HW.
86 if (readq_poll_timeout(base + PORT_HDR_CTRL, v, v & PORT_CTRL_SFTRST,
87 RST_POLL_INVL, RST_POLL_TIMEOUT)) {
88 dev_err(&pdev->dev, "timeout, fail to reset device\n");
89 return -ETIMEDOUT;
92 return 0;
96 * This function resets the FPGA Port and its accelerator (AFU) by function
97 * __port_disable and __port_enable (set port soft reset bit and then clear
98 * it). Userspace can do Port reset at any time, e.g. during DMA or Partial
99 * Reconfiguration. But it should never cause any system level issue, only
100 * functional failure (e.g. DMA or PR operation failure) and be recoverable
101 * from the failure.
103 * Note: the accelerator (AFU) is not accessible when its port is in reset
104 * (disabled). Any attempts on MMIO access to AFU while in reset, will
105 * result errors reported via port error reporting sub feature (if present).
107 static int __port_reset(struct platform_device *pdev)
109 int ret;
111 ret = __afu_port_disable(pdev);
112 if (!ret)
113 __afu_port_enable(pdev);
115 return ret;
118 static int port_reset(struct platform_device *pdev)
120 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
121 int ret;
123 mutex_lock(&pdata->lock);
124 ret = __port_reset(pdev);
125 mutex_unlock(&pdata->lock);
127 return ret;
130 static int port_get_id(struct platform_device *pdev)
132 void __iomem *base;
134 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
136 return FIELD_GET(PORT_CAP_PORT_NUM, readq(base + PORT_HDR_CAP));
139 static ssize_t
140 id_show(struct device *dev, struct device_attribute *attr, char *buf)
142 int id = port_get_id(to_platform_device(dev));
144 return scnprintf(buf, PAGE_SIZE, "%d\n", id);
146 static DEVICE_ATTR_RO(id);
148 static ssize_t
149 ltr_show(struct device *dev, struct device_attribute *attr, char *buf)
151 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
152 void __iomem *base;
153 u64 v;
155 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
157 mutex_lock(&pdata->lock);
158 v = readq(base + PORT_HDR_CTRL);
159 mutex_unlock(&pdata->lock);
161 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_CTRL_LATENCY, v));
164 static ssize_t
165 ltr_store(struct device *dev, struct device_attribute *attr,
166 const char *buf, size_t count)
168 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
169 void __iomem *base;
170 bool ltr;
171 u64 v;
173 if (kstrtobool(buf, &ltr))
174 return -EINVAL;
176 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
178 mutex_lock(&pdata->lock);
179 v = readq(base + PORT_HDR_CTRL);
180 v &= ~PORT_CTRL_LATENCY;
181 v |= FIELD_PREP(PORT_CTRL_LATENCY, ltr ? 1 : 0);
182 writeq(v, base + PORT_HDR_CTRL);
183 mutex_unlock(&pdata->lock);
185 return count;
187 static DEVICE_ATTR_RW(ltr);
189 static ssize_t
190 ap1_event_show(struct device *dev, struct device_attribute *attr, char *buf)
192 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
193 void __iomem *base;
194 u64 v;
196 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
198 mutex_lock(&pdata->lock);
199 v = readq(base + PORT_HDR_STS);
200 mutex_unlock(&pdata->lock);
202 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_STS_AP1_EVT, v));
205 static ssize_t
206 ap1_event_store(struct device *dev, struct device_attribute *attr,
207 const char *buf, size_t count)
209 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
210 void __iomem *base;
211 bool clear;
213 if (kstrtobool(buf, &clear) || !clear)
214 return -EINVAL;
216 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
218 mutex_lock(&pdata->lock);
219 writeq(PORT_STS_AP1_EVT, base + PORT_HDR_STS);
220 mutex_unlock(&pdata->lock);
222 return count;
224 static DEVICE_ATTR_RW(ap1_event);
226 static ssize_t
227 ap2_event_show(struct device *dev, struct device_attribute *attr,
228 char *buf)
230 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
231 void __iomem *base;
232 u64 v;
234 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
236 mutex_lock(&pdata->lock);
237 v = readq(base + PORT_HDR_STS);
238 mutex_unlock(&pdata->lock);
240 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_STS_AP2_EVT, v));
243 static ssize_t
244 ap2_event_store(struct device *dev, struct device_attribute *attr,
245 const char *buf, size_t count)
247 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
248 void __iomem *base;
249 bool clear;
251 if (kstrtobool(buf, &clear) || !clear)
252 return -EINVAL;
254 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
256 mutex_lock(&pdata->lock);
257 writeq(PORT_STS_AP2_EVT, base + PORT_HDR_STS);
258 mutex_unlock(&pdata->lock);
260 return count;
262 static DEVICE_ATTR_RW(ap2_event);
264 static ssize_t
265 power_state_show(struct device *dev, struct device_attribute *attr, char *buf)
267 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
268 void __iomem *base;
269 u64 v;
271 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
273 mutex_lock(&pdata->lock);
274 v = readq(base + PORT_HDR_STS);
275 mutex_unlock(&pdata->lock);
277 return sprintf(buf, "0x%x\n", (u8)FIELD_GET(PORT_STS_PWR_STATE, v));
279 static DEVICE_ATTR_RO(power_state);
281 static ssize_t
282 userclk_freqcmd_store(struct device *dev, struct device_attribute *attr,
283 const char *buf, size_t count)
285 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
286 u64 userclk_freq_cmd;
287 void __iomem *base;
289 if (kstrtou64(buf, 0, &userclk_freq_cmd))
290 return -EINVAL;
292 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
294 mutex_lock(&pdata->lock);
295 writeq(userclk_freq_cmd, base + PORT_HDR_USRCLK_CMD0);
296 mutex_unlock(&pdata->lock);
298 return count;
300 static DEVICE_ATTR_WO(userclk_freqcmd);
302 static ssize_t
303 userclk_freqcntrcmd_store(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
306 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
307 u64 userclk_freqcntr_cmd;
308 void __iomem *base;
310 if (kstrtou64(buf, 0, &userclk_freqcntr_cmd))
311 return -EINVAL;
313 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
315 mutex_lock(&pdata->lock);
316 writeq(userclk_freqcntr_cmd, base + PORT_HDR_USRCLK_CMD1);
317 mutex_unlock(&pdata->lock);
319 return count;
321 static DEVICE_ATTR_WO(userclk_freqcntrcmd);
323 static ssize_t
324 userclk_freqsts_show(struct device *dev, struct device_attribute *attr,
325 char *buf)
327 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
328 u64 userclk_freqsts;
329 void __iomem *base;
331 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
333 mutex_lock(&pdata->lock);
334 userclk_freqsts = readq(base + PORT_HDR_USRCLK_STS0);
335 mutex_unlock(&pdata->lock);
337 return sprintf(buf, "0x%llx\n", (unsigned long long)userclk_freqsts);
339 static DEVICE_ATTR_RO(userclk_freqsts);
341 static ssize_t
342 userclk_freqcntrsts_show(struct device *dev, struct device_attribute *attr,
343 char *buf)
345 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
346 u64 userclk_freqcntrsts;
347 void __iomem *base;
349 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
351 mutex_lock(&pdata->lock);
352 userclk_freqcntrsts = readq(base + PORT_HDR_USRCLK_STS1);
353 mutex_unlock(&pdata->lock);
355 return sprintf(buf, "0x%llx\n",
356 (unsigned long long)userclk_freqcntrsts);
358 static DEVICE_ATTR_RO(userclk_freqcntrsts);
360 static struct attribute *port_hdr_attrs[] = {
361 &dev_attr_id.attr,
362 &dev_attr_ltr.attr,
363 &dev_attr_ap1_event.attr,
364 &dev_attr_ap2_event.attr,
365 &dev_attr_power_state.attr,
366 &dev_attr_userclk_freqcmd.attr,
367 &dev_attr_userclk_freqcntrcmd.attr,
368 &dev_attr_userclk_freqsts.attr,
369 &dev_attr_userclk_freqcntrsts.attr,
370 NULL,
373 static umode_t port_hdr_attrs_visible(struct kobject *kobj,
374 struct attribute *attr, int n)
376 struct device *dev = kobj_to_dev(kobj);
377 umode_t mode = attr->mode;
378 void __iomem *base;
380 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
382 if (dfl_feature_revision(base) > 0) {
384 * userclk sysfs interfaces are only visible in case port
385 * revision is 0, as hardware with revision >0 doesn't
386 * support this.
388 if (attr == &dev_attr_userclk_freqcmd.attr ||
389 attr == &dev_attr_userclk_freqcntrcmd.attr ||
390 attr == &dev_attr_userclk_freqsts.attr ||
391 attr == &dev_attr_userclk_freqcntrsts.attr)
392 mode = 0;
395 return mode;
398 static const struct attribute_group port_hdr_group = {
399 .attrs = port_hdr_attrs,
400 .is_visible = port_hdr_attrs_visible,
403 static int port_hdr_init(struct platform_device *pdev,
404 struct dfl_feature *feature)
406 port_reset(pdev);
408 return 0;
411 static long
412 port_hdr_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
413 unsigned int cmd, unsigned long arg)
415 long ret;
417 switch (cmd) {
418 case DFL_FPGA_PORT_RESET:
419 if (!arg)
420 ret = port_reset(pdev);
421 else
422 ret = -EINVAL;
423 break;
424 default:
425 dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
426 ret = -ENODEV;
429 return ret;
432 static const struct dfl_feature_id port_hdr_id_table[] = {
433 {.id = PORT_FEATURE_ID_HEADER,},
434 {0,}
437 static const struct dfl_feature_ops port_hdr_ops = {
438 .init = port_hdr_init,
439 .ioctl = port_hdr_ioctl,
442 static ssize_t
443 afu_id_show(struct device *dev, struct device_attribute *attr, char *buf)
445 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
446 void __iomem *base;
447 u64 guidl, guidh;
449 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_AFU);
451 mutex_lock(&pdata->lock);
452 if (pdata->disable_count) {
453 mutex_unlock(&pdata->lock);
454 return -EBUSY;
457 guidl = readq(base + GUID_L);
458 guidh = readq(base + GUID_H);
459 mutex_unlock(&pdata->lock);
461 return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n", guidh, guidl);
463 static DEVICE_ATTR_RO(afu_id);
465 static struct attribute *port_afu_attrs[] = {
466 &dev_attr_afu_id.attr,
467 NULL
470 static umode_t port_afu_attrs_visible(struct kobject *kobj,
471 struct attribute *attr, int n)
473 struct device *dev = kobj_to_dev(kobj);
476 * sysfs entries are visible only if related private feature is
477 * enumerated.
479 if (!dfl_get_feature_by_id(dev, PORT_FEATURE_ID_AFU))
480 return 0;
482 return attr->mode;
485 static const struct attribute_group port_afu_group = {
486 .attrs = port_afu_attrs,
487 .is_visible = port_afu_attrs_visible,
490 static int port_afu_init(struct platform_device *pdev,
491 struct dfl_feature *feature)
493 struct resource *res = &pdev->resource[feature->resource_index];
495 return afu_mmio_region_add(dev_get_platdata(&pdev->dev),
496 DFL_PORT_REGION_INDEX_AFU,
497 resource_size(res), res->start,
498 DFL_PORT_REGION_MMAP | DFL_PORT_REGION_READ |
499 DFL_PORT_REGION_WRITE);
502 static const struct dfl_feature_id port_afu_id_table[] = {
503 {.id = PORT_FEATURE_ID_AFU,},
504 {0,}
507 static const struct dfl_feature_ops port_afu_ops = {
508 .init = port_afu_init,
511 static int port_stp_init(struct platform_device *pdev,
512 struct dfl_feature *feature)
514 struct resource *res = &pdev->resource[feature->resource_index];
516 return afu_mmio_region_add(dev_get_platdata(&pdev->dev),
517 DFL_PORT_REGION_INDEX_STP,
518 resource_size(res), res->start,
519 DFL_PORT_REGION_MMAP | DFL_PORT_REGION_READ |
520 DFL_PORT_REGION_WRITE);
523 static const struct dfl_feature_id port_stp_id_table[] = {
524 {.id = PORT_FEATURE_ID_STP,},
525 {0,}
528 static const struct dfl_feature_ops port_stp_ops = {
529 .init = port_stp_init,
532 static struct dfl_feature_driver port_feature_drvs[] = {
534 .id_table = port_hdr_id_table,
535 .ops = &port_hdr_ops,
538 .id_table = port_afu_id_table,
539 .ops = &port_afu_ops,
542 .id_table = port_err_id_table,
543 .ops = &port_err_ops,
546 .id_table = port_stp_id_table,
547 .ops = &port_stp_ops,
550 .ops = NULL,
554 static int afu_open(struct inode *inode, struct file *filp)
556 struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
557 struct dfl_feature_platform_data *pdata;
558 int ret;
560 pdata = dev_get_platdata(&fdev->dev);
561 if (WARN_ON(!pdata))
562 return -ENODEV;
564 ret = dfl_feature_dev_use_begin(pdata);
565 if (ret)
566 return ret;
568 dev_dbg(&fdev->dev, "Device File Open\n");
569 filp->private_data = fdev;
571 return 0;
574 static int afu_release(struct inode *inode, struct file *filp)
576 struct platform_device *pdev = filp->private_data;
577 struct dfl_feature_platform_data *pdata;
579 dev_dbg(&pdev->dev, "Device File Release\n");
581 pdata = dev_get_platdata(&pdev->dev);
583 mutex_lock(&pdata->lock);
584 __port_reset(pdev);
585 afu_dma_region_destroy(pdata);
586 mutex_unlock(&pdata->lock);
588 dfl_feature_dev_use_end(pdata);
590 return 0;
593 static long afu_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
594 unsigned long arg)
596 /* No extension support for now */
597 return 0;
600 static long
601 afu_ioctl_get_info(struct dfl_feature_platform_data *pdata, void __user *arg)
603 struct dfl_fpga_port_info info;
604 struct dfl_afu *afu;
605 unsigned long minsz;
607 minsz = offsetofend(struct dfl_fpga_port_info, num_umsgs);
609 if (copy_from_user(&info, arg, minsz))
610 return -EFAULT;
612 if (info.argsz < minsz)
613 return -EINVAL;
615 mutex_lock(&pdata->lock);
616 afu = dfl_fpga_pdata_get_private(pdata);
617 info.flags = 0;
618 info.num_regions = afu->num_regions;
619 info.num_umsgs = afu->num_umsgs;
620 mutex_unlock(&pdata->lock);
622 if (copy_to_user(arg, &info, sizeof(info)))
623 return -EFAULT;
625 return 0;
628 static long afu_ioctl_get_region_info(struct dfl_feature_platform_data *pdata,
629 void __user *arg)
631 struct dfl_fpga_port_region_info rinfo;
632 struct dfl_afu_mmio_region region;
633 unsigned long minsz;
634 long ret;
636 minsz = offsetofend(struct dfl_fpga_port_region_info, offset);
638 if (copy_from_user(&rinfo, arg, minsz))
639 return -EFAULT;
641 if (rinfo.argsz < minsz || rinfo.padding)
642 return -EINVAL;
644 ret = afu_mmio_region_get_by_index(pdata, rinfo.index, &region);
645 if (ret)
646 return ret;
648 rinfo.flags = region.flags;
649 rinfo.size = region.size;
650 rinfo.offset = region.offset;
652 if (copy_to_user(arg, &rinfo, sizeof(rinfo)))
653 return -EFAULT;
655 return 0;
658 static long
659 afu_ioctl_dma_map(struct dfl_feature_platform_data *pdata, void __user *arg)
661 struct dfl_fpga_port_dma_map map;
662 unsigned long minsz;
663 long ret;
665 minsz = offsetofend(struct dfl_fpga_port_dma_map, iova);
667 if (copy_from_user(&map, arg, minsz))
668 return -EFAULT;
670 if (map.argsz < minsz || map.flags)
671 return -EINVAL;
673 ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
674 if (ret)
675 return ret;
677 if (copy_to_user(arg, &map, sizeof(map))) {
678 afu_dma_unmap_region(pdata, map.iova);
679 return -EFAULT;
682 dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
683 (unsigned long long)map.user_addr,
684 (unsigned long long)map.length,
685 (unsigned long long)map.iova);
687 return 0;
690 static long
691 afu_ioctl_dma_unmap(struct dfl_feature_platform_data *pdata, void __user *arg)
693 struct dfl_fpga_port_dma_unmap unmap;
694 unsigned long minsz;
696 minsz = offsetofend(struct dfl_fpga_port_dma_unmap, iova);
698 if (copy_from_user(&unmap, arg, minsz))
699 return -EFAULT;
701 if (unmap.argsz < minsz || unmap.flags)
702 return -EINVAL;
704 return afu_dma_unmap_region(pdata, unmap.iova);
707 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
709 struct platform_device *pdev = filp->private_data;
710 struct dfl_feature_platform_data *pdata;
711 struct dfl_feature *f;
712 long ret;
714 dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
716 pdata = dev_get_platdata(&pdev->dev);
718 switch (cmd) {
719 case DFL_FPGA_GET_API_VERSION:
720 return DFL_FPGA_API_VERSION;
721 case DFL_FPGA_CHECK_EXTENSION:
722 return afu_ioctl_check_extension(pdata, arg);
723 case DFL_FPGA_PORT_GET_INFO:
724 return afu_ioctl_get_info(pdata, (void __user *)arg);
725 case DFL_FPGA_PORT_GET_REGION_INFO:
726 return afu_ioctl_get_region_info(pdata, (void __user *)arg);
727 case DFL_FPGA_PORT_DMA_MAP:
728 return afu_ioctl_dma_map(pdata, (void __user *)arg);
729 case DFL_FPGA_PORT_DMA_UNMAP:
730 return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
731 default:
733 * Let sub-feature's ioctl function to handle the cmd
734 * Sub-feature's ioctl returns -ENODEV when cmd is not
735 * handled in this sub feature, and returns 0 and other
736 * error code if cmd is handled.
738 dfl_fpga_dev_for_each_feature(pdata, f)
739 if (f->ops && f->ops->ioctl) {
740 ret = f->ops->ioctl(pdev, f, cmd, arg);
741 if (ret != -ENODEV)
742 return ret;
746 return -EINVAL;
749 static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
751 struct platform_device *pdev = filp->private_data;
752 struct dfl_feature_platform_data *pdata;
753 u64 size = vma->vm_end - vma->vm_start;
754 struct dfl_afu_mmio_region region;
755 u64 offset;
756 int ret;
758 if (!(vma->vm_flags & VM_SHARED))
759 return -EINVAL;
761 pdata = dev_get_platdata(&pdev->dev);
763 offset = vma->vm_pgoff << PAGE_SHIFT;
764 ret = afu_mmio_region_get_by_offset(pdata, offset, size, &region);
765 if (ret)
766 return ret;
768 if (!(region.flags & DFL_PORT_REGION_MMAP))
769 return -EINVAL;
771 if ((vma->vm_flags & VM_READ) && !(region.flags & DFL_PORT_REGION_READ))
772 return -EPERM;
774 if ((vma->vm_flags & VM_WRITE) &&
775 !(region.flags & DFL_PORT_REGION_WRITE))
776 return -EPERM;
778 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
780 return remap_pfn_range(vma, vma->vm_start,
781 (region.phys + (offset - region.offset)) >> PAGE_SHIFT,
782 size, vma->vm_page_prot);
785 static const struct file_operations afu_fops = {
786 .owner = THIS_MODULE,
787 .open = afu_open,
788 .release = afu_release,
789 .unlocked_ioctl = afu_ioctl,
790 .mmap = afu_mmap,
793 static int afu_dev_init(struct platform_device *pdev)
795 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
796 struct dfl_afu *afu;
798 afu = devm_kzalloc(&pdev->dev, sizeof(*afu), GFP_KERNEL);
799 if (!afu)
800 return -ENOMEM;
802 afu->pdata = pdata;
804 mutex_lock(&pdata->lock);
805 dfl_fpga_pdata_set_private(pdata, afu);
806 afu_mmio_region_init(pdata);
807 afu_dma_region_init(pdata);
808 mutex_unlock(&pdata->lock);
810 return 0;
813 static int afu_dev_destroy(struct platform_device *pdev)
815 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
817 mutex_lock(&pdata->lock);
818 afu_mmio_region_destroy(pdata);
819 afu_dma_region_destroy(pdata);
820 dfl_fpga_pdata_set_private(pdata, NULL);
821 mutex_unlock(&pdata->lock);
823 return 0;
826 static int port_enable_set(struct platform_device *pdev, bool enable)
828 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
829 int ret = 0;
831 mutex_lock(&pdata->lock);
832 if (enable)
833 __afu_port_enable(pdev);
834 else
835 ret = __afu_port_disable(pdev);
836 mutex_unlock(&pdata->lock);
838 return ret;
841 static struct dfl_fpga_port_ops afu_port_ops = {
842 .name = DFL_FPGA_FEATURE_DEV_PORT,
843 .owner = THIS_MODULE,
844 .get_id = port_get_id,
845 .enable_set = port_enable_set,
848 static int afu_probe(struct platform_device *pdev)
850 int ret;
852 dev_dbg(&pdev->dev, "%s\n", __func__);
854 ret = afu_dev_init(pdev);
855 if (ret)
856 goto exit;
858 ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs);
859 if (ret)
860 goto dev_destroy;
862 ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE);
863 if (ret) {
864 dfl_fpga_dev_feature_uinit(pdev);
865 goto dev_destroy;
868 return 0;
870 dev_destroy:
871 afu_dev_destroy(pdev);
872 exit:
873 return ret;
876 static int afu_remove(struct platform_device *pdev)
878 dev_dbg(&pdev->dev, "%s\n", __func__);
880 dfl_fpga_dev_ops_unregister(pdev);
881 dfl_fpga_dev_feature_uinit(pdev);
882 afu_dev_destroy(pdev);
884 return 0;
887 static const struct attribute_group *afu_dev_groups[] = {
888 &port_hdr_group,
889 &port_afu_group,
890 &port_err_group,
891 NULL
894 static struct platform_driver afu_driver = {
895 .driver = {
896 .name = DFL_FPGA_FEATURE_DEV_PORT,
897 .dev_groups = afu_dev_groups,
899 .probe = afu_probe,
900 .remove = afu_remove,
903 static int __init afu_init(void)
905 int ret;
907 dfl_fpga_port_ops_add(&afu_port_ops);
909 ret = platform_driver_register(&afu_driver);
910 if (ret)
911 dfl_fpga_port_ops_del(&afu_port_ops);
913 return ret;
916 static void __exit afu_exit(void)
918 platform_driver_unregister(&afu_driver);
920 dfl_fpga_port_ops_del(&afu_port_ops);
923 module_init(afu_init);
924 module_exit(afu_exit);
926 MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver");
927 MODULE_AUTHOR("Intel Corporation");
928 MODULE_LICENSE("GPL v2");
929 MODULE_ALIAS("platform:dfl-port");