dma-noncoherent.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 Ani Joshi <[email protected]>
  4. * Copyright (C) 2000, 2001, 06 Ralf Baechle <[email protected]>
  5. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  6. */
  7. #include <linux/dma-direct.h>
  8. #include <linux/dma-map-ops.h>
  9. #include <linux/highmem.h>
  10. #include <asm/cache.h>
  11. #include <asm/cpu-type.h>
  12. #include <asm/io.h>
  13. /*
  14. * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
  15. * fill random cachelines with stale data at any time, requiring an extra
  16. * flush post-DMA.
  17. *
  18. * Warning on the terminology - Linux calls an uncached area coherent; MIPS
  19. * terminology calls memory areas with hardware maintained coherency coherent.
  20. *
  21. * Note that the R14000 and R16000 should also be checked for in this condition.
  22. * However this function is only called on non-I/O-coherent systems and only the
  23. * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
  24. * SGI IP32 aka O2.
  25. */
  26. static inline bool cpu_needs_post_dma_flush(void)
  27. {
  28. switch (boot_cpu_type()) {
  29. case CPU_R10000:
  30. case CPU_R12000:
  31. case CPU_BMIPS5000:
  32. case CPU_LOONGSON2EF:
  33. case CPU_XBURST:
  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_prep_coherent(struct page *page, size_t size)
  45. {
  46. dma_cache_wback_inv((unsigned long)page_address(page), size);
  47. }
  48. void *arch_dma_set_uncached(void *addr, size_t size)
  49. {
  50. return (void *)(__pa(addr) + UNCAC_BASE);
  51. }
  52. static inline void dma_sync_virt_for_device(void *addr, size_t size,
  53. enum dma_data_direction dir)
  54. {
  55. switch (dir) {
  56. case DMA_TO_DEVICE:
  57. dma_cache_wback((unsigned long)addr, size);
  58. break;
  59. case DMA_FROM_DEVICE:
  60. dma_cache_inv((unsigned long)addr, size);
  61. break;
  62. case DMA_BIDIRECTIONAL:
  63. dma_cache_wback_inv((unsigned long)addr, size);
  64. break;
  65. default:
  66. BUG();
  67. }
  68. }
  69. static inline void dma_sync_virt_for_cpu(void *addr, size_t size,
  70. enum dma_data_direction dir)
  71. {
  72. switch (dir) {
  73. case DMA_TO_DEVICE:
  74. break;
  75. case DMA_FROM_DEVICE:
  76. case DMA_BIDIRECTIONAL:
  77. dma_cache_inv((unsigned long)addr, size);
  78. break;
  79. default:
  80. BUG();
  81. }
  82. }
  83. /*
  84. * A single sg entry may refer to multiple physically contiguous pages. But
  85. * we still need to process highmem pages individually. If highmem is not
  86. * configured then the bulk of this loop gets optimized out.
  87. */
  88. static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
  89. enum dma_data_direction dir, bool for_device)
  90. {
  91. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  92. unsigned long offset = paddr & ~PAGE_MASK;
  93. size_t left = size;
  94. do {
  95. size_t len = left;
  96. void *addr;
  97. if (PageHighMem(page)) {
  98. if (offset + len > PAGE_SIZE)
  99. len = PAGE_SIZE - offset;
  100. }
  101. addr = kmap_atomic(page);
  102. if (for_device)
  103. dma_sync_virt_for_device(addr + offset, len, dir);
  104. else
  105. dma_sync_virt_for_cpu(addr + offset, len, dir);
  106. kunmap_atomic(addr);
  107. offset = 0;
  108. page++;
  109. left -= len;
  110. } while (left);
  111. }
  112. void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
  113. enum dma_data_direction dir)
  114. {
  115. dma_sync_phys(paddr, size, dir, true);
  116. }
  117. #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
  118. void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  119. enum dma_data_direction dir)
  120. {
  121. if (cpu_needs_post_dma_flush())
  122. dma_sync_phys(paddr, size, dir, false);
  123. }
  124. #endif
  125. #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
  126. void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
  127. const struct iommu_ops *iommu, bool coherent)
  128. {
  129. dev->dma_coherent = coherent;
  130. }
  131. #endif