]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/xen/console/console.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / xen / console / console.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 #include <sys/param.h>
5 #include <sys/module.h>
6 #include <sys/systm.h>
7 #include <sys/consio.h>
8 #include <sys/priv.h>
9 #include <sys/proc.h>
10 #include <sys/uio.h>
11 #include <sys/tty.h>
12 #include <sys/systm.h>
13 #include <sys/taskqueue.h>
14 #include <sys/conf.h>
15 #include <sys/kernel.h>
16 #include <sys/bus.h>
17 #include <machine/stdarg.h>
18 #include <machine/xen/xen-os.h>
19 #include <xen/hypervisor.h>
20 #include <xen/xen_intr.h>
21 #include <sys/cons.h>
22 #include <sys/kdb.h>
23 #include <sys/proc.h>
24
25 #include <dev/xen/console/xencons_ring.h>
26 #include <xen/interface/io/console.h>
27
28
29 #include "opt_ddb.h"
30 #ifdef DDB
31 #include <ddb/ddb.h>
32 #endif
33
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);
40
41 /* switch console so that shutdown can occur gracefully */
42 static void xc_shutdown(void *arg, int howto);
43 static int xc_mute;
44
45 static void xcons_force_flush(void);
46 static void xencons_priv_interrupt(void *);
47
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;
54
55 #define XC_POLLTIME     (hz/10)
56
57 CONS_DRIVER(xc, xccnprobe, xccninit, NULL, xccngetc, 
58             xccncheckc, xccnputc, NULL);
59
60 static int xen_console_up;
61 static boolean_t xc_start_needed;
62 static struct callout xc_callout;
63 struct mtx              cn_mtx;
64
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];
71 static int rc, rp;
72 static unsigned int cnsl_evt_reg;
73 static unsigned int wc, wp; /* write_cons, write_prod */
74
75 #define CDEV_MAJOR 12
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)
80
81 #define CN_LOCK(l)                                                                      \
82                 do {                                                                                    \
83                                 if (panicstr == NULL)                                   \
84                         mtx_lock_spin(&(l));                    \
85                 } while (0)
86 #define CN_UNLOCK(l)                                                            \
87                 do {                                                                                    \
88                                 if (panicstr == NULL)                                   \
89                         mtx_unlock_spin(&(l));                  \
90                 } while (0)
91 #define CN_LOCK_ASSERT(x)    mtx_assert(&x, MA_OWNED)
92 #define CN_LOCK_DESTROY(x)   mtx_destroy(&x)
93
94
95 static struct tty *xccons;
96
97 static tsw_open_t       xcopen;
98 static tsw_close_t      xcclose;
99
100 static struct ttydevsw xc_ttydevsw = {
101         .tsw_flags      = TF_NOPREFIX,
102         .tsw_open       = xcopen,
103         .tsw_close      = xcclose,
104         .tsw_outwakeup  = xcoutwakeup,
105 };
106
107 static void
108 xccnprobe(struct consdev *cp)
109 {
110         cp->cn_pri = CN_REMOTE;
111         sprintf(cp->cn_name, "%s0", driver_name);
112 }
113
114
115 static void
116 xccninit(struct consdev *cp)
117
118         CN_LOCK_INIT(cn_mtx,"XCONS LOCK");
119
120 }
121 int
122 xccngetc(struct consdev *dev)
123 {
124         int c;
125         if (xc_mute)
126                 return 0;
127         do {
128                 if ((c = xccncheckc(dev)) == -1) {
129 #ifdef KDB
130                         if (!kdb_active)
131 #endif
132                                 /*
133                                  * Polling without sleeping in Xen
134                                  * doesn't work well.  Sleeping gives
135                                  * other things like clock a chance to
136                                  * run
137                                  */
138                                 tsleep(&cn_mtx, PWAIT | PCATCH,
139                                     "console sleep", XC_POLLTIME);
140                 }
141         } while(c == -1);
142         return c;
143 }
144
145 int
146 xccncheckc(struct consdev *dev)
147 {
148         int ret = (xc_mute ? 0 : -1);
149
150         if (xencons_has_input())
151                 xencons_handle_input(NULL);
152         
153         CN_LOCK(cn_mtx);
154         if ((rp - rc)) {
155                 /* we need to return only one char */
156                 ret = (int)rbuf[RBUF_MASK(rc)];
157                 rc++;
158         }
159         CN_UNLOCK(cn_mtx);
160         return(ret);
161 }
162
163 static void
164 xccnputc(struct consdev *dev, int c)
165 {
166         xcons_putc(c);
167 }
168
169 static void
170 xccnputc_dom0(struct consdev *dev, int c)
171 {
172         HYPERVISOR_console_io(CONSOLEIO_write, 1, (char *)&c);
173 }
174
175 extern int db_active;
176 static boolean_t
177 xcons_putc(int c)
178 {
179         int force_flush = xc_mute ||
180 #ifdef DDB
181                 db_active ||
182 #endif
183                 panicstr;       /* we're not gonna recover, so force
184                                  * flush 
185                                  */
186
187         if ((wp-wc) < (WBUF_SIZE-1)) {
188                 if ((wbuf[WBUF_MASK(wp++)] = c) == '\n') {
189                         wbuf[WBUF_MASK(wp++)] = '\r';
190 #ifdef notyet
191                         if (force_flush)
192                                 xcons_force_flush();
193 #endif
194                 }
195         } else if (force_flush) {
196 #ifdef notyet
197                 xcons_force_flush();
198 #endif          
199         }
200         if (cnsl_evt_reg)
201                 __xencons_tx_flush();
202         
203         /* inform start path that we're pretty full */
204         return ((wp - wc) >= WBUF_SIZE - 100) ? TRUE : FALSE;
205 }
206
207 static void
208 xc_identify(driver_t *driver, device_t parent)
209 {
210         device_t child;
211         child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
212         device_set_driver(child, driver);
213         device_set_desc(child, "Xen Console");
214 }
215
216 static int
217 xc_probe(device_t dev)
218 {
219
220         return (0);
221 }
222
223 static int
224 xc_attach(device_t dev) 
225 {
226         int error;
227
228         if (xen_start_info->flags & SIF_INITDOMAIN) {
229                 xc_consdev.cn_putc = xccnputc_dom0;
230         } 
231
232         xccons = tty_alloc(&xc_ttydevsw, NULL);
233         tty_makedev(xccons, NULL, "xc%r", 0);
234
235         callout_init(&xc_callout, 0);
236
237         xencons_ring_init();
238
239         cnsl_evt_reg = 1;
240         callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, xccons);
241     
242         if (xen_start_info->flags & SIF_INITDOMAIN) {
243                         error = bind_virq_to_irqhandler(
244                                  VIRQ_CONSOLE,
245                                  0,
246                                  "console",
247                                  NULL,
248                                  xencons_priv_interrupt, NULL,
249                                  INTR_TYPE_TTY, NULL);
250                 
251                                 KASSERT(error >= 0, ("can't register console interrupt"));
252         }
253
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");
258         
259         return (0);
260 }
261
262 /*
263  * return 0 for all console input, force flush all output.
264  */
265 static void
266 xc_shutdown(void *arg, int howto)
267 {
268         xc_mute = 1;
269         xcons_force_flush();
270 }
271
272 void 
273 xencons_rx(char *buf, unsigned len)
274 {
275         int           i;
276         struct tty *tp = xccons;
277
278         if (xen_console_up
279 #ifdef DDB
280             && !kdb_active
281 #endif
282                 ) {
283                 tty_lock(tp);
284                 for (i = 0; i < len; i++)
285                         ttydisc_rint(tp, buf[i], 0);
286                 ttydisc_rint_done(tp);
287                 tty_unlock(tp);
288         } else {
289                 CN_LOCK(cn_mtx);
290                 for (i = 0; i < len; i++)
291                         rbuf[RBUF_MASK(rp++)] = buf[i];
292                 CN_UNLOCK(cn_mtx);
293         }
294 }
295
296 static void 
297 __xencons_tx_flush(void)
298 {
299         int        sz;
300
301         CN_LOCK(cn_mtx);
302         while (wc != wp) {
303                 int sent;
304                 sz = wp - wc;
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)]);
309                         wc += sz;
310                 } else {
311                         sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
312                         if (sent == 0) 
313                                 break;
314                         wc += sent;
315                 }
316         }
317         CN_UNLOCK(cn_mtx);
318 }
319
320 void
321 xencons_tx(void)
322 {
323         __xencons_tx_flush();
324 }
325
326 static void
327 xencons_priv_interrupt(void *arg)
328 {
329
330         static char rbuf[16];
331         int         l;
332
333         while ((l = HYPERVISOR_console_io(CONSOLEIO_read, 16, rbuf)) > 0)
334                 xencons_rx(rbuf, l);
335
336         xencons_tx();
337 }
338
339 static int
340 xcopen(struct tty *tp)
341 {
342
343         xen_console_up = 1;
344         return (0);
345 }
346
347 static void
348 xcclose(struct tty *tp)
349 {
350
351         xen_console_up = 0;
352 }
353
354 static inline int 
355 __xencons_put_char(int ch)
356 {
357         char _ch = (char)ch;
358         if ((wp - wc) == WBUF_SIZE)
359                 return 0;
360         wbuf[WBUF_MASK(wp++)] = _ch;
361         return 1;
362 }
363
364
365 static void
366 xcoutwakeup(struct tty *tp)
367 {
368         boolean_t cons_full = FALSE;
369         char c;
370
371         while (ttydisc_getc(tp, &c, 1) == 1 && !cons_full)
372                 cons_full = xcons_putc(c);
373
374         if (cons_full) {
375                 /* let the timeout kick us in a bit */
376                 xc_start_needed = TRUE;
377         }
378
379 }
380
381 static void
382 xc_timeout(void *v)
383 {
384         struct  tty *tp;
385         int     c;
386
387         tp = (struct tty *)v;
388
389         tty_lock(tp);
390         while ((c = xccncheckc(NULL)) != -1)
391                 ttydisc_rint(tp, c, 0);
392
393         if (xc_start_needed) {
394                 xc_start_needed = FALSE;
395                 xcoutwakeup(tp);
396         }
397         tty_unlock(tp);
398
399         callout_reset(&xc_callout, XC_POLLTIME, xc_timeout, tp);
400 }
401
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),
406         {0, 0}
407 };
408
409 static driver_t xc_driver = {
410         driver_name,
411         xc_methods,
412         0,
413 };
414
415 /*** Forcibly flush console data before dying. ***/
416 void 
417 xcons_force_flush(void)
418 {
419         int        sz;
420
421         if (xen_start_info->flags & SIF_INITDOMAIN)
422                 return;
423
424         /* Spin until console data is flushed through to the domain controller. */
425         while (wc != wp) {
426                 int sent = 0;
427                 if ((sz = wp - wc) == 0)
428                         continue;
429                 
430                 sent = xencons_ring_send(&wbuf[WBUF_MASK(wc)], sz);
431                 if (sent > 0)
432                         wc += sent;             
433         }
434 }
435
436 DRIVER_MODULE(xc, nexus, xc_driver, xc_devclass, 0, 0);