virtio_console.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
  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>
  20. #include <linux/virtio.h>
  21. #include <linux/virtio_console.h>
  22. #include "hvc_console.h"
  23. static struct virtqueue *in_vq, *out_vq;
  24. static struct virtio_device *vdev;
  25. /* This is our input buffer, and how much data is left in it. */
  26. static unsigned int in_len;
  27. static char *in, *inbuf;
  28. /* The hvc device */
  29. static struct hvc_struct *hvc;
  30. /* This is the very early arch-specified put chars function. */
  31. static int (*early_put_chars)(u32, const char *, int);
  32. /*
  33. * The put_chars() callback is pretty straightforward.
  34. *
  35. * We turn the characters into a scatter-gather list, add it to the
  36. * output queue and then kick the Host. Then we sit here waiting for
  37. * it to finish: inefficient in theory, but in practice
  38. * implementations will do it immediately (lguest's Launcher does).
  39. */
  40. static int put_chars(u32 vtermno, const char *buf, int count)
  41. {
  42. struct scatterlist sg[1];
  43. unsigned int len;
  44. if (unlikely(early_put_chars))
  45. return early_put_chars(vtermno, buf, count);
  46. /* This is a convenient routine to initialize a single-elem sg list */
  47. sg_init_one(sg, buf, count);
  48. /*
  49. * add_buf wants a token to identify this buffer: we hand it
  50. * any non-NULL pointer, since there's only ever one buffer.
  51. */
  52. if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) >= 0) {
  53. /* Tell Host to go! */
  54. out_vq->vq_ops->kick(out_vq);
  55. /* Chill out until it's done with the buffer. */
  56. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  57. cpu_relax();
  58. }
  59. /* We're expected to return the amount of data we wrote: all of it. */
  60. return count;
  61. }
  62. /*
  63. * Create a scatter-gather list representing our input buffer and put
  64. * it in the queue.
  65. */
  66. static void add_inbuf(void)
  67. {
  68. struct scatterlist sg[1];
  69. sg_init_one(sg, inbuf, PAGE_SIZE);
  70. /* We should always be able to add one buffer to an empty queue. */
  71. if (in_vq->vq_ops->add_buf(in_vq, sg, 0, 1, inbuf) < 0)
  72. BUG();
  73. in_vq->vq_ops->kick(in_vq);
  74. }
  75. /*
  76. * get_chars() is the callback from the hvc_console infrastructure
  77. * when an interrupt is received.
  78. *
  79. * Most of the code deals with the fact that the hvc_console()
  80. * infrastructure only asks us for 16 bytes at a time. We keep
  81. * in_offset and in_used fields for partially-filled buffers.
  82. */
  83. static int get_chars(u32 vtermno, char *buf, int count)
  84. {
  85. /* If we don't have an input queue yet, we can't get input. */
  86. BUG_ON(!in_vq);
  87. /* No buffer? Try to get one. */
  88. if (!in_len) {
  89. in = in_vq->vq_ops->get_buf(in_vq, &in_len);
  90. if (!in)
  91. return 0;
  92. }
  93. /* You want more than we have to give? Well, try wanting less! */
  94. if (in_len < count)
  95. count = in_len;
  96. /* Copy across to their buffer and increment offset. */
  97. memcpy(buf, in, count);
  98. in += count;
  99. in_len -= count;
  100. /* Finished? Re-register buffer so Host will use it again. */
  101. if (in_len == 0)
  102. add_inbuf();
  103. return count;
  104. }
  105. /*
  106. * virtio console configuration. This supports:
  107. * - console resize
  108. */
  109. static void virtcons_apply_config(struct virtio_device *dev)
  110. {
  111. struct winsize ws;
  112. if (virtio_has_feature(dev, VIRTIO_CONSOLE_F_SIZE)) {
  113. dev->config->get(dev,
  114. offsetof(struct virtio_console_config, cols),
  115. &ws.ws_col, sizeof(u16));
  116. dev->config->get(dev,
  117. offsetof(struct virtio_console_config, rows),
  118. &ws.ws_row, sizeof(u16));
  119. hvc_resize(hvc, ws);
  120. }
  121. }
  122. /*
  123. * we support only one console, the hvc struct is a global var We set
  124. * the configuration at this point, since we now have a tty
  125. */
  126. static int notifier_add_vio(struct hvc_struct *hp, int data)
  127. {
  128. hp->irq_requested = 1;
  129. virtcons_apply_config(vdev);
  130. return 0;
  131. }
  132. static void notifier_del_vio(struct hvc_struct *hp, int data)
  133. {
  134. hp->irq_requested = 0;
  135. }
  136. static void hvc_handle_input(struct virtqueue *vq)
  137. {
  138. if (hvc_poll(hvc))
  139. hvc_kick();
  140. }
  141. /* The operations for the console. */
  142. static const struct hv_ops hv_ops = {
  143. .get_chars = get_chars,
  144. .put_chars = put_chars,
  145. .notifier_add = notifier_add_vio,
  146. .notifier_del = notifier_del_vio,
  147. .notifier_hangup = notifier_del_vio,
  148. };
  149. /*
  150. * Console drivers are initialized very early so boot messages can go
  151. * out, so we do things slightly differently from the generic virtio
  152. * initialization of the net and block drivers.
  153. *
  154. * At this stage, the console is output-only. It's too early to set
  155. * up a virtqueue, so we let the drivers do some boutique early-output
  156. * thing.
  157. */
  158. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  159. {
  160. early_put_chars = put_chars;
  161. return hvc_instantiate(0, 0, &hv_ops);
  162. }
  163. /*
  164. * Once we're further in boot, we get probed like any other virtio
  165. * device. At this stage we set up the output virtqueue.
  166. *
  167. * To set up and manage our virtual console, we call hvc_alloc().
  168. * Since we never remove the console device we never need this pointer
  169. * again.
  170. *
  171. * Finally we put our input buffer in the input queue, ready to
  172. * receive.
  173. */
  174. static int __devinit virtcons_probe(struct virtio_device *dev)
  175. {
  176. vq_callback_t *callbacks[] = { hvc_handle_input, NULL};
  177. const char *names[] = { "input", "output" };
  178. struct virtqueue *vqs[2];
  179. int err;
  180. if (vdev) {
  181. dev_warn(&vdev->dev,
  182. "Multiple virtio-console devices not supported yet\n");
  183. return -EEXIST;
  184. }
  185. vdev = dev;
  186. /* This is the scratch page we use to receive console input */
  187. inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  188. if (!inbuf) {
  189. err = -ENOMEM;
  190. goto fail;
  191. }
  192. /* Find the queues. */
  193. err = vdev->config->find_vqs(vdev, 2, vqs, callbacks, names);
  194. if (err)
  195. goto free;
  196. in_vq = vqs[0];
  197. out_vq = vqs[1];
  198. /*
  199. * The first argument of hvc_alloc() is the virtual console
  200. * number, so we use zero. The second argument is the
  201. * parameter for the notification mechanism (like irq
  202. * number). We currently leave this as zero, virtqueues have
  203. * implicit notifications.
  204. *
  205. * The third argument is a "struct hv_ops" containing the
  206. * put_chars(), get_chars(), notifier_add() and notifier_del()
  207. * pointers. The final argument is the output buffer size: we
  208. * can do any size, so we put PAGE_SIZE here.
  209. */
  210. hvc = hvc_alloc(0, 0, &hv_ops, PAGE_SIZE);
  211. if (IS_ERR(hvc)) {
  212. err = PTR_ERR(hvc);
  213. goto free_vqs;
  214. }
  215. /* Register the input buffer the first time. */
  216. add_inbuf();
  217. /* Start using the new console output. */
  218. early_put_chars = NULL;
  219. return 0;
  220. free_vqs:
  221. vdev->config->del_vqs(vdev);
  222. free:
  223. kfree(inbuf);
  224. fail:
  225. return err;
  226. }
  227. static struct virtio_device_id id_table[] = {
  228. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  229. { 0 },
  230. };
  231. static unsigned int features[] = {
  232. VIRTIO_CONSOLE_F_SIZE,
  233. };
  234. static struct virtio_driver virtio_console = {
  235. .feature_table = features,
  236. .feature_table_size = ARRAY_SIZE(features),
  237. .driver.name = KBUILD_MODNAME,
  238. .driver.owner = THIS_MODULE,
  239. .id_table = id_table,
  240. .probe = virtcons_probe,
  241. .config_changed = virtcons_apply_config,
  242. };
  243. static int __init init(void)
  244. {
  245. return register_virtio_driver(&virtio_console);
  246. }
  247. module_init(init);
  248. MODULE_DEVICE_TABLE(virtio, id_table);
  249. MODULE_DESCRIPTION("Virtio console driver");
  250. MODULE_LICENSE("GPL");