blob: 34ff53290b0378434a253b75a2c2eac4215a4fb5 [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>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010019#include <linux/sched/task.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070020#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070024#include <linux/wait.h>
25
26static unsigned int test_buf_size = 16384;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030027module_param(test_buf_size, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070028MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
29
Kay Sievers06190d82008-11-11 13:12:33 -070030static char test_channel[20];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030031module_param_string(channel, test_channel, sizeof(test_channel),
32 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070033MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
34
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +010035static char test_device[32];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030036module_param_string(device, test_device, sizeof(test_device),
37 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070038MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
39
40static unsigned int threads_per_chan = 1;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030041module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070042MODULE_PARM_DESC(threads_per_chan,
43 "Number of threads to start per channel (default: 1)");
44
45static unsigned int max_channels;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030046module_param(max_channels, uint, S_IRUGO | S_IWUSR);
Dan Williams33df8ca2009-01-06 11:38:15 -070047MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070048 "Maximum number of channels to use (default: all)");
49
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020050static unsigned int iterations;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030051module_param(iterations, uint, S_IRUGO | S_IWUSR);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020052MODULE_PARM_DESC(iterations,
53 "Iterations before stopping test (default: infinite)");
54
Eugeniy Paltsevd8646722016-09-14 20:40:38 +030055static unsigned int dmatest;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053056module_param(dmatest, uint, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(dmatest,
Dave Jiangc678fa62017-08-21 10:23:13 -070058 "dmatest 0-memcpy 1-memset (default: 0)");
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053059
Dan Williamsb54d5cb2009-03-25 09:13:25 -070060static unsigned int xor_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030061module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
Dan Williamsb54d5cb2009-03-25 09:13:25 -070062MODULE_PARM_DESC(xor_sources,
63 "Number of xor source buffers (default: 3)");
64
Dan Williams58691d62009-08-29 19:09:27 -070065static unsigned int pq_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030066module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
Dan Williams58691d62009-08-29 19:09:27 -070067MODULE_PARM_DESC(pq_sources,
68 "Number of p+q source buffers (default: 3)");
69
Viresh Kumard42efe62011-03-22 17:27:25 +053070static int timeout = 3000;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030071module_param(timeout, uint, S_IRUGO | S_IWUSR);
Joe Perches85ee7a12011-04-23 20:38:19 -070072MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
73 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053074
Dan Williamse3b9c342013-11-06 16:30:05 -080075static bool noverify;
76module_param(noverify, bool, S_IRUGO | S_IWUSR);
77MODULE_PARM_DESC(noverify, "Disable random data setup and verification");
Andy Shevchenko74b5c072013-03-04 11:09:32 +020078
Dan Williams50137a72013-11-08 12:26:26 -080079static bool verbose;
80module_param(verbose, bool, S_IRUGO | S_IWUSR);
81MODULE_PARM_DESC(verbose, "Enable \"success\" result messages (default: off)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070082
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020083/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020084 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020085 * @buf_size: size of the memcpy test buffer
86 * @channel: bus ID of the channel to test
87 * @device: bus ID of the DMA Engine to test
88 * @threads_per_chan: number of threads to start per channel
89 * @max_channels: maximum number of channels to use
90 * @iterations: iterations before stopping test
91 * @xor_sources: number of xor source buffers
92 * @pq_sources: number of p+q source buffers
93 * @timeout: transfer timeout in msec, -1 for infinite timeout
94 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020095struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020096 unsigned int buf_size;
97 char channel[20];
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +010098 char device[32];
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020099 unsigned int threads_per_chan;
100 unsigned int max_channels;
101 unsigned int iterations;
102 unsigned int xor_sources;
103 unsigned int pq_sources;
104 int timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800105 bool noverify;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200106};
107
108/**
109 * struct dmatest_info - test information.
110 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200111 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200112 */
Dan Williamsa310d032013-11-06 16:30:01 -0800113static struct dmatest_info {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200114 /* Test parameters */
115 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200116
117 /* Internal state */
118 struct list_head channels;
119 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200120 struct mutex lock;
Dan Williamsa310d032013-11-06 16:30:01 -0800121 bool did_init;
122} test_info = {
123 .channels = LIST_HEAD_INIT(test_info.channels),
124 .lock = __MUTEX_INITIALIZER(test_info.lock),
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200125};
126
Dan Williamsa310d032013-11-06 16:30:01 -0800127static int dmatest_run_set(const char *val, const struct kernel_param *kp);
128static int dmatest_run_get(char *val, const struct kernel_param *kp);
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930129static const struct kernel_param_ops run_ops = {
Dan Williamsa310d032013-11-06 16:30:01 -0800130 .set = dmatest_run_set,
131 .get = dmatest_run_get,
132};
133static bool dmatest_run;
134module_param_cb(run, &run_ops, &dmatest_run, S_IRUGO | S_IWUSR);
135MODULE_PARM_DESC(run, "Run the test (default: false)");
136
137/* Maximum amount of mismatched bytes in buffer to print */
138#define MAX_ERROR_COUNT 32
139
140/*
141 * Initialization patterns. All bytes in the source buffer has bit 7
142 * set, all bytes in the destination buffer has bit 7 cleared.
143 *
144 * Bit 6 is set for all bytes which are to be copied by the DMA
145 * engine. Bit 5 is set for all bytes which are to be overwritten by
146 * the DMA engine.
147 *
148 * The remaining bits are the inverse of a counter which increments by
149 * one for each byte address.
150 */
151#define PATTERN_SRC 0x80
152#define PATTERN_DST 0x00
153#define PATTERN_COPY 0x40
154#define PATTERN_OVERWRITE 0x20
155#define PATTERN_COUNT_MASK 0x1f
Sinan Kaya61b5f542017-06-29 22:30:58 -0400156#define PATTERN_MEMSET_IDX 0x01
Dan Williamsa310d032013-11-06 16:30:01 -0800157
158struct dmatest_thread {
159 struct list_head node;
160 struct dmatest_info *info;
161 struct task_struct *task;
162 struct dma_chan *chan;
163 u8 **srcs;
Dave Jiangd6481602016-11-29 13:22:20 -0700164 u8 **usrcs;
Dan Williamsa310d032013-11-06 16:30:01 -0800165 u8 **dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700166 u8 **udsts;
Dan Williamsa310d032013-11-06 16:30:01 -0800167 enum dma_transaction_type type;
168 bool done;
169};
170
171struct dmatest_chan {
172 struct list_head node;
173 struct dma_chan *chan;
174 struct list_head threads;
175};
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200176
Dan Williams2d88ce72013-11-06 16:30:09 -0800177static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
178static bool wait;
179
180static bool is_threaded_test_run(struct dmatest_info *info)
181{
182 struct dmatest_chan *dtc;
183
184 list_for_each_entry(dtc, &info->channels, node) {
185 struct dmatest_thread *thread;
186
187 list_for_each_entry(thread, &dtc->threads, node) {
188 if (!thread->done)
189 return true;
190 }
191 }
192
193 return false;
194}
195
196static int dmatest_wait_get(char *val, const struct kernel_param *kp)
197{
198 struct dmatest_info *info = &test_info;
199 struct dmatest_params *params = &info->params;
200
201 if (params->iterations)
202 wait_event(thread_wait, !is_threaded_test_run(info));
203 wait = true;
204 return param_get_bool(val, kp);
205}
206
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930207static const struct kernel_param_ops wait_ops = {
Dan Williams2d88ce72013-11-06 16:30:09 -0800208 .get = dmatest_wait_get,
209 .set = param_set_bool,
210};
211module_param_cb(wait, &wait_ops, &wait, S_IRUGO);
212MODULE_PARM_DESC(wait, "Wait for tests to complete (default: false)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700213
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200214static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200215 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700216{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200217 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700218 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200219 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700220}
221
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200222static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200223 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700224{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200225 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700226 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200227 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700228}
229
230static unsigned long dmatest_random(void)
231{
232 unsigned long buf;
233
Dan Williamsbe9fa5a2013-11-06 16:30:03 -0800234 prandom_bytes(&buf, sizeof(buf));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700235 return buf;
236}
237
Sinan Kaya61b5f542017-06-29 22:30:58 -0400238static inline u8 gen_inv_idx(u8 index, bool is_memset)
239{
240 u8 val = is_memset ? PATTERN_MEMSET_IDX : index;
241
242 return ~val & PATTERN_COUNT_MASK;
243}
244
245static inline u8 gen_src_value(u8 index, bool is_memset)
246{
247 return PATTERN_SRC | gen_inv_idx(index, is_memset);
248}
249
250static inline u8 gen_dst_value(u8 index, bool is_memset)
251{
252 return PATTERN_DST | gen_inv_idx(index, is_memset);
253}
254
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200255static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400256 unsigned int buf_size, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700257{
258 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700259 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700260
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700261 for (; (buf = *bufs); bufs++) {
262 for (i = 0; i < start; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400263 buf[i] = gen_src_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700264 for ( ; i < start + len; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400265 buf[i] = gen_src_value(i, is_memset) | PATTERN_COPY;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200266 for ( ; i < buf_size; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400267 buf[i] = gen_src_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700268 buf++;
269 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700270}
271
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200272static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400273 unsigned int buf_size, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700274{
275 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700276 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700277
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700278 for (; (buf = *bufs); bufs++) {
279 for (i = 0; i < start; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400280 buf[i] = gen_dst_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700281 for ( ; i < start + len; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400282 buf[i] = gen_dst_value(i, is_memset) |
283 PATTERN_OVERWRITE;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200284 for ( ; i < buf_size; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400285 buf[i] = gen_dst_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700286 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700287}
288
Dan Williams7b610172013-11-06 16:29:57 -0800289static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400290 unsigned int counter, bool is_srcbuf, bool is_memset)
Dan Williams7b610172013-11-06 16:29:57 -0800291{
292 u8 diff = actual ^ pattern;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400293 u8 expected = pattern | gen_inv_idx(counter, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800294 const char *thread_name = current->comm;
295
296 if (is_srcbuf)
297 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
298 thread_name, index, expected, actual);
299 else if ((pattern & PATTERN_COPY)
300 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
301 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
302 thread_name, index, expected, actual);
303 else if (diff & PATTERN_SRC)
304 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
305 thread_name, index, expected, actual);
306 else
307 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
308 thread_name, index, expected, actual);
309}
310
311static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
312 unsigned int end, unsigned int counter, u8 pattern,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400313 bool is_srcbuf, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700314{
315 unsigned int i;
316 unsigned int error_count = 0;
317 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700318 u8 expected;
319 u8 *buf;
320 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700321
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700322 for (; (buf = *bufs); bufs++) {
323 counter = counter_orig;
324 for (i = start; i < end; i++) {
325 actual = buf[i];
Sinan Kaya61b5f542017-06-29 22:30:58 -0400326 expected = pattern | gen_inv_idx(counter, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700327 if (actual != expected) {
Dan Williams7b610172013-11-06 16:29:57 -0800328 if (error_count < MAX_ERROR_COUNT)
329 dmatest_mismatch(actual, pattern, i,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400330 counter, is_srcbuf,
331 is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700332 error_count++;
333 }
334 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700335 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700336 }
337
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200338 if (error_count > MAX_ERROR_COUNT)
Dan Williams7b610172013-11-06 16:29:57 -0800339 pr_warn("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200340 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700341
342 return error_count;
343}
344
Tejun Heoadfa5432011-11-23 09:28:16 -0800345/* poor man's completion - we want to use wait_event_freezable() on it */
346struct dmatest_done {
347 bool done;
348 wait_queue_head_t *wait;
349};
350
351static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700352{
Tejun Heoadfa5432011-11-23 09:28:16 -0800353 struct dmatest_done *done = arg;
354
355 done->done = true;
356 wake_up_all(done->wait);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700357}
358
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900359static unsigned int min_odd(unsigned int x, unsigned int y)
360{
361 unsigned int val = min(x, y);
362
363 return val % 2 ? val : val - 1;
364}
365
Dan Williams872f05c2013-11-06 16:29:58 -0800366static void result(const char *err, unsigned int n, unsigned int src_off,
367 unsigned int dst_off, unsigned int len, unsigned long data)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200368{
Jerome Blin2acec152014-03-04 10:38:55 +0100369 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 -0800370 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200371}
372
Dan Williams872f05c2013-11-06 16:29:58 -0800373static void dbg_result(const char *err, unsigned int n, unsigned int src_off,
374 unsigned int dst_off, unsigned int len,
375 unsigned long data)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200376{
Jerome Blin2acec152014-03-04 10:38:55 +0100377 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 +0300378 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200379}
380
Andy Shevchenkoa835bb82014-10-22 16:16:42 +0300381#define verbose_result(err, n, src_off, dst_off, len, data) ({ \
382 if (verbose) \
383 result(err, n, src_off, dst_off, len, data); \
384 else \
385 dbg_result(err, n, src_off, dst_off, len, data);\
Dan Williams50137a72013-11-08 12:26:26 -0800386})
387
Dan Williams86727442013-11-06 16:30:07 -0800388static unsigned long long dmatest_persec(s64 runtime, unsigned int val)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200389{
Dan Williams86727442013-11-06 16:30:07 -0800390 unsigned long long per_sec = 1000000;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200391
Dan Williams86727442013-11-06 16:30:07 -0800392 if (runtime <= 0)
393 return 0;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200394
Dan Williams86727442013-11-06 16:30:07 -0800395 /* drop precision until runtime is 32-bits */
396 while (runtime > UINT_MAX) {
397 runtime >>= 1;
398 per_sec <<= 1;
399 }
Andy Shevchenko95019c82013-03-04 11:09:33 +0200400
Dan Williams86727442013-11-06 16:30:07 -0800401 per_sec *= val;
402 do_div(per_sec, runtime);
403 return per_sec;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200404}
405
Dan Williams86727442013-11-06 16:30:07 -0800406static unsigned long long dmatest_KBs(s64 runtime, unsigned long long len)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200407{
Dan Williams86727442013-11-06 16:30:07 -0800408 return dmatest_persec(runtime, len >> 10);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200409}
410
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700411/*
412 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700413 * offsets for a given operation type until it is told to exit by
414 * kthread_stop(). There may be multiple threads running this function
415 * in parallel for a single channel, and there may be multiple channels
416 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700417 *
418 * Before each test, the source and destination buffer is initialized
419 * with a known pattern. This pattern is different depending on
420 * whether it's in an area which is supposed to be copied or
421 * overwritten, and different in the source and destination buffers.
422 * So if the DMA engine doesn't copy exactly what we tell it to copy,
423 * we'll notice.
424 */
425static int dmatest_func(void *data)
426{
Tejun Heoadfa5432011-11-23 09:28:16 -0800427 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700428 struct dmatest_thread *thread = data;
Tejun Heoadfa5432011-11-23 09:28:16 -0800429 struct dmatest_done done = { .wait = &done_wait };
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200430 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200431 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700432 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900433 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700434 unsigned int error_count;
435 unsigned int failed_tests = 0;
436 unsigned int total_tests = 0;
437 dma_cookie_t cookie;
438 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700439 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200440 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700441 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700442 int src_cnt;
443 int dst_cnt;
444 int i;
Sinan Kayae9405ef2016-09-01 10:02:55 -0400445 ktime_t ktime, start, diff;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100446 ktime_t filltime = 0;
447 ktime_t comparetime = 0;
Dan Williams86727442013-11-06 16:30:07 -0800448 s64 runtime = 0;
449 unsigned long long total_len = 0;
Dave Jiangd6481602016-11-29 13:22:20 -0700450 u8 align = 0;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400451 bool is_memset = false;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700452
Tejun Heoadfa5432011-11-23 09:28:16 -0800453 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700454
455 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700456
457 smp_rmb();
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200458 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200459 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700460 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900461 dev = chan->device;
Dave Jiangd6481602016-11-29 13:22:20 -0700462 if (thread->type == DMA_MEMCPY) {
463 align = dev->copy_align;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700464 src_cnt = dst_cnt = 1;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400465 } else if (thread->type == DMA_MEMSET) {
466 align = dev->fill_align;
467 src_cnt = dst_cnt = 1;
468 is_memset = true;
Dave Jiangd6481602016-11-29 13:22:20 -0700469 } else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900470 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200471 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700472 dst_cnt = 1;
Dave Jiangd6481602016-11-29 13:22:20 -0700473 align = dev->xor_align;
Dan Williams58691d62009-08-29 19:09:27 -0700474 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900475 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200476 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700477 dst_cnt = 2;
Dave Jiangd6481602016-11-29 13:22:20 -0700478 align = dev->pq_align;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200479
Dave Jiang31d18252016-11-29 13:22:01 -0700480 pq_coefs = kmalloc(params->pq_sources + 1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200481 if (!pq_coefs)
482 goto err_thread_type;
483
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100484 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700485 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700486 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200487 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700488
Dave Jiang31d18252016-11-29 13:22:01 -0700489 thread->srcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700490 if (!thread->srcs)
491 goto err_srcs;
Dave Jiangd6481602016-11-29 13:22:20 -0700492
493 thread->usrcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
494 if (!thread->usrcs)
495 goto err_usrcs;
496
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700497 for (i = 0; i < src_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700498 thread->usrcs[i] = kmalloc(params->buf_size + align,
499 GFP_KERNEL);
500 if (!thread->usrcs[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700501 goto err_srcbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700502
503 /* align srcs to alignment restriction */
504 if (align)
505 thread->srcs[i] = PTR_ALIGN(thread->usrcs[i], align);
506 else
507 thread->srcs[i] = thread->usrcs[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700508 }
509 thread->srcs[i] = NULL;
510
Dave Jiang31d18252016-11-29 13:22:01 -0700511 thread->dsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700512 if (!thread->dsts)
513 goto err_dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700514
515 thread->udsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
516 if (!thread->udsts)
517 goto err_udsts;
518
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700519 for (i = 0; i < dst_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700520 thread->udsts[i] = kmalloc(params->buf_size + align,
521 GFP_KERNEL);
522 if (!thread->udsts[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700523 goto err_dstbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700524
525 /* align dsts to alignment restriction */
526 if (align)
527 thread->dsts[i] = PTR_ALIGN(thread->udsts[i], align);
528 else
529 thread->dsts[i] = thread->udsts[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700530 }
531 thread->dsts[i] = NULL;
532
Dan Williamse44e0aa2009-03-25 09:13:25 -0700533 set_user_nice(current, 10);
534
Ira Snyderb203bd32011-03-03 07:54:53 +0000535 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200536 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000537 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200538 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700539
Dan Williams86727442013-11-06 16:30:07 -0800540 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200541 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200542 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700543 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams4076e752013-11-06 16:30:10 -0800544 struct dmaengine_unmap_data *um;
545 dma_addr_t srcs[src_cnt];
546 dma_addr_t *dsts;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300547 unsigned int src_off, dst_off, len;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700548
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700549 total_tests++;
550
Stefan Roesefbfb8e12017-04-27 14:21:41 +0200551 /* Check if buffer count fits into map count variable (u8) */
552 if ((src_cnt + dst_cnt) >= 255) {
553 pr_err("too many buffers (%d of 255 supported)\n",
554 src_cnt + dst_cnt);
555 break;
556 }
557
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200558 if (1 << align > params->buf_size) {
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100559 pr_err("%u-byte buffer too small for %d-byte alignment\n",
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200560 params->buf_size, 1 << align);
Guennadi Liakhovetskicfe4f272009-12-04 19:44:48 +0100561 break;
562 }
563
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300564 if (params->noverify)
Dan Williamse3b9c342013-11-06 16:30:05 -0800565 len = params->buf_size;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300566 else
567 len = dmatest_random() % params->buf_size + 1;
568
569 len = (len >> align) << align;
570 if (!len)
571 len = 1 << align;
572
573 total_len += len;
574
575 if (params->noverify) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800576 src_off = 0;
577 dst_off = 0;
578 } else {
Sinan Kayae9405ef2016-09-01 10:02:55 -0400579 start = ktime_get();
Dan Williamse3b9c342013-11-06 16:30:05 -0800580 src_off = dmatest_random() % (params->buf_size - len + 1);
581 dst_off = dmatest_random() % (params->buf_size - len + 1);
582
583 src_off = (src_off >> align) << align;
584 dst_off = (dst_off >> align) << align;
585
586 dmatest_init_srcs(thread->srcs, src_off, len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400587 params->buf_size, is_memset);
Dan Williamse3b9c342013-11-06 16:30:05 -0800588 dmatest_init_dsts(thread->dsts, dst_off, len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400589 params->buf_size, is_memset);
Sinan Kayae9405ef2016-09-01 10:02:55 -0400590
591 diff = ktime_sub(ktime_get(), start);
592 filltime = ktime_add(filltime, diff);
Dan Williamse3b9c342013-11-06 16:30:05 -0800593 }
594
Dave Jiang31d18252016-11-29 13:22:01 -0700595 um = dmaengine_get_unmap_data(dev->dev, src_cnt + dst_cnt,
Dan Williams4076e752013-11-06 16:30:10 -0800596 GFP_KERNEL);
597 if (!um) {
598 failed_tests++;
599 result("unmap data NULL", total_tests,
600 src_off, dst_off, len, ret);
601 continue;
602 }
Dan Williams83544ae2009-09-08 17:42:53 -0700603
Dan Williams4076e752013-11-06 16:30:10 -0800604 um->len = params->buf_size;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700605 for (i = 0; i < src_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800606 void *buf = thread->srcs[i];
Dan Williams4076e752013-11-06 16:30:10 -0800607 struct page *pg = virt_to_page(buf);
Geliang Tangf62e5f62017-04-22 09:18:03 +0800608 unsigned long pg_off = offset_in_page(buf);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700609
Dan Williams4076e752013-11-06 16:30:10 -0800610 um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
611 um->len, DMA_TO_DEVICE);
612 srcs[i] = um->addr[i] + src_off;
613 ret = dma_mapping_error(dev->dev, um->addr[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800614 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800615 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800616 result("src mapping error", total_tests,
617 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800618 failed_tests++;
619 continue;
620 }
Dan Williams4076e752013-11-06 16:30:10 -0800621 um->to_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700622 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700623 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williams4076e752013-11-06 16:30:10 -0800624 dsts = &um->addr[src_cnt];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700625 for (i = 0; i < dst_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800626 void *buf = thread->dsts[i];
Dan Williams4076e752013-11-06 16:30:10 -0800627 struct page *pg = virt_to_page(buf);
Geliang Tangf62e5f62017-04-22 09:18:03 +0800628 unsigned long pg_off = offset_in_page(buf);
Dan Williams4076e752013-11-06 16:30:10 -0800629
630 dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
631 DMA_BIDIRECTIONAL);
632 ret = dma_mapping_error(dev->dev, dsts[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800633 if (ret) {
Dan Williams4076e752013-11-06 16:30:10 -0800634 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800635 result("dst mapping error", total_tests,
636 src_off, dst_off, len, ret);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800637 failed_tests++;
638 continue;
639 }
Dan Williams4076e752013-11-06 16:30:10 -0800640 um->bidi_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700641 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700642
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700643 if (thread->type == DMA_MEMCPY)
644 tx = dev->device_prep_dma_memcpy(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800645 dsts[0] + dst_off,
646 srcs[0], len, flags);
Sinan Kaya61b5f542017-06-29 22:30:58 -0400647 else if (thread->type == DMA_MEMSET)
648 tx = dev->device_prep_dma_memset(chan,
649 dsts[0] + dst_off,
650 *(thread->srcs[0] + src_off),
651 len, flags);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700652 else if (thread->type == DMA_XOR)
653 tx = dev->device_prep_dma_xor(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800654 dsts[0] + dst_off,
655 srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700656 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700657 else if (thread->type == DMA_PQ) {
658 dma_addr_t dma_pq[dst_cnt];
659
660 for (i = 0; i < dst_cnt; i++)
Dan Williams4076e752013-11-06 16:30:10 -0800661 dma_pq[i] = dsts[i] + dst_off;
662 tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100663 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700664 len, flags);
665 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700666
Atsushi Nemotod86be862009-01-13 09:22:20 -0700667 if (!tx) {
Dan Williams4076e752013-11-06 16:30:10 -0800668 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800669 result("prep error", total_tests, src_off,
670 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700671 msleep(100);
672 failed_tests++;
673 continue;
674 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700675
Tejun Heoadfa5432011-11-23 09:28:16 -0800676 done.done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700677 tx->callback = dmatest_callback;
Tejun Heoadfa5432011-11-23 09:28:16 -0800678 tx->callback_param = &done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700679 cookie = tx->tx_submit(tx);
680
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700681 if (dma_submit_error(cookie)) {
Dan Williams4076e752013-11-06 16:30:10 -0800682 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800683 result("submit error", total_tests, src_off,
684 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700685 msleep(100);
686 failed_tests++;
687 continue;
688 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700689 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700690
Andy Shevchenkobcc567e2013-05-23 14:29:53 +0300691 wait_event_freezable_timeout(done_wait, done.done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200692 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200693
Dan Williamse44e0aa2009-03-25 09:13:25 -0700694 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700695
Tejun Heoadfa5432011-11-23 09:28:16 -0800696 if (!done.done) {
697 /*
698 * We're leaving the timed out dma operation with
699 * dangling pointer to done_wait. To make this
700 * correct, we'll need to allocate wait_done for
701 * each test iteration and perform "who's gonna
702 * free it this time?" dancing. For now, just
703 * leave it dangling.
704 */
Dan Williams4076e752013-11-06 16:30:10 -0800705 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800706 result("test timed out", total_tests, src_off, dst_off,
707 len, 0);
Dan Williamse44e0aa2009-03-25 09:13:25 -0700708 failed_tests++;
709 continue;
Vinod Koul19e9f992013-10-16 13:37:27 +0530710 } else if (status != DMA_COMPLETE) {
Dan Williams4076e752013-11-06 16:30:10 -0800711 dmaengine_unmap_put(um);
Dan Williams872f05c2013-11-06 16:29:58 -0800712 result(status == DMA_ERROR ?
713 "completion error status" :
714 "completion busy status", total_tests, src_off,
715 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700716 failed_tests++;
717 continue;
718 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700719
Dan Williams4076e752013-11-06 16:30:10 -0800720 dmaengine_unmap_put(um);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700721
Dan Williamse3b9c342013-11-06 16:30:05 -0800722 if (params->noverify) {
Dan Williams50137a72013-11-08 12:26:26 -0800723 verbose_result("test passed", total_tests, src_off,
724 dst_off, len, 0);
Dan Williamse3b9c342013-11-06 16:30:05 -0800725 continue;
726 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700727
Sinan Kayae9405ef2016-09-01 10:02:55 -0400728 start = ktime_get();
Dan Williams872f05c2013-11-06 16:29:58 -0800729 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800730 error_count = dmatest_verify(thread->srcs, 0, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400731 0, PATTERN_SRC, true, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800732 error_count += dmatest_verify(thread->srcs, src_off,
733 src_off + len, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400734 PATTERN_SRC | PATTERN_COPY, true, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800735 error_count += dmatest_verify(thread->srcs, src_off + len,
736 params->buf_size, src_off + len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400737 PATTERN_SRC, true, is_memset);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700738
Dan Williams872f05c2013-11-06 16:29:58 -0800739 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800740 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400741 0, PATTERN_DST, false, is_memset);
742
Dan Williams7b610172013-11-06 16:29:57 -0800743 error_count += dmatest_verify(thread->dsts, dst_off,
744 dst_off + len, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400745 PATTERN_SRC | PATTERN_COPY, false, is_memset);
746
Dan Williams7b610172013-11-06 16:29:57 -0800747 error_count += dmatest_verify(thread->dsts, dst_off + len,
748 params->buf_size, dst_off + len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400749 PATTERN_DST, false, is_memset);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700750
Sinan Kayae9405ef2016-09-01 10:02:55 -0400751 diff = ktime_sub(ktime_get(), start);
752 comparetime = ktime_add(comparetime, diff);
753
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700754 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800755 result("data error", total_tests, src_off, dst_off,
756 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700757 failed_tests++;
758 } else {
Dan Williams50137a72013-11-08 12:26:26 -0800759 verbose_result("test passed", total_tests, src_off,
760 dst_off, len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700761 }
762 }
Sinan Kayae9405ef2016-09-01 10:02:55 -0400763 ktime = ktime_sub(ktime_get(), ktime);
764 ktime = ktime_sub(ktime, comparetime);
765 ktime = ktime_sub(ktime, filltime);
766 runtime = ktime_to_us(ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700767
768 ret = 0;
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300769err_dstbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700770 for (i = 0; thread->udsts[i]; i++)
771 kfree(thread->udsts[i]);
772 kfree(thread->udsts);
773err_udsts:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700774 kfree(thread->dsts);
775err_dsts:
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300776err_srcbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700777 for (i = 0; thread->usrcs[i]; i++)
778 kfree(thread->usrcs[i]);
779 kfree(thread->usrcs);
780err_usrcs:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700781 kfree(thread->srcs);
782err_srcs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200783 kfree(pq_coefs);
784err_thread_type:
Dan Williams86727442013-11-06 16:30:07 -0800785 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
786 current->comm, total_tests, failed_tests,
787 dmatest_persec(runtime, total_tests),
788 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200789
Viresh Kumar9704efa2011-07-29 16:21:57 +0530790 /* terminate all transfers on specified channels */
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000791 if (ret)
792 dmaengine_terminate_all(chan);
793
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200794 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800795 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200796
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700797 return ret;
798}
799
800static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
801{
802 struct dmatest_thread *thread;
803 struct dmatest_thread *_thread;
804 int ret;
805
806 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
807 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800808 pr_debug("thread %s exited with status %d\n",
809 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700810 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800811 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700812 kfree(thread);
813 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530814
815 /* terminate all transfers on specified channels */
Jon Mason944ea4d2012-11-11 23:03:20 +0000816 dmaengine_terminate_all(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530817
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700818 kfree(dtc);
819}
820
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200821static int dmatest_add_threads(struct dmatest_info *info,
822 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700823{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200824 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700825 struct dmatest_thread *thread;
826 struct dma_chan *chan = dtc->chan;
827 char *op;
828 unsigned int i;
829
830 if (type == DMA_MEMCPY)
831 op = "copy";
Sinan Kaya61b5f542017-06-29 22:30:58 -0400832 else if (type == DMA_MEMSET)
833 op = "set";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700834 else if (type == DMA_XOR)
835 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700836 else if (type == DMA_PQ)
837 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700838 else
839 return -EINVAL;
840
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200841 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700842 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
843 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800844 pr_warn("No memory for %s-%s%u\n",
845 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700846 break;
847 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200848 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700849 thread->chan = dtc->chan;
850 thread->type = type;
851 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800852 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700853 dma_chan_name(chan), op, i);
854 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800855 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800856 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700857 kfree(thread);
858 break;
859 }
860
861 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800862 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700863 list_add_tail(&thread->node, &dtc->threads);
Dan Williams2d88ce72013-11-06 16:30:09 -0800864 wake_up_process(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700865 }
866
867 return i;
868}
869
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200870static int dmatest_add_channel(struct dmatest_info *info,
871 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700872{
873 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700874 struct dma_device *dma_dev = chan->device;
875 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400876 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700877
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700878 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700879 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800880 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700881 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700882 }
883
884 dtc->chan = chan;
885 INIT_LIST_HEAD(&dtc->threads);
886
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700887 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530888 if (dmatest == 0) {
889 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
890 thread_count += cnt > 0 ? cnt : 0;
891 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700892 }
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530893
Sinan Kaya61b5f542017-06-29 22:30:58 -0400894 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530895 if (dmatest == 1) {
Dave Jiangc678fa62017-08-21 10:23:13 -0700896 cnt = dmatest_add_threads(info, dtc, DMA_MEMSET);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530897 thread_count += cnt > 0 ? cnt : 0;
898 }
899 }
900
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700901 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200902 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200903 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700904 }
Dan Williams58691d62009-08-29 19:09:27 -0700905 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200906 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700907 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700908 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700909
Dan Williams0adff802013-11-06 16:30:00 -0800910 pr_info("Started %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700911 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700912
Andy Shevchenko838cc702013-03-04 11:09:28 +0200913 list_add_tail(&dtc->node, &info->channels);
914 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700915
Dan Williams33df8ca2009-01-06 11:38:15 -0700916 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700917}
918
Dan Williams7dd60252009-01-06 11:38:19 -0700919static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700920{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200921 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200922
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200923 if (!dmatest_match_channel(params, chan) ||
924 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700925 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700926 else
Dan Williams7dd60252009-01-06 11:38:19 -0700927 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700928}
929
Dan Williamsa9e55492013-11-06 16:30:02 -0800930static void request_channels(struct dmatest_info *info,
931 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700932{
Dan Williams33df8ca2009-01-06 11:38:15 -0700933 dma_cap_mask_t mask;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700934
Dan Williams33df8ca2009-01-06 11:38:15 -0700935 dma_cap_zero(mask);
Dan Williamsa9e55492013-11-06 16:30:02 -0800936 dma_cap_set(type, mask);
Dan Williams33df8ca2009-01-06 11:38:15 -0700937 for (;;) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800938 struct dmatest_params *params = &info->params;
939 struct dma_chan *chan;
940
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200941 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -0700942 if (chan) {
Dan Williamsa9e55492013-11-06 16:30:02 -0800943 if (dmatest_add_channel(info, chan)) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700944 dma_release_channel(chan);
945 break; /* add_channel failed, punt */
946 }
947 } else
948 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200949 if (params->max_channels &&
950 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -0700951 break; /* we have all we need */
952 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700953}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700954
Dan Williamsa9e55492013-11-06 16:30:02 -0800955static void run_threaded_test(struct dmatest_info *info)
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200956{
957 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200958
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200959 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +0300960 params->buf_size = test_buf_size;
961 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
962 strlcpy(params->device, strim(test_device), sizeof(params->device));
963 params->threads_per_chan = threads_per_chan;
964 params->max_channels = max_channels;
965 params->iterations = iterations;
966 params->xor_sources = xor_sources;
967 params->pq_sources = pq_sources;
968 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800969 params->noverify = noverify;
Dan Williamsa310d032013-11-06 16:30:01 -0800970
Dan Williamsa9e55492013-11-06 16:30:02 -0800971 request_channels(info, DMA_MEMCPY);
Sinan Kaya61b5f542017-06-29 22:30:58 -0400972 request_channels(info, DMA_MEMSET);
Dan Williamsa9e55492013-11-06 16:30:02 -0800973 request_channels(info, DMA_XOR);
974 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700975}
976
Dan Williamsa310d032013-11-06 16:30:01 -0800977static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700978{
979 struct dmatest_chan *dtc, *_dtc;
980 struct dma_chan *chan;
981
982 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
983 list_del(&dtc->node);
984 chan = dtc->chan;
985 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -0800986 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200987 dma_release_channel(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700988 }
Dan Williams33df8ca2009-01-06 11:38:15 -0700989
Dan Williams7cbd4872009-03-04 16:06:03 -0700990 info->nr_channels = 0;
Dan Williams33df8ca2009-01-06 11:38:15 -0700991}
Andy Shevchenko838cc702013-03-04 11:09:28 +0200992
Dan Williamsa9e55492013-11-06 16:30:02 -0800993static void restart_threaded_test(struct dmatest_info *info, bool run)
Dan Williams7cbd4872009-03-04 16:06:03 -0700994{
Dan Williamsa310d032013-11-06 16:30:01 -0800995 /* we might be called early to set run=, defer running until all
996 * parameters have been evaluated
997 */
998 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -0800999 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001000
Dan Williamsa310d032013-11-06 16:30:01 -08001001 /* Stop any running test first */
1002 stop_threaded_test(info);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001003
1004 /* Run test with new parameters */
Dan Williamsa9e55492013-11-06 16:30:02 -08001005 run_threaded_test(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001006}
1007
Dan Williamsa310d032013-11-06 16:30:01 -08001008static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001009{
Dan Williamsa310d032013-11-06 16:30:01 -08001010 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001011
1012 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -08001013 if (is_threaded_test_run(info)) {
1014 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001015 } else {
Dan Williamsa310d032013-11-06 16:30:01 -08001016 stop_threaded_test(info);
1017 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001018 }
Dan Williamsa310d032013-11-06 16:30:01 -08001019 mutex_unlock(&info->lock);
1020
1021 return param_get_bool(val, kp);
1022}
1023
1024static int dmatest_run_set(const char *val, const struct kernel_param *kp)
1025{
1026 struct dmatest_info *info = &test_info;
1027 int ret;
1028
1029 mutex_lock(&info->lock);
1030 ret = param_set_bool(val, kp);
1031 if (ret) {
1032 mutex_unlock(&info->lock);
1033 return ret;
1034 }
1035
1036 if (is_threaded_test_run(info))
1037 ret = -EBUSY;
1038 else if (dmatest_run)
Dan Williamsa9e55492013-11-06 16:30:02 -08001039 restart_threaded_test(info, dmatest_run);
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001040
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001041 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001042
Dan Williamsa310d032013-11-06 16:30:01 -08001043 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001044}
1045
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001046static int __init dmatest_init(void)
1047{
1048 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -08001049 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001050
Dan Williamsa310d032013-11-06 16:30:01 -08001051 if (dmatest_run) {
1052 mutex_lock(&info->lock);
Dan Williamsa9e55492013-11-06 16:30:02 -08001053 run_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001054 mutex_unlock(&info->lock);
1055 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001056
Dan Williams2d88ce72013-11-06 16:30:09 -08001057 if (params->iterations && wait)
1058 wait_event(thread_wait, !is_threaded_test_run(info));
Andy Shevchenko838cc702013-03-04 11:09:28 +02001059
Dan Williamsa310d032013-11-06 16:30:01 -08001060 /* module parameters are stable, inittime tests are started,
1061 * let userspace take over 'run' control
1062 */
1063 info->did_init = true;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001064
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001065 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001066}
1067/* when compiled-in wait for drivers to load first */
1068late_initcall(dmatest_init);
1069
1070static void __exit dmatest_exit(void)
1071{
1072 struct dmatest_info *info = &test_info;
1073
Dan Williamsa310d032013-11-06 16:30:01 -08001074 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001075 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001076 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001077}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001078module_exit(dmatest_exit);
1079
Jean Delvaree05503e2011-05-18 16:49:24 +02001080MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001081MODULE_LICENSE("GPL v2");