From patchwork Sun Nov 26 16:25:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jisheng Zhang X-Patchwork-Id: 13468893 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87E6AC07CA9 for ; Sun, 26 Nov 2023 16:38:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=R3ZmHbZPNRfRx2ZuPRcwJjrhkTXiUJkuyHpEap2PuAU=; b=aCmSCEuGXw+CpA q9I6wkyHkk2ls+sNQxrMOuB84IXi3iJKUsRhfqajLdAL0sMRobpuwkz3Lr7rEsaWmnMzW1WNOHJRR cOuIZTA4b+/ljwhlK1j1ma8zGeXp3v8Zomi5SibvLCqEJdA3qpSlACsAUtuCKBWwph5nm5MsEh6CL 9SY47FohyRTk9ahlpdaCy3gzc57aCFgjMXsPTUrUWKZc3TtreADh+7G9QlSpuuVmfxTJtWTlDBepp ruHTjrvuOp0+oMWVBu1eQqGAv2dQPsOSw6HNGvdv4fa6jTCsSVIEprGB2PLUJg/lNU36xuEO0OOS3 vW3AehQBZV1Sz6+dtY7A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r7I8t-00CxNw-0z; Sun, 26 Nov 2023 16:37:59 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r7I8q-00CxLY-0K for linux-riscv@lists.infradead.org; Sun, 26 Nov 2023 16:37:57 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3FD3A60ED6; Sun, 26 Nov 2023 16:37:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2236C433C7; Sun, 26 Nov 2023 16:37:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701016674; bh=14LKXLMt/+5p7qNVS51zb2f8quhJJfzs+4w5ju16qQE=; h=From:To:Cc:Subject:Date:From; b=mt7oHGf9eqanTX1nkP81wW4zCU1I2igViCVsXynMsPOkiJ6FEJmGV3NY3UUTdBhbj 1Pj5QIjP6KqCyvS7c2o5SObZ73Os35SJblEnA86rrUC2Pq/Q6cRqf/pLj2r8cvXTKI J9ojaEZvebtfR1eAn00K/dtPAx6ldxX9WhHRCOP7ZYJ4D5xWD/dd2sk0MTqLmpR2sf y0FH0hkb9xRAWaNDFq91uV+HkipxIhLnGdQGEZe4pCGvxQaVoyrhPSsE07JFRUwqTa icYlYgsGQWf5zPKFq/5j2XdxddlL7yX5q4BjVli5kdSgsECw7Dtbrwfta+pHqpCYXi yfPhtdRPfHtjw== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] riscv: mm: still create swiotlb buffer for kmalloc() bouncing if required Date: Mon, 27 Nov 2023 00:25:28 +0800 Message-Id: <20231126162528.2411-1-jszhang@kernel.org> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231126_083756_207938_49B0793F X-CRM114-Status: GOOD ( 11.15 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 --- arch/riscv/mm/init.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 2e011cbddf3a..cbcb9918f721 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -162,11 +162,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();