dma-noncoherent.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*
  16. * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
  17. * fill random cachelines with stale data at any time, requiring an extra
  18. * flush post-DMA.
  19. *
  20. * Warning on the terminology - Linux calls an uncached area coherent; MIPS
  21. * terminology calls memory areas with hardware maintained coherency coherent.
  22. *
  23. * Note that the R14000 and R16000 should also be checked for in this condition.
  24. * However this function is only called on non-I/O-coherent systems and only the
  25. * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
  26. * SGI IP32 aka O2.
  27. */
  28. static inline bool cpu_needs_post_dma_flush(struct device *dev)
  29. {
  30. switch (boot_cpu_type()) {
  31. case CPU_R10000:
  32. case CPU_R12000:
  33. case CPU_BMIPS5000:
  34. return true;
  35. default:
  36. /*
  37. * Presence of MAARs suggests that the CPU supports
  38. * speculatively prefetching data, and therefore requires
  39. * the post-DMA flush/invalidate.
  40. */
  41. return cpu_has_maar;
  42. }
  43. }
  44. void *arch_dma_alloc(struct device *dev, size_t size,
  45. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  46. {
  47. void *ret;
  48. ret = dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs);
  49. if (ret && !(attrs & DMA_ATTR_NON_CONSISTENT)) {
  50. dma_cache_wback_inv((unsigned long) ret, size);
  51. ret = (void *)UNCAC_ADDR(ret);
  52. }
  53. return ret;
  54. }
  55. void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
  56. dma_addr_t dma_addr, unsigned long attrs)
  57. {
  58. if (!(attrs & DMA_ATTR_NON_CONSISTENT))
  59. cpu_addr = (void *)CAC_ADDR((unsigned long)cpu_addr);
  60. dma_direct_free_pages(dev, size, cpu_addr, dma_addr, attrs);
  61. }
  62. long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
  63. dma_addr_t dma_addr)
  64. {
  65. unsigned long addr = CAC_ADDR((unsigned long)cpu_addr);
  66. return page_to_pfn(virt_to_page((void *)addr));
  67. }
  68. pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
  69. unsigned long attrs)
  70. {
  71. if (attrs & DMA_ATTR_WRITE_COMBINE)
  72. return pgprot_writecombine(prot);
  73. return pgprot_noncached(prot);
  74. }
  75. static inline void dma_sync_virt(void *addr, size_t size,
  76. enum dma_data_direction dir)
  77. {
  78. switch (dir) {
  79. case DMA_TO_DEVICE:
  80. dma_cache_wback((unsigned long)addr, size);
  81. break;
  82. case DMA_FROM_DEVICE:
  83. dma_cache_inv((unsigned long)addr, size);
  84. break;
  85. case DMA_BIDIRECTIONAL:
  86. dma_cache_wback_inv((unsigned long)addr, size);
  87. break;
  88. default:
  89. BUG();
  90. }
  91. }
  92. /*
  93. * A single sg entry may refer to multiple physically contiguous pages. But
  94. * we still need to process highmem pages individually. If highmem is not
  95. * configured then the bulk of this loop gets optimized out.
  96. */
  97. static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
  98. enum dma_data_direction dir)
  99. {
  100. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  101. unsigned long offset = paddr & ~PAGE_MASK;
  102. size_t left = size;
  103. do {
  104. size_t len = left;
  105. if (PageHighMem(page)) {
  106. void *addr;
  107. if (offset + len > PAGE_SIZE) {
  108. if (offset >= PAGE_SIZE) {
  109. page += offset >> PAGE_SHIFT;
  110. offset &= ~PAGE_MASK;
  111. }
  112. len = PAGE_SIZE - offset;
  113. }
  114. addr = kmap_atomic(page);
  115. dma_sync_virt(addr + offset, len, dir);
  116. kunmap_atomic(addr);
  117. } else
  118. dma_sync_virt(page_address(page) + offset, size, dir);
  119. offset = 0;
  120. page++;
  121. left -= len;
  122. } while (left);
  123. }
  124. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  125. size_t size, enum dma_data_direction dir)
  126. {
  127. dma_sync_phys(paddr, size, dir);
  128. }
  129. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  130. size_t size, enum dma_data_direction dir)
  131. {
  132. if (cpu_needs_post_dma_flush(dev))
  133. dma_sync_phys(paddr, size, dir);
  134. }
  135. void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  136. enum dma_data_direction direction)
  137. {
  138. BUG_ON(direction == DMA_NONE);
  139. dma_sync_virt(vaddr, size, direction);
  140. }