dmatest.c 28 KB

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