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