watchdog: store the watchdog sample period as a variable
[linux-2.6-block.git] / drivers / dma / dmatest.c
CommitLineData
4a776f0a
HS
1/*
2 * DMA Engine test module
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/delay.h>
b7f080cf 11#include <linux/dma-mapping.h>
4a776f0a 12#include <linux/dmaengine.h>
981ed70d 13#include <linux/freezer.h>
4a776f0a
HS
14#include <linux/init.h>
15#include <linux/kthread.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/random.h>
5a0e3ad6 19#include <linux/slab.h>
4a776f0a
HS
20#include <linux/wait.h>
21
22static unsigned int test_buf_size = 16384;
23module_param(test_buf_size, uint, S_IRUGO);
24MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
25
06190d84 26static char test_channel[20];
4a776f0a
HS
27module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
28MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
29
06190d84 30static char test_device[20];
4a776f0a
HS
31module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
32MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
33
34static unsigned int threads_per_chan = 1;
35module_param(threads_per_chan, uint, S_IRUGO);
36MODULE_PARM_DESC(threads_per_chan,
37 "Number of threads to start per channel (default: 1)");
38
39static unsigned int max_channels;
40module_param(max_channels, uint, S_IRUGO);
33df8ca0 41MODULE_PARM_DESC(max_channels,
4a776f0a
HS
42 "Maximum number of channels to use (default: all)");
43
0a2ff57d
NF
44static unsigned int iterations;
45module_param(iterations, uint, S_IRUGO);
46MODULE_PARM_DESC(iterations,
47 "Iterations before stopping test (default: infinite)");
48
b54d5cb9
DW
49static unsigned int xor_sources = 3;
50module_param(xor_sources, uint, S_IRUGO);
51MODULE_PARM_DESC(xor_sources,
52 "Number of xor source buffers (default: 3)");
53
58691d64
DW
54static unsigned int pq_sources = 3;
55module_param(pq_sources, uint, S_IRUGO);
56MODULE_PARM_DESC(pq_sources,
57 "Number of p+q source buffers (default: 3)");
58
d42efe6b
VK
59static int timeout = 3000;
60module_param(timeout, uint, S_IRUGO);
85ee7a1d
JP
61MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
62 "Pass -1 for infinite timeout");
d42efe6b 63
4a776f0a
HS
64/*
65 * Initialization patterns. All bytes in the source buffer has bit 7
66 * set, all bytes in the destination buffer has bit 7 cleared.
67 *
68 * Bit 6 is set for all bytes which are to be copied by the DMA
69 * engine. Bit 5 is set for all bytes which are to be overwritten by
70 * the DMA engine.
71 *
72 * The remaining bits are the inverse of a counter which increments by
73 * one for each byte address.
74 */
75#define PATTERN_SRC 0x80
76#define PATTERN_DST 0x00
77#define PATTERN_COPY 0x40
78#define PATTERN_OVERWRITE 0x20
79#define PATTERN_COUNT_MASK 0x1f
80
81struct dmatest_thread {
82 struct list_head node;
83 struct task_struct *task;
84 struct dma_chan *chan;
b54d5cb9
DW
85 u8 **srcs;
86 u8 **dsts;
87 enum dma_transaction_type type;
4a776f0a
HS
88};
89
90struct dmatest_chan {
91 struct list_head node;
92 struct dma_chan *chan;
93 struct list_head threads;
94};
95
96/*
97 * These are protected by dma_list_mutex since they're only used by
33df8ca0 98 * the DMA filter function callback
4a776f0a
HS
99 */
100static LIST_HEAD(dmatest_channels);
101static unsigned int nr_channels;
102
103static bool dmatest_match_channel(struct dma_chan *chan)
104{
105 if (test_channel[0] == '\0')
106 return true;
41d5e59c 107 return strcmp(dma_chan_name(chan), test_channel) == 0;
4a776f0a
HS
108}
109
110static bool dmatest_match_device(struct dma_device *device)
111{
112 if (test_device[0] == '\0')
113 return true;
06190d84 114 return strcmp(dev_name(device->dev), test_device) == 0;
4a776f0a
HS
115}
116
117static unsigned long dmatest_random(void)
118{
119 unsigned long buf;
120
121 get_random_bytes(&buf, sizeof(buf));
122 return buf;
123}
124
b54d5cb9 125static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
4a776f0a
HS
126{
127 unsigned int i;
b54d5cb9
DW
128 u8 *buf;
129
130 for (; (buf = *bufs); bufs++) {
131 for (i = 0; i < start; i++)
132 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
133 for ( ; i < start + len; i++)
134 buf[i] = PATTERN_SRC | PATTERN_COPY
c019894e 135 | (~i & PATTERN_COUNT_MASK);
b54d5cb9
DW
136 for ( ; i < test_buf_size; i++)
137 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
138 buf++;
139 }
4a776f0a
HS
140}
141
b54d5cb9 142static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
4a776f0a
HS
143{
144 unsigned int i;
b54d5cb9
DW
145 u8 *buf;
146
147 for (; (buf = *bufs); bufs++) {
148 for (i = 0; i < start; i++)
149 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
150 for ( ; i < start + len; i++)
151 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
152 | (~i & PATTERN_COUNT_MASK);
153 for ( ; i < test_buf_size; i++)
154 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
155 }
4a776f0a
HS
156}
157
158static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
159 unsigned int counter, bool is_srcbuf)
160{
161 u8 diff = actual ^ pattern;
162 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
163 const char *thread_name = current->comm;
164
165 if (is_srcbuf)
166 pr_warning("%s: srcbuf[0x%x] overwritten!"
167 " Expected %02x, got %02x\n",
168 thread_name, index, expected, actual);
169 else if ((pattern & PATTERN_COPY)
170 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
171 pr_warning("%s: dstbuf[0x%x] not copied!"
172 " Expected %02x, got %02x\n",
173 thread_name, index, expected, actual);
174 else if (diff & PATTERN_SRC)
175 pr_warning("%s: dstbuf[0x%x] was copied!"
176 " Expected %02x, got %02x\n",
177 thread_name, index, expected, actual);
178 else
179 pr_warning("%s: dstbuf[0x%x] mismatch!"
180 " Expected %02x, got %02x\n",
181 thread_name, index, expected, actual);
182}
183
b54d5cb9 184static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
4a776f0a
HS
185 unsigned int end, unsigned int counter, u8 pattern,
186 bool is_srcbuf)
187{
188 unsigned int i;
189 unsigned int error_count = 0;
190 u8 actual;
b54d5cb9
DW
191 u8 expected;
192 u8 *buf;
193 unsigned int counter_orig = counter;
194
195 for (; (buf = *bufs); bufs++) {
196 counter = counter_orig;
197 for (i = start; i < end; i++) {
198 actual = buf[i];
199 expected = pattern | (~counter & PATTERN_COUNT_MASK);
200 if (actual != expected) {
201 if (error_count < 32)
202 dmatest_mismatch(actual, pattern, i,
203 counter, is_srcbuf);
204 error_count++;
205 }
206 counter++;
4a776f0a 207 }
4a776f0a
HS
208 }
209
210 if (error_count > 32)
211 pr_warning("%s: %u errors suppressed\n",
212 current->comm, error_count - 32);
213
214 return error_count;
215}
216
adfa543e
TH
217/* poor man's completion - we want to use wait_event_freezable() on it */
218struct dmatest_done {
219 bool done;
220 wait_queue_head_t *wait;
221};
222
223static void dmatest_callback(void *arg)
e44e0aa3 224{
adfa543e
TH
225 struct dmatest_done *done = arg;
226
227 done->done = true;
228 wake_up_all(done->wait);
e44e0aa3
DW
229}
230
4a776f0a
HS
231/*
232 * This function repeatedly tests DMA transfers of various lengths and
b54d5cb9
DW
233 * offsets for a given operation type until it is told to exit by
234 * kthread_stop(). There may be multiple threads running this function
235 * in parallel for a single channel, and there may be multiple channels
236 * being tested in parallel.
4a776f0a
HS
237 *
238 * Before each test, the source and destination buffer is initialized
239 * with a known pattern. This pattern is different depending on
240 * whether it's in an area which is supposed to be copied or
241 * overwritten, and different in the source and destination buffers.
242 * So if the DMA engine doesn't copy exactly what we tell it to copy,
243 * we'll notice.
244 */
245static int dmatest_func(void *data)
246{
adfa543e 247 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
4a776f0a 248 struct dmatest_thread *thread = data;
adfa543e 249 struct dmatest_done done = { .wait = &done_wait };
4a776f0a
HS
250 struct dma_chan *chan;
251 const char *thread_name;
252 unsigned int src_off, dst_off, len;
253 unsigned int error_count;
254 unsigned int failed_tests = 0;
255 unsigned int total_tests = 0;
256 dma_cookie_t cookie;
257 enum dma_status status;
b54d5cb9 258 enum dma_ctrl_flags flags;
94de648d 259 u8 pq_coefs[pq_sources + 1];
4a776f0a 260 int ret;
b54d5cb9
DW
261 int src_cnt;
262 int dst_cnt;
263 int i;
4a776f0a
HS
264
265 thread_name = current->comm;
adfa543e 266 set_freezable();
4a776f0a
HS
267
268 ret = -ENOMEM;
4a776f0a
HS
269
270 smp_rmb();
271 chan = thread->chan;
b54d5cb9
DW
272 if (thread->type == DMA_MEMCPY)
273 src_cnt = dst_cnt = 1;
274 else if (thread->type == DMA_XOR) {
275 src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
276 dst_cnt = 1;
58691d64
DW
277 } else if (thread->type == DMA_PQ) {
278 src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
279 dst_cnt = 2;
94de648d 280 for (i = 0; i < src_cnt; i++)
58691d64 281 pq_coefs[i] = 1;
b54d5cb9
DW
282 } else
283 goto err_srcs;
284
285 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
286 if (!thread->srcs)
287 goto err_srcs;
288 for (i = 0; i < src_cnt; i++) {
289 thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
290 if (!thread->srcs[i])
291 goto err_srcbuf;
292 }
293 thread->srcs[i] = NULL;
294
295 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
296 if (!thread->dsts)
297 goto err_dsts;
298 for (i = 0; i < dst_cnt; i++) {
299 thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
300 if (!thread->dsts[i])
301 goto err_dstbuf;
302 }
303 thread->dsts[i] = NULL;
304
e44e0aa3
DW
305 set_user_nice(current, 10);
306
b203bd3f
IS
307 /*
308 * src buffers are freed by the DMAEngine code with dma_unmap_single()
309 * dst buffers are freed by ourselves below
310 */
311 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
312 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
4a776f0a 313
0a2ff57d
NF
314 while (!kthread_should_stop()
315 && !(iterations && total_tests >= iterations)) {
d86be86e 316 struct dma_device *dev = chan->device;
b54d5cb9
DW
317 struct dma_async_tx_descriptor *tx = NULL;
318 dma_addr_t dma_srcs[src_cnt];
319 dma_addr_t dma_dsts[dst_cnt];
83544ae9 320 u8 align = 0;
d86be86e 321
4a776f0a
HS
322 total_tests++;
323
83544ae9
DW
324 /* honor alignment restrictions */
325 if (thread->type == DMA_MEMCPY)
326 align = dev->copy_align;
327 else if (thread->type == DMA_XOR)
328 align = dev->xor_align;
329 else if (thread->type == DMA_PQ)
330 align = dev->pq_align;
331
cfe4f275
GL
332 if (1 << align > test_buf_size) {
333 pr_err("%u-byte buffer too small for %d-byte alignment\n",
334 test_buf_size, 1 << align);
335 break;
336 }
337
338 len = dmatest_random() % test_buf_size + 1;
83544ae9 339 len = (len >> align) << align;
cfe4f275
GL
340 if (!len)
341 len = 1 << align;
342 src_off = dmatest_random() % (test_buf_size - len + 1);
343 dst_off = dmatest_random() % (test_buf_size - len + 1);
344
83544ae9
DW
345 src_off = (src_off >> align) << align;
346 dst_off = (dst_off >> align) << align;
347
b54d5cb9
DW
348 dmatest_init_srcs(thread->srcs, src_off, len);
349 dmatest_init_dsts(thread->dsts, dst_off, len);
4a776f0a 350
b54d5cb9
DW
351 for (i = 0; i < src_cnt; i++) {
352 u8 *buf = thread->srcs[i] + src_off;
353
354 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
355 DMA_TO_DEVICE);
356 }
d86be86e 357 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
b54d5cb9
DW
358 for (i = 0; i < dst_cnt; i++) {
359 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
360 test_buf_size,
361 DMA_BIDIRECTIONAL);
362 }
363
83544ae9 364
b54d5cb9
DW
365 if (thread->type == DMA_MEMCPY)
366 tx = dev->device_prep_dma_memcpy(chan,
367 dma_dsts[0] + dst_off,
368 dma_srcs[0], len,
369 flags);
370 else if (thread->type == DMA_XOR)
371 tx = dev->device_prep_dma_xor(chan,
372 dma_dsts[0] + dst_off,
67b9124f 373 dma_srcs, src_cnt,
b54d5cb9 374 len, flags);
58691d64
DW
375 else if (thread->type == DMA_PQ) {
376 dma_addr_t dma_pq[dst_cnt];
377
378 for (i = 0; i < dst_cnt; i++)
379 dma_pq[i] = dma_dsts[i] + dst_off;
380 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
94de648d 381 src_cnt, pq_coefs,
58691d64
DW
382 len, flags);
383 }
d86be86e 384
d86be86e 385 if (!tx) {
b54d5cb9
DW
386 for (i = 0; i < src_cnt; i++)
387 dma_unmap_single(dev->dev, dma_srcs[i], len,
388 DMA_TO_DEVICE);
389 for (i = 0; i < dst_cnt; i++)
390 dma_unmap_single(dev->dev, dma_dsts[i],
391 test_buf_size,
392 DMA_BIDIRECTIONAL);
d86be86e
AN
393 pr_warning("%s: #%u: prep error with src_off=0x%x "
394 "dst_off=0x%x len=0x%x\n",
395 thread_name, total_tests - 1,
396 src_off, dst_off, len);
397 msleep(100);
398 failed_tests++;
399 continue;
400 }
e44e0aa3 401
adfa543e 402 done.done = false;
e44e0aa3 403 tx->callback = dmatest_callback;
adfa543e 404 tx->callback_param = &done;
d86be86e
AN
405 cookie = tx->tx_submit(tx);
406
4a776f0a
HS
407 if (dma_submit_error(cookie)) {
408 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
409 "dst_off=0x%x len=0x%x\n",
410 thread_name, total_tests - 1, cookie,
411 src_off, dst_off, len);
412 msleep(100);
413 failed_tests++;
414 continue;
415 }
b54d5cb9 416 dma_async_issue_pending(chan);
4a776f0a 417
adfa543e
TH
418 wait_event_freezable_timeout(done_wait, done.done,
419 msecs_to_jiffies(timeout));
981ed70d 420
e44e0aa3 421 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
4a776f0a 422
adfa543e
TH
423 if (!done.done) {
424 /*
425 * We're leaving the timed out dma operation with
426 * dangling pointer to done_wait. To make this
427 * correct, we'll need to allocate wait_done for
428 * each test iteration and perform "who's gonna
429 * free it this time?" dancing. For now, just
430 * leave it dangling.
431 */
e44e0aa3
DW
432 pr_warning("%s: #%u: test timed out\n",
433 thread_name, total_tests - 1);
434 failed_tests++;
435 continue;
436 } else if (status != DMA_SUCCESS) {
437 pr_warning("%s: #%u: got completion callback,"
438 " but status is \'%s\'\n",
439 thread_name, total_tests - 1,
440 status == DMA_ERROR ? "error" : "in progress");
4a776f0a
HS
441 failed_tests++;
442 continue;
443 }
e44e0aa3 444
d86be86e 445 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
b54d5cb9
DW
446 for (i = 0; i < dst_cnt; i++)
447 dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
448 DMA_BIDIRECTIONAL);
4a776f0a
HS
449
450 error_count = 0;
451
452 pr_debug("%s: verifying source buffer...\n", thread_name);
b54d5cb9 453 error_count += dmatest_verify(thread->srcs, 0, src_off,
4a776f0a 454 0, PATTERN_SRC, true);
b54d5cb9 455 error_count += dmatest_verify(thread->srcs, src_off,
4a776f0a
HS
456 src_off + len, src_off,
457 PATTERN_SRC | PATTERN_COPY, true);
b54d5cb9 458 error_count += dmatest_verify(thread->srcs, src_off + len,
4a776f0a
HS
459 test_buf_size, src_off + len,
460 PATTERN_SRC, true);
461
462 pr_debug("%s: verifying dest buffer...\n",
463 thread->task->comm);
b54d5cb9 464 error_count += dmatest_verify(thread->dsts, 0, dst_off,
4a776f0a 465 0, PATTERN_DST, false);
b54d5cb9 466 error_count += dmatest_verify(thread->dsts, dst_off,
4a776f0a
HS
467 dst_off + len, src_off,
468 PATTERN_SRC | PATTERN_COPY, false);
b54d5cb9 469 error_count += dmatest_verify(thread->dsts, dst_off + len,
4a776f0a
HS
470 test_buf_size, dst_off + len,
471 PATTERN_DST, false);
472
473 if (error_count) {
474 pr_warning("%s: #%u: %u errors with "
475 "src_off=0x%x dst_off=0x%x len=0x%x\n",
476 thread_name, total_tests - 1, error_count,
477 src_off, dst_off, len);
478 failed_tests++;
479 } else {
480 pr_debug("%s: #%u: No errors with "
481 "src_off=0x%x dst_off=0x%x len=0x%x\n",
482 thread_name, total_tests - 1,
483 src_off, dst_off, len);
484 }
485 }
486
487 ret = 0;
b54d5cb9
DW
488 for (i = 0; thread->dsts[i]; i++)
489 kfree(thread->dsts[i]);
4a776f0a 490err_dstbuf:
b54d5cb9
DW
491 kfree(thread->dsts);
492err_dsts:
493 for (i = 0; thread->srcs[i]; i++)
494 kfree(thread->srcs[i]);
4a776f0a 495err_srcbuf:
b54d5cb9
DW
496 kfree(thread->srcs);
497err_srcs:
4a776f0a
HS
498 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
499 thread_name, total_tests, failed_tests, ret);
0a2ff57d 500
9704efaa
VK
501 /* terminate all transfers on specified channels */
502 chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
0a2ff57d
NF
503 if (iterations > 0)
504 while (!kthread_should_stop()) {
b953df7c 505 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
0a2ff57d
NF
506 interruptible_sleep_on(&wait_dmatest_exit);
507 }
508
4a776f0a
HS
509 return ret;
510}
511
512static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
513{
514 struct dmatest_thread *thread;
515 struct dmatest_thread *_thread;
516 int ret;
517
518 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
519 ret = kthread_stop(thread->task);
520 pr_debug("dmatest: thread %s exited with status %d\n",
521 thread->task->comm, ret);
522 list_del(&thread->node);
523 kfree(thread);
524 }
9704efaa
VK
525
526 /* terminate all transfers on specified channels */
527 dtc->chan->device->device_control(dtc->chan, DMA_TERMINATE_ALL, 0);
528
4a776f0a
HS
529 kfree(dtc);
530}
531
b54d5cb9 532static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
4a776f0a 533{
b54d5cb9
DW
534 struct dmatest_thread *thread;
535 struct dma_chan *chan = dtc->chan;
536 char *op;
537 unsigned int i;
4a776f0a 538
b54d5cb9
DW
539 if (type == DMA_MEMCPY)
540 op = "copy";
541 else if (type == DMA_XOR)
542 op = "xor";
58691d64
DW
543 else if (type == DMA_PQ)
544 op = "pq";
b54d5cb9
DW
545 else
546 return -EINVAL;
4a776f0a
HS
547
548 for (i = 0; i < threads_per_chan; i++) {
549 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
550 if (!thread) {
b54d5cb9
DW
551 pr_warning("dmatest: No memory for %s-%s%u\n",
552 dma_chan_name(chan), op, i);
553
4a776f0a
HS
554 break;
555 }
556 thread->chan = dtc->chan;
b54d5cb9 557 thread->type = type;
4a776f0a 558 smp_wmb();
b54d5cb9
DW
559 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
560 dma_chan_name(chan), op, i);
4a776f0a 561 if (IS_ERR(thread->task)) {
b54d5cb9
DW
562 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
563 dma_chan_name(chan), op, i);
4a776f0a
HS
564 kfree(thread);
565 break;
566 }
567
568 /* srcbuf and dstbuf are allocated by the thread itself */
569
570 list_add_tail(&thread->node, &dtc->threads);
571 }
572
b54d5cb9
DW
573 return i;
574}
575
576static int dmatest_add_channel(struct dma_chan *chan)
577{
578 struct dmatest_chan *dtc;
579 struct dma_device *dma_dev = chan->device;
580 unsigned int thread_count = 0;
b9033e68 581 int cnt;
b54d5cb9
DW
582
583 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
584 if (!dtc) {
585 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
586 return -ENOMEM;
587 }
588
589 dtc->chan = chan;
590 INIT_LIST_HEAD(&dtc->threads);
591
592 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
593 cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
f1aef8b6 594 thread_count += cnt > 0 ? cnt : 0;
b54d5cb9
DW
595 }
596 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
597 cnt = dmatest_add_threads(dtc, DMA_XOR);
f1aef8b6 598 thread_count += cnt > 0 ? cnt : 0;
b54d5cb9 599 }
58691d64
DW
600 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
601 cnt = dmatest_add_threads(dtc, DMA_PQ);
d07a74a5 602 thread_count += cnt > 0 ? cnt : 0;
58691d64 603 }
b54d5cb9
DW
604
605 pr_info("dmatest: Started %u threads using %s\n",
606 thread_count, dma_chan_name(chan));
4a776f0a
HS
607
608 list_add_tail(&dtc->node, &dmatest_channels);
609 nr_channels++;
610
33df8ca0 611 return 0;
4a776f0a
HS
612}
613
7dd60251 614static bool filter(struct dma_chan *chan, void *param)
4a776f0a 615{
33df8ca0 616 if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
7dd60251 617 return false;
33df8ca0 618 else
7dd60251 619 return true;
4a776f0a
HS
620}
621
4a776f0a
HS
622static int __init dmatest_init(void)
623{
33df8ca0
DW
624 dma_cap_mask_t mask;
625 struct dma_chan *chan;
626 int err = 0;
627
628 dma_cap_zero(mask);
629 dma_cap_set(DMA_MEMCPY, mask);
630 for (;;) {
631 chan = dma_request_channel(mask, filter, NULL);
632 if (chan) {
633 err = dmatest_add_channel(chan);
c56c81ab 634 if (err) {
33df8ca0
DW
635 dma_release_channel(chan);
636 break; /* add_channel failed, punt */
637 }
638 } else
639 break; /* no more channels available */
640 if (max_channels && nr_channels >= max_channels)
641 break; /* we have all we need */
642 }
4a776f0a 643
33df8ca0 644 return err;
4a776f0a 645}
33df8ca0
DW
646/* when compiled-in wait for drivers to load first */
647late_initcall(dmatest_init);
4a776f0a
HS
648
649static void __exit dmatest_exit(void)
650{
33df8ca0 651 struct dmatest_chan *dtc, *_dtc;
7cbd4877 652 struct dma_chan *chan;
33df8ca0
DW
653
654 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
655 list_del(&dtc->node);
7cbd4877 656 chan = dtc->chan;
33df8ca0
DW
657 dmatest_cleanup_channel(dtc);
658 pr_debug("dmatest: dropped channel %s\n",
7cbd4877
DW
659 dma_chan_name(chan));
660 dma_release_channel(chan);
33df8ca0 661 }
4a776f0a
HS
662}
663module_exit(dmatest_exit);
664
e05503ef 665MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4a776f0a 666MODULE_LICENSE("GPL v2");