xref: /onnv-gate/usr/src/uts/intel/io/vgatext/vgatext.c (revision 11762:91c82ea93957)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51106Smrj  * Common Development and Distribution License (the "License").
61106Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211106Smrj 
220Sstevel@tonic-gate /*
23*11762SEnrico.Perla@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
272845Srugrat /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
282845Srugrat /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
292845Srugrat /*	  All Rights Reserved  	*/
302845Srugrat 
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/visual_io.h>
360Sstevel@tonic-gate #include <sys/font.h>
370Sstevel@tonic-gate #include <sys/fbio.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/ddi.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/open.h>
440Sstevel@tonic-gate #include <sys/modctl.h>
450Sstevel@tonic-gate #include <sys/vgareg.h>
460Sstevel@tonic-gate #include <sys/vgasubr.h>
470Sstevel@tonic-gate #include <sys/pci.h>
480Sstevel@tonic-gate #include <sys/kd.h>
490Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
500Sstevel@tonic-gate #include <sys/sunldi.h>
510Sstevel@tonic-gate #include <sys/agpgart.h>
520Sstevel@tonic-gate #include <sys/agp/agpdefs.h>
530Sstevel@tonic-gate #include <sys/agp/agpmaster_io.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	MYNAME	"vgatext"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
582820Skz151634  * Each instance of this driver has 2 minor nodes:
592820Skz151634  * 0: for common graphics operations
602820Skz151634  * 1: for agpmaster operations
610Sstevel@tonic-gate  */
622820Skz151634 #define	GFX_MINOR		0
632820Skz151634 #define	AGPMASTER_MINOR		1
641661Sms148562 
652820Skz151634 #define	MY_NBITSMINOR		1
662820Skz151634 #define	DEV2INST(dev)		(getminor(dev) >> MY_NBITSMINOR)
672820Skz151634 #define	DEV2MINOR(dev)		(getminor(dev) & ((1 << MY_NBITSMINOR) - 1))
682820Skz151634 #define	INST2NODE1(inst)	((inst) << MY_NBITSMINOR + GFX_MINOR)
692820Skz151634 #define	INST2NODE2(inst)	(((inst) << MY_NBITSMINOR) + AGPMASTER_MINOR)
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* I don't know exactly where these should be defined, but this is a	*/
720Sstevel@tonic-gate /* heck of a lot better than constants in the code.			*/
730Sstevel@tonic-gate #define	TEXT_ROWS		25
740Sstevel@tonic-gate #define	TEXT_COLS		80
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #define	VGA_BRIGHT_WHITE	0x0f
770Sstevel@tonic-gate #define	VGA_BLACK		0x00
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	VGA_REG_ADDR		0x3c0
800Sstevel@tonic-gate #define	VGA_REG_SIZE		0x20
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	VGA_MEM_ADDR		0xa0000
830Sstevel@tonic-gate #define	VGA_MEM_SIZE		0x20000
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	VGA_MMAP_FB_BASE	VGA_MEM_ADDR
860Sstevel@tonic-gate 
875295Srandyf /*
885295Srandyf  * This variable allows for this driver to suspend even if it
895295Srandyf  * shouldn't.  Note that by setting it, the framebuffer will probably
905295Srandyf  * not come back.  So use it with a serial console, or with serial
915295Srandyf  * line debugging (say, for example, if this driver is being modified
925295Srandyf  * to support _some_ hardware doing suspend and resume).
935295Srandyf  */
945295Srandyf int vgatext_force_suspend = 0;
955295Srandyf 
960Sstevel@tonic-gate static int vgatext_open(dev_t *, int, int, cred_t *);
970Sstevel@tonic-gate static int vgatext_close(dev_t, int, int, cred_t *);
980Sstevel@tonic-gate static int vgatext_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
990Sstevel@tonic-gate static int vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
1000Sstevel@tonic-gate 			    size_t *, uint_t);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static 	struct cb_ops cb_vgatext_ops = {
1030Sstevel@tonic-gate 	vgatext_open,		/* cb_open */
1040Sstevel@tonic-gate 	vgatext_close,		/* cb_close */
1050Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1060Sstevel@tonic-gate 	nodev,			/* cb_print */
1070Sstevel@tonic-gate 	nodev,			/* cb_dump */
1080Sstevel@tonic-gate 	nodev,			/* cb_read */
1090Sstevel@tonic-gate 	nodev,			/* cb_write */
1100Sstevel@tonic-gate 	vgatext_ioctl,		/* cb_ioctl */
1110Sstevel@tonic-gate 	vgatext_devmap,		/* cb_devmap */
1120Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1130Sstevel@tonic-gate 	ddi_devmap_segmap,	/* cb_segmap */
1140Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1150Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1160Sstevel@tonic-gate 	0,			/* cb_stream */
1170Sstevel@tonic-gate 	D_NEW | D_MTSAFE	/* cb_flag */
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static int vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1210Sstevel@tonic-gate 		void **result);
1220Sstevel@tonic-gate static int vgatext_attach(dev_info_t *, ddi_attach_cmd_t);
1230Sstevel@tonic-gate static int vgatext_detach(dev_info_t *, ddi_detach_cmd_t);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static struct vis_identifier text_ident = { "SUNWtext" };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static struct dev_ops vgatext_ops = {
1280Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1290Sstevel@tonic-gate 	0,			/* devo_refcnt */
1300Sstevel@tonic-gate 	vgatext_info,		/* devo_getinfo */
1310Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1320Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1330Sstevel@tonic-gate 	vgatext_attach,		/* devo_attach */
1340Sstevel@tonic-gate 	vgatext_detach,		/* devo_detach */
1350Sstevel@tonic-gate 	nodev,			/* devo_reset */
1360Sstevel@tonic-gate 	&cb_vgatext_ops,	/* devo_cb_ops */
1370Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
1387656SSherry.Moore@Sun.COM 	NULL,			/* power */
1397656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,	/* quiesce */
1400Sstevel@tonic-gate };
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate struct vgatext_softc {
1430Sstevel@tonic-gate 	struct vgaregmap 	regs;
1440Sstevel@tonic-gate 	struct vgaregmap 	fb;
1450Sstevel@tonic-gate 	off_t			fb_size;
1460Sstevel@tonic-gate 	int			fb_regno;
1470Sstevel@tonic-gate 	dev_info_t		*devi;
1480Sstevel@tonic-gate 	int			mode;	/* KD_TEXT or KD_GRAPHICS */
1490Sstevel@tonic-gate 	caddr_t			text_base;	/* hardware text base */
1500Sstevel@tonic-gate 	char			shadow[TEXT_ROWS*TEXT_COLS*2];
1510Sstevel@tonic-gate 	caddr_t			current_base;	/* hardware or shadow */
1520Sstevel@tonic-gate 	struct {
1530Sstevel@tonic-gate 		boolean_t visible;
1540Sstevel@tonic-gate 		int row;
1550Sstevel@tonic-gate 		int col;
1560Sstevel@tonic-gate 	}			cursor;
1570Sstevel@tonic-gate 	struct vis_polledio	polledio;
1580Sstevel@tonic-gate 	struct {
1590Sstevel@tonic-gate 		unsigned char red;
1600Sstevel@tonic-gate 		unsigned char green;
1610Sstevel@tonic-gate 		unsigned char blue;
1620Sstevel@tonic-gate 	}			colormap[VGA8_CMAP_ENTRIES];
1630Sstevel@tonic-gate 	unsigned char attrib_palette[VGA_ATR_NUM_PLT];
1648960SJan.Setje-Eilers@Sun.COM 	agp_master_softc_t	*agp_master; /* NULL means not PCI, for AGP */
1652820Skz151634 	ddi_acc_handle_t	*pci_cfg_hdlp;	/* PCI conf handle */
1663499Srugrat 	unsigned int flags;
1678960SJan.Setje-Eilers@Sun.COM 	kmutex_t lock;
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1703499Srugrat #define	VGATEXT_FLAG_CONSOLE 0x00000001
1713499Srugrat #define	VGATEXT_IS_CONSOLE(softc) ((softc)->flags & VGATEXT_FLAG_CONSOLE)
1723499Srugrat 
1730Sstevel@tonic-gate static int vgatext_devinit(struct vgatext_softc *, struct vis_devinit *data);
1740Sstevel@tonic-gate static void	vgatext_cons_copy(struct vgatext_softc *,
1750Sstevel@tonic-gate 			struct vis_conscopy *);
1760Sstevel@tonic-gate static void	vgatext_cons_display(struct vgatext_softc *,
1770Sstevel@tonic-gate 			struct vis_consdisplay *);
1780Sstevel@tonic-gate static void	vgatext_cons_cursor(struct vgatext_softc *,
1790Sstevel@tonic-gate 			struct vis_conscursor *);
1800Sstevel@tonic-gate static void	vgatext_polled_copy(struct vis_polledio_arg *,
1810Sstevel@tonic-gate 			struct vis_conscopy *);
1820Sstevel@tonic-gate static void	vgatext_polled_display(struct vis_polledio_arg *,
1830Sstevel@tonic-gate 			struct vis_consdisplay *);
1840Sstevel@tonic-gate static void	vgatext_polled_cursor(struct vis_polledio_arg *,
1850Sstevel@tonic-gate 			struct vis_conscursor *);
1860Sstevel@tonic-gate static void	vgatext_init(struct vgatext_softc *);
1870Sstevel@tonic-gate static void	vgatext_set_text(struct vgatext_softc *);
1880Sstevel@tonic-gate #if	defined(USE_BORDERS)
1890Sstevel@tonic-gate static void	vgatext_init_graphics(struct vgatext_softc *);
1900Sstevel@tonic-gate #endif
1910Sstevel@tonic-gate static int vgatext_kdsetmode(struct vgatext_softc *softc, int mode);
1920Sstevel@tonic-gate static void vgatext_setfont(struct vgatext_softc *softc);
1930Sstevel@tonic-gate static void vgatext_get_cursor(struct vgatext_softc *softc,
1940Sstevel@tonic-gate 		screen_pos_t *row, screen_pos_t *col);
1950Sstevel@tonic-gate static void vgatext_set_cursor(struct vgatext_softc *softc, int row, int col);
1960Sstevel@tonic-gate static void vgatext_hide_cursor(struct vgatext_softc *softc);
1970Sstevel@tonic-gate static void vgatext_save_colormap(struct vgatext_softc *softc);
1980Sstevel@tonic-gate static void vgatext_restore_colormap(struct vgatext_softc *softc);
1990Sstevel@tonic-gate static int vgatext_get_pci_reg_index(dev_info_t *const devi,
2000Sstevel@tonic-gate 		unsigned long himask, unsigned long hival, unsigned long addr,
2010Sstevel@tonic-gate 		off_t *offset);
2020Sstevel@tonic-gate static int vgatext_get_isa_reg_index(dev_info_t *const devi,
2030Sstevel@tonic-gate 		unsigned long hival, unsigned long addr, off_t *offset);
2040Sstevel@tonic-gate static void	*vgatext_softc_head;
2050Sstevel@tonic-gate static char	vgatext_silent;
2060Sstevel@tonic-gate static char	happyface_boot;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /* Loadable Driver stuff */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static struct modldrv modldrv = {
2110Sstevel@tonic-gate 	&mod_driverops,		/* Type of module.  This one is a driver */
2127542SRichard.Bean@Sun.COM 	"VGA text driver",	/* Name of the module. */
2130Sstevel@tonic-gate 	&vgatext_ops,		/* driver ops */
2140Sstevel@tonic-gate };
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static struct modlinkage modlinkage = {
2170Sstevel@tonic-gate 	MODREV_1, (void *) &modldrv, NULL
2180Sstevel@tonic-gate };
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate typedef enum pc_colors {
2210Sstevel@tonic-gate 	pc_black	= 0,
2220Sstevel@tonic-gate 	pc_blue		= 1,
2230Sstevel@tonic-gate 	pc_green	= 2,
2240Sstevel@tonic-gate 	pc_cyan		= 3,
2250Sstevel@tonic-gate 	pc_red		= 4,
2260Sstevel@tonic-gate 	pc_magenta	= 5,
2270Sstevel@tonic-gate 	pc_brown	= 6,
2280Sstevel@tonic-gate 	pc_white	= 7,
2290Sstevel@tonic-gate 	pc_grey		= 8,
2300Sstevel@tonic-gate 	pc_brt_blue	= 9,
2310Sstevel@tonic-gate 	pc_brt_green	= 10,
2320Sstevel@tonic-gate 	pc_brt_cyan	= 11,
2330Sstevel@tonic-gate 	pc_brt_red	= 12,
2340Sstevel@tonic-gate 	pc_brt_magenta	= 13,
2350Sstevel@tonic-gate 	pc_yellow	= 14,
2360Sstevel@tonic-gate 	pc_brt_white	= 15
2370Sstevel@tonic-gate } pc_colors_t;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static const unsigned char solaris_color_to_pc_color[16] = {
2400Sstevel@tonic-gate 	pc_brt_white,		/*  0 - brt_white	*/
2410Sstevel@tonic-gate 	pc_black,		/*  1 - black		*/
2420Sstevel@tonic-gate 	pc_blue,		/*  2 - blue		*/
2430Sstevel@tonic-gate 	pc_green,		/*  3 - green		*/
2440Sstevel@tonic-gate 	pc_cyan,		/*  4 - cyan		*/
2450Sstevel@tonic-gate 	pc_red,			/*  5 - red		*/
2460Sstevel@tonic-gate 	pc_magenta,		/*  6 - magenta		*/
2470Sstevel@tonic-gate 	pc_brown,		/*  7 - brown		*/
2480Sstevel@tonic-gate 	pc_white,		/*  8 - white		*/
2490Sstevel@tonic-gate 	pc_grey,		/*  9 - gery		*/
2500Sstevel@tonic-gate 	pc_brt_blue,		/* 10 - brt_blue	*/
2510Sstevel@tonic-gate 	pc_brt_green,		/* 11 - brt_green	*/
2520Sstevel@tonic-gate 	pc_brt_cyan,		/* 12 - brt_cyan	*/
2530Sstevel@tonic-gate 	pc_brt_red,		/* 13 - brt_red		*/
2540Sstevel@tonic-gate 	pc_brt_magenta,		/* 14 - brt_magenta	*/
2550Sstevel@tonic-gate 	pc_yellow		/* 15 - yellow		*/
2560Sstevel@tonic-gate };
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static ddi_device_acc_attr_t i8xx_dev_access = {
2590Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2600Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2610Sstevel@tonic-gate 	DDI_STRICTORDER_ACC
2620Sstevel@tonic-gate };
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static ddi_device_acc_attr_t dev_attr = {
2650Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2660Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2670Sstevel@tonic-gate 	DDI_STRICTORDER_ACC,
2680Sstevel@tonic-gate };
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate int
_init(void)2710Sstevel@tonic-gate _init(void)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	int e;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&vgatext_softc_head,
2760Sstevel@tonic-gate 		    sizeof (struct vgatext_softc), 1)) != 0) {
2770Sstevel@tonic-gate 	    return (e);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	e = mod_install(&modlinkage);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (e) {
2835295Srandyf 		ddi_soft_state_fini(&vgatext_softc_head);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	return (e);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate int
_fini(void)2890Sstevel@tonic-gate _fini(void)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	int e;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
2945295Srandyf 		return (e);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	ddi_soft_state_fini(&vgatext_softc_head);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3020Sstevel@tonic-gate _info(struct modinfo *modinfop)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /* default structure for FBIOGATTR ioctl */
3080Sstevel@tonic-gate static struct fbgattr vgatext_attr =  {
3090Sstevel@tonic-gate /*	real_type	owner */
3100Sstevel@tonic-gate 	FBTYPE_SUNFAST_COLOR, 0,
3110Sstevel@tonic-gate /* fbtype: type		h  w  depth cms  size */
3120Sstevel@tonic-gate 	{ FBTYPE_SUNFAST_COLOR, TEXT_ROWS, TEXT_COLS, 1,    256,  0 },
3130Sstevel@tonic-gate /* fbsattr: flags emu_type	dev_specific */
3140Sstevel@tonic-gate 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
3150Sstevel@tonic-gate /*	emu_types */
3160Sstevel@tonic-gate 	{ -1 }
3170Sstevel@tonic-gate };
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * handy macros
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate #define	getsoftc(instance) ((struct vgatext_softc *)	\
3240Sstevel@tonic-gate 			ddi_get_soft_state(vgatext_softc_head, (instance)))
3250Sstevel@tonic-gate 
3263499Srugrat #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
3273499Srugrat 
328*11762SEnrico.Perla@Sun.COM /*
329*11762SEnrico.Perla@Sun.COM  * NOTE: this function is duplicated here and in gfx_private/vgatext while
330*11762SEnrico.Perla@Sun.COM  *       we work on a set of commitable interfaces to sunpci.c.
331*11762SEnrico.Perla@Sun.COM  *
332*11762SEnrico.Perla@Sun.COM  * Use the class code to determine if the device is a PCI-to-PCI bridge.
333*11762SEnrico.Perla@Sun.COM  * Returns:  B_TRUE  if the device is a bridge.
334*11762SEnrico.Perla@Sun.COM  *           B_FALSE if the device is not a bridge or the property cannot be
335*11762SEnrico.Perla@Sun.COM  *		     retrieved.
336*11762SEnrico.Perla@Sun.COM  */
337*11762SEnrico.Perla@Sun.COM static boolean_t
is_pci_bridge(dev_info_t * dip)338*11762SEnrico.Perla@Sun.COM is_pci_bridge(dev_info_t *dip)
339*11762SEnrico.Perla@Sun.COM {
340*11762SEnrico.Perla@Sun.COM 	uint32_t class_code;
341*11762SEnrico.Perla@Sun.COM 
342*11762SEnrico.Perla@Sun.COM 	class_code = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, dip,
343*11762SEnrico.Perla@Sun.COM 	    DDI_PROP_DONTPASS, "class-code", 0xffffffff);
344*11762SEnrico.Perla@Sun.COM 
345*11762SEnrico.Perla@Sun.COM 	if (class_code == 0xffffffff || class_code == DDI_PROP_NOT_FOUND)
346*11762SEnrico.Perla@Sun.COM 		return (B_FALSE);
347*11762SEnrico.Perla@Sun.COM 
348*11762SEnrico.Perla@Sun.COM 	class_code &= 0x00ffff00;
349*11762SEnrico.Perla@Sun.COM 	if (class_code == ((PCI_CLASS_BRIDGE << 16) | (PCI_BRIDGE_PCI << 8)))
350*11762SEnrico.Perla@Sun.COM 		return (B_TRUE);
351*11762SEnrico.Perla@Sun.COM 
352*11762SEnrico.Perla@Sun.COM 	return (B_FALSE);
353*11762SEnrico.Perla@Sun.COM }
354*11762SEnrico.Perla@Sun.COM 
3553499Srugrat static void
vgatext_check_for_console(dev_info_t * devi,struct vgatext_softc * softc,int pci_pcie_bus)3563499Srugrat vgatext_check_for_console(dev_info_t *devi, struct vgatext_softc *softc,
3573499Srugrat 	int pci_pcie_bus)
3583499Srugrat {
3593499Srugrat 	ddi_acc_handle_t pci_conf;
3603499Srugrat 	dev_info_t *pdevi;
3613499Srugrat 	uint16_t data16;
3623499Srugrat 
3633499Srugrat 	/*
3643499Srugrat 	 * Based on Section 11.3, "PCI Display Subsystem Initialization",
3653499Srugrat 	 * of the 1.1 PCI-to-PCI Bridge Architecture Specification
3663499Srugrat 	 * determine if this is the boot console device.  First, see
3673499Srugrat 	 * if the SBIOS has turned on PCI I/O for this device.  Then if
3683499Srugrat 	 * this is PCI/PCI-E, verify the parent bridge has VGAEnable set.
3693499Srugrat 	 */
3703499Srugrat 
3713499Srugrat 	if (pci_config_setup(devi, &pci_conf) != DDI_SUCCESS) {
3723499Srugrat 		cmn_err(CE_WARN,
3735295Srandyf 		    MYNAME ": can't get PCI conf handle");
3743499Srugrat 		return;
3753499Srugrat 	}
3763499Srugrat 
3773499Srugrat 	data16 = pci_config_get16(pci_conf, PCI_CONF_COMM);
3783499Srugrat 	if (data16 & PCI_COMM_IO)
3793499Srugrat 		softc->flags |= VGATEXT_FLAG_CONSOLE;
3803499Srugrat 
3813499Srugrat 	pci_config_teardown(&pci_conf);
3823499Srugrat 
3833499Srugrat 	/* If IO not enabled or ISA/EISA, just return */
3843499Srugrat 	if (!(softc->flags & VGATEXT_FLAG_CONSOLE) || !pci_pcie_bus)
3853499Srugrat 		return;
3863499Srugrat 
3873499Srugrat 	/*
3883499Srugrat 	 * Check for VGA Enable in the Bridge Control register for all
3893499Srugrat 	 * PCI/PCIEX parents.  If not set all the way up the chain,
3903499Srugrat 	 * this cannot be the boot console.
3913499Srugrat 	 */
3923499Srugrat 
393*11762SEnrico.Perla@Sun.COM 	pdevi = devi;
394*11762SEnrico.Perla@Sun.COM 	while (pdevi = ddi_get_parent(pdevi)) {
3953499Srugrat 		int	error;
3963499Srugrat 		ddi_acc_handle_t ppci_conf;
3973499Srugrat 		char	*parent_type = NULL;
3983499Srugrat 
3993499Srugrat 		error = ddi_prop_lookup_string(DDI_DEV_T_ANY, pdevi,
4003499Srugrat 		    DDI_PROP_DONTPASS, "device_type", &parent_type);
4013499Srugrat 		if (error != DDI_SUCCESS) {
4023499Srugrat 			return;
4033499Srugrat 		}
4043499Srugrat 
4053499Srugrat 		/* Verify still on the PCI/PCIEX parent tree */
4063499Srugrat 		if (!STREQ(parent_type, "pci") &&
4073499Srugrat 		    !STREQ(parent_type, "pciex")) {
4083499Srugrat 			ddi_prop_free(parent_type);
4093499Srugrat 			return;
4103499Srugrat 		}
4113499Srugrat 
4123499Srugrat 		ddi_prop_free(parent_type);
4133499Srugrat 		parent_type = NULL;
4143499Srugrat 
415*11762SEnrico.Perla@Sun.COM 		/* VGAEnable is set only for PCI-to-PCI bridges. */
416*11762SEnrico.Perla@Sun.COM 		if (is_pci_bridge(pdevi) == B_FALSE)
417*11762SEnrico.Perla@Sun.COM 			continue;
418*11762SEnrico.Perla@Sun.COM 
419*11762SEnrico.Perla@Sun.COM 		if (pci_config_setup(pdevi, &ppci_conf) != DDI_SUCCESS)
420*11762SEnrico.Perla@Sun.COM 			continue;
4213499Srugrat 
4223499Srugrat 		data16 = pci_config_get16(ppci_conf, PCI_BCNF_BCNTRL);
4233499Srugrat 		pci_config_teardown(&ppci_conf);
4243499Srugrat 
4253499Srugrat 		if (!(data16 & PCI_BCNF_BCNTRL_VGA_ENABLE)) {
4263499Srugrat 			softc->flags &= ~VGATEXT_FLAG_CONSOLE;
4273499Srugrat 			return;
4283499Srugrat 		}
4293499Srugrat 	}
4303499Srugrat }
4313499Srugrat 
4320Sstevel@tonic-gate static int
vgatext_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)4330Sstevel@tonic-gate vgatext_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	struct vgatext_softc *softc;
4360Sstevel@tonic-gate 	int	unit = ddi_get_instance(devi);
4370Sstevel@tonic-gate 	int	error;
4380Sstevel@tonic-gate 	char	*parent_type = NULL;
4390Sstevel@tonic-gate 	int	reg_rnumber;
4402820Skz151634 	int	agpm = 0;
4410Sstevel@tonic-gate 	off_t	reg_offset;
4420Sstevel@tonic-gate 	off_t	mem_offset;
4430Sstevel@tonic-gate 	char	buf[80], *cons;
4443499Srugrat 	int pci_pcie_bus = 0;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	switch (cmd) {
4480Sstevel@tonic-gate 	case DDI_ATTACH:
4495295Srandyf 		break;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	case DDI_RESUME:
4525295Srandyf 		/*
4535295Srandyf 		 * Though vgatext doesn't really know how to resume
4545295Srandyf 		 * on a generic framebuffer, we should succeed, as
4555295Srandyf 		 * it is far better to have no console, than potentiall
4565295Srandyf 		 * have no machine.
4575295Srandyf 		 */
4585295Srandyf 		return (DDI_SUCCESS);
4590Sstevel@tonic-gate 	default:
4605295Srandyf 		return (DDI_FAILURE);
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* DDI_ATTACH */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/* Allocate softc struct */
4660Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(vgatext_softc_head, unit) != DDI_SUCCESS) {
4670Sstevel@tonic-gate 		return (DDI_FAILURE);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 	softc = getsoftc(unit);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* link it in */
4720Sstevel@tonic-gate 	softc->devi = devi;
4730Sstevel@tonic-gate 	ddi_set_driver_private(devi, softc);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	softc->polledio.arg = (struct vis_polledio_arg *)softc;
4760Sstevel@tonic-gate 	softc->polledio.display = vgatext_polled_display;
4770Sstevel@tonic-gate 	softc->polledio.copy = vgatext_polled_copy;
4780Sstevel@tonic-gate 	softc->polledio.cursor = vgatext_polled_cursor;
4790Sstevel@tonic-gate 
4808960SJan.Setje-Eilers@Sun.COM 	mutex_init(&(softc->lock), NULL, MUTEX_DRIVER, NULL);
4818960SJan.Setje-Eilers@Sun.COM 
4820Sstevel@tonic-gate 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
4835295Srandyf 	    DDI_PROP_DONTPASS, "device_type", &parent_type);
4840Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
4850Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
4860Sstevel@tonic-gate 		goto fail;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
4900Sstevel@tonic-gate 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
4915295Srandyf 		    &reg_offset);
4920Sstevel@tonic-gate 		if (reg_rnumber < 0) {
4930Sstevel@tonic-gate 			cmn_err(CE_WARN,
4945295Srandyf 			    MYNAME ": can't find reg entry for registers");
4952297Sms148562 			error = DDI_FAILURE;
4960Sstevel@tonic-gate 			goto fail;
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_isa_reg_index(devi, 0,
4995295Srandyf 		    VGA_MEM_ADDR, &mem_offset);
5000Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
5010Sstevel@tonic-gate 			cmn_err(CE_WARN,
5025295Srandyf 			    MYNAME ": can't find reg entry for memory");
5032297Sms148562 			error = DDI_FAILURE;
5040Sstevel@tonic-gate 			goto fail;
5050Sstevel@tonic-gate 		}
506881Sjohnny 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
5073499Srugrat 		pci_pcie_bus = 1;
5080Sstevel@tonic-gate 		reg_rnumber = vgatext_get_pci_reg_index(devi,
5095295Srandyf 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
5105295Srandyf 		    PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
5115295Srandyf 		    &reg_offset);
5120Sstevel@tonic-gate 		if (reg_rnumber < 0) {
5130Sstevel@tonic-gate 			cmn_err(CE_WARN,
5145295Srandyf 			    MYNAME ": can't find reg entry for registers");
5152297Sms148562 			error = DDI_FAILURE;
5160Sstevel@tonic-gate 			goto fail;
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_pci_reg_index(devi,
5195295Srandyf 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
5205295Srandyf 		    PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
5215295Srandyf 		    &mem_offset);
5220Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
5230Sstevel@tonic-gate 			cmn_err(CE_WARN,
5245295Srandyf 			    MYNAME ": can't find reg entry for memory");
5252297Sms148562 			error = DDI_FAILURE;
5260Sstevel@tonic-gate 			goto fail;
5270Sstevel@tonic-gate 		}
5282820Skz151634 		agpm = 1;	/* should have AGP master support */
5290Sstevel@tonic-gate 	} else {
5300Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
5315295Srandyf 		    parent_type);
5322297Sms148562 		error = DDI_FAILURE;
5330Sstevel@tonic-gate 		goto fail;
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 	ddi_prop_free(parent_type);
5360Sstevel@tonic-gate 	parent_type = NULL;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, reg_rnumber,
5395295Srandyf 	    (caddr_t *)&softc->regs.addr, reg_offset, VGA_REG_SIZE,
5405295Srandyf 	    &dev_attr, &softc->regs.handle);
5410Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5420Sstevel@tonic-gate 		goto fail;
5430Sstevel@tonic-gate 	softc->regs.mapped = B_TRUE;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	softc->fb_size = VGA_MEM_SIZE;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, softc->fb_regno,
5485295Srandyf 	    (caddr_t *)&softc->fb.addr,
5495295Srandyf 	    mem_offset, softc->fb_size,
5505295Srandyf 	    &dev_attr, &softc->fb.handle);
5510Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5520Sstevel@tonic-gate 		goto fail;
5530Sstevel@tonic-gate 	softc->fb.mapped = B_TRUE;
5540Sstevel@tonic-gate 
5551106Smrj 	if (ddi_get8(softc->regs.handle,
5560Sstevel@tonic-gate 	    softc->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
5570Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_COLOR_BASE;
5580Sstevel@tonic-gate 	else
5590Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_MONO_BASE;
5608960SJan.Setje-Eilers@Sun.COM 
5618960SJan.Setje-Eilers@Sun.COM 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
5628960SJan.Setje-Eilers@Sun.COM 	    DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
5638960SJan.Setje-Eilers@Sun.COM 		if (strcmp(cons, "graphics") == 0) {
5648960SJan.Setje-Eilers@Sun.COM 			happyface_boot = 1;
5658960SJan.Setje-Eilers@Sun.COM 			vgatext_silent = 1;
5668960SJan.Setje-Eilers@Sun.COM 			softc->current_base = softc->shadow;
5678960SJan.Setje-Eilers@Sun.COM 		} else {
5688960SJan.Setje-Eilers@Sun.COM 			softc->current_base = softc->text_base;
5698960SJan.Setje-Eilers@Sun.COM 		}
5708960SJan.Setje-Eilers@Sun.COM 		ddi_prop_free(cons);
5718960SJan.Setje-Eilers@Sun.COM 	} else {
5728960SJan.Setje-Eilers@Sun.COM 		softc->current_base = softc->text_base;
5738960SJan.Setje-Eilers@Sun.COM 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	(void) sprintf(buf, "text-%d", unit);
5760Sstevel@tonic-gate 	error = ddi_create_minor_node(devi, buf, S_IFCHR,
5770Sstevel@tonic-gate 	    INST2NODE1(unit), DDI_NT_DISPLAY, NULL);
5780Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5790Sstevel@tonic-gate 		goto fail;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
5820Sstevel@tonic-gate 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
5830Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
5840Sstevel@tonic-gate 		goto fail;
5850Sstevel@tonic-gate 
5863499Srugrat 	vgatext_check_for_console(devi, softc, pci_pcie_bus);
5873499Srugrat 
5880Sstevel@tonic-gate 	/* only do this if not in graphics mode */
5893499Srugrat 	if ((vgatext_silent == 0) && (VGATEXT_IS_CONSOLE(softc))) {
5900Sstevel@tonic-gate 		vgatext_init(softc);
5910Sstevel@tonic-gate 		vgatext_save_colormap(softc);
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5942820Skz151634 	if (agpm != 0) { /* try AGP master attach */
5952820Skz151634 		/* setup mapping for PCI config space access */
5962820Skz151634 		softc->pci_cfg_hdlp = (ddi_acc_handle_t *)
5972820Skz151634 		    kmem_zalloc(sizeof (ddi_acc_handle_t), KM_SLEEP);
5982820Skz151634 		error = pci_config_setup(devi, softc->pci_cfg_hdlp);
5992820Skz151634 		if (error != DDI_SUCCESS) {
6002820Skz151634 			cmn_err(CE_WARN, "vgatext_attach: "
6012820Skz151634 			    "PCI configuration space setup failed");
6022820Skz151634 			goto fail;
6032820Skz151634 		}
6040Sstevel@tonic-gate 
6052820Skz151634 		(void) agpmaster_attach(softc->devi, &softc->agp_master,
6062820Skz151634 		    *softc->pci_cfg_hdlp, INST2NODE2(unit));
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	return (DDI_SUCCESS);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate fail:
6120Sstevel@tonic-gate 	if (parent_type != NULL)
6130Sstevel@tonic-gate 		ddi_prop_free(parent_type);
6140Sstevel@tonic-gate 	(void) vgatext_detach(devi, DDI_DETACH);
6150Sstevel@tonic-gate 	return (error);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate static int
vgatext_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)6190Sstevel@tonic-gate vgatext_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
6220Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(instance);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	switch (cmd) {
6260Sstevel@tonic-gate 	case DDI_DETACH:
6270Sstevel@tonic-gate 		if (softc->agp_master != NULL) { /* agp initiated */
6282820Skz151634 			agpmaster_detach(&softc->agp_master);
6292820Skz151634 			pci_config_teardown(softc->pci_cfg_hdlp);
6300Sstevel@tonic-gate 		}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (softc->fb.mapped)
6330Sstevel@tonic-gate 			ddi_regs_map_free(&softc->fb.handle);
6340Sstevel@tonic-gate 		if (softc->regs.mapped)
6350Sstevel@tonic-gate 			ddi_regs_map_free(&softc->regs.handle);
6368960SJan.Setje-Eilers@Sun.COM 		mutex_destroy(&(softc->lock));
6370Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
6380Sstevel@tonic-gate 		(void) ddi_soft_state_free(vgatext_softc_head, instance);
6390Sstevel@tonic-gate 		return (DDI_SUCCESS);
6400Sstevel@tonic-gate 
6415295Srandyf 	case DDI_SUSPEND:
6425295Srandyf 		/*
6435295Srandyf 		 * This is a generic VGA file, and therefore, cannot
6445295Srandyf 		 * understand how to deal with suspend and resume on
6455295Srandyf 		 * a generic interface.  So we fail any attempt to
6465295Srandyf 		 * suspend.  At some point in the future, we might use
6475295Srandyf 		 * this as an entrypoint for display drivers and this
6485295Srandyf 		 * assumption may change.
6495295Srandyf 		 *
6505295Srandyf 		 * However, from a platform development perspective,
6515295Srandyf 		 * it is important that this driver suspend if a
6525295Srandyf 		 * developer is using a serial console and/or working
6535295Srandyf 		 * on a framebuffer driver that will support suspend
6545295Srandyf 		 * and resume.  Therefore, we have this module tunable
6555295Srandyf 		 * (purposely using a long name) that will allow for
6565295Srandyf 		 * suspend it it is set.  Otherwise we fail.
6575295Srandyf 		 */
6585295Srandyf 		if (vgatext_force_suspend != 0)
6595295Srandyf 			return (DDI_SUCCESS);
6605295Srandyf 		else
6615295Srandyf 			return (DDI_FAILURE);
6625295Srandyf 
6630Sstevel@tonic-gate 	default:
6640Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext_detach: unknown cmd 0x%x\n", cmd);
6650Sstevel@tonic-gate 		return (DDI_FAILURE);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*ARGSUSED*/
6700Sstevel@tonic-gate static int
vgatext_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6710Sstevel@tonic-gate vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	dev_t dev;
6740Sstevel@tonic-gate 	int error;
6750Sstevel@tonic-gate 	int instance;
6760Sstevel@tonic-gate 	struct vgatext_softc *softc;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	error = DDI_SUCCESS;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	dev = (dev_t)arg;
6810Sstevel@tonic-gate 	instance = DEV2INST(dev);
6820Sstevel@tonic-gate 	softc = getsoftc(instance);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	switch (infocmd) {
6850Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6860Sstevel@tonic-gate 		if (softc == NULL || softc->devi == NULL) {
6870Sstevel@tonic-gate 			error = DDI_FAILURE;
6880Sstevel@tonic-gate 		} else {
6890Sstevel@tonic-gate 			*result = (void *) softc->devi;
6900Sstevel@tonic-gate 			error = DDI_SUCCESS;
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		break;
6930Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6940Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
6950Sstevel@tonic-gate 		error = DDI_SUCCESS;
6960Sstevel@tonic-gate 		break;
6970Sstevel@tonic-gate 	default:
6980Sstevel@tonic-gate 		error = DDI_FAILURE;
6990Sstevel@tonic-gate 		break;
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 	return (error);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*ARGSUSED*/
7060Sstevel@tonic-gate static int
vgatext_open(dev_t * devp,int flag,int otyp,cred_t * cred)7070Sstevel@tonic-gate vgatext_open(dev_t *devp, int flag, int otyp, cred_t *cred)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(DEV2INST(*devp));
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (softc == NULL || otyp == OTYP_BLK)
7120Sstevel@tonic-gate 		return (ENXIO);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	return (0);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /*ARGSUSED*/
7180Sstevel@tonic-gate static int
vgatext_close(dev_t devp,int flag,int otyp,cred_t * cred)7190Sstevel@tonic-gate vgatext_close(dev_t devp, int flag, int otyp, cred_t *cred)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	return (0);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate static int
do_gfx_ioctl(int cmd,intptr_t data,int mode,struct vgatext_softc * softc)7252820Skz151634 do_gfx_ioctl(int cmd, intptr_t data, int mode, struct vgatext_softc *softc)
7260Sstevel@tonic-gate {
7272820Skz151634 	static char kernel_only[] =
7282820Skz151634 	    "do_gfx_ioctl: %s is a kernel only ioctl";
7290Sstevel@tonic-gate 	int err;
7300Sstevel@tonic-gate 	int kd_mode;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	switch (cmd) {
7330Sstevel@tonic-gate 	case KDSETMODE:
7340Sstevel@tonic-gate 		return (vgatext_kdsetmode(softc, (int)data));
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	case KDGETMODE:
7370Sstevel@tonic-gate 		kd_mode = softc->mode;
7380Sstevel@tonic-gate 		if (ddi_copyout(&kd_mode, (void *)data, sizeof (int), mode))
7390Sstevel@tonic-gate 			return (EFAULT);
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	case VIS_GETIDENTIFIER:
7430Sstevel@tonic-gate 		if (ddi_copyout(&text_ident, (void *)data,
7440Sstevel@tonic-gate 		    sizeof (struct vis_identifier), mode))
7450Sstevel@tonic-gate 			return (EFAULT);
7460Sstevel@tonic-gate 		break;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	case VIS_DEVINIT:
7490Sstevel@tonic-gate 
7505295Srandyf 		if (!(mode & FKIOCTL)) {
7515295Srandyf 			cmn_err(CE_CONT, kernel_only, "VIS_DEVINIT");
7525295Srandyf 			return (ENXIO);
7535295Srandyf 		}
7540Sstevel@tonic-gate 
7555295Srandyf 		err = vgatext_devinit(softc, (struct vis_devinit *)data);
7565295Srandyf 		if (err != 0) {
7575295Srandyf 			cmn_err(CE_WARN,
7585295Srandyf 			    "vgatext_ioctl:  could not initialize console");
7595295Srandyf 			return (err);
7605295Srandyf 		}
7615295Srandyf 		break;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	case VIS_CONSCOPY:	/* move */
7645295Srandyf 		{
7655295Srandyf 		struct vis_conscopy pma;
7660Sstevel@tonic-gate 
7675295Srandyf 		if (ddi_copyin((void *)data, &pma,
7685295Srandyf 		    sizeof (struct vis_conscopy), mode))
7695295Srandyf 			return (EFAULT);
7700Sstevel@tonic-gate 
7715295Srandyf 		vgatext_cons_copy(softc, &pma);
7725295Srandyf 		break;
7735295Srandyf 		}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	case VIS_CONSDISPLAY:	/* display */
7765295Srandyf 		{
7775295Srandyf 		struct vis_consdisplay display_request;
7780Sstevel@tonic-gate 
7795295Srandyf 		if (ddi_copyin((void *)data, &display_request,
7805295Srandyf 		    sizeof (display_request), mode))
7815295Srandyf 			return (EFAULT);
7820Sstevel@tonic-gate 
7835295Srandyf 		vgatext_cons_display(softc, &display_request);
7845295Srandyf 		break;
7855295Srandyf 		}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	case VIS_CONSCURSOR:
7885295Srandyf 		{
7895295Srandyf 		struct vis_conscursor cursor_request;
7900Sstevel@tonic-gate 
7915295Srandyf 		if (ddi_copyin((void *)data, &cursor_request,
7925295Srandyf 		    sizeof (cursor_request), mode))
7935295Srandyf 			return (EFAULT);
7940Sstevel@tonic-gate 
7955295Srandyf 		vgatext_cons_cursor(softc, &cursor_request);
7960Sstevel@tonic-gate 
7975295Srandyf 		if (cursor_request.action == VIS_GET_CURSOR &&
7985295Srandyf 		    ddi_copyout(&cursor_request, (void *)data,
7995295Srandyf 		    sizeof (cursor_request), mode))
8005295Srandyf 			return (EFAULT);
8015295Srandyf 		break;
8025295Srandyf 		}
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	case VIS_GETCMAP:
8050Sstevel@tonic-gate 	case VIS_PUTCMAP:
8060Sstevel@tonic-gate 	case FBIOPUTCMAP:
8070Sstevel@tonic-gate 	case FBIOGETCMAP:
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * At the moment, text mode is not considered to have
8100Sstevel@tonic-gate 		 * a color map.
8110Sstevel@tonic-gate 		 */
8120Sstevel@tonic-gate 		return (EINVAL);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	case FBIOGATTR:
8150Sstevel@tonic-gate 		if (copyout(&vgatext_attr, (void *)data,
8160Sstevel@tonic-gate 		    sizeof (struct fbgattr)))
8170Sstevel@tonic-gate 			return (EFAULT);
8180Sstevel@tonic-gate 		break;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	case FBIOGTYPE:
8210Sstevel@tonic-gate 		if (copyout(&vgatext_attr.fbtype, (void *)data,
8220Sstevel@tonic-gate 		    sizeof (struct fbtype)))
8230Sstevel@tonic-gate 			return (EFAULT);
8240Sstevel@tonic-gate 		break;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	default:
8270Sstevel@tonic-gate 		return (ENXIO);
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	return (0);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8322820Skz151634 
8332820Skz151634 /*ARGSUSED*/
8342820Skz151634 static int
vgatext_ioctl(dev_t dev,int cmd,intptr_t data,int mode,cred_t * cred,int * rval)8352820Skz151634 vgatext_ioctl(
8362820Skz151634     dev_t dev,
8372820Skz151634     int cmd,
8382820Skz151634     intptr_t data,
8392820Skz151634     int mode,
8402820Skz151634     cred_t *cred,
8412820Skz151634     int *rval)
8422820Skz151634 {
8432820Skz151634 	struct vgatext_softc *softc = getsoftc(DEV2INST(dev));
8442820Skz151634 	int err;
8452820Skz151634 
8462820Skz151634 	switch (DEV2MINOR(dev)) {
8472820Skz151634 	case GFX_MINOR:
8488960SJan.Setje-Eilers@Sun.COM 		mutex_enter(&(softc->lock));
8492820Skz151634 		err = do_gfx_ioctl(cmd, data, mode, softc);
8508960SJan.Setje-Eilers@Sun.COM 		mutex_exit(&(softc->lock));
8512820Skz151634 		break;
8522820Skz151634 
8532820Skz151634 	case AGPMASTER_MINOR:
8542820Skz151634 		err = agpmaster_ioctl(dev, cmd, data, mode, cred, rval,
8552820Skz151634 		    softc->agp_master);
8562820Skz151634 		break;
8572820Skz151634 
8582820Skz151634 	default:
8592820Skz151634 		/* not a valid minor node */
8602820Skz151634 		return (EBADF);
8612820Skz151634 	}
8622820Skz151634 	return (err);
8638960SJan.Setje-Eilers@Sun.COM }
8642820Skz151634 
8658960SJan.Setje-Eilers@Sun.COM static void
vgatext_save_text(struct vgatext_softc * softc)8668960SJan.Setje-Eilers@Sun.COM vgatext_save_text(struct vgatext_softc *softc)
8678960SJan.Setje-Eilers@Sun.COM {
8688960SJan.Setje-Eilers@Sun.COM 	unsigned i;
8698960SJan.Setje-Eilers@Sun.COM 
8708960SJan.Setje-Eilers@Sun.COM 	for (i = 0; i < sizeof (softc->shadow); i++)
8718960SJan.Setje-Eilers@Sun.COM 		softc->shadow[i] = softc->current_base[i];
8728960SJan.Setje-Eilers@Sun.COM }
8738960SJan.Setje-Eilers@Sun.COM 
8748960SJan.Setje-Eilers@Sun.COM static void
vgatext_progressbar_stop()8758960SJan.Setje-Eilers@Sun.COM vgatext_progressbar_stop()
8768960SJan.Setje-Eilers@Sun.COM {
8778960SJan.Setje-Eilers@Sun.COM 	extern void progressbar_stop(void);
8788960SJan.Setje-Eilers@Sun.COM 
8798960SJan.Setje-Eilers@Sun.COM 	if (vgatext_silent == 1) {
8808960SJan.Setje-Eilers@Sun.COM 		vgatext_silent = 0;
8818960SJan.Setje-Eilers@Sun.COM 		progressbar_stop();
8828960SJan.Setje-Eilers@Sun.COM 	}
8838960SJan.Setje-Eilers@Sun.COM }
8848960SJan.Setje-Eilers@Sun.COM 
8858960SJan.Setje-Eilers@Sun.COM static void
vgatext_kdsettext(struct vgatext_softc * softc)8868960SJan.Setje-Eilers@Sun.COM vgatext_kdsettext(struct vgatext_softc *softc)
8878960SJan.Setje-Eilers@Sun.COM {
8888960SJan.Setje-Eilers@Sun.COM 	int i;
8898960SJan.Setje-Eilers@Sun.COM 
8908960SJan.Setje-Eilers@Sun.COM 	vgatext_init(softc);
8918960SJan.Setje-Eilers@Sun.COM 	for (i = 0; i < sizeof (softc->shadow); i++) {
8928960SJan.Setje-Eilers@Sun.COM 		softc->text_base[i] = softc->shadow[i];
8938960SJan.Setje-Eilers@Sun.COM 	}
8948960SJan.Setje-Eilers@Sun.COM 	softc->current_base = softc->text_base;
8958960SJan.Setje-Eilers@Sun.COM 	if (softc->cursor.visible) {
8968960SJan.Setje-Eilers@Sun.COM 		vgatext_set_cursor(softc,
8978960SJan.Setje-Eilers@Sun.COM 		    softc->cursor.row, softc->cursor.col);
8988960SJan.Setje-Eilers@Sun.COM 	}
8998960SJan.Setje-Eilers@Sun.COM 	vgatext_restore_colormap(softc);
9008960SJan.Setje-Eilers@Sun.COM }
9018960SJan.Setje-Eilers@Sun.COM 
9028960SJan.Setje-Eilers@Sun.COM static void
vgatext_kdsetgraphics(struct vgatext_softc * softc)9038960SJan.Setje-Eilers@Sun.COM vgatext_kdsetgraphics(struct vgatext_softc *softc)
9048960SJan.Setje-Eilers@Sun.COM {
9058960SJan.Setje-Eilers@Sun.COM 	vgatext_progressbar_stop();
9068960SJan.Setje-Eilers@Sun.COM 	vgatext_save_text(softc);
9078960SJan.Setje-Eilers@Sun.COM 	softc->current_base = softc->shadow;
9088960SJan.Setje-Eilers@Sun.COM #if	defined(USE_BORDERS)
9098960SJan.Setje-Eilers@Sun.COM 	vgatext_init_graphics(softc);
9108960SJan.Setje-Eilers@Sun.COM #endif
9112820Skz151634 }
9122820Skz151634 
9130Sstevel@tonic-gate static int
vgatext_kdsetmode(struct vgatext_softc * softc,int mode)9140Sstevel@tonic-gate vgatext_kdsetmode(struct vgatext_softc *softc, int mode)
9150Sstevel@tonic-gate {
9163499Srugrat 	if ((mode == softc->mode) || (!VGATEXT_IS_CONSOLE(softc)))
9170Sstevel@tonic-gate 		return (0);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	switch (mode) {
9200Sstevel@tonic-gate 	case KD_TEXT:
9218960SJan.Setje-Eilers@Sun.COM 		vgatext_kdsettext(softc);
9220Sstevel@tonic-gate 		break;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	case KD_GRAPHICS:
9258960SJan.Setje-Eilers@Sun.COM 		vgatext_kdsetgraphics(softc);
9268960SJan.Setje-Eilers@Sun.COM 		break;
9270Sstevel@tonic-gate 
9288960SJan.Setje-Eilers@Sun.COM 	case KD_RESETTEXT:
9298960SJan.Setje-Eilers@Sun.COM 		/*
9308960SJan.Setje-Eilers@Sun.COM 		 * In order to avoid racing with a starting X server,
9318960SJan.Setje-Eilers@Sun.COM 		 * this needs to be a test and set that is performed in
9328960SJan.Setje-Eilers@Sun.COM 		 * a single (softc->lock protected) ioctl into this driver.
9338960SJan.Setje-Eilers@Sun.COM 		 */
9348960SJan.Setje-Eilers@Sun.COM 		if (softc->mode == KD_TEXT && vgatext_silent == 1) {
9358960SJan.Setje-Eilers@Sun.COM 			vgatext_progressbar_stop();
9368960SJan.Setje-Eilers@Sun.COM 			vgatext_kdsettext(softc);
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	default:
9410Sstevel@tonic-gate 		return (EINVAL);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	softc->mode = mode;
9440Sstevel@tonic-gate 	return (0);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /*ARGSUSED*/
9480Sstevel@tonic-gate static int
vgatext_devmap(dev_t dev,devmap_cookie_t dhp,offset_t off,size_t len,size_t * maplen,uint_t model)9490Sstevel@tonic-gate vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
9500Sstevel@tonic-gate 		size_t *maplen, uint_t model)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	struct vgatext_softc *softc;
9530Sstevel@tonic-gate 	int err;
9540Sstevel@tonic-gate 	size_t length;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	softc = getsoftc(DEV2INST(dev));
9580Sstevel@tonic-gate 	if (softc == NULL) {
9590Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
9600Sstevel@tonic-gate 		return (-1);
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if (!(off >= VGA_MMAP_FB_BASE &&
9645295Srandyf 	    off < VGA_MMAP_FB_BASE + softc->fb_size)) {
9650Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
9660Sstevel@tonic-gate 		return (-1);
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	if (off + len > VGA_MMAP_FB_BASE + softc->fb_size)
9700Sstevel@tonic-gate 		length = VGA_MMAP_FB_BASE + softc->fb_size - off;
9710Sstevel@tonic-gate 	else
9720Sstevel@tonic-gate 		length = len;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	if ((err = devmap_devmem_setup(dhp, softc->devi, NULL, softc->fb_regno,
9755295Srandyf 	    off - VGA_MMAP_FB_BASE,
9765295Srandyf 	    length, PROT_ALL, 0, &dev_attr)) < 0) {
9770Sstevel@tonic-gate 		return (err);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	*maplen = length;
9820Sstevel@tonic-gate 	return (0);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate static int
vgatext_devinit(struct vgatext_softc * softc,struct vis_devinit * data)9870Sstevel@tonic-gate vgatext_devinit(struct vgatext_softc *softc, struct vis_devinit *data)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate 	/* initialize console instance */
9900Sstevel@tonic-gate 	data->version = VIS_CONS_REV;
9910Sstevel@tonic-gate 	data->width = TEXT_COLS;
9920Sstevel@tonic-gate 	data->height = TEXT_ROWS;
9930Sstevel@tonic-gate 	data->linebytes = TEXT_COLS;
9940Sstevel@tonic-gate 	data->depth = 4;
9950Sstevel@tonic-gate 	data->mode = VIS_TEXT;
9960Sstevel@tonic-gate 	data->polledio = &softc->polledio;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	return (0);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate /*
10020Sstevel@tonic-gate  * display a string on the screen at (row, col)
10030Sstevel@tonic-gate  *	 assume it has been cropped to fit.
10040Sstevel@tonic-gate  */
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate static void
vgatext_cons_display(struct vgatext_softc * softc,struct vis_consdisplay * da)10070Sstevel@tonic-gate vgatext_cons_display(struct vgatext_softc *softc, struct vis_consdisplay *da)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	unsigned char	*string;
10100Sstevel@tonic-gate 	int	i;
10110Sstevel@tonic-gate 	unsigned char	attr;
10120Sstevel@tonic-gate 	struct cgatext {
10130Sstevel@tonic-gate 		unsigned char ch;
10140Sstevel@tonic-gate 		unsigned char attr;
10150Sstevel@tonic-gate 	};
10160Sstevel@tonic-gate 	struct cgatext *addr;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10190Sstevel@tonic-gate 	 * Sanity checks.  This is a last-ditch effort to avoid damage
10200Sstevel@tonic-gate 	 * from brokenness or maliciousness above.
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 	if (da->row < 0 || da->row >= TEXT_ROWS ||
10230Sstevel@tonic-gate 	    da->col < 0 || da->col >= TEXT_COLS ||
10240Sstevel@tonic-gate 	    da->col + da->width > TEXT_COLS)
10250Sstevel@tonic-gate 		return;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	/*
10280Sstevel@tonic-gate 	 * To be fully general, we should copyin the data.  This is not
10290Sstevel@tonic-gate 	 * really relevant for this text-only driver, but a graphical driver
10300Sstevel@tonic-gate 	 * should support these ioctls from userland to enable simple
10310Sstevel@tonic-gate 	 * system startup graphics.
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	attr = (solaris_color_to_pc_color[da->bg_color & 0xf] << 4)
10345295Srandyf 	    | solaris_color_to_pc_color[da->fg_color & 0xf];
10350Sstevel@tonic-gate 	string = da->data;
10360Sstevel@tonic-gate 	addr = (struct cgatext *)softc->current_base
10375295Srandyf 	    +  (da->row * TEXT_COLS + da->col);
10380Sstevel@tonic-gate 	for (i = 0; i < da->width; i++) {
10390Sstevel@tonic-gate 		addr->ch = string[i];
10400Sstevel@tonic-gate 		addr->attr = attr;
10410Sstevel@tonic-gate 		addr++;
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate static void
vgatext_polled_display(struct vis_polledio_arg * arg,struct vis_consdisplay * da)10460Sstevel@tonic-gate vgatext_polled_display(
10470Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
10480Sstevel@tonic-gate 	struct vis_consdisplay *da)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	vgatext_cons_display((struct vgatext_softc *)arg, da);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate /*
10540Sstevel@tonic-gate  * screen-to-screen copy
10550Sstevel@tonic-gate  */
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate static void
vgatext_cons_copy(struct vgatext_softc * softc,struct vis_conscopy * ma)10580Sstevel@tonic-gate vgatext_cons_copy(struct vgatext_softc *softc, struct vis_conscopy *ma)
10590Sstevel@tonic-gate {
10600Sstevel@tonic-gate 	unsigned short	*from;
10610Sstevel@tonic-gate 	unsigned short	*to;
10620Sstevel@tonic-gate 	int		cnt;
10630Sstevel@tonic-gate 	screen_size_t chars_per_row;
10640Sstevel@tonic-gate 	unsigned short	*to_row_start;
10650Sstevel@tonic-gate 	unsigned short	*from_row_start;
10660Sstevel@tonic-gate 	screen_size_t	rows_to_move;
10670Sstevel@tonic-gate 	unsigned short	*base;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	/*
10700Sstevel@tonic-gate 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
10710Sstevel@tonic-gate 	 * damage caused by broken-ness or maliciousness above.
10720Sstevel@tonic-gate 	 */
10730Sstevel@tonic-gate 	if (ma->s_col < 0 || ma->s_col >= TEXT_COLS ||
10740Sstevel@tonic-gate 	    ma->s_row < 0 || ma->s_row >= TEXT_ROWS ||
10750Sstevel@tonic-gate 	    ma->e_col < 0 || ma->e_col >= TEXT_COLS ||
10760Sstevel@tonic-gate 	    ma->e_row < 0 || ma->e_row >= TEXT_ROWS ||
10770Sstevel@tonic-gate 	    ma->t_col < 0 || ma->t_col >= TEXT_COLS ||
10780Sstevel@tonic-gate 	    ma->t_row < 0 || ma->t_row >= TEXT_ROWS ||
10790Sstevel@tonic-gate 	    ma->s_col > ma->e_col ||
10800Sstevel@tonic-gate 	    ma->s_row > ma->e_row)
10810Sstevel@tonic-gate 		return;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	/*
10840Sstevel@tonic-gate 	 * Remember we're going to copy shorts because each
10850Sstevel@tonic-gate 	 * character/attribute pair is 16 bits.
10860Sstevel@tonic-gate 	 */
10870Sstevel@tonic-gate 	chars_per_row = ma->e_col - ma->s_col + 1;
10880Sstevel@tonic-gate 	rows_to_move = ma->e_row - ma->s_row + 1;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	/* More sanity checks. */
10910Sstevel@tonic-gate 	if (ma->t_row + rows_to_move > TEXT_ROWS ||
10920Sstevel@tonic-gate 	    ma->t_col + chars_per_row > TEXT_COLS)
10930Sstevel@tonic-gate 		return;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	base = (unsigned short *)softc->current_base;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	to_row_start = base + ((ma->t_row * TEXT_COLS) + ma->t_col);
10980Sstevel@tonic-gate 	from_row_start = base + ((ma->s_row * TEXT_COLS) + ma->s_col);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (to_row_start < from_row_start) {
11010Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
11020Sstevel@tonic-gate 			to = to_row_start;
11030Sstevel@tonic-gate 			from = from_row_start;
11040Sstevel@tonic-gate 			to_row_start += TEXT_COLS;
11050Sstevel@tonic-gate 			from_row_start += TEXT_COLS;
11060Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
11070Sstevel@tonic-gate 				*to++ = *from++;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 	} else {
11100Sstevel@tonic-gate 		/*
11110Sstevel@tonic-gate 		 * Offset to the end of the region and copy backwards.
11120Sstevel@tonic-gate 		 */
11130Sstevel@tonic-gate 		cnt = rows_to_move * TEXT_COLS + chars_per_row;
11140Sstevel@tonic-gate 		to_row_start += cnt;
11150Sstevel@tonic-gate 		from_row_start += cnt;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
11180Sstevel@tonic-gate 			to_row_start -= TEXT_COLS;
11190Sstevel@tonic-gate 			from_row_start -= TEXT_COLS;
11200Sstevel@tonic-gate 			to = to_row_start;
11210Sstevel@tonic-gate 			from = from_row_start;
11220Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
11230Sstevel@tonic-gate 				*--to = *--from;
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate static void
vgatext_polled_copy(struct vis_polledio_arg * arg,struct vis_conscopy * ca)11290Sstevel@tonic-gate vgatext_polled_copy(
11300Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11310Sstevel@tonic-gate 	struct vis_conscopy *ca)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	vgatext_cons_copy((struct vgatext_softc *)arg, ca);
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate static void
vgatext_cons_cursor(struct vgatext_softc * softc,struct vis_conscursor * ca)11380Sstevel@tonic-gate vgatext_cons_cursor(struct vgatext_softc *softc, struct vis_conscursor *ca)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	if (vgatext_silent)
11410Sstevel@tonic-gate 		return;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	switch (ca->action) {
11440Sstevel@tonic-gate 	case VIS_HIDE_CURSOR:
11450Sstevel@tonic-gate 		softc->cursor.visible = B_FALSE;
11460Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
11470Sstevel@tonic-gate 			vgatext_hide_cursor(softc);
11480Sstevel@tonic-gate 		break;
11490Sstevel@tonic-gate 	case VIS_DISPLAY_CURSOR:
11500Sstevel@tonic-gate 		/*
11510Sstevel@tonic-gate 		 * Sanity check.  This is a last-ditch effort to avoid
11520Sstevel@tonic-gate 		 * damage from brokenness or maliciousness above.
11530Sstevel@tonic-gate 		 */
11540Sstevel@tonic-gate 		if (ca->col < 0 || ca->col >= TEXT_COLS ||
11550Sstevel@tonic-gate 		    ca->row < 0 || ca->row >= TEXT_ROWS)
11560Sstevel@tonic-gate 			return;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 		softc->cursor.visible = B_TRUE;
11590Sstevel@tonic-gate 		softc->cursor.col = ca->col;
11600Sstevel@tonic-gate 		softc->cursor.row = ca->row;
11610Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
11620Sstevel@tonic-gate 			vgatext_set_cursor(softc, ca->row, ca->col);
11630Sstevel@tonic-gate 		break;
11640Sstevel@tonic-gate 	case VIS_GET_CURSOR:
11650Sstevel@tonic-gate 		if (softc->current_base == softc->text_base) {
11660Sstevel@tonic-gate 			vgatext_get_cursor(softc, &ca->row, &ca->col);
11670Sstevel@tonic-gate 		}
11680Sstevel@tonic-gate 		break;
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate static void
vgatext_polled_cursor(struct vis_polledio_arg * arg,struct vis_conscursor * ca)11730Sstevel@tonic-gate vgatext_polled_cursor(
11740Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11750Sstevel@tonic-gate 	struct vis_conscursor *ca)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	vgatext_cons_cursor((struct vgatext_softc *)arg, ca);
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /*ARGSUSED*/
11830Sstevel@tonic-gate static void
vgatext_hide_cursor(struct vgatext_softc * softc)11840Sstevel@tonic-gate vgatext_hide_cursor(struct vgatext_softc *softc)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	/* Nothing at present */
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate static void
vgatext_set_cursor(struct vgatext_softc * softc,int row,int col)11900Sstevel@tonic-gate vgatext_set_cursor(struct vgatext_softc *softc, int row, int col)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	short	addr;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (vgatext_silent)
11950Sstevel@tonic-gate 		return;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	addr = row * TEXT_COLS + col;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAH, addr >> 8);
12000Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAL, addr & 0xff);
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate static int vga_row, vga_col;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate static void
vgatext_get_cursor(struct vgatext_softc * softc,screen_pos_t * row,screen_pos_t * col)12060Sstevel@tonic-gate vgatext_get_cursor(struct vgatext_softc *softc,
12070Sstevel@tonic-gate     screen_pos_t *row, screen_pos_t *col)
12080Sstevel@tonic-gate {
12090Sstevel@tonic-gate 	short   addr;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	addr = (vga_get_crtc(&softc->regs, VGA_CRTC_CLAH) << 8) +
12120Sstevel@tonic-gate 	    vga_get_crtc(&softc->regs, VGA_CRTC_CLAL);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	vga_row = *row = addr / TEXT_COLS;
12150Sstevel@tonic-gate 	vga_col = *col = addr % TEXT_COLS;
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate  * This code is experimental. It's only enabled if console is
12200Sstevel@tonic-gate  * set to graphics, a preliminary implementation of happyface boot.
12210Sstevel@tonic-gate  */
12220Sstevel@tonic-gate static void
vgatext_set_text(struct vgatext_softc * softc)12230Sstevel@tonic-gate vgatext_set_text(struct vgatext_softc *softc)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	int i;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if (happyface_boot == 0)
12280Sstevel@tonic-gate 		return;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	/* we are in graphics mode, set to text 80X25 mode */
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/* set misc registers */
12330Sstevel@tonic-gate 	vga_set_reg(&softc->regs, VGA_MISC_W, VGA_MISC_TEXT);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	/* set sequencer registers */
12360Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
12375295Srandyf 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) &
12385295Srandyf 	    ~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
12390Sstevel@tonic-gate 	for (i = 1; i < NUM_SEQ_REG; i++) {
12400Sstevel@tonic-gate 		vga_set_seq(&softc->regs, i, VGA_SEQ_TEXT[i]);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
12435295Srandyf 	    (vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) |
12445295Srandyf 	    VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
12455295Srandyf 	    VGA_SEQ_RST_SYN_NO_SYNC_RESET));
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/* set crt controller registers */
12480Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_VRE,
12495295Srandyf 	    (vga_get_crtc(&softc->regs, VGA_CRTC_VRE) &
12505295Srandyf 	    ~VGA_CRTC_VRE_LOCK));
12510Sstevel@tonic-gate 	for (i = 0; i < NUM_CRTC_REG; i++) {
12520Sstevel@tonic-gate 		vga_set_crtc(&softc->regs, i, VGA_CRTC_TEXT[i]);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	/* set graphics controller registers */
12560Sstevel@tonic-gate 	for (i = 0; i < NUM_GRC_REG; i++) {
12570Sstevel@tonic-gate 		vga_set_grc(&softc->regs, i, VGA_GRC_TEXT[i]);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/* set attribute registers */
12610Sstevel@tonic-gate 	for (i = 0; i < NUM_ATR_REG; i++) {
12620Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, VGA_ATR_TEXT[i]);
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/* set palette */
12660Sstevel@tonic-gate 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
12670Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, VGA_TEXT_PALETTES[i][0] << 2,
12685295Srandyf 		    VGA_TEXT_PALETTES[i][1] << 2,
12695295Srandyf 		    VGA_TEXT_PALETTES[i][2] << 2);
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
12720Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, 0, 0, 0);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	vgatext_save_colormap(softc);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate static void
vgatext_init(struct vgatext_softc * softc)12790Sstevel@tonic-gate vgatext_init(struct vgatext_softc *softc)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	unsigned char atr_mode;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
12840Sstevel@tonic-gate 	if (atr_mode & VGA_ATR_MODE_GRAPH)
12850Sstevel@tonic-gate 		vgatext_set_text(softc);
12860Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
12870Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_BLINK;
12880Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
12890Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_MODE, atr_mode);
12900Sstevel@tonic-gate #if	defined(USE_BORDERS)
12910Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
12925295Srandyf 	    vga_get_atr(&softc->regs, VGA_BRIGHT_WHITE));
12930Sstevel@tonic-gate #else
12940Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
12955295Srandyf 	    vga_get_atr(&softc->regs, VGA_BLACK));
12960Sstevel@tonic-gate #endif
12970Sstevel@tonic-gate 	vgatext_setfont(softc);	/* need selectable font? */
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate #if	defined(USE_BORDERS)
13010Sstevel@tonic-gate static void
vgatext_init_graphics(struct vgatext_softc * softc)13020Sstevel@tonic-gate vgatext_init_graphics(struct vgatext_softc *softc)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
13055295Srandyf 	    vga_get_atr(&softc->regs, VGA_BLACK));
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate #endif
13080Sstevel@tonic-gate 
130925Sszhou static char vga_fontslot = 0;
131025Sszhou 
13110Sstevel@tonic-gate static void
vgatext_setfont(struct vgatext_softc * softc)13120Sstevel@tonic-gate vgatext_setfont(struct vgatext_softc *softc)
13130Sstevel@tonic-gate {
131425Sszhou 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
131525Sszhou 
13160Sstevel@tonic-gate 	extern unsigned char *ENCODINGS[];
131725Sszhou 	uchar_t *from;
131825Sszhou 	uchar_t volatile *to;
131925Sszhou 	int	i, j, s;
132025Sszhou 	int	bpc, f_offset;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
13230Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 *  enable write to plane2, since fonts
13260Sstevel@tonic-gate 	 * could only be loaded into plane2
13270Sstevel@tonic-gate 	 */
13280Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x04);
13290Sstevel@tonic-gate 	/*
13300Sstevel@tonic-gate 	 *  sequentially access data in the bit map being
13310Sstevel@tonic-gate 	 * selected by MapMask register (index 0x02)
13320Sstevel@tonic-gate 	 */
13330Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x07);
13340Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
13350Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	/*
13380Sstevel@tonic-gate 	 *  select plane 2 on Read Mode 0
13390Sstevel@tonic-gate 	 */
13400Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x02);
13410Sstevel@tonic-gate 	/*
13420Sstevel@tonic-gate 	 *  system addresses sequentially access data, follow
13430Sstevel@tonic-gate 	 * Memory Mode register bit 2 in the sequencer
13440Sstevel@tonic-gate 	 */
13450Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x00);
13460Sstevel@tonic-gate 	/*
13470Sstevel@tonic-gate 	 * set range of host memory addresses decoded by VGA
13480Sstevel@tonic-gate 	 * hardware -- A0000h-BFFFFh (128K region)
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x00);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * This assumes 8x16 characters, which yield the traditional 80x25
13540Sstevel@tonic-gate 	 * screen.  It really should support other character heights.
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	bpc = 16;
135725Sszhou 	s = vga_fontslot;
135825Sszhou 	f_offset = s * 8 * 1024;
13590Sstevel@tonic-gate 	for (i = 0; i < 256; i++) {
13600Sstevel@tonic-gate 		from = ENCODINGS[i];
136125Sszhou 		to = (unsigned char *)softc->fb.addr + f_offset + i * 0x20;
13620Sstevel@tonic-gate 		for (j = 0; j < bpc; j++)
13630Sstevel@tonic-gate 			*to++ = *from++;
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
13670Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
13680Sstevel@tonic-gate 	/* enable write to plane 0 and 1 */
13690Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x03);
13700Sstevel@tonic-gate 	/*
13710Sstevel@tonic-gate 	 * enable character map selection
13720Sstevel@tonic-gate 	 * and odd/even addressing
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x03);
13750Sstevel@tonic-gate 	/*
137625Sszhou 	 * select font map
13770Sstevel@tonic-gate 	 */
137825Sszhou 	vga_set_seq(&softc->regs, 0x03, fsreg[s]);
13790Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
13800Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	/* restore graphic registers */
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	/* select plane 0 */
13850Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x00);
13860Sstevel@tonic-gate 	/* enable odd/even addressing mode */
13870Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x10);
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	 * range of host memory addresses decoded by VGA
13900Sstevel@tonic-gate 	 * hardware -- B8000h-BFFFFh (32K region)
13910Sstevel@tonic-gate 	 */
13920Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x0e);
13930Sstevel@tonic-gate 	/* enable all color plane */
13940Sstevel@tonic-gate 	vga_set_atr(&softc->regs, 0x12, 0x0f);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate static void
vgatext_save_colormap(struct vgatext_softc * softc)13990Sstevel@tonic-gate vgatext_save_colormap(struct vgatext_softc *softc)
14000Sstevel@tonic-gate {
14010Sstevel@tonic-gate 	int i;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
14040Sstevel@tonic-gate 		softc->attrib_palette[i] = vga_get_atr(&softc->regs, i);
14050Sstevel@tonic-gate 	}
14060Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
14070Sstevel@tonic-gate 		vga_get_cmap(&softc->regs, i,
14085295Srandyf 		    &softc->colormap[i].red,
14095295Srandyf 		    &softc->colormap[i].green,
14105295Srandyf 		    &softc->colormap[i].blue);
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate static void
vgatext_restore_colormap(struct vgatext_softc * softc)14150Sstevel@tonic-gate vgatext_restore_colormap(struct vgatext_softc *softc)
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	int i;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
14200Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, softc->attrib_palette[i]);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
14230Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i,
14245295Srandyf 		    softc->colormap[i].red,
14255295Srandyf 		    softc->colormap[i].green,
14265295Srandyf 		    softc->colormap[i].blue);
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate }
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
14320Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
14330Sstevel@tonic-gate  *
14340Sstevel@tonic-gate  * This version searches a PCI-style "reg" property.  It was prompted by
14350Sstevel@tonic-gate  * issues surrounding the presence or absence of an entry for the ROM:
14360Sstevel@tonic-gate  * (a) a transition problem with PowerPC Virtual Open Firmware
14370Sstevel@tonic-gate  * (b) uncertainty as to whether an entry will be included on a device
14380Sstevel@tonic-gate  *     with ROM support (and so an "active" ROM base address register),
14390Sstevel@tonic-gate  *     but no ROM actually installed.
14400Sstevel@tonic-gate  *
14410Sstevel@tonic-gate  * See the note below on vgatext_get_isa_reg_index for the reasons for
14420Sstevel@tonic-gate  * returning the offset.
14430Sstevel@tonic-gate  *
14440Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
14450Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
14460Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
14470Sstevel@tonic-gate  */
14480Sstevel@tonic-gate static int
vgatext_get_pci_reg_index(dev_info_t * const devi,unsigned long himask,unsigned long hival,unsigned long addr,off_t * offset)14490Sstevel@tonic-gate vgatext_get_pci_reg_index(
14500Sstevel@tonic-gate 	dev_info_t *const devi,
14510Sstevel@tonic-gate 	unsigned long himask,
14520Sstevel@tonic-gate 	unsigned long hival,
14530Sstevel@tonic-gate 	unsigned long addr,
14540Sstevel@tonic-gate 	off_t *offset)
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	int			length, index;
14580Sstevel@tonic-gate 	pci_regspec_t	*reg;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
14615295Srandyf 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
14620Sstevel@tonic-gate 		return (-1);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
14660Sstevel@tonic-gate 		if ((reg[index].pci_phys_hi & himask) != hival)
14670Sstevel@tonic-gate 			continue;
14680Sstevel@tonic-gate 		if (reg[index].pci_size_hi != 0)
14690Sstevel@tonic-gate 			continue;
14700Sstevel@tonic-gate 		if (reg[index].pci_phys_mid != 0)
14710Sstevel@tonic-gate 			continue;
14720Sstevel@tonic-gate 		if (reg[index].pci_phys_low > addr)
14730Sstevel@tonic-gate 			continue;
14740Sstevel@tonic-gate 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
14750Sstevel@tonic-gate 			continue;
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 		*offset = addr - reg[index].pci_phys_low;
14780Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
14790Sstevel@tonic-gate 		return (index);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	return (-1);
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate /*
14870Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
14880Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
14890Sstevel@tonic-gate  *
14900Sstevel@tonic-gate  * This version searches a ISA-style "reg" property.  It was prompted by
14910Sstevel@tonic-gate  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
14920Sstevel@tonic-gate  * 8514/A registers should have been added after all standard VGA registers.
14930Sstevel@tonic-gate  * Unfortunately, the Solaris/Intel device configuration framework
14940Sstevel@tonic-gate  * (a) lists the 8514/A registers before the video memory, and then
14950Sstevel@tonic-gate  * (b) also sorts the entries so that I/O entries come before memory
14960Sstevel@tonic-gate  *     entries.
14970Sstevel@tonic-gate  *
14980Sstevel@tonic-gate  * It returns the "reg" index and offset into that register set.
14990Sstevel@tonic-gate  * The offset is needed because there exist (broken?) BIOSes that
15000Sstevel@tonic-gate  * report larger ranges enclosing the standard ranges.  One reports
15010Sstevel@tonic-gate  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
15020Sstevel@tonic-gate  * offset adjusts for this difference in the base of the register set.
15030Sstevel@tonic-gate  *
15040Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
15050Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
15060Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
15070Sstevel@tonic-gate  */
15080Sstevel@tonic-gate static int
vgatext_get_isa_reg_index(dev_info_t * const devi,unsigned long hival,unsigned long addr,off_t * offset)15090Sstevel@tonic-gate vgatext_get_isa_reg_index(
15100Sstevel@tonic-gate 	dev_info_t *const devi,
15110Sstevel@tonic-gate 	unsigned long hival,
15120Sstevel@tonic-gate 	unsigned long addr,
15130Sstevel@tonic-gate 	off_t *offset)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	int		length, index;
15170Sstevel@tonic-gate 	struct regspec	*reg;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
15205295Srandyf 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
15210Sstevel@tonic-gate 		return (-1);
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (struct regspec); index++) {
15250Sstevel@tonic-gate 		if (reg[index].regspec_bustype != hival)
15260Sstevel@tonic-gate 			continue;
15270Sstevel@tonic-gate 		if (reg[index].regspec_addr > addr)
15280Sstevel@tonic-gate 			continue;
15290Sstevel@tonic-gate 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
15300Sstevel@tonic-gate 			continue;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 		*offset = addr - reg[index].regspec_addr;
15330Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
15340Sstevel@tonic-gate 		return (index);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	return (-1);
15390Sstevel@tonic-gate }
1540