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.21 2007/04/04 07:14:26 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>
50 #include <machine/pc/vesa.h>
53 #include <vm/vm_param.h>
64 vga_probe_unit(int unit, video_adapter_t *buf, int flags)
70 sw = vid_get_switch(VGA_DRIVER_NAME);
73 error = (*sw->probe)(unit, &adp, NULL, flags);
76 bcopy(adp, buf, sizeof(*buf));
81 vga_attach_unit(int unit, vga_softc_t *sc, int flags)
86 sw = vid_get_switch(VGA_DRIVER_NAME);
90 error = (*sw->probe)(unit, &sc->adp, NULL, flags);
93 return (*sw->init)(unit, sc->adp, flags);
96 /* cdev driver functions */
98 #ifdef FB_INSTALL_CDEV
103 vga_open(cdev_t dev, vga_softc_t *sc, int flag, int mode, struct ucred *cred)
107 if (mode & (O_CREAT | O_APPEND | O_TRUNC))
110 return genfbopen(&sc->gensc, sc->adp, flag, mode, cred);
114 vga_close(cdev_t dev, vga_softc_t *sc, int flag, int mode)
116 return genfbclose(&sc->gensc, sc->adp, flag, mode);
120 vga_read(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
122 return genfbread(&sc->gensc, sc->adp, uio, flag);
126 vga_write(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
128 return genfbread(&sc->gensc, sc->adp, uio, flag);
132 vga_ioctl(cdev_t dev, vga_softc_t *sc, u_long cmd, caddr_t arg, int flag,
135 return genfbioctl(&sc->gensc, sc->adp, cmd, arg, flag, cred);
139 vga_mmap(cdev_t dev, vga_softc_t *sc, vm_offset_t offset, int prot)
141 return genfbmmap(&sc->gensc, sc->adp, offset, prot);
144 #endif /* FB_INSTALL_CDEV */
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)
152 /* this should really be in `rtc.h' */
153 #define RTC_EQUIPMENT 0x14
156 #define V_MODE_MAP_SIZE (M_VGA_CG320 + 1)
157 #define V_MODE_PARAM_SIZE 64
159 /* video adapter state buffer */
162 #define V_STATE_SIG 0x736f6962
163 u_char regs[V_MODE_PARAM_SIZE];
165 typedef struct adp_state adp_state_t;
167 /* video adapter information */
171 #define DCC_EGAMONO 3
176 * NOTE: `va_window' should have a virtual address, but is initialized
177 * with a physical address in the following table, as verify_adapter()
178 * will perform address conversion at run-time.
180 static video_adapter_t adapter_init_value[] = {
182 { 0, KD_MONO, "mda", 0, 0, 0, IO_MDA, IO_MDASIZE, MONO_CRTC,
183 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
186 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
187 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
190 { 0, KD_CGA, "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
191 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
194 { 0, KD_EGA, "ega", 0, 0, 0, IO_MDA, 48, MONO_CRTC,
195 EGA_BUF_BASE, EGA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
198 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
199 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
202 { 0, KD_EGA, "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48, COLOR_CRTC,
203 EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
207 static video_adapter_t biosadapter[2];
208 static int biosadapters = 0;
210 /* video driver declarations */
211 static int vga_configure(int flags);
212 int (*vga_sub_configure)(int flags);
214 static int vga_nop(void);
216 static int vga_error(void);
217 static vi_probe_t vga_probe;
218 static vi_init_t vga_init;
219 static vi_get_info_t vga_get_info;
220 static vi_query_mode_t vga_query_mode;
221 static vi_set_mode_t vga_set_mode;
222 static vi_save_font_t vga_save_font;
223 static vi_load_font_t vga_load_font;
224 static vi_show_font_t vga_show_font;
225 static vi_save_palette_t vga_save_palette;
226 static vi_load_palette_t vga_load_palette;
227 static vi_set_border_t vga_set_border;
228 static vi_save_state_t vga_save_state;
229 static vi_load_state_t vga_load_state;
230 static vi_set_win_org_t vga_set_origin;
231 static vi_read_hw_cursor_t vga_read_hw_cursor;
232 static vi_set_hw_cursor_t vga_set_hw_cursor;
233 static vi_set_hw_cursor_shape_t vga_set_hw_cursor_shape;
234 static vi_blank_display_t vga_blank_display;
235 static vi_mmap_t vga_mmap_buf;
236 static vi_ioctl_t vga_dev_ioctl;
237 #ifndef VGA_NO_MODE_CHANGE
238 static vi_clear_t vga_clear;
239 static vi_fill_rect_t vga_fill_rect;
240 static vi_bitblt_t vga_bitblt;
241 #else /* VGA_NO_MODE_CHANGE */
242 #define vga_clear (vi_clear_t *)vga_error
243 #define vga_fill_rect (vi_fill_rect_t *)vga_error
244 #define vga_bitblt (vi_bitblt_t *)vga_error
246 static vi_diag_t vga_diag;
248 static video_switch_t vgavidsw = {
265 vga_set_hw_cursor_shape,
277 VIDEO_DRIVER(mda, vgavidsw, NULL);
278 VIDEO_DRIVER(cga, vgavidsw, NULL);
279 VIDEO_DRIVER(ega, vgavidsw, NULL);
280 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
282 /* VGA BIOS standard video modes */
286 static video_info_t bios_vmode[] = {
288 { M_B40x25, V_INFO_COLOR, 40, 25, 8, 8, 2, 1,
289 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
290 { M_C40x25, V_INFO_COLOR, 40, 25, 8, 8, 4, 1,
291 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
292 { M_B80x25, V_INFO_COLOR, 80, 25, 8, 8, 2, 1,
293 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
294 { M_C80x25, V_INFO_COLOR, 80, 25, 8, 8, 4, 1,
295 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
297 { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
298 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
299 { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
300 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
301 { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
302 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
303 { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
304 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
306 { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
307 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
308 { M_VGA_M80x25, 0, 80, 25, 8, 16, 2, 1,
309 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
310 { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
311 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
313 { M_EGAMONO80x25, 0, 80, 25, 8, 14, 2, 1,
314 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
316 { M_ENH_B80x43, 0, 80, 43, 8, 8, 2, 1,
317 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
318 { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8, 8, 4, 1,
319 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
321 { M_VGA_M80x30, 0, 80, 30, 8, 16, 2, 1,
322 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
323 { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
324 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
325 { M_VGA_M80x50, 0, 80, 50, 8, 8, 2, 1,
326 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
327 { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8, 8, 4, 1,
328 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
329 { M_VGA_M80x60, 0, 80, 60, 8, 8, 2, 1,
330 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
331 { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8, 8, 4, 1,
332 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
334 #ifndef VGA_NO_MODE_CHANGE
337 { M_VGA_M90x25, 0, 90, 25, 8, 16, 2, 1,
338 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
339 { M_VGA_C90x25, V_INFO_COLOR, 90, 25, 8, 16, 4, 1,
340 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
341 { M_VGA_M90x30, 0, 90, 30, 8, 16, 2, 1,
342 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
343 { M_VGA_C90x30, V_INFO_COLOR, 90, 30, 8, 16, 4, 1,
344 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
345 { M_VGA_M90x43, 0, 90, 43, 8, 8, 2, 1,
346 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
347 { M_VGA_C90x43, V_INFO_COLOR, 90, 43, 8, 8, 4, 1,
348 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
349 { M_VGA_M90x50, 0, 90, 50, 8, 8, 2, 1,
350 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
351 { M_VGA_C90x50, V_INFO_COLOR, 90, 50, 8, 8, 4, 1,
352 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
353 { M_VGA_M90x60, 0, 90, 60, 8, 8, 2, 1,
354 MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
355 { M_VGA_C90x60, V_INFO_COLOR, 90, 60, 8, 8, 4, 1,
356 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
357 #endif /* VGA_WIDTH90 */
360 { M_BG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
361 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
362 { M_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 2, 1,
363 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
364 { M_BG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 1, 1,
365 CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
367 { M_CG320_D, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 4, 4,
368 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
370 { M_CG640_E, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8, 8, 4, 4,
371 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
373 { M_EGAMONOAPA, V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
374 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 ,
376 { M_ENHMONOAPA2,V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
377 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
379 { M_CG640x350, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
380 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
382 { M_ENH_CG640, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
383 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
386 { M_BG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
387 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
389 { M_CG640x480, V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
390 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
392 { M_VGA_CG320, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8, 8, 8, 1,
393 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
394 V_INFO_MM_PACKED, 1 },
395 { M_VGA_MODEX, V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8, 8, 8, 4,
396 GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
398 #endif /* VGA_NO_MODE_CHANGE */
403 static int vga_init_done = FALSE;
404 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
405 static u_char *video_mode_ptr = NULL; /* EGA/VGA */
406 static u_char *video_mode_ptr2 = NULL; /* CGA/MDA */
408 static u_char *mode_map[V_MODE_MAP_SIZE];
409 static adp_state_t adpstate;
410 static adp_state_t adpstate2;
411 static int rows_offset = 1;
413 /* local macros and functions */
414 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
416 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
417 static void map_mode_table(u_char **, u_char *, int);
419 static void clear_mode_map(video_adapter_t *, u_char **, int, int);
420 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
421 static int map_mode_num(int);
423 static int map_bios_mode_num(int, int, int);
424 static u_char *get_mode_param(int);
426 static void fill_adapter_param(int, video_adapter_t *);
428 static int verify_adapter(video_adapter_t *);
429 static void update_adapter_info(video_adapter_t *, video_info_t *);
430 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
431 #define COMP_IDENTICAL 0
432 #define COMP_SIMILAR 1
433 #define COMP_DIFFERENT 2
434 static int comp_adpregs(u_char *, u_char *);
436 static int probe_adapters(void);
437 static int set_line_length(video_adapter_t *, int);
438 static int set_display_start(video_adapter_t *, int, int);
440 #ifndef VGA_NO_MODE_CHANGE
442 static void set_width90(adp_state_t *);
444 #endif /* !VGA_NO_MODE_CHANGE */
446 #ifndef VGA_NO_FONT_LOADING
447 #define PARAM_BUFSIZE 6
448 static void set_font_mode(video_adapter_t *, u_char *);
449 static void set_normal_mode(video_adapter_t *, u_char *);
452 #ifndef VGA_NO_MODE_CHANGE
453 static void filll_io(int, vm_offset_t, size_t);
454 static void planar_fill(video_adapter_t *, int);
455 static void packed_fill(video_adapter_t *, int);
456 static void direct_fill(video_adapter_t *, int);
458 static void planar_fill_rect(video_adapter_t *, int, int, int, int, int);
459 static void packed_fill_rect(video_adapter_t *, int, int, int, int, int);
460 static void direct_fill_rect16(video_adapter_t *, int, int, int, int, int);
461 static void direct_fill_rect24(video_adapter_t *, int, int, int, int, int);
462 static void direct_fill_rect32(video_adapter_t *, int, int, int, int, int);
464 #endif /* !VGA_NO_MODE_CHANGE */
466 static void dump_buffer(u_char *, size_t);
468 #define ISMAPPED(pa, width) \
469 (((pa) <= (u_long)0x1000 - (width)) \
470 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
472 #define prologue(adp, flag, err) \
473 if (!vga_init_done || !((adp)->va_flags & (flag))) \
476 /* a backdoor for the console driver */
478 vga_configure(int flags)
483 for (i = 0; i < biosadapters; ++i) {
484 if (!probe_done(&biosadapter[i]))
486 biosadapter[i].va_flags |= V_ADP_INITIALIZED;
487 if (!config_done(&biosadapter[i])) {
488 if (vid_register(&biosadapter[i]) < 0)
490 biosadapter[i].va_flags |= V_ADP_REGISTERED;
493 if (vga_sub_configure != NULL)
494 (*vga_sub_configure)(flags);
499 /* local subroutines */
501 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
502 /* construct the mode parameter map */
504 map_mode_table(u_char *map[], u_char *table, int max)
508 for(i = 0; i < max; ++i)
509 map[i] = table + i*V_MODE_PARAM_SIZE;
510 for(; i < V_MODE_MAP_SIZE; ++i)
513 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
516 clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
522 * NOTE: we don't touch `bios_vmode[]' because it is shared
525 for(i = 0; i < max; ++i) {
526 if (vga_get_info(adp, i, &info))
528 if ((info.vi_flags & V_INFO_COLOR) != color)
533 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
534 /* map the non-standard video mode to a known mode number */
536 map_mode_num(int mode)
542 { M_ENH_B80x43, M_ENH_B80x25 },
543 { M_ENH_C80x43, M_ENH_C80x25 },
544 { M_VGA_M80x30, M_VGA_M80x25 },
545 { M_VGA_C80x30, M_VGA_C80x25 },
546 { M_VGA_M80x50, M_VGA_M80x25 },
547 { M_VGA_C80x50, M_VGA_C80x25 },
548 { M_VGA_M80x60, M_VGA_M80x25 },
549 { M_VGA_C80x60, M_VGA_C80x25 },
551 { M_VGA_M90x25, M_VGA_M80x25 },
552 { M_VGA_C90x25, M_VGA_C80x25 },
553 { M_VGA_M90x30, M_VGA_M80x25 },
554 { M_VGA_C90x30, M_VGA_C80x25 },
555 { M_VGA_M90x43, M_ENH_B80x25 },
556 { M_VGA_C90x43, M_ENH_C80x25 },
557 { M_VGA_M90x50, M_VGA_M80x25 },
558 { M_VGA_C90x50, M_VGA_C80x25 },
559 { M_VGA_M90x60, M_VGA_M80x25 },
560 { M_VGA_C90x60, M_VGA_C80x25 },
562 { M_VGA_MODEX, M_VGA_CG320 },
566 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
567 if (mode_map[i].from == mode)
568 return mode_map[i].to;
572 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
574 /* turn the BIOS video number into our video mode number */
576 map_bios_mode_num(int type, int color, int bios_mode)
578 static int cga_modes[7] = {
579 M_B40x25, M_C40x25, /* 0, 1 */
580 M_B80x25, M_C80x25, /* 2, 3 */
584 static int ega_modes[17] = {
585 M_ENH_B40x25, M_ENH_C40x25, /* 0, 1 */
586 M_ENH_B80x25, M_ENH_C80x25, /* 2, 3 */
589 M_EGAMONO80x25, /* 7 */
593 M_ENHMONOAPA2, /* XXX: video momery > 64K */
594 M_ENH_CG640, /* XXX: video momery > 64K */
596 static int vga_modes[20] = {
597 M_VGA_C40x25, M_VGA_C40x25, /* 0, 1 */
598 M_VGA_C80x25, M_VGA_C80x25, /* 2, 3 */
601 M_VGA_M80x25, /* 7 */
607 M_BG640x480, M_CG640x480,
614 if (bios_mode < sizeof(vga_modes)/sizeof(vga_modes[0]))
615 return vga_modes[bios_mode];
623 if (bios_mode < sizeof(ega_modes)/sizeof(ega_modes[0]))
624 return ega_modes[bios_mode];
628 return M_EGAMONO80x25;
632 if (bios_mode < sizeof(cga_modes)/sizeof(cga_modes[0]))
633 return cga_modes[bios_mode];
640 return M_EGAMONO80x25; /* XXX: this name is confusing */
648 /* look up a parameter table entry */
650 get_mode_param(int mode)
652 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
653 if (mode >= V_MODE_MAP_SIZE)
654 mode = map_mode_num(mode);
656 if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
657 return mode_map[mode];
664 fill_adapter_param(int code, video_adapter_t *adp)
670 { DCC_MONO, DCC_EGA40 /* CGA monitor */ },
671 { DCC_MONO, DCC_EGA80 /* CGA monitor */ },
672 { DCC_MONO, DCC_EGA80 },
673 { DCC_MONO, DCC_EGA80 },
674 { DCC_CGA40, DCC_EGAMONO },
675 { DCC_CGA80, DCC_EGAMONO },
676 { DCC_EGA40 /* CGA monitor */, DCC_MONO},
677 { DCC_EGA80 /* CGA monitor */, DCC_MONO},
678 { DCC_EGA80, DCC_MONO },
679 { DCC_EGA80, DCC_MONO },
680 { DCC_EGAMONO, DCC_CGA40 },
681 { DCC_EGAMONO, DCC_CGA80 },
684 if ((code < 0) || (code >= sizeof(dcc)/sizeof(dcc[0]))) {
685 adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
686 adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
688 adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
689 adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
692 #endif /* VGA_NO_BIOS */
695 verify_adapter(video_adapter_t *adp)
699 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
703 buf = BIOS_PADDRTOVADDR(adp->va_window);
706 if (readw(buf) != 0xA55A)
710 switch (adp->va_type) {
713 outb(adp->va_crtc_addr, 7);
714 if (inb(adp->va_crtc_addr) == 7) {
715 adp->va_type = KD_VGA;
716 adp->va_name = "vga";
717 adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
719 adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
720 /* the color adapter may be in the 40x25 mode... XXX */
722 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
723 /* get the BIOS video mode pointer */
724 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
725 p = BIOS_SADDRTOLADDR(p);
726 if (ISMAPPED(p, sizeof(u_int32_t))) {
727 p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
728 p = BIOS_SADDRTOLADDR(p);
729 if (ISMAPPED(p, V_MODE_PARAM_SIZE))
730 video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
736 adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
737 /* may be in the 40x25 mode... XXX */
738 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
739 /* get the BIOS video mode pointer */
740 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
741 p = BIOS_SADDRTOLADDR(p);
742 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
747 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
748 /* get the BIOS video mode pointer */
749 p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
750 p = BIOS_SADDRTOLADDR(p);
751 video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
760 update_adapter_info(video_adapter_t *adp, video_info_t *info)
762 adp->va_flags &= ~V_ADP_COLOR;
764 (info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
766 (adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
767 adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
768 adp->va_window_size = info->vi_window_size;
769 adp->va_window_gran = info->vi_window_gran;
770 adp->va_window_orig = 0;
772 adp->va_buffer = info->vi_buffer;
773 adp->va_buffer_size = info->vi_buffer_size;
774 if (info->vi_mem_model == V_INFO_MM_VGAX) {
775 adp->va_line_width = info->vi_width/2;
776 } else if (info->vi_flags & V_INFO_GRAPHICS) {
777 switch (info->vi_depth/info->vi_planes) {
779 adp->va_line_width = info->vi_width/8;
782 adp->va_line_width = info->vi_width/4;
785 adp->va_line_width = info->vi_width/2;
788 default: /* shouldn't happen */
789 adp->va_line_width = info->vi_width;
793 adp->va_line_width = info->vi_width;
795 adp->va_disp_start.x = 0;
796 adp->va_disp_start.y = 0;
797 bcopy(info, &adp->va_info, sizeof(adp->va_info));
800 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
801 /* compare two parameter table entries */
803 comp_adpregs(u_char *buf1, u_char *buf2)
807 } params[V_MODE_PARAM_SIZE] = {
808 {0xff}, {0x00}, {0xff}, /* COLS}, ROWS}, POINTS */
809 {0x00}, {0x00}, /* page length */
810 {0xfe}, {0xff}, {0xff}, {0xff}, /* sequencer registers */
811 {0xf3}, /* misc register */
812 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff}, /* CRTC */
813 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
814 {0x00}, {0x00}, {0x00}, {0x00}, {0x00},
815 {0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
816 {0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
817 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* attribute controller regs */
818 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
819 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
820 {0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
821 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* GDC register */
822 {0xff}, {0xff}, {0xff}, {0xff},
824 int identical = TRUE;
827 if ((buf1 == NULL) || (buf2 == NULL))
828 return COMP_DIFFERENT;
830 for (i = 0; i < sizeof(params)/sizeof(params[0]); ++i) {
831 if (params[i].mask == 0) /* don't care */
833 if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
834 return COMP_DIFFERENT;
835 if (buf1[i] != buf2[i])
838 return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
840 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
842 /* probe video adapters and return the number of detected adapters */
846 video_adapter_t *adp;
848 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
853 /* do this test only once */
856 vga_init_done = TRUE;
859 * Locate display adapters.
860 * The AT architecture supports upto two adapters. `syscons' allows
861 * the following combinations of adapters:
863 * 2) MDA + EGA/VGA color
864 * 3) CGA + EGA/VGA mono
865 * Note that `syscons' doesn't bother with MCGA as it is only
866 * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
867 * thus, they are not running FreeBSD!
868 * When there are two adapaters in the system, one becomes `primary'
869 * and the other `secondary'. The EGA adapter has a set of DIP
870 * switches on board for this information and the EGA BIOS copies
871 * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88).
872 * The VGA BIOS has more sophisticated mechanism and has this
873 * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
874 * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
878 * Check rtc and BIOS data area.
879 * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
880 * copy of RTC_EQUIPMENT. Bits 4 and 5 of ETC_EQUIPMENT are
881 * zeros for EGA and VGA. However, the EGA/VGA BIOS sets
882 * these bits in BIOSDATA_EQUIPMENT according to the monitor
886 if (*(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8)) {
887 /* EGA/VGA BIOS is present */
888 fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
891 switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) { /* bit 4 and 5 */
893 /* EGA/VGA: shouldn't be happening */
894 fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
899 /* FIXME: switch to the 80x25 mode? XXX */
900 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
901 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
905 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
906 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
910 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
911 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
916 /* assume EGA/VGA? XXX */
917 biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_EGA80];
918 biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
919 #endif /* VGA_NO_BIOS */
922 if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
924 biosadapter[V_ADP_SECONDARY].va_flags |= V_ADP_PROBED;
925 biosadapter[V_ADP_SECONDARY].va_mode =
926 biosadapter[V_ADP_SECONDARY].va_initial_mode =
927 map_bios_mode_num(biosadapter[V_ADP_SECONDARY].va_type,
928 biosadapter[V_ADP_SECONDARY].va_flags
930 biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
932 biosadapter[V_ADP_SECONDARY].va_type = -1;
934 if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
936 biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
938 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
939 readb(BIOS_PADDRTOVADDR(0x449));
941 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3; /* XXX */
943 biosadapter[V_ADP_PRIMARY].va_mode =
944 biosadapter[V_ADP_PRIMARY].va_initial_mode =
945 map_bios_mode_num(biosadapter[V_ADP_PRIMARY].va_type,
946 biosadapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
947 biosadapter[V_ADP_PRIMARY].va_initial_bios_mode);
949 biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
950 biosadapter[V_ADP_SECONDARY].va_type = -1;
952 if (biosadapters == 0)
954 biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
955 biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
957 #if 0 /* we don't need these... */
958 fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
959 fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
964 * We cannot have two video adapter of the same type; there must be
965 * only one of color or mono adapter, or one each of them.
967 if (biosadapters > 1) {
968 if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
970 /* we have two mono or color adapters!! */
971 return (biosadapters = 0);
976 * Ensure a zero start address. This is mainly to recover after
977 * switching from pcvt using userconfig(). The registers are w/o
978 * for old hardware so it's too hard to relocate the active screen
980 * This must be done before vga_save_state() for VGA.
982 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 12);
983 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
984 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 13);
985 outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
987 /* the video mode parameter table in EGA/VGA BIOS */
988 /* NOTE: there can be only one EGA/VGA, wheather color or mono,
989 * recognized by the video BIOS.
991 if ((biosadapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
992 (biosadapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
993 adp = &biosadapter[V_ADP_PRIMARY];
994 } else if ((biosadapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
995 (biosadapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
996 adp = &biosadapter[V_ADP_SECONDARY];
1000 bzero(mode_map, sizeof(mode_map));
1002 if (adp->va_type == KD_VGA) {
1003 vga_save_state(adp, &adpstate, sizeof(adpstate));
1004 for(i = 0; i < 16; i++)
1005 adp->va_palette_regs[i] = adpstate.regs[35 + i];
1006 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1007 mode_map[adp->va_initial_mode] = adpstate.regs;
1009 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1010 if (video_mode_ptr == NULL) {
1011 mode_map[adp->va_initial_mode] = adpstate.regs;
1014 /* discard the table if we are not familiar with it... */
1015 map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1016 mp = get_mode_param(adp->va_initial_mode);
1018 bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1019 switch (comp_adpregs(adpstate.regs, mp)) {
1020 case COMP_IDENTICAL:
1022 * OK, this parameter table looks reasonably familiar
1026 * This is a kludge for Toshiba DynaBook SS433
1027 * whose BIOS video mode table entry has the actual #
1028 * of rows at the offset 1; BIOSes from other
1029 * manufacturers store the # of rows - 1 there. XXX
1031 rows_offset = adpstate.regs[1] + 1 - mp[1];
1036 * Not exactly the same, but similar enough to be
1037 * trusted. However, use the saved register values
1038 * for the initial mode and other modes which are
1039 * based on the initial mode.
1041 mode_map[adp->va_initial_mode] = adpstate.regs;
1042 rows_offset = adpstate.regs[1] + 1 - mp[1];
1043 adpstate.regs[1] -= rows_offset - 1;
1046 case COMP_DIFFERENT:
1049 * Don't use the paramter table in BIOS. It doesn't
1050 * look familiar to us. Video mode switching is allowed
1051 * only if the new mode is the same as or based on
1054 video_mode_ptr = NULL;
1055 bzero(mode_map, sizeof(mode_map));
1056 mode_map[adp->va_initial_mode] = adpstate.regs;
1061 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1063 #ifndef VGA_NO_MODE_CHANGE
1064 adp->va_flags |= V_ADP_MODECHANGE;
1066 #ifndef VGA_NO_FONT_LOADING
1067 adp->va_flags |= V_ADP_FONT;
1069 } else if (adp->va_type == KD_EGA) {
1070 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1072 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1073 if (video_mode_ptr == NULL) {
1076 map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
1077 /* XXX how can one validate the EGA table... */
1078 mp = get_mode_param(adp->va_initial_mode);
1080 adp->va_flags |= V_ADP_MODECHANGE;
1081 #ifndef VGA_NO_FONT_LOADING
1082 adp->va_flags |= V_ADP_FONT;
1087 * This is serious. We will not be able to switch video
1090 video_mode_ptr = NULL;
1091 bzero(mode_map, sizeof(mode_map));
1095 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1099 /* remove conflicting modes if we have more than one adapter */
1100 if (biosadapters > 0) {
1101 for (i = 0; i < biosadapters; ++i) {
1102 if (!(biosadapter[i].va_flags & V_ADP_MODECHANGE))
1104 clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1105 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1107 if ((biosadapter[i].va_type == KD_VGA)
1108 || (biosadapter[i].va_type == KD_EGA)) {
1109 biosadapter[i].va_io_base =
1110 (biosadapter[i].va_flags & V_ADP_COLOR) ?
1112 biosadapter[i].va_io_size = 32;
1117 /* buffer address */
1118 vga_get_info(&biosadapter[V_ADP_PRIMARY],
1119 biosadapter[V_ADP_PRIMARY].va_initial_mode, &info);
1120 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1121 update_adapter_info(&biosadapter[V_ADP_PRIMARY], &info);
1123 if (biosadapters > 1) {
1124 vga_get_info(&biosadapter[V_ADP_SECONDARY],
1125 biosadapter[V_ADP_SECONDARY].va_initial_mode, &info);
1126 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1127 update_adapter_info(&biosadapter[V_ADP_SECONDARY], &info);
1131 * XXX: we should verify the following values for the primary adapter...
1132 * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
1133 * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
1134 * ? 0 : V_ADP_COLOR;
1135 * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
1136 * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
1137 * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
1138 * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
1141 return biosadapters;
1144 /* set the scan line length in pixel */
1146 set_line_length(video_adapter_t *adp, int pixel)
1149 int ppw; /* pixels per word */
1150 int bpl; /* bytes per line */
1153 if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1155 mp = get_mode_param(adp->va_mode);
1159 switch (adp->va_info.vi_mem_model) {
1160 case V_INFO_MM_PLANAR:
1161 ppw = 16/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1162 count = (pixel + ppw - 1)/ppw/2;
1163 bpl = ((pixel + ppw - 1)/ppw/2)*4;
1165 case V_INFO_MM_PACKED:
1166 count = (pixel + 7)/8;
1167 bpl = ((pixel + 7)/8)*8;
1169 case V_INFO_MM_TEXT:
1170 count = (pixel + 7)/8; /* columns */
1171 bpl = (pixel + 7)/8; /* columns */
1177 if (mp[10 + 0x17] & 0x40) /* CRTC mode control reg */
1178 count *= 2; /* byte mode */
1179 outb(adp->va_crtc_addr, 0x13);
1180 outb(adp->va_crtc_addr + 1, count);
1181 adp->va_line_width = bpl;
1187 set_display_start(video_adapter_t *adp, int x, int y)
1189 int off; /* byte offset (graphics mode)/word offset (text mode) */
1190 int poff; /* pixel offset */
1191 int roff; /* row offset */
1192 int ppb; /* pixels per byte */
1194 if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1196 if (adp->va_info.vi_flags & V_INFO_GRAPHICS) {
1197 ppb = 8/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1198 off = y*adp->va_line_width + x/ppb;
1202 if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1211 off = y/adp->va_info.vi_cheight*adp->va_line_width + x/ppb;
1212 roff = y%adp->va_info.vi_cheight;
1213 /* FIXME: is this correct? XXX */
1221 outb(adp->va_crtc_addr, 0xc); /* high */
1222 outb(adp->va_crtc_addr + 1, off >> 8);
1223 outb(adp->va_crtc_addr, 0xd); /* low */
1224 outb(adp->va_crtc_addr + 1, off & 0xff);
1226 /* horizontal pel pan */
1227 if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1228 inb(adp->va_crtc_addr + 6);
1229 outb(ATC, 0x13 | 0x20);
1231 inb(adp->va_crtc_addr + 6);
1235 /* preset raw scan */
1236 outb(adp->va_crtc_addr, 8);
1237 outb(adp->va_crtc_addr + 1, roff);
1239 adp->va_disp_start.x = x;
1240 adp->va_disp_start.y = y;
1244 #ifndef VGA_NO_MODE_CHANGE
1245 #ifdef __i386__ /* XXX */
1247 fill(int val, void *d, size_t size)
1254 #endif /* __i386__ */
1257 filll_io(int val, vm_offset_t d, size_t size)
1259 while (size-- > 0) {
1261 d += sizeof(u_int32_t);
1264 #endif /* !VGA_NO_MODE_CHANGE */
1283 vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1286 if (unit >= biosadapters)
1289 *adpp = &biosadapter[unit];
1295 vga_init(int unit, video_adapter_t *adp, int flags)
1297 if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1300 if (!init_done(adp)) {
1301 /* nothing to do really... */
1302 adp->va_flags |= V_ADP_INITIALIZED;
1305 if (!config_done(adp)) {
1306 if (vid_register(adp) < 0)
1308 adp->va_flags |= V_ADP_REGISTERED;
1310 if (vga_sub_configure != NULL)
1311 (*vga_sub_configure)(0);
1318 * Return the video_info structure of the requested video mode.
1323 vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1330 #ifndef VGA_NO_MODE_CHANGE
1331 if (adp->va_flags & V_ADP_MODECHANGE) {
1333 * If the parameter table entry for this mode is not found,
1334 * the mode is not supported...
1336 if (get_mode_param(mode) == NULL)
1339 #endif /* VGA_NO_MODE_CHANGE */
1342 * Even if we don't support video mode switching on this adapter,
1343 * the information on the initial (thus current) video mode
1344 * should be made available.
1346 if (mode != adp->va_initial_mode)
1350 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1351 if (bios_vmode[i].vi_mode == NA)
1353 if (mode == bios_vmode[i].vi_mode) {
1354 *info = bios_vmode[i];
1356 info->vi_buffer_size = info->vi_window_size*info->vi_planes;
1365 * Find a video mode matching the requested parameters.
1366 * Fields filled with 0 are considered "don't care" fields and
1372 vga_query_mode(video_adapter_t *adp, video_info_t *info)
1379 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1380 if (bios_vmode[i].vi_mode == NA)
1383 if ((info->vi_width != 0)
1384 && (info->vi_width != bios_vmode[i].vi_width))
1386 if ((info->vi_height != 0)
1387 && (info->vi_height != bios_vmode[i].vi_height))
1389 if ((info->vi_cwidth != 0)
1390 && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1392 if ((info->vi_cheight != 0)
1393 && (info->vi_cheight != bios_vmode[i].vi_cheight))
1395 if ((info->vi_depth != 0)
1396 && (info->vi_depth != bios_vmode[i].vi_depth))
1398 if ((info->vi_planes != 0)
1399 && (info->vi_planes != bios_vmode[i].vi_planes))
1401 /* XXX: should check pixel format, memory model */
1402 if ((info->vi_flags != 0)
1403 && (info->vi_flags != bios_vmode[i].vi_flags))
1406 /* verify if this mode is supported on this adapter */
1407 if (vga_get_info(adp, bios_vmode[i].vi_mode, info))
1416 * Change the video mode.
1421 #ifndef VGA_NO_MODE_CHANGE
1424 set_width90(adp_state_t *params)
1427 * Based on code submitted by Kelly Yancey (kbyanc@freedomnet.com)
1428 * and alexv@sui.gda.itesm.mx.
1430 params->regs[5] |= 1; /* toggle 8 pixel wide fonts */
1431 params->regs[10+0x0] = 0x6b;
1432 params->regs[10+0x1] = 0x59;
1433 params->regs[10+0x2] = 0x5a;
1434 params->regs[10+0x3] = 0x8e;
1435 params->regs[10+0x4] = 0x5e;
1436 params->regs[10+0x5] = 0x8a;
1437 params->regs[10+0x13] = 45;
1438 params->regs[35+0x13] = 0;
1440 #endif /* VGA_WIDTH90 */
1441 #endif /* !VGA_NO_MODE_CHANGE */
1444 vga_set_mode(video_adapter_t *adp, int mode)
1446 #ifndef VGA_NO_MODE_CHANGE
1450 prologue(adp, V_ADP_MODECHANGE, ENODEV);
1452 if (vga_get_info(adp, mode, &info))
1456 kprintf("vga_set_mode(): setting mode %d\n", mode);
1459 params.sig = V_STATE_SIG;
1460 bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1464 case M_VGA_C90x60: case M_VGA_M90x60:
1465 set_width90(¶ms);
1468 case M_VGA_C80x60: case M_VGA_M80x60:
1469 params.regs[2] = 0x08;
1470 params.regs[19] = 0x47;
1474 case M_VGA_C90x30: case M_VGA_M90x30:
1475 set_width90(¶ms);
1478 case M_VGA_C80x30: case M_VGA_M80x30:
1479 params.regs[19] = 0x4f;
1481 params.regs[9] |= 0xc0;
1482 params.regs[16] = 0x08;
1483 params.regs[17] = 0x3e;
1484 params.regs[26] = 0xea;
1485 params.regs[28] = 0xdf;
1486 params.regs[31] = 0xe7;
1487 params.regs[32] = 0x04;
1491 case M_VGA_C90x43: case M_VGA_M90x43:
1492 set_width90(¶ms);
1495 case M_ENH_C80x43: case M_ENH_B80x43:
1496 params.regs[28] = 87;
1500 case M_VGA_C90x50: case M_VGA_M90x50:
1501 set_width90(¶ms);
1504 case M_VGA_C80x50: case M_VGA_M80x50:
1507 params.regs[19] = 7;
1511 case M_VGA_C90x25: case M_VGA_M90x25:
1512 set_width90(¶ms);
1515 case M_VGA_C40x25: case M_VGA_C80x25:
1517 case M_B40x25: case M_C40x25:
1518 case M_B80x25: case M_C80x25:
1519 case M_ENH_B40x25: case M_ENH_C40x25:
1520 case M_ENH_B80x25: case M_ENH_C80x25:
1521 case M_EGAMONO80x25:
1524 vga_load_state(adp, ¶ms);
1528 /* "unchain" the VGA mode */
1529 params.regs[5-1+0x04] &= 0xf7;
1530 params.regs[5-1+0x04] |= 0x04;
1531 /* turn off doubleword mode */
1532 params.regs[10+0x14] &= 0xbf;
1533 /* turn off word addressing */
1534 params.regs[10+0x17] |= 0x40;
1535 /* set logical screen width */
1536 params.regs[10+0x13] = 80;
1538 params.regs[10+0x11] = 0x2c;
1539 params.regs[10+0x06] = 0x0d;
1540 params.regs[10+0x07] = 0x3e;
1541 params.regs[10+0x10] = 0xea;
1542 params.regs[10+0x11] = 0xac;
1543 params.regs[10+0x12] = 0xdf;
1544 params.regs[10+0x15] = 0xe7;
1545 params.regs[10+0x16] = 0x06;
1546 /* set vertical sync polarity to reflect aspect ratio */
1547 params.regs[9] = 0xe3;
1550 case M_BG320: case M_CG320: case M_BG640:
1551 case M_CG320_D: case M_CG640_E:
1552 case M_CG640x350: case M_ENH_CG640:
1553 case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1556 vga_load_state(adp, ¶ms);
1563 adp->va_mode = mode;
1564 info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1565 update_adapter_info(adp, &info);
1567 /* move hardware cursor out of the way */
1568 (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1571 #else /* VGA_NO_MODE_CHANGE */
1573 #endif /* VGA_NO_MODE_CHANGE */
1576 #ifndef VGA_NO_FONT_LOADING
1579 set_font_mode(video_adapter_t *adp, u_char *buf)
1585 /* save register values */
1586 if (adp->va_type == KD_VGA) {
1587 outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1588 outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1589 outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1590 outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1591 outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1592 inb(adp->va_crtc_addr + 6);
1593 outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1594 } else /* if (adp->va_type == KD_EGA) */ {
1596 * EGA cannot be read; copy parameters from the mode parameter
1599 mp = get_mode_param(adp->va_mode);
1600 buf[0] = mp[5 + 0x02 - 1];
1601 buf[1] = mp[5 + 0x04 - 1];
1602 buf[2] = mp[55 + 0x04];
1603 buf[3] = mp[55 + 0x05];
1604 buf[4] = mp[55 + 0x06];
1605 buf[5] = mp[35 + 0x10];
1608 /* setup vga for loading fonts */
1609 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1610 outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1611 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1612 outb(ATC, 0x20); /* enable palette */
1614 #if VGA_SLOW_IOACCESS
1615 #ifdef VGA_ALT_SEQACCESS
1616 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1618 outb(TSIDX, 0x02); outb(TSREG, 0x04);
1619 outb(TSIDX, 0x04); outb(TSREG, 0x07);
1620 #ifdef VGA_ALT_SEQACCESS
1621 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1623 outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
1624 outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
1625 outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
1626 #else /* VGA_SLOW_IOACCESS */
1627 #ifdef VGA_ALT_SEQACCESS
1628 outw(TSIDX, 0x0100);
1630 outw(TSIDX, 0x0402);
1631 outw(TSIDX, 0x0704);
1632 #ifdef VGA_ALT_SEQACCESS
1633 outw(TSIDX, 0x0300);
1635 outw(GDCIDX, 0x0204);
1636 outw(GDCIDX, 0x0005);
1637 outw(GDCIDX, 0x0406); /* addr = a0000, 64kb */
1638 #endif /* VGA_SLOW_IOACCESS */
1644 set_normal_mode(video_adapter_t *adp, u_char *buf)
1648 /* setup vga for normal operation mode again */
1649 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1650 outb(ATC, 0x10); outb(ATC, buf[5]);
1651 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1652 outb(ATC, 0x20); /* enable palette */
1654 #if VGA_SLOW_IOACCESS
1655 #ifdef VGA_ALT_SEQACCESS
1656 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1658 outb(TSIDX, 0x02); outb(TSREG, buf[0]);
1659 outb(TSIDX, 0x04); outb(TSREG, buf[1]);
1660 #ifdef VGA_ALT_SEQACCESS
1661 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1663 outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
1664 outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
1665 if (adp->va_crtc_addr == MONO_CRTC) {
1666 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
1668 outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1670 #else /* VGA_SLOW_IOACCESS */
1671 #ifdef VGA_ALT_SEQACCESS
1672 outw(TSIDX, 0x0100);
1674 outw(TSIDX, 0x0002 | (buf[0] << 8));
1675 outw(TSIDX, 0x0004 | (buf[1] << 8));
1676 #ifdef VGA_ALT_SEQACCESS
1677 outw(TSIDX, 0x0300);
1679 outw(GDCIDX, 0x0004 | (buf[2] << 8));
1680 outw(GDCIDX, 0x0005 | (buf[3] << 8));
1681 if (adp->va_crtc_addr == MONO_CRTC)
1682 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
1684 outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1685 #endif /* VGA_SLOW_IOACCESS */
1690 #endif /* VGA_NO_FONT_LOADING */
1694 * Read the font data in the requested font page from the video adapter.
1699 vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1702 #ifndef VGA_NO_FONT_LOADING
1703 u_char buf[PARAM_BUFSIZE];
1706 #ifdef VGA_ALT_SEQACCESS
1710 prologue(adp, V_ADP_FONT, ENODEV);
1712 if (fontsize < 14) {
1715 } else if (fontsize >= 32) {
1717 } else if (fontsize >= 16) {
1725 if (page < 0 || page >= 8)
1727 segment = FONT_BUF + 0x4000*page;
1731 #ifdef VGA_ALT_SEQACCESS
1732 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1734 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1735 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1736 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1737 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1742 set_font_mode(adp, buf);
1743 if (fontsize == 32) {
1744 bcopy_fromio(segment + ch*32, data, fontsize*count);
1746 for (c = ch; count > 0; ++c, --count) {
1747 bcopy_fromio(segment + c*32, data, fontsize);
1751 set_normal_mode(adp, buf);
1753 #ifdef VGA_ALT_SEQACCESS
1754 if (adp->va_type == KD_VGA) {
1756 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1757 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1758 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1764 #else /* VGA_NO_FONT_LOADING */
1766 #endif /* VGA_NO_FONT_LOADING */
1771 * Set the font data in the requested font page.
1772 * NOTE: it appears that some recent video adapters do not support
1773 * the font page other than 0... XXX
1778 vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1781 #ifndef VGA_NO_FONT_LOADING
1782 u_char buf[PARAM_BUFSIZE];
1785 #ifdef VGA_ALT_SEQACCESS
1789 prologue(adp, V_ADP_FONT, ENODEV);
1791 if (fontsize < 14) {
1794 } else if (fontsize >= 32) {
1796 } else if (fontsize >= 16) {
1804 if (page < 0 || page >= 8)
1806 segment = FONT_BUF + 0x4000*page;
1810 #ifdef VGA_ALT_SEQACCESS
1811 if (adp->va_type == KD_VGA) { /* what about EGA? XXX */
1813 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1814 outb(TSIDX, 0x01); val = inb(TSREG); /* disable screen */
1815 outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1816 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1821 set_font_mode(adp, buf);
1822 if (fontsize == 32) {
1823 bcopy_toio(data, segment + ch*32, fontsize*count);
1825 for (c = ch; count > 0; ++c, --count) {
1826 bcopy_toio(data, segment + c*32, fontsize);
1830 set_normal_mode(adp, buf);
1832 #ifdef VGA_ALT_SEQACCESS
1833 if (adp->va_type == KD_VGA) {
1835 outb(TSIDX, 0x00); outb(TSREG, 0x01);
1836 outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1837 outb(TSIDX, 0x00); outb(TSREG, 0x03);
1843 #else /* VGA_NO_FONT_LOADING */
1845 #endif /* VGA_NO_FONT_LOADING */
1850 * Activate the requested font page.
1851 * NOTE: it appears that some recent video adapters do not support
1852 * the font page other than 0... XXX
1857 vga_show_font(video_adapter_t *adp, int page)
1859 #ifndef VGA_NO_FONT_LOADING
1860 static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1862 prologue(adp, V_ADP_FONT, ENODEV);
1863 if (page < 0 || page >= 8)
1867 outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1871 #else /* VGA_NO_FONT_LOADING */
1873 #endif /* VGA_NO_FONT_LOADING */
1878 * Read DAC values. The values have expressed in 8 bits.
1883 vga_save_palette(video_adapter_t *adp, u_char *palette)
1887 prologue(adp, V_ADP_PALETTE, ENODEV);
1890 * We store 8 bit values in the palette buffer, while the standard
1891 * VGA has 6 bit DAC .
1893 outb(PALRADR, 0x00);
1894 for (i = 0; i < 256*3; ++i)
1895 palette[i] = inb(PALDATA) << 2;
1896 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1901 vga_save_palette2(video_adapter_t *adp, int base, int count,
1902 u_char *r, u_char *g, u_char *b)
1906 prologue(adp, V_ADP_PALETTE, ENODEV);
1908 outb(PALRADR, base);
1909 for (i = 0; i < count; ++i) {
1910 r[i] = inb(PALDATA) << 2;
1911 g[i] = inb(PALDATA) << 2;
1912 b[i] = inb(PALDATA) << 2;
1914 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1925 vga_load_palette(video_adapter_t *adp, const u_char *palette)
1929 prologue(adp, V_ADP_PALETTE, ENODEV);
1931 outb(PIXMASK, 0xff); /* no pixelmask */
1932 outb(PALWADR, 0x00);
1933 for (i = 0; i < 256*3; ++i)
1934 outb(PALDATA, palette[i] >> 2);
1935 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1936 outb(ATC, 0x20); /* enable palette */
1941 vga_load_palette2(video_adapter_t *adp, int base, int count,
1942 u_char *r, u_char *g, u_char *b)
1946 prologue(adp, V_ADP_PALETTE, ENODEV);
1948 outb(PIXMASK, 0xff); /* no pixelmask */
1949 outb(PALWADR, base);
1950 for (i = 0; i < count; ++i) {
1951 outb(PALDATA, r[i] >> 2);
1952 outb(PALDATA, g[i] >> 2);
1953 outb(PALDATA, b[i] >> 2);
1955 inb(adp->va_crtc_addr + 6); /* reset flip/flop */
1956 outb(ATC, 0x20); /* enable palette */
1962 * Change the border color.
1967 vga_set_border(video_adapter_t *adp, int color)
1969 prologue(adp, V_ADP_BORDER, ENODEV);
1971 switch (adp->va_type) {
1974 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
1975 outb(ATC, 0x31); outb(ATC, color & 0xff);
1978 outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
1990 * Read video register values.
1991 * NOTE: this function only reads the standard EGA/VGA registers.
1992 * any extra/extended registers of SVGA adapters are not saved.
1997 vga_save_state(video_adapter_t *adp, void *p, size_t size)
2005 /* return the required buffer size */
2006 prologue(adp, V_ADP_STATESAVE, 0);
2007 return sizeof(adp_state_t);
2009 prologue(adp, V_ADP_STATESAVE, ENODEV);
2010 if (size < sizeof(adp_state_t))
2014 ((adp_state_t *)p)->sig = V_STATE_SIG;
2015 buf = ((adp_state_t *)p)->regs;
2016 bzero(buf, V_MODE_PARAM_SIZE);
2017 crtc_addr = adp->va_crtc_addr;
2021 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
2022 for (i = 0, j = 5; i < 4; i++) {
2024 buf[j++] = inb(TSREG);
2026 buf[9] = inb(MISC + 10); /* dot-clock */
2027 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
2029 for (i = 0, j = 10; i < 25; i++) { /* crtc */
2031 buf[j++] = inb(crtc_addr + 1);
2033 for (i = 0, j = 35; i < 20; i++) { /* attribute ctrl */
2034 inb(crtc_addr + 6); /* reset flip-flop */
2036 buf[j++] = inb(ATC + 1);
2038 for (i = 0, j = 55; i < 9; i++) { /* graph data ctrl */
2040 buf[j++] = inb(GDCREG);
2042 inb(crtc_addr + 6); /* reset flip-flop */
2043 outb(ATC, 0x20); /* enable palette */
2048 if (vga_get_info(adp, adp->va_mode, &info) == 0) {
2049 if (info.vi_flags & V_INFO_GRAPHICS) {
2050 buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
2051 buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
2053 buf[0] = info.vi_width; /* COLS */
2054 buf[1] = info.vi_height - 1; /* ROWS */
2056 buf[2] = info.vi_cheight; /* POINTS */
2058 /* XXX: shouldn't be happening... */
2059 kprintf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
2060 adp->va_unit, adp->va_name);
2063 buf[0] = readb(BIOS_PADDRTOVADDR(0x44a)); /* COLS */
2064 buf[1] = readb(BIOS_PADDRTOVADDR(0x484)); /* ROWS */
2065 buf[2] = readb(BIOS_PADDRTOVADDR(0x485)); /* POINTS */
2066 buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
2067 buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
2075 * Set video registers at once.
2076 * NOTE: this function only updates the standard EGA/VGA registers.
2077 * any extra/extended registers of SVGA adapters are not changed.
2082 vga_load_state(video_adapter_t *adp, void *p)
2088 prologue(adp, V_ADP_STATELOAD, ENODEV);
2089 if (((adp_state_t *)p)->sig != V_STATE_SIG)
2092 buf = ((adp_state_t *)p)->regs;
2093 crtc_addr = adp->va_crtc_addr;
2096 dump_buffer(buf, V_MODE_PARAM_SIZE);
2101 outb(TSIDX, 0x00); outb(TSREG, 0x01); /* stop sequencer */
2102 for (i = 0; i < 4; ++i) { /* program sequencer */
2104 outb(TSREG, buf[i + 5]);
2106 outb(MISC, buf[9]); /* set dot-clock */
2107 outb(TSIDX, 0x00); outb(TSREG, 0x03); /* start sequencer */
2108 outb(crtc_addr, 0x11);
2109 outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
2110 for (i = 0; i < 25; ++i) { /* program crtc */
2112 outb(crtc_addr + 1, buf[i + 10]);
2114 inb(crtc_addr+6); /* reset flip-flop */
2115 for (i = 0; i < 20; ++i) { /* program attribute ctrl */
2117 outb(ATC, buf[i + 35]);
2119 for (i = 0; i < 9; ++i) { /* program graph data ctrl */
2121 outb(GDCREG, buf[i + 55]);
2123 inb(crtc_addr + 6); /* reset flip-flop */
2124 outb(ATC, 0x20); /* enable palette */
2126 #if notyet /* a temporary workaround for kernel panic, XXX */
2128 if (adp->va_unit == V_ADP_PRIMARY) {
2129 writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]); /* COLS */
2130 writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
2131 writeb(BIOS_PADDRTOVADDR(0x485), buf[2]); /* POINTS */
2133 writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
2134 writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
2137 #endif /* VGA_NO_BIOS */
2146 * Change the origin (window mapping) of the banked frame buffer.
2149 vga_set_origin(video_adapter_t *adp, off_t offset)
2152 * The standard video modes do not require window mapping;
2153 * always return error.
2160 * Read the position of the hardware text cursor.
2165 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
2172 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2176 outb(adp->va_crtc_addr, 14);
2177 off = inb(adp->va_crtc_addr + 1);
2178 outb(adp->va_crtc_addr, 15);
2179 off = (off << 8) | inb(adp->va_crtc_addr + 1);
2182 *row = off / adp->va_info.vi_width;
2183 *col = off % adp->va_info.vi_width;
2190 * Move the hardware text cursor. If col and row are both -1,
2191 * the cursor won't be shown.
2196 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
2203 if ((col == -1) && (row == -1)) {
2206 if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2208 off = row*adp->va_info.vi_width + col;
2212 outb(adp->va_crtc_addr, 14);
2213 outb(adp->va_crtc_addr + 1, off >> 8);
2214 outb(adp->va_crtc_addr, 15);
2215 outb(adp->va_crtc_addr + 1, off & 0x00ff);
2222 * set_hw_cursor_shape():
2223 * Change the shape of the hardware text cursor. If the height is
2224 * zero or negative, the cursor won't be shown.
2229 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2230 int celsize, int blink)
2236 switch (adp->va_type) {
2243 /* make the cursor invisible */
2244 outb(adp->va_crtc_addr, 10);
2245 outb(adp->va_crtc_addr + 1, 32);
2246 outb(adp->va_crtc_addr, 11);
2247 outb(adp->va_crtc_addr + 1, 0);
2249 outb(adp->va_crtc_addr, 10);
2250 outb(adp->va_crtc_addr + 1, celsize - base - height);
2251 outb(adp->va_crtc_addr, 11);
2252 outb(adp->va_crtc_addr + 1, celsize - base - 1);
2257 /* make the cursor invisible */
2258 outb(adp->va_crtc_addr, 10);
2259 outb(adp->va_crtc_addr + 1, celsize);
2260 outb(adp->va_crtc_addr, 11);
2261 outb(adp->va_crtc_addr + 1, 0);
2263 outb(adp->va_crtc_addr, 10);
2264 outb(adp->va_crtc_addr + 1, celsize - base - height);
2265 outb(adp->va_crtc_addr, 11);
2266 outb(adp->va_crtc_addr + 1, celsize - base);
2277 * Put the display in power save/power off mode.
2282 vga_blank_display(video_adapter_t *adp, int mode)
2287 switch (adp->va_type) {
2290 case V_DISPLAY_SUSPEND:
2291 case V_DISPLAY_STAND_BY:
2295 outb(TSREG, val | 0x20);
2296 outb(adp->va_crtc_addr, 0x17);
2297 val = inb(adp->va_crtc_addr + 1);
2298 outb(adp->va_crtc_addr + 1, val & ~0x80);
2300 case V_DISPLAY_BLANK:
2304 outb(TSREG, val | 0x20);
2310 outb(TSREG, val & 0xDF);
2311 outb(adp->va_crtc_addr, 0x17);
2312 val = inb(adp->va_crtc_addr + 1);
2313 outb(adp->va_crtc_addr + 1, val | 0x80);
2319 /* no support yet */
2325 case V_DISPLAY_SUSPEND:
2326 case V_DISPLAY_STAND_BY:
2327 case V_DISPLAY_BLANK:
2328 outb(adp->va_crtc_addr + 4, 0x25);
2331 outb(adp->va_crtc_addr + 4, 0x2d);
2339 case V_DISPLAY_SUSPEND:
2340 case V_DISPLAY_STAND_BY:
2341 case V_DISPLAY_BLANK:
2342 outb(adp->va_crtc_addr + 4, 0x21);
2345 outb(adp->va_crtc_addr + 4, 0x29);
2359 * Mmap frame buffer.
2364 vga_mmap_buf(video_adapter_t *adp, vm_offset_t offset, int prot)
2366 if (adp->va_info.vi_flags & V_INFO_LINEAR)
2370 kprintf("vga_mmap_buf(): window:0x%x, offset:0x%x\n",
2371 adp->va_info.vi_window, offset);
2374 /* XXX: is this correct? */
2375 if (offset > adp->va_window_size - PAGE_SIZE)
2379 return i386_btop(adp->va_info.vi_window + offset);
2383 #ifndef VGA_NO_MODE_CHANGE
2386 planar_fill(video_adapter_t *adp, int val)
2389 int at; /* position in the frame buffer */
2392 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
2393 outw(GDCIDX, 0x0003); /* data rotate/function select */
2394 outw(GDCIDX, 0x0f01); /* set/reset enable */
2395 outw(GDCIDX, 0xff08); /* bit mask */
2396 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
2398 length = adp->va_line_width*adp->va_info.vi_height;
2399 while (length > 0) {
2400 l = imin(length, adp->va_window_size);
2401 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2402 bzero_io(adp->va_window, l);
2406 outw(GDCIDX, 0x0000); /* set/reset */
2407 outw(GDCIDX, 0x0001); /* set/reset enable */
2411 packed_fill(video_adapter_t *adp, int val)
2414 int at; /* position in the frame buffer */
2418 length = adp->va_line_width*adp->va_info.vi_height;
2419 while (length > 0) {
2420 l = imin(length, adp->va_window_size);
2421 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2422 fill_io(val, adp->va_window, l);
2429 direct_fill(video_adapter_t *adp, int val)
2432 int at; /* position in the frame buffer */
2436 length = adp->va_line_width*adp->va_info.vi_height;
2437 while (length > 0) {
2438 l = imin(length, adp->va_window_size);
2439 (*vidsw[adp->va_index]->set_win_org)(adp, at);
2440 switch (adp->va_info.vi_pixel_size) {
2441 case sizeof(u_int16_t):
2442 fillw_io(val, adp->va_window, l/sizeof(u_int16_t));
2447 case sizeof(u_int32_t):
2448 filll_io(val, adp->va_window, l/sizeof(u_int32_t));
2457 vga_clear(video_adapter_t *adp)
2459 switch (adp->va_info.vi_mem_model) {
2460 case V_INFO_MM_TEXT:
2461 /* do nothing? XXX */
2463 case V_INFO_MM_PLANAR:
2464 planar_fill(adp, 0);
2466 case V_INFO_MM_PACKED:
2467 packed_fill(adp, 0);
2469 case V_INFO_MM_DIRECT:
2470 direct_fill(adp, 0);
2478 planar_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2483 int offset; /* offset within window */
2487 outw(GDCIDX, 0x0005); /* read mode 0, write mode 0 */
2488 outw(GDCIDX, 0x0003); /* data rotate/function select */
2489 outw(GDCIDX, 0x0f01); /* set/reset enable */
2490 outw(GDCIDX, 0xff08); /* bit mask */
2491 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
2493 banksize = adp->va_window_size;
2496 pos = adp->va_line_width*y + x/8;
2497 if (bank != pos/banksize) {
2498 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2499 bank = pos/banksize;
2501 offset = pos%banksize;
2502 bx = (x + cx)/8 - x/8;
2504 outw(GDCIDX, ((0xff00 >> (x % 8)) & 0xff00) | 0x08);
2505 writeb(adp->va_window + offset, 0);
2508 if (offset >= banksize) {
2510 ++bank; /* next bank */
2511 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2513 outw(GDCIDX, 0xff08); /* bit mask */
2516 l = imin(bx, banksize);
2517 bzero_io(adp->va_window + offset, l);
2520 if (offset >= banksize) {
2522 ++bank; /* next bank */
2523 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2527 outw(GDCIDX, (~(0xff00 >> ((x + cx) % 8)) & 0xff00) | 0x08);
2528 writeb(adp->va_window + offset, 0);
2530 if (offset >= banksize) {
2532 ++bank; /* next bank */
2533 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2535 outw(GDCIDX, 0xff08); /* bit mask */
2541 outw(GDCIDX, 0xff08); /* bit mask */
2542 outw(GDCIDX, 0x0000); /* set/reset */
2543 outw(GDCIDX, 0x0001); /* set/reset enable */
2547 packed_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2552 int offset; /* offset within window */
2555 banksize = adp->va_window_size;
2557 cx *= adp->va_info.vi_pixel_size;
2559 pos = adp->va_line_width*y + x*adp->va_info.vi_pixel_size;
2560 if (bank != pos/banksize) {
2561 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2562 bank = pos/banksize;
2564 offset = pos%banksize;
2565 end = imin(offset + cx, banksize);
2566 fill_io(val, adp->va_window + offset,
2567 (end - offset)/adp->va_info.vi_pixel_size);
2568 /* the line may cross the window boundary */
2569 if (offset + cx > banksize) {
2570 ++bank; /* next bank */
2571 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2572 end = offset + cx - banksize;
2573 fill_io(val, adp->va_window, end/adp->va_info.vi_pixel_size);
2581 direct_fill_rect16(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2586 int offset; /* offset within window */
2590 * XXX: the function assumes that banksize is a muliple of
2591 * sizeof(u_int16_t).
2593 banksize = adp->va_window_size;
2595 cx *= sizeof(u_int16_t);
2597 pos = adp->va_line_width*y + x*sizeof(u_int16_t);
2598 if (bank != pos/banksize) {
2599 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2600 bank = pos/banksize;
2602 offset = pos%banksize;
2603 end = imin(offset + cx, banksize);
2604 fillw_io(val, adp->va_window + offset,
2605 (end - offset)/sizeof(u_int16_t));
2606 /* the line may cross the window boundary */
2607 if (offset + cx > banksize) {
2608 ++bank; /* next bank */
2609 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2610 end = offset + cx - banksize;
2611 fillw_io(val, adp->va_window, end/sizeof(u_int16_t));
2619 direct_fill_rect24(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2624 int offset; /* offset within window */
2630 b[0] = val & 0x0000ff;
2631 b[1] = (val >> 8) & 0x0000ff;
2632 b[2] = (val >> 16) & 0x0000ff;
2633 banksize = adp->va_window_size;
2637 pos = adp->va_line_width*y + x*3;
2638 if (bank != pos/banksize) {
2639 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2640 bank = pos/banksize;
2642 offset = pos%banksize;
2643 end = imin(offset + cx, banksize);
2644 for (i = 0, j = offset; j < end; i = (++i)%3, ++j) {
2645 writeb(adp->va_window + j, b[i]);
2647 /* the line may cross the window boundary */
2648 if (offset + cx >= banksize) {
2649 ++bank; /* next bank */
2650 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2652 end = offset + cx - banksize;
2653 for (; j < end; i = (++i)%3, ++j) {
2654 writeb(adp->va_window + j, b[i]);
2663 direct_fill_rect32(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2668 int offset; /* offset within window */
2672 * XXX: the function assumes that banksize is a muliple of
2673 * sizeof(u_int32_t).
2675 banksize = adp->va_window_size;
2677 cx *= sizeof(u_int32_t);
2679 pos = adp->va_line_width*y + x*sizeof(u_int32_t);
2680 if (bank != pos/banksize) {
2681 (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2682 bank = pos/banksize;
2684 offset = pos%banksize;
2685 end = imin(offset + cx, banksize);
2686 filll_io(val, adp->va_window + offset,
2687 (end - offset)/sizeof(u_int32_t));
2688 /* the line may cross the window boundary */
2689 if (offset + cx > banksize) {
2690 ++bank; /* next bank */
2691 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2692 end = offset + cx - banksize;
2693 filll_io(val, adp->va_window, end/sizeof(u_int32_t));
2701 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2703 switch (adp->va_info.vi_mem_model) {
2704 case V_INFO_MM_TEXT:
2705 /* do nothing? XXX */
2707 case V_INFO_MM_PLANAR:
2708 planar_fill_rect(adp, val, x, y, cx, cy);
2710 case V_INFO_MM_PACKED:
2711 packed_fill_rect(adp, val, x, y, cx, cy);
2713 case V_INFO_MM_DIRECT:
2714 switch (adp->va_info.vi_pixel_size) {
2715 case sizeof(u_int16_t):
2716 direct_fill_rect16(adp, val, x, y, cx, cy);
2719 direct_fill_rect24(adp, val, x, y, cx, cy);
2721 case sizeof(u_int32_t):
2722 direct_fill_rect32(adp, val, x, y, cx, cy);
2731 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2738 vga_bitblt(video_adapter_t *adp,...)
2744 #endif /* !VGA_NO_MODE_CHANGE */
2747 get_palette(video_adapter_t *adp, int base, int count,
2748 u_char *red, u_char *green, u_char *blue, u_char *trans)
2754 if (count < 0 || base < 0 || count > 256 || base > 256 ||
2758 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2761 if (vga_save_palette2(adp, base, count, r, g, b)) {
2765 copyout(r, red, count);
2766 copyout(g, green, count);
2767 copyout(b, blue, count);
2768 if (trans != NULL) {
2770 copyout(r, trans, count);
2778 set_palette(video_adapter_t *adp, int base, int count,
2779 u_char *red, u_char *green, u_char *blue, u_char *trans)
2786 if (count < 0 || base < 0 || count > 256 || base > 256 ||
2790 r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2793 err = copyin(red, r, count);
2795 err = copyin(green, g, count);
2797 err = copyin(blue, b, count);
2799 err = vga_load_palette2(adp, base, count, r, g, b);
2802 return (err ? ENODEV : 0);
2806 vga_dev_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
2809 case FBIO_GETWINORG: /* get frame buffer window origin */
2813 case FBIO_SETWINORG: /* set frame buffer window origin */
2816 case FBIO_SETDISPSTART: /* set display start address */
2817 return (set_display_start(adp,
2818 ((video_display_start_t *)arg)->x,
2819 ((video_display_start_t *)arg)->y)
2822 case FBIO_SETLINEWIDTH: /* set scan line length in pixel */
2823 return (set_line_length(adp, *(u_int *)arg) ? ENODEV : 0);
2825 case FBIO_GETPALETTE: /* get color palette */
2826 return get_palette(adp, ((video_color_palette_t *)arg)->index,
2827 ((video_color_palette_t *)arg)->count,
2828 ((video_color_palette_t *)arg)->red,
2829 ((video_color_palette_t *)arg)->green,
2830 ((video_color_palette_t *)arg)->blue,
2831 ((video_color_palette_t *)arg)->transparent);
2833 case FBIO_SETPALETTE: /* set color palette */
2834 return set_palette(adp, ((video_color_palette_t *)arg)->index,
2835 ((video_color_palette_t *)arg)->count,
2836 ((video_color_palette_t *)arg)->red,
2837 ((video_color_palette_t *)arg)->green,
2838 ((video_color_palette_t *)arg)->blue,
2839 ((video_color_palette_t *)arg)->transparent);
2841 case FBIOGTYPE: /* get frame buffer type info. */
2842 ((struct fbtype *)arg)->fb_type = fb_type(adp->va_type);
2843 ((struct fbtype *)arg)->fb_height = adp->va_info.vi_height;
2844 ((struct fbtype *)arg)->fb_width = adp->va_info.vi_width;
2845 ((struct fbtype *)arg)->fb_depth = adp->va_info.vi_depth;
2846 if ((adp->va_info.vi_depth <= 1) || (adp->va_info.vi_depth > 8))
2847 ((struct fbtype *)arg)->fb_cmsize = 0;
2849 ((struct fbtype *)arg)->fb_cmsize = 1 << adp->va_info.vi_depth;
2850 ((struct fbtype *)arg)->fb_size = adp->va_buffer_size;
2853 case FBIOGETCMAP: /* get color palette */
2854 return get_palette(adp, ((struct fbcmap *)arg)->index,
2855 ((struct fbcmap *)arg)->count,
2856 ((struct fbcmap *)arg)->red,
2857 ((struct fbcmap *)arg)->green,
2858 ((struct fbcmap *)arg)->blue, NULL);
2860 case FBIOPUTCMAP: /* set color palette */
2861 return set_palette(adp, ((struct fbcmap *)arg)->index,
2862 ((struct fbcmap *)arg)->count,
2863 ((struct fbcmap *)arg)->red,
2864 ((struct fbcmap *)arg)->green,
2865 ((struct fbcmap *)arg)->blue, NULL);
2868 return fb_commonioctl(adp, cmd, arg);
2873 dump_buffer(u_char *buf, size_t len)
2877 for(i = 0; i < len;) {
2878 kprintf("%02x ", buf[i]);
2879 if ((++i % 16) == 0)
2886 * Print some information about the video adapter and video modes,
2887 * with requested level of details.
2892 vga_diag(video_adapter_t *adp, int level)
2905 kprintf("vga: RTC equip. code:0x%02x, DCC code:0x%02x\n",
2906 rtcin(RTC_EQUIPMENT), readb(BIOS_PADDRTOVADDR(0x488)));
2907 kprintf("vga: CRTC:0x%x, video option:0x%02x, ",
2908 readw(BIOS_PADDRTOVADDR(0x463)),
2909 readb(BIOS_PADDRTOVADDR(0x487)));
2910 kprintf("rows:%d, cols:%d, font height:%d\n",
2911 readb(BIOS_PADDRTOVADDR(0x44a)),
2912 readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2913 readb(BIOS_PADDRTOVADDR(0x485)));
2914 #endif /* VGA_NO_BIOS */
2915 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2916 kprintf("vga: param table EGA/VGA:%p", video_mode_ptr);
2917 kprintf(", CGA/MDA:%p\n", video_mode_ptr2);
2918 kprintf("vga: rows_offset:%d\n", rows_offset);
2920 #endif /* FB_DEBUG > 1 */
2922 fb_dump_adp_info(VGA_DRIVER_NAME, adp, level);
2925 if (adp->va_flags & V_ADP_MODECHANGE) {
2926 for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2927 if (bios_vmode[i].vi_mode == NA)
2929 if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2931 fb_dump_mode_info(VGA_DRIVER_NAME, adp, &bios_vmode[i], level);
2934 vga_get_info(adp, adp->va_initial_mode, &info); /* shouldn't fail */
2935 fb_dump_mode_info(VGA_DRIVER_NAME, adp, &info, level);
2937 #endif /* FB_DEBUG > 1 */
2939 if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
2941 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2942 if (video_mode_ptr == NULL)
2943 kprintf("vga%d: %s: WARNING: video mode switching is not "
2944 "fully supported on this adapter\n",
2945 adp->va_unit, adp->va_name);
2950 if (adp->va_type == KD_VGA) {
2951 kprintf("VGA parameters upon power-up\n");
2952 dump_buffer(adpstate.regs, sizeof(adpstate.regs));
2953 kprintf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2954 dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
2957 mp = get_mode_param(adp->va_initial_mode);
2958 if (mp == NULL) /* this shouldn't be happening */
2960 kprintf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2961 dump_buffer(mp, V_MODE_PARAM_SIZE);