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