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