Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / fpga / dfl-afu-main.c
blob753cda4b2568380ec9b9fed5f0ae8a0140b5c3db
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,
87 v & PORT_CTRL_SFTRST_ACK,
88 RST_POLL_INVL, RST_POLL_TIMEOUT)) {
89 dev_err(&pdev->dev, "timeout, fail to reset device\n");
90 return -ETIMEDOUT;
93 return 0;
97 * This function resets the FPGA Port and its accelerator (AFU) by function
98 * __port_disable and __port_enable (set port soft reset bit and then clear
99 * it). Userspace can do Port reset at any time, e.g. during DMA or Partial
100 * Reconfiguration. But it should never cause any system level issue, only
101 * functional failure (e.g. DMA or PR operation failure) and be recoverable
102 * from the failure.
104 * Note: the accelerator (AFU) is not accessible when its port is in reset
105 * (disabled). Any attempts on MMIO access to AFU while in reset, will
106 * result errors reported via port error reporting sub feature (if present).
108 static int __port_reset(struct platform_device *pdev)
110 int ret;
112 ret = __afu_port_disable(pdev);
113 if (!ret)
114 __afu_port_enable(pdev);
116 return ret;
119 static int port_reset(struct platform_device *pdev)
121 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
122 int ret;
124 mutex_lock(&pdata->lock);
125 ret = __port_reset(pdev);
126 mutex_unlock(&pdata->lock);
128 return ret;
131 static int port_get_id(struct platform_device *pdev)
133 void __iomem *base;
135 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
137 return FIELD_GET(PORT_CAP_PORT_NUM, readq(base + PORT_HDR_CAP));
140 static ssize_t
141 id_show(struct device *dev, struct device_attribute *attr, char *buf)
143 int id = port_get_id(to_platform_device(dev));
145 return scnprintf(buf, PAGE_SIZE, "%d\n", id);
147 static DEVICE_ATTR_RO(id);
149 static ssize_t
150 ltr_show(struct device *dev, struct device_attribute *attr, char *buf)
152 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
153 void __iomem *base;
154 u64 v;
156 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
158 mutex_lock(&pdata->lock);
159 v = readq(base + PORT_HDR_CTRL);
160 mutex_unlock(&pdata->lock);
162 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_CTRL_LATENCY, v));
165 static ssize_t
166 ltr_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
169 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
170 void __iomem *base;
171 bool ltr;
172 u64 v;
174 if (kstrtobool(buf, &ltr))
175 return -EINVAL;
177 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
179 mutex_lock(&pdata->lock);
180 v = readq(base + PORT_HDR_CTRL);
181 v &= ~PORT_CTRL_LATENCY;
182 v |= FIELD_PREP(PORT_CTRL_LATENCY, ltr ? 1 : 0);
183 writeq(v, base + PORT_HDR_CTRL);
184 mutex_unlock(&pdata->lock);
186 return count;
188 static DEVICE_ATTR_RW(ltr);
190 static ssize_t
191 ap1_event_show(struct device *dev, struct device_attribute *attr, char *buf)
193 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
194 void __iomem *base;
195 u64 v;
197 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
199 mutex_lock(&pdata->lock);
200 v = readq(base + PORT_HDR_STS);
201 mutex_unlock(&pdata->lock);
203 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_STS_AP1_EVT, v));
206 static ssize_t
207 ap1_event_store(struct device *dev, struct device_attribute *attr,
208 const char *buf, size_t count)
210 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
211 void __iomem *base;
212 bool clear;
214 if (kstrtobool(buf, &clear) || !clear)
215 return -EINVAL;
217 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
219 mutex_lock(&pdata->lock);
220 writeq(PORT_STS_AP1_EVT, base + PORT_HDR_STS);
221 mutex_unlock(&pdata->lock);
223 return count;
225 static DEVICE_ATTR_RW(ap1_event);
227 static ssize_t
228 ap2_event_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
231 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
232 void __iomem *base;
233 u64 v;
235 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
237 mutex_lock(&pdata->lock);
238 v = readq(base + PORT_HDR_STS);
239 mutex_unlock(&pdata->lock);
241 return sprintf(buf, "%x\n", (u8)FIELD_GET(PORT_STS_AP2_EVT, v));
244 static ssize_t
245 ap2_event_store(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
248 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
249 void __iomem *base;
250 bool clear;
252 if (kstrtobool(buf, &clear) || !clear)
253 return -EINVAL;
255 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
257 mutex_lock(&pdata->lock);
258 writeq(PORT_STS_AP2_EVT, base + PORT_HDR_STS);
259 mutex_unlock(&pdata->lock);
261 return count;
263 static DEVICE_ATTR_RW(ap2_event);
265 static ssize_t
266 power_state_show(struct device *dev, struct device_attribute *attr, char *buf)
268 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
269 void __iomem *base;
270 u64 v;
272 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
274 mutex_lock(&pdata->lock);
275 v = readq(base + PORT_HDR_STS);
276 mutex_unlock(&pdata->lock);
278 return sprintf(buf, "0x%x\n", (u8)FIELD_GET(PORT_STS_PWR_STATE, v));
280 static DEVICE_ATTR_RO(power_state);
282 static ssize_t
283 userclk_freqcmd_store(struct device *dev, struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
287 u64 userclk_freq_cmd;
288 void __iomem *base;
290 if (kstrtou64(buf, 0, &userclk_freq_cmd))
291 return -EINVAL;
293 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
295 mutex_lock(&pdata->lock);
296 writeq(userclk_freq_cmd, base + PORT_HDR_USRCLK_CMD0);
297 mutex_unlock(&pdata->lock);
299 return count;
301 static DEVICE_ATTR_WO(userclk_freqcmd);
303 static ssize_t
304 userclk_freqcntrcmd_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
307 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
308 u64 userclk_freqcntr_cmd;
309 void __iomem *base;
311 if (kstrtou64(buf, 0, &userclk_freqcntr_cmd))
312 return -EINVAL;
314 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
316 mutex_lock(&pdata->lock);
317 writeq(userclk_freqcntr_cmd, base + PORT_HDR_USRCLK_CMD1);
318 mutex_unlock(&pdata->lock);
320 return count;
322 static DEVICE_ATTR_WO(userclk_freqcntrcmd);
324 static ssize_t
325 userclk_freqsts_show(struct device *dev, struct device_attribute *attr,
326 char *buf)
328 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
329 u64 userclk_freqsts;
330 void __iomem *base;
332 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
334 mutex_lock(&pdata->lock);
335 userclk_freqsts = readq(base + PORT_HDR_USRCLK_STS0);
336 mutex_unlock(&pdata->lock);
338 return sprintf(buf, "0x%llx\n", (unsigned long long)userclk_freqsts);
340 static DEVICE_ATTR_RO(userclk_freqsts);
342 static ssize_t
343 userclk_freqcntrsts_show(struct device *dev, struct device_attribute *attr,
344 char *buf)
346 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
347 u64 userclk_freqcntrsts;
348 void __iomem *base;
350 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
352 mutex_lock(&pdata->lock);
353 userclk_freqcntrsts = readq(base + PORT_HDR_USRCLK_STS1);
354 mutex_unlock(&pdata->lock);
356 return sprintf(buf, "0x%llx\n",
357 (unsigned long long)userclk_freqcntrsts);
359 static DEVICE_ATTR_RO(userclk_freqcntrsts);
361 static struct attribute *port_hdr_attrs[] = {
362 &dev_attr_id.attr,
363 &dev_attr_ltr.attr,
364 &dev_attr_ap1_event.attr,
365 &dev_attr_ap2_event.attr,
366 &dev_attr_power_state.attr,
367 &dev_attr_userclk_freqcmd.attr,
368 &dev_attr_userclk_freqcntrcmd.attr,
369 &dev_attr_userclk_freqsts.attr,
370 &dev_attr_userclk_freqcntrsts.attr,
371 NULL,
374 static umode_t port_hdr_attrs_visible(struct kobject *kobj,
375 struct attribute *attr, int n)
377 struct device *dev = kobj_to_dev(kobj);
378 umode_t mode = attr->mode;
379 void __iomem *base;
381 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
383 if (dfl_feature_revision(base) > 0) {
385 * userclk sysfs interfaces are only visible in case port
386 * revision is 0, as hardware with revision >0 doesn't
387 * support this.
389 if (attr == &dev_attr_userclk_freqcmd.attr ||
390 attr == &dev_attr_userclk_freqcntrcmd.attr ||
391 attr == &dev_attr_userclk_freqsts.attr ||
392 attr == &dev_attr_userclk_freqcntrsts.attr)
393 mode = 0;
396 return mode;
399 static const struct attribute_group port_hdr_group = {
400 .attrs = port_hdr_attrs,
401 .is_visible = port_hdr_attrs_visible,
404 static int port_hdr_init(struct platform_device *pdev,
405 struct dfl_feature *feature)
407 port_reset(pdev);
409 return 0;
412 static long
413 port_hdr_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
414 unsigned int cmd, unsigned long arg)
416 long ret;
418 switch (cmd) {
419 case DFL_FPGA_PORT_RESET:
420 if (!arg)
421 ret = port_reset(pdev);
422 else
423 ret = -EINVAL;
424 break;
425 default:
426 dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
427 ret = -ENODEV;
430 return ret;
433 static const struct dfl_feature_id port_hdr_id_table[] = {
434 {.id = PORT_FEATURE_ID_HEADER,},
435 {0,}
438 static const struct dfl_feature_ops port_hdr_ops = {
439 .init = port_hdr_init,
440 .ioctl = port_hdr_ioctl,
443 static ssize_t
444 afu_id_show(struct device *dev, struct device_attribute *attr, char *buf)
446 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
447 void __iomem *base;
448 u64 guidl, guidh;
450 base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_AFU);
452 mutex_lock(&pdata->lock);
453 if (pdata->disable_count) {
454 mutex_unlock(&pdata->lock);
455 return -EBUSY;
458 guidl = readq(base + GUID_L);
459 guidh = readq(base + GUID_H);
460 mutex_unlock(&pdata->lock);
462 return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n", guidh, guidl);
464 static DEVICE_ATTR_RO(afu_id);
466 static struct attribute *port_afu_attrs[] = {
467 &dev_attr_afu_id.attr,
468 NULL
471 static umode_t port_afu_attrs_visible(struct kobject *kobj,
472 struct attribute *attr, int n)
474 struct device *dev = kobj_to_dev(kobj);
477 * sysfs entries are visible only if related private feature is
478 * enumerated.
480 if (!dfl_get_feature_by_id(dev, PORT_FEATURE_ID_AFU))
481 return 0;
483 return attr->mode;
486 static const struct attribute_group port_afu_group = {
487 .attrs = port_afu_attrs,
488 .is_visible = port_afu_attrs_visible,
491 static int port_afu_init(struct platform_device *pdev,
492 struct dfl_feature *feature)
494 struct resource *res = &pdev->resource[feature->resource_index];
496 return afu_mmio_region_add(dev_get_platdata(&pdev->dev),
497 DFL_PORT_REGION_INDEX_AFU,
498 resource_size(res), res->start,
499 DFL_PORT_REGION_MMAP | DFL_PORT_REGION_READ |
500 DFL_PORT_REGION_WRITE);
503 static const struct dfl_feature_id port_afu_id_table[] = {
504 {.id = PORT_FEATURE_ID_AFU,},
505 {0,}
508 static const struct dfl_feature_ops port_afu_ops = {
509 .init = port_afu_init,
512 static int port_stp_init(struct platform_device *pdev,
513 struct dfl_feature *feature)
515 struct resource *res = &pdev->resource[feature->resource_index];
517 return afu_mmio_region_add(dev_get_platdata(&pdev->dev),
518 DFL_PORT_REGION_INDEX_STP,
519 resource_size(res), res->start,
520 DFL_PORT_REGION_MMAP | DFL_PORT_REGION_READ |
521 DFL_PORT_REGION_WRITE);
524 static const struct dfl_feature_id port_stp_id_table[] = {
525 {.id = PORT_FEATURE_ID_STP,},
526 {0,}
529 static const struct dfl_feature_ops port_stp_ops = {
530 .init = port_stp_init,
533 static long
534 port_uint_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
535 unsigned int cmd, unsigned long arg)
537 switch (cmd) {
538 case DFL_FPGA_PORT_UINT_GET_IRQ_NUM:
539 return dfl_feature_ioctl_get_num_irqs(pdev, feature, arg);
540 case DFL_FPGA_PORT_UINT_SET_IRQ:
541 return dfl_feature_ioctl_set_irq(pdev, feature, arg);
542 default:
543 dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
544 return -ENODEV;
548 static const struct dfl_feature_id port_uint_id_table[] = {
549 {.id = PORT_FEATURE_ID_UINT,},
550 {0,}
553 static const struct dfl_feature_ops port_uint_ops = {
554 .ioctl = port_uint_ioctl,
557 static struct dfl_feature_driver port_feature_drvs[] = {
559 .id_table = port_hdr_id_table,
560 .ops = &port_hdr_ops,
563 .id_table = port_afu_id_table,
564 .ops = &port_afu_ops,
567 .id_table = port_err_id_table,
568 .ops = &port_err_ops,
571 .id_table = port_stp_id_table,
572 .ops = &port_stp_ops,
575 .id_table = port_uint_id_table,
576 .ops = &port_uint_ops,
579 .ops = NULL,
583 static int afu_open(struct inode *inode, struct file *filp)
585 struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
586 struct dfl_feature_platform_data *pdata;
587 int ret;
589 pdata = dev_get_platdata(&fdev->dev);
590 if (WARN_ON(!pdata))
591 return -ENODEV;
593 mutex_lock(&pdata->lock);
594 ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
595 if (!ret) {
596 dev_dbg(&fdev->dev, "Device File Opened %d Times\n",
597 dfl_feature_dev_use_count(pdata));
598 filp->private_data = fdev;
600 mutex_unlock(&pdata->lock);
602 return ret;
605 static int afu_release(struct inode *inode, struct file *filp)
607 struct platform_device *pdev = filp->private_data;
608 struct dfl_feature_platform_data *pdata;
609 struct dfl_feature *feature;
611 dev_dbg(&pdev->dev, "Device File Release\n");
613 pdata = dev_get_platdata(&pdev->dev);
615 mutex_lock(&pdata->lock);
616 dfl_feature_dev_use_end(pdata);
618 if (!dfl_feature_dev_use_count(pdata)) {
619 dfl_fpga_dev_for_each_feature(pdata, feature)
620 dfl_fpga_set_irq_triggers(feature, 0,
621 feature->nr_irqs, NULL);
622 __port_reset(pdev);
623 afu_dma_region_destroy(pdata);
625 mutex_unlock(&pdata->lock);
627 return 0;
630 static long afu_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
631 unsigned long arg)
633 /* No extension support for now */
634 return 0;
637 static long
638 afu_ioctl_get_info(struct dfl_feature_platform_data *pdata, void __user *arg)
640 struct dfl_fpga_port_info info;
641 struct dfl_afu *afu;
642 unsigned long minsz;
644 minsz = offsetofend(struct dfl_fpga_port_info, num_umsgs);
646 if (copy_from_user(&info, arg, minsz))
647 return -EFAULT;
649 if (info.argsz < minsz)
650 return -EINVAL;
652 mutex_lock(&pdata->lock);
653 afu = dfl_fpga_pdata_get_private(pdata);
654 info.flags = 0;
655 info.num_regions = afu->num_regions;
656 info.num_umsgs = afu->num_umsgs;
657 mutex_unlock(&pdata->lock);
659 if (copy_to_user(arg, &info, sizeof(info)))
660 return -EFAULT;
662 return 0;
665 static long afu_ioctl_get_region_info(struct dfl_feature_platform_data *pdata,
666 void __user *arg)
668 struct dfl_fpga_port_region_info rinfo;
669 struct dfl_afu_mmio_region region;
670 unsigned long minsz;
671 long ret;
673 minsz = offsetofend(struct dfl_fpga_port_region_info, offset);
675 if (copy_from_user(&rinfo, arg, minsz))
676 return -EFAULT;
678 if (rinfo.argsz < minsz || rinfo.padding)
679 return -EINVAL;
681 ret = afu_mmio_region_get_by_index(pdata, rinfo.index, &region);
682 if (ret)
683 return ret;
685 rinfo.flags = region.flags;
686 rinfo.size = region.size;
687 rinfo.offset = region.offset;
689 if (copy_to_user(arg, &rinfo, sizeof(rinfo)))
690 return -EFAULT;
692 return 0;
695 static long
696 afu_ioctl_dma_map(struct dfl_feature_platform_data *pdata, void __user *arg)
698 struct dfl_fpga_port_dma_map map;
699 unsigned long minsz;
700 long ret;
702 minsz = offsetofend(struct dfl_fpga_port_dma_map, iova);
704 if (copy_from_user(&map, arg, minsz))
705 return -EFAULT;
707 if (map.argsz < minsz || map.flags)
708 return -EINVAL;
710 ret = afu_dma_map_region(pdata, map.user_addr, map.length, &map.iova);
711 if (ret)
712 return ret;
714 if (copy_to_user(arg, &map, sizeof(map))) {
715 afu_dma_unmap_region(pdata, map.iova);
716 return -EFAULT;
719 dev_dbg(&pdata->dev->dev, "dma map: ua=%llx, len=%llx, iova=%llx\n",
720 (unsigned long long)map.user_addr,
721 (unsigned long long)map.length,
722 (unsigned long long)map.iova);
724 return 0;
727 static long
728 afu_ioctl_dma_unmap(struct dfl_feature_platform_data *pdata, void __user *arg)
730 struct dfl_fpga_port_dma_unmap unmap;
731 unsigned long minsz;
733 minsz = offsetofend(struct dfl_fpga_port_dma_unmap, iova);
735 if (copy_from_user(&unmap, arg, minsz))
736 return -EFAULT;
738 if (unmap.argsz < minsz || unmap.flags)
739 return -EINVAL;
741 return afu_dma_unmap_region(pdata, unmap.iova);
744 static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
746 struct platform_device *pdev = filp->private_data;
747 struct dfl_feature_platform_data *pdata;
748 struct dfl_feature *f;
749 long ret;
751 dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
753 pdata = dev_get_platdata(&pdev->dev);
755 switch (cmd) {
756 case DFL_FPGA_GET_API_VERSION:
757 return DFL_FPGA_API_VERSION;
758 case DFL_FPGA_CHECK_EXTENSION:
759 return afu_ioctl_check_extension(pdata, arg);
760 case DFL_FPGA_PORT_GET_INFO:
761 return afu_ioctl_get_info(pdata, (void __user *)arg);
762 case DFL_FPGA_PORT_GET_REGION_INFO:
763 return afu_ioctl_get_region_info(pdata, (void __user *)arg);
764 case DFL_FPGA_PORT_DMA_MAP:
765 return afu_ioctl_dma_map(pdata, (void __user *)arg);
766 case DFL_FPGA_PORT_DMA_UNMAP:
767 return afu_ioctl_dma_unmap(pdata, (void __user *)arg);
768 default:
770 * Let sub-feature's ioctl function to handle the cmd
771 * Sub-feature's ioctl returns -ENODEV when cmd is not
772 * handled in this sub feature, and returns 0 and other
773 * error code if cmd is handled.
775 dfl_fpga_dev_for_each_feature(pdata, f)
776 if (f->ops && f->ops->ioctl) {
777 ret = f->ops->ioctl(pdev, f, cmd, arg);
778 if (ret != -ENODEV)
779 return ret;
783 return -EINVAL;
786 static const struct vm_operations_struct afu_vma_ops = {
787 #ifdef CONFIG_HAVE_IOREMAP_PROT
788 .access = generic_access_phys,
789 #endif
792 static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
794 struct platform_device *pdev = filp->private_data;
795 struct dfl_feature_platform_data *pdata;
796 u64 size = vma->vm_end - vma->vm_start;
797 struct dfl_afu_mmio_region region;
798 u64 offset;
799 int ret;
801 if (!(vma->vm_flags & VM_SHARED))
802 return -EINVAL;
804 pdata = dev_get_platdata(&pdev->dev);
806 offset = vma->vm_pgoff << PAGE_SHIFT;
807 ret = afu_mmio_region_get_by_offset(pdata, offset, size, &region);
808 if (ret)
809 return ret;
811 if (!(region.flags & DFL_PORT_REGION_MMAP))
812 return -EINVAL;
814 if ((vma->vm_flags & VM_READ) && !(region.flags & DFL_PORT_REGION_READ))
815 return -EPERM;
817 if ((vma->vm_flags & VM_WRITE) &&
818 !(region.flags & DFL_PORT_REGION_WRITE))
819 return -EPERM;
821 /* Support debug access to the mapping */
822 vma->vm_ops = &afu_vma_ops;
824 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
826 return remap_pfn_range(vma, vma->vm_start,
827 (region.phys + (offset - region.offset)) >> PAGE_SHIFT,
828 size, vma->vm_page_prot);
831 static const struct file_operations afu_fops = {
832 .owner = THIS_MODULE,
833 .open = afu_open,
834 .release = afu_release,
835 .unlocked_ioctl = afu_ioctl,
836 .mmap = afu_mmap,
839 static int afu_dev_init(struct platform_device *pdev)
841 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
842 struct dfl_afu *afu;
844 afu = devm_kzalloc(&pdev->dev, sizeof(*afu), GFP_KERNEL);
845 if (!afu)
846 return -ENOMEM;
848 afu->pdata = pdata;
850 mutex_lock(&pdata->lock);
851 dfl_fpga_pdata_set_private(pdata, afu);
852 afu_mmio_region_init(pdata);
853 afu_dma_region_init(pdata);
854 mutex_unlock(&pdata->lock);
856 return 0;
859 static int afu_dev_destroy(struct platform_device *pdev)
861 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
863 mutex_lock(&pdata->lock);
864 afu_mmio_region_destroy(pdata);
865 afu_dma_region_destroy(pdata);
866 dfl_fpga_pdata_set_private(pdata, NULL);
867 mutex_unlock(&pdata->lock);
869 return 0;
872 static int port_enable_set(struct platform_device *pdev, bool enable)
874 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
875 int ret = 0;
877 mutex_lock(&pdata->lock);
878 if (enable)
879 __afu_port_enable(pdev);
880 else
881 ret = __afu_port_disable(pdev);
882 mutex_unlock(&pdata->lock);
884 return ret;
887 static struct dfl_fpga_port_ops afu_port_ops = {
888 .name = DFL_FPGA_FEATURE_DEV_PORT,
889 .owner = THIS_MODULE,
890 .get_id = port_get_id,
891 .enable_set = port_enable_set,
894 static int afu_probe(struct platform_device *pdev)
896 int ret;
898 dev_dbg(&pdev->dev, "%s\n", __func__);
900 ret = afu_dev_init(pdev);
901 if (ret)
902 goto exit;
904 ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs);
905 if (ret)
906 goto dev_destroy;
908 ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE);
909 if (ret) {
910 dfl_fpga_dev_feature_uinit(pdev);
911 goto dev_destroy;
914 return 0;
916 dev_destroy:
917 afu_dev_destroy(pdev);
918 exit:
919 return ret;
922 static int afu_remove(struct platform_device *pdev)
924 dev_dbg(&pdev->dev, "%s\n", __func__);
926 dfl_fpga_dev_ops_unregister(pdev);
927 dfl_fpga_dev_feature_uinit(pdev);
928 afu_dev_destroy(pdev);
930 return 0;
933 static const struct attribute_group *afu_dev_groups[] = {
934 &port_hdr_group,
935 &port_afu_group,
936 &port_err_group,
937 NULL
940 static struct platform_driver afu_driver = {
941 .driver = {
942 .name = DFL_FPGA_FEATURE_DEV_PORT,
943 .dev_groups = afu_dev_groups,
945 .probe = afu_probe,
946 .remove = afu_remove,
949 static int __init afu_init(void)
951 int ret;
953 dfl_fpga_port_ops_add(&afu_port_ops);
955 ret = platform_driver_register(&afu_driver);
956 if (ret)
957 dfl_fpga_port_ops_del(&afu_port_ops);
959 return ret;
962 static void __exit afu_exit(void)
964 platform_driver_unregister(&afu_driver);
966 dfl_fpga_port_ops_del(&afu_port_ops);
969 module_init(afu_init);
970 module_exit(afu_exit);
972 MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver");
973 MODULE_AUTHOR("Intel Corporation");
974 MODULE_LICENSE("GPL v2");
975 MODULE_ALIAS("platform:dfl-port");