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 | S_IWUSR);
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),
34 MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
36 static char test_device[20];
37 module_param_string(device, test_device, sizeof(test_device),
39 MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
41 static unsigned int threads_per_chan = 1;
42 module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(threads_per_chan,
44 "Number of threads to start per channel (default: 1)");
46 static unsigned int max_channels;
47 module_param(max_channels, uint, S_IRUGO | S_IWUSR);
48 MODULE_PARM_DESC(max_channels,
49 "Maximum number of channels to use (default: all)");
51 static unsigned int iterations;
52 module_param(iterations, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(iterations,
54 "Iterations before stopping test (default: infinite)");
56 static unsigned int xor_sources = 3;
57 module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(xor_sources,
59 "Number of xor source buffers (default: 3)");
61 static unsigned int pq_sources = 3;
62 module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(pq_sources,
64 "Number of p+q source buffers (default: 3)");
66 static int timeout = 3000;
67 module_param(timeout, uint, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
69 "Pass -1 for infinite timeout");
71 /* Maximum amount of mismatched bytes in buffer to print */
72 #define MAX_ERROR_COUNT 32
75 * Initialization patterns. All bytes in the source buffer has bit 7
76 * set, all bytes in the destination buffer has bit 7 cleared.
78 * Bit 6 is set for all bytes which are to be copied by the DMA
79 * engine. Bit 5 is set for all bytes which are to be overwritten by
82 * The remaining bits are the inverse of a counter which increments by
83 * one for each byte address.
85 #define PATTERN_SRC 0x80
86 #define PATTERN_DST 0x00
87 #define PATTERN_COPY 0x40
88 #define PATTERN_OVERWRITE 0x20
89 #define PATTERN_COUNT_MASK 0x1f
91 enum dmatest_error_type {
99 DMATEST_ET_DMA_IN_PROGRESS,
103 struct dmatest_thread_result {
104 struct list_head node;
106 unsigned int src_off;
107 unsigned int dst_off;
109 enum dmatest_error_type type;
113 enum dma_status status;
118 struct dmatest_result {
119 struct list_head node;
121 struct list_head results;
126 struct dmatest_thread {
127 struct list_head node;
128 struct dmatest_info *info;
129 struct task_struct *task;
130 struct dma_chan *chan;
133 enum dma_transaction_type type;
137 struct dmatest_chan {
138 struct list_head node;
139 struct dma_chan *chan;
140 struct list_head threads;
144 * struct dmatest_params - test parameters.
145 * @buf_size: size of the memcpy test buffer
146 * @channel: bus ID of the channel to test
147 * @device: bus ID of the DMA Engine to test
148 * @threads_per_chan: number of threads to start per channel
149 * @max_channels: maximum number of channels to use
150 * @iterations: iterations before stopping test
151 * @xor_sources: number of xor source buffers
152 * @pq_sources: number of p+q source buffers
153 * @timeout: transfer timeout in msec, -1 for infinite timeout
155 struct dmatest_params {
156 unsigned int buf_size;
159 unsigned int threads_per_chan;
160 unsigned int max_channels;
161 unsigned int iterations;
162 unsigned int xor_sources;
163 unsigned int pq_sources;
168 * struct dmatest_info - test information.
169 * @params: test parameters
170 * @lock: access protection to the fields of this structure
172 struct dmatest_info {
173 /* Test parameters */
174 struct dmatest_params params;
177 struct list_head channels;
178 unsigned int nr_channels;
181 /* debugfs related stuff */
185 struct list_head results;
186 struct mutex results_lock;
189 static struct dmatest_info test_info;
191 static bool dmatest_match_channel(struct dmatest_params *params,
192 struct dma_chan *chan)
194 if (params->channel[0] == '\0')
196 return strcmp(dma_chan_name(chan), params->channel) == 0;
199 static bool dmatest_match_device(struct dmatest_params *params,
200 struct dma_device *device)
202 if (params->device[0] == '\0')
204 return strcmp(dev_name(device->dev), params->device) == 0;
207 static unsigned long dmatest_random(void)
211 get_random_bytes(&buf, sizeof(buf));
215 static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
216 unsigned int buf_size)
221 for (; (buf = *bufs); bufs++) {
222 for (i = 0; i < start; i++)
223 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
224 for ( ; i < start + len; i++)
225 buf[i] = PATTERN_SRC | PATTERN_COPY
226 | (~i & PATTERN_COUNT_MASK);
227 for ( ; i < buf_size; i++)
228 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
233 static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
234 unsigned int buf_size)
239 for (; (buf = *bufs); bufs++) {
240 for (i = 0; i < start; i++)
241 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
242 for ( ; i < start + len; i++)
243 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
244 | (~i & PATTERN_COUNT_MASK);
245 for ( ; i < buf_size; i++)
246 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
250 static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
251 unsigned int counter, bool is_srcbuf)
253 u8 diff = actual ^ pattern;
254 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
255 const char *thread_name = current->comm;
258 pr_warn("%s: srcbuf[0x%x] overwritten! 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_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
263 thread_name, index, expected, actual);
264 else if (diff & PATTERN_SRC)
265 pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
266 thread_name, index, expected, actual);
268 pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
269 thread_name, index, expected, actual);
272 static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
273 unsigned int end, unsigned int counter, u8 pattern,
277 unsigned int error_count = 0;
281 unsigned int counter_orig = counter;
283 for (; (buf = *bufs); bufs++) {
284 counter = counter_orig;
285 for (i = start; i < end; i++) {
287 expected = pattern | (~counter & PATTERN_COUNT_MASK);
288 if (actual != expected) {
289 if (error_count < MAX_ERROR_COUNT)
290 dmatest_mismatch(actual, pattern, i,
298 if (error_count > MAX_ERROR_COUNT)
299 pr_warn("%s: %u errors suppressed\n",
300 current->comm, error_count - MAX_ERROR_COUNT);
305 /* poor man's completion - we want to use wait_event_freezable() on it */
306 struct dmatest_done {
308 wait_queue_head_t *wait;
311 static void dmatest_callback(void *arg)
313 struct dmatest_done *done = arg;
316 wake_up_all(done->wait);
319 static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
323 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
326 static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
330 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
333 static unsigned int min_odd(unsigned int x, unsigned int y)
335 unsigned int val = min(x, y);
337 return val % 2 ? val : val - 1;
340 static char *thread_result_get(const char *name,
341 struct dmatest_thread_result *tr)
343 static const char * const messages[] = {
344 [DMATEST_ET_OK] = "No errors",
345 [DMATEST_ET_MAP_SRC] = "src mapping error",
346 [DMATEST_ET_MAP_DST] = "dst mapping error",
347 [DMATEST_ET_PREP] = "prep error",
348 [DMATEST_ET_SUBMIT] = "submit error",
349 [DMATEST_ET_TIMEOUT] = "test timed out",
350 [DMATEST_ET_DMA_ERROR] =
351 "got completion callback (DMA_ERROR)",
352 [DMATEST_ET_DMA_IN_PROGRESS] =
353 "got completion callback (DMA_IN_PROGRESS)",
354 [DMATEST_ET_VERIFY] = "errors",
356 static char buf[512];
358 snprintf(buf, sizeof(buf) - 1,
359 "%s: #%u: %s with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
360 name, tr->n, messages[tr->type], tr->src_off, tr->dst_off,
366 static int thread_result_add(struct dmatest_info *info,
367 struct dmatest_result *r, enum dmatest_error_type type,
368 unsigned int n, unsigned int src_off, unsigned int dst_off,
369 unsigned int len, unsigned long data)
371 struct dmatest_thread_result *tr;
373 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
379 tr->src_off = src_off;
380 tr->dst_off = dst_off;
384 mutex_lock(&info->results_lock);
385 list_add_tail(&tr->node, &r->results);
386 mutex_unlock(&info->results_lock);
388 if (tr->type == DMATEST_ET_OK)
389 pr_debug("%s\n", thread_result_get(r->name, tr));
391 pr_warn("%s\n", thread_result_get(r->name, tr));
396 static void result_free(struct dmatest_info *info, const char *name)
398 struct dmatest_result *r, *_r;
400 mutex_lock(&info->results_lock);
401 list_for_each_entry_safe(r, _r, &info->results, node) {
402 struct dmatest_thread_result *tr, *_tr;
404 if (name && strcmp(r->name, name))
407 list_for_each_entry_safe(tr, _tr, &r->results, node) {
417 mutex_unlock(&info->results_lock);
420 static struct dmatest_result *result_init(struct dmatest_info *info,
423 struct dmatest_result *r;
425 r = kzalloc(sizeof(*r), GFP_KERNEL);
427 r->name = kstrdup(name, GFP_KERNEL);
428 INIT_LIST_HEAD(&r->results);
429 mutex_lock(&info->results_lock);
430 list_add_tail(&r->node, &info->results);
431 mutex_unlock(&info->results_lock);
437 * This function repeatedly tests DMA transfers of various lengths and
438 * offsets for a given operation type until it is told to exit by
439 * kthread_stop(). There may be multiple threads running this function
440 * in parallel for a single channel, and there may be multiple channels
441 * being tested in parallel.
443 * Before each test, the source and destination buffer is initialized
444 * with a known pattern. This pattern is different depending on
445 * whether it's in an area which is supposed to be copied or
446 * overwritten, and different in the source and destination buffers.
447 * So if the DMA engine doesn't copy exactly what we tell it to copy,
450 static int dmatest_func(void *data)
452 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
453 struct dmatest_thread *thread = data;
454 struct dmatest_done done = { .wait = &done_wait };
455 struct dmatest_info *info;
456 struct dmatest_params *params;
457 struct dma_chan *chan;
458 struct dma_device *dev;
459 const char *thread_name;
460 unsigned int src_off, dst_off, len;
461 unsigned int error_count;
462 unsigned int failed_tests = 0;
463 unsigned int total_tests = 0;
465 enum dma_status status;
466 enum dma_ctrl_flags flags;
472 struct dmatest_result *result;
474 thread_name = current->comm;
481 params = &info->params;
484 if (thread->type == DMA_MEMCPY)
485 src_cnt = dst_cnt = 1;
486 else if (thread->type == DMA_XOR) {
487 /* force odd to ensure dst = src */
488 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
490 } else if (thread->type == DMA_PQ) {
491 /* force odd to ensure dst = src */
492 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
495 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
497 goto err_thread_type;
499 for (i = 0; i < src_cnt; i++)
502 goto err_thread_type;
504 result = result_init(info, thread_name);
508 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
511 for (i = 0; i < src_cnt; i++) {
512 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
513 if (!thread->srcs[i])
516 thread->srcs[i] = NULL;
518 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
521 for (i = 0; i < dst_cnt; i++) {
522 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
523 if (!thread->dsts[i])
526 thread->dsts[i] = NULL;
528 set_user_nice(current, 10);
531 * src and dst buffers are freed by ourselves below
533 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
535 while (!kthread_should_stop()
536 && !(params->iterations && total_tests >= params->iterations)) {
537 struct dma_async_tx_descriptor *tx = NULL;
538 dma_addr_t dma_srcs[src_cnt];
539 dma_addr_t dma_dsts[dst_cnt];
544 /* honor alignment restrictions */
545 if (thread->type == DMA_MEMCPY)
546 align = dev->copy_align;
547 else if (thread->type == DMA_XOR)
548 align = dev->xor_align;
549 else if (thread->type == DMA_PQ)
550 align = dev->pq_align;
552 if (1 << align > params->buf_size) {
553 pr_err("%u-byte buffer too small for %d-byte alignment\n",
554 params->buf_size, 1 << align);
558 len = dmatest_random() % params->buf_size + 1;
559 len = (len >> align) << align;
562 src_off = dmatest_random() % (params->buf_size - len + 1);
563 dst_off = dmatest_random() % (params->buf_size - len + 1);
565 src_off = (src_off >> align) << align;
566 dst_off = (dst_off >> align) << align;
568 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
569 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
571 for (i = 0; i < src_cnt; i++) {
572 u8 *buf = thread->srcs[i] + src_off;
574 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
576 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
578 unmap_src(dev->dev, dma_srcs, len, i);
579 thread_result_add(info, result,
581 total_tests, src_off, dst_off,
587 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
588 for (i = 0; i < dst_cnt; i++) {
589 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
592 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
594 unmap_src(dev->dev, dma_srcs, len, src_cnt);
595 unmap_dst(dev->dev, dma_dsts, params->buf_size,
597 thread_result_add(info, result,
599 total_tests, src_off, dst_off,
606 if (thread->type == DMA_MEMCPY)
607 tx = dev->device_prep_dma_memcpy(chan,
608 dma_dsts[0] + dst_off,
611 else if (thread->type == DMA_XOR)
612 tx = dev->device_prep_dma_xor(chan,
613 dma_dsts[0] + dst_off,
616 else if (thread->type == DMA_PQ) {
617 dma_addr_t dma_pq[dst_cnt];
619 for (i = 0; i < dst_cnt; i++)
620 dma_pq[i] = dma_dsts[i] + dst_off;
621 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
627 unmap_src(dev->dev, dma_srcs, len, src_cnt);
628 unmap_dst(dev->dev, dma_dsts, params->buf_size,
630 thread_result_add(info, result, DMATEST_ET_PREP,
631 total_tests, src_off, dst_off,
639 tx->callback = dmatest_callback;
640 tx->callback_param = &done;
641 cookie = tx->tx_submit(tx);
643 if (dma_submit_error(cookie)) {
644 thread_result_add(info, result, DMATEST_ET_SUBMIT,
645 total_tests, src_off, dst_off,
651 dma_async_issue_pending(chan);
653 wait_event_freezable_timeout(done_wait, done.done,
654 msecs_to_jiffies(params->timeout));
656 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
660 * We're leaving the timed out dma operation with
661 * dangling pointer to done_wait. To make this
662 * correct, we'll need to allocate wait_done for
663 * each test iteration and perform "who's gonna
664 * free it this time?" dancing. For now, just
667 thread_result_add(info, result, DMATEST_ET_TIMEOUT,
668 total_tests, src_off, dst_off,
672 } else if (status != DMA_SUCCESS) {
673 enum dmatest_error_type type = (status == DMA_ERROR) ?
674 DMATEST_ET_DMA_ERROR : DMATEST_ET_DMA_IN_PROGRESS;
675 thread_result_add(info, result, type,
676 total_tests, src_off, dst_off,
682 /* Unmap by myself */
683 unmap_src(dev->dev, dma_srcs, len, src_cnt);
684 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
688 pr_debug("%s: verifying source buffer...\n", thread_name);
689 error_count += dmatest_verify(thread->srcs, 0, src_off,
690 0, PATTERN_SRC, true);
691 error_count += dmatest_verify(thread->srcs, src_off,
692 src_off + len, src_off,
693 PATTERN_SRC | PATTERN_COPY, true);
694 error_count += dmatest_verify(thread->srcs, src_off + len,
695 params->buf_size, src_off + len,
698 pr_debug("%s: verifying dest buffer...\n",
700 error_count += dmatest_verify(thread->dsts, 0, dst_off,
701 0, PATTERN_DST, false);
702 error_count += dmatest_verify(thread->dsts, dst_off,
703 dst_off + len, src_off,
704 PATTERN_SRC | PATTERN_COPY, false);
705 error_count += dmatest_verify(thread->dsts, dst_off + len,
706 params->buf_size, dst_off + len,
710 thread_result_add(info, result, DMATEST_ET_VERIFY,
711 total_tests, src_off, dst_off,
715 thread_result_add(info, result, DMATEST_ET_OK,
716 total_tests, src_off, dst_off,
722 for (i = 0; thread->dsts[i]; i++)
723 kfree(thread->dsts[i]);
727 for (i = 0; thread->srcs[i]; i++)
728 kfree(thread->srcs[i]);
734 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
735 thread_name, total_tests, failed_tests, ret);
737 /* terminate all transfers on specified channels */
739 dmaengine_terminate_all(chan);
743 if (params->iterations > 0)
744 while (!kthread_should_stop()) {
745 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
746 interruptible_sleep_on(&wait_dmatest_exit);
752 static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
754 struct dmatest_thread *thread;
755 struct dmatest_thread *_thread;
758 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
759 ret = kthread_stop(thread->task);
760 pr_debug("dmatest: thread %s exited with status %d\n",
761 thread->task->comm, ret);
762 list_del(&thread->node);
766 /* terminate all transfers on specified channels */
767 dmaengine_terminate_all(dtc->chan);
772 static int dmatest_add_threads(struct dmatest_info *info,
773 struct dmatest_chan *dtc, enum dma_transaction_type type)
775 struct dmatest_params *params = &info->params;
776 struct dmatest_thread *thread;
777 struct dma_chan *chan = dtc->chan;
781 if (type == DMA_MEMCPY)
783 else if (type == DMA_XOR)
785 else if (type == DMA_PQ)
790 for (i = 0; i < params->threads_per_chan; i++) {
791 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
793 pr_warning("dmatest: No memory for %s-%s%u\n",
794 dma_chan_name(chan), op, i);
799 thread->chan = dtc->chan;
802 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
803 dma_chan_name(chan), op, i);
804 if (IS_ERR(thread->task)) {
805 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
806 dma_chan_name(chan), op, i);
811 /* srcbuf and dstbuf are allocated by the thread itself */
813 list_add_tail(&thread->node, &dtc->threads);
819 static int dmatest_add_channel(struct dmatest_info *info,
820 struct dma_chan *chan)
822 struct dmatest_chan *dtc;
823 struct dma_device *dma_dev = chan->device;
824 unsigned int thread_count = 0;
827 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
829 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
834 INIT_LIST_HEAD(&dtc->threads);
836 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
837 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
838 thread_count += cnt > 0 ? cnt : 0;
840 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
841 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
842 thread_count += cnt > 0 ? cnt : 0;
844 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
845 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
846 thread_count += cnt > 0 ? cnt : 0;
849 pr_info("dmatest: Started %u threads using %s\n",
850 thread_count, dma_chan_name(chan));
852 list_add_tail(&dtc->node, &info->channels);
858 static bool filter(struct dma_chan *chan, void *param)
860 struct dmatest_params *params = param;
862 if (!dmatest_match_channel(params, chan) ||
863 !dmatest_match_device(params, chan->device))
869 static int __run_threaded_test(struct dmatest_info *info)
872 struct dma_chan *chan;
873 struct dmatest_params *params = &info->params;
877 dma_cap_set(DMA_MEMCPY, mask);
879 chan = dma_request_channel(mask, filter, params);
881 err = dmatest_add_channel(info, chan);
883 dma_release_channel(chan);
884 break; /* add_channel failed, punt */
887 break; /* no more channels available */
888 if (params->max_channels &&
889 info->nr_channels >= params->max_channels)
890 break; /* we have all we need */
896 static int run_threaded_test(struct dmatest_info *info)
900 mutex_lock(&info->lock);
901 ret = __run_threaded_test(info);
902 mutex_unlock(&info->lock);
907 static void __stop_threaded_test(struct dmatest_info *info)
909 struct dmatest_chan *dtc, *_dtc;
910 struct dma_chan *chan;
912 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
913 list_del(&dtc->node);
915 dmatest_cleanup_channel(dtc);
916 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
917 dma_release_channel(chan);
920 info->nr_channels = 0;
923 static void stop_threaded_test(struct dmatest_info *info)
925 mutex_lock(&info->lock);
926 __stop_threaded_test(info);
927 mutex_unlock(&info->lock);
930 static int __restart_threaded_test(struct dmatest_info *info, bool run)
932 struct dmatest_params *params = &info->params;
934 /* Stop any running test first */
935 __stop_threaded_test(info);
940 /* Clear results from previous run */
941 result_free(info, NULL);
943 /* Copy test parameters */
944 params->buf_size = test_buf_size;
945 strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
946 strlcpy(params->device, strim(test_device), sizeof(params->device));
947 params->threads_per_chan = threads_per_chan;
948 params->max_channels = max_channels;
949 params->iterations = iterations;
950 params->xor_sources = xor_sources;
951 params->pq_sources = pq_sources;
952 params->timeout = timeout;
954 /* Run test with new parameters */
955 return __run_threaded_test(info);
958 static bool __is_threaded_test_run(struct dmatest_info *info)
960 struct dmatest_chan *dtc;
962 list_for_each_entry(dtc, &info->channels, node) {
963 struct dmatest_thread *thread;
965 list_for_each_entry(thread, &dtc->threads, node) {
974 static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
975 size_t count, loff_t *ppos)
977 struct dmatest_info *info = file->private_data;
980 mutex_lock(&info->lock);
982 if (__is_threaded_test_run(info)) {
985 __stop_threaded_test(info);
989 mutex_unlock(&info->lock);
992 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
995 static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
996 size_t count, loff_t *ppos)
998 struct dmatest_info *info = file->private_data;
1003 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
1006 if (strtobool(buf, &bv) == 0) {
1007 mutex_lock(&info->lock);
1009 if (__is_threaded_test_run(info))
1012 ret = __restart_threaded_test(info, bv);
1014 mutex_unlock(&info->lock);
1017 return ret ? ret : count;
1020 static const struct file_operations dtf_run_fops = {
1021 .read = dtf_read_run,
1022 .write = dtf_write_run,
1023 .open = simple_open,
1024 .llseek = default_llseek,
1027 static int dtf_results_show(struct seq_file *sf, void *data)
1029 struct dmatest_info *info = sf->private;
1030 struct dmatest_result *result;
1031 struct dmatest_thread_result *tr;
1033 mutex_lock(&info->results_lock);
1034 list_for_each_entry(result, &info->results, node) {
1035 list_for_each_entry(tr, &result->results, node)
1036 seq_printf(sf, "%s\n",
1037 thread_result_get(result->name, tr));
1040 mutex_unlock(&info->results_lock);
1044 static int dtf_results_open(struct inode *inode, struct file *file)
1046 return single_open(file, dtf_results_show, inode->i_private);
1049 static const struct file_operations dtf_results_fops = {
1050 .open = dtf_results_open,
1052 .llseek = seq_lseek,
1053 .release = single_release,
1056 static int dmatest_register_dbgfs(struct dmatest_info *info)
1060 d = debugfs_create_dir("dmatest", NULL);
1068 /* Run or stop threaded test */
1069 debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root, info,
1072 /* Results of test in progress */
1073 debugfs_create_file("results", S_IRUGO, info->root, info,
1079 pr_err("dmatest: Failed to initialize debugfs\n");
1083 static int __init dmatest_init(void)
1085 struct dmatest_info *info = &test_info;
1088 memset(info, 0, sizeof(*info));
1090 mutex_init(&info->lock);
1091 INIT_LIST_HEAD(&info->channels);
1093 mutex_init(&info->results_lock);
1094 INIT_LIST_HEAD(&info->results);
1096 ret = dmatest_register_dbgfs(info);
1103 return run_threaded_test(info);
1106 /* when compiled-in wait for drivers to load first */
1107 late_initcall(dmatest_init);
1109 static void __exit dmatest_exit(void)
1111 struct dmatest_info *info = &test_info;
1113 debugfs_remove_recursive(info->root);
1114 stop_threaded_test(info);
1115 result_free(info, NULL);
1117 module_exit(dmatest_exit);
1119 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1120 MODULE_LICENSE("GPL v2");