dmatest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. struct dma_device *dev = chan->device;
  193. struct dma_async_tx_descriptor *tx;
  194. dma_addr_t dma_src, dma_dest;
  195. total_tests++;
  196. len = dmatest_random() % test_buf_size + 1;
  197. src_off = dmatest_random() % (test_buf_size - len + 1);
  198. dst_off = dmatest_random() % (test_buf_size - len + 1);
  199. dmatest_init_srcbuf(thread->srcbuf, src_off, len);
  200. dmatest_init_dstbuf(thread->dstbuf, dst_off, len);
  201. dma_src = dma_map_single(dev->dev, thread->srcbuf + src_off,
  202. len, DMA_TO_DEVICE);
  203. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  204. dma_dest = dma_map_single(dev->dev, thread->dstbuf,
  205. test_buf_size, DMA_BIDIRECTIONAL);
  206. tx = dev->device_prep_dma_memcpy(chan, dma_dest + dst_off,
  207. dma_src, len,
  208. DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP);
  209. if (!tx) {
  210. dma_unmap_single(dev->dev, dma_src, len, DMA_TO_DEVICE);
  211. dma_unmap_single(dev->dev, dma_dest,
  212. test_buf_size, DMA_BIDIRECTIONAL);
  213. pr_warning("%s: #%u: prep error with src_off=0x%x "
  214. "dst_off=0x%x len=0x%x\n",
  215. thread_name, total_tests - 1,
  216. src_off, dst_off, len);
  217. msleep(100);
  218. failed_tests++;
  219. continue;
  220. }
  221. tx->callback = NULL;
  222. cookie = tx->tx_submit(tx);
  223. if (dma_submit_error(cookie)) {
  224. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  225. "dst_off=0x%x len=0x%x\n",
  226. thread_name, total_tests - 1, cookie,
  227. src_off, dst_off, len);
  228. msleep(100);
  229. failed_tests++;
  230. continue;
  231. }
  232. dma_async_memcpy_issue_pending(chan);
  233. do {
  234. msleep(1);
  235. status = dma_async_memcpy_complete(
  236. chan, cookie, NULL, NULL);
  237. } while (status == DMA_IN_PROGRESS);
  238. if (status == DMA_ERROR) {
  239. pr_warning("%s: #%u: error during copy\n",
  240. thread_name, total_tests - 1);
  241. failed_tests++;
  242. continue;
  243. }
  244. /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
  245. dma_unmap_single(dev->dev, dma_dest,
  246. test_buf_size, DMA_BIDIRECTIONAL);
  247. error_count = 0;
  248. pr_debug("%s: verifying source buffer...\n", thread_name);
  249. error_count += dmatest_verify(thread->srcbuf, 0, src_off,
  250. 0, PATTERN_SRC, true);
  251. error_count += dmatest_verify(thread->srcbuf, src_off,
  252. src_off + len, src_off,
  253. PATTERN_SRC | PATTERN_COPY, true);
  254. error_count += dmatest_verify(thread->srcbuf, src_off + len,
  255. test_buf_size, src_off + len,
  256. PATTERN_SRC, true);
  257. pr_debug("%s: verifying dest buffer...\n",
  258. thread->task->comm);
  259. error_count += dmatest_verify(thread->dstbuf, 0, dst_off,
  260. 0, PATTERN_DST, false);
  261. error_count += dmatest_verify(thread->dstbuf, dst_off,
  262. dst_off + len, src_off,
  263. PATTERN_SRC | PATTERN_COPY, false);
  264. error_count += dmatest_verify(thread->dstbuf, dst_off + len,
  265. test_buf_size, dst_off + len,
  266. PATTERN_DST, false);
  267. if (error_count) {
  268. pr_warning("%s: #%u: %u errors with "
  269. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  270. thread_name, total_tests - 1, error_count,
  271. src_off, dst_off, len);
  272. failed_tests++;
  273. } else {
  274. pr_debug("%s: #%u: No errors with "
  275. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  276. thread_name, total_tests - 1,
  277. src_off, dst_off, len);
  278. }
  279. }
  280. ret = 0;
  281. kfree(thread->dstbuf);
  282. err_dstbuf:
  283. kfree(thread->srcbuf);
  284. err_srcbuf:
  285. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  286. thread_name, total_tests, failed_tests, ret);
  287. return ret;
  288. }
  289. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  290. {
  291. struct dmatest_thread *thread;
  292. struct dmatest_thread *_thread;
  293. int ret;
  294. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  295. ret = kthread_stop(thread->task);
  296. pr_debug("dmatest: thread %s exited with status %d\n",
  297. thread->task->comm, ret);
  298. list_del(&thread->node);
  299. kfree(thread);
  300. }
  301. kfree(dtc);
  302. }
  303. static int dmatest_add_channel(struct dma_chan *chan)
  304. {
  305. struct dmatest_chan *dtc;
  306. struct dmatest_thread *thread;
  307. unsigned int i;
  308. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  309. if (!dtc) {
  310. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  311. return -ENOMEM;
  312. }
  313. dtc->chan = chan;
  314. INIT_LIST_HEAD(&dtc->threads);
  315. for (i = 0; i < threads_per_chan; i++) {
  316. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  317. if (!thread) {
  318. pr_warning("dmatest: No memory for %s-test%u\n",
  319. dma_chan_name(chan), i);
  320. break;
  321. }
  322. thread->chan = dtc->chan;
  323. smp_wmb();
  324. thread->task = kthread_run(dmatest_func, thread, "%s-test%u",
  325. dma_chan_name(chan), i);
  326. if (IS_ERR(thread->task)) {
  327. pr_warning("dmatest: Failed to run thread %s-test%u\n",
  328. dma_chan_name(chan), i);
  329. kfree(thread);
  330. break;
  331. }
  332. /* srcbuf and dstbuf are allocated by the thread itself */
  333. list_add_tail(&thread->node, &dtc->threads);
  334. }
  335. pr_info("dmatest: Started %u threads using %s\n", i, dma_chan_name(chan));
  336. list_add_tail(&dtc->node, &dmatest_channels);
  337. nr_channels++;
  338. return 0;
  339. }
  340. static bool filter(struct dma_chan *chan, void *param)
  341. {
  342. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  343. return false;
  344. else
  345. return true;
  346. }
  347. static int __init dmatest_init(void)
  348. {
  349. dma_cap_mask_t mask;
  350. struct dma_chan *chan;
  351. int err = 0;
  352. dma_cap_zero(mask);
  353. dma_cap_set(DMA_MEMCPY, mask);
  354. for (;;) {
  355. chan = dma_request_channel(mask, filter, NULL);
  356. if (chan) {
  357. err = dmatest_add_channel(chan);
  358. if (err == 0)
  359. continue;
  360. else {
  361. dma_release_channel(chan);
  362. break; /* add_channel failed, punt */
  363. }
  364. } else
  365. break; /* no more channels available */
  366. if (max_channels && nr_channels >= max_channels)
  367. break; /* we have all we need */
  368. }
  369. return err;
  370. }
  371. /* when compiled-in wait for drivers to load first */
  372. late_initcall(dmatest_init);
  373. static void __exit dmatest_exit(void)
  374. {
  375. struct dmatest_chan *dtc, *_dtc;
  376. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  377. list_del(&dtc->node);
  378. dmatest_cleanup_channel(dtc);
  379. pr_debug("dmatest: dropped channel %s\n",
  380. dma_chan_name(dtc->chan));
  381. dma_release_channel(dtc->chan);
  382. }
  383. }
  384. module_exit(dmatest_exit);
  385. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  386. MODULE_LICENSE("GPL v2");