virtio_console.c 8.1 KB

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