xref: /illumos-gate/usr/src/uts/common/io/tem.c (revision c938dc67d93d6388d698ee0d599fb7fce2963f92)
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 	tem_safe_reset_display(ptem, credp, CALLED_FROM_NORMAL,
240 	    clear_screen, init_color);
241 
242 	ptem->tvs_utf8_left = 0;
243 	ptem->tvs_utf8_partial = 0;
244 
245 	/* Get default attributes and fill up the screen buffer. */
246 	tem_safe_get_attr(ptem, &fg, &bg, &attr, TEM_ATTR_SCREEN_REVERSE);
247 	for (i = 0; i < ptem->tvs_screen_history_size; i++) {
248 		ptem->tvs_screen_rows[i] = &ptem->tvs_screen_buf[i * width];
249 
250 		for (j = 0; j < width; j++) {
251 			ptem->tvs_screen_rows[i][j].tc_fg_color = fg;
252 			ptem->tvs_screen_rows[i][j].tc_bg_color = bg;
253 			ptem->tvs_screen_rows[i][j].tc_char =
254 			    TEM_ATTR(attr) | ' ';
255 		}
256 	}
257 
258 	ptem->tvs_initialized = B_TRUE;
259 }
260 
261 boolean_t
262 tem_initialized(tem_vt_state_t tem_arg)
263 {
264 	struct tem_vt_state *ptem = (struct tem_vt_state *)tem_arg;
265 	boolean_t ret;
266 
267 	mutex_enter(&ptem->tvs_lock);
268 	ret = ptem->tvs_initialized;
269 	mutex_exit(&ptem->tvs_lock);
270 
271 	return (ret);
272 }
273 
274 tem_vt_state_t
275 tem_init(cred_t *credp)
276 {
277 	struct tem_vt_state *ptem;
278 
279 	ptem = kmem_zalloc(sizeof (struct tem_vt_state), KM_SLEEP);
280 	mutex_init(&ptem->tvs_lock, (char *)NULL, MUTEX_DRIVER, NULL);
281 
282 	mutex_enter(&tems.ts_lock);
283 	mutex_enter(&ptem->tvs_lock);
284 
285 	ptem->tvs_isactive = B_FALSE;
286 	ptem->tvs_fbmode = KD_TEXT;
287 
288 	/*
289 	 * A tem is regarded as initialized only after tem_internal_init(),
290 	 * will be set at the end of tem_internal_init().
291 	 */
292 	ptem->tvs_initialized = B_FALSE;
293 
294 
295 	if (!tems.ts_initialized) {
296 		/*
297 		 * Only happens during early console configuration.
298 		 */
299 		tem_add(ptem);
300 		mutex_exit(&ptem->tvs_lock);
301 		mutex_exit(&tems.ts_lock);
302 		return ((tem_vt_state_t)ptem);
303 	}
304 
305 	tem_internal_init(ptem, credp, B_TRUE, B_FALSE);
306 	tem_add(ptem);
307 	mutex_exit(&ptem->tvs_lock);
308 	mutex_exit(&tems.ts_lock);
309 
310 	return ((tem_vt_state_t)ptem);
311 }
312 
313 /*
314  * re-init the tem after video mode has changed and tems_info has
315  * been re-inited. The lock is already held.
316  */
317 static void
318 tem_reinit(struct tem_vt_state *tem, boolean_t reset_display)
319 {
320 	ASSERT(MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock));
321 
322 	tem_free_buf(tem); /* only free virtual buffers */
323 
324 	/* reserve color */
325 	tem_internal_init(tem, kcred, B_FALSE, reset_display);
326 }
327 
328 static void
329 tem_free_buf(struct tem_vt_state *tem)
330 {
331 	ASSERT(tem != NULL && MUTEX_HELD(&tem->tvs_lock));
332 
333 	if (tem->tvs_outbuf != NULL)
334 		kmem_free(tem->tvs_outbuf, tem->tvs_outbuf_size);
335 	if (tem->tvs_pix_data != NULL)
336 		kmem_free(tem->tvs_pix_data, tem->tvs_pix_data_size);
337 	if (tem->tvs_screen_buf != NULL)
338 		kmem_free(tem->tvs_screen_buf, tem->tvs_screen_buf_size);
339 	if (tem->tvs_screen_rows != NULL) {
340 		kmem_free(tem->tvs_screen_rows, tem->tvs_screen_history_size *
341 		    sizeof (term_char_t *));
342 	}
343 }
344 
345 void
346 tem_destroy(tem_vt_state_t tem_arg, cred_t *credp)
347 {
348 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
349 
350 	mutex_enter(&tems.ts_lock);
351 	mutex_enter(&tem->tvs_lock);
352 
353 	if (tem->tvs_isactive && tem->tvs_fbmode == KD_TEXT)
354 		tem_safe_blank_screen(tem, credp, CALLED_FROM_NORMAL);
355 
356 	tem_free_buf(tem);
357 	tem_rm(tem);
358 
359 	if (tems.ts_active == tem)
360 		tems.ts_active = NULL;
361 
362 	mutex_exit(&tem->tvs_lock);
363 	mutex_exit(&tems.ts_lock);
364 
365 	kmem_free(tem, sizeof (struct tem_vt_state));
366 }
367 
368 static int
369 tems_failed(cred_t *credp, boolean_t finish_ioctl)
370 {
371 	int	lyr_rval;
372 
373 	ASSERT(MUTEX_HELD(&tems.ts_lock));
374 
375 	if (finish_ioctl)
376 		(void) ldi_ioctl(tems.ts_hdl, VIS_DEVFINI, 0,
377 		    FWRITE|FKIOCTL, credp, &lyr_rval);
378 
379 	(void) ldi_close(tems.ts_hdl, 0, credp);
380 	tems.ts_hdl = NULL;
381 	return (ENXIO);
382 }
383 
384 /*
385  * only called once during boot
386  */
387 int
388 tem_info_init(char *pathname, cred_t *credp)
389 {
390 	int			lyr_rval, ret;
391 	struct vis_devinit	temargs;
392 	char			*pathbuf;
393 	size_t height = 0;
394 	size_t width = 0;
395 	struct tem_vt_state *p;
396 
397 	mutex_enter(&tems.ts_lock);
398 
399 	if (tems.ts_initialized) {
400 		mutex_exit(&tems.ts_lock);
401 		return (0);
402 	}
403 
404 	/*
405 	 * Open the layered device using the devfs physical device name
406 	 * after adding the /devices prefix.
407 	 */
408 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
409 	(void) strcpy(pathbuf, "/devices");
410 	if (i_ddi_prompath_to_devfspath(pathname,
411 	    pathbuf + strlen("/devices")) != DDI_SUCCESS) {
412 		cmn_err(CE_WARN, "terminal-emulator:  path conversion error");
413 		kmem_free(pathbuf, MAXPATHLEN);
414 
415 		mutex_exit(&tems.ts_lock);
416 		return (ENXIO);
417 	}
418 	if (ldi_open_by_name(pathbuf, FWRITE, credp,
419 	    &tems.ts_hdl, term_li) != 0) {
420 		cmn_err(CE_WARN, "terminal-emulator:  device path open error");
421 		kmem_free(pathbuf, MAXPATHLEN);
422 
423 		mutex_exit(&tems.ts_lock);
424 		return (ENXIO);
425 	}
426 	kmem_free(pathbuf, MAXPATHLEN);
427 
428 	temargs.modechg_cb  = (vis_modechg_cb_t)tems_modechange_callback;
429 	temargs.modechg_arg = NULL;
430 
431 	/*
432 	 * Initialize the console and get the device parameters
433 	 */
434 	if (ldi_ioctl(tems.ts_hdl, VIS_DEVINIT,
435 	    (intptr_t)&temargs, FWRITE|FKIOCTL, credp, &lyr_rval) != 0) {
436 		cmn_err(CE_WARN, "terminal emulator: Compatible fb not found");
437 		ret = tems_failed(credp, B_FALSE);
438 		mutex_exit(&tems.ts_lock);
439 		return (ret);
440 	}
441 
442 	/* Make sure the fb driver and terminal emulator versions match */
443 	if (temargs.version != VIS_CONS_REV) {
444 		cmn_err(CE_WARN,
445 		    "terminal emulator: VIS_CONS_REV %d (see sys/visual_io.h) "
446 		    "of console fb driver not supported", temargs.version);
447 		ret = tems_failed(credp, B_TRUE);
448 		mutex_exit(&tems.ts_lock);
449 		return (ret);
450 	}
451 
452 	if ((tems.ts_fb_polledio = temargs.polledio) == NULL) {
453 		cmn_err(CE_WARN, "terminal emulator: fb doesn't support polled "
454 		    "I/O");
455 		ret = tems_failed(credp, B_TRUE);
456 		mutex_exit(&tems.ts_lock);
457 		return (ret);
458 	}
459 
460 	/* other sanity checks */
461 	if (!((temargs.depth == 4) || (temargs.depth == 8) ||
462 	    (temargs.depth == 15) || (temargs.depth == 16) ||
463 	    (temargs.depth == 24) || (temargs.depth == 32))) {
464 		cmn_err(CE_WARN, "terminal emulator: unsupported depth");
465 		ret = tems_failed(credp, B_TRUE);
466 		mutex_exit(&tems.ts_lock);
467 		return (ret);
468 	}
469 
470 	if ((temargs.mode != VIS_TEXT) && (temargs.mode != VIS_PIXEL)) {
471 		cmn_err(CE_WARN, "terminal emulator: unsupported mode");
472 		ret = tems_failed(credp, B_TRUE);
473 		mutex_exit(&tems.ts_lock);
474 		return (ret);
475 	}
476 
477 	if ((temargs.mode == VIS_PIXEL) && plat_stdout_is_framebuffer())
478 		plat_tem_get_prom_size(&height, &width);
479 
480 	/*
481 	 * Initialize the common terminal emulator info
482 	 */
483 	tems_setup_terminal(&temargs, height, width);
484 
485 	tems_reset_colormap(credp, CALLED_FROM_NORMAL);
486 	tems_get_initial_color(&tems.ts_init_color);
487 
488 	tems.ts_initialized = 1; /* initialization flag */
489 
490 	for (p = list_head(&tems.ts_list); p != NULL;
491 	    p = list_next(&tems.ts_list, p)) {
492 		mutex_enter(&p->tvs_lock);
493 		tem_internal_init(p, credp, B_TRUE, B_FALSE);
494 		if (temargs.mode == VIS_PIXEL)
495 			tem_pix_align(p, credp, CALLED_FROM_NORMAL);
496 		mutex_exit(&p->tvs_lock);
497 	}
498 
499 	mutex_exit(&tems.ts_lock);
500 	return (0);
501 }
502 
503 #define	TEMS_DEPTH_DIFF		0x01
504 #define	TEMS_DIMENSION_DIFF	0x02
505 
506 static uchar_t
507 tems_check_videomode(struct vis_devinit *tp)
508 {
509 	uchar_t result = 0;
510 
511 	if (tems.ts_pdepth != tp->depth)
512 		result |= TEMS_DEPTH_DIFF;
513 
514 	if (tp->mode == VIS_TEXT) {
515 		if (tems.ts_c_dimension.width != tp->width ||
516 		    tems.ts_c_dimension.height != tp->height)
517 			result |= TEMS_DIMENSION_DIFF;
518 	} else {
519 		if (tems.ts_p_dimension.width != tp->width ||
520 		    tems.ts_p_dimension.height != tp->height)
521 			result |= TEMS_DIMENSION_DIFF;
522 	}
523 
524 	return (result);
525 }
526 
527 static void
528 tems_setup_terminal(struct vis_devinit *tp, size_t height, size_t width)
529 {
530 	bitmap_data_t *font_data;
531 	int i;
532 	int old_blank_buf_size = tems.ts_c_dimension.width *
533 	    sizeof (*tems.ts_blank_line);
534 
535 	ASSERT(MUTEX_HELD(&tems.ts_lock));
536 
537 	tems.ts_pdepth = tp->depth;
538 	tems.ts_linebytes = tp->linebytes;
539 	tems.ts_display_mode = tp->mode;
540 	tems.ts_color_map = tp->color_map;
541 
542 	switch (tp->mode) {
543 	case VIS_TEXT:
544 		tems.ts_p_dimension.width = 0;
545 		tems.ts_p_dimension.height = 0;
546 		tems.ts_c_dimension.width = tp->width;
547 		tems.ts_c_dimension.height = tp->height;
548 		tems.ts_callbacks = &tem_safe_text_callbacks;
549 
550 		break;
551 
552 	case VIS_PIXEL:
553 		/*
554 		 * First check to see if the user has specified a screen size.
555 		 * If so, use those values.  Else use 34x80 as the default.
556 		 */
557 		if (width == 0) {
558 			width = TEM_DEFAULT_COLS;
559 			height = TEM_DEFAULT_ROWS;
560 		}
561 		tems.ts_c_dimension.height = (screen_size_t)height;
562 		tems.ts_c_dimension.width = (screen_size_t)width;
563 
564 		tems.ts_p_dimension.height = tp->height;
565 		tems.ts_p_dimension.width = tp->width;
566 
567 		tems.ts_callbacks = &tem_safe_pix_callbacks;
568 
569 		/*
570 		 * set_font() will select a appropriate sized font for
571 		 * the number of rows and columns selected. If we don't
572 		 * have a font that will fit, then it will use the
573 		 * default builtin font. set_font() will adjust the rows
574 		 * and columns to fit on the screen.
575 		 */
576 		font_data = set_font(&tems.ts_c_dimension.height,
577 		    &tems.ts_c_dimension.width,
578 		    tems.ts_p_dimension.height,
579 		    tems.ts_p_dimension.width);
580 
581 		for (i = 0; i < VFNT_MAPS; i++) {
582 			tems.ts_font.vf_map[i] =
583 			    font_data->font->vf_map[i];
584 			tems.ts_font.vf_map_count[i] =
585 			    font_data->font->vf_map_count[i];
586 		}
587 		tems.ts_font.vf_bytes = font_data->font->vf_bytes;
588 		tems.ts_font.vf_width = font_data->font->vf_width;
589 		tems.ts_font.vf_height = font_data->font->vf_height;
590 
591 		tems.ts_p_offset.y = (tems.ts_p_dimension.height -
592 		    (tems.ts_c_dimension.height * tems.ts_font.vf_height)) / 2;
593 		tems.ts_p_offset.x = (tems.ts_p_dimension.width -
594 		    (tems.ts_c_dimension.width * tems.ts_font.vf_width)) / 2;
595 
596 		tems.ts_pix_data_size =
597 		    tems.ts_font.vf_width * tems.ts_font.vf_height;
598 
599 		tems.ts_pix_data_size *= 4;
600 
601 		tems.ts_pdepth = tp->depth;
602 
603 		break;
604 	}
605 
606 	/* Now virtual cls also uses the blank_line buffer */
607 	if (tems.ts_blank_line)
608 		kmem_free(tems.ts_blank_line, old_blank_buf_size);
609 
610 	tems.ts_blank_line = kmem_alloc(tems.ts_c_dimension.width *
611 	    sizeof (*tems.ts_blank_line), KM_SLEEP);
612 }
613 
614 /*
615  * This is a callback function that we register with the frame
616  * buffer driver layered underneath.  It gets invoked from
617  * the underlying frame buffer driver to reconfigure the terminal
618  * emulator to a new screen size and depth in conjunction with
619  * framebuffer videomode changes.
620  * Here we keep the foreground/background color and attributes,
621  * which may be different with the initial settings, so that
622  * the color won't change while the framebuffer videomode changes.
623  * And we also reset the kernel terminal emulator and clear the
624  * whole screen.
625  */
626 /* ARGSUSED */
627 void
628 tems_modechange_callback(struct vis_modechg_arg *arg,
629     struct vis_devinit *devinit)
630 {
631 	uchar_t diff;
632 	struct tem_vt_state *p;
633 	tem_modechg_cb_t cb;
634 	tem_modechg_cb_arg_t cb_arg;
635 
636 	ASSERT(!(list_is_empty(&tems.ts_list)));
637 
638 	mutex_enter(&tems.ts_lock);
639 
640 	/*
641 	 * currently only for pixel mode
642 	 */
643 	diff = tems_check_videomode(devinit);
644 	if (diff == 0) {
645 		mutex_exit(&tems.ts_lock);
646 		return;
647 	}
648 
649 	diff = diff & TEMS_DIMENSION_DIFF;
650 
651 	if (diff == 0) {
652 		/*
653 		 * Only need to reinit the active tem.
654 		 */
655 		struct tem_vt_state *active = tems.ts_active;
656 		tems.ts_pdepth = devinit->depth;
657 
658 		mutex_enter(&active->tvs_lock);
659 		ASSERT(active->tvs_isactive);
660 		tem_reinit(active, B_TRUE);
661 		mutex_exit(&active->tvs_lock);
662 
663 		mutex_exit(&tems.ts_lock);
664 		return;
665 	}
666 
667 	tems_setup_terminal(devinit, tems.ts_c_dimension.height,
668 	    tems.ts_c_dimension.width);
669 
670 	for (p = list_head(&tems.ts_list); p != NULL;
671 	    p = list_next(&tems.ts_list, p)) {
672 		mutex_enter(&p->tvs_lock);
673 		tem_reinit(p, p->tvs_isactive);
674 		mutex_exit(&p->tvs_lock);
675 	}
676 
677 
678 	if (tems.ts_modechg_cb == NULL) {
679 		mutex_exit(&tems.ts_lock);
680 		return;
681 	}
682 
683 	cb = tems.ts_modechg_cb;
684 	cb_arg = tems.ts_modechg_arg;
685 
686 	/*
687 	 * Release the lock while doing callback.
688 	 */
689 	mutex_exit(&tems.ts_lock);
690 	cb(cb_arg);
691 }
692 
693 /*
694  * This function is used to clear entire screen via the underlying framebuffer
695  * driver.
696  */
697 int
698 tems_cls_layered(struct vis_consclear *pda,
699     cred_t *credp)
700 {
701 	int rval;
702 
703 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCLEAR,
704 	    (intptr_t)pda, FKIOCTL, credp, &rval);
705 	return (rval);
706 }
707 
708 /*
709  * This function is used to display a rectangular blit of data
710  * of a given size and location via the underlying framebuffer driver.
711  * The blit can be as small as a pixel or as large as the screen.
712  */
713 void
714 tems_display_layered(struct vis_consdisplay *pda,
715     cred_t *credp)
716 {
717 	int rval;
718 
719 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSDISPLAY,
720 	    (intptr_t)pda, FKIOCTL, credp, &rval);
721 }
722 
723 /*
724  * This function is used to invoke a block copy operation in the
725  * underlying framebuffer driver.  Rectangle copies are how scrolling
726  * is implemented, as well as horizontal text shifting escape seqs.
727  * such as from vi when deleting characters and words.
728  */
729 void
730 tems_copy_layered(struct vis_conscopy *pma,
731     cred_t *credp)
732 {
733 	int rval;
734 
735 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCOPY,
736 	    (intptr_t)pma, FKIOCTL, credp, &rval);
737 }
738 
739 /*
740  * This function is used to show or hide a rectangluar monochrom
741  * pixel inverting, text block cursor via the underlying framebuffer.
742  */
743 void
744 tems_cursor_layered(struct vis_conscursor *pca,
745     cred_t *credp)
746 {
747 	int rval;
748 
749 	(void) ldi_ioctl(tems.ts_hdl, VIS_CONSCURSOR,
750 	    (intptr_t)pca, FKIOCTL, credp, &rval);
751 }
752 
753 static void
754 tem_kdsetmode(int mode, cred_t *credp)
755 {
756 	int rval;
757 
758 	(void) ldi_ioctl(tems.ts_hdl, KDSETMODE,
759 	    (intptr_t)mode, FKIOCTL, credp, &rval);
760 
761 }
762 
763 static void
764 tems_reset_colormap(cred_t *credp, enum called_from called_from)
765 {
766 	struct vis_cmap cm;
767 	int rval;
768 
769 	if (called_from == CALLED_FROM_STANDALONE)
770 		return;
771 
772 	switch (tems.ts_pdepth) {
773 	case 8:
774 		cm.index = 0;
775 		cm.count = 16;
776 		cm.red   = (uint8_t *)cmap4_to_24.red;
777 		cm.blue  = (uint8_t *)cmap4_to_24.blue;
778 		cm.green = (uint8_t *)cmap4_to_24.green;
779 		(void) ldi_ioctl(tems.ts_hdl, VIS_PUTCMAP, (intptr_t)&cm,
780 		    FKIOCTL, credp, &rval);
781 		break;
782 	}
783 }
784 
785 void
786 tem_get_size(ushort_t *r, ushort_t *c, ushort_t *x, ushort_t *y)
787 {
788 	mutex_enter(&tems.ts_lock);
789 	*r = (ushort_t)tems.ts_c_dimension.height;
790 	*c = (ushort_t)tems.ts_c_dimension.width;
791 	*x = (ushort_t)tems.ts_p_dimension.width;
792 	*y = (ushort_t)tems.ts_p_dimension.height;
793 	mutex_exit(&tems.ts_lock);
794 }
795 
796 void
797 tem_register_modechg_cb(tem_modechg_cb_t func, tem_modechg_cb_arg_t arg)
798 {
799 	mutex_enter(&tems.ts_lock);
800 
801 	tems.ts_modechg_cb = func;
802 	tems.ts_modechg_arg = arg;
803 
804 	mutex_exit(&tems.ts_lock);
805 }
806 
807 /*
808  * This function is to scroll up the OBP output, which has
809  * different screen height and width with our kernel console.
810  */
811 static void
812 tem_prom_scroll_up(struct tem_vt_state *tem, int nrows, cred_t *credp,
813     enum called_from called_from)
814 {
815 	struct vis_conscopy	ma;
816 	int	ncols, width;
817 
818 	/* copy */
819 	ma.s_row = nrows * tems.ts_font.vf_height;
820 	ma.e_row = tems.ts_p_dimension.height - 1;
821 	ma.t_row = 0;
822 
823 	ma.s_col = 0;
824 	ma.e_col = tems.ts_p_dimension.width - 1;
825 	ma.t_col = 0;
826 
827 	tems_safe_copy(&ma, credp, called_from);
828 
829 	/* clear */
830 	width = tems.ts_font.vf_width;
831 	ncols = (tems.ts_p_dimension.width + (width - 1))/ width;
832 
833 	tem_safe_pix_cls_range(tem, 0, nrows, tems.ts_p_offset.y,
834 	    0, ncols, 0, B_TRUE, credp, called_from);
835 }
836 
837 #define	PROM_DEFAULT_FONT_HEIGHT	22
838 #define	PROM_DEFAULT_WINDOW_TOP		0x8a
839 
840 /*
841  * This function is to compute the starting row of the console, according to
842  * PROM cursor's position. Here we have to take different fonts into account.
843  */
844 static int
845 tem_adjust_row(struct tem_vt_state *tem, int prom_row, cred_t *credp,
846     enum called_from called_from)
847 {
848 	int	tem_row;
849 	int	tem_y;
850 	int	prom_charheight = 0;
851 	int	prom_window_top = 0;
852 	int	scroll_up_lines;
853 
854 	plat_tem_get_prom_font_size(&prom_charheight, &prom_window_top);
855 	if (prom_charheight == 0)
856 		prom_charheight = PROM_DEFAULT_FONT_HEIGHT;
857 	if (prom_window_top == 0)
858 		prom_window_top = PROM_DEFAULT_WINDOW_TOP;
859 
860 	tem_y = (prom_row + 1) * prom_charheight + prom_window_top -
861 	    tems.ts_p_offset.y;
862 	tem_row = (tem_y + tems.ts_font.vf_height - 1) /
863 	    tems.ts_font.vf_height - 1;
864 
865 	if (tem_row < 0) {
866 		tem_row = 0;
867 	} else if (tem_row >= (tems.ts_c_dimension.height - 1)) {
868 		/*
869 		 * Scroll up the prom outputs if the PROM cursor's position is
870 		 * below our tem's lower boundary.
871 		 */
872 		scroll_up_lines = tem_row -
873 		    (tems.ts_c_dimension.height - 1);
874 		tem_prom_scroll_up(tem, scroll_up_lines, credp, called_from);
875 		tem_row = tems.ts_c_dimension.height - 1;
876 	}
877 
878 	return (tem_row);
879 }
880 
881 void
882 tem_pix_align(struct tem_vt_state *tem, cred_t *credp,
883     enum called_from called_from)
884 {
885 	uint32_t row = 0;
886 	uint32_t col = 0;
887 
888 	if (plat_stdout_is_framebuffer()) {
889 		plat_tem_hide_prom_cursor();
890 
891 		/*
892 		 * We are getting the current cursor position in pixel
893 		 * mode so that we don't over-write the console output
894 		 * during boot.
895 		 */
896 		plat_tem_get_prom_pos(&row, &col);
897 
898 		/*
899 		 * Adjust the row if necessary when the font of our
900 		 * kernel console tem is different with that of prom
901 		 * tem.
902 		 */
903 		row = tem_adjust_row(tem, row, credp, called_from);
904 
905 		/* first line of our kernel console output */
906 		tem->tvs_first_line = row + 1;
907 
908 		/* re-set and align cusror position */
909 		tem->tvs_s_cursor.row = tem->tvs_c_cursor.row =
910 		    (screen_pos_t)row;
911 		tem->tvs_s_cursor.col = tem->tvs_c_cursor.col = 0;
912 
913 		/*
914 		 * When tem is starting up, part of the screen is filled
915 		 * with information from boot loader and early boot.
916 		 * For tem, the screen content above current cursor
917 		 * should be treated as image.
918 		 */
919 		for (; row > 0; row--) {
920 			for (col = 0; col < tems.ts_c_dimension.width; col++) {
921 				tem->tvs_screen_rows[row][col].tc_char =
922 				    TEM_ATTR(TEM_ATTR_IMAGE);
923 			}
924 		}
925 	} else {
926 		tem_safe_reset_display(tem, credp, called_from, B_TRUE, B_TRUE);
927 	}
928 }
929 
930 static void
931 tems_get_inverses(boolean_t *p_inverse, boolean_t *p_inverse_screen)
932 {
933 	int i_inverse = 0;
934 	int i_inverse_screen = 0;
935 
936 	plat_tem_get_inverses(&i_inverse, &i_inverse_screen);
937 
938 	*p_inverse = (i_inverse == 0) ? B_FALSE : B_TRUE;
939 	*p_inverse_screen = (i_inverse_screen == 0) ? B_FALSE : B_TRUE;
940 }
941 
942 /*
943  * Get the foreground/background color and attributes from the initial
944  * PROM, so that our kernel console can keep the same visual behaviour.
945  */
946 static void
947 tems_get_initial_color(tem_color_t *pcolor)
948 {
949 	boolean_t inverse, inverse_screen;
950 	unsigned short  flags = 0;
951 
952 	pcolor->fg_color = DEFAULT_ANSI_FOREGROUND;
953 	pcolor->bg_color = DEFAULT_ANSI_BACKGROUND;
954 #ifndef _HAVE_TEM_FIRMWARE
955 	/*
956 	 * _HAVE_TEM_FIRMWARE is defined on SPARC, at this time, the
957 	 * plat_tem_get_colors() is implemented only on x86.
958 	 */
959 	plat_tem_get_colors(&pcolor->fg_color, &pcolor->bg_color);
960 #endif
961 
962 	tems_get_inverses(&inverse, &inverse_screen);
963 	if (inverse)
964 		flags |= TEM_ATTR_REVERSE;
965 	if (inverse_screen)
966 		flags |= TEM_ATTR_SCREEN_REVERSE;
967 
968 	if (flags != 0) {
969 		/*
970 		 * If either reverse flag is set, the screen is in
971 		 * white-on-black mode.  We set the bold flag to
972 		 * improve readability.
973 		 */
974 		flags |= TEM_ATTR_BOLD;
975 	} else {
976 		/*
977 		 * Otherwise, the screen is in black-on-white mode.
978 		 * The SPARC PROM console, which starts in this mode,
979 		 * uses the bright white background colour so we
980 		 * match it here.
981 		 */
982 		if (pcolor->bg_color == ANSI_COLOR_WHITE)
983 			flags |= TEM_ATTR_BRIGHT_BG;
984 	}
985 
986 	pcolor->a_flags = flags;
987 }
988 
989 uchar_t
990 tem_get_fbmode(tem_vt_state_t tem_arg)
991 {
992 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
993 
994 	uchar_t fbmode;
995 
996 	mutex_enter(&tem->tvs_lock);
997 	fbmode = tem->tvs_fbmode;
998 	mutex_exit(&tem->tvs_lock);
999 
1000 	return (fbmode);
1001 }
1002 
1003 void
1004 tem_set_fbmode(tem_vt_state_t tem_arg, uchar_t fbmode, cred_t *credp)
1005 {
1006 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
1007 
1008 	mutex_enter(&tems.ts_lock);
1009 	mutex_enter(&tem->tvs_lock);
1010 
1011 	if (fbmode == tem->tvs_fbmode) {
1012 		mutex_exit(&tem->tvs_lock);
1013 		mutex_exit(&tems.ts_lock);
1014 		return;
1015 	}
1016 
1017 	tem->tvs_fbmode = fbmode;
1018 
1019 	if (tem->tvs_isactive) {
1020 		tem_kdsetmode(tem->tvs_fbmode, credp);
1021 		if (fbmode == KD_TEXT)
1022 			tem_safe_unblank_screen(tem, credp, CALLED_FROM_NORMAL);
1023 	}
1024 
1025 	mutex_exit(&tem->tvs_lock);
1026 	mutex_exit(&tems.ts_lock);
1027 }
1028 
1029 void
1030 tem_activate(tem_vt_state_t tem_arg, boolean_t unblank, cred_t *credp)
1031 {
1032 	struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg;
1033 
1034 	mutex_enter(&tems.ts_lock);
1035 	tems.ts_active = tem;
1036 
1037 	mutex_enter(&tem->tvs_lock);
1038 	tem->tvs_isactive = B_TRUE;
1039 
1040 	tem_kdsetmode(tem->tvs_fbmode, credp);
1041 
1042 	if (unblank)
1043 		tem_safe_unblank_screen(tem, credp, CALLED_FROM_NORMAL);
1044 
1045 	mutex_exit(&tem->tvs_lock);
1046 	mutex_exit(&tems.ts_lock);
1047 }
1048 
1049 void
1050 tem_switch(tem_vt_state_t tem_arg1, tem_vt_state_t tem_arg2, cred_t *credp)
1051 {
1052 	struct tem_vt_state *cur = (struct tem_vt_state *)tem_arg1;
1053 	struct tem_vt_state *tobe = (struct tem_vt_state *)tem_arg2;
1054 
1055 	mutex_enter(&tems.ts_lock);
1056 	mutex_enter(&tobe->tvs_lock);
1057 	mutex_enter(&cur->tvs_lock);
1058 
1059 	tems.ts_active = tobe;
1060 	cur->tvs_isactive = B_FALSE;
1061 	tobe->tvs_isactive = B_TRUE;
1062 
1063 	mutex_exit(&cur->tvs_lock);
1064 
1065 	if (cur->tvs_fbmode != tobe->tvs_fbmode)
1066 		tem_kdsetmode(tobe->tvs_fbmode, credp);
1067 
1068 	if (tobe->tvs_fbmode == KD_TEXT)
1069 		tem_safe_unblank_screen(tobe, credp, CALLED_FROM_NORMAL);
1070 
1071 	mutex_exit(&tobe->tvs_lock);
1072 	mutex_exit(&tems.ts_lock);
1073 }
1074