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