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 struct hvc_struct
*hvc
;
46 static LIST_HEAD(xenconsoles
);
47 static DEFINE_SPINLOCK(xencons_lock
);
49 /* ------------------------------------------------------------------ */
51 static struct xencons_info
*vtermno_to_xencons(int vtermno
)
53 struct xencons_info
*entry
, *n
, *ret
= NULL
;
55 if (list_empty(&xenconsoles
))
58 list_for_each_entry_safe(entry
, n
, &xenconsoles
, list
) {
59 if (entry
->vtermno
== vtermno
) {
68 static inline int xenbus_devid_to_vtermno(int devid
)
70 return devid
+ HVC_COOKIE
;
73 static inline void notify_daemon(struct xencons_info
*cons
)
75 /* Use evtchn: this is called early, before irq is set up. */
76 notify_remote_via_evtchn(cons
->evtchn
);
79 static int __write_console(struct xencons_info
*xencons
,
80 const char *data
, int len
)
82 XENCONS_RING_IDX cons
, prod
;
83 struct xencons_interface
*intf
= xencons
->intf
;
86 cons
= intf
->out_cons
;
87 prod
= intf
->out_prod
;
88 mb(); /* update queue values before going on */
89 BUG_ON((prod
- cons
) > sizeof(intf
->out
));
91 while ((sent
< len
) && ((prod
- cons
) < sizeof(intf
->out
)))
92 intf
->out
[MASK_XENCONS_IDX(prod
++, intf
->out
)] = data
[sent
++];
94 wmb(); /* write ring before updating pointer */
95 intf
->out_prod
= prod
;
98 notify_daemon(xencons
);
102 static int domU_write_console(uint32_t vtermno
, const char *data
, int len
)
105 struct xencons_info
*cons
= vtermno_to_xencons(vtermno
);
110 * Make sure the whole buffer is emitted, polling if
111 * necessary. We don't ever want to rely on the hvc daemon
112 * because the most interesting console output is when the
113 * kernel is crippled.
116 int sent
= __write_console(cons
, data
, len
);
122 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
128 static int domU_read_console(uint32_t vtermno
, char *buf
, int len
)
130 struct xencons_interface
*intf
;
131 XENCONS_RING_IDX cons
, prod
;
133 struct xencons_info
*xencons
= vtermno_to_xencons(vtermno
);
136 intf
= xencons
->intf
;
138 cons
= intf
->in_cons
;
139 prod
= intf
->in_prod
;
140 mb(); /* get pointers before reading ring */
141 BUG_ON((prod
- cons
) > sizeof(intf
->in
));
143 while (cons
!= prod
&& recv
< len
)
144 buf
[recv
++] = intf
->in
[MASK_XENCONS_IDX(cons
++, intf
->in
)];
146 mb(); /* read ring before consuming */
147 intf
->in_cons
= cons
;
149 notify_daemon(xencons
);
153 static const struct hv_ops domU_hvc_ops
= {
154 .get_chars
= domU_read_console
,
155 .put_chars
= domU_write_console
,
156 .notifier_add
= notifier_add_irq
,
157 .notifier_del
= notifier_del_irq
,
158 .notifier_hangup
= notifier_hangup_irq
,
161 static int dom0_read_console(uint32_t vtermno
, char *buf
, int len
)
163 return HYPERVISOR_console_io(CONSOLEIO_read
, len
, buf
);
167 * Either for a dom0 to write to the system console, or a domU with a
168 * debug version of Xen
170 static int dom0_write_console(uint32_t vtermno
, const char *str
, int len
)
172 int rc
= HYPERVISOR_console_io(CONSOLEIO_write
, len
, (char *)str
);
179 static const struct hv_ops dom0_hvc_ops
= {
180 .get_chars
= dom0_read_console
,
181 .put_chars
= dom0_write_console
,
182 .notifier_add
= notifier_add_irq
,
183 .notifier_del
= notifier_del_irq
,
184 .notifier_hangup
= notifier_hangup_irq
,
187 static int xen_hvm_console_init(void)
192 struct xencons_info
*info
;
194 if (!xen_hvm_domain())
197 info
= vtermno_to_xencons(HVC_COOKIE
);
199 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
202 } else if (info
->intf
!= NULL
) {
203 /* already configured */
207 * If the toolstack (or the hypervisor) hasn't set these values, the
208 * default value is 0. Even though gfn = 0 and evtchn = 0 are
209 * theoretically correct values, in practice they never are and they
210 * mean that a legacy toolstack hasn't initialized the pv console correctly.
212 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
217 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_PFN
, &v
);
221 info
->intf
= xen_remap(gfn
<< XEN_PAGE_SHIFT
, XEN_PAGE_SIZE
);
222 if (info
->intf
== NULL
)
224 info
->vtermno
= HVC_COOKIE
;
226 spin_lock(&xencons_lock
);
227 list_add_tail(&info
->list
, &xenconsoles
);
228 spin_unlock(&xencons_lock
);
236 static int xencons_info_pv_init(struct xencons_info
*info
, int vtermno
)
238 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
239 /* GFN == MFN for PV guest */
240 info
->intf
= gfn_to_virt(xen_start_info
->console
.domU
.mfn
);
241 info
->vtermno
= vtermno
;
243 list_add_tail(&info
->list
, &xenconsoles
);
248 static int xen_pv_console_init(void)
250 struct xencons_info
*info
;
252 if (!xen_pv_domain())
255 if (!xen_start_info
->console
.domU
.evtchn
)
258 info
= vtermno_to_xencons(HVC_COOKIE
);
260 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
263 } else if (info
->intf
!= NULL
) {
264 /* already configured */
267 spin_lock(&xencons_lock
);
268 xencons_info_pv_init(info
, HVC_COOKIE
);
269 spin_unlock(&xencons_lock
);
274 static int xen_initial_domain_console_init(void)
276 struct xencons_info
*info
;
278 if (!xen_initial_domain())
281 info
= vtermno_to_xencons(HVC_COOKIE
);
283 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
288 info
->irq
= bind_virq_to_irq(VIRQ_CONSOLE
, 0, false);
289 info
->vtermno
= HVC_COOKIE
;
291 spin_lock(&xencons_lock
);
292 list_add_tail(&info
->list
, &xenconsoles
);
293 spin_unlock(&xencons_lock
);
298 static void xen_console_update_evtchn(struct xencons_info
*info
)
300 if (xen_hvm_domain()) {
304 err
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
308 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
311 void xen_console_resume(void)
313 struct xencons_info
*info
= vtermno_to_xencons(HVC_COOKIE
);
314 if (info
!= NULL
&& info
->irq
) {
315 if (!xen_initial_domain())
316 xen_console_update_evtchn(info
);
317 rebind_evtchn_irq(info
->evtchn
, info
->irq
);
321 #ifdef CONFIG_HVC_XEN_FRONTEND
322 static void xencons_disconnect_backend(struct xencons_info
*info
)
325 unbind_from_irqhandler(info
->irq
, NULL
);
327 if (info
->evtchn
> 0)
328 xenbus_free_evtchn(info
->xbdev
, info
->evtchn
);
330 if (info
->gntref
> 0)
331 gnttab_free_grant_references(info
->gntref
);
333 if (info
->hvc
!= NULL
)
334 hvc_remove(info
->hvc
);
338 static void xencons_free(struct xencons_info
*info
)
340 free_page((unsigned long)info
->intf
);
346 static int xen_console_remove(struct xencons_info
*info
)
348 xencons_disconnect_backend(info
);
349 spin_lock(&xencons_lock
);
350 list_del(&info
->list
);
351 spin_unlock(&xencons_lock
);
352 if (info
->xbdev
!= NULL
)
355 if (xen_hvm_domain())
362 static int xencons_remove(struct xenbus_device
*dev
)
364 return xen_console_remove(dev_get_drvdata(&dev
->dev
));
367 static int xencons_connect_backend(struct xenbus_device
*dev
,
368 struct xencons_info
*info
)
370 int ret
, evtchn
, devid
, ref
, irq
;
371 struct xenbus_transaction xbt
;
372 grant_ref_t gref_head
;
374 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
377 info
->evtchn
= evtchn
;
378 irq
= bind_evtchn_to_irq(evtchn
);
382 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
383 info
->hvc
= hvc_alloc(xenbus_devid_to_vtermno(devid
),
384 irq
, &domU_hvc_ops
, 256);
385 if (IS_ERR(info
->hvc
))
386 return PTR_ERR(info
->hvc
);
387 ret
= gnttab_alloc_grant_references(1, &gref_head
);
390 info
->gntref
= gref_head
;
391 ref
= gnttab_claim_grant_reference(&gref_head
);
394 gnttab_grant_foreign_access_ref(ref
, info
->xbdev
->otherend_id
,
395 virt_to_gfn(info
->intf
), 0);
398 ret
= xenbus_transaction_start(&xbt
);
400 xenbus_dev_fatal(dev
, ret
, "starting transaction");
403 ret
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%d", ref
);
406 ret
= xenbus_printf(xbt
, dev
->nodename
, "port", "%u",
410 ret
= xenbus_transaction_end(xbt
, 0);
414 xenbus_dev_fatal(dev
, ret
, "completing transaction");
418 xenbus_switch_state(dev
, XenbusStateInitialised
);
422 xenbus_transaction_end(xbt
, 1);
423 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
427 static int xencons_probe(struct xenbus_device
*dev
,
428 const struct xenbus_device_id
*id
)
431 struct xencons_info
*info
;
433 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
437 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
440 dev_set_drvdata(&dev
->dev
, info
);
442 info
->vtermno
= xenbus_devid_to_vtermno(devid
);
443 info
->intf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
447 ret
= xencons_connect_backend(dev
, info
);
450 spin_lock(&xencons_lock
);
451 list_add_tail(&info
->list
, &xenconsoles
);
452 spin_unlock(&xencons_lock
);
458 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
460 xencons_disconnect_backend(info
);
465 static int xencons_resume(struct xenbus_device
*dev
)
467 struct xencons_info
*info
= dev_get_drvdata(&dev
->dev
);
469 xencons_disconnect_backend(info
);
470 memset(info
->intf
, 0, XEN_PAGE_SIZE
);
471 return xencons_connect_backend(dev
, info
);
474 static void xencons_backend_changed(struct xenbus_device
*dev
,
475 enum xenbus_state backend_state
)
477 switch (backend_state
) {
478 case XenbusStateReconfiguring
:
479 case XenbusStateReconfigured
:
480 case XenbusStateInitialising
:
481 case XenbusStateInitialised
:
482 case XenbusStateUnknown
:
485 case XenbusStateInitWait
:
488 case XenbusStateConnected
:
489 xenbus_switch_state(dev
, XenbusStateConnected
);
492 case XenbusStateClosed
:
493 if (dev
->state
== XenbusStateClosed
)
495 /* fall through - Missed the backend's CLOSING state. */
496 case XenbusStateClosing
:
497 xenbus_frontend_closed(dev
);
502 static const struct xenbus_device_id xencons_ids
[] = {
507 static struct xenbus_driver xencons_driver
= {
508 .name
= "xenconsole",
510 .probe
= xencons_probe
,
511 .remove
= xencons_remove
,
512 .resume
= xencons_resume
,
513 .otherend_changed
= xencons_backend_changed
,
515 #endif /* CONFIG_HVC_XEN_FRONTEND */
517 static int __init
xen_hvc_init(void)
520 struct xencons_info
*info
;
521 const struct hv_ops
*ops
;
526 if (xen_initial_domain()) {
528 r
= xen_initial_domain_console_init();
531 info
= vtermno_to_xencons(HVC_COOKIE
);
534 if (xen_hvm_domain())
535 r
= xen_hvm_console_init();
537 r
= xen_pv_console_init();
541 info
= vtermno_to_xencons(HVC_COOKIE
);
542 info
->irq
= bind_evtchn_to_irq(info
->evtchn
);
545 info
->irq
= 0; /* NO_IRQ */
547 irq_set_noprobe(info
->irq
);
549 info
->hvc
= hvc_alloc(HVC_COOKIE
, info
->irq
, ops
, 256);
550 if (IS_ERR(info
->hvc
)) {
551 r
= PTR_ERR(info
->hvc
);
552 spin_lock(&xencons_lock
);
553 list_del(&info
->list
);
554 spin_unlock(&xencons_lock
);
556 unbind_from_irqhandler(info
->irq
, NULL
);
562 #ifdef CONFIG_HVC_XEN_FRONTEND
563 r
= xenbus_register_frontend(&xencons_driver
);
567 device_initcall(xen_hvc_init
);
569 static int xen_cons_init(void)
571 const struct hv_ops
*ops
;
576 if (xen_initial_domain())
582 if (xen_hvm_domain())
583 r
= xen_hvm_console_init();
585 r
= xen_pv_console_init();
590 hvc_instantiate(HVC_COOKIE
, 0, ops
);
593 console_initcall(xen_cons_init
);
596 static void xen_hvm_early_write(uint32_t vtermno
, const char *str
, int len
)
598 if (xen_cpuid_base())
599 outsb(0xe9, str
, len
);
602 static void xen_hvm_early_write(uint32_t vtermno
, const char *str
, int len
) { }
605 #ifdef CONFIG_EARLY_PRINTK
606 static int __init
xenboot_setup_console(struct console
*console
, char *string
)
608 static struct xencons_info xenboot
;
610 if (xen_initial_domain())
612 if (!xen_pv_domain())
615 return xencons_info_pv_init(&xenboot
, 0);
618 static void xenboot_write_console(struct console
*console
, const char *string
,
621 unsigned int linelen
, off
= 0;
624 if (!xen_pv_domain()) {
625 xen_hvm_early_write(0, string
, len
);
629 dom0_write_console(0, string
, len
);
631 if (xen_initial_domain())
634 domU_write_console(0, "(early) ", 8);
635 while (off
< len
&& NULL
!= (pos
= strchr(string
+off
, '\n'))) {
636 linelen
= pos
-string
+off
;
637 if (off
+ linelen
> len
)
639 domU_write_console(0, string
+off
, linelen
);
640 domU_write_console(0, "\r\n", 2);
644 domU_write_console(0, string
+off
, len
-off
);
647 struct console xenboot_console
= {
649 .write
= xenboot_write_console
,
650 .setup
= xenboot_setup_console
,
651 .flags
= CON_PRINTBUFFER
| CON_BOOT
| CON_ANYTIME
,
654 #endif /* CONFIG_EARLY_PRINTK */
656 void xen_raw_console_write(const char *str
)
658 ssize_t len
= strlen(str
);
662 rc
= dom0_write_console(0, str
, len
);
663 if (rc
!= -ENOSYS
|| !xen_hvm_domain())
666 xen_hvm_early_write(0, str
, len
);
669 void xen_raw_printk(const char *fmt
, ...)
671 static char buf
[512];
675 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
678 xen_raw_console_write(buf
);
681 static void xenboot_earlycon_write(struct console
*console
,
685 dom0_write_console(0, string
, len
);
688 static int __init
xenboot_earlycon_setup(struct earlycon_device
*device
,
691 device
->con
->write
= xenboot_earlycon_write
;
694 EARLYCON_DECLARE(xenboot
, xenboot_earlycon_setup
);