1 // SPDX-License-Identifier: GPL-2.0+
3 * GPIO driver for virtio-based virtual GPIO controllers
5 * Copyright (C) 2021 metux IT consult
6 * Enrico Weigelt, metux IT consult <info@metux.net>
8 * Copyright (C) 2021 Linaro.
9 * Viresh Kumar <viresh.kumar@linaro.org>
12 #include <linux/completion.h>
13 #include <linux/err.h>
14 #include <linux/gpio/driver.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/spinlock.h>
20 #include <linux/virtio_config.h>
21 #include <uapi/linux/virtio_gpio.h>
22 #include <uapi/linux/virtio_ids.h>
24 struct virtio_gpio_line {
25 struct mutex lock; /* Protects line operation */
26 struct completion completion;
27 struct virtio_gpio_request req ____cacheline_aligned;
28 struct virtio_gpio_response res ____cacheline_aligned;
32 struct vgpio_irq_line {
40 struct virtio_gpio_irq_request ireq ____cacheline_aligned;
41 struct virtio_gpio_irq_response ires ____cacheline_aligned;
45 struct virtio_device *vdev;
46 struct mutex lock; /* Protects virtqueue operation */
48 struct virtio_gpio_line *lines;
49 struct virtqueue *request_vq;
52 struct virtqueue *event_vq;
53 struct mutex irq_lock; /* Protects irq operation */
54 raw_spinlock_t eventq_lock; /* Protects queuing of the buffer */
55 struct vgpio_irq_line *irq_lines;
58 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
59 u8 txvalue, u8 *rxvalue, void *response, u32 rxlen)
61 struct virtio_gpio_line *line = &vgpio->lines[gpio];
62 struct virtio_gpio_request *req = &line->req;
63 struct virtio_gpio_response *res = response;
64 struct scatterlist *sgs[2], req_sg, res_sg;
65 struct device *dev = &vgpio->vdev->dev;
69 * Prevent concurrent requests for the same line since we have
70 * pre-allocated request/response buffers for each GPIO line. Moreover
71 * Linux always accesses a GPIO line sequentially, so this locking shall
72 * always go through without any delays.
74 mutex_lock(&line->lock);
76 req->type = cpu_to_le16(type);
77 req->gpio = cpu_to_le16(gpio);
78 req->value = cpu_to_le32(txvalue);
80 sg_init_one(&req_sg, req, sizeof(*req));
81 sg_init_one(&res_sg, res, rxlen);
86 reinit_completion(&line->completion);
89 * Virtqueue callers need to ensure they don't call its APIs with other
90 * virtqueue operations at the same time.
92 mutex_lock(&vgpio->lock);
93 ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
95 dev_err(dev, "failed to add request to vq\n");
96 mutex_unlock(&vgpio->lock);
100 virtqueue_kick(vgpio->request_vq);
101 mutex_unlock(&vgpio->lock);
103 wait_for_completion(&line->completion);
105 if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
106 dev_err(dev, "GPIO request failed: %d\n", gpio);
111 if (unlikely(line->rxlen != rxlen)) {
112 dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
119 *rxvalue = res->value;
122 mutex_unlock(&line->lock);
126 static int virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
127 u8 txvalue, u8 *rxvalue)
129 struct virtio_gpio_line *line = &vgpio->lines[gpio];
130 struct virtio_gpio_response *res = &line->res;
132 return _virtio_gpio_req(vgpio, type, gpio, txvalue, rxvalue, res,
136 static void virtio_gpio_free(struct gpio_chip *gc, unsigned int gpio)
138 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
140 virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
141 VIRTIO_GPIO_DIRECTION_NONE, NULL);
144 static int virtio_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
146 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
150 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_DIRECTION, gpio, 0,
156 case VIRTIO_GPIO_DIRECTION_IN:
157 return GPIO_LINE_DIRECTION_IN;
158 case VIRTIO_GPIO_DIRECTION_OUT:
159 return GPIO_LINE_DIRECTION_OUT;
165 static int virtio_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
167 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
169 return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
170 VIRTIO_GPIO_DIRECTION_IN, NULL);
173 static int virtio_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
176 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
179 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
183 return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
184 VIRTIO_GPIO_DIRECTION_OUT, NULL);
187 static int virtio_gpio_get(struct gpio_chip *gc, unsigned int gpio)
189 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
193 ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_VALUE, gpio, 0, &value);
194 return ret ? ret : value;
197 static void virtio_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
199 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
201 virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
204 /* Interrupt handling */
205 static void virtio_gpio_irq_prepare(struct virtio_gpio *vgpio, u16 gpio)
207 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[gpio];
208 struct virtio_gpio_irq_request *ireq = &irq_line->ireq;
209 struct virtio_gpio_irq_response *ires = &irq_line->ires;
210 struct scatterlist *sgs[2], req_sg, res_sg;
213 if (WARN_ON(irq_line->queued || irq_line->masked || irq_line->disabled))
216 ireq->gpio = cpu_to_le16(gpio);
217 sg_init_one(&req_sg, ireq, sizeof(*ireq));
218 sg_init_one(&res_sg, ires, sizeof(*ires));
222 ret = virtqueue_add_sgs(vgpio->event_vq, sgs, 1, 1, irq_line, GFP_ATOMIC);
224 dev_err(&vgpio->vdev->dev, "failed to add request to eventq\n");
228 irq_line->queued = true;
229 virtqueue_kick(vgpio->event_vq);
232 static void virtio_gpio_irq_enable(struct irq_data *d)
234 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
235 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
236 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
238 raw_spin_lock(&vgpio->eventq_lock);
239 irq_line->disabled = false;
240 irq_line->masked = false;
241 irq_line->queue_pending = true;
242 raw_spin_unlock(&vgpio->eventq_lock);
244 irq_line->update_pending = true;
247 static void virtio_gpio_irq_disable(struct irq_data *d)
249 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
250 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
251 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
253 raw_spin_lock(&vgpio->eventq_lock);
254 irq_line->disabled = true;
255 irq_line->masked = true;
256 irq_line->queue_pending = false;
257 raw_spin_unlock(&vgpio->eventq_lock);
259 irq_line->update_pending = true;
262 static void virtio_gpio_irq_mask(struct irq_data *d)
264 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
265 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
266 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
268 raw_spin_lock(&vgpio->eventq_lock);
269 irq_line->masked = true;
270 raw_spin_unlock(&vgpio->eventq_lock);
273 static void virtio_gpio_irq_unmask(struct irq_data *d)
275 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
276 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
277 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
279 raw_spin_lock(&vgpio->eventq_lock);
280 irq_line->masked = false;
282 /* Queue the buffer unconditionally on unmask */
283 virtio_gpio_irq_prepare(vgpio, d->hwirq);
284 raw_spin_unlock(&vgpio->eventq_lock);
287 static int virtio_gpio_irq_set_type(struct irq_data *d, unsigned int type)
289 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
290 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
291 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
294 case IRQ_TYPE_EDGE_RISING:
295 type = VIRTIO_GPIO_IRQ_TYPE_EDGE_RISING;
297 case IRQ_TYPE_EDGE_FALLING:
298 type = VIRTIO_GPIO_IRQ_TYPE_EDGE_FALLING;
300 case IRQ_TYPE_EDGE_BOTH:
301 type = VIRTIO_GPIO_IRQ_TYPE_EDGE_BOTH;
303 case IRQ_TYPE_LEVEL_LOW:
304 type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_LOW;
306 case IRQ_TYPE_LEVEL_HIGH:
307 type = VIRTIO_GPIO_IRQ_TYPE_LEVEL_HIGH;
310 dev_err(&vgpio->vdev->dev, "unsupported irq type: %u\n", type);
314 irq_line->type = type;
315 irq_line->update_pending = true;
320 static void virtio_gpio_irq_bus_lock(struct irq_data *d)
322 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
323 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
325 mutex_lock(&vgpio->irq_lock);
328 static void virtio_gpio_irq_bus_sync_unlock(struct irq_data *d)
330 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
331 struct virtio_gpio *vgpio = gpiochip_get_data(gc);
332 struct vgpio_irq_line *irq_line = &vgpio->irq_lines[d->hwirq];
333 u8 type = irq_line->disabled ? VIRTIO_GPIO_IRQ_TYPE_NONE : irq_line->type;
336 if (irq_line->update_pending) {
337 irq_line->update_pending = false;
338 virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_IRQ_TYPE, d->hwirq, type,
341 /* Queue the buffer only after interrupt is enabled */
342 raw_spin_lock_irqsave(&vgpio->eventq_lock, flags);
343 if (irq_line->queue_pending) {
344 irq_line->queue_pending = false;
345 virtio_gpio_irq_prepare(vgpio, d->hwirq);
347 raw_spin_unlock_irqrestore(&vgpio->eventq_lock, flags);
350 mutex_unlock(&vgpio->irq_lock);
353 static struct irq_chip vgpio_irq_chip = {
354 .name = "virtio-gpio",
355 .irq_enable = virtio_gpio_irq_enable,
356 .irq_disable = virtio_gpio_irq_disable,
357 .irq_mask = virtio_gpio_irq_mask,
358 .irq_unmask = virtio_gpio_irq_unmask,
359 .irq_set_type = virtio_gpio_irq_set_type,
361 /* These are required to implement irqchip for slow busses */
362 .irq_bus_lock = virtio_gpio_irq_bus_lock,
363 .irq_bus_sync_unlock = virtio_gpio_irq_bus_sync_unlock,
366 static bool ignore_irq(struct virtio_gpio *vgpio, int gpio,
367 struct vgpio_irq_line *irq_line)
371 raw_spin_lock(&vgpio->eventq_lock);
372 irq_line->queued = false;
374 /* Interrupt is disabled currently */
375 if (irq_line->masked || irq_line->disabled) {
381 * Buffer is returned as the interrupt was disabled earlier, but is
382 * enabled again now. Requeue the buffers.
384 if (irq_line->ires.status == VIRTIO_GPIO_IRQ_STATUS_INVALID) {
385 virtio_gpio_irq_prepare(vgpio, gpio);
390 if (WARN_ON(irq_line->ires.status != VIRTIO_GPIO_IRQ_STATUS_VALID))
394 raw_spin_unlock(&vgpio->eventq_lock);
399 static void virtio_gpio_event_vq(struct virtqueue *vq)
401 struct virtio_gpio *vgpio = vq->vdev->priv;
402 struct device *dev = &vgpio->vdev->dev;
403 struct vgpio_irq_line *irq_line;
408 irq_line = virtqueue_get_buf(vgpio->event_vq, &len);
412 if (len != sizeof(irq_line->ires)) {
413 dev_err(dev, "irq with incorrect length (%u : %u)\n",
414 len, (unsigned int)sizeof(irq_line->ires));
419 * Find GPIO line number from the offset of irq_line within the
420 * irq_lines block. We can also get GPIO number from
421 * irq-request, but better not to rely on a buffer returned by
424 gpio = irq_line - vgpio->irq_lines;
425 WARN_ON(gpio >= vgpio->gc.ngpio);
427 if (unlikely(ignore_irq(vgpio, gpio, irq_line)))
430 ret = generic_handle_domain_irq(vgpio->gc.irq.domain, gpio);
432 dev_err(dev, "failed to handle interrupt: %d\n", ret);
436 static void virtio_gpio_request_vq(struct virtqueue *vq)
438 struct virtio_gpio_line *line;
442 line = virtqueue_get_buf(vq, &len);
447 complete(&line->completion);
451 static void virtio_gpio_free_vqs(struct virtio_device *vdev)
453 virtio_reset_device(vdev);
454 vdev->config->del_vqs(vdev);
457 static int virtio_gpio_alloc_vqs(struct virtio_gpio *vgpio,
458 struct virtio_device *vdev)
460 const char * const names[] = { "requestq", "eventq" };
461 vq_callback_t *cbs[] = {
462 virtio_gpio_request_vq,
463 virtio_gpio_event_vq,
465 struct virtqueue *vqs[2] = { NULL, NULL };
468 ret = virtio_find_vqs(vdev, vgpio->irq_lines ? 2 : 1, vqs, cbs, names, NULL);
470 dev_err(&vdev->dev, "failed to find vqs: %d\n", ret);
475 dev_err(&vdev->dev, "failed to find requestq vq\n");
478 vgpio->request_vq = vqs[0];
480 if (vgpio->irq_lines && !vqs[1]) {
481 dev_err(&vdev->dev, "failed to find eventq vq\n");
484 vgpio->event_vq = vqs[1];
489 if (vqs[0] || vqs[1])
490 virtio_gpio_free_vqs(vdev);
495 static const char **virtio_gpio_get_names(struct virtio_gpio *vgpio,
496 u32 gpio_names_size, u16 ngpio)
498 struct virtio_gpio_response_get_names *res;
499 struct device *dev = &vgpio->vdev->dev;
500 u8 *gpio_names, *str;
504 if (!gpio_names_size)
507 len = sizeof(*res) + gpio_names_size;
508 res = devm_kzalloc(dev, len, GFP_KERNEL);
511 gpio_names = res->value;
513 ret = _virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_NAMES, 0, 0, NULL,
516 dev_err(dev, "Failed to get GPIO names: %d\n", ret);
520 names = devm_kcalloc(dev, ngpio, sizeof(*names), GFP_KERNEL);
524 /* NULL terminate the string instead of checking it */
525 gpio_names[gpio_names_size - 1] = '\0';
527 for (i = 0, str = gpio_names; i < ngpio; i++) {
529 str += strlen(str) + 1; /* zero-length strings are allowed */
531 if (str > gpio_names + gpio_names_size) {
532 dev_err(dev, "gpio_names block is too short (%d)\n", i);
540 static int virtio_gpio_probe(struct virtio_device *vdev)
542 struct virtio_gpio_config config;
543 struct device *dev = &vdev->dev;
544 struct virtio_gpio *vgpio;
549 vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
553 /* Read configuration */
554 virtio_cread_bytes(vdev, 0, &config, sizeof(config));
555 gpio_names_size = le32_to_cpu(config.gpio_names_size);
556 ngpio = le16_to_cpu(config.ngpio);
558 dev_err(dev, "Number of GPIOs can't be zero\n");
562 vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
566 for (i = 0; i < ngpio; i++) {
567 mutex_init(&vgpio->lines[i].lock);
568 init_completion(&vgpio->lines[i].completion);
571 mutex_init(&vgpio->lock);
575 vgpio->gc.free = virtio_gpio_free;
576 vgpio->gc.get_direction = virtio_gpio_get_direction;
577 vgpio->gc.direction_input = virtio_gpio_direction_input;
578 vgpio->gc.direction_output = virtio_gpio_direction_output;
579 vgpio->gc.get = virtio_gpio_get;
580 vgpio->gc.set = virtio_gpio_set;
581 vgpio->gc.ngpio = ngpio;
582 vgpio->gc.base = -1; /* Allocate base dynamically */
583 vgpio->gc.label = dev_name(dev);
584 vgpio->gc.parent = dev;
585 vgpio->gc.owner = THIS_MODULE;
586 vgpio->gc.can_sleep = true;
588 /* Interrupt support */
589 if (virtio_has_feature(vdev, VIRTIO_GPIO_F_IRQ)) {
590 vgpio->irq_lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->irq_lines), GFP_KERNEL);
591 if (!vgpio->irq_lines)
594 /* The event comes from the outside so no parent handler */
595 vgpio->gc.irq.parent_handler = NULL;
596 vgpio->gc.irq.num_parents = 0;
597 vgpio->gc.irq.parents = NULL;
598 vgpio->gc.irq.default_type = IRQ_TYPE_NONE;
599 vgpio->gc.irq.handler = handle_level_irq;
600 vgpio->gc.irq.chip = &vgpio_irq_chip;
602 for (i = 0; i < ngpio; i++) {
603 vgpio->irq_lines[i].type = VIRTIO_GPIO_IRQ_TYPE_NONE;
604 vgpio->irq_lines[i].disabled = true;
605 vgpio->irq_lines[i].masked = true;
608 mutex_init(&vgpio->irq_lock);
609 raw_spin_lock_init(&vgpio->eventq_lock);
612 ret = virtio_gpio_alloc_vqs(vgpio, vdev);
616 /* Mark the device ready to perform operations from within probe() */
617 virtio_device_ready(vdev);
619 vgpio->gc.names = virtio_gpio_get_names(vgpio, gpio_names_size, ngpio);
621 ret = gpiochip_add_data(&vgpio->gc, vgpio);
623 virtio_gpio_free_vqs(vdev);
624 dev_err(dev, "Failed to add virtio-gpio controller\n");
630 static void virtio_gpio_remove(struct virtio_device *vdev)
632 struct virtio_gpio *vgpio = vdev->priv;
634 gpiochip_remove(&vgpio->gc);
635 virtio_gpio_free_vqs(vdev);
638 static const struct virtio_device_id id_table[] = {
639 { VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
642 MODULE_DEVICE_TABLE(virtio, id_table);
644 static const unsigned int features[] = {
648 static struct virtio_driver virtio_gpio_driver = {
649 .feature_table = features,
650 .feature_table_size = ARRAY_SIZE(features),
651 .id_table = id_table,
652 .probe = virtio_gpio_probe,
653 .remove = virtio_gpio_remove,
655 .name = KBUILD_MODNAME,
656 .owner = THIS_MODULE,
659 module_virtio_driver(virtio_gpio_driver);
661 MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>");
662 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
663 MODULE_DESCRIPTION("VirtIO GPIO driver");
664 MODULE_LICENSE("GPL");