Commit | Line | Data |
---|---|---|
4a776f0a HS |
1 | /* |
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> | |
b7f080cf | 11 | #include <linux/dma-mapping.h> |
4a776f0a | 12 | #include <linux/dmaengine.h> |
981ed70d | 13 | #include <linux/freezer.h> |
4a776f0a HS |
14 | #include <linux/init.h> |
15 | #include <linux/kthread.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/moduleparam.h> | |
18 | #include <linux/random.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
4a776f0a HS |
20 | #include <linux/wait.h> |
21 | ||
22 | static unsigned int test_buf_size = 16384; | |
23 | module_param(test_buf_size, uint, S_IRUGO); | |
24 | MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer"); | |
25 | ||
06190d84 | 26 | static char test_channel[20]; |
4a776f0a HS |
27 | module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO); |
28 | MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)"); | |
29 | ||
06190d84 | 30 | static char test_device[20]; |
4a776f0a HS |
31 | module_param_string(device, test_device, sizeof(test_device), S_IRUGO); |
32 | MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)"); | |
33 | ||
34 | static unsigned int threads_per_chan = 1; | |
35 | module_param(threads_per_chan, uint, S_IRUGO); | |
36 | MODULE_PARM_DESC(threads_per_chan, | |
37 | "Number of threads to start per channel (default: 1)"); | |
38 | ||
39 | static unsigned int max_channels; | |
40 | module_param(max_channels, uint, S_IRUGO); | |
33df8ca0 | 41 | MODULE_PARM_DESC(max_channels, |
4a776f0a HS |
42 | "Maximum number of channels to use (default: all)"); |
43 | ||
0a2ff57d NF |
44 | static unsigned int iterations; |
45 | module_param(iterations, uint, S_IRUGO); | |
46 | MODULE_PARM_DESC(iterations, | |
47 | "Iterations before stopping test (default: infinite)"); | |
48 | ||
b54d5cb9 DW |
49 | static unsigned int xor_sources = 3; |
50 | module_param(xor_sources, uint, S_IRUGO); | |
51 | MODULE_PARM_DESC(xor_sources, | |
52 | "Number of xor source buffers (default: 3)"); | |
53 | ||
58691d64 DW |
54 | static unsigned int pq_sources = 3; |
55 | module_param(pq_sources, uint, S_IRUGO); | |
56 | MODULE_PARM_DESC(pq_sources, | |
57 | "Number of p+q source buffers (default: 3)"); | |
58 | ||
d42efe6b VK |
59 | static int timeout = 3000; |
60 | module_param(timeout, uint, S_IRUGO); | |
85ee7a1d JP |
61 | MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), " |
62 | "Pass -1 for infinite timeout"); | |
d42efe6b | 63 | |
4a776f0a HS |
64 | /* |
65 | * Initialization patterns. All bytes in the source buffer has bit 7 | |
66 | * set, all bytes in the destination buffer has bit 7 cleared. | |
67 | * | |
68 | * Bit 6 is set for all bytes which are to be copied by the DMA | |
69 | * engine. Bit 5 is set for all bytes which are to be overwritten by | |
70 | * the DMA engine. | |
71 | * | |
72 | * The remaining bits are the inverse of a counter which increments by | |
73 | * one for each byte address. | |
74 | */ | |
75 | #define PATTERN_SRC 0x80 | |
76 | #define PATTERN_DST 0x00 | |
77 | #define PATTERN_COPY 0x40 | |
78 | #define PATTERN_OVERWRITE 0x20 | |
79 | #define PATTERN_COUNT_MASK 0x1f | |
80 | ||
81 | struct dmatest_thread { | |
82 | struct list_head node; | |
83 | struct task_struct *task; | |
84 | struct dma_chan *chan; | |
b54d5cb9 DW |
85 | u8 **srcs; |
86 | u8 **dsts; | |
87 | enum dma_transaction_type type; | |
4a776f0a HS |
88 | }; |
89 | ||
90 | struct dmatest_chan { | |
91 | struct list_head node; | |
92 | struct dma_chan *chan; | |
93 | struct list_head threads; | |
94 | }; | |
95 | ||
96 | /* | |
97 | * These are protected by dma_list_mutex since they're only used by | |
33df8ca0 | 98 | * the DMA filter function callback |
4a776f0a HS |
99 | */ |
100 | static LIST_HEAD(dmatest_channels); | |
101 | static unsigned int nr_channels; | |
102 | ||
103 | static bool dmatest_match_channel(struct dma_chan *chan) | |
104 | { | |
105 | if (test_channel[0] == '\0') | |
106 | return true; | |
41d5e59c | 107 | return strcmp(dma_chan_name(chan), test_channel) == 0; |
4a776f0a HS |
108 | } |
109 | ||
110 | static bool dmatest_match_device(struct dma_device *device) | |
111 | { | |
112 | if (test_device[0] == '\0') | |
113 | return true; | |
06190d84 | 114 | return strcmp(dev_name(device->dev), test_device) == 0; |
4a776f0a HS |
115 | } |
116 | ||
117 | static unsigned long dmatest_random(void) | |
118 | { | |
119 | unsigned long buf; | |
120 | ||
121 | get_random_bytes(&buf, sizeof(buf)); | |
122 | return buf; | |
123 | } | |
124 | ||
b54d5cb9 | 125 | static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len) |
4a776f0a HS |
126 | { |
127 | unsigned int i; | |
b54d5cb9 DW |
128 | u8 *buf; |
129 | ||
130 | for (; (buf = *bufs); bufs++) { | |
131 | for (i = 0; i < start; i++) | |
132 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); | |
133 | for ( ; i < start + len; i++) | |
134 | buf[i] = PATTERN_SRC | PATTERN_COPY | |
c019894e | 135 | | (~i & PATTERN_COUNT_MASK); |
b54d5cb9 DW |
136 | for ( ; i < test_buf_size; i++) |
137 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); | |
138 | buf++; | |
139 | } | |
4a776f0a HS |
140 | } |
141 | ||
b54d5cb9 | 142 | static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len) |
4a776f0a HS |
143 | { |
144 | unsigned int i; | |
b54d5cb9 DW |
145 | u8 *buf; |
146 | ||
147 | for (; (buf = *bufs); bufs++) { | |
148 | for (i = 0; i < start; i++) | |
149 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); | |
150 | for ( ; i < start + len; i++) | |
151 | buf[i] = PATTERN_DST | PATTERN_OVERWRITE | |
152 | | (~i & PATTERN_COUNT_MASK); | |
153 | for ( ; i < test_buf_size; i++) | |
154 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); | |
155 | } | |
4a776f0a HS |
156 | } |
157 | ||
158 | static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index, | |
159 | unsigned int counter, bool is_srcbuf) | |
160 | { | |
161 | u8 diff = actual ^ pattern; | |
162 | u8 expected = pattern | (~counter & PATTERN_COUNT_MASK); | |
163 | const char *thread_name = current->comm; | |
164 | ||
165 | if (is_srcbuf) | |
166 | pr_warning("%s: srcbuf[0x%x] overwritten!" | |
167 | " Expected %02x, got %02x\n", | |
168 | thread_name, index, expected, actual); | |
169 | else if ((pattern & PATTERN_COPY) | |
170 | && (diff & (PATTERN_COPY | PATTERN_OVERWRITE))) | |
171 | pr_warning("%s: dstbuf[0x%x] not copied!" | |
172 | " Expected %02x, got %02x\n", | |
173 | thread_name, index, expected, actual); | |
174 | else if (diff & PATTERN_SRC) | |
175 | pr_warning("%s: dstbuf[0x%x] was copied!" | |
176 | " Expected %02x, got %02x\n", | |
177 | thread_name, index, expected, actual); | |
178 | else | |
179 | pr_warning("%s: dstbuf[0x%x] mismatch!" | |
180 | " Expected %02x, got %02x\n", | |
181 | thread_name, index, expected, actual); | |
182 | } | |
183 | ||
b54d5cb9 | 184 | static unsigned int dmatest_verify(u8 **bufs, unsigned int start, |
4a776f0a HS |
185 | unsigned int end, unsigned int counter, u8 pattern, |
186 | bool is_srcbuf) | |
187 | { | |
188 | unsigned int i; | |
189 | unsigned int error_count = 0; | |
190 | u8 actual; | |
b54d5cb9 DW |
191 | u8 expected; |
192 | u8 *buf; | |
193 | unsigned int counter_orig = counter; | |
194 | ||
195 | for (; (buf = *bufs); bufs++) { | |
196 | counter = counter_orig; | |
197 | for (i = start; i < end; i++) { | |
198 | actual = buf[i]; | |
199 | expected = pattern | (~counter & PATTERN_COUNT_MASK); | |
200 | if (actual != expected) { | |
201 | if (error_count < 32) | |
202 | dmatest_mismatch(actual, pattern, i, | |
203 | counter, is_srcbuf); | |
204 | error_count++; | |
205 | } | |
206 | counter++; | |
4a776f0a | 207 | } |
4a776f0a HS |
208 | } |
209 | ||
210 | if (error_count > 32) | |
211 | pr_warning("%s: %u errors suppressed\n", | |
212 | current->comm, error_count - 32); | |
213 | ||
214 | return error_count; | |
215 | } | |
216 | ||
adfa543e TH |
217 | /* poor man's completion - we want to use wait_event_freezable() on it */ |
218 | struct dmatest_done { | |
219 | bool done; | |
220 | wait_queue_head_t *wait; | |
221 | }; | |
222 | ||
223 | static void dmatest_callback(void *arg) | |
e44e0aa3 | 224 | { |
adfa543e TH |
225 | struct dmatest_done *done = arg; |
226 | ||
227 | done->done = true; | |
228 | wake_up_all(done->wait); | |
e44e0aa3 DW |
229 | } |
230 | ||
632fd283 AS |
231 | static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len, |
232 | unsigned int count) | |
233 | { | |
234 | while (count--) | |
235 | dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE); | |
236 | } | |
237 | ||
238 | static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len, | |
239 | unsigned int count) | |
240 | { | |
241 | while (count--) | |
242 | dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL); | |
243 | } | |
244 | ||
8be9e32b AM |
245 | static unsigned int min_odd(unsigned int x, unsigned int y) |
246 | { | |
247 | unsigned int val = min(x, y); | |
248 | ||
249 | return val % 2 ? val : val - 1; | |
250 | } | |
251 | ||
4a776f0a HS |
252 | /* |
253 | * This function repeatedly tests DMA transfers of various lengths and | |
b54d5cb9 DW |
254 | * offsets for a given operation type until it is told to exit by |
255 | * kthread_stop(). There may be multiple threads running this function | |
256 | * in parallel for a single channel, and there may be multiple channels | |
257 | * being tested in parallel. | |
4a776f0a HS |
258 | * |
259 | * Before each test, the source and destination buffer is initialized | |
260 | * with a known pattern. This pattern is different depending on | |
261 | * whether it's in an area which is supposed to be copied or | |
262 | * overwritten, and different in the source and destination buffers. | |
263 | * So if the DMA engine doesn't copy exactly what we tell it to copy, | |
264 | * we'll notice. | |
265 | */ | |
266 | static int dmatest_func(void *data) | |
267 | { | |
adfa543e | 268 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait); |
4a776f0a | 269 | struct dmatest_thread *thread = data; |
adfa543e | 270 | struct dmatest_done done = { .wait = &done_wait }; |
4a776f0a | 271 | struct dma_chan *chan; |
8be9e32b | 272 | struct dma_device *dev; |
4a776f0a HS |
273 | const char *thread_name; |
274 | unsigned int src_off, dst_off, len; | |
275 | unsigned int error_count; | |
276 | unsigned int failed_tests = 0; | |
277 | unsigned int total_tests = 0; | |
278 | dma_cookie_t cookie; | |
279 | enum dma_status status; | |
b54d5cb9 | 280 | enum dma_ctrl_flags flags; |
94de648d | 281 | u8 pq_coefs[pq_sources + 1]; |
4a776f0a | 282 | int ret; |
b54d5cb9 DW |
283 | int src_cnt; |
284 | int dst_cnt; | |
285 | int i; | |
4a776f0a HS |
286 | |
287 | thread_name = current->comm; | |
adfa543e | 288 | set_freezable(); |
4a776f0a HS |
289 | |
290 | ret = -ENOMEM; | |
4a776f0a HS |
291 | |
292 | smp_rmb(); | |
293 | chan = thread->chan; | |
8be9e32b | 294 | dev = chan->device; |
b54d5cb9 DW |
295 | if (thread->type == DMA_MEMCPY) |
296 | src_cnt = dst_cnt = 1; | |
297 | else if (thread->type == DMA_XOR) { | |
8be9e32b AM |
298 | /* force odd to ensure dst = src */ |
299 | src_cnt = min_odd(xor_sources | 1, dev->max_xor); | |
b54d5cb9 | 300 | dst_cnt = 1; |
58691d64 | 301 | } else if (thread->type == DMA_PQ) { |
8be9e32b AM |
302 | /* force odd to ensure dst = src */ |
303 | src_cnt = min_odd(pq_sources | 1, dma_maxpq(dev, 0)); | |
58691d64 | 304 | dst_cnt = 2; |
94de648d | 305 | for (i = 0; i < src_cnt; i++) |
58691d64 | 306 | pq_coefs[i] = 1; |
b54d5cb9 DW |
307 | } else |
308 | goto err_srcs; | |
309 | ||
310 | thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL); | |
311 | if (!thread->srcs) | |
312 | goto err_srcs; | |
313 | for (i = 0; i < src_cnt; i++) { | |
314 | thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL); | |
315 | if (!thread->srcs[i]) | |
316 | goto err_srcbuf; | |
317 | } | |
318 | thread->srcs[i] = NULL; | |
319 | ||
320 | thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL); | |
321 | if (!thread->dsts) | |
322 | goto err_dsts; | |
323 | for (i = 0; i < dst_cnt; i++) { | |
324 | thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL); | |
325 | if (!thread->dsts[i]) | |
326 | goto err_dstbuf; | |
327 | } | |
328 | thread->dsts[i] = NULL; | |
329 | ||
e44e0aa3 DW |
330 | set_user_nice(current, 10); |
331 | ||
b203bd3f IS |
332 | /* |
333 | * src buffers are freed by the DMAEngine code with dma_unmap_single() | |
334 | * dst buffers are freed by ourselves below | |
335 | */ | |
336 | flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | |
337 | | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE; | |
4a776f0a | 338 | |
0a2ff57d NF |
339 | while (!kthread_should_stop() |
340 | && !(iterations && total_tests >= iterations)) { | |
b54d5cb9 DW |
341 | struct dma_async_tx_descriptor *tx = NULL; |
342 | dma_addr_t dma_srcs[src_cnt]; | |
343 | dma_addr_t dma_dsts[dst_cnt]; | |
83544ae9 | 344 | u8 align = 0; |
d86be86e | 345 | |
4a776f0a HS |
346 | total_tests++; |
347 | ||
83544ae9 DW |
348 | /* honor alignment restrictions */ |
349 | if (thread->type == DMA_MEMCPY) | |
350 | align = dev->copy_align; | |
351 | else if (thread->type == DMA_XOR) | |
352 | align = dev->xor_align; | |
353 | else if (thread->type == DMA_PQ) | |
354 | align = dev->pq_align; | |
355 | ||
cfe4f275 GL |
356 | if (1 << align > test_buf_size) { |
357 | pr_err("%u-byte buffer too small for %d-byte alignment\n", | |
358 | test_buf_size, 1 << align); | |
359 | break; | |
360 | } | |
361 | ||
362 | len = dmatest_random() % test_buf_size + 1; | |
83544ae9 | 363 | len = (len >> align) << align; |
cfe4f275 GL |
364 | if (!len) |
365 | len = 1 << align; | |
366 | src_off = dmatest_random() % (test_buf_size - len + 1); | |
367 | dst_off = dmatest_random() % (test_buf_size - len + 1); | |
368 | ||
83544ae9 DW |
369 | src_off = (src_off >> align) << align; |
370 | dst_off = (dst_off >> align) << align; | |
371 | ||
b54d5cb9 DW |
372 | dmatest_init_srcs(thread->srcs, src_off, len); |
373 | dmatest_init_dsts(thread->dsts, dst_off, len); | |
4a776f0a | 374 | |
b54d5cb9 DW |
375 | for (i = 0; i < src_cnt; i++) { |
376 | u8 *buf = thread->srcs[i] + src_off; | |
377 | ||
378 | dma_srcs[i] = dma_map_single(dev->dev, buf, len, | |
379 | DMA_TO_DEVICE); | |
afde3be1 AS |
380 | ret = dma_mapping_error(dev->dev, dma_srcs[i]); |
381 | if (ret) { | |
382 | unmap_src(dev->dev, dma_srcs, len, i); | |
383 | pr_warn("%s: #%u: mapping error %d with " | |
384 | "src_off=0x%x len=0x%x\n", | |
385 | thread_name, total_tests - 1, ret, | |
386 | src_off, len); | |
387 | failed_tests++; | |
388 | continue; | |
389 | } | |
b54d5cb9 | 390 | } |
d86be86e | 391 | /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */ |
b54d5cb9 DW |
392 | for (i = 0; i < dst_cnt; i++) { |
393 | dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i], | |
394 | test_buf_size, | |
395 | DMA_BIDIRECTIONAL); | |
afde3be1 AS |
396 | ret = dma_mapping_error(dev->dev, dma_dsts[i]); |
397 | if (ret) { | |
398 | unmap_src(dev->dev, dma_srcs, len, src_cnt); | |
399 | unmap_dst(dev->dev, dma_dsts, test_buf_size, i); | |
400 | pr_warn("%s: #%u: mapping error %d with " | |
401 | "dst_off=0x%x len=0x%x\n", | |
402 | thread_name, total_tests - 1, ret, | |
403 | dst_off, test_buf_size); | |
404 | failed_tests++; | |
405 | continue; | |
406 | } | |
b54d5cb9 DW |
407 | } |
408 | ||
409 | if (thread->type == DMA_MEMCPY) | |
410 | tx = dev->device_prep_dma_memcpy(chan, | |
411 | dma_dsts[0] + dst_off, | |
412 | dma_srcs[0], len, | |
413 | flags); | |
414 | else if (thread->type == DMA_XOR) | |
415 | tx = dev->device_prep_dma_xor(chan, | |
416 | dma_dsts[0] + dst_off, | |
67b9124f | 417 | dma_srcs, src_cnt, |
b54d5cb9 | 418 | len, flags); |
58691d64 DW |
419 | else if (thread->type == DMA_PQ) { |
420 | dma_addr_t dma_pq[dst_cnt]; | |
421 | ||
422 | for (i = 0; i < dst_cnt; i++) | |
423 | dma_pq[i] = dma_dsts[i] + dst_off; | |
424 | tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs, | |
94de648d | 425 | src_cnt, pq_coefs, |
58691d64 DW |
426 | len, flags); |
427 | } | |
d86be86e | 428 | |
d86be86e | 429 | if (!tx) { |
632fd283 AS |
430 | unmap_src(dev->dev, dma_srcs, len, src_cnt); |
431 | unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt); | |
d86be86e AN |
432 | pr_warning("%s: #%u: prep error with src_off=0x%x " |
433 | "dst_off=0x%x len=0x%x\n", | |
434 | thread_name, total_tests - 1, | |
435 | src_off, dst_off, len); | |
436 | msleep(100); | |
437 | failed_tests++; | |
438 | continue; | |
439 | } | |
e44e0aa3 | 440 | |
adfa543e | 441 | done.done = false; |
e44e0aa3 | 442 | tx->callback = dmatest_callback; |
adfa543e | 443 | tx->callback_param = &done; |
d86be86e AN |
444 | cookie = tx->tx_submit(tx); |
445 | ||
4a776f0a HS |
446 | if (dma_submit_error(cookie)) { |
447 | pr_warning("%s: #%u: submit error %d with src_off=0x%x " | |
448 | "dst_off=0x%x len=0x%x\n", | |
449 | thread_name, total_tests - 1, cookie, | |
450 | src_off, dst_off, len); | |
451 | msleep(100); | |
452 | failed_tests++; | |
453 | continue; | |
454 | } | |
b54d5cb9 | 455 | dma_async_issue_pending(chan); |
4a776f0a | 456 | |
adfa543e TH |
457 | wait_event_freezable_timeout(done_wait, done.done, |
458 | msecs_to_jiffies(timeout)); | |
981ed70d | 459 | |
e44e0aa3 | 460 | status = dma_async_is_tx_complete(chan, cookie, NULL, NULL); |
4a776f0a | 461 | |
adfa543e TH |
462 | if (!done.done) { |
463 | /* | |
464 | * We're leaving the timed out dma operation with | |
465 | * dangling pointer to done_wait. To make this | |
466 | * correct, we'll need to allocate wait_done for | |
467 | * each test iteration and perform "who's gonna | |
468 | * free it this time?" dancing. For now, just | |
469 | * leave it dangling. | |
470 | */ | |
e44e0aa3 DW |
471 | pr_warning("%s: #%u: test timed out\n", |
472 | thread_name, total_tests - 1); | |
473 | failed_tests++; | |
474 | continue; | |
475 | } else if (status != DMA_SUCCESS) { | |
476 | pr_warning("%s: #%u: got completion callback," | |
477 | " but status is \'%s\'\n", | |
478 | thread_name, total_tests - 1, | |
479 | status == DMA_ERROR ? "error" : "in progress"); | |
4a776f0a HS |
480 | failed_tests++; |
481 | continue; | |
482 | } | |
e44e0aa3 | 483 | |
d86be86e | 484 | /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */ |
632fd283 | 485 | unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt); |
4a776f0a HS |
486 | |
487 | error_count = 0; | |
488 | ||
489 | pr_debug("%s: verifying source buffer...\n", thread_name); | |
b54d5cb9 | 490 | error_count += dmatest_verify(thread->srcs, 0, src_off, |
4a776f0a | 491 | 0, PATTERN_SRC, true); |
b54d5cb9 | 492 | error_count += dmatest_verify(thread->srcs, src_off, |
4a776f0a HS |
493 | src_off + len, src_off, |
494 | PATTERN_SRC | PATTERN_COPY, true); | |
b54d5cb9 | 495 | error_count += dmatest_verify(thread->srcs, src_off + len, |
4a776f0a HS |
496 | test_buf_size, src_off + len, |
497 | PATTERN_SRC, true); | |
498 | ||
499 | pr_debug("%s: verifying dest buffer...\n", | |
500 | thread->task->comm); | |
b54d5cb9 | 501 | error_count += dmatest_verify(thread->dsts, 0, dst_off, |
4a776f0a | 502 | 0, PATTERN_DST, false); |
b54d5cb9 | 503 | error_count += dmatest_verify(thread->dsts, dst_off, |
4a776f0a HS |
504 | dst_off + len, src_off, |
505 | PATTERN_SRC | PATTERN_COPY, false); | |
b54d5cb9 | 506 | error_count += dmatest_verify(thread->dsts, dst_off + len, |
4a776f0a HS |
507 | test_buf_size, dst_off + len, |
508 | PATTERN_DST, false); | |
509 | ||
510 | if (error_count) { | |
511 | pr_warning("%s: #%u: %u errors with " | |
512 | "src_off=0x%x dst_off=0x%x len=0x%x\n", | |
513 | thread_name, total_tests - 1, error_count, | |
514 | src_off, dst_off, len); | |
515 | failed_tests++; | |
516 | } else { | |
517 | pr_debug("%s: #%u: No errors with " | |
518 | "src_off=0x%x dst_off=0x%x len=0x%x\n", | |
519 | thread_name, total_tests - 1, | |
520 | src_off, dst_off, len); | |
521 | } | |
522 | } | |
523 | ||
524 | ret = 0; | |
b54d5cb9 DW |
525 | for (i = 0; thread->dsts[i]; i++) |
526 | kfree(thread->dsts[i]); | |
4a776f0a | 527 | err_dstbuf: |
b54d5cb9 DW |
528 | kfree(thread->dsts); |
529 | err_dsts: | |
530 | for (i = 0; thread->srcs[i]; i++) | |
531 | kfree(thread->srcs[i]); | |
4a776f0a | 532 | err_srcbuf: |
b54d5cb9 DW |
533 | kfree(thread->srcs); |
534 | err_srcs: | |
4a776f0a HS |
535 | pr_notice("%s: terminating after %u tests, %u failures (status %d)\n", |
536 | thread_name, total_tests, failed_tests, ret); | |
0a2ff57d | 537 | |
9704efaa | 538 | /* terminate all transfers on specified channels */ |
5e034f7b SH |
539 | if (ret) |
540 | dmaengine_terminate_all(chan); | |
541 | ||
0a2ff57d NF |
542 | if (iterations > 0) |
543 | while (!kthread_should_stop()) { | |
b953df7c | 544 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit); |
0a2ff57d NF |
545 | interruptible_sleep_on(&wait_dmatest_exit); |
546 | } | |
547 | ||
4a776f0a HS |
548 | return ret; |
549 | } | |
550 | ||
551 | static void dmatest_cleanup_channel(struct dmatest_chan *dtc) | |
552 | { | |
553 | struct dmatest_thread *thread; | |
554 | struct dmatest_thread *_thread; | |
555 | int ret; | |
556 | ||
557 | list_for_each_entry_safe(thread, _thread, &dtc->threads, node) { | |
558 | ret = kthread_stop(thread->task); | |
559 | pr_debug("dmatest: thread %s exited with status %d\n", | |
560 | thread->task->comm, ret); | |
561 | list_del(&thread->node); | |
562 | kfree(thread); | |
563 | } | |
9704efaa VK |
564 | |
565 | /* terminate all transfers on specified channels */ | |
944ea4dd | 566 | dmaengine_terminate_all(dtc->chan); |
9704efaa | 567 | |
4a776f0a HS |
568 | kfree(dtc); |
569 | } | |
570 | ||
b54d5cb9 | 571 | static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type) |
4a776f0a | 572 | { |
b54d5cb9 DW |
573 | struct dmatest_thread *thread; |
574 | struct dma_chan *chan = dtc->chan; | |
575 | char *op; | |
576 | unsigned int i; | |
4a776f0a | 577 | |
b54d5cb9 DW |
578 | if (type == DMA_MEMCPY) |
579 | op = "copy"; | |
580 | else if (type == DMA_XOR) | |
581 | op = "xor"; | |
58691d64 DW |
582 | else if (type == DMA_PQ) |
583 | op = "pq"; | |
b54d5cb9 DW |
584 | else |
585 | return -EINVAL; | |
4a776f0a HS |
586 | |
587 | for (i = 0; i < threads_per_chan; i++) { | |
588 | thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL); | |
589 | if (!thread) { | |
b54d5cb9 DW |
590 | pr_warning("dmatest: No memory for %s-%s%u\n", |
591 | dma_chan_name(chan), op, i); | |
592 | ||
4a776f0a HS |
593 | break; |
594 | } | |
595 | thread->chan = dtc->chan; | |
b54d5cb9 | 596 | thread->type = type; |
4a776f0a | 597 | smp_wmb(); |
b54d5cb9 DW |
598 | thread->task = kthread_run(dmatest_func, thread, "%s-%s%u", |
599 | dma_chan_name(chan), op, i); | |
4a776f0a | 600 | if (IS_ERR(thread->task)) { |
b54d5cb9 DW |
601 | pr_warning("dmatest: Failed to run thread %s-%s%u\n", |
602 | dma_chan_name(chan), op, i); | |
4a776f0a HS |
603 | kfree(thread); |
604 | break; | |
605 | } | |
606 | ||
607 | /* srcbuf and dstbuf are allocated by the thread itself */ | |
608 | ||
609 | list_add_tail(&thread->node, &dtc->threads); | |
610 | } | |
611 | ||
b54d5cb9 DW |
612 | return i; |
613 | } | |
614 | ||
615 | static int dmatest_add_channel(struct dma_chan *chan) | |
616 | { | |
617 | struct dmatest_chan *dtc; | |
618 | struct dma_device *dma_dev = chan->device; | |
619 | unsigned int thread_count = 0; | |
b9033e68 | 620 | int cnt; |
b54d5cb9 DW |
621 | |
622 | dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL); | |
623 | if (!dtc) { | |
624 | pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan)); | |
625 | return -ENOMEM; | |
626 | } | |
627 | ||
628 | dtc->chan = chan; | |
629 | INIT_LIST_HEAD(&dtc->threads); | |
630 | ||
631 | if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { | |
632 | cnt = dmatest_add_threads(dtc, DMA_MEMCPY); | |
f1aef8b6 | 633 | thread_count += cnt > 0 ? cnt : 0; |
b54d5cb9 DW |
634 | } |
635 | if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { | |
636 | cnt = dmatest_add_threads(dtc, DMA_XOR); | |
f1aef8b6 | 637 | thread_count += cnt > 0 ? cnt : 0; |
b54d5cb9 | 638 | } |
58691d64 DW |
639 | if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) { |
640 | cnt = dmatest_add_threads(dtc, DMA_PQ); | |
d07a74a5 | 641 | thread_count += cnt > 0 ? cnt : 0; |
58691d64 | 642 | } |
b54d5cb9 DW |
643 | |
644 | pr_info("dmatest: Started %u threads using %s\n", | |
645 | thread_count, dma_chan_name(chan)); | |
4a776f0a HS |
646 | |
647 | list_add_tail(&dtc->node, &dmatest_channels); | |
648 | nr_channels++; | |
649 | ||
33df8ca0 | 650 | return 0; |
4a776f0a HS |
651 | } |
652 | ||
7dd60251 | 653 | static bool filter(struct dma_chan *chan, void *param) |
4a776f0a | 654 | { |
33df8ca0 | 655 | if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device)) |
7dd60251 | 656 | return false; |
33df8ca0 | 657 | else |
7dd60251 | 658 | return true; |
4a776f0a HS |
659 | } |
660 | ||
4a776f0a HS |
661 | static int __init dmatest_init(void) |
662 | { | |
33df8ca0 DW |
663 | dma_cap_mask_t mask; |
664 | struct dma_chan *chan; | |
665 | int err = 0; | |
666 | ||
667 | dma_cap_zero(mask); | |
668 | dma_cap_set(DMA_MEMCPY, mask); | |
669 | for (;;) { | |
670 | chan = dma_request_channel(mask, filter, NULL); | |
671 | if (chan) { | |
672 | err = dmatest_add_channel(chan); | |
c56c81ab | 673 | if (err) { |
33df8ca0 DW |
674 | dma_release_channel(chan); |
675 | break; /* add_channel failed, punt */ | |
676 | } | |
677 | } else | |
678 | break; /* no more channels available */ | |
679 | if (max_channels && nr_channels >= max_channels) | |
680 | break; /* we have all we need */ | |
681 | } | |
4a776f0a | 682 | |
33df8ca0 | 683 | return err; |
4a776f0a | 684 | } |
33df8ca0 DW |
685 | /* when compiled-in wait for drivers to load first */ |
686 | late_initcall(dmatest_init); | |
4a776f0a HS |
687 | |
688 | static void __exit dmatest_exit(void) | |
689 | { | |
33df8ca0 | 690 | struct dmatest_chan *dtc, *_dtc; |
7cbd4877 | 691 | struct dma_chan *chan; |
33df8ca0 DW |
692 | |
693 | list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) { | |
694 | list_del(&dtc->node); | |
7cbd4877 | 695 | chan = dtc->chan; |
33df8ca0 DW |
696 | dmatest_cleanup_channel(dtc); |
697 | pr_debug("dmatest: dropped channel %s\n", | |
7cbd4877 DW |
698 | dma_chan_name(chan)); |
699 | dma_release_channel(chan); | |
33df8ca0 | 700 | } |
4a776f0a HS |
701 | } |
702 | module_exit(dmatest_exit); | |
703 | ||
e05503ef | 704 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
4a776f0a | 705 | MODULE_LICENSE("GPL v2"); |