hvc_xen.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * xen console driver interface to hvc_console.c
  3. *
  4. * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/console.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <asm/xen/hypervisor.h>
  26. #include <xen/page.h>
  27. #include <xen/events.h>
  28. #include <xen/interface/io/console.h>
  29. #include <xen/hvc-console.h>
  30. #include "hvc_console.h"
  31. #define HVC_COOKIE 0x58656e /* "Xen" in hex */
  32. static struct hvc_struct *hvc;
  33. static int xencons_irq;
  34. /* ------------------------------------------------------------------ */
  35. static inline struct xencons_interface *xencons_interface(void)
  36. {
  37. return mfn_to_virt(xen_start_info->console.domU.mfn);
  38. }
  39. static inline void notify_daemon(void)
  40. {
  41. /* Use evtchn: this is called early, before irq is set up. */
  42. notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
  43. }
  44. static int write_console(uint32_t vtermno, const char *data, int len)
  45. {
  46. struct xencons_interface *intf = xencons_interface();
  47. XENCONS_RING_IDX cons, prod;
  48. int sent = 0;
  49. cons = intf->out_cons;
  50. prod = intf->out_prod;
  51. mb(); /* update queue values before going on */
  52. BUG_ON((prod - cons) > sizeof(intf->out));
  53. while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
  54. intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
  55. wmb(); /* write ring before updating pointer */
  56. intf->out_prod = prod;
  57. notify_daemon();
  58. return sent;
  59. }
  60. static int read_console(uint32_t vtermno, char *buf, int len)
  61. {
  62. struct xencons_interface *intf = xencons_interface();
  63. XENCONS_RING_IDX cons, prod;
  64. int recv = 0;
  65. cons = intf->in_cons;
  66. prod = intf->in_prod;
  67. mb(); /* get pointers before reading ring */
  68. BUG_ON((prod - cons) > sizeof(intf->in));
  69. while (cons != prod && recv < len)
  70. buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
  71. mb(); /* read ring before consuming */
  72. intf->in_cons = cons;
  73. notify_daemon();
  74. return recv;
  75. }
  76. static struct hv_ops hvc_ops = {
  77. .get_chars = read_console,
  78. .put_chars = write_console,
  79. };
  80. static int __init xen_init(void)
  81. {
  82. struct hvc_struct *hp;
  83. if (!is_running_on_xen())
  84. return 0;
  85. xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
  86. if (xencons_irq < 0)
  87. xencons_irq = 0 /* NO_IRQ */;
  88. hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256);
  89. if (IS_ERR(hp))
  90. return PTR_ERR(hp);
  91. hvc = hp;
  92. return 0;
  93. }
  94. static void __exit xen_fini(void)
  95. {
  96. if (hvc)
  97. hvc_remove(hvc);
  98. }
  99. static int xen_cons_init(void)
  100. {
  101. if (!is_running_on_xen())
  102. return 0;
  103. hvc_instantiate(HVC_COOKIE, 0, &hvc_ops);
  104. return 0;
  105. }
  106. module_init(xen_init);
  107. module_exit(xen_fini);
  108. console_initcall(xen_cons_init);
  109. static void xenboot_write_console(struct console *console, const char *string,
  110. unsigned len)
  111. {
  112. unsigned int linelen, off = 0;
  113. const char *pos;
  114. while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
  115. linelen = pos-string+off;
  116. if (off + linelen > len)
  117. break;
  118. write_console(0, string+off, linelen);
  119. write_console(0, "\r\n", 2);
  120. off += linelen + 1;
  121. }
  122. if (off < len)
  123. write_console(0, string+off, len-off);
  124. }
  125. struct console xenboot_console = {
  126. .name = "xenboot",
  127. .write = xenboot_write_console,
  128. .flags = CON_PRINTBUFFER | CON_BOOT,
  129. };