hvc_lguest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*D:300
  2. * The Guest console driver
  3. *
  4. * This is a trivial console driver: we use lguest's DMA mechanism to send
  5. * bytes out, and register a DMA buffer to receive bytes in. It is assumed to
  6. * be present and available from the very beginning of boot.
  7. *
  8. * Writing console drivers is one of the few remaining Dark Arts in Linux.
  9. * Fortunately for us, the path of virtual consoles has been well-trodden by
  10. * the PowerPC folks, who wrote "hvc_console.c" to generically support any
  11. * virtual console. We use that infrastructure which only requires us to write
  12. * the basic put_chars and get_chars functions and call the right register
  13. * functions.
  14. :*/
  15. /*M:002 The console can be flooded: while the Guest is processing input the
  16. * Host can send more. Buffering in the Host could alleviate this, but it is a
  17. * difficult problem in general. :*/
  18. /* Copyright (C) 2006 Rusty Russell, IBM Corporation
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. */
  34. #include <linux/err.h>
  35. #include <linux/init.h>
  36. #include <linux/lguest_bus.h>
  37. #include "hvc_console.h"
  38. /*D:340 This is our single console input buffer, with associated "struct
  39. * lguest_dma" referring to it. Note the 0-terminated length array, and the
  40. * use of physical address for the buffer itself. */
  41. static char inbuf[256];
  42. static struct lguest_dma cons_input = { .used_len = 0,
  43. .addr[0] = __pa(inbuf),
  44. .len[0] = sizeof(inbuf),
  45. .len[1] = 0 };
  46. /*D:310 The put_chars() callback is pretty straightforward.
  47. *
  48. * First we put the pointer and length in a "struct lguest_dma": we only have
  49. * one pointer, so we set the second length to 0. Then we use SEND_DMA to send
  50. * the data to (Host) buffers attached to the console key. Usually a device's
  51. * key is a physical address within the device's memory, but because the
  52. * console device doesn't have any associated physical memory, we use the
  53. * LGUEST_CONSOLE_DMA_KEY constant (aka 0). */
  54. static int put_chars(u32 vtermno, const char *buf, int count)
  55. {
  56. struct lguest_dma dma;
  57. /* FIXME: DMA buffers in a "struct lguest_dma" are not allowed
  58. * to go over page boundaries. This never seems to happen,
  59. * but if it did we'd need to fix this code. */
  60. dma.len[0] = count;
  61. dma.len[1] = 0;
  62. dma.addr[0] = __pa(buf);
  63. lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
  64. /* We're expected to return the amount of data we wrote: all of it. */
  65. return count;
  66. }
  67. /*D:350 get_chars() is the callback from the hvc_console infrastructure when
  68. * an interrupt is received.
  69. *
  70. * Firstly we see if our buffer has been filled: if not, we return. The rest
  71. * of the code deals with the fact that the hvc_console() infrastructure only
  72. * asks us for 16 bytes at a time. We keep a "cons_offset" variable for
  73. * partially-read buffers. */
  74. static int get_chars(u32 vtermno, char *buf, int count)
  75. {
  76. static int cons_offset;
  77. /* Nothing left to see here... */
  78. if (!cons_input.used_len)
  79. return 0;
  80. /* You want more than we have to give? Well, try wanting less! */
  81. if (cons_input.used_len - cons_offset < count)
  82. count = cons_input.used_len - cons_offset;
  83. /* Copy across to their buffer and increment offset. */
  84. memcpy(buf, inbuf + cons_offset, count);
  85. cons_offset += count;
  86. /* Finished? Zero offset, and reset cons_input so Host will use it
  87. * again. */
  88. if (cons_offset == cons_input.used_len) {
  89. cons_offset = 0;
  90. cons_input.used_len = 0;
  91. }
  92. return count;
  93. }
  94. /*:*/
  95. static struct hv_ops lguest_cons = {
  96. .get_chars = get_chars,
  97. .put_chars = put_chars,
  98. };
  99. /*D:320 Console drivers are initialized very early so boot messages can go
  100. * out. At this stage, the console is output-only. Our driver checks we're a
  101. * Guest, and if so hands hvc_instantiate() the console number (0), priority
  102. * (0), and the struct hv_ops containing the put_chars() function. */
  103. static int __init cons_init(void)
  104. {
  105. if (strcmp(paravirt_ops.name, "lguest") != 0)
  106. return 0;
  107. return hvc_instantiate(0, 0, &lguest_cons);
  108. }
  109. console_initcall(cons_init);
  110. /*D:370 To set up and manage our virtual console, we call hvc_alloc() and
  111. * stash the result in the private pointer of the "struct lguest_device".
  112. * Since we never remove the console device we never need this pointer again,
  113. * but using ->private is considered good form, and you never know who's going
  114. * to copy your driver.
  115. *
  116. * Once the console is set up, we bind our input buffer ready for input. */
  117. static int lguestcons_probe(struct lguest_device *lgdev)
  118. {
  119. int err;
  120. /* The first argument of hvc_alloc() is the virtual console number, so
  121. * we use zero. The second argument is the interrupt number.
  122. *
  123. * The third argument is a "struct hv_ops" containing the put_chars()
  124. * and get_chars() pointers. The final argument is the output buffer
  125. * size: we use 256 and expect the Host to have room for us to send
  126. * that much. */
  127. lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
  128. if (IS_ERR(lgdev->private))
  129. return PTR_ERR(lgdev->private);
  130. /* We bind a single DMA buffer at key LGUEST_CONSOLE_DMA_KEY.
  131. * "cons_input" is that statically-initialized global DMA buffer we saw
  132. * above, and we also give the interrupt we want. */
  133. err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
  134. lgdev_irq(lgdev));
  135. if (err)
  136. printk("lguest console: failed to bind buffer.\n");
  137. return err;
  138. }
  139. /* Note the use of lgdev_irq() for the interrupt number. We tell hvc_alloc()
  140. * to expect input when this interrupt is triggered, and then tell
  141. * lguest_bind_dma() that is the interrupt to send us when input comes in. */
  142. /*D:360 From now on the console driver follows standard Guest driver form:
  143. * register_lguest_driver() registers the device type and probe function, and
  144. * the probe function sets up the device.
  145. *
  146. * The standard "struct lguest_driver": */
  147. static struct lguest_driver lguestcons_drv = {
  148. .name = "lguestcons",
  149. .owner = THIS_MODULE,
  150. .device_type = LGUEST_DEVICE_T_CONSOLE,
  151. .probe = lguestcons_probe,
  152. };
  153. /* The standard init function */
  154. static int __init hvc_lguest_init(void)
  155. {
  156. return register_lguest_driver(&lguestcons_drv);
  157. }
  158. module_init(hvc_lguest_init);