dma.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. struct dma_attrs *attrs)
  32. {
  33. #ifdef NOT_COHERENT_CACHE
  34. return consistent_alloc(flag, size, dma_handle);
  35. #else
  36. void *ret;
  37. struct page *page;
  38. int node = dev_to_node(dev);
  39. /* ignore region specifiers */
  40. flag &= ~(__GFP_HIGHMEM);
  41. page = alloc_pages_node(node, flag, get_order(size));
  42. if (page == NULL)
  43. return NULL;
  44. ret = page_address(page);
  45. memset(ret, 0, size);
  46. *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
  47. return ret;
  48. #endif
  49. }
  50. static void dma_direct_free_coherent(struct device *dev, size_t size,
  51. void *vaddr, dma_addr_t dma_handle,
  52. struct dma_attrs *attrs)
  53. {
  54. #ifdef NOT_COHERENT_CACHE
  55. consistent_free(size, vaddr);
  56. #else
  57. free_pages((unsigned long)vaddr, get_order(size));
  58. #endif
  59. }
  60. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  61. int nents, enum dma_data_direction direction,
  62. struct dma_attrs *attrs)
  63. {
  64. struct scatterlist *sg;
  65. int i;
  66. /* FIXME this part of code is untested */
  67. for_each_sg(sgl, sg, nents, i) {
  68. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  69. __dma_sync(page_to_phys(sg_page(sg)) + sg->offset,
  70. sg->length, direction);
  71. }
  72. return nents;
  73. }
  74. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  75. int nents, enum dma_data_direction direction,
  76. struct dma_attrs *attrs)
  77. {
  78. }
  79. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  80. {
  81. return 1;
  82. }
  83. static inline dma_addr_t dma_direct_map_page(struct device *dev,
  84. struct page *page,
  85. unsigned long offset,
  86. size_t size,
  87. enum dma_data_direction direction,
  88. struct dma_attrs *attrs)
  89. {
  90. __dma_sync(page_to_phys(page) + offset, size, direction);
  91. return page_to_phys(page) + offset + get_dma_direct_offset(dev);
  92. }
  93. static inline void dma_direct_unmap_page(struct device *dev,
  94. dma_addr_t dma_address,
  95. size_t size,
  96. enum dma_data_direction direction,
  97. struct dma_attrs *attrs)
  98. {
  99. /* There is not necessary to do cache cleanup
  100. *
  101. * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
  102. * dma_address is physical address
  103. */
  104. __dma_sync(dma_address, size, direction);
  105. }
  106. static inline void
  107. dma_direct_sync_single_for_cpu(struct device *dev,
  108. dma_addr_t dma_handle, size_t size,
  109. enum dma_data_direction direction)
  110. {
  111. /*
  112. * It's pointless to flush the cache as the memory segment
  113. * is given to the CPU
  114. */
  115. if (direction == DMA_FROM_DEVICE)
  116. __dma_sync(dma_handle, size, direction);
  117. }
  118. static inline void
  119. dma_direct_sync_single_for_device(struct device *dev,
  120. dma_addr_t dma_handle, size_t size,
  121. enum dma_data_direction direction)
  122. {
  123. /*
  124. * It's pointless to invalidate the cache if the device isn't
  125. * supposed to write to the relevant region
  126. */
  127. if (direction == DMA_TO_DEVICE)
  128. __dma_sync(dma_handle, size, direction);
  129. }
  130. static inline void
  131. dma_direct_sync_sg_for_cpu(struct device *dev,
  132. struct scatterlist *sgl, int nents,
  133. enum dma_data_direction direction)
  134. {
  135. struct scatterlist *sg;
  136. int i;
  137. /* FIXME this part of code is untested */
  138. if (direction == DMA_FROM_DEVICE)
  139. for_each_sg(sgl, sg, nents, i)
  140. __dma_sync(sg->dma_address, sg->length, direction);
  141. }
  142. static inline void
  143. dma_direct_sync_sg_for_device(struct device *dev,
  144. struct scatterlist *sgl, int nents,
  145. enum dma_data_direction direction)
  146. {
  147. struct scatterlist *sg;
  148. int i;
  149. /* FIXME this part of code is untested */
  150. if (direction == DMA_TO_DEVICE)
  151. for_each_sg(sgl, sg, nents, i)
  152. __dma_sync(sg->dma_address, sg->length, direction);
  153. }
  154. struct dma_map_ops dma_direct_ops = {
  155. .alloc = dma_direct_alloc_coherent,
  156. .free = dma_direct_free_coherent,
  157. .map_sg = dma_direct_map_sg,
  158. .unmap_sg = dma_direct_unmap_sg,
  159. .dma_supported = dma_direct_dma_supported,
  160. .map_page = dma_direct_map_page,
  161. .unmap_page = dma_direct_unmap_page,
  162. .sync_single_for_cpu = dma_direct_sync_single_for_cpu,
  163. .sync_single_for_device = dma_direct_sync_single_for_device,
  164. .sync_sg_for_cpu = dma_direct_sync_sg_for_cpu,
  165. .sync_sg_for_device = dma_direct_sync_sg_for_device,
  166. };
  167. EXPORT_SYMBOL(dma_direct_ops);
  168. /* Number of entries preallocated for DMA-API debugging */
  169. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  170. static int __init dma_init(void)
  171. {
  172. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  173. return 0;
  174. }
  175. fs_initcall(dma_init);