dma-coherent.c 5.7 KB

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