xref: /freebsd/sys/dev/fb/vesa.c (revision 205d67b00d81bcc0c8e84f20f95b26ed94acb923)
1f45c063aSXin LI /*-
2f45c063aSXin LI  * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3f45c063aSXin LI  * All rights reserved.
4f45c063aSXin LI  *
5f45c063aSXin LI  * Redistribution and use in source and binary forms, with or without
6f45c063aSXin LI  * modification, are permitted provided that the following conditions
7f45c063aSXin LI  * are met:
8f45c063aSXin LI  * 1. Redistributions of source code must retain the above copyright
9f45c063aSXin LI  *    notice, this list of conditions and the following disclaimer as
10f45c063aSXin LI  *    the first lines of this file unmodified.
11f45c063aSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12f45c063aSXin LI  *    notice, this list of conditions and the following disclaimer in the
13f45c063aSXin LI  *    documentation and/or other materials provided with the distribution.
14f45c063aSXin LI  *
15f45c063aSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16f45c063aSXin LI  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f45c063aSXin LI  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f45c063aSXin LI  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19f45c063aSXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f45c063aSXin LI  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f45c063aSXin LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f45c063aSXin LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f45c063aSXin LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f45c063aSXin LI  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f45c063aSXin LI  */
26f45c063aSXin LI 
27f45c063aSXin LI #include <sys/cdefs.h>
28f45c063aSXin LI __FBSDID("$FreeBSD$");
29f45c063aSXin LI 
30f45c063aSXin LI #include "opt_vga.h"
31f45c063aSXin LI #include "opt_vesa.h"
32f45c063aSXin LI 
33f45c063aSXin LI #ifndef VGA_NO_MODE_CHANGE
34f45c063aSXin LI 
35f45c063aSXin LI #include <sys/param.h>
36f45c063aSXin LI #include <sys/systm.h>
37f45c063aSXin LI #include <sys/kernel.h>
38f45c063aSXin LI #include <sys/module.h>
39f45c063aSXin LI #include <sys/malloc.h>
40f45c063aSXin LI #include <sys/fbio.h>
41f45c063aSXin LI 
42f45c063aSXin LI #include <vm/vm.h>
43f45c063aSXin LI #include <vm/vm_extern.h>
44f45c063aSXin LI #include <vm/vm_kern.h>
45f45c063aSXin LI #include <vm/vm_param.h>
46f45c063aSXin LI #include <vm/pmap.h>
47f45c063aSXin LI 
48205d67b0SXin LI #include <machine/pc/bios.h>
49ee5e90daSXin LI #include <dev/fb/vesa.h>
50f45c063aSXin LI 
51f45c063aSXin LI #include <dev/fb/fbreg.h>
52f45c063aSXin LI #include <dev/fb/vgareg.h>
53f45c063aSXin LI 
54f45c063aSXin LI #include <isa/isareg.h>
55ee5e90daSXin LI 
56205d67b0SXin LI #include <dev/x86bios/x86bios.h>
57f45c063aSXin LI 
58f45c063aSXin LI #define	VESA_VIA_CLE266		"VIA CLE266\r\n"
59f45c063aSXin LI 
60f45c063aSXin LI #ifndef VESA_DEBUG
61f45c063aSXin LI #define VESA_DEBUG	0
62f45c063aSXin LI #endif
63f45c063aSXin LI 
64f45c063aSXin LI /* VESA video adapter state buffer stub */
65f45c063aSXin LI struct adp_state {
66f45c063aSXin LI 	int		sig;
67f45c063aSXin LI #define V_STATE_SIG	0x61736576
68f45c063aSXin LI 	u_char		regs[1];
69f45c063aSXin LI };
70f45c063aSXin LI typedef struct adp_state adp_state_t;
71f45c063aSXin LI 
72f45c063aSXin LI /* VESA video adapter */
73f45c063aSXin LI static video_adapter_t *vesa_adp = NULL;
74f45c063aSXin LI static int vesa_state_buf_size = 0;
75205d67b0SXin LI #define VESA_BIOS_BUFSIZE	(3 * PAGE_SIZE)
76f45c063aSXin LI 
77f45c063aSXin LI /* VESA functions */
78f45c063aSXin LI #if 0
79f45c063aSXin LI static int			vesa_nop(void);
80f45c063aSXin LI #endif
81f45c063aSXin LI static int			vesa_error(void);
82f45c063aSXin LI static vi_probe_t		vesa_probe;
83f45c063aSXin LI static vi_init_t		vesa_init;
84f45c063aSXin LI static vi_get_info_t		vesa_get_info;
85f45c063aSXin LI static vi_query_mode_t		vesa_query_mode;
86f45c063aSXin LI static vi_set_mode_t		vesa_set_mode;
87f45c063aSXin LI static vi_save_font_t		vesa_save_font;
88f45c063aSXin LI static vi_load_font_t		vesa_load_font;
89f45c063aSXin LI static vi_show_font_t		vesa_show_font;
90f45c063aSXin LI static vi_save_palette_t	vesa_save_palette;
91f45c063aSXin LI static vi_load_palette_t	vesa_load_palette;
92f45c063aSXin LI static vi_set_border_t		vesa_set_border;
93f45c063aSXin LI static vi_save_state_t		vesa_save_state;
94f45c063aSXin LI static vi_load_state_t		vesa_load_state;
95f45c063aSXin LI static vi_set_win_org_t		vesa_set_origin;
96f45c063aSXin LI static vi_read_hw_cursor_t	vesa_read_hw_cursor;
97f45c063aSXin LI static vi_set_hw_cursor_t	vesa_set_hw_cursor;
98f45c063aSXin LI static vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
99f45c063aSXin LI static vi_blank_display_t	vesa_blank_display;
100f45c063aSXin LI static vi_mmap_t		vesa_mmap;
101f45c063aSXin LI static vi_ioctl_t		vesa_ioctl;
102f45c063aSXin LI static vi_clear_t		vesa_clear;
103f45c063aSXin LI static vi_fill_rect_t		vesa_fill_rect;
104f45c063aSXin LI static vi_bitblt_t		vesa_bitblt;
105f45c063aSXin LI static vi_diag_t		vesa_diag;
106f45c063aSXin LI static int			vesa_bios_info(int level);
107ee5e90daSXin LI 
108f45c063aSXin LI static video_switch_t vesavidsw = {
109f45c063aSXin LI 	vesa_probe,
110f45c063aSXin LI 	vesa_init,
111f45c063aSXin LI 	vesa_get_info,
112f45c063aSXin LI 	vesa_query_mode,
113f45c063aSXin LI 	vesa_set_mode,
114f45c063aSXin LI 	vesa_save_font,
115f45c063aSXin LI 	vesa_load_font,
116f45c063aSXin LI 	vesa_show_font,
117f45c063aSXin LI 	vesa_save_palette,
118f45c063aSXin LI 	vesa_load_palette,
119f45c063aSXin LI 	vesa_set_border,
120f45c063aSXin LI 	vesa_save_state,
121f45c063aSXin LI 	vesa_load_state,
122f45c063aSXin LI 	vesa_set_origin,
123f45c063aSXin LI 	vesa_read_hw_cursor,
124f45c063aSXin LI 	vesa_set_hw_cursor,
125f45c063aSXin LI 	vesa_set_hw_cursor_shape,
126f45c063aSXin LI 	vesa_blank_display,
127f45c063aSXin LI 	vesa_mmap,
128f45c063aSXin LI 	vesa_ioctl,
129f45c063aSXin LI 	vesa_clear,
130f45c063aSXin LI 	vesa_fill_rect,
131f45c063aSXin LI 	vesa_bitblt,
132f45c063aSXin LI 	vesa_error,
133f45c063aSXin LI 	vesa_error,
134f45c063aSXin LI 	vesa_diag,
135f45c063aSXin LI };
136f45c063aSXin LI 
137f45c063aSXin LI static video_switch_t *prevvidsw;
138f45c063aSXin LI 
139f45c063aSXin LI /* VESA BIOS video modes */
140f45c063aSXin LI #define VESA_MAXMODES	64
141f45c063aSXin LI #define EOT		(-1)
142f45c063aSXin LI #define NA		(-2)
143f45c063aSXin LI 
144f45c063aSXin LI #define MODE_TABLE_DELTA 8
145f45c063aSXin LI 
146f45c063aSXin LI static int vesa_vmode_max = 0;
147f45c063aSXin LI static video_info_t vesa_vmode_empty = { EOT };
148f45c063aSXin LI static video_info_t *vesa_vmode = &vesa_vmode_empty;
149f45c063aSXin LI 
150f45c063aSXin LI static int vesa_init_done = FALSE;
151f45c063aSXin LI static int has_vesa_bios = FALSE;
152f45c063aSXin LI static struct vesa_info *vesa_adp_info = NULL;
153f45c063aSXin LI static u_int16_t *vesa_vmodetab = NULL;
154f45c063aSXin LI static char *vesa_oemstr = NULL;
155f45c063aSXin LI static char *vesa_venderstr = NULL;
156f45c063aSXin LI static char *vesa_prodstr = NULL;
157f45c063aSXin LI static char *vesa_revstr = NULL;
158f45c063aSXin LI 
159f45c063aSXin LI /* local macros and functions */
160f45c063aSXin LI #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
161f45c063aSXin LI 
162f45c063aSXin LI static int int10_set_mode(int mode);
163f45c063aSXin LI static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
164f45c063aSXin LI static int vesa_bios_set_mode(int mode);
165f45c063aSXin LI static int vesa_bios_get_dac(void);
166f45c063aSXin LI static int vesa_bios_set_dac(int bits);
167f45c063aSXin LI static int vesa_bios_save_palette(int start, int colors, u_char *palette,
168f45c063aSXin LI 				  int bits);
169f45c063aSXin LI static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
170f45c063aSXin LI 				   u_char *b, int bits);
171f45c063aSXin LI static int vesa_bios_load_palette(int start, int colors, u_char *palette,
172f45c063aSXin LI 				  int bits);
173f45c063aSXin LI #ifdef notyet
174f45c063aSXin LI static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
175f45c063aSXin LI 				   u_char *b, int bits);
176f45c063aSXin LI #endif
177f45c063aSXin LI #define STATE_SIZE	0
178f45c063aSXin LI #define STATE_SAVE	1
179f45c063aSXin LI #define STATE_LOAD	2
180f45c063aSXin LI #define STATE_HW	(1<<0)
181f45c063aSXin LI #define STATE_DATA	(1<<1)
182f45c063aSXin LI #define STATE_DAC	(1<<2)
183f45c063aSXin LI #define STATE_REG	(1<<3)
184f45c063aSXin LI #define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
185f45c063aSXin LI #define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
186f45c063aSXin LI static int vesa_bios_state_buf_size(void);
187f45c063aSXin LI static int vesa_bios_save_restore(int code, void *p, size_t size);
188f45c063aSXin LI static int vesa_bios_get_line_length(void);
189f45c063aSXin LI static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
190f45c063aSXin LI #if 0
191f45c063aSXin LI static int vesa_bios_get_start(int *x, int *y);
192f45c063aSXin LI #endif
193f45c063aSXin LI static int vesa_bios_set_start(int x, int y);
194f45c063aSXin LI static int vesa_map_gen_mode_num(int type, int color, int mode);
195f45c063aSXin LI static int vesa_translate_flags(u_int16_t vflags);
196f45c063aSXin LI static int vesa_translate_mmodel(u_int8_t vmodel);
197f45c063aSXin LI static int vesa_bios_init(void);
198f45c063aSXin LI static void vesa_clear_modes(video_info_t *info, int color);
199f45c063aSXin LI static vm_offset_t vesa_map_buffer(u_int paddr, size_t size);
200f45c063aSXin LI static void vesa_unmap_buffer(vm_offset_t vaddr, size_t size);
201f45c063aSXin LI 
202f45c063aSXin LI #if 0
203f45c063aSXin LI static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
204f45c063aSXin LI #endif
205f45c063aSXin LI 
206f45c063aSXin LI static void
207f45c063aSXin LI dump_buffer(u_char *buf, size_t len)
208f45c063aSXin LI {
209f45c063aSXin LI     int i;
210f45c063aSXin LI 
211f45c063aSXin LI     for(i = 0; i < len;) {
212f45c063aSXin LI 	printf("%02x ", buf[i]);
213f45c063aSXin LI 	if ((++i % 16) == 0)
214f45c063aSXin LI 	    printf("\n");
215f45c063aSXin LI     }
216f45c063aSXin LI }
217f45c063aSXin LI 
218f45c063aSXin LI /* INT 10 BIOS calls */
219f45c063aSXin LI static int
220f45c063aSXin LI int10_set_mode(int mode)
221f45c063aSXin LI {
222205d67b0SXin LI 	x86regs_t regs;
223205d67b0SXin LI 
224205d67b0SXin LI 	regs.R_EAX = 0x0000 | mode;
225205d67b0SXin LI 
226205d67b0SXin LI 	x86biosCall(&regs, 0x10);
227f45c063aSXin LI 
228f45c063aSXin LI 	return 0;
229f45c063aSXin LI }
230f45c063aSXin LI 
231f45c063aSXin LI /* VESA BIOS calls */
232f45c063aSXin LI static int
233f45c063aSXin LI vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
234f45c063aSXin LI {
235205d67b0SXin LI 	x86regs_t regs;
236205d67b0SXin LI 	int offs;
237f45c063aSXin LI 	u_char *buf;
238f45c063aSXin LI 
239205d67b0SXin LI 	regs.R_EAX = 0x4f01;
240205d67b0SXin LI 	regs.R_ECX = mode;
241f45c063aSXin LI 
242205d67b0SXin LI 	buf = (u_char *)x86biosAlloc(1, &offs);
243ee5e90daSXin LI 
244205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
245205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
246ee5e90daSXin LI 
247205d67b0SXin LI 	x86biosCall(&regs, 0x10);
248205d67b0SXin LI 
249205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
250205d67b0SXin LI 	{
251205d67b0SXin LI 		x86biosFree(buf, 1);
252f45c063aSXin LI 		return 1;
253205d67b0SXin LI 	}
254ee5e90daSXin LI 
255f45c063aSXin LI 	bcopy(buf, vmode, sizeof(*vmode));
256205d67b0SXin LI 	x86biosFree(buf, 1);
257ee5e90daSXin LI 
258f45c063aSXin LI 	return 0;
259f45c063aSXin LI }
260f45c063aSXin LI 
261f45c063aSXin LI static int
262f45c063aSXin LI vesa_bios_set_mode(int mode)
263f45c063aSXin LI {
264205d67b0SXin LI 	x86regs_t regs;
265f45c063aSXin LI 
266205d67b0SXin LI 	regs.R_EAX = 0x4f02;
267205d67b0SXin LI 	regs.R_EBX = mode;
268ee5e90daSXin LI 
269205d67b0SXin LI 	x86biosCall(&regs, 0x10);
270205d67b0SXin LI 
271205d67b0SXin LI 	return ((regs.R_AX & 0xff) != 0x4f);
272f45c063aSXin LI }
273f45c063aSXin LI 
274f45c063aSXin LI static int
275f45c063aSXin LI vesa_bios_get_dac(void)
276f45c063aSXin LI {
277205d67b0SXin LI 	x86regs_t regs;
278f45c063aSXin LI 
279205d67b0SXin LI 	regs.R_EAX = 0x4f08;
280205d67b0SXin LI 	regs.R_EBX = 1;
281ee5e90daSXin LI 
282205d67b0SXin LI 	x86biosCall(&regs, 0x10);
283205d67b0SXin LI 
284205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
285ee5e90daSXin LI 		return 6;
286ee5e90daSXin LI 
287205d67b0SXin LI 	return ((regs.R_EBX >> 8) & 0x00ff);
288f45c063aSXin LI }
289f45c063aSXin LI 
290f45c063aSXin LI static int
291f45c063aSXin LI vesa_bios_set_dac(int bits)
292f45c063aSXin LI {
293205d67b0SXin LI 	x86regs_t regs;
294f45c063aSXin LI 
295205d67b0SXin LI 	regs.R_EAX = 0x4f08;
296205d67b0SXin LI 	regs.R_EBX = (bits << 8);
297ee5e90daSXin LI 
298205d67b0SXin LI 	x86biosCall(&regs, 0x10);
299205d67b0SXin LI 
300205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
301ee5e90daSXin LI 		return 6;
302ee5e90daSXin LI 
303205d67b0SXin LI 	return ((regs.R_EBX >> 8) & 0x00ff);
304f45c063aSXin LI }
305f45c063aSXin LI 
306f45c063aSXin LI static int
307f45c063aSXin LI vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
308f45c063aSXin LI {
309205d67b0SXin LI 	x86regs_t regs;
310205d67b0SXin LI 	int offs;
311f45c063aSXin LI 	u_char *p;
312f45c063aSXin LI 	int i;
313f45c063aSXin LI 
314205d67b0SXin LI 	regs.R_EAX = 0x4f09;
315205d67b0SXin LI 	regs.R_EBX = 1;
316205d67b0SXin LI 	regs.R_ECX = colors;
317205d67b0SXin LI 	regs.R_EDX = start;
318f45c063aSXin LI 
319205d67b0SXin LI 	p = (u_char *)x86biosAlloc(1, &offs);
320ee5e90daSXin LI 
321205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
322205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
323ee5e90daSXin LI 
324205d67b0SXin LI 	x86biosCall(&regs, 0x10);
325ee5e90daSXin LI 
326205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
327205d67b0SXin LI 	{
328205d67b0SXin LI 		x86biosFree(p, 1);
329f45c063aSXin LI 		return 1;
330205d67b0SXin LI 	}
331f45c063aSXin LI 
332f45c063aSXin LI 	bits = 8 - bits;
333f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
334f45c063aSXin LI 		palette[i*3]     = p[i*4 + 2] << bits;
335f45c063aSXin LI 		palette[i*3 + 1] = p[i*4 + 1] << bits;
336f45c063aSXin LI 		palette[i*3 + 2] = p[i*4] << bits;
337f45c063aSXin LI 	}
338205d67b0SXin LI 
339205d67b0SXin LI 	x86biosFree(p, 1);
340f45c063aSXin LI 	return 0;
341f45c063aSXin LI }
342f45c063aSXin LI 
343f45c063aSXin LI static int
344f45c063aSXin LI vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
345f45c063aSXin LI 			int bits)
346f45c063aSXin LI {
347205d67b0SXin LI 	x86regs_t regs;
348205d67b0SXin LI 	int offs;
349f45c063aSXin LI 	u_char *p;
350f45c063aSXin LI 	int i;
351f45c063aSXin LI 
352205d67b0SXin LI 	regs.R_EAX = 0x4f09;
353205d67b0SXin LI 	regs.R_EBX = 1;
354205d67b0SXin LI 	regs.R_ECX = colors;
355205d67b0SXin LI 	regs.R_EDX = start;
356f45c063aSXin LI 
357205d67b0SXin LI 	p = (u_char *)x86biosAlloc(1, &offs);
358ee5e90daSXin LI 
359205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
360205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
361ee5e90daSXin LI 
362205d67b0SXin LI 	x86biosCall(&regs, 0x10);
363ee5e90daSXin LI 
364205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
365205d67b0SXin LI 	{
366205d67b0SXin LI 		x86biosFree(p, 1);
367f45c063aSXin LI 		return 1;
368205d67b0SXin LI 	}
369f45c063aSXin LI 
370f45c063aSXin LI 	bits = 8 - bits;
371f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
372f45c063aSXin LI 		r[i] = p[i*4 + 2] << bits;
373f45c063aSXin LI 		g[i] = p[i*4 + 1] << bits;
374f45c063aSXin LI 		b[i] = p[i*4] << bits;
375f45c063aSXin LI 	}
376205d67b0SXin LI 
377205d67b0SXin LI 	x86biosFree(p, 1);
378f45c063aSXin LI 	return 0;
379f45c063aSXin LI }
380f45c063aSXin LI 
381f45c063aSXin LI static int
382f45c063aSXin LI vesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
383f45c063aSXin LI {
384205d67b0SXin LI 	x86regs_t regs;
385205d67b0SXin LI 	int offs;
386f45c063aSXin LI 	u_char *p;
387f45c063aSXin LI 	int i;
388f45c063aSXin LI 
389205d67b0SXin LI 	p = (u_char *)x86biosAlloc(1, &offs);
390ee5e90daSXin LI 
391f45c063aSXin LI 	bits = 8 - bits;
392f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
393f45c063aSXin LI 		p[i*4]	   = palette[i*3 + 2] >> bits;
394f45c063aSXin LI 		p[i*4 + 1] = palette[i*3 + 1] >> bits;
395f45c063aSXin LI 		p[i*4 + 2] = palette[i*3] >> bits;
396f45c063aSXin LI 		p[i*4 + 3] = 0;
397f45c063aSXin LI 	}
398f45c063aSXin LI 
399205d67b0SXin LI 	regs.R_EAX = 0x4f09;
400205d67b0SXin LI 	regs.R_EBX = 0;
401205d67b0SXin LI 	regs.R_ECX = colors;
402205d67b0SXin LI 	regs.R_EDX = start;
403f45c063aSXin LI 
404205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
405205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
406ee5e90daSXin LI 
407205d67b0SXin LI 	x86biosCall(&regs, 0x10);
408ee5e90daSXin LI 
409205d67b0SXin LI 	x86biosFree(p, 1);
410205d67b0SXin LI 
411205d67b0SXin LI 	return ((regs.R_AX & 0xff) != 0x4f);
412f45c063aSXin LI }
413f45c063aSXin LI 
414f45c063aSXin LI #ifdef notyet
415f45c063aSXin LI static int
416f45c063aSXin LI vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
417f45c063aSXin LI 			int bits)
418f45c063aSXin LI {
419205d67b0SXin LI 	x86regs_t regs;
420205d67b0SXin LI 	int offs;
421f45c063aSXin LI 	u_char *p;
422f45c063aSXin LI 	int i;
423f45c063aSXin LI 
424205d67b0SXin LI 	p = (u_char *)x86biosAlloc(1, &offs);
425ee5e90daSXin LI 
426f45c063aSXin LI 	bits = 8 - bits;
427f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
428f45c063aSXin LI 		p[i*4]	   = b[i] >> bits;
429f45c063aSXin LI 		p[i*4 + 1] = g[i] >> bits;
430f45c063aSXin LI 		p[i*4 + 2] = r[i] >> bits;
431f45c063aSXin LI 		p[i*4 + 3] = 0;
432f45c063aSXin LI 	}
433f45c063aSXin LI 
434205d67b0SXin LI 	regs.R_EAX = 0x4f09;
435205d67b0SXin LI 	regs.R_EBX = 0;
436205d67b0SXin LI 	regs.R_ECX = colors;
437205d67b0SXin LI 	regs.R_EDX = start;
438f45c063aSXin LI 
439205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
440205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
441ee5e90daSXin LI 
442205d67b0SXin LI 	x86biosCall(&regs, 0x10);
443ee5e90daSXin LI 
444205d67b0SXin LI 	x86biosFree(p, 1);
445205d67b0SXin LI 
446205d67b0SXin LI 	return ((regs.R_AX & 0xff) != 0x4f);
447f45c063aSXin LI }
448f45c063aSXin LI #endif
449f45c063aSXin LI 
450f45c063aSXin LI static int
451f45c063aSXin LI vesa_bios_state_buf_size(void)
452f45c063aSXin LI {
453205d67b0SXin LI 	x86regs_t regs;
454f45c063aSXin LI 
455205d67b0SXin LI 	regs.R_EAX = 0x4f04;
456205d67b0SXin LI 	regs.R_ECX = STATE_ALL;
457205d67b0SXin LI 	regs.R_EDX = STATE_SIZE;
458ee5e90daSXin LI 
459205d67b0SXin LI 	x86biosCall(&regs, 0x10);
460205d67b0SXin LI 
461205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
462f45c063aSXin LI 		return 0;
463ee5e90daSXin LI 
464205d67b0SXin LI 	return regs.R_BX*64;
465f45c063aSXin LI }
466f45c063aSXin LI 
467f45c063aSXin LI static int
468f45c063aSXin LI vesa_bios_save_restore(int code, void *p, size_t size)
469f45c063aSXin LI {
470205d67b0SXin LI 	x86regs_t regs;
471205d67b0SXin LI 	int offs;
472f45c063aSXin LI 	u_char *buf;
473f45c063aSXin LI 
474205d67b0SXin LI 	if (size > VESA_BIOS_BUFSIZE)
475f45c063aSXin LI 		return (1);
476f45c063aSXin LI 
477205d67b0SXin LI 	regs.R_EAX = 0x4f04;
478205d67b0SXin LI 	regs.R_ECX = STATE_ALL;
479205d67b0SXin LI 	regs.R_EDX = code;
480ee5e90daSXin LI 
481205d67b0SXin LI 	buf = (u_char *)x86biosAlloc(1, &offs);
482ee5e90daSXin LI 
483205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
484205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
485ee5e90daSXin LI 
486f45c063aSXin LI 	bcopy(p, buf, size);
487f45c063aSXin LI 
488205d67b0SXin LI 	x86biosCall(&regs, 0x10);
489ee5e90daSXin LI 
490f45c063aSXin LI 	bcopy(buf, p, size);
491ee5e90daSXin LI 
492205d67b0SXin LI 	x86biosFree(p, 1);
493205d67b0SXin LI 
494205d67b0SXin LI 	return ((regs.R_AX & 0xff) != 0x4f);
495f45c063aSXin LI }
496f45c063aSXin LI 
497f45c063aSXin LI static int
498f45c063aSXin LI vesa_bios_get_line_length(void)
499f45c063aSXin LI {
500205d67b0SXin LI 	x86regs_t regs;
501f45c063aSXin LI 
502205d67b0SXin LI 	regs.R_EAX = 0x4f06;
503205d67b0SXin LI 	regs.R_EBX = 1;
504ee5e90daSXin LI 
505205d67b0SXin LI 	x86biosCall(&regs, 0x10);
506205d67b0SXin LI 
507205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
508f45c063aSXin LI 		return -1;
509205d67b0SXin LI 
510205d67b0SXin LI 	return regs.R_BX;
511f45c063aSXin LI }
512f45c063aSXin LI 
513f45c063aSXin LI static int
514f45c063aSXin LI vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
515f45c063aSXin LI {
516205d67b0SXin LI 	x86regs_t regs;
517f45c063aSXin LI 
518205d67b0SXin LI 	regs.R_EAX = 0x4f06;
519205d67b0SXin LI 	regs.R_EBX = 0;
520205d67b0SXin LI 	regs.R_ECX = pixel;
521205d67b0SXin LI 
522205d67b0SXin LI 	x86biosCall(&regs, 0x10);
523ee5e90daSXin LI 
524f45c063aSXin LI #if VESA_DEBUG > 1
525205d67b0SXin LI 	printf("bx:%d, cx:%d, dx:%d\n", regs.R_BX, regs.R_CX, regs.R_DX);
526f45c063aSXin LI #endif
527205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
528ee5e90daSXin LI 		return -1;
529ee5e90daSXin LI 
530f45c063aSXin LI 	if (bytes)
531205d67b0SXin LI 		*bytes = regs.R_BX;
532f45c063aSXin LI 	if (lines)
533205d67b0SXin LI 		*lines = regs.R_DX;
534ee5e90daSXin LI 
535f45c063aSXin LI 	return 0;
536f45c063aSXin LI }
537f45c063aSXin LI 
538f45c063aSXin LI #if 0
539f45c063aSXin LI static int
540f45c063aSXin LI vesa_bios_get_start(int *x, int *y)
541f45c063aSXin LI {
542205d67b0SXin LI 	x86regs_t regs;
543f45c063aSXin LI 
544205d67b0SXin LI 	regs.R_EAX = 0x4f07;
545205d67b0SXin LI 	regs.R_EBX = 1;
546ee5e90daSXin LI 
547205d67b0SXin LI 	x86biosCall(&regs, 0x10);
548205d67b0SXin LI 
549205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
550ee5e90daSXin LI 		return -1;
551ee5e90daSXin LI 
552205d67b0SXin LI 	*x = regs.R_CX;
553205d67b0SXin LI 	*y = regs.R_DX;
554ee5e90daSXin LI 
555f45c063aSXin LI 	return 0;
556f45c063aSXin LI }
557f45c063aSXin LI #endif
558f45c063aSXin LI 
559f45c063aSXin LI static int
560f45c063aSXin LI vesa_bios_set_start(int x, int y)
561f45c063aSXin LI {
562205d67b0SXin LI 	x86regs_t regs;
563f45c063aSXin LI 
564205d67b0SXin LI 	regs.R_EAX = 0x4f07;
565205d67b0SXin LI 	regs.R_EBX = 0x80;
566205d67b0SXin LI 	regs.R_EDX = y;
567205d67b0SXin LI 	regs.R_ECX = x;
568ee5e90daSXin LI 
569205d67b0SXin LI 	x86biosCall(&regs, 0x10);
570205d67b0SXin LI 
571205d67b0SXin LI 	return ((regs.R_AX & 0xff) != 0x4f);
572f45c063aSXin LI }
573f45c063aSXin LI 
574f45c063aSXin LI /* map a generic video mode to a known mode */
575f45c063aSXin LI static int
576f45c063aSXin LI vesa_map_gen_mode_num(int type, int color, int mode)
577f45c063aSXin LI {
578f45c063aSXin LI     static struct {
579f45c063aSXin LI 	int from;
580f45c063aSXin LI 	int to;
581f45c063aSXin LI     } mode_map[] = {
582f45c063aSXin LI 	{ M_TEXT_132x25, M_VESA_C132x25 },
583f45c063aSXin LI 	{ M_TEXT_132x43, M_VESA_C132x43 },
584f45c063aSXin LI 	{ M_TEXT_132x50, M_VESA_C132x50 },
585f45c063aSXin LI 	{ M_TEXT_132x60, M_VESA_C132x60 },
586f45c063aSXin LI     };
587f45c063aSXin LI     int i;
588f45c063aSXin LI 
589f45c063aSXin LI     for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
590f45c063aSXin LI         if (mode_map[i].from == mode)
591f45c063aSXin LI             return mode_map[i].to;
592f45c063aSXin LI     }
593f45c063aSXin LI     return mode;
594f45c063aSXin LI }
595f45c063aSXin LI 
596f45c063aSXin LI static int
597f45c063aSXin LI vesa_translate_flags(u_int16_t vflags)
598f45c063aSXin LI {
599f45c063aSXin LI 	static struct {
600f45c063aSXin LI 		u_int16_t mask;
601f45c063aSXin LI 		int set;
602f45c063aSXin LI 		int reset;
603f45c063aSXin LI 	} ftable[] = {
604f45c063aSXin LI 		{ V_MODECOLOR, V_INFO_COLOR, 0 },
605f45c063aSXin LI 		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
606f45c063aSXin LI 		{ V_MODELFB, V_INFO_LINEAR, 0 },
607f45c063aSXin LI 	};
608f45c063aSXin LI 	int flags;
609f45c063aSXin LI 	int i;
610f45c063aSXin LI 
611f45c063aSXin LI 	for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
612f45c063aSXin LI 		flags |= (vflags & ftable[i].mask) ?
613f45c063aSXin LI 			 ftable[i].set : ftable[i].reset;
614f45c063aSXin LI 	}
615f45c063aSXin LI 	return flags;
616f45c063aSXin LI }
617f45c063aSXin LI 
618f45c063aSXin LI static int
619f45c063aSXin LI vesa_translate_mmodel(u_int8_t vmodel)
620f45c063aSXin LI {
621f45c063aSXin LI 	static struct {
622f45c063aSXin LI 		u_int8_t vmodel;
623f45c063aSXin LI 		int mmodel;
624f45c063aSXin LI 	} mtable[] = {
625f45c063aSXin LI 		{ V_MMTEXT,	V_INFO_MM_TEXT },
626f45c063aSXin LI 		{ V_MMCGA,	V_INFO_MM_CGA },
627f45c063aSXin LI 		{ V_MMHGC,	V_INFO_MM_HGC },
628f45c063aSXin LI 		{ V_MMEGA,	V_INFO_MM_PLANAR },
629f45c063aSXin LI 		{ V_MMPACKED,	V_INFO_MM_PACKED },
630f45c063aSXin LI 		{ V_MMDIRCOLOR,	V_INFO_MM_DIRECT },
631f45c063aSXin LI 	};
632f45c063aSXin LI 	int i;
633f45c063aSXin LI 
634f45c063aSXin LI 	for (i = 0; mtable[i].mmodel >= 0; ++i) {
635f45c063aSXin LI 		if (mtable[i].vmodel == vmodel)
636f45c063aSXin LI 			return mtable[i].mmodel;
637f45c063aSXin LI 	}
638f45c063aSXin LI 	return V_INFO_MM_OTHER;
639f45c063aSXin LI }
640f45c063aSXin LI 
641f45c063aSXin LI static int
642f45c063aSXin LI vesa_bios_init(void)
643f45c063aSXin LI {
644ee5e90daSXin LI 	static struct vesa_info buf;
645f45c063aSXin LI 	struct vesa_mode vmode;
646f45c063aSXin LI 	video_info_t *p;
647205d67b0SXin LI 	x86regs_t regs;
648205d67b0SXin LI 	int offs;
649f45c063aSXin LI 	u_char *vmbuf;
650f45c063aSXin LI 	int is_via_cle266;
651f45c063aSXin LI 	int modes;
652f45c063aSXin LI 	int i;
653f45c063aSXin LI 
654f45c063aSXin LI 	if (vesa_init_done)
655f45c063aSXin LI 		return 0;
656f45c063aSXin LI 
657f45c063aSXin LI 	has_vesa_bios = FALSE;
658f45c063aSXin LI 	vesa_adp_info = NULL;
659f45c063aSXin LI 	vesa_vmode_max = 0;
660f45c063aSXin LI 	vesa_vmode[0].vi_mode = EOT;
661f45c063aSXin LI 
662205d67b0SXin LI 	vmbuf = (u_char *)x86biosAlloc(1, &offs);
663f45c063aSXin LI 	bcopy("VBE2", vmbuf, 4);	/* try for VBE2 data */
664f45c063aSXin LI 
665205d67b0SXin LI 	regs.R_EAX = 0x4f00;
666205d67b0SXin LI 	regs.R_ES = SEG_ADDR(offs);
667205d67b0SXin LI 	regs.R_DI = SEG_OFF(offs);
668ee5e90daSXin LI 
669205d67b0SXin LI 	x86biosCall(&regs, 0x10);
670ee5e90daSXin LI 
671205d67b0SXin LI 	if (((regs.R_AX & 0xff) != 0x4f) || bcmp("VESA", vmbuf, 4))
672f45c063aSXin LI 		return 1;
673ee5e90daSXin LI 
674ee5e90daSXin LI 	bcopy(vmbuf, &buf, sizeof(buf));
675ee5e90daSXin LI 
676ee5e90daSXin LI 	vesa_adp_info = &buf;
677f45c063aSXin LI 	if (bootverbose) {
678f45c063aSXin LI 		printf("VESA: information block\n");
679ee5e90daSXin LI 		dump_buffer((u_char *)&buf, sizeof(buf));
680f45c063aSXin LI 	}
681f45c063aSXin LI 	if (vesa_adp_info->v_version < 0x0102) {
682f45c063aSXin LI 		printf("VESA: VBE version %d.%d is not supported; "
683f45c063aSXin LI 		       "version 1.2 or later is required.\n",
684f45c063aSXin LI 		       ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
685f45c063aSXin LI 			   + ((vesa_adp_info->v_version & 0x0f00) >> 8),
686f45c063aSXin LI 		       ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
687f45c063aSXin LI 			   + (vesa_adp_info->v_version & 0x000f));
688f45c063aSXin LI 		return 1;
689f45c063aSXin LI 	}
690f45c063aSXin LI 
691205d67b0SXin LI 	vesa_oemstr = (char *)x86biosOffs(FARP(vesa_adp_info->v_oemstr));
692ee5e90daSXin LI 
693f45c063aSXin LI 	is_via_cle266 = strcmp(vesa_oemstr, VESA_VIA_CLE266) == 0;
694f45c063aSXin LI 
695f45c063aSXin LI 	if (vesa_adp_info->v_version >= 0x0200) {
696205d67b0SXin LI 		vesa_venderstr = (char *)x86biosOffs(FARP(vesa_adp_info->v_venderstr));
697205d67b0SXin LI 		vesa_prodstr = (char *)x86biosOffs(FARP(vesa_adp_info->v_prodstr));
698205d67b0SXin LI 		vesa_revstr = (char *)x86biosOffs(FARP(vesa_adp_info->v_revstr));
699f45c063aSXin LI 	}
700f45c063aSXin LI 
701205d67b0SXin LI 	vesa_vmodetab = (u_int16_t *)x86biosOffs(FARP(vesa_adp_info->v_modetable));
702ee5e90daSXin LI 
703f45c063aSXin LI 	if (vesa_vmodetab == NULL)
704f45c063aSXin LI 		return 1;
705ee5e90daSXin LI 
706f45c063aSXin LI 	for (i = 0, modes = 0;
707f45c063aSXin LI 		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
708f45c063aSXin LI 		&& (vesa_vmodetab[i] != 0xffff); ++i) {
709f45c063aSXin LI 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
710f45c063aSXin LI 			continue;
711f45c063aSXin LI 
712f45c063aSXin LI 		/* reject unsupported modes */
713f45c063aSXin LI #if 0
714f45c063aSXin LI 		if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
715f45c063aSXin LI 					| V_MODENONVGA))
716f45c063aSXin LI 		    != (V_MODESUPP | V_MODEOPTINFO))
717f45c063aSXin LI 			continue;
718f45c063aSXin LI #else
719f45c063aSXin LI 		if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
720f45c063aSXin LI #if VESA_DEBUG > 1
721f45c063aSXin LI 			printf(
722f45c063aSXin LI 		"Rejecting VESA %s mode: %d x %d x %d bpp  attr = %x\n",
723f45c063aSXin LI 			    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
724f45c063aSXin LI 			    vmode.v_width, vmode.v_height, vmode.v_bpp,
725f45c063aSXin LI 			    vmode.v_modeattr);
726f45c063aSXin LI #endif
727f45c063aSXin LI 			continue;
728f45c063aSXin LI 		}
729f45c063aSXin LI #endif
730f45c063aSXin LI 
731f45c063aSXin LI 		/* expand the array if necessary */
732f45c063aSXin LI 		if (modes >= vesa_vmode_max) {
733f45c063aSXin LI 			vesa_vmode_max += MODE_TABLE_DELTA;
734f45c063aSXin LI 			p = malloc(sizeof(*vesa_vmode)*(vesa_vmode_max + 1),
735f45c063aSXin LI 				   M_DEVBUF, M_WAITOK);
736f45c063aSXin LI #if VESA_DEBUG > 1
737f45c063aSXin LI 			printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
738f45c063aSXin LI 			       modes, vesa_vmode_max);
739f45c063aSXin LI #endif
740f45c063aSXin LI 			if (modes > 0) {
741f45c063aSXin LI 				bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
742f45c063aSXin LI 				free(vesa_vmode, M_DEVBUF);
743f45c063aSXin LI 			}
744f45c063aSXin LI 			vesa_vmode = p;
745f45c063aSXin LI 		}
746f45c063aSXin LI 
747f45c063aSXin LI #if VESA_DEBUG > 1
748f45c063aSXin LI 		printf("Found VESA %s mode: %d x %d x %d bpp\n",
749f45c063aSXin LI 		    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
750f45c063aSXin LI 		    vmode.v_width, vmode.v_height, vmode.v_bpp);
751f45c063aSXin LI #endif
752f45c063aSXin LI 		if (is_via_cle266) {
753f45c063aSXin LI 		    if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
754f45c063aSXin LI 			vmode.v_width &= 0xff;
755f45c063aSXin LI 			vmode.v_waseg = 0xb8000 >> 4;
756f45c063aSXin LI 		    }
757f45c063aSXin LI 		}
758f45c063aSXin LI 
759f45c063aSXin LI 		/* copy some fields */
760f45c063aSXin LI 		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
761f45c063aSXin LI 		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
762f45c063aSXin LI 		vesa_vmode[modes].vi_width = vmode.v_width;
763f45c063aSXin LI 		vesa_vmode[modes].vi_height = vmode.v_height;
764f45c063aSXin LI 		vesa_vmode[modes].vi_depth = vmode.v_bpp;
765f45c063aSXin LI 		vesa_vmode[modes].vi_planes = vmode.v_planes;
766f45c063aSXin LI 		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
767f45c063aSXin LI 		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
768f45c063aSXin LI 		vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
769f45c063aSXin LI 		/* XXX window B */
770f45c063aSXin LI 		vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
771f45c063aSXin LI 		vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
772f45c063aSXin LI 		if (vmode.v_modeattr & V_MODELFB)
773f45c063aSXin LI 			vesa_vmode[modes].vi_buffer = vmode.v_lfb;
774f45c063aSXin LI 		else
775f45c063aSXin LI 			vesa_vmode[modes].vi_buffer = 0;
776f45c063aSXin LI 		/* XXX */
777f45c063aSXin LI 		vesa_vmode[modes].vi_buffer_size
778f45c063aSXin LI 			= vesa_adp_info->v_memsize*64*1024;
779f45c063aSXin LI #if 0
780f45c063aSXin LI 		if (vmode.v_offscreen > vmode.v_lfb)
781f45c063aSXin LI 			vesa_vmode[modes].vi_buffer_size
782f45c063aSXin LI 				= vmode.v_offscreen + vmode.v_offscreensize*1024
783f45c063aSXin LI 				      - vmode.v_lfb;
784f45c063aSXin LI 		else
785f45c063aSXin LI 			vesa_vmode[modes].vi_buffer_size
786205d67b0SXin LI 				= vmode.v_offscreen + vmode.v_offscreensize*1024;
787f45c063aSXin LI #endif
788f45c063aSXin LI 		vesa_vmode[modes].vi_mem_model
789f45c063aSXin LI 			= vesa_translate_mmodel(vmode.v_memmodel);
790f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[0] = 0;
791f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[1] = 0;
792f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[2] = 0;
793f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[3] = 0;
794f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[0] = 0;
795f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[1] = 0;
796f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[2] = 0;
797f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[3] = 0;
798f45c063aSXin LI 		if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_PACKED) {
799f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
800f45c063aSXin LI 		} else if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_DIRECT) {
801f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
802f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[0]
803f45c063aSXin LI 			    = vmode.v_redfieldpos;
804f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[1]
805f45c063aSXin LI 			    = vmode.v_greenfieldpos;
806f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[2]
807f45c063aSXin LI 			    = vmode.v_bluefieldpos;
808f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[3]
809f45c063aSXin LI 			    = vmode.v_resfieldpos;
810f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[0]
811f45c063aSXin LI 			    = vmode.v_redmasksize;
812f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[1]
813f45c063aSXin LI 			    = vmode.v_greenmasksize;
814f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[2]
815f45c063aSXin LI 			    = vmode.v_bluemasksize;
816f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[3]
817f45c063aSXin LI 			    = vmode.v_resmasksize;
818f45c063aSXin LI 		} else {
819f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = 0;
820f45c063aSXin LI 		}
821f45c063aSXin LI 
822f45c063aSXin LI 		vesa_vmode[modes].vi_flags
823f45c063aSXin LI 			= vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
824f45c063aSXin LI 		++modes;
825f45c063aSXin LI 	}
826f45c063aSXin LI 	vesa_vmode[modes].vi_mode = EOT;
827205d67b0SXin LI 
828205d67b0SXin LI 	x86biosFree(vmbuf, 1);
829205d67b0SXin LI 
830f45c063aSXin LI 	if (bootverbose)
831f45c063aSXin LI 		printf("VESA: %d mode(s) found\n", modes);
832f45c063aSXin LI 
833f45c063aSXin LI 	has_vesa_bios = (modes > 0);
834f45c063aSXin LI 	if (!has_vesa_bios)
835f45c063aSXin LI 		return (1);
836f45c063aSXin LI 
837f45c063aSXin LI 	return (0);
838f45c063aSXin LI }
839f45c063aSXin LI 
840f45c063aSXin LI static void
841f45c063aSXin LI vesa_clear_modes(video_info_t *info, int color)
842f45c063aSXin LI {
843f45c063aSXin LI 	while (info->vi_mode != EOT) {
844f45c063aSXin LI 		if ((info->vi_flags & V_INFO_COLOR) != color)
845f45c063aSXin LI 			info->vi_mode = NA;
846f45c063aSXin LI 		++info;
847f45c063aSXin LI 	}
848f45c063aSXin LI }
849f45c063aSXin LI 
850f45c063aSXin LI static vm_offset_t
851f45c063aSXin LI vesa_map_buffer(u_int paddr, size_t size)
852f45c063aSXin LI {
853f45c063aSXin LI 	vm_offset_t vaddr;
854f45c063aSXin LI 	u_int off;
855f45c063aSXin LI 
856f45c063aSXin LI 	off = paddr - trunc_page(paddr);
857afb9e5cfSJung-uk Kim 	vaddr = (vm_offset_t)pmap_mapdev_attr(paddr - off, size + off,
858afb9e5cfSJung-uk Kim 	    PAT_WRITE_COMBINING);
859f45c063aSXin LI #if VESA_DEBUG > 1
860ee5e90daSXin LI 	printf("vesa_map_buffer: paddr:%x vaddr:%tx size:%zx off:%x\n",
861f45c063aSXin LI 	       paddr, vaddr, size, off);
862f45c063aSXin LI #endif
863f45c063aSXin LI 	return (vaddr + off);
864f45c063aSXin LI }
865f45c063aSXin LI 
866f45c063aSXin LI static void
867f45c063aSXin LI vesa_unmap_buffer(vm_offset_t vaddr, size_t size)
868f45c063aSXin LI {
869f45c063aSXin LI #if VESA_DEBUG > 1
870ee5e90daSXin LI 	printf("vesa_unmap_buffer: vaddr:%tx size:%zx\n", vaddr, size);
871f45c063aSXin LI #endif
872f45c063aSXin LI 	kmem_free(kernel_map, vaddr, size);
873f45c063aSXin LI }
874f45c063aSXin LI 
875f45c063aSXin LI /* entry points */
876f45c063aSXin LI 
877f45c063aSXin LI static int
878f45c063aSXin LI vesa_configure(int flags)
879f45c063aSXin LI {
880f45c063aSXin LI 	video_adapter_t *adp;
881f45c063aSXin LI 	int adapters;
882f45c063aSXin LI 	int error;
883f45c063aSXin LI 	int i;
884f45c063aSXin LI 
885f45c063aSXin LI 	if (vesa_init_done)
886f45c063aSXin LI 		return 0;
887f45c063aSXin LI 	if (flags & VIO_PROBE_ONLY)
888f45c063aSXin LI 		return 0;		/* XXX */
889f45c063aSXin LI 
890f45c063aSXin LI 	/*
891f45c063aSXin LI 	 * If the VESA module has already been loaded, abort loading
892f45c063aSXin LI 	 * the module this time.
893f45c063aSXin LI 	 */
894f45c063aSXin LI 	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
895f45c063aSXin LI 		if (adp->va_flags & V_ADP_VESA)
896f45c063aSXin LI 			return ENXIO;
897f45c063aSXin LI 		if (adp->va_type == KD_VGA)
898f45c063aSXin LI 			break;
899f45c063aSXin LI 	}
900f45c063aSXin LI 	/*
901f45c063aSXin LI 	 * The VGA adapter is not found.  This is because either
902f45c063aSXin LI 	 * 1) the VGA driver has not been initialized, or 2) the VGA card
903f45c063aSXin LI 	 * is not present.  If 1) is the case, we shall defer
904f45c063aSXin LI 	 * initialization for now and try again later.
905f45c063aSXin LI 	 */
906f45c063aSXin LI 	if (adp == NULL) {
907f45c063aSXin LI 		vga_sub_configure = vesa_configure;
908f45c063aSXin LI 		return ENODEV;
909f45c063aSXin LI 	}
910f45c063aSXin LI 
911f45c063aSXin LI 	/* count number of registered adapters */
912f45c063aSXin LI 	for (++i; vid_get_adapter(i) != NULL; ++i)
913f45c063aSXin LI 		;
914f45c063aSXin LI 	adapters = i;
915f45c063aSXin LI 
916f45c063aSXin LI 	/* call VESA BIOS */
917f45c063aSXin LI 	vesa_adp = adp;
918f45c063aSXin LI 	if (vesa_bios_init()) {
919f45c063aSXin LI 		vesa_adp = NULL;
920f45c063aSXin LI 		return ENXIO;
921f45c063aSXin LI 	}
922f45c063aSXin LI 	vesa_adp->va_flags |= V_ADP_VESA;
923f45c063aSXin LI 
924f45c063aSXin LI 	/* remove conflicting modes if we have more than one adapter */
925f45c063aSXin LI 	if (adapters > 1) {
926f45c063aSXin LI 		vesa_clear_modes(vesa_vmode,
927f45c063aSXin LI 				 (vesa_adp->va_flags & V_ADP_COLOR) ?
928f45c063aSXin LI 				     V_INFO_COLOR : 0);
929f45c063aSXin LI 	}
930f45c063aSXin LI 
931f45c063aSXin LI 	if ((error = vesa_load_ioctl()) == 0) {
932f45c063aSXin LI 		prevvidsw = vidsw[vesa_adp->va_index];
933f45c063aSXin LI 		vidsw[vesa_adp->va_index] = &vesavidsw;
934f45c063aSXin LI 		vesa_init_done = TRUE;
935f45c063aSXin LI 	} else {
936f45c063aSXin LI 		vesa_adp = NULL;
937f45c063aSXin LI 		return error;
938f45c063aSXin LI 	}
939f45c063aSXin LI 
940f45c063aSXin LI 	return 0;
941f45c063aSXin LI }
942f45c063aSXin LI 
943f45c063aSXin LI #if 0
944f45c063aSXin LI static int
945f45c063aSXin LI vesa_nop(void)
946f45c063aSXin LI {
947f45c063aSXin LI 	return 0;
948f45c063aSXin LI }
949f45c063aSXin LI #endif
950f45c063aSXin LI 
951f45c063aSXin LI static int
952f45c063aSXin LI vesa_error(void)
953f45c063aSXin LI {
954f45c063aSXin LI 	return 1;
955f45c063aSXin LI }
956f45c063aSXin LI 
957f45c063aSXin LI static int
958f45c063aSXin LI vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
959f45c063aSXin LI {
960f45c063aSXin LI 	return (*prevvidsw->probe)(unit, adpp, arg, flags);
961f45c063aSXin LI }
962f45c063aSXin LI 
963f45c063aSXin LI static int
964f45c063aSXin LI vesa_init(int unit, video_adapter_t *adp, int flags)
965f45c063aSXin LI {
966f45c063aSXin LI 	return (*prevvidsw->init)(unit, adp, flags);
967f45c063aSXin LI }
968f45c063aSXin LI 
969f45c063aSXin LI static int
970f45c063aSXin LI vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
971f45c063aSXin LI {
972f45c063aSXin LI 	int i;
973f45c063aSXin LI 
974f45c063aSXin LI 	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
975f45c063aSXin LI 		return 0;
976f45c063aSXin LI 
977f45c063aSXin LI 	if (adp != vesa_adp)
978f45c063aSXin LI 		return 1;
979f45c063aSXin LI 
980f45c063aSXin LI 	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
981f45c063aSXin LI 				     vesa_adp->va_flags & V_ADP_COLOR, mode);
982f45c063aSXin LI 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
983f45c063aSXin LI 		if (vesa_vmode[i].vi_mode == NA)
984f45c063aSXin LI 			continue;
985f45c063aSXin LI 		if (vesa_vmode[i].vi_mode == mode) {
986f45c063aSXin LI 			*info = vesa_vmode[i];
987f45c063aSXin LI 			return 0;
988f45c063aSXin LI 		}
989f45c063aSXin LI 	}
990f45c063aSXin LI 	return 1;
991f45c063aSXin LI }
992f45c063aSXin LI 
993f45c063aSXin LI static int
994f45c063aSXin LI vesa_query_mode(video_adapter_t *adp, video_info_t *info)
995f45c063aSXin LI {
996f45c063aSXin LI 	int i;
997f45c063aSXin LI 
998f45c063aSXin LI 	if ((*prevvidsw->query_mode)(adp, info) == 0)
999f45c063aSXin LI 		return 0;
1000f45c063aSXin LI 	if (adp != vesa_adp)
1001f45c063aSXin LI 		return ENODEV;
1002f45c063aSXin LI 
1003f45c063aSXin LI 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1004f45c063aSXin LI 		if ((info->vi_width != 0)
1005f45c063aSXin LI 		    && (info->vi_width != vesa_vmode[i].vi_width))
1006f45c063aSXin LI 			continue;
1007f45c063aSXin LI 		if ((info->vi_height != 0)
1008f45c063aSXin LI 		    && (info->vi_height != vesa_vmode[i].vi_height))
1009f45c063aSXin LI 			continue;
1010f45c063aSXin LI 		if ((info->vi_cwidth != 0)
1011f45c063aSXin LI 		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
1012f45c063aSXin LI 			continue;
1013f45c063aSXin LI 		if ((info->vi_cheight != 0)
1014f45c063aSXin LI 		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
1015f45c063aSXin LI 			continue;
1016f45c063aSXin LI 		if ((info->vi_depth != 0)
1017f45c063aSXin LI 		    && (info->vi_depth != vesa_vmode[i].vi_depth))
1018f45c063aSXin LI 			continue;
1019f45c063aSXin LI 		if ((info->vi_planes != 0)
1020f45c063aSXin LI 		    && (info->vi_planes != vesa_vmode[i].vi_planes))
1021f45c063aSXin LI 			continue;
1022f45c063aSXin LI 		/* pixel format, memory model */
1023f45c063aSXin LI 		if ((info->vi_flags != 0)
1024f45c063aSXin LI 		    && (info->vi_flags != vesa_vmode[i].vi_flags))
1025f45c063aSXin LI 			continue;
1026f45c063aSXin LI 		*info = vesa_vmode[i];
1027f45c063aSXin LI 		return 0;
1028f45c063aSXin LI 	}
1029f45c063aSXin LI 	return ENODEV;
1030f45c063aSXin LI }
1031f45c063aSXin LI 
1032f45c063aSXin LI static int
1033f45c063aSXin LI vesa_set_mode(video_adapter_t *adp, int mode)
1034f45c063aSXin LI {
1035f45c063aSXin LI 	video_info_t info;
1036f45c063aSXin LI 	int len;
1037f45c063aSXin LI 
1038f45c063aSXin LI 	if (adp != vesa_adp)
1039f45c063aSXin LI 		return (*prevvidsw->set_mode)(adp, mode);
1040f45c063aSXin LI 
1041f45c063aSXin LI 	mode = vesa_map_gen_mode_num(adp->va_type,
1042f45c063aSXin LI 				     adp->va_flags & V_ADP_COLOR, mode);
1043f45c063aSXin LI #if VESA_DEBUG > 0
1044f45c063aSXin LI 	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1045f45c063aSXin LI 		adp->va_mode, adp->va_mode, mode, mode);
1046f45c063aSXin LI #endif
1047f45c063aSXin LI 	/*
1048f45c063aSXin LI 	 * If the current mode is a VESA mode and the new mode is not,
1049f45c063aSXin LI 	 * restore the state of the adapter first by setting one of the
1050f45c063aSXin LI 	 * standard VGA mode, so that non-standard, extended SVGA registers
1051f45c063aSXin LI 	 * are set to the state compatible with the standard VGA modes.
1052f45c063aSXin LI 	 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1053f45c063aSXin LI 	 * the new mode correctly.
1054f45c063aSXin LI 	 */
1055f45c063aSXin LI 	if (VESA_MODE(adp->va_mode)) {
1056f45c063aSXin LI 		if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
1057f45c063aSXin LI 			int10_set_mode(adp->va_initial_bios_mode);
1058f45c063aSXin LI 			if (adp->va_info.vi_flags & V_INFO_LINEAR)
1059f45c063aSXin LI 				vesa_unmap_buffer(adp->va_buffer,
1060f45c063aSXin LI 						  vesa_adp_info->v_memsize*64*1024);
1061f45c063aSXin LI 			/*
1062f45c063aSXin LI 			 * Once (*prevvidsw->get_info)() succeeded,
1063f45c063aSXin LI 			 * (*prevvidsw->set_mode)() below won't fail...
1064f45c063aSXin LI 			 */
1065f45c063aSXin LI 		}
1066f45c063aSXin LI 	}
1067f45c063aSXin LI 
1068f45c063aSXin LI 	/* we may not need to handle this mode after all... */
1069f45c063aSXin LI 	if ((*prevvidsw->set_mode)(adp, mode) == 0)
1070f45c063aSXin LI 		return 0;
1071f45c063aSXin LI 
1072f45c063aSXin LI 	/* is the new mode supported? */
1073f45c063aSXin LI 	if (vesa_get_info(adp, mode, &info))
1074f45c063aSXin LI 		return 1;
1075f45c063aSXin LI 	/* assert(VESA_MODE(mode)); */
1076f45c063aSXin LI 
1077f45c063aSXin LI #if VESA_DEBUG > 0
1078f45c063aSXin LI 	printf("VESA: about to set a VESA mode...\n");
1079f45c063aSXin LI #endif
1080f45c063aSXin LI 	/* don't use the linear frame buffer for text modes. XXX */
1081f45c063aSXin LI 	if (!(info.vi_flags & V_INFO_GRAPHICS))
1082f45c063aSXin LI 		info.vi_flags &= ~V_INFO_LINEAR;
1083f45c063aSXin LI 
1084f45c063aSXin LI 	if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
1085f45c063aSXin LI 		return 1;
1086f45c063aSXin LI 
1087f45c063aSXin LI 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1088f45c063aSXin LI 		vesa_unmap_buffer(adp->va_buffer,
1089f45c063aSXin LI 				  vesa_adp_info->v_memsize*64*1024);
1090f45c063aSXin LI 
1091f45c063aSXin LI #if VESA_DEBUG > 0
1092f45c063aSXin LI 	printf("VESA: mode set!\n");
1093f45c063aSXin LI #endif
1094f45c063aSXin LI 	vesa_adp->va_mode = mode;
1095f45c063aSXin LI 	vesa_adp->va_flags &= ~V_ADP_COLOR;
1096f45c063aSXin LI 	vesa_adp->va_flags |=
1097f45c063aSXin LI 		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1098f45c063aSXin LI 	vesa_adp->va_crtc_addr =
1099f45c063aSXin LI 		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1100f45c063aSXin LI 	if (info.vi_flags & V_INFO_LINEAR) {
1101f45c063aSXin LI #if VESA_DEBUG > 1
1102f45c063aSXin LI 		printf("VESA: setting up LFB\n");
1103f45c063aSXin LI #endif
1104f45c063aSXin LI 		vesa_adp->va_buffer =
1105f45c063aSXin LI 			vesa_map_buffer(info.vi_buffer,
1106f45c063aSXin LI 					vesa_adp_info->v_memsize*64*1024);
1107f45c063aSXin LI 		vesa_adp->va_buffer_size = info.vi_buffer_size;
1108f45c063aSXin LI 		vesa_adp->va_window = vesa_adp->va_buffer;
1109f45c063aSXin LI 		vesa_adp->va_window_size = info.vi_buffer_size/info.vi_planes;
1110f45c063aSXin LI 		vesa_adp->va_window_gran = info.vi_buffer_size/info.vi_planes;
1111f45c063aSXin LI 	} else {
1112f45c063aSXin LI 		vesa_adp->va_buffer = 0;
1113f45c063aSXin LI 		vesa_adp->va_buffer_size = info.vi_buffer_size;
1114205d67b0SXin LI 		vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
1115f45c063aSXin LI 		vesa_adp->va_window_size = info.vi_window_size;
1116f45c063aSXin LI 		vesa_adp->va_window_gran = info.vi_window_gran;
1117f45c063aSXin LI 	}
1118f45c063aSXin LI 	vesa_adp->va_window_orig = 0;
1119f45c063aSXin LI 	len = vesa_bios_get_line_length();
1120f45c063aSXin LI 	if (len > 0) {
1121f45c063aSXin LI 		vesa_adp->va_line_width = len;
1122f45c063aSXin LI 	} else if (info.vi_flags & V_INFO_GRAPHICS) {
1123f45c063aSXin LI 		switch (info.vi_depth/info.vi_planes) {
1124f45c063aSXin LI 		case 1:
1125f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/8;
1126f45c063aSXin LI 			break;
1127f45c063aSXin LI 		case 2:
1128f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/4;
1129f45c063aSXin LI 			break;
1130f45c063aSXin LI 		case 4:
1131f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/2;
1132f45c063aSXin LI 			break;
1133f45c063aSXin LI 		case 8:
1134f45c063aSXin LI 		default: /* shouldn't happen */
1135f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width;
1136f45c063aSXin LI 			break;
1137f45c063aSXin LI 		case 15:
1138f45c063aSXin LI 		case 16:
1139f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width*2;
1140f45c063aSXin LI 			break;
1141f45c063aSXin LI 		case 24:
1142f45c063aSXin LI 		case 32:
1143f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width*4;
1144f45c063aSXin LI 			break;
1145f45c063aSXin LI 		}
1146f45c063aSXin LI 	} else {
1147f45c063aSXin LI 		vesa_adp->va_line_width = info.vi_width;
1148f45c063aSXin LI 	}
1149f45c063aSXin LI 	vesa_adp->va_disp_start.x = 0;
1150f45c063aSXin LI 	vesa_adp->va_disp_start.y = 0;
1151f45c063aSXin LI #if VESA_DEBUG > 0
1152f45c063aSXin LI 	printf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n",
1153f45c063aSXin LI 	       info.vi_width, len, vesa_adp->va_line_width);
1154f45c063aSXin LI #endif
1155f45c063aSXin LI 	bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1156f45c063aSXin LI 
1157f45c063aSXin LI 	/* move hardware cursor out of the way */
1158f45c063aSXin LI 	(*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1159f45c063aSXin LI 
1160f45c063aSXin LI 	return 0;
1161f45c063aSXin LI }
1162f45c063aSXin LI 
1163f45c063aSXin LI static int
1164f45c063aSXin LI vesa_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1165f45c063aSXin LI 	       u_char *data, int ch, int count)
1166f45c063aSXin LI {
1167f45c063aSXin LI 	return (*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data,
1168f45c063aSXin LI 		ch, count);
1169f45c063aSXin LI }
1170f45c063aSXin LI 
1171f45c063aSXin LI static int
1172f45c063aSXin LI vesa_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1173f45c063aSXin LI 	       u_char *data, int ch, int count)
1174f45c063aSXin LI {
1175f45c063aSXin LI 	return (*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data,
1176f45c063aSXin LI 		ch, count);
1177f45c063aSXin LI }
1178f45c063aSXin LI 
1179f45c063aSXin LI static int
1180f45c063aSXin LI vesa_show_font(video_adapter_t *adp, int page)
1181f45c063aSXin LI {
1182f45c063aSXin LI 	return (*prevvidsw->show_font)(adp, page);
1183f45c063aSXin LI }
1184f45c063aSXin LI 
1185f45c063aSXin LI static int
1186f45c063aSXin LI vesa_save_palette(video_adapter_t *adp, u_char *palette)
1187f45c063aSXin LI {
1188f45c063aSXin LI 	int bits;
1189f45c063aSXin LI 	int error;
1190f45c063aSXin LI 
1191f45c063aSXin LI 	if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1192f45c063aSXin LI 	    && VESA_MODE(adp->va_mode)) {
1193f45c063aSXin LI 		bits = vesa_bios_get_dac();
1194f45c063aSXin LI 		error = vesa_bios_save_palette(0, 256, palette, bits);
1195f45c063aSXin LI 		if (error == 0)
1196f45c063aSXin LI 			return 0;
1197f45c063aSXin LI 		if (bits != 6)
1198f45c063aSXin LI 			return error;
1199f45c063aSXin LI 	}
1200f45c063aSXin LI 
1201f45c063aSXin LI 	return (*prevvidsw->save_palette)(adp, palette);
1202f45c063aSXin LI }
1203f45c063aSXin LI 
1204f45c063aSXin LI static int
1205f45c063aSXin LI vesa_load_palette(video_adapter_t *adp, u_char *palette)
1206f45c063aSXin LI {
1207f45c063aSXin LI #ifdef notyet
1208f45c063aSXin LI 	int bits;
1209f45c063aSXin LI 	int error;
1210f45c063aSXin LI 
1211f45c063aSXin LI 	if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1212f45c063aSXin LI 	    && VESA_MODE(adp->va_mode) && ((bits = vesa_bios_set_dac(8)) > 6)) {
1213f45c063aSXin LI 		error = vesa_bios_load_palette(0, 256, palette, bits);
1214f45c063aSXin LI 		if (error == 0)
1215f45c063aSXin LI 			return 0;
1216f45c063aSXin LI 		if (vesa_bios_set_dac(6) != 6)
1217f45c063aSXin LI 			return 1;
1218f45c063aSXin LI 	}
1219f45c063aSXin LI #endif /* notyet */
1220f45c063aSXin LI 
1221f45c063aSXin LI 	return (*prevvidsw->load_palette)(adp, palette);
1222f45c063aSXin LI }
1223f45c063aSXin LI 
1224f45c063aSXin LI static int
1225f45c063aSXin LI vesa_set_border(video_adapter_t *adp, int color)
1226f45c063aSXin LI {
1227f45c063aSXin LI 	return (*prevvidsw->set_border)(adp, color);
1228f45c063aSXin LI }
1229f45c063aSXin LI 
1230f45c063aSXin LI static int
1231f45c063aSXin LI vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1232f45c063aSXin LI {
1233f45c063aSXin LI 	if (adp != vesa_adp)
1234f45c063aSXin LI 		return (*prevvidsw->save_state)(adp, p, size);
1235f45c063aSXin LI 
1236f45c063aSXin LI 	if (vesa_state_buf_size == 0)
1237f45c063aSXin LI 		vesa_state_buf_size = vesa_bios_state_buf_size();
1238f45c063aSXin LI 	if (size == 0)
1239f45c063aSXin LI 		return (sizeof(int) + vesa_state_buf_size);
1240f45c063aSXin LI 	else if (size < (sizeof(int) + vesa_state_buf_size))
1241f45c063aSXin LI 		return 1;
1242f45c063aSXin LI 
1243f45c063aSXin LI 	((adp_state_t *)p)->sig = V_STATE_SIG;
1244f45c063aSXin LI 	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1245f45c063aSXin LI 	return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
1246f45c063aSXin LI 				      vesa_state_buf_size);
1247f45c063aSXin LI }
1248f45c063aSXin LI 
1249f45c063aSXin LI static int
1250f45c063aSXin LI vesa_load_state(video_adapter_t *adp, void *p)
1251f45c063aSXin LI {
1252f45c063aSXin LI 	if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1253f45c063aSXin LI 		return (*prevvidsw->load_state)(adp, p);
1254f45c063aSXin LI 
1255f45c063aSXin LI 	return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
1256f45c063aSXin LI 				      vesa_state_buf_size);
1257f45c063aSXin LI }
1258f45c063aSXin LI 
1259f45c063aSXin LI #if 0
1260f45c063aSXin LI static int
1261f45c063aSXin LI vesa_get_origin(video_adapter_t *adp, off_t *offset)
1262f45c063aSXin LI {
1263205d67b0SXin LI 	x86regs_t regs;
1264f45c063aSXin LI 
1265205d67b0SXin LI 	regs.R_EAX = 0x4f05;
1266205d67b0SXin LI 	regs.R_EBX = 0x10;
1267ee5e90daSXin LI 
1268205d67b0SXin LI 	x86biosCall(&regs, 0x10);
1269205d67b0SXin LI 
1270205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
1271f45c063aSXin LI 		return 1;
1272205d67b0SXin LI 	*offset = regs.DX*adp->va_window_gran;
1273ee5e90daSXin LI 
1274f45c063aSXin LI 	return 0;
1275f45c063aSXin LI }
1276f45c063aSXin LI #endif
1277f45c063aSXin LI 
1278f45c063aSXin LI static int
1279f45c063aSXin LI vesa_set_origin(video_adapter_t *adp, off_t offset)
1280f45c063aSXin LI {
1281205d67b0SXin LI 	x86regs_t regs;
1282205d67b0SXin LI 
1283f45c063aSXin LI 	/*
1284f45c063aSXin LI 	 * This function should return as quickly as possible to
1285f45c063aSXin LI 	 * maintain good performance of the system. For this reason,
1286f45c063aSXin LI 	 * error checking is kept minimal and let the VESA BIOS to
1287f45c063aSXin LI 	 * detect error.
1288f45c063aSXin LI 	 */
1289f45c063aSXin LI 	if (adp != vesa_adp)
1290f45c063aSXin LI 		return (*prevvidsw->set_win_org)(adp, offset);
1291f45c063aSXin LI 
1292f45c063aSXin LI 	/* if this is a linear frame buffer, do nothing */
1293f45c063aSXin LI 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1294f45c063aSXin LI 		return 0;
1295f45c063aSXin LI 	/* XXX */
1296f45c063aSXin LI 	if (adp->va_window_gran == 0)
1297f45c063aSXin LI 		return 1;
1298f45c063aSXin LI 
1299205d67b0SXin LI 	regs.R_EAX = 0x4f05;
1300205d67b0SXin LI 	regs.R_EBX = 0;
1301205d67b0SXin LI 	regs.R_EDX = offset/adp->va_window_gran;
1302205d67b0SXin LI 	x86biosCall(&regs, 0x10);
1303ee5e90daSXin LI 
1304205d67b0SXin LI 	if ((regs.R_AX & 0xff) != 0x4f)
1305f45c063aSXin LI 		return 1;
1306ee5e90daSXin LI 
1307205d67b0SXin LI 	regs.R_EAX = 0x4f05;
1308205d67b0SXin LI 	regs.R_EBX = 1;
1309205d67b0SXin LI 	regs.R_EDX = offset/adp->va_window_gran;
1310205d67b0SXin LI 	x86biosCall(&regs, 0x10);
1311ee5e90daSXin LI 
1312f45c063aSXin LI 	adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1313f45c063aSXin LI 	return 0;			/* XXX */
1314f45c063aSXin LI }
1315f45c063aSXin LI 
1316f45c063aSXin LI static int
1317f45c063aSXin LI vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1318f45c063aSXin LI {
1319f45c063aSXin LI 	return (*prevvidsw->read_hw_cursor)(adp, col, row);
1320f45c063aSXin LI }
1321f45c063aSXin LI 
1322f45c063aSXin LI static int
1323f45c063aSXin LI vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1324f45c063aSXin LI {
1325f45c063aSXin LI 	return (*prevvidsw->set_hw_cursor)(adp, col, row);
1326f45c063aSXin LI }
1327f45c063aSXin LI 
1328f45c063aSXin LI static int
1329f45c063aSXin LI vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1330f45c063aSXin LI 			 int celsize, int blink)
1331f45c063aSXin LI {
1332f45c063aSXin LI 	return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1333f45c063aSXin LI 						 blink);
1334f45c063aSXin LI }
1335f45c063aSXin LI 
1336f45c063aSXin LI static int
1337f45c063aSXin LI vesa_blank_display(video_adapter_t *adp, int mode)
1338f45c063aSXin LI {
1339f45c063aSXin LI 	/* XXX: use VESA DPMS */
1340f45c063aSXin LI 	return (*prevvidsw->blank_display)(adp, mode);
1341f45c063aSXin LI }
1342f45c063aSXin LI 
1343f45c063aSXin LI static int
1344f45c063aSXin LI vesa_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
1345f45c063aSXin LI 	  int prot)
1346f45c063aSXin LI {
1347f45c063aSXin LI #if VESA_DEBUG > 0
1348ee5e90daSXin LI 	printf("vesa_mmap(): window:0x%tx, buffer:0x%tx, offset:0x%tx\n",
1349f45c063aSXin LI 	       adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1350f45c063aSXin LI #endif
1351f45c063aSXin LI 
1352f45c063aSXin LI 	if ((adp == vesa_adp) && (adp->va_info.vi_flags & V_INFO_LINEAR)) {
1353f45c063aSXin LI 		/* va_window_size == va_buffer_size/vi_planes */
1354f45c063aSXin LI 		/* XXX: is this correct? */
1355f45c063aSXin LI 		if (offset > adp->va_window_size - PAGE_SIZE)
1356f45c063aSXin LI 			return -1;
1357f45c063aSXin LI 		*paddr = adp->va_info.vi_buffer + offset;
1358f45c063aSXin LI 		return 0;
1359f45c063aSXin LI 	} else {
1360f45c063aSXin LI 		return (*prevvidsw->mmap)(adp, offset, paddr, prot);
1361f45c063aSXin LI 	}
1362f45c063aSXin LI }
1363f45c063aSXin LI 
1364f45c063aSXin LI static int
1365f45c063aSXin LI vesa_clear(video_adapter_t *adp)
1366f45c063aSXin LI {
1367f45c063aSXin LI 	return (*prevvidsw->clear)(adp);
1368f45c063aSXin LI }
1369f45c063aSXin LI 
1370f45c063aSXin LI static int
1371f45c063aSXin LI vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1372f45c063aSXin LI {
1373f45c063aSXin LI 	return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
1374f45c063aSXin LI }
1375f45c063aSXin LI 
1376f45c063aSXin LI static int
1377f45c063aSXin LI vesa_bitblt(video_adapter_t *adp,...)
1378f45c063aSXin LI {
1379f45c063aSXin LI 	/* FIXME */
1380f45c063aSXin LI 	return 1;
1381f45c063aSXin LI }
1382f45c063aSXin LI 
1383f45c063aSXin LI static int
1384f45c063aSXin LI get_palette(video_adapter_t *adp, int base, int count,
1385f45c063aSXin LI 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1386f45c063aSXin LI {
1387f45c063aSXin LI 	u_char *r;
1388f45c063aSXin LI 	u_char *g;
1389f45c063aSXin LI 	u_char *b;
1390f45c063aSXin LI 	int bits;
1391f45c063aSXin LI 	int error;
1392f45c063aSXin LI 
1393f45c063aSXin LI 	if ((base < 0) || (base >= 256) || (count < 0) || (count > 256))
1394f45c063aSXin LI 		return 1;
1395f45c063aSXin LI 	if ((base + count) > 256)
1396f45c063aSXin LI 		return 1;
1397f45c063aSXin LI 	if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode))
1398f45c063aSXin LI 		return 1;
1399f45c063aSXin LI 
1400f45c063aSXin LI 	bits = vesa_bios_get_dac();
1401f45c063aSXin LI 	if (bits <= 6)
1402f45c063aSXin LI 		return 1;
1403f45c063aSXin LI 
1404f45c063aSXin LI 	r = malloc(count*3, M_DEVBUF, M_WAITOK);
1405f45c063aSXin LI 	g = r + count;
1406f45c063aSXin LI 	b = g + count;
1407f45c063aSXin LI 	error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1408f45c063aSXin LI 	if (error == 0) {
1409f45c063aSXin LI 		copyout(r, red, count);
1410f45c063aSXin LI 		copyout(g, green, count);
1411f45c063aSXin LI 		copyout(b, blue, count);
1412f45c063aSXin LI 		if (trans != NULL) {
1413f45c063aSXin LI 			bzero(r, count);
1414f45c063aSXin LI 			copyout(r, trans, count);
1415f45c063aSXin LI 		}
1416f45c063aSXin LI 	}
1417f45c063aSXin LI 	free(r, M_DEVBUF);
1418f45c063aSXin LI 
1419f45c063aSXin LI 	/* if error && bits != 6 at this point, we are in trouble... XXX */
1420f45c063aSXin LI 	return error;
1421f45c063aSXin LI }
1422f45c063aSXin LI 
1423f45c063aSXin LI static int
1424f45c063aSXin LI set_palette(video_adapter_t *adp, int base, int count,
1425f45c063aSXin LI 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1426f45c063aSXin LI {
1427f45c063aSXin LI 	return 1;
1428f45c063aSXin LI #ifdef notyet
1429f45c063aSXin LI 	u_char *r;
1430f45c063aSXin LI 	u_char *g;
1431f45c063aSXin LI 	u_char *b;
1432f45c063aSXin LI 	int bits;
1433f45c063aSXin LI 	int error;
1434f45c063aSXin LI 
1435f45c063aSXin LI 	if ((base < 0) || (base >= 256) || (base + count > 256))
1436f45c063aSXin LI 		return 1;
1437f45c063aSXin LI 	if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode)
1438f45c063aSXin LI 		|| ((bits = vesa_bios_set_dac(8)) <= 6))
1439f45c063aSXin LI 		return 1;
1440f45c063aSXin LI 
1441f45c063aSXin LI 	r = malloc(count*3, M_DEVBUF, M_WAITOK);
1442f45c063aSXin LI 	g = r + count;
1443f45c063aSXin LI 	b = g + count;
1444f45c063aSXin LI 	copyin(red, r, count);
1445f45c063aSXin LI 	copyin(green, g, count);
1446f45c063aSXin LI 	copyin(blue, b, count);
1447f45c063aSXin LI 
1448f45c063aSXin LI 	error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1449f45c063aSXin LI 	free(r, M_DEVBUF);
1450f45c063aSXin LI 	if (error == 0)
1451f45c063aSXin LI 		return 0;
1452f45c063aSXin LI 
1453f45c063aSXin LI 	/* if the following call fails, we are in trouble... XXX */
1454f45c063aSXin LI 	vesa_bios_set_dac(6);
1455f45c063aSXin LI 	return 1;
1456f45c063aSXin LI #endif /* notyet */
1457f45c063aSXin LI }
1458f45c063aSXin LI 
1459f45c063aSXin LI static int
1460f45c063aSXin LI vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1461f45c063aSXin LI {
1462f45c063aSXin LI 	int bytes;
1463f45c063aSXin LI 
1464f45c063aSXin LI 	if (adp != vesa_adp)
1465f45c063aSXin LI 		return (*prevvidsw->ioctl)(adp, cmd, arg);
1466f45c063aSXin LI 
1467f45c063aSXin LI 	switch (cmd) {
1468f45c063aSXin LI 	case FBIO_SETWINORG:	/* set frame buffer window origin */
1469f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1470f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1471f45c063aSXin LI 		return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1472f45c063aSXin LI 
1473f45c063aSXin LI 	case FBIO_SETDISPSTART:	/* set display start address */
1474f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1475f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1476f45c063aSXin LI 		if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1477f45c063aSXin LI 					((video_display_start_t *)arg)->y))
1478f45c063aSXin LI 			return ENODEV;
1479f45c063aSXin LI 		adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1480f45c063aSXin LI 		adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1481f45c063aSXin LI 		return 0;
1482f45c063aSXin LI 
1483f45c063aSXin LI 	case FBIO_SETLINEWIDTH:	/* set line length in pixel */
1484f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1485f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1486f45c063aSXin LI 		if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1487f45c063aSXin LI 			return ENODEV;
1488f45c063aSXin LI 		adp->va_line_width = bytes;
1489f45c063aSXin LI #if VESA_DEBUG > 1
1490f45c063aSXin LI 		printf("new line width:%d\n", adp->va_line_width);
1491f45c063aSXin LI #endif
1492f45c063aSXin LI 		return 0;
1493f45c063aSXin LI 
1494f45c063aSXin LI 	case FBIO_GETPALETTE:	/* get color palette */
1495f45c063aSXin LI 		if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1496f45c063aSXin LI 				((video_color_palette_t *)arg)->count,
1497f45c063aSXin LI 				((video_color_palette_t *)arg)->red,
1498f45c063aSXin LI 				((video_color_palette_t *)arg)->green,
1499f45c063aSXin LI 				((video_color_palette_t *)arg)->blue,
1500f45c063aSXin LI 				((video_color_palette_t *)arg)->transparent))
1501f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1502f45c063aSXin LI 		return 0;
1503f45c063aSXin LI 
1504f45c063aSXin LI 
1505f45c063aSXin LI 	case FBIO_SETPALETTE:	/* set color palette */
1506f45c063aSXin LI 		if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1507f45c063aSXin LI 				((video_color_palette_t *)arg)->count,
1508f45c063aSXin LI 				((video_color_palette_t *)arg)->red,
1509f45c063aSXin LI 				((video_color_palette_t *)arg)->green,
1510f45c063aSXin LI 				((video_color_palette_t *)arg)->blue,
1511f45c063aSXin LI 				((video_color_palette_t *)arg)->transparent))
1512f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1513f45c063aSXin LI 		return 0;
1514f45c063aSXin LI 
1515f45c063aSXin LI 	case FBIOGETCMAP:	/* get color palette */
1516f45c063aSXin LI 		if (get_palette(adp, ((struct fbcmap *)arg)->index,
1517f45c063aSXin LI 				((struct fbcmap *)arg)->count,
1518f45c063aSXin LI 				((struct fbcmap *)arg)->red,
1519f45c063aSXin LI 				((struct fbcmap *)arg)->green,
1520f45c063aSXin LI 				((struct fbcmap *)arg)->blue, NULL))
1521f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1522f45c063aSXin LI 		return 0;
1523f45c063aSXin LI 
1524f45c063aSXin LI 	case FBIOPUTCMAP:	/* set color palette */
1525f45c063aSXin LI 		if (set_palette(adp, ((struct fbcmap *)arg)->index,
1526f45c063aSXin LI 				((struct fbcmap *)arg)->count,
1527f45c063aSXin LI 				((struct fbcmap *)arg)->red,
1528f45c063aSXin LI 				((struct fbcmap *)arg)->green,
1529f45c063aSXin LI 				((struct fbcmap *)arg)->blue, NULL))
1530f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1531f45c063aSXin LI 		return 0;
1532f45c063aSXin LI 
1533f45c063aSXin LI 	default:
1534f45c063aSXin LI 		return (*prevvidsw->ioctl)(adp, cmd, arg);
1535f45c063aSXin LI 	}
1536f45c063aSXin LI }
1537f45c063aSXin LI 
1538f45c063aSXin LI static int
1539f45c063aSXin LI vesa_diag(video_adapter_t *adp, int level)
1540f45c063aSXin LI {
1541f45c063aSXin LI 	int error;
1542f45c063aSXin LI 
1543f45c063aSXin LI 	/* call the previous handler first */
1544f45c063aSXin LI 	error = (*prevvidsw->diag)(adp, level);
1545f45c063aSXin LI 	if (error)
1546f45c063aSXin LI 		return error;
1547f45c063aSXin LI 
1548f45c063aSXin LI 	if (adp != vesa_adp)
1549f45c063aSXin LI 		return 1;
1550f45c063aSXin LI 
1551f45c063aSXin LI 	if (level <= 0)
1552f45c063aSXin LI 		return 0;
1553f45c063aSXin LI 
1554f45c063aSXin LI 	return 0;
1555f45c063aSXin LI }
1556f45c063aSXin LI 
1557f45c063aSXin LI static int
1558f45c063aSXin LI vesa_bios_info(int level)
1559f45c063aSXin LI {
1560f45c063aSXin LI #if VESA_DEBUG > 1
1561f45c063aSXin LI 	struct vesa_mode vmode;
1562f45c063aSXin LI 	int i;
1563f45c063aSXin LI #endif
1564f45c063aSXin LI 
1565f45c063aSXin LI 	if (bootverbose) {
1566f45c063aSXin LI 		/* general adapter information */
1567f45c063aSXin LI 		printf(
1568f45c063aSXin LI 	"VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1569f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0xf000) >> 12) * 10 +
1570f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0x0f00) >> 8),
1571f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10 +
1572f45c063aSXin LI 		    (vesa_adp_info->v_version & 0x000f),
1573f45c063aSXin LI 		    vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1574f45c063aSXin LI 		    vesa_vmodetab, vesa_adp_info->v_modetable);
1575f45c063aSXin LI 
1576f45c063aSXin LI 		/* OEM string */
1577f45c063aSXin LI 		if (vesa_oemstr != NULL)
1578f45c063aSXin LI 			printf("VESA: %s\n", vesa_oemstr);
1579f45c063aSXin LI 	}
1580f45c063aSXin LI 
1581f45c063aSXin LI 	if (level <= 0)
1582f45c063aSXin LI 		return 0;
1583f45c063aSXin LI 
1584f45c063aSXin LI 	if (vesa_adp_info->v_version >= 0x0200 && bootverbose) {
1585f45c063aSXin LI 		/* vender name, product name, product revision */
1586f45c063aSXin LI 		printf("VESA: %s %s %s\n",
1587f45c063aSXin LI 			(vesa_venderstr != NULL) ? vesa_venderstr : "unknown",
1588f45c063aSXin LI 			(vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1589f45c063aSXin LI 			(vesa_revstr != NULL) ? vesa_revstr : "?");
1590f45c063aSXin LI 	}
1591f45c063aSXin LI 
1592f45c063aSXin LI #if VESA_DEBUG > 1
1593f45c063aSXin LI 	/* mode information */
1594f45c063aSXin LI 	for (i = 0;
1595f45c063aSXin LI 		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1596f45c063aSXin LI 		&& (vesa_vmodetab[i] != 0xffff); ++i) {
1597f45c063aSXin LI 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
1598f45c063aSXin LI 			continue;
1599f45c063aSXin LI 
1600f45c063aSXin LI 		/* print something for diagnostic purpose */
1601f45c063aSXin LI 		printf("VESA: mode:0x%03x, flags:0x%04x",
1602f45c063aSXin LI 		       vesa_vmodetab[i], vmode.v_modeattr);
1603f45c063aSXin LI 		if (vmode.v_modeattr & V_MODEOPTINFO) {
1604f45c063aSXin LI 			if (vmode.v_modeattr & V_MODEGRAPHICS) {
1605f45c063aSXin LI 				printf(", G %dx%dx%d %d, ",
1606f45c063aSXin LI 				       vmode.v_width, vmode.v_height,
1607f45c063aSXin LI 				       vmode.v_bpp, vmode.v_planes);
1608f45c063aSXin LI 			} else {
1609f45c063aSXin LI 				printf(", T %dx%d, ",
1610f45c063aSXin LI 				       vmode.v_width, vmode.v_height);
1611f45c063aSXin LI 			}
1612f45c063aSXin LI 			printf("font:%dx%d, ",
1613f45c063aSXin LI 			       vmode.v_cwidth, vmode.v_cheight);
1614f45c063aSXin LI 			printf("pages:%d, mem:%d",
1615f45c063aSXin LI 			       vmode.v_ipages + 1, vmode.v_memmodel);
1616f45c063aSXin LI 		}
1617f45c063aSXin LI 		if (vmode.v_modeattr & V_MODELFB) {
1618f45c063aSXin LI 			printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1619f45c063aSXin LI 			       vmode.v_lfb, vmode.v_offscreen,
1620f45c063aSXin LI 			       vmode.v_offscreensize*1024);
1621f45c063aSXin LI 		}
1622f45c063aSXin LI 		printf("\n");
1623f45c063aSXin LI 		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1624f45c063aSXin LI 		       vmode.v_waseg, vmode.v_waattr,
1625f45c063aSXin LI 		       vmode.v_wbseg, vmode.v_wbattr);
1626f45c063aSXin LI 		printf("size:%dk, gran:%dk\n",
1627f45c063aSXin LI 		       vmode.v_wsize, vmode.v_wgran);
1628f45c063aSXin LI 	}
1629f45c063aSXin LI #endif /* VESA_DEBUG > 1 */
1630f45c063aSXin LI 
1631f45c063aSXin LI 	return 0;
1632f45c063aSXin LI }
1633f45c063aSXin LI 
1634f45c063aSXin LI /* module loading */
1635f45c063aSXin LI 
1636f45c063aSXin LI static int
1637f45c063aSXin LI vesa_load(void)
1638f45c063aSXin LI {
1639f45c063aSXin LI 	int error;
1640f45c063aSXin LI 	int s;
1641f45c063aSXin LI 
1642f45c063aSXin LI 	if (vesa_init_done)
1643f45c063aSXin LI 		return 0;
1644f45c063aSXin LI 
1645f45c063aSXin LI 	/* locate a VGA adapter */
1646f45c063aSXin LI 	s = spltty();
1647f45c063aSXin LI 	vesa_adp = NULL;
1648f45c063aSXin LI 	error = vesa_configure(0);
1649f45c063aSXin LI 	splx(s);
1650f45c063aSXin LI 
1651f45c063aSXin LI 	if (error == 0)
1652f45c063aSXin LI 		vesa_bios_info(bootverbose);
1653f45c063aSXin LI 
1654f45c063aSXin LI 	return error;
1655f45c063aSXin LI }
1656f45c063aSXin LI 
1657f45c063aSXin LI static int
1658f45c063aSXin LI vesa_unload(void)
1659f45c063aSXin LI {
1660f45c063aSXin LI 	u_char palette[256*3];
1661f45c063aSXin LI 	int error;
1662f45c063aSXin LI 	int bits;
1663f45c063aSXin LI 	int s;
1664f45c063aSXin LI 
1665f45c063aSXin LI 	/* if the adapter is currently in a VESA mode, don't unload */
1666f45c063aSXin LI 	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1667f45c063aSXin LI 		return EBUSY;
1668f45c063aSXin LI 	/*
1669f45c063aSXin LI 	 * FIXME: if there is at least one vty which is in a VESA mode,
1670f45c063aSXin LI 	 * we shouldn't be unloading! XXX
1671f45c063aSXin LI 	 */
1672f45c063aSXin LI 
1673f45c063aSXin LI 	s = spltty();
1674f45c063aSXin LI 	if ((error = vesa_unload_ioctl()) == 0) {
1675f45c063aSXin LI 		if (vesa_adp != NULL) {
1676f45c063aSXin LI 			if (vesa_adp_info->v_flags & V_DAC8)  {
1677f45c063aSXin LI 				bits = vesa_bios_get_dac();
1678f45c063aSXin LI 				if (bits > 6) {
1679f45c063aSXin LI 					vesa_bios_save_palette(0, 256,
1680f45c063aSXin LI 							       palette, bits);
1681f45c063aSXin LI 					vesa_bios_set_dac(6);
1682f45c063aSXin LI 					vesa_bios_load_palette(0, 256,
1683f45c063aSXin LI 							       palette, 6);
1684f45c063aSXin LI 				}
1685f45c063aSXin LI 			}
1686f45c063aSXin LI 			vesa_adp->va_flags &= ~V_ADP_VESA;
1687f45c063aSXin LI 			vidsw[vesa_adp->va_index] = prevvidsw;
1688f45c063aSXin LI 		}
1689f45c063aSXin LI 	}
1690f45c063aSXin LI 	splx(s);
1691f45c063aSXin LI 
1692f45c063aSXin LI 	return error;
1693f45c063aSXin LI }
1694f45c063aSXin LI 
1695f45c063aSXin LI static int
1696f45c063aSXin LI vesa_mod_event(module_t mod, int type, void *data)
1697f45c063aSXin LI {
1698f45c063aSXin LI 	switch (type) {
1699f45c063aSXin LI 	case MOD_LOAD:
1700f45c063aSXin LI 		return vesa_load();
1701f45c063aSXin LI 	case MOD_UNLOAD:
1702f45c063aSXin LI 		return vesa_unload();
1703f45c063aSXin LI 	default:
1704f45c063aSXin LI 		return EOPNOTSUPP;
1705f45c063aSXin LI 	}
1706f45c063aSXin LI 	return 0;
1707f45c063aSXin LI }
1708f45c063aSXin LI 
1709f45c063aSXin LI static moduledata_t vesa_mod = {
1710f45c063aSXin LI 	"vesa",
1711f45c063aSXin LI 	vesa_mod_event,
1712f45c063aSXin LI 	NULL,
1713f45c063aSXin LI };
1714f45c063aSXin LI 
1715f45c063aSXin LI DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1716205d67b0SXin LI MODULE_DEPEND(vesa, x86bios, 1, 1, 1);
1717f45c063aSXin LI 
1718f45c063aSXin LI #endif	/* VGA_NO_MODE_CHANGE */
1719