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/dma-debug.h>
  11. #include <asm/bug.h>
  12. #include <asm/cacheflush.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 inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
  22. size_t size, enum dma_data_direction direction)
  23. {
  24. switch (direction) {
  25. case DMA_TO_DEVICE:
  26. flush_dcache_range(paddr + offset, paddr + offset + size);
  27. break;
  28. case DMA_FROM_DEVICE:
  29. invalidate_dcache_range(paddr + offset, paddr + offset + size);
  30. break;
  31. default:
  32. BUG();
  33. }
  34. }
  35. static unsigned long get_dma_direct_offset(struct device *dev)
  36. {
  37. if (dev)
  38. return (unsigned long)dev->archdata.dma_data;
  39. return PCI_DRAM_OFFSET; /* FIXME Not sure if is correct */
  40. }
  41. #define NOT_COHERENT_CACHE
  42. static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  43. dma_addr_t *dma_handle, gfp_t flag)
  44. {
  45. #ifdef NOT_COHERENT_CACHE
  46. return consistent_alloc(flag, size, dma_handle);
  47. #else
  48. void *ret;
  49. struct page *page;
  50. int node = dev_to_node(dev);
  51. /* ignore region specifiers */
  52. flag &= ~(__GFP_HIGHMEM);
  53. page = alloc_pages_node(node, flag, get_order(size));
  54. if (page == NULL)
  55. return NULL;
  56. ret = page_address(page);
  57. memset(ret, 0, size);
  58. *dma_handle = virt_to_phys(ret) + get_dma_direct_offset(dev);
  59. return ret;
  60. #endif
  61. }
  62. static void dma_direct_free_coherent(struct device *dev, size_t size,
  63. void *vaddr, dma_addr_t dma_handle)
  64. {
  65. #ifdef NOT_COHERENT_CACHE
  66. consistent_free(vaddr);
  67. #else
  68. free_pages((unsigned long)vaddr, get_order(size));
  69. #endif
  70. }
  71. static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
  72. int nents, enum dma_data_direction direction,
  73. struct dma_attrs *attrs)
  74. {
  75. struct scatterlist *sg;
  76. int i;
  77. /* FIXME this part of code is untested */
  78. for_each_sg(sgl, sg, nents, i) {
  79. sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
  80. sg->dma_length = sg->length;
  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);