Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/common/dmabounce.c |
| 3 | * |
| 4 | * Special dma_{map/unmap/dma_sync}_* routines for systems that have |
| 5 | * limited DMA windows. These functions utilize bounce buffers to |
| 6 | * copy data to/from buffers located outside the DMA region. This |
| 7 | * only works for systems in which DMA memory is at the bottom of |
Erik Hovland | 3a2916a | 2006-03-22 21:02:11 +0000 | [diff] [blame] | 8 | * RAM, the remainder of memory is at the top and the DMA memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * can be marked as ZONE_DMA. Anything beyond that such as discontigous |
| 10 | * DMA windows will require custom implementations that reserve memory |
| 11 | * areas at early bootup. |
| 12 | * |
| 13 | * Original version by Brad Parker (brad@heeltoe.com) |
| 14 | * Re-written by Christopher Hoover <ch@murgatroid.com> |
| 15 | * Made generic by Deepak Saxena <dsaxena@plexity.net> |
| 16 | * |
| 17 | * Copyright (C) 2002 Hewlett Packard Company. |
| 18 | * Copyright (C) 2004 MontaVista Software, Inc. |
| 19 | * |
| 20 | * This program is free software; you can redistribute it and/or |
| 21 | * modify it under the terms of the GNU General Public License |
| 22 | * version 2 as published by the Free Software Foundation. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | #include <linux/dmapool.h> |
| 31 | #include <linux/list.h> |
| 32 | |
Russell King | 14eb75b | 2005-06-20 16:56:08 +0100 | [diff] [blame] | 33 | #include <asm/cacheflush.h> |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #undef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #undef STATS |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #ifdef STATS |
| 39 | #define DO_STATS(X) do { X ; } while (0) |
| 40 | #else |
| 41 | #define DO_STATS(X) do { } while (0) |
| 42 | #endif |
| 43 | |
| 44 | /* ************************************************** */ |
| 45 | |
| 46 | struct safe_buffer { |
| 47 | struct list_head node; |
| 48 | |
| 49 | /* original request */ |
| 50 | void *ptr; |
| 51 | size_t size; |
| 52 | int direction; |
| 53 | |
| 54 | /* safe buffer info */ |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 55 | struct dmabounce_pool *pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | void *safe; |
| 57 | dma_addr_t safe_dma_addr; |
| 58 | }; |
| 59 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 60 | struct dmabounce_pool { |
| 61 | unsigned long size; |
| 62 | struct dma_pool *pool; |
| 63 | #ifdef STATS |
| 64 | unsigned long allocs; |
| 65 | #endif |
| 66 | }; |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | struct dmabounce_device_info { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct list_head safe_buffers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #ifdef STATS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | unsigned long total_allocs; |
| 73 | unsigned long map_op_count; |
| 74 | unsigned long bounce_count; |
| 75 | #endif |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 76 | struct dmabounce_pool small; |
| 77 | struct dmabounce_pool large; |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 78 | |
| 79 | rwlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #ifdef STATS |
| 83 | static void print_alloc_stats(struct dmabounce_device_info *device_info) |
| 84 | { |
| 85 | printk(KERN_INFO |
| 86 | "%s: dmabounce: sbp: %lu, lbp: %lu, other: %lu, total: %lu\n", |
| 87 | device_info->dev->bus_id, |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 88 | device_info->small.allocs, device_info->large.allocs, |
| 89 | device_info->total_allocs - device_info->small.allocs - |
| 90 | device_info->large.allocs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | device_info->total_allocs); |
| 92 | } |
| 93 | #endif |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* allocate a 'safe' buffer and keep track of it */ |
| 97 | static inline struct safe_buffer * |
| 98 | alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr, |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 99 | size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct safe_buffer *buf; |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 102 | struct dmabounce_pool *pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | struct device *dev = device_info->dev; |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 104 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n", |
| 107 | __func__, ptr, size, dir); |
| 108 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 109 | if (size <= device_info->small.size) { |
| 110 | pool = &device_info->small; |
| 111 | } else if (size <= device_info->large.size) { |
| 112 | pool = &device_info->large; |
| 113 | } else { |
| 114 | pool = NULL; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC); |
| 118 | if (buf == NULL) { |
| 119 | dev_warn(dev, "%s: kmalloc failed\n", __func__); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 123 | buf->ptr = ptr; |
| 124 | buf->size = size; |
| 125 | buf->direction = dir; |
| 126 | buf->pool = pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 128 | if (pool) { |
| 129 | buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC, |
| 130 | &buf->safe_dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } else { |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 132 | buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr, |
| 133 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 136 | if (buf->safe == NULL) { |
| 137 | dev_warn(dev, |
| 138 | "%s: could not alloc dma memory (size=%d)\n", |
| 139 | __func__, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | kfree(buf); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | #ifdef STATS |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 145 | if (pool) |
| 146 | pool->allocs++; |
| 147 | device_info->total_allocs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (device_info->total_allocs % 1000 == 0) |
| 149 | print_alloc_stats(device_info); |
| 150 | #endif |
| 151 | |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 152 | write_lock_irqsave(&device_info->lock, flags); |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | list_add(&buf->node, &device_info->safe_buffers); |
| 155 | |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 156 | write_unlock_irqrestore(&device_info->lock, flags); |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return buf; |
| 159 | } |
| 160 | |
| 161 | /* determine if a buffer is from our "safe" pool */ |
| 162 | static inline struct safe_buffer * |
| 163 | find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr) |
| 164 | { |
Kevin Hilman | e2785f0 | 2006-08-18 15:32:14 +0100 | [diff] [blame] | 165 | struct safe_buffer *b, *rb = NULL; |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 166 | unsigned long flags; |
| 167 | |
| 168 | read_lock_irqsave(&device_info->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Russell King | b46a58fd | 2005-06-22 21:25:58 +0100 | [diff] [blame] | 170 | list_for_each_entry(b, &device_info->safe_buffers, node) |
Kevin Hilman | e2785f0 | 2006-08-18 15:32:14 +0100 | [diff] [blame] | 171 | if (b->safe_dma_addr == safe_dma_addr) { |
| 172 | rb = b; |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 173 | break; |
Kevin Hilman | e2785f0 | 2006-08-18 15:32:14 +0100 | [diff] [blame] | 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 176 | read_unlock_irqrestore(&device_info->lock, flags); |
Kevin Hilman | e2785f0 | 2006-08-18 15:32:14 +0100 | [diff] [blame] | 177 | return rb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static inline void |
| 181 | free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf) |
| 182 | { |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 183 | unsigned long flags; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf); |
| 186 | |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 187 | write_lock_irqsave(&device_info->lock, flags); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | list_del(&buf->node); |
| 190 | |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 191 | write_unlock_irqrestore(&device_info->lock, flags); |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (buf->pool) |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 194 | dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | else |
| 196 | dma_free_coherent(device_info->dev, buf->size, buf->safe, |
| 197 | buf->safe_dma_addr); |
| 198 | |
| 199 | kfree(buf); |
| 200 | } |
| 201 | |
| 202 | /* ************************************************** */ |
| 203 | |
| 204 | #ifdef STATS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static void print_map_stats(struct dmabounce_device_info *device_info) |
| 206 | { |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 207 | dev_info(device_info->dev, |
| 208 | "dmabounce: map_op_count=%lu, bounce_count=%lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | device_info->map_op_count, device_info->bounce_count); |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | static inline dma_addr_t |
| 214 | map_single(struct device *dev, void *ptr, size_t size, |
| 215 | enum dma_data_direction dir) |
| 216 | { |
Russell King | ab2c215 | 2007-02-12 10:28:24 +0000 | [diff] [blame^] | 217 | struct dmabounce_device_info *device_info = dev->archdata.dmabounce; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | dma_addr_t dma_addr; |
| 219 | int needs_bounce = 0; |
| 220 | |
| 221 | if (device_info) |
| 222 | DO_STATS ( device_info->map_op_count++ ); |
| 223 | |
| 224 | dma_addr = virt_to_dma(dev, ptr); |
| 225 | |
| 226 | if (dev->dma_mask) { |
| 227 | unsigned long mask = *dev->dma_mask; |
| 228 | unsigned long limit; |
| 229 | |
| 230 | limit = (mask + 1) & ~mask; |
| 231 | if (limit && size > limit) { |
| 232 | dev_err(dev, "DMA mapping too big (requested %#x " |
| 233 | "mask %#Lx)\n", size, *dev->dma_mask); |
| 234 | return ~0; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Figure out if we need to bounce from the DMA mask. |
| 239 | */ |
| 240 | needs_bounce = (dma_addr | (dma_addr + size - 1)) & ~mask; |
| 241 | } |
| 242 | |
| 243 | if (device_info && (needs_bounce || dma_needs_bounce(dev, dma_addr, size))) { |
| 244 | struct safe_buffer *buf; |
| 245 | |
| 246 | buf = alloc_safe_buffer(device_info, ptr, size, dir); |
| 247 | if (buf == 0) { |
| 248 | dev_err(dev, "%s: unable to map unsafe buffer %p!\n", |
| 249 | __func__, ptr); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | dev_dbg(dev, |
| 254 | "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n", |
| 255 | __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr), |
| 256 | buf->safe, (void *) buf->safe_dma_addr); |
| 257 | |
| 258 | if ((dir == DMA_TO_DEVICE) || |
| 259 | (dir == DMA_BIDIRECTIONAL)) { |
| 260 | dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n", |
| 261 | __func__, ptr, buf->safe, size); |
| 262 | memcpy(buf->safe, ptr, size); |
| 263 | } |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 264 | ptr = buf->safe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | dma_addr = buf->safe_dma_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 269 | consistent_sync(ptr, size, dir); |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return dma_addr; |
| 272 | } |
| 273 | |
| 274 | static inline void |
| 275 | unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 276 | enum dma_data_direction dir) |
| 277 | { |
Russell King | ab2c215 | 2007-02-12 10:28:24 +0000 | [diff] [blame^] | 278 | struct dmabounce_device_info *device_info = dev->archdata.dmabounce; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | struct safe_buffer *buf = NULL; |
| 280 | |
| 281 | /* |
| 282 | * Trying to unmap an invalid mapping |
| 283 | */ |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 284 | if (dma_mapping_error(dma_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | dev_err(dev, "Trying to unmap invalid mapping\n"); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | if (device_info) |
| 290 | buf = find_safe_buffer(device_info, dma_addr); |
| 291 | |
| 292 | if (buf) { |
| 293 | BUG_ON(buf->size != size); |
| 294 | |
| 295 | dev_dbg(dev, |
| 296 | "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n", |
| 297 | __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr), |
| 298 | buf->safe, (void *) buf->safe_dma_addr); |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | DO_STATS ( device_info->bounce_count++ ); |
| 301 | |
Russell King | 5abc100 | 2005-06-20 12:31:14 +0100 | [diff] [blame] | 302 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) { |
| 303 | unsigned long ptr; |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | dev_dbg(dev, |
| 306 | "%s: copy back safe %p to unsafe %p size %d\n", |
| 307 | __func__, buf->safe, buf->ptr, size); |
| 308 | memcpy(buf->ptr, buf->safe, size); |
Russell King | 5abc100 | 2005-06-20 12:31:14 +0100 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * DMA buffers must have the same cache properties |
| 312 | * as if they were really used for DMA - which means |
| 313 | * data must be written back to RAM. Note that |
| 314 | * we don't use dmac_flush_range() here for the |
| 315 | * bidirectional case because we know the cache |
| 316 | * lines will be coherent with the data written. |
| 317 | */ |
| 318 | ptr = (unsigned long)buf->ptr; |
| 319 | dmac_clean_range(ptr, ptr + size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | free_safe_buffer(device_info, buf); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static inline void |
| 326 | sync_single(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 327 | enum dma_data_direction dir) |
| 328 | { |
Russell King | ab2c215 | 2007-02-12 10:28:24 +0000 | [diff] [blame^] | 329 | struct dmabounce_device_info *device_info = dev->archdata.dmabounce; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | struct safe_buffer *buf = NULL; |
| 331 | |
| 332 | if (device_info) |
| 333 | buf = find_safe_buffer(device_info, dma_addr); |
| 334 | |
| 335 | if (buf) { |
| 336 | /* |
| 337 | * Both of these checks from original code need to be |
| 338 | * commented out b/c some drivers rely on the following: |
| 339 | * |
| 340 | * 1) Drivers may map a large chunk of memory into DMA space |
| 341 | * but only sync a small portion of it. Good example is |
| 342 | * allocating a large buffer, mapping it, and then |
| 343 | * breaking it up into small descriptors. No point |
| 344 | * in syncing the whole buffer if you only have to |
| 345 | * touch one descriptor. |
| 346 | * |
| 347 | * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are |
| 348 | * usually only synced in one dir at a time. |
| 349 | * |
| 350 | * See drivers/net/eepro100.c for examples of both cases. |
| 351 | * |
| 352 | * -ds |
| 353 | * |
| 354 | * BUG_ON(buf->size != size); |
| 355 | * BUG_ON(buf->direction != dir); |
| 356 | */ |
| 357 | |
| 358 | dev_dbg(dev, |
| 359 | "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n", |
| 360 | __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr), |
| 361 | buf->safe, (void *) buf->safe_dma_addr); |
| 362 | |
| 363 | DO_STATS ( device_info->bounce_count++ ); |
| 364 | |
| 365 | switch (dir) { |
| 366 | case DMA_FROM_DEVICE: |
| 367 | dev_dbg(dev, |
| 368 | "%s: copy back safe %p to unsafe %p size %d\n", |
| 369 | __func__, buf->safe, buf->ptr, size); |
| 370 | memcpy(buf->ptr, buf->safe, size); |
| 371 | break; |
| 372 | case DMA_TO_DEVICE: |
| 373 | dev_dbg(dev, |
| 374 | "%s: copy out unsafe %p to safe %p, size %d\n", |
| 375 | __func__,buf->ptr, buf->safe, size); |
| 376 | memcpy(buf->safe, buf->ptr, size); |
| 377 | break; |
| 378 | case DMA_BIDIRECTIONAL: |
| 379 | BUG(); /* is this allowed? what does it mean? */ |
| 380 | default: |
| 381 | BUG(); |
| 382 | } |
| 383 | consistent_sync(buf->safe, size, dir); |
| 384 | } else { |
| 385 | consistent_sync(dma_to_virt(dev, dma_addr), size, dir); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* ************************************************** */ |
| 390 | |
| 391 | /* |
| 392 | * see if a buffer address is in an 'unsafe' range. if it is |
| 393 | * allocate a 'safe' buffer and copy the unsafe buffer into it. |
| 394 | * substitute the safe buffer for the unsafe one. |
| 395 | * (basically move the buffer from an unsafe area to a safe one) |
| 396 | */ |
| 397 | dma_addr_t |
| 398 | dma_map_single(struct device *dev, void *ptr, size_t size, |
| 399 | enum dma_data_direction dir) |
| 400 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | dma_addr_t dma_addr; |
| 402 | |
| 403 | dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", |
| 404 | __func__, ptr, size, dir); |
| 405 | |
| 406 | BUG_ON(dir == DMA_NONE); |
| 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | dma_addr = map_single(dev, ptr, size, dir); |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | return dma_addr; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * see if a mapped address was really a "safe" buffer and if so, copy |
| 415 | * the data from the safe buffer back to the unsafe buffer and free up |
| 416 | * the safe buffer. (basically return things back to the way they |
| 417 | * should be) |
| 418 | */ |
| 419 | |
| 420 | void |
| 421 | dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 422 | enum dma_data_direction dir) |
| 423 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", |
| 425 | __func__, (void *) dma_addr, size, dir); |
| 426 | |
| 427 | BUG_ON(dir == DMA_NONE); |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | unmap_single(dev, dma_addr, size, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | int |
| 433 | dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 434 | enum dma_data_direction dir) |
| 435 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | int i; |
| 437 | |
| 438 | dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", |
| 439 | __func__, sg, nents, dir); |
| 440 | |
| 441 | BUG_ON(dir == DMA_NONE); |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | for (i = 0; i < nents; i++, sg++) { |
| 444 | struct page *page = sg->page; |
| 445 | unsigned int offset = sg->offset; |
| 446 | unsigned int length = sg->length; |
| 447 | void *ptr = page_address(page) + offset; |
| 448 | |
| 449 | sg->dma_address = |
| 450 | map_single(dev, ptr, length, dir); |
| 451 | } |
| 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return nents; |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 458 | enum dma_data_direction dir) |
| 459 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | int i; |
| 461 | |
| 462 | dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", |
| 463 | __func__, sg, nents, dir); |
| 464 | |
| 465 | BUG_ON(dir == DMA_NONE); |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | for (i = 0; i < nents; i++, sg++) { |
| 468 | dma_addr_t dma_addr = sg->dma_address; |
| 469 | unsigned int length = sg->length; |
| 470 | |
| 471 | unmap_single(dev, dma_addr, length, dir); |
| 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void |
| 476 | dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 477 | enum dma_data_direction dir) |
| 478 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", |
| 480 | __func__, (void *) dma_addr, size, dir); |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | sync_single(dev, dma_addr, size, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | void |
| 486 | dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 487 | enum dma_data_direction dir) |
| 488 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n", |
| 490 | __func__, (void *) dma_addr, size, dir); |
| 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | sync_single(dev, dma_addr, size, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void |
| 496 | dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents, |
| 497 | enum dma_data_direction dir) |
| 498 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | int i; |
| 500 | |
| 501 | dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", |
| 502 | __func__, sg, nents, dir); |
| 503 | |
| 504 | BUG_ON(dir == DMA_NONE); |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | for (i = 0; i < nents; i++, sg++) { |
| 507 | dma_addr_t dma_addr = sg->dma_address; |
| 508 | unsigned int length = sg->length; |
| 509 | |
| 510 | sync_single(dev, dma_addr, length, dir); |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void |
| 515 | dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents, |
| 516 | enum dma_data_direction dir) |
| 517 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | int i; |
| 519 | |
| 520 | dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n", |
| 521 | __func__, sg, nents, dir); |
| 522 | |
| 523 | BUG_ON(dir == DMA_NONE); |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | for (i = 0; i < nents; i++, sg++) { |
| 526 | dma_addr_t dma_addr = sg->dma_address; |
| 527 | unsigned int length = sg->length; |
| 528 | |
| 529 | sync_single(dev, dma_addr, length, dir); |
| 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 533 | static int |
| 534 | dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev, const char *name, |
| 535 | unsigned long size) |
| 536 | { |
| 537 | pool->size = size; |
| 538 | DO_STATS(pool->allocs = 0); |
| 539 | pool->pool = dma_pool_create(name, dev, size, |
| 540 | 0 /* byte alignment */, |
| 541 | 0 /* no page-crossing issues */); |
| 542 | |
| 543 | return pool->pool ? 0 : -ENOMEM; |
| 544 | } |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | int |
| 547 | dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size, |
| 548 | unsigned long large_buffer_size) |
| 549 | { |
| 550 | struct dmabounce_device_info *device_info; |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 551 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
| 553 | device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC); |
| 554 | if (!device_info) { |
| 555 | printk(KERN_ERR |
| 556 | "Could not allocated dmabounce_device_info for %s", |
| 557 | dev->bus_id); |
| 558 | return -ENOMEM; |
| 559 | } |
| 560 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 561 | ret = dmabounce_init_pool(&device_info->small, dev, |
| 562 | "small_dmabounce_pool", small_buffer_size); |
| 563 | if (ret) { |
| 564 | dev_err(dev, |
| 565 | "dmabounce: could not allocate DMA pool for %ld byte objects\n", |
| 566 | small_buffer_size); |
| 567 | goto err_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | if (large_buffer_size) { |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 571 | ret = dmabounce_init_pool(&device_info->large, dev, |
| 572 | "large_dmabounce_pool", |
| 573 | large_buffer_size); |
| 574 | if (ret) { |
| 575 | dev_err(dev, |
| 576 | "dmabounce: could not allocate DMA pool for %ld byte objects\n", |
| 577 | large_buffer_size); |
| 578 | goto err_destroy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | device_info->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | INIT_LIST_HEAD(&device_info->safe_buffers); |
Kevin Hilman | 823588c | 2006-06-22 22:27:14 +0100 | [diff] [blame] | 584 | rwlock_init(&device_info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | #ifdef STATS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | device_info->total_allocs = 0; |
| 588 | device_info->map_op_count = 0; |
| 589 | device_info->bounce_count = 0; |
| 590 | #endif |
| 591 | |
Russell King | ab2c215 | 2007-02-12 10:28:24 +0000 | [diff] [blame^] | 592 | dev->archdata.dmabounce = device_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | printk(KERN_INFO "dmabounce: registered device %s on %s bus\n", |
| 595 | dev->bus_id, dev->bus->name); |
| 596 | |
| 597 | return 0; |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 598 | |
| 599 | err_destroy: |
| 600 | dma_pool_destroy(device_info->small.pool); |
| 601 | err_free: |
| 602 | kfree(device_info); |
| 603 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | void |
| 607 | dmabounce_unregister_dev(struct device *dev) |
| 608 | { |
Russell King | ab2c215 | 2007-02-12 10:28:24 +0000 | [diff] [blame^] | 609 | struct dmabounce_device_info *device_info = dev->archdata.dmabounce; |
| 610 | |
| 611 | dev->archdata.dmabounce = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | if (!device_info) { |
| 614 | printk(KERN_WARNING |
| 615 | "%s: Never registered with dmabounce but attempting" \ |
| 616 | "to unregister!\n", dev->bus_id); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | if (!list_empty(&device_info->safe_buffers)) { |
| 621 | printk(KERN_ERR |
| 622 | "%s: Removing from dmabounce with pending buffers!\n", |
| 623 | dev->bus_id); |
| 624 | BUG(); |
| 625 | } |
| 626 | |
Russell King | cb7610d | 2005-10-30 21:12:08 +0000 | [diff] [blame] | 627 | if (device_info->small.pool) |
| 628 | dma_pool_destroy(device_info->small.pool); |
| 629 | if (device_info->large.pool) |
| 630 | dma_pool_destroy(device_info->large.pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | #ifdef STATS |
| 633 | print_alloc_stats(device_info); |
| 634 | print_map_stats(device_info); |
| 635 | #endif |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | kfree(device_info); |
| 638 | |
| 639 | printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n", |
| 640 | dev->bus_id, dev->bus->name); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | EXPORT_SYMBOL(dma_map_single); |
| 645 | EXPORT_SYMBOL(dma_unmap_single); |
| 646 | EXPORT_SYMBOL(dma_map_sg); |
| 647 | EXPORT_SYMBOL(dma_unmap_sg); |
Kevin Hilman | 7321818 | 2006-11-02 23:44:24 +0100 | [diff] [blame] | 648 | EXPORT_SYMBOL(dma_sync_single_for_cpu); |
| 649 | EXPORT_SYMBOL(dma_sync_single_for_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | EXPORT_SYMBOL(dma_sync_sg); |
| 651 | EXPORT_SYMBOL(dmabounce_register_dev); |
| 652 | EXPORT_SYMBOL(dmabounce_unregister_dev); |
| 653 | |
| 654 | MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>"); |
| 655 | MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows"); |
| 656 | MODULE_LICENSE("GPL"); |