xref: /freebsd/sys/dev/fb/vesa.c (revision 7c1b51d6dc2e165ae7333373513b080f17cf79bd)
1 /*-
2  * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3  * Copyright (c) 2009-2013 Jung-uk Kim <jkim@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_vga.h"
32 #include "opt_vesa.h"
33 
34 #ifndef VGA_NO_MODE_CHANGE
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/fbio.h>
45 #include <sys/sysctl.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_param.h>
51 #include <vm/pmap.h>
52 
53 #include <machine/pc/bios.h>
54 #include <dev/fb/vesa.h>
55 
56 #include <dev/fb/fbreg.h>
57 #include <dev/fb/vgareg.h>
58 
59 #include <dev/pci/pcivar.h>
60 
61 #include <isa/isareg.h>
62 
63 #include <compat/x86bios/x86bios.h>
64 
65 #define	VESA_BIOS_OFFSET	0xc0000
66 #define	VESA_PALETTE_SIZE	(256 * 4)
67 #define	VESA_VIA_CLE266		"VIA CLE266\r\n"
68 
69 #ifndef VESA_DEBUG
70 #define VESA_DEBUG	0
71 #endif
72 
73 /* VESA video adapter state buffer stub */
74 struct adp_state {
75 	int		sig;
76 #define V_STATE_SIG	0x61736576
77 	u_char		regs[1];
78 };
79 typedef struct adp_state adp_state_t;
80 
81 static struct mtx vesa_lock;
82 MTX_SYSINIT(vesa_lock, &vesa_lock, "VESA lock", MTX_DEF);
83 
84 static int vesa_state;
85 static void *vesa_state_buf;
86 static uint32_t vesa_state_buf_offs;
87 static size_t vesa_state_buf_size;
88 
89 static u_char *vesa_palette;
90 static uint32_t vesa_palette_offs;
91 
92 static void *vesa_vmem_buf;
93 static size_t vesa_vmem_max;
94 
95 static void *vesa_bios;
96 static uint32_t vesa_bios_offs;
97 static uint32_t vesa_bios_int10;
98 static size_t vesa_bios_size;
99 
100 /* VESA video adapter */
101 static video_adapter_t *vesa_adp;
102 
103 static SYSCTL_NODE(_debug, OID_AUTO, vesa, CTLFLAG_RD, NULL, "VESA debugging");
104 static int vesa_shadow_rom;
105 SYSCTL_INT(_debug_vesa, OID_AUTO, shadow_rom, CTLFLAG_RDTUN, &vesa_shadow_rom,
106     0, "Enable video BIOS shadow");
107 
108 /* VESA functions */
109 #if 0
110 static int			vesa_nop(void);
111 #endif
112 static int			vesa_error(void);
113 static vi_probe_t		vesa_probe;
114 static vi_init_t		vesa_init;
115 static vi_get_info_t		vesa_get_info;
116 static vi_query_mode_t		vesa_query_mode;
117 static vi_set_mode_t		vesa_set_mode;
118 static vi_save_font_t		vesa_save_font;
119 static vi_load_font_t		vesa_load_font;
120 static vi_show_font_t		vesa_show_font;
121 static vi_save_palette_t	vesa_save_palette;
122 static vi_load_palette_t	vesa_load_palette;
123 static vi_set_border_t		vesa_set_border;
124 static vi_save_state_t		vesa_save_state;
125 static vi_load_state_t		vesa_load_state;
126 static vi_set_win_org_t		vesa_set_origin;
127 static vi_read_hw_cursor_t	vesa_read_hw_cursor;
128 static vi_set_hw_cursor_t	vesa_set_hw_cursor;
129 static vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
130 static vi_blank_display_t	vesa_blank_display;
131 static vi_mmap_t		vesa_mmap;
132 static vi_ioctl_t		vesa_ioctl;
133 static vi_clear_t		vesa_clear;
134 static vi_fill_rect_t		vesa_fill_rect;
135 static vi_bitblt_t		vesa_bitblt;
136 static vi_diag_t		vesa_diag;
137 static int			vesa_bios_info(int level);
138 
139 static video_switch_t vesavidsw = {
140 	vesa_probe,
141 	vesa_init,
142 	vesa_get_info,
143 	vesa_query_mode,
144 	vesa_set_mode,
145 	vesa_save_font,
146 	vesa_load_font,
147 	vesa_show_font,
148 	vesa_save_palette,
149 	vesa_load_palette,
150 	vesa_set_border,
151 	vesa_save_state,
152 	vesa_load_state,
153 	vesa_set_origin,
154 	vesa_read_hw_cursor,
155 	vesa_set_hw_cursor,
156 	vesa_set_hw_cursor_shape,
157 	vesa_blank_display,
158 	vesa_mmap,
159 	vesa_ioctl,
160 	vesa_clear,
161 	vesa_fill_rect,
162 	vesa_bitblt,
163 	vesa_error,
164 	vesa_error,
165 	vesa_diag,
166 };
167 
168 static video_switch_t *prevvidsw;
169 
170 /* VESA BIOS video modes */
171 #define VESA_MAXMODES	64
172 #define EOT		(-1)
173 #define NA		(-2)
174 
175 #define MODE_TABLE_DELTA 8
176 
177 static int vesa_vmode_max;
178 static video_info_t *vesa_vmode;
179 
180 static int vesa_init_done;
181 static struct vesa_info *vesa_adp_info;
182 static u_int16_t *vesa_vmodetab;
183 static char *vesa_oemstr;
184 static char *vesa_venderstr;
185 static char *vesa_prodstr;
186 static char *vesa_revstr;
187 
188 /* local macros and functions */
189 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
190 
191 static int int10_set_mode(int mode);
192 static int vesa_bios_post(void);
193 static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags);
194 static int vesa_bios_set_mode(int mode);
195 #if 0
196 static int vesa_bios_get_dac(void);
197 #endif
198 static int vesa_bios_set_dac(int bits);
199 static int vesa_bios_save_palette(int start, int colors, u_char *palette,
200 				  int bits);
201 static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
202 				   u_char *b, int bits);
203 static int vesa_bios_load_palette(int start, int colors, u_char *palette,
204 				  int bits);
205 static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
206 				   u_char *b, int bits);
207 #define STATE_SIZE	0
208 #define STATE_SAVE	1
209 #define STATE_LOAD	2
210 static size_t vesa_bios_state_buf_size(int);
211 static int vesa_bios_save_restore(int code, void *p);
212 #ifdef MODE_TABLE_BROKEN
213 static int vesa_bios_get_line_length(void);
214 #endif
215 static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
216 #if 0
217 static int vesa_bios_get_start(int *x, int *y);
218 #endif
219 static int vesa_bios_set_start(int x, int y);
220 static int vesa_map_gen_mode_num(int type, int color, int mode);
221 static int vesa_translate_flags(u_int16_t vflags);
222 static int vesa_translate_mmodel(u_int8_t vmodel);
223 static int vesa_get_bpscanline(struct vesa_mode *vmode);
224 static int vesa_bios_init(void);
225 static void vesa_bios_uninit(void);
226 static void vesa_clear_modes(video_info_t *info, int color);
227 
228 #if 0
229 static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
230 #endif
231 
232 /* INT 10 BIOS calls */
233 static int
234 int10_set_mode(int mode)
235 {
236 	x86regs_t regs;
237 
238 	x86bios_init_regs(&regs);
239 	regs.R_AL = mode;
240 
241 	x86bios_intr(&regs, 0x10);
242 
243 	return (0);
244 }
245 
246 static int
247 vesa_bios_post(void)
248 {
249 	x86regs_t regs;
250 	devclass_t dc;
251 	device_t *devs;
252 	device_t dev;
253 	int count, i, is_pci;
254 
255 	if (x86bios_get_orm(vesa_bios_offs) == NULL)
256 		return (1);
257 
258 	dev = NULL;
259 	is_pci = 0;
260 
261 	/* Find the matching PCI video controller. */
262 	dc = devclass_find("vgapci");
263 	if (dc != NULL && devclass_get_devices(dc, &devs, &count) == 0) {
264 		for (i = 0; i < count; i++)
265 			if (device_get_flags(devs[i]) != 0 &&
266 			    x86bios_match_device(vesa_bios_offs, devs[i])) {
267 				dev = devs[i];
268 				is_pci = 1;
269 				break;
270 			}
271 		free(devs, M_TEMP);
272 	}
273 
274 	/* Try VGA if a PCI device is not found. */
275 	if (dev == NULL) {
276 		dc = devclass_find(VGA_DRIVER_NAME);
277 		if (dc != NULL)
278 			dev = devclass_get_device(dc, 0);
279 	}
280 
281 	if (bootverbose)
282 		printf("%s: calling BIOS POST\n",
283 		    dev == NULL ? "VESA" : device_get_nameunit(dev));
284 
285 	x86bios_init_regs(&regs);
286 	if (is_pci) {
287 		regs.R_AH = pci_get_bus(dev);
288 		regs.R_AL = (pci_get_slot(dev) << 3) |
289 		    (pci_get_function(dev) & 0x07);
290 	}
291 	regs.R_DL = 0x80;
292 	x86bios_call(&regs, X86BIOS_PHYSTOSEG(vesa_bios_offs + 3),
293 	    X86BIOS_PHYSTOOFF(vesa_bios_offs + 3));
294 
295 	if (x86bios_get_intr(0x10) == 0)
296 		return (1);
297 
298 	return (0);
299 }
300 
301 /* VESA BIOS calls */
302 static int
303 vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags)
304 {
305 	x86regs_t regs;
306 	uint32_t offs;
307 	void *buf;
308 
309 	buf = x86bios_alloc(&offs, sizeof(*vmode), flags);
310 	if (buf == NULL)
311 		return (1);
312 
313 	x86bios_init_regs(&regs);
314 	regs.R_AX = 0x4f01;
315 	regs.R_CX = mode;
316 
317 	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
318 	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
319 
320 	x86bios_intr(&regs, 0x10);
321 
322 	if (regs.R_AX != 0x004f) {
323 		x86bios_free(buf, sizeof(*vmode));
324 		return (1);
325 	}
326 
327 	bcopy(buf, vmode, sizeof(*vmode));
328 	x86bios_free(buf, sizeof(*vmode));
329 
330 	return (0);
331 }
332 
333 static int
334 vesa_bios_set_mode(int mode)
335 {
336 	x86regs_t regs;
337 
338 	x86bios_init_regs(&regs);
339 	regs.R_AX = 0x4f02;
340 	regs.R_BX = mode;
341 
342 	x86bios_intr(&regs, 0x10);
343 
344 	return (regs.R_AX != 0x004f);
345 }
346 
347 #if 0
348 static int
349 vesa_bios_get_dac(void)
350 {
351 	x86regs_t regs;
352 
353 	x86bios_init_regs(&regs);
354 	regs.R_AX = 0x4f08;
355 	regs.R_BL = 1;
356 
357 	x86bios_intr(&regs, 0x10);
358 
359 	if (regs.R_AX != 0x004f)
360 		return (6);
361 
362 	return (regs.R_BH);
363 }
364 #endif
365 
366 static int
367 vesa_bios_set_dac(int bits)
368 {
369 	x86regs_t regs;
370 
371 	x86bios_init_regs(&regs);
372 	regs.R_AX = 0x4f08;
373 	/* regs.R_BL = 0; */
374 	regs.R_BH = bits;
375 
376 	x86bios_intr(&regs, 0x10);
377 
378 	if (regs.R_AX != 0x004f)
379 		return (6);
380 
381 	return (regs.R_BH);
382 }
383 
384 static int
385 vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
386 {
387 	x86regs_t regs;
388 	int i;
389 
390 	x86bios_init_regs(&regs);
391 	regs.R_AX = 0x4f09;
392 	regs.R_BL = 1;
393 	regs.R_CX = colors;
394 	regs.R_DX = start;
395 
396 	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
397 	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
398 
399 	bits = 8 - bits;
400 	mtx_lock(&vesa_lock);
401 	x86bios_intr(&regs, 0x10);
402 	if (regs.R_AX != 0x004f) {
403 		mtx_unlock(&vesa_lock);
404 		return (1);
405 	}
406 	for (i = 0; i < colors; ++i) {
407 		palette[i * 3] = vesa_palette[i * 4 + 2] << bits;
408 		palette[i * 3 + 1] = vesa_palette[i * 4 + 1] << bits;
409 		palette[i * 3 + 2] = vesa_palette[i * 4] << bits;
410 	}
411 	mtx_unlock(&vesa_lock);
412 
413 	return (0);
414 }
415 
416 static int
417 vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
418 			int bits)
419 {
420 	x86regs_t regs;
421 	int i;
422 
423 	x86bios_init_regs(&regs);
424 	regs.R_AX = 0x4f09;
425 	regs.R_BL = 1;
426 	regs.R_CX = colors;
427 	regs.R_DX = start;
428 
429 	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
430 	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
431 
432 	bits = 8 - bits;
433 	mtx_lock(&vesa_lock);
434 	x86bios_intr(&regs, 0x10);
435 	if (regs.R_AX != 0x004f) {
436 		mtx_unlock(&vesa_lock);
437 		return (1);
438 	}
439 	for (i = 0; i < colors; ++i) {
440 		r[i] = vesa_palette[i * 4 + 2] << bits;
441 		g[i] = vesa_palette[i * 4 + 1] << bits;
442 		b[i] = vesa_palette[i * 4] << bits;
443 	}
444 	mtx_unlock(&vesa_lock);
445 
446 	return (0);
447 }
448 
449 static int
450 vesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
451 {
452 	x86regs_t regs;
453 	int i;
454 
455 	x86bios_init_regs(&regs);
456 	regs.R_AX = 0x4f09;
457 	/* regs.R_BL = 0; */
458 	regs.R_CX = colors;
459 	regs.R_DX = start;
460 
461 	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
462 	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
463 
464 	bits = 8 - bits;
465 	mtx_lock(&vesa_lock);
466 	for (i = 0; i < colors; ++i) {
467 		vesa_palette[i * 4] = palette[i * 3 + 2] >> bits;
468 		vesa_palette[i * 4 + 1] = palette[i * 3 + 1] >> bits;
469 		vesa_palette[i * 4 + 2] = palette[i * 3] >> bits;
470 		vesa_palette[i * 4 + 3] = 0;
471 	}
472 	x86bios_intr(&regs, 0x10);
473 	mtx_unlock(&vesa_lock);
474 
475 	return (regs.R_AX != 0x004f);
476 }
477 
478 static int
479 vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
480 			int bits)
481 {
482 	x86regs_t regs;
483 	int i;
484 
485 	x86bios_init_regs(&regs);
486 	regs.R_AX = 0x4f09;
487 	/* regs.R_BL = 0; */
488 	regs.R_CX = colors;
489 	regs.R_DX = start;
490 
491 	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
492 	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
493 
494 	bits = 8 - bits;
495 	mtx_lock(&vesa_lock);
496 	for (i = 0; i < colors; ++i) {
497 		vesa_palette[i * 4] = b[i] >> bits;
498 		vesa_palette[i * 4 + 1] = g[i] >> bits;
499 		vesa_palette[i * 4 + 2] = r[i] >> bits;
500 		vesa_palette[i * 4 + 3] = 0;
501 	}
502 	x86bios_intr(&regs, 0x10);
503 	mtx_unlock(&vesa_lock);
504 
505 	return (regs.R_AX != 0x004f);
506 }
507 
508 static size_t
509 vesa_bios_state_buf_size(int state)
510 {
511 	x86regs_t regs;
512 
513 	x86bios_init_regs(&regs);
514 	regs.R_AX = 0x4f04;
515 	/* regs.R_DL = STATE_SIZE; */
516 	regs.R_CX = state;
517 
518 	x86bios_intr(&regs, 0x10);
519 
520 	if (regs.R_AX != 0x004f)
521 		return (0);
522 
523 	return (regs.R_BX * 64);
524 }
525 
526 static int
527 vesa_bios_save_restore(int code, void *p)
528 {
529 	x86regs_t regs;
530 
531 	if (code != STATE_SAVE && code != STATE_LOAD)
532 		return (1);
533 
534 	x86bios_init_regs(&regs);
535 	regs.R_AX = 0x4f04;
536 	regs.R_DL = code;
537 	regs.R_CX = vesa_state;
538 
539 	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_state_buf_offs);
540 	regs.R_BX = X86BIOS_PHYSTOOFF(vesa_state_buf_offs);
541 
542 	mtx_lock(&vesa_lock);
543 	switch (code) {
544 	case STATE_SAVE:
545 		x86bios_intr(&regs, 0x10);
546 		if (regs.R_AX == 0x004f)
547 			bcopy(vesa_state_buf, p, vesa_state_buf_size);
548 		break;
549 	case STATE_LOAD:
550 		bcopy(p, vesa_state_buf, vesa_state_buf_size);
551 		x86bios_intr(&regs, 0x10);
552 		break;
553 	}
554 	mtx_unlock(&vesa_lock);
555 
556 	return (regs.R_AX != 0x004f);
557 }
558 
559 #ifdef MODE_TABLE_BROKEN
560 static int
561 vesa_bios_get_line_length(void)
562 {
563 	x86regs_t regs;
564 
565 	x86bios_init_regs(&regs);
566 	regs.R_AX = 0x4f06;
567 	regs.R_BL = 1;
568 
569 	x86bios_intr(&regs, 0x10);
570 
571 	if (regs.R_AX != 0x004f)
572 		return (-1);
573 
574 	return (regs.R_BX);
575 }
576 #endif
577 
578 static int
579 vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
580 {
581 	x86regs_t regs;
582 
583 	x86bios_init_regs(&regs);
584 	regs.R_AX = 0x4f06;
585 	/* regs.R_BL = 0; */
586 	regs.R_CX = pixel;
587 
588 	x86bios_intr(&regs, 0x10);
589 
590 #if VESA_DEBUG > 1
591 	printf("bx:%d, cx:%d, dx:%d\n", regs.R_BX, regs.R_CX, regs.R_DX);
592 #endif
593 	if (regs.R_AX != 0x004f)
594 		return (-1);
595 
596 	if (bytes != NULL)
597 		*bytes = regs.R_BX;
598 	if (lines != NULL)
599 		*lines = regs.R_DX;
600 
601 	return (0);
602 }
603 
604 #if 0
605 static int
606 vesa_bios_get_start(int *x, int *y)
607 {
608 	x86regs_t regs;
609 
610 	x86bios_init_regs(&regs);
611 	regs.R_AX = 0x4f07;
612 	regs.R_BL = 1;
613 
614 	x86bios_intr(&regs, 0x10);
615 
616 	if (regs.R_AX != 0x004f)
617 		return (-1);
618 
619 	*x = regs.R_CX;
620 	*y = regs.R_DX;
621 
622 	return (0);
623 }
624 #endif
625 
626 static int
627 vesa_bios_set_start(int x, int y)
628 {
629 	x86regs_t regs;
630 
631 	x86bios_init_regs(&regs);
632 	regs.R_AX = 0x4f07;
633 	regs.R_BL = 0x80;
634 	regs.R_CX = x;
635 	regs.R_DX = y;
636 
637 	x86bios_intr(&regs, 0x10);
638 
639 	return (regs.R_AX != 0x004f);
640 }
641 
642 /* map a generic video mode to a known mode */
643 static int
644 vesa_map_gen_mode_num(int type, int color, int mode)
645 {
646     static struct {
647 	int from;
648 	int to;
649     } mode_map[] = {
650 	{ M_TEXT_132x25, M_VESA_C132x25 },
651 	{ M_TEXT_132x43, M_VESA_C132x43 },
652 	{ M_TEXT_132x50, M_VESA_C132x50 },
653 	{ M_TEXT_132x60, M_VESA_C132x60 },
654     };
655     int i;
656 
657     for (i = 0; i < nitems(mode_map); ++i) {
658         if (mode_map[i].from == mode)
659             return (mode_map[i].to);
660     }
661     return (mode);
662 }
663 
664 static int
665 vesa_translate_flags(u_int16_t vflags)
666 {
667 	static struct {
668 		u_int16_t mask;
669 		int set;
670 		int reset;
671 	} ftable[] = {
672 		{ V_MODECOLOR, V_INFO_COLOR, 0 },
673 		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
674 		{ V_MODELFB, V_INFO_LINEAR, 0 },
675 		{ V_MODENONVGA, V_INFO_NONVGA, 0 },
676 	};
677 	int flags;
678 	int i;
679 
680 	for (flags = 0, i = 0; i < nitems(ftable); ++i) {
681 		flags |= (vflags & ftable[i].mask) ?
682 			 ftable[i].set : ftable[i].reset;
683 	}
684 	return (flags);
685 }
686 
687 static int
688 vesa_translate_mmodel(u_int8_t vmodel)
689 {
690 	static struct {
691 		u_int8_t vmodel;
692 		int mmodel;
693 	} mtable[] = {
694 		{ V_MMTEXT,	V_INFO_MM_TEXT },
695 		{ V_MMCGA,	V_INFO_MM_CGA },
696 		{ V_MMHGC,	V_INFO_MM_HGC },
697 		{ V_MMEGA,	V_INFO_MM_PLANAR },
698 		{ V_MMPACKED,	V_INFO_MM_PACKED },
699 		{ V_MMDIRCOLOR,	V_INFO_MM_DIRECT },
700 	};
701 	int i;
702 
703 	for (i = 0; mtable[i].mmodel >= 0; ++i) {
704 		if (mtable[i].vmodel == vmodel)
705 			return (mtable[i].mmodel);
706 	}
707 	return (V_INFO_MM_OTHER);
708 }
709 
710 static int
711 vesa_get_bpscanline(struct vesa_mode *vmode)
712 {
713 	int bpsl;
714 
715 	if ((vmode->v_modeattr & V_MODEGRAPHICS) != 0) {
716 		/* Find the minimum length. */
717 		switch (vmode->v_bpp / vmode->v_planes) {
718 		case 1:
719 			bpsl = vmode->v_width / 8;
720 			break;
721 		case 2:
722 			bpsl = vmode->v_width / 4;
723 			break;
724 		case 4:
725 			bpsl = vmode->v_width / 2;
726 			break;
727 		default:
728 			bpsl = vmode->v_width * ((vmode->v_bpp + 7) / 8);
729 			bpsl /= vmode->v_planes;
730 			break;
731 		}
732 
733 		/* Use VBE 3.0 information if it looks sane. */
734 		if ((vmode->v_modeattr & V_MODELFB) != 0 &&
735 		    vesa_adp_info->v_version >= 0x0300 &&
736 		    vmode->v_linbpscanline > bpsl)
737 			return (vmode->v_linbpscanline);
738 
739 		/* Return the minimum if the mode table looks absurd. */
740 		if (vmode->v_bpscanline < bpsl)
741 			return (bpsl);
742 	}
743 
744 	return (vmode->v_bpscanline);
745 }
746 
747 #define	VESA_MAXSTR		256
748 
749 #define	VESA_STRCPY(dst, src)	do {				\
750 	char *str;						\
751 	int i;							\
752 	dst = malloc(VESA_MAXSTR, M_DEVBUF, M_WAITOK);		\
753 	str = x86bios_offset(BIOS_SADDRTOLADDR(src));		\
754 	for (i = 0; i < VESA_MAXSTR - 1 && str[i] != '\0'; i++)	\
755 		dst[i] = str[i];				\
756 	dst[i] = '\0';						\
757 } while (0)
758 
759 static int
760 vesa_bios_init(void)
761 {
762 	struct vesa_mode vmode;
763 	struct vesa_info *buf;
764 	video_info_t *p;
765 	x86regs_t regs;
766 	size_t bsize;
767 	size_t msize;
768 	void *vmbuf;
769 	uint8_t *vbios;
770 	uint32_t offs;
771 	uint16_t vers;
772 	int is_via_cle266;
773 	int modes;
774 	int i;
775 
776 	if (vesa_init_done)
777 		return (0);
778 
779 	vesa_bios_offs = VESA_BIOS_OFFSET;
780 
781 	/*
782 	 * If the VBE real mode interrupt vector is not found, try BIOS POST.
783 	 */
784 	vesa_bios_int10 = x86bios_get_intr(0x10);
785 	if (vesa_bios_int10 == 0) {
786 		if (vesa_bios_post() != 0)
787 			return (1);
788 		vesa_bios_int10 = x86bios_get_intr(0x10);
789 		if (vesa_bios_int10 == 0)
790 			return (1);
791 	}
792 
793 	/*
794 	 * Shadow video ROM.
795 	 */
796 	offs = vesa_bios_int10;
797 	if (vesa_shadow_rom) {
798 		vbios = x86bios_get_orm(vesa_bios_offs);
799 		if (vbios != NULL) {
800 			vesa_bios_size = vbios[2] * 512;
801 			if (((VESA_BIOS_OFFSET << 12) & 0xffff0000) ==
802 			    (vesa_bios_int10 & 0xffff0000) &&
803 			    vesa_bios_size > (vesa_bios_int10 & 0xffff)) {
804 				vesa_bios = x86bios_alloc(&vesa_bios_offs,
805 				    vesa_bios_size, M_WAITOK);
806 				bcopy(vbios, vesa_bios, vesa_bios_size);
807 				offs = ((vesa_bios_offs << 12) & 0xffff0000) +
808 				    (vesa_bios_int10 & 0xffff);
809 				x86bios_set_intr(0x10, offs);
810 			}
811 		}
812 		if (vesa_bios == NULL)
813 			printf("VESA: failed to shadow video ROM\n");
814 	}
815 	if (bootverbose)
816 		printf("VESA: INT 0x10 vector 0x%04x:0x%04x\n",
817 		    (offs >> 16) & 0xffff, offs & 0xffff);
818 
819 	x86bios_init_regs(&regs);
820 	regs.R_AX = 0x4f00;
821 
822 	vmbuf = x86bios_alloc(&offs, sizeof(*buf), M_WAITOK);
823 
824 	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
825 	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
826 
827 	bcopy("VBE2", vmbuf, 4);	/* try for VBE2 data */
828 	x86bios_intr(&regs, 0x10);
829 
830 	if (regs.R_AX != 0x004f || bcmp("VESA", vmbuf, 4) != 0)
831 		goto fail;
832 
833 	vesa_adp_info = buf = malloc(sizeof(*buf), M_DEVBUF, M_WAITOK);
834 	bcopy(vmbuf, buf, sizeof(*buf));
835 
836 	if (bootverbose) {
837 		printf("VESA: information block\n");
838 		hexdump(buf, sizeof(*buf), NULL, HD_OMIT_CHARS);
839 	}
840 
841 	vers = buf->v_version = le16toh(buf->v_version);
842 	buf->v_oemstr = le32toh(buf->v_oemstr);
843 	buf->v_flags = le32toh(buf->v_flags);
844 	buf->v_modetable = le32toh(buf->v_modetable);
845 	buf->v_memsize = le16toh(buf->v_memsize);
846 	buf->v_revision = le16toh(buf->v_revision);
847 	buf->v_venderstr = le32toh(buf->v_venderstr);
848 	buf->v_prodstr = le32toh(buf->v_prodstr);
849 	buf->v_revstr = le32toh(buf->v_revstr);
850 
851 	if (vers < 0x0102) {
852 		printf("VESA: VBE version %d.%d is not supported; "
853 		    "version 1.2 or later is required.\n",
854 		    ((vers & 0xf000) >> 12) * 10 + ((vers & 0x0f00) >> 8),
855 		    ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f));
856 		goto fail;
857 	}
858 
859 	VESA_STRCPY(vesa_oemstr, buf->v_oemstr);
860 	if (vers >= 0x0200) {
861 		VESA_STRCPY(vesa_venderstr, buf->v_venderstr);
862 		VESA_STRCPY(vesa_prodstr, buf->v_prodstr);
863 		VESA_STRCPY(vesa_revstr, buf->v_revstr);
864 	}
865 	is_via_cle266 = strncmp(vesa_oemstr, VESA_VIA_CLE266,
866 	    sizeof(VESA_VIA_CLE266)) == 0;
867 
868 	if (buf->v_modetable == 0)
869 		goto fail;
870 
871 	msize = (size_t)buf->v_memsize * 64 * 1024;
872 
873 	vesa_vmodetab = x86bios_offset(BIOS_SADDRTOLADDR(buf->v_modetable));
874 
875 	for (i = 0, modes = 0; (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) &&
876 	    (vesa_vmodetab[i] != 0xffff); ++i) {
877 		vesa_vmodetab[i] = le16toh(vesa_vmodetab[i]);
878 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_WAITOK))
879 			continue;
880 
881 		vmode.v_modeattr = le16toh(vmode.v_modeattr);
882 		vmode.v_wgran = le16toh(vmode.v_wgran);
883 		vmode.v_wsize = le16toh(vmode.v_wsize);
884 		vmode.v_waseg = le16toh(vmode.v_waseg);
885 		vmode.v_wbseg = le16toh(vmode.v_wbseg);
886 		vmode.v_posfunc = le32toh(vmode.v_posfunc);
887 		vmode.v_bpscanline = le16toh(vmode.v_bpscanline);
888 		vmode.v_width = le16toh(vmode.v_width);
889 		vmode.v_height = le16toh(vmode.v_height);
890 		vmode.v_lfb = le32toh(vmode.v_lfb);
891 		vmode.v_offscreen = le32toh(vmode.v_offscreen);
892 		vmode.v_offscreensize = le16toh(vmode.v_offscreensize);
893 		vmode.v_linbpscanline = le16toh(vmode.v_linbpscanline);
894 		vmode.v_maxpixelclock = le32toh(vmode.v_maxpixelclock);
895 
896 		/* reject unsupported modes */
897 #if 0
898 		if ((vmode.v_modeattr &
899 		    (V_MODESUPP | V_MODEOPTINFO | V_MODENONVGA)) !=
900 		    (V_MODESUPP | V_MODEOPTINFO))
901 			continue;
902 #else
903 		if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
904 #if VESA_DEBUG > 1
905 			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
906 			    " attr = %x\n",
907 			    vmode.v_modeattr & V_MODEGRAPHICS ?
908 			    "graphics" : "text",
909 			    vmode.v_width, vmode.v_height, vmode.v_bpp,
910 			    vmode.v_modeattr);
911 #endif
912 			continue;
913 		}
914 #endif
915 
916 		bsize = vesa_get_bpscanline(&vmode) * vmode.v_height;
917 		if ((vmode.v_modeattr & V_MODEGRAPHICS) != 0)
918 			bsize *= vmode.v_planes;
919 
920 		/* Does it have enough memory to support this mode? */
921 		if (msize < bsize) {
922 #if VESA_DEBUG > 1
923 			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
924 			    " attr = %x, not enough memory\n",
925 			    vmode.v_modeattr & V_MODEGRAPHICS ?
926 			    "graphics" : "text",
927 			    vmode.v_width, vmode.v_height, vmode.v_bpp,
928 			    vmode.v_modeattr);
929 #endif
930 			continue;
931 		}
932 		if (bsize > vesa_vmem_max)
933 			vesa_vmem_max = bsize;
934 
935 		/* expand the array if necessary */
936 		if (modes >= vesa_vmode_max) {
937 			vesa_vmode_max += MODE_TABLE_DELTA;
938 			p = malloc(sizeof(*vesa_vmode) * (vesa_vmode_max + 1),
939 			    M_DEVBUF, M_WAITOK);
940 #if VESA_DEBUG > 1
941 			printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
942 			    modes, vesa_vmode_max);
943 #endif
944 			if (modes > 0) {
945 				bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
946 				free(vesa_vmode, M_DEVBUF);
947 			}
948 			vesa_vmode = p;
949 		}
950 
951 #if VESA_DEBUG > 1
952 		printf("Found VESA %s mode: %d x %d x %d bpp\n",
953 		    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
954 		    vmode.v_width, vmode.v_height, vmode.v_bpp);
955 #endif
956 		if (is_via_cle266) {
957 		    if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
958 			vmode.v_width &= 0xff;
959 			vmode.v_waseg = 0xb8000 >> 4;
960 		    }
961 		}
962 
963 		/* copy some fields */
964 		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
965 		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
966 		vesa_vmode[modes].vi_width = vmode.v_width;
967 		vesa_vmode[modes].vi_height = vmode.v_height;
968 		vesa_vmode[modes].vi_depth = vmode.v_bpp;
969 		vesa_vmode[modes].vi_planes = vmode.v_planes;
970 		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
971 		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
972 		vesa_vmode[modes].vi_window = (vm_offset_t)vmode.v_waseg << 4;
973 		/* XXX window B */
974 		vesa_vmode[modes].vi_window_size = vmode.v_wsize * 1024;
975 		vesa_vmode[modes].vi_window_gran = vmode.v_wgran * 1024;
976 		if (vmode.v_modeattr & V_MODELFB)
977 			vesa_vmode[modes].vi_buffer = vmode.v_lfb;
978 		vesa_vmode[modes].vi_buffer_size = bsize;
979 		vesa_vmode[modes].vi_mem_model =
980 		    vesa_translate_mmodel(vmode.v_memmodel);
981 		switch (vesa_vmode[modes].vi_mem_model) {
982 		case V_INFO_MM_DIRECT:
983 			if ((vmode.v_modeattr & V_MODELFB) != 0 &&
984 			    vers >= 0x0300) {
985 				vesa_vmode[modes].vi_pixel_fields[0] =
986 				    vmode.v_linredfieldpos;
987 				vesa_vmode[modes].vi_pixel_fields[1] =
988 				    vmode.v_lingreenfieldpos;
989 				vesa_vmode[modes].vi_pixel_fields[2] =
990 				    vmode.v_linbluefieldpos;
991 				vesa_vmode[modes].vi_pixel_fields[3] =
992 				    vmode.v_linresfieldpos;
993 				vesa_vmode[modes].vi_pixel_fsizes[0] =
994 				    vmode.v_linredmasksize;
995 				vesa_vmode[modes].vi_pixel_fsizes[1] =
996 				    vmode.v_lingreenmasksize;
997 				vesa_vmode[modes].vi_pixel_fsizes[2] =
998 				    vmode.v_linbluemasksize;
999 				vesa_vmode[modes].vi_pixel_fsizes[3] =
1000 				    vmode.v_linresmasksize;
1001 			} else {
1002 				vesa_vmode[modes].vi_pixel_fields[0] =
1003 				    vmode.v_redfieldpos;
1004 				vesa_vmode[modes].vi_pixel_fields[1] =
1005 				    vmode.v_greenfieldpos;
1006 				vesa_vmode[modes].vi_pixel_fields[2] =
1007 				    vmode.v_bluefieldpos;
1008 				vesa_vmode[modes].vi_pixel_fields[3] =
1009 				    vmode.v_resfieldpos;
1010 				vesa_vmode[modes].vi_pixel_fsizes[0] =
1011 				    vmode.v_redmasksize;
1012 				vesa_vmode[modes].vi_pixel_fsizes[1] =
1013 				    vmode.v_greenmasksize;
1014 				vesa_vmode[modes].vi_pixel_fsizes[2] =
1015 				    vmode.v_bluemasksize;
1016 				vesa_vmode[modes].vi_pixel_fsizes[3] =
1017 				    vmode.v_resmasksize;
1018 			}
1019 			/* FALLTHROUGH */
1020 		case V_INFO_MM_PACKED:
1021 			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7) / 8;
1022 			break;
1023 		}
1024 		vesa_vmode[modes].vi_flags =
1025 		    vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
1026 
1027 		++modes;
1028 	}
1029 	if (vesa_vmode != NULL)
1030 		vesa_vmode[modes].vi_mode = EOT;
1031 
1032 	if (bootverbose)
1033 		printf("VESA: %d mode(s) found\n", modes);
1034 
1035 	if (modes == 0)
1036 		goto fail;
1037 
1038 	x86bios_free(vmbuf, sizeof(*buf));
1039 
1040 	/* Probe supported save/restore states. */
1041 	for (i = 0; i < 4; i++)
1042 		if (vesa_bios_state_buf_size(1 << i) > 0)
1043 			vesa_state |= 1 << i;
1044 	if (vesa_state != 0)
1045 		vesa_state_buf_size = vesa_bios_state_buf_size(vesa_state);
1046 	vesa_palette = x86bios_alloc(&vesa_palette_offs,
1047 	    VESA_PALETTE_SIZE + vesa_state_buf_size, M_WAITOK);
1048 	if (vesa_state_buf_size > 0) {
1049 		vesa_state_buf = vesa_palette + VESA_PALETTE_SIZE;
1050 		vesa_state_buf_offs = vesa_palette_offs + VESA_PALETTE_SIZE;
1051 	}
1052 
1053 	return (0);
1054 
1055 fail:
1056 	x86bios_free(vmbuf, sizeof(buf));
1057 	vesa_bios_uninit();
1058 	return (1);
1059 }
1060 
1061 static void
1062 vesa_bios_uninit(void)
1063 {
1064 
1065 	if (vesa_bios != NULL) {
1066 		x86bios_set_intr(0x10, vesa_bios_int10);
1067 		vesa_bios_offs = VESA_BIOS_OFFSET;
1068 		x86bios_free(vesa_bios, vesa_bios_size);
1069 		vesa_bios = NULL;
1070 	}
1071 	if (vesa_adp_info != NULL) {
1072 		free(vesa_adp_info, M_DEVBUF);
1073 		vesa_adp_info = NULL;
1074 	}
1075 	if (vesa_oemstr != NULL) {
1076 		free(vesa_oemstr, M_DEVBUF);
1077 		vesa_oemstr = NULL;
1078 	}
1079 	if (vesa_venderstr != NULL) {
1080 		free(vesa_venderstr, M_DEVBUF);
1081 		vesa_venderstr = NULL;
1082 	}
1083 	if (vesa_prodstr != NULL) {
1084 		free(vesa_prodstr, M_DEVBUF);
1085 		vesa_prodstr = NULL;
1086 	}
1087 	if (vesa_revstr != NULL) {
1088 		free(vesa_revstr, M_DEVBUF);
1089 		vesa_revstr = NULL;
1090 	}
1091 	if (vesa_vmode != NULL) {
1092 		free(vesa_vmode, M_DEVBUF);
1093 		vesa_vmode = NULL;
1094 	}
1095 	if (vesa_palette != NULL) {
1096 		x86bios_free(vesa_palette,
1097 		    VESA_PALETTE_SIZE + vesa_state_buf_size);
1098 		vesa_palette = NULL;
1099 	}
1100 }
1101 
1102 static void
1103 vesa_clear_modes(video_info_t *info, int color)
1104 {
1105 	while (info->vi_mode != EOT) {
1106 		if ((info->vi_flags & V_INFO_COLOR) != color)
1107 			info->vi_mode = NA;
1108 		++info;
1109 	}
1110 }
1111 
1112 /* entry points */
1113 
1114 static int
1115 vesa_configure(int flags)
1116 {
1117 	video_adapter_t *adp;
1118 	int adapters;
1119 	int error;
1120 	int i;
1121 
1122 	if (vesa_init_done)
1123 		return (0);
1124 	if (flags & VIO_PROBE_ONLY)
1125 		return (0);
1126 
1127 	/*
1128 	 * If the VESA module has already been loaded, abort loading
1129 	 * the module this time.
1130 	 */
1131 	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
1132 		if (adp->va_flags & V_ADP_VESA)
1133 			return (ENXIO);
1134 		if (adp->va_type == KD_VGA)
1135 			break;
1136 	}
1137 
1138 	/*
1139 	 * The VGA adapter is not found.  This is because either
1140 	 * 1) the VGA driver has not been initialized, or 2) the VGA card
1141 	 * is not present.  If 1) is the case, we shall defer
1142 	 * initialization for now and try again later.
1143 	 */
1144 	if (adp == NULL) {
1145 		vga_sub_configure = vesa_configure;
1146 		return (ENODEV);
1147 	}
1148 
1149 	/* count number of registered adapters */
1150 	for (++i; vid_get_adapter(i) != NULL; ++i)
1151 		;
1152 	adapters = i;
1153 
1154 	/* call VESA BIOS */
1155 	vesa_adp = adp;
1156 	if (vesa_bios_init()) {
1157 		vesa_adp = NULL;
1158 		return (ENXIO);
1159 	}
1160 	vesa_adp->va_flags |= V_ADP_VESA;
1161 
1162 	/* remove conflicting modes if we have more than one adapter */
1163 	if (adapters > 1) {
1164 		vesa_clear_modes(vesa_vmode,
1165 				 (vesa_adp->va_flags & V_ADP_COLOR) ?
1166 				     V_INFO_COLOR : 0);
1167 	}
1168 
1169 	if ((error = vesa_load_ioctl()) == 0) {
1170 		prevvidsw = vidsw[vesa_adp->va_index];
1171 		vidsw[vesa_adp->va_index] = &vesavidsw;
1172 		vesa_init_done = TRUE;
1173 	} else {
1174 		vesa_adp = NULL;
1175 		return (error);
1176 	}
1177 
1178 	return (0);
1179 }
1180 
1181 #if 0
1182 static int
1183 vesa_nop(void)
1184 {
1185 
1186 	return (0);
1187 }
1188 #endif
1189 
1190 static int
1191 vesa_error(void)
1192 {
1193 
1194 	return (1);
1195 }
1196 
1197 static int
1198 vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1199 {
1200 
1201 	return ((*prevvidsw->probe)(unit, adpp, arg, flags));
1202 }
1203 
1204 static int
1205 vesa_init(int unit, video_adapter_t *adp, int flags)
1206 {
1207 
1208 	return ((*prevvidsw->init)(unit, adp, flags));
1209 }
1210 
1211 static int
1212 vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1213 {
1214 	int i;
1215 
1216 	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
1217 		return (0);
1218 
1219 	if (adp != vesa_adp)
1220 		return (1);
1221 
1222 	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
1223 				     vesa_adp->va_flags & V_ADP_COLOR, mode);
1224 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1225 		if (vesa_vmode[i].vi_mode == NA)
1226 			continue;
1227 		if (vesa_vmode[i].vi_mode == mode) {
1228 			*info = vesa_vmode[i];
1229 			return (0);
1230 		}
1231 	}
1232 	return (1);
1233 }
1234 
1235 static int
1236 vesa_query_mode(video_adapter_t *adp, video_info_t *info)
1237 {
1238 	int i;
1239 
1240 	if ((*prevvidsw->query_mode)(adp, info) == 0)
1241 		return (0);
1242 	if (adp != vesa_adp)
1243 		return (ENODEV);
1244 
1245 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1246 		if ((info->vi_width != 0)
1247 		    && (info->vi_width != vesa_vmode[i].vi_width))
1248 			continue;
1249 		if ((info->vi_height != 0)
1250 		    && (info->vi_height != vesa_vmode[i].vi_height))
1251 			continue;
1252 		if ((info->vi_cwidth != 0)
1253 		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
1254 			continue;
1255 		if ((info->vi_cheight != 0)
1256 		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
1257 			continue;
1258 		if ((info->vi_depth != 0)
1259 		    && (info->vi_depth != vesa_vmode[i].vi_depth))
1260 			continue;
1261 		if ((info->vi_planes != 0)
1262 		    && (info->vi_planes != vesa_vmode[i].vi_planes))
1263 			continue;
1264 		/* pixel format, memory model */
1265 		if ((info->vi_flags != 0)
1266 		    && (info->vi_flags != vesa_vmode[i].vi_flags))
1267 			continue;
1268 		*info = vesa_vmode[i];
1269 		return (0);
1270 	}
1271 	return (ENODEV);
1272 }
1273 
1274 static int
1275 vesa_set_mode(video_adapter_t *adp, int mode)
1276 {
1277 	video_info_t info;
1278 
1279 	if (adp != vesa_adp)
1280 		return ((*prevvidsw->set_mode)(adp, mode));
1281 
1282 	mode = vesa_map_gen_mode_num(adp->va_type,
1283 				     adp->va_flags & V_ADP_COLOR, mode);
1284 #if VESA_DEBUG > 0
1285 	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1286 		adp->va_mode, adp->va_mode, mode, mode);
1287 #endif
1288 	/*
1289 	 * If the current mode is a VESA mode and the new mode is not,
1290 	 * restore the state of the adapter first by setting one of the
1291 	 * standard VGA mode, so that non-standard, extended SVGA registers
1292 	 * are set to the state compatible with the standard VGA modes.
1293 	 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1294 	 * the new mode correctly.
1295 	 */
1296 	if (VESA_MODE(adp->va_mode)) {
1297 		if (!VESA_MODE(mode) &&
1298 		    (*prevvidsw->get_info)(adp, mode, &info) == 0) {
1299 			if ((adp->va_flags & V_ADP_DAC8) != 0) {
1300 				vesa_bios_set_dac(6);
1301 				adp->va_flags &= ~V_ADP_DAC8;
1302 			}
1303 			int10_set_mode(adp->va_initial_bios_mode);
1304 			if (adp->va_info.vi_flags & V_INFO_LINEAR)
1305 				pmap_unmapdev(adp->va_buffer, vesa_vmem_max);
1306 			/*
1307 			 * Once (*prevvidsw->get_info)() succeeded,
1308 			 * (*prevvidsw->set_mode)() below won't fail...
1309 			 */
1310 		}
1311 	}
1312 
1313 	/* we may not need to handle this mode after all... */
1314 	if (!VESA_MODE(mode) && (*prevvidsw->set_mode)(adp, mode) == 0)
1315 		return (0);
1316 
1317 	/* is the new mode supported? */
1318 	if (vesa_get_info(adp, mode, &info))
1319 		return (1);
1320 	/* assert(VESA_MODE(mode)); */
1321 
1322 #if VESA_DEBUG > 0
1323 	printf("VESA: about to set a VESA mode...\n");
1324 #endif
1325 	/*
1326 	 * The mode change should reset the palette format to 6 bits, so
1327 	 * we must reset V_ADP_DAC8.  Some BIOSes do an incomplete reset
1328 	 * if we call them with an 8-bit palette, so reset directly.
1329 	 */
1330 	if (adp->va_flags & V_ADP_DAC8) {
1331 	    vesa_bios_set_dac(6);
1332 	    adp->va_flags &= ~V_ADP_DAC8;
1333 	}
1334 
1335 	/* don't use the linear frame buffer for text modes. XXX */
1336 	if (!(info.vi_flags & V_INFO_GRAPHICS))
1337 		info.vi_flags &= ~V_INFO_LINEAR;
1338 
1339 	if ((info.vi_flags & V_INFO_LINEAR) != 0)
1340 		mode |= 0x4000;
1341 	if (vesa_bios_set_mode(mode | 0x8000))
1342 		return (1);
1343 
1344 	if ((vesa_adp_info->v_flags & V_DAC8) != 0 &&
1345 	    (info.vi_flags & V_INFO_GRAPHICS) != 0 &&
1346 	    vesa_bios_set_dac(8) > 6)
1347 		adp->va_flags |= V_ADP_DAC8;
1348 
1349 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1350 		pmap_unmapdev(adp->va_buffer, vesa_vmem_max);
1351 
1352 #if VESA_DEBUG > 0
1353 	printf("VESA: mode set!\n");
1354 #endif
1355 	vesa_adp->va_mode = mode & 0x1ff;	/* Mode number is 9-bit. */
1356 	vesa_adp->va_flags &= ~V_ADP_COLOR;
1357 	vesa_adp->va_flags |=
1358 		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1359 	vesa_adp->va_crtc_addr =
1360 		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1361 
1362 	vesa_adp->va_flags &= ~V_ADP_CWIDTH9;
1363 	vesa_adp->va_line_width = info.vi_buffer_size / info.vi_height;
1364 	if ((info.vi_flags & V_INFO_GRAPHICS) != 0)
1365 		vesa_adp->va_line_width /= info.vi_planes;
1366 
1367 #ifdef MODE_TABLE_BROKEN
1368 	/* If VBE function returns bigger bytes per scan line, use it. */
1369 	{
1370 		int bpsl = vesa_bios_get_line_length();
1371 		if (bpsl > vesa_adp->va_line_width) {
1372 			vesa_adp->va_line_width = bpsl;
1373 			info.vi_buffer_size = bpsl * info.vi_height;
1374 			if ((info.vi_flags & V_INFO_GRAPHICS) != 0)
1375 				info.vi_buffer_size *= info.vi_planes;
1376 		}
1377 	}
1378 #endif
1379 
1380 	if (info.vi_flags & V_INFO_LINEAR) {
1381 #if VESA_DEBUG > 1
1382 		printf("VESA: setting up LFB\n");
1383 #endif
1384 		vesa_adp->va_buffer =
1385 		    (vm_offset_t)pmap_mapdev_attr(info.vi_buffer,
1386 		    vesa_vmem_max, PAT_WRITE_COMBINING);
1387 		vesa_adp->va_window = vesa_adp->va_buffer;
1388 		vesa_adp->va_window_size = info.vi_buffer_size / info.vi_planes;
1389 		vesa_adp->va_window_gran = info.vi_buffer_size / info.vi_planes;
1390 	} else {
1391 		vesa_adp->va_buffer = 0;
1392 		vesa_adp->va_window = (vm_offset_t)x86bios_offset(info.vi_window);
1393 		vesa_adp->va_window_size = info.vi_window_size;
1394 		vesa_adp->va_window_gran = info.vi_window_gran;
1395 	}
1396 	vesa_adp->va_buffer_size = info.vi_buffer_size;
1397 	vesa_adp->va_window_orig = 0;
1398 	vesa_adp->va_disp_start.x = 0;
1399 	vesa_adp->va_disp_start.y = 0;
1400 #if VESA_DEBUG > 0
1401 	printf("vesa_set_mode(): vi_width:%d, line_width:%d\n",
1402 	       info.vi_width, vesa_adp->va_line_width);
1403 #endif
1404 	bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1405 
1406 	/* move hardware cursor out of the way */
1407 	(*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1408 
1409 	return (0);
1410 }
1411 
1412 static int
1413 vesa_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1414 	       u_char *data, int ch, int count)
1415 {
1416 
1417 	return ((*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data,
1418 	    ch, count));
1419 }
1420 
1421 static int
1422 vesa_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1423 	       u_char *data, int ch, int count)
1424 {
1425 
1426 	return ((*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data,
1427 		ch, count));
1428 }
1429 
1430 static int
1431 vesa_show_font(video_adapter_t *adp, int page)
1432 {
1433 
1434 	return ((*prevvidsw->show_font)(adp, page));
1435 }
1436 
1437 static int
1438 vesa_save_palette(video_adapter_t *adp, u_char *palette)
1439 {
1440 	int bits;
1441 
1442 	if (adp == vesa_adp && VESA_MODE(adp->va_mode)) {
1443 		bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1444 		if (vesa_bios_save_palette(0, 256, palette, bits) == 0)
1445 			return (0);
1446 	}
1447 
1448 	return ((*prevvidsw->save_palette)(adp, palette));
1449 }
1450 
1451 static int
1452 vesa_load_palette(video_adapter_t *adp, u_char *palette)
1453 {
1454 	int bits;
1455 
1456 	if (adp == vesa_adp && VESA_MODE(adp->va_mode)) {
1457 		bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1458 		if (vesa_bios_load_palette(0, 256, palette, bits) == 0)
1459 			return (0);
1460 	}
1461 
1462 	return ((*prevvidsw->load_palette)(adp, palette));
1463 }
1464 
1465 static int
1466 vesa_set_border(video_adapter_t *adp, int color)
1467 {
1468 
1469 	return ((*prevvidsw->set_border)(adp, color));
1470 }
1471 
1472 static int
1473 vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1474 {
1475 	void *buf;
1476 	size_t bsize;
1477 
1478 	if (adp != vesa_adp || (size == 0 && vesa_state_buf_size == 0))
1479 		return ((*prevvidsw->save_state)(adp, p, size));
1480 
1481 	bsize = offsetof(adp_state_t, regs) + vesa_state_buf_size;
1482 	if (size == 0)
1483 		return (bsize);
1484 	if (vesa_state_buf_size > 0 && size < bsize)
1485 		return (EINVAL);
1486 
1487 	if (vesa_vmem_buf != NULL) {
1488 		free(vesa_vmem_buf, M_DEVBUF);
1489 		vesa_vmem_buf = NULL;
1490 	}
1491 	if (VESA_MODE(adp->va_mode)) {
1492 		buf = (void *)adp->va_buffer;
1493 		if (buf != NULL) {
1494 			bsize = adp->va_buffer_size;
1495 			vesa_vmem_buf = malloc(bsize, M_DEVBUF, M_NOWAIT);
1496 			if (vesa_vmem_buf != NULL)
1497 				bcopy(buf, vesa_vmem_buf, bsize);
1498 		}
1499 	}
1500 	if (vesa_state_buf_size == 0)
1501 		return ((*prevvidsw->save_state)(adp, p, size));
1502 	((adp_state_t *)p)->sig = V_STATE_SIG;
1503 	return (vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs));
1504 }
1505 
1506 static int
1507 vesa_load_state(video_adapter_t *adp, void *p)
1508 {
1509 	void *buf;
1510 	size_t bsize;
1511 	int error, mode;
1512 
1513 	if (adp != vesa_adp)
1514 		return ((*prevvidsw->load_state)(adp, p));
1515 
1516 	/* Try BIOS POST to restore a sane state. */
1517 	(void)vesa_bios_post();
1518 	bsize = adp->va_buffer_size;
1519 	mode = adp->va_mode;
1520 	error = vesa_set_mode(adp, adp->va_initial_mode);
1521 	if (mode != adp->va_initial_mode)
1522 		error = vesa_set_mode(adp, mode);
1523 
1524 	if (vesa_vmem_buf != NULL) {
1525 		if (error == 0 && VESA_MODE(mode)) {
1526 			buf = (void *)adp->va_buffer;
1527 			if (buf != NULL)
1528 				bcopy(vesa_vmem_buf, buf, bsize);
1529 		}
1530 		free(vesa_vmem_buf, M_DEVBUF);
1531 		vesa_vmem_buf = NULL;
1532 	}
1533 	if (((adp_state_t *)p)->sig != V_STATE_SIG)
1534 		return ((*prevvidsw->load_state)(adp, p));
1535 	return (vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs));
1536 }
1537 
1538 #if 0
1539 static int
1540 vesa_get_origin(video_adapter_t *adp, off_t *offset)
1541 {
1542 	x86regs_t regs;
1543 
1544 	x86bios_init_regs(&regs);
1545 	regs.R_AX = 0x4f05;
1546 	regs.R_BL = 0x10;
1547 
1548 	x86bios_intr(&regs, 0x10);
1549 
1550 	if (regs.R_AX != 0x004f)
1551 		return (1);
1552 	*offset = regs.DX * adp->va_window_gran;
1553 
1554 	return (0);
1555 }
1556 #endif
1557 
1558 static int
1559 vesa_set_origin(video_adapter_t *adp, off_t offset)
1560 {
1561 	x86regs_t regs;
1562 
1563 	/*
1564 	 * This function should return as quickly as possible to
1565 	 * maintain good performance of the system. For this reason,
1566 	 * error checking is kept minimal and let the VESA BIOS to
1567 	 * detect error.
1568 	 */
1569 	if (adp != vesa_adp)
1570 		return ((*prevvidsw->set_win_org)(adp, offset));
1571 
1572 	/* if this is a linear frame buffer, do nothing */
1573 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1574 		return (0);
1575 	/* XXX */
1576 	if (adp->va_window_gran == 0)
1577 		return (1);
1578 
1579 	x86bios_init_regs(&regs);
1580 	regs.R_AX = 0x4f05;
1581 	regs.R_DX = offset / adp->va_window_gran;
1582 
1583 	x86bios_intr(&regs, 0x10);
1584 
1585 	if (regs.R_AX != 0x004f)
1586 		return (1);
1587 
1588 	x86bios_init_regs(&regs);
1589 	regs.R_AX = 0x4f05;
1590 	regs.R_BL = 1;
1591 	regs.R_DX = offset / adp->va_window_gran;
1592 	x86bios_intr(&regs, 0x10);
1593 
1594 	adp->va_window_orig = rounddown(offset, adp->va_window_gran);
1595 	return (0);			/* XXX */
1596 }
1597 
1598 static int
1599 vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1600 {
1601 
1602 	return ((*prevvidsw->read_hw_cursor)(adp, col, row));
1603 }
1604 
1605 static int
1606 vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1607 {
1608 
1609 	return ((*prevvidsw->set_hw_cursor)(adp, col, row));
1610 }
1611 
1612 static int
1613 vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1614 			 int celsize, int blink)
1615 {
1616 
1617 	return ((*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1618 	    blink));
1619 }
1620 
1621 static int
1622 vesa_blank_display(video_adapter_t *adp, int mode)
1623 {
1624 
1625 	/* XXX: use VESA DPMS */
1626 	return ((*prevvidsw->blank_display)(adp, mode));
1627 }
1628 
1629 static int
1630 vesa_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
1631 	  int prot, vm_memattr_t *memattr)
1632 {
1633 
1634 #if VESA_DEBUG > 0
1635 	printf("vesa_mmap(): window:0x%tx, buffer:0x%tx, offset:0x%jx\n",
1636 	       adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1637 #endif
1638 
1639 	if ((adp == vesa_adp) &&
1640 	    (adp->va_info.vi_flags & V_INFO_LINEAR) != 0) {
1641 		/* va_window_size == va_buffer_size/vi_planes */
1642 		/* XXX: is this correct? */
1643 		if (offset > adp->va_window_size - PAGE_SIZE)
1644 			return (-1);
1645 		*paddr = adp->va_info.vi_buffer + offset;
1646 #ifdef VM_MEMATTR_WRITE_COMBINING
1647 		*memattr = VM_MEMATTR_WRITE_COMBINING;
1648 #endif
1649 		return (0);
1650 	}
1651 	return ((*prevvidsw->mmap)(adp, offset, paddr, prot, memattr));
1652 }
1653 
1654 static int
1655 vesa_clear(video_adapter_t *adp)
1656 {
1657 
1658 	return ((*prevvidsw->clear)(adp));
1659 }
1660 
1661 static int
1662 vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1663 {
1664 
1665 	return ((*prevvidsw->fill_rect)(adp, val, x, y, cx, cy));
1666 }
1667 
1668 static int
1669 vesa_bitblt(video_adapter_t *adp,...)
1670 {
1671 
1672 	/* FIXME */
1673 	return (1);
1674 }
1675 
1676 static int
1677 get_palette(video_adapter_t *adp, int base, int count,
1678 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1679 {
1680 	u_char *r;
1681 	u_char *g;
1682 	u_char *b;
1683 	int bits;
1684 	int error;
1685 
1686 	if (base < 0 || base >= 256 || count < 0 || count > 256)
1687 		return (1);
1688 	if ((base + count) > 256)
1689 		return (1);
1690 	if (!VESA_MODE(adp->va_mode))
1691 		return (1);
1692 
1693 	bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1694 	r = malloc(count * 3, M_DEVBUF, M_WAITOK);
1695 	g = r + count;
1696 	b = g + count;
1697 	error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1698 	if (error == 0) {
1699 		copyout(r, red, count);
1700 		copyout(g, green, count);
1701 		copyout(b, blue, count);
1702 		if (trans != NULL) {
1703 			bzero(r, count);
1704 			copyout(r, trans, count);
1705 		}
1706 	}
1707 	free(r, M_DEVBUF);
1708 
1709 	return (error);
1710 }
1711 
1712 static int
1713 set_palette(video_adapter_t *adp, int base, int count,
1714 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1715 {
1716 	u_char *r;
1717 	u_char *g;
1718 	u_char *b;
1719 	int bits;
1720 	int error;
1721 
1722 	if (base < 0 || base >= 256 || count < 0 || count > 256)
1723 		return (1);
1724 	if ((base + count) > 256)
1725 		return (1);
1726 	if (!VESA_MODE(adp->va_mode))
1727 		return (1);
1728 
1729 	bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1730 	r = malloc(count * 3, M_DEVBUF, M_WAITOK);
1731 	g = r + count;
1732 	b = g + count;
1733 	copyin(red, r, count);
1734 	copyin(green, g, count);
1735 	copyin(blue, b, count);
1736 
1737 	error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1738 	free(r, M_DEVBUF);
1739 
1740 	return (error);
1741 }
1742 
1743 static int
1744 vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1745 {
1746 	int bytes;
1747 
1748 	if (adp != vesa_adp)
1749 		return ((*prevvidsw->ioctl)(adp, cmd, arg));
1750 
1751 	switch (cmd) {
1752 	case FBIO_SETWINORG:	/* set frame buffer window origin */
1753 		if (!VESA_MODE(adp->va_mode))
1754 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1755 		return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1756 
1757 	case FBIO_SETDISPSTART:	/* set display start address */
1758 		if (!VESA_MODE(adp->va_mode))
1759 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1760 		if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1761 					((video_display_start_t *)arg)->y))
1762 			return (ENODEV);
1763 		adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1764 		adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1765 		return (0);
1766 
1767 	case FBIO_SETLINEWIDTH:	/* set line length in pixel */
1768 		if (!VESA_MODE(adp->va_mode))
1769 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1770 		if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1771 			return (ENODEV);
1772 		adp->va_line_width = bytes;
1773 #if VESA_DEBUG > 1
1774 		printf("new line width:%d\n", adp->va_line_width);
1775 #endif
1776 		return (0);
1777 
1778 	case FBIO_GETPALETTE:	/* get color palette */
1779 		if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1780 				((video_color_palette_t *)arg)->count,
1781 				((video_color_palette_t *)arg)->red,
1782 				((video_color_palette_t *)arg)->green,
1783 				((video_color_palette_t *)arg)->blue,
1784 				((video_color_palette_t *)arg)->transparent))
1785 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1786 		return (0);
1787 
1788 
1789 	case FBIO_SETPALETTE:	/* set color palette */
1790 		if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1791 				((video_color_palette_t *)arg)->count,
1792 				((video_color_palette_t *)arg)->red,
1793 				((video_color_palette_t *)arg)->green,
1794 				((video_color_palette_t *)arg)->blue,
1795 				((video_color_palette_t *)arg)->transparent))
1796 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1797 		return (0);
1798 
1799 	case FBIOGETCMAP:	/* get color palette */
1800 		if (get_palette(adp, ((struct fbcmap *)arg)->index,
1801 				((struct fbcmap *)arg)->count,
1802 				((struct fbcmap *)arg)->red,
1803 				((struct fbcmap *)arg)->green,
1804 				((struct fbcmap *)arg)->blue, NULL))
1805 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1806 		return (0);
1807 
1808 	case FBIOPUTCMAP:	/* set color palette */
1809 		if (set_palette(adp, ((struct fbcmap *)arg)->index,
1810 				((struct fbcmap *)arg)->count,
1811 				((struct fbcmap *)arg)->red,
1812 				((struct fbcmap *)arg)->green,
1813 				((struct fbcmap *)arg)->blue, NULL))
1814 			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1815 		return (0);
1816 
1817 	default:
1818 		return ((*prevvidsw->ioctl)(adp, cmd, arg));
1819 	}
1820 }
1821 
1822 static int
1823 vesa_diag(video_adapter_t *adp, int level)
1824 {
1825 	int error;
1826 
1827 	/* call the previous handler first */
1828 	error = (*prevvidsw->diag)(adp, level);
1829 	if (error)
1830 		return (error);
1831 
1832 	if (adp != vesa_adp)
1833 		return (1);
1834 
1835 	if (level <= 0)
1836 		return (0);
1837 
1838 	return (0);
1839 }
1840 
1841 static int
1842 vesa_bios_info(int level)
1843 {
1844 #if VESA_DEBUG > 1
1845 	struct vesa_mode vmode;
1846 	int i;
1847 #endif
1848 	uint16_t vers;
1849 
1850 	vers = vesa_adp_info->v_version;
1851 
1852 	if (bootverbose) {
1853 		/* general adapter information */
1854 		printf(
1855 	"VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1856 		    (vers >> 12) * 10 + ((vers & 0x0f00) >> 8),
1857 		    ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f),
1858 		    vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1859 		    vesa_vmodetab, vesa_adp_info->v_modetable);
1860 
1861 		/* OEM string */
1862 		if (vesa_oemstr != NULL)
1863 			printf("VESA: %s\n", vesa_oemstr);
1864 	}
1865 
1866 	if (level <= 0)
1867 		return (0);
1868 
1869 	if (vers >= 0x0200 && bootverbose) {
1870 		/* vender name, product name, product revision */
1871 		printf("VESA: %s %s %s\n",
1872 			(vesa_venderstr != NULL) ? vesa_venderstr : "unknown",
1873 			(vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1874 			(vesa_revstr != NULL) ? vesa_revstr : "?");
1875 	}
1876 
1877 #if VESA_DEBUG > 1
1878 	/* mode information */
1879 	for (i = 0;
1880 		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1881 		&& (vesa_vmodetab[i] != 0xffff); ++i) {
1882 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_NOWAIT))
1883 			continue;
1884 
1885 		/* print something for diagnostic purpose */
1886 		printf("VESA: mode:0x%03x, flags:0x%04x",
1887 		       vesa_vmodetab[i], vmode.v_modeattr);
1888 		if (vmode.v_modeattr & V_MODEOPTINFO) {
1889 			if (vmode.v_modeattr & V_MODEGRAPHICS) {
1890 				printf(", G %dx%dx%d %d, ",
1891 				       vmode.v_width, vmode.v_height,
1892 				       vmode.v_bpp, vmode.v_planes);
1893 			} else {
1894 				printf(", T %dx%d, ",
1895 				       vmode.v_width, vmode.v_height);
1896 			}
1897 			printf("font:%dx%d, ",
1898 			       vmode.v_cwidth, vmode.v_cheight);
1899 			printf("pages:%d, mem:%d",
1900 			       vmode.v_ipages + 1, vmode.v_memmodel);
1901 		}
1902 		if (vmode.v_modeattr & V_MODELFB) {
1903 			printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1904 			       vmode.v_lfb, vmode.v_offscreen,
1905 			       vmode.v_offscreensize*1024);
1906 		}
1907 		printf("\n");
1908 		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1909 		       vmode.v_waseg, vmode.v_waattr,
1910 		       vmode.v_wbseg, vmode.v_wbattr);
1911 		printf("size:%dk, gran:%dk\n",
1912 		       vmode.v_wsize, vmode.v_wgran);
1913 	}
1914 #endif /* VESA_DEBUG > 1 */
1915 
1916 	return (0);
1917 }
1918 
1919 /* module loading */
1920 
1921 static int
1922 vesa_load(void)
1923 {
1924 	int error;
1925 
1926 	if (vesa_init_done)
1927 		return (0);
1928 
1929 	/* locate a VGA adapter */
1930 	vesa_adp = NULL;
1931 	error = vesa_configure(0);
1932 
1933 	if (error == 0)
1934 		vesa_bios_info(bootverbose);
1935 
1936 	return (error);
1937 }
1938 
1939 static int
1940 vesa_unload(void)
1941 {
1942 	int error;
1943 
1944 	/* if the adapter is currently in a VESA mode, don't unload */
1945 	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1946 		return (EBUSY);
1947 	/*
1948 	 * FIXME: if there is at least one vty which is in a VESA mode,
1949 	 * we shouldn't be unloading! XXX
1950 	 */
1951 
1952 	if ((error = vesa_unload_ioctl()) == 0) {
1953 		if (vesa_adp != NULL) {
1954 			if ((vesa_adp->va_flags & V_ADP_DAC8) != 0) {
1955 				vesa_bios_set_dac(6);
1956 				vesa_adp->va_flags &= ~V_ADP_DAC8;
1957 			}
1958 			vesa_adp->va_flags &= ~V_ADP_VESA;
1959 			vidsw[vesa_adp->va_index] = prevvidsw;
1960 		}
1961 	}
1962 
1963 	vesa_bios_uninit();
1964 
1965 	return (error);
1966 }
1967 
1968 static int
1969 vesa_mod_event(module_t mod, int type, void *data)
1970 {
1971 
1972 	switch (type) {
1973 	case MOD_LOAD:
1974 		return (vesa_load());
1975 	case MOD_UNLOAD:
1976 		return (vesa_unload());
1977 	}
1978 	return (EOPNOTSUPP);
1979 }
1980 
1981 static moduledata_t vesa_mod = {
1982 	"vesa",
1983 	vesa_mod_event,
1984 	NULL,
1985 };
1986 
1987 DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1988 MODULE_DEPEND(vesa, x86bios, 1, 1, 1);
1989 
1990 #endif	/* VGA_NO_MODE_CHANGE */
1991