1 /*- 2 * Copyright (C) 2003,2004 3 * Hidetoshi Shimokawa. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * 16 * This product includes software developed by Hidetoshi Shimokawa. 17 * 18 * 4. Neither the name of the author nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/kdb.h> 39 #include <gdb/gdb.h> 40 #include <sys/kernel.h> 41 #include <sys/module.h> 42 #include <sys/systm.h> 43 #include <sys/types.h> 44 #include <sys/conf.h> 45 #include <sys/cons.h> 46 #include <sys/consio.h> 47 #include <sys/tty.h> 48 #include <sys/malloc.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/ucred.h> 52 53 #include <machine/bus.h> 54 55 #include <dev/dcons/dcons.h> 56 #include <dev/dcons/dcons_os.h> 57 58 #include <ddb/ddb.h> 59 #include <sys/reboot.h> 60 61 #include <sys/sysctl.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_param.h> 65 #include <vm/pmap.h> 66 67 #include "opt_dcons.h" 68 #include "opt_kdb.h" 69 #include "opt_gdb.h" 70 #include "opt_ddb.h" 71 72 73 #ifndef DCONS_POLL_HZ 74 #define DCONS_POLL_HZ 25 75 #endif 76 77 #ifndef DCONS_BUF_SIZE 78 #define DCONS_BUF_SIZE (16*1024) 79 #endif 80 81 #ifndef DCONS_FORCE_CONSOLE 82 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 83 #endif 84 85 #ifndef KLD_MODULE 86 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 87 #endif 88 89 /* global data */ 90 static struct dcons_global dg; 91 struct dcons_global *dcons_conf; 92 static int poll_hz = DCONS_POLL_HZ; 93 94 static struct dcons_softc sc[DCONS_NPORT]; 95 96 static SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 97 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 98 "dcons polling rate"); 99 100 static int drv_init = 0; 101 static struct callout dcons_callout; 102 struct dcons_buf *dcons_buf; /* for local dconschat */ 103 104 static void dcons_timeout(void *); 105 static int dcons_drv_init(int); 106 107 static cn_probe_t dcons_cnprobe; 108 static cn_init_t dcons_cninit; 109 static cn_term_t dcons_cnterm; 110 static cn_getc_t dcons_cngetc; 111 static cn_putc_t dcons_cnputc; 112 static cn_grab_t dcons_cngrab; 113 static cn_ungrab_t dcons_cnungrab; 114 115 CONSOLE_DRIVER(dcons); 116 117 #if defined(GDB) 118 static gdb_probe_f dcons_dbg_probe; 119 static gdb_init_f dcons_dbg_init; 120 static gdb_term_f dcons_dbg_term; 121 static gdb_getc_f dcons_dbg_getc; 122 static gdb_putc_f dcons_dbg_putc; 123 124 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 125 dcons_dbg_getc, dcons_dbg_putc); 126 127 extern struct gdb_dbgport *gdb_cur; 128 #endif 129 130 static tsw_outwakeup_t dcons_outwakeup; 131 132 static struct ttydevsw dcons_ttydevsw = { 133 .tsw_flags = TF_NOPREFIX, 134 .tsw_outwakeup = dcons_outwakeup, 135 }; 136 137 #if (defined(GDB) || defined(DDB)) 138 static int 139 dcons_check_break(struct dcons_softc *dc, int c) 140 { 141 142 if (c < 0) 143 return (c); 144 145 #ifdef GDB 146 if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport) 147 kdb_alt_break_gdb(c, &dc->brk_state); 148 else 149 #endif 150 kdb_alt_break(c, &dc->brk_state); 151 152 return (c); 153 } 154 #else 155 #define dcons_check_break(dc, c) (c) 156 #endif 157 158 static int 159 dcons_os_checkc_nopoll(struct dcons_softc *dc) 160 { 161 int c; 162 163 if (dg.dma_tag != NULL) 164 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 165 166 c = dcons_check_break(dc, dcons_checkc(dc)); 167 168 if (dg.dma_tag != NULL) 169 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 170 171 return (c); 172 } 173 174 static int 175 dcons_os_checkc(struct dcons_softc *dc) 176 { 177 EVENTHANDLER_INVOKE(dcons_poll, 0); 178 return (dcons_os_checkc_nopoll(dc)); 179 } 180 181 static void 182 dcons_os_putc(struct dcons_softc *dc, int c) 183 { 184 if (dg.dma_tag != NULL) 185 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 186 187 dcons_putc(dc, c); 188 189 if (dg.dma_tag != NULL) 190 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 191 } 192 193 static void 194 dcons_outwakeup(struct tty *tp) 195 { 196 struct dcons_softc *dc; 197 char ch; 198 199 dc = tty_softc(tp); 200 201 while (ttydisc_getc(tp, &ch, sizeof ch) != 0) 202 dcons_os_putc(dc, ch); 203 } 204 205 static void 206 dcons_timeout(void *v) 207 { 208 struct tty *tp; 209 struct dcons_softc *dc; 210 int i, c, polltime; 211 212 for (i = 0; i < DCONS_NPORT; i ++) { 213 dc = &sc[i]; 214 tp = dc->tty; 215 216 tty_lock(tp); 217 while ((c = dcons_os_checkc_nopoll(dc)) != -1) 218 ttydisc_rint(tp, c, 0); 219 ttydisc_rint_done(tp); 220 tty_unlock(tp); 221 } 222 polltime = hz / poll_hz; 223 if (polltime < 1) 224 polltime = 1; 225 callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 226 } 227 228 static void 229 dcons_cnprobe(struct consdev *cp) 230 { 231 sprintf(cp->cn_name, "dcons"); 232 #if DCONS_FORCE_CONSOLE 233 cp->cn_pri = CN_REMOTE; 234 #else 235 cp->cn_pri = CN_NORMAL; 236 #endif 237 } 238 239 static void 240 dcons_cninit(struct consdev *cp) 241 { 242 dcons_drv_init(0); 243 cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 244 } 245 246 static void 247 dcons_cnterm(struct consdev *cp) 248 { 249 } 250 251 static void 252 dcons_cngrab(struct consdev *cp) 253 { 254 } 255 256 static void 257 dcons_cnungrab(struct consdev *cp) 258 { 259 } 260 261 static int 262 dcons_cngetc(struct consdev *cp) 263 { 264 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 265 return (dcons_os_checkc(dc)); 266 } 267 268 static void 269 dcons_cnputc(struct consdev *cp, int c) 270 { 271 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 272 dcons_os_putc(dc, c); 273 } 274 275 static int 276 dcons_drv_init(int stage) 277 { 278 #if defined(__i386__) || defined(__amd64__) 279 quad_t addr, size; 280 #endif 281 282 if (drv_init) 283 return(drv_init); 284 285 drv_init = -1; 286 287 bzero(&dg, sizeof(dg)); 288 dcons_conf = &dg; 289 dg.cdev = &dcons_consdev; 290 dg.buf = NULL; 291 dg.size = DCONS_BUF_SIZE; 292 293 #if defined(__i386__) || defined(__amd64__) 294 if (getenv_quad("dcons.addr", &addr) > 0 && 295 getenv_quad("dcons.size", &size) > 0) { 296 #ifdef __i386__ 297 vm_paddr_t pa; 298 /* 299 * Allow read/write access to dcons buffer. 300 */ 301 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 302 *vtopte(KERNBASE + pa) |= PG_RW; 303 invltlb(); 304 #endif 305 /* XXX P to V */ 306 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 307 dg.size = size; 308 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 309 dg.buf = NULL; 310 } 311 #endif 312 if (dg.buf != NULL) 313 goto ok; 314 315 #ifndef KLD_MODULE 316 if (stage == 0) { /* XXX or cold */ 317 /* 318 * DCONS_FORCE_CONSOLE == 1 and statically linked. 319 * called from cninit(). can't use contigmalloc yet . 320 */ 321 dg.buf = (struct dcons_buf *) bssbuf; 322 dcons_init(dg.buf, dg.size, sc); 323 } else 324 #endif 325 { 326 /* 327 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 328 * if the module is loaded after boot, 329 * bssbuf could be non-continuous. 330 */ 331 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 332 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 333 if (dg.buf == NULL) 334 return (-1); 335 dcons_init(dg.buf, dg.size, sc); 336 } 337 338 ok: 339 dcons_buf = dg.buf; 340 341 drv_init = 1; 342 343 return 0; 344 } 345 346 347 static int 348 dcons_attach_port(int port, char *name, int flags) 349 { 350 struct dcons_softc *dc; 351 struct tty *tp; 352 353 dc = &sc[port]; 354 tp = tty_alloc(&dcons_ttydevsw, dc); 355 dc->flags = flags; 356 dc->tty = tp; 357 tty_init_console(tp, 0); 358 tty_makedev(tp, NULL, "%s", name); 359 return(0); 360 } 361 362 static int 363 dcons_attach(void) 364 { 365 int polltime; 366 367 dcons_attach_port(DCONS_CON, "dcons", 0); 368 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 369 callout_init(&dcons_callout, CALLOUT_MPSAFE); 370 polltime = hz / poll_hz; 371 if (polltime < 1) 372 polltime = 1; 373 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 374 return(0); 375 } 376 377 static int 378 dcons_detach(int port) 379 { 380 struct tty *tp; 381 struct dcons_softc *dc; 382 383 dc = &sc[port]; 384 tp = dc->tty; 385 386 tty_lock(tp); 387 tty_rel_gone(tp); 388 389 return(0); 390 } 391 392 static int 393 dcons_modevent(module_t mode, int type, void *data) 394 { 395 int err = 0, ret; 396 397 switch (type) { 398 case MOD_LOAD: 399 ret = dcons_drv_init(1); 400 if (ret != -1) 401 dcons_attach(); 402 if (ret == 0) { 403 dcons_cnprobe(&dcons_consdev); 404 dcons_cninit(&dcons_consdev); 405 cnadd(&dcons_consdev); 406 } 407 break; 408 case MOD_UNLOAD: 409 printf("dcons: unload\n"); 410 if (drv_init == 1) { 411 callout_stop(&dcons_callout); 412 cnremove(&dcons_consdev); 413 dcons_detach(DCONS_CON); 414 dcons_detach(DCONS_GDB); 415 dg.buf->magic = 0; 416 417 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 418 } 419 420 break; 421 case MOD_SHUTDOWN: 422 #if 0 /* Keep connection after halt */ 423 dg.buf->magic = 0; 424 #endif 425 break; 426 default: 427 err = EOPNOTSUPP; 428 break; 429 } 430 return(err); 431 } 432 433 #if defined(GDB) 434 /* Debugger interface */ 435 436 static int 437 dcons_os_getc(struct dcons_softc *dc) 438 { 439 int c; 440 441 while ((c = dcons_os_checkc(dc)) == -1); 442 443 return (c & 0xff); 444 } 445 446 static int 447 dcons_dbg_probe(void) 448 { 449 int dcons_gdb; 450 451 if (getenv_int("dcons_gdb", &dcons_gdb) == 0) 452 return (-1); 453 return (dcons_gdb); 454 } 455 456 static void 457 dcons_dbg_init(void) 458 { 459 } 460 461 static void 462 dcons_dbg_term(void) 463 { 464 } 465 466 static void 467 dcons_dbg_putc(int c) 468 { 469 struct dcons_softc *dc = &sc[DCONS_GDB]; 470 dcons_os_putc(dc, c); 471 } 472 473 static int 474 dcons_dbg_getc(void) 475 { 476 struct dcons_softc *dc = &sc[DCONS_GDB]; 477 return (dcons_os_getc(dc)); 478 } 479 #endif 480 481 DEV_MODULE(dcons, dcons_modevent, NULL); 482 MODULE_VERSION(dcons, DCONS_VERSION); 483