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