dmabounce.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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
  8. * RAM, the remainder of memory is at the top and the DMA memory
  9. * can be marked as ZONE_DMA. Anything beyond that such as discontiguous
  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. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/device.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/dmapool.h>
  31. #include <linux/list.h>
  32. #include <linux/scatterlist.h>
  33. #include <asm/cacheflush.h>
  34. #undef STATS
  35. #ifdef STATS
  36. #define DO_STATS(X) do { X ; } while (0)
  37. #else
  38. #define DO_STATS(X) do { } while (0)
  39. #endif
  40. /* ************************************************** */
  41. struct safe_buffer {
  42. struct list_head node;
  43. /* original request */
  44. void *ptr;
  45. size_t size;
  46. int direction;
  47. /* safe buffer info */
  48. struct dmabounce_pool *pool;
  49. void *safe;
  50. dma_addr_t safe_dma_addr;
  51. };
  52. struct dmabounce_pool {
  53. unsigned long size;
  54. struct dma_pool *pool;
  55. #ifdef STATS
  56. unsigned long allocs;
  57. #endif
  58. };
  59. struct dmabounce_device_info {
  60. struct device *dev;
  61. struct list_head safe_buffers;
  62. #ifdef STATS
  63. unsigned long total_allocs;
  64. unsigned long map_op_count;
  65. unsigned long bounce_count;
  66. int attr_res;
  67. #endif
  68. struct dmabounce_pool small;
  69. struct dmabounce_pool large;
  70. rwlock_t lock;
  71. int (*needs_bounce)(struct device *, dma_addr_t, size_t);
  72. };
  73. #ifdef STATS
  74. static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
  75. char *buf)
  76. {
  77. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  78. return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
  79. device_info->small.allocs,
  80. device_info->large.allocs,
  81. device_info->total_allocs - device_info->small.allocs -
  82. device_info->large.allocs,
  83. device_info->total_allocs,
  84. device_info->map_op_count,
  85. device_info->bounce_count);
  86. }
  87. static DEVICE_ATTR(dmabounce_stats, 0400, dmabounce_show, NULL);
  88. #endif
  89. /* allocate a 'safe' buffer and keep track of it */
  90. static inline struct safe_buffer *
  91. alloc_safe_buffer(struct dmabounce_device_info *device_info, void *ptr,
  92. size_t size, enum dma_data_direction dir)
  93. {
  94. struct safe_buffer *buf;
  95. struct dmabounce_pool *pool;
  96. struct device *dev = device_info->dev;
  97. unsigned long flags;
  98. dev_dbg(dev, "%s(ptr=%p, size=%d, dir=%d)\n",
  99. __func__, ptr, size, dir);
  100. if (size <= device_info->small.size) {
  101. pool = &device_info->small;
  102. } else if (size <= device_info->large.size) {
  103. pool = &device_info->large;
  104. } else {
  105. pool = NULL;
  106. }
  107. buf = kmalloc(sizeof(struct safe_buffer), GFP_ATOMIC);
  108. if (buf == NULL) {
  109. dev_warn(dev, "%s: kmalloc failed\n", __func__);
  110. return NULL;
  111. }
  112. buf->ptr = ptr;
  113. buf->size = size;
  114. buf->direction = dir;
  115. buf->pool = pool;
  116. if (pool) {
  117. buf->safe = dma_pool_alloc(pool->pool, GFP_ATOMIC,
  118. &buf->safe_dma_addr);
  119. } else {
  120. buf->safe = dma_alloc_coherent(dev, size, &buf->safe_dma_addr,
  121. GFP_ATOMIC);
  122. }
  123. if (buf->safe == NULL) {
  124. dev_warn(dev,
  125. "%s: could not alloc dma memory (size=%d)\n",
  126. __func__, size);
  127. kfree(buf);
  128. return NULL;
  129. }
  130. #ifdef STATS
  131. if (pool)
  132. pool->allocs++;
  133. device_info->total_allocs++;
  134. #endif
  135. write_lock_irqsave(&device_info->lock, flags);
  136. list_add(&buf->node, &device_info->safe_buffers);
  137. write_unlock_irqrestore(&device_info->lock, flags);
  138. return buf;
  139. }
  140. /* determine if a buffer is from our "safe" pool */
  141. static inline struct safe_buffer *
  142. find_safe_buffer(struct dmabounce_device_info *device_info, dma_addr_t safe_dma_addr)
  143. {
  144. struct safe_buffer *b, *rb = NULL;
  145. unsigned long flags;
  146. read_lock_irqsave(&device_info->lock, flags);
  147. list_for_each_entry(b, &device_info->safe_buffers, node)
  148. if (b->safe_dma_addr <= safe_dma_addr &&
  149. b->safe_dma_addr + b->size > safe_dma_addr) {
  150. rb = b;
  151. break;
  152. }
  153. read_unlock_irqrestore(&device_info->lock, flags);
  154. return rb;
  155. }
  156. static inline void
  157. free_safe_buffer(struct dmabounce_device_info *device_info, struct safe_buffer *buf)
  158. {
  159. unsigned long flags;
  160. dev_dbg(device_info->dev, "%s(buf=%p)\n", __func__, buf);
  161. write_lock_irqsave(&device_info->lock, flags);
  162. list_del(&buf->node);
  163. write_unlock_irqrestore(&device_info->lock, flags);
  164. if (buf->pool)
  165. dma_pool_free(buf->pool->pool, buf->safe, buf->safe_dma_addr);
  166. else
  167. dma_free_coherent(device_info->dev, buf->size, buf->safe,
  168. buf->safe_dma_addr);
  169. kfree(buf);
  170. }
  171. /* ************************************************** */
  172. static struct safe_buffer *find_safe_buffer_dev(struct device *dev,
  173. dma_addr_t dma_addr, const char *where)
  174. {
  175. if (!dev || !dev->archdata.dmabounce)
  176. return NULL;
  177. if (dma_mapping_error(dev, dma_addr)) {
  178. dev_err(dev, "Trying to %s invalid mapping\n", where);
  179. return NULL;
  180. }
  181. return find_safe_buffer(dev->archdata.dmabounce, dma_addr);
  182. }
  183. static int needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
  184. {
  185. if (!dev || !dev->archdata.dmabounce)
  186. return 0;
  187. if (dev->dma_mask) {
  188. unsigned long limit, mask = *dev->dma_mask;
  189. limit = (mask + 1) & ~mask;
  190. if (limit && size > limit) {
  191. dev_err(dev, "DMA mapping too big (requested %#x "
  192. "mask %#Lx)\n", size, *dev->dma_mask);
  193. return -E2BIG;
  194. }
  195. /* Figure out if we need to bounce from the DMA mask. */
  196. if ((dma_addr | (dma_addr + size - 1)) & ~mask)
  197. return 1;
  198. }
  199. return !!dev->archdata.dmabounce->needs_bounce(dev, dma_addr, size);
  200. }
  201. static inline dma_addr_t map_single(struct device *dev, void *ptr, size_t size,
  202. enum dma_data_direction dir)
  203. {
  204. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  205. struct safe_buffer *buf;
  206. if (device_info)
  207. DO_STATS ( device_info->map_op_count++ );
  208. buf = alloc_safe_buffer(device_info, ptr, size, dir);
  209. if (buf == NULL) {
  210. dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
  211. __func__, ptr);
  212. return DMA_ERROR_CODE;
  213. }
  214. dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
  215. __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
  216. buf->safe, buf->safe_dma_addr);
  217. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) {
  218. dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
  219. __func__, ptr, buf->safe, size);
  220. memcpy(buf->safe, ptr, size);
  221. }
  222. return buf->safe_dma_addr;
  223. }
  224. static inline void unmap_single(struct device *dev, struct safe_buffer *buf,
  225. size_t size, enum dma_data_direction dir)
  226. {
  227. BUG_ON(buf->size != size);
  228. BUG_ON(buf->direction != dir);
  229. dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
  230. __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
  231. buf->safe, buf->safe_dma_addr);
  232. DO_STATS(dev->archdata.dmabounce->bounce_count++);
  233. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
  234. void *ptr = buf->ptr;
  235. dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
  236. __func__, buf->safe, ptr, size);
  237. memcpy(ptr, buf->safe, size);
  238. /*
  239. * Since we may have written to a page cache page,
  240. * we need to ensure that the data will be coherent
  241. * with user mappings.
  242. */
  243. __cpuc_flush_dcache_area(ptr, size);
  244. }
  245. free_safe_buffer(dev->archdata.dmabounce, buf);
  246. }
  247. /* ************************************************** */
  248. /*
  249. * see if a buffer address is in an 'unsafe' range. if it is
  250. * allocate a 'safe' buffer and copy the unsafe buffer into it.
  251. * substitute the safe buffer for the unsafe one.
  252. * (basically move the buffer from an unsafe area to a safe one)
  253. */
  254. dma_addr_t __dma_map_page(struct device *dev, struct page *page,
  255. unsigned long offset, size_t size, enum dma_data_direction dir)
  256. {
  257. dma_addr_t dma_addr;
  258. int ret;
  259. dev_dbg(dev, "%s(page=%p,off=%#lx,size=%zx,dir=%x)\n",
  260. __func__, page, offset, size, dir);
  261. dma_addr = pfn_to_dma(dev, page_to_pfn(page)) + offset;
  262. ret = needs_bounce(dev, dma_addr, size);
  263. if (ret < 0)
  264. return DMA_ERROR_CODE;
  265. if (ret == 0) {
  266. __dma_page_cpu_to_dev(page, offset, size, dir);
  267. return dma_addr;
  268. }
  269. if (PageHighMem(page)) {
  270. dev_err(dev, "DMA buffer bouncing of HIGHMEM pages is not supported\n");
  271. return DMA_ERROR_CODE;
  272. }
  273. return map_single(dev, page_address(page) + offset, size, dir);
  274. }
  275. EXPORT_SYMBOL(__dma_map_page);
  276. /*
  277. * see if a mapped address was really a "safe" buffer and if so, copy
  278. * the data from the safe buffer back to the unsafe buffer and free up
  279. * the safe buffer. (basically return things back to the way they
  280. * should be)
  281. */
  282. void __dma_unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
  283. enum dma_data_direction dir)
  284. {
  285. struct safe_buffer *buf;
  286. dev_dbg(dev, "%s(dma=%#x,size=%d,dir=%x)\n",
  287. __func__, dma_addr, size, dir);
  288. buf = find_safe_buffer_dev(dev, dma_addr, __func__);
  289. if (!buf) {
  290. __dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, dma_addr)),
  291. dma_addr & ~PAGE_MASK, size, dir);
  292. return;
  293. }
  294. unmap_single(dev, buf, size, dir);
  295. }
  296. EXPORT_SYMBOL(__dma_unmap_page);
  297. int dmabounce_sync_for_cpu(struct device *dev, dma_addr_t addr,
  298. size_t sz, enum dma_data_direction dir)
  299. {
  300. struct safe_buffer *buf;
  301. unsigned long off;
  302. dev_dbg(dev, "%s(dma=%#x,off=%#lx,sz=%zx,dir=%x)\n",
  303. __func__, addr, off, sz, dir);
  304. buf = find_safe_buffer_dev(dev, addr, __func__);
  305. if (!buf)
  306. return 1;
  307. off = addr - buf->safe_dma_addr;
  308. BUG_ON(buf->direction != dir);
  309. dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
  310. __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
  311. buf->safe, buf->safe_dma_addr);
  312. DO_STATS(dev->archdata.dmabounce->bounce_count++);
  313. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
  314. dev_dbg(dev, "%s: copy back safe %p to unsafe %p size %d\n",
  315. __func__, buf->safe + off, buf->ptr + off, sz);
  316. memcpy(buf->ptr + off, buf->safe + off, sz);
  317. }
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(dmabounce_sync_for_cpu);
  321. int dmabounce_sync_for_device(struct device *dev, dma_addr_t addr,
  322. size_t sz, enum dma_data_direction dir)
  323. {
  324. struct safe_buffer *buf;
  325. unsigned long off;
  326. dev_dbg(dev, "%s(dma=%#x,off=%#lx,sz=%zx,dir=%x)\n",
  327. __func__, addr, off, sz, dir);
  328. buf = find_safe_buffer_dev(dev, addr, __func__);
  329. if (!buf)
  330. return 1;
  331. off = addr - buf->safe_dma_addr;
  332. BUG_ON(buf->direction != dir);
  333. dev_dbg(dev, "%s: unsafe buffer %p (dma=%#x) mapped to %p (dma=%#x)\n",
  334. __func__, buf->ptr, virt_to_dma(dev, buf->ptr),
  335. buf->safe, buf->safe_dma_addr);
  336. DO_STATS(dev->archdata.dmabounce->bounce_count++);
  337. if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) {
  338. dev_dbg(dev, "%s: copy out unsafe %p to safe %p, size %d\n",
  339. __func__,buf->ptr + off, buf->safe + off, sz);
  340. memcpy(buf->safe + off, buf->ptr + off, sz);
  341. }
  342. return 0;
  343. }
  344. EXPORT_SYMBOL(dmabounce_sync_for_device);
  345. static int dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev,
  346. const char *name, unsigned long size)
  347. {
  348. pool->size = size;
  349. DO_STATS(pool->allocs = 0);
  350. pool->pool = dma_pool_create(name, dev, size,
  351. 0 /* byte alignment */,
  352. 0 /* no page-crossing issues */);
  353. return pool->pool ? 0 : -ENOMEM;
  354. }
  355. int dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
  356. unsigned long large_buffer_size,
  357. int (*needs_bounce_fn)(struct device *, dma_addr_t, size_t))
  358. {
  359. struct dmabounce_device_info *device_info;
  360. int ret;
  361. device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
  362. if (!device_info) {
  363. dev_err(dev,
  364. "Could not allocated dmabounce_device_info\n");
  365. return -ENOMEM;
  366. }
  367. ret = dmabounce_init_pool(&device_info->small, dev,
  368. "small_dmabounce_pool", small_buffer_size);
  369. if (ret) {
  370. dev_err(dev,
  371. "dmabounce: could not allocate DMA pool for %ld byte objects\n",
  372. small_buffer_size);
  373. goto err_free;
  374. }
  375. if (large_buffer_size) {
  376. ret = dmabounce_init_pool(&device_info->large, dev,
  377. "large_dmabounce_pool",
  378. large_buffer_size);
  379. if (ret) {
  380. dev_err(dev,
  381. "dmabounce: could not allocate DMA pool for %ld byte objects\n",
  382. large_buffer_size);
  383. goto err_destroy;
  384. }
  385. }
  386. device_info->dev = dev;
  387. INIT_LIST_HEAD(&device_info->safe_buffers);
  388. rwlock_init(&device_info->lock);
  389. device_info->needs_bounce = needs_bounce_fn;
  390. #ifdef STATS
  391. device_info->total_allocs = 0;
  392. device_info->map_op_count = 0;
  393. device_info->bounce_count = 0;
  394. device_info->attr_res = device_create_file(dev, &dev_attr_dmabounce_stats);
  395. #endif
  396. dev->archdata.dmabounce = device_info;
  397. dev_info(dev, "dmabounce: registered device\n");
  398. return 0;
  399. err_destroy:
  400. dma_pool_destroy(device_info->small.pool);
  401. err_free:
  402. kfree(device_info);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL(dmabounce_register_dev);
  406. void dmabounce_unregister_dev(struct device *dev)
  407. {
  408. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  409. dev->archdata.dmabounce = NULL;
  410. if (!device_info) {
  411. dev_warn(dev,
  412. "Never registered with dmabounce but attempting"
  413. "to unregister!\n");
  414. return;
  415. }
  416. if (!list_empty(&device_info->safe_buffers)) {
  417. dev_err(dev,
  418. "Removing from dmabounce with pending buffers!\n");
  419. BUG();
  420. }
  421. if (device_info->small.pool)
  422. dma_pool_destroy(device_info->small.pool);
  423. if (device_info->large.pool)
  424. dma_pool_destroy(device_info->large.pool);
  425. #ifdef STATS
  426. if (device_info->attr_res == 0)
  427. device_remove_file(dev, &dev_attr_dmabounce_stats);
  428. #endif
  429. kfree(device_info);
  430. dev_info(dev, "dmabounce: device unregistered\n");
  431. }
  432. EXPORT_SYMBOL(dmabounce_unregister_dev);
  433. MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
  434. MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
  435. MODULE_LICENSE("GPL");