From patchwork Tue Jul 2 09:09:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13719173 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BCCBC30658 for ; Tue, 2 Jul 2024 09:10:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E794C6B00AA; Tue, 2 Jul 2024 05:10:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E28876B00AB; Tue, 2 Jul 2024 05:10:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDB746B00AC; Tue, 2 Jul 2024 05:10:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9E85B6B00AA for ; Tue, 2 Jul 2024 05:10:36 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4F84681FD2 for ; Tue, 2 Jul 2024 09:10:36 +0000 (UTC) X-FDA: 82294242072.19.BE052BD Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 94FEA40013 for ; Tue, 2 Jul 2024 09:10:34 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YYKS1Ot/"; spf=pass (imf04.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719911422; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Sg8VrrbO63E4vxe03RzTRxr3bmVdW29o9ynUGItU87Y=; b=TE7nVyka9jHANOCuoqRprWqJlHSbyXcpB/K1FAJ4k/E9mLNZtXtHLFsBz5+y5xHpGcp3Yc HNHxd9MUs8huINfYqOL9FkSbIWOZvaup+5/X9MHK9KZ9IBqDyR36O7+oCJ5HZGW3A34KXf Grd6voMRFNe58UkLff1f6XSppHImISQ= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YYKS1Ot/"; spf=pass (imf04.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719911422; a=rsa-sha256; cv=none; b=XZfE6rG58LjC6tnJ4+eTo30ClPks0IKOnFAzU0Jmx4ILP5wY3t/GmyrurCimvUh9ct5oq2 DZx5JSA2jpfSmEGgTi2xGlwXJmFo58Hur8kBjck8+67/7hQw94RFT1pnXXdjRKClTRFfRR uS0JqgHXksakhSkVSZERfTws2Moyrg4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C632061A0C; Tue, 2 Jul 2024 09:10:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD899C4AF0A; Tue, 2 Jul 2024 09:10:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719911433; bh=0ck5rd5jPLMOP1nHX+50lNZNnmmRrxabk/SQIbsa6U4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YYKS1Ot/toIO1bChF3hjNIXQnLRGAFZW60cYZGmLLcwlJGabkFTKyyB9S4UjzMRw4 OWdB+gcPZfiFupAaORoIq2eLPt1FH+wTYrENPy5qAeVHARSxgi1nIntNE+I24Dz9VP HGwRhb2VAiNoV6XRMQ24UVg2ee/gd+HohgenhbH1Ffqm3bb0/puHq59LU7IGpEL/vk sVPeLGHK/A9Eqt9VnWOf9HrTGNvjmQUyWmuLi3tGXthIBjVPGtIFwFF4zX749XQYhs NGGEjZMQlBISbiHromVsoVz6nukh5hle9SkHC3Rj4ksdesgD1SzycXc0OVeUTNAhcG ADXWSXiEd0N1A== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Keith Busch , Christoph Hellwig , "Zeng, Oak" , Chaitanya Kulkarni Cc: Leon Romanovsky , Sagi Grimberg , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1 04/18] dma-mapping: implement link range API Date: Tue, 2 Jul 2024 12:09:34 +0300 Message-ID: <8944a1211b243fed1234a56bc8004a11dbf85a87.1719909395.git.leon@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 94FEA40013 X-Stat-Signature: rcqm86kw5u6x8ayd77pcym6991rj8y6o X-Rspam-User: X-HE-Tag: 1719911434-996644 X-HE-Meta: U2FsdGVkX19f9j1oWHTe05mEVAwLyL45SnrsONoj6lyv+lCpPXsC4SnriTefe+WMWj7VcSpeQN+PwS1iVVENVpXg2xna8TgR4TIgFSLNcvWOD9Lqrdn6dfk8VE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zYHRc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Leon Romanovsky Introduce new DMA APIs to perform DMA linkage of buffers in layers higher than DMA. In proposed API, the callers will perform the following steps: dma_alloc_iova() if (dma_can_use_iova(...)) dma_start_range(...) for (page in range) dma_link_range(...) dma_end_range(...) else /* Fallback to legacy map pages */ dma_map_page(...) Signed-off-by: Leon Romanovsky --- include/linux/dma-map-ops.h | 6 +++ include/linux/dma-mapping.h | 22 +++++++++++ kernel/dma/mapping.c | 78 ++++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index b52e9c8db241..4868586b015e 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -90,6 +90,12 @@ struct dma_map_ops { dma_addr_t (*alloc_iova)(struct device *dev, size_t size); void (*free_iova)(struct device *dev, dma_addr_t dma_addr, size_t size); + int (*link_range)(struct dma_iova_state *state, phys_addr_t phys, + dma_addr_t addr, size_t size); + void (*unlink_range)(struct dma_iova_state *state, + dma_addr_t dma_handle, size_t size); + int (*start_range)(struct dma_iova_state *state); + void (*end_range)(struct dma_iova_state *state); }; #ifdef CONFIG_DMA_OPS diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 9d1e020869a6..c530095ff232 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -11,6 +11,7 @@ #include #include #include +#include /** * List of possible attributes associated with a DMA mapping. The semantics @@ -103,6 +104,8 @@ struct dma_iova_attrs { struct dma_iova_state { struct dma_iova_attrs *iova; struct dma_memory_type *type; + struct iommu_domain *domain; + size_t range_size; }; #ifdef CONFIG_DMA_API_DEBUG @@ -184,6 +187,10 @@ int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, void dma_get_memory_type(struct page *page, struct dma_memory_type *type); bool dma_can_use_iova(struct dma_iova_state *state, size_t size); +int dma_start_range(struct dma_iova_state *state); +void dma_end_range(struct dma_iova_state *state); +int dma_link_range(struct dma_iova_state *state, phys_addr_t phys, size_t size); +void dma_unlink_range(struct dma_iova_state *state); #else /* CONFIG_HAS_DMA */ static inline int dma_alloc_iova(struct dma_iova_attrs *iova) { @@ -329,6 +336,21 @@ static inline bool dma_can_use_iova(struct dma_iova_state *state, size_t size) { return false; } +static inline int dma_start_range(struct dma_iova_state *state) +{ + return -EOPNOTSUPP; +} +static inline void dma_end_range(struct dma_iova_state *state) +{ +} +static inline int dma_link_range(struct dma_iova_state *state, phys_addr_t phys, + size_t size) +{ + return -EOPNOTSUPP; +} +static inline void dma_unlink_range(struct dma_iova_state *state) +{ +} #endif /* CONFIG_HAS_DMA */ #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 9044ee525fdb..089b4a977bab 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -989,7 +989,8 @@ bool dma_can_use_iova(struct dma_iova_state *state, size_t size) dev_use_swiotlb(dev, size, state->iova->dir)) return false; - if (dma_map_direct(dev, ops) || !ops->alloc_iova) + if (dma_map_direct(dev, ops) || !ops->alloc_iova || !ops->link_range || + !ops->start_range) return false; if (type->type == DMA_MEMORY_TYPE_P2P) { @@ -1000,3 +1001,78 @@ bool dma_can_use_iova(struct dma_iova_state *state, size_t size) return type->type == DMA_MEMORY_TYPE_NORMAL; } EXPORT_SYMBOL_GPL(dma_can_use_iova); + +/** + * dma_start_range - Start a range of IOVA space + * @state: IOVA state + * + * Start a range of IOVA space for the given IOVA state. + */ +int dma_start_range(struct dma_iova_state *state) +{ + struct device *dev = state->iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + + if (!ops->start_range) + return 0; + + return ops->start_range(state); +} +EXPORT_SYMBOL_GPL(dma_start_range); + +/** + * dma_end_range - End a range of IOVA space + * @state: IOVA state + * + * End a range of IOVA space for the given IOVA state. + */ +void dma_end_range(struct dma_iova_state *state) +{ + struct device *dev = state->iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + + if (!ops->end_range) + return; + + ops->end_range(state); +} +EXPORT_SYMBOL_GPL(dma_end_range); + +/** + * dma_link_range - Link a range of IOVA space + * @state: IOVA state + * @phys: physical address to link + * @size: size of the buffer + * + * Link a range of IOVA space for the given IOVA state. + */ +int dma_link_range(struct dma_iova_state *state, phys_addr_t phys, size_t size) +{ + struct device *dev = state->iova->dev; + dma_addr_t addr = state->iova->addr + state->range_size; + const struct dma_map_ops *ops = get_dma_ops(dev); + int ret; + + ret = ops->link_range(state, phys, addr, size); + if (ret) + return ret; + + state->range_size += size; + return 0; +} +EXPORT_SYMBOL_GPL(dma_link_range); + +/** + * dma_unlink_range - Unlink a range of IOVA space + * @state: IOVA state + * + * Unlink a range of IOVA space for the given IOVA state. + */ +void dma_unlink_range(struct dma_iova_state *state) +{ + struct device *dev = state->iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + + ops->unlink_range(state, state->iova->addr, state->range_size); +} +EXPORT_SYMBOL_GPL(dma_unlink_range);