blob: 490e1fe286d5ce164b991125fa0af3454fe3c28c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hovland3a2916a2006-03-22 21:02:11 +00008 * RAM, the remainder of memory is at the top and the DMA memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 King14eb75b2005-06-20 16:56:08 +010033#include <asm/cacheflush.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#undef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#undef STATS
Russell Kingcb7610d2005-10-30 21:12:08 +000037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
46struct 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 Kingcb7610d2005-10-30 21:12:08 +000055 struct dmabounce_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 void *safe;
57 dma_addr_t safe_dma_addr;
58};
59
Russell Kingcb7610d2005-10-30 21:12:08 +000060struct dmabounce_pool {
61 unsigned long size;
62 struct dma_pool *pool;
63#ifdef STATS
64 unsigned long allocs;
65#endif
66};
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068struct dmabounce_device_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct list_head safe_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef STATS
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long total_allocs;
73 unsigned long map_op_count;
74 unsigned long bounce_count;
75#endif
Russell Kingcb7610d2005-10-30 21:12:08 +000076 struct dmabounce_pool small;
77 struct dmabounce_pool large;
Kevin Hilman823588c2006-06-22 22:27:14 +010078
79 rwlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#ifdef STATS
83static 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 Kingcb7610d2005-10-30 21:12:08 +000088 device_info->small.allocs, device_info->large.allocs,
89 device_info->total_allocs - device_info->small.allocs -
90 device_info->large.allocs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 device_info->total_allocs);
92}
93#endif
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* allocate a 'safe' buffer and keep track of it */
97static inline struct safe_buffer *
98alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
Russell Kingcb7610d2005-10-30 21:12:08 +000099 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 struct safe_buffer *buf;
Russell Kingcb7610d2005-10-30 21:12:08 +0000102 struct dmabounce_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct device *dev = device_info->dev;
Kevin Hilman823588c2006-06-22 22:27:14 +0100104 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
107 __func__, ptr, size, dir);
108
Russell Kingcb7610d2005-10-30 21:12:08 +0000109 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 Torvalds1da177e2005-04-16 15:20:36 -0700116
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 Kingcb7610d2005-10-30 21:12:08 +0000123 buf->ptr = ptr;
124 buf->size = size;
125 buf->direction = dir;
126 buf->pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Russell Kingcb7610d2005-10-30 21:12:08 +0000128 if (pool) {
129 buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
130 &buf->safe_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else {
Russell Kingcb7610d2005-10-30 21:12:08 +0000132 buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
133 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
Russell Kingcb7610d2005-10-30 21:12:08 +0000136 if (buf->safe == NULL) {
137 dev_warn(dev,
138 "%s: could not alloc dma memory (size=%d)\n",
139 __func__, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 kfree(buf);
141 return NULL;
142 }
143
144#ifdef STATS
Russell Kingcb7610d2005-10-30 21:12:08 +0000145 if (pool)
146 pool->allocs++;
147 device_info->total_allocs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (device_info->total_allocs % 1000 == 0)
149 print_alloc_stats(device_info);
150#endif
151
Kevin Hilman823588c2006-06-22 22:27:14 +0100152 write_lock_irqsave(&device_info->lock, flags);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 list_add(&buf->node, &device_info->safe_buffers);
155
Kevin Hilman823588c2006-06-22 22:27:14 +0100156 write_unlock_irqrestore(&device_info->lock, flags);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return buf;
159}
160
161/* determine if a buffer is from our "safe" pool */
162static inline struct safe_buffer *
163find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
164{
Kevin Hilmane2785f02006-08-18 15:32:14 +0100165 struct safe_buffer *b, *rb = NULL;
Kevin Hilman823588c2006-06-22 22:27:14 +0100166 unsigned long flags;
167
168 read_lock_irqsave(&device_info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Russell Kingb46a58fd2005-06-22 21:25:58 +0100170 list_for_each_entry(b, &device_info->safe_buffers, node)
Kevin Hilmane2785f02006-08-18 15:32:14 +0100171 if (b->safe_dma_addr == safe_dma_addr) {
172 rb = b;
Kevin Hilman823588c2006-06-22 22:27:14 +0100173 break;
Kevin Hilmane2785f02006-08-18 15:32:14 +0100174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Kevin Hilman823588c2006-06-22 22:27:14 +0100176 read_unlock_irqrestore(&device_info->lock, flags);
Kevin Hilmane2785f02006-08-18 15:32:14 +0100177 return rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static inline void
181free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
182{
Kevin Hilman823588c2006-06-22 22:27:14 +0100183 unsigned long flags;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
186
Kevin Hilman823588c2006-06-22 22:27:14 +0100187 write_lock_irqsave(&device_info->lock, flags);
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 list_del(&buf->node);
190
Kevin Hilman823588c2006-06-22 22:27:14 +0100191 write_unlock_irqrestore(&device_info->lock, flags);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (buf->pool)
Russell Kingcb7610d2005-10-30 21:12:08 +0000194 dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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 Torvalds1da177e2005-04-16 15:20:36 -0700205static void print_map_stats(struct dmabounce_device_info *device_info)
206{
Russell Kingcb7610d2005-10-30 21:12:08 +0000207 dev_info(device_info->dev,
208 "dmabounce: map_op_count=%lu, bounce_count=%lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 device_info->map_op_count, device_info->bounce_count);
210}
211#endif
212
213static inline dma_addr_t
214map_single(struct device *dev, void *ptr, size_t size,
215 enum dma_data_direction dir)
216{
Russell Kingab2c2152007-02-12 10:28:24 +0000217 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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 Kingcb7610d2005-10-30 21:12:08 +0000264 ptr = buf->safe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 dma_addr = buf->safe_dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Russell Kingcb7610d2005-10-30 21:12:08 +0000269 consistent_sync(ptr, size, dir);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return dma_addr;
272}
273
274static inline void
275unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
276 enum dma_data_direction dir)
277{
Russell Kingab2c2152007-02-12 10:28:24 +0000278 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct safe_buffer *buf = NULL;
280
281 /*
282 * Trying to unmap an invalid mapping
283 */
Russell Kingcb7610d2005-10-30 21:12:08 +0000284 if (dma_mapping_error(dma_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 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 Torvalds1da177e2005-04-16 15:20:36 -0700300 DO_STATS ( device_info->bounce_count++ );
301
Russell King5abc1002005-06-20 12:31:14 +0100302 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
303 unsigned long ptr;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 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 King5abc1002005-06-20 12:31:14 +0100309
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 Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 free_safe_buffer(device_info, buf);
322 }
323}
324
325static inline void
326sync_single(struct device *dev, dma_addr_t dma_addr, size_t size,
327 enum dma_data_direction dir)
328{
Russell Kingab2c2152007-02-12 10:28:24 +0000329 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 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 */
397dma_addr_t
398dma_map_single(struct device *dev, void *ptr, size_t size,
399 enum dma_data_direction dir)
400{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 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 Torvalds1da177e2005-04-16 15:20:36 -0700408 dma_addr = map_single(dev, ptr, size, dir);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 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
420void
421dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
422 enum dma_data_direction dir)
423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 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 Torvalds1da177e2005-04-16 15:20:36 -0700429 unmap_single(dev, dma_addr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432int
433dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
434 enum dma_data_direction dir)
435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 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 Torvalds1da177e2005-04-16 15:20:36 -0700443 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 Torvalds1da177e2005-04-16 15:20:36 -0700453 return nents;
454}
455
456void
457dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
458 enum dma_data_direction dir)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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 Torvalds1da177e2005-04-16 15:20:36 -0700467 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 Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475void
476dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size,
477 enum dma_data_direction dir)
478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
480 __func__, (void *) dma_addr, size, dir);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 sync_single(dev, dma_addr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485void
486dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size,
487 enum dma_data_direction dir)
488{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
490 __func__, (void *) dma_addr, size, dir);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 sync_single(dev, dma_addr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495void
496dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
497 enum dma_data_direction dir)
498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 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 Torvalds1da177e2005-04-16 15:20:36 -0700506 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 Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514void
515dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
516 enum dma_data_direction dir)
517{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 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 Torvalds1da177e2005-04-16 15:20:36 -0700525 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 Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Russell Kingcb7610d2005-10-30 21:12:08 +0000533static int
534dmabounce_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 Torvalds1da177e2005-04-16 15:20:36 -0700546int
547dmabounce_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 Kingcb7610d2005-10-30 21:12:08 +0000551 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
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 Kingcb7610d2005-10-30 21:12:08 +0000561 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 Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 if (large_buffer_size) {
Russell Kingcb7610d2005-10-30 21:12:08 +0000571 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 Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 }
581
582 device_info->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 INIT_LIST_HEAD(&device_info->safe_buffers);
Kevin Hilman823588c2006-06-22 22:27:14 +0100584 rwlock_init(&device_info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586#ifdef STATS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 device_info->total_allocs = 0;
588 device_info->map_op_count = 0;
589 device_info->bounce_count = 0;
590#endif
591
Russell Kingab2c2152007-02-12 10:28:24 +0000592 dev->archdata.dmabounce = device_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 printk(KERN_INFO "dmabounce: registered device %s on %s bus\n",
595 dev->bus_id, dev->bus->name);
596
597 return 0;
Russell Kingcb7610d2005-10-30 21:12:08 +0000598
599 err_destroy:
600 dma_pool_destroy(device_info->small.pool);
601 err_free:
602 kfree(device_info);
603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606void
607dmabounce_unregister_dev(struct device *dev)
608{
Russell Kingab2c2152007-02-12 10:28:24 +0000609 struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
610
611 dev->archdata.dmabounce = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
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 Kingcb7610d2005-10-30 21:12:08 +0000627 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 Torvalds1da177e2005-04-16 15:20:36 -0700631
632#ifdef STATS
633 print_alloc_stats(device_info);
634 print_map_stats(device_info);
635#endif
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 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
644EXPORT_SYMBOL(dma_map_single);
645EXPORT_SYMBOL(dma_unmap_single);
646EXPORT_SYMBOL(dma_map_sg);
647EXPORT_SYMBOL(dma_unmap_sg);
Kevin Hilman73218182006-11-02 23:44:24 +0100648EXPORT_SYMBOL(dma_sync_single_for_cpu);
649EXPORT_SYMBOL(dma_sync_single_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650EXPORT_SYMBOL(dma_sync_sg);
651EXPORT_SYMBOL(dmabounce_register_dev);
652EXPORT_SYMBOL(dmabounce_unregister_dev);
653
654MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
655MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
656MODULE_LICENSE("GPL");