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;
49 static LIST_HEAD(xenconsoles);
50 static DEFINE_SPINLOCK(xencons_lock);
52 /* ------------------------------------------------------------------ */
54 static struct xencons_info *vtermno_to_xencons(int vtermno)
56 struct xencons_info *entry, *ret = NULL;
59 spin_lock_irqsave(&xencons_lock, flags);
60 if (list_empty(&xenconsoles)) {
61 spin_unlock_irqrestore(&xencons_lock, flags);
65 list_for_each_entry(entry, &xenconsoles, list) {
66 if (entry->vtermno == vtermno) {
71 spin_unlock_irqrestore(&xencons_lock, flags);
76 static inline int xenbus_devid_to_vtermno(int devid)
78 return devid + HVC_COOKIE;
81 static inline void notify_daemon(struct xencons_info *cons)
83 /* Use evtchn: this is called early, before irq is set up. */
84 notify_remote_via_evtchn(cons->evtchn);
87 static ssize_t __write_console(struct xencons_info *xencons,
88 const u8 *data, size_t len)
90 XENCONS_RING_IDX cons, prod;
91 struct xencons_interface *intf = xencons->intf;
95 spin_lock_irqsave(&xencons->ring_lock, flags);
96 cons = intf->out_cons;
97 prod = intf->out_prod;
98 mb(); /* update queue values before going on */
100 if ((prod - cons) > sizeof(intf->out)) {
101 spin_unlock_irqrestore(&xencons->ring_lock, flags);
102 pr_err_once("xencons: Illegal ring page indices");
106 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
107 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
109 wmb(); /* write ring before updating pointer */
110 intf->out_prod = prod;
111 spin_unlock_irqrestore(&xencons->ring_lock, flags);
114 notify_daemon(xencons);
118 static ssize_t domU_write_console(uint32_t vtermno, const u8 *data, size_t len)
120 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 ssize_t sent = __write_console(cons, data, len);
142 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
148 static ssize_t domU_read_console(uint32_t vtermno, u8 *buf, size_t len)
150 struct xencons_interface *intf;
151 XENCONS_RING_IDX cons, prod;
152 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
153 unsigned int eoiflag = 0;
159 intf = xencons->intf;
161 spin_lock_irqsave(&xencons->ring_lock, flags);
162 cons = intf->in_cons;
163 prod = intf->in_prod;
164 mb(); /* get pointers before reading ring */
166 if ((prod - cons) > sizeof(intf->in)) {
167 spin_unlock_irqrestore(&xencons->ring_lock, flags);
168 pr_err_once("xencons: Illegal ring page indices");
172 while (cons != prod && recv < len)
173 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
175 mb(); /* read ring before consuming */
176 intf->in_cons = cons;
179 * When to mark interrupt having been spurious:
180 * - there was no new data to be read, and
181 * - the backend did not consume some output bytes, and
182 * - the previous round with no read data didn't see consumed bytes
183 * (we might have a race with an interrupt being in flight while
184 * updating xencons->out_cons, so account for that by allowing one
185 * round without any visible reason)
187 if (intf->out_cons != xencons->out_cons) {
188 xencons->out_cons = intf->out_cons;
189 xencons->out_cons_same = 0;
191 if (!recv && xencons->out_cons_same++ > 1) {
192 eoiflag = XEN_EOI_FLAG_SPURIOUS;
194 spin_unlock_irqrestore(&xencons->ring_lock, flags);
197 notify_daemon(xencons);
200 xen_irq_lateeoi(xencons->irq, eoiflag);
205 static const struct hv_ops domU_hvc_ops = {
206 .get_chars = domU_read_console,
207 .put_chars = domU_write_console,
208 .notifier_add = notifier_add_irq,
209 .notifier_del = notifier_del_irq,
210 .notifier_hangup = notifier_hangup_irq,
213 static ssize_t dom0_read_console(uint32_t vtermno, u8 *buf, size_t len)
215 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
219 * Either for a dom0 to write to the system console, or a domU with a
220 * debug version of Xen
222 static ssize_t dom0_write_console(uint32_t vtermno, const u8 *str, size_t len)
224 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (u8 *)str);
231 static const struct hv_ops dom0_hvc_ops = {
232 .get_chars = dom0_read_console,
233 .put_chars = dom0_write_console,
234 .notifier_add = notifier_add_irq,
235 .notifier_del = notifier_del_irq,
236 .notifier_hangup = notifier_hangup_irq,
239 static int xen_hvm_console_init(void)
243 unsigned long gfn, flags;
244 struct xencons_info *info;
246 if (!xen_hvm_domain())
249 info = vtermno_to_xencons(HVC_COOKIE);
251 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
254 spin_lock_init(&info->ring_lock);
255 } else if (info->intf != NULL) {
256 /* already configured */
260 * If the toolstack (or the hypervisor) hasn't set these values, the
261 * default value is 0. Even though gfn = 0 and evtchn = 0 are
262 * theoretically correct values, in practice they never are and they
263 * mean that a legacy toolstack hasn't initialized the pv console correctly.
265 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
270 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
274 info->intf = memremap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE, MEMREMAP_WB);
275 if (info->intf == NULL)
277 info->vtermno = HVC_COOKIE;
279 spin_lock_irqsave(&xencons_lock, flags);
280 list_add_tail(&info->list, &xenconsoles);
281 spin_unlock_irqrestore(&xencons_lock, flags);
289 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
291 spin_lock_init(&info->ring_lock);
292 info->evtchn = xen_start_info->console.domU.evtchn;
293 /* GFN == MFN for PV guest */
294 info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
295 info->vtermno = vtermno;
297 list_add_tail(&info->list, &xenconsoles);
302 static int xen_pv_console_init(void)
304 struct xencons_info *info;
307 if (!xen_pv_domain())
310 if (!xen_start_info->console.domU.evtchn)
313 info = vtermno_to_xencons(HVC_COOKIE);
315 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
318 } else if (info->intf != NULL) {
319 /* already configured */
322 spin_lock_irqsave(&xencons_lock, flags);
323 xencons_info_pv_init(info, HVC_COOKIE);
324 spin_unlock_irqrestore(&xencons_lock, flags);
329 static int xen_initial_domain_console_init(void)
331 struct xencons_info *info;
334 if (!xen_initial_domain())
337 info = vtermno_to_xencons(HVC_COOKIE);
339 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
342 spin_lock_init(&info->ring_lock);
345 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
346 info->vtermno = HVC_COOKIE;
348 spin_lock_irqsave(&xencons_lock, flags);
349 list_add_tail(&info->list, &xenconsoles);
350 spin_unlock_irqrestore(&xencons_lock, flags);
355 static void xen_console_update_evtchn(struct xencons_info *info)
357 if (xen_hvm_domain()) {
361 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
365 info->evtchn = xen_start_info->console.domU.evtchn;
368 void xen_console_resume(void)
370 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
371 if (info != NULL && info->irq) {
372 if (!xen_initial_domain())
373 xen_console_update_evtchn(info);
374 rebind_evtchn_irq(info->evtchn, info->irq);
378 #ifdef CONFIG_HVC_XEN_FRONTEND
379 static void xencons_disconnect_backend(struct xencons_info *info)
381 if (info->hvc != NULL)
382 hvc_remove(info->hvc);
385 evtchn_put(info->evtchn);
389 /* evtchn_put() will also close it so this is only an error path */
390 if (info->evtchn > 0)
391 xenbus_free_evtchn(info->xbdev, info->evtchn);
393 if (info->gntref > 0)
394 gnttab_free_grant_references(info->gntref);
398 static void xencons_free(struct xencons_info *info)
400 free_page((unsigned long)info->intf);
406 static int xen_console_remove(struct xencons_info *info)
410 xencons_disconnect_backend(info);
411 spin_lock_irqsave(&xencons_lock, flags);
412 list_del(&info->list);
413 spin_unlock_irqrestore(&xencons_lock, flags);
414 if (info->xbdev != NULL)
417 if (xen_hvm_domain())
424 static void xencons_remove(struct xenbus_device *dev)
426 xen_console_remove(dev_get_drvdata(&dev->dev));
429 static int xencons_connect_backend(struct xenbus_device *dev,
430 struct xencons_info *info)
432 int ret, evtchn, devid, ref, irq;
433 struct xenbus_transaction xbt;
434 grant_ref_t gref_head;
436 ret = xenbus_alloc_evtchn(dev, &evtchn);
439 info->evtchn = evtchn;
440 irq = bind_evtchn_to_irq_lateeoi(evtchn);
444 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
445 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
446 irq, &domU_hvc_ops, 256);
447 if (IS_ERR(info->hvc))
448 return PTR_ERR(info->hvc);
449 ret = gnttab_alloc_grant_references(1, &gref_head);
452 info->gntref = gref_head;
453 ref = gnttab_claim_grant_reference(&gref_head);
456 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
457 virt_to_gfn(info->intf), 0);
460 ret = xenbus_transaction_start(&xbt);
462 xenbus_dev_fatal(dev, ret, "starting transaction");
465 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
468 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
472 ret = xenbus_transaction_end(xbt, 0);
476 xenbus_dev_fatal(dev, ret, "completing transaction");
480 xenbus_switch_state(dev, XenbusStateInitialised);
484 xenbus_transaction_end(xbt, 1);
485 xenbus_dev_fatal(dev, ret, "writing xenstore");
489 static int xencons_probe(struct xenbus_device *dev,
490 const struct xenbus_device_id *id)
493 struct xencons_info *info;
496 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
500 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
503 spin_lock_init(&info->ring_lock);
504 dev_set_drvdata(&dev->dev, info);
506 info->vtermno = xenbus_devid_to_vtermno(devid);
507 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
511 ret = xencons_connect_backend(dev, info);
514 spin_lock_irqsave(&xencons_lock, flags);
515 list_add_tail(&info->list, &xenconsoles);
516 spin_unlock_irqrestore(&xencons_lock, flags);
522 xenbus_dev_fatal(dev, ret, "allocating device memory");
524 xencons_disconnect_backend(info);
529 static int xencons_resume(struct xenbus_device *dev)
531 struct xencons_info *info = dev_get_drvdata(&dev->dev);
533 xencons_disconnect_backend(info);
534 memset(info->intf, 0, XEN_PAGE_SIZE);
535 return xencons_connect_backend(dev, info);
538 static void xencons_backend_changed(struct xenbus_device *dev,
539 enum xenbus_state backend_state)
541 switch (backend_state) {
542 case XenbusStateReconfiguring:
543 case XenbusStateReconfigured:
544 case XenbusStateInitialising:
545 case XenbusStateInitialised:
546 case XenbusStateUnknown:
549 case XenbusStateInitWait:
552 case XenbusStateConnected:
553 xenbus_switch_state(dev, XenbusStateConnected);
556 case XenbusStateClosed:
557 if (dev->state == XenbusStateClosed)
559 fallthrough; /* Missed the backend's CLOSING state */
560 case XenbusStateClosing: {
561 struct xencons_info *info = dev_get_drvdata(&dev->dev);;
564 * Don't tear down the evtchn and grant ref before the other
565 * end has disconnected, but do stop userspace from trying
566 * to use the device before we allow the backend to close.
569 hvc_remove(info->hvc);
573 xenbus_frontend_closed(dev);
579 static const struct xenbus_device_id xencons_ids[] = {
584 static struct xenbus_driver xencons_driver = {
585 .name = "xenconsole",
587 .probe = xencons_probe,
588 .remove = xencons_remove,
589 .resume = xencons_resume,
590 .otherend_changed = xencons_backend_changed,
591 .not_essential = true,
593 #endif /* CONFIG_HVC_XEN_FRONTEND */
595 static int __init xen_hvc_init(void)
598 struct xencons_info *info;
599 const struct hv_ops *ops;
604 if (xen_initial_domain()) {
606 r = xen_initial_domain_console_init();
609 info = vtermno_to_xencons(HVC_COOKIE);
612 if (xen_hvm_domain())
613 r = xen_hvm_console_init();
615 r = xen_pv_console_init();
619 info = vtermno_to_xencons(HVC_COOKIE);
620 info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn);
623 info->irq = 0; /* NO_IRQ */
625 irq_set_noprobe(info->irq);
627 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
628 if (IS_ERR(info->hvc)) {
631 r = PTR_ERR(info->hvc);
632 spin_lock_irqsave(&xencons_lock, flags);
633 list_del(&info->list);
634 spin_unlock_irqrestore(&xencons_lock, flags);
636 evtchn_put(info->evtchn);
643 #ifdef CONFIG_HVC_XEN_FRONTEND
644 r = xenbus_register_frontend(&xencons_driver);
648 device_initcall(xen_hvc_init);
650 static int xen_cons_init(void)
652 const struct hv_ops *ops;
657 if (xen_initial_domain())
663 if (xen_hvm_domain())
664 r = xen_hvm_console_init();
666 r = xen_pv_console_init();
671 hvc_instantiate(HVC_COOKIE, 0, ops);
674 console_initcall(xen_cons_init);
677 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
679 if (xen_cpuid_base())
680 outsb(0xe9, str, len);
683 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
686 #ifdef CONFIG_EARLY_PRINTK
687 static int __init xenboot_console_setup(struct console *console, char *string)
689 static struct xencons_info xenboot;
691 if (xen_initial_domain() || !xen_pv_domain())
694 return xencons_info_pv_init(&xenboot, 0);
697 static void xenboot_write_console(struct console *console, const char *string,
700 unsigned int linelen, off = 0;
703 if (dom0_write_console(0, string, len) >= 0)
706 if (!xen_pv_domain()) {
707 xen_hvm_early_write(0, string, len);
711 if (domU_write_console(0, "(early) ", 8) < 0)
713 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
714 linelen = pos-string+off;
715 if (off + linelen > len)
717 domU_write_console(0, string+off, linelen);
718 domU_write_console(0, "\r\n", 2);
722 domU_write_console(0, string+off, len-off);
725 struct console xenboot_console = {
727 .write = xenboot_write_console,
728 .setup = xenboot_console_setup,
729 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
732 #endif /* CONFIG_EARLY_PRINTK */
734 void xen_raw_console_write(const char *str)
736 ssize_t len = strlen(str);
740 rc = dom0_write_console(0, str, len);
741 if (rc != -ENOSYS || !xen_hvm_domain())
744 xen_hvm_early_write(0, str, len);
747 void xen_raw_printk(const char *fmt, ...)
749 static char buf[512];
753 vsnprintf(buf, sizeof(buf), fmt, ap);
756 xen_raw_console_write(buf);
759 static void xenboot_earlycon_write(struct console *console,
763 dom0_write_console(0, string, len);
766 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
769 device->con->write = xenboot_earlycon_write;
772 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);