dmabounce.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 inline dma_addr_t
  170. map_single(struct device *dev, void *ptr, size_t size,
  171. enum dma_data_direction dir)
  172. {
  173. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  174. dma_addr_t dma_addr;
  175. int needs_bounce = 0;
  176. if (device_info)
  177. DO_STATS ( device_info->map_op_count++ );
  178. dma_addr = virt_to_dma(dev, ptr);
  179. if (dev->dma_mask) {
  180. unsigned long mask = *dev->dma_mask;
  181. unsigned long limit;
  182. limit = (mask + 1) & ~mask;
  183. if (limit && size > limit) {
  184. dev_err(dev, "DMA mapping too big (requested %#x "
  185. "mask %#Lx)\n", size, *dev->dma_mask);
  186. return ~0;
  187. }
  188. /*
  189. * Figure out if we need to bounce from the DMA mask.
  190. */
  191. needs_bounce = (dma_addr | (dma_addr + size - 1)) & ~mask;
  192. }
  193. if (device_info && (needs_bounce || dma_needs_bounce(dev, dma_addr, size))) {
  194. struct safe_buffer *buf;
  195. buf = alloc_safe_buffer(device_info, ptr, size, dir);
  196. if (buf == 0) {
  197. dev_err(dev, "%s: unable to map unsafe buffer %p!\n",
  198. __func__, ptr);
  199. return 0;
  200. }
  201. dev_dbg(dev,
  202. "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
  203. __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
  204. buf->safe, (void *) buf->safe_dma_addr);
  205. if ((dir == DMA_TO_DEVICE) ||
  206. (dir == DMA_BIDIRECTIONAL)) {
  207. dev_dbg(dev, "%s: copy unsafe %p to safe %p, size %d\n",
  208. __func__, ptr, buf->safe, size);
  209. memcpy(buf->safe, ptr, size);
  210. }
  211. ptr = buf->safe;
  212. dma_addr = buf->safe_dma_addr;
  213. } else {
  214. /*
  215. * We don't need to sync the DMA buffer since
  216. * it was allocated via the coherent allocators.
  217. */
  218. dma_cache_maint(ptr, size, dir);
  219. }
  220. return dma_addr;
  221. }
  222. static inline void
  223. unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  224. enum dma_data_direction dir)
  225. {
  226. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  227. struct safe_buffer *buf = NULL;
  228. /*
  229. * Trying to unmap an invalid mapping
  230. */
  231. if (dma_mapping_error(dma_addr)) {
  232. dev_err(dev, "Trying to unmap invalid mapping\n");
  233. return;
  234. }
  235. if (device_info)
  236. buf = find_safe_buffer(device_info, dma_addr);
  237. if (buf) {
  238. BUG_ON(buf->size != size);
  239. dev_dbg(dev,
  240. "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
  241. __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
  242. buf->safe, (void *) buf->safe_dma_addr);
  243. DO_STATS ( device_info->bounce_count++ );
  244. if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) {
  245. void *ptr = buf->ptr;
  246. dev_dbg(dev,
  247. "%s: copy back safe %p to unsafe %p size %d\n",
  248. __func__, buf->safe, ptr, size);
  249. memcpy(ptr, buf->safe, size);
  250. /*
  251. * DMA buffers must have the same cache properties
  252. * as if they were really used for DMA - which means
  253. * data must be written back to RAM. Note that
  254. * we don't use dmac_flush_range() here for the
  255. * bidirectional case because we know the cache
  256. * lines will be coherent with the data written.
  257. */
  258. dmac_clean_range(ptr, ptr + size);
  259. outer_clean_range(__pa(ptr), __pa(ptr) + size);
  260. }
  261. free_safe_buffer(device_info, buf);
  262. }
  263. }
  264. static inline void
  265. sync_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  266. enum dma_data_direction dir)
  267. {
  268. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  269. struct safe_buffer *buf = NULL;
  270. if (device_info)
  271. buf = find_safe_buffer(device_info, dma_addr);
  272. if (buf) {
  273. /*
  274. * Both of these checks from original code need to be
  275. * commented out b/c some drivers rely on the following:
  276. *
  277. * 1) Drivers may map a large chunk of memory into DMA space
  278. * but only sync a small portion of it. Good example is
  279. * allocating a large buffer, mapping it, and then
  280. * breaking it up into small descriptors. No point
  281. * in syncing the whole buffer if you only have to
  282. * touch one descriptor.
  283. *
  284. * 2) Buffers that are mapped as DMA_BIDIRECTIONAL are
  285. * usually only synced in one dir at a time.
  286. *
  287. * See drivers/net/eepro100.c for examples of both cases.
  288. *
  289. * -ds
  290. *
  291. * BUG_ON(buf->size != size);
  292. * BUG_ON(buf->direction != dir);
  293. */
  294. dev_dbg(dev,
  295. "%s: unsafe buffer %p (phy=%p) mapped to %p (phy=%p)\n",
  296. __func__, buf->ptr, (void *) virt_to_dma(dev, buf->ptr),
  297. buf->safe, (void *) buf->safe_dma_addr);
  298. DO_STATS ( device_info->bounce_count++ );
  299. switch (dir) {
  300. case DMA_FROM_DEVICE:
  301. dev_dbg(dev,
  302. "%s: copy back safe %p to unsafe %p size %d\n",
  303. __func__, buf->safe, buf->ptr, size);
  304. memcpy(buf->ptr, buf->safe, size);
  305. break;
  306. case DMA_TO_DEVICE:
  307. dev_dbg(dev,
  308. "%s: copy out unsafe %p to safe %p, size %d\n",
  309. __func__,buf->ptr, buf->safe, size);
  310. memcpy(buf->safe, buf->ptr, size);
  311. break;
  312. case DMA_BIDIRECTIONAL:
  313. BUG(); /* is this allowed? what does it mean? */
  314. default:
  315. BUG();
  316. }
  317. /*
  318. * No need to sync the safe buffer - it was allocated
  319. * via the coherent allocators.
  320. */
  321. } else {
  322. dma_cache_maint(dma_to_virt(dev, dma_addr), size, dir);
  323. }
  324. }
  325. /* ************************************************** */
  326. /*
  327. * see if a buffer address is in an 'unsafe' range. if it is
  328. * allocate a 'safe' buffer and copy the unsafe buffer into it.
  329. * substitute the safe buffer for the unsafe one.
  330. * (basically move the buffer from an unsafe area to a safe one)
  331. */
  332. dma_addr_t
  333. dma_map_single(struct device *dev, void *ptr, size_t size,
  334. enum dma_data_direction dir)
  335. {
  336. dma_addr_t dma_addr;
  337. dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
  338. __func__, ptr, size, dir);
  339. BUG_ON(dir == DMA_NONE);
  340. dma_addr = map_single(dev, ptr, size, dir);
  341. return dma_addr;
  342. }
  343. /*
  344. * see if a mapped address was really a "safe" buffer and if so, copy
  345. * the data from the safe buffer back to the unsafe buffer and free up
  346. * the safe buffer. (basically return things back to the way they
  347. * should be)
  348. */
  349. void
  350. dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  351. enum dma_data_direction dir)
  352. {
  353. dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
  354. __func__, (void *) dma_addr, size, dir);
  355. BUG_ON(dir == DMA_NONE);
  356. unmap_single(dev, dma_addr, size, dir);
  357. }
  358. int
  359. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  360. enum dma_data_direction dir)
  361. {
  362. int i;
  363. dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
  364. __func__, sg, nents, dir);
  365. BUG_ON(dir == DMA_NONE);
  366. for (i = 0; i < nents; i++, sg++) {
  367. struct page *page = sg_page(sg);
  368. unsigned int offset = sg->offset;
  369. unsigned int length = sg->length;
  370. void *ptr = page_address(page) + offset;
  371. sg->dma_address =
  372. map_single(dev, ptr, length, dir);
  373. }
  374. return nents;
  375. }
  376. void
  377. dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  378. enum dma_data_direction dir)
  379. {
  380. int i;
  381. dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
  382. __func__, sg, nents, dir);
  383. BUG_ON(dir == DMA_NONE);
  384. for (i = 0; i < nents; i++, sg++) {
  385. dma_addr_t dma_addr = sg->dma_address;
  386. unsigned int length = sg->length;
  387. unmap_single(dev, dma_addr, length, dir);
  388. }
  389. }
  390. void
  391. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size,
  392. enum dma_data_direction dir)
  393. {
  394. dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
  395. __func__, (void *) dma_addr, size, dir);
  396. sync_single(dev, dma_addr, size, dir);
  397. }
  398. void
  399. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, size_t size,
  400. enum dma_data_direction dir)
  401. {
  402. dev_dbg(dev, "%s(ptr=%p,size=%d,dir=%x)\n",
  403. __func__, (void *) dma_addr, size, dir);
  404. sync_single(dev, dma_addr, size, dir);
  405. }
  406. void
  407. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  408. enum dma_data_direction dir)
  409. {
  410. int i;
  411. dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
  412. __func__, sg, nents, dir);
  413. BUG_ON(dir == DMA_NONE);
  414. for (i = 0; i < nents; i++, sg++) {
  415. dma_addr_t dma_addr = sg->dma_address;
  416. unsigned int length = sg->length;
  417. sync_single(dev, dma_addr, length, dir);
  418. }
  419. }
  420. void
  421. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
  422. enum dma_data_direction dir)
  423. {
  424. int i;
  425. dev_dbg(dev, "%s(sg=%p,nents=%d,dir=%x)\n",
  426. __func__, sg, nents, dir);
  427. BUG_ON(dir == DMA_NONE);
  428. for (i = 0; i < nents; i++, sg++) {
  429. dma_addr_t dma_addr = sg->dma_address;
  430. unsigned int length = sg->length;
  431. sync_single(dev, dma_addr, length, dir);
  432. }
  433. }
  434. static int
  435. dmabounce_init_pool(struct dmabounce_pool *pool, struct device *dev, const char *name,
  436. unsigned long size)
  437. {
  438. pool->size = size;
  439. DO_STATS(pool->allocs = 0);
  440. pool->pool = dma_pool_create(name, dev, size,
  441. 0 /* byte alignment */,
  442. 0 /* no page-crossing issues */);
  443. return pool->pool ? 0 : -ENOMEM;
  444. }
  445. int
  446. dmabounce_register_dev(struct device *dev, unsigned long small_buffer_size,
  447. unsigned long large_buffer_size)
  448. {
  449. struct dmabounce_device_info *device_info;
  450. int ret;
  451. device_info = kmalloc(sizeof(struct dmabounce_device_info), GFP_ATOMIC);
  452. if (!device_info) {
  453. printk(KERN_ERR
  454. "Could not allocated dmabounce_device_info for %s",
  455. dev->bus_id);
  456. return -ENOMEM;
  457. }
  458. ret = dmabounce_init_pool(&device_info->small, dev,
  459. "small_dmabounce_pool", small_buffer_size);
  460. if (ret) {
  461. dev_err(dev,
  462. "dmabounce: could not allocate DMA pool for %ld byte objects\n",
  463. small_buffer_size);
  464. goto err_free;
  465. }
  466. if (large_buffer_size) {
  467. ret = dmabounce_init_pool(&device_info->large, dev,
  468. "large_dmabounce_pool",
  469. large_buffer_size);
  470. if (ret) {
  471. dev_err(dev,
  472. "dmabounce: could not allocate DMA pool for %ld byte objects\n",
  473. large_buffer_size);
  474. goto err_destroy;
  475. }
  476. }
  477. device_info->dev = dev;
  478. INIT_LIST_HEAD(&device_info->safe_buffers);
  479. rwlock_init(&device_info->lock);
  480. #ifdef STATS
  481. device_info->total_allocs = 0;
  482. device_info->map_op_count = 0;
  483. device_info->bounce_count = 0;
  484. device_info->attr_res = device_create_file(dev, &dev_attr_dmabounce_stats);
  485. #endif
  486. dev->archdata.dmabounce = device_info;
  487. printk(KERN_INFO "dmabounce: registered device %s on %s bus\n",
  488. dev->bus_id, dev->bus->name);
  489. return 0;
  490. err_destroy:
  491. dma_pool_destroy(device_info->small.pool);
  492. err_free:
  493. kfree(device_info);
  494. return ret;
  495. }
  496. void
  497. dmabounce_unregister_dev(struct device *dev)
  498. {
  499. struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
  500. dev->archdata.dmabounce = NULL;
  501. if (!device_info) {
  502. printk(KERN_WARNING
  503. "%s: Never registered with dmabounce but attempting" \
  504. "to unregister!\n", dev->bus_id);
  505. return;
  506. }
  507. if (!list_empty(&device_info->safe_buffers)) {
  508. printk(KERN_ERR
  509. "%s: Removing from dmabounce with pending buffers!\n",
  510. dev->bus_id);
  511. BUG();
  512. }
  513. if (device_info->small.pool)
  514. dma_pool_destroy(device_info->small.pool);
  515. if (device_info->large.pool)
  516. dma_pool_destroy(device_info->large.pool);
  517. #ifdef STATS
  518. if (device_info->attr_res == 0)
  519. device_remove_file(dev, &dev_attr_dmabounce_stats);
  520. #endif
  521. kfree(device_info);
  522. printk(KERN_INFO "dmabounce: device %s on %s bus unregistered\n",
  523. dev->bus_id, dev->bus->name);
  524. }
  525. EXPORT_SYMBOL(dma_map_single);
  526. EXPORT_SYMBOL(dma_unmap_single);
  527. EXPORT_SYMBOL(dma_map_sg);
  528. EXPORT_SYMBOL(dma_unmap_sg);
  529. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  530. EXPORT_SYMBOL(dma_sync_single_for_device);
  531. EXPORT_SYMBOL(dma_sync_sg);
  532. EXPORT_SYMBOL(dmabounce_register_dev);
  533. EXPORT_SYMBOL(dmabounce_unregister_dev);
  534. MODULE_AUTHOR("Christopher Hoover <ch@hpl.hp.com>, Deepak Saxena <dsaxena@plexity.net>");
  535. MODULE_DESCRIPTION("Special dma_{map/unmap/dma_sync}_* routines for systems with limited DMA windows");
  536. MODULE_LICENSE("GPL");