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