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