From patchwork Fri Apr 18 06:47:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 14056837 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 2C144C369AB for ; Fri, 18 Apr 2025 06:48:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 733EC2800B3; Fri, 18 Apr 2025 02:48:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DEA12800A8; Fri, 18 Apr 2025 02:48:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C00D2800B3; Fri, 18 Apr 2025 02:48:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 22A602800A8 for ; Fri, 18 Apr 2025 02:48:47 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0AF30141110 for ; Fri, 18 Apr 2025 06:48:48 +0000 (UTC) X-FDA: 83346236736.11.B136739 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 65F34C000B for ; Fri, 18 Apr 2025 06:48:46 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r2GrQrM1; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744958926; a=rsa-sha256; cv=none; b=olLYxcb+P3PLJEcNCmo2ejW67t+TOua3N+QQM1nMCCpN8irU61xtpIjBQ/UCcNIBDE7bqf zEA25riaRh+dQ3B/QC71l3K1463W80HoavrfCBOQhjVmlv9nZaG1GfZCkZwRw4enn/hhMt jGtaF5h3DlWeRRcetrLQG7d9BQ9CUkk= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=r2GrQrM1; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744958926; 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=9H3bcXG/N19CglIEheJVgXiI636IApf7TAb56XgWmn0=; b=hhbGd2qGaDg1Sn+fLeAKZ55He4fOXC8y0wE8YE2+V1QQa0Ybabi7KZeqFAZVm9JzgGH0Ea e6GPPlyT0SdV6JuGS+9Jnbb7NjiU7pdy8bcSF1g6DPmhnQ3R17AzTVJWqzljO9Gn8Xn6So HlflHbx+F58RAag/9Dmc6PAG+k/Nx0g= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 91CFF5C4CB7; Fri, 18 Apr 2025 06:46:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8020C4CEEC; Fri, 18 Apr 2025 06:48:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1744958925; bh=cd0FlyxuFNAWWVLkx8Gv7nFcobsMdup5imOW63rLIiY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r2GrQrM1iJcotnTYpoBOEMQkMd2FYYYR3If5NuISFP3dv6HMdKh+IqFuTbfxEis2U gRHg5BOBFgccqnKlacOkC6ZSySzdhwABdAuS5/7IIzAv3kCZO0DWq7PZtxneGHwBl3 riBWIXXpuZyMV/VDiXr7pL4SqGexiU+kPjoGWmv1g4oYMZ3qQCJKGqCjt6WQuGpIZY 8FQ+XxwCQjfW4pk0NCGwqgAxniWpl7rinqksY8nuZ1NJeFwImJlzrSqrgrYfgNsks4 vf4d71nZDFUWF6AxwUo2hRgF1E83eJ23ywByRkIBButXqhnuXbEfLe0hZQJoHFCO9s 0oSF+CDzo+wgA== From: Leon Romanovsky To: Marek Szyprowski , Jens Axboe , Christoph Hellwig , Keith Busch Cc: Jake Edge , Jonathan Corbet , Jason Gunthorpe , Zhu Yanjun , Robin Murphy , Joerg Roedel , Will Deacon , Sagi Grimberg , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , =?utf-8?b?SsOpcsO0bWUgR2xp?= =?utf-8?b?c3Nl?= , Andrew Morton , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@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, Niklas Schnelle , Chuck Lever , Luis Chamberlain , Matthew Wilcox , Dan Williams , Kanchan Joshi , Chaitanya Kulkarni , Leon Romanovsky Subject: [PATCH v8 09/24] docs: core-api: document the IOVA-based API Date: Fri, 18 Apr 2025 09:47:39 +0300 Message-ID: X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 65F34C000B X-Rspamd-Server: rspam04 X-Stat-Signature: i1ywmu6a73iaeo4d143r9cwmn1893ijb X-HE-Tag: 1744958926-404244 X-HE-Meta: U2FsdGVkX1/M45mM6PPTeQvC1ONlndxRglGV6j3l1W+KXUsPlpo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PeZJcufvNUMdjWao68VT5cgpHcysVen8V7TA5zytIzLdQsl/Qsdn2xj6/e5Znpx6gQjocqbB3eUWvgE5g4dJNH8iK4= 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: Christoph Hellwig Add an explanation of the newly added IOVA-based mapping API. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- Documentation/core-api/dma-api.rst | 71 ++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Documentation/core-api/dma-api.rst b/Documentation/core-api/dma-api.rst index 8e3cce3d0a23..2ad08517e626 100644 --- a/Documentation/core-api/dma-api.rst +++ b/Documentation/core-api/dma-api.rst @@ -530,6 +530,77 @@ routines, e.g.::: .... } +Part Ie - IOVA-based DMA mappings +--------------------------------- + +These APIs allow a very efficient mapping when using an IOMMU. They are an +optional path that requires extra code and are only recommended for drivers +where DMA mapping performance, or the space usage for storing the DMA addresses +matter. All the considerations from the previous section apply here as well. + +:: + + bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state, + phys_addr_t phys, size_t size); + +Is used to try to allocate IOVA space for mapping operation. If it returns +false this API can't be used for the given device and the normal streaming +DMA mapping API should be used. The ``struct dma_iova_state`` is allocated +by the driver and must be kept around until unmap time. + +:: + + static inline bool dma_use_iova(struct dma_iova_state *state) + +Can be used by the driver to check if the IOVA-based API is used after a +call to dma_iova_try_alloc. This can be useful in the unmap path. + +:: + + int dma_iova_link(struct device *dev, struct dma_iova_state *state, + phys_addr_t phys, size_t offset, size_t size, + enum dma_data_direction dir, unsigned long attrs); + +Is used to link ranges to the IOVA previously allocated. The start of all +but the first call to dma_iova_link for a given state must be aligned +to the DMA merge boundary returned by ``dma_get_merge_boundary())``, and +the size of all but the last range must be aligned to the DMA merge boundary +as well. + +:: + + int dma_iova_sync(struct device *dev, struct dma_iova_state *state, + size_t offset, size_t size); + +Must be called to sync the IOMMU page tables for IOVA-range mapped by one or +more calls to ``dma_iova_link()``. + +For drivers that use a one-shot mapping, all ranges can be unmapped and the +IOVA freed by calling: + +:: + + void dma_iova_destroy(struct device *dev, struct dma_iova_state *state, + size_t mapped_len, enum dma_data_direction dir, + unsigned long attrs); + +Alternatively drivers can dynamically manage the IOVA space by unmapping +and mapping individual regions. In that case + +:: + + void dma_iova_unlink(struct device *dev, struct dma_iova_state *state, + size_t offset, size_t size, enum dma_data_direction dir, + unsigned long attrs); + +is used to unmap a range previously mapped, and + +:: + + void dma_iova_free(struct device *dev, struct dma_iova_state *state); + +is used to free the IOVA space. All regions must have been unmapped using +``dma_iova_unlink()`` before calling ``dma_iova_free()``. Part II - Non-coherent DMA allocations --------------------------------------