dmabounce.c 16 KB

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