dmatest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. /*
  201. * This function repeatedly tests DMA transfers of various lengths and
  202. * offsets for a given operation type until it is told to exit by
  203. * kthread_stop(). There may be multiple threads running this function
  204. * in parallel for a single channel, and there may be multiple channels
  205. * being tested in parallel.
  206. *
  207. * Before each test, the source and destination buffer is initialized
  208. * with a known pattern. This pattern is different depending on
  209. * whether it's in an area which is supposed to be copied or
  210. * overwritten, and different in the source and destination buffers.
  211. * So if the DMA engine doesn't copy exactly what we tell it to copy,
  212. * we'll notice.
  213. */
  214. static int dmatest_func(void *data)
  215. {
  216. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
  217. struct dmatest_thread *thread = data;
  218. struct dmatest_done done = { .wait = &done_wait };
  219. struct dma_chan *chan;
  220. const char *thread_name;
  221. unsigned int src_off, dst_off, len;
  222. unsigned int error_count;
  223. unsigned int failed_tests = 0;
  224. unsigned int total_tests = 0;
  225. dma_cookie_t cookie;
  226. enum dma_status status;
  227. enum dma_ctrl_flags flags;
  228. u8 pq_coefs[pq_sources + 1];
  229. int ret;
  230. int src_cnt;
  231. int dst_cnt;
  232. int i;
  233. thread_name = current->comm;
  234. set_freezable();
  235. ret = -ENOMEM;
  236. smp_rmb();
  237. chan = thread->chan;
  238. if (thread->type == DMA_MEMCPY)
  239. src_cnt = dst_cnt = 1;
  240. else if (thread->type == DMA_XOR) {
  241. src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
  242. dst_cnt = 1;
  243. } else if (thread->type == DMA_PQ) {
  244. src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
  245. dst_cnt = 2;
  246. for (i = 0; i < src_cnt; i++)
  247. pq_coefs[i] = 1;
  248. } else
  249. goto err_srcs;
  250. thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  251. if (!thread->srcs)
  252. goto err_srcs;
  253. for (i = 0; i < src_cnt; i++) {
  254. thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
  255. if (!thread->srcs[i])
  256. goto err_srcbuf;
  257. }
  258. thread->srcs[i] = NULL;
  259. thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  260. if (!thread->dsts)
  261. goto err_dsts;
  262. for (i = 0; i < dst_cnt; i++) {
  263. thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
  264. if (!thread->dsts[i])
  265. goto err_dstbuf;
  266. }
  267. thread->dsts[i] = NULL;
  268. set_user_nice(current, 10);
  269. /*
  270. * src buffers are freed by the DMAEngine code with dma_unmap_single()
  271. * dst buffers are freed by ourselves below
  272. */
  273. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
  274. | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
  275. while (!kthread_should_stop()
  276. && !(iterations && total_tests >= iterations)) {
  277. struct dma_device *dev = chan->device;
  278. struct dma_async_tx_descriptor *tx = NULL;
  279. dma_addr_t dma_srcs[src_cnt];
  280. dma_addr_t dma_dsts[dst_cnt];
  281. u8 align = 0;
  282. total_tests++;
  283. /* honor alignment restrictions */
  284. if (thread->type == DMA_MEMCPY)
  285. align = dev->copy_align;
  286. else if (thread->type == DMA_XOR)
  287. align = dev->xor_align;
  288. else if (thread->type == DMA_PQ)
  289. align = dev->pq_align;
  290. if (1 << align > test_buf_size) {
  291. pr_err("%u-byte buffer too small for %d-byte alignment\n",
  292. test_buf_size, 1 << align);
  293. break;
  294. }
  295. len = dmatest_random() % test_buf_size + 1;
  296. len = (len >> align) << align;
  297. if (!len)
  298. len = 1 << align;
  299. src_off = dmatest_random() % (test_buf_size - len + 1);
  300. dst_off = dmatest_random() % (test_buf_size - len + 1);
  301. src_off = (src_off >> align) << align;
  302. dst_off = (dst_off >> align) << align;
  303. dmatest_init_srcs(thread->srcs, src_off, len);
  304. dmatest_init_dsts(thread->dsts, dst_off, len);
  305. for (i = 0; i < src_cnt; i++) {
  306. u8 *buf = thread->srcs[i] + src_off;
  307. dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  308. DMA_TO_DEVICE);
  309. }
  310. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  311. for (i = 0; i < dst_cnt; i++) {
  312. dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  313. test_buf_size,
  314. DMA_BIDIRECTIONAL);
  315. }
  316. if (thread->type == DMA_MEMCPY)
  317. tx = dev->device_prep_dma_memcpy(chan,
  318. dma_dsts[0] + dst_off,
  319. dma_srcs[0], len,
  320. flags);
  321. else if (thread->type == DMA_XOR)
  322. tx = dev->device_prep_dma_xor(chan,
  323. dma_dsts[0] + dst_off,
  324. dma_srcs, src_cnt,
  325. len, flags);
  326. else if (thread->type == DMA_PQ) {
  327. dma_addr_t dma_pq[dst_cnt];
  328. for (i = 0; i < dst_cnt; i++)
  329. dma_pq[i] = dma_dsts[i] + dst_off;
  330. tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
  331. src_cnt, pq_coefs,
  332. len, flags);
  333. }
  334. if (!tx) {
  335. for (i = 0; i < src_cnt; i++)
  336. dma_unmap_single(dev->dev, dma_srcs[i], len,
  337. DMA_TO_DEVICE);
  338. for (i = 0; i < dst_cnt; i++)
  339. dma_unmap_single(dev->dev, dma_dsts[i],
  340. test_buf_size,
  341. DMA_BIDIRECTIONAL);
  342. pr_warning("%s: #%u: prep error with src_off=0x%x "
  343. "dst_off=0x%x len=0x%x\n",
  344. thread_name, total_tests - 1,
  345. src_off, dst_off, len);
  346. msleep(100);
  347. failed_tests++;
  348. continue;
  349. }
  350. done.done = false;
  351. tx->callback = dmatest_callback;
  352. tx->callback_param = &done;
  353. cookie = tx->tx_submit(tx);
  354. if (dma_submit_error(cookie)) {
  355. pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  356. "dst_off=0x%x len=0x%x\n",
  357. thread_name, total_tests - 1, cookie,
  358. src_off, dst_off, len);
  359. msleep(100);
  360. failed_tests++;
  361. continue;
  362. }
  363. dma_async_issue_pending(chan);
  364. wait_event_freezable_timeout(done_wait, done.done,
  365. msecs_to_jiffies(timeout));
  366. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  367. if (!done.done) {
  368. /*
  369. * We're leaving the timed out dma operation with
  370. * dangling pointer to done_wait. To make this
  371. * correct, we'll need to allocate wait_done for
  372. * each test iteration and perform "who's gonna
  373. * free it this time?" dancing. For now, just
  374. * leave it dangling.
  375. */
  376. pr_warning("%s: #%u: test timed out\n",
  377. thread_name, total_tests - 1);
  378. failed_tests++;
  379. continue;
  380. } else if (status != DMA_SUCCESS) {
  381. pr_warning("%s: #%u: got completion callback,"
  382. " but status is \'%s\'\n",
  383. thread_name, total_tests - 1,
  384. status == DMA_ERROR ? "error" : "in progress");
  385. failed_tests++;
  386. continue;
  387. }
  388. /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
  389. for (i = 0; i < dst_cnt; i++)
  390. dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
  391. DMA_BIDIRECTIONAL);
  392. error_count = 0;
  393. pr_debug("%s: verifying source buffer...\n", thread_name);
  394. error_count += dmatest_verify(thread->srcs, 0, src_off,
  395. 0, PATTERN_SRC, true);
  396. error_count += dmatest_verify(thread->srcs, src_off,
  397. src_off + len, src_off,
  398. PATTERN_SRC | PATTERN_COPY, true);
  399. error_count += dmatest_verify(thread->srcs, src_off + len,
  400. test_buf_size, src_off + len,
  401. PATTERN_SRC, true);
  402. pr_debug("%s: verifying dest buffer...\n",
  403. thread->task->comm);
  404. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  405. 0, PATTERN_DST, false);
  406. error_count += dmatest_verify(thread->dsts, dst_off,
  407. dst_off + len, src_off,
  408. PATTERN_SRC | PATTERN_COPY, false);
  409. error_count += dmatest_verify(thread->dsts, dst_off + len,
  410. test_buf_size, dst_off + len,
  411. PATTERN_DST, false);
  412. if (error_count) {
  413. pr_warning("%s: #%u: %u errors with "
  414. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  415. thread_name, total_tests - 1, error_count,
  416. src_off, dst_off, len);
  417. failed_tests++;
  418. } else {
  419. pr_debug("%s: #%u: No errors with "
  420. "src_off=0x%x dst_off=0x%x len=0x%x\n",
  421. thread_name, total_tests - 1,
  422. src_off, dst_off, len);
  423. }
  424. }
  425. ret = 0;
  426. for (i = 0; thread->dsts[i]; i++)
  427. kfree(thread->dsts[i]);
  428. err_dstbuf:
  429. kfree(thread->dsts);
  430. err_dsts:
  431. for (i = 0; thread->srcs[i]; i++)
  432. kfree(thread->srcs[i]);
  433. err_srcbuf:
  434. kfree(thread->srcs);
  435. err_srcs:
  436. pr_notice("%s: terminating after %u tests, %u failures (status %d)\n",
  437. thread_name, total_tests, failed_tests, ret);
  438. /* terminate all transfers on specified channels */
  439. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  440. if (iterations > 0)
  441. while (!kthread_should_stop()) {
  442. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
  443. interruptible_sleep_on(&wait_dmatest_exit);
  444. }
  445. return ret;
  446. }
  447. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  448. {
  449. struct dmatest_thread *thread;
  450. struct dmatest_thread *_thread;
  451. int ret;
  452. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  453. ret = kthread_stop(thread->task);
  454. pr_debug("dmatest: thread %s exited with status %d\n",
  455. thread->task->comm, ret);
  456. list_del(&thread->node);
  457. kfree(thread);
  458. }
  459. /* terminate all transfers on specified channels */
  460. dtc->chan->device->device_control(dtc->chan, DMA_TERMINATE_ALL, 0);
  461. kfree(dtc);
  462. }
  463. static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
  464. {
  465. struct dmatest_thread *thread;
  466. struct dma_chan *chan = dtc->chan;
  467. char *op;
  468. unsigned int i;
  469. if (type == DMA_MEMCPY)
  470. op = "copy";
  471. else if (type == DMA_XOR)
  472. op = "xor";
  473. else if (type == DMA_PQ)
  474. op = "pq";
  475. else
  476. return -EINVAL;
  477. for (i = 0; i < threads_per_chan; i++) {
  478. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  479. if (!thread) {
  480. pr_warning("dmatest: No memory for %s-%s%u\n",
  481. dma_chan_name(chan), op, i);
  482. break;
  483. }
  484. thread->chan = dtc->chan;
  485. thread->type = type;
  486. smp_wmb();
  487. thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  488. dma_chan_name(chan), op, i);
  489. if (IS_ERR(thread->task)) {
  490. pr_warning("dmatest: Failed to run thread %s-%s%u\n",
  491. dma_chan_name(chan), op, i);
  492. kfree(thread);
  493. break;
  494. }
  495. /* srcbuf and dstbuf are allocated by the thread itself */
  496. list_add_tail(&thread->node, &dtc->threads);
  497. }
  498. return i;
  499. }
  500. static int dmatest_add_channel(struct dma_chan *chan)
  501. {
  502. struct dmatest_chan *dtc;
  503. struct dma_device *dma_dev = chan->device;
  504. unsigned int thread_count = 0;
  505. int cnt;
  506. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  507. if (!dtc) {
  508. pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan));
  509. return -ENOMEM;
  510. }
  511. dtc->chan = chan;
  512. INIT_LIST_HEAD(&dtc->threads);
  513. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  514. cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
  515. thread_count += cnt > 0 ? cnt : 0;
  516. }
  517. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  518. cnt = dmatest_add_threads(dtc, DMA_XOR);
  519. thread_count += cnt > 0 ? cnt : 0;
  520. }
  521. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  522. cnt = dmatest_add_threads(dtc, DMA_PQ);
  523. thread_count += cnt > 0 ? cnt : 0;
  524. }
  525. pr_info("dmatest: Started %u threads using %s\n",
  526. thread_count, dma_chan_name(chan));
  527. list_add_tail(&dtc->node, &dmatest_channels);
  528. nr_channels++;
  529. return 0;
  530. }
  531. static bool filter(struct dma_chan *chan, void *param)
  532. {
  533. if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
  534. return false;
  535. else
  536. return true;
  537. }
  538. static int __init dmatest_init(void)
  539. {
  540. dma_cap_mask_t mask;
  541. struct dma_chan *chan;
  542. int err = 0;
  543. dma_cap_zero(mask);
  544. dma_cap_set(DMA_MEMCPY, mask);
  545. for (;;) {
  546. chan = dma_request_channel(mask, filter, NULL);
  547. if (chan) {
  548. err = dmatest_add_channel(chan);
  549. if (err) {
  550. dma_release_channel(chan);
  551. break; /* add_channel failed, punt */
  552. }
  553. } else
  554. break; /* no more channels available */
  555. if (max_channels && nr_channels >= max_channels)
  556. break; /* we have all we need */
  557. }
  558. return err;
  559. }
  560. /* when compiled-in wait for drivers to load first */
  561. late_initcall(dmatest_init);
  562. static void __exit dmatest_exit(void)
  563. {
  564. struct dmatest_chan *dtc, *_dtc;
  565. struct dma_chan *chan;
  566. list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  567. list_del(&dtc->node);
  568. chan = dtc->chan;
  569. dmatest_cleanup_channel(dtc);
  570. pr_debug("dmatest: dropped channel %s\n",
  571. dma_chan_name(chan));
  572. dma_release_channel(chan);
  573. }
  574. }
  575. module_exit(dmatest_exit);
  576. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  577. MODULE_LICENSE("GPL v2");