Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 3 | * |
| 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 Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 20 | #include <linux/list.h> |
| 21 | #include <linux/spinlock.h> |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 22 | #include <linux/virtio.h> |
| 23 | #include <linux/virtio_console.h> |
| 24 | #include "hvc_console.h" |
| 25 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 26 | /* |
| 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 | */ |
| 34 | struct ports_driver_data { |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 35 | /* |
| 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 Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 47 | /* All the console devices handled by this driver */ |
| 48 | struct list_head consoles; |
| 49 | }; |
| 50 | static struct ports_driver_data pdrvdata; |
| 51 | |
| 52 | DEFINE_SPINLOCK(pdrvdata_lock); |
| 53 | |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 54 | /* This struct holds information that's relevant only for console ports */ |
| 55 | struct 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 Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 72 | /* |
| 73 | * This is a per-device struct that stores data common to all the |
| 74 | * ports for that device (vdev->priv). |
| 75 | */ |
| 76 | struct ports_device { |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 77 | /* Array of per-port IO virtqueues */ |
| 78 | struct virtqueue **in_vqs, **out_vqs; |
| 79 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 80 | struct virtio_device *vdev; |
| 81 | }; |
| 82 | |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 83 | struct port_buffer { |
| 84 | char *buf; |
| 85 | |
| 86 | /* size of the buffer in *buf above */ |
| 87 | size_t size; |
| 88 | |
| 89 | /* used length of the buffer */ |
| 90 | size_t len; |
| 91 | /* offset in the buf from which to consume data */ |
| 92 | size_t offset; |
| 93 | }; |
| 94 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 95 | /* This struct holds the per-port data */ |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 96 | struct port { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 97 | /* Pointer to the parent virtio_console device */ |
| 98 | struct ports_device *portdev; |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 99 | |
| 100 | /* The current buffer from which data has to be fed to readers */ |
| 101 | struct port_buffer *inbuf; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 102 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 103 | /* |
| 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()). |
| 107 | */ |
| 108 | spinlock_t inbuf_lock; |
| 109 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 110 | /* The IO vqs for this port */ |
| 111 | struct virtqueue *in_vq, *out_vq; |
| 112 | |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 113 | /* |
| 114 | * The entries in this struct will be valid if this port is |
| 115 | * hooked up to an hvc console |
| 116 | */ |
| 117 | struct console cons; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 118 | }; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 119 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 120 | /* This is the very early arch-specified put chars function. */ |
| 121 | static int (*early_put_chars)(u32, const char *, int); |
| 122 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 123 | static struct port *find_port_by_vtermno(u32 vtermno) |
| 124 | { |
| 125 | struct port *port; |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 126 | struct console *cons; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 127 | unsigned long flags; |
| 128 | |
| 129 | spin_lock_irqsave(&pdrvdata_lock, flags); |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 130 | list_for_each_entry(cons, &pdrvdata.consoles, list) { |
| 131 | if (cons->vtermno == vtermno) { |
| 132 | port = container_of(cons, struct port, cons); |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 133 | goto out; |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 134 | } |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 135 | } |
| 136 | port = NULL; |
| 137 | out: |
| 138 | spin_unlock_irqrestore(&pdrvdata_lock, flags); |
| 139 | return port; |
| 140 | } |
| 141 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 142 | static struct port *find_port_by_vq(struct ports_device *portdev, |
| 143 | struct virtqueue *vq) |
| 144 | { |
| 145 | struct port *port; |
| 146 | struct console *cons; |
| 147 | unsigned long flags; |
| 148 | |
| 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) |
| 153 | goto out; |
| 154 | } |
| 155 | port = NULL; |
| 156 | out: |
| 157 | spin_unlock_irqrestore(&pdrvdata_lock, flags); |
| 158 | return port; |
| 159 | } |
| 160 | |
Amit Shah | fdb9a05 | 2010-01-18 19:15:01 +0530 | [diff] [blame] | 161 | static void free_buf(struct port_buffer *buf) |
| 162 | { |
| 163 | kfree(buf->buf); |
| 164 | kfree(buf); |
| 165 | } |
| 166 | |
| 167 | static struct port_buffer *alloc_buf(size_t buf_size) |
| 168 | { |
| 169 | struct port_buffer *buf; |
| 170 | |
| 171 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
| 172 | if (!buf) |
| 173 | goto fail; |
| 174 | buf->buf = kzalloc(buf_size, GFP_KERNEL); |
| 175 | if (!buf->buf) |
| 176 | goto free_buf; |
| 177 | buf->len = 0; |
| 178 | buf->offset = 0; |
| 179 | buf->size = buf_size; |
| 180 | return buf; |
| 181 | |
| 182 | free_buf: |
| 183 | kfree(buf); |
| 184 | fail: |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Amit Shah | a3cde44 | 2010-01-18 19:15:03 +0530 | [diff] [blame] | 188 | /* Callers should take appropriate locks */ |
| 189 | static void *get_inbuf(struct port *port) |
| 190 | { |
| 191 | struct port_buffer *buf; |
| 192 | struct virtqueue *vq; |
| 193 | unsigned int len; |
| 194 | |
| 195 | vq = port->in_vq; |
| 196 | buf = vq->vq_ops->get_buf(vq, &len); |
| 197 | if (buf) { |
| 198 | buf->len = len; |
| 199 | buf->offset = 0; |
| 200 | } |
| 201 | return buf; |
| 202 | } |
| 203 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 204 | /* |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 205 | * Create a scatter-gather list representing our input buffer and put |
| 206 | * it in the queue. |
| 207 | * |
| 208 | * Callers should take appropriate locks. |
| 209 | */ |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 210 | static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf) |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 211 | { |
| 212 | struct scatterlist sg[1]; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 213 | int ret; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 214 | |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 215 | sg_init_one(sg, buf->buf, buf->size); |
| 216 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 217 | ret = vq->vq_ops->add_buf(vq, sg, 0, 1, buf); |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 218 | vq->vq_ops->kick(vq); |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static bool port_has_data(struct port *port) |
| 223 | { |
| 224 | unsigned long flags; |
| 225 | bool ret; |
| 226 | |
| 227 | ret = false; |
| 228 | spin_lock_irqsave(&port->inbuf_lock, flags); |
| 229 | if (port->inbuf) |
| 230 | ret = true; |
| 231 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Give out the data that's requested from the buffer that we have |
| 238 | * queued up. |
| 239 | */ |
| 240 | static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count) |
| 241 | { |
| 242 | struct port_buffer *buf; |
| 243 | unsigned long flags; |
| 244 | |
| 245 | if (!out_count || !port_has_data(port)) |
| 246 | return 0; |
| 247 | |
| 248 | buf = port->inbuf; |
| 249 | if (out_count > buf->len - buf->offset) |
| 250 | out_count = buf->len - buf->offset; |
| 251 | |
| 252 | memcpy(out_buf, buf->buf + buf->offset, out_count); |
| 253 | |
| 254 | /* Return the number of bytes actually copied */ |
| 255 | buf->offset += out_count; |
| 256 | |
| 257 | if (buf->offset == buf->len) { |
| 258 | /* |
| 259 | * We're done using all the data in this buffer. |
| 260 | * Re-queue so that the Host can send us more data. |
| 261 | */ |
| 262 | spin_lock_irqsave(&port->inbuf_lock, flags); |
| 263 | port->inbuf = NULL; |
| 264 | |
| 265 | if (add_inbuf(port->in_vq, buf) < 0) |
| 266 | dev_warn(&port->portdev->vdev->dev, "failed add_buf\n"); |
| 267 | |
| 268 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
| 269 | } |
| 270 | return out_count; |
Amit Shah | e27b519 | 2010-01-18 19:15:02 +0530 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 274 | * The put_chars() callback is pretty straightforward. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 275 | * |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 276 | * We turn the characters into a scatter-gather list, add it to the |
| 277 | * output queue and then kick the Host. Then we sit here waiting for |
| 278 | * it to finish: inefficient in theory, but in practice |
| 279 | * implementations will do it immediately (lguest's Launcher does). |
| 280 | */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 281 | static int put_chars(u32 vtermno, const char *buf, int count) |
| 282 | { |
| 283 | struct scatterlist sg[1]; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 284 | struct port *port; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 285 | struct virtqueue *out_vq; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 286 | unsigned int len; |
| 287 | |
| 288 | port = find_port_by_vtermno(vtermno); |
| 289 | if (!port) |
| 290 | return 0; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 291 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 292 | if (unlikely(early_put_chars)) |
| 293 | return early_put_chars(vtermno, buf, count); |
| 294 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 295 | out_vq = port->out_vq; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 296 | /* This is a convenient routine to initialize a single-elem sg list */ |
| 297 | sg_init_one(sg, buf, count); |
| 298 | |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 299 | /* This shouldn't fail: if it does, we lose chars. */ |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 300 | if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, port) >= 0) { |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 301 | /* Tell Host to go! */ |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 302 | out_vq->vq_ops->kick(out_vq); |
| 303 | while (!out_vq->vq_ops->get_buf(out_vq, &len)) |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 304 | cpu_relax(); |
| 305 | } |
| 306 | |
| 307 | /* We're expected to return the amount of data we wrote: all of it. */ |
| 308 | return count; |
| 309 | } |
| 310 | |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 311 | /* |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 312 | * get_chars() is the callback from the hvc_console infrastructure |
| 313 | * when an interrupt is received. |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 314 | * |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 315 | * We call out to fill_readbuf that gets us the required data from the |
| 316 | * buffers that are queued up. |
Rusty Russell | a23ea92 | 2010-01-18 19:14:55 +0530 | [diff] [blame] | 317 | */ |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 318 | static int get_chars(u32 vtermno, char *buf, int count) |
| 319 | { |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 320 | struct port *port; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 321 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 322 | port = find_port_by_vtermno(vtermno); |
| 323 | if (!port) |
| 324 | return 0; |
Rusty Russell | 21206ed | 2010-01-18 19:15:00 +0530 | [diff] [blame] | 325 | |
| 326 | /* If we don't have an input queue yet, we can't get input. */ |
| 327 | BUG_ON(!port->in_vq); |
| 328 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 329 | return fill_readbuf(port, buf, count); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 330 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 331 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 332 | static void resize_console(struct port *port) |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 333 | { |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 334 | struct virtio_device *vdev; |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 335 | struct winsize ws; |
| 336 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 337 | vdev = port->portdev->vdev; |
| 338 | if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) { |
| 339 | vdev->config->get(vdev, |
| 340 | offsetof(struct virtio_console_config, cols), |
| 341 | &ws.ws_col, sizeof(u16)); |
| 342 | vdev->config->get(vdev, |
| 343 | offsetof(struct virtio_console_config, rows), |
| 344 | &ws.ws_row, sizeof(u16)); |
Amit Shah | 4f23c57 | 2010-01-18 19:15:09 +0530 | [diff] [blame] | 345 | hvc_resize(port->cons.hvc, ws); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 349 | static void virtcons_apply_config(struct virtio_device *vdev) |
| 350 | { |
| 351 | resize_console(find_port_by_vtermno(0)); |
| 352 | } |
| 353 | |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 354 | /* 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] | 355 | static int notifier_add_vio(struct hvc_struct *hp, int data) |
| 356 | { |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 357 | struct port *port; |
| 358 | |
| 359 | port = find_port_by_vtermno(hp->vtermno); |
| 360 | if (!port) |
| 361 | return -EINVAL; |
| 362 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 363 | hp->irq_requested = 1; |
Amit Shah | cb06e36 | 2010-01-18 19:15:08 +0530 | [diff] [blame] | 364 | resize_console(port); |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 365 | |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static void notifier_del_vio(struct hvc_struct *hp, int data) |
| 370 | { |
| 371 | hp->irq_requested = 0; |
| 372 | } |
| 373 | |
| 374 | static void hvc_handle_input(struct virtqueue *vq) |
| 375 | { |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 376 | struct port *port; |
| 377 | unsigned long flags; |
Rusty Russell | 7395448 | 2010-01-18 19:15:04 +0530 | [diff] [blame] | 378 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 379 | port = find_port_by_vq(vq->vdev->priv, vq); |
| 380 | if (!port) |
| 381 | return; |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 382 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 383 | spin_lock_irqsave(&port->inbuf_lock, flags); |
| 384 | port->inbuf = get_inbuf(port); |
| 385 | spin_unlock_irqrestore(&port->inbuf_lock, flags); |
| 386 | |
| 387 | if (hvc_poll(port->cons.hvc)) |
Christian Borntraeger | 91fcad1 | 2008-06-20 15:24:15 +0200 | [diff] [blame] | 388 | hvc_kick(); |
| 389 | } |
| 390 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 391 | /* The operations for the console. */ |
Rusty Russell | 1dff399 | 2009-11-28 12:20:26 +0530 | [diff] [blame] | 392 | static const struct hv_ops hv_ops = { |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 393 | .get_chars = get_chars, |
| 394 | .put_chars = put_chars, |
| 395 | .notifier_add = notifier_add_vio, |
| 396 | .notifier_del = notifier_del_vio, |
| 397 | .notifier_hangup = notifier_del_vio, |
| 398 | }; |
| 399 | |
| 400 | /* |
| 401 | * Console drivers are initialized very early so boot messages can go |
| 402 | * out, so we do things slightly differently from the generic virtio |
| 403 | * initialization of the net and block drivers. |
| 404 | * |
| 405 | * At this stage, the console is output-only. It's too early to set |
| 406 | * up a virtqueue, so we let the drivers do some boutique early-output |
| 407 | * thing. |
| 408 | */ |
| 409 | int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int)) |
| 410 | { |
| 411 | early_put_chars = put_chars; |
| 412 | return hvc_instantiate(0, 0, &hv_ops); |
| 413 | } |
| 414 | |
Amit Shah | cfa6d37 | 2010-01-18 19:15:10 +0530 | [diff] [blame] | 415 | int __devinit init_port_console(struct port *port) |
| 416 | { |
| 417 | int ret; |
| 418 | |
| 419 | /* |
| 420 | * The Host's telling us this port is a console port. Hook it |
| 421 | * up with an hvc console. |
| 422 | * |
| 423 | * To set up and manage our virtual console, we call |
| 424 | * hvc_alloc(). |
| 425 | * |
| 426 | * The first argument of hvc_alloc() is the virtual console |
| 427 | * number. The second argument is the parameter for the |
| 428 | * notification mechanism (like irq number). We currently |
| 429 | * leave this as zero, virtqueues have implicit notifications. |
| 430 | * |
| 431 | * The third argument is a "struct hv_ops" containing the |
| 432 | * put_chars() get_chars(), notifier_add() and notifier_del() |
| 433 | * pointers. The final argument is the output buffer size: we |
| 434 | * can do any size, so we put PAGE_SIZE here. |
| 435 | */ |
| 436 | port->cons.vtermno = pdrvdata.next_vtermno; |
| 437 | |
| 438 | port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE); |
| 439 | if (IS_ERR(port->cons.hvc)) { |
| 440 | ret = PTR_ERR(port->cons.hvc); |
| 441 | port->cons.hvc = NULL; |
| 442 | return ret; |
| 443 | } |
| 444 | spin_lock_irq(&pdrvdata_lock); |
| 445 | pdrvdata.next_vtermno++; |
| 446 | list_add_tail(&port->cons.list, &pdrvdata.consoles); |
| 447 | spin_unlock_irq(&pdrvdata_lock); |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 452 | static int __devinit add_port(struct ports_device *portdev) |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 453 | { |
| 454 | struct port *port; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 455 | struct port_buffer *inbuf; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 456 | int err; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 457 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 458 | port = kmalloc(sizeof(*port), GFP_KERNEL); |
Rusty Russell | d8a02bd | 2010-01-18 19:15:06 +0530 | [diff] [blame] | 459 | if (!port) { |
| 460 | err = -ENOMEM; |
| 461 | goto fail; |
Amit Shah | f550804 | 2010-01-18 19:14:59 +0530 | [diff] [blame] | 462 | } |
Rusty Russell | 7395448 | 2010-01-18 19:15:04 +0530 | [diff] [blame] | 463 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 464 | port->portdev = portdev; |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 465 | |
| 466 | port->inbuf = NULL; |
| 467 | |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 468 | port->in_vq = portdev->in_vqs[0]; |
| 469 | port->out_vq = portdev->out_vqs[0]; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 470 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 471 | spin_lock_init(&port->inbuf_lock); |
| 472 | |
| 473 | inbuf = alloc_buf(PAGE_SIZE); |
| 474 | if (!inbuf) { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 475 | err = -ENOMEM; |
| 476 | goto free_port; |
| 477 | } |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 478 | |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 479 | /* Register the input buffer the first time. */ |
| 480 | add_inbuf(port->in_vq, inbuf); |
| 481 | |
Amit Shah | cfa6d37 | 2010-01-18 19:15:10 +0530 | [diff] [blame] | 482 | err = init_port_console(port); |
| 483 | if (err) |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 484 | goto free_inbuf; |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 485 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 486 | return 0; |
| 487 | |
| 488 | free_inbuf: |
Amit Shah | 203baab | 2010-01-18 19:15:12 +0530 | [diff] [blame^] | 489 | free_buf(inbuf); |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 490 | free_port: |
| 491 | kfree(port); |
| 492 | fail: |
| 493 | return err; |
| 494 | } |
| 495 | |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 496 | static int init_vqs(struct ports_device *portdev) |
| 497 | { |
| 498 | vq_callback_t **io_callbacks; |
| 499 | char **io_names; |
| 500 | struct virtqueue **vqs; |
| 501 | u32 nr_ports, nr_queues; |
| 502 | int err; |
| 503 | |
| 504 | /* We currently only have one port and two queues for that port */ |
| 505 | nr_ports = 1; |
| 506 | nr_queues = 2; |
| 507 | |
| 508 | vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL); |
| 509 | if (!vqs) { |
| 510 | err = -ENOMEM; |
| 511 | goto fail; |
| 512 | } |
| 513 | io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL); |
| 514 | if (!io_callbacks) { |
| 515 | err = -ENOMEM; |
| 516 | goto free_vqs; |
| 517 | } |
| 518 | io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL); |
| 519 | if (!io_names) { |
| 520 | err = -ENOMEM; |
| 521 | goto free_callbacks; |
| 522 | } |
| 523 | portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), |
| 524 | GFP_KERNEL); |
| 525 | if (!portdev->in_vqs) { |
| 526 | err = -ENOMEM; |
| 527 | goto free_names; |
| 528 | } |
| 529 | portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), |
| 530 | GFP_KERNEL); |
| 531 | if (!portdev->out_vqs) { |
| 532 | err = -ENOMEM; |
| 533 | goto free_invqs; |
| 534 | } |
| 535 | |
| 536 | io_callbacks[0] = hvc_handle_input; |
| 537 | io_callbacks[1] = NULL; |
| 538 | io_names[0] = "input"; |
| 539 | io_names[1] = "output"; |
| 540 | |
| 541 | /* Find the queues. */ |
| 542 | err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs, |
| 543 | io_callbacks, |
| 544 | (const char **)io_names); |
| 545 | if (err) |
| 546 | goto free_outvqs; |
| 547 | |
| 548 | portdev->in_vqs[0] = vqs[0]; |
| 549 | portdev->out_vqs[0] = vqs[1]; |
| 550 | |
| 551 | kfree(io_callbacks); |
| 552 | kfree(io_names); |
| 553 | kfree(vqs); |
| 554 | |
| 555 | return 0; |
| 556 | |
| 557 | free_names: |
| 558 | kfree(io_names); |
| 559 | free_callbacks: |
| 560 | kfree(io_callbacks); |
| 561 | free_outvqs: |
| 562 | kfree(portdev->out_vqs); |
| 563 | free_invqs: |
| 564 | kfree(portdev->in_vqs); |
| 565 | free_vqs: |
| 566 | kfree(vqs); |
| 567 | fail: |
| 568 | return err; |
| 569 | } |
| 570 | |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 571 | /* |
| 572 | * Once we're further in boot, we get probed like any other virtio |
| 573 | * device. |
| 574 | */ |
| 575 | static int __devinit virtcons_probe(struct virtio_device *vdev) |
| 576 | { |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 577 | struct ports_device *portdev; |
| 578 | int err; |
| 579 | |
| 580 | portdev = kmalloc(sizeof(*portdev), GFP_KERNEL); |
| 581 | if (!portdev) { |
| 582 | err = -ENOMEM; |
| 583 | goto fail; |
| 584 | } |
| 585 | |
| 586 | /* Attach this portdev to this virtio_device, and vice-versa. */ |
| 587 | portdev->vdev = vdev; |
| 588 | vdev->priv = portdev; |
| 589 | |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 590 | err = init_vqs(portdev); |
| 591 | if (err < 0) { |
| 592 | dev_err(&vdev->dev, "Error %d initializing vqs\n", err); |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 593 | goto free; |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 594 | } |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 595 | |
| 596 | /* We only have one port. */ |
| 597 | err = add_port(portdev); |
| 598 | if (err) |
| 599 | goto free_vqs; |
| 600 | |
Rusty Russell | 971f339 | 2010-01-18 19:14:56 +0530 | [diff] [blame] | 601 | /* Start using the new console output. */ |
| 602 | early_put_chars = NULL; |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 603 | return 0; |
| 604 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 605 | free_vqs: |
| 606 | vdev->config->del_vqs(vdev); |
Amit Shah | 2658a79 | 2010-01-18 19:15:11 +0530 | [diff] [blame] | 607 | kfree(portdev->in_vqs); |
| 608 | kfree(portdev->out_vqs); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 609 | free: |
Amit Shah | 1c85bf3 | 2010-01-18 19:15:07 +0530 | [diff] [blame] | 610 | kfree(portdev); |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 611 | fail: |
| 612 | return err; |
| 613 | } |
| 614 | |
| 615 | static struct virtio_device_id id_table[] = { |
| 616 | { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, |
| 617 | { 0 }, |
| 618 | }; |
| 619 | |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 620 | static unsigned int features[] = { |
| 621 | VIRTIO_CONSOLE_F_SIZE, |
| 622 | }; |
| 623 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 624 | static struct virtio_driver virtio_console = { |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 625 | .feature_table = features, |
| 626 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 627 | .driver.name = KBUILD_MODNAME, |
| 628 | .driver.owner = THIS_MODULE, |
| 629 | .id_table = id_table, |
| 630 | .probe = virtcons_probe, |
Christian Borntraeger | c298345 | 2008-11-25 13:36:26 +0100 | [diff] [blame] | 631 | .config_changed = virtcons_apply_config, |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static int __init init(void) |
| 635 | { |
Amit Shah | 38edf58 | 2010-01-18 19:15:05 +0530 | [diff] [blame] | 636 | INIT_LIST_HEAD(&pdrvdata.consoles); |
| 637 | |
Rusty Russell | 3161043 | 2007-10-22 11:03:39 +1000 | [diff] [blame] | 638 | return register_virtio_driver(&virtio_console); |
| 639 | } |
| 640 | module_init(init); |
| 641 | |
| 642 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 643 | MODULE_DESCRIPTION("Virtio console driver"); |
| 644 | MODULE_LICENSE("GPL"); |