dmatest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. static unsigned int xor_sources = 3;
  36. module_param(xor_sources, uint, S_IRUGO);
  37. MODULE_PARM_DESC(xor_sources,
  38. "Number of xor source buffers (default: 3)");
  39. /*
  40. * Initialization patterns. All bytes in the source buffer has bit 7
  41. * set, all bytes in the destination buffer has bit 7 cleared.
  42. *
  43. * Bit 6 is set for all bytes which are to be copied by the DMA
  44. * engine. Bit 5 is set for all bytes which are to be overwritten by
  45. * the DMA engine.
  46. *
  47. * The remaining bits are the inverse of a counter which increments by
  48. * one for each byte address.
  49. */
  50. #define PATTERN_SRC 0x80
  51. #define PATTERN_DST 0x00
  52. #define PATTERN_COPY 0x40
  53. #define PATTERN_OVERWRITE 0x20
  54. #define PATTERN_COUNT_MASK 0x1f
  55. struct dmatest_thread {
  56. struct list_head node;
  57. struct task_struct *task;
  58. struct dma_chan *chan;
  59. u8 **srcs;
  60. u8 **dsts;
  61. enum dma_transaction_type type;
  62. };
  63. struct dmatest_chan {
  64. struct list_head node;
  65. struct dma_chan *chan;
  66. struct list_head threads;
  67. };
  68. /*
  69. * These are protected by dma_list_mutex since they're only used by
  70. * the DMA filter function callback
  71. */
  72. static LIST_HEAD(dmatest_channels);
  73. static unsigned int nr_channels;
  74. static bool dmatest_match_channel(struct dma_chan *chan)
  75. {
  76. if (test_channel[0] == '\0')
  77. return true;
  78. return strcmp(dma_chan_name(chan), test_channel) == 0;
  79. }
  80. static bool dmatest_match_device(struct dma_device *device)
  81. {
  82. if (test_device[0] == '\0')
  83. return true;
  84. return strcmp(dev_name(device->dev), test_device) == 0;
  85. }
  86. static unsigned long dmatest_random(void)
  87. {
  88. unsigned long buf;
  89. get_random_bytes(&buf, sizeof(buf));
  90. return buf;
  91. }
  92. static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
  93. {
  94. unsigned int i;
  95. u8 *buf;
  96. for (; (buf = *bufs); bufs++) {
  97. for (i = 0; i < start; i++)
  98. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  99. for ( ; i < start + len; i++)
  100. buf[i] = PATTERN_SRC | PATTERN_COPY
  101. | (~i & PATTERN_COUNT_MASK);;
  102. for ( ; i < test_buf_size; i++)
  103. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  104. buf++;
  105. }
  106. }
  107. static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
  108. {
  109. unsigned int i;
  110. u8 *buf;
  111. for (; (buf = *bufs); bufs++) {
  112. for (i = 0; i < start; i++)
  113. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  114. for ( ; i < start + len; i++)
  115. buf[i] = PATTERN_DST | PATTERN_OVERWRITE
  116. | (~i & PATTERN_COUNT_MASK);
  117. for ( ; i < test_buf_size; i++)
  118. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  119. }
  120. }
  121. static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
  122. unsigned int counter, bool is_srcbuf)
  123. {
  124. u8 diff = actual ^ pattern;
  125. u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
  126. const char *thread_name = current->comm;
  127. if (is_srcbuf)
  128. pr_warning("%s: srcbuf[0x%x] overwritten!"
  129. " Expected %02x, got %02x\n",
  130. thread_name, index, expected, actual);
  131. else if ((pattern & PATTERN_COPY)
  132. && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
  133. pr_warning("%s: dstbuf[0x%x] not copied!"
  134. " Expected %02x, got %02x\n",
  135. thread_name, index, expected, actual);
  136. else if (diff & PATTERN_SRC)
  137. pr_warning("%s: dstbuf[0x%x] was copied!"
  138. " Expected %02x, got %02x\n",
  139. thread_name, index, expected, actual);
  140. else
  141. pr_warning("%s: dstbuf[0x%x] mismatch!"
  142. " Expected %02x, got %02x\n",
  143. thread_name, index, expected, actual);
  144. }
  145. static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
  146. unsigned int end, unsigned int counter, u8 pattern,
  147. bool is_srcbuf)
  148. {
  149. unsigned int i;
  150. unsigned int error_count = 0;
  151. u8 actual;
  152. u8 expected;
  153. u8 *buf;
  154. unsigned int counter_orig = counter;
  155. for (; (buf = *bufs); bufs++) {
  156. counter = counter_orig;
  157. for (i = start; i < end; i++) {
  158. actual = buf[i];
  159. expected = pattern | (~counter & PATTERN_COUNT_MASK);
  160. if (actual != expected) {
  161. if (error_count < 32)
  162. dmatest_mismatch(actual, pattern, i,
  163. counter, is_srcbuf);
  164. error_count++;
  165. }
  166. counter++;
  167. }
  168. }
  169. if (error_count > 32)
  170. pr_warning("%s: %u errors suppressed\n",
  171. current->comm, error_count - 32);
  172. return error_count;
  173. }
  174. static void dmatest_callback(void *completion)
  175. {
  176. complete(completion);
  177. }
  178. /*
  179. * This function repeatedly tests DMA transfers of various lengths and
  180. * offsets for a given operation type until it is told to exit by
  181. * kthread_stop(). There may be multiple threads running this function
  182. * in parallel for a single channel, and there may be multiple channels
  183. * being tested in parallel.
  184. *
  185. * Before each test, the source and destination buffer is initialized
  186. * with a known pattern. This pattern is different depending on
  187. * whether it's in an area which is supposed to be copied or
  188. * overwritten, and different in the source and destination buffers.
  189. * So if the DMA engine doesn't copy exactly what we tell it to copy,
  190. * we'll notice.
  191. */
  192. static int dmatest_func(void *data)
  193. {
  194. struct dmatest_thread *thread = data;
  195. struct dma_chan *chan;
  196. const char *thread_name;
  197. unsigned int src_off, dst_off, len;
  198. unsigned int error_count;
  199. unsigned int failed_tests = 0;
  200. unsigned int total_tests = 0;
  201. dma_cookie_t cookie;
  202. enum dma_status status;
  203. enum dma_ctrl_flags flags;
  204. int ret;
  205. int src_cnt;
  206. int dst_cnt;
  207. int i;
  208. thread_name = current->comm;
  209. ret = -ENOMEM;
  210. smp_rmb();
  211. chan = thread->chan;
  212. if (thread->type == DMA_MEMCPY)
  213. src_cnt = dst_cnt = 1;
  214. else if (thread->type == DMA_XOR) {
  215. src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
  216. dst_cnt = 1;
  217. } else
  218. goto err_srcs;
  219. thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  220. if (!thread->srcs)
  221. goto err_srcs;
  222. for (i = 0; i < src_cnt; i++) {
  223. thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
  224. if (!thread->srcs[i])
  225. goto err_srcbuf;
  226. }
  227. thread->srcs[i] = NULL;
  228. thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  229. if (!thread->dsts)
  230. goto err_dsts;
  231. for (i = 0; i < dst_cnt; i++) {
  232. thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
  233. if (!thread->dsts[i])
  234. goto err_dstbuf;
  235. }
  236. thread->dsts[i] = NULL;
  237. set_user_nice(current, 10);
  238. flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP | DMA_PREP_INTERRUPT;
  239. while (!kthread_should_stop()) {
  240. struct dma_device *dev = chan->device;
  241. struct dma_async_tx_descriptor *tx = NULL;
  242. dma_addr_t dma_srcs[src_cnt];
  243. dma_addr_t dma_dsts[dst_cnt];
  244. struct completion cmp;
  245. unsigned long tmo = msecs_to_jiffies(3000);
  246. total_tests++;
  247. len = dmatest_random() % test_buf_size + 1;
  248. src_off = dmatest_random() % (test_buf_size - len + 1);
  249. dst_off = dmatest_random() % (test_buf_size - len + 1);
  250. dmatest_init_srcs(thread->srcs, src_off, len);
  251. dmatest_init_dsts(thread->dsts, dst_off, len);
  252. for (i = 0; i < src_cnt; i++) {
  253. u8 *buf = thread->srcs[i] + src_off;
  254. dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  255. DMA_TO_DEVICE);
  256. }
  257. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  258. for (i = 0; i < dst_cnt; i++) {
  259. dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  260. test_buf_size,
  261. DMA_BIDIRECTIONAL);
  262. }
  263. if (thread->type == DMA_MEMCPY)
  264. tx = dev->device_prep_dma_memcpy(chan,
  265. dma_dsts[0] + dst_off,
  266. dma_srcs[0], len,
  267. flags);
  268. else if (thread->type == DMA_XOR)
  269. tx = dev->device_prep_dma_xor(chan,
  270. dma_dsts[0] + dst_off,
  271. dma_srcs, xor_sources,
  272. len, flags);
  273. if (!tx) {
  274. for (i = 0; i < src_cnt; i++)
  275. dma_unmap_single(dev->dev, dma_srcs[i], len,
  276. DMA_TO_DEVICE);
  277. for (i = 0; i < dst_cnt; i++)
  278. dma_unmap_single(dev->dev, dma_dsts[i],
  279. test_buf_size,
  280. DMA_BIDIRECTIONAL);
  281. pr_warning("%s: #%u: prep error with src_off=0x%x "
  282. "dst_off=0x%x len=0x%x\n",
  283. thread_name, total_tests - 1,
  284. src_off, dst_off, len);
  285. msleep(100);
  286. failed_tests++;
  287. continue;
  288. }
  289. init_completion(&cmp);
  290. tx->callback = dmatest_callback;
  291. tx->callback_param = &cmp;
  292. cookie = tx->tx_submit(tx);
  293. if (dma_submit_error(cookie)) {
  294. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  295. "dst_off=0x%x len=0x%x\n",
  296. thread_name, total_tests - 1, cookie,
  297. src_off, dst_off, len);
  298. msleep(100);
  299. failed_tests++;
  300. continue;
  301. }
  302. dma_async_issue_pending(chan);
  303. tmo = wait_for_completion_timeout(&cmp, tmo);
  304. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  305. if (tmo == 0) {
  306. pr_warning("%s: #%u: test timed out\n",
  307. thread_name, total_tests - 1);
  308. failed_tests++;
  309. continue;
  310. } else if (status != DMA_SUCCESS) {
  311. pr_warning("%s: #%u: got completion callback,"
  312. " but status is \'%s\'\n",
  313. thread_name, total_tests - 1,
  314. status == DMA_ERROR ? "error" : "in progress");
  315. failed_tests++;
  316. continue;
  317. }
  318. /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
  319. for (i = 0; i < dst_cnt; i++)
  320. dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
  321. DMA_BIDIRECTIONAL);
  322. error_count = 0;
  323. pr_debug("%s: verifying source buffer...\n", thread_name);
  324. error_count += dmatest_verify(thread->srcs, 0, src_off,
  325. 0, PATTERN_SRC, true);
  326. error_count += dmatest_verify(thread->srcs, src_off,
  327. src_off + len, src_off,
  328. PATTERN_SRC | PATTERN_COPY, true);
  329. error_count += dmatest_verify(thread->srcs, src_off + len,
  330. test_buf_size, src_off + len,
  331. PATTERN_SRC, true);
  332. pr_debug("%s: verifying dest buffer...\n",
  333. thread->task->comm);
  334. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  335. 0, PATTERN_DST, false);
  336. error_count += dmatest_verify(thread->dsts, dst_off,
  337. dst_off + len, src_off,
  338. PATTERN_SRC | PATTERN_COPY, false);
  339. error_count += dmatest_verify(thread->dsts, dst_off + len,
  340. test_buf_size, dst_off + len,
  341. PATTERN_DST, false);
  342. if (error_count) {
  343. pr_warning("%s: #%u: %u errors with "
  344. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  345. thread_name, total_tests - 1, error_count,
  346. src_off, dst_off, len);
  347. failed_tests++;
  348. } else {
  349. pr_debug("%s: #%u: No errors with "
  350. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  351. thread_name, total_tests - 1,
  352. src_off, dst_off, len);
  353. }
  354. }
  355. ret = 0;
  356. for (i = 0; thread->dsts[i]; i++)
  357. kfree(thread->dsts[i]);
  358. err_dstbuf:
  359. kfree(thread->dsts);
  360. err_dsts:
  361. for (i = 0; thread->srcs[i]; i++)
  362. kfree(thread->srcs[i]);
  363. err_srcbuf:
  364. kfree(thread->srcs);
  365. err_srcs:
  366. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  367. thread_name, total_tests, failed_tests, ret);
  368. return ret;
  369. }
  370. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  371. {
  372. struct dmatest_thread *thread;
  373. struct dmatest_thread *_thread;
  374. int ret;
  375. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  376. ret = kthread_stop(thread->task);
  377. pr_debug("dmatest: thread %s exited with status %d\n",
  378. thread->task->comm, ret);
  379. list_del(&thread->node);
  380. kfree(thread);
  381. }
  382. kfree(dtc);
  383. }
  384. static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
  385. {
  386. struct dmatest_thread *thread;
  387. struct dma_chan *chan = dtc->chan;
  388. char *op;
  389. unsigned int i;
  390. if (type == DMA_MEMCPY)
  391. op = "copy";
  392. else if (type == DMA_XOR)
  393. op = "xor";
  394. else
  395. return -EINVAL;
  396. for (i = 0; i < threads_per_chan; i++) {
  397. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  398. if (!thread) {
  399. pr_warning("dmatest: No memory for %s-%s%u\n",
  400. dma_chan_name(chan), op, i);
  401. break;
  402. }
  403. thread->chan = dtc->chan;
  404. thread->type = type;
  405. smp_wmb();
  406. thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  407. dma_chan_name(chan), op, i);
  408. if (IS_ERR(thread->task)) {
  409. pr_warning("dmatest: Failed to run thread %s-%s%u\n",
  410. dma_chan_name(chan), op, i);
  411. kfree(thread);
  412. break;
  413. }
  414. /* srcbuf and dstbuf are allocated by the thread itself */
  415. list_add_tail(&thread->node, &dtc->threads);
  416. }
  417. return i;
  418. }
  419. static int dmatest_add_channel(struct dma_chan *chan)
  420. {
  421. struct dmatest_chan *dtc;
  422. struct dma_device *dma_dev = chan->device;
  423. unsigned int thread_count = 0;
  424. unsigned int cnt;
  425. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  426. if (!dtc) {
  427. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  428. return -ENOMEM;
  429. }
  430. dtc->chan = chan;
  431. INIT_LIST_HEAD(&dtc->threads);
  432. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  433. cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
  434. thread_count += cnt > 0 ?: 0;
  435. }
  436. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  437. cnt = dmatest_add_threads(dtc, DMA_XOR);
  438. thread_count += cnt > 0 ?: 0;
  439. }
  440. pr_info("dmatest: Started %u threads using %s\n",
  441. thread_count, dma_chan_name(chan));
  442. list_add_tail(&dtc->node, &dmatest_channels);
  443. nr_channels++;
  444. return 0;
  445. }
  446. static bool filter(struct dma_chan *chan, void *param)
  447. {
  448. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  449. return false;
  450. else
  451. return true;
  452. }
  453. static int __init dmatest_init(void)
  454. {
  455. dma_cap_mask_t mask;
  456. struct dma_chan *chan;
  457. int err = 0;
  458. dma_cap_zero(mask);
  459. dma_cap_set(DMA_MEMCPY, mask);
  460. for (;;) {
  461. chan = dma_request_channel(mask, filter, NULL);
  462. if (chan) {
  463. err = dmatest_add_channel(chan);
  464. if (err) {
  465. dma_release_channel(chan);
  466. break; /* add_channel failed, punt */
  467. }
  468. } else
  469. break; /* no more channels available */
  470. if (max_channels && nr_channels >= max_channels)
  471. break; /* we have all we need */
  472. }
  473. return err;
  474. }
  475. /* when compiled-in wait for drivers to load first */
  476. late_initcall(dmatest_init);
  477. static void __exit dmatest_exit(void)
  478. {
  479. struct dmatest_chan *dtc, *_dtc;
  480. struct dma_chan *chan;
  481. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  482. list_del(&dtc->node);
  483. chan = dtc->chan;
  484. dmatest_cleanup_channel(dtc);
  485. pr_debug("dmatest: dropped channel %s\n",
  486. dma_chan_name(chan));
  487. dma_release_channel(chan);
  488. }
  489. }
  490. module_exit(dmatest_exit);
  491. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  492. MODULE_LICENSE("GPL v2");