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