dma-coherent.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
  7. * Copyright (C) 2000, 2001 Ralf Baechle <ralf@gnu.org>
  8. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <asm/cache.h>
  16. #include <asm/io.h>
  17. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  18. dma_addr_t * dma_handle, gfp_t gfp)
  19. {
  20. void *ret;
  21. /* ignore region specifiers */
  22. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  23. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  24. gfp |= GFP_DMA;
  25. ret = (void *) __get_free_pages(gfp, get_order(size));
  26. if (ret != NULL) {
  27. memset(ret, 0, size);
  28. *dma_handle = virt_to_phys(ret);
  29. }
  30. return ret;
  31. }
  32. EXPORT_SYMBOL(dma_alloc_noncoherent);
  33. void *dma_alloc_coherent(struct device *dev, size_t size,
  34. dma_addr_t * dma_handle, gfp_t gfp)
  35. __attribute__((alias("dma_alloc_noncoherent")));
  36. EXPORT_SYMBOL(dma_alloc_coherent);
  37. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  38. dma_addr_t dma_handle)
  39. {
  40. unsigned long addr = (unsigned long) vaddr;
  41. free_pages(addr, get_order(size));
  42. }
  43. EXPORT_SYMBOL(dma_free_noncoherent);
  44. void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  45. dma_addr_t dma_handle) __attribute__((alias("dma_free_noncoherent")));
  46. EXPORT_SYMBOL(dma_free_coherent);
  47. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  48. enum dma_data_direction direction)
  49. {
  50. BUG_ON(direction == DMA_NONE);
  51. return __pa(ptr);
  52. }
  53. EXPORT_SYMBOL(dma_map_single);
  54. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  55. enum dma_data_direction direction)
  56. {
  57. BUG_ON(direction == DMA_NONE);
  58. }
  59. EXPORT_SYMBOL(dma_unmap_single);
  60. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  61. enum dma_data_direction direction)
  62. {
  63. int i;
  64. BUG_ON(direction == DMA_NONE);
  65. for (i = 0; i < nents; i++, sg++) {
  66. sg->dma_address = (dma_addr_t)page_to_phys(sg->page) + sg->offset;
  67. }
  68. return nents;
  69. }
  70. EXPORT_SYMBOL(dma_map_sg);
  71. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  72. unsigned long offset, size_t size, enum dma_data_direction direction)
  73. {
  74. BUG_ON(direction == DMA_NONE);
  75. return page_to_phys(page) + offset;
  76. }
  77. EXPORT_SYMBOL(dma_map_page);
  78. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  79. enum dma_data_direction direction)
  80. {
  81. BUG_ON(direction == DMA_NONE);
  82. }
  83. EXPORT_SYMBOL(dma_unmap_page);
  84. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  85. enum dma_data_direction direction)
  86. {
  87. BUG_ON(direction == DMA_NONE);
  88. }
  89. EXPORT_SYMBOL(dma_unmap_sg);
  90. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  91. size_t size, enum dma_data_direction direction)
  92. {
  93. BUG_ON(direction == DMA_NONE);
  94. }
  95. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  96. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  97. size_t size, enum dma_data_direction direction)
  98. {
  99. BUG_ON(direction == DMA_NONE);
  100. }
  101. EXPORT_SYMBOL(dma_sync_single_for_device);
  102. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  103. unsigned long offset, size_t size,
  104. enum dma_data_direction direction)
  105. {
  106. BUG_ON(direction == DMA_NONE);
  107. }
  108. EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
  109. void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  110. unsigned long offset, size_t size,
  111. enum dma_data_direction direction)
  112. {
  113. BUG_ON(direction == DMA_NONE);
  114. }
  115. EXPORT_SYMBOL(dma_sync_single_range_for_device);
  116. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
  117. enum dma_data_direction direction)
  118. {
  119. BUG_ON(direction == DMA_NONE);
  120. }
  121. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  122. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
  123. enum dma_data_direction direction)
  124. {
  125. BUG_ON(direction == DMA_NONE);
  126. }
  127. EXPORT_SYMBOL(dma_sync_sg_for_device);
  128. int dma_mapping_error(dma_addr_t dma_addr)
  129. {
  130. return 0;
  131. }
  132. EXPORT_SYMBOL(dma_mapping_error);
  133. int dma_supported(struct device *dev, u64 mask)
  134. {
  135. /*
  136. * we fall back to GFP_DMA when the mask isn't all 1s,
  137. * so we can't guarantee allocations that must be
  138. * within a tighter range than GFP_DMA..
  139. */
  140. if (mask < 0x00ffffff)
  141. return 0;
  142. return 1;
  143. }
  144. EXPORT_SYMBOL(dma_supported);
  145. int dma_is_consistent(struct device *dev, dma_addr_t dma_addr)
  146. {
  147. return 1;
  148. }
  149. EXPORT_SYMBOL(dma_is_consistent);
  150. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  151. enum dma_data_direction direction)
  152. {
  153. BUG_ON(direction == DMA_NONE);
  154. }
  155. EXPORT_SYMBOL(dma_cache_sync);
  156. /* The DAC routines are a PCIism.. */
  157. #ifdef CONFIG_PCI
  158. #include <linux/pci.h>
  159. dma64_addr_t pci_dac_page_to_dma(struct pci_dev *pdev,
  160. struct page *page, unsigned long offset, int direction)
  161. {
  162. return (dma64_addr_t)page_to_phys(page) + offset;
  163. }
  164. EXPORT_SYMBOL(pci_dac_page_to_dma);
  165. struct page *pci_dac_dma_to_page(struct pci_dev *pdev,
  166. dma64_addr_t dma_addr)
  167. {
  168. return mem_map + (dma_addr >> PAGE_SHIFT);
  169. }
  170. EXPORT_SYMBOL(pci_dac_dma_to_page);
  171. unsigned long pci_dac_dma_to_offset(struct pci_dev *pdev,
  172. dma64_addr_t dma_addr)
  173. {
  174. return dma_addr & ~PAGE_MASK;
  175. }
  176. EXPORT_SYMBOL(pci_dac_dma_to_offset);
  177. void pci_dac_dma_sync_single_for_cpu(struct pci_dev *pdev,
  178. dma64_addr_t dma_addr, size_t len, int direction)
  179. {
  180. BUG_ON(direction == PCI_DMA_NONE);
  181. }
  182. EXPORT_SYMBOL(pci_dac_dma_sync_single_for_cpu);
  183. void pci_dac_dma_sync_single_for_device(struct pci_dev *pdev,
  184. dma64_addr_t dma_addr, size_t len, int direction)
  185. {
  186. BUG_ON(direction == PCI_DMA_NONE);
  187. }
  188. EXPORT_SYMBOL(pci_dac_dma_sync_single_for_device);
  189. #endif /* CONFIG_PCI */