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