2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * Copyright (c) 1992-1998 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/dev/fb/vga.c,v 1.9.2.1 2001/08/11 02:58:44 yokota Exp $
30 * $DragonFly: src/sys/dev/video/fb/vga.c,v 1.23 2007/09/20 17:54:27 swildner Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
43 #include <sys/thread2.h>
45 #include <bus/isa/isareg.h>
47 #include <machine/clock.h>
48 #include <machine/md_var.h>
49 #include <machine/pc/bios.h>
52 #include <vm/vm_param.h>
63 vga_probe_unit(int unit, video_adapter_t *buf, int flags)
69 sw = vid_get_switch(VGA_DRIVER_NAME);
72 error = (*sw->probe)(unit, &adp, NULL, flags);
75 bcopy(adp, buf, sizeof(*buf));
80 vga_attach_unit(int unit, vga_softc_t *sc, int flags)
85 sw = vid_get_switch(VGA_DRIVER_NAME);
89 error = (*sw->probe)(unit, &sc->adp, NULL, flags);
92 return (*sw->init)(unit, sc->adp, flags);
95 /* cdev driver functions */
97 #ifdef FB_INSTALL_CDEV
102 vga_open(cdev_t dev, vga_softc_t *sc, int flag, int mode, struct ucred *cred)
106 if (mode & (O_CREAT | O_APPEND | O_TRUNC))
109 return genfbopen(&sc->gensc, sc->adp, flag, mode, cred);
113 vga_close(cdev_t dev, vga_softc_t *sc, int flag, int mode)
115 return genfbclose(&sc->gensc, sc->adp, flag, mode);
119 vga_read(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
121 return genfbread(&sc->gensc, sc->adp, uio, flag);
125 vga_write(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
127 return genfbread(&sc->gensc, sc->adp, uio, flag);
131 vga_ioctl(cdev_t dev, vga_softc_t *sc, u_long cmd, caddr_t arg, int flag,
134 return genfbioctl(&sc->gensc, sc->adp, cmd, arg, flag, cred);
138 vga_mmap(cdev_t dev, vga_softc_t *sc, vm_offset_t offset, int prot)
140 return genfbmmap(&sc->gensc, sc->adp, offset, prot);
143 #endif /* FB_INSTALL_CDEV */
147 #define probe_done(adp) ((adp)->va_flags & V_ADP_PROBED)
148 #define init_done(adp) ((adp)->va_flags & V_ADP_INITIALIZED)
149 #define config_done(adp) ((adp)->va_flags & V_ADP_REGISTERED)
151 /* this should really be in `rtc.h' */
152 #define RTC_EQUIPMENT 0x14
155 #define V_MODE_MAP_SIZE (M_VGA_CG320 + 1)
156 #define V_MODE_PARAM_SIZE 64
158 /* video adapter state buffer */
161 #define V_STATE_SIG 0x736f6962
162 u_char regs[V_MODE_PARAM_SIZE];
164 typedef struct adp_state adp_state_t;
166 /* video adapter information */
170 #define DCC_EGAMONO 3
175 * NOTE: `va_window' should have a virtual address, but is initialized
176 * with a physical address in the following table, as verify_adapter()
177 * will perform address conversion at run-time.
179 static video_adapter_t adapter_init_value[] = {
181 { 0, KD_MONO, "mda", 0, 0, 0, IO_MDA, IO_MDASIZE, MONO_CRTC,
182 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
185 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
186 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
189 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
190 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
193 { 0, KD_EGA, "ega", 0, 0, 0, IO_MDA, 48, MONO_CRTC,
194 EGA_BUF_BASE, EGA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
197 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
198 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
201 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
202 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
206 static video_adapter_t biosadapter[2];
207 static int biosadapters = 0;
209 /* video driver declarations */
210 static int vga_configure(int flags);
211 int (*vga_sub_configure)(int flags);
213 static int vga_nop(void);
215 static int vga_error(void);
216 static vi_probe_t vga_probe;
217 static vi_init_t vga_init;
218 static vi_get_info_t vga_get_info;
219 static vi_query_mode_t vga_query_mode;
220 static vi_set_mode_t vga_set_mode;
221 static vi_save_font_t vga_save_font;
222 static vi_load_font_t vga_load_font;
223 static vi_show_font_t vga_show_font;
224 static vi_save_palette_t vga_save_palette;
225 static vi_load_palette_t vga_load_palette;
226 static vi_set_border_t vga_set_border;
227 static vi_save_state_t vga_save_state;
228 static vi_load_state_t vga_load_state;
229 static vi_set_win_org_t vga_set_origin;
230 static vi_read_hw_cursor_t vga_read_hw_cursor;
231 static vi_set_hw_cursor_t vga_set_hw_cursor;
232 static vi_set_hw_cursor_shape_t vga_set_hw_cursor_shape;
233 static vi_blank_display_t vga_blank_display;
234 static vi_mmap_t vga_mmap_buf;
235 static vi_ioctl_t vga_dev_ioctl;
236 #ifndef VGA_NO_MODE_CHANGE
237 static vi_clear_t vga_clear;
238 static vi_fill_rect_t vga_fill_rect;
239 static vi_bitblt_t vga_bitblt;
240 #else /* VGA_NO_MODE_CHANGE */
241 #define vga_clear (vi_clear_t *)vga_error
242 #define vga_fill_rect (vi_fill_rect_t *)vga_error
243 #define vga_bitblt (vi_bitblt_t *)vga_error
245 static vi_diag_t vga_diag;
247 static video_switch_t vgavidsw = {
264 vga_set_hw_cursor_shape,
276 VIDEO_DRIVER(mda, vgavidsw, NULL);
277 VIDEO_DRIVER(cga, vgavidsw, NULL);
278 VIDEO_DRIVER(ega, vgavidsw, NULL);
279 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
281 /* VGA BIOS standard video modes */
285 static video_info_t bios_vmode[] = {
287 { M_B40x25, V_INFO_COLOR, 40, 25, 8, 8, 2, 1,
288 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
289 { M_C40x25, V_INFO_COLOR, 40, 25, 8, 8, 4, 1,
290 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
291 { M_B80x25, V_INFO_COLOR, 80, 25, 8, 8, 2, 1,
292 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
293 { M_C80x25, V_INFO_COLOR, 80, 25, 8, 8, 4, 1,
294 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
296 { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
297 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
298 { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
299 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
300 { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
301 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
302 { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
303 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
305 { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
306 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
307 { M_VGA_M80x25, 0, 80, 25, 8, 16, 2, 1,
308 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
309 { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
310 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
312 { M_EGAMONO80x25, 0, 80, 25, 8, 14, 2, 1,
313 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
315 { M_ENH_B80x43, 0, 80, 43, 8, 8, 2, 1,
316 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
317 { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8, 8, 4, 1,
318 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
320 { M_VGA_M80x30, 0, 80, 30, 8, 16, 2, 1,
321 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
322 { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
323 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
324 { M_VGA_M80x50, 0, 80, 50, 8, 8, 2, 1,
325 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
326 { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8, 8, 4, 1,
327 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
328 { M_VGA_M80x60, 0, 80, 60, 8, 8, 2, 1,
329 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
330 { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8, 8, 4, 1,
331 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
333 #ifndef VGA_NO_MODE_CHANGE
336 { M_VGA_M90x25, 0, 90, 25, 8, 16, 2, 1,
337 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
338 { M_VGA_C90x25, V_INFO_COLOR, 90, 25, 8, 16, 4, 1,
339 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
340 { M_VGA_M90x30, 0, 90, 30, 8, 16, 2, 1,
341 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
342 { M_VGA_C90x30, V_INFO_COLOR, 90, 30, 8, 16, 4, 1,
343 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
344 { M_VGA_M90x43, 0, 90, 43, 8, 8, 2, 1,
345 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
346 { M_VGA_C90x43, V_INFO_COLOR, 90, 43, 8, 8, 4, 1,
347 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
348 { M_VGA_M90x50, 0, 90, 50, 8, 8, 2, 1,
349 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
350 { M_VGA_C90x50, V_INFO_COLOR, 90, 50, 8, 8, 4, 1,
351 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
352 { M_VGA_M90x60, 0, 90, 60, 8, 8, 2, 1,
353 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
354 { M_VGA_C90x60, V_INFO_COLOR, 90, 60, 8, 8, 4, 1,
355 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
356 #endif /* VGA_WIDTH90 */
359 { M_BG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
360 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
361 { M_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
362 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
363 { M_BG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 1, 1,
364 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
366 { M_CG320_D, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 4, 4,
367 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
369 { M_CG640_E, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 4, 4,
370 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
372 { M_EGAMONOAPA, V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
373 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 ,
375 { M_ENHMONOAPA2,V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
376 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
378 { M_CG640x350, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
379 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
381 { M_ENH_CG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
382 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
385 { M_BG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
386 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
388 { M_CG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
389 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
391 { M_VGA_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 8, 1,
392 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
393 V_INFO_MM_PACKED, 1 },
394 { M_VGA_MODEX, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8, 8, 8, 4,
395 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
397 #endif /* VGA_NO_MODE_CHANGE */
402 static int vga_init_done = FALSE;
403 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
404 static u_char *video_mode_ptr = NULL; /* EGA/VGA */
405 static u_char *video_mode_ptr2 = NULL; /* CGA/MDA */
407 static u_char *mode_map[V_MODE_MAP_SIZE];
408 static adp_state_t adpstate;
409 static adp_state_t adpstate2;
410 static int rows_offset = 1;
412 /* local macros and functions */
413 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
415 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
416 static void map_mode_table(u_char **, u_char *, int);
418 static void clear_mode_map(video_adapter_t *, u_char **, int, int);
419 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
420 static int map_mode_num(int);
422 static int map_bios_mode_num(int, int, int);
423 static u_char *get_mode_param(int);
425 static void fill_adapter_param(int, video_adapter_t *);
427 static int verify_adapter(video_adapter_t *);
428 static void update_adapter_info(video_adapter_t *, video_info_t *);
429 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
430 #define COMP_IDENTICAL 0
431 #define COMP_SIMILAR 1
432 #define COMP_DIFFERENT 2
433 static int comp_adpregs(u_char *, u_char *);
435 static int probe_adapters(void);
436 static int set_line_length(video_adapter_t *, int);
437 static int set_display_start(video_adapter_t *, int, int);
439 #ifndef VGA_NO_MODE_CHANGE
441 static void set_width90(adp_state_t *);
443 #endif /* !VGA_NO_MODE_CHANGE */
445 #ifndef VGA_NO_FONT_LOADING
446 #define PARAM_BUFSIZE 6
447 static void set_font_mode(video_adapter_t *, u_char *);
448 static void set_normal_mode(video_adapter_t *, u_char *);
451 #ifndef VGA_NO_MODE_CHANGE
452 static void filll_io(int, vm_offset_t, size_t);
453 static void planar_fill(video_adapter_t *, int);
454 static void packed_fill(video_adapter_t *, int);
455 static void direct_fill(video_adapter_t *, int);
457 static void planar_fill_rect(video_adapter_t *, int, int, int, int, int);
458 static void packed_fill_rect(video_adapter_t *, int, int, int, int, int);
459 static void direct_fill_rect16(video_adapter_t *, int, int, int, int, int);
460 static void direct_fill_rect24(video_adapter_t *, int, int, int, int, int);
461 static void direct_fill_rect32(video_adapter_t *, int, int, int, int, int);
463 #endif /* !VGA_NO_MODE_CHANGE */
465 static void dump_buffer(u_char *, size_t);
467 #define ISMAPPED(pa, width) \
468 (((pa) <= (u_long)0x1000 - (width)) \
469 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
471 #define prologue(adp, flag, err) \
472 if (!vga_init_done || !((adp)->va_flags & (flag))) \
475 /* a backdoor for the console driver */
477 vga_configure(int flags)
482 for (i = 0; i < biosadapters; ++i) {
483 if (!probe_done(&biosadapter[i]))
485 biosadapter[i].va_flags |= V_ADP_INITIALIZED;
486 if (!config_done(&biosadapter[i])) {
487 if (vid_register(&biosadapter[i]) < 0)
489 biosadapter[i].va_flags |= V_ADP_REGISTERED;
492 if (vga_sub_configure != NULL)
493 (*vga_sub_configure)(flags);
498 /* local subroutines */
500 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
501 /* construct the mode parameter map */
503 map_mode_table(u_char *map[], u_char *table, int max)
507 for(i = 0; i < max; ++i)
508 map[i] = table + i*V_MODE_PARAM_SIZE;
509 for(; i < V_MODE_MAP_SIZE; ++i)
512 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
515 clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
521 * NOTE: we don't touch `bios_vmode[]' because it is shared
524 for(i = 0; i < max; ++i) {
525 if (vga_get_info(adp, i, &info))
527 if ((info.vi_flags & V_INFO_COLOR) != color)
532 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
533 /* map the non-standard video mode to a known mode number */
535 map_mode_num(int mode)
541 { M_ENH_B80x43, M_ENH_B80x25 },
542 { M_ENH_C80x43, M_ENH_C80x25 },
543 { M_VGA_M80x30, M_VGA_M80x25 },
544 { M_VGA_C80x30, M_VGA_C80x25 },
545 { M_VGA_M80x50, M_VGA_M80x25 },
546 { M_VGA_C80x50, M_VGA_C80x25 },
547 { M_VGA_M80x60, M_VGA_M80x25 },
548 { M_VGA_C80x60, M_VGA_C80x25 },
550 { M_VGA_M90x25, M_VGA_M80x25 },
551 { M_VGA_C90x25, M_VGA_C80x25 },
552 { M_VGA_M90x30, M_VGA_M80x25 },
553 { M_VGA_C90x30, M_VGA_C80x25 },
554 { M_VGA_M90x43, M_ENH_B80x25 },
555 { M_VGA_C90x43, M_ENH_C80x25 },
556 { M_VGA_M90x50, M_VGA_M80x25 },
557 { M_VGA_C90x50, M_VGA_C80x25 },
558 { M_VGA_M90x60, M_VGA_M80x25 },
559 { M_VGA_C90x60, M_VGA_C80x25 },
561 { M_VGA_MODEX, M_VGA_CG320 },
565 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
566 if (mode_map[i].from == mode)
567 return mode_map[i].to;
571 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
573 /* turn the BIOS video number into our video mode number */
575 map_bios_mode_num(int type, int color, int bios_mode)
577 static int cga_modes[7] = {
578 M_B40x25, M_C40x25, /* 0, 1 */
579 M_B80x25, M_C80x25, /* 2, 3 */
583 static int ega_modes[17] = {
584 M_ENH_B40x25, M_ENH_C40x25, /* 0, 1 */
585 M_ENH_B80x25, M_ENH_C80x25, /* 2, 3 */
588 M_EGAMONO80x25, /* 7 */
592 M_ENHMONOAPA2, /* XXX: video momery > 64K */
593 M_ENH_CG640, /* XXX: video momery > 64K */
595 static int vga_modes[20] = {
596 M_VGA_C40x25, M_VGA_C40x25, /* 0, 1 */
597 M_VGA_C80x25, M_VGA_C80x25, /* 2, 3 */
600 M_VGA_M80x25, /* 7 */
606 M_BG640x480, M_CG640x480,
613 if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
614 return vga_modes[bios_mode];
622 if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
623 return ega_modes[bios_mode];
627 return M_EGAMONO80x25;
631 if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
632 return cga_modes[bios_mode];
639 return M_EGAMONO80x25; /* XXX: this name is confusing */
647 /* look up a parameter table entry */
649 get_mode_param(int mode)
651 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
652 if (mode >= V_MODE_MAP_SIZE)
653 mode = map_mode_num(mode);
655 if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
656 return mode_map[mode];
663 fill_adapter_param(int code, video_adapter_t *adp)
669 { DCC_MONO, DCC_EGA40 /* CGA monitor */ },
670 { DCC_MONO, DCC_EGA80 /* CGA monitor */ },
671 { DCC_MONO, DCC_EGA80 },
672 { DCC_MONO, DCC_EGA80 },
673 { DCC_CGA40, DCC_EGAMONO },
674 { DCC_CGA80, DCC_EGAMONO },
675 { DCC_EGA40 /* CGA monitor */, DCC_MONO},
676 { DCC_EGA80 /* CGA monitor */, DCC_MONO},
677 { DCC_EGA80, DCC_MONO },
678 { DCC_EGA80, DCC_MONO },
679 { DCC_EGAMONO, DCC_CGA40 },
680 { DCC_EGAMONO, DCC_CGA80 },
683 if ((code < 0) || (code >= sizeof(dcc)/sizeof(dcc[0]))) {
684 adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
685 adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
687 adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
688 adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
691 #endif /* VGA_NO_BIOS */
694 verify_adapter(video_adapter_t *adp)
698 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
702 buf = BIOS_PADDRTOVADDR(adp->va_window);
705 if (readw(buf) != 0xA55A)
709 switch (adp->va_type) {
712 outb(adp->va_crtc_addr, 7);
713 if (inb(adp->va_crtc_addr) == 7) {
714 adp->va_type = KD_VGA;
715 adp->va_name = "vga";
716 adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
718 adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
719 /* the color adapter may be in the 40x25 mode... XXX */
721 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
722 /* get the BIOS video mode pointer */
723 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
724 p = BIOS_SADDRTOLADDR(p);
725 if (ISMAPPED(p, sizeof(u_int32_t))) {
726 p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
727 p = BIOS_SADDRTOLADDR(p);
728 if (ISMAPPED(p, V_MODE_PARAM_SIZE))
729 video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
735 adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
736 /* may be in the 40x25 mode... XXX */
737 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
738 /* get the BIOS video mode pointer */
739 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
740 p = BIOS_SADDRTOLADDR(p);
741 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
746 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
747 /* get the BIOS video mode pointer */
748 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
749 p = BIOS_SADDRTOLADDR(p);
750 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
759 update_adapter_info(video_adapter_t *adp, video_info_t *info)
761 adp->va_flags &= ~V_ADP_COLOR;
763 (info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
765 (adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
766 adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
767 adp->va_window_size = info->vi_window_size;
768 adp->va_window_gran = info->vi_window_gran;
769 adp->va_window_orig = 0;
771 adp->va_buffer = info->vi_buffer;
772 adp->va_buffer_size = info->vi_buffer_size;
773 if (info->vi_mem_model == V_INFO_MM_VGAX) {
774 adp->va_line_width = info->vi_width/2;
775 } else if (info->vi_flags & V_INFO_GRAPHICS) {
776 switch (info->vi_depth/info->vi_planes) {
778 adp->va_line_width = info->vi_width/8;
781 adp->va_line_width = info->vi_width/4;
784 adp->va_line_width = info->vi_width/2;
787 default: /* shouldn't happen */
788 adp->va_line_width = info->vi_width;
792 adp->va_line_width = info->vi_width;
794 adp->va_disp_start.x = 0;
795 adp->va_disp_start.y = 0;
796 bcopy(info, &adp->va_info, sizeof(adp->va_info));
799 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
800 /* compare two parameter table entries */
802 comp_adpregs(u_char *buf1, u_char *buf2)
806 } params[V_MODE_PARAM_SIZE] = {
807 {0xff}, {0x00}, {0xff}, /* COLS}, ROWS}, POINTS */
808 {0x00}, {0x00}, /* page length */
809 {0xfe}, {0xff}, {0xff}, {0xff}, /* sequencer registers */
810 {0xf3}, /* misc register */
811 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff}, /* CRTC */
812 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
813 {0x00}, {0x00}, {0x00}, {0x00}, {0x00},
814 {0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
815 {0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
816 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* attribute controller regs */
817 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
818 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
819 {0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
820 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* GDC register */
821 {0xff}, {0xff}, {0xff}, {0xff},
823 int identical = TRUE;
826 if ((buf1 == NULL) || (buf2 == NULL))
827 return COMP_DIFFERENT;
829 for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
830 if (params[i].mask == 0) /* don't care */
832 if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
833 return COMP_DIFFERENT;
834 if (buf1[i] != buf2[i])
837 return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
839 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
841 /* probe video adapters and return the number of detected adapters */
845 video_adapter_t *adp;
847 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
852 /* do this test only once */
855 vga_init_done = TRUE;
858 * Locate display adapters.
859 * The AT architecture supports upto two adapters. `syscons' allows
860 * the following combinations of adapters:
862 * 2) MDA + EGA/VGA color
863 * 3) CGA + EGA/VGA mono
864 * Note that `syscons' doesn't bother with MCGA as it is only
865 * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
866 * thus, they are not running FreeBSD!
867 * When there are two adapaters in the system, one becomes `primary'
868 * and the other `secondary'. The EGA adapter has a set of DIP
869 * switches on board for this information and the EGA BIOS copies
870 * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88).
871 * The VGA BIOS has more sophisticated mechanism and has this
872 * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
873 * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
877 * Check rtc and BIOS data area.
878 * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
879 * copy of RTC_EQUIPMENT. Bits 4 and 5 of ETC_EQUIPMENT are
880 * zeros for EGA and VGA. However, the EGA/VGA BIOS sets
881 * these bits in BIOSDATA_EQUIPMENT according to the monitor
885 if (*(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8)) {
886 /* EGA/VGA BIOS is present */
887 fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
890 switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) { /* bit 4 and 5 */
892 /* EGA/VGA: shouldn't be happening */
893 fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
898 /* FIXME: switch to the 80x25 mode? XXX */
899 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
900 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
904 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
905 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
909 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
910 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
915 /* assume EGA/VGA? XXX */
916 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_EGA80];
917 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
918 #endif /* VGA_NO_BIOS */
921 if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
923 biosadapter[V_ADP_SECONDARY].va_flags |= V_ADP_PROBED;
924 biosadapter[V_ADP_SECONDARY].va_mode =
925 biosadapter[V_ADP_SECONDARY].va_initial_mode =
926 map_bios_mode_num(biosadapter[V_ADP_SECONDARY].va_type,
927 biosadapter[V_ADP_SECONDARY].va_flags
929 biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
931 biosadapter[V_ADP_SECONDARY].va_type = -1;
933 if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
935 biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
937 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
938 readb(BIOS_PADDRTOVADDR(0x449));
940 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3; /* XXX */
942 biosadapter[V_ADP_PRIMARY].va_mode =
943 biosadapter[V_ADP_PRIMARY].va_initial_mode =
944 map_bios_mode_num(biosadapter[V_ADP_PRIMARY].va_type,
945 biosadapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
946 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode);
948 biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
949 biosadapter[V_ADP_SECONDARY].va_type = -1;
951 if (biosadapters == 0)
953 biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
954 biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
956 #if 0 /* we don't need these... */
957 fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
958 fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
963 * We cannot have two video adapter of the same type; there must be
964 * only one of color or mono adapter, or one each of them.
966 if (biosadapters > 1) {
967 if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
969 /* we have two mono or color adapters!! */
970 return (biosadapters = 0);
975 * Ensure a zero start address. This is mainly to recover after
976 * switching from pcvt using userconfig(). The registers are w/o
977 * for old hardware so it's too hard to relocate the active screen
979 * This must be done before vga_save_state() for VGA.
981 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 12);
982 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
983 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 13);
984 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
986 /* the video mode parameter table in EGA/VGA BIOS */
987 /* NOTE: there can be only one EGA/VGA, wheather color or mono,
988 * recognized by the video BIOS.
990 if ((biosadapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
991 (biosadapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
992 adp = &biosadapter[V_ADP_PRIMARY];
993 } else if ((biosadapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
994 (biosadapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
995 adp = &biosadapter[V_ADP_SECONDARY];
999 bzero(mode_map, sizeof(mode_map));
1001 if (adp->va_type == KD_VGA) {
1002 vga_save_state(adp, &adpstate, sizeof(adpstate));
1003 for(i = 0; i < 16; i++)
1004 adp->va_palette_regs[i] = adpstate.regs[35 + i];
1005 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1006 mode_map[adp->va_initial_mode] = adpstate.regs;
1008 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1009 if (video_mode_ptr == NULL) {
1010 mode_map[adp->va_initial_mode] = adpstate.regs;
1013 /* discard the table if we are not familiar with it... */
1014 map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1015 mp = get_mode_param(adp->va_initial_mode);
1017 bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1018 switch (comp_adpregs(adpstate.regs, mp)) {
1019 case COMP_IDENTICAL:
1021 * OK, this parameter table looks reasonably familiar
1025 * This is a kludge for Toshiba DynaBook SS433
1026 * whose BIOS video mode table entry has the actual #
1027 * of rows at the offset 1; BIOSes from other
1028 * manufacturers store the # of rows - 1 there. XXX
1030 rows_offset = adpstate.regs[1] + 1 - mp[1];
1035 * Not exactly the same, but similar enough to be
1036 * trusted. However, use the saved register values
1037 * for the initial mode and other modes which are
1038 * based on the initial mode.
1040 mode_map[adp->va_initial_mode] = adpstate.regs;
1041 rows_offset = adpstate.regs[1] + 1 - mp[1];
1042 adpstate.regs[1] -= rows_offset - 1;
1045 case COMP_DIFFERENT:
1048 * Don't use the paramter table in BIOS. It doesn't
1049 * look familiar to us. Video mode switching is allowed
1050 * only if the new mode is the same as or based on
1053 video_mode_ptr = NULL;
1054 bzero(mode_map, sizeof(mode_map));
1055 mode_map[adp->va_initial_mode] = adpstate.regs;
1060 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1062 #ifndef VGA_NO_MODE_CHANGE
1063 adp->va_flags |= V_ADP_MODECHANGE;
1065 #ifndef VGA_NO_FONT_LOADING
1066 adp->va_flags |= V_ADP_FONT;
1068 } else if (adp->va_type == KD_EGA) {
1069 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1071 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1072 if (video_mode_ptr == NULL) {
1075 map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
1076 /* XXX how can one validate the EGA table... */
1077 mp = get_mode_param(adp->va_initial_mode);
1079 adp->va_flags |= V_ADP_MODECHANGE;
1080 #ifndef VGA_NO_FONT_LOADING
1081 adp->va_flags |= V_ADP_FONT;
1086 * This is serious. We will not be able to switch video
1089 video_mode_ptr = NULL;
1090 bzero(mode_map, sizeof(mode_map));
1094 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1098 /* remove conflicting modes if we have more than one adapter */
1099 if (biosadapters > 0) {
1100 for (i = 0; i < biosadapters; ++i) {
1101 if (!(biosadapter[i].va_flags & V_ADP_MODECHANGE))
1103 clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1104 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1106 if ((biosadapter[i].va_type == KD_VGA)
1107 || (biosadapter[i].va_type == KD_EGA)) {
1108 biosadapter[i].va_io_base =
1109 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1111 biosadapter[i].va_io_size = 32;
1116 /* buffer address */
1117 vga_get_info(&biosadapter[V_ADP_PRIMARY],
1118 biosadapter[V_ADP_PRIMARY].va_initial_mode, &info);
1119 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1120 update_adapter_info(&biosadapter[V_ADP_PRIMARY], &info);
1122 if (biosadapters > 1) {
1123 vga_get_info(&biosadapter[V_ADP_SECONDARY],
1124 biosadapter[V_ADP_SECONDARY].va_initial_mode, &info);
1125 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1126 update_adapter_info(&biosadapter[V_ADP_SECONDARY], &info);
1130 * XXX: we should verify the following values for the primary adapter...
1131 * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
1132 * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
1133 * ? 0 : V_ADP_COLOR;
1134 * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
1135 * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
1136 * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
1137 * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
1140 return biosadapters;
1143 /* set the scan line length in pixel */
1145 set_line_length(video_adapter_t *adp, int pixel)
1148 int ppw; /* pixels per word */
1149 int bpl; /* bytes per line */
1152 if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1154 mp = get_mode_param(adp->va_mode);
1158 switch (adp->va_info.vi_mem_model) {
1159 case V_INFO_MM_PLANAR:
1160 ppw = 16/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1161 count = (pixel + ppw - 1)/ppw/2;
1162 bpl = ((pixel + ppw - 1)/ppw/2)*4;
1164 case V_INFO_MM_PACKED:
1165 count = (pixel + 7)/8;
1166 bpl = ((pixel + 7)/8)*8;
1168 case V_INFO_MM_TEXT:
1169 count = (pixel + 7)/8; /* columns */
1170 bpl = (pixel + 7)/8; /* columns */
1176 if (mp[10 + 0x17] & 0x40) /* CRTC mode control reg */
1177 count *= 2; /* byte mode */
1178 outb(adp->va_crtc_addr, 0x13);
1179 outb(adp->va_crtc_addr + 1, count);
1180 adp->va_line_width = bpl;
1186 set_display_start(video_adapter_t *adp, int x, int y)
1188 int off; /* byte offset (graphics mode)/word offset (text mode) */
1189 int poff; /* pixel offset */
1190 int roff; /* row offset */
1191 int ppb; /* pixels per byte */
1193 if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1195 if (adp->va_info.vi_flags & V_INFO_GRAPHICS) {
1196 ppb = 8/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1197 off = y*adp->va_line_width + x/ppb;
1201 if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1210 off = y/adp->va_info.vi_cheight*adp->va_line_width + x/ppb;
1211 roff = y%adp->va_info.vi_cheight;
1212 /* FIXME: is this correct? XXX */
1220 outb(adp->va_crtc_addr, 0xc); /* high */
1221 outb(adp->va_crtc_addr + 1, off >> 8);
1222 outb(adp->va_crtc_addr, 0xd); /* low */
1223 outb(adp->va_crtc_addr + 1, off & 0xff);
1225 /* horizontal pel pan */
1226 if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1227 inb(adp->va_crtc_addr + 6);
1228 outb(ATC, 0x13 | 0x20);
1230 inb(adp->va_crtc_addr + 6);
1234 /* preset raw scan */
1235 outb(adp->va_crtc_addr, 8);
1236 outb(adp->va_crtc_addr + 1, roff);
1238 adp->va_disp_start.x = x;
1239 adp->va_disp_start.y = y;
1243 #ifndef VGA_NO_MODE_CHANGE
1244 #ifdef __i386__ /* XXX */
1246 fill(int val, void *d, size_t size)
1253 #endif /* __i386__ */
1256 filll_io(int val, vm_offset_t d, size_t size)
1258 while (size-- > 0) {
1260 d += sizeof(u_int32_t);
1263 #endif /* !VGA_NO_MODE_CHANGE */
1282 vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1285 if (unit >= biosadapters)
1288 *adpp = &biosadapter[unit];
1294 vga_init(int unit, video_adapter_t *adp, int flags)
1296 if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1299 if (!init_done(adp)) {
1300 /* nothing to do really... */
1301 adp->va_flags |= V_ADP_INITIALIZED;
1304 if (!config_done(adp)) {
1305 if (vid_register(adp) < 0)
1307 adp->va_flags |= V_ADP_REGISTERED;
1309 if (vga_sub_configure != NULL)
1310 (*vga_sub_configure)(0);
1317 * Return the video_info structure of the requested video mode.
1322 vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1329 #ifndef VGA_NO_MODE_CHANGE
1330 if (adp->va_flags & V_ADP_MODECHANGE) {
1332 * If the parameter table entry for this mode is not found,
1333 * the mode is not supported...
1335 if (get_mode_param(mode) == NULL)
1338 #endif /* VGA_NO_MODE_CHANGE */
1341 * Even if we don't support video mode switching on this adapter,
1342 * the information on the initial (thus current) video mode
1343 * should be made available.
1345 if (mode != adp->va_initial_mode)
1349 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1350 if (bios_vmode[i].vi_mode == NA)
1352 if (mode == bios_vmode[i].vi_mode) {
1353 *info = bios_vmode[i];
1355 info->vi_buffer_size = info->vi_window_size*info->vi_planes;
1364 * Find a video mode matching the requested parameters.
1365 * Fields filled with 0 are considered "don't care" fields and
1371 vga_query_mode(video_adapter_t *adp, video_info_t *info)
1378 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1379 if (bios_vmode[i].vi_mode == NA)
1382 if ((info->vi_width != 0)
1383 && (info->vi_width != bios_vmode[i].vi_width))
1385 if ((info->vi_height != 0)
1386 && (info->vi_height != bios_vmode[i].vi_height))
1388 if ((info->vi_cwidth != 0)
1389 && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1391 if ((info->vi_cheight != 0)
1392 && (info->vi_cheight != bios_vmode[i].vi_cheight))
1394 if ((info->vi_depth != 0)
1395 && (info->vi_depth != bios_vmode[i].vi_depth))
1397 if ((info->vi_planes != 0)
1398 && (info->vi_planes != bios_vmode[i].vi_planes))
1400 /* XXX: should check pixel format, memory model */
1401 if ((info->vi_flags != 0)
1402 && (info->vi_flags != bios_vmode[i].vi_flags))
1405 /* verify if this mode is supported on this adapter */
1406 if (vga_get_info(adp, bios_vmode[i].vi_mode, info))
1415 * Change the video mode.
1420 #ifndef VGA_NO_MODE_CHANGE
1423 set_width90(adp_state_t *params)
1426 * Based on code submitted by Kelly Yancey (kbyanc@freedomnet.com)
1427 * and alexv@sui.gda.itesm.mx.
1429 params->regs[5] |= 1; /* toggle 8 pixel wide fonts */
1430 params->regs[10+0x0] = 0x6b;
1431 params->regs[10+0x1] = 0x59;
1432 params->regs[10+0x2] = 0x5a;
1433 params->regs[10+0x3] = 0x8e;
1434 params->regs[10+0x4] = 0x5e;
1435 params->regs[10+0x5] = 0x8a;
1436 params->regs[10+0x13] = 45;
1437 params->regs[35+0x13] = 0;
1439 #endif /* VGA_WIDTH90 */
1440 #endif /* !VGA_NO_MODE_CHANGE */
1443 vga_set_mode(video_adapter_t *adp, int mode)
1445 #ifndef VGA_NO_MODE_CHANGE
1449 prologue(adp, V_ADP_MODECHANGE, ENODEV);
1451 if (vga_get_info(adp, mode, &info))
1455 kprintf("vga_set_mode(): setting mode %d\n", mode);
1458 params.sig = V_STATE_SIG;
1459 bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1463 case M_VGA_C90x60: case M_VGA_M90x60:
1464 set_width90(¶ms);
1467 case M_VGA_C80x60: case M_VGA_M80x60:
1468 params.regs[2] = 0x08;
1469 params.regs[19] = 0x47;
1473 case M_VGA_C90x30: case M_VGA_M90x30:
1474 set_width90(¶ms);
1477 case M_VGA_C80x30: case M_VGA_M80x30:
1478 params.regs[19] = 0x4f;
1480 params.regs[9] |= 0xc0;
1481 params.regs[16] = 0x08;
1482 params.regs[17] = 0x3e;
1483 params.regs[26] = 0xea;
1484 params.regs[28] = 0xdf;
1485 params.regs[31] = 0xe7;
1486 params.regs[32] = 0x04;
1490 case M_VGA_C90x43: case M_VGA_M90x43:
1491 set_width90(¶ms);
1494 case M_ENH_C80x43: case M_ENH_B80x43:
1495 params.regs[28] = 87;
1499 case M_VGA_C90x50: case M_VGA_M90x50:
1500 set_width90(¶ms);
1503 case M_VGA_C80x50: case M_VGA_M80x50:
1506 params.regs[19] = 7;
1510 case M_VGA_C90x25: case M_VGA_M90x25:
1511 set_width90(¶ms);
1514 case M_VGA_C40x25: case M_VGA_C80x25:
1516 case M_B40x25: case M_C40x25:
1517 case M_B80x25: case M_C80x25:
1518 case M_ENH_B40x25: case M_ENH_C40x25:
1519 case M_ENH_B80x25: case M_ENH_C80x25:
1520 case M_EGAMONO80x25:
1523 vga_load_state(adp, ¶ms);
1527 /* "unchain" the VGA mode */
1528 params.regs[5-1+0x04] &= 0xf7;
1529 params.regs[5-1+0x04] |= 0x04;
1530 /* turn off doubleword mode */
1531 params.regs[10+0x14] &= 0xbf;
1532 /* turn off word addressing */
1533 params.regs[10+0x17] |= 0x40;
1534 /* set logical screen width */
1535 params.regs[10+0x13] = 80;
1537 params.regs[10+0x11] = 0x2c;
1538 params.regs[10+0x06] = 0x0d;
1539 params.regs[10+0x07] = 0x3e;
1540 params.regs[10+0x10] = 0xea;
1541 params.regs[10+0x11] = 0xac;
1542 params.regs[10+0x12] = 0xdf;
1543 params.regs[10+0x15] = 0xe7;
1544 params.regs[10+0x16] = 0x06;
1545 /* set vertical sync polarity to reflect aspect ratio */
1546 params.regs[9] = 0xe3;
1549 case M_BG320: case M_CG320: case M_BG640:
1550 case M_CG320_D: case M_CG640_E:
1551 case M_CG640x350: case M_ENH_CG640:
1552 case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1555 vga_load_state(adp, ¶ms);
1562 adp->va_mode = mode;
1563 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1564 update_adapter_info(adp, &info);
1566 /* move hardware cursor out of the way */
1567 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1570 #else /* VGA_NO_MODE_CHANGE */
1572 #endif /* VGA_NO_MODE_CHANGE */
1575 #ifndef VGA_NO_FONT_LOADING
1578 set_font_mode(video_adapter_t *adp, u_char *buf)
1584 /* save register values */
1585 if (adp->va_type == KD_VGA) {
1586 outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1587 outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1588 outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1589 outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1590 outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1591 inb(adp->va_crtc_addr + 6);
1592 outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1593 } else /* if (adp->va_type == KD_EGA) */ {
1595 * EGA cannot be read; copy parameters from the mode parameter
1598 mp = get_mode_param(adp->va_mode);
1599 buf[0] = mp[5 + 0x02 - 1];
1600 buf[1] = mp[5 + 0x04 - 1];
1601 buf[2] = mp[55 + 0x04];
1602 buf[3] = mp[55 + 0x05];
1603 buf[4] = mp[55 + 0x06];
1604 buf[5] = mp[35 + 0x10];
1607 /* setup vga for loading fonts */
1608 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1609 outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1610 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1611 outb(ATC, 0x20); /* enable palette */
1613 #if VGA_SLOW_IOACCESS
1614 #ifdef VGA_ALT_SEQACCESS
1615 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1617 outb(TSIDX, 0x02); outb(TSREG, 0x04);
1618 outb(TSIDX, 0x04); outb(TSREG, 0x07);
1619 #ifdef VGA_ALT_SEQACCESS
1620 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1622 outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
1623 outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
1624 outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
1625 #else /* VGA_SLOW_IOACCESS */
1626 #ifdef VGA_ALT_SEQACCESS
1627 outw(TSIDX, 0x0100);
1629 outw(TSIDX, 0x0402);
1630 outw(TSIDX, 0x0704);
1631 #ifdef VGA_ALT_SEQACCESS
1632 outw(TSIDX, 0x0300);
1634 outw(GDCIDX, 0x0204);
1635 outw(GDCIDX, 0x0005);
1636 outw(GDCIDX, 0x0406); /* addr = a0000, 64kb */
1637 #endif /* VGA_SLOW_IOACCESS */
1643 set_normal_mode(video_adapter_t *adp, u_char *buf)
1647 /* setup vga for normal operation mode again */
1648 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1649 outb(ATC, 0x10); outb(ATC, buf[5]);
1650 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1651 outb(ATC, 0x20); /* enable palette */
1653 #if VGA_SLOW_IOACCESS
1654 #ifdef VGA_ALT_SEQACCESS
1655 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1657 outb(TSIDX, 0x02); outb(TSREG, buf[0]);
1658 outb(TSIDX, 0x04); outb(TSREG, buf[1]);
1659 #ifdef VGA_ALT_SEQACCESS
1660 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1662 outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
1663 outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
1664 if (adp->va_crtc_addr == MONO_CRTC) {
1665 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
1667 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1669 #else /* VGA_SLOW_IOACCESS */
1670 #ifdef VGA_ALT_SEQACCESS
1671 outw(TSIDX, 0x0100);
1673 outw(TSIDX, 0x0002 | (buf[0] << 8));
1674 outw(TSIDX, 0x0004 | (buf[1] << 8));
1675 #ifdef VGA_ALT_SEQACCESS
1676 outw(TSIDX, 0x0300);
1678 outw(GDCIDX, 0x0004 | (buf[2] << 8));
1679 outw(GDCIDX, 0x0005 | (buf[3] << 8));
1680 if (adp->va_crtc_addr == MONO_CRTC)
1681 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
1683 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1684 #endif /* VGA_SLOW_IOACCESS */
1689 #endif /* VGA_NO_FONT_LOADING */
1693 * Read the font data in the requested font page from the video adapter.
1698 vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1701 #ifndef VGA_NO_FONT_LOADING
1702 u_char buf[PARAM_BUFSIZE];
1705 #ifdef VGA_ALT_SEQACCESS
1709 prologue(adp, V_ADP_FONT, ENODEV);
1711 if (fontsize < 14) {
1714 } else if (fontsize >= 32) {
1716 } else if (fontsize >= 16) {
1724 if (page < 0 || page >= 8)
1726 segment = FONT_BUF + 0x4000*page;
1730 #ifdef VGA_ALT_SEQACCESS
1731 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1733 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1734 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1735 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1736 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1741 set_font_mode(adp, buf);
1742 if (fontsize == 32) {
1743 bcopy_fromio(segment + ch*32, data, fontsize*count);
1745 for (c = ch; count > 0; ++c, --count) {
1746 bcopy_fromio(segment + c*32, data, fontsize);
1750 set_normal_mode(adp, buf);
1752 #ifdef VGA_ALT_SEQACCESS
1753 if (adp->va_type == KD_VGA) {
1755 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1756 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1757 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1763 #else /* VGA_NO_FONT_LOADING */
1765 #endif /* VGA_NO_FONT_LOADING */
1770 * Set the font data in the requested font page.
1771 * NOTE: it appears that some recent video adapters do not support
1772 * the font page other than 0... XXX
1777 vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1780 #ifndef VGA_NO_FONT_LOADING
1781 u_char buf[PARAM_BUFSIZE];
1784 #ifdef VGA_ALT_SEQACCESS
1788 prologue(adp, V_ADP_FONT, ENODEV);
1790 if (fontsize < 14) {
1793 } else if (fontsize >= 32) {
1795 } else if (fontsize >= 16) {
1803 if (page < 0 || page >= 8)
1805 segment = FONT_BUF + 0x4000*page;
1809 #ifdef VGA_ALT_SEQACCESS
1810 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1812 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1813 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1814 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1815 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1820 set_font_mode(adp, buf);
1821 if (fontsize == 32) {
1822 bcopy_toio(data, segment + ch*32, fontsize*count);
1824 for (c = ch; count > 0; ++c, --count) {
1825 bcopy_toio(data, segment + c*32, fontsize);
1829 set_normal_mode(adp, buf);
1831 #ifdef VGA_ALT_SEQACCESS
1832 if (adp->va_type == KD_VGA) {
1834 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1835 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1836 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1842 #else /* VGA_NO_FONT_LOADING */
1844 #endif /* VGA_NO_FONT_LOADING */
1849 * Activate the requested font page.
1850 * NOTE: it appears that some recent video adapters do not support
1851 * the font page other than 0... XXX
1856 vga_show_font(video_adapter_t *adp, int page)
1858 #ifndef VGA_NO_FONT_LOADING
1859 static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1861 prologue(adp, V_ADP_FONT, ENODEV);
1862 if (page < 0 || page >= 8)
1866 outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1870 #else /* VGA_NO_FONT_LOADING */
1872 #endif /* VGA_NO_FONT_LOADING */
1877 * Read DAC values. The values have expressed in 8 bits.
1882 vga_save_palette(video_adapter_t *adp, u_char *palette)
1886 prologue(adp, V_ADP_PALETTE, ENODEV);
1889 * We store 8 bit values in the palette buffer, while the standard
1890 * VGA has 6 bit DAC .
1892 outb(PALRADR, 0x00);
1893 for (i = 0; i < 256*3; ++i)
1894 palette[i] = inb(PALDATA) << 2;
1895 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1900 vga_save_palette2(video_adapter_t *adp, int base, int count,
1901 u_char *r, u_char *g, u_char *b)
1905 prologue(adp, V_ADP_PALETTE, ENODEV);
1907 outb(PALRADR, base);
1908 for (i = 0; i < count; ++i) {
1909 r[i] = inb(PALDATA) << 2;
1910 g[i] = inb(PALDATA) << 2;
1911 b[i] = inb(PALDATA) << 2;
1913 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1924 vga_load_palette(video_adapter_t *adp, const u_char *palette)
1928 prologue(adp, V_ADP_PALETTE, ENODEV);
1930 outb(PIXMASK, 0xff); /* no pixelmask */
1931 outb(PALWADR, 0x00);
1932 for (i = 0; i < 256*3; ++i)
1933 outb(PALDATA, palette[i] >> 2);
1934 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1935 outb(ATC, 0x20); /* enable palette */
1940 vga_load_palette2(video_adapter_t *adp, int base, int count,
1941 u_char *r, u_char *g, u_char *b)
1945 prologue(adp, V_ADP_PALETTE, ENODEV);
1947 outb(PIXMASK, 0xff); /* no pixelmask */
1948 outb(PALWADR, base);
1949 for (i = 0; i < count; ++i) {
1950 outb(PALDATA, r[i] >> 2);
1951 outb(PALDATA, g[i] >> 2);
1952 outb(PALDATA, b[i] >> 2);
1954 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1955 outb(ATC, 0x20); /* enable palette */
1961 * Change the border color.
1966 vga_set_border(video_adapter_t *adp, int color)
1968 prologue(adp, V_ADP_BORDER, ENODEV);
1970 switch (adp->va_type) {
1973 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1974 outb(ATC, 0x31); outb(ATC, color & 0xff);
1977 outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
1989 * Read video register values.
1990 * NOTE: this function only reads the standard EGA/VGA registers.
1991 * any extra/extended registers of SVGA adapters are not saved.
1996 vga_save_state(video_adapter_t *adp, void *p, size_t size)
2004 /* return the required buffer size */
2005 prologue(adp, V_ADP_STATESAVE, 0);
2006 return sizeof(adp_state_t);
2008 prologue(adp, V_ADP_STATESAVE, ENODEV);
2009 if (size < sizeof(adp_state_t))
2013 ((adp_state_t *)p)->sig = V_STATE_SIG;
2014 buf = ((adp_state_t *)p)->regs;
2015 bzero(buf, V_MODE_PARAM_SIZE);
2016 crtc_addr = adp->va_crtc_addr;
2020 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
2021 for (i = 0, j = 5; i < 4; i++) {
2023 buf[j++] = inb(TSREG);
2025 buf[9] = inb(MISC + 10); /* dot-clock */
2026 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
2028 for (i = 0, j = 10; i < 25; i++) { /* crtc */
2030 buf[j++] = inb(crtc_addr + 1);
2032 for (i = 0, j = 35; i < 20; i++) { /* attribute ctrl */
2033 inb(crtc_addr + 6); /* reset flip-flop */
2035 buf[j++] = inb(ATC + 1);
2037 for (i = 0, j = 55; i < 9; i++) { /* graph data ctrl */
2039 buf[j++] = inb(GDCREG);
2041 inb(crtc_addr + 6); /* reset flip-flop */
2042 outb(ATC, 0x20); /* enable palette */
2047 if (vga_get_info(adp, adp->va_mode, &info) == 0) {
2048 if (info.vi_flags & V_INFO_GRAPHICS) {
2049 buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
2050 buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
2052 buf[0] = info.vi_width; /* COLS */
2053 buf[1] = info.vi_height - 1; /* ROWS */
2055 buf[2] = info.vi_cheight; /* POINTS */
2057 /* XXX: shouldn't be happening... */
2058 kprintf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
2059 adp->va_unit, adp->va_name);
2062 buf[0] = readb(BIOS_PADDRTOVADDR(0x44a)); /* COLS */
2063 buf[1] = readb(BIOS_PADDRTOVADDR(0x484)); /* ROWS */
2064 buf[2] = readb(BIOS_PADDRTOVADDR(0x485)); /* POINTS */
2065 buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
2066 buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
2074 * Set video registers at once.
2075 * NOTE: this function only updates the standard EGA/VGA registers.
2076 * any extra/extended registers of SVGA adapters are not changed.
2081 vga_load_state(video_adapter_t *adp, void *p)
2087 prologue(adp, V_ADP_STATELOAD, ENODEV);
2088 if (((adp_state_t *)p)->sig != V_STATE_SIG)
2091 buf = ((adp_state_t *)p)->regs;
2092 crtc_addr = adp->va_crtc_addr;
2095 dump_buffer(buf, V_MODE_PARAM_SIZE);
2100 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
2101 for (i = 0; i < 4; ++i) { /* program sequencer */
2103 outb(TSREG, buf[i + 5]);
2105 outb(MISC, buf[9]); /* set dot-clock */
2106 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
2107 outb(crtc_addr, 0x11);
2108 outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
2109 for (i = 0; i < 25; ++i) { /* program crtc */
2111 outb(crtc_addr + 1, buf[i + 10]);
2113 inb(crtc_addr+6); /* reset flip-flop */
2114 for (i = 0; i < 20; ++i) { /* program attribute ctrl */
2116 outb(ATC, buf[i + 35]);
2118 for (i = 0; i < 9; ++i) { /* program graph data ctrl */
2120 outb(GDCREG, buf[i + 55]);
2122 inb(crtc_addr + 6); /* reset flip-flop */
2123 outb(ATC, 0x20); /* enable palette */
2125 #if notyet /* a temporary workaround for kernel panic, XXX */
2127 if (adp->va_unit == V_ADP_PRIMARY) {
2128 writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]); /* COLS */
2129 writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
2130 writeb(BIOS_PADDRTOVADDR(0x485), buf[2]); /* POINTS */
2132 writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
2133 writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
2136 #endif /* VGA_NO_BIOS */
2145 * Change the origin (window mapping) of the banked frame buffer.
2148 vga_set_origin(video_adapter_t *adp, off_t offset)
2151 * The standard video modes do not require window mapping;
2152 * always return error.
2159 * Read the position of the hardware text cursor.
2164 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
2171 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2175 outb(adp->va_crtc_addr, 14);
2176 off = inb(adp->va_crtc_addr + 1);
2177 outb(adp->va_crtc_addr, 15);
2178 off = (off << 8) | inb(adp->va_crtc_addr + 1);
2181 *row = off / adp->va_info.vi_width;
2182 *col = off % adp->va_info.vi_width;
2189 * Move the hardware text cursor. If col and row are both -1,
2190 * the cursor won't be shown.
2195 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
2202 if ((col == -1) && (row == -1)) {
2205 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2207 off = row*adp->va_info.vi_width + col;
2211 outb(adp->va_crtc_addr, 14);
2212 outb(adp->va_crtc_addr + 1, off >> 8);
2213 outb(adp->va_crtc_addr, 15);
2214 outb(adp->va_crtc_addr + 1, off & 0x00ff);
2221 * set_hw_cursor_shape():
2222 * Change the shape of the hardware text cursor. If the height is
2223 * zero or negative, the cursor won't be shown.
2228 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2229 int celsize, int blink)
2235 switch (adp->va_type) {
2242 /* make the cursor invisible */
2243 outb(adp->va_crtc_addr, 10);
2244 outb(adp->va_crtc_addr + 1, 32);
2245 outb(adp->va_crtc_addr, 11);
2246 outb(adp->va_crtc_addr + 1, 0);
2248 outb(adp->va_crtc_addr, 10);
2249 outb(adp->va_crtc_addr + 1, celsize - base - height);
2250 outb(adp->va_crtc_addr, 11);
2251 outb(adp->va_crtc_addr + 1, celsize - base - 1);
2256 /* make the cursor invisible */
2257 outb(adp->va_crtc_addr, 10);
2258 outb(adp->va_crtc_addr + 1, celsize);
2259 outb(adp->va_crtc_addr, 11);
2260 outb(adp->va_crtc_addr + 1, 0);
2262 outb(adp->va_crtc_addr, 10);
2263 outb(adp->va_crtc_addr + 1, celsize - base - height);
2264 outb(adp->va_crtc_addr, 11);
2265 outb(adp->va_crtc_addr + 1, celsize - base);
2276 * Put the display in power save/power off mode.
2281 vga_blank_display(video_adapter_t *adp, int mode)
2286 switch (adp->va_type) {
2289 case V_DISPLAY_SUSPEND:
2290 case V_DISPLAY_STAND_BY:
2294 outb(TSREG, val | 0x20);
2295 outb(adp->va_crtc_addr, 0x17);
2296 val = inb(adp->va_crtc_addr + 1);
2297 outb(adp->va_crtc_addr + 1, val & ~0x80);
2303 outb(TSREG, val | 0x20);
2309 outb(TSREG, val & 0xDF);
2310 outb(adp->va_crtc_addr, 0x17);
2311 val = inb(adp->va_crtc_addr + 1);
2312 outb(adp->va_crtc_addr + 1, val | 0x80);
2318 /* no support yet */
2324 case V_DISPLAY_SUSPEND:
2325 case V_DISPLAY_STAND_BY:
2327 outb(adp->va_crtc_addr + 4, 0x25);
2330 outb(adp->va_crtc_addr + 4, 0x2d);
2338 case V_DISPLAY_SUSPEND:
2339 case V_DISPLAY_STAND_BY:
2341 outb(adp->va_crtc_addr + 4, 0x21);
2344 outb(adp->va_crtc_addr + 4, 0x29);
2358 * Mmap frame buffer.
2363 vga_mmap_buf(video_adapter_t *adp, vm_offset_t offset, int prot)
2365 if (adp->va_info.vi_flags & V_INFO_LINEAR)
2369 kprintf("vga_mmap_buf(): window:0x%x, offset:0x%x\n",
2370 adp->va_info.vi_window, offset);
2373 /* XXX: is this correct? */
2374 if (offset > adp->va_window_size - PAGE_SIZE)
2378 return i386_btop(adp->va_info.vi_window + offset);
2382 #ifndef VGA_NO_MODE_CHANGE
2385 planar_fill(video_adapter_t *adp, int val)
2388 int at; /* position in the frame buffer */
2391 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
2392 outw(GDCIDX, 0x0003); /* data rotate/function select */
2393 outw(GDCIDX, 0x0f01); /* set/reset enable */
2394 outw(GDCIDX, 0xff08); /* bit mask */
2395 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
2397 length = adp->va_line_width*adp->va_info.vi_height;
2398 while (length > 0) {
2399 l = imin(length, adp->va_window_size);
2400 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2401 bzero_io(adp->va_window, l);
2405 outw(GDCIDX, 0x0000); /* set/reset */
2406 outw(GDCIDX, 0x0001); /* set/reset enable */
2410 packed_fill(video_adapter_t *adp, int val)
2413 int at; /* position in the frame buffer */
2417 length = adp->va_line_width*adp->va_info.vi_height;
2418 while (length > 0) {
2419 l = imin(length, adp->va_window_size);
2420 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2421 fill_io(val, adp->va_window, l);
2428 direct_fill(video_adapter_t *adp, int val)
2431 int at; /* position in the frame buffer */
2435 length = adp->va_line_width*adp->va_info.vi_height;
2436 while (length > 0) {
2437 l = imin(length, adp->va_window_size);
2438 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2439 switch (adp->va_info.vi_pixel_size) {
2440 case sizeof(u_int16_t):
2441 fillw_io(val, adp->va_window, l/sizeof(u_int16_t));
2446 case sizeof(u_int32_t):
2447 filll_io(val, adp->va_window, l/sizeof(u_int32_t));
2456 vga_clear(video_adapter_t *adp)
2458 switch (adp->va_info.vi_mem_model) {
2459 case V_INFO_MM_TEXT:
2460 /* do nothing? XXX */
2462 case V_INFO_MM_PLANAR:
2463 planar_fill(adp, 0);
2465 case V_INFO_MM_PACKED:
2466 packed_fill(adp, 0);
2468 case V_INFO_MM_DIRECT:
2469 direct_fill(adp, 0);
2477 planar_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2482 int offset; /* offset within window */
2486 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
2487 outw(GDCIDX, 0x0003); /* data rotate/function select */
2488 outw(GDCIDX, 0x0f01); /* set/reset enable */
2489 outw(GDCIDX, 0xff08); /* bit mask */
2490 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
2492 banksize = adp->va_window_size;
2495 pos = adp->va_line_width*y + x/8;
2496 if (bank != pos/banksize) {
2497 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2498 bank = pos/banksize;
2500 offset = pos%banksize;
2501 bx = (x + cx)/8 - x/8;
2503 outw(GDCIDX, ((0xff00 >> (x % 8)) & 0xff00) | 0x08);
2504 writeb(adp->va_window + offset, 0);
2507 if (offset >= banksize) {
2509 ++bank; /* next bank */
2510 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2512 outw(GDCIDX, 0xff08); /* bit mask */
2515 l = imin(bx, banksize);
2516 bzero_io(adp->va_window + offset, l);
2519 if (offset >= banksize) {
2521 ++bank; /* next bank */
2522 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2526 outw(GDCIDX, (~(0xff00 >> ((x + cx) % 8)) & 0xff00) | 0x08);
2527 writeb(adp->va_window + offset, 0);
2529 if (offset >= banksize) {
2531 ++bank; /* next bank */
2532 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2534 outw(GDCIDX, 0xff08); /* bit mask */
2540 outw(GDCIDX, 0xff08); /* bit mask */
2541 outw(GDCIDX, 0x0000); /* set/reset */
2542 outw(GDCIDX, 0x0001); /* set/reset enable */
2546 packed_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2551 int offset; /* offset within window */
2554 banksize = adp->va_window_size;
2556 cx *= adp->va_info.vi_pixel_size;
2558 pos = adp->va_line_width*y + x*adp->va_info.vi_pixel_size;
2559 if (bank != pos/banksize) {
2560 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2561 bank = pos/banksize;
2563 offset = pos%banksize;
2564 end = imin(offset + cx, banksize);
2565 fill_io(val, adp->va_window + offset,
2566 (end - offset)/adp->va_info.vi_pixel_size);
2567 /* the line may cross the window boundary */
2568 if (offset + cx > banksize) {
2569 ++bank; /* next bank */
2570 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2571 end = offset + cx - banksize;
2572 fill_io(val, adp->va_window, end/adp->va_info.vi_pixel_size);
2580 direct_fill_rect16(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2585 int offset; /* offset within window */
2589 * XXX: the function assumes that banksize is a muliple of
2590 * sizeof(u_int16_t).
2592 banksize = adp->va_window_size;
2594 cx *= sizeof(u_int16_t);
2596 pos = adp->va_line_width*y + x*sizeof(u_int16_t);
2597 if (bank != pos/banksize) {
2598 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2599 bank = pos/banksize;
2601 offset = pos%banksize;
2602 end = imin(offset + cx, banksize);
2603 fillw_io(val, adp->va_window + offset,
2604 (end - offset)/sizeof(u_int16_t));
2605 /* the line may cross the window boundary */
2606 if (offset + cx > banksize) {
2607 ++bank; /* next bank */
2608 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2609 end = offset + cx - banksize;
2610 fillw_io(val, adp->va_window, end/sizeof(u_int16_t));
2618 direct_fill_rect24(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2623 int offset; /* offset within window */
2629 b[0] = val & 0x0000ff;
2630 b[1] = (val >> 8) & 0x0000ff;
2631 b[2] = (val >> 16) & 0x0000ff;
2632 banksize = adp->va_window_size;
2636 pos = adp->va_line_width*y + x*3;
2637 if (bank != pos/banksize) {
2638 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2639 bank = pos/banksize;
2641 offset = pos%banksize;
2642 end = imin(offset + cx, banksize);
2643 for (i = 0, j = offset; j < end; i = (++i)%3, ++j) {
2644 writeb(adp->va_window + j, b[i]);
2646 /* the line may cross the window boundary */
2647 if (offset + cx >= banksize) {
2648 ++bank; /* next bank */
2649 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2651 end = offset + cx - banksize;
2652 for (; j < end; i = (++i)%3, ++j) {
2653 writeb(adp->va_window + j, b[i]);
2662 direct_fill_rect32(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2667 int offset; /* offset within window */
2671 * XXX: the function assumes that banksize is a muliple of
2672 * sizeof(u_int32_t).
2674 banksize = adp->va_window_size;
2676 cx *= sizeof(u_int32_t);
2678 pos = adp->va_line_width*y + x*sizeof(u_int32_t);
2679 if (bank != pos/banksize) {
2680 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2681 bank = pos/banksize;
2683 offset = pos%banksize;
2684 end = imin(offset + cx, banksize);
2685 filll_io(val, adp->va_window + offset,
2686 (end - offset)/sizeof(u_int32_t));
2687 /* the line may cross the window boundary */
2688 if (offset + cx > banksize) {
2689 ++bank; /* next bank */
2690 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2691 end = offset + cx - banksize;
2692 filll_io(val, adp->va_window, end/sizeof(u_int32_t));
2700 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2702 switch (adp->va_info.vi_mem_model) {
2703 case V_INFO_MM_TEXT:
2704 /* do nothing? XXX */
2706 case V_INFO_MM_PLANAR:
2707 planar_fill_rect(adp, val, x, y, cx, cy);
2709 case V_INFO_MM_PACKED:
2710 packed_fill_rect(adp, val, x, y, cx, cy);
2712 case V_INFO_MM_DIRECT:
2713 switch (adp->va_info.vi_pixel_size) {
2714 case sizeof(u_int16_t):
2715 direct_fill_rect16(adp, val, x, y, cx, cy);
2718 direct_fill_rect24(adp, val, x, y, cx, cy);
2720 case sizeof(u_int32_t):
2721 direct_fill_rect32(adp, val, x, y, cx, cy);
2730 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2737 vga_bitblt(video_adapter_t *adp,...)
2743 #endif /* !VGA_NO_MODE_CHANGE */
2746 get_palette(video_adapter_t *adp, int base, int count,
2747 u_char *red, u_char *green, u_char *blue, u_char *trans)
2753 if (count < 0 || base < 0 || count > 256 || base > 256 ||
2757 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2760 if (vga_save_palette2(adp, base, count, r, g, b)) {
2764 copyout(r, red, count);
2765 copyout(g, green, count);
2766 copyout(b, blue, count);
2767 if (trans != NULL) {
2769 copyout(r, trans, count);
2777 set_palette(video_adapter_t *adp, int base, int count,
2778 u_char *red, u_char *green, u_char *blue, u_char *trans)
2785 if (count < 0 || base < 0 || count > 256 || base > 256 ||
2789 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2792 err = copyin(red, r, count);
2794 err = copyin(green, g, count);
2796 err = copyin(blue, b, count);
2798 err = vga_load_palette2(adp, base, count, r, g, b);
2801 return (err ? ENODEV : 0);
2805 vga_dev_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
2808 case FBIO_GETWINORG: /* get frame buffer window origin */
2812 case FBIO_SETWINORG: /* set frame buffer window origin */
2815 case FBIO_SETDISPSTART: /* set display start address */
2816 return (set_display_start(adp,
2817 ((video_display_start_t *)arg)->x,
2818 ((video_display_start_t *)arg)->y)
2821 case FBIO_SETLINEWIDTH: /* set scan line length in pixel */
2822 return (set_line_length(adp, *(u_int *)arg) ? ENODEV : 0);
2824 case FBIO_GETPALETTE: /* get color palette */
2825 return get_palette(adp, ((video_color_palette_t *)arg)->index,
2826 ((video_color_palette_t *)arg)->count,
2827 ((video_color_palette_t *)arg)->red,
2828 ((video_color_palette_t *)arg)->green,
2829 ((video_color_palette_t *)arg)->blue,
2830 ((video_color_palette_t *)arg)->transparent);
2832 case FBIO_SETPALETTE: /* set color palette */
2833 return set_palette(adp, ((video_color_palette_t *)arg)->index,
2834 ((video_color_palette_t *)arg)->count,
2835 ((video_color_palette_t *)arg)->red,
2836 ((video_color_palette_t *)arg)->green,
2837 ((video_color_palette_t *)arg)->blue,
2838 ((video_color_palette_t *)arg)->transparent);
2840 case FBIOGTYPE: /* get frame buffer type info. */
2841 ((struct fbtype *)arg)->fb_type = fb_type(adp->va_type);
2842 ((struct fbtype *)arg)->fb_height = adp->va_info.vi_height;
2843 ((struct fbtype *)arg)->fb_width = adp->va_info.vi_width;
2844 ((struct fbtype *)arg)->fb_depth = adp->va_info.vi_depth;
2845 if ((adp->va_info.vi_depth <= 1) || (adp->va_info.vi_depth > 8))
2846 ((struct fbtype *)arg)->fb_cmsize = 0;
2848 ((struct fbtype *)arg)->fb_cmsize = 1 << adp->va_info.vi_depth;
2849 ((struct fbtype *)arg)->fb_size = adp->va_buffer_size;
2852 case FBIOGETCMAP: /* get color palette */
2853 return get_palette(adp, ((struct fbcmap *)arg)->index,
2854 ((struct fbcmap *)arg)->count,
2855 ((struct fbcmap *)arg)->red,
2856 ((struct fbcmap *)arg)->green,
2857 ((struct fbcmap *)arg)->blue, NULL);
2859 case FBIOPUTCMAP: /* set color palette */
2860 return set_palette(adp, ((struct fbcmap *)arg)->index,
2861 ((struct fbcmap *)arg)->count,
2862 ((struct fbcmap *)arg)->red,
2863 ((struct fbcmap *)arg)->green,
2864 ((struct fbcmap *)arg)->blue, NULL);
2867 return fb_commonioctl(adp, cmd, arg);
2872 dump_buffer(u_char *buf, size_t len)
2876 for(i = 0; i < len;) {
2877 kprintf("%02x ", buf[i]);
2878 if ((++i % 16) == 0)
2885 * Print some information about the video adapter and video modes,
2886 * with requested level of details.
2891 vga_diag(video_adapter_t *adp, int level)
2904 kprintf("vga: RTC equip. code:0x%02x, DCC code:0x%02x\n",
2905 rtcin(RTC_EQUIPMENT), readb(BIOS_PADDRTOVADDR(0x488)));
2906 kprintf("vga: CRTC:0x%x, video option:0x%02x, ",
2907 readw(BIOS_PADDRTOVADDR(0x463)),
2908 readb(BIOS_PADDRTOVADDR(0x487)));
2909 kprintf("rows:%d, cols:%d, font height:%d\n",
2910 readb(BIOS_PADDRTOVADDR(0x44a)),
2911 readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2912 readb(BIOS_PADDRTOVADDR(0x485)));
2913 #endif /* VGA_NO_BIOS */
2914 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2915 kprintf("vga: param table EGA/VGA:%p", video_mode_ptr);
2916 kprintf(", CGA/MDA:%p\n", video_mode_ptr2);
2917 kprintf("vga: rows_offset:%d\n", rows_offset);
2919 #endif /* FB_DEBUG > 1 */
2921 fb_dump_adp_info(VGA_DRIVER_NAME, adp, level);
2924 if (adp->va_flags & V_ADP_MODECHANGE) {
2925 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2926 if (bios_vmode[i].vi_mode == NA)
2928 if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2930 fb_dump_mode_info(VGA_DRIVER_NAME, adp, &bios_vmode[i], level);
2933 vga_get_info(adp, adp->va_initial_mode, &info); /* shouldn't fail */
2934 fb_dump_mode_info(VGA_DRIVER_NAME, adp, &info, level);
2936 #endif /* FB_DEBUG > 1 */
2938 if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
2940 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2941 if (video_mode_ptr == NULL)
2942 kprintf("vga%d: %s: WARNING: video mode switching is not "
2943 "fully supported on this adapter\n",
2944 adp->va_unit, adp->va_name);
2949 if (adp->va_type == KD_VGA) {
2950 kprintf("VGA parameters upon power-up\n");
2951 dump_buffer(adpstate.regs, sizeof(adpstate.regs));
2952 kprintf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2953 dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
2956 mp = get_mode_param(adp->va_initial_mode);
2957 if (mp == NULL) /* this shouldn't be happening */
2959 kprintf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2960 dump_buffer(mp, V_MODE_PARAM_SIZE);