2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/spinlock.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_console.h>
25 #include <linux/workqueue.h>
26 #include "hvc_console.h"
29 * This is a global struct for storing common data for all the devices
30 * this driver handles.
32 * Mainly, it has a linked list for all the consoles in one place so
33 * that callbacks from hvc for get_chars(), put_chars() work properly
34 * across multiple devices and multiple ports per device.
36 struct ports_driver_data {
38 * This is used to keep track of the number of hvc consoles
39 * spawned by this driver. This number is given as the first
40 * argument to hvc_alloc(). To correctly map an initial
41 * console spawned via hvc_instantiate to the console being
42 * hooked up via hvc_alloc, we need to pass the same vtermno.
44 * We also just assume the first console being initialised was
45 * the first one that got used as the initial console.
47 unsigned int next_vtermno;
49 /* All the console devices handled by this driver */
50 struct list_head consoles;
52 static struct ports_driver_data pdrvdata;
54 DEFINE_SPINLOCK(pdrvdata_lock);
56 /* This struct holds information that's relevant only for console ports */
58 /* We'll place all consoles in a list in the pdrvdata struct */
59 struct list_head list;
61 /* The hvc device associated with this console port */
62 struct hvc_struct *hvc;
65 * This number identifies the number that we used to register
66 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
67 * number passed on by the hvc callbacks to us to
68 * differentiate between the other console ports handled by
77 /* size of the buffer in *buf above */
80 /* used length of the buffer */
82 /* offset in the buf from which to consume data */
87 * This is a per-device struct that stores data common to all the
88 * ports for that device (vdev->priv).
92 * Workqueue handlers where we process deferred work after
95 struct work_struct control_work;
97 struct list_head ports;
99 /* To protect the list of ports */
100 spinlock_t ports_lock;
102 /* To protect the vq operations for the control channel */
105 /* The current config space is stored here */
106 struct virtio_console_config config;
108 /* The virtio device we're associated with */
109 struct virtio_device *vdev;
112 * A couple of virtqueues for the control channel: one for
113 * guest->host transfers, one for host->guest transfers
115 struct virtqueue *c_ivq, *c_ovq;
117 /* Array of per-port IO virtqueues */
118 struct virtqueue **in_vqs, **out_vqs;
121 /* This struct holds the per-port data */
123 /* Next port in the list, head is in the ports_device */
124 struct list_head list;
126 /* Pointer to the parent virtio_console device */
127 struct ports_device *portdev;
129 /* The current buffer from which data has to be fed to readers */
130 struct port_buffer *inbuf;
133 * To protect the operations on the in_vq associated with this
134 * port. Has to be a spinlock because it can be called from
135 * interrupt context (get_char()).
137 spinlock_t inbuf_lock;
139 /* The IO vqs for this port */
140 struct virtqueue *in_vq, *out_vq;
143 * The entries in this struct will be valid if this port is
144 * hooked up to an hvc console
148 /* The 'id' to identify the port with the Host */
152 /* This is the very early arch-specified put chars function. */
153 static int (*early_put_chars)(u32, const char *, int);
155 static struct port *find_port_by_vtermno(u32 vtermno)
158 struct console *cons;
161 spin_lock_irqsave(&pdrvdata_lock, flags);
162 list_for_each_entry(cons, &pdrvdata.consoles, list) {
163 if (cons->vtermno == vtermno) {
164 port = container_of(cons, struct port, cons);
170 spin_unlock_irqrestore(&pdrvdata_lock, flags);
174 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
179 spin_lock_irqsave(&portdev->ports_lock, flags);
180 list_for_each_entry(port, &portdev->ports, list)
185 spin_unlock_irqrestore(&portdev->ports_lock, flags);
190 static struct port *find_port_by_vq(struct ports_device *portdev,
191 struct virtqueue *vq)
196 spin_lock_irqsave(&portdev->ports_lock, flags);
197 list_for_each_entry(port, &portdev->ports, list)
198 if (port->in_vq == vq || port->out_vq == vq)
202 spin_unlock_irqrestore(&portdev->ports_lock, flags);
206 static bool is_console_port(struct port *port)
213 static inline bool use_multiport(struct ports_device *portdev)
216 * This condition can be true when put_chars is called from
221 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
224 static void free_buf(struct port_buffer *buf)
230 static struct port_buffer *alloc_buf(size_t buf_size)
232 struct port_buffer *buf;
234 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
237 buf->buf = kzalloc(buf_size, GFP_KERNEL);
242 buf->size = buf_size;
251 /* Callers should take appropriate locks */
252 static void *get_inbuf(struct port *port)
254 struct port_buffer *buf;
255 struct virtqueue *vq;
259 buf = vq->vq_ops->get_buf(vq, &len);
268 * Create a scatter-gather list representing our input buffer and put
271 * Callers should take appropriate locks.
273 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
275 struct scatterlist sg[1];
278 sg_init_one(sg, buf->buf, buf->size);
280 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
281 vq->vq_ops->kick(vq);
285 static bool port_has_data(struct port *port)
291 spin_lock_irqsave(&port->inbuf_lock, flags);
294 spin_unlock_irqrestore(&port->inbuf_lock, flags);
299 static ssize_t send_control_msg(struct port *port, unsigned int event,
302 struct scatterlist sg[1];
303 struct virtio_console_control cpkt;
304 struct virtqueue *vq;
307 if (!use_multiport(port->portdev))
314 vq = port->portdev->c_ovq;
316 sg_init_one(sg, &cpkt, sizeof(cpkt));
317 if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
318 vq->vq_ops->kick(vq);
319 while (!vq->vq_ops->get_buf(vq, &len))
325 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
327 struct scatterlist sg[1];
328 struct virtqueue *out_vq;
332 out_vq = port->out_vq;
334 sg_init_one(sg, in_buf, in_count);
335 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
337 /* Tell Host to go! */
338 out_vq->vq_ops->kick(out_vq);
346 * Wait till the host acknowledges it pushed out the data we
347 * sent. Also ensure we return to userspace the number of
348 * bytes that were successfully consumed by the host.
350 while (!out_vq->vq_ops->get_buf(out_vq, &len))
353 /* We're expected to return the amount of data we wrote */
358 * Give out the data that's requested from the buffer that we have
361 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count)
363 struct port_buffer *buf;
366 if (!out_count || !port_has_data(port))
370 if (out_count > buf->len - buf->offset)
371 out_count = buf->len - buf->offset;
373 memcpy(out_buf, buf->buf + buf->offset, out_count);
375 /* Return the number of bytes actually copied */
376 buf->offset += out_count;
378 if (buf->offset == buf->len) {
380 * We're done using all the data in this buffer.
381 * Re-queue so that the Host can send us more data.
383 spin_lock_irqsave(&port->inbuf_lock, flags);
386 if (add_inbuf(port->in_vq, buf) < 0)
387 dev_warn(&port->portdev->vdev->dev, "failed add_buf\n");
389 spin_unlock_irqrestore(&port->inbuf_lock, flags);
395 * The put_chars() callback is pretty straightforward.
397 * We turn the characters into a scatter-gather list, add it to the
398 * output queue and then kick the Host. Then we sit here waiting for
399 * it to finish: inefficient in theory, but in practice
400 * implementations will do it immediately (lguest's Launcher does).
402 static int put_chars(u32 vtermno, const char *buf, int count)
406 port = find_port_by_vtermno(vtermno);
410 if (unlikely(early_put_chars))
411 return early_put_chars(vtermno, buf, count);
413 return send_buf(port, (void *)buf, count);
417 * get_chars() is the callback from the hvc_console infrastructure
418 * when an interrupt is received.
420 * We call out to fill_readbuf that gets us the required data from the
421 * buffers that are queued up.
423 static int get_chars(u32 vtermno, char *buf, int count)
427 port = find_port_by_vtermno(vtermno);
431 /* If we don't have an input queue yet, we can't get input. */
432 BUG_ON(!port->in_vq);
434 return fill_readbuf(port, buf, count);
437 static void resize_console(struct port *port)
439 struct virtio_device *vdev;
442 vdev = port->portdev->vdev;
443 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
444 vdev->config->get(vdev,
445 offsetof(struct virtio_console_config, cols),
446 &ws.ws_col, sizeof(u16));
447 vdev->config->get(vdev,
448 offsetof(struct virtio_console_config, rows),
449 &ws.ws_row, sizeof(u16));
450 hvc_resize(port->cons.hvc, ws);
454 static void virtcons_apply_config(struct virtio_device *vdev)
456 resize_console(find_port_by_vtermno(0));
459 /* We set the configuration at this point, since we now have a tty */
460 static int notifier_add_vio(struct hvc_struct *hp, int data)
464 port = find_port_by_vtermno(hp->vtermno);
468 hp->irq_requested = 1;
469 resize_console(port);
474 static void notifier_del_vio(struct hvc_struct *hp, int data)
476 hp->irq_requested = 0;
479 /* The operations for console ports. */
480 static const struct hv_ops hv_ops = {
481 .get_chars = get_chars,
482 .put_chars = put_chars,
483 .notifier_add = notifier_add_vio,
484 .notifier_del = notifier_del_vio,
485 .notifier_hangup = notifier_del_vio,
489 * Console drivers are initialized very early so boot messages can go
490 * out, so we do things slightly differently from the generic virtio
491 * initialization of the net and block drivers.
493 * At this stage, the console is output-only. It's too early to set
494 * up a virtqueue, so we let the drivers do some boutique early-output
497 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
499 early_put_chars = put_chars;
500 return hvc_instantiate(0, 0, &hv_ops);
503 int init_port_console(struct port *port)
508 * The Host's telling us this port is a console port. Hook it
509 * up with an hvc console.
511 * To set up and manage our virtual console, we call
514 * The first argument of hvc_alloc() is the virtual console
515 * number. The second argument is the parameter for the
516 * notification mechanism (like irq number). We currently
517 * leave this as zero, virtqueues have implicit notifications.
519 * The third argument is a "struct hv_ops" containing the
520 * put_chars() get_chars(), notifier_add() and notifier_del()
521 * pointers. The final argument is the output buffer size: we
522 * can do any size, so we put PAGE_SIZE here.
524 port->cons.vtermno = pdrvdata.next_vtermno;
526 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
527 if (IS_ERR(port->cons.hvc)) {
528 ret = PTR_ERR(port->cons.hvc);
529 port->cons.hvc = NULL;
532 spin_lock_irq(&pdrvdata_lock);
533 pdrvdata.next_vtermno++;
534 list_add_tail(&port->cons.list, &pdrvdata.consoles);
535 spin_unlock_irq(&pdrvdata_lock);
540 /* Any private messages that the Host and Guest want to share */
541 static void handle_control_message(struct ports_device *portdev,
542 struct port_buffer *buf)
544 struct virtio_console_control *cpkt;
547 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
549 port = find_port_by_id(portdev, cpkt->id);
551 /* No valid header at start of buffer. Drop it. */
552 dev_dbg(&portdev->vdev->dev,
553 "Invalid index %u in control packet\n", cpkt->id);
557 switch (cpkt->event) {
558 case VIRTIO_CONSOLE_CONSOLE_PORT:
561 if (is_console_port(port))
564 init_port_console(port);
566 * Could remove the port here in case init fails - but
567 * have to notify the host first.
570 case VIRTIO_CONSOLE_RESIZE:
571 if (!is_console_port(port))
573 port->cons.hvc->irq_requested = 1;
574 resize_console(port);
579 static void control_work_handler(struct work_struct *work)
581 struct ports_device *portdev;
582 struct virtqueue *vq;
583 struct port_buffer *buf;
586 portdev = container_of(work, struct ports_device, control_work);
589 spin_lock(&portdev->cvq_lock);
590 while ((buf = vq->vq_ops->get_buf(vq, &len))) {
591 spin_unlock(&portdev->cvq_lock);
596 handle_control_message(portdev, buf);
598 spin_lock(&portdev->cvq_lock);
599 if (add_inbuf(portdev->c_ivq, buf) < 0) {
600 dev_warn(&portdev->vdev->dev,
601 "Error adding buffer to queue\n");
605 spin_unlock(&portdev->cvq_lock);
608 static void in_intr(struct virtqueue *vq)
613 port = find_port_by_vq(vq->vdev->priv, vq);
617 spin_lock_irqsave(&port->inbuf_lock, flags);
618 port->inbuf = get_inbuf(port);
620 spin_unlock_irqrestore(&port->inbuf_lock, flags);
622 if (is_console_port(port) && hvc_poll(port->cons.hvc))
626 static void control_intr(struct virtqueue *vq)
628 struct ports_device *portdev;
630 portdev = vq->vdev->priv;
631 schedule_work(&portdev->control_work);
634 static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
636 struct port_buffer *buf;
640 buf = alloc_buf(PAGE_SIZE);
645 ret = add_inbuf(vq, buf);
647 spin_unlock_irq(lock);
651 spin_unlock_irq(lock);
655 static int add_port(struct ports_device *portdev, u32 id)
658 struct port_buffer *inbuf;
661 port = kmalloc(sizeof(*port), GFP_KERNEL);
667 port->portdev = portdev;
671 port->cons.hvc = NULL;
673 port->in_vq = portdev->in_vqs[port->id];
674 port->out_vq = portdev->out_vqs[port->id];
676 spin_lock_init(&port->inbuf_lock);
678 inbuf = alloc_buf(PAGE_SIZE);
684 /* Register the input buffer the first time. */
685 add_inbuf(port->in_vq, inbuf);
688 * If we're not using multiport support, this has to be a console port
690 if (!use_multiport(port->portdev)) {
691 err = init_port_console(port);
696 spin_lock_irq(&portdev->ports_lock);
697 list_add_tail(&port->list, &port->portdev->ports);
698 spin_unlock_irq(&portdev->ports_lock);
701 * Tell the Host we're set so that it can send us various
702 * configuration parameters for this port (eg, port name,
703 * caching, whether this is a console port, etc.)
705 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
717 static int init_vqs(struct ports_device *portdev)
719 vq_callback_t **io_callbacks;
721 struct virtqueue **vqs;
722 u32 i, j, nr_ports, nr_queues;
725 nr_ports = portdev->config.max_nr_ports;
726 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
728 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
733 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
738 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
743 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
745 if (!portdev->in_vqs) {
749 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
751 if (!portdev->out_vqs) {
757 * For backward compat (newer host but older guest), the host
758 * spawns a console port first and also inits the vqs for port
762 io_callbacks[j] = in_intr;
763 io_callbacks[j + 1] = NULL;
764 io_names[j] = "input";
765 io_names[j + 1] = "output";
768 if (use_multiport(portdev)) {
769 io_callbacks[j] = control_intr;
770 io_callbacks[j + 1] = NULL;
771 io_names[j] = "control-i";
772 io_names[j + 1] = "control-o";
774 for (i = 1; i < nr_ports; i++) {
776 io_callbacks[j] = in_intr;
777 io_callbacks[j + 1] = NULL;
778 io_names[j] = "input";
779 io_names[j + 1] = "output";
782 /* Find the queues. */
783 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
785 (const char **)io_names);
790 portdev->in_vqs[0] = vqs[0];
791 portdev->out_vqs[0] = vqs[1];
793 if (use_multiport(portdev)) {
794 portdev->c_ivq = vqs[j];
795 portdev->c_ovq = vqs[j + 1];
797 for (i = 1; i < nr_ports; i++) {
799 portdev->in_vqs[i] = vqs[j];
800 portdev->out_vqs[i] = vqs[j + 1];
814 kfree(portdev->out_vqs);
816 kfree(portdev->in_vqs);
824 * Once we're further in boot, we get probed like any other virtio
827 * If the host also supports multiple console ports, we check the
828 * config space to see how many ports the host has spawned. We
829 * initialize each port found.
831 static int __devinit virtcons_probe(struct virtio_device *vdev)
833 struct ports_device *portdev;
838 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
844 /* Attach this portdev to this virtio_device, and vice-versa. */
845 portdev->vdev = vdev;
846 vdev->priv = portdev;
849 portdev->config.nr_ports = 1;
850 portdev->config.max_nr_ports = 1;
851 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
853 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
855 vdev->config->get(vdev, offsetof(struct virtio_console_config,
857 &portdev->config.nr_ports,
858 sizeof(portdev->config.nr_ports));
859 vdev->config->get(vdev, offsetof(struct virtio_console_config,
861 &portdev->config.max_nr_ports,
862 sizeof(portdev->config.max_nr_ports));
863 if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
865 "More ports (%u) specified than allowed (%u). Will init %u ports.",
866 portdev->config.nr_ports,
867 portdev->config.max_nr_ports,
868 portdev->config.max_nr_ports);
870 portdev->config.nr_ports = portdev->config.max_nr_ports;
874 /* Let the Host know we support multiple ports.*/
875 vdev->config->finalize_features(vdev);
877 err = init_vqs(portdev);
879 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
883 spin_lock_init(&portdev->ports_lock);
884 INIT_LIST_HEAD(&portdev->ports);
887 spin_lock_init(&portdev->cvq_lock);
888 INIT_WORK(&portdev->control_work, &control_work_handler);
890 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
893 for (i = 0; i < portdev->config.nr_ports; i++)
894 add_port(portdev, i);
896 /* Start using the new console output. */
897 early_put_chars = NULL;
901 vdev->config->del_vqs(vdev);
902 kfree(portdev->in_vqs);
903 kfree(portdev->out_vqs);
910 static struct virtio_device_id id_table[] = {
911 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
915 static unsigned int features[] = {
916 VIRTIO_CONSOLE_F_SIZE,
917 VIRTIO_CONSOLE_F_MULTIPORT,
920 static struct virtio_driver virtio_console = {
921 .feature_table = features,
922 .feature_table_size = ARRAY_SIZE(features),
923 .driver.name = KBUILD_MODNAME,
924 .driver.owner = THIS_MODULE,
925 .id_table = id_table,
926 .probe = virtcons_probe,
927 .config_changed = virtcons_apply_config,
930 static int __init init(void)
932 INIT_LIST_HEAD(&pdrvdata.consoles);
934 return register_virtio_driver(&virtio_console);
938 MODULE_DEVICE_TABLE(virtio, id_table);
939 MODULE_DESCRIPTION("Virtio console driver");
940 MODULE_LICENSE("GPL");