vesa.c revision 212070
1/*-
2 * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3 * Copyright (c) 2009-2010 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 212070 2010-08-31 20:21:52Z 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 void *vesa_state_buf = NULL;
84static uint32_t vesa_state_buf_offs = 0;
85static ssize_t vesa_state_buf_size = 0;
86
87static u_char *vesa_palette = NULL;
88static uint32_t vesa_palette_offs = 0;
89
90static void *vesa_bios = NULL;
91static uint32_t vesa_bios_offs = VESA_BIOS_OFFSET;
92static uint32_t vesa_bios_int10 = 0;
93static size_t vesa_bios_size = 0;
94
95/* VESA video adapter */
96static video_adapter_t *vesa_adp = NULL;
97
98SYSCTL_NODE(_debug, OID_AUTO, vesa, CTLFLAG_RD, NULL, "VESA debugging");
99static int vesa_shadow_rom = 1;
100TUNABLE_INT("debug.vesa.shadow_rom", &vesa_shadow_rom);
101SYSCTL_INT(_debug_vesa, OID_AUTO, shadow_rom, CTLFLAG_RDTUN, &vesa_shadow_rom,
102    1, "Enable video BIOS shadow");
103
104/* VESA functions */
105#if 0
106static int			vesa_nop(void);
107#endif
108static int			vesa_error(void);
109static vi_probe_t		vesa_probe;
110static vi_init_t		vesa_init;
111static vi_get_info_t		vesa_get_info;
112static vi_query_mode_t		vesa_query_mode;
113static vi_set_mode_t		vesa_set_mode;
114static vi_save_font_t		vesa_save_font;
115static vi_load_font_t		vesa_load_font;
116static vi_show_font_t		vesa_show_font;
117static vi_save_palette_t	vesa_save_palette;
118static vi_load_palette_t	vesa_load_palette;
119static vi_set_border_t		vesa_set_border;
120static vi_save_state_t		vesa_save_state;
121static vi_load_state_t		vesa_load_state;
122static vi_set_win_org_t		vesa_set_origin;
123static vi_read_hw_cursor_t	vesa_read_hw_cursor;
124static vi_set_hw_cursor_t	vesa_set_hw_cursor;
125static vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
126static vi_blank_display_t	vesa_blank_display;
127static vi_mmap_t		vesa_mmap;
128static vi_ioctl_t		vesa_ioctl;
129static vi_clear_t		vesa_clear;
130static vi_fill_rect_t		vesa_fill_rect;
131static vi_bitblt_t		vesa_bitblt;
132static vi_diag_t		vesa_diag;
133static int			vesa_bios_info(int level);
134
135static video_switch_t vesavidsw = {
136	vesa_probe,
137	vesa_init,
138	vesa_get_info,
139	vesa_query_mode,
140	vesa_set_mode,
141	vesa_save_font,
142	vesa_load_font,
143	vesa_show_font,
144	vesa_save_palette,
145	vesa_load_palette,
146	vesa_set_border,
147	vesa_save_state,
148	vesa_load_state,
149	vesa_set_origin,
150	vesa_read_hw_cursor,
151	vesa_set_hw_cursor,
152	vesa_set_hw_cursor_shape,
153	vesa_blank_display,
154	vesa_mmap,
155	vesa_ioctl,
156	vesa_clear,
157	vesa_fill_rect,
158	vesa_bitblt,
159	vesa_error,
160	vesa_error,
161	vesa_diag,
162};
163
164static video_switch_t *prevvidsw;
165
166/* VESA BIOS video modes */
167#define VESA_MAXMODES	64
168#define EOT		(-1)
169#define NA		(-2)
170
171#define MODE_TABLE_DELTA 8
172
173static int vesa_vmode_max = 0;
174static video_info_t vesa_vmode_empty = { EOT };
175static video_info_t *vesa_vmode = &vesa_vmode_empty;
176
177static int vesa_init_done = FALSE;
178static int has_vesa_bios = FALSE;
179static struct vesa_info *vesa_adp_info = NULL;
180static u_int16_t *vesa_vmodetab = NULL;
181static char *vesa_oemstr = NULL;
182static char *vesa_venderstr = NULL;
183static char *vesa_prodstr = NULL;
184static char *vesa_revstr = NULL;
185
186/* local macros and functions */
187#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
188
189static int int10_set_mode(int mode);
190static int vesa_bios_post(void);
191static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags);
192static int vesa_bios_set_mode(int mode);
193#if 0
194static int vesa_bios_get_dac(void);
195#endif
196static int vesa_bios_set_dac(int bits);
197static int vesa_bios_save_palette(int start, int colors, u_char *palette,
198				  int bits);
199static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
200				   u_char *b, int bits);
201static int vesa_bios_load_palette(int start, int colors, u_char *palette,
202				  int bits);
203static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
204				   u_char *b, int bits);
205#define STATE_SIZE	0
206#define STATE_SAVE	1
207#define STATE_LOAD	2
208#define STATE_HW	(1<<0)
209#define STATE_DATA	(1<<1)
210#define STATE_DAC	(1<<2)
211#define STATE_REG	(1<<3)
212#define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
213#define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
214static ssize_t vesa_bios_state_buf_size(void);
215static int vesa_bios_save_restore(int code, void *p);
216#ifdef MODE_TABLE_BROKEN
217static int vesa_bios_get_line_length(void);
218#endif
219static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
220#if 0
221static int vesa_bios_get_start(int *x, int *y);
222#endif
223static int vesa_bios_set_start(int x, int y);
224static int vesa_map_gen_mode_num(int type, int color, int mode);
225static int vesa_translate_flags(u_int16_t vflags);
226static int vesa_translate_mmodel(u_int8_t vmodel);
227static int vesa_get_bpscanline(struct vesa_mode *vmode);
228static int vesa_bios_init(void);
229static void vesa_clear_modes(video_info_t *info, int color);
230
231#if 0
232static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
233#endif
234
235/* INT 10 BIOS calls */
236static int
237int10_set_mode(int mode)
238{
239	x86regs_t regs;
240
241	x86bios_init_regs(&regs);
242	regs.R_AL = mode;
243
244	x86bios_intr(&regs, 0x10);
245
246	return (0);
247}
248
249static int
250vesa_bios_post(void)
251{
252	x86regs_t regs;
253	devclass_t dc;
254	device_t *devs;
255	device_t dev;
256	int count, i, is_pci;
257
258	if (x86bios_get_orm(vesa_bios_offs) == NULL)
259		return (1);
260
261	dev = NULL;
262	is_pci = 0;
263
264	/* Find the matching PCI video controller. */
265	dc = devclass_find("vgapci");
266	if (dc != NULL && devclass_get_devices(dc, &devs, &count) == 0) {
267		for (i = 0; i < count; i++)
268			if (device_get_flags(devs[i]) != 0 &&
269			    x86bios_match_device(vesa_bios_offs, devs[i])) {
270				dev = devs[i];
271				is_pci = 1;
272				break;
273			}
274		free(devs, M_TEMP);
275	}
276
277	/* Try VGA if a PCI device is not found. */
278	if (dev == NULL) {
279		dc = devclass_find(VGA_DRIVER_NAME);
280		if (dc != NULL)
281			dev = devclass_get_device(dc, 0);
282	}
283
284	if (bootverbose)
285		printf("%s: calling BIOS POST\n",
286		    dev == NULL ? "VESA" : device_get_nameunit(dev));
287
288	x86bios_init_regs(&regs);
289	if (is_pci) {
290		regs.R_AH = pci_get_bus(dev);
291		regs.R_AL = (pci_get_slot(dev) << 3) |
292		    (pci_get_function(dev) & 0x07);
293	}
294	regs.R_DL = 0x80;
295	x86bios_call(&regs, X86BIOS_PHYSTOSEG(vesa_bios_offs + 3),
296	    X86BIOS_PHYSTOOFF(vesa_bios_offs + 3));
297
298	if (x86bios_get_intr(0x10) == 0)
299		return (1);
300
301	return (0);
302}
303
304/* VESA BIOS calls */
305static int
306vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags)
307{
308	x86regs_t regs;
309	uint32_t offs;
310	void *buf;
311
312	buf = x86bios_alloc(&offs, sizeof(*vmode), flags);
313	if (buf == NULL)
314		return (1);
315
316	x86bios_init_regs(&regs);
317	regs.R_AX = 0x4f01;
318	regs.R_CX = mode;
319
320	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
321	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
322
323	x86bios_intr(&regs, 0x10);
324
325	if (regs.R_AX != 0x004f) {
326		x86bios_free(buf, sizeof(*vmode));
327		return (1);
328	}
329
330	bcopy(buf, vmode, sizeof(*vmode));
331	x86bios_free(buf, sizeof(*vmode));
332
333	return (0);
334}
335
336static int
337vesa_bios_set_mode(int mode)
338{
339	x86regs_t regs;
340
341	x86bios_init_regs(&regs);
342	regs.R_AX = 0x4f02;
343	regs.R_BX = mode;
344
345	x86bios_intr(&regs, 0x10);
346
347	return (regs.R_AX != 0x004f);
348}
349
350#if 0
351static int
352vesa_bios_get_dac(void)
353{
354	x86regs_t regs;
355
356	x86bios_init_regs(&regs);
357	regs.R_AX = 0x4f08;
358	regs.R_BL = 1;
359
360	x86bios_intr(&regs, 0x10);
361
362	if (regs.R_AX != 0x004f)
363		return (6);
364
365	return (regs.R_BH);
366}
367#endif
368
369static int
370vesa_bios_set_dac(int bits)
371{
372	x86regs_t regs;
373
374	x86bios_init_regs(&regs);
375	regs.R_AX = 0x4f08;
376	/* regs.R_BL = 0; */
377	regs.R_BH = bits;
378
379	x86bios_intr(&regs, 0x10);
380
381	if (regs.R_AX != 0x004f)
382		return (6);
383
384	return (regs.R_BH);
385}
386
387static int
388vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
389{
390	x86regs_t regs;
391	int i;
392
393	x86bios_init_regs(&regs);
394	regs.R_AX = 0x4f09;
395	regs.R_BL = 1;
396	regs.R_CX = colors;
397	regs.R_DX = start;
398
399	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
400	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
401
402	bits = 8 - bits;
403	mtx_lock(&vesa_lock);
404	x86bios_intr(&regs, 0x10);
405	if (regs.R_AX != 0x004f) {
406		mtx_unlock(&vesa_lock);
407		return (1);
408	}
409	for (i = 0; i < colors; ++i) {
410		palette[i * 3] = vesa_palette[i * 4 + 2] << bits;
411		palette[i * 3 + 1] = vesa_palette[i * 4 + 1] << bits;
412		palette[i * 3 + 2] = vesa_palette[i * 4] << bits;
413	}
414	mtx_unlock(&vesa_lock);
415
416	return (0);
417}
418
419static int
420vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
421			int bits)
422{
423	x86regs_t regs;
424	int i;
425
426	x86bios_init_regs(&regs);
427	regs.R_AX = 0x4f09;
428	regs.R_BL = 1;
429	regs.R_CX = colors;
430	regs.R_DX = start;
431
432	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
433	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
434
435	bits = 8 - bits;
436	mtx_lock(&vesa_lock);
437	x86bios_intr(&regs, 0x10);
438	if (regs.R_AX != 0x004f) {
439		mtx_unlock(&vesa_lock);
440		return (1);
441	}
442	for (i = 0; i < colors; ++i) {
443		r[i] = vesa_palette[i * 4 + 2] << bits;
444		g[i] = vesa_palette[i * 4 + 1] << bits;
445		b[i] = vesa_palette[i * 4] << bits;
446	}
447	mtx_unlock(&vesa_lock);
448
449	return (0);
450}
451
452static int
453vesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
454{
455	x86regs_t regs;
456	int i;
457
458	x86bios_init_regs(&regs);
459	regs.R_AX = 0x4f09;
460	/* regs.R_BL = 0; */
461	regs.R_CX = colors;
462	regs.R_DX = start;
463
464	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
465	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
466
467	bits = 8 - bits;
468	mtx_lock(&vesa_lock);
469	for (i = 0; i < colors; ++i) {
470		vesa_palette[i * 4] = palette[i * 3 + 2] >> bits;
471		vesa_palette[i * 4 + 1] = palette[i * 3 + 1] >> bits;
472		vesa_palette[i * 4 + 2] = palette[i * 3] >> bits;
473		vesa_palette[i * 4 + 3] = 0;
474	}
475	x86bios_intr(&regs, 0x10);
476	mtx_unlock(&vesa_lock);
477
478	return (regs.R_AX != 0x004f);
479}
480
481static int
482vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
483			int bits)
484{
485	x86regs_t regs;
486	int i;
487
488	x86bios_init_regs(&regs);
489	regs.R_AX = 0x4f09;
490	/* regs.R_BL = 0; */
491	regs.R_CX = colors;
492	regs.R_DX = start;
493
494	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
495	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
496
497	bits = 8 - bits;
498	mtx_lock(&vesa_lock);
499	for (i = 0; i < colors; ++i) {
500		vesa_palette[i * 4] = b[i] >> bits;
501		vesa_palette[i * 4 + 1] = g[i] >> bits;
502		vesa_palette[i * 4 + 2] = r[i] >> bits;
503		vesa_palette[i * 4 + 3] = 0;
504	}
505	x86bios_intr(&regs, 0x10);
506	mtx_unlock(&vesa_lock);
507
508	return (regs.R_AX != 0x004f);
509}
510
511static ssize_t
512vesa_bios_state_buf_size(void)
513{
514	x86regs_t regs;
515
516	x86bios_init_regs(&regs);
517	regs.R_AX = 0x4f04;
518	/* regs.R_DL = STATE_SIZE; */
519	regs.R_CX = STATE_MOST;
520
521	x86bios_intr(&regs, 0x10);
522
523	if (regs.R_AX != 0x004f)
524		return (0);
525
526	return (regs.R_BX * 64);
527}
528
529static int
530vesa_bios_save_restore(int code, void *p)
531{
532	x86regs_t regs;
533
534	if (code != STATE_SAVE && code != STATE_LOAD)
535		return (1);
536
537	x86bios_init_regs(&regs);
538	regs.R_AX = 0x4f04;
539	regs.R_DL = code;
540	regs.R_CX = STATE_MOST;
541
542	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_state_buf_offs);
543	regs.R_BX = X86BIOS_PHYSTOOFF(vesa_state_buf_offs);
544
545	mtx_lock(&vesa_lock);
546	switch (code) {
547	case STATE_SAVE:
548		x86bios_intr(&regs, 0x10);
549		bcopy(vesa_state_buf, p, vesa_state_buf_size);
550		break;
551	case STATE_LOAD:
552		bcopy(p, vesa_state_buf, vesa_state_buf_size);
553		x86bios_intr(&regs, 0x10);
554		break;
555	}
556	mtx_unlock(&vesa_lock);
557
558	return (regs.R_AX != 0x004f);
559}
560
561#ifdef MODE_TABLE_BROKEN
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
712static int
713vesa_get_bpscanline(struct vesa_mode *vmode)
714{
715	int bpsl;
716
717	if ((vmode->v_modeattr & V_MODEGRAPHICS) != 0) {
718		/* Find the minimum length. */
719		switch (vmode->v_bpp / vmode->v_planes) {
720		case 1:
721			bpsl = vmode->v_width / 8;
722			break;
723		case 2:
724			bpsl = vmode->v_width / 4;
725			break;
726		case 4:
727			bpsl = vmode->v_width / 2;
728			break;
729		default:
730			bpsl = vmode->v_width * ((vmode->v_bpp + 7) / 8);
731			bpsl /= vmode->v_planes;
732			break;
733		}
734
735		/* Use VBE 3.0 information if it looks sane. */
736		if ((vmode->v_modeattr & V_MODELFB) != 0 &&
737		    vesa_adp_info->v_version >= 0x0300 &&
738		    vmode->v_linbpscanline > bpsl)
739			return (vmode->v_linbpscanline);
740
741		/* Return the minimum if the mode table looks absurd. */
742		if (vmode->v_bpscanline < bpsl)
743			return (bpsl);
744	}
745
746	return (vmode->v_bpscanline);
747}
748
749#define	VESA_MAXSTR		256
750
751#define	VESA_STRCPY(dst, src)	do {				\
752	char *str;						\
753	int i;							\
754	dst = malloc(VESA_MAXSTR, M_DEVBUF, M_WAITOK);		\
755	str = x86bios_offset(BIOS_SADDRTOLADDR(src));		\
756	for (i = 0; i < VESA_MAXSTR - 1 && str[i] != '\0'; i++)	\
757		dst[i] = str[i];				\
758	dst[i] = '\0';						\
759} while (0)
760
761static int
762vesa_bios_init(void)
763{
764	struct vesa_mode vmode;
765	struct vesa_info *buf;
766	video_info_t *p;
767	x86regs_t regs;
768	size_t bsize;
769	size_t msize;
770	void *vmbuf;
771	uint8_t *vbios;
772	uint32_t offs;
773	uint16_t vers;
774	int is_via_cle266;
775	int modes;
776	int i;
777
778	if (vesa_init_done)
779		return (0);
780
781	has_vesa_bios = FALSE;
782	vesa_adp_info = NULL;
783	vesa_bios_offs = VESA_BIOS_OFFSET;
784	vesa_vmode_max = 0;
785	vesa_vmode[0].vi_mode = EOT;
786
787	/*
788	 * If the VBE real mode interrupt vector is not found, try BIOS POST.
789	 */
790	vesa_bios_int10 = x86bios_get_intr(0x10);
791	if (vesa_bios_int10 == 0) {
792		if (vesa_bios_post() != 0)
793			return (1);
794		vesa_bios_int10 = x86bios_get_intr(0x10);
795		if (vesa_bios_int10 == 0)
796			return (1);
797	}
798
799	/*
800	 * Shadow video ROM.
801	 */
802	offs = vesa_bios_int10;
803	if (vesa_shadow_rom) {
804		vbios = x86bios_get_orm(vesa_bios_offs);
805		if (vbios != NULL) {
806			vesa_bios_size = vbios[2] * 512;
807			offs = BIOS_SADDRTOLADDR(vesa_bios_int10);
808			if (offs > vesa_bios_offs &&
809			    offs < vesa_bios_offs + vesa_bios_size) {
810				vesa_bios = x86bios_alloc(&vesa_bios_offs,
811				    vesa_bios_size, M_WAITOK);
812				memcpy(vesa_bios, vbios, vesa_bios_size);
813				offs = offs - VESA_BIOS_OFFSET + vesa_bios_offs;
814				offs = (X86BIOS_PHYSTOSEG(offs) << 16) +
815				    X86BIOS_PHYSTOOFF(offs);
816				x86bios_set_intr(0x10, offs);
817			} else
818				offs = vesa_bios_int10;
819		}
820		if (vesa_bios == NULL)
821			printf("VESA: failed to shadow video ROM\n");
822	}
823	if (bootverbose)
824		printf("VESA: INT 0x10 vector 0x%04x:0x%04x\n",
825		    (offs >> 16) & 0xffff, offs & 0xffff);
826
827	x86bios_init_regs(&regs);
828	regs.R_AX = 0x4f00;
829
830	vmbuf = x86bios_alloc(&offs, sizeof(*buf), M_WAITOK);
831
832	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
833	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
834
835	bcopy("VBE2", vmbuf, 4);	/* try for VBE2 data */
836	x86bios_intr(&regs, 0x10);
837
838	if (regs.R_AX != 0x004f || bcmp("VESA", vmbuf, 4) != 0)
839		goto fail;
840
841	vesa_adp_info = buf = malloc(sizeof(*buf), M_DEVBUF, M_WAITOK);
842	bcopy(vmbuf, buf, sizeof(*buf));
843
844	if (bootverbose) {
845		printf("VESA: information block\n");
846		hexdump(buf, sizeof(*buf), NULL, HD_OMIT_CHARS);
847	}
848
849	vers = buf->v_version = le16toh(buf->v_version);
850	buf->v_oemstr = le32toh(buf->v_oemstr);
851	buf->v_flags = le32toh(buf->v_flags);
852	buf->v_modetable = le32toh(buf->v_modetable);
853	buf->v_memsize = le16toh(buf->v_memsize);
854	buf->v_revision = le16toh(buf->v_revision);
855	buf->v_venderstr = le32toh(buf->v_venderstr);
856	buf->v_prodstr = le32toh(buf->v_prodstr);
857	buf->v_revstr = le32toh(buf->v_revstr);
858
859	if (vers < 0x0102) {
860		printf("VESA: VBE version %d.%d is not supported; "
861		    "version 1.2 or later is required.\n",
862		    ((vers & 0xf000) >> 12) * 10 + ((vers & 0x0f00) >> 8),
863		    ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f));
864		goto fail;
865	}
866
867	VESA_STRCPY(vesa_oemstr, buf->v_oemstr);
868	if (vers >= 0x0200) {
869		VESA_STRCPY(vesa_venderstr, buf->v_venderstr);
870		VESA_STRCPY(vesa_prodstr, buf->v_prodstr);
871		VESA_STRCPY(vesa_revstr, buf->v_revstr);
872	}
873	is_via_cle266 = strncmp(vesa_oemstr, VESA_VIA_CLE266,
874	    sizeof(VESA_VIA_CLE266)) == 0;
875
876	if (buf->v_modetable == 0)
877		goto fail;
878
879	msize = (size_t)buf->v_memsize * 64 * 1024;
880
881	vesa_vmodetab = x86bios_offset(BIOS_SADDRTOLADDR(buf->v_modetable));
882
883	for (i = 0, modes = 0; (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) &&
884	    (vesa_vmodetab[i] != 0xffff); ++i) {
885		vesa_vmodetab[i] = le16toh(vesa_vmodetab[i]);
886		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_WAITOK))
887			continue;
888
889		vmode.v_modeattr = le16toh(vmode.v_modeattr);
890		vmode.v_wgran = le16toh(vmode.v_wgran);
891		vmode.v_wsize = le16toh(vmode.v_wsize);
892		vmode.v_waseg = le16toh(vmode.v_waseg);
893		vmode.v_wbseg = le16toh(vmode.v_wbseg);
894		vmode.v_posfunc = le32toh(vmode.v_posfunc);
895		vmode.v_bpscanline = le16toh(vmode.v_bpscanline);
896		vmode.v_width = le16toh(vmode.v_width);
897		vmode.v_height = le16toh(vmode.v_height);
898		vmode.v_lfb = le32toh(vmode.v_lfb);
899		vmode.v_offscreen = le32toh(vmode.v_offscreen);
900		vmode.v_offscreensize = le16toh(vmode.v_offscreensize);
901		vmode.v_linbpscanline = le16toh(vmode.v_linbpscanline);
902		vmode.v_maxpixelclock = le32toh(vmode.v_maxpixelclock);
903
904		/* reject unsupported modes */
905#if 0
906		if ((vmode.v_modeattr &
907		    (V_MODESUPP | V_MODEOPTINFO | V_MODENONVGA)) !=
908		    (V_MODESUPP | V_MODEOPTINFO))
909			continue;
910#else
911		if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
912#if VESA_DEBUG > 1
913			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
914			    " attr = %x\n",
915			    vmode.v_modeattr & V_MODEGRAPHICS ?
916			    "graphics" : "text",
917			    vmode.v_width, vmode.v_height, vmode.v_bpp,
918			    vmode.v_modeattr);
919#endif
920			continue;
921		}
922#endif
923
924		bsize = vesa_get_bpscanline(&vmode) * vmode.v_height;
925		if ((vmode.v_modeattr & V_MODEGRAPHICS) != 0)
926			bsize *= vmode.v_planes;
927
928		/* Does it have enough memory to support this mode? */
929		if (msize < bsize) {
930#if VESA_DEBUG > 1
931			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
932			    " attr = %x, not enough memory\n",
933			    vmode.v_modeattr & V_MODEGRAPHICS ?
934			    "graphics" : "text",
935			    vmode.v_width, vmode.v_height, vmode.v_bpp,
936			    vmode.v_modeattr);
937#endif
938			continue;
939		}
940
941		/* expand the array if necessary */
942		if (modes >= vesa_vmode_max) {
943			vesa_vmode_max += MODE_TABLE_DELTA;
944			p = malloc(sizeof(*vesa_vmode) * (vesa_vmode_max + 1),
945			    M_DEVBUF, M_WAITOK);
946#if VESA_DEBUG > 1
947			printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
948			    modes, vesa_vmode_max);
949#endif
950			if (modes > 0) {
951				bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
952				free(vesa_vmode, M_DEVBUF);
953			}
954			vesa_vmode = p;
955		}
956
957#if VESA_DEBUG > 1
958		printf("Found VESA %s mode: %d x %d x %d bpp\n",
959		    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
960		    vmode.v_width, vmode.v_height, vmode.v_bpp);
961#endif
962		if (is_via_cle266) {
963		    if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
964			vmode.v_width &= 0xff;
965			vmode.v_waseg = 0xb8000 >> 4;
966		    }
967		}
968
969		/* copy some fields */
970		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
971		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
972		vesa_vmode[modes].vi_width = vmode.v_width;
973		vesa_vmode[modes].vi_height = vmode.v_height;
974		vesa_vmode[modes].vi_depth = vmode.v_bpp;
975		vesa_vmode[modes].vi_planes = vmode.v_planes;
976		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
977		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
978		vesa_vmode[modes].vi_window = (vm_offset_t)vmode.v_waseg << 4;
979		/* XXX window B */
980		vesa_vmode[modes].vi_window_size = vmode.v_wsize * 1024;
981		vesa_vmode[modes].vi_window_gran = vmode.v_wgran * 1024;
982		if (vmode.v_modeattr & V_MODELFB)
983			vesa_vmode[modes].vi_buffer = vmode.v_lfb;
984		vesa_vmode[modes].vi_buffer_size = bsize;
985		vesa_vmode[modes].vi_mem_model =
986		    vesa_translate_mmodel(vmode.v_memmodel);
987		switch (vesa_vmode[modes].vi_mem_model) {
988		case V_INFO_MM_DIRECT:
989			if ((vmode.v_modeattr & V_MODELFB) != 0 &&
990			    vers >= 0x0300) {
991				vesa_vmode[modes].vi_pixel_fields[0] =
992				    vmode.v_linredfieldpos;
993				vesa_vmode[modes].vi_pixel_fields[1] =
994				    vmode.v_lingreenfieldpos;
995				vesa_vmode[modes].vi_pixel_fields[2] =
996				    vmode.v_linbluefieldpos;
997				vesa_vmode[modes].vi_pixel_fields[3] =
998				    vmode.v_linresfieldpos;
999				vesa_vmode[modes].vi_pixel_fsizes[0] =
1000				    vmode.v_linredmasksize;
1001				vesa_vmode[modes].vi_pixel_fsizes[1] =
1002				    vmode.v_lingreenmasksize;
1003				vesa_vmode[modes].vi_pixel_fsizes[2] =
1004				    vmode.v_linbluemasksize;
1005				vesa_vmode[modes].vi_pixel_fsizes[3] =
1006				    vmode.v_linresmasksize;
1007			} else {
1008				vesa_vmode[modes].vi_pixel_fields[0] =
1009				    vmode.v_redfieldpos;
1010				vesa_vmode[modes].vi_pixel_fields[1] =
1011				    vmode.v_greenfieldpos;
1012				vesa_vmode[modes].vi_pixel_fields[2] =
1013				    vmode.v_bluefieldpos;
1014				vesa_vmode[modes].vi_pixel_fields[3] =
1015				    vmode.v_resfieldpos;
1016				vesa_vmode[modes].vi_pixel_fsizes[0] =
1017				    vmode.v_redmasksize;
1018				vesa_vmode[modes].vi_pixel_fsizes[1] =
1019				    vmode.v_greenmasksize;
1020				vesa_vmode[modes].vi_pixel_fsizes[2] =
1021				    vmode.v_bluemasksize;
1022				vesa_vmode[modes].vi_pixel_fsizes[3] =
1023				    vmode.v_resmasksize;
1024			}
1025			/* FALLTHROUGH */
1026		case V_INFO_MM_PACKED:
1027			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7) / 8;
1028			break;
1029		}
1030		vesa_vmode[modes].vi_flags =
1031		    vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
1032
1033		++modes;
1034	}
1035	vesa_vmode[modes].vi_mode = EOT;
1036
1037	if (bootverbose)
1038		printf("VESA: %d mode(s) found\n", modes);
1039
1040	has_vesa_bios = (modes > 0);
1041	if (!has_vesa_bios)
1042		goto fail;
1043
1044	x86bios_free(vmbuf, sizeof(*buf));
1045
1046	vesa_state_buf_size = vesa_bios_state_buf_size();
1047	vesa_palette = x86bios_alloc(&vesa_palette_offs,
1048	    VESA_PALETTE_SIZE + vesa_state_buf_size, M_WAITOK);
1049	if (vesa_state_buf_size > 0) {
1050		vesa_state_buf = vesa_palette + VESA_PALETTE_SIZE;
1051		vesa_state_buf_offs = vesa_palette_offs + VESA_PALETTE_SIZE;
1052	}
1053
1054	return (0);
1055
1056fail:
1057	if (vesa_bios != NULL) {
1058		x86bios_set_intr(0x10, vesa_bios_int10);
1059		vesa_bios_offs = VESA_BIOS_OFFSET;
1060		x86bios_free(vesa_bios, vesa_bios_size);
1061		vesa_bios = NULL;
1062	}
1063	if (vmbuf != NULL)
1064		x86bios_free(vmbuf, sizeof(buf));
1065	if (vesa_adp_info != NULL) {
1066		free(vesa_adp_info, M_DEVBUF);
1067		vesa_adp_info = NULL;
1068	}
1069	if (vesa_oemstr != NULL) {
1070		free(vesa_oemstr, M_DEVBUF);
1071		vesa_oemstr = NULL;
1072	}
1073	if (vesa_venderstr != NULL) {
1074		free(vesa_venderstr, M_DEVBUF);
1075		vesa_venderstr = NULL;
1076	}
1077	if (vesa_prodstr != NULL) {
1078		free(vesa_prodstr, M_DEVBUF);
1079		vesa_prodstr = NULL;
1080	}
1081	if (vesa_revstr != NULL) {
1082		free(vesa_revstr, M_DEVBUF);
1083		vesa_revstr = NULL;
1084	}
1085	return (1);
1086}
1087
1088static void
1089vesa_clear_modes(video_info_t *info, int color)
1090{
1091	while (info->vi_mode != EOT) {
1092		if ((info->vi_flags & V_INFO_COLOR) != color)
1093			info->vi_mode = NA;
1094		++info;
1095	}
1096}
1097
1098/* entry points */
1099
1100static int
1101vesa_configure(int flags)
1102{
1103	video_adapter_t *adp;
1104	int adapters;
1105	int error;
1106	int i;
1107
1108	if (vesa_init_done)
1109		return (0);
1110	if (flags & VIO_PROBE_ONLY)
1111		return (0);
1112
1113	/*
1114	 * If the VESA module has already been loaded, abort loading
1115	 * the module this time.
1116	 */
1117	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
1118		if (adp->va_flags & V_ADP_VESA)
1119			return (ENXIO);
1120		if (adp->va_type == KD_VGA)
1121			break;
1122	}
1123
1124	/*
1125	 * The VGA adapter is not found.  This is because either
1126	 * 1) the VGA driver has not been initialized, or 2) the VGA card
1127	 * is not present.  If 1) is the case, we shall defer
1128	 * initialization for now and try again later.
1129	 */
1130	if (adp == NULL) {
1131		vga_sub_configure = vesa_configure;
1132		return (ENODEV);
1133	}
1134
1135	/* count number of registered adapters */
1136	for (++i; vid_get_adapter(i) != NULL; ++i)
1137		;
1138	adapters = i;
1139
1140	/* call VESA BIOS */
1141	vesa_adp = adp;
1142	if (vesa_bios_init()) {
1143		vesa_adp = NULL;
1144		return (ENXIO);
1145	}
1146	vesa_adp->va_flags |= V_ADP_VESA;
1147
1148	/* remove conflicting modes if we have more than one adapter */
1149	if (adapters > 1) {
1150		vesa_clear_modes(vesa_vmode,
1151				 (vesa_adp->va_flags & V_ADP_COLOR) ?
1152				     V_INFO_COLOR : 0);
1153	}
1154
1155	if ((error = vesa_load_ioctl()) == 0) {
1156		prevvidsw = vidsw[vesa_adp->va_index];
1157		vidsw[vesa_adp->va_index] = &vesavidsw;
1158		vesa_init_done = TRUE;
1159	} else {
1160		vesa_adp = NULL;
1161		return (error);
1162	}
1163
1164	return (0);
1165}
1166
1167#if 0
1168static int
1169vesa_nop(void)
1170{
1171
1172	return (0);
1173}
1174#endif
1175
1176static int
1177vesa_error(void)
1178{
1179
1180	return (1);
1181}
1182
1183static int
1184vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1185{
1186
1187	return ((*prevvidsw->probe)(unit, adpp, arg, flags));
1188}
1189
1190static int
1191vesa_init(int unit, video_adapter_t *adp, int flags)
1192{
1193
1194	return ((*prevvidsw->init)(unit, adp, flags));
1195}
1196
1197static int
1198vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1199{
1200	int i;
1201
1202	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
1203		return (0);
1204
1205	if (adp != vesa_adp)
1206		return (1);
1207
1208	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
1209				     vesa_adp->va_flags & V_ADP_COLOR, mode);
1210	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1211		if (vesa_vmode[i].vi_mode == NA)
1212			continue;
1213		if (vesa_vmode[i].vi_mode == mode) {
1214			*info = vesa_vmode[i];
1215			return (0);
1216		}
1217	}
1218	return (1);
1219}
1220
1221static int
1222vesa_query_mode(video_adapter_t *adp, video_info_t *info)
1223{
1224	int i;
1225
1226	if ((*prevvidsw->query_mode)(adp, info) == 0)
1227		return (0);
1228	if (adp != vesa_adp)
1229		return (ENODEV);
1230
1231	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1232		if ((info->vi_width != 0)
1233		    && (info->vi_width != vesa_vmode[i].vi_width))
1234			continue;
1235		if ((info->vi_height != 0)
1236		    && (info->vi_height != vesa_vmode[i].vi_height))
1237			continue;
1238		if ((info->vi_cwidth != 0)
1239		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
1240			continue;
1241		if ((info->vi_cheight != 0)
1242		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
1243			continue;
1244		if ((info->vi_depth != 0)
1245		    && (info->vi_depth != vesa_vmode[i].vi_depth))
1246			continue;
1247		if ((info->vi_planes != 0)
1248		    && (info->vi_planes != vesa_vmode[i].vi_planes))
1249			continue;
1250		/* pixel format, memory model */
1251		if ((info->vi_flags != 0)
1252		    && (info->vi_flags != vesa_vmode[i].vi_flags))
1253			continue;
1254		*info = vesa_vmode[i];
1255		return (0);
1256	}
1257	return (ENODEV);
1258}
1259
1260static int
1261vesa_set_mode(video_adapter_t *adp, int mode)
1262{
1263	video_info_t info;
1264
1265	if (adp != vesa_adp)
1266		return ((*prevvidsw->set_mode)(adp, mode));
1267
1268	mode = vesa_map_gen_mode_num(adp->va_type,
1269				     adp->va_flags & V_ADP_COLOR, mode);
1270#if VESA_DEBUG > 0
1271	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1272		adp->va_mode, adp->va_mode, mode, mode);
1273#endif
1274	/*
1275	 * If the current mode is a VESA mode and the new mode is not,
1276	 * restore the state of the adapter first by setting one of the
1277	 * standard VGA mode, so that non-standard, extended SVGA registers
1278	 * are set to the state compatible with the standard VGA modes.
1279	 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1280	 * the new mode correctly.
1281	 */
1282	if (VESA_MODE(adp->va_mode)) {
1283		if (!VESA_MODE(mode) &&
1284		    (*prevvidsw->get_info)(adp, mode, &info) == 0) {
1285			if ((adp->va_flags & V_ADP_DAC8) != 0) {
1286				vesa_bios_set_dac(6);
1287				adp->va_flags &= ~V_ADP_DAC8;
1288			}
1289			int10_set_mode(adp->va_initial_bios_mode);
1290			if (adp->va_info.vi_flags & V_INFO_LINEAR)
1291				pmap_unmapdev(adp->va_buffer,
1292				    vesa_adp_info->v_memsize * 64 * 1024);
1293			/*
1294			 * Once (*prevvidsw->get_info)() succeeded,
1295			 * (*prevvidsw->set_mode)() below won't fail...
1296			 */
1297		}
1298	}
1299
1300	/* we may not need to handle this mode after all... */
1301	if (!VESA_MODE(mode) && (*prevvidsw->set_mode)(adp, mode) == 0)
1302		return (0);
1303
1304	/* is the new mode supported? */
1305	if (vesa_get_info(adp, mode, &info))
1306		return (1);
1307	/* assert(VESA_MODE(mode)); */
1308
1309#if VESA_DEBUG > 0
1310	printf("VESA: about to set a VESA mode...\n");
1311#endif
1312	/* don't use the linear frame buffer for text modes. XXX */
1313	if (!(info.vi_flags & V_INFO_GRAPHICS))
1314		info.vi_flags &= ~V_INFO_LINEAR;
1315
1316	if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
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;
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)
1455		return ((*prevvidsw->save_state)(adp, p, size));
1456
1457	if (vesa_state_buf_size == 0)
1458		return (1);
1459	if (size == 0)
1460		return (offsetof(adp_state_t, regs) + vesa_state_buf_size);
1461	if (size < (offsetof(adp_state_t, regs) + vesa_state_buf_size))
1462		return (1);
1463
1464	((adp_state_t *)p)->sig = V_STATE_SIG;
1465	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1466	return (vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs));
1467}
1468
1469static int
1470vesa_load_state(video_adapter_t *adp, void *p)
1471{
1472
1473	if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1474		return ((*prevvidsw->load_state)(adp, p));
1475
1476	if (vesa_state_buf_size == 0)
1477		return (1);
1478
1479	/* Try BIOS POST to restore a sane state. */
1480	(void)vesa_bios_post();
1481	(void)int10_set_mode(adp->va_initial_bios_mode);
1482	(void)vesa_set_mode(adp, adp->va_mode);
1483
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