blob: 1245db5438e1b010cefc7a79a426f363f487ee7c [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;
Dan Williams86727442013-11-06 16:30:07 -0800429 ktime_t ktime;
430 s64 runtime = 0;
431 unsigned long long total_len = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700432
Tejun Heoadfa5432011-11-23 09:28:16 -0800433 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700434
435 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700436
437 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200438 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200439 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700440 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900441 dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700442 if (thread->type == DMA_MEMCPY)
443 src_cnt = dst_cnt = 1;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530444 else if (thread->type == DMA_SG)
445 src_cnt = dst_cnt = sg_buffers;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700446 else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900447 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200448 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700449 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700450 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900451 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200452 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700453 dst_cnt = 2;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200454
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200455 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200456 if (!pq_coefs)
457 goto err_thread_type;
458
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100459 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700460 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700461 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200462 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700463
464 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
465 if (!thread->srcs)
466 goto err_srcs;
467 for (i = 0; i < src_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200468 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700469 if (!thread->srcs[i])
470 goto err_srcbuf;
471 }
472 thread->srcs[i] = NULL;
473
474 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
475 if (!thread->dsts)
476 goto err_dsts;
477 for (i = 0; i < dst_cnt; i++) {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200478 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700479 if (!thread->dsts[i])
480 goto err_dstbuf;
481 }
482 thread->dsts[i] = NULL;
483
Dan Williamse44e0aa2009-03-25 09:13:25 -0700484 set_user_nice(current, 10);
485
Ira Snyderb203bd32011-03-03 07:54:53 +0000486 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200487 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000488 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200489 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700490
Dan Williams86727442013-11-06 16:30:07 -0800491 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200492 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200493 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700494 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams4076e752013-11-06 16:30:10 -0800495 struct dmaengine_unmap_data *um;
496 dma_addr_t srcs[src_cnt];
497 dma_addr_t *dsts;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300498 unsigned int src_off, dst_off, len;
Dan Williams83544ae2009-09-08 17:42:53 -0700499 u8 align = 0;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530500 struct scatterlist tx_sg[src_cnt];
501 struct scatterlist rx_sg[src_cnt];
Atsushi Nemotod86be862009-01-13 09:22:20 -0700502
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700503 total_tests++;
504
Dan Williams83544ae2009-09-08 17:42:53 -0700505 /* honor alignment restrictions */
506 if (thread->type == DMA_MEMCPY)
507 align = dev->copy_align;
508 else if (thread->type == DMA_XOR)
509 align = dev->xor_align;
510 else if (thread->type == DMA_PQ)
511 align = dev->pq_align;
512
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200513 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100514 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200515 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100516 break;
517 }
518
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300519 if (params->noverify)
Dan Williamse3b9c342013-11-06 16:30:05 -0800520 len = params->buf_size;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300521 else
522 len = dmatest_random() % params->buf_size + 1;
523
524 len = (len >> align) << align;
525 if (!len)
526 len = 1 << align;
527
528 total_len += len;
529
530 if (params->noverify) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800531 src_off = 0;
532 dst_off = 0;
533 } else {
Dan Williamse3b9c342013-11-06 16:30:05 -0800534 src_off = dmatest_random() % (params->buf_size - len + 1);
535 dst_off = dmatest_random() % (params->buf_size - len + 1);
536
537 src_off = (src_off >> align) << align;
538 dst_off = (dst_off >> align) << align;
539
540 dmatest_init_srcs(thread->srcs, src_off, len,
541 params->buf_size);
542 dmatest_init_dsts(thread->dsts, dst_off, len,
543 params->buf_size);
544 }
545
Dan Williams4076e752013-11-06 16:30:10 -0800546 um = dmaengine_get_unmap_data(dev->dev, src_cnt+dst_cnt,
547 GFP_KERNEL);
548 if (!um) {
549 failed_tests++;
550 result("unmap data NULL", total_tests,
551 src_off, dst_off, len, ret);
552 continue;
553 }
Dan Williams83544ae2009-09-08 17:42:53 -0700554
Dan Williams4076e752013-11-06 16:30:10 -0800555 um->len = params->buf_size;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700556 for (i = 0; i < src_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800557 void *buf = thread->srcs[i];
Dan Williams4076e752013-11-06 16:30:10 -0800558 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800559 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700560
Dan Williams4076e752013-11-06 16:30:10 -0800561 um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
562 um->len, DMA_TO_DEVICE);
563 srcs[i] = um->addr[i] + src_off;
564 ret = dma_mapping_error(dev->dev, um->addr[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800565 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800566 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800567 result("src mapping error", total_tests,
568 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800569 failed_tests++;
570 continue;
571 }
Dan Williams4076e752013-11-06 16:30:10 -0800572 um->to_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700573 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700574 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williams4076e752013-11-06 16:30:10 -0800575 dsts = &um->addr[src_cnt];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700576 for (i = 0; i < dst_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800577 void *buf = thread->dsts[i];
Dan Williams4076e752013-11-06 16:30:10 -0800578 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800579 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williams4076e752013-11-06 16:30:10 -0800580
581 dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
582 DMA_BIDIRECTIONAL);
583 ret = dma_mapping_error(dev->dev, dsts[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800584 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800585 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800586 result("dst mapping error", total_tests,
587 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800588 failed_tests++;
589 continue;
590 }
Dan Williams4076e752013-11-06 16:30:10 -0800591 um->bidi_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700592 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700593
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530594 sg_init_table(tx_sg, src_cnt);
595 sg_init_table(rx_sg, src_cnt);
596 for (i = 0; i < src_cnt; i++) {
597 sg_dma_address(&rx_sg[i]) = srcs[i];
598 sg_dma_address(&tx_sg[i]) = dsts[i] + dst_off;
599 sg_dma_len(&tx_sg[i]) = len;
600 sg_dma_len(&rx_sg[i]) = len;
601 }
602
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700603 if (thread->type == DMA_MEMCPY)
604 tx = dev->device_prep_dma_memcpy(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800605 dsts[0] + dst_off,
606 srcs[0], len, flags);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530607 else if (thread->type == DMA_SG)
608 tx = dev->device_prep_dma_sg(chan, tx_sg, src_cnt,
609 rx_sg, src_cnt, flags);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700610 else if (thread->type == DMA_XOR)
611 tx = dev->device_prep_dma_xor(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800612 dsts[0] + dst_off,
613 srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700614 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700615 else if (thread->type == DMA_PQ) {
616 dma_addr_t dma_pq[dst_cnt];
617
618 for (i = 0; i < dst_cnt; i++)
Dan Williams4076e752013-11-06 16:30:10 -0800619 dma_pq[i] = dsts[i] + dst_off;
620 tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100621 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700622 len, flags);
623 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700624
Atsushi Nemotod86be862009-01-13 09:22:20 -0700625 if (!tx) {
Dan Williams4076e752013-11-06 16:30:10 -0800626 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800627 result("prep error", total_tests, src_off,
628 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700629 msleep(100);
630 failed_tests++;
631 continue;
632 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700633
Tejun Heoadfa5432011-11-23 09:28:16 -0800634 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700635 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800636 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700637 cookie = tx->tx_submit(tx);
638
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700639 if (dma_submit_error(cookie)) {
Dan Williams4076e752013-11-06 16:30:10 -0800640 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800641 result("submit error", total_tests, src_off,
642 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700643 msleep(100);
644 failed_tests++;
645 continue;
646 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700647 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700648
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300649 wait_event_freezable_timeout(done_wait, done.done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200650 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200651
Dan Williamse44e0aa2009-03-25 09:13:25 -0700652 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700653
Tejun Heoadfa5432011-11-23 09:28:16 -0800654 if (!done.done) {
655 /*
656 * We're leaving the timed out dma operation with
657 * dangling pointer to done_wait. To make this
658 * correct, we'll need to allocate wait_done for
659 * each test iteration and perform "who's gonna
660 * free it this time?" dancing. For now, just
661 * leave it dangling.
662 */
Dan Williams4076e752013-11-06 16:30:10 -0800663 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800664 result("test timed out", total_tests, src_off, dst_off,
665 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700666 failed_tests++;
667 continue;
Vinod Koul19e9f992013-10-16 13:37:27 +0530668 } else if (status != DMA_COMPLETE) {
Dan Williams4076e752013-11-06 16:30:10 -0800669 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800670 result(status == DMA_ERROR ?
671 "completion error status" :
672 "completion busy status", total_tests, src_off,
673 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700674 failed_tests++;
675 continue;
676 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700677
Dan Williams4076e752013-11-06 16:30:10 -0800678 dmaengine_unmap_put(um);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700679
Dan Williamse3b9c342013-11-06 16:30:05 -0800680 if (params->noverify) {
Dan Williams50137a72013-11-08 12:26:26 -0800681 verbose_result("test passed", total_tests, src_off,
682 dst_off, len, 0);
Dan Williamse3b9c342013-11-06 16:30:05 -0800683 continue;
684 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700685
Dan Williams872f05c2013-11-06 16:29:58 -0800686 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800687 error_count = dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700688 0, PATTERN_SRC, true);
Dan Williams7b610172013-11-06 16:29:57 -0800689 error_count += dmatest_verify(thread->srcs, src_off,
690 src_off + len, src_off,
691 PATTERN_SRC | PATTERN_COPY, true);
692 error_count += dmatest_verify(thread->srcs, src_off + len,
693 params->buf_size, src_off + len,
694 PATTERN_SRC, true);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700695
Dan Williams872f05c2013-11-06 16:29:58 -0800696 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800697 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700698 0, PATTERN_DST, false);
Dan Williams7b610172013-11-06 16:29:57 -0800699 error_count += dmatest_verify(thread->dsts, dst_off,
700 dst_off + len, src_off,
701 PATTERN_SRC | PATTERN_COPY, false);
702 error_count += dmatest_verify(thread->dsts, dst_off + len,
703 params->buf_size, dst_off + len,
704 PATTERN_DST, false);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700705
706 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800707 result("data error", total_tests, src_off, dst_off,
708 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700709 failed_tests++;
710 } else {
Dan Williams50137a72013-11-08 12:26:26 -0800711 verbose_result("test passed", total_tests, src_off,
712 dst_off, len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700713 }
714 }
Dan Williams86727442013-11-06 16:30:07 -0800715 runtime = ktime_us_delta(ktime_get(), ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700716
717 ret = 0;
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300718err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700719 for (i = 0; thread->dsts[i]; i++)
720 kfree(thread->dsts[i]);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700721 kfree(thread->dsts);
722err_dsts:
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300723err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700724 for (i = 0; thread->srcs[i]; i++)
725 kfree(thread->srcs[i]);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700726 kfree(thread->srcs);
727err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200728 kfree(pq_coefs);
729err_thread_type:
Dan Williams86727442013-11-06 16:30:07 -0800730 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
731 current->comm, total_tests, failed_tests,
732 dmatest_persec(runtime, total_tests),
733 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200734
Viresh Kumar9704efa2011-07-29 16:21:57 +0530735 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000736 if (ret)
737 dmaengine_terminate_all(chan);
738
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200739 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800740 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200741
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700742 return ret;
743}
744
745static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
746{
747 struct dmatest_thread *thread;
748 struct dmatest_thread *_thread;
749 int ret;
750
751 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
752 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800753 pr_debug("thread %s exited with status %d\n",
754 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700755 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800756 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700757 kfree(thread);
758 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530759
760 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000761 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530762
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700763 kfree(dtc);
764}
765
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200766static int dmatest_add_threads(struct dmatest_info *info,
767 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700768{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200769 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700770 struct dmatest_thread *thread;
771 struct dma_chan *chan = dtc->chan;
772 char *op;
773 unsigned int i;
774
775 if (type == DMA_MEMCPY)
776 op = "copy";
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530777 else if (type == DMA_SG)
778 op = "sg";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700779 else if (type == DMA_XOR)
780 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700781 else if (type == DMA_PQ)
782 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700783 else
784 return -EINVAL;
785
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200786 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700787 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
788 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800789 pr_warn("No memory for %s-%s%u\n",
790 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700791 break;
792 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200793 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700794 thread->chan = dtc->chan;
795 thread->type = type;
796 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800797 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700798 dma_chan_name(chan), op, i);
799 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800800 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800801 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700802 kfree(thread);
803 break;
804 }
805
806 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800807 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700808 list_add_tail(&thread->node, &dtc->threads);
Dan Williams2d88ce72013-11-06 16:30:09 -0800809 wake_up_process(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700810 }
811
812 return i;
813}
814
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200815static int dmatest_add_channel(struct dmatest_info *info,
816 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700817{
818 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700819 struct dma_device *dma_dev = chan->device;
820 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400821 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700822
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700823 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700824 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800825 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700826 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700827 }
828
829 dtc->chan = chan;
830 INIT_LIST_HEAD(&dtc->threads);
831
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700832 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530833 if (dmatest == 0) {
834 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
835 thread_count += cnt > 0 ? cnt : 0;
836 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700837 }
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530838
839 if (dma_has_cap(DMA_SG, dma_dev->cap_mask)) {
840 if (dmatest == 1) {
841 cnt = dmatest_add_threads(info, dtc, DMA_SG);
842 thread_count += cnt > 0 ? cnt : 0;
843 }
844 }
845
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700846 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200847 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200848 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700849 }
Dan Williams58691d62009-08-29 19:09:27 -0700850 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200851 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700852 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700853 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700854
Dan Williams0adff802013-11-06 16:30:00 -0800855 pr_info("Started %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700856 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700857
Andy Shevchenko838cc702013-03-04 11:09:28 +0200858 list_add_tail(&dtc->node, &info->channels);
859 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700860
Dan Williams33df8ca2009-01-06 11:38:15 -0700861 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700862}
863
Dan Williams7dd60252009-01-06 11:38:19 -0700864static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700865{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200866 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200867
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200868 if (!dmatest_match_channel(params, chan) ||
869 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700870 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700871 else
Dan Williams7dd60252009-01-06 11:38:19 -0700872 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700873}
874
Dan Williamsa9e55492013-11-06 16:30:02 -0800875static void request_channels(struct dmatest_info *info,
876 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700877{
Dan Williams33df8ca2009-01-06 11:38:15 -0700878 dma_cap_mask_t mask;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700879
Dan Williams33df8ca2009-01-06 11:38:15 -0700880 dma_cap_zero(mask);
Dan Williamsa9e55492013-11-06 16:30:02 -0800881 dma_cap_set(type, mask);
Dan Williams33df8ca2009-01-06 11:38:15 -0700882 for (;;) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800883 struct dmatest_params *params = &info->params;
884 struct dma_chan *chan;
885
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200886 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700887 if (chan) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800888 if (dmatest_add_channel(info, chan)) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700889 dma_release_channel(chan);
890 break; /* add_channel failed, punt */
891 }
892 } else
893 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200894 if (params->max_channels &&
895 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700896 break; /* we have all we need */
897 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700898}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700899
Dan Williamsa9e55492013-11-06 16:30:02 -0800900static void run_threaded_test(struct dmatest_info *info)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200901{
902 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200903
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200904 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +0300905 params->buf_size = test_buf_size;
906 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
907 strlcpy(params->device, strim(test_device), sizeof(params->device));
908 params->threads_per_chan = threads_per_chan;
909 params->max_channels = max_channels;
910 params->iterations = iterations;
911 params->xor_sources = xor_sources;
912 params->pq_sources = pq_sources;
913 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800914 params->noverify = noverify;
Dan Williamsa310d032013-11-06 16:30:01 -0800915
Dan Williamsa9e55492013-11-06 16:30:02 -0800916 request_channels(info, DMA_MEMCPY);
917 request_channels(info, DMA_XOR);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530918 request_channels(info, DMA_SG);
Dan Williamsa9e55492013-11-06 16:30:02 -0800919 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700920}
921
Dan Williamsa310d032013-11-06 16:30:01 -0800922static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700923{
924 struct dmatest_chan *dtc, *_dtc;
925 struct dma_chan *chan;
926
927 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
928 list_del(&dtc->node);
929 chan = dtc->chan;
930 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -0800931 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200932 dma_release_channel(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700933 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700934
Dan Williams7cbd4872009-03-04 16:06:03 -0700935 info->nr_channels = 0;
Dan Williams33df8ca2009-01-06 11:38:15 -0700936}
Andy Shevchenko838cc702013-03-04 11:09:28 +0200937
Dan Williamsa9e55492013-11-06 16:30:02 -0800938static void restart_threaded_test(struct dmatest_info *info, bool run)
Dan Williams7cbd4872009-03-04 16:06:03 -0700939{
Dan Williamsa310d032013-11-06 16:30:01 -0800940 /* we might be called early to set run=, defer running until all
941 * parameters have been evaluated
942 */
943 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -0800944 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200945
Dan Williamsa310d032013-11-06 16:30:01 -0800946 /* Stop any running test first */
947 stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200948
949 /* Run test with new parameters */
Dan Williamsa9e55492013-11-06 16:30:02 -0800950 run_threaded_test(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300951}
952
Dan Williamsa310d032013-11-06 16:30:01 -0800953static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300954{
Dan Williamsa310d032013-11-06 16:30:01 -0800955 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200956
957 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -0800958 if (is_threaded_test_run(info)) {
959 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200960 } else {
Dan Williamsa310d032013-11-06 16:30:01 -0800961 stop_threaded_test(info);
962 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200963 }
Dan Williamsa310d032013-11-06 16:30:01 -0800964 mutex_unlock(&info->lock);
965
966 return param_get_bool(val, kp);
967}
968
969static int dmatest_run_set(const char *val, const struct kernel_param *kp)
970{
971 struct dmatest_info *info = &test_info;
972 int ret;
973
974 mutex_lock(&info->lock);
975 ret = param_set_bool(val, kp);
976 if (ret) {
977 mutex_unlock(&info->lock);
978 return ret;
979 }
980
981 if (is_threaded_test_run(info))
982 ret = -EBUSY;
983 else if (dmatest_run)
Dan Williamsa9e55492013-11-06 16:30:02 -0800984 restart_threaded_test(info, dmatest_run);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200985
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200986 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200987
Dan Williamsa310d032013-11-06 16:30:01 -0800988 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200989}
990
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200991static int __init dmatest_init(void)
992{
993 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -0800994 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200995
Dan Williamsa310d032013-11-06 16:30:01 -0800996 if (dmatest_run) {
997 mutex_lock(&info->lock);
Dan Williamsa9e55492013-11-06 16:30:02 -0800998 run_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -0800999 mutex_unlock(&info->lock);
1000 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001001
Dan Williams2d88ce72013-11-06 16:30:09 -08001002 if (params->iterations && wait)
1003 wait_event(thread_wait, !is_threaded_test_run(info));
Andy Shevchenko838cc702013-03-04 11:09:28 +02001004
Dan Williamsa310d032013-11-06 16:30:01 -08001005 /* module parameters are stable, inittime tests are started,
1006 * let userspace take over 'run' control
1007 */
1008 info->did_init = true;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001009
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001010 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001011}
1012/* when compiled-in wait for drivers to load first */
1013late_initcall(dmatest_init);
1014
1015static void __exit dmatest_exit(void)
1016{
1017 struct dmatest_info *info = &test_info;
1018
Dan Williamsa310d032013-11-06 16:30:01 -08001019 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001020 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001021 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001022}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001023module_exit(dmatest_exit);
1024
Jean Delvaree05503e2011-05-18 16:49:24 +02001025MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001026MODULE_LICENSE("GPL v2");