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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/freezer.h>
17 #include <linux/init.h>
18 #include <linux/kthread.h>
19 #include <linux/sched/task.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/random.h>
23 #include <linux/slab.h>
24 #include <linux/wait.h>
26 static unsigned int test_buf_size
= 16384;
27 module_param(test_buf_size
, uint
, S_IRUGO
| S_IWUSR
);
28 MODULE_PARM_DESC(test_buf_size
, "Size of the memcpy test buffer");
30 static char test_channel
[20];
31 module_param_string(channel
, test_channel
, sizeof(test_channel
),
33 MODULE_PARM_DESC(channel
, "Bus ID of the channel to test (default: any)");
35 static char test_device
[32];
36 module_param_string(device
, test_device
, sizeof(test_device
),
38 MODULE_PARM_DESC(device
, "Bus ID of the DMA Engine to test (default: any)");
40 static unsigned int threads_per_chan
= 1;
41 module_param(threads_per_chan
, uint
, S_IRUGO
| S_IWUSR
);
42 MODULE_PARM_DESC(threads_per_chan
,
43 "Number of threads to start per channel (default: 1)");
45 static unsigned int max_channels
;
46 module_param(max_channels
, uint
, S_IRUGO
| S_IWUSR
);
47 MODULE_PARM_DESC(max_channels
,
48 "Maximum number of channels to use (default: all)");
50 static unsigned int iterations
;
51 module_param(iterations
, uint
, S_IRUGO
| S_IWUSR
);
52 MODULE_PARM_DESC(iterations
,
53 "Iterations before stopping test (default: infinite)");
55 static unsigned int sg_buffers
= 1;
56 module_param(sg_buffers
, uint
, S_IRUGO
| S_IWUSR
);
57 MODULE_PARM_DESC(sg_buffers
,
58 "Number of scatter gather buffers (default: 1)");
60 static unsigned int dmatest
;
61 module_param(dmatest
, uint
, S_IRUGO
| S_IWUSR
);
62 MODULE_PARM_DESC(dmatest
,
63 "dmatest 0-memcpy 1-slave_sg (default: 0)");
65 static unsigned int xor_sources
= 3;
66 module_param(xor_sources
, uint
, S_IRUGO
| S_IWUSR
);
67 MODULE_PARM_DESC(xor_sources
,
68 "Number of xor source buffers (default: 3)");
70 static unsigned int pq_sources
= 3;
71 module_param(pq_sources
, uint
, S_IRUGO
| S_IWUSR
);
72 MODULE_PARM_DESC(pq_sources
,
73 "Number of p+q source buffers (default: 3)");
75 static int timeout
= 3000;
76 module_param(timeout
, uint
, S_IRUGO
| S_IWUSR
);
77 MODULE_PARM_DESC(timeout
, "Transfer Timeout in msec (default: 3000), "
78 "Pass -1 for infinite timeout");
81 module_param(noverify
, bool, S_IRUGO
| S_IWUSR
);
82 MODULE_PARM_DESC(noverify
, "Disable random data setup and verification");
85 module_param(verbose
, bool, S_IRUGO
| S_IWUSR
);
86 MODULE_PARM_DESC(verbose
, "Enable \"success\" result messages (default: off)");
89 * struct dmatest_params - test parameters.
90 * @buf_size: size of the memcpy test buffer
91 * @channel: bus ID of the channel to test
92 * @device: bus ID of the DMA Engine to test
93 * @threads_per_chan: number of threads to start per channel
94 * @max_channels: maximum number of channels to use
95 * @iterations: iterations before stopping test
96 * @xor_sources: number of xor source buffers
97 * @pq_sources: number of p+q source buffers
98 * @timeout: transfer timeout in msec, -1 for infinite timeout
100 struct dmatest_params
{
101 unsigned int buf_size
;
104 unsigned int threads_per_chan
;
105 unsigned int max_channels
;
106 unsigned int iterations
;
107 unsigned int xor_sources
;
108 unsigned int pq_sources
;
114 * struct dmatest_info - test information.
115 * @params: test parameters
116 * @lock: access protection to the fields of this structure
118 static struct dmatest_info
{
119 /* Test parameters */
120 struct dmatest_params params
;
123 struct list_head channels
;
124 unsigned int nr_channels
;
128 .channels
= LIST_HEAD_INIT(test_info
.channels
),
129 .lock
= __MUTEX_INITIALIZER(test_info
.lock
),
132 static int dmatest_run_set(const char *val
, const struct kernel_param
*kp
);
133 static int dmatest_run_get(char *val
, const struct kernel_param
*kp
);
134 static const struct kernel_param_ops run_ops
= {
135 .set
= dmatest_run_set
,
136 .get
= dmatest_run_get
,
138 static bool dmatest_run
;
139 module_param_cb(run
, &run_ops
, &dmatest_run
, S_IRUGO
| S_IWUSR
);
140 MODULE_PARM_DESC(run
, "Run the test (default: false)");
142 /* Maximum amount of mismatched bytes in buffer to print */
143 #define MAX_ERROR_COUNT 32
146 * Initialization patterns. All bytes in the source buffer has bit 7
147 * set, all bytes in the destination buffer has bit 7 cleared.
149 * Bit 6 is set for all bytes which are to be copied by the DMA
150 * engine. Bit 5 is set for all bytes which are to be overwritten by
153 * The remaining bits are the inverse of a counter which increments by
154 * one for each byte address.
156 #define PATTERN_SRC 0x80
157 #define PATTERN_DST 0x00
158 #define PATTERN_COPY 0x40
159 #define PATTERN_OVERWRITE 0x20
160 #define PATTERN_COUNT_MASK 0x1f
162 struct dmatest_thread
{
163 struct list_head node
;
164 struct dmatest_info
*info
;
165 struct task_struct
*task
;
166 struct dma_chan
*chan
;
171 enum dma_transaction_type type
;
175 struct dmatest_chan
{
176 struct list_head node
;
177 struct dma_chan
*chan
;
178 struct list_head threads
;
181 static DECLARE_WAIT_QUEUE_HEAD(thread_wait
);
184 static bool is_threaded_test_run(struct dmatest_info
*info
)
186 struct dmatest_chan
*dtc
;
188 list_for_each_entry(dtc
, &info
->channels
, node
) {
189 struct dmatest_thread
*thread
;
191 list_for_each_entry(thread
, &dtc
->threads
, node
) {
200 static int dmatest_wait_get(char *val
, const struct kernel_param
*kp
)
202 struct dmatest_info
*info
= &test_info
;
203 struct dmatest_params
*params
= &info
->params
;
205 if (params
->iterations
)
206 wait_event(thread_wait
, !is_threaded_test_run(info
));
208 return param_get_bool(val
, kp
);
211 static const struct kernel_param_ops wait_ops
= {
212 .get
= dmatest_wait_get
,
213 .set
= param_set_bool
,
215 module_param_cb(wait
, &wait_ops
, &wait
, S_IRUGO
);
216 MODULE_PARM_DESC(wait
, "Wait for tests to complete (default: false)");
218 static bool dmatest_match_channel(struct dmatest_params
*params
,
219 struct dma_chan
*chan
)
221 if (params
->channel
[0] == '\0')
223 return strcmp(dma_chan_name(chan
), params
->channel
) == 0;
226 static bool dmatest_match_device(struct dmatest_params
*params
,
227 struct dma_device
*device
)
229 if (params
->device
[0] == '\0')
231 return strcmp(dev_name(device
->dev
), params
->device
) == 0;
234 static unsigned long dmatest_random(void)
238 prandom_bytes(&buf
, sizeof(buf
));
242 static void dmatest_init_srcs(u8
**bufs
, unsigned int start
, unsigned int len
,
243 unsigned int buf_size
)
248 for (; (buf
= *bufs
); bufs
++) {
249 for (i
= 0; i
< start
; i
++)
250 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
251 for ( ; i
< start
+ len
; i
++)
252 buf
[i
] = PATTERN_SRC
| PATTERN_COPY
253 | (~i
& PATTERN_COUNT_MASK
);
254 for ( ; i
< buf_size
; i
++)
255 buf
[i
] = PATTERN_SRC
| (~i
& PATTERN_COUNT_MASK
);
260 static void dmatest_init_dsts(u8
**bufs
, unsigned int start
, unsigned int len
,
261 unsigned int buf_size
)
266 for (; (buf
= *bufs
); bufs
++) {
267 for (i
= 0; i
< start
; i
++)
268 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
269 for ( ; i
< start
+ len
; i
++)
270 buf
[i
] = PATTERN_DST
| PATTERN_OVERWRITE
271 | (~i
& PATTERN_COUNT_MASK
);
272 for ( ; i
< buf_size
; i
++)
273 buf
[i
] = PATTERN_DST
| (~i
& PATTERN_COUNT_MASK
);
277 static void dmatest_mismatch(u8 actual
, u8 pattern
, unsigned int index
,
278 unsigned int counter
, bool is_srcbuf
)
280 u8 diff
= actual
^ pattern
;
281 u8 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
282 const char *thread_name
= current
->comm
;
285 pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
286 thread_name
, index
, expected
, actual
);
287 else if ((pattern
& PATTERN_COPY
)
288 && (diff
& (PATTERN_COPY
| PATTERN_OVERWRITE
)))
289 pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
290 thread_name
, index
, expected
, actual
);
291 else if (diff
& PATTERN_SRC
)
292 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
293 thread_name
, index
, expected
, actual
);
295 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
296 thread_name
, index
, expected
, actual
);
299 static unsigned int dmatest_verify(u8
**bufs
, unsigned int start
,
300 unsigned int end
, unsigned int counter
, u8 pattern
,
304 unsigned int error_count
= 0;
308 unsigned int counter_orig
= counter
;
310 for (; (buf
= *bufs
); bufs
++) {
311 counter
= counter_orig
;
312 for (i
= start
; i
< end
; i
++) {
314 expected
= pattern
| (~counter
& PATTERN_COUNT_MASK
);
315 if (actual
!= expected
) {
316 if (error_count
< MAX_ERROR_COUNT
)
317 dmatest_mismatch(actual
, pattern
, i
,
325 if (error_count
> MAX_ERROR_COUNT
)
326 pr_warn("%s: %u errors suppressed\n",
327 current
->comm
, error_count
- MAX_ERROR_COUNT
);
332 /* poor man's completion - we want to use wait_event_freezable() on it */
333 struct dmatest_done
{
335 wait_queue_head_t
*wait
;
338 static void dmatest_callback(void *arg
)
340 struct dmatest_done
*done
= arg
;
343 wake_up_all(done
->wait
);
346 static unsigned int min_odd(unsigned int x
, unsigned int y
)
348 unsigned int val
= min(x
, y
);
350 return val
% 2 ? val
: val
- 1;
353 static void result(const char *err
, unsigned int n
, unsigned int src_off
,
354 unsigned int dst_off
, unsigned int len
, unsigned long data
)
356 pr_info("%s: result #%u: '%s' with src_off=0x%x dst_off=0x%x len=0x%x (%lu)\n",
357 current
->comm
, n
, err
, src_off
, dst_off
, len
, data
);
360 static void dbg_result(const char *err
, unsigned int n
, unsigned int src_off
,
361 unsigned int dst_off
, unsigned int len
,
364 pr_debug("%s: result #%u: '%s' with src_off=0x%x dst_off=0x%x len=0x%x (%lu)\n",
365 current
->comm
, n
, err
, src_off
, dst_off
, len
, data
);
368 #define verbose_result(err, n, src_off, dst_off, len, data) ({ \
370 result(err, n, src_off, dst_off, len, data); \
372 dbg_result(err, n, src_off, dst_off, len, data);\
375 static unsigned long long dmatest_persec(s64 runtime
, unsigned int val
)
377 unsigned long long per_sec
= 1000000;
382 /* drop precision until runtime is 32-bits */
383 while (runtime
> UINT_MAX
) {
389 do_div(per_sec
, runtime
);
393 static unsigned long long dmatest_KBs(s64 runtime
, unsigned long long len
)
395 return dmatest_persec(runtime
, len
>> 10);
399 * This function repeatedly tests DMA transfers of various lengths and
400 * offsets for a given operation type until it is told to exit by
401 * kthread_stop(). There may be multiple threads running this function
402 * in parallel for a single channel, and there may be multiple channels
403 * being tested in parallel.
405 * Before each test, the source and destination buffer is initialized
406 * with a known pattern. This pattern is different depending on
407 * whether it's in an area which is supposed to be copied or
408 * overwritten, and different in the source and destination buffers.
409 * So if the DMA engine doesn't copy exactly what we tell it to copy,
412 static int dmatest_func(void *data
)
414 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait
);
415 struct dmatest_thread
*thread
= data
;
416 struct dmatest_done done
= { .wait
= &done_wait
};
417 struct dmatest_info
*info
;
418 struct dmatest_params
*params
;
419 struct dma_chan
*chan
;
420 struct dma_device
*dev
;
421 unsigned int error_count
;
422 unsigned int failed_tests
= 0;
423 unsigned int total_tests
= 0;
425 enum dma_status status
;
426 enum dma_ctrl_flags flags
;
432 ktime_t ktime
, start
, diff
;
433 ktime_t filltime
= 0;
434 ktime_t comparetime
= 0;
436 unsigned long long total_len
= 0;
445 params
= &info
->params
;
448 if (thread
->type
== DMA_MEMCPY
) {
449 align
= dev
->copy_align
;
450 src_cnt
= dst_cnt
= 1;
451 } else if (thread
->type
== DMA_SG
) {
452 align
= dev
->copy_align
;
453 src_cnt
= dst_cnt
= sg_buffers
;
454 } else if (thread
->type
== DMA_XOR
) {
455 /* force odd to ensure dst = src */
456 src_cnt
= min_odd(params
->xor_sources
| 1, dev
->max_xor
);
458 align
= dev
->xor_align
;
459 } else if (thread
->type
== DMA_PQ
) {
460 /* force odd to ensure dst = src */
461 src_cnt
= min_odd(params
->pq_sources
| 1, dma_maxpq(dev
, 0));
463 align
= dev
->pq_align
;
465 pq_coefs
= kmalloc(params
->pq_sources
+ 1, GFP_KERNEL
);
467 goto err_thread_type
;
469 for (i
= 0; i
< src_cnt
; i
++)
472 goto err_thread_type
;
474 thread
->srcs
= kcalloc(src_cnt
+ 1, sizeof(u8
*), GFP_KERNEL
);
478 thread
->usrcs
= kcalloc(src_cnt
+ 1, sizeof(u8
*), GFP_KERNEL
);
482 for (i
= 0; i
< src_cnt
; i
++) {
483 thread
->usrcs
[i
] = kmalloc(params
->buf_size
+ align
,
485 if (!thread
->usrcs
[i
])
488 /* align srcs to alignment restriction */
490 thread
->srcs
[i
] = PTR_ALIGN(thread
->usrcs
[i
], align
);
492 thread
->srcs
[i
] = thread
->usrcs
[i
];
494 thread
->srcs
[i
] = NULL
;
496 thread
->dsts
= kcalloc(dst_cnt
+ 1, sizeof(u8
*), GFP_KERNEL
);
500 thread
->udsts
= kcalloc(dst_cnt
+ 1, sizeof(u8
*), GFP_KERNEL
);
504 for (i
= 0; i
< dst_cnt
; i
++) {
505 thread
->udsts
[i
] = kmalloc(params
->buf_size
+ align
,
507 if (!thread
->udsts
[i
])
510 /* align dsts to alignment restriction */
512 thread
->dsts
[i
] = PTR_ALIGN(thread
->udsts
[i
], align
);
514 thread
->dsts
[i
] = thread
->udsts
[i
];
516 thread
->dsts
[i
] = NULL
;
518 set_user_nice(current
, 10);
521 * src and dst buffers are freed by ourselves below
523 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
526 while (!kthread_should_stop()
527 && !(params
->iterations
&& total_tests
>= params
->iterations
)) {
528 struct dma_async_tx_descriptor
*tx
= NULL
;
529 struct dmaengine_unmap_data
*um
;
530 dma_addr_t srcs
[src_cnt
];
532 unsigned int src_off
, dst_off
, len
;
533 struct scatterlist tx_sg
[src_cnt
];
534 struct scatterlist rx_sg
[src_cnt
];
538 /* Check if buffer count fits into map count variable (u8) */
539 if ((src_cnt
+ dst_cnt
) >= 255) {
540 pr_err("too many buffers (%d of 255 supported)\n",
545 if (1 << align
> params
->buf_size
) {
546 pr_err("%u-byte buffer too small for %d-byte alignment\n",
547 params
->buf_size
, 1 << align
);
551 if (params
->noverify
)
552 len
= params
->buf_size
;
554 len
= dmatest_random() % params
->buf_size
+ 1;
556 len
= (len
>> align
) << align
;
562 if (params
->noverify
) {
567 src_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
568 dst_off
= dmatest_random() % (params
->buf_size
- len
+ 1);
570 src_off
= (src_off
>> align
) << align
;
571 dst_off
= (dst_off
>> align
) << align
;
573 dmatest_init_srcs(thread
->srcs
, src_off
, len
,
575 dmatest_init_dsts(thread
->dsts
, dst_off
, len
,
578 diff
= ktime_sub(ktime_get(), start
);
579 filltime
= ktime_add(filltime
, diff
);
582 um
= dmaengine_get_unmap_data(dev
->dev
, src_cnt
+ dst_cnt
,
586 result("unmap data NULL", total_tests
,
587 src_off
, dst_off
, len
, ret
);
591 um
->len
= params
->buf_size
;
592 for (i
= 0; i
< src_cnt
; i
++) {
593 void *buf
= thread
->srcs
[i
];
594 struct page
*pg
= virt_to_page(buf
);
595 unsigned long pg_off
= offset_in_page(buf
);
597 um
->addr
[i
] = dma_map_page(dev
->dev
, pg
, pg_off
,
598 um
->len
, DMA_TO_DEVICE
);
599 srcs
[i
] = um
->addr
[i
] + src_off
;
600 ret
= dma_mapping_error(dev
->dev
, um
->addr
[i
]);
602 dmaengine_unmap_put(um
);
603 result("src mapping error", total_tests
,
604 src_off
, dst_off
, len
, ret
);
610 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
611 dsts
= &um
->addr
[src_cnt
];
612 for (i
= 0; i
< dst_cnt
; i
++) {
613 void *buf
= thread
->dsts
[i
];
614 struct page
*pg
= virt_to_page(buf
);
615 unsigned long pg_off
= offset_in_page(buf
);
617 dsts
[i
] = dma_map_page(dev
->dev
, pg
, pg_off
, um
->len
,
619 ret
= dma_mapping_error(dev
->dev
, dsts
[i
]);
621 dmaengine_unmap_put(um
);
622 result("dst mapping error", total_tests
,
623 src_off
, dst_off
, len
, ret
);
630 sg_init_table(tx_sg
, src_cnt
);
631 sg_init_table(rx_sg
, src_cnt
);
632 for (i
= 0; i
< src_cnt
; i
++) {
633 sg_dma_address(&rx_sg
[i
]) = srcs
[i
];
634 sg_dma_address(&tx_sg
[i
]) = dsts
[i
] + dst_off
;
635 sg_dma_len(&tx_sg
[i
]) = len
;
636 sg_dma_len(&rx_sg
[i
]) = len
;
639 if (thread
->type
== DMA_MEMCPY
)
640 tx
= dev
->device_prep_dma_memcpy(chan
,
642 srcs
[0], len
, flags
);
643 else if (thread
->type
== DMA_SG
)
644 tx
= dev
->device_prep_dma_sg(chan
, tx_sg
, src_cnt
,
645 rx_sg
, src_cnt
, flags
);
646 else if (thread
->type
== DMA_XOR
)
647 tx
= dev
->device_prep_dma_xor(chan
,
651 else if (thread
->type
== DMA_PQ
) {
652 dma_addr_t dma_pq
[dst_cnt
];
654 for (i
= 0; i
< dst_cnt
; i
++)
655 dma_pq
[i
] = dsts
[i
] + dst_off
;
656 tx
= dev
->device_prep_dma_pq(chan
, dma_pq
, srcs
,
662 dmaengine_unmap_put(um
);
663 result("prep error", total_tests
, src_off
,
671 tx
->callback
= dmatest_callback
;
672 tx
->callback_param
= &done
;
673 cookie
= tx
->tx_submit(tx
);
675 if (dma_submit_error(cookie
)) {
676 dmaengine_unmap_put(um
);
677 result("submit error", total_tests
, src_off
,
683 dma_async_issue_pending(chan
);
685 wait_event_freezable_timeout(done_wait
, done
.done
,
686 msecs_to_jiffies(params
->timeout
));
688 status
= dma_async_is_tx_complete(chan
, cookie
, NULL
, NULL
);
692 * We're leaving the timed out dma operation with
693 * dangling pointer to done_wait. To make this
694 * correct, we'll need to allocate wait_done for
695 * each test iteration and perform "who's gonna
696 * free it this time?" dancing. For now, just
699 dmaengine_unmap_put(um
);
700 result("test timed out", total_tests
, src_off
, dst_off
,
704 } else if (status
!= DMA_COMPLETE
) {
705 dmaengine_unmap_put(um
);
706 result(status
== DMA_ERROR
?
707 "completion error status" :
708 "completion busy status", total_tests
, src_off
,
714 dmaengine_unmap_put(um
);
716 if (params
->noverify
) {
717 verbose_result("test passed", total_tests
, src_off
,
723 pr_debug("%s: verifying source buffer...\n", current
->comm
);
724 error_count
= dmatest_verify(thread
->srcs
, 0, src_off
,
725 0, PATTERN_SRC
, true);
726 error_count
+= dmatest_verify(thread
->srcs
, src_off
,
727 src_off
+ len
, src_off
,
728 PATTERN_SRC
| PATTERN_COPY
, true);
729 error_count
+= dmatest_verify(thread
->srcs
, src_off
+ len
,
730 params
->buf_size
, src_off
+ len
,
733 pr_debug("%s: verifying dest buffer...\n", current
->comm
);
734 error_count
+= dmatest_verify(thread
->dsts
, 0, dst_off
,
735 0, PATTERN_DST
, false);
736 error_count
+= dmatest_verify(thread
->dsts
, dst_off
,
737 dst_off
+ len
, src_off
,
738 PATTERN_SRC
| PATTERN_COPY
, false);
739 error_count
+= dmatest_verify(thread
->dsts
, dst_off
+ len
,
740 params
->buf_size
, dst_off
+ len
,
743 diff
= ktime_sub(ktime_get(), start
);
744 comparetime
= ktime_add(comparetime
, diff
);
747 result("data error", total_tests
, src_off
, dst_off
,
751 verbose_result("test passed", total_tests
, src_off
,
755 ktime
= ktime_sub(ktime_get(), ktime
);
756 ktime
= ktime_sub(ktime
, comparetime
);
757 ktime
= ktime_sub(ktime
, filltime
);
758 runtime
= ktime_to_us(ktime
);
762 for (i
= 0; thread
->udsts
[i
]; i
++)
763 kfree(thread
->udsts
[i
]);
764 kfree(thread
->udsts
);
769 for (i
= 0; thread
->usrcs
[i
]; i
++)
770 kfree(thread
->usrcs
[i
]);
771 kfree(thread
->usrcs
);
777 pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
778 current
->comm
, total_tests
, failed_tests
,
779 dmatest_persec(runtime
, total_tests
),
780 dmatest_KBs(runtime
, total_len
), ret
);
782 /* terminate all transfers on specified channels */
784 dmaengine_terminate_all(chan
);
787 wake_up(&thread_wait
);
792 static void dmatest_cleanup_channel(struct dmatest_chan
*dtc
)
794 struct dmatest_thread
*thread
;
795 struct dmatest_thread
*_thread
;
798 list_for_each_entry_safe(thread
, _thread
, &dtc
->threads
, node
) {
799 ret
= kthread_stop(thread
->task
);
800 pr_debug("thread %s exited with status %d\n",
801 thread
->task
->comm
, ret
);
802 list_del(&thread
->node
);
803 put_task_struct(thread
->task
);
807 /* terminate all transfers on specified channels */
808 dmaengine_terminate_all(dtc
->chan
);
813 static int dmatest_add_threads(struct dmatest_info
*info
,
814 struct dmatest_chan
*dtc
, enum dma_transaction_type type
)
816 struct dmatest_params
*params
= &info
->params
;
817 struct dmatest_thread
*thread
;
818 struct dma_chan
*chan
= dtc
->chan
;
822 if (type
== DMA_MEMCPY
)
824 else if (type
== DMA_SG
)
826 else if (type
== DMA_XOR
)
828 else if (type
== DMA_PQ
)
833 for (i
= 0; i
< params
->threads_per_chan
; i
++) {
834 thread
= kzalloc(sizeof(struct dmatest_thread
), GFP_KERNEL
);
836 pr_warn("No memory for %s-%s%u\n",
837 dma_chan_name(chan
), op
, i
);
841 thread
->chan
= dtc
->chan
;
844 thread
->task
= kthread_create(dmatest_func
, thread
, "%s-%s%u",
845 dma_chan_name(chan
), op
, i
);
846 if (IS_ERR(thread
->task
)) {
847 pr_warn("Failed to create thread %s-%s%u\n",
848 dma_chan_name(chan
), op
, i
);
853 /* srcbuf and dstbuf are allocated by the thread itself */
854 get_task_struct(thread
->task
);
855 list_add_tail(&thread
->node
, &dtc
->threads
);
856 wake_up_process(thread
->task
);
862 static int dmatest_add_channel(struct dmatest_info
*info
,
863 struct dma_chan
*chan
)
865 struct dmatest_chan
*dtc
;
866 struct dma_device
*dma_dev
= chan
->device
;
867 unsigned int thread_count
= 0;
870 dtc
= kmalloc(sizeof(struct dmatest_chan
), GFP_KERNEL
);
872 pr_warn("No memory for %s\n", dma_chan_name(chan
));
877 INIT_LIST_HEAD(&dtc
->threads
);
879 if (dma_has_cap(DMA_MEMCPY
, dma_dev
->cap_mask
)) {
881 cnt
= dmatest_add_threads(info
, dtc
, DMA_MEMCPY
);
882 thread_count
+= cnt
> 0 ? cnt
: 0;
886 if (dma_has_cap(DMA_SG
, dma_dev
->cap_mask
)) {
888 cnt
= dmatest_add_threads(info
, dtc
, DMA_SG
);
889 thread_count
+= cnt
> 0 ? cnt
: 0;
893 if (dma_has_cap(DMA_XOR
, dma_dev
->cap_mask
)) {
894 cnt
= dmatest_add_threads(info
, dtc
, DMA_XOR
);
895 thread_count
+= cnt
> 0 ? cnt
: 0;
897 if (dma_has_cap(DMA_PQ
, dma_dev
->cap_mask
)) {
898 cnt
= dmatest_add_threads(info
, dtc
, DMA_PQ
);
899 thread_count
+= cnt
> 0 ? cnt
: 0;
902 pr_info("Started %u threads using %s\n",
903 thread_count
, dma_chan_name(chan
));
905 list_add_tail(&dtc
->node
, &info
->channels
);
911 static bool filter(struct dma_chan
*chan
, void *param
)
913 struct dmatest_params
*params
= param
;
915 if (!dmatest_match_channel(params
, chan
) ||
916 !dmatest_match_device(params
, chan
->device
))
922 static void request_channels(struct dmatest_info
*info
,
923 enum dma_transaction_type type
)
928 dma_cap_set(type
, mask
);
930 struct dmatest_params
*params
= &info
->params
;
931 struct dma_chan
*chan
;
933 chan
= dma_request_channel(mask
, filter
, params
);
935 if (dmatest_add_channel(info
, chan
)) {
936 dma_release_channel(chan
);
937 break; /* add_channel failed, punt */
940 break; /* no more channels available */
941 if (params
->max_channels
&&
942 info
->nr_channels
>= params
->max_channels
)
943 break; /* we have all we need */
947 static void run_threaded_test(struct dmatest_info
*info
)
949 struct dmatest_params
*params
= &info
->params
;
951 /* Copy test parameters */
952 params
->buf_size
= test_buf_size
;
953 strlcpy(params
->channel
, strim(test_channel
), sizeof(params
->channel
));
954 strlcpy(params
->device
, strim(test_device
), sizeof(params
->device
));
955 params
->threads_per_chan
= threads_per_chan
;
956 params
->max_channels
= max_channels
;
957 params
->iterations
= iterations
;
958 params
->xor_sources
= xor_sources
;
959 params
->pq_sources
= pq_sources
;
960 params
->timeout
= timeout
;
961 params
->noverify
= noverify
;
963 request_channels(info
, DMA_MEMCPY
);
964 request_channels(info
, DMA_XOR
);
965 request_channels(info
, DMA_SG
);
966 request_channels(info
, DMA_PQ
);
969 static void stop_threaded_test(struct dmatest_info
*info
)
971 struct dmatest_chan
*dtc
, *_dtc
;
972 struct dma_chan
*chan
;
974 list_for_each_entry_safe(dtc
, _dtc
, &info
->channels
, node
) {
975 list_del(&dtc
->node
);
977 dmatest_cleanup_channel(dtc
);
978 pr_debug("dropped channel %s\n", dma_chan_name(chan
));
979 dma_release_channel(chan
);
982 info
->nr_channels
= 0;
985 static void restart_threaded_test(struct dmatest_info
*info
, bool run
)
987 /* we might be called early to set run=, defer running until all
988 * parameters have been evaluated
993 /* Stop any running test first */
994 stop_threaded_test(info
);
996 /* Run test with new parameters */
997 run_threaded_test(info
);
1000 static int dmatest_run_get(char *val
, const struct kernel_param
*kp
)
1002 struct dmatest_info
*info
= &test_info
;
1004 mutex_lock(&info
->lock
);
1005 if (is_threaded_test_run(info
)) {
1008 stop_threaded_test(info
);
1009 dmatest_run
= false;
1011 mutex_unlock(&info
->lock
);
1013 return param_get_bool(val
, kp
);
1016 static int dmatest_run_set(const char *val
, const struct kernel_param
*kp
)
1018 struct dmatest_info
*info
= &test_info
;
1021 mutex_lock(&info
->lock
);
1022 ret
= param_set_bool(val
, kp
);
1024 mutex_unlock(&info
->lock
);
1028 if (is_threaded_test_run(info
))
1030 else if (dmatest_run
)
1031 restart_threaded_test(info
, dmatest_run
);
1033 mutex_unlock(&info
->lock
);
1038 static int __init
dmatest_init(void)
1040 struct dmatest_info
*info
= &test_info
;
1041 struct dmatest_params
*params
= &info
->params
;
1044 mutex_lock(&info
->lock
);
1045 run_threaded_test(info
);
1046 mutex_unlock(&info
->lock
);
1049 if (params
->iterations
&& wait
)
1050 wait_event(thread_wait
, !is_threaded_test_run(info
));
1052 /* module parameters are stable, inittime tests are started,
1053 * let userspace take over 'run' control
1055 info
->did_init
= true;
1059 /* when compiled-in wait for drivers to load first */
1060 late_initcall(dmatest_init
);
1062 static void __exit
dmatest_exit(void)
1064 struct dmatest_info
*info
= &test_info
;
1066 mutex_lock(&info
->lock
);
1067 stop_threaded_test(info
);
1068 mutex_unlock(&info
->lock
);
1070 module_exit(dmatest_exit
);
1072 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1073 MODULE_LICENSE("GPL v2");