]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/dma/dmatest.c
dmatest: run test via debugfs
[mirror_ubuntu-bionic-kernel.git] / drivers / dma / dmatest.c
CommitLineData
4a776f0a
HS
1/*
2 * DMA Engine test module
3 *
4 * Copyright (C) 2007 Atmel Corporation
851b7e16 5 * Copyright (C) 2013 Intel Corporation
4a776f0a
HS
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/delay.h>
b7f080cf 12#include <linux/dma-mapping.h>
4a776f0a 13#include <linux/dmaengine.h>
981ed70d 14#include <linux/freezer.h>
4a776f0a
HS
15#include <linux/init.h>
16#include <linux/kthread.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/random.h>
5a0e3ad6 20#include <linux/slab.h>
4a776f0a 21#include <linux/wait.h>
851b7e16
AS
22#include <linux/ctype.h>
23#include <linux/debugfs.h>
24#include <linux/uaccess.h>
25#include <linux/seq_file.h>
4a776f0a
HS
26
27static unsigned int test_buf_size = 16384;
28module_param(test_buf_size, uint, S_IRUGO);
29MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
30
06190d84 31static char test_channel[20];
4a776f0a
HS
32module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
33MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
34
06190d84 35static char test_device[20];
4a776f0a
HS
36module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
37MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
38
39static unsigned int threads_per_chan = 1;
40module_param(threads_per_chan, uint, S_IRUGO);
41MODULE_PARM_DESC(threads_per_chan,
42 "Number of threads to start per channel (default: 1)");
43
44static unsigned int max_channels;
45module_param(max_channels, uint, S_IRUGO);
33df8ca0 46MODULE_PARM_DESC(max_channels,
4a776f0a
HS
47 "Maximum number of channels to use (default: all)");
48
0a2ff57d
NF
49static unsigned int iterations;
50module_param(iterations, uint, S_IRUGO);
51MODULE_PARM_DESC(iterations,
52 "Iterations before stopping test (default: infinite)");
53
b54d5cb9
DW
54static unsigned int xor_sources = 3;
55module_param(xor_sources, uint, S_IRUGO);
56MODULE_PARM_DESC(xor_sources,
57 "Number of xor source buffers (default: 3)");
58
58691d64
DW
59static unsigned int pq_sources = 3;
60module_param(pq_sources, uint, S_IRUGO);
61MODULE_PARM_DESC(pq_sources,
62 "Number of p+q source buffers (default: 3)");
63
d42efe6b
VK
64static int timeout = 3000;
65module_param(timeout, uint, S_IRUGO);
85ee7a1d
JP
66MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
67 "Pass -1 for infinite timeout");
d42efe6b 68
4a776f0a
HS
69/*
70 * Initialization patterns. All bytes in the source buffer has bit 7
71 * set, all bytes in the destination buffer has bit 7 cleared.
72 *
73 * Bit 6 is set for all bytes which are to be copied by the DMA
74 * engine. Bit 5 is set for all bytes which are to be overwritten by
75 * the DMA engine.
76 *
77 * The remaining bits are the inverse of a counter which increments by
78 * one for each byte address.
79 */
80#define PATTERN_SRC 0x80
81#define PATTERN_DST 0x00
82#define PATTERN_COPY 0x40
83#define PATTERN_OVERWRITE 0x20
84#define PATTERN_COUNT_MASK 0x1f
85
e03e93a9
AS
86struct dmatest_info;
87
4a776f0a
HS
88struct dmatest_thread {
89 struct list_head node;
e03e93a9 90 struct dmatest_info *info;
4a776f0a
HS
91 struct task_struct *task;
92 struct dma_chan *chan;
b54d5cb9
DW
93 u8 **srcs;
94 u8 **dsts;
95 enum dma_transaction_type type;
4a776f0a
HS
96};
97
98struct dmatest_chan {
99 struct list_head node;
100 struct dma_chan *chan;
101 struct list_head threads;
102};
103
e03e93a9 104/**
15b8a8ea 105 * struct dmatest_params - test parameters.
e03e93a9
AS
106 * @buf_size: size of the memcpy test buffer
107 * @channel: bus ID of the channel to test
108 * @device: bus ID of the DMA Engine to test
109 * @threads_per_chan: number of threads to start per channel
110 * @max_channels: maximum number of channels to use
111 * @iterations: iterations before stopping test
112 * @xor_sources: number of xor source buffers
113 * @pq_sources: number of p+q source buffers
114 * @timeout: transfer timeout in msec, -1 for infinite timeout
115 */
15b8a8ea 116struct dmatest_params {
e03e93a9
AS
117 unsigned int buf_size;
118 char channel[20];
119 char device[20];
120 unsigned int threads_per_chan;
121 unsigned int max_channels;
122 unsigned int iterations;
123 unsigned int xor_sources;
124 unsigned int pq_sources;
125 int timeout;
15b8a8ea
AS
126};
127
128/**
129 * struct dmatest_info - test information.
130 * @params: test parameters
851b7e16 131 * @lock: access protection to the fields of this structure
15b8a8ea
AS
132 */
133struct dmatest_info {
134 /* Test parameters */
135 struct dmatest_params params;
838cc704
AS
136
137 /* Internal state */
138 struct list_head channels;
139 unsigned int nr_channels;
851b7e16
AS
140 struct mutex lock;
141
142 /* debugfs related stuff */
143 struct dentry *root;
144 struct dmatest_params dbgfs_params;
e03e93a9
AS
145};
146
147static struct dmatest_info test_info;
148
15b8a8ea 149static bool dmatest_match_channel(struct dmatest_params *params,
e03e93a9 150 struct dma_chan *chan)
4a776f0a 151{
15b8a8ea 152 if (params->channel[0] == '\0')
4a776f0a 153 return true;
15b8a8ea 154 return strcmp(dma_chan_name(chan), params->channel) == 0;
4a776f0a
HS
155}
156
15b8a8ea 157static bool dmatest_match_device(struct dmatest_params *params,
e03e93a9 158 struct dma_device *device)
4a776f0a 159{
15b8a8ea 160 if (params->device[0] == '\0')
4a776f0a 161 return true;
15b8a8ea 162 return strcmp(dev_name(device->dev), params->device) == 0;
4a776f0a
HS
163}
164
165static unsigned long dmatest_random(void)
166{
167 unsigned long buf;
168
169 get_random_bytes(&buf, sizeof(buf));
170 return buf;
171}
172
e03e93a9
AS
173static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
174 unsigned int buf_size)
4a776f0a
HS
175{
176 unsigned int i;
b54d5cb9
DW
177 u8 *buf;
178
179 for (; (buf = *bufs); bufs++) {
180 for (i = 0; i < start; i++)
181 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
182 for ( ; i < start + len; i++)
183 buf[i] = PATTERN_SRC | PATTERN_COPY
c019894e 184 | (~i & PATTERN_COUNT_MASK);
e03e93a9 185 for ( ; i < buf_size; i++)
b54d5cb9
DW
186 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
187 buf++;
188 }
4a776f0a
HS
189}
190
e03e93a9
AS
191static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
192 unsigned int buf_size)
4a776f0a
HS
193{
194 unsigned int i;
b54d5cb9
DW
195 u8 *buf;
196
197 for (; (buf = *bufs); bufs++) {
198 for (i = 0; i < start; i++)
199 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
200 for ( ; i < start + len; i++)
201 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
202 | (~i & PATTERN_COUNT_MASK);
e03e93a9 203 for ( ; i < buf_size; i++)
b54d5cb9
DW
204 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
205 }
4a776f0a
HS
206}
207
208static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
209 unsigned int counter, bool is_srcbuf)
210{
211 u8 diff = actual ^ pattern;
212 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
213 const char *thread_name = current->comm;
214
215 if (is_srcbuf)
216 pr_warning("%s: srcbuf[0x%x] overwritten!"
217 " Expected %02x, got %02x\n",
218 thread_name, index, expected, actual);
219 else if ((pattern & PATTERN_COPY)
220 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
221 pr_warning("%s: dstbuf[0x%x] not copied!"
222 " Expected %02x, got %02x\n",
223 thread_name, index, expected, actual);
224 else if (diff & PATTERN_SRC)
225 pr_warning("%s: dstbuf[0x%x] was copied!"
226 " Expected %02x, got %02x\n",
227 thread_name, index, expected, actual);
228 else
229 pr_warning("%s: dstbuf[0x%x] mismatch!"
230 " Expected %02x, got %02x\n",
231 thread_name, index, expected, actual);
232}
233
b54d5cb9 234static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
4a776f0a
HS
235 unsigned int end, unsigned int counter, u8 pattern,
236 bool is_srcbuf)
237{
238 unsigned int i;
239 unsigned int error_count = 0;
240 u8 actual;
b54d5cb9
DW
241 u8 expected;
242 u8 *buf;
243 unsigned int counter_orig = counter;
244
245 for (; (buf = *bufs); bufs++) {
246 counter = counter_orig;
247 for (i = start; i < end; i++) {
248 actual = buf[i];
249 expected = pattern | (~counter & PATTERN_COUNT_MASK);
250 if (actual != expected) {
251 if (error_count < 32)
252 dmatest_mismatch(actual, pattern, i,
253 counter, is_srcbuf);
254 error_count++;
255 }
256 counter++;
4a776f0a 257 }
4a776f0a
HS
258 }
259
260 if (error_count > 32)
261 pr_warning("%s: %u errors suppressed\n",
262 current->comm, error_count - 32);
263
264 return error_count;
265}
266
adfa543e
TH
267/* poor man's completion - we want to use wait_event_freezable() on it */
268struct dmatest_done {
269 bool done;
270 wait_queue_head_t *wait;
271};
272
273static void dmatest_callback(void *arg)
e44e0aa3 274{
adfa543e
TH
275 struct dmatest_done *done = arg;
276
277 done->done = true;
278 wake_up_all(done->wait);
e44e0aa3
DW
279}
280
632fd283
AS
281static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
282 unsigned int count)
283{
284 while (count--)
285 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
286}
287
288static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
289 unsigned int count)
290{
291 while (count--)
292 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
293}
294
8be9e32b
AM
295static unsigned int min_odd(unsigned int x, unsigned int y)
296{
297 unsigned int val = min(x, y);
298
299 return val % 2 ? val : val - 1;
300}
301
4a776f0a
HS
302/*
303 * This function repeatedly tests DMA transfers of various lengths and
b54d5cb9
DW
304 * offsets for a given operation type until it is told to exit by
305 * kthread_stop(). There may be multiple threads running this function
306 * in parallel for a single channel, and there may be multiple channels
307 * being tested in parallel.
4a776f0a
HS
308 *
309 * Before each test, the source and destination buffer is initialized
310 * with a known pattern. This pattern is different depending on
311 * whether it's in an area which is supposed to be copied or
312 * overwritten, and different in the source and destination buffers.
313 * So if the DMA engine doesn't copy exactly what we tell it to copy,
314 * we'll notice.
315 */
316static int dmatest_func(void *data)
317{
adfa543e 318 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
4a776f0a 319 struct dmatest_thread *thread = data;
adfa543e 320 struct dmatest_done done = { .wait = &done_wait };
e03e93a9 321 struct dmatest_info *info;
15b8a8ea 322 struct dmatest_params *params;
4a776f0a 323 struct dma_chan *chan;
8be9e32b 324 struct dma_device *dev;
4a776f0a
HS
325 const char *thread_name;
326 unsigned int src_off, dst_off, len;
327 unsigned int error_count;
328 unsigned int failed_tests = 0;
329 unsigned int total_tests = 0;
330 dma_cookie_t cookie;
331 enum dma_status status;
b54d5cb9 332 enum dma_ctrl_flags flags;
945b5af3 333 u8 *pq_coefs = NULL;
4a776f0a 334 int ret;
b54d5cb9
DW
335 int src_cnt;
336 int dst_cnt;
337 int i;
4a776f0a
HS
338
339 thread_name = current->comm;
adfa543e 340 set_freezable();
4a776f0a
HS
341
342 ret = -ENOMEM;
4a776f0a
HS
343
344 smp_rmb();
e03e93a9 345 info = thread->info;
15b8a8ea 346 params = &info->params;
4a776f0a 347 chan = thread->chan;
8be9e32b 348 dev = chan->device;
b54d5cb9
DW
349 if (thread->type == DMA_MEMCPY)
350 src_cnt = dst_cnt = 1;
351 else if (thread->type == DMA_XOR) {
8be9e32b 352 /* force odd to ensure dst = src */
15b8a8ea 353 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
b54d5cb9 354 dst_cnt = 1;
58691d64 355 } else if (thread->type == DMA_PQ) {
8be9e32b 356 /* force odd to ensure dst = src */
15b8a8ea 357 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
58691d64 358 dst_cnt = 2;
945b5af3 359
15b8a8ea 360 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
945b5af3
AS
361 if (!pq_coefs)
362 goto err_thread_type;
363
94de648d 364 for (i = 0; i < src_cnt; i++)
58691d64 365 pq_coefs[i] = 1;
b54d5cb9 366 } else
945b5af3 367 goto err_thread_type;
b54d5cb9
DW
368
369 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
370 if (!thread->srcs)
371 goto err_srcs;
372 for (i = 0; i < src_cnt; i++) {
15b8a8ea 373 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
b54d5cb9
DW
374 if (!thread->srcs[i])
375 goto err_srcbuf;
376 }
377 thread->srcs[i] = NULL;
378
379 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
380 if (!thread->dsts)
381 goto err_dsts;
382 for (i = 0; i < dst_cnt; i++) {
15b8a8ea 383 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
b54d5cb9
DW
384 if (!thread->dsts[i])
385 goto err_dstbuf;
386 }
387 thread->dsts[i] = NULL;
388
e44e0aa3
DW
389 set_user_nice(current, 10);
390
b203bd3f
IS
391 /*
392 * src buffers are freed by the DMAEngine code with dma_unmap_single()
393 * dst buffers are freed by ourselves below
394 */
395 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
396 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
4a776f0a 397
0a2ff57d 398 while (!kthread_should_stop()
15b8a8ea 399 && !(params->iterations && total_tests >= params->iterations)) {
b54d5cb9
DW
400 struct dma_async_tx_descriptor *tx = NULL;
401 dma_addr_t dma_srcs[src_cnt];
402 dma_addr_t dma_dsts[dst_cnt];
83544ae9 403 u8 align = 0;
d86be86e 404
4a776f0a
HS
405 total_tests++;
406
83544ae9
DW
407 /* honor alignment restrictions */
408 if (thread->type == DMA_MEMCPY)
409 align = dev->copy_align;
410 else if (thread->type == DMA_XOR)
411 align = dev->xor_align;
412 else if (thread->type == DMA_PQ)
413 align = dev->pq_align;
414
15b8a8ea 415 if (1 << align > params->buf_size) {
cfe4f275 416 pr_err("%u-byte buffer too small for %d-byte alignment\n",
15b8a8ea 417 params->buf_size, 1 << align);
cfe4f275
GL
418 break;
419 }
420
15b8a8ea 421 len = dmatest_random() % params->buf_size + 1;
83544ae9 422 len = (len >> align) << align;
cfe4f275
GL
423 if (!len)
424 len = 1 << align;
15b8a8ea
AS
425 src_off = dmatest_random() % (params->buf_size - len + 1);
426 dst_off = dmatest_random() % (params->buf_size - len + 1);
cfe4f275 427
83544ae9
DW
428 src_off = (src_off >> align) << align;
429 dst_off = (dst_off >> align) << align;
430
15b8a8ea
AS
431 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
432 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
4a776f0a 433
b54d5cb9
DW
434 for (i = 0; i < src_cnt; i++) {
435 u8 *buf = thread->srcs[i] + src_off;
436
437 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
438 DMA_TO_DEVICE);
afde3be1
AS
439 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
440 if (ret) {
441 unmap_src(dev->dev, dma_srcs, len, i);
442 pr_warn("%s: #%u: mapping error %d with "
443 "src_off=0x%x len=0x%x\n",
444 thread_name, total_tests - 1, ret,
445 src_off, len);
446 failed_tests++;
447 continue;
448 }
b54d5cb9 449 }
d86be86e 450 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
b54d5cb9
DW
451 for (i = 0; i < dst_cnt; i++) {
452 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
15b8a8ea 453 params->buf_size,
b54d5cb9 454 DMA_BIDIRECTIONAL);
afde3be1
AS
455 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
456 if (ret) {
457 unmap_src(dev->dev, dma_srcs, len, src_cnt);
15b8a8ea
AS
458 unmap_dst(dev->dev, dma_dsts, params->buf_size,
459 i);
afde3be1
AS
460 pr_warn("%s: #%u: mapping error %d with "
461 "dst_off=0x%x len=0x%x\n",
462 thread_name, total_tests - 1, ret,
15b8a8ea 463 dst_off, params->buf_size);
afde3be1
AS
464 failed_tests++;
465 continue;
466 }
b54d5cb9
DW
467 }
468
469 if (thread->type == DMA_MEMCPY)
470 tx = dev->device_prep_dma_memcpy(chan,
471 dma_dsts[0] + dst_off,
472 dma_srcs[0], len,
473 flags);
474 else if (thread->type == DMA_XOR)
475 tx = dev->device_prep_dma_xor(chan,
476 dma_dsts[0] + dst_off,
67b9124f 477 dma_srcs, src_cnt,
b54d5cb9 478 len, flags);
58691d64
DW
479 else if (thread->type == DMA_PQ) {
480 dma_addr_t dma_pq[dst_cnt];
481
482 for (i = 0; i < dst_cnt; i++)
483 dma_pq[i] = dma_dsts[i] + dst_off;
484 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
94de648d 485 src_cnt, pq_coefs,
58691d64
DW
486 len, flags);
487 }
d86be86e 488
d86be86e 489 if (!tx) {
632fd283 490 unmap_src(dev->dev, dma_srcs, len, src_cnt);
15b8a8ea
AS
491 unmap_dst(dev->dev, dma_dsts, params->buf_size,
492 dst_cnt);
d86be86e
AN
493 pr_warning("%s: #%u: prep error with src_off=0x%x "
494 "dst_off=0x%x len=0x%x\n",
495 thread_name, total_tests - 1,
496 src_off, dst_off, len);
497 msleep(100);
498 failed_tests++;
499 continue;
500 }
e44e0aa3 501
adfa543e 502 done.done = false;
e44e0aa3 503 tx->callback = dmatest_callback;
adfa543e 504 tx->callback_param = &done;
d86be86e
AN
505 cookie = tx->tx_submit(tx);
506
4a776f0a
HS
507 if (dma_submit_error(cookie)) {
508 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
509 "dst_off=0x%x len=0x%x\n",
510 thread_name, total_tests - 1, cookie,
511 src_off, dst_off, len);
512 msleep(100);
513 failed_tests++;
514 continue;
515 }
b54d5cb9 516 dma_async_issue_pending(chan);
4a776f0a 517
77101ce5
AS
518 wait_event_freezable_timeout(done_wait,
519 done.done || kthread_should_stop(),
15b8a8ea 520 msecs_to_jiffies(params->timeout));
981ed70d 521
e44e0aa3 522 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
4a776f0a 523
adfa543e
TH
524 if (!done.done) {
525 /*
526 * We're leaving the timed out dma operation with
527 * dangling pointer to done_wait. To make this
528 * correct, we'll need to allocate wait_done for
529 * each test iteration and perform "who's gonna
530 * free it this time?" dancing. For now, just
531 * leave it dangling.
532 */
e44e0aa3
DW
533 pr_warning("%s: #%u: test timed out\n",
534 thread_name, total_tests - 1);
535 failed_tests++;
536 continue;
537 } else if (status != DMA_SUCCESS) {
538 pr_warning("%s: #%u: got completion callback,"
539 " but status is \'%s\'\n",
540 thread_name, total_tests - 1,
541 status == DMA_ERROR ? "error" : "in progress");
4a776f0a
HS
542 failed_tests++;
543 continue;
544 }
e44e0aa3 545
d86be86e 546 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
15b8a8ea 547 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
4a776f0a
HS
548
549 error_count = 0;
550
551 pr_debug("%s: verifying source buffer...\n", thread_name);
b54d5cb9 552 error_count += dmatest_verify(thread->srcs, 0, src_off,
4a776f0a 553 0, PATTERN_SRC, true);
b54d5cb9 554 error_count += dmatest_verify(thread->srcs, src_off,
4a776f0a
HS
555 src_off + len, src_off,
556 PATTERN_SRC | PATTERN_COPY, true);
b54d5cb9 557 error_count += dmatest_verify(thread->srcs, src_off + len,
15b8a8ea 558 params->buf_size, src_off + len,
4a776f0a
HS
559 PATTERN_SRC, true);
560
561 pr_debug("%s: verifying dest buffer...\n",
562 thread->task->comm);
b54d5cb9 563 error_count += dmatest_verify(thread->dsts, 0, dst_off,
4a776f0a 564 0, PATTERN_DST, false);
b54d5cb9 565 error_count += dmatest_verify(thread->dsts, dst_off,
4a776f0a
HS
566 dst_off + len, src_off,
567 PATTERN_SRC | PATTERN_COPY, false);
b54d5cb9 568 error_count += dmatest_verify(thread->dsts, dst_off + len,
15b8a8ea 569 params->buf_size, dst_off + len,
4a776f0a
HS
570 PATTERN_DST, false);
571
572 if (error_count) {
573 pr_warning("%s: #%u: %u errors with "
574 "src_off=0x%x dst_off=0x%x len=0x%x\n",
575 thread_name, total_tests - 1, error_count,
576 src_off, dst_off, len);
577 failed_tests++;
578 } else {
579 pr_debug("%s: #%u: No errors with "
580 "src_off=0x%x dst_off=0x%x len=0x%x\n",
581 thread_name, total_tests - 1,
582 src_off, dst_off, len);
583 }
584 }
585
586 ret = 0;
b54d5cb9
DW
587 for (i = 0; thread->dsts[i]; i++)
588 kfree(thread->dsts[i]);
4a776f0a 589err_dstbuf:
b54d5cb9
DW
590 kfree(thread->dsts);
591err_dsts:
592 for (i = 0; thread->srcs[i]; i++)
593 kfree(thread->srcs[i]);
4a776f0a 594err_srcbuf:
b54d5cb9
DW
595 kfree(thread->srcs);
596err_srcs:
945b5af3
AS
597 kfree(pq_coefs);
598err_thread_type:
4a776f0a
HS
599 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
600 thread_name, total_tests, failed_tests, ret);
0a2ff57d 601
9704efaa 602 /* terminate all transfers on specified channels */
5e034f7b
SH
603 if (ret)
604 dmaengine_terminate_all(chan);
605
15b8a8ea 606 if (params->iterations > 0)
0a2ff57d 607 while (!kthread_should_stop()) {
b953df7c 608 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
0a2ff57d
NF
609 interruptible_sleep_on(&wait_dmatest_exit);
610 }
611
4a776f0a
HS
612 return ret;
613}
614
615static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
616{
617 struct dmatest_thread *thread;
618 struct dmatest_thread *_thread;
619 int ret;
620
621 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
622 ret = kthread_stop(thread->task);
623 pr_debug("dmatest: thread %s exited with status %d\n",
624 thread->task->comm, ret);
625 list_del(&thread->node);
626 kfree(thread);
627 }
9704efaa
VK
628
629 /* terminate all transfers on specified channels */
944ea4dd 630 dmaengine_terminate_all(dtc->chan);
9704efaa 631
4a776f0a
HS
632 kfree(dtc);
633}
634
e03e93a9
AS
635static int dmatest_add_threads(struct dmatest_info *info,
636 struct dmatest_chan *dtc, enum dma_transaction_type type)
4a776f0a 637{
15b8a8ea 638 struct dmatest_params *params = &info->params;
b54d5cb9
DW
639 struct dmatest_thread *thread;
640 struct dma_chan *chan = dtc->chan;
641 char *op;
642 unsigned int i;
4a776f0a 643
b54d5cb9
DW
644 if (type == DMA_MEMCPY)
645 op = "copy";
646 else if (type == DMA_XOR)
647 op = "xor";
58691d64
DW
648 else if (type == DMA_PQ)
649 op = "pq";
b54d5cb9
DW
650 else
651 return -EINVAL;
4a776f0a 652
15b8a8ea 653 for (i = 0; i < params->threads_per_chan; i++) {
4a776f0a
HS
654 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
655 if (!thread) {
b54d5cb9
DW
656 pr_warning("dmatest: No memory for %s-%s%u\n",
657 dma_chan_name(chan), op, i);
658
4a776f0a
HS
659 break;
660 }
e03e93a9 661 thread->info = info;
4a776f0a 662 thread->chan = dtc->chan;
b54d5cb9 663 thread->type = type;
4a776f0a 664 smp_wmb();
b54d5cb9
DW
665 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
666 dma_chan_name(chan), op, i);
4a776f0a 667 if (IS_ERR(thread->task)) {
b54d5cb9
DW
668 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
669 dma_chan_name(chan), op, i);
4a776f0a
HS
670 kfree(thread);
671 break;
672 }
673
674 /* srcbuf and dstbuf are allocated by the thread itself */
675
676 list_add_tail(&thread->node, &dtc->threads);
677 }
678
b54d5cb9
DW
679 return i;
680}
681
e03e93a9
AS
682static int dmatest_add_channel(struct dmatest_info *info,
683 struct dma_chan *chan)
b54d5cb9
DW
684{
685 struct dmatest_chan *dtc;
686 struct dma_device *dma_dev = chan->device;
687 unsigned int thread_count = 0;
b9033e68 688 int cnt;
b54d5cb9
DW
689
690 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
691 if (!dtc) {
692 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
693 return -ENOMEM;
694 }
695
696 dtc->chan = chan;
697 INIT_LIST_HEAD(&dtc->threads);
698
699 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
e03e93a9 700 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
f1aef8b6 701 thread_count += cnt > 0 ? cnt : 0;
b54d5cb9
DW
702 }
703 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
e03e93a9 704 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
f1aef8b6 705 thread_count += cnt > 0 ? cnt : 0;
b54d5cb9 706 }
58691d64 707 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
e03e93a9 708 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
d07a74a5 709 thread_count += cnt > 0 ? cnt : 0;
58691d64 710 }
b54d5cb9
DW
711
712 pr_info("dmatest: Started %u threads using %s\n",
713 thread_count, dma_chan_name(chan));
4a776f0a 714
838cc704
AS
715 list_add_tail(&dtc->node, &info->channels);
716 info->nr_channels++;
4a776f0a 717
33df8ca0 718 return 0;
4a776f0a
HS
719}
720
7dd60251 721static bool filter(struct dma_chan *chan, void *param)
4a776f0a 722{
15b8a8ea 723 struct dmatest_params *params = param;
e03e93a9 724
15b8a8ea
AS
725 if (!dmatest_match_channel(params, chan) ||
726 !dmatest_match_device(params, chan->device))
7dd60251 727 return false;
33df8ca0 728 else
7dd60251 729 return true;
4a776f0a
HS
730}
731
851b7e16 732static int __run_threaded_test(struct dmatest_info *info)
4a776f0a 733{
33df8ca0
DW
734 dma_cap_mask_t mask;
735 struct dma_chan *chan;
15b8a8ea 736 struct dmatest_params *params = &info->params;
33df8ca0
DW
737 int err = 0;
738
739 dma_cap_zero(mask);
740 dma_cap_set(DMA_MEMCPY, mask);
741 for (;;) {
15b8a8ea 742 chan = dma_request_channel(mask, filter, params);
33df8ca0 743 if (chan) {
e03e93a9 744 err = dmatest_add_channel(info, chan);
c56c81ab 745 if (err) {
33df8ca0
DW
746 dma_release_channel(chan);
747 break; /* add_channel failed, punt */
748 }
749 } else
750 break; /* no more channels available */
15b8a8ea
AS
751 if (params->max_channels &&
752 info->nr_channels >= params->max_channels)
33df8ca0
DW
753 break; /* we have all we need */
754 }
33df8ca0 755 return err;
4a776f0a 756}
4a776f0a 757
851b7e16
AS
758#ifndef MODULE
759static int run_threaded_test(struct dmatest_info *info)
760{
761 int ret;
762
763 mutex_lock(&info->lock);
764 ret = __run_threaded_test(info);
765 mutex_unlock(&info->lock);
766 return ret;
767}
768#endif
769
770static void __stop_threaded_test(struct dmatest_info *info)
4a776f0a 771{
33df8ca0 772 struct dmatest_chan *dtc, *_dtc;
7cbd4877 773 struct dma_chan *chan;
33df8ca0 774
838cc704 775 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
33df8ca0 776 list_del(&dtc->node);
7cbd4877 777 chan = dtc->chan;
33df8ca0 778 dmatest_cleanup_channel(dtc);
838cc704 779 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
7cbd4877 780 dma_release_channel(chan);
33df8ca0 781 }
838cc704
AS
782
783 info->nr_channels = 0;
4a776f0a 784}
e03e93a9 785
851b7e16
AS
786static void stop_threaded_test(struct dmatest_info *info)
787{
788 mutex_lock(&info->lock);
789 __stop_threaded_test(info);
790 mutex_unlock(&info->lock);
791}
792
793static int __restart_threaded_test(struct dmatest_info *info, bool run)
794{
795 struct dmatest_params *params = &info->params;
796 int ret;
797
798 /* Stop any running test first */
799 __stop_threaded_test(info);
800
801 if (run == false)
802 return 0;
803
804 /* Copy test parameters */
805 memcpy(params, &info->dbgfs_params, sizeof(*params));
806
807 /* Run test with new parameters */
808 ret = __run_threaded_test(info);
809 if (ret) {
810 __stop_threaded_test(info);
811 pr_err("dmatest: Can't run test\n");
812 }
813
814 return ret;
815}
816
817static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos,
818 const void __user *from, size_t count)
819{
820 char tmp[20];
821 ssize_t len;
822
823 len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count);
824 if (len >= 0) {
825 tmp[len] = '\0';
826 strlcpy(to, strim(tmp), available);
827 }
828
829 return len;
830}
831
832static ssize_t dtf_read_channel(struct file *file, char __user *buf,
833 size_t count, loff_t *ppos)
834{
835 struct dmatest_info *info = file->private_data;
836 return simple_read_from_buffer(buf, count, ppos,
837 info->dbgfs_params.channel,
838 strlen(info->dbgfs_params.channel));
839}
840
841static ssize_t dtf_write_channel(struct file *file, const char __user *buf,
842 size_t size, loff_t *ppos)
843{
844 struct dmatest_info *info = file->private_data;
845 return dtf_write_string(info->dbgfs_params.channel,
846 sizeof(info->dbgfs_params.channel),
847 ppos, buf, size);
848}
849
850static const struct file_operations dtf_channel_fops = {
851 .read = dtf_read_channel,
852 .write = dtf_write_channel,
853 .open = simple_open,
854 .llseek = default_llseek,
855};
856
857static ssize_t dtf_read_device(struct file *file, char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 struct dmatest_info *info = file->private_data;
861 return simple_read_from_buffer(buf, count, ppos,
862 info->dbgfs_params.device,
863 strlen(info->dbgfs_params.device));
864}
865
866static ssize_t dtf_write_device(struct file *file, const char __user *buf,
867 size_t size, loff_t *ppos)
868{
869 struct dmatest_info *info = file->private_data;
870 return dtf_write_string(info->dbgfs_params.device,
871 sizeof(info->dbgfs_params.device),
872 ppos, buf, size);
873}
874
875static const struct file_operations dtf_device_fops = {
876 .read = dtf_read_device,
877 .write = dtf_write_device,
878 .open = simple_open,
879 .llseek = default_llseek,
880};
881
882static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
883 size_t count, loff_t *ppos)
884{
885 struct dmatest_info *info = file->private_data;
886 char buf[3];
887
888 mutex_lock(&info->lock);
889 if (info->nr_channels)
890 buf[0] = 'Y';
891 else
892 buf[0] = 'N';
893 mutex_unlock(&info->lock);
894 buf[1] = '\n';
895 buf[2] = 0x00;
896 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
897}
898
899static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
900 size_t count, loff_t *ppos)
901{
902 struct dmatest_info *info = file->private_data;
903 char buf[16];
904 bool bv;
905 int ret = 0;
906
907 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
908 return -EFAULT;
909
910 if (strtobool(buf, &bv) == 0) {
911 mutex_lock(&info->lock);
912 ret = __restart_threaded_test(info, bv);
913 mutex_unlock(&info->lock);
914 }
915
916 return ret ? ret : count;
917}
918
919static const struct file_operations dtf_run_fops = {
920 .read = dtf_read_run,
921 .write = dtf_write_run,
922 .open = simple_open,
923 .llseek = default_llseek,
924};
925
926static int dmatest_register_dbgfs(struct dmatest_info *info)
927{
928 struct dentry *d;
929 struct dmatest_params *params = &info->dbgfs_params;
930 int ret = -ENOMEM;
931
932 d = debugfs_create_dir("dmatest", NULL);
933 if (IS_ERR(d))
934 return PTR_ERR(d);
935 if (!d)
936 goto err_root;
937
938 info->root = d;
939
940 /* Copy initial values */
941 memcpy(params, &info->params, sizeof(*params));
942
943 /* Test parameters */
944
945 d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root,
946 (u32 *)&params->buf_size);
947 if (IS_ERR_OR_NULL(d))
948 goto err_node;
949
950 d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root,
951 info, &dtf_channel_fops);
952 if (IS_ERR_OR_NULL(d))
953 goto err_node;
954
955 d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root,
956 info, &dtf_device_fops);
957 if (IS_ERR_OR_NULL(d))
958 goto err_node;
959
960 d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root,
961 (u32 *)&params->threads_per_chan);
962 if (IS_ERR_OR_NULL(d))
963 goto err_node;
964
965 d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root,
966 (u32 *)&params->max_channels);
967 if (IS_ERR_OR_NULL(d))
968 goto err_node;
969
970 d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root,
971 (u32 *)&params->iterations);
972 if (IS_ERR_OR_NULL(d))
973 goto err_node;
974
975 d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root,
976 (u32 *)&params->xor_sources);
977 if (IS_ERR_OR_NULL(d))
978 goto err_node;
979
980 d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root,
981 (u32 *)&params->pq_sources);
982 if (IS_ERR_OR_NULL(d))
983 goto err_node;
984
985 d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root,
986 (u32 *)&params->timeout);
987 if (IS_ERR_OR_NULL(d))
988 goto err_node;
989
990 /* Run or stop threaded test */
991 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
992 info, &dtf_run_fops);
993 if (IS_ERR_OR_NULL(d))
994 goto err_node;
995
996 return 0;
997
998err_node:
999 debugfs_remove_recursive(info->root);
1000err_root:
1001 pr_err("dmatest: Failed to initialize debugfs\n");
1002 return ret;
1003}
1004
e03e93a9
AS
1005static int __init dmatest_init(void)
1006{
1007 struct dmatest_info *info = &test_info;
15b8a8ea 1008 struct dmatest_params *params = &info->params;
851b7e16 1009 int ret;
e03e93a9
AS
1010
1011 memset(info, 0, sizeof(*info));
1012
851b7e16 1013 mutex_init(&info->lock);
838cc704
AS
1014 INIT_LIST_HEAD(&info->channels);
1015
1016 /* Set default parameters */
15b8a8ea
AS
1017 params->buf_size = test_buf_size;
1018 strlcpy(params->channel, test_channel, sizeof(params->channel));
1019 strlcpy(params->device, test_device, sizeof(params->device));
1020 params->threads_per_chan = threads_per_chan;
1021 params->max_channels = max_channels;
1022 params->iterations = iterations;
1023 params->xor_sources = xor_sources;
1024 params->pq_sources = pq_sources;
1025 params->timeout = timeout;
e03e93a9 1026
851b7e16
AS
1027 ret = dmatest_register_dbgfs(info);
1028 if (ret)
1029 return ret;
1030
1031#ifdef MODULE
1032 return 0;
1033#else
e03e93a9 1034 return run_threaded_test(info);
851b7e16 1035#endif
e03e93a9
AS
1036}
1037/* when compiled-in wait for drivers to load first */
1038late_initcall(dmatest_init);
1039
1040static void __exit dmatest_exit(void)
1041{
1042 struct dmatest_info *info = &test_info;
1043
851b7e16 1044 debugfs_remove_recursive(info->root);
e03e93a9
AS
1045 stop_threaded_test(info);
1046}
4a776f0a
HS
1047module_exit(dmatest_exit);
1048
e05503ef 1049MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4a776f0a 1050MODULE_LICENSE("GPL v2");