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>
30 #include <asm/xen/hypervisor.h>
33 #include <xen/interface/xen.h>
35 #include <xen/grant_table.h>
37 #include <xen/events.h>
38 #include <xen/interface/io/console.h>
39 #include <xen/interface/sched.h>
40 #include <xen/hvc-console.h>
41 #include <xen/xenbus.h>
43 #include "hvc_console.h"
45 #define HVC_COOKIE 0x58656e /* "Xen" in hex */
48 struct list_head list
;
49 struct xenbus_device
*xbdev
;
50 struct xencons_interface
*intf
;
52 struct hvc_struct
*hvc
;
58 static LIST_HEAD(xenconsoles
);
59 static DEFINE_SPINLOCK(xencons_lock
);
61 /* ------------------------------------------------------------------ */
63 static struct xencons_info
*vtermno_to_xencons(int vtermno
)
65 struct xencons_info
*entry
, *n
, *ret
= NULL
;
67 if (list_empty(&xenconsoles
))
70 list_for_each_entry_safe(entry
, n
, &xenconsoles
, list
) {
71 if (entry
->vtermno
== vtermno
) {
80 static inline int xenbus_devid_to_vtermno(int devid
)
82 return devid
+ HVC_COOKIE
;
85 static inline void notify_daemon(struct xencons_info
*cons
)
87 /* Use evtchn: this is called early, before irq is set up. */
88 notify_remote_via_evtchn(cons
->evtchn
);
91 static int __write_console(struct xencons_info
*xencons
,
92 const char *data
, int len
)
94 XENCONS_RING_IDX cons
, prod
;
95 struct xencons_interface
*intf
= xencons
->intf
;
98 cons
= intf
->out_cons
;
99 prod
= intf
->out_prod
;
100 mb(); /* update queue values before going on */
101 BUG_ON((prod
- cons
) > sizeof(intf
->out
));
103 while ((sent
< len
) && ((prod
- cons
) < sizeof(intf
->out
)))
104 intf
->out
[MASK_XENCONS_IDX(prod
++, intf
->out
)] = data
[sent
++];
106 wmb(); /* write ring before updating pointer */
107 intf
->out_prod
= prod
;
110 notify_daemon(xencons
);
114 static int domU_write_console(uint32_t vtermno
, const char *data
, int len
)
117 struct xencons_info
*cons
= vtermno_to_xencons(vtermno
);
122 * Make sure the whole buffer is emitted, polling if
123 * necessary. We don't ever want to rely on the hvc daemon
124 * because the most interesting console output is when the
125 * kernel is crippled.
128 int sent
= __write_console(cons
, data
, len
);
134 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
140 static int domU_read_console(uint32_t vtermno
, char *buf
, int len
)
142 struct xencons_interface
*intf
;
143 XENCONS_RING_IDX cons
, prod
;
145 struct xencons_info
*xencons
= vtermno_to_xencons(vtermno
);
148 intf
= xencons
->intf
;
150 cons
= intf
->in_cons
;
151 prod
= intf
->in_prod
;
152 mb(); /* get pointers before reading ring */
153 BUG_ON((prod
- cons
) > sizeof(intf
->in
));
155 while (cons
!= prod
&& recv
< len
)
156 buf
[recv
++] = intf
->in
[MASK_XENCONS_IDX(cons
++, intf
->in
)];
158 mb(); /* read ring before consuming */
159 intf
->in_cons
= cons
;
161 notify_daemon(xencons
);
165 static const struct hv_ops domU_hvc_ops
= {
166 .get_chars
= domU_read_console
,
167 .put_chars
= domU_write_console
,
168 .notifier_add
= notifier_add_irq
,
169 .notifier_del
= notifier_del_irq
,
170 .notifier_hangup
= notifier_hangup_irq
,
173 static int dom0_read_console(uint32_t vtermno
, char *buf
, int len
)
175 return HYPERVISOR_console_io(CONSOLEIO_read
, len
, buf
);
179 * Either for a dom0 to write to the system console, or a domU with a
180 * debug version of Xen
182 static int dom0_write_console(uint32_t vtermno
, const char *str
, int len
)
184 int rc
= HYPERVISOR_console_io(CONSOLEIO_write
, len
, (char *)str
);
191 static const struct hv_ops dom0_hvc_ops
= {
192 .get_chars
= dom0_read_console
,
193 .put_chars
= dom0_write_console
,
194 .notifier_add
= notifier_add_irq
,
195 .notifier_del
= notifier_del_irq
,
196 .notifier_hangup
= notifier_hangup_irq
,
199 static int xen_hvm_console_init(void)
204 struct xencons_info
*info
;
206 if (!xen_hvm_domain())
209 info
= vtermno_to_xencons(HVC_COOKIE
);
211 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
214 } else if (info
->intf
!= NULL
) {
215 /* already configured */
219 * If the toolstack (or the hypervisor) hasn't set these values, the
220 * default value is 0. Even though gfn = 0 and evtchn = 0 are
221 * theoretically correct values, in practice they never are and they
222 * mean that a legacy toolstack hasn't initialized the pv console correctly.
224 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
229 r
= hvm_get_parameter(HVM_PARAM_CONSOLE_PFN
, &v
);
233 info
->intf
= xen_remap(gfn
<< XEN_PAGE_SHIFT
, XEN_PAGE_SIZE
);
234 if (info
->intf
== NULL
)
236 info
->vtermno
= HVC_COOKIE
;
238 spin_lock(&xencons_lock
);
239 list_add_tail(&info
->list
, &xenconsoles
);
240 spin_unlock(&xencons_lock
);
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 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
268 /* GFN == MFN for PV guest */
269 info
->intf
= gfn_to_virt(xen_start_info
->console
.domU
.mfn
);
270 info
->vtermno
= HVC_COOKIE
;
272 spin_lock(&xencons_lock
);
273 list_add_tail(&info
->list
, &xenconsoles
);
274 spin_unlock(&xencons_lock
);
279 static int xen_initial_domain_console_init(void)
281 struct xencons_info
*info
;
283 if (!xen_initial_domain())
286 info
= vtermno_to_xencons(HVC_COOKIE
);
288 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
293 info
->irq
= bind_virq_to_irq(VIRQ_CONSOLE
, 0, false);
294 info
->vtermno
= HVC_COOKIE
;
296 spin_lock(&xencons_lock
);
297 list_add_tail(&info
->list
, &xenconsoles
);
298 spin_unlock(&xencons_lock
);
303 static void xen_console_update_evtchn(struct xencons_info
*info
)
305 if (xen_hvm_domain()) {
309 err
= hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN
, &v
);
313 info
->evtchn
= xen_start_info
->console
.domU
.evtchn
;
316 void xen_console_resume(void)
318 struct xencons_info
*info
= vtermno_to_xencons(HVC_COOKIE
);
319 if (info
!= NULL
&& info
->irq
) {
320 if (!xen_initial_domain())
321 xen_console_update_evtchn(info
);
322 rebind_evtchn_irq(info
->evtchn
, info
->irq
);
326 #ifdef CONFIG_HVC_XEN_FRONTEND
327 static void xencons_disconnect_backend(struct xencons_info
*info
)
330 unbind_from_irqhandler(info
->irq
, NULL
);
332 if (info
->evtchn
> 0)
333 xenbus_free_evtchn(info
->xbdev
, info
->evtchn
);
335 if (info
->gntref
> 0)
336 gnttab_free_grant_references(info
->gntref
);
338 if (info
->hvc
!= NULL
)
339 hvc_remove(info
->hvc
);
343 static void xencons_free(struct xencons_info
*info
)
345 free_page((unsigned long)info
->intf
);
351 static int xen_console_remove(struct xencons_info
*info
)
353 xencons_disconnect_backend(info
);
354 spin_lock(&xencons_lock
);
355 list_del(&info
->list
);
356 spin_unlock(&xencons_lock
);
357 if (info
->xbdev
!= NULL
)
360 if (xen_hvm_domain())
367 static int xencons_remove(struct xenbus_device
*dev
)
369 return xen_console_remove(dev_get_drvdata(&dev
->dev
));
372 static int xencons_connect_backend(struct xenbus_device
*dev
,
373 struct xencons_info
*info
)
375 int ret
, evtchn
, devid
, ref
, irq
;
376 struct xenbus_transaction xbt
;
377 grant_ref_t gref_head
;
379 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
382 info
->evtchn
= evtchn
;
383 irq
= bind_evtchn_to_irq(evtchn
);
387 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
388 info
->hvc
= hvc_alloc(xenbus_devid_to_vtermno(devid
),
389 irq
, &domU_hvc_ops
, 256);
390 if (IS_ERR(info
->hvc
))
391 return PTR_ERR(info
->hvc
);
392 ret
= gnttab_alloc_grant_references(1, &gref_head
);
395 info
->gntref
= gref_head
;
396 ref
= gnttab_claim_grant_reference(&gref_head
);
399 gnttab_grant_foreign_access_ref(ref
, info
->xbdev
->otherend_id
,
400 virt_to_gfn(info
->intf
), 0);
403 ret
= xenbus_transaction_start(&xbt
);
405 xenbus_dev_fatal(dev
, ret
, "starting transaction");
408 ret
= xenbus_printf(xbt
, dev
->nodename
, "ring-ref", "%d", ref
);
411 ret
= xenbus_printf(xbt
, dev
->nodename
, "port", "%u",
415 ret
= xenbus_transaction_end(xbt
, 0);
419 xenbus_dev_fatal(dev
, ret
, "completing transaction");
423 xenbus_switch_state(dev
, XenbusStateInitialised
);
427 xenbus_transaction_end(xbt
, 1);
428 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
432 static int xencons_probe(struct xenbus_device
*dev
,
433 const struct xenbus_device_id
*id
)
436 struct xencons_info
*info
;
438 devid
= dev
->nodename
[strlen(dev
->nodename
) - 1] - '0';
442 info
= kzalloc(sizeof(struct xencons_info
), GFP_KERNEL
);
445 dev_set_drvdata(&dev
->dev
, info
);
447 info
->vtermno
= xenbus_devid_to_vtermno(devid
);
448 info
->intf
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
452 ret
= xencons_connect_backend(dev
, info
);
455 spin_lock(&xencons_lock
);
456 list_add_tail(&info
->list
, &xenconsoles
);
457 spin_unlock(&xencons_lock
);
463 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
465 xencons_disconnect_backend(info
);
470 static int xencons_resume(struct xenbus_device
*dev
)
472 struct xencons_info
*info
= dev_get_drvdata(&dev
->dev
);
474 xencons_disconnect_backend(info
);
475 memset(info
->intf
, 0, XEN_PAGE_SIZE
);
476 return xencons_connect_backend(dev
, info
);
479 static void xencons_backend_changed(struct xenbus_device
*dev
,
480 enum xenbus_state backend_state
)
482 switch (backend_state
) {
483 case XenbusStateReconfiguring
:
484 case XenbusStateReconfigured
:
485 case XenbusStateInitialising
:
486 case XenbusStateInitialised
:
487 case XenbusStateUnknown
:
490 case XenbusStateInitWait
:
493 case XenbusStateConnected
:
494 xenbus_switch_state(dev
, XenbusStateConnected
);
497 case XenbusStateClosed
:
498 if (dev
->state
== XenbusStateClosed
)
500 /* Missed the backend's CLOSING state -- fallthrough */
501 case XenbusStateClosing
:
502 xenbus_frontend_closed(dev
);
507 static const struct xenbus_device_id xencons_ids
[] = {
512 static struct xenbus_driver xencons_driver
= {
513 .name
= "xenconsole",
515 .probe
= xencons_probe
,
516 .remove
= xencons_remove
,
517 .resume
= xencons_resume
,
518 .otherend_changed
= xencons_backend_changed
,
520 #endif /* CONFIG_HVC_XEN_FRONTEND */
522 static int __init
xen_hvc_init(void)
525 struct xencons_info
*info
;
526 const struct hv_ops
*ops
;
531 if (xen_initial_domain()) {
533 r
= xen_initial_domain_console_init();
536 info
= vtermno_to_xencons(HVC_COOKIE
);
539 if (xen_hvm_domain())
540 r
= xen_hvm_console_init();
542 r
= xen_pv_console_init();
546 info
= vtermno_to_xencons(HVC_COOKIE
);
547 info
->irq
= bind_evtchn_to_irq(info
->evtchn
);
550 info
->irq
= 0; /* NO_IRQ */
552 irq_set_noprobe(info
->irq
);
554 info
->hvc
= hvc_alloc(HVC_COOKIE
, info
->irq
, ops
, 256);
555 if (IS_ERR(info
->hvc
)) {
556 r
= PTR_ERR(info
->hvc
);
557 spin_lock(&xencons_lock
);
558 list_del(&info
->list
);
559 spin_unlock(&xencons_lock
);
561 unbind_from_irqhandler(info
->irq
, NULL
);
567 #ifdef CONFIG_HVC_XEN_FRONTEND
568 r
= xenbus_register_frontend(&xencons_driver
);
572 device_initcall(xen_hvc_init
);
574 static int xen_cons_init(void)
576 const struct hv_ops
*ops
;
581 if (xen_initial_domain())
587 if (xen_hvm_domain())
588 r
= xen_hvm_console_init();
590 r
= xen_pv_console_init();
595 hvc_instantiate(HVC_COOKIE
, 0, ops
);
598 console_initcall(xen_cons_init
);
600 #ifdef CONFIG_EARLY_PRINTK
601 static void xenboot_write_console(struct console
*console
, const char *string
,
604 unsigned int linelen
, off
= 0;
607 if (!xen_pv_domain())
610 dom0_write_console(0, string
, len
);
612 if (xen_initial_domain())
615 domU_write_console(0, "(early) ", 8);
616 while (off
< len
&& NULL
!= (pos
= strchr(string
+off
, '\n'))) {
617 linelen
= pos
-string
+off
;
618 if (off
+ linelen
> len
)
620 domU_write_console(0, string
+off
, linelen
);
621 domU_write_console(0, "\r\n", 2);
625 domU_write_console(0, string
+off
, len
-off
);
628 struct console xenboot_console
= {
630 .write
= xenboot_write_console
,
631 .flags
= CON_PRINTBUFFER
| CON_BOOT
| CON_ANYTIME
,
634 #endif /* CONFIG_EARLY_PRINTK */
636 void xen_raw_console_write(const char *str
)
638 ssize_t len
= strlen(str
);
642 rc
= dom0_write_console(0, str
, len
);
644 if (rc
== -ENOSYS
&& xen_hvm_domain())
647 } else if (xen_cpuid_base()) {
650 for (i
= 0; i
< len
; i
++)
656 void xen_raw_printk(const char *fmt
, ...)
658 static char buf
[512];
662 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
665 xen_raw_console_write(buf
);