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");
69 /* Maximum amount of mismatched bytes in buffer to print */
70 #define MAX_ERROR_COUNT 32
73 * Initialization patterns. All bytes in the source buffer has bit 7
74 * set, all bytes in the destination buffer has bit 7 cleared.
76 * Bit 6 is set for all bytes which are to be copied by the DMA
77 * engine. Bit 5 is set for all bytes which are to be overwritten by
80 * The remaining bits are the inverse of a counter which increments by
81 * one for each byte address.
83 #define PATTERN_SRC 0x80
84 #define PATTERN_DST 0x00
85 #define PATTERN_COPY 0x40
86 #define PATTERN_OVERWRITE 0x20
87 #define PATTERN_COUNT_MASK 0x1f
89 enum dmatest_error_type
{
97 DMATEST_ET_DMA_IN_PROGRESS
,
101 struct dmatest_thread_result
{
102 struct list_head node
;
104 unsigned int src_off
;
105 unsigned int dst_off
;
107 enum dmatest_error_type type
;
111 enum dma_status status
;
116 struct dmatest_result
{
117 struct list_head node
;
119 struct list_head results
;
124 struct dmatest_thread
{
125 struct list_head node
;
126 struct dmatest_info
*info
;
127 struct task_struct
*task
;
128 struct dma_chan
*chan
;
131 enum dma_transaction_type type
;
135 struct dmatest_chan
{
136 struct list_head node
;
137 struct dma_chan
*chan
;
138 struct list_head threads
;
142 * struct dmatest_params - test parameters.
143 * @buf_size: size of the memcpy test buffer
144 * @channel: bus ID of the channel to test
145 * @device: bus ID of the DMA Engine to test
146 * @threads_per_chan: number of threads to start per channel
147 * @max_channels: maximum number of channels to use
148 * @iterations: iterations before stopping test
149 * @xor_sources: number of xor source buffers
150 * @pq_sources: number of p+q source buffers
151 * @timeout: transfer timeout in msec, -1 for infinite timeout
153 struct dmatest_params
{
154 unsigned int buf_size
;
157 unsigned int threads_per_chan
;
158 unsigned int max_channels
;
159 unsigned int iterations
;
160 unsigned int xor_sources
;
161 unsigned int pq_sources
;
166 * struct dmatest_info - test information.
167 * @params: test parameters
168 * @lock: access protection to the fields of this structure
170 struct dmatest_info
{
171 /* Test parameters */
172 struct dmatest_params params
;
175 struct list_head channels
;
176 unsigned int nr_channels
;
179 /* debugfs related stuff */
181 struct dmatest_params dbgfs_params
;
184 struct list_head results
;
185 struct mutex results_lock
;
188 static struct dmatest_info test_info
;
190 static bool dmatest_match_channel(struct dmatest_params
*params
,
191 struct dma_chan
*chan
)
193 if (params
->channel
[0] == '\0')
195 return strcmp(dma_chan_name(chan
), params
->channel
) == 0;
198 static bool dmatest_match_device(struct dmatest_params
*params
,
199 struct dma_device
*device
)
201 if (params
->device
[0] == '\0')
203 return strcmp(dev_name(device
->dev
), params
->device
) == 0;
206 static unsigned long dmatest_random(void)
210 get_random_bytes(&buf
, sizeof(buf
));
214 static void dmatest_init_srcs(u8
**bufs
, unsigned int start
, unsigned int len
,
215 unsigned int buf_size
)
220 for (; (buf
= *bufs
); bufs
++) {
221 for (i
= 0; i
< start
; i
++)
222 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
223 for ( ; i
< start
+ len
; i
++)
224 buf
[i
] = PATTERN_SRC
| PATTERN_COPY
225 | (~i
& PATTERN_COUNT_MASK
);
226 for ( ; i
< buf_size
; i
++)
227 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
232 static void dmatest_init_dsts(u8
**bufs
, unsigned int start
, unsigned int len
,
233 unsigned int buf_size
)
238 for (; (buf
= *bufs
); bufs
++) {
239 for (i
= 0; i
< start
; i
++)
240 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
241 for ( ; i
< start
+ len
; i
++)
242 buf
[i
] = PATTERN_DST
| PATTERN_OVERWRITE
243 | (~i
& PATTERN_COUNT_MASK
);
244 for ( ; i
< buf_size
; i
++)
245 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
249 static void dmatest_mismatch(u8 actual
, u8 pattern
, unsigned int index
,
250 unsigned int counter
, bool is_srcbuf
)
252 u8 diff
= actual
^ pattern
;
253 u8 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
254 const char *thread_name
= current
->comm
;
257 pr_warning("%s: srcbuf[0x%x] overwritten!"
258 " Expected %02x, got %02x\n",
259 thread_name
, index
, expected
, actual
);
260 else if ((pattern
& PATTERN_COPY
)
261 && (diff
& (PATTERN_COPY
| PATTERN_OVERWRITE
)))
262 pr_warning("%s: dstbuf[0x%x] not copied!"
263 " Expected %02x, got %02x\n",
264 thread_name
, index
, expected
, actual
);
265 else if (diff
& PATTERN_SRC
)
266 pr_warning("%s: dstbuf[0x%x] was copied!"
267 " Expected %02x, got %02x\n",
268 thread_name
, index
, expected
, actual
);
270 pr_warning("%s: dstbuf[0x%x] mismatch!"
271 " Expected %02x, got %02x\n",
272 thread_name
, index
, expected
, actual
);
275 static unsigned int dmatest_verify(u8
**bufs
, unsigned int start
,
276 unsigned int end
, unsigned int counter
, u8 pattern
,
280 unsigned int error_count
= 0;
284 unsigned int counter_orig
= counter
;
286 for (; (buf
= *bufs
); bufs
++) {
287 counter
= counter_orig
;
288 for (i
= start
; i
< end
; i
++) {
290 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
291 if (actual
!= expected
) {
292 if (error_count
< MAX_ERROR_COUNT
)
293 dmatest_mismatch(actual
, pattern
, i
,
301 if (error_count
> MAX_ERROR_COUNT
)
302 pr_warning("%s: %u errors suppressed\n",
303 current
->comm
, error_count
- MAX_ERROR_COUNT
);
308 /* poor man's completion - we want to use wait_event_freezable() on it */
309 struct dmatest_done
{
311 wait_queue_head_t
*wait
;
314 static void dmatest_callback(void *arg
)
316 struct dmatest_done
*done
= arg
;
319 wake_up_all(done
->wait
);
322 static inline void unmap_src(struct device
*dev
, dma_addr_t
*addr
, size_t len
,
326 dma_unmap_single(dev
, addr
[count
], len
, DMA_TO_DEVICE
);
329 static inline void unmap_dst(struct device
*dev
, dma_addr_t
*addr
, size_t len
,
333 dma_unmap_single(dev
, addr
[count
], len
, DMA_BIDIRECTIONAL
);
336 static unsigned int min_odd(unsigned int x
, unsigned int y
)
338 unsigned int val
= min(x
, y
);
340 return val
% 2 ? val
: val
- 1;
343 static char *thread_result_get(const char *name
,
344 struct dmatest_thread_result
*tr
)
346 static const char * const messages
[] = {
347 [DMATEST_ET_OK
] = "No errors",
348 [DMATEST_ET_MAP_SRC
] = "src mapping error",
349 [DMATEST_ET_MAP_DST
] = "dst mapping error",
350 [DMATEST_ET_PREP
] = "prep error",
351 [DMATEST_ET_SUBMIT
] = "submit error",
352 [DMATEST_ET_TIMEOUT
] = "test timed out",
353 [DMATEST_ET_DMA_ERROR
] =
354 "got completion callback (DMA_ERROR)",
355 [DMATEST_ET_DMA_IN_PROGRESS
] =
356 "got completion callback (DMA_IN_PROGRESS)",
357 [DMATEST_ET_VERIFY
] = "errors",
359 static char buf
[512];
361 snprintf(buf
, sizeof(buf
) - 1,
362 "%s: #%u: %s with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
363 name
, tr
->n
, messages
[tr
->type
], tr
->src_off
, tr
->dst_off
,
369 static int thread_result_add(struct dmatest_info
*info
,
370 struct dmatest_result
*r
, enum dmatest_error_type type
,
371 unsigned int n
, unsigned int src_off
, unsigned int dst_off
,
372 unsigned int len
, unsigned long data
)
374 struct dmatest_thread_result
*tr
;
376 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
382 tr
->src_off
= src_off
;
383 tr
->dst_off
= dst_off
;
387 mutex_lock(&info
->results_lock
);
388 list_add_tail(&tr
->node
, &r
->results
);
389 mutex_unlock(&info
->results_lock
);
391 pr_warn("%s\n", thread_result_get(r
->name
, tr
));
395 static void result_free(struct dmatest_info
*info
, const char *name
)
397 struct dmatest_result
*r
, *_r
;
399 mutex_lock(&info
->results_lock
);
400 list_for_each_entry_safe(r
, _r
, &info
->results
, node
) {
401 struct dmatest_thread_result
*tr
, *_tr
;
403 if (name
&& strcmp(r
->name
, name
))
406 list_for_each_entry_safe(tr
, _tr
, &r
->results
, node
) {
416 mutex_unlock(&info
->results_lock
);
419 static struct dmatest_result
*result_init(struct dmatest_info
*info
,
422 struct dmatest_result
*r
;
424 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
426 r
->name
= kstrdup(name
, GFP_KERNEL
);
427 INIT_LIST_HEAD(&r
->results
);
428 mutex_lock(&info
->results_lock
);
429 list_add_tail(&r
->node
, &info
->results
);
430 mutex_unlock(&info
->results_lock
);
436 * This function repeatedly tests DMA transfers of various lengths and
437 * offsets for a given operation type until it is told to exit by
438 * kthread_stop(). There may be multiple threads running this function
439 * in parallel for a single channel, and there may be multiple channels
440 * being tested in parallel.
442 * Before each test, the source and destination buffer is initialized
443 * with a known pattern. This pattern is different depending on
444 * whether it's in an area which is supposed to be copied or
445 * overwritten, and different in the source and destination buffers.
446 * So if the DMA engine doesn't copy exactly what we tell it to copy,
449 static int dmatest_func(void *data
)
451 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait
);
452 struct dmatest_thread
*thread
= data
;
453 struct dmatest_done done
= { .wait
= &done_wait
};
454 struct dmatest_info
*info
;
455 struct dmatest_params
*params
;
456 struct dma_chan
*chan
;
457 struct dma_device
*dev
;
458 const char *thread_name
;
459 unsigned int src_off
, dst_off
, len
;
460 unsigned int error_count
;
461 unsigned int failed_tests
= 0;
462 unsigned int total_tests
= 0;
464 enum dma_status status
;
465 enum dma_ctrl_flags flags
;
471 struct dmatest_result
*result
;
473 thread_name
= current
->comm
;
480 params
= &info
->params
;
483 if (thread
->type
== DMA_MEMCPY
)
484 src_cnt
= dst_cnt
= 1;
485 else if (thread
->type
== DMA_XOR
) {
486 /* force odd to ensure dst = src */
487 src_cnt
= min_odd(params
->xor_sources
| 1, dev
->max_xor
);
489 } else if (thread
->type
== DMA_PQ
) {
490 /* force odd to ensure dst = src */
491 src_cnt
= min_odd(params
->pq_sources
| 1, dma_maxpq(dev
, 0));
494 pq_coefs
= kmalloc(params
->pq_sources
+1, GFP_KERNEL
);
496 goto err_thread_type
;
498 for (i
= 0; i
< src_cnt
; i
++)
501 goto err_thread_type
;
503 result
= result_init(info
, thread_name
);
507 thread
->srcs
= kcalloc(src_cnt
+1, sizeof(u8
*), GFP_KERNEL
);
510 for (i
= 0; i
< src_cnt
; i
++) {
511 thread
->srcs
[i
] = kmalloc(params
->buf_size
, GFP_KERNEL
);
512 if (!thread
->srcs
[i
])
515 thread
->srcs
[i
] = NULL
;
517 thread
->dsts
= kcalloc(dst_cnt
+1, sizeof(u8
*), GFP_KERNEL
);
520 for (i
= 0; i
< dst_cnt
; i
++) {
521 thread
->dsts
[i
] = kmalloc(params
->buf_size
, GFP_KERNEL
);
522 if (!thread
->dsts
[i
])
525 thread
->dsts
[i
] = NULL
;
527 set_user_nice(current
, 10);
530 * src buffers are freed by the DMAEngine code with dma_unmap_single()
531 * dst buffers are freed by ourselves below
533 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
534 | DMA_COMPL_SKIP_DEST_UNMAP
| DMA_COMPL_SRC_UNMAP_SINGLE
;
536 while (!kthread_should_stop()
537 && !(params
->iterations
&& total_tests
>= params
->iterations
)) {
538 struct dma_async_tx_descriptor
*tx
= NULL
;
539 dma_addr_t dma_srcs
[src_cnt
];
540 dma_addr_t dma_dsts
[dst_cnt
];
545 /* honor alignment restrictions */
546 if (thread
->type
== DMA_MEMCPY
)
547 align
= dev
->copy_align
;
548 else if (thread
->type
== DMA_XOR
)
549 align
= dev
->xor_align
;
550 else if (thread
->type
== DMA_PQ
)
551 align
= dev
->pq_align
;
553 if (1 << align
> params
->buf_size
) {
554 pr_err("%u-byte buffer too small for %d-byte alignment\n",
555 params
->buf_size
, 1 << align
);
559 len
= dmatest_random() % params
->buf_size
+ 1;
560 len
= (len
>> align
) << align
;
563 src_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
564 dst_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
566 src_off
= (src_off
>> align
) << align
;
567 dst_off
= (dst_off
>> align
) << align
;
569 dmatest_init_srcs(thread
->srcs
, src_off
, len
, params
->buf_size
);
570 dmatest_init_dsts(thread
->dsts
, dst_off
, len
, params
->buf_size
);
572 for (i
= 0; i
< src_cnt
; i
++) {
573 u8
*buf
= thread
->srcs
[i
] + src_off
;
575 dma_srcs
[i
] = dma_map_single(dev
->dev
, buf
, len
,
577 ret
= dma_mapping_error(dev
->dev
, dma_srcs
[i
]);
579 unmap_src(dev
->dev
, dma_srcs
, len
, i
);
580 thread_result_add(info
, result
,
582 total_tests
, src_off
, dst_off
,
588 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
589 for (i
= 0; i
< dst_cnt
; i
++) {
590 dma_dsts
[i
] = dma_map_single(dev
->dev
, thread
->dsts
[i
],
593 ret
= dma_mapping_error(dev
->dev
, dma_dsts
[i
]);
595 unmap_src(dev
->dev
, dma_srcs
, len
, src_cnt
);
596 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
,
598 thread_result_add(info
, result
,
600 total_tests
, src_off
, dst_off
,
607 if (thread
->type
== DMA_MEMCPY
)
608 tx
= dev
->device_prep_dma_memcpy(chan
,
609 dma_dsts
[0] + dst_off
,
612 else if (thread
->type
== DMA_XOR
)
613 tx
= dev
->device_prep_dma_xor(chan
,
614 dma_dsts
[0] + dst_off
,
617 else if (thread
->type
== DMA_PQ
) {
618 dma_addr_t dma_pq
[dst_cnt
];
620 for (i
= 0; i
< dst_cnt
; i
++)
621 dma_pq
[i
] = dma_dsts
[i
] + dst_off
;
622 tx
= dev
->device_prep_dma_pq(chan
, dma_pq
, dma_srcs
,
628 unmap_src(dev
->dev
, dma_srcs
, len
, src_cnt
);
629 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
,
631 thread_result_add(info
, result
, DMATEST_ET_PREP
,
632 total_tests
, src_off
, dst_off
,
640 tx
->callback
= dmatest_callback
;
641 tx
->callback_param
= &done
;
642 cookie
= tx
->tx_submit(tx
);
644 if (dma_submit_error(cookie
)) {
645 thread_result_add(info
, result
, DMATEST_ET_SUBMIT
,
646 total_tests
, src_off
, dst_off
,
652 dma_async_issue_pending(chan
);
654 wait_event_freezable_timeout(done_wait
,
655 done
.done
|| kthread_should_stop(),
656 msecs_to_jiffies(params
->timeout
));
658 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
662 * We're leaving the timed out dma operation with
663 * dangling pointer to done_wait. To make this
664 * correct, we'll need to allocate wait_done for
665 * each test iteration and perform "who's gonna
666 * free it this time?" dancing. For now, just
669 thread_result_add(info
, result
, DMATEST_ET_TIMEOUT
,
670 total_tests
, src_off
, dst_off
,
674 } else if (status
!= DMA_SUCCESS
) {
675 enum dmatest_error_type type
= (status
== DMA_ERROR
) ?
676 DMATEST_ET_DMA_ERROR
: DMATEST_ET_DMA_IN_PROGRESS
;
677 thread_result_add(info
, result
, type
,
678 total_tests
, src_off
, dst_off
,
684 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
685 unmap_dst(dev
->dev
, dma_dsts
, params
->buf_size
, dst_cnt
);
689 pr_debug("%s: verifying source buffer...\n", thread_name
);
690 error_count
+= dmatest_verify(thread
->srcs
, 0, src_off
,
691 0, PATTERN_SRC
, true);
692 error_count
+= dmatest_verify(thread
->srcs
, src_off
,
693 src_off
+ len
, src_off
,
694 PATTERN_SRC
| PATTERN_COPY
, true);
695 error_count
+= dmatest_verify(thread
->srcs
, src_off
+ len
,
696 params
->buf_size
, src_off
+ len
,
699 pr_debug("%s: verifying dest buffer...\n",
701 error_count
+= dmatest_verify(thread
->dsts
, 0, dst_off
,
702 0, PATTERN_DST
, false);
703 error_count
+= dmatest_verify(thread
->dsts
, dst_off
,
704 dst_off
+ len
, src_off
,
705 PATTERN_SRC
| PATTERN_COPY
, false);
706 error_count
+= dmatest_verify(thread
->dsts
, dst_off
+ len
,
707 params
->buf_size
, dst_off
+ len
,
711 thread_result_add(info
, result
, DMATEST_ET_VERIFY
,
712 total_tests
, src_off
, dst_off
,
716 thread_result_add(info
, result
, DMATEST_ET_OK
,
717 total_tests
, src_off
, dst_off
,
723 for (i
= 0; thread
->dsts
[i
]; i
++)
724 kfree(thread
->dsts
[i
]);
728 for (i
= 0; thread
->srcs
[i
]; i
++)
729 kfree(thread
->srcs
[i
]);
735 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
736 thread_name
, total_tests
, failed_tests
, ret
);
738 /* terminate all transfers on specified channels */
740 dmaengine_terminate_all(chan
);
744 if (params
->iterations
> 0)
745 while (!kthread_should_stop()) {
746 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit
);
747 interruptible_sleep_on(&wait_dmatest_exit
);
753 static void dmatest_cleanup_channel(struct dmatest_chan
*dtc
)
755 struct dmatest_thread
*thread
;
756 struct dmatest_thread
*_thread
;
759 list_for_each_entry_safe(thread
, _thread
, &dtc
->threads
, node
) {
760 ret
= kthread_stop(thread
->task
);
761 pr_debug("dmatest: thread %s exited with status %d\n",
762 thread
->task
->comm
, ret
);
763 list_del(&thread
->node
);
767 /* terminate all transfers on specified channels */
768 dmaengine_terminate_all(dtc
->chan
);
773 static int dmatest_add_threads(struct dmatest_info
*info
,
774 struct dmatest_chan
*dtc
, enum dma_transaction_type type
)
776 struct dmatest_params
*params
= &info
->params
;
777 struct dmatest_thread
*thread
;
778 struct dma_chan
*chan
= dtc
->chan
;
782 if (type
== DMA_MEMCPY
)
784 else if (type
== DMA_XOR
)
786 else if (type
== DMA_PQ
)
791 for (i
= 0; i
< params
->threads_per_chan
; i
++) {
792 thread
= kzalloc(sizeof(struct dmatest_thread
), GFP_KERNEL
);
794 pr_warning("dmatest: No memory for %s-%s%u\n",
795 dma_chan_name(chan
), op
, i
);
800 thread
->chan
= dtc
->chan
;
803 thread
->task
= kthread_run(dmatest_func
, thread
, "%s-%s%u",
804 dma_chan_name(chan
), op
, i
);
805 if (IS_ERR(thread
->task
)) {
806 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
807 dma_chan_name(chan
), op
, i
);
812 /* srcbuf and dstbuf are allocated by the thread itself */
814 list_add_tail(&thread
->node
, &dtc
->threads
);
820 static int dmatest_add_channel(struct dmatest_info
*info
,
821 struct dma_chan
*chan
)
823 struct dmatest_chan
*dtc
;
824 struct dma_device
*dma_dev
= chan
->device
;
825 unsigned int thread_count
= 0;
828 dtc
= kmalloc(sizeof(struct dmatest_chan
), GFP_KERNEL
);
830 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan
));
835 INIT_LIST_HEAD(&dtc
->threads
);
837 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
838 cnt
= dmatest_add_threads(info
, dtc
, DMA_MEMCPY
);
839 thread_count
+= cnt
> 0 ? cnt
: 0;
841 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
842 cnt
= dmatest_add_threads(info
, dtc
, DMA_XOR
);
843 thread_count
+= cnt
> 0 ? cnt
: 0;
845 if (dma_has_cap(DMA_PQ
, dma_dev
->cap_mask
)) {
846 cnt
= dmatest_add_threads(info
, dtc
, DMA_PQ
);
847 thread_count
+= cnt
> 0 ? cnt
: 0;
850 pr_info("dmatest: Started %u threads using %s\n",
851 thread_count
, dma_chan_name(chan
));
853 list_add_tail(&dtc
->node
, &info
->channels
);
859 static bool filter(struct dma_chan
*chan
, void *param
)
861 struct dmatest_params
*params
= param
;
863 if (!dmatest_match_channel(params
, chan
) ||
864 !dmatest_match_device(params
, chan
->device
))
870 static int __run_threaded_test(struct dmatest_info
*info
)
873 struct dma_chan
*chan
;
874 struct dmatest_params
*params
= &info
->params
;
878 dma_cap_set(DMA_MEMCPY
, mask
);
880 chan
= dma_request_channel(mask
, filter
, params
);
882 err
= dmatest_add_channel(info
, chan
);
884 dma_release_channel(chan
);
885 break; /* add_channel failed, punt */
888 break; /* no more channels available */
889 if (params
->max_channels
&&
890 info
->nr_channels
>= params
->max_channels
)
891 break; /* we have all we need */
897 static int run_threaded_test(struct dmatest_info
*info
)
901 mutex_lock(&info
->lock
);
902 ret
= __run_threaded_test(info
);
903 mutex_unlock(&info
->lock
);
908 static void __stop_threaded_test(struct dmatest_info
*info
)
910 struct dmatest_chan
*dtc
, *_dtc
;
911 struct dma_chan
*chan
;
913 list_for_each_entry_safe(dtc
, _dtc
, &info
->channels
, node
) {
914 list_del(&dtc
->node
);
916 dmatest_cleanup_channel(dtc
);
917 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan
));
918 dma_release_channel(chan
);
921 info
->nr_channels
= 0;
924 static void stop_threaded_test(struct dmatest_info
*info
)
926 mutex_lock(&info
->lock
);
927 __stop_threaded_test(info
);
928 mutex_unlock(&info
->lock
);
931 static int __restart_threaded_test(struct dmatest_info
*info
, bool run
)
933 struct dmatest_params
*params
= &info
->params
;
936 /* Stop any running test first */
937 __stop_threaded_test(info
);
942 /* Clear results from previous run */
943 result_free(info
, NULL
);
945 /* Copy test parameters */
946 memcpy(params
, &info
->dbgfs_params
, sizeof(*params
));
948 /* Run test with new parameters */
949 ret
= __run_threaded_test(info
);
951 __stop_threaded_test(info
);
952 pr_err("dmatest: Can't run test\n");
958 static ssize_t
dtf_write_string(void *to
, size_t available
, loff_t
*ppos
,
959 const void __user
*from
, size_t count
)
964 len
= simple_write_to_buffer(tmp
, sizeof(tmp
) - 1, ppos
, from
, count
);
967 strlcpy(to
, strim(tmp
), available
);
973 static ssize_t
dtf_read_channel(struct file
*file
, char __user
*buf
,
974 size_t count
, loff_t
*ppos
)
976 struct dmatest_info
*info
= file
->private_data
;
977 return simple_read_from_buffer(buf
, count
, ppos
,
978 info
->dbgfs_params
.channel
,
979 strlen(info
->dbgfs_params
.channel
));
982 static ssize_t
dtf_write_channel(struct file
*file
, const char __user
*buf
,
983 size_t size
, loff_t
*ppos
)
985 struct dmatest_info
*info
= file
->private_data
;
986 return dtf_write_string(info
->dbgfs_params
.channel
,
987 sizeof(info
->dbgfs_params
.channel
),
991 static const struct file_operations dtf_channel_fops
= {
992 .read
= dtf_read_channel
,
993 .write
= dtf_write_channel
,
995 .llseek
= default_llseek
,
998 static ssize_t
dtf_read_device(struct file
*file
, char __user
*buf
,
999 size_t count
, loff_t
*ppos
)
1001 struct dmatest_info
*info
= file
->private_data
;
1002 return simple_read_from_buffer(buf
, count
, ppos
,
1003 info
->dbgfs_params
.device
,
1004 strlen(info
->dbgfs_params
.device
));
1007 static ssize_t
dtf_write_device(struct file
*file
, const char __user
*buf
,
1008 size_t size
, loff_t
*ppos
)
1010 struct dmatest_info
*info
= file
->private_data
;
1011 return dtf_write_string(info
->dbgfs_params
.device
,
1012 sizeof(info
->dbgfs_params
.device
),
1016 static const struct file_operations dtf_device_fops
= {
1017 .read
= dtf_read_device
,
1018 .write
= dtf_write_device
,
1019 .open
= simple_open
,
1020 .llseek
= default_llseek
,
1023 static ssize_t
dtf_read_run(struct file
*file
, char __user
*user_buf
,
1024 size_t count
, loff_t
*ppos
)
1026 struct dmatest_info
*info
= file
->private_data
;
1028 struct dmatest_chan
*dtc
;
1031 mutex_lock(&info
->lock
);
1032 list_for_each_entry(dtc
, &info
->channels
, node
) {
1033 struct dmatest_thread
*thread
;
1035 list_for_each_entry(thread
, &dtc
->threads
, node
) {
1036 if (!thread
->done
) {
1046 __stop_threaded_test(info
);
1050 mutex_unlock(&info
->lock
);
1053 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
1056 static ssize_t
dtf_write_run(struct file
*file
, const char __user
*user_buf
,
1057 size_t count
, loff_t
*ppos
)
1059 struct dmatest_info
*info
= file
->private_data
;
1064 if (copy_from_user(buf
, user_buf
, min(count
, (sizeof(buf
) - 1))))
1067 if (strtobool(buf
, &bv
) == 0) {
1068 mutex_lock(&info
->lock
);
1069 ret
= __restart_threaded_test(info
, bv
);
1070 mutex_unlock(&info
->lock
);
1073 return ret
? ret
: count
;
1076 static const struct file_operations dtf_run_fops
= {
1077 .read
= dtf_read_run
,
1078 .write
= dtf_write_run
,
1079 .open
= simple_open
,
1080 .llseek
= default_llseek
,
1083 static int dtf_results_show(struct seq_file
*sf
, void *data
)
1085 struct dmatest_info
*info
= sf
->private;
1086 struct dmatest_result
*result
;
1087 struct dmatest_thread_result
*tr
;
1089 mutex_lock(&info
->results_lock
);
1090 list_for_each_entry(result
, &info
->results
, node
) {
1091 list_for_each_entry(tr
, &result
->results
, node
)
1092 seq_printf(sf
, "%s\n",
1093 thread_result_get(result
->name
, tr
));
1096 mutex_unlock(&info
->results_lock
);
1100 static int dtf_results_open(struct inode
*inode
, struct file
*file
)
1102 return single_open(file
, dtf_results_show
, inode
->i_private
);
1105 static const struct file_operations dtf_results_fops
= {
1106 .open
= dtf_results_open
,
1108 .llseek
= seq_lseek
,
1109 .release
= single_release
,
1112 static int dmatest_register_dbgfs(struct dmatest_info
*info
)
1115 struct dmatest_params
*params
= &info
->dbgfs_params
;
1118 d
= debugfs_create_dir("dmatest", NULL
);
1126 /* Copy initial values */
1127 memcpy(params
, &info
->params
, sizeof(*params
));
1129 /* Test parameters */
1131 d
= debugfs_create_u32("test_buf_size", S_IWUSR
| S_IRUGO
, info
->root
,
1132 (u32
*)¶ms
->buf_size
);
1133 if (IS_ERR_OR_NULL(d
))
1136 d
= debugfs_create_file("channel", S_IRUGO
| S_IWUSR
, info
->root
,
1137 info
, &dtf_channel_fops
);
1138 if (IS_ERR_OR_NULL(d
))
1141 d
= debugfs_create_file("device", S_IRUGO
| S_IWUSR
, info
->root
,
1142 info
, &dtf_device_fops
);
1143 if (IS_ERR_OR_NULL(d
))
1146 d
= debugfs_create_u32("threads_per_chan", S_IWUSR
| S_IRUGO
, info
->root
,
1147 (u32
*)¶ms
->threads_per_chan
);
1148 if (IS_ERR_OR_NULL(d
))
1151 d
= debugfs_create_u32("max_channels", S_IWUSR
| S_IRUGO
, info
->root
,
1152 (u32
*)¶ms
->max_channels
);
1153 if (IS_ERR_OR_NULL(d
))
1156 d
= debugfs_create_u32("iterations", S_IWUSR
| S_IRUGO
, info
->root
,
1157 (u32
*)¶ms
->iterations
);
1158 if (IS_ERR_OR_NULL(d
))
1161 d
= debugfs_create_u32("xor_sources", S_IWUSR
| S_IRUGO
, info
->root
,
1162 (u32
*)¶ms
->xor_sources
);
1163 if (IS_ERR_OR_NULL(d
))
1166 d
= debugfs_create_u32("pq_sources", S_IWUSR
| S_IRUGO
, info
->root
,
1167 (u32
*)¶ms
->pq_sources
);
1168 if (IS_ERR_OR_NULL(d
))
1171 d
= debugfs_create_u32("timeout", S_IWUSR
| S_IRUGO
, info
->root
,
1172 (u32
*)¶ms
->timeout
);
1173 if (IS_ERR_OR_NULL(d
))
1176 /* Run or stop threaded test */
1177 d
= debugfs_create_file("run", S_IWUSR
| S_IRUGO
, info
->root
,
1178 info
, &dtf_run_fops
);
1179 if (IS_ERR_OR_NULL(d
))
1182 /* Results of test in progress */
1183 d
= debugfs_create_file("results", S_IRUGO
, info
->root
, info
,
1185 if (IS_ERR_OR_NULL(d
))
1191 debugfs_remove_recursive(info
->root
);
1193 pr_err("dmatest: Failed to initialize debugfs\n");
1197 static int __init
dmatest_init(void)
1199 struct dmatest_info
*info
= &test_info
;
1200 struct dmatest_params
*params
= &info
->params
;
1203 memset(info
, 0, sizeof(*info
));
1205 mutex_init(&info
->lock
);
1206 INIT_LIST_HEAD(&info
->channels
);
1208 mutex_init(&info
->results_lock
);
1209 INIT_LIST_HEAD(&info
->results
);
1211 /* Set default parameters */
1212 params
->buf_size
= test_buf_size
;
1213 strlcpy(params
->channel
, test_channel
, sizeof(params
->channel
));
1214 strlcpy(params
->device
, test_device
, sizeof(params
->device
));
1215 params
->threads_per_chan
= threads_per_chan
;
1216 params
->max_channels
= max_channels
;
1217 params
->iterations
= iterations
;
1218 params
->xor_sources
= xor_sources
;
1219 params
->pq_sources
= pq_sources
;
1220 params
->timeout
= timeout
;
1222 ret
= dmatest_register_dbgfs(info
);
1229 return run_threaded_test(info
);
1232 /* when compiled-in wait for drivers to load first */
1233 late_initcall(dmatest_init
);
1235 static void __exit
dmatest_exit(void)
1237 struct dmatest_info
*info
= &test_info
;
1239 debugfs_remove_recursive(info
->root
);
1240 stop_threaded_test(info
);
1241 result_free(info
, NULL
);
1243 module_exit(dmatest_exit
);
1245 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1246 MODULE_LICENSE("GPL v2");