dmatest.c 27 KB

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