dma-mapping.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef __ASM_SH_DMA_MAPPING_H
  2. #define __ASM_SH_DMA_MAPPING_H
  3. #include <linux/mm.h>
  4. #include <asm/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 s = (unsigned long) vaddr & L1_CACHE_ALIGN_MASK;
  36. unsigned long e = (vaddr + size) & L1_CACHE_ALIGN_MASK;
  37. for (; s <= e; s += L1_CACHE_BYTES)
  38. asm volatile ("ocbp %0, 0" : : "r" (s));
  39. }
  40. static inline dma_addr_t dma_map_single(struct device *dev,
  41. void *ptr, size_t size,
  42. enum dma_data_direction dir)
  43. {
  44. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  45. if (dev->bus == &pci_bus_type)
  46. return virt_to_phys(ptr);
  47. #endif
  48. dma_cache_sync(dev, ptr, size, dir);
  49. return virt_to_phys(ptr);
  50. }
  51. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  52. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  53. int nents, enum dma_data_direction dir)
  54. {
  55. int i;
  56. for (i = 0; i < nents; i++) {
  57. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  58. dma_cache_sync(dev, page_address(sg[i].page) + sg[i].offset,
  59. sg[i].length, dir);
  60. #endif
  61. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  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, page_address(sg[i].page) + sg[i].offset,
  104. sg[i].length, dir);
  105. #endif
  106. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  107. }
  108. }
  109. static inline void dma_sync_single_for_cpu(struct device *dev,
  110. dma_addr_t dma_handle, size_t size,
  111. enum dma_data_direction dir)
  112. {
  113. dma_sync_single(dev, dma_handle, size, dir);
  114. }
  115. static inline void dma_sync_single_for_device(struct device *dev,
  116. dma_addr_t dma_handle, size_t size,
  117. enum dma_data_direction dir)
  118. {
  119. dma_sync_single(dev, dma_handle, size, dir);
  120. }
  121. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  122. dma_addr_t dma_handle,
  123. unsigned long offset,
  124. size_t size,
  125. enum dma_data_direction direction)
  126. {
  127. dma_sync_single_for_cpu(dev, dma_handle+offset, size, direction);
  128. }
  129. static inline void dma_sync_single_range_for_device(struct device *dev,
  130. dma_addr_t dma_handle,
  131. unsigned long offset,
  132. size_t size,
  133. enum dma_data_direction direction)
  134. {
  135. dma_sync_single_for_device(dev, dma_handle+offset, size, direction);
  136. }
  137. static inline void dma_sync_sg_for_cpu(struct device *dev,
  138. struct scatterlist *sg, int nelems,
  139. enum dma_data_direction dir)
  140. {
  141. dma_sync_sg(dev, sg, nelems, dir);
  142. }
  143. static inline void dma_sync_sg_for_device(struct device *dev,
  144. struct scatterlist *sg, int nelems,
  145. enum dma_data_direction dir)
  146. {
  147. dma_sync_sg(dev, sg, nelems, dir);
  148. }
  149. static inline int dma_get_cache_alignment(void)
  150. {
  151. /*
  152. * Each processor family will define its own L1_CACHE_SHIFT,
  153. * L1_CACHE_BYTES wraps to this, so this is always safe.
  154. */
  155. return L1_CACHE_BYTES;
  156. }
  157. static inline int dma_mapping_error(dma_addr_t dma_addr)
  158. {
  159. return dma_addr == 0;
  160. }
  161. #endif /* __ASM_SH_DMA_MAPPING_H */