Skip to content

Commit dcb2743

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: mm: still create swiotlb buffer for kmalloc() bouncing if required
After commit f51f7a0 ("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 <[email protected]> Reviewed-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 0fdbb06 commit dcb2743

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

arch/riscv/include/asm/cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
#ifndef __ASSEMBLY__
2828

29-
#ifdef CONFIG_RISCV_DMA_NONCOHERENT
3029
extern int dma_cache_alignment;
30+
#ifdef CONFIG_RISCV_DMA_NONCOHERENT
3131
#define dma_get_cache_alignment dma_get_cache_alignment
3232
static inline int dma_get_cache_alignment(void)
3333
{

arch/riscv/mm/init.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,25 @@ static void print_vm_layout(void) { }
161161

162162
void __init mem_init(void)
163163
{
164+
bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit);
164165
#ifdef CONFIG_FLATMEM
165166
BUG_ON(!mem_map);
166167
#endif /* CONFIG_FLATMEM */
167168

168-
swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE);
169+
if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
170+
dma_cache_alignment != 1) {
171+
/*
172+
* If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb
173+
* buffer per 1GB of RAM for kmalloc() bouncing on
174+
* non-coherent platforms.
175+
*/
176+
unsigned long size =
177+
DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
178+
swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
179+
swiotlb = true;
180+
}
181+
182+
swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
169183
memblock_free_all();
170184

171185
print_vm_layout();

0 commit comments

Comments
 (0)