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