Skip to contentSkip to conversation

drm: Add support for userspace drivers

RFC In review Noralf Trønnes 3 participants 10 messages Jan 4, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover drm: Add support for userspace drivers

Hi,

I was previously working on tinydrm as a replacement for staging/fbtft. During a break from that work, I started to think about if it would be possible to move the drivers to userspace instead. No point in having them in the kernel if it's not necessary.

This patchset includes all the pieces necessary to get a userspace driver[1] working that is compatible with the fbtft/fb_ili9341 driver. It is tested with a SPI interfaced display hat/shield for the Raspberry Pi, which has an eeprom with a Device Tree fragment on it (merged by the bootloader). With the help of udev and systemd, the driver is able to autoload when the display is connected.
Performance wise, the kernel driver can do ~19fps on a 320x240 spi@32MHz display, whereas the userspace version does ~18fps. So performance is not an argument to keep the driver in the kernel.

What do you think about this approach?

Note: This patchset is based on 4.9

[1] https://github.com/notro/udrm

Noralf.

Noralf Trønnes (6):
drm/modes: Export drm_mode_convert_umode()
drm: Add support for userspace drivers
dma-buf: Support generic userspace allocations
spi: Let clients do scatter/gather transfers
spi: spidev: Add dma-buf support
spi: spidev: Add userspace driver support

 drivers/dma-buf/Makefile         |   2 +-
 drivers/dma-buf/dev.c            | 211 ++++++++++++++++++
 drivers/gpu/drm/Kconfig          |   2 +
 drivers/gpu/drm/Makefile         |   1 +
 drivers/gpu/drm/drm_modes.c      |   1 +
 drivers/gpu/drm/udrm/Kconfig     |   9 +
 drivers/gpu/drm/udrm/Makefile    |   4 +
 drivers/gpu/drm/udrm/udrm-dev.c  | 276 ++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-drv.c  | 324 ++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-fb.c   | 369 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-pipe.c | 170 +++++++++++++++
 drivers/gpu/drm/udrm/udrm.h      |  84 ++++++++
 drivers/spi/Kconfig              |   1 +
 drivers/spi/spi.c                |  24 ++-
 drivers/spi/spidev.c             | 447 ++++++++++++++++++++++++++++++++++++++-
 include/linux/spi/spi.h          |   4 +
 include/uapi/drm/udrm.h          |  78 +++++++
 include/uapi/linux/dma-buf-dev.h |  35 +++
 include/uapi/linux/spi/spidev.h  |   8 +
 19 files changed, 2032 insertions(+), 18 deletions(-)
 create mode 100644 drivers/dma-buf/dev.c
 create mode 100644 drivers/gpu/drm/udrm/Kconfig
 create mode 100644 drivers/gpu/drm/udrm/Makefile
 create mode 100644 drivers/gpu/drm/udrm/udrm-dev.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-drv.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-fb.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-pipe.c
 create mode 100644 drivers/gpu/drm/udrm/udrm.h
 create mode 100644 include/uapi/drm/udrm.h
 create mode 100644 include/uapi/linux/dma-buf-dev.h

--
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw

RFC 1/6 drm/modes: Export drm_mode_convert_umode()

Export drm_mode_convert_umode() for the benefit of udrm.

---
 drivers/gpu/drm/drm_modes.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/drm_modes.c +1 -0
index 53f07ac..97cf51f 100644
@@ -1555,3 +1555,4 @@ int drm_mode_convert_umode(struct drm_display_mode *out,
 out:
 	return ret;
 }
+EXPORT_SYMBOL(drm_mode_convert_umode);
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

RFC 2/6 drm: Add support for userspace drivers

In reply to Noralf Trønnes

Add support for writing drm userspace drivers.

Userspace driver usage:
Open /dev/udrm
Ioctl create drm driver/device passing in mode, formats and optional
dma-buf as transfer buffer
Read/poll for events:
framebuffer: create, destroy, dirty
pipe: enable, disable
Write back status value from the event execution
Closing file will delete the drm driver.

The reason for doing buffer copy in the kernel is that on a Raspberry Pi copying (actually reading) a mmap'ed 150k dma-buf in userspace took 32ms while in-kernel was 13ms.

---
 drivers/gpu/drm/Kconfig          |   2 +
 drivers/gpu/drm/Makefile         |   1 +
 drivers/gpu/drm/udrm/Kconfig     |   9 +
 drivers/gpu/drm/udrm/Makefile    |   4 +
 drivers/gpu/drm/udrm/udrm-dev.c  | 276 +++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-drv.c  | 324 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-fb.c   | 369 +++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-pipe.c | 170 ++++++++++++++++++
 drivers/gpu/drm/udrm/udrm.h      |  84 +++++++++
 include/uapi/drm/udrm.h          |  78 +++++++++
 10 files changed, 1317 insertions(+)
 create mode 100644 drivers/gpu/drm/udrm/Kconfig
 create mode 100644 drivers/gpu/drm/udrm/Makefile
 create mode 100644 drivers/gpu/drm/udrm/udrm-dev.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-drv.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-fb.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-pipe.c
 create mode 100644 drivers/gpu/drm/udrm/udrm.h
 create mode 100644 include/uapi/drm/udrm.h
drivers/gpu/drm/Kconfig +2 -0
index 483059a..b351798 100644
@@ -223,6 +223,8 @@ source "drivers/gpu/drm/hisilicon/Kconfig"
 
 source "drivers/gpu/drm/mediatek/Kconfig"
 
+source "drivers/gpu/drm/udrm/Kconfig"
+
 # Keep legacy drivers last
 
 menuconfig DRM_LEGACY
drivers/gpu/drm/Makefile +1 -0
index 25c7204..29175bc 100644
@@ -86,3 +86,4 @@ obj-$(CONFIG_DRM_FSL_DCU) += fsl-dcu/
 obj-$(CONFIG_DRM_ETNAVIV) += etnaviv/
 obj-$(CONFIG_DRM_ARCPGU)+= arc/
 obj-y			+= hisilicon/
+obj-y			+= udrm/
drivers/gpu/drm/udrm/Kconfig new file +9 -0
new file mode 100644
index 0000000..41faa4b
@@ -0,0 +1,9 @@
+config DRM_USER
+	tristate "Support for userspace DRM drivers"
+	depends on DRM
+	select DRM_KMS_HELPER
+	select DRM_KMS_CMA_HELPER
+	select VIDEOMODE_HELPERS
+	help
+	  Choose this option if you have a userspace DRM driver.
+	  If M is selected the module will be called tinydrm.
drivers/gpu/drm/udrm/Makefile new file +4 -0
new file mode 100644
index 0000000..9eb8c27
@@ -0,0 +1,4 @@
+ccflags-y += -I$(src)/include
+
+udrm-y := udrm-dev.o udrm-drv.o udrm-fb.o udrm-pipe.o
+obj-$(CONFIG_DRM_USER) += udrm.o
drivers/gpu/drm/udrm/udrm-dev.c new file +276 -0

276 lines not on this page · raw message

drivers/gpu/drm/udrm/udrm-drv.c new file +324 -0

324 lines not on this page · raw message

drivers/gpu/drm/udrm/udrm-fb.c new file +369 -0

369 lines not on this page · raw message

drivers/gpu/drm/udrm/udrm-pipe.c new file +170 -0

170 lines not on this page · raw message

drivers/gpu/drm/udrm/udrm.h new file +84 -0

84 lines not on this page · raw message

include/uapi/drm/udrm.h new file +78 -0

78 lines not on this page · raw message

--
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

RFC 3/6 dma-buf: Support generic userspace allocations

In reply to Noralf Trønnes

Add a generic way for userspace to allocate dma-buf's for SPI transfers.

---
 drivers/dma-buf/Makefile         |   2 +-
 drivers/dma-buf/dev.c            | 211 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/dma-buf-dev.h |  35 +++++++
 3 files changed, 247 insertions(+), 1 deletion(-)
 create mode 100644 drivers/dma-buf/dev.c
 create mode 100644 include/uapi/linux/dma-buf-dev.h
drivers/dma-buf/Makefile +1 -1
index 210a10b..ec867f7 100644
@@ -1,3 +1,3 @@
-obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-array.o
+obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-array.o dev.o
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
 obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
drivers/dma-buf/dev.c new file +211 -0

211 lines not on this page · raw message

include/uapi/linux/dma-buf-dev.h new file +35 -0
new file mode 100644
index 0000000..fddbe04
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 Noralf Trønnes
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef _DMA_BUF_DEV_UAPI_H_
+#define _DMA_BUF_DEV_UAPI_H_
+
+#include <linux/types.h>
+
+/**
+ * struct dma_buf_dev_create -
+ * @attrs: Attributes of mapping properties requested in dma_alloc_attrs
+ * @size: Buffer size
+ * @flags: Mode flags for the dma-buf file
+ * @fd: Returned dma-buf file descriptor
+ */
+struct dma_buf_dev_create {
+	__u64 attrs;
+#define DMA_BUF_DEV_ATTR_WRITE_COMBINE          BIT(2)
+	__u64 size;
+	__u64 flags;
+
+	__s64 fd;
+};
+
+/* FIXME: Update Documentation/ioctl/ioctl-number.txt */
+#define DMA_BUF_DEV_BASE		0xB6
+#define DMA_BUF_DEV_IOCTL_CREATE	_IOWR(DMA_BUF_DEV_BASE, 0, struct dma_buf_dev_create)
+
+#endif
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

Re: [RFC 3/6] dma-buf: Support generic userspace allocations

Quoted 3 lines · Noralf Trønnes

Add a generic way for userspace to allocate dma-buf's for SPI transfers.

Having a central dma-buf allocator is a common thing, there's already ION in drivers/staging/android. If we need one I think it's better to accelarate ION destaging than creating yet another one.

Quoted 148 lines
---
 drivers/dma-buf/Makefile         |   2 +-
 drivers/dma-buf/dev.c            | 211 +++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/dma-buf-dev.h |  35 +++++++
 3 files changed, 247 insertions(+), 1 deletion(-)
 create mode 100644 drivers/dma-buf/dev.c
 create mode 100644 include/uapi/linux/dma-buf-dev.h
drivers/dma-buf/Makefile +1 -1
index 210a10b..ec867f7 100644
@@ -1,3 +1,3 @@
-obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-array.o
+obj-y := dma-buf.o fence.o reservation.o seqno-fence.o fence-array.o dev.o
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
 obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
drivers/dma-buf/dev.c new file +125 -0
new file mode 100644
index 0000000..536d9bf
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2016 Noralf Trønnes
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+
+#include <uapi/linux/dma-buf-dev.h>
+
+struct dma_buf_dev_object {
+	struct device *dev;
+	unsigned long attrs;
+	dma_addr_t dma_addr;
+	void *vaddr;
+	size_t size;
+};
+
+static struct sg_table *
+dma_buf_dev_map_dma_buf(struct dma_buf_attachment *attach,
+			enum dma_data_direction dir)
+{
+	struct dma_buf_dev_object *obj = attach->dmabuf->priv;
+	struct sg_table *sgt;
+	int ret;
+
+	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
+	if (!sgt)
+		return ERR_PTR(-ENOMEM);
+
+	ret = dma_get_sgtable(obj->dev, sgt, obj->vaddr,
+			      obj->dma_addr, obj->size);
+	if (ret < 0)
+		goto err_free;
+
+	if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
+		ret = -ENOMEM;
+		goto err_free_table;
+	}
+
+	return sgt;
+
+err_free_table:
+	sg_free_table(sgt);
+err_free:
+	kfree(sgt);
+
+	return ERR_PTR(ret);
+}
+
+static void dma_buf_dev_unmap_dma_buf(struct dma_buf_attachment *attach,
+				      struct sg_table *sgt,
+				      enum dma_data_direction dir)
+{
+	dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
+	sg_free_table(sgt);
+	kfree(sgt);
+}
+
+static void dma_buf_dev_release(struct dma_buf *dma_buf)
+{
+	struct dma_buf_dev_object *obj = dma_buf->priv;
+
+/* FIXME remove */
+pr_info("%s()\n", __func__);
+	dma_free_attrs(obj->dev, obj->size, obj->vaddr, obj->dma_addr,
+		       obj->attrs);
+	kfree(obj);
+}
+
+static void *dma_buf_dev_kmap(struct dma_buf *dma_buf, unsigned long page_num)
+{
+	struct dma_buf_dev_object *obj = dma_buf->priv;
+
+	return obj->vaddr + page_num * PAGE_SIZE;
+}
+
+static void *dma_buf_dev_vmap(struct dma_buf *dma_buf)
+{
+	struct dma_buf_dev_object *obj = dma_buf->priv;
+
+	return obj->vaddr;
+}
+
+static int dma_buf_dev_mmap(struct dma_buf *dma_buf,
+			    struct vm_area_struct *vma)
+{
+	struct dma_buf_dev_object *obj = dma_buf->priv;
+	int ret;
+
+	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+
+	ret = dma_mmap_attrs(obj->dev, vma, obj->vaddr, obj->dma_addr,
+			     vma->vm_end - vma->vm_start, obj->attrs);
+
+	return ret;
+}
+
+static const struct dma_buf_ops dma_buf_dev_ops =  {
+	.map_dma_buf = dma_buf_dev_map_dma_buf,
+	.unmap_dma_buf = dma_buf_dev_unmap_dma_buf,
+	.release = dma_buf_dev_release,
+	.kmap_atomic = dma_buf_dev_kmap,
+	.kmap = dma_buf_dev_kmap,
+	.vmap = dma_buf_dev_vmap,
+	.mmap = dma_buf_dev_mmap,
+};
+
+struct dma_buf *dma_buf_dev_alloc_attrs(struct device *dev, size_t size,
+					unsigned long attrs, int flags)
+{
+	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
+	struct dma_buf_dev_object *obj;
+	struct dma_buf *dmabuf;
+	int ret;
+
+	if (flags & ~(O_CLOEXEC | O_ACCMODE))
+		return ERR_PTR(-EINVAL);

drivers/dma-buf/dev.c line 135

Quoted 10 lines
drivers/dma-buf/dev.c new file +10 -0
+
+	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+	if (!obj)
+		return ERR_PTR(-ENOMEM);
+
+	obj->dev = dev;
+	obj->size = size;
+	obj->attrs = attrs;
+
+	obj->vaddr = dma_alloc_attrs(dev, size, &obj->dma_addr, GFP_KERNEL, attrs);

Hm, does dma_alloc reall always work with dev == NULL? I had no idea this was possible ...

Cheers, Daniel

Quoted 124 lines
drivers/dma-buf/dev.c new file +76 -0
+	if (!obj->vaddr) {
+		ret = -ENOMEM;
+		goto err_free_obj;
+	}
+
+	exp_info.ops = &dma_buf_dev_ops;
+	exp_info.size = obj->size;
+	exp_info.flags = flags;
+	exp_info.priv = obj;
+
+	dmabuf = dma_buf_export(&exp_info);
+	if (IS_ERR(dmabuf)) {
+		ret = PTR_ERR(dmabuf);
+		goto err_free_buf;
+	}
+
+	return dmabuf;
+
+err_free_buf:
+	dma_free_attrs(dev, size, obj->vaddr, obj->dma_addr, attrs);
+err_free_obj:
+	kfree(obj);
+
+	return ERR_PTR(ret);
+}
+
+static long dma_buf_dev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct dma_buf_dev_create create;
+	struct dma_buf *dmabuf;
+
+	switch (cmd) {
+	case DMA_BUF_DEV_IOCTL_CREATE:
+
+		if (copy_from_user(&create, (void __user *)arg, sizeof(create)))
+			return -EFAULT;
+
+		if (!create.size)
+			return -EINVAL;
+
+		dmabuf = dma_buf_dev_alloc_attrs(NULL, create.size,
+						 create.attrs, create.flags);
+		if (IS_ERR(dmabuf))
+			return PTR_ERR(dmabuf);
+
+		create.fd = dma_buf_fd(dmabuf, create.flags);
+		if (create.fd < 0) {
+			dma_buf_put(dmabuf);
+			return create.fd;
+		}
+
+		if (copy_to_user((void __user *)arg, &create, sizeof(create)))
+			return -EFAULT;
+
+		return 0;
+	default:
+		return -ENOTTY;
+	}
+}
+
+static const struct file_operations dma_buf_dev_fops = {
+	.owner		= THIS_MODULE,
+	.unlocked_ioctl	= dma_buf_dev_ioctl,
+	.compat_ioctl	= dma_buf_dev_ioctl,
+};
+
+static struct miscdevice dma_buf_dev_misc = {
+	.fops		= &dma_buf_dev_fops,
+	.minor		= MISC_DYNAMIC_MINOR,
+	.name		= "dma-buf",
+};
+module_misc_device(dma_buf_dev_misc);
+
+MODULE_AUTHOR("Noralf Trønnes");
+MODULE_DESCRIPTION("User mode dma-buf creation");
+MODULE_LICENSE("GPL");
include/uapi/linux/dma-buf-dev.h new file +35 -0
new file mode 100644
index 0000000..fddbe04
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2016 Noralf Trønnes
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef _DMA_BUF_DEV_UAPI_H_
+#define _DMA_BUF_DEV_UAPI_H_
+
+#include <linux/types.h>
+
+/**
+ * struct dma_buf_dev_create -
+ * @attrs: Attributes of mapping properties requested in dma_alloc_attrs
+ * @size: Buffer size
+ * @flags: Mode flags for the dma-buf file
+ * @fd: Returned dma-buf file descriptor
+ */
+struct dma_buf_dev_create {
+	__u64 attrs;
+#define DMA_BUF_DEV_ATTR_WRITE_COMBINE          BIT(2)
+	__u64 size;
+	__u64 flags;
+
+	__s64 fd;
+};
+
+/* FIXME: Update Documentation/ioctl/ioctl-number.txt */
+#define DMA_BUF_DEV_BASE		0xB6
+#define DMA_BUF_DEV_IOCTL_CREATE	_IOWR(DMA_BUF_DEV_BASE, 0, struct dma_buf_dev_create)
+
+#endif
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

RFC 4/6 spi: Let clients do scatter/gather transfers

In reply to Noralf Trønnes

Just a hack. Someone probably has an idea about how this should be done.

---
 drivers/spi/spi.c       | 24 +++++++++++++++++-------
 include/linux/spi/spi.h |  4 ++++
 2 files changed, 21 insertions(+), 7 deletions(-)
drivers/spi/spi.c +17 -7
index 838783c..b2958be 100644
@@ -808,23 +808,28 @@ static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
 		if (!master->can_dma(master, msg->spi, xfer))
 			continue;
 
-		if (xfer->tx_buf != NULL) {
+		if (xfer->tx_buf != NULL && !xfer->tx_sg.nents) {
 			ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
 					  (void *)xfer->tx_buf, xfer->len,
 					  DMA_TO_DEVICE);
 			if (ret != 0)
 				return ret;
+
+			xfer->tx_sg_core_mapped = true;
 		}
 
-		if (xfer->rx_buf != NULL) {
+		if (xfer->rx_buf != NULL && !xfer->rx_sg.nents) {
 			ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
 					  xfer->rx_buf, xfer->len,
 					  DMA_FROM_DEVICE);
 			if (ret != 0) {
 				spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
 					      DMA_TO_DEVICE);
+				xfer->tx_sg_core_mapped = false;
 				return ret;
 			}
+
+			xfer->rx_sg_core_mapped = true;
 		}
 	}
 
@@ -852,11 +857,16 @@ static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
 		rx_dev = &master->dev;
 
 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
-		if (!master->can_dma(master, msg->spi, xfer))
-			continue;
-
-		spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
-		spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
+		if (xfer->rx_sg_core_mapped) {
+			spi_unmap_buf(master, rx_dev, &xfer->rx_sg,
+				      DMA_FROM_DEVICE);
+			xfer->rx_sg.nents = 0;
+		}
+		if (xfer->tx_sg_core_mapped) {
+			spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
+				      DMA_TO_DEVICE);
+			xfer->tx_sg.nents = 0;
+		}
 	}
 
 	return 0;
include/linux/spi/spi.h +4 -0
index 4b743ac..f27fda6 100644
@@ -682,6 +682,8 @@ extern void spi_res_release(struct spi_master *master,
  * @transfer_list: transfers are sequenced through @spi_message.transfers
  * @tx_sg: Scatterlist for transmit, currently not for client use
  * @rx_sg: Scatterlist for receive, currently not for client use
+ * @tx_sg_core_mapped: Scatterlist has been mapped by spi core
+ * @rx_sg_core_mapped: Scatterlist has been mapped by spi core
  *
  * SPI transfers always write the same number of bytes as they read.
  * Protocol drivers should always provide @rx_buf and/or @tx_buf.
@@ -751,6 +753,8 @@ struct spi_transfer {
 	dma_addr_t	rx_dma;
 	struct sg_table tx_sg;
 	struct sg_table rx_sg;
+	bool		tx_sg_core_mapped;
+	bool		rx_sg_core_mapped;
 
 	unsigned	cs_change:1;
 	unsigned	tx_nbits:3;
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

RFC 5/6 spi: spidev: Add dma-buf support

In reply to Noralf Trønnes

Add support for using dma-buf buffers in transfers from userspace.

FIXME: Backwards compatibility needs to be taken care of somehow.

---
 drivers/spi/Kconfig             |   1 +
 drivers/spi/spidev.c            | 158 +++++++++++++++++++++++++++++++++++++++-
 include/uapi/linux/spi/spidev.h |   8 ++
 3 files changed, 163 insertions(+), 4 deletions(-)
drivers/spi/Kconfig +1 -0
index b799547..ac6bbd1 100644
@@ -731,6 +731,7 @@ comment "SPI Protocol Masters"
 
 config SPI_SPIDEV
 	tristate "User mode SPI device driver support"
+	select SG_SPLIT if DMA_SHARED_BUFFER
 	help
 	  This supports user mode SPI protocol drivers.
 
drivers/spi/spidev.c +154 -4

220 lines not on this page · raw message

include/uapi/linux/spi/spidev.h +8 -0
index dd5f21e..a9b6cd0 100644
@@ -64,6 +64,9 @@
  * @delay_usecs: If nonzero, how long to delay after the last bit transfer
  *	before optionally deselecting the device before the next transfer.
  * @cs_change: True to deselect device before starting the next transfer.
+ * @tx_dma_fd: File descriptor for transmitting dma-buf buffers.
+ * @rx_dma_fd: File descriptor for receiving dma-buf buffers.
+ * @dma_offset: Offset into dma-buf buffer.
  *
  * This structure is mapped directly to the kernel spi_transfer structure;
  * the fields have the same meanings, except of course that the pointers
@@ -100,6 +103,11 @@ struct spi_ioc_transfer {
 	__u8		rx_nbits;
 	__u16		pad;
 
+	__s32		tx_dma_fd;
+	__s32		rx_dma_fd;
+	__u32		dma_offset;
+	__u32		pad2;
+
 	/* If the contents of 'struct spi_ioc_transfer' ever change
 	 * incompatibly, then the ioctl number (currently 0) must change;
 	 * ioctls with constant size fields get a bit more in the way of
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#7 lore raw

RFC 6/6 spi: spidev: Add userspace driver support

In reply to Noralf Trønnes

Add support for spi userspace drivers backed by it's own spi_driver.

Userspace driver usage:
Open /dev/spidev
Write a string containing driver name and optional DT compatible.
This registers a spidev spi_driver.
Read/poll to receive notice when devices have been bound/probed. The driver now uses /dev/spidevN.N as normal to access the device. When the file is closed, the spi_driver is unregistered.

---
 drivers/spi/spidev.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 283 insertions(+), 6 deletions(-)
drivers/spi/spidev.c +283 -6
index 35e6377..b8f3559 100644
@@ -26,11 +26,13 @@
 #include <linux/err.h>
 #include <linux/list.h>
 #include <linux/errno.h>
+#include <linux/miscdevice.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
 #include <linux/compat.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/poll.h>
 #include <linux/acpi.h>
 
 #include <linux/spi/spi.h>
@@ -99,6 +101,20 @@ struct spidev_dmabuf {
 	unsigned int nents;
 };
 
+struct spidev_drv {
+	struct spi_driver	spidrv;
+	struct mutex		event_lock;
+	struct list_head	events;
+	wait_queue_head_t	waitq;
+	struct completion	completion;
+};
+
+struct spidev_drv_event {
+	struct list_head list;
+	u8 bus_num;
+	u8 chip_select;
+};
+
 static LIST_HEAD(device_list);
 static DEFINE_MUTEX(device_list_lock);
 
@@ -994,6 +1010,254 @@ static struct spi_driver spidev_spi_driver = {
 
 /*-------------------------------------------------------------------------*/
 
+static int spidev_drv_probe(struct spi_device *spi)
+{
+	struct spi_driver *spidrv = to_spi_driver(spi->dev.driver);
+	struct spidev_drv *sdrv = container_of(spidrv, struct spidev_drv,
+					       spidrv);
+	struct spidev_drv_event *new_device;
+	int ret;
+
+	ret = spidev_probe(spi);
+	if (ret)
+		return ret;
+
+	ret = mutex_lock_interruptible(&sdrv->event_lock);
+	if (ret)
+		goto out;
+
+	new_device = kzalloc(sizeof(*new_device), GFP_KERNEL);
+	if (new_device) {
+		new_device->bus_num = spi->master->bus_num;
+		new_device->chip_select = spi->chip_select;
+		list_add_tail(&new_device->list, &sdrv->events);
+	} else {
+		ret = -ENOMEM;
+	}
+
+	mutex_unlock(&sdrv->event_lock);
+
+	wake_up_interruptible(&sdrv->waitq);
+out:
+	if (ret)
+		dev_err(&spi->dev, "Failed to add event %d\n", ret);
+
+	return 0;
+}
+
+static int spidev_drv_remove(struct spi_device *spi)
+{
+	int ret;
+
+	ret = spidev_remove(spi);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static ssize_t spidev_drv_write(struct file *file, const char __user *buffer,
+				size_t count, loff_t *ppos)
+{
+	char *str, *token, *drvname, *compatible;
+	struct of_device_id *of_ids = NULL;
+	struct spidev_drv *sdrv = NULL;
+	struct spi_driver *spidrv;
+	unsigned int i;
+	int status;
+
+	if (file->private_data)
+		return -EBUSY;
+
+	if (!count)
+		return 0;
+
+	if (count == 1)
+		return -EINVAL;
+
+	str = strndup_user(buffer, count);
+	if (IS_ERR(str))
+		return PTR_ERR(str);
+
+	for (i = 0, token = str; *token; token++)
+		if (*token == '\n')
+			i++;
+
+	if (i > 1) {
+		status = -EINVAL;
+		goto err_free;
+	}
+
+	drvname = str;
+	if (i) {
+		strsep(&str, "\n");
+		compatible = str;
+	} else {
+		compatible = NULL;
+	}
+
+	if (compatible && strlen(compatible) > 127) {
+		status = -EINVAL;
+		goto err_free;
+	}
+
+pr_info("spidev: Add driver '%s', compatible='%s'\n", drvname, compatible);
+
+	sdrv = kzalloc(sizeof(*sdrv), GFP_KERNEL);
+	if (!sdrv) {
+		status = -ENOMEM;
+		goto err_free;
+	}
+
+	INIT_LIST_HEAD(&sdrv->events);
+	mutex_init(&sdrv->event_lock);
+	init_waitqueue_head(&sdrv->waitq);
+
+	spidrv = &sdrv->spidrv;
+	spidrv->driver.name = drvname;
+	spidrv->probe = spidev_drv_probe;
+	spidrv->remove = spidev_drv_remove;
+
+	if (compatible) {
+		/* the second blank entry is the sentinel */
+		of_ids = kcalloc(2, sizeof(*of_ids), GFP_KERNEL);
+		if (!of_ids) {
+			status = -ENOMEM;
+			goto err_free;
+		}
+		strcpy(of_ids[0].compatible, compatible);
+		spidrv->driver.of_match_table = of_ids;
+	}
+
+	status = spi_register_driver(spidrv);
+	if (status < 0)
+		goto err_free;
+
+	file->private_data = sdrv;
+
+	return count;
+
+err_free:
+	kfree(sdrv);
+	kfree(of_ids);
+	kfree(str);
+
+	return status;
+}
+
+static ssize_t spidev_drv_read(struct file *file, char __user *buffer,
+			       size_t count, loff_t *ppos)
+{
+	struct spidev_drv *sdrv = file->private_data;
+	struct spidev_drv_event *new_device;
+	char str[32];
+	ssize_t ret;
+
+	if (!sdrv)
+		return -ENODEV;
+
+	if (!count)
+		return 0;
+
+	do {
+		ret = mutex_lock_interruptible(&sdrv->event_lock);
+		if (ret)
+			return ret;
+
+		if (list_empty(&sdrv->events)) {
+			if (file->f_flags & O_NONBLOCK)
+				ret = -EAGAIN;
+		} else {
+			new_device = list_first_entry(&sdrv->events,
+						      struct spidev_drv_event,
+						      list);
+			ret = scnprintf(str, sizeof(str) - 1, "spidev%u.%u",
+					new_device->bus_num,
+					new_device->chip_select);
+			if (ret < 0)
+				goto unlock;
+
+			str[ret++] = '\0';
+
+			if (ret > count) {
+				ret = -EINVAL;
+				goto unlock;
+			} else if (copy_to_user(buffer, str, ret)) {
+				ret = -EFAULT;
+				goto unlock;
+			}
+
+			list_del(&new_device->list);
+			kfree(new_device);
+		}
+unlock:
+		mutex_unlock(&sdrv->event_lock);
+
+		if (ret)
+			break;
+
+		if (!(file->f_flags & O_NONBLOCK))
+			ret = wait_event_interruptible(sdrv->waitq,
+						!list_empty(&sdrv->events));
+	} while (ret == 0);
+
+	return ret;
+}
+
+static unsigned int spidev_drv_poll(struct file *file, poll_table *wait)
+{
+	struct spidev_drv *sdrv = file->private_data;
+
+	poll_wait(file, &sdrv->waitq, wait);
+
+	if (!list_empty(&sdrv->events))
+		return POLLIN | POLLRDNORM;
+
+	return 0;
+}
+
+static int spidev_drv_open(struct inode *inode, struct file *file)
+{
+	file->private_data = NULL;
+	nonseekable_open(inode, file);
+
+	return 0;
+}
+
+static int spidev_drv_release(struct inode *inode, struct file *file)
+{
+	struct spidev_drv *sdrv = file->private_data;
+	struct spidev_drv_event *entry, *tmp;
+
+	if (sdrv) {
+		spi_unregister_driver(&sdrv->spidrv);
+		list_for_each_entry_safe(entry, tmp, &sdrv->events, list)
+			kfree(entry);
+		kfree(sdrv->spidrv.driver.name);
+		kfree(sdrv);
+	}
+
+	return 0;
+}
+
+static const struct file_operations spidev_drv_fops = {
+	.owner		= THIS_MODULE,
+	.open		= spidev_drv_open,
+	.release	= spidev_drv_release,
+	.read		= spidev_drv_read,
+	.write		= spidev_drv_write,
+	.poll		= spidev_drv_poll,
+	.llseek		= no_llseek,
+};
+
+static struct miscdevice spidev_misc = {
+	.fops		= &spidev_drv_fops,
+	.minor		= MISC_DYNAMIC_MINOR,
+	.name		= "spidev",
+};
+
+/*-------------------------------------------------------------------------*/
+
 static int __init spidev_init(void)
 {
 	int status;
@@ -1009,21 +1273,34 @@ static int __init spidev_init(void)
 
 	spidev_class = class_create(THIS_MODULE, "spidev");
 	if (IS_ERR(spidev_class)) {
-		unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
-		return PTR_ERR(spidev_class);
+		status = PTR_ERR(spidev_class);
+		goto err_unreg_chardev;
 	}
 
 	status = spi_register_driver(&spidev_spi_driver);
-	if (status < 0) {
-		class_destroy(spidev_class);
-		unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
-	}
+	if (status < 0)
+		goto err_destroy_class;
+
+	status = misc_register(&spidev_misc);
+	if (status < 0)
+		goto err_unreg_driver;
+
+	return 0;
+
+err_unreg_driver:
+	spi_unregister_driver(&spidev_spi_driver);
+err_destroy_class:
+	class_destroy(spidev_class);
+err_unreg_chardev:
+	unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
+
 	return status;
 }
 module_init(spidev_init);
 
 static void __exit spidev_exit(void)
 {
+	misc_deregister(&spidev_misc);
 	spi_unregister_driver(&spidev_spi_driver);
 	class_destroy(spidev_class);
 	unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);

--
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw
In reply to Noralf Trønnes
Quoted 17 lines · Noralf Trønnes

Hi,

I was previously working on tinydrm as a replacement for staging/fbtft. During a break from that work, I started to think about if it would be possible to move the drivers to userspace instead. No point in having them in the kernel if it's not necessary.

This patchset includes all the pieces necessary to get a userspace driver[1] working that is compatible with the fbtft/fb_ili9341 driver. It is tested with a SPI interfaced display hat/shield for the Raspberry Pi, which has an eeprom with a Device Tree fragment on it (merged by the bootloader). With the help of udev and systemd, the driver is able to autoload when the display is connected.
Performance wise, the kernel driver can do ~19fps on a 320x240 spi@32MHz display, whereas the userspace version does ~18fps. So performance is not an argument to keep the driver in the kernel.

Quoted 1 line

What do you think about this approach?

It's a pretty nifty thing, and iirc some of the ideas for implementing miracast centered around the same idea: Small userspace drm driver shim that forwards everything to the miracast code running all in userspace. David Herrmann looked into that years ago, not sure he ever got the full stack up&running.

For dumb panels itself I'm not sure it's the right design. Adding a kernel/userspace interface will make code sharing harder, and experience also says that we'll rework kms semantics every few years. In-kernel drivers seem to me like the better default choice, except when there's a clear reason for why userspace is the only option. For miracast that's the case, since porting the userspace video encode libraries to the kernel just doesn't make sense. For SPI and other slow buses I don't see any such compelling reason to move it into userspace.
-Daniel

Quoted 55 lines

Note: This patchset is based on 4.9

[1] https://github.com/notro/udrm

Noralf.

Noralf Trønnes (6):
drm/modes: Export drm_mode_convert_umode()
drm: Add support for userspace drivers
dma-buf: Support generic userspace allocations
spi: Let clients do scatter/gather transfers
spi: spidev: Add dma-buf support
spi: spidev: Add userspace driver support

 drivers/dma-buf/Makefile         |   2 +-
 drivers/dma-buf/dev.c            | 211 ++++++++++++++++++
 drivers/gpu/drm/Kconfig          |   2 +
 drivers/gpu/drm/Makefile         |   1 +
 drivers/gpu/drm/drm_modes.c      |   1 +
 drivers/gpu/drm/udrm/Kconfig     |   9 +
 drivers/gpu/drm/udrm/Makefile    |   4 +
 drivers/gpu/drm/udrm/udrm-dev.c  | 276 ++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-drv.c  | 324 ++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-fb.c   | 369 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/udrm/udrm-pipe.c | 170 +++++++++++++++
 drivers/gpu/drm/udrm/udrm.h      |  84 ++++++++
 drivers/spi/Kconfig              |   1 +
 drivers/spi/spi.c                |  24 ++-
 drivers/spi/spidev.c             | 447 ++++++++++++++++++++++++++++++++++++++-
 include/linux/spi/spi.h          |   4 +
 include/uapi/drm/udrm.h          |  78 +++++++
 include/uapi/linux/dma-buf-dev.h |  35 +++
 include/uapi/linux/spi/spidev.h  |   8 +
 19 files changed, 2032 insertions(+), 18 deletions(-)
 create mode 100644 drivers/dma-buf/dev.c
 create mode 100644 drivers/gpu/drm/udrm/Kconfig
 create mode 100644 drivers/gpu/drm/udrm/Makefile
 create mode 100644 drivers/gpu/drm/udrm/udrm-dev.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-drv.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-fb.c
 create mode 100644 drivers/gpu/drm/udrm/udrm-pipe.c
 create mode 100644 drivers/gpu/drm/udrm/udrm.h
 create mode 100644 include/uapi/drm/udrm.h
 create mode 100644 include/uapi/linux/dma-buf-dev.h

--
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw
Quoted 18 lines · Daniel Vetter
Quoted 17 lines · Noralf Trønnes

Hi,

I was previously working on tinydrm as a replacement for staging/fbtft. During a break from that work, I started to think about if it would be possible to move the drivers to userspace instead. No point in having them in the kernel if it's not necessary.

This patchset includes all the pieces necessary to get a userspace driver[1] working that is compatible with the fbtft/fb_ili9341 driver. It is tested with a SPI interfaced display hat/shield for the Raspberry Pi, which has an eeprom with a Device Tree fragment on it (merged by the bootloader). With the help of udev and systemd, the driver is able to autoload when the display is connected.
Performance wise, the kernel driver can do ~19fps on a 320x240 spi@32MHz display, whereas the userspace version does ~18fps. So performance is not an argument to keep the driver in the kernel.

Quoted 7 lines
Quoted 1 line

What do you think about this approach?

It's a pretty nifty thing, and iirc some of the ideas for implementing miracast centered around the same idea: Small userspace drm driver shim that forwards everything to the miracast code running all in userspace. David Herrmann looked into that years ago, not sure he ever got the full stack up&running.

This is actually something David Herrmann told us not to do, stating that it was too hard for Desktop Environments to select which GPUs to output to. I however disagreed as this is something they need to support anyway for USB GPUs and miracast could piggy back on this effort.

An engineer in Intel Finland did work on this and we got approval for Open Sourcing it but I really have no time for this right now though :s

That was my 2 cents.

Martin
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel