dmatest.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * DMA Engine test module
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. * Copyright (C) 2013 Intel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/delay.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/freezer.h>
  16. #include <linux/init.h>
  17. #include <linux/kthread.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/random.h>
  21. #include <linux/slab.h>
  22. #include <linux/wait.h>
  23. static unsigned int test_buf_size = 16384;
  24. module_param(test_buf_size, uint, S_IRUGO | S_IWUSR);
  25. MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
  26. static char test_channel[20];
  27. module_param_string(channel, test_channel, sizeof(test_channel),
  28. S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
  30. static char test_device[20];
  31. module_param_string(device, test_device, sizeof(test_device),
  32. S_IRUGO | S_IWUSR);
  33. MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
  34. static unsigned int threads_per_chan = 1;
  35. module_param(threads_per_chan, uint, S_IRUGO | S_IWUSR);
  36. MODULE_PARM_DESC(threads_per_chan,
  37. "Number of threads to start per channel (default: 1)");
  38. static unsigned int max_channels;
  39. module_param(max_channels, uint, S_IRUGO | S_IWUSR);
  40. MODULE_PARM_DESC(max_channels,
  41. "Maximum number of channels to use (default: all)");
  42. static unsigned int iterations;
  43. module_param(iterations, uint, S_IRUGO | S_IWUSR);
  44. MODULE_PARM_DESC(iterations,
  45. "Iterations before stopping test (default: infinite)");
  46. static unsigned int xor_sources = 3;
  47. module_param(xor_sources, uint, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(xor_sources,
  49. "Number of xor source buffers (default: 3)");
  50. static unsigned int pq_sources = 3;
  51. module_param(pq_sources, uint, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(pq_sources,
  53. "Number of p+q source buffers (default: 3)");
  54. static int timeout = 3000;
  55. module_param(timeout, uint, S_IRUGO | S_IWUSR);
  56. MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
  57. "Pass -1 for infinite timeout");
  58. /**
  59. * struct dmatest_params - test parameters.
  60. * @buf_size: size of the memcpy test buffer
  61. * @channel: bus ID of the channel to test
  62. * @device: bus ID of the DMA Engine to test
  63. * @threads_per_chan: number of threads to start per channel
  64. * @max_channels: maximum number of channels to use
  65. * @iterations: iterations before stopping test
  66. * @xor_sources: number of xor source buffers
  67. * @pq_sources: number of p+q source buffers
  68. * @timeout: transfer timeout in msec, -1 for infinite timeout
  69. */
  70. struct dmatest_params {
  71. unsigned int buf_size;
  72. char channel[20];
  73. char device[20];
  74. unsigned int threads_per_chan;
  75. unsigned int max_channels;
  76. unsigned int iterations;
  77. unsigned int xor_sources;
  78. unsigned int pq_sources;
  79. int timeout;
  80. };
  81. /**
  82. * struct dmatest_info - test information.
  83. * @params: test parameters
  84. * @lock: access protection to the fields of this structure
  85. */
  86. static struct dmatest_info {
  87. /* Test parameters */
  88. struct dmatest_params params;
  89. /* Internal state */
  90. struct list_head channels;
  91. unsigned int nr_channels;
  92. struct mutex lock;
  93. bool did_init;
  94. } test_info = {
  95. .channels = LIST_HEAD_INIT(test_info.channels),
  96. .lock = __MUTEX_INITIALIZER(test_info.lock),
  97. };
  98. static int dmatest_run_set(const char *val, const struct kernel_param *kp);
  99. static int dmatest_run_get(char *val, const struct kernel_param *kp);
  100. static struct kernel_param_ops run_ops = {
  101. .set = dmatest_run_set,
  102. .get = dmatest_run_get,
  103. };
  104. static bool dmatest_run;
  105. module_param_cb(run, &run_ops, &dmatest_run, S_IRUGO | S_IWUSR);
  106. MODULE_PARM_DESC(run, "Run the test (default: false)");
  107. /* Maximum amount of mismatched bytes in buffer to print */
  108. #define MAX_ERROR_COUNT 32
  109. /*
  110. * Initialization patterns. All bytes in the source buffer has bit 7
  111. * set, all bytes in the destination buffer has bit 7 cleared.
  112. *
  113. * Bit 6 is set for all bytes which are to be copied by the DMA
  114. * engine. Bit 5 is set for all bytes which are to be overwritten by
  115. * the DMA engine.
  116. *
  117. * The remaining bits are the inverse of a counter which increments by
  118. * one for each byte address.
  119. */
  120. #define PATTERN_SRC 0x80
  121. #define PATTERN_DST 0x00
  122. #define PATTERN_COPY 0x40
  123. #define PATTERN_OVERWRITE 0x20
  124. #define PATTERN_COUNT_MASK 0x1f
  125. struct dmatest_thread {
  126. struct list_head node;
  127. struct dmatest_info *info;
  128. struct task_struct *task;
  129. struct dma_chan *chan;
  130. u8 **srcs;
  131. u8 **dsts;
  132. enum dma_transaction_type type;
  133. bool done;
  134. };
  135. struct dmatest_chan {
  136. struct list_head node;
  137. struct dma_chan *chan;
  138. struct list_head threads;
  139. };
  140. static bool dmatest_match_channel(struct dmatest_params *params,
  141. struct dma_chan *chan)
  142. {
  143. if (params->channel[0] == '\0')
  144. return true;
  145. return strcmp(dma_chan_name(chan), params->channel) == 0;
  146. }
  147. static bool dmatest_match_device(struct dmatest_params *params,
  148. struct dma_device *device)
  149. {
  150. if (params->device[0] == '\0')
  151. return true;
  152. return strcmp(dev_name(device->dev), params->device) == 0;
  153. }
  154. static unsigned long dmatest_random(void)
  155. {
  156. unsigned long buf;
  157. get_random_bytes(&buf, sizeof(buf));
  158. return buf;
  159. }
  160. static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len,
  161. unsigned int buf_size)
  162. {
  163. unsigned int i;
  164. u8 *buf;
  165. for (; (buf = *bufs); bufs++) {
  166. for (i = 0; i < start; i++)
  167. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  168. for ( ; i < start + len; i++)
  169. buf[i] = PATTERN_SRC | PATTERN_COPY
  170. | (~i & PATTERN_COUNT_MASK);
  171. for ( ; i < buf_size; i++)
  172. buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  173. buf++;
  174. }
  175. }
  176. static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len,
  177. unsigned int buf_size)
  178. {
  179. unsigned int i;
  180. u8 *buf;
  181. for (; (buf = *bufs); bufs++) {
  182. for (i = 0; i < start; i++)
  183. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  184. for ( ; i < start + len; i++)
  185. buf[i] = PATTERN_DST | PATTERN_OVERWRITE
  186. | (~i & PATTERN_COUNT_MASK);
  187. for ( ; i < buf_size; i++)
  188. buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  189. }
  190. }
  191. static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
  192. unsigned int counter, bool is_srcbuf)
  193. {
  194. u8 diff = actual ^ pattern;
  195. u8 expected = pattern | (~counter & PATTERN_COUNT_MASK);
  196. const char *thread_name = current->comm;
  197. if (is_srcbuf)
  198. pr_warn("%s: srcbuf[0x%x] overwritten! Expected %02x, got %02x\n",
  199. thread_name, index, expected, actual);
  200. else if ((pattern & PATTERN_COPY)
  201. && (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
  202. pr_warn("%s: dstbuf[0x%x] not copied! Expected %02x, got %02x\n",
  203. thread_name, index, expected, actual);
  204. else if (diff & PATTERN_SRC)
  205. pr_warn("%s: dstbuf[0x%x] was copied! Expected %02x, got %02x\n",
  206. thread_name, index, expected, actual);
  207. else
  208. pr_warn("%s: dstbuf[0x%x] mismatch! Expected %02x, got %02x\n",
  209. thread_name, index, expected, actual);
  210. }
  211. static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
  212. unsigned int end, unsigned int counter, u8 pattern,
  213. bool is_srcbuf)
  214. {
  215. unsigned int i;
  216. unsigned int error_count = 0;
  217. u8 actual;
  218. u8 expected;
  219. u8 *buf;
  220. unsigned int counter_orig = counter;
  221. for (; (buf = *bufs); bufs++) {
  222. counter = counter_orig;
  223. for (i = start; i < end; i++) {
  224. actual = buf[i];
  225. expected = pattern | (~counter & PATTERN_COUNT_MASK);
  226. if (actual != expected) {
  227. if (error_count < MAX_ERROR_COUNT)
  228. dmatest_mismatch(actual, pattern, i,
  229. counter, is_srcbuf);
  230. error_count++;
  231. }
  232. counter++;
  233. }
  234. }
  235. if (error_count > MAX_ERROR_COUNT)
  236. pr_warn("%s: %u errors suppressed\n",
  237. current->comm, error_count - MAX_ERROR_COUNT);
  238. return error_count;
  239. }
  240. /* poor man's completion - we want to use wait_event_freezable() on it */
  241. struct dmatest_done {
  242. bool done;
  243. wait_queue_head_t *wait;
  244. };
  245. static void dmatest_callback(void *arg)
  246. {
  247. struct dmatest_done *done = arg;
  248. done->done = true;
  249. wake_up_all(done->wait);
  250. }
  251. static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len,
  252. unsigned int count)
  253. {
  254. while (count--)
  255. dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE);
  256. }
  257. static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len,
  258. unsigned int count)
  259. {
  260. while (count--)
  261. dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL);
  262. }
  263. static unsigned int min_odd(unsigned int x, unsigned int y)
  264. {
  265. unsigned int val = min(x, y);
  266. return val % 2 ? val : val - 1;
  267. }
  268. static void result(const char *err, unsigned int n, unsigned int src_off,
  269. unsigned int dst_off, unsigned int len, unsigned long data)
  270. {
  271. pr_info("%s: result #%u: '%s' with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
  272. current->comm, n, err, src_off, dst_off, len, data);
  273. }
  274. static void dbg_result(const char *err, unsigned int n, unsigned int src_off,
  275. unsigned int dst_off, unsigned int len,
  276. unsigned long data)
  277. {
  278. pr_debug("%s: result #%u: '%s' with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)",
  279. current->comm, n, err, src_off, dst_off, len, data);
  280. }
  281. /*
  282. * This function repeatedly tests DMA transfers of various lengths and
  283. * offsets for a given operation type until it is told to exit by
  284. * kthread_stop(). There may be multiple threads running this function
  285. * in parallel for a single channel, and there may be multiple channels
  286. * being tested in parallel.
  287. *
  288. * Before each test, the source and destination buffer is initialized
  289. * with a known pattern. This pattern is different depending on
  290. * whether it's in an area which is supposed to be copied or
  291. * overwritten, and different in the source and destination buffers.
  292. * So if the DMA engine doesn't copy exactly what we tell it to copy,
  293. * we'll notice.
  294. */
  295. static int dmatest_func(void *data)
  296. {
  297. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
  298. struct dmatest_thread *thread = data;
  299. struct dmatest_done done = { .wait = &done_wait };
  300. struct dmatest_info *info;
  301. struct dmatest_params *params;
  302. struct dma_chan *chan;
  303. struct dma_device *dev;
  304. unsigned int src_off, dst_off, len;
  305. unsigned int error_count;
  306. unsigned int failed_tests = 0;
  307. unsigned int total_tests = 0;
  308. dma_cookie_t cookie;
  309. enum dma_status status;
  310. enum dma_ctrl_flags flags;
  311. u8 *pq_coefs = NULL;
  312. int ret;
  313. int src_cnt;
  314. int dst_cnt;
  315. int i;
  316. set_freezable();
  317. ret = -ENOMEM;
  318. smp_rmb();
  319. info = thread->info;
  320. params = &info->params;
  321. chan = thread->chan;
  322. dev = chan->device;
  323. if (thread->type == DMA_MEMCPY)
  324. src_cnt = dst_cnt = 1;
  325. else if (thread->type == DMA_XOR) {
  326. /* force odd to ensure dst = src */
  327. src_cnt = min_odd(params->xor_sources | 1, dev->max_xor);
  328. dst_cnt = 1;
  329. } else if (thread->type == DMA_PQ) {
  330. /* force odd to ensure dst = src */
  331. src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0));
  332. dst_cnt = 2;
  333. pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL);
  334. if (!pq_coefs)
  335. goto err_thread_type;
  336. for (i = 0; i < src_cnt; i++)
  337. pq_coefs[i] = 1;
  338. } else
  339. goto err_thread_type;
  340. thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  341. if (!thread->srcs)
  342. goto err_srcs;
  343. for (i = 0; i < src_cnt; i++) {
  344. thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL);
  345. if (!thread->srcs[i])
  346. goto err_srcbuf;
  347. }
  348. thread->srcs[i] = NULL;
  349. thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  350. if (!thread->dsts)
  351. goto err_dsts;
  352. for (i = 0; i < dst_cnt; i++) {
  353. thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL);
  354. if (!thread->dsts[i])
  355. goto err_dstbuf;
  356. }
  357. thread->dsts[i] = NULL;
  358. set_user_nice(current, 10);
  359. /*
  360. * src and dst buffers are freed by ourselves below
  361. */
  362. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  363. while (!kthread_should_stop()
  364. && !(params->iterations && total_tests >= params->iterations)) {
  365. struct dma_async_tx_descriptor *tx = NULL;
  366. dma_addr_t dma_srcs[src_cnt];
  367. dma_addr_t dma_dsts[dst_cnt];
  368. u8 align = 0;
  369. total_tests++;
  370. /* honor alignment restrictions */
  371. if (thread->type == DMA_MEMCPY)
  372. align = dev->copy_align;
  373. else if (thread->type == DMA_XOR)
  374. align = dev->xor_align;
  375. else if (thread->type == DMA_PQ)
  376. align = dev->pq_align;
  377. if (1 << align > params->buf_size) {
  378. pr_err("%u-byte buffer too small for %d-byte alignment\n",
  379. params->buf_size, 1 << align);
  380. break;
  381. }
  382. len = dmatest_random() % params->buf_size + 1;
  383. len = (len >> align) << align;
  384. if (!len)
  385. len = 1 << align;
  386. src_off = dmatest_random() % (params->buf_size - len + 1);
  387. dst_off = dmatest_random() % (params->buf_size - len + 1);
  388. src_off = (src_off >> align) << align;
  389. dst_off = (dst_off >> align) << align;
  390. dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size);
  391. dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size);
  392. for (i = 0; i < src_cnt; i++) {
  393. u8 *buf = thread->srcs[i] + src_off;
  394. dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  395. DMA_TO_DEVICE);
  396. ret = dma_mapping_error(dev->dev, dma_srcs[i]);
  397. if (ret) {
  398. unmap_src(dev->dev, dma_srcs, len, i);
  399. result("src mapping error", total_tests,
  400. src_off, dst_off, len, ret);
  401. failed_tests++;
  402. continue;
  403. }
  404. }
  405. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  406. for (i = 0; i < dst_cnt; i++) {
  407. dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  408. params->buf_size,
  409. DMA_BIDIRECTIONAL);
  410. ret = dma_mapping_error(dev->dev, dma_dsts[i]);
  411. if (ret) {
  412. unmap_src(dev->dev, dma_srcs, len, src_cnt);
  413. unmap_dst(dev->dev, dma_dsts, params->buf_size,
  414. i);
  415. result("dst mapping error", total_tests,
  416. src_off, dst_off, len, ret);
  417. failed_tests++;
  418. continue;
  419. }
  420. }
  421. if (thread->type == DMA_MEMCPY)
  422. tx = dev->device_prep_dma_memcpy(chan,
  423. dma_dsts[0] + dst_off,
  424. dma_srcs[0], len,
  425. flags);
  426. else if (thread->type == DMA_XOR)
  427. tx = dev->device_prep_dma_xor(chan,
  428. dma_dsts[0] + dst_off,
  429. dma_srcs, src_cnt,
  430. len, flags);
  431. else if (thread->type == DMA_PQ) {
  432. dma_addr_t dma_pq[dst_cnt];
  433. for (i = 0; i < dst_cnt; i++)
  434. dma_pq[i] = dma_dsts[i] + dst_off;
  435. tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
  436. src_cnt, pq_coefs,
  437. len, flags);
  438. }
  439. if (!tx) {
  440. unmap_src(dev->dev, dma_srcs, len, src_cnt);
  441. unmap_dst(dev->dev, dma_dsts, params->buf_size,
  442. dst_cnt);
  443. result("prep error", total_tests, src_off,
  444. dst_off, len, ret);
  445. msleep(100);
  446. failed_tests++;
  447. continue;
  448. }
  449. done.done = false;
  450. tx->callback = dmatest_callback;
  451. tx->callback_param = &done;
  452. cookie = tx->tx_submit(tx);
  453. if (dma_submit_error(cookie)) {
  454. result("submit error", total_tests, src_off,
  455. dst_off, len, ret);
  456. msleep(100);
  457. failed_tests++;
  458. continue;
  459. }
  460. dma_async_issue_pending(chan);
  461. wait_event_freezable_timeout(done_wait, done.done,
  462. msecs_to_jiffies(params->timeout));
  463. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  464. if (!done.done) {
  465. /*
  466. * We're leaving the timed out dma operation with
  467. * dangling pointer to done_wait. To make this
  468. * correct, we'll need to allocate wait_done for
  469. * each test iteration and perform "who's gonna
  470. * free it this time?" dancing. For now, just
  471. * leave it dangling.
  472. */
  473. result("test timed out", total_tests, src_off, dst_off,
  474. len, 0);
  475. failed_tests++;
  476. continue;
  477. } else if (status != DMA_SUCCESS) {
  478. result(status == DMA_ERROR ?
  479. "completion error status" :
  480. "completion busy status", total_tests, src_off,
  481. dst_off, len, ret);
  482. failed_tests++;
  483. continue;
  484. }
  485. /* Unmap by myself */
  486. unmap_src(dev->dev, dma_srcs, len, src_cnt);
  487. unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt);
  488. error_count = 0;
  489. pr_debug("%s: verifying source buffer...\n", current->comm);
  490. error_count += dmatest_verify(thread->srcs, 0, src_off,
  491. 0, PATTERN_SRC, true);
  492. error_count += dmatest_verify(thread->srcs, src_off,
  493. src_off + len, src_off,
  494. PATTERN_SRC | PATTERN_COPY, true);
  495. error_count += dmatest_verify(thread->srcs, src_off + len,
  496. params->buf_size, src_off + len,
  497. PATTERN_SRC, true);
  498. pr_debug("%s: verifying dest buffer...\n", current->comm);
  499. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  500. 0, PATTERN_DST, false);
  501. error_count += dmatest_verify(thread->dsts, dst_off,
  502. dst_off + len, src_off,
  503. PATTERN_SRC | PATTERN_COPY, false);
  504. error_count += dmatest_verify(thread->dsts, dst_off + len,
  505. params->buf_size, dst_off + len,
  506. PATTERN_DST, false);
  507. if (error_count) {
  508. result("data error", total_tests, src_off, dst_off,
  509. len, error_count);
  510. failed_tests++;
  511. } else {
  512. dbg_result("test passed", total_tests, src_off, dst_off,
  513. len, 0);
  514. }
  515. }
  516. ret = 0;
  517. for (i = 0; thread->dsts[i]; i++)
  518. kfree(thread->dsts[i]);
  519. err_dstbuf:
  520. kfree(thread->dsts);
  521. err_dsts:
  522. for (i = 0; thread->srcs[i]; i++)
  523. kfree(thread->srcs[i]);
  524. err_srcbuf:
  525. kfree(thread->srcs);
  526. err_srcs:
  527. kfree(pq_coefs);
  528. err_thread_type:
  529. pr_info("%s: terminating after %u tests, %u failures (status %d)\n",
  530. current->comm, total_tests, failed_tests, ret);
  531. /* terminate all transfers on specified channels */
  532. if (ret)
  533. dmaengine_terminate_all(chan);
  534. thread->done = true;
  535. if (params->iterations > 0)
  536. while (!kthread_should_stop()) {
  537. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
  538. interruptible_sleep_on(&wait_dmatest_exit);
  539. }
  540. return ret;
  541. }
  542. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  543. {
  544. struct dmatest_thread *thread;
  545. struct dmatest_thread *_thread;
  546. int ret;
  547. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  548. ret = kthread_stop(thread->task);
  549. pr_debug("thread %s exited with status %d\n",
  550. thread->task->comm, ret);
  551. list_del(&thread->node);
  552. kfree(thread);
  553. }
  554. /* terminate all transfers on specified channels */
  555. dmaengine_terminate_all(dtc->chan);
  556. kfree(dtc);
  557. }
  558. static int dmatest_add_threads(struct dmatest_info *info,
  559. struct dmatest_chan *dtc, enum dma_transaction_type type)
  560. {
  561. struct dmatest_params *params = &info->params;
  562. struct dmatest_thread *thread;
  563. struct dma_chan *chan = dtc->chan;
  564. char *op;
  565. unsigned int i;
  566. if (type == DMA_MEMCPY)
  567. op = "copy";
  568. else if (type == DMA_XOR)
  569. op = "xor";
  570. else if (type == DMA_PQ)
  571. op = "pq";
  572. else
  573. return -EINVAL;
  574. for (i = 0; i < params->threads_per_chan; i++) {
  575. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  576. if (!thread) {
  577. pr_warn("No memory for %s-%s%u\n",
  578. dma_chan_name(chan), op, i);
  579. break;
  580. }
  581. thread->info = info;
  582. thread->chan = dtc->chan;
  583. thread->type = type;
  584. smp_wmb();
  585. thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  586. dma_chan_name(chan), op, i);
  587. if (IS_ERR(thread->task)) {
  588. pr_warn("Failed to run thread %s-%s%u\n",
  589. dma_chan_name(chan), op, i);
  590. kfree(thread);
  591. break;
  592. }
  593. /* srcbuf and dstbuf are allocated by the thread itself */
  594. list_add_tail(&thread->node, &dtc->threads);
  595. }
  596. return i;
  597. }
  598. static int dmatest_add_channel(struct dmatest_info *info,
  599. struct dma_chan *chan)
  600. {
  601. struct dmatest_chan *dtc;
  602. struct dma_device *dma_dev = chan->device;
  603. unsigned int thread_count = 0;
  604. int cnt;
  605. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  606. if (!dtc) {
  607. pr_warn("No memory for %s\n", dma_chan_name(chan));
  608. return -ENOMEM;
  609. }
  610. dtc->chan = chan;
  611. INIT_LIST_HEAD(&dtc->threads);
  612. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  613. cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
  614. thread_count += cnt > 0 ? cnt : 0;
  615. }
  616. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  617. cnt = dmatest_add_threads(info, dtc, DMA_XOR);
  618. thread_count += cnt > 0 ? cnt : 0;
  619. }
  620. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  621. cnt = dmatest_add_threads(info, dtc, DMA_PQ);
  622. thread_count += cnt > 0 ? cnt : 0;
  623. }
  624. pr_info("Started %u threads using %s\n",
  625. thread_count, dma_chan_name(chan));
  626. list_add_tail(&dtc->node, &info->channels);
  627. info->nr_channels++;
  628. return 0;
  629. }
  630. static bool filter(struct dma_chan *chan, void *param)
  631. {
  632. struct dmatest_params *params = param;
  633. if (!dmatest_match_channel(params, chan) ||
  634. !dmatest_match_device(params, chan->device))
  635. return false;
  636. else
  637. return true;
  638. }
  639. static int run_threaded_test(struct dmatest_info *info)
  640. {
  641. dma_cap_mask_t mask;
  642. struct dma_chan *chan;
  643. struct dmatest_params *params = &info->params;
  644. int err = 0;
  645. /* Copy test parameters */
  646. params->buf_size = test_buf_size;
  647. strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
  648. strlcpy(params->device, strim(test_device), sizeof(params->device));
  649. params->threads_per_chan = threads_per_chan;
  650. params->max_channels = max_channels;
  651. params->iterations = iterations;
  652. params->xor_sources = xor_sources;
  653. params->pq_sources = pq_sources;
  654. params->timeout = timeout;
  655. dma_cap_zero(mask);
  656. dma_cap_set(DMA_MEMCPY, mask);
  657. for (;;) {
  658. chan = dma_request_channel(mask, filter, params);
  659. if (chan) {
  660. err = dmatest_add_channel(info, chan);
  661. if (err) {
  662. dma_release_channel(chan);
  663. break; /* add_channel failed, punt */
  664. }
  665. } else
  666. break; /* no more channels available */
  667. if (params->max_channels &&
  668. info->nr_channels >= params->max_channels)
  669. break; /* we have all we need */
  670. }
  671. return err;
  672. }
  673. static void stop_threaded_test(struct dmatest_info *info)
  674. {
  675. struct dmatest_chan *dtc, *_dtc;
  676. struct dma_chan *chan;
  677. list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
  678. list_del(&dtc->node);
  679. chan = dtc->chan;
  680. dmatest_cleanup_channel(dtc);
  681. pr_debug("dropped channel %s\n", dma_chan_name(chan));
  682. dma_release_channel(chan);
  683. }
  684. info->nr_channels = 0;
  685. }
  686. static int restart_threaded_test(struct dmatest_info *info, bool run)
  687. {
  688. /* we might be called early to set run=, defer running until all
  689. * parameters have been evaluated
  690. */
  691. if (!info->did_init)
  692. return 0;
  693. /* Stop any running test first */
  694. stop_threaded_test(info);
  695. /* Run test with new parameters */
  696. return run_threaded_test(info);
  697. }
  698. static bool is_threaded_test_run(struct dmatest_info *info)
  699. {
  700. struct dmatest_chan *dtc;
  701. list_for_each_entry(dtc, &info->channels, node) {
  702. struct dmatest_thread *thread;
  703. list_for_each_entry(thread, &dtc->threads, node) {
  704. if (!thread->done)
  705. return true;
  706. }
  707. }
  708. return false;
  709. }
  710. static int dmatest_run_get(char *val, const struct kernel_param *kp)
  711. {
  712. struct dmatest_info *info = &test_info;
  713. mutex_lock(&info->lock);
  714. if (is_threaded_test_run(info)) {
  715. dmatest_run = true;
  716. } else {
  717. stop_threaded_test(info);
  718. dmatest_run = false;
  719. }
  720. mutex_unlock(&info->lock);
  721. return param_get_bool(val, kp);
  722. }
  723. static int dmatest_run_set(const char *val, const struct kernel_param *kp)
  724. {
  725. struct dmatest_info *info = &test_info;
  726. int ret;
  727. mutex_lock(&info->lock);
  728. ret = param_set_bool(val, kp);
  729. if (ret) {
  730. mutex_unlock(&info->lock);
  731. return ret;
  732. }
  733. if (is_threaded_test_run(info))
  734. ret = -EBUSY;
  735. else if (dmatest_run)
  736. ret = restart_threaded_test(info, dmatest_run);
  737. mutex_unlock(&info->lock);
  738. return ret;
  739. }
  740. static int __init dmatest_init(void)
  741. {
  742. struct dmatest_info *info = &test_info;
  743. int ret = 0;
  744. if (dmatest_run) {
  745. mutex_lock(&info->lock);
  746. ret = run_threaded_test(info);
  747. mutex_unlock(&info->lock);
  748. }
  749. /* module parameters are stable, inittime tests are started,
  750. * let userspace take over 'run' control
  751. */
  752. info->did_init = true;
  753. return ret;
  754. }
  755. /* when compiled-in wait for drivers to load first */
  756. late_initcall(dmatest_init);
  757. static void __exit dmatest_exit(void)
  758. {
  759. struct dmatest_info *info = &test_info;
  760. mutex_lock(&info->lock);
  761. stop_threaded_test(info);
  762. mutex_unlock(&info->lock);
  763. }
  764. module_exit(dmatest_exit);
  765. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  766. MODULE_LICENSE("GPL v2");