blob: fc31542e7200c399c8c1e209cdc20984fa2f79f9 [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
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070069/*
70 * Initialization patterns. All bytes in the source buffer has bit 7
71 * set, all bytes in the destination buffer has bit 7 cleared.
72 *
73 * Bit 6 is set for all bytes which are to be copied by the DMA
74 * engine. Bit 5 is set for all bytes which are to be overwritten by
75 * the DMA engine.
76 *
77 * The remaining bits are the inverse of a counter which increments by
78 * one for each byte address.
79 */
80#define PATTERN_SRC 0x80
81#define PATTERN_DST 0x00
82#define PATTERN_COPY 0x40
83#define PATTERN_OVERWRITE 0x20
84#define PATTERN_COUNT_MASK 0x1f
85
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020086struct dmatest_info;
87
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070088struct dmatest_thread {
89 struct list_head node;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020090 struct dmatest_info *info;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070091 struct task_struct *task;
92 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070093 u8 **srcs;
94 u8 **dsts;
95 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070096};
97
98struct dmatest_chan {
99 struct list_head node;
100 struct dma_chan *chan;
101 struct list_head threads;
102};
103
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200104/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200105 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200106 * @buf_size: size of the memcpy test buffer
107 * @channel: bus ID of the channel to test
108 * @device: bus ID of the DMA Engine to test
109 * @threads_per_chan: number of threads to start per channel
110 * @max_channels: maximum number of channels to use
111 * @iterations: iterations before stopping test
112 * @xor_sources: number of xor source buffers
113 * @pq_sources: number of p+q source buffers
114 * @timeout: transfer timeout in msec, -1 for infinite timeout
115 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200116struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200117 unsigned int buf_size;
118 char channel[20];
119 char device[20];
120 unsigned int threads_per_chan;
121 unsigned int max_channels;
122 unsigned int iterations;
123 unsigned int xor_sources;
124 unsigned int pq_sources;
125 int timeout;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200126};
127
128/**
129 * struct dmatest_info - test information.
130 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200131 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200132 */
133struct dmatest_info {
134 /* Test parameters */
135 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200136
137 /* Internal state */
138 struct list_head channels;
139 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200140 struct mutex lock;
141
142 /* debugfs related stuff */
143 struct dentry *root;
144 struct dmatest_params dbgfs_params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200145};
146
147static struct dmatest_info test_info;
148
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200149static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200150 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700151{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200152 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700153 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200154 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700155}
156
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200157static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200158 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700159{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200160 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700161 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200162 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700163}
164
165static unsigned long dmatest_random(void)
166{
167 unsigned long buf;
168
169 get_random_bytes(&buf, sizeof(buf));
170 return buf;
171}
172
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200173static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
174 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700175{
176 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700177 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700178
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700179 for (; (buf = *bufs); bufs++) {
180 for (i = 0; i < start; i++)
181 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
182 for ( ; i < start + len; i++)
183 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700184 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200185 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700186 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
187 buf++;
188 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700189}
190
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200191static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
192 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700193{
194 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700195 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700196
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700197 for (; (buf = *bufs); bufs++) {
198 for (i = 0; i < start; i++)
199 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
200 for ( ; i < start + len; i++)
201 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
202 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200203 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700204 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
205 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700206}
207
208static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
209 unsigned int counter, bool is_srcbuf)
210{
211 u8 diff = actual ^ pattern;
212 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
213 const char *thread_name = current->comm;
214
215 if (is_srcbuf)
216 pr_warning("%s: srcbuf[0x%x] overwritten!"
217 " Expected %02x, got %02x\n",
218 thread_name, index, expected, actual);
219 else if ((pattern & PATTERN_COPY)
220 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
221 pr_warning("%s: dstbuf[0x%x] not copied!"
222 " Expected %02x, got %02x\n",
223 thread_name, index, expected, actual);
224 else if (diff & PATTERN_SRC)
225 pr_warning("%s: dstbuf[0x%x] was copied!"
226 " Expected %02x, got %02x\n",
227 thread_name, index, expected, actual);
228 else
229 pr_warning("%s: dstbuf[0x%x] mismatch!"
230 " Expected %02x, got %02x\n",
231 thread_name, index, expected, actual);
232}
233
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700234static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700235 unsigned int end, unsigned int counter, u8 pattern,
236 bool is_srcbuf)
237{
238 unsigned int i;
239 unsigned int error_count = 0;
240 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700241 u8 expected;
242 u8 *buf;
243 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700244
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700245 for (; (buf = *bufs); bufs++) {
246 counter = counter_orig;
247 for (i = start; i < end; i++) {
248 actual = buf[i];
249 expected = pattern | (~counter & PATTERN_COUNT_MASK);
250 if (actual != expected) {
251 if (error_count < 32)
252 dmatest_mismatch(actual, pattern, i,
253 counter, is_srcbuf);
254 error_count++;
255 }
256 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700257 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700258 }
259
260 if (error_count > 32)
261 pr_warning("%s: %u errors suppressed\n",
262 current->comm, error_count - 32);
263
264 return error_count;
265}
266
Tejun Heoadfa5432011-11-23 09:28:16 -0800267/* poor man's completion - we want to use wait_event_freezable() on it */
268struct dmatest_done {
269 bool done;
270 wait_queue_head_t *wait;
271};
272
273static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700274{
Tejun Heoadfa5432011-11-23 09:28:16 -0800275 struct dmatest_done *done = arg;
276
277 done->done = true;
278 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700279}
280
Andy Shevchenko632fd282012-12-17 15:59:52 -0800281static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
282 unsigned int count)
283{
284 while (count--)
285 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
286}
287
288static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
289 unsigned int count)
290{
291 while (count--)
292 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
293}
294
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900295static unsigned int min_odd(unsigned int x, unsigned int y)
296{
297 unsigned int val = min(x, y);
298
299 return val % 2 ? val : val - 1;
300}
301
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700302/*
303 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700304 * offsets for a given operation type until it is told to exit by
305 * kthread_stop(). There may be multiple threads running this function
306 * in parallel for a single channel, and there may be multiple channels
307 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700308 *
309 * Before each test, the source and destination buffer is initialized
310 * with a known pattern. This pattern is different depending on
311 * whether it's in an area which is supposed to be copied or
312 * overwritten, and different in the source and destination buffers.
313 * So if the DMA engine doesn't copy exactly what we tell it to copy,
314 * we'll notice.
315 */
316static int dmatest_func(void *data)
317{
Tejun Heoadfa5432011-11-23 09:28:16 -0800318 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700319 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800320 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200321 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200322 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700323 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900324 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700325 const char *thread_name;
326 unsigned int src_off, dst_off, len;
327 unsigned int error_count;
328 unsigned int failed_tests = 0;
329 unsigned int total_tests = 0;
330 dma_cookie_t cookie;
331 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700332 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200333 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700334 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700335 int src_cnt;
336 int dst_cnt;
337 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700338
339 thread_name = current->comm;
Tejun Heoadfa5432011-11-23 09:28:16 -0800340 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700341
342 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700343
344 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200345 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200346 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700347 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900348 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700349 if (thread->type == DMA_MEMCPY)
350 src_cnt = dst_cnt = 1;
351 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900352 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200353 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700354 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700355 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900356 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200357 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700358 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200359
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200360 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200361 if (!pq_coefs)
362 goto err_thread_type;
363
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100364 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700365 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700366 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200367 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700368
369 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
370 if (!thread->srcs)
371 goto err_srcs;
372 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200373 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700374 if (!thread->srcs[i])
375 goto err_srcbuf;
376 }
377 thread->srcs[i] = NULL;
378
379 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
380 if (!thread->dsts)
381 goto err_dsts;
382 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200383 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700384 if (!thread->dsts[i])
385 goto err_dstbuf;
386 }
387 thread->dsts[i] = NULL;
388
Dan Williamse44e0aa2009-03-25 09:13:25 -0700389 set_user_nice(current, 10);
390
Ira Snyderb203bd32011-03-03 07:54:53 +0000391 /*
392 * src buffers are freed by the DMAEngine code with dma_unmap_single()
393 * dst buffers are freed by ourselves below
394 */
395 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
396 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700397
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200398 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200399 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700400 struct dma_async_tx_descriptor *tx = NULL;
401 dma_addr_t dma_srcs[src_cnt];
402 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700403 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700404
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700405 total_tests++;
406
Dan Williams83544ae2009-09-08 17:42:53 -0700407 /* honor alignment restrictions */
408 if (thread->type == DMA_MEMCPY)
409 align = dev->copy_align;
410 else if (thread->type == DMA_XOR)
411 align = dev->xor_align;
412 else if (thread->type == DMA_PQ)
413 align = dev->pq_align;
414
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200415 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100416 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200417 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100418 break;
419 }
420
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200421 len = dmatest_random() % params->buf_size + 1;
Dan Williams83544ae2009-09-08 17:42:53 -0700422 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100423 if (!len)
424 len = 1 << align;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200425 src_off = dmatest_random() % (params->buf_size - len + 1);
426 dst_off = dmatest_random() % (params->buf_size - len + 1);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100427
Dan Williams83544ae2009-09-08 17:42:53 -0700428 src_off = (src_off >> align) << align;
429 dst_off = (dst_off >> align) << align;
430
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200431 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
432 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700433
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700434 for (i = 0; i < src_cnt; i++) {
435 u8 *buf = thread->srcs[i] + src_off;
436
437 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
438 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800439 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
440 if (ret) {
441 unmap_src(dev->dev, dma_srcs, len, i);
442 pr_warn("%s: #%u: mapping error %d with "
443 "src_off=0x%x len=0x%x\n",
444 thread_name, total_tests - 1, ret,
445 src_off, len);
446 failed_tests++;
447 continue;
448 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700449 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700450 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700451 for (i = 0; i < dst_cnt; i++) {
452 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200453 params->buf_size,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700454 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800455 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
456 if (ret) {
457 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200458 unmap_dst(dev->dev, dma_dsts, params->buf_size,
459 i);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800460 pr_warn("%s: #%u: mapping error %d with "
461 "dst_off=0x%x len=0x%x\n",
462 thread_name, total_tests - 1, ret,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200463 dst_off, params->buf_size);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800464 failed_tests++;
465 continue;
466 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700467 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700468
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700469 if (thread->type == DMA_MEMCPY)
470 tx = dev->device_prep_dma_memcpy(chan,
471 dma_dsts[0] + dst_off,
472 dma_srcs[0], len,
473 flags);
474 else if (thread->type == DMA_XOR)
475 tx = dev->device_prep_dma_xor(chan,
476 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700477 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700478 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700479 else if (thread->type == DMA_PQ) {
480 dma_addr_t dma_pq[dst_cnt];
481
482 for (i = 0; i < dst_cnt; i++)
483 dma_pq[i] = dma_dsts[i] + dst_off;
484 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100485 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700486 len, flags);
487 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700488
Atsushi Nemotod86be862009-01-13 09:22:20 -0700489 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800490 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200491 unmap_dst(dev->dev, dma_dsts, params->buf_size,
492 dst_cnt);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700493 pr_warning("%s: #%u: prep error with src_off=0x%x "
494 "dst_off=0x%x len=0x%x\n",
495 thread_name, total_tests - 1,
496 src_off, dst_off, len);
497 msleep(100);
498 failed_tests++;
499 continue;
500 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700501
Tejun Heoadfa5432011-11-23 09:28:16 -0800502 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700503 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800504 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700505 cookie = tx->tx_submit(tx);
506
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700507 if (dma_submit_error(cookie)) {
508 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
509 "dst_off=0x%x len=0x%x\n",
510 thread_name, total_tests - 1, cookie,
511 src_off, dst_off, len);
512 msleep(100);
513 failed_tests++;
514 continue;
515 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700516 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700517
Andy Shevchenko77101ce2013-03-04 11:09:25 +0200518 wait_event_freezable_timeout(done_wait,
519 done.done || kthread_should_stop(),
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200520 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200521
Dan Williamse44e0aa2009-03-25 09:13:25 -0700522 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700523
Tejun Heoadfa5432011-11-23 09:28:16 -0800524 if (!done.done) {
525 /*
526 * We're leaving the timed out dma operation with
527 * dangling pointer to done_wait. To make this
528 * correct, we'll need to allocate wait_done for
529 * each test iteration and perform "who's gonna
530 * free it this time?" dancing. For now, just
531 * leave it dangling.
532 */
Dan Williamse44e0aa2009-03-25 09:13:25 -0700533 pr_warning("%s: #%u: test timed out\n",
534 thread_name, total_tests - 1);
535 failed_tests++;
536 continue;
537 } else if (status != DMA_SUCCESS) {
538 pr_warning("%s: #%u: got completion callback,"
539 " but status is \'%s\'\n",
540 thread_name, total_tests - 1,
541 status == DMA_ERROR ? "error" : "in progress");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700542 failed_tests++;
543 continue;
544 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700545
Atsushi Nemotod86be862009-01-13 09:22:20 -0700546 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200547 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700548
549 error_count = 0;
550
551 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700552 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700553 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700554 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700555 src_off + len, src_off,
556 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700557 error_count += dmatest_verify(thread->srcs, src_off + len,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200558 params->buf_size, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700559 PATTERN_SRC, true);
560
561 pr_debug("%s: verifying dest buffer...\n",
562 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700563 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700564 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700565 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700566 dst_off + len, src_off,
567 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700568 error_count += dmatest_verify(thread->dsts, dst_off + len,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200569 params->buf_size, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700570 PATTERN_DST, false);
571
572 if (error_count) {
573 pr_warning("%s: #%u: %u errors with "
574 "src_off=0x%x dst_off=0x%x len=0x%x\n",
575 thread_name, total_tests - 1, error_count,
576 src_off, dst_off, len);
577 failed_tests++;
578 } else {
579 pr_debug("%s: #%u: No errors with "
580 "src_off=0x%x dst_off=0x%x len=0x%x\n",
581 thread_name, total_tests - 1,
582 src_off, dst_off, len);
583 }
584 }
585
586 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700587 for (i = 0; thread->dsts[i]; i++)
588 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700589err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700590 kfree(thread->dsts);
591err_dsts:
592 for (i = 0; thread->srcs[i]; i++)
593 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700594err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700595 kfree(thread->srcs);
596err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200597 kfree(pq_coefs);
598err_thread_type:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700599 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
600 thread_name, total_tests, failed_tests, ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200601
Viresh Kumar9704efa2011-07-29 16:21:57 +0530602 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000603 if (ret)
604 dmaengine_terminate_all(chan);
605
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200606 if (params->iterations > 0)
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200607 while (!kthread_should_stop()) {
Yong Zhangb953df72010-02-05 21:52:37 +0800608 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200609 interruptible_sleep_on(&wait_dmatest_exit);
610 }
611
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700612 return ret;
613}
614
615static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
616{
617 struct dmatest_thread *thread;
618 struct dmatest_thread *_thread;
619 int ret;
620
621 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
622 ret = kthread_stop(thread->task);
623 pr_debug("dmatest: thread %s exited with status %d\n",
624 thread->task->comm, ret);
625 list_del(&thread->node);
626 kfree(thread);
627 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530628
629 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000630 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530631
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700632 kfree(dtc);
633}
634
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200635static int dmatest_add_threads(struct dmatest_info *info,
636 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700637{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200638 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700639 struct dmatest_thread *thread;
640 struct dma_chan *chan = dtc->chan;
641 char *op;
642 unsigned int i;
643
644 if (type == DMA_MEMCPY)
645 op = "copy";
646 else if (type == DMA_XOR)
647 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700648 else if (type == DMA_PQ)
649 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700650 else
651 return -EINVAL;
652
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200653 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700654 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
655 if (!thread) {
656 pr_warning("dmatest: No memory for %s-%s%u\n",
657 dma_chan_name(chan), op, i);
658
659 break;
660 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200661 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700662 thread->chan = dtc->chan;
663 thread->type = type;
664 smp_wmb();
665 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
666 dma_chan_name(chan), op, i);
667 if (IS_ERR(thread->task)) {
668 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
669 dma_chan_name(chan), op, i);
670 kfree(thread);
671 break;
672 }
673
674 /* srcbuf and dstbuf are allocated by the thread itself */
675
676 list_add_tail(&thread->node, &dtc->threads);
677 }
678
679 return i;
680}
681
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200682static int dmatest_add_channel(struct dmatest_info *info,
683 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700684{
685 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700686 struct dma_device *dma_dev = chan->device;
687 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400688 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700689
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700690 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700691 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700692 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700693 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700694 }
695
696 dtc->chan = chan;
697 INIT_LIST_HEAD(&dtc->threads);
698
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700699 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200700 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200701 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700702 }
703 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200704 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200705 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700706 }
Dan Williams58691d62009-08-29 19:09:27 -0700707 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200708 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700709 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700710 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700711
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700712 pr_info("dmatest: Started %u threads using %s\n",
713 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700714
Andy Shevchenko838cc702013-03-04 11:09:28 +0200715 list_add_tail(&dtc->node, &info->channels);
716 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700717
Dan Williams33df8ca2009-01-06 11:38:15 -0700718 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700719}
720
Dan Williams7dd60252009-01-06 11:38:19 -0700721static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700722{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200723 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200724
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200725 if (!dmatest_match_channel(params, chan) ||
726 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700727 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700728 else
Dan Williams7dd60252009-01-06 11:38:19 -0700729 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700730}
731
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200732static int __run_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700733{
Dan Williams33df8ca2009-01-06 11:38:15 -0700734 dma_cap_mask_t mask;
735 struct dma_chan *chan;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200736 struct dmatest_params *params = &info->params;
Dan Williams33df8ca2009-01-06 11:38:15 -0700737 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700738
Dan Williams33df8ca2009-01-06 11:38:15 -0700739 dma_cap_zero(mask);
740 dma_cap_set(DMA_MEMCPY, mask);
741 for (;;) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200742 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700743 if (chan) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200744 err = dmatest_add_channel(info, chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700745 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700746 dma_release_channel(chan);
747 break; /* add_channel failed, punt */
748 }
749 } else
750 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200751 if (params->max_channels &&
752 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700753 break; /* we have all we need */
754 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700755 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700756}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700757
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200758#ifndef MODULE
759static int run_threaded_test(struct dmatest_info *info)
760{
761 int ret;
762
763 mutex_lock(&info->lock);
764 ret = __run_threaded_test(info);
765 mutex_unlock(&info->lock);
766 return ret;
767}
768#endif
769
770static void __stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700771{
Dan Williams33df8ca2009-01-06 11:38:15 -0700772 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700773 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700774
Andy Shevchenko838cc702013-03-04 11:09:28 +0200775 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700776 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700777 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700778 dmatest_cleanup_channel(dtc);
Andy Shevchenko838cc702013-03-04 11:09:28 +0200779 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
Dan Williams7cbd4872009-03-04 16:06:03 -0700780 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700781 }
Andy Shevchenko838cc702013-03-04 11:09:28 +0200782
783 info->nr_channels = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700784}
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200785
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200786static void stop_threaded_test(struct dmatest_info *info)
787{
788 mutex_lock(&info->lock);
789 __stop_threaded_test(info);
790 mutex_unlock(&info->lock);
791}
792
793static int __restart_threaded_test(struct dmatest_info *info, bool run)
794{
795 struct dmatest_params *params = &info->params;
796 int ret;
797
798 /* Stop any running test first */
799 __stop_threaded_test(info);
800
801 if (run == false)
802 return 0;
803
804 /* Copy test parameters */
805 memcpy(params, &info->dbgfs_params, sizeof(*params));
806
807 /* Run test with new parameters */
808 ret = __run_threaded_test(info);
809 if (ret) {
810 __stop_threaded_test(info);
811 pr_err("dmatest: Can't run test\n");
812 }
813
814 return ret;
815}
816
817static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos,
818 const void __user *from, size_t count)
819{
820 char tmp[20];
821 ssize_t len;
822
823 len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count);
824 if (len >= 0) {
825 tmp[len] = '\0';
826 strlcpy(to, strim(tmp), available);
827 }
828
829 return len;
830}
831
832static ssize_t dtf_read_channel(struct file *file, char __user *buf,
833 size_t count, loff_t *ppos)
834{
835 struct dmatest_info *info = file->private_data;
836 return simple_read_from_buffer(buf, count, ppos,
837 info->dbgfs_params.channel,
838 strlen(info->dbgfs_params.channel));
839}
840
841static ssize_t dtf_write_channel(struct file *file, const char __user *buf,
842 size_t size, loff_t *ppos)
843{
844 struct dmatest_info *info = file->private_data;
845 return dtf_write_string(info->dbgfs_params.channel,
846 sizeof(info->dbgfs_params.channel),
847 ppos, buf, size);
848}
849
850static const struct file_operations dtf_channel_fops = {
851 .read = dtf_read_channel,
852 .write = dtf_write_channel,
853 .open = simple_open,
854 .llseek = default_llseek,
855};
856
857static ssize_t dtf_read_device(struct file *file, char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 struct dmatest_info *info = file->private_data;
861 return simple_read_from_buffer(buf, count, ppos,
862 info->dbgfs_params.device,
863 strlen(info->dbgfs_params.device));
864}
865
866static ssize_t dtf_write_device(struct file *file, const char __user *buf,
867 size_t size, loff_t *ppos)
868{
869 struct dmatest_info *info = file->private_data;
870 return dtf_write_string(info->dbgfs_params.device,
871 sizeof(info->dbgfs_params.device),
872 ppos, buf, size);
873}
874
875static const struct file_operations dtf_device_fops = {
876 .read = dtf_read_device,
877 .write = dtf_write_device,
878 .open = simple_open,
879 .llseek = default_llseek,
880};
881
882static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
883 size_t count, loff_t *ppos)
884{
885 struct dmatest_info *info = file->private_data;
886 char buf[3];
887
888 mutex_lock(&info->lock);
889 if (info->nr_channels)
890 buf[0] = 'Y';
891 else
892 buf[0] = 'N';
893 mutex_unlock(&info->lock);
894 buf[1] = '\n';
895 buf[2] = 0x00;
896 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
897}
898
899static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
900 size_t count, loff_t *ppos)
901{
902 struct dmatest_info *info = file->private_data;
903 char buf[16];
904 bool bv;
905 int ret = 0;
906
907 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
908 return -EFAULT;
909
910 if (strtobool(buf, &bv) == 0) {
911 mutex_lock(&info->lock);
912 ret = __restart_threaded_test(info, bv);
913 mutex_unlock(&info->lock);
914 }
915
916 return ret ? ret : count;
917}
918
919static const struct file_operations dtf_run_fops = {
920 .read = dtf_read_run,
921 .write = dtf_write_run,
922 .open = simple_open,
923 .llseek = default_llseek,
924};
925
926static int dmatest_register_dbgfs(struct dmatest_info *info)
927{
928 struct dentry *d;
929 struct dmatest_params *params = &info->dbgfs_params;
930 int ret = -ENOMEM;
931
932 d = debugfs_create_dir("dmatest", NULL);
933 if (IS_ERR(d))
934 return PTR_ERR(d);
935 if (!d)
936 goto err_root;
937
938 info->root = d;
939
940 /* Copy initial values */
941 memcpy(params, &info->params, sizeof(*params));
942
943 /* Test parameters */
944
945 d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root,
946 (u32 *)&params->buf_size);
947 if (IS_ERR_OR_NULL(d))
948 goto err_node;
949
950 d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root,
951 info, &dtf_channel_fops);
952 if (IS_ERR_OR_NULL(d))
953 goto err_node;
954
955 d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root,
956 info, &dtf_device_fops);
957 if (IS_ERR_OR_NULL(d))
958 goto err_node;
959
960 d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root,
961 (u32 *)&params->threads_per_chan);
962 if (IS_ERR_OR_NULL(d))
963 goto err_node;
964
965 d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root,
966 (u32 *)&params->max_channels);
967 if (IS_ERR_OR_NULL(d))
968 goto err_node;
969
970 d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root,
971 (u32 *)&params->iterations);
972 if (IS_ERR_OR_NULL(d))
973 goto err_node;
974
975 d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root,
976 (u32 *)&params->xor_sources);
977 if (IS_ERR_OR_NULL(d))
978 goto err_node;
979
980 d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root,
981 (u32 *)&params->pq_sources);
982 if (IS_ERR_OR_NULL(d))
983 goto err_node;
984
985 d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root,
986 (u32 *)&params->timeout);
987 if (IS_ERR_OR_NULL(d))
988 goto err_node;
989
990 /* Run or stop threaded test */
991 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
992 info, &dtf_run_fops);
993 if (IS_ERR_OR_NULL(d))
994 goto err_node;
995
996 return 0;
997
998err_node:
999 debugfs_remove_recursive(info->root);
1000err_root:
1001 pr_err("dmatest: Failed to initialize debugfs\n");
1002 return ret;
1003}
1004
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001005static int __init dmatest_init(void)
1006{
1007 struct dmatest_info *info = &test_info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001008 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001009 int ret;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001010
1011 memset(info, 0, sizeof(*info));
1012
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001013 mutex_init(&info->lock);
Andy Shevchenko838cc702013-03-04 11:09:28 +02001014 INIT_LIST_HEAD(&info->channels);
1015
1016 /* Set default parameters */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001017 params->buf_size = test_buf_size;
1018 strlcpy(params->channel, test_channel, sizeof(params->channel));
1019 strlcpy(params->device, test_device, sizeof(params->device));
1020 params->threads_per_chan = threads_per_chan;
1021 params->max_channels = max_channels;
1022 params->iterations = iterations;
1023 params->xor_sources = xor_sources;
1024 params->pq_sources = pq_sources;
1025 params->timeout = timeout;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001026
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001027 ret = dmatest_register_dbgfs(info);
1028 if (ret)
1029 return ret;
1030
1031#ifdef MODULE
1032 return 0;
1033#else
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001034 return run_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001035#endif
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001036}
1037/* when compiled-in wait for drivers to load first */
1038late_initcall(dmatest_init);
1039
1040static void __exit dmatest_exit(void)
1041{
1042 struct dmatest_info *info = &test_info;
1043
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001044 debugfs_remove_recursive(info->root);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001045 stop_threaded_test(info);
1046}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001047module_exit(dmatest_exit);
1048
Jean Delvaree05503e2011-05-18 16:49:24 +02001049MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001050MODULE_LICENSE("GPL v2");