vesa.c revision 39643
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.2 1998/09/23 09:59:00 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	static u_char buf[512];
331	struct vm86frame vmf;
332	struct vesa_mode vmode;
333	u_int32_t p;
334	int modes;
335	int err;
336	int i;
337
338	if (vesa_init_done)
339		return 0;
340
341	has_vesa_bios = FALSE;
342	vesa_adp_info = NULL;
343	vesa_vmode[0].vi_mode = EOT;
344
345	bzero(&vmf, sizeof(vmf));	/* paranoia */
346	bzero(buf, sizeof(buf));
347	bcopy("VBE2", buf, 4);		/* try for VBE2 data */
348	vmf.vmf_eax = 0x4f00;
349	err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf),
350			  &vmf.vmf_es, &vmf.vmf_di);
351	if ((err != 0) || (vmf.vmf_eax != 0x4f) || bcmp("VESA", buf, 4))
352		return 1;
353	vesa_adp_info = (struct vesa_info *)buf;
354	if (bootverbose)
355		dump_buffer(buf, 64);
356	if (vesa_adp_info->v_flags & V_NONVGA)
357		return 1;
358
359	/* obtain video mode information */
360	p = BIOS_SADDRTOLADDR(vesa_adp_info->v_modetable);
361	vesa_vmodetab = (u_int16_t *)BIOS_PADDRTOVADDR(p);
362	for (i = 0, modes = 0; vesa_vmodetab[i] != 0xffff; ++i) {
363		if (modes >= VESA_MAXMODES)
364			break;
365		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
366			continue;
367
368		/* reject unsupported modes */
369#if 0
370		if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
371					| V_MODENONVGA))
372		    != (V_MODESUPP | V_MODEOPTINFO))
373			continue;
374#else
375		if ((vmode.v_modeattr & (V_MODEOPTINFO | V_MODENONVGA))
376		    != (V_MODEOPTINFO))
377			continue;
378#endif
379
380		/* copy some fields */
381		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
382		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
383		vesa_vmode[modes].vi_width = vmode.v_width;
384		vesa_vmode[modes].vi_height = vmode.v_height;
385		vesa_vmode[modes].vi_depth = vmode.v_bpp;
386		vesa_vmode[modes].vi_planes = vmode.v_planes;
387		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
388		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
389		vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
390		/* XXX window B */
391		vesa_vmode[modes].vi_window_size = vmode.v_wsize;
392		vesa_vmode[modes].vi_window_gran = vmode.v_wgran;
393		vesa_vmode[modes].vi_buffer = vmode.v_lfb;
394		vesa_vmode[modes].vi_buffer_size = vmode.v_offscreen;
395		/* pixel format, memory model... */
396		vesa_vmode[modes].vi_flags
397			= vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
398		++modes;
399	}
400	vesa_vmode[modes].vi_mode = EOT;
401	if (bootverbose)
402		printf("VESA: %d mode(s) found\n", modes);
403
404	has_vesa_bios = TRUE;
405	return 0;
406}
407
408static void
409vesa_clear_modes(video_info_t *info, int color)
410{
411	while (info->vi_mode != EOT) {
412		if ((info->vi_flags & V_INFO_COLOR) != color)
413			info->vi_mode = NA;
414		++info;
415	}
416}
417
418/* exported functions */
419
420static int
421vesa_init(void)
422{
423	int adapters;
424	int i;
425
426	adapters = (*prevvidsw.init)();
427	for (i = 0; i < adapters; ++i) {
428		if ((vesa_adp = (*prevvidsw.adapter)(i)) == NULL)
429			continue;
430		if (vesa_adp->va_type == KD_VGA) {
431			vesa_adp->va_flags |= V_ADP_VESA;
432			return adapters;
433		}
434	}
435	vesa_adp = NULL;
436	return adapters;
437}
438
439static video_adapter_t
440*vesa_adapter(int ad)
441{
442	return (*prevvidsw.adapter)(ad);
443}
444
445static int
446vesa_get_info(int ad, int mode, video_info_t *info)
447{
448	int i;
449
450	if ((*prevvidsw.get_info)(ad, mode, info) == 0)
451		return 0;
452
453	if (ad != vesa_adp->va_index)
454		return 1;
455
456	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
457				     vesa_adp->va_flags & V_ADP_COLOR, mode);
458	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
459		if (vesa_vmode[i].vi_mode == NA)
460			continue;
461		if (vesa_vmode[i].vi_mode == mode) {
462			*info = vesa_vmode[i];
463			return 0;
464		}
465	}
466	return 1;
467}
468
469static int
470vesa_query_mode(int ad, video_info_t *info)
471{
472	int i;
473
474	if ((i = (*prevvidsw.query_mode)(ad, info)) != -1)
475		return i;
476	if (ad != vesa_adp->va_index)
477		return -1;
478
479	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
480		if ((info->vi_width != 0)
481		    && (info->vi_width != vesa_vmode[i].vi_width))
482			continue;
483		if ((info->vi_height != 0)
484		    && (info->vi_height != vesa_vmode[i].vi_height))
485			continue;
486		if ((info->vi_cwidth != 0)
487		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
488			continue;
489		if ((info->vi_cheight != 0)
490		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
491			continue;
492		if ((info->vi_depth != 0)
493		    && (info->vi_depth != vesa_vmode[i].vi_depth))
494			continue;
495		if ((info->vi_planes != 0)
496		    && (info->vi_planes != vesa_vmode[i].vi_planes))
497			continue;
498		/* pixel format, memory model */
499		if ((info->vi_flags != 0)
500		    && (info->vi_flags != vesa_vmode[i].vi_flags))
501			continue;
502		return vesa_vmode[i].vi_mode;
503	}
504	return -1;
505}
506
507static int
508vesa_set_mode(int ad, int mode)
509{
510	video_info_t info;
511	size_t len;
512
513	if (ad != vesa_adp->va_index)
514		return (*prevvidsw.set_mode)(ad, mode);
515
516	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
517				     vesa_adp->va_flags & V_ADP_COLOR, mode);
518#ifdef SC_VIDEO_DEBUG
519	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
520		vesa_adp->va_mode, vesa_adp->va_mode, mode, mode);
521#endif
522	/*
523	 * If the current mode is a VESA mode and the new mode is not,
524	 * restore the state of the adapter first, so that non-standard,
525	 * extended SVGA registers are set to the state compatible with
526	 * the standard VGA modes. Otherwise (*prevvidsw.set_mode)()
527	 * may not be able to set up the new mode correctly.
528	 */
529	if (VESA_MODE(vesa_adp->va_mode)) {
530		if ((*prevvidsw.get_info)(ad, mode, &info) == 0) {
531			/* assert(vesa_state_buf != NULL); */
532		    	if ((vesa_state_buf == NULL)
533			    || vesa_load_state(ad, vesa_state_buf))
534				return 1;
535			free(vesa_state_buf, M_DEVBUF);
536			vesa_state_buf = NULL;
537#ifdef SC_VIDEO_DEBUG
538			printf("VESA: restored\n");
539#endif
540		}
541		/*
542		 * once (*prevvidsw.get_info)() succeeded,
543		 * (*prevvidsw.set_mode)() below won't fail...
544		 */
545	}
546
547	/* we may not need to handle this mode after all... */
548	if ((*prevvidsw.set_mode)(ad, mode) == 0)
549		return 0;
550
551	/* is the new mode supported? */
552	if (vesa_get_info(ad, mode, &info))
553		return 1;
554	/* assert(VESA_MODE(mode)); */
555
556#ifdef SC_VIDEO_DEBUG
557	printf("VESA: about to set a VESA mode...\n");
558#endif
559	/*
560	 * If the current mode is not a VESA mode, save the current state
561	 * so that the adapter state can be restored later when a non-VESA
562	 * mode is to be set up. See above.
563	 */
564	if (!VESA_MODE(vesa_adp->va_mode) && (vesa_state_buf == NULL)) {
565		len = vesa_save_state(ad, NULL, 0);
566		vesa_state_buf = malloc(len, M_DEVBUF, M_WAITOK);
567		if (vesa_save_state(ad, vesa_state_buf, len)) {
568#ifdef SC_VIDEO_DEBUG
569			printf("VESA: state save failed! (len=%d)\n", len);
570#endif
571			free(vesa_state_buf, M_DEVBUF);
572			vesa_state_buf = NULL;
573			return 1;
574		}
575#ifdef SC_VIDEO_DEBUG
576		printf("VESA: saved (len=%d)\n", len);
577		dump_buffer(vesa_state_buf, len);
578#endif
579	}
580
581	if (vesa_bios_set_mode(mode))
582		return 1;
583
584#ifdef SC_VIDEO_DEBUG
585	printf("VESA: mode set!\n");
586#endif
587	vesa_adp->va_mode = mode;
588	vesa_adp->va_flags &= ~V_ADP_COLOR;
589	vesa_adp->va_flags |=
590		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
591	vesa_adp->va_crtc_addr =
592		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_BASE : MONO_BASE;
593	vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
594	vesa_adp->va_window_size = info.vi_window_size;
595	vesa_adp->va_window_gran = info.vi_window_gran;
596	if (info.vi_buffer_size == 0) {
597		vesa_adp->va_buffer = 0;
598		vesa_adp->va_buffer_size = 0;
599	} else {
600		vesa_adp->va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer);
601		vesa_adp->va_buffer_size = info.vi_buffer_size;
602	}
603
604	return 0;
605}
606
607static int
608vesa_save_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
609{
610	return (*prevvidsw.save_font)(ad, page, fontsize, data, ch, count);
611}
612
613static int
614vesa_load_font(int ad, int page, int fontsize, u_char *data, int ch, int count)
615{
616	return (*prevvidsw.load_font)(ad, page, fontsize, data, ch, count);
617}
618
619static int
620vesa_show_font(int ad, int page)
621{
622	return (*prevvidsw.show_font)(ad, page);
623}
624
625static int
626vesa_save_palette(int ad, u_char *palette)
627{
628	if ((ad != vesa_adp->va_index) || !(vesa_adp_info->v_flags & V_DAC8)
629	    || vesa_bios_set_dac(8))
630		return (*prevvidsw.save_palette)(ad, palette);
631
632	return vesa_bios_save_palette(0, 256, palette);
633}
634
635static int
636vesa_load_palette(int ad, u_char *palette)
637{
638	if ((ad != vesa_adp->va_index) || !(vesa_adp_info->v_flags & V_DAC8)
639	    || vesa_bios_set_dac(8))
640		return (*prevvidsw.load_palette)(ad, palette);
641
642	return vesa_bios_load_palette(0, 256, palette);
643}
644
645static int
646vesa_set_border(int ad, int color)
647{
648	return (*prevvidsw.set_border)(ad, color);
649}
650
651static int
652vesa_save_state(int ad, void *p, size_t size)
653{
654	if (ad != vesa_adp->va_index)
655		return (*prevvidsw.save_state)(ad, p, size);
656
657	if (vesa_state_buf_size == 0)
658		vesa_state_buf_size = vesa_bios_state_buf_size();
659	if (size == 0)
660		return (sizeof(int) + vesa_state_buf_size);
661	else if (size < (sizeof(int) + vesa_state_buf_size))
662		return 1;
663
664	((adp_state_t *)p)->sig = V_STATE_SIG;
665	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
666	return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
667				      vesa_state_buf_size);
668}
669
670static int
671vesa_load_state(int ad, void *p)
672{
673	if ((ad != vesa_adp->va_index)
674	    || (((adp_state_t *)p)->sig != V_STATE_SIG))
675		return (*prevvidsw.load_state)(ad, p);
676
677	return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
678				      vesa_state_buf_size);
679}
680
681static int
682vesa_set_origin(int ad, off_t offset)
683{
684	struct vm86frame vmf;
685	int err;
686
687	/*
688	 * This function should return as quickly as possible to
689	 * maintain good performance of the system. For this reason,
690	 * error checking is kept minimal and let the VESA BIOS to
691	 * detect error.
692	 */
693	if (ad != vesa_adp->va_index)
694		return (*prevvidsw.set_win_org)(ad, offset);
695
696	if (vesa_adp->va_window_gran == 0)
697		return 1;
698	bzero(&vmf, sizeof(vmf));
699	vmf.vmf_eax = 0x4f05;
700	vmf.vmf_ebx = 0;		/* WINDOW_A, XXX */
701	vmf.vmf_edx = offset/vesa_adp->va_window_gran;
702	err = vm86_intcall(0x10, &vmf);
703	return ((err != 0) || (vmf.vmf_eax != 0x4f));
704}
705
706static int
707vesa_read_hw_cursor(int ad, int *col, int *row)
708{
709	return (*prevvidsw.read_hw_cursor)(ad, col, row);
710}
711
712static int
713vesa_set_hw_cursor(int ad, int col, int row)
714{
715	return (*prevvidsw.set_hw_cursor)(ad, col, row);
716}
717
718static int
719vesa_diag(int level)
720{
721	struct vesa_mode vmode;
722	u_int32_t p;
723	int i;
724
725#ifndef VESA_MODULE
726	/* call the previous handler first */
727	(*prevvidsw.diag)(level);
728#endif
729
730	/* general adapter information */
731	printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
732	       ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
733		   + ((vesa_adp_info->v_version & 0x0f00) >> 8),
734	       ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
735		   + (vesa_adp_info->v_version & 0x000f),
736	       vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
737	       vesa_vmodetab, vesa_adp_info->v_modetable);
738	/* OEM string */
739	p = BIOS_SADDRTOLADDR(vesa_adp_info->v_oemstr);
740	if (p != 0)
741		printf("VESA: %s\n", (char *)BIOS_PADDRTOVADDR(p));
742
743	if (level <= 0)
744		return 0;
745
746	if (vesa_adp_info->v_version >= 0x0200) {
747		/* vendor name */
748		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_venderstr);
749		if (p != 0)
750			printf("VESA: %s, ", (char *)BIOS_PADDRTOVADDR(p));
751		/* product name */
752		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_prodstr);
753		if (p != 0)
754			printf("%s, ", (char *)BIOS_PADDRTOVADDR(p));
755		/* product revision */
756		p = BIOS_SADDRTOLADDR(vesa_adp_info->v_revstr);
757		if (p != 0)
758			printf("%s\n", (char *)BIOS_PADDRTOVADDR(p));
759	}
760
761	/* mode information */
762	for (i = 0; vesa_vmodetab[i] != 0xffff; ++i) {
763		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
764			continue;
765
766		/* print something for diagnostic purpose */
767		printf("VESA: mode:0x%03x, flags:0x%04x",
768		       vesa_vmodetab[i], vmode.v_modeattr);
769		if (vmode.v_modeattr & V_MODEOPTINFO) {
770			if (vmode.v_modeattr & V_MODEGRAPHICS) {
771				printf(", G %dx%dx%d %d, ",
772				       vmode.v_width, vmode.v_height,
773				       vmode.v_bpp, vmode.v_planes);
774			} else {
775				printf(", T %dx%d, ",
776				       vmode.v_width, vmode.v_height);
777			}
778			printf("font:%dx%d",
779			       vmode.v_cwidth, vmode.v_cheight);
780		}
781		if (vmode.v_modeattr & V_MODELFB) {
782			printf(", mem:%d, LFB:0x%x, off:0x%x",
783			       vmode.v_memmodel, vmode.v_lfb,
784			       vmode.v_offscreen);
785		}
786		printf("\n");
787		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
788		       vmode.v_waseg, vmode.v_waattr,
789		       vmode.v_wbseg, vmode.v_wbattr);
790		printf("size:%dk, gran:%dk\n",
791		       vmode.v_wsize, vmode.v_wgran);
792	}
793
794	return 0;
795}
796
797/* module loading */
798
799#ifdef VESA_MODULE
800static int
801vesa_load(struct lkm_table *lkmtp, int cmd)
802#else
803int
804vesa_load(void)
805#endif
806{
807	int adapters;
808	int error;
809	int s;
810	int i;
811
812	if (vesa_init_done)
813		return 0;
814
815	/*
816	 * If the VESA module is statically linked to the kernel, or
817	 * it has already been loaded, abort loading this module this time.
818	 */
819	vesa_adp = NULL;
820	adapters = (*biosvidsw.init)();
821	for (i = 0; i < adapters; ++i) {
822		if ((vesa_adp = (*biosvidsw.adapter)(i)) == NULL)
823			continue;
824		if (vesa_adp->va_flags & V_ADP_VESA)
825			return ENXIO;
826		if (vesa_adp->va_type == KD_VGA)
827			break;
828	}
829	/* if a VGA adapter is not found, abort */
830	if (i >= adapters)
831		return ENXIO;
832
833	if (vesa_bios_init())
834		return ENXIO;
835	vesa_adp->va_flags |= V_ADP_VESA;
836
837	/* remove conflicting modes if we have more than one adapter */
838	if (adapters > 1) {
839		vesa_clear_modes(vesa_vmode,
840				 (vesa_adp->va_flags & V_ADP_COLOR) ?
841				     V_INFO_COLOR : 0);
842	}
843
844#ifdef VESA_MODULE
845	s = spltty();
846#endif
847	if ((error = vesa_load_ioctl()) == 0) {
848		bcopy(&biosvidsw, &prevvidsw, sizeof(prevvidsw));
849		bcopy(&vesavidsw, &biosvidsw, sizeof(vesavidsw));
850		vesa_init_done = TRUE;
851	}
852#ifdef VESA_MODULE
853	splx(s);
854
855	if (error == 0)
856		vesa_diag(bootverbose);
857#endif
858
859	return error;
860}
861
862#ifdef VESA_MODULE
863
864static int
865vesa_unload(struct lkm_table *lkmtp, int cmd)
866{
867	int error;
868	int s;
869
870	/* if the adapter is currently in a VESA mode, don't unload */
871	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
872		return EBUSY;
873	/*
874	 * FIXME: if there is at least one vty which is in a VESA mode,
875	 * we shouldn't be unloading! XXX
876	 */
877
878	s = spltty();
879	if ((error = vesa_unload_ioctl()) == 0) {
880		if (vesa_adp)
881			vesa_adp->va_flags &= ~V_ADP_VESA;
882		bcopy(&prevvidsw, &biosvidsw, sizeof(biosvidsw));
883	}
884	splx(s);
885
886	return error;
887}
888
889int
890vesa_mod(struct lkm_table *lkmtp, int cmd, int ver)
891{
892	MOD_DISPATCH(vesa, lkmtp, cmd, ver,
893		vesa_load, vesa_unload, lkm_nullcmd);
894}
895
896#endif /* VESA_MODULE */
897
898#endif /* (NSC > 0 && VESA && VM86) || VESA_MODULE */
899