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/kdb.h> 41 #include <gdb/gdb.h> 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 #include <sys/systm.h> 45 #include <sys/types.h> 46 #include <sys/conf.h> 47 #include <sys/cons.h> 48 #include <sys/consio.h> 49 #include <sys/tty.h> 50 #include <sys/malloc.h> 51 #include <sys/priv.h> 52 #include <sys/proc.h> 53 #include <sys/ucred.h> 54 55 #include <machine/bus.h> 56 57 #include <dev/dcons/dcons.h> 58 #include <dev/dcons/dcons_os.h> 59 60 #include <ddb/ddb.h> 61 #include <sys/reboot.h> 62 63 #include <sys/sysctl.h> 64 65 #include <vm/vm.h> 66 #include <vm/vm_param.h> 67 #include <vm/pmap.h> 68 69 #include "opt_dcons.h" 70 #include "opt_kdb.h" 71 #include "opt_gdb.h" 72 #include "opt_ddb.h" 73 74 75 #ifndef DCONS_POLL_HZ 76 #define DCONS_POLL_HZ 25 77 #endif 78 79 #ifndef DCONS_POLL_IDLE 80 #define DCONS_POLL_IDLE 256 81 #endif 82 83 #ifndef DCONS_BUF_SIZE 84 #define DCONS_BUF_SIZE (16*1024) 85 #endif 86 87 #ifndef DCONS_FORCE_CONSOLE 88 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */ 89 #endif 90 91 #ifndef KLD_MODULE 92 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */ 93 #endif 94 95 /* global data */ 96 static struct dcons_global dg; 97 struct dcons_global *dcons_conf; 98 static int poll_hz = DCONS_POLL_HZ; 99 static u_int poll_idle = DCONS_POLL_HZ * DCONS_POLL_IDLE; 100 101 static struct dcons_softc sc[DCONS_NPORT]; 102 103 static SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console"); 104 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0, 105 "dcons polling rate"); 106 107 static int drv_init = 0; 108 static struct callout dcons_callout; 109 struct dcons_buf *dcons_buf; /* for local dconschat */ 110 111 static void dcons_timeout(void *); 112 static int dcons_drv_init(int); 113 114 static cn_probe_t dcons_cnprobe; 115 static cn_init_t dcons_cninit; 116 static cn_term_t dcons_cnterm; 117 static cn_getc_t dcons_cngetc; 118 static cn_putc_t dcons_cnputc; 119 static cn_grab_t dcons_cngrab; 120 static cn_ungrab_t dcons_cnungrab; 121 122 CONSOLE_DRIVER(dcons); 123 124 #if defined(GDB) 125 static gdb_probe_f dcons_dbg_probe; 126 static gdb_init_f dcons_dbg_init; 127 static gdb_term_f dcons_dbg_term; 128 static gdb_getc_f dcons_dbg_getc; 129 static gdb_putc_f dcons_dbg_putc; 130 131 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term, 132 dcons_dbg_getc, dcons_dbg_putc); 133 134 extern struct gdb_dbgport *gdb_cur; 135 #endif 136 137 static tsw_outwakeup_t dcons_outwakeup; 138 139 static struct ttydevsw dcons_ttydevsw = { 140 .tsw_flags = TF_NOPREFIX, 141 .tsw_outwakeup = dcons_outwakeup, 142 }; 143 144 #if (defined(GDB) || defined(DDB)) 145 static int 146 dcons_check_break(struct dcons_softc *dc, int c) 147 { 148 149 if (c < 0) 150 return (c); 151 152 #ifdef GDB 153 if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport) 154 kdb_alt_break_gdb(c, &dc->brk_state); 155 else 156 #endif 157 kdb_alt_break(c, &dc->brk_state); 158 159 return (c); 160 } 161 #else 162 #define dcons_check_break(dc, c) (c) 163 #endif 164 165 static int 166 dcons_os_checkc_nopoll(struct dcons_softc *dc) 167 { 168 int c; 169 170 if (dg.dma_tag != NULL) 171 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD); 172 173 c = dcons_check_break(dc, dcons_checkc(dc)); 174 175 if (dg.dma_tag != NULL) 176 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD); 177 178 return (c); 179 } 180 181 static int 182 dcons_os_checkc(struct dcons_softc *dc) 183 { 184 EVENTHANDLER_INVOKE(dcons_poll, 0); 185 return (dcons_os_checkc_nopoll(dc)); 186 } 187 188 static void 189 dcons_os_putc(struct dcons_softc *dc, int c) 190 { 191 if (dg.dma_tag != NULL) 192 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE); 193 194 dcons_putc(dc, c); 195 196 if (dg.dma_tag != NULL) 197 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE); 198 } 199 200 static void 201 dcons_outwakeup(struct tty *tp) 202 { 203 struct dcons_softc *dc; 204 char ch; 205 206 dc = tty_softc(tp); 207 208 while (ttydisc_getc(tp, &ch, sizeof ch) != 0) 209 dcons_os_putc(dc, ch); 210 } 211 212 static void 213 dcons_timeout(void *v) 214 { 215 struct tty *tp; 216 struct dcons_softc *dc; 217 int i, c, polltime; 218 219 for (i = 0; i < DCONS_NPORT; i ++) { 220 dc = &sc[i]; 221 tp = dc->tty; 222 223 tty_lock(tp); 224 while ((c = dcons_os_checkc_nopoll(dc)) != -1) { 225 ttydisc_rint(tp, c, 0); 226 poll_idle = 0; 227 } 228 ttydisc_rint_done(tp); 229 tty_unlock(tp); 230 } 231 poll_idle++; 232 polltime = hz; 233 if (poll_idle <= (poll_hz * DCONS_POLL_IDLE)) 234 polltime /= poll_hz; 235 callout_reset(&dcons_callout, polltime, dcons_timeout, tp); 236 } 237 238 static void 239 dcons_cnprobe(struct consdev *cp) 240 { 241 sprintf(cp->cn_name, "dcons"); 242 #if DCONS_FORCE_CONSOLE 243 cp->cn_pri = CN_REMOTE; 244 #else 245 cp->cn_pri = CN_NORMAL; 246 #endif 247 } 248 249 static void 250 dcons_cninit(struct consdev *cp) 251 { 252 dcons_drv_init(0); 253 cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */ 254 } 255 256 static void 257 dcons_cnterm(struct consdev *cp) 258 { 259 } 260 261 static void 262 dcons_cngrab(struct consdev *cp) 263 { 264 } 265 266 static void 267 dcons_cnungrab(struct consdev *cp) 268 { 269 } 270 271 static int 272 dcons_cngetc(struct consdev *cp) 273 { 274 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 275 return (dcons_os_checkc(dc)); 276 } 277 278 static void 279 dcons_cnputc(struct consdev *cp, int c) 280 { 281 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg; 282 dcons_os_putc(dc, c); 283 } 284 285 static int 286 dcons_drv_init(int stage) 287 { 288 #if defined(__i386__) || defined(__amd64__) 289 quad_t addr, size; 290 #endif 291 292 if (drv_init) 293 return(drv_init); 294 295 drv_init = -1; 296 297 bzero(&dg, sizeof(dg)); 298 dcons_conf = &dg; 299 dg.cdev = &dcons_consdev; 300 dg.buf = NULL; 301 dg.size = DCONS_BUF_SIZE; 302 303 #if defined(__i386__) || defined(__amd64__) 304 if (getenv_quad("dcons.addr", &addr) > 0 && 305 getenv_quad("dcons.size", &size) > 0) { 306 #ifdef __i386__ 307 vm_paddr_t pa; 308 /* 309 * Allow read/write access to dcons buffer. 310 */ 311 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE) 312 *vtopte(KERNBASE + pa) |= PG_RW; 313 invltlb(); 314 #endif 315 /* XXX P to V */ 316 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr); 317 dg.size = size; 318 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0) 319 dg.buf = NULL; 320 } 321 #endif 322 if (dg.buf != NULL) 323 goto ok; 324 325 #ifndef KLD_MODULE 326 if (stage == 0) { /* XXX or cold */ 327 /* 328 * DCONS_FORCE_CONSOLE == 1 and statically linked. 329 * called from cninit(). can't use contigmalloc yet . 330 */ 331 dg.buf = (struct dcons_buf *) bssbuf; 332 dcons_init(dg.buf, dg.size, sc); 333 } else 334 #endif 335 { 336 /* 337 * DCONS_FORCE_CONSOLE == 0 or kernel module case. 338 * if the module is loaded after boot, 339 * bssbuf could be non-continuous. 340 */ 341 dg.buf = (struct dcons_buf *) contigmalloc(dg.size, 342 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 343 if (dg.buf == NULL) 344 return (-1); 345 dcons_init(dg.buf, dg.size, sc); 346 } 347 348 ok: 349 dcons_buf = dg.buf; 350 351 drv_init = 1; 352 353 return 0; 354 } 355 356 357 static int 358 dcons_attach_port(int port, char *name, int flags) 359 { 360 struct dcons_softc *dc; 361 struct tty *tp; 362 363 dc = &sc[port]; 364 tp = tty_alloc(&dcons_ttydevsw, dc); 365 dc->flags = flags; 366 dc->tty = tp; 367 tty_init_console(tp, 0); 368 tty_makedev(tp, NULL, "%s", name); 369 return(0); 370 } 371 372 static int 373 dcons_attach(void) 374 { 375 int polltime; 376 377 dcons_attach_port(DCONS_CON, "dcons", 0); 378 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB); 379 callout_init(&dcons_callout, 1); 380 polltime = hz / poll_hz; 381 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL); 382 return(0); 383 } 384 385 static int 386 dcons_detach(int port) 387 { 388 struct tty *tp; 389 struct dcons_softc *dc; 390 391 dc = &sc[port]; 392 tp = dc->tty; 393 394 tty_lock(tp); 395 tty_rel_gone(tp); 396 397 return(0); 398 } 399 400 static int 401 dcons_modevent(module_t mode, int type, void *data) 402 { 403 int err = 0, ret; 404 405 switch (type) { 406 case MOD_LOAD: 407 ret = dcons_drv_init(1); 408 if (ret != -1) 409 dcons_attach(); 410 if (ret == 0) { 411 dcons_cnprobe(&dcons_consdev); 412 dcons_cninit(&dcons_consdev); 413 cnadd(&dcons_consdev); 414 } 415 break; 416 case MOD_UNLOAD: 417 printf("dcons: unload\n"); 418 if (drv_init == 1) { 419 callout_stop(&dcons_callout); 420 cnremove(&dcons_consdev); 421 dcons_detach(DCONS_CON); 422 dcons_detach(DCONS_GDB); 423 dg.buf->magic = 0; 424 425 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF); 426 } 427 428 break; 429 case MOD_SHUTDOWN: 430 #if 0 /* Keep connection after halt */ 431 dg.buf->magic = 0; 432 #endif 433 break; 434 default: 435 err = EOPNOTSUPP; 436 break; 437 } 438 return(err); 439 } 440 441 #if defined(GDB) 442 /* Debugger interface */ 443 444 static int 445 dcons_os_getc(struct dcons_softc *dc) 446 { 447 int c; 448 449 while ((c = dcons_os_checkc(dc)) == -1); 450 451 return (c & 0xff); 452 } 453 454 static int 455 dcons_dbg_probe(void) 456 { 457 int dcons_gdb; 458 459 if (getenv_int("dcons_gdb", &dcons_gdb) == 0) 460 return (-1); 461 return (dcons_gdb); 462 } 463 464 static void 465 dcons_dbg_init(void) 466 { 467 } 468 469 static void 470 dcons_dbg_term(void) 471 { 472 } 473 474 static void 475 dcons_dbg_putc(int c) 476 { 477 struct dcons_softc *dc = &sc[DCONS_GDB]; 478 dcons_os_putc(dc, c); 479 } 480 481 static int 482 dcons_dbg_getc(void) 483 { 484 struct dcons_softc *dc = &sc[DCONS_GDB]; 485 return (dcons_os_getc(dc)); 486 } 487 #endif 488 489 DEV_MODULE(dcons, dcons_modevent, NULL); 490 MODULE_VERSION(dcons, DCONS_VERSION); 491