hvc_xen.c 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * xen console driver interface to hvc_console.c
 *
 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/console.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/types.h>

#include <asm/xen/hypervisor.h>
28 29

#include <xen/xen.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <xen/page.h>
#include <xen/events.h>
#include <xen/interface/io/console.h>
#include <xen/hvc-console.h>

#include "hvc_console.h"

#define HVC_COOKIE   0x58656e /* "Xen" in hex */

static struct hvc_struct *hvc;
static int xencons_irq;

/* ------------------------------------------------------------------ */

44 45
static unsigned long console_pfn = ~0ul;

46 47
static inline struct xencons_interface *xencons_interface(void)
{
48 49 50 51
	if (console_pfn == ~0ul)
		return mfn_to_virt(xen_start_info->console.domU.mfn);
	else
		return __va(console_pfn << PAGE_SHIFT);
52 53 54 55 56 57 58 59
}

static inline void notify_daemon(void)
{
	/* Use evtchn: this is called early, before irq is set up. */
	notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
}

60
static int __write_console(const char *data, int len)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
	struct xencons_interface *intf = xencons_interface();
	XENCONS_RING_IDX cons, prod;
	int sent = 0;

	cons = intf->out_cons;
	prod = intf->out_prod;
	mb();			/* update queue values before going on */
	BUG_ON((prod - cons) > sizeof(intf->out));

	while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
		intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];

	wmb();			/* write ring before updating pointer */
	intf->out_prod = prod;

77 78
	if (sent)
		notify_daemon();
79 80 81
	return sent;
}

82
static int domU_write_console(uint32_t vtermno, const char *data, int len)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	int ret = len;

	/*
	 * Make sure the whole buffer is emitted, polling if
	 * necessary.  We don't ever want to rely on the hvc daemon
	 * because the most interesting console output is when the
	 * kernel is crippled.
	 */
	while (len) {
		int sent = __write_console(data, len);
		
		data += sent;
		len -= sent;

		if (unlikely(len))
			HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
	}

	return ret;
}

105
static int domU_read_console(uint32_t vtermno, char *buf, int len)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
	struct xencons_interface *intf = xencons_interface();
	XENCONS_RING_IDX cons, prod;
	int recv = 0;

	cons = intf->in_cons;
	prod = intf->in_prod;
	mb();			/* get pointers before reading ring */
	BUG_ON((prod - cons) > sizeof(intf->in));

	while (cons != prod && recv < len)
		buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];

	mb();			/* read ring before consuming */
	intf->in_cons = cons;

	notify_daemon();
	return recv;
}

126 127 128
static struct hv_ops domU_hvc_ops = {
	.get_chars = domU_read_console,
	.put_chars = domU_write_console,
129 130
	.notifier_add = notifier_add_irq,
	.notifier_del = notifier_del_irq,
131
	.notifier_hangup = notifier_hangup_irq,
132 133
};

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static int dom0_read_console(uint32_t vtermno, char *buf, int len)
{
	return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
}

/*
 * Either for a dom0 to write to the system console, or a domU with a
 * debug version of Xen
 */
static int dom0_write_console(uint32_t vtermno, const char *str, int len)
{
	int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
	if (rc < 0)
		return 0;

	return len;
}

static struct hv_ops dom0_hvc_ops = {
	.get_chars = dom0_read_console,
	.put_chars = dom0_write_console,
	.notifier_add = notifier_add_irq,
	.notifier_del = notifier_del_irq,
	.notifier_hangup = notifier_hangup_irq,
};

static int __init xen_hvc_init(void)
161 162
{
	struct hvc_struct *hp;
163
	struct hv_ops *ops;
164

165
	if (!xen_pv_domain())
166
		return -ENODEV;
167

168 169 170 171 172 173 174 175 176 177
	if (xen_initial_domain()) {
		ops = &dom0_hvc_ops;
		xencons_irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
	} else {
		if (!xen_start_info->console.domU.evtchn)
			return -ENODEV;

		ops = &domU_hvc_ops;
		xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
	}
178
	if (xencons_irq < 0)
179
		xencons_irq = 0; /* NO_IRQ */
180
	else
181
		irq_set_noprobe(xencons_irq);
182

183
	hp = hvc_alloc(HVC_COOKIE, xencons_irq, ops, 256);
184 185 186 187
	if (IS_ERR(hp))
		return PTR_ERR(hp);

	hvc = hp;
188 189 190

	console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);

191 192 193
	return 0;
}

194 195 196 197 198 199
void xen_console_resume(void)
{
	if (xencons_irq)
		rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
}

200
static void __exit xen_hvc_fini(void)
201 202 203 204 205 206 207
{
	if (hvc)
		hvc_remove(hvc);
}

static int xen_cons_init(void)
{
208 209
	struct hv_ops *ops;

210
	if (!xen_pv_domain())
211 212
		return 0;

213 214 215 216 217 218
	if (xen_initial_domain())
		ops = &dom0_hvc_ops;
	else
		ops = &domU_hvc_ops;

	hvc_instantiate(HVC_COOKIE, 0, ops);
219 220 221
	return 0;
}

222 223
module_init(xen_hvc_init);
module_exit(xen_hvc_fini);
224 225
console_initcall(xen_cons_init);

226
#ifdef CONFIG_EARLY_PRINTK
227 228 229 230 231 232
static void xenboot_write_console(struct console *console, const char *string,
				  unsigned len)
{
	unsigned int linelen, off = 0;
	const char *pos;

233 234 235 236
	dom0_write_console(0, string, len);

	if (xen_initial_domain())
		return;
237

238
	domU_write_console(0, "(early) ", 8);
239 240 241 242
	while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
		linelen = pos-string+off;
		if (off + linelen > len)
			break;
243 244
		domU_write_console(0, string+off, linelen);
		domU_write_console(0, "\r\n", 2);
245 246 247
		off += linelen + 1;
	}
	if (off < len)
248
		domU_write_console(0, string+off, len-off);
249 250 251 252 253
}

struct console xenboot_console = {
	.name		= "xenboot",
	.write		= xenboot_write_console,
254
	.flags		= CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
255
};
256
#endif	/* CONFIG_EARLY_PRINTK */
257 258 259

void xen_raw_console_write(const char *str)
{
260
	dom0_write_console(0, str, strlen(str));
261 262 263 264 265 266 267 268 269 270 271 272 273
}

void xen_raw_printk(const char *fmt, ...)
{
	static char buf[512];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	xen_raw_console_write(buf);
}