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 struct virtqueue *in_vq, *out_vq;
78 struct virtio_device *vdev;
84 /* size of the buffer in *buf above */
87 /* used length of the buffer */
89 /* offset in the buf from which to consume data */
93 /* This struct holds the per-port data */
95 /* Pointer to the parent virtio_console device */
96 struct ports_device *portdev;
98 /* The current buffer from which data has to be fed to readers */
99 struct port_buffer *inbuf;
101 /* The IO vqs for this port */
102 struct virtqueue *in_vq, *out_vq;
105 * The entries in this struct will be valid if this port is
106 * hooked up to an hvc console
111 /* This is the very early arch-specified put chars function. */
112 static int (*early_put_chars)(u32, const char *, int);
114 static struct port *find_port_by_vtermno(u32 vtermno)
117 struct console *cons;
120 spin_lock_irqsave(&pdrvdata_lock, flags);
121 list_for_each_entry(cons, &pdrvdata.consoles, list) {
122 if (cons->vtermno == vtermno) {
123 port = container_of(cons, struct port, cons);
129 spin_unlock_irqrestore(&pdrvdata_lock, flags);
133 static void free_buf(struct port_buffer *buf)
139 static struct port_buffer *alloc_buf(size_t buf_size)
141 struct port_buffer *buf;
143 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
146 buf->buf = kzalloc(buf_size, GFP_KERNEL);
151 buf->size = buf_size;
160 /* Callers should take appropriate locks */
161 static void *get_inbuf(struct port *port)
163 struct port_buffer *buf;
164 struct virtqueue *vq;
168 buf = vq->vq_ops->get_buf(vq, &len);
177 * Create a scatter-gather list representing our input buffer and put
180 * Callers should take appropriate locks.
182 static void add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
184 struct scatterlist sg[1];
186 sg_init_one(sg, buf->buf, buf->size);
188 if (vq->vq_ops->add_buf(vq, sg, 0, 1, buf) < 0)
190 vq->vq_ops->kick(vq);
194 * The put_chars() callback is pretty straightforward.
196 * We turn the characters into a scatter-gather list, add it to the
197 * output queue and then kick the Host. Then we sit here waiting for
198 * it to finish: inefficient in theory, but in practice
199 * implementations will do it immediately (lguest's Launcher does).
201 static int put_chars(u32 vtermno, const char *buf, int count)
203 struct scatterlist sg[1];
205 struct virtqueue *out_vq;
208 port = find_port_by_vtermno(vtermno);
212 if (unlikely(early_put_chars))
213 return early_put_chars(vtermno, buf, count);
215 out_vq = port->out_vq;
216 /* This is a convenient routine to initialize a single-elem sg list */
217 sg_init_one(sg, buf, count);
219 /* This shouldn't fail: if it does, we lose chars. */
220 if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) {
221 /* Tell Host to go! */
222 out_vq->vq_ops->kick(out_vq);
223 while (!out_vq->vq_ops->get_buf(out_vq, &len))
227 /* We're expected to return the amount of data we wrote: all of it. */
232 * get_chars() is the callback from the hvc_console infrastructure
233 * when an interrupt is received.
235 * Most of the code deals with the fact that the hvc_console()
236 * infrastructure only asks us for 16 bytes at a time. We keep
237 * in_offset and in_used fields for partially-filled buffers.
239 static int get_chars(u32 vtermno, char *buf, int count)
243 port = find_port_by_vtermno(vtermno);
247 /* If we don't have an input queue yet, we can't get input. */
248 BUG_ON(!port->in_vq);
250 /* No more in buffer? See if they've (re)used it. */
251 if (port->inbuf->offset == port->inbuf->len) {
252 if (!get_inbuf(port))
256 /* You want more than we have to give? Well, try wanting less! */
257 if (port->inbuf->offset + count > port->inbuf->len)
258 count = port->inbuf->len - port->inbuf->offset;
260 /* Copy across to their buffer and increment offset. */
261 memcpy(buf, port->inbuf->buf + port->inbuf->offset, count);
262 port->inbuf->offset += count;
264 /* Finished? Re-register buffer so Host will use it again. */
265 if (port->inbuf->offset == port->inbuf->len)
266 add_inbuf(port->in_vq, port->inbuf);
271 static void resize_console(struct port *port)
273 struct virtio_device *vdev;
276 vdev = port->portdev->vdev;
277 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
278 vdev->config->get(vdev,
279 offsetof(struct virtio_console_config, cols),
280 &ws.ws_col, sizeof(u16));
281 vdev->config->get(vdev,
282 offsetof(struct virtio_console_config, rows),
283 &ws.ws_row, sizeof(u16));
284 hvc_resize(port->cons.hvc, ws);
288 static void virtcons_apply_config(struct virtio_device *vdev)
290 resize_console(find_port_by_vtermno(0));
293 /* We set the configuration at this point, since we now have a tty */
294 static int notifier_add_vio(struct hvc_struct *hp, int data)
298 port = find_port_by_vtermno(hp->vtermno);
302 hp->irq_requested = 1;
303 resize_console(port);
308 static void notifier_del_vio(struct hvc_struct *hp, int data)
310 hp->irq_requested = 0;
313 static void hvc_handle_input(struct virtqueue *vq)
315 struct console *cons;
316 bool activity = false;
318 list_for_each_entry(cons, &pdrvdata.consoles, list)
319 activity |= hvc_poll(cons->hvc);
325 /* The operations for the console. */
326 static const struct hv_ops hv_ops = {
327 .get_chars = get_chars,
328 .put_chars = put_chars,
329 .notifier_add = notifier_add_vio,
330 .notifier_del = notifier_del_vio,
331 .notifier_hangup = notifier_del_vio,
335 * Console drivers are initialized very early so boot messages can go
336 * out, so we do things slightly differently from the generic virtio
337 * initialization of the net and block drivers.
339 * At this stage, the console is output-only. It's too early to set
340 * up a virtqueue, so we let the drivers do some boutique early-output
343 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
345 early_put_chars = put_chars;
346 return hvc_instantiate(0, 0, &hv_ops);
349 int __devinit init_port_console(struct port *port)
354 * The Host's telling us this port is a console port. Hook it
355 * up with an hvc console.
357 * To set up and manage our virtual console, we call
360 * The first argument of hvc_alloc() is the virtual console
361 * number. The second argument is the parameter for the
362 * notification mechanism (like irq number). We currently
363 * leave this as zero, virtqueues have implicit notifications.
365 * The third argument is a "struct hv_ops" containing the
366 * put_chars() get_chars(), notifier_add() and notifier_del()
367 * pointers. The final argument is the output buffer size: we
368 * can do any size, so we put PAGE_SIZE here.
370 port->cons.vtermno = pdrvdata.next_vtermno;
372 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
373 if (IS_ERR(port->cons.hvc)) {
374 ret = PTR_ERR(port->cons.hvc);
375 port->cons.hvc = NULL;
378 spin_lock_irq(&pdrvdata_lock);
379 pdrvdata.next_vtermno++;
380 list_add_tail(&port->cons.list, &pdrvdata.consoles);
381 spin_unlock_irq(&pdrvdata_lock);
386 static int __devinit add_port(struct ports_device *portdev)
391 port = kmalloc(sizeof(*port), GFP_KERNEL);
397 port->portdev = portdev;
398 port->in_vq = portdev->in_vq;
399 port->out_vq = portdev->out_vq;
401 port->inbuf = alloc_buf(PAGE_SIZE);
407 err = init_port_console(port);
411 /* Register the input buffer the first time. */
412 add_inbuf(port->in_vq, port->inbuf);
417 free_buf(port->inbuf);
425 * Once we're further in boot, we get probed like any other virtio
428 static int __devinit virtcons_probe(struct virtio_device *vdev)
430 vq_callback_t *callbacks[] = { hvc_handle_input, NULL};
431 const char *names[] = { "input", "output" };
432 struct virtqueue *vqs[2];
433 struct ports_device *portdev;
436 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
442 /* Attach this portdev to this virtio_device, and vice-versa. */
443 portdev->vdev = vdev;
444 vdev->priv = portdev;
446 /* Find the queues. */
447 err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
451 portdev->in_vq = vqs[0];
452 portdev->out_vq = vqs[1];
454 /* We only have one port. */
455 err = add_port(portdev);
459 /* Start using the new console output. */
460 early_put_chars = NULL;
464 vdev->config->del_vqs(vdev);
471 static struct virtio_device_id id_table[] = {
472 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
476 static unsigned int features[] = {
477 VIRTIO_CONSOLE_F_SIZE,
480 static struct virtio_driver virtio_console = {
481 .feature_table = features,
482 .feature_table_size = ARRAY_SIZE(features),
483 .driver.name = KBUILD_MODNAME,
484 .driver.owner = THIS_MODULE,
485 .id_table = id_table,
486 .probe = virtcons_probe,
487 .config_changed = virtcons_apply_config,
490 static int __init init(void)
492 INIT_LIST_HEAD(&pdrvdata.consoles);
494 return register_virtio_driver(&virtio_console);
498 MODULE_DEVICE_TABLE(virtio, id_table);
499 MODULE_DESCRIPTION("Virtio console driver");
500 MODULE_LICENSE("GPL");