vesa.c revision 42611
1/*-
2 * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $Id: vesa.c,v 1.12 1999/01/11 03:18:24 yokota Exp $
27 */
28
29#include "vga.h"
30#include "opt_vga.h"
31#include "opt_vesa.h"
32#include "opt_vm86.h"
33#include "opt_fb.h"
34
35#ifdef VGA_NO_MODE_CHANGE
36#undef VESA
37#endif
38
39#if (NVGA > 0 && defined(VESA) && defined(VM86)) || defined(KLD_MODULE)
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/module.h>
45#include <sys/malloc.h>
46#include <vm/vm.h>
47#include <vm/pmap.h>
48
49#include <machine/console.h>
50#include <machine/md_var.h>
51#include <machine/vm86.h>
52#include <machine/pc/bios.h>
53#include <machine/pc/vesa.h>
54
55#include <dev/fb/fbreg.h>
56#include <dev/fb/vgareg.h>
57
58#ifndef __i386__
59#include <isa/isareg.h>
60#else
61#include <i386/isa/isa.h>
62#endif
63
64#ifndef VESA_DEBUG
65#define VESA_DEBUG	0
66#endif
67
68/* VESA video adapter state buffer stub */
69struct adp_state {
70	int		sig;
71#define V_STATE_SIG	0x61736576
72	u_char		regs[1];
73};
74typedef struct adp_state adp_state_t;
75
76/* VESA video adapter */
77static video_adapter_t *vesa_adp = NULL;
78static int vesa_state_buf_size = 0;
79#if 0
80static void *vesa_state_buf = NULL;
81#endif
82
83/* VESA functions */
84static int			vesa_nop(void);
85static vi_probe_t		vesa_probe;
86static vi_init_t		vesa_init;
87static vi_get_info_t		vesa_get_info;
88static vi_query_mode_t		vesa_query_mode;
89static vi_set_mode_t		vesa_set_mode;
90static vi_save_font_t		vesa_save_font;
91static vi_load_font_t		vesa_load_font;
92static vi_show_font_t		vesa_show_font;
93static vi_save_palette_t	vesa_save_palette;
94static vi_load_palette_t	vesa_load_palette;
95static vi_set_border_t		vesa_set_border;
96static vi_save_state_t		vesa_save_state;
97static vi_load_state_t		vesa_load_state;
98static vi_set_win_org_t		vesa_set_origin;
99static vi_read_hw_cursor_t	vesa_read_hw_cursor;
100static vi_set_hw_cursor_t	vesa_set_hw_cursor;
101static vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
102static vi_mmap_t		vesa_mmap;
103static vi_diag_t		vesa_diag;
104
105static video_switch_t vesavidsw = {
106	vesa_probe,
107	vesa_init,
108	vesa_get_info,
109	vesa_query_mode,
110	vesa_set_mode,
111	vesa_save_font,
112	vesa_load_font,
113	vesa_show_font,
114	vesa_save_palette,
115	vesa_load_palette,
116	vesa_set_border,
117	vesa_save_state,
118	vesa_load_state,
119	vesa_set_origin,
120	vesa_read_hw_cursor,
121	vesa_set_hw_cursor,
122	vesa_set_hw_cursor_shape,
123	(vi_blank_display_t *)vesa_nop,
124	vesa_mmap,
125	vesa_diag,
126};
127
128static video_switch_t *prevvidsw;
129
130/* VESA BIOS video modes */
131#define VESA_MAXMODES	64
132#define EOT		(-1)
133#define NA		(-2)
134
135static video_info_t vesa_vmode[VESA_MAXMODES + 1] = {
136	{ EOT, },
137};
138
139static int vesa_init_done = FALSE;
140static int has_vesa_bios = FALSE;
141static struct vesa_info *vesa_adp_info = NULL;
142static u_int16_t *vesa_vmodetab = NULL;
143static char *vesa_oemstr = NULL;
144static char *vesa_venderstr = NULL;
145static char *vesa_prodstr = NULL;
146static char *vesa_revstr = NULL;
147
148/* local macros and functions */
149#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
150
151static int int10_set_mode(int mode);
152static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
153static int vesa_bios_set_mode(int mode);
154static int vesa_bios_set_dac(int bits);
155static int vesa_bios_save_palette(int start, int colors, u_char *palette);
156static int vesa_bios_load_palette(int start, int colors, u_char *palette);
157#define STATE_SIZE	0
158#define STATE_SAVE	1
159#define STATE_LOAD	2
160#define STATE_HW	(1<<0)
161#define STATE_DATA	(1<<1)
162#define STATE_DAC	(1<<2)
163#define STATE_REG	(1<<3)
164#define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
165#define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
166static int vesa_bios_state_buf_size(void);
167static int vesa_bios_save_restore(int code, void *p, size_t size);
168static int vesa_map_gen_mode_num(int type, int color, int mode);
169static int vesa_translate_flags(u_int16_t vflags);
170static void *vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off,
171			  u_char *buf);
172static int vesa_bios_init(void);
173static void vesa_clear_modes(video_info_t *info, int color);
174
175static void
176dump_buffer(u_char *buf, size_t len)
177{
178    int i;
179
180    for(i = 0; i < len;) {
181	printf("%02x ", buf[i]);
182	if ((++i % 16) == 0)
183	    printf("\n");
184    }
185}
186
187/* INT 10 BIOS calls */
188static int
189int10_set_mode(int mode)
190{
191	struct vm86frame vmf;
192
193	bzero(&vmf, sizeof(vmf));
194	vmf.vmf_eax = 0x0000 | mode;
195	vm86_intcall(0x10, &vmf);
196	return 0;
197}
198
199/* VESA BIOS calls */
200static int
201vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
202{
203	struct vm86frame vmf;
204	u_char buf[256];
205	int err;
206
207	bzero(&vmf, sizeof(vmf));
208	bzero(buf, sizeof(buf));
209	vmf.vmf_eax = 0x4f01;
210	vmf.vmf_ecx = mode;
211	err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf),
212			  &vmf.vmf_es, &vmf.vmf_di);
213	if ((err != 0) || (vmf.vmf_eax != 0x4f))
214		return 1;
215	bcopy(buf, vmode, sizeof(*vmode));
216	return 0;
217}
218
219static int
220vesa_bios_set_mode(int mode)
221{
222	struct vm86frame vmf;
223	int err;
224
225	bzero(&vmf, sizeof(vmf));
226	vmf.vmf_eax = 0x4f02;
227	vmf.vmf_ebx = mode;
228	err = vm86_intcall(0x10, &vmf);
229	return ((err != 0) || (vmf.vmf_eax != 0x4f));
230}
231
232static int
233vesa_bios_set_dac(int bits)
234{
235	struct vm86frame vmf;
236	int err;
237
238	bzero(&vmf, sizeof(vmf));
239	vmf.vmf_eax = 0x4f08;
240	vmf.vmf_ebx = (bits << 8);
241	err = vm86_intcall(0x10, &vmf);
242	return ((err != 0) || (vmf.vmf_eax != 0x4f));
243}
244
245static int
246vesa_bios_save_palette(int start, int colors, u_char *palette)
247{
248	struct vm86frame vmf;
249	u_char *p;
250	int err;
251	int i;
252
253	p = malloc(colors*4, M_DEVBUF, M_WAITOK);
254
255	bzero(&vmf, sizeof(vmf));
256	vmf.vmf_eax = 0x4f09;
257	vmf.vmf_ebx = 1;	/* get primary palette data */
258	vmf.vmf_ecx = colors;
259	vmf.vmf_edx = start;
260	err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di);
261	if ((err != 0) || (vmf.vmf_eax != 0x4f)) {
262		free(p, M_DEVBUF);
263		return 1;
264	}
265
266	for (i = 0; i < colors; ++i) {
267		palette[i*3]     = p[i*4 + 1];
268		palette[i*3 + 1] = p[i*4 + 2];
269		palette[i*3 + 2] = p[i*4 + 3];
270	}
271	free(p, M_DEVBUF);
272	return 0;
273}
274
275static int
276vesa_bios_load_palette(int start, int colors, u_char *palette)
277{
278	struct vm86frame vmf;
279	u_char *p;
280	int err;
281	int i;
282
283	p = malloc(colors*4, M_DEVBUF, M_WAITOK);
284	for (i = 0; i < colors; ++i) {
285		p[i*4]     = 0;
286		p[i*4 + 1] = palette[i*3];
287		p[i*4 + 2] = palette[i*3 + 1];
288		p[i*4 + 3] = palette[i*3 + 2];
289	}
290
291	bzero(&vmf, sizeof(vmf));
292	vmf.vmf_eax = 0x4f09;
293	vmf.vmf_ebx = 0;	/* set primary palette data */
294	vmf.vmf_ecx = colors;
295	vmf.vmf_edx = start;
296	err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di);
297	free(p, M_DEVBUF);
298	return ((err != 0) || (vmf.vmf_eax != 0x4f));
299}
300
301static int
302vesa_bios_state_buf_size(void)
303{
304	struct vm86frame vmf;
305	int err;
306
307	bzero(&vmf, sizeof(vmf));
308	vmf.vmf_eax = 0x4f04;
309	vmf.vmf_ecx = STATE_MOST;
310	vmf.vmf_edx = STATE_SIZE;
311	err = vm86_intcall(0x10, &vmf);
312	if ((err != 0) || (vmf.vmf_eax != 0x4f))
313		return 0;
314	return vmf.vmf_ebx*64;
315}
316
317static int
318vesa_bios_save_restore(int code, void *p, size_t size)
319{
320	struct vm86frame vmf;
321	int err;
322
323	bzero(&vmf, sizeof(vmf));
324	vmf.vmf_eax = 0x4f04;
325	vmf.vmf_ecx = STATE_MOST;
326	vmf.vmf_edx = code;	/* STATE_SAVE/STATE_LOAD */
327	err = vm86_datacall(0x10, &vmf, (char *)p, size,
328			  &vmf.vmf_es, &vmf.vmf_bx);
329	return ((err != 0) || (vmf.vmf_eax != 0x4f));
330}
331
332/* map a generic video mode to a known mode */
333static int
334vesa_map_gen_mode_num(int type, int color, int mode)
335{
336    static struct {
337	int from;
338	int to;
339    } mode_map[] = {
340	{ M_TEXT_132x25, M_VESA_C132x25 },
341	{ M_TEXT_132x43, M_VESA_C132x43 },
342	{ M_TEXT_132x50, M_VESA_C132x50 },
343	{ M_TEXT_132x60, M_VESA_C132x60 },
344    };
345    int i;
346
347    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
348        if (mode_map[i].from == mode)
349            return mode_map[i].to;
350    }
351    return mode;
352}
353
354static int
355vesa_translate_flags(u_int16_t vflags)
356{
357	static struct {
358		u_int16_t mask;
359		int set;
360		int reset;
361	} ftable[] = {
362		{ V_MODECOLOR, V_INFO_COLOR, 0 },
363		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
364		{ V_MODELFB, V_INFO_LINEAR, 0 },
365	};
366	int flags;
367	int i;
368
369	for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
370		flags |= (vflags & ftable[i].mask) ?
371			 ftable[i].set : ftable[i].reset;
372	}
373	return flags;
374}
375
376static void
377*vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off, u_char *buf)
378{
379	if (p == 0)
380		return NULL;
381	if (((p >> 16) == seg) && ((p & 0xffff) >= off))
382		return (void *)(buf + ((p & 0xffff) - off));
383	else {
384		p = BIOS_SADDRTOLADDR(p);
385		return (void *)BIOS_PADDRTOVADDR(p);
386	}
387}
388
389static int
390vesa_bios_init(void)
391{
392	static u_char buf[512];
393	struct vm86frame vmf;
394	struct vesa_mode vmode;
395	int modes;
396	int err;
397	int i;
398
399	if (vesa_init_done)
400		return 0;
401
402	has_vesa_bios = FALSE;
403	vesa_adp_info = NULL;
404	vesa_vmode[0].vi_mode = EOT;
405
406	bzero(&vmf, sizeof(vmf));	/* paranoia */
407	bzero(buf, sizeof(buf));
408	bcopy("VBE2", buf, 4);		/* try for VBE2 data */
409	vmf.vmf_eax = 0x4f00;
410	err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf),
411			  &vmf.vmf_es, &vmf.vmf_di);
412	if ((err != 0) || (vmf.vmf_eax != 0x4f) || bcmp("VESA", buf, 4))
413		return 1;
414	vesa_adp_info = (struct vesa_info *)buf;
415	if (bootverbose) {
416		printf("VESA: information block\n");
417		dump_buffer(buf, 64);
418	}
419	if (vesa_adp_info->v_flags & V_NONVGA)
420		return 1;
421
422	/* fix string ptrs */
423	vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr,
424					   vmf.vmf_es, vmf.vmf_di, buf);
425	if (vesa_adp_info->v_version >= 0x0200) {
426		vesa_venderstr =
427		    (char *)vesa_fix_ptr(vesa_adp_info->v_venderstr,
428					 vmf.vmf_es, vmf.vmf_di, buf);
429		vesa_prodstr =
430		    (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr,
431					 vmf.vmf_es, vmf.vmf_di, buf);
432		vesa_revstr =
433		    (char *)vesa_fix_ptr(vesa_adp_info->v_revstr,
434					 vmf.vmf_es, vmf.vmf_di, buf);
435	}
436
437	/* obtain video mode information */
438	vesa_vmode[0].vi_mode = EOT;
439	vesa_vmodetab = (u_int16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable,
440						  vmf.vmf_es, vmf.vmf_di, buf);
441	if (vesa_vmodetab == NULL)
442		return 1;
443	for (i = 0, modes = 0;
444		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
445		&& (vesa_vmodetab[i] != 0xffff); ++i) {
446		if (modes >= VESA_MAXMODES)
447			break;
448		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
449			continue;
450
451		/* reject unsupported modes */
452#if 0
453		if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
454					| V_MODENONVGA))
455		    != (V_MODESUPP | V_MODEOPTINFO))
456			continue;
457#else
458		if ((vmode.v_modeattr & (V_MODEOPTINFO | V_MODENONVGA))
459		    != (V_MODEOPTINFO))
460			continue;
461#endif
462
463		/* copy some fields */
464		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
465		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
466		vesa_vmode[modes].vi_width = vmode.v_width;
467		vesa_vmode[modes].vi_height = vmode.v_height;
468		vesa_vmode[modes].vi_depth = vmode.v_bpp;
469		vesa_vmode[modes].vi_planes = vmode.v_planes;
470		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
471		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
472		vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
473		/* XXX window B */
474		vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
475		vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
476		vesa_vmode[modes].vi_buffer = vmode.v_lfb;
477		vesa_vmode[modes].vi_buffer_size = vmode.v_offscreen;
478		/* pixel format, memory model... */
479		vesa_vmode[modes].vi_flags
480			= vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
481		++modes;
482	}
483	vesa_vmode[modes].vi_mode = EOT;
484	if (bootverbose)
485		printf("VESA: %d mode(s) found\n", modes);
486
487	has_vesa_bios = (modes > 0);
488	return (has_vesa_bios ? 0 : 1);
489}
490
491static void
492vesa_clear_modes(video_info_t *info, int color)
493{
494	while (info->vi_mode != EOT) {
495		if ((info->vi_flags & V_INFO_COLOR) != color)
496			info->vi_mode = NA;
497		++info;
498	}
499}
500
501/* entry points */
502
503static int
504vesa_configure(int flags)
505{
506	video_adapter_t *adp;
507	int adapters;
508	int error;
509	int i;
510
511	if (vesa_init_done)
512		return 0;
513	if (flags & VIO_PROBE_ONLY)
514		return 0;		/* XXX */
515
516	/*
517	 * If the VESA module has already been loaded, abort loading
518	 * the module this time.
519	 */
520	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
521		if (adp->va_flags & V_ADP_VESA)
522			return ENXIO;
523		if (adp->va_type == KD_VGA)
524			break;
525	}
526	/*
527	 * The VGA adapter is not found.  This is because either
528	 * 1) the VGA driver has not been initialized, or 2) the VGA card
529	 * is not present.  If 1) is the case, we shall defer
530	 * initialization for now and try again later.
531	 */
532	if (adp == NULL) {
533		vga_sub_configure = vesa_configure;
534		return ENODEV;
535	}
536
537	/* count number of registered adapters */
538	for (++i; vid_get_adapter(i) != NULL; ++i)
539		;
540	adapters = i;
541
542	/* call VESA BIOS */
543	vesa_adp = adp;
544	if (vesa_bios_init()) {
545		vesa_adp = NULL;
546		return ENXIO;
547	}
548	vesa_adp->va_flags |= V_ADP_VESA;
549
550	/* remove conflicting modes if we have more than one adapter */
551	if (adapters > 1) {
552		vesa_clear_modes(vesa_vmode,
553				 (vesa_adp->va_flags & V_ADP_COLOR) ?
554				     V_INFO_COLOR : 0);
555	}
556
557	if ((error = vesa_load_ioctl()) == 0) {
558		prevvidsw = vidsw[vesa_adp->va_index];
559		vidsw[vesa_adp->va_index] = &vesavidsw;
560		vesa_init_done = TRUE;
561	} else {
562		vesa_adp = NULL;
563		return error;
564	}
565
566	return 0;
567}
568
569static int
570vesa_nop(void)
571{
572	return 0;
573}
574
575static int
576vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
577{
578	return (*prevvidsw->probe)(unit, adpp, arg, flags);
579}
580
581static int
582vesa_init(int unit, video_adapter_t *adp, int flags)
583{
584	return (*prevvidsw->init)(unit, adp, flags);
585}
586
587static int
588vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
589{
590	int i;
591
592	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
593		return 0;
594
595	if (adp != vesa_adp)
596		return 1;
597
598	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
599				     vesa_adp->va_flags & V_ADP_COLOR, mode);
600	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
601		if (vesa_vmode[i].vi_mode == NA)
602			continue;
603		if (vesa_vmode[i].vi_mode == mode) {
604			*info = vesa_vmode[i];
605			return 0;
606		}
607	}
608	return 1;
609}
610
611static int
612vesa_query_mode(video_adapter_t *adp, video_info_t *info)
613{
614	int i;
615
616	if ((i = (*prevvidsw->query_mode)(adp, info)) != -1)
617		return i;
618	if (adp != vesa_adp)
619		return -1;
620
621	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
622		if ((info->vi_width != 0)
623		    && (info->vi_width != vesa_vmode[i].vi_width))
624			continue;
625		if ((info->vi_height != 0)
626		    && (info->vi_height != vesa_vmode[i].vi_height))
627			continue;
628		if ((info->vi_cwidth != 0)
629		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
630			continue;
631		if ((info->vi_cheight != 0)
632		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
633			continue;
634		if ((info->vi_depth != 0)
635		    && (info->vi_depth != vesa_vmode[i].vi_depth))
636			continue;
637		if ((info->vi_planes != 0)
638		    && (info->vi_planes != vesa_vmode[i].vi_planes))
639			continue;
640		/* pixel format, memory model */
641		if ((info->vi_flags != 0)
642		    && (info->vi_flags != vesa_vmode[i].vi_flags))
643			continue;
644		return vesa_vmode[i].vi_mode;
645	}
646	return -1;
647}
648
649static int
650vesa_set_mode(video_adapter_t *adp, int mode)
651{
652	video_info_t info;
653#if 0
654	size_t len;
655#endif
656
657	if (adp != vesa_adp)
658		return (*prevvidsw->set_mode)(adp, mode);
659
660	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
661				     vesa_adp->va_flags & V_ADP_COLOR, mode);
662#if VESA_DEBUG > 0
663	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
664		vesa_adp->va_mode, vesa_adp->va_mode, mode, mode);
665#endif
666	/*
667	 * If the current mode is a VESA mode and the new mode is not,
668	 * restore the state of the adapter first, so that non-standard,
669	 * extended SVGA registers are set to the state compatible with
670	 * the standard VGA modes. Otherwise (*prevvidsw->set_mode)()
671	 * may not be able to set up the new mode correctly.
672	 */
673	if (VESA_MODE(vesa_adp->va_mode)) {
674		if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
675			int10_set_mode(vesa_adp->va_initial_bios_mode);
676#if 0
677			/* assert(vesa_state_buf != NULL); */
678		    	if ((vesa_state_buf == NULL)
679			    || vesa_load_state(adp, vesa_state_buf))
680				return 1;
681			free(vesa_state_buf, M_DEVBUF);
682			vesa_state_buf = NULL;
683#if VESA_DEBUG > 0
684			printf("VESA: restored\n");
685#endif
686#endif /* 0 */
687		}
688		/*
689		 * once (*prevvidsw->get_info)() succeeded,
690		 * (*prevvidsw->set_mode)() below won't fail...
691		 */
692	}
693
694	/* we may not need to handle this mode after all... */
695	if ((*prevvidsw->set_mode)(adp, mode) == 0)
696		return 0;
697
698	/* is the new mode supported? */
699	if (vesa_get_info(adp, mode, &info))
700		return 1;
701	/* assert(VESA_MODE(mode)); */
702
703#if VESA_DEBUG > 0
704	printf("VESA: about to set a VESA mode...\n");
705#endif
706	/*
707	 * If the current mode is not a VESA mode, save the current state
708	 * so that the adapter state can be restored later when a non-VESA
709	 * mode is to be set up. See above.
710	 */
711#if 0
712	if (!VESA_MODE(vesa_adp->va_mode) && (vesa_state_buf == NULL)) {
713		len = vesa_save_state(adp, NULL, 0);
714		vesa_state_buf = malloc(len, M_DEVBUF, M_WAITOK);
715		if (vesa_save_state(adp, vesa_state_buf, len)) {
716#if VESA_DEBUG > 0
717			printf("VESA: state save failed! (len=%d)\n", len);
718#endif
719			free(vesa_state_buf, M_DEVBUF);
720			vesa_state_buf = NULL;
721			return 1;
722		}
723#if VESA_DEBUG > 0
724		printf("VESA: saved (len=%d)\n", len);
725		dump_buffer(vesa_state_buf, len);
726#endif
727	}
728#endif /* 0 */
729
730	if (vesa_bios_set_mode(mode))
731		return 1;
732
733#if VESA_DEBUG > 0
734	printf("VESA: mode set!\n");
735#endif
736	vesa_adp->va_mode = mode;
737	vesa_adp->va_flags &= ~V_ADP_COLOR;
738	vesa_adp->va_flags |=
739		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
740	vesa_adp->va_crtc_addr =
741		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
742	vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
743	vesa_adp->va_window_size = info.vi_window_size;
744	vesa_adp->va_window_gran = info.vi_window_gran;
745	if (info.vi_buffer_size == 0) {
746		vesa_adp->va_buffer = 0;
747		vesa_adp->va_buffer_size = 0;
748	} else {
749		vesa_adp->va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
750		vesa_adp->va_buffer_size = info.vi_buffer_size;
751	}
752
753	/* move hardware cursor out of the way */
754	(*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
755
756	return 0;
757}
758
759static int
760vesa_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
761	       int ch, int count)
762{
763	return (*prevvidsw->save_font)(adp, page, fontsize, data, ch, count);
764}
765
766static int
767vesa_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
768	       int ch, int count)
769{
770	return (*prevvidsw->load_font)(adp, page, fontsize, data, ch, count);
771}
772
773static int
774vesa_show_font(video_adapter_t *adp, int page)
775{
776	return (*prevvidsw->show_font)(adp, page);
777}
778
779static int
780vesa_save_palette(video_adapter_t *adp, u_char *palette)
781{
782	if ((adp != vesa_adp) || !(vesa_adp_info->v_flags & V_DAC8)
783	    || vesa_bios_set_dac(8))
784		return (*prevvidsw->save_palette)(adp, palette);
785
786	return vesa_bios_save_palette(0, 256, palette);
787}
788
789static int
790vesa_load_palette(video_adapter_t *adp, u_char *palette)
791{
792	if ((adp != vesa_adp) || !(vesa_adp_info->v_flags & V_DAC8)
793	    || vesa_bios_set_dac(8))
794		return (*prevvidsw->load_palette)(adp, palette);
795
796	return vesa_bios_load_palette(0, 256, palette);
797}
798
799static int
800vesa_set_border(video_adapter_t *adp, int color)
801{
802	return (*prevvidsw->set_border)(adp, color);
803}
804
805static int
806vesa_save_state(video_adapter_t *adp, void *p, size_t size)
807{
808	if (adp != vesa_adp)
809		return (*prevvidsw->save_state)(adp, p, size);
810
811	if (vesa_state_buf_size == 0)
812		vesa_state_buf_size = vesa_bios_state_buf_size();
813	if (size == 0)
814		return (sizeof(int) + vesa_state_buf_size);
815	else if (size < (sizeof(int) + vesa_state_buf_size))
816		return 1;
817
818	((adp_state_t *)p)->sig = V_STATE_SIG;
819	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
820	return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
821				      vesa_state_buf_size);
822}
823
824static int
825vesa_load_state(video_adapter_t *adp, void *p)
826{
827	if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
828		return (*prevvidsw->load_state)(adp, p);
829
830	return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
831				      vesa_state_buf_size);
832}
833
834static int
835vesa_set_origin(video_adapter_t *adp, off_t offset)
836{
837	struct vm86frame vmf;
838	int err;
839
840	/*
841	 * This function should return as quickly as possible to
842	 * maintain good performance of the system. For this reason,
843	 * error checking is kept minimal and let the VESA BIOS to
844	 * detect error.
845	 */
846	if (adp != vesa_adp)
847		return (*prevvidsw->set_win_org)(adp, offset);
848
849	if (vesa_adp->va_window_gran == 0)
850		return 1;
851	bzero(&vmf, sizeof(vmf));
852	vmf.vmf_eax = 0x4f05;
853	vmf.vmf_ebx = 0;		/* WINDOW_A, XXX */
854	vmf.vmf_edx = offset/vesa_adp->va_window_gran;
855	err = vm86_intcall(0x10, &vmf);
856	return ((err != 0) || (vmf.vmf_eax != 0x4f));
857}
858
859static int
860vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
861{
862	return (*prevvidsw->read_hw_cursor)(adp, col, row);
863}
864
865static int
866vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
867{
868	return (*prevvidsw->set_hw_cursor)(adp, col, row);
869}
870
871static int
872vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
873			 int celsize, int blink)
874{
875	return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
876						 blink);
877}
878
879static int
880vesa_mmap(video_adapter_t *adp, vm_offset_t offset)
881{
882	return (*prevvidsw->mmap)(adp, offset);
883}
884
885static int
886vesa_diag(video_adapter_t *adp, int level)
887{
888#if VESA_DEBUG > 1
889	struct vesa_mode vmode;
890	int i;
891#endif
892
893	if (adp != vesa_adp)
894		return 1;
895
896#ifndef KLD_MODULE
897	/* call the previous handler first */
898	(*prevvidsw->diag)(adp, level);
899#endif
900
901	/* general adapter information */
902	printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
903	       ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
904		   + ((vesa_adp_info->v_version & 0x0f00) >> 8),
905	       ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
906		   + (vesa_adp_info->v_version & 0x000f),
907	       vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
908	       vesa_vmodetab, vesa_adp_info->v_modetable);
909	/* OEM string */
910	if (vesa_oemstr != NULL)
911		printf("VESA: %s\n", vesa_oemstr);
912
913	if (level <= 0)
914		return 0;
915
916	if (vesa_adp_info->v_version >= 0x0200) {
917		/* vendor name */
918		if (vesa_venderstr != NULL)
919			printf("VESA: %s\n", vesa_venderstr);
920		/* product name */
921		if (vesa_prodstr != NULL)
922			printf("VESA: %s\n", vesa_prodstr);
923		/* product revision */
924		if (vesa_revstr != NULL)
925			printf("VESA: %s\n", vesa_revstr);
926	}
927
928#if VESA_DEBUG > 1
929	/* mode information */
930	for (i = 0;
931		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
932		&& (vesa_vmodetab[i] != 0xffff); ++i) {
933		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
934			continue;
935
936		/* print something for diagnostic purpose */
937		printf("VESA: mode:0x%03x, flags:0x%04x",
938		       vesa_vmodetab[i], vmode.v_modeattr);
939		if (vmode.v_modeattr & V_MODEOPTINFO) {
940			if (vmode.v_modeattr & V_MODEGRAPHICS) {
941				printf(", G %dx%dx%d %d, ",
942				       vmode.v_width, vmode.v_height,
943				       vmode.v_bpp, vmode.v_planes);
944			} else {
945				printf(", T %dx%d, ",
946				       vmode.v_width, vmode.v_height);
947			}
948			printf("font:%dx%d",
949			       vmode.v_cwidth, vmode.v_cheight);
950		}
951		if (vmode.v_modeattr & V_MODELFB) {
952			printf(", mem:%d, LFB:0x%x, off:0x%x",
953			       vmode.v_memmodel, vmode.v_lfb,
954			       vmode.v_offscreen);
955		}
956		printf("\n");
957		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
958		       vmode.v_waseg, vmode.v_waattr,
959		       vmode.v_wbseg, vmode.v_wbattr);
960		printf("size:%dk, gran:%dk\n",
961		       vmode.v_wsize, vmode.v_wgran);
962	}
963#endif
964
965	return 0;
966}
967
968/* module loading */
969
970static int
971vesa_load(void)
972{
973	int error;
974	int s;
975
976	if (vesa_init_done)
977		return 0;
978
979	/* locate a VGA adapter */
980	s = spltty();
981	vesa_adp = NULL;
982	error = vesa_configure(0);
983	splx(s);
984
985#ifdef KLD_MODULE
986	if (error == 0)
987		vesa_diag(vesa_adp, bootverbose);
988#endif
989
990	return error;
991}
992
993#ifdef KLD_MODULE
994
995static int
996vesa_unload(void)
997{
998	int error;
999	int s;
1000
1001	/* if the adapter is currently in a VESA mode, don't unload */
1002	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1003		return EBUSY;
1004	/*
1005	 * FIXME: if there is at least one vty which is in a VESA mode,
1006	 * we shouldn't be unloading! XXX
1007	 */
1008
1009	s = spltty();
1010	if ((error = vesa_unload_ioctl()) == 0) {
1011		if (vesa_adp != NULL)
1012			vesa_adp->va_flags &= ~V_ADP_VESA;
1013		vidsw[vesa_adp->va_index] = prevvidsw;
1014	}
1015	splx(s);
1016
1017	return error;
1018}
1019
1020static int
1021vesa_mod_event(module_t mod, int type, void *data)
1022{
1023	switch (type) {
1024	case MOD_LOAD:
1025		return vesa_load();
1026	case MOD_UNLOAD:
1027		return vesa_unload();
1028	default:
1029		break;
1030	}
1031	return 0;
1032}
1033
1034static moduledata_t vesa_mod = {
1035	"vesa",
1036	vesa_mod_event,
1037	NULL,
1038};
1039
1040DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1041
1042#else /* KLD_MODULE */
1043
1044SYSINIT(vesa, SI_SUB_DRIVERS, SI_ORDER_MIDDLE,
1045	(void (*)(void *))vesa_load, NULL);
1046
1047#endif /* KLD_MODULE */
1048
1049#endif /* (NVGA > 0 && VESA && VM86) || KLD_MODULE */
1050