dmatest.c 26 KB

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