2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/virtio.h>
23 #include <linux/virtio_console.h>
24 #include "hvc_console.h"
27 * This is a global struct for storing common data for all the devices
28 * this driver handles.
30 * Mainly, it has a linked list for all the consoles in one place so
31 * that callbacks from hvc for get_chars(), put_chars() work properly
32 * across multiple devices and multiple ports per device.
34 struct ports_driver_data {
36 * This is used to keep track of the number of hvc consoles
37 * spawned by this driver. This number is given as the first
38 * argument to hvc_alloc(). To correctly map an initial
39 * console spawned via hvc_instantiate to the console being
40 * hooked up via hvc_alloc, we need to pass the same vtermno.
42 * We also just assume the first console being initialised was
43 * the first one that got used as the initial console.
45 unsigned int next_vtermno;
47 /* All the console devices handled by this driver */
48 struct list_head consoles;
50 static struct ports_driver_data pdrvdata;
52 DEFINE_SPINLOCK(pdrvdata_lock);
54 /* This struct holds information that's relevant only for console ports */
56 /* We'll place all consoles in a list in the pdrvdata struct */
57 struct list_head list;
59 /* The hvc device associated with this console port */
60 struct hvc_struct *hvc;
63 * This number identifies the number that we used to register
64 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
65 * number passed on by the hvc callbacks to us to
66 * differentiate between the other console ports handled by
73 * This is a per-device struct that stores data common to all the
74 * ports for that device (vdev->priv).
77 /* Array of per-port IO virtqueues */
78 struct virtqueue **in_vqs, **out_vqs;
80 struct virtio_device *vdev;
86 /* size of the buffer in *buf above */
89 /* used length of the buffer */
91 /* offset in the buf from which to consume data */
95 /* This struct holds the per-port data */
97 /* Pointer to the parent virtio_console device */
98 struct ports_device *portdev;
100 /* The current buffer from which data has to be fed to readers */
101 struct port_buffer *inbuf;
104 * To protect the operations on the in_vq associated with this
105 * port. Has to be a spinlock because it can be called from
106 * interrupt context (get_char()).
108 spinlock_t inbuf_lock;
110 /* The IO vqs for this port */
111 struct virtqueue *in_vq, *out_vq;
114 * The entries in this struct will be valid if this port is
115 * hooked up to an hvc console
120 /* This is the very early arch-specified put chars function. */
121 static int (*early_put_chars)(u32, const char *, int);
123 static struct port *find_port_by_vtermno(u32 vtermno)
126 struct console *cons;
129 spin_lock_irqsave(&pdrvdata_lock, flags);
130 list_for_each_entry(cons, &pdrvdata.consoles, list) {
131 if (cons->vtermno == vtermno) {
132 port = container_of(cons, struct port, cons);
138 spin_unlock_irqrestore(&pdrvdata_lock, flags);
142 static struct port *find_port_by_vq(struct ports_device *portdev,
143 struct virtqueue *vq)
146 struct console *cons;
149 spin_lock_irqsave(&pdrvdata_lock, flags);
150 list_for_each_entry(cons, &pdrvdata.consoles, list) {
151 port = container_of(cons, struct port, cons);
152 if (port->in_vq == vq || port->out_vq == vq)
157 spin_unlock_irqrestore(&pdrvdata_lock, flags);
161 static void free_buf(struct port_buffer *buf)
167 static struct port_buffer *alloc_buf(size_t buf_size)
169 struct port_buffer *buf;
171 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
174 buf->buf = kzalloc(buf_size, GFP_KERNEL);
179 buf->size = buf_size;
188 /* Callers should take appropriate locks */
189 static void *get_inbuf(struct port *port)
191 struct port_buffer *buf;
192 struct virtqueue *vq;
196 buf = vq->vq_ops->get_buf(vq, &len);
205 * Create a scatter-gather list representing our input buffer and put
208 * Callers should take appropriate locks.
210 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
212 struct scatterlist sg[1];
215 sg_init_one(sg, buf->buf, buf->size);
217 ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf);
218 vq->vq_ops->kick(vq);
222 static bool port_has_data(struct port *port)
228 spin_lock_irqsave(&port->inbuf_lock, flags);
231 spin_unlock_irqrestore(&port->inbuf_lock, flags);
236 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count)
238 struct scatterlist sg[1];
239 struct virtqueue *out_vq;
243 out_vq = port->out_vq;
245 sg_init_one(sg, in_buf, in_count);
246 ret = out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, in_buf);
248 /* Tell Host to go! */
249 out_vq->vq_ops->kick(out_vq);
257 * Wait till the host acknowledges it pushed out the data we
258 * sent. Also ensure we return to userspace the number of
259 * bytes that were successfully consumed by the host.
261 while (!out_vq->vq_ops->get_buf(out_vq, &len))
264 /* We're expected to return the amount of data we wrote */
269 * Give out the data that's requested from the buffer that we have
272 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count)
274 struct port_buffer *buf;
277 if (!out_count || !port_has_data(port))
281 if (out_count > buf->len - buf->offset)
282 out_count = buf->len - buf->offset;
284 memcpy(out_buf, buf->buf + buf->offset, out_count);
286 /* Return the number of bytes actually copied */
287 buf->offset += out_count;
289 if (buf->offset == buf->len) {
291 * We're done using all the data in this buffer.
292 * Re-queue so that the Host can send us more data.
294 spin_lock_irqsave(&port->inbuf_lock, flags);
297 if (add_inbuf(port->in_vq, buf) < 0)
298 dev_warn(&port->portdev->vdev->dev, "failed add_buf\n");
300 spin_unlock_irqrestore(&port->inbuf_lock, flags);
306 * The put_chars() callback is pretty straightforward.
308 * We turn the characters into a scatter-gather list, add it to the
309 * output queue and then kick the Host. Then we sit here waiting for
310 * it to finish: inefficient in theory, but in practice
311 * implementations will do it immediately (lguest's Launcher does).
313 static int put_chars(u32 vtermno, const char *buf, int count)
317 port = find_port_by_vtermno(vtermno);
321 if (unlikely(early_put_chars))
322 return early_put_chars(vtermno, buf, count);
324 return send_buf(port, (void *)buf, count);
328 * get_chars() is the callback from the hvc_console infrastructure
329 * when an interrupt is received.
331 * We call out to fill_readbuf that gets us the required data from the
332 * buffers that are queued up.
334 static int get_chars(u32 vtermno, char *buf, int count)
338 port = find_port_by_vtermno(vtermno);
342 /* If we don't have an input queue yet, we can't get input. */
343 BUG_ON(!port->in_vq);
345 return fill_readbuf(port, buf, count);
348 static void resize_console(struct port *port)
350 struct virtio_device *vdev;
353 vdev = port->portdev->vdev;
354 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
355 vdev->config->get(vdev,
356 offsetof(struct virtio_console_config, cols),
357 &ws.ws_col, sizeof(u16));
358 vdev->config->get(vdev,
359 offsetof(struct virtio_console_config, rows),
360 &ws.ws_row, sizeof(u16));
361 hvc_resize(port->cons.hvc, ws);
365 static void virtcons_apply_config(struct virtio_device *vdev)
367 resize_console(find_port_by_vtermno(0));
370 /* We set the configuration at this point, since we now have a tty */
371 static int notifier_add_vio(struct hvc_struct *hp, int data)
375 port = find_port_by_vtermno(hp->vtermno);
379 hp->irq_requested = 1;
380 resize_console(port);
385 static void notifier_del_vio(struct hvc_struct *hp, int data)
387 hp->irq_requested = 0;
390 static void hvc_handle_input(struct virtqueue *vq)
395 port = find_port_by_vq(vq->vdev->priv, vq);
399 spin_lock_irqsave(&port->inbuf_lock, flags);
400 port->inbuf = get_inbuf(port);
401 spin_unlock_irqrestore(&port->inbuf_lock, flags);
403 if (hvc_poll(port->cons.hvc))
407 /* The operations for the console. */
408 static const struct hv_ops hv_ops = {
409 .get_chars = get_chars,
410 .put_chars = put_chars,
411 .notifier_add = notifier_add_vio,
412 .notifier_del = notifier_del_vio,
413 .notifier_hangup = notifier_del_vio,
417 * Console drivers are initialized very early so boot messages can go
418 * out, so we do things slightly differently from the generic virtio
419 * initialization of the net and block drivers.
421 * At this stage, the console is output-only. It's too early to set
422 * up a virtqueue, so we let the drivers do some boutique early-output
425 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
427 early_put_chars = put_chars;
428 return hvc_instantiate(0, 0, &hv_ops);
431 int __devinit init_port_console(struct port *port)
436 * The Host's telling us this port is a console port. Hook it
437 * up with an hvc console.
439 * To set up and manage our virtual console, we call
442 * The first argument of hvc_alloc() is the virtual console
443 * number. The second argument is the parameter for the
444 * notification mechanism (like irq number). We currently
445 * leave this as zero, virtqueues have implicit notifications.
447 * The third argument is a "struct hv_ops" containing the
448 * put_chars() get_chars(), notifier_add() and notifier_del()
449 * pointers. The final argument is the output buffer size: we
450 * can do any size, so we put PAGE_SIZE here.
452 port->cons.vtermno = pdrvdata.next_vtermno;
454 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
455 if (IS_ERR(port->cons.hvc)) {
456 ret = PTR_ERR(port->cons.hvc);
457 port->cons.hvc = NULL;
460 spin_lock_irq(&pdrvdata_lock);
461 pdrvdata.next_vtermno++;
462 list_add_tail(&port->cons.list, &pdrvdata.consoles);
463 spin_unlock_irq(&pdrvdata_lock);
468 static int __devinit add_port(struct ports_device *portdev)
471 struct port_buffer *inbuf;
474 port = kmalloc(sizeof(*port), GFP_KERNEL);
480 port->portdev = portdev;
484 port->in_vq = portdev->in_vqs[0];
485 port->out_vq = portdev->out_vqs[0];
487 spin_lock_init(&port->inbuf_lock);
489 inbuf = alloc_buf(PAGE_SIZE);
495 /* Register the input buffer the first time. */
496 add_inbuf(port->in_vq, inbuf);
498 err = init_port_console(port);
512 static int init_vqs(struct ports_device *portdev)
514 vq_callback_t **io_callbacks;
516 struct virtqueue **vqs;
517 u32 nr_ports, nr_queues;
520 /* We currently only have one port and two queues for that port */
524 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
529 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
534 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
539 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
541 if (!portdev->in_vqs) {
545 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
547 if (!portdev->out_vqs) {
552 io_callbacks[0] = hvc_handle_input;
553 io_callbacks[1] = NULL;
554 io_names[0] = "input";
555 io_names[1] = "output";
557 /* Find the queues. */
558 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
560 (const char **)io_names);
564 portdev->in_vqs[0] = vqs[0];
565 portdev->out_vqs[0] = vqs[1];
578 kfree(portdev->out_vqs);
580 kfree(portdev->in_vqs);
588 * Once we're further in boot, we get probed like any other virtio
591 static int __devinit virtcons_probe(struct virtio_device *vdev)
593 struct ports_device *portdev;
596 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
602 /* Attach this portdev to this virtio_device, and vice-versa. */
603 portdev->vdev = vdev;
604 vdev->priv = portdev;
606 err = init_vqs(portdev);
608 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
612 /* We only have one port. */
613 err = add_port(portdev);
617 /* Start using the new console output. */
618 early_put_chars = NULL;
622 vdev->config->del_vqs(vdev);
623 kfree(portdev->in_vqs);
624 kfree(portdev->out_vqs);
631 static struct virtio_device_id id_table[] = {
632 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
636 static unsigned int features[] = {
637 VIRTIO_CONSOLE_F_SIZE,
640 static struct virtio_driver virtio_console = {
641 .feature_table = features,
642 .feature_table_size = ARRAY_SIZE(features),
643 .driver.name = KBUILD_MODNAME,
644 .driver.owner = THIS_MODULE,
645 .id_table = id_table,
646 .probe = virtcons_probe,
647 .config_changed = virtcons_apply_config,
650 static int __init init(void)
652 INIT_LIST_HEAD(&pdrvdata.consoles);
654 return register_virtio_driver(&virtio_console);
658 MODULE_DEVICE_TABLE(virtio, id_table);
659 MODULE_DESCRIPTION("Virtio console driver");
660 MODULE_LICENSE("GPL");