dcons_os.c revision 225203
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 225203 2011-08-26 21:46:36Z rwatson $
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	25
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))
137static int
138dcons_check_break(struct dcons_softc *dc, int c)
139{
140
141	if (c < 0)
142		return (c);
143
144#ifdef GDB
145	if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport)
146		kdb_alt_break_gdb(c, &dc->brk_state);
147	else
148#endif
149		kdb_alt_break(c, &dc->brk_state);
150
151	return (c);
152}
153#else
154#define	dcons_check_break(dc, c)	(c)
155#endif
156
157static int
158dcons_os_checkc_nopoll(struct dcons_softc *dc)
159{
160	int c;
161
162	if (dg.dma_tag != NULL)
163		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
164
165	c = dcons_check_break(dc, dcons_checkc(dc));
166
167	if (dg.dma_tag != NULL)
168		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
169
170	return (c);
171}
172
173static int
174dcons_os_checkc(struct dcons_softc *dc)
175{
176	EVENTHANDLER_INVOKE(dcons_poll, 0);
177	return (dcons_os_checkc_nopoll(dc));
178}
179
180static void
181dcons_os_putc(struct dcons_softc *dc, int c)
182{
183	if (dg.dma_tag != NULL)
184		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
185
186	dcons_putc(dc, c);
187
188	if (dg.dma_tag != NULL)
189		bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
190}
191
192static void
193dcons_outwakeup(struct tty *tp)
194{
195	struct dcons_softc *dc;
196	char ch;
197
198	dc = tty_softc(tp);
199
200	while (ttydisc_getc(tp, &ch, sizeof ch) != 0)
201		dcons_os_putc(dc, ch);
202}
203
204static void
205dcons_timeout(void *v)
206{
207	struct	tty *tp;
208	struct dcons_softc *dc;
209	int i, c, polltime;
210
211	for (i = 0; i < DCONS_NPORT; i ++) {
212		dc = &sc[i];
213		tp = dc->tty;
214
215		tty_lock(tp);
216		while ((c = dcons_os_checkc_nopoll(dc)) != -1)
217			ttydisc_rint(tp, c, 0);
218		ttydisc_rint_done(tp);
219		tty_unlock(tp);
220	}
221	polltime = hz / poll_hz;
222	if (polltime < 1)
223		polltime = 1;
224	callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
225}
226
227static void
228dcons_cnprobe(struct consdev *cp)
229{
230	sprintf(cp->cn_name, "dcons");
231#if DCONS_FORCE_CONSOLE
232	cp->cn_pri = CN_REMOTE;
233#else
234	cp->cn_pri = CN_NORMAL;
235#endif
236}
237
238static void
239dcons_cninit(struct consdev *cp)
240{
241	dcons_drv_init(0);
242	cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
243}
244
245static void
246dcons_cnterm(struct consdev *cp)
247{
248}
249
250static int
251dcons_cngetc(struct consdev *cp)
252{
253	struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
254	return (dcons_os_checkc(dc));
255}
256
257static void
258dcons_cnputc(struct consdev *cp, int c)
259{
260	struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
261	dcons_os_putc(dc, c);
262}
263
264static int
265dcons_drv_init(int stage)
266{
267#if defined(__i386__) || defined(__amd64__)
268	quad_t addr, size;
269#endif
270
271	if (drv_init)
272		return(drv_init);
273
274	drv_init = -1;
275
276	bzero(&dg, sizeof(dg));
277	dcons_conf = &dg;
278	dg.cdev = &dcons_consdev;
279	dg.buf = NULL;
280	dg.size = DCONS_BUF_SIZE;
281
282#if defined(__i386__) || defined(__amd64__)
283	if (getenv_quad("dcons.addr", &addr) > 0 &&
284	    getenv_quad("dcons.size", &size) > 0) {
285#ifdef __i386__
286		vm_paddr_t pa;
287		/*
288		 * Allow read/write access to dcons buffer.
289		 */
290		for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
291			*vtopte(KERNBASE + pa) |= PG_RW;
292		invltlb();
293#endif
294		/* XXX P to V */
295		dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
296		dg.size = size;
297		if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
298			dg.buf = NULL;
299	}
300#endif
301	if (dg.buf != NULL)
302		goto ok;
303
304#ifndef KLD_MODULE
305	if (stage == 0) { /* XXX or cold */
306		/*
307		 * DCONS_FORCE_CONSOLE == 1 and statically linked.
308		 * called from cninit(). can't use contigmalloc yet .
309		 */
310		dg.buf = (struct dcons_buf *) bssbuf;
311		dcons_init(dg.buf, dg.size, sc);
312	} else
313#endif
314	{
315		/*
316		 * DCONS_FORCE_CONSOLE == 0 or kernel module case.
317		 * if the module is loaded after boot,
318		 * bssbuf could be non-continuous.
319		 */
320		dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
321			M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
322		if (dg.buf == NULL)
323			return (-1);
324		dcons_init(dg.buf, dg.size, sc);
325	}
326
327ok:
328	dcons_buf = dg.buf;
329
330	drv_init = 1;
331
332	return 0;
333}
334
335
336static int
337dcons_attach_port(int port, char *name, int flags)
338{
339	struct dcons_softc *dc;
340	struct tty *tp;
341
342	dc = &sc[port];
343	tp = tty_alloc(&dcons_ttydevsw, dc);
344	dc->flags = flags;
345	dc->tty   = tp;
346	tty_init_console(tp, 0);
347	tty_makedev(tp, NULL, "%s", name);
348	return(0);
349}
350
351static int
352dcons_attach(void)
353{
354	int polltime;
355
356	dcons_attach_port(DCONS_CON, "dcons", 0);
357	dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
358	callout_init(&dcons_callout, CALLOUT_MPSAFE);
359	polltime = hz / poll_hz;
360	if (polltime < 1)
361		polltime = 1;
362	callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
363	return(0);
364}
365
366static int
367dcons_detach(int port)
368{
369	struct	tty *tp;
370	struct dcons_softc *dc;
371
372	dc = &sc[port];
373	tp = dc->tty;
374
375	tty_lock(tp);
376	tty_rel_gone(tp);
377
378	return(0);
379}
380
381static int
382dcons_modevent(module_t mode, int type, void *data)
383{
384	int err = 0, ret;
385
386	switch (type) {
387	case MOD_LOAD:
388		ret = dcons_drv_init(1);
389		if (ret != -1)
390			dcons_attach();
391		if (ret == 0) {
392			dcons_cnprobe(&dcons_consdev);
393			dcons_cninit(&dcons_consdev);
394			cnadd(&dcons_consdev);
395		}
396		break;
397	case MOD_UNLOAD:
398		printf("dcons: unload\n");
399		if (drv_init == 1) {
400			callout_stop(&dcons_callout);
401			cnremove(&dcons_consdev);
402			dcons_detach(DCONS_CON);
403			dcons_detach(DCONS_GDB);
404			dg.buf->magic = 0;
405
406			contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
407		}
408
409		break;
410	case MOD_SHUTDOWN:
411#if 0		/* Keep connection after halt */
412		dg.buf->magic = 0;
413#endif
414		break;
415	default:
416		err = EOPNOTSUPP;
417		break;
418	}
419	return(err);
420}
421
422#if defined(GDB)
423/* Debugger interface */
424
425static int
426dcons_os_getc(struct dcons_softc *dc)
427{
428	int c;
429
430	while ((c = dcons_os_checkc(dc)) == -1);
431
432	return (c & 0xff);
433}
434
435static int
436dcons_dbg_probe(void)
437{
438	int dcons_gdb;
439
440	if (getenv_int("dcons_gdb", &dcons_gdb) == 0)
441		return (-1);
442	return (dcons_gdb);
443}
444
445static void
446dcons_dbg_init(void)
447{
448}
449
450static void
451dcons_dbg_term(void)
452{
453}
454
455static void
456dcons_dbg_putc(int c)
457{
458	struct dcons_softc *dc = &sc[DCONS_GDB];
459	dcons_os_putc(dc, c);
460}
461
462static int
463dcons_dbg_getc(void)
464{
465	struct dcons_softc *dc = &sc[DCONS_GDB];
466	return (dcons_os_getc(dc));
467}
468#endif
469
470DEV_MODULE(dcons, dcons_modevent, NULL);
471MODULE_VERSION(dcons, DCONS_VERSION);
472