xref: /illumos-gate/usr/src/uts/common/io/tem.c (revision b8052df9f609edb713f6828c9eecc3d7be19dfb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * ANSI terminal emulator module; parse ANSI X3.64 escape sequences and
29  * the like.
30  *
31  * How Virtual Terminal Emulator Works:
32  *
33  * Every virtual terminal is associated with a tem_vt_state structure
34  * and maintains a virtual screen buffer in tvs_screen_buf, which contains
35  * all the characters which should be shown on the physical screen when
36  * the terminal is activated.  There are also two other buffers, tvs_fg_buf
37  * and tvs_bg_buf, which track the foreground and background colors of the
38  * on screen characters
39  *
40  * Data written to a virtual terminal is composed of characters which
41  * should be displayed on the screen when this virtual terminal is
42  * activated, fg/bg colors of these characters, and other control
43  * information (escape sequence, etc).
44  *
45  * When data is passed to a virtual terminal it first is parsed for
46  * control information by tem_safe_parse().  Subsequently the character
47  * and color data are written to tvs_screen_buf, tvs_fg_buf, and
48  * tvs_bg_buf.  They are saved in these buffers in order to refresh
49  * the screen when this terminal is activated.  If the terminal is
50  * currently active, the data (characters and colors) are also written
51  * to the physical screen by invoking a callback function,
52  * tem_safe_text_callbacks() or tem_safe_pix_callbacks().
53  *
54  * When rendering data to the framebuffer, if the framebuffer is in
55  * VIS_PIXEL mode, the character data will first be converted to pixel
56  * data using tem_safe_pix_bit2pix(), and then the pixels get displayed
57  * on the physical screen.  We only store the character and color data in
58  * tem_vt_state since the bit2pix conversion only happens when actually
59  * rendering to the physical framebuffer.
60  */
61 
62 
63 #include <sys/types.h>
64 #include <sys/file.h>
65 #include <sys/conf.h>
66 #include <sys/errno.h>
67 #include <sys/open.h>
68 #include <sys/cred.h>
69 #include <sys/kmem.h>
70 #include <sys/ascii.h>
71 #include <sys/consdev.h>
72 #include <sys/font.h>
73 #include <sys/fbio.h>
74 #include <sys/conf.h>
75 #include <sys/modctl.h>
76 #include <sys/strsubr.h>
77 #include <sys/stat.h>
78 #include <sys/visual_io.h>
79 #include <sys/mutex.h>
80 #include <sys/param.h>
81 #include <sys/debug.h>
82 #include <sys/cmn_err.h>
83 #include <sys/console.h>
84 #include <sys/ddi.h>
85 #include <sys/sunddi.h>
86 #include <sys/sunldi.h>
87 #include <sys/tem_impl.h>
88 #ifdef _HAVE_TEM_FIRMWARE
89 #include <sys/promif.h>
90 #endif /* _HAVE_TEM_FIRMWARE */
91 #include <sys/consplat.h>
92 #include <sys/kd.h>
93 #include <sys/sysmacros.h>
94 #include <sys/note.h>
95 #include <sys/t_lock.h>
96 
97 /* Terminal emulator internal helper functions */
98 static void	tems_setup_terminal(struct vis_devinit *, size_t, size_t);
99 static void	tems_modechange_callback(struct vis_modechg_arg *,
100 		struct vis_devinit *);
101 
102 static void	tems_reset_colormap(cred_t *, enum called_from);
103 
104 static void	tem_free_buf(struct tem_vt_state *);
105 static void	tem_internal_init(struct tem_vt_state *, cred_t *, boolean_t,
106 		    boolean_t);
107 static void	tems_get_initial_color(tem_color_t *pcolor);
108 
109 /*
110  * Globals
111  */
112 static ldi_ident_t	term_li = NULL;
113 tem_state_t	tems;	/* common term info */
114 _NOTE(MUTEX_PROTECTS_DATA(tems.ts_lock, tems))
115 
116 extern struct mod_ops mod_miscops;
117 
118 static struct modlmisc	modlmisc = {
119 	&mod_miscops,	/* modops */
120 	"ANSI Terminal Emulator", /* name */
121 };
122 
123 static struct modlinkage modlinkage = {
124 	MODREV_1, { (void *)&modlmisc, NULL }
125 };
126 
127 int
128 _init(void)
129 {
130 	int ret;
131 	ret = mod_install(&modlinkage);
132 	if (ret != 0)
133 		return (ret);
134 	ret = ldi_ident_from_mod(&modlinkage, &term_li);
135 	if (ret != 0) {
136 		(void) mod_remove(&modlinkage);
137 		return (ret);
138 	}
139 
140 	mutex_init(&tems.ts_lock, (char *)NULL, MUTEX_DRIVER, NULL);
141 	list_create(&tems.ts_list, sizeof (struct tem_vt_state),
142 	    offsetof(struct tem_vt_state, tvs_list_node));
143 	tems.ts_active = NULL;
144 
145 	return (0);
146 }
147 
148 int
149 _fini()
150 {
151 	int ret;
152 
153 	ret = mod_remove(&modlinkage);
154 	if (ret == 0) {
155 		ldi_ident_release(term_li);
156 		term_li = NULL;
157 	}
158 	return (ret);
159 }
160 
161 int
162 _info(struct modinfo *modinfop)
163 {
164 	return (mod_info(&modlinkage, modinfop));
165 }
166 
167 static void
168 tem_add(struct tem_vt_state *tem)
169 {
170 	ASSERT(MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock));
171 
172 	list_insert_head(&tems.ts_list, tem);
173 }
174 
175 static void
176 tem_rm(struct tem_vt_state *tem)
177 {
178 	ASSERT(MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock));
179 
180 	list_remove(&tems.ts_list, tem);
181 }
182 
183 /*
184  * This is the main entry point to the module.  It handles output requests
185  * during normal system operation, when (e.g.) mutexes are available.
186  */
187 void
188 tem_write(tem_vt_state_t tem_arg, uchar_t *buf, ssize_t len, cred_t *credp)
189 {
190 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
191 
192 	mutex_enter(&tems.ts_lock);
193 	mutex_enter(&tem->tvs_lock);
194 
195 	if (!tem->tvs_initialized) {
196 		mutex_exit(&tem->tvs_lock);
197 		mutex_exit(&tems.ts_lock);
198 		return;
199 	}
200 
201 	tem_safe_check_first_time(tem, credp, CALLED_FROM_NORMAL);
202 	tem_safe_terminal_emulate(tem, buf, len, credp, CALLED_FROM_NORMAL);
203 
204 	mutex_exit(&tem->tvs_lock);
205 	mutex_exit(&tems.ts_lock);
206 }
207 
208 static void
209 tem_internal_init(struct tem_vt_state *ptem, cred_t *credp,
210     boolean_t init_color, boolean_t clear_screen)
211 {
212 	unsigned i, j, width, height;
213 	text_attr_t attr;
214 	text_color_t fg;
215 	text_color_t bg;
216 
217 	ASSERT(MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&ptem->tvs_lock));
218 
219 	if (tems.ts_display_mode == VIS_PIXEL) {
220 		ptem->tvs_pix_data_size = tems.ts_pix_data_size;
221 		ptem->tvs_pix_data =
222 		    kmem_alloc(ptem->tvs_pix_data_size, KM_SLEEP);
223 	}
224 
225 	ptem->tvs_outbuf_size = tems.ts_c_dimension.width *
226 	    sizeof (*ptem->tvs_outbuf);
227 	ptem->tvs_outbuf = kmem_alloc(ptem->tvs_outbuf_size, KM_SLEEP);
228 
229 	width = tems.ts_c_dimension.width;
230 	height = tems.ts_c_dimension.height;
231 	ptem->tvs_screen_history_size = height;
232 
233 	ptem->tvs_screen_buf_size = width * ptem->tvs_screen_history_size *
234 	    sizeof (*ptem->tvs_screen_buf);
235 	ptem->tvs_screen_buf = kmem_alloc(ptem->tvs_screen_buf_size, KM_SLEEP);
236 	ptem->tvs_screen_rows = kmem_alloc(ptem->tvs_screen_history_size *
237 	    sizeof (term_char_t *), KM_SLEEP);
238 
239 	ptem->tvs_maxtab = width / 8;
240 	ptem->tvs_tabs = kmem_alloc(ptem->tvs_maxtab * sizeof (*ptem->tvs_tabs),
241 	    KM_SLEEP);
242 
243 	tem_safe_reset_display(ptem, credp, CALLED_FROM_NORMAL,
244 	    clear_screen, init_color);
245 
246 	ptem->tvs_utf8_left = 0;
247 	ptem->tvs_utf8_partial = 0;
248 
249 	/* Get default attributes and fill up the screen buffer. */
250 	tem_safe_get_attr(ptem, &fg, &bg, &attr, TEM_ATTR_SCREEN_REVERSE);
251 	for (i = 0; i < ptem->tvs_screen_history_size; i++) {
252 		ptem->tvs_screen_rows[i] = &ptem->tvs_screen_buf[i * width];
253 
254 		for (j = 0; j < width; j++) {
255 			ptem->tvs_screen_rows[i][j].tc_fg_color = fg;
256 			ptem->tvs_screen_rows[i][j].tc_bg_color = bg;
257 			ptem->tvs_screen_rows[i][j].tc_char =
258 			    TEM_ATTR(attr) | ' ';
259 		}
260 	}
261 
262 	ptem->tvs_initialized = B_TRUE;
263 }
264 
265 boolean_t
266 tem_initialized(tem_vt_state_t tem_arg)
267 {
268 	struct tem_vt_state *ptem = (struct tem_vt_state *)tem_arg;
269 	boolean_t ret;
270 
271 	mutex_enter(&ptem->tvs_lock);
272 	ret = ptem->tvs_initialized;
273 	mutex_exit(&ptem->tvs_lock);
274 
275 	return (ret);
276 }
277 
278 tem_vt_state_t
279 tem_init(cred_t *credp, queue_t *rq)
280 {
281 	struct tem_vt_state *ptem;
282 
283 	ptem = kmem_zalloc(sizeof (struct tem_vt_state), KM_SLEEP);
284 	mutex_init(&ptem->tvs_lock, (char *)NULL, MUTEX_DRIVER, NULL);
285 
286 	mutex_enter(&tems.ts_lock);
287 	mutex_enter(&ptem->tvs_lock);
288 
289 	ptem->tvs_isactive = B_FALSE;
290 	ptem->tvs_fbmode = KD_TEXT;
291 	ptem->tvs_queue = rq;
292 
293 	/*
294 	 * A tem is regarded as initialized only after tem_internal_init(),
295 	 * will be set at the end of tem_internal_init().
296 	 */
297 	ptem->tvs_initialized = B_FALSE;
298 
299 
300 	if (!tems.ts_initialized) {
301 		/*
302 		 * Only happens during early console configuration.
303 		 */
304 		tem_add(ptem);
305 		mutex_exit(&ptem->tvs_lock);
306 		mutex_exit(&tems.ts_lock);
307 		return ((tem_vt_state_t)ptem);
308 	}
309 
310 	tem_internal_init(ptem, credp, B_TRUE, B_FALSE);
311 	tem_add(ptem);
312 	mutex_exit(&ptem->tvs_lock);
313 	mutex_exit(&tems.ts_lock);
314 
315 	return ((tem_vt_state_t)ptem);
316 }
317 
318 /*
319  * re-init the tem after video mode has changed and tems_info has
320  * been re-inited. The lock is already held.
321  */
322 static void
323 tem_reinit(struct tem_vt_state *tem, boolean_t reset_display)
324 {
325 	ASSERT(MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock));
326 
327 	tem_free_buf(tem); /* only free virtual buffers */
328 
329 	/* reserve color */
330 	tem_internal_init(tem, kcred, B_FALSE, reset_display);
331 }
332 
333 static void
334 tem_free_buf(struct tem_vt_state *tem)
335 {
336 	ASSERT(tem != NULL && MUTEX_HELD(&tem->tvs_lock));
337 
338 	if (tem->tvs_outbuf != NULL)
339 		kmem_free(tem->tvs_outbuf, tem->tvs_outbuf_size);
340 	if (tem->tvs_pix_data != NULL)
341 		kmem_free(tem->tvs_pix_data, tem->tvs_pix_data_size);
342 	if (tem->tvs_screen_buf != NULL)
343 		kmem_free(tem->tvs_screen_buf, tem->tvs_screen_buf_size);
344 	if (tem->tvs_screen_rows != NULL) {
345 		kmem_free(tem->tvs_screen_rows, tem->tvs_screen_history_size *
346 		    sizeof (term_char_t *));
347 	}
348 	if (tem->tvs_tabs != NULL) {
349 		kmem_free(tem->tvs_tabs, tem->tvs_maxtab *
350 		    sizeof (*tem->tvs_tabs));
351 	}
352 }
353 
354 void
355 tem_destroy(tem_vt_state_t tem_arg, cred_t *credp)
356 {
357 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
358 
359 	mutex_enter(&tems.ts_lock);
360 	mutex_enter(&tem->tvs_lock);
361 
362 	if (tem->tvs_isactive && tem->tvs_fbmode == KD_TEXT)
363 		tem_safe_blank_screen(tem, credp, CALLED_FROM_NORMAL);
364 
365 	tem_free_buf(tem);
366 	tem_rm(tem);
367 
368 	if (tems.ts_active == tem)
369 		tems.ts_active = NULL;
370 
371 	mutex_exit(&tem->tvs_lock);
372 	mutex_exit(&tems.ts_lock);
373 
374 	kmem_free(tem, sizeof (struct tem_vt_state));
375 }
376 
377 static int
378 tems_failed(cred_t *credp, boolean_t finish_ioctl)
379 {
380 	int	lyr_rval;
381 
382 	ASSERT(MUTEX_HELD(&tems.ts_lock));
383 
384 	if (finish_ioctl)
385 		(void) ldi_ioctl(tems.ts_hdl, VIS_DEVFINI, 0,
386 		    FWRITE | FKIOCTL, credp, &lyr_rval);
387 
388 	(void) ldi_close(tems.ts_hdl, 0, credp);
389 	tems.ts_hdl = NULL;
390 	return (ENXIO);
391 }
392 
393 /*
394  * only called once during boot
395  */
396 int
397 tem_info_init(char *pathname, cred_t *credp)
398 {
399 	int			lyr_rval, ret;
400 	struct vis_devinit	temargs;
401 	char			*pathbuf;
402 	size_t height = 0;
403 	size_t width = 0;
404 	struct tem_vt_state *p;
405 
406 	mutex_enter(&tems.ts_lock);
407 
408 	if (tems.ts_initialized) {
409 		mutex_exit(&tems.ts_lock);
410 		return (0);
411 	}
412 
413 	/*
414 	 * Open the layered device using the devfs physical device name
415 	 * after adding the /devices prefix.
416 	 */
417 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
418 	(void) strcpy(pathbuf, "/devices");
419 	if (i_ddi_prompath_to_devfspath(pathname,
420 	    pathbuf + strlen("/devices")) != DDI_SUCCESS) {
421 		cmn_err(CE_WARN, "terminal-emulator:  path conversion error");
422 		kmem_free(pathbuf, MAXPATHLEN);
423 
424 		mutex_exit(&tems.ts_lock);
425 		return (ENXIO);
426 	}
427 	if (ldi_open_by_name(pathbuf, FWRITE, credp,
428 	    &tems.ts_hdl, term_li) != 0) {
429 		cmn_err(CE_WARN, "terminal-emulator:  device path open error");
430 		kmem_free(pathbuf, MAXPATHLEN);
431 
432 		mutex_exit(&tems.ts_lock);
433 		return (ENXIO);
434 	}
435 	kmem_free(pathbuf, MAXPATHLEN);
436 
437 	temargs.modechg_cb  = (vis_modechg_cb_t)tems_modechange_callback;
438 	temargs.modechg_arg = NULL;
439 
440 	/*
441 	 * Initialize the console and get the device parameters
442 	 */
443 	if (ldi_ioctl(tems.ts_hdl, VIS_DEVINIT,
444 	    (intptr_t)&temargs, FWRITE|FKIOCTL, credp, &lyr_rval) != 0) {
445 		cmn_err(CE_WARN, "terminal emulator: Compatible fb not found");
446 		ret = tems_failed(credp, B_FALSE);
447 		mutex_exit(&tems.ts_lock);
448 		return (ret);
449 	}
450 
451 	/* Make sure the fb driver and terminal emulator versions match */
452 	if (temargs.version != VIS_CONS_REV) {
453 		cmn_err(CE_WARN,
454 		    "terminal emulator: VIS_CONS_REV %d (see sys/visual_io.h) "
455 		    "of console fb driver not supported", temargs.version);
456 		ret = tems_failed(credp, B_TRUE);
457 		mutex_exit(&tems.ts_lock);
458 		return (ret);
459 	}
460 
461 	if ((tems.ts_fb_polledio = temargs.polledio) == NULL) {
462 		cmn_err(CE_WARN, "terminal emulator: fb doesn't support polled "
463 		    "I/O");
464 		ret = tems_failed(credp, B_TRUE);
465 		mutex_exit(&tems.ts_lock);
466 		return (ret);
467 	}
468 
469 	/* other sanity checks */
470 	if (!((temargs.depth == 4) || (temargs.depth == 8) ||
471 	    (temargs.depth == 15) || (temargs.depth == 16) ||
472 	    (temargs.depth == 24) || (temargs.depth == 32))) {
473 		cmn_err(CE_WARN, "terminal emulator: unsupported depth");
474 		ret = tems_failed(credp, B_TRUE);
475 		mutex_exit(&tems.ts_lock);
476 		return (ret);
477 	}
478 
479 	if ((temargs.mode != VIS_TEXT) && (temargs.mode != VIS_PIXEL)) {
480 		cmn_err(CE_WARN, "terminal emulator: unsupported mode");
481 		ret = tems_failed(credp, B_TRUE);
482 		mutex_exit(&tems.ts_lock);
483 		return (ret);
484 	}
485 
486 	if ((temargs.mode == VIS_PIXEL) && plat_stdout_is_framebuffer())
487 		plat_tem_get_prom_size(&height, &width);
488 
489 	/*
490 	 * Initialize the common terminal emulator info
491 	 */
492 	tems_setup_terminal(&temargs, height, width);
493 
494 	tems_reset_colormap(credp, CALLED_FROM_NORMAL);
495 	tems_get_initial_color(&tems.ts_init_color);
496 
497 	tems.ts_initialized = 1; /* initialization flag */
498 
499 	for (p = list_head(&tems.ts_list); p != NULL;
500 	    p = list_next(&tems.ts_list, p)) {
501 		mutex_enter(&p->tvs_lock);
502 		tem_internal_init(p, credp, B_TRUE, B_FALSE);
503 		tem_align(p, credp, CALLED_FROM_NORMAL);
504 		mutex_exit(&p->tvs_lock);
505 	}
506 
507 	mutex_exit(&tems.ts_lock);
508 	return (0);
509 }
510 
511 #define	TEMS_DEPTH_DIFF		0x01
512 #define	TEMS_DIMENSION_DIFF	0x02
513 
514 static uchar_t
515 tems_check_videomode(struct vis_devinit *tp)
516 {
517 	uchar_t result = 0;
518 
519 	if (tems.ts_pdepth != tp->depth)
520 		result |= TEMS_DEPTH_DIFF;
521 
522 	if (tp->mode == VIS_TEXT) {
523 		if (tems.ts_c_dimension.width != tp->width ||
524 		    tems.ts_c_dimension.height != tp->height)
525 			result |= TEMS_DIMENSION_DIFF;
526 	} else {
527 		if (tems.ts_p_dimension.width != tp->width ||
528 		    tems.ts_p_dimension.height != tp->height)
529 			result |= TEMS_DIMENSION_DIFF;
530 	}
531 
532 	return (result);
533 }
534 
535 static void
536 tems_setup_font(screen_size_t height, screen_size_t width)
537 {
538 	bitmap_data_t *font_data;
539 	int i;
540 
541 	/*
542 	 * set_font() will select an appropriate sized font for
543 	 * the number of rows and columns selected. If we don't
544 	 * have a font that will fit, then it will use the
545 	 * default builtin font and adjust the rows and columns
546 	 * to fit on the screen.
547 	 */
548 	font_data = set_font(&tems.ts_c_dimension.height,
549 	    &tems.ts_c_dimension.width, height, width);
550 
551 	/*
552 	 * To use loaded font, we assign the loaded font data to tems.ts_font.
553 	 * In case of next load, the previously loaded data is freed
554 	 * when loading the new font.
555 	 */
556 	for (i = 0; i < VFNT_MAPS; i++) {
557 		tems.ts_font.vf_map[i] =
558 		    font_data->font->vf_map[i];
559 		tems.ts_font.vf_map_count[i] =
560 		    font_data->font->vf_map_count[i];
561 	}
562 
563 	tems.ts_font.vf_bytes = font_data->font->vf_bytes;
564 	tems.ts_font.vf_width = font_data->font->vf_width;
565 	tems.ts_font.vf_height = font_data->font->vf_height;
566 }
567 
568 static void
569 tems_setup_terminal(struct vis_devinit *tp, size_t height, size_t width)
570 {
571 	int old_blank_buf_size = tems.ts_c_dimension.width *
572 	    sizeof (*tems.ts_blank_line);
573 
574 	ASSERT(MUTEX_HELD(&tems.ts_lock));
575 
576 	tems.ts_pdepth = tp->depth;
577 	tems.ts_linebytes = tp->linebytes;
578 	tems.ts_display_mode = tp->mode;
579 	tems.ts_color_map = tp->color_map;
580 
581 	switch (tp->mode) {
582 	case VIS_TEXT:
583 		tems.ts_p_dimension.width = 0;
584 		tems.ts_p_dimension.height = 0;
585 		tems.ts_c_dimension.width = tp->width;
586 		tems.ts_c_dimension.height = tp->height;
587 		tems.ts_callbacks = &tem_safe_text_callbacks;
588 
589 		tems_setup_font(16 * tp->height + BORDER_PIXELS,
590 		    8 * tp->width + BORDER_PIXELS);
591 
592 		break;
593 
594 	case VIS_PIXEL:
595 		/*
596 		 * First check to see if the user has specified a screen size.
597 		 * If so, use those values.  Else use 34x80 as the default.
598 		 */
599 		if (width == 0) {
600 			width = TEM_DEFAULT_COLS;
601 			height = TEM_DEFAULT_ROWS;
602 		}
603 		tems.ts_c_dimension.height = (screen_size_t)height;
604 		tems.ts_c_dimension.width = (screen_size_t)width;
605 		tems.ts_p_dimension.height = tp->height;
606 		tems.ts_p_dimension.width = tp->width;
607 		tems.ts_callbacks = &tem_safe_pix_callbacks;
608 
609 		tems_setup_font(tp->height, tp->width);
610 
611 		tems.ts_p_offset.y = (tems.ts_p_dimension.height -
612 		    (tems.ts_c_dimension.height * tems.ts_font.vf_height)) / 2;
613 		tems.ts_p_offset.x = (tems.ts_p_dimension.width -
614 		    (tems.ts_c_dimension.width * tems.ts_font.vf_width)) / 2;
615 
616 		tems.ts_pix_data_size =
617 		    tems.ts_font.vf_width * tems.ts_font.vf_height;
618 		tems.ts_pix_data_size *= 4;
619 		tems.ts_pdepth = tp->depth;
620 
621 		break;
622 	}
623 
624 	/* Now virtual cls also uses the blank_line buffer */
625 	if (tems.ts_blank_line)
626 		kmem_free(tems.ts_blank_line, old_blank_buf_size);
627 
628 	tems.ts_blank_line = kmem_alloc(tems.ts_c_dimension.width *
629 	    sizeof (*tems.ts_blank_line), KM_SLEEP);
630 }
631 
632 /*
633  * This is a callback function that we register with the frame
634  * buffer driver layered underneath.  It gets invoked from
635  * the underlying frame buffer driver to reconfigure the terminal
636  * emulator to a new screen size and depth in conjunction with
637  * framebuffer videomode changes.
638  * Here we keep the foreground/background color and attributes,
639  * which may be different with the initial settings, so that
640  * the color won't change while the framebuffer videomode changes.
641  * And we also reset the kernel terminal emulator and clear the
642  * whole screen.
643  */
644 /* ARGSUSED */
645 void
646 tems_modechange_callback(struct vis_modechg_arg *arg,
647     struct vis_devinit *devinit)
648 {
649 	uchar_t diff;
650 	struct tem_vt_state *p;
651 	tem_modechg_cb_t cb;
652 	tem_modechg_cb_arg_t cb_arg;
653 
654 	ASSERT(!(list_is_empty(&tems.ts_list)));
655 
656 	mutex_enter(&tems.ts_lock);
657 
658 	/*
659 	 * currently only for pixel mode
660 	 */
661 	diff = tems_check_videomode(devinit);
662 	if (diff == 0) {
663 		mutex_exit(&tems.ts_lock);
664 		return;
665 	}
666 
667 	diff = diff & TEMS_DIMENSION_DIFF;
668 
669 	if (diff == 0) {
670 		/*
671 		 * Only need to reinit the active tem.
672 		 */
673 		struct tem_vt_state *active = tems.ts_active;
674 		tems.ts_pdepth = devinit->depth;
675 
676 		mutex_enter(&active->tvs_lock);
677 		ASSERT(active->tvs_isactive);
678 		tem_reinit(active, B_TRUE);
679 		mutex_exit(&active->tvs_lock);
680 
681 		mutex_exit(&tems.ts_lock);
682 		return;
683 	}
684 
685 	tems_setup_terminal(devinit, tems.ts_c_dimension.height,
686 	    tems.ts_c_dimension.width);
687 
688 	for (p = list_head(&tems.ts_list); p != NULL;
689 	    p = list_next(&tems.ts_list, p)) {
690 		mutex_enter(&p->tvs_lock);
691 		tem_reinit(p, p->tvs_isactive);
692 		mutex_exit(&p->tvs_lock);
693 	}
694 
695 
696 	if (tems.ts_modechg_cb == NULL) {
697 		mutex_exit(&tems.ts_lock);
698 		return;
699 	}
700 
701 	cb = tems.ts_modechg_cb;
702 	cb_arg = tems.ts_modechg_arg;
703 
704 	/*
705 	 * Release the lock while doing callback.
706 	 */
707 	mutex_exit(&tems.ts_lock);
708 	cb(cb_arg);
709 }
710 
711 /*
712  * This function is used to clear entire screen via the underlying framebuffer
713  * driver.
714  */
715 int
716 tems_cls_layered(struct vis_consclear *pda,
717     cred_t *credp)
718 {
719 	int rval;
720 
721 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCLEAR,
722 	    (intptr_t)pda, FKIOCTL, credp, &rval);
723 	return (rval);
724 }
725 
726 /*
727  * This function is used to display a rectangular blit of data
728  * of a given size and location via the underlying framebuffer driver.
729  * The blit can be as small as a pixel or as large as the screen.
730  */
731 void
732 tems_display_layered(struct vis_consdisplay *pda,
733     cred_t *credp)
734 {
735 	int rval;
736 
737 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSDISPLAY,
738 	    (intptr_t)pda, FKIOCTL, credp, &rval);
739 }
740 
741 /*
742  * This function is used to invoke a block copy operation in the
743  * underlying framebuffer driver.  Rectangle copies are how scrolling
744  * is implemented, as well as horizontal text shifting escape seqs.
745  * such as from vi when deleting characters and words.
746  */
747 void
748 tems_copy_layered(struct vis_conscopy *pma,
749     cred_t *credp)
750 {
751 	int rval;
752 
753 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCOPY,
754 	    (intptr_t)pma, FKIOCTL, credp, &rval);
755 }
756 
757 /*
758  * This function is used to show or hide a rectangluar monochrom
759  * pixel inverting, text block cursor via the underlying framebuffer.
760  */
761 void
762 tems_cursor_layered(struct vis_conscursor *pca,
763     cred_t *credp)
764 {
765 	int rval;
766 
767 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCURSOR,
768 	    (intptr_t)pca, FKIOCTL, credp, &rval);
769 }
770 
771 static void
772 tem_kdsetmode(int mode, cred_t *credp)
773 {
774 	int rval;
775 
776 	(void) ldi_ioctl(tems.ts_hdl, KDSETMODE,
777 	    (intptr_t)mode, FKIOCTL, credp, &rval);
778 
779 }
780 
781 static void
782 tems_reset_colormap(cred_t *credp, enum called_from called_from)
783 {
784 	struct vis_cmap cm;
785 	int rval;
786 
787 	if (called_from == CALLED_FROM_STANDALONE)
788 		return;
789 
790 	switch (tems.ts_pdepth) {
791 	case 8:
792 		cm.index = 0;
793 		cm.count = 16;
794 		cm.red   = (uint8_t *)cmap4_to_24.red;
795 		cm.blue  = (uint8_t *)cmap4_to_24.blue;
796 		cm.green = (uint8_t *)cmap4_to_24.green;
797 		(void) ldi_ioctl(tems.ts_hdl, VIS_PUTCMAP, (intptr_t)&cm,
798 		    FKIOCTL, credp, &rval);
799 		break;
800 	}
801 }
802 
803 void
804 tem_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y)
805 {
806 	mutex_enter(&tems.ts_lock);
807 	*r = (ushort_t)tems.ts_c_dimension.height;
808 	*c = (ushort_t)tems.ts_c_dimension.width;
809 	*x = (ushort_t)tems.ts_p_dimension.width;
810 	*y = (ushort_t)tems.ts_p_dimension.height;
811 	mutex_exit(&tems.ts_lock);
812 }
813 
814 void
815 tem_register_modechg_cb(tem_modechg_cb_t func, tem_modechg_cb_arg_t arg)
816 {
817 	mutex_enter(&tems.ts_lock);
818 
819 	tems.ts_modechg_cb = func;
820 	tems.ts_modechg_arg = arg;
821 
822 	mutex_exit(&tems.ts_lock);
823 }
824 
825 /*
826  * This function is to scroll up the OBP output, which has
827  * different screen height and width with our kernel console.
828  */
829 static void
830 tem_prom_scroll_up(struct tem_vt_state *tem, int nrows, cred_t *credp,
831     enum called_from called_from)
832 {
833 	struct vis_conscopy	ma;
834 	int	ncols, width;
835 
836 	/* copy */
837 	ma.s_row = nrows * tems.ts_font.vf_height;
838 	ma.e_row = tems.ts_p_dimension.height - 1;
839 	ma.t_row = 0;
840 
841 	ma.s_col = 0;
842 	ma.e_col = tems.ts_p_dimension.width - 1;
843 	ma.t_col = 0;
844 
845 	tems_safe_copy(&ma, credp, called_from);
846 
847 	/* clear */
848 	width = tems.ts_font.vf_width;
849 	ncols = (tems.ts_p_dimension.width + (width - 1))/ width;
850 
851 	tem_safe_pix_cls_range(tem, 0, nrows, tems.ts_p_offset.y,
852 	    0, ncols, 0, B_TRUE, credp, called_from);
853 }
854 
855 #define	PROM_DEFAULT_FONT_HEIGHT	22
856 #define	PROM_DEFAULT_WINDOW_TOP		0x8a
857 
858 /*
859  * This function is to compute the starting row of the console, according to
860  * PROM cursor's position. Here we have to take different fonts into account.
861  */
862 static int
863 tem_adjust_row(struct tem_vt_state *tem, int prom_row, cred_t *credp,
864     enum called_from called_from)
865 {
866 	int	tem_row;
867 	int	tem_y;
868 	int	prom_charheight = 0;
869 	int	prom_window_top = 0;
870 	int	scroll_up_lines;
871 
872 	if (tems.ts_display_mode == VIS_TEXT)
873 		return (prom_row);
874 
875 	plat_tem_get_prom_font_size(&prom_charheight, &prom_window_top);
876 	if (prom_charheight == 0)
877 		prom_charheight = PROM_DEFAULT_FONT_HEIGHT;
878 	if (prom_window_top == 0)
879 		prom_window_top = PROM_DEFAULT_WINDOW_TOP;
880 
881 	tem_y = (prom_row + 1) * prom_charheight + prom_window_top -
882 	    tems.ts_p_offset.y;
883 	tem_row = (tem_y + tems.ts_font.vf_height - 1) /
884 	    tems.ts_font.vf_height - 1;
885 
886 	if (tem_row < 0) {
887 		tem_row = 0;
888 	} else if (tem_row >= (tems.ts_c_dimension.height - 1)) {
889 		/*
890 		 * Scroll up the prom outputs if the PROM cursor's position is
891 		 * below our tem's lower boundary.
892 		 */
893 		scroll_up_lines = tem_row -
894 		    (tems.ts_c_dimension.height - 1);
895 		tem_prom_scroll_up(tem, scroll_up_lines, credp, called_from);
896 		tem_row = tems.ts_c_dimension.height - 1;
897 	}
898 
899 	return (tem_row);
900 }
901 
902 void
903 tem_align(struct tem_vt_state *tem, cred_t *credp,
904     enum called_from called_from)
905 {
906 	uint32_t row = 0;
907 	uint32_t col = 0;
908 
909 	plat_tem_hide_prom_cursor();
910 
911 	/*
912 	 * We are getting the current cursor position in pixel
913 	 * mode so that we don't over-write the console output
914 	 * during boot.
915 	 */
916 	plat_tem_get_prom_pos(&row, &col);
917 
918 	/*
919 	 * Adjust the row if necessary when the font of our
920 	 * kernel console tem is different with that of prom
921 	 * tem.
922 	 */
923 	row = tem_adjust_row(tem, row, credp, called_from);
924 
925 	/* first line of our kernel console output */
926 	tem->tvs_first_line = row + 1;
927 
928 	/* re-set and align cursor position */
929 	tem->tvs_s_cursor.row = tem->tvs_c_cursor.row =
930 	    (screen_pos_t)row;
931 	tem->tvs_s_cursor.col = tem->tvs_c_cursor.col = 0;
932 
933 	/*
934 	 * When tem is starting up, part of the screen is filled
935 	 * with information from boot loader and early boot.
936 	 * For tem, the screen content above current cursor
937 	 * should be treated as image.
938 	 */
939 	for (; row > 0; row--) {
940 		for (col = 0; col < tems.ts_c_dimension.width; col++) {
941 			tem->tvs_screen_rows[row][col].tc_char =
942 			    TEM_ATTR(TEM_ATTR_IMAGE);
943 		}
944 	}
945 }
946 
947 static void
948 tems_get_inverses(boolean_t *p_inverse, boolean_t *p_inverse_screen)
949 {
950 	int i_inverse = 0;
951 	int i_inverse_screen = 0;
952 
953 	plat_tem_get_inverses(&i_inverse, &i_inverse_screen);
954 
955 	*p_inverse = (i_inverse == 0) ? B_FALSE : B_TRUE;
956 	*p_inverse_screen = (i_inverse_screen == 0) ? B_FALSE : B_TRUE;
957 }
958 
959 /*
960  * Get the foreground/background color and attributes from the initial
961  * PROM, so that our kernel console can keep the same visual behaviour.
962  */
963 static void
964 tems_get_initial_color(tem_color_t *pcolor)
965 {
966 	boolean_t inverse, inverse_screen;
967 	unsigned short  flags = 0;
968 	uint8_t fg, bg;
969 
970 	fg = DEFAULT_ANSI_FOREGROUND;
971 	bg = DEFAULT_ANSI_BACKGROUND;
972 #ifndef _HAVE_TEM_FIRMWARE
973 	/*
974 	 * _HAVE_TEM_FIRMWARE is defined on SPARC, at this time, the
975 	 * plat_tem_get_colors() is implemented only on x86.
976 	 */
977 
978 	plat_tem_get_colors(&fg, &bg);
979 #endif
980 	pcolor->fg_color.n = fg;
981 	pcolor->bg_color.n = bg;
982 
983 	tems_get_inverses(&inverse, &inverse_screen);
984 	if (inverse)
985 		flags |= TEM_ATTR_REVERSE;
986 	if (inverse_screen)
987 		flags |= TEM_ATTR_SCREEN_REVERSE;
988 
989 #ifdef _HAVE_TEM_FIRMWARE
990 	if (flags != 0) {
991 		/*
992 		 * If either reverse flag is set, the screen is in
993 		 * white-on-black mode.  We set the bold flag to
994 		 * improve readability.
995 		 */
996 		flags |= TEM_ATTR_BOLD;
997 	} else {
998 		/*
999 		 * Otherwise, the screen is in black-on-white mode.
1000 		 * The SPARC PROM console, which starts in this mode,
1001 		 * uses the bright white background colour so we
1002 		 * match it here.
1003 		 */
1004 		if (pcolor->bg_color.n == ANSI_COLOR_WHITE)
1005 			flags |= TEM_ATTR_BRIGHT_BG;
1006 	}
1007 #else
1008 	if (flags != 0) {
1009 		if (pcolor->fg_color.n == ANSI_COLOR_WHITE)
1010 			flags |= TEM_ATTR_BRIGHT_BG;
1011 
1012 		if (pcolor->fg_color.n == ANSI_COLOR_BLACK)
1013 			flags &= ~TEM_ATTR_BRIGHT_BG;
1014 	} else {
1015 		/*
1016 		 * In case of black on white we want bright white for BG.
1017 		 */
1018 		if (pcolor->bg_color.n == ANSI_COLOR_WHITE)
1019 			flags |= TEM_ATTR_BRIGHT_BG;
1020 	}
1021 #endif
1022 
1023 	pcolor->a_flags = flags;
1024 }
1025 
1026 uchar_t
1027 tem_get_fbmode(tem_vt_state_t tem_arg)
1028 {
1029 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
1030 
1031 	uchar_t fbmode;
1032 
1033 	mutex_enter(&tem->tvs_lock);
1034 	fbmode = tem->tvs_fbmode;
1035 	mutex_exit(&tem->tvs_lock);
1036 
1037 	return (fbmode);
1038 }
1039 
1040 void
1041 tem_set_fbmode(tem_vt_state_t tem_arg, uchar_t fbmode, cred_t *credp)
1042 {
1043 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
1044 
1045 	mutex_enter(&tems.ts_lock);
1046 	mutex_enter(&tem->tvs_lock);
1047 
1048 	if (fbmode == tem->tvs_fbmode) {
1049 		mutex_exit(&tem->tvs_lock);
1050 		mutex_exit(&tems.ts_lock);
1051 		return;
1052 	}
1053 
1054 	tem->tvs_fbmode = fbmode;
1055 
1056 	if (tem->tvs_isactive) {
1057 		tem_kdsetmode(tem->tvs_fbmode, credp);
1058 		if (fbmode == KD_TEXT)
1059 			tem_safe_unblank_screen(tem, credp, CALLED_FROM_NORMAL);
1060 	}
1061 
1062 	mutex_exit(&tem->tvs_lock);
1063 	mutex_exit(&tems.ts_lock);
1064 }
1065 
1066 void
1067 tem_activate(tem_vt_state_t tem_arg, boolean_t unblank, cred_t *credp)
1068 {
1069 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
1070 
1071 	mutex_enter(&tems.ts_lock);
1072 	tems.ts_active = tem;
1073 
1074 	mutex_enter(&tem->tvs_lock);
1075 	tem->tvs_isactive = B_TRUE;
1076 
1077 	tem_kdsetmode(tem->tvs_fbmode, credp);
1078 
1079 	if (unblank)
1080 		tem_safe_unblank_screen(tem, credp, CALLED_FROM_NORMAL);
1081 
1082 	mutex_exit(&tem->tvs_lock);
1083 	mutex_exit(&tems.ts_lock);
1084 }
1085 
1086 void
1087 tem_switch(tem_vt_state_t tem_arg1, tem_vt_state_t tem_arg2, cred_t *credp)
1088 {
1089 	struct tem_vt_state *cur = (struct tem_vt_state *)tem_arg1;
1090 	struct tem_vt_state *tobe = (struct tem_vt_state *)tem_arg2;
1091 
1092 	mutex_enter(&tems.ts_lock);
1093 	mutex_enter(&tobe->tvs_lock);
1094 	mutex_enter(&cur->tvs_lock);
1095 
1096 	tems.ts_active = tobe;
1097 	cur->tvs_isactive = B_FALSE;
1098 	tobe->tvs_isactive = B_TRUE;
1099 
1100 	mutex_exit(&cur->tvs_lock);
1101 
1102 	if (cur->tvs_fbmode != tobe->tvs_fbmode)
1103 		tem_kdsetmode(tobe->tvs_fbmode, credp);
1104 
1105 	if (tobe->tvs_fbmode == KD_TEXT)
1106 		tem_safe_unblank_screen(tobe, credp, CALLED_FROM_NORMAL);
1107 
1108 	mutex_exit(&tobe->tvs_lock);
1109 	mutex_exit(&tems.ts_lock);
1110 }
1111