vesa.c revision 39744
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.3 1998/09/25 11:55:46 yokota Exp $
27 */
28
29#include "sc.h"
30#include "opt_vesa.h"
31#include "opt_vm86.h"
32
33#if (NSC > 0 && defined(VESA) && defined(VM86)) || defined(VESA_MODULE)
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/malloc.h>
39#include <vm/vm.h>
40#include <vm/pmap.h>
41
42#include <machine/console.h>
43#include <machine/md_var.h>
44#include <machine/vm86.h>
45#include <machine/pc/bios.h>
46#include <machine/pc/vesa.h>
47
48#include <i386/isa/videoio.h>
49
50#ifdef VESA_MODULE
51#include <sys/exec.h>
52#include <sys/sysent.h>
53#include <sys/lkm.h>
54
55MOD_MISC(vesa);
56#endif
57
58/* VESA video adapter state buffer stub */
59struct adp_state {
60	int		sig;
61#define V_STATE_SIG	0x61736576
62	u_char		regs[1];
63};
64typedef struct adp_state adp_state_t;
65
66/* VESA video adapter */
67static video_adapter_t *vesa_adp = NULL;
68static int vesa_state_buf_size = 0;
69static void *vesa_state_buf = NULL;
70
71/* VESA functions */
72static vi_init_t		vesa_init;
73static vi_adapter_t		vesa_adapter;
74static vi_get_info_t		vesa_get_info;
75static vi_query_mode_t		vesa_query_mode;
76static vi_set_mode_t		vesa_set_mode;
77static vi_save_font_t		vesa_save_font;
78static vi_load_font_t		vesa_load_font;
79static vi_show_font_t		vesa_show_font;
80static vi_save_palette_t	vesa_save_palette;
81static vi_load_palette_t	vesa_load_palette;
82static vi_set_border_t		vesa_set_border;
83static vi_save_state_t		vesa_save_state;
84static vi_load_state_t		vesa_load_state;
85static vi_set_win_org_t		vesa_set_origin;
86static vi_read_hw_cursor_t	vesa_read_hw_cursor;
87static vi_set_hw_cursor_t	vesa_set_hw_cursor;
88static vi_diag_t		vesa_diag;
89
90static struct vidsw vesavidsw = {
91	vesa_init,	vesa_adapter,	vesa_get_info,	vesa_query_mode,
92	vesa_set_mode,	vesa_save_font,	vesa_load_font,	vesa_show_font,
93	vesa_save_palette,vesa_load_palette,vesa_set_border,vesa_save_state,
94	vesa_load_state,vesa_set_origin,vesa_read_hw_cursor,vesa_set_hw_cursor,
95	vesa_diag,
96};
97
98static struct vidsw prevvidsw;
99
100/* VESA BIOS video modes */
101#define VESA_MAXMODES	64
102#define EOT		(-1)
103#define NA		(-2)
104
105static video_info_t vesa_vmode[VESA_MAXMODES + 1] = {
106	{ EOT, },
107};
108
109static int vesa_init_done = FALSE;
110static int has_vesa_bios = FALSE;
111static struct vesa_info *vesa_adp_info = NULL;
112static u_int16_t *vesa_vmodetab = NULL;
113
114/* local macros and functions */
115#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
116
117static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
118static int vesa_bios_set_mode(int mode);
119static int vesa_bios_set_dac(int bits);
120static int vesa_bios_save_palette(int start, int colors, u_char *palette);
121static int vesa_bios_load_palette(int start, int colors, u_char *palette);
122#define STATE_SIZE	0
123#define STATE_SAVE	1
124#define STATE_LOAD	2
125#define STATE_HW	(1<<0)
126#define STATE_DATA	(1<<1)
127#define STATE_DAC	(1<<2)
128#define STATE_REG	(1<<3)
129#define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
130#define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
131static int vesa_bios_state_buf_size(void);
132static int vesa_bios_save_restore(int code, void *p, size_t size);
133static int vesa_map_gen_mode_num(int type, int color, int mode);
134static int vesa_translate_flags(u_int16_t vflags);
135static int vesa_bios_init(void);
136static void vesa_clear_modes(video_info_t *info, int color);
137
138static void
139dump_buffer(u_char *buf, size_t len)
140{
141    int i;
142
143    for(i = 0; i < len;) {
144	printf("%02x ", buf[i]);
145	if ((++i % 16) == 0)
146	    printf("\n");
147    }
148}
149
150/* VESA BIOS calls */
151static int
152vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
153{
154	struct vm86frame vmf;
155	u_char buf[256];
156	int err;
157
158	bzero(&vmf, sizeof(vmf));
159	bzero(buf, sizeof(buf));
160	vmf.vmf_eax = 0x4f01;
161	vmf.vmf_ecx = mode;
162	err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf),
163			  &vmf.vmf_es, &vmf.vmf_di);
164	if ((err != 0) || (vmf.vmf_eax != 0x4f))
165		return 1;
166	bcopy(buf, vmode, sizeof(*vmode));
167	return 0;
168}
169
170static int
171vesa_bios_set_mode(int mode)
172{
173	struct vm86frame vmf;
174	int err;
175
176	bzero(&vmf, sizeof(vmf));
177	vmf.vmf_eax = 0x4f02;
178	vmf.vmf_ebx = mode;
179	err = vm86_intcall(0x10, &vmf);
180	return ((err != 0) || (vmf.vmf_eax != 0x4f));
181}
182
183static int
184vesa_bios_set_dac(int bits)
185{
186	struct vm86frame vmf;
187	int err;
188
189	bzero(&vmf, sizeof(vmf));
190	vmf.vmf_eax = 0x4f08;
191	vmf.vmf_ebx = (bits << 8);
192	err = vm86_intcall(0x10, &vmf);
193	return ((err != 0) || (vmf.vmf_eax != 0x4f));
194}
195
196static int
197vesa_bios_save_palette(int start, int colors, u_char *palette)
198{
199	struct vm86frame vmf;
200	u_char *p;
201	int err;
202	int i;
203
204	p = malloc(colors*4, M_DEVBUF, M_WAITOK);
205
206	bzero(&vmf, sizeof(vmf));
207	vmf.vmf_eax = 0x4f09;
208	vmf.vmf_ebx = 1;	/* get primary palette data */
209	vmf.vmf_ecx = colors;
210	vmf.vmf_edx = start;
211	err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di);
212	if ((err != 0) || (vmf.vmf_eax != 0x4f)) {
213		free(p, M_DEVBUF);
214		return 1;
215	}
216
217	for (i = 0; i < colors; ++i) {
218		palette[i*3]     = p[i*4 + 1];
219		palette[i*3 + 1] = p[i*4 + 2];
220		palette[i*3 + 2] = p[i*4 + 3];
221	}
222	free(p, M_DEVBUF);
223	return 0;
224}
225
226static int
227vesa_bios_load_palette(int start, int colors, u_char *palette)
228{
229	struct vm86frame vmf;
230	u_char *p;
231	int err;
232	int i;
233
234	p = malloc(colors*4, M_DEVBUF, M_WAITOK);
235	for (i = 0; i < colors; ++i) {
236		p[i*4]     = 0;
237		p[i*4 + 1] = palette[i*3];
238		p[i*4 + 2] = palette[i*3 + 1];
239		p[i*4 + 3] = palette[i*3 + 2];
240	}
241
242	bzero(&vmf, sizeof(vmf));
243	vmf.vmf_eax = 0x4f09;
244	vmf.vmf_ebx = 0;	/* set primary palette data */
245	vmf.vmf_ecx = colors;
246	vmf.vmf_edx = start;
247	err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di);
248	free(p, M_DEVBUF);
249	return ((err != 0) || (vmf.vmf_eax != 0x4f));
250}
251
252static int
253vesa_bios_state_buf_size(void)
254{
255	struct vm86frame vmf;
256	int err;
257
258	bzero(&vmf, sizeof(vmf));
259	vmf.vmf_eax = 0x4f04;
260	vmf.vmf_ecx = STATE_MOST;
261	vmf.vmf_edx = STATE_SIZE;
262	err = vm86_intcall(0x10, &vmf);
263	if ((err != 0) || (vmf.vmf_eax != 0x4f))
264		return 0;
265	return vmf.vmf_ebx*64;
266}
267
268static int
269vesa_bios_save_restore(int code, void *p, size_t size)
270{
271	struct vm86frame vmf;
272	int err;
273
274	bzero(&vmf, sizeof(vmf));
275	vmf.vmf_eax = 0x4f04;
276	vmf.vmf_ecx = STATE_MOST;
277	vmf.vmf_edx = code;	/* STATE_SAVE/STATE_LOAD */
278	err = vm86_datacall(0x10, &vmf, (char *)p, size,
279			  &vmf.vmf_es, &vmf.vmf_bx);
280	return ((err != 0) || (vmf.vmf_eax != 0x4f));
281}
282
283/* map a generic video mode to a known mode */
284static int
285vesa_map_gen_mode_num(int type, int color, int mode)
286{
287    static struct {
288	int from;
289	int to;
290    } mode_map[] = {
291	{ M_TEXT_132x25, M_VESA_C132x25 },
292	{ M_TEXT_132x43, M_VESA_C132x43 },
293	{ M_TEXT_132x50, M_VESA_C132x50 },
294	{ M_TEXT_132x60, M_VESA_C132x60 },
295    };
296    int i;
297
298    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
299        if (mode_map[i].from == mode)
300            return mode_map[i].to;
301    }
302    return mode;
303}
304
305static int
306vesa_translate_flags(u_int16_t vflags)
307{
308	static struct {
309		u_int16_t mask;
310		int set;
311		int reset;
312	} ftable[] = {
313		{ V_MODECOLOR, V_INFO_COLOR, 0 },
314		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
315		{ V_MODELFB, V_INFO_LENEAR, 0 },
316	};
317	int flags;
318	int i;
319
320	for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
321		flags |= (vflags & ftable[i].mask) ?
322			 ftable[i].set : ftable[i].reset;
323	}
324	return flags;
325}
326
327static int
328vesa_bios_init(void)
329{
330#define VESA_INFO_SIZE 512
331	static u_char buffer[VESA_INFO_SIZE * 2];
332	static u_char *buf = buffer;
333	struct vm86frame vmf;
334	struct vesa_mode vmode;
335	u_int32_t p;
336	u_short offset;
337	int modes;
338	int err;
339	int i;
340
341	if (vesa_init_done)
342		return 0;
343
344	has_vesa_bios = FALSE;
345	vesa_adp_info = NULL;
346	vesa_vmode[0].vi_mode = EOT;
347
348	bzero(&vmf, sizeof(vmf));	/* paranoia */
349	bzero(buffer, sizeof(buffer));
350	vmf.vmf_eax = 0x4f00;
351
352	/* workaround - see vm86.c:vm86_datacall */
353	offset = (u_int)buf & PAGE_MASK;
354	if ((offset + VESA_INFO_SIZE) & PG_FRAME)
355		buf += PAGE_SIZE - offset;
356
357	bcopy("VBE2", buf, 4);		/* try for VBE2 data */
358	err = vm86_datacall(0x10, &vmf, (char *)buf, VESA_INFO_SIZE,
359			  &vmf.vmf_es, &vmf.vmf_di);
360	if ((err != 0) || (vmf.vmf_eax != 0x4f) || bcmp("VESA", buf, 4))
361		return 1;
362	vesa_adp_info = (struct vesa_info *)buf;
363	if (bootverbose)
364		dump_buffer(buf, 64);
365	if (vesa_adp_info->v_flags & V_NONVGA)
366		return 1;
367
368	/* obtain video mode information */
369	p = BIOS_SADDRTOLADDR(vesa_adp_info->v_modetable);
370	vesa_vmodetab = (u_int16_t *)BIOS_PADDRTOVADDR(p);
371	for (i = 0, modes = 0; vesa_vmodetab[i] != 0xffff; ++i) {
372		if (modes >= VESA_MAXMODES)
373			break;
374		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
375			continue;
376
377		/* reject unsupported modes */
378#if 0
379		if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
380					| V_MODENONVGA))
381		    != (V_MODESUPP | V_MODEOPTINFO))
382			continue;
383#else
384		if ((vmode.v_modeattr & (V_MODEOPTINFO | V_MODENONVGA))
385		    != (V_MODEOPTINFO))
386			continue;
387#endif
388
389		/* copy some fields */
390		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
391		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
392		vesa_vmode[modes].vi_width = vmode.v_width;
393		vesa_vmode[modes].vi_height = vmode.v_height;
394		vesa_vmode[modes].vi_depth = vmode.v_bpp;
395		vesa_vmode[modes].vi_planes = vmode.v_planes;
396		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
397		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
398		vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
399		/* XXX window B */
400		vesa_vmode[modes].vi_window_size = vmode.v_wsize;
401		vesa_vmode[modes].vi_window_gran = vmode.v_wgran;
402		vesa_vmode[modes].vi_buffer = vmode.v_lfb;
403		vesa_vmode[modes].vi_buffer_size = vmode.v_offscreen;
404		/* pixel format, memory model... */
405		vesa_vmode[modes].vi_flags
406			= vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
407		++modes;
408	}
409	vesa_vmode[modes].vi_mode = EOT;
410	if (bootverbose)
411		printf("VESA: %d mode(s) found\n", modes);
412
413	has_vesa_bios = TRUE;
414	return 0;
415}
416
417static void
418vesa_clear_modes(video_info_t *info, int color)
419{
420	while (info->vi_mode != EOT) {
421		if ((info->vi_flags & V_INFO_COLOR) != color)
422			info->vi_mode = NA;
423		++info;
424	}
425}
426
427/* exported functions */
428
429static int
430vesa_init(void)
431{
432	int adapters;
433	int i;
434
435	adapters = (*prevvidsw.init)();
436	for (i = 0; i < adapters; ++i) {
437		if ((vesa_adp = (*prevvidsw.adapter)(i)) == NULL)
438			continue;
439		if (vesa_adp->va_type == KD_VGA) {
440			vesa_adp->va_flags |= V_ADP_VESA;
441			return adapters;
442		}
443	}
444	vesa_adp = NULL;
445	return adapters;
446}
447
448static video_adapter_t
449*vesa_adapter(int ad)
450{
451	return (*prevvidsw.adapter)(ad);
452}
453
454static int
455vesa_get_info(int ad, int mode, video_info_t *info)
456{
457	int i;
458
459	if ((*prevvidsw.get_info)(ad, mode, info) == 0)
460		return 0;
461
462	if (ad != vesa_adp->va_index)
463		return 1;
464
465	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
466				     vesa_adp->va_flags & V_ADP_COLOR, mode);
467	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
468		if (vesa_vmode[i].vi_mode == NA)
469			continue;
470		if (vesa_vmode[i].vi_mode == mode) {
471			*info = vesa_vmode[i];
472			return 0;
473		}
474	}
475	return 1;
476}
477
478static int
479vesa_query_mode(int ad, video_info_t *info)
480{
481	int i;
482
483	if ((i = (*prevvidsw.query_mode)(ad, info)) != -1)
484		return i;
485	if (ad != vesa_adp->va_index)
486		return -1;
487
488	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
489		if ((info->vi_width != 0)
490		    && (info->vi_width != vesa_vmode[i].vi_width))
491			continue;
492		if ((info->vi_height != 0)
493		    && (info->vi_height != vesa_vmode[i].vi_height))
494			continue;
495		if ((info->vi_cwidth != 0)
496		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
497			continue;
498		if ((info->vi_cheight != 0)
499		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
500			continue;
501		if ((info->vi_depth != 0)
502		    && (info->vi_depth != vesa_vmode[i].vi_depth))
503			continue;
504		if ((info->vi_planes != 0)
505		    && (info->vi_planes != vesa_vmode[i].vi_planes))
506			continue;
507		/* pixel format, memory model */
508		if ((info->vi_flags != 0)
509		    && (info->vi_flags != vesa_vmode[i].vi_flags))
510			continue;
511		return vesa_vmode[i].vi_mode;
512	}
513	return -1;
514}
515
516static int
517vesa_set_mode(int ad, int mode)
518{
519	video_info_t info;
520	size_t len;
521
522	if (ad != vesa_adp->va_index)
523		return (*prevvidsw.set_mode)(ad, mode);
524
525	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
526				     vesa_adp->va_flags & V_ADP_COLOR, mode);
527#ifdef SC_VIDEO_DEBUG
528	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
529		vesa_adp->va_mode, vesa_adp->va_mode, mode, mode);
530#endif
531	/*
532	 * If the current mode is a VESA mode and the new mode is not,
533	 * restore the state of the adapter first, so that non-standard,
534	 * extended SVGA registers are set to the state compatible with
535	 * the standard VGA modes. Otherwise (*prevvidsw.set_mode)()
536	 * may not be able to set up the new mode correctly.
537	 */
538	if (VESA_MODE(vesa_adp->va_mode)) {
539		if ((*prevvidsw.get_info)(ad, mode, &info) == 0) {
540			/* assert(vesa_state_buf != NULL); */
541		    	if ((vesa_state_buf == NULL)
542			    || vesa_load_state(ad, vesa_state_buf))
543				return 1;
544			free(vesa_state_buf, M_DEVBUF);
545			vesa_state_buf = NULL;
546#ifdef SC_VIDEO_DEBUG
547			printf("VESA: restored\n");
548#endif
549		}
550		/*
551		 * once (*prevvidsw.get_info)() succeeded,
552		 * (*prevvidsw.set_mode)() below won't fail...
553		 */
554	}
555
556	/* we may not need to handle this mode after all... */
557	if ((*prevvidsw.set_mode)(ad, mode) == 0)
558		return 0;
559
560	/* is the new mode supported? */
561	if (vesa_get_info(ad, mode, &info))
562		return 1;
563	/* assert(VESA_MODE(mode)); */
564
565#ifdef SC_VIDEO_DEBUG
566	printf("VESA: about to set a VESA mode...\n");
567#endif
568	/*
569	 * If the current mode is not a VESA mode, save the current state
570	 * so that the adapter state can be restored later when a non-VESA
571	 * mode is to be set up. See above.
572	 */
573	if (!VESA_MODE(vesa_adp->va_mode) && (vesa_state_buf == NULL)) {
574		len = vesa_save_state(ad, NULL, 0);
575		vesa_state_buf = malloc(len, M_DEVBUF, M_WAITOK);
576		if (vesa_save_state(ad, vesa_state_buf, len)) {
577#ifdef SC_VIDEO_DEBUG
578			printf("VESA: state save failed! (len=%d)\n", len);
579#endif
580			free(vesa_state_buf, M_DEVBUF);
581			vesa_state_buf = NULL;
582			return 1;
583		}
584#ifdef SC_VIDEO_DEBUG
585		printf("VESA: saved (len=%d)\n", len);
586		dump_buffer(vesa_state_buf, len);
587#endif
588	}
589
590	if (vesa_bios_set_mode(mode))
591		return 1;
592
593#ifdef SC_VIDEO_DEBUG
594	printf("VESA: mode set!\n");
595#endif
596	vesa_adp->va_mode = mode;
597	vesa_adp->va_flags &= ~V_ADP_COLOR;
598	vesa_adp->va_flags |=
599		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
600	vesa_adp->va_crtc_addr =
601		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_BASE : MONO_BASE;
602	vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
603	vesa_adp->va_window_size = info.vi_window_size;
604	vesa_adp->va_window_gran = info.vi_window_gran;
605	if (info.vi_buffer_size == 0) {
606		vesa_adp->va_buffer = 0;
607		vesa_adp->va_buffer_size = 0;
608	} else {
609		vesa_adp->va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
610		vesa_adp->va_buffer_size = info.vi_buffer_size;
611	}
612
613	return 0;
614}
615
616static int
617vesa_save_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
618{
619	return (*prevvidsw.save_font)(ad, page, fontsize, data, ch, count);
620}
621
622static int
623vesa_load_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
624{
625	return (*prevvidsw.load_font)(ad, page, fontsize, data, ch, count);
626}
627
628static int
629vesa_show_font(int ad, int page)
630{
631	return (*prevvidsw.show_font)(ad, page);
632}
633
634static int
635vesa_save_palette(int ad, u_char *palette)
636{
637	if ((ad != vesa_adp->va_index) || !(vesa_adp_info->v_flags & V_DAC8)
638	    || vesa_bios_set_dac(8))
639		return (*prevvidsw.save_palette)(ad, palette);
640
641	return vesa_bios_save_palette(0, 256, palette);
642}
643
644static int
645vesa_load_palette(int ad, u_char *palette)
646{
647	if ((ad != vesa_adp->va_index) || !(vesa_adp_info->v_flags & V_DAC8)
648	    || vesa_bios_set_dac(8))
649		return (*prevvidsw.load_palette)(ad, palette);
650
651	return vesa_bios_load_palette(0, 256, palette);
652}
653
654static int
655vesa_set_border(int ad, int color)
656{
657	return (*prevvidsw.set_border)(ad, color);
658}
659
660static int
661vesa_save_state(int ad, void *p, size_t size)
662{
663	if (ad != vesa_adp->va_index)
664		return (*prevvidsw.save_state)(ad, p, size);
665
666	if (vesa_state_buf_size == 0)
667		vesa_state_buf_size = vesa_bios_state_buf_size();
668	if (size == 0)
669		return (sizeof(int) + vesa_state_buf_size);
670	else if (size < (sizeof(int) + vesa_state_buf_size))
671		return 1;
672
673	((adp_state_t *)p)->sig = V_STATE_SIG;
674	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
675	return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
676				      vesa_state_buf_size);
677}
678
679static int
680vesa_load_state(int ad, void *p)
681{
682	if ((ad != vesa_adp->va_index)
683	    || (((adp_state_t *)p)->sig != V_STATE_SIG))
684		return (*prevvidsw.load_state)(ad, p);
685
686	return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
687				      vesa_state_buf_size);
688}
689
690static int
691vesa_set_origin(int ad, off_t offset)
692{
693	struct vm86frame vmf;
694	int err;
695
696	/*
697	 * This function should return as quickly as possible to
698	 * maintain good performance of the system. For this reason,
699	 * error checking is kept minimal and let the VESA BIOS to
700	 * detect error.
701	 */
702	if (ad != vesa_adp->va_index)
703		return (*prevvidsw.set_win_org)(ad, offset);
704
705	if (vesa_adp->va_window_gran == 0)
706		return 1;
707	bzero(&vmf, sizeof(vmf));
708	vmf.vmf_eax = 0x4f05;
709	vmf.vmf_ebx = 0;		/* WINDOW_A, XXX */
710	vmf.vmf_edx = offset/vesa_adp->va_window_gran;
711	err = vm86_intcall(0x10, &vmf);
712	return ((err != 0) || (vmf.vmf_eax != 0x4f));
713}
714
715static int
716vesa_read_hw_cursor(int ad, int *col, int *row)
717{
718	return (*prevvidsw.read_hw_cursor)(ad, col, row);
719}
720
721static int
722vesa_set_hw_cursor(int ad, int col, int row)
723{
724	return (*prevvidsw.set_hw_cursor)(ad, col, row);
725}
726
727static int
728vesa_diag(int level)
729{
730	struct vesa_mode vmode;
731	u_int32_t p;
732	int i;
733
734#ifndef VESA_MODULE
735	/* call the previous handler first */
736	(*prevvidsw.diag)(level);
737#endif
738
739	/* general adapter information */
740	printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
741	       ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
742		   + ((vesa_adp_info->v_version & 0x0f00) >> 8),
743	       ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
744		   + (vesa_adp_info->v_version & 0x000f),
745	       vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
746	       vesa_vmodetab, vesa_adp_info->v_modetable);
747	/* OEM string */
748	p = BIOS_SADDRTOLADDR(vesa_adp_info->v_oemstr);
749	if (p != 0)
750		printf("VESA: %s\n", (char *)BIOS_PADDRTOVADDR(p));
751
752	if (level <= 0)
753		return 0;
754
755	if (vesa_adp_info->v_version >= 0x0200) {
756		/* vendor name */
757		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_venderstr);
758		if (p != 0)
759			printf("VESA: %s, ", (char *)BIOS_PADDRTOVADDR(p));
760		/* product name */
761		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_prodstr);
762		if (p != 0)
763			printf("%s, ", (char *)BIOS_PADDRTOVADDR(p));
764		/* product revision */
765		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_revstr);
766		if (p != 0)
767			printf("%s\n", (char *)BIOS_PADDRTOVADDR(p));
768	}
769
770	/* mode information */
771	for (i = 0; vesa_vmodetab[i] != 0xffff; ++i) {
772		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
773			continue;
774
775		/* print something for diagnostic purpose */
776		printf("VESA: mode:0x%03x, flags:0x%04x",
777		       vesa_vmodetab[i], vmode.v_modeattr);
778		if (vmode.v_modeattr & V_MODEOPTINFO) {
779			if (vmode.v_modeattr & V_MODEGRAPHICS) {
780				printf(", G %dx%dx%d %d, ",
781				       vmode.v_width, vmode.v_height,
782				       vmode.v_bpp, vmode.v_planes);
783			} else {
784				printf(", T %dx%d, ",
785				       vmode.v_width, vmode.v_height);
786			}
787			printf("font:%dx%d",
788			       vmode.v_cwidth, vmode.v_cheight);
789		}
790		if (vmode.v_modeattr & V_MODELFB) {
791			printf(", mem:%d, LFB:0x%x, off:0x%x",
792			       vmode.v_memmodel, vmode.v_lfb,
793			       vmode.v_offscreen);
794		}
795		printf("\n");
796		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
797		       vmode.v_waseg, vmode.v_waattr,
798		       vmode.v_wbseg, vmode.v_wbattr);
799		printf("size:%dk, gran:%dk\n",
800		       vmode.v_wsize, vmode.v_wgran);
801	}
802
803	return 0;
804}
805
806/* module loading */
807
808#ifdef VESA_MODULE
809static int
810vesa_load(struct lkm_table *lkmtp, int cmd)
811#else
812int
813vesa_load(void)
814#endif
815{
816	int adapters;
817	int error;
818	int s;
819	int i;
820
821	if (vesa_init_done)
822		return 0;
823
824	/*
825	 * If the VESA module is statically linked to the kernel, or
826	 * it has already been loaded, abort loading this module this time.
827	 */
828	vesa_adp = NULL;
829	adapters = (*biosvidsw.init)();
830	for (i = 0; i < adapters; ++i) {
831		if ((vesa_adp = (*biosvidsw.adapter)(i)) == NULL)
832			continue;
833		if (vesa_adp->va_flags & V_ADP_VESA)
834			return ENXIO;
835		if (vesa_adp->va_type == KD_VGA)
836			break;
837	}
838	/* if a VGA adapter is not found, abort */
839	if (i >= adapters)
840		return ENXIO;
841
842	if (vesa_bios_init())
843		return ENXIO;
844	vesa_adp->va_flags |= V_ADP_VESA;
845
846	/* remove conflicting modes if we have more than one adapter */
847	if (adapters > 1) {
848		vesa_clear_modes(vesa_vmode,
849				 (vesa_adp->va_flags & V_ADP_COLOR) ?
850				     V_INFO_COLOR : 0);
851	}
852
853#ifdef VESA_MODULE
854	s = spltty();
855#endif
856	if ((error = vesa_load_ioctl()) == 0) {
857		bcopy(&biosvidsw, &prevvidsw, sizeof(prevvidsw));
858		bcopy(&vesavidsw, &biosvidsw, sizeof(vesavidsw));
859		vesa_init_done = TRUE;
860	}
861#ifdef VESA_MODULE
862	splx(s);
863
864	if (error == 0)
865		vesa_diag(bootverbose);
866#endif
867
868	return error;
869}
870
871#ifdef VESA_MODULE
872
873static int
874vesa_unload(struct lkm_table *lkmtp, int cmd)
875{
876	int error;
877	int s;
878
879	/* if the adapter is currently in a VESA mode, don't unload */
880	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
881		return EBUSY;
882	/*
883	 * FIXME: if there is at least one vty which is in a VESA mode,
884	 * we shouldn't be unloading! XXX
885	 */
886
887	s = spltty();
888	if ((error = vesa_unload_ioctl()) == 0) {
889		if (vesa_adp)
890			vesa_adp->va_flags &= ~V_ADP_VESA;
891		bcopy(&prevvidsw, &biosvidsw, sizeof(biosvidsw));
892	}
893	splx(s);
894
895	return error;
896}
897
898int
899vesa_mod(struct lkm_table *lkmtp, int cmd, int ver)
900{
901	MOD_DISPATCH(vesa, lkmtp, cmd, ver,
902		vesa_load, vesa_unload, lkm_nullcmd);
903}
904
905#endif /* VESA_MODULE */
906
907#endif /* (NSC > 0 && VESA && VM86) || VESA_MODULE */
908