dmatest.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. /* Check if buffer count fits into map count variable (u8) */
  463. if ((src_cnt + dst_cnt) >= 255) {
  464. pr_err("too many buffers (%d of 255 supported)\n",
  465. src_cnt + dst_cnt);
  466. break;
  467. }
  468. if (1 << align > params->buf_size) {
  469. pr_err("%u-byte buffer too small for %d-byte alignment\n",
  470. params->buf_size, 1 << align);
  471. break;
  472. }
  473. if (params->noverify)
  474. len = params->buf_size;
  475. else
  476. len = dmatest_random() % params->buf_size + 1;
  477. len = (len >> align) << align;
  478. if (!len)
  479. len = 1 << align;
  480. total_len += len;
  481. if (params->noverify) {
  482. src_off = 0;
  483. dst_off = 0;
  484. } else {
  485. start = ktime_get();
  486. src_off = dmatest_random() % (params->buf_size - len + 1);
  487. dst_off = dmatest_random() % (params->buf_size - len + 1);
  488. src_off = (src_off >> align) << align;
  489. dst_off = (dst_off >> align) << align;
  490. dmatest_init_srcs(thread->srcs, src_off, len,
  491. params->buf_size);
  492. dmatest_init_dsts(thread->dsts, dst_off, len,
  493. params->buf_size);
  494. diff = ktime_sub(ktime_get(), start);
  495. filltime = ktime_add(filltime, diff);
  496. }
  497. um = dmaengine_get_unmap_data(dev->dev, src_cnt + dst_cnt,
  498. GFP_KERNEL);
  499. if (!um) {
  500. failed_tests++;
  501. result("unmap data NULL", total_tests,
  502. src_off, dst_off, len, ret);
  503. continue;
  504. }
  505. um->len = params->buf_size;
  506. for (i = 0; i < src_cnt; i++) {
  507. void *buf = thread->srcs[i];
  508. struct page *pg = virt_to_page(buf);
  509. unsigned long pg_off = offset_in_page(buf);
  510. um->addr[i] = dma_map_page(dev->dev, pg, pg_off,
  511. um->len, DMA_TO_DEVICE);
  512. srcs[i] = um->addr[i] + src_off;
  513. ret = dma_mapping_error(dev->dev, um->addr[i]);
  514. if (ret) {
  515. dmaengine_unmap_put(um);
  516. result("src mapping error", total_tests,
  517. src_off, dst_off, len, ret);
  518. failed_tests++;
  519. continue;
  520. }
  521. um->to_cnt++;
  522. }
  523. /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
  524. dsts = &um->addr[src_cnt];
  525. for (i = 0; i < dst_cnt; i++) {
  526. void *buf = thread->dsts[i];
  527. struct page *pg = virt_to_page(buf);
  528. unsigned long pg_off = offset_in_page(buf);
  529. dsts[i] = dma_map_page(dev->dev, pg, pg_off, um->len,
  530. DMA_BIDIRECTIONAL);
  531. ret = dma_mapping_error(dev->dev, dsts[i]);
  532. if (ret) {
  533. dmaengine_unmap_put(um);
  534. result("dst mapping error", total_tests,
  535. src_off, dst_off, len, ret);
  536. failed_tests++;
  537. continue;
  538. }
  539. um->bidi_cnt++;
  540. }
  541. sg_init_table(tx_sg, src_cnt);
  542. sg_init_table(rx_sg, src_cnt);
  543. for (i = 0; i < src_cnt; i++) {
  544. sg_dma_address(&rx_sg[i]) = srcs[i];
  545. sg_dma_address(&tx_sg[i]) = dsts[i] + dst_off;
  546. sg_dma_len(&tx_sg[i]) = len;
  547. sg_dma_len(&rx_sg[i]) = len;
  548. }
  549. if (thread->type == DMA_MEMCPY)
  550. tx = dev->device_prep_dma_memcpy(chan,
  551. dsts[0] + dst_off,
  552. srcs[0], len, flags);
  553. else if (thread->type == DMA_SG)
  554. tx = dev->device_prep_dma_sg(chan, tx_sg, src_cnt,
  555. rx_sg, src_cnt, flags);
  556. else if (thread->type == DMA_XOR)
  557. tx = dev->device_prep_dma_xor(chan,
  558. dsts[0] + dst_off,
  559. srcs, src_cnt,
  560. len, flags);
  561. else if (thread->type == DMA_PQ) {
  562. dma_addr_t dma_pq[dst_cnt];
  563. for (i = 0; i < dst_cnt; i++)
  564. dma_pq[i] = dsts[i] + dst_off;
  565. tx = dev->device_prep_dma_pq(chan, dma_pq, srcs,
  566. src_cnt, pq_coefs,
  567. len, flags);
  568. }
  569. if (!tx) {
  570. dmaengine_unmap_put(um);
  571. result("prep error", total_tests, src_off,
  572. dst_off, len, ret);
  573. msleep(100);
  574. failed_tests++;
  575. continue;
  576. }
  577. done.done = false;
  578. tx->callback = dmatest_callback;
  579. tx->callback_param = &done;
  580. cookie = tx->tx_submit(tx);
  581. if (dma_submit_error(cookie)) {
  582. dmaengine_unmap_put(um);
  583. result("submit error", total_tests, src_off,
  584. dst_off, len, ret);
  585. msleep(100);
  586. failed_tests++;
  587. continue;
  588. }
  589. dma_async_issue_pending(chan);
  590. wait_event_freezable_timeout(done_wait, done.done,
  591. msecs_to_jiffies(params->timeout));
  592. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  593. if (!done.done) {
  594. /*
  595. * We're leaving the timed out dma operation with
  596. * dangling pointer to done_wait. To make this
  597. * correct, we'll need to allocate wait_done for
  598. * each test iteration and perform "who's gonna
  599. * free it this time?" dancing. For now, just
  600. * leave it dangling.
  601. */
  602. dmaengine_unmap_put(um);
  603. result("test timed out", total_tests, src_off, dst_off,
  604. len, 0);
  605. failed_tests++;
  606. continue;
  607. } else if (status != DMA_COMPLETE) {
  608. dmaengine_unmap_put(um);
  609. result(status == DMA_ERROR ?
  610. "completion error status" :
  611. "completion busy status", total_tests, src_off,
  612. dst_off, len, ret);
  613. failed_tests++;
  614. continue;
  615. }
  616. dmaengine_unmap_put(um);
  617. if (params->noverify) {
  618. verbose_result("test passed", total_tests, src_off,
  619. dst_off, len, 0);
  620. continue;
  621. }
  622. start = ktime_get();
  623. pr_debug("%s: verifying source buffer...\n", current->comm);
  624. error_count = dmatest_verify(thread->srcs, 0, src_off,
  625. 0, PATTERN_SRC, true);
  626. error_count += dmatest_verify(thread->srcs, src_off,
  627. src_off + len, src_off,
  628. PATTERN_SRC | PATTERN_COPY, true);
  629. error_count += dmatest_verify(thread->srcs, src_off + len,
  630. params->buf_size, src_off + len,
  631. PATTERN_SRC, true);
  632. pr_debug("%s: verifying dest buffer...\n", current->comm);
  633. error_count += dmatest_verify(thread->dsts, 0, dst_off,
  634. 0, PATTERN_DST, false);
  635. error_count += dmatest_verify(thread->dsts, dst_off,
  636. dst_off + len, src_off,
  637. PATTERN_SRC | PATTERN_COPY, false);
  638. error_count += dmatest_verify(thread->dsts, dst_off + len,
  639. params->buf_size, dst_off + len,
  640. PATTERN_DST, false);
  641. diff = ktime_sub(ktime_get(), start);
  642. comparetime = ktime_add(comparetime, diff);
  643. if (error_count) {
  644. result("data error", total_tests, src_off, dst_off,
  645. len, error_count);
  646. failed_tests++;
  647. } else {
  648. verbose_result("test passed", total_tests, src_off,
  649. dst_off, len, 0);
  650. }
  651. }
  652. ktime = ktime_sub(ktime_get(), ktime);
  653. ktime = ktime_sub(ktime, comparetime);
  654. ktime = ktime_sub(ktime, filltime);
  655. runtime = ktime_to_us(ktime);
  656. ret = 0;
  657. err_dstbuf:
  658. for (i = 0; thread->udsts[i]; i++)
  659. kfree(thread->udsts[i]);
  660. kfree(thread->udsts);
  661. err_udsts:
  662. kfree(thread->dsts);
  663. err_dsts:
  664. err_srcbuf:
  665. for (i = 0; thread->usrcs[i]; i++)
  666. kfree(thread->usrcs[i]);
  667. kfree(thread->usrcs);
  668. err_usrcs:
  669. kfree(thread->srcs);
  670. err_srcs:
  671. kfree(pq_coefs);
  672. err_thread_type:
  673. pr_info("%s: summary %u tests, %u failures %llu iops %llu KB/s (%d)\n",
  674. current->comm, total_tests, failed_tests,
  675. dmatest_persec(runtime, total_tests),
  676. dmatest_KBs(runtime, total_len), ret);
  677. /* terminate all transfers on specified channels */
  678. if (ret)
  679. dmaengine_terminate_all(chan);
  680. thread->done = true;
  681. wake_up(&thread_wait);
  682. return ret;
  683. }
  684. static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  685. {
  686. struct dmatest_thread *thread;
  687. struct dmatest_thread *_thread;
  688. int ret;
  689. list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  690. ret = kthread_stop(thread->task);
  691. pr_debug("thread %s exited with status %d\n",
  692. thread->task->comm, ret);
  693. list_del(&thread->node);
  694. put_task_struct(thread->task);
  695. kfree(thread);
  696. }
  697. /* terminate all transfers on specified channels */
  698. dmaengine_terminate_all(dtc->chan);
  699. kfree(dtc);
  700. }
  701. static int dmatest_add_threads(struct dmatest_info *info,
  702. struct dmatest_chan *dtc, enum dma_transaction_type type)
  703. {
  704. struct dmatest_params *params = &info->params;
  705. struct dmatest_thread *thread;
  706. struct dma_chan *chan = dtc->chan;
  707. char *op;
  708. unsigned int i;
  709. if (type == DMA_MEMCPY)
  710. op = "copy";
  711. else if (type == DMA_SG)
  712. op = "sg";
  713. else if (type == DMA_XOR)
  714. op = "xor";
  715. else if (type == DMA_PQ)
  716. op = "pq";
  717. else
  718. return -EINVAL;
  719. for (i = 0; i < params->threads_per_chan; i++) {
  720. thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  721. if (!thread) {
  722. pr_warn("No memory for %s-%s%u\n",
  723. dma_chan_name(chan), op, i);
  724. break;
  725. }
  726. thread->info = info;
  727. thread->chan = dtc->chan;
  728. thread->type = type;
  729. smp_wmb();
  730. thread->task = kthread_create(dmatest_func, thread, "%s-%s%u",
  731. dma_chan_name(chan), op, i);
  732. if (IS_ERR(thread->task)) {
  733. pr_warn("Failed to create thread %s-%s%u\n",
  734. dma_chan_name(chan), op, i);
  735. kfree(thread);
  736. break;
  737. }
  738. /* srcbuf and dstbuf are allocated by the thread itself */
  739. get_task_struct(thread->task);
  740. list_add_tail(&thread->node, &dtc->threads);
  741. wake_up_process(thread->task);
  742. }
  743. return i;
  744. }
  745. static int dmatest_add_channel(struct dmatest_info *info,
  746. struct dma_chan *chan)
  747. {
  748. struct dmatest_chan *dtc;
  749. struct dma_device *dma_dev = chan->device;
  750. unsigned int thread_count = 0;
  751. int cnt;
  752. dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  753. if (!dtc) {
  754. pr_warn("No memory for %s\n", dma_chan_name(chan));
  755. return -ENOMEM;
  756. }
  757. dtc->chan = chan;
  758. INIT_LIST_HEAD(&dtc->threads);
  759. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  760. if (dmatest == 0) {
  761. cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY);
  762. thread_count += cnt > 0 ? cnt : 0;
  763. }
  764. }
  765. if (dma_has_cap(DMA_SG, dma_dev->cap_mask)) {
  766. if (dmatest == 1) {
  767. cnt = dmatest_add_threads(info, dtc, DMA_SG);
  768. thread_count += cnt > 0 ? cnt : 0;
  769. }
  770. }
  771. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  772. cnt = dmatest_add_threads(info, dtc, DMA_XOR);
  773. thread_count += cnt > 0 ? cnt : 0;
  774. }
  775. if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  776. cnt = dmatest_add_threads(info, dtc, DMA_PQ);
  777. thread_count += cnt > 0 ? cnt : 0;
  778. }
  779. pr_info("Started %u threads using %s\n",
  780. thread_count, dma_chan_name(chan));
  781. list_add_tail(&dtc->node, &info->channels);
  782. info->nr_channels++;
  783. return 0;
  784. }
  785. static bool filter(struct dma_chan *chan, void *param)
  786. {
  787. struct dmatest_params *params = param;
  788. if (!dmatest_match_channel(params, chan) ||
  789. !dmatest_match_device(params, chan->device))
  790. return false;
  791. else
  792. return true;
  793. }
  794. static void request_channels(struct dmatest_info *info,
  795. enum dma_transaction_type type)
  796. {
  797. dma_cap_mask_t mask;
  798. dma_cap_zero(mask);
  799. dma_cap_set(type, mask);
  800. for (;;) {
  801. struct dmatest_params *params = &info->params;
  802. struct dma_chan *chan;
  803. chan = dma_request_channel(mask, filter, params);
  804. if (chan) {
  805. if (dmatest_add_channel(info, chan)) {
  806. dma_release_channel(chan);
  807. break; /* add_channel failed, punt */
  808. }
  809. } else
  810. break; /* no more channels available */
  811. if (params->max_channels &&
  812. info->nr_channels >= params->max_channels)
  813. break; /* we have all we need */
  814. }
  815. }
  816. static void run_threaded_test(struct dmatest_info *info)
  817. {
  818. struct dmatest_params *params = &info->params;
  819. /* Copy test parameters */
  820. params->buf_size = test_buf_size;
  821. strlcpy(params->channel, strim(test_channel), sizeof(params->channel));
  822. strlcpy(params->device, strim(test_device), sizeof(params->device));
  823. params->threads_per_chan = threads_per_chan;
  824. params->max_channels = max_channels;
  825. params->iterations = iterations;
  826. params->xor_sources = xor_sources;
  827. params->pq_sources = pq_sources;
  828. params->timeout = timeout;
  829. params->noverify = noverify;
  830. request_channels(info, DMA_MEMCPY);
  831. request_channels(info, DMA_XOR);
  832. request_channels(info, DMA_SG);
  833. request_channels(info, DMA_PQ);
  834. }
  835. static void stop_threaded_test(struct dmatest_info *info)
  836. {
  837. struct dmatest_chan *dtc, *_dtc;
  838. struct dma_chan *chan;
  839. list_for_each_entry_safe(dtc, _dtc, &info->channels, node) {
  840. list_del(&dtc->node);
  841. chan = dtc->chan;
  842. dmatest_cleanup_channel(dtc);
  843. pr_debug("dropped channel %s\n", dma_chan_name(chan));
  844. dma_release_channel(chan);
  845. }
  846. info->nr_channels = 0;
  847. }
  848. static void restart_threaded_test(struct dmatest_info *info, bool run)
  849. {
  850. /* we might be called early to set run=, defer running until all
  851. * parameters have been evaluated
  852. */
  853. if (!info->did_init)
  854. return;
  855. /* Stop any running test first */
  856. stop_threaded_test(info);
  857. /* Run test with new parameters */
  858. run_threaded_test(info);
  859. }
  860. static int dmatest_run_get(char *val, const struct kernel_param *kp)
  861. {
  862. struct dmatest_info *info = &test_info;
  863. mutex_lock(&info->lock);
  864. if (is_threaded_test_run(info)) {
  865. dmatest_run = true;
  866. } else {
  867. stop_threaded_test(info);
  868. dmatest_run = false;
  869. }
  870. mutex_unlock(&info->lock);
  871. return param_get_bool(val, kp);
  872. }
  873. static int dmatest_run_set(const char *val, const struct kernel_param *kp)
  874. {
  875. struct dmatest_info *info = &test_info;
  876. int ret;
  877. mutex_lock(&info->lock);
  878. ret = param_set_bool(val, kp);
  879. if (ret) {
  880. mutex_unlock(&info->lock);
  881. return ret;
  882. }
  883. if (is_threaded_test_run(info))
  884. ret = -EBUSY;
  885. else if (dmatest_run)
  886. restart_threaded_test(info, dmatest_run);
  887. mutex_unlock(&info->lock);
  888. return ret;
  889. }
  890. static int __init dmatest_init(void)
  891. {
  892. struct dmatest_info *info = &test_info;
  893. struct dmatest_params *params = &info->params;
  894. if (dmatest_run) {
  895. mutex_lock(&info->lock);
  896. run_threaded_test(info);
  897. mutex_unlock(&info->lock);
  898. }
  899. if (params->iterations && wait)
  900. wait_event(thread_wait, !is_threaded_test_run(info));
  901. /* module parameters are stable, inittime tests are started,
  902. * let userspace take over 'run' control
  903. */
  904. info->did_init = true;
  905. return 0;
  906. }
  907. /* when compiled-in wait for drivers to load first */
  908. late_initcall(dmatest_init);
  909. static void __exit dmatest_exit(void)
  910. {
  911. struct dmatest_info *info = &test_info;
  912. mutex_lock(&info->lock);
  913. stop_threaded_test(info);
  914. mutex_unlock(&info->lock);
  915. }
  916. module_exit(dmatest_exit);
  917. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  918. MODULE_LICENSE("GPL v2");