2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 2003,2004
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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:
18 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
39 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
48 #include <sys/consio.h>
50 #include <sys/malloc.h>
53 #include <sys/ucred.h>
55 #include <machine/atomic.h>
56 #include <machine/bus.h>
58 #include <dev/dcons/dcons.h>
59 #include <dev/dcons/dcons_os.h>
62 #include <sys/reboot.h>
64 #include <sys/sysctl.h>
67 #include <vm/vm_param.h>
70 #include "opt_dcons.h"
77 #define DCONS_POLL_HZ 25
80 #ifndef DCONS_POLL_IDLE
81 #define DCONS_POLL_IDLE 256
84 #ifndef DCONS_BUF_SIZE
85 #define DCONS_BUF_SIZE (16*1024)
88 #ifndef DCONS_FORCE_CONSOLE
89 #define DCONS_FORCE_CONSOLE 0 /* Mostly for FreeBSD-4/DragonFly */
93 static char bssbuf[DCONS_BUF_SIZE]; /* buf in bss */
97 static struct dcons_global dg;
98 struct dcons_global *dcons_conf;
99 static int poll_hz = DCONS_POLL_HZ;
100 static u_int poll_idle = DCONS_POLL_HZ * DCONS_POLL_IDLE;
102 static struct dcons_softc sc[DCONS_NPORT];
104 static SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
105 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
106 "dcons polling rate");
108 static int drv_init = 0;
109 static struct callout dcons_callout;
110 struct dcons_buf *dcons_buf; /* for local dconschat */
112 static void dcons_timeout(void *);
113 static int dcons_drv_init(int);
115 static cn_probe_t dcons_cnprobe;
116 static cn_init_t dcons_cninit;
117 static cn_term_t dcons_cnterm;
118 static cn_getc_t dcons_cngetc;
119 static cn_putc_t dcons_cnputc;
120 static cn_grab_t dcons_cngrab;
121 static cn_ungrab_t dcons_cnungrab;
123 CONSOLE_DRIVER(dcons);
126 static gdb_probe_f dcons_dbg_probe;
127 static gdb_init_f dcons_dbg_init;
128 static gdb_term_f dcons_dbg_term;
129 static gdb_getc_f dcons_dbg_getc;
130 static gdb_putc_f dcons_dbg_putc;
132 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
133 dcons_dbg_getc, dcons_dbg_putc);
135 extern struct gdb_dbgport *gdb_cur;
138 static tsw_outwakeup_t dcons_outwakeup;
139 static tsw_free_t dcons_free;
141 static struct ttydevsw dcons_ttydevsw = {
142 .tsw_flags = TF_NOPREFIX,
143 .tsw_outwakeup = dcons_outwakeup,
144 .tsw_free = dcons_free,
147 static int dcons_close_refs;
149 #if (defined(GDB) || defined(DDB))
151 dcons_check_break(struct dcons_softc *dc, int c)
158 if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport)
159 kdb_alt_break_gdb(c, &dc->brk_state);
162 kdb_alt_break(c, &dc->brk_state);
167 #define dcons_check_break(dc, c) (c)
171 dcons_os_checkc_nopoll(struct dcons_softc *dc)
175 if (dg.dma_tag != NULL)
176 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
178 c = dcons_check_break(dc, dcons_checkc(dc));
180 if (dg.dma_tag != NULL)
181 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
187 dcons_os_checkc(struct dcons_softc *dc)
189 EVENTHANDLER_INVOKE(dcons_poll, 0);
190 return (dcons_os_checkc_nopoll(dc));
194 dcons_os_putc(struct dcons_softc *dc, int c)
196 if (dg.dma_tag != NULL)
197 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
201 if (dg.dma_tag != NULL)
202 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
206 dcons_free(void *xsc __unused)
209 /* Our deferred free has arrived, now we're waiting for one fewer. */
210 atomic_subtract_rel_int(&dcons_close_refs, 1);
214 dcons_outwakeup(struct tty *tp)
216 struct dcons_softc *dc;
221 while (ttydisc_getc(tp, &ch, sizeof ch) != 0)
222 dcons_os_putc(dc, ch);
226 dcons_timeout(void *v)
229 struct dcons_softc *dc;
232 for (i = 0; i < DCONS_NPORT; i ++) {
237 while ((c = dcons_os_checkc_nopoll(dc)) != -1) {
238 ttydisc_rint(tp, c, 0);
241 ttydisc_rint_done(tp);
246 if (poll_idle <= (poll_hz * DCONS_POLL_IDLE))
248 callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
252 dcons_cnprobe(struct consdev *cp)
254 sprintf(cp->cn_name, "dcons");
255 #if DCONS_FORCE_CONSOLE
256 cp->cn_pri = CN_REMOTE;
258 cp->cn_pri = CN_NORMAL;
263 dcons_cninit(struct consdev *cp)
266 cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
270 dcons_cnterm(struct consdev *cp)
275 dcons_cngrab(struct consdev *cp)
280 dcons_cnungrab(struct consdev *cp)
285 dcons_cngetc(struct consdev *cp)
287 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
288 return (dcons_os_checkc(dc));
292 dcons_cnputc(struct consdev *cp, int c)
294 struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
295 dcons_os_putc(dc, c);
299 dcons_drv_init(int stage)
301 #if defined(__i386__) || defined(__amd64__)
310 bzero(&dg, sizeof(dg));
312 dg.cdev = &dcons_consdev;
314 dg.size = DCONS_BUF_SIZE;
316 #if defined(__i386__) || defined(__amd64__)
317 if (getenv_quad("dcons.addr", &addr) > 0 &&
318 getenv_quad("dcons.size", &size) > 0) {
322 * Allow read/write access to dcons buffer.
324 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
325 *vtopte(PMAP_MAP_LOW + pa) |= PG_RW;
330 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
332 dg.buf = (struct dcons_buf *)((vm_offset_t)PMAP_MAP_LOW +
336 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
344 if (stage == 0) { /* XXX or cold */
346 * DCONS_FORCE_CONSOLE == 1 and statically linked.
347 * called from cninit(). can't use contigmalloc yet .
349 dg.buf = (struct dcons_buf *) bssbuf;
350 dcons_init(dg.buf, dg.size, sc);
355 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
356 * if the module is loaded after boot,
357 * bssbuf could be non-continuous.
359 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
360 M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
363 dcons_init(dg.buf, dg.size, sc);
376 dcons_attach_port(int port, char *name, int flags)
378 struct dcons_softc *dc;
382 tp = tty_alloc(&dcons_ttydevsw, dc);
385 tty_init_console(tp, 0);
386 tty_makedev(tp, NULL, "%s", name);
395 dcons_attach_port(DCONS_CON, "dcons", 0);
396 dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
397 callout_init(&dcons_callout, 1);
398 polltime = hz / poll_hz;
399 callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
404 dcons_detach(int port)
407 struct dcons_softc *dc;
412 /* tty_rel_gone() schedules a deferred free callback, count it. */
413 atomic_add_int(&dcons_close_refs, 1);
421 dcons_modevent(module_t mode, int type, void *data)
427 ret = dcons_drv_init(1);
431 dcons_cnprobe(&dcons_consdev);
432 dcons_cninit(&dcons_consdev);
433 cnadd(&dcons_consdev);
437 printf("dcons: unload\n");
439 callout_stop(&dcons_callout);
440 cnremove(&dcons_consdev);
441 dcons_detach(DCONS_CON);
442 dcons_detach(DCONS_GDB);
445 contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
448 /* Wait for tty deferred free callbacks to complete. */
449 while (atomic_load_acq_int(&dcons_close_refs) > 0)
450 pause_sbt("dcunld", mstosbt(50), mstosbt(10), 0);
453 #if 0 /* Keep connection after halt */
465 /* Debugger interface */
468 dcons_os_getc(struct dcons_softc *dc)
472 while ((c = dcons_os_checkc(dc)) == -1);
478 dcons_dbg_probe(void)
482 if (getenv_int("dcons_gdb", &dcons_gdb) == 0)
498 dcons_dbg_putc(int c)
500 struct dcons_softc *dc = &sc[DCONS_GDB];
501 dcons_os_putc(dc, c);
507 struct dcons_softc *dc = &sc[DCONS_GDB];
508 return (dcons_os_getc(dc));
512 DEV_MODULE(dcons, dcons_modevent, NULL);
513 MODULE_VERSION(dcons, DCONS_VERSION);