From patchwork Thu Apr 10 14:53:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bastien Curutchet X-Patchwork-Id: 14046676 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 639F5C3601E for ; Thu, 10 Apr 2025 15:17:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BE3AB10E9D5; Thu, 10 Apr 2025 15:17:04 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=bootlin.com header.i=@bootlin.com header.b="WONdSUJ8"; dkim-atps=neutral Received: from mslow3.mail.gandi.net (mslow3.mail.gandi.net [217.70.178.249]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5AB1710E9D5 for ; Thu, 10 Apr 2025 15:17:00 +0000 (UTC) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by mslow3.mail.gandi.net (Postfix) with ESMTP id 108785805BE for ; Thu, 10 Apr 2025 14:53:23 +0000 (UTC) Received: by mail.gandi.net (Postfix) with ESMTPSA id 3B06944340; Thu, 10 Apr 2025 14:53:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1744296801; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=U4BA7fAejlBY9PaU8OYnulpA//8kpOAI95crSo0/NRU=; b=WONdSUJ8hAGugdtpL9o6dJ72PwX4ERue/6AhJdlPgrdf8BZlwzcP7lHvl43EWtwWNe7NKf CItQ6N5oWCZBFZhVTd3eWTa50iQeviBTw5mc1XgEVijV7HhvFAI5CUWxvZRD3lVwAkp9AI U89xIk/qFghdV8dJAjt8Wuefj3ddKtC6RVffHEO9Wg5ZtOqOPo+mkW3eTNVt3A0T217rJo rF4rGJe2f/+QDIoRPbyYrQGiYnQBhCnty2xEmXjZDzUn52m2l3wke/BQJyOrZhqWV83kzo HxsTAhOkOb1BIHp3+LBjUA1n7Nv1ikRc8Vn4m2nITwrQypcRJIf7U4t5y1/4mQ== From: Bastien Curutchet Date: Thu, 10 Apr 2025 16:53:18 +0200 Subject: [PATCH 1/3] dma-buf: Allow heap that doesn't provide map_buf/unmap_buf MIME-Version: 1.0 Message-Id: <20250410-uio-dma-v1-1-6468ace2c786@bootlin.com> References: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> In-Reply-To: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> To: Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Greg Kroah-Hartman Cc: Thomas Petazzoni , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, Bastien Curutchet X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdelvddtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepuegrshhtihgvnhcuvehurhhuthgthhgvthcuoegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpedtvdehfffgjeduteekgeekuddvffejueevkeektdeggfetkeeghefffeehudethfenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrheegngdpmhgrihhlfhhrohhmpegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopeelpdhrtghpthhtohepshhumhhithdrshgvmhifrghlsehlihhnrghrohdrohhrghdprhgtphhtthhopegthhhrihhsthhirghnrdhkohgvnhhighesrghmugdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdro hhrghdprhgtphhtthhopehgrhgvghhkhheslhhinhhugihfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopehlihhnrghrohdqmhhmqdhsihhgsehlihhsthhsrdhlihhnrghrohdrohhrghdprhgtphhtthhopehlihhnuhigqdhmvgguihgrsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsggrshhtihgvnhdrtghurhhuthgthhgvthessghoohhtlhhinhdrtghomh X-GND-Sasl: bastien.curutchet@bootlin.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" dma_buf_export() rejects the creation of dma_buf that don't implement the map/unmap_buf operations while these operations aren't needed if the buffer isn't shared by the user. Allow dma_buf to be created even if these operations aren't implemented. Add a check of their existence before using them. Signed-off-by: Bastien Curutchet --- drivers/dma-buf/dma-buf.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 5baa83b855156516a0a766bee0789b122473efb3..398418bd9731ad7a3a1f12eaea6a155fa77a22fe 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -631,8 +631,6 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) int ret; if (WARN_ON(!exp_info->priv || !exp_info->ops - || !exp_info->ops->map_dma_buf - || !exp_info->ops->unmap_dma_buf || !exp_info->ops->release)) return ERR_PTR(-EINVAL); @@ -796,6 +794,9 @@ static struct sg_table *__map_dma_buf(struct dma_buf_attachment *attach, struct sg_table *sg_table; signed long ret; + if (!attach->dmabuf->ops->map_dma_buf) + return ERR_PTR(-EINVAL); + sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); if (IS_ERR_OR_NULL(sg_table)) return sg_table; @@ -1002,7 +1003,8 @@ static void __unmap_dma_buf(struct dma_buf_attachment *attach, /* uses XOR, hence this unmangles */ mangle_sg_table(sg_table); - attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); + if (attach->dmabuf->ops->unmap_dma_buf) + attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); } /** From patchwork Thu Apr 10 14:53:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bastien Curutchet X-Patchwork-Id: 14046679 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 C41C0C369A2 for ; Thu, 10 Apr 2025 15:17:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D012B10E9EE; Thu, 10 Apr 2025 15:17:05 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=bootlin.com header.i=@bootlin.com header.b="L748CGEy"; dkim-atps=neutral Received: from mslow3.mail.gandi.net (mslow3.mail.gandi.net [217.70.178.249]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5CC6710E9EE for ; Thu, 10 Apr 2025 15:17:00 +0000 (UTC) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by mslow3.mail.gandi.net (Postfix) with ESMTP id 96898580151 for ; Thu, 10 Apr 2025 14:53:23 +0000 (UTC) Received: by mail.gandi.net (Postfix) with ESMTPSA id B2C5342EF2; Thu, 10 Apr 2025 14:53:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1744296802; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b8CmCxrHUmcZpijI8nj3HAO5vWfDAACyJpqlcTZxtes=; b=L748CGEy+bq7CQSmVscaBJmhUCt5hr5nzmtNbv90B6S29pA5I4LgEYhAMerlikZ6+MvC+Y uZGk/c7hmKfYLigPAicKM4YybY4lLiD3LJFllXkcPq4bOnH30HRQlS6DOabk3Y00DsVJdz z+V8QNx/k7MPXn8768jqQ2WCjGroL8oWqXjOSQlPvZpQbEtl6zEbNq4fM5+9qB96ULRRxw /6LXTJOdHT4iUanRy/xOuclIhd2KXc04t8s4NczTWekJS/EexVvLObnPG1vgvEQ53yEIt2 x86DvBzYj/o1ij5o+c6xdOSDlKujPN19uceM2OmQCUWwdHnd9vkDVN9V/AWyWQ== From: Bastien Curutchet Date: Thu, 10 Apr 2025 16:53:19 +0200 Subject: [PATCH 2/3] dma-buf: Add DMA_BUF_IOCTL_GET_DMA_ADDR MIME-Version: 1.0 Message-Id: <20250410-uio-dma-v1-2-6468ace2c786@bootlin.com> References: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> In-Reply-To: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> To: Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Greg Kroah-Hartman Cc: Thomas Petazzoni , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, Bastien Curutchet X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdelvddtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepuegrshhtihgvnhcuvehurhhuthgthhgvthcuoegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpedtvdehfffgjeduteekgeekuddvffejueevkeektdeggfetkeeghefffeehudethfenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrheegngdpmhgrihhlfhhrohhmpegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopeelpdhrtghpthhtohepshhumhhithdrshgvmhifrghlsehlihhnrghrohdrohhrghdprhgtphhtthhopegthhhrihhsthhirghnrdhkohgvnhhighesrghmugdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdro hhrghdprhgtphhtthhopehgrhgvghhkhheslhhinhhugihfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopehlihhnrghrohdqmhhmqdhsihhgsehlihhsthhsrdhlihhnrghrohdrohhrghdprhgtphhtthhopehlihhnuhigqdhmvgguihgrsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsggrshhtihgvnhdrtghurhhuthgthhgvthessghoohhtlhhinhdrtghomh X-GND-Sasl: bastien.curutchet@bootlin.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" There is no way to transmit the DMA address of a buffer to userspace. Some UIO users need this to handle DMA from userspace. Add a new dma_buf_ops operation that returns the DMA address. Add a new ioctl to transmit this DMA address to userspace. Signed-off-by: Bastien Curutchet --- drivers/dma-buf/dma-buf.c | 21 +++++++++++++++++++++ include/linux/dma-buf.h | 1 + include/uapi/linux/dma-buf.h | 1 + 3 files changed, 23 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 398418bd9731ad7a3a1f12eaea6a155fa77a22fe..cbbb518981e54e50f479c3d1fcf6da6971f639c1 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -454,6 +454,24 @@ static long dma_buf_import_sync_file(struct dma_buf *dmabuf, } #endif +static int dma_buf_get_dma_addr(struct dma_buf *dmabuf, u64 __user *arg) +{ + u64 addr; + int ret; + + if (!dmabuf->ops->get_dma_addr) + return -EINVAL; + + ret = dmabuf->ops->get_dma_addr(dmabuf, &addr); + if (ret) + return ret; + + if (copy_to_user(arg, &addr, sizeof(u64))) + return -EFAULT; + + return 0; +} + static long dma_buf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { @@ -504,6 +522,9 @@ static long dma_buf_ioctl(struct file *file, return dma_buf_import_sync_file(dmabuf, (const void __user *)arg); #endif + case DMA_BUF_IOCTL_GET_DMA_ADDR: + return dma_buf_get_dma_addr(dmabuf, (u64 __user *)arg); + default: return -ENOTTY; } diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 36216d28d8bdc01a9c9c47e27c392413f7f6c5fb..ed4bf15d3ce82e7a86323fff459699a9bc8baa3b 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -285,6 +285,7 @@ struct dma_buf_ops { int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map); void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map); + int (*get_dma_addr)(struct dma_buf *dmabuf, u64 *addr); }; /** diff --git a/include/uapi/linux/dma-buf.h b/include/uapi/linux/dma-buf.h index 5a6fda66d9adf01438619e7e67fa69f0fec2d88d..f3aba46942042de6a2e3a4cca3eb3f87175e29c9 100644 --- a/include/uapi/linux/dma-buf.h +++ b/include/uapi/linux/dma-buf.h @@ -178,5 +178,6 @@ struct dma_buf_import_sync_file { #define DMA_BUF_SET_NAME_B _IOW(DMA_BUF_BASE, 1, __u64) #define DMA_BUF_IOCTL_EXPORT_SYNC_FILE _IOWR(DMA_BUF_BASE, 2, struct dma_buf_export_sync_file) #define DMA_BUF_IOCTL_IMPORT_SYNC_FILE _IOW(DMA_BUF_BASE, 3, struct dma_buf_import_sync_file) +#define DMA_BUF_IOCTL_GET_DMA_ADDR _IOR(DMA_BUF_BASE, 4, __u64 *) #endif From patchwork Thu Apr 10 14:53:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bastien Curutchet X-Patchwork-Id: 14046677 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 69941C3601E for ; Thu, 10 Apr 2025 15:17:08 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5115310E9EA; Thu, 10 Apr 2025 15:17:05 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=bootlin.com header.i=@bootlin.com header.b="hH1poRN5"; dkim-atps=neutral Received: from mslow3.mail.gandi.net (mslow3.mail.gandi.net [217.70.178.249]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5B1E510E9EA for ; Thu, 10 Apr 2025 15:17:00 +0000 (UTC) Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by mslow3.mail.gandi.net (Postfix) with ESMTP id 230535804D7 for ; Thu, 10 Apr 2025 14:53:24 +0000 (UTC) Received: by mail.gandi.net (Postfix) with ESMTPSA id 3DD7F44339; Thu, 10 Apr 2025 14:53:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1744296802; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NO5o2gLsjg/fYr8Sfdfzm0+WOdUe2aREMsUjD6hGuw0=; b=hH1poRN5LKnBl1WVpo//1Utx5CtOETUIJ5r9rsAyKBGXXbp4i7KK3KH+ss6wP+Au+zVLSI Lk53grJWGQ8h1NVzQirQGLKs2QNy45Lh/8EJrESAlcNaC2nIw3Ob+WsQdg2hytGkAqAoLN QUZSv0fCBHIJysyiecYtl6rVOaJ675jtD0Qvc/bYobUwIlkaZ5zJUy3m4NZogy8DO3OjWn jI80cUC6T9CWPu3CqLHpCtPX4NyclOoB4xRYGIqRzhWO9kMsvLdP8+PiAkQuWR9Fqzd6gU nUTl4yL1OkZt5R3Iz87yi60D94C6HbzwggvXAIzfHp+cpRa16+fbs8A0Okz+Sg== From: Bastien Curutchet Date: Thu, 10 Apr 2025 16:53:20 +0200 Subject: [PATCH 3/3] uio: Add UIO_DMABUF_HEAP MIME-Version: 1.0 Message-Id: <20250410-uio-dma-v1-3-6468ace2c786@bootlin.com> References: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> In-Reply-To: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com> To: Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Greg Kroah-Hartman Cc: Thomas Petazzoni , linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, Bastien Curutchet X-Mailer: b4 0.14.2 X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvtdelvddtucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepuegrshhtihgvnhcuvehurhhuthgthhgvthcuoegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpedtvdehfffgjeduteekgeekuddvffejueevkeektdeggfetkeeghefffeehudethfenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrheegngdpmhgrihhlfhhrohhmpegsrghsthhivghnrdgtuhhruhhttghhvghtsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopeelpdhrtghpthhtohepshhumhhithdrshgvmhifrghlsehlihhnrghrohdrohhrghdprhgtphhtthhopegthhhrihhsthhirghnrdhkohgvnhhighesrghmugdrtghomhdprhgtphhtthhopehthhhomhgrshdrphgvthgriiiiohhnihessghoohhtlhhinhdrtghomhdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdro hhrghdprhgtphhtthhopehgrhgvghhkhheslhhinhhugihfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopehlihhnrghrohdqmhhmqdhsihhgsehlihhsthhsrdhlihhnrghrohdrohhrghdprhgtphhtthhopehlihhnuhigqdhmvgguihgrsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepsggrshhtihgvnhdrtghurhhuthgthhgvthessghoohhtlhhinhdrtghomh X-GND-Sasl: bastien.curutchet@bootlin.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Some UIO users need to access DMA addresses from userspace to be able to configure DMA done by the UIO device. Currently there is no way of doing this. Add a UIO_DMABUF_HEAP Kconfig option. When selected, a dma-heap allocator is created for every new UIO device. This allocator only implements 4 basic operations: allocate, release, mmap and get_dma_addr. The buffer allocation is done through dma_alloc_coherent(). Signed-off-by: Bastien Curutchet --- drivers/uio/Kconfig | 9 ++++ drivers/uio/Makefile | 1 + drivers/uio/uio.c | 4 ++ drivers/uio/uio_heap.c | 120 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/uio_driver.h | 2 + 5 files changed, 136 insertions(+) diff --git a/drivers/uio/Kconfig b/drivers/uio/Kconfig index b060dcd7c6350191726c0830a1ae7b9a388ca4bb..2f3b1e57fceb01354b65cc4d39f342f645a238db 100644 --- a/drivers/uio/Kconfig +++ b/drivers/uio/Kconfig @@ -164,4 +164,13 @@ config UIO_DFL opae-sdk/tools/libopaeuio/ If you compile this as a module, it will be called uio_dfl. + +config UIO_DMABUF_HEAP + bool "DMA-BUF UIO Heap" + select DMABUF_HEAPS + help + Choose this option to enable DMA-BUF UIO heap. It will create a new + heap allocator under /dev/dma_heap/ for every UIO device. This + allocator allows userspace applications to allocate DMA buffers and + access their DMA addresses thanks to the DMA_BUF_IOCTL_GET_DMA_HANDLE endif diff --git a/drivers/uio/Makefile b/drivers/uio/Makefile index 1c5f3b5a95cf5b681a843b745a046d7ce123255d..f6696daa36567a4e5d18b1b89ba688057e758400 100644 --- a/drivers/uio/Makefile +++ b/drivers/uio/Makefile @@ -11,3 +11,4 @@ obj-$(CONFIG_UIO_MF624) += uio_mf624.o obj-$(CONFIG_UIO_FSL_ELBC_GPCM) += uio_fsl_elbc_gpcm.o obj-$(CONFIG_UIO_HV_GENERIC) += uio_hv_generic.o obj-$(CONFIG_UIO_DFL) += uio_dfl.o +obj-$(CONFIG_UIO_DMABUF_HEAP) += uio_heap.o diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c index d93ed4e86a174b5bad193a61aa522cd833fe7bb5..f31936a897805a284165cccfee3d66e96acd4b39 100644 --- a/drivers/uio/uio.c +++ b/drivers/uio/uio.c @@ -1046,7 +1046,11 @@ int __uio_register_device(struct module *owner, } } +#if defined(CONFIG_UIO_DMABUF_HEAP) + return add_uio_heap(idev); +#else return 0; +#endif err_request_irq: uio_dev_del_attributes(idev); diff --git a/drivers/uio/uio_heap.c b/drivers/uio/uio_heap.c new file mode 100644 index 0000000000000000000000000000000000000000..2e836b503458e280babba0e0adc4f6d8344efc82 --- /dev/null +++ b/drivers/uio/uio_heap.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +struct uio_heap { + struct dma_heap *heap; + struct device *dev; +}; + +struct uio_heap_buffer { + struct uio_heap *heap; + dma_addr_t dma_addr; + unsigned long len; + void *vaddr; +}; + +static int uio_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) +{ + struct uio_heap_buffer *buffer = dmabuf->priv; + + return dma_mmap_coherent(buffer->heap->dev, vma, buffer->vaddr, + buffer->dma_addr, buffer->len); +} + +static void uio_heap_dma_buf_release(struct dma_buf *dmabuf) +{ + struct uio_heap_buffer *buffer = dmabuf->priv; + + dma_free_coherent(buffer->heap->dev, buffer->len, buffer->vaddr, + buffer->dma_addr); + kfree(buffer); +} + +static int uio_heap_get_dma_addr(struct dma_buf *dmabuf, u64 *addr) +{ + struct uio_heap_buffer *buffer = dmabuf->priv; + + *addr = buffer->dma_addr; + return 0; +} + +static const struct dma_buf_ops uio_heap_buf_ops = { + .mmap = uio_heap_mmap, + .release = uio_heap_dma_buf_release, + .get_dma_addr = uio_heap_get_dma_addr, +}; + +static struct dma_buf *uio_heap_allocate(struct dma_heap *heap, + unsigned long len, + u32 fd_flags, + u64 heap_flags) +{ + struct uio_heap *uio_heap = dma_heap_get_drvdata(heap); + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct uio_heap_buffer *buffer; + struct dma_buf *dmabuf; + + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); + if (!buffer) + return ERR_PTR(-ENOMEM); + + dma_set_coherent_mask(uio_heap->dev, DMA_BIT_MASK(32)); + + buffer->heap = uio_heap; + buffer->len = len; + buffer->vaddr = dma_alloc_coherent(uio_heap->dev, buffer->len, + &buffer->dma_addr, GFP_KERNEL); + if (IS_ERR(buffer->vaddr)) + goto free_buf; + + exp_info.exp_name = dma_heap_get_name(heap); + exp_info.ops = &uio_heap_buf_ops; + exp_info.size = buffer->len; + exp_info.flags = fd_flags; + exp_info.priv = buffer; + dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) + goto free_dma; + + return dmabuf; + +free_dma: + dma_free_coherent(uio_heap->dev, buffer->len, buffer->vaddr, buffer->dma_addr); +free_buf: + kfree(buffer); + + return ERR_PTR(-ENOMEM); +} + +static const struct dma_heap_ops uio_heap_ops = { + .allocate = uio_heap_allocate, +}; + +int add_uio_heap(struct uio_device *uio) +{ + struct dma_heap_export_info exp_info; + struct uio_heap *uio_heap; + + uio_heap = kzalloc(sizeof(*uio_heap), GFP_KERNEL); + if (!uio_heap) + return -ENOMEM; + + uio_heap->dev = &uio->dev; + + /* Use device name as heap name */ + exp_info.name = uio_heap->dev->kobj.name; + exp_info.ops = &uio_heap_ops; + exp_info.priv = uio_heap; + + uio_heap->heap = dma_heap_add(&exp_info); + if (IS_ERR(uio_heap->heap)) { + int ret = PTR_ERR(uio_heap->heap); + + kfree(uio_heap); + return ret; + } + + return 0; +} diff --git a/include/linux/uio_driver.h b/include/linux/uio_driver.h index 18238dc8bfd356a20996ba6cd84f1ff508bbb81c..f8b774d2fa1c7de4b6af881f1e53dfa9f25b3dbf 100644 --- a/include/linux/uio_driver.h +++ b/include/linux/uio_driver.h @@ -143,6 +143,8 @@ extern int __must_check struct device *parent, struct uio_info *info); +extern int add_uio_heap(struct uio_device *uio); + /* use a define to avoid include chaining to get THIS_MODULE */ /**