virtio_console.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * we support only one console, the hvc struct is a global var
  123. * There is no need to do anything
  124. */
  125. static int notifier_add_vio(struct hvc_struct *hp, int data)
  126. {
  127. hp->irq_requested = 1;
  128. return 0;
  129. }
  130. static void notifier_del_vio(struct hvc_struct *hp, int data)
  131. {
  132. hp->irq_requested = 0;
  133. }
  134. static void hvc_handle_input(struct virtqueue *vq)
  135. {
  136. if (hvc_poll(hvc))
  137. hvc_kick();
  138. }
  139. /*D:370 Once we're further in boot, we get probed like any other virtio device.
  140. * At this stage we set up the output virtqueue.
  141. *
  142. * To set up and manage our virtual console, we call hvc_alloc(). Since we
  143. * never remove the console device we never need this pointer again.
  144. *
  145. * Finally we put our input buffer in the input queue, ready to receive. */
  146. static int __devinit virtcons_probe(struct virtio_device *dev)
  147. {
  148. int err;
  149. vdev = dev;
  150. /* This is the scratch page we use to receive console input */
  151. inbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  152. if (!inbuf) {
  153. err = -ENOMEM;
  154. goto fail;
  155. }
  156. /* Find the input queue. */
  157. /* FIXME: This is why we want to wean off hvc: we do nothing
  158. * when input comes in. */
  159. in_vq = vdev->config->find_vq(vdev, 0, hvc_handle_input);
  160. if (IS_ERR(in_vq)) {
  161. err = PTR_ERR(in_vq);
  162. goto free;
  163. }
  164. out_vq = vdev->config->find_vq(vdev, 1, NULL);
  165. if (IS_ERR(out_vq)) {
  166. err = PTR_ERR(out_vq);
  167. goto free_in_vq;
  168. }
  169. /* Start using the new console output. */
  170. virtio_cons.get_chars = get_chars;
  171. virtio_cons.put_chars = put_chars;
  172. virtio_cons.notifier_add = notifier_add_vio;
  173. virtio_cons.notifier_del = notifier_del_vio;
  174. /* The first argument of hvc_alloc() is the virtual console number, so
  175. * we use zero. The second argument is the parameter for the
  176. * notification mechanism (like irq number). We currently leave this
  177. * as zero, virtqueues have implicit notifications.
  178. *
  179. * The third argument is a "struct hv_ops" containing the put_chars()
  180. * get_chars(), notifier_add() and notifier_del() pointers.
  181. * The final argument is the output buffer size: we can do any size,
  182. * so we put PAGE_SIZE here. */
  183. hvc = hvc_alloc(0, 0, &virtio_cons, PAGE_SIZE);
  184. if (IS_ERR(hvc)) {
  185. err = PTR_ERR(hvc);
  186. goto free_out_vq;
  187. }
  188. /* Register the input buffer the first time. */
  189. add_inbuf();
  190. return 0;
  191. free_out_vq:
  192. vdev->config->del_vq(out_vq);
  193. free_in_vq:
  194. vdev->config->del_vq(in_vq);
  195. free:
  196. kfree(inbuf);
  197. fail:
  198. return err;
  199. }
  200. static struct virtio_device_id id_table[] = {
  201. { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
  202. { 0 },
  203. };
  204. static struct virtio_driver virtio_console = {
  205. .driver.name = KBUILD_MODNAME,
  206. .driver.owner = THIS_MODULE,
  207. .id_table = id_table,
  208. .probe = virtcons_probe,
  209. };
  210. static int __init init(void)
  211. {
  212. return register_virtio_driver(&virtio_console);
  213. }
  214. module_init(init);
  215. MODULE_DEVICE_TABLE(virtio, id_table);
  216. MODULE_DESCRIPTION("Virtio console driver");
  217. MODULE_LICENSE("GPL");