Re: [PATCH 00/03][RFC] Reusable UIO Platform Driver

Previous thread: linux-next: Tree for May 20 by Stephen Rothwell on Tuesday, May 20, 2008 - 3:27 am. (3 messages)

Next thread: [PATCH] memory hotplug: fix early allocation handling by Heiko Carstens on Tuesday, May 20, 2008 - 3:51 am. (3 messages)
From: Magnus Damm
Date: Tuesday, May 20, 2008 - 3:51 am

These patches implement a reusable UIO platform driver. This driver
can be used to export hardware to user space, as long as the device
is a) memory mapped and b) equipped with an unique IRQ.

The uio_platform driver gets all information through platform data,
including address window information and IRQ number. The driver also
supports assigning a contiguous piece of memory to each instance.
This may be useful when the exported hardware blocks can bus master
but requires physically contiguous memory.

There are not many surprises in the code if you are familiar with UIO,
except for the interrupt handling. All UIO kernel drivers that I've
seen so far have hardware specific interrupt acknowledge code in the
interrupt handler. The uio_platform driver is different.

The interrupt handling code in uio_platform assumes the device is the
only user of the assigned interrupt. This may be rare in the PC world
but for SuperH almost all interrupt sources are unique. Having an
unique interrupt for the device allows the code to use disable_irq()
and enable_irq() in kernel space and leave actual interrupt acknowledge
to user space. That way we have no hardware specific code in the kernel.

Interrupts are of course serviced in kernel space by the uio_platform
driver, but the uio_platform interrupt handler will simply disable the
IRQ until next read() or poll() syscall. The uio_platform kernel driver
assumes that the user space driver has taken care of acknowledging
the interrupt before doing read() and waiting for events again. If no
acknowledge has happened then an interrupt will occur again (since it's
still pending) and the kernel interrupt handler will disable the IRQ
again and unblock the user space process.

The last patch contains SuperH specific code that exports various
multimedia acceleration blocks to userspace. The following processors
and hardware blocks are exported for now:

sh7343: VPU
sh7722: VPU, VEU
sh7723: VPU, VEU, VEU

If anyone is interested then I have a proof ...
From: Magnus Damm
Date: Tuesday, May 20, 2008 - 3:51 am

Add enable_irq() callback to struct uio_info. This callback is needed by
the uio_platform driver so interrupts can be enabled before blocking.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 drivers/uio/uio.c          |    6 ++++++
 include/linux/uio_driver.h |    1 +
 2 files changed, 7 insertions(+)

--- 0001/drivers/uio/uio.c
+++ work/drivers/uio/uio.c	2008-05-19 14:52:08.000000000 +0900
@@ -365,6 +365,9 @@ static unsigned int uio_poll(struct file
 	if (idev->info->irq == UIO_IRQ_NONE)
 		return -EIO;
 
+	if (idev->info->enable_irq)
+		idev->info->enable_irq(idev->info);
+
 	poll_wait(filep, &idev->wait, wait);
 	if (listener->event_count != atomic_read(&idev->event))
 		return POLLIN | POLLRDNORM;
@@ -391,6 +394,9 @@ static ssize_t uio_read(struct file *fil
 	do {
 		set_current_state(TASK_INTERRUPTIBLE);
 
+		if (idev->info->enable_irq)
+			idev->info->enable_irq(idev->info);
+
 		event_count = atomic_read(&idev->event);
 		if (event_count != listener->event_count) {
 			if (copy_to_user(buf, &event_count, count))
--- 0001/include/linux/uio_driver.h
+++ work/include/linux/uio_driver.h	2008-05-19 14:52:08.000000000 +0900
@@ -64,6 +64,7 @@ struct uio_info {
 	void			*priv;
 	irqreturn_t (*handler)(int irq, struct uio_info *dev_info);
 	int (*mmap)(struct uio_info *info, struct vm_area_struct *vma);
+	void (*enable_irq)(struct uio_info *info);
 	int (*open)(struct uio_info *info, struct inode *inode);
 	int (*release)(struct uio_info *info, struct inode *inode);
 };
--

From: Magnus Damm
Date: Wednesday, May 21, 2008 - 4:58 am

We can most likely use a single uio platform driver if this patch is
acceptable. Any NAKs?

Thanks,

/ magnus
--

From: Hans J. Koch
Date: Thursday, May 22, 2008 - 1:18 pm

Yes. Your approach only allows enabling interrupts, but not disabling
them. And I don't like that it is not possible for generic userspace
tools to find out if a UIO device has this auto-irq-enabling capability
or not.

I just posted a patch that allows enabling _and_ disabling of irqs from
userspace by writing 0 or 1 to /dev/uioX. I've CCed you, could you
please test? If this doesn't do what you need, please let me know.

Thanks,
Hans

--

From: Magnus Damm
Date: Thursday, May 22, 2008 - 6:24 pm

Hi Hans,


Thanks for your effort. I understand that you need to enable and
disable interrupts from user space, but that's a bit different from
what I want to do. I just want interrupts to be enabled before I do
read() or poll().

Also, adding the capability of disabling and enabling interrupts from
user space seems a bit error prone to me. Mainly since user space then
needs to know that the interrupt handler in kernel space can cope with
such changes. Not such a clean interface IMO. OTOH you may need that
to cope with some broken hardware.

But why does user space need to know if the auto-irq-enabling function
is there or not? If the user is interfacing to the wrong kernel UIO
driver with wrong behavior then he has obviously done something wrong.
Knowing if auto-irq-enabling is there from user space isn't going to
save users from themselves. They can and will mix and match things in

I'm sure your patch or the ioctl suggestion both allow re-enabling
interrupts from user space. That's great, but both of them add extra
syscall overhead compared to my suggestion. They also make the user
space interface in user space part of the driver a bit more
complicated.

I do understand that you don't want to mess up your UIO kernel
callbacks by introducing just merging new ones all the time. OTOH, my
patch is just a few lines. Is introducing one extra syscall good
enough performance wise?

Thanks for your help!

/ magnus
--

From: Hans J. Koch
Date: Friday, May 23, 2008 - 1:43 am

Am Fri, 23 May 2008 10:24:42 +0900

Are you sure this works cleanly? You usually do a read immediately

No, it doesn't. If the kernel driver doesn't implement the irqcontrol()

All of this talk is _only_ about broken hardware. Decent hardware has
seperate IRQ mask and status registers, in which case userspace has no
problems at all to deal with several internal interrupt sources of the
chip. We only need all this for chips where it is not possible to mask
an IRQ through a mappable register or (more often) where acknowledging
the interrupt also clears the status register so that userspace has no
way of knowing what the source of the interrupt was. The latter applies



UIO deals with two things, device memory and interrupts. We have mmap()
for mem and read() for waiting for an irq. This looks clean and logical
to me:

1) mmap() => device memory

This doesn't seem to be a problem, really. This write() is straight
forward, without any wait queues etc, so what?

Thanks,
Hans

--

From: Magnus Damm
Date: Tuesday, May 20, 2008 - 3:51 am

This patch implements a reusable UIO platform driver. Only memory mapped
hardware devices with unique IRQs are supported.

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 Tested using the VEU on a SuperH sh7722.

 drivers/uio/Kconfig          |   10 ++
 drivers/uio/Makefile         |    1 
 drivers/uio/uio_platform.c   |  161 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/uio_platform.h |   10 ++
 4 files changed, 182 insertions(+)

--- 0001/drivers/uio/Kconfig
+++ work/drivers/uio/Kconfig	2008-05-20 17:20:18.000000000 +0900
@@ -39,4 +39,14 @@ config UIO_SMX
 
 	  If you compile this as a module, it will be called uio_smx.
 
+config UIO_PLATFORM
+	tristate "Userspace I/O Platform driver"
+	depends on UIO
+	default n
+	help
+	  Reusable userspace IO interface for memory mapped devices
+	  equipped with an unique IRQ. IRQ sharing is not supported.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called uio_platform.
 endif
--- 0001/drivers/uio/Makefile
+++ work/drivers/uio/Makefile	2008-05-20 17:20:18.000000000 +0900
@@ -1,3 +1,4 @@
 obj-$(CONFIG_UIO)	+= uio.o
 obj-$(CONFIG_UIO_CIF)	+= uio_cif.o
 obj-$(CONFIG_UIO_SMX)	+= uio_smx.o
+obj-$(CONFIG_UIO_PLATFORM)	+= uio_platform.o
--- /dev/null
+++ work/drivers/uio/uio_platform.c	2008-05-20 18:06:08.000000000 +0900
@@ -0,0 +1,161 @@
+/*
+ * Userspace I/O Platform Driver
+ *
+ * Copyright(C) 2008 Magnus Damm
+ *
+ * Platform data driven UIO kernel glue, only memory mapped I/O devices
+ * with unique IRQs are supported.
+ *
+ * Licensed under the GPLv2 only.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/delay.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/uio_driver.h>
+#include <linux/uio_platform.h>
+#include <linux/bitops.h>
+#include <linux/dma-mapping.h>
+#include <linux/mm.h>
+
+struct uio_platform_priv {
+	struct uio_info ...
From: Magnus Damm
Date: Tuesday, May 20, 2008 - 3:51 am

This patch exports the following SuperH hardware to user space:

sh7343: VPU
sh7722: VPU, VEU
sh7723: VPU, VEU, VEU

Signed-off-by: Magnus Damm <damm@igel.co.jp>
---

 arch/sh/kernel/cpu/sh4a/setup-sh7343.c |   32 ++++++++++
 arch/sh/kernel/cpu/sh4a/setup-sh7722.c |   63 +++++++++++++++++++++
 arch/sh/kernel/cpu/sh4a/setup-sh7723.c |   94 ++++++++++++++++++++++++++++++++
 3 files changed, 189 insertions(+)

--- 0001/arch/sh/kernel/cpu/sh4a/setup-sh7343.c
+++ work/arch/sh/kernel/cpu/sh4a/setup-sh7343.c	2008-05-20 17:19:05.000000000 +0900
@@ -11,6 +11,37 @@
 #include <linux/init.h>
 #include <linux/serial.h>
 #include <linux/serial_sci.h>
+#include <linux/uio_platform.h>
+
+static struct uio_platform_info vpu_platform_data = {
+	.name = "VPU",
+	.version = "0.0.1",
+	.memsize = 1 << 20,
+};
+
+static struct resource vpu_resources[] = {
+	[0] = {
+		.name	= "VPU",
+		.start	= 0xfe900000,
+		.end	= 0xfe9022ec,
+		.flags	= IORESOURCE_MEM,
+	},
+	[1] = {
+		.start	= 60,
+		.end	= 60,
+		.flags	= IORESOURCE_IRQ,
+	},
+};
+
+static struct platform_device vpu_device = {
+	.name		= "uio-platform",
+	.id		= 0,
+	.dev = {
+		.platform_data	= &vpu_platform_data,
+	},
+	.resource	= vpu_resources,
+	.num_resources	= ARRAY_SIZE(vpu_resources),
+};
 
 static struct plat_sci_port sci_platform_data[] = {
 	{
@@ -33,6 +64,7 @@ static struct platform_device sci_device
 
 static struct platform_device *sh7343_devices[] __initdata = {
 	&sci_device,
+	&vpu_device,
 };
 
 static int __init sh7343_devices_setup(void)
--- 0001/arch/sh/kernel/cpu/sh4a/setup-sh7722.c
+++ work/arch/sh/kernel/cpu/sh4a/setup-sh7722.c	2008-05-20 17:19:05.000000000 +0900
@@ -12,6 +12,7 @@
 #include <linux/serial.h>
 #include <linux/serial_sci.h>
 #include <linux/mm.h>
+#include <linux/uio_platform.h>
 #include <asm/mmzone.h>
 
 static struct resource usbf_resources[] = {
@@ -59,6 +60,66 @@ static struct platform_device iic_device
 	.resource       = iic_resources,
 };
 
+static ...
From: Hans J. Koch
Date: Tuesday, May 20, 2008 - 2:07 pm

On Tue, May 20, 2008 at 07:51:32PM +0900, Magnus Damm wrote:


Uwe Kleine-Koenig already submitted such a framework:

http://lkml.org/lkml/2008/5/20/94

It's his third version, and it looks good. I presume you didn't know
about his work. The main difference is that he leaves interrupt handling
to platform code. That might look strange (it did to me first), but it
has the advantage that you can put hardware dependent stuff in your
board support (which depends on hardware anyway).

Could you have a look at his patch and tell me if that does what you


This sounds quite interesting. Unfortunately, I'm not familiar with the
SuperH architecture. Could you also do this with Uwe's approach?

I'm about to sign-off Uwe's patch, and we'll possibly have that in
mainline soon. I don't mind having a second "generic platform" driver,
but you'll need to have good technical arguments.

Thanks,
Hans

--

From: Magnus Damm
Date: Tuesday, May 20, 2008 - 8:31 pm

Hi Hans!



The uio_pdrv driver doesn't do what I need at this point, though I may
be able to extend it with the following:
- Interrupt enable/disable code
- Physically contiguous memory support

The interrupt code may be placed in the board/cpu code, but I need to
share that code between multiple UIO driver instances. We want to use
the same UIO driver for many different processor models and hardware
blocks. Extending uio_pdrv driver with a chunk of physically
contiguous memory isn't a big deal though.

To be frank, I have my doubts in adding an extra forwarding-only
platform layer on top of UIO compared to using uio_register_device()
directly from the board code. I like that the platform layer is using
struct resource and handles resource ranges for us automatically, but
wouldn't it make more sense to extend the UIO core to always use
struct resource instead of struct uio_mem? I'd be happy to help out -

True, but the uio_pdrv driver is choosing to not deal with interrupts
at all. I'd like to have shared interrupt handling code. With my
driver, you just feed it io memory window parameters and an interrupt

I could handle things by extending Uwe's uio_pdrv driver, but I still
need the enable_irq() callback. I wonder if it makes sense to let the
two drivers coexist side by side, since they are solving different
problems. I can rename my driver to uio_pdrv_unique_irq or something,
or maybe uio_superh.c. I dislike the latter since my driver doesn't do
anything SuperH specific and that I suspect it can be useful for other

I'd like to have this driver upstream as well, and sharing the code
with the uio_pdrv driver is one way, but I suspect that adding another
reusable layer on top of that driver will just complicate things. The
uio-specific part of my patches is less than 200 lines of code. From
the top of my head I can think of at least 10 different SuperH
hardware devices that can reuse this driver.

Please let me know what you prefer and I'll update the code and ...
From: Uwe
Date: Tuesday, May 20, 2008 - 11:49 pm

Hello Magnus,

What about adding uio_platform_handler (with a different name) to
uio_pdrv.c?
OTOH I don't see why you want to disable the irq.  Can you describe the
I wonder how you use that memory.  Isn't it just some kind of shared
memory?  If so, why not use normal shared memory?  Do you really need
That alone doesn't help.  You need a struct device to register a uio
In my eyes this isn't completly correct.  Just the way you specify your
handler is a bit different.  You can pass a handler via platform data
with my driver, too.
 
BTW, you don't need "depends on UIO" (because it's in a if UIO/endif
block) and "default n" (as this is the default anyhow).  See also 
http://thread.gmane.org/gmane.linux.kernel/663884/focus=683097

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

From: Paul Mundt
Date: Wednesday, May 21, 2008 - 12:49 am

Physically contiguous memory is a real requirement, especially for DMA.
I'm not sure what's confusing about that?
--

From: Uwe
Date: Wednesday, May 21, 2008 - 1:05 am

I got that, yes.  The problem is I don't see how you can use it for DMA.
The physical address is stored in info->mem[$last].internal_addr and if
there is a way to access that variable from user space, I don't see it
and would appretiate a hint.  Sorry for not expressing my concern more
clear at the first go.  I hope it's understandable now.

@Magnus: Maybe you can provide the userspace part of the driver?
How is that mapping used there?

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

From: Magnus Damm
Date: Wednesday, May 21, 2008 - 1:22 am

On Wed, May 21, 2008 at 5:05 PM, Uwe Kleine-K=F6nig

[Added Matsubara-san as CC]

Sure, here is a little test program. Have a look at "uio_mem". The
"address" member contains the physical address that can be used for
bus mastering DMA. Compare that to "iomem" which is the pointer to the
virtual memory area in user space.

Hope this helps!

/ magnus
From: Uwe
Date: Wednesday, May 21, 2008 - 1:50 am

Hello,

Yes it does.  I thought the physical address is stored in internal_addr
and the virtual in addr, but it's the other way round.  Thanks.

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

From: Magnus Damm
Date: Wednesday, May 21, 2008 - 1:09 am

Hi Uwe!

Thanks for your email.

On Wed, May 21, 2008 at 3:49 PM, Uwe Kleine-König


Most UIO kernel drivers today contain hardware device specific code to
acknowledge interrupts. In other words, most UIO interrupt handlers
touches some device specific bits so the interrupt gets deasserted.

My uio_platform driver handles interrupts in a different way. The
kernel UIO driver is not aware of the hardware device specific method
to acknowledge the interrupt, instead it simply disables the interrupt
and notifies user space which instead will acknowledge the interrupt.
Next time a read() or poll() call gets made, the interrupt is enabled
again.

This allows us to export a hardware device to user space and allow
user space to handle interrupts without knowing in kernel space how to

Yes, I need that to give the exported hardware device some physically
contiguous memory for DMA. At this point our hardware is missing IOMMU

I don't mind that you are using platform devices. Actually, I think
platform devices are great. We use them for all sorts of things on the
SuperH architecture. I'm trying to suggest that maybe it's a good idea
to change the UIO core code to use struct resource instead of struct

I don't want to pass any handler. All devices share the same interrupt
handler, the only thing that differs between multiple uio_platform

Ah, thanks for pointing that out!

Thank you for your feedback!

/ magnus
--

From: Uwe
Date: Wednesday, May 21, 2008 - 2:25 am

Just add irq_disabled to struct uio_platdata and define

	irqreturn_t uio_pdrv_disirq(int irq, struct uio_info *dev_info)
	{
		struct uio_platdata *pdata = container_of(dev_info, struct uio_platdata, uio_info);

		disable_irq(irq);
		pdata->irq_disabled = 1;
		return IRQ_HANDLED;
	}
	EXPORT_SYMBOL(uio_pdrv_disirq);

	void uio_pdrv_enirq(struct uio_info *dev_info)
	{
		...
	}
	EXPORT_SYMBOL(uio_pdrv_enirq);

and then you can do

	info->handler = uio_pdrv_disirq;
	info->enable_irq = uio_pdrv_enirq;

in the arch specific code.  I just realize that you need to compile UIO
statically then :-(

IMHO something like prep_read_poll is a better name than enable_irq for
OK, got it.  The down-side is that you can only get a single interrupt
between two calls to read() (or poll()).  So you might or might not
loose information.  And you might run into problems if your device or
your interrupt goes berserk as your handler always returns IRQ_HANDLED.
With a functional handler you can rely on existing mechanisms in the
struct resource alone doesn't provide enough information.  At least
memtype is needed.  And you don't need the pointers *parent, *sibling,
See above.  That would be the cost to share code with "my" driver.

All in all I'm not conviced that it's a good idea to use the irq_disable
trick to save acking in kernel space.  This doesn't need to stop you
doing it that way of course.

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

From: Magnus Damm
Date: Wednesday, May 21, 2008 - 3:50 am

On Wed, May 21, 2008 at 6:25 PM, Uwe Kleine-König

I understand now. Thanks for the clear description.

What about letting the uio_pdrv code override info->handler and
info->enable_irq with the above functions if info->handler is NULL?
That would be one step closer to a shared driver in my opinion. And it
would remove the need for symbol exports and solve the
static-compile-only issue.

The physically contiguous memory issue still needs to be solved
somehow though. What about using struct resouce flagged as


I agree that I only get a single interrupt, but I'm not agreeing
regarding the problems. =)

In my mind, disabling interrupts and acking them from user space only
leads to increased interrupt latencies. People may dislike increased
interrupt latencies, but if so they shouldn't have their driver in
user space. And you may of course choose to ack interrupts in kernel
space and queue information there which user space later reads out.
But that sounds more like a specialized kernel driver. And that is not
what i'm trying to do.

Regarding loosing information, if your hardware device can't cope with
long latencies and drops things on the floor then improve your
latency, increase buffer size or design better hardware. Also, I don't
think the interrupt can go berserk since it will be disabled directly

Maybe the flags member of struct resource together with IORESOURCE_xxx
can be used instead of memtype. But there is no point in changing
things just for the sake of it, so it is fine as-is in my opinion.

Thank you!

/ magnus
--

From: Uwe
Date: Wednesday, May 21, 2008 - 4:04 am

... if both info->handler and info->prep_read_poll are NULL and
I'm not sure that solving that problem in uio_pdrv is the right
approach.  Other uio drivers might have the same problem, so better
allow the userspace driver to allocate some memory in a more generic
Assume your irq is stuck at its active level.  Normally the irq is
then disabled after some time.  You can handle that in your userspace
driver, but with acking in kernel space and returning IRQ_NONE or
IRQ_HANDLED you get it for free.  Nevertheless, go on.

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

From: Magnus Damm
Date: Wednesday, May 21, 2008 - 4:56 am

On Wed, May 21, 2008 at 8:04 PM, Uwe Kleine-König


I don't think there is any generic way for a user space driver to
allocate physically contiguous memory. If such way exists then we

Ok, so normally if the irq is stuck as asserted then it gets disabled
after some time. In my case it gets disabled directly so see it as a
feature. =)

Would you like to fold in the irq_handler and irq_enable function in
your patch, or would you like me to make a patch that fits on top of
your latest version?

Thanks for your help!

/ magnus
--

From: Uwe
Date: Wednesday, May 21, 2008 - 5:09 am

I would prefer the latter, because my patch already has acks and is
complete as such.  Moreover your suggestion needs the irq_enable patch.

Best regards
Uwe

-- 
Uwe Kleine-König, Software Engineer
Digi International GmbH Branch Breisach, Küferstrasse 8, 79206 Breisach, Germany
Tax: 315/5781/0242 / VAT: DE153662976 / Reg. Amtsgericht Dortmund HRB 13962
--

Previous thread: linux-next: Tree for May 20 by Stephen Rothwell on Tuesday, May 20, 2008 - 3:27 am. (3 messages)

Next thread: [PATCH] memory hotplug: fix early allocation handling by Heiko Carstens on Tuesday, May 20, 2008 - 3:51 am. (3 messages)