blob: e3955be2e3a4ec894615fc0b4371481a1f8db6bc [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>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000011#include <linux/dma-mapping.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070012#include <linux/dmaengine.h>
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +020013#include <linux/freezer.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070014#include <linux/init.h>
15#include <linux/kthread.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070020#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
Kay Sievers06190d82008-11-11 13:12:33 -070026static char test_channel[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070027module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
28MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
29
Kay Sievers06190d82008-11-11 13:12:33 -070030static char test_device[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070031module_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);
Dan Williams33df8ca2009-01-06 11:38:15 -070041MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070042 "Maximum number of channels to use (default: all)");
43
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020044static unsigned int iterations;
45module_param(iterations, uint, S_IRUGO);
46MODULE_PARM_DESC(iterations,
47 "Iterations before stopping test (default: infinite)");
48
Dan Williamsb54d5cb2009-03-25 09:13:25 -070049static 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
Dan Williams58691d62009-08-29 19:09:27 -070054static 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
Viresh Kumard42efe62011-03-22 17:27:25 +053059static int timeout = 3000;
60module_param(timeout, uint, S_IRUGO);
Joe Perches85ee7a12011-04-23 20:38:19 -070061MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
62 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053063
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070064/*
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;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070085 u8 **srcs;
86 u8 **dsts;
87 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070088};
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
Dan Williams33df8ca2009-01-06 11:38:15 -070098 * the DMA filter function callback
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070099 */
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;
Dan Williams41d5e592009-01-06 11:38:21 -0700107 return strcmp(dma_chan_name(chan), test_channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700108}
109
110static bool dmatest_match_device(struct dma_device *device)
111{
112 if (test_device[0] == '\0')
113 return true;
Kay Sievers06190d82008-11-11 13:12:33 -0700114 return strcmp(dev_name(device->dev), test_device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700115}
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
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700125static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700126{
127 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700128 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700129
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700130 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
Joe Perchesc0198942009-06-28 09:26:21 -0700135 | (~i & PATTERN_COUNT_MASK);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700136 for ( ; i < test_buf_size; i++)
137 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
138 buf++;
139 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700140}
141
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700142static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700143{
144 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700145 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700146
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700147 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 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700156}
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
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700184static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700185 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;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700191 u8 expected;
192 u8 *buf;
193 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700194
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700195 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++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700207 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700208 }
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
Tejun Heoadfa5432011-11-23 09:28:16 -0800217/* 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)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700224{
Tejun Heoadfa5432011-11-23 09:28:16 -0800225 struct dmatest_done *done = arg;
226
227 done->done = true;
228 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700229}
230
Andy Shevchenko632fd282012-12-17 15:59:52 -0800231static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
232 unsigned int count)
233{
234 while (count--)
235 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
236}
237
238static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
239 unsigned int count)
240{
241 while (count--)
242 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
243}
244
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900245static unsigned int min_odd(unsigned int x, unsigned int y)
246{
247 unsigned int val = min(x, y);
248
249 return val % 2 ? val : val - 1;
250}
251
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700252/*
253 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700254 * offsets for a given operation type until it is told to exit by
255 * kthread_stop(). There may be multiple threads running this function
256 * in parallel for a single channel, and there may be multiple channels
257 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700258 *
259 * Before each test, the source and destination buffer is initialized
260 * with a known pattern. This pattern is different depending on
261 * whether it's in an area which is supposed to be copied or
262 * overwritten, and different in the source and destination buffers.
263 * So if the DMA engine doesn't copy exactly what we tell it to copy,
264 * we'll notice.
265 */
266static int dmatest_func(void *data)
267{
Tejun Heoadfa5432011-11-23 09:28:16 -0800268 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700269 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800270 struct dmatest_done done = { .wait = &done_wait };
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700271 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900272 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700273 const char *thread_name;
274 unsigned int src_off, dst_off, len;
275 unsigned int error_count;
276 unsigned int failed_tests = 0;
277 unsigned int total_tests = 0;
278 dma_cookie_t cookie;
279 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700280 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200281 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700282 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700283 int src_cnt;
284 int dst_cnt;
285 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700286
287 thread_name = current->comm;
Tejun Heoadfa5432011-11-23 09:28:16 -0800288 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700289
290 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700291
292 smp_rmb();
293 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900294 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700295 if (thread->type == DMA_MEMCPY)
296 src_cnt = dst_cnt = 1;
297 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900298 /* force odd to ensure dst = src */
299 src_cnt = min_odd(xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700300 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700301 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900302 /* force odd to ensure dst = src */
303 src_cnt = min_odd(pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700304 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200305
306 pq_coefs = kmalloc(pq_sources+1, GFP_KERNEL);
307 if (!pq_coefs)
308 goto err_thread_type;
309
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100310 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700311 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700312 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200313 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700314
315 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
316 if (!thread->srcs)
317 goto err_srcs;
318 for (i = 0; i < src_cnt; i++) {
319 thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
320 if (!thread->srcs[i])
321 goto err_srcbuf;
322 }
323 thread->srcs[i] = NULL;
324
325 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
326 if (!thread->dsts)
327 goto err_dsts;
328 for (i = 0; i < dst_cnt; i++) {
329 thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
330 if (!thread->dsts[i])
331 goto err_dstbuf;
332 }
333 thread->dsts[i] = NULL;
334
Dan Williamse44e0aa2009-03-25 09:13:25 -0700335 set_user_nice(current, 10);
336
Ira Snyderb203bd32011-03-03 07:54:53 +0000337 /*
338 * src buffers are freed by the DMAEngine code with dma_unmap_single()
339 * dst buffers are freed by ourselves below
340 */
341 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
342 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700343
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200344 while (!kthread_should_stop()
345 && !(iterations && total_tests >= iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700346 struct dma_async_tx_descriptor *tx = NULL;
347 dma_addr_t dma_srcs[src_cnt];
348 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700349 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700350
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700351 total_tests++;
352
Dan Williams83544ae2009-09-08 17:42:53 -0700353 /* honor alignment restrictions */
354 if (thread->type == DMA_MEMCPY)
355 align = dev->copy_align;
356 else if (thread->type == DMA_XOR)
357 align = dev->xor_align;
358 else if (thread->type == DMA_PQ)
359 align = dev->pq_align;
360
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100361 if (1 << align > test_buf_size) {
362 pr_err("%u-byte buffer too small for %d-byte alignment\n",
363 test_buf_size, 1 << align);
364 break;
365 }
366
367 len = dmatest_random() % test_buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700368 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100369 if (!len)
370 len = 1 << align;
371 src_off = dmatest_random() % (test_buf_size - len + 1);
372 dst_off = dmatest_random() % (test_buf_size - len + 1);
373
Dan Williams83544ae2009-09-08 17:42:53 -0700374 src_off = (src_off >> align) << align;
375 dst_off = (dst_off >> align) << align;
376
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700377 dmatest_init_srcs(thread->srcs, src_off, len);
378 dmatest_init_dsts(thread->dsts, dst_off, len);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700379
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700380 for (i = 0; i < src_cnt; i++) {
381 u8 *buf = thread->srcs[i] + src_off;
382
383 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
384 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800385 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
386 if (ret) {
387 unmap_src(dev->dev, dma_srcs, len, i);
388 pr_warn("%s: #%u: mapping error %d with "
389 "src_off=0x%x len=0x%x\n",
390 thread_name, total_tests - 1, ret,
391 src_off, len);
392 failed_tests++;
393 continue;
394 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700395 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700396 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700397 for (i = 0; i < dst_cnt; i++) {
398 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
399 test_buf_size,
400 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800401 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
402 if (ret) {
403 unmap_src(dev->dev, dma_srcs, len, src_cnt);
404 unmap_dst(dev->dev, dma_dsts, test_buf_size, i);
405 pr_warn("%s: #%u: mapping error %d with "
406 "dst_off=0x%x len=0x%x\n",
407 thread_name, total_tests - 1, ret,
408 dst_off, test_buf_size);
409 failed_tests++;
410 continue;
411 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700412 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700413
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700414 if (thread->type == DMA_MEMCPY)
415 tx = dev->device_prep_dma_memcpy(chan,
416 dma_dsts[0] + dst_off,
417 dma_srcs[0], len,
418 flags);
419 else if (thread->type == DMA_XOR)
420 tx = dev->device_prep_dma_xor(chan,
421 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700422 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700423 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700424 else if (thread->type == DMA_PQ) {
425 dma_addr_t dma_pq[dst_cnt];
426
427 for (i = 0; i < dst_cnt; i++)
428 dma_pq[i] = dma_dsts[i] + dst_off;
429 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100430 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700431 len, flags);
432 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700433
Atsushi Nemotod86be862009-01-13 09:22:20 -0700434 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800435 unmap_src(dev->dev, dma_srcs, len, src_cnt);
436 unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700437 pr_warning("%s: #%u: prep error with src_off=0x%x "
438 "dst_off=0x%x len=0x%x\n",
439 thread_name, total_tests - 1,
440 src_off, dst_off, len);
441 msleep(100);
442 failed_tests++;
443 continue;
444 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700445
Tejun Heoadfa5432011-11-23 09:28:16 -0800446 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700447 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800448 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700449 cookie = tx->tx_submit(tx);
450
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700451 if (dma_submit_error(cookie)) {
452 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
453 "dst_off=0x%x len=0x%x\n",
454 thread_name, total_tests - 1, cookie,
455 src_off, dst_off, len);
456 msleep(100);
457 failed_tests++;
458 continue;
459 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700460 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700461
Andy Shevchenko77101ce2013-03-04 11:09:25 +0200462 wait_event_freezable_timeout(done_wait,
463 done.done || kthread_should_stop(),
Tejun Heoadfa5432011-11-23 09:28:16 -0800464 msecs_to_jiffies(timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200465
Dan Williamse44e0aa2009-03-25 09:13:25 -0700466 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700467
Tejun Heoadfa5432011-11-23 09:28:16 -0800468 if (!done.done) {
469 /*
470 * We're leaving the timed out dma operation with
471 * dangling pointer to done_wait. To make this
472 * correct, we'll need to allocate wait_done for
473 * each test iteration and perform "who's gonna
474 * free it this time?" dancing. For now, just
475 * leave it dangling.
476 */
Dan Williamse44e0aa2009-03-25 09:13:25 -0700477 pr_warning("%s: #%u: test timed out\n",
478 thread_name, total_tests - 1);
479 failed_tests++;
480 continue;
481 } else if (status != DMA_SUCCESS) {
482 pr_warning("%s: #%u: got completion callback,"
483 " but status is \'%s\'\n",
484 thread_name, total_tests - 1,
485 status == DMA_ERROR ? "error" : "in progress");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700486 failed_tests++;
487 continue;
488 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700489
Atsushi Nemotod86be862009-01-13 09:22:20 -0700490 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Andy Shevchenko632fd282012-12-17 15:59:52 -0800491 unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700492
493 error_count = 0;
494
495 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700496 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700497 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700498 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700499 src_off + len, src_off,
500 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700501 error_count += dmatest_verify(thread->srcs, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700502 test_buf_size, src_off + len,
503 PATTERN_SRC, true);
504
505 pr_debug("%s: verifying dest buffer...\n",
506 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700507 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700508 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700509 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700510 dst_off + len, src_off,
511 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700512 error_count += dmatest_verify(thread->dsts, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700513 test_buf_size, dst_off + len,
514 PATTERN_DST, false);
515
516 if (error_count) {
517 pr_warning("%s: #%u: %u errors with "
518 "src_off=0x%x dst_off=0x%x len=0x%x\n",
519 thread_name, total_tests - 1, error_count,
520 src_off, dst_off, len);
521 failed_tests++;
522 } else {
523 pr_debug("%s: #%u: No errors with "
524 "src_off=0x%x dst_off=0x%x len=0x%x\n",
525 thread_name, total_tests - 1,
526 src_off, dst_off, len);
527 }
528 }
529
530 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700531 for (i = 0; thread->dsts[i]; i++)
532 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700533err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700534 kfree(thread->dsts);
535err_dsts:
536 for (i = 0; thread->srcs[i]; i++)
537 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700538err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700539 kfree(thread->srcs);
540err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200541 kfree(pq_coefs);
542err_thread_type:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700543 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
544 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200545
Viresh Kumar9704efa2011-07-29 16:21:57 +0530546 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000547 if (ret)
548 dmaengine_terminate_all(chan);
549
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200550 if (iterations > 0)
551 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800552 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200553 interruptible_sleep_on(&wait_dmatest_exit);
554 }
555
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700556 return ret;
557}
558
559static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
560{
561 struct dmatest_thread *thread;
562 struct dmatest_thread *_thread;
563 int ret;
564
565 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
566 ret = kthread_stop(thread->task);
567 pr_debug("dmatest: thread %s exited with status %d\n",
568 thread->task->comm, ret);
569 list_del(&thread->node);
570 kfree(thread);
571 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530572
573 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000574 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530575
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700576 kfree(dtc);
577}
578
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700579static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
580{
581 struct dmatest_thread *thread;
582 struct dma_chan *chan = dtc->chan;
583 char *op;
584 unsigned int i;
585
586 if (type == DMA_MEMCPY)
587 op = "copy";
588 else if (type == DMA_XOR)
589 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700590 else if (type == DMA_PQ)
591 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700592 else
593 return -EINVAL;
594
595 for (i = 0; i < threads_per_chan; i++) {
596 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
597 if (!thread) {
598 pr_warning("dmatest: No memory for %s-%s%u\n",
599 dma_chan_name(chan), op, i);
600
601 break;
602 }
603 thread->chan = dtc->chan;
604 thread->type = type;
605 smp_wmb();
606 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
607 dma_chan_name(chan), op, i);
608 if (IS_ERR(thread->task)) {
609 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
610 dma_chan_name(chan), op, i);
611 kfree(thread);
612 break;
613 }
614
615 /* srcbuf and dstbuf are allocated by the thread itself */
616
617 list_add_tail(&thread->node, &dtc->threads);
618 }
619
620 return i;
621}
622
Dan Williams33df8ca2009-01-06 11:38:15 -0700623static int dmatest_add_channel(struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700624{
625 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700626 struct dma_device *dma_dev = chan->device;
627 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400628 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700629
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700630 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700631 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700632 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700633 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700634 }
635
636 dtc->chan = chan;
637 INIT_LIST_HEAD(&dtc->threads);
638
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700639 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
640 cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200641 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700642 }
643 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
644 cnt = dmatest_add_threads(dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200645 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700646 }
Dan Williams58691d62009-08-29 19:09:27 -0700647 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
648 cnt = dmatest_add_threads(dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700649 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700650 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700651
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700652 pr_info("dmatest: Started %u threads using %s\n",
653 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700654
655 list_add_tail(&dtc->node, &dmatest_channels);
656 nr_channels++;
657
Dan Williams33df8ca2009-01-06 11:38:15 -0700658 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700659}
660
Dan Williams7dd60252009-01-06 11:38:19 -0700661static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700662{
Dan Williams33df8ca2009-01-06 11:38:15 -0700663 if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700664 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700665 else
Dan Williams7dd60252009-01-06 11:38:19 -0700666 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700667}
668
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700669static int __init dmatest_init(void)
670{
Dan Williams33df8ca2009-01-06 11:38:15 -0700671 dma_cap_mask_t mask;
672 struct dma_chan *chan;
673 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700674
Dan Williams33df8ca2009-01-06 11:38:15 -0700675 dma_cap_zero(mask);
676 dma_cap_set(DMA_MEMCPY, mask);
677 for (;;) {
678 chan = dma_request_channel(mask, filter, NULL);
679 if (chan) {
680 err = dmatest_add_channel(chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700681 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700682 dma_release_channel(chan);
683 break; /* add_channel failed, punt */
684 }
685 } else
686 break; /* no more channels available */
687 if (max_channels && nr_channels >= max_channels)
688 break; /* we have all we need */
689 }
690
691 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700692}
Dan Williams33df8ca2009-01-06 11:38:15 -0700693/* when compiled-in wait for drivers to load first */
694late_initcall(dmatest_init);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700695
696static void __exit dmatest_exit(void)
697{
Dan Williams33df8ca2009-01-06 11:38:15 -0700698 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700699 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700700
701 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
702 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700703 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700704 dmatest_cleanup_channel(dtc);
705 pr_debug("dmatest: dropped channel %s\n",
Dan Williams7cbd4872009-03-04 16:06:03 -0700706 dma_chan_name(chan));
707 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700708 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700709}
710module_exit(dmatest_exit);
711
Jean Delvaree05503e2011-05-18 16:49:24 +0200712MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700713MODULE_LICENSE("GPL v2");