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