dma.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <asm/cacheflush.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 inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
  23. size_t size, enum dma_data_direction direction)
  24. {
  25. switch (direction) {
  26. case DMA_TO_DEVICE:
  27. flush_dcache_range(paddr + offset, paddr + offset + size);
  28. break;
  29. case DMA_FROM_DEVICE:
  30. invalidate_dcache_range(paddr + offset, paddr + offset + size);
  31. break;
  32. default:
  33. BUG();
  34. }
  35. }
  36. static unsigned long get_dma_direct_offset(struct device *dev)
  37. {
  38. if (likely(dev))
  39. return (unsigned long)dev->archdata.dma_data;
  40. return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
  41. }
  42. #define NOT_COHERENT_CACHE
  43. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  44. dma_addr_t *dma_handle, gfp_t flag)
  45. {
  46. #ifdef NOT_COHERENT_CACHE
  47. return consistent_alloc(flag, size, dma_handle);
  48. #else
  49. void *ret;
  50. struct page *page;
  51. int node = dev_to_node(dev);
  52. /* ignore region specifiers */
  53. flag &= ~(__GFP_HIGHMEM);
  54. page = alloc_pages_node(node, flag, get_order(size));
  55. if (page == NULL)
  56. return NULL;
  57. ret = page_address(page);
  58. memset(ret, 0, size);
  59. *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
  60. return ret;
  61. #endif
  62. }
  63. static void dma_direct_free_coherent(struct device *dev, size_t size,
  64. void *vaddr, dma_addr_t dma_handle)
  65. {
  66. #ifdef NOT_COHERENT_CACHE
  67. consistent_free(size, vaddr);
  68. #else
  69. free_pages((unsigned long)vaddr, get_order(size));
  70. #endif
  71. }
  72. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  73. int nents, enum dma_data_direction direction,
  74. struct dma_attrs *attrs)
  75. {
  76. struct scatterlist *sg;
  77. int i;
  78. /* FIXME this part of code is untested */
  79. for_each_sg(sgl, sg, nents, i) {
  80. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  81. __dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
  82. sg->length, direction);
  83. }
  84. return nents;
  85. }
  86. static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
  87. int nents, enum dma_data_direction direction,
  88. struct dma_attrs *attrs)
  89. {
  90. }
  91. static int dma_direct_dma_supported(struct device *dev, u64 mask)
  92. {
  93. return 1;
  94. }
  95. static inline dma_addr_t dma_direct_map_page(struct device *dev,
  96. struct page *page,
  97. unsigned long offset,
  98. size_t size,
  99. enum dma_data_direction direction,
  100. struct dma_attrs *attrs)
  101. {
  102. __dma_sync_page(page_to_phys(page), offset, size, direction);
  103. return page_to_phys(page) + offset + get_dma_direct_offset(dev);
  104. }
  105. static inline void dma_direct_unmap_page(struct device *dev,
  106. dma_addr_t dma_address,
  107. size_t size,
  108. enum dma_data_direction direction,
  109. struct dma_attrs *attrs)
  110. {
  111. /* There is not necessary to do cache cleanup
  112. *
  113. * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
  114. * dma_address is physical address
  115. */
  116. __dma_sync_page(dma_address, 0 , size, direction);
  117. }
  118. struct dma_map_ops dma_direct_ops = {
  119. .alloc_coherent = dma_direct_alloc_coherent,
  120. .free_coherent = dma_direct_free_coherent,
  121. .map_sg = dma_direct_map_sg,
  122. .unmap_sg = dma_direct_unmap_sg,
  123. .dma_supported = dma_direct_dma_supported,
  124. .map_page = dma_direct_map_page,
  125. .unmap_page = dma_direct_unmap_page,
  126. };
  127. EXPORT_SYMBOL(dma_direct_ops);
  128. /* Number of entries preallocated for DMA-API debugging */
  129. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  130. static int __init dma_init(void)
  131. {
  132. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  133. return 0;
  134. }
  135. fs_initcall(dma_init);