riscv: mm: fix SWIOTLB initialization for systems with DRAM above 4GB
Subsystems (2)
PATCH v2 1/1 riscv: mm: fix SWIOTLB initialization for systems with DRAM above 4GB
On RISC-V platforms where the entire physical memory (DRAM) resides above the 32-bit address space (i.e., above dma32_phys_limit), the current SWIOTLB initialization logic fails.
This patch addresses two interconnected issues on such platforms:
1. Incorrect 32-bit DMA bounce assumption:
The existing condition max_pfn > PFN_DOWN(dma32_phys_limit) assumes that a 32-bit DMA bounce buffer is required simply because the maximum PFN exceeds the 32-bit limit. However, if all DRAM starts above 4GB, no memory exists below the limit to satisfy this allocation. Fix this by adding a check to ensure memblock_start_of_DRAM() is actually below the 32-bit limit before enforcing 32-bit SWIOTLB.
2. kmalloc() bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-aligned kmalloc(), even if 32-bit DMA bouncing is skipped. Without the SWIOTLB_ANY flag, swiotlb_init() defaults to allocating from low memory, which fails completely when DRAM only exists in high memory. By appending SWIOTLB_ANY to swiotlb_flags, the allocator is permitted to allocate this alignment buffer from high memory.
With this patch, systems with non-coherent DMA and DRAM entirely above 4GB can successfully map the software IO TLB in high memory and boot normally.
---
Changes in v2:
- add Anirudh's TB tag
- Link to v1: https://lore.kernel.org/r/[email protected]
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- arch/riscv/mm/init.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
arch/riscv/mm/init.c +11 -5
@@ -168,7 +168,9 @@ static void print_vm_layout(void) { }void __init arch_mm_preinit (void ) { -bool swiotlb = max_pfn >PFN_DOWN (dma32_phys_limit); +bool swiotlb = max_pfn >PFN_DOWN (dma32_phys_limit) && +memblock_start_of_DRAM () < dma32_phys_limit; +unsigned int swiotlb_flags =SWIOTLB_VERBOSE ;#ifdef CONFIG_FLATMEM BUG_ON (!mem_map);#endif /* CONFIG_FLATMEM */
@@ -176,17 +178,21 @@ void __init arch_mm_preinit(void)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. +* No 32-bit DMA bouncing needed (either all DRAM is within +* the 32-bit limit, or it all starts above it), but +* non-coherent hardware still requires cache-line-aligned +* bounce buffers for kmalloc(). Use SWIOTLB_ANY so that the +* buffer can be allocated from high memory when DRAM starts +* above dma32_phys_limit. Allocate ~1 MB per 1 GB of RAM. */ unsigned long size =DIV_ROUND_UP (memblock_phys_mem_size (),1024 );swiotlb_adjust_size (min (swiotlb_size_or_default (), size)); swiotlb = true; + swiotlb_flags |=SWIOTLB_ANY ; } -swiotlb_init (swiotlb,SWIOTLB_VERBOSE ); +swiotlb_init (swiotlb, swiotlb_flags);print_vm_layout (); }
---
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
change-id: 20260331-fix-riscv-swiotlb-6f1c226071d1
Best regards,
Signature · 1 line
Troy Mitchell <[email protected]>
Quoted 22 lines · Troy Mitchell
On RISC-V platforms where the entire physical memory (DRAM) resides above the 32-bit address space (i.e., above
dma32_phys_limit), the current SWIOTLB initialization logic fails.This patch addresses two interconnected issues on such platforms:
1. Incorrect 32-bit DMA bounce assumption:
The existing conditionmax_pfn > PFN_DOWN(dma32_phys_limit)assumes that a 32-bit DMA bounce buffer is required simply because the maximum PFN exceeds the 32-bit limit. However, if all DRAM starts above 4GB, no memory exists below the limit to satisfy this allocation. Fix this by adding a check to ensurememblock_start_of_DRAM()is actually below the 32-bit limit before enforcing 32-bit SWIOTLB.2.
kmalloc()bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-alignedkmalloc(), even if 32-bit DMA bouncing is skipped. Without theSWIOTLB_ANYflag,swiotlb_init()defaults to allocating from low memory, which fails completely when DRAM only exists in high memory. By appendingSWIOTLB_ANYtoswiotlb_flags, the allocator is permitted to allocate this alignment buffer from high memory.
Quoted 6 lines
With this patch, systems with non-coherent DMA and DRAM entirely above 4GB can successfully map the software IO TLB in high memory and boot normally.
Tested-by Anirudh Srinivasan <[email protected]>Signed-off-by Troy Mitchell <[email protected]>
Just a gentle ping on this series.
Please let me know if anyone has any feedback or if there is anything I should update.
- Troy
Hi Paul,
Quoted 22 lines · Troy Mitchell
On RISC-V platforms where the entire physical memory (DRAM) resides above the 32-bit address space (i.e., above
dma32_phys_limit), the current SWIOTLB initialization logic fails.This patch addresses two interconnected issues on such platforms:
1. Incorrect 32-bit DMA bounce assumption:
The existing conditionmax_pfn > PFN_DOWN(dma32_phys_limit)assumes that a 32-bit DMA bounce buffer is required simply because the maximum PFN exceeds the 32-bit limit. However, if all DRAM starts above 4GB, no memory exists below the limit to satisfy this allocation. Fix this by adding a check to ensurememblock_start_of_DRAM()is actually below the 32-bit limit before enforcing 32-bit SWIOTLB.2.
kmalloc()bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-alignedkmalloc(), even if 32-bit DMA bouncing is skipped. Without theSWIOTLB_ANYflag,swiotlb_init()defaults to allocating from low memory, which fails completely when DRAM only exists in high memory. By appendingSWIOTLB_ANYtoswiotlb_flags, the allocator is permitted to allocate this alignment buffer from high memory.
Quoted 6 lines
With this patch, systems with non-coherent DMA and DRAM entirely above 4GB can successfully map the software IO TLB in high memory and boot normally.
Tested-by Anirudh Srinivasan <[email protected]>Signed-off-by Troy Mitchell <[email protected]>
Do you think you can take this patch in?
Regards
Anirudh Srinivasan
Paul Walmsley
maintainerHi Troy, Anirudh,
a few questions -
Quoted 4 lines · Anirudh Srinivasan
Quoted 3 lines · Troy Mitchell
On RISC-V platforms where the entire physical memory (DRAM) resides above the 32-bit address space (i.e., above
dma32_phys_limit), the current SWIOTLB initialization logic fails.
Is this primarily for the SpacemiT K3? Or are there other platforms that this fixes?
Quoted 10 lines
Quoted 10 lines
This patch addresses two interconnected issues on such platforms:
1. Incorrect 32-bit DMA bounce assumption:
The existing conditionmax_pfn > PFN_DOWN(dma32_phys_limit)assumes that a 32-bit DMA bounce buffer is required simply because the maximum PFN exceeds the 32-bit limit. However, if all DRAM starts above 4GB, no memory exists below the limit to satisfy this allocation. Fix this by adding a check to ensurememblock_start_of_DRAM()is actually below the 32-bit limit before enforcing 32-bit SWIOTLB.
Quoted 3 lines
Quoted 3 lines
2.
kmalloc()bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-alignedkmalloc(), even if 32-bit DMA bouncing is skipped.
I'm not sure I'm parsing this correctly. Shouldn't this read "for *non*-cache-aligned kmalloc()" ? Or am I missing the point?
- Paul
Hi Paul,
Quoted 7 lines · Paul Walmsley
Hi Troy, Anirudh,
a few questions -
On Tue, 14 Jul 2026, Anirudh Srinivasan wrote:
Quoted 7 lines
Quoted 4 lines · Troy Mitchell
Quoted 3 lines
On RISC-V platforms where the entire physical memory (DRAM) resides above the 32-bit address space (i.e., above
dma32_phys_limit), the current SWIOTLB initialization logic fails.Is this primarily for the SpacemiT K3? Or are there other platforms that this fixes?
Blackhole has this similar issue with no DMA32 ranges. This fix applies to that too.
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
This patch addresses two interconnected issues on such platforms:
1. Incorrect 32-bit DMA bounce assumption:
The existing conditionmax_pfn > PFN_DOWN(dma32_phys_limit)assumes that a 32-bit DMA bounce buffer is required simply because the maximum PFN exceeds the 32-bit limit. However, if all DRAM starts above 4GB, no memory exists below the limit to satisfy this allocation. Fix this by adding a check to ensurememblock_start_of_DRAM()is actually below the 32-bit limit before enforcing 32-bit SWIOTLB.Quoted 3 lines
Quoted 3 lines
2.
kmalloc()bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-alignedkmalloc(), even if 32-bit DMA bouncing is skipped.I'm not sure I'm parsing this correctly. Shouldn't this read "for *non*-cache-aligned
kmalloc()" ? Or am I missing the point?- Paul
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
2.
kmalloc()bounce buffer allocation failure on non-coherent systems:
For non-coherent hardware, a bounce buffer is still mandatory for cache-line-alignedkmalloc(), even if 32-bit DMA bouncing is skipped.I'm not sure I'm parsing this correctly. Shouldn't this read "for *non*-cache-aligned
kmalloc()" ? Or am I missing the point?
You are right; this should refer to kmalloc() buffers whose size is not cache-line-aligned. dma_kmalloc_needs_bounce() requests bouncing only when dma_kmalloc_size_aligned(size) is false for a non-coherent device and a direction other than DMA_TO_DEVICE. I will correct both the commit message and the corresponding code comment in the next revision.
- Troy