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 int __write_console(struct xencons_info *xencons,
88 const char *data, int 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 int domU_write_console(uint32_t vtermno, const char *data, int len)
121 struct xencons_info *cons = vtermno_to_xencons(vtermno);
126 * Make sure the whole buffer is emitted, polling if
127 * necessary. We don't ever want to rely on the hvc daemon
128 * because the most interesting console output is when the
129 * kernel is crippled.
132 int sent = __write_console(cons, data, len);
141 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
147 static int domU_read_console(uint32_t vtermno, char *buf, int len)
149 struct xencons_interface *intf;
150 XENCONS_RING_IDX cons, prod;
152 struct xencons_info *xencons = vtermno_to_xencons(vtermno);
153 unsigned int eoiflag = 0;
158 intf = xencons->intf;
160 spin_lock_irqsave(&xencons->ring_lock, flags);
161 cons = intf->in_cons;
162 prod = intf->in_prod;
163 mb(); /* get pointers before reading ring */
165 if ((prod - cons) > sizeof(intf->in)) {
166 spin_unlock_irqrestore(&xencons->ring_lock, flags);
167 pr_err_once("xencons: Illegal ring page indices");
171 while (cons != prod && recv < len)
172 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
174 mb(); /* read ring before consuming */
175 intf->in_cons = cons;
178 * When to mark interrupt having been spurious:
179 * - there was no new data to be read, and
180 * - the backend did not consume some output bytes, and
181 * - the previous round with no read data didn't see consumed bytes
182 * (we might have a race with an interrupt being in flight while
183 * updating xencons->out_cons, so account for that by allowing one
184 * round without any visible reason)
186 if (intf->out_cons != xencons->out_cons) {
187 xencons->out_cons = intf->out_cons;
188 xencons->out_cons_same = 0;
190 if (!recv && xencons->out_cons_same++ > 1) {
191 eoiflag = XEN_EOI_FLAG_SPURIOUS;
193 spin_unlock_irqrestore(&xencons->ring_lock, flags);
196 notify_daemon(xencons);
199 xen_irq_lateeoi(xencons->irq, eoiflag);
204 static const struct hv_ops domU_hvc_ops = {
205 .get_chars = domU_read_console,
206 .put_chars = domU_write_console,
207 .notifier_add = notifier_add_irq,
208 .notifier_del = notifier_del_irq,
209 .notifier_hangup = notifier_hangup_irq,
212 static int dom0_read_console(uint32_t vtermno, char *buf, int len)
214 return HYPERVISOR_console_io(CONSOLEIO_read, len, buf);
218 * Either for a dom0 to write to the system console, or a domU with a
219 * debug version of Xen
221 static int dom0_write_console(uint32_t vtermno, const char *str, int len)
223 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
230 static const struct hv_ops dom0_hvc_ops = {
231 .get_chars = dom0_read_console,
232 .put_chars = dom0_write_console,
233 .notifier_add = notifier_add_irq,
234 .notifier_del = notifier_del_irq,
235 .notifier_hangup = notifier_hangup_irq,
238 static int xen_hvm_console_init(void)
242 unsigned long gfn, flags;
243 struct xencons_info *info;
245 if (!xen_hvm_domain())
248 info = vtermno_to_xencons(HVC_COOKIE);
250 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
253 spin_lock_init(&info->ring_lock);
254 } else if (info->intf != NULL) {
255 /* already configured */
259 * If the toolstack (or the hypervisor) hasn't set these values, the
260 * default value is 0. Even though gfn = 0 and evtchn = 0 are
261 * theoretically correct values, in practice they never are and they
262 * mean that a legacy toolstack hasn't initialized the pv console correctly.
264 r = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
269 r = hvm_get_parameter(HVM_PARAM_CONSOLE_PFN, &v);
273 info->intf = xen_remap(gfn << XEN_PAGE_SHIFT, XEN_PAGE_SIZE);
274 if (info->intf == NULL)
276 info->vtermno = HVC_COOKIE;
278 spin_lock_irqsave(&xencons_lock, flags);
279 list_add_tail(&info->list, &xenconsoles);
280 spin_unlock_irqrestore(&xencons_lock, flags);
288 static int xencons_info_pv_init(struct xencons_info *info, int vtermno)
290 spin_lock_init(&info->ring_lock);
291 info->evtchn = xen_start_info->console.domU.evtchn;
292 /* GFN == MFN for PV guest */
293 info->intf = gfn_to_virt(xen_start_info->console.domU.mfn);
294 info->vtermno = vtermno;
296 list_add_tail(&info->list, &xenconsoles);
301 static int xen_pv_console_init(void)
303 struct xencons_info *info;
306 if (!xen_pv_domain())
309 if (!xen_start_info->console.domU.evtchn)
312 info = vtermno_to_xencons(HVC_COOKIE);
314 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
317 } else if (info->intf != NULL) {
318 /* already configured */
321 spin_lock_irqsave(&xencons_lock, flags);
322 xencons_info_pv_init(info, HVC_COOKIE);
323 spin_unlock_irqrestore(&xencons_lock, flags);
328 static int xen_initial_domain_console_init(void)
330 struct xencons_info *info;
333 if (!xen_initial_domain())
336 info = vtermno_to_xencons(HVC_COOKIE);
338 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
341 spin_lock_init(&info->ring_lock);
344 info->irq = bind_virq_to_irq(VIRQ_CONSOLE, 0, false);
345 info->vtermno = HVC_COOKIE;
347 spin_lock_irqsave(&xencons_lock, flags);
348 list_add_tail(&info->list, &xenconsoles);
349 spin_unlock_irqrestore(&xencons_lock, flags);
354 static void xen_console_update_evtchn(struct xencons_info *info)
356 if (xen_hvm_domain()) {
360 err = hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN, &v);
364 info->evtchn = xen_start_info->console.domU.evtchn;
367 void xen_console_resume(void)
369 struct xencons_info *info = vtermno_to_xencons(HVC_COOKIE);
370 if (info != NULL && info->irq) {
371 if (!xen_initial_domain())
372 xen_console_update_evtchn(info);
373 rebind_evtchn_irq(info->evtchn, info->irq);
377 #ifdef CONFIG_HVC_XEN_FRONTEND
378 static void xencons_disconnect_backend(struct xencons_info *info)
380 if (info->hvc != NULL)
381 hvc_remove(info->hvc);
384 evtchn_put(info->evtchn);
388 /* evtchn_put() will also close it so this is only an error path */
389 if (info->evtchn > 0)
390 xenbus_free_evtchn(info->xbdev, info->evtchn);
392 if (info->gntref > 0)
393 gnttab_free_grant_references(info->gntref);
397 static void xencons_free(struct xencons_info *info)
399 free_page((unsigned long)info->intf);
405 static int xen_console_remove(struct xencons_info *info)
409 xencons_disconnect_backend(info);
410 spin_lock_irqsave(&xencons_lock, flags);
411 list_del(&info->list);
412 spin_unlock_irqrestore(&xencons_lock, flags);
413 if (info->xbdev != NULL)
416 if (xen_hvm_domain())
423 static int xencons_remove(struct xenbus_device *dev)
425 return xen_console_remove(dev_get_drvdata(&dev->dev));
428 static int xencons_connect_backend(struct xenbus_device *dev,
429 struct xencons_info *info)
431 int ret, evtchn, devid, ref, irq;
432 struct xenbus_transaction xbt;
433 grant_ref_t gref_head;
435 ret = xenbus_alloc_evtchn(dev, &evtchn);
438 info->evtchn = evtchn;
439 irq = bind_interdomain_evtchn_to_irq_lateeoi(dev->otherend_id, evtchn);
443 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
444 info->hvc = hvc_alloc(xenbus_devid_to_vtermno(devid),
445 irq, &domU_hvc_ops, 256);
446 if (IS_ERR(info->hvc))
447 return PTR_ERR(info->hvc);
448 ret = gnttab_alloc_grant_references(1, &gref_head);
451 info->gntref = gref_head;
452 ref = gnttab_claim_grant_reference(&gref_head);
455 gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
456 virt_to_gfn(info->intf), 0);
459 ret = xenbus_transaction_start(&xbt);
461 xenbus_dev_fatal(dev, ret, "starting transaction");
464 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", ref);
467 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
471 ret = xenbus_transaction_end(xbt, 0);
475 xenbus_dev_fatal(dev, ret, "completing transaction");
479 xenbus_switch_state(dev, XenbusStateInitialised);
483 xenbus_transaction_end(xbt, 1);
484 xenbus_dev_fatal(dev, ret, "writing xenstore");
488 static int xencons_probe(struct xenbus_device *dev,
489 const struct xenbus_device_id *id)
492 struct xencons_info *info;
495 devid = dev->nodename[strlen(dev->nodename) - 1] - '0';
499 info = kzalloc(sizeof(struct xencons_info), GFP_KERNEL);
502 spin_lock_init(&info->ring_lock);
503 dev_set_drvdata(&dev->dev, info);
505 info->vtermno = xenbus_devid_to_vtermno(devid);
506 info->intf = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
510 ret = xencons_connect_backend(dev, info);
513 spin_lock_irqsave(&xencons_lock, flags);
514 list_add_tail(&info->list, &xenconsoles);
515 spin_unlock_irqrestore(&xencons_lock, flags);
521 xenbus_dev_fatal(dev, ret, "allocating device memory");
523 xencons_disconnect_backend(info);
528 static int xencons_resume(struct xenbus_device *dev)
530 struct xencons_info *info = dev_get_drvdata(&dev->dev);
532 xencons_disconnect_backend(info);
533 memset(info->intf, 0, XEN_PAGE_SIZE);
534 return xencons_connect_backend(dev, info);
537 static void xencons_backend_changed(struct xenbus_device *dev,
538 enum xenbus_state backend_state)
540 switch (backend_state) {
541 case XenbusStateReconfiguring:
542 case XenbusStateReconfigured:
543 case XenbusStateInitialising:
544 case XenbusStateInitialised:
545 case XenbusStateUnknown:
548 case XenbusStateInitWait:
551 case XenbusStateConnected:
552 xenbus_switch_state(dev, XenbusStateConnected);
555 case XenbusStateClosed:
556 if (dev->state == XenbusStateClosed)
558 fallthrough; /* Missed the backend's CLOSING state */
559 case XenbusStateClosing: {
560 struct xencons_info *info = dev_get_drvdata(&dev->dev);;
563 * Don't tear down the evtchn and grant ref before the other
564 * end has disconnected, but do stop userspace from trying
565 * to use the device before we allow the backend to close.
568 hvc_remove(info->hvc);
572 xenbus_frontend_closed(dev);
578 static const struct xenbus_device_id xencons_ids[] = {
583 static struct xenbus_driver xencons_driver = {
584 .name = "xenconsole",
586 .probe = xencons_probe,
587 .remove = xencons_remove,
588 .resume = xencons_resume,
589 .otherend_changed = xencons_backend_changed,
591 #endif /* CONFIG_HVC_XEN_FRONTEND */
593 static int __init xen_hvc_init(void)
596 struct xencons_info *info;
597 const struct hv_ops *ops;
602 if (xen_initial_domain()) {
604 r = xen_initial_domain_console_init();
607 info = vtermno_to_xencons(HVC_COOKIE);
610 if (xen_hvm_domain())
611 r = xen_hvm_console_init();
613 r = xen_pv_console_init();
617 info = vtermno_to_xencons(HVC_COOKIE);
618 info->irq = bind_evtchn_to_irq_lateeoi(info->evtchn);
621 info->irq = 0; /* NO_IRQ */
623 irq_set_noprobe(info->irq);
625 info->hvc = hvc_alloc(HVC_COOKIE, info->irq, ops, 256);
626 if (IS_ERR(info->hvc)) {
629 r = PTR_ERR(info->hvc);
630 spin_lock_irqsave(&xencons_lock, flags);
631 list_del(&info->list);
632 spin_unlock_irqrestore(&xencons_lock, flags);
634 evtchn_put(info->evtchn);
641 #ifdef CONFIG_HVC_XEN_FRONTEND
642 r = xenbus_register_frontend(&xencons_driver);
646 device_initcall(xen_hvc_init);
648 static int xen_cons_init(void)
650 const struct hv_ops *ops;
655 if (xen_initial_domain())
661 if (xen_hvm_domain())
662 r = xen_hvm_console_init();
664 r = xen_pv_console_init();
669 hvc_instantiate(HVC_COOKIE, 0, ops);
672 console_initcall(xen_cons_init);
675 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len)
677 if (xen_cpuid_base())
678 outsb(0xe9, str, len);
681 static void xen_hvm_early_write(uint32_t vtermno, const char *str, int len) { }
684 #ifdef CONFIG_EARLY_PRINTK
685 static int __init xenboot_console_setup(struct console *console, char *string)
687 static struct xencons_info xenboot;
689 if (xen_initial_domain())
691 if (!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 (!xen_pv_domain()) {
704 xen_hvm_early_write(0, string, len);
708 dom0_write_console(0, string, len);
710 if (xen_initial_domain())
713 domU_write_console(0, "(early) ", 8);
714 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
715 linelen = pos-string+off;
716 if (off + linelen > len)
718 domU_write_console(0, string+off, linelen);
719 domU_write_console(0, "\r\n", 2);
723 domU_write_console(0, string+off, len-off);
726 struct console xenboot_console = {
728 .write = xenboot_write_console,
729 .setup = xenboot_console_setup,
730 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
733 #endif /* CONFIG_EARLY_PRINTK */
735 void xen_raw_console_write(const char *str)
737 ssize_t len = strlen(str);
741 rc = dom0_write_console(0, str, len);
742 if (rc != -ENOSYS || !xen_hvm_domain())
745 xen_hvm_early_write(0, str, len);
748 void xen_raw_printk(const char *fmt, ...)
750 static char buf[512];
754 vsnprintf(buf, sizeof(buf), fmt, ap);
757 xen_raw_console_write(buf);
760 static void xenboot_earlycon_write(struct console *console,
764 dom0_write_console(0, string, len);
767 static int __init xenboot_earlycon_setup(struct earlycon_device *device,
770 device->con->write = xenboot_earlycon_write;
773 EARLYCON_DECLARE(xenboot, xenboot_earlycon_setup);