dmatest.c 26 KB

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