Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DMA Engine test module |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/dmaengine.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/kthread.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/random.h> |
| 17 | #include <linux/wait.h> |
| 18 | |
| 19 | static unsigned int test_buf_size = 16384; |
| 20 | module_param(test_buf_size, uint, S_IRUGO); |
| 21 | MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer"); |
| 22 | |
Kay Sievers | 06190d8 | 2008-11-11 13:12:33 -0700 | [diff] [blame] | 23 | static char test_channel[20]; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 24 | module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO); |
| 25 | MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)"); |
| 26 | |
Kay Sievers | 06190d8 | 2008-11-11 13:12:33 -0700 | [diff] [blame] | 27 | static char test_device[20]; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 28 | module_param_string(device, test_device, sizeof(test_device), S_IRUGO); |
| 29 | MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)"); |
| 30 | |
| 31 | static unsigned int threads_per_chan = 1; |
| 32 | module_param(threads_per_chan, uint, S_IRUGO); |
| 33 | MODULE_PARM_DESC(threads_per_chan, |
| 34 | "Number of threads to start per channel (default: 1)"); |
| 35 | |
| 36 | static unsigned int max_channels; |
| 37 | module_param(max_channels, uint, S_IRUGO); |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 38 | MODULE_PARM_DESC(max_channels, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 39 | "Maximum number of channels to use (default: all)"); |
| 40 | |
Nicolas Ferre | 0a2ff57d | 2009-07-03 19:26:51 +0200 | [diff] [blame] | 41 | static unsigned int iterations; |
| 42 | module_param(iterations, uint, S_IRUGO); |
| 43 | MODULE_PARM_DESC(iterations, |
| 44 | "Iterations before stopping test (default: infinite)"); |
| 45 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 46 | static unsigned int xor_sources = 3; |
| 47 | module_param(xor_sources, uint, S_IRUGO); |
| 48 | MODULE_PARM_DESC(xor_sources, |
| 49 | "Number of xor source buffers (default: 3)"); |
| 50 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Initialization patterns. All bytes in the source buffer has bit 7 |
| 53 | * set, all bytes in the destination buffer has bit 7 cleared. |
| 54 | * |
| 55 | * Bit 6 is set for all bytes which are to be copied by the DMA |
| 56 | * engine. Bit 5 is set for all bytes which are to be overwritten by |
| 57 | * the DMA engine. |
| 58 | * |
| 59 | * The remaining bits are the inverse of a counter which increments by |
| 60 | * one for each byte address. |
| 61 | */ |
| 62 | #define PATTERN_SRC 0x80 |
| 63 | #define PATTERN_DST 0x00 |
| 64 | #define PATTERN_COPY 0x40 |
| 65 | #define PATTERN_OVERWRITE 0x20 |
| 66 | #define PATTERN_COUNT_MASK 0x1f |
| 67 | |
| 68 | struct dmatest_thread { |
| 69 | struct list_head node; |
| 70 | struct task_struct *task; |
| 71 | struct dma_chan *chan; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 72 | u8 **srcs; |
| 73 | u8 **dsts; |
| 74 | enum dma_transaction_type type; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct dmatest_chan { |
| 78 | struct list_head node; |
| 79 | struct dma_chan *chan; |
| 80 | struct list_head threads; |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * These are protected by dma_list_mutex since they're only used by |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 85 | * the DMA filter function callback |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 86 | */ |
| 87 | static LIST_HEAD(dmatest_channels); |
| 88 | static unsigned int nr_channels; |
| 89 | |
| 90 | static bool dmatest_match_channel(struct dma_chan *chan) |
| 91 | { |
| 92 | if (test_channel[0] == '\0') |
| 93 | return true; |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 94 | return strcmp(dma_chan_name(chan), test_channel) == 0; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static bool dmatest_match_device(struct dma_device *device) |
| 98 | { |
| 99 | if (test_device[0] == '\0') |
| 100 | return true; |
Kay Sievers | 06190d8 | 2008-11-11 13:12:33 -0700 | [diff] [blame] | 101 | return strcmp(dev_name(device->dev), test_device) == 0; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static unsigned long dmatest_random(void) |
| 105 | { |
| 106 | unsigned long buf; |
| 107 | |
| 108 | get_random_bytes(&buf, sizeof(buf)); |
| 109 | return buf; |
| 110 | } |
| 111 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 112 | static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len) |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned int i; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 115 | u8 *buf; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 116 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 117 | for (; (buf = *bufs); bufs++) { |
| 118 | for (i = 0; i < start; i++) |
| 119 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); |
| 120 | for ( ; i < start + len; i++) |
| 121 | buf[i] = PATTERN_SRC | PATTERN_COPY |
Joe Perches | c019894 | 2009-06-28 09:26:21 -0700 | [diff] [blame] | 122 | | (~i & PATTERN_COUNT_MASK); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 123 | for ( ; i < test_buf_size; i++) |
| 124 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); |
| 125 | buf++; |
| 126 | } |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 129 | static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len) |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 130 | { |
| 131 | unsigned int i; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 132 | u8 *buf; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 133 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 134 | for (; (buf = *bufs); bufs++) { |
| 135 | for (i = 0; i < start; i++) |
| 136 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); |
| 137 | for ( ; i < start + len; i++) |
| 138 | buf[i] = PATTERN_DST | PATTERN_OVERWRITE |
| 139 | | (~i & PATTERN_COUNT_MASK); |
| 140 | for ( ; i < test_buf_size; i++) |
| 141 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); |
| 142 | } |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index, |
| 146 | unsigned int counter, bool is_srcbuf) |
| 147 | { |
| 148 | u8 diff = actual ^ pattern; |
| 149 | u8 expected = pattern | (~counter & PATTERN_COUNT_MASK); |
| 150 | const char *thread_name = current->comm; |
| 151 | |
| 152 | if (is_srcbuf) |
| 153 | pr_warning("%s: srcbuf[0x%x] overwritten!" |
| 154 | " Expected %02x, got %02x\n", |
| 155 | thread_name, index, expected, actual); |
| 156 | else if ((pattern & PATTERN_COPY) |
| 157 | && (diff & (PATTERN_COPY | PATTERN_OVERWRITE))) |
| 158 | pr_warning("%s: dstbuf[0x%x] not copied!" |
| 159 | " Expected %02x, got %02x\n", |
| 160 | thread_name, index, expected, actual); |
| 161 | else if (diff & PATTERN_SRC) |
| 162 | pr_warning("%s: dstbuf[0x%x] was copied!" |
| 163 | " Expected %02x, got %02x\n", |
| 164 | thread_name, index, expected, actual); |
| 165 | else |
| 166 | pr_warning("%s: dstbuf[0x%x] mismatch!" |
| 167 | " Expected %02x, got %02x\n", |
| 168 | thread_name, index, expected, actual); |
| 169 | } |
| 170 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 171 | static unsigned int dmatest_verify(u8 **bufs, unsigned int start, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 172 | unsigned int end, unsigned int counter, u8 pattern, |
| 173 | bool is_srcbuf) |
| 174 | { |
| 175 | unsigned int i; |
| 176 | unsigned int error_count = 0; |
| 177 | u8 actual; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 178 | u8 expected; |
| 179 | u8 *buf; |
| 180 | unsigned int counter_orig = counter; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 181 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 182 | for (; (buf = *bufs); bufs++) { |
| 183 | counter = counter_orig; |
| 184 | for (i = start; i < end; i++) { |
| 185 | actual = buf[i]; |
| 186 | expected = pattern | (~counter & PATTERN_COUNT_MASK); |
| 187 | if (actual != expected) { |
| 188 | if (error_count < 32) |
| 189 | dmatest_mismatch(actual, pattern, i, |
| 190 | counter, is_srcbuf); |
| 191 | error_count++; |
| 192 | } |
| 193 | counter++; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 194 | } |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (error_count > 32) |
| 198 | pr_warning("%s: %u errors suppressed\n", |
| 199 | current->comm, error_count - 32); |
| 200 | |
| 201 | return error_count; |
| 202 | } |
| 203 | |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 204 | static void dmatest_callback(void *completion) |
| 205 | { |
| 206 | complete(completion); |
| 207 | } |
| 208 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 209 | /* |
| 210 | * This function repeatedly tests DMA transfers of various lengths and |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 211 | * offsets for a given operation type until it is told to exit by |
| 212 | * kthread_stop(). There may be multiple threads running this function |
| 213 | * in parallel for a single channel, and there may be multiple channels |
| 214 | * being tested in parallel. |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 215 | * |
| 216 | * Before each test, the source and destination buffer is initialized |
| 217 | * with a known pattern. This pattern is different depending on |
| 218 | * whether it's in an area which is supposed to be copied or |
| 219 | * overwritten, and different in the source and destination buffers. |
| 220 | * So if the DMA engine doesn't copy exactly what we tell it to copy, |
| 221 | * we'll notice. |
| 222 | */ |
| 223 | static int dmatest_func(void *data) |
| 224 | { |
| 225 | struct dmatest_thread *thread = data; |
| 226 | struct dma_chan *chan; |
| 227 | const char *thread_name; |
| 228 | unsigned int src_off, dst_off, len; |
| 229 | unsigned int error_count; |
| 230 | unsigned int failed_tests = 0; |
| 231 | unsigned int total_tests = 0; |
| 232 | dma_cookie_t cookie; |
| 233 | enum dma_status status; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 234 | enum dma_ctrl_flags flags; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 235 | int ret; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 236 | int src_cnt; |
| 237 | int dst_cnt; |
| 238 | int i; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 239 | |
| 240 | thread_name = current->comm; |
| 241 | |
| 242 | ret = -ENOMEM; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 243 | |
| 244 | smp_rmb(); |
| 245 | chan = thread->chan; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 246 | if (thread->type == DMA_MEMCPY) |
| 247 | src_cnt = dst_cnt = 1; |
| 248 | else if (thread->type == DMA_XOR) { |
| 249 | src_cnt = xor_sources | 1; /* force odd to ensure dst = src */ |
| 250 | dst_cnt = 1; |
| 251 | } else |
| 252 | goto err_srcs; |
| 253 | |
| 254 | thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL); |
| 255 | if (!thread->srcs) |
| 256 | goto err_srcs; |
| 257 | for (i = 0; i < src_cnt; i++) { |
| 258 | thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL); |
| 259 | if (!thread->srcs[i]) |
| 260 | goto err_srcbuf; |
| 261 | } |
| 262 | thread->srcs[i] = NULL; |
| 263 | |
| 264 | thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL); |
| 265 | if (!thread->dsts) |
| 266 | goto err_dsts; |
| 267 | for (i = 0; i < dst_cnt; i++) { |
| 268 | thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL); |
| 269 | if (!thread->dsts[i]) |
| 270 | goto err_dstbuf; |
| 271 | } |
| 272 | thread->dsts[i] = NULL; |
| 273 | |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 274 | set_user_nice(current, 10); |
| 275 | |
| 276 | flags = DMA_CTRL_ACK | DMA_COMPL_SKIP_DEST_UNMAP | DMA_PREP_INTERRUPT; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 277 | |
Nicolas Ferre | 0a2ff57d | 2009-07-03 19:26:51 +0200 | [diff] [blame] | 278 | while (!kthread_should_stop() |
| 279 | && !(iterations && total_tests >= iterations)) { |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 280 | struct dma_device *dev = chan->device; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 281 | struct dma_async_tx_descriptor *tx = NULL; |
| 282 | dma_addr_t dma_srcs[src_cnt]; |
| 283 | dma_addr_t dma_dsts[dst_cnt]; |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 284 | struct completion cmp; |
| 285 | unsigned long tmo = msecs_to_jiffies(3000); |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 286 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 287 | total_tests++; |
| 288 | |
| 289 | len = dmatest_random() % test_buf_size + 1; |
| 290 | src_off = dmatest_random() % (test_buf_size - len + 1); |
| 291 | dst_off = dmatest_random() % (test_buf_size - len + 1); |
| 292 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 293 | dmatest_init_srcs(thread->srcs, src_off, len); |
| 294 | dmatest_init_dsts(thread->dsts, dst_off, len); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 295 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 296 | for (i = 0; i < src_cnt; i++) { |
| 297 | u8 *buf = thread->srcs[i] + src_off; |
| 298 | |
| 299 | dma_srcs[i] = dma_map_single(dev->dev, buf, len, |
| 300 | DMA_TO_DEVICE); |
| 301 | } |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 302 | /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */ |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 303 | for (i = 0; i < dst_cnt; i++) { |
| 304 | dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i], |
| 305 | test_buf_size, |
| 306 | DMA_BIDIRECTIONAL); |
| 307 | } |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 308 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 309 | if (thread->type == DMA_MEMCPY) |
| 310 | tx = dev->device_prep_dma_memcpy(chan, |
| 311 | dma_dsts[0] + dst_off, |
| 312 | dma_srcs[0], len, |
| 313 | flags); |
| 314 | else if (thread->type == DMA_XOR) |
| 315 | tx = dev->device_prep_dma_xor(chan, |
| 316 | dma_dsts[0] + dst_off, |
| 317 | dma_srcs, xor_sources, |
| 318 | len, flags); |
| 319 | |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 320 | if (!tx) { |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 321 | for (i = 0; i < src_cnt; i++) |
| 322 | dma_unmap_single(dev->dev, dma_srcs[i], len, |
| 323 | DMA_TO_DEVICE); |
| 324 | for (i = 0; i < dst_cnt; i++) |
| 325 | dma_unmap_single(dev->dev, dma_dsts[i], |
| 326 | test_buf_size, |
| 327 | DMA_BIDIRECTIONAL); |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 328 | pr_warning("%s: #%u: prep error with src_off=0x%x " |
| 329 | "dst_off=0x%x len=0x%x\n", |
| 330 | thread_name, total_tests - 1, |
| 331 | src_off, dst_off, len); |
| 332 | msleep(100); |
| 333 | failed_tests++; |
| 334 | continue; |
| 335 | } |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 336 | |
| 337 | init_completion(&cmp); |
| 338 | tx->callback = dmatest_callback; |
| 339 | tx->callback_param = &cmp; |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 340 | cookie = tx->tx_submit(tx); |
| 341 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 342 | if (dma_submit_error(cookie)) { |
| 343 | pr_warning("%s: #%u: submit error %d with src_off=0x%x " |
| 344 | "dst_off=0x%x len=0x%x\n", |
| 345 | thread_name, total_tests - 1, cookie, |
| 346 | src_off, dst_off, len); |
| 347 | msleep(100); |
| 348 | failed_tests++; |
| 349 | continue; |
| 350 | } |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 351 | dma_async_issue_pending(chan); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 352 | |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 353 | tmo = wait_for_completion_timeout(&cmp, tmo); |
| 354 | status = dma_async_is_tx_complete(chan, cookie, NULL, NULL); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 355 | |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 356 | if (tmo == 0) { |
| 357 | pr_warning("%s: #%u: test timed out\n", |
| 358 | thread_name, total_tests - 1); |
| 359 | failed_tests++; |
| 360 | continue; |
| 361 | } else if (status != DMA_SUCCESS) { |
| 362 | pr_warning("%s: #%u: got completion callback," |
| 363 | " but status is \'%s\'\n", |
| 364 | thread_name, total_tests - 1, |
| 365 | status == DMA_ERROR ? "error" : "in progress"); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 366 | failed_tests++; |
| 367 | continue; |
| 368 | } |
Dan Williams | e44e0aa | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 369 | |
Atsushi Nemoto | d86be86 | 2009-01-13 09:22:20 -0700 | [diff] [blame] | 370 | /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */ |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 371 | for (i = 0; i < dst_cnt; i++) |
| 372 | dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size, |
| 373 | DMA_BIDIRECTIONAL); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 374 | |
| 375 | error_count = 0; |
| 376 | |
| 377 | pr_debug("%s: verifying source buffer...\n", thread_name); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 378 | error_count += dmatest_verify(thread->srcs, 0, src_off, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 379 | 0, PATTERN_SRC, true); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 380 | error_count += dmatest_verify(thread->srcs, src_off, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 381 | src_off + len, src_off, |
| 382 | PATTERN_SRC | PATTERN_COPY, true); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 383 | error_count += dmatest_verify(thread->srcs, src_off + len, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 384 | test_buf_size, src_off + len, |
| 385 | PATTERN_SRC, true); |
| 386 | |
| 387 | pr_debug("%s: verifying dest buffer...\n", |
| 388 | thread->task->comm); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 389 | error_count += dmatest_verify(thread->dsts, 0, dst_off, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 390 | 0, PATTERN_DST, false); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 391 | error_count += dmatest_verify(thread->dsts, dst_off, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 392 | dst_off + len, src_off, |
| 393 | PATTERN_SRC | PATTERN_COPY, false); |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 394 | error_count += dmatest_verify(thread->dsts, dst_off + len, |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 395 | test_buf_size, dst_off + len, |
| 396 | PATTERN_DST, false); |
| 397 | |
| 398 | if (error_count) { |
| 399 | pr_warning("%s: #%u: %u errors with " |
| 400 | "src_off=0x%x dst_off=0x%x len=0x%x\n", |
| 401 | thread_name, total_tests - 1, error_count, |
| 402 | src_off, dst_off, len); |
| 403 | failed_tests++; |
| 404 | } else { |
| 405 | pr_debug("%s: #%u: No errors with " |
| 406 | "src_off=0x%x dst_off=0x%x len=0x%x\n", |
| 407 | thread_name, total_tests - 1, |
| 408 | src_off, dst_off, len); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | ret = 0; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 413 | for (i = 0; thread->dsts[i]; i++) |
| 414 | kfree(thread->dsts[i]); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 415 | err_dstbuf: |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 416 | kfree(thread->dsts); |
| 417 | err_dsts: |
| 418 | for (i = 0; thread->srcs[i]; i++) |
| 419 | kfree(thread->srcs[i]); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 420 | err_srcbuf: |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 421 | kfree(thread->srcs); |
| 422 | err_srcs: |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 423 | pr_notice("%s: terminating after %u tests, %u failures (status %d)\n", |
| 424 | thread_name, total_tests, failed_tests, ret); |
Nicolas Ferre | 0a2ff57d | 2009-07-03 19:26:51 +0200 | [diff] [blame] | 425 | |
| 426 | if (iterations > 0) |
| 427 | while (!kthread_should_stop()) { |
| 428 | DECLARE_WAIT_QUEUE_HEAD(wait_dmatest_exit); |
| 429 | interruptible_sleep_on(&wait_dmatest_exit); |
| 430 | } |
| 431 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static void dmatest_cleanup_channel(struct dmatest_chan *dtc) |
| 436 | { |
| 437 | struct dmatest_thread *thread; |
| 438 | struct dmatest_thread *_thread; |
| 439 | int ret; |
| 440 | |
| 441 | list_for_each_entry_safe(thread, _thread, &dtc->threads, node) { |
| 442 | ret = kthread_stop(thread->task); |
| 443 | pr_debug("dmatest: thread %s exited with status %d\n", |
| 444 | thread->task->comm, ret); |
| 445 | list_del(&thread->node); |
| 446 | kfree(thread); |
| 447 | } |
| 448 | kfree(dtc); |
| 449 | } |
| 450 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 451 | static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type) |
| 452 | { |
| 453 | struct dmatest_thread *thread; |
| 454 | struct dma_chan *chan = dtc->chan; |
| 455 | char *op; |
| 456 | unsigned int i; |
| 457 | |
| 458 | if (type == DMA_MEMCPY) |
| 459 | op = "copy"; |
| 460 | else if (type == DMA_XOR) |
| 461 | op = "xor"; |
| 462 | else |
| 463 | return -EINVAL; |
| 464 | |
| 465 | for (i = 0; i < threads_per_chan; i++) { |
| 466 | thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL); |
| 467 | if (!thread) { |
| 468 | pr_warning("dmatest: No memory for %s-%s%u\n", |
| 469 | dma_chan_name(chan), op, i); |
| 470 | |
| 471 | break; |
| 472 | } |
| 473 | thread->chan = dtc->chan; |
| 474 | thread->type = type; |
| 475 | smp_wmb(); |
| 476 | thread->task = kthread_run(dmatest_func, thread, "%s-%s%u", |
| 477 | dma_chan_name(chan), op, i); |
| 478 | if (IS_ERR(thread->task)) { |
| 479 | pr_warning("dmatest: Failed to run thread %s-%s%u\n", |
| 480 | dma_chan_name(chan), op, i); |
| 481 | kfree(thread); |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | /* srcbuf and dstbuf are allocated by the thread itself */ |
| 486 | |
| 487 | list_add_tail(&thread->node, &dtc->threads); |
| 488 | } |
| 489 | |
| 490 | return i; |
| 491 | } |
| 492 | |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 493 | static int dmatest_add_channel(struct dma_chan *chan) |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 494 | { |
| 495 | struct dmatest_chan *dtc; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 496 | struct dma_device *dma_dev = chan->device; |
| 497 | unsigned int thread_count = 0; |
| 498 | unsigned int cnt; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 499 | |
Andrew Morton | 6fdb8bd | 2008-09-19 04:16:23 -0700 | [diff] [blame] | 500 | dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 501 | if (!dtc) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 502 | pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan)); |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 503 | return -ENOMEM; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | dtc->chan = chan; |
| 507 | INIT_LIST_HEAD(&dtc->threads); |
| 508 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 509 | if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { |
| 510 | cnt = dmatest_add_threads(dtc, DMA_MEMCPY); |
Nicolas Ferre | f1aef8b | 2009-07-06 18:19:44 +0200 | [diff] [blame] | 511 | thread_count += cnt > 0 ? cnt : 0; |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 512 | } |
| 513 | if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { |
| 514 | cnt = dmatest_add_threads(dtc, DMA_XOR); |
Nicolas Ferre | f1aef8b | 2009-07-06 18:19:44 +0200 | [diff] [blame] | 515 | thread_count += cnt > 0 ? cnt : 0; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Dan Williams | b54d5cb | 2009-03-25 09:13:25 -0700 | [diff] [blame] | 518 | pr_info("dmatest: Started %u threads using %s\n", |
| 519 | thread_count, dma_chan_name(chan)); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 520 | |
| 521 | list_add_tail(&dtc->node, &dmatest_channels); |
| 522 | nr_channels++; |
| 523 | |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 524 | return 0; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dan Williams | 7dd6025 | 2009-01-06 11:38:19 -0700 | [diff] [blame] | 527 | static bool filter(struct dma_chan *chan, void *param) |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 528 | { |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 529 | if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device)) |
Dan Williams | 7dd6025 | 2009-01-06 11:38:19 -0700 | [diff] [blame] | 530 | return false; |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 531 | else |
Dan Williams | 7dd6025 | 2009-01-06 11:38:19 -0700 | [diff] [blame] | 532 | return true; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 535 | static int __init dmatest_init(void) |
| 536 | { |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 537 | dma_cap_mask_t mask; |
| 538 | struct dma_chan *chan; |
| 539 | int err = 0; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 540 | |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 541 | dma_cap_zero(mask); |
| 542 | dma_cap_set(DMA_MEMCPY, mask); |
| 543 | for (;;) { |
| 544 | chan = dma_request_channel(mask, filter, NULL); |
| 545 | if (chan) { |
| 546 | err = dmatest_add_channel(chan); |
Dan Williams | c56c81a | 2009-04-08 15:08:23 -0700 | [diff] [blame] | 547 | if (err) { |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 548 | dma_release_channel(chan); |
| 549 | break; /* add_channel failed, punt */ |
| 550 | } |
| 551 | } else |
| 552 | break; /* no more channels available */ |
| 553 | if (max_channels && nr_channels >= max_channels) |
| 554 | break; /* we have all we need */ |
| 555 | } |
| 556 | |
| 557 | return err; |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 558 | } |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 559 | /* when compiled-in wait for drivers to load first */ |
| 560 | late_initcall(dmatest_init); |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 561 | |
| 562 | static void __exit dmatest_exit(void) |
| 563 | { |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 564 | struct dmatest_chan *dtc, *_dtc; |
Dan Williams | 7cbd487 | 2009-03-04 16:06:03 -0700 | [diff] [blame] | 565 | struct dma_chan *chan; |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 566 | |
| 567 | list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) { |
| 568 | list_del(&dtc->node); |
Dan Williams | 7cbd487 | 2009-03-04 16:06:03 -0700 | [diff] [blame] | 569 | chan = dtc->chan; |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 570 | dmatest_cleanup_channel(dtc); |
| 571 | pr_debug("dmatest: dropped channel %s\n", |
Dan Williams | 7cbd487 | 2009-03-04 16:06:03 -0700 | [diff] [blame] | 572 | dma_chan_name(chan)); |
| 573 | dma_release_channel(chan); |
Dan Williams | 33df8ca | 2009-01-06 11:38:15 -0700 | [diff] [blame] | 574 | } |
Haavard Skinnemoen | 4a776f0 | 2008-07-08 11:58:45 -0700 | [diff] [blame] | 575 | } |
| 576 | module_exit(dmatest_exit); |
| 577 | |
| 578 | MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>"); |
| 579 | MODULE_LICENSE("GPL v2"); |