blob: 563c2c0d0c8193907821d933c5efe28bc1956169 [file] [log] [blame]
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +02001// 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
12#include <asm/cache.h>
13#include <asm/cpu-type.h>
14#include <asm/dma-coherence.h>
15#include <asm/io.h>
16
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020017/*
18 * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
19 * fill random cachelines with stale data at any time, requiring an extra
20 * flush post-DMA.
21 *
22 * Warning on the terminology - Linux calls an uncached area coherent; MIPS
23 * terminology calls memory areas with hardware maintained coherency coherent.
24 *
25 * Note that the R14000 and R16000 should also be checked for in this condition.
26 * However this function is only called on non-I/O-coherent systems and only the
27 * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
28 * SGI IP32 aka O2.
29 */
Christoph Hellwig56e35f92019-11-07 18:03:11 +010030static inline bool cpu_needs_post_dma_flush(void)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020031{
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020032 switch (boot_cpu_type()) {
33 case CPU_R10000:
34 case CPU_R12000:
35 case CPU_BMIPS5000:
Lichao Liua202bf72020-05-28 09:10:31 +080036 case CPU_LOONGSON2EF:
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020037 return true;
38 default:
39 /*
40 * Presence of MAARs suggests that the CPU supports
41 * speculatively prefetching data, and therefore requires
42 * the post-DMA flush/invalidate.
43 */
44 return cpu_has_maar;
45 }
46}
47
Christoph Hellwig2e96e042019-04-28 13:57:39 -050048void arch_dma_prep_coherent(struct page *page, size_t size)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020049{
Christoph Hellwig2e96e042019-04-28 13:57:39 -050050 dma_cache_wback_inv((unsigned long)page_address(page), size);
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020051}
52
Christoph Hellwigfa7e2242020-02-21 15:55:43 -080053void *arch_dma_set_uncached(void *addr, size_t size)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020054{
Christoph Hellwig2e96e042019-04-28 13:57:39 -050055 return (void *)(__pa(addr) + UNCAC_BASE);
56}
57
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020058static inline void dma_sync_virt(void *addr, size_t size,
59 enum dma_data_direction dir)
60{
61 switch (dir) {
62 case DMA_TO_DEVICE:
63 dma_cache_wback((unsigned long)addr, size);
64 break;
65
66 case DMA_FROM_DEVICE:
67 dma_cache_inv((unsigned long)addr, size);
68 break;
69
70 case DMA_BIDIRECTIONAL:
71 dma_cache_wback_inv((unsigned long)addr, size);
72 break;
73
74 default:
75 BUG();
76 }
77}
78
79/*
80 * A single sg entry may refer to multiple physically contiguous pages. But
81 * we still need to process highmem pages individually. If highmem is not
82 * configured then the bulk of this loop gets optimized out.
83 */
84static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
85 enum dma_data_direction dir)
86{
87 struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
88 unsigned long offset = paddr & ~PAGE_MASK;
89 size_t left = size;
90
91 do {
92 size_t len = left;
93
94 if (PageHighMem(page)) {
95 void *addr;
96
Paul Burtond411da02019-02-15 22:03:04 +000097 if (offset + len > PAGE_SIZE)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020098 len = PAGE_SIZE - offset;
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +020099
100 addr = kmap_atomic(page);
101 dma_sync_virt(addr + offset, len, dir);
102 kunmap_atomic(addr);
103 } else
104 dma_sync_virt(page_address(page) + offset, size, dir);
105 offset = 0;
106 page++;
107 left -= len;
108 } while (left);
109}
110
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100111void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
112 enum dma_data_direction dir)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200113{
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200114 dma_sync_phys(paddr, size, dir);
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200115}
116
Hauke Mehrtensf263f2a2018-12-09 16:49:57 +0100117#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100118void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
119 enum dma_data_direction dir)
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200120{
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100121 if (cpu_needs_post_dma_flush())
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200122 dma_sync_phys(paddr, size, dir);
123}
Hauke Mehrtensf263f2a2018-12-09 16:49:57 +0100124#endif
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200125
126void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
127 enum dma_data_direction direction)
128{
129 BUG_ON(direction == DMA_NONE);
130
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200131 dma_sync_virt(vaddr, size, direction);
Christoph Hellwigf8c55dc2018-06-15 13:08:46 +0200132}
Christoph Hellwig347cb6a2019-01-07 13:36:20 -0500133
134#ifdef CONFIG_DMA_PERDEV_COHERENT
135void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
136 const struct iommu_ops *iommu, bool coherent)
137{
138 dev->dma_coherent = coherent;
139}
140#endif