1 // SPDX-License-Identifier: GPL-2.0+
3 * xen console driver interface to hvc_console.c
5 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/console.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/irq.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/list.h>
29 #include <linux/serial_core.h>
32 #include <asm/xen/hypervisor.h>
35 #include <xen/interface/xen.h>
37 #include <xen/grant_table.h>
39 #include <xen/events.h>
40 #include <xen/interface/io/console.h>
41 #include <xen/interface/sched.h>
42 #include <xen/hvc-console.h>
43 #include <xen/xenbus.h>
45 #include "hvc_console.h"
47 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
50 struct list_head list;
51 struct xenbus_device *xbdev;
52 struct xencons_interface *intf;
54 struct hvc_struct *hvc;
60 static LIST_HEAD(xenconsoles);
61 static DEFINE_SPINLOCK(xencons_lock);
63 /* ------------------------------------------------------------------ */
65 static struct xencons_info *vtermno_to_xencons(int vtermno)
67 struct xencons_info *entry, *n, *ret = NULL;
69 if (list_empty(&xenconsoles))
72 list_for_each_entry_safe(entry, n, &xenconsoles, list) {
73 if (entry->vtermno == vtermno) {
82 static inline int xenbus_devid_to_vtermno(int devid)
84 return devid + HVC_COOKIE;
87 static inline void notify_daemon(struct xencons_info *cons)
89 /* Use evtchn: this is called early, before irq is set up. */
90 notify_remote_via_evtchn(cons->evtchn);
93 static int __write_console(struct xencons_info *xencons,
94 const char *data, int len)
96 XENCONS_RING_IDX cons, prod;
97 struct xencons_interface *intf = xencons->intf;
100 cons = intf->out_cons;
101 prod = intf->out_prod;
102 mb(); /* update queue values before going on */
103 BUG_ON((prod - cons) > sizeof(intf->out));
105 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
106 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
108 wmb(); /* write ring before updating pointer */
109 intf->out_prod = prod;
112 notify_daemon(xencons);
116 static int domU_write_console(uint32_t vtermno, const char *data, int len)
119 struct xencons_info *cons = vtermno_to_xencons(vtermno);
124 * Make sure the whole buffer is emitted, polling if
125 * necessary. We don't ever want to rely on the hvc daemon
126 * because the most interesting console output is when the
127 * kernel is crippled.
130 int sent = __write_console(cons, data, len);
136 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
142 static int domU_read_console(uint32_t vtermno, char *buf, int len)
144 struct xencons_interface *intf;
145 XENCONS_RING_IDX cons, prod;
147 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
150 intf = xencons->intf;
152 cons = intf->in_cons;
153 prod = intf->in_prod;
154 mb(); /* get pointers before reading ring */
155 BUG_ON((prod - cons) > sizeof(intf->in));
157 while (cons != prod && recv < len)
158 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
160 mb(); /* read ring before consuming */
161 intf->in_cons = cons;
163 notify_daemon(xencons);
167 static const struct hv_ops domU_hvc_ops = {
168 .get_chars = domU_read_console,
169 .put_chars = domU_write_console,
170 .notifier_add = notifier_add_irq,
171 .notifier_del = notifier_del_irq,
172 .notifier_hangup = notifier_hangup_irq,
175 static int dom0_read_console(uint32_t vtermno, char *buf, int len)
177 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
181 * Either for a dom0 to write to the system console, or a domU with a
182 * debug version of Xen
184 static int dom0_write_console(uint32_t vtermno, const char *str, int len)
186 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
193 static const struct hv_ops dom0_hvc_ops = {
194 .get_chars = dom0_read_console,
195 .put_chars = dom0_write_console,
196 .notifier_add = notifier_add_irq,
197 .notifier_del = notifier_del_irq,
198 .notifier_hangup = notifier_hangup_irq,
201 static int xen_hvm_console_init(void)
206 struct xencons_info *info;
208 if (!xen_hvm_domain())
211 info = vtermno_to_xencons(HVC_COOKIE);
213 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
216 } else if (info->intf != NULL) {
217 /* already configured */
221 * If the toolstack (or the hypervisor) hasn't set these values, the
222 * default value is 0. Even though gfn = 0 and evtchn = 0 are
223 * theoretically correct values, in practice they never are and they
224 * mean that a legacy toolstack hasn't initialized the pv console correctly.
226 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
231 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
235 info->intf = xen_remap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE);
236 if (info->intf == NULL)
238 info->vtermno = HVC_COOKIE;
240 spin_lock(&xencons_lock);
241 list_add_tail(&info->list, &xenconsoles);
242 spin_unlock(&xencons_lock);
250 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
252 info->evtchn = xen_start_info->console.domU.evtchn;
253 /* GFN == MFN for PV guest */
254 info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
255 info->vtermno = vtermno;
257 list_add_tail(&info->list, &xenconsoles);
262 static int xen_pv_console_init(void)
264 struct xencons_info *info;
266 if (!xen_pv_domain())
269 if (!xen_start_info->console.domU.evtchn)
272 info = vtermno_to_xencons(HVC_COOKIE);
274 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
277 } else if (info->intf != NULL) {
278 /* already configured */
281 spin_lock(&xencons_lock);
282 xencons_info_pv_init(info, HVC_COOKIE);
283 spin_unlock(&xencons_lock);
288 static int xen_initial_domain_console_init(void)
290 struct xencons_info *info;
292 if (!xen_initial_domain())
295 info = vtermno_to_xencons(HVC_COOKIE);
297 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
302 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
303 info->vtermno = HVC_COOKIE;
305 spin_lock(&xencons_lock);
306 list_add_tail(&info->list, &xenconsoles);
307 spin_unlock(&xencons_lock);
312 static void xen_console_update_evtchn(struct xencons_info *info)
314 if (xen_hvm_domain()) {
318 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
322 info->evtchn = xen_start_info->console.domU.evtchn;
325 void xen_console_resume(void)
327 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
328 if (info != NULL && info->irq) {
329 if (!xen_initial_domain())
330 xen_console_update_evtchn(info);
331 rebind_evtchn_irq(info->evtchn, info->irq);
335 #ifdef CONFIG_HVC_XEN_FRONTEND
336 static void xencons_disconnect_backend(struct xencons_info *info)
339 unbind_from_irqhandler(info->irq, NULL);
341 if (info->evtchn > 0)
342 xenbus_free_evtchn(info->xbdev, info->evtchn);
344 if (info->gntref > 0)
345 gnttab_free_grant_references(info->gntref);
347 if (info->hvc != NULL)
348 hvc_remove(info->hvc);
352 static void xencons_free(struct xencons_info *info)
354 free_page((unsigned long)info->intf);
360 static int xen_console_remove(struct xencons_info *info)
362 xencons_disconnect_backend(info);
363 spin_lock(&xencons_lock);
364 list_del(&info->list);
365 spin_unlock(&xencons_lock);
366 if (info->xbdev != NULL)
369 if (xen_hvm_domain())
376 static int xencons_remove(struct xenbus_device *dev)
378 return xen_console_remove(dev_get_drvdata(&dev->dev));
381 static int xencons_connect_backend(struct xenbus_device *dev,
382 struct xencons_info *info)
384 int ret, evtchn, devid, ref, irq;
385 struct xenbus_transaction xbt;
386 grant_ref_t gref_head;
388 ret = xenbus_alloc_evtchn(dev, &evtchn);
391 info->evtchn = evtchn;
392 irq = bind_evtchn_to_irq(evtchn);
396 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
397 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
398 irq, &domU_hvc_ops, 256);
399 if (IS_ERR(info->hvc))
400 return PTR_ERR(info->hvc);
401 ret = gnttab_alloc_grant_references(1, &gref_head);
404 info->gntref = gref_head;
405 ref = gnttab_claim_grant_reference(&gref_head);
408 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
409 virt_to_gfn(info->intf), 0);
412 ret = xenbus_transaction_start(&xbt);
414 xenbus_dev_fatal(dev, ret, "starting transaction");
417 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
420 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
424 ret = xenbus_transaction_end(xbt, 0);
428 xenbus_dev_fatal(dev, ret, "completing transaction");
432 xenbus_switch_state(dev, XenbusStateInitialised);
436 xenbus_transaction_end(xbt, 1);
437 xenbus_dev_fatal(dev, ret, "writing xenstore");
441 static int xencons_probe(struct xenbus_device *dev,
442 const struct xenbus_device_id *id)
445 struct xencons_info *info;
447 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
451 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
454 dev_set_drvdata(&dev->dev, info);
456 info->vtermno = xenbus_devid_to_vtermno(devid);
457 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
461 ret = xencons_connect_backend(dev, info);
464 spin_lock(&xencons_lock);
465 list_add_tail(&info->list, &xenconsoles);
466 spin_unlock(&xencons_lock);
472 xenbus_dev_fatal(dev, ret, "allocating device memory");
474 xencons_disconnect_backend(info);
479 static int xencons_resume(struct xenbus_device *dev)
481 struct xencons_info *info = dev_get_drvdata(&dev->dev);
483 xencons_disconnect_backend(info);
484 memset(info->intf, 0, XEN_PAGE_SIZE);
485 return xencons_connect_backend(dev, info);
488 static void xencons_backend_changed(struct xenbus_device *dev,
489 enum xenbus_state backend_state)
491 switch (backend_state) {
492 case XenbusStateReconfiguring:
493 case XenbusStateReconfigured:
494 case XenbusStateInitialising:
495 case XenbusStateInitialised:
496 case XenbusStateUnknown:
499 case XenbusStateInitWait:
502 case XenbusStateConnected:
503 xenbus_switch_state(dev, XenbusStateConnected);
506 case XenbusStateClosed:
507 if (dev->state == XenbusStateClosed)
509 /* Missed the backend's CLOSING state -- fallthrough */
510 case XenbusStateClosing:
511 xenbus_frontend_closed(dev);
516 static const struct xenbus_device_id xencons_ids[] = {
521 static struct xenbus_driver xencons_driver = {
522 .name = "xenconsole",
524 .probe = xencons_probe,
525 .remove = xencons_remove,
526 .resume = xencons_resume,
527 .otherend_changed = xencons_backend_changed,
529 #endif /* CONFIG_HVC_XEN_FRONTEND */
531 static int __init xen_hvc_init(void)
534 struct xencons_info *info;
535 const struct hv_ops *ops;
540 if (xen_initial_domain()) {
542 r = xen_initial_domain_console_init();
545 info = vtermno_to_xencons(HVC_COOKIE);
548 if (xen_hvm_domain())
549 r = xen_hvm_console_init();
551 r = xen_pv_console_init();
555 info = vtermno_to_xencons(HVC_COOKIE);
556 info->irq = bind_evtchn_to_irq(info->evtchn);
559 info->irq = 0; /* NO_IRQ */
561 irq_set_noprobe(info->irq);
563 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
564 if (IS_ERR(info->hvc)) {
565 r = PTR_ERR(info->hvc);
566 spin_lock(&xencons_lock);
567 list_del(&info->list);
568 spin_unlock(&xencons_lock);
570 unbind_from_irqhandler(info->irq, NULL);
576 #ifdef CONFIG_HVC_XEN_FRONTEND
577 r = xenbus_register_frontend(&xencons_driver);
581 device_initcall(xen_hvc_init);
583 static int xen_cons_init(void)
585 const struct hv_ops *ops;
590 if (xen_initial_domain())
596 if (xen_hvm_domain())
597 r = xen_hvm_console_init();
599 r = xen_pv_console_init();
604 hvc_instantiate(HVC_COOKIE, 0, ops);
607 console_initcall(xen_cons_init);
610 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
612 if (xen_cpuid_base())
613 outsb(0xe9, str, len);
616 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
619 #ifdef CONFIG_EARLY_PRINTK
620 static int __init xenboot_setup_console(struct console *console, char *string)
622 static struct xencons_info xenboot;
624 if (xen_initial_domain())
626 if (!xen_pv_domain())
629 return xencons_info_pv_init(&xenboot, 0);
632 static void xenboot_write_console(struct console *console, const char *string,
635 unsigned int linelen, off = 0;
638 if (!xen_pv_domain()) {
639 xen_hvm_early_write(0, string, len);
643 dom0_write_console(0, string, len);
645 if (xen_initial_domain())
648 domU_write_console(0, "(early) ", 8);
649 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
650 linelen = pos-string+off;
651 if (off + linelen > len)
653 domU_write_console(0, string+off, linelen);
654 domU_write_console(0, "\r\n", 2);
658 domU_write_console(0, string+off, len-off);
661 struct console xenboot_console = {
663 .write = xenboot_write_console,
664 .setup = xenboot_setup_console,
665 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
668 #endif /* CONFIG_EARLY_PRINTK */
670 void xen_raw_console_write(const char *str)
672 ssize_t len = strlen(str);
676 rc = dom0_write_console(0, str, len);
677 if (rc != -ENOSYS || !xen_hvm_domain())
680 xen_hvm_early_write(0, str, len);
683 void xen_raw_printk(const char *fmt, ...)
685 static char buf[512];
689 vsnprintf(buf, sizeof(buf), fmt, ap);
692 xen_raw_console_write(buf);
695 static void xenboot_earlycon_write(struct console *console,
699 dom0_write_console(0, string, len);
702 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
705 device->con->write = xenboot_earlycon_write;
708 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);