blob: c9297605058c1aa68aa0fa6d90f9a0fc1a6cdc0d [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
Eugeniy Paltsevd8646722016-09-14 20:40:38 +030059static unsigned int dmatest;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053060module_param(dmatest, uint, S_IRUGO | S_IWUSR);
61MODULE_PARM_DESC(dmatest,
Eugeniy Paltsevd8646722016-09-14 20:40:38 +030062 "dmatest 0-memcpy 1-slave_sg (default: 0)");
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053063
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;
Dave Jiangd6481602016-11-29 13:22:20 -0700167 u8 **usrcs;
Dan Williamsa310d032013-11-06 16:30:01 -0800168 u8 **dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700169 u8 **udsts;
Dan Williamsa310d032013-11-06 16:30:01 -0800170 enum dma_transaction_type type;
171 bool done;
172};
173
174struct dmatest_chan {
175 struct list_head node;
176 struct dma_chan *chan;
177 struct list_head threads;
178};
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200179
Dan Williams2d88ce72013-11-06 16:30:09 -0800180static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
181static bool wait;
182
183static bool is_threaded_test_run(struct dmatest_info *info)
184{
185 struct dmatest_chan *dtc;
186
187 list_for_each_entry(dtc, &info->channels, node) {
188 struct dmatest_thread *thread;
189
190 list_for_each_entry(thread, &dtc->threads, node) {
191 if (!thread->done)
192 return true;
193 }
194 }
195
196 return false;
197}
198
199static int dmatest_wait_get(char *val, const struct kernel_param *kp)
200{
201 struct dmatest_info *info = &test_info;
202 struct dmatest_params *params = &info->params;
203
204 if (params->iterations)
205 wait_event(thread_wait, !is_threaded_test_run(info));
206 wait = true;
207 return param_get_bool(val, kp);
208}
209
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930210static const struct kernel_param_ops wait_ops = {
Dan Williams2d88ce72013-11-06 16:30:09 -0800211 .get = dmatest_wait_get,
212 .set = param_set_bool,
213};
214module_param_cb(wait, &wait_ops, &wait, S_IRUGO);
215MODULE_PARM_DESC(wait, "Wait for tests to complete (default: false)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700216
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200217static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200218 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700219{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200220 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700221 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200222 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700223}
224
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200225static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200226 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700227{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200228 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700229 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200230 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700231}
232
233static unsigned long dmatest_random(void)
234{
235 unsigned long buf;
236
Dan Williamsbe9fa5a2013-11-06 16:30:03 -0800237 prandom_bytes(&buf, sizeof(buf));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700238 return buf;
239}
240
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200241static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
242 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700243{
244 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700245 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700246
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700247 for (; (buf = *bufs); bufs++) {
248 for (i = 0; i < start; i++)
249 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
250 for ( ; i < start + len; i++)
251 buf[i] = PATTERN_SRC | PATTERN_COPY
Joe Perchesc0198942009-06-28 09:26:21 -0700252 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200253 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700254 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
255 buf++;
256 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700257}
258
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200259static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
260 unsigned int buf_size)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700261{
262 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700263 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700264
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700265 for (; (buf = *bufs); bufs++) {
266 for (i = 0; i < start; i++)
267 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
268 for ( ; i < start + len; i++)
269 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
270 | (~i & PATTERN_COUNT_MASK);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200271 for ( ; i < buf_size; i++)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700272 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
273 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700274}
275
Dan Williams7b610172013-11-06 16:29:57 -0800276static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
277 unsigned int counter, bool is_srcbuf)
278{
279 u8 diff = actual ^ pattern;
280 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
281 const char *thread_name = current->comm;
282
283 if (is_srcbuf)
284 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
285 thread_name, index, expected, actual);
286 else if ((pattern & PATTERN_COPY)
287 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
288 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
289 thread_name, index, expected, actual);
290 else if (diff & PATTERN_SRC)
291 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
292 thread_name, index, expected, actual);
293 else
294 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
295 thread_name, index, expected, actual);
296}
297
298static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
299 unsigned int end, unsigned int counter, u8 pattern,
300 bool is_srcbuf)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700301{
302 unsigned int i;
303 unsigned int error_count = 0;
304 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700305 u8 expected;
306 u8 *buf;
307 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700308
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700309 for (; (buf = *bufs); bufs++) {
310 counter = counter_orig;
311 for (i = start; i < end; i++) {
312 actual = buf[i];
313 expected = pattern | (~counter & PATTERN_COUNT_MASK);
314 if (actual != expected) {
Dan Williams7b610172013-11-06 16:29:57 -0800315 if (error_count < MAX_ERROR_COUNT)
316 dmatest_mismatch(actual, pattern, i,
317 counter, is_srcbuf);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700318 error_count++;
319 }
320 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700321 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700322 }
323
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200324 if (error_count > MAX_ERROR_COUNT)
Dan Williams7b610172013-11-06 16:29:57 -0800325 pr_warn("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200326 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700327
328 return error_count;
329}
330
Tejun Heoadfa5432011-11-23 09:28:16 -0800331/* poor man's completion - we want to use wait_event_freezable() on it */
332struct dmatest_done {
333 bool done;
334 wait_queue_head_t *wait;
335};
336
337static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700338{
Tejun Heoadfa5432011-11-23 09:28:16 -0800339 struct dmatest_done *done = arg;
340
341 done->done = true;
342 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700343}
344
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900345static unsigned int min_odd(unsigned int x, unsigned int y)
346{
347 unsigned int val = min(x, y);
348
349 return val % 2 ? val : val - 1;
350}
351
Dan Williams872f05c2013-11-06 16:29:58 -0800352static void result(const char *err, unsigned int n, unsigned int src_off,
353 unsigned int dst_off, unsigned int len, unsigned long data)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200354{
Jerome Blin2acec152014-03-04 10:38:55 +0100355 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 -0800356 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200357}
358
Dan Williams872f05c2013-11-06 16:29:58 -0800359static void dbg_result(const char *err, unsigned int n, unsigned int src_off,
360 unsigned int dst_off, unsigned int len,
361 unsigned long data)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200362{
Jerome Blin2acec152014-03-04 10:38:55 +0100363 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 +0300364 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200365}
366
Andy Shevchenkoa835bb82014-10-22 16:16:42 +0300367#define verbose_result(err, n, src_off, dst_off, len, data) ({ \
368 if (verbose) \
369 result(err, n, src_off, dst_off, len, data); \
370 else \
371 dbg_result(err, n, src_off, dst_off, len, data);\
Dan Williams50137a72013-11-08 12:26:26 -0800372})
373
Dan Williams86727442013-11-06 16:30:07 -0800374static unsigned long long dmatest_persec(s64 runtime, unsigned int val)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200375{
Dan Williams86727442013-11-06 16:30:07 -0800376 unsigned long long per_sec = 1000000;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200377
Dan Williams86727442013-11-06 16:30:07 -0800378 if (runtime <= 0)
379 return 0;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200380
Dan Williams86727442013-11-06 16:30:07 -0800381 /* drop precision until runtime is 32-bits */
382 while (runtime > UINT_MAX) {
383 runtime >>= 1;
384 per_sec <<= 1;
385 }
Andy Shevchenko95019c82013-03-04 11:09:33 +0200386
Dan Williams86727442013-11-06 16:30:07 -0800387 per_sec *= val;
388 do_div(per_sec, runtime);
389 return per_sec;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200390}
391
Dan Williams86727442013-11-06 16:30:07 -0800392static unsigned long long dmatest_KBs(s64 runtime, unsigned long long len)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200393{
Dan Williams86727442013-11-06 16:30:07 -0800394 return dmatest_persec(runtime, len >> 10);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200395}
396
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700397/*
398 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700399 * offsets for a given operation type until it is told to exit by
400 * kthread_stop(). There may be multiple threads running this function
401 * in parallel for a single channel, and there may be multiple channels
402 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700403 *
404 * Before each test, the source and destination buffer is initialized
405 * with a known pattern. This pattern is different depending on
406 * whether it's in an area which is supposed to be copied or
407 * overwritten, and different in the source and destination buffers.
408 * So if the DMA engine doesn't copy exactly what we tell it to copy,
409 * we'll notice.
410 */
411static int dmatest_func(void *data)
412{
Tejun Heoadfa5432011-11-23 09:28:16 -0800413 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700414 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800415 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200416 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200417 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700418 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900419 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700420 unsigned int error_count;
421 unsigned int failed_tests = 0;
422 unsigned int total_tests = 0;
423 dma_cookie_t cookie;
424 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700425 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200426 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700427 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700428 int src_cnt;
429 int dst_cnt;
430 int i;
Sinan Kayae9405ef2016-09-01 10:02:55 -0400431 ktime_t ktime, start, diff;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100432 ktime_t filltime = 0;
433 ktime_t comparetime = 0;
Dan Williams86727442013-11-06 16:30:07 -0800434 s64 runtime = 0;
435 unsigned long long total_len = 0;
Dave Jiangd6481602016-11-29 13:22:20 -0700436 u8 align = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700437
Tejun Heoadfa5432011-11-23 09:28:16 -0800438 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700439
440 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700441
442 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200443 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200444 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700445 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900446 dev = chan->device;
Dave Jiangd6481602016-11-29 13:22:20 -0700447 if (thread->type == DMA_MEMCPY) {
448 align = dev->copy_align;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700449 src_cnt = dst_cnt = 1;
Dave Jiangd6481602016-11-29 13:22:20 -0700450 } else if (thread->type == DMA_SG) {
451 align = dev->copy_align;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530452 src_cnt = dst_cnt = sg_buffers;
Dave Jiangd6481602016-11-29 13:22:20 -0700453 } else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900454 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200455 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700456 dst_cnt = 1;
Dave Jiangd6481602016-11-29 13:22:20 -0700457 align = dev->xor_align;
Dan Williams58691d62009-08-29 19:09:27 -0700458 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900459 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200460 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700461 dst_cnt = 2;
Dave Jiangd6481602016-11-29 13:22:20 -0700462 align = dev->pq_align;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200463
Dave Jiang31d18252016-11-29 13:22:01 -0700464 pq_coefs = kmalloc(params->pq_sources + 1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200465 if (!pq_coefs)
466 goto err_thread_type;
467
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100468 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700469 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700470 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200471 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700472
Dave Jiang31d18252016-11-29 13:22:01 -0700473 thread->srcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700474 if (!thread->srcs)
475 goto err_srcs;
Dave Jiangd6481602016-11-29 13:22:20 -0700476
477 thread->usrcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
478 if (!thread->usrcs)
479 goto err_usrcs;
480
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700481 for (i = 0; i < src_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700482 thread->usrcs[i] = kmalloc(params->buf_size + align,
483 GFP_KERNEL);
484 if (!thread->usrcs[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700485 goto err_srcbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700486
487 /* align srcs to alignment restriction */
488 if (align)
489 thread->srcs[i] = PTR_ALIGN(thread->usrcs[i], align);
490 else
491 thread->srcs[i] = thread->usrcs[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700492 }
493 thread->srcs[i] = NULL;
494
Dave Jiang31d18252016-11-29 13:22:01 -0700495 thread->dsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700496 if (!thread->dsts)
497 goto err_dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700498
499 thread->udsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
500 if (!thread->udsts)
501 goto err_udsts;
502
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700503 for (i = 0; i < dst_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700504 thread->udsts[i] = kmalloc(params->buf_size + align,
505 GFP_KERNEL);
506 if (!thread->udsts[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700507 goto err_dstbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700508
509 /* align dsts to alignment restriction */
510 if (align)
511 thread->dsts[i] = PTR_ALIGN(thread->udsts[i], align);
512 else
513 thread->dsts[i] = thread->udsts[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700514 }
515 thread->dsts[i] = NULL;
516
Dan Williamse44e0aa2009-03-25 09:13:25 -0700517 set_user_nice(current, 10);
518
Ira Snyderb203bd32011-03-03 07:54:53 +0000519 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200520 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000521 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200522 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700523
Dan Williams86727442013-11-06 16:30:07 -0800524 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200525 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200526 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700527 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams4076e752013-11-06 16:30:10 -0800528 struct dmaengine_unmap_data *um;
529 dma_addr_t srcs[src_cnt];
530 dma_addr_t *dsts;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300531 unsigned int src_off, dst_off, len;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530532 struct scatterlist tx_sg[src_cnt];
533 struct scatterlist rx_sg[src_cnt];
Atsushi Nemotod86be862009-01-13 09:22:20 -0700534
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700535 total_tests++;
536
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200537 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100538 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200539 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100540 break;
541 }
542
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300543 if (params->noverify)
Dan Williamse3b9c342013-11-06 16:30:05 -0800544 len = params->buf_size;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300545 else
546 len = dmatest_random() % params->buf_size + 1;
547
548 len = (len >> align) << align;
549 if (!len)
550 len = 1 << align;
551
552 total_len += len;
553
554 if (params->noverify) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800555 src_off = 0;
556 dst_off = 0;
557 } else {
Sinan Kayae9405ef2016-09-01 10:02:55 -0400558 start = ktime_get();
Dan Williamse3b9c342013-11-06 16:30:05 -0800559 src_off = dmatest_random() % (params->buf_size - len + 1);
560 dst_off = dmatest_random() % (params->buf_size - len + 1);
561
562 src_off = (src_off >> align) << align;
563 dst_off = (dst_off >> align) << align;
564
565 dmatest_init_srcs(thread->srcs, src_off, len,
566 params->buf_size);
567 dmatest_init_dsts(thread->dsts, dst_off, len,
568 params->buf_size);
Sinan Kayae9405ef2016-09-01 10:02:55 -0400569
570 diff = ktime_sub(ktime_get(), start);
571 filltime = ktime_add(filltime, diff);
Dan Williamse3b9c342013-11-06 16:30:05 -0800572 }
573
Dave Jiang31d18252016-11-29 13:22:01 -0700574 um = dmaengine_get_unmap_data(dev->dev, src_cnt + dst_cnt,
Dan Williams4076e752013-11-06 16:30:10 -0800575 GFP_KERNEL);
576 if (!um) {
577 failed_tests++;
578 result("unmap data NULL", total_tests,
579 src_off, dst_off, len, ret);
580 continue;
581 }
Dan Williams83544ae2009-09-08 17:42:53 -0700582
Dan Williams4076e752013-11-06 16:30:10 -0800583 um->len = params->buf_size;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700584 for (i = 0; i < src_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800585 void *buf = thread->srcs[i];
Dan Williams4076e752013-11-06 16:30:10 -0800586 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800587 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700588
Dan Williams4076e752013-11-06 16:30:10 -0800589 um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
590 um->len, DMA_TO_DEVICE);
591 srcs[i] = um->addr[i] + src_off;
592 ret = dma_mapping_error(dev->dev, um->addr[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800593 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800594 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800595 result("src mapping error", total_tests,
596 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800597 failed_tests++;
598 continue;
599 }
Dan Williams4076e752013-11-06 16:30:10 -0800600 um->to_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700601 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700602 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williams4076e752013-11-06 16:30:10 -0800603 dsts = &um->addr[src_cnt];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700604 for (i = 0; i < dst_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800605 void *buf = thread->dsts[i];
Dan Williams4076e752013-11-06 16:30:10 -0800606 struct page *pg = virt_to_page(buf);
Dan Williams745c00d2013-12-09 11:16:01 -0800607 unsigned pg_off = (unsigned long) buf & ~PAGE_MASK;
Dan Williams4076e752013-11-06 16:30:10 -0800608
609 dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
610 DMA_BIDIRECTIONAL);
611 ret = dma_mapping_error(dev->dev, dsts[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800612 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800613 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800614 result("dst mapping error", total_tests,
615 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800616 failed_tests++;
617 continue;
618 }
Dan Williams4076e752013-11-06 16:30:10 -0800619 um->bidi_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700620 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700621
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530622 sg_init_table(tx_sg, src_cnt);
623 sg_init_table(rx_sg, src_cnt);
624 for (i = 0; i < src_cnt; i++) {
625 sg_dma_address(&rx_sg[i]) = srcs[i];
626 sg_dma_address(&tx_sg[i]) = dsts[i] + dst_off;
627 sg_dma_len(&tx_sg[i]) = len;
628 sg_dma_len(&rx_sg[i]) = len;
629 }
630
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700631 if (thread->type == DMA_MEMCPY)
632 tx = dev->device_prep_dma_memcpy(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800633 dsts[0] + dst_off,
634 srcs[0], len, flags);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530635 else if (thread->type == DMA_SG)
636 tx = dev->device_prep_dma_sg(chan, tx_sg, src_cnt,
637 rx_sg, src_cnt, flags);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700638 else if (thread->type == DMA_XOR)
639 tx = dev->device_prep_dma_xor(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800640 dsts[0] + dst_off,
641 srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700642 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700643 else if (thread->type == DMA_PQ) {
644 dma_addr_t dma_pq[dst_cnt];
645
646 for (i = 0; i < dst_cnt; i++)
Dan Williams4076e752013-11-06 16:30:10 -0800647 dma_pq[i] = dsts[i] + dst_off;
648 tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100649 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700650 len, flags);
651 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700652
Atsushi Nemotod86be862009-01-13 09:22:20 -0700653 if (!tx) {
Dan Williams4076e752013-11-06 16:30:10 -0800654 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800655 result("prep error", total_tests, src_off,
656 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700657 msleep(100);
658 failed_tests++;
659 continue;
660 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700661
Tejun Heoadfa5432011-11-23 09:28:16 -0800662 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700663 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800664 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700665 cookie = tx->tx_submit(tx);
666
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700667 if (dma_submit_error(cookie)) {
Dan Williams4076e752013-11-06 16:30:10 -0800668 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800669 result("submit error", total_tests, src_off,
670 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700671 msleep(100);
672 failed_tests++;
673 continue;
674 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700675 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700676
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300677 wait_event_freezable_timeout(done_wait, done.done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200678 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200679
Dan Williamse44e0aa2009-03-25 09:13:25 -0700680 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700681
Tejun Heoadfa5432011-11-23 09:28:16 -0800682 if (!done.done) {
683 /*
684 * We're leaving the timed out dma operation with
685 * dangling pointer to done_wait. To make this
686 * correct, we'll need to allocate wait_done for
687 * each test iteration and perform "who's gonna
688 * free it this time?" dancing. For now, just
689 * leave it dangling.
690 */
Dan Williams4076e752013-11-06 16:30:10 -0800691 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800692 result("test timed out", total_tests, src_off, dst_off,
693 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700694 failed_tests++;
695 continue;
Vinod Koul19e9f992013-10-16 13:37:27 +0530696 } else if (status != DMA_COMPLETE) {
Dan Williams4076e752013-11-06 16:30:10 -0800697 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800698 result(status == DMA_ERROR ?
699 "completion error status" :
700 "completion busy status", total_tests, src_off,
701 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700702 failed_tests++;
703 continue;
704 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700705
Dan Williams4076e752013-11-06 16:30:10 -0800706 dmaengine_unmap_put(um);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700707
Dan Williamse3b9c342013-11-06 16:30:05 -0800708 if (params->noverify) {
Dan Williams50137a72013-11-08 12:26:26 -0800709 verbose_result("test passed", total_tests, src_off,
710 dst_off, len, 0);
Dan Williamse3b9c342013-11-06 16:30:05 -0800711 continue;
712 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700713
Sinan Kayae9405ef2016-09-01 10:02:55 -0400714 start = ktime_get();
Dan Williams872f05c2013-11-06 16:29:58 -0800715 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800716 error_count = dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700717 0, PATTERN_SRC, true);
Dan Williams7b610172013-11-06 16:29:57 -0800718 error_count += dmatest_verify(thread->srcs, src_off,
719 src_off + len, src_off,
720 PATTERN_SRC | PATTERN_COPY, true);
721 error_count += dmatest_verify(thread->srcs, src_off + len,
722 params->buf_size, src_off + len,
723 PATTERN_SRC, true);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700724
Dan Williams872f05c2013-11-06 16:29:58 -0800725 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800726 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700727 0, PATTERN_DST, false);
Dan Williams7b610172013-11-06 16:29:57 -0800728 error_count += dmatest_verify(thread->dsts, dst_off,
729 dst_off + len, src_off,
730 PATTERN_SRC | PATTERN_COPY, false);
731 error_count += dmatest_verify(thread->dsts, dst_off + len,
732 params->buf_size, dst_off + len,
733 PATTERN_DST, false);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700734
Sinan Kayae9405ef2016-09-01 10:02:55 -0400735 diff = ktime_sub(ktime_get(), start);
736 comparetime = ktime_add(comparetime, diff);
737
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700738 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800739 result("data error", total_tests, src_off, dst_off,
740 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700741 failed_tests++;
742 } else {
Dan Williams50137a72013-11-08 12:26:26 -0800743 verbose_result("test passed", total_tests, src_off,
744 dst_off, len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700745 }
746 }
Sinan Kayae9405ef2016-09-01 10:02:55 -0400747 ktime = ktime_sub(ktime_get(), ktime);
748 ktime = ktime_sub(ktime, comparetime);
749 ktime = ktime_sub(ktime, filltime);
750 runtime = ktime_to_us(ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700751
752 ret = 0;
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300753err_dstbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700754 for (i = 0; thread->udsts[i]; i++)
755 kfree(thread->udsts[i]);
756 kfree(thread->udsts);
757err_udsts:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700758 kfree(thread->dsts);
759err_dsts:
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300760err_srcbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700761 for (i = 0; thread->usrcs[i]; i++)
762 kfree(thread->usrcs[i]);
763 kfree(thread->usrcs);
764err_usrcs:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700765 kfree(thread->srcs);
766err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200767 kfree(pq_coefs);
768err_thread_type:
Dan Williams86727442013-11-06 16:30:07 -0800769 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
770 current->comm, total_tests, failed_tests,
771 dmatest_persec(runtime, total_tests),
772 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200773
Viresh Kumar9704efa2011-07-29 16:21:57 +0530774 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000775 if (ret)
776 dmaengine_terminate_all(chan);
777
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200778 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800779 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200780
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700781 return ret;
782}
783
784static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
785{
786 struct dmatest_thread *thread;
787 struct dmatest_thread *_thread;
788 int ret;
789
790 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
791 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800792 pr_debug("thread %s exited with status %d\n",
793 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700794 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800795 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700796 kfree(thread);
797 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530798
799 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000800 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530801
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700802 kfree(dtc);
803}
804
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200805static int dmatest_add_threads(struct dmatest_info *info,
806 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700807{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200808 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700809 struct dmatest_thread *thread;
810 struct dma_chan *chan = dtc->chan;
811 char *op;
812 unsigned int i;
813
814 if (type == DMA_MEMCPY)
815 op = "copy";
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530816 else if (type == DMA_SG)
817 op = "sg";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700818 else if (type == DMA_XOR)
819 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700820 else if (type == DMA_PQ)
821 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700822 else
823 return -EINVAL;
824
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200825 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700826 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
827 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800828 pr_warn("No memory for %s-%s%u\n",
829 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700830 break;
831 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200832 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700833 thread->chan = dtc->chan;
834 thread->type = type;
835 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800836 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700837 dma_chan_name(chan), op, i);
838 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800839 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800840 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700841 kfree(thread);
842 break;
843 }
844
845 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800846 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700847 list_add_tail(&thread->node, &dtc->threads);
Dan Williams2d88ce72013-11-06 16:30:09 -0800848 wake_up_process(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700849 }
850
851 return i;
852}
853
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200854static int dmatest_add_channel(struct dmatest_info *info,
855 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700856{
857 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700858 struct dma_device *dma_dev = chan->device;
859 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400860 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700861
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700862 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700863 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800864 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700865 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700866 }
867
868 dtc->chan = chan;
869 INIT_LIST_HEAD(&dtc->threads);
870
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700871 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530872 if (dmatest == 0) {
873 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
874 thread_count += cnt > 0 ? cnt : 0;
875 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700876 }
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530877
878 if (dma_has_cap(DMA_SG, dma_dev->cap_mask)) {
879 if (dmatest == 1) {
880 cnt = dmatest_add_threads(info, dtc, DMA_SG);
881 thread_count += cnt > 0 ? cnt : 0;
882 }
883 }
884
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700885 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200886 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200887 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700888 }
Dan Williams58691d62009-08-29 19:09:27 -0700889 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200890 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700891 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700892 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700893
Dan Williams0adff802013-11-06 16:30:00 -0800894 pr_info("Started %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700895 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700896
Andy Shevchenko838cc702013-03-04 11:09:28 +0200897 list_add_tail(&dtc->node, &info->channels);
898 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700899
Dan Williams33df8ca2009-01-06 11:38:15 -0700900 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700901}
902
Dan Williams7dd60252009-01-06 11:38:19 -0700903static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700904{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200905 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200906
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200907 if (!dmatest_match_channel(params, chan) ||
908 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700909 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700910 else
Dan Williams7dd60252009-01-06 11:38:19 -0700911 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700912}
913
Dan Williamsa9e55492013-11-06 16:30:02 -0800914static void request_channels(struct dmatest_info *info,
915 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700916{
Dan Williams33df8ca2009-01-06 11:38:15 -0700917 dma_cap_mask_t mask;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700918
Dan Williams33df8ca2009-01-06 11:38:15 -0700919 dma_cap_zero(mask);
Dan Williamsa9e55492013-11-06 16:30:02 -0800920 dma_cap_set(type, mask);
Dan Williams33df8ca2009-01-06 11:38:15 -0700921 for (;;) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800922 struct dmatest_params *params = &info->params;
923 struct dma_chan *chan;
924
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200925 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700926 if (chan) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800927 if (dmatest_add_channel(info, chan)) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700928 dma_release_channel(chan);
929 break; /* add_channel failed, punt */
930 }
931 } else
932 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200933 if (params->max_channels &&
934 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700935 break; /* we have all we need */
936 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700937}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700938
Dan Williamsa9e55492013-11-06 16:30:02 -0800939static void run_threaded_test(struct dmatest_info *info)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200940{
941 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200942
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200943 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +0300944 params->buf_size = test_buf_size;
945 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
946 strlcpy(params->device, strim(test_device), sizeof(params->device));
947 params->threads_per_chan = threads_per_chan;
948 params->max_channels = max_channels;
949 params->iterations = iterations;
950 params->xor_sources = xor_sources;
951 params->pq_sources = pq_sources;
952 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800953 params->noverify = noverify;
Dan Williamsa310d032013-11-06 16:30:01 -0800954
Dan Williamsa9e55492013-11-06 16:30:02 -0800955 request_channels(info, DMA_MEMCPY);
956 request_channels(info, DMA_XOR);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530957 request_channels(info, DMA_SG);
Dan Williamsa9e55492013-11-06 16:30:02 -0800958 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700959}
960
Dan Williamsa310d032013-11-06 16:30:01 -0800961static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700962{
963 struct dmatest_chan *dtc, *_dtc;
964 struct dma_chan *chan;
965
966 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
967 list_del(&dtc->node);
968 chan = dtc->chan;
969 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -0800970 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200971 dma_release_channel(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700972 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700973
Dan Williams7cbd4872009-03-04 16:06:03 -0700974 info->nr_channels = 0;
Dan Williams33df8ca2009-01-06 11:38:15 -0700975}
Andy Shevchenko838cc702013-03-04 11:09:28 +0200976
Dan Williamsa9e55492013-11-06 16:30:02 -0800977static void restart_threaded_test(struct dmatest_info *info, bool run)
Dan Williams7cbd4872009-03-04 16:06:03 -0700978{
Dan Williamsa310d032013-11-06 16:30:01 -0800979 /* we might be called early to set run=, defer running until all
980 * parameters have been evaluated
981 */
982 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -0800983 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200984
Dan Williamsa310d032013-11-06 16:30:01 -0800985 /* Stop any running test first */
986 stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200987
988 /* Run test with new parameters */
Dan Williamsa9e55492013-11-06 16:30:02 -0800989 run_threaded_test(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300990}
991
Dan Williamsa310d032013-11-06 16:30:01 -0800992static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300993{
Dan Williamsa310d032013-11-06 16:30:01 -0800994 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200995
996 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -0800997 if (is_threaded_test_run(info)) {
998 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200999 } else {
Dan Williamsa310d032013-11-06 16:30:01 -08001000 stop_threaded_test(info);
1001 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001002 }
Dan Williamsa310d032013-11-06 16:30:01 -08001003 mutex_unlock(&info->lock);
1004
1005 return param_get_bool(val, kp);
1006}
1007
1008static int dmatest_run_set(const char *val, const struct kernel_param *kp)
1009{
1010 struct dmatest_info *info = &test_info;
1011 int ret;
1012
1013 mutex_lock(&info->lock);
1014 ret = param_set_bool(val, kp);
1015 if (ret) {
1016 mutex_unlock(&info->lock);
1017 return ret;
1018 }
1019
1020 if (is_threaded_test_run(info))
1021 ret = -EBUSY;
1022 else if (dmatest_run)
Dan Williamsa9e55492013-11-06 16:30:02 -08001023 restart_threaded_test(info, dmatest_run);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001024
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001025 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001026
Dan Williamsa310d032013-11-06 16:30:01 -08001027 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001028}
1029
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001030static int __init dmatest_init(void)
1031{
1032 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -08001033 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001034
Dan Williamsa310d032013-11-06 16:30:01 -08001035 if (dmatest_run) {
1036 mutex_lock(&info->lock);
Dan Williamsa9e55492013-11-06 16:30:02 -08001037 run_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001038 mutex_unlock(&info->lock);
1039 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001040
Dan Williams2d88ce72013-11-06 16:30:09 -08001041 if (params->iterations && wait)
1042 wait_event(thread_wait, !is_threaded_test_run(info));
Andy Shevchenko838cc702013-03-04 11:09:28 +02001043
Dan Williamsa310d032013-11-06 16:30:01 -08001044 /* module parameters are stable, inittime tests are started,
1045 * let userspace take over 'run' control
1046 */
1047 info->did_init = true;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001048
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001049 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001050}
1051/* when compiled-in wait for drivers to load first */
1052late_initcall(dmatest_init);
1053
1054static void __exit dmatest_exit(void)
1055{
1056 struct dmatest_info *info = &test_info;
1057
Dan Williamsa310d032013-11-06 16:30:01 -08001058 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001059 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001060 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001061}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001062module_exit(dmatest_exit);
1063
Jean Delvaree05503e2011-05-18 16:49:24 +02001064MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001065MODULE_LICENSE("GPL v2");