blob: 738fbd1dd57a07320ac31e2d861c3002f62e74a6 [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
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +010034static char test_device[32];
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
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053054static unsigned int sg_buffers = 1;
55module_param(sg_buffers, uint, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(sg_buffers,
57 "Number of scatter gather buffers (default: 1)");
58
59static unsigned int dmatest = 1;
60module_param(dmatest, uint, S_IRUGO | S_IWUSR);
61MODULE_PARM_DESC(dmatest,
62 "dmatest 0-memcpy 1-slave_sg (default: 1)");
63
Dan Williamsb54d5cb2009-03-25 09:13:25 -070064static unsigned int xor_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030065module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
Dan Williamsb54d5cb2009-03-25 09:13:25 -070066MODULE_PARM_DESC(xor_sources,
67 "Number of xor source buffers (default: 3)");
68
Dan Williams58691d62009-08-29 19:09:27 -070069static unsigned int pq_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030070module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
Dan Williams58691d62009-08-29 19:09:27 -070071MODULE_PARM_DESC(pq_sources,
72 "Number of p+q source buffers (default: 3)");
73
Viresh Kumard42efe62011-03-22 17:27:25 +053074static int timeout = 3000;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030075module_param(timeout, uint, S_IRUGO | S_IWUSR);
Joe Perches85ee7a12011-04-23 20:38:19 -070076MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
77 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053078
Dan Williamse3b9c342013-11-06 16:30:05 -080079static bool noverify;
80module_param(noverify, bool, S_IRUGO | S_IWUSR);
81MODULE_PARM_DESC(noverify, "Disable random data setup and verification");
Andy Shevchenko74b5c072013-03-04 11:09:32 +020082
Dan Williams50137a72013-11-08 12:26:26 -080083static bool verbose;
84module_param(verbose, bool, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(verbose, "Enable \"success\" result messages (default: off)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070086
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020087/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020088 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020089 * @buf_size: size of the memcpy test buffer
90 * @channel: bus ID of the channel to test
91 * @device: bus ID of the DMA Engine to test
92 * @threads_per_chan: number of threads to start per channel
93 * @max_channels: maximum number of channels to use
94 * @iterations: iterations before stopping test
95 * @xor_sources: number of xor source buffers
96 * @pq_sources: number of p+q source buffers
97 * @timeout: transfer timeout in msec, -1 for infinite timeout
98 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020099struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200100 unsigned int buf_size;
101 char channel[20];
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +0100102 char device[32];
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200103 unsigned int threads_per_chan;
104 unsigned int max_channels;
105 unsigned int iterations;
106 unsigned int xor_sources;
107 unsigned int pq_sources;
108 int timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800109 bool noverify;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200110};
111
112/**
113 * struct dmatest_info - test information.
114 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200115 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200116 */
Dan Williamsa310d032013-11-06 16:30:01 -0800117static struct dmatest_info {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200118 /* Test parameters */
119 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200120
121 /* Internal state */
122 struct list_head channels;
123 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200124 struct mutex lock;
Dan Williamsa310d032013-11-06 16:30:01 -0800125 bool did_init;
126} test_info = {
127 .channels = LIST_HEAD_INIT(test_info.channels),
128 .lock = __MUTEX_INITIALIZER(test_info.lock),
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200129};
130
Dan Williamsa310d032013-11-06 16:30:01 -0800131static int dmatest_run_set(const char *val, const struct kernel_param *kp);
132static int dmatest_run_get(char *val, const struct kernel_param *kp);
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930133static const struct kernel_param_ops run_ops = {
Dan Williamsa310d032013-11-06 16:30:01 -0800134 .set = dmatest_run_set,
135 .get = dmatest_run_get,
136};
137static bool dmatest_run;
138module_param_cb(run, &run_ops, &dmatest_run, S_IRUGO | S_IWUSR);
139MODULE_PARM_DESC(run, "Run the test (default: false)");
140
141/* Maximum amount of mismatched bytes in buffer to print */
142#define MAX_ERROR_COUNT 32
143
144/*
145 * Initialization patterns. All bytes in the source buffer has bit 7
146 * set, all bytes in the destination buffer has bit 7 cleared.
147 *
148 * Bit 6 is set for all bytes which are to be copied by the DMA
149 * engine. Bit 5 is set for all bytes which are to be overwritten by
150 * the DMA engine.
151 *
152 * The remaining bits are the inverse of a counter which increments by
153 * one for each byte address.
154 */
155#define PATTERN_SRC 0x80
156#define PATTERN_DST 0x00
157#define PATTERN_COPY 0x40
158#define PATTERN_OVERWRITE 0x20
159#define PATTERN_COUNT_MASK 0x1f
160
161struct dmatest_thread {
162 struct list_head node;
163 struct dmatest_info *info;
164 struct task_struct *task;
165 struct dma_chan *chan;
166 u8 **srcs;
167 u8 **dsts;
168 enum dma_transaction_type type;
169 bool done;
170};
171
172struct dmatest_chan {
173 struct list_head node;
174 struct dma_chan *chan;
175 struct list_head threads;
176};
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200177
Dan Williams2d88ce72013-11-06 16:30:09 -0800178static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
179static bool wait;
180
181static bool is_threaded_test_run(struct dmatest_info *info)
182{
183 struct dmatest_chan *dtc;
184
185 list_for_each_entry(dtc, &info->channels, node) {
186 struct dmatest_thread *thread;
187
188 list_for_each_entry(thread, &dtc->threads, node) {
189 if (!thread->done)
190 return true;
191 }
192 }
193
194 return false;
195}
196
197static int dmatest_wait_get(char *val, const struct kernel_param *kp)
198{
199 struct dmatest_info *info = &test_info;
200 struct dmatest_params *params = &info->params;
201
202 if (params->iterations)
203 wait_event(thread_wait, !is_threaded_test_run(info));
204 wait = true;
205 return param_get_bool(val, kp);
206}
207
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930208static const struct kernel_param_ops wait_ops = {
Dan Williams2d88ce72013-11-06 16:30:09 -0800209 .get = dmatest_wait_get,
210 .set = param_set_bool,
211};
212module_param_cb(wait, &wait_ops, &wait, S_IRUGO);
213MODULE_PARM_DESC(wait, "Wait for tests to complete (default: false)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700214
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200215static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200216 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700217{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200218 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700219 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200220 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700221}
222
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200223static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200224 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700225{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200226 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700227 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200228 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700229}
230
231static unsigned long dmatest_random(void)
232{
233 unsigned long buf;
234
Dan Williamsbe9fa5a2013-11-06 16:30:03 -0800235 prandom_bytes(&buf, sizeof(buf));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700236 return buf;
237}
238
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200239static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
240 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700241{
242 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700243 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700244
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700245 for (; (buf = *bufs); bufs++) {
246 for (i = 0; i < start; i++)
247 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
248 for ( ; i < start + len; i++)
249 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700250 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200251 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700252 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
253 buf++;
254 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700255}
256
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200257static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
258 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700259{
260 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700261 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700262
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700263 for (; (buf = *bufs); bufs++) {
264 for (i = 0; i < start; i++)
265 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
266 for ( ; i < start + len; i++)
267 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
268 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200269 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700270 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
271 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700272}
273
Dan Williams7b610172013-11-06 16:29:57 -0800274static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
275 unsigned int counter, bool is_srcbuf)
276{
277 u8 diff = actual ^ pattern;
278 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
279 const char *thread_name = current->comm;
280
281 if (is_srcbuf)
282 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
283 thread_name, index, expected, actual);
284 else if ((pattern & PATTERN_COPY)
285 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
286 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
287 thread_name, index, expected, actual);
288 else if (diff & PATTERN_SRC)
289 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
290 thread_name, index, expected, actual);
291 else
292 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
293 thread_name, index, expected, actual);
294}
295
296static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
297 unsigned int end, unsigned int counter, u8 pattern,
298 bool is_srcbuf)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700299{
300 unsigned int i;
301 unsigned int error_count = 0;
302 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700303 u8 expected;
304 u8 *buf;
305 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700306
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700307 for (; (buf = *bufs); bufs++) {
308 counter = counter_orig;
309 for (i = start; i < end; i++) {
310 actual = buf[i];
311 expected = pattern | (~counter & PATTERN_COUNT_MASK);
312 if (actual != expected) {
Dan Williams7b610172013-11-06 16:29:57 -0800313 if (error_count < MAX_ERROR_COUNT)
314 dmatest_mismatch(actual, pattern, i,
315 counter, is_srcbuf);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700316 error_count++;
317 }
318 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700319 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700320 }
321
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200322 if (error_count > MAX_ERROR_COUNT)
Dan Williams7b610172013-11-06 16:29:57 -0800323 pr_warn("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200324 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700325
326 return error_count;
327}
328
Tejun Heoadfa5432011-11-23 09:28:16 -0800329/* poor man's completion - we want to use wait_event_freezable() on it */
330struct dmatest_done {
331 bool done;
332 wait_queue_head_t *wait;
333};
334
335static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700336{
Tejun Heoadfa5432011-11-23 09:28:16 -0800337 struct dmatest_done *done = arg;
338
339 done->done = true;
340 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700341}
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 Shevchenkod86b2f22013-03-04 11:09:34 +0200352{
Jerome Blin2acec152014-03-04 10:38:55 +0100353 pr_info("%s: result #%u: '%s' with src_off=0x%x dst_off=0x%x len=0x%x (%lu)\n",
Dan Williams872f05c2013-11-06 16:29:58 -0800354 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +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{
Jerome Blin2acec152014-03-04 10:38:55 +0100361 pr_debug("%s: result #%u: '%s' with src_off=0x%x dst_off=0x%x len=0x%x (%lu)\n",
Andy Shevchenkoa835bb82014-10-22 16:16:42 +0300362 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200363}
364
Andy Shevchenkoa835bb82014-10-22 16:16:42 +0300365#define verbose_result(err, n, src_off, dst_off, len, data) ({ \
366 if (verbose) \
367 result(err, n, src_off, dst_off, len, data); \
368 else \
369 dbg_result(err, n, src_off, dst_off, len, data);\
Dan Williams50137a72013-11-08 12:26:26 -0800370})
371
Dan Williams86727442013-11-06 16:30:07 -0800372static unsigned long long dmatest_persec(s64 runtime, unsigned int val)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200373{
Dan Williams86727442013-11-06 16:30:07 -0800374 unsigned long long per_sec = 1000000;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200375
Dan Williams86727442013-11-06 16:30:07 -0800376 if (runtime <= 0)
377 return 0;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200378
Dan Williams86727442013-11-06 16:30:07 -0800379 /* drop precision until runtime is 32-bits */
380 while (runtime > UINT_MAX) {
381 runtime >>= 1;
382 per_sec <<= 1;
383 }
Andy Shevchenko95019c82013-03-04 11:09:33 +0200384
Dan Williams86727442013-11-06 16:30:07 -0800385 per_sec *= val;
386 do_div(per_sec, runtime);
387 return per_sec;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200388}
389
Dan Williams86727442013-11-06 16:30:07 -0800390static unsigned long long dmatest_KBs(s64 runtime, unsigned long long len)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200391{
Dan Williams86727442013-11-06 16:30:07 -0800392 return dmatest_persec(runtime, len >> 10);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200393}
394
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700395/*
396 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700397 * offsets for a given operation type until it is told to exit by
398 * kthread_stop(). There may be multiple threads running this function
399 * in parallel for a single channel, and there may be multiple channels
400 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700401 *
402 * Before each test, the source and destination buffer is initialized
403 * with a known pattern. This pattern is different depending on
404 * whether it's in an area which is supposed to be copied or
405 * overwritten, and different in the source and destination buffers.
406 * So if the DMA engine doesn't copy exactly what we tell it to copy,
407 * we'll notice.
408 */
409static int dmatest_func(void *data)
410{
Tejun Heoadfa5432011-11-23 09:28:16 -0800411 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700412 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800413 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200414 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200415 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700416 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900417 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700418 unsigned int error_count;
419 unsigned int failed_tests = 0;
420 unsigned int total_tests = 0;
421 dma_cookie_t cookie;
422 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700423 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200424 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700425 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700426 int src_cnt;
427 int dst_cnt;
428 int i;
Sinan Kayae9405ef2016-09-01 10:02:55 -0400429 ktime_t ktime, start, diff;
430 ktime_t filltime = ktime_set(0, 0);
431 ktime_t comparetime = ktime_set(0, 0);
Dan Williams86727442013-11-06 16:30:07 -0800432 s64 runtime = 0;
433 unsigned long long total_len = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700434
Tejun Heoadfa5432011-11-23 09:28:16 -0800435 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700436
437 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700438
439 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200440 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200441 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700442 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900443 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700444 if (thread->type == DMA_MEMCPY)
445 src_cnt = dst_cnt = 1;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530446 else if (thread->type == DMA_SG)
447 src_cnt = dst_cnt = sg_buffers;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700448 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900449 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200450 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700451 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700452 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900453 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200454 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700455 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200456
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200457 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200458 if (!pq_coefs)
459 goto err_thread_type;
460
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100461 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700462 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700463 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200464 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700465
466 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
467 if (!thread->srcs)
468 goto err_srcs;
469 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200470 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700471 if (!thread->srcs[i])
472 goto err_srcbuf;
473 }
474 thread->srcs[i] = NULL;
475
476 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
477 if (!thread->dsts)
478 goto err_dsts;
479 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200480 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700481 if (!thread->dsts[i])
482 goto err_dstbuf;
483 }
484 thread->dsts[i] = NULL;
485
Dan Williamse44e0aa2009-03-25 09:13:25 -0700486 set_user_nice(current, 10);
487
Ira Snyderb203bd32011-03-03 07:54:53 +0000488 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200489 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000490 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200491 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700492
Dan Williams86727442013-11-06 16:30:07 -0800493 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200494 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200495 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700496 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams4076e752013-11-06 16:30:10 -0800497 struct dmaengine_unmap_data *um;
498 dma_addr_t srcs[src_cnt];
499 dma_addr_t *dsts;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300500 unsigned int src_off, dst_off, len;
Dan Williams83544ae2009-09-08 17:42:53 -0700501 u8 align = 0;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530502 struct scatterlist tx_sg[src_cnt];
503 struct scatterlist rx_sg[src_cnt];
Atsushi Nemotod86be862009-01-13 09:22:20 -0700504
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700505 total_tests++;
506
Dan Williams83544ae2009-09-08 17:42:53 -0700507 /* honor alignment restrictions */
508 if (thread->type == DMA_MEMCPY)
509 align = dev->copy_align;
510 else if (thread->type == DMA_XOR)
511 align = dev->xor_align;
512 else if (thread->type == DMA_PQ)
513 align = dev->pq_align;
514
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200515 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100516 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200517 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100518 break;
519 }
520
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300521 if (params->noverify)
Dan Williamse3b9c342013-11-06 16:30:05 -0800522 len = params->buf_size;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300523 else
524 len = dmatest_random() % params->buf_size + 1;
525
526 len = (len >> align) << align;
527 if (!len)
528 len = 1 << align;
529
530 total_len += len;
531
532 if (params->noverify) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800533 src_off = 0;
534 dst_off = 0;
535 } else {
Sinan Kayae9405ef2016-09-01 10:02:55 -0400536 start = ktime_get();
Dan Williamse3b9c342013-11-06 16:30:05 -0800537 src_off = dmatest_random() % (params->buf_size - len + 1);
538 dst_off = dmatest_random() % (params->buf_size - len + 1);
539
540 src_off = (src_off >> align) << align;
541 dst_off = (dst_off >> align) << align;
542
543 dmatest_init_srcs(thread->srcs, src_off, len,
544 params->buf_size);
545 dmatest_init_dsts(thread->dsts, dst_off, len,
546 params->buf_size);
Sinan Kayae9405ef2016-09-01 10:02:55 -0400547
548 diff = ktime_sub(ktime_get(), start);
549 filltime = ktime_add(filltime, diff);
Dan Williamse3b9c342013-11-06 16:30:05 -0800550 }
551
Dan Williams4076e752013-11-06 16:30:10 -0800552 um = dmaengine_get_unmap_data(dev->dev, src_cnt+dst_cnt,
553 GFP_KERNEL);
554 if (!um) {
555 failed_tests++;
556 result("unmap data NULL", total_tests,
557 src_off, dst_off, len, ret);
558 continue;
559 }
Dan Williams83544ae2009-09-08 17:42:53 -0700560
Dan Williams4076e752013-11-06 16:30:10 -0800561 um->len = params->buf_size;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700562 for (i = 0; i < src_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800563 void *buf = thread->srcs[i];
Dan Williams4076e752013-11-06 16:30:10 -0800564 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800565 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700566
Dan Williams4076e752013-11-06 16:30:10 -0800567 um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
568 um->len, DMA_TO_DEVICE);
569 srcs[i] = um->addr[i] + src_off;
570 ret = dma_mapping_error(dev->dev, um->addr[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800571 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800572 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800573 result("src mapping error", total_tests,
574 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800575 failed_tests++;
576 continue;
577 }
Dan Williams4076e752013-11-06 16:30:10 -0800578 um->to_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700579 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700580 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williams4076e752013-11-06 16:30:10 -0800581 dsts = &um->addr[src_cnt];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700582 for (i = 0; i < dst_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800583 void *buf = thread->dsts[i];
Dan Williams4076e752013-11-06 16:30:10 -0800584 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800585 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williams4076e752013-11-06 16:30:10 -0800586
587 dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
588 DMA_BIDIRECTIONAL);
589 ret = dma_mapping_error(dev->dev, dsts[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800590 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800591 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800592 result("dst mapping error", total_tests,
593 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800594 failed_tests++;
595 continue;
596 }
Dan Williams4076e752013-11-06 16:30:10 -0800597 um->bidi_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700598 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700599
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530600 sg_init_table(tx_sg, src_cnt);
601 sg_init_table(rx_sg, src_cnt);
602 for (i = 0; i < src_cnt; i++) {
603 sg_dma_address(&rx_sg[i]) = srcs[i];
604 sg_dma_address(&tx_sg[i]) = dsts[i] + dst_off;
605 sg_dma_len(&tx_sg[i]) = len;
606 sg_dma_len(&rx_sg[i]) = len;
607 }
608
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700609 if (thread->type == DMA_MEMCPY)
610 tx = dev->device_prep_dma_memcpy(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800611 dsts[0] + dst_off,
612 srcs[0], len, flags);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530613 else if (thread->type == DMA_SG)
614 tx = dev->device_prep_dma_sg(chan, tx_sg, src_cnt,
615 rx_sg, src_cnt, flags);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700616 else if (thread->type == DMA_XOR)
617 tx = dev->device_prep_dma_xor(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800618 dsts[0] + dst_off,
619 srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700620 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700621 else if (thread->type == DMA_PQ) {
622 dma_addr_t dma_pq[dst_cnt];
623
624 for (i = 0; i < dst_cnt; i++)
Dan Williams4076e752013-11-06 16:30:10 -0800625 dma_pq[i] = dsts[i] + dst_off;
626 tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100627 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700628 len, flags);
629 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700630
Atsushi Nemotod86be862009-01-13 09:22:20 -0700631 if (!tx) {
Dan Williams4076e752013-11-06 16:30:10 -0800632 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800633 result("prep error", total_tests, src_off,
634 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700635 msleep(100);
636 failed_tests++;
637 continue;
638 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700639
Tejun Heoadfa5432011-11-23 09:28:16 -0800640 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700641 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800642 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700643 cookie = tx->tx_submit(tx);
644
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700645 if (dma_submit_error(cookie)) {
Dan Williams4076e752013-11-06 16:30:10 -0800646 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800647 result("submit error", total_tests, src_off,
648 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700649 msleep(100);
650 failed_tests++;
651 continue;
652 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700653 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700654
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300655 wait_event_freezable_timeout(done_wait, done.done,
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 */
Dan Williams4076e752013-11-06 16:30:10 -0800669 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800670 result("test timed out", total_tests, src_off, dst_off,
671 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700672 failed_tests++;
673 continue;
Vinod Koul19e9f992013-10-16 13:37:27 +0530674 } else if (status != DMA_COMPLETE) {
Dan Williams4076e752013-11-06 16:30:10 -0800675 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800676 result(status == DMA_ERROR ?
677 "completion error status" :
678 "completion busy status", total_tests, src_off,
679 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700680 failed_tests++;
681 continue;
682 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700683
Dan Williams4076e752013-11-06 16:30:10 -0800684 dmaengine_unmap_put(um);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700685
Dan Williamse3b9c342013-11-06 16:30:05 -0800686 if (params->noverify) {
Dan Williams50137a72013-11-08 12:26:26 -0800687 verbose_result("test passed", total_tests, src_off,
688 dst_off, len, 0);
Dan Williamse3b9c342013-11-06 16:30:05 -0800689 continue;
690 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700691
Sinan Kayae9405ef2016-09-01 10:02:55 -0400692 start = ktime_get();
Dan Williams872f05c2013-11-06 16:29:58 -0800693 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800694 error_count = dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700695 0, PATTERN_SRC, true);
Dan Williams7b610172013-11-06 16:29:57 -0800696 error_count += dmatest_verify(thread->srcs, src_off,
697 src_off + len, src_off,
698 PATTERN_SRC | PATTERN_COPY, true);
699 error_count += dmatest_verify(thread->srcs, src_off + len,
700 params->buf_size, src_off + len,
701 PATTERN_SRC, true);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700702
Dan Williams872f05c2013-11-06 16:29:58 -0800703 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800704 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700705 0, PATTERN_DST, false);
Dan Williams7b610172013-11-06 16:29:57 -0800706 error_count += dmatest_verify(thread->dsts, dst_off,
707 dst_off + len, src_off,
708 PATTERN_SRC | PATTERN_COPY, false);
709 error_count += dmatest_verify(thread->dsts, dst_off + len,
710 params->buf_size, dst_off + len,
711 PATTERN_DST, false);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700712
Sinan Kayae9405ef2016-09-01 10:02:55 -0400713 diff = ktime_sub(ktime_get(), start);
714 comparetime = ktime_add(comparetime, diff);
715
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700716 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800717 result("data error", total_tests, src_off, dst_off,
718 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700719 failed_tests++;
720 } else {
Dan Williams50137a72013-11-08 12:26:26 -0800721 verbose_result("test passed", total_tests, src_off,
722 dst_off, len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700723 }
724 }
Sinan Kayae9405ef2016-09-01 10:02:55 -0400725 ktime = ktime_sub(ktime_get(), ktime);
726 ktime = ktime_sub(ktime, comparetime);
727 ktime = ktime_sub(ktime, filltime);
728 runtime = ktime_to_us(ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700729
730 ret = 0;
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300731err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700732 for (i = 0; thread->dsts[i]; i++)
733 kfree(thread->dsts[i]);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700734 kfree(thread->dsts);
735err_dsts:
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300736err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700737 for (i = 0; thread->srcs[i]; i++)
738 kfree(thread->srcs[i]);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700739 kfree(thread->srcs);
740err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200741 kfree(pq_coefs);
742err_thread_type:
Dan Williams86727442013-11-06 16:30:07 -0800743 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
744 current->comm, total_tests, failed_tests,
745 dmatest_persec(runtime, total_tests),
746 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200747
Viresh Kumar9704efa2011-07-29 16:21:57 +0530748 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000749 if (ret)
750 dmaengine_terminate_all(chan);
751
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200752 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800753 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200754
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700755 return ret;
756}
757
758static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
759{
760 struct dmatest_thread *thread;
761 struct dmatest_thread *_thread;
762 int ret;
763
764 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
765 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800766 pr_debug("thread %s exited with status %d\n",
767 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700768 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800769 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700770 kfree(thread);
771 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530772
773 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000774 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530775
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700776 kfree(dtc);
777}
778
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200779static int dmatest_add_threads(struct dmatest_info *info,
780 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700781{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200782 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700783 struct dmatest_thread *thread;
784 struct dma_chan *chan = dtc->chan;
785 char *op;
786 unsigned int i;
787
788 if (type == DMA_MEMCPY)
789 op = "copy";
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530790 else if (type == DMA_SG)
791 op = "sg";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700792 else if (type == DMA_XOR)
793 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700794 else if (type == DMA_PQ)
795 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700796 else
797 return -EINVAL;
798
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200799 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700800 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
801 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800802 pr_warn("No memory for %s-%s%u\n",
803 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700804 break;
805 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200806 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700807 thread->chan = dtc->chan;
808 thread->type = type;
809 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800810 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700811 dma_chan_name(chan), op, i);
812 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800813 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800814 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700815 kfree(thread);
816 break;
817 }
818
819 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800820 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700821 list_add_tail(&thread->node, &dtc->threads);
Dan Williams2d88ce72013-11-06 16:30:09 -0800822 wake_up_process(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700823 }
824
825 return i;
826}
827
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200828static int dmatest_add_channel(struct dmatest_info *info,
829 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700830{
831 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700832 struct dma_device *dma_dev = chan->device;
833 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400834 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700835
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700836 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700837 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800838 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700839 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700840 }
841
842 dtc->chan = chan;
843 INIT_LIST_HEAD(&dtc->threads);
844
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700845 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530846 if (dmatest == 0) {
847 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
848 thread_count += cnt > 0 ? cnt : 0;
849 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700850 }
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530851
852 if (dma_has_cap(DMA_SG, dma_dev->cap_mask)) {
853 if (dmatest == 1) {
854 cnt = dmatest_add_threads(info, dtc, DMA_SG);
855 thread_count += cnt > 0 ? cnt : 0;
856 }
857 }
858
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700859 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200860 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200861 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700862 }
Dan Williams58691d62009-08-29 19:09:27 -0700863 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200864 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700865 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700866 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700867
Dan Williams0adff802013-11-06 16:30:00 -0800868 pr_info("Started %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700869 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700870
Andy Shevchenko838cc702013-03-04 11:09:28 +0200871 list_add_tail(&dtc->node, &info->channels);
872 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700873
Dan Williams33df8ca2009-01-06 11:38:15 -0700874 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700875}
876
Dan Williams7dd60252009-01-06 11:38:19 -0700877static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700878{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200879 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200880
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200881 if (!dmatest_match_channel(params, chan) ||
882 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700883 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700884 else
Dan Williams7dd60252009-01-06 11:38:19 -0700885 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700886}
887
Dan Williamsa9e55492013-11-06 16:30:02 -0800888static void request_channels(struct dmatest_info *info,
889 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700890{
Dan Williams33df8ca2009-01-06 11:38:15 -0700891 dma_cap_mask_t mask;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700892
Dan Williams33df8ca2009-01-06 11:38:15 -0700893 dma_cap_zero(mask);
Dan Williamsa9e55492013-11-06 16:30:02 -0800894 dma_cap_set(type, mask);
Dan Williams33df8ca2009-01-06 11:38:15 -0700895 for (;;) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800896 struct dmatest_params *params = &info->params;
897 struct dma_chan *chan;
898
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200899 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700900 if (chan) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800901 if (dmatest_add_channel(info, chan)) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700902 dma_release_channel(chan);
903 break; /* add_channel failed, punt */
904 }
905 } else
906 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200907 if (params->max_channels &&
908 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700909 break; /* we have all we need */
910 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700911}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700912
Dan Williamsa9e55492013-11-06 16:30:02 -0800913static void run_threaded_test(struct dmatest_info *info)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200914{
915 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200916
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200917 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +0300918 params->buf_size = test_buf_size;
919 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
920 strlcpy(params->device, strim(test_device), sizeof(params->device));
921 params->threads_per_chan = threads_per_chan;
922 params->max_channels = max_channels;
923 params->iterations = iterations;
924 params->xor_sources = xor_sources;
925 params->pq_sources = pq_sources;
926 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800927 params->noverify = noverify;
Dan Williamsa310d032013-11-06 16:30:01 -0800928
Dan Williamsa9e55492013-11-06 16:30:02 -0800929 request_channels(info, DMA_MEMCPY);
930 request_channels(info, DMA_XOR);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530931 request_channels(info, DMA_SG);
Dan Williamsa9e55492013-11-06 16:30:02 -0800932 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700933}
934
Dan Williamsa310d032013-11-06 16:30:01 -0800935static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700936{
937 struct dmatest_chan *dtc, *_dtc;
938 struct dma_chan *chan;
939
940 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
941 list_del(&dtc->node);
942 chan = dtc->chan;
943 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -0800944 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200945 dma_release_channel(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700946 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700947
Dan Williams7cbd4872009-03-04 16:06:03 -0700948 info->nr_channels = 0;
Dan Williams33df8ca2009-01-06 11:38:15 -0700949}
Andy Shevchenko838cc702013-03-04 11:09:28 +0200950
Dan Williamsa9e55492013-11-06 16:30:02 -0800951static void restart_threaded_test(struct dmatest_info *info, bool run)
Dan Williams7cbd4872009-03-04 16:06:03 -0700952{
Dan Williamsa310d032013-11-06 16:30:01 -0800953 /* we might be called early to set run=, defer running until all
954 * parameters have been evaluated
955 */
956 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -0800957 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200958
Dan Williamsa310d032013-11-06 16:30:01 -0800959 /* Stop any running test first */
960 stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200961
962 /* Run test with new parameters */
Dan Williamsa9e55492013-11-06 16:30:02 -0800963 run_threaded_test(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300964}
965
Dan Williamsa310d032013-11-06 16:30:01 -0800966static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300967{
Dan Williamsa310d032013-11-06 16:30:01 -0800968 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200969
970 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -0800971 if (is_threaded_test_run(info)) {
972 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200973 } else {
Dan Williamsa310d032013-11-06 16:30:01 -0800974 stop_threaded_test(info);
975 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200976 }
Dan Williamsa310d032013-11-06 16:30:01 -0800977 mutex_unlock(&info->lock);
978
979 return param_get_bool(val, kp);
980}
981
982static int dmatest_run_set(const char *val, const struct kernel_param *kp)
983{
984 struct dmatest_info *info = &test_info;
985 int ret;
986
987 mutex_lock(&info->lock);
988 ret = param_set_bool(val, kp);
989 if (ret) {
990 mutex_unlock(&info->lock);
991 return ret;
992 }
993
994 if (is_threaded_test_run(info))
995 ret = -EBUSY;
996 else if (dmatest_run)
Dan Williamsa9e55492013-11-06 16:30:02 -0800997 restart_threaded_test(info, dmatest_run);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200998
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200999 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001000
Dan Williamsa310d032013-11-06 16:30:01 -08001001 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001002}
1003
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001004static int __init dmatest_init(void)
1005{
1006 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -08001007 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001008
Dan Williamsa310d032013-11-06 16:30:01 -08001009 if (dmatest_run) {
1010 mutex_lock(&info->lock);
Dan Williamsa9e55492013-11-06 16:30:02 -08001011 run_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001012 mutex_unlock(&info->lock);
1013 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001014
Dan Williams2d88ce72013-11-06 16:30:09 -08001015 if (params->iterations && wait)
1016 wait_event(thread_wait, !is_threaded_test_run(info));
Andy Shevchenko838cc702013-03-04 11:09:28 +02001017
Dan Williamsa310d032013-11-06 16:30:01 -08001018 /* module parameters are stable, inittime tests are started,
1019 * let userspace take over 'run' control
1020 */
1021 info->did_init = true;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001022
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001023 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001024}
1025/* when compiled-in wait for drivers to load first */
1026late_initcall(dmatest_init);
1027
1028static void __exit dmatest_exit(void)
1029{
1030 struct dmatest_info *info = &test_info;
1031
Dan Williamsa310d032013-11-06 16:30:01 -08001032 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001033 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001034 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001035}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001036module_exit(dmatest_exit);
1037
Jean Delvaree05503e2011-05-18 16:49:24 +02001038MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001039MODULE_LICENSE("GPL v2");