2 * DMA Engine test module
4 * Copyright (C) 2007 Atmel Corporation
5 * Copyright (C) 2013 Intel Corporation
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.
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmaengine.h>
14 #include <linux/freezer.h>
15 #include <linux/init.h>
16 #include <linux/kthread.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/random.h>
20 #include <linux/slab.h>
21 #include <linux/wait.h>
22 #include <linux/ctype.h>
23 #include <linux/debugfs.h>
24 #include <linux/uaccess.h>
25 #include <linux/seq_file.h>
27 static unsigned int test_buf_size
= 16384;
28 module_param(test_buf_size
, uint
, S_IRUGO
);
29 MODULE_PARM_DESC(test_buf_size
, "Size of the memcpy test buffer");
31 static char test_channel
[20];
32 module_param_string(channel
, test_channel
, sizeof(test_channel
), S_IRUGO
);
33 MODULE_PARM_DESC(channel
, "Bus ID of the channel to test (default: any)");
35 static char test_device
[20];
36 module_param_string(device
, test_device
, sizeof(test_device
), S_IRUGO
);
37 MODULE_PARM_DESC(device
, "Bus ID of the DMA Engine to test (default: any)");
39 static unsigned int threads_per_chan
= 1;
40 module_param(threads_per_chan
, uint
, S_IRUGO
);
41 MODULE_PARM_DESC(threads_per_chan
,
42 "Number of threads to start per channel (default: 1)");
44 static unsigned int max_channels
;
45 module_param(max_channels
, uint
, S_IRUGO
);
46 MODULE_PARM_DESC(max_channels
,
47 "Maximum number of channels to use (default: all)");
49 static unsigned int iterations
;
50 module_param(iterations
, uint
, S_IRUGO
);
51 MODULE_PARM_DESC(iterations
,
52 "Iterations before stopping test (default: infinite)");
54 static unsigned int xor_sources
= 3;
55 module_param(xor_sources
, uint
, S_IRUGO
);
56 MODULE_PARM_DESC(xor_sources
,
57 "Number of xor source buffers (default: 3)");
59 static unsigned int pq_sources
= 3;
60 module_param(pq_sources
, uint
, S_IRUGO
);
61 MODULE_PARM_DESC(pq_sources
,
62 "Number of p+q source buffers (default: 3)");
64 static int timeout
= 3000;
65 module_param(timeout
, uint
, S_IRUGO
);
66 MODULE_PARM_DESC(timeout
, "Transfer Timeout in msec (default: 3000), "
67 "Pass -1 for infinite timeout");
70 * Initialization patterns. All bytes in the source buffer has bit 7
71 * set, all bytes in the destination buffer has bit 7 cleared.
73 * Bit 6 is set for all bytes which are to be copied by the DMA
74 * engine. Bit 5 is set for all bytes which are to be overwritten by
77 * The remaining bits are the inverse of a counter which increments by
78 * one for each byte address.
80 #define PATTERN_SRC 0x80
81 #define PATTERN_DST 0x00
82 #define PATTERN_COPY 0x40
83 #define PATTERN_OVERWRITE 0x20
84 #define PATTERN_COUNT_MASK 0x1f
88 struct dmatest_thread
{
89 struct list_head node
;
90 struct dmatest_info
*info
;
91 struct task_struct
*task
;
92 struct dma_chan
*chan
;
95 enum dma_transaction_type type
;
100 struct list_head node
;
101 struct dma_chan
*chan
;
102 struct list_head threads
;
106 * struct dmatest_params - test parameters.
107 * @buf_size: size of the memcpy test buffer
108 * @channel: bus ID of the channel to test
109 * @device: bus ID of the DMA Engine to test
110 * @threads_per_chan: number of threads to start per channel
111 * @max_channels: maximum number of channels to use
112 * @iterations: iterations before stopping test
113 * @xor_sources: number of xor source buffers
114 * @pq_sources: number of p+q source buffers
115 * @timeout: transfer timeout in msec, -1 for infinite timeout
117 struct dmatest_params
{
118 unsigned int buf_size
;
121 unsigned int threads_per_chan
;
122 unsigned int max_channels
;
123 unsigned int iterations
;
124 unsigned int xor_sources
;
125 unsigned int pq_sources
;
130 * struct dmatest_info - test information.
131 * @params: test parameters
132 * @lock: access protection to the fields of this structure
134 struct dmatest_info
{
135 /* Test parameters */
136 struct dmatest_params params
;
139 struct list_head channels
;
140 unsigned int nr_channels
;
143 /* debugfs related stuff */
145 struct dmatest_params dbgfs_params
;
148 static struct dmatest_info test_info
;
150 static bool dmatest_match_channel(struct dmatest_params
*params
,
151 struct dma_chan
*chan
)
153 if (params
->channel
[0] == '\0')
155 return strcmp(dma_chan_name(chan
), params
->channel
) == 0;
158 static bool dmatest_match_device(struct dmatest_params
*params
,
159 struct dma_device
*device
)
161 if (params
->device
[0] == '\0')
163 return strcmp(dev_name(device
->dev
), params
->device
) == 0;
166 static unsigned long dmatest_random(void)
170 get_random_bytes(&buf
, sizeof(buf
));
174 static void dmatest_init_srcs(u8
**bufs
, unsigned int start
, unsigned int len
,
175 unsigned int buf_size
)
180 for (; (buf
= *bufs
); bufs
++) {
181 for (i
= 0; i
< start
; i
++)
182 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
183 for ( ; i
< start
+ len
; i
++)
184 buf
[i
] = PATTERN_SRC
| PATTERN_COPY
185 | (~i
& PATTERN_COUNT_MASK
);
186 for ( ; i
< buf_size
; i
++)
187 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
192 static void dmatest_init_dsts(u8
**bufs
, unsigned int start
, unsigned int len
,
193 unsigned int buf_size
)
198 for (; (buf
= *bufs
); bufs
++) {
199 for (i
= 0; i
< start
; i
++)
200 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
201 for ( ; i
< start
+ len
; i
++)
202 buf
[i
] = PATTERN_DST
| PATTERN_OVERWRITE
203 | (~i
& PATTERN_COUNT_MASK
);
204 for ( ; i
< buf_size
; i
++)
205 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
209 static void dmatest_mismatch(u8 actual
, u8 pattern
, unsigned int index
,
210 unsigned int counter
, bool is_srcbuf
)
212 u8 diff
= actual
^ pattern
;
213 u8 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
214 const char *thread_name
= current
->comm
;
217 pr_warning("%s: srcbuf[0x%x] overwritten!"
218 " Expected %02x, got %02x\n",
219 thread_name
, index
, expected
, actual
);
220 else if ((pattern
& PATTERN_COPY
)
221 && (diff
& (PATTERN_COPY
| PATTERN_OVERWRITE
)))
222 pr_warning("%s: dstbuf[0x%x] not copied!"
223 " Expected %02x, got %02x\n",
224 thread_name
, index
, expected
, actual
);
225 else if (diff
& PATTERN_SRC
)
226 pr_warning("%s: dstbuf[0x%x] was copied!"
227 " Expected %02x, got %02x\n",
228 thread_name
, index
, expected
, actual
);
230 pr_warning("%s: dstbuf[0x%x] mismatch!"
231 " Expected %02x, got %02x\n",
232 thread_name
, index
, expected
, actual
);
235 static unsigned int dmatest_verify(u8
**bufs
, unsigned int start
,
236 unsigned int end
, unsigned int counter
, u8 pattern
,
240 unsigned int error_count
= 0;
244 unsigned int counter_orig
= counter
;
246 for (; (buf
= *bufs
); bufs
++) {
247 counter
= counter_orig
;
248 for (i
= start
; i
< end
; i
++) {
250 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
251 if (actual
!= expected
) {
252 if (error_count
< 32)
253 dmatest_mismatch(actual
, pattern
, i
,
261 if (error_count
> 32)
262 pr_warning("%s: %u errors suppressed\n",
263 current
->comm
, error_count
- 32);
268 /* poor man's completion - we want to use wait_event_freezable() on it */
269 struct dmatest_done
{
271 wait_queue_head_t
*wait
;
274 static void dmatest_callback(void *arg
)
276 struct dmatest_done
*done
= arg
;
279 wake_up_all(done
->wait
);
282 static inline void unmap_src(struct device
*dev
, dma_addr_t
*addr
, size_t len
,
286 dma_unmap_single(dev
, addr
[count
], len
, DMA_TO_DEVICE
);
289 static inline void unmap_dst(struct device
*dev
, dma_addr_t
*addr
, size_t len
,
293 dma_unmap_single(dev
, addr
[count
], len
, DMA_BIDIRECTIONAL
);
296 static unsigned int min_odd(unsigned int x
, unsigned int y
)
298 unsigned int val
= min(x
, y
);
300 return val
% 2 ? val
: val
- 1;
304 * This function repeatedly tests DMA transfers of various lengths and
305 * offsets for a given operation type until it is told to exit by
306 * kthread_stop(). There may be multiple threads running this function
307 * in parallel for a single channel, and there may be multiple channels
308 * being tested in parallel.
310 * Before each test, the source and destination buffer is initialized
311 * with a known pattern. This pattern is different depending on
312 * whether it's in an area which is supposed to be copied or
313 * overwritten, and different in the source and destination buffers.
314 * So if the DMA engine doesn't copy exactly what we tell it to copy,
317 static int dmatest_func(void *data
)
319 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait
);
320 struct dmatest_thread
*thread
= data
;
321 struct dmatest_done done
= { .wait
= &done_wait
};
322 struct dmatest_info
*info
;
323 struct dmatest_params
*params
;
324 struct dma_chan
*chan
;
325 struct dma_device
*dev
;
326 const char *thread_name
;
327 unsigned int src_off
, dst_off
, len
;
328 unsigned int error_count
;
329 unsigned int failed_tests
= 0;
330 unsigned int total_tests
= 0;
332 enum dma_status status
;
333 enum dma_ctrl_flags flags
;
340 thread_name
= current
->comm
;
347 params
= &info
->params
;
350 if (thread
->type
== DMA_MEMCPY
)
351 src_cnt
= dst_cnt
= 1;
352 else if (thread
->type
== DMA_XOR
) {
353 /* force odd to ensure dst = src */
354 src_cnt
= min_odd(params
->xor_sources
| 1, dev
->max_xor
);
356 } else if (thread
->type
== DMA_PQ
) {
357 /* force odd to ensure dst = src */
358 src_cnt
= min_odd(params
->pq_sources
| 1, dma_maxpq(dev
, 0));
361 pq_coefs
= kmalloc(params
->pq_sources
+1, GFP_KERNEL
);
363 goto err_thread_type
;
365 for (i
= 0; i
< src_cnt
; i
++)
368 goto err_thread_type
;
370 thread
->srcs
= kcalloc(src_cnt
+1, sizeof(u8
*), GFP_KERNEL
);
373 for (i
= 0; i
< src_cnt
; i
++) {
374 thread
->srcs
[i
] = kmalloc(params
->buf_size
, GFP_KERNEL
);
375 if (!thread
->srcs
[i
])
378 thread
->srcs
[i
] = NULL
;
380 thread
->dsts
= kcalloc(dst_cnt
+1, sizeof(u8
*), GFP_KERNEL
);
383 for (i
= 0; i
< dst_cnt
; i
++) {
384 thread
->dsts
[i
] = kmalloc(params
->buf_size
, GFP_KERNEL
);
385 if (!thread
->dsts
[i
])
388 thread
->dsts
[i
] = NULL
;
390 set_user_nice(current
, 10);
393 * src buffers are freed by the DMAEngine code with dma_unmap_single()
394 * dst buffers are freed by ourselves below
396 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
397 | DMA_COMPL_SKIP_DEST_UNMAP
| DMA_COMPL_SRC_UNMAP_SINGLE
;
399 while (!kthread_should_stop()
400 && !(params
->iterations
&& total_tests
>= params
->iterations
)) {
401 struct dma_async_tx_descriptor
*tx
= NULL
;
402 dma_addr_t dma_srcs
[src_cnt
];
403 dma_addr_t dma_dsts
[dst_cnt
];
408 /* honor alignment restrictions */
409 if (thread
->type
== DMA_MEMCPY
)
410 align
= dev
->copy_align
;
411 else if (thread
->type
== DMA_XOR
)
412 align
= dev
->xor_align
;
413 else if (thread
->type
== DMA_PQ
)
414 align
= dev
->pq_align
;
416 if (1 << align
> params
->buf_size
) {
417 pr_err("%u-byte buffer too small for %d-byte alignment\n",
418 params
->buf_size
, 1 << align
);
422 len
= dmatest_random() % params
->buf_size
+ 1;
423 len
= (len
>> align
) << align
;
426 src_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
427 dst_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
429 src_off
= (src_off
>> align
) << align
;
430 dst_off
= (dst_off
>> align
) << align
;
432 dmatest_init_srcs(thread
->srcs
, src_off
, len
, params
->buf_size
);
433 dmatest_init_dsts(thread
->dsts
, dst_off
, len
, params
->buf_size
);
435 for (i
= 0; i
< src_cnt
; i
++) {
436 u8
*buf
= thread
->srcs
[i
] + src_off
;
438 dma_srcs
[i
] = dma_map_single(dev
->dev
, buf
, len
,
440 ret
= dma_mapping_error(dev
->dev
, dma_srcs
[i
]);
442 unmap_src(dev
->dev
, dma_srcs
, len
, i
);
443 pr_warn("%s: #%u: mapping error %d with "
444 "src_off=0x%x len=0x%x\n",
445 thread_name
, total_tests
- 1, ret
,
451 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
452 for (i
= 0; i
< dst_cnt
; i
++) {
453 dma_dsts
[i
] = dma_map_single(dev
->dev
, thread
->dsts
[i
],
456 ret
= dma_mapping_error(dev
->dev
, dma_dsts
[i
]);
458 unmap_src(dev
->dev
, dma_srcs
, len
, src_cnt
);
459 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
,
461 pr_warn("%s: #%u: mapping error %d with "
462 "dst_off=0x%x len=0x%x\n",
463 thread_name
, total_tests
- 1, ret
,
464 dst_off
, params
->buf_size
);
470 if (thread
->type
== DMA_MEMCPY
)
471 tx
= dev
->device_prep_dma_memcpy(chan
,
472 dma_dsts
[0] + dst_off
,
475 else if (thread
->type
== DMA_XOR
)
476 tx
= dev
->device_prep_dma_xor(chan
,
477 dma_dsts
[0] + dst_off
,
480 else if (thread
->type
== DMA_PQ
) {
481 dma_addr_t dma_pq
[dst_cnt
];
483 for (i
= 0; i
< dst_cnt
; i
++)
484 dma_pq
[i
] = dma_dsts
[i
] + dst_off
;
485 tx
= dev
->device_prep_dma_pq(chan
, dma_pq
, dma_srcs
,
491 unmap_src(dev
->dev
, dma_srcs
, len
, src_cnt
);
492 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
,
494 pr_warning("%s: #%u: prep error with src_off=0x%x "
495 "dst_off=0x%x len=0x%x\n",
496 thread_name
, total_tests
- 1,
497 src_off
, dst_off
, len
);
504 tx
->callback
= dmatest_callback
;
505 tx
->callback_param
= &done
;
506 cookie
= tx
->tx_submit(tx
);
508 if (dma_submit_error(cookie
)) {
509 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
510 "dst_off=0x%x len=0x%x\n",
511 thread_name
, total_tests
- 1, cookie
,
512 src_off
, dst_off
, len
);
517 dma_async_issue_pending(chan
);
519 wait_event_freezable_timeout(done_wait
,
520 done
.done
|| kthread_should_stop(),
521 msecs_to_jiffies(params
->timeout
));
523 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
527 * We're leaving the timed out dma operation with
528 * dangling pointer to done_wait. To make this
529 * correct, we'll need to allocate wait_done for
530 * each test iteration and perform "who's gonna
531 * free it this time?" dancing. For now, just
534 pr_warning("%s: #%u: test timed out\n",
535 thread_name
, total_tests
- 1);
538 } else if (status
!= DMA_SUCCESS
) {
539 pr_warning("%s: #%u: got completion callback,"
540 " but status is \'%s\'\n",
541 thread_name
, total_tests
- 1,
542 status
== DMA_ERROR
? "error" : "in progress");
547 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
548 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
, dst_cnt
);
552 pr_debug("%s: verifying source buffer...\n", thread_name
);
553 error_count
+= dmatest_verify(thread
->srcs
, 0, src_off
,
554 0, PATTERN_SRC
, true);
555 error_count
+= dmatest_verify(thread
->srcs
, src_off
,
556 src_off
+ len
, src_off
,
557 PATTERN_SRC
| PATTERN_COPY
, true);
558 error_count
+= dmatest_verify(thread
->srcs
, src_off
+ len
,
559 params
->buf_size
, src_off
+ len
,
562 pr_debug("%s: verifying dest buffer...\n",
564 error_count
+= dmatest_verify(thread
->dsts
, 0, dst_off
,
565 0, PATTERN_DST
, false);
566 error_count
+= dmatest_verify(thread
->dsts
, dst_off
,
567 dst_off
+ len
, src_off
,
568 PATTERN_SRC
| PATTERN_COPY
, false);
569 error_count
+= dmatest_verify(thread
->dsts
, dst_off
+ len
,
570 params
->buf_size
, dst_off
+ len
,
574 pr_warning("%s: #%u: %u errors with "
575 "src_off=0x%x dst_off=0x%x len=0x%x\n",
576 thread_name
, total_tests
- 1, error_count
,
577 src_off
, dst_off
, len
);
580 pr_debug("%s: #%u: No errors with "
581 "src_off=0x%x dst_off=0x%x len=0x%x\n",
582 thread_name
, total_tests
- 1,
583 src_off
, dst_off
, len
);
588 for (i
= 0; thread
->dsts
[i
]; i
++)
589 kfree(thread
->dsts
[i
]);
593 for (i
= 0; thread
->srcs
[i
]; i
++)
594 kfree(thread
->srcs
[i
]);
600 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
601 thread_name
, total_tests
, failed_tests
, ret
);
603 /* terminate all transfers on specified channels */
605 dmaengine_terminate_all(chan
);
609 if (params
->iterations
> 0)
610 while (!kthread_should_stop()) {
611 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit
);
612 interruptible_sleep_on(&wait_dmatest_exit
);
618 static void dmatest_cleanup_channel(struct dmatest_chan
*dtc
)
620 struct dmatest_thread
*thread
;
621 struct dmatest_thread
*_thread
;
624 list_for_each_entry_safe(thread
, _thread
, &dtc
->threads
, node
) {
625 ret
= kthread_stop(thread
->task
);
626 pr_debug("dmatest: thread %s exited with status %d\n",
627 thread
->task
->comm
, ret
);
628 list_del(&thread
->node
);
632 /* terminate all transfers on specified channels */
633 dmaengine_terminate_all(dtc
->chan
);
638 static int dmatest_add_threads(struct dmatest_info
*info
,
639 struct dmatest_chan
*dtc
, enum dma_transaction_type type
)
641 struct dmatest_params
*params
= &info
->params
;
642 struct dmatest_thread
*thread
;
643 struct dma_chan
*chan
= dtc
->chan
;
647 if (type
== DMA_MEMCPY
)
649 else if (type
== DMA_XOR
)
651 else if (type
== DMA_PQ
)
656 for (i
= 0; i
< params
->threads_per_chan
; i
++) {
657 thread
= kzalloc(sizeof(struct dmatest_thread
), GFP_KERNEL
);
659 pr_warning("dmatest: No memory for %s-%s%u\n",
660 dma_chan_name(chan
), op
, i
);
665 thread
->chan
= dtc
->chan
;
668 thread
->task
= kthread_run(dmatest_func
, thread
, "%s-%s%u",
669 dma_chan_name(chan
), op
, i
);
670 if (IS_ERR(thread
->task
)) {
671 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
672 dma_chan_name(chan
), op
, i
);
677 /* srcbuf and dstbuf are allocated by the thread itself */
679 list_add_tail(&thread
->node
, &dtc
->threads
);
685 static int dmatest_add_channel(struct dmatest_info
*info
,
686 struct dma_chan
*chan
)
688 struct dmatest_chan
*dtc
;
689 struct dma_device
*dma_dev
= chan
->device
;
690 unsigned int thread_count
= 0;
693 dtc
= kmalloc(sizeof(struct dmatest_chan
), GFP_KERNEL
);
695 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan
));
700 INIT_LIST_HEAD(&dtc
->threads
);
702 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
703 cnt
= dmatest_add_threads(info
, dtc
, DMA_MEMCPY
);
704 thread_count
+= cnt
> 0 ? cnt
: 0;
706 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
707 cnt
= dmatest_add_threads(info
, dtc
, DMA_XOR
);
708 thread_count
+= cnt
> 0 ? cnt
: 0;
710 if (dma_has_cap(DMA_PQ
, dma_dev
->cap_mask
)) {
711 cnt
= dmatest_add_threads(info
, dtc
, DMA_PQ
);
712 thread_count
+= cnt
> 0 ? cnt
: 0;
715 pr_info("dmatest: Started %u threads using %s\n",
716 thread_count
, dma_chan_name(chan
));
718 list_add_tail(&dtc
->node
, &info
->channels
);
724 static bool filter(struct dma_chan
*chan
, void *param
)
726 struct dmatest_params
*params
= param
;
728 if (!dmatest_match_channel(params
, chan
) ||
729 !dmatest_match_device(params
, chan
->device
))
735 static int __run_threaded_test(struct dmatest_info
*info
)
738 struct dma_chan
*chan
;
739 struct dmatest_params
*params
= &info
->params
;
743 dma_cap_set(DMA_MEMCPY
, mask
);
745 chan
= dma_request_channel(mask
, filter
, params
);
747 err
= dmatest_add_channel(info
, chan
);
749 dma_release_channel(chan
);
750 break; /* add_channel failed, punt */
753 break; /* no more channels available */
754 if (params
->max_channels
&&
755 info
->nr_channels
>= params
->max_channels
)
756 break; /* we have all we need */
762 static int run_threaded_test(struct dmatest_info
*info
)
766 mutex_lock(&info
->lock
);
767 ret
= __run_threaded_test(info
);
768 mutex_unlock(&info
->lock
);
773 static void __stop_threaded_test(struct dmatest_info
*info
)
775 struct dmatest_chan
*dtc
, *_dtc
;
776 struct dma_chan
*chan
;
778 list_for_each_entry_safe(dtc
, _dtc
, &info
->channels
, node
) {
779 list_del(&dtc
->node
);
781 dmatest_cleanup_channel(dtc
);
782 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan
));
783 dma_release_channel(chan
);
786 info
->nr_channels
= 0;
789 static void stop_threaded_test(struct dmatest_info
*info
)
791 mutex_lock(&info
->lock
);
792 __stop_threaded_test(info
);
793 mutex_unlock(&info
->lock
);
796 static int __restart_threaded_test(struct dmatest_info
*info
, bool run
)
798 struct dmatest_params
*params
= &info
->params
;
801 /* Stop any running test first */
802 __stop_threaded_test(info
);
807 /* Copy test parameters */
808 memcpy(params
, &info
->dbgfs_params
, sizeof(*params
));
810 /* Run test with new parameters */
811 ret
= __run_threaded_test(info
);
813 __stop_threaded_test(info
);
814 pr_err("dmatest: Can't run test\n");
820 static ssize_t
dtf_write_string(void *to
, size_t available
, loff_t
*ppos
,
821 const void __user
*from
, size_t count
)
826 len
= simple_write_to_buffer(tmp
, sizeof(tmp
) - 1, ppos
, from
, count
);
829 strlcpy(to
, strim(tmp
), available
);
835 static ssize_t
dtf_read_channel(struct file
*file
, char __user
*buf
,
836 size_t count
, loff_t
*ppos
)
838 struct dmatest_info
*info
= file
->private_data
;
839 return simple_read_from_buffer(buf
, count
, ppos
,
840 info
->dbgfs_params
.channel
,
841 strlen(info
->dbgfs_params
.channel
));
844 static ssize_t
dtf_write_channel(struct file
*file
, const char __user
*buf
,
845 size_t size
, loff_t
*ppos
)
847 struct dmatest_info
*info
= file
->private_data
;
848 return dtf_write_string(info
->dbgfs_params
.channel
,
849 sizeof(info
->dbgfs_params
.channel
),
853 static const struct file_operations dtf_channel_fops
= {
854 .read
= dtf_read_channel
,
855 .write
= dtf_write_channel
,
857 .llseek
= default_llseek
,
860 static ssize_t
dtf_read_device(struct file
*file
, char __user
*buf
,
861 size_t count
, loff_t
*ppos
)
863 struct dmatest_info
*info
= file
->private_data
;
864 return simple_read_from_buffer(buf
, count
, ppos
,
865 info
->dbgfs_params
.device
,
866 strlen(info
->dbgfs_params
.device
));
869 static ssize_t
dtf_write_device(struct file
*file
, const char __user
*buf
,
870 size_t size
, loff_t
*ppos
)
872 struct dmatest_info
*info
= file
->private_data
;
873 return dtf_write_string(info
->dbgfs_params
.device
,
874 sizeof(info
->dbgfs_params
.device
),
878 static const struct file_operations dtf_device_fops
= {
879 .read
= dtf_read_device
,
880 .write
= dtf_write_device
,
882 .llseek
= default_llseek
,
885 static ssize_t
dtf_read_run(struct file
*file
, char __user
*user_buf
,
886 size_t count
, loff_t
*ppos
)
888 struct dmatest_info
*info
= file
->private_data
;
890 struct dmatest_chan
*dtc
;
893 mutex_lock(&info
->lock
);
894 list_for_each_entry(dtc
, &info
->channels
, node
) {
895 struct dmatest_thread
*thread
;
897 list_for_each_entry(thread
, &dtc
->threads
, node
) {
908 __stop_threaded_test(info
);
912 mutex_unlock(&info
->lock
);
915 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
918 static ssize_t
dtf_write_run(struct file
*file
, const char __user
*user_buf
,
919 size_t count
, loff_t
*ppos
)
921 struct dmatest_info
*info
= file
->private_data
;
926 if (copy_from_user(buf
, user_buf
, min(count
, (sizeof(buf
) - 1))))
929 if (strtobool(buf
, &bv
) == 0) {
930 mutex_lock(&info
->lock
);
931 ret
= __restart_threaded_test(info
, bv
);
932 mutex_unlock(&info
->lock
);
935 return ret
? ret
: count
;
938 static const struct file_operations dtf_run_fops
= {
939 .read
= dtf_read_run
,
940 .write
= dtf_write_run
,
942 .llseek
= default_llseek
,
945 static int dmatest_register_dbgfs(struct dmatest_info
*info
)
948 struct dmatest_params
*params
= &info
->dbgfs_params
;
951 d
= debugfs_create_dir("dmatest", NULL
);
959 /* Copy initial values */
960 memcpy(params
, &info
->params
, sizeof(*params
));
962 /* Test parameters */
964 d
= debugfs_create_u32("test_buf_size", S_IWUSR
| S_IRUGO
, info
->root
,
965 (u32
*)¶ms
->buf_size
);
966 if (IS_ERR_OR_NULL(d
))
969 d
= debugfs_create_file("channel", S_IRUGO
| S_IWUSR
, info
->root
,
970 info
, &dtf_channel_fops
);
971 if (IS_ERR_OR_NULL(d
))
974 d
= debugfs_create_file("device", S_IRUGO
| S_IWUSR
, info
->root
,
975 info
, &dtf_device_fops
);
976 if (IS_ERR_OR_NULL(d
))
979 d
= debugfs_create_u32("threads_per_chan", S_IWUSR
| S_IRUGO
, info
->root
,
980 (u32
*)¶ms
->threads_per_chan
);
981 if (IS_ERR_OR_NULL(d
))
984 d
= debugfs_create_u32("max_channels", S_IWUSR
| S_IRUGO
, info
->root
,
985 (u32
*)¶ms
->max_channels
);
986 if (IS_ERR_OR_NULL(d
))
989 d
= debugfs_create_u32("iterations", S_IWUSR
| S_IRUGO
, info
->root
,
990 (u32
*)¶ms
->iterations
);
991 if (IS_ERR_OR_NULL(d
))
994 d
= debugfs_create_u32("xor_sources", S_IWUSR
| S_IRUGO
, info
->root
,
995 (u32
*)¶ms
->xor_sources
);
996 if (IS_ERR_OR_NULL(d
))
999 d
= debugfs_create_u32("pq_sources", S_IWUSR
| S_IRUGO
, info
->root
,
1000 (u32
*)¶ms
->pq_sources
);
1001 if (IS_ERR_OR_NULL(d
))
1004 d
= debugfs_create_u32("timeout", S_IWUSR
| S_IRUGO
, info
->root
,
1005 (u32
*)¶ms
->timeout
);
1006 if (IS_ERR_OR_NULL(d
))
1009 /* Run or stop threaded test */
1010 d
= debugfs_create_file("run", S_IWUSR
| S_IRUGO
, info
->root
,
1011 info
, &dtf_run_fops
);
1012 if (IS_ERR_OR_NULL(d
))
1018 debugfs_remove_recursive(info
->root
);
1020 pr_err("dmatest: Failed to initialize debugfs\n");
1024 static int __init
dmatest_init(void)
1026 struct dmatest_info
*info
= &test_info
;
1027 struct dmatest_params
*params
= &info
->params
;
1030 memset(info
, 0, sizeof(*info
));
1032 mutex_init(&info
->lock
);
1033 INIT_LIST_HEAD(&info
->channels
);
1035 /* Set default parameters */
1036 params
->buf_size
= test_buf_size
;
1037 strlcpy(params
->channel
, test_channel
, sizeof(params
->channel
));
1038 strlcpy(params
->device
, test_device
, sizeof(params
->device
));
1039 params
->threads_per_chan
= threads_per_chan
;
1040 params
->max_channels
= max_channels
;
1041 params
->iterations
= iterations
;
1042 params
->xor_sources
= xor_sources
;
1043 params
->pq_sources
= pq_sources
;
1044 params
->timeout
= timeout
;
1046 ret
= dmatest_register_dbgfs(info
);
1053 return run_threaded_test(info
);
1056 /* when compiled-in wait for drivers to load first */
1057 late_initcall(dmatest_init
);
1059 static void __exit
dmatest_exit(void)
1061 struct dmatest_info
*info
= &test_info
;
1063 debugfs_remove_recursive(info
->root
);
1064 stop_threaded_test(info
);
1066 module_exit(dmatest_exit
);
1068 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1069 MODULE_LICENSE("GPL v2");