riscv: mm: still create swiotlb buffer for kmalloc() bouncing if required
authorJisheng Zhang <jszhang@kernel.org>
Mon, 25 Mar 2024 11:00:36 +0000 (19:00 +0800)
committerPalmer Dabbelt <palmer@rivosinc.com>
Tue, 30 Apr 2024 17:35:45 +0000 (10:35 -0700)
After commit f51f7a0fc2f4 ("riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC
for !dma_coherent"), for non-coherent platforms with less than 4GB
memory, we rely on users to pass "swiotlb=mmnn,force" kernel parameters
to enable DMA bouncing for unaligned kmalloc() buffers. Now let's go
further: If no bouncing needed for ZONE_DMA, let kernel automatically
allocate 1MB swiotlb buffer per 1GB of RAM for kmalloc() bouncing on
non-coherent platforms, so that no need to pass "swiotlb=mmnn,force"
any more.

The math of "1MB swiotlb buffer per 1GB of RAM for kmalloc() bouncing"
is taken from arm64. Users can still force smaller swiotlb buffer by
passing "swiotlb=mmnn".

Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240325110036.1564-1-jszhang@kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/include/asm/cache.h
arch/riscv/mm/init.c

index 2174fe7bac9ac352a1bd72d12972d4993c8bf882..570e9d8acad1e5298e9228cc6124c24e808dd9b5 100644 (file)
@@ -26,8 +26,8 @@
 
 #ifndef __ASSEMBLY__
 
-#ifdef CONFIG_RISCV_DMA_NONCOHERENT
 extern int dma_cache_alignment;
+#ifdef CONFIG_RISCV_DMA_NONCOHERENT
 #define dma_get_cache_alignment dma_get_cache_alignment
 static inline int dma_get_cache_alignment(void)
 {
index 8631b33368eccdd9f59a02dcadfc855317ee0a46..2574f6a3b0e7999b1a0b1a4bc60567150565f7af 100644 (file)
@@ -161,11 +161,25 @@ static void print_vm_layout(void) { }
 
 void __init mem_init(void)
 {
+       bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit);
 #ifdef CONFIG_FLATMEM
        BUG_ON(!mem_map);
 #endif /* CONFIG_FLATMEM */
 
-       swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE);
+       if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
+           dma_cache_alignment != 1) {
+               /*
+                * If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb
+                * buffer per 1GB of RAM for kmalloc() bouncing on
+                * non-coherent platforms.
+                */
+               unsigned long size =
+                       DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+               swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
+               swiotlb = true;
+       }
+
+       swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
        memblock_free_all();
 
        print_vm_layout();