dmatest.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. static unsigned int pq_sources = 3;
  40. module_param(pq_sources, uint, S_IRUGO);
  41. MODULE_PARM_DESC(pq_sources,
  42. "Number of p+q 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. u8 pq_coefs[pq_sources];
  209. int ret;
  210. int src_cnt;
  211. int dst_cnt;
  212. int i;
  213. thread_name = current->comm;
  214. ret = -ENOMEM;
  215. smp_rmb();
  216. chan = thread->chan;
  217. if (thread->type == DMA_MEMCPY)
  218. src_cnt = dst_cnt = 1;
  219. else if (thread->type == DMA_XOR) {
  220. src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
  221. dst_cnt = 1;
  222. } else if (thread->type == DMA_PQ) {
  223. src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
  224. dst_cnt = 2;
  225. for (i = 0; i < pq_sources; i++)
  226. pq_coefs[i] = 1;
  227. } else
  228. goto err_srcs;
  229. thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  230. if (!thread->srcs)
  231. goto err_srcs;
  232. for (i = 0; i < src_cnt; i++) {
  233. thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
  234. if (!thread->srcs[i])
  235. goto err_srcbuf;
  236. }
  237. thread->srcs[i] = NULL;
  238. thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  239. if (!thread->dsts)
  240. goto err_dsts;
  241. for (i = 0; i < dst_cnt; i++) {
  242. thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
  243. if (!thread->dsts[i])
  244. goto err_dstbuf;
  245. }
  246. thread->dsts[i] = NULL;
  247. set_user_nice(current, 10);
  248. flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP | DMA_PREP_INTERRUPT;
  249. while (!kthread_should_stop()) {
  250. struct dma_device *dev = chan->device;
  251. struct dma_async_tx_descriptor *tx = NULL;
  252. dma_addr_t dma_srcs[src_cnt];
  253. dma_addr_t dma_dsts[dst_cnt];
  254. struct completion cmp;
  255. unsigned long tmo = msecs_to_jiffies(3000);
  256. u8 align = 0;
  257. total_tests++;
  258. len = dmatest_random() % test_buf_size + 1;
  259. src_off = dmatest_random() % (test_buf_size - len + 1);
  260. dst_off = dmatest_random() % (test_buf_size - len + 1);
  261. /* honor alignment restrictions */
  262. if (thread->type == DMA_MEMCPY)
  263. align = dev->copy_align;
  264. else if (thread->type == DMA_XOR)
  265. align = dev->xor_align;
  266. else if (thread->type == DMA_PQ)
  267. align = dev->pq_align;
  268. len = (len >> align) << align;
  269. src_off = (src_off >> align) << align;
  270. dst_off = (dst_off >> align) << align;
  271. dmatest_init_srcs(thread->srcs, src_off, len);
  272. dmatest_init_dsts(thread->dsts, dst_off, len);
  273. for (i = 0; i < src_cnt; i++) {
  274. u8 *buf = thread->srcs[i] + src_off;
  275. dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  276. DMA_TO_DEVICE);
  277. }
  278. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  279. for (i = 0; i < dst_cnt; i++) {
  280. dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  281. test_buf_size,
  282. DMA_BIDIRECTIONAL);
  283. }
  284. if (thread->type == DMA_MEMCPY)
  285. tx = dev->device_prep_dma_memcpy(chan,
  286. dma_dsts[0] + dst_off,
  287. dma_srcs[0], len,
  288. flags);
  289. else if (thread->type == DMA_XOR)
  290. tx = dev->device_prep_dma_xor(chan,
  291. dma_dsts[0] + dst_off,
  292. dma_srcs, xor_sources,
  293. len, flags);
  294. else if (thread->type == DMA_PQ) {
  295. dma_addr_t dma_pq[dst_cnt];
  296. for (i = 0; i < dst_cnt; i++)
  297. dma_pq[i] = dma_dsts[i] + dst_off;
  298. tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
  299. pq_sources, pq_coefs,
  300. len, flags);
  301. }
  302. if (!tx) {
  303. for (i = 0; i < src_cnt; i++)
  304. dma_unmap_single(dev->dev, dma_srcs[i], len,
  305. DMA_TO_DEVICE);
  306. for (i = 0; i < dst_cnt; i++)
  307. dma_unmap_single(dev->dev, dma_dsts[i],
  308. test_buf_size,
  309. DMA_BIDIRECTIONAL);
  310. pr_warning("%s: #%u: prep error with src_off=0x%x "
  311. "dst_off=0x%x len=0x%x\n",
  312. thread_name, total_tests - 1,
  313. src_off, dst_off, len);
  314. msleep(100);
  315. failed_tests++;
  316. continue;
  317. }
  318. init_completion(&cmp);
  319. tx->callback = dmatest_callback;
  320. tx->callback_param = &cmp;
  321. cookie = tx->tx_submit(tx);
  322. if (dma_submit_error(cookie)) {
  323. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  324. "dst_off=0x%x len=0x%x\n",
  325. thread_name, total_tests - 1, cookie,
  326. src_off, dst_off, len);
  327. msleep(100);
  328. failed_tests++;
  329. continue;
  330. }
  331. dma_async_issue_pending(chan);
  332. tmo = wait_for_completion_timeout(&cmp, tmo);
  333. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  334. if (tmo == 0) {
  335. pr_warning("%s: #%u: test timed out\n",
  336. thread_name, total_tests - 1);
  337. failed_tests++;
  338. continue;
  339. } else if (status != DMA_SUCCESS) {
  340. pr_warning("%s: #%u: got completion callback,"
  341. " but status is \'%s\'\n",
  342. thread_name, total_tests - 1,
  343. status == DMA_ERROR ? "error" : "in progress");
  344. failed_tests++;
  345. continue;
  346. }
  347. /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
  348. for (i = 0; i < dst_cnt; i++)
  349. dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
  350. DMA_BIDIRECTIONAL);
  351. error_count = 0;
  352. pr_debug("%s: verifying source buffer...\n", thread_name);
  353. error_count += dmatest_verify(thread->srcs, 0, src_off,
  354. 0, PATTERN_SRC, true);
  355. error_count += dmatest_verify(thread->srcs, src_off,
  356. src_off + len, src_off,
  357. PATTERN_SRC | PATTERN_COPY, true);
  358. error_count += dmatest_verify(thread->srcs, src_off + len,
  359. test_buf_size, src_off + len,
  360. PATTERN_SRC, true);
  361. pr_debug("%s: verifying dest buffer...\n",
  362. thread->task->comm);
  363. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  364. 0, PATTERN_DST, false);
  365. error_count += dmatest_verify(thread->dsts, dst_off,
  366. dst_off + len, src_off,
  367. PATTERN_SRC | PATTERN_COPY, false);
  368. error_count += dmatest_verify(thread->dsts, dst_off + len,
  369. test_buf_size, dst_off + len,
  370. PATTERN_DST, false);
  371. if (error_count) {
  372. pr_warning("%s: #%u: %u errors with "
  373. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  374. thread_name, total_tests - 1, error_count,
  375. src_off, dst_off, len);
  376. failed_tests++;
  377. } else {
  378. pr_debug("%s: #%u: No errors with "
  379. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  380. thread_name, total_tests - 1,
  381. src_off, dst_off, len);
  382. }
  383. }
  384. ret = 0;
  385. for (i = 0; thread->dsts[i]; i++)
  386. kfree(thread->dsts[i]);
  387. err_dstbuf:
  388. kfree(thread->dsts);
  389. err_dsts:
  390. for (i = 0; thread->srcs[i]; i++)
  391. kfree(thread->srcs[i]);
  392. err_srcbuf:
  393. kfree(thread->srcs);
  394. err_srcs:
  395. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  396. thread_name, total_tests, failed_tests, ret);
  397. return ret;
  398. }
  399. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  400. {
  401. struct dmatest_thread *thread;
  402. struct dmatest_thread *_thread;
  403. int ret;
  404. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  405. ret = kthread_stop(thread->task);
  406. pr_debug("dmatest: thread %s exited with status %d\n",
  407. thread->task->comm, ret);
  408. list_del(&thread->node);
  409. kfree(thread);
  410. }
  411. kfree(dtc);
  412. }
  413. static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
  414. {
  415. struct dmatest_thread *thread;
  416. struct dma_chan *chan = dtc->chan;
  417. char *op;
  418. unsigned int i;
  419. if (type == DMA_MEMCPY)
  420. op = "copy";
  421. else if (type == DMA_XOR)
  422. op = "xor";
  423. else if (type == DMA_PQ)
  424. op = "pq";
  425. else
  426. return -EINVAL;
  427. for (i = 0; i < threads_per_chan; i++) {
  428. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  429. if (!thread) {
  430. pr_warning("dmatest: No memory for %s-%s%u\n",
  431. dma_chan_name(chan), op, i);
  432. break;
  433. }
  434. thread->chan = dtc->chan;
  435. thread->type = type;
  436. smp_wmb();
  437. thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  438. dma_chan_name(chan), op, i);
  439. if (IS_ERR(thread->task)) {
  440. pr_warning("dmatest: Failed to run thread %s-%s%u\n",
  441. dma_chan_name(chan), op, i);
  442. kfree(thread);
  443. break;
  444. }
  445. /* srcbuf and dstbuf are allocated by the thread itself */
  446. list_add_tail(&thread->node, &dtc->threads);
  447. }
  448. return i;
  449. }
  450. static int dmatest_add_channel(struct dma_chan *chan)
  451. {
  452. struct dmatest_chan *dtc;
  453. struct dma_device *dma_dev = chan->device;
  454. unsigned int thread_count = 0;
  455. unsigned int cnt;
  456. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  457. if (!dtc) {
  458. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  459. return -ENOMEM;
  460. }
  461. dtc->chan = chan;
  462. INIT_LIST_HEAD(&dtc->threads);
  463. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  464. cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
  465. thread_count += cnt > 0 ?: 0;
  466. }
  467. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  468. cnt = dmatest_add_threads(dtc, DMA_XOR);
  469. thread_count += cnt > 0 ?: 0;
  470. }
  471. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  472. cnt = dmatest_add_threads(dtc, DMA_PQ);
  473. thread_count += cnt > 0 ?: 0;
  474. }
  475. pr_info("dmatest: Started %u threads using %s\n",
  476. thread_count, dma_chan_name(chan));
  477. list_add_tail(&dtc->node, &dmatest_channels);
  478. nr_channels++;
  479. return 0;
  480. }
  481. static bool filter(struct dma_chan *chan, void *param)
  482. {
  483. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  484. return false;
  485. else
  486. return true;
  487. }
  488. static int __init dmatest_init(void)
  489. {
  490. dma_cap_mask_t mask;
  491. struct dma_chan *chan;
  492. int err = 0;
  493. dma_cap_zero(mask);
  494. dma_cap_set(DMA_MEMCPY, mask);
  495. for (;;) {
  496. chan = dma_request_channel(mask, filter, NULL);
  497. if (chan) {
  498. err = dmatest_add_channel(chan);
  499. if (err) {
  500. dma_release_channel(chan);
  501. break; /* add_channel failed, punt */
  502. }
  503. } else
  504. break; /* no more channels available */
  505. if (max_channels && nr_channels >= max_channels)
  506. break; /* we have all we need */
  507. }
  508. return err;
  509. }
  510. /* when compiled-in wait for drivers to load first */
  511. late_initcall(dmatest_init);
  512. static void __exit dmatest_exit(void)
  513. {
  514. struct dmatest_chan *dtc, *_dtc;
  515. struct dma_chan *chan;
  516. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  517. list_del(&dtc->node);
  518. chan = dtc->chan;
  519. dmatest_cleanup_channel(dtc);
  520. pr_debug("dmatest: dropped channel %s\n",
  521. dma_chan_name(chan));
  522. dma_release_channel(chan);
  523. }
  524. }
  525. module_exit(dmatest_exit);
  526. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  527. MODULE_LICENSE("GPL v2");