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;
54 #define XC_POLLTIME (hz/10)
58 static int xen_console_up;
59 static boolean_t xc_start_needed;
60 static struct callout xc_callout;
63 #define RBUF_SIZE 1024
64 #define RBUF_MASK(_i) ((_i)&(RBUF_SIZE-1))
65 #define WBUF_SIZE 4096
66 #define WBUF_MASK(_i) ((_i)&(WBUF_SIZE-1))
67 static char wbuf[WBUF_SIZE];
68 static char rbuf[RBUF_SIZE];
70 static unsigned int cnsl_evt_reg;
71 static unsigned int wc, wp; /* write_cons, write_prod */
78 #define XCUNIT(x) (dev2unit(x))
79 #define ISTTYOPEN(tp) ((tp) && ((tp)->t_state & TS_ISOPEN))
80 #define CN_LOCK_INIT(x, _name) \
81 mtx_init(&x, _name, NULL, MTX_SPIN|MTX_RECURSE)
85 if (panicstr == NULL) \
86 mtx_lock_spin(&(l)); \
88 #define CN_UNLOCK(l) \
90 if (panicstr == NULL) \
91 mtx_unlock_spin(&(l)); \
93 #define CN_LOCK_ASSERT(x) mtx_assert(&x, MA_OWNED)
94 #define CN_LOCK_DESTROY(x) mtx_destroy(&x)
97 static struct tty *xccons;
99 static tsw_open_t xcopen;
100 static tsw_close_t xcclose;
102 static struct ttydevsw xc_ttydevsw = {
103 .tsw_flags = TF_NOPREFIX,
105 .tsw_close = xcclose,
106 .tsw_outwakeup = xcoutwakeup,
110 xc_cnprobe(struct consdev *cp)
112 cp->cn_pri = CN_REMOTE;
113 sprintf(cp->cn_name, "%s0", driver_name);
118 xc_cninit(struct consdev *cp)
120 CN_LOCK_INIT(cn_mtx,"XCONS LOCK");
125 xc_cnterm(struct consdev *cp)
130 xc_cngetc(struct consdev *dev)
134 if (xencons_has_input())
135 xencons_handle_input(NULL);
138 if ((rp - rc) && !xc_mute) {
139 /* we need to return only one char */
140 ret = (int)rbuf[RBUF_MASK(rc)];
149 xc_cnputc_domu(struct consdev *dev, int c)
155 xc_cnputc_dom0(struct consdev *dev, int c)
157 HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&c);
161 xc_cnputc(struct consdev *dev, int c)
164 if (xen_start_info->flags & SIF_INITDOMAIN)
165 xc_cnputc_dom0(dev, c);
167 xc_cnputc_domu(dev, c);
170 extern int db_active;
174 int force_flush = xc_mute ||
178 panicstr; /* we're not gonna recover, so force
182 if ((wp-wc) < (WBUF_SIZE-1)) {
183 if ((wbuf[WBUF_MASK(wp++)] = c) == '\n') {
184 wbuf[WBUF_MASK(wp++)] = '\r';
190 } else if (force_flush) {
196 __xencons_tx_flush();
198 /* inform start path that we're pretty full */
199 return ((wp - wc) >= WBUF_SIZE - 100) ? TRUE : FALSE;
203 xc_identify(driver_t *driver, device_t parent)
206 child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
207 device_set_driver(child, driver);
208 device_set_desc(child, "Xen Console");
212 xc_probe(device_t dev)
219 xc_attach(device_t dev)
223 xccons = tty_alloc(&xc_ttydevsw, NULL);
224 tty_makedev(xccons, NULL, "xc%r", 0);
226 callout_init(&xc_callout, 0);
231 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, xccons);
233 if (xen_start_info->flags & SIF_INITDOMAIN) {
234 error = bind_virq_to_irqhandler(
239 xencons_priv_interrupt, NULL,
240 INTR_TYPE_TTY, NULL);
242 KASSERT(error >= 0, ("can't register console interrupt"));
245 /* register handler to flush console on shutdown */
246 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xc_shutdown,
247 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
248 printf("xencons: shutdown event registration failed!\n");
254 * return 0 for all console input, force flush all output.
257 xc_shutdown(void *arg, int howto)
264 xencons_rx(char *buf, unsigned len)
267 struct tty *tp = xccons;
275 for (i = 0; i < len; i++) {
277 kdb_alt_break(buf[i], &xc_altbrk);
279 ttydisc_rint(tp, buf[i], 0);
281 ttydisc_rint_done(tp);
285 for (i = 0; i < len; i++)
286 rbuf[RBUF_MASK(rp++)] = buf[i];
292 __xencons_tx_flush(void)
300 if (sz > (WBUF_SIZE - WBUF_MASK(wc)))
301 sz = WBUF_SIZE - WBUF_MASK(wc);
302 if (xen_start_info->flags & SIF_INITDOMAIN) {
303 HYPERVISOR_console_io(CONSOLEIO_write, sz, &wbuf[WBUF_MASK(wc)]);
306 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
318 __xencons_tx_flush();
322 xencons_priv_interrupt(void *arg)
325 static char rbuf[16];
328 while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
335 xcopen(struct tty *tp)
343 xcclose(struct tty *tp)
350 __xencons_put_char(int ch)
353 if ((wp - wc) == WBUF_SIZE)
355 wbuf[WBUF_MASK(wp++)] = _ch;
361 xcoutwakeup(struct tty *tp)
363 boolean_t cons_full = FALSE;
366 while (ttydisc_getc(tp, &c, 1) == 1 && !cons_full)
367 cons_full = xcons_putc(c);
370 /* let the timeout kick us in a bit */
371 xc_start_needed = TRUE;
382 tp = (struct tty *)v;
385 while ((c = xc_cngetc(NULL)) != -1)
386 ttydisc_rint(tp, c, 0);
388 if (xc_start_needed) {
389 xc_start_needed = FALSE;
394 callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, tp);
397 static device_method_t xc_methods[] = {
398 DEVMETHOD(device_identify, xc_identify),
399 DEVMETHOD(device_probe, xc_probe),
400 DEVMETHOD(device_attach, xc_attach),
404 static driver_t xc_driver = {
410 /*** Forcibly flush console data before dying. ***/
412 xcons_force_flush(void)
416 if (xen_start_info->flags & SIF_INITDOMAIN)
419 /* Spin until console data is flushed through to the domain controller. */
422 if ((sz = wp - wc) == 0)
425 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
431 DRIVER_MODULE(xc, nexus, xc_driver, xc_devclass, 0, 0);