1 // SPDX-License-Identifier: GPL-2.0+
3 * xen console driver interface to hvc_console.c
5 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
8 #include <linux/console.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/irq.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/list.h>
15 #include <linux/serial_core.h>
18 #include <asm/xen/hypervisor.h>
21 #include <xen/interface/xen.h>
23 #include <xen/grant_table.h>
25 #include <xen/events.h>
26 #include <xen/interface/io/console.h>
27 #include <xen/interface/sched.h>
28 #include <xen/hvc-console.h>
29 #include <xen/xenbus.h>
31 #include "hvc_console.h"
33 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
36 struct list_head list;
37 struct xenbus_device *xbdev;
38 struct xencons_interface *intf;
40 XENCONS_RING_IDX out_cons;
41 unsigned int out_cons_same;
42 struct hvc_struct *hvc;
48 static LIST_HEAD(xenconsoles);
49 static DEFINE_SPINLOCK(xencons_lock);
51 /* ------------------------------------------------------------------ */
53 static struct xencons_info *vtermno_to_xencons(int vtermno)
55 struct xencons_info *entry, *n, *ret = NULL;
57 if (list_empty(&xenconsoles))
60 list_for_each_entry_safe(entry, n, &xenconsoles, list) {
61 if (entry->vtermno == vtermno) {
70 static inline int xenbus_devid_to_vtermno(int devid)
72 return devid + HVC_COOKIE;
75 static inline void notify_daemon(struct xencons_info *cons)
77 /* Use evtchn: this is called early, before irq is set up. */
78 notify_remote_via_evtchn(cons->evtchn);
81 static int __write_console(struct xencons_info *xencons,
82 const char *data, int len)
84 XENCONS_RING_IDX cons, prod;
85 struct xencons_interface *intf = xencons->intf;
88 cons = intf->out_cons;
89 prod = intf->out_prod;
90 mb(); /* update queue values before going on */
92 if ((prod - cons) > sizeof(intf->out)) {
93 pr_err_once("xencons: Illegal ring page indices");
97 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
98 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
100 wmb(); /* write ring before updating pointer */
101 intf->out_prod = prod;
104 notify_daemon(xencons);
108 static int domU_write_console(uint32_t vtermno, const char *data, int len)
111 struct xencons_info *cons = vtermno_to_xencons(vtermno);
116 * Make sure the whole buffer is emitted, polling if
117 * necessary. We don't ever want to rely on the hvc daemon
118 * because the most interesting console output is when the
119 * kernel is crippled.
122 int sent = __write_console(cons, data, len);
131 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
137 static int domU_read_console(uint32_t vtermno, char *buf, int len)
139 struct xencons_interface *intf;
140 XENCONS_RING_IDX cons, prod;
142 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
143 unsigned int eoiflag = 0;
147 intf = xencons->intf;
149 cons = intf->in_cons;
150 prod = intf->in_prod;
151 mb(); /* get pointers before reading ring */
153 if ((prod - cons) > sizeof(intf->in)) {
154 pr_err_once("xencons: Illegal ring page indices");
158 while (cons != prod && recv < len)
159 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
161 mb(); /* read ring before consuming */
162 intf->in_cons = cons;
165 * When to mark interrupt having been spurious:
166 * - there was no new data to be read, and
167 * - the backend did not consume some output bytes, and
168 * - the previous round with no read data didn't see consumed bytes
169 * (we might have a race with an interrupt being in flight while
170 * updating xencons->out_cons, so account for that by allowing one
171 * round without any visible reason)
173 if (intf->out_cons != xencons->out_cons) {
174 xencons->out_cons = intf->out_cons;
175 xencons->out_cons_same = 0;
178 notify_daemon(xencons);
179 } else if (xencons->out_cons_same++ > 1) {
180 eoiflag = XEN_EOI_FLAG_SPURIOUS;
183 xen_irq_lateeoi(xencons->irq, eoiflag);
188 static const struct hv_ops domU_hvc_ops = {
189 .get_chars = domU_read_console,
190 .put_chars = domU_write_console,
191 .notifier_add = notifier_add_irq,
192 .notifier_del = notifier_del_irq,
193 .notifier_hangup = notifier_hangup_irq,
196 static int dom0_read_console(uint32_t vtermno, char *buf, int len)
198 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
202 * Either for a dom0 to write to the system console, or a domU with a
203 * debug version of Xen
205 static int dom0_write_console(uint32_t vtermno, const char *str, int len)
207 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
214 static const struct hv_ops dom0_hvc_ops = {
215 .get_chars = dom0_read_console,
216 .put_chars = dom0_write_console,
217 .notifier_add = notifier_add_irq,
218 .notifier_del = notifier_del_irq,
219 .notifier_hangup = notifier_hangup_irq,
222 static int xen_hvm_console_init(void)
227 struct xencons_info *info;
229 if (!xen_hvm_domain())
232 info = vtermno_to_xencons(HVC_COOKIE);
234 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
237 } else if (info->intf != NULL) {
238 /* already configured */
242 * If the toolstack (or the hypervisor) hasn't set these values, the
243 * default value is 0. Even though gfn = 0 and evtchn = 0 are
244 * theoretically correct values, in practice they never are and they
245 * mean that a legacy toolstack hasn't initialized the pv console correctly.
247 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
252 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
256 info->intf = xen_remap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE);
257 if (info->intf == NULL)
259 info->vtermno = HVC_COOKIE;
261 spin_lock(&xencons_lock);
262 list_add_tail(&info->list, &xenconsoles);
263 spin_unlock(&xencons_lock);
271 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
273 info->evtchn = xen_start_info->console.domU.evtchn;
274 /* GFN == MFN for PV guest */
275 info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
276 info->vtermno = vtermno;
278 list_add_tail(&info->list, &xenconsoles);
283 static int xen_pv_console_init(void)
285 struct xencons_info *info;
287 if (!xen_pv_domain())
290 if (!xen_start_info->console.domU.evtchn)
293 info = vtermno_to_xencons(HVC_COOKIE);
295 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
298 } else if (info->intf != NULL) {
299 /* already configured */
302 spin_lock(&xencons_lock);
303 xencons_info_pv_init(info, HVC_COOKIE);
304 spin_unlock(&xencons_lock);
309 static int xen_initial_domain_console_init(void)
311 struct xencons_info *info;
313 if (!xen_initial_domain())
316 info = vtermno_to_xencons(HVC_COOKIE);
318 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
323 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
324 info->vtermno = HVC_COOKIE;
326 spin_lock(&xencons_lock);
327 list_add_tail(&info->list, &xenconsoles);
328 spin_unlock(&xencons_lock);
333 static void xen_console_update_evtchn(struct xencons_info *info)
335 if (xen_hvm_domain()) {
339 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
343 info->evtchn = xen_start_info->console.domU.evtchn;
346 void xen_console_resume(void)
348 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
349 if (info != NULL && info->irq) {
350 if (!xen_initial_domain())
351 xen_console_update_evtchn(info);
352 rebind_evtchn_irq(info->evtchn, info->irq);
356 #ifdef CONFIG_HVC_XEN_FRONTEND
357 static void xencons_disconnect_backend(struct xencons_info *info)
360 unbind_from_irqhandler(info->irq, NULL);
362 if (info->evtchn > 0)
363 xenbus_free_evtchn(info->xbdev, info->evtchn);
365 if (info->gntref > 0)
366 gnttab_free_grant_references(info->gntref);
368 if (info->hvc != NULL)
369 hvc_remove(info->hvc);
373 static void xencons_free(struct xencons_info *info)
375 free_page((unsigned long)info->intf);
381 static int xen_console_remove(struct xencons_info *info)
383 xencons_disconnect_backend(info);
384 spin_lock(&xencons_lock);
385 list_del(&info->list);
386 spin_unlock(&xencons_lock);
387 if (info->xbdev != NULL)
390 if (xen_hvm_domain())
397 static int xencons_remove(struct xenbus_device *dev)
399 return xen_console_remove(dev_get_drvdata(&dev->dev));
402 static int xencons_connect_backend(struct xenbus_device *dev,
403 struct xencons_info *info)
405 int ret, evtchn, devid, ref, irq;
406 struct xenbus_transaction xbt;
407 grant_ref_t gref_head;
409 ret = xenbus_alloc_evtchn(dev, &evtchn);
412 info->evtchn = evtchn;
413 irq = bind_interdomain_evtchn_to_irq_lateeoi(dev->otherend_id, evtchn);
417 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
418 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
419 irq, &domU_hvc_ops, 256);
420 if (IS_ERR(info->hvc))
421 return PTR_ERR(info->hvc);
422 ret = gnttab_alloc_grant_references(1, &gref_head);
425 info->gntref = gref_head;
426 ref = gnttab_claim_grant_reference(&gref_head);
429 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
430 virt_to_gfn(info->intf), 0);
433 ret = xenbus_transaction_start(&xbt);
435 xenbus_dev_fatal(dev, ret, "starting transaction");
438 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
441 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
445 ret = xenbus_transaction_end(xbt, 0);
449 xenbus_dev_fatal(dev, ret, "completing transaction");
453 xenbus_switch_state(dev, XenbusStateInitialised);
457 xenbus_transaction_end(xbt, 1);
458 xenbus_dev_fatal(dev, ret, "writing xenstore");
462 static int xencons_probe(struct xenbus_device *dev,
463 const struct xenbus_device_id *id)
466 struct xencons_info *info;
468 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
472 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
475 dev_set_drvdata(&dev->dev, info);
477 info->vtermno = xenbus_devid_to_vtermno(devid);
478 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
482 ret = xencons_connect_backend(dev, info);
485 spin_lock(&xencons_lock);
486 list_add_tail(&info->list, &xenconsoles);
487 spin_unlock(&xencons_lock);
493 xenbus_dev_fatal(dev, ret, "allocating device memory");
495 xencons_disconnect_backend(info);
500 static int xencons_resume(struct xenbus_device *dev)
502 struct xencons_info *info = dev_get_drvdata(&dev->dev);
504 xencons_disconnect_backend(info);
505 memset(info->intf, 0, XEN_PAGE_SIZE);
506 return xencons_connect_backend(dev, info);
509 static void xencons_backend_changed(struct xenbus_device *dev,
510 enum xenbus_state backend_state)
512 switch (backend_state) {
513 case XenbusStateReconfiguring:
514 case XenbusStateReconfigured:
515 case XenbusStateInitialising:
516 case XenbusStateInitialised:
517 case XenbusStateUnknown:
520 case XenbusStateInitWait:
523 case XenbusStateConnected:
524 xenbus_switch_state(dev, XenbusStateConnected);
527 case XenbusStateClosed:
528 if (dev->state == XenbusStateClosed)
530 fallthrough; /* Missed the backend's CLOSING state */
531 case XenbusStateClosing:
532 xenbus_frontend_closed(dev);
537 static const struct xenbus_device_id xencons_ids[] = {
542 static struct xenbus_driver xencons_driver = {
543 .name = "xenconsole",
545 .probe = xencons_probe,
546 .remove = xencons_remove,
547 .resume = xencons_resume,
548 .otherend_changed = xencons_backend_changed,
550 #endif /* CONFIG_HVC_XEN_FRONTEND */
552 static int __init xen_hvc_init(void)
555 struct xencons_info *info;
556 const struct hv_ops *ops;
561 if (xen_initial_domain()) {
563 r = xen_initial_domain_console_init();
566 info = vtermno_to_xencons(HVC_COOKIE);
569 if (xen_hvm_domain())
570 r = xen_hvm_console_init();
572 r = xen_pv_console_init();
576 info = vtermno_to_xencons(HVC_COOKIE);
577 info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn);
580 info->irq = 0; /* NO_IRQ */
582 irq_set_noprobe(info->irq);
584 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
585 if (IS_ERR(info->hvc)) {
586 r = PTR_ERR(info->hvc);
587 spin_lock(&xencons_lock);
588 list_del(&info->list);
589 spin_unlock(&xencons_lock);
591 unbind_from_irqhandler(info->irq, NULL);
597 #ifdef CONFIG_HVC_XEN_FRONTEND
598 r = xenbus_register_frontend(&xencons_driver);
602 device_initcall(xen_hvc_init);
604 static int xen_cons_init(void)
606 const struct hv_ops *ops;
611 if (xen_initial_domain())
617 if (xen_hvm_domain())
618 r = xen_hvm_console_init();
620 r = xen_pv_console_init();
625 hvc_instantiate(HVC_COOKIE, 0, ops);
628 console_initcall(xen_cons_init);
631 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
633 if (xen_cpuid_base())
634 outsb(0xe9, str, len);
637 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
640 #ifdef CONFIG_EARLY_PRINTK
641 static int __init xenboot_console_setup(struct console *console, char *string)
643 static struct xencons_info xenboot;
645 if (xen_initial_domain())
647 if (!xen_pv_domain())
650 return xencons_info_pv_init(&xenboot, 0);
653 static void xenboot_write_console(struct console *console, const char *string,
656 unsigned int linelen, off = 0;
659 if (!xen_pv_domain()) {
660 xen_hvm_early_write(0, string, len);
664 dom0_write_console(0, string, len);
666 if (xen_initial_domain())
669 domU_write_console(0, "(early) ", 8);
670 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
671 linelen = pos-string+off;
672 if (off + linelen > len)
674 domU_write_console(0, string+off, linelen);
675 domU_write_console(0, "\r\n", 2);
679 domU_write_console(0, string+off, len-off);
682 struct console xenboot_console = {
684 .write = xenboot_write_console,
685 .setup = xenboot_console_setup,
686 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
689 #endif /* CONFIG_EARLY_PRINTK */
691 void xen_raw_console_write(const char *str)
693 ssize_t len = strlen(str);
697 rc = dom0_write_console(0, str, len);
698 if (rc != -ENOSYS || !xen_hvm_domain())
701 xen_hvm_early_write(0, str, len);
704 void xen_raw_printk(const char *fmt, ...)
706 static char buf[512];
710 vsnprintf(buf, sizeof(buf), fmt, ap);
713 xen_raw_console_write(buf);
716 static void xenboot_earlycon_write(struct console *console,
720 dom0_write_console(0, string, len);
723 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
726 device->con->write = xenboot_earlycon_write;
729 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);