dma-noncoherent.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
  4. * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
  5. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  6. */
  7. #include <linux/dma-direct.h>
  8. #include <linux/dma-noncoherent.h>
  9. #include <linux/dma-contiguous.h>
  10. #include <linux/highmem.h>
  11. #include <asm/cache.h>
  12. #include <asm/cpu-type.h>
  13. #include <asm/dma-coherence.h>
  14. #include <asm/io.h>
  15. #ifdef CONFIG_DMA_PERDEV_COHERENT
  16. static inline int dev_is_coherent(struct device *dev)
  17. {
  18. return dev->archdata.dma_coherent;
  19. }
  20. #else
  21. static inline int dev_is_coherent(struct device *dev)
  22. {
  23. switch (coherentio) {
  24. default:
  25. case IO_COHERENCE_DEFAULT:
  26. return hw_coherentio;
  27. case IO_COHERENCE_ENABLED:
  28. return 1;
  29. case IO_COHERENCE_DISABLED:
  30. return 0;
  31. }
  32. }
  33. #endif /* CONFIG_DMA_PERDEV_COHERENT */
  34. /*
  35. * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
  36. * fill random cachelines with stale data at any time, requiring an extra
  37. * flush post-DMA.
  38. *
  39. * Warning on the terminology - Linux calls an uncached area coherent; MIPS
  40. * terminology calls memory areas with hardware maintained coherency coherent.
  41. *
  42. * Note that the R14000 and R16000 should also be checked for in this condition.
  43. * However this function is only called on non-I/O-coherent systems and only the
  44. * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
  45. * SGI IP32 aka O2.
  46. */
  47. static inline bool cpu_needs_post_dma_flush(struct device *dev)
  48. {
  49. if (dev_is_coherent(dev))
  50. return false;
  51. switch (boot_cpu_type()) {
  52. case CPU_R10000:
  53. case CPU_R12000:
  54. case CPU_BMIPS5000:
  55. return true;
  56. default:
  57. /*
  58. * Presence of MAARs suggests that the CPU supports
  59. * speculatively prefetching data, and therefore requires
  60. * the post-DMA flush/invalidate.
  61. */
  62. return cpu_has_maar;
  63. }
  64. }
  65. void *arch_dma_alloc(struct device *dev, size_t size,
  66. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  67. {
  68. void *ret;
  69. ret = dma_direct_alloc(dev, size, dma_handle, gfp, attrs);
  70. if (!ret)
  71. return NULL;
  72. if (!dev_is_coherent(dev) && !(attrs & DMA_ATTR_NON_CONSISTENT)) {
  73. dma_cache_wback_inv((unsigned long) ret, size);
  74. ret = (void *)UNCAC_ADDR(ret);
  75. }
  76. return ret;
  77. }
  78. void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
  79. dma_addr_t dma_addr, unsigned long attrs)
  80. {
  81. if (!(attrs & DMA_ATTR_NON_CONSISTENT) && !dev_is_coherent(dev))
  82. cpu_addr = (void *)CAC_ADDR((unsigned long)cpu_addr);
  83. dma_direct_free(dev, size, cpu_addr, dma_addr, attrs);
  84. }
  85. int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  86. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  87. unsigned long attrs)
  88. {
  89. unsigned long user_count = vma_pages(vma);
  90. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  91. unsigned long addr = (unsigned long)cpu_addr;
  92. unsigned long off = vma->vm_pgoff;
  93. unsigned long pfn;
  94. int ret = -ENXIO;
  95. if (!dev_is_coherent(dev))
  96. addr = CAC_ADDR(addr);
  97. pfn = page_to_pfn(virt_to_page((void *)addr));
  98. if (attrs & DMA_ATTR_WRITE_COMBINE)
  99. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  100. else
  101. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  102. if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  103. return ret;
  104. if (off < count && user_count <= (count - off)) {
  105. ret = remap_pfn_range(vma, vma->vm_start,
  106. pfn + off,
  107. user_count << PAGE_SHIFT,
  108. vma->vm_page_prot);
  109. }
  110. return ret;
  111. }
  112. static inline void dma_sync_virt(void *addr, size_t size,
  113. enum dma_data_direction dir)
  114. {
  115. switch (dir) {
  116. case DMA_TO_DEVICE:
  117. dma_cache_wback((unsigned long)addr, size);
  118. break;
  119. case DMA_FROM_DEVICE:
  120. dma_cache_inv((unsigned long)addr, size);
  121. break;
  122. case DMA_BIDIRECTIONAL:
  123. dma_cache_wback_inv((unsigned long)addr, size);
  124. break;
  125. default:
  126. BUG();
  127. }
  128. }
  129. /*
  130. * A single sg entry may refer to multiple physically contiguous pages. But
  131. * we still need to process highmem pages individually. If highmem is not
  132. * configured then the bulk of this loop gets optimized out.
  133. */
  134. static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
  135. enum dma_data_direction dir)
  136. {
  137. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  138. unsigned long offset = paddr & ~PAGE_MASK;
  139. size_t left = size;
  140. do {
  141. size_t len = left;
  142. if (PageHighMem(page)) {
  143. void *addr;
  144. if (offset + len > PAGE_SIZE) {
  145. if (offset >= PAGE_SIZE) {
  146. page += offset >> PAGE_SHIFT;
  147. offset &= ~PAGE_MASK;
  148. }
  149. len = PAGE_SIZE - offset;
  150. }
  151. addr = kmap_atomic(page);
  152. dma_sync_virt(addr + offset, len, dir);
  153. kunmap_atomic(addr);
  154. } else
  155. dma_sync_virt(page_address(page) + offset, size, dir);
  156. offset = 0;
  157. page++;
  158. left -= len;
  159. } while (left);
  160. }
  161. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  162. size_t size, enum dma_data_direction dir)
  163. {
  164. if (!dev_is_coherent(dev))
  165. dma_sync_phys(paddr, size, dir);
  166. }
  167. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  168. size_t size, enum dma_data_direction dir)
  169. {
  170. if (cpu_needs_post_dma_flush(dev))
  171. dma_sync_phys(paddr, size, dir);
  172. }
  173. void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  174. enum dma_data_direction direction)
  175. {
  176. BUG_ON(direction == DMA_NONE);
  177. if (!dev_is_coherent(dev))
  178. dma_sync_virt(vaddr, size, direction);
  179. }