blob: 1f7e13a43a880f24cc88085cc766abf62e9384ed [file] [log] [blame]
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -07001/*
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
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/types.h>
26
27#include <asm/xen/hypervisor.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070028
29#include <xen/xen.h>
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070030#include <xen/page.h>
31#include <xen/events.h>
32#include <xen/interface/io/console.h>
33#include <xen/hvc-console.h>
34
35#include "hvc_console.h"
36
37#define HVC_COOKIE 0x58656e /* "Xen" in hex */
38
39static struct hvc_struct *hvc;
40static int xencons_irq;
41
42/* ------------------------------------------------------------------ */
43
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +010044static unsigned long console_pfn = ~0ul;
45
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070046static inline struct xencons_interface *xencons_interface(void)
47{
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +010048 if (console_pfn == ~0ul)
49 return mfn_to_virt(xen_start_info->console.domU.mfn);
50 else
51 return __va(console_pfn << PAGE_SHIFT);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070052}
53
54static inline void notify_daemon(void)
55{
56 /* Use evtchn: this is called early, before irq is set up. */
57 notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
58}
59
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +090060static int __write_console(const char *data, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -070061{
62 struct xencons_interface *intf = xencons_interface();
63 XENCONS_RING_IDX cons, prod;
64 int sent = 0;
65
66 cons = intf->out_cons;
67 prod = intf->out_prod;
68 mb(); /* update queue values before going on */
69 BUG_ON((prod - cons) > sizeof(intf->out));
70
71 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
72 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
73
74 wmb(); /* write ring before updating pointer */
75 intf->out_prod = prod;
76
77 notify_daemon();
78 return sent;
79}
80
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +010081static int domU_write_console(uint32_t vtermno, const char *data, int len)
Jeremy Fitzhardinge7825cf12009-10-20 15:28:21 +090082{
83 int ret = len;
84
85 /*
86 * Make sure the whole buffer is emitted, polling if
87 * necessary. We don't ever want to rely on the hvc daemon
88 * because the most interesting console output is when the
89 * kernel is crippled.
90 */
91 while (len) {
92 int sent = __write_console(data, len);
93
94 data += sent;
95 len -= sent;
96
97 if (unlikely(len))
98 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
99 }
100
101 return ret;
102}
103
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100104static int domU_read_console(uint32_t vtermno, char *buf, int len)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700105{
106 struct xencons_interface *intf = xencons_interface();
107 XENCONS_RING_IDX cons, prod;
108 int recv = 0;
109
110 cons = intf->in_cons;
111 prod = intf->in_prod;
112 mb(); /* get pointers before reading ring */
113 BUG_ON((prod - cons) > sizeof(intf->in));
114
115 while (cons != prod && recv < len)
116 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
117
118 mb(); /* read ring before consuming */
119 intf->in_cons = cons;
120
121 notify_daemon();
122 return recv;
123}
124
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100125static struct hv_ops domU_hvc_ops = {
126 .get_chars = domU_read_console,
127 .put_chars = domU_write_console,
Christian Borntraeger611e0972008-06-20 15:24:08 +0200128 .notifier_add = notifier_add_irq,
129 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000130 .notifier_hangup = notifier_hangup_irq,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700131};
132
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100133static int dom0_read_console(uint32_t vtermno, char *buf, int len)
134{
135 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
136}
137
138/*
139 * Either for a dom0 to write to the system console, or a domU with a
140 * debug version of Xen
141 */
142static int dom0_write_console(uint32_t vtermno, const char *str, int len)
143{
144 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
145 if (rc < 0)
146 return 0;
147
148 return len;
149}
150
151static struct hv_ops dom0_hvc_ops = {
152 .get_chars = dom0_read_console,
153 .put_chars = dom0_write_console,
154 .notifier_add = notifier_add_irq,
155 .notifier_del = notifier_del_irq,
156 .notifier_hangup = notifier_hangup_irq,
157};
158
159static int __init xen_hvc_init(void)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700160{
161 struct hvc_struct *hp;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100162 struct hv_ops *ops;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700163
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100164 if (!xen_pv_domain())
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100165 return -ENODEV;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700166
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100167 if (xen_initial_domain()) {
168 ops = &dom0_hvc_ops;
169 xencons_irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
170 } else {
171 if (!xen_start_info->console.domU.evtchn)
172 return -ENODEV;
173
174 ops = &domU_hvc_ops;
175 xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
176 }
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700177 if (xencons_irq < 0)
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100178 xencons_irq = 0; /* NO_IRQ */
179
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100180 hp = hvc_alloc(HVC_COOKIE, xencons_irq, ops, 256);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700181 if (IS_ERR(hp))
182 return PTR_ERR(hp);
183
184 hvc = hp;
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100185
186 console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
187
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700188 return 0;
189}
190
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100191void xen_console_resume(void)
192{
193 if (xencons_irq)
194 rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
195}
196
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100197static void __exit xen_hvc_fini(void)
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700198{
199 if (hvc)
200 hvc_remove(hvc);
201}
202
203static int xen_cons_init(void)
204{
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100205 struct hv_ops *ops;
206
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700207 if (!xen_pv_domain())
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700208 return 0;
209
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100210 if (xen_initial_domain())
211 ops = &dom0_hvc_ops;
212 else
213 ops = &domU_hvc_ops;
214
215 hvc_instantiate(HVC_COOKIE, 0, ops);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700216 return 0;
217}
218
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100219module_init(xen_hvc_init);
220module_exit(xen_hvc_fini);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700221console_initcall(xen_cons_init);
222
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100223#ifdef CONFIG_EARLY_PRINTK
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700224static void xenboot_write_console(struct console *console, const char *string,
225 unsigned len)
226{
227 unsigned int linelen, off = 0;
228 const char *pos;
229
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100230 dom0_write_console(0, string, len);
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100231
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100232 if (xen_initial_domain())
233 return;
234
235 domU_write_console(0, "(early) ", 8);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700236 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
237 linelen = pos-string+off;
238 if (off + linelen > len)
239 break;
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100240 domU_write_console(0, string+off, linelen);
241 domU_write_console(0, "\r\n", 2);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700242 off += linelen + 1;
243 }
244 if (off < len)
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100245 domU_write_console(0, string+off, len-off);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700246}
247
248struct console xenboot_console = {
249 .name = "xenboot",
250 .write = xenboot_write_console,
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100251 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700252};
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100253#endif /* CONFIG_EARLY_PRINTK */
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100254
255void xen_raw_console_write(const char *str)
256{
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100257 dom0_write_console(0, str, strlen(str));
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100258}
259
260void xen_raw_printk(const char *fmt, ...)
261{
262 static char buf[512];
263 va_list ap;
264
265 va_start(ap, fmt);
266 vsnprintf(buf, sizeof(buf), fmt, ap);
267 va_end(ap);
268
269 xen_raw_console_write(buf);
270}