Skip to content

Commit 1b85399

Browse files
atishp04esmil
authored andcommitted
RISC-V: Support non-coherent DMA operations
** Do not upstream ** This is hacky fix just for testing. The actual patch would read the RISCV_UNCACHED_OFFSET from the DT for only the non-coherent devices. All other devices on beagleV and all other platform should just set dma_default_coherent to true. [Emil: remove spurious whitespace and fix format string warning] Signed-off-by: Atish Patra <atish.patra@wdc.com> Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
1 parent 791244c commit 1b85399

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

arch/riscv/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ config PGTABLE_LEVELS
218218
config LOCKDEP_SUPPORT
219219
def_bool y
220220

221+
config RISCV_UNCACHED_OFFSET
222+
hex "Base address of uncached alias"
223+
default 0xF80000000 if ARCH_HAS_DMA_SET_UNCACHED && SOC_STARFIVE
224+
default 0 if !ARCH_HAS_DMA_SET_UNCACHED
225+
226+
config RISCV_DMA_NONCOHERENT
227+
bool
228+
select ARCH_HAS_DMA_PREP_COHERENT
229+
select ARCH_HAS_SYNC_DMA_FOR_DEVICE
230+
select ARCH_HAS_SYNC_DMA_FOR_CPU
231+
select ARCH_HAS_DMA_SET_UNCACHED
232+
select ARCH_HAS_DMA_CLEAR_UNCACHED
233+
select ARCH_HAS_SETUP_DMA_OPS
234+
221235
source "arch/riscv/Kconfig.socs"
222236
source "arch/riscv/Kconfig.erratas"
223237

arch/riscv/Kconfig.socs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config SOC_STARFIVE
2323
bool "StarFive SoCs"
2424
select PINCTRL
2525
select RESET_CONTROLLER
26+
select RISCV_DMA_NONCOHERENT
2627
select SIFIVE_L2
2728
select SIFIVE_L2_FLUSH
2829
select SIFIVE_PLIC

arch/riscv/mm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ KASAN_SANITIZE_init.o := n
2727
endif
2828

2929
obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
30+
obj-$(CONFIG_RISCV_DMA_NONCOHERENT) += dma-noncoherent.o

arch/riscv/mm/dma-noncoherent.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* DMA mapping implementation inspired from arm/mm/dma-mapping.c
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/init.h>
11+
#include <linux/io.h>
12+
#include <linux/mm.h>
13+
#include <asm/cpu_ops.h>
14+
#include <asm/sbi.h>
15+
#include <asm/smp.h>
16+
17+
//TODO Do it through SBI
18+
#include <soc/sifive/sifive_l2_cache.h>
19+
20+
void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
21+
{
22+
sifive_l2_flush64_range(paddr, size);
23+
}
24+
25+
void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
26+
{
27+
sifive_l2_flush64_range(paddr, size);
28+
}
29+
30+
void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
31+
const struct iommu_ops *iommu, bool coherent)
32+
{
33+
dev_info(dev, "coherent device %d dev->dma_coherent %d\n", coherent, dev->dma_coherent);
34+
dev->dma_coherent = coherent;
35+
}
36+
37+
//TODO: We are supposed to invalidate the cache here
38+
void arch_dma_prep_coherent(struct page *page, size_t size)
39+
{
40+
void *flush_addr = page_address(page);
41+
42+
memset(flush_addr, 0, size);
43+
sifive_l2_flush64_range(__pa(flush_addr), size);
44+
}
45+
46+
void arch_dma_clear_uncached(void *addr, size_t size)
47+
{
48+
memunmap(addr);
49+
}
50+
51+
void *arch_dma_set_uncached(void *addr, size_t size)
52+
{
53+
phys_addr_t phys_addr = __pa(addr) + CONFIG_RISCV_UNCACHED_OFFSET;
54+
void *mem_base = NULL;
55+
56+
mem_base = memremap(phys_addr, size, MEMREMAP_WT);
57+
if (!mem_base) {
58+
pr_err("%s memremap failed for addr %px\n", __func__, addr);
59+
return ERR_PTR(-EINVAL);
60+
}
61+
62+
return mem_base;
63+
}

0 commit comments

Comments
 (0)