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.25 2008/01/19 08:50:12 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>
50 #include <machine/pc/bios.h>
54 #include <vm/vm_param.h>
64 /* machine/pc/bios.h has got too much i386-specific stuff in it */
65 #ifndef BIOS_PADDRTOVADDR
66 #define BIOS_PADDRTOVADDR(x) (((x) - ISA_HOLE_START) + atdevbase)
69 vga_probe_unit(int unit
, video_adapter_t
*buf
, int flags
)
75 sw
= vid_get_switch(VGA_DRIVER_NAME
);
78 error
= (*sw
->probe
)(unit
, &adp
, NULL
, flags
);
81 bcopy(adp
, buf
, sizeof(*buf
));
86 vga_attach_unit(int unit
, vga_softc_t
*sc
, int flags
)
91 sw
= vid_get_switch(VGA_DRIVER_NAME
);
95 error
= (*sw
->probe
)(unit
, &sc
->adp
, NULL
, flags
);
98 return (*sw
->init
)(unit
, sc
->adp
, flags
);
101 /* cdev driver functions */
103 #ifdef FB_INSTALL_CDEV
108 vga_open(cdev_t dev
, vga_softc_t
*sc
, int flag
, int mode
, struct ucred
*cred
)
112 if (mode
& (O_CREAT
| O_APPEND
| O_TRUNC
))
115 return genfbopen(&sc
->gensc
, sc
->adp
, flag
, mode
, cred
);
119 vga_close(cdev_t dev
, vga_softc_t
*sc
, int flag
, int mode
)
121 return genfbclose(&sc
->gensc
, sc
->adp
, flag
, mode
);
125 vga_read(cdev_t dev
, vga_softc_t
*sc
, struct uio
*uio
, int flag
)
127 return genfbread(&sc
->gensc
, sc
->adp
, uio
, flag
);
131 vga_write(cdev_t dev
, vga_softc_t
*sc
, struct uio
*uio
, int flag
)
133 return genfbread(&sc
->gensc
, sc
->adp
, uio
, flag
);
137 vga_ioctl(cdev_t dev
, vga_softc_t
*sc
, u_long cmd
, caddr_t arg
, int flag
,
140 return genfbioctl(&sc
->gensc
, sc
->adp
, cmd
, arg
, flag
, cred
);
144 vga_mmap(cdev_t dev
, vga_softc_t
*sc
, vm_offset_t offset
, int prot
)
146 return genfbmmap(&sc
->gensc
, sc
->adp
, offset
, prot
);
149 #endif /* FB_INSTALL_CDEV */
153 #define probe_done(adp) ((adp)->va_flags & V_ADP_PROBED)
154 #define init_done(adp) ((adp)->va_flags & V_ADP_INITIALIZED)
155 #define config_done(adp) ((adp)->va_flags & V_ADP_REGISTERED)
158 #define V_MODE_MAP_SIZE (M_VGA_CG320 + 1)
159 #define V_MODE_PARAM_SIZE 64
161 /* video adapter state buffer */
164 #define V_STATE_SIG 0x736f6962
165 u_char regs
[V_MODE_PARAM_SIZE
];
167 typedef struct adp_state adp_state_t
;
170 * NOTE: `va_window' should have a virtual address, but is initialized
171 * with a physical address in the following table, as verify_adapter()
172 * will perform address conversion at run-time.
174 static video_adapter_t biosadapter
= {
175 0, KD_VGA
, VGA_DRIVER_NAME
, 0, 0, V_ADP_COLOR
, IO_VGA
, 32,
176 EGA_BUF_BASE
, EGA_BUF_SIZE
, CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
,
177 0, 0, 0, M_VGA_C80x25
, M_C80x25
, M_VGA_C80x25
180 /* video driver declarations */
181 static int vga_configure(int flags
);
182 int (*vga_sub_configure
)(int flags
);
184 static int vga_nop(void);
186 static int vga_error(void);
187 static vi_probe_t vga_probe
;
188 static vi_init_t vga_init
;
189 static vi_get_info_t vga_get_info
;
190 static vi_query_mode_t vga_query_mode
;
191 static vi_set_mode_t vga_set_mode
;
192 static vi_save_font_t vga_save_font
;
193 static vi_load_font_t vga_load_font
;
194 static vi_show_font_t vga_show_font
;
195 static vi_save_palette_t vga_save_palette
;
196 static vi_load_palette_t vga_load_palette
;
197 static vi_set_border_t vga_set_border
;
198 static vi_save_state_t vga_save_state
;
199 static vi_load_state_t vga_load_state
;
200 static vi_set_win_org_t vga_set_origin
;
201 static vi_read_hw_cursor_t vga_read_hw_cursor
;
202 static vi_set_hw_cursor_t vga_set_hw_cursor
;
203 static vi_set_hw_cursor_shape_t vga_set_hw_cursor_shape
;
204 static vi_blank_display_t vga_blank_display
;
205 static vi_mmap_t vga_mmap_buf
;
206 static vi_ioctl_t vga_dev_ioctl
;
207 #ifndef VGA_NO_MODE_CHANGE
208 static vi_clear_t vga_clear
;
209 static vi_fill_rect_t vga_fill_rect
;
210 static vi_bitblt_t vga_bitblt
;
211 #else /* VGA_NO_MODE_CHANGE */
212 #define vga_clear (vi_clear_t *)vga_error
213 #define vga_fill_rect (vi_fill_rect_t *)vga_error
214 #define vga_bitblt (vi_bitblt_t *)vga_error
216 static vi_diag_t vga_diag
;
218 static video_switch_t vgavidsw
= {
235 vga_set_hw_cursor_shape
,
247 VIDEO_DRIVER(vga
, vgavidsw
, vga_configure
);
249 /* VGA BIOS standard video modes */
253 static video_info_t bios_vmode
[] = {
255 { M_B40x25
, V_INFO_COLOR
, 40, 25, 8, 8, 2, 1,
256 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
257 { M_C40x25
, V_INFO_COLOR
, 40, 25, 8, 8, 4, 1,
258 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
259 { M_B80x25
, V_INFO_COLOR
, 80, 25, 8, 8, 2, 1,
260 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
261 { M_C80x25
, V_INFO_COLOR
, 80, 25, 8, 8, 4, 1,
262 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
264 { M_ENH_B40x25
, V_INFO_COLOR
, 40, 25, 8, 14, 2, 1,
265 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
266 { M_ENH_C40x25
, V_INFO_COLOR
, 40, 25, 8, 14, 4, 1,
267 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
268 { M_ENH_B80x25
, V_INFO_COLOR
, 80, 25, 8, 14, 2, 1,
269 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
270 { M_ENH_C80x25
, V_INFO_COLOR
, 80, 25, 8, 14, 4, 1,
271 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
273 { M_VGA_C40x25
, V_INFO_COLOR
, 40, 25, 8, 16, 4, 1,
274 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
275 { M_VGA_M80x25
, 0, 80, 25, 8, 16, 2, 1,
276 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
277 { M_VGA_C80x25
, V_INFO_COLOR
, 80, 25, 8, 16, 4, 1,
278 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
280 { M_EGAMONO80x25
, 0, 80, 25, 8, 14, 2, 1,
281 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
283 { M_ENH_B80x43
, 0, 80, 43, 8, 8, 2, 1,
284 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
285 { M_ENH_C80x43
, V_INFO_COLOR
, 80, 43, 8, 8, 4, 1,
286 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
288 { M_VGA_M80x30
, 0, 80, 30, 8, 16, 2, 1,
289 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
290 { M_VGA_C80x30
, V_INFO_COLOR
, 80, 30, 8, 16, 4, 1,
291 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
292 { M_VGA_M80x50
, 0, 80, 50, 8, 8, 2, 1,
293 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
294 { M_VGA_C80x50
, V_INFO_COLOR
, 80, 50, 8, 8, 4, 1,
295 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
296 { M_VGA_M80x60
, 0, 80, 60, 8, 8, 2, 1,
297 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
298 { M_VGA_C80x60
, V_INFO_COLOR
, 80, 60, 8, 8, 4, 1,
299 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
301 #ifndef VGA_NO_MODE_CHANGE
304 { M_VGA_M90x25
, 0, 90, 25, 8, 16, 2, 1,
305 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
306 { M_VGA_C90x25
, V_INFO_COLOR
, 90, 25, 8, 16, 4, 1,
307 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
308 { M_VGA_M90x30
, 0, 90, 30, 8, 16, 2, 1,
309 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
310 { M_VGA_C90x30
, V_INFO_COLOR
, 90, 30, 8, 16, 4, 1,
311 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
312 { M_VGA_M90x43
, 0, 90, 43, 8, 8, 2, 1,
313 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
314 { M_VGA_C90x43
, V_INFO_COLOR
, 90, 43, 8, 8, 4, 1,
315 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
316 { M_VGA_M90x50
, 0, 90, 50, 8, 8, 2, 1,
317 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
318 { M_VGA_C90x50
, V_INFO_COLOR
, 90, 50, 8, 8, 4, 1,
319 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
320 { M_VGA_M90x60
, 0, 90, 60, 8, 8, 2, 1,
321 MDA_BUF_BASE
, MDA_BUF_SIZE
, MDA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
322 { M_VGA_C90x60
, V_INFO_COLOR
, 90, 60, 8, 8, 4, 1,
323 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_TEXT
},
324 #endif /* VGA_WIDTH90 */
327 { M_BG320
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 320, 200, 8, 8, 2, 1,
328 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_CGA
},
329 { M_CG320
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 320, 200, 8, 8, 2, 1,
330 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_CGA
},
331 { M_BG640
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 200, 8, 8, 1, 1,
332 CGA_BUF_BASE
, CGA_BUF_SIZE
, CGA_BUF_SIZE
, 0, 0, V_INFO_MM_CGA
},
334 { M_CG320_D
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 320, 200, 8, 8, 4, 4,
335 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0,
337 { M_CG640_E
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 200, 8, 8, 4, 4,
338 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
340 { M_EGAMONOAPA
, V_INFO_GRAPHICS
, 640, 350, 8, 14, 4, 4,
341 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, 64*1024, 0, 0 ,
343 { M_ENHMONOAPA2
,V_INFO_GRAPHICS
, 640, 350, 8, 14, 4, 4,
344 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
346 { M_CG640x350
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 350, 8, 14, 2, 2,
347 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
349 { M_ENH_CG640
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 350, 8, 14, 4, 4,
350 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
353 { M_BG640x480
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 480, 8, 16, 4, 4,
354 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
356 { M_CG640x480
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 640, 480, 8, 16, 4, 4,
357 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0 ,
359 { M_VGA_CG320
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 320, 200, 8, 8, 8, 1,
360 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0,
361 V_INFO_MM_PACKED
, 1 },
362 { M_VGA_MODEX
, V_INFO_COLOR
| V_INFO_GRAPHICS
, 320, 240, 8, 8, 8, 4,
363 GRAPHICS_BUF_BASE
, GRAPHICS_BUF_SIZE
, GRAPHICS_BUF_SIZE
, 0, 0,
365 #endif /* VGA_NO_MODE_CHANGE */
370 static int vga_init_done
= FALSE
;
371 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
372 static u_char
*video_mode_ptr
= NULL
;
374 static u_char
*mode_map
[V_MODE_MAP_SIZE
];
375 static adp_state_t adpstate
;
376 static adp_state_t adpstate2
;
377 static int rows_offset
= 1;
379 /* local macros and functions */
380 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
382 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
383 static void map_mode_table(u_char
**, u_char
*, int);
385 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
386 static int map_mode_num(int);
388 static int map_bios_mode_num(int);
389 static u_char
*get_mode_param(int);
390 static int verify_adapter(video_adapter_t
*);
391 static void update_adapter_info(video_adapter_t
*, video_info_t
*);
392 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
393 #define COMP_IDENTICAL 0
394 #define COMP_SIMILAR 1
395 #define COMP_DIFFERENT 2
396 static int comp_adpregs(u_char
*, u_char
*);
398 static int probe_adapters(void);
399 static int set_line_length(video_adapter_t
*, int);
400 static int set_display_start(video_adapter_t
*, int, int);
402 #ifndef VGA_NO_MODE_CHANGE
404 static void set_width90(adp_state_t
*);
406 #endif /* !VGA_NO_MODE_CHANGE */
408 #ifndef VGA_NO_FONT_LOADING
409 #define PARAM_BUFSIZE 6
410 static void set_font_mode(video_adapter_t
*, u_char
*);
411 static void set_normal_mode(video_adapter_t
*, u_char
*);
414 #ifndef VGA_NO_MODE_CHANGE
415 static void filll_io(int, vm_offset_t
, size_t);
416 static void planar_fill(video_adapter_t
*, int);
417 static void packed_fill(video_adapter_t
*, int);
418 static void direct_fill(video_adapter_t
*, int);
420 static void planar_fill_rect(video_adapter_t
*, int, int, int, int, int);
421 static void packed_fill_rect(video_adapter_t
*, int, int, int, int, int);
422 static void direct_fill_rect16(video_adapter_t
*, int, int, int, int, int);
423 static void direct_fill_rect24(video_adapter_t
*, int, int, int, int, int);
424 static void direct_fill_rect32(video_adapter_t
*, int, int, int, int, int);
426 #endif /* !VGA_NO_MODE_CHANGE */
428 static void dump_buffer(u_char
*, size_t);
430 #define ISMAPPED(pa, width) \
431 (((pa) <= (u_long)0x1000 - (width)) \
432 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
434 #define prologue(adp, flag, err) \
435 if (!vga_init_done || !((adp)->va_flags & (flag))) \
438 /* a backdoor for the console driver */
440 vga_configure(int flags
)
443 if (probe_done(&biosadapter
)) {
444 biosadapter
.va_flags
|= V_ADP_INITIALIZED
;
445 if (!config_done(&biosadapter
) && !(vid_register(&biosadapter
) < 0))
446 biosadapter
.va_flags
|= V_ADP_REGISTERED
;
448 if (vga_sub_configure
!= NULL
)
449 (*vga_sub_configure
)(flags
);
454 /* local subroutines */
456 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
457 /* construct the mode parameter map */
459 map_mode_table(u_char
*map
[], u_char
*table
, int max
)
463 for(i
= 0; i
< max
; ++i
)
464 map
[i
] = table
+ i
*V_MODE_PARAM_SIZE
;
465 for(; i
< V_MODE_MAP_SIZE
; ++i
)
468 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
470 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
471 /* map the non-standard video mode to a known mode number */
473 map_mode_num(int mode
)
479 { M_ENH_B80x43
, M_ENH_B80x25
},
480 { M_ENH_C80x43
, M_ENH_C80x25
},
481 { M_VGA_M80x30
, M_VGA_M80x25
},
482 { M_VGA_C80x30
, M_VGA_C80x25
},
483 { M_VGA_M80x50
, M_VGA_M80x25
},
484 { M_VGA_C80x50
, M_VGA_C80x25
},
485 { M_VGA_M80x60
, M_VGA_M80x25
},
486 { M_VGA_C80x60
, M_VGA_C80x25
},
488 { M_VGA_M90x25
, M_VGA_M80x25
},
489 { M_VGA_C90x25
, M_VGA_C80x25
},
490 { M_VGA_M90x30
, M_VGA_M80x25
},
491 { M_VGA_C90x30
, M_VGA_C80x25
},
492 { M_VGA_M90x43
, M_ENH_B80x25
},
493 { M_VGA_C90x43
, M_ENH_C80x25
},
494 { M_VGA_M90x50
, M_VGA_M80x25
},
495 { M_VGA_C90x50
, M_VGA_C80x25
},
496 { M_VGA_M90x60
, M_VGA_M80x25
},
497 { M_VGA_C90x60
, M_VGA_C80x25
},
499 { M_VGA_MODEX
, M_VGA_CG320
},
503 for (i
= 0; i
< sizeof(mode_map
)/sizeof(mode_map
[0]); ++i
) {
504 if (mode_map
[i
].from
== mode
)
505 return mode_map
[i
].to
;
509 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
511 /* turn the BIOS video number into our video mode number */
513 map_bios_mode_num(int bios_mode
)
515 static int vga_modes
[20] = {
516 M_VGA_C40x25
, M_VGA_C40x25
, /* 0, 1 */
517 M_VGA_C80x25
, M_VGA_C80x25
, /* 2, 3 */
520 M_VGA_M80x25
, /* 7 */
526 M_BG640x480
, M_CG640x480
,
530 if (bios_mode
< sizeof(vga_modes
)/sizeof(vga_modes
[0]))
531 return vga_modes
[bios_mode
];
536 /* look up a parameter table entry */
538 get_mode_param(int mode
)
540 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
541 if (mode
>= V_MODE_MAP_SIZE
)
542 mode
= map_mode_num(mode
);
544 if ((mode
>= 0) && (mode
< V_MODE_MAP_SIZE
))
545 return mode_map
[mode
];
551 verify_adapter(video_adapter_t
*adp
)
555 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
559 buf
= BIOS_PADDRTOVADDR(adp
->va_window
);
562 if (readw(buf
) != 0xA55A)
570 adp
->va_flags
|= V_ADP_STATELOAD
| V_ADP_STATESAVE
| V_ADP_PALETTE
|
573 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
574 /* get the BIOS video mode pointer */
575 p
= *(u_int32_t
*)BIOS_PADDRTOVADDR(0x4a8);
576 p
= BIOS_SADDRTOLADDR(p
);
577 if (ISMAPPED(p
, sizeof(u_int32_t
))) {
578 p
= *(u_int32_t
*)BIOS_PADDRTOVADDR(p
);
579 p
= BIOS_SADDRTOLADDR(p
);
580 if (ISMAPPED(p
, V_MODE_PARAM_SIZE
))
581 video_mode_ptr
= (u_char
*)BIOS_PADDRTOVADDR(p
);
589 update_adapter_info(video_adapter_t
*adp
, video_info_t
*info
)
591 adp
->va_flags
|= V_ADP_COLOR
;
592 adp
->va_window
= BIOS_PADDRTOVADDR(info
->vi_window
);
593 adp
->va_window_size
= info
->vi_window_size
;
594 adp
->va_window_gran
= info
->vi_window_gran
;
595 adp
->va_window_orig
= 0;
597 adp
->va_buffer
= info
->vi_buffer
;
598 adp
->va_buffer_size
= info
->vi_buffer_size
;
599 if (info
->vi_mem_model
== V_INFO_MM_VGAX
) {
600 adp
->va_line_width
= info
->vi_width
/2;
601 } else if (info
->vi_flags
& V_INFO_GRAPHICS
) {
602 switch (info
->vi_depth
/info
->vi_planes
) {
604 adp
->va_line_width
= info
->vi_width
/8;
607 adp
->va_line_width
= info
->vi_width
/4;
610 adp
->va_line_width
= info
->vi_width
/2;
613 default: /* shouldn't happen */
614 adp
->va_line_width
= info
->vi_width
;
618 adp
->va_line_width
= info
->vi_width
;
620 adp
->va_disp_start
.x
= 0;
621 adp
->va_disp_start
.y
= 0;
622 bcopy(info
, &adp
->va_info
, sizeof(adp
->va_info
));
625 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
626 /* compare two parameter table entries */
628 comp_adpregs(u_char
*buf1
, u_char
*buf2
)
632 } params
[V_MODE_PARAM_SIZE
] = {
633 {0xff}, {0x00}, {0xff}, /* COLS}, ROWS}, POINTS */
634 {0x00}, {0x00}, /* page length */
635 {0xfe}, {0xff}, {0xff}, {0xff}, /* sequencer registers */
636 {0xf3}, /* misc register */
637 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff}, /* CRTC */
638 {0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
639 {0x00}, {0x00}, {0x00}, {0x00}, {0x00},
640 {0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
641 {0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
642 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* attribute controller regs */
643 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
644 {0xff}, {0xff}, {0xff}, {0xff}, {0xff},
645 {0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
646 {0xff}, {0xff}, {0xff}, {0xff}, {0xff}, /* GDC register */
647 {0xff}, {0xff}, {0xff}, {0xff},
649 int identical
= TRUE
;
652 if ((buf1
== NULL
) || (buf2
== NULL
))
653 return COMP_DIFFERENT
;
655 for (i
= 0; i
< sizeof(params
)/sizeof(params
[0]); ++i
) {
656 if (params
[i
].mask
== 0) /* don't care */
658 if ((buf1
[i
] & params
[i
].mask
) != (buf2
[i
] & params
[i
].mask
))
659 return COMP_DIFFERENT
;
660 if (buf1
[i
] != buf2
[i
])
663 return (identical
) ? COMP_IDENTICAL
: COMP_SIMILAR
;
665 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
667 /* probe video adapters and return the number of detected adapters */
671 video_adapter_t
*adp
;
673 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
678 /* do this test only once */
681 vga_init_done
= TRUE
;
684 * Check BIOS data area.
685 * The VGA BIOS has more sophisticated mechanism and has this
686 * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
687 * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
691 * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
692 * copy of RTC_EQUIPMENT. Bits 4 and 5 of ETC_EQUIPMENT are
693 * zeros for VGA. However, VGA BIOS sets these bits in
694 * BIOSDATA_EQUIPMENT according to the monitor type detected.
697 if ((readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f) != 0x09)
699 #endif /* VGA_NO_BIOS */
701 if (verify_adapter(&biosadapter
) != 0)
704 biosadapter
.va_flags
|= V_ADP_PROBED
;
706 biosadapter
.va_initial_bios_mode
= readb(BIOS_PADDRTOVADDR(0x449));
707 biosadapter
.va_mode
= biosadapter
.va_initial_mode
=
708 map_bios_mode_num(biosadapter
.va_initial_bios_mode
);
712 * Ensure a zero start address. This is mainly to recover after
713 * switching from pcvt using userconfig(). The registers are w/o
714 * for old hardware so it's too hard to relocate the active screen
716 * This must be done before vga_save_state() for VGA.
723 /* the video mode parameter table in VGA BIOS */
724 /* NOTE: there can be only one VGA recognized by the video BIOS.
727 bzero(mode_map
, sizeof(mode_map
));
728 vga_save_state(adp
, &adpstate
, sizeof(adpstate
));
729 for(i
= 0; i
< 16; i
++)
730 adp
->va_palette_regs
[i
] = adpstate
.regs
[35 + i
];
731 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
732 mode_map
[adp
->va_initial_mode
] = adpstate
.regs
;
734 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
735 if (video_mode_ptr
== NULL
) {
736 mode_map
[adp
->va_initial_mode
] = adpstate
.regs
;
739 /* discard the table if we are not familiar with it... */
740 map_mode_table(mode_map
, video_mode_ptr
, M_VGA_CG320
+ 1);
741 mp
= get_mode_param(adp
->va_initial_mode
);
743 bcopy(mp
, adpstate2
.regs
, sizeof(adpstate2
.regs
));
744 switch (comp_adpregs(adpstate
.regs
, mp
)) {
747 * OK, this parameter table looks reasonably familiar
751 * This is a kludge for Toshiba DynaBook SS433
752 * whose BIOS video mode table entry has the actual #
753 * of rows at the offset 1; BIOSes from other
754 * manufacturers store the # of rows - 1 there. XXX
756 rows_offset
= adpstate
.regs
[1] + 1 - mp
[1];
761 * Not exactly the same, but similar enough to be
762 * trusted. However, use the saved register values
763 * for the initial mode and other modes which are
764 * based on the initial mode.
766 mode_map
[adp
->va_initial_mode
] = adpstate
.regs
;
767 rows_offset
= adpstate
.regs
[1] + 1 - mp
[1];
768 adpstate
.regs
[1] -= rows_offset
- 1;
774 * Don't use the paramter table in BIOS. It doesn't
775 * look familiar to us. Video mode switching is allowed
776 * only if the new mode is the same as or based on
779 video_mode_ptr
= NULL
;
780 bzero(mode_map
, sizeof(mode_map
));
781 mode_map
[adp
->va_initial_mode
] = adpstate
.regs
;
786 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
788 #ifndef VGA_NO_MODE_CHANGE
789 adp
->va_flags
|= V_ADP_MODECHANGE
;
791 #ifndef VGA_NO_FONT_LOADING
792 adp
->va_flags
|= V_ADP_FONT
;
795 /* XXX remove conflicting modes */
796 for (i
= 0; i
< M_VGA_CG320
; i
++) {
797 if (vga_get_info(&biosadapter
, i
, &info
))
799 if ((info
.vi_flags
& V_INFO_COLOR
) != V_ADP_COLOR
)
804 vga_get_info(&biosadapter
, biosadapter
.va_initial_mode
, &info
);
805 info
.vi_flags
&= ~V_INFO_LINEAR
; /* XXX */
806 update_adapter_info(&biosadapter
, &info
);
809 * XXX: we should verify the following values for the primary adapter...
810 * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
811 * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
813 * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
814 * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
815 * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
816 * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
822 /* set the scan line length in pixel */
824 set_line_length(video_adapter_t
*adp
, int pixel
)
827 int ppw
; /* pixels per word */
828 int bpl
; /* bytes per line */
831 mp
= get_mode_param(adp
->va_mode
);
835 switch (adp
->va_info
.vi_mem_model
) {
836 case V_INFO_MM_PLANAR
:
837 ppw
= 16/(adp
->va_info
.vi_depth
/adp
->va_info
.vi_planes
);
838 count
= (pixel
+ ppw
- 1)/ppw
/2;
839 bpl
= ((pixel
+ ppw
- 1)/ppw
/2)*4;
841 case V_INFO_MM_PACKED
:
842 count
= (pixel
+ 7)/8;
843 bpl
= ((pixel
+ 7)/8)*8;
846 count
= (pixel
+ 7)/8; /* columns */
847 bpl
= (pixel
+ 7)/8; /* columns */
853 if (mp
[10 + 0x17] & 0x40) /* CRTC mode control reg */
854 count
*= 2; /* byte mode */
857 adp
->va_line_width
= bpl
;
863 set_display_start(video_adapter_t
*adp
, int x
, int y
)
865 int off
; /* byte offset (graphics mode)/word offset (text mode) */
866 int poff
; /* pixel offset */
867 int roff
; /* row offset */
868 int ppb
; /* pixels per byte */
870 if (adp
->va_info
.vi_flags
& V_INFO_GRAPHICS
) {
871 ppb
= 8/(adp
->va_info
.vi_depth
/adp
->va_info
.vi_planes
);
872 off
= y
*adp
->va_line_width
+ x
/ppb
;
881 off
= y
/adp
->va_info
.vi_cheight
*adp
->va_line_width
+ x
/ppb
;
882 roff
= y
%adp
->va_info
.vi_cheight
;
883 /* FIXME: is this correct? XXX */
891 outb(CRTC
, 0xc); /* high */
892 outb(CRTC
+ 1, off
>> 8);
893 outb(CRTC
, 0xd); /* low */
894 outb(CRTC
+ 1, off
& 0xff);
896 /* horizontal pel pan */
898 outb(ATC
, 0x13 | 0x20);
903 /* preset raw scan */
905 outb(CRTC
+ 1, roff
);
907 adp
->va_disp_start
.x
= x
;
908 adp
->va_disp_start
.y
= y
;
912 #ifndef VGA_NO_MODE_CHANGE
913 #if defined(__i386__) || defined(__amd64__) /* XXX */
915 fill(int val
, void *d
, size_t size
)
922 #endif /* __i386__ || __amd64__ */
925 filll_io(int val
, vm_offset_t d
, size_t size
)
929 d
+= sizeof(u_int32_t
);
932 #endif /* !VGA_NO_MODE_CHANGE */
951 vga_probe(int unit
, video_adapter_t
**adpp
, void *arg
, int flags
)
957 *adpp
= &biosadapter
;
963 vga_init(int unit
, video_adapter_t
*adp
, int flags
)
965 if ((unit
!= 0) || (adp
== NULL
) || !probe_done(adp
))
968 if (!init_done(adp
)) {
969 /* nothing to do really... */
970 adp
->va_flags
|= V_ADP_INITIALIZED
;
973 if (!config_done(adp
)) {
974 if (vid_register(adp
) < 0)
976 adp
->va_flags
|= V_ADP_REGISTERED
;
978 if (vga_sub_configure
!= NULL
)
979 (*vga_sub_configure
)(0);
986 * Return the video_info structure of the requested video mode.
989 vga_get_info(video_adapter_t
*adp
, int mode
, video_info_t
*info
)
996 #ifndef VGA_NO_MODE_CHANGE
997 if (adp
->va_flags
& V_ADP_MODECHANGE
) {
999 * If the parameter table entry for this mode is not found,
1000 * the mode is not supported...
1002 if (get_mode_param(mode
) == NULL
)
1005 #endif /* VGA_NO_MODE_CHANGE */
1008 * Even if we don't support video mode switching on this adapter,
1009 * the information on the initial (thus current) video mode
1010 * should be made available.
1012 if (mode
!= adp
->va_initial_mode
)
1016 for (i
= 0; bios_vmode
[i
].vi_mode
!= EOT
; ++i
) {
1017 if (bios_vmode
[i
].vi_mode
== NA
)
1019 if (mode
== bios_vmode
[i
].vi_mode
) {
1020 *info
= bios_vmode
[i
];
1022 info
->vi_buffer_size
= info
->vi_window_size
*info
->vi_planes
;
1031 * Find a video mode matching the requested parameters.
1032 * Fields filled with 0 are considered "don't care" fields and
1036 vga_query_mode(video_adapter_t
*adp
, video_info_t
*info
)
1043 for (i
= 0; bios_vmode
[i
].vi_mode
!= EOT
; ++i
) {
1044 if (bios_vmode
[i
].vi_mode
== NA
)
1047 if ((info
->vi_width
!= 0)
1048 && (info
->vi_width
!= bios_vmode
[i
].vi_width
))
1050 if ((info
->vi_height
!= 0)
1051 && (info
->vi_height
!= bios_vmode
[i
].vi_height
))
1053 if ((info
->vi_cwidth
!= 0)
1054 && (info
->vi_cwidth
!= bios_vmode
[i
].vi_cwidth
))
1056 if ((info
->vi_cheight
!= 0)
1057 && (info
->vi_cheight
!= bios_vmode
[i
].vi_cheight
))
1059 if ((info
->vi_depth
!= 0)
1060 && (info
->vi_depth
!= bios_vmode
[i
].vi_depth
))
1062 if ((info
->vi_planes
!= 0)
1063 && (info
->vi_planes
!= bios_vmode
[i
].vi_planes
))
1065 /* XXX: should check pixel format, memory model */
1066 if ((info
->vi_flags
!= 0)
1067 && (info
->vi_flags
!= bios_vmode
[i
].vi_flags
))
1070 /* verify if this mode is supported on this adapter */
1071 if (vga_get_info(adp
, bios_vmode
[i
].vi_mode
, info
))
1080 * Change the video mode.
1083 #ifndef VGA_NO_MODE_CHANGE
1086 set_width90(adp_state_t
*params
)
1089 * Based on code submitted by Kelly Yancey (kbyanc@freedomnet.com)
1090 * and alexv@sui.gda.itesm.mx.
1092 params
->regs
[5] |= 1; /* toggle 8 pixel wide fonts */
1093 params
->regs
[10+0x0] = 0x6b;
1094 params
->regs
[10+0x1] = 0x59;
1095 params
->regs
[10+0x2] = 0x5a;
1096 params
->regs
[10+0x3] = 0x8e;
1097 params
->regs
[10+0x4] = 0x5e;
1098 params
->regs
[10+0x5] = 0x8a;
1099 params
->regs
[10+0x13] = 45;
1100 params
->regs
[35+0x13] = 0;
1102 #endif /* VGA_WIDTH90 */
1103 #endif /* !VGA_NO_MODE_CHANGE */
1106 vga_set_mode(video_adapter_t
*adp
, int mode
)
1108 #ifndef VGA_NO_MODE_CHANGE
1112 prologue(adp
, V_ADP_MODECHANGE
, ENODEV
);
1114 if (vga_get_info(adp
, mode
, &info
))
1118 kprintf("vga_set_mode(): setting mode %d\n", mode
);
1121 params
.sig
= V_STATE_SIG
;
1122 bcopy(get_mode_param(mode
), params
.regs
, sizeof(params
.regs
));
1126 case M_VGA_C90x60
: case M_VGA_M90x60
:
1127 set_width90(¶ms
);
1130 case M_VGA_C80x60
: case M_VGA_M80x60
:
1131 params
.regs
[2] = 0x08;
1132 params
.regs
[19] = 0x47;
1136 case M_VGA_C90x30
: case M_VGA_M90x30
:
1137 set_width90(¶ms
);
1140 case M_VGA_C80x30
: case M_VGA_M80x30
:
1141 params
.regs
[19] = 0x4f;
1143 params
.regs
[9] |= 0xc0;
1144 params
.regs
[16] = 0x08;
1145 params
.regs
[17] = 0x3e;
1146 params
.regs
[26] = 0xea;
1147 params
.regs
[28] = 0xdf;
1148 params
.regs
[31] = 0xe7;
1149 params
.regs
[32] = 0x04;
1153 case M_VGA_C90x43
: case M_VGA_M90x43
:
1154 set_width90(¶ms
);
1157 case M_ENH_C80x43
: case M_ENH_B80x43
:
1158 params
.regs
[28] = 87;
1162 case M_VGA_C90x50
: case M_VGA_M90x50
:
1163 set_width90(¶ms
);
1166 case M_VGA_C80x50
: case M_VGA_M80x50
:
1169 params
.regs
[19] = 7;
1173 case M_VGA_C90x25
: case M_VGA_M90x25
:
1174 set_width90(¶ms
);
1177 case M_VGA_C40x25
: case M_VGA_C80x25
:
1179 case M_B40x25
: case M_C40x25
:
1180 case M_B80x25
: case M_C80x25
:
1181 case M_ENH_B40x25
: case M_ENH_C40x25
:
1182 case M_ENH_B80x25
: case M_ENH_C80x25
:
1183 case M_EGAMONO80x25
:
1186 vga_load_state(adp
, ¶ms
);
1190 /* "unchain" the VGA mode */
1191 params
.regs
[5-1+0x04] &= 0xf7;
1192 params
.regs
[5-1+0x04] |= 0x04;
1193 /* turn off doubleword mode */
1194 params
.regs
[10+0x14] &= 0xbf;
1195 /* turn off word addressing */
1196 params
.regs
[10+0x17] |= 0x40;
1197 /* set logical screen width */
1198 params
.regs
[10+0x13] = 80;
1200 params
.regs
[10+0x11] = 0x2c;
1201 params
.regs
[10+0x06] = 0x0d;
1202 params
.regs
[10+0x07] = 0x3e;
1203 params
.regs
[10+0x10] = 0xea;
1204 params
.regs
[10+0x11] = 0xac;
1205 params
.regs
[10+0x12] = 0xdf;
1206 params
.regs
[10+0x15] = 0xe7;
1207 params
.regs
[10+0x16] = 0x06;
1208 /* set vertical sync polarity to reflect aspect ratio */
1209 params
.regs
[9] = 0xe3;
1212 case M_BG320
: case M_CG320
: case M_BG640
:
1213 case M_CG320_D
: case M_CG640_E
:
1214 case M_CG640x350
: case M_ENH_CG640
:
1215 case M_BG640x480
: case M_CG640x480
: case M_VGA_CG320
:
1218 vga_load_state(adp
, ¶ms
);
1225 adp
->va_mode
= mode
;
1226 info
.vi_flags
&= ~V_INFO_LINEAR
; /* XXX */
1227 update_adapter_info(adp
, &info
);
1229 /* move hardware cursor out of the way */
1230 (*vidsw
[adp
->va_index
]->set_hw_cursor
)(adp
, -1, -1);
1233 #else /* VGA_NO_MODE_CHANGE */
1235 #endif /* VGA_NO_MODE_CHANGE */
1238 #ifndef VGA_NO_FONT_LOADING
1241 set_font_mode(video_adapter_t
*adp
, u_char
*buf
)
1245 /* save register values */
1246 outb(TSIDX
, 0x02); buf
[0] = inb(TSREG
);
1247 outb(TSIDX
, 0x04); buf
[1] = inb(TSREG
);
1248 outb(GDCIDX
, 0x04); buf
[2] = inb(GDCREG
);
1249 outb(GDCIDX
, 0x05); buf
[3] = inb(GDCREG
);
1250 outb(GDCIDX
, 0x06); buf
[4] = inb(GDCREG
);
1252 outb(ATC
, 0x10); buf
[5] = inb(ATC
+ 1);
1254 /* setup vga for loading fonts */
1255 inb(CRTC
+ 6); /* reset flip-flop */
1256 outb(ATC
, 0x10); outb(ATC
, buf
[5] & ~0x01);
1257 inb(CRTC
+ 6); /* reset flip-flop */
1258 outb(ATC
, 0x20); /* enable palette */
1260 #if VGA_SLOW_IOACCESS
1261 #ifdef VGA_ALT_SEQACCESS
1262 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1264 outb(TSIDX
, 0x02); outb(TSREG
, 0x04);
1265 outb(TSIDX
, 0x04); outb(TSREG
, 0x07);
1266 #ifdef VGA_ALT_SEQACCESS
1267 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1269 outb(GDCIDX
, 0x04); outb(GDCREG
, 0x02);
1270 outb(GDCIDX
, 0x05); outb(GDCREG
, 0x00);
1271 outb(GDCIDX
, 0x06); outb(GDCREG
, 0x04);
1272 #else /* VGA_SLOW_IOACCESS */
1273 #ifdef VGA_ALT_SEQACCESS
1274 outw(TSIDX
, 0x0100);
1276 outw(TSIDX
, 0x0402);
1277 outw(TSIDX
, 0x0704);
1278 #ifdef VGA_ALT_SEQACCESS
1279 outw(TSIDX
, 0x0300);
1281 outw(GDCIDX
, 0x0204);
1282 outw(GDCIDX
, 0x0005);
1283 outw(GDCIDX
, 0x0406); /* addr = a0000, 64kb */
1284 #endif /* VGA_SLOW_IOACCESS */
1290 set_normal_mode(video_adapter_t
*adp
, u_char
*buf
)
1294 /* setup vga for normal operation mode again */
1295 inb(CRTC
+ 6); /* reset flip-flop */
1296 outb(ATC
, 0x10); outb(ATC
, buf
[5]);
1297 inb(CRTC
+ 6); /* reset flip-flop */
1298 outb(ATC
, 0x20); /* enable palette */
1300 #if VGA_SLOW_IOACCESS
1301 #ifdef VGA_ALT_SEQACCESS
1302 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1304 outb(TSIDX
, 0x02); outb(TSREG
, buf
[0]);
1305 outb(TSIDX
, 0x04); outb(TSREG
, buf
[1]);
1306 #ifdef VGA_ALT_SEQACCESS
1307 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1309 outb(GDCIDX
, 0x04); outb(GDCREG
, buf
[2]);
1310 outb(GDCIDX
, 0x05); outb(GDCREG
, buf
[3]);
1311 outb(GDCIDX
, 0x06); outb(GDCREG
,(buf
[4] & 0x03) | 0x0c);
1312 #else /* VGA_SLOW_IOACCESS */
1313 #ifdef VGA_ALT_SEQACCESS
1314 outw(TSIDX
, 0x0100);
1316 outw(TSIDX
, 0x0002 | (buf
[0] << 8));
1317 outw(TSIDX
, 0x0004 | (buf
[1] << 8));
1318 #ifdef VGA_ALT_SEQACCESS
1319 outw(TSIDX
, 0x0300);
1321 outw(GDCIDX
, 0x0004 | (buf
[2] << 8));
1322 outw(GDCIDX
, 0x0005 | (buf
[3] << 8));
1323 outw(GDCIDX
, 0x0006 | (((buf
[4] & 0x03) | 0x0c)<<8));
1324 #endif /* VGA_SLOW_IOACCESS */
1329 #endif /* VGA_NO_FONT_LOADING */
1333 * Read the font data in the requested font page from the video adapter.
1336 vga_save_font(video_adapter_t
*adp
, int page
, int fontsize
, u_char
*data
,
1339 #ifndef VGA_NO_FONT_LOADING
1340 u_char buf
[PARAM_BUFSIZE
];
1341 vm_offset_t segment
;
1343 #ifdef VGA_ALT_SEQACCESS
1347 prologue(adp
, V_ADP_FONT
, ENODEV
);
1349 if (fontsize
< 14) {
1352 } else if (fontsize
>= 32) {
1354 } else if (fontsize
>= 16) {
1362 if (page
< 0 || page
>= 8)
1364 segment
= FONT_BUF
+ 0x4000*page
;
1368 #ifdef VGA_ALT_SEQACCESS
1370 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1371 outb(TSIDX
, 0x01); val
= inb(TSREG
); /* disable screen */
1372 outb(TSIDX
, 0x01); outb(TSREG
, val
| 0x20);
1373 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1377 set_font_mode(adp
, buf
);
1378 if (fontsize
== 32) {
1379 bcopy_fromio((uintptr_t)segment
+ ch
*32, data
, fontsize
*count
);
1381 for (c
= ch
; count
> 0; ++c
, --count
) {
1382 bcopy_fromio((uintptr_t)segment
+ c
*32, data
, fontsize
);
1386 set_normal_mode(adp
, buf
);
1388 #ifdef VGA_ALT_SEQACCESS
1390 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1391 outb(TSIDX
, 0x01); outb(TSREG
, val
& 0xdf); /* enable screen */
1392 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1397 #else /* VGA_NO_FONT_LOADING */
1399 #endif /* VGA_NO_FONT_LOADING */
1404 * Set the font data in the requested font page.
1405 * NOTE: it appears that some recent video adapters do not support
1406 * the font page other than 0... XXX
1409 vga_load_font(video_adapter_t
*adp
, int page
, int fontsize
, u_char
*data
,
1412 #ifndef VGA_NO_FONT_LOADING
1413 u_char buf
[PARAM_BUFSIZE
];
1414 vm_offset_t segment
;
1416 #ifdef VGA_ALT_SEQACCESS
1420 prologue(adp
, V_ADP_FONT
, ENODEV
);
1422 if (fontsize
< 14) {
1425 } else if (fontsize
>= 32) {
1427 } else if (fontsize
>= 16) {
1435 if (page
< 0 || page
>= 8)
1437 segment
= FONT_BUF
+ 0x4000*page
;
1441 #ifdef VGA_ALT_SEQACCESS
1443 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1444 outb(TSIDX
, 0x01); val
= inb(TSREG
); /* disable screen */
1445 outb(TSIDX
, 0x01); outb(TSREG
, val
| 0x20);
1446 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1450 set_font_mode(adp
, buf
);
1451 if (fontsize
== 32) {
1452 bcopy_toio(data
, (uintptr_t)segment
+ ch
*32, fontsize
*count
);
1454 for (c
= ch
; count
> 0; ++c
, --count
) {
1455 bcopy_toio(data
, (uintptr_t)segment
+ c
*32, fontsize
);
1459 set_normal_mode(adp
, buf
);
1461 #ifdef VGA_ALT_SEQACCESS
1463 outb(TSIDX
, 0x00); outb(TSREG
, 0x01);
1464 outb(TSIDX
, 0x01); outb(TSREG
, val
& 0xdf); /* enable screen */
1465 outb(TSIDX
, 0x00); outb(TSREG
, 0x03);
1470 #else /* VGA_NO_FONT_LOADING */
1472 #endif /* VGA_NO_FONT_LOADING */
1477 * Activate the requested font page.
1478 * NOTE: it appears that some recent video adapters do not support
1479 * the font page other than 0... XXX
1482 vga_show_font(video_adapter_t
*adp
, int page
)
1484 #ifndef VGA_NO_FONT_LOADING
1485 static u_char cg
[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1487 prologue(adp
, V_ADP_FONT
, ENODEV
);
1488 if (page
< 0 || page
>= 8)
1492 outb(TSIDX
, 0x03); outb(TSREG
, cg
[page
]);
1496 #else /* VGA_NO_FONT_LOADING */
1498 #endif /* VGA_NO_FONT_LOADING */
1503 * Read DAC values. The values have expressed in 8 bits.
1508 vga_save_palette(video_adapter_t
*adp
, u_char
*palette
)
1512 prologue(adp
, V_ADP_PALETTE
, ENODEV
);
1515 * We store 8 bit values in the palette buffer, while the standard
1516 * VGA has 6 bit DAC .
1518 outb(PALRADR
, 0x00);
1519 for (i
= 0; i
< 256*3; ++i
)
1520 palette
[i
] = inb(PALDATA
) << 2;
1521 inb(CRTC
+ 6); /* reset flip/flop */
1526 vga_save_palette2(video_adapter_t
*adp
, int base
, int count
,
1527 u_char
*r
, u_char
*g
, u_char
*b
)
1531 prologue(adp
, V_ADP_PALETTE
, ENODEV
);
1533 outb(PALRADR
, base
);
1534 for (i
= 0; i
< count
; ++i
) {
1535 r
[i
] = inb(PALDATA
) << 2;
1536 g
[i
] = inb(PALDATA
) << 2;
1537 b
[i
] = inb(PALDATA
) << 2;
1539 inb(CRTC
+ 6); /* reset flip/flop */
1550 vga_load_palette(video_adapter_t
*adp
, const u_char
*palette
)
1554 prologue(adp
, V_ADP_PALETTE
, ENODEV
);
1556 outb(PIXMASK
, 0xff); /* no pixelmask */
1557 outb(PALWADR
, 0x00);
1558 for (i
= 0; i
< 256*3; ++i
)
1559 outb(PALDATA
, palette
[i
] >> 2);
1560 inb(CRTC
+ 6); /* reset flip/flop */
1561 outb(ATC
, 0x20); /* enable palette */
1566 vga_load_palette2(video_adapter_t
*adp
, int base
, int count
,
1567 u_char
*r
, u_char
*g
, u_char
*b
)
1571 prologue(adp
, V_ADP_PALETTE
, ENODEV
);
1573 outb(PIXMASK
, 0xff); /* no pixelmask */
1574 outb(PALWADR
, base
);
1575 for (i
= 0; i
< count
; ++i
) {
1576 outb(PALDATA
, r
[i
] >> 2);
1577 outb(PALDATA
, g
[i
] >> 2);
1578 outb(PALDATA
, b
[i
] >> 2);
1580 inb(CRTC
+ 6); /* reset flip/flop */
1581 outb(ATC
, 0x20); /* enable palette */
1587 * Change the border color.
1590 vga_set_border(video_adapter_t
*adp
, int color
)
1592 prologue(adp
, V_ADP_BORDER
, ENODEV
);
1594 inb(CRTC
+ 6); /* reset flip-flop */
1595 outb(ATC
, 0x31); outb(ATC
, color
& 0xff);
1602 * Read video register values.
1603 * NOTE: this function only reads the standard VGA registers.
1604 * any extra/extended registers of SVGA adapters are not saved.
1607 vga_save_state(video_adapter_t
*adp
, void *p
, size_t size
)
1615 /* return the required buffer size */
1616 prologue(adp
, V_ADP_STATESAVE
, 0);
1617 return sizeof(adp_state_t
);
1619 prologue(adp
, V_ADP_STATESAVE
, ENODEV
);
1620 if (size
< sizeof(adp_state_t
))
1623 ((adp_state_t
*)p
)->sig
= V_STATE_SIG
;
1624 buf
= ((adp_state_t
*)p
)->regs
;
1625 bzero(buf
, V_MODE_PARAM_SIZE
);
1630 outb(TSIDX
, 0x00); outb(TSREG
, 0x01); /* stop sequencer */
1631 for (i
= 0, j
= 5; i
< 4; i
++) {
1633 buf
[j
++] = inb(TSREG
);
1635 buf
[9] = inb(MISC
+ 10); /* dot-clock */
1636 outb(TSIDX
, 0x00); outb(TSREG
, 0x03); /* start sequencer */
1638 for (i
= 0, j
= 10; i
< 25; i
++) { /* crtc */
1640 buf
[j
++] = inb(crtc_addr
+ 1);
1642 for (i
= 0, j
= 35; i
< 20; i
++) { /* attribute ctrl */
1643 inb(crtc_addr
+ 6); /* reset flip-flop */
1645 buf
[j
++] = inb(ATC
+ 1);
1647 for (i
= 0, j
= 55; i
< 9; i
++) { /* graph data ctrl */
1649 buf
[j
++] = inb(GDCREG
);
1651 inb(crtc_addr
+ 6); /* reset flip-flop */
1652 outb(ATC
, 0x20); /* enable palette */
1657 if (vga_get_info(adp
, adp
->va_mode
, &info
) == 0) {
1658 if (info
.vi_flags
& V_INFO_GRAPHICS
) {
1659 buf
[0] = info
.vi_width
/info
.vi_cwidth
; /* COLS */
1660 buf
[1] = info
.vi_height
/info
.vi_cheight
- 1; /* ROWS */
1662 buf
[0] = info
.vi_width
; /* COLS */
1663 buf
[1] = info
.vi_height
- 1; /* ROWS */
1665 buf
[2] = info
.vi_cheight
; /* POINTS */
1667 /* XXX: shouldn't be happening... */
1668 kprintf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
1669 adp
->va_unit
, adp
->va_name
);
1672 buf
[0] = readb(BIOS_PADDRTOVADDR(0x44a)); /* COLS */
1673 buf
[1] = readb(BIOS_PADDRTOVADDR(0x484)); /* ROWS */
1674 buf
[2] = readb(BIOS_PADDRTOVADDR(0x485)); /* POINTS */
1675 buf
[3] = readb(BIOS_PADDRTOVADDR(0x44c));
1676 buf
[4] = readb(BIOS_PADDRTOVADDR(0x44d));
1684 * Set video registers at once.
1685 * NOTE: this function only updates the standard VGA registers.
1686 * any extra/extended registers of SVGA adapters are not changed.
1689 vga_load_state(video_adapter_t
*adp
, void *p
)
1695 prologue(adp
, V_ADP_STATELOAD
, ENODEV
);
1696 if (((adp_state_t
*)p
)->sig
!= V_STATE_SIG
)
1699 buf
= ((adp_state_t
*)p
)->regs
;
1703 dump_buffer(buf
, V_MODE_PARAM_SIZE
);
1708 outb(TSIDX
, 0x00); outb(TSREG
, 0x01); /* stop sequencer */
1709 for (i
= 0; i
< 4; ++i
) { /* program sequencer */
1711 outb(TSREG
, buf
[i
+ 5]);
1713 outb(MISC
, buf
[9]); /* set dot-clock */
1714 outb(TSIDX
, 0x00); outb(TSREG
, 0x03); /* start sequencer */
1715 outb(crtc_addr
, 0x11);
1716 outb(crtc_addr
+ 1, inb(crtc_addr
+ 1) & 0x7F);
1717 for (i
= 0; i
< 25; ++i
) { /* program crtc */
1719 outb(crtc_addr
+ 1, buf
[i
+ 10]);
1721 inb(crtc_addr
+6); /* reset flip-flop */
1722 for (i
= 0; i
< 20; ++i
) { /* program attribute ctrl */
1724 outb(ATC
, buf
[i
+ 35]);
1726 for (i
= 0; i
< 9; ++i
) { /* program graph data ctrl */
1728 outb(GDCREG
, buf
[i
+ 55]);
1730 inb(crtc_addr
+ 6); /* reset flip-flop */
1731 outb(ATC
, 0x20); /* enable palette */
1733 #if notyet /* a temporary workaround for kernel panic, XXX */
1735 if (adp
->va_unit
== V_ADP_PRIMARY
) {
1736 writeb(BIOS_PADDRTOVADDR(0x44a), buf
[0]); /* COLS */
1737 writeb(BIOS_PADDRTOVADDR(0x484), buf
[1] + rows_offset
- 1); /* ROWS */
1738 writeb(BIOS_PADDRTOVADDR(0x485), buf
[2]); /* POINTS */
1740 writeb(BIOS_PADDRTOVADDR(0x44c), buf
[3]);
1741 writeb(BIOS_PADDRTOVADDR(0x44d), buf
[4]);
1744 #endif /* VGA_NO_BIOS */
1753 * Change the origin (window mapping) of the banked frame buffer.
1756 vga_set_origin(video_adapter_t
*adp
, off_t offset
)
1759 * The standard video modes do not require window mapping;
1760 * always return error.
1767 * Read the position of the hardware text cursor.
1770 vga_read_hw_cursor(video_adapter_t
*adp
, int *col
, int *row
)
1777 if (adp
->va_info
.vi_flags
& V_INFO_GRAPHICS
)
1782 off
= inb(CRTC
+ 1);
1784 off
= (off
<< 8) | inb(CRTC
+ 1);
1787 *row
= off
/ adp
->va_info
.vi_width
;
1788 *col
= off
% adp
->va_info
.vi_width
;
1795 * Move the hardware text cursor. If col and row are both -1,
1796 * the cursor won't be shown.
1799 vga_set_hw_cursor(video_adapter_t
*adp
, int col
, int row
)
1806 if ((col
== -1) && (row
== -1)) {
1809 if (adp
->va_info
.vi_flags
& V_INFO_GRAPHICS
)
1811 off
= row
*adp
->va_info
.vi_width
+ col
;
1816 outb(CRTC
+ 1, off
>> 8);
1818 outb(CRTC
+ 1, off
& 0x00ff);
1825 * set_hw_cursor_shape():
1826 * Change the shape of the hardware text cursor. If the height is
1827 * zero or negative, the cursor won't be shown.
1830 vga_set_hw_cursor_shape(video_adapter_t
*adp
, int base
, int height
,
1831 int celsize
, int blink
)
1838 /* make the cursor invisible */
1845 outb(CRTC
+ 1, celsize
- base
- height
);
1847 outb(CRTC
+ 1, celsize
- base
- 1);
1856 * Put the display in power save/power off mode.
1859 vga_blank_display(video_adapter_t
*adp
, int mode
)
1865 case V_DISPLAY_SUSPEND
:
1866 case V_DISPLAY_STAND_BY
:
1870 outb(TSREG
, val
| 0x20);
1872 val
= inb(CRTC
+ 1);
1873 outb(CRTC
+ 1, val
& ~0x80);
1879 outb(TSREG
, val
| 0x20);
1885 outb(TSREG
, val
& 0xDF);
1887 val
= inb(CRTC
+ 1);
1888 outb(CRTC
+ 1, val
| 0x80);
1898 * Mmap frame buffer.
1901 vga_mmap_buf(video_adapter_t
*adp
, vm_offset_t offset
, int prot
)
1903 if (adp
->va_info
.vi_flags
& V_INFO_LINEAR
)
1907 kprintf("vga_mmap_buf(): window:0x%x, offset:%p\n",
1908 adp
->va_info
.vi_window
, (void *)offset
);
1911 /* XXX: is this correct? */
1912 if (offset
> adp
->va_window_size
- PAGE_SIZE
)
1915 #if defined(__i386__)
1916 return i386_btop(adp
->va_info
.vi_window
+ offset
);
1917 #elif defined(__amd64__)
1918 return amd64_btop(adp
->va_info
.vi_window
+ offset
);
1920 #error "vga_mmap_buf needs to return something"
1924 #ifndef VGA_NO_MODE_CHANGE
1927 planar_fill(video_adapter_t
*adp
, int val
)
1930 int at
; /* position in the frame buffer */
1933 outw(GDCIDX
, 0x0005); /* read mode 0, write mode 0 */
1934 outw(GDCIDX
, 0x0003); /* data rotate/function select */
1935 outw(GDCIDX
, 0x0f01); /* set/reset enable */
1936 outw(GDCIDX
, 0xff08); /* bit mask */
1937 outw(GDCIDX
, (val
<< 8) | 0x00); /* set/reset */
1939 length
= adp
->va_line_width
*adp
->va_info
.vi_height
;
1940 while (length
> 0) {
1941 l
= imin(length
, adp
->va_window_size
);
1942 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, at
);
1943 bzero_io(adp
->va_window
, l
);
1947 outw(GDCIDX
, 0x0000); /* set/reset */
1948 outw(GDCIDX
, 0x0001); /* set/reset enable */
1952 packed_fill(video_adapter_t
*adp
, int val
)
1955 int at
; /* position in the frame buffer */
1959 length
= adp
->va_line_width
*adp
->va_info
.vi_height
;
1960 while (length
> 0) {
1961 l
= imin(length
, adp
->va_window_size
);
1962 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, at
);
1963 fill_io(val
, adp
->va_window
, l
);
1970 direct_fill(video_adapter_t
*adp
, int val
)
1973 int at
; /* position in the frame buffer */
1977 length
= adp
->va_line_width
*adp
->va_info
.vi_height
;
1978 while (length
> 0) {
1979 l
= imin(length
, adp
->va_window_size
);
1980 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, at
);
1981 switch (adp
->va_info
.vi_pixel_size
) {
1982 case sizeof(u_int16_t
):
1983 fillw_io(val
, adp
->va_window
, l
/sizeof(u_int16_t
));
1988 case sizeof(u_int32_t
):
1989 filll_io(val
, adp
->va_window
, l
/sizeof(u_int32_t
));
1998 vga_clear(video_adapter_t
*adp
)
2000 switch (adp
->va_info
.vi_mem_model
) {
2001 case V_INFO_MM_TEXT
:
2002 /* do nothing? XXX */
2004 case V_INFO_MM_PLANAR
:
2005 planar_fill(adp
, 0);
2007 case V_INFO_MM_PACKED
:
2008 packed_fill(adp
, 0);
2010 case V_INFO_MM_DIRECT
:
2011 direct_fill(adp
, 0);
2019 planar_fill_rect(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2024 int offset
; /* offset within window */
2028 outw(GDCIDX
, 0x0005); /* read mode 0, write mode 0 */
2029 outw(GDCIDX
, 0x0003); /* data rotate/function select */
2030 outw(GDCIDX
, 0x0f01); /* set/reset enable */
2031 outw(GDCIDX
, 0xff08); /* bit mask */
2032 outw(GDCIDX
, (val
<< 8) | 0x00); /* set/reset */
2034 banksize
= adp
->va_window_size
;
2037 pos
= adp
->va_line_width
*y
+ x
/8;
2038 if (bank
!= pos
/banksize
) {
2039 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, pos
);
2040 bank
= pos
/banksize
;
2042 offset
= pos
%banksize
;
2043 bx
= (x
+ cx
)/8 - x
/8;
2045 outw(GDCIDX
, ((0xff00 >> (x
% 8)) & 0xff00) | 0x08);
2046 writeb(adp
->va_window
+ offset
, 0);
2049 if (offset
>= banksize
) {
2051 ++bank
; /* next bank */
2052 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2054 outw(GDCIDX
, 0xff08); /* bit mask */
2057 l
= imin(bx
, banksize
);
2058 bzero_io(adp
->va_window
+ offset
, l
);
2061 if (offset
>= banksize
) {
2063 ++bank
; /* next bank */
2064 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2068 outw(GDCIDX
, (~(0xff00 >> ((x
+ cx
) % 8)) & 0xff00) | 0x08);
2069 writeb(adp
->va_window
+ offset
, 0);
2071 if (offset
>= banksize
) {
2073 ++bank
; /* next bank */
2074 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2076 outw(GDCIDX
, 0xff08); /* bit mask */
2082 outw(GDCIDX
, 0xff08); /* bit mask */
2083 outw(GDCIDX
, 0x0000); /* set/reset */
2084 outw(GDCIDX
, 0x0001); /* set/reset enable */
2088 packed_fill_rect(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2093 int offset
; /* offset within window */
2096 banksize
= adp
->va_window_size
;
2098 cx
*= adp
->va_info
.vi_pixel_size
;
2100 pos
= adp
->va_line_width
*y
+ x
*adp
->va_info
.vi_pixel_size
;
2101 if (bank
!= pos
/banksize
) {
2102 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, pos
);
2103 bank
= pos
/banksize
;
2105 offset
= pos
%banksize
;
2106 end
= imin(offset
+ cx
, banksize
);
2107 fill_io(val
, adp
->va_window
+ offset
,
2108 (end
- offset
)/adp
->va_info
.vi_pixel_size
);
2109 /* the line may cross the window boundary */
2110 if (offset
+ cx
> banksize
) {
2111 ++bank
; /* next bank */
2112 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2113 end
= offset
+ cx
- banksize
;
2114 fill_io(val
, adp
->va_window
, end
/adp
->va_info
.vi_pixel_size
);
2122 direct_fill_rect16(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2127 int offset
; /* offset within window */
2131 * XXX: the function assumes that banksize is a muliple of
2132 * sizeof(u_int16_t).
2134 banksize
= adp
->va_window_size
;
2136 cx
*= sizeof(u_int16_t
);
2138 pos
= adp
->va_line_width
*y
+ x
*sizeof(u_int16_t
);
2139 if (bank
!= pos
/banksize
) {
2140 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, pos
);
2141 bank
= pos
/banksize
;
2143 offset
= pos
%banksize
;
2144 end
= imin(offset
+ cx
, banksize
);
2145 fillw_io(val
, adp
->va_window
+ offset
,
2146 (end
- offset
)/sizeof(u_int16_t
));
2147 /* the line may cross the window boundary */
2148 if (offset
+ cx
> banksize
) {
2149 ++bank
; /* next bank */
2150 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2151 end
= offset
+ cx
- banksize
;
2152 fillw_io(val
, adp
->va_window
, end
/sizeof(u_int16_t
));
2160 direct_fill_rect24(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2165 int offset
; /* offset within window */
2171 b
[0] = val
& 0x0000ff;
2172 b
[1] = (val
>> 8) & 0x0000ff;
2173 b
[2] = (val
>> 16) & 0x0000ff;
2174 banksize
= adp
->va_window_size
;
2178 pos
= adp
->va_line_width
*y
+ x
*3;
2179 if (bank
!= pos
/banksize
) {
2180 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, pos
);
2181 bank
= pos
/banksize
;
2183 offset
= pos
%banksize
;
2184 end
= imin(offset
+ cx
, banksize
);
2185 for (i
= 0, j
= offset
; j
< end
; i
= (++i
)%3, ++j
) {
2186 writeb(adp
->va_window
+ j
, b
[i
]);
2188 /* the line may cross the window boundary */
2189 if (offset
+ cx
>= banksize
) {
2190 ++bank
; /* next bank */
2191 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2193 end
= offset
+ cx
- banksize
;
2194 for (; j
< end
; i
= (++i
)%3, ++j
) {
2195 writeb(adp
->va_window
+ j
, b
[i
]);
2204 direct_fill_rect32(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2209 int offset
; /* offset within window */
2213 * XXX: the function assumes that banksize is a muliple of
2214 * sizeof(u_int32_t).
2216 banksize
= adp
->va_window_size
;
2218 cx
*= sizeof(u_int32_t
);
2220 pos
= adp
->va_line_width
*y
+ x
*sizeof(u_int32_t
);
2221 if (bank
!= pos
/banksize
) {
2222 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, pos
);
2223 bank
= pos
/banksize
;
2225 offset
= pos
%banksize
;
2226 end
= imin(offset
+ cx
, banksize
);
2227 filll_io(val
, adp
->va_window
+ offset
,
2228 (end
- offset
)/sizeof(u_int32_t
));
2229 /* the line may cross the window boundary */
2230 if (offset
+ cx
> banksize
) {
2231 ++bank
; /* next bank */
2232 (*vidsw
[adp
->va_index
]->set_win_org
)(adp
, bank
*banksize
);
2233 end
= offset
+ cx
- banksize
;
2234 filll_io(val
, adp
->va_window
, end
/sizeof(u_int32_t
));
2242 vga_fill_rect(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2244 switch (adp
->va_info
.vi_mem_model
) {
2245 case V_INFO_MM_TEXT
:
2246 /* do nothing? XXX */
2248 case V_INFO_MM_PLANAR
:
2249 planar_fill_rect(adp
, val
, x
, y
, cx
, cy
);
2251 case V_INFO_MM_PACKED
:
2252 packed_fill_rect(adp
, val
, x
, y
, cx
, cy
);
2254 case V_INFO_MM_DIRECT
:
2255 switch (adp
->va_info
.vi_pixel_size
) {
2256 case sizeof(u_int16_t
):
2257 direct_fill_rect16(adp
, val
, x
, y
, cx
, cy
);
2260 direct_fill_rect24(adp
, val
, x
, y
, cx
, cy
);
2262 case sizeof(u_int32_t
):
2263 direct_fill_rect32(adp
, val
, x
, y
, cx
, cy
);
2272 vga_fill_rect(video_adapter_t
*adp
, int val
, int x
, int y
, int cx
, int cy
)
2279 vga_bitblt(video_adapter_t
*adp
,...)
2285 #endif /* !VGA_NO_MODE_CHANGE */
2288 get_palette(video_adapter_t
*adp
, int base
, int count
,
2289 u_char
*red
, u_char
*green
, u_char
*blue
, u_char
*trans
)
2295 if (count
< 0 || base
< 0 || count
> 256 || base
> 256 ||
2299 r
= kmalloc(count
*3, M_DEVBUF
, M_WAITOK
);
2302 if (vga_save_palette2(adp
, base
, count
, r
, g
, b
)) {
2306 copyout(r
, red
, count
);
2307 copyout(g
, green
, count
);
2308 copyout(b
, blue
, count
);
2309 if (trans
!= NULL
) {
2311 copyout(r
, trans
, count
);
2319 set_palette(video_adapter_t
*adp
, int base
, int count
,
2320 u_char
*red
, u_char
*green
, u_char
*blue
, u_char
*trans
)
2327 if (count
< 0 || base
< 0 || count
> 256 || base
> 256 ||
2331 r
= kmalloc(count
*3, M_DEVBUF
, M_WAITOK
);
2334 err
= copyin(red
, r
, count
);
2336 err
= copyin(green
, g
, count
);
2338 err
= copyin(blue
, b
, count
);
2340 err
= vga_load_palette2(adp
, base
, count
, r
, g
, b
);
2343 return (err
? ENODEV
: 0);
2347 vga_dev_ioctl(video_adapter_t
*adp
, u_long cmd
, caddr_t arg
)
2350 case FBIO_GETWINORG
: /* get frame buffer window origin */
2354 case FBIO_SETWINORG
: /* set frame buffer window origin */
2357 case FBIO_SETDISPSTART
: /* set display start address */
2358 return (set_display_start(adp
,
2359 ((video_display_start_t
*)arg
)->x
,
2360 ((video_display_start_t
*)arg
)->y
)
2363 case FBIO_SETLINEWIDTH
: /* set scan line length in pixel */
2364 return (set_line_length(adp
, *(u_int
*)arg
) ? ENODEV
: 0);
2366 case FBIO_GETPALETTE
: /* get color palette */
2367 return get_palette(adp
, ((video_color_palette_t
*)arg
)->index
,
2368 ((video_color_palette_t
*)arg
)->count
,
2369 ((video_color_palette_t
*)arg
)->red
,
2370 ((video_color_palette_t
*)arg
)->green
,
2371 ((video_color_palette_t
*)arg
)->blue
,
2372 ((video_color_palette_t
*)arg
)->transparent
);
2374 case FBIO_SETPALETTE
: /* set color palette */
2375 return set_palette(adp
, ((video_color_palette_t
*)arg
)->index
,
2376 ((video_color_palette_t
*)arg
)->count
,
2377 ((video_color_palette_t
*)arg
)->red
,
2378 ((video_color_palette_t
*)arg
)->green
,
2379 ((video_color_palette_t
*)arg
)->blue
,
2380 ((video_color_palette_t
*)arg
)->transparent
);
2382 case FBIOGTYPE
: /* get frame buffer type info. */
2383 ((struct fbtype
*)arg
)->fb_type
= fb_type(adp
->va_type
);
2384 ((struct fbtype
*)arg
)->fb_height
= adp
->va_info
.vi_height
;
2385 ((struct fbtype
*)arg
)->fb_width
= adp
->va_info
.vi_width
;
2386 ((struct fbtype
*)arg
)->fb_depth
= adp
->va_info
.vi_depth
;
2387 if ((adp
->va_info
.vi_depth
<= 1) || (adp
->va_info
.vi_depth
> 8))
2388 ((struct fbtype
*)arg
)->fb_cmsize
= 0;
2390 ((struct fbtype
*)arg
)->fb_cmsize
= 1 << adp
->va_info
.vi_depth
;
2391 ((struct fbtype
*)arg
)->fb_size
= adp
->va_buffer_size
;
2394 case FBIOGETCMAP
: /* get color palette */
2395 return get_palette(adp
, ((struct fbcmap
*)arg
)->index
,
2396 ((struct fbcmap
*)arg
)->count
,
2397 ((struct fbcmap
*)arg
)->red
,
2398 ((struct fbcmap
*)arg
)->green
,
2399 ((struct fbcmap
*)arg
)->blue
, NULL
);
2401 case FBIOPUTCMAP
: /* set color palette */
2402 return set_palette(adp
, ((struct fbcmap
*)arg
)->index
,
2403 ((struct fbcmap
*)arg
)->count
,
2404 ((struct fbcmap
*)arg
)->red
,
2405 ((struct fbcmap
*)arg
)->green
,
2406 ((struct fbcmap
*)arg
)->blue
, NULL
);
2409 return fb_commonioctl(adp
, cmd
, arg
);
2414 dump_buffer(u_char
*buf
, size_t len
)
2418 for(i
= 0; i
< len
;) {
2419 kprintf("%02x ", buf
[i
]);
2420 if ((++i
% 16) == 0)
2427 * Print some information about the video adapter and video modes,
2428 * with requested level of details.
2431 vga_diag(video_adapter_t
*adp
, int level
)
2444 kprintf("vga: DCC code:0x%02x\n",
2445 readb(BIOS_PADDRTOVADDR(0x488)));
2446 kprintf("vga: CRTC:0x%x, video option:0x%02x, ",
2447 readw(BIOS_PADDRTOVADDR(0x463)),
2448 readb(BIOS_PADDRTOVADDR(0x487)));
2449 kprintf("rows:%d, cols:%d, font height:%d\n",
2450 readb(BIOS_PADDRTOVADDR(0x44a)),
2451 readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2452 readb(BIOS_PADDRTOVADDR(0x485)));
2453 #endif /* VGA_NO_BIOS */
2454 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2455 kprintf("vga: param table:%p\n", video_mode_ptr
);
2456 kprintf("vga: rows_offset:%d\n", rows_offset
);
2458 #endif /* FB_DEBUG > 1 */
2460 fb_dump_adp_info(VGA_DRIVER_NAME
, adp
, level
);
2463 if (adp
->va_flags
& V_ADP_MODECHANGE
) {
2464 for (i
= 0; bios_vmode
[i
].vi_mode
!= EOT
; ++i
) {
2465 if (bios_vmode
[i
].vi_mode
== NA
)
2467 if (get_mode_param(bios_vmode
[i
].vi_mode
) == NULL
)
2469 fb_dump_mode_info(VGA_DRIVER_NAME
, adp
, &bios_vmode
[i
], level
);
2472 vga_get_info(adp
, adp
->va_initial_mode
, &info
); /* shouldn't fail */
2473 fb_dump_mode_info(VGA_DRIVER_NAME
, adp
, &info
, level
);
2475 #endif /* FB_DEBUG > 1 */
2477 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2478 if (video_mode_ptr
== NULL
)
2479 kprintf("vga%d: %s: WARNING: video mode switching is not "
2480 "fully supported on this adapter\n",
2481 adp
->va_unit
, adp
->va_name
);
2486 kprintf("VGA parameters upon power-up\n");
2487 dump_buffer(adpstate
.regs
, sizeof(adpstate
.regs
));
2488 kprintf("VGA parameters in BIOS for mode %d\n", adp
->va_initial_mode
);
2489 dump_buffer(adpstate2
.regs
, sizeof(adpstate2
.regs
));
2491 mp
= get_mode_param(adp
->va_initial_mode
);
2492 if (mp
== NULL
) /* this shouldn't be happening */
2494 kprintf("VGA parameters to be used for mode %d\n", adp
->va_initial_mode
);
2495 dump_buffer(mp
, V_MODE_PARAM_SIZE
);