dmatest.c 28 KB

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