xref: /illumos-gate/usr/src/uts/intel/io/vgatext/vgatext.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54ab75253Smrj  * Common Development and Distribution License (the "License").
64ab75253Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
214ab75253Smrj 
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
237c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
247c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*d6bb6a84Sms148562  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/visual_io.h>
387c478bd9Sstevel@tonic-gate #include <sys/font.h>
397c478bd9Sstevel@tonic-gate #include <sys/fbio.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/open.h>
467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/vgareg.h>
487c478bd9Sstevel@tonic-gate #include <sys/vgasubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/pci.h>
507c478bd9Sstevel@tonic-gate #include <sys/kd.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunldi.h>
537c478bd9Sstevel@tonic-gate #include <sys/agpgart.h>
547c478bd9Sstevel@tonic-gate #include <sys/agp/agpdefs.h>
557c478bd9Sstevel@tonic-gate #include <sys/agp/agpmaster_io.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	MYNAME	"vgatext"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * agp support macros
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	I8XX_MMIO_REGSET	2
637c478bd9Sstevel@tonic-gate #define	I8XX_FB_REGSET		1
647c478bd9Sstevel@tonic-gate #define	I8XX_PTE_OFFSET		0x10000
657c478bd9Sstevel@tonic-gate #define	I8XX_PGTBL_CTL		0x2020
66*d6bb6a84Sms148562 #define	I915_GTTADDR_BAR	4
67*d6bb6a84Sms148562 #define	I915_FB_REGSET		3
68*d6bb6a84Sms148562 
69*d6bb6a84Sms148562 #define	IS_IGD(agp_master) ((agp_master->agpm_dev_type == DEVICE_IS_I810) || \
70*d6bb6a84Sms148562 		    (agp_master->agpm_dev_type == DEVICE_IS_I830))
71*d6bb6a84Sms148562 
727c478bd9Sstevel@tonic-gate #define	DEV2INST(dev)		(getminor(dev) >> 1)
737c478bd9Sstevel@tonic-gate #define	INST2NODE1(inst)	((inst) << 1)
747c478bd9Sstevel@tonic-gate #define	INST2NODE2(inst)	(((inst) << 1) + 1)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* I don't know exactly where these should be defined, but this is a	*/
777c478bd9Sstevel@tonic-gate /* heck of a lot better than constants in the code.			*/
787c478bd9Sstevel@tonic-gate #define	TEXT_ROWS		25
797c478bd9Sstevel@tonic-gate #define	TEXT_COLS		80
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	VGA_BRIGHT_WHITE	0x0f
827c478bd9Sstevel@tonic-gate #define	VGA_BLACK		0x00
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	VGA_REG_ADDR		0x3c0
857c478bd9Sstevel@tonic-gate #define	VGA_REG_SIZE		0x20
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	VGA_MEM_ADDR		0xa0000
887c478bd9Sstevel@tonic-gate #define	VGA_MEM_SIZE		0x20000
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	VGA_MMAP_FB_BASE	VGA_MEM_ADDR
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static int vgatext_open(dev_t *, int, int, cred_t *);
937c478bd9Sstevel@tonic-gate static int vgatext_close(dev_t, int, int, cred_t *);
947c478bd9Sstevel@tonic-gate static int vgatext_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
957c478bd9Sstevel@tonic-gate static int vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
967c478bd9Sstevel@tonic-gate 			    size_t *, uint_t);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static 	struct cb_ops cb_vgatext_ops = {
997c478bd9Sstevel@tonic-gate 	vgatext_open,		/* cb_open */
1007c478bd9Sstevel@tonic-gate 	vgatext_close,		/* cb_close */
1017c478bd9Sstevel@tonic-gate 	nodev,			/* cb_strategy */
1027c478bd9Sstevel@tonic-gate 	nodev,			/* cb_print */
1037c478bd9Sstevel@tonic-gate 	nodev,			/* cb_dump */
1047c478bd9Sstevel@tonic-gate 	nodev,			/* cb_read */
1057c478bd9Sstevel@tonic-gate 	nodev,			/* cb_write */
1067c478bd9Sstevel@tonic-gate 	vgatext_ioctl,		/* cb_ioctl */
1077c478bd9Sstevel@tonic-gate 	vgatext_devmap,		/* cb_devmap */
1087c478bd9Sstevel@tonic-gate 	nodev,			/* cb_mmap */
1097c478bd9Sstevel@tonic-gate 	ddi_devmap_segmap,	/* cb_segmap */
1107c478bd9Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
1117c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1127c478bd9Sstevel@tonic-gate 	0,			/* cb_stream */
1137c478bd9Sstevel@tonic-gate 	D_NEW | D_MTSAFE	/* cb_flag */
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static int vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1187c478bd9Sstevel@tonic-gate 		void **result);
1197c478bd9Sstevel@tonic-gate static int vgatext_attach(dev_info_t *, ddi_attach_cmd_t);
1207c478bd9Sstevel@tonic-gate static int vgatext_detach(dev_info_t *, ddi_detach_cmd_t);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static struct vis_identifier text_ident = { "SUNWtext" };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static struct dev_ops vgatext_ops = {
1257c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1267c478bd9Sstevel@tonic-gate 	0,			/* devo_refcnt */
1277c478bd9Sstevel@tonic-gate 	vgatext_info,		/* devo_getinfo */
1287c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_identify */
1297c478bd9Sstevel@tonic-gate 	nulldev,		/* devo_probe */
1307c478bd9Sstevel@tonic-gate 	vgatext_attach,		/* devo_attach */
1317c478bd9Sstevel@tonic-gate 	vgatext_detach,		/* devo_detach */
1327c478bd9Sstevel@tonic-gate 	nodev,			/* devo_reset */
1337c478bd9Sstevel@tonic-gate 	&cb_vgatext_ops,	/* devo_cb_ops */
1347c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,	/* devo_bus_ops */
1357c478bd9Sstevel@tonic-gate 	NULL			/* power */
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * agp support data structures
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate typedef struct gtt_impl {
1437c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t	gtt_mmio_handle; /* mmaped graph registers */
1447c478bd9Sstevel@tonic-gate 	caddr_t			gtt_mmio_base; /* pointer to register base */
1457c478bd9Sstevel@tonic-gate 	caddr_t			gtt_addr; /* pointer to gtt */
1467c478bd9Sstevel@tonic-gate 	igd_info_t		gtt_info; /* for I8XX_GET_INFO ioctl */
1477c478bd9Sstevel@tonic-gate } gtt_impl_t;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate typedef struct agp_master_softc {
1507c478bd9Sstevel@tonic-gate 	uint32_t		agpm_id; /* agp master device id */
1517c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t	agpm_acc_hdl; /* agp master pci conf handle */
1527c478bd9Sstevel@tonic-gate 	int			agpm_dev_type; /* which agp device type */
1537c478bd9Sstevel@tonic-gate 	union {
1547c478bd9Sstevel@tonic-gate 		off_t		agpm_acaptr; /* AGP capability reg pointer */
1557c478bd9Sstevel@tonic-gate 		gtt_impl_t	agpm_gtt; /* for gtt table */
1567c478bd9Sstevel@tonic-gate 	} agpm_data;
1577c478bd9Sstevel@tonic-gate } agp_master_softc_t;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate struct vgatext_softc {
1627c478bd9Sstevel@tonic-gate 	struct vgaregmap 	regs;
1637c478bd9Sstevel@tonic-gate 	struct vgaregmap 	fb;
1647c478bd9Sstevel@tonic-gate 	off_t			fb_size;
1657c478bd9Sstevel@tonic-gate 	int			fb_regno;
1667c478bd9Sstevel@tonic-gate 	dev_info_t		*devi;
1677c478bd9Sstevel@tonic-gate 	int			mode;	/* KD_TEXT or KD_GRAPHICS */
1687c478bd9Sstevel@tonic-gate 	caddr_t			text_base;	/* hardware text base */
1697c478bd9Sstevel@tonic-gate 	char			shadow[TEXT_ROWS*TEXT_COLS*2];
1707c478bd9Sstevel@tonic-gate 	caddr_t			current_base;	/* hardware or shadow */
1717c478bd9Sstevel@tonic-gate 	struct {
1727c478bd9Sstevel@tonic-gate 		boolean_t visible;
1737c478bd9Sstevel@tonic-gate 		int row;
1747c478bd9Sstevel@tonic-gate 		int col;
1757c478bd9Sstevel@tonic-gate 	}			cursor;
1767c478bd9Sstevel@tonic-gate 	struct vis_polledio	polledio;
1777c478bd9Sstevel@tonic-gate 	struct {
1787c478bd9Sstevel@tonic-gate 		unsigned char red;
1797c478bd9Sstevel@tonic-gate 		unsigned char green;
1807c478bd9Sstevel@tonic-gate 		unsigned char blue;
1817c478bd9Sstevel@tonic-gate 	}			colormap[VGA8_CMAP_ENTRIES];
1827c478bd9Sstevel@tonic-gate 	unsigned char attrib_palette[VGA_ATR_NUM_PLT];
1837c478bd9Sstevel@tonic-gate 	agp_master_softc_t	*agp_master; /* NULL mean not PCI, for AGP */
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static int vgatext_devinit(struct vgatext_softc *, struct vis_devinit *data);
1877c478bd9Sstevel@tonic-gate static void	vgatext_cons_copy(struct vgatext_softc *,
1887c478bd9Sstevel@tonic-gate 			struct vis_conscopy *);
1897c478bd9Sstevel@tonic-gate static void	vgatext_cons_display(struct vgatext_softc *,
1907c478bd9Sstevel@tonic-gate 			struct vis_consdisplay *);
1917c478bd9Sstevel@tonic-gate static void	vgatext_cons_cursor(struct vgatext_softc *,
1927c478bd9Sstevel@tonic-gate 			struct vis_conscursor *);
1937c478bd9Sstevel@tonic-gate static void	vgatext_polled_copy(struct vis_polledio_arg *,
1947c478bd9Sstevel@tonic-gate 			struct vis_conscopy *);
1957c478bd9Sstevel@tonic-gate static void	vgatext_polled_display(struct vis_polledio_arg *,
1967c478bd9Sstevel@tonic-gate 			struct vis_consdisplay *);
1977c478bd9Sstevel@tonic-gate static void	vgatext_polled_cursor(struct vis_polledio_arg *,
1987c478bd9Sstevel@tonic-gate 			struct vis_conscursor *);
1997c478bd9Sstevel@tonic-gate static void	vgatext_init(struct vgatext_softc *);
2007c478bd9Sstevel@tonic-gate static void	vgatext_set_text(struct vgatext_softc *);
2017c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
2027c478bd9Sstevel@tonic-gate static void	vgatext_init_graphics(struct vgatext_softc *);
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate static int vgatext_kdsetmode(struct vgatext_softc *softc, int mode);
2057c478bd9Sstevel@tonic-gate static void vgatext_setfont(struct vgatext_softc *softc);
2067c478bd9Sstevel@tonic-gate static void vgatext_get_cursor(struct vgatext_softc *softc,
2077c478bd9Sstevel@tonic-gate 		screen_pos_t *row, screen_pos_t *col);
2087c478bd9Sstevel@tonic-gate static void vgatext_set_cursor(struct vgatext_softc *softc, int row, int col);
2097c478bd9Sstevel@tonic-gate static void vgatext_hide_cursor(struct vgatext_softc *softc);
2107c478bd9Sstevel@tonic-gate static void vgatext_save_colormap(struct vgatext_softc *softc);
2117c478bd9Sstevel@tonic-gate static void vgatext_restore_colormap(struct vgatext_softc *softc);
2127c478bd9Sstevel@tonic-gate static int vgatext_get_pci_reg_index(dev_info_t *const devi,
2137c478bd9Sstevel@tonic-gate 		unsigned long himask, unsigned long hival, unsigned long addr,
2147c478bd9Sstevel@tonic-gate 		off_t *offset);
2157c478bd9Sstevel@tonic-gate static int vgatext_get_isa_reg_index(dev_info_t *const devi,
2167c478bd9Sstevel@tonic-gate 		unsigned long hival, unsigned long addr, off_t *offset);
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * agp support functions prototype
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate static off_t agp_master_cap_find(ddi_acc_handle_t);
2217c478bd9Sstevel@tonic-gate static int detect_i8xx_device(agp_master_softc_t *);
2227c478bd9Sstevel@tonic-gate static int detect_agp_devcice(agp_master_softc_t *);
2237c478bd9Sstevel@tonic-gate static int agp_master_init(struct vgatext_softc *);
2247c478bd9Sstevel@tonic-gate static void agp_master_end(agp_master_softc_t *);
2257c478bd9Sstevel@tonic-gate static int phys2entry(uint32_t, uint32_t, uint32_t *);
2267c478bd9Sstevel@tonic-gate static int i8xx_add_to_gtt(gtt_impl_t *, igd_gtt_seg_t);
2277c478bd9Sstevel@tonic-gate static void i8xx_remove_from_gtt(gtt_impl_t *, igd_gtt_seg_t);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void	*vgatext_softc_head;
2307c478bd9Sstevel@tonic-gate static char	vgatext_silent;
2317c478bd9Sstevel@tonic-gate static char	happyface_boot;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /* Loadable Driver stuff */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2367c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module.  This one is a driver */
2377c478bd9Sstevel@tonic-gate 	"VGA text driver v%I%",	/* Name of the module. */
2387c478bd9Sstevel@tonic-gate 	&vgatext_ops,		/* driver ops */
2397c478bd9Sstevel@tonic-gate };
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2427c478bd9Sstevel@tonic-gate 	MODREV_1, (void *) &modldrv, NULL
2437c478bd9Sstevel@tonic-gate };
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate typedef enum pc_colors {
2467c478bd9Sstevel@tonic-gate 	pc_black	= 0,
2477c478bd9Sstevel@tonic-gate 	pc_blue		= 1,
2487c478bd9Sstevel@tonic-gate 	pc_green	= 2,
2497c478bd9Sstevel@tonic-gate 	pc_cyan		= 3,
2507c478bd9Sstevel@tonic-gate 	pc_red		= 4,
2517c478bd9Sstevel@tonic-gate 	pc_magenta	= 5,
2527c478bd9Sstevel@tonic-gate 	pc_brown	= 6,
2537c478bd9Sstevel@tonic-gate 	pc_white	= 7,
2547c478bd9Sstevel@tonic-gate 	pc_grey		= 8,
2557c478bd9Sstevel@tonic-gate 	pc_brt_blue	= 9,
2567c478bd9Sstevel@tonic-gate 	pc_brt_green	= 10,
2577c478bd9Sstevel@tonic-gate 	pc_brt_cyan	= 11,
2587c478bd9Sstevel@tonic-gate 	pc_brt_red	= 12,
2597c478bd9Sstevel@tonic-gate 	pc_brt_magenta	= 13,
2607c478bd9Sstevel@tonic-gate 	pc_yellow	= 14,
2617c478bd9Sstevel@tonic-gate 	pc_brt_white	= 15
2627c478bd9Sstevel@tonic-gate } pc_colors_t;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static const unsigned char solaris_color_to_pc_color[16] = {
2657c478bd9Sstevel@tonic-gate 	pc_brt_white,		/*  0 - brt_white	*/
2667c478bd9Sstevel@tonic-gate 	pc_black,		/*  1 - black		*/
2677c478bd9Sstevel@tonic-gate 	pc_blue,		/*  2 - blue		*/
2687c478bd9Sstevel@tonic-gate 	pc_green,		/*  3 - green		*/
2697c478bd9Sstevel@tonic-gate 	pc_cyan,		/*  4 - cyan		*/
2707c478bd9Sstevel@tonic-gate 	pc_red,			/*  5 - red		*/
2717c478bd9Sstevel@tonic-gate 	pc_magenta,		/*  6 - magenta		*/
2727c478bd9Sstevel@tonic-gate 	pc_brown,		/*  7 - brown		*/
2737c478bd9Sstevel@tonic-gate 	pc_white,		/*  8 - white		*/
2747c478bd9Sstevel@tonic-gate 	pc_grey,		/*  9 - gery		*/
2757c478bd9Sstevel@tonic-gate 	pc_brt_blue,		/* 10 - brt_blue	*/
2767c478bd9Sstevel@tonic-gate 	pc_brt_green,		/* 11 - brt_green	*/
2777c478bd9Sstevel@tonic-gate 	pc_brt_cyan,		/* 12 - brt_cyan	*/
2787c478bd9Sstevel@tonic-gate 	pc_brt_red,		/* 13 - brt_red		*/
2797c478bd9Sstevel@tonic-gate 	pc_brt_magenta,		/* 14 - brt_magenta	*/
2807c478bd9Sstevel@tonic-gate 	pc_yellow		/* 15 - yellow		*/
2817c478bd9Sstevel@tonic-gate };
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static ddi_device_acc_attr_t i8xx_dev_access = {
2847c478bd9Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2857c478bd9Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2867c478bd9Sstevel@tonic-gate 	DDI_STRICTORDER_ACC
2877c478bd9Sstevel@tonic-gate };
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static ddi_device_acc_attr_t dev_attr = {
2907c478bd9Sstevel@tonic-gate 	DDI_DEVICE_ATTR_V0,
2917c478bd9Sstevel@tonic-gate 	DDI_NEVERSWAP_ACC,
2927c478bd9Sstevel@tonic-gate 	DDI_STRICTORDER_ACC,
2937c478bd9Sstevel@tonic-gate };
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate int
2967c478bd9Sstevel@tonic-gate _init(void)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int e;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&vgatext_softc_head,
3017c478bd9Sstevel@tonic-gate 		    sizeof (struct vgatext_softc), 1)) != 0) {
3027c478bd9Sstevel@tonic-gate 	    return (e);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	e = mod_install(&modlinkage);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (e) {
3087c478bd9Sstevel@tonic-gate 	    ddi_soft_state_fini(&vgatext_softc_head);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	return (e);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate int
3147c478bd9Sstevel@tonic-gate _fini(void)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int e;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
3197c478bd9Sstevel@tonic-gate 	    return (e);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&vgatext_softc_head);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate int
3277c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /* default structure for FBIOGATTR ioctl */
3337c478bd9Sstevel@tonic-gate static struct fbgattr vgatext_attr =  {
3347c478bd9Sstevel@tonic-gate /*	real_type	owner */
3357c478bd9Sstevel@tonic-gate 	FBTYPE_SUNFAST_COLOR, 0,
3367c478bd9Sstevel@tonic-gate /* fbtype: type		h  w  depth cms  size */
3377c478bd9Sstevel@tonic-gate 	{ FBTYPE_SUNFAST_COLOR, TEXT_ROWS, TEXT_COLS, 1,    256,  0 },
3387c478bd9Sstevel@tonic-gate /* fbsattr: flags emu_type	dev_specific */
3397c478bd9Sstevel@tonic-gate 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
3407c478bd9Sstevel@tonic-gate /*	emu_types */
3417c478bd9Sstevel@tonic-gate 	{ -1 }
3427c478bd9Sstevel@tonic-gate };
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * handy macros
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #define	getsoftc(instance) ((struct vgatext_softc *)	\
3497c478bd9Sstevel@tonic-gate 			ddi_get_soft_state(vgatext_softc_head, (instance)))
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static int
3527c478bd9Sstevel@tonic-gate vgatext_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
3557c478bd9Sstevel@tonic-gate 	int	unit = ddi_get_instance(devi);
3567c478bd9Sstevel@tonic-gate 	int	error;
3577c478bd9Sstevel@tonic-gate 	char	*parent_type = NULL;
3587c478bd9Sstevel@tonic-gate 	int	reg_rnumber;
3597c478bd9Sstevel@tonic-gate 	off_t	reg_offset;
3607c478bd9Sstevel@tonic-gate 	off_t	mem_offset;
3617c478bd9Sstevel@tonic-gate 	char	buf[80], *cons;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	switch (cmd) {
3657c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
3667c478bd9Sstevel@tonic-gate 	    break;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3697c478bd9Sstevel@tonic-gate 	    return (DDI_SUCCESS);
3707c478bd9Sstevel@tonic-gate 	default:
3717c478bd9Sstevel@tonic-gate 	    return (DDI_FAILURE);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/* DDI_ATTACH */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* Allocate softc struct */
3777c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(vgatext_softc_head, unit) != DDI_SUCCESS) {
3787c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	softc = getsoftc(unit);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* link it in */
3837c478bd9Sstevel@tonic-gate 	softc->devi = devi;
3847c478bd9Sstevel@tonic-gate 	ddi_set_driver_private(devi, softc);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	softc->polledio.arg = (struct vis_polledio_arg *)softc;
3877c478bd9Sstevel@tonic-gate 	softc->polledio.display = vgatext_polled_display;
3887c478bd9Sstevel@tonic-gate 	softc->polledio.copy = vgatext_polled_copy;
3897c478bd9Sstevel@tonic-gate 	softc->polledio.cursor = vgatext_polled_cursor;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
3927c478bd9Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "device_type", &parent_type);
3937c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS) {
3947c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
3957c478bd9Sstevel@tonic-gate 		goto fail;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
3997c478bd9Sstevel@tonic-gate 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
4007c478bd9Sstevel@tonic-gate 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
4017c478bd9Sstevel@tonic-gate 			&reg_offset);
4027c478bd9Sstevel@tonic-gate 		if (reg_rnumber < 0) {
4037c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4047c478bd9Sstevel@tonic-gate 				MYNAME ": can't find reg entry for registers");
4057c478bd9Sstevel@tonic-gate 			goto fail;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_isa_reg_index(devi, 0,
4087c478bd9Sstevel@tonic-gate 			VGA_MEM_ADDR, &mem_offset);
4097c478bd9Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
4107c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4117c478bd9Sstevel@tonic-gate 				MYNAME ": can't find reg entry for memory");
4127c478bd9Sstevel@tonic-gate 			goto fail;
4137c478bd9Sstevel@tonic-gate 		}
41470025d76Sjohnny 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
4157c478bd9Sstevel@tonic-gate 		reg_rnumber = vgatext_get_pci_reg_index(devi,
4167c478bd9Sstevel@tonic-gate 			PCI_REG_ADDR_M|PCI_REG_REL_M,
4177c478bd9Sstevel@tonic-gate 			PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
4187c478bd9Sstevel@tonic-gate 			&reg_offset);
4197c478bd9Sstevel@tonic-gate 		if (reg_rnumber < 0) {
4207c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4217c478bd9Sstevel@tonic-gate 				MYNAME ": can't find reg entry for registers");
4227c478bd9Sstevel@tonic-gate 			goto fail;
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		softc->fb_regno = vgatext_get_pci_reg_index(devi,
4257c478bd9Sstevel@tonic-gate 			PCI_REG_ADDR_M|PCI_REG_REL_M,
4267c478bd9Sstevel@tonic-gate 			PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
4277c478bd9Sstevel@tonic-gate 			&mem_offset);
4287c478bd9Sstevel@tonic-gate 		if (softc->fb_regno < 0) {
4297c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
4307c478bd9Sstevel@tonic-gate 				MYNAME ": can't find reg entry for memory");
4317c478bd9Sstevel@tonic-gate 			goto fail;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 		softc->agp_master = (agp_master_softc_t *)
4347c478bd9Sstevel@tonic-gate 		    kmem_zalloc(sizeof (agp_master_softc_t), KM_SLEEP);
4357c478bd9Sstevel@tonic-gate 	} else {
4367c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
4377c478bd9Sstevel@tonic-gate 			parent_type);
4387c478bd9Sstevel@tonic-gate 		goto fail;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	ddi_prop_free(parent_type);
4417c478bd9Sstevel@tonic-gate 	parent_type = NULL;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, reg_rnumber,
4447c478bd9Sstevel@tonic-gate 		(caddr_t *)&softc->regs.addr, reg_offset, VGA_REG_SIZE,
4457c478bd9Sstevel@tonic-gate 		&dev_attr, &softc->regs.handle);
4467c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4477c478bd9Sstevel@tonic-gate 		goto fail;
4487c478bd9Sstevel@tonic-gate 	softc->regs.mapped = B_TRUE;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	softc->fb_size = VGA_MEM_SIZE;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	error = ddi_regs_map_setup(devi, softc->fb_regno,
4537c478bd9Sstevel@tonic-gate 		(caddr_t *)&softc->fb.addr,
4547c478bd9Sstevel@tonic-gate 		mem_offset, softc->fb_size,
4557c478bd9Sstevel@tonic-gate 		&dev_attr, &softc->fb.handle);
4567c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4577c478bd9Sstevel@tonic-gate 		goto fail;
4587c478bd9Sstevel@tonic-gate 	softc->fb.mapped = B_TRUE;
4597c478bd9Sstevel@tonic-gate 
4604ab75253Smrj 	if (ddi_get8(softc->regs.handle,
4617c478bd9Sstevel@tonic-gate 	    softc->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
4627c478bd9Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_COLOR_BASE;
4637c478bd9Sstevel@tonic-gate 	else
4647c478bd9Sstevel@tonic-gate 		softc->text_base = (caddr_t)softc->fb.addr + VGA_MONO_BASE;
4657c478bd9Sstevel@tonic-gate 	softc->current_base = softc->text_base;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "text-%d", unit);
4687c478bd9Sstevel@tonic-gate 	error = ddi_create_minor_node(devi, buf, S_IFCHR,
4697c478bd9Sstevel@tonic-gate 	    INST2NODE1(unit), DDI_NT_DISPLAY, NULL);
4707c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4717c478bd9Sstevel@tonic-gate 		goto fail;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
4747c478bd9Sstevel@tonic-gate 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
4757c478bd9Sstevel@tonic-gate 	if (error != DDI_SUCCESS)
4767c478bd9Sstevel@tonic-gate 		goto fail;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
4797c478bd9Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
4807c478bd9Sstevel@tonic-gate 		if (strcmp(cons, "graphics") == 0) {
4817c478bd9Sstevel@tonic-gate 			happyface_boot = 1;
4827c478bd9Sstevel@tonic-gate 			vgatext_silent = 1;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		ddi_prop_free(cons);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* only do this if not in graphics mode */
4887c478bd9Sstevel@tonic-gate 	if (vgatext_silent == 0) {
4897c478bd9Sstevel@tonic-gate 		vgatext_init(softc);
4907c478bd9Sstevel@tonic-gate 		vgatext_save_colormap(softc);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (softc->agp_master != NULL) { /* is PCI */
4947c478bd9Sstevel@tonic-gate 		if (agp_master_init(softc) != 0) { /* unsuccessful */
4957c478bd9Sstevel@tonic-gate 			kmem_free(softc->agp_master,
4967c478bd9Sstevel@tonic-gate 			    sizeof (agp_master_softc_t));
4977c478bd9Sstevel@tonic-gate 			softc->agp_master = NULL;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate fail:
5057c478bd9Sstevel@tonic-gate 	if (parent_type != NULL)
5067c478bd9Sstevel@tonic-gate 		ddi_prop_free(parent_type);
5077c478bd9Sstevel@tonic-gate 	(void) vgatext_detach(devi, DDI_DETACH);
5087c478bd9Sstevel@tonic-gate 	return (error);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate static int
5127c478bd9Sstevel@tonic-gate vgatext_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(devi);
5157c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(instance);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	switch (cmd) {
5197c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
5207c478bd9Sstevel@tonic-gate 		if (softc->agp_master != NULL) { /* agp initiated */
5217c478bd9Sstevel@tonic-gate 			agp_master_end(softc->agp_master);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 			kmem_free(softc->agp_master,
5247c478bd9Sstevel@tonic-gate 			    sizeof (agp_master_softc_t));
5257c478bd9Sstevel@tonic-gate 			softc->agp_master = NULL;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		if (softc->fb.mapped)
5307c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&softc->fb.handle);
5317c478bd9Sstevel@tonic-gate 		if (softc->regs.mapped)
5327c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(&softc->regs.handle);
5337c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
5347c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_free(vgatext_softc_head, instance);
5357c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	default:
5387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext_detach: unknown cmd 0x%x\n", cmd);
5397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5447c478bd9Sstevel@tonic-gate static int
5457c478bd9Sstevel@tonic-gate vgatext_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	dev_t dev;
5487c478bd9Sstevel@tonic-gate 	int error;
5497c478bd9Sstevel@tonic-gate 	int instance;
5507c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	error = DDI_SUCCESS;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	dev = (dev_t)arg;
5557c478bd9Sstevel@tonic-gate 	instance = DEV2INST(dev);
5567c478bd9Sstevel@tonic-gate 	softc = getsoftc(instance);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	switch (infocmd) {
5597c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
5607c478bd9Sstevel@tonic-gate 		if (softc == NULL || softc->devi == NULL) {
5617c478bd9Sstevel@tonic-gate 			error = DDI_FAILURE;
5627c478bd9Sstevel@tonic-gate 		} else {
5637c478bd9Sstevel@tonic-gate 			*result = (void *) softc->devi;
5647c478bd9Sstevel@tonic-gate 			error = DDI_SUCCESS;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 		break;
5677c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
5687c478bd9Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
5697c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
5707c478bd9Sstevel@tonic-gate 		break;
5717c478bd9Sstevel@tonic-gate 	default:
5727c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
5737c478bd9Sstevel@tonic-gate 		break;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 	return (error);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5807c478bd9Sstevel@tonic-gate static int
5817c478bd9Sstevel@tonic-gate vgatext_open(dev_t *devp, int flag, int otyp, cred_t *cred)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(DEV2INST(*devp));
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (softc == NULL || otyp == OTYP_BLK)
5867c478bd9Sstevel@tonic-gate 		return (ENXIO);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	return (0);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5927c478bd9Sstevel@tonic-gate static int
5937c478bd9Sstevel@tonic-gate vgatext_close(dev_t devp, int flag, int otyp, cred_t *cred)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	return (0);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5997c478bd9Sstevel@tonic-gate static int
6007c478bd9Sstevel@tonic-gate vgatext_ioctl(
6017c478bd9Sstevel@tonic-gate     dev_t dev,
6027c478bd9Sstevel@tonic-gate     int cmd,
6037c478bd9Sstevel@tonic-gate     intptr_t data,
6047c478bd9Sstevel@tonic-gate     int mode,
6057c478bd9Sstevel@tonic-gate     cred_t *cred,
6067c478bd9Sstevel@tonic-gate     int *rval)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc = getsoftc(DEV2INST(dev));
6097c478bd9Sstevel@tonic-gate 	static char kernel_only[] = "vgatext_ioctl: %s is a kernel only ioctl";
6107c478bd9Sstevel@tonic-gate 	int err;
6117c478bd9Sstevel@tonic-gate 	int kd_mode;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	switch (cmd) {
6147c478bd9Sstevel@tonic-gate 	case KDSETMODE:
6157c478bd9Sstevel@tonic-gate 		return (vgatext_kdsetmode(softc, (int)data));
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	case KDGETMODE:
6187c478bd9Sstevel@tonic-gate 		kd_mode = softc->mode;
6197c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&kd_mode, (void *)data, sizeof (int), mode))
6207c478bd9Sstevel@tonic-gate 			return (EFAULT);
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	case VIS_GETIDENTIFIER:
6247c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&text_ident, (void *)data,
6257c478bd9Sstevel@tonic-gate 		    sizeof (struct vis_identifier), mode))
6267c478bd9Sstevel@tonic-gate 			return (EFAULT);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	case VIS_DEVINIT:
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	    if (!(mode & FKIOCTL)) {
6327c478bd9Sstevel@tonic-gate 		    cmn_err(CE_CONT, kernel_only, "VIS_DEVINIT");
6337c478bd9Sstevel@tonic-gate 		    return (ENXIO);
6347c478bd9Sstevel@tonic-gate 	    }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	    err = vgatext_devinit(softc, (struct vis_devinit *)data);
6377c478bd9Sstevel@tonic-gate 	    if (err != 0) {
6387c478bd9Sstevel@tonic-gate 		    cmn_err(CE_WARN,
6397c478bd9Sstevel@tonic-gate 			"vgatext_ioctl:  could not initialize console");
6407c478bd9Sstevel@tonic-gate 		    return (err);
6417c478bd9Sstevel@tonic-gate 	    }
6427c478bd9Sstevel@tonic-gate 	    break;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	case VIS_CONSCOPY:	/* move */
6457c478bd9Sstevel@tonic-gate 	{
6467c478bd9Sstevel@tonic-gate 	    struct vis_conscopy pma;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	    if (ddi_copyin((void *)data, &pma,
6497c478bd9Sstevel@tonic-gate 		sizeof (struct vis_conscopy), mode))
6507c478bd9Sstevel@tonic-gate 		    return (EFAULT);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	    vgatext_cons_copy(softc, &pma);
6537c478bd9Sstevel@tonic-gate 	    break;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	case VIS_CONSDISPLAY:	/* display */
6577c478bd9Sstevel@tonic-gate 	{
6587c478bd9Sstevel@tonic-gate 	    struct vis_consdisplay display_request;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	    if (ddi_copyin((void *)data, &display_request,
6617c478bd9Sstevel@tonic-gate 		sizeof (display_request), mode))
6627c478bd9Sstevel@tonic-gate 		    return (EFAULT);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	    vgatext_cons_display(softc, &display_request);
6657c478bd9Sstevel@tonic-gate 	    break;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	case VIS_CONSCURSOR:
6697c478bd9Sstevel@tonic-gate 	{
6707c478bd9Sstevel@tonic-gate 	    struct vis_conscursor cursor_request;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	    if (ddi_copyin((void *)data, &cursor_request,
6737c478bd9Sstevel@tonic-gate 		sizeof (cursor_request), mode))
6747c478bd9Sstevel@tonic-gate 		    return (EFAULT);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	    vgatext_cons_cursor(softc, &cursor_request);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	    if (cursor_request.action == VIS_GET_CURSOR &&
6797c478bd9Sstevel@tonic-gate 		ddi_copyout(&cursor_request, (void *)data,
6807c478bd9Sstevel@tonic-gate 		sizeof (cursor_request), mode))
6817c478bd9Sstevel@tonic-gate 		    return (EFAULT);
6827c478bd9Sstevel@tonic-gate 	    break;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	case VIS_GETCMAP:
6867c478bd9Sstevel@tonic-gate 	case VIS_PUTCMAP:
6877c478bd9Sstevel@tonic-gate 	case FBIOPUTCMAP:
6887c478bd9Sstevel@tonic-gate 	case FBIOGETCMAP:
6897c478bd9Sstevel@tonic-gate 		/*
6907c478bd9Sstevel@tonic-gate 		 * At the moment, text mode is not considered to have
6917c478bd9Sstevel@tonic-gate 		 * a color map.
6927c478bd9Sstevel@tonic-gate 		 */
6937c478bd9Sstevel@tonic-gate 		return (EINVAL);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	case FBIOGATTR:
6967c478bd9Sstevel@tonic-gate 		if (copyout(&vgatext_attr, (void *)data,
6977c478bd9Sstevel@tonic-gate 		    sizeof (struct fbgattr)))
6987c478bd9Sstevel@tonic-gate 			return (EFAULT);
6997c478bd9Sstevel@tonic-gate 		break;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	case FBIOGTYPE:
7027c478bd9Sstevel@tonic-gate 		if (copyout(&vgatext_attr.fbtype, (void *)data,
7037c478bd9Sstevel@tonic-gate 		    sizeof (struct fbtype)))
7047c478bd9Sstevel@tonic-gate 			return (EFAULT);
7057c478bd9Sstevel@tonic-gate 		break;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate #if 0
7087c478bd9Sstevel@tonic-gate 	case GLY_LD_GLYPH:
7097c478bd9Sstevel@tonic-gate 	{
7107c478bd9Sstevel@tonic-gate 		temstat_t	*ap;
7117c478bd9Sstevel@tonic-gate 		da_t	da;
7127c478bd9Sstevel@tonic-gate 		struct glyph	g;
7137c478bd9Sstevel@tonic-gate 		int	size;
7147c478bd9Sstevel@tonic-gate 		uchar_t	*c;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		if (ddi_copyin(arg, &g, sizeof (g), flag))
7177c478bd9Sstevel@tonic-gate 			return (EFAULT);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		size = g.width * g.height;
7207c478bd9Sstevel@tonic-gate 		c = kmem_alloc(size, KM_SLEEP);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		if (ddi_copyin(g.raster, c, size, flag)) {
7237c478bd9Sstevel@tonic-gate 			kmem_free(c, size);
7247c478bd9Sstevel@tonic-gate 			return (EFAULT);
7257c478bd9Sstevel@tonic-gate 		}
7267c478bd9Sstevel@tonic-gate 		ap = (temstat_t *)something;
7277c478bd9Sstevel@tonic-gate 		da.data = c;
7287c478bd9Sstevel@tonic-gate 		da.width = g.width;
7297c478bd9Sstevel@tonic-gate 		da.height = g.height;
7307c478bd9Sstevel@tonic-gate 		da.col = g.x_dest;
7317c478bd9Sstevel@tonic-gate 		da.row = g.y_dest;
7327c478bd9Sstevel@tonic-gate 		ap->a_fp.f_ad_display(ap->a_private, &da);
7337c478bd9Sstevel@tonic-gate 		kmem_free(c, size);
7347c478bd9Sstevel@tonic-gate 		return (0);
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate #endif
7377c478bd9Sstevel@tonic-gate 	case DEVICE_DETECT:
7387c478bd9Sstevel@tonic-gate 	{
7397c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
7427c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "DEVICE_DETECT");
7437c478bd9Sstevel@tonic-gate 			return (ENXIO);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
7467c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		if (!agp_master)
7497c478bd9Sstevel@tonic-gate 			return (EINVAL);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&agp_master->agpm_dev_type,
7527c478bd9Sstevel@tonic-gate 		    (void *)data, sizeof (int), mode))
7537c478bd9Sstevel@tonic-gate 			return (EFAULT);
7547c478bd9Sstevel@tonic-gate 		break;
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	case I8XX_GET_INFO:
7577c478bd9Sstevel@tonic-gate 	{
7587c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
7617c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "I8XX_GET_INFO");
7627c478bd9Sstevel@tonic-gate 			return (ENXIO);
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
7657c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		if (!agp_master)
7687c478bd9Sstevel@tonic-gate 			return (EINVAL);
769*d6bb6a84Sms148562 		ASSERT(IS_IGD(agp_master));
7707c478bd9Sstevel@tonic-gate 
771*d6bb6a84Sms148562 		if (!IS_IGD(agp_master))
7727c478bd9Sstevel@tonic-gate 			return (EINVAL);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&agp_master->agpm_data.agpm_gtt.gtt_info,
775*d6bb6a84Sms148562 		    (void *)data, sizeof (igd_info_t), mode))
7767c478bd9Sstevel@tonic-gate 			return (EFAULT);
7777c478bd9Sstevel@tonic-gate 		break;
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	case I810_SET_GTT_BASE:
7807c478bd9Sstevel@tonic-gate 	{
7817c478bd9Sstevel@tonic-gate 		uint32_t base;
7827c478bd9Sstevel@tonic-gate 		uint32_t addr;
7837c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
7867c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "I8XX_SET_GTT_ADDR");
7877c478bd9Sstevel@tonic-gate 			return (ENXIO);
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
7907c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
7917c478bd9Sstevel@tonic-gate 		if (!agp_master)
7927c478bd9Sstevel@tonic-gate 			return (EINVAL);
7937c478bd9Sstevel@tonic-gate 		ASSERT(agp_master->agpm_dev_type == DEVICE_IS_I810);
794*d6bb6a84Sms148562 
7957c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_dev_type != DEVICE_IS_I810)
7967c478bd9Sstevel@tonic-gate 			return (EINVAL);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &base, sizeof (uint32_t), mode))
7997c478bd9Sstevel@tonic-gate 			return (EFAULT);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		/* enables page table */
8027c478bd9Sstevel@tonic-gate 		addr = (base & GTT_BASE_MASK) | GTT_TABLE_VALID;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		ddi_put32(agp_master->agpm_data.agpm_gtt.gtt_mmio_handle,
8057c478bd9Sstevel@tonic-gate 		    (uint32_t *)(agp_master->agpm_data.agpm_gtt.gtt_mmio_base +
8067c478bd9Sstevel@tonic-gate 		    I8XX_PGTBL_CTL),
8077c478bd9Sstevel@tonic-gate 		    addr);
8087c478bd9Sstevel@tonic-gate 		break;
8097c478bd9Sstevel@tonic-gate 	}
8107c478bd9Sstevel@tonic-gate 	case I8XX_ADD2GTT:
8117c478bd9Sstevel@tonic-gate 	{
8127c478bd9Sstevel@tonic-gate 		igd_gtt_seg_t seg;
8137c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
8167c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "I8XX_ADD2GTT");
8177c478bd9Sstevel@tonic-gate 			return (ENXIO);
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
8207c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
8217c478bd9Sstevel@tonic-gate 		if (!agp_master)
8227c478bd9Sstevel@tonic-gate 			return (EINVAL);
823*d6bb6a84Sms148562 		ASSERT(IS_IGD(agp_master));
8247c478bd9Sstevel@tonic-gate 
825*d6bb6a84Sms148562 		if (!IS_IGD(agp_master))
8267c478bd9Sstevel@tonic-gate 			return (EINVAL);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &seg,
8297c478bd9Sstevel@tonic-gate 		    sizeof (igd_gtt_seg_t), mode))
8307c478bd9Sstevel@tonic-gate 			return (EFAULT);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if (i8xx_add_to_gtt(&agp_master->agpm_data.agpm_gtt, seg))
8337c478bd9Sstevel@tonic-gate 			return (EINVAL);
8347c478bd9Sstevel@tonic-gate 		break;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	case I8XX_REM_GTT:
8377c478bd9Sstevel@tonic-gate 	{
8387c478bd9Sstevel@tonic-gate 		igd_gtt_seg_t seg;
8397c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
8427c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "I8XX_REM_GTT");
8437c478bd9Sstevel@tonic-gate 			return (ENXIO);
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
8467c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
8477c478bd9Sstevel@tonic-gate 		if (!agp_master)
8487c478bd9Sstevel@tonic-gate 			return (EINVAL);
849*d6bb6a84Sms148562 		ASSERT(IS_IGD(agp_master));
8507c478bd9Sstevel@tonic-gate 
851*d6bb6a84Sms148562 		if (!IS_IGD(agp_master))
8527c478bd9Sstevel@tonic-gate 			return (EINVAL);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &seg,
8557c478bd9Sstevel@tonic-gate 		    sizeof (igd_gtt_seg_t), mode))
8567c478bd9Sstevel@tonic-gate 			return (EFAULT);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		i8xx_remove_from_gtt(&agp_master->agpm_data.agpm_gtt, seg);
8597c478bd9Sstevel@tonic-gate 		break;
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 	case I8XX_UNCONFIG:
8627c478bd9Sstevel@tonic-gate 	{
8637c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
8667c478bd9Sstevel@tonic-gate 		    cmn_err(CE_CONT, kernel_only, "I8XX_UNCONFIG");
8677c478bd9Sstevel@tonic-gate 		    return (ENXIO);
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
8707c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
8717c478bd9Sstevel@tonic-gate 		if (!agp_master)
8727c478bd9Sstevel@tonic-gate 			return (EINVAL);
873*d6bb6a84Sms148562 		ASSERT(IS_IGD(agp_master));
8747c478bd9Sstevel@tonic-gate 
875*d6bb6a84Sms148562 		if (!IS_IGD(agp_master))
8767c478bd9Sstevel@tonic-gate 			return (EINVAL);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_dev_type == DEVICE_IS_I810)
8797c478bd9Sstevel@tonic-gate 			ddi_put32(
8807c478bd9Sstevel@tonic-gate 			    agp_master->agpm_data.agpm_gtt.gtt_mmio_handle,
8817c478bd9Sstevel@tonic-gate 			    (uint32_t *)
8827c478bd9Sstevel@tonic-gate 			    (agp_master->agpm_data.agpm_gtt.gtt_mmio_base +
8837c478bd9Sstevel@tonic-gate 			    I8XX_PGTBL_CTL),
8847c478bd9Sstevel@tonic-gate 			    0);
8857c478bd9Sstevel@tonic-gate 		/*
8867c478bd9Sstevel@tonic-gate 		 * may clear all gtt entries here for i830,
8877c478bd9Sstevel@tonic-gate 		 * but may not be necessary
8887c478bd9Sstevel@tonic-gate 		 */
8897c478bd9Sstevel@tonic-gate 		break;
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	case AGP_MASTER_GETINFO:
8927c478bd9Sstevel@tonic-gate 	{
8937c478bd9Sstevel@tonic-gate 		agp_info_t info;
8947c478bd9Sstevel@tonic-gate 		uint32_t value;
8957c478bd9Sstevel@tonic-gate 		off_t cap;
8967c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
8997c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "AGP_MASTER_GETINFO");
9007c478bd9Sstevel@tonic-gate 			return (ENXIO);
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
9037c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
9047c478bd9Sstevel@tonic-gate 		if (!agp_master)
9057c478bd9Sstevel@tonic-gate 			return (EINVAL);
9067c478bd9Sstevel@tonic-gate 		ASSERT(agp_master->agpm_dev_type == DEVICE_IS_AGP);
9077c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_dev_type != DEVICE_IS_AGP)
9087c478bd9Sstevel@tonic-gate 			return (EINVAL);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		ASSERT(agp_master->agpm_data.agpm_acaptr);
9117c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_data.agpm_acaptr == 0)
9127c478bd9Sstevel@tonic-gate 			return (EINVAL);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		cap = agp_master->agpm_data.agpm_acaptr;
9157c478bd9Sstevel@tonic-gate 		value = pci_config_get32(agp_master->agpm_acc_hdl, cap);
9167c478bd9Sstevel@tonic-gate 		info.agpi_version.agpv_major = (uint16_t)((value >> 20) & 0xf);
9177c478bd9Sstevel@tonic-gate 		info.agpi_version.agpv_minor = (uint16_t)((value >> 16) & 0xf);
9187c478bd9Sstevel@tonic-gate 		info.agpi_devid = agp_master->agpm_id;
9197c478bd9Sstevel@tonic-gate 		info.agpi_mode = pci_config_get32(
9207c478bd9Sstevel@tonic-gate 		    agp_master->agpm_acc_hdl, cap + AGP_CONF_STATUS);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&info, (void *)data,
9237c478bd9Sstevel@tonic-gate 		    sizeof (agp_info_t), mode))
9247c478bd9Sstevel@tonic-gate 			return (EFAULT);
9257c478bd9Sstevel@tonic-gate 		break;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	case AGP_MASTER_SETCMD:
9287c478bd9Sstevel@tonic-gate 	{
9297c478bd9Sstevel@tonic-gate 		uint32_t command;
9307c478bd9Sstevel@tonic-gate 		agp_master_softc_t *agp_master;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		if (!(mode & FKIOCTL)) {
9337c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, kernel_only, "AGP_MASTER_SETCMD");
9347c478bd9Sstevel@tonic-gate 			return (ENXIO);
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 		agp_master = softc->agp_master;
9377c478bd9Sstevel@tonic-gate 		ASSERT(agp_master);
9387c478bd9Sstevel@tonic-gate 		if (!agp_master)
9397c478bd9Sstevel@tonic-gate 			return (EINVAL);
9407c478bd9Sstevel@tonic-gate 		ASSERT(agp_master->agpm_dev_type == DEVICE_IS_AGP);
9417c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_dev_type != DEVICE_IS_AGP)
9427c478bd9Sstevel@tonic-gate 			return (EINVAL);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		ASSERT(agp_master->agpm_data.agpm_acaptr);
9457c478bd9Sstevel@tonic-gate 		if (agp_master->agpm_data.agpm_acaptr == 0)
9467c478bd9Sstevel@tonic-gate 			return (EINVAL);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		if (ddi_copyin((void *)data, &command,
9497c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t), mode))
9507c478bd9Sstevel@tonic-gate 			return (EFAULT);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		pci_config_put32(agp_master->agpm_acc_hdl,
9537c478bd9Sstevel@tonic-gate 		    agp_master->agpm_data.agpm_acaptr + AGP_CONF_COMMAND,
9547c478bd9Sstevel@tonic-gate 		    command);
9557c478bd9Sstevel@tonic-gate 		break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	default:
9597c478bd9Sstevel@tonic-gate 		return (ENXIO);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	return (0);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate static int
9657c478bd9Sstevel@tonic-gate vgatext_kdsetmode(struct vgatext_softc *softc, int mode)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	int i;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (mode == softc->mode)
9707c478bd9Sstevel@tonic-gate 		return (0);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	switch (mode) {
9737c478bd9Sstevel@tonic-gate 	case KD_TEXT:
9747c478bd9Sstevel@tonic-gate 		vgatext_init(softc);
9757c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (softc->shadow); i++) {
9767c478bd9Sstevel@tonic-gate 			softc->text_base[i] = softc->shadow[i];
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		softc->current_base = softc->text_base;
9797c478bd9Sstevel@tonic-gate 		if (softc->cursor.visible) {
9807c478bd9Sstevel@tonic-gate 			vgatext_set_cursor(softc,
9817c478bd9Sstevel@tonic-gate 				softc->cursor.row, softc->cursor.col);
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 		vgatext_restore_colormap(softc);
9847c478bd9Sstevel@tonic-gate 		break;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	case KD_GRAPHICS:
9877c478bd9Sstevel@tonic-gate 		if (vgatext_silent == 1) {
9887c478bd9Sstevel@tonic-gate 			extern void progressbar_stop(void);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 			vgatext_silent = 0;
9917c478bd9Sstevel@tonic-gate 			progressbar_stop();
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (softc->shadow); i++) {
9947c478bd9Sstevel@tonic-gate 			softc->shadow[i] = softc->text_base[i];
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		softc->current_base = softc->shadow;
9977c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
9987c478bd9Sstevel@tonic-gate 		vgatext_init_graphics(softc);
9997c478bd9Sstevel@tonic-gate #endif
10007c478bd9Sstevel@tonic-gate 		break;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	default:
10037c478bd9Sstevel@tonic-gate 		return (EINVAL);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 	softc->mode = mode;
10067c478bd9Sstevel@tonic-gate 	return (0);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10107c478bd9Sstevel@tonic-gate static int
10117c478bd9Sstevel@tonic-gate vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
10127c478bd9Sstevel@tonic-gate 		size_t *maplen, uint_t model)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	struct vgatext_softc *softc;
10157c478bd9Sstevel@tonic-gate 	int err;
10167c478bd9Sstevel@tonic-gate 	size_t length;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	softc = getsoftc(DEV2INST(dev));
10207c478bd9Sstevel@tonic-gate 	if (softc == NULL) {
10217c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
10227c478bd9Sstevel@tonic-gate 		return (-1);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (!(off >= VGA_MMAP_FB_BASE &&
10267c478bd9Sstevel@tonic-gate 		off < VGA_MMAP_FB_BASE + softc->fb_size)) {
10277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
10287c478bd9Sstevel@tonic-gate 		return (-1);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (off + len > VGA_MMAP_FB_BASE + softc->fb_size)
10327c478bd9Sstevel@tonic-gate 		length = VGA_MMAP_FB_BASE + softc->fb_size - off;
10337c478bd9Sstevel@tonic-gate 	else
10347c478bd9Sstevel@tonic-gate 		length = len;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if ((err = devmap_devmem_setup(dhp, softc->devi, NULL, softc->fb_regno,
10377c478bd9Sstevel@tonic-gate 					off - VGA_MMAP_FB_BASE,
10387c478bd9Sstevel@tonic-gate 					length, PROT_ALL, 0, &dev_attr)) < 0) {
10397c478bd9Sstevel@tonic-gate 		return (err);
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	*maplen = length;
10447c478bd9Sstevel@tonic-gate 	return (0);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate static int
10497c478bd9Sstevel@tonic-gate vgatext_devinit(struct vgatext_softc *softc, struct vis_devinit *data)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	/* initialize console instance */
10527c478bd9Sstevel@tonic-gate 	data->version = VIS_CONS_REV;
10537c478bd9Sstevel@tonic-gate 	data->width = TEXT_COLS;
10547c478bd9Sstevel@tonic-gate 	data->height = TEXT_ROWS;
10557c478bd9Sstevel@tonic-gate 	data->linebytes = TEXT_COLS;
10567c478bd9Sstevel@tonic-gate 	data->depth = 4;
10577c478bd9Sstevel@tonic-gate 	data->mode = VIS_TEXT;
10587c478bd9Sstevel@tonic-gate 	data->polledio = &softc->polledio;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	return (0);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * display a string on the screen at (row, col)
10657c478bd9Sstevel@tonic-gate  *	 assume it has been cropped to fit.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static void
10697c478bd9Sstevel@tonic-gate vgatext_cons_display(struct vgatext_softc *softc, struct vis_consdisplay *da)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	unsigned char	*string;
10727c478bd9Sstevel@tonic-gate 	int	i;
10737c478bd9Sstevel@tonic-gate 	unsigned char	attr;
10747c478bd9Sstevel@tonic-gate 	struct cgatext {
10757c478bd9Sstevel@tonic-gate 		unsigned char ch;
10767c478bd9Sstevel@tonic-gate 		unsigned char attr;
10777c478bd9Sstevel@tonic-gate 	};
10787c478bd9Sstevel@tonic-gate 	struct cgatext *addr;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
10817c478bd9Sstevel@tonic-gate 		return;
10827c478bd9Sstevel@tonic-gate 	/*
10837c478bd9Sstevel@tonic-gate 	 * Sanity checks.  This is a last-ditch effort to avoid damage
10847c478bd9Sstevel@tonic-gate 	 * from brokenness or maliciousness above.
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	if (da->row < 0 || da->row >= TEXT_ROWS ||
10877c478bd9Sstevel@tonic-gate 	    da->col < 0 || da->col >= TEXT_COLS ||
10887c478bd9Sstevel@tonic-gate 	    da->col + da->width > TEXT_COLS)
10897c478bd9Sstevel@tonic-gate 		return;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * To be fully general, we should copyin the data.  This is not
10937c478bd9Sstevel@tonic-gate 	 * really relevant for this text-only driver, but a graphical driver
10947c478bd9Sstevel@tonic-gate 	 * should support these ioctls from userland to enable simple
10957c478bd9Sstevel@tonic-gate 	 * system startup graphics.
10967c478bd9Sstevel@tonic-gate 	 */
10977c478bd9Sstevel@tonic-gate 	attr = (solaris_color_to_pc_color[da->bg_color & 0xf] << 4)
10987c478bd9Sstevel@tonic-gate 		| solaris_color_to_pc_color[da->fg_color & 0xf];
10997c478bd9Sstevel@tonic-gate 	string = da->data;
11007c478bd9Sstevel@tonic-gate 	addr = (struct cgatext *)softc->current_base
11017c478bd9Sstevel@tonic-gate 		+  (da->row * TEXT_COLS + da->col);
11027c478bd9Sstevel@tonic-gate 	for (i = 0; i < da->width; i++) {
11037c478bd9Sstevel@tonic-gate 		addr->ch = string[i];
11047c478bd9Sstevel@tonic-gate 		addr->attr = attr;
11057c478bd9Sstevel@tonic-gate 		addr++;
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static void
11107c478bd9Sstevel@tonic-gate vgatext_polled_display(
11117c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11127c478bd9Sstevel@tonic-gate 	struct vis_consdisplay *da)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	vgatext_cons_display((struct vgatext_softc *)arg, da);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * screen-to-screen copy
11197c478bd9Sstevel@tonic-gate  */
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate static void
11227c478bd9Sstevel@tonic-gate vgatext_cons_copy(struct vgatext_softc *softc, struct vis_conscopy *ma)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	unsigned short	*from;
11257c478bd9Sstevel@tonic-gate 	unsigned short	*to;
11267c478bd9Sstevel@tonic-gate 	int		cnt;
11277c478bd9Sstevel@tonic-gate 	screen_size_t chars_per_row;
11287c478bd9Sstevel@tonic-gate 	unsigned short	*to_row_start;
11297c478bd9Sstevel@tonic-gate 	unsigned short	*from_row_start;
11307c478bd9Sstevel@tonic-gate 	screen_size_t	rows_to_move;
11317c478bd9Sstevel@tonic-gate 	unsigned short	*base;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
11347c478bd9Sstevel@tonic-gate 		return;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/*
11377c478bd9Sstevel@tonic-gate 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
11387c478bd9Sstevel@tonic-gate 	 * damage caused by broken-ness or maliciousness above.
11397c478bd9Sstevel@tonic-gate 	 */
11407c478bd9Sstevel@tonic-gate 	if (ma->s_col < 0 || ma->s_col >= TEXT_COLS ||
11417c478bd9Sstevel@tonic-gate 	    ma->s_row < 0 || ma->s_row >= TEXT_ROWS ||
11427c478bd9Sstevel@tonic-gate 	    ma->e_col < 0 || ma->e_col >= TEXT_COLS ||
11437c478bd9Sstevel@tonic-gate 	    ma->e_row < 0 || ma->e_row >= TEXT_ROWS ||
11447c478bd9Sstevel@tonic-gate 	    ma->t_col < 0 || ma->t_col >= TEXT_COLS ||
11457c478bd9Sstevel@tonic-gate 	    ma->t_row < 0 || ma->t_row >= TEXT_ROWS ||
11467c478bd9Sstevel@tonic-gate 	    ma->s_col > ma->e_col ||
11477c478bd9Sstevel@tonic-gate 	    ma->s_row > ma->e_row)
11487c478bd9Sstevel@tonic-gate 		return;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * Remember we're going to copy shorts because each
11527c478bd9Sstevel@tonic-gate 	 * character/attribute pair is 16 bits.
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	chars_per_row = ma->e_col - ma->s_col + 1;
11557c478bd9Sstevel@tonic-gate 	rows_to_move = ma->e_row - ma->s_row + 1;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/* More sanity checks. */
11587c478bd9Sstevel@tonic-gate 	if (ma->t_row + rows_to_move > TEXT_ROWS ||
11597c478bd9Sstevel@tonic-gate 	    ma->t_col + chars_per_row > TEXT_COLS)
11607c478bd9Sstevel@tonic-gate 		return;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	base = (unsigned short *)softc->current_base;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	to_row_start = base + ((ma->t_row * TEXT_COLS) + ma->t_col);
11657c478bd9Sstevel@tonic-gate 	from_row_start = base + ((ma->s_row * TEXT_COLS) + ma->s_col);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	if (to_row_start < from_row_start) {
11687c478bd9Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
11697c478bd9Sstevel@tonic-gate 			to = to_row_start;
11707c478bd9Sstevel@tonic-gate 			from = from_row_start;
11717c478bd9Sstevel@tonic-gate 			to_row_start += TEXT_COLS;
11727c478bd9Sstevel@tonic-gate 			from_row_start += TEXT_COLS;
11737c478bd9Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
11747c478bd9Sstevel@tonic-gate 				*to++ = *from++;
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 	} else {
11777c478bd9Sstevel@tonic-gate 		/*
11787c478bd9Sstevel@tonic-gate 		 * Offset to the end of the region and copy backwards.
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		cnt = rows_to_move * TEXT_COLS + chars_per_row;
11817c478bd9Sstevel@tonic-gate 		to_row_start += cnt;
11827c478bd9Sstevel@tonic-gate 		from_row_start += cnt;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		while (rows_to_move-- > 0) {
11857c478bd9Sstevel@tonic-gate 			to_row_start -= TEXT_COLS;
11867c478bd9Sstevel@tonic-gate 			from_row_start -= TEXT_COLS;
11877c478bd9Sstevel@tonic-gate 			to = to_row_start;
11887c478bd9Sstevel@tonic-gate 			from = from_row_start;
11897c478bd9Sstevel@tonic-gate 			for (cnt = chars_per_row; cnt-- > 0; )
11907c478bd9Sstevel@tonic-gate 				*--to = *--from;
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate static void
11967c478bd9Sstevel@tonic-gate vgatext_polled_copy(
11977c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
11987c478bd9Sstevel@tonic-gate 	struct vis_conscopy *ca)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 	vgatext_cons_copy((struct vgatext_softc *)arg, ca);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate static void
12057c478bd9Sstevel@tonic-gate vgatext_cons_cursor(struct vgatext_softc *softc, struct vis_conscursor *ca)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
12087c478bd9Sstevel@tonic-gate 		return;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	switch (ca->action) {
12117c478bd9Sstevel@tonic-gate 	case VIS_HIDE_CURSOR:
12127c478bd9Sstevel@tonic-gate 		softc->cursor.visible = B_FALSE;
12137c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
12147c478bd9Sstevel@tonic-gate 			vgatext_hide_cursor(softc);
12157c478bd9Sstevel@tonic-gate 		break;
12167c478bd9Sstevel@tonic-gate 	case VIS_DISPLAY_CURSOR:
12177c478bd9Sstevel@tonic-gate 		/*
12187c478bd9Sstevel@tonic-gate 		 * Sanity check.  This is a last-ditch effort to avoid
12197c478bd9Sstevel@tonic-gate 		 * damage from brokenness or maliciousness above.
12207c478bd9Sstevel@tonic-gate 		 */
12217c478bd9Sstevel@tonic-gate 		if (ca->col < 0 || ca->col >= TEXT_COLS ||
12227c478bd9Sstevel@tonic-gate 		    ca->row < 0 || ca->row >= TEXT_ROWS)
12237c478bd9Sstevel@tonic-gate 			return;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		softc->cursor.visible = B_TRUE;
12267c478bd9Sstevel@tonic-gate 		softc->cursor.col = ca->col;
12277c478bd9Sstevel@tonic-gate 		softc->cursor.row = ca->row;
12287c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base)
12297c478bd9Sstevel@tonic-gate 			vgatext_set_cursor(softc, ca->row, ca->col);
12307c478bd9Sstevel@tonic-gate 		break;
12317c478bd9Sstevel@tonic-gate 	case VIS_GET_CURSOR:
12327c478bd9Sstevel@tonic-gate 		if (softc->current_base == softc->text_base) {
12337c478bd9Sstevel@tonic-gate 			vgatext_get_cursor(softc, &ca->row, &ca->col);
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		break;
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate static void
12407c478bd9Sstevel@tonic-gate vgatext_polled_cursor(
12417c478bd9Sstevel@tonic-gate 	struct vis_polledio_arg *arg,
12427c478bd9Sstevel@tonic-gate 	struct vis_conscursor *ca)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	vgatext_cons_cursor((struct vgatext_softc *)arg, ca);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12507c478bd9Sstevel@tonic-gate static void
12517c478bd9Sstevel@tonic-gate vgatext_hide_cursor(struct vgatext_softc *softc)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	/* Nothing at present */
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate static void
12577c478bd9Sstevel@tonic-gate vgatext_set_cursor(struct vgatext_softc *softc, int row, int col)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	short	addr;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	if (vgatext_silent)
12627c478bd9Sstevel@tonic-gate 		return;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	addr = row * TEXT_COLS + col;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAH, addr >> 8);
12677c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_CLAL, addr & 0xff);
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate static int vga_row, vga_col;
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate static void
12737c478bd9Sstevel@tonic-gate vgatext_get_cursor(struct vgatext_softc *softc,
12747c478bd9Sstevel@tonic-gate     screen_pos_t *row, screen_pos_t *col)
12757c478bd9Sstevel@tonic-gate {
12767c478bd9Sstevel@tonic-gate 	short   addr;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	addr = (vga_get_crtc(&softc->regs, VGA_CRTC_CLAH) << 8) +
12797c478bd9Sstevel@tonic-gate 	    vga_get_crtc(&softc->regs, VGA_CRTC_CLAL);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	vga_row = *row = addr / TEXT_COLS;
12827c478bd9Sstevel@tonic-gate 	vga_col = *col = addr % TEXT_COLS;
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate /*
12867c478bd9Sstevel@tonic-gate  * This code is experimental. It's only enabled if console is
12877c478bd9Sstevel@tonic-gate  * set to graphics, a preliminary implementation of happyface boot.
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate static void
12907c478bd9Sstevel@tonic-gate vgatext_set_text(struct vgatext_softc *softc)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	int i;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	if (happyface_boot == 0)
12957c478bd9Sstevel@tonic-gate 		return;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/* we are in graphics mode, set to text 80X25 mode */
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/* set misc registers */
13007c478bd9Sstevel@tonic-gate 	vga_set_reg(&softc->regs, VGA_MISC_W, VGA_MISC_TEXT);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	/* set sequencer registers */
13037c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
13047c478bd9Sstevel@tonic-gate 		(vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) &
13057c478bd9Sstevel@tonic-gate 		~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
13067c478bd9Sstevel@tonic-gate 	for (i = 1; i < NUM_SEQ_REG; i++) {
13077c478bd9Sstevel@tonic-gate 		vga_set_seq(&softc->regs, i, VGA_SEQ_TEXT[i]);
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, VGA_SEQ_RST_SYN,
13107c478bd9Sstevel@tonic-gate 		(vga_get_seq(&softc->regs, VGA_SEQ_RST_SYN) |
13117c478bd9Sstevel@tonic-gate 		VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
13127c478bd9Sstevel@tonic-gate 		VGA_SEQ_RST_SYN_NO_SYNC_RESET));
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	/* set crt controller registers */
13157c478bd9Sstevel@tonic-gate 	vga_set_crtc(&softc->regs, VGA_CRTC_VRE,
13167c478bd9Sstevel@tonic-gate 		(vga_get_crtc(&softc->regs, VGA_CRTC_VRE) &
13177c478bd9Sstevel@tonic-gate 		~VGA_CRTC_VRE_LOCK));
13187c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_CRTC_REG; i++) {
13197c478bd9Sstevel@tonic-gate 		vga_set_crtc(&softc->regs, i, VGA_CRTC_TEXT[i]);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/* set graphics controller registers */
13237c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_GRC_REG; i++) {
13247c478bd9Sstevel@tonic-gate 		vga_set_grc(&softc->regs, i, VGA_GRC_TEXT[i]);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	/* set attribute registers */
13287c478bd9Sstevel@tonic-gate 	for (i = 0; i < NUM_ATR_REG; i++) {
13297c478bd9Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, VGA_ATR_TEXT[i]);
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/* set palette */
13337c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
13347c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, VGA_TEXT_PALETTES[i][0] << 2,
13357c478bd9Sstevel@tonic-gate 			VGA_TEXT_PALETTES[i][1] << 2,
13367c478bd9Sstevel@tonic-gate 			VGA_TEXT_PALETTES[i][2] << 2);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
13397c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i, 0, 0, 0);
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	vgatext_save_colormap(softc);
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate static void
13467c478bd9Sstevel@tonic-gate vgatext_init(struct vgatext_softc *softc)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	unsigned char atr_mode;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
13517c478bd9Sstevel@tonic-gate 	if (atr_mode & VGA_ATR_MODE_GRAPH)
13527c478bd9Sstevel@tonic-gate 		vgatext_set_text(softc);
13537c478bd9Sstevel@tonic-gate 	atr_mode = vga_get_atr(&softc->regs, VGA_ATR_MODE);
13547c478bd9Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_BLINK;
13557c478bd9Sstevel@tonic-gate 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
13567c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_MODE, atr_mode);
13577c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
13587c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
13597c478bd9Sstevel@tonic-gate 		vga_get_atr(&softc->regs, VGA_BRIGHT_WHITE));
13607c478bd9Sstevel@tonic-gate #else
13617c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
13627c478bd9Sstevel@tonic-gate 		vga_get_atr(&softc->regs, VGA_BLACK));
13637c478bd9Sstevel@tonic-gate #endif
13647c478bd9Sstevel@tonic-gate 	vgatext_setfont(softc);	/* need selectable font? */
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate #if	defined(USE_BORDERS)
13687c478bd9Sstevel@tonic-gate static void
13697c478bd9Sstevel@tonic-gate vgatext_init_graphics(struct vgatext_softc *softc)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, VGA_ATR_BDR_CLR,
13727c478bd9Sstevel@tonic-gate 		vga_get_atr(&softc->regs, VGA_BLACK));
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate #endif
13757c478bd9Sstevel@tonic-gate 
13762269adc8Sszhou static char vga_fontslot = 0;
13772269adc8Sszhou 
13787c478bd9Sstevel@tonic-gate static void
13797c478bd9Sstevel@tonic-gate vgatext_setfont(struct vgatext_softc *softc)
13807c478bd9Sstevel@tonic-gate {
13812269adc8Sszhou 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
13822269adc8Sszhou 
13837c478bd9Sstevel@tonic-gate 	extern unsigned char *ENCODINGS[];
13842269adc8Sszhou 	uchar_t *from;
13852269adc8Sszhou 	uchar_t volatile *to;
13862269adc8Sszhou 	int	i, j, s;
13872269adc8Sszhou 	int	bpc, f_offset;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
13907c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
13917c478bd9Sstevel@tonic-gate 	/*
13927c478bd9Sstevel@tonic-gate 	 *  enable write to plane2, since fonts
13937c478bd9Sstevel@tonic-gate 	 * could only be loaded into plane2
13947c478bd9Sstevel@tonic-gate 	 */
13957c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x04);
13967c478bd9Sstevel@tonic-gate 	/*
13977c478bd9Sstevel@tonic-gate 	 *  sequentially access data in the bit map being
13987c478bd9Sstevel@tonic-gate 	 * selected by MapMask register (index 0x02)
13997c478bd9Sstevel@tonic-gate 	 */
14007c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x07);
14017c478bd9Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
14027c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/*
14057c478bd9Sstevel@tonic-gate 	 *  select plane 2 on Read Mode 0
14067c478bd9Sstevel@tonic-gate 	 */
14077c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x02);
14087c478bd9Sstevel@tonic-gate 	/*
14097c478bd9Sstevel@tonic-gate 	 *  system addresses sequentially access data, follow
14107c478bd9Sstevel@tonic-gate 	 * Memory Mode register bit 2 in the sequencer
14117c478bd9Sstevel@tonic-gate 	 */
14127c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x00);
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * set range of host memory addresses decoded by VGA
14157c478bd9Sstevel@tonic-gate 	 * hardware -- A0000h-BFFFFh (128K region)
14167c478bd9Sstevel@tonic-gate 	 */
14177c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x00);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * This assumes 8x16 characters, which yield the traditional 80x25
14217c478bd9Sstevel@tonic-gate 	 * screen.  It really should support other character heights.
14227c478bd9Sstevel@tonic-gate 	 */
14237c478bd9Sstevel@tonic-gate 	bpc = 16;
14242269adc8Sszhou 	s = vga_fontslot;
14252269adc8Sszhou 	f_offset = s * 8 * 1024;
14267c478bd9Sstevel@tonic-gate 	for (i = 0; i < 256; i++) {
14277c478bd9Sstevel@tonic-gate 		from = ENCODINGS[i];
14282269adc8Sszhou 		to = (unsigned char *)softc->fb.addr + f_offset + i * 0x20;
14297c478bd9Sstevel@tonic-gate 		for (j = 0; j < bpc; j++)
14307c478bd9Sstevel@tonic-gate 			*to++ = *from++;
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	/* Sync-reset the sequencer registers */
14347c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x01);
14357c478bd9Sstevel@tonic-gate 	/* enable write to plane 0 and 1 */
14367c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x02, 0x03);
14377c478bd9Sstevel@tonic-gate 	/*
14387c478bd9Sstevel@tonic-gate 	 * enable character map selection
14397c478bd9Sstevel@tonic-gate 	 * and odd/even addressing
14407c478bd9Sstevel@tonic-gate 	 */
14417c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x04, 0x03);
14427c478bd9Sstevel@tonic-gate 	/*
14432269adc8Sszhou 	 * select font map
14447c478bd9Sstevel@tonic-gate 	 */
14452269adc8Sszhou 	vga_set_seq(&softc->regs, 0x03, fsreg[s]);
14467c478bd9Sstevel@tonic-gate 	/* Sync-reset ended, and allow the sequencer to operate */
14477c478bd9Sstevel@tonic-gate 	vga_set_seq(&softc->regs, 0x00, 0x03);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/* restore graphic registers */
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/* select plane 0 */
14527c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x04, 0x00);
14537c478bd9Sstevel@tonic-gate 	/* enable odd/even addressing mode */
14547c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x05, 0x10);
14557c478bd9Sstevel@tonic-gate 	/*
14567c478bd9Sstevel@tonic-gate 	 * range of host memory addresses decoded by VGA
14577c478bd9Sstevel@tonic-gate 	 * hardware -- B8000h-BFFFFh (32K region)
14587c478bd9Sstevel@tonic-gate 	 */
14597c478bd9Sstevel@tonic-gate 	vga_set_grc(&softc->regs, 0x06, 0x0e);
14607c478bd9Sstevel@tonic-gate 	/* enable all color plane */
14617c478bd9Sstevel@tonic-gate 	vga_set_atr(&softc->regs, 0x12, 0x0f);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate }
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate static void
14667c478bd9Sstevel@tonic-gate vgatext_save_colormap(struct vgatext_softc *softc)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	int i;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
14717c478bd9Sstevel@tonic-gate 		softc->attrib_palette[i] = vga_get_atr(&softc->regs, i);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
14747c478bd9Sstevel@tonic-gate 		vga_get_cmap(&softc->regs, i,
14757c478bd9Sstevel@tonic-gate 			&softc->colormap[i].red,
14767c478bd9Sstevel@tonic-gate 			&softc->colormap[i].green,
14777c478bd9Sstevel@tonic-gate 			&softc->colormap[i].blue);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate static void
14827c478bd9Sstevel@tonic-gate vgatext_restore_colormap(struct vgatext_softc *softc)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	int i;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
14877c478bd9Sstevel@tonic-gate 		vga_set_atr(&softc->regs, i, softc->attrib_palette[i]);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
14907c478bd9Sstevel@tonic-gate 		vga_put_cmap(&softc->regs, i,
14917c478bd9Sstevel@tonic-gate 			softc->colormap[i].red,
14927c478bd9Sstevel@tonic-gate 			softc->colormap[i].green,
14937c478bd9Sstevel@tonic-gate 			softc->colormap[i].blue);
14947c478bd9Sstevel@tonic-gate 	}
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate /*
14987c478bd9Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
14997c478bd9Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
15007c478bd9Sstevel@tonic-gate  *
15017c478bd9Sstevel@tonic-gate  * This version searches a PCI-style "reg" property.  It was prompted by
15027c478bd9Sstevel@tonic-gate  * issues surrounding the presence or absence of an entry for the ROM:
15037c478bd9Sstevel@tonic-gate  * (a) a transition problem with PowerPC Virtual Open Firmware
15047c478bd9Sstevel@tonic-gate  * (b) uncertainty as to whether an entry will be included on a device
15057c478bd9Sstevel@tonic-gate  *     with ROM support (and so an "active" ROM base address register),
15067c478bd9Sstevel@tonic-gate  *     but no ROM actually installed.
15077c478bd9Sstevel@tonic-gate  *
15087c478bd9Sstevel@tonic-gate  * See the note below on vgatext_get_isa_reg_index for the reasons for
15097c478bd9Sstevel@tonic-gate  * returning the offset.
15107c478bd9Sstevel@tonic-gate  *
15117c478bd9Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
15127c478bd9Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
15137c478bd9Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
15147c478bd9Sstevel@tonic-gate  */
15157c478bd9Sstevel@tonic-gate static int
15167c478bd9Sstevel@tonic-gate vgatext_get_pci_reg_index(
15177c478bd9Sstevel@tonic-gate 	dev_info_t *const devi,
15187c478bd9Sstevel@tonic-gate 	unsigned long himask,
15197c478bd9Sstevel@tonic-gate 	unsigned long hival,
15207c478bd9Sstevel@tonic-gate 	unsigned long addr,
15217c478bd9Sstevel@tonic-gate 	off_t *offset)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	int			length, index;
15257c478bd9Sstevel@tonic-gate 	pci_regspec_t	*reg;
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
15287c478bd9Sstevel@tonic-gate 		"reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
15297c478bd9Sstevel@tonic-gate 		return (-1);
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
15337c478bd9Sstevel@tonic-gate 		if ((reg[index].pci_phys_hi & himask) != hival)
15347c478bd9Sstevel@tonic-gate 			continue;
15357c478bd9Sstevel@tonic-gate 		if (reg[index].pci_size_hi != 0)
15367c478bd9Sstevel@tonic-gate 			continue;
15377c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_mid != 0)
15387c478bd9Sstevel@tonic-gate 			continue;
15397c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_low > addr)
15407c478bd9Sstevel@tonic-gate 			continue;
15417c478bd9Sstevel@tonic-gate 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
15427c478bd9Sstevel@tonic-gate 			continue;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		*offset = addr - reg[index].pci_phys_low;
15457c478bd9Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
15467c478bd9Sstevel@tonic-gate 		return (index);
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	return (-1);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate /*
15547c478bd9Sstevel@tonic-gate  * search the entries of the "reg" property for one which has the desired
15557c478bd9Sstevel@tonic-gate  * combination of phys_hi bits and contains the desired address.
15567c478bd9Sstevel@tonic-gate  *
15577c478bd9Sstevel@tonic-gate  * This version searches a ISA-style "reg" property.  It was prompted by
15587c478bd9Sstevel@tonic-gate  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
15597c478bd9Sstevel@tonic-gate  * 8514/A registers should have been added after all standard VGA registers.
15607c478bd9Sstevel@tonic-gate  * Unfortunately, the Solaris/Intel device configuration framework
15617c478bd9Sstevel@tonic-gate  * (a) lists the 8514/A registers before the video memory, and then
15627c478bd9Sstevel@tonic-gate  * (b) also sorts the entries so that I/O entries come before memory
15637c478bd9Sstevel@tonic-gate  *     entries.
15647c478bd9Sstevel@tonic-gate  *
15657c478bd9Sstevel@tonic-gate  * It returns the "reg" index and offset into that register set.
15667c478bd9Sstevel@tonic-gate  * The offset is needed because there exist (broken?) BIOSes that
15677c478bd9Sstevel@tonic-gate  * report larger ranges enclosing the standard ranges.  One reports
15687c478bd9Sstevel@tonic-gate  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
15697c478bd9Sstevel@tonic-gate  * offset adjusts for this difference in the base of the register set.
15707c478bd9Sstevel@tonic-gate  *
15717c478bd9Sstevel@tonic-gate  * Note that this routine may not be fully general; it is intended for the
15727c478bd9Sstevel@tonic-gate  * specific purpose of finding a couple of particular VGA reg entries and
15737c478bd9Sstevel@tonic-gate  * may not be suitable for all reg-searching purposes.
15747c478bd9Sstevel@tonic-gate  */
15757c478bd9Sstevel@tonic-gate static int
15767c478bd9Sstevel@tonic-gate vgatext_get_isa_reg_index(
15777c478bd9Sstevel@tonic-gate 	dev_info_t *const devi,
15787c478bd9Sstevel@tonic-gate 	unsigned long hival,
15797c478bd9Sstevel@tonic-gate 	unsigned long addr,
15807c478bd9Sstevel@tonic-gate 	off_t *offset)
15817c478bd9Sstevel@tonic-gate {
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	int		length, index;
15847c478bd9Sstevel@tonic-gate 	struct regspec	*reg;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
15877c478bd9Sstevel@tonic-gate 		"reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
15887c478bd9Sstevel@tonic-gate 		return (-1);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	for (index = 0; index < length / sizeof (struct regspec); index++) {
15927c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_bustype != hival)
15937c478bd9Sstevel@tonic-gate 			continue;
15947c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_addr > addr)
15957c478bd9Sstevel@tonic-gate 			continue;
15967c478bd9Sstevel@tonic-gate 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
15977c478bd9Sstevel@tonic-gate 			continue;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 		*offset = addr - reg[index].regspec_addr;
16007c478bd9Sstevel@tonic-gate 		kmem_free(reg, (size_t)length);
16017c478bd9Sstevel@tonic-gate 		return (index);
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 	kmem_free(reg, (size_t)length);
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	return (-1);
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate /*
16097c478bd9Sstevel@tonic-gate  * If AGP cap pointer is successfully found, none-zero value is returned.
16107c478bd9Sstevel@tonic-gate  * Otherwise 0 is returned.
16117c478bd9Sstevel@tonic-gate  */
16127c478bd9Sstevel@tonic-gate static off_t
16137c478bd9Sstevel@tonic-gate agp_master_cap_find(ddi_acc_handle_t acc_handle)
16147c478bd9Sstevel@tonic-gate {
16157c478bd9Sstevel@tonic-gate 	off_t		nextcap;
16167c478bd9Sstevel@tonic-gate 	uint32_t	ncapid;
16177c478bd9Sstevel@tonic-gate 	uint8_t		value;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/* check if this device supports capibility pointer */
16207c478bd9Sstevel@tonic-gate 	value = (uint8_t)(pci_config_get16(acc_handle, PCI_CONF_STAT)
16217c478bd9Sstevel@tonic-gate 			    & PCI_CONF_CAP_MASK);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	if (!value)
16247c478bd9Sstevel@tonic-gate 		return (0);
16257c478bd9Sstevel@tonic-gate 	/* get the offset of the first capability pointer from CAPPTR */
16267c478bd9Sstevel@tonic-gate 	nextcap = (off_t)(pci_config_get8(acc_handle, AGP_CONF_CAPPTR));
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	/* check AGP capability from the first capability pointer */
16297c478bd9Sstevel@tonic-gate 	while (nextcap) {
16307c478bd9Sstevel@tonic-gate 		ncapid = pci_config_get32(acc_handle, nextcap);
16317c478bd9Sstevel@tonic-gate 		if ((ncapid & PCI_CONF_CAPID_MASK)
16327c478bd9Sstevel@tonic-gate 		    == AGP_CAP_ID) /* find AGP cap */
16337c478bd9Sstevel@tonic-gate 			break;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 		nextcap = (off_t)((ncapid & PCI_CONF_NCAPID_MASK) >> 8);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	return (nextcap);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate /*
16437c478bd9Sstevel@tonic-gate  * If i8xx device is successfully detected, 0 is returned.
16447c478bd9Sstevel@tonic-gate  * Otherwise -1 is returned.
16457c478bd9Sstevel@tonic-gate  */
16467c478bd9Sstevel@tonic-gate static int
16477c478bd9Sstevel@tonic-gate detect_i8xx_device(agp_master_softc_t *master_softc)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	switch (master_softc->agpm_id) {
16517c478bd9Sstevel@tonic-gate 	case INTEL_IGD_810:
16527c478bd9Sstevel@tonic-gate 	case INTEL_IGD_810DC:
16537c478bd9Sstevel@tonic-gate 	case INTEL_IGD_810E:
16547c478bd9Sstevel@tonic-gate 	case INTEL_IGD_815:
16557c478bd9Sstevel@tonic-gate 		master_softc->agpm_dev_type = DEVICE_IS_I810;
16567c478bd9Sstevel@tonic-gate 		break;
16577c478bd9Sstevel@tonic-gate 	case INTEL_IGD_830M:
16587c478bd9Sstevel@tonic-gate 	case INTEL_IGD_845G:
16597c478bd9Sstevel@tonic-gate 	case INTEL_IGD_855GM:
16607c478bd9Sstevel@tonic-gate 	case INTEL_IGD_865G:
1661*d6bb6a84Sms148562 	case INTEL_IGD_910:
1662*d6bb6a84Sms148562 	case INTEL_IGD_910M:
16637c478bd9Sstevel@tonic-gate 		master_softc->agpm_dev_type = DEVICE_IS_I830;
16647c478bd9Sstevel@tonic-gate 		break;
16657c478bd9Sstevel@tonic-gate 	default:		/* unknown id */
16667c478bd9Sstevel@tonic-gate 		return (-1);
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	return (0);
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate /*
16737c478bd9Sstevel@tonic-gate  * If agp master is succssfully detected, 0 is returned.
16747c478bd9Sstevel@tonic-gate  * Otherwise -1 is returned.
16757c478bd9Sstevel@tonic-gate  */
16767c478bd9Sstevel@tonic-gate static int
16777c478bd9Sstevel@tonic-gate detect_agp_devcice(agp_master_softc_t *master_softc)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate 	off_t cap;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	cap = agp_master_cap_find(master_softc->agpm_acc_hdl);
16827c478bd9Sstevel@tonic-gate 	if (cap) {
16837c478bd9Sstevel@tonic-gate 		master_softc->agpm_dev_type = DEVICE_IS_AGP;
16847c478bd9Sstevel@tonic-gate 		master_softc->agpm_data.agpm_acaptr = cap;
16857c478bd9Sstevel@tonic-gate 		return (0);
16867c478bd9Sstevel@tonic-gate 	} else {
16877c478bd9Sstevel@tonic-gate 		return (-1);
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate }
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate /*
16937c478bd9Sstevel@tonic-gate  * If agp master is successfully initialized, 0 is returned.
16947c478bd9Sstevel@tonic-gate  * Otherwise -1 is returned.
16957c478bd9Sstevel@tonic-gate  */
16967c478bd9Sstevel@tonic-gate static int
16977c478bd9Sstevel@tonic-gate agp_master_init(struct vgatext_softc *softc)
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
17007c478bd9Sstevel@tonic-gate 	int instance;
17017c478bd9Sstevel@tonic-gate 	int status;
17027c478bd9Sstevel@tonic-gate 	agp_master_softc_t *agp_master;
17037c478bd9Sstevel@tonic-gate 	uint32_t value;
17047c478bd9Sstevel@tonic-gate 	off_t reg_size;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	ASSERT(softc);
17087c478bd9Sstevel@tonic-gate 	agp_master = softc->agp_master;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	devi = softc->devi;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(devi);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	status = pci_config_setup(devi, &agp_master->agpm_acc_hdl);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
17177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "agp_master_init: pci_config_setup failed");
17187c478bd9Sstevel@tonic-gate 		return (-1);
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	agp_master->agpm_id =
17227c478bd9Sstevel@tonic-gate 	    pci_config_get32(agp_master->agpm_acc_hdl, PCI_CONF_VENID);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	if (!detect_i8xx_device(agp_master)) {
17257c478bd9Sstevel@tonic-gate 		/* map mmio register set */
1726*d6bb6a84Sms148562 		if ((agp_master->agpm_id == INTEL_IGD_910) ||
1727*d6bb6a84Sms148562 		    (agp_master->agpm_id == INTEL_IGD_910M)) {
1728*d6bb6a84Sms148562 			status = ddi_regs_map_setup(devi, I915_GTTADDR_BAR,
1729*d6bb6a84Sms148562 			    &agp_master->agpm_data.agpm_gtt.gtt_mmio_base,
1730*d6bb6a84Sms148562 			    0, 0, &i8xx_dev_access,
1731*d6bb6a84Sms148562 			    &agp_master->agpm_data.agpm_gtt.gtt_mmio_handle);
1732*d6bb6a84Sms148562 
1733*d6bb6a84Sms148562 		} else {
17347c478bd9Sstevel@tonic-gate 			status = ddi_regs_map_setup(devi, I8XX_MMIO_REGSET,
17357c478bd9Sstevel@tonic-gate 			    &agp_master->agpm_data.agpm_gtt.gtt_mmio_base,
17367c478bd9Sstevel@tonic-gate 			    0, 0, &i8xx_dev_access,
17377c478bd9Sstevel@tonic-gate 			    &agp_master->agpm_data.agpm_gtt.gtt_mmio_handle);
1738*d6bb6a84Sms148562 		}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
17417c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
17427c478bd9Sstevel@tonic-gate 			    "agp_master_init: ddi_regs_map_setup failed");
17437c478bd9Sstevel@tonic-gate 			agp_master_end(agp_master);
17447c478bd9Sstevel@tonic-gate 			return (-1);
17457c478bd9Sstevel@tonic-gate 		}
17467c478bd9Sstevel@tonic-gate 		/* get GTT range base offset */
1747*d6bb6a84Sms148562 		if ((agp_master->agpm_id == INTEL_IGD_910) ||
1748*d6bb6a84Sms148562 		    (agp_master->agpm_id == INTEL_IGD_910M)) {
1749*d6bb6a84Sms148562 			agp_master->agpm_data.agpm_gtt.gtt_addr =
1750*d6bb6a84Sms148562 			    agp_master->agpm_data.agpm_gtt.gtt_mmio_base;
1751*d6bb6a84Sms148562 		} else
17527c478bd9Sstevel@tonic-gate 			agp_master->agpm_data.agpm_gtt.gtt_addr =
17537c478bd9Sstevel@tonic-gate 			    agp_master->agpm_data.agpm_gtt.gtt_mmio_base +
17547c478bd9Sstevel@tonic-gate 			    I8XX_PTE_OFFSET;
1755*d6bb6a84Sms148562 
17567c478bd9Sstevel@tonic-gate 		/* get graphics memory size */
1757*d6bb6a84Sms148562 		if ((agp_master->agpm_id == INTEL_IGD_910) ||
1758*d6bb6a84Sms148562 		    (agp_master->agpm_id == INTEL_IGD_910M)) {
1759*d6bb6a84Sms148562 			status = ddi_dev_regsize(devi, I915_FB_REGSET,
1760*d6bb6a84Sms148562 			    &reg_size);
1761*d6bb6a84Sms148562 		} else
17627c478bd9Sstevel@tonic-gate 			status = ddi_dev_regsize(devi, I8XX_FB_REGSET,
17637c478bd9Sstevel@tonic-gate 			    &reg_size);
17647c478bd9Sstevel@tonic-gate 		/*
17657c478bd9Sstevel@tonic-gate 		 * if memory size is smaller than a certain value, it means
17667c478bd9Sstevel@tonic-gate 		 * the register set number for graphics memory range might
17677c478bd9Sstevel@tonic-gate 		 * be wrong
17687c478bd9Sstevel@tonic-gate 		 */
17697c478bd9Sstevel@tonic-gate 		if (status != DDI_SUCCESS || reg_size < 0x400000) {
17707c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
17717c478bd9Sstevel@tonic-gate 			    "agp_master_init: ddi_dev_regsize error");
17727c478bd9Sstevel@tonic-gate 			agp_master_end(agp_master);
17737c478bd9Sstevel@tonic-gate 			return (-1);
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 		agp_master->agpm_data.agpm_gtt.gtt_info.igd_apersize =
17777c478bd9Sstevel@tonic-gate 		    BYTES2MB(reg_size);
1778*d6bb6a84Sms148562 
1779*d6bb6a84Sms148562 		if ((agp_master->agpm_id == INTEL_IGD_910) ||
1780*d6bb6a84Sms148562 		    (agp_master->agpm_id == INTEL_IGD_910M))
1781*d6bb6a84Sms148562 			value = pci_config_get32(agp_master->agpm_acc_hdl,
1782*d6bb6a84Sms148562 			    I915_CONF_GMADR);
1783*d6bb6a84Sms148562 		else
17847c478bd9Sstevel@tonic-gate 			value = pci_config_get32(agp_master->agpm_acc_hdl,
17857c478bd9Sstevel@tonic-gate 			    I8XX_CONF_GMADR);
17867c478bd9Sstevel@tonic-gate 		agp_master->agpm_data.agpm_gtt.gtt_info.igd_aperbase =
17877c478bd9Sstevel@tonic-gate 		    value & GTT_BASE_MASK;
17887c478bd9Sstevel@tonic-gate 		agp_master->agpm_data.agpm_gtt.gtt_info.igd_devid =
17897c478bd9Sstevel@tonic-gate 		    agp_master->agpm_id;
17907c478bd9Sstevel@tonic-gate 	} else if (detect_agp_devcice(agp_master)) {
17917c478bd9Sstevel@tonic-gate 		/*
17927c478bd9Sstevel@tonic-gate 		 * non IGD or AGP devices, not error
17937c478bd9Sstevel@tonic-gate 		 */
17947c478bd9Sstevel@tonic-gate 		agp_master_end(agp_master);
17957c478bd9Sstevel@tonic-gate 		return (-1);
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/* create extra minor node for IGD or AGP device */
17997c478bd9Sstevel@tonic-gate 	status = ddi_create_minor_node(softc->devi, AGPMASTER_NAME,
18007c478bd9Sstevel@tonic-gate 		    S_IFCHR, INST2NODE2(instance), DDI_NT_AGP_MASTER, 0);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	if (status != DDI_SUCCESS) {
18037c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
18047c478bd9Sstevel@tonic-gate 		    "agp_master_init: create agpmaster node failed");
18057c478bd9Sstevel@tonic-gate 		agp_master_end(agp_master);
18067c478bd9Sstevel@tonic-gate 		return (-1);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	return (0);
18107c478bd9Sstevel@tonic-gate }
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate /*
18137c478bd9Sstevel@tonic-gate  * Minor node is not removed here, since vgatext_detach is responsible
18147c478bd9Sstevel@tonic-gate  * for removing all nodes.
18157c478bd9Sstevel@tonic-gate  */
18167c478bd9Sstevel@tonic-gate static void
18177c478bd9Sstevel@tonic-gate agp_master_end(agp_master_softc_t *master_softc)
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	ASSERT(master_softc);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/* intel integrated device */
1822*d6bb6a84Sms148562 	if (IS_IGD(master_softc)) {
18237c478bd9Sstevel@tonic-gate 		if (master_softc->agpm_data.agpm_gtt.gtt_mmio_handle != NULL) {
18247c478bd9Sstevel@tonic-gate 			ddi_regs_map_free(
18257c478bd9Sstevel@tonic-gate 			    &master_softc->agpm_data.agpm_gtt.gtt_mmio_handle);
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 	if (master_softc->agpm_acc_hdl != NULL) {
18297c478bd9Sstevel@tonic-gate 		pci_config_teardown(&master_softc->agpm_acc_hdl);
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	bzero(master_softc, sizeof (agp_master_softc_t));
18337c478bd9Sstevel@tonic-gate 	return;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate /*
18387c478bd9Sstevel@tonic-gate  * Please refer to GART and GTT entry format table in agpdefs.h for
18397c478bd9Sstevel@tonic-gate  * intel GTT entry format.
18407c478bd9Sstevel@tonic-gate  */
18417c478bd9Sstevel@tonic-gate static int
18427c478bd9Sstevel@tonic-gate phys2entry(uint32_t type, uint32_t physaddr, uint32_t *entry)
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	uint32_t value;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	switch (type) {
18477c478bd9Sstevel@tonic-gate 	case AGP_PHYSICAL:
18487c478bd9Sstevel@tonic-gate 	case AGP_NORMAL:
18497c478bd9Sstevel@tonic-gate 		value = (physaddr & GTT_PTE_MASK) | GTT_PTE_VALID;
18507c478bd9Sstevel@tonic-gate 		break;
18517c478bd9Sstevel@tonic-gate 	default:
18527c478bd9Sstevel@tonic-gate 		return (-1);
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	*entry = value;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	return (0);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate static int
18617c478bd9Sstevel@tonic-gate i8xx_add_to_gtt(gtt_impl_t *gtt, igd_gtt_seg_t seg)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	int i;
18647c478bd9Sstevel@tonic-gate 	uint32_t *paddr;
18657c478bd9Sstevel@tonic-gate 	uint32_t entry;
18667c478bd9Sstevel@tonic-gate 	uint32_t maxpages;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	maxpages = gtt->gtt_info.igd_apersize;
18697c478bd9Sstevel@tonic-gate 	maxpages = GTT_MB_TO_PAGES(maxpages);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	paddr = seg.igs_phyaddr;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * check if gtt max pages reached
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	if ((seg.igs_pgstart + seg.igs_npage) > maxpages)
18777c478bd9Sstevel@tonic-gate 		return (-1);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	paddr = seg.igs_phyaddr;
18807c478bd9Sstevel@tonic-gate 	for (i = seg.igs_pgstart; i < (seg.igs_pgstart + seg.igs_npage);
18817c478bd9Sstevel@tonic-gate 	    i++, paddr++) {
18827c478bd9Sstevel@tonic-gate 		if (phys2entry(seg.igs_type, *paddr, &entry))
18837c478bd9Sstevel@tonic-gate 			return (-1);
18847c478bd9Sstevel@tonic-gate 		ddi_put32(gtt->gtt_mmio_handle,
18857c478bd9Sstevel@tonic-gate 		    (uint32_t *)(gtt->gtt_addr + i * sizeof (uint32_t)),
18867c478bd9Sstevel@tonic-gate 		    entry);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	return (0);
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate static void
18937c478bd9Sstevel@tonic-gate i8xx_remove_from_gtt(gtt_impl_t *gtt, igd_gtt_seg_t seg)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	int i;
18967c478bd9Sstevel@tonic-gate 	uint32_t maxpages;
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	maxpages = gtt->gtt_info.igd_apersize;
18997c478bd9Sstevel@tonic-gate 	maxpages = GTT_MB_TO_PAGES(maxpages);
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	/*
19027c478bd9Sstevel@tonic-gate 	 * check if gtt max pages reached
19037c478bd9Sstevel@tonic-gate 	 */
19047c478bd9Sstevel@tonic-gate 	if ((seg.igs_pgstart + seg.igs_npage) > maxpages)
19057c478bd9Sstevel@tonic-gate 		return;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	for (i = seg.igs_pgstart; i < (seg.igs_pgstart + seg.igs_npage); i++) {
19087c478bd9Sstevel@tonic-gate 		ddi_put32(gtt->gtt_mmio_handle,
19097c478bd9Sstevel@tonic-gate 		    (uint32_t *)(gtt->gtt_addr +
19107c478bd9Sstevel@tonic-gate 		    i * sizeof (uint32_t)),
19117c478bd9Sstevel@tonic-gate 		    0);
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate }
1914