dmabounce.c 15 KB

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