dma.c 5.2 KB

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