dma-mapping.h 5.2 KB

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