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