dmatest.c 27 KB

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