blob: 6511928b4cdfe11c944223a8c7e3e8b0f8b9cc96 [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
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +010030static char test_device[32];
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030031module_param_string(device, test_device, sizeof(test_device),
32 S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070033MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
34
35static unsigned int threads_per_chan = 1;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030036module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070037MODULE_PARM_DESC(threads_per_chan,
38 "Number of threads to start per channel (default: 1)");
39
40static unsigned int max_channels;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030041module_param(max_channels, uint, S_IRUGO | S_IWUSR);
Dan Williams33df8ca2009-01-06 11:38:15 -070042MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070043 "Maximum number of channels to use (default: all)");
44
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020045static unsigned int iterations;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030046module_param(iterations, uint, S_IRUGO | S_IWUSR);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +020047MODULE_PARM_DESC(iterations,
48 "Iterations before stopping test (default: infinite)");
49
Eugeniy Paltsevd8646722016-09-14 20:40:38 +030050static unsigned int dmatest;
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053051module_param(dmatest, uint, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(dmatest,
Dave Jiangc678fa62017-08-21 10:23:13 -070053 "dmatest 0-memcpy 1-memset (default: 0)");
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +053054
Dan Williamsb54d5cb2009-03-25 09:13:25 -070055static unsigned int xor_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030056module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
Dan Williamsb54d5cb2009-03-25 09:13:25 -070057MODULE_PARM_DESC(xor_sources,
58 "Number of xor source buffers (default: 3)");
59
Dan Williams58691d62009-08-29 19:09:27 -070060static unsigned int pq_sources = 3;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030061module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
Dan Williams58691d62009-08-29 19:09:27 -070062MODULE_PARM_DESC(pq_sources,
63 "Number of p+q source buffers (default: 3)");
64
Viresh Kumard42efe62011-03-22 17:27:25 +053065static int timeout = 3000;
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +030066module_param(timeout, uint, S_IRUGO | S_IWUSR);
Joe Perches85ee7a12011-04-23 20:38:19 -070067MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
68 "Pass -1 for infinite timeout");
Viresh Kumard42efe62011-03-22 17:27:25 +053069
Dan Williamse3b9c342013-11-06 16:30:05 -080070static bool noverify;
71module_param(noverify, bool, S_IRUGO | S_IWUSR);
Yang Shunyong2e67a082018-02-02 17:51:09 +080072MODULE_PARM_DESC(noverify, "Disable data verification (default: verify)");
73
74static bool norandom;
75module_param(norandom, bool, 0644);
76MODULE_PARM_DESC(norandom, "Disable random offset setup (default: random)");
Andy Shevchenko74b5c072013-03-04 11:09:32 +020077
Dan Williams50137a72013-11-08 12:26:26 -080078static bool verbose;
79module_param(verbose, bool, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(verbose, "Enable \"success\" result messages (default: off)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070081
Seraj Alijana875abf2018-12-10 08:52:37 +000082static int alignment = -1;
83module_param(alignment, int, 0644);
84MODULE_PARM_DESC(alignment, "Custom data address alignment taken as 2^(alignment) (default: not used (-1))");
85
Seraj Alijan13396a12018-12-10 08:52:39 +000086static unsigned int transfer_size;
87module_param(transfer_size, uint, 0644);
88MODULE_PARM_DESC(transfer_size, "Optional custom transfer size in bytes (default: not used (0))");
89
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020090/**
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +020091 * struct dmatest_params - test parameters.
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +020092 * @buf_size: size of the memcpy test buffer
93 * @channel: bus ID of the channel to test
94 * @device: bus ID of the DMA Engine to test
95 * @threads_per_chan: number of threads to start per channel
96 * @max_channels: maximum number of channels to use
97 * @iterations: iterations before stopping test
98 * @xor_sources: number of xor source buffers
99 * @pq_sources: number of p+q source buffers
100 * @timeout: transfer timeout in msec, -1 for infinite timeout
101 */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200102struct dmatest_params {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200103 unsigned int buf_size;
104 char channel[20];
Guennadi Liakhovetskia85159f2013-12-30 14:58:04 +0100105 char device[32];
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200106 unsigned int threads_per_chan;
107 unsigned int max_channels;
108 unsigned int iterations;
109 unsigned int xor_sources;
110 unsigned int pq_sources;
111 int timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -0800112 bool noverify;
Yang Shunyong2e67a082018-02-02 17:51:09 +0800113 bool norandom;
Seraj Alijana875abf2018-12-10 08:52:37 +0000114 int alignment;
Seraj Alijan13396a12018-12-10 08:52:39 +0000115 unsigned int transfer_size;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200116};
117
118/**
119 * struct dmatest_info - test information.
120 * @params: test parameters
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200121 * @lock: access protection to the fields of this structure
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200122 */
Dan Williamsa310d032013-11-06 16:30:01 -0800123static struct dmatest_info {
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200124 /* Test parameters */
125 struct dmatest_params params;
Andy Shevchenko838cc702013-03-04 11:09:28 +0200126
127 /* Internal state */
128 struct list_head channels;
129 unsigned int nr_channels;
Andy Shevchenko851b7e12013-03-04 11:09:30 +0200130 struct mutex lock;
Dan Williamsa310d032013-11-06 16:30:01 -0800131 bool did_init;
132} test_info = {
133 .channels = LIST_HEAD_INIT(test_info.channels),
134 .lock = __MUTEX_INITIALIZER(test_info.lock),
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200135};
136
Dan Williamsa310d032013-11-06 16:30:01 -0800137static int dmatest_run_set(const char *val, const struct kernel_param *kp);
138static int dmatest_run_get(char *val, const struct kernel_param *kp);
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930139static const struct kernel_param_ops run_ops = {
Dan Williamsa310d032013-11-06 16:30:01 -0800140 .set = dmatest_run_set,
141 .get = dmatest_run_get,
142};
143static bool dmatest_run;
144module_param_cb(run, &run_ops, &dmatest_run, S_IRUGO | S_IWUSR);
145MODULE_PARM_DESC(run, "Run the test (default: false)");
146
Seraj Alijand53513d2018-12-10 08:52:31 +0000147static int dmatest_chan_set(const char *val, const struct kernel_param *kp);
148static int dmatest_chan_get(char *val, const struct kernel_param *kp);
149static const struct kernel_param_ops multi_chan_ops = {
150 .set = dmatest_chan_set,
151 .get = dmatest_chan_get,
152};
153
154static char test_channel[20];
155static struct kparam_string newchan_kps = {
156 .string = test_channel,
157 .maxlen = 20,
158};
159module_param_cb(channel, &multi_chan_ops, &newchan_kps, 0644);
160MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
161
162static int dmatest_test_list_get(char *val, const struct kernel_param *kp);
163static const struct kernel_param_ops test_list_ops = {
164 .get = dmatest_test_list_get,
165};
166module_param_cb(test_list, &test_list_ops, NULL, 0444);
167MODULE_PARM_DESC(test_list, "Print current test list");
168
Dan Williamsa310d032013-11-06 16:30:01 -0800169/* Maximum amount of mismatched bytes in buffer to print */
170#define MAX_ERROR_COUNT 32
171
172/*
173 * Initialization patterns. All bytes in the source buffer has bit 7
174 * set, all bytes in the destination buffer has bit 7 cleared.
175 *
176 * Bit 6 is set for all bytes which are to be copied by the DMA
177 * engine. Bit 5 is set for all bytes which are to be overwritten by
178 * the DMA engine.
179 *
180 * The remaining bits are the inverse of a counter which increments by
181 * one for each byte address.
182 */
183#define PATTERN_SRC 0x80
184#define PATTERN_DST 0x00
185#define PATTERN_COPY 0x40
186#define PATTERN_OVERWRITE 0x20
187#define PATTERN_COUNT_MASK 0x1f
Sinan Kaya61b5f542017-06-29 22:30:58 -0400188#define PATTERN_MEMSET_IDX 0x01
Dan Williamsa310d032013-11-06 16:30:01 -0800189
Seraj Alijan6138f962018-12-10 08:52:34 +0000190/* Fixed point arithmetic ops */
191#define FIXPT_SHIFT 8
192#define FIXPNT_MASK 0xFF
193#define FIXPT_TO_INT(a) ((a) >> FIXPT_SHIFT)
194#define INT_TO_FIXPT(a) ((a) << FIXPT_SHIFT)
195#define FIXPT_GET_FRAC(a) ((((a) & FIXPNT_MASK) * 100) >> FIXPT_SHIFT)
196
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500197/* poor man's completion - we want to use wait_event_freezable() on it */
198struct dmatest_done {
199 bool done;
200 wait_queue_head_t *wait;
201};
202
Dan Williamsa310d032013-11-06 16:30:01 -0800203struct dmatest_thread {
204 struct list_head node;
205 struct dmatest_info *info;
206 struct task_struct *task;
207 struct dma_chan *chan;
208 u8 **srcs;
Dave Jiangd6481602016-11-29 13:22:20 -0700209 u8 **usrcs;
Dan Williamsa310d032013-11-06 16:30:01 -0800210 u8 **dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700211 u8 **udsts;
Dan Williamsa310d032013-11-06 16:30:01 -0800212 enum dma_transaction_type type;
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500213 wait_queue_head_t done_wait;
214 struct dmatest_done test_done;
Dan Williamsa310d032013-11-06 16:30:01 -0800215 bool done;
Seraj Alijand53513d2018-12-10 08:52:31 +0000216 bool pending;
Dan Williamsa310d032013-11-06 16:30:01 -0800217};
218
219struct dmatest_chan {
220 struct list_head node;
221 struct dma_chan *chan;
222 struct list_head threads;
223};
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200224
Dan Williams2d88ce72013-11-06 16:30:09 -0800225static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
226static bool wait;
227
228static bool is_threaded_test_run(struct dmatest_info *info)
229{
230 struct dmatest_chan *dtc;
231
232 list_for_each_entry(dtc, &info->channels, node) {
233 struct dmatest_thread *thread;
234
235 list_for_each_entry(thread, &dtc->threads, node) {
236 if (!thread->done)
237 return true;
238 }
239 }
240
241 return false;
242}
243
Seraj Alijand53513d2018-12-10 08:52:31 +0000244static bool is_threaded_test_pending(struct dmatest_info *info)
245{
246 struct dmatest_chan *dtc;
247
248 list_for_each_entry(dtc, &info->channels, node) {
249 struct dmatest_thread *thread;
250
251 list_for_each_entry(thread, &dtc->threads, node) {
252 if (thread->pending)
253 return true;
254 }
255 }
256
257 return false;
258}
259
Dan Williams2d88ce72013-11-06 16:30:09 -0800260static int dmatest_wait_get(char *val, const struct kernel_param *kp)
261{
262 struct dmatest_info *info = &test_info;
263 struct dmatest_params *params = &info->params;
264
265 if (params->iterations)
266 wait_event(thread_wait, !is_threaded_test_run(info));
267 wait = true;
268 return param_get_bool(val, kp);
269}
270
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930271static const struct kernel_param_ops wait_ops = {
Dan Williams2d88ce72013-11-06 16:30:09 -0800272 .get = dmatest_wait_get,
273 .set = param_set_bool,
274};
275module_param_cb(wait, &wait_ops, &wait, S_IRUGO);
276MODULE_PARM_DESC(wait, "Wait for tests to complete (default: false)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700277
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200278static bool dmatest_match_channel(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200279 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700280{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200281 if (params->channel[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700282 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200283 return strcmp(dma_chan_name(chan), params->channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700284}
285
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200286static bool dmatest_match_device(struct dmatest_params *params,
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200287 struct dma_device *device)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700288{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200289 if (params->device[0] == '\0')
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700290 return true;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200291 return strcmp(dev_name(device->dev), params->device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700292}
293
294static unsigned long dmatest_random(void)
295{
296 unsigned long buf;
297
Dan Williamsbe9fa5a2013-11-06 16:30:03 -0800298 prandom_bytes(&buf, sizeof(buf));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700299 return buf;
300}
301
Sinan Kaya61b5f542017-06-29 22:30:58 -0400302static inline u8 gen_inv_idx(u8 index, bool is_memset)
303{
304 u8 val = is_memset ? PATTERN_MEMSET_IDX : index;
305
306 return ~val & PATTERN_COUNT_MASK;
307}
308
309static inline u8 gen_src_value(u8 index, bool is_memset)
310{
311 return PATTERN_SRC | gen_inv_idx(index, is_memset);
312}
313
314static inline u8 gen_dst_value(u8 index, bool is_memset)
315{
316 return PATTERN_DST | gen_inv_idx(index, is_memset);
317}
318
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200319static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400320 unsigned int buf_size, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700321{
322 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700323 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700324
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700325 for (; (buf = *bufs); bufs++) {
326 for (i = 0; i < start; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400327 buf[i] = gen_src_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700328 for ( ; i < start + len; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400329 buf[i] = gen_src_value(i, is_memset) | PATTERN_COPY;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200330 for ( ; i < buf_size; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400331 buf[i] = gen_src_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700332 buf++;
333 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700334}
335
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200336static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400337 unsigned int buf_size, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700338{
339 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700340 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700341
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700342 for (; (buf = *bufs); bufs++) {
343 for (i = 0; i < start; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400344 buf[i] = gen_dst_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700345 for ( ; i < start + len; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400346 buf[i] = gen_dst_value(i, is_memset) |
347 PATTERN_OVERWRITE;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200348 for ( ; i < buf_size; i++)
Sinan Kaya61b5f542017-06-29 22:30:58 -0400349 buf[i] = gen_dst_value(i, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700350 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700351}
352
Dan Williams7b610172013-11-06 16:29:57 -0800353static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400354 unsigned int counter, bool is_srcbuf, bool is_memset)
Dan Williams7b610172013-11-06 16:29:57 -0800355{
356 u8 diff = actual ^ pattern;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400357 u8 expected = pattern | gen_inv_idx(counter, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800358 const char *thread_name = current->comm;
359
360 if (is_srcbuf)
361 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
362 thread_name, index, expected, actual);
363 else if ((pattern & PATTERN_COPY)
364 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
365 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
366 thread_name, index, expected, actual);
367 else if (diff & PATTERN_SRC)
368 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
369 thread_name, index, expected, actual);
370 else
371 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
372 thread_name, index, expected, actual);
373}
374
375static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
376 unsigned int end, unsigned int counter, u8 pattern,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400377 bool is_srcbuf, bool is_memset)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700378{
379 unsigned int i;
380 unsigned int error_count = 0;
381 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700382 u8 expected;
383 u8 *buf;
384 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700385
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700386 for (; (buf = *bufs); bufs++) {
387 counter = counter_orig;
388 for (i = start; i < end; i++) {
389 actual = buf[i];
Sinan Kaya61b5f542017-06-29 22:30:58 -0400390 expected = pattern | gen_inv_idx(counter, is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700391 if (actual != expected) {
Dan Williams7b610172013-11-06 16:29:57 -0800392 if (error_count < MAX_ERROR_COUNT)
393 dmatest_mismatch(actual, pattern, i,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400394 counter, is_srcbuf,
395 is_memset);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700396 error_count++;
397 }
398 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700399 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700400 }
401
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200402 if (error_count > MAX_ERROR_COUNT)
Dan Williams7b610172013-11-06 16:29:57 -0800403 pr_warn("%s: %u errors suppressed\n",
Andy Shevchenko74b5c072013-03-04 11:09:32 +0200404 current->comm, error_count - MAX_ERROR_COUNT);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700405
406 return error_count;
407}
408
Tejun Heoadfa5432011-11-23 09:28:16 -0800409
410static void dmatest_callback(void *arg)
Dan Williamse44e0aa2009-03-25 09:13:25 -0700411{
Tejun Heoadfa5432011-11-23 09:28:16 -0800412 struct dmatest_done *done = arg;
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500413 struct dmatest_thread *thread =
Yang Shunyong66b3bd22018-01-29 14:40:11 +0800414 container_of(done, struct dmatest_thread, test_done);
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500415 if (!thread->done) {
416 done->done = true;
417 wake_up_all(done->wait);
418 } else {
419 /*
420 * If thread->done, it means that this callback occurred
421 * after the parent thread has cleaned up. This can
422 * happen in the case that driver doesn't implement
423 * the terminate_all() functionality and a dma operation
424 * did not occur within the timeout period
425 */
426 WARN(1, "dmatest: Kernel memory may be corrupted!!\n");
427 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700428}
429
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900430static unsigned int min_odd(unsigned int x, unsigned int y)
431{
432 unsigned int val = min(x, y);
433
434 return val % 2 ? val : val - 1;
435}
436
Dan Williams872f05c2013-11-06 16:29:58 -0800437static void result(const char *err, unsigned int n, unsigned int src_off,
438 unsigned int dst_off, unsigned int len, unsigned long data)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200439{
Jerome Blin2acec152014-03-04 10:38:55 +0100440 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 -0800441 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200442}
443
Dan Williams872f05c2013-11-06 16:29:58 -0800444static void dbg_result(const char *err, unsigned int n, unsigned int src_off,
445 unsigned int dst_off, unsigned int len,
446 unsigned long data)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200447{
Jerome Blin2acec152014-03-04 10:38:55 +0100448 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 +0300449 current->comm, n, err, src_off, dst_off, len, data);
Andy Shevchenko95019c82013-03-04 11:09:33 +0200450}
451
Andy Shevchenkoa835bb82014-10-22 16:16:42 +0300452#define verbose_result(err, n, src_off, dst_off, len, data) ({ \
453 if (verbose) \
454 result(err, n, src_off, dst_off, len, data); \
455 else \
456 dbg_result(err, n, src_off, dst_off, len, data);\
Dan Williams50137a72013-11-08 12:26:26 -0800457})
458
Dan Williams86727442013-11-06 16:30:07 -0800459static unsigned long long dmatest_persec(s64 runtime, unsigned int val)
Andy Shevchenko95019c82013-03-04 11:09:33 +0200460{
Dan Williams86727442013-11-06 16:30:07 -0800461 unsigned long long per_sec = 1000000;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200462
Dan Williams86727442013-11-06 16:30:07 -0800463 if (runtime <= 0)
464 return 0;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200465
Dan Williams86727442013-11-06 16:30:07 -0800466 /* drop precision until runtime is 32-bits */
467 while (runtime > UINT_MAX) {
468 runtime >>= 1;
469 per_sec <<= 1;
470 }
Andy Shevchenko95019c82013-03-04 11:09:33 +0200471
Dan Williams86727442013-11-06 16:30:07 -0800472 per_sec *= val;
Seraj Alijan6138f962018-12-10 08:52:34 +0000473 per_sec = INT_TO_FIXPT(per_sec);
Dan Williams86727442013-11-06 16:30:07 -0800474 do_div(per_sec, runtime);
Seraj Alijan6138f962018-12-10 08:52:34 +0000475
Dan Williams86727442013-11-06 16:30:07 -0800476 return per_sec;
Andy Shevchenko95019c82013-03-04 11:09:33 +0200477}
478
Dan Williams86727442013-11-06 16:30:07 -0800479static unsigned long long dmatest_KBs(s64 runtime, unsigned long long len)
Andy Shevchenkod86b2f22013-03-04 11:09:34 +0200480{
Seraj Alijan6138f962018-12-10 08:52:34 +0000481 return FIXPT_TO_INT(dmatest_persec(runtime, len >> 10));
Andy Shevchenko95019c82013-03-04 11:09:33 +0200482}
483
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700484/*
485 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700486 * offsets for a given operation type until it is told to exit by
487 * kthread_stop(). There may be multiple threads running this function
488 * in parallel for a single channel, and there may be multiple channels
489 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700490 *
491 * Before each test, the source and destination buffer is initialized
492 * with a known pattern. This pattern is different depending on
493 * whether it's in an area which is supposed to be copied or
494 * overwritten, and different in the source and destination buffers.
495 * So if the DMA engine doesn't copy exactly what we tell it to copy,
496 * we'll notice.
497 */
498static int dmatest_func(void *data)
499{
500 struct dmatest_thread *thread = data;
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500501 struct dmatest_done *done = &thread->test_done;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200502 struct dmatest_info *info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200503 struct dmatest_params *params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700504 struct dma_chan *chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900505 struct dma_device *dev;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700506 unsigned int error_count;
507 unsigned int failed_tests = 0;
508 unsigned int total_tests = 0;
509 dma_cookie_t cookie;
510 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700511 enum dma_ctrl_flags flags;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200512 u8 *pq_coefs = NULL;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700513 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700514 int src_cnt;
515 int dst_cnt;
516 int i;
Sinan Kayae9405ef2016-09-01 10:02:55 -0400517 ktime_t ktime, start, diff;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100518 ktime_t filltime = 0;
519 ktime_t comparetime = 0;
Dan Williams86727442013-11-06 16:30:07 -0800520 s64 runtime = 0;
521 unsigned long long total_len = 0;
Seraj Alijan6138f962018-12-10 08:52:34 +0000522 unsigned long long iops = 0;
Dave Jiangd6481602016-11-29 13:22:20 -0700523 u8 align = 0;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400524 bool is_memset = false;
Laura Abbott72ef08b2018-04-10 18:02:16 -0700525 dma_addr_t *srcs;
526 dma_addr_t *dma_pq;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700527
Tejun Heoadfa5432011-11-23 09:28:16 -0800528 set_freezable();
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700529
530 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700531
532 smp_rmb();
Seraj Alijand53513d2018-12-10 08:52:31 +0000533 thread->pending = false;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200534 info = thread->info;
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200535 params = &info->params;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700536 chan = thread->chan;
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900537 dev = chan->device;
Dave Jiangd6481602016-11-29 13:22:20 -0700538 if (thread->type == DMA_MEMCPY) {
Seraj Alijana875abf2018-12-10 08:52:37 +0000539 align = params->alignment < 0 ? dev->copy_align :
540 params->alignment;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700541 src_cnt = dst_cnt = 1;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400542 } else if (thread->type == DMA_MEMSET) {
Seraj Alijana875abf2018-12-10 08:52:37 +0000543 align = params->alignment < 0 ? dev->fill_align :
544 params->alignment;
Sinan Kaya61b5f542017-06-29 22:30:58 -0400545 src_cnt = dst_cnt = 1;
546 is_memset = true;
Dave Jiangd6481602016-11-29 13:22:20 -0700547 } else if (thread->type == DMA_XOR) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900548 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200549 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700550 dst_cnt = 1;
Seraj Alijana875abf2018-12-10 08:52:37 +0000551 align = params->alignment < 0 ? dev->xor_align :
552 params->alignment;
Dan Williams58691d62009-08-29 19:09:27 -0700553 } else if (thread->type == DMA_PQ) {
Akinobu Mita8be9e32b2012-10-28 00:49:32 +0900554 /* force odd to ensure dst = src */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200555 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
Dan Williams58691d62009-08-29 19:09:27 -0700556 dst_cnt = 2;
Seraj Alijana875abf2018-12-10 08:52:37 +0000557 align = params->alignment < 0 ? dev->pq_align :
558 params->alignment;
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200559
Dave Jiang31d18252016-11-29 13:22:01 -0700560 pq_coefs = kmalloc(params->pq_sources + 1, GFP_KERNEL);
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200561 if (!pq_coefs)
562 goto err_thread_type;
563
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100564 for (i = 0; i < src_cnt; i++)
Dan Williams58691d62009-08-29 19:09:27 -0700565 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700566 } else
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200567 goto err_thread_type;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700568
Alexandru Ardelean787d3082018-11-01 18:07:16 +0200569 /* Check if buffer count fits into map count variable (u8) */
570 if ((src_cnt + dst_cnt) >= 255) {
571 pr_err("too many buffers (%d of 255 supported)\n",
572 src_cnt + dst_cnt);
Dan Carpenter3f3c7552018-12-03 17:49:33 +0300573 goto err_free_coefs;
Alexandru Ardelean787d3082018-11-01 18:07:16 +0200574 }
575
576 if (1 << align > params->buf_size) {
577 pr_err("%u-byte buffer too small for %d-byte alignment\n",
578 params->buf_size, 1 << align);
Dan Carpenter3f3c7552018-12-03 17:49:33 +0300579 goto err_free_coefs;
Alexandru Ardelean787d3082018-11-01 18:07:16 +0200580 }
581
Dave Jiang31d18252016-11-29 13:22:01 -0700582 thread->srcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700583 if (!thread->srcs)
Dan Carpenter3f3c7552018-12-03 17:49:33 +0300584 goto err_free_coefs;
Dave Jiangd6481602016-11-29 13:22:20 -0700585
586 thread->usrcs = kcalloc(src_cnt + 1, sizeof(u8 *), GFP_KERNEL);
587 if (!thread->usrcs)
588 goto err_usrcs;
589
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700590 for (i = 0; i < src_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700591 thread->usrcs[i] = kmalloc(params->buf_size + align,
592 GFP_KERNEL);
593 if (!thread->usrcs[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700594 goto err_srcbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700595
596 /* align srcs to alignment restriction */
597 if (align)
598 thread->srcs[i] = PTR_ALIGN(thread->usrcs[i], align);
599 else
600 thread->srcs[i] = thread->usrcs[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700601 }
602 thread->srcs[i] = NULL;
603
Dave Jiang31d18252016-11-29 13:22:01 -0700604 thread->dsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700605 if (!thread->dsts)
606 goto err_dsts;
Dave Jiangd6481602016-11-29 13:22:20 -0700607
608 thread->udsts = kcalloc(dst_cnt + 1, sizeof(u8 *), GFP_KERNEL);
609 if (!thread->udsts)
610 goto err_udsts;
611
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700612 for (i = 0; i < dst_cnt; i++) {
Dave Jiangd6481602016-11-29 13:22:20 -0700613 thread->udsts[i] = kmalloc(params->buf_size + align,
614 GFP_KERNEL);
615 if (!thread->udsts[i])
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700616 goto err_dstbuf;
Dave Jiangd6481602016-11-29 13:22:20 -0700617
618 /* align dsts to alignment restriction */
619 if (align)
620 thread->dsts[i] = PTR_ALIGN(thread->udsts[i], align);
621 else
622 thread->dsts[i] = thread->udsts[i];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700623 }
624 thread->dsts[i] = NULL;
625
Dan Williamse44e0aa2009-03-25 09:13:25 -0700626 set_user_nice(current, 10);
627
Laura Abbott72ef08b2018-04-10 18:02:16 -0700628 srcs = kcalloc(src_cnt, sizeof(dma_addr_t), GFP_KERNEL);
629 if (!srcs)
630 goto err_dstbuf;
631
632 dma_pq = kcalloc(dst_cnt, sizeof(dma_addr_t), GFP_KERNEL);
633 if (!dma_pq)
634 goto err_srcs_array;
635
Ira Snyderb203bd32011-03-03 07:54:53 +0000636 /*
Bartlomiej Zolnierkiewiczd1cab342013-10-18 19:35:21 +0200637 * src and dst buffers are freed by ourselves below
Ira Snyderb203bd32011-03-03 07:54:53 +0000638 */
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +0200639 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700640
Dan Williams86727442013-11-06 16:30:07 -0800641 ktime = ktime_get();
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200642 while (!kthread_should_stop()
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200643 && !(params->iterations && total_tests >= params->iterations)) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700644 struct dma_async_tx_descriptor *tx = NULL;
Dan Williams4076e752013-11-06 16:30:10 -0800645 struct dmaengine_unmap_data *um;
Dan Williams4076e752013-11-06 16:30:10 -0800646 dma_addr_t *dsts;
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300647 unsigned int src_off, dst_off, len;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700648
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700649 total_tests++;
650
Seraj Alijan13396a12018-12-10 08:52:39 +0000651 if (params->transfer_size) {
652 if (params->transfer_size >= params->buf_size) {
653 pr_err("%u-byte transfer size must be lower than %u-buffer size\n",
654 params->transfer_size, params->buf_size);
655 break;
656 }
657 len = params->transfer_size;
658 } else if (params->norandom) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800659 len = params->buf_size;
Seraj Alijan13396a12018-12-10 08:52:39 +0000660 } else {
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300661 len = dmatest_random() % params->buf_size + 1;
Seraj Alijan13396a12018-12-10 08:52:39 +0000662 }
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300663
Seraj Alijan13396a12018-12-10 08:52:39 +0000664 /* Do not alter transfer size explicitly defined by user */
665 if (!params->transfer_size) {
666 len = (len >> align) << align;
667 if (!len)
668 len = 1 << align;
669 }
Andy Shevchenkoede23a52014-10-22 16:16:43 +0300670 total_len += len;
671
Yang Shunyong2e67a082018-02-02 17:51:09 +0800672 if (params->norandom) {
Dan Williamse3b9c342013-11-06 16:30:05 -0800673 src_off = 0;
674 dst_off = 0;
675 } else {
Dan Williamse3b9c342013-11-06 16:30:05 -0800676 src_off = dmatest_random() % (params->buf_size - len + 1);
677 dst_off = dmatest_random() % (params->buf_size - len + 1);
678
679 src_off = (src_off >> align) << align;
680 dst_off = (dst_off >> align) << align;
Yang Shunyong2e67a082018-02-02 17:51:09 +0800681 }
Dan Williamse3b9c342013-11-06 16:30:05 -0800682
Yang Shunyong2e67a082018-02-02 17:51:09 +0800683 if (!params->noverify) {
684 start = ktime_get();
Dan Williamse3b9c342013-11-06 16:30:05 -0800685 dmatest_init_srcs(thread->srcs, src_off, len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400686 params->buf_size, is_memset);
Dan Williamse3b9c342013-11-06 16:30:05 -0800687 dmatest_init_dsts(thread->dsts, dst_off, len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400688 params->buf_size, is_memset);
Sinan Kayae9405ef2016-09-01 10:02:55 -0400689
690 diff = ktime_sub(ktime_get(), start);
691 filltime = ktime_add(filltime, diff);
Dan Williamse3b9c342013-11-06 16:30:05 -0800692 }
693
Dave Jiang31d18252016-11-29 13:22:01 -0700694 um = dmaengine_get_unmap_data(dev->dev, src_cnt + dst_cnt,
Dan Williams4076e752013-11-06 16:30:10 -0800695 GFP_KERNEL);
696 if (!um) {
697 failed_tests++;
698 result("unmap data NULL", total_tests,
699 src_off, dst_off, len, ret);
700 continue;
701 }
Dan Williams83544ae2009-09-08 17:42:53 -0700702
Dan Williams4076e752013-11-06 16:30:10 -0800703 um->len = params->buf_size;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700704 for (i = 0; i < src_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800705 void *buf = thread->srcs[i];
Dan Williams4076e752013-11-06 16:30:10 -0800706 struct page *pg = virt_to_page(buf);
Geliang Tangf62e5f62017-04-22 09:18:03 +0800707 unsigned long pg_off = offset_in_page(buf);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700708
Dan Williams4076e752013-11-06 16:30:10 -0800709 um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
710 um->len, DMA_TO_DEVICE);
711 srcs[i] = um->addr[i] + src_off;
712 ret = dma_mapping_error(dev->dev, um->addr[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800713 if (ret) {
Dan Williams872f05c2013-11-06 16:29:58 -0800714 result("src mapping error", total_tests,
715 src_off, dst_off, len, ret);
Andy Shevchenko64543682019-01-30 21:48:44 +0200716 goto error_unmap_continue;
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800717 }
Dan Williams4076e752013-11-06 16:30:10 -0800718 um->to_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700719 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700720 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williams4076e752013-11-06 16:30:10 -0800721 dsts = &um->addr[src_cnt];
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700722 for (i = 0; i < dst_cnt; i++) {
Dan Williams745c00d2013-12-09 11:16:01 -0800723 void *buf = thread->dsts[i];
Dan Williams4076e752013-11-06 16:30:10 -0800724 struct page *pg = virt_to_page(buf);
Geliang Tangf62e5f62017-04-22 09:18:03 +0800725 unsigned long pg_off = offset_in_page(buf);
Dan Williams4076e752013-11-06 16:30:10 -0800726
727 dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
728 DMA_BIDIRECTIONAL);
729 ret = dma_mapping_error(dev->dev, dsts[i]);
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800730 if (ret) {
Dan Williams872f05c2013-11-06 16:29:58 -0800731 result("dst mapping error", total_tests,
732 src_off, dst_off, len, ret);
Andy Shevchenko64543682019-01-30 21:48:44 +0200733 goto error_unmap_continue;
Andy Shevchenkoafde3be2012-12-17 15:59:53 -0800734 }
Dan Williams4076e752013-11-06 16:30:10 -0800735 um->bidi_cnt++;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700736 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700737
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700738 if (thread->type == DMA_MEMCPY)
739 tx = dev->device_prep_dma_memcpy(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800740 dsts[0] + dst_off,
741 srcs[0], len, flags);
Sinan Kaya61b5f542017-06-29 22:30:58 -0400742 else if (thread->type == DMA_MEMSET)
743 tx = dev->device_prep_dma_memset(chan,
744 dsts[0] + dst_off,
745 *(thread->srcs[0] + src_off),
746 len, flags);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700747 else if (thread->type == DMA_XOR)
748 tx = dev->device_prep_dma_xor(chan,
Dan Williams4076e752013-11-06 16:30:10 -0800749 dsts[0] + dst_off,
750 srcs, src_cnt,
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700751 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700752 else if (thread->type == DMA_PQ) {
Dan Williams58691d62009-08-29 19:09:27 -0700753 for (i = 0; i < dst_cnt; i++)
Dan Williams4076e752013-11-06 16:30:10 -0800754 dma_pq[i] = dsts[i] + dst_off;
755 tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
Anatolij Gustschin94de6482010-02-15 22:35:23 +0100756 src_cnt, pq_coefs,
Dan Williams58691d62009-08-29 19:09:27 -0700757 len, flags);
758 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700759
Atsushi Nemotod86be862009-01-13 09:22:20 -0700760 if (!tx) {
Dan Williams872f05c2013-11-06 16:29:58 -0800761 result("prep error", total_tests, src_off,
762 dst_off, len, ret);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700763 msleep(100);
Andy Shevchenko64543682019-01-30 21:48:44 +0200764 goto error_unmap_continue;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700765 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700766
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500767 done->done = false;
Dan Williamse44e0aa2009-03-25 09:13:25 -0700768 tx->callback = dmatest_callback;
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500769 tx->callback_param = done;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700770 cookie = tx->tx_submit(tx);
771
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700772 if (dma_submit_error(cookie)) {
Dan Williams872f05c2013-11-06 16:29:58 -0800773 result("submit error", total_tests, src_off,
774 dst_off, len, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700775 msleep(100);
Andy Shevchenko64543682019-01-30 21:48:44 +0200776 goto error_unmap_continue;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700777 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700778 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700779
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500780 wait_event_freezable_timeout(thread->done_wait, done->done,
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200781 msecs_to_jiffies(params->timeout));
Guennadi Liakhovetski981ed702011-08-18 16:50:51 +0200782
Dan Williamse44e0aa2009-03-25 09:13:25 -0700783 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700784
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500785 if (!done->done) {
Dan Williams872f05c2013-11-06 16:29:58 -0800786 result("test timed out", total_tests, src_off, dst_off,
787 len, 0);
Andy Shevchenko64543682019-01-30 21:48:44 +0200788 goto error_unmap_continue;
Vinod Koul19e9f992013-10-16 13:37:27 +0530789 } else if (status != DMA_COMPLETE) {
Dan Williams872f05c2013-11-06 16:29:58 -0800790 result(status == DMA_ERROR ?
791 "completion error status" :
792 "completion busy status", total_tests, src_off,
793 dst_off, len, ret);
Andy Shevchenko64543682019-01-30 21:48:44 +0200794 goto error_unmap_continue;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700795 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700796
Andy Shevchenko64543682019-01-30 21:48:44 +0200797 dmaengine_unmap_put(um);
798
Dan Williamse3b9c342013-11-06 16:30:05 -0800799 if (params->noverify) {
Dan Williams50137a72013-11-08 12:26:26 -0800800 verbose_result("test passed", total_tests, src_off,
801 dst_off, len, 0);
Dan Williamse3b9c342013-11-06 16:30:05 -0800802 continue;
803 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700804
Sinan Kayae9405ef2016-09-01 10:02:55 -0400805 start = ktime_get();
Dan Williams872f05c2013-11-06 16:29:58 -0800806 pr_debug("%s: verifying source buffer...\n", current->comm);
Dan Williamse3b9c342013-11-06 16:30:05 -0800807 error_count = dmatest_verify(thread->srcs, 0, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400808 0, PATTERN_SRC, true, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800809 error_count += dmatest_verify(thread->srcs, src_off,
810 src_off + len, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400811 PATTERN_SRC | PATTERN_COPY, true, is_memset);
Dan Williams7b610172013-11-06 16:29:57 -0800812 error_count += dmatest_verify(thread->srcs, src_off + len,
813 params->buf_size, src_off + len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400814 PATTERN_SRC, true, is_memset);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700815
Dan Williams872f05c2013-11-06 16:29:58 -0800816 pr_debug("%s: verifying dest buffer...\n", current->comm);
Dan Williams7b610172013-11-06 16:29:57 -0800817 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400818 0, PATTERN_DST, false, is_memset);
819
Dan Williams7b610172013-11-06 16:29:57 -0800820 error_count += dmatest_verify(thread->dsts, dst_off,
821 dst_off + len, src_off,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400822 PATTERN_SRC | PATTERN_COPY, false, is_memset);
823
Dan Williams7b610172013-11-06 16:29:57 -0800824 error_count += dmatest_verify(thread->dsts, dst_off + len,
825 params->buf_size, dst_off + len,
Sinan Kaya61b5f542017-06-29 22:30:58 -0400826 PATTERN_DST, false, is_memset);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700827
Sinan Kayae9405ef2016-09-01 10:02:55 -0400828 diff = ktime_sub(ktime_get(), start);
829 comparetime = ktime_add(comparetime, diff);
830
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700831 if (error_count) {
Dan Williams872f05c2013-11-06 16:29:58 -0800832 result("data error", total_tests, src_off, dst_off,
833 len, error_count);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700834 failed_tests++;
835 } else {
Dan Williams50137a72013-11-08 12:26:26 -0800836 verbose_result("test passed", total_tests, src_off,
837 dst_off, len, 0);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700838 }
Andy Shevchenko64543682019-01-30 21:48:44 +0200839
840 continue;
841
842error_unmap_continue:
843 dmaengine_unmap_put(um);
844 failed_tests++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700845 }
Sinan Kayae9405ef2016-09-01 10:02:55 -0400846 ktime = ktime_sub(ktime_get(), ktime);
847 ktime = ktime_sub(ktime, comparetime);
848 ktime = ktime_sub(ktime, filltime);
849 runtime = ktime_to_us(ktime);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700850
851 ret = 0;
Laura Abbott72ef08b2018-04-10 18:02:16 -0700852 kfree(dma_pq);
853err_srcs_array:
854 kfree(srcs);
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300855err_dstbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700856 for (i = 0; thread->udsts[i]; i++)
857 kfree(thread->udsts[i]);
858 kfree(thread->udsts);
859err_udsts:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700860 kfree(thread->dsts);
861err_dsts:
Andy Shevchenko8e1f50d2014-08-22 15:19:44 +0300862err_srcbuf:
Dave Jiangd6481602016-11-29 13:22:20 -0700863 for (i = 0; thread->usrcs[i]; i++)
864 kfree(thread->usrcs[i]);
865 kfree(thread->usrcs);
866err_usrcs:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700867 kfree(thread->srcs);
Dan Carpenter3f3c7552018-12-03 17:49:33 +0300868err_free_coefs:
Andy Shevchenko945b5af2013-03-04 11:09:26 +0200869 kfree(pq_coefs);
870err_thread_type:
Seraj Alijan6138f962018-12-10 08:52:34 +0000871 iops = dmatest_persec(runtime, total_tests);
872 pr_info("%s: summary %u tests, %u failures %llu.%02llu iops %llu KB/s (%d)\n",
Dan Williams86727442013-11-06 16:30:07 -0800873 current->comm, total_tests, failed_tests,
Seraj Alijan6138f962018-12-10 08:52:34 +0000874 FIXPT_TO_INT(iops), FIXPT_GET_FRAC(iops),
Dan Williams86727442013-11-06 16:30:07 -0800875 dmatest_KBs(runtime, total_len), ret);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200876
Viresh Kumar9704efa2011-07-29 16:21:57 +0530877 /* terminate all transfers on specified channels */
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500878 if (ret || failed_tests)
Alexandru Ardeleanfbffb6b2018-10-29 11:23:36 +0200879 dmaengine_terminate_sync(chan);
Shiraz Hashim5e034f72012-11-09 15:26:29 +0000880
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +0200881 thread->done = true;
Dan Williams2d88ce72013-11-06 16:30:09 -0800882 wake_up(&thread_wait);
Nicolas Ferre0a2ff57d2009-07-03 19:26:51 +0200883
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700884 return ret;
885}
886
887static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
888{
889 struct dmatest_thread *thread;
890 struct dmatest_thread *_thread;
891 int ret;
892
893 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
894 ret = kthread_stop(thread->task);
Dan Williams0adff802013-11-06 16:30:00 -0800895 pr_debug("thread %s exited with status %d\n",
896 thread->task->comm, ret);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700897 list_del(&thread->node);
Dan Williams2d88ce72013-11-06 16:30:09 -0800898 put_task_struct(thread->task);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700899 kfree(thread);
900 }
Viresh Kumar9704efa2011-07-29 16:21:57 +0530901
902 /* terminate all transfers on specified channels */
Alexandru Ardeleanfbffb6b2018-10-29 11:23:36 +0200903 dmaengine_terminate_sync(dtc->chan);
Viresh Kumar9704efa2011-07-29 16:21:57 +0530904
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700905 kfree(dtc);
906}
907
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200908static int dmatest_add_threads(struct dmatest_info *info,
909 struct dmatest_chan *dtc, enum dma_transaction_type type)
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700910{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200911 struct dmatest_params *params = &info->params;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700912 struct dmatest_thread *thread;
913 struct dma_chan *chan = dtc->chan;
914 char *op;
915 unsigned int i;
916
917 if (type == DMA_MEMCPY)
918 op = "copy";
Sinan Kaya61b5f542017-06-29 22:30:58 -0400919 else if (type == DMA_MEMSET)
920 op = "set";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700921 else if (type == DMA_XOR)
922 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700923 else if (type == DMA_PQ)
924 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700925 else
926 return -EINVAL;
927
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +0200928 for (i = 0; i < params->threads_per_chan; i++) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700929 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
930 if (!thread) {
Dan Williams0adff802013-11-06 16:30:00 -0800931 pr_warn("No memory for %s-%s%u\n",
932 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700933 break;
934 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200935 thread->info = info;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700936 thread->chan = dtc->chan;
937 thread->type = type;
Adam Wallis6f6a23a2017-11-27 10:45:01 -0500938 thread->test_done.wait = &thread->done_wait;
939 init_waitqueue_head(&thread->done_wait);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700940 smp_wmb();
Dan Williams2d88ce72013-11-06 16:30:09 -0800941 thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700942 dma_chan_name(chan), op, i);
943 if (IS_ERR(thread->task)) {
Dan Williams2d88ce72013-11-06 16:30:09 -0800944 pr_warn("Failed to create thread %s-%s%u\n",
Dan Williams0adff802013-11-06 16:30:00 -0800945 dma_chan_name(chan), op, i);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700946 kfree(thread);
947 break;
948 }
949
950 /* srcbuf and dstbuf are allocated by the thread itself */
Dan Williams2d88ce72013-11-06 16:30:09 -0800951 get_task_struct(thread->task);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700952 list_add_tail(&thread->node, &dtc->threads);
Seraj Alijand53513d2018-12-10 08:52:31 +0000953 thread->pending = true;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700954 }
955
956 return i;
957}
958
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200959static int dmatest_add_channel(struct dmatest_info *info,
960 struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700961{
962 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700963 struct dma_device *dma_dev = chan->device;
964 unsigned int thread_count = 0;
Kulikov Vasiliyb9033e62010-07-17 19:19:48 +0400965 int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700966
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700967 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700968 if (!dtc) {
Dan Williams0adff802013-11-06 16:30:00 -0800969 pr_warn("No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700970 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700971 }
972
973 dtc->chan = chan;
974 INIT_LIST_HEAD(&dtc->threads);
975
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700976 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530977 if (dmatest == 0) {
978 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
979 thread_count += cnt > 0 ? cnt : 0;
980 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700981 }
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530982
Sinan Kaya61b5f542017-06-29 22:30:58 -0400983 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530984 if (dmatest == 1) {
Dave Jiangc678fa62017-08-21 10:23:13 -0700985 cnt = dmatest_add_threads(info, dtc, DMA_MEMSET);
Kedareswara rao Appanaa0d4cb42016-06-09 21:10:14 +0530986 thread_count += cnt > 0 ? cnt : 0;
987 }
988 }
989
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700990 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200991 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
Nicolas Ferref1aef8b2009-07-06 18:19:44 +0200992 thread_count += cnt > 0 ? cnt : 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700993 }
Dan Williams58691d62009-08-29 19:09:27 -0700994 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +0200995 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
Dr. David Alan Gilbertd07a74a2011-08-25 16:13:55 -0700996 thread_count += cnt > 0 ? cnt : 0;
Dan Williams58691d62009-08-29 19:09:27 -0700997 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700998
Seraj Alijand53513d2018-12-10 08:52:31 +0000999 pr_info("Added %u threads using %s\n",
Dan Williamsb54d5cb2009-03-25 09:13:25 -07001000 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001001
Andy Shevchenko838cc702013-03-04 11:09:28 +02001002 list_add_tail(&dtc->node, &info->channels);
1003 info->nr_channels++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001004
Dan Williams33df8ca2009-01-06 11:38:15 -07001005 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001006}
1007
Dan Williams7dd60252009-01-06 11:38:19 -07001008static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001009{
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001010 struct dmatest_params *params = param;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001011
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001012 if (!dmatest_match_channel(params, chan) ||
1013 !dmatest_match_device(params, chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -07001014 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -07001015 else
Dan Williams7dd60252009-01-06 11:38:19 -07001016 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001017}
1018
Dan Williamsa9e55492013-11-06 16:30:02 -08001019static void request_channels(struct dmatest_info *info,
1020 enum dma_transaction_type type)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001021{
Dan Williams33df8ca2009-01-06 11:38:15 -07001022 dma_cap_mask_t mask;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001023
Dan Williams33df8ca2009-01-06 11:38:15 -07001024 dma_cap_zero(mask);
Dan Williamsa9e55492013-11-06 16:30:02 -08001025 dma_cap_set(type, mask);
Dan Williams33df8ca2009-01-06 11:38:15 -07001026 for (;;) {
Dan Williamsa9e55492013-11-06 16:30:02 -08001027 struct dmatest_params *params = &info->params;
1028 struct dma_chan *chan;
1029
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001030 chan = dma_request_channel(mask, filter, params);
Dan Williams33df8ca2009-01-06 11:38:15 -07001031 if (chan) {
Dan Williamsa9e55492013-11-06 16:30:02 -08001032 if (dmatest_add_channel(info, chan)) {
Dan Williams33df8ca2009-01-06 11:38:15 -07001033 dma_release_channel(chan);
1034 break; /* add_channel failed, punt */
1035 }
1036 } else
1037 break; /* no more channels available */
Andy Shevchenko15b8a8e2013-03-04 11:09:29 +02001038 if (params->max_channels &&
1039 info->nr_channels >= params->max_channels)
Dan Williams33df8ca2009-01-06 11:38:15 -07001040 break; /* we have all we need */
1041 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001042}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001043
Seraj Alijand53513d2018-12-10 08:52:31 +00001044static void add_threaded_test(struct dmatest_info *info)
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001045{
1046 struct dmatest_params *params = &info->params;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001047
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001048 /* Copy test parameters */
Andy Shevchenkoa6c268d2013-07-23 18:36:46 +03001049 params->buf_size = test_buf_size;
1050 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
1051 strlcpy(params->device, strim(test_device), sizeof(params->device));
1052 params->threads_per_chan = threads_per_chan;
1053 params->max_channels = max_channels;
1054 params->iterations = iterations;
1055 params->xor_sources = xor_sources;
1056 params->pq_sources = pq_sources;
1057 params->timeout = timeout;
Dan Williamse3b9c342013-11-06 16:30:05 -08001058 params->noverify = noverify;
Yang Shunyong2e67a082018-02-02 17:51:09 +08001059 params->norandom = norandom;
Seraj Alijana875abf2018-12-10 08:52:37 +00001060 params->alignment = alignment;
Seraj Alijan13396a12018-12-10 08:52:39 +00001061 params->transfer_size = transfer_size;
Dan Williamsa310d032013-11-06 16:30:01 -08001062
Dan Williamsa9e55492013-11-06 16:30:02 -08001063 request_channels(info, DMA_MEMCPY);
Sinan Kaya61b5f542017-06-29 22:30:58 -04001064 request_channels(info, DMA_MEMSET);
Dan Williamsa9e55492013-11-06 16:30:02 -08001065 request_channels(info, DMA_XOR);
1066 request_channels(info, DMA_PQ);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001067}
1068
Seraj Alijand53513d2018-12-10 08:52:31 +00001069static void run_pending_tests(struct dmatest_info *info)
1070{
1071 struct dmatest_chan *dtc;
1072 unsigned int thread_count = 0;
1073
1074 list_for_each_entry(dtc, &info->channels, node) {
1075 struct dmatest_thread *thread;
1076
1077 thread_count = 0;
1078 list_for_each_entry(thread, &dtc->threads, node) {
1079 wake_up_process(thread->task);
1080 thread_count++;
1081 }
1082 pr_info("Started %u threads using %s\n",
1083 thread_count, dma_chan_name(dtc->chan));
1084 }
1085}
1086
Dan Williamsa310d032013-11-06 16:30:01 -08001087static void stop_threaded_test(struct dmatest_info *info)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001088{
1089 struct dmatest_chan *dtc, *_dtc;
1090 struct dma_chan *chan;
1091
1092 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
1093 list_del(&dtc->node);
1094 chan = dtc->chan;
1095 dmatest_cleanup_channel(dtc);
Dan Williams0adff802013-11-06 16:30:00 -08001096 pr_debug("dropped channel %s\n", dma_chan_name(chan));
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001097 dma_release_channel(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001098 }
Dan Williams33df8ca2009-01-06 11:38:15 -07001099
Dan Williams7cbd4872009-03-04 16:06:03 -07001100 info->nr_channels = 0;
Dan Williams33df8ca2009-01-06 11:38:15 -07001101}
Andy Shevchenko838cc702013-03-04 11:09:28 +02001102
Seraj Alijand53513d2018-12-10 08:52:31 +00001103static void start_threaded_tests(struct dmatest_info *info)
Dan Williams7cbd4872009-03-04 16:06:03 -07001104{
Dan Williamsa310d032013-11-06 16:30:01 -08001105 /* we might be called early to set run=, defer running until all
1106 * parameters have been evaluated
1107 */
1108 if (!info->did_init)
Dan Williamsa9e55492013-11-06 16:30:02 -08001109 return;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001110
Seraj Alijand53513d2018-12-10 08:52:31 +00001111 run_pending_tests(info);
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001112}
1113
Dan Williamsa310d032013-11-06 16:30:01 -08001114static int dmatest_run_get(char *val, const struct kernel_param *kp)
Andy Shevchenkobcc567e2013-05-23 14:29:53 +03001115{
Dan Williamsa310d032013-11-06 16:30:01 -08001116 struct dmatest_info *info = &test_info;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001117
1118 mutex_lock(&info->lock);
Dan Williamsa310d032013-11-06 16:30:01 -08001119 if (is_threaded_test_run(info)) {
1120 dmatest_run = true;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001121 } else {
Seraj Alijand53513d2018-12-10 08:52:31 +00001122 if (!is_threaded_test_pending(info))
1123 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001124 dmatest_run = false;
Andy Shevchenko3e5ccd82013-03-04 11:09:31 +02001125 }
Dan Williamsa310d032013-11-06 16:30:01 -08001126 mutex_unlock(&info->lock);
1127
1128 return param_get_bool(val, kp);
1129}
1130
1131static int dmatest_run_set(const char *val, const struct kernel_param *kp)
1132{
1133 struct dmatest_info *info = &test_info;
1134 int ret;
1135
1136 mutex_lock(&info->lock);
1137 ret = param_set_bool(val, kp);
1138 if (ret) {
1139 mutex_unlock(&info->lock);
1140 return ret;
Seraj Alijand53513d2018-12-10 08:52:31 +00001141 } else if (dmatest_run) {
1142 if (is_threaded_test_pending(info))
1143 start_threaded_tests(info);
1144 else
1145 pr_info("Could not start test, no channels configured\n");
1146 } else {
1147 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001148 }
1149
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001150 mutex_unlock(&info->lock);
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001151
Dan Williamsa310d032013-11-06 16:30:01 -08001152 return ret;
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001153}
1154
Seraj Alijand53513d2018-12-10 08:52:31 +00001155static int dmatest_chan_set(const char *val, const struct kernel_param *kp)
1156{
1157 struct dmatest_info *info = &test_info;
1158 struct dmatest_chan *dtc;
1159 char chan_reset_val[20];
1160 int ret = 0;
1161
1162 mutex_lock(&info->lock);
1163 ret = param_set_copystring(val, kp);
1164 if (ret) {
1165 mutex_unlock(&info->lock);
1166 return ret;
1167 }
1168 /*Clear any previously run threads */
1169 if (!is_threaded_test_run(info) && !is_threaded_test_pending(info))
1170 stop_threaded_test(info);
1171 /* Reject channels that are already registered */
1172 if (is_threaded_test_pending(info)) {
1173 list_for_each_entry(dtc, &info->channels, node) {
1174 if (strcmp(dma_chan_name(dtc->chan),
1175 strim(test_channel)) == 0) {
1176 dtc = list_last_entry(&info->channels,
1177 struct dmatest_chan,
1178 node);
1179 strlcpy(chan_reset_val,
1180 dma_chan_name(dtc->chan),
1181 sizeof(chan_reset_val));
1182 ret = -EBUSY;
1183 goto add_chan_err;
1184 }
1185 }
1186 }
1187
1188 add_threaded_test(info);
1189
1190 /* Check if channel was added successfully */
1191 dtc = list_last_entry(&info->channels, struct dmatest_chan, node);
1192
1193 if (dtc->chan) {
1194 /*
1195 * if new channel was not successfully added, revert the
1196 * "test_channel" string to the name of the last successfully
1197 * added channel. exception for when users issues empty string
1198 * to channel parameter.
1199 */
1200 if ((strcmp(dma_chan_name(dtc->chan), strim(test_channel)) != 0)
1201 && (strcmp("", strim(test_channel)) != 0)) {
1202 ret = -EINVAL;
1203 strlcpy(chan_reset_val, dma_chan_name(dtc->chan),
1204 sizeof(chan_reset_val));
1205 goto add_chan_err;
1206 }
1207
1208 } else {
1209 /* Clear test_channel if no channels were added successfully */
1210 strlcpy(chan_reset_val, "", sizeof(chan_reset_val));
1211 ret = -EBUSY;
1212 goto add_chan_err;
1213 }
1214
1215 mutex_unlock(&info->lock);
1216
1217 return ret;
1218
1219add_chan_err:
1220 param_set_copystring(chan_reset_val, kp);
1221 mutex_unlock(&info->lock);
1222
1223 return ret;
1224}
1225
1226static int dmatest_chan_get(char *val, const struct kernel_param *kp)
1227{
1228 struct dmatest_info *info = &test_info;
1229
1230 mutex_lock(&info->lock);
1231 if (!is_threaded_test_run(info) && !is_threaded_test_pending(info)) {
1232 stop_threaded_test(info);
1233 strlcpy(test_channel, "", sizeof(test_channel));
1234 }
1235 mutex_unlock(&info->lock);
1236
1237 return param_get_string(val, kp);
1238}
1239
1240static int dmatest_test_list_get(char *val, const struct kernel_param *kp)
1241{
1242 struct dmatest_info *info = &test_info;
1243 struct dmatest_chan *dtc;
1244 unsigned int thread_count = 0;
1245
1246 list_for_each_entry(dtc, &info->channels, node) {
1247 struct dmatest_thread *thread;
1248
1249 thread_count = 0;
1250 list_for_each_entry(thread, &dtc->threads, node) {
1251 thread_count++;
1252 }
1253 pr_info("%u threads using %s\n",
1254 thread_count, dma_chan_name(dtc->chan));
1255 }
1256
1257 return 0;
1258}
1259
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001260static int __init dmatest_init(void)
1261{
1262 struct dmatest_info *info = &test_info;
Dan Williams2d88ce72013-11-06 16:30:09 -08001263 struct dmatest_params *params = &info->params;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001264
Dan Williamsa310d032013-11-06 16:30:01 -08001265 if (dmatest_run) {
1266 mutex_lock(&info->lock);
Seraj Alijand53513d2018-12-10 08:52:31 +00001267 add_threaded_test(info);
1268 run_pending_tests(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001269 mutex_unlock(&info->lock);
1270 }
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001271
Dan Williams2d88ce72013-11-06 16:30:09 -08001272 if (params->iterations && wait)
1273 wait_event(thread_wait, !is_threaded_test_run(info));
Andy Shevchenko838cc702013-03-04 11:09:28 +02001274
Dan Williamsa310d032013-11-06 16:30:01 -08001275 /* module parameters are stable, inittime tests are started,
1276 * let userspace take over 'run' control
1277 */
1278 info->did_init = true;
Andy Shevchenko95019c82013-03-04 11:09:33 +02001279
Andy Shevchenko851b7e12013-03-04 11:09:30 +02001280 return 0;
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001281}
1282/* when compiled-in wait for drivers to load first */
1283late_initcall(dmatest_init);
1284
1285static void __exit dmatest_exit(void)
1286{
1287 struct dmatest_info *info = &test_info;
1288
Dan Williamsa310d032013-11-06 16:30:01 -08001289 mutex_lock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001290 stop_threaded_test(info);
Dan Williamsa310d032013-11-06 16:30:01 -08001291 mutex_unlock(&info->lock);
Andy Shevchenkoe03e93a2013-03-04 11:09:27 +02001292}
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001293module_exit(dmatest_exit);
1294
Jean Delvaree05503e2011-05-18 16:49:24 +02001295MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001296MODULE_LICENSE("GPL v2");