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 * 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
);
38 WARN_ON(!pdata
->disable_count
);
40 if (--pdata
->disable_count
!= 0)
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 */
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
61 static int port_disable(struct platform_device
*pdev
)
63 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
67 if (pdata
->disable_count
++ != 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");
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
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
)
107 ret
= port_disable(pdev
);
114 static int port_reset(struct platform_device
*pdev
)
116 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
119 mutex_lock(&pdata
->lock
);
120 ret
= __port_reset(pdev
);
121 mutex_unlock(&pdata
->lock
);
126 static int port_get_id(struct platform_device
*pdev
)
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
));
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
[] = {
149 static int port_hdr_init(struct platform_device
*pdev
,
150 struct dfl_feature
*feature
)
152 dev_dbg(&pdev
->dev
, "PORT HDR Init.\n");
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
);
168 port_hdr_ioctl(struct platform_device
*pdev
, struct dfl_feature
*feature
,
169 unsigned int cmd
, unsigned long arg
)
174 case DFL_FPGA_PORT_RESET
:
176 ret
= port_reset(pdev
);
181 dev_dbg(&pdev
->dev
, "%x cmd not handled", cmd
);
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
,
195 afu_id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
197 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(dev
);
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
);
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
,
222 static int port_afu_init(struct platform_device
*pdev
,
223 struct dfl_feature
*feature
)
225 struct resource
*res
= &pdev
->resource
[feature
->resource_index
];
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
);
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
,
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
;
273 pdata
= dev_get_platdata(&fdev
->dev
);
277 ret
= dfl_feature_dev_use_begin(pdata
);
281 dev_dbg(&fdev
->dev
, "Device File Open\n");
282 filp
->private_data
= fdev
;
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
);
298 afu_dma_region_destroy(pdata
);
299 mutex_unlock(&pdata
->lock
);
301 dfl_feature_dev_use_end(pdata
);
306 static long afu_ioctl_check_extension(struct dfl_feature_platform_data
*pdata
,
309 /* No extension support for now */
314 afu_ioctl_get_info(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
316 struct dfl_fpga_port_info info
;
320 minsz
= offsetofend(struct dfl_fpga_port_info
, num_umsgs
);
322 if (copy_from_user(&info
, arg
, minsz
))
325 if (info
.argsz
< minsz
)
328 mutex_lock(&pdata
->lock
);
329 afu
= dfl_fpga_pdata_get_private(pdata
);
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
)))
341 static long afu_ioctl_get_region_info(struct dfl_feature_platform_data
*pdata
,
344 struct dfl_fpga_port_region_info rinfo
;
345 struct dfl_afu_mmio_region region
;
349 minsz
= offsetofend(struct dfl_fpga_port_region_info
, offset
);
351 if (copy_from_user(&rinfo
, arg
, minsz
))
354 if (rinfo
.argsz
< minsz
|| rinfo
.padding
)
357 ret
= afu_mmio_region_get_by_index(pdata
, rinfo
.index
, ®ion
);
361 rinfo
.flags
= region
.flags
;
362 rinfo
.size
= region
.size
;
363 rinfo
.offset
= region
.offset
;
365 if (copy_to_user(arg
, &rinfo
, sizeof(rinfo
)))
372 afu_ioctl_dma_map(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
374 struct dfl_fpga_port_dma_map map
;
378 minsz
= offsetofend(struct dfl_fpga_port_dma_map
, iova
);
380 if (copy_from_user(&map
, arg
, minsz
))
383 if (map
.argsz
< minsz
|| map
.flags
)
386 ret
= afu_dma_map_region(pdata
, map
.user_addr
, map
.length
, &map
.iova
);
390 if (copy_to_user(arg
, &map
, sizeof(map
))) {
391 afu_dma_unmap_region(pdata
, map
.iova
);
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
);
404 afu_ioctl_dma_unmap(struct dfl_feature_platform_data
*pdata
, void __user
*arg
)
406 struct dfl_fpga_port_dma_unmap unmap
;
409 minsz
= offsetofend(struct dfl_fpga_port_dma_unmap
, iova
);
411 if (copy_from_user(&unmap
, arg
, minsz
))
414 if (unmap
.argsz
< minsz
|| unmap
.flags
)
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
;
427 dev_dbg(&pdev
->dev
, "%s cmd 0x%x\n", __func__
, cmd
);
429 pdata
= dev_get_platdata(&pdev
->dev
);
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
);
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
);
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
;
471 if (!(vma
->vm_flags
& VM_SHARED
))
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
, ®ion
);
481 if (!(region
.flags
& DFL_PORT_REGION_MMAP
))
484 if ((vma
->vm_flags
& VM_READ
) && !(region
.flags
& DFL_PORT_REGION_READ
))
487 if ((vma
->vm_flags
& VM_WRITE
) &&
488 !(region
.flags
& DFL_PORT_REGION_WRITE
))
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
,
501 .release
= afu_release
,
502 .unlocked_ioctl
= afu_ioctl
,
506 static int afu_dev_init(struct platform_device
*pdev
)
508 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
511 afu
= devm_kzalloc(&pdev
->dev
, sizeof(*afu
), GFP_KERNEL
);
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
);
526 static int afu_dev_destroy(struct platform_device
*pdev
)
528 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
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
);
541 static int port_enable_set(struct platform_device
*pdev
, bool enable
)
543 struct dfl_feature_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
546 mutex_lock(&pdata
->lock
);
550 ret
= port_disable(pdev
);
551 mutex_unlock(&pdata
->lock
);
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
)
567 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
569 ret
= afu_dev_init(pdev
);
573 ret
= dfl_fpga_dev_feature_init(pdev
, port_feature_drvs
);
577 ret
= dfl_fpga_dev_ops_register(pdev
, &afu_fops
, THIS_MODULE
);
579 dfl_fpga_dev_feature_uinit(pdev
);
586 afu_dev_destroy(pdev
);
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
);
602 static struct platform_driver afu_driver
= {
604 .name
= DFL_FPGA_FEATURE_DEV_PORT
,
607 .remove
= afu_remove
,
610 static int __init
afu_init(void)
614 dfl_fpga_port_ops_add(&afu_port_ops
);
616 ret
= platform_driver_register(&afu_driver
);
618 dfl_fpga_port_ops_del(&afu_port_ops
);
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");