virtio_console.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /*D:310 The put_chars() callback is pretty straightforward.
  45. *
  46. * We turn the characters into a scatter-gather list, add it to the output
  47. * queue and then kick the Host. Then we sit here waiting for it to finish:
  48. * inefficient in theory, but in practice implementations will do it
  49. * immediately (lguest's Launcher does). */
  50. static int put_chars(u32 vtermno, const char *buf, int count)
  51. {
  52. struct scatterlist sg[1];
  53. unsigned int len;
  54. /* This is a convenient routine to initialize a single-elem sg list */
  55. sg_init_one(sg, buf, count);
  56. /* add_buf wants a token to identify this buffer: we hand it any
  57. * non-NULL pointer, since there's only ever one buffer. */
  58. if (out_vq->vq_ops->add_buf(out_vq, sg, 1, 0, (void *)1) == 0) {
  59. /* Tell Host to go! */
  60. out_vq->vq_ops->kick(out_vq);
  61. /* Chill out until it's done with the buffer. */
  62. while (!out_vq->vq_ops->get_buf(out_vq, &len))
  63. cpu_relax();
  64. }
  65. /* We're expected to return the amount of data we wrote: all of it. */
  66. return count;
  67. }
  68. /* Create a scatter-gather list representing our input buffer and put it in the
  69. * queue. */
  70. static void add_inbuf(void)
  71. {
  72. struct scatterlist sg[1];
  73. sg_init_one(sg, inbuf, PAGE_SIZE);
  74. /* We should always be able to add one buffer to an empty queue. */
  75. if (in_vq->vq_ops->add_buf(in_vq, sg, 0, 1, inbuf) != 0)
  76. BUG();
  77. in_vq->vq_ops->kick(in_vq);
  78. }
  79. /*D:350 get_chars() is the callback from the hvc_console infrastructure when
  80. * an interrupt is received.
  81. *
  82. * Most of the code deals with the fact that the hvc_console() infrastructure
  83. * only asks us for 16 bytes at a time. We keep in_offset and in_used fields
  84. * for partially-filled buffers. */
  85. static int get_chars(u32 vtermno, char *buf, int count)
  86. {
  87. /* If we don't have an input queue yet, we can't get input. */
  88. BUG_ON(!in_vq);
  89. /* No buffer? Try to get one. */
  90. if (!in_len) {
  91. in = in_vq->vq_ops->get_buf(in_vq, &in_len);
  92. if (!in)
  93. return 0;
  94. }
  95. /* You want more than we have to give? Well, try wanting less! */
  96. if (in_len < count)
  97. count = in_len;
  98. /* Copy across to their buffer and increment offset. */
  99. memcpy(buf, in, count);
  100. in += count;
  101. in_len -= count;
  102. /* Finished? Re-register buffer so Host will use it again. */
  103. if (in_len == 0)
  104. add_inbuf();
  105. return count;
  106. }
  107. /*:*/
  108. /*D:320 Console drivers are initialized very early so boot messages can go out,
  109. * so we do things slightly differently from the generic virtio initialization
  110. * of the net and block drivers.
  111. *
  112. * At this stage, the console is output-only. It's too early to set up a
  113. * virtqueue, so we let the drivers do some boutique early-output thing. */
  114. int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
  115. {
  116. virtio_cons.put_chars = put_chars;
  117. return hvc_instantiate(0, 0, &virtio_cons);
  118. }
  119. /*D:370 Once we're further in boot, we get probed like any other virtio device.
  120. * At this stage we set up the output virtqueue.
  121. *
  122. * To set up and manage our virtual console, we call hvc_alloc(). Since we
  123. * never remove the console device we never need this pointer again.
  124. *
  125. * Finally we put our input buffer in the input queue, ready to receive. */
  126. static int __devinit virtcons_probe(struct virtio_device *dev)
  127. {
  128. int err;
  129. struct hvc_struct *hvc;
  130. vdev = dev;
  131. /* This is the scratch page we use to receive console input */
  132. inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  133. if (!inbuf) {
  134. err = -ENOMEM;
  135. goto fail;
  136. }
  137. /* Find the input queue. */
  138. /* FIXME: This is why we want to wean off hvc: we do nothing
  139. * when input comes in. */
  140. in_vq = vdev->config->find_vq(vdev, NULL);
  141. if (IS_ERR(in_vq)) {
  142. err = PTR_ERR(in_vq);
  143. goto free;
  144. }
  145. out_vq = vdev->config->find_vq(vdev, NULL);
  146. if (IS_ERR(out_vq)) {
  147. err = PTR_ERR(out_vq);
  148. goto free_in_vq;
  149. }
  150. /* Start using the new console output. */
  151. virtio_cons.get_chars = get_chars;
  152. virtio_cons.put_chars = put_chars;
  153. /* The first argument of hvc_alloc() is the virtual console number, so
  154. * we use zero. The second argument is the interrupt number; we
  155. * currently leave this as zero: it would be better not to use the
  156. * hvc mechanism and fix this (FIXME!).
  157. *
  158. * The third argument is a "struct hv_ops" containing the put_chars()
  159. * and get_chars() pointers. The final argument is the output buffer
  160. * size: we can do any size, so we put PAGE_SIZE here. */
  161. hvc = hvc_alloc(0, 0, &virtio_cons, PAGE_SIZE);
  162. if (IS_ERR(hvc)) {
  163. err = PTR_ERR(hvc);
  164. goto free_out_vq;
  165. }
  166. /* Register the input buffer the first time. */
  167. add_inbuf();
  168. return 0;
  169. free_out_vq:
  170. vdev->config->del_vq(out_vq);
  171. free_in_vq:
  172. vdev->config->del_vq(in_vq);
  173. free:
  174. kfree(inbuf);
  175. fail:
  176. return err;
  177. }
  178. static struct virtio_device_id id_table[] = {
  179. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  180. { 0 },
  181. };
  182. static struct virtio_driver virtio_console = {
  183. .driver.name = KBUILD_MODNAME,
  184. .driver.owner = THIS_MODULE,
  185. .id_table = id_table,
  186. .probe = virtcons_probe,
  187. };
  188. static int __init init(void)
  189. {
  190. return register_virtio_driver(&virtio_console);
  191. }
  192. module_init(init);
  193. MODULE_DEVICE_TABLE(virtio, id_table);
  194. MODULE_DESCRIPTION("Virtio console driver");
  195. MODULE_LICENSE("GPL");