aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/dw-xdata-pcie.c
blob: 257c25da5199600d55371429a0458a4a97239e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2020 Synopsys, Inc. and/or its affiliates.
 * Synopsys DesignWare xData driver
 *
 * Author: Gustavo Pimentel <gustavo.pimentel@synopsys.com>
 */

#include <linux/miscdevice.h>
#include <linux/bitfield.h>
#include <linux/pci-epf.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/bitops.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/pci.h>

#define DW_XDATA_DRIVER_NAME		"dw-xdata-pcie"

#define DW_XDATA_EP_MEM_OFFSET		0x8000000

static DEFINE_IDA(xdata_ida);

#define STATUS_DONE			BIT(0)

#define CONTROL_DOORBELL		BIT(0)
#define CONTROL_IS_WRITE		BIT(1)
#define CONTROL_LENGTH(a)		FIELD_PREP(GENMASK(13, 2), a)
#define CONTROL_PATTERN_INC		BIT(16)
#define CONTROL_NO_ADDR_INC		BIT(18)

#define XPERF_CONTROL_ENABLE		BIT(5)

#define BURST_REPEAT			BIT(31)
#define BURST_VALUE			0x1001

#define PATTERN_VALUE			0x0

struct dw_xdata_regs {
	u32 addr_lsb;					/* 0x000 */
	u32 addr_msb;					/* 0x004 */
	u32 burst_cnt;					/* 0x008 */
	u32 control;					/* 0x00c */
	u32 pattern;					/* 0x010 */
	u32 status;					/* 0x014 */
	u32 RAM_addr;					/* 0x018 */
	u32 RAM_port;					/* 0x01c */
	u32 _reserved0[14];				/* 0x020..0x054 */
	u32 perf_control;				/* 0x058 */
	u32 _reserved1[41];				/* 0x05c..0x0fc */
	u32 wr_cnt_lsb;					/* 0x100 */
	u32 wr_cnt_msb;					/* 0x104 */
	u32 rd_cnt_lsb;					/* 0x108 */
	u32 rd_cnt_msb;					/* 0x10c */
} __packed;

struct dw_xdata_region {
	phys_addr_t paddr;				/* physical address */
	void __iomem *vaddr;				/* virtual address */
};

struct dw_xdata {
	struct dw_xdata_region rg_region;		/* registers */
	size_t max_wr_len;				/* max wr xfer len */
	size_t max_rd_len;				/* max rd xfer len */
	struct mutex mutex;
	struct pci_dev *pdev;
	struct miscdevice misc_dev;
};

static inline struct dw_xdata_regs __iomem *__dw_regs(struct dw_xdata *dw)
{
	return dw->rg_region.vaddr;
}

static void dw_xdata_stop(struct dw_xdata *dw)
{
	u32 burst;

	mutex_lock(&dw->mutex);

	burst = readl(&(__dw_regs(dw)->burst_cnt));

	if (burst & BURST_REPEAT) {
		burst &= ~(u32)BURST_REPEAT;
		writel(burst, &(__dw_regs(dw)->burst_cnt));
	}

	mutex_unlock(&dw->mutex);
}

static void dw_xdata_start(struct dw_xdata *dw, bool write)
{
	struct device *dev = &dw->pdev->dev;
	u32 control, status;

	/* Stop first if xfer in progress */
	dw_xdata_stop(dw);

	mutex_lock(&dw->mutex);

	/* Clear status register */
	writel(0x0, &(__dw_regs(dw)->status));

	/* Burst count register set for continuous until stopped */
	writel(BURST_REPEAT | BURST_VALUE, &(__dw_regs(dw)->burst_cnt));

	/* Pattern register */
	writel(PATTERN_VALUE, &(__dw_regs(dw)->pattern));

	/* Control register */
	control = CONTROL_DOORBELL | CONTROL_PATTERN_INC | CONTROL_NO_ADDR_INC;
	if (write) {
		control |= CONTROL_IS_WRITE;
		control |= CONTROL_LENGTH(dw->max_wr_len);
	} else {
		control |= CONTROL_LENGTH(dw->max_rd_len);
	}
	writel(control, &(__dw_regs(dw)->control));

	/*
	 * The xData HW block needs about 100 ms to initiate the traffic
	 * generation according this HW block datasheet.
	 */
	usleep_range(100, 150);

	status = readl(&(__dw_regs(dw)->status));

	mutex_unlock(&dw->mutex);

	if (!(status & STATUS_DONE))
		dev_dbg(dev, "xData: started %s direction\n",
			write ? "write" : "read");
}

static void dw_xdata_perf_meas(struct dw_xdata *dw, u64 *data, bool write)
{
	if (write) {
		*data = readl(&(__dw_regs(dw)->wr_cnt_msb));
		*data <<= 32;
		*data |= readl(&(__dw_regs(dw)->wr_cnt_lsb));
	} else {
		*data = readl(&(__dw_regs(dw)->rd_cnt_msb));
		*data <<= 32;
		*data |= readl(&(__dw_regs(dw)->rd_cnt_lsb));
	}
}

static u64 dw_xdata_perf_diff(u64 *m1, u64 *m2, u64 time)
{
	u64 rate = (*m1 - *m2);

	rate *= (1000 * 1000 * 1000);
	rate >>= 20;
	rate = DIV_ROUND_CLOSEST_ULL(rate, time);

	return rate;
}

static void dw_xdata_perf(struct dw_xdata *dw, u64 *rate, bool write)
{
	struct device *dev = &dw->pdev->dev;
	u64 data[2], time[2], diff;

	mutex_lock(&dw->mutex);

	/* First acquisition of current count frames */
	writel(0x0, &(__dw_regs(dw)->perf_control));
	dw_xdata_perf_meas(dw, &data[0], write);
	time[0] = jiffies;
	writel((u32)XPERF_CONTROL_ENABLE, &(__dw_regs(dw)->perf_control));

	/*
	 * Wait 100ms between the 1st count frame acquisition and the 2nd
	 * count frame acquisition, in order to calculate the speed later
	 */
	mdelay(100);

	/* Second acquisition of current count frames */
	writel(0x0, &(__dw_regs(dw)->perf_control));
	dw_xdata_perf_meas(dw, &data[1], write);
	time[1] = jiffies;
	writel((u32)XPERF_CONTROL_ENABLE, &(__dw_regs(dw)->perf_control));

	/*
	 * Speed calculation
	 *
	 * rate = (2nd count frames - 1st count frames) / (time elapsed)
	 */
	diff = jiffies_to_nsecs(time[1] - time[0]);
	*rate = dw_xdata_perf_diff(&data[1], &data[0], diff);

	mutex_unlock(&dw->mutex);

	dev_dbg(dev, "xData: time=%llu us, %s=%llu MB/s\n",
		diff, write ? "write" : "read", *rate);
}

static struct dw_xdata *misc_dev_to_dw(struct miscdevice *misc_dev)
{
	return container_of(misc_dev, struct dw_xdata, misc_dev);
}

static ssize_t write_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	struct miscdevice *misc_dev = dev_get_drvdata(dev);
	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
	u64 rate;

	dw_xdata_perf(dw, &rate, true);

	return sysfs_emit(buf, "%llu\n", rate);
}

static ssize_t write_store(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t size)
{
	struct miscdevice *misc_dev = dev_get_drvdata(dev);
	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
	bool enabled;
	int ret;

	ret = kstrtobool(buf, &enabled);
	if (ret < 0)
		return ret;

	if (enabled) {
		dev_dbg(dev, "xData: requested write transfer\n");
		dw_xdata_start(dw, true);
	} else {
		dev_dbg(dev, "xData: requested stop transfer\n");
		dw_xdata_stop(dw);
	}

	return size;
}

static DEVICE_ATTR_RW(write);

static ssize_t read_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct miscdevice *misc_dev = dev_get_drvdata(dev);
	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
	u64 rate;

	dw_xdata_perf(dw, &rate, false);

	return sysfs_emit(buf, "%llu\n", rate);
}

static ssize_t read_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t size)
{
	struct miscdevice *misc_dev = dev_get_drvdata(dev);
	struct dw_xdata *dw = misc_dev_to_dw(misc_dev);
	bool enabled;
	int ret;

	ret = kstrtobool(buf, &enabled);
	if (ret < 0)
		return ret;

	if (enabled) {
		dev_dbg(dev, "xData: requested read transfer\n");
		dw_xdata_start(dw, false);
	} else {
		dev_dbg(dev, "xData: requested stop transfer\n");
		dw_xdata_stop(dw);
	}

	return size;
}

static DEVICE_ATTR_RW(read);

static struct attribute *xdata_attrs[] = {
	&dev_attr_write.attr,
	&dev_attr_read.attr,
	NULL,
};

ATTRIBUTE_GROUPS(xdata);

static int dw_xdata_pcie_probe(struct pci_dev *pdev,
			       const struct pci_device_id *pid)
{
	struct device *dev = &pdev->dev;
	struct dw_xdata *dw;
	char name[24];
	u64 addr;
	int err;
	int id;

	/* Enable PCI device */
	err = pcim_enable_device(pdev);
	if (err) {
		dev_err(dev, "enabling device failed\n");
		return err;
	}

	/* Mapping PCI BAR regions */
	err = pcim_iomap_regions(pdev, BIT(BAR_0), pci_name(pdev));
	if (err) {
		dev_err(dev, "xData BAR I/O remapping failed\n");
		return err;
	}

	pci_set_master(pdev);

	/* Allocate memory */
	dw = devm_kzalloc(dev, sizeof(*dw), GFP_KERNEL);
	if (!dw)
		return -ENOMEM;

	/* Data structure initialization */
	mutex_init(&dw->mutex);

	dw->rg_region.vaddr = pcim_iomap_table(pdev)[BAR_0];
	if (!dw->rg_region.vaddr)
		return -ENOMEM;

	dw->rg_region.paddr = pdev->resource[BAR_0].start;

	dw->max_wr_len = pcie_get_mps(pdev);
	dw->max_wr_len >>= 2;

	dw->max_rd_len = pcie_get_readrq(pdev);
	dw->max_rd_len >>= 2;

	dw->pdev = pdev;

	id = ida_simple_get(&xdata_ida, 0, 0, GFP_KERNEL);
	if (id < 0) {
		dev_err(dev, "xData: unable to get id\n");
		return id;
	}

	snprintf(name, sizeof(name), DW_XDATA_DRIVER_NAME ".%d", id);
	dw->misc_dev.name = kstrdup(name, GFP_KERNEL);
	if (!dw->misc_dev.name) {
		err = -ENOMEM;
		goto err_ida_remove;
	}

	dw->misc_dev.minor = MISC_DYNAMIC_MINOR;
	dw->misc_dev.parent = dev;
	dw->misc_dev.groups = xdata_groups;

	writel(0x0, &(__dw_regs(dw)->RAM_addr));
	writel(0x0, &(__dw_regs(dw)->RAM_port));

	addr = dw->rg_region.paddr + DW_XDATA_EP_MEM_OFFSET;
	writel(lower_32_bits(addr), &(__dw_regs(dw)->addr_lsb));
	writel(upper_32_bits(addr), &(__dw_regs(dw)->addr_msb));
	dev_dbg(dev, "xData: target address = 0x%.16llx\n", addr);

	dev_dbg(dev, "xData: wr_len = %zu, rd_len = %zu\n",
		dw->max_wr_len * 4, dw->max_rd_len * 4);

	/* Saving data structure reference */
	pci_set_drvdata(pdev, dw);

	/* Register misc device */
	err = misc_register(&dw->misc_dev);
	if (err) {
		dev_err(dev, "xData: failed to register device\n");
		goto err_kfree_name;
	}

	return 0;

err_kfree_name:
	kfree(dw->misc_dev.name);

err_ida_remove:
	ida_simple_remove(&xdata_ida, id);

	return err;
}

static void dw_xdata_pcie_remove(struct pci_dev *pdev)
{
	struct dw_xdata *dw = pci_get_drvdata(pdev);
	int id;

	if (sscanf(dw->misc_dev.name, DW_XDATA_DRIVER_NAME ".%d", &id) != 1)
		return;

	if (id < 0)
		return;

	dw_xdata_stop(dw);
	misc_deregister(&dw->misc_dev);
	kfree(dw->misc_dev.name);
	ida_simple_remove(&xdata_ida, id);
}

static const struct pci_device_id dw_xdata_pcie_id_table[] = {
	{ PCI_DEVICE_DATA(SYNOPSYS, EDDA, NULL) },
	{ }
};
MODULE_DEVICE_TABLE(pci, dw_xdata_pcie_id_table);

static struct pci_driver dw_xdata_pcie_driver = {
	.name		= DW_XDATA_DRIVER_NAME,
	.id_table	= dw_xdata_pcie_id_table,
	.probe		= dw_xdata_pcie_probe,
	.remove		= dw_xdata_pcie_remove,
};

module_pci_driver(dw_xdata_pcie_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Synopsys DesignWare xData PCIe driver");
MODULE_AUTHOR("Gustavo Pimentel <gustavo.pimentel@synopsys.com>");