2022-07-06 16:15:35 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* RISC-V specific functions to support DMA for non-coherent devices
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Western Digital Corporation or its affiliates.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/dma-direct.h>
|
|
|
|
#include <linux/dma-map-ops.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <asm/cacheflush.h>
|
2023-08-18 06:57:20 -07:00
|
|
|
#include <asm/dma-noncoherent.h>
|
2022-07-06 16:15:35 -07:00
|
|
|
|
2023-06-14 09:55:04 -07:00
|
|
|
static bool noncoherent_supported __ro_after_init;
|
riscv: allow kmalloc() caches aligned to the smallest value
Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E
64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel
Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus
it brings some bad effects to coherent platforms:
Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and
kmalloc-8 slab caches don't exist any more, they are replaced with
either kmalloc-128 or kmalloc-64.
Secondly, larger than necessary kmalloc aligned allocations results
in unnecessary cache/TLB pressure.
This issue also exists on arm64 platforms. From last year, Catalin
tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from
ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to
dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage
in various drivers with ARCH_DMA_MINALIGN etc.[1]
One fact we can make use of for riscv: if the CPU doesn't support
ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on
Catalin's work and above fact, we can easily solve the kmalloc align
issue for riscv: we can override dma_get_cache_alignment(), then let
it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know
the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.
So what about if the CPU supports ZICBOM or T-HEAD CMO, but all the
devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the
kmalloc minimum alignment, nothing changed in this case. This case
can be improved in the future.
After this patch, a simple test of booting to a small buildroot rootfs
on qemu shows:
kmalloc-96 5041 5041 96 ...
kmalloc-64 9606 9606 64 ...
kmalloc-32 5128 5128 32 ...
kmalloc-16 7682 7682 16 ...
kmalloc-8 10246 10246 8 ...
So we save about 1268KB memory. The saving will be much larger in normal
OS env on real HW platforms.
Link: https://lore.kernel.org/linux-arm-kernel/20230524171904.3967031-1-catalin.marinas@arm.com/ [1]
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230718152214.2907-2-jszhang@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-07-18 08:22:13 -07:00
|
|
|
int dma_cache_alignment __ro_after_init = ARCH_DMA_MINALIGN;
|
|
|
|
EXPORT_SYMBOL_GPL(dma_cache_alignment);
|
2022-07-06 16:15:35 -07:00
|
|
|
|
2023-08-16 16:23:36 -07:00
|
|
|
static inline void arch_dma_cache_wback(phys_addr_t paddr, size_t size)
|
|
|
|
{
|
|
|
|
void *vaddr = phys_to_virt(paddr);
|
|
|
|
|
2023-08-18 06:57:20 -07:00
|
|
|
#ifdef CONFIG_RISCV_NONSTANDARD_CACHE_OPS
|
|
|
|
if (unlikely(noncoherent_cache_ops.wback)) {
|
|
|
|
noncoherent_cache_ops.wback(paddr, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2023-09-15 08:40:44 -07:00
|
|
|
ALT_CMO_OP(CLEAN, vaddr, size, riscv_cbom_block_size);
|
2023-08-16 16:23:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_dma_cache_inv(phys_addr_t paddr, size_t size)
|
|
|
|
{
|
|
|
|
void *vaddr = phys_to_virt(paddr);
|
|
|
|
|
2023-08-18 06:57:20 -07:00
|
|
|
#ifdef CONFIG_RISCV_NONSTANDARD_CACHE_OPS
|
|
|
|
if (unlikely(noncoherent_cache_ops.inv)) {
|
|
|
|
noncoherent_cache_ops.inv(paddr, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-15 08:40:44 -07:00
|
|
|
ALT_CMO_OP(INVAL, vaddr, size, riscv_cbom_block_size);
|
2023-08-16 16:23:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_dma_cache_wback_inv(phys_addr_t paddr, size_t size)
|
2022-07-06 16:15:35 -07:00
|
|
|
{
|
|
|
|
void *vaddr = phys_to_virt(paddr);
|
|
|
|
|
2023-08-18 06:57:20 -07:00
|
|
|
#ifdef CONFIG_RISCV_NONSTANDARD_CACHE_OPS
|
|
|
|
if (unlikely(noncoherent_cache_ops.wback_inv)) {
|
|
|
|
noncoherent_cache_ops.wback_inv(paddr, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-15 08:40:44 -07:00
|
|
|
ALT_CMO_OP(FLUSH, vaddr, size, riscv_cbom_block_size);
|
2023-08-16 16:23:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool arch_sync_dma_clean_before_fromdevice(void)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool arch_sync_dma_cpu_needs_post_dma_flush(void)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
|
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
2022-07-06 16:15:35 -07:00
|
|
|
switch (dir) {
|
|
|
|
case DMA_TO_DEVICE:
|
2023-08-16 16:23:36 -07:00
|
|
|
arch_dma_cache_wback(paddr, size);
|
2022-07-06 16:15:35 -07:00
|
|
|
break;
|
2023-08-16 16:23:36 -07:00
|
|
|
|
2022-07-06 16:15:35 -07:00
|
|
|
case DMA_FROM_DEVICE:
|
2023-08-16 16:23:36 -07:00
|
|
|
if (!arch_sync_dma_clean_before_fromdevice()) {
|
|
|
|
arch_dma_cache_inv(paddr, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fallthrough;
|
|
|
|
|
2022-07-06 16:15:35 -07:00
|
|
|
case DMA_BIDIRECTIONAL:
|
2023-08-16 16:23:36 -07:00
|
|
|
/* Skip the invalidate here if it's done later */
|
|
|
|
if (IS_ENABLED(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) &&
|
|
|
|
arch_sync_dma_cpu_needs_post_dma_flush())
|
|
|
|
arch_dma_cache_wback(paddr, size);
|
|
|
|
else
|
|
|
|
arch_dma_cache_wback_inv(paddr, size);
|
2022-07-06 16:15:35 -07:00
|
|
|
break;
|
2023-08-16 16:23:36 -07:00
|
|
|
|
2022-07-06 16:15:35 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
|
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
switch (dir) {
|
|
|
|
case DMA_TO_DEVICE:
|
|
|
|
break;
|
2023-08-16 16:23:36 -07:00
|
|
|
|
2022-07-06 16:15:35 -07:00
|
|
|
case DMA_FROM_DEVICE:
|
|
|
|
case DMA_BIDIRECTIONAL:
|
2023-08-16 16:23:36 -07:00
|
|
|
/* FROM_DEVICE invalidate needed if speculative CPU prefetch only */
|
|
|
|
if (arch_sync_dma_cpu_needs_post_dma_flush())
|
|
|
|
arch_dma_cache_inv(paddr, size);
|
2022-07-06 16:15:35 -07:00
|
|
|
break;
|
2023-08-16 16:23:36 -07:00
|
|
|
|
2022-07-06 16:15:35 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arch_dma_prep_coherent(struct page *page, size_t size)
|
|
|
|
{
|
|
|
|
void *flush_addr = page_address(page);
|
|
|
|
|
2023-08-18 06:57:20 -07:00
|
|
|
#ifdef CONFIG_RISCV_NONSTANDARD_CACHE_OPS
|
|
|
|
if (unlikely(noncoherent_cache_ops.wback_inv)) {
|
|
|
|
noncoherent_cache_ops.wback_inv(page_to_phys(page), size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-15 08:40:44 -07:00
|
|
|
ALT_CMO_OP(FLUSH, flush_addr, size, riscv_cbom_block_size);
|
2022-07-06 16:15:35 -07:00
|
|
|
}
|
|
|
|
|
2024-04-19 09:54:46 -07:00
|
|
|
void arch_setup_dma_ops(struct device *dev, bool coherent)
|
2022-07-06 16:15:35 -07:00
|
|
|
{
|
|
|
|
WARN_TAINT(!coherent && riscv_cbom_block_size > ARCH_DMA_MINALIGN,
|
|
|
|
TAINT_CPU_OUT_OF_SPEC,
|
|
|
|
"%s %s: ARCH_DMA_MINALIGN smaller than riscv,cbom-block-size (%d < %d)",
|
|
|
|
dev_driver_string(dev), dev_name(dev),
|
|
|
|
ARCH_DMA_MINALIGN, riscv_cbom_block_size);
|
|
|
|
|
|
|
|
WARN_TAINT(!coherent && !noncoherent_supported, TAINT_CPU_OUT_OF_SPEC,
|
|
|
|
"%s %s: device non-coherent but no non-coherent operations supported",
|
|
|
|
dev_driver_string(dev), dev_name(dev));
|
|
|
|
|
|
|
|
dev->dma_coherent = coherent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void riscv_noncoherent_supported(void)
|
|
|
|
{
|
2022-09-12 15:48:01 -07:00
|
|
|
WARN(!riscv_cbom_block_size,
|
|
|
|
"Non-coherent DMA support enabled without a block size\n");
|
2022-07-06 16:15:35 -07:00
|
|
|
noncoherent_supported = true;
|
|
|
|
}
|
riscv: allow kmalloc() caches aligned to the smallest value
Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E
64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel
Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus
it brings some bad effects to coherent platforms:
Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and
kmalloc-8 slab caches don't exist any more, they are replaced with
either kmalloc-128 or kmalloc-64.
Secondly, larger than necessary kmalloc aligned allocations results
in unnecessary cache/TLB pressure.
This issue also exists on arm64 platforms. From last year, Catalin
tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from
ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to
dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage
in various drivers with ARCH_DMA_MINALIGN etc.[1]
One fact we can make use of for riscv: if the CPU doesn't support
ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on
Catalin's work and above fact, we can easily solve the kmalloc align
issue for riscv: we can override dma_get_cache_alignment(), then let
it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know
the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.
So what about if the CPU supports ZICBOM or T-HEAD CMO, but all the
devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the
kmalloc minimum alignment, nothing changed in this case. This case
can be improved in the future.
After this patch, a simple test of booting to a small buildroot rootfs
on qemu shows:
kmalloc-96 5041 5041 96 ...
kmalloc-64 9606 9606 64 ...
kmalloc-32 5128 5128 32 ...
kmalloc-16 7682 7682 16 ...
kmalloc-8 10246 10246 8 ...
So we save about 1268KB memory. The saving will be much larger in normal
OS env on real HW platforms.
Link: https://lore.kernel.org/linux-arm-kernel/20230524171904.3967031-1-catalin.marinas@arm.com/ [1]
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230718152214.2907-2-jszhang@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-07-18 08:22:13 -07:00
|
|
|
|
|
|
|
void __init riscv_set_dma_cache_alignment(void)
|
|
|
|
{
|
|
|
|
if (!noncoherent_supported)
|
|
|
|
dma_cache_alignment = 1;
|
|
|
|
}
|