xref: /dragonfly/sys/dev/video/fb/vga.c (revision dda92f98)
1 /*-
2  * (MPSAFE ?)
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * Copyright (c) 1992-1998 Søren Schmidt
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer as
13  *    the first lines of this file unmodified.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/dev/fb/vga.c,v 1.9.2.1 2001/08/11 02:58:44 yokota Exp $
32  */
33 
34 #include "opt_vga.h"
35 #include "opt_fb.h"
36 #ifndef FB_DEBUG
37 #define	FB_DEBUG	0
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/conf.h>
44 #include <sys/fcntl.h>
45 #include <sys/malloc.h>
46 #include <sys/fbio.h>
47 #include <sys/thread2.h>
48 
49 #include <bus/isa/isareg.h>
50 
51 #include <machine/clock.h>
52 #include <machine/md_var.h>
53 #include <machine/framebuffer.h>
54 #ifdef __x86_64__
55 #include <machine/pc/bios.h>
56 #endif
57 
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <vm/pmap.h>
61 
62 #include "fbreg.h"
63 #include "vgareg.h"
64 
65 #ifndef VGA_DEBUG
66 #define VGA_DEBUG		0
67 #endif
68 
69 int
70 vga_probe_unit(int unit, video_adapter_t *buf, int flags)
71 {
72 	video_adapter_t *adp;
73 	video_switch_t *sw;
74 	int error;
75 
76 	sw = vid_get_switch(VGA_DRIVER_NAME);
77 	if (sw == NULL)
78 		return 0;
79 	error = (*sw->probe)(unit, &adp, NULL, flags);
80 	if (error)
81 		return error;
82 	bcopy(adp, buf, sizeof(*buf));
83 	return 0;
84 }
85 
86 int
87 vga_attach_unit(int unit, vga_softc_t *sc, int flags)
88 {
89 	video_switch_t *sw;
90 	int error;
91 
92 	sw = vid_get_switch(VGA_DRIVER_NAME);
93 	if (sw == NULL)
94 		return ENXIO;
95 
96 	error = (*sw->probe)(unit, &sc->adp, NULL, flags);
97 	if (error)
98 		return error;
99 	return (*sw->init)(unit, sc->adp, flags);
100 }
101 
102 /* cdev driver functions */
103 
104 #ifdef FB_INSTALL_CDEV
105 
106 struct ucred;
107 
108 int
109 vga_open(cdev_t dev, vga_softc_t *sc, int flag, int mode, struct ucred *cred)
110 {
111 	if (sc == NULL)
112 		return ENXIO;
113 	if (mode & (O_CREAT | O_APPEND | O_TRUNC))
114 		return ENODEV;
115 
116 	return genfbopen(&sc->gensc, sc->adp, flag, mode, cred);
117 }
118 
119 int
120 vga_close(cdev_t dev, vga_softc_t *sc, int flag, int mode)
121 {
122 	return genfbclose(&sc->gensc, sc->adp, flag, mode);
123 }
124 
125 int
126 vga_read(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
127 {
128 	return genfbread(&sc->gensc, sc->adp, uio, flag);
129 }
130 
131 int
132 vga_write(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
133 {
134 	return genfbread(&sc->gensc, sc->adp, uio, flag);
135 }
136 
137 int
138 vga_ioctl(cdev_t dev, vga_softc_t *sc, u_long cmd, caddr_t arg, int flag,
139 	  struct ucred *cred)
140 {
141 	return genfbioctl(&sc->gensc, sc->adp, cmd, arg, flag, cred);
142 }
143 
144 int
145 vga_mmap(cdev_t dev, vga_softc_t *sc, vm_offset_t offset, int prot)
146 {
147 	return genfbmmap(&sc->gensc, sc->adp, offset, prot);
148 }
149 
150 #endif /* FB_INSTALL_CDEV */
151 
152 /* LOW-LEVEL */
153 
154 #define probe_done(adp)		((adp)->va_flags & V_ADP_PROBED)
155 #define init_done(adp)		((adp)->va_flags & V_ADP_INITIALIZED)
156 #define config_done(adp)	((adp)->va_flags & V_ADP_REGISTERED)
157 
158 /* various sizes */
159 #define V_MODE_MAP_SIZE		(M_VGA_CG320 + 1)
160 #define V_MODE_PARAM_SIZE	64
161 
162 /* video adapter state buffer */
163 struct adp_state {
164     int			sig;
165 #define V_STATE_SIG	0x736f6962
166     u_char		regs[V_MODE_PARAM_SIZE];
167 };
168 typedef struct adp_state adp_state_t;
169 
170 /*
171  * NOTE: `va_window' should have a virtual address, but is initialized
172  * with a physical address in the following table, as verify_adapter()
173  * will perform address conversion at run-time.
174  */
175 static video_adapter_t biosadapter = {
176     0, KD_VGA, VGA_DRIVER_NAME, 0, 0, V_ADP_COLOR, IO_VGA, 32,
177     EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
178     0, 0, 0, M_VGA_C80x25, M_C80x25, M_VGA_C80x25
179 };
180 
181 /* video driver declarations */
182 static int			vga_configure(int flags);
183 static int			vga_error(void);
184 static vi_probe_t		vga_probe;
185 static vi_init_t		vga_init;
186 static vi_get_info_t		vga_get_info;
187 static vi_query_mode_t		vga_query_mode;
188 static vi_set_mode_t		vga_set_mode;
189 static vi_save_font_t		vga_save_font;
190 static vi_load_font_t		vga_load_font;
191 static vi_show_font_t		vga_show_font;
192 static vi_save_palette_t	vga_save_palette;
193 static vi_load_palette_t	vga_load_palette;
194 static vi_set_border_t		vga_set_border;
195 static vi_save_state_t		vga_save_state;
196 static vi_load_state_t		vga_load_state;
197 static vi_set_win_org_t		vga_set_origin;
198 static vi_read_hw_cursor_t	vga_read_hw_cursor;
199 static vi_set_hw_cursor_t	vga_set_hw_cursor;
200 static vi_set_hw_cursor_shape_t	vga_set_hw_cursor_shape;
201 static vi_blank_display_t	vga_blank_display;
202 static vi_mmap_t		vga_mmap_buf;
203 static vi_ioctl_t		vga_dev_ioctl;
204 #ifndef VGA_NO_MODE_CHANGE
205 static vi_clear_t		vga_clear;
206 static vi_fill_rect_t		vga_fill_rect;
207 static vi_bitblt_t		vga_bitblt;
208 #else /* VGA_NO_MODE_CHANGE */
209 #define vga_clear		(vi_clear_t *)vga_error
210 #define vga_fill_rect		(vi_fill_rect_t *)vga_error
211 #define vga_bitblt		(vi_bitblt_t *)vga_error
212 #endif
213 static vi_diag_t		vga_diag;
214 
215 static video_switch_t vgavidsw = {
216 	vga_probe,
217 	vga_init,
218 	vga_get_info,
219 	vga_query_mode,
220 	vga_set_mode,
221 	vga_save_font,
222 	vga_load_font,
223 	vga_show_font,
224 	vga_save_palette,
225 	vga_load_palette,
226 	vga_set_border,
227 	vga_save_state,
228 	vga_load_state,
229 	vga_set_origin,
230 	vga_read_hw_cursor,
231 	vga_set_hw_cursor,
232 	vga_set_hw_cursor_shape,
233 	vga_blank_display,
234 	vga_mmap_buf,
235 	vga_dev_ioctl,
236 	vga_clear,
237 	vga_fill_rect,
238 	vga_bitblt,
239 	vga_error,
240 	vga_error,
241 	vga_diag,
242 };
243 
244 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
245 
246 /* VGA BIOS standard video modes */
247 #define EOT		(-1)
248 #define NA		(-2)
249 
250 static video_info_t bios_vmode[] = {
251     /* CGA */
252     { M_B40x25,     V_INFO_COLOR, 40, 25, 8,  8, 2, 1,
253       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
254     { M_C40x25,     V_INFO_COLOR, 40, 25, 8,  8, 4, 1,
255       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
256     { M_B80x25,     V_INFO_COLOR, 80, 25, 8,  8, 2, 1,
257       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
258     { M_C80x25,     V_INFO_COLOR, 80, 25, 8,  8, 4, 1,
259       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
260     /* EGA */
261     { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
262       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
263     { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
264       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
265     { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
266       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
267     { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
268       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
269     /* VGA */
270     { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
271       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
272     { M_VGA_M80x25, 0,            80, 25, 8, 16, 2, 1,
273       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
274     { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
275       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
276     /* MDA */
277     { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1,
278       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
279     /* EGA */
280     { M_ENH_B80x43, 0,            80, 43, 8,  8, 2, 1,
281       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
282     { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8,  8, 4, 1,
283       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
284     /* VGA */
285     { M_VGA_M80x30, 0,            80, 30, 8, 16, 2, 1,
286       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
287     { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
288       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
289     { M_VGA_M80x50, 0,            80, 50, 8,  8, 2, 1,
290       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
291     { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8,  8, 4, 1,
292       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
293     { M_VGA_M80x60, 0,            80, 60, 8,  8, 2, 1,
294       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
295     { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8,  8, 4, 1,
296       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
297 
298 #ifndef VGA_NO_MODE_CHANGE
299 
300 #ifdef VGA_WIDTH90
301     { M_VGA_M90x25, 0,            90, 25, 8, 16, 2, 1,
302       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
303     { M_VGA_C90x25, V_INFO_COLOR, 90, 25, 8, 16, 4, 1,
304       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
305     { M_VGA_M90x30, 0,            90, 30, 8, 16, 2, 1,
306       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
307     { M_VGA_C90x30, V_INFO_COLOR, 90, 30, 8, 16, 4, 1,
308       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
309     { M_VGA_M90x43, 0,            90, 43, 8,  8, 2, 1,
310       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
311     { M_VGA_C90x43, V_INFO_COLOR, 90, 43, 8,  8, 4, 1,
312       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
313     { M_VGA_M90x50, 0,            90, 50, 8,  8, 2, 1,
314       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
315     { M_VGA_C90x50, V_INFO_COLOR, 90, 50, 8,  8, 4, 1,
316       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
317     { M_VGA_M90x60, 0,            90, 60, 8,  8, 2, 1,
318       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
319     { M_VGA_C90x60, V_INFO_COLOR, 90, 60, 8,  8, 4, 1,
320       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
321 #endif /* VGA_WIDTH90 */
322 
323     /* CGA */
324     { M_BG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
325       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
326     { M_CG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
327       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
328     { M_BG640,      V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 1, 1,
329       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
330     /* EGA */
331     { M_CG320_D,    V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 4, 4,
332       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
333       V_INFO_MM_PLANAR },
334     { M_CG640_E,    V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 4, 4,
335       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
336       V_INFO_MM_PLANAR },
337     { M_EGAMONOAPA, V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
338       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 ,
339       V_INFO_MM_PLANAR },
340     { M_ENHMONOAPA2,V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
341       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
342       V_INFO_MM_PLANAR },
343     { M_CG640x350,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
344       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
345       V_INFO_MM_PLANAR },
346     { M_ENH_CG640,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
347       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
348       V_INFO_MM_PLANAR },
349     /* VGA */
350     { M_BG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
351       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
352       V_INFO_MM_PLANAR },
353     { M_CG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
354       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
355       V_INFO_MM_PLANAR },
356     { M_VGA_CG320,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 8, 1,
357       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
358       V_INFO_MM_PACKED, 1 },
359     { M_VGA_MODEX,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8,  8, 8, 4,
360       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
361       V_INFO_MM_VGAX, 1 },
362 #endif /* VGA_NO_MODE_CHANGE */
363 
364     { EOT },
365 };
366 
367 static int		vga_init_done = FALSE;
368 #ifndef VGA_NO_MODE_CHANGE
369 static u_char		*video_mode_ptr = NULL;
370 #endif
371 static u_char		*mode_map[V_MODE_MAP_SIZE];
372 static adp_state_t	adpstate;
373 static adp_state_t	adpstate2;
374 static int		rows_offset = 1;
375 
376 /* local macros and functions */
377 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
378 
379 #ifndef VGA_NO_MODE_CHANGE
380 static void map_mode_table(u_char **, u_char *);
381 static int map_mode_num(int);
382 static int map_bios_mode_num(int);
383 #endif
384 static u_char *get_mode_param(int);
385 static int verify_adapter(video_adapter_t *);
386 static void update_adapter_info(video_adapter_t *, video_info_t *);
387 static int probe_adapters(void);
388 static int set_line_length(video_adapter_t *, int);
389 static int set_display_start(video_adapter_t *, int, int);
390 
391 #ifndef VGA_NO_MODE_CHANGE
392 #ifdef VGA_WIDTH90
393 static void set_width90(adp_state_t *);
394 #endif
395 #endif /* !VGA_NO_MODE_CHANGE */
396 
397 #ifndef VGA_NO_FONT_LOADING
398 #define PARAM_BUFSIZE	6
399 static void set_font_mode(video_adapter_t *, u_char *);
400 static void set_normal_mode(video_adapter_t *, u_char *);
401 #endif
402 
403 #ifndef VGA_NO_MODE_CHANGE
404 static void filll_io(int, vm_offset_t, size_t);
405 static void planar_fill(video_adapter_t *, int);
406 static void packed_fill(video_adapter_t *, int);
407 static void direct_fill(video_adapter_t *, int);
408 #endif /* !VGA_NO_MODE_CHANGE */
409 
410 #define	ISMAPPED(pa, width)				\
411 	(((pa) <= (u_long)0x1000 - (width)) 		\
412 	 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
413 
414 #define	prologue(adp, flag, err)			\
415 	if (!vga_init_done || !((adp)->va_flags & (flag)))	\
416 	    return (err)
417 
418 /* a backdoor for the console driver */
419 static int
420 vga_configure(int flags)
421 {
422     /* No VGA when booted via UEFI */
423     if (probe_efi_fb(0) == 0)
424         return 0;	/* XXX nobody uses this return value */
425 
426     /* XXX Also abort if VGA isn't present according to ACPI FADT BootFlags */
427 
428     probe_adapters();
429     if (probe_done(&biosadapter)) {
430 	biosadapter.va_flags |= V_ADP_INITIALIZED;
431 	if (!config_done(&biosadapter) && !(vid_register(&biosadapter) < 0))
432 	    biosadapter.va_flags |= V_ADP_REGISTERED;
433     }
434 
435     return 1;
436 }
437 
438 /* local subroutines */
439 
440 #ifndef VGA_NO_MODE_CHANGE
441 /* construct the mode parameter map (accept 40x25, 80x25 and 80x30 modes) */
442 static void
443 map_mode_table(u_char *map[], u_char *table)
444 {
445     int i, valid;
446 
447     for(i = 0; i < V_MODE_MAP_SIZE; ++i) {
448 	map[i] = table + i*V_MODE_PARAM_SIZE;
449 	valid = 0;
450 	if ((map[i][0] == 40 && map[i][1] == 24) ||
451 	    (map[i][0] == 80 && (map[i][1] == 24 || map[i][1] == 29)))
452 	    valid++;
453 	if (!valid)
454 	    map[i] = NULL;
455     }
456 }
457 #endif /* !VGA_NO_MODE_CHANGE */
458 
459 #ifndef VGA_NO_MODE_CHANGE
460 /* map the non-standard video mode to a known mode number */
461 static int
462 map_mode_num(int mode)
463 {
464     static struct {
465         int from;
466         int to;
467     } mode_map[] = {
468         { M_ENH_B80x43, M_ENH_B80x25 },
469         { M_ENH_C80x43, M_ENH_C80x25 },
470         { M_VGA_M80x30, M_VGA_M80x25 },
471         { M_VGA_C80x30, M_VGA_C80x25 },
472         { M_VGA_M80x50, M_VGA_M80x25 },
473         { M_VGA_C80x50, M_VGA_C80x25 },
474         { M_VGA_M80x60, M_VGA_M80x25 },
475         { M_VGA_C80x60, M_VGA_C80x25 },
476 #ifdef VGA_WIDTH90
477         { M_VGA_M90x25, M_VGA_M80x25 },
478         { M_VGA_C90x25, M_VGA_C80x25 },
479         { M_VGA_M90x30, M_VGA_M80x25 },
480         { M_VGA_C90x30, M_VGA_C80x25 },
481         { M_VGA_M90x43, M_ENH_B80x25 },
482         { M_VGA_C90x43, M_ENH_C80x25 },
483         { M_VGA_M90x50, M_VGA_M80x25 },
484         { M_VGA_C90x50, M_VGA_C80x25 },
485         { M_VGA_M90x60, M_VGA_M80x25 },
486         { M_VGA_C90x60, M_VGA_C80x25 },
487 #endif
488         { M_VGA_MODEX,  M_VGA_CG320 },
489     };
490     int i;
491 
492     for (i = 0; i < NELEM(mode_map); ++i) {
493         if (mode_map[i].from == mode)
494             return mode_map[i].to;
495     }
496     return mode;
497 }
498 
499 /* turn the BIOS video number into our video mode number */
500 static int
501 map_bios_mode_num(int bios_mode)
502 {
503     static int vga_modes[20] = {
504 	M_VGA_C40x25, M_VGA_C40x25,	/* 0, 1 */
505 	M_VGA_C80x25, M_VGA_C80x25,	/* 2, 3 */
506 	M_BG320, M_CG320,
507 	M_BG640,
508 	M_VGA_M80x25,			/* 7 */
509 	8, 9, 10, 11, 12,
510 	M_CG320_D,
511 	M_CG640_E,
512 	M_ENHMONOAPA2,
513 	M_ENH_CG640,
514 	M_BG640x480, M_CG640x480,
515 	M_VGA_CG320,
516     };
517 
518     if (bios_mode < NELEM(vga_modes))
519 	return vga_modes[bios_mode];
520 
521     return M_VGA_C80x25;
522 }
523 #endif /* !VGA_NO_MODE_CHANGE */
524 
525 /* look up a parameter table entry */
526 static u_char *
527 get_mode_param(int mode)
528 {
529 #ifndef VGA_NO_MODE_CHANGE
530     if (mode >= V_MODE_MAP_SIZE)
531 	mode = map_mode_num(mode);
532 #endif
533     if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
534 	return mode_map[mode];
535     else
536 	return NULL;
537 }
538 
539 static int
540 verify_adapter(video_adapter_t *adp)
541 {
542     vm_offset_t buf;
543     u_int16_t v;
544 #ifndef VGA_NO_MODE_CHANGE
545     u_int32_t p;
546 #endif
547 
548     buf = BIOS_PADDRTOVADDR(adp->va_window);
549     v = readw(buf);
550     writew(buf, 0xA55A);
551     if (readw(buf) != 0xA55A)
552 	return ENXIO;
553     writew(buf, v);
554 
555     outb(CRTC, 7);
556     if (inb(CRTC) != 7)
557 	return ENXIO;
558 
559     adp->va_flags |= V_ADP_STATELOAD | V_ADP_STATESAVE | V_ADP_PALETTE |
560 	V_ADP_BORDER;
561 
562 #ifndef VGA_NO_MODE_CHANGE
563     /* get the BIOS video mode pointer */
564     p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
565     p = BIOS_SADDRTOLADDR(p);
566     if (ISMAPPED(p, sizeof(u_int32_t))) {
567 	p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
568 	p = BIOS_SADDRTOLADDR(p);
569 	if (ISMAPPED(p, V_MODE_PARAM_SIZE))
570 	    video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
571     }
572 #endif
573 
574     return 0;
575 }
576 
577 static void
578 update_adapter_info(video_adapter_t *adp, video_info_t *info)
579 {
580     adp->va_flags |= V_ADP_COLOR;
581     adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
582     adp->va_window_size = info->vi_window_size;
583     adp->va_window_gran = info->vi_window_gran;
584     adp->va_window_orig = 0;
585     /* XXX */
586     adp->va_buffer = info->vi_buffer;
587     adp->va_buffer_size = info->vi_buffer_size;
588     if (info->vi_mem_model == V_INFO_MM_VGAX) {
589 	adp->va_line_width = info->vi_width/2;
590     } else if (info->vi_flags & V_INFO_GRAPHICS) {
591 	switch (info->vi_depth/info->vi_planes) {
592 	case 1:
593 	    adp->va_line_width = info->vi_width/8;
594 	    break;
595 	case 2:
596 	    adp->va_line_width = info->vi_width/4;
597 	    break;
598 	case 4:
599 	    adp->va_line_width = info->vi_width/2;
600 	    break;
601 	case 8:
602 	default: /* shouldn't happen */
603 	    adp->va_line_width = info->vi_width;
604 	    break;
605 	}
606     } else {
607 	adp->va_line_width = info->vi_width;
608     }
609     adp->va_disp_start.x = 0;
610     adp->va_disp_start.y = 0;
611     bcopy(info, &adp->va_info, sizeof(adp->va_info));
612 }
613 
614 /* probe video adapters and return the number of detected adapters */
615 static int
616 probe_adapters(void)
617 {
618     video_adapter_t *adp;
619     video_info_t info;
620 #ifndef VGA_NO_MODE_CHANGE
621     u_char *mp;
622 #endif
623     int i;
624 
625     /* do this test only once */
626     if (vga_init_done)
627 	return 1;
628     vga_init_done = TRUE;
629 
630     if (verify_adapter(&biosadapter) != 0)
631 	return 0;
632 
633     biosadapter.va_flags |= V_ADP_PROBED;
634 #ifndef VGA_NO_MODE_CHANGE
635     biosadapter.va_initial_bios_mode = readb(BIOS_PADDRTOVADDR(0x449));
636     biosadapter.va_mode = biosadapter.va_initial_mode =
637 	map_bios_mode_num(biosadapter.va_initial_bios_mode);
638 #endif
639 
640     /*
641      * Ensure a zero start address. The registers are w/o for old
642      * hardware so it's too hard to relocate the active screen
643      * memory.
644      * This must be done before vga_save_state() for VGA.
645      */
646     outb(CRTC, 12);
647     outb(CRTC + 1, 0);
648     outb(CRTC, 13);
649     outb(CRTC + 1, 0);
650 
651     /* the video mode parameter table in VGA BIOS */
652     /* NOTE: there can be only one VGA recognized by the video BIOS.
653      */
654     adp = &biosadapter;
655     bzero(mode_map, sizeof(mode_map));
656     vga_save_state(adp, &adpstate, sizeof(adpstate));
657     for(i = 0; i < 16; i++)
658 	adp->va_palette_regs[i] = adpstate.regs[35 + i];
659 #ifdef VGA_NO_MODE_CHANGE
660     mode_map[adp->va_initial_mode] = adpstate.regs;
661     rows_offset = 1;
662 #else /* !VGA_NO_MODE_CHANGE */
663     if (video_mode_ptr == NULL) {
664 	mode_map[adp->va_initial_mode] = adpstate.regs;
665 	rows_offset = 1;
666     } else {
667 	/* discard modes that we are not familiar with */
668 	map_mode_table(mode_map, video_mode_ptr);
669 	mp = get_mode_param(adp->va_initial_mode);
670 #ifndef VGA_KEEP_POWERON_MODE
671 	if (mp != NULL) {
672 	    bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
673 	    rows_offset = adpstate.regs[1] + 1 - mp[1];
674 	} else
675 #endif
676 	{
677 	    mode_map[adp->va_initial_mode] = adpstate.regs;
678 	    rows_offset = 1;
679 	}
680     }
681 #endif /* VGA_NO_MODE_CHANGE */
682 
683 #ifndef VGA_NO_MODE_CHANGE
684     adp->va_flags |= V_ADP_MODECHANGE;
685 #endif
686 #ifndef VGA_NO_FONT_LOADING
687     adp->va_flags |= V_ADP_FONT;
688 #endif
689 
690     /* XXX remove conflicting modes */
691     for (i = 0; i < M_VGA_CG320; i++) {
692 	if (vga_get_info(&biosadapter, i, &info))
693 	    continue;
694 	if ((info.vi_flags & V_INFO_COLOR) != V_ADP_COLOR)
695 	    mode_map[i] = NULL;
696     }
697 
698     /* buffer address */
699     vga_get_info(&biosadapter, biosadapter.va_initial_mode, &info);
700     info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
701     update_adapter_info(&biosadapter, &info);
702 
703     /*
704      * XXX: we should verify the following values for the primary adapter...
705      * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
706      * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
707      *                     ? 0 : V_ADP_COLOR;
708      * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
709      * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
710      * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
711      * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
712      */
713 
714     return 1;
715 }
716 
717 /* set the scan line length in pixel */
718 static int
719 set_line_length(video_adapter_t *adp, int pixel)
720 {
721     u_char *mp;
722     int ppw;	/* pixels per word */
723     int bpl;	/* bytes per line */
724     int count;
725 
726     mp = get_mode_param(adp->va_mode);
727     if (mp == NULL)
728 	return EINVAL;
729 
730     switch (adp->va_info.vi_mem_model) {
731     case V_INFO_MM_PLANAR:
732 	ppw = 16/(adp->va_info.vi_depth/adp->va_info.vi_planes);
733 	count = (pixel + ppw - 1)/ppw/2;
734 	bpl = ((pixel + ppw - 1)/ppw/2)*4;
735 	break;
736     case V_INFO_MM_PACKED:
737 	count = (pixel + 7)/8;
738 	bpl = rounddown(pixel + 7, 8);
739 	break;
740     case V_INFO_MM_TEXT:
741 	count = (pixel + 7)/8;			/* columns */
742 	bpl = (pixel + 7)/8;			/* columns */
743 	break;
744     default:
745 	return ENODEV;
746     }
747 
748     if (mp[10 + 0x17] & 0x40)			/* CRTC mode control reg */
749 	count *= 2;				/* byte mode */
750     outb(CRTC, 0x13);
751     outb(CRTC + 1, count);
752     adp->va_line_width = bpl;
753 
754     return 0;
755 }
756 
757 static int
758 set_display_start(video_adapter_t *adp, int x, int y)
759 {
760     int off;	/* byte offset (graphics mode)/word offset (text mode) */
761     int poff;	/* pixel offset */
762     int roff;	/* row offset */
763     int ppb;	/* pixels per byte */
764 
765     if (adp->va_info.vi_flags & V_INFO_GRAPHICS) {
766 	ppb = 8/(adp->va_info.vi_depth/adp->va_info.vi_planes);
767 	off = y*adp->va_line_width + x/ppb;
768 	roff = 0;
769 	poff = x%ppb;
770     } else {
771 	outb(TSIDX, 1);
772 	if (inb(TSREG) & 1)
773 	    ppb = 9;
774 	else
775 	    ppb = 8;
776 	off = y/adp->va_info.vi_cheight*adp->va_line_width + x/ppb;
777 	roff = y%adp->va_info.vi_cheight;
778 	/* FIXME: is this correct? XXX */
779 	if (ppb == 8)
780 	    poff = x%ppb;
781 	else
782 	    poff = (x + 8)%ppb;
783     }
784 
785     /* start address */
786     outb(CRTC, 0xc);		/* high */
787     outb(CRTC + 1, off >> 8);
788     outb(CRTC, 0xd);		/* low */
789     outb(CRTC + 1, off & 0xff);
790 
791     /* horizontal pel pan */
792     inb(CRTC + 6);
793     outb(ATC, 0x13 | 0x20);
794     outb(ATC, poff);
795     inb(CRTC + 6);
796     outb(ATC, 0x20);
797 
798     /* preset row scan */
799     outb(CRTC, 8);
800     outb(CRTC + 1, roff);
801 
802     adp->va_disp_start.x = x;
803     adp->va_disp_start.y = y;
804     return 0;
805 }
806 
807 #ifndef VGA_NO_MODE_CHANGE
808 #if defined(__x86_64__)	/* XXX */
809 static void
810 fill(int val, void *d, size_t size)
811 {
812     u_char *p = d;
813 
814     while (size-- > 0)
815 	*p++ = val;
816 }
817 #endif /* __x86_64__ */
818 
819 static void
820 filll_io(int val, vm_offset_t d, size_t size)
821 {
822     while (size-- > 0) {
823 	writel(d, val);
824 	d += sizeof(u_int32_t);
825     }
826 }
827 #endif /* !VGA_NO_MODE_CHANGE */
828 
829 /* entry points */
830 
831 static int
832 vga_error(void)
833 {
834     return ENODEV;
835 }
836 
837 static int
838 vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
839 {
840     /* No VGA when booted via UEFI */
841     if (probe_efi_fb(0) == 0)
842         return (ENXIO);
843 
844     /* XXX Also abort if VGA isn't present according to ACPI FADT BootFlags */
845 
846     probe_adapters();
847     if (unit != 0)
848 	return ENXIO;
849 
850     *adpp = &biosadapter;
851 
852     return 0;
853 }
854 
855 static int
856 vga_init(int unit, video_adapter_t *adp, int flags)
857 {
858     if ((unit != 0) || (adp == NULL) || !probe_done(adp))
859 	return ENXIO;
860 
861     if (!init_done(adp)) {
862 	/* nothing to do really... */
863 	adp->va_flags |= V_ADP_INITIALIZED;
864     }
865 
866     if (!config_done(adp)) {
867 	if (vid_register(adp) < 0)
868 		return ENXIO;
869 	adp->va_flags |= V_ADP_REGISTERED;
870     }
871 
872     return 0;
873 }
874 
875 /*
876  * get_info():
877  * Return the video_info structure of the requested video mode.
878  */
879 static int
880 vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
881 {
882     int i;
883 
884     if (!vga_init_done)
885 	return ENXIO;
886 
887 #ifndef VGA_NO_MODE_CHANGE
888     if (adp->va_flags & V_ADP_MODECHANGE) {
889 	/*
890 	 * If the parameter table entry for this mode is not found,
891 	 * the mode is not supported...
892 	 */
893 	if (get_mode_param(mode) == NULL)
894 	    return EINVAL;
895     } else
896 #endif /* VGA_NO_MODE_CHANGE */
897     {
898 	/*
899 	 * Even if we don't support video mode switching on this adapter,
900 	 * the information on the initial (thus current) video mode
901 	 * should be made available.
902 	 */
903 	if (mode != adp->va_initial_mode)
904 	    return EINVAL;
905     }
906 
907     for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
908 	if (bios_vmode[i].vi_mode == NA)
909 	    continue;
910 	if (mode == bios_vmode[i].vi_mode) {
911 	    *info = bios_vmode[i];
912 	    /* XXX */
913 	    info->vi_buffer_size = info->vi_window_size*info->vi_planes;
914 	    return 0;
915 	}
916     }
917     return EINVAL;
918 }
919 
920 /*
921  * query_mode():
922  * Find a video mode matching the requested parameters.
923  * Fields filled with 0 are considered "don't care" fields and
924  * match any modes.
925  */
926 static int
927 vga_query_mode(video_adapter_t *adp, video_info_t *info)
928 {
929     int i;
930 
931     if (!vga_init_done)
932 	return ENXIO;
933 
934     for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
935 	if (bios_vmode[i].vi_mode == NA)
936 	    continue;
937 
938 	if ((info->vi_width != 0)
939 	    && (info->vi_width != bios_vmode[i].vi_width))
940 		continue;
941 	if ((info->vi_height != 0)
942 	    && (info->vi_height != bios_vmode[i].vi_height))
943 		continue;
944 	if ((info->vi_cwidth != 0)
945 	    && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
946 		continue;
947 	if ((info->vi_cheight != 0)
948 	    && (info->vi_cheight != bios_vmode[i].vi_cheight))
949 		continue;
950 	if ((info->vi_depth != 0)
951 	    && (info->vi_depth != bios_vmode[i].vi_depth))
952 		continue;
953 	if ((info->vi_planes != 0)
954 	    && (info->vi_planes != bios_vmode[i].vi_planes))
955 		continue;
956 	/* XXX: should check pixel format, memory model */
957 	if ((info->vi_flags != 0)
958 	    && (info->vi_flags != bios_vmode[i].vi_flags))
959 		continue;
960 
961 	/* verify if this mode is supported on this adapter */
962 	if (vga_get_info(adp, bios_vmode[i].vi_mode, info))
963 		continue;
964 	return 0;
965     }
966     return ENODEV;
967 }
968 
969 /*
970  * set_mode():
971  * Change the video mode.
972  */
973 
974 #ifndef VGA_NO_MODE_CHANGE
975 #ifdef VGA_WIDTH90
976 static void
977 set_width90(adp_state_t *params)
978 {
979     /*
980      * Based on code submitted by Kelly Yancey (kbyanc@freedomnet.com)
981      * and alexv@sui.gda.itesm.mx.
982      */
983     params->regs[5] |= 1;		/* toggle 8 pixel wide fonts */
984     params->regs[10+0x0] = 0x6b;
985     params->regs[10+0x1] = 0x59;
986     params->regs[10+0x2] = 0x5a;
987     params->regs[10+0x3] = 0x8e;
988     params->regs[10+0x4] = 0x5e;
989     params->regs[10+0x5] = 0x8a;
990     params->regs[10+0x13] = 45;
991     params->regs[35+0x13] = 0;
992 }
993 #endif /* VGA_WIDTH90 */
994 #endif /* !VGA_NO_MODE_CHANGE */
995 
996 static int
997 vga_set_mode(video_adapter_t *adp, int mode)
998 {
999 #ifndef VGA_NO_MODE_CHANGE
1000     video_info_t info;
1001     adp_state_t params;
1002 
1003     prologue(adp, V_ADP_MODECHANGE, ENODEV);
1004 
1005     if (vga_get_info(adp, mode, &info))
1006 	return EINVAL;
1007 
1008     lwkt_gettoken(&vga_token);
1009 
1010 #if VGA_DEBUG > 1
1011     kprintf("vga_set_mode(): setting mode %d\n", mode);
1012 #endif
1013 
1014     params.sig = V_STATE_SIG;
1015     bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1016 
1017     switch (mode) {
1018 #ifdef VGA_WIDTH90
1019     case M_VGA_C90x60: case M_VGA_M90x60:
1020 	set_width90(&params);
1021 	/* FALLTHROUGH */
1022 #endif
1023     case M_VGA_C80x60: case M_VGA_M80x60:
1024 	params.regs[2]  = 0x08;
1025 	params.regs[19] = 0x47;
1026 	goto special_480l;
1027 
1028 #ifdef VGA_WIDTH90
1029     case M_VGA_C90x30: case M_VGA_M90x30:
1030 	set_width90(&params);
1031 	/* FALLTHROUGH */
1032 #endif
1033     case M_VGA_C80x30: case M_VGA_M80x30:
1034 	params.regs[19] = 0x4f;
1035 special_480l:
1036 	params.regs[9] |= 0xc0;
1037 	params.regs[16] = 0x08;
1038 	params.regs[17] = 0x3e;
1039 	params.regs[26] = 0xea;
1040 	params.regs[28] = 0xdf;
1041 	params.regs[31] = 0xe7;
1042 	params.regs[32] = 0x04;
1043 	goto setup_mode;
1044 
1045 #ifdef VGA_WIDTH90
1046     case M_VGA_C90x43: case M_VGA_M90x43:
1047 	set_width90(&params);
1048 	/* FALLTHROUGH */
1049 #endif
1050     case M_ENH_C80x43: case M_ENH_B80x43:
1051 	params.regs[28] = 87;
1052 	goto special_80x50;
1053 
1054 #ifdef VGA_WIDTH90
1055     case M_VGA_C90x50: case M_VGA_M90x50:
1056 	set_width90(&params);
1057 	/* FALLTHROUGH */
1058 #endif
1059     case M_VGA_C80x50: case M_VGA_M80x50:
1060 special_80x50:
1061 	params.regs[2] = 8;
1062 	params.regs[19] = 7;
1063 	goto setup_mode;
1064 
1065 #ifdef VGA_WIDTH90
1066     case M_VGA_C90x25: case M_VGA_M90x25:
1067 	set_width90(&params);
1068 	/* FALLTHROUGH */
1069 #endif
1070     case M_VGA_C40x25: case M_VGA_C80x25:
1071     case M_VGA_M80x25:
1072     case M_B40x25:     case M_C40x25:
1073     case M_B80x25:     case M_C80x25:
1074     case M_ENH_B40x25: case M_ENH_C40x25:
1075     case M_ENH_B80x25: case M_ENH_C80x25:
1076     case M_EGAMONO80x25:
1077 
1078 setup_mode:
1079 	vga_load_state(adp, &params);
1080 	break;
1081 
1082     case M_VGA_MODEX:
1083 	/* "unchain" the VGA mode */
1084 	params.regs[5-1+0x04] &= 0xf7;
1085 	params.regs[5-1+0x04] |= 0x04;
1086 	/* turn off doubleword mode */
1087 	params.regs[10+0x14] &= 0xbf;
1088 	/* turn off word addressing */
1089 	params.regs[10+0x17] |= 0x40;
1090 	/* set logical screen width */
1091 	params.regs[10+0x13] = 80;
1092 	/* set 240 lines */
1093 	params.regs[10+0x11] = 0x2c;
1094 	params.regs[10+0x06] = 0x0d;
1095 	params.regs[10+0x07] = 0x3e;
1096 	params.regs[10+0x10] = 0xea;
1097 	params.regs[10+0x11] = 0xac;
1098 	params.regs[10+0x12] = 0xdf;
1099 	params.regs[10+0x15] = 0xe7;
1100 	params.regs[10+0x16] = 0x06;
1101 	/* set vertical sync polarity to reflect aspect ratio */
1102 	params.regs[9] = 0xe3;
1103 	goto setup_grmode;
1104 
1105     case M_BG320:     case M_CG320:     case M_BG640:
1106     case M_CG320_D:   case M_CG640_E:
1107     case M_CG640x350: case M_ENH_CG640:
1108     case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1109 
1110 setup_grmode:
1111 	vga_load_state(adp, &params);
1112 	break;
1113 
1114     default:
1115         lwkt_reltoken(&vga_token);
1116 	return EINVAL;
1117     }
1118 
1119     adp->va_mode = mode;
1120     info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1121     update_adapter_info(adp, &info);
1122 
1123     /* move hardware cursor out of the way */
1124     (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1125 
1126     lwkt_reltoken(&vga_token);
1127     return 0;
1128 #else /* VGA_NO_MODE_CHANGE */
1129     return ENODEV;
1130 #endif /* VGA_NO_MODE_CHANGE */
1131 }
1132 
1133 #ifndef VGA_NO_FONT_LOADING
1134 
1135 static void
1136 set_font_mode(video_adapter_t *adp, u_char *buf)
1137 {
1138     crit_enter();
1139 
1140     /* save register values */
1141     outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1142     outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1143     outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1144     outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1145     outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1146     inb(CRTC + 6);
1147     outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1148 
1149     /* setup vga for loading fonts */
1150     inb(CRTC + 6);				/* reset flip-flop */
1151     outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1152     inb(CRTC + 6);				/* reset flip-flop */
1153     outb(ATC, 0x20);				/* enable palette */
1154     outw(TSIDX, 0x0402);
1155     outw(TSIDX, 0x0704);
1156     outw(GDCIDX, 0x0204);
1157     outw(GDCIDX, 0x0005);
1158     outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
1159 
1160     crit_exit();
1161 }
1162 
1163 static void
1164 set_normal_mode(video_adapter_t *adp, u_char *buf)
1165 {
1166     crit_enter();
1167 
1168     /* setup vga for normal operation mode again */
1169     inb(CRTC + 6);				/* reset flip-flop */
1170     outb(ATC, 0x10); outb(ATC, buf[5]);
1171     inb(CRTC + 6);				/* reset flip-flop */
1172     outb(ATC, 0x20);				/* enable palette */
1173     outw(TSIDX, 0x0002 | (buf[0] << 8));
1174     outw(TSIDX, 0x0004 | (buf[1] << 8));
1175     outw(GDCIDX, 0x0004 | (buf[2] << 8));
1176     outw(GDCIDX, 0x0005 | (buf[3] << 8));
1177     outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1178 
1179     crit_exit();
1180 }
1181 
1182 #endif /* VGA_NO_FONT_LOADING */
1183 
1184 /*
1185  * save_font():
1186  * Read the font data in the requested font page from the video adapter.
1187  */
1188 static int
1189 vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1190 	      int ch, int count)
1191 {
1192 #ifndef VGA_NO_FONT_LOADING
1193     u_char buf[PARAM_BUFSIZE];
1194     vm_offset_t segment;
1195     int c;
1196 
1197     prologue(adp, V_ADP_FONT, ENODEV);
1198 
1199     if (fontsize < 14) {
1200 	/* FONT_8 */
1201 	fontsize = 8;
1202     } else if (fontsize >= 32) {
1203 	fontsize = 32;
1204     } else if (fontsize >= 16) {
1205 	/* FONT_16 */
1206 	fontsize = 16;
1207     } else {
1208 	/* FONT_14 */
1209 	fontsize = 14;
1210     }
1211 
1212     if (page < 0 || page >= 8)
1213 	return EINVAL;
1214     segment = FONT_BUF + 0x4000*page;
1215     if (page > 3)
1216 	segment -= 0xe000;
1217 
1218     set_font_mode(adp, buf);
1219     if (fontsize == 32) {
1220 	bcopy_fromio((uintptr_t)segment + ch*32, data, fontsize*count);
1221     } else {
1222 	for (c = ch; count > 0; ++c, --count) {
1223 	    bcopy_fromio((uintptr_t)segment + c*32, data, fontsize);
1224 	    data += fontsize;
1225 	}
1226     }
1227     set_normal_mode(adp, buf);
1228 
1229     return 0;
1230 #else /* VGA_NO_FONT_LOADING */
1231     return ENODEV;
1232 #endif /* VGA_NO_FONT_LOADING */
1233 }
1234 
1235 /*
1236  * load_font():
1237  * Set the font data in the requested font page.
1238  * NOTE: it appears that some recent video adapters do not support
1239  * the font page other than 0... XXX
1240  */
1241 static int
1242 vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1243 	      int ch, int count)
1244 {
1245 #ifndef VGA_NO_FONT_LOADING
1246     u_char buf[PARAM_BUFSIZE];
1247     vm_offset_t segment;
1248     int c;
1249 
1250     prologue(adp, V_ADP_FONT, ENODEV);
1251 
1252     if (fontsize < 14) {
1253 	/* FONT_8 */
1254 	fontsize = 8;
1255     } else if (fontsize >= 32) {
1256 	fontsize = 32;
1257     } else if (fontsize >= 16) {
1258 	/* FONT_16 */
1259 	fontsize = 16;
1260     } else {
1261 	/* FONT_14 */
1262 	fontsize = 14;
1263     }
1264 
1265     if (page < 0 || page >= 8)
1266 	return EINVAL;
1267     segment = FONT_BUF + 0x4000*page;
1268     if (page > 3)
1269 	segment -= 0xe000;
1270 
1271     set_font_mode(adp, buf);
1272     if (fontsize == 32) {
1273 	bcopy_toio(data, (uintptr_t)segment + ch*32, fontsize*count);
1274     } else {
1275 	for (c = ch; count > 0; ++c, --count) {
1276 	    bcopy_toio(data, (uintptr_t)segment + c*32, fontsize);
1277 	    data += fontsize;
1278 	}
1279     }
1280     set_normal_mode(adp, buf);
1281 
1282     return 0;
1283 #else /* VGA_NO_FONT_LOADING */
1284     return ENODEV;
1285 #endif /* VGA_NO_FONT_LOADING */
1286 }
1287 
1288 /*
1289  * show_font():
1290  * Activate the requested font page.
1291  * NOTE: it appears that some recent video adapters do not support
1292  * the font page other than 0... XXX
1293  */
1294 static int
1295 vga_show_font(video_adapter_t *adp, int page)
1296 {
1297 #ifndef VGA_NO_FONT_LOADING
1298     static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1299 
1300     prologue(adp, V_ADP_FONT, ENODEV);
1301     if (page < 0 || page >= 8)
1302 	return EINVAL;
1303 
1304     crit_enter();
1305     outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1306     crit_exit();
1307 
1308     return 0;
1309 #else /* VGA_NO_FONT_LOADING */
1310     return ENODEV;
1311 #endif /* VGA_NO_FONT_LOADING */
1312 }
1313 
1314 /*
1315  * save_palette():
1316  * Read DAC values. The values have expressed in 8 bits.
1317  *
1318  * VGA
1319  */
1320 static int
1321 vga_save_palette(video_adapter_t *adp, u_char *palette)
1322 {
1323     int i;
1324 
1325     prologue(adp, V_ADP_PALETTE, ENODEV);
1326 
1327     /*
1328      * We store 8 bit values in the palette buffer, while the standard
1329      * VGA has 6 bit DAC .
1330      */
1331     outb(PALRADR, 0x00);
1332     for (i = 0; i < 256*3; ++i)
1333 	palette[i] = inb(PALDATA) << 2;
1334     inb(CRTC + 6);		/* reset flip/flop */
1335     return 0;
1336 }
1337 
1338 static int
1339 vga_save_palette2(video_adapter_t *adp, int base, int count,
1340 		  u_char *r, u_char *g, u_char *b)
1341 {
1342     int i;
1343 
1344     prologue(adp, V_ADP_PALETTE, ENODEV);
1345 
1346     outb(PALRADR, base);
1347     for (i = 0; i < count; ++i) {
1348 	r[i] = inb(PALDATA) << 2;
1349 	g[i] = inb(PALDATA) << 2;
1350 	b[i] = inb(PALDATA) << 2;
1351     }
1352     inb(CRTC + 6);		/* reset flip/flop */
1353     return 0;
1354 }
1355 
1356 /*
1357  * load_palette():
1358  * Set DAC values.
1359  *
1360  * VGA
1361  */
1362 static int
1363 vga_load_palette(video_adapter_t *adp, const u_char *palette)
1364 {
1365     int i;
1366 
1367     prologue(adp, V_ADP_PALETTE, ENODEV);
1368 
1369     outb(PIXMASK, 0xff);		/* no pixelmask */
1370     outb(PALWADR, 0x00);
1371     for (i = 0; i < 256*3; ++i)
1372 	outb(PALDATA, palette[i] >> 2);
1373     inb(CRTC + 6);			/* reset flip/flop */
1374     outb(ATC, 0x20);			/* enable palette */
1375     return 0;
1376 }
1377 
1378 static int
1379 vga_load_palette2(video_adapter_t *adp, int base, int count,
1380 		  u_char *r, u_char *g, u_char *b)
1381 {
1382     int i;
1383 
1384     prologue(adp, V_ADP_PALETTE, ENODEV);
1385 
1386     outb(PIXMASK, 0xff);		/* no pixelmask */
1387     outb(PALWADR, base);
1388     for (i = 0; i < count; ++i) {
1389 	outb(PALDATA, r[i] >> 2);
1390 	outb(PALDATA, g[i] >> 2);
1391 	outb(PALDATA, b[i] >> 2);
1392     }
1393     inb(CRTC + 6);			/* reset flip/flop */
1394     outb(ATC, 0x20);			/* enable palette */
1395     return 0;
1396 }
1397 
1398 /*
1399  * set_border():
1400  * Change the border color.
1401  */
1402 static int
1403 vga_set_border(video_adapter_t *adp, int color)
1404 {
1405     prologue(adp, V_ADP_BORDER, ENODEV);
1406 
1407     inb(CRTC + 6);	/* reset flip-flop */
1408     outb(ATC, 0x31); outb(ATC, color & 0xff);
1409 
1410     return 0;
1411 }
1412 
1413 /*
1414  * save_state():
1415  * Read video register values.
1416  * NOTE: this function only reads the standard VGA registers.
1417  * any extra/extended registers of SVGA adapters are not saved.
1418  */
1419 static int
1420 vga_save_state(video_adapter_t *adp, void *p, size_t size)
1421 {
1422     video_info_t info;
1423     u_char *buf;
1424     int i, j;
1425 
1426     if (size == 0) {
1427 	/* return the required buffer size */
1428 	prologue(adp, V_ADP_STATESAVE, 0);
1429 	return sizeof(adp_state_t);
1430     } else {
1431 	prologue(adp, V_ADP_STATESAVE, ENODEV);
1432 	if (size < sizeof(adp_state_t))
1433 	    return EINVAL;
1434     }
1435     ((adp_state_t *)p)->sig = V_STATE_SIG;
1436     buf = ((adp_state_t *)p)->regs;
1437     bzero(buf, V_MODE_PARAM_SIZE);
1438 
1439     crit_enter();
1440 
1441     outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1442     for (i = 0, j = 5; i < 4; i++) {
1443 	outb(TSIDX, i + 1);
1444 	buf[j++]  =  inb(TSREG);
1445     }
1446     buf[9]  =  inb(MISC + 10);			/* dot-clock */
1447     outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
1448 
1449     for (i = 0, j = 10; i < 25; i++) {		/* crtc */
1450 	outb(CRTC, i);
1451 	buf[j++]  =  inb(CRTC + 1);
1452     }
1453     for (i = 0, j = 35; i < 20; i++) {		/* attribute ctrl */
1454         inb(CRTC + 6);				/* reset flip-flop */
1455 	outb(ATC, i);
1456 	buf[j++]  =  inb(ATC + 1);
1457     }
1458     for (i = 0, j = 55; i < 9; i++) {		/* graph data ctrl */
1459 	outb(GDCIDX, i);
1460 	buf[j++]  =  inb(GDCREG);
1461     }
1462     inb(CRTC + 6);				/* reset flip-flop */
1463     outb(ATC, 0x20);				/* enable palette */
1464 
1465     crit_exit();
1466 
1467 #if 1
1468     if (vga_get_info(adp, adp->va_mode, &info) == 0) {
1469 	if (info.vi_flags & V_INFO_GRAPHICS) {
1470 	    buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
1471 	    buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
1472 	} else {
1473 	    buf[0] = info.vi_width;		/* COLS */
1474 	    buf[1] = info.vi_height - 1;	/* ROWS */
1475 	}
1476 	buf[2] = info.vi_cheight;		/* POINTS */
1477     } else {
1478 	/* XXX: shouldn't be happening... */
1479 	kprintf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
1480 	       adp->va_unit, adp->va_name);
1481     }
1482 #else
1483     buf[0] = readb(BIOS_PADDRTOVADDR(0x44a));	/* COLS */
1484     buf[1] = readb(BIOS_PADDRTOVADDR(0x484));	/* ROWS */
1485     buf[2] = readb(BIOS_PADDRTOVADDR(0x485));	/* POINTS */
1486     buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
1487     buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
1488 #endif
1489 
1490     return 0;
1491 }
1492 
1493 /*
1494  * load_state():
1495  * Set video registers at once.
1496  * NOTE: this function only updates the standard VGA registers.
1497  * any extra/extended registers of SVGA adapters are not changed.
1498  */
1499 static int
1500 vga_load_state(video_adapter_t *adp, void *p)
1501 {
1502     u_char *buf;
1503     int i;
1504 
1505     prologue(adp, V_ADP_STATELOAD, ENODEV);
1506     if (((adp_state_t *)p)->sig != V_STATE_SIG)
1507 	return EINVAL;
1508 
1509     buf = ((adp_state_t *)p)->regs;
1510 
1511 #if VGA_DEBUG > 1
1512     hexdump(buf, V_MODE_PARAM_SIZE, NULL, HD_OMIT_CHARS | HD_OMIT_COUNT);
1513 #endif
1514 
1515     crit_enter();
1516 
1517     outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
1518     for (i = 0; i < 4; ++i) {			/* program sequencer */
1519 	outb(TSIDX, i + 1);
1520 	outb(TSREG, buf[i + 5]);
1521     }
1522     outb(MISC, buf[9]);				/* set dot-clock */
1523     outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
1524     outb(CRTC, 0x11);
1525     outb(CRTC + 1, inb(CRTC + 1) & 0x7F);
1526     for (i = 0; i < 25; ++i) {			/* program crtc */
1527 	outb(CRTC, i);
1528 	outb(CRTC + 1, buf[i + 10]);
1529     }
1530     inb(CRTC + 6);				/* reset flip-flop */
1531     for (i = 0; i < 20; ++i) {			/* program attribute ctrl */
1532 	outb(ATC, i);
1533 	outb(ATC, buf[i + 35]);
1534     }
1535     for (i = 0; i < 9; ++i) {			/* program graph data ctrl */
1536 	outb(GDCIDX, i);
1537 	outb(GDCREG, buf[i + 55]);
1538     }
1539     inb(CRTC + 6);				/* reset flip-flop */
1540     outb(ATC, 0x20);				/* enable palette */
1541 
1542 #if 0 /* XXX a temporary workaround for kernel panic */
1543 #ifndef VGA_NO_MODE_CHANGE
1544     if (adp->va_unit == V_ADP_PRIMARY) {
1545 	writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]);	/* COLS */
1546 	writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
1547 	writeb(BIOS_PADDRTOVADDR(0x485), buf[2]);	/* POINTS */
1548 #if 0
1549 	writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
1550 	writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
1551 #endif
1552     }
1553 #endif /* !VGA_NO_MODE_CHANGE */
1554 #endif /* XXX */
1555 
1556     crit_exit();
1557     return 0;
1558 }
1559 
1560 /*
1561  * set_origin():
1562  * Change the origin (window mapping) of the banked frame buffer.
1563  */
1564 static int
1565 vga_set_origin(video_adapter_t *adp, off_t offset)
1566 {
1567     /*
1568      * The standard video modes do not require window mapping;
1569      * always return error.
1570      */
1571     return ENODEV;
1572 }
1573 
1574 /*
1575  * read_hw_cursor():
1576  * Read the position of the hardware text cursor.
1577  */
1578 static int
1579 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1580 {
1581     u_int16_t off;
1582 
1583     if (!vga_init_done)
1584 	return ENXIO;
1585 
1586     if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1587 	return ENODEV;
1588 
1589     crit_enter();
1590     outb(CRTC, 14);
1591     off = inb(CRTC + 1);
1592     outb(CRTC, 15);
1593     off = (off << 8) | inb(CRTC + 1);
1594     crit_exit();
1595 
1596     *row = off / adp->va_info.vi_width;
1597     *col = off % adp->va_info.vi_width;
1598 
1599     return 0;
1600 }
1601 
1602 /*
1603  * set_hw_cursor():
1604  * Move the hardware text cursor.  If col and row are both -1,
1605  * the cursor won't be shown.
1606  */
1607 static int
1608 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
1609 {
1610     u_int16_t off;
1611 
1612     if (!vga_init_done)
1613 	return ENXIO;
1614 
1615     if ((col == -1) && (row == -1)) {
1616 	off = -1;
1617     } else {
1618 	if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1619 	    return ENODEV;
1620 	off = row*adp->va_info.vi_width + col;
1621     }
1622 
1623     crit_enter();
1624     outb(CRTC, 14);
1625     outb(CRTC + 1, off >> 8);
1626     outb(CRTC, 15);
1627     outb(CRTC + 1, off & 0x00ff);
1628     crit_exit();
1629 
1630     return 0;
1631 }
1632 
1633 /*
1634  * set_hw_cursor_shape():
1635  * Change the shape of the hardware text cursor. If the height is
1636  * zero or negative, the cursor won't be shown.
1637  */
1638 static int
1639 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1640 			int celsize, int blink)
1641 {
1642     if (!vga_init_done)
1643 	return ENXIO;
1644 
1645     crit_enter();
1646     if (height <= 0) {
1647 	/* make the cursor invisible */
1648 	outb(CRTC, 10);
1649 	outb(CRTC + 1, 32);
1650 	outb(CRTC, 11);
1651 	outb(CRTC + 1, 0);
1652     } else {
1653 	outb(CRTC, 10);
1654 	outb(CRTC + 1, celsize - base - height);
1655 	outb(CRTC, 11);
1656 	outb(CRTC + 1, celsize - base - 1);
1657     }
1658     crit_exit();
1659 
1660     return 0;
1661 }
1662 
1663 /*
1664  * blank_display()
1665  * Put the display in power save/power off mode.
1666  */
1667 static int
1668 vga_blank_display(video_adapter_t *adp, int mode)
1669 {
1670     u_char val;
1671 
1672     crit_enter();
1673     switch (mode) {
1674     case V_DISPLAY_SUSPEND:
1675     case V_DISPLAY_STAND_BY:
1676 	outb(TSIDX, 0x01);
1677 	val = inb(TSREG);
1678 	outb(TSIDX, 0x01);
1679 	outb(TSREG, val | 0x20);
1680 	outb(CRTC, 0x17);
1681 	val = inb(CRTC + 1);
1682 	outb(CRTC + 1, val & ~0x80);
1683 	break;
1684     case V_DISPLAY_BLANK:
1685 	outb(TSIDX, 0x01);
1686 	val = inb(TSREG);
1687 	outb(TSIDX, 0x01);
1688 	outb(TSREG, val | 0x20);
1689 	break;
1690     case V_DISPLAY_ON:
1691 	outb(TSIDX, 0x01);
1692 	val = inb(TSREG);
1693 	outb(TSIDX, 0x01);
1694 	outb(TSREG, val & 0xDF);
1695 	outb(CRTC, 0x17);
1696 	val = inb(CRTC + 1);
1697 	outb(CRTC + 1, val | 0x80);
1698 	break;
1699     }
1700     crit_exit();
1701 
1702     return 0;
1703 }
1704 
1705 /*
1706  * mmap():
1707  * Mmap frame buffer.
1708  */
1709 static int
1710 vga_mmap_buf(video_adapter_t *adp, vm_offset_t offset, int prot)
1711 {
1712     if (adp->va_info.vi_flags & V_INFO_LINEAR)
1713 	return -1;
1714 
1715 #if VGA_DEBUG > 0
1716     kprintf("vga_mmap_buf(): window:0x%x, offset:%p\n",
1717 	   adp->va_info.vi_window, (void *)offset);
1718 #endif
1719 
1720     /* XXX: is this correct? */
1721     if (offset > adp->va_window_size - PAGE_SIZE)
1722 	return -1;
1723 
1724 #if defined(__x86_64__)
1725     return x86_64_btop(adp->va_info.vi_window + offset);
1726 #else
1727 #error "vga_mmap_buf needs to return something"
1728 #endif
1729 }
1730 
1731 #ifndef VGA_NO_MODE_CHANGE
1732 
1733 static void
1734 planar_fill(video_adapter_t *adp, int val)
1735 {
1736     int length;
1737     int at;			/* position in the frame buffer */
1738     int l;
1739 
1740     lwkt_gettoken(&vga_token);
1741     outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
1742     outw(GDCIDX, 0x0003);		/* data rotate/function select */
1743     outw(GDCIDX, 0x0f01);		/* set/reset enable */
1744     outw(GDCIDX, 0xff08);		/* bit mask */
1745     outw(GDCIDX, (val << 8) | 0x00);	/* set/reset */
1746     at = 0;
1747     length = adp->va_line_width*adp->va_info.vi_height;
1748     while (length > 0) {
1749 	l = imin(length, adp->va_window_size);
1750 	(*vidsw[adp->va_index]->set_win_org)(adp, at);
1751 	bzero_io(adp->va_window, l);
1752 	length -= l;
1753 	at += l;
1754     }
1755     outw(GDCIDX, 0x0000);		/* set/reset */
1756     outw(GDCIDX, 0x0001);		/* set/reset enable */
1757     lwkt_reltoken(&vga_token);
1758 }
1759 
1760 static void
1761 packed_fill(video_adapter_t *adp, int val)
1762 {
1763     int length;
1764     int at;			/* position in the frame buffer */
1765     int l;
1766 
1767     lwkt_gettoken(&vga_token);
1768     at = 0;
1769     length = adp->va_line_width*adp->va_info.vi_height;
1770     while (length > 0) {
1771 	l = imin(length, adp->va_window_size);
1772 	(*vidsw[adp->va_index]->set_win_org)(adp, at);
1773 	fill_io(val, adp->va_window, l);
1774 	length -= l;
1775 	at += l;
1776     }
1777     lwkt_reltoken(&vga_token);
1778 }
1779 
1780 static void
1781 direct_fill(video_adapter_t *adp, int val)
1782 {
1783     int length;
1784     int at;			/* position in the frame buffer */
1785     int l;
1786 
1787     lwkt_gettoken(&vga_token);
1788     at = 0;
1789     length = adp->va_line_width*adp->va_info.vi_height;
1790     while (length > 0) {
1791 	l = imin(length, adp->va_window_size);
1792 	(*vidsw[adp->va_index]->set_win_org)(adp, at);
1793 	switch (adp->va_info.vi_pixel_size) {
1794 	case sizeof(u_int16_t):
1795 	    fillw_io(val, adp->va_window, l/sizeof(u_int16_t));
1796 	    break;
1797 	case 3:
1798 	    /* FIXME */
1799 	    break;
1800 	case sizeof(u_int32_t):
1801 	    filll_io(val, adp->va_window, l/sizeof(u_int32_t));
1802 	    break;
1803 	}
1804 	length -= l;
1805 	at += l;
1806     }
1807     lwkt_reltoken(&vga_token);
1808 }
1809 
1810 static int
1811 vga_clear(video_adapter_t *adp)
1812 {
1813     switch (adp->va_info.vi_mem_model) {
1814     case V_INFO_MM_TEXT:
1815 	/* do nothing? XXX */
1816 	break;
1817     case V_INFO_MM_PLANAR:
1818 	planar_fill(adp, 0);
1819 	break;
1820     case V_INFO_MM_PACKED:
1821 	packed_fill(adp, 0);
1822 	break;
1823     case V_INFO_MM_DIRECT:
1824 	direct_fill(adp, 0);
1825 	break;
1826     }
1827     return 0;
1828 }
1829 
1830 static int
1831 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1832 {
1833     return ENODEV;
1834 }
1835 
1836 static int
1837 vga_bitblt(video_adapter_t *adp,...)
1838 {
1839     /* FIXME */
1840     return ENODEV;
1841 }
1842 
1843 #endif /* !VGA_NO_MODE_CHANGE */
1844 
1845 static int
1846 get_palette(video_adapter_t *adp, int base, int count,
1847 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1848 {
1849     u_char *r;
1850     u_char *g;
1851     u_char *b;
1852 
1853     if (count < 0 || base < 0 || count > 256 || base > 256 ||
1854 	base + count > 256)
1855 	return EINVAL;
1856 
1857     r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
1858     g = r + count;
1859     b = g + count;
1860     if (vga_save_palette2(adp, base, count, r, g, b)) {
1861 	kfree(r, M_DEVBUF);
1862 	return ENODEV;
1863     }
1864     copyout(r, red, count);
1865     copyout(g, green, count);
1866     copyout(b, blue, count);
1867     if (trans != NULL) {
1868 	bzero(r, count);
1869 	copyout(r, trans, count);
1870     }
1871     kfree(r, M_DEVBUF);
1872 
1873     return 0;
1874 }
1875 
1876 static int
1877 set_palette(video_adapter_t *adp, int base, int count,
1878 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1879 {
1880     u_char *r;
1881     u_char *g;
1882     u_char *b;
1883     int err;
1884 
1885     if (count < 0 || base < 0 || count > 256 || base > 256 ||
1886 	base + count > 256)
1887 	return EINVAL;
1888 
1889     r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
1890     g = r + count;
1891     b = g + count;
1892     err = copyin(red, r, count);
1893     if (!err)
1894 	err = copyin(green, g, count);
1895     if (!err)
1896 	err = copyin(blue, b, count);
1897     if (!err)
1898 	err = vga_load_palette2(adp, base, count, r, g, b);
1899     kfree(r, M_DEVBUF);
1900 
1901     return (err ? ENODEV : 0);
1902 }
1903 
1904 static int
1905 vga_dev_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1906 {
1907     switch (cmd) {
1908     case FBIO_GETWINORG:	/* get frame buffer window origin */
1909 	*(u_int *)arg = 0;
1910 	return 0;
1911 
1912     case FBIO_SETWINORG:	/* set frame buffer window origin */
1913 	return ENODEV;
1914 
1915     case FBIO_SETDISPSTART:	/* set display start address */
1916 	return (set_display_start(adp,
1917 				  ((video_display_start_t *)arg)->x,
1918 			  	  ((video_display_start_t *)arg)->y)
1919 		? ENODEV : 0);
1920 
1921     case FBIO_SETLINEWIDTH:	/* set scan line length in pixel */
1922 	return (set_line_length(adp, *(u_int *)arg) ? ENODEV : 0);
1923 
1924     case FBIO_GETPALETTE:	/* get color palette */
1925 	return get_palette(adp, ((video_color_palette_t *)arg)->index,
1926 			   ((video_color_palette_t *)arg)->count,
1927 			   ((video_color_palette_t *)arg)->red,
1928 			   ((video_color_palette_t *)arg)->green,
1929 			   ((video_color_palette_t *)arg)->blue,
1930 			   ((video_color_palette_t *)arg)->transparent);
1931 
1932     case FBIO_SETPALETTE:	/* set color palette */
1933 	return set_palette(adp, ((video_color_palette_t *)arg)->index,
1934 			   ((video_color_palette_t *)arg)->count,
1935 			   ((video_color_palette_t *)arg)->red,
1936 			   ((video_color_palette_t *)arg)->green,
1937 			   ((video_color_palette_t *)arg)->blue,
1938 			   ((video_color_palette_t *)arg)->transparent);
1939 
1940     case FBIOGTYPE:		/* get frame buffer type info. */
1941 	((struct fbtype *)arg)->fb_type = fb_type(adp->va_type);
1942 	((struct fbtype *)arg)->fb_height = adp->va_info.vi_height;
1943 	((struct fbtype *)arg)->fb_width = adp->va_info.vi_width;
1944 	((struct fbtype *)arg)->fb_depth = adp->va_info.vi_depth;
1945 	if ((adp->va_info.vi_depth <= 1) || (adp->va_info.vi_depth > 8))
1946 	    ((struct fbtype *)arg)->fb_cmsize = 0;
1947 	else
1948 	    ((struct fbtype *)arg)->fb_cmsize = 1 << adp->va_info.vi_depth;
1949 	((struct fbtype *)arg)->fb_size = adp->va_buffer_size;
1950 	return 0;
1951 
1952     case FBIOGETCMAP:		/* get color palette */
1953 	return get_palette(adp, ((struct fbcmap *)arg)->index,
1954 			   ((struct fbcmap *)arg)->count,
1955 			   ((struct fbcmap *)arg)->red,
1956 			   ((struct fbcmap *)arg)->green,
1957 			   ((struct fbcmap *)arg)->blue, NULL);
1958 
1959     case FBIOPUTCMAP:		/* set color palette */
1960 	return set_palette(adp, ((struct fbcmap *)arg)->index,
1961 			   ((struct fbcmap *)arg)->count,
1962 			   ((struct fbcmap *)arg)->red,
1963 			   ((struct fbcmap *)arg)->green,
1964 			   ((struct fbcmap *)arg)->blue, NULL);
1965 
1966     default:
1967 	return fb_commonioctl(adp, cmd, arg);
1968     }
1969 }
1970 
1971 /*
1972  * diag():
1973  * Print some information about the video adapter and video modes,
1974  * with requested level of details.
1975  */
1976 static int
1977 vga_diag(video_adapter_t *adp, int level)
1978 {
1979     u_char *mp;
1980 #if FB_DEBUG > 1
1981     video_info_t info;
1982     int i;
1983 #endif
1984 
1985     if (!vga_init_done)
1986 	return ENXIO;
1987 
1988 #if FB_DEBUG > 1
1989 #ifndef VGA_NO_MODE_CHANGE
1990     kprintf("vga: DCC code:0x%02x\n",
1991 	readb(BIOS_PADDRTOVADDR(0x488)));
1992     kprintf("vga: CRTC:0x%x, video option:0x%02x, ",
1993 	readw(BIOS_PADDRTOVADDR(0x463)),
1994 	readb(BIOS_PADDRTOVADDR(0x487)));
1995     kprintf("rows:%d, cols:%d, font height:%d\n",
1996 	readb(BIOS_PADDRTOVADDR(0x44a)),
1997 	readb(BIOS_PADDRTOVADDR(0x484)) + 1,
1998 	readb(BIOS_PADDRTOVADDR(0x485)));
1999     kprintf("vga: param table:%p\n", video_mode_ptr);
2000     kprintf("vga: rows_offset:%d\n", rows_offset);
2001 #endif
2002 #endif /* FB_DEBUG > 1 */
2003 
2004     fb_dump_adp_info(VGA_DRIVER_NAME, adp, level);
2005 
2006 #if FB_DEBUG > 1
2007     if (adp->va_flags & V_ADP_MODECHANGE) {
2008 	for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2009 	    if (bios_vmode[i].vi_mode == NA)
2010 		continue;
2011 	    if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2012 		continue;
2013 	    fb_dump_mode_info(VGA_DRIVER_NAME, adp, &bios_vmode[i], level);
2014 	}
2015     } else {
2016 	vga_get_info(adp, adp->va_initial_mode, &info);	/* shouldn't fail */
2017 	fb_dump_mode_info(VGA_DRIVER_NAME, adp, &info, level);
2018     }
2019 #endif /* FB_DEBUG > 1 */
2020 
2021 #ifndef VGA_NO_MODE_CHANGE
2022     if (video_mode_ptr == NULL)
2023 	kprintf("vga%d: %s: WARNING: video mode switching is not "
2024 	       "fully supported on this adapter\n",
2025 	       adp->va_unit, adp->va_name);
2026 #endif
2027     if (level <= 0)
2028 	return 0;
2029 
2030     kprintf("VGA parameters upon power-up\n");
2031     hexdump(adpstate.regs, sizeof(adpstate.regs), NULL,
2032 	HD_OMIT_CHARS | HD_OMIT_COUNT);
2033 
2034     mp = get_mode_param(adp->va_initial_mode);
2035     if (mp == NULL)	/* this shouldn't be happening */
2036 	return 0;
2037     kprintf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2038     hexdump(adpstate2.regs, sizeof(adpstate2.regs), NULL,
2039 	HD_OMIT_CHARS | HD_OMIT_COUNT);
2040     kprintf("VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2041     hexdump(mp, V_MODE_PARAM_SIZE, NULL, HD_OMIT_CHARS | HD_OMIT_COUNT);
2042 
2043     return 0;
2044 }
2045