dma-mapping.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* DMA mapping routines for the MN10300 arch
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_DMA_MAPPING_H
  12. #define _ASM_DMA_MAPPING_H
  13. #include <linux/mm.h>
  14. #include <linux/scatterlist.h>
  15. #include <asm/cache.h>
  16. #include <asm/io.h>
  17. extern void *dma_alloc_coherent(struct device *dev, size_t size,
  18. dma_addr_t *dma_handle, int flag);
  19. extern void dma_free_coherent(struct device *dev, size_t size,
  20. void *vaddr, dma_addr_t dma_handle);
  21. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent((d), (s), (h), (f))
  22. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent((d), (s), (v), (h))
  23. /*
  24. * Map a single buffer of the indicated size for DMA in streaming mode. The
  25. * 32-bit bus address to use is returned.
  26. *
  27. * Once the device is given the dma address, the device owns this memory until
  28. * either pci_unmap_single or pci_dma_sync_single is performed.
  29. */
  30. static inline
  31. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  32. enum dma_data_direction direction)
  33. {
  34. BUG_ON(direction == DMA_NONE);
  35. mn10300_dcache_flush_inv();
  36. return virt_to_bus(ptr);
  37. }
  38. /*
  39. * Unmap a single streaming mode DMA translation. The dma_addr and size must
  40. * match what was provided for in a previous pci_map_single call. All other
  41. * usages are undefined.
  42. *
  43. * After this call, reads by the cpu to the buffer are guarenteed to see
  44. * whatever the device wrote there.
  45. */
  46. static inline
  47. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  48. enum dma_data_direction direction)
  49. {
  50. BUG_ON(direction == DMA_NONE);
  51. }
  52. /*
  53. * Map a set of buffers described by scatterlist in streaming mode for DMA.
  54. * This is the scather-gather version of the above pci_map_single interface.
  55. * Here the scatter gather list elements are each tagged with the appropriate
  56. * dma address and length. They are obtained via sg_dma_{address,length}(SG).
  57. *
  58. * NOTE: An implementation may be able to use a smaller number of DMA
  59. * address/length pairs than there are SG table elements. (for example
  60. * via virtual mapping capabilities) The routine returns the number of
  61. * addr/length pairs actually used, at most nents.
  62. *
  63. * Device ownership issues as mentioned above for pci_map_single are the same
  64. * here.
  65. */
  66. static inline
  67. int dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  68. enum dma_data_direction direction)
  69. {
  70. struct scatterlist *sg;
  71. int i;
  72. BUG_ON(!valid_dma_direction(direction));
  73. WARN_ON(nents == 0 || sglist[0].length == 0);
  74. for_each_sg(sglist, sg, nents, i) {
  75. BUG_ON(!sg_page(sg));
  76. sg->dma_address = sg_phys(sg);
  77. }
  78. mn10300_dcache_flush_inv();
  79. return nents;
  80. }
  81. /*
  82. * Unmap a set of streaming mode DMA translations.
  83. * Again, cpu read rules concerning calls here are the same as for
  84. * pci_unmap_single() above.
  85. */
  86. static inline
  87. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  88. enum dma_data_direction direction)
  89. {
  90. BUG_ON(!valid_dma_direction(direction));
  91. }
  92. /*
  93. * pci_{map,unmap}_single_page maps a kernel page to a dma_addr_t. identical
  94. * to pci_map_single, but takes a struct page instead of a virtual address
  95. */
  96. static inline
  97. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  98. unsigned long offset, size_t size,
  99. enum dma_data_direction direction)
  100. {
  101. BUG_ON(direction == DMA_NONE);
  102. return page_to_bus(page) + offset;
  103. }
  104. static inline
  105. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  106. enum dma_data_direction direction)
  107. {
  108. BUG_ON(direction == DMA_NONE);
  109. }
  110. /*
  111. * Make physical memory consistent for a single streaming mode DMA translation
  112. * after a transfer.
  113. *
  114. * If you perform a pci_map_single() but wish to interrogate the buffer using
  115. * the cpu, yet do not wish to teardown the PCI dma mapping, you must call this
  116. * function before doing so. At the next point you give the PCI dma address
  117. * back to the card, the device again owns the buffer.
  118. */
  119. static inline
  120. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  121. size_t size, enum dma_data_direction direction)
  122. {
  123. }
  124. static inline
  125. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  126. size_t size, enum dma_data_direction direction)
  127. {
  128. mn10300_dcache_flush_inv();
  129. }
  130. static inline
  131. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  132. unsigned long offset, size_t size,
  133. enum dma_data_direction direction)
  134. {
  135. }
  136. static inline void
  137. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  138. unsigned long offset, size_t size,
  139. enum dma_data_direction direction)
  140. {
  141. mn10300_dcache_flush_inv();
  142. }
  143. /*
  144. * Make physical memory consistent for a set of streaming mode DMA translations
  145. * after a transfer.
  146. *
  147. * The same as pci_dma_sync_single but for a scatter-gather list, same rules
  148. * and usage.
  149. */
  150. static inline
  151. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  152. int nelems, enum dma_data_direction direction)
  153. {
  154. }
  155. static inline
  156. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  157. int nelems, enum dma_data_direction direction)
  158. {
  159. mn10300_dcache_flush_inv();
  160. }
  161. static inline
  162. int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  163. {
  164. return 0;
  165. }
  166. /*
  167. * Return whether the given PCI device DMA address mask can be supported
  168. * properly. For example, if your device can only drive the low 24-bits during
  169. * PCI bus mastering, then you would pass 0x00ffffff as the mask to this
  170. * function.
  171. */
  172. static inline
  173. int dma_supported(struct device *dev, u64 mask)
  174. {
  175. /*
  176. * we fall back to GFP_DMA when the mask isn't all 1s, so we can't
  177. * guarantee allocations that must be within a tighter range than
  178. * GFP_DMA
  179. */
  180. if (mask < 0x00ffffff)
  181. return 0;
  182. return 1;
  183. }
  184. static inline
  185. int dma_set_mask(struct device *dev, u64 mask)
  186. {
  187. if (!dev->dma_mask || !dma_supported(dev, mask))
  188. return -EIO;
  189. *dev->dma_mask = mask;
  190. return 0;
  191. }
  192. static inline
  193. int dma_get_cache_alignment(void)
  194. {
  195. return 1 << L1_CACHE_SHIFT;
  196. }
  197. #define dma_is_consistent(d) (1)
  198. static inline
  199. void dma_cache_sync(void *vaddr, size_t size,
  200. enum dma_data_direction direction)
  201. {
  202. mn10300_dcache_flush_inv();
  203. }
  204. #endif