blob: 7e1b0aa0ca50808d4c5dce0f94dc166fe10f9255 [file] [log] [blame]
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001/*
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>
11#include <linux/dmaengine.h>
12#include <linux/init.h>
13#include <linux/kthread.h>
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070018#include <linux/wait.h>
19
20static unsigned int test_buf_size = 16384;
21module_param(test_buf_size, uint, S_IRUGO);
22MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
23
Kay Sievers06190d82008-11-11 13:12:33 -070024static char test_channel[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070025module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
26MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
27
Kay Sievers06190d82008-11-11 13:12:33 -070028static char test_device[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070029module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
30MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
31
32static unsigned int threads_per_chan = 1;
33module_param(threads_per_chan, uint, S_IRUGO);
34MODULE_PARM_DESC(threads_per_chan,
35 "Number of threads to start per channel (default: 1)");
36
37static unsigned int max_channels;
38module_param(max_channels, uint, S_IRUGO);
Dan Williams33df8ca2009-01-06 11:38:15 -070039MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070040 "Maximum number of channels to use (default: all)");
41
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020042static unsigned int iterations;
43module_param(iterations, uint, S_IRUGO);
44MODULE_PARM_DESC(iterations,
45 "Iterations before stopping test (default: infinite)");
46
Dan Williamsb54d5cb2009-03-25 09:13:25 -070047static unsigned int xor_sources = 3;
48module_param(xor_sources, uint, S_IRUGO);
49MODULE_PARM_DESC(xor_sources,
50 "Number of xor source buffers (default: 3)");
51
Dan Williams58691d62009-08-29 19:09:27 -070052static unsigned int pq_sources = 3;
53module_param(pq_sources, uint, S_IRUGO);
54MODULE_PARM_DESC(pq_sources,
55 "Number of p+q source buffers (default: 3)");
56
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070057/*
58 * Initialization patterns. All bytes in the source buffer has bit 7
59 * set, all bytes in the destination buffer has bit 7 cleared.
60 *
61 * Bit 6 is set for all bytes which are to be copied by the DMA
62 * engine. Bit 5 is set for all bytes which are to be overwritten by
63 * the DMA engine.
64 *
65 * The remaining bits are the inverse of a counter which increments by
66 * one for each byte address.
67 */
68#define PATTERN_SRC 0x80
69#define PATTERN_DST 0x00
70#define PATTERN_COPY 0x40
71#define PATTERN_OVERWRITE 0x20
72#define PATTERN_COUNT_MASK 0x1f
73
74struct dmatest_thread {
75 struct list_head node;
76 struct task_struct *task;
77 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070078 u8 **srcs;
79 u8 **dsts;
80 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070081};
82
83struct dmatest_chan {
84 struct list_head node;
85 struct dma_chan *chan;
86 struct list_head threads;
87};
88
89/*
90 * These are protected by dma_list_mutex since they're only used by
Dan Williams33df8ca2009-01-06 11:38:15 -070091 * the DMA filter function callback
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070092 */
93static LIST_HEAD(dmatest_channels);
94static unsigned int nr_channels;
95
96static bool dmatest_match_channel(struct dma_chan *chan)
97{
98 if (test_channel[0] == '\0')
99 return true;
Dan Williams41d5e592009-01-06 11:38:21 -0700100 return strcmp(dma_chan_name(chan), test_channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700101}
102
103static bool dmatest_match_device(struct dma_device *device)
104{
105 if (test_device[0] == '\0')
106 return true;
Kay Sievers06190d82008-11-11 13:12:33 -0700107 return strcmp(dev_name(device->dev), test_device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700108}
109
110static unsigned long dmatest_random(void)
111{
112 unsigned long buf;
113
114 get_random_bytes(&buf, sizeof(buf));
115 return buf;
116}
117
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700118static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700119{
120 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700121 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700122
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700123 for (; (buf = *bufs); bufs++) {
124 for (i = 0; i < start; i++)
125 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
126 for ( ; i < start + len; i++)
127 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700128 | (~i & PATTERN_COUNT_MASK);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700129 for ( ; i < test_buf_size; i++)
130 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
131 buf++;
132 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700133}
134
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700135static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700136{
137 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700138 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700139
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700140 for (; (buf = *bufs); bufs++) {
141 for (i = 0; i < start; i++)
142 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
143 for ( ; i < start + len; i++)
144 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
145 | (~i & PATTERN_COUNT_MASK);
146 for ( ; i < test_buf_size; i++)
147 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
148 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700149}
150
151static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
152 unsigned int counter, bool is_srcbuf)
153{
154 u8 diff = actual ^ pattern;
155 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
156 const char *thread_name = current->comm;
157
158 if (is_srcbuf)
159 pr_warning("%s: srcbuf[0x%x] overwritten!"
160 " Expected %02x, got %02x\n",
161 thread_name, index, expected, actual);
162 else if ((pattern & PATTERN_COPY)
163 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
164 pr_warning("%s: dstbuf[0x%x] not copied!"
165 " Expected %02x, got %02x\n",
166 thread_name, index, expected, actual);
167 else if (diff & PATTERN_SRC)
168 pr_warning("%s: dstbuf[0x%x] was copied!"
169 " Expected %02x, got %02x\n",
170 thread_name, index, expected, actual);
171 else
172 pr_warning("%s: dstbuf[0x%x] mismatch!"
173 " Expected %02x, got %02x\n",
174 thread_name, index, expected, actual);
175}
176
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700177static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700178 unsigned int end, unsigned int counter, u8 pattern,
179 bool is_srcbuf)
180{
181 unsigned int i;
182 unsigned int error_count = 0;
183 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700184 u8 expected;
185 u8 *buf;
186 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700187
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700188 for (; (buf = *bufs); bufs++) {
189 counter = counter_orig;
190 for (i = start; i < end; i++) {
191 actual = buf[i];
192 expected = pattern | (~counter & PATTERN_COUNT_MASK);
193 if (actual != expected) {
194 if (error_count < 32)
195 dmatest_mismatch(actual, pattern, i,
196 counter, is_srcbuf);
197 error_count++;
198 }
199 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700200 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700201 }
202
203 if (error_count > 32)
204 pr_warning("%s: %u errors suppressed\n",
205 current->comm, error_count - 32);
206
207 return error_count;
208}
209
Dan Williamse44e0aa2009-03-25 09:13:25 -0700210static void dmatest_callback(void *completion)
211{
212 complete(completion);
213}
214
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700215/*
216 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700217 * offsets for a given operation type until it is told to exit by
218 * kthread_stop(). There may be multiple threads running this function
219 * in parallel for a single channel, and there may be multiple channels
220 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700221 *
222 * Before each test, the source and destination buffer is initialized
223 * with a known pattern. This pattern is different depending on
224 * whether it's in an area which is supposed to be copied or
225 * overwritten, and different in the source and destination buffers.
226 * So if the DMA engine doesn't copy exactly what we tell it to copy,
227 * we'll notice.
228 */
229static int dmatest_func(void *data)
230{
231 struct dmatest_thread *thread = data;
232 struct dma_chan *chan;
233 const char *thread_name;
234 unsigned int src_off, dst_off, len;
235 unsigned int error_count;
236 unsigned int failed_tests = 0;
237 unsigned int total_tests = 0;
238 dma_cookie_t cookie;
239 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700240 enum dma_ctrl_flags flags;
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100241 u8 pq_coefs[pq_sources + 1];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700242 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700243 int src_cnt;
244 int dst_cnt;
245 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700246
247 thread_name = current->comm;
248
249 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700250
251 smp_rmb();
252 chan = thread->chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700253 if (thread->type == DMA_MEMCPY)
254 src_cnt = dst_cnt = 1;
255 else if (thread->type == DMA_XOR) {
256 src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
257 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700258 } else if (thread->type == DMA_PQ) {
259 src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
260 dst_cnt = 2;
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100261 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700262 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700263 } else
264 goto err_srcs;
265
266 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
267 if (!thread->srcs)
268 goto err_srcs;
269 for (i = 0; i < src_cnt; i++) {
270 thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
271 if (!thread->srcs[i])
272 goto err_srcbuf;
273 }
274 thread->srcs[i] = NULL;
275
276 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
277 if (!thread->dsts)
278 goto err_dsts;
279 for (i = 0; i < dst_cnt; i++) {
280 thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
281 if (!thread->dsts[i])
282 goto err_dstbuf;
283 }
284 thread->dsts[i] = NULL;
285
Dan Williamse44e0aa2009-03-25 09:13:25 -0700286 set_user_nice(current, 10);
287
Ira Snyderb203bd32011-03-03 07:54:53 +0000288 /*
289 * src buffers are freed by the DMAEngine code with dma_unmap_single()
290 * dst buffers are freed by ourselves below
291 */
292 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
293 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700294
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200295 while (!kthread_should_stop()
296 && !(iterations && total_tests >= iterations)) {
Atsushi Nemotod86be862009-01-13 09:22:20 -0700297 struct dma_device *dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700298 struct dma_async_tx_descriptor *tx = NULL;
299 dma_addr_t dma_srcs[src_cnt];
300 dma_addr_t dma_dsts[dst_cnt];
Dan Williamse44e0aa2009-03-25 09:13:25 -0700301 struct completion cmp;
302 unsigned long tmo = msecs_to_jiffies(3000);
Dan Williams83544ae2009-09-08 17:42:53 -0700303 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700304
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700305 total_tests++;
306
Dan Williams83544ae2009-09-08 17:42:53 -0700307 /* honor alignment restrictions */
308 if (thread->type == DMA_MEMCPY)
309 align = dev->copy_align;
310 else if (thread->type == DMA_XOR)
311 align = dev->xor_align;
312 else if (thread->type == DMA_PQ)
313 align = dev->pq_align;
314
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100315 if (1 << align > test_buf_size) {
316 pr_err("%u-byte buffer too small for %d-byte alignment\n",
317 test_buf_size, 1 << align);
318 break;
319 }
320
321 len = dmatest_random() % test_buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700322 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100323 if (!len)
324 len = 1 << align;
325 src_off = dmatest_random() % (test_buf_size - len + 1);
326 dst_off = dmatest_random() % (test_buf_size - len + 1);
327
Dan Williams83544ae2009-09-08 17:42:53 -0700328 src_off = (src_off >> align) << align;
329 dst_off = (dst_off >> align) << align;
330
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700331 dmatest_init_srcs(thread->srcs, src_off, len);
332 dmatest_init_dsts(thread->dsts, dst_off, len);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700333
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700334 for (i = 0; i < src_cnt; i++) {
335 u8 *buf = thread->srcs[i] + src_off;
336
337 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
338 DMA_TO_DEVICE);
339 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700340 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700341 for (i = 0; i < dst_cnt; i++) {
342 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
343 test_buf_size,
344 DMA_BIDIRECTIONAL);
345 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700346
Dan Williams83544ae2009-09-08 17:42:53 -0700347
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700348 if (thread->type == DMA_MEMCPY)
349 tx = dev->device_prep_dma_memcpy(chan,
350 dma_dsts[0] + dst_off,
351 dma_srcs[0], len,
352 flags);
353 else if (thread->type == DMA_XOR)
354 tx = dev->device_prep_dma_xor(chan,
355 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700356 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700357 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700358 else if (thread->type == DMA_PQ) {
359 dma_addr_t dma_pq[dst_cnt];
360
361 for (i = 0; i < dst_cnt; i++)
362 dma_pq[i] = dma_dsts[i] + dst_off;
363 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100364 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700365 len, flags);
366 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700367
Atsushi Nemotod86be862009-01-13 09:22:20 -0700368 if (!tx) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700369 for (i = 0; i < src_cnt; i++)
370 dma_unmap_single(dev->dev, dma_srcs[i], len,
371 DMA_TO_DEVICE);
372 for (i = 0; i < dst_cnt; i++)
373 dma_unmap_single(dev->dev, dma_dsts[i],
374 test_buf_size,
375 DMA_BIDIRECTIONAL);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700376 pr_warning("%s: #%u: prep error with src_off=0x%x "
377 "dst_off=0x%x len=0x%x\n",
378 thread_name, total_tests - 1,
379 src_off, dst_off, len);
380 msleep(100);
381 failed_tests++;
382 continue;
383 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700384
385 init_completion(&cmp);
386 tx->callback = dmatest_callback;
387 tx->callback_param = &cmp;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700388 cookie = tx->tx_submit(tx);
389
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700390 if (dma_submit_error(cookie)) {
391 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
392 "dst_off=0x%x len=0x%x\n",
393 thread_name, total_tests - 1, cookie,
394 src_off, dst_off, len);
395 msleep(100);
396 failed_tests++;
397 continue;
398 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700399 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700400
Dan Williamse44e0aa2009-03-25 09:13:25 -0700401 tmo = wait_for_completion_timeout(&cmp, tmo);
402 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700403
Dan Williamse44e0aa2009-03-25 09:13:25 -0700404 if (tmo == 0) {
405 pr_warning("%s: #%u: test timed out\n",
406 thread_name, total_tests - 1);
407 failed_tests++;
408 continue;
409 } else if (status != DMA_SUCCESS) {
410 pr_warning("%s: #%u: got completion callback,"
411 " but status is \'%s\'\n",
412 thread_name, total_tests - 1,
413 status == DMA_ERROR ? "error" : "in progress");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700414 failed_tests++;
415 continue;
416 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700417
Atsushi Nemotod86be862009-01-13 09:22:20 -0700418 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700419 for (i = 0; i < dst_cnt; i++)
420 dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
421 DMA_BIDIRECTIONAL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700422
423 error_count = 0;
424
425 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700426 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700427 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700428 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700429 src_off + len, src_off,
430 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700431 error_count += dmatest_verify(thread->srcs, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700432 test_buf_size, src_off + len,
433 PATTERN_SRC, true);
434
435 pr_debug("%s: verifying dest buffer...\n",
436 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700437 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700438 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700439 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700440 dst_off + len, src_off,
441 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700442 error_count += dmatest_verify(thread->dsts, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700443 test_buf_size, dst_off + len,
444 PATTERN_DST, false);
445
446 if (error_count) {
447 pr_warning("%s: #%u: %u errors with "
448 "src_off=0x%x dst_off=0x%x len=0x%x\n",
449 thread_name, total_tests - 1, error_count,
450 src_off, dst_off, len);
451 failed_tests++;
452 } else {
453 pr_debug("%s: #%u: No errors with "
454 "src_off=0x%x dst_off=0x%x len=0x%x\n",
455 thread_name, total_tests - 1,
456 src_off, dst_off, len);
457 }
458 }
459
460 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700461 for (i = 0; thread->dsts[i]; i++)
462 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700463err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700464 kfree(thread->dsts);
465err_dsts:
466 for (i = 0; thread->srcs[i]; i++)
467 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700468err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700469 kfree(thread->srcs);
470err_srcs:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700471 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
472 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200473
474 if (iterations > 0)
475 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800476 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200477 interruptible_sleep_on(&wait_dmatest_exit);
478 }
479
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700480 return ret;
481}
482
483static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
484{
485 struct dmatest_thread *thread;
486 struct dmatest_thread *_thread;
487 int ret;
488
489 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
490 ret = kthread_stop(thread->task);
491 pr_debug("dmatest: thread %s exited with status %d\n",
492 thread->task->comm, ret);
493 list_del(&thread->node);
494 kfree(thread);
495 }
496 kfree(dtc);
497}
498
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700499static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
500{
501 struct dmatest_thread *thread;
502 struct dma_chan *chan = dtc->chan;
503 char *op;
504 unsigned int i;
505
506 if (type == DMA_MEMCPY)
507 op = "copy";
508 else if (type == DMA_XOR)
509 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700510 else if (type == DMA_PQ)
511 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700512 else
513 return -EINVAL;
514
515 for (i = 0; i < threads_per_chan; i++) {
516 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
517 if (!thread) {
518 pr_warning("dmatest: No memory for %s-%s%u\n",
519 dma_chan_name(chan), op, i);
520
521 break;
522 }
523 thread->chan = dtc->chan;
524 thread->type = type;
525 smp_wmb();
526 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
527 dma_chan_name(chan), op, i);
528 if (IS_ERR(thread->task)) {
529 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
530 dma_chan_name(chan), op, i);
531 kfree(thread);
532 break;
533 }
534
535 /* srcbuf and dstbuf are allocated by the thread itself */
536
537 list_add_tail(&thread->node, &dtc->threads);
538 }
539
540 return i;
541}
542
Dan Williams33df8ca2009-01-06 11:38:15 -0700543static int dmatest_add_channel(struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700544{
545 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700546 struct dma_device *dma_dev = chan->device;
547 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400548 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700549
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700550 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700551 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700552 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700553 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700554 }
555
556 dtc->chan = chan;
557 INIT_LIST_HEAD(&dtc->threads);
558
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700559 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
560 cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200561 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700562 }
563 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
564 cnt = dmatest_add_threads(dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200565 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700566 }
Dan Williams58691d62009-08-29 19:09:27 -0700567 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
568 cnt = dmatest_add_threads(dtc, DMA_PQ);
569 thread_count += cnt > 0 ?: 0;
570 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700571
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700572 pr_info("dmatest: Started %u threads using %s\n",
573 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700574
575 list_add_tail(&dtc->node, &dmatest_channels);
576 nr_channels++;
577
Dan Williams33df8ca2009-01-06 11:38:15 -0700578 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700579}
580
Dan Williams7dd60252009-01-06 11:38:19 -0700581static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700582{
Dan Williams33df8ca2009-01-06 11:38:15 -0700583 if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700584 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700585 else
Dan Williams7dd60252009-01-06 11:38:19 -0700586 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700587}
588
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700589static int __init dmatest_init(void)
590{
Dan Williams33df8ca2009-01-06 11:38:15 -0700591 dma_cap_mask_t mask;
592 struct dma_chan *chan;
593 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700594
Dan Williams33df8ca2009-01-06 11:38:15 -0700595 dma_cap_zero(mask);
596 dma_cap_set(DMA_MEMCPY, mask);
597 for (;;) {
598 chan = dma_request_channel(mask, filter, NULL);
599 if (chan) {
600 err = dmatest_add_channel(chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700601 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700602 dma_release_channel(chan);
603 break; /* add_channel failed, punt */
604 }
605 } else
606 break; /* no more channels available */
607 if (max_channels && nr_channels >= max_channels)
608 break; /* we have all we need */
609 }
610
611 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700612}
Dan Williams33df8ca2009-01-06 11:38:15 -0700613/* when compiled-in wait for drivers to load first */
614late_initcall(dmatest_init);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700615
616static void __exit dmatest_exit(void)
617{
Dan Williams33df8ca2009-01-06 11:38:15 -0700618 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700619 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700620
621 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
622 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700623 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700624 dmatest_cleanup_channel(dtc);
625 pr_debug("dmatest: dropped channel %s\n",
Dan Williams7cbd4872009-03-04 16:06:03 -0700626 dma_chan_name(chan));
627 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700628 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700629}
630module_exit(dmatest_exit);
631
632MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
633MODULE_LICENSE("GPL v2");