xref: /openbmc/linux/arch/riscv/mm/dma-noncoherent.c (revision ecefa105)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RISC-V specific functions to support DMA for non-coherent devices
4  *
5  * Copyright (c) 2021 Western Digital Corporation or its affiliates.
6  */
7 
8 #include <linux/dma-direct.h>
9 #include <linux/dma-map-ops.h>
10 #include <linux/mm.h>
11 #include <asm/cacheflush.h>
12 
13 static bool noncoherent_supported;
14 
15 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
16 			      enum dma_data_direction dir)
17 {
18 	void *vaddr = phys_to_virt(paddr);
19 
20 	switch (dir) {
21 	case DMA_TO_DEVICE:
22 		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
23 		break;
24 	case DMA_FROM_DEVICE:
25 		ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
26 		break;
27 	case DMA_BIDIRECTIONAL:
28 		ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
29 		break;
30 	default:
31 		break;
32 	}
33 }
34 
35 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
36 			   enum dma_data_direction dir)
37 {
38 	void *vaddr = phys_to_virt(paddr);
39 
40 	switch (dir) {
41 	case DMA_TO_DEVICE:
42 		break;
43 	case DMA_FROM_DEVICE:
44 	case DMA_BIDIRECTIONAL:
45 		ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
46 		break;
47 	default:
48 		break;
49 	}
50 }
51 
52 void arch_dma_prep_coherent(struct page *page, size_t size)
53 {
54 	void *flush_addr = page_address(page);
55 
56 	ALT_CMO_OP(flush, flush_addr, size, riscv_cbom_block_size);
57 }
58 
59 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
60 		const struct iommu_ops *iommu, bool coherent)
61 {
62 	WARN_TAINT(!coherent && riscv_cbom_block_size > ARCH_DMA_MINALIGN,
63 		   TAINT_CPU_OUT_OF_SPEC,
64 		   "%s %s: ARCH_DMA_MINALIGN smaller than riscv,cbom-block-size (%d < %d)",
65 		   dev_driver_string(dev), dev_name(dev),
66 		   ARCH_DMA_MINALIGN, riscv_cbom_block_size);
67 
68 	WARN_TAINT(!coherent && !noncoherent_supported, TAINT_CPU_OUT_OF_SPEC,
69 		   "%s %s: device non-coherent but no non-coherent operations supported",
70 		   dev_driver_string(dev), dev_name(dev));
71 
72 	dev->dma_coherent = coherent;
73 }
74 
75 void riscv_noncoherent_supported(void)
76 {
77 	WARN(!riscv_cbom_block_size,
78 	     "Non-coherent DMA support enabled without a block size\n");
79 	noncoherent_supported = true;
80 }
81