blob: 7f9e3cc9361bf9e4974c120b7c7da66e76e730e7 [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
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020081struct dmatest_info;
82
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070083struct dmatest_thread {
84 struct list_head node;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020085 struct dmatest_info *info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070086 struct task_struct *task;
87 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070088 u8 **srcs;
89 u8 **dsts;
90 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070091};
92
93struct dmatest_chan {
94 struct list_head node;
95 struct dma_chan *chan;
96 struct list_head threads;
97};
98
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020099/**
100 * struct dmatest_info - test information.
101 * @buf_size: size of the memcpy test buffer
102 * @channel: bus ID of the channel to test
103 * @device: bus ID of the DMA Engine to test
104 * @threads_per_chan: number of threads to start per channel
105 * @max_channels: maximum number of channels to use
106 * @iterations: iterations before stopping test
107 * @xor_sources: number of xor source buffers
108 * @pq_sources: number of p+q source buffers
109 * @timeout: transfer timeout in msec, -1 for infinite timeout
110 */
111struct dmatest_info {
112 unsigned int buf_size;
113 char channel[20];
114 char device[20];
115 unsigned int threads_per_chan;
116 unsigned int max_channels;
117 unsigned int iterations;
118 unsigned int xor_sources;
119 unsigned int pq_sources;
120 int timeout;
121};
122
123static struct dmatest_info test_info;
124
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700125/*
126 * These are protected by dma_list_mutex since they're only used by
Dan Williams33df8ca2009-01-06 11:38:15 -0700127 * the DMA filter function callback
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700128 */
129static LIST_HEAD(dmatest_channels);
130static unsigned int nr_channels;
131
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200132static bool dmatest_match_channel(struct dmatest_info *info,
133 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700134{
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200135 if (info->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700136 return true;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200137 return strcmp(dma_chan_name(chan), info->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700138}
139
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200140static bool dmatest_match_device(struct dmatest_info *info,
141 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700142{
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200143 if (info->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700144 return true;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200145 return strcmp(dev_name(device->dev), info->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700146}
147
148static unsigned long dmatest_random(void)
149{
150 unsigned long buf;
151
152 get_random_bytes(&buf, sizeof(buf));
153 return buf;
154}
155
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200156static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
157 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700158{
159 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700160 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700161
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700162 for (; (buf = *bufs); bufs++) {
163 for (i = 0; i < start; i++)
164 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
165 for ( ; i < start + len; i++)
166 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700167 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200168 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700169 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
170 buf++;
171 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700172}
173
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200174static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
175 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700176{
177 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700178 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700179
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700180 for (; (buf = *bufs); bufs++) {
181 for (i = 0; i < start; i++)
182 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
183 for ( ; i < start + len; i++)
184 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
185 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200186 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700187 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
188 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700189}
190
191static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
192 unsigned int counter, bool is_srcbuf)
193{
194 u8 diff = actual ^ pattern;
195 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
196 const char *thread_name = current->comm;
197
198 if (is_srcbuf)
199 pr_warning("%s: srcbuf[0x%x] overwritten!"
200 " Expected %02x, got %02x\n",
201 thread_name, index, expected, actual);
202 else if ((pattern & PATTERN_COPY)
203 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
204 pr_warning("%s: dstbuf[0x%x] not copied!"
205 " Expected %02x, got %02x\n",
206 thread_name, index, expected, actual);
207 else if (diff & PATTERN_SRC)
208 pr_warning("%s: dstbuf[0x%x] was copied!"
209 " Expected %02x, got %02x\n",
210 thread_name, index, expected, actual);
211 else
212 pr_warning("%s: dstbuf[0x%x] mismatch!"
213 " Expected %02x, got %02x\n",
214 thread_name, index, expected, actual);
215}
216
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700217static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700218 unsigned int end, unsigned int counter, u8 pattern,
219 bool is_srcbuf)
220{
221 unsigned int i;
222 unsigned int error_count = 0;
223 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700224 u8 expected;
225 u8 *buf;
226 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700227
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700228 for (; (buf = *bufs); bufs++) {
229 counter = counter_orig;
230 for (i = start; i < end; i++) {
231 actual = buf[i];
232 expected = pattern | (~counter & PATTERN_COUNT_MASK);
233 if (actual != expected) {
234 if (error_count < 32)
235 dmatest_mismatch(actual, pattern, i,
236 counter, is_srcbuf);
237 error_count++;
238 }
239 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700240 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700241 }
242
243 if (error_count > 32)
244 pr_warning("%s: %u errors suppressed\n",
245 current->comm, error_count - 32);
246
247 return error_count;
248}
249
Tejun Heoadfa5432011-11-23 09:28:16 -0800250/* poor man's completion - we want to use wait_event_freezable() on it */
251struct dmatest_done {
252 bool done;
253 wait_queue_head_t *wait;
254};
255
256static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700257{
Tejun Heoadfa5432011-11-23 09:28:16 -0800258 struct dmatest_done *done = arg;
259
260 done->done = true;
261 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700262}
263
Andy Shevchenko632fd282012-12-17 15:59:52 -0800264static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
265 unsigned int count)
266{
267 while (count--)
268 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
269}
270
271static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
272 unsigned int count)
273{
274 while (count--)
275 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
276}
277
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900278static unsigned int min_odd(unsigned int x, unsigned int y)
279{
280 unsigned int val = min(x, y);
281
282 return val % 2 ? val : val - 1;
283}
284
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700285/*
286 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700287 * offsets for a given operation type until it is told to exit by
288 * kthread_stop(). There may be multiple threads running this function
289 * in parallel for a single channel, and there may be multiple channels
290 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700291 *
292 * Before each test, the source and destination buffer is initialized
293 * with a known pattern. This pattern is different depending on
294 * whether it's in an area which is supposed to be copied or
295 * overwritten, and different in the source and destination buffers.
296 * So if the DMA engine doesn't copy exactly what we tell it to copy,
297 * we'll notice.
298 */
299static int dmatest_func(void *data)
300{
Tejun Heoadfa5432011-11-23 09:28:16 -0800301 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700302 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800303 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200304 struct dmatest_info *info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700305 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900306 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700307 const char *thread_name;
308 unsigned int src_off, dst_off, len;
309 unsigned int error_count;
310 unsigned int failed_tests = 0;
311 unsigned int total_tests = 0;
312 dma_cookie_t cookie;
313 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700314 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200315 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700316 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700317 int src_cnt;
318 int dst_cnt;
319 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700320
321 thread_name = current->comm;
Tejun Heoadfa5432011-11-23 09:28:16 -0800322 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700323
324 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700325
326 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200327 info = thread->info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700328 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900329 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700330 if (thread->type == DMA_MEMCPY)
331 src_cnt = dst_cnt = 1;
332 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900333 /* force odd to ensure dst = src */
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200334 src_cnt = min_odd(info->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700335 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700336 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900337 /* force odd to ensure dst = src */
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200338 src_cnt = min_odd(info->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700339 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200340
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200341 pq_coefs = kmalloc(info->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200342 if (!pq_coefs)
343 goto err_thread_type;
344
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100345 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700346 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700347 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200348 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700349
350 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
351 if (!thread->srcs)
352 goto err_srcs;
353 for (i = 0; i < src_cnt; i++) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200354 thread->srcs[i] = kmalloc(info->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700355 if (!thread->srcs[i])
356 goto err_srcbuf;
357 }
358 thread->srcs[i] = NULL;
359
360 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
361 if (!thread->dsts)
362 goto err_dsts;
363 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200364 thread->dsts[i] = kmalloc(info->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700365 if (!thread->dsts[i])
366 goto err_dstbuf;
367 }
368 thread->dsts[i] = NULL;
369
Dan Williamse44e0aa2009-03-25 09:13:25 -0700370 set_user_nice(current, 10);
371
Ira Snyderb203bd32011-03-03 07:54:53 +0000372 /*
373 * src buffers are freed by the DMAEngine code with dma_unmap_single()
374 * dst buffers are freed by ourselves below
375 */
376 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
377 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700378
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200379 while (!kthread_should_stop()
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200380 && !(info->iterations && total_tests >= info->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700381 struct dma_async_tx_descriptor *tx = NULL;
382 dma_addr_t dma_srcs[src_cnt];
383 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700384 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700385
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700386 total_tests++;
387
Dan Williams83544ae2009-09-08 17:42:53 -0700388 /* honor alignment restrictions */
389 if (thread->type == DMA_MEMCPY)
390 align = dev->copy_align;
391 else if (thread->type == DMA_XOR)
392 align = dev->xor_align;
393 else if (thread->type == DMA_PQ)
394 align = dev->pq_align;
395
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200396 if (1 << align > info->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100397 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200398 info->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100399 break;
400 }
401
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200402 len = dmatest_random() % info->buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700403 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100404 if (!len)
405 len = 1 << align;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200406 src_off = dmatest_random() % (info->buf_size - len + 1);
407 dst_off = dmatest_random() % (info->buf_size - len + 1);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100408
Dan Williams83544ae2009-09-08 17:42:53 -0700409 src_off = (src_off >> align) << align;
410 dst_off = (dst_off >> align) << align;
411
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200412 dmatest_init_srcs(thread->srcs, src_off, len, info->buf_size);
413 dmatest_init_dsts(thread->dsts, dst_off, len, info->buf_size);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700414
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700415 for (i = 0; i < src_cnt; i++) {
416 u8 *buf = thread->srcs[i] + src_off;
417
418 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
419 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800420 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
421 if (ret) {
422 unmap_src(dev->dev, dma_srcs, len, i);
423 pr_warn("%s: #%u: mapping error %d with "
424 "src_off=0x%x len=0x%x\n",
425 thread_name, total_tests - 1, ret,
426 src_off, len);
427 failed_tests++;
428 continue;
429 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700430 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700431 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700432 for (i = 0; i < dst_cnt; i++) {
433 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200434 info->buf_size,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700435 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800436 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
437 if (ret) {
438 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200439 unmap_dst(dev->dev, dma_dsts, info->buf_size, i);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800440 pr_warn("%s: #%u: mapping error %d with "
441 "dst_off=0x%x len=0x%x\n",
442 thread_name, total_tests - 1, ret,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200443 dst_off, info->buf_size);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800444 failed_tests++;
445 continue;
446 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700447 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700448
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700449 if (thread->type == DMA_MEMCPY)
450 tx = dev->device_prep_dma_memcpy(chan,
451 dma_dsts[0] + dst_off,
452 dma_srcs[0], len,
453 flags);
454 else if (thread->type == DMA_XOR)
455 tx = dev->device_prep_dma_xor(chan,
456 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700457 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700458 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700459 else if (thread->type == DMA_PQ) {
460 dma_addr_t dma_pq[dst_cnt];
461
462 for (i = 0; i < dst_cnt; i++)
463 dma_pq[i] = dma_dsts[i] + dst_off;
464 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100465 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700466 len, flags);
467 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700468
Atsushi Nemotod86be862009-01-13 09:22:20 -0700469 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800470 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200471 unmap_dst(dev->dev, dma_dsts, info->buf_size, dst_cnt);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700472 pr_warning("%s: #%u: prep error with src_off=0x%x "
473 "dst_off=0x%x len=0x%x\n",
474 thread_name, total_tests - 1,
475 src_off, dst_off, len);
476 msleep(100);
477 failed_tests++;
478 continue;
479 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700480
Tejun Heoadfa5432011-11-23 09:28:16 -0800481 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700482 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800483 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700484 cookie = tx->tx_submit(tx);
485
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700486 if (dma_submit_error(cookie)) {
487 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
488 "dst_off=0x%x len=0x%x\n",
489 thread_name, total_tests - 1, cookie,
490 src_off, dst_off, len);
491 msleep(100);
492 failed_tests++;
493 continue;
494 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700495 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700496
Andy Shevchenko77101ce2013-03-04 11:09:25 +0200497 wait_event_freezable_timeout(done_wait,
498 done.done || kthread_should_stop(),
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200499 msecs_to_jiffies(info->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200500
Dan Williamse44e0aa2009-03-25 09:13:25 -0700501 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700502
Tejun Heoadfa5432011-11-23 09:28:16 -0800503 if (!done.done) {
504 /*
505 * We're leaving the timed out dma operation with
506 * dangling pointer to done_wait. To make this
507 * correct, we'll need to allocate wait_done for
508 * each test iteration and perform "who's gonna
509 * free it this time?" dancing. For now, just
510 * leave it dangling.
511 */
Dan Williamse44e0aa2009-03-25 09:13:25 -0700512 pr_warning("%s: #%u: test timed out\n",
513 thread_name, total_tests - 1);
514 failed_tests++;
515 continue;
516 } else if (status != DMA_SUCCESS) {
517 pr_warning("%s: #%u: got completion callback,"
518 " but status is \'%s\'\n",
519 thread_name, total_tests - 1,
520 status == DMA_ERROR ? "error" : "in progress");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700521 failed_tests++;
522 continue;
523 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700524
Atsushi Nemotod86be862009-01-13 09:22:20 -0700525 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200526 unmap_dst(dev->dev, dma_dsts, info->buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700527
528 error_count = 0;
529
530 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700531 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700532 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700533 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700534 src_off + len, src_off,
535 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700536 error_count += dmatest_verify(thread->srcs, src_off + len,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200537 info->buf_size, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700538 PATTERN_SRC, true);
539
540 pr_debug("%s: verifying dest buffer...\n",
541 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700542 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700543 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700544 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700545 dst_off + len, src_off,
546 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700547 error_count += dmatest_verify(thread->dsts, dst_off + len,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200548 info->buf_size, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700549 PATTERN_DST, false);
550
551 if (error_count) {
552 pr_warning("%s: #%u: %u errors with "
553 "src_off=0x%x dst_off=0x%x len=0x%x\n",
554 thread_name, total_tests - 1, error_count,
555 src_off, dst_off, len);
556 failed_tests++;
557 } else {
558 pr_debug("%s: #%u: No errors with "
559 "src_off=0x%x dst_off=0x%x len=0x%x\n",
560 thread_name, total_tests - 1,
561 src_off, dst_off, len);
562 }
563 }
564
565 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700566 for (i = 0; thread->dsts[i]; i++)
567 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700568err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700569 kfree(thread->dsts);
570err_dsts:
571 for (i = 0; thread->srcs[i]; i++)
572 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700573err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700574 kfree(thread->srcs);
575err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200576 kfree(pq_coefs);
577err_thread_type:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700578 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
579 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200580
Viresh Kumar9704efa2011-07-29 16:21:57 +0530581 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000582 if (ret)
583 dmaengine_terminate_all(chan);
584
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200585 if (info->iterations > 0)
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200586 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800587 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200588 interruptible_sleep_on(&wait_dmatest_exit);
589 }
590
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700591 return ret;
592}
593
594static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
595{
596 struct dmatest_thread *thread;
597 struct dmatest_thread *_thread;
598 int ret;
599
600 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
601 ret = kthread_stop(thread->task);
602 pr_debug("dmatest: thread %s exited with status %d\n",
603 thread->task->comm, ret);
604 list_del(&thread->node);
605 kfree(thread);
606 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530607
608 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000609 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530610
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700611 kfree(dtc);
612}
613
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200614static int dmatest_add_threads(struct dmatest_info *info,
615 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700616{
617 struct dmatest_thread *thread;
618 struct dma_chan *chan = dtc->chan;
619 char *op;
620 unsigned int i;
621
622 if (type == DMA_MEMCPY)
623 op = "copy";
624 else if (type == DMA_XOR)
625 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700626 else if (type == DMA_PQ)
627 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700628 else
629 return -EINVAL;
630
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200631 for (i = 0; i < info->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700632 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
633 if (!thread) {
634 pr_warning("dmatest: No memory for %s-%s%u\n",
635 dma_chan_name(chan), op, i);
636
637 break;
638 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200639 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700640 thread->chan = dtc->chan;
641 thread->type = type;
642 smp_wmb();
643 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
644 dma_chan_name(chan), op, i);
645 if (IS_ERR(thread->task)) {
646 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
647 dma_chan_name(chan), op, i);
648 kfree(thread);
649 break;
650 }
651
652 /* srcbuf and dstbuf are allocated by the thread itself */
653
654 list_add_tail(&thread->node, &dtc->threads);
655 }
656
657 return i;
658}
659
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200660static int dmatest_add_channel(struct dmatest_info *info,
661 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700662{
663 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700664 struct dma_device *dma_dev = chan->device;
665 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400666 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700667
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700668 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700669 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700670 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700671 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700672 }
673
674 dtc->chan = chan;
675 INIT_LIST_HEAD(&dtc->threads);
676
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700677 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200678 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200679 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700680 }
681 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200682 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200683 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700684 }
Dan Williams58691d62009-08-29 19:09:27 -0700685 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200686 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700687 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700688 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700689
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700690 pr_info("dmatest: Started %u threads using %s\n",
691 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700692
693 list_add_tail(&dtc->node, &dmatest_channels);
694 nr_channels++;
695
Dan Williams33df8ca2009-01-06 11:38:15 -0700696 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700697}
698
Dan Williams7dd60252009-01-06 11:38:19 -0700699static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700700{
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200701 struct dmatest_info *info = param;
702
703 if (!dmatest_match_channel(info, chan) ||
704 !dmatest_match_device(info, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700705 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700706 else
Dan Williams7dd60252009-01-06 11:38:19 -0700707 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700708}
709
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200710static int run_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700711{
Dan Williams33df8ca2009-01-06 11:38:15 -0700712 dma_cap_mask_t mask;
713 struct dma_chan *chan;
714 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700715
Dan Williams33df8ca2009-01-06 11:38:15 -0700716 dma_cap_zero(mask);
717 dma_cap_set(DMA_MEMCPY, mask);
718 for (;;) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200719 chan = dma_request_channel(mask, filter, info);
Dan Williams33df8ca2009-01-06 11:38:15 -0700720 if (chan) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200721 err = dmatest_add_channel(info, chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700722 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700723 dma_release_channel(chan);
724 break; /* add_channel failed, punt */
725 }
726 } else
727 break; /* no more channels available */
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200728 if (info->max_channels && nr_channels >= info->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700729 break; /* we have all we need */
730 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700731 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700732}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700733
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200734static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700735{
Dan Williams33df8ca2009-01-06 11:38:15 -0700736 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700737 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700738
739 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
740 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700741 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700742 dmatest_cleanup_channel(dtc);
743 pr_debug("dmatest: dropped channel %s\n",
Dan Williams7cbd4872009-03-04 16:06:03 -0700744 dma_chan_name(chan));
745 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700746 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700747}
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200748
749static int __init dmatest_init(void)
750{
751 struct dmatest_info *info = &test_info;
752
753 memset(info, 0, sizeof(*info));
754
755 info->buf_size = test_buf_size;
756 strlcpy(info->channel, test_channel, sizeof(info->channel));
757 strlcpy(info->device, test_device, sizeof(info->device));
758 info->threads_per_chan = threads_per_chan;
759 info->max_channels = max_channels;
760 info->iterations = iterations;
761 info->xor_sources = xor_sources;
762 info->pq_sources = pq_sources;
763 info->timeout = timeout;
764
765 return run_threaded_test(info);
766}
767/* when compiled-in wait for drivers to load first */
768late_initcall(dmatest_init);
769
770static void __exit dmatest_exit(void)
771{
772 struct dmatest_info *info = &test_info;
773
774 stop_threaded_test(info);
775}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700776module_exit(dmatest_exit);
777
Jean Delvaree05503e2011-05-18 16:49:24 +0200778MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700779MODULE_LICENSE("GPL v2");