blob: d36b3e9f398428efcd7d9c481bd163640e398ee5 [file] [log] [blame]
Wu Hao1a1527c2018-06-30 08:53:30 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for FPGA Accelerated Function Unit (AFU)
4 *
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
6 *
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>
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
Wu Haoe4664c02018-06-30 08:53:32 +080019#include <linux/fpga-dfl.h>
Wu Hao1a1527c2018-06-30 08:53:30 +080020
21#include "dfl.h"
22
Wu Hao47c1b192018-06-30 08:53:31 +080023/**
24 * port_enable - enable a port
25 * @pdev: port platform device.
26 *
27 * Enable Port by clear the port soft reset bit, which is set by default.
28 * The User AFU is unable to respond to any MMIO access while in reset.
29 * port_enable function should only be used after port_disable
30 * function.
31 */
32static void port_enable(struct platform_device *pdev)
33{
34 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
35 void __iomem *base;
36 u64 v;
37
38 WARN_ON(!pdata->disable_count);
39
40 if (--pdata->disable_count != 0)
41 return;
42
43 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
44
45 /* Clear port soft reset */
46 v = readq(base + PORT_HDR_CTRL);
47 v &= ~PORT_CTRL_SFTRST;
48 writeq(v, base + PORT_HDR_CTRL);
49}
50
51#define RST_POLL_INVL 10 /* us */
52#define RST_POLL_TIMEOUT 1000 /* us */
53
54/**
55 * port_disable - disable a port
56 * @pdev: port platform device.
57 *
58 * Disable Port by setting the port soft reset bit, it puts the port into
59 * reset.
60 */
61static int port_disable(struct platform_device *pdev)
62{
63 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
64 void __iomem *base;
65 u64 v;
66
67 if (pdata->disable_count++ != 0)
68 return 0;
69
70 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
71
72 /* Set port soft reset */
73 v = readq(base + PORT_HDR_CTRL);
74 v |= PORT_CTRL_SFTRST;
75 writeq(v, base + PORT_HDR_CTRL);
76
77 /*
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.
81 */
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;
86 }
87
88 return 0;
89}
90
Wu Haoe4664c02018-06-30 08:53:32 +080091/*
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.
98 *
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).
102 */
103static int __port_reset(struct platform_device *pdev)
104{
105 int ret;
106
107 ret = port_disable(pdev);
108 if (!ret)
109 port_enable(pdev);
110
111 return ret;
112}
113
114static int port_reset(struct platform_device *pdev)
115{
116 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
117 int ret;
118
119 mutex_lock(&pdata->lock);
120 ret = __port_reset(pdev);
121 mutex_unlock(&pdata->lock);
122
123 return ret;
124}
125
Wu Hao47c1b192018-06-30 08:53:31 +0800126static int port_get_id(struct platform_device *pdev)
127{
128 void __iomem *base;
129
130 base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
131
132 return FIELD_GET(PORT_CAP_PORT_NUM, readq(base + PORT_HDR_CAP));
133}
134
Wu Haoe4664c02018-06-30 08:53:32 +0800135static ssize_t
136id_show(struct device *dev, struct device_attribute *attr, char *buf)
137{
138 int id = port_get_id(to_platform_device(dev));
139
140 return scnprintf(buf, PAGE_SIZE, "%d\n", id);
141}
142static DEVICE_ATTR_RO(id);
143
144static const struct attribute *port_hdr_attrs[] = {
145 &dev_attr_id.attr,
146 NULL,
147};
148
Wu Hao1a1527c2018-06-30 08:53:30 +0800149static int port_hdr_init(struct platform_device *pdev,
150 struct dfl_feature *feature)
151{
152 dev_dbg(&pdev->dev, "PORT HDR Init.\n");
153
Wu Haoe4664c02018-06-30 08:53:32 +0800154 port_reset(pdev);
155
156 return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
Wu Hao1a1527c2018-06-30 08:53:30 +0800157}
158
159static void port_hdr_uinit(struct platform_device *pdev,
160 struct dfl_feature *feature)
161{
162 dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
Wu Haoe4664c02018-06-30 08:53:32 +0800163
164 sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
165}
166
167static long
168port_hdr_ioctl(struct platform_device *pdev, struct dfl_feature *feature,
169 unsigned int cmd, unsigned long arg)
170{
171 long ret;
172
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;
183 }
184
185 return ret;
Wu Hao1a1527c2018-06-30 08:53:30 +0800186}
187
188static const struct dfl_feature_ops port_hdr_ops = {
189 .init = port_hdr_init,
190 .uinit = port_hdr_uinit,
Wu Haoe4664c02018-06-30 08:53:32 +0800191 .ioctl = port_hdr_ioctl,
Wu Hao1a1527c2018-06-30 08:53:30 +0800192};
193
194static struct dfl_feature_driver port_feature_drvs[] = {
195 {
196 .id = PORT_FEATURE_ID_HEADER,
197 .ops = &port_hdr_ops,
198 },
199 {
200 .ops = NULL,
201 }
202};
203
204static int afu_open(struct inode *inode, struct file *filp)
205{
206 struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
207 struct dfl_feature_platform_data *pdata;
208 int ret;
209
210 pdata = dev_get_platdata(&fdev->dev);
211 if (WARN_ON(!pdata))
212 return -ENODEV;
213
214 ret = dfl_feature_dev_use_begin(pdata);
215 if (ret)
216 return ret;
217
218 dev_dbg(&fdev->dev, "Device File Open\n");
219 filp->private_data = fdev;
220
221 return 0;
222}
223
224static int afu_release(struct inode *inode, struct file *filp)
225{
226 struct platform_device *pdev = filp->private_data;
227 struct dfl_feature_platform_data *pdata;
228
229 dev_dbg(&pdev->dev, "Device File Release\n");
230
231 pdata = dev_get_platdata(&pdev->dev);
232
Wu Haoe4664c02018-06-30 08:53:32 +0800233 port_reset(pdev);
Wu Hao1a1527c2018-06-30 08:53:30 +0800234 dfl_feature_dev_use_end(pdata);
235
236 return 0;
237}
238
239static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
240{
241 struct platform_device *pdev = filp->private_data;
242 struct dfl_feature_platform_data *pdata;
243 struct dfl_feature *f;
244 long ret;
245
246 dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
247
248 pdata = dev_get_platdata(&pdev->dev);
249
250 switch (cmd) {
251 default:
252 /*
253 * Let sub-feature's ioctl function to handle the cmd
254 * Sub-feature's ioctl returns -ENODEV when cmd is not
255 * handled in this sub feature, and returns 0 and other
256 * error code if cmd is handled.
257 */
258 dfl_fpga_dev_for_each_feature(pdata, f)
259 if (f->ops && f->ops->ioctl) {
260 ret = f->ops->ioctl(pdev, f, cmd, arg);
261 if (ret != -ENODEV)
262 return ret;
263 }
264 }
265
266 return -EINVAL;
267}
268
269static const struct file_operations afu_fops = {
270 .owner = THIS_MODULE,
271 .open = afu_open,
272 .release = afu_release,
273 .unlocked_ioctl = afu_ioctl,
274};
275
Wu Hao47c1b192018-06-30 08:53:31 +0800276static int port_enable_set(struct platform_device *pdev, bool enable)
277{
278 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
279 int ret = 0;
280
281 mutex_lock(&pdata->lock);
282 if (enable)
283 port_enable(pdev);
284 else
285 ret = port_disable(pdev);
286 mutex_unlock(&pdata->lock);
287
288 return ret;
289}
290
291static struct dfl_fpga_port_ops afu_port_ops = {
292 .name = DFL_FPGA_FEATURE_DEV_PORT,
293 .owner = THIS_MODULE,
294 .get_id = port_get_id,
295 .enable_set = port_enable_set,
296};
297
Wu Hao1a1527c2018-06-30 08:53:30 +0800298static int afu_probe(struct platform_device *pdev)
299{
300 int ret;
301
302 dev_dbg(&pdev->dev, "%s\n", __func__);
303
304 ret = dfl_fpga_dev_feature_init(pdev, port_feature_drvs);
305 if (ret)
306 return ret;
307
308 ret = dfl_fpga_dev_ops_register(pdev, &afu_fops, THIS_MODULE);
309 if (ret)
310 dfl_fpga_dev_feature_uinit(pdev);
311
312 return ret;
313}
314
315static int afu_remove(struct platform_device *pdev)
316{
317 dev_dbg(&pdev->dev, "%s\n", __func__);
318
319 dfl_fpga_dev_ops_unregister(pdev);
320 dfl_fpga_dev_feature_uinit(pdev);
321
322 return 0;
323}
324
325static struct platform_driver afu_driver = {
326 .driver = {
327 .name = DFL_FPGA_FEATURE_DEV_PORT,
328 },
329 .probe = afu_probe,
330 .remove = afu_remove,
331};
332
Wu Hao47c1b192018-06-30 08:53:31 +0800333static int __init afu_init(void)
334{
335 int ret;
336
337 dfl_fpga_port_ops_add(&afu_port_ops);
338
339 ret = platform_driver_register(&afu_driver);
340 if (ret)
341 dfl_fpga_port_ops_del(&afu_port_ops);
342
343 return ret;
344}
345
346static void __exit afu_exit(void)
347{
348 platform_driver_unregister(&afu_driver);
349
350 dfl_fpga_port_ops_del(&afu_port_ops);
351}
352
353module_init(afu_init);
354module_exit(afu_exit);
Wu Hao1a1527c2018-06-30 08:53:30 +0800355
356MODULE_DESCRIPTION("FPGA Accelerated Function Unit driver");
357MODULE_AUTHOR("Intel Corporation");
358MODULE_LICENSE("GPL v2");
359MODULE_ALIAS("platform:dfl-port");