dmatest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * DMA Engine test module
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/init.h>
  13. #include <linux/kthread.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/random.h>
  17. #include <linux/wait.h>
  18. static unsigned int test_buf_size = 16384;
  19. module_param(test_buf_size, uint, S_IRUGO);
  20. MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
  21. static char test_channel[20];
  22. module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
  23. MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
  24. static char test_device[20];
  25. module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
  26. MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
  27. static unsigned int threads_per_chan = 1;
  28. module_param(threads_per_chan, uint, S_IRUGO);
  29. MODULE_PARM_DESC(threads_per_chan,
  30. "Number of threads to start per channel (default: 1)");
  31. static unsigned int max_channels;
  32. module_param(max_channels, uint, S_IRUGO);
  33. MODULE_PARM_DESC(max_channels,
  34. "Maximum number of channels to use (default: all)");
  35. /*
  36. * Initialization patterns. All bytes in the source buffer has bit 7
  37. * set, all bytes in the destination buffer has bit 7 cleared.
  38. *
  39. * Bit 6 is set for all bytes which are to be copied by the DMA
  40. * engine. Bit 5 is set for all bytes which are to be overwritten by
  41. * the DMA engine.
  42. *
  43. * The remaining bits are the inverse of a counter which increments by
  44. * one for each byte address.
  45. */
  46. #define PATTERN_SRC 0x80
  47. #define PATTERN_DST 0x00
  48. #define PATTERN_COPY 0x40
  49. #define PATTERN_OVERWRITE 0x20
  50. #define PATTERN_COUNT_MASK 0x1f
  51. struct dmatest_thread {
  52. struct list_head node;
  53. struct task_struct *task;
  54. struct dma_chan *chan;
  55. u8 *srcbuf;
  56. u8 *dstbuf;
  57. };
  58. struct dmatest_chan {
  59. struct list_head node;
  60. struct dma_chan *chan;
  61. struct list_head threads;
  62. };
  63. /*
  64. * These are protected by dma_list_mutex since they're only used by
  65. * the DMA filter function callback
  66. */
  67. static LIST_HEAD(dmatest_channels);
  68. static unsigned int nr_channels;
  69. static bool dmatest_match_channel(struct dma_chan *chan)
  70. {
  71. if (test_channel[0] == '\0')
  72. return true;
  73. return strcmp(dma_chan_name(chan), test_channel) == 0;
  74. }
  75. static bool dmatest_match_device(struct dma_device *device)
  76. {
  77. if (test_device[0] == '\0')
  78. return true;
  79. return strcmp(dev_name(device->dev), test_device) == 0;
  80. }
  81. static unsigned long dmatest_random(void)
  82. {
  83. unsigned long buf;
  84. get_random_bytes(&buf, sizeof(buf));
  85. return buf;
  86. }
  87. static void dmatest_init_srcbuf(u8 *buf, unsigned int start, unsigned int len)
  88. {
  89. unsigned int i;
  90. for (i = 0; i < start; i++)
  91. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  92. for ( ; i < start + len; i++)
  93. buf[i] = PATTERN_SRC | PATTERN_COPY
  94. | (~i & PATTERN_COUNT_MASK);;
  95. for ( ; i < test_buf_size; i++)
  96. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  97. }
  98. static void dmatest_init_dstbuf(u8 *buf, unsigned int start, unsigned int len)
  99. {
  100. unsigned int i;
  101. for (i = 0; i < start; i++)
  102. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  103. for ( ; i < start + len; i++)
  104. buf[i] = PATTERN_DST | PATTERN_OVERWRITE
  105. | (~i & PATTERN_COUNT_MASK);
  106. for ( ; i < test_buf_size; i++)
  107. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  108. }
  109. static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
  110. unsigned int counter, bool is_srcbuf)
  111. {
  112. u8 diff = actual ^ pattern;
  113. u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
  114. const char *thread_name = current->comm;
  115. if (is_srcbuf)
  116. pr_warning("%s: srcbuf[0x%x] overwritten!"
  117. " Expected %02x, got %02x\n",
  118. thread_name, index, expected, actual);
  119. else if ((pattern & PATTERN_COPY)
  120. && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
  121. pr_warning("%s: dstbuf[0x%x] not copied!"
  122. " Expected %02x, got %02x\n",
  123. thread_name, index, expected, actual);
  124. else if (diff & PATTERN_SRC)
  125. pr_warning("%s: dstbuf[0x%x] was copied!"
  126. " Expected %02x, got %02x\n",
  127. thread_name, index, expected, actual);
  128. else
  129. pr_warning("%s: dstbuf[0x%x] mismatch!"
  130. " Expected %02x, got %02x\n",
  131. thread_name, index, expected, actual);
  132. }
  133. static unsigned int dmatest_verify(u8 *buf, unsigned int start,
  134. unsigned int end, unsigned int counter, u8 pattern,
  135. bool is_srcbuf)
  136. {
  137. unsigned int i;
  138. unsigned int error_count = 0;
  139. u8 actual;
  140. for (i = start; i < end; i++) {
  141. actual = buf[i];
  142. if (actual != (pattern | (~counter & PATTERN_COUNT_MASK))) {
  143. if (error_count < 32)
  144. dmatest_mismatch(actual, pattern, i, counter,
  145. is_srcbuf);
  146. error_count++;
  147. }
  148. counter++;
  149. }
  150. if (error_count > 32)
  151. pr_warning("%s: %u errors suppressed\n",
  152. current->comm, error_count - 32);
  153. return error_count;
  154. }
  155. /*
  156. * This function repeatedly tests DMA transfers of various lengths and
  157. * offsets until it is told to exit by kthread_stop(). There may be
  158. * multiple threads running this function in parallel for a single
  159. * channel, and there may be multiple channels being tested in
  160. * parallel.
  161. *
  162. * Before each test, the source and destination buffer is initialized
  163. * with a known pattern. This pattern is different depending on
  164. * whether it's in an area which is supposed to be copied or
  165. * overwritten, and different in the source and destination buffers.
  166. * So if the DMA engine doesn't copy exactly what we tell it to copy,
  167. * we'll notice.
  168. */
  169. static int dmatest_func(void *data)
  170. {
  171. struct dmatest_thread *thread = data;
  172. struct dma_chan *chan;
  173. const char *thread_name;
  174. unsigned int src_off, dst_off, len;
  175. unsigned int error_count;
  176. unsigned int failed_tests = 0;
  177. unsigned int total_tests = 0;
  178. dma_cookie_t cookie;
  179. enum dma_status status;
  180. int ret;
  181. thread_name = current->comm;
  182. ret = -ENOMEM;
  183. thread->srcbuf = kmalloc(test_buf_size, GFP_KERNEL);
  184. if (!thread->srcbuf)
  185. goto err_srcbuf;
  186. thread->dstbuf = kmalloc(test_buf_size, GFP_KERNEL);
  187. if (!thread->dstbuf)
  188. goto err_dstbuf;
  189. smp_rmb();
  190. chan = thread->chan;
  191. while (!kthread_should_stop()) {
  192. total_tests++;
  193. len = dmatest_random() % test_buf_size + 1;
  194. src_off = dmatest_random() % (test_buf_size - len + 1);
  195. dst_off = dmatest_random() % (test_buf_size - len + 1);
  196. dmatest_init_srcbuf(thread->srcbuf, src_off, len);
  197. dmatest_init_dstbuf(thread->dstbuf, dst_off, len);
  198. cookie = dma_async_memcpy_buf_to_buf(chan,
  199. thread->dstbuf + dst_off,
  200. thread->srcbuf + src_off,
  201. len);
  202. if (dma_submit_error(cookie)) {
  203. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  204. "dst_off=0x%x len=0x%x\n",
  205. thread_name, total_tests - 1, cookie,
  206. src_off, dst_off, len);
  207. msleep(100);
  208. failed_tests++;
  209. continue;
  210. }
  211. dma_async_memcpy_issue_pending(chan);
  212. do {
  213. msleep(1);
  214. status = dma_async_memcpy_complete(
  215. chan, cookie, NULL, NULL);
  216. } while (status == DMA_IN_PROGRESS);
  217. if (status == DMA_ERROR) {
  218. pr_warning("%s: #%u: error during copy\n",
  219. thread_name, total_tests - 1);
  220. failed_tests++;
  221. continue;
  222. }
  223. error_count = 0;
  224. pr_debug("%s: verifying source buffer...\n", thread_name);
  225. error_count += dmatest_verify(thread->srcbuf, 0, src_off,
  226. 0, PATTERN_SRC, true);
  227. error_count += dmatest_verify(thread->srcbuf, src_off,
  228. src_off + len, src_off,
  229. PATTERN_SRC | PATTERN_COPY, true);
  230. error_count += dmatest_verify(thread->srcbuf, src_off + len,
  231. test_buf_size, src_off + len,
  232. PATTERN_SRC, true);
  233. pr_debug("%s: verifying dest buffer...\n",
  234. thread->task->comm);
  235. error_count += dmatest_verify(thread->dstbuf, 0, dst_off,
  236. 0, PATTERN_DST, false);
  237. error_count += dmatest_verify(thread->dstbuf, dst_off,
  238. dst_off + len, src_off,
  239. PATTERN_SRC | PATTERN_COPY, false);
  240. error_count += dmatest_verify(thread->dstbuf, dst_off + len,
  241. test_buf_size, dst_off + len,
  242. PATTERN_DST, false);
  243. if (error_count) {
  244. pr_warning("%s: #%u: %u errors with "
  245. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  246. thread_name, total_tests - 1, error_count,
  247. src_off, dst_off, len);
  248. failed_tests++;
  249. } else {
  250. pr_debug("%s: #%u: No errors with "
  251. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  252. thread_name, total_tests - 1,
  253. src_off, dst_off, len);
  254. }
  255. }
  256. ret = 0;
  257. kfree(thread->dstbuf);
  258. err_dstbuf:
  259. kfree(thread->srcbuf);
  260. err_srcbuf:
  261. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  262. thread_name, total_tests, failed_tests, ret);
  263. return ret;
  264. }
  265. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  266. {
  267. struct dmatest_thread *thread;
  268. struct dmatest_thread *_thread;
  269. int ret;
  270. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  271. ret = kthread_stop(thread->task);
  272. pr_debug("dmatest: thread %s exited with status %d\n",
  273. thread->task->comm, ret);
  274. list_del(&thread->node);
  275. kfree(thread);
  276. }
  277. kfree(dtc);
  278. }
  279. static int dmatest_add_channel(struct dma_chan *chan)
  280. {
  281. struct dmatest_chan *dtc;
  282. struct dmatest_thread *thread;
  283. unsigned int i;
  284. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  285. if (!dtc) {
  286. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  287. return -ENOMEM;
  288. }
  289. dtc->chan = chan;
  290. INIT_LIST_HEAD(&dtc->threads);
  291. for (i = 0; i < threads_per_chan; i++) {
  292. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  293. if (!thread) {
  294. pr_warning("dmatest: No memory for %s-test%u\n",
  295. dma_chan_name(chan), i);
  296. break;
  297. }
  298. thread->chan = dtc->chan;
  299. smp_wmb();
  300. thread->task = kthread_run(dmatest_func, thread, "%s-test%u",
  301. dma_chan_name(chan), i);
  302. if (IS_ERR(thread->task)) {
  303. pr_warning("dmatest: Failed to run thread %s-test%u\n",
  304. dma_chan_name(chan), i);
  305. kfree(thread);
  306. break;
  307. }
  308. /* srcbuf and dstbuf are allocated by the thread itself */
  309. list_add_tail(&thread->node, &dtc->threads);
  310. }
  311. pr_info("dmatest: Started %u threads using %s\n", i, dma_chan_name(chan));
  312. list_add_tail(&dtc->node, &dmatest_channels);
  313. nr_channels++;
  314. return 0;
  315. }
  316. static bool filter(struct dma_chan *chan, void *param)
  317. {
  318. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  319. return false;
  320. else
  321. return true;
  322. }
  323. static int __init dmatest_init(void)
  324. {
  325. dma_cap_mask_t mask;
  326. struct dma_chan *chan;
  327. int err = 0;
  328. dma_cap_zero(mask);
  329. dma_cap_set(DMA_MEMCPY, mask);
  330. for (;;) {
  331. chan = dma_request_channel(mask, filter, NULL);
  332. if (chan) {
  333. err = dmatest_add_channel(chan);
  334. if (err == 0)
  335. continue;
  336. else {
  337. dma_release_channel(chan);
  338. break; /* add_channel failed, punt */
  339. }
  340. } else
  341. break; /* no more channels available */
  342. if (max_channels && nr_channels >= max_channels)
  343. break; /* we have all we need */
  344. }
  345. return err;
  346. }
  347. /* when compiled-in wait for drivers to load first */
  348. late_initcall(dmatest_init);
  349. static void __exit dmatest_exit(void)
  350. {
  351. struct dmatest_chan *dtc, *_dtc;
  352. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  353. list_del(&dtc->node);
  354. dmatest_cleanup_channel(dtc);
  355. pr_debug("dmatest: dropped channel %s\n",
  356. dma_chan_name(dtc->chan));
  357. dma_release_channel(dtc->chan);
  358. }
  359. }
  360. module_exit(dmatest_exit);
  361. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  362. MODULE_LICENSE("GPL v2");