blob: 91716f404c039bbb4cb960e54f6e78ff3d74c168 [file] [log] [blame]
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001/*
2 * DMA Engine test module
3 *
4 * Copyright (C) 2007 Atmel Corporation
Andy Shevchenko851b7e12013-03-04 11:09:30 +02005 * Copyright (C) 2013 Intel Corporation
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/delay.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000012#include <linux/dma-mapping.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070013#include <linux/dmaengine.h>
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +020014#include <linux/freezer.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070015#include <linux/init.h>
16#include <linux/kthread.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070021#include <linux/wait.h>
Andy Shevchenko851b7e12013-03-04 11:09:30 +020022#include <linux/ctype.h>
23#include <linux/debugfs.h>
24#include <linux/uaccess.h>
25#include <linux/seq_file.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070026
27static unsigned int test_buf_size = 16384;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030028module_param(test_buf_size, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070029MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
30
Kay Sievers06190d82008-11-11 13:12:33 -070031static char test_channel[20];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030032module_param_string(channel, test_channel, sizeof(test_channel),
33 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070034MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
35
Kay Sievers06190d82008-11-11 13:12:33 -070036static char test_device[20];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030037module_param_string(device, test_device, sizeof(test_device),
38 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070039MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
40
41static unsigned int threads_per_chan = 1;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030042module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070043MODULE_PARM_DESC(threads_per_chan,
44 "Number of threads to start per channel (default: 1)");
45
46static unsigned int max_channels;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030047module_param(max_channels, uint, S_IRUGO | S_IWUSR);
Dan Williams33df8ca2009-01-06 11:38:15 -070048MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070049 "Maximum number of channels to use (default: all)");
50
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020051static unsigned int iterations;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030052module_param(iterations, uint, S_IRUGO | S_IWUSR);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020053MODULE_PARM_DESC(iterations,
54 "Iterations before stopping test (default: infinite)");
55
Dan Williamsb54d5cb2009-03-25 09:13:25 -070056static unsigned int xor_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030057module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
Dan Williamsb54d5cb2009-03-25 09:13:25 -070058MODULE_PARM_DESC(xor_sources,
59 "Number of xor source buffers (default: 3)");
60
Dan Williams58691d62009-08-29 19:09:27 -070061static unsigned int pq_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030062module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
Dan Williams58691d62009-08-29 19:09:27 -070063MODULE_PARM_DESC(pq_sources,
64 "Number of p+q source buffers (default: 3)");
65
Viresh Kumard42efe62011-03-22 17:27:25 +053066static int timeout = 3000;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030067module_param(timeout, uint, S_IRUGO | S_IWUSR);
Joe Perches85ee7a12011-04-23 20:38:19 -070068MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
69 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053070
Andy Shevchenko74b5c072013-03-04 11:09:32 +020071/* Maximum amount of mismatched bytes in buffer to print */
72#define MAX_ERROR_COUNT 32
73
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070074/*
75 * Initialization patterns. All bytes in the source buffer has bit 7
76 * set, all bytes in the destination buffer has bit 7 cleared.
77 *
78 * Bit 6 is set for all bytes which are to be copied by the DMA
79 * engine. Bit 5 is set for all bytes which are to be overwritten by
80 * the DMA engine.
81 *
82 * The remaining bits are the inverse of a counter which increments by
83 * one for each byte address.
84 */
85#define PATTERN_SRC 0x80
86#define PATTERN_DST 0x00
87#define PATTERN_COPY 0x40
88#define PATTERN_OVERWRITE 0x20
89#define PATTERN_COUNT_MASK 0x1f
90
Andy Shevchenko95019c82013-03-04 11:09:33 +020091enum dmatest_error_type {
92 DMATEST_ET_OK,
93 DMATEST_ET_MAP_SRC,
94 DMATEST_ET_MAP_DST,
95 DMATEST_ET_PREP,
96 DMATEST_ET_SUBMIT,
97 DMATEST_ET_TIMEOUT,
98 DMATEST_ET_DMA_ERROR,
99 DMATEST_ET_DMA_IN_PROGRESS,
100 DMATEST_ET_VERIFY,
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200101 DMATEST_ET_VERIFY_BUF,
102};
103
104struct dmatest_verify_buffer {
105 unsigned int index;
106 u8 expected;
107 u8 actual;
108};
109
110struct dmatest_verify_result {
111 unsigned int error_count;
112 struct dmatest_verify_buffer data[MAX_ERROR_COUNT];
113 u8 pattern;
114 bool is_srcbuf;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200115};
116
117struct dmatest_thread_result {
118 struct list_head node;
119 unsigned int n;
120 unsigned int src_off;
121 unsigned int dst_off;
122 unsigned int len;
123 enum dmatest_error_type type;
124 union {
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200125 unsigned long data;
126 dma_cookie_t cookie;
127 enum dma_status status;
128 int error;
129 struct dmatest_verify_result *vr;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200130 };
131};
132
133struct dmatest_result {
134 struct list_head node;
135 char *name;
136 struct list_head results;
137};
138
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200139struct dmatest_info;
140
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700141struct dmatest_thread {
142 struct list_head node;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200143 struct dmatest_info *info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700144 struct task_struct *task;
145 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700146 u8 **srcs;
147 u8 **dsts;
148 enum dma_transaction_type type;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200149 bool done;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700150};
151
152struct dmatest_chan {
153 struct list_head node;
154 struct dma_chan *chan;
155 struct list_head threads;
156};
157
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200158/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200159 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200160 * @buf_size: size of the memcpy test buffer
161 * @channel: bus ID of the channel to test
162 * @device: bus ID of the DMA Engine to test
163 * @threads_per_chan: number of threads to start per channel
164 * @max_channels: maximum number of channels to use
165 * @iterations: iterations before stopping test
166 * @xor_sources: number of xor source buffers
167 * @pq_sources: number of p+q source buffers
168 * @timeout: transfer timeout in msec, -1 for infinite timeout
169 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200170struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200171 unsigned int buf_size;
172 char channel[20];
173 char device[20];
174 unsigned int threads_per_chan;
175 unsigned int max_channels;
176 unsigned int iterations;
177 unsigned int xor_sources;
178 unsigned int pq_sources;
179 int timeout;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200180};
181
182/**
183 * struct dmatest_info - test information.
184 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200185 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200186 */
187struct dmatest_info {
188 /* Test parameters */
189 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200190
191 /* Internal state */
192 struct list_head channels;
193 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200194 struct mutex lock;
195
196 /* debugfs related stuff */
197 struct dentry *root;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200198
199 /* Test results */
200 struct list_head results;
201 struct mutex results_lock;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200202};
203
204static struct dmatest_info test_info;
205
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200206static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200207 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700208{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200209 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700210 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200211 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700212}
213
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200214static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200215 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700216{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200217 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700218 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200219 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700220}
221
222static unsigned long dmatest_random(void)
223{
224 unsigned long buf;
225
226 get_random_bytes(&buf, sizeof(buf));
227 return buf;
228}
229
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200230static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
231 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700232{
233 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700234 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700235
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700236 for (; (buf = *bufs); bufs++) {
237 for (i = 0; i < start; i++)
238 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
239 for ( ; i < start + len; i++)
240 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700241 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200242 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700243 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
244 buf++;
245 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700246}
247
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200248static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
249 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700250{
251 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700252 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700253
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700254 for (; (buf = *bufs); bufs++) {
255 for (i = 0; i < start; i++)
256 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
257 for ( ; i < start + len; i++)
258 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
259 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200260 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700261 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
262 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700263}
264
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200265static unsigned int dmatest_verify(struct dmatest_verify_result *vr, u8 **bufs,
266 unsigned int start, unsigned int end, unsigned int counter,
267 u8 pattern, bool is_srcbuf)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700268{
269 unsigned int i;
270 unsigned int error_count = 0;
271 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700272 u8 expected;
273 u8 *buf;
274 unsigned int counter_orig = counter;
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200275 struct dmatest_verify_buffer *vb;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700276
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700277 for (; (buf = *bufs); bufs++) {
278 counter = counter_orig;
279 for (i = start; i < end; i++) {
280 actual = buf[i];
281 expected = pattern | (~counter & PATTERN_COUNT_MASK);
282 if (actual != expected) {
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200283 if (error_count < MAX_ERROR_COUNT && vr) {
284 vb = &vr->data[error_count];
285 vb->index = i;
286 vb->expected = expected;
287 vb->actual = actual;
288 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700289 error_count++;
290 }
291 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700292 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700293 }
294
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200295 if (error_count > MAX_ERROR_COUNT)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700296 pr_warning("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200297 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700298
299 return error_count;
300}
301
Tejun Heoadfa5432011-11-23 09:28:16 -0800302/* poor man's completion - we want to use wait_event_freezable() on it */
303struct dmatest_done {
304 bool done;
305 wait_queue_head_t *wait;
306};
307
308static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700309{
Tejun Heoadfa5432011-11-23 09:28:16 -0800310 struct dmatest_done *done = arg;
311
312 done->done = true;
313 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700314}
315
Andy Shevchenko632fd282012-12-17 15:59:52 -0800316static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
317 unsigned int count)
318{
319 while (count--)
320 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
321}
322
323static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
324 unsigned int count)
325{
326 while (count--)
327 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
328}
329
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900330static unsigned int min_odd(unsigned int x, unsigned int y)
331{
332 unsigned int val = min(x, y);
333
334 return val % 2 ? val : val - 1;
335}
336
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200337static char *verify_result_get_one(struct dmatest_verify_result *vr,
338 unsigned int i)
339{
340 struct dmatest_verify_buffer *vb = &vr->data[i];
341 u8 diff = vb->actual ^ vr->pattern;
342 static char buf[512];
343 char *msg;
344
345 if (vr->is_srcbuf)
346 msg = "srcbuf overwritten!";
347 else if ((vr->pattern & PATTERN_COPY)
348 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
349 msg = "dstbuf not copied!";
350 else if (diff & PATTERN_SRC)
351 msg = "dstbuf was copied!";
352 else
353 msg = "dstbuf mismatch!";
354
355 snprintf(buf, sizeof(buf) - 1, "%s [0x%x] Expected %02x, got %02x", msg,
356 vb->index, vb->expected, vb->actual);
357
358 return buf;
359}
360
Andy Shevchenko95019c82013-03-04 11:09:33 +0200361static char *thread_result_get(const char *name,
362 struct dmatest_thread_result *tr)
363{
364 static const char * const messages[] = {
365 [DMATEST_ET_OK] = "No errors",
366 [DMATEST_ET_MAP_SRC] = "src mapping error",
367 [DMATEST_ET_MAP_DST] = "dst mapping error",
368 [DMATEST_ET_PREP] = "prep error",
369 [DMATEST_ET_SUBMIT] = "submit error",
370 [DMATEST_ET_TIMEOUT] = "test timed out",
371 [DMATEST_ET_DMA_ERROR] =
372 "got completion callback (DMA_ERROR)",
373 [DMATEST_ET_DMA_IN_PROGRESS] =
374 "got completion callback (DMA_IN_PROGRESS)",
375 [DMATEST_ET_VERIFY] = "errors",
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200376 [DMATEST_ET_VERIFY_BUF] = "verify errors",
Andy Shevchenko95019c82013-03-04 11:09:33 +0200377 };
378 static char buf[512];
379
380 snprintf(buf, sizeof(buf) - 1,
381 "%s: #%u: %s with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
382 name, tr->n, messages[tr->type], tr->src_off, tr->dst_off,
383 tr->len, tr->data);
384
385 return buf;
386}
387
388static int thread_result_add(struct dmatest_info *info,
389 struct dmatest_result *r, enum dmatest_error_type type,
390 unsigned int n, unsigned int src_off, unsigned int dst_off,
391 unsigned int len, unsigned long data)
392{
393 struct dmatest_thread_result *tr;
394
395 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
396 if (!tr)
397 return -ENOMEM;
398
399 tr->type = type;
400 tr->n = n;
401 tr->src_off = src_off;
402 tr->dst_off = dst_off;
403 tr->len = len;
404 tr->data = data;
405
406 mutex_lock(&info->results_lock);
407 list_add_tail(&tr->node, &r->results);
408 mutex_unlock(&info->results_lock);
409
410 pr_warn("%s\n", thread_result_get(r->name, tr));
411 return 0;
412}
413
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200414static unsigned int verify_result_add(struct dmatest_info *info,
415 struct dmatest_result *r, unsigned int n,
416 unsigned int src_off, unsigned int dst_off, unsigned int len,
417 u8 **bufs, int whence, unsigned int counter, u8 pattern,
418 bool is_srcbuf)
419{
420 struct dmatest_verify_result *vr;
421 unsigned int error_count;
422 unsigned int buf_off = is_srcbuf ? src_off : dst_off;
423 unsigned int start, end;
424
425 if (whence < 0) {
426 start = 0;
427 end = buf_off;
428 } else if (whence > 0) {
429 start = buf_off + len;
430 end = info->params.buf_size;
431 } else {
432 start = buf_off;
433 end = buf_off + len;
434 }
435
436 vr = kmalloc(sizeof(*vr), GFP_KERNEL);
437 if (!vr) {
438 pr_warn("dmatest: No memory to store verify result\n");
439 return dmatest_verify(NULL, bufs, start, end, counter, pattern,
440 is_srcbuf);
441 }
442
443 vr->pattern = pattern;
444 vr->is_srcbuf = is_srcbuf;
445
446 error_count = dmatest_verify(vr, bufs, start, end, counter, pattern,
447 is_srcbuf);
448 if (error_count) {
449 vr->error_count = error_count;
450 thread_result_add(info, r, DMATEST_ET_VERIFY_BUF, n, src_off,
451 dst_off, len, (unsigned long)vr);
452 return error_count;
453 }
454
455 kfree(vr);
456 return 0;
457}
458
Andy Shevchenko95019c82013-03-04 11:09:33 +0200459static void result_free(struct dmatest_info *info, const char *name)
460{
461 struct dmatest_result *r, *_r;
462
463 mutex_lock(&info->results_lock);
464 list_for_each_entry_safe(r, _r, &info->results, node) {
465 struct dmatest_thread_result *tr, *_tr;
466
467 if (name && strcmp(r->name, name))
468 continue;
469
470 list_for_each_entry_safe(tr, _tr, &r->results, node) {
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200471 if (tr->type == DMATEST_ET_VERIFY_BUF)
472 kfree(tr->vr);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200473 list_del(&tr->node);
474 kfree(tr);
475 }
476
477 kfree(r->name);
478 list_del(&r->node);
479 kfree(r);
480 }
481
482 mutex_unlock(&info->results_lock);
483}
484
485static struct dmatest_result *result_init(struct dmatest_info *info,
486 const char *name)
487{
488 struct dmatest_result *r;
489
490 r = kzalloc(sizeof(*r), GFP_KERNEL);
491 if (r) {
492 r->name = kstrdup(name, GFP_KERNEL);
493 INIT_LIST_HEAD(&r->results);
494 mutex_lock(&info->results_lock);
495 list_add_tail(&r->node, &info->results);
496 mutex_unlock(&info->results_lock);
497 }
498 return r;
499}
500
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700501/*
502 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700503 * offsets for a given operation type until it is told to exit by
504 * kthread_stop(). There may be multiple threads running this function
505 * in parallel for a single channel, and there may be multiple channels
506 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700507 *
508 * Before each test, the source and destination buffer is initialized
509 * with a known pattern. This pattern is different depending on
510 * whether it's in an area which is supposed to be copied or
511 * overwritten, and different in the source and destination buffers.
512 * So if the DMA engine doesn't copy exactly what we tell it to copy,
513 * we'll notice.
514 */
515static int dmatest_func(void *data)
516{
Tejun Heoadfa5432011-11-23 09:28:16 -0800517 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700518 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800519 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200520 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200521 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700522 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900523 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700524 const char *thread_name;
525 unsigned int src_off, dst_off, len;
526 unsigned int error_count;
527 unsigned int failed_tests = 0;
528 unsigned int total_tests = 0;
529 dma_cookie_t cookie;
530 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700531 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200532 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700533 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700534 int src_cnt;
535 int dst_cnt;
536 int i;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200537 struct dmatest_result *result;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700538
539 thread_name = current->comm;
Tejun Heoadfa5432011-11-23 09:28:16 -0800540 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700541
542 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700543
544 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200545 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200546 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700547 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900548 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700549 if (thread->type == DMA_MEMCPY)
550 src_cnt = dst_cnt = 1;
551 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900552 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200553 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700554 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700555 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900556 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200557 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700558 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200559
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200560 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200561 if (!pq_coefs)
562 goto err_thread_type;
563
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100564 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700565 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700566 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200567 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700568
Andy Shevchenko95019c82013-03-04 11:09:33 +0200569 result = result_init(info, thread_name);
570 if (!result)
571 goto err_srcs;
572
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700573 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
574 if (!thread->srcs)
575 goto err_srcs;
576 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200577 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700578 if (!thread->srcs[i])
579 goto err_srcbuf;
580 }
581 thread->srcs[i] = NULL;
582
583 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
584 if (!thread->dsts)
585 goto err_dsts;
586 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200587 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700588 if (!thread->dsts[i])
589 goto err_dstbuf;
590 }
591 thread->dsts[i] = NULL;
592
Dan Williamse44e0aa2009-03-25 09:13:25 -0700593 set_user_nice(current, 10);
594
Ira Snyderb203bd32011-03-03 07:54:53 +0000595 /*
596 * src buffers are freed by the DMAEngine code with dma_unmap_single()
597 * dst buffers are freed by ourselves below
598 */
599 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
600 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700601
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200602 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200603 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700604 struct dma_async_tx_descriptor *tx = NULL;
605 dma_addr_t dma_srcs[src_cnt];
606 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700607 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700608
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700609 total_tests++;
610
Dan Williams83544ae2009-09-08 17:42:53 -0700611 /* honor alignment restrictions */
612 if (thread->type == DMA_MEMCPY)
613 align = dev->copy_align;
614 else if (thread->type == DMA_XOR)
615 align = dev->xor_align;
616 else if (thread->type == DMA_PQ)
617 align = dev->pq_align;
618
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200619 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100620 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200621 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100622 break;
623 }
624
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200625 len = dmatest_random() % params->buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700626 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100627 if (!len)
628 len = 1 << align;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200629 src_off = dmatest_random() % (params->buf_size - len + 1);
630 dst_off = dmatest_random() % (params->buf_size - len + 1);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100631
Dan Williams83544ae2009-09-08 17:42:53 -0700632 src_off = (src_off >> align) << align;
633 dst_off = (dst_off >> align) << align;
634
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200635 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
636 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700637
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700638 for (i = 0; i < src_cnt; i++) {
639 u8 *buf = thread->srcs[i] + src_off;
640
641 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
642 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800643 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
644 if (ret) {
645 unmap_src(dev->dev, dma_srcs, len, i);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200646 thread_result_add(info, result,
647 DMATEST_ET_MAP_SRC,
648 total_tests, src_off, dst_off,
649 len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800650 failed_tests++;
651 continue;
652 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700653 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700654 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700655 for (i = 0; i < dst_cnt; i++) {
656 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200657 params->buf_size,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700658 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800659 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
660 if (ret) {
661 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200662 unmap_dst(dev->dev, dma_dsts, params->buf_size,
663 i);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200664 thread_result_add(info, result,
665 DMATEST_ET_MAP_DST,
666 total_tests, src_off, dst_off,
667 len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800668 failed_tests++;
669 continue;
670 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700671 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700672
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700673 if (thread->type == DMA_MEMCPY)
674 tx = dev->device_prep_dma_memcpy(chan,
675 dma_dsts[0] + dst_off,
676 dma_srcs[0], len,
677 flags);
678 else if (thread->type == DMA_XOR)
679 tx = dev->device_prep_dma_xor(chan,
680 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700681 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700682 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700683 else if (thread->type == DMA_PQ) {
684 dma_addr_t dma_pq[dst_cnt];
685
686 for (i = 0; i < dst_cnt; i++)
687 dma_pq[i] = dma_dsts[i] + dst_off;
688 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100689 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700690 len, flags);
691 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700692
Atsushi Nemotod86be862009-01-13 09:22:20 -0700693 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800694 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200695 unmap_dst(dev->dev, dma_dsts, params->buf_size,
696 dst_cnt);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200697 thread_result_add(info, result, DMATEST_ET_PREP,
698 total_tests, src_off, dst_off,
699 len, 0);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700700 msleep(100);
701 failed_tests++;
702 continue;
703 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700704
Tejun Heoadfa5432011-11-23 09:28:16 -0800705 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700706 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800707 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700708 cookie = tx->tx_submit(tx);
709
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700710 if (dma_submit_error(cookie)) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200711 thread_result_add(info, result, DMATEST_ET_SUBMIT,
712 total_tests, src_off, dst_off,
713 len, cookie);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700714 msleep(100);
715 failed_tests++;
716 continue;
717 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700718 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700719
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300720 wait_event_freezable_timeout(done_wait, done.done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200721 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200722
Dan Williamse44e0aa2009-03-25 09:13:25 -0700723 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700724
Tejun Heoadfa5432011-11-23 09:28:16 -0800725 if (!done.done) {
726 /*
727 * We're leaving the timed out dma operation with
728 * dangling pointer to done_wait. To make this
729 * correct, we'll need to allocate wait_done for
730 * each test iteration and perform "who's gonna
731 * free it this time?" dancing. For now, just
732 * leave it dangling.
733 */
Andy Shevchenko95019c82013-03-04 11:09:33 +0200734 thread_result_add(info, result, DMATEST_ET_TIMEOUT,
735 total_tests, src_off, dst_off,
736 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700737 failed_tests++;
738 continue;
739 } else if (status != DMA_SUCCESS) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200740 enum dmatest_error_type type = (status == DMA_ERROR) ?
741 DMATEST_ET_DMA_ERROR : DMATEST_ET_DMA_IN_PROGRESS;
742 thread_result_add(info, result, type,
743 total_tests, src_off, dst_off,
744 len, status);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700745 failed_tests++;
746 continue;
747 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700748
Atsushi Nemotod86be862009-01-13 09:22:20 -0700749 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200750 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700751
752 error_count = 0;
753
754 pr_debug("%s: verifying source buffer...\n", thread_name);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200755 error_count += verify_result_add(info, result, total_tests,
756 src_off, dst_off, len, thread->srcs, -1,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700757 0, PATTERN_SRC, true);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200758 error_count += verify_result_add(info, result, total_tests,
759 src_off, dst_off, len, thread->srcs, 0,
760 src_off, PATTERN_SRC | PATTERN_COPY, true);
761 error_count += verify_result_add(info, result, total_tests,
762 src_off, dst_off, len, thread->srcs, 1,
763 src_off + len, PATTERN_SRC, true);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700764
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200765 pr_debug("%s: verifying dest buffer...\n", thread_name);
766 error_count += verify_result_add(info, result, total_tests,
767 src_off, dst_off, len, thread->dsts, -1,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700768 0, PATTERN_DST, false);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200769 error_count += verify_result_add(info, result, total_tests,
770 src_off, dst_off, len, thread->dsts, 0,
771 src_off, PATTERN_SRC | PATTERN_COPY, false);
772 error_count += verify_result_add(info, result, total_tests,
773 src_off, dst_off, len, thread->dsts, 1,
774 dst_off + len, PATTERN_DST, false);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700775
776 if (error_count) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200777 thread_result_add(info, result, DMATEST_ET_VERIFY,
778 total_tests, src_off, dst_off,
779 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700780 failed_tests++;
781 } else {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200782 thread_result_add(info, result, DMATEST_ET_OK,
783 total_tests, src_off, dst_off,
784 len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700785 }
786 }
787
788 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700789 for (i = 0; thread->dsts[i]; i++)
790 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700791err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700792 kfree(thread->dsts);
793err_dsts:
794 for (i = 0; thread->srcs[i]; i++)
795 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700796err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700797 kfree(thread->srcs);
798err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200799 kfree(pq_coefs);
800err_thread_type:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700801 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
802 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200803
Viresh Kumar9704efa2011-07-29 16:21:57 +0530804 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000805 if (ret)
806 dmaengine_terminate_all(chan);
807
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200808 thread->done = true;
809
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200810 if (params->iterations > 0)
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200811 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800812 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200813 interruptible_sleep_on(&wait_dmatest_exit);
814 }
815
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700816 return ret;
817}
818
819static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
820{
821 struct dmatest_thread *thread;
822 struct dmatest_thread *_thread;
823 int ret;
824
825 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
826 ret = kthread_stop(thread->task);
827 pr_debug("dmatest: thread %s exited with status %d\n",
828 thread->task->comm, ret);
829 list_del(&thread->node);
830 kfree(thread);
831 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530832
833 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000834 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530835
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700836 kfree(dtc);
837}
838
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200839static int dmatest_add_threads(struct dmatest_info *info,
840 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700841{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200842 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700843 struct dmatest_thread *thread;
844 struct dma_chan *chan = dtc->chan;
845 char *op;
846 unsigned int i;
847
848 if (type == DMA_MEMCPY)
849 op = "copy";
850 else if (type == DMA_XOR)
851 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700852 else if (type == DMA_PQ)
853 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700854 else
855 return -EINVAL;
856
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200857 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700858 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
859 if (!thread) {
860 pr_warning("dmatest: No memory for %s-%s%u\n",
861 dma_chan_name(chan), op, i);
862
863 break;
864 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200865 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700866 thread->chan = dtc->chan;
867 thread->type = type;
868 smp_wmb();
869 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
870 dma_chan_name(chan), op, i);
871 if (IS_ERR(thread->task)) {
872 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
873 dma_chan_name(chan), op, i);
874 kfree(thread);
875 break;
876 }
877
878 /* srcbuf and dstbuf are allocated by the thread itself */
879
880 list_add_tail(&thread->node, &dtc->threads);
881 }
882
883 return i;
884}
885
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200886static int dmatest_add_channel(struct dmatest_info *info,
887 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700888{
889 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700890 struct dma_device *dma_dev = chan->device;
891 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400892 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700893
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700894 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700895 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700896 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700897 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700898 }
899
900 dtc->chan = chan;
901 INIT_LIST_HEAD(&dtc->threads);
902
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700903 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200904 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200905 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700906 }
907 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200908 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200909 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700910 }
Dan Williams58691d62009-08-29 19:09:27 -0700911 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200912 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700913 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700914 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700915
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700916 pr_info("dmatest: Started %u threads using %s\n",
917 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700918
Andy Shevchenko838cc702013-03-04 11:09:28 +0200919 list_add_tail(&dtc->node, &info->channels);
920 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700921
Dan Williams33df8ca2009-01-06 11:38:15 -0700922 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700923}
924
Dan Williams7dd60252009-01-06 11:38:19 -0700925static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700926{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200927 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200928
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200929 if (!dmatest_match_channel(params, chan) ||
930 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700931 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700932 else
Dan Williams7dd60252009-01-06 11:38:19 -0700933 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700934}
935
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200936static int __run_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700937{
Dan Williams33df8ca2009-01-06 11:38:15 -0700938 dma_cap_mask_t mask;
939 struct dma_chan *chan;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200940 struct dmatest_params *params = &info->params;
Dan Williams33df8ca2009-01-06 11:38:15 -0700941 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700942
Dan Williams33df8ca2009-01-06 11:38:15 -0700943 dma_cap_zero(mask);
944 dma_cap_set(DMA_MEMCPY, mask);
945 for (;;) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200946 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700947 if (chan) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200948 err = dmatest_add_channel(info, chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700949 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700950 dma_release_channel(chan);
951 break; /* add_channel failed, punt */
952 }
953 } else
954 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200955 if (params->max_channels &&
956 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700957 break; /* we have all we need */
958 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700959 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700960}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700961
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200962#ifndef MODULE
963static int run_threaded_test(struct dmatest_info *info)
964{
965 int ret;
966
967 mutex_lock(&info->lock);
968 ret = __run_threaded_test(info);
969 mutex_unlock(&info->lock);
970 return ret;
971}
972#endif
973
974static void __stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700975{
Dan Williams33df8ca2009-01-06 11:38:15 -0700976 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700977 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700978
Andy Shevchenko838cc702013-03-04 11:09:28 +0200979 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700980 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700981 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700982 dmatest_cleanup_channel(dtc);
Andy Shevchenko838cc702013-03-04 11:09:28 +0200983 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
Dan Williams7cbd4872009-03-04 16:06:03 -0700984 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700985 }
Andy Shevchenko838cc702013-03-04 11:09:28 +0200986
987 info->nr_channels = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700988}
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200989
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200990static void stop_threaded_test(struct dmatest_info *info)
991{
992 mutex_lock(&info->lock);
993 __stop_threaded_test(info);
994 mutex_unlock(&info->lock);
995}
996
997static int __restart_threaded_test(struct dmatest_info *info, bool run)
998{
999 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001000
1001 /* Stop any running test first */
1002 __stop_threaded_test(info);
1003
1004 if (run == false)
1005 return 0;
1006
Andy Shevchenko95019c82013-03-04 11:09:33 +02001007 /* Clear results from previous run */
1008 result_free(info, NULL);
1009
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001010 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +03001011 params->buf_size = test_buf_size;
1012 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
1013 strlcpy(params->device, strim(test_device), sizeof(params->device));
1014 params->threads_per_chan = threads_per_chan;
1015 params->max_channels = max_channels;
1016 params->iterations = iterations;
1017 params->xor_sources = xor_sources;
1018 params->pq_sources = pq_sources;
1019 params->timeout = timeout;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001020
1021 /* Run test with new parameters */
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001022 return __run_threaded_test(info);
1023}
1024
1025static bool __is_threaded_test_run(struct dmatest_info *info)
1026{
1027 struct dmatest_chan *dtc;
1028
1029 list_for_each_entry(dtc, &info->channels, node) {
1030 struct dmatest_thread *thread;
1031
1032 list_for_each_entry(thread, &dtc->threads, node) {
1033 if (!thread->done)
1034 return true;
1035 }
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001036 }
1037
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001038 return false;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001039}
1040
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001041static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
1042 size_t count, loff_t *ppos)
1043{
1044 struct dmatest_info *info = file->private_data;
1045 char buf[3];
1046
1047 mutex_lock(&info->lock);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001048
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001049 if (__is_threaded_test_run(info)) {
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001050 buf[0] = 'Y';
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001051 } else {
1052 __stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001053 buf[0] = 'N';
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001054 }
1055
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001056 mutex_unlock(&info->lock);
1057 buf[1] = '\n';
1058 buf[2] = 0x00;
1059 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1060}
1061
1062static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
1063 size_t count, loff_t *ppos)
1064{
1065 struct dmatest_info *info = file->private_data;
1066 char buf[16];
1067 bool bv;
1068 int ret = 0;
1069
1070 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
1071 return -EFAULT;
1072
1073 if (strtobool(buf, &bv) == 0) {
1074 mutex_lock(&info->lock);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001075
1076 if (__is_threaded_test_run(info))
1077 ret = -EBUSY;
1078 else
1079 ret = __restart_threaded_test(info, bv);
1080
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001081 mutex_unlock(&info->lock);
1082 }
1083
1084 return ret ? ret : count;
1085}
1086
1087static const struct file_operations dtf_run_fops = {
1088 .read = dtf_read_run,
1089 .write = dtf_write_run,
1090 .open = simple_open,
1091 .llseek = default_llseek,
1092};
1093
Andy Shevchenko95019c82013-03-04 11:09:33 +02001094static int dtf_results_show(struct seq_file *sf, void *data)
1095{
1096 struct dmatest_info *info = sf->private;
1097 struct dmatest_result *result;
1098 struct dmatest_thread_result *tr;
Andy Shevchenkod86b2f22013-03-04 11:09:34 +02001099 unsigned int i;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001100
1101 mutex_lock(&info->results_lock);
1102 list_for_each_entry(result, &info->results, node) {
Andy Shevchenkod86b2f22013-03-04 11:09:34 +02001103 list_for_each_entry(tr, &result->results, node) {
Andy Shevchenko95019c82013-03-04 11:09:33 +02001104 seq_printf(sf, "%s\n",
1105 thread_result_get(result->name, tr));
Andy Shevchenkod86b2f22013-03-04 11:09:34 +02001106 if (tr->type == DMATEST_ET_VERIFY_BUF) {
1107 for (i = 0; i < tr->vr->error_count; i++) {
1108 seq_printf(sf, "\t%s\n",
1109 verify_result_get_one(tr->vr, i));
1110 }
1111 }
1112 }
Andy Shevchenko95019c82013-03-04 11:09:33 +02001113 }
1114
1115 mutex_unlock(&info->results_lock);
1116 return 0;
1117}
1118
1119static int dtf_results_open(struct inode *inode, struct file *file)
1120{
1121 return single_open(file, dtf_results_show, inode->i_private);
1122}
1123
1124static const struct file_operations dtf_results_fops = {
1125 .open = dtf_results_open,
1126 .read = seq_read,
1127 .llseek = seq_lseek,
1128 .release = single_release,
1129};
1130
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001131static int dmatest_register_dbgfs(struct dmatest_info *info)
1132{
1133 struct dentry *d;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001134 int ret = -ENOMEM;
1135
1136 d = debugfs_create_dir("dmatest", NULL);
1137 if (IS_ERR(d))
1138 return PTR_ERR(d);
1139 if (!d)
1140 goto err_root;
1141
1142 info->root = d;
1143
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001144 /* Run or stop threaded test */
1145 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
1146 info, &dtf_run_fops);
1147 if (IS_ERR_OR_NULL(d))
1148 goto err_node;
1149
Andy Shevchenko95019c82013-03-04 11:09:33 +02001150 /* Results of test in progress */
1151 d = debugfs_create_file("results", S_IRUGO, info->root, info,
1152 &dtf_results_fops);
1153 if (IS_ERR_OR_NULL(d))
1154 goto err_node;
1155
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001156 return 0;
1157
1158err_node:
1159 debugfs_remove_recursive(info->root);
1160err_root:
1161 pr_err("dmatest: Failed to initialize debugfs\n");
1162 return ret;
1163}
1164
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001165static int __init dmatest_init(void)
1166{
1167 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001168 int ret;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001169
1170 memset(info, 0, sizeof(*info));
1171
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001172 mutex_init(&info->lock);
Andy Shevchenko838cc702013-03-04 11:09:28 +02001173 INIT_LIST_HEAD(&info->channels);
1174
Andy Shevchenko95019c82013-03-04 11:09:33 +02001175 mutex_init(&info->results_lock);
1176 INIT_LIST_HEAD(&info->results);
1177
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001178 ret = dmatest_register_dbgfs(info);
1179 if (ret)
1180 return ret;
1181
1182#ifdef MODULE
1183 return 0;
1184#else
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001185 return run_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001186#endif
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001187}
1188/* when compiled-in wait for drivers to load first */
1189late_initcall(dmatest_init);
1190
1191static void __exit dmatest_exit(void)
1192{
1193 struct dmatest_info *info = &test_info;
1194
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001195 debugfs_remove_recursive(info->root);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001196 stop_threaded_test(info);
Andy Shevchenko95019c82013-03-04 11:09:33 +02001197 result_free(info, NULL);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001198}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001199module_exit(dmatest_exit);
1200
Jean Delvaree05503e2011-05-18 16:49:24 +02001201MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001202MODULE_LICENSE("GPL v2");