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
91 struct dmatest_thread {
92 struct list_head node;
93 struct dmatest_info *info;
94 struct task_struct *task;
95 struct dma_chan *chan;
98 enum dma_transaction_type type;
102 struct dmatest_chan {
103 struct list_head node;
104 struct dma_chan *chan;
105 struct list_head threads;
109 * struct dmatest_params - test parameters.
110 * @buf_size: size of the memcpy test buffer
111 * @channel: bus ID of the channel to test
112 * @device: bus ID of the DMA Engine to test
113 * @threads_per_chan: number of threads to start per channel
114 * @max_channels: maximum number of channels to use
115 * @iterations: iterations before stopping test
116 * @xor_sources: number of xor source buffers
117 * @pq_sources: number of p+q source buffers
118 * @timeout: transfer timeout in msec, -1 for infinite timeout
120 struct dmatest_params {
121 unsigned int buf_size;
124 unsigned int threads_per_chan;
125 unsigned int max_channels;
126 unsigned int iterations;
127 unsigned int xor_sources;
128 unsigned int pq_sources;
133 * struct dmatest_info - test information.
134 * @params: test parameters
135 * @lock: access protection to the fields of this structure
137 struct dmatest_info {
138 /* Test parameters */
139 struct dmatest_params params;
142 struct list_head channels;
143 unsigned int nr_channels;
146 /* debugfs related stuff */
148 struct dmatest_params dbgfs_params;
151 static struct dmatest_info test_info;
153 static bool dmatest_match_channel(struct dmatest_params *params,
154 struct dma_chan *chan)
156 if (params->channel[0] == '\0')
158 return strcmp(dma_chan_name(chan), params->channel) == 0;
161 static bool dmatest_match_device(struct dmatest_params *params,
162 struct dma_device *device)
164 if (params->device[0] == '\0')
166 return strcmp(dev_name(device->dev), params->device) == 0;
169 static unsigned long dmatest_random(void)
173 get_random_bytes(&buf, sizeof(buf));
177 static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
178 unsigned int buf_size)
183 for (; (buf = *bufs); bufs++) {
184 for (i = 0; i < start; i++)
185 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
186 for ( ; i < start + len; i++)
187 buf[i] = PATTERN_SRC | PATTERN_COPY
188 | (~i & PATTERN_COUNT_MASK);
189 for ( ; i < buf_size; i++)
190 buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
195 static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
196 unsigned int buf_size)
201 for (; (buf = *bufs); bufs++) {
202 for (i = 0; i < start; i++)
203 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
204 for ( ; i < start + len; i++)
205 buf[i] = PATTERN_DST | PATTERN_OVERWRITE
206 | (~i & PATTERN_COUNT_MASK);
207 for ( ; i < buf_size; i++)
208 buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
212 static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
213 unsigned int counter, bool is_srcbuf)
215 u8 diff = actual ^ pattern;
216 u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
217 const char *thread_name = current->comm;
220 pr_warning("%s: srcbuf[0x%x] overwritten!"
221 " Expected %02x, got %02x\n",
222 thread_name, index, expected, actual);
223 else if ((pattern & PATTERN_COPY)
224 && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
225 pr_warning("%s: dstbuf[0x%x] not copied!"
226 " Expected %02x, got %02x\n",
227 thread_name, index, expected, actual);
228 else if (diff & PATTERN_SRC)
229 pr_warning("%s: dstbuf[0x%x] was copied!"
230 " Expected %02x, got %02x\n",
231 thread_name, index, expected, actual);
233 pr_warning("%s: dstbuf[0x%x] mismatch!"
234 " Expected %02x, got %02x\n",
235 thread_name, index, expected, actual);
238 static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
239 unsigned int end, unsigned int counter, u8 pattern,
243 unsigned int error_count = 0;
247 unsigned int counter_orig = counter;
249 for (; (buf = *bufs); bufs++) {
250 counter = counter_orig;
251 for (i = start; i < end; i++) {
253 expected = pattern | (~counter & PATTERN_COUNT_MASK);
254 if (actual != expected) {
255 if (error_count < MAX_ERROR_COUNT)
256 dmatest_mismatch(actual, pattern, i,
264 if (error_count > MAX_ERROR_COUNT)
265 pr_warning("%s: %u errors suppressed\n",
266 current->comm, error_count - MAX_ERROR_COUNT);
271 /* poor man's completion - we want to use wait_event_freezable() on it */
272 struct dmatest_done {
274 wait_queue_head_t *wait;
277 static void dmatest_callback(void *arg)
279 struct dmatest_done *done = arg;
282 wake_up_all(done->wait);
285 static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
289 dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
292 static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
296 dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
299 static unsigned int min_odd(unsigned int x, unsigned int y)
301 unsigned int val = min(x, y);
303 return val % 2 ? val : val - 1;
307 * This function repeatedly tests DMA transfers of various lengths and
308 * offsets for a given operation type until it is told to exit by
309 * kthread_stop(). There may be multiple threads running this function
310 * in parallel for a single channel, and there may be multiple channels
311 * being tested in parallel.
313 * Before each test, the source and destination buffer is initialized
314 * with a known pattern. This pattern is different depending on
315 * whether it's in an area which is supposed to be copied or
316 * overwritten, and different in the source and destination buffers.
317 * So if the DMA engine doesn't copy exactly what we tell it to copy,
320 static int dmatest_func(void *data)
322 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
323 struct dmatest_thread *thread = data;
324 struct dmatest_done done = { .wait = &done_wait };
325 struct dmatest_info *info;
326 struct dmatest_params *params;
327 struct dma_chan *chan;
328 struct dma_device *dev;
329 const char *thread_name;
330 unsigned int src_off, dst_off, len;
331 unsigned int error_count;
332 unsigned int failed_tests = 0;
333 unsigned int total_tests = 0;
335 enum dma_status status;
336 enum dma_ctrl_flags flags;
343 thread_name = current->comm;
350 params = &info->params;
353 if (thread->type == DMA_MEMCPY)
354 src_cnt = dst_cnt = 1;
355 else if (thread->type == DMA_XOR) {
356 /* force odd to ensure dst = src */
357 src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
359 } else if (thread->type == DMA_PQ) {
360 /* force odd to ensure dst = src */
361 src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
364 pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
366 goto err_thread_type;
368 for (i = 0; i < src_cnt; i++)
371 goto err_thread_type;
373 thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
376 for (i = 0; i < src_cnt; i++) {
377 thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
378 if (!thread->srcs[i])
381 thread->srcs[i] = NULL;
383 thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
386 for (i = 0; i < dst_cnt; i++) {
387 thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
388 if (!thread->dsts[i])
391 thread->dsts[i] = NULL;
393 set_user_nice(current, 10);
396 * src buffers are freed by the DMAEngine code with dma_unmap_single()
397 * dst buffers are freed by ourselves below
399 flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
400 | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
402 while (!kthread_should_stop()
403 && !(params->iterations && total_tests >= params->iterations)) {
404 struct dma_async_tx_descriptor *tx = NULL;
405 dma_addr_t dma_srcs[src_cnt];
406 dma_addr_t dma_dsts[dst_cnt];
411 /* honor alignment restrictions */
412 if (thread->type == DMA_MEMCPY)
413 align = dev->copy_align;
414 else if (thread->type == DMA_XOR)
415 align = dev->xor_align;
416 else if (thread->type == DMA_PQ)
417 align = dev->pq_align;
419 if (1 << align > params->buf_size) {
420 pr_err("%u-byte buffer too small for %d-byte alignment\n",
421 params->buf_size, 1 << align);
425 len = dmatest_random() % params->buf_size + 1;
426 len = (len >> align) << align;
429 src_off = dmatest_random() % (params->buf_size - len + 1);
430 dst_off = dmatest_random() % (params->buf_size - len + 1);
432 src_off = (src_off >> align) << align;
433 dst_off = (dst_off >> align) << align;
435 dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
436 dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
438 for (i = 0; i < src_cnt; i++) {
439 u8 *buf = thread->srcs[i] + src_off;
441 dma_srcs[i] = dma_map_single(dev->dev, buf, len,
443 ret = dma_mapping_error(dev->dev, dma_srcs[i]);
445 unmap_src(dev->dev, dma_srcs, len, i);
446 pr_warn("%s: #%u: mapping error %d with "
447 "src_off=0x%x len=0x%x\n",
448 thread_name, total_tests - 1, ret,
454 /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
455 for (i = 0; i < dst_cnt; i++) {
456 dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
459 ret = dma_mapping_error(dev->dev, dma_dsts[i]);
461 unmap_src(dev->dev, dma_srcs, len, src_cnt);
462 unmap_dst(dev->dev, dma_dsts, params->buf_size,
464 pr_warn("%s: #%u: mapping error %d with "
465 "dst_off=0x%x len=0x%x\n",
466 thread_name, total_tests - 1, ret,
467 dst_off, params->buf_size);
473 if (thread->type == DMA_MEMCPY)
474 tx = dev->device_prep_dma_memcpy(chan,
475 dma_dsts[0] + dst_off,
478 else if (thread->type == DMA_XOR)
479 tx = dev->device_prep_dma_xor(chan,
480 dma_dsts[0] + dst_off,
483 else if (thread->type == DMA_PQ) {
484 dma_addr_t dma_pq[dst_cnt];
486 for (i = 0; i < dst_cnt; i++)
487 dma_pq[i] = dma_dsts[i] + dst_off;
488 tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
494 unmap_src(dev->dev, dma_srcs, len, src_cnt);
495 unmap_dst(dev->dev, dma_dsts, params->buf_size,
497 pr_warning("%s: #%u: prep error with src_off=0x%x "
498 "dst_off=0x%x len=0x%x\n",
499 thread_name, total_tests - 1,
500 src_off, dst_off, len);
507 tx->callback = dmatest_callback;
508 tx->callback_param = &done;
509 cookie = tx->tx_submit(tx);
511 if (dma_submit_error(cookie)) {
512 pr_warning("%s: #%u: submit error %d with src_off=0x%x "
513 "dst_off=0x%x len=0x%x\n",
514 thread_name, total_tests - 1, cookie,
515 src_off, dst_off, len);
520 dma_async_issue_pending(chan);
522 wait_event_freezable_timeout(done_wait,
523 done.done || kthread_should_stop(),
524 msecs_to_jiffies(params->timeout));
526 status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
530 * We're leaving the timed out dma operation with
531 * dangling pointer to done_wait. To make this
532 * correct, we'll need to allocate wait_done for
533 * each test iteration and perform "who's gonna
534 * free it this time?" dancing. For now, just
537 pr_warning("%s: #%u: test timed out\n",
538 thread_name, total_tests - 1);
541 } else if (status != DMA_SUCCESS) {
542 pr_warning("%s: #%u: got completion callback,"
543 " but status is \'%s\'\n",
544 thread_name, total_tests - 1,
545 status == DMA_ERROR ? "error" : "in progress");
550 /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
551 unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
555 pr_debug("%s: verifying source buffer...\n", thread_name);
556 error_count += dmatest_verify(thread->srcs, 0, src_off,
557 0, PATTERN_SRC, true);
558 error_count += dmatest_verify(thread->srcs, src_off,
559 src_off + len, src_off,
560 PATTERN_SRC | PATTERN_COPY, true);
561 error_count += dmatest_verify(thread->srcs, src_off + len,
562 params->buf_size, src_off + len,
565 pr_debug("%s: verifying dest buffer...\n",
567 error_count += dmatest_verify(thread->dsts, 0, dst_off,
568 0, PATTERN_DST, false);
569 error_count += dmatest_verify(thread->dsts, dst_off,
570 dst_off + len, src_off,
571 PATTERN_SRC | PATTERN_COPY, false);
572 error_count += dmatest_verify(thread->dsts, dst_off + len,
573 params->buf_size, dst_off + len,
577 pr_warning("%s: #%u: %u errors with "
578 "src_off=0x%x dst_off=0x%x len=0x%x\n",
579 thread_name, total_tests - 1, error_count,
580 src_off, dst_off, len);
583 pr_debug("%s: #%u: No errors with "
584 "src_off=0x%x dst_off=0x%x len=0x%x\n",
585 thread_name, total_tests - 1,
586 src_off, dst_off, len);
591 for (i = 0; thread->dsts[i]; i++)
592 kfree(thread->dsts[i]);
596 for (i = 0; thread->srcs[i]; i++)
597 kfree(thread->srcs[i]);
603 pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
604 thread_name, total_tests, failed_tests, ret);
606 /* terminate all transfers on specified channels */
608 dmaengine_terminate_all(chan);
612 if (params->iterations > 0)
613 while (!kthread_should_stop()) {
614 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
615 interruptible_sleep_on(&wait_dmatest_exit);
621 static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
623 struct dmatest_thread *thread;
624 struct dmatest_thread *_thread;
627 list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
628 ret = kthread_stop(thread->task);
629 pr_debug("dmatest: thread %s exited with status %d\n",
630 thread->task->comm, ret);
631 list_del(&thread->node);
635 /* terminate all transfers on specified channels */
636 dmaengine_terminate_all(dtc->chan);
641 static int dmatest_add_threads(struct dmatest_info *info,
642 struct dmatest_chan *dtc, enum dma_transaction_type type)
644 struct dmatest_params *params = &info->params;
645 struct dmatest_thread *thread;
646 struct dma_chan *chan = dtc->chan;
650 if (type == DMA_MEMCPY)
652 else if (type == DMA_XOR)
654 else if (type == DMA_PQ)
659 for (i = 0; i < params->threads_per_chan; i++) {
660 thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
662 pr_warning("dmatest: No memory for %s-%s%u\n",
663 dma_chan_name(chan), op, i);
668 thread->chan = dtc->chan;
671 thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
672 dma_chan_name(chan), op, i);
673 if (IS_ERR(thread->task)) {
674 pr_warning("dmatest: Failed to run thread %s-%s%u\n",
675 dma_chan_name(chan), op, i);
680 /* srcbuf and dstbuf are allocated by the thread itself */
682 list_add_tail(&thread->node, &dtc->threads);
688 static int dmatest_add_channel(struct dmatest_info *info,
689 struct dma_chan *chan)
691 struct dmatest_chan *dtc;
692 struct dma_device *dma_dev = chan->device;
693 unsigned int thread_count = 0;
696 dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
698 pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
703 INIT_LIST_HEAD(&dtc->threads);
705 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
706 cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
707 thread_count += cnt > 0 ? cnt : 0;
709 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
710 cnt = dmatest_add_threads(info, dtc, DMA_XOR);
711 thread_count += cnt > 0 ? cnt : 0;
713 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
714 cnt = dmatest_add_threads(info, dtc, DMA_PQ);
715 thread_count += cnt > 0 ? cnt : 0;
718 pr_info("dmatest: Started %u threads using %s\n",
719 thread_count, dma_chan_name(chan));
721 list_add_tail(&dtc->node, &info->channels);
727 static bool filter(struct dma_chan *chan, void *param)
729 struct dmatest_params *params = param;
731 if (!dmatest_match_channel(params, chan) ||
732 !dmatest_match_device(params, chan->device))
738 static int __run_threaded_test(struct dmatest_info *info)
741 struct dma_chan *chan;
742 struct dmatest_params *params = &info->params;
746 dma_cap_set(DMA_MEMCPY, mask);
748 chan = dma_request_channel(mask, filter, params);
750 err = dmatest_add_channel(info, chan);
752 dma_release_channel(chan);
753 break; /* add_channel failed, punt */
756 break; /* no more channels available */
757 if (params->max_channels &&
758 info->nr_channels >= params->max_channels)
759 break; /* we have all we need */
765 static int run_threaded_test(struct dmatest_info *info)
769 mutex_lock(&info->lock);
770 ret = __run_threaded_test(info);
771 mutex_unlock(&info->lock);
776 static void __stop_threaded_test(struct dmatest_info *info)
778 struct dmatest_chan *dtc, *_dtc;
779 struct dma_chan *chan;
781 list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
782 list_del(&dtc->node);
784 dmatest_cleanup_channel(dtc);
785 pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan));
786 dma_release_channel(chan);
789 info->nr_channels = 0;
792 static void stop_threaded_test(struct dmatest_info *info)
794 mutex_lock(&info->lock);
795 __stop_threaded_test(info);
796 mutex_unlock(&info->lock);
799 static int __restart_threaded_test(struct dmatest_info *info, bool run)
801 struct dmatest_params *params = &info->params;
804 /* Stop any running test first */
805 __stop_threaded_test(info);
810 /* Copy test parameters */
811 memcpy(params, &info->dbgfs_params, sizeof(*params));
813 /* Run test with new parameters */
814 ret = __run_threaded_test(info);
816 __stop_threaded_test(info);
817 pr_err("dmatest: Can't run test\n");
823 static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos,
824 const void __user *from, size_t count)
829 len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count);
832 strlcpy(to, strim(tmp), available);
838 static ssize_t dtf_read_channel(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
841 struct dmatest_info *info = file->private_data;
842 return simple_read_from_buffer(buf, count, ppos,
843 info->dbgfs_params.channel,
844 strlen(info->dbgfs_params.channel));
847 static ssize_t dtf_write_channel(struct file *file, const char __user *buf,
848 size_t size, loff_t *ppos)
850 struct dmatest_info *info = file->private_data;
851 return dtf_write_string(info->dbgfs_params.channel,
852 sizeof(info->dbgfs_params.channel),
856 static const struct file_operations dtf_channel_fops = {
857 .read = dtf_read_channel,
858 .write = dtf_write_channel,
860 .llseek = default_llseek,
863 static ssize_t dtf_read_device(struct file *file, char __user *buf,
864 size_t count, loff_t *ppos)
866 struct dmatest_info *info = file->private_data;
867 return simple_read_from_buffer(buf, count, ppos,
868 info->dbgfs_params.device,
869 strlen(info->dbgfs_params.device));
872 static ssize_t dtf_write_device(struct file *file, const char __user *buf,
873 size_t size, loff_t *ppos)
875 struct dmatest_info *info = file->private_data;
876 return dtf_write_string(info->dbgfs_params.device,
877 sizeof(info->dbgfs_params.device),
881 static const struct file_operations dtf_device_fops = {
882 .read = dtf_read_device,
883 .write = dtf_write_device,
885 .llseek = default_llseek,
888 static ssize_t dtf_read_run(struct file *file, char __user *user_buf,
889 size_t count, loff_t *ppos)
891 struct dmatest_info *info = file->private_data;
893 struct dmatest_chan *dtc;
896 mutex_lock(&info->lock);
897 list_for_each_entry(dtc, &info->channels, node) {
898 struct dmatest_thread *thread;
900 list_for_each_entry(thread, &dtc->threads, node) {
911 __stop_threaded_test(info);
915 mutex_unlock(&info->lock);
918 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
921 static ssize_t dtf_write_run(struct file *file, const char __user *user_buf,
922 size_t count, loff_t *ppos)
924 struct dmatest_info *info = file->private_data;
929 if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1))))
932 if (strtobool(buf, &bv) == 0) {
933 mutex_lock(&info->lock);
934 ret = __restart_threaded_test(info, bv);
935 mutex_unlock(&info->lock);
938 return ret ? ret : count;
941 static const struct file_operations dtf_run_fops = {
942 .read = dtf_read_run,
943 .write = dtf_write_run,
945 .llseek = default_llseek,
948 static int dmatest_register_dbgfs(struct dmatest_info *info)
951 struct dmatest_params *params = &info->dbgfs_params;
954 d = debugfs_create_dir("dmatest", NULL);
962 /* Copy initial values */
963 memcpy(params, &info->params, sizeof(*params));
965 /* Test parameters */
967 d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root,
968 (u32 *)¶ms->buf_size);
969 if (IS_ERR_OR_NULL(d))
972 d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root,
973 info, &dtf_channel_fops);
974 if (IS_ERR_OR_NULL(d))
977 d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root,
978 info, &dtf_device_fops);
979 if (IS_ERR_OR_NULL(d))
982 d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root,
983 (u32 *)¶ms->threads_per_chan);
984 if (IS_ERR_OR_NULL(d))
987 d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root,
988 (u32 *)¶ms->max_channels);
989 if (IS_ERR_OR_NULL(d))
992 d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root,
993 (u32 *)¶ms->iterations);
994 if (IS_ERR_OR_NULL(d))
997 d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root,
998 (u32 *)¶ms->xor_sources);
999 if (IS_ERR_OR_NULL(d))
1002 d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root,
1003 (u32 *)¶ms->pq_sources);
1004 if (IS_ERR_OR_NULL(d))
1007 d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root,
1008 (u32 *)¶ms->timeout);
1009 if (IS_ERR_OR_NULL(d))
1012 /* Run or stop threaded test */
1013 d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root,
1014 info, &dtf_run_fops);
1015 if (IS_ERR_OR_NULL(d))
1021 debugfs_remove_recursive(info->root);
1023 pr_err("dmatest: Failed to initialize debugfs\n");
1027 static int __init dmatest_init(void)
1029 struct dmatest_info *info = &test_info;
1030 struct dmatest_params *params = &info->params;
1033 memset(info, 0, sizeof(*info));
1035 mutex_init(&info->lock);
1036 INIT_LIST_HEAD(&info->channels);
1038 /* Set default parameters */
1039 params->buf_size = test_buf_size;
1040 strlcpy(params->channel, test_channel, sizeof(params->channel));
1041 strlcpy(params->device, test_device, sizeof(params->device));
1042 params->threads_per_chan = threads_per_chan;
1043 params->max_channels = max_channels;
1044 params->iterations = iterations;
1045 params->xor_sources = xor_sources;
1046 params->pq_sources = pq_sources;
1047 params->timeout = timeout;
1049 ret = dmatest_register_dbgfs(info);
1056 return run_threaded_test(info);
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 debugfs_remove_recursive(info->root);
1067 stop_threaded_test(info);
1069 module_exit(dmatest_exit);
1071 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1072 MODULE_LICENSE("GPL v2");