blob: dd4d84d556d5a39af024337352a6e7638ea71165 [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 */
Dan Williams872f05c2013-11-06 16:29:58 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070013#include <linux/delay.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000014#include <linux/dma-mapping.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070015#include <linux/dmaengine.h>
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +020016#include <linux/freezer.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070017#include <linux/init.h>
18#include <linux/kthread.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070023#include <linux/wait.h>
24
25static unsigned int test_buf_size = 16384;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030026module_param(test_buf_size, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070027MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
28
Kay Sievers06190d82008-11-11 13:12:33 -070029static char test_channel[20];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030030module_param_string(channel, test_channel, sizeof(test_channel),
31 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070032MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
33
Kay Sievers06190d82008-11-11 13:12:33 -070034static char test_device[20];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030035module_param_string(device, test_device, sizeof(test_device),
36 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070037MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
38
39static unsigned int threads_per_chan = 1;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030040module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070041MODULE_PARM_DESC(threads_per_chan,
42 "Number of threads to start per channel (default: 1)");
43
44static unsigned int max_channels;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030045module_param(max_channels, uint, S_IRUGO | S_IWUSR);
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;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030050module_param(iterations, uint, S_IRUGO | S_IWUSR);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020051MODULE_PARM_DESC(iterations,
52 "Iterations before stopping test (default: infinite)");
53
Dan Williamsb54d5cb2009-03-25 09:13:25 -070054static unsigned int xor_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030055module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
Dan Williamsb54d5cb2009-03-25 09:13:25 -070056MODULE_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;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030060module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
Dan Williams58691d62009-08-29 19:09:27 -070061MODULE_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;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030065module_param(timeout, uint, S_IRUGO | S_IWUSR);
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
Dan Williamse3b9c342013-11-06 16:30:05 -080069static bool noverify;
70module_param(noverify, bool, S_IRUGO | S_IWUSR);
71MODULE_PARM_DESC(noverify, "Disable random data setup and verification");
72
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020073/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020074 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020075 * @buf_size: size of the memcpy test buffer
76 * @channel: bus ID of the channel to test
77 * @device: bus ID of the DMA Engine to test
78 * @threads_per_chan: number of threads to start per channel
79 * @max_channels: maximum number of channels to use
80 * @iterations: iterations before stopping test
81 * @xor_sources: number of xor source buffers
82 * @pq_sources: number of p+q source buffers
83 * @timeout: transfer timeout in msec, -1 for infinite timeout
84 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020085struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020086 unsigned int buf_size;
87 char channel[20];
88 char device[20];
89 unsigned int threads_per_chan;
90 unsigned int max_channels;
91 unsigned int iterations;
92 unsigned int xor_sources;
93 unsigned int pq_sources;
94 int timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -080095 bool noverify;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020096};
97
98/**
99 * struct dmatest_info - test information.
100 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200101 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200102 */
Dan Williamsa310d032013-11-06 16:30:01 -0800103static struct dmatest_info {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200104 /* Test parameters */
105 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200106
107 /* Internal state */
108 struct list_head channels;
109 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200110 struct mutex lock;
Dan Williamsa310d032013-11-06 16:30:01 -0800111 bool did_init;
112} test_info = {
113 .channels = LIST_HEAD_INIT(test_info.channels),
114 .lock = __MUTEX_INITIALIZER(test_info.lock),
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200115};
116
Dan Williamsa310d032013-11-06 16:30:01 -0800117static int dmatest_run_set(const char *val, const struct kernel_param *kp);
118static int dmatest_run_get(char *val, const struct kernel_param *kp);
119static struct kernel_param_ops run_ops = {
120 .set = dmatest_run_set,
121 .get = dmatest_run_get,
122};
123static bool dmatest_run;
124module_param_cb(run, &run_ops, &dmatest_run, S_IRUGO | S_IWUSR);
125MODULE_PARM_DESC(run, "Run the test (default: false)");
126
127/* Maximum amount of mismatched bytes in buffer to print */
128#define MAX_ERROR_COUNT 32
129
130/*
131 * Initialization patterns. All bytes in the source buffer has bit 7
132 * set, all bytes in the destination buffer has bit 7 cleared.
133 *
134 * Bit 6 is set for all bytes which are to be copied by the DMA
135 * engine. Bit 5 is set for all bytes which are to be overwritten by
136 * the DMA engine.
137 *
138 * The remaining bits are the inverse of a counter which increments by
139 * one for each byte address.
140 */
141#define PATTERN_SRC 0x80
142#define PATTERN_DST 0x00
143#define PATTERN_COPY 0x40
144#define PATTERN_OVERWRITE 0x20
145#define PATTERN_COUNT_MASK 0x1f
146
147struct dmatest_thread {
148 struct list_head node;
149 struct dmatest_info *info;
150 struct task_struct *task;
151 struct dma_chan *chan;
152 u8 **srcs;
153 u8 **dsts;
154 enum dma_transaction_type type;
155 bool done;
156};
157
158struct dmatest_chan {
159 struct list_head node;
160 struct dma_chan *chan;
161 struct list_head threads;
162};
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200163
Dan Williams2d88ce72013-11-06 16:30:09 -0800164static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
165static bool wait;
166
167static bool is_threaded_test_run(struct dmatest_info *info)
168{
169 struct dmatest_chan *dtc;
170
171 list_for_each_entry(dtc, &info->channels, node) {
172 struct dmatest_thread *thread;
173
174 list_for_each_entry(thread, &dtc->threads, node) {
175 if (!thread->done)
176 return true;
177 }
178 }
179
180 return false;
181}
182
183static int dmatest_wait_get(char *val, const struct kernel_param *kp)
184{
185 struct dmatest_info *info = &test_info;
186 struct dmatest_params *params = &info->params;
187
188 if (params->iterations)
189 wait_event(thread_wait, !is_threaded_test_run(info));
190 wait = true;
191 return param_get_bool(val, kp);
192}
193
194static struct kernel_param_ops wait_ops = {
195 .get = dmatest_wait_get,
196 .set = param_set_bool,
197};
198module_param_cb(wait, &wait_ops, &wait, S_IRUGO);
199MODULE_PARM_DESC(wait, "Wait for tests to complete (default: false)");
200
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200201static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200202 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700203{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200204 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700205 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200206 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700207}
208
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200209static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200210 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700211{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200212 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700213 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200214 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700215}
216
217static unsigned long dmatest_random(void)
218{
219 unsigned long buf;
220
Dan Williamsbe9fa5a2013-11-06 16:30:03 -0800221 prandom_bytes(&buf, sizeof(buf));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700222 return buf;
223}
224
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200225static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
226 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700227{
228 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700229 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700230
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700231 for (; (buf = *bufs); bufs++) {
232 for (i = 0; i < start; i++)
233 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
234 for ( ; i < start + len; i++)
235 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700236 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200237 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700238 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
239 buf++;
240 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700241}
242
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200243static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
244 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700245{
246 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700247 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700248
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700249 for (; (buf = *bufs); bufs++) {
250 for (i = 0; i < start; i++)
251 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
252 for ( ; i < start + len; i++)
253 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
254 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200255 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700256 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
257 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700258}
259
Dan Williams7b610172013-11-06 16:29:57 -0800260static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
261 unsigned int counter, bool is_srcbuf)
262{
263 u8 diff = actual ^ pattern;
264 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
265 const char *thread_name = current->comm;
266
267 if (is_srcbuf)
268 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
269 thread_name, index, expected, actual);
270 else if ((pattern & PATTERN_COPY)
271 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
272 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
273 thread_name, index, expected, actual);
274 else if (diff & PATTERN_SRC)
275 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
276 thread_name, index, expected, actual);
277 else
278 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
279 thread_name, index, expected, actual);
280}
281
282static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
283 unsigned int end, unsigned int counter, u8 pattern,
284 bool is_srcbuf)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700285{
286 unsigned int i;
287 unsigned int error_count = 0;
288 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700289 u8 expected;
290 u8 *buf;
291 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700292
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700293 for (; (buf = *bufs); bufs++) {
294 counter = counter_orig;
295 for (i = start; i < end; i++) {
296 actual = buf[i];
297 expected = pattern | (~counter & PATTERN_COUNT_MASK);
298 if (actual != expected) {
Dan Williams7b610172013-11-06 16:29:57 -0800299 if (error_count < MAX_ERROR_COUNT)
300 dmatest_mismatch(actual, pattern, i,
301 counter, is_srcbuf);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700302 error_count++;
303 }
304 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700305 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700306 }
307
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200308 if (error_count > MAX_ERROR_COUNT)
Dan Williams7b610172013-11-06 16:29:57 -0800309 pr_warn("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200310 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700311
312 return error_count;
313}
314
Tejun Heoadfa5432011-11-23 09:28:16 -0800315/* poor man's completion - we want to use wait_event_freezable() on it */
316struct dmatest_done {
317 bool done;
318 wait_queue_head_t *wait;
319};
320
321static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700322{
Tejun Heoadfa5432011-11-23 09:28:16 -0800323 struct dmatest_done *done = arg;
324
325 done->done = true;
326 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700327}
328
Andy Shevchenko632fd282012-12-17 15:59:52 -0800329static inline void unmap_src(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_TO_DEVICE);
334}
335
336static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
337 unsigned int count)
338{
339 while (count--)
340 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
341}
342
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900343static unsigned int min_odd(unsigned int x, unsigned int y)
344{
345 unsigned int val = min(x, y);
346
347 return val % 2 ? val : val - 1;
348}
349
Dan Williams872f05c2013-11-06 16:29:58 -0800350static void result(const char *err, unsigned int n, unsigned int src_off,
351 unsigned int dst_off, unsigned int len, unsigned long data)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200352{
Dan Williams872f05c2013-11-06 16:29:58 -0800353 pr_info("%s: result #%u: '%s' with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
354 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200355}
356
Dan Williams872f05c2013-11-06 16:29:58 -0800357static void dbg_result(const char *err, unsigned int n, unsigned int src_off,
358 unsigned int dst_off, unsigned int len,
359 unsigned long data)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200360{
Dan Williams872f05c2013-11-06 16:29:58 -0800361 pr_debug("%s: result #%u: '%s' with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
362 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200363}
364
Dan Williams86727442013-11-06 16:30:07 -0800365static unsigned long long dmatest_persec(s64 runtime, unsigned int val)
366{
367 unsigned long long per_sec = 1000000;
368
369 if (runtime <= 0)
370 return 0;
371
372 /* drop precision until runtime is 32-bits */
373 while (runtime > UINT_MAX) {
374 runtime >>= 1;
375 per_sec <<= 1;
376 }
377
378 per_sec *= val;
379 do_div(per_sec, runtime);
380 return per_sec;
381}
382
383static unsigned long long dmatest_KBs(s64 runtime, unsigned long long len)
384{
385 return dmatest_persec(runtime, len >> 10);
386}
387
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700388/*
389 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700390 * offsets for a given operation type until it is told to exit by
391 * kthread_stop(). There may be multiple threads running this function
392 * in parallel for a single channel, and there may be multiple channels
393 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700394 *
395 * Before each test, the source and destination buffer is initialized
396 * with a known pattern. This pattern is different depending on
397 * whether it's in an area which is supposed to be copied or
398 * overwritten, and different in the source and destination buffers.
399 * So if the DMA engine doesn't copy exactly what we tell it to copy,
400 * we'll notice.
401 */
402static int dmatest_func(void *data)
403{
Tejun Heoadfa5432011-11-23 09:28:16 -0800404 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700405 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800406 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200407 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200408 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700409 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900410 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700411 unsigned int src_off, dst_off, len;
412 unsigned int error_count;
413 unsigned int failed_tests = 0;
414 unsigned int total_tests = 0;
415 dma_cookie_t cookie;
416 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700417 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200418 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700419 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700420 int src_cnt;
421 int dst_cnt;
422 int i;
Dan Williams86727442013-11-06 16:30:07 -0800423 ktime_t ktime;
424 s64 runtime = 0;
425 unsigned long long total_len = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700426
Tejun Heoadfa5432011-11-23 09:28:16 -0800427 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700428
429 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700430
431 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200432 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200433 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700434 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900435 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700436 if (thread->type == DMA_MEMCPY)
437 src_cnt = dst_cnt = 1;
438 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900439 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200440 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700441 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700442 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900443 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200444 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700445 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200446
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200447 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200448 if (!pq_coefs)
449 goto err_thread_type;
450
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100451 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700452 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700453 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200454 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700455
456 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
457 if (!thread->srcs)
458 goto err_srcs;
459 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200460 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700461 if (!thread->srcs[i])
462 goto err_srcbuf;
463 }
464 thread->srcs[i] = NULL;
465
466 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
467 if (!thread->dsts)
468 goto err_dsts;
469 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200470 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700471 if (!thread->dsts[i])
472 goto err_dstbuf;
473 }
474 thread->dsts[i] = NULL;
475
Dan Williamse44e0aa2009-03-25 09:13:25 -0700476 set_user_nice(current, 10);
477
Ira Snyderb203bd32011-03-03 07:54:53 +0000478 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200479 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000480 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200481 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700482
Dan Williams86727442013-11-06 16:30:07 -0800483 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200484 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200485 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700486 struct dma_async_tx_descriptor *tx = NULL;
487 dma_addr_t dma_srcs[src_cnt];
488 dma_addr_t dma_dsts[dst_cnt];
Dan Williams83544ae2009-09-08 17:42:53 -0700489 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700490
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700491 total_tests++;
492
Dan Williams83544ae2009-09-08 17:42:53 -0700493 /* honor alignment restrictions */
494 if (thread->type == DMA_MEMCPY)
495 align = dev->copy_align;
496 else if (thread->type == DMA_XOR)
497 align = dev->xor_align;
498 else if (thread->type == DMA_PQ)
499 align = dev->pq_align;
500
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200501 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100502 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200503 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100504 break;
505 }
506
Dan Williamse3b9c342013-11-06 16:30:05 -0800507 if (params->noverify) {
508 len = params->buf_size;
509 src_off = 0;
510 dst_off = 0;
511 } else {
512 len = dmatest_random() % params->buf_size + 1;
513 len = (len >> align) << align;
514 if (!len)
515 len = 1 << align;
516 src_off = dmatest_random() % (params->buf_size - len + 1);
517 dst_off = dmatest_random() % (params->buf_size - len + 1);
518
519 src_off = (src_off >> align) << align;
520 dst_off = (dst_off >> align) << align;
521
522 dmatest_init_srcs(thread->srcs, src_off, len,
523 params->buf_size);
524 dmatest_init_dsts(thread->dsts, dst_off, len,
525 params->buf_size);
526 }
527
Dan Williams83544ae2009-09-08 17:42:53 -0700528 len = (len >> align) << align;
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100529 if (!len)
530 len = 1 << align;
Dan Williams86727442013-11-06 16:30:07 -0800531 total_len += len;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700532
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700533 for (i = 0; i < src_cnt; i++) {
534 u8 *buf = thread->srcs[i] + src_off;
535
536 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
537 DMA_TO_DEVICE);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800538 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
539 if (ret) {
540 unmap_src(dev->dev, dma_srcs, len, i);
Dan Williams872f05c2013-11-06 16:29:58 -0800541 result("src mapping error", total_tests,
542 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800543 failed_tests++;
544 continue;
545 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700546 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700547 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700548 for (i = 0; i < dst_cnt; i++) {
549 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200550 params->buf_size,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700551 DMA_BIDIRECTIONAL);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800552 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
553 if (ret) {
554 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200555 unmap_dst(dev->dev, dma_dsts, params->buf_size,
556 i);
Dan Williams872f05c2013-11-06 16:29:58 -0800557 result("dst mapping error", total_tests,
558 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800559 failed_tests++;
560 continue;
561 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700562 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700563
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700564 if (thread->type == DMA_MEMCPY)
565 tx = dev->device_prep_dma_memcpy(chan,
566 dma_dsts[0] + dst_off,
567 dma_srcs[0], len,
568 flags);
569 else if (thread->type == DMA_XOR)
570 tx = dev->device_prep_dma_xor(chan,
571 dma_dsts[0] + dst_off,
Dan Williams67b91242010-02-28 22:20:18 -0700572 dma_srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700573 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700574 else if (thread->type == DMA_PQ) {
575 dma_addr_t dma_pq[dst_cnt];
576
577 for (i = 0; i < dst_cnt; i++)
578 dma_pq[i] = dma_dsts[i] + dst_off;
579 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100580 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700581 len, flags);
582 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700583
Atsushi Nemotod86be862009-01-13 09:22:20 -0700584 if (!tx) {
Andy Shevchenko632fd282012-12-17 15:59:52 -0800585 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200586 unmap_dst(dev->dev, dma_dsts, params->buf_size,
587 dst_cnt);
Dan Williams872f05c2013-11-06 16:29:58 -0800588 result("prep error", total_tests, src_off,
589 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700590 msleep(100);
591 failed_tests++;
592 continue;
593 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700594
Tejun Heoadfa5432011-11-23 09:28:16 -0800595 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700596 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800597 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700598 cookie = tx->tx_submit(tx);
599
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700600 if (dma_submit_error(cookie)) {
Dan Williams872f05c2013-11-06 16:29:58 -0800601 result("submit error", total_tests, src_off,
602 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700603 msleep(100);
604 failed_tests++;
605 continue;
606 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700607 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700608
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300609 wait_event_freezable_timeout(done_wait, done.done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200610 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200611
Dan Williamse44e0aa2009-03-25 09:13:25 -0700612 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700613
Tejun Heoadfa5432011-11-23 09:28:16 -0800614 if (!done.done) {
615 /*
616 * We're leaving the timed out dma operation with
617 * dangling pointer to done_wait. To make this
618 * correct, we'll need to allocate wait_done for
619 * each test iteration and perform "who's gonna
620 * free it this time?" dancing. For now, just
621 * leave it dangling.
622 */
Dan Williams872f05c2013-11-06 16:29:58 -0800623 result("test timed out", total_tests, src_off, dst_off,
624 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700625 failed_tests++;
626 continue;
627 } else if (status != DMA_SUCCESS) {
Dan Williams872f05c2013-11-06 16:29:58 -0800628 result(status == DMA_ERROR ?
629 "completion error status" :
630 "completion busy status", total_tests, src_off,
631 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700632 failed_tests++;
633 continue;
634 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700635
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200636 /* Unmap by myself */
637 unmap_src(dev->dev, dma_srcs, len, src_cnt);
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200638 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700639
Dan Williamse3b9c342013-11-06 16:30:05 -0800640 if (params->noverify) {
641 dbg_result("test passed", total_tests, src_off, dst_off,
642 len, 0);
643 continue;
644 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700645
Dan Williams872f05c2013-11-06 16:29:58 -0800646 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800647 error_count = dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700648 0, PATTERN_SRC, true);
Dan Williams7b610172013-11-06 16:29:57 -0800649 error_count += dmatest_verify(thread->srcs, src_off,
650 src_off + len, src_off,
651 PATTERN_SRC | PATTERN_COPY, true);
652 error_count += dmatest_verify(thread->srcs, src_off + len,
653 params->buf_size, src_off + len,
654 PATTERN_SRC, true);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700655
Dan Williams872f05c2013-11-06 16:29:58 -0800656 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800657 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700658 0, PATTERN_DST, false);
Dan Williams7b610172013-11-06 16:29:57 -0800659 error_count += dmatest_verify(thread->dsts, dst_off,
660 dst_off + len, src_off,
661 PATTERN_SRC | PATTERN_COPY, false);
662 error_count += dmatest_verify(thread->dsts, dst_off + len,
663 params->buf_size, dst_off + len,
664 PATTERN_DST, false);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700665
666 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800667 result("data error", total_tests, src_off, dst_off,
668 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700669 failed_tests++;
670 } else {
Dan Williams872f05c2013-11-06 16:29:58 -0800671 dbg_result("test passed", total_tests, src_off, dst_off,
672 len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700673 }
674 }
Dan Williams86727442013-11-06 16:30:07 -0800675 runtime = ktime_us_delta(ktime_get(), ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700676
677 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700678 for (i = 0; thread->dsts[i]; i++)
679 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700680err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700681 kfree(thread->dsts);
682err_dsts:
683 for (i = 0; thread->srcs[i]; i++)
684 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700685err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700686 kfree(thread->srcs);
687err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200688 kfree(pq_coefs);
689err_thread_type:
Dan Williams86727442013-11-06 16:30:07 -0800690 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
691 current->comm, total_tests, failed_tests,
692 dmatest_persec(runtime, total_tests),
693 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200694
Viresh Kumar9704efa2011-07-29 16:21:57 +0530695 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000696 if (ret)
697 dmaengine_terminate_all(chan);
698
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200699 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800700 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200701
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700702 return ret;
703}
704
705static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
706{
707 struct dmatest_thread *thread;
708 struct dmatest_thread *_thread;
709 int ret;
710
711 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
712 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800713 pr_debug("thread %s exited with status %d\n",
714 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700715 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800716 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700717 kfree(thread);
718 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530719
720 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000721 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530722
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700723 kfree(dtc);
724}
725
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200726static int dmatest_add_threads(struct dmatest_info *info,
727 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700728{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200729 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700730 struct dmatest_thread *thread;
731 struct dma_chan *chan = dtc->chan;
732 char *op;
733 unsigned int i;
734
735 if (type == DMA_MEMCPY)
736 op = "copy";
737 else if (type == DMA_XOR)
738 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700739 else if (type == DMA_PQ)
740 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700741 else
742 return -EINVAL;
743
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200744 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700745 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
746 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800747 pr_warn("No memory for %s-%s%u\n",
748 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700749 break;
750 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200751 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700752 thread->chan = dtc->chan;
753 thread->type = type;
754 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800755 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700756 dma_chan_name(chan), op, i);
757 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800758 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800759 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700760 kfree(thread);
761 break;
762 }
763
764 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800765 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700766 list_add_tail(&thread->node, &dtc->threads);
Dan Williams2d88ce72013-11-06 16:30:09 -0800767 wake_up_process(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700768 }
769
770 return i;
771}
772
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200773static int dmatest_add_channel(struct dmatest_info *info,
774 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700775{
776 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700777 struct dma_device *dma_dev = chan->device;
778 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400779 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700780
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700781 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700782 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800783 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700784 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700785 }
786
787 dtc->chan = chan;
788 INIT_LIST_HEAD(&dtc->threads);
789
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700790 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200791 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200792 thread_count += cnt > 0 ? cnt : 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700793 }
794 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200795 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200796 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700797 }
Dan Williams58691d62009-08-29 19:09:27 -0700798 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200799 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700800 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700801 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700802
Dan Williams0adff802013-11-06 16:30:00 -0800803 pr_info("Started %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700804 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700805
Andy Shevchenko838cc702013-03-04 11:09:28 +0200806 list_add_tail(&dtc->node, &info->channels);
807 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700808
Dan Williams33df8ca2009-01-06 11:38:15 -0700809 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700810}
811
Dan Williams7dd60252009-01-06 11:38:19 -0700812static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700813{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200814 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200815
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200816 if (!dmatest_match_channel(params, chan) ||
817 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700818 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700819 else
Dan Williams7dd60252009-01-06 11:38:19 -0700820 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700821}
822
Dan Williamsa9e55492013-11-06 16:30:02 -0800823static void request_channels(struct dmatest_info *info,
824 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700825{
Dan Williams33df8ca2009-01-06 11:38:15 -0700826 dma_cap_mask_t mask;
Dan Williamsa9e55492013-11-06 16:30:02 -0800827
828 dma_cap_zero(mask);
829 dma_cap_set(type, mask);
830 for (;;) {
831 struct dmatest_params *params = &info->params;
832 struct dma_chan *chan;
833
834 chan = dma_request_channel(mask, filter, params);
835 if (chan) {
836 if (dmatest_add_channel(info, chan)) {
837 dma_release_channel(chan);
838 break; /* add_channel failed, punt */
839 }
840 } else
841 break; /* no more channels available */
842 if (params->max_channels &&
843 info->nr_channels >= params->max_channels)
844 break; /* we have all we need */
845 }
846}
847
848static void run_threaded_test(struct dmatest_info *info)
849{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200850 struct dmatest_params *params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700851
Dan Williamsa310d032013-11-06 16:30:01 -0800852 /* Copy test parameters */
853 params->buf_size = test_buf_size;
854 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
855 strlcpy(params->device, strim(test_device), sizeof(params->device));
856 params->threads_per_chan = threads_per_chan;
857 params->max_channels = max_channels;
858 params->iterations = iterations;
859 params->xor_sources = xor_sources;
860 params->pq_sources = pq_sources;
861 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800862 params->noverify = noverify;
Dan Williamsa310d032013-11-06 16:30:01 -0800863
Dan Williamsa9e55492013-11-06 16:30:02 -0800864 request_channels(info, DMA_MEMCPY);
865 request_channels(info, DMA_XOR);
866 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700867}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700868
Dan Williamsa310d032013-11-06 16:30:01 -0800869static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700870{
Dan Williams33df8ca2009-01-06 11:38:15 -0700871 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700872 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700873
Andy Shevchenko838cc702013-03-04 11:09:28 +0200874 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700875 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700876 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700877 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -0800878 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Dan Williams7cbd4872009-03-04 16:06:03 -0700879 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700880 }
Andy Shevchenko838cc702013-03-04 11:09:28 +0200881
882 info->nr_channels = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700883}
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200884
Dan Williamsa9e55492013-11-06 16:30:02 -0800885static void restart_threaded_test(struct dmatest_info *info, bool run)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200886{
Dan Williamsa310d032013-11-06 16:30:01 -0800887 /* we might be called early to set run=, defer running until all
888 * parameters have been evaluated
889 */
890 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -0800891 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200892
Dan Williamsa310d032013-11-06 16:30:01 -0800893 /* Stop any running test first */
894 stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200895
896 /* Run test with new parameters */
Dan Williamsa9e55492013-11-06 16:30:02 -0800897 run_threaded_test(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300898}
899
Dan Williamsa310d032013-11-06 16:30:01 -0800900static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200901{
Dan Williamsa310d032013-11-06 16:30:01 -0800902 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200903
904 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -0800905 if (is_threaded_test_run(info)) {
906 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200907 } else {
Dan Williamsa310d032013-11-06 16:30:01 -0800908 stop_threaded_test(info);
909 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200910 }
Dan Williamsa310d032013-11-06 16:30:01 -0800911 mutex_unlock(&info->lock);
912
913 return param_get_bool(val, kp);
914}
915
916static int dmatest_run_set(const char *val, const struct kernel_param *kp)
917{
918 struct dmatest_info *info = &test_info;
919 int ret;
920
921 mutex_lock(&info->lock);
922 ret = param_set_bool(val, kp);
923 if (ret) {
924 mutex_unlock(&info->lock);
925 return ret;
926 }
927
928 if (is_threaded_test_run(info))
929 ret = -EBUSY;
930 else if (dmatest_run)
Dan Williamsa9e55492013-11-06 16:30:02 -0800931 restart_threaded_test(info, dmatest_run);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200932
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200933 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200934
Dan Williamsa310d032013-11-06 16:30:01 -0800935 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200936}
937
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200938static int __init dmatest_init(void)
939{
940 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -0800941 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200942
Dan Williamsa310d032013-11-06 16:30:01 -0800943 if (dmatest_run) {
944 mutex_lock(&info->lock);
Dan Williamsa9e55492013-11-06 16:30:02 -0800945 run_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -0800946 mutex_unlock(&info->lock);
947 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200948
Dan Williams2d88ce72013-11-06 16:30:09 -0800949 if (params->iterations && wait)
950 wait_event(thread_wait, !is_threaded_test_run(info));
951
Dan Williamsa310d032013-11-06 16:30:01 -0800952 /* module parameters are stable, inittime tests are started,
953 * let userspace take over 'run' control
954 */
955 info->did_init = true;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200956
Dan Williamsa9e55492013-11-06 16:30:02 -0800957 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200958}
959/* when compiled-in wait for drivers to load first */
960late_initcall(dmatest_init);
961
962static void __exit dmatest_exit(void)
963{
964 struct dmatest_info *info = &test_info;
965
Dan Williamsa310d032013-11-06 16:30:01 -0800966 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200967 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -0800968 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200969}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700970module_exit(dmatest_exit);
971
Jean Delvaree05503e2011-05-18 16:49:24 +0200972MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700973MODULE_LICENSE("GPL v2");