2 * xen console driver interface to hvc_console.c
4 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/console.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <linux/irq.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/serial_core.h>
31 #include <asm/xen/hypervisor.h>
34 #include <xen/interface/xen.h>
36 #include <xen/grant_table.h>
38 #include <xen/events.h>
39 #include <xen/interface/io/console.h>
40 #include <xen/interface/sched.h>
41 #include <xen/hvc-console.h>
42 #include <xen/xenbus.h>
44 #include "hvc_console.h"
46 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
49 struct list_head list
;
50 struct xenbus_device
*xbdev
;
51 struct xencons_interface
*intf
;
53 struct hvc_struct
*hvc
;
59 static LIST_HEAD(xenconsoles
);
60 static DEFINE_SPINLOCK(xencons_lock
);
62 /* ------------------------------------------------------------------ */
64 static struct xencons_info
*vtermno_to_xencons(int vtermno
)
66 struct xencons_info
*entry
, *n
, *ret
= NULL
;
68 if (list_empty(&xenconsoles
))
71 list_for_each_entry_safe(entry
, n
, &xenconsoles
, list
) {
72 if (entry
->vtermno
== vtermno
) {
81 static inline int xenbus_devid_to_vtermno(int devid
)
83 return devid
+ HVC_COOKIE
;
86 static inline void notify_daemon(struct xencons_info
*cons
)
88 /* Use evtchn: this is called early, before irq is set up. */
89 notify_remote_via_evtchn(cons
->evtchn
);
92 static int __write_console(struct xencons_info
*xencons
,
93 const char *data
, int len
)
95 XENCONS_RING_IDX cons
, prod
;
96 struct xencons_interface
*intf
= xencons
->intf
;
99 cons
= intf
->out_cons
;
100 prod
= intf
->out_prod
;
101 mb(); /* update queue values before going on */
102 BUG_ON((prod
- cons
) > sizeof(intf
->out
));
104 while ((sent
< len
) && ((prod
- cons
) < sizeof(intf
->out
)))
105 intf
->out
[MASK_XENCONS_IDX(prod
++, intf
->out
)] = data
[sent
++];
107 wmb(); /* write ring before updating pointer */
108 intf
->out_prod
= prod
;
111 notify_daemon(xencons
);
115 static int domU_write_console(uint32_t vtermno
, const char *data
, int len
)
118 struct xencons_info
*cons
= vtermno_to_xencons(vtermno
);
123 * Make sure the whole buffer is emitted, polling if
124 * necessary. We don't ever want to rely on the hvc daemon
125 * because the most interesting console output is when the
126 * kernel is crippled.
129 int sent
= __write_console(cons
, data
, len
);
135 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
141 static int domU_read_console(uint32_t vtermno
, char *buf
, int len
)
143 struct xencons_interface
*intf
;
144 XENCONS_RING_IDX cons
, prod
;
146 struct xencons_info
*xencons
= vtermno_to_xencons(vtermno
);
149 intf
= xencons
->intf
;
151 cons
= intf
->in_cons
;
152 prod
= intf
->in_prod
;
153 mb(); /* get pointers before reading ring */
154 BUG_ON((prod
- cons
) > sizeof(intf
->in
));
156 while (cons
!= prod
&& recv
< len
)
157 buf
[recv
++] = intf
->in
[MASK_XENCONS_IDX(cons
++, intf
->in
)];
159 mb(); /* read ring before consuming */
160 intf
->in_cons
= cons
;
162 notify_daemon(xencons
);
166 static const struct hv_ops domU_hvc_ops
= {
167 .get_chars
= domU_read_console
,
168 .put_chars
= domU_write_console
,
169 .notifier_add
= notifier_add_irq
,
170 .notifier_del
= notifier_del_irq
,
171 .notifier_hangup
= notifier_hangup_irq
,
174 static int dom0_read_console(uint32_t vtermno
, char *buf
, int len
)
176 return HYPERVISOR_console_io(CONSOLEIO_read
, len
, buf
);
180 * Either for a dom0 to write to the system console, or a domU with a
181 * debug version of Xen
183 static int dom0_write_console(uint32_t vtermno
, const char *str
, int len
)
185 int rc
= HYPERVISOR_console_io(CONSOLEIO_write
, len
, (char *)str
);
192 static const struct hv_ops dom0_hvc_ops
= {
193 .get_chars
= dom0_read_console
,
194 .put_chars
= dom0_write_console
,
195 .notifier_add
= notifier_add_irq
,
196 .notifier_del
= notifier_del_irq
,
197 .notifier_hangup
= notifier_hangup_irq
,
200 static int xen_hvm_console_init(void)
205 struct xencons_info
*info
;
207 if (!xen_hvm_domain())
210 info
= vtermno_to_xencons(HVC_COOKIE
);
212 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
215 } else if (info
->intf
!= NULL
) {
216 /* already configured */
220 * If the toolstack (or the hypervisor) hasn't set these values, the
221 * default value is 0. Even though gfn = 0 and evtchn = 0 are
222 * theoretically correct values, in practice they never are and they
223 * mean that a legacy toolstack hasn't initialized the pv console correctly.
225 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
230 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_PFN
, &v
);
234 info
->intf
= xen_remap(gfn
<< XEN_PAGE_SHIFT
, XEN_PAGE_SIZE
);
235 if (info
->intf
== NULL
)
237 info
->vtermno
= HVC_COOKIE
;
239 spin_lock(&xencons_lock
);
240 list_add_tail(&info
->list
, &xenconsoles
);
241 spin_unlock(&xencons_lock
);
249 static int xencons_info_pv_init(struct xencons_info
*info
, int vtermno
)
251 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
252 /* GFN == MFN for PV guest */
253 info
->intf
= gfn_to_virt(xen_start_info
->console
.domU
.mfn
);
254 info
->vtermno
= vtermno
;
256 list_add_tail(&info
->list
, &xenconsoles
);
261 static int xen_pv_console_init(void)
263 struct xencons_info
*info
;
265 if (!xen_pv_domain())
268 if (!xen_start_info
->console
.domU
.evtchn
)
271 info
= vtermno_to_xencons(HVC_COOKIE
);
273 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
276 } else if (info
->intf
!= NULL
) {
277 /* already configured */
280 spin_lock(&xencons_lock
);
281 xencons_info_pv_init(info
, HVC_COOKIE
);
282 spin_unlock(&xencons_lock
);
287 static int xen_initial_domain_console_init(void)
289 struct xencons_info
*info
;
291 if (!xen_initial_domain())
294 info
= vtermno_to_xencons(HVC_COOKIE
);
296 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
301 info
->irq
= bind_virq_to_irq(VIRQ_CONSOLE
, 0, false);
302 info
->vtermno
= HVC_COOKIE
;
304 spin_lock(&xencons_lock
);
305 list_add_tail(&info
->list
, &xenconsoles
);
306 spin_unlock(&xencons_lock
);
311 static void xen_console_update_evtchn(struct xencons_info
*info
)
313 if (xen_hvm_domain()) {
317 err
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
321 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
324 void xen_console_resume(void)
326 struct xencons_info
*info
= vtermno_to_xencons(HVC_COOKIE
);
327 if (info
!= NULL
&& info
->irq
) {
328 if (!xen_initial_domain())
329 xen_console_update_evtchn(info
);
330 rebind_evtchn_irq(info
->evtchn
, info
->irq
);
334 #ifdef CONFIG_HVC_XEN_FRONTEND
335 static void xencons_disconnect_backend(struct xencons_info
*info
)
338 unbind_from_irqhandler(info
->irq
, NULL
);
340 if (info
->evtchn
> 0)
341 xenbus_free_evtchn(info
->xbdev
, info
->evtchn
);
343 if (info
->gntref
> 0)
344 gnttab_free_grant_references(info
->gntref
);
346 if (info
->hvc
!= NULL
)
347 hvc_remove(info
->hvc
);
351 static void xencons_free(struct xencons_info
*info
)
353 free_page((unsigned long)info
->intf
);
359 static int xen_console_remove(struct xencons_info
*info
)
361 xencons_disconnect_backend(info
);
362 spin_lock(&xencons_lock
);
363 list_del(&info
->list
);
364 spin_unlock(&xencons_lock
);
365 if (info
->xbdev
!= NULL
)
368 if (xen_hvm_domain())
375 static int xencons_remove(struct xenbus_device
*dev
)
377 return xen_console_remove(dev_get_drvdata(&dev
->dev
));
380 static int xencons_connect_backend(struct xenbus_device
*dev
,
381 struct xencons_info
*info
)
383 int ret
, evtchn
, devid
, ref
, irq
;
384 struct xenbus_transaction xbt
;
385 grant_ref_t gref_head
;
387 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
390 info
->evtchn
= evtchn
;
391 irq
= bind_evtchn_to_irq(evtchn
);
395 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
396 info
->hvc
= hvc_alloc(xenbus_devid_to_vtermno(devid
),
397 irq
, &domU_hvc_ops
, 256);
398 if (IS_ERR(info
->hvc
))
399 return PTR_ERR(info
->hvc
);
400 ret
= gnttab_alloc_grant_references(1, &gref_head
);
403 info
->gntref
= gref_head
;
404 ref
= gnttab_claim_grant_reference(&gref_head
);
407 gnttab_grant_foreign_access_ref(ref
, info
->xbdev
->otherend_id
,
408 virt_to_gfn(info
->intf
), 0);
411 ret
= xenbus_transaction_start(&xbt
);
413 xenbus_dev_fatal(dev
, ret
, "starting transaction");
416 ret
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%d", ref
);
419 ret
= xenbus_printf(xbt
, dev
->nodename
, "port", "%u",
423 ret
= xenbus_transaction_end(xbt
, 0);
427 xenbus_dev_fatal(dev
, ret
, "completing transaction");
431 xenbus_switch_state(dev
, XenbusStateInitialised
);
435 xenbus_transaction_end(xbt
, 1);
436 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
440 static int xencons_probe(struct xenbus_device
*dev
,
441 const struct xenbus_device_id
*id
)
444 struct xencons_info
*info
;
446 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
450 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
453 dev_set_drvdata(&dev
->dev
, info
);
455 info
->vtermno
= xenbus_devid_to_vtermno(devid
);
456 info
->intf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
460 ret
= xencons_connect_backend(dev
, info
);
463 spin_lock(&xencons_lock
);
464 list_add_tail(&info
->list
, &xenconsoles
);
465 spin_unlock(&xencons_lock
);
471 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
473 xencons_disconnect_backend(info
);
478 static int xencons_resume(struct xenbus_device
*dev
)
480 struct xencons_info
*info
= dev_get_drvdata(&dev
->dev
);
482 xencons_disconnect_backend(info
);
483 memset(info
->intf
, 0, XEN_PAGE_SIZE
);
484 return xencons_connect_backend(dev
, info
);
487 static void xencons_backend_changed(struct xenbus_device
*dev
,
488 enum xenbus_state backend_state
)
490 switch (backend_state
) {
491 case XenbusStateReconfiguring
:
492 case XenbusStateReconfigured
:
493 case XenbusStateInitialising
:
494 case XenbusStateInitialised
:
495 case XenbusStateUnknown
:
498 case XenbusStateInitWait
:
501 case XenbusStateConnected
:
502 xenbus_switch_state(dev
, XenbusStateConnected
);
505 case XenbusStateClosed
:
506 if (dev
->state
== XenbusStateClosed
)
508 /* Missed the backend's CLOSING state -- fallthrough */
509 case XenbusStateClosing
:
510 xenbus_frontend_closed(dev
);
515 static const struct xenbus_device_id xencons_ids
[] = {
520 static struct xenbus_driver xencons_driver
= {
521 .name
= "xenconsole",
523 .probe
= xencons_probe
,
524 .remove
= xencons_remove
,
525 .resume
= xencons_resume
,
526 .otherend_changed
= xencons_backend_changed
,
528 #endif /* CONFIG_HVC_XEN_FRONTEND */
530 static int __init
xen_hvc_init(void)
533 struct xencons_info
*info
;
534 const struct hv_ops
*ops
;
539 if (xen_initial_domain()) {
541 r
= xen_initial_domain_console_init();
544 info
= vtermno_to_xencons(HVC_COOKIE
);
547 if (xen_hvm_domain())
548 r
= xen_hvm_console_init();
550 r
= xen_pv_console_init();
554 info
= vtermno_to_xencons(HVC_COOKIE
);
555 info
->irq
= bind_evtchn_to_irq(info
->evtchn
);
558 info
->irq
= 0; /* NO_IRQ */
560 irq_set_noprobe(info
->irq
);
562 info
->hvc
= hvc_alloc(HVC_COOKIE
, info
->irq
, ops
, 256);
563 if (IS_ERR(info
->hvc
)) {
564 r
= PTR_ERR(info
->hvc
);
565 spin_lock(&xencons_lock
);
566 list_del(&info
->list
);
567 spin_unlock(&xencons_lock
);
569 unbind_from_irqhandler(info
->irq
, NULL
);
575 #ifdef CONFIG_HVC_XEN_FRONTEND
576 r
= xenbus_register_frontend(&xencons_driver
);
580 device_initcall(xen_hvc_init
);
582 static int xen_cons_init(void)
584 const struct hv_ops
*ops
;
589 if (xen_initial_domain())
595 if (xen_hvm_domain())
596 r
= xen_hvm_console_init();
598 r
= xen_pv_console_init();
603 hvc_instantiate(HVC_COOKIE
, 0, ops
);
606 console_initcall(xen_cons_init
);
609 static void xen_hvm_early_write(uint32_t vtermno
, const char *str
, int len
)
611 if (xen_cpuid_base())
612 outsb(0xe9, str
, len
);
615 static void xen_hvm_early_write(uint32_t vtermno
, const char *str
, int len
) { }
618 #ifdef CONFIG_EARLY_PRINTK
619 static int __init
xenboot_setup_console(struct console
*console
, char *string
)
621 static struct xencons_info xenboot
;
623 if (xen_initial_domain())
625 if (!xen_pv_domain())
628 return xencons_info_pv_init(&xenboot
, 0);
631 static void xenboot_write_console(struct console
*console
, const char *string
,
634 unsigned int linelen
, off
= 0;
637 if (!xen_pv_domain()) {
638 xen_hvm_early_write(0, string
, len
);
642 dom0_write_console(0, string
, len
);
644 if (xen_initial_domain())
647 domU_write_console(0, "(early) ", 8);
648 while (off
< len
&& NULL
!= (pos
= strchr(string
+off
, '\n'))) {
649 linelen
= pos
-string
+off
;
650 if (off
+ linelen
> len
)
652 domU_write_console(0, string
+off
, linelen
);
653 domU_write_console(0, "\r\n", 2);
657 domU_write_console(0, string
+off
, len
-off
);
660 struct console xenboot_console
= {
662 .write
= xenboot_write_console
,
663 .setup
= xenboot_setup_console
,
664 .flags
= CON_PRINTBUFFER
| CON_BOOT
| CON_ANYTIME
,
667 #endif /* CONFIG_EARLY_PRINTK */
669 void xen_raw_console_write(const char *str
)
671 ssize_t len
= strlen(str
);
675 rc
= dom0_write_console(0, str
, len
);
676 if (rc
!= -ENOSYS
|| !xen_hvm_domain())
679 xen_hvm_early_write(0, str
, len
);
682 void xen_raw_printk(const char *fmt
, ...)
684 static char buf
[512];
688 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
691 xen_raw_console_write(buf
);
694 static void xenboot_earlycon_write(struct console
*console
,
698 dom0_write_console(0, string
, len
);
701 static int __init
xenboot_earlycon_setup(struct earlycon_device
*device
,
704 device
->con
->write
= xenboot_earlycon_write
;
707 EARLYCON_DECLARE(xenboot
, xenboot_earlycon_setup
);