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