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