blob: 3697bd49ed4ce0028d2ed24f89819f8ecc2ee651 [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;
28module_param(test_buf_size, uint, S_IRUGO);
29MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
30
Kay Sievers06190d82008-11-11 13:12:33 -070031static char test_channel[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070032module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
33MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
34
Kay Sievers06190d82008-11-11 13:12:33 -070035static char test_device[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070036module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
37MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
38
39static unsigned int threads_per_chan = 1;
40module_param(threads_per_chan, uint, S_IRUGO);
41MODULE_PARM_DESC(threads_per_chan,
42 "Number of threads to start per channel (default: 1)");
43
44static unsigned int max_channels;
45module_param(max_channels, uint, S_IRUGO);
Dan Williams33df8ca2009-01-06 11:38:15 -070046MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070047 "Maximum number of channels to use (default: all)");
48
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020049static unsigned int iterations;
50module_param(iterations, uint, S_IRUGO);
51MODULE_PARM_DESC(iterations,
52 "Iterations before stopping test (default: infinite)");
53
Dan Williamsb54d5cb2009-03-25 09:13:25 -070054static unsigned int xor_sources = 3;
55module_param(xor_sources, uint, S_IRUGO);
56MODULE_PARM_DESC(xor_sources,
57 "Number of xor source buffers (default: 3)");
58
Dan Williams58691d62009-08-29 19:09:27 -070059static unsigned int pq_sources = 3;
60module_param(pq_sources, uint, S_IRUGO);
61MODULE_PARM_DESC(pq_sources,
62 "Number of p+q source buffers (default: 3)");
63
Viresh Kumard42efe62011-03-22 17:27:25 +053064static int timeout = 3000;
65module_param(timeout, uint, S_IRUGO);
Joe Perches85ee7a12011-04-23 20:38:19 -070066MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
67 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053068
Andy Shevchenko74b5c072013-03-04 11:09:32 +020069/* Maximum amount of mismatched bytes in buffer to print */
70#define MAX_ERROR_COUNT 32
71
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070072/*
73 * Initialization patterns. All bytes in the source buffer has bit 7
74 * set, all bytes in the destination buffer has bit 7 cleared.
75 *
76 * Bit 6 is set for all bytes which are to be copied by the DMA
77 * engine. Bit 5 is set for all bytes which are to be overwritten by
78 * the DMA engine.
79 *
80 * The remaining bits are the inverse of a counter which increments by
81 * one for each byte address.
82 */
83#define PATTERN_SRC 0x80
84#define PATTERN_DST 0x00
85#define PATTERN_COPY 0x40
86#define PATTERN_OVERWRITE 0x20
87#define PATTERN_COUNT_MASK 0x1f
88
Andy Shevchenko95019c82013-03-04 11:09:33 +020089enum dmatest_error_type {
90 DMATEST_ET_OK,
91 DMATEST_ET_MAP_SRC,
92 DMATEST_ET_MAP_DST,
93 DMATEST_ET_PREP,
94 DMATEST_ET_SUBMIT,
95 DMATEST_ET_TIMEOUT,
96 DMATEST_ET_DMA_ERROR,
97 DMATEST_ET_DMA_IN_PROGRESS,
98 DMATEST_ET_VERIFY,
99};
100
101struct dmatest_thread_result {
102 struct list_head node;
103 unsigned int n;
104 unsigned int src_off;
105 unsigned int dst_off;
106 unsigned int len;
107 enum dmatest_error_type type;
108 union {
109 unsigned long data;
110 dma_cookie_t cookie;
111 enum dma_status status;
112 int error;
113 };
114};
115
116struct dmatest_result {
117 struct list_head node;
118 char *name;
119 struct list_head results;
120};
121
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200122struct dmatest_info;
123
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700124struct dmatest_thread {
125 struct list_head node;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200126 struct dmatest_info *info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700127 struct task_struct *task;
128 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700129 u8 **srcs;
130 u8 **dsts;
131 enum dma_transaction_type type;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200132 bool done;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700133};
134
135struct dmatest_chan {
136 struct list_head node;
137 struct dma_chan *chan;
138 struct list_head threads;
139};
140
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200141/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200142 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200143 * @buf_size: size of the memcpy test buffer
144 * @channel: bus ID of the channel to test
145 * @device: bus ID of the DMA Engine to test
146 * @threads_per_chan: number of threads to start per channel
147 * @max_channels: maximum number of channels to use
148 * @iterations: iterations before stopping test
149 * @xor_sources: number of xor source buffers
150 * @pq_sources: number of p+q source buffers
151 * @timeout: transfer timeout in msec, -1 for infinite timeout
152 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200153struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200154 unsigned int buf_size;
155 char channel[20];
156 char device[20];
157 unsigned int threads_per_chan;
158 unsigned int max_channels;
159 unsigned int iterations;
160 unsigned int xor_sources;
161 unsigned int pq_sources;
162 int timeout;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200163};
164
165/**
166 * struct dmatest_info - test information.
167 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200168 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200169 */
170struct dmatest_info {
171 /* Test parameters */
172 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200173
174 /* Internal state */
175 struct list_head channels;
176 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200177 struct mutex lock;
178
179 /* debugfs related stuff */
180 struct dentry *root;
181 struct dmatest_params dbgfs_params;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200182
183 /* Test results */
184 struct list_head results;
185 struct mutex results_lock;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200186};
187
188static struct dmatest_info test_info;
189
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200190static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200191 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700192{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200193 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700194 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200195 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700196}
197
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200198static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200199 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700200{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200201 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700202 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200203 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700204}
205
206static unsigned long dmatest_random(void)
207{
208 unsigned long buf;
209
210 get_random_bytes(&buf, sizeof(buf));
211 return buf;
212}
213
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200214static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
215 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700216{
217 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700218 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700219
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700220 for (; (buf = *bufs); bufs++) {
221 for (i = 0; i < start; i++)
222 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
223 for ( ; i < start + len; i++)
224 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700225 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200226 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700227 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
228 buf++;
229 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700230}
231
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200232static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
233 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700234{
235 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700236 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700237
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700238 for (; (buf = *bufs); bufs++) {
239 for (i = 0; i < start; i++)
240 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
241 for ( ; i < start + len; i++)
242 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
243 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200244 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700245 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
246 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700247}
248
249static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
250 unsigned int counter, bool is_srcbuf)
251{
252 u8 diff = actual ^ pattern;
253 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
254 const char *thread_name = current->comm;
255
256 if (is_srcbuf)
257 pr_warning("%s: srcbuf[0x%x] overwritten!"
258 " Expected %02x, got %02x\n",
259 thread_name, index, expected, actual);
260 else if ((pattern & PATTERN_COPY)
261 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
262 pr_warning("%s: dstbuf[0x%x] not copied!"
263 " Expected %02x, got %02x\n",
264 thread_name, index, expected, actual);
265 else if (diff & PATTERN_SRC)
266 pr_warning("%s: dstbuf[0x%x] was copied!"
267 " Expected %02x, got %02x\n",
268 thread_name, index, expected, actual);
269 else
270 pr_warning("%s: dstbuf[0x%x] mismatch!"
271 " Expected %02x, got %02x\n",
272 thread_name, index, expected, actual);
273}
274
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700275static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700276 unsigned int end, unsigned int counter, u8 pattern,
277 bool is_srcbuf)
278{
279 unsigned int i;
280 unsigned int error_count = 0;
281 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700282 u8 expected;
283 u8 *buf;
284 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700285
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700286 for (; (buf = *bufs); bufs++) {
287 counter = counter_orig;
288 for (i = start; i < end; i++) {
289 actual = buf[i];
290 expected = pattern | (~counter & PATTERN_COUNT_MASK);
291 if (actual != expected) {
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200292 if (error_count < MAX_ERROR_COUNT)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700293 dmatest_mismatch(actual, pattern, i,
294 counter, is_srcbuf);
295 error_count++;
296 }
297 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700298 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700299 }
300
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200301 if (error_count > MAX_ERROR_COUNT)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700302 pr_warning("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200303 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700304
305 return error_count;
306}
307
Tejun Heoadfa5432011-11-23 09:28:16 -0800308/* poor man's completion - we want to use wait_event_freezable() on it */
309struct dmatest_done {
310 bool done;
311 wait_queue_head_t *wait;
312};
313
314static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700315{
Tejun Heoadfa5432011-11-23 09:28:16 -0800316 struct dmatest_done *done = arg;
317
318 done->done = true;
319 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700320}
321
Andy Shevchenko632fd282012-12-17 15:59:52 -0800322static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
323 unsigned int count)
324{
325 while (count--)
326 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
327}
328
329static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
330 unsigned int count)
331{
332 while (count--)
333 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
334}
335
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900336static unsigned int min_odd(unsigned int x, unsigned int y)
337{
338 unsigned int val = min(x, y);
339
340 return val % 2 ? val : val - 1;
341}
342
Andy Shevchenko95019c82013-03-04 11:09:33 +0200343static char *thread_result_get(const char *name,
344 struct dmatest_thread_result *tr)
345{
346 static const char * const messages[] = {
347 [DMATEST_ET_OK] = "No errors",
348 [DMATEST_ET_MAP_SRC] = "src mapping error",
349 [DMATEST_ET_MAP_DST] = "dst mapping error",
350 [DMATEST_ET_PREP] = "prep error",
351 [DMATEST_ET_SUBMIT] = "submit error",
352 [DMATEST_ET_TIMEOUT] = "test timed out",
353 [DMATEST_ET_DMA_ERROR] =
354 "got completion callback (DMA_ERROR)",
355 [DMATEST_ET_DMA_IN_PROGRESS] =
356 "got completion callback (DMA_IN_PROGRESS)",
357 [DMATEST_ET_VERIFY] = "errors",
358 };
359 static char buf[512];
360
361 snprintf(buf, sizeof(buf) - 1,
362 "%s: #%u: %s with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
363 name, tr->n, messages[tr->type], tr->src_off, tr->dst_off,
364 tr->len, tr->data);
365
366 return buf;
367}
368
369static int thread_result_add(struct dmatest_info *info,
370 struct dmatest_result *r, enum dmatest_error_type type,
371 unsigned int n, unsigned int src_off, unsigned int dst_off,
372 unsigned int len, unsigned long data)
373{
374 struct dmatest_thread_result *tr;
375
376 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
377 if (!tr)
378 return -ENOMEM;
379
380 tr->type = type;
381 tr->n = n;
382 tr->src_off = src_off;
383 tr->dst_off = dst_off;
384 tr->len = len;
385 tr->data = data;
386
387 mutex_lock(&info->results_lock);
388 list_add_tail(&tr->node, &r->results);
389 mutex_unlock(&info->results_lock);
390
391 pr_warn("%s\n", thread_result_get(r->name, tr));
392 return 0;
393}
394
395static void result_free(struct dmatest_info *info, const char *name)
396{
397 struct dmatest_result *r, *_r;
398
399 mutex_lock(&info->results_lock);
400 list_for_each_entry_safe(r, _r, &info->results, node) {
401 struct dmatest_thread_result *tr, *_tr;
402
403 if (name && strcmp(r->name, name))
404 continue;
405
406 list_for_each_entry_safe(tr, _tr, &r->results, node) {
407 list_del(&tr->node);
408 kfree(tr);
409 }
410
411 kfree(r->name);
412 list_del(&r->node);
413 kfree(r);
414 }
415
416 mutex_unlock(&info->results_lock);
417}
418
419static struct dmatest_result *result_init(struct dmatest_info *info,
420 const char *name)
421{
422 struct dmatest_result *r;
423
424 r = kzalloc(sizeof(*r), GFP_KERNEL);
425 if (r) {
426 r->name = kstrdup(name, GFP_KERNEL);
427 INIT_LIST_HEAD(&r->results);
428 mutex_lock(&info->results_lock);
429 list_add_tail(&r->node, &info->results);
430 mutex_unlock(&info->results_lock);
431 }
432 return r;
433}
434
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700435/*
436 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700437 * offsets for a given operation type until it is told to exit by
438 * kthread_stop(). There may be multiple threads running this function
439 * in parallel for a single channel, and there may be multiple channels
440 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700441 *
442 * Before each test, the source and destination buffer is initialized
443 * with a known pattern. This pattern is different depending on
444 * whether it's in an area which is supposed to be copied or
445 * overwritten, and different in the source and destination buffers.
446 * So if the DMA engine doesn't copy exactly what we tell it to copy,
447 * we'll notice.
448 */
449static int dmatest_func(void *data)
450{
Tejun Heoadfa5432011-11-23 09:28:16 -0800451 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700452 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800453 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200454 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200455 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700456 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900457 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700458 const char *thread_name;
459 unsigned int src_off, dst_off, len;
460 unsigned int error_count;
461 unsigned int failed_tests = 0;
462 unsigned int total_tests = 0;
463 dma_cookie_t cookie;
464 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700465 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200466 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700467 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700468 int src_cnt;
469 int dst_cnt;
470 int i;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200471 struct dmatest_result *result;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700472
473 thread_name = current->comm;
Tejun Heoadfa5432011-11-23 09:28:16 -0800474 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700475
476 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700477
478 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200479 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200480 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700481 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900482 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700483 if (thread->type == DMA_MEMCPY)
484 src_cnt = dst_cnt = 1;
485 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900486 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200487 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700488 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700489 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900490 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200491 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700492 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200493
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200494 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200495 if (!pq_coefs)
496 goto err_thread_type;
497
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100498 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700499 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700500 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200501 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700502
Andy Shevchenko95019c82013-03-04 11:09:33 +0200503 result = result_init(info, thread_name);
504 if (!result)
505 goto err_srcs;
506
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700507 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
508 if (!thread->srcs)
509 goto err_srcs;
510 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200511 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700512 if (!thread->srcs[i])
513 goto err_srcbuf;
514 }
515 thread->srcs[i] = NULL;
516
517 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
518 if (!thread->dsts)
519 goto err_dsts;
520 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200521 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700522 if (!thread->dsts[i])
523 goto err_dstbuf;
524 }
525 thread->dsts[i] = NULL;
526
Dan Williamse44e0aa2009-03-25 09:13:25 -0700527 set_user_nice(current, 10);
528
Ira Snyderb203bd32011-03-03 07:54:53 +0000529 /*
530 * src buffers are freed by the DMAEngine code with dma_unmap_single()
531 * dst buffers are freed by ourselves below
532 */
533 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
534 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700535
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200536 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200537 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700538 struct dma_async_tx_descriptor *tx = NULL;
539 dma_addr_t dma_srcs[src_cnt];
540 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700541 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700542
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700543 total_tests++;
544
Dan Williams83544ae2009-09-08 17:42:53 -0700545 /* honor alignment restrictions */
546 if (thread->type == DMA_MEMCPY)
547 align = dev->copy_align;
548 else if (thread->type == DMA_XOR)
549 align = dev->xor_align;
550 else if (thread->type == DMA_PQ)
551 align = dev->pq_align;
552
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200553 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100554 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200555 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100556 break;
557 }
558
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200559 len = dmatest_random() % params->buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700560 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100561 if (!len)
562 len = 1 << align;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200563 src_off = dmatest_random() % (params->buf_size - len + 1);
564 dst_off = dmatest_random() % (params->buf_size - len + 1);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100565
Dan Williams83544ae2009-09-08 17:42:53 -0700566 src_off = (src_off >> align) << align;
567 dst_off = (dst_off >> align) << align;
568
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200569 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
570 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700571
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700572 for (i = 0; i < src_cnt; i++) {
573 u8 *buf = thread->srcs[i] + src_off;
574
575 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
576 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800577 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
578 if (ret) {
579 unmap_src(dev->dev, dma_srcs, len, i);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200580 thread_result_add(info, result,
581 DMATEST_ET_MAP_SRC,
582 total_tests, src_off, dst_off,
583 len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800584 failed_tests++;
585 continue;
586 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700587 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700588 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700589 for (i = 0; i < dst_cnt; i++) {
590 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200591 params->buf_size,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700592 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800593 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
594 if (ret) {
595 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200596 unmap_dst(dev->dev, dma_dsts, params->buf_size,
597 i);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200598 thread_result_add(info, result,
599 DMATEST_ET_MAP_DST,
600 total_tests, src_off, dst_off,
601 len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800602 failed_tests++;
603 continue;
604 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700605 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700606
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700607 if (thread->type == DMA_MEMCPY)
608 tx = dev->device_prep_dma_memcpy(chan,
609 dma_dsts[0] + dst_off,
610 dma_srcs[0], len,
611 flags);
612 else if (thread->type == DMA_XOR)
613 tx = dev->device_prep_dma_xor(chan,
614 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700615 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700616 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700617 else if (thread->type == DMA_PQ) {
618 dma_addr_t dma_pq[dst_cnt];
619
620 for (i = 0; i < dst_cnt; i++)
621 dma_pq[i] = dma_dsts[i] + dst_off;
622 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100623 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700624 len, flags);
625 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700626
Atsushi Nemotod86be862009-01-13 09:22:20 -0700627 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800628 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200629 unmap_dst(dev->dev, dma_dsts, params->buf_size,
630 dst_cnt);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200631 thread_result_add(info, result, DMATEST_ET_PREP,
632 total_tests, src_off, dst_off,
633 len, 0);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700634 msleep(100);
635 failed_tests++;
636 continue;
637 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700638
Tejun Heoadfa5432011-11-23 09:28:16 -0800639 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700640 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800641 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700642 cookie = tx->tx_submit(tx);
643
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700644 if (dma_submit_error(cookie)) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200645 thread_result_add(info, result, DMATEST_ET_SUBMIT,
646 total_tests, src_off, dst_off,
647 len, cookie);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700648 msleep(100);
649 failed_tests++;
650 continue;
651 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700652 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700653
Andy Shevchenko77101ce2013-03-04 11:09:25 +0200654 wait_event_freezable_timeout(done_wait,
655 done.done || kthread_should_stop(),
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200656 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200657
Dan Williamse44e0aa2009-03-25 09:13:25 -0700658 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700659
Tejun Heoadfa5432011-11-23 09:28:16 -0800660 if (!done.done) {
661 /*
662 * We're leaving the timed out dma operation with
663 * dangling pointer to done_wait. To make this
664 * correct, we'll need to allocate wait_done for
665 * each test iteration and perform "who's gonna
666 * free it this time?" dancing. For now, just
667 * leave it dangling.
668 */
Andy Shevchenko95019c82013-03-04 11:09:33 +0200669 thread_result_add(info, result, DMATEST_ET_TIMEOUT,
670 total_tests, src_off, dst_off,
671 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700672 failed_tests++;
673 continue;
674 } else if (status != DMA_SUCCESS) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200675 enum dmatest_error_type type = (status == DMA_ERROR) ?
676 DMATEST_ET_DMA_ERROR : DMATEST_ET_DMA_IN_PROGRESS;
677 thread_result_add(info, result, type,
678 total_tests, src_off, dst_off,
679 len, status);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700680 failed_tests++;
681 continue;
682 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700683
Atsushi Nemotod86be862009-01-13 09:22:20 -0700684 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200685 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700686
687 error_count = 0;
688
689 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700690 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700691 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700692 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700693 src_off + len, src_off,
694 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700695 error_count += dmatest_verify(thread->srcs, src_off + len,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200696 params->buf_size, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700697 PATTERN_SRC, true);
698
699 pr_debug("%s: verifying dest buffer...\n",
700 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700701 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700702 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700703 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700704 dst_off + len, src_off,
705 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700706 error_count += dmatest_verify(thread->dsts, dst_off + len,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200707 params->buf_size, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700708 PATTERN_DST, false);
709
710 if (error_count) {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200711 thread_result_add(info, result, DMATEST_ET_VERIFY,
712 total_tests, src_off, dst_off,
713 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700714 failed_tests++;
715 } else {
Andy Shevchenko95019c82013-03-04 11:09:33 +0200716 thread_result_add(info, result, DMATEST_ET_OK,
717 total_tests, src_off, dst_off,
718 len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700719 }
720 }
721
722 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700723 for (i = 0; thread->dsts[i]; i++)
724 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700725err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700726 kfree(thread->dsts);
727err_dsts:
728 for (i = 0; thread->srcs[i]; i++)
729 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700730err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700731 kfree(thread->srcs);
732err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200733 kfree(pq_coefs);
734err_thread_type:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700735 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
736 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200737
Viresh Kumar9704efa2011-07-29 16:21:57 +0530738 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000739 if (ret)
740 dmaengine_terminate_all(chan);
741
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200742 thread->done = true;
743
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200744 if (params->iterations > 0)
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200745 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800746 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200747 interruptible_sleep_on(&wait_dmatest_exit);
748 }
749
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700750 return ret;
751}
752
753static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
754{
755 struct dmatest_thread *thread;
756 struct dmatest_thread *_thread;
757 int ret;
758
759 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
760 ret = kthread_stop(thread->task);
761 pr_debug("dmatest: thread %s exited with status %d\n",
762 thread->task->comm, ret);
763 list_del(&thread->node);
764 kfree(thread);
765 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530766
767 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000768 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530769
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700770 kfree(dtc);
771}
772
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200773static int dmatest_add_threads(struct dmatest_info *info,
774 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700775{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200776 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700777 struct dmatest_thread *thread;
778 struct dma_chan *chan = dtc->chan;
779 char *op;
780 unsigned int i;
781
782 if (type == DMA_MEMCPY)
783 op = "copy";
784 else if (type == DMA_XOR)
785 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700786 else if (type == DMA_PQ)
787 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700788 else
789 return -EINVAL;
790
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200791 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700792 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
793 if (!thread) {
794 pr_warning("dmatest: No memory for %s-%s%u\n",
795 dma_chan_name(chan), op, i);
796
797 break;
798 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200799 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700800 thread->chan = dtc->chan;
801 thread->type = type;
802 smp_wmb();
803 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
804 dma_chan_name(chan), op, i);
805 if (IS_ERR(thread->task)) {
806 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
807 dma_chan_name(chan), op, i);
808 kfree(thread);
809 break;
810 }
811
812 /* srcbuf and dstbuf are allocated by the thread itself */
813
814 list_add_tail(&thread->node, &dtc->threads);
815 }
816
817 return i;
818}
819
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200820static int dmatest_add_channel(struct dmatest_info *info,
821 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700822{
823 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700824 struct dma_device *dma_dev = chan->device;
825 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400826 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700827
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700828 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700829 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700830 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700831 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700832 }
833
834 dtc->chan = chan;
835 INIT_LIST_HEAD(&dtc->threads);
836
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700837 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200838 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200839 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700840 }
841 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200842 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200843 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700844 }
Dan Williams58691d62009-08-29 19:09:27 -0700845 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200846 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700847 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700848 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700849
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700850 pr_info("dmatest: Started %u threads using %s\n",
851 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700852
Andy Shevchenko838cc702013-03-04 11:09:28 +0200853 list_add_tail(&dtc->node, &info->channels);
854 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700855
Dan Williams33df8ca2009-01-06 11:38:15 -0700856 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700857}
858
Dan Williams7dd60252009-01-06 11:38:19 -0700859static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700860{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200861 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200862
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200863 if (!dmatest_match_channel(params, chan) ||
864 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700865 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700866 else
Dan Williams7dd60252009-01-06 11:38:19 -0700867 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700868}
869
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200870static int __run_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700871{
Dan Williams33df8ca2009-01-06 11:38:15 -0700872 dma_cap_mask_t mask;
873 struct dma_chan *chan;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200874 struct dmatest_params *params = &info->params;
Dan Williams33df8ca2009-01-06 11:38:15 -0700875 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700876
Dan Williams33df8ca2009-01-06 11:38:15 -0700877 dma_cap_zero(mask);
878 dma_cap_set(DMA_MEMCPY, mask);
879 for (;;) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200880 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700881 if (chan) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200882 err = dmatest_add_channel(info, chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700883 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700884 dma_release_channel(chan);
885 break; /* add_channel failed, punt */
886 }
887 } else
888 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200889 if (params->max_channels &&
890 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700891 break; /* we have all we need */
892 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700893 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700894}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700895
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200896#ifndef MODULE
897static int run_threaded_test(struct dmatest_info *info)
898{
899 int ret;
900
901 mutex_lock(&info->lock);
902 ret = __run_threaded_test(info);
903 mutex_unlock(&info->lock);
904 return ret;
905}
906#endif
907
908static void __stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700909{
Dan Williams33df8ca2009-01-06 11:38:15 -0700910 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700911 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700912
Andy Shevchenko838cc702013-03-04 11:09:28 +0200913 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700914 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700915 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700916 dmatest_cleanup_channel(dtc);
Andy Shevchenko838cc702013-03-04 11:09:28 +0200917 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
Dan Williams7cbd4872009-03-04 16:06:03 -0700918 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700919 }
Andy Shevchenko838cc702013-03-04 11:09:28 +0200920
921 info->nr_channels = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700922}
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200923
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200924static void stop_threaded_test(struct dmatest_info *info)
925{
926 mutex_lock(&info->lock);
927 __stop_threaded_test(info);
928 mutex_unlock(&info->lock);
929}
930
931static int __restart_threaded_test(struct dmatest_info *info, bool run)
932{
933 struct dmatest_params *params = &info->params;
934 int ret;
935
936 /* Stop any running test first */
937 __stop_threaded_test(info);
938
939 if (run == false)
940 return 0;
941
Andy Shevchenko95019c82013-03-04 11:09:33 +0200942 /* Clear results from previous run */
943 result_free(info, NULL);
944
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200945 /* Copy test parameters */
946 memcpy(params, &info->dbgfs_params, sizeof(*params));
947
948 /* Run test with new parameters */
949 ret = __run_threaded_test(info);
950 if (ret) {
951 __stop_threaded_test(info);
952 pr_err("dmatest: Can't run test\n");
953 }
954
955 return ret;
956}
957
958static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos,
959 const void __user *from, size_t count)
960{
961 char tmp[20];
962 ssize_t len;
963
964 len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count);
965 if (len >= 0) {
966 tmp[len] = '\0';
967 strlcpy(to, strim(tmp), available);
968 }
969
970 return len;
971}
972
973static ssize_t dtf_read_channel(struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
975{
976 struct dmatest_info *info = file->private_data;
977 return simple_read_from_buffer(buf, count, ppos,
978 info->dbgfs_params.channel,
979 strlen(info->dbgfs_params.channel));
980}
981
982static ssize_t dtf_write_channel(struct file *file, const char __user *buf,
983 size_t size, loff_t *ppos)
984{
985 struct dmatest_info *info = file->private_data;
986 return dtf_write_string(info->dbgfs_params.channel,
987 sizeof(info->dbgfs_params.channel),
988 ppos, buf, size);
989}
990
991static const struct file_operations dtf_channel_fops = {
992 .read = dtf_read_channel,
993 .write = dtf_write_channel,
994 .open = simple_open,
995 .llseek = default_llseek,
996};
997
998static ssize_t dtf_read_device(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1000{
1001 struct dmatest_info *info = file->private_data;
1002 return simple_read_from_buffer(buf, count, ppos,
1003 info->dbgfs_params.device,
1004 strlen(info->dbgfs_params.device));
1005}
1006
1007static ssize_t dtf_write_device(struct file *file, const char __user *buf,
1008 size_t size, loff_t *ppos)
1009{
1010 struct dmatest_info *info = file->private_data;
1011 return dtf_write_string(info->dbgfs_params.device,
1012 sizeof(info->dbgfs_params.device),
1013 ppos, buf, size);
1014}
1015
1016static const struct file_operations dtf_device_fops = {
1017 .read = dtf_read_device,
1018 .write = dtf_write_device,
1019 .open = simple_open,
1020 .llseek = default_llseek,
1021};
1022
1023static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
1024 size_t count, loff_t *ppos)
1025{
1026 struct dmatest_info *info = file->private_data;
1027 char buf[3];
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001028 struct dmatest_chan *dtc;
1029 bool alive = false;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001030
1031 mutex_lock(&info->lock);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001032 list_for_each_entry(dtc, &info->channels, node) {
1033 struct dmatest_thread *thread;
1034
1035 list_for_each_entry(thread, &dtc->threads, node) {
1036 if (!thread->done) {
1037 alive = true;
1038 break;
1039 }
1040 }
1041 }
1042
1043 if (alive) {
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001044 buf[0] = 'Y';
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001045 } else {
1046 __stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001047 buf[0] = 'N';
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001048 }
1049
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001050 mutex_unlock(&info->lock);
1051 buf[1] = '\n';
1052 buf[2] = 0x00;
1053 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1054}
1055
1056static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
1057 size_t count, loff_t *ppos)
1058{
1059 struct dmatest_info *info = file->private_data;
1060 char buf[16];
1061 bool bv;
1062 int ret = 0;
1063
1064 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
1065 return -EFAULT;
1066
1067 if (strtobool(buf, &bv) == 0) {
1068 mutex_lock(&info->lock);
1069 ret = __restart_threaded_test(info, bv);
1070 mutex_unlock(&info->lock);
1071 }
1072
1073 return ret ? ret : count;
1074}
1075
1076static const struct file_operations dtf_run_fops = {
1077 .read = dtf_read_run,
1078 .write = dtf_write_run,
1079 .open = simple_open,
1080 .llseek = default_llseek,
1081};
1082
Andy Shevchenko95019c82013-03-04 11:09:33 +02001083static int dtf_results_show(struct seq_file *sf, void *data)
1084{
1085 struct dmatest_info *info = sf->private;
1086 struct dmatest_result *result;
1087 struct dmatest_thread_result *tr;
1088
1089 mutex_lock(&info->results_lock);
1090 list_for_each_entry(result, &info->results, node) {
1091 list_for_each_entry(tr, &result->results, node)
1092 seq_printf(sf, "%s\n",
1093 thread_result_get(result->name, tr));
1094 }
1095
1096 mutex_unlock(&info->results_lock);
1097 return 0;
1098}
1099
1100static int dtf_results_open(struct inode *inode, struct file *file)
1101{
1102 return single_open(file, dtf_results_show, inode->i_private);
1103}
1104
1105static const struct file_operations dtf_results_fops = {
1106 .open = dtf_results_open,
1107 .read = seq_read,
1108 .llseek = seq_lseek,
1109 .release = single_release,
1110};
1111
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001112static int dmatest_register_dbgfs(struct dmatest_info *info)
1113{
1114 struct dentry *d;
1115 struct dmatest_params *params = &info->dbgfs_params;
1116 int ret = -ENOMEM;
1117
1118 d = debugfs_create_dir("dmatest", NULL);
1119 if (IS_ERR(d))
1120 return PTR_ERR(d);
1121 if (!d)
1122 goto err_root;
1123
1124 info->root = d;
1125
1126 /* Copy initial values */
1127 memcpy(params, &info->params, sizeof(*params));
1128
1129 /* Test parameters */
1130
1131 d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root,
1132 (u32 *)&params->buf_size);
1133 if (IS_ERR_OR_NULL(d))
1134 goto err_node;
1135
1136 d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root,
1137 info, &dtf_channel_fops);
1138 if (IS_ERR_OR_NULL(d))
1139 goto err_node;
1140
1141 d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root,
1142 info, &dtf_device_fops);
1143 if (IS_ERR_OR_NULL(d))
1144 goto err_node;
1145
1146 d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root,
1147 (u32 *)&params->threads_per_chan);
1148 if (IS_ERR_OR_NULL(d))
1149 goto err_node;
1150
1151 d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root,
1152 (u32 *)&params->max_channels);
1153 if (IS_ERR_OR_NULL(d))
1154 goto err_node;
1155
1156 d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root,
1157 (u32 *)&params->iterations);
1158 if (IS_ERR_OR_NULL(d))
1159 goto err_node;
1160
1161 d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root,
1162 (u32 *)&params->xor_sources);
1163 if (IS_ERR_OR_NULL(d))
1164 goto err_node;
1165
1166 d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root,
1167 (u32 *)&params->pq_sources);
1168 if (IS_ERR_OR_NULL(d))
1169 goto err_node;
1170
1171 d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root,
1172 (u32 *)&params->timeout);
1173 if (IS_ERR_OR_NULL(d))
1174 goto err_node;
1175
1176 /* Run or stop threaded test */
1177 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
1178 info, &dtf_run_fops);
1179 if (IS_ERR_OR_NULL(d))
1180 goto err_node;
1181
Andy Shevchenko95019c82013-03-04 11:09:33 +02001182 /* Results of test in progress */
1183 d = debugfs_create_file("results", S_IRUGO, info->root, info,
1184 &dtf_results_fops);
1185 if (IS_ERR_OR_NULL(d))
1186 goto err_node;
1187
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001188 return 0;
1189
1190err_node:
1191 debugfs_remove_recursive(info->root);
1192err_root:
1193 pr_err("dmatest: Failed to initialize debugfs\n");
1194 return ret;
1195}
1196
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001197static int __init dmatest_init(void)
1198{
1199 struct dmatest_info *info = &test_info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001200 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001201 int ret;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001202
1203 memset(info, 0, sizeof(*info));
1204
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001205 mutex_init(&info->lock);
Andy Shevchenko838cc702013-03-04 11:09:28 +02001206 INIT_LIST_HEAD(&info->channels);
1207
Andy Shevchenko95019c82013-03-04 11:09:33 +02001208 mutex_init(&info->results_lock);
1209 INIT_LIST_HEAD(&info->results);
1210
Andy Shevchenko838cc702013-03-04 11:09:28 +02001211 /* Set default parameters */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001212 params->buf_size = test_buf_size;
1213 strlcpy(params->channel, test_channel, sizeof(params->channel));
1214 strlcpy(params->device, test_device, sizeof(params->device));
1215 params->threads_per_chan = threads_per_chan;
1216 params->max_channels = max_channels;
1217 params->iterations = iterations;
1218 params->xor_sources = xor_sources;
1219 params->pq_sources = pq_sources;
1220 params->timeout = timeout;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001221
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001222 ret = dmatest_register_dbgfs(info);
1223 if (ret)
1224 return ret;
1225
1226#ifdef MODULE
1227 return 0;
1228#else
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001229 return run_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001230#endif
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001231}
1232/* when compiled-in wait for drivers to load first */
1233late_initcall(dmatest_init);
1234
1235static void __exit dmatest_exit(void)
1236{
1237 struct dmatest_info *info = &test_info;
1238
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001239 debugfs_remove_recursive(info->root);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001240 stop_threaded_test(info);
Andy Shevchenko95019c82013-03-04 11:09:33 +02001241 result_free(info, NULL);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001242}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001243module_exit(dmatest_exit);
1244
Jean Delvaree05503e2011-05-18 16:49:24 +02001245MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001246MODULE_LICENSE("GPL v2");