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;
99 struct list_head node;
100 struct dma_chan *chan;
101 struct list_head threads;
105 * struct dmatest_params - test parameters.
106 * @buf_size: size of the memcpy test buffer
107 * @channel: bus ID of the channel to test
108 * @device: bus ID of the DMA Engine to test
109 * @threads_per_chan: number of threads to start per channel
110 * @max_channels: maximum number of channels to use
111 * @iterations: iterations before stopping test
112 * @xor_sources: number of xor source buffers
113 * @pq_sources: number of p+q source buffers
114 * @timeout: transfer timeout in msec, -1 for infinite timeout
116 struct dmatest_params {
117 unsigned int buf_size;
120 unsigned int threads_per_chan;
121 unsigned int max_channels;
122 unsigned int iterations;
123 unsigned int xor_sources;
124 unsigned int pq_sources;
129 * struct dmatest_info - test information.
130 * @params: test parameters
131 * @lock: access protection to the fields of this structure
133 struct dmatest_info {
134 /* Test parameters */
135 struct dmatest_params params;
138 struct list_head channels;
139 unsigned int nr_channels;
142 /* debugfs related stuff */
144 struct dmatest_params dbgfs_params;
147 static struct dmatest_info test_info;
149 static bool dmatest_match_channel(struct dmatest_params *params,
150 struct dma_chan *chan)
152 if (params->channel[0] == '\0')
154 return strcmp(dma_chan_name(chan), params->channel) == 0;
157 static bool dmatest_match_device(struct dmatest_params *params,
158 struct dma_device *device)
160 if (params->device[0] == '\0')
162 return strcmp(dev_name(device->dev), params->device) == 0;
165 static unsigned long dmatest_random(void)
169 get_random_bytes(&buf, sizeof(buf));
173 static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
174 unsigned int buf_size)
179 for (; (buf = *bufs); bufs++) {
180 for (i = 0; i < start; i++)
181 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
182 for ( ; i < start + len; i++)
183 buf[i] = PATTERN_SRC | PATTERN_COPY
184 | (~i & PATTERN_COUNT_MASK);
185 for ( ; i < buf_size; i++)
186 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
191 static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
192 unsigned int buf_size)
197 for (; (buf = *bufs); bufs++) {
198 for (i = 0; i < start; i++)
199 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
200 for ( ; i < start + len; i++)
201 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
202 | (~i & PATTERN_COUNT_MASK);
203 for ( ; i < buf_size; i++)
204 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
208 static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
209 unsigned int counter, bool is_srcbuf)
211 u8 diff = actual ^ pattern;
212 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
213 const char *thread_name = current->comm;
216 pr_warning("%s: srcbuf[0x%x] overwritten!"
217 " Expected %02x, got %02x\n",
218 thread_name, index, expected, actual);
219 else if ((pattern & PATTERN_COPY)
220 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
221 pr_warning("%s: dstbuf[0x%x] not copied!"
222 " Expected %02x, got %02x\n",
223 thread_name, index, expected, actual);
224 else if (diff & PATTERN_SRC)
225 pr_warning("%s: dstbuf[0x%x] was copied!"
226 " Expected %02x, got %02x\n",
227 thread_name, index, expected, actual);
229 pr_warning("%s: dstbuf[0x%x] mismatch!"
230 " Expected %02x, got %02x\n",
231 thread_name, index, expected, actual);
234 static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
235 unsigned int end, unsigned int counter, u8 pattern,
239 unsigned int error_count = 0;
243 unsigned int counter_orig = counter;
245 for (; (buf = *bufs); bufs++) {
246 counter = counter_orig;
247 for (i = start; i < end; i++) {
249 expected = pattern | (~counter & PATTERN_COUNT_MASK);
250 if (actual != expected) {
251 if (error_count < 32)
252 dmatest_mismatch(actual, pattern, i,
260 if (error_count > 32)
261 pr_warning("%s: %u errors suppressed\n",
262 current->comm, error_count - 32);
267 /* poor man's completion - we want to use wait_event_freezable() on it */
268 struct dmatest_done {
270 wait_queue_head_t *wait;
273 static void dmatest_callback(void *arg)
275 struct dmatest_done *done = arg;
278 wake_up_all(done->wait);
281 static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
285 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
288 static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
292 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
295 static unsigned int min_odd(unsigned int x, unsigned int y)
297 unsigned int val = min(x, y);
299 return val % 2 ? val : val - 1;
303 * This function repeatedly tests DMA transfers of various lengths and
304 * offsets for a given operation type until it is told to exit by
305 * kthread_stop(). There may be multiple threads running this function
306 * in parallel for a single channel, and there may be multiple channels
307 * being tested in parallel.
309 * Before each test, the source and destination buffer is initialized
310 * with a known pattern. This pattern is different depending on
311 * whether it's in an area which is supposed to be copied or
312 * overwritten, and different in the source and destination buffers.
313 * So if the DMA engine doesn't copy exactly what we tell it to copy,
316 static int dmatest_func(void *data)
318 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
319 struct dmatest_thread *thread = data;
320 struct dmatest_done done = { .wait = &done_wait };
321 struct dmatest_info *info;
322 struct dmatest_params *params;
323 struct dma_chan *chan;
324 struct dma_device *dev;
325 const char *thread_name;
326 unsigned int src_off, dst_off, len;
327 unsigned int error_count;
328 unsigned int failed_tests = 0;
329 unsigned int total_tests = 0;
331 enum dma_status status;
332 enum dma_ctrl_flags flags;
339 thread_name = current->comm;
346 params = &info->params;
349 if (thread->type == DMA_MEMCPY)
350 src_cnt = dst_cnt = 1;
351 else if (thread->type == DMA_XOR) {
352 /* force odd to ensure dst = src */
353 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
355 } else if (thread->type == DMA_PQ) {
356 /* force odd to ensure dst = src */
357 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
360 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
362 goto err_thread_type;
364 for (i = 0; i < src_cnt; i++)
367 goto err_thread_type;
369 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
372 for (i = 0; i < src_cnt; i++) {
373 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
374 if (!thread->srcs[i])
377 thread->srcs[i] = NULL;
379 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
382 for (i = 0; i < dst_cnt; i++) {
383 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
384 if (!thread->dsts[i])
387 thread->dsts[i] = NULL;
389 set_user_nice(current, 10);
392 * src buffers are freed by the DMAEngine code with dma_unmap_single()
393 * dst buffers are freed by ourselves below
395 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
396 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
398 while (!kthread_should_stop()
399 && !(params->iterations && total_tests >= params->iterations)) {
400 struct dma_async_tx_descriptor *tx = NULL;
401 dma_addr_t dma_srcs[src_cnt];
402 dma_addr_t dma_dsts[dst_cnt];
407 /* honor alignment restrictions */
408 if (thread->type == DMA_MEMCPY)
409 align = dev->copy_align;
410 else if (thread->type == DMA_XOR)
411 align = dev->xor_align;
412 else if (thread->type == DMA_PQ)
413 align = dev->pq_align;
415 if (1 << align > params->buf_size) {
416 pr_err("%u-byte buffer too small for %d-byte alignment\n",
417 params->buf_size, 1 << align);
421 len = dmatest_random() % params->buf_size + 1;
422 len = (len >> align) << align;
425 src_off = dmatest_random() % (params->buf_size - len + 1);
426 dst_off = dmatest_random() % (params->buf_size - len + 1);
428 src_off = (src_off >> align) << align;
429 dst_off = (dst_off >> align) << align;
431 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
432 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
434 for (i = 0; i < src_cnt; i++) {
435 u8 *buf = thread->srcs[i] + src_off;
437 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
439 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
441 unmap_src(dev->dev, dma_srcs, len, i);
442 pr_warn("%s: #%u: mapping error %d with "
443 "src_off=0x%x len=0x%x\n",
444 thread_name, total_tests - 1, ret,
450 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
451 for (i = 0; i < dst_cnt; i++) {
452 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
455 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
457 unmap_src(dev->dev, dma_srcs, len, src_cnt);
458 unmap_dst(dev->dev, dma_dsts, params->buf_size,
460 pr_warn("%s: #%u: mapping error %d with "
461 "dst_off=0x%x len=0x%x\n",
462 thread_name, total_tests - 1, ret,
463 dst_off, params->buf_size);
469 if (thread->type == DMA_MEMCPY)
470 tx = dev->device_prep_dma_memcpy(chan,
471 dma_dsts[0] + dst_off,
474 else if (thread->type == DMA_XOR)
475 tx = dev->device_prep_dma_xor(chan,
476 dma_dsts[0] + dst_off,
479 else if (thread->type == DMA_PQ) {
480 dma_addr_t dma_pq[dst_cnt];
482 for (i = 0; i < dst_cnt; i++)
483 dma_pq[i] = dma_dsts[i] + dst_off;
484 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
490 unmap_src(dev->dev, dma_srcs, len, src_cnt);
491 unmap_dst(dev->dev, dma_dsts, params->buf_size,
493 pr_warning("%s: #%u: prep error with src_off=0x%x "
494 "dst_off=0x%x len=0x%x\n",
495 thread_name, total_tests - 1,
496 src_off, dst_off, len);
503 tx->callback = dmatest_callback;
504 tx->callback_param = &done;
505 cookie = tx->tx_submit(tx);
507 if (dma_submit_error(cookie)) {
508 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
509 "dst_off=0x%x len=0x%x\n",
510 thread_name, total_tests - 1, cookie,
511 src_off, dst_off, len);
516 dma_async_issue_pending(chan);
518 wait_event_freezable_timeout(done_wait,
519 done.done || kthread_should_stop(),
520 msecs_to_jiffies(params->timeout));
522 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
526 * We're leaving the timed out dma operation with
527 * dangling pointer to done_wait. To make this
528 * correct, we'll need to allocate wait_done for
529 * each test iteration and perform "who's gonna
530 * free it this time?" dancing. For now, just
533 pr_warning("%s: #%u: test timed out\n",
534 thread_name, total_tests - 1);
537 } else if (status != DMA_SUCCESS) {
538 pr_warning("%s: #%u: got completion callback,"
539 " but status is \'%s\'\n",
540 thread_name, total_tests - 1,
541 status == DMA_ERROR ? "error" : "in progress");
546 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
547 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
551 pr_debug("%s: verifying source buffer...\n", thread_name);
552 error_count += dmatest_verify(thread->srcs, 0, src_off,
553 0, PATTERN_SRC, true);
554 error_count += dmatest_verify(thread->srcs, src_off,
555 src_off + len, src_off,
556 PATTERN_SRC | PATTERN_COPY, true);
557 error_count += dmatest_verify(thread->srcs, src_off + len,
558 params->buf_size, src_off + len,
561 pr_debug("%s: verifying dest buffer...\n",
563 error_count += dmatest_verify(thread->dsts, 0, dst_off,
564 0, PATTERN_DST, false);
565 error_count += dmatest_verify(thread->dsts, dst_off,
566 dst_off + len, src_off,
567 PATTERN_SRC | PATTERN_COPY, false);
568 error_count += dmatest_verify(thread->dsts, dst_off + len,
569 params->buf_size, dst_off + len,
573 pr_warning("%s: #%u: %u errors with "
574 "src_off=0x%x dst_off=0x%x len=0x%x\n",
575 thread_name, total_tests - 1, error_count,
576 src_off, dst_off, len);
579 pr_debug("%s: #%u: No errors with "
580 "src_off=0x%x dst_off=0x%x len=0x%x\n",
581 thread_name, total_tests - 1,
582 src_off, dst_off, len);
587 for (i = 0; thread->dsts[i]; i++)
588 kfree(thread->dsts[i]);
592 for (i = 0; thread->srcs[i]; i++)
593 kfree(thread->srcs[i]);
599 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
600 thread_name, total_tests, failed_tests, ret);
602 /* terminate all transfers on specified channels */
604 dmaengine_terminate_all(chan);
606 if (params->iterations > 0)
607 while (!kthread_should_stop()) {
608 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
609 interruptible_sleep_on(&wait_dmatest_exit);
615 static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
617 struct dmatest_thread *thread;
618 struct dmatest_thread *_thread;
621 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
622 ret = kthread_stop(thread->task);
623 pr_debug("dmatest: thread %s exited with status %d\n",
624 thread->task->comm, ret);
625 list_del(&thread->node);
629 /* terminate all transfers on specified channels */
630 dmaengine_terminate_all(dtc->chan);
635 static int dmatest_add_threads(struct dmatest_info *info,
636 struct dmatest_chan *dtc, enum dma_transaction_type type)
638 struct dmatest_params *params = &info->params;
639 struct dmatest_thread *thread;
640 struct dma_chan *chan = dtc->chan;
644 if (type == DMA_MEMCPY)
646 else if (type == DMA_XOR)
648 else if (type == DMA_PQ)
653 for (i = 0; i < params->threads_per_chan; i++) {
654 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
656 pr_warning("dmatest: No memory for %s-%s%u\n",
657 dma_chan_name(chan), op, i);
662 thread->chan = dtc->chan;
665 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
666 dma_chan_name(chan), op, i);
667 if (IS_ERR(thread->task)) {
668 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
669 dma_chan_name(chan), op, i);
674 /* srcbuf and dstbuf are allocated by the thread itself */
676 list_add_tail(&thread->node, &dtc->threads);
682 static int dmatest_add_channel(struct dmatest_info *info,
683 struct dma_chan *chan)
685 struct dmatest_chan *dtc;
686 struct dma_device *dma_dev = chan->device;
687 unsigned int thread_count = 0;
690 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
692 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
697 INIT_LIST_HEAD(&dtc->threads);
699 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
700 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
701 thread_count += cnt > 0 ? cnt : 0;
703 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
704 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
705 thread_count += cnt > 0 ? cnt : 0;
707 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
708 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
709 thread_count += cnt > 0 ? cnt : 0;
712 pr_info("dmatest: Started %u threads using %s\n",
713 thread_count, dma_chan_name(chan));
715 list_add_tail(&dtc->node, &info->channels);
721 static bool filter(struct dma_chan *chan, void *param)
723 struct dmatest_params *params = param;
725 if (!dmatest_match_channel(params, chan) ||
726 !dmatest_match_device(params, chan->device))
732 static int __run_threaded_test(struct dmatest_info *info)
735 struct dma_chan *chan;
736 struct dmatest_params *params = &info->params;
740 dma_cap_set(DMA_MEMCPY, mask);
742 chan = dma_request_channel(mask, filter, params);
744 err = dmatest_add_channel(info, chan);
746 dma_release_channel(chan);
747 break; /* add_channel failed, punt */
750 break; /* no more channels available */
751 if (params->max_channels &&
752 info->nr_channels >= params->max_channels)
753 break; /* we have all we need */
759 static int run_threaded_test(struct dmatest_info *info)
763 mutex_lock(&info->lock);
764 ret = __run_threaded_test(info);
765 mutex_unlock(&info->lock);
770 static void __stop_threaded_test(struct dmatest_info *info)
772 struct dmatest_chan *dtc, *_dtc;
773 struct dma_chan *chan;
775 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
776 list_del(&dtc->node);
778 dmatest_cleanup_channel(dtc);
779 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
780 dma_release_channel(chan);
783 info->nr_channels = 0;
786 static void stop_threaded_test(struct dmatest_info *info)
788 mutex_lock(&info->lock);
789 __stop_threaded_test(info);
790 mutex_unlock(&info->lock);
793 static int __restart_threaded_test(struct dmatest_info *info, bool run)
795 struct dmatest_params *params = &info->params;
798 /* Stop any running test first */
799 __stop_threaded_test(info);
804 /* Copy test parameters */
805 memcpy(params, &info->dbgfs_params, sizeof(*params));
807 /* Run test with new parameters */
808 ret = __run_threaded_test(info);
810 __stop_threaded_test(info);
811 pr_err("dmatest: Can't run test\n");
817 static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos,
818 const void __user *from, size_t count)
823 len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count);
826 strlcpy(to, strim(tmp), available);
832 static ssize_t dtf_read_channel(struct file *file, char __user *buf,
833 size_t count, loff_t *ppos)
835 struct dmatest_info *info = file->private_data;
836 return simple_read_from_buffer(buf, count, ppos,
837 info->dbgfs_params.channel,
838 strlen(info->dbgfs_params.channel));
841 static ssize_t dtf_write_channel(struct file *file, const char __user *buf,
842 size_t size, loff_t *ppos)
844 struct dmatest_info *info = file->private_data;
845 return dtf_write_string(info->dbgfs_params.channel,
846 sizeof(info->dbgfs_params.channel),
850 static const struct file_operations dtf_channel_fops = {
851 .read = dtf_read_channel,
852 .write = dtf_write_channel,
854 .llseek = default_llseek,
857 static ssize_t dtf_read_device(struct file *file, char __user *buf,
858 size_t count, loff_t *ppos)
860 struct dmatest_info *info = file->private_data;
861 return simple_read_from_buffer(buf, count, ppos,
862 info->dbgfs_params.device,
863 strlen(info->dbgfs_params.device));
866 static ssize_t dtf_write_device(struct file *file, const char __user *buf,
867 size_t size, loff_t *ppos)
869 struct dmatest_info *info = file->private_data;
870 return dtf_write_string(info->dbgfs_params.device,
871 sizeof(info->dbgfs_params.device),
875 static const struct file_operations dtf_device_fops = {
876 .read = dtf_read_device,
877 .write = dtf_write_device,
879 .llseek = default_llseek,
882 static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
883 size_t count, loff_t *ppos)
885 struct dmatest_info *info = file->private_data;
888 mutex_lock(&info->lock);
889 if (info->nr_channels)
893 mutex_unlock(&info->lock);
896 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
899 static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
900 size_t count, loff_t *ppos)
902 struct dmatest_info *info = file->private_data;
907 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
910 if (strtobool(buf, &bv) == 0) {
911 mutex_lock(&info->lock);
912 ret = __restart_threaded_test(info, bv);
913 mutex_unlock(&info->lock);
916 return ret ? ret : count;
919 static const struct file_operations dtf_run_fops = {
920 .read = dtf_read_run,
921 .write = dtf_write_run,
923 .llseek = default_llseek,
926 static int dmatest_register_dbgfs(struct dmatest_info *info)
929 struct dmatest_params *params = &info->dbgfs_params;
932 d = debugfs_create_dir("dmatest", NULL);
940 /* Copy initial values */
941 memcpy(params, &info->params, sizeof(*params));
943 /* Test parameters */
945 d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root,
946 (u32 *)¶ms->buf_size);
947 if (IS_ERR_OR_NULL(d))
950 d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root,
951 info, &dtf_channel_fops);
952 if (IS_ERR_OR_NULL(d))
955 d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root,
956 info, &dtf_device_fops);
957 if (IS_ERR_OR_NULL(d))
960 d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root,
961 (u32 *)¶ms->threads_per_chan);
962 if (IS_ERR_OR_NULL(d))
965 d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root,
966 (u32 *)¶ms->max_channels);
967 if (IS_ERR_OR_NULL(d))
970 d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root,
971 (u32 *)¶ms->iterations);
972 if (IS_ERR_OR_NULL(d))
975 d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root,
976 (u32 *)¶ms->xor_sources);
977 if (IS_ERR_OR_NULL(d))
980 d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root,
981 (u32 *)¶ms->pq_sources);
982 if (IS_ERR_OR_NULL(d))
985 d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root,
986 (u32 *)¶ms->timeout);
987 if (IS_ERR_OR_NULL(d))
990 /* Run or stop threaded test */
991 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
992 info, &dtf_run_fops);
993 if (IS_ERR_OR_NULL(d))
999 debugfs_remove_recursive(info->root);
1001 pr_err("dmatest: Failed to initialize debugfs\n");
1005 static int __init dmatest_init(void)
1007 struct dmatest_info *info = &test_info;
1008 struct dmatest_params *params = &info->params;
1011 memset(info, 0, sizeof(*info));
1013 mutex_init(&info->lock);
1014 INIT_LIST_HEAD(&info->channels);
1016 /* Set default parameters */
1017 params->buf_size = test_buf_size;
1018 strlcpy(params->channel, test_channel, sizeof(params->channel));
1019 strlcpy(params->device, test_device, sizeof(params->device));
1020 params->threads_per_chan = threads_per_chan;
1021 params->max_channels = max_channels;
1022 params->iterations = iterations;
1023 params->xor_sources = xor_sources;
1024 params->pq_sources = pq_sources;
1025 params->timeout = timeout;
1027 ret = dmatest_register_dbgfs(info);
1034 return run_threaded_test(info);
1037 /* when compiled-in wait for drivers to load first */
1038 late_initcall(dmatest_init);
1040 static void __exit dmatest_exit(void)
1042 struct dmatest_info *info = &test_info;
1044 debugfs_remove_recursive(info->root);
1045 stop_threaded_test(info);
1047 module_exit(dmatest_exit);
1049 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1050 MODULE_LICENSE("GPL v2");