dmatest.c 27 KB

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