Skip to contentSkip to conversation

gpio: Add virtio based driver

PATCH Superseded Viresh Kumar 4 participants 11 messages Aug 10, 2021 – Aug 23, 2021

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover gpio: Add virtio based driver

Hello,

This adds Virtio GPIO driver based on the proposed specification [1].

The specification for basic GPIO operations is already reviewed by Linus and Arnd, while the IRQ stuff is still under discussion and not finalized.

I am sharing the code, so everyone gets more clarity on how it will work eventually in Linux.

I have tested this patchset with Qemu guest with help of the libgpiod utility. I have also tested basic handling of interrupts on the guest side. It works as expected.

The host side virtio-backend isn't ready yet and my tests only tested the flow control between guest and host, but didn't play with real GPIO pins. That will be done once I have a working backend in place (WIP).

V4->V5:
- Use ____cacheline_aligned for buffers.
- Proper locking in place, which avoids the use of work-item for processing interrupts.
- Separate callbacks for enable/disable of irqs.
- The irq is disabled at the host only for enable/disable now, instead of mask/unmask.
- mask/unmask only control the queuing of buffers now.
- Use handle_level_irq() instead of handle_fasteoi_irq().
- Other minor changes.

V3->V4:
- Lots of changes, as the specification changed too much. Better forget everything we have done until now :)

--
Viresh

[1] https://lists.oasis-open.org/archives/virtio-dev/202107/msg00232.html

Viresh Kumar (2):
gpio: Add virtio-gpio driver
gpio: virtio: Add IRQ support

 MAINTAINERS                      |   7 +
 drivers/gpio/Kconfig             |  10 +
 drivers/gpio/Makefile            |   1 +
 drivers/gpio/gpio-virtio.c       | 668 +++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_gpio.h |  72 ++++
 5 files changed, 758 insertions(+)
 create mode 100644 drivers/gpio/gpio-virtio.c
 create mode 100644 include/uapi/linux/virtio_gpio.h
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH v5 1/2 gpio: Add virtio-gpio driver

This patch adds a new driver for Virtio based GPIO devices.

This allows a guest VM running Linux to access GPIO lines provided by the host. It supports all basic operations, except interrupts for the GPIO lines.

Based on the initial work posted by:
"Enrico Weigelt, metux IT consult" <[email protected]>.

---
Enrico, lemme know if you want me to add your co-developed by and signed-off-by. Didn't want to add without checking with you first.

---
 MAINTAINERS                      |   7 +
 drivers/gpio/Kconfig             |   9 +
 drivers/gpio/Makefile            |   1 +
 drivers/gpio/gpio-virtio.c       | 375 +++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_gpio.h |  47 ++++
 5 files changed, 439 insertions(+)
 create mode 100644 drivers/gpio/gpio-virtio.c
 create mode 100644 include/uapi/linux/virtio_gpio.h
MAINTAINERS +7 -0
index a61f4f3b78a9..f632acd7d98c 100644
@@ -19647,6 +19647,13 @@ F:	Documentation/filesystems/virtiofs.rst
 F:	fs/fuse/virtio_fs.c
 F:	include/uapi/linux/virtio_fs.h
 
+VIRTIO GPIO DRIVER
+M:	Enrico Weigelt, metux IT consult <[email protected]>
+M:	Viresh Kumar <[email protected]>
+S:	Maintained
+F:	drivers/gpio/gpio-virtio.c
+F:	include/uapi/linux/virtio_gpio.h
+
 VIRTIO GPU DRIVER
 M:	David Airlie <[email protected]>
 M:	Gerd Hoffmann <[email protected]>
drivers/gpio/Kconfig +9 -0
index fab571016adf..e5993d6864fb 100644
@@ -1669,6 +1669,15 @@ config GPIO_MOCKUP
 	  tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
 	  it.
 
+config GPIO_VIRTIO
+	tristate "VirtIO GPIO support"
+	depends on VIRTIO
+	help
+	  Say Y here to enable guest support for virtio-based GPIO controllers.
+
+	  These virtual GPIOs can be routed to real GPIOs or attached to
+	  simulators on the host (like QEMU).
+
 endmenu
 
 endif
drivers/gpio/Makefile +1 -0
index 32a32659866a..e0301cfedd8d 100644
@@ -165,6 +165,7 @@ obj-$(CONFIG_GPIO_UCB1400)		+= gpio-ucb1400.o
 obj-$(CONFIG_GPIO_UNIPHIER)		+= gpio-uniphier.o
 obj-$(CONFIG_GPIO_VF610)		+= gpio-vf610.o
 obj-$(CONFIG_GPIO_VIPERBOARD)		+= gpio-viperboard.o
+obj-$(CONFIG_GPIO_VIRTIO)		+= gpio-virtio.o
 obj-$(CONFIG_GPIO_VISCONTI)		+= gpio-visconti.o
 obj-$(CONFIG_GPIO_VR41XX)		+= gpio-vr41xx.o
 obj-$(CONFIG_GPIO_VX855)		+= gpio-vx855.o
drivers/gpio/gpio-virtio.c new file +375 -0

375 lines not on this page · raw message

include/uapi/linux/virtio_gpio.h new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#8 lore raw

PATCH v5 2/2 gpio: virtio: Add IRQ support

In reply to Viresh Kumar

This patch adds IRQ support for the virtio GPIO driver. Note that this uses the irq_bus_lock/unlock() callbacks, since those operations over virtio may sleep. Also the notifications for the eventq are processed using a work item to allow sleep-able operations.

---
 drivers/gpio/Kconfig             |   1 +
 drivers/gpio/gpio-virtio.c       | 301 ++++++++++++++++++++++++++++++-
 include/uapi/linux/virtio_gpio.h |  25 +++
 3 files changed, 323 insertions(+), 4 deletions(-)
drivers/gpio/Kconfig +1 -0
index e5993d6864fb..222f4ae98a35 100644
@@ -1672,6 +1672,7 @@ config GPIO_MOCKUP
 config GPIO_VIRTIO
 	tristate "VirtIO GPIO support"
 	depends on VIRTIO
+	select GPIOLIB_IRQCHIP
 	help
 	  Say Y here to enable guest support for virtio-based GPIO controllers.
 
drivers/gpio/gpio-virtio.c +297 -4

359 lines not on this page · raw message

include/uapi/linux/virtio_gpio.h +25 -0
index 844574acf095..297ffdae1a5d 100644
@@ -5,12 +5,16 @@
 
 #include <linux/types.h>
 
+/* Virtio GPIO Feature bits */
+#define VIRTIO_GPIO_F_IRQ			0
+
 /* Virtio GPIO request types */
 #define VIRTIO_GPIO_MSG_GET_NAMES		0x0001
 #define VIRTIO_GPIO_MSG_GET_DIRECTION		0x0002
 #define VIRTIO_GPIO_MSG_SET_DIRECTION		0x0003
 #define VIRTIO_GPIO_MSG_GET_VALUE		0x0004
 #define VIRTIO_GPIO_MSG_SET_VALUE		0x0005
+#define VIRTIO_GPIO_MSG_IRQ_TYPE		0x0006
 
 /* Possible values of the status field */
 #define VIRTIO_GPIO_STATUS_OK			0x0
@@ -21,6 +25,14 @@
 #define VIRTIO_GPIO_DIRECTION_OUT		0x01
 #define VIRTIO_GPIO_DIRECTION_IN		0x02
 
+/* Virtio GPIO IRQ types */
+#define VIRTIO_GPIO_IRQ_TYPE_NONE		0x00
+#define VIRTIO_GPIO_IRQ_TYPE_EDGE_RISING	0x01
+#define VIRTIO_GPIO_IRQ_TYPE_EDGE_FALLING	0x02
+#define VIRTIO_GPIO_IRQ_TYPE_EDGE_BOTH		0x03
+#define VIRTIO_GPIO_IRQ_TYPE_LEVEL_HIGH		0x04
+#define VIRTIO_GPIO_IRQ_TYPE_LEVEL_LOW		0x08
+
 struct virtio_gpio_config {
 	__u16 ngpio;
 	__u8 padding[2];
@@ -44,4 +56,17 @@ struct virtio_gpio_response_get_names {
 	__u8 value[];
 };
 
+/* Virtio GPIO IRQ Request / Response */
+struct virtio_gpio_irq_request {
+	__u16 gpio;
+};
+
+struct virtio_gpio_irq_response {
+	__u8 status;
+};
+
+/* Possible values of the interrupt status field */
+#define VIRTIO_GPIO_IRQ_STATUS_INVALID		0x0
+#define VIRTIO_GPIO_IRQ_STATUS_VALID		0x1
+
 #endif /* _LINUX_VIRTIO_GPIO_H */
Signature · 1 line
2.31.1.272.g89b43f80a514
#2 lore raw

Re: [PATCH V5 1/2] gpio: Add virtio-gpio driver

In reply to Viresh Kumar
I love your patch! Yet something to improve:

Hi Viresh,

I love your patch! Yet something to improve:

[auto build test ERROR on soc/for-next]
[also build test ERROR on vhost/linux-next linus/master v5.14-rc5 next-20210810] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: riscv-randconfig-r042-20210810 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 614c7d03877fd99c2de47429b15be3f00306a3bd)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/4fe66a15717a74f8f876f70595f050b4dffda7ae
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659 git checkout 4fe66a15717a74f8f876f70595f050b4dffda7ae
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross O=build_dir ARCH=riscv SHELL=/bin/bash drivers/gpio/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> drivers/gpio/gpio-virtio.c:356:4: error: use of undeclared identifier 'VIRTIO_ID_GPIO'

{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
^
1 error generated.

vim +/VIRTIO_ID_GPIO +356 drivers/gpio/gpio-virtio.c

354
355 static const struct virtio_device_id id_table[] = {

 > 356		{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },

357 {},
358 };
359 MODULE_DEVICE_TABLE(virtio, id_table);
360

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [PATCH V5 1/2] gpio: Add virtio-gpio driver

Quoted 26 lines · kernel test robot

Hi Viresh,

I love your patch! Yet something to improve:

[auto build test ERROR on soc/for-next]
[also build test ERROR on vhost/linux-next linus/master v5.14-rc5 next-20210810] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: riscv-randconfig-r042-20210810 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 614c7d03877fd99c2de47429b15be3f00306a3bd)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/4fe66a15717a74f8f876f70595f050b4dffda7ae
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659 git checkout 4fe66a15717a74f8f876f70595f050b4dffda7ae
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross O=build_dir ARCH=riscv SHELL=/bin/bash drivers/gpio/

If you fix the issue, kindly add following tag as appropriate

Quoted 8 lines

All errors (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

drivers/gpio/gpio-virtio.c:356:4: error: use of undeclared identifier 'VIRTIO_ID_GPIO'

{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
^
1 error generated.

I forgot to mention a dependency :(

https://lore.kernel.org/lkml/61b27e3bc61fb0c9f067001e95cfafc5d37d414a.1627362340.git.viresh.kumar(a)linaro.org/

Signature · 1 line
viresh
Reviewed-by on v5 1/2
#6 lore raw

Re: [PATCH V5 1/2] gpio: Add virtio-gpio driver

In reply to Viresh Kumar
Quoted 10 lines · Viresh Kumar

This patch adds a new driver for Virtio based GPIO devices.

This allows a guest VM running Linux to access GPIO lines provided by the host. It supports all basic operations, except interrupts for the GPIO lines.

Based on the initial work posted by:
"Enrico Weigelt, metux IT consult" <[email protected]>.

I see there is a dependency that needs to be fixed but the driver looks good to me:

Yours,
Linus Walleij
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Reviewed-by on v5 2/2
#9 lore raw

Re: [PATCH V5 2/2] gpio: virtio: Add IRQ support

In reply to Viresh Kumar
Quoted 6 lines · Viresh Kumar

This patch adds IRQ support for the virtio GPIO driver. Note that this uses the irq_bus_lock/unlock() callbacks, since those operations over virtio may sleep. Also the notifications for the eventq are processed using a work item to allow sleep-able operations.

Looks good to me from a GPIO point of view:

Yours,
Linus Walleij
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#10 lore raw

Re: [PATCH V5 2/2] gpio: virtio: Add IRQ support

In reply to Viresh Kumar
Quoted 11 lines · Viresh Kumar

This patch adds IRQ support for the virtio GPIO driver. Note that this uses the irq_bus_lock/unlock() callbacks, since those operations over virtio may sleep. Also the notifications for the eventq are processed using a work item to allow sleep-able operations.

---
 drivers/gpio/Kconfig             |   1 +
 drivers/gpio/gpio-virtio.c       | 301 ++++++++++++++++++++++++++++++-
 include/uapi/linux/virtio_gpio.h |  25 +++
 3 files changed, 323 insertions(+), 4 deletions(-)

Marc,

Will it be possible for you to have a look at this patch, it will help us understand shortcomings of the specification better and this implementation as well.

Thanks.

Signature · 1 line
viresh
#7 lore raw

Re: [PATCH V5 1/2] gpio: Add virtio-gpio driver

In reply to Viresh Kumar
I love your patch! Yet something to improve:

Hi Viresh,

I love your patch! Yet something to improve:

[auto build test ERROR on soc/for-next]
[also build test ERROR on vhost/linux-next linus/master v5.14-rc6 next-20210820] [cannot apply to gpio/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: x86_64-allyesconfig (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/4fe66a15717a74f8f876f70595f050b4dffda7ae
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659
        git checkout 4fe66a15717a74f8f876f70595f050b4dffda7ae
        # save the attached .config to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> drivers/gpio/gpio-virtio.c:356:4: error: 'VIRTIO_ID_GPIO' undeclared here (not in a function); did you mean 'VIRTIO_ID_GPU'?
     356 |  { VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
         |    ^~~~~~~~~~~~~~
         |    VIRTIO_ID_GPU

vim +356 drivers/gpio/gpio-virtio.c

354
355 static const struct virtio_device_id id_table[] = {

 > 356		{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },

357 {},
358 };
359 MODULE_DEVICE_TABLE(virtio, id_table);
360

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

Re: [PATCH V5 1/2] gpio: Add virtio-gpio driver

In reply to Viresh Kumar
Quoted 31 lines · Viresh Kumar
Quoted 30 lines · kernel test robot

Hi Viresh,

I love your patch! Yet something to improve:

[auto build test ERROR on soc/for-next]
[also build test ERROR on vhost/linux-next linus/master v5.14-rc5 next-20210810] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659
base:   https://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git for-next
config: riscv-randconfig-r042-20210810 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 614c7d03877fd99c2de47429b15be3f00306a3bd)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/4fe66a15717a74f8f876f70595f050b4dffda7ae
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Viresh-Kumar/gpio-Add-virtio-based-driver/20210810-182659 git checkout 4fe66a15717a74f8f876f70595f050b4dffda7ae
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross O=build_dir ARCH=riscv SHELL=/bin/bash drivers/gpio/

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

drivers/gpio/gpio-virtio.c:356:4: error: use of undeclared identifier 'VIRTIO_ID_GPIO'

{ VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
^
1 error generated.

I forgot to mention a dependency :(

https://lore.kernel.org/lkml/61b27e3bc61fb0c9f067001e95cfafc5d37d414a.1627362340.git.viresh.kumar(a)linaro.org/

Maybe just include it here for completeness.

Quoted 2 lines
Signature · 1 line
viresh