2 * xen console driver interface to hvc_console.c
4 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
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.
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.
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
21 #include <linux/console.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <linux/irq.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/serial_core.h>
31 #include <asm/xen/hypervisor.h>
34 #include <xen/interface/xen.h>
36 #include <xen/grant_table.h>
38 #include <xen/events.h>
39 #include <xen/interface/io/console.h>
40 #include <xen/interface/sched.h>
41 #include <xen/hvc-console.h>
42 #include <xen/xenbus.h>
44 #include "hvc_console.h"
46 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
49 struct list_head list;
50 struct xenbus_device *xbdev;
51 struct xencons_interface *intf;
53 struct hvc_struct *hvc;
59 static LIST_HEAD(xenconsoles);
60 static DEFINE_SPINLOCK(xencons_lock);
62 /* ------------------------------------------------------------------ */
64 static struct xencons_info *vtermno_to_xencons(int vtermno)
66 struct xencons_info *entry, *n, *ret = NULL;
68 if (list_empty(&xenconsoles))
71 list_for_each_entry_safe(entry, n, &xenconsoles, list) {
72 if (entry->vtermno == vtermno) {
81 static inline int xenbus_devid_to_vtermno(int devid)
83 return devid + HVC_COOKIE;
86 static inline void notify_daemon(struct xencons_info *cons)
88 /* Use evtchn: this is called early, before irq is set up. */
89 notify_remote_via_evtchn(cons->evtchn);
92 static int __write_console(struct xencons_info *xencons,
93 const char *data, int len)
95 XENCONS_RING_IDX cons, prod;
96 struct xencons_interface *intf = xencons->intf;
99 cons = intf->out_cons;
100 prod = intf->out_prod;
101 mb(); /* update queue values before going on */
103 if ((prod - cons) > sizeof(intf->out)) {
104 pr_err_once("xencons: Illegal ring page indices");
108 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
109 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
111 wmb(); /* write ring before updating pointer */
112 intf->out_prod = prod;
115 notify_daemon(xencons);
119 static int domU_write_console(uint32_t vtermno, const char *data, int len)
122 struct xencons_info *cons = vtermno_to_xencons(vtermno);
127 * Make sure the whole buffer is emitted, polling if
128 * necessary. We don't ever want to rely on the hvc daemon
129 * because the most interesting console output is when the
130 * kernel is crippled.
133 int sent = __write_console(cons, data, len);
142 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
148 static int domU_read_console(uint32_t vtermno, char *buf, int len)
150 struct xencons_interface *intf;
151 XENCONS_RING_IDX cons, prod;
153 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
156 intf = xencons->intf;
158 cons = intf->in_cons;
159 prod = intf->in_prod;
160 mb(); /* get pointers before reading ring */
162 if ((prod - cons) > sizeof(intf->in)) {
163 pr_err_once("xencons: Illegal ring page indices");
167 while (cons != prod && recv < len)
168 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
170 mb(); /* read ring before consuming */
171 intf->in_cons = cons;
173 notify_daemon(xencons);
177 static const struct hv_ops domU_hvc_ops = {
178 .get_chars = domU_read_console,
179 .put_chars = domU_write_console,
180 .notifier_add = notifier_add_irq,
181 .notifier_del = notifier_del_irq,
182 .notifier_hangup = notifier_hangup_irq,
185 static int dom0_read_console(uint32_t vtermno, char *buf, int len)
187 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
191 * Either for a dom0 to write to the system console, or a domU with a
192 * debug version of Xen
194 static int dom0_write_console(uint32_t vtermno, const char *str, int len)
196 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
203 static const struct hv_ops dom0_hvc_ops = {
204 .get_chars = dom0_read_console,
205 .put_chars = dom0_write_console,
206 .notifier_add = notifier_add_irq,
207 .notifier_del = notifier_del_irq,
208 .notifier_hangup = notifier_hangup_irq,
211 static int xen_hvm_console_init(void)
216 struct xencons_info *info;
218 if (!xen_hvm_domain())
221 info = vtermno_to_xencons(HVC_COOKIE);
223 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
226 } else if (info->intf != NULL) {
227 /* already configured */
231 * If the toolstack (or the hypervisor) hasn't set these values, the
232 * default value is 0. Even though gfn = 0 and evtchn = 0 are
233 * theoretically correct values, in practice they never are and they
234 * mean that a legacy toolstack hasn't initialized the pv console correctly.
236 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
241 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
245 info->intf = xen_remap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE);
246 if (info->intf == NULL)
248 info->vtermno = HVC_COOKIE;
250 spin_lock(&xencons_lock);
251 list_add_tail(&info->list, &xenconsoles);
252 spin_unlock(&xencons_lock);
260 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
262 info->evtchn = xen_start_info->console.domU.evtchn;
263 /* GFN == MFN for PV guest */
264 info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
265 info->vtermno = vtermno;
267 list_add_tail(&info->list, &xenconsoles);
272 static int xen_pv_console_init(void)
274 struct xencons_info *info;
276 if (!xen_pv_domain())
279 if (!xen_start_info->console.domU.evtchn)
282 info = vtermno_to_xencons(HVC_COOKIE);
284 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
287 } else if (info->intf != NULL) {
288 /* already configured */
291 spin_lock(&xencons_lock);
292 xencons_info_pv_init(info, HVC_COOKIE);
293 spin_unlock(&xencons_lock);
298 static int xen_initial_domain_console_init(void)
300 struct xencons_info *info;
302 if (!xen_initial_domain())
305 info = vtermno_to_xencons(HVC_COOKIE);
307 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
312 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
313 info->vtermno = HVC_COOKIE;
315 spin_lock(&xencons_lock);
316 list_add_tail(&info->list, &xenconsoles);
317 spin_unlock(&xencons_lock);
322 static void xen_console_update_evtchn(struct xencons_info *info)
324 if (xen_hvm_domain()) {
328 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
332 info->evtchn = xen_start_info->console.domU.evtchn;
335 void xen_console_resume(void)
337 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
338 if (info != NULL && info->irq) {
339 if (!xen_initial_domain())
340 xen_console_update_evtchn(info);
341 rebind_evtchn_irq(info->evtchn, info->irq);
345 #ifdef CONFIG_HVC_XEN_FRONTEND
346 static void xencons_disconnect_backend(struct xencons_info *info)
349 unbind_from_irqhandler(info->irq, NULL);
351 if (info->evtchn > 0)
352 xenbus_free_evtchn(info->xbdev, info->evtchn);
354 if (info->gntref > 0)
355 gnttab_free_grant_references(info->gntref);
357 if (info->hvc != NULL)
358 hvc_remove(info->hvc);
362 static void xencons_free(struct xencons_info *info)
364 free_page((unsigned long)info->intf);
370 static int xen_console_remove(struct xencons_info *info)
372 xencons_disconnect_backend(info);
373 spin_lock(&xencons_lock);
374 list_del(&info->list);
375 spin_unlock(&xencons_lock);
376 if (info->xbdev != NULL)
379 if (xen_hvm_domain())
386 static int xencons_remove(struct xenbus_device *dev)
388 return xen_console_remove(dev_get_drvdata(&dev->dev));
391 static int xencons_connect_backend(struct xenbus_device *dev,
392 struct xencons_info *info)
394 int ret, evtchn, devid, ref, irq;
395 struct xenbus_transaction xbt;
396 grant_ref_t gref_head;
398 ret = xenbus_alloc_evtchn(dev, &evtchn);
401 info->evtchn = evtchn;
402 irq = bind_evtchn_to_irq(evtchn);
406 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
407 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
408 irq, &domU_hvc_ops, 256);
409 if (IS_ERR(info->hvc))
410 return PTR_ERR(info->hvc);
411 ret = gnttab_alloc_grant_references(1, &gref_head);
414 info->gntref = gref_head;
415 ref = gnttab_claim_grant_reference(&gref_head);
418 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
419 virt_to_gfn(info->intf), 0);
422 ret = xenbus_transaction_start(&xbt);
424 xenbus_dev_fatal(dev, ret, "starting transaction");
427 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
430 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
434 ret = xenbus_transaction_end(xbt, 0);
438 xenbus_dev_fatal(dev, ret, "completing transaction");
442 xenbus_switch_state(dev, XenbusStateInitialised);
446 xenbus_transaction_end(xbt, 1);
447 xenbus_dev_fatal(dev, ret, "writing xenstore");
451 static int xencons_probe(struct xenbus_device *dev,
452 const struct xenbus_device_id *id)
455 struct xencons_info *info;
457 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
461 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
464 dev_set_drvdata(&dev->dev, info);
466 info->vtermno = xenbus_devid_to_vtermno(devid);
467 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
471 ret = xencons_connect_backend(dev, info);
474 spin_lock(&xencons_lock);
475 list_add_tail(&info->list, &xenconsoles);
476 spin_unlock(&xencons_lock);
482 xenbus_dev_fatal(dev, ret, "allocating device memory");
484 xencons_disconnect_backend(info);
489 static int xencons_resume(struct xenbus_device *dev)
491 struct xencons_info *info = dev_get_drvdata(&dev->dev);
493 xencons_disconnect_backend(info);
494 memset(info->intf, 0, XEN_PAGE_SIZE);
495 return xencons_connect_backend(dev, info);
498 static void xencons_backend_changed(struct xenbus_device *dev,
499 enum xenbus_state backend_state)
501 switch (backend_state) {
502 case XenbusStateReconfiguring:
503 case XenbusStateReconfigured:
504 case XenbusStateInitialising:
505 case XenbusStateInitialised:
506 case XenbusStateUnknown:
509 case XenbusStateInitWait:
512 case XenbusStateConnected:
513 xenbus_switch_state(dev, XenbusStateConnected);
516 case XenbusStateClosed:
517 if (dev->state == XenbusStateClosed)
519 /* Missed the backend's CLOSING state -- fallthrough */
520 case XenbusStateClosing:
521 xenbus_frontend_closed(dev);
526 static const struct xenbus_device_id xencons_ids[] = {
531 static struct xenbus_driver xencons_driver = {
532 .name = "xenconsole",
534 .probe = xencons_probe,
535 .remove = xencons_remove,
536 .resume = xencons_resume,
537 .otherend_changed = xencons_backend_changed,
539 #endif /* CONFIG_HVC_XEN_FRONTEND */
541 static int __init xen_hvc_init(void)
544 struct xencons_info *info;
545 const struct hv_ops *ops;
550 if (xen_initial_domain()) {
552 r = xen_initial_domain_console_init();
555 info = vtermno_to_xencons(HVC_COOKIE);
558 if (xen_hvm_domain())
559 r = xen_hvm_console_init();
561 r = xen_pv_console_init();
565 info = vtermno_to_xencons(HVC_COOKIE);
566 info->irq = bind_evtchn_to_irq(info->evtchn);
569 info->irq = 0; /* NO_IRQ */
571 irq_set_noprobe(info->irq);
573 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
574 if (IS_ERR(info->hvc)) {
575 r = PTR_ERR(info->hvc);
576 spin_lock(&xencons_lock);
577 list_del(&info->list);
578 spin_unlock(&xencons_lock);
580 unbind_from_irqhandler(info->irq, NULL);
586 #ifdef CONFIG_HVC_XEN_FRONTEND
587 r = xenbus_register_frontend(&xencons_driver);
591 device_initcall(xen_hvc_init);
593 static int xen_cons_init(void)
595 const struct hv_ops *ops;
600 if (xen_initial_domain())
606 if (xen_hvm_domain())
607 r = xen_hvm_console_init();
609 r = xen_pv_console_init();
614 hvc_instantiate(HVC_COOKIE, 0, ops);
617 console_initcall(xen_cons_init);
620 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
622 if (xen_cpuid_base())
623 outsb(0xe9, str, len);
626 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
629 #ifdef CONFIG_EARLY_PRINTK
630 static int __init xenboot_setup_console(struct console *console, char *string)
632 static struct xencons_info xenboot;
634 if (xen_initial_domain())
636 if (!xen_pv_domain())
639 return xencons_info_pv_init(&xenboot, 0);
642 static void xenboot_write_console(struct console *console, const char *string,
645 unsigned int linelen, off = 0;
648 if (!xen_pv_domain()) {
649 xen_hvm_early_write(0, string, len);
653 dom0_write_console(0, string, len);
655 if (xen_initial_domain())
658 domU_write_console(0, "(early) ", 8);
659 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
660 linelen = pos-string+off;
661 if (off + linelen > len)
663 domU_write_console(0, string+off, linelen);
664 domU_write_console(0, "\r\n", 2);
668 domU_write_console(0, string+off, len-off);
671 struct console xenboot_console = {
673 .write = xenboot_write_console,
674 .setup = xenboot_setup_console,
675 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
678 #endif /* CONFIG_EARLY_PRINTK */
680 void xen_raw_console_write(const char *str)
682 ssize_t len = strlen(str);
686 rc = dom0_write_console(0, str, len);
687 if (rc != -ENOSYS || !xen_hvm_domain())
690 xen_hvm_early_write(0, str, len);
693 void xen_raw_printk(const char *fmt, ...)
695 static char buf[512];
699 vsnprintf(buf, sizeof(buf), fmt, ap);
702 xen_raw_console_write(buf);
705 static void xenboot_earlycon_write(struct console *console,
709 dom0_write_console(0, string, len);
712 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
715 device->con->write = xenboot_earlycon_write;
718 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);