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)
150 if (xencons_has_input())
151 xencons_handle_input(NULL);
154 if ((rp - rc) && !xc_mute) {
155 /* we need to return only one char */
156 ret = (int)rbuf[RBUF_MASK(rc)];
165 xccnputc(struct consdev *dev, int c)
171 xccnputc_dom0(struct consdev *dev, int c)
173 HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&c);
176 extern int db_active;
180 int force_flush = xc_mute ||
184 panicstr; /* we're not gonna recover, so force
188 if ((wp-wc) < (WBUF_SIZE-1)) {
189 if ((wbuf[WBUF_MASK(wp++)] = c) == '\n') {
190 wbuf[WBUF_MASK(wp++)] = '\r';
196 } else if (force_flush) {
202 __xencons_tx_flush();
204 /* inform start path that we're pretty full */
205 return ((wp - wc) >= WBUF_SIZE - 100) ? TRUE : FALSE;
209 xc_identify(driver_t *driver, device_t parent)
212 child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
213 device_set_driver(child, driver);
214 device_set_desc(child, "Xen Console");
218 xc_probe(device_t dev)
225 xc_attach(device_t dev)
229 if (xen_start_info->flags & SIF_INITDOMAIN) {
230 xc_consdev.cn_putc = xccnputc_dom0;
233 xccons = tty_alloc(&xc_ttydevsw, NULL);
234 tty_makedev(xccons, NULL, "xc%r", 0);
236 callout_init(&xc_callout, 0);
241 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, xccons);
243 if (xen_start_info->flags & SIF_INITDOMAIN) {
244 error = bind_virq_to_irqhandler(
249 xencons_priv_interrupt, NULL,
250 INTR_TYPE_TTY, NULL);
252 KASSERT(error >= 0, ("can't register console interrupt"));
255 /* register handler to flush console on shutdown */
256 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xc_shutdown,
257 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
258 printf("xencons: shutdown event registration failed!\n");
264 * return 0 for all console input, force flush all output.
267 xc_shutdown(void *arg, int howto)
274 xencons_rx(char *buf, unsigned len)
277 struct tty *tp = xccons;
285 for (i = 0; i < len; i++)
286 ttydisc_rint(tp, buf[i], 0);
287 ttydisc_rint_done(tp);
291 for (i = 0; i < len; i++)
292 rbuf[RBUF_MASK(rp++)] = buf[i];
298 __xencons_tx_flush(void)
306 if (sz > (WBUF_SIZE - WBUF_MASK(wc)))
307 sz = WBUF_SIZE - WBUF_MASK(wc);
308 if (xen_start_info->flags & SIF_INITDOMAIN) {
309 HYPERVISOR_console_io(CONSOLEIO_write, sz, &wbuf[WBUF_MASK(wc)]);
312 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
324 __xencons_tx_flush();
328 xencons_priv_interrupt(void *arg)
331 static char rbuf[16];
334 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
341 xcopen(struct tty *tp)
349 xcclose(struct tty *tp)
356 __xencons_put_char(int ch)
359 if ((wp - wc) == WBUF_SIZE)
361 wbuf[WBUF_MASK(wp++)] = _ch;
367 xcoutwakeup(struct tty *tp)
369 boolean_t cons_full = FALSE;
372 while (ttydisc_getc(tp, &c, 1) == 1 && !cons_full)
373 cons_full = xcons_putc(c);
376 /* let the timeout kick us in a bit */
377 xc_start_needed = TRUE;
388 tp = (struct tty *)v;
391 while ((c = xccncheckc(NULL)) != -1)
392 ttydisc_rint(tp, c, 0);
394 if (xc_start_needed) {
395 xc_start_needed = FALSE;
400 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, tp);
403 static device_method_t xc_methods[] = {
404 DEVMETHOD(device_identify, xc_identify),
405 DEVMETHOD(device_probe, xc_probe),
406 DEVMETHOD(device_attach, xc_attach),
410 static driver_t xc_driver = {
416 /*** Forcibly flush console data before dying. ***/
418 xcons_force_flush(void)
422 if (xen_start_info->flags & SIF_INITDOMAIN)
425 /* Spin until console data is flushed through to the domain controller. */
428 if ((sz = wp - wc) == 0)
431 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
437 DRIVER_MODULE(xc, nexus, xc_driver, xc_devclass, 0, 0);