dmatest.c 28 KB

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