dmatest.c 28 KB

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