dmatest.c 28 KB

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