dma-mapping.h 4.7 KB

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