mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bastien Curutchet <bastien.curutchet@bootlin.com>
To: "Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	 linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	 linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org,
	 Bastien Curutchet <bastien.curutchet@bootlin.com>
Subject: [PATCH 3/3] uio: Add UIO_DMABUF_HEAP
Date: Thu, 10 Apr 2025 16:53:20 +0200	[thread overview]
Message-ID: <20250410-uio-dma-v1-3-6468ace2c786@bootlin.com> (raw)
In-Reply-To: <20250410-uio-dma-v1-0-6468ace2c786@bootlin.com>

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 <bastien.curutchet@bootlin.com>
---
 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 <linux/dma-buf.h>
+#include <linux/dma-heap.h>
+#include <linux/uio_driver.h>
+
+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 */
 
 /**

-- 
2.49.0


  parent reply	other threads:[~2025-04-10 14:53 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10 14:53 [PATCH 0/3] uio/dma-buf: Give UIO users access to DMA addresses Bastien Curutchet
2025-04-10 14:53 ` [PATCH 1/3] dma-buf: Allow heap that doesn't provide map_buf/unmap_buf Bastien Curutchet
2025-04-10 14:53 ` [PATCH 2/3] dma-buf: Add DMA_BUF_IOCTL_GET_DMA_ADDR Bastien Curutchet
2025-04-11 18:34   ` Nicolas Dufresne
2025-04-29  6:39     ` Simona Vetter
2025-04-29  8:12       ` Christian König
2025-04-10 14:53 ` Bastien Curutchet [this message]
2025-04-11 18:41   ` [PATCH 3/3] uio: Add UIO_DMABUF_HEAP Nicolas Dufresne
2025-04-10 16:29 ` [PATCH 0/3] uio/dma-buf: Give UIO users access to DMA addresses Christian König
2025-04-10 19:43   ` Thomas Petazzoni
     [not found]     ` <b596c9af-c0e3-4557-b45a-462a33179235@amd.com>
2025-04-11  8:14       ` Bastien Curutchet
2025-04-11 12:41         ` Christian König
2025-04-14  8:17           ` Thomas Petazzoni
2025-04-14  8:59             ` Christian König
2025-04-14  5:55 ` Christoph Hellwig
2025-04-14  8:24   ` Thomas Petazzoni
2025-04-14  9:11     ` Christian König
2025-04-14 11:52       ` Thomas Petazzoni
2025-04-14 11:24     ` Christoph Hellwig
2025-04-14 11:48       ` Thomas Petazzoni
2025-04-14 17:08         ` Andrew Davis
2025-04-14 19:21           ` Thomas Petazzoni
2025-04-14 20:13             ` Andrew Davis
2025-04-22 18:16             ` Jason Gunthorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250410-uio-dma-v1-3-6468ace2c786@bootlin.com \
    --to=bastien.curutchet@bootlin.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=sumit.semwal@linaro.org \
    --cc=thomas.petazzoni@bootlin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®