blob: 93d33816c9d9d96378041252e50810fc2d3d115b [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
60static int write_console(uint32_t vtermno, const char *data, int len)
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
81static int read_console(uint32_t vtermno, char *buf, int len)
82{
83 struct xencons_interface *intf = xencons_interface();
84 XENCONS_RING_IDX cons, prod;
85 int recv = 0;
86
87 cons = intf->in_cons;
88 prod = intf->in_prod;
89 mb(); /* get pointers before reading ring */
90 BUG_ON((prod - cons) > sizeof(intf->in));
91
92 while (cons != prod && recv < len)
93 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
94
95 mb(); /* read ring before consuming */
96 intf->in_cons = cons;
97
98 notify_daemon();
99 return recv;
100}
101
102static struct hv_ops hvc_ops = {
103 .get_chars = read_console,
104 .put_chars = write_console,
Christian Borntraeger611e0972008-06-20 15:24:08 +0200105 .notifier_add = notifier_add_irq,
106 .notifier_del = notifier_del_irq,
Hendrik Bruecknerfc362e22008-10-13 23:12:48 +0000107 .notifier_hangup = notifier_hangup_irq,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700108};
109
110static int __init xen_init(void)
111{
112 struct hvc_struct *hp;
113
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700114 if (!xen_pv_domain() ||
115 xen_initial_domain() ||
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100116 !xen_start_info->console.domU.evtchn)
117 return -ENODEV;
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700118
119 xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
120 if (xencons_irq < 0)
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100121 xencons_irq = 0; /* NO_IRQ */
122
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700123 hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256);
124 if (IS_ERR(hp))
125 return PTR_ERR(hp);
126
127 hvc = hp;
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100128
129 console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
130
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700131 return 0;
132}
133
Jeremy Fitzhardinge6b9b7322008-05-26 23:31:25 +0100134void xen_console_resume(void)
135{
136 if (xencons_irq)
137 rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
138}
139
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700140static void __exit xen_fini(void)
141{
142 if (hvc)
143 hvc_remove(hvc);
144}
145
146static int xen_cons_init(void)
147{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -0700148 if (!xen_pv_domain())
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700149 return 0;
150
151 hvc_instantiate(HVC_COOKIE, 0, &hvc_ops);
152 return 0;
153}
154
155module_init(xen_init);
156module_exit(xen_fini);
157console_initcall(xen_cons_init);
158
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100159static void raw_console_write(const char *str, int len)
160{
161 while(len > 0) {
162 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
163 if (rc <= 0)
164 break;
165
166 str += rc;
167 len -= rc;
168 }
169}
170
171#ifdef CONFIG_EARLY_PRINTK
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700172static void xenboot_write_console(struct console *console, const char *string,
173 unsigned len)
174{
175 unsigned int linelen, off = 0;
176 const char *pos;
177
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100178 raw_console_write(string, len);
179
Jeremy Fitzhardinge83abc702008-05-26 23:31:12 +0100180 write_console(0, "(early) ", 8);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700181 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
182 linelen = pos-string+off;
183 if (off + linelen > len)
184 break;
185 write_console(0, string+off, linelen);
186 write_console(0, "\r\n", 2);
187 off += linelen + 1;
188 }
189 if (off < len)
190 write_console(0, string+off, len-off);
191}
192
193struct console xenboot_console = {
194 .name = "xenboot",
195 .write = xenboot_write_console,
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100196 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700197};
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100198#endif /* CONFIG_EARLY_PRINTK */
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100199
200void xen_raw_console_write(const char *str)
201{
Jeremy Fitzhardinge0922abd2008-05-26 23:31:00 +0100202 raw_console_write(str, strlen(str));
Jeremy Fitzhardinge0acf10d2008-05-26 23:30:59 +0100203}
204
205void xen_raw_printk(const char *fmt, ...)
206{
207 static char buf[512];
208 va_list ap;
209
210 va_start(ap, fmt);
211 vsnprintf(buf, sizeof(buf), fmt, ap);
212 va_end(ap);
213
214 xen_raw_console_write(buf);
215}