Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 1 | /*D:300 |
| 2 | * The Guest console driver |
| 3 | * |
| 4 | * Writing console drivers is one of the few remaining Dark Arts in Linux. |
| 5 | * Fortunately for us, the path of virtual consoles has been well-trodden by |
| 6 | * the PowerPC folks, who wrote "hvc_console.c" to generically support any |
| 7 | * virtual console. We use that infrastructure which only requires us to write |
| 8 | * the basic put_chars and get_chars functions and call the right register |
| 9 | * functions. |
| 10 | :*/ |
| 11 | |
| 12 | /*M:002 The console can be flooded: while the Guest is processing input the |
| 13 | * Host can send more. Buffering in the Host could alleviate this, but it is a |
| 14 | * difficult problem in general. :*/ |
| 15 | /* Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License as published by |
| 19 | * the Free Software Foundation; either version 2 of the License, or |
| 20 | * (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 30 | */ |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/virtio.h> |
Fernando Luis Vazquez Cao | 3ca4f5c | 2009-07-31 15:25:56 +0900 | [diff] [blame] | 34 | #include <linux/virtio_ids.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 35 | #include <linux/virtio_console.h> |
| 36 | #include "hvc_console.h" |
| 37 | |
| 38 | /*D:340 These represent our input and output console queues, and the virtio |
| 39 | * operations for them. */ |
| 40 | static struct virtqueue *in_vq, *out_vq; |
| 41 | static struct virtio_device *vdev; |
| 42 | |
| 43 | /* This is our input buffer, and how much data is left in it. */ |
| 44 | static unsigned int in_len; |
| 45 | static char *in, *inbuf; |
| 46 | |
| 47 | /* The operations for our console. */ |
| 48 | static struct hv_ops virtio_cons; |
| 49 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 50 | /* The hvc device */ |
| 51 | static struct hvc_struct *hvc; |
| 52 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 53 | /*D:310 The put_chars() callback is pretty straightforward. |
| 54 | * |
| 55 | * We turn the characters into a scatter-gather list, add it to the output |
| 56 | * queue and then kick the Host. Then we sit here waiting for it to finish: |
| 57 | * inefficient in theory, but in practice implementations will do it |
| 58 | * immediately (lguest's Launcher does). */ |
| 59 | static int put_chars(u32 vtermno, const char *buf, int count) |
| 60 | { |
| 61 | struct scatterlist sg[1]; |
| 62 | unsigned int len; |
| 63 | |
| 64 | /* This is a convenient routine to initialize a single-elem sg list */ |
| 65 | sg_init_one(sg, buf, count); |
| 66 | |
| 67 | /* add_buf wants a token to identify this buffer: we hand it any |
| 68 | * non-NULL pointer, since there's only ever one buffer. */ |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 69 | if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) >= 0) { |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 70 | /* Tell Host to go! */ |
| 71 | out_vq->vq_ops->kick(out_vq); |
| 72 | /* Chill out until it's done with the buffer. */ |
| 73 | while (!out_vq->vq_ops->get_buf(out_vq, &len)) |
| 74 | cpu_relax(); |
| 75 | } |
| 76 | |
| 77 | /* We're expected to return the amount of data we wrote: all of it. */ |
| 78 | return count; |
| 79 | } |
| 80 | |
| 81 | /* Create a scatter-gather list representing our input buffer and put it in the |
| 82 | * queue. */ |
| 83 | static void add_inbuf(void) |
| 84 | { |
| 85 | struct scatterlist sg[1]; |
| 86 | sg_init_one(sg, inbuf, PAGE_SIZE); |
| 87 | |
| 88 | /* We should always be able to add one buffer to an empty queue. */ |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 89 | if (in_vq->vq_ops->add_buf(in_vq, sg, 0, 1, inbuf) < 0) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 90 | BUG(); |
| 91 | in_vq->vq_ops->kick(in_vq); |
| 92 | } |
| 93 | |
| 94 | /*D:350 get_chars() is the callback from the hvc_console infrastructure when |
| 95 | * an interrupt is received. |
| 96 | * |
| 97 | * Most of the code deals with the fact that the hvc_console() infrastructure |
| 98 | * only asks us for 16 bytes at a time. We keep in_offset and in_used fields |
| 99 | * for partially-filled buffers. */ |
| 100 | static int get_chars(u32 vtermno, char *buf, int count) |
| 101 | { |
| 102 | /* If we don't have an input queue yet, we can't get input. */ |
| 103 | BUG_ON(!in_vq); |
| 104 | |
| 105 | /* No buffer? Try to get one. */ |
| 106 | if (!in_len) { |
| 107 | in = in_vq->vq_ops->get_buf(in_vq, &in_len); |
| 108 | if (!in) |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* You want more than we have to give? Well, try wanting less! */ |
| 113 | if (in_len < count) |
| 114 | count = in_len; |
| 115 | |
| 116 | /* Copy across to their buffer and increment offset. */ |
| 117 | memcpy(buf, in, count); |
| 118 | in += count; |
| 119 | in_len -= count; |
| 120 | |
| 121 | /* Finished? Re-register buffer so Host will use it again. */ |
| 122 | if (in_len == 0) |
| 123 | add_inbuf(); |
| 124 | |
| 125 | return count; |
| 126 | } |
| 127 | /*:*/ |
| 128 | |
| 129 | /*D:320 Console drivers are initialized very early so boot messages can go out, |
| 130 | * so we do things slightly differently from the generic virtio initialization |
| 131 | * of the net and block drivers. |
| 132 | * |
| 133 | * At this stage, the console is output-only. It's too early to set up a |
| 134 | * virtqueue, so we let the drivers do some boutique early-output thing. */ |
| 135 | int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int)) |
| 136 | { |
| 137 | virtio_cons.put_chars = put_chars; |
| 138 | return hvc_instantiate(0, 0, &virtio_cons); |
| 139 | } |
| 140 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 141 | /* |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 142 | * virtio console configuration. This supports: |
| 143 | * - console resize |
| 144 | */ |
| 145 | static void virtcons_apply_config(struct virtio_device *dev) |
| 146 | { |
| 147 | struct winsize ws; |
| 148 | |
| 149 | if (virtio_has_feature(dev, VIRTIO_CONSOLE_F_SIZE)) { |
| 150 | dev->config->get(dev, |
| 151 | offsetof(struct virtio_console_config, cols), |
| 152 | &ws.ws_col, sizeof(u16)); |
| 153 | dev->config->get(dev, |
| 154 | offsetof(struct virtio_console_config, rows), |
| 155 | &ws.ws_row, sizeof(u16)); |
| 156 | hvc_resize(hvc, ws); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 161 | * we support only one console, the hvc struct is a global var |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 162 | * We set the configuration at this point, since we now have a tty |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 163 | */ |
| 164 | static int notifier_add_vio(struct hvc_struct *hp, int data) |
| 165 | { |
| 166 | hp->irq_requested = 1; |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 167 | virtcons_apply_config(vdev); |
| 168 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void notifier_del_vio(struct hvc_struct *hp, int data) |
| 173 | { |
| 174 | hp->irq_requested = 0; |
| 175 | } |
| 176 | |
| 177 | static void hvc_handle_input(struct virtqueue *vq) |
| 178 | { |
| 179 | if (hvc_poll(hvc)) |
| 180 | hvc_kick(); |
| 181 | } |
| 182 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 183 | /*D:370 Once we're further in boot, we get probed like any other virtio device. |
| 184 | * At this stage we set up the output virtqueue. |
| 185 | * |
| 186 | * To set up and manage our virtual console, we call hvc_alloc(). Since we |
| 187 | * never remove the console device we never need this pointer again. |
| 188 | * |
| 189 | * Finally we put our input buffer in the input queue, ready to receive. */ |
Randy Dunlap | 139b829 | 2007-11-05 14:51:01 -0800 | [diff] [blame] | 190 | static int __devinit virtcons_probe(struct virtio_device *dev) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 191 | { |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 192 | vq_callback_t *callbacks[] = { hvc_handle_input, NULL}; |
| 193 | const char *names[] = { "input", "output" }; |
| 194 | struct virtqueue *vqs[2]; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 195 | int err; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 196 | |
| 197 | vdev = dev; |
| 198 | |
| 199 | /* This is the scratch page we use to receive console input */ |
| 200 | inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 201 | if (!inbuf) { |
| 202 | err = -ENOMEM; |
| 203 | goto fail; |
| 204 | } |
| 205 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 206 | /* Find the queues. */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 207 | /* FIXME: This is why we want to wean off hvc: we do nothing |
| 208 | * when input comes in. */ |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 209 | err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names); |
| 210 | if (err) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 211 | goto free; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 212 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 213 | in_vq = vqs[0]; |
| 214 | out_vq = vqs[1]; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 215 | |
| 216 | /* Start using the new console output. */ |
| 217 | virtio_cons.get_chars = get_chars; |
| 218 | virtio_cons.put_chars = put_chars; |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 219 | virtio_cons.notifier_add = notifier_add_vio; |
| 220 | virtio_cons.notifier_del = notifier_del_vio; |
Hendrik Brueckner | fc362e2 | 2008-10-13 23:12:48 +0000 | [diff] [blame] | 221 | virtio_cons.notifier_hangup = notifier_del_vio; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 222 | |
| 223 | /* The first argument of hvc_alloc() is the virtual console number, so |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 224 | * we use zero. The second argument is the parameter for the |
| 225 | * notification mechanism (like irq number). We currently leave this |
| 226 | * as zero, virtqueues have implicit notifications. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 227 | * |
| 228 | * The third argument is a "struct hv_ops" containing the put_chars() |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 229 | * get_chars(), notifier_add() and notifier_del() pointers. |
| 230 | * The final argument is the output buffer size: we can do any size, |
| 231 | * so we put PAGE_SIZE here. */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 232 | hvc = hvc_alloc(0, 0, &virtio_cons, PAGE_SIZE); |
| 233 | if (IS_ERR(hvc)) { |
| 234 | err = PTR_ERR(hvc); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 235 | goto free_vqs; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* Register the input buffer the first time. */ |
| 239 | add_inbuf(); |
| 240 | return 0; |
| 241 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 242 | free_vqs: |
| 243 | vdev->config->del_vqs(vdev); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 244 | free: |
| 245 | kfree(inbuf); |
| 246 | fail: |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | static struct virtio_device_id id_table[] = { |
| 251 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, |
| 252 | { 0 }, |
| 253 | }; |
| 254 | |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 255 | static unsigned int features[] = { |
| 256 | VIRTIO_CONSOLE_F_SIZE, |
| 257 | }; |
| 258 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 259 | static struct virtio_driver virtio_console = { |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 260 | .feature_table = features, |
| 261 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 262 | .driver.name = KBUILD_MODNAME, |
| 263 | .driver.owner = THIS_MODULE, |
| 264 | .id_table = id_table, |
| 265 | .probe = virtcons_probe, |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 266 | .config_changed = virtcons_apply_config, |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | static int __init init(void) |
| 270 | { |
| 271 | return register_virtio_driver(&virtio_console); |
| 272 | } |
| 273 | module_init(init); |
| 274 | |
| 275 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 276 | MODULE_DESCRIPTION("Virtio console driver"); |
| 277 | MODULE_LICENSE("GPL"); |