Commit | Line | Data |
---|---|---|
b536b4b9 JF |
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 | ||
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> | |
1ccbf534 JF |
28 | |
29 | #include <xen/xen.h> | |
b536b4b9 JF |
30 | #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 | ||
39 | static struct hvc_struct *hvc; | |
40 | static int xencons_irq; | |
41 | ||
42 | /* ------------------------------------------------------------------ */ | |
43 | ||
6b9b732d JF |
44 | static unsigned long console_pfn = ~0ul; |
45 | ||
b536b4b9 JF |
46 | static inline struct xencons_interface *xencons_interface(void) |
47 | { | |
6b9b732d JF |
48 | if (console_pfn == ~0ul) |
49 | return mfn_to_virt(xen_start_info->console.domU.mfn); | |
50 | else | |
51 | return __va(console_pfn << PAGE_SHIFT); | |
b536b4b9 JF |
52 | } |
53 | ||
54 | static 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 | ||
7825cf10 | 60 | static int __write_console(const char *data, int len) |
b536b4b9 JF |
61 | { |
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 | ||
7825cf10 JF |
81 | static int write_console(uint32_t vtermno, const char *data, int len) |
82 | { | |
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 | ||
b536b4b9 JF |
104 | static int read_console(uint32_t vtermno, char *buf, int len) |
105 | { | |
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 | ||
1dff3996 | 125 | static const struct hv_ops hvc_ops = { |
b536b4b9 JF |
126 | .get_chars = read_console, |
127 | .put_chars = write_console, | |
611e097d CB |
128 | .notifier_add = notifier_add_irq, |
129 | .notifier_del = notifier_del_irq, | |
fc362e2e | 130 | .notifier_hangup = notifier_hangup_irq, |
b536b4b9 JF |
131 | }; |
132 | ||
133 | static int __init xen_init(void) | |
134 | { | |
135 | struct hvc_struct *hp; | |
136 | ||
6e833587 JF |
137 | if (!xen_pv_domain() || |
138 | xen_initial_domain() || | |
6b9b732d JF |
139 | !xen_start_info->console.domU.evtchn) |
140 | return -ENODEV; | |
b536b4b9 JF |
141 | |
142 | xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn); | |
143 | if (xencons_irq < 0) | |
6b9b732d JF |
144 | xencons_irq = 0; /* NO_IRQ */ |
145 | ||
b536b4b9 JF |
146 | hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256); |
147 | if (IS_ERR(hp)) | |
148 | return PTR_ERR(hp); | |
149 | ||
150 | hvc = hp; | |
6b9b732d JF |
151 | |
152 | console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn); | |
153 | ||
b536b4b9 JF |
154 | return 0; |
155 | } | |
156 | ||
6b9b732d JF |
157 | void xen_console_resume(void) |
158 | { | |
159 | if (xencons_irq) | |
160 | rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq); | |
161 | } | |
162 | ||
b536b4b9 JF |
163 | static void __exit xen_fini(void) |
164 | { | |
165 | if (hvc) | |
166 | hvc_remove(hvc); | |
167 | } | |
168 | ||
169 | static int xen_cons_init(void) | |
170 | { | |
6e833587 | 171 | if (!xen_pv_domain()) |
b536b4b9 JF |
172 | return 0; |
173 | ||
174 | hvc_instantiate(HVC_COOKIE, 0, &hvc_ops); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | module_init(xen_init); | |
179 | module_exit(xen_fini); | |
180 | console_initcall(xen_cons_init); | |
181 | ||
0922abdc JF |
182 | static void raw_console_write(const char *str, int len) |
183 | { | |
184 | while(len > 0) { | |
185 | int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str); | |
186 | if (rc <= 0) | |
187 | break; | |
188 | ||
189 | str += rc; | |
190 | len -= rc; | |
191 | } | |
192 | } | |
193 | ||
194 | #ifdef CONFIG_EARLY_PRINTK | |
b536b4b9 JF |
195 | static void xenboot_write_console(struct console *console, const char *string, |
196 | unsigned len) | |
197 | { | |
198 | unsigned int linelen, off = 0; | |
199 | const char *pos; | |
200 | ||
0922abdc JF |
201 | raw_console_write(string, len); |
202 | ||
83abc70a | 203 | write_console(0, "(early) ", 8); |
b536b4b9 JF |
204 | while (off < len && NULL != (pos = strchr(string+off, '\n'))) { |
205 | linelen = pos-string+off; | |
206 | if (off + linelen > len) | |
207 | break; | |
208 | write_console(0, string+off, linelen); | |
209 | write_console(0, "\r\n", 2); | |
210 | off += linelen + 1; | |
211 | } | |
212 | if (off < len) | |
213 | write_console(0, string+off, len-off); | |
214 | } | |
215 | ||
216 | struct console xenboot_console = { | |
217 | .name = "xenboot", | |
218 | .write = xenboot_write_console, | |
0922abdc | 219 | .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME, |
b536b4b9 | 220 | }; |
0922abdc | 221 | #endif /* CONFIG_EARLY_PRINTK */ |
0acf10d8 JF |
222 | |
223 | void xen_raw_console_write(const char *str) | |
224 | { | |
0922abdc | 225 | raw_console_write(str, strlen(str)); |
0acf10d8 JF |
226 | } |
227 | ||
228 | void xen_raw_printk(const char *fmt, ...) | |
229 | { | |
230 | static char buf[512]; | |
231 | va_list ap; | |
232 | ||
233 | va_start(ap, fmt); | |
234 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
235 | va_end(ap); | |
236 | ||
237 | xen_raw_console_write(buf); | |
238 | } |