2 * DMA Engine test module
4 * Copyright (C) 2007 Atmel Corporation
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.
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>
19 static unsigned int test_buf_size
= 16384;
20 module_param(test_buf_size
, uint
, S_IRUGO
);
21 MODULE_PARM_DESC(test_buf_size
, "Size of the memcpy test buffer");
23 static char test_channel
[20];
24 module_param_string(channel
, test_channel
, sizeof(test_channel
), S_IRUGO
);
25 MODULE_PARM_DESC(channel
, "Bus ID of the channel to test (default: any)");
27 static char test_device
[20];
28 module_param_string(device
, test_device
, sizeof(test_device
), S_IRUGO
);
29 MODULE_PARM_DESC(device
, "Bus ID of the DMA Engine to test (default: any)");
31 static unsigned int threads_per_chan
= 1;
32 module_param(threads_per_chan
, uint
, S_IRUGO
);
33 MODULE_PARM_DESC(threads_per_chan
,
34 "Number of threads to start per channel (default: 1)");
36 static unsigned int max_channels
;
37 module_param(max_channels
, uint
, S_IRUGO
);
38 MODULE_PARM_DESC(max_channels
,
39 "Maximum number of channels to use (default: all)");
42 * Initialization patterns. All bytes in the source buffer has bit 7
43 * set, all bytes in the destination buffer has bit 7 cleared.
45 * Bit 6 is set for all bytes which are to be copied by the DMA
46 * engine. Bit 5 is set for all bytes which are to be overwritten by
49 * The remaining bits are the inverse of a counter which increments by
50 * one for each byte address.
52 #define PATTERN_SRC 0x80
53 #define PATTERN_DST 0x00
54 #define PATTERN_COPY 0x40
55 #define PATTERN_OVERWRITE 0x20
56 #define PATTERN_COUNT_MASK 0x1f
58 struct dmatest_thread
{
59 struct list_head node
;
60 struct task_struct
*task
;
61 struct dma_chan
*chan
;
67 struct list_head node
;
68 struct dma_chan
*chan
;
69 struct list_head threads
;
73 * These are protected by dma_list_mutex since they're only used by
74 * the DMA filter function callback
76 static LIST_HEAD(dmatest_channels
);
77 static unsigned int nr_channels
;
79 static bool dmatest_match_channel(struct dma_chan
*chan
)
81 if (test_channel
[0] == '\0')
83 return strcmp(dma_chan_name(chan
), test_channel
) == 0;
86 static bool dmatest_match_device(struct dma_device
*device
)
88 if (test_device
[0] == '\0')
90 return strcmp(dev_name(device
->dev
), test_device
) == 0;
93 static unsigned long dmatest_random(void)
97 get_random_bytes(&buf
, sizeof(buf
));
101 static void dmatest_init_srcbuf(u8
*buf
, unsigned int start
, unsigned int len
)
105 for (i
= 0; i
< start
; i
++)
106 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
107 for ( ; i
< start
+ len
; i
++)
108 buf
[i
] = PATTERN_SRC
| PATTERN_COPY
109 | (~i
& PATTERN_COUNT_MASK
);;
110 for ( ; i
< test_buf_size
; i
++)
111 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
114 static void dmatest_init_dstbuf(u8
*buf
, unsigned int start
, unsigned int len
)
118 for (i
= 0; i
< start
; i
++)
119 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
120 for ( ; i
< start
+ len
; i
++)
121 buf
[i
] = PATTERN_DST
| PATTERN_OVERWRITE
122 | (~i
& PATTERN_COUNT_MASK
);
123 for ( ; i
< test_buf_size
; i
++)
124 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
127 static void dmatest_mismatch(u8 actual
, u8 pattern
, unsigned int index
,
128 unsigned int counter
, bool is_srcbuf
)
130 u8 diff
= actual
^ pattern
;
131 u8 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
132 const char *thread_name
= current
->comm
;
135 pr_warning("%s: srcbuf[0x%x] overwritten!"
136 " Expected %02x, got %02x\n",
137 thread_name
, index
, expected
, actual
);
138 else if ((pattern
& PATTERN_COPY
)
139 && (diff
& (PATTERN_COPY
| PATTERN_OVERWRITE
)))
140 pr_warning("%s: dstbuf[0x%x] not copied!"
141 " Expected %02x, got %02x\n",
142 thread_name
, index
, expected
, actual
);
143 else if (diff
& PATTERN_SRC
)
144 pr_warning("%s: dstbuf[0x%x] was copied!"
145 " Expected %02x, got %02x\n",
146 thread_name
, index
, expected
, actual
);
148 pr_warning("%s: dstbuf[0x%x] mismatch!"
149 " Expected %02x, got %02x\n",
150 thread_name
, index
, expected
, actual
);
153 static unsigned int dmatest_verify(u8
*buf
, unsigned int start
,
154 unsigned int end
, unsigned int counter
, u8 pattern
,
158 unsigned int error_count
= 0;
161 for (i
= start
; i
< end
; i
++) {
163 if (actual
!= (pattern
| (~counter
& PATTERN_COUNT_MASK
))) {
164 if (error_count
< 32)
165 dmatest_mismatch(actual
, pattern
, i
, counter
,
172 if (error_count
> 32)
173 pr_warning("%s: %u errors suppressed\n",
174 current
->comm
, error_count
- 32);
180 * This function repeatedly tests DMA transfers of various lengths and
181 * offsets until it is told to exit by kthread_stop(). There may be
182 * multiple threads running this function in parallel for a single
183 * channel, and there may be multiple channels being tested in
186 * Before each test, the source and destination buffer is initialized
187 * with a known pattern. This pattern is different depending on
188 * whether it's in an area which is supposed to be copied or
189 * overwritten, and different in the source and destination buffers.
190 * So if the DMA engine doesn't copy exactly what we tell it to copy,
193 static int dmatest_func(void *data
)
195 struct dmatest_thread
*thread
= data
;
196 struct dma_chan
*chan
;
197 const char *thread_name
;
198 unsigned int src_off
, dst_off
, len
;
199 unsigned int error_count
;
200 unsigned int failed_tests
= 0;
201 unsigned int total_tests
= 0;
203 enum dma_status status
;
206 thread_name
= current
->comm
;
209 thread
->srcbuf
= kmalloc(test_buf_size
, GFP_KERNEL
);
212 thread
->dstbuf
= kmalloc(test_buf_size
, GFP_KERNEL
);
219 while (!kthread_should_stop()) {
222 len
= dmatest_random() % test_buf_size
+ 1;
223 src_off
= dmatest_random() % (test_buf_size
- len
+ 1);
224 dst_off
= dmatest_random() % (test_buf_size
- len
+ 1);
226 dmatest_init_srcbuf(thread
->srcbuf
, src_off
, len
);
227 dmatest_init_dstbuf(thread
->dstbuf
, dst_off
, len
);
229 cookie
= dma_async_memcpy_buf_to_buf(chan
,
230 thread
->dstbuf
+ dst_off
,
231 thread
->srcbuf
+ src_off
,
233 if (dma_submit_error(cookie
)) {
234 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
235 "dst_off=0x%x len=0x%x\n",
236 thread_name
, total_tests
- 1, cookie
,
237 src_off
, dst_off
, len
);
242 dma_async_memcpy_issue_pending(chan
);
246 status
= dma_async_memcpy_complete(
247 chan
, cookie
, NULL
, NULL
);
248 } while (status
== DMA_IN_PROGRESS
);
250 if (status
== DMA_ERROR
) {
251 pr_warning("%s: #%u: error during copy\n",
252 thread_name
, total_tests
- 1);
259 pr_debug("%s: verifying source buffer...\n", thread_name
);
260 error_count
+= dmatest_verify(thread
->srcbuf
, 0, src_off
,
261 0, PATTERN_SRC
, true);
262 error_count
+= dmatest_verify(thread
->srcbuf
, src_off
,
263 src_off
+ len
, src_off
,
264 PATTERN_SRC
| PATTERN_COPY
, true);
265 error_count
+= dmatest_verify(thread
->srcbuf
, src_off
+ len
,
266 test_buf_size
, src_off
+ len
,
269 pr_debug("%s: verifying dest buffer...\n",
271 error_count
+= dmatest_verify(thread
->dstbuf
, 0, dst_off
,
272 0, PATTERN_DST
, false);
273 error_count
+= dmatest_verify(thread
->dstbuf
, dst_off
,
274 dst_off
+ len
, src_off
,
275 PATTERN_SRC
| PATTERN_COPY
, false);
276 error_count
+= dmatest_verify(thread
->dstbuf
, dst_off
+ len
,
277 test_buf_size
, dst_off
+ len
,
281 pr_warning("%s: #%u: %u errors with "
282 "src_off=0x%x dst_off=0x%x len=0x%x\n",
283 thread_name
, total_tests
- 1, error_count
,
284 src_off
, dst_off
, len
);
287 pr_debug("%s: #%u: No errors with "
288 "src_off=0x%x dst_off=0x%x len=0x%x\n",
289 thread_name
, total_tests
- 1,
290 src_off
, dst_off
, len
);
295 kfree(thread
->dstbuf
);
297 kfree(thread
->srcbuf
);
299 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
300 thread_name
, total_tests
, failed_tests
, ret
);
304 static void dmatest_cleanup_channel(struct dmatest_chan
*dtc
)
306 struct dmatest_thread
*thread
;
307 struct dmatest_thread
*_thread
;
310 list_for_each_entry_safe(thread
, _thread
, &dtc
->threads
, node
) {
311 ret
= kthread_stop(thread
->task
);
312 pr_debug("dmatest: thread %s exited with status %d\n",
313 thread
->task
->comm
, ret
);
314 list_del(&thread
->node
);
320 static int dmatest_add_channel(struct dma_chan
*chan
)
322 struct dmatest_chan
*dtc
;
323 struct dmatest_thread
*thread
;
326 dtc
= kmalloc(sizeof(struct dmatest_chan
), GFP_KERNEL
);
328 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan
));
333 INIT_LIST_HEAD(&dtc
->threads
);
335 for (i
= 0; i
< threads_per_chan
; i
++) {
336 thread
= kzalloc(sizeof(struct dmatest_thread
), GFP_KERNEL
);
338 pr_warning("dmatest: No memory for %s-test%u\n",
339 dma_chan_name(chan
), i
);
342 thread
->chan
= dtc
->chan
;
344 thread
->task
= kthread_run(dmatest_func
, thread
, "%s-test%u",
345 dma_chan_name(chan
), i
);
346 if (IS_ERR(thread
->task
)) {
347 pr_warning("dmatest: Failed to run thread %s-test%u\n",
348 dma_chan_name(chan
), i
);
353 /* srcbuf and dstbuf are allocated by the thread itself */
355 list_add_tail(&thread
->node
, &dtc
->threads
);
358 pr_info("dmatest: Started %u threads using %s\n", i
, dma_chan_name(chan
));
360 list_add_tail(&dtc
->node
, &dmatest_channels
);
366 static bool filter(struct dma_chan
*chan
, void *param
)
368 if (!dmatest_match_channel(chan
) || !dmatest_match_device(chan
->device
))
374 static int __init
dmatest_init(void)
377 struct dma_chan
*chan
;
381 dma_cap_set(DMA_MEMCPY
, mask
);
383 chan
= dma_request_channel(mask
, filter
, NULL
);
385 err
= dmatest_add_channel(chan
);
389 dma_release_channel(chan
);
390 break; /* add_channel failed, punt */
393 break; /* no more channels available */
394 if (max_channels
&& nr_channels
>= max_channels
)
395 break; /* we have all we need */
400 /* when compiled-in wait for drivers to load first */
401 late_initcall(dmatest_init
);
403 static void __exit
dmatest_exit(void)
405 struct dmatest_chan
*dtc
, *_dtc
;
407 list_for_each_entry_safe(dtc
, _dtc
, &dmatest_channels
, node
) {
408 list_del(&dtc
->node
);
409 dmatest_cleanup_channel(dtc
);
410 pr_debug("dmatest: dropped channel %s\n",
411 dma_chan_name(dtc
->chan
));
412 dma_release_channel(dtc
->chan
);
415 module_exit(dmatest_exit
);
417 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
418 MODULE_LICENSE("GPL v2");