1 // SPDX-License-Identifier: GPL-2.0-only
3 * RISC-V specific functions to support DMA for non-coherent devices
5 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
8 #include <linux/dma-direct.h>
9 #include <linux/dma-map-ops.h>
12 #include <linux/of_device.h>
13 #include <asm/cacheflush.h>
15 static unsigned int riscv_cbom_block_size = L1_CACHE_BYTES;
16 static bool noncoherent_supported;
18 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
19 enum dma_data_direction dir)
21 void *vaddr = phys_to_virt(paddr);
25 ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
28 ALT_CMO_OP(clean, vaddr, size, riscv_cbom_block_size);
30 case DMA_BIDIRECTIONAL:
31 ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
38 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
39 enum dma_data_direction dir)
41 void *vaddr = phys_to_virt(paddr);
47 case DMA_BIDIRECTIONAL:
48 ALT_CMO_OP(flush, vaddr, size, riscv_cbom_block_size);
55 void arch_dma_prep_coherent(struct page *page, size_t size)
57 void *flush_addr = page_address(page);
59 ALT_CMO_OP(flush, flush_addr, size, riscv_cbom_block_size);
62 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
63 const struct iommu_ops *iommu, bool coherent)
65 WARN_TAINT(!coherent && riscv_cbom_block_size > ARCH_DMA_MINALIGN,
66 TAINT_CPU_OUT_OF_SPEC,
67 "%s %s: ARCH_DMA_MINALIGN smaller than riscv,cbom-block-size (%d < %d)",
68 dev_driver_string(dev), dev_name(dev),
69 ARCH_DMA_MINALIGN, riscv_cbom_block_size);
71 WARN_TAINT(!coherent && !noncoherent_supported, TAINT_CPU_OUT_OF_SPEC,
72 "%s %s: device non-coherent but no non-coherent operations supported",
73 dev_driver_string(dev), dev_name(dev));
75 dev->dma_coherent = coherent;
78 #ifdef CONFIG_RISCV_ISA_ZICBOM
79 void riscv_init_cbom_blocksize(void)
81 struct device_node *node;
85 for_each_of_cpu_node(node) {
86 int hartid = riscv_of_processor_hartid(node);
92 /* set block-size for cbom extension if available */
93 ret = of_property_read_u32(node, "riscv,cbom-block-size", &val);
97 if (!riscv_cbom_block_size) {
98 riscv_cbom_block_size = val;
101 if (riscv_cbom_block_size != val)
102 pr_warn("cbom-block-size mismatched between harts %d and %d\n",
103 cbom_hartid, hartid);
109 void riscv_noncoherent_supported(void)
111 noncoherent_supported = true;