xref: /illumos-gate/usr/src/uts/intel/io/vgatext/vgatext.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 2009 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 
39 #include <sys/ddi.h>
40 #include <sys/stat.h>
41 #include <sys/sunddi.h>
42 #include <sys/file.h>
43 #include <sys/open.h>
44 #include <sys/modctl.h>
45 #include <sys/vgareg.h>
46 #include <sys/vgasubr.h>
47 #include <sys/pci.h>
48 #include <sys/kd.h>
49 #include <sys/ddi_impldefs.h>
50 #include <sys/sunldi.h>
51 #include <sys/agpgart.h>
52 #include <sys/agp/agpdefs.h>
53 #include <sys/agp/agpmaster_io.h>
54 
55 #define	MYNAME	"vgatext"
56 
57 /*
58  * Each instance of this driver has 2 minor nodes:
59  * 0: for common graphics operations
60  * 1: for agpmaster operations
61  */
62 #define	GFX_MINOR		0
63 #define	AGPMASTER_MINOR		1
64 
65 #define	MY_NBITSMINOR		1
66 #define	DEV2INST(dev)		(getminor(dev) >> MY_NBITSMINOR)
67 #define	DEV2MINOR(dev)		(getminor(dev) & ((1 << MY_NBITSMINOR) - 1))
68 #define	INST2NODE1(inst)	((inst) << MY_NBITSMINOR + GFX_MINOR)
69 #define	INST2NODE2(inst)	(((inst) << MY_NBITSMINOR) + AGPMASTER_MINOR)
70 
71 /* I don't know exactly where these should be defined, but this is a	*/
72 /* heck of a lot better than constants in the code.			*/
73 #define	TEXT_ROWS		25
74 #define	TEXT_COLS		80
75 
76 #define	VGA_BRIGHT_WHITE	0x0f
77 #define	VGA_BLACK		0x00
78 
79 #define	VGA_REG_ADDR		0x3c0
80 #define	VGA_REG_SIZE		0x20
81 
82 #define	VGA_MEM_ADDR		0xa0000
83 #define	VGA_MEM_SIZE		0x20000
84 
85 #define	VGA_MMAP_FB_BASE	VGA_MEM_ADDR
86 
87 /*
88  * This variable allows for this driver to suspend even if it
89  * shouldn't.  Note that by setting it, the framebuffer will probably
90  * not come back.  So use it with a serial console, or with serial
91  * line debugging (say, for example, if this driver is being modified
92  * to support _some_ hardware doing suspend and resume).
93  */
94 int vgatext_force_suspend = 0;
95 
96 static int vgatext_open(dev_t *, int, int, cred_t *);
97 static int vgatext_close(dev_t, int, int, cred_t *);
98 static int vgatext_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
99 static int vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
100 			    size_t *, uint_t);
101 
102 static 	struct cb_ops cb_vgatext_ops = {
103 	vgatext_open,		/* cb_open */
104 	vgatext_close,		/* cb_close */
105 	nodev,			/* cb_strategy */
106 	nodev,			/* cb_print */
107 	nodev,			/* cb_dump */
108 	nodev,			/* cb_read */
109 	nodev,			/* cb_write */
110 	vgatext_ioctl,		/* cb_ioctl */
111 	vgatext_devmap,		/* cb_devmap */
112 	nodev,			/* cb_mmap */
113 	ddi_devmap_segmap,	/* cb_segmap */
114 	nochpoll,		/* cb_chpoll */
115 	ddi_prop_op,		/* cb_prop_op */
116 	0,			/* cb_stream */
117 	D_NEW | D_MTSAFE	/* cb_flag */
118 };
119 
120 static int vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
121 		void **result);
122 static int vgatext_attach(dev_info_t *, ddi_attach_cmd_t);
123 static int vgatext_detach(dev_info_t *, ddi_detach_cmd_t);
124 
125 static struct vis_identifier text_ident = { "SUNWtext" };
126 
127 static struct dev_ops vgatext_ops = {
128 	DEVO_REV,		/* devo_rev */
129 	0,			/* devo_refcnt */
130 	vgatext_info,		/* devo_getinfo */
131 	nulldev,		/* devo_identify */
132 	nulldev,		/* devo_probe */
133 	vgatext_attach,		/* devo_attach */
134 	vgatext_detach,		/* devo_detach */
135 	nodev,			/* devo_reset */
136 	&cb_vgatext_ops,	/* devo_cb_ops */
137 	(struct bus_ops *)NULL,	/* devo_bus_ops */
138 	NULL,			/* power */
139 	ddi_quiesce_not_needed,	/* quiesce */
140 };
141 
142 struct vgatext_softc {
143 	struct vgaregmap 	regs;
144 	struct vgaregmap 	fb;
145 	off_t			fb_size;
146 	int			fb_regno;
147 	dev_info_t		*devi;
148 	int			mode;	/* KD_TEXT or KD_GRAPHICS */
149 	caddr_t			text_base;	/* hardware text base */
150 	char			shadow[TEXT_ROWS*TEXT_COLS*2];
151 	caddr_t			current_base;	/* hardware or shadow */
152 	struct {
153 		boolean_t visible;
154 		int row;
155 		int col;
156 	}			cursor;
157 	struct vis_polledio	polledio;
158 	struct {
159 		unsigned char red;
160 		unsigned char green;
161 		unsigned char blue;
162 	}			colormap[VGA8_CMAP_ENTRIES];
163 	unsigned char attrib_palette[VGA_ATR_NUM_PLT];
164 	agp_master_softc_t	*agp_master; /* NULL means not PCI, for AGP */
165 	ddi_acc_handle_t	*pci_cfg_hdlp;	/* PCI conf handle */
166 	unsigned int flags;
167 	kmutex_t lock;
168 };
169 
170 #define	VGATEXT_FLAG_CONSOLE 0x00000001
171 #define	VGATEXT_IS_CONSOLE(softc) ((softc)->flags & VGATEXT_FLAG_CONSOLE)
172 
173 static int vgatext_devinit(struct vgatext_softc *, struct vis_devinit *data);
174 static void	vgatext_cons_copy(struct vgatext_softc *,
175 			struct vis_conscopy *);
176 static void	vgatext_cons_display(struct vgatext_softc *,
177 			struct vis_consdisplay *);
178 static void	vgatext_cons_cursor(struct vgatext_softc *,
179 			struct vis_conscursor *);
180 static void	vgatext_polled_copy(struct vis_polledio_arg *,
181 			struct vis_conscopy *);
182 static void	vgatext_polled_display(struct vis_polledio_arg *,
183 			struct vis_consdisplay *);
184 static void	vgatext_polled_cursor(struct vis_polledio_arg *,
185 			struct vis_conscursor *);
186 static void	vgatext_init(struct vgatext_softc *);
187 static void	vgatext_set_text(struct vgatext_softc *);
188 #if	defined(USE_BORDERS)
189 static void	vgatext_init_graphics(struct vgatext_softc *);
190 #endif
191 static int vgatext_kdsetmode(struct vgatext_softc *softc, int mode);
192 static void vgatext_setfont(struct vgatext_softc *softc);
193 static void vgatext_get_cursor(struct vgatext_softc *softc,
194 		screen_pos_t *row, screen_pos_t *col);
195 static void vgatext_set_cursor(struct vgatext_softc *softc, int row, int col);
196 static void vgatext_hide_cursor(struct vgatext_softc *softc);
197 static void vgatext_save_colormap(struct vgatext_softc *softc);
198 static void vgatext_restore_colormap(struct vgatext_softc *softc);
199 static int vgatext_get_pci_reg_index(dev_info_t *const devi,
200 		unsigned long himask, unsigned long hival, unsigned long addr,
201 		off_t *offset);
202 static int vgatext_get_isa_reg_index(dev_info_t *const devi,
203 		unsigned long hival, unsigned long addr, off_t *offset);
204 static void	*vgatext_softc_head;
205 static char	vgatext_silent;
206 static char	happyface_boot;
207 
208 /* Loadable Driver stuff */
209 
210 static struct modldrv modldrv = {
211 	&mod_driverops,		/* Type of module.  This one is a driver */
212 	"VGA text driver",	/* Name of the module. */
213 	&vgatext_ops,		/* driver ops */
214 };
215 
216 static struct modlinkage modlinkage = {
217 	MODREV_1, (void *) &modldrv, NULL
218 };
219 
220 typedef enum pc_colors {
221 	pc_black	= 0,
222 	pc_blue		= 1,
223 	pc_green	= 2,
224 	pc_cyan		= 3,
225 	pc_red		= 4,
226 	pc_magenta	= 5,
227 	pc_brown	= 6,
228 	pc_white	= 7,
229 	pc_grey		= 8,
230 	pc_brt_blue	= 9,
231 	pc_brt_green	= 10,
232 	pc_brt_cyan	= 11,
233 	pc_brt_red	= 12,
234 	pc_brt_magenta	= 13,
235 	pc_yellow	= 14,
236 	pc_brt_white	= 15
237 } pc_colors_t;
238 
239 static const unsigned char solaris_color_to_pc_color[16] = {
240 	pc_brt_white,		/*  0 - brt_white	*/
241 	pc_black,		/*  1 - black		*/
242 	pc_blue,		/*  2 - blue		*/
243 	pc_green,		/*  3 - green		*/
244 	pc_cyan,		/*  4 - cyan		*/
245 	pc_red,			/*  5 - red		*/
246 	pc_magenta,		/*  6 - magenta		*/
247 	pc_brown,		/*  7 - brown		*/
248 	pc_white,		/*  8 - white		*/
249 	pc_grey,		/*  9 - gery		*/
250 	pc_brt_blue,		/* 10 - brt_blue	*/
251 	pc_brt_green,		/* 11 - brt_green	*/
252 	pc_brt_cyan,		/* 12 - brt_cyan	*/
253 	pc_brt_red,		/* 13 - brt_red		*/
254 	pc_brt_magenta,		/* 14 - brt_magenta	*/
255 	pc_yellow		/* 15 - yellow		*/
256 };
257 
258 static ddi_device_acc_attr_t i8xx_dev_access = {
259 	DDI_DEVICE_ATTR_V0,
260 	DDI_NEVERSWAP_ACC,
261 	DDI_STRICTORDER_ACC
262 };
263 
264 static ddi_device_acc_attr_t dev_attr = {
265 	DDI_DEVICE_ATTR_V0,
266 	DDI_NEVERSWAP_ACC,
267 	DDI_STRICTORDER_ACC,
268 };
269 
270 int
271 _init(void)
272 {
273 	int e;
274 
275 	if ((e = ddi_soft_state_init(&vgatext_softc_head,
276 		    sizeof (struct vgatext_softc), 1)) != 0) {
277 	    return (e);
278 	}
279 
280 	e = mod_install(&modlinkage);
281 
282 	if (e) {
283 		ddi_soft_state_fini(&vgatext_softc_head);
284 	}
285 	return (e);
286 }
287 
288 int
289 _fini(void)
290 {
291 	int e;
292 
293 	if ((e = mod_remove(&modlinkage)) != 0)
294 		return (e);
295 
296 	ddi_soft_state_fini(&vgatext_softc_head);
297 
298 	return (0);
299 }
300 
301 int
302 _info(struct modinfo *modinfop)
303 {
304 	return (mod_info(&modlinkage, modinfop));
305 }
306 
307 /* default structure for FBIOGATTR ioctl */
308 static struct fbgattr vgatext_attr =  {
309 /*	real_type	owner */
310 	FBTYPE_SUNFAST_COLOR, 0,
311 /* fbtype: type		h  w  depth cms  size */
312 	{ FBTYPE_SUNFAST_COLOR, TEXT_ROWS, TEXT_COLS, 1,    256,  0 },
313 /* fbsattr: flags emu_type	dev_specific */
314 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
315 /*	emu_types */
316 	{ -1 }
317 };
318 
319 /*
320  * handy macros
321  */
322 
323 #define	getsoftc(instance) ((struct vgatext_softc *)	\
324 			ddi_get_soft_state(vgatext_softc_head, (instance)))
325 
326 #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
327 
328 static void
329 vgatext_check_for_console(dev_info_t *devi, struct vgatext_softc *softc,
330 	int pci_pcie_bus)
331 {
332 	ddi_acc_handle_t pci_conf;
333 	dev_info_t *pdevi;
334 	uint16_t data16;
335 
336 	/*
337 	 * Based on Section 11.3, "PCI Display Subsystem Initialization",
338 	 * of the 1.1 PCI-to-PCI Bridge Architecture Specification
339 	 * determine if this is the boot console device.  First, see
340 	 * if the SBIOS has turned on PCI I/O for this device.  Then if
341 	 * this is PCI/PCI-E, verify the parent bridge has VGAEnable set.
342 	 */
343 
344 	if (pci_config_setup(devi, &pci_conf) != DDI_SUCCESS) {
345 		cmn_err(CE_WARN,
346 		    MYNAME ": can't get PCI conf handle");
347 		return;
348 	}
349 
350 	data16 = pci_config_get16(pci_conf, PCI_CONF_COMM);
351 	if (data16 & PCI_COMM_IO)
352 		softc->flags |= VGATEXT_FLAG_CONSOLE;
353 
354 	pci_config_teardown(&pci_conf);
355 
356 	/* If IO not enabled or ISA/EISA, just return */
357 	if (!(softc->flags & VGATEXT_FLAG_CONSOLE) || !pci_pcie_bus)
358 		return;
359 
360 	/*
361 	 * Check for VGA Enable in the Bridge Control register for all
362 	 * PCI/PCIEX parents.  If not set all the way up the chain,
363 	 * this cannot be the boot console.
364 	 */
365 
366 	pdevi = ddi_get_parent(devi);
367 	while (pdevi) {
368 		int	error;
369 		ddi_acc_handle_t ppci_conf;
370 		char	*parent_type = NULL;
371 
372 		error = ddi_prop_lookup_string(DDI_DEV_T_ANY, pdevi,
373 		    DDI_PROP_DONTPASS, "device_type", &parent_type);
374 		if (error != DDI_SUCCESS) {
375 			return;
376 		}
377 
378 		/* Verify still on the PCI/PCIEX parent tree */
379 		if (!STREQ(parent_type, "pci") &&
380 		    !STREQ(parent_type, "pciex")) {
381 			ddi_prop_free(parent_type);
382 			return;
383 		}
384 
385 		ddi_prop_free(parent_type);
386 		parent_type = NULL;
387 
388 		if (pci_config_setup(pdevi, &ppci_conf) != DDI_SUCCESS) {
389 			/* No registers on root node, done with check */
390 			return;
391 		}
392 
393 		data16 = pci_config_get16(ppci_conf, PCI_BCNF_BCNTRL);
394 		pci_config_teardown(&ppci_conf);
395 
396 		if (!(data16 & PCI_BCNF_BCNTRL_VGA_ENABLE)) {
397 			softc->flags &= ~VGATEXT_FLAG_CONSOLE;
398 			return;
399 		}
400 
401 		pdevi = ddi_get_parent(pdevi);
402 	}
403 }
404 
405 static int
406 vgatext_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
407 {
408 	struct vgatext_softc *softc;
409 	int	unit = ddi_get_instance(devi);
410 	int	error;
411 	char	*parent_type = NULL;
412 	int	reg_rnumber;
413 	int	agpm = 0;
414 	off_t	reg_offset;
415 	off_t	mem_offset;
416 	char	buf[80], *cons;
417 	int pci_pcie_bus = 0;
418 
419 
420 	switch (cmd) {
421 	case DDI_ATTACH:
422 		break;
423 
424 	case DDI_RESUME:
425 		/*
426 		 * Though vgatext doesn't really know how to resume
427 		 * on a generic framebuffer, we should succeed, as
428 		 * it is far better to have no console, than potentiall
429 		 * have no machine.
430 		 */
431 		return (DDI_SUCCESS);
432 	default:
433 		return (DDI_FAILURE);
434 	}
435 
436 	/* DDI_ATTACH */
437 
438 	/* Allocate softc struct */
439 	if (ddi_soft_state_zalloc(vgatext_softc_head, unit) != DDI_SUCCESS) {
440 		return (DDI_FAILURE);
441 	}
442 	softc = getsoftc(unit);
443 
444 	/* link it in */
445 	softc->devi = devi;
446 	ddi_set_driver_private(devi, softc);
447 
448 	softc->polledio.arg = (struct vis_polledio_arg *)softc;
449 	softc->polledio.display = vgatext_polled_display;
450 	softc->polledio.copy = vgatext_polled_copy;
451 	softc->polledio.cursor = vgatext_polled_cursor;
452 
453 	mutex_init(&(softc->lock), NULL, MUTEX_DRIVER, NULL);
454 
455 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
456 	    DDI_PROP_DONTPASS, "device_type", &parent_type);
457 	if (error != DDI_SUCCESS) {
458 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
459 		goto fail;
460 	}
461 
462 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
463 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
464 		    &reg_offset);
465 		if (reg_rnumber < 0) {
466 			cmn_err(CE_WARN,
467 			    MYNAME ": can't find reg entry for registers");
468 			error = DDI_FAILURE;
469 			goto fail;
470 		}
471 		softc->fb_regno = vgatext_get_isa_reg_index(devi, 0,
472 		    VGA_MEM_ADDR, &mem_offset);
473 		if (softc->fb_regno < 0) {
474 			cmn_err(CE_WARN,
475 			    MYNAME ": can't find reg entry for memory");
476 			error = DDI_FAILURE;
477 			goto fail;
478 		}
479 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
480 		pci_pcie_bus = 1;
481 		reg_rnumber = vgatext_get_pci_reg_index(devi,
482 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
483 		    PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
484 		    &reg_offset);
485 		if (reg_rnumber < 0) {
486 			cmn_err(CE_WARN,
487 			    MYNAME ": can't find reg entry for registers");
488 			error = DDI_FAILURE;
489 			goto fail;
490 		}
491 		softc->fb_regno = vgatext_get_pci_reg_index(devi,
492 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
493 		    PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
494 		    &mem_offset);
495 		if (softc->fb_regno < 0) {
496 			cmn_err(CE_WARN,
497 			    MYNAME ": can't find reg entry for memory");
498 			error = DDI_FAILURE;
499 			goto fail;
500 		}
501 		agpm = 1;	/* should have AGP master support */
502 	} else {
503 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
504 		    parent_type);
505 		error = DDI_FAILURE;
506 		goto fail;
507 	}
508 	ddi_prop_free(parent_type);
509 	parent_type = NULL;
510 
511 	error = ddi_regs_map_setup(devi, reg_rnumber,
512 	    (caddr_t *)&softc->regs.addr, reg_offset, VGA_REG_SIZE,
513 	    &dev_attr, &softc->regs.handle);
514 	if (error != DDI_SUCCESS)
515 		goto fail;
516 	softc->regs.mapped = B_TRUE;
517 
518 	softc->fb_size = VGA_MEM_SIZE;
519 
520 	error = ddi_regs_map_setup(devi, softc->fb_regno,
521 	    (caddr_t *)&softc->fb.addr,
522 	    mem_offset, softc->fb_size,
523 	    &dev_attr, &softc->fb.handle);
524 	if (error != DDI_SUCCESS)
525 		goto fail;
526 	softc->fb.mapped = B_TRUE;
527 
528 	if (ddi_get8(softc->regs.handle,
529 	    softc->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
530 		softc->text_base = (caddr_t)softc->fb.addr + VGA_COLOR_BASE;
531 	else
532 		softc->text_base = (caddr_t)softc->fb.addr + VGA_MONO_BASE;
533 
534 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
535 	    DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
536 		if (strcmp(cons, "graphics") == 0) {
537 			happyface_boot = 1;
538 			vgatext_silent = 1;
539 			softc->current_base = softc->shadow;
540 		} else {
541 			softc->current_base = softc->text_base;
542 		}
543 		ddi_prop_free(cons);
544 	} else {
545 		softc->current_base = softc->text_base;
546 	}
547 
548 	(void) sprintf(buf, "text-%d", unit);
549 	error = ddi_create_minor_node(devi, buf, S_IFCHR,
550 	    INST2NODE1(unit), DDI_NT_DISPLAY, NULL);
551 	if (error != DDI_SUCCESS)
552 		goto fail;
553 
554 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
555 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
556 	if (error != DDI_SUCCESS)
557 		goto fail;
558 
559 	vgatext_check_for_console(devi, softc, pci_pcie_bus);
560 
561 	/* only do this if not in graphics mode */
562 	if ((vgatext_silent == 0) && (VGATEXT_IS_CONSOLE(softc))) {
563 		vgatext_init(softc);
564 		vgatext_save_colormap(softc);
565 	}
566 
567 	if (agpm != 0) { /* try AGP master attach */
568 		/* setup mapping for PCI config space access */
569 		softc->pci_cfg_hdlp = (ddi_acc_handle_t *)
570 		    kmem_zalloc(sizeof (ddi_acc_handle_t), KM_SLEEP);
571 		error = pci_config_setup(devi, softc->pci_cfg_hdlp);
572 		if (error != DDI_SUCCESS) {
573 			cmn_err(CE_WARN, "vgatext_attach: "
574 			    "PCI configuration space setup failed");
575 			goto fail;
576 		}
577 
578 		(void) agpmaster_attach(softc->devi, &softc->agp_master,
579 		    *softc->pci_cfg_hdlp, INST2NODE2(unit));
580 	}
581 
582 	return (DDI_SUCCESS);
583 
584 fail:
585 	if (parent_type != NULL)
586 		ddi_prop_free(parent_type);
587 	(void) vgatext_detach(devi, DDI_DETACH);
588 	return (error);
589 }
590 
591 static int
592 vgatext_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
593 {
594 	int instance = ddi_get_instance(devi);
595 	struct vgatext_softc *softc = getsoftc(instance);
596 
597 
598 	switch (cmd) {
599 	case DDI_DETACH:
600 		if (softc->agp_master != NULL) { /* agp initiated */
601 			agpmaster_detach(&softc->agp_master);
602 			pci_config_teardown(softc->pci_cfg_hdlp);
603 		}
604 
605 		if (softc->fb.mapped)
606 			ddi_regs_map_free(&softc->fb.handle);
607 		if (softc->regs.mapped)
608 			ddi_regs_map_free(&softc->regs.handle);
609 		mutex_destroy(&(softc->lock));
610 		ddi_remove_minor_node(devi, NULL);
611 		(void) ddi_soft_state_free(vgatext_softc_head, instance);
612 		return (DDI_SUCCESS);
613 
614 	case DDI_SUSPEND:
615 		/*
616 		 * This is a generic VGA file, and therefore, cannot
617 		 * understand how to deal with suspend and resume on
618 		 * a generic interface.  So we fail any attempt to
619 		 * suspend.  At some point in the future, we might use
620 		 * this as an entrypoint for display drivers and this
621 		 * assumption may change.
622 		 *
623 		 * However, from a platform development perspective,
624 		 * it is important that this driver suspend if a
625 		 * developer is using a serial console and/or working
626 		 * on a framebuffer driver that will support suspend
627 		 * and resume.  Therefore, we have this module tunable
628 		 * (purposely using a long name) that will allow for
629 		 * suspend it it is set.  Otherwise we fail.
630 		 */
631 		if (vgatext_force_suspend != 0)
632 			return (DDI_SUCCESS);
633 		else
634 			return (DDI_FAILURE);
635 
636 	default:
637 		cmn_err(CE_WARN, "vgatext_detach: unknown cmd 0x%x\n", cmd);
638 		return (DDI_FAILURE);
639 	}
640 }
641 
642 /*ARGSUSED*/
643 static int
644 vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
645 {
646 	dev_t dev;
647 	int error;
648 	int instance;
649 	struct vgatext_softc *softc;
650 
651 	error = DDI_SUCCESS;
652 
653 	dev = (dev_t)arg;
654 	instance = DEV2INST(dev);
655 	softc = getsoftc(instance);
656 
657 	switch (infocmd) {
658 	case DDI_INFO_DEVT2DEVINFO:
659 		if (softc == NULL || softc->devi == NULL) {
660 			error = DDI_FAILURE;
661 		} else {
662 			*result = (void *) softc->devi;
663 			error = DDI_SUCCESS;
664 		}
665 		break;
666 	case DDI_INFO_DEVT2INSTANCE:
667 		*result = (void *)(uintptr_t)instance;
668 		error = DDI_SUCCESS;
669 		break;
670 	default:
671 		error = DDI_FAILURE;
672 		break;
673 	}
674 	return (error);
675 }
676 
677 
678 /*ARGSUSED*/
679 static int
680 vgatext_open(dev_t *devp, int flag, int otyp, cred_t *cred)
681 {
682 	struct vgatext_softc *softc = getsoftc(DEV2INST(*devp));
683 
684 	if (softc == NULL || otyp == OTYP_BLK)
685 		return (ENXIO);
686 
687 	return (0);
688 }
689 
690 /*ARGSUSED*/
691 static int
692 vgatext_close(dev_t devp, int flag, int otyp, cred_t *cred)
693 {
694 	return (0);
695 }
696 
697 static int
698 do_gfx_ioctl(int cmd, intptr_t data, int mode, struct vgatext_softc *softc)
699 {
700 	static char kernel_only[] =
701 	    "do_gfx_ioctl: %s is a kernel only ioctl";
702 	int err;
703 	int kd_mode;
704 
705 	switch (cmd) {
706 	case KDSETMODE:
707 		return (vgatext_kdsetmode(softc, (int)data));
708 
709 	case KDGETMODE:
710 		kd_mode = softc->mode;
711 		if (ddi_copyout(&kd_mode, (void *)data, sizeof (int), mode))
712 			return (EFAULT);
713 		break;
714 
715 	case VIS_GETIDENTIFIER:
716 		if (ddi_copyout(&text_ident, (void *)data,
717 		    sizeof (struct vis_identifier), mode))
718 			return (EFAULT);
719 		break;
720 
721 	case VIS_DEVINIT:
722 
723 		if (!(mode & FKIOCTL)) {
724 			cmn_err(CE_CONT, kernel_only, "VIS_DEVINIT");
725 			return (ENXIO);
726 		}
727 
728 		err = vgatext_devinit(softc, (struct vis_devinit *)data);
729 		if (err != 0) {
730 			cmn_err(CE_WARN,
731 			    "vgatext_ioctl:  could not initialize console");
732 			return (err);
733 		}
734 		break;
735 
736 	case VIS_CONSCOPY:	/* move */
737 		{
738 		struct vis_conscopy pma;
739 
740 		if (ddi_copyin((void *)data, &pma,
741 		    sizeof (struct vis_conscopy), mode))
742 			return (EFAULT);
743 
744 		vgatext_cons_copy(softc, &pma);
745 		break;
746 		}
747 
748 	case VIS_CONSDISPLAY:	/* display */
749 		{
750 		struct vis_consdisplay display_request;
751 
752 		if (ddi_copyin((void *)data, &display_request,
753 		    sizeof (display_request), mode))
754 			return (EFAULT);
755 
756 		vgatext_cons_display(softc, &display_request);
757 		break;
758 		}
759 
760 	case VIS_CONSCURSOR:
761 		{
762 		struct vis_conscursor cursor_request;
763 
764 		if (ddi_copyin((void *)data, &cursor_request,
765 		    sizeof (cursor_request), mode))
766 			return (EFAULT);
767 
768 		vgatext_cons_cursor(softc, &cursor_request);
769 
770 		if (cursor_request.action == VIS_GET_CURSOR &&
771 		    ddi_copyout(&cursor_request, (void *)data,
772 		    sizeof (cursor_request), mode))
773 			return (EFAULT);
774 		break;
775 		}
776 
777 	case VIS_GETCMAP:
778 	case VIS_PUTCMAP:
779 	case FBIOPUTCMAP:
780 	case FBIOGETCMAP:
781 		/*
782 		 * At the moment, text mode is not considered to have
783 		 * a color map.
784 		 */
785 		return (EINVAL);
786 
787 	case FBIOGATTR:
788 		if (copyout(&vgatext_attr, (void *)data,
789 		    sizeof (struct fbgattr)))
790 			return (EFAULT);
791 		break;
792 
793 	case FBIOGTYPE:
794 		if (copyout(&vgatext_attr.fbtype, (void *)data,
795 		    sizeof (struct fbtype)))
796 			return (EFAULT);
797 		break;
798 
799 	default:
800 		return (ENXIO);
801 	}
802 	return (0);
803 }
804 
805 
806 /*ARGSUSED*/
807 static int
808 vgatext_ioctl(
809     dev_t dev,
810     int cmd,
811     intptr_t data,
812     int mode,
813     cred_t *cred,
814     int *rval)
815 {
816 	struct vgatext_softc *softc = getsoftc(DEV2INST(dev));
817 	int err;
818 
819 	switch (DEV2MINOR(dev)) {
820 	case GFX_MINOR:
821 		mutex_enter(&(softc->lock));
822 		err = do_gfx_ioctl(cmd, data, mode, softc);
823 		mutex_exit(&(softc->lock));
824 		break;
825 
826 	case AGPMASTER_MINOR:
827 		err = agpmaster_ioctl(dev, cmd, data, mode, cred, rval,
828 		    softc->agp_master);
829 		break;
830 
831 	default:
832 		/* not a valid minor node */
833 		return (EBADF);
834 	}
835 	return (err);
836 }
837 
838 static void
839 vgatext_save_text(struct vgatext_softc *softc)
840 {
841 	unsigned i;
842 
843 	for (i = 0; i < sizeof (softc->shadow); i++)
844 		softc->shadow[i] = softc->current_base[i];
845 }
846 
847 static void
848 vgatext_progressbar_stop()
849 {
850 	extern void progressbar_stop(void);
851 
852 	if (vgatext_silent == 1) {
853 		vgatext_silent = 0;
854 		progressbar_stop();
855 	}
856 }
857 
858 static void
859 vgatext_kdsettext(struct vgatext_softc *softc)
860 {
861 	int i;
862 
863 	vgatext_init(softc);
864 	for (i = 0; i < sizeof (softc->shadow); i++) {
865 		softc->text_base[i] = softc->shadow[i];
866 	}
867 	softc->current_base = softc->text_base;
868 	if (softc->cursor.visible) {
869 		vgatext_set_cursor(softc,
870 		    softc->cursor.row, softc->cursor.col);
871 	}
872 	vgatext_restore_colormap(softc);
873 }
874 
875 static void
876 vgatext_kdsetgraphics(struct vgatext_softc *softc)
877 {
878 	vgatext_progressbar_stop();
879 	vgatext_save_text(softc);
880 	softc->current_base = softc->shadow;
881 #if	defined(USE_BORDERS)
882 	vgatext_init_graphics(softc);
883 #endif
884 }
885 
886 static int
887 vgatext_kdsetmode(struct vgatext_softc *softc, int mode)
888 {
889 	if ((mode == softc->mode) || (!VGATEXT_IS_CONSOLE(softc)))
890 		return (0);
891 
892 	switch (mode) {
893 	case KD_TEXT:
894 		vgatext_kdsettext(softc);
895 		break;
896 
897 	case KD_GRAPHICS:
898 		vgatext_kdsetgraphics(softc);
899 		break;
900 
901 	case KD_RESETTEXT:
902 		/*
903 		 * In order to avoid racing with a starting X server,
904 		 * this needs to be a test and set that is performed in
905 		 * a single (softc->lock protected) ioctl into this driver.
906 		 */
907 		if (softc->mode == KD_TEXT && vgatext_silent == 1) {
908 			vgatext_progressbar_stop();
909 			vgatext_kdsettext(softc);
910 		}
911 		break;
912 
913 	default:
914 		return (EINVAL);
915 	}
916 	softc->mode = mode;
917 	return (0);
918 }
919 
920 /*ARGSUSED*/
921 static int
922 vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
923 		size_t *maplen, uint_t model)
924 {
925 	struct vgatext_softc *softc;
926 	int err;
927 	size_t length;
928 
929 
930 	softc = getsoftc(DEV2INST(dev));
931 	if (softc == NULL) {
932 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
933 		return (-1);
934 	}
935 
936 	if (!(off >= VGA_MMAP_FB_BASE &&
937 	    off < VGA_MMAP_FB_BASE + softc->fb_size)) {
938 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
939 		return (-1);
940 	}
941 
942 	if (off + len > VGA_MMAP_FB_BASE + softc->fb_size)
943 		length = VGA_MMAP_FB_BASE + softc->fb_size - off;
944 	else
945 		length = len;
946 
947 	if ((err = devmap_devmem_setup(dhp, softc->devi, NULL, softc->fb_regno,
948 	    off - VGA_MMAP_FB_BASE,
949 	    length, PROT_ALL, 0, &dev_attr)) < 0) {
950 		return (err);
951 	}
952 
953 
954 	*maplen = length;
955 	return (0);
956 }
957 
958 
959 static int
960 vgatext_devinit(struct vgatext_softc *softc, struct vis_devinit *data)
961 {
962 	/* initialize console instance */
963 	data->version = VIS_CONS_REV;
964 	data->width = TEXT_COLS;
965 	data->height = TEXT_ROWS;
966 	data->linebytes = TEXT_COLS;
967 	data->depth = 4;
968 	data->mode = VIS_TEXT;
969 	data->polledio = &softc->polledio;
970 
971 	return (0);
972 }
973 
974 /*
975  * display a string on the screen at (row, col)
976  *	 assume it has been cropped to fit.
977  */
978 
979 static void
980 vgatext_cons_display(struct vgatext_softc *softc, struct vis_consdisplay *da)
981 {
982 	unsigned char	*string;
983 	int	i;
984 	unsigned char	attr;
985 	struct cgatext {
986 		unsigned char ch;
987 		unsigned char attr;
988 	};
989 	struct cgatext *addr;
990 
991 	/*
992 	 * Sanity checks.  This is a last-ditch effort to avoid damage
993 	 * from brokenness or maliciousness above.
994 	 */
995 	if (da->row < 0 || da->row >= TEXT_ROWS ||
996 	    da->col < 0 || da->col >= TEXT_COLS ||
997 	    da->col + da->width > TEXT_COLS)
998 		return;
999 
1000 	/*
1001 	 * To be fully general, we should copyin the data.  This is not
1002 	 * really relevant for this text-only driver, but a graphical driver
1003 	 * should support these ioctls from userland to enable simple
1004 	 * system startup graphics.
1005 	 */
1006 	attr = (solaris_color_to_pc_color[da->bg_color & 0xf] << 4)
1007 	    | solaris_color_to_pc_color[da->fg_color & 0xf];
1008 	string = da->data;
1009 	addr = (struct cgatext *)softc->current_base
1010 	    +  (da->row * TEXT_COLS + da->col);
1011 	for (i = 0; i < da->width; i++) {
1012 		addr->ch = string[i];
1013 		addr->attr = attr;
1014 		addr++;
1015 	}
1016 }
1017 
1018 static void
1019 vgatext_polled_display(
1020 	struct vis_polledio_arg *arg,
1021 	struct vis_consdisplay *da)
1022 {
1023 	vgatext_cons_display((struct vgatext_softc *)arg, da);
1024 }
1025 
1026 /*
1027  * screen-to-screen copy
1028  */
1029 
1030 static void
1031 vgatext_cons_copy(struct vgatext_softc *softc, struct vis_conscopy *ma)
1032 {
1033 	unsigned short	*from;
1034 	unsigned short	*to;
1035 	int		cnt;
1036 	screen_size_t chars_per_row;
1037 	unsigned short	*to_row_start;
1038 	unsigned short	*from_row_start;
1039 	screen_size_t	rows_to_move;
1040 	unsigned short	*base;
1041 
1042 	/*
1043 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
1044 	 * damage caused by broken-ness or maliciousness above.
1045 	 */
1046 	if (ma->s_col < 0 || ma->s_col >= TEXT_COLS ||
1047 	    ma->s_row < 0 || ma->s_row >= TEXT_ROWS ||
1048 	    ma->e_col < 0 || ma->e_col >= TEXT_COLS ||
1049 	    ma->e_row < 0 || ma->e_row >= TEXT_ROWS ||
1050 	    ma->t_col < 0 || ma->t_col >= TEXT_COLS ||
1051 	    ma->t_row < 0 || ma->t_row >= TEXT_ROWS ||
1052 	    ma->s_col > ma->e_col ||
1053 	    ma->s_row > ma->e_row)
1054 		return;
1055 
1056 	/*
1057 	 * Remember we're going to copy shorts because each
1058 	 * character/attribute pair is 16 bits.
1059 	 */
1060 	chars_per_row = ma->e_col - ma->s_col + 1;
1061 	rows_to_move = ma->e_row - ma->s_row + 1;
1062 
1063 	/* More sanity checks. */
1064 	if (ma->t_row + rows_to_move > TEXT_ROWS ||
1065 	    ma->t_col + chars_per_row > TEXT_COLS)
1066 		return;
1067 
1068 	base = (unsigned short *)softc->current_base;
1069 
1070 	to_row_start = base + ((ma->t_row * TEXT_COLS) + ma->t_col);
1071 	from_row_start = base + ((ma->s_row * TEXT_COLS) + ma->s_col);
1072 
1073 	if (to_row_start < from_row_start) {
1074 		while (rows_to_move-- > 0) {
1075 			to = to_row_start;
1076 			from = from_row_start;
1077 			to_row_start += TEXT_COLS;
1078 			from_row_start += TEXT_COLS;
1079 			for (cnt = chars_per_row; cnt-- > 0; )
1080 				*to++ = *from++;
1081 		}
1082 	} else {
1083 		/*
1084 		 * Offset to the end of the region and copy backwards.
1085 		 */
1086 		cnt = rows_to_move * TEXT_COLS + chars_per_row;
1087 		to_row_start += cnt;
1088 		from_row_start += cnt;
1089 
1090 		while (rows_to_move-- > 0) {
1091 			to_row_start -= TEXT_COLS;
1092 			from_row_start -= TEXT_COLS;
1093 			to = to_row_start;
1094 			from = from_row_start;
1095 			for (cnt = chars_per_row; cnt-- > 0; )
1096 				*--to = *--from;
1097 		}
1098 	}
1099 }
1100 
1101 static void
1102 vgatext_polled_copy(
1103 	struct vis_polledio_arg *arg,
1104 	struct vis_conscopy *ca)
1105 {
1106 	vgatext_cons_copy((struct vgatext_softc *)arg, ca);
1107 }
1108 
1109 
1110 static void
1111 vgatext_cons_cursor(struct vgatext_softc *softc, struct vis_conscursor *ca)
1112 {
1113 	if (vgatext_silent)
1114 		return;
1115 
1116 	switch (ca->action) {
1117 	case VIS_HIDE_CURSOR:
1118 		softc->cursor.visible = B_FALSE;
1119 		if (softc->current_base == softc->text_base)
1120 			vgatext_hide_cursor(softc);
1121 		break;
1122 	case VIS_DISPLAY_CURSOR:
1123 		/*
1124 		 * Sanity check.  This is a last-ditch effort to avoid
1125 		 * damage from brokenness or maliciousness above.
1126 		 */
1127 		if (ca->col < 0 || ca->col >= TEXT_COLS ||
1128 		    ca->row < 0 || ca->row >= TEXT_ROWS)
1129 			return;
1130 
1131 		softc->cursor.visible = B_TRUE;
1132 		softc->cursor.col = ca->col;
1133 		softc->cursor.row = ca->row;
1134 		if (softc->current_base == softc->text_base)
1135 			vgatext_set_cursor(softc, ca->row, ca->col);
1136 		break;
1137 	case VIS_GET_CURSOR:
1138 		if (softc->current_base == softc->text_base) {
1139 			vgatext_get_cursor(softc, &ca->row, &ca->col);
1140 		}
1141 		break;
1142 	}
1143 }
1144 
1145 static void
1146 vgatext_polled_cursor(
1147 	struct vis_polledio_arg *arg,
1148 	struct vis_conscursor *ca)
1149 {
1150 	vgatext_cons_cursor((struct vgatext_softc *)arg, ca);
1151 }
1152 
1153 
1154 
1155 /*ARGSUSED*/
1156 static void
1157 vgatext_hide_cursor(struct vgatext_softc *softc)
1158 {
1159 	/* Nothing at present */
1160 }
1161 
1162 static void
1163 vgatext_set_cursor(struct vgatext_softc *softc, int row, int col)
1164 {
1165 	short	addr;
1166 
1167 	if (vgatext_silent)
1168 		return;
1169 
1170 	addr = row * TEXT_COLS + col;
1171 
1172 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAH, addr >> 8);
1173 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAL, addr & 0xff);
1174 }
1175 
1176 static int vga_row, vga_col;
1177 
1178 static void
1179 vgatext_get_cursor(struct vgatext_softc *softc,
1180     screen_pos_t *row, screen_pos_t *col)
1181 {
1182 	short   addr;
1183 
1184 	addr = (vga_get_crtc(&softc->regs, VGA_CRTC_CLAH) << 8) +
1185 	    vga_get_crtc(&softc->regs, VGA_CRTC_CLAL);
1186 
1187 	vga_row = *row = addr / TEXT_COLS;
1188 	vga_col = *col = addr % TEXT_COLS;
1189 }
1190 
1191 /*
1192  * This code is experimental. It's only enabled if console is
1193  * set to graphics, a preliminary implementation of happyface boot.
1194  */
1195 static void
1196 vgatext_set_text(struct vgatext_softc *softc)
1197 {
1198 	int i;
1199 
1200 	if (happyface_boot == 0)
1201 		return;
1202 
1203 	/* we are in graphics mode, set to text 80X25 mode */
1204 
1205 	/* set misc registers */
1206 	vga_set_reg(&softc->regs, VGA_MISC_W, VGA_MISC_TEXT);
1207 
1208 	/* set sequencer registers */
1209 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
1210 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) &
1211 	    ~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
1212 	for (i = 1; i < NUM_SEQ_REG; i++) {
1213 		vga_set_seq(&softc->regs, i, VGA_SEQ_TEXT[i]);
1214 	}
1215 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
1216 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) |
1217 	    VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
1218 	    VGA_SEQ_RST_SYN_NO_SYNC_RESET));
1219 
1220 	/* set crt controller registers */
1221 	vga_set_crtc(&softc->regs, VGA_CRTC_VRE,
1222 	    (vga_get_crtc(&softc->regs, VGA_CRTC_VRE) &
1223 	    ~VGA_CRTC_VRE_LOCK));
1224 	for (i = 0; i < NUM_CRTC_REG; i++) {
1225 		vga_set_crtc(&softc->regs, i, VGA_CRTC_TEXT[i]);
1226 	}
1227 
1228 	/* set graphics controller registers */
1229 	for (i = 0; i < NUM_GRC_REG; i++) {
1230 		vga_set_grc(&softc->regs, i, VGA_GRC_TEXT[i]);
1231 	}
1232 
1233 	/* set attribute registers */
1234 	for (i = 0; i < NUM_ATR_REG; i++) {
1235 		vga_set_atr(&softc->regs, i, VGA_ATR_TEXT[i]);
1236 	}
1237 
1238 	/* set palette */
1239 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
1240 		vga_put_cmap(&softc->regs, i, VGA_TEXT_PALETTES[i][0] << 2,
1241 		    VGA_TEXT_PALETTES[i][1] << 2,
1242 		    VGA_TEXT_PALETTES[i][2] << 2);
1243 	}
1244 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
1245 		vga_put_cmap(&softc->regs, i, 0, 0, 0);
1246 	}
1247 
1248 	vgatext_save_colormap(softc);
1249 }
1250 
1251 static void
1252 vgatext_init(struct vgatext_softc *softc)
1253 {
1254 	unsigned char atr_mode;
1255 
1256 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
1257 	if (atr_mode & VGA_ATR_MODE_GRAPH)
1258 		vgatext_set_text(softc);
1259 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
1260 	atr_mode &= ~VGA_ATR_MODE_BLINK;
1261 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
1262 	vga_set_atr(&softc->regs, VGA_ATR_MODE, atr_mode);
1263 #if	defined(USE_BORDERS)
1264 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
1265 	    vga_get_atr(&softc->regs, VGA_BRIGHT_WHITE));
1266 #else
1267 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
1268 	    vga_get_atr(&softc->regs, VGA_BLACK));
1269 #endif
1270 	vgatext_setfont(softc);	/* need selectable font? */
1271 }
1272 
1273 #if	defined(USE_BORDERS)
1274 static void
1275 vgatext_init_graphics(struct vgatext_softc *softc)
1276 {
1277 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
1278 	    vga_get_atr(&softc->regs, VGA_BLACK));
1279 }
1280 #endif
1281 
1282 static char vga_fontslot = 0;
1283 
1284 static void
1285 vgatext_setfont(struct vgatext_softc *softc)
1286 {
1287 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
1288 
1289 	extern unsigned char *ENCODINGS[];
1290 	uchar_t *from;
1291 	uchar_t volatile *to;
1292 	int	i, j, s;
1293 	int	bpc, f_offset;
1294 
1295 	/* Sync-reset the sequencer registers */
1296 	vga_set_seq(&softc->regs, 0x00, 0x01);
1297 	/*
1298 	 *  enable write to plane2, since fonts
1299 	 * could only be loaded into plane2
1300 	 */
1301 	vga_set_seq(&softc->regs, 0x02, 0x04);
1302 	/*
1303 	 *  sequentially access data in the bit map being
1304 	 * selected by MapMask register (index 0x02)
1305 	 */
1306 	vga_set_seq(&softc->regs, 0x04, 0x07);
1307 	/* Sync-reset ended, and allow the sequencer to operate */
1308 	vga_set_seq(&softc->regs, 0x00, 0x03);
1309 
1310 	/*
1311 	 *  select plane 2 on Read Mode 0
1312 	 */
1313 	vga_set_grc(&softc->regs, 0x04, 0x02);
1314 	/*
1315 	 *  system addresses sequentially access data, follow
1316 	 * Memory Mode register bit 2 in the sequencer
1317 	 */
1318 	vga_set_grc(&softc->regs, 0x05, 0x00);
1319 	/*
1320 	 * set range of host memory addresses decoded by VGA
1321 	 * hardware -- A0000h-BFFFFh (128K region)
1322 	 */
1323 	vga_set_grc(&softc->regs, 0x06, 0x00);
1324 
1325 	/*
1326 	 * This assumes 8x16 characters, which yield the traditional 80x25
1327 	 * screen.  It really should support other character heights.
1328 	 */
1329 	bpc = 16;
1330 	s = vga_fontslot;
1331 	f_offset = s * 8 * 1024;
1332 	for (i = 0; i < 256; i++) {
1333 		from = ENCODINGS[i];
1334 		to = (unsigned char *)softc->fb.addr + f_offset + i * 0x20;
1335 		for (j = 0; j < bpc; j++)
1336 			*to++ = *from++;
1337 	}
1338 
1339 	/* Sync-reset the sequencer registers */
1340 	vga_set_seq(&softc->regs, 0x00, 0x01);
1341 	/* enable write to plane 0 and 1 */
1342 	vga_set_seq(&softc->regs, 0x02, 0x03);
1343 	/*
1344 	 * enable character map selection
1345 	 * and odd/even addressing
1346 	 */
1347 	vga_set_seq(&softc->regs, 0x04, 0x03);
1348 	/*
1349 	 * select font map
1350 	 */
1351 	vga_set_seq(&softc->regs, 0x03, fsreg[s]);
1352 	/* Sync-reset ended, and allow the sequencer to operate */
1353 	vga_set_seq(&softc->regs, 0x00, 0x03);
1354 
1355 	/* restore graphic registers */
1356 
1357 	/* select plane 0 */
1358 	vga_set_grc(&softc->regs, 0x04, 0x00);
1359 	/* enable odd/even addressing mode */
1360 	vga_set_grc(&softc->regs, 0x05, 0x10);
1361 	/*
1362 	 * range of host memory addresses decoded by VGA
1363 	 * hardware -- B8000h-BFFFFh (32K region)
1364 	 */
1365 	vga_set_grc(&softc->regs, 0x06, 0x0e);
1366 	/* enable all color plane */
1367 	vga_set_atr(&softc->regs, 0x12, 0x0f);
1368 
1369 }
1370 
1371 static void
1372 vgatext_save_colormap(struct vgatext_softc *softc)
1373 {
1374 	int i;
1375 
1376 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1377 		softc->attrib_palette[i] = vga_get_atr(&softc->regs, i);
1378 	}
1379 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1380 		vga_get_cmap(&softc->regs, i,
1381 		    &softc->colormap[i].red,
1382 		    &softc->colormap[i].green,
1383 		    &softc->colormap[i].blue);
1384 	}
1385 }
1386 
1387 static void
1388 vgatext_restore_colormap(struct vgatext_softc *softc)
1389 {
1390 	int i;
1391 
1392 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1393 		vga_set_atr(&softc->regs, i, softc->attrib_palette[i]);
1394 	}
1395 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1396 		vga_put_cmap(&softc->regs, i,
1397 		    softc->colormap[i].red,
1398 		    softc->colormap[i].green,
1399 		    softc->colormap[i].blue);
1400 	}
1401 }
1402 
1403 /*
1404  * search the entries of the "reg" property for one which has the desired
1405  * combination of phys_hi bits and contains the desired address.
1406  *
1407  * This version searches a PCI-style "reg" property.  It was prompted by
1408  * issues surrounding the presence or absence of an entry for the ROM:
1409  * (a) a transition problem with PowerPC Virtual Open Firmware
1410  * (b) uncertainty as to whether an entry will be included on a device
1411  *     with ROM support (and so an "active" ROM base address register),
1412  *     but no ROM actually installed.
1413  *
1414  * See the note below on vgatext_get_isa_reg_index for the reasons for
1415  * returning the offset.
1416  *
1417  * Note that this routine may not be fully general; it is intended for the
1418  * specific purpose of finding a couple of particular VGA reg entries and
1419  * may not be suitable for all reg-searching purposes.
1420  */
1421 static int
1422 vgatext_get_pci_reg_index(
1423 	dev_info_t *const devi,
1424 	unsigned long himask,
1425 	unsigned long hival,
1426 	unsigned long addr,
1427 	off_t *offset)
1428 {
1429 
1430 	int			length, index;
1431 	pci_regspec_t	*reg;
1432 
1433 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1434 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1435 		return (-1);
1436 	}
1437 
1438 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
1439 		if ((reg[index].pci_phys_hi & himask) != hival)
1440 			continue;
1441 		if (reg[index].pci_size_hi != 0)
1442 			continue;
1443 		if (reg[index].pci_phys_mid != 0)
1444 			continue;
1445 		if (reg[index].pci_phys_low > addr)
1446 			continue;
1447 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
1448 			continue;
1449 
1450 		*offset = addr - reg[index].pci_phys_low;
1451 		kmem_free(reg, (size_t)length);
1452 		return (index);
1453 	}
1454 	kmem_free(reg, (size_t)length);
1455 
1456 	return (-1);
1457 }
1458 
1459 /*
1460  * search the entries of the "reg" property for one which has the desired
1461  * combination of phys_hi bits and contains the desired address.
1462  *
1463  * This version searches a ISA-style "reg" property.  It was prompted by
1464  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
1465  * 8514/A registers should have been added after all standard VGA registers.
1466  * Unfortunately, the Solaris/Intel device configuration framework
1467  * (a) lists the 8514/A registers before the video memory, and then
1468  * (b) also sorts the entries so that I/O entries come before memory
1469  *     entries.
1470  *
1471  * It returns the "reg" index and offset into that register set.
1472  * The offset is needed because there exist (broken?) BIOSes that
1473  * report larger ranges enclosing the standard ranges.  One reports
1474  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
1475  * offset adjusts for this difference in the base of the register set.
1476  *
1477  * Note that this routine may not be fully general; it is intended for the
1478  * specific purpose of finding a couple of particular VGA reg entries and
1479  * may not be suitable for all reg-searching purposes.
1480  */
1481 static int
1482 vgatext_get_isa_reg_index(
1483 	dev_info_t *const devi,
1484 	unsigned long hival,
1485 	unsigned long addr,
1486 	off_t *offset)
1487 {
1488 
1489 	int		length, index;
1490 	struct regspec	*reg;
1491 
1492 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1493 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1494 		return (-1);
1495 	}
1496 
1497 	for (index = 0; index < length / sizeof (struct regspec); index++) {
1498 		if (reg[index].regspec_bustype != hival)
1499 			continue;
1500 		if (reg[index].regspec_addr > addr)
1501 			continue;
1502 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
1503 			continue;
1504 
1505 		*offset = addr - reg[index].regspec_addr;
1506 		kmem_free(reg, (size_t)length);
1507 		return (index);
1508 	}
1509 	kmem_free(reg, (size_t)length);
1510 
1511 	return (-1);
1512 }
1513