dmatest.c 29 KB

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