dmatest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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(nr_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 client event 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(dev_name(&chan->dev), 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. dma_chan_get(chan);
  192. while (!kthread_should_stop()) {
  193. total_tests++;
  194. len = dmatest_random() % test_buf_size + 1;
  195. src_off = dmatest_random() % (test_buf_size - len + 1);
  196. dst_off = dmatest_random() % (test_buf_size - len + 1);
  197. dmatest_init_srcbuf(thread->srcbuf, src_off, len);
  198. dmatest_init_dstbuf(thread->dstbuf, dst_off, len);
  199. cookie = dma_async_memcpy_buf_to_buf(chan,
  200. thread->dstbuf + dst_off,
  201. thread->srcbuf + src_off,
  202. len);
  203. if (dma_submit_error(cookie)) {
  204. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  205. "dst_off=0x%x len=0x%x\n",
  206. thread_name, total_tests - 1, cookie,
  207. src_off, dst_off, len);
  208. msleep(100);
  209. failed_tests++;
  210. continue;
  211. }
  212. dma_async_memcpy_issue_pending(chan);
  213. do {
  214. msleep(1);
  215. status = dma_async_memcpy_complete(
  216. chan, cookie, NULL, NULL);
  217. } while (status == DMA_IN_PROGRESS);
  218. if (status == DMA_ERROR) {
  219. pr_warning("%s: #%u: error during copy\n",
  220. thread_name, total_tests - 1);
  221. failed_tests++;
  222. continue;
  223. }
  224. error_count = 0;
  225. pr_debug("%s: verifying source buffer...\n", thread_name);
  226. error_count += dmatest_verify(thread->srcbuf, 0, src_off,
  227. 0, PATTERN_SRC, true);
  228. error_count += dmatest_verify(thread->srcbuf, src_off,
  229. src_off + len, src_off,
  230. PATTERN_SRC | PATTERN_COPY, true);
  231. error_count += dmatest_verify(thread->srcbuf, src_off + len,
  232. test_buf_size, src_off + len,
  233. PATTERN_SRC, true);
  234. pr_debug("%s: verifying dest buffer...\n",
  235. thread->task->comm);
  236. error_count += dmatest_verify(thread->dstbuf, 0, dst_off,
  237. 0, PATTERN_DST, false);
  238. error_count += dmatest_verify(thread->dstbuf, dst_off,
  239. dst_off + len, src_off,
  240. PATTERN_SRC | PATTERN_COPY, false);
  241. error_count += dmatest_verify(thread->dstbuf, dst_off + len,
  242. test_buf_size, dst_off + len,
  243. PATTERN_DST, false);
  244. if (error_count) {
  245. pr_warning("%s: #%u: %u errors with "
  246. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  247. thread_name, total_tests - 1, error_count,
  248. src_off, dst_off, len);
  249. failed_tests++;
  250. } else {
  251. pr_debug("%s: #%u: No errors with "
  252. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  253. thread_name, total_tests - 1,
  254. src_off, dst_off, len);
  255. }
  256. }
  257. ret = 0;
  258. dma_chan_put(chan);
  259. kfree(thread->dstbuf);
  260. err_dstbuf:
  261. kfree(thread->srcbuf);
  262. err_srcbuf:
  263. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  264. thread_name, total_tests, failed_tests, ret);
  265. return ret;
  266. }
  267. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  268. {
  269. struct dmatest_thread *thread;
  270. struct dmatest_thread *_thread;
  271. int ret;
  272. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  273. ret = kthread_stop(thread->task);
  274. pr_debug("dmatest: thread %s exited with status %d\n",
  275. thread->task->comm, ret);
  276. list_del(&thread->node);
  277. kfree(thread);
  278. }
  279. kfree(dtc);
  280. }
  281. static enum dma_state_client dmatest_add_channel(struct dma_chan *chan)
  282. {
  283. struct dmatest_chan *dtc;
  284. struct dmatest_thread *thread;
  285. unsigned int i;
  286. /* Have we already been told about this channel? */
  287. list_for_each_entry(dtc, &dmatest_channels, node)
  288. if (dtc->chan == chan)
  289. return DMA_DUP;
  290. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  291. if (!dtc) {
  292. pr_warning("dmatest: No memory for %s\n", dev_name(&chan->dev));
  293. return DMA_NAK;
  294. }
  295. dtc->chan = chan;
  296. INIT_LIST_HEAD(&dtc->threads);
  297. for (i = 0; i < threads_per_chan; i++) {
  298. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  299. if (!thread) {
  300. pr_warning("dmatest: No memory for %s-test%u\n",
  301. dev_name(&chan->dev), i);
  302. break;
  303. }
  304. thread->chan = dtc->chan;
  305. smp_wmb();
  306. thread->task = kthread_run(dmatest_func, thread, "%s-test%u",
  307. dev_name(&chan->dev), i);
  308. if (IS_ERR(thread->task)) {
  309. pr_warning("dmatest: Failed to run thread %s-test%u\n",
  310. dev_name(&chan->dev), i);
  311. kfree(thread);
  312. break;
  313. }
  314. /* srcbuf and dstbuf are allocated by the thread itself */
  315. list_add_tail(&thread->node, &dtc->threads);
  316. }
  317. pr_info("dmatest: Started %u threads using %s\n", i, dev_name(&chan->dev));
  318. list_add_tail(&dtc->node, &dmatest_channels);
  319. nr_channels++;
  320. return DMA_ACK;
  321. }
  322. static enum dma_state_client dmatest_remove_channel(struct dma_chan *chan)
  323. {
  324. struct dmatest_chan *dtc, *_dtc;
  325. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  326. if (dtc->chan == chan) {
  327. list_del(&dtc->node);
  328. dmatest_cleanup_channel(dtc);
  329. pr_debug("dmatest: lost channel %s\n",
  330. dev_name(&chan->dev));
  331. return DMA_ACK;
  332. }
  333. }
  334. return DMA_DUP;
  335. }
  336. /*
  337. * Start testing threads as new channels are assigned to us, and kill
  338. * them when the channels go away.
  339. *
  340. * When we unregister the client, all channels are removed so this
  341. * will also take care of cleaning things up when the module is
  342. * unloaded.
  343. */
  344. static enum dma_state_client
  345. dmatest_event(struct dma_client *client, struct dma_chan *chan,
  346. enum dma_state state)
  347. {
  348. enum dma_state_client ack = DMA_NAK;
  349. switch (state) {
  350. case DMA_RESOURCE_AVAILABLE:
  351. if (!dmatest_match_channel(chan)
  352. || !dmatest_match_device(chan->device))
  353. ack = DMA_DUP;
  354. else if (max_channels && nr_channels >= max_channels)
  355. ack = DMA_NAK;
  356. else
  357. ack = dmatest_add_channel(chan);
  358. break;
  359. case DMA_RESOURCE_REMOVED:
  360. ack = dmatest_remove_channel(chan);
  361. break;
  362. default:
  363. pr_info("dmatest: Unhandled event %u (%s)\n",
  364. state, dev_name(&chan->dev));
  365. break;
  366. }
  367. return ack;
  368. }
  369. static struct dma_client dmatest_client = {
  370. .event_callback = dmatest_event,
  371. };
  372. static int __init dmatest_init(void)
  373. {
  374. dma_cap_set(DMA_MEMCPY, dmatest_client.cap_mask);
  375. dma_async_client_register(&dmatest_client);
  376. dma_async_client_chan_request(&dmatest_client);
  377. return 0;
  378. }
  379. module_init(dmatest_init);
  380. static void __exit dmatest_exit(void)
  381. {
  382. dma_async_client_unregister(&dmatest_client);
  383. }
  384. module_exit(dmatest_exit);
  385. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  386. MODULE_LICENSE("GPL v2");