dma-mapping.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef __ASM_SH_DMA_MAPPING_H
  2. #define __ASM_SH_DMA_MAPPING_H
  3. #include <linux/config.h>
  4. #include <linux/mm.h>
  5. #include <asm/scatterlist.h>
  6. #include <asm/cacheflush.h>
  7. #include <asm/io.h>
  8. extern struct bus_type pci_bus_type;
  9. /* arch/sh/mm/consistent.c */
  10. extern void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *handle);
  11. extern void consistent_free(void *vaddr, size_t size);
  12. extern void consistent_sync(void *vaddr, size_t size, int direction);
  13. #define dma_supported(dev, mask) (1)
  14. static inline int dma_set_mask(struct device *dev, u64 mask)
  15. {
  16. if (!dev->dma_mask || !dma_supported(dev, mask))
  17. return -EIO;
  18. *dev->dma_mask = mask;
  19. return 0;
  20. }
  21. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  22. dma_addr_t *dma_handle, gfp_t flag)
  23. {
  24. if (sh_mv.mv_consistent_alloc) {
  25. void *ret;
  26. ret = sh_mv.mv_consistent_alloc(dev, size, dma_handle, flag);
  27. if (ret != NULL)
  28. return ret;
  29. }
  30. return consistent_alloc(flag, size, dma_handle);
  31. }
  32. static inline void dma_free_coherent(struct device *dev, size_t size,
  33. void *vaddr, dma_addr_t dma_handle)
  34. {
  35. if (sh_mv.mv_consistent_free) {
  36. int ret;
  37. ret = sh_mv.mv_consistent_free(dev, size, vaddr, dma_handle);
  38. if (ret == 0)
  39. return;
  40. }
  41. consistent_free(vaddr, size);
  42. }
  43. static inline void dma_cache_sync(void *vaddr, size_t size,
  44. enum dma_data_direction dir)
  45. {
  46. consistent_sync(vaddr, size, (int)dir);
  47. }
  48. static inline dma_addr_t dma_map_single(struct device *dev,
  49. void *ptr, size_t size,
  50. enum dma_data_direction dir)
  51. {
  52. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  53. if (dev->bus == &pci_bus_type)
  54. return virt_to_bus(ptr);
  55. #endif
  56. dma_cache_sync(ptr, size, dir);
  57. return virt_to_bus(ptr);
  58. }
  59. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  60. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  61. int nents, enum dma_data_direction dir)
  62. {
  63. int i;
  64. for (i = 0; i < nents; i++) {
  65. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  66. dma_cache_sync(page_address(sg[i].page) + sg[i].offset,
  67. sg[i].length, dir);
  68. #endif
  69. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  70. }
  71. return nents;
  72. }
  73. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  74. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  75. unsigned long offset, size_t size,
  76. enum dma_data_direction dir)
  77. {
  78. return dma_map_single(dev, page_address(page) + offset, size, dir);
  79. }
  80. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  81. size_t size, enum dma_data_direction dir)
  82. {
  83. dma_unmap_single(dev, dma_address, size, dir);
  84. }
  85. static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
  86. size_t size, enum dma_data_direction dir)
  87. {
  88. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  89. if (dev->bus == &pci_bus_type)
  90. return;
  91. #endif
  92. dma_cache_sync(bus_to_virt(dma_handle), size, dir);
  93. }
  94. static inline void dma_sync_single_range(struct device *dev,
  95. dma_addr_t dma_handle,
  96. unsigned long offset, size_t size,
  97. enum dma_data_direction dir)
  98. {
  99. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  100. if (dev->bus == &pci_bus_type)
  101. return;
  102. #endif
  103. dma_cache_sync(bus_to_virt(dma_handle) + offset, size, dir);
  104. }
  105. static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
  106. int nelems, enum dma_data_direction dir)
  107. {
  108. int i;
  109. for (i = 0; i < nelems; i++) {
  110. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  111. dma_cache_sync(page_address(sg[i].page) + sg[i].offset,
  112. sg[i].length, dir);
  113. #endif
  114. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  115. }
  116. }
  117. static void dma_sync_single_for_cpu(struct device *dev,
  118. dma_addr_t dma_handle, size_t size,
  119. enum dma_data_direction dir)
  120. __attribute__ ((alias("dma_sync_single")));
  121. static void dma_sync_single_for_device(struct device *dev,
  122. dma_addr_t dma_handle, size_t size,
  123. enum dma_data_direction dir)
  124. __attribute__ ((alias("dma_sync_single")));
  125. static void dma_sync_sg_for_cpu(struct device *dev,
  126. struct scatterlist *sg, int nelems,
  127. enum dma_data_direction dir)
  128. __attribute__ ((alias("dma_sync_sg")));
  129. static void dma_sync_sg_for_device(struct device *dev,
  130. struct scatterlist *sg, int nelems,
  131. enum dma_data_direction dir)
  132. __attribute__ ((alias("dma_sync_sg")));
  133. static inline int dma_get_cache_alignment(void)
  134. {
  135. /*
  136. * Each processor family will define its own L1_CACHE_SHIFT,
  137. * L1_CACHE_BYTES wraps to this, so this is always safe.
  138. */
  139. return L1_CACHE_BYTES;
  140. }
  141. static inline int dma_mapping_error(dma_addr_t dma_addr)
  142. {
  143. return dma_addr == 0;
  144. }
  145. #endif /* __ASM_SH_DMA_MAPPING_H */