dmatest.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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/dma-mapping.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/freezer.h>
  14. #include <linux/init.h>
  15. #include <linux/kthread.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/random.h>
  19. #include <linux/slab.h>
  20. #include <linux/wait.h>
  21. static unsigned int test_buf_size = 16384;
  22. module_param(test_buf_size, uint, S_IRUGO);
  23. MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
  24. static char test_channel[20];
  25. module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
  26. MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
  27. static char test_device[20];
  28. module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
  29. MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
  30. static unsigned int threads_per_chan = 1;
  31. module_param(threads_per_chan, uint, S_IRUGO);
  32. MODULE_PARM_DESC(threads_per_chan,
  33. "Number of threads to start per channel (default: 1)");
  34. static unsigned int max_channels;
  35. module_param(max_channels, uint, S_IRUGO);
  36. MODULE_PARM_DESC(max_channels,
  37. "Maximum number of channels to use (default: all)");
  38. static unsigned int iterations;
  39. module_param(iterations, uint, S_IRUGO);
  40. MODULE_PARM_DESC(iterations,
  41. "Iterations before stopping test (default: infinite)");
  42. static unsigned int xor_sources = 3;
  43. module_param(xor_sources, uint, S_IRUGO);
  44. MODULE_PARM_DESC(xor_sources,
  45. "Number of xor source buffers (default: 3)");
  46. static unsigned int pq_sources = 3;
  47. module_param(pq_sources, uint, S_IRUGO);
  48. MODULE_PARM_DESC(pq_sources,
  49. "Number of p+q source buffers (default: 3)");
  50. static int timeout = 3000;
  51. module_param(timeout, uint, S_IRUGO);
  52. MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
  53. "Pass -1 for infinite timeout");
  54. /*
  55. * Initialization patterns. All bytes in the source buffer has bit 7
  56. * set, all bytes in the destination buffer has bit 7 cleared.
  57. *
  58. * Bit 6 is set for all bytes which are to be copied by the DMA
  59. * engine. Bit 5 is set for all bytes which are to be overwritten by
  60. * the DMA engine.
  61. *
  62. * The remaining bits are the inverse of a counter which increments by
  63. * one for each byte address.
  64. */
  65. #define PATTERN_SRC 0x80
  66. #define PATTERN_DST 0x00
  67. #define PATTERN_COPY 0x40
  68. #define PATTERN_OVERWRITE 0x20
  69. #define PATTERN_COUNT_MASK 0x1f
  70. struct dmatest_thread {
  71. struct list_head node;
  72. struct task_struct *task;
  73. struct dma_chan *chan;
  74. u8 **srcs;
  75. u8 **dsts;
  76. enum dma_transaction_type type;
  77. };
  78. struct dmatest_chan {
  79. struct list_head node;
  80. struct dma_chan *chan;
  81. struct list_head threads;
  82. };
  83. /*
  84. * These are protected by dma_list_mutex since they're only used by
  85. * the DMA filter function callback
  86. */
  87. static LIST_HEAD(dmatest_channels);
  88. static unsigned int nr_channels;
  89. static bool dmatest_match_channel(struct dma_chan *chan)
  90. {
  91. if (test_channel[0] == '\0')
  92. return true;
  93. return strcmp(dma_chan_name(chan), test_channel) == 0;
  94. }
  95. static bool dmatest_match_device(struct dma_device *device)
  96. {
  97. if (test_device[0] == '\0')
  98. return true;
  99. return strcmp(dev_name(device->dev), test_device) == 0;
  100. }
  101. static unsigned long dmatest_random(void)
  102. {
  103. unsigned long buf;
  104. get_random_bytes(&buf, sizeof(buf));
  105. return buf;
  106. }
  107. static void dmatest_init_srcs(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_SRC | (~i & PATTERN_COUNT_MASK);
  114. for ( ; i < start + len; i++)
  115. buf[i] = PATTERN_SRC | PATTERN_COPY
  116. | (~i & PATTERN_COUNT_MASK);
  117. for ( ; i < test_buf_size; i++)
  118. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  119. buf++;
  120. }
  121. }
  122. static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
  123. {
  124. unsigned int i;
  125. u8 *buf;
  126. for (; (buf = *bufs); bufs++) {
  127. for (i = 0; i < start; i++)
  128. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  129. for ( ; i < start + len; i++)
  130. buf[i] = PATTERN_DST | PATTERN_OVERWRITE
  131. | (~i & PATTERN_COUNT_MASK);
  132. for ( ; i < test_buf_size; i++)
  133. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  134. }
  135. }
  136. static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
  137. unsigned int counter, bool is_srcbuf)
  138. {
  139. u8 diff = actual ^ pattern;
  140. u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
  141. const char *thread_name = current->comm;
  142. if (is_srcbuf)
  143. pr_warning("%s: srcbuf[0x%x] overwritten!"
  144. " Expected %02x, got %02x\n",
  145. thread_name, index, expected, actual);
  146. else if ((pattern & PATTERN_COPY)
  147. && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
  148. pr_warning("%s: dstbuf[0x%x] not copied!"
  149. " Expected %02x, got %02x\n",
  150. thread_name, index, expected, actual);
  151. else if (diff & PATTERN_SRC)
  152. pr_warning("%s: dstbuf[0x%x] was copied!"
  153. " Expected %02x, got %02x\n",
  154. thread_name, index, expected, actual);
  155. else
  156. pr_warning("%s: dstbuf[0x%x] mismatch!"
  157. " Expected %02x, got %02x\n",
  158. thread_name, index, expected, actual);
  159. }
  160. static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
  161. unsigned int end, unsigned int counter, u8 pattern,
  162. bool is_srcbuf)
  163. {
  164. unsigned int i;
  165. unsigned int error_count = 0;
  166. u8 actual;
  167. u8 expected;
  168. u8 *buf;
  169. unsigned int counter_orig = counter;
  170. for (; (buf = *bufs); bufs++) {
  171. counter = counter_orig;
  172. for (i = start; i < end; i++) {
  173. actual = buf[i];
  174. expected = pattern | (~counter & PATTERN_COUNT_MASK);
  175. if (actual != expected) {
  176. if (error_count < 32)
  177. dmatest_mismatch(actual, pattern, i,
  178. counter, is_srcbuf);
  179. error_count++;
  180. }
  181. counter++;
  182. }
  183. }
  184. if (error_count > 32)
  185. pr_warning("%s: %u errors suppressed\n",
  186. current->comm, error_count - 32);
  187. return error_count;
  188. }
  189. /* poor man's completion - we want to use wait_event_freezable() on it */
  190. struct dmatest_done {
  191. bool done;
  192. wait_queue_head_t *wait;
  193. };
  194. static void dmatest_callback(void *arg)
  195. {
  196. struct dmatest_done *done = arg;
  197. done->done = true;
  198. wake_up_all(done->wait);
  199. }
  200. static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
  201. unsigned int count)
  202. {
  203. while (count--)
  204. dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
  205. }
  206. static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
  207. unsigned int count)
  208. {
  209. while (count--)
  210. dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
  211. }
  212. static unsigned int min_odd(unsigned int x, unsigned int y)
  213. {
  214. unsigned int val = min(x, y);
  215. return val % 2 ? val : val - 1;
  216. }
  217. /*
  218. * This function repeatedly tests DMA transfers of various lengths and
  219. * offsets for a given operation type until it is told to exit by
  220. * kthread_stop(). There may be multiple threads running this function
  221. * in parallel for a single channel, and there may be multiple channels
  222. * being tested in parallel.
  223. *
  224. * Before each test, the source and destination buffer is initialized
  225. * with a known pattern. This pattern is different depending on
  226. * whether it's in an area which is supposed to be copied or
  227. * overwritten, and different in the source and destination buffers.
  228. * So if the DMA engine doesn't copy exactly what we tell it to copy,
  229. * we'll notice.
  230. */
  231. static int dmatest_func(void *data)
  232. {
  233. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
  234. struct dmatest_thread *thread = data;
  235. struct dmatest_done done = { .wait = &done_wait };
  236. struct dma_chan *chan;
  237. struct dma_device *dev;
  238. const char *thread_name;
  239. unsigned int src_off, dst_off, len;
  240. unsigned int error_count;
  241. unsigned int failed_tests = 0;
  242. unsigned int total_tests = 0;
  243. dma_cookie_t cookie;
  244. enum dma_status status;
  245. enum dma_ctrl_flags flags;
  246. u8 *pq_coefs = NULL;
  247. int ret;
  248. int src_cnt;
  249. int dst_cnt;
  250. int i;
  251. thread_name = current->comm;
  252. set_freezable();
  253. ret = -ENOMEM;
  254. smp_rmb();
  255. chan = thread->chan;
  256. dev = chan->device;
  257. if (thread->type == DMA_MEMCPY)
  258. src_cnt = dst_cnt = 1;
  259. else if (thread->type == DMA_XOR) {
  260. /* force odd to ensure dst = src */
  261. src_cnt = min_odd(xor_sources | 1, dev->max_xor);
  262. dst_cnt = 1;
  263. } else if (thread->type == DMA_PQ) {
  264. /* force odd to ensure dst = src */
  265. src_cnt = min_odd(pq_sources | 1, dma_maxpq(dev, 0));
  266. dst_cnt = 2;
  267. pq_coefs = kmalloc(pq_sources+1, GFP_KERNEL);
  268. if (!pq_coefs)
  269. goto err_thread_type;
  270. for (i = 0; i < src_cnt; i++)
  271. pq_coefs[i] = 1;
  272. } else
  273. goto err_thread_type;
  274. thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  275. if (!thread->srcs)
  276. goto err_srcs;
  277. for (i = 0; i < src_cnt; i++) {
  278. thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
  279. if (!thread->srcs[i])
  280. goto err_srcbuf;
  281. }
  282. thread->srcs[i] = NULL;
  283. thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  284. if (!thread->dsts)
  285. goto err_dsts;
  286. for (i = 0; i < dst_cnt; i++) {
  287. thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
  288. if (!thread->dsts[i])
  289. goto err_dstbuf;
  290. }
  291. thread->dsts[i] = NULL;
  292. set_user_nice(current, 10);
  293. /*
  294. * src buffers are freed by the DMAEngine code with dma_unmap_single()
  295. * dst buffers are freed by ourselves below
  296. */
  297. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
  298. | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
  299. while (!kthread_should_stop()
  300. && !(iterations && total_tests >= iterations)) {
  301. struct dma_async_tx_descriptor *tx = NULL;
  302. dma_addr_t dma_srcs[src_cnt];
  303. dma_addr_t dma_dsts[dst_cnt];
  304. u8 align = 0;
  305. total_tests++;
  306. /* honor alignment restrictions */
  307. if (thread->type == DMA_MEMCPY)
  308. align = dev->copy_align;
  309. else if (thread->type == DMA_XOR)
  310. align = dev->xor_align;
  311. else if (thread->type == DMA_PQ)
  312. align = dev->pq_align;
  313. if (1 << align > test_buf_size) {
  314. pr_err("%u-byte buffer too small for %d-byte alignment\n",
  315. test_buf_size, 1 << align);
  316. break;
  317. }
  318. len = dmatest_random() % test_buf_size + 1;
  319. len = (len >> align) << align;
  320. if (!len)
  321. len = 1 << align;
  322. src_off = dmatest_random() % (test_buf_size - len + 1);
  323. dst_off = dmatest_random() % (test_buf_size - len + 1);
  324. src_off = (src_off >> align) << align;
  325. dst_off = (dst_off >> align) << align;
  326. dmatest_init_srcs(thread->srcs, src_off, len);
  327. dmatest_init_dsts(thread->dsts, dst_off, len);
  328. for (i = 0; i < src_cnt; i++) {
  329. u8 *buf = thread->srcs[i] + src_off;
  330. dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  331. DMA_TO_DEVICE);
  332. ret = dma_mapping_error(dev->dev, dma_srcs[i]);
  333. if (ret) {
  334. unmap_src(dev->dev, dma_srcs, len, i);
  335. pr_warn("%s: #%u: mapping error %d with "
  336. "src_off=0x%x len=0x%x\n",
  337. thread_name, total_tests - 1, ret,
  338. src_off, len);
  339. failed_tests++;
  340. continue;
  341. }
  342. }
  343. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  344. for (i = 0; i < dst_cnt; i++) {
  345. dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  346. test_buf_size,
  347. DMA_BIDIRECTIONAL);
  348. ret = dma_mapping_error(dev->dev, dma_dsts[i]);
  349. if (ret) {
  350. unmap_src(dev->dev, dma_srcs, len, src_cnt);
  351. unmap_dst(dev->dev, dma_dsts, test_buf_size, i);
  352. pr_warn("%s: #%u: mapping error %d with "
  353. "dst_off=0x%x len=0x%x\n",
  354. thread_name, total_tests - 1, ret,
  355. dst_off, test_buf_size);
  356. failed_tests++;
  357. continue;
  358. }
  359. }
  360. if (thread->type == DMA_MEMCPY)
  361. tx = dev->device_prep_dma_memcpy(chan,
  362. dma_dsts[0] + dst_off,
  363. dma_srcs[0], len,
  364. flags);
  365. else if (thread->type == DMA_XOR)
  366. tx = dev->device_prep_dma_xor(chan,
  367. dma_dsts[0] + dst_off,
  368. dma_srcs, src_cnt,
  369. len, flags);
  370. else if (thread->type == DMA_PQ) {
  371. dma_addr_t dma_pq[dst_cnt];
  372. for (i = 0; i < dst_cnt; i++)
  373. dma_pq[i] = dma_dsts[i] + dst_off;
  374. tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
  375. src_cnt, pq_coefs,
  376. len, flags);
  377. }
  378. if (!tx) {
  379. unmap_src(dev->dev, dma_srcs, len, src_cnt);
  380. unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt);
  381. pr_warning("%s: #%u: prep error with src_off=0x%x "
  382. "dst_off=0x%x len=0x%x\n",
  383. thread_name, total_tests - 1,
  384. src_off, dst_off, len);
  385. msleep(100);
  386. failed_tests++;
  387. continue;
  388. }
  389. done.done = false;
  390. tx->callback = dmatest_callback;
  391. tx->callback_param = &done;
  392. cookie = tx->tx_submit(tx);
  393. if (dma_submit_error(cookie)) {
  394. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  395. "dst_off=0x%x len=0x%x\n",
  396. thread_name, total_tests - 1, cookie,
  397. src_off, dst_off, len);
  398. msleep(100);
  399. failed_tests++;
  400. continue;
  401. }
  402. dma_async_issue_pending(chan);
  403. wait_event_freezable_timeout(done_wait,
  404. done.done || kthread_should_stop(),
  405. msecs_to_jiffies(timeout));
  406. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  407. if (!done.done) {
  408. /*
  409. * We're leaving the timed out dma operation with
  410. * dangling pointer to done_wait. To make this
  411. * correct, we'll need to allocate wait_done for
  412. * each test iteration and perform "who's gonna
  413. * free it this time?" dancing. For now, just
  414. * leave it dangling.
  415. */
  416. pr_warning("%s: #%u: test timed out\n",
  417. thread_name, total_tests - 1);
  418. failed_tests++;
  419. continue;
  420. } else if (status != DMA_SUCCESS) {
  421. pr_warning("%s: #%u: got completion callback,"
  422. " but status is \'%s\'\n",
  423. thread_name, total_tests - 1,
  424. status == DMA_ERROR ? "error" : "in progress");
  425. failed_tests++;
  426. continue;
  427. }
  428. /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
  429. unmap_dst(dev->dev, dma_dsts, test_buf_size, dst_cnt);
  430. error_count = 0;
  431. pr_debug("%s: verifying source buffer...\n", thread_name);
  432. error_count += dmatest_verify(thread->srcs, 0, src_off,
  433. 0, PATTERN_SRC, true);
  434. error_count += dmatest_verify(thread->srcs, src_off,
  435. src_off + len, src_off,
  436. PATTERN_SRC | PATTERN_COPY, true);
  437. error_count += dmatest_verify(thread->srcs, src_off + len,
  438. test_buf_size, src_off + len,
  439. PATTERN_SRC, true);
  440. pr_debug("%s: verifying dest buffer...\n",
  441. thread->task->comm);
  442. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  443. 0, PATTERN_DST, false);
  444. error_count += dmatest_verify(thread->dsts, dst_off,
  445. dst_off + len, src_off,
  446. PATTERN_SRC | PATTERN_COPY, false);
  447. error_count += dmatest_verify(thread->dsts, dst_off + len,
  448. test_buf_size, dst_off + len,
  449. PATTERN_DST, false);
  450. if (error_count) {
  451. pr_warning("%s: #%u: %u errors with "
  452. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  453. thread_name, total_tests - 1, error_count,
  454. src_off, dst_off, len);
  455. failed_tests++;
  456. } else {
  457. pr_debug("%s: #%u: No errors with "
  458. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  459. thread_name, total_tests - 1,
  460. src_off, dst_off, len);
  461. }
  462. }
  463. ret = 0;
  464. for (i = 0; thread->dsts[i]; i++)
  465. kfree(thread->dsts[i]);
  466. err_dstbuf:
  467. kfree(thread->dsts);
  468. err_dsts:
  469. for (i = 0; thread->srcs[i]; i++)
  470. kfree(thread->srcs[i]);
  471. err_srcbuf:
  472. kfree(thread->srcs);
  473. err_srcs:
  474. kfree(pq_coefs);
  475. err_thread_type:
  476. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  477. thread_name, total_tests, failed_tests, ret);
  478. /* terminate all transfers on specified channels */
  479. if (ret)
  480. dmaengine_terminate_all(chan);
  481. if (iterations > 0)
  482. while (!kthread_should_stop()) {
  483. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
  484. interruptible_sleep_on(&wait_dmatest_exit);
  485. }
  486. return ret;
  487. }
  488. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  489. {
  490. struct dmatest_thread *thread;
  491. struct dmatest_thread *_thread;
  492. int ret;
  493. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  494. ret = kthread_stop(thread->task);
  495. pr_debug("dmatest: thread %s exited with status %d\n",
  496. thread->task->comm, ret);
  497. list_del(&thread->node);
  498. kfree(thread);
  499. }
  500. /* terminate all transfers on specified channels */
  501. dmaengine_terminate_all(dtc->chan);
  502. kfree(dtc);
  503. }
  504. static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
  505. {
  506. struct dmatest_thread *thread;
  507. struct dma_chan *chan = dtc->chan;
  508. char *op;
  509. unsigned int i;
  510. if (type == DMA_MEMCPY)
  511. op = "copy";
  512. else if (type == DMA_XOR)
  513. op = "xor";
  514. else if (type == DMA_PQ)
  515. op = "pq";
  516. else
  517. return -EINVAL;
  518. for (i = 0; i < threads_per_chan; i++) {
  519. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  520. if (!thread) {
  521. pr_warning("dmatest: No memory for %s-%s%u\n",
  522. dma_chan_name(chan), op, i);
  523. break;
  524. }
  525. thread->chan = dtc->chan;
  526. thread->type = type;
  527. smp_wmb();
  528. thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  529. dma_chan_name(chan), op, i);
  530. if (IS_ERR(thread->task)) {
  531. pr_warning("dmatest: Failed to run thread %s-%s%u\n",
  532. dma_chan_name(chan), op, i);
  533. kfree(thread);
  534. break;
  535. }
  536. /* srcbuf and dstbuf are allocated by the thread itself */
  537. list_add_tail(&thread->node, &dtc->threads);
  538. }
  539. return i;
  540. }
  541. static int dmatest_add_channel(struct dma_chan *chan)
  542. {
  543. struct dmatest_chan *dtc;
  544. struct dma_device *dma_dev = chan->device;
  545. unsigned int thread_count = 0;
  546. int cnt;
  547. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  548. if (!dtc) {
  549. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  550. return -ENOMEM;
  551. }
  552. dtc->chan = chan;
  553. INIT_LIST_HEAD(&dtc->threads);
  554. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  555. cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
  556. thread_count += cnt > 0 ? cnt : 0;
  557. }
  558. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  559. cnt = dmatest_add_threads(dtc, DMA_XOR);
  560. thread_count += cnt > 0 ? cnt : 0;
  561. }
  562. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  563. cnt = dmatest_add_threads(dtc, DMA_PQ);
  564. thread_count += cnt > 0 ? cnt : 0;
  565. }
  566. pr_info("dmatest: Started %u threads using %s\n",
  567. thread_count, dma_chan_name(chan));
  568. list_add_tail(&dtc->node, &dmatest_channels);
  569. nr_channels++;
  570. return 0;
  571. }
  572. static bool filter(struct dma_chan *chan, void *param)
  573. {
  574. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  575. return false;
  576. else
  577. return true;
  578. }
  579. static int __init dmatest_init(void)
  580. {
  581. dma_cap_mask_t mask;
  582. struct dma_chan *chan;
  583. int err = 0;
  584. dma_cap_zero(mask);
  585. dma_cap_set(DMA_MEMCPY, mask);
  586. for (;;) {
  587. chan = dma_request_channel(mask, filter, NULL);
  588. if (chan) {
  589. err = dmatest_add_channel(chan);
  590. if (err) {
  591. dma_release_channel(chan);
  592. break; /* add_channel failed, punt */
  593. }
  594. } else
  595. break; /* no more channels available */
  596. if (max_channels && nr_channels >= max_channels)
  597. break; /* we have all we need */
  598. }
  599. return err;
  600. }
  601. /* when compiled-in wait for drivers to load first */
  602. late_initcall(dmatest_init);
  603. static void __exit dmatest_exit(void)
  604. {
  605. struct dmatest_chan *dtc, *_dtc;
  606. struct dma_chan *chan;
  607. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  608. list_del(&dtc->node);
  609. chan = dtc->chan;
  610. dmatest_cleanup_channel(dtc);
  611. pr_debug("dmatest: dropped channel %s\n",
  612. dma_chan_name(chan));
  613. dma_release_channel(chan);
  614. }
  615. }
  616. module_exit(dmatest_exit);
  617. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  618. MODULE_LICENSE("GPL v2");