dma-mapping.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  3. * Copyright (C) 2009 Wind River Systems Inc
  4. *
  5. * This file is subject to the terms and conditions of the GNU General
  6. * Public License. See the file COPYING in the main directory of this
  7. * archive for more details.
  8. */
  9. #ifndef _ASM_NIOS2_DMA_MAPPING_H
  10. #define _ASM_NIOS2_DMA_MAPPING_H
  11. #include <linux/scatterlist.h>
  12. #include <linux/cache.h>
  13. #include <asm/cacheflush.h>
  14. static inline void __dma_sync_for_device(void *vaddr, size_t size,
  15. enum dma_data_direction direction)
  16. {
  17. switch (direction) {
  18. case DMA_FROM_DEVICE:
  19. invalidate_dcache_range((unsigned long)vaddr,
  20. (unsigned long)(vaddr + size));
  21. break;
  22. case DMA_TO_DEVICE:
  23. /*
  24. * We just need to flush the caches here , but Nios2 flush
  25. * instruction will do both writeback and invalidate.
  26. */
  27. case DMA_BIDIRECTIONAL: /* flush and invalidate */
  28. flush_dcache_range((unsigned long)vaddr,
  29. (unsigned long)(vaddr + size));
  30. break;
  31. default:
  32. BUG();
  33. }
  34. }
  35. static inline void __dma_sync_for_cpu(void *vaddr, size_t size,
  36. enum dma_data_direction direction)
  37. {
  38. switch (direction) {
  39. case DMA_BIDIRECTIONAL:
  40. case DMA_FROM_DEVICE:
  41. invalidate_dcache_range((unsigned long)vaddr,
  42. (unsigned long)(vaddr + size));
  43. break;
  44. case DMA_TO_DEVICE:
  45. break;
  46. default:
  47. BUG();
  48. }
  49. }
  50. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  51. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  52. void *dma_alloc_coherent(struct device *dev, size_t size,
  53. dma_addr_t *dma_handle, gfp_t flag);
  54. void dma_free_coherent(struct device *dev, size_t size,
  55. void *vaddr, dma_addr_t dma_handle);
  56. static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
  57. size_t size,
  58. enum dma_data_direction direction)
  59. {
  60. BUG_ON(!valid_dma_direction(direction));
  61. __dma_sync_for_device(ptr, size, direction);
  62. return virt_to_phys(ptr);
  63. }
  64. static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  65. size_t size, enum dma_data_direction direction)
  66. {
  67. }
  68. extern int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  69. enum dma_data_direction direction);
  70. extern dma_addr_t dma_map_page(struct device *dev, struct page *page,
  71. unsigned long offset, size_t size, enum dma_data_direction direction);
  72. extern void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  73. size_t size, enum dma_data_direction direction);
  74. extern void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  75. int nhwentries, enum dma_data_direction direction);
  76. extern void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  77. size_t size, enum dma_data_direction direction);
  78. extern void dma_sync_single_for_device(struct device *dev,
  79. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction);
  80. extern void dma_sync_single_range_for_cpu(struct device *dev,
  81. dma_addr_t dma_handle, unsigned long offset, size_t size,
  82. enum dma_data_direction direction);
  83. extern void dma_sync_single_range_for_device(struct device *dev,
  84. dma_addr_t dma_handle, unsigned long offset, size_t size,
  85. enum dma_data_direction direction);
  86. extern void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  87. int nelems, enum dma_data_direction direction);
  88. extern void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  89. int nelems, enum dma_data_direction direction);
  90. static inline int dma_supported(struct device *dev, u64 mask)
  91. {
  92. return 1;
  93. }
  94. static inline int dma_set_mask(struct device *dev, u64 mask)
  95. {
  96. if (!dev->dma_mask || !dma_supported(dev, mask))
  97. return -EIO;
  98. *dev->dma_mask = mask;
  99. return 0;
  100. }
  101. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  102. {
  103. return 0;
  104. }
  105. /*
  106. * dma_alloc_noncoherent() returns non-cacheable memory, so there's no need to
  107. * do any flushing here.
  108. */
  109. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  110. enum dma_data_direction direction)
  111. {
  112. }
  113. /* drivers/base/dma-mapping.c */
  114. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  115. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  116. extern int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  117. void *cpu_addr, dma_addr_t dma_addr,
  118. size_t size);
  119. #define dma_mmap_coherent(d, v, c, h, s) dma_common_mmap(d, v, c, h, s)
  120. #define dma_get_sgtable(d, t, v, h, s) dma_common_get_sgtable(d, t, v, h, s)
  121. #endif /* _ASM_NIOS2_DMA_MAPPING_H */