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