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/cdev.h>
20 #include <linux/device.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/virtio.h>
26 #include <linux/virtio_console.h>
27 #include <linux/workqueue.h>
28 #include "hvc_console.h"
31 * This is a global struct for storing common data for all the devices
32 * this driver handles.
34 * Mainly, it has a linked list for all the consoles in one place so
35 * that callbacks from hvc for get_chars(), put_chars() work properly
36 * across multiple devices and multiple ports per device.
38 struct ports_driver_data {
39 /* Used for registering chardevs */
42 /* Number of devices this driver is handling */
46 * This is used to keep track of the number of hvc consoles
47 * spawned by this driver. This number is given as the first
48 * argument to hvc_alloc(). To correctly map an initial
49 * console spawned via hvc_instantiate to the console being
50 * hooked up via hvc_alloc, we need to pass the same vtermno.
52 * We also just assume the first console being initialised was
53 * the first one that got used as the initial console.
55 unsigned int next_vtermno;
57 /* All the console devices handled by this driver */
58 struct list_head consoles;
60 static struct ports_driver_data pdrvdata;
62 DEFINE_SPINLOCK(pdrvdata_lock);
64 /* This struct holds information that's relevant only for console ports */
66 /* We'll place all consoles in a list in the pdrvdata struct */
67 struct list_head list;
69 /* The hvc device associated with this console port */
70 struct hvc_struct *hvc;
73 * This number identifies the number that we used to register
74 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
75 * number passed on by the hvc callbacks to us to
76 * differentiate between the other console ports handled by
85 /* size of the buffer in *buf above */
88 /* used length of the buffer */
90 /* offset in the buf from which to consume data */
95 * This is a per-device struct that stores data common to all the
96 * ports for that device (vdev->priv).
100 * Workqueue handlers where we process deferred work after
103 struct work_struct control_work;
105 struct list_head ports;
107 /* To protect the list of ports */
108 spinlock_t ports_lock;
110 /* To protect the vq operations for the control channel */
113 /* The current config space is stored here */
114 struct virtio_console_config config;
116 /* The virtio device we're associated with */
117 struct virtio_device *vdev;
120 * A couple of virtqueues for the control channel: one for
121 * guest->host transfers, one for host->guest transfers
123 struct virtqueue *c_ivq, *c_ovq;
125 /* Array of per-port IO virtqueues */
126 struct virtqueue **in_vqs, **out_vqs;
128 /* Used for numbering devices for sysfs and debugfs */
129 unsigned int drv_index;
131 /* Major number for this device. Ports will be created as minors. */
135 /* This struct holds the per-port data */
137 /* Next port in the list, head is in the ports_device */
138 struct list_head list;
140 /* Pointer to the parent virtio_console device */
141 struct ports_device *portdev;
143 /* The current buffer from which data has to be fed to readers */
144 struct port_buffer *inbuf;
147 * To protect the operations on the in_vq associated with this
148 * port. Has to be a spinlock because it can be called from
149 * interrupt context (get_char()).
151 spinlock_t inbuf_lock;
153 /* The IO vqs for this port */
154 struct virtqueue *in_vq, *out_vq;
157 * The entries in this struct will be valid if this port is
158 * hooked up to an hvc console
162 /* Each port associates with a separate char device */
166 /* The 'id' to identify the port with the Host */
170 /* This is the very early arch-specified put chars function. */
171 static int (*early_put_chars)(u32, const char *, int);
173 static struct port *find_port_by_vtermno(u32 vtermno)
176 struct console *cons;
179 spin_lock_irqsave(&pdrvdata_lock, flags);
180 list_for_each_entry(cons, &pdrvdata.consoles, list) {
181 if (cons->vtermno == vtermno) {
182 port = container_of(cons, struct port, cons);
188 spin_unlock_irqrestore(&pdrvdata_lock, flags);
192 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
197 spin_lock_irqsave(&portdev->ports_lock, flags);
198 list_for_each_entry(port, &portdev->ports, list)
203 spin_unlock_irqrestore(&portdev->ports_lock, flags);
208 static struct port *find_port_by_vq(struct ports_device *portdev,
209 struct virtqueue *vq)
214 spin_lock_irqsave(&portdev->ports_lock, flags);
215 list_for_each_entry(port, &portdev->ports, list)
216 if (port->in_vq == vq || port->out_vq == vq)
220 spin_unlock_irqrestore(&portdev->ports_lock, flags);
224 static bool is_console_port(struct port *port)
231 static inline bool use_multiport(struct ports_device *portdev)
234 * This condition can be true when put_chars is called from
239 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
242 static void free_buf(struct port_buffer *buf)
248 static struct port_buffer *alloc_buf(size_t buf_size)
250 struct port_buffer *buf;
252 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
255 buf->buf = kzalloc(buf_size, GFP_KERNEL);
260 buf->size = buf_size;
269 /* Callers should take appropriate locks */
270 static void *get_inbuf(struct port *port)
272 struct port_buffer *buf;
273 struct virtqueue *vq;
277 buf = vq->vq_ops->get_buf(vq, &len);
286 * Create a scatter-gather list representing our input buffer and put
289 * Callers should take appropriate locks.
291 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
293 struct scatterlist sg[1];
296 sg_init_one(sg, buf->buf, buf->size);
298 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
299 vq->vq_ops->kick(vq);
303 static bool port_has_data(struct port *port)
309 spin_lock_irqsave(&port->inbuf_lock, flags);
312 spin_unlock_irqrestore(&port->inbuf_lock, flags);
317 static ssize_t send_control_msg(struct port *port, unsigned int event,
320 struct scatterlist sg[1];
321 struct virtio_console_control cpkt;
322 struct virtqueue *vq;
325 if (!use_multiport(port->portdev))
332 vq = port->portdev->c_ovq;
334 sg_init_one(sg, &cpkt, sizeof(cpkt));
335 if (vq->vq_ops->add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
336 vq->vq_ops->kick(vq);
337 while (!vq->vq_ops->get_buf(vq, &len))
343 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
345 struct scatterlist sg[1];
346 struct virtqueue *out_vq;
350 out_vq = port->out_vq;
352 sg_init_one(sg, in_buf, in_count);
353 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
355 /* Tell Host to go! */
356 out_vq->vq_ops->kick(out_vq);
364 * Wait till the host acknowledges it pushed out the data we
365 * sent. Also ensure we return to userspace the number of
366 * bytes that were successfully consumed by the host.
368 while (!out_vq->vq_ops->get_buf(out_vq, &len))
371 /* We're expected to return the amount of data we wrote */
376 * Give out the data that's requested from the buffer that we have
379 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
382 struct port_buffer *buf;
385 if (!out_count || !port_has_data(port))
389 out_count = min(out_count, buf->len - buf->offset);
394 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
398 memcpy(out_buf, buf->buf + buf->offset, out_count);
401 buf->offset += out_count;
403 if (buf->offset == buf->len) {
405 * We're done using all the data in this buffer.
406 * Re-queue so that the Host can send us more data.
408 spin_lock_irqsave(&port->inbuf_lock, flags);
411 if (add_inbuf(port->in_vq, buf) < 0)
412 dev_warn(port->dev, "failed add_buf\n");
414 spin_unlock_irqrestore(&port->inbuf_lock, flags);
416 /* Return the number of bytes actually copied */
421 * The put_chars() callback is pretty straightforward.
423 * We turn the characters into a scatter-gather list, add it to the
424 * output queue and then kick the Host. Then we sit here waiting for
425 * it to finish: inefficient in theory, but in practice
426 * implementations will do it immediately (lguest's Launcher does).
428 static int put_chars(u32 vtermno, const char *buf, int count)
432 port = find_port_by_vtermno(vtermno);
436 if (unlikely(early_put_chars))
437 return early_put_chars(vtermno, buf, count);
439 return send_buf(port, (void *)buf, count);
443 * get_chars() is the callback from the hvc_console infrastructure
444 * when an interrupt is received.
446 * We call out to fill_readbuf that gets us the required data from the
447 * buffers that are queued up.
449 static int get_chars(u32 vtermno, char *buf, int count)
453 port = find_port_by_vtermno(vtermno);
457 /* If we don't have an input queue yet, we can't get input. */
458 BUG_ON(!port->in_vq);
460 return fill_readbuf(port, buf, count, false);
463 static void resize_console(struct port *port)
465 struct virtio_device *vdev;
468 vdev = port->portdev->vdev;
469 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
470 vdev->config->get(vdev,
471 offsetof(struct virtio_console_config, cols),
472 &ws.ws_col, sizeof(u16));
473 vdev->config->get(vdev,
474 offsetof(struct virtio_console_config, rows),
475 &ws.ws_row, sizeof(u16));
476 hvc_resize(port->cons.hvc, ws);
480 static void virtcons_apply_config(struct virtio_device *vdev)
482 resize_console(find_port_by_vtermno(0));
485 /* We set the configuration at this point, since we now have a tty */
486 static int notifier_add_vio(struct hvc_struct *hp, int data)
490 port = find_port_by_vtermno(hp->vtermno);
494 hp->irq_requested = 1;
495 resize_console(port);
500 static void notifier_del_vio(struct hvc_struct *hp, int data)
502 hp->irq_requested = 0;
505 /* The operations for console ports. */
506 static const struct hv_ops hv_ops = {
507 .get_chars = get_chars,
508 .put_chars = put_chars,
509 .notifier_add = notifier_add_vio,
510 .notifier_del = notifier_del_vio,
511 .notifier_hangup = notifier_del_vio,
515 * Console drivers are initialized very early so boot messages can go
516 * out, so we do things slightly differently from the generic virtio
517 * initialization of the net and block drivers.
519 * At this stage, the console is output-only. It's too early to set
520 * up a virtqueue, so we let the drivers do some boutique early-output
523 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
525 early_put_chars = put_chars;
526 return hvc_instantiate(0, 0, &hv_ops);
529 int init_port_console(struct port *port)
534 * The Host's telling us this port is a console port. Hook it
535 * up with an hvc console.
537 * To set up and manage our virtual console, we call
540 * The first argument of hvc_alloc() is the virtual console
541 * number. The second argument is the parameter for the
542 * notification mechanism (like irq number). We currently
543 * leave this as zero, virtqueues have implicit notifications.
545 * The third argument is a "struct hv_ops" containing the
546 * put_chars() get_chars(), notifier_add() and notifier_del()
547 * pointers. The final argument is the output buffer size: we
548 * can do any size, so we put PAGE_SIZE here.
550 port->cons.vtermno = pdrvdata.next_vtermno;
552 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
553 if (IS_ERR(port->cons.hvc)) {
554 ret = PTR_ERR(port->cons.hvc);
555 port->cons.hvc = NULL;
558 spin_lock_irq(&pdrvdata_lock);
559 pdrvdata.next_vtermno++;
560 list_add_tail(&port->cons.list, &pdrvdata.consoles);
561 spin_unlock_irq(&pdrvdata_lock);
566 /* Any private messages that the Host and Guest want to share */
567 static void handle_control_message(struct ports_device *portdev,
568 struct port_buffer *buf)
570 struct virtio_console_control *cpkt;
573 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
575 port = find_port_by_id(portdev, cpkt->id);
577 /* No valid header at start of buffer. Drop it. */
578 dev_dbg(&portdev->vdev->dev,
579 "Invalid index %u in control packet\n", cpkt->id);
583 switch (cpkt->event) {
584 case VIRTIO_CONSOLE_CONSOLE_PORT:
587 if (is_console_port(port))
590 init_port_console(port);
592 * Could remove the port here in case init fails - but
593 * have to notify the host first.
596 case VIRTIO_CONSOLE_RESIZE:
597 if (!is_console_port(port))
599 port->cons.hvc->irq_requested = 1;
600 resize_console(port);
605 static void control_work_handler(struct work_struct *work)
607 struct ports_device *portdev;
608 struct virtqueue *vq;
609 struct port_buffer *buf;
612 portdev = container_of(work, struct ports_device, control_work);
615 spin_lock(&portdev->cvq_lock);
616 while ((buf = vq->vq_ops->get_buf(vq, &len))) {
617 spin_unlock(&portdev->cvq_lock);
622 handle_control_message(portdev, buf);
624 spin_lock(&portdev->cvq_lock);
625 if (add_inbuf(portdev->c_ivq, buf) < 0) {
626 dev_warn(&portdev->vdev->dev,
627 "Error adding buffer to queue\n");
631 spin_unlock(&portdev->cvq_lock);
634 static void in_intr(struct virtqueue *vq)
639 port = find_port_by_vq(vq->vdev->priv, vq);
643 spin_lock_irqsave(&port->inbuf_lock, flags);
644 port->inbuf = get_inbuf(port);
646 spin_unlock_irqrestore(&port->inbuf_lock, flags);
648 if (is_console_port(port) && hvc_poll(port->cons.hvc))
652 static void control_intr(struct virtqueue *vq)
654 struct ports_device *portdev;
656 portdev = vq->vdev->priv;
657 schedule_work(&portdev->control_work);
660 static void fill_queue(struct virtqueue *vq, spinlock_t *lock)
662 struct port_buffer *buf;
666 buf = alloc_buf(PAGE_SIZE);
671 ret = add_inbuf(vq, buf);
673 spin_unlock_irq(lock);
677 spin_unlock_irq(lock);
681 static int add_port(struct ports_device *portdev, u32 id)
684 struct port_buffer *inbuf;
688 port = kmalloc(sizeof(*port), GFP_KERNEL);
694 port->portdev = portdev;
698 port->cons.hvc = NULL;
700 port->in_vq = portdev->in_vqs[port->id];
701 port->out_vq = portdev->out_vqs[port->id];
703 cdev_init(&port->cdev, NULL);
705 devt = MKDEV(portdev->chr_major, id);
706 err = cdev_add(&port->cdev, devt, 1);
708 dev_err(&port->portdev->vdev->dev,
709 "Error %d adding cdev for port %u\n", err, id);
712 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
713 devt, port, "vport%up%u",
714 port->portdev->drv_index, id);
715 if (IS_ERR(port->dev)) {
716 err = PTR_ERR(port->dev);
717 dev_err(&port->portdev->vdev->dev,
718 "Error %d creating device for port %u\n",
723 spin_lock_init(&port->inbuf_lock);
725 inbuf = alloc_buf(PAGE_SIZE);
731 /* Register the input buffer the first time. */
732 add_inbuf(port->in_vq, inbuf);
735 * If we're not using multiport support, this has to be a console port
737 if (!use_multiport(port->portdev)) {
738 err = init_port_console(port);
743 spin_lock_irq(&portdev->ports_lock);
744 list_add_tail(&port->list, &port->portdev->ports);
745 spin_unlock_irq(&portdev->ports_lock);
748 * Tell the Host we're set so that it can send us various
749 * configuration parameters for this port (eg, port name,
750 * caching, whether this is a console port, etc.)
752 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
759 device_destroy(pdrvdata.class, port->dev->devt);
761 cdev_del(&port->cdev);
768 static int init_vqs(struct ports_device *portdev)
770 vq_callback_t **io_callbacks;
772 struct virtqueue **vqs;
773 u32 i, j, nr_ports, nr_queues;
776 nr_ports = portdev->config.max_nr_ports;
777 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
779 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
784 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
789 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
794 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
796 if (!portdev->in_vqs) {
800 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
802 if (!portdev->out_vqs) {
808 * For backward compat (newer host but older guest), the host
809 * spawns a console port first and also inits the vqs for port
813 io_callbacks[j] = in_intr;
814 io_callbacks[j + 1] = NULL;
815 io_names[j] = "input";
816 io_names[j + 1] = "output";
819 if (use_multiport(portdev)) {
820 io_callbacks[j] = control_intr;
821 io_callbacks[j + 1] = NULL;
822 io_names[j] = "control-i";
823 io_names[j + 1] = "control-o";
825 for (i = 1; i < nr_ports; i++) {
827 io_callbacks[j] = in_intr;
828 io_callbacks[j + 1] = NULL;
829 io_names[j] = "input";
830 io_names[j + 1] = "output";
833 /* Find the queues. */
834 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
836 (const char **)io_names);
841 portdev->in_vqs[0] = vqs[0];
842 portdev->out_vqs[0] = vqs[1];
844 if (use_multiport(portdev)) {
845 portdev->c_ivq = vqs[j];
846 portdev->c_ovq = vqs[j + 1];
848 for (i = 1; i < nr_ports; i++) {
850 portdev->in_vqs[i] = vqs[j];
851 portdev->out_vqs[i] = vqs[j + 1];
865 kfree(portdev->out_vqs);
867 kfree(portdev->in_vqs);
874 static const struct file_operations portdev_fops = {
875 .owner = THIS_MODULE,
879 * Once we're further in boot, we get probed like any other virtio
882 * If the host also supports multiple console ports, we check the
883 * config space to see how many ports the host has spawned. We
884 * initialize each port found.
886 static int __devinit virtcons_probe(struct virtio_device *vdev)
888 struct ports_device *portdev;
893 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
899 /* Attach this portdev to this virtio_device, and vice-versa. */
900 portdev->vdev = vdev;
901 vdev->priv = portdev;
903 spin_lock_irq(&pdrvdata_lock);
904 portdev->drv_index = pdrvdata.index++;
905 spin_unlock_irq(&pdrvdata_lock);
907 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
909 if (portdev->chr_major < 0) {
911 "Error %d registering chrdev for device %u\n",
912 portdev->chr_major, portdev->drv_index);
913 err = portdev->chr_major;
918 portdev->config.nr_ports = 1;
919 portdev->config.max_nr_ports = 1;
920 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
922 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
924 vdev->config->get(vdev, offsetof(struct virtio_console_config,
926 &portdev->config.nr_ports,
927 sizeof(portdev->config.nr_ports));
928 vdev->config->get(vdev, offsetof(struct virtio_console_config,
930 &portdev->config.max_nr_ports,
931 sizeof(portdev->config.max_nr_ports));
932 if (portdev->config.nr_ports > portdev->config.max_nr_ports) {
934 "More ports (%u) specified than allowed (%u). Will init %u ports.",
935 portdev->config.nr_ports,
936 portdev->config.max_nr_ports,
937 portdev->config.max_nr_ports);
939 portdev->config.nr_ports = portdev->config.max_nr_ports;
943 /* Let the Host know we support multiple ports.*/
944 vdev->config->finalize_features(vdev);
946 err = init_vqs(portdev);
948 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
952 spin_lock_init(&portdev->ports_lock);
953 INIT_LIST_HEAD(&portdev->ports);
956 spin_lock_init(&portdev->cvq_lock);
957 INIT_WORK(&portdev->control_work, &control_work_handler);
959 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
962 for (i = 0; i < portdev->config.nr_ports; i++)
963 add_port(portdev, i);
965 /* Start using the new console output. */
966 early_put_chars = NULL;
970 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
977 static struct virtio_device_id id_table[] = {
978 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
982 static unsigned int features[] = {
983 VIRTIO_CONSOLE_F_SIZE,
984 VIRTIO_CONSOLE_F_MULTIPORT,
987 static struct virtio_driver virtio_console = {
988 .feature_table = features,
989 .feature_table_size = ARRAY_SIZE(features),
990 .driver.name = KBUILD_MODNAME,
991 .driver.owner = THIS_MODULE,
992 .id_table = id_table,
993 .probe = virtcons_probe,
994 .config_changed = virtcons_apply_config,
997 static int __init init(void)
1001 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1002 if (IS_ERR(pdrvdata.class)) {
1003 err = PTR_ERR(pdrvdata.class);
1004 pr_err("Error %d creating virtio-ports class\n", err);
1007 INIT_LIST_HEAD(&pdrvdata.consoles);
1009 return register_virtio_driver(&virtio_console);
1013 MODULE_DEVICE_TABLE(virtio, id_table);
1014 MODULE_DESCRIPTION("Virtio console driver");
1015 MODULE_LICENSE("GPL");