blob: a3722a7384b5048a938d52215498218fffaf4bb0 [file] [log] [blame]
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -07001/*
2 * DMA Engine test module
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/delay.h>
11#include <linux/dmaengine.h>
12#include <linux/init.h>
13#include <linux/kthread.h>
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/random.h>
17#include <linux/wait.h>
18
19static unsigned int test_buf_size = 16384;
20module_param(test_buf_size, uint, S_IRUGO);
21MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
22
Kay Sievers06190d82008-11-11 13:12:33 -070023static char test_channel[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070024module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
25MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
26
Kay Sievers06190d82008-11-11 13:12:33 -070027static char test_device[20];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070028module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
29MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
30
31static unsigned int threads_per_chan = 1;
32module_param(threads_per_chan, uint, S_IRUGO);
33MODULE_PARM_DESC(threads_per_chan,
34 "Number of threads to start per channel (default: 1)");
35
36static unsigned int max_channels;
37module_param(max_channels, uint, S_IRUGO);
Dan Williams33df8ca2009-01-06 11:38:15 -070038MODULE_PARM_DESC(max_channels,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070039 "Maximum number of channels to use (default: all)");
40
Dan Williamsb54d5cb2009-03-25 09:13:25 -070041static unsigned int xor_sources = 3;
42module_param(xor_sources, uint, S_IRUGO);
43MODULE_PARM_DESC(xor_sources,
44 "Number of xor source buffers (default: 3)");
45
Dan Williams58691d62009-08-29 19:09:27 -070046static unsigned int pq_sources = 3;
47module_param(pq_sources, uint, S_IRUGO);
48MODULE_PARM_DESC(pq_sources,
49 "Number of p+q source buffers (default: 3)");
50
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070051/*
52 * Initialization patterns. All bytes in the source buffer has bit 7
53 * set, all bytes in the destination buffer has bit 7 cleared.
54 *
55 * Bit 6 is set for all bytes which are to be copied by the DMA
56 * engine. Bit 5 is set for all bytes which are to be overwritten by
57 * the DMA engine.
58 *
59 * The remaining bits are the inverse of a counter which increments by
60 * one for each byte address.
61 */
62#define PATTERN_SRC 0x80
63#define PATTERN_DST 0x00
64#define PATTERN_COPY 0x40
65#define PATTERN_OVERWRITE 0x20
66#define PATTERN_COUNT_MASK 0x1f
67
68struct dmatest_thread {
69 struct list_head node;
70 struct task_struct *task;
71 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070072 u8 **srcs;
73 u8 **dsts;
74 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070075};
76
77struct dmatest_chan {
78 struct list_head node;
79 struct dma_chan *chan;
80 struct list_head threads;
81};
82
83/*
84 * These are protected by dma_list_mutex since they're only used by
Dan Williams33df8ca2009-01-06 11:38:15 -070085 * the DMA filter function callback
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070086 */
87static LIST_HEAD(dmatest_channels);
88static unsigned int nr_channels;
89
90static bool dmatest_match_channel(struct dma_chan *chan)
91{
92 if (test_channel[0] == '\0')
93 return true;
Dan Williams41d5e592009-01-06 11:38:21 -070094 return strcmp(dma_chan_name(chan), test_channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070095}
96
97static bool dmatest_match_device(struct dma_device *device)
98{
99 if (test_device[0] == '\0')
100 return true;
Kay Sievers06190d82008-11-11 13:12:33 -0700101 return strcmp(dev_name(device->dev), test_device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700102}
103
104static unsigned long dmatest_random(void)
105{
106 unsigned long buf;
107
108 get_random_bytes(&buf, sizeof(buf));
109 return buf;
110}
111
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700112static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700113{
114 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700115 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700116
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700117 for (; (buf = *bufs); bufs++) {
118 for (i = 0; i < start; i++)
119 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
120 for ( ; i < start + len; i++)
121 buf[i] = PATTERN_SRC | PATTERN_COPY
122 | (~i & PATTERN_COUNT_MASK);;
123 for ( ; i < test_buf_size; i++)
124 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
125 buf++;
126 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700127}
128
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700129static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700130{
131 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700132 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700133
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700134 for (; (buf = *bufs); bufs++) {
135 for (i = 0; i < start; i++)
136 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
137 for ( ; i < start + len; i++)
138 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
139 | (~i & PATTERN_COUNT_MASK);
140 for ( ; i < test_buf_size; i++)
141 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
142 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700143}
144
145static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
146 unsigned int counter, bool is_srcbuf)
147{
148 u8 diff = actual ^ pattern;
149 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
150 const char *thread_name = current->comm;
151
152 if (is_srcbuf)
153 pr_warning("%s: srcbuf[0x%x] overwritten!"
154 " Expected %02x, got %02x\n",
155 thread_name, index, expected, actual);
156 else if ((pattern & PATTERN_COPY)
157 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
158 pr_warning("%s: dstbuf[0x%x] not copied!"
159 " Expected %02x, got %02x\n",
160 thread_name, index, expected, actual);
161 else if (diff & PATTERN_SRC)
162 pr_warning("%s: dstbuf[0x%x] was copied!"
163 " Expected %02x, got %02x\n",
164 thread_name, index, expected, actual);
165 else
166 pr_warning("%s: dstbuf[0x%x] mismatch!"
167 " Expected %02x, got %02x\n",
168 thread_name, index, expected, actual);
169}
170
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700171static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700172 unsigned int end, unsigned int counter, u8 pattern,
173 bool is_srcbuf)
174{
175 unsigned int i;
176 unsigned int error_count = 0;
177 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700178 u8 expected;
179 u8 *buf;
180 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700181
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700182 for (; (buf = *bufs); bufs++) {
183 counter = counter_orig;
184 for (i = start; i < end; i++) {
185 actual = buf[i];
186 expected = pattern | (~counter & PATTERN_COUNT_MASK);
187 if (actual != expected) {
188 if (error_count < 32)
189 dmatest_mismatch(actual, pattern, i,
190 counter, is_srcbuf);
191 error_count++;
192 }
193 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700194 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700195 }
196
197 if (error_count > 32)
198 pr_warning("%s: %u errors suppressed\n",
199 current->comm, error_count - 32);
200
201 return error_count;
202}
203
Dan Williamse44e0aa2009-03-25 09:13:25 -0700204static void dmatest_callback(void *completion)
205{
206 complete(completion);
207}
208
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700209/*
210 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700211 * offsets for a given operation type until it is told to exit by
212 * kthread_stop(). There may be multiple threads running this function
213 * in parallel for a single channel, and there may be multiple channels
214 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700215 *
216 * Before each test, the source and destination buffer is initialized
217 * with a known pattern. This pattern is different depending on
218 * whether it's in an area which is supposed to be copied or
219 * overwritten, and different in the source and destination buffers.
220 * So if the DMA engine doesn't copy exactly what we tell it to copy,
221 * we'll notice.
222 */
223static int dmatest_func(void *data)
224{
225 struct dmatest_thread *thread = data;
226 struct dma_chan *chan;
227 const char *thread_name;
228 unsigned int src_off, dst_off, len;
229 unsigned int error_count;
230 unsigned int failed_tests = 0;
231 unsigned int total_tests = 0;
232 dma_cookie_t cookie;
233 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700234 enum dma_ctrl_flags flags;
Dan Williams58691d62009-08-29 19:09:27 -0700235 u8 pq_coefs[pq_sources];
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700236 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700237 int src_cnt;
238 int dst_cnt;
239 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700240
241 thread_name = current->comm;
242
243 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700244
245 smp_rmb();
246 chan = thread->chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700247 if (thread->type == DMA_MEMCPY)
248 src_cnt = dst_cnt = 1;
249 else if (thread->type == DMA_XOR) {
250 src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
251 dst_cnt = 1;
Dan Williams58691d62009-08-29 19:09:27 -0700252 } else if (thread->type == DMA_PQ) {
253 src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
254 dst_cnt = 2;
255 for (i = 0; i < pq_sources; i++)
256 pq_coefs[i] = 1;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700257 } else
258 goto err_srcs;
259
260 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
261 if (!thread->srcs)
262 goto err_srcs;
263 for (i = 0; i < src_cnt; i++) {
264 thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
265 if (!thread->srcs[i])
266 goto err_srcbuf;
267 }
268 thread->srcs[i] = NULL;
269
270 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
271 if (!thread->dsts)
272 goto err_dsts;
273 for (i = 0; i < dst_cnt; i++) {
274 thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
275 if (!thread->dsts[i])
276 goto err_dstbuf;
277 }
278 thread->dsts[i] = NULL;
279
Dan Williamse44e0aa2009-03-25 09:13:25 -0700280 set_user_nice(current, 10);
281
282 flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP | DMA_PREP_INTERRUPT;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700283
284 while (!kthread_should_stop()) {
Atsushi Nemotod86be862009-01-13 09:22:20 -0700285 struct dma_device *dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700286 struct dma_async_tx_descriptor *tx = NULL;
287 dma_addr_t dma_srcs[src_cnt];
288 dma_addr_t dma_dsts[dst_cnt];
Dan Williamse44e0aa2009-03-25 09:13:25 -0700289 struct completion cmp;
290 unsigned long tmo = msecs_to_jiffies(3000);
Dan Williams83544ae2009-09-08 17:42:53 -0700291 u8 align = 0;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700292
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700293 total_tests++;
294
295 len = dmatest_random() % test_buf_size + 1;
296 src_off = dmatest_random() % (test_buf_size - len + 1);
297 dst_off = dmatest_random() % (test_buf_size - len + 1);
298
Dan Williams83544ae2009-09-08 17:42:53 -0700299 /* honor alignment restrictions */
300 if (thread->type == DMA_MEMCPY)
301 align = dev->copy_align;
302 else if (thread->type == DMA_XOR)
303 align = dev->xor_align;
304 else if (thread->type == DMA_PQ)
305 align = dev->pq_align;
306
307 len = (len >> align) << align;
308 src_off = (src_off >> align) << align;
309 dst_off = (dst_off >> align) << align;
310
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700311 dmatest_init_srcs(thread->srcs, src_off, len);
312 dmatest_init_dsts(thread->dsts, dst_off, len);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700313
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700314 for (i = 0; i < src_cnt; i++) {
315 u8 *buf = thread->srcs[i] + src_off;
316
317 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
318 DMA_TO_DEVICE);
319 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700320 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700321 for (i = 0; i < dst_cnt; i++) {
322 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
323 test_buf_size,
324 DMA_BIDIRECTIONAL);
325 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700326
Dan Williams83544ae2009-09-08 17:42:53 -0700327
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700328 if (thread->type == DMA_MEMCPY)
329 tx = dev->device_prep_dma_memcpy(chan,
330 dma_dsts[0] + dst_off,
331 dma_srcs[0], len,
332 flags);
333 else if (thread->type == DMA_XOR)
334 tx = dev->device_prep_dma_xor(chan,
335 dma_dsts[0] + dst_off,
336 dma_srcs, xor_sources,
337 len, flags);
Dan Williams58691d62009-08-29 19:09:27 -0700338 else if (thread->type == DMA_PQ) {
339 dma_addr_t dma_pq[dst_cnt];
340
341 for (i = 0; i < dst_cnt; i++)
342 dma_pq[i] = dma_dsts[i] + dst_off;
343 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
344 pq_sources, pq_coefs,
345 len, flags);
346 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700347
Atsushi Nemotod86be862009-01-13 09:22:20 -0700348 if (!tx) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700349 for (i = 0; i < src_cnt; i++)
350 dma_unmap_single(dev->dev, dma_srcs[i], len,
351 DMA_TO_DEVICE);
352 for (i = 0; i < dst_cnt; i++)
353 dma_unmap_single(dev->dev, dma_dsts[i],
354 test_buf_size,
355 DMA_BIDIRECTIONAL);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700356 pr_warning("%s: #%u: prep error with src_off=0x%x "
357 "dst_off=0x%x len=0x%x\n",
358 thread_name, total_tests - 1,
359 src_off, dst_off, len);
360 msleep(100);
361 failed_tests++;
362 continue;
363 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700364
365 init_completion(&cmp);
366 tx->callback = dmatest_callback;
367 tx->callback_param = &cmp;
Atsushi Nemotod86be862009-01-13 09:22:20 -0700368 cookie = tx->tx_submit(tx);
369
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700370 if (dma_submit_error(cookie)) {
371 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
372 "dst_off=0x%x len=0x%x\n",
373 thread_name, total_tests - 1, cookie,
374 src_off, dst_off, len);
375 msleep(100);
376 failed_tests++;
377 continue;
378 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700379 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700380
Dan Williamse44e0aa2009-03-25 09:13:25 -0700381 tmo = wait_for_completion_timeout(&cmp, tmo);
382 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700383
Dan Williamse44e0aa2009-03-25 09:13:25 -0700384 if (tmo == 0) {
385 pr_warning("%s: #%u: test timed out\n",
386 thread_name, total_tests - 1);
387 failed_tests++;
388 continue;
389 } else if (status != DMA_SUCCESS) {
390 pr_warning("%s: #%u: got completion callback,"
391 " but status is \'%s\'\n",
392 thread_name, total_tests - 1,
393 status == DMA_ERROR ? "error" : "in progress");
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700394 failed_tests++;
395 continue;
396 }
Dan Williamse44e0aa2009-03-25 09:13:25 -0700397
Atsushi Nemotod86be862009-01-13 09:22:20 -0700398 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700399 for (i = 0; i < dst_cnt; i++)
400 dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
401 DMA_BIDIRECTIONAL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700402
403 error_count = 0;
404
405 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700406 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700407 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700408 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700409 src_off + len, src_off,
410 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700411 error_count += dmatest_verify(thread->srcs, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700412 test_buf_size, src_off + len,
413 PATTERN_SRC, true);
414
415 pr_debug("%s: verifying dest buffer...\n",
416 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700417 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700418 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700419 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700420 dst_off + len, src_off,
421 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700422 error_count += dmatest_verify(thread->dsts, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700423 test_buf_size, dst_off + len,
424 PATTERN_DST, false);
425
426 if (error_count) {
427 pr_warning("%s: #%u: %u errors with "
428 "src_off=0x%x dst_off=0x%x len=0x%x\n",
429 thread_name, total_tests - 1, error_count,
430 src_off, dst_off, len);
431 failed_tests++;
432 } else {
433 pr_debug("%s: #%u: No errors with "
434 "src_off=0x%x dst_off=0x%x len=0x%x\n",
435 thread_name, total_tests - 1,
436 src_off, dst_off, len);
437 }
438 }
439
440 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700441 for (i = 0; thread->dsts[i]; i++)
442 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700443err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700444 kfree(thread->dsts);
445err_dsts:
446 for (i = 0; thread->srcs[i]; i++)
447 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700448err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700449 kfree(thread->srcs);
450err_srcs:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700451 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
452 thread_name, total_tests, failed_tests, ret);
453 return ret;
454}
455
456static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
457{
458 struct dmatest_thread *thread;
459 struct dmatest_thread *_thread;
460 int ret;
461
462 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
463 ret = kthread_stop(thread->task);
464 pr_debug("dmatest: thread %s exited with status %d\n",
465 thread->task->comm, ret);
466 list_del(&thread->node);
467 kfree(thread);
468 }
469 kfree(dtc);
470}
471
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700472static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
473{
474 struct dmatest_thread *thread;
475 struct dma_chan *chan = dtc->chan;
476 char *op;
477 unsigned int i;
478
479 if (type == DMA_MEMCPY)
480 op = "copy";
481 else if (type == DMA_XOR)
482 op = "xor";
Dan Williams58691d62009-08-29 19:09:27 -0700483 else if (type == DMA_PQ)
484 op = "pq";
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700485 else
486 return -EINVAL;
487
488 for (i = 0; i < threads_per_chan; i++) {
489 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
490 if (!thread) {
491 pr_warning("dmatest: No memory for %s-%s%u\n",
492 dma_chan_name(chan), op, i);
493
494 break;
495 }
496 thread->chan = dtc->chan;
497 thread->type = type;
498 smp_wmb();
499 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
500 dma_chan_name(chan), op, i);
501 if (IS_ERR(thread->task)) {
502 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
503 dma_chan_name(chan), op, i);
504 kfree(thread);
505 break;
506 }
507
508 /* srcbuf and dstbuf are allocated by the thread itself */
509
510 list_add_tail(&thread->node, &dtc->threads);
511 }
512
513 return i;
514}
515
Dan Williams33df8ca2009-01-06 11:38:15 -0700516static int dmatest_add_channel(struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700517{
518 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700519 struct dma_device *dma_dev = chan->device;
520 unsigned int thread_count = 0;
521 unsigned int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700522
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700523 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700524 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700525 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700526 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700527 }
528
529 dtc->chan = chan;
530 INIT_LIST_HEAD(&dtc->threads);
531
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700532 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
533 cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
534 thread_count += cnt > 0 ?: 0;
535 }
536 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
537 cnt = dmatest_add_threads(dtc, DMA_XOR);
538 thread_count += cnt > 0 ?: 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700539 }
Dan Williams58691d62009-08-29 19:09:27 -0700540 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
541 cnt = dmatest_add_threads(dtc, DMA_PQ);
542 thread_count += cnt > 0 ?: 0;
543 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700544
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700545 pr_info("dmatest: Started %u threads using %s\n",
546 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700547
548 list_add_tail(&dtc->node, &dmatest_channels);
549 nr_channels++;
550
Dan Williams33df8ca2009-01-06 11:38:15 -0700551 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700552}
553
Dan Williams7dd60252009-01-06 11:38:19 -0700554static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700555{
Dan Williams33df8ca2009-01-06 11:38:15 -0700556 if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700557 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700558 else
Dan Williams7dd60252009-01-06 11:38:19 -0700559 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700560}
561
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700562static int __init dmatest_init(void)
563{
Dan Williams33df8ca2009-01-06 11:38:15 -0700564 dma_cap_mask_t mask;
565 struct dma_chan *chan;
566 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700567
Dan Williams33df8ca2009-01-06 11:38:15 -0700568 dma_cap_zero(mask);
569 dma_cap_set(DMA_MEMCPY, mask);
570 for (;;) {
571 chan = dma_request_channel(mask, filter, NULL);
572 if (chan) {
573 err = dmatest_add_channel(chan);
Dan Williamsc56c81a2009-04-08 15:08:23 -0700574 if (err) {
Dan Williams33df8ca2009-01-06 11:38:15 -0700575 dma_release_channel(chan);
576 break; /* add_channel failed, punt */
577 }
578 } else
579 break; /* no more channels available */
580 if (max_channels && nr_channels >= max_channels)
581 break; /* we have all we need */
582 }
583
584 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700585}
Dan Williams33df8ca2009-01-06 11:38:15 -0700586/* when compiled-in wait for drivers to load first */
587late_initcall(dmatest_init);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700588
589static void __exit dmatest_exit(void)
590{
Dan Williams33df8ca2009-01-06 11:38:15 -0700591 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700592 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700593
594 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
595 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700596 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700597 dmatest_cleanup_channel(dtc);
598 pr_debug("dmatest: dropped channel %s\n",
Dan Williams7cbd4872009-03-04 16:06:03 -0700599 dma_chan_name(chan));
600 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700601 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700602}
603module_exit(dmatest_exit);
604
605MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
606MODULE_LICENSE("GPL v2");