dma.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2009-2010 PetaLogix
  3. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  4. *
  5. * Provide default implementations of the DMA mapping callbacks for
  6. * directly mapped busses.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/gfp.h>
  11. #include <linux/dma-debug.h>
  12. #include <linux/export.h>
  13. #include <asm/bug.h>
  14. /*
  15. * Generic direct DMA implementation
  16. *
  17. * This implementation supports a per-device offset that can be applied if
  18. * the address at which memory is visible to devices is not 0. Platform code
  19. * can set archdata.dma_data to an unsigned long holding the offset. By
  20. * default the offset is PCI_DRAM_OFFSET.
  21. */
  22. static unsigned long get_dma_direct_offset(struct device *dev)
  23. {
  24. if (likely(dev))
  25. return (unsigned long)dev->archdata.dma_data;
  26. return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
  27. }
  28. #define NOT_COHERENT_CACHE
  29. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  30. dma_addr_t *dma_handle, gfp_t flag)
  31. {
  32. #ifdef NOT_COHERENT_CACHE
  33. return consistent_alloc(flag, size, dma_handle);
  34. #else
  35. void *ret;
  36. struct page *page;
  37. int node = dev_to_node(dev);
  38. /* ignore region specifiers */
  39. flag &= ~(__GFP_HIGHMEM);
  40. page = alloc_pages_node(node, flag, get_order(size));
  41. if (page == NULL)
  42. return NULL;
  43. ret = page_address(page);
  44. memset(ret, 0, size);
  45. *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
  46. return ret;
  47. #endif
  48. }
  49. static void dma_direct_free_coherent(struct device *dev, size_t size,
  50. void *vaddr, dma_addr_t dma_handle)
  51. {
  52. #ifdef NOT_COHERENT_CACHE
  53. consistent_free(size, vaddr);
  54. #else
  55. free_pages((unsigned long)vaddr, get_order(size));
  56. #endif
  57. }
  58. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  59. int nents, enum dma_data_direction direction,
  60. struct dma_attrs *attrs)
  61. {
  62. struct scatterlist *sg;
  63. int i;
  64. /* FIXME this part of code is untested */
  65. for_each_sg(sgl, sg, nents, i) {
  66. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  67. __dma_sync(page_to_phys(sg_page(sg)) + sg->offset,
  68. sg->length, direction);
  69. }
  70. return nents;
  71. }
  72. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  73. int nents, enum dma_data_direction direction,
  74. struct dma_attrs *attrs)
  75. {
  76. }
  77. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  78. {
  79. return 1;
  80. }
  81. static inline dma_addr_t dma_direct_map_page(struct device *dev,
  82. struct page *page,
  83. unsigned long offset,
  84. size_t size,
  85. enum dma_data_direction direction,
  86. struct dma_attrs *attrs)
  87. {
  88. __dma_sync(page_to_phys(page) + offset, size, direction);
  89. return page_to_phys(page) + offset + get_dma_direct_offset(dev);
  90. }
  91. static inline void dma_direct_unmap_page(struct device *dev,
  92. dma_addr_t dma_address,
  93. size_t size,
  94. enum dma_data_direction direction,
  95. struct dma_attrs *attrs)
  96. {
  97. /* There is not necessary to do cache cleanup
  98. *
  99. * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
  100. * dma_address is physical address
  101. */
  102. __dma_sync(dma_address, size, direction);
  103. }
  104. static inline void
  105. dma_direct_sync_single_for_cpu(struct device *dev,
  106. dma_addr_t dma_handle, size_t size,
  107. enum dma_data_direction direction)
  108. {
  109. /*
  110. * It's pointless to flush the cache as the memory segment
  111. * is given to the CPU
  112. */
  113. if (direction == DMA_FROM_DEVICE)
  114. __dma_sync(dma_handle, size, direction);
  115. }
  116. static inline void
  117. dma_direct_sync_single_for_device(struct device *dev,
  118. dma_addr_t dma_handle, size_t size,
  119. enum dma_data_direction direction)
  120. {
  121. /*
  122. * It's pointless to invalidate the cache if the device isn't
  123. * supposed to write to the relevant region
  124. */
  125. if (direction == DMA_TO_DEVICE)
  126. __dma_sync(dma_handle, size, direction);
  127. }
  128. static inline void
  129. dma_direct_sync_sg_for_cpu(struct device *dev,
  130. struct scatterlist *sgl, int nents,
  131. enum dma_data_direction direction)
  132. {
  133. struct scatterlist *sg;
  134. int i;
  135. /* FIXME this part of code is untested */
  136. if (direction == DMA_FROM_DEVICE)
  137. for_each_sg(sgl, sg, nents, i)
  138. __dma_sync(sg->dma_address, sg->length, direction);
  139. }
  140. static inline void
  141. dma_direct_sync_sg_for_device(struct device *dev,
  142. struct scatterlist *sgl, int nents,
  143. enum dma_data_direction direction)
  144. {
  145. struct scatterlist *sg;
  146. int i;
  147. /* FIXME this part of code is untested */
  148. if (direction == DMA_TO_DEVICE)
  149. for_each_sg(sgl, sg, nents, i)
  150. __dma_sync(sg->dma_address, sg->length, direction);
  151. }
  152. struct dma_map_ops dma_direct_ops = {
  153. .alloc_coherent = dma_direct_alloc_coherent,
  154. .free_coherent = dma_direct_free_coherent,
  155. .map_sg = dma_direct_map_sg,
  156. .unmap_sg = dma_direct_unmap_sg,
  157. .dma_supported = dma_direct_dma_supported,
  158. .map_page = dma_direct_map_page,
  159. .unmap_page = dma_direct_unmap_page,
  160. .sync_single_for_cpu = dma_direct_sync_single_for_cpu,
  161. .sync_single_for_device = dma_direct_sync_single_for_device,
  162. .sync_sg_for_cpu = dma_direct_sync_sg_for_cpu,
  163. .sync_sg_for_device = dma_direct_sync_sg_for_device,
  164. };
  165. EXPORT_SYMBOL(dma_direct_ops);
  166. /* Number of entries preallocated for DMA-API debugging */
  167. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  168. static int __init dma_init(void)
  169. {
  170. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  171. return 0;
  172. }
  173. fs_initcall(dma_init);