dmabounce.c 16 KB

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