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