1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for FPGA Accelerated Function Unit (AFU)
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
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>
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
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
);
41 WARN_ON(!pdata
->disable_count
);
43 if (--pdata
->disable_count
!= 0)
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 */
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
);
71 if (pdata
->disable_count
++ != 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");
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
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
)
112 ret
= __afu_port_disable(pdev
);
114 __afu_port_enable(pdev
);
119 static int port_reset(struct platform_device
*pdev
)
121 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
124 mutex_lock(&pdata
->lock
);
125 ret
= __port_reset(pdev
);
126 mutex_unlock(&pdata
->lock
);
131 static int port_get_id(struct platform_device
*pdev
)
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
));
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
);
150 ltr_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
152 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
));
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
);
174 if (kstrtobool(buf
, <r
))
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
);
188 static DEVICE_ATTR_RW(ltr
);
191 ap1_event_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
193 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
));
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
);
214 if (kstrtobool(buf
, &clear
) || !clear
)
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
);
225 static DEVICE_ATTR_RW(ap1_event
);
228 ap2_event_show(struct device
*dev
, struct device_attribute
*attr
,
231 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
));
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
);
252 if (kstrtobool(buf
, &clear
) || !clear
)
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
);
263 static DEVICE_ATTR_RW(ap2_event
);
266 power_state_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
268 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
);
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
;
290 if (kstrtou64(buf
, 0, &userclk_freq_cmd
))
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
);
301 static DEVICE_ATTR_WO(userclk_freqcmd
);
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
;
311 if (kstrtou64(buf
, 0, &userclk_freqcntr_cmd
))
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
);
322 static DEVICE_ATTR_WO(userclk_freqcntrcmd
);
325 userclk_freqsts_show(struct device
*dev
, struct device_attribute
*attr
,
328 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
);
343 userclk_freqcntrsts_show(struct device
*dev
, struct device_attribute
*attr
,
346 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
347 u64 userclk_freqcntrsts
;
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
[] = {
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
,
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
;
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
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
)
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
)
413 port_hdr_ioctl(struct platform_device
*pdev
, struct dfl_feature
*feature
,
414 unsigned int cmd
, unsigned long arg
)
419 case DFL_FPGA_PORT_RESET
:
421 ret
= port_reset(pdev
);
426 dev_dbg(&pdev
->dev
, "%x cmd not handled", cmd
);
433 static const struct dfl_feature_id port_hdr_id_table
[] = {
434 {.id
= PORT_FEATURE_ID_HEADER
,},
438 static const struct dfl_feature_ops port_hdr_ops
= {
439 .init
= port_hdr_init
,
440 .ioctl
= port_hdr_ioctl
,
444 afu_id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
446 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
);
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
,
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
480 if (!dfl_get_feature_by_id(dev
, PORT_FEATURE_ID_AFU
))
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
,},
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
,},
529 static const struct dfl_feature_ops port_stp_ops
= {
530 .init
= port_stp_init
,
534 port_uint_ioctl(struct platform_device
*pdev
, struct dfl_feature
*feature
,
535 unsigned int cmd
, unsigned long arg
)
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
);
543 dev_dbg(&pdev
->dev
, "%x cmd not handled", cmd
);
548 static const struct dfl_feature_id port_uint_id_table
[] = {
549 {.id
= PORT_FEATURE_ID_UINT
,},
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
,
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
;
589 pdata
= dev_get_platdata(&fdev
->dev
);
593 mutex_lock(&pdata
->lock
);
594 ret
= dfl_feature_dev_use_begin(pdata
, filp
->f_flags
& O_EXCL
);
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
);
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
);
623 afu_dma_region_destroy(pdata
);
625 mutex_unlock(&pdata
->lock
);
630 static long afu_ioctl_check_extension(struct dfl_feature_platform_data
*pdata
,
633 /* No extension support for now */
638 afu_ioctl_get_info(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
640 struct dfl_fpga_port_info info
;
644 minsz
= offsetofend(struct dfl_fpga_port_info
, num_umsgs
);
646 if (copy_from_user(&info
, arg
, minsz
))
649 if (info
.argsz
< minsz
)
652 mutex_lock(&pdata
->lock
);
653 afu
= dfl_fpga_pdata_get_private(pdata
);
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
)))
665 static long afu_ioctl_get_region_info(struct dfl_feature_platform_data
*pdata
,
668 struct dfl_fpga_port_region_info rinfo
;
669 struct dfl_afu_mmio_region region
;
673 minsz
= offsetofend(struct dfl_fpga_port_region_info
, offset
);
675 if (copy_from_user(&rinfo
, arg
, minsz
))
678 if (rinfo
.argsz
< minsz
|| rinfo
.padding
)
681 ret
= afu_mmio_region_get_by_index(pdata
, rinfo
.index
, ®ion
);
685 rinfo
.flags
= region
.flags
;
686 rinfo
.size
= region
.size
;
687 rinfo
.offset
= region
.offset
;
689 if (copy_to_user(arg
, &rinfo
, sizeof(rinfo
)))
696 afu_ioctl_dma_map(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
698 struct dfl_fpga_port_dma_map map
;
702 minsz
= offsetofend(struct dfl_fpga_port_dma_map
, iova
);
704 if (copy_from_user(&map
, arg
, minsz
))
707 if (map
.argsz
< minsz
|| map
.flags
)
710 ret
= afu_dma_map_region(pdata
, map
.user_addr
, map
.length
, &map
.iova
);
714 if (copy_to_user(arg
, &map
, sizeof(map
))) {
715 afu_dma_unmap_region(pdata
, map
.iova
);
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
);
728 afu_ioctl_dma_unmap(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
730 struct dfl_fpga_port_dma_unmap unmap
;
733 minsz
= offsetofend(struct dfl_fpga_port_dma_unmap
, iova
);
735 if (copy_from_user(&unmap
, arg
, minsz
))
738 if (unmap
.argsz
< minsz
|| unmap
.flags
)
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
;
751 dev_dbg(&pdev
->dev
, "%s cmd 0x%x\n", __func__
, cmd
);
753 pdata
= dev_get_platdata(&pdev
->dev
);
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
);
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
);
786 static const struct vm_operations_struct afu_vma_ops
= {
787 #ifdef CONFIG_HAVE_IOREMAP_PROT
788 .access
= generic_access_phys
,
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
;
801 if (!(vma
->vm_flags
& VM_SHARED
))
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
, ®ion
);
811 if (!(region
.flags
& DFL_PORT_REGION_MMAP
))
814 if ((vma
->vm_flags
& VM_READ
) && !(region
.flags
& DFL_PORT_REGION_READ
))
817 if ((vma
->vm_flags
& VM_WRITE
) &&
818 !(region
.flags
& DFL_PORT_REGION_WRITE
))
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
,
834 .release
= afu_release
,
835 .unlocked_ioctl
= afu_ioctl
,
839 static int afu_dev_init(struct platform_device
*pdev
)
841 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
844 afu
= devm_kzalloc(&pdev
->dev
, sizeof(*afu
), GFP_KERNEL
);
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
);
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
);
872 static int port_enable_set(struct platform_device
*pdev
, bool enable
)
874 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
877 mutex_lock(&pdata
->lock
);
879 __afu_port_enable(pdev
);
881 ret
= __afu_port_disable(pdev
);
882 mutex_unlock(&pdata
->lock
);
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
)
898 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
900 ret
= afu_dev_init(pdev
);
904 ret
= dfl_fpga_dev_feature_init(pdev
, port_feature_drvs
);
908 ret
= dfl_fpga_dev_ops_register(pdev
, &afu_fops
, THIS_MODULE
);
910 dfl_fpga_dev_feature_uinit(pdev
);
917 afu_dev_destroy(pdev
);
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
);
933 static const struct attribute_group
*afu_dev_groups
[] = {
940 static struct platform_driver afu_driver
= {
942 .name
= DFL_FPGA_FEATURE_DEV_PORT
,
943 .dev_groups
= afu_dev_groups
,
946 .remove
= afu_remove
,
949 static int __init
afu_init(void)
953 dfl_fpga_port_ops_add(&afu_port_ops
);
955 ret
= platform_driver_register(&afu_driver
);
957 dfl_fpga_port_ops_del(&afu_port_ops
);
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");