xref: /openbmc/linux/arch/mips/mm/dma-noncoherent.c (revision 2e96e04d25caaca8039ba9561e7e02ee8a192553)
1f8c55dc6SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2f8c55dc6SChristoph Hellwig /*
3f8c55dc6SChristoph Hellwig  * Copyright (C) 2000  Ani Joshi <ajoshi@unixbox.com>
4f8c55dc6SChristoph Hellwig  * Copyright (C) 2000, 2001, 06	 Ralf Baechle <ralf@linux-mips.org>
5f8c55dc6SChristoph Hellwig  * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
6f8c55dc6SChristoph Hellwig  */
7f8c55dc6SChristoph Hellwig #include <linux/dma-direct.h>
8f8c55dc6SChristoph Hellwig #include <linux/dma-noncoherent.h>
9f8c55dc6SChristoph Hellwig #include <linux/dma-contiguous.h>
10f8c55dc6SChristoph Hellwig #include <linux/highmem.h>
11f8c55dc6SChristoph Hellwig 
12f8c55dc6SChristoph Hellwig #include <asm/cache.h>
13f8c55dc6SChristoph Hellwig #include <asm/cpu-type.h>
14f8c55dc6SChristoph Hellwig #include <asm/dma-coherence.h>
15f8c55dc6SChristoph Hellwig #include <asm/io.h>
16f8c55dc6SChristoph Hellwig 
17f8c55dc6SChristoph Hellwig /*
18f8c55dc6SChristoph Hellwig  * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
19f8c55dc6SChristoph Hellwig  * fill random cachelines with stale data at any time, requiring an extra
20f8c55dc6SChristoph Hellwig  * flush post-DMA.
21f8c55dc6SChristoph Hellwig  *
22f8c55dc6SChristoph Hellwig  * Warning on the terminology - Linux calls an uncached area coherent;  MIPS
23f8c55dc6SChristoph Hellwig  * terminology calls memory areas with hardware maintained coherency coherent.
24f8c55dc6SChristoph Hellwig  *
25f8c55dc6SChristoph Hellwig  * Note that the R14000 and R16000 should also be checked for in this condition.
26f8c55dc6SChristoph Hellwig  * However this function is only called on non-I/O-coherent systems and only the
27f8c55dc6SChristoph Hellwig  * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
28f8c55dc6SChristoph Hellwig  * SGI IP32 aka O2.
29f8c55dc6SChristoph Hellwig  */
30f8c55dc6SChristoph Hellwig static inline bool cpu_needs_post_dma_flush(struct device *dev)
31f8c55dc6SChristoph Hellwig {
32f8c55dc6SChristoph Hellwig 	switch (boot_cpu_type()) {
33f8c55dc6SChristoph Hellwig 	case CPU_R10000:
34f8c55dc6SChristoph Hellwig 	case CPU_R12000:
35f8c55dc6SChristoph Hellwig 	case CPU_BMIPS5000:
36f8c55dc6SChristoph Hellwig 		return true;
37f8c55dc6SChristoph Hellwig 	default:
38f8c55dc6SChristoph Hellwig 		/*
39f8c55dc6SChristoph Hellwig 		 * Presence of MAARs suggests that the CPU supports
40f8c55dc6SChristoph Hellwig 		 * speculatively prefetching data, and therefore requires
41f8c55dc6SChristoph Hellwig 		 * the post-DMA flush/invalidate.
42f8c55dc6SChristoph Hellwig 		 */
43f8c55dc6SChristoph Hellwig 		return cpu_has_maar;
44f8c55dc6SChristoph Hellwig 	}
45f8c55dc6SChristoph Hellwig }
46f8c55dc6SChristoph Hellwig 
47*2e96e04dSChristoph Hellwig void arch_dma_prep_coherent(struct page *page, size_t size)
48f8c55dc6SChristoph Hellwig {
49*2e96e04dSChristoph Hellwig 	dma_cache_wback_inv((unsigned long)page_address(page), size);
50f8c55dc6SChristoph Hellwig }
51f8c55dc6SChristoph Hellwig 
52*2e96e04dSChristoph Hellwig void *uncached_kernel_address(void *addr)
53*2e96e04dSChristoph Hellwig {
54*2e96e04dSChristoph Hellwig 	return (void *)(__pa(addr) + UNCAC_BASE);
55f8c55dc6SChristoph Hellwig }
56f8c55dc6SChristoph Hellwig 
57*2e96e04dSChristoph Hellwig void *cached_kernel_address(void *addr)
58f8c55dc6SChristoph Hellwig {
59*2e96e04dSChristoph Hellwig 	return __va(addr) - UNCAC_BASE;
60f8c55dc6SChristoph Hellwig }
61f8c55dc6SChristoph Hellwig 
6258b04406SChristoph Hellwig long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
6358b04406SChristoph Hellwig 		dma_addr_t dma_addr)
64f8c55dc6SChristoph Hellwig {
65*2e96e04dSChristoph Hellwig 	return page_to_pfn(virt_to_page(cached_kernel_address(cpu_addr)));
66f8c55dc6SChristoph Hellwig }
67f8c55dc6SChristoph Hellwig 
6858b04406SChristoph Hellwig pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
6958b04406SChristoph Hellwig 		unsigned long attrs)
7058b04406SChristoph Hellwig {
7158b04406SChristoph Hellwig 	if (attrs & DMA_ATTR_WRITE_COMBINE)
7258b04406SChristoph Hellwig 		return pgprot_writecombine(prot);
7358b04406SChristoph Hellwig 	return pgprot_noncached(prot);
74f8c55dc6SChristoph Hellwig }
75f8c55dc6SChristoph Hellwig 
76f8c55dc6SChristoph Hellwig static inline void dma_sync_virt(void *addr, size_t size,
77f8c55dc6SChristoph Hellwig 		enum dma_data_direction dir)
78f8c55dc6SChristoph Hellwig {
79f8c55dc6SChristoph Hellwig 	switch (dir) {
80f8c55dc6SChristoph Hellwig 	case DMA_TO_DEVICE:
81f8c55dc6SChristoph Hellwig 		dma_cache_wback((unsigned long)addr, size);
82f8c55dc6SChristoph Hellwig 		break;
83f8c55dc6SChristoph Hellwig 
84f8c55dc6SChristoph Hellwig 	case DMA_FROM_DEVICE:
85f8c55dc6SChristoph Hellwig 		dma_cache_inv((unsigned long)addr, size);
86f8c55dc6SChristoph Hellwig 		break;
87f8c55dc6SChristoph Hellwig 
88f8c55dc6SChristoph Hellwig 	case DMA_BIDIRECTIONAL:
89f8c55dc6SChristoph Hellwig 		dma_cache_wback_inv((unsigned long)addr, size);
90f8c55dc6SChristoph Hellwig 		break;
91f8c55dc6SChristoph Hellwig 
92f8c55dc6SChristoph Hellwig 	default:
93f8c55dc6SChristoph Hellwig 		BUG();
94f8c55dc6SChristoph Hellwig 	}
95f8c55dc6SChristoph Hellwig }
96f8c55dc6SChristoph Hellwig 
97f8c55dc6SChristoph Hellwig /*
98f8c55dc6SChristoph Hellwig  * A single sg entry may refer to multiple physically contiguous pages.  But
99f8c55dc6SChristoph Hellwig  * we still need to process highmem pages individually.  If highmem is not
100f8c55dc6SChristoph Hellwig  * configured then the bulk of this loop gets optimized out.
101f8c55dc6SChristoph Hellwig  */
102f8c55dc6SChristoph Hellwig static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
103f8c55dc6SChristoph Hellwig 		enum dma_data_direction dir)
104f8c55dc6SChristoph Hellwig {
105f8c55dc6SChristoph Hellwig 	struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
106f8c55dc6SChristoph Hellwig 	unsigned long offset = paddr & ~PAGE_MASK;
107f8c55dc6SChristoph Hellwig 	size_t left = size;
108f8c55dc6SChristoph Hellwig 
109f8c55dc6SChristoph Hellwig 	do {
110f8c55dc6SChristoph Hellwig 		size_t len = left;
111f8c55dc6SChristoph Hellwig 
112f8c55dc6SChristoph Hellwig 		if (PageHighMem(page)) {
113f8c55dc6SChristoph Hellwig 			void *addr;
114f8c55dc6SChristoph Hellwig 
115d411da06SPaul Burton 			if (offset + len > PAGE_SIZE)
116f8c55dc6SChristoph Hellwig 				len = PAGE_SIZE - offset;
117f8c55dc6SChristoph Hellwig 
118f8c55dc6SChristoph Hellwig 			addr = kmap_atomic(page);
119f8c55dc6SChristoph Hellwig 			dma_sync_virt(addr + offset, len, dir);
120f8c55dc6SChristoph Hellwig 			kunmap_atomic(addr);
121f8c55dc6SChristoph Hellwig 		} else
122f8c55dc6SChristoph Hellwig 			dma_sync_virt(page_address(page) + offset, size, dir);
123f8c55dc6SChristoph Hellwig 		offset = 0;
124f8c55dc6SChristoph Hellwig 		page++;
125f8c55dc6SChristoph Hellwig 		left -= len;
126f8c55dc6SChristoph Hellwig 	} while (left);
127f8c55dc6SChristoph Hellwig }
128f8c55dc6SChristoph Hellwig 
129f8c55dc6SChristoph Hellwig void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
130f8c55dc6SChristoph Hellwig 		size_t size, enum dma_data_direction dir)
131f8c55dc6SChristoph Hellwig {
132f8c55dc6SChristoph Hellwig 	dma_sync_phys(paddr, size, dir);
133f8c55dc6SChristoph Hellwig }
134f8c55dc6SChristoph Hellwig 
135f263f2a2SHauke Mehrtens #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
136f8c55dc6SChristoph Hellwig void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
137f8c55dc6SChristoph Hellwig 		size_t size, enum dma_data_direction dir)
138f8c55dc6SChristoph Hellwig {
139f8c55dc6SChristoph Hellwig 	if (cpu_needs_post_dma_flush(dev))
140f8c55dc6SChristoph Hellwig 		dma_sync_phys(paddr, size, dir);
141f8c55dc6SChristoph Hellwig }
142f263f2a2SHauke Mehrtens #endif
143f8c55dc6SChristoph Hellwig 
144f8c55dc6SChristoph Hellwig void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
145f8c55dc6SChristoph Hellwig 		enum dma_data_direction direction)
146f8c55dc6SChristoph Hellwig {
147f8c55dc6SChristoph Hellwig 	BUG_ON(direction == DMA_NONE);
148f8c55dc6SChristoph Hellwig 
149f8c55dc6SChristoph Hellwig 	dma_sync_virt(vaddr, size, direction);
150f8c55dc6SChristoph Hellwig }
151347cb6afSChristoph Hellwig 
152347cb6afSChristoph Hellwig #ifdef CONFIG_DMA_PERDEV_COHERENT
153347cb6afSChristoph Hellwig void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
154347cb6afSChristoph Hellwig 		const struct iommu_ops *iommu, bool coherent)
155347cb6afSChristoph Hellwig {
156347cb6afSChristoph Hellwig 	dev->dma_coherent = coherent;
157347cb6afSChristoph Hellwig }
158347cb6afSChristoph Hellwig #endif
159