dmatest.c 28 KB

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