hvc_xen.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 unsigned long console_pfn = ~0ul;
  36. static inline struct xencons_interface *xencons_interface(void)
  37. {
  38. if (console_pfn == ~0ul)
  39. return mfn_to_virt(xen_start_info->console.domU.mfn);
  40. else
  41. return __va(console_pfn << PAGE_SHIFT);
  42. }
  43. static inline void notify_daemon(void)
  44. {
  45. /* Use evtchn: this is called early, before irq is set up. */
  46. notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
  47. }
  48. static int write_console(uint32_t vtermno, const char *data, int len)
  49. {
  50. struct xencons_interface *intf = xencons_interface();
  51. XENCONS_RING_IDX cons, prod;
  52. int sent = 0;
  53. cons = intf->out_cons;
  54. prod = intf->out_prod;
  55. mb(); /* update queue values before going on */
  56. BUG_ON((prod - cons) > sizeof(intf->out));
  57. while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
  58. intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
  59. wmb(); /* write ring before updating pointer */
  60. intf->out_prod = prod;
  61. notify_daemon();
  62. return sent;
  63. }
  64. static int read_console(uint32_t vtermno, char *buf, int len)
  65. {
  66. struct xencons_interface *intf = xencons_interface();
  67. XENCONS_RING_IDX cons, prod;
  68. int recv = 0;
  69. cons = intf->in_cons;
  70. prod = intf->in_prod;
  71. mb(); /* get pointers before reading ring */
  72. BUG_ON((prod - cons) > sizeof(intf->in));
  73. while (cons != prod && recv < len)
  74. buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
  75. mb(); /* read ring before consuming */
  76. intf->in_cons = cons;
  77. notify_daemon();
  78. return recv;
  79. }
  80. static struct hv_ops hvc_ops = {
  81. .get_chars = read_console,
  82. .put_chars = write_console,
  83. .notifier_add = notifier_add_irq,
  84. .notifier_del = notifier_del_irq,
  85. .notifier_hangup = notifier_hangup_irq,
  86. };
  87. static int __init xen_init(void)
  88. {
  89. struct hvc_struct *hp;
  90. if (!xen_pv_domain() ||
  91. xen_initial_domain() ||
  92. !xen_start_info->console.domU.evtchn)
  93. return -ENODEV;
  94. xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
  95. if (xencons_irq < 0)
  96. xencons_irq = 0; /* NO_IRQ */
  97. hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256);
  98. if (IS_ERR(hp))
  99. return PTR_ERR(hp);
  100. hvc = hp;
  101. console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
  102. return 0;
  103. }
  104. void xen_console_resume(void)
  105. {
  106. if (xencons_irq)
  107. rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
  108. }
  109. static void __exit xen_fini(void)
  110. {
  111. if (hvc)
  112. hvc_remove(hvc);
  113. }
  114. static int xen_cons_init(void)
  115. {
  116. if (!xen_pv_domain())
  117. return 0;
  118. hvc_instantiate(HVC_COOKIE, 0, &hvc_ops);
  119. return 0;
  120. }
  121. module_init(xen_init);
  122. module_exit(xen_fini);
  123. console_initcall(xen_cons_init);
  124. static void raw_console_write(const char *str, int len)
  125. {
  126. while(len > 0) {
  127. int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
  128. if (rc <= 0)
  129. break;
  130. str += rc;
  131. len -= rc;
  132. }
  133. }
  134. #ifdef CONFIG_EARLY_PRINTK
  135. static void xenboot_write_console(struct console *console, const char *string,
  136. unsigned len)
  137. {
  138. unsigned int linelen, off = 0;
  139. const char *pos;
  140. raw_console_write(string, len);
  141. write_console(0, "(early) ", 8);
  142. while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
  143. linelen = pos-string+off;
  144. if (off + linelen > len)
  145. break;
  146. write_console(0, string+off, linelen);
  147. write_console(0, "\r\n", 2);
  148. off += linelen + 1;
  149. }
  150. if (off < len)
  151. write_console(0, string+off, len-off);
  152. }
  153. struct console xenboot_console = {
  154. .name = "xenboot",
  155. .write = xenboot_write_console,
  156. .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
  157. };
  158. #endif /* CONFIG_EARLY_PRINTK */
  159. void xen_raw_console_write(const char *str)
  160. {
  161. raw_console_write(str, strlen(str));
  162. }
  163. void xen_raw_printk(const char *fmt, ...)
  164. {
  165. static char buf[512];
  166. va_list ap;
  167. va_start(ap, fmt);
  168. vsnprintf(buf, sizeof(buf), fmt, ap);
  169. va_end(ap);
  170. xen_raw_console_write(buf);
  171. }