dma-noncoherent.c 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2000  Ani Joshi <ajoshi@unixbox.com>
 * Copyright (C) 2000, 2001, 06	 Ralf Baechle <ralf@linux-mips.org>
 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
 */
#include <linux/dma-direct.h>
#include <linux/dma-noncoherent.h>
#include <linux/dma-contiguous.h>
#include <linux/highmem.h>

#include <asm/cache.h>
#include <asm/cpu-type.h>
#include <asm/dma-coherence.h>
#include <asm/io.h>

/*
 * The affected CPUs below in 'cpu_needs_post_dma_flush()' can speculatively
 * fill random cachelines with stale data at any time, requiring an extra
 * flush post-DMA.
 *
 * Warning on the terminology - Linux calls an uncached area coherent;  MIPS
 * terminology calls memory areas with hardware maintained coherency coherent.
 *
 * Note that the R14000 and R16000 should also be checked for in this condition.
 * However this function is only called on non-I/O-coherent systems and only the
 * R10000 and R12000 are used in such systems, the SGI IP28 Indigo² rsp.
 * SGI IP32 aka O2.
 */
30
static inline bool cpu_needs_post_dma_flush(void)
31 32 33 34 35
{
	switch (boot_cpu_type()) {
	case CPU_R10000:
	case CPU_R12000:
	case CPU_BMIPS5000:
36
	case CPU_LOONGSON2EF:
37 38 39 40 41 42 43 44 45 46 47
		return true;
	default:
		/*
		 * Presence of MAARs suggests that the CPU supports
		 * speculatively prefetching data, and therefore requires
		 * the post-DMA flush/invalidate.
		 */
		return cpu_has_maar;
	}
}

48
void arch_dma_prep_coherent(struct page *page, size_t size)
49
{
50 51
	dma_cache_wback_inv((unsigned long)page_address(page), size);
}
52

53
void *arch_dma_set_uncached(void *addr, size_t size)
54 55
{
	return (void *)(__pa(addr) + UNCAC_BASE);
56 57
}

58
static inline void dma_sync_virt_for_device(void *addr, size_t size,
59 60 61 62 63 64 65 66 67 68 69 70
		enum dma_data_direction dir)
{
	switch (dir) {
	case DMA_TO_DEVICE:
		dma_cache_wback((unsigned long)addr, size);
		break;
	case DMA_FROM_DEVICE:
		dma_cache_inv((unsigned long)addr, size);
		break;
	case DMA_BIDIRECTIONAL:
		dma_cache_wback_inv((unsigned long)addr, size);
		break;
71 72 73 74
	default:
		BUG();
	}
}
75

76 77 78 79 80 81 82 83 84 85
static inline void dma_sync_virt_for_cpu(void *addr, size_t size,
		enum dma_data_direction dir)
{
	switch (dir) {
	case DMA_TO_DEVICE:
		break;
	case DMA_FROM_DEVICE:
	case DMA_BIDIRECTIONAL:
		dma_cache_inv((unsigned long)addr, size);
		break;
86 87 88 89 90 91 92 93 94 95 96
	default:
		BUG();
	}
}

/*
 * A single sg entry may refer to multiple physically contiguous pages.  But
 * we still need to process highmem pages individually.  If highmem is not
 * configured then the bulk of this loop gets optimized out.
 */
static inline void dma_sync_phys(phys_addr_t paddr, size_t size,
97
		enum dma_data_direction dir, bool for_device)
98 99 100 101 102 103 104
{
	struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
	unsigned long offset = paddr & ~PAGE_MASK;
	size_t left = size;

	do {
		size_t len = left;
105
		void *addr;
106 107

		if (PageHighMem(page)) {
108
			if (offset + len > PAGE_SIZE)
109
				len = PAGE_SIZE - offset;
110 111 112 113 114 115 116 117
		}

		addr = kmap_atomic(page);
		if (for_device)
			dma_sync_virt_for_device(addr + offset, len, dir);
		else
			dma_sync_virt_for_cpu(addr + offset, len, dir);
		kunmap_atomic(addr);
118 119 120 121 122 123 124

		offset = 0;
		page++;
		left -= len;
	} while (left);
}

125 126
void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
		enum dma_data_direction dir)
127
{
128
	dma_sync_phys(paddr, size, dir, true);
129 130
}

131
#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
132 133
void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
		enum dma_data_direction dir)
134
{
135
	if (cpu_needs_post_dma_flush())
136
		dma_sync_phys(paddr, size, dir, false);
137
}
138
#endif
139 140 141 142

void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
		enum dma_data_direction direction)
{
143
	dma_sync_virt_for_device(vaddr, size, direction);
144
}
145 146 147 148 149 150 151 152

#ifdef CONFIG_DMA_PERDEV_COHERENT
void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
		const struct iommu_ops *iommu, bool coherent)
{
	dev->dma_coherent = coherent;
}
#endif