5 #include <sys/module.h>
7 #include <sys/consio.h>
12 #include <sys/systm.h>
13 #include <sys/taskqueue.h>
15 #include <sys/kernel.h>
17 #include <machine/stdarg.h>
18 #include <machine/xen/xen-os.h>
19 #include <xen/hypervisor.h>
20 #include <xen/xen_intr.h>
25 #include <dev/xen/console/xencons_ring.h>
26 #include <xen/interface/io/console.h>
34 static char driver_name[] = "xc";
35 devclass_t xc_devclass; /* do not make static */
36 static void xcoutwakeup(struct tty *);
37 static void xc_timeout(void *);
38 static void __xencons_tx_flush(void);
39 static boolean_t xcons_putc(int c);
41 /* switch console so that shutdown can occur gracefully */
42 static void xc_shutdown(void *arg, int howto);
45 static void xcons_force_flush(void);
46 static void xencons_priv_interrupt(void *);
48 static cn_probe_t xccnprobe;
49 static cn_init_t xccninit;
50 static cn_getc_t xccngetc;
51 static cn_putc_t xccnputc;
52 static cn_putc_t xccnputc_dom0;
53 static cn_checkc_t xccncheckc;
55 #define XC_POLLTIME (hz/10)
57 CONS_DRIVER(xc, xccnprobe, xccninit, NULL, xccngetc,
58 xccncheckc, xccnputc, NULL);
60 static int xen_console_up;
61 static boolean_t xc_start_needed;
62 static struct callout xc_callout;
65 #define RBUF_SIZE 1024
66 #define RBUF_MASK(_i) ((_i)&(RBUF_SIZE-1))
67 #define WBUF_SIZE 4096
68 #define WBUF_MASK(_i) ((_i)&(WBUF_SIZE-1))
69 static char wbuf[WBUF_SIZE];
70 static char rbuf[RBUF_SIZE];
72 static unsigned int cnsl_evt_reg;
73 static unsigned int wc, wp; /* write_cons, write_prod */
76 #define XCUNIT(x) (dev2unit(x))
77 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
78 #define CN_LOCK_INIT(x, _name) \
79 mtx_init(&x, _name, NULL, MTX_SPIN|MTX_RECURSE)
83 if (panicstr == NULL) \
84 mtx_lock_spin(&(l)); \
86 #define CN_UNLOCK(l) \
88 if (panicstr == NULL) \
89 mtx_unlock_spin(&(l)); \
91 #define CN_LOCK_ASSERT(x) mtx_assert(&x, MA_OWNED)
92 #define CN_LOCK_DESTROY(x) mtx_destroy(&x)
95 static struct tty *xccons;
97 static tsw_open_t xcopen;
98 static tsw_close_t xcclose;
100 static struct ttydevsw xc_ttydevsw = {
101 .tsw_flags = TF_NOPREFIX,
103 .tsw_close = xcclose,
104 .tsw_outwakeup = xcoutwakeup,
108 xccnprobe(struct consdev *cp)
110 cp->cn_pri = CN_REMOTE;
111 sprintf(cp->cn_name, "%s0", driver_name);
116 xccninit(struct consdev *cp)
118 CN_LOCK_INIT(cn_mtx,"XCONS LOCK");
122 xccngetc(struct consdev *dev)
128 if ((c = xccncheckc(dev)) == -1) {
133 * Polling without sleeping in Xen
134 * doesn't work well. Sleeping gives
135 * other things like clock a chance to
138 tsleep(&cn_mtx, PWAIT | PCATCH,
139 "console sleep", XC_POLLTIME);
146 xccncheckc(struct consdev *dev)
148 int ret = (xc_mute ? 0 : -1);
150 if (xencons_has_input())
151 xencons_handle_input(NULL);
155 /* we need to return only one char */
156 ret = (int)rbuf[RBUF_MASK(rc)];
164 xccnputc(struct consdev *dev, int c)
170 xccnputc_dom0(struct consdev *dev, int c)
172 HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&c);
175 extern int db_active;
179 int force_flush = xc_mute ||
183 panicstr; /* we're not gonna recover, so force
187 if ((wp-wc) < (WBUF_SIZE-1)) {
188 if ((wbuf[WBUF_MASK(wp++)] = c) == '\n') {
189 wbuf[WBUF_MASK(wp++)] = '\r';
195 } else if (force_flush) {
201 __xencons_tx_flush();
203 /* inform start path that we're pretty full */
204 return ((wp - wc) >= WBUF_SIZE - 100) ? TRUE : FALSE;
208 xc_identify(driver_t *driver, device_t parent)
211 child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
212 device_set_driver(child, driver);
213 device_set_desc(child, "Xen Console");
217 xc_probe(device_t dev)
224 xc_attach(device_t dev)
228 if (xen_start_info->flags & SIF_INITDOMAIN) {
229 xc_consdev.cn_putc = xccnputc_dom0;
232 xccons = tty_alloc(&xc_ttydevsw, NULL);
233 tty_makedev(xccons, NULL, "xc%r", 0);
235 callout_init(&xc_callout, 0);
240 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, xccons);
242 if (xen_start_info->flags & SIF_INITDOMAIN) {
243 error = bind_virq_to_irqhandler(
248 xencons_priv_interrupt, NULL,
249 INTR_TYPE_TTY, NULL);
251 KASSERT(error >= 0, ("can't register console interrupt"));
254 /* register handler to flush console on shutdown */
255 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xc_shutdown,
256 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
257 printf("xencons: shutdown event registration failed!\n");
263 * return 0 for all console input, force flush all output.
266 xc_shutdown(void *arg, int howto)
273 xencons_rx(char *buf, unsigned len)
276 struct tty *tp = xccons;
284 for (i = 0; i < len; i++)
285 ttydisc_rint(tp, buf[i], 0);
286 ttydisc_rint_done(tp);
290 for (i = 0; i < len; i++)
291 rbuf[RBUF_MASK(rp++)] = buf[i];
297 __xencons_tx_flush(void)
305 if (sz > (WBUF_SIZE - WBUF_MASK(wc)))
306 sz = WBUF_SIZE - WBUF_MASK(wc);
307 if (xen_start_info->flags & SIF_INITDOMAIN) {
308 HYPERVISOR_console_io(CONSOLEIO_write, sz, &wbuf[WBUF_MASK(wc)]);
311 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
323 __xencons_tx_flush();
327 xencons_priv_interrupt(void *arg)
330 static char rbuf[16];
333 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
340 xcopen(struct tty *tp)
348 xcclose(struct tty *tp)
355 __xencons_put_char(int ch)
358 if ((wp - wc) == WBUF_SIZE)
360 wbuf[WBUF_MASK(wp++)] = _ch;
366 xcoutwakeup(struct tty *tp)
368 boolean_t cons_full = FALSE;
371 while (ttydisc_getc(tp, &c, 1) == 1 && !cons_full)
372 cons_full = xcons_putc(c);
375 /* let the timeout kick us in a bit */
376 xc_start_needed = TRUE;
387 tp = (struct tty *)v;
390 while ((c = xccncheckc(NULL)) != -1)
391 ttydisc_rint(tp, c, 0);
393 if (xc_start_needed) {
394 xc_start_needed = FALSE;
399 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, tp);
402 static device_method_t xc_methods[] = {
403 DEVMETHOD(device_identify, xc_identify),
404 DEVMETHOD(device_probe, xc_probe),
405 DEVMETHOD(device_attach, xc_attach),
409 static driver_t xc_driver = {
415 /*** Forcibly flush console data before dying. ***/
417 xcons_force_flush(void)
421 if (xen_start_info->flags & SIF_INITDOMAIN)
424 /* Spin until console data is flushed through to the domain controller. */
427 if ((sz = wp - wc) == 0)
430 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
436 DRIVER_MODULE(xc, nexus, xc_driver, xc_devclass, 0, 0);