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 xc_cnprobe;
49 static cn_init_t xc_cninit;
50 static cn_term_t xc_cnterm;
51 static cn_getc_t xc_cngetc;
52 static cn_putc_t xc_cnputc;
53 static cn_grab_t xc_cngrab;
54 static cn_ungrab_t xc_cnungrab;
56 #define XC_POLLTIME (hz/10)
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 */
80 #define XCUNIT(x) (dev2unit(x))
81 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
82 #define CN_LOCK_INIT(x, _name) \
83 mtx_init(&x, _name, NULL, MTX_SPIN|MTX_RECURSE)
87 if (panicstr == NULL) \
88 mtx_lock_spin(&(l)); \
90 #define CN_UNLOCK(l) \
92 if (panicstr == NULL) \
93 mtx_unlock_spin(&(l)); \
95 #define CN_LOCK_ASSERT(x) mtx_assert(&x, MA_OWNED)
96 #define CN_LOCK_DESTROY(x) mtx_destroy(&x)
99 static struct tty *xccons;
101 static tsw_open_t xcopen;
102 static tsw_close_t xcclose;
104 static struct ttydevsw xc_ttydevsw = {
105 .tsw_flags = TF_NOPREFIX,
107 .tsw_close = xcclose,
108 .tsw_outwakeup = xcoutwakeup,
112 xc_cnprobe(struct consdev *cp)
114 cp->cn_pri = CN_REMOTE;
115 sprintf(cp->cn_name, "%s0", driver_name);
120 xc_cninit(struct consdev *cp)
122 CN_LOCK_INIT(cn_mtx,"XCONS LOCK");
127 xc_cnterm(struct consdev *cp)
132 xc_cngrab(struct consdev *cp)
137 xc_cnungrab(struct consdev *cp)
142 xc_cngetc(struct consdev *dev)
146 if (xencons_has_input())
147 xencons_handle_input(NULL);
150 if ((rp - rc) && !xc_mute) {
151 /* we need to return only one char */
152 ret = (int)rbuf[RBUF_MASK(rc)];
161 xc_cnputc_domu(struct consdev *dev, int c)
167 xc_cnputc_dom0(struct consdev *dev, int c)
169 HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&c);
173 xc_cnputc(struct consdev *dev, int c)
176 if (xen_start_info->flags & SIF_INITDOMAIN)
177 xc_cnputc_dom0(dev, c);
179 xc_cnputc_domu(dev, c);
182 extern int db_active;
186 int force_flush = xc_mute ||
190 panicstr; /* we're not gonna recover, so force
194 if ((wp-wc) < (WBUF_SIZE-1)) {
195 if ((wbuf[WBUF_MASK(wp++)] = c) == '\n') {
196 wbuf[WBUF_MASK(wp++)] = '\r';
202 } else if (force_flush) {
208 __xencons_tx_flush();
210 /* inform start path that we're pretty full */
211 return ((wp - wc) >= WBUF_SIZE - 100) ? TRUE : FALSE;
215 xc_identify(driver_t *driver, device_t parent)
218 child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
219 device_set_driver(child, driver);
220 device_set_desc(child, "Xen Console");
224 xc_probe(device_t dev)
231 xc_attach(device_t dev)
235 xccons = tty_alloc(&xc_ttydevsw, NULL);
236 tty_makedev(xccons, NULL, "xc%r", 0);
238 callout_init(&xc_callout, 0);
243 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, xccons);
245 if (xen_start_info->flags & SIF_INITDOMAIN) {
246 error = bind_virq_to_irqhandler(
251 xencons_priv_interrupt, NULL,
252 INTR_TYPE_TTY, NULL);
254 KASSERT(error >= 0, ("can't register console interrupt"));
257 /* register handler to flush console on shutdown */
258 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xc_shutdown,
259 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
260 printf("xencons: shutdown event registration failed!\n");
266 * return 0 for all console input, force flush all output.
269 xc_shutdown(void *arg, int howto)
276 xencons_rx(char *buf, unsigned len)
279 struct tty *tp = xccons;
287 for (i = 0; i < len; i++) {
289 kdb_alt_break(buf[i], &xc_altbrk);
291 ttydisc_rint(tp, buf[i], 0);
293 ttydisc_rint_done(tp);
297 for (i = 0; i < len; i++)
298 rbuf[RBUF_MASK(rp++)] = buf[i];
304 __xencons_tx_flush(void)
312 if (sz > (WBUF_SIZE - WBUF_MASK(wc)))
313 sz = WBUF_SIZE - WBUF_MASK(wc);
314 if (xen_start_info->flags & SIF_INITDOMAIN) {
315 HYPERVISOR_console_io(CONSOLEIO_write, sz, &wbuf[WBUF_MASK(wc)]);
318 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
330 __xencons_tx_flush();
334 xencons_priv_interrupt(void *arg)
337 static char rbuf[16];
340 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
347 xcopen(struct tty *tp)
355 xcclose(struct tty *tp)
363 __xencons_put_char(int ch)
366 if ((wp - wc) == WBUF_SIZE)
368 wbuf[WBUF_MASK(wp++)] = _ch;
375 xcoutwakeup(struct tty *tp)
377 boolean_t cons_full = FALSE;
380 while (ttydisc_getc(tp, &c, 1) == 1 && !cons_full)
381 cons_full = xcons_putc(c);
384 /* let the timeout kick us in a bit */
385 xc_start_needed = TRUE;
396 tp = (struct tty *)v;
399 while ((c = xc_cngetc(NULL)) != -1)
400 ttydisc_rint(tp, c, 0);
402 if (xc_start_needed) {
403 xc_start_needed = FALSE;
408 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, tp);
411 static device_method_t xc_methods[] = {
412 DEVMETHOD(device_identify, xc_identify),
413 DEVMETHOD(device_probe, xc_probe),
414 DEVMETHOD(device_attach, xc_attach),
418 static driver_t xc_driver = {
424 /*** Forcibly flush console data before dying. ***/
426 xcons_force_flush(void)
430 if (xen_start_info->flags & SIF_INITDOMAIN)
433 /* Spin until console data is flushed through to the domain controller. */
436 if ((sz = wp - wc) == 0)
439 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
445 DRIVER_MODULE(xc, nexus, xc_driver, xc_devclass, 0, 0);