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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
29 /*		All Rights Reserved	*/
30 
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/conf.h>
34 #include <sys/kmem.h>
35 #include <sys/visual_io.h>
36 #include <sys/font.h>
37 #include <sys/fbio.h>
38 #include <sys/ddi.h>
39 #include <sys/stat.h>
40 #include <sys/sunddi.h>
41 #include <sys/open.h>
42 #include <sys/modctl.h>
43 #include <sys/pci.h>
44 #include <sys/kd.h>
45 #include <sys/ddi_impldefs.h>
46 #include <sys/gfx_private.h>
47 #include <sys/vgareg.h>
48 #include "gfxp_fb.h"
49 
50 #define	MYNAME	"gfxp_vgatext"
51 
52 static ddi_device_acc_attr_t dev_attr = {
53 	DDI_DEVICE_ATTR_V0,
54 	DDI_NEVERSWAP_ACC,
55 	DDI_STRICTORDER_ACC,
56 };
57 
58 /* default structure for FBIOGATTR ioctl */
59 static struct fbgattr vgatext_attr =  {
60 /*	real_type	owner */
61 	FBTYPE_SUNFAST_COLOR, 0,
62 /* fbtype: type		h  w  depth cms  size */
63 	{ FBTYPE_SUNFAST_COLOR, VGA_TEXT_ROWS, VGA_TEXT_COLS, 1,    256,  0 },
64 /* fbsattr: flags emu_type	dev_specific */
65 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
66 /*	emu_types */
67 	{ -1 }
68 };
69 
70 static struct vis_identifier gfxp_vgatext_ident = { "illumos_text" };
71 
72 static int vgatext_devinit(struct gfxp_fb_softc *, struct vis_devinit *data);
73 static void	vgatext_cons_copy(struct gfxp_fb_softc *,
74 			struct vis_conscopy *);
75 static void	vgatext_cons_display(struct gfxp_fb_softc *,
76 			struct vis_consdisplay *);
77 static int	vgatext_cons_clear(struct gfxp_fb_softc *,
78 			struct vis_consclear *);
79 static void	vgatext_cons_cursor(struct gfxp_fb_softc *,
80 			struct vis_conscursor *);
81 static void	vgatext_polled_copy(struct vis_polledio_arg *,
82 			struct vis_conscopy *);
83 static void	vgatext_polled_display(struct vis_polledio_arg *,
84 			struct vis_consdisplay *);
85 static void	vgatext_polled_cursor(struct vis_polledio_arg *,
86 			struct vis_conscursor *);
87 static void	vgatext_init(struct gfxp_fb_softc *);
88 static void	vgatext_set_text(struct gfxp_fb_softc *);
89 
90 static void	vgatext_get_text(struct gfxp_fb_softc *softc);
91 static void	vgatext_save_text(struct gfxp_fb_softc *softc);
92 static void	vgatext_kdsettext(struct gfxp_fb_softc *softc);
93 static int	vgatext_suspend(struct gfxp_fb_softc *softc);
94 static void	vgatext_resume(struct gfxp_fb_softc *softc);
95 static int	vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
96     size_t *, uint_t, void *);
97 
98 #if	defined(USE_BORDERS)
99 static void	vgatext_init_graphics(struct gfxp_fb_softc *);
100 #endif
101 
102 static int vgatext_kdsetmode(struct gfxp_fb_softc *softc, int mode);
103 static void vgatext_setfont(struct gfxp_fb_softc *softc);
104 static void vgatext_get_cursor(struct gfxp_fb_softc *softc,
105     screen_pos_t *row, screen_pos_t *col);
106 static void vgatext_set_cursor(struct gfxp_fb_softc *softc, int row, int col);
107 static void vgatext_hide_cursor(struct gfxp_fb_softc *softc);
108 static void vgatext_save_colormap(struct gfxp_fb_softc *softc);
109 static void vgatext_restore_colormap(struct gfxp_fb_softc *softc);
110 static int vgatext_get_pci_reg_index(dev_info_t *const devi,
111     unsigned long himask, unsigned long hival, unsigned long addr,
112 		off_t *offset);
113 static int vgatext_get_isa_reg_index(dev_info_t *const devi,
114 		unsigned long hival, unsigned long addr, off_t *offset);
115 
116 static struct gfxp_ops gfxp_vgatext_ops = {
117 	.ident = &gfxp_vgatext_ident,
118 	.kdsetmode = vgatext_kdsetmode,
119 	.devinit = vgatext_devinit,
120 	.cons_copy = vgatext_cons_copy,
121 	.cons_display = vgatext_cons_display,
122 	.cons_cursor = vgatext_cons_cursor,
123 	.cons_clear = vgatext_cons_clear,
124 	.suspend = vgatext_suspend,
125 	.resume = vgatext_resume,
126 	.devmap = vgatext_devmap
127 };
128 
129 #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
130 
131 int
gfxp_vga_attach(dev_info_t * devi,struct gfxp_fb_softc * softc)132 gfxp_vga_attach(dev_info_t *devi, struct gfxp_fb_softc *softc)
133 {
134 	int	unit = ddi_get_instance(devi);
135 	int	error;
136 	char	*parent_type = NULL;
137 	int	reg_rnumber;
138 	off_t	reg_offset;
139 	off_t	mem_offset;
140 	char	*cons;
141 	struct gfx_vga *vga;
142 
143 
144 	softc->polledio.display = vgatext_polled_display;
145 	softc->polledio.copy = vgatext_polled_copy;
146 	softc->polledio.cursor = vgatext_polled_cursor;
147 	softc->gfxp_ops = &gfxp_vgatext_ops;
148 	softc->fbgattr = &vgatext_attr;
149 	vga = kmem_zalloc(sizeof (*vga), KM_SLEEP);
150 	softc->console = (union gfx_console *)vga;
151 
152 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
153 	    DDI_PROP_DONTPASS, "device_type", &parent_type);
154 	if (error != DDI_SUCCESS) {
155 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
156 		goto fail;
157 	}
158 
159 	/* Not enable AGP and DRM by default */
160 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
161 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
162 		    &reg_offset);
163 		if (reg_rnumber < 0) {
164 			cmn_err(CE_WARN,
165 			    MYNAME
166 			    ": can't find reg entry for registers");
167 			error = DDI_FAILURE;
168 			goto fail;
169 		}
170 		vga->fb_regno = vgatext_get_isa_reg_index(devi, 0,
171 		    VGA_MEM_ADDR, &mem_offset);
172 		if (vga->fb_regno < 0) {
173 			cmn_err(CE_WARN,
174 			    MYNAME ": can't find reg entry for memory");
175 			error = DDI_FAILURE;
176 			goto fail;
177 		}
178 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
179 		reg_rnumber = vgatext_get_pci_reg_index(devi,
180 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
181 		    PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
182 		    &reg_offset);
183 		if (reg_rnumber < 0) {
184 			cmn_err(CE_WARN,
185 			    MYNAME ": can't find reg entry for registers");
186 			error = DDI_FAILURE;
187 			goto fail;
188 		}
189 		vga->fb_regno = vgatext_get_pci_reg_index(devi,
190 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
191 		    PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
192 		    &mem_offset);
193 		if (vga->fb_regno < 0) {
194 			cmn_err(CE_WARN,
195 			    MYNAME ": can't find reg entry for memory");
196 			error = DDI_FAILURE;
197 			goto fail;
198 		}
199 	} else {
200 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
201 		    parent_type);
202 		error = DDI_FAILURE;
203 		goto fail;
204 	}
205 	ddi_prop_free(parent_type);
206 	parent_type = NULL;
207 
208 	error = ddi_regs_map_setup(devi, reg_rnumber,
209 	    (caddr_t *)&vga->regs.addr, reg_offset, VGA_REG_SIZE,
210 	    &dev_attr, &vga->regs.handle);
211 	if (error != DDI_SUCCESS)
212 		goto fail;
213 	vga->regs.mapped = B_TRUE;
214 
215 	vga->fb_size = VGA_MEM_SIZE;
216 
217 	error = ddi_regs_map_setup(devi, vga->fb_regno,
218 	    (caddr_t *)&vga->fb.addr, mem_offset, vga->fb_size, &dev_attr,
219 	    &vga->fb.handle);
220 	if (error != DDI_SUCCESS)
221 		goto fail;
222 	vga->fb.mapped = B_TRUE;
223 
224 	if (ddi_get8(vga->regs.handle,
225 	    vga->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
226 		vga->text_base = (caddr_t)vga->fb.addr + VGA_COLOR_BASE;
227 	else
228 		vga->text_base = (caddr_t)vga->fb.addr + VGA_MONO_BASE;
229 
230 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
231 	    DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
232 		if (strcmp(cons, "graphics") == 0) {
233 			softc->happyface_boot = 1;
234 			softc->silent = 1;
235 			vga->current_base = vga->shadow;
236 		} else {
237 			vga->current_base = vga->text_base;
238 		}
239 		ddi_prop_free(cons);
240 	} else {
241 		vga->current_base = vga->text_base;
242 	}
243 
244 	/* Set cursor info. */
245 	vga->cursor.visible = fb_info.cursor.visible;
246 	vga->cursor.row = fb_info.cursor.pos.y;
247 	vga->cursor.col = fb_info.cursor.pos.x;
248 
249 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
250 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
251 	if (error != DDI_SUCCESS)
252 		goto fail;
253 
254 	/* only do this if not in graphics mode */
255 	if ((softc->silent == 0) && (GFXP_IS_CONSOLE(softc))) {
256 		vgatext_init(softc);
257 		vgatext_save_colormap(softc);
258 	}
259 
260 	return (DDI_SUCCESS);
261 
262 fail:
263 	kmem_free(vga, sizeof (*vga));
264 	if (parent_type != NULL)
265 		ddi_prop_free(parent_type);
266 	return (error);
267 }
268 
269 int
gfxp_vga_detach(dev_info_t * devi __unused,struct gfxp_fb_softc * softc)270 gfxp_vga_detach(dev_info_t *devi __unused, struct gfxp_fb_softc *softc)
271 {
272 	if (softc->console->vga.fb.mapped)
273 		ddi_regs_map_free(&softc->console->vga.fb.handle);
274 	if (softc->console->vga.regs.mapped)
275 		ddi_regs_map_free(&softc->console->vga.regs.handle);
276 	kmem_free(softc->console, sizeof (struct gfx_vga));
277 	return (DDI_SUCCESS);
278 }
279 
280 /*
281  * vgatext_save_text
282  * vgatext_suspend
283  * vgatext_resume
284  *
285  *	Routines to save and restore contents of the VGA text area
286  * Mostly, this is to support Suspend/Resume operation for graphics
287  * device drivers.  Here in the VGAtext common code, we simply squirrel
288  * away the contents of the hardware's text area during Suspend and then
289  * put it back during Resume
290  */
291 static void
vgatext_save_text(struct gfxp_fb_softc * softc)292 vgatext_save_text(struct gfxp_fb_softc *softc)
293 {
294 	union gfx_console *console = softc->console;
295 	unsigned i;
296 
297 	for (i = 0; i < sizeof (console->vga.shadow); i++)
298 		console->vga.shadow[i] = console->vga.current_base[i];
299 }
300 
301 static int
vgatext_suspend(struct gfxp_fb_softc * softc)302 vgatext_suspend(struct gfxp_fb_softc *softc)
303 {
304 	switch (softc->mode) {
305 	case KD_TEXT:
306 		vgatext_save_text(softc);
307 		break;
308 
309 	case KD_GRAPHICS:
310 		break;
311 
312 	default:
313 		cmn_err(CE_WARN, MYNAME ": unknown mode in vgatext_suspend.");
314 		return (DDI_FAILURE);
315 	}
316 	return (DDI_SUCCESS);
317 }
318 
319 static void
vgatext_resume(struct gfxp_fb_softc * softc)320 vgatext_resume(struct gfxp_fb_softc *softc)
321 {
322 
323 	switch (softc->mode) {
324 	case KD_TEXT:
325 		vgatext_kdsettext(softc);
326 		break;
327 
328 	case KD_GRAPHICS:
329 
330 		/*
331 		 * Upon RESUME, the graphics device will always actually
332 		 * be in TEXT mode even though the Xorg server did not
333 		 * make that mode change itself (the suspend code did).
334 		 * We want first, therefore, to restore textmode
335 		 * operation fully, and then the Xorg server will
336 		 * do the rest to restore the device to its
337 		 * (hi resolution) graphics mode
338 		 */
339 		vgatext_kdsettext(softc);
340 #if	defined(USE_BORDERS)
341 		vgatext_init_graphics(softc);
342 #endif
343 		break;
344 	default:
345 		cmn_err(CE_WARN, MYNAME ": unknown mode in vgatext_resume.");
346 		break;
347 	}
348 }
349 
350 static void
vgatext_progressbar_stop(struct gfxp_fb_softc * softc)351 vgatext_progressbar_stop(struct gfxp_fb_softc *softc)
352 {
353 	extern void progressbar_stop(void);
354 
355 	if (softc->silent == 1) {
356 		softc->silent = 0;
357 		progressbar_stop();
358 	}
359 }
360 
361 static void
vgatext_kdsettext(struct gfxp_fb_softc * softc)362 vgatext_kdsettext(struct gfxp_fb_softc *softc)
363 {
364 	union gfx_console *console = softc->console;
365 	int i;
366 
367 	vgatext_init(softc);
368 	for (i = 0; i < sizeof (console->vga.shadow); i++) {
369 		console->vga.text_base[i] = console->vga.shadow[i];
370 	}
371 	console->vga.current_base = console->vga.text_base;
372 	if (console->vga.cursor.visible) {
373 		vgatext_set_cursor(softc,
374 		    console->vga.cursor.row,
375 		    console->vga.cursor.col);
376 	}
377 	vgatext_restore_colormap(softc);
378 }
379 
380 static void
vgatext_kdsetgraphics(struct gfxp_fb_softc * softc)381 vgatext_kdsetgraphics(struct gfxp_fb_softc *softc)
382 {
383 	vgatext_progressbar_stop(softc);
384 	vgatext_save_text(softc);
385 	softc->console->vga.current_base = softc->console->vga.shadow;
386 	vgatext_get_text(softc);
387 #if	defined(USE_BORDERS)
388 	vgatext_init_graphics(softc);
389 #endif
390 }
391 
392 static int
vgatext_kdsetmode(struct gfxp_fb_softc * softc,int mode)393 vgatext_kdsetmode(struct gfxp_fb_softc *softc, int mode)
394 {
395 	switch (mode) {
396 	case KD_TEXT:
397 		if (softc->blt_ops.setmode != NULL)
398 			if (softc->blt_ops.setmode(KD_TEXT) != 0)
399 				return (EIO);
400 
401 		vgatext_kdsettext(softc);
402 		break;
403 
404 	case KD_GRAPHICS:
405 		vgatext_kdsetgraphics(softc);
406 		if (softc->blt_ops.setmode != NULL)
407 			if (softc->blt_ops.setmode(KD_GRAPHICS) != 0) {
408 				vgatext_kdsettext(softc);
409 				return (EIO);
410 			}
411 		break;
412 
413 	case KD_RESETTEXT:
414 		/*
415 		 * In order to avoid racing with a starting X server,
416 		 * this needs to be a test and set that is performed in
417 		 * a single (softc->lock protected) ioctl into this driver.
418 		 */
419 		if (softc->mode == KD_TEXT && softc->silent == 1) {
420 			vgatext_progressbar_stop(softc);
421 			vgatext_kdsettext(softc);
422 		}
423 		mode = KD_TEXT;
424 		break;
425 
426 	default:
427 		return (EINVAL);
428 	}
429 
430 	softc->mode = mode;
431 	return (0);
432 }
433 
434 /*ARGSUSED*/
435 static int
vgatext_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t model,void * ptr)436 vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
437     size_t *maplen, uint_t model, void *ptr)
438 {
439 	struct gfxp_fb_softc *softc = (struct gfxp_fb_softc *)ptr;
440 	union gfx_console *console;
441 	int err;
442 	size_t length;
443 
444 
445 	if (softc == NULL) {
446 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
447 		return (-1);
448 	}
449 	console = softc->console;
450 
451 	if (!(off >= VGA_MEM_ADDR &&
452 	    off < VGA_MEM_ADDR + console->vga.fb_size)) {
453 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
454 		return (-1);
455 	}
456 
457 	if (off + len > VGA_MEM_ADDR + console->vga.fb_size)
458 		length = VGA_MEM_ADDR + console->vga.fb_size - off;
459 	else
460 		length = len;
461 
462 	if ((err = devmap_devmem_setup(dhp, softc->devi,
463 	    NULL, console->vga.fb_regno, off - VGA_MEM_ADDR,
464 	    length, PROT_ALL, 0, &dev_attr)) < 0) {
465 		return (err);
466 	}
467 
468 
469 	*maplen = length;
470 	return (0);
471 }
472 
473 
474 static int
vgatext_devinit(struct gfxp_fb_softc * softc,struct vis_devinit * data)475 vgatext_devinit(struct gfxp_fb_softc *softc, struct vis_devinit *data)
476 {
477 	/* initialize console instance */
478 	data->version = VIS_CONS_REV;
479 	data->width = VGA_TEXT_COLS;
480 	data->height = VGA_TEXT_ROWS;
481 	data->linebytes = VGA_TEXT_COLS;
482 	data->color_map = NULL;
483 	data->depth = 4;
484 	data->mode = VIS_TEXT;
485 	data->polledio = &softc->polledio;
486 
487 	vgatext_save_text(softc);	/* save current console */
488 	vgatext_hide_cursor(softc);
489 	return (0);
490 }
491 
492 /*
493  * Binary searchable table for Unicode to CP437 conversion.
494  */
495 struct unicp437 {
496 	uint16_t	unicode_base;
497 	uint8_t		cp437_base;
498 	uint8_t		length;
499 };
500 
501 static const struct unicp437 cp437table[] = {
502 	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 }, { 0x00a1, 0xad, 0x00 },
503 	{ 0x00a2, 0x9b, 0x00 }, { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
504 	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 }, { 0x00ab, 0xae, 0x00 },
505 	{ 0x00ac, 0xaa, 0x00 }, { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
506 	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 }, { 0x00b6, 0x14, 0x00 },
507 	{ 0x00b7, 0xfa, 0x00 }, { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
508 	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 }, { 0x00bf, 0xa8, 0x00 },
509 	{ 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
510 	{ 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 }, { 0x00d6, 0x99, 0x00 },
511 	{ 0x00dc, 0x9a, 0x00 }, { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
512 	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 }, { 0x00e4, 0x84, 0x00 },
513 	{ 0x00e5, 0x86, 0x00 }, { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
514 	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 }, { 0x00ea, 0x88, 0x01 },
515 	{ 0x00ec, 0x8d, 0x00 }, { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
516 	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 }, { 0x00f1, 0xa4, 0x00 },
517 	{ 0x00f2, 0x95, 0x00 }, { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
518 	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 }, { 0x00f8, 0xed, 0x00 },
519 	{ 0x00f9, 0x97, 0x00 }, { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
520 	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 }, { 0x0192, 0x9f, 0x00 },
521 	{ 0x0393, 0xe2, 0x00 }, { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
522 	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 }, { 0x03b1, 0xe0, 0x01 },
523 	{ 0x03b4, 0xeb, 0x00 }, { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
524 	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 }, { 0x03c4, 0xe7, 0x00 },
525 	{ 0x03c6, 0xed, 0x00 }, { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
526 	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 }, { 0x2019, 0x27, 0x00 },
527 	{ 0x201c, 0x22, 0x00 }, { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
528 	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 }, { 0x20a7, 0x9e, 0x00 },
529 	{ 0x20ac, 0xee, 0x00 }, { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
530 	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 }, { 0x2193, 0x19, 0x00 },
531 	{ 0x2194, 0x1d, 0x00 }, { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
532 	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 }, { 0x2211, 0xe4, 0x00 },
533 	{ 0x2212, 0x2d, 0x00 }, { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
534 	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 }, { 0x2229, 0xef, 0x00 },
535 	{ 0x2248, 0xf7, 0x00 }, { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
536 	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 }, { 0x2310, 0xa9, 0x00 },
537 	{ 0x2320, 0xf4, 0x00 }, { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
538 	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 }, { 0x2510, 0xbf, 0x00 },
539 	{ 0x2514, 0xc0, 0x00 }, { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
540 	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 }, { 0x2534, 0xc1, 0x00 },
541 	{ 0x253c, 0xc5, 0x00 }, { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
542 	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 }, { 0x2554, 0xc9, 0x00 },
543 	{ 0x2555, 0xb8, 0x00 }, { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
544 	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 }, { 0x255a, 0xc8, 0x00 },
545 	{ 0x255b, 0xbe, 0x00 }, { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
546 	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 }, { 0x2561, 0xb5, 0x00 },
547 	{ 0x2562, 0xb6, 0x00 }, { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
548 	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 }, { 0x2568, 0xd0, 0x00 },
549 	{ 0x2569, 0xca, 0x00 }, { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
550 	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 }, { 0x2584, 0xdc, 0x00 },
551 	{ 0x2588, 0xdb, 0x00 }, { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
552 	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 }, { 0x25ac, 0x16, 0x00 },
553 	{ 0x25b2, 0x1e, 0x00 }, { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
554 	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 }, { 0x25d8, 0x08, 0x00 },
555 	{ 0x25d9, 0x0a, 0x00 }, { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
556 	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 }, { 0x2660, 0x06, 0x00 },
557 	{ 0x2663, 0x05, 0x00 }, { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
558 	{ 0x266c, 0x0e, 0x00 }
559 };
560 
561 static uint8_t
vga_get_cp437(uint32_t c)562 vga_get_cp437(uint32_t c)
563 {
564 	int min, mid, max;
565 
566 	min = 0;
567 	max = (sizeof (cp437table) / sizeof (struct unicp437)) - 1;
568 
569 	if (c < cp437table[0].unicode_base ||
570 	    c > cp437table[max].unicode_base + cp437table[max].length)
571 		return ('?');
572 
573 	while (max >= min) {
574 		mid = (min + max) / 2;
575 		if (c < cp437table[mid].unicode_base)
576 			max = mid - 1;
577 		else if (c > cp437table[mid].unicode_base +
578 		    cp437table[mid].length)
579 			min = mid + 1;
580 		else
581 			return (c - cp437table[mid].unicode_base +
582 			    cp437table[mid].cp437_base);
583 	}
584 
585 	return ('?');
586 }
587 
588 /*
589  * display a string on the screen at (row, col)
590  *	 assume it has been cropped to fit.
591  */
592 static void
vgatext_cons_display(struct gfxp_fb_softc * softc,struct vis_consdisplay * da)593 vgatext_cons_display(struct gfxp_fb_softc *softc, struct vis_consdisplay *da)
594 {
595 	uint32_t *string;
596 	int	i;
597 	unsigned char	attr;
598 	struct cgatext {
599 		unsigned char ch;
600 		unsigned char attr;
601 	};
602 	struct cgatext *addr;
603 
604 	/*
605 	 * Sanity checks.  This is a last-ditch effort to avoid damage
606 	 * from brokenness or maliciousness above.
607 	 */
608 	if (da->row < 0 || da->row >= VGA_TEXT_ROWS ||
609 	    da->col < 0 || da->col >= VGA_TEXT_COLS ||
610 	    da->col + da->width > VGA_TEXT_COLS)
611 		return;
612 
613 	/*
614 	 * To be fully general, we should copyin the data.  This is not
615 	 * really relevant for this text-only driver, but a graphical driver
616 	 * should support these ioctls from userland to enable simple
617 	 * system startup graphics.
618 	 */
619 	attr = (solaris_color_to_pc_color[da->bg_color.four & 0xf] << 4)
620 	    | solaris_color_to_pc_color[da->fg_color.four & 0xf];
621 	string = (uint32_t *)da->data;
622 	addr = (struct cgatext *)softc->console->vga.current_base
623 	    +  (da->row * VGA_TEXT_COLS + da->col);
624 	for (i = 0; i < da->width; i++) {
625 		addr[i].ch = vga_get_cp437(string[i]);
626 		addr[i].attr = attr;
627 	}
628 }
629 
630 static void
vgatext_polled_display(struct vis_polledio_arg * arg,struct vis_consdisplay * da)631 vgatext_polled_display(
632 	struct vis_polledio_arg *arg,
633 	struct vis_consdisplay *da)
634 {
635 	vgatext_cons_display((struct gfxp_fb_softc *)arg, da);
636 }
637 
638 /*
639  * screen-to-screen copy
640  */
641 
642 static void
vgatext_cons_copy(struct gfxp_fb_softc * softc,struct vis_conscopy * ma)643 vgatext_cons_copy(struct gfxp_fb_softc *softc, struct vis_conscopy *ma)
644 {
645 	unsigned short	*from;
646 	unsigned short	*to;
647 	int		cnt;
648 	screen_size_t chars_per_row;
649 	unsigned short	*to_row_start;
650 	unsigned short	*from_row_start;
651 	screen_size_t	rows_to_move;
652 	unsigned short	*base;
653 
654 	/*
655 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
656 	 * damage caused by broken-ness or maliciousness above.
657 	 */
658 	if (ma->s_col < 0 || ma->s_col >= VGA_TEXT_COLS ||
659 	    ma->s_row < 0 || ma->s_row >= VGA_TEXT_ROWS ||
660 	    ma->e_col < 0 || ma->e_col >= VGA_TEXT_COLS ||
661 	    ma->e_row < 0 || ma->e_row >= VGA_TEXT_ROWS ||
662 	    ma->t_col < 0 || ma->t_col >= VGA_TEXT_COLS ||
663 	    ma->t_row < 0 || ma->t_row >= VGA_TEXT_ROWS ||
664 	    ma->s_col > ma->e_col ||
665 	    ma->s_row > ma->e_row)
666 		return;
667 
668 	/*
669 	 * Remember we're going to copy shorts because each
670 	 * character/attribute pair is 16 bits.
671 	 */
672 	chars_per_row = ma->e_col - ma->s_col + 1;
673 	rows_to_move = ma->e_row - ma->s_row + 1;
674 
675 	/* More sanity checks. */
676 	if (ma->t_row + rows_to_move > VGA_TEXT_ROWS ||
677 	    ma->t_col + chars_per_row > VGA_TEXT_COLS)
678 		return;
679 
680 	base = (unsigned short *)softc->console->vga.current_base;
681 
682 	to_row_start = base + ((ma->t_row * VGA_TEXT_COLS) + ma->t_col);
683 	from_row_start = base + ((ma->s_row * VGA_TEXT_COLS) + ma->s_col);
684 
685 	if (to_row_start < from_row_start) {
686 		while (rows_to_move-- > 0) {
687 			to = to_row_start;
688 			from = from_row_start;
689 			to_row_start += VGA_TEXT_COLS;
690 			from_row_start += VGA_TEXT_COLS;
691 			for (cnt = chars_per_row; cnt-- > 0; )
692 				*to++ = *from++;
693 		}
694 	} else {
695 		/*
696 		 * Offset to the end of the region and copy backwards.
697 		 */
698 		cnt = rows_to_move * VGA_TEXT_COLS + chars_per_row;
699 		to_row_start += cnt;
700 		from_row_start += cnt;
701 
702 		while (rows_to_move-- > 0) {
703 			to_row_start -= VGA_TEXT_COLS;
704 			from_row_start -= VGA_TEXT_COLS;
705 			to = to_row_start;
706 			from = from_row_start;
707 			for (cnt = chars_per_row; cnt-- > 0; )
708 				*--to = *--from;
709 		}
710 	}
711 }
712 
713 static void
vgatext_polled_copy(struct vis_polledio_arg * arg,struct vis_conscopy * ca)714 vgatext_polled_copy(
715 	struct vis_polledio_arg *arg,
716 	struct vis_conscopy *ca)
717 {
718 	vgatext_cons_copy((struct gfxp_fb_softc *)arg, ca);
719 }
720 
721 /*ARGSUSED*/
722 static int
vgatext_cons_clear(struct gfxp_fb_softc * softc,struct vis_consclear * ca)723 vgatext_cons_clear(struct gfxp_fb_softc *softc, struct vis_consclear *ca)
724 {
725 	uint16_t val, fg, *base;
726 	int i;
727 
728 	if (ca->bg_color.four == 0)	/* bright white */
729 		fg = 1;			/* black */
730 	else
731 		fg = 8;
732 
733 	val = (solaris_color_to_pc_color[ca->bg_color.four & 0xf] << 4) |
734 	    solaris_color_to_pc_color[fg];
735 	val = (val << 8) | ' ';
736 
737 	base = (uint16_t *)softc->console->vga.current_base;
738 	for (i = 0; i < VGA_TEXT_ROWS * VGA_TEXT_COLS; i++)
739 		base[i] = val;
740 
741 	return (0);
742 }
743 
744 static void
vgatext_cons_cursor(struct gfxp_fb_softc * softc,struct vis_conscursor * ca)745 vgatext_cons_cursor(struct gfxp_fb_softc *softc, struct vis_conscursor *ca)
746 {
747 	if (softc->silent)
748 		return;
749 
750 	switch (ca->action) {
751 	case VIS_HIDE_CURSOR:
752 		softc->console->vga.cursor.visible = B_FALSE;
753 		if (softc->console->vga.current_base ==
754 		    softc->console->vga.text_base)
755 			vgatext_hide_cursor(softc);
756 		break;
757 	case VIS_DISPLAY_CURSOR:
758 		/*
759 		 * Sanity check.  This is a last-ditch effort to avoid
760 		 * damage from brokenness or maliciousness above.
761 		 */
762 		if (ca->col < 0 || ca->col >= VGA_TEXT_COLS ||
763 		    ca->row < 0 || ca->row >= VGA_TEXT_ROWS)
764 			return;
765 
766 		softc->console->vga.cursor.visible = B_TRUE;
767 		softc->console->vga.cursor.col = ca->col;
768 		softc->console->vga.cursor.row = ca->row;
769 		if (softc->console->vga.current_base ==
770 		    softc->console->vga.text_base)
771 			vgatext_set_cursor(softc, ca->row, ca->col);
772 		break;
773 	case VIS_GET_CURSOR:
774 		if (softc->console->vga.current_base ==
775 		    softc->console->vga.text_base) {
776 			vgatext_get_cursor(softc, &ca->row, &ca->col);
777 		}
778 		break;
779 	}
780 }
781 
782 static void
vgatext_polled_cursor(struct vis_polledio_arg * arg,struct vis_conscursor * ca)783 vgatext_polled_cursor(
784 	struct vis_polledio_arg *arg,
785 	struct vis_conscursor *ca)
786 {
787 	vgatext_cons_cursor((struct gfxp_fb_softc *)arg, ca);
788 }
789 
790 static void
vgatext_hide_cursor(struct gfxp_fb_softc * softc)791 vgatext_hide_cursor(struct gfxp_fb_softc *softc)
792 {
793 	union gfx_console *console = softc->console;
794 	uint8_t msl, s;
795 
796 	if (softc->silent)
797 		return;
798 
799 	msl = vga_get_crtc(&console->vga.regs, VGA_CRTC_MAX_S_LN) & 0x1f;
800 	s = vga_get_crtc(&console->vga.regs, VGA_CRTC_CSSL) & 0xc0;
801 	s |= (1 << 5);
802 
803 	/* disable cursor */
804 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CSSL, s);
805 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CESL, msl);
806 }
807 
808 static void
vgatext_set_cursor(struct gfxp_fb_softc * softc,int row,int col)809 vgatext_set_cursor(struct gfxp_fb_softc *softc, int row, int col)
810 {
811 	union gfx_console *console = softc->console;
812 	short	addr;
813 	uint8_t msl, s;
814 
815 	if (softc->silent)
816 		return;
817 
818 	msl = vga_get_crtc(&console->vga.regs, VGA_CRTC_MAX_S_LN) & 0x1f;
819 	s = vga_get_crtc(&console->vga.regs, VGA_CRTC_CSSL) & 0xc0;
820 
821 	addr = row * VGA_TEXT_COLS + col;
822 
823 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CLAH, addr >> 8);
824 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CLAL, addr & 0xff);
825 
826 	/* enable cursor */
827 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CSSL, s);
828 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CESL, msl);
829 }
830 
831 static void
vgatext_get_cursor(struct gfxp_fb_softc * softc,screen_pos_t * row,screen_pos_t * col)832 vgatext_get_cursor(struct gfxp_fb_softc *softc,
833     screen_pos_t *row, screen_pos_t *col)
834 {
835 	union gfx_console *console = softc->console;
836 	short   addr;
837 
838 	addr = (vga_get_crtc(&console->vga.regs, VGA_CRTC_CLAH) << 8) +
839 	    vga_get_crtc(&console->vga.regs, VGA_CRTC_CLAL);
840 
841 	*row = addr / VGA_TEXT_COLS;
842 	*col = addr % VGA_TEXT_COLS;
843 }
844 
845 static void
vgatext_get_text(struct gfxp_fb_softc * softc)846 vgatext_get_text(struct gfxp_fb_softc *softc)
847 {
848 	union gfx_console *console = softc->console;
849 	struct vgareg *vga_reg;
850 	struct vgaregmap *regs;
851 	int i;
852 
853 	regs = &console->vga.regs;
854 	vga_reg = &console->vga.vga_reg;
855 
856 	vga_reg->vga_misc = vga_get_reg(regs, VGA_MISC_R);
857 
858 	/* get crt controller registers */
859 	for (i = 0; i < NUM_CRTC_REG; i++) {
860 		vga_reg->vga_crtc[i] = vga_get_crtc(regs, i);
861 	}
862 
863 	/* get attribute registers */
864 	for (i = 0; i < NUM_ATR_REG; i++) {
865 		vga_reg->vga_atr[i] = vga_get_atr(regs, i);
866 	}
867 
868 	/* get graphics controller registers */
869 	for (i = 0; i < NUM_GRC_REG; i++) {
870 		vga_reg->vga_grc[i] = vga_get_grc(regs, i);
871 	}
872 
873 	/* get sequencer registers */
874 	for (i = 1; i < NUM_SEQ_REG; i++) {
875 		vga_reg->vga_seq[i] = vga_get_seq(regs, i);
876 	}
877 }
878 
879 /*
880  * This code is experimental. It's only enabled if console is
881  * set to graphics, a preliminary implementation of happyface boot.
882  */
883 static void
vgatext_set_text(struct gfxp_fb_softc * softc)884 vgatext_set_text(struct gfxp_fb_softc *softc)
885 {
886 	union gfx_console *console = softc->console;
887 	struct vgareg *vga_reg;
888 	struct vgaregmap *regs;
889 	int i;
890 
891 	regs = &console->vga.regs;
892 	vga_reg = &console->vga.vga_reg;
893 
894 	vgatext_get_text(softc);
895 
896 	/*
897 	 * Set output register bits for text mode.
898 	 * Make sure the VGA adapter is not in monochrome emulation mode.
899 	 */
900 	vga_set_reg(regs, VGA_MISC_W, VGA_MISC_HSP | VGA_MISC_PGSL |
901 	    VGA_MISC_VCLK1 | VGA_MISC_ENB_RAM | VGA_MISC_IOA_SEL);
902 
903 	/* set sequencer registers */
904 	vga_set_seq(regs, VGA_SEQ_RST_SYN,
905 	    (vga_get_seq(regs, VGA_SEQ_RST_SYN) &
906 	    ~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
907 	for (i = 1; i < NUM_SEQ_REG; i++) {
908 		vga_set_seq(regs, i, VGA_SEQ_TEXT[i]);
909 	}
910 	vga_set_seq(regs, VGA_SEQ_RST_SYN,
911 	    (vga_get_seq(regs, VGA_SEQ_RST_SYN) |
912 	    VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
913 	    VGA_SEQ_RST_SYN_NO_SYNC_RESET));
914 
915 	/* set crt controller registers */
916 	vga_set_crtc(regs, VGA_CRTC_VRE,
917 	    (vga_reg->vga_crtc[VGA_CRTC_VRE] & ~VGA_CRTC_VRE_LOCK));
918 	for (i = 0; i < NUM_CRTC_REG; i++) {
919 		vga_set_crtc(regs, i, VGA_CRTC_TEXT[i]);
920 	}
921 
922 	/* set graphics controller registers */
923 	for (i = 0; i < NUM_GRC_REG; i++) {
924 		vga_set_grc(regs, i, VGA_GRC_TEXT[i]);
925 	}
926 
927 	/* set attribute registers */
928 	for (i = 0; i < NUM_ATR_REG; i++) {
929 		vga_set_atr(regs, i, VGA_ATR_TEXT[i]);
930 	}
931 
932 	/* set palette */
933 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
934 		vga_put_cmap(regs, i,
935 		    VGA_TEXT_PALETTES[i][0] << 2,
936 		    VGA_TEXT_PALETTES[i][1] << 2,
937 		    VGA_TEXT_PALETTES[i][2] << 2);
938 	}
939 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
940 		vga_put_cmap(regs, i, 0, 0, 0);
941 	}
942 }
943 
944 static void
vgatext_init(struct gfxp_fb_softc * softc)945 vgatext_init(struct gfxp_fb_softc *softc)
946 {
947 	union gfx_console *console = softc->console;
948 	bitmap_data_t *font_data;
949 	unsigned char atr_mode;
950 	short width, height;
951 	int i;
952 
953 	atr_mode = vga_get_atr(&console->vga.regs, VGA_ATR_MODE);
954 	if (atr_mode & VGA_ATR_MODE_GRAPH)
955 		vgatext_set_text(softc);
956 	atr_mode = vga_get_atr(&console->vga.regs, VGA_ATR_MODE);
957 	atr_mode &= ~VGA_ATR_MODE_BLINK;
958 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
959 	vga_set_atr(&console->vga.regs, VGA_ATR_MODE, atr_mode);
960 #if	defined(USE_BORDERS)
961 	vga_set_atr(&console->vga.regs, VGA_ATR_BDR_CLR,
962 	    vga_get_atr(&console->vga.regs, pc_brt_white));
963 #else
964 	vga_set_atr(&console->vga.regs, VGA_ATR_BDR_CLR,
965 	    vga_get_atr(&console->vga.regs, pc_black));
966 #endif
967 
968 	width = VGA_TEXT_COLS;
969 	height = VGA_TEXT_ROWS;
970 	font_data = set_font(&height, &width,
971 	    16 * height + BORDER_PIXELS,
972 	    8 * width + BORDER_PIXELS);
973 	for (i = 0; i < VFNT_MAPS; i++) {
974 		console->vga.font.vf_map[i] = font_data->font->vf_map[i];
975 		console->vga.font.vf_map_count[i] =
976 		    font_data->font->vf_map_count[i];
977 	}
978 	console->vga.font.vf_bytes = font_data->font->vf_bytes;
979 	console->vga.font.vf_width = font_data->font->vf_width;
980 	console->vga.font.vf_height = font_data->font->vf_height;
981 	vgatext_setfont(softc);
982 }
983 
984 #if	defined(USE_BORDERS)
985 static void
vgatext_init_graphics(struct gfxp_fb_softc * softc)986 vgatext_init_graphics(struct gfxp_fb_softc *softc)
987 {
988 	vga_set_atr(&softc->console->vga.regs, VGA_ATR_BDR_CLR,
989 	    vga_get_atr(&softc->console->vga.regs, pc_black));
990 }
991 #endif
992 
993 /*
994  * Binary searchable table for CP437 to Unicode conversion.
995  */
996 struct cp437uni {
997 	uint8_t		cp437_base;
998 	uint16_t	unicode_base;
999 	uint8_t		length;
1000 };
1001 
1002 static const struct cp437uni cp437unitable[] = {
1003 	{   0, 0x0000, 0 }, {   1, 0x263A, 1 }, {   3, 0x2665, 1 },
1004 	{   5, 0x2663, 0 }, {   6, 0x2660, 0 }, {   7, 0x2022, 0 },
1005 	{   8, 0x25D8, 0 }, {   9, 0x25CB, 0 }, {  10, 0x25D9, 0 },
1006 	{  11, 0x2642, 0 }, {  12, 0x2640, 0 }, {  13, 0x266A, 1 },
1007 	{  15, 0x263C, 0 }, {  16, 0x25BA, 0 }, {  17, 0x25C4, 0 },
1008 	{  18, 0x2195, 0 }, {  19, 0x203C, 0 }, {  20, 0x00B6, 0 },
1009 	{  21, 0x00A7, 0 }, {  22, 0x25AC, 0 }, {  23, 0x21A8, 0 },
1010 	{  24, 0x2191, 0 }, {  25, 0x2193, 0 }, {  26, 0x2192, 0 },
1011 	{  27, 0x2190, 0 }, {  28, 0x221F, 0 }, {  29, 0x2194, 0 },
1012 	{  30, 0x25B2, 0 }, {  31, 0x25BC, 0 }, {  32, 0x0020, 0x5e },
1013 	{ 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 },
1014 	{ 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 },
1015 	{ 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 },
1016 	{ 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 },
1017 	{ 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 },
1018 	{ 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 },
1019 	{ 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 },
1020 	{ 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 },
1021 	{ 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 },
1022 	{ 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 },
1023 	{ 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 },
1024 	{ 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 },
1025 	{ 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 },
1026 	{ 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 },
1027 	{ 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 },
1028 	{ 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 },
1029 	{ 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 },
1030 	{ 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 },
1031 	{ 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 },
1032 	{ 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 },
1033 	{ 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 },
1034 	{ 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 },
1035 	{ 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 },
1036 	{ 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 },
1037 	{ 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 },
1038 	{ 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 },
1039 	{ 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 },
1040 	{ 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 },
1041 	{ 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 },
1042 	{ 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 },
1043 	{ 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 },
1044 	{ 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 },
1045 	{ 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 },
1046 	{ 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 },
1047 	{ 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 },
1048 	{ 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 },
1049 	{ 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 },
1050 	{ 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 },
1051 	{ 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 },
1052 	{ 255, 0x00A0, 0 }
1053 };
1054 
1055 static uint16_t
vga_cp437_to_uni(uint8_t c)1056 vga_cp437_to_uni(uint8_t c)
1057 {
1058 	int min, mid, max;
1059 
1060 	min = 0;
1061 	max = (sizeof (cp437unitable) / sizeof (struct cp437uni)) - 1;
1062 
1063 	while (max >= min) {
1064 		mid = (min + max) / 2;
1065 		if (c < cp437unitable[mid].cp437_base)
1066 			max = mid - 1;
1067 		else if (c > cp437unitable[mid].cp437_base +
1068 		    cp437unitable[mid].length)
1069 			min = mid + 1;
1070 		else
1071 			return (c - cp437unitable[mid].cp437_base +
1072 			    cp437unitable[mid].unicode_base);
1073 	}
1074 
1075 	return ('?');
1076 }
1077 
1078 static void
vgatext_setfont(struct gfxp_fb_softc * softc)1079 vgatext_setfont(struct gfxp_fb_softc *softc)
1080 {
1081 	union gfx_console *console = softc->console;
1082 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
1083 
1084 	const uchar_t *from;
1085 	uchar_t volatile *to;
1086 	uint16_t c;
1087 	int	i, j, s;
1088 	int	bpc, f_offset;
1089 
1090 	/* Sync-reset the sequencer registers */
1091 	vga_set_seq(&console->vga.regs, 0x00, 0x01);
1092 	/*
1093 	 *  enable write to plane2, since fonts
1094 	 * could only be loaded into plane2
1095 	 */
1096 	vga_set_seq(&console->vga.regs, 0x02, 0x04);
1097 	/*
1098 	 *  sequentially access data in the bit map being
1099 	 * selected by MapMask register (index 0x02)
1100 	 */
1101 	vga_set_seq(&console->vga.regs, 0x04, 0x07);
1102 	/* Sync-reset ended, and allow the sequencer to operate */
1103 	vga_set_seq(&console->vga.regs, 0x00, 0x03);
1104 
1105 	/*
1106 	 *  select plane 2 on Read Mode 0
1107 	 */
1108 	vga_set_grc(&console->vga.regs, 0x04, 0x02);
1109 	/*
1110 	 *  system addresses sequentially access data, follow
1111 	 * Memory Mode register bit 2 in the sequencer
1112 	 */
1113 	vga_set_grc(&console->vga.regs, 0x05, 0x00);
1114 	/*
1115 	 * set range of host memory addresses decoded by VGA
1116 	 * hardware -- A0000h-BFFFFh (128K region)
1117 	 */
1118 	vga_set_grc(&console->vga.regs, 0x06, 0x00);
1119 
1120 	/*
1121 	 * This assumes 8x16 characters, which yield the traditional 80x25
1122 	 * screen.  It really should support other character heights.
1123 	 */
1124 	bpc = 16;
1125 	s = console->vga.vga_fontslot;
1126 	f_offset = s * 8 * 1024;
1127 	for (i = 0; i < 256; i++) {
1128 		c = vga_cp437_to_uni(i);
1129 		from = font_lookup(&console->vga.font, c);
1130 		to = (unsigned char *)console->vga.fb.addr + f_offset +
1131 		    i * 0x20;
1132 		for (j = 0; j < bpc; j++)
1133 			*to++ = *from++;
1134 	}
1135 
1136 	/* Sync-reset the sequencer registers */
1137 	vga_set_seq(&console->vga.regs, 0x00, 0x01);
1138 	/* enable write to plane 0 and 1 */
1139 	vga_set_seq(&console->vga.regs, 0x02, 0x03);
1140 	/*
1141 	 * enable character map selection
1142 	 * and odd/even addressing
1143 	 */
1144 	vga_set_seq(&console->vga.regs, 0x04, 0x03);
1145 	/*
1146 	 * select font map
1147 	 */
1148 	vga_set_seq(&console->vga.regs, 0x03, fsreg[s]);
1149 	/* Sync-reset ended, and allow the sequencer to operate */
1150 	vga_set_seq(&console->vga.regs, 0x00, 0x03);
1151 
1152 	/* restore graphic registers */
1153 
1154 	/* select plane 0 */
1155 	vga_set_grc(&console->vga.regs, 0x04, 0x00);
1156 	/* enable odd/even addressing mode */
1157 	vga_set_grc(&console->vga.regs, 0x05, 0x10);
1158 	/*
1159 	 * range of host memory addresses decoded by VGA
1160 	 * hardware -- B8000h-BFFFFh (32K region)
1161 	 */
1162 	vga_set_grc(&console->vga.regs, 0x06, 0x0e);
1163 	/* enable all color plane */
1164 	vga_set_atr(&console->vga.regs, 0x12, 0x0f);
1165 
1166 }
1167 
1168 static void
vgatext_save_colormap(struct gfxp_fb_softc * softc)1169 vgatext_save_colormap(struct gfxp_fb_softc *softc)
1170 {
1171 	union gfx_console *console = softc->console;
1172 	int i;
1173 
1174 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1175 		console->vga.attrib_palette[i] =
1176 		    vga_get_atr(&console->vga.regs, i);
1177 	}
1178 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1179 		vga_get_cmap(&console->vga.regs, i,
1180 		    &console->vga.colormap[i].red,
1181 		    &console->vga.colormap[i].green,
1182 		    &console->vga.colormap[i].blue);
1183 	}
1184 }
1185 
1186 static void
vgatext_restore_colormap(struct gfxp_fb_softc * softc)1187 vgatext_restore_colormap(struct gfxp_fb_softc *softc)
1188 {
1189 	union gfx_console *console = softc->console;
1190 	int i;
1191 
1192 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1193 		vga_set_atr(&console->vga.regs, i,
1194 		    console->vga.attrib_palette[i]);
1195 	}
1196 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1197 		vga_put_cmap(&console->vga.regs, i,
1198 		    console->vga.colormap[i].red,
1199 		    console->vga.colormap[i].green,
1200 		    console->vga.colormap[i].blue);
1201 	}
1202 }
1203 
1204 /*
1205  * search the entries of the "reg" property for one which has the desired
1206  * combination of phys_hi bits and contains the desired address.
1207  *
1208  * This version searches a PCI-style "reg" property.  It was prompted by
1209  * issues surrounding the presence or absence of an entry for the ROM:
1210  * (a) a transition problem with PowerPC Virtual Open Firmware
1211  * (b) uncertainty as to whether an entry will be included on a device
1212  *     with ROM support (and so an "active" ROM base address register),
1213  *     but no ROM actually installed.
1214  *
1215  * See the note below on vgatext_get_isa_reg_index for the reasons for
1216  * returning the offset.
1217  *
1218  * Note that this routine may not be fully general; it is intended for the
1219  * specific purpose of finding a couple of particular VGA reg entries and
1220  * may not be suitable for all reg-searching purposes.
1221  */
1222 static int
vgatext_get_pci_reg_index(dev_info_t * const devi,unsigned long himask,unsigned long hival,unsigned long addr,off_t * offset)1223 vgatext_get_pci_reg_index(
1224 	dev_info_t *const devi,
1225 	unsigned long himask,
1226 	unsigned long hival,
1227 	unsigned long addr,
1228 	off_t *offset)
1229 {
1230 
1231 	int			length, index;
1232 	pci_regspec_t	*reg;
1233 
1234 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1235 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1236 		return (-1);
1237 	}
1238 
1239 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
1240 		if ((reg[index].pci_phys_hi & himask) != hival)
1241 			continue;
1242 		if (reg[index].pci_size_hi != 0)
1243 			continue;
1244 		if (reg[index].pci_phys_mid != 0)
1245 			continue;
1246 		if (reg[index].pci_phys_low > addr)
1247 			continue;
1248 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
1249 			continue;
1250 
1251 		*offset = addr - reg[index].pci_phys_low;
1252 		kmem_free(reg, (size_t)length);
1253 		return (index);
1254 	}
1255 	kmem_free(reg, (size_t)length);
1256 
1257 	return (-1);
1258 }
1259 
1260 /*
1261  * search the entries of the "reg" property for one which has the desired
1262  * combination of phys_hi bits and contains the desired address.
1263  *
1264  * This version searches a ISA-style "reg" property.  It was prompted by
1265  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
1266  * 8514/A registers should have been added after all standard VGA registers.
1267  * Unfortunately, the Solaris/Intel device configuration framework
1268  * (a) lists the 8514/A registers before the video memory, and then
1269  * (b) also sorts the entries so that I/O entries come before memory
1270  *     entries.
1271  *
1272  * It returns the "reg" index and offset into that register set.
1273  * The offset is needed because there exist (broken?) BIOSes that
1274  * report larger ranges enclosing the standard ranges.  One reports
1275  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
1276  * offset adjusts for this difference in the base of the register set.
1277  *
1278  * Note that this routine may not be fully general; it is intended for the
1279  * specific purpose of finding a couple of particular VGA reg entries and
1280  * may not be suitable for all reg-searching purposes.
1281  */
1282 static int
vgatext_get_isa_reg_index(dev_info_t * const devi,unsigned long hival,unsigned long addr,off_t * offset)1283 vgatext_get_isa_reg_index(
1284 	dev_info_t *const devi,
1285 	unsigned long hival,
1286 	unsigned long addr,
1287 	off_t *offset)
1288 {
1289 
1290 	int		length, index;
1291 	struct regspec	*reg;
1292 
1293 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1294 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1295 		return (-1);
1296 	}
1297 
1298 	for (index = 0; index < length / sizeof (struct regspec); index++) {
1299 		if (reg[index].regspec_bustype != hival)
1300 			continue;
1301 		if (reg[index].regspec_addr > addr)
1302 			continue;
1303 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
1304 			continue;
1305 
1306 		*offset = addr - reg[index].regspec_addr;
1307 		kmem_free(reg, (size_t)length);
1308 		return (index);
1309 	}
1310 	kmem_free(reg, (size_t)length);
1311 
1312 	return (-1);
1313 }
1314 
1315 /*
1316  * This vgatext function is used to return the fb, and reg pointers
1317  * and handles for peer graphics drivers.
1318  */
1319 
1320 void
vgatext_return_pointers(struct gfxp_fb_softc * softc,struct vgaregmap * fbs,struct vgaregmap * regss)1321 vgatext_return_pointers(struct gfxp_fb_softc *softc, struct vgaregmap *fbs,
1322     struct vgaregmap *regss)
1323 {
1324 
1325 	fbs->addr	= softc->console->vga.fb.addr;
1326 	fbs->handle	= softc->console->vga.fb.handle;
1327 	fbs->mapped	= softc->console->vga.fb.mapped;
1328 	regss->addr	= softc->console->vga.regs.addr;
1329 	regss->handle	= softc->console->vga.regs.handle;
1330 	regss->mapped	= softc->console->vga.regs.mapped;
1331 }
1332