hvc_xen.c revision 02e19f9c7cacfb33d7b2f5cace7972fa60f92319
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#include <linux/list.h>
27
28#include <asm/io.h>
29#include <asm/xen/hypervisor.h>
30
31#include <xen/xen.h>
32#include <xen/interface/xen.h>
33#include <xen/hvm.h>
34#include <xen/grant_table.h>
35#include <xen/page.h>
36#include <xen/events.h>
37#include <xen/interface/io/console.h>
38#include <xen/hvc-console.h>
39#include <xen/xenbus.h>
40
41#include "hvc_console.h"
42
43#define HVC_COOKIE   0x58656e /* "Xen" in hex */
44
45struct xencons_info {
46	struct list_head list;
47	struct xenbus_device *xbdev;
48	struct xencons_interface *intf;
49	unsigned int evtchn;
50	struct hvc_struct *hvc;
51	int irq;
52	int vtermno;
53	grant_ref_t gntref;
54};
55
56static LIST_HEAD(xenconsoles);
57static DEFINE_SPINLOCK(xencons_lock);
58static struct xenbus_driver xencons_driver;
59
60/* ------------------------------------------------------------------ */
61
62static struct xencons_info *vtermno_to_xencons(int vtermno)
63{
64	struct xencons_info *entry, *n, *ret = NULL;
65
66	if (list_empty(&xenconsoles))
67			return NULL;
68
69	list_for_each_entry_safe(entry, n, &xenconsoles, list) {
70		if (entry->vtermno == vtermno) {
71			ret  = entry;
72			break;
73		}
74	}
75
76	return ret;
77}
78
79static inline int xenbus_devid_to_vtermno(int devid)
80{
81	return devid + HVC_COOKIE;
82}
83
84static inline void notify_daemon(struct xencons_info *cons)
85{
86	/* Use evtchn: this is called early, before irq is set up. */
87	notify_remote_via_evtchn(cons->evtchn);
88}
89
90static int __write_console(struct xencons_info *xencons,
91		const char *data, int len)
92{
93	XENCONS_RING_IDX cons, prod;
94	struct xencons_interface *intf = xencons->intf;
95	int sent = 0;
96
97	cons = intf->out_cons;
98	prod = intf->out_prod;
99	mb();			/* update queue values before going on */
100	BUG_ON((prod - cons) > sizeof(intf->out));
101
102	while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
103		intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
104
105	wmb();			/* write ring before updating pointer */
106	intf->out_prod = prod;
107
108	if (sent)
109		notify_daemon(xencons);
110	return sent;
111}
112
113static int domU_write_console(uint32_t vtermno, const char *data, int len)
114{
115	int ret = len;
116	struct xencons_info *cons = vtermno_to_xencons(vtermno);
117	if (cons == NULL)
118		return -EINVAL;
119
120	/*
121	 * Make sure the whole buffer is emitted, polling if
122	 * necessary.  We don't ever want to rely on the hvc daemon
123	 * because the most interesting console output is when the
124	 * kernel is crippled.
125	 */
126	while (len) {
127		int sent = __write_console(cons, data, len);
128
129		data += sent;
130		len -= sent;
131
132		if (unlikely(len))
133			HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
134	}
135
136	return ret;
137}
138
139static int domU_read_console(uint32_t vtermno, char *buf, int len)
140{
141	struct xencons_interface *intf;
142	XENCONS_RING_IDX cons, prod;
143	int recv = 0;
144	struct xencons_info *xencons = vtermno_to_xencons(vtermno);
145	if (xencons == NULL)
146		return -EINVAL;
147	intf = xencons->intf;
148
149	cons = intf->in_cons;
150	prod = intf->in_prod;
151	mb();			/* get pointers before reading ring */
152	BUG_ON((prod - cons) > sizeof(intf->in));
153
154	while (cons != prod && recv < len)
155		buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
156
157	mb();			/* read ring before consuming */
158	intf->in_cons = cons;
159
160	notify_daemon(xencons);
161	return recv;
162}
163
164static struct hv_ops domU_hvc_ops = {
165	.get_chars = domU_read_console,
166	.put_chars = domU_write_console,
167	.notifier_add = notifier_add_irq,
168	.notifier_del = notifier_del_irq,
169	.notifier_hangup = notifier_hangup_irq,
170};
171
172static int dom0_read_console(uint32_t vtermno, char *buf, int len)
173{
174	return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
175}
176
177/*
178 * Either for a dom0 to write to the system console, or a domU with a
179 * debug version of Xen
180 */
181static int dom0_write_console(uint32_t vtermno, const char *str, int len)
182{
183	int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
184	if (rc < 0)
185		return 0;
186
187	return len;
188}
189
190static struct hv_ops dom0_hvc_ops = {
191	.get_chars = dom0_read_console,
192	.put_chars = dom0_write_console,
193	.notifier_add = notifier_add_irq,
194	.notifier_del = notifier_del_irq,
195	.notifier_hangup = notifier_hangup_irq,
196};
197
198static int xen_hvm_console_init(void)
199{
200	int r;
201	uint64_t v = 0;
202	unsigned long mfn;
203	struct xencons_info *info;
204
205	if (!xen_hvm_domain())
206		return -ENODEV;
207
208	info = vtermno_to_xencons(HVC_COOKIE);
209	if (!info) {
210		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
211		if (!info)
212			return -ENOMEM;
213	}
214
215	/* already configured */
216	if (info->intf != NULL)
217		return 0;
218
219	r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
220	if (r < 0) {
221		kfree(info);
222		return -ENODEV;
223	}
224	info->evtchn = v;
225	hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
226	if (r < 0) {
227		kfree(info);
228		return -ENODEV;
229	}
230	mfn = v;
231	info->intf = ioremap(mfn << PAGE_SHIFT, PAGE_SIZE);
232	if (info->intf == NULL) {
233		kfree(info);
234		return -ENODEV;
235	}
236	info->vtermno = HVC_COOKIE;
237
238	spin_lock(&xencons_lock);
239	list_add_tail(&info->list, &xenconsoles);
240	spin_unlock(&xencons_lock);
241
242	return 0;
243}
244
245static int xen_pv_console_init(void)
246{
247	struct xencons_info *info;
248
249	if (!xen_pv_domain())
250		return -ENODEV;
251
252	if (!xen_start_info->console.domU.evtchn)
253		return -ENODEV;
254
255	info = vtermno_to_xencons(HVC_COOKIE);
256	if (!info) {
257		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
258		if (!info)
259			return -ENOMEM;
260	}
261
262	/* already configured */
263	if (info->intf != NULL)
264		return 0;
265
266	info->evtchn = xen_start_info->console.domU.evtchn;
267	info->intf = mfn_to_virt(xen_start_info->console.domU.mfn);
268	info->vtermno = HVC_COOKIE;
269
270	spin_lock(&xencons_lock);
271	list_add_tail(&info->list, &xenconsoles);
272	spin_unlock(&xencons_lock);
273
274	return 0;
275}
276
277static int xen_initial_domain_console_init(void)
278{
279	struct xencons_info *info;
280
281	if (!xen_initial_domain())
282		return -ENODEV;
283
284	info = vtermno_to_xencons(HVC_COOKIE);
285	if (!info) {
286		info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
287		if (!info)
288			return -ENOMEM;
289	}
290
291	info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0);
292	info->vtermno = HVC_COOKIE;
293
294	spin_lock(&xencons_lock);
295	list_add_tail(&info->list, &xenconsoles);
296	spin_unlock(&xencons_lock);
297
298	return 0;
299}
300
301static int __init xen_hvc_init(void)
302{
303	int r;
304	struct xencons_info *info;
305	const struct hv_ops *ops;
306
307	if (!xen_domain())
308		return -ENODEV;
309
310	if (xen_initial_domain()) {
311		ops = &dom0_hvc_ops;
312		r = xen_initial_domain_console_init();
313		if (r < 0)
314			return r;
315		info = vtermno_to_xencons(HVC_COOKIE);
316	} else {
317		ops = &domU_hvc_ops;
318		if (xen_hvm_domain())
319			r = xen_hvm_console_init();
320		else
321			r = xen_pv_console_init();
322		if (r < 0)
323			return r;
324
325		info = vtermno_to_xencons(HVC_COOKIE);
326		info->irq = bind_evtchn_to_irq(info->evtchn);
327	}
328	if (info->irq < 0)
329		info->irq = 0; /* NO_IRQ */
330	else
331		irq_set_noprobe(info->irq);
332
333	info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
334	if (IS_ERR(info->hvc)) {
335		r = PTR_ERR(info->hvc);
336		spin_lock(&xencons_lock);
337		list_del(&info->list);
338		spin_unlock(&xencons_lock);
339		if (info->irq)
340			unbind_from_irqhandler(info->irq, NULL);
341		kfree(info);
342		return r;
343	}
344
345	return xenbus_register_frontend(&xencons_driver);
346}
347
348void xen_console_resume(void)
349{
350	struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
351	if (info != NULL && info->irq)
352		rebind_evtchn_irq(info->evtchn, info->irq);
353}
354
355static void xencons_disconnect_backend(struct xencons_info *info)
356{
357	if (info->irq > 0)
358		unbind_from_irqhandler(info->irq, NULL);
359	info->irq = 0;
360	if (info->evtchn > 0)
361		xenbus_free_evtchn(info->xbdev, info->evtchn);
362	info->evtchn = 0;
363	if (info->gntref > 0)
364		gnttab_free_grant_references(info->gntref);
365	info->gntref = 0;
366	if (info->hvc != NULL)
367		hvc_remove(info->hvc);
368	info->hvc = NULL;
369}
370
371static void xencons_free(struct xencons_info *info)
372{
373	free_page((unsigned long)info->intf);
374	info->intf = NULL;
375	info->vtermno = 0;
376	kfree(info);
377}
378
379static int xen_console_remove(struct xencons_info *info)
380{
381	xencons_disconnect_backend(info);
382	spin_lock(&xencons_lock);
383	list_del(&info->list);
384	spin_unlock(&xencons_lock);
385	if (info->xbdev != NULL)
386		xencons_free(info);
387	else {
388		if (xen_hvm_domain())
389			iounmap(info->intf);
390		kfree(info);
391	}
392	return 0;
393}
394
395static int xencons_remove(struct xenbus_device *dev)
396{
397	return xen_console_remove(dev_get_drvdata(&dev->dev));
398}
399
400static int xencons_connect_backend(struct xenbus_device *dev,
401				  struct xencons_info *info)
402{
403	int ret, evtchn, devid, ref, irq;
404	struct xenbus_transaction xbt;
405	grant_ref_t gref_head;
406	unsigned long mfn;
407
408	ret = xenbus_alloc_evtchn(dev, &evtchn);
409	if (ret)
410		return ret;
411	info->evtchn = evtchn;
412	irq = bind_evtchn_to_irq(evtchn);
413	if (irq < 0)
414		return irq;
415	info->irq = irq;
416	devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
417	info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
418			irq, &domU_hvc_ops, 256);
419	if (IS_ERR(info->hvc))
420		return PTR_ERR(info->hvc);
421	if (xen_pv_domain())
422		mfn = virt_to_mfn(info->intf);
423	else
424		mfn = __pa(info->intf) >> PAGE_SHIFT;
425	ret = gnttab_alloc_grant_references(1, &gref_head);
426	if (ret < 0)
427		return ret;
428	info->gntref = gref_head;
429	ref = gnttab_claim_grant_reference(&gref_head);
430	if (ref < 0)
431		return ref;
432	gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
433			mfn, 0);
434
435 again:
436	ret = xenbus_transaction_start(&xbt);
437	if (ret) {
438		xenbus_dev_fatal(dev, ret, "starting transaction");
439		return ret;
440	}
441	ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
442	if (ret)
443		goto error_xenbus;
444	ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
445			    evtchn);
446	if (ret)
447		goto error_xenbus;
448	ret = xenbus_printf(xbt, dev->nodename, "type", "ioemu");
449	if (ret)
450		goto error_xenbus;
451	ret = xenbus_transaction_end(xbt, 0);
452	if (ret) {
453		if (ret == -EAGAIN)
454			goto again;
455		xenbus_dev_fatal(dev, ret, "completing transaction");
456		return ret;
457	}
458
459	xenbus_switch_state(dev, XenbusStateInitialised);
460	return 0;
461
462 error_xenbus:
463	xenbus_transaction_end(xbt, 1);
464	xenbus_dev_fatal(dev, ret, "writing xenstore");
465	return ret;
466}
467
468static int __devinit xencons_probe(struct xenbus_device *dev,
469				  const struct xenbus_device_id *id)
470{
471	int ret, devid;
472	struct xencons_info *info;
473
474	devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
475	if (devid == 0)
476		return -ENODEV;
477
478	info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL | __GFP_ZERO);
479	if (!info)
480		goto error_nomem;
481	dev_set_drvdata(&dev->dev, info);
482	info->xbdev = dev;
483	info->vtermno = xenbus_devid_to_vtermno(devid);
484	info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
485	if (!info->intf)
486		goto error_nomem;
487
488	ret = xencons_connect_backend(dev, info);
489	if (ret < 0)
490		goto error;
491	spin_lock(&xencons_lock);
492	list_add_tail(&info->list, &xenconsoles);
493	spin_unlock(&xencons_lock);
494
495	return 0;
496
497 error_nomem:
498	ret = -ENOMEM;
499	xenbus_dev_fatal(dev, ret, "allocating device memory");
500 error:
501	xencons_disconnect_backend(info);
502	xencons_free(info);
503	return ret;
504}
505
506static int xencons_resume(struct xenbus_device *dev)
507{
508	struct xencons_info *info = dev_get_drvdata(&dev->dev);
509
510	xencons_disconnect_backend(info);
511	memset(info->intf, 0, PAGE_SIZE);
512	return xencons_connect_backend(dev, info);
513}
514
515static void xencons_backend_changed(struct xenbus_device *dev,
516				   enum xenbus_state backend_state)
517{
518	switch (backend_state) {
519	case XenbusStateReconfiguring:
520	case XenbusStateReconfigured:
521	case XenbusStateInitialising:
522	case XenbusStateInitialised:
523	case XenbusStateUnknown:
524	case XenbusStateClosed:
525		break;
526
527	case XenbusStateInitWait:
528		break;
529
530	case XenbusStateConnected:
531		xenbus_switch_state(dev, XenbusStateConnected);
532		break;
533
534	case XenbusStateClosing:
535		xenbus_frontend_closed(dev);
536		break;
537	}
538}
539
540static const struct xenbus_device_id xencons_ids[] = {
541	{ "console" },
542	{ "" }
543};
544
545
546static void __exit xen_hvc_fini(void)
547{
548	struct xencons_info *entry, *next;
549
550	if (list_empty(&xenconsoles))
551			return;
552
553	list_for_each_entry_safe(entry, next, &xenconsoles, list) {
554		xen_console_remove(entry);
555	}
556}
557
558static int xen_cons_init(void)
559{
560	const struct hv_ops *ops;
561
562	if (!xen_domain())
563		return 0;
564
565	if (xen_initial_domain())
566		ops = &dom0_hvc_ops;
567	else {
568		int r;
569		ops = &domU_hvc_ops;
570
571		if (xen_hvm_domain())
572			r = xen_hvm_console_init();
573		else
574			r = xen_pv_console_init();
575		if (r < 0)
576			return r;
577	}
578
579	hvc_instantiate(HVC_COOKIE, 0, ops);
580	return 0;
581}
582
583static DEFINE_XENBUS_DRIVER(xencons, "xenconsole",
584	.probe = xencons_probe,
585	.remove = xencons_remove,
586	.resume = xencons_resume,
587	.otherend_changed = xencons_backend_changed,
588);
589
590module_init(xen_hvc_init);
591module_exit(xen_hvc_fini);
592console_initcall(xen_cons_init);
593
594#ifdef CONFIG_EARLY_PRINTK
595static void xenboot_write_console(struct console *console, const char *string,
596				  unsigned len)
597{
598	unsigned int linelen, off = 0;
599	const char *pos;
600
601	if (!xen_pv_domain())
602		return;
603
604	dom0_write_console(0, string, len);
605
606	if (xen_initial_domain())
607		return;
608
609	domU_write_console(0, "(early) ", 8);
610	while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
611		linelen = pos-string+off;
612		if (off + linelen > len)
613			break;
614		domU_write_console(0, string+off, linelen);
615		domU_write_console(0, "\r\n", 2);
616		off += linelen + 1;
617	}
618	if (off < len)
619		domU_write_console(0, string+off, len-off);
620}
621
622struct console xenboot_console = {
623	.name		= "xenboot",
624	.write		= xenboot_write_console,
625	.flags		= CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
626};
627#endif	/* CONFIG_EARLY_PRINTK */
628
629void xen_raw_console_write(const char *str)
630{
631	dom0_write_console(0, str, strlen(str));
632}
633
634void xen_raw_printk(const char *fmt, ...)
635{
636	static char buf[512];
637	va_list ap;
638
639	va_start(ap, fmt);
640	vsnprintf(buf, sizeof(buf), fmt, ap);
641	va_end(ap);
642
643	xen_raw_console_write(buf);
644}
645