From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.30 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796108874; b=NxaOsQbAC9t2S2ycVMiJM1TLsEMtPny+UrN6BRjFNlul08CczRTZyt1p937dFze915J1 NPGojH3VMQoBHo36nF6Ue5jexk5Yevgqm5EYhY+vCtJv8E4VISrxdqENyn9ndjpgaBfWF dJz48mNx2IcK6kxYtUcgZHqHH5xdC0/gZ4DiLtCnOz2AZ4pjDYKCluldD0Y63KpUG7h+5 gLdfvfTrsXT3MzNNG+c6R08LQikEtmiLt//G0b8J9vQjQd9bbn9ZVEsDCOmCPusB1xEbr yXsGmrYYfCo+m6/B+j6AX07gxhbVmxS+eGV0QMedE1ZHNq3zHNKxWZvfJeRYKuLeLWg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796108874; h=DMARC-Filter:DKIM-Signature:Received:Received:From:Date:Subject: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=Ubq4iZ8UIzEAZmBwHtw+FjCAbzMbW+ZNPSC8ltjHFuE=; b=qQfz054dojvjQbC5NFWjs14LxiY4Ze8fmcdIJyOdUXDH4fryiPlWGhH7y2D5i1EyB7Go sgsA1EJyzPXAoY3Hr5Ep27BM3U1JHrXIuP4eQr/n10HPLTIR+PKMDSDoQ1xuYL5Dn4Cmt qB1srlzbbN79RaX6YQBQYz+b62mHt7DB5V8VZhhqHqPUIKkNSy6pOJf5ZrY2uEzKpxVTR 7GvSpv5Of1xWGOmgKgKC6Gmo9sObiN2cpzi07gt6vwnWyvtkAVXGIxGDRiOomtu4jcWO4 gW+xBLf15MwDF9jx8MMWdr/QlEkgul4onTVL5efFbMMSvLaaMBl+b9HzCne8jJnhXcA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=m5h5c2s5; arc=pass header.oldest-pass=0 smtp.remote-ip=40.93.201.30 Received: from CY3PR05CU001.outbound.protection.outlook.com (mail-westcentralusazon11013030.outbound.protection.outlook.com [40.93.201.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59739606A335; Tue, 1 Dec 2026 07:07:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 59739606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=m5h5c2s5 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=STb34VdbQpaYcOCKOjG5syTTH34ETyzZztX5N3ewPEaBy4t/opU5lxMyxXbTCgArUy59Z+546RmRRmtqzGNgsnfUyI588vQrLOpP8ArersG32VVD0HkJzv50AnOwQ5YdbaxHcU0HaUwwm1e1N9DFXfA702F6CCWEn4FlmX5boCd8k4o/WUcRKfxXxL8cKmPcQpTlqXFPe6EZ50tCqi7C+vuGZm7Qnm0OprppUrlK4uDbRXCskuJ8Uz4Eeppix1CxhgDFbnxiA8vKJ+5n+9sQVevn86WDc8cCAtumen+sDDBlo+0DEkr9DeeB2oqGcp2SqqQJiPGgBWCtD+EZympzBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Ubq4iZ8UIzEAZmBwHtw+FjCAbzMbW+ZNPSC8ltjHFuE=; b=dPQRRnKhoqa06tBLKTEfraBxOXwaaTaocb/QIiJaK6yNlkw9T7UKesz38ZqJnDJ68y5YQcyO7qZNd8NX2v7g/xqlMoKhesXbrxsfvrvwZ2OOAFZkwTslPAaIuubWBKKErG8fUCmv5XxJy6w20BUMx/GcwLBfgiuQhdbJKLe0Ykgd7uLBKXhaw5MM0TACG4R7k7cUt8PHaKy1wr69PejCrbum4DbBSmioSVda2C5V1WhsEKaQHE1AGhY+o8/dEZdMR9JcWAJtyHk3qNASB7RWa+A429Dw9Fz0hTAx93lAkAOBTVLVvig6YiE8uUC14xBMJa19uWCGASXRh3l8x12czw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Ubq4iZ8UIzEAZmBwHtw+FjCAbzMbW+ZNPSC8ltjHFuE=; b=m5h5c2s5yuHm3KEtq6SRaxIudvvZuEjLB0qpH2e3b+APsqkaowzFHOpr9vaaHtC7TQuhFWzg+XYJjGBfWiwgJ37AwFO5XORRAJniN7i7Ndc1ZS3WMegZV+4EpHxajEuwsWxWs7Jd+DHt26l0BvxE2AYumFx6eRB5+aPHHYfwUb1WWXrEMnopAwzhqmc2a5fn4Qc1h7JXavPChhMDIBDQg/tTyu8Ve3/aBhkHzxOleMKYAM1qaS+UZwtn624K5vXhiwDd9SatPKeHrKaDmK5jujBfW8Y1phoHn1V44cYXwCh+rtZV1XTlL5vUXEdWmvflV7ngKiILmWMZFmxGHEN6Yg== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SA3PR12MB598757.namprd12.prod.outlook.com (2603:10b6:806:52d::14) by PH8PR12MB6721.namprd12.prod.outlook.com (2603:10b6:510:1cc::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.12; Tue, 1 Dec 2026 07:07:39 +0000 Received: from SA3PR12MB598757.namprd12.prod.outlook.com ([fe80::e206:100:5946:a777]) by SA3PR12MB598757.namprd12.prod.outlook.com ([fe80::e206:100:5946:a777%5]) with mapi id 15.21.0688.009; Tue, 1 Dec 2026 07:07:39 +0000 From: Alistair Popple Date: Tue, 01 Dec 2026 18:06:23 +1100 Subject: [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-7-b672717c7fc9@nvidia.com> References: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com> In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com> To: Karol Wachowski , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , David Airlie , Simona Vetter , Rodrigo Vivi , Matthew Brost , =?utf-8?q?Thomas_Hellstr=C3=B6m?= , Jason Gunthorpe , Leon Romanovsky , Kees Cook , "Gustavo A. R. Silva" , Alex Williamson , Kevin Tian , "Joerg Roedel (AMD)" , Will Deacon , Robin Murphy Cc: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, intel-xe@lists.freedesktop.org, linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, kvm@vger.kernel.org, iommu@lists.linux.dev, Alexey Kardashevskiy , Mina Almasry , Baolu Lu , chaitanyak@nvidia.com, Dongwon Kim , Felix Kuehling , jhubbard@nvidia.com, Karl Mehltretter , Vivek Kasireddy , Xu Yilun , zhipingz@meta.com, Alistair Popple X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796108809; l=7250; i=apopple@nvidia.com; s=20261105; h=from:subject:message-id; bh=LPpsRcrTxm3gDzhfYKBe/xVqgUBOhsZNhcug9+Y7pjI=; b=L3BRsgmItJOw+i9OLbEchfnrdMvtCgBow4RLG9zUr4XCe2r1wwCnxN8nXHZ2236zV8g+6TG8M nBpSdPSh7SCDiZSCmAnUq/aKBA+8tv25VMrnZIKj1J8+83m52zfioPY X-Developer-Key: i=apopple@nvidia.com; a=ed25519; pk=WdWPT9rAq3YNfXldbGv1NSGVLGxzQGFGJvRUiShhNqs= X-ClientProxiedBy: SY8PR01CA0013.ausprd01.prod.outlook.com (2603:10c6:10:29c::20) To DS4PR12MB634945.namprd12.prod.outlook.com (2603:10b6:8:514::7) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA3PR12MB598757:EE_|PH8PR12MB6721:EE_ X-MS-Office365-Filtering-Correlation-Id: fe0e7baf-ca05-484f-345a-08df4f79c8cc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016|23010399003|5023799004|11063799006|6133799003|261009223027099003|18002099003|22082099003|921020|10067099003|261028144727099003|56012099006|261026121527099003|261102130727099003|261026140227099003|261022075727099003|261021121427099003|261022105027099003|261102123227099003; X-Microsoft-Antispam-Message-Info: j9114zx3UxdLTJHgF6btjNLsJ5OLw1uBG6RZHXpbOPwIsCD8z/OXXIUKWeEbxI/ZEu7ZmujxROsSOi1nSauKNvFuR/GbcqMw3hYuLLQ7qEUEQnMD2MmPsJKLeI1a2XGcyy/rejvmegSMHLnR7+HODv1drHNCBh+iOuBYJpyFfYGBI/ctyLnv092Qmic/ouuahw4iV32alEEGIVGrGXvXjE3fWWGGnYN/A9P5Kl30bHA++F0/fyHT1TbhcT2TWdXweQwqga3M7MjjIVT3+KPtHDYQGdcwR2CHrrmC1qIYkIoMYEh1jkY0cA01F8eQs9E5JOAwIajvvvld6U1TbD26WQfCkbeG8SbqPu+iFFpkUgmUcQEZZtzF+JGnkOPYe1H/XLX472lRK/UAjTQrTNsYyR+e1SMAgrOn75cgaA0uIDSSkCTg+F1LmpMkCWMENotR0i3rnMH/WBd5XFv6u0NW0B9O1qZHbDHyTPs5pwYF3jR1PlizJeRTkW9fFVl14W3HAGMtznBSfExDOonZH2BiCrxMO4ci0kcRZ96RLRznImFH9F/jSBhdMuXf03c0+/pMKU4yUWZbNdpaY3pZU2wgKBsYw+LnOTUaWUf2Oqh1YqeCW0n781OJUTPFO9T/veWnJ5OaTO+DkDa+lLGCVQUcYiQyaVgM1D3ZQKclnfJV76ozwUw+bSilRpCYViRX4GVHzPMS7y9SDK6X7hkznnGsyg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA3PR12MB598757.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(366016)(23010399003)(5023799004)(11063799006)(6133799003)(261009223027099003)(18002099003)(22082099003)(921020)(10067099003)(261028144727099003)(56012099006)(261026121527099003)(261102130727099003)(261026140227099003)(261022075727099003)(261021121427099003)(261022105027099003)(261102123227099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?Q1hvdXNscUd1VGVyMm0rcU0wUnpJUXlUdlFpaVRlVGpQZXlHanN4MjNTVUZn?= =?utf-8?B?d0x0MC9GVkhhbk5raTVZN0dHdjJiMTVaYndCaDZkbDgxMWFCWUhGc05PT3JO?= =?utf-8?B?SVVNVW9FRW51N2ZsTzVldE5McThJYkpTZ1o3Vk5mS053OXduK1lISFd1TWJ2?= =?utf-8?B?Rm5tVDFzWlhuSVkvSHJuVDBoUURReGFOL3NVK09lcjNTZVlxTUR3Wk14OGRm?= =?utf-8?B?SVNCa2Jpb1kzVjJKNVAxY2tiRGJVTk01RzNFa2ZqNm4zQTQ4WDZmSFNheFFX?= =?utf-8?B?ODNObEtOSlJWb3JidXJLdGtFUDRYeE5qRk1aMWsrN2tGbjllM2ZLTE9ZZ3da?= =?utf-8?B?MVk2QWdhSDVUWlFiT1psUTExZnZUZEJ0YWVoa1lzRnl2by80Ylh6SzQvNXFi?= =?utf-8?B?Sml6ZEFNZytJVlJCZ25oaTZPOTZid21aTVZaMzArTGM0MVpRMEhSZEQzTW5E?= =?utf-8?B?dVo4YjhoRjhxK0Q1RTlSNkdNM3hWcUg2WU9QTU5RQXFnSm5YL0t4a0tHZ3FK?= =?utf-8?B?SjBUSXFQNnl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zz09?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe0e7baf-ca05-484f-345a-08df4f79c8cc X-MS-Exchange-CrossTenant-AuthSource: DS4PR12MB634945.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2026 07:07:39.2912 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: J8KRqjuAxoTfRJgD1+1BAx8CTCZxxrbr39MPYuwY3nGQoYYBAPae8LRlhFYvGpvRDvGxQKWlgFlhARA6q+76cA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6721 iommufd imports DMA-bufs to program their memory into IOMMU domains. Today it does so through a private interface with vfio-pci that hands it the address of the BAR, but this doesn't work with other drivers such as GPU drivers meaning their DMA-bufs cannot be used with iommufd to orchestrate things such as direct PCIe P2P storage access. So add an importer for IOMMU domains. The iommufd importer calls dma_buf_iommu_map() with its domain, an IOVA and a window into the buffer, and dma_buf_iommu_unmap() to undo it. The core gets the dma_shared_buf from the exporter and has dma_shared_buf_iommu_map() program the part inside the window into the domain, marking it IOMMU_MMIO when the dma_shared_buf has a p2pdma provider. Only an exporter implementing the map_dma_shared_buf() ops can be mapped this way. The functions are exported to iommufd only as there are no other users of this functionality. Signed-off-by: Alistair Popple --- drivers/dma-buf/dma-buf-mapping.c | 124 ++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf-mapping.h | 15 +++++ 2 files changed, 139 insertions(+) diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c index 5167cda7ea21..ec9776b1dff0 100644 --- a/drivers/dma-buf/dma-buf-mapping.c +++ b/drivers/dma-buf/dma-buf-mapping.c @@ -5,6 +5,7 @@ */ #include #include +#include #include #include @@ -259,3 +260,126 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt, } EXPORT_SYMBOL_NS_GPL(dma_buf_free_sgt, "DMA_BUF"); + +/* + * IOMMU domain importers + * + * An importer that programs an IOMMU domain itself hands over the domain and + * an IOVA window, the exporter hands over its dma_shared_buf, and the core, + * which sees both, has it programmed into the domain. Neither side sees + * the other's data. Only exporters handing out a dma_shared_buf can be mapped + * this way, and only iommufd may ask. + */ + +/** + * dma_buf_iommu_mappable - Can the buffer be mapped into an IOMMU domain + * @dmabuf: the buffer + * + * Lets an importer fail at import time rather than when a domain is first + * filled. + * + * Returns: true when the exporter hands out a dma_shared_buf, so + * dma_buf_iommu_map() can work. + */ +bool dma_buf_iommu_mappable(struct dma_buf *dmabuf) +{ + return dmabuf->ops->map_dma_shared_buf; +} +EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_mappable, "iommufd"); + +/** + * dma_buf_iommu_map - Map a window of the buffer into an IOMMU domain + * @attach: the attachment + * @domain: the domain to program + * @iova: where in @domain the window starts + * @offset: where in the buffer the window starts + * @size: length of the window + * @prot: IOMMU_READ/IOMMU_WRITE/... for the mapping. IOMMU_MMIO is added + * and IOMMU_CACHE removed when the buffer is MMIO. + * @gfp: allocation flags for the page tables + * + * Called with the dma_resv lock held and the attachment pinned. Asks the + * exporter for its dma_shared_buf, has the part of it inside the window + * programmed into @domain and releases it again: nothing about the + * buffer's addresses is kept between calls. The importer undoes the mapping + * with dma_buf_iommu_unmap(), which it must do before the exporter revokes, + * i.e. from its invalidate_mappings(). + * + * The buffer's DMA_RESV_USAGE_KERNEL fences are waited for before the domain + * is programmed, as the importer has no way to wait for them once the memory + * is reachable through @domain. + * + * Returns: 0 on success, -EOPNOTSUPP if the exporter does not hand out a + * dma_shared_buf or the buffer is MMIO and the importer does not allow + * peer2peer, -EINVAL if the window is outside the buffer, -ERESTARTSYS if the + * fence wait is interrupted by a signal, or the error from the exporter or + * iommu_map(). + */ +int dma_buf_iommu_map(struct dma_buf_attachment *attach, + struct iommu_domain *domain, unsigned long iova, + size_t offset, size_t size, int prot, gfp_t gfp) +{ + const struct dma_buf_ops *ops = attach->dmabuf->ops; + struct dma_shared_buf *shared_buf; + long wait; + int ret; + + dma_resv_assert_held(attach->dmabuf->resv); + + if (!ops->map_dma_shared_buf) + return -EOPNOTSUPP; + + shared_buf = ops->map_dma_shared_buf(attach); + if (!shared_buf) + return -ENOMEM; + if (IS_ERR(shared_buf)) + return PTR_ERR(shared_buf); + + if (shared_buf->provider && !attach->peer2peer) { + ret = -EOPNOTSUPP; + goto out_unmap_list; + } + + /* + * The exporter may still be moving the buffer to where it was pinned, + * wait until that is finished. + */ + wait = dma_resv_wait_timeout(attach->dmabuf->resv, + DMA_RESV_USAGE_KERNEL, true, + MAX_SCHEDULE_TIMEOUT); + if (wait < 0) { + ret = wait; + goto out_unmap_list; + } + + ret = dma_shared_buf_iommu_map(shared_buf, domain, iova, offset, + size, prot, gfp); + +out_unmap_list: + ops->unmap_dma_shared_buf(attach, shared_buf); + return ret; +} +EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_map, "iommufd"); + +/** + * dma_buf_iommu_unmap - Undo dma_buf_iommu_map() for a window + * @attach: the attachment the window was mapped for + * @domain: the domain it was mapped into + * @iova: where in @domain the window starts + * @size: length of the window + * + * The exporter is not involved and no lock is needed: this only unmaps the + * IOVA range. It exists so the importer never operates on a buffer's domain + * mappings itself, and must be called before the exporter revokes the + * buffer, so from the importer's invalidate_mappings(). + */ +void dma_buf_iommu_unmap(struct dma_buf_attachment *attach, + struct iommu_domain *domain, unsigned long iova, + size_t size) +{ + size_t unmapped = iommu_unmap(domain, iova, size); + + /* Anything else is a bug here or in the IOMMU driver */ + WARN_ON(unmapped != size); +} +EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_unmap, "iommufd"); diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h index 09bde3f748e4..04fab6bd5dc7 100644 --- a/include/linux/dma-buf-mapping.h +++ b/include/linux/dma-buf-mapping.h @@ -14,4 +14,19 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach, enum dma_data_direction dir); void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt, enum dma_data_direction dir); + +struct iommu_domain; + +/* + * Importers programming an IOMMU domain themselves. Restricted to iommufd: + * the exporter's dma_shared_buf goes into the domain through the core, so + * neither driver sees the other's objects. + */ +bool dma_buf_iommu_mappable(struct dma_buf *dmabuf); +int dma_buf_iommu_map(struct dma_buf_attachment *attach, + struct iommu_domain *domain, unsigned long iova, + size_t offset, size_t size, int prot, gfp_t gfp); +void dma_buf_iommu_unmap(struct dma_buf_attachment *attach, + struct iommu_domain *domain, unsigned long iova, + size_t size); #endif -- 2.54.0