blob: debc86542858202e4d60037f5b4fc7fbffe7588b [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 Shah1c85bf32010-01-18 19:15:07 +053054/*
55 * This is a per-device struct that stores data common to all the
56 * ports for that device (vdev->priv).
57 */
58struct ports_device {
59 struct virtqueue *in_vq, *out_vq;
60 struct virtio_device *vdev;
61};
62
Amit Shahfdb9a052010-01-18 19:15:01 +053063struct port_buffer {
64 char *buf;
65
66 /* size of the buffer in *buf above */
67 size_t size;
68
69 /* used length of the buffer */
70 size_t len;
71 /* offset in the buf from which to consume data */
72 size_t offset;
73};
74
Amit Shah1c85bf32010-01-18 19:15:07 +053075/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +053076struct port {
Amit Shah1c85bf32010-01-18 19:15:07 +053077 /* Pointer to the parent virtio_console device */
78 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +053079
80 /* The current buffer from which data has to be fed to readers */
81 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +100082
Amit Shah1c85bf32010-01-18 19:15:07 +053083 /* The IO vqs for this port */
84 struct virtqueue *in_vq, *out_vq;
85
Amit Shah38edf582010-01-18 19:15:05 +053086 /* For console ports, hvc != NULL and these are valid. */
Rusty Russell21206ed2010-01-18 19:15:00 +053087 /* The hvc device */
88 struct hvc_struct *hvc;
Amit Shah38edf582010-01-18 19:15:05 +053089
90 /* We'll place all consoles in a list in the pdrvdata struct */
91 struct list_head list;
92
93 /* Our vterm number. */
94 u32 vtermno;
Rusty Russell21206ed2010-01-18 19:15:00 +053095};
Rusty Russell31610432007-10-22 11:03:39 +100096
Rusty Russell971f3392010-01-18 19:14:56 +053097/* This is the very early arch-specified put chars function. */
98static int (*early_put_chars)(u32, const char *, int);
99
Amit Shah38edf582010-01-18 19:15:05 +0530100static struct port *find_port_by_vtermno(u32 vtermno)
101{
102 struct port *port;
103 unsigned long flags;
104
105 spin_lock_irqsave(&pdrvdata_lock, flags);
106 list_for_each_entry(port, &pdrvdata.consoles, list) {
107 if (port->vtermno == vtermno)
108 goto out;
109 }
110 port = NULL;
111out:
112 spin_unlock_irqrestore(&pdrvdata_lock, flags);
113 return port;
114}
115
Amit Shahfdb9a052010-01-18 19:15:01 +0530116static void free_buf(struct port_buffer *buf)
117{
118 kfree(buf->buf);
119 kfree(buf);
120}
121
122static struct port_buffer *alloc_buf(size_t buf_size)
123{
124 struct port_buffer *buf;
125
126 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
127 if (!buf)
128 goto fail;
129 buf->buf = kzalloc(buf_size, GFP_KERNEL);
130 if (!buf->buf)
131 goto free_buf;
132 buf->len = 0;
133 buf->offset = 0;
134 buf->size = buf_size;
135 return buf;
136
137free_buf:
138 kfree(buf);
139fail:
140 return NULL;
141}
142
Amit Shaha3cde442010-01-18 19:15:03 +0530143/* Callers should take appropriate locks */
144static void *get_inbuf(struct port *port)
145{
146 struct port_buffer *buf;
147 struct virtqueue *vq;
148 unsigned int len;
149
150 vq = port->in_vq;
151 buf = vq->vq_ops->get_buf(vq, &len);
152 if (buf) {
153 buf->len = len;
154 buf->offset = 0;
155 }
156 return buf;
157}
158
Rusty Russella23ea922010-01-18 19:14:55 +0530159/*
Amit Shahe27b5192010-01-18 19:15:02 +0530160 * Create a scatter-gather list representing our input buffer and put
161 * it in the queue.
162 *
163 * Callers should take appropriate locks.
164 */
165static void add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
166{
167 struct scatterlist sg[1];
Amit Shah1c85bf32010-01-18 19:15:07 +0530168
Amit Shahe27b5192010-01-18 19:15:02 +0530169 sg_init_one(sg, buf->buf, buf->size);
170
171 if (vq->vq_ops->add_buf(vq, sg, 0, 1, buf) < 0)
172 BUG();
173 vq->vq_ops->kick(vq);
174}
175
176/*
Rusty Russella23ea922010-01-18 19:14:55 +0530177 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000178 *
Rusty Russella23ea922010-01-18 19:14:55 +0530179 * We turn the characters into a scatter-gather list, add it to the
180 * output queue and then kick the Host. Then we sit here waiting for
181 * it to finish: inefficient in theory, but in practice
182 * implementations will do it immediately (lguest's Launcher does).
183 */
Rusty Russell31610432007-10-22 11:03:39 +1000184static int put_chars(u32 vtermno, const char *buf, int count)
185{
186 struct scatterlist sg[1];
Rusty Russell21206ed2010-01-18 19:15:00 +0530187 struct port *port;
Amit Shah1c85bf32010-01-18 19:15:07 +0530188 struct virtqueue *out_vq;
Amit Shah38edf582010-01-18 19:15:05 +0530189 unsigned int len;
190
191 port = find_port_by_vtermno(vtermno);
192 if (!port)
193 return 0;
Rusty Russell31610432007-10-22 11:03:39 +1000194
Rusty Russell971f3392010-01-18 19:14:56 +0530195 if (unlikely(early_put_chars))
196 return early_put_chars(vtermno, buf, count);
197
Amit Shah1c85bf32010-01-18 19:15:07 +0530198 out_vq = port->out_vq;
Rusty Russell31610432007-10-22 11:03:39 +1000199 /* This is a convenient routine to initialize a single-elem sg list */
200 sg_init_one(sg, buf, count);
201
Rusty Russell21206ed2010-01-18 19:15:00 +0530202 /* This shouldn't fail: if it does, we lose chars. */
Amit Shah1c85bf32010-01-18 19:15:07 +0530203 if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) {
Rusty Russell31610432007-10-22 11:03:39 +1000204 /* Tell Host to go! */
Amit Shah1c85bf32010-01-18 19:15:07 +0530205 out_vq->vq_ops->kick(out_vq);
206 while (!out_vq->vq_ops->get_buf(out_vq, &len))
Rusty Russell31610432007-10-22 11:03:39 +1000207 cpu_relax();
208 }
209
210 /* We're expected to return the amount of data we wrote: all of it. */
211 return count;
212}
213
Rusty Russella23ea922010-01-18 19:14:55 +0530214/*
Rusty Russella23ea922010-01-18 19:14:55 +0530215 * get_chars() is the callback from the hvc_console infrastructure
216 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000217 *
Rusty Russella23ea922010-01-18 19:14:55 +0530218 * Most of the code deals with the fact that the hvc_console()
219 * infrastructure only asks us for 16 bytes at a time. We keep
220 * in_offset and in_used fields for partially-filled buffers.
221 */
Rusty Russell31610432007-10-22 11:03:39 +1000222static int get_chars(u32 vtermno, char *buf, int count)
223{
Rusty Russell21206ed2010-01-18 19:15:00 +0530224 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000225
Amit Shah38edf582010-01-18 19:15:05 +0530226 port = find_port_by_vtermno(vtermno);
227 if (!port)
228 return 0;
Rusty Russell21206ed2010-01-18 19:15:00 +0530229
230 /* If we don't have an input queue yet, we can't get input. */
231 BUG_ON(!port->in_vq);
232
233 /* No more in buffer? See if they've (re)used it. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530234 if (port->inbuf->offset == port->inbuf->len) {
Amit Shaha3cde442010-01-18 19:15:03 +0530235 if (!get_inbuf(port))
Rusty Russell31610432007-10-22 11:03:39 +1000236 return 0;
237 }
238
239 /* You want more than we have to give? Well, try wanting less! */
Amit Shahfdb9a052010-01-18 19:15:01 +0530240 if (port->inbuf->offset + count > port->inbuf->len)
241 count = port->inbuf->len - port->inbuf->offset;
Rusty Russell31610432007-10-22 11:03:39 +1000242
243 /* Copy across to their buffer and increment offset. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530244 memcpy(buf, port->inbuf->buf + port->inbuf->offset, count);
245 port->inbuf->offset += count;
Rusty Russell31610432007-10-22 11:03:39 +1000246
247 /* Finished? Re-register buffer so Host will use it again. */
Amit Shahfdb9a052010-01-18 19:15:01 +0530248 if (port->inbuf->offset == port->inbuf->len)
Amit Shahe27b5192010-01-18 19:15:02 +0530249 add_inbuf(port->in_vq, port->inbuf);
Rusty Russell31610432007-10-22 11:03:39 +1000250
251 return count;
252}
Rusty Russell31610432007-10-22 11:03:39 +1000253
Amit Shahcb06e362010-01-18 19:15:08 +0530254static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100255{
Amit Shahcb06e362010-01-18 19:15:08 +0530256 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100257 struct winsize ws;
258
Amit Shahcb06e362010-01-18 19:15:08 +0530259 vdev = port->portdev->vdev;
260 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
261 vdev->config->get(vdev,
262 offsetof(struct virtio_console_config, cols),
263 &ws.ws_col, sizeof(u16));
264 vdev->config->get(vdev,
265 offsetof(struct virtio_console_config, rows),
266 &ws.ws_row, sizeof(u16));
267 hvc_resize(port->hvc, ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100268 }
269}
270
Amit Shahcb06e362010-01-18 19:15:08 +0530271static void virtcons_apply_config(struct virtio_device *vdev)
272{
273 resize_console(find_port_by_vtermno(0));
274}
275
Amit Shah38edf582010-01-18 19:15:05 +0530276/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200277static int notifier_add_vio(struct hvc_struct *hp, int data)
278{
Amit Shah38edf582010-01-18 19:15:05 +0530279 struct port *port;
280
281 port = find_port_by_vtermno(hp->vtermno);
282 if (!port)
283 return -EINVAL;
284
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200285 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530286 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100287
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200288 return 0;
289}
290
291static void notifier_del_vio(struct hvc_struct *hp, int data)
292{
293 hp->irq_requested = 0;
294}
295
296static void hvc_handle_input(struct virtqueue *vq)
297{
Amit Shah1c85bf32010-01-18 19:15:07 +0530298 struct port *port;
299 bool activity = false;
Rusty Russell73954482010-01-18 19:15:04 +0530300
Amit Shah1c85bf32010-01-18 19:15:07 +0530301 list_for_each_entry(port, &pdrvdata.consoles, list)
302 activity |= hvc_poll(port->hvc);
303
304 if (activity)
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200305 hvc_kick();
306}
307
Rusty Russell971f3392010-01-18 19:14:56 +0530308/* The operations for the console. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530309static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530310 .get_chars = get_chars,
311 .put_chars = put_chars,
312 .notifier_add = notifier_add_vio,
313 .notifier_del = notifier_del_vio,
314 .notifier_hangup = notifier_del_vio,
315};
316
317/*
318 * Console drivers are initialized very early so boot messages can go
319 * out, so we do things slightly differently from the generic virtio
320 * initialization of the net and block drivers.
321 *
322 * At this stage, the console is output-only. It's too early to set
323 * up a virtqueue, so we let the drivers do some boutique early-output
324 * thing.
325 */
326int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
327{
328 early_put_chars = put_chars;
329 return hvc_instantiate(0, 0, &hv_ops);
330}
331
Amit Shah1c85bf32010-01-18 19:15:07 +0530332static int __devinit add_port(struct ports_device *portdev)
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530333{
334 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000335 int err;
Rusty Russell31610432007-10-22 11:03:39 +1000336
Amit Shah1c85bf32010-01-18 19:15:07 +0530337 port = kmalloc(sizeof(*port), GFP_KERNEL);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530338 if (!port) {
339 err = -ENOMEM;
340 goto fail;
Amit Shahf5508042010-01-18 19:14:59 +0530341 }
Rusty Russell73954482010-01-18 19:15:04 +0530342
Amit Shah1c85bf32010-01-18 19:15:07 +0530343 port->portdev = portdev;
344 port->in_vq = portdev->in_vq;
345 port->out_vq = portdev->out_vq;
Rusty Russell31610432007-10-22 11:03:39 +1000346
Amit Shah1c85bf32010-01-18 19:15:07 +0530347 port->inbuf = alloc_buf(PAGE_SIZE);
348 if (!port->inbuf) {
349 err = -ENOMEM;
350 goto free_port;
351 }
Rusty Russell31610432007-10-22 11:03:39 +1000352
Rusty Russella23ea922010-01-18 19:14:55 +0530353 /*
354 * The first argument of hvc_alloc() is the virtual console
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530355 * number. The second argument is the parameter for the
356 * notification mechanism (like irq number). We currently
357 * leave this as zero, virtqueues have implicit notifications.
Rusty Russell31610432007-10-22 11:03:39 +1000358 *
Rusty Russella23ea922010-01-18 19:14:55 +0530359 * The third argument is a "struct hv_ops" containing the
360 * put_chars(), get_chars(), notifier_add() and notifier_del()
361 * pointers. The final argument is the output buffer size: we
362 * can do any size, so we put PAGE_SIZE here.
363 */
Amit Shah1c85bf32010-01-18 19:15:07 +0530364 port->vtermno = pdrvdata.next_vtermno;
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530365 port->hvc = hvc_alloc(port->vtermno, 0, &hv_ops, PAGE_SIZE);
Rusty Russell21206ed2010-01-18 19:15:00 +0530366 if (IS_ERR(port->hvc)) {
367 err = PTR_ERR(port->hvc);
Amit Shah1c85bf32010-01-18 19:15:07 +0530368 goto free_inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000369 }
370
Amit Shah38edf582010-01-18 19:15:05 +0530371 /* Add to vtermno list. */
372 spin_lock_irq(&pdrvdata_lock);
Rusty Russelld8a02bd2010-01-18 19:15:06 +0530373 pdrvdata.next_vtermno++;
Amit Shah38edf582010-01-18 19:15:05 +0530374 list_add(&port->list, &pdrvdata.consoles);
375 spin_unlock_irq(&pdrvdata_lock);
376
Rusty Russell31610432007-10-22 11:03:39 +1000377 /* Register the input buffer the first time. */
Amit Shahe27b5192010-01-18 19:15:02 +0530378 add_inbuf(port->in_vq, port->inbuf);
Rusty Russell971f3392010-01-18 19:14:56 +0530379
Amit Shah1c85bf32010-01-18 19:15:07 +0530380 return 0;
381
382free_inbuf:
383 free_buf(port->inbuf);
384free_port:
385 kfree(port);
386fail:
387 return err;
388}
389
390/*
391 * Once we're further in boot, we get probed like any other virtio
392 * device.
393 */
394static int __devinit virtcons_probe(struct virtio_device *vdev)
395{
396 vq_callback_t *callbacks[] = { hvc_handle_input, NULL};
397 const char *names[] = { "input", "output" };
398 struct virtqueue *vqs[2];
399 struct ports_device *portdev;
400 int err;
401
402 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
403 if (!portdev) {
404 err = -ENOMEM;
405 goto fail;
406 }
407
408 /* Attach this portdev to this virtio_device, and vice-versa. */
409 portdev->vdev = vdev;
410 vdev->priv = portdev;
411
412 /* Find the queues. */
413 err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
414 if (err)
415 goto free;
416
417 portdev->in_vq = vqs[0];
418 portdev->out_vq = vqs[1];
419
420 /* We only have one port. */
421 err = add_port(portdev);
422 if (err)
423 goto free_vqs;
424
Rusty Russell971f3392010-01-18 19:14:56 +0530425 /* Start using the new console output. */
426 early_put_chars = NULL;
Rusty Russell31610432007-10-22 11:03:39 +1000427 return 0;
428
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600429free_vqs:
430 vdev->config->del_vqs(vdev);
Rusty Russell31610432007-10-22 11:03:39 +1000431free:
Amit Shah1c85bf32010-01-18 19:15:07 +0530432 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +1000433fail:
434 return err;
435}
436
437static struct virtio_device_id id_table[] = {
438 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
439 { 0 },
440};
441
Christian Borntraegerc2983452008-11-25 13:36:26 +0100442static unsigned int features[] = {
443 VIRTIO_CONSOLE_F_SIZE,
444};
445
Rusty Russell31610432007-10-22 11:03:39 +1000446static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +0100447 .feature_table = features,
448 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +1000449 .driver.name = KBUILD_MODNAME,
450 .driver.owner = THIS_MODULE,
451 .id_table = id_table,
452 .probe = virtcons_probe,
Christian Borntraegerc2983452008-11-25 13:36:26 +0100453 .config_changed = virtcons_apply_config,
Rusty Russell31610432007-10-22 11:03:39 +1000454};
455
456static int __init init(void)
457{
Amit Shah38edf582010-01-18 19:15:05 +0530458 INIT_LIST_HEAD(&pdrvdata.consoles);
459
Rusty Russell31610432007-10-22 11:03:39 +1000460 return register_virtio_driver(&virtio_console);
461}
462module_init(init);
463
464MODULE_DEVICE_TABLE(virtio, id_table);
465MODULE_DESCRIPTION("Virtio console driver");
466MODULE_LICENSE("GPL");