dmatest.c 15 KB

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