xref: /freebsd/sys/dev/dcons/dcons_os.c (revision a25896ca1270e25b657ceaa8d47d5699515f5c25)
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(PMAP_MAP_LOW + pa) |= PG_RW;
313 		invltlb();
314 #endif
315 		/* XXX P to V */
316 #ifdef __amd64__
317 		dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
318 #else /* __i386__ */
319 		dg.buf = (struct dcons_buf *)((vm_offset_t)PMAP_MAP_LOW +
320 		    addr);
321 #endif
322 		dg.size = size;
323 		if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
324 			dg.buf = NULL;
325 	}
326 #endif
327 	if (dg.buf != NULL)
328 		goto ok;
329 
330 #ifndef KLD_MODULE
331 	if (stage == 0) { /* XXX or cold */
332 		/*
333 		 * DCONS_FORCE_CONSOLE == 1 and statically linked.
334 		 * called from cninit(). can't use contigmalloc yet .
335 		 */
336 		dg.buf = (struct dcons_buf *) bssbuf;
337 		dcons_init(dg.buf, dg.size, sc);
338 	} else
339 #endif
340 	{
341 		/*
342 		 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
343 		 * if the module is loaded after boot,
344 		 * bssbuf could be non-continuous.
345 		 */
346 		dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
347 			M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
348 		if (dg.buf == NULL)
349 			return (-1);
350 		dcons_init(dg.buf, dg.size, sc);
351 	}
352 
353 ok:
354 	dcons_buf = dg.buf;
355 
356 	drv_init = 1;
357 
358 	return 0;
359 }
360 
361 
362 static int
363 dcons_attach_port(int port, char *name, int flags)
364 {
365 	struct dcons_softc *dc;
366 	struct tty *tp;
367 
368 	dc = &sc[port];
369 	tp = tty_alloc(&dcons_ttydevsw, dc);
370 	dc->flags = flags;
371 	dc->tty   = tp;
372 	tty_init_console(tp, 0);
373 	tty_makedev(tp, NULL, "%s", name);
374 	return(0);
375 }
376 
377 static int
378 dcons_attach(void)
379 {
380 	int polltime;
381 
382 	dcons_attach_port(DCONS_CON, "dcons", 0);
383 	dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
384 	callout_init(&dcons_callout, 1);
385 	polltime = hz / poll_hz;
386 	callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
387 	return(0);
388 }
389 
390 static int
391 dcons_detach(int port)
392 {
393 	struct	tty *tp;
394 	struct dcons_softc *dc;
395 
396 	dc = &sc[port];
397 	tp = dc->tty;
398 
399 	tty_lock(tp);
400 	tty_rel_gone(tp);
401 
402 	return(0);
403 }
404 
405 static int
406 dcons_modevent(module_t mode, int type, void *data)
407 {
408 	int err = 0, ret;
409 
410 	switch (type) {
411 	case MOD_LOAD:
412 		ret = dcons_drv_init(1);
413 		if (ret != -1)
414 			dcons_attach();
415 		if (ret == 0) {
416 			dcons_cnprobe(&dcons_consdev);
417 			dcons_cninit(&dcons_consdev);
418 			cnadd(&dcons_consdev);
419 		}
420 		break;
421 	case MOD_UNLOAD:
422 		printf("dcons: unload\n");
423 		if (drv_init == 1) {
424 			callout_stop(&dcons_callout);
425 			cnremove(&dcons_consdev);
426 			dcons_detach(DCONS_CON);
427 			dcons_detach(DCONS_GDB);
428 			dg.buf->magic = 0;
429 
430 			contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
431 		}
432 
433 		break;
434 	case MOD_SHUTDOWN:
435 #if 0		/* Keep connection after halt */
436 		dg.buf->magic = 0;
437 #endif
438 		break;
439 	default:
440 		err = EOPNOTSUPP;
441 		break;
442 	}
443 	return(err);
444 }
445 
446 #if defined(GDB)
447 /* Debugger interface */
448 
449 static int
450 dcons_os_getc(struct dcons_softc *dc)
451 {
452 	int c;
453 
454 	while ((c = dcons_os_checkc(dc)) == -1);
455 
456 	return (c & 0xff);
457 }
458 
459 static int
460 dcons_dbg_probe(void)
461 {
462 	int dcons_gdb;
463 
464 	if (getenv_int("dcons_gdb", &dcons_gdb) == 0)
465 		return (-1);
466 	return (dcons_gdb);
467 }
468 
469 static void
470 dcons_dbg_init(void)
471 {
472 }
473 
474 static void
475 dcons_dbg_term(void)
476 {
477 }
478 
479 static void
480 dcons_dbg_putc(int c)
481 {
482 	struct dcons_softc *dc = &sc[DCONS_GDB];
483 	dcons_os_putc(dc, c);
484 }
485 
486 static int
487 dcons_dbg_getc(void)
488 {
489 	struct dcons_softc *dc = &sc[DCONS_GDB];
490 	return (dcons_os_getc(dc));
491 }
492 #endif
493 
494 DEV_MODULE(dcons, dcons_modevent, NULL);
495 MODULE_VERSION(dcons, DCONS_VERSION);
496