blob: 224acf478fec2d93636c505756ebb6ef00dbbe70 [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
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070046/*
47 * Initialization patterns. All bytes in the source buffer has bit 7
48 * set, all bytes in the destination buffer has bit 7 cleared.
49 *
50 * Bit 6 is set for all bytes which are to be copied by the DMA
51 * engine. Bit 5 is set for all bytes which are to be overwritten by
52 * the DMA engine.
53 *
54 * The remaining bits are the inverse of a counter which increments by
55 * one for each byte address.
56 */
57#define PATTERN_SRC 0x80
58#define PATTERN_DST 0x00
59#define PATTERN_COPY 0x40
60#define PATTERN_OVERWRITE 0x20
61#define PATTERN_COUNT_MASK 0x1f
62
63struct dmatest_thread {
64 struct list_head node;
65 struct task_struct *task;
66 struct dma_chan *chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -070067 u8 **srcs;
68 u8 **dsts;
69 enum dma_transaction_type type;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070070};
71
72struct dmatest_chan {
73 struct list_head node;
74 struct dma_chan *chan;
75 struct list_head threads;
76};
77
78/*
79 * These are protected by dma_list_mutex since they're only used by
Dan Williams33df8ca2009-01-06 11:38:15 -070080 * the DMA filter function callback
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070081 */
82static LIST_HEAD(dmatest_channels);
83static unsigned int nr_channels;
84
85static bool dmatest_match_channel(struct dma_chan *chan)
86{
87 if (test_channel[0] == '\0')
88 return true;
Dan Williams41d5e592009-01-06 11:38:21 -070089 return strcmp(dma_chan_name(chan), test_channel) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070090}
91
92static bool dmatest_match_device(struct dma_device *device)
93{
94 if (test_device[0] == '\0')
95 return true;
Kay Sievers06190d82008-11-11 13:12:33 -070096 return strcmp(dev_name(device->dev), test_device) == 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -070097}
98
99static unsigned long dmatest_random(void)
100{
101 unsigned long buf;
102
103 get_random_bytes(&buf, sizeof(buf));
104 return buf;
105}
106
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700107static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700108{
109 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700110 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700111
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700112 for (; (buf = *bufs); bufs++) {
113 for (i = 0; i < start; i++)
114 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
115 for ( ; i < start + len; i++)
116 buf[i] = PATTERN_SRC | PATTERN_COPY
117 | (~i & PATTERN_COUNT_MASK);;
118 for ( ; i < test_buf_size; i++)
119 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
120 buf++;
121 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700122}
123
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700124static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700125{
126 unsigned int i;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700127 u8 *buf;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700128
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700129 for (; (buf = *bufs); bufs++) {
130 for (i = 0; i < start; i++)
131 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
132 for ( ; i < start + len; i++)
133 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
134 | (~i & PATTERN_COUNT_MASK);
135 for ( ; i < test_buf_size; i++)
136 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
137 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700138}
139
140static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
141 unsigned int counter, bool is_srcbuf)
142{
143 u8 diff = actual ^ pattern;
144 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
145 const char *thread_name = current->comm;
146
147 if (is_srcbuf)
148 pr_warning("%s: srcbuf[0x%x] overwritten!"
149 " Expected %02x, got %02x\n",
150 thread_name, index, expected, actual);
151 else if ((pattern & PATTERN_COPY)
152 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
153 pr_warning("%s: dstbuf[0x%x] not copied!"
154 " Expected %02x, got %02x\n",
155 thread_name, index, expected, actual);
156 else if (diff & PATTERN_SRC)
157 pr_warning("%s: dstbuf[0x%x] was copied!"
158 " Expected %02x, got %02x\n",
159 thread_name, index, expected, actual);
160 else
161 pr_warning("%s: dstbuf[0x%x] mismatch!"
162 " Expected %02x, got %02x\n",
163 thread_name, index, expected, actual);
164}
165
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700166static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700167 unsigned int end, unsigned int counter, u8 pattern,
168 bool is_srcbuf)
169{
170 unsigned int i;
171 unsigned int error_count = 0;
172 u8 actual;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700173 u8 expected;
174 u8 *buf;
175 unsigned int counter_orig = counter;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700176
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700177 for (; (buf = *bufs); bufs++) {
178 counter = counter_orig;
179 for (i = start; i < end; i++) {
180 actual = buf[i];
181 expected = pattern | (~counter & PATTERN_COUNT_MASK);
182 if (actual != expected) {
183 if (error_count < 32)
184 dmatest_mismatch(actual, pattern, i,
185 counter, is_srcbuf);
186 error_count++;
187 }
188 counter++;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700189 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700190 }
191
192 if (error_count > 32)
193 pr_warning("%s: %u errors suppressed\n",
194 current->comm, error_count - 32);
195
196 return error_count;
197}
198
199/*
200 * This function repeatedly tests DMA transfers of various lengths and
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700201 * offsets for a given operation type until it is told to exit by
202 * kthread_stop(). There may be multiple threads running this function
203 * in parallel for a single channel, and there may be multiple channels
204 * being tested in parallel.
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700205 *
206 * Before each test, the source and destination buffer is initialized
207 * with a known pattern. This pattern is different depending on
208 * whether it's in an area which is supposed to be copied or
209 * overwritten, and different in the source and destination buffers.
210 * So if the DMA engine doesn't copy exactly what we tell it to copy,
211 * we'll notice.
212 */
213static int dmatest_func(void *data)
214{
215 struct dmatest_thread *thread = data;
216 struct dma_chan *chan;
217 const char *thread_name;
218 unsigned int src_off, dst_off, len;
219 unsigned int error_count;
220 unsigned int failed_tests = 0;
221 unsigned int total_tests = 0;
222 dma_cookie_t cookie;
223 enum dma_status status;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700224 enum dma_ctrl_flags flags;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700225 int ret;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700226 int src_cnt;
227 int dst_cnt;
228 int i;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700229
230 thread_name = current->comm;
231
232 ret = -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700233
234 smp_rmb();
235 chan = thread->chan;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700236 if (thread->type == DMA_MEMCPY)
237 src_cnt = dst_cnt = 1;
238 else if (thread->type == DMA_XOR) {
239 src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
240 dst_cnt = 1;
241 } else
242 goto err_srcs;
243
244 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
245 if (!thread->srcs)
246 goto err_srcs;
247 for (i = 0; i < src_cnt; i++) {
248 thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
249 if (!thread->srcs[i])
250 goto err_srcbuf;
251 }
252 thread->srcs[i] = NULL;
253
254 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
255 if (!thread->dsts)
256 goto err_dsts;
257 for (i = 0; i < dst_cnt; i++) {
258 thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
259 if (!thread->dsts[i])
260 goto err_dstbuf;
261 }
262 thread->dsts[i] = NULL;
263
264 flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700265
266 while (!kthread_should_stop()) {
Atsushi Nemotod86be862009-01-13 09:22:20 -0700267 struct dma_device *dev = chan->device;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700268 struct dma_async_tx_descriptor *tx = NULL;
269 dma_addr_t dma_srcs[src_cnt];
270 dma_addr_t dma_dsts[dst_cnt];
Atsushi Nemotod86be862009-01-13 09:22:20 -0700271
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700272 total_tests++;
273
274 len = dmatest_random() % test_buf_size + 1;
275 src_off = dmatest_random() % (test_buf_size - len + 1);
276 dst_off = dmatest_random() % (test_buf_size - len + 1);
277
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700278 dmatest_init_srcs(thread->srcs, src_off, len);
279 dmatest_init_dsts(thread->dsts, dst_off, len);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700280
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700281 for (i = 0; i < src_cnt; i++) {
282 u8 *buf = thread->srcs[i] + src_off;
283
284 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
285 DMA_TO_DEVICE);
286 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700287 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700288 for (i = 0; i < dst_cnt; i++) {
289 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
290 test_buf_size,
291 DMA_BIDIRECTIONAL);
292 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700293
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700294 if (thread->type == DMA_MEMCPY)
295 tx = dev->device_prep_dma_memcpy(chan,
296 dma_dsts[0] + dst_off,
297 dma_srcs[0], len,
298 flags);
299 else if (thread->type == DMA_XOR)
300 tx = dev->device_prep_dma_xor(chan,
301 dma_dsts[0] + dst_off,
302 dma_srcs, xor_sources,
303 len, flags);
304
Atsushi Nemotod86be862009-01-13 09:22:20 -0700305 if (!tx) {
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700306 for (i = 0; i < src_cnt; i++)
307 dma_unmap_single(dev->dev, dma_srcs[i], len,
308 DMA_TO_DEVICE);
309 for (i = 0; i < dst_cnt; i++)
310 dma_unmap_single(dev->dev, dma_dsts[i],
311 test_buf_size,
312 DMA_BIDIRECTIONAL);
Atsushi Nemotod86be862009-01-13 09:22:20 -0700313 pr_warning("%s: #%u: prep error with src_off=0x%x "
314 "dst_off=0x%x len=0x%x\n",
315 thread_name, total_tests - 1,
316 src_off, dst_off, len);
317 msleep(100);
318 failed_tests++;
319 continue;
320 }
321 tx->callback = NULL;
322 cookie = tx->tx_submit(tx);
323
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700324 if (dma_submit_error(cookie)) {
325 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
326 "dst_off=0x%x len=0x%x\n",
327 thread_name, total_tests - 1, cookie,
328 src_off, dst_off, len);
329 msleep(100);
330 failed_tests++;
331 continue;
332 }
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700333 dma_async_issue_pending(chan);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700334
335 do {
336 msleep(1);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700337 status = dma_async_is_tx_complete(
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700338 chan, cookie, NULL, NULL);
339 } while (status == DMA_IN_PROGRESS);
340
341 if (status == DMA_ERROR) {
342 pr_warning("%s: #%u: error during copy\n",
343 thread_name, total_tests - 1);
344 failed_tests++;
345 continue;
346 }
Atsushi Nemotod86be862009-01-13 09:22:20 -0700347 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700348 for (i = 0; i < dst_cnt; i++)
349 dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
350 DMA_BIDIRECTIONAL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700351
352 error_count = 0;
353
354 pr_debug("%s: verifying source buffer...\n", thread_name);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700355 error_count += dmatest_verify(thread->srcs, 0, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700356 0, PATTERN_SRC, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700357 error_count += dmatest_verify(thread->srcs, src_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700358 src_off + len, src_off,
359 PATTERN_SRC | PATTERN_COPY, true);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700360 error_count += dmatest_verify(thread->srcs, src_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700361 test_buf_size, src_off + len,
362 PATTERN_SRC, true);
363
364 pr_debug("%s: verifying dest buffer...\n",
365 thread->task->comm);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700366 error_count += dmatest_verify(thread->dsts, 0, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700367 0, PATTERN_DST, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700368 error_count += dmatest_verify(thread->dsts, dst_off,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700369 dst_off + len, src_off,
370 PATTERN_SRC | PATTERN_COPY, false);
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700371 error_count += dmatest_verify(thread->dsts, dst_off + len,
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700372 test_buf_size, dst_off + len,
373 PATTERN_DST, false);
374
375 if (error_count) {
376 pr_warning("%s: #%u: %u errors with "
377 "src_off=0x%x dst_off=0x%x len=0x%x\n",
378 thread_name, total_tests - 1, error_count,
379 src_off, dst_off, len);
380 failed_tests++;
381 } else {
382 pr_debug("%s: #%u: No errors with "
383 "src_off=0x%x dst_off=0x%x len=0x%x\n",
384 thread_name, total_tests - 1,
385 src_off, dst_off, len);
386 }
387 }
388
389 ret = 0;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700390 for (i = 0; thread->dsts[i]; i++)
391 kfree(thread->dsts[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700392err_dstbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700393 kfree(thread->dsts);
394err_dsts:
395 for (i = 0; thread->srcs[i]; i++)
396 kfree(thread->srcs[i]);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700397err_srcbuf:
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700398 kfree(thread->srcs);
399err_srcs:
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700400 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
401 thread_name, total_tests, failed_tests, ret);
402 return ret;
403}
404
405static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
406{
407 struct dmatest_thread *thread;
408 struct dmatest_thread *_thread;
409 int ret;
410
411 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
412 ret = kthread_stop(thread->task);
413 pr_debug("dmatest: thread %s exited with status %d\n",
414 thread->task->comm, ret);
415 list_del(&thread->node);
416 kfree(thread);
417 }
418 kfree(dtc);
419}
420
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700421static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
422{
423 struct dmatest_thread *thread;
424 struct dma_chan *chan = dtc->chan;
425 char *op;
426 unsigned int i;
427
428 if (type == DMA_MEMCPY)
429 op = "copy";
430 else if (type == DMA_XOR)
431 op = "xor";
432 else
433 return -EINVAL;
434
435 for (i = 0; i < threads_per_chan; i++) {
436 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
437 if (!thread) {
438 pr_warning("dmatest: No memory for %s-%s%u\n",
439 dma_chan_name(chan), op, i);
440
441 break;
442 }
443 thread->chan = dtc->chan;
444 thread->type = type;
445 smp_wmb();
446 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
447 dma_chan_name(chan), op, i);
448 if (IS_ERR(thread->task)) {
449 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
450 dma_chan_name(chan), op, i);
451 kfree(thread);
452 break;
453 }
454
455 /* srcbuf and dstbuf are allocated by the thread itself */
456
457 list_add_tail(&thread->node, &dtc->threads);
458 }
459
460 return i;
461}
462
Dan Williams33df8ca2009-01-06 11:38:15 -0700463static int dmatest_add_channel(struct dma_chan *chan)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700464{
465 struct dmatest_chan *dtc;
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700466 struct dma_device *dma_dev = chan->device;
467 unsigned int thread_count = 0;
468 unsigned int cnt;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700469
Andrew Morton6fdb8bd2008-09-19 04:16:23 -0700470 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700471 if (!dtc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700472 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
Dan Williams33df8ca2009-01-06 11:38:15 -0700473 return -ENOMEM;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700474 }
475
476 dtc->chan = chan;
477 INIT_LIST_HEAD(&dtc->threads);
478
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700479 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
480 cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
481 thread_count += cnt > 0 ?: 0;
482 }
483 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
484 cnt = dmatest_add_threads(dtc, DMA_XOR);
485 thread_count += cnt > 0 ?: 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700486 }
487
Dan Williamsb54d5cb2009-03-25 09:13:25 -0700488 pr_info("dmatest: Started %u threads using %s\n",
489 thread_count, dma_chan_name(chan));
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700490
491 list_add_tail(&dtc->node, &dmatest_channels);
492 nr_channels++;
493
Dan Williams33df8ca2009-01-06 11:38:15 -0700494 return 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700495}
496
Dan Williams7dd60252009-01-06 11:38:19 -0700497static bool filter(struct dma_chan *chan, void *param)
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700498{
Dan Williams33df8ca2009-01-06 11:38:15 -0700499 if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
Dan Williams7dd60252009-01-06 11:38:19 -0700500 return false;
Dan Williams33df8ca2009-01-06 11:38:15 -0700501 else
Dan Williams7dd60252009-01-06 11:38:19 -0700502 return true;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700503}
504
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700505static int __init dmatest_init(void)
506{
Dan Williams33df8ca2009-01-06 11:38:15 -0700507 dma_cap_mask_t mask;
508 struct dma_chan *chan;
509 int err = 0;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700510
Dan Williams33df8ca2009-01-06 11:38:15 -0700511 dma_cap_zero(mask);
512 dma_cap_set(DMA_MEMCPY, mask);
513 for (;;) {
514 chan = dma_request_channel(mask, filter, NULL);
515 if (chan) {
516 err = dmatest_add_channel(chan);
517 if (err == 0)
518 continue;
519 else {
520 dma_release_channel(chan);
521 break; /* add_channel failed, punt */
522 }
523 } else
524 break; /* no more channels available */
525 if (max_channels && nr_channels >= max_channels)
526 break; /* we have all we need */
527 }
528
529 return err;
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700530}
Dan Williams33df8ca2009-01-06 11:38:15 -0700531/* when compiled-in wait for drivers to load first */
532late_initcall(dmatest_init);
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700533
534static void __exit dmatest_exit(void)
535{
Dan Williams33df8ca2009-01-06 11:38:15 -0700536 struct dmatest_chan *dtc, *_dtc;
Dan Williams7cbd4872009-03-04 16:06:03 -0700537 struct dma_chan *chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700538
539 list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
540 list_del(&dtc->node);
Dan Williams7cbd4872009-03-04 16:06:03 -0700541 chan = dtc->chan;
Dan Williams33df8ca2009-01-06 11:38:15 -0700542 dmatest_cleanup_channel(dtc);
543 pr_debug("dmatest: dropped channel %s\n",
Dan Williams7cbd4872009-03-04 16:06:03 -0700544 dma_chan_name(chan));
545 dma_release_channel(chan);
Dan Williams33df8ca2009-01-06 11:38:15 -0700546 }
Haavard Skinnemoen4a776f02008-07-08 11:58:45 -0700547}
548module_exit(dmatest_exit);
549
550MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
551MODULE_LICENSE("GPL v2");