virtio_console.c 8.1 KB

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