blob: c6c6f52043b5b4e7f4145a841fdb8cc4b9ca006c [file] [log] [blame]
Rusty Russella23ea922010-01-18 19:14:55 +05301/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Rusty Russell31610432007-10-22 11:03:39 +10003 *
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.
8 *
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.
13 *
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
17 */
18#include <linux/err.h>
19#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053020#include <linux/list.h>
21#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100022#include <linux/virtio.h>
23#include <linux/virtio_console.h>
24#include "hvc_console.h"
25
Amit Shah38edf582010-01-18 19:15:05 +053026/*
27 * This is a global struct for storing common data for all the devices
28 * this driver handles.
29 *
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.
33 */
34struct ports_driver_data {
Rusty Russelld8a02bd2010-01-18 19:15:06 +053035 /*
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.
41 *
42 * We also just assume the first console being initialised was
43 * the first one that got used as the initial console.
44 */
45 unsigned int next_vtermno;
46
Amit Shah38edf582010-01-18 19:15:05 +053047 /* All the console devices handled by this driver */
48 struct list_head consoles;
49};
50static struct ports_driver_data pdrvdata;
51
52DEFINE_SPINLOCK(pdrvdata_lock);
53
Amit Shah4f23c572010-01-18 19:15:09 +053054/* This struct holds information that's relevant only for console ports */
55struct console {
56 /* We'll place all consoles in a list in the pdrvdata struct */
57 struct list_head list;
58
59 /* The hvc device associated with this console port */
60 struct hvc_struct *hvc;
61
62 /*
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
67 * this driver
68 */
69 u32 vtermno;
70};
71
Amit Shah1c85bf32010-01-18 19:15:07 +053072/*
73 * This is a per-device struct that stores data common to all the
74 * ports for that device (vdev->priv).
75 */
76struct ports_device {
77 struct virtqueue *in_vq, *out_vq;
78 struct virtio_device *vdev;
79};
80
Amit Shahfdb9a052010-01-18 19:15:01 +053081struct port_buffer {
82 char *buf;
83
84 /* size of the buffer in *buf above */
85 size_t size;
86
87 /* used length of the buffer */
88 size_t len;
89 /* offset in the buf from which to consume data */
90 size_t offset;
91};
92
Amit Shah1c85bf32010-01-18 19:15:07 +053093/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +053094struct port {
Amit Shah1c85bf32010-01-18 19:15:07 +053095 /* Pointer to the parent virtio_console device */
96 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +053097
98 /* The current buffer from which data has to be fed to readers */
99 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000100
Amit Shah1c85bf32010-01-18 19:15:07 +0530101 /* The IO vqs for this port */
102 struct virtqueue *in_vq, *out_vq;
103
Amit Shah4f23c572010-01-18 19:15:09 +0530104 /*
105 * The entries in this struct will be valid if this port is
106 * hooked up to an hvc console
107 */
108 struct console cons;
Rusty Russell21206ed2010-01-18 19:15:00 +0530109};
Rusty Russell31610432007-10-22 11:03:39 +1000110
Rusty Russell971f3392010-01-18 19:14:56 +0530111/* This is the very early arch-specified put chars function. */
112static int (*early_put_chars)(u32, const char *, int);
113
Amit Shah38edf582010-01-18 19:15:05 +0530114static struct port *find_port_by_vtermno(u32 vtermno)
115{
116 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530117 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530118 unsigned long flags;
119
120 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530121 list_for_each_entry(cons, &pdrvdata.consoles, list) {
122 if (cons->vtermno == vtermno) {
123 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530124 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530125 }
Amit Shah38edf582010-01-18 19:15:05 +0530126 }
127 port = NULL;
128out:
129 spin_unlock_irqrestore(&pdrvdata_lock, flags);
130 return port;
131}
132
Amit Shahfdb9a052010-01-18 19:15:01 +0530133static void free_buf(struct port_buffer *buf)
134{
135 kfree(buf->buf);
136 kfree(buf);
137}
138
139static struct port_buffer *alloc_buf(size_t buf_size)
140{
141 struct port_buffer *buf;
142
143 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
144 if (!buf)
145 goto fail;
146 buf->buf = kzalloc(buf_size, GFP_KERNEL);
147 if (!buf->buf)
148 goto free_buf;
149 buf->len = 0;
150 buf->offset = 0;
151 buf->size = buf_size;
152 return buf;
153
154free_buf:
155 kfree(buf);
156fail:
157 return NULL;
158}
159
Amit Shaha3cde442010-01-18 19:15:03 +0530160/* Callers should take appropriate locks */
161static void *get_inbuf(struct port *port)
162{
163 struct port_buffer *buf;
164 struct virtqueue *vq;
165 unsigned int len;
166
167 vq = port->in_vq;
168 buf = vq->vq_ops->get_buf(vq, &len);
169 if (buf) {
170 buf->len = len;
171 buf->offset = 0;
172 }
173 return buf;
174}
175
Rusty Russella23ea922010-01-18 19:14:55 +0530176/*
Amit Shahe27b5192010-01-18 19:15:02 +0530177 * Create a scatter-gather list representing our input buffer and put
178 * it in the queue.
179 *
180 * Callers should take appropriate locks.
181 */
182static void add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
183{
184 struct scatterlist sg[1];
Amit Shah1c85bf32010-01-18 19:15:07 +0530185
Amit Shahe27b5192010-01-18 19:15:02 +0530186 sg_init_one(sg, buf->buf, buf->size);
187
188 if (vq->vq_ops->add_buf(vq, sg, 0, 1, buf) < 0)
189 BUG();
190 vq->vq_ops->kick(vq);
191}
192
193/*
Rusty Russella23ea922010-01-18 19:14:55 +0530194 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000195 *
Rusty Russella23ea922010-01-18 19:14:55 +0530196 * 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).
200 */
Rusty Russell31610432007-10-22 11:03:39 +1000201static int put_chars(u32 vtermno, const char *buf, int count)
202{
203 struct scatterlist sg[1];
Rusty Russell21206ed2010-01-18 19:15:00 +0530204 struct port *port;
Amit Shah1c85bf32010-01-18 19:15:07 +0530205 struct virtqueue *out_vq;
Amit Shah38edf582010-01-18 19:15:05 +0530206 unsigned int len;
207
208 port = find_port_by_vtermno(vtermno);
209 if (!port)
210 return 0;
Rusty Russell31610432007-10-22 11:03:39 +1000211
Rusty Russell971f3392010-01-18 19:14:56 +0530212 if (unlikely(early_put_chars))
213 return early_put_chars(vtermno, buf, count);
214
Amit Shah1c85bf32010-01-18 19:15:07 +0530215 out_vq = port->out_vq;
Rusty Russell31610432007-10-22 11:03:39 +1000216 /* This is a convenient routine to initialize a single-elem sg list */
217 sg_init_one(sg, buf, count);
218
Rusty Russell21206ed2010-01-18 19:15:00 +0530219 /* This shouldn't fail: if it does, we lose chars. */
Amit Shah1c85bf32010-01-18 19:15:07 +0530220 if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) {
Rusty Russell31610432007-10-22 11:03:39 +1000221 /* Tell Host to go! */
Amit Shah1c85bf32010-01-18 19:15:07 +0530222 out_vq->vq_ops->kick(out_vq);
223 while (!out_vq->vq_ops->get_buf(out_vq, &len))
Rusty Russell31610432007-10-22 11:03:39 +1000224 cpu_relax();
225 }
226
227 /* We're expected to return the amount of data we wrote: all of it. */
228 return count;
229}
230
Rusty Russella23ea922010-01-18 19:14:55 +0530231/*
Rusty Russella23ea922010-01-18 19:14:55 +0530232 * get_chars() is the callback from the hvc_console infrastructure
233 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000234 *
Rusty Russella23ea922010-01-18 19:14:55 +0530235 * 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.
238 */
Rusty Russell31610432007-10-22 11:03:39 +1000239static int get_chars(u32 vtermno, char *buf, int count)
240{
Rusty Russell21206ed2010-01-18 19:15:00 +0530241 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000242
Amit Shah38edf582010-01-18 19:15:05 +0530243 port = find_port_by_vtermno(vtermno);
244 if (!port)
245 return 0;
Rusty Russell21206ed2010-01-18 19:15:00 +0530246
247 /* If we don't have an input queue yet, we can't get input. */
248 BUG_ON(!port->in_vq);
249
250 /* No more in buffer? See if they've (re)used it. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530251 if (port->inbuf->offset == port->inbuf->len) {
Amit Shaha3cde442010-01-18 19:15:03 +0530252 if (!get_inbuf(port))
Rusty Russell31610432007-10-22 11:03:39 +1000253 return 0;
254 }
255
256 /* You want more than we have to give? Well, try wanting less! */
Amit Shahfdb9a052010-01-18 19:15:01 +0530257 if (port->inbuf->offset + count > port->inbuf->len)
258 count = port->inbuf->len - port->inbuf->offset;
Rusty Russell31610432007-10-22 11:03:39 +1000259
260 /* Copy across to their buffer and increment offset. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530261 memcpy(buf, port->inbuf->buf + port->inbuf->offset, count);
262 port->inbuf->offset += count;
Rusty Russell31610432007-10-22 11:03:39 +1000263
264 /* Finished? Re-register buffer so Host will use it again. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530265 if (port->inbuf->offset == port->inbuf->len)
Amit Shahe27b5192010-01-18 19:15:02 +0530266 add_inbuf(port->in_vq, port->inbuf);
Rusty Russell31610432007-10-22 11:03:39 +1000267
268 return count;
269}
Rusty Russell31610432007-10-22 11:03:39 +1000270
Amit Shahcb06e362010-01-18 19:15:08 +0530271static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100272{
Amit Shahcb06e362010-01-18 19:15:08 +0530273 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100274 struct winsize ws;
275
Amit Shahcb06e362010-01-18 19:15:08 +0530276 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));
Amit Shah4f23c572010-01-18 19:15:09 +0530284 hvc_resize(port->cons.hvc, ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100285 }
286}
287
Amit Shahcb06e362010-01-18 19:15:08 +0530288static void virtcons_apply_config(struct virtio_device *vdev)
289{
290 resize_console(find_port_by_vtermno(0));
291}
292
Amit Shah38edf582010-01-18 19:15:05 +0530293/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200294static int notifier_add_vio(struct hvc_struct *hp, int data)
295{
Amit Shah38edf582010-01-18 19:15:05 +0530296 struct port *port;
297
298 port = find_port_by_vtermno(hp->vtermno);
299 if (!port)
300 return -EINVAL;
301
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200302 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530303 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100304
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200305 return 0;
306}
307
308static void notifier_del_vio(struct hvc_struct *hp, int data)
309{
310 hp->irq_requested = 0;
311}
312
313static void hvc_handle_input(struct virtqueue *vq)
314{
Amit Shah4f23c572010-01-18 19:15:09 +0530315 struct console *cons;
Amit Shah1c85bf32010-01-18 19:15:07 +0530316 bool activity = false;
Rusty Russell73954482010-01-18 19:15:04 +0530317
Amit Shah4f23c572010-01-18 19:15:09 +0530318 list_for_each_entry(cons, &pdrvdata.consoles, list)
319 activity |= hvc_poll(cons->hvc);
Amit Shah1c85bf32010-01-18 19:15:07 +0530320
321 if (activity)
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200322 hvc_kick();
323}
324
Rusty Russell971f3392010-01-18 19:14:56 +0530325/* The operations for the console. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530326static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530327 .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,
332};
333
334/*
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.
338 *
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
341 * thing.
342 */
343int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
344{
345 early_put_chars = put_chars;
346 return hvc_instantiate(0, 0, &hv_ops);
347}
348
Amit Shah1c85bf32010-01-18 19:15:07 +0530349static int __devinit add_port(struct ports_device *portdev)
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530350{
351 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000352 int err;
Rusty Russell31610432007-10-22 11:03:39 +1000353
Amit Shah1c85bf32010-01-18 19:15:07 +0530354 port = kmalloc(sizeof(*port), GFP_KERNEL);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530355 if (!port) {
356 err = -ENOMEM;
357 goto fail;
Amit Shahf5508042010-01-18 19:14:59 +0530358 }
Rusty Russell73954482010-01-18 19:15:04 +0530359
Amit Shah1c85bf32010-01-18 19:15:07 +0530360 port->portdev = portdev;
361 port->in_vq = portdev->in_vq;
362 port->out_vq = portdev->out_vq;
Rusty Russell31610432007-10-22 11:03:39 +1000363
Amit Shah1c85bf32010-01-18 19:15:07 +0530364 port->inbuf = alloc_buf(PAGE_SIZE);
365 if (!port->inbuf) {
366 err = -ENOMEM;
367 goto free_port;
368 }
Rusty Russell31610432007-10-22 11:03:39 +1000369
Rusty Russella23ea922010-01-18 19:14:55 +0530370 /*
371 * The first argument of hvc_alloc() is the virtual console
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530372 * number. The second argument is the parameter for the
373 * notification mechanism (like irq number). We currently
374 * leave this as zero, virtqueues have implicit notifications.
Rusty Russell31610432007-10-22 11:03:39 +1000375 *
Rusty Russella23ea922010-01-18 19:14:55 +0530376 * The third argument is a "struct hv_ops" containing the
377 * put_chars(), get_chars(), notifier_add() and notifier_del()
378 * pointers. The final argument is the output buffer size: we
379 * can do any size, so we put PAGE_SIZE here.
380 */
Amit Shah4f23c572010-01-18 19:15:09 +0530381 port->cons.vtermno = pdrvdata.next_vtermno;
382 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
383 if (IS_ERR(port->cons.hvc)) {
384 err = PTR_ERR(port->cons.hvc);
Amit Shah1c85bf32010-01-18 19:15:07 +0530385 goto free_inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000386 }
387
Amit Shah38edf582010-01-18 19:15:05 +0530388 /* Add to vtermno list. */
389 spin_lock_irq(&pdrvdata_lock);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530390 pdrvdata.next_vtermno++;
Amit Shah4f23c572010-01-18 19:15:09 +0530391 list_add(&port->cons.list, &pdrvdata.consoles);
Amit Shah38edf582010-01-18 19:15:05 +0530392 spin_unlock_irq(&pdrvdata_lock);
393
Rusty Russell31610432007-10-22 11:03:39 +1000394 /* Register the input buffer the first time. */
Amit Shahe27b5192010-01-18 19:15:02 +0530395 add_inbuf(port->in_vq, port->inbuf);
Rusty Russell971f3392010-01-18 19:14:56 +0530396
Amit Shah1c85bf32010-01-18 19:15:07 +0530397 return 0;
398
399free_inbuf:
400 free_buf(port->inbuf);
401free_port:
402 kfree(port);
403fail:
404 return err;
405}
406
407/*
408 * Once we're further in boot, we get probed like any other virtio
409 * device.
410 */
411static int __devinit virtcons_probe(struct virtio_device *vdev)
412{
413 vq_callback_t *callbacks[] = { hvc_handle_input, NULL};
414 const char *names[] = { "input", "output" };
415 struct virtqueue *vqs[2];
416 struct ports_device *portdev;
417 int err;
418
419 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
420 if (!portdev) {
421 err = -ENOMEM;
422 goto fail;
423 }
424
425 /* Attach this portdev to this virtio_device, and vice-versa. */
426 portdev->vdev = vdev;
427 vdev->priv = portdev;
428
429 /* Find the queues. */
430 err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
431 if (err)
432 goto free;
433
434 portdev->in_vq = vqs[0];
435 portdev->out_vq = vqs[1];
436
437 /* We only have one port. */
438 err = add_port(portdev);
439 if (err)
440 goto free_vqs;
441
Rusty Russell971f3392010-01-18 19:14:56 +0530442 /* Start using the new console output. */
443 early_put_chars = NULL;
Rusty Russell31610432007-10-22 11:03:39 +1000444 return 0;
445
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600446free_vqs:
447 vdev->config->del_vqs(vdev);
Rusty Russell31610432007-10-22 11:03:39 +1000448free:
Amit Shah1c85bf32010-01-18 19:15:07 +0530449 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +1000450fail:
451 return err;
452}
453
454static struct virtio_device_id id_table[] = {
455 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
456 { 0 },
457};
458
Christian Borntraegerc2983452008-11-25 13:36:26 +0100459static unsigned int features[] = {
460 VIRTIO_CONSOLE_F_SIZE,
461};
462
Rusty Russell31610432007-10-22 11:03:39 +1000463static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +0100464 .feature_table = features,
465 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +1000466 .driver.name = KBUILD_MODNAME,
467 .driver.owner = THIS_MODULE,
468 .id_table = id_table,
469 .probe = virtcons_probe,
Christian Borntraegerc2983452008-11-25 13:36:26 +0100470 .config_changed = virtcons_apply_config,
Rusty Russell31610432007-10-22 11:03:39 +1000471};
472
473static int __init init(void)
474{
Amit Shah38edf582010-01-18 19:15:05 +0530475 INIT_LIST_HEAD(&pdrvdata.consoles);
476
Rusty Russell31610432007-10-22 11:03:39 +1000477 return register_virtio_driver(&virtio_console);
478}
479module_init(init);
480
481MODULE_DEVICE_TABLE(virtio, id_table);
482MODULE_DESCRIPTION("Virtio console driver");
483MODULE_LICENSE("GPL");