vesa.c revision 232061
1254721Semaste/*-
2254721Semaste * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3353358Sdim * Copyright (c) 2009-2010 Jung-uk Kim <jkim@FreeBSD.org>
4353358Sdim * All rights reserved.
5353358Sdim *
6254721Semaste * Redistribution and use in source and binary forms, with or without
7254721Semaste * modification, are permitted provided that the following conditions
8254721Semaste * are met:
9314564Sdim * 1. Redistributions of source code must retain the above copyright
10321369Sdim *    notice, this list of conditions and the following disclaimer as
11254721Semaste *    the first lines of this file unmodified.
12254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
13296417Sdim *    notice, this list of conditions and the following disclaimer in the
14254721Semaste *    documentation and/or other materials provided with the distribution.
15254721Semaste *
16254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17254721Semaste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18254721Semaste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19254721Semaste * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20254721Semaste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21254721Semaste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22321369Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23344779Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24254721Semaste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25353358Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26353358Sdim */
27254721Semaste
28254721Semaste#include <sys/cdefs.h>
29314564Sdim__FBSDID("$FreeBSD: head/sys/dev/fb/vesa.c 232061 2012-02-23 18:59:32Z jkim $");
30314564Sdim
31314564Sdim#include "opt_vga.h"
32309124Sdim#include "opt_vesa.h"
33314564Sdim
34309124Sdim#ifndef VGA_NO_MODE_CHANGE
35314564Sdim
36314564Sdim#include <sys/param.h>
37309124Sdim#include <sys/bus.h>
38314564Sdim#include <sys/systm.h>
39314564Sdim#include <sys/kernel.h>
40309124Sdim#include <sys/lock.h>
41314564Sdim#include <sys/module.h>
42309124Sdim#include <sys/malloc.h>
43314564Sdim#include <sys/mutex.h>
44309124Sdim#include <sys/fbio.h>
45314564Sdim#include <sys/sysctl.h>
46254721Semaste
47254721Semaste#include <vm/vm.h>
48314564Sdim#include <vm/vm_extern.h>
49254721Semaste#include <vm/vm_kern.h>
50314564Sdim#include <vm/vm_param.h>
51314564Sdim#include <vm/pmap.h>
52314564Sdim
53314564Sdim#include <machine/pc/bios.h>
54314564Sdim#include <dev/fb/vesa.h>
55314564Sdim
56314564Sdim#include <dev/fb/fbreg.h>
57314564Sdim#include <dev/fb/vgareg.h>
58314564Sdim
59309124Sdim#include <dev/pci/pcivar.h>
60321369Sdim
61314564Sdim#include <isa/isareg.h>
62254721Semaste
63341825Sdim#include <compat/x86bios/x86bios.h>
64341825Sdim
65309124Sdim#define	VESA_BIOS_OFFSET	0xc0000
66321369Sdim#define	VESA_PALETTE_SIZE	(256 * 4)
67314564Sdim#define	VESA_VIA_CLE266		"VIA CLE266\r\n"
68309124Sdim
69314564Sdim#ifndef VESA_DEBUG
70314564Sdim#define VESA_DEBUG	0
71314564Sdim#endif
72314564Sdim
73309124Sdim/* VESA video adapter state buffer stub */
74314564Sdimstruct adp_state {
75314564Sdim	int		sig;
76314564Sdim#define V_STATE_SIG	0x61736576
77314564Sdim	u_char		regs[1];
78314564Sdim};
79314564Sdimtypedef struct adp_state adp_state_t;
80314564Sdim
81309124Sdimstatic struct mtx vesa_lock;
82360784Sdim
83360784Sdimstatic void *vesa_state_buf = NULL;
84309124Sdimstatic uint32_t vesa_state_buf_offs = 0;
85314564Sdimstatic ssize_t vesa_state_buf_size = 0;
86314564Sdim
87309124Sdimstatic u_char *vesa_palette = NULL;
88314564Sdimstatic uint32_t vesa_palette_offs = 0;
89314564Sdim
90314564Sdimstatic void *vesa_bios = NULL;
91314564Sdimstatic uint32_t vesa_bios_offs = VESA_BIOS_OFFSET;
92314564Sdimstatic uint32_t vesa_bios_int10 = 0;
93309124Sdimstatic size_t vesa_bios_size = 0;
94314564Sdim
95309124Sdim/* VESA video adapter */
96314564Sdimstatic video_adapter_t *vesa_adp = NULL;
97314564Sdim
98321369Sdimstatic SYSCTL_NODE(_debug, OID_AUTO, vesa, CTLFLAG_RD, NULL, "VESA debugging");
99314564Sdimstatic int vesa_shadow_rom = 0;
100314564SdimTUNABLE_INT("debug.vesa.shadow_rom", &vesa_shadow_rom);
101314564SdimSYSCTL_INT(_debug_vesa, OID_AUTO, shadow_rom, CTLFLAG_RDTUN, &vesa_shadow_rom,
102314564Sdim    0, "Enable video BIOS shadow");
103309124Sdim
104314564Sdim/* VESA functions */
105309124Sdim#if 0
106321369Sdimstatic int			vesa_nop(void);
107309124Sdim#endif
108314564Sdimstatic int			vesa_error(void);
109314564Sdimstatic vi_probe_t		vesa_probe;
110309124Sdimstatic vi_init_t		vesa_init;
111314564Sdimstatic vi_get_info_t		vesa_get_info;
112314564Sdimstatic vi_query_mode_t		vesa_query_mode;
113314564Sdimstatic vi_set_mode_t		vesa_set_mode;
114314564Sdimstatic vi_save_font_t		vesa_save_font;
115314564Sdimstatic vi_load_font_t		vesa_load_font;
116314564Sdimstatic vi_show_font_t		vesa_show_font;
117254721Semastestatic vi_save_palette_t	vesa_save_palette;
118314564Sdimstatic vi_load_palette_t	vesa_load_palette;
119309124Sdimstatic vi_set_border_t		vesa_set_border;
120321369Sdimstatic vi_save_state_t		vesa_save_state;
121321369Sdimstatic vi_load_state_t		vesa_load_state;
122309124Sdimstatic vi_set_win_org_t		vesa_set_origin;
123314564Sdimstatic vi_read_hw_cursor_t	vesa_read_hw_cursor;
124314564Sdimstatic vi_set_hw_cursor_t	vesa_set_hw_cursor;
125314564Sdimstatic vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
126314564Sdimstatic vi_blank_display_t	vesa_blank_display;
127309124Sdimstatic vi_mmap_t		vesa_mmap;
128314564Sdimstatic vi_ioctl_t		vesa_ioctl;
129309124Sdimstatic vi_clear_t		vesa_clear;
130314564Sdimstatic vi_fill_rect_t		vesa_fill_rect;
131314564Sdimstatic vi_bitblt_t		vesa_bitblt;
132314564Sdimstatic vi_diag_t		vesa_diag;
133314564Sdimstatic int			vesa_bios_info(int level);
134314564Sdim
135254721Semastestatic video_switch_t vesavidsw = {
136314564Sdim	vesa_probe,
137309124Sdim	vesa_init,
138314564Sdim	vesa_get_info,
139321369Sdim	vesa_query_mode,
140314564Sdim	vesa_set_mode,
141254721Semaste	vesa_save_font,
142314564Sdim	vesa_load_font,
143309124Sdim	vesa_show_font,
144314564Sdim	vesa_save_palette,
145360784Sdim	vesa_load_palette,
146360784Sdim	vesa_set_border,
147360784Sdim	vesa_save_state,
148360784Sdim	vesa_load_state,
149360784Sdim	vesa_set_origin,
150360784Sdim	vesa_read_hw_cursor,
151314564Sdim	vesa_set_hw_cursor,
152309124Sdim	vesa_set_hw_cursor_shape,
153314564Sdim	vesa_blank_display,
154314564Sdim	vesa_mmap,
155314564Sdim	vesa_ioctl,
156314564Sdim	vesa_clear,
157314564Sdim	vesa_fill_rect,
158309124Sdim	vesa_bitblt,
159314564Sdim	vesa_error,
160314564Sdim	vesa_error,
161314564Sdim	vesa_diag,
162309124Sdim};
163314564Sdim
164314564Sdimstatic video_switch_t *prevvidsw;
165314564Sdim
166314564Sdim/* VESA BIOS video modes */
167314564Sdim#define VESA_MAXMODES	64
168321369Sdim#define EOT		(-1)
169309124Sdim#define NA		(-2)
170314564Sdim
171314564Sdim#define MODE_TABLE_DELTA 8
172314564Sdim
173314564Sdimstatic int vesa_vmode_max = 0;
174309124Sdimstatic video_info_t vesa_vmode_empty = { EOT };
175314564Sdimstatic video_info_t *vesa_vmode = &vesa_vmode_empty;
176314564Sdim
177314564Sdimstatic int vesa_init_done = FALSE;
178314564Sdimstatic int has_vesa_bios = FALSE;
179314564Sdimstatic struct vesa_info *vesa_adp_info = NULL;
180314564Sdimstatic u_int16_t *vesa_vmodetab = NULL;
181314564Sdimstatic char *vesa_oemstr = NULL;
182314564Sdimstatic char *vesa_venderstr = NULL;
183314564Sdimstatic char *vesa_prodstr = NULL;
184314564Sdimstatic char *vesa_revstr = NULL;
185314564Sdim
186254721Semaste/* local macros and functions */
187314564Sdim#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
188309124Sdim
189314564Sdimstatic int int10_set_mode(int mode);
190314564Sdimstatic int vesa_bios_post(void);
191321369Sdimstatic int vesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags);
192314564Sdimstatic int vesa_bios_set_mode(int mode);
193309124Sdim#if 0
194314564Sdimstatic int vesa_bios_get_dac(void);
195254721Semaste#endif
196314564Sdimstatic int vesa_bios_set_dac(int bits);
197360784Sdimstatic int vesa_bios_save_palette(int start, int colors, u_char *palette,
198360784Sdim				  int bits);
199360784Sdimstatic int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
200360784Sdim				   u_char *b, int bits);
201360784Sdimstatic int vesa_bios_load_palette(int start, int colors, u_char *palette,
202360784Sdim				  int bits);
203314564Sdimstatic int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
204309124Sdim				   u_char *b, int bits);
205314564Sdim#define STATE_SIZE	0
206314564Sdim#define STATE_SAVE	1
207314564Sdim#define STATE_LOAD	2
208296417Sdim#define STATE_HW	(1<<0)
209314564Sdim#define STATE_DATA	(1<<1)
210314564Sdim#define STATE_DAC	(1<<2)
211314564Sdim#define STATE_REG	(1<<3)
212314564Sdim#define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
213314564Sdim#define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
214314564Sdimstatic ssize_t vesa_bios_state_buf_size(void);
215314564Sdimstatic int vesa_bios_save_restore(int code, void *p);
216314564Sdim#ifdef MODE_TABLE_BROKEN
217341825Sdimstatic int vesa_bios_get_line_length(void);
218341825Sdim#endif
219309124Sdimstatic int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
220314564Sdim#if 0
221321369Sdimstatic int vesa_bios_get_start(int *x, int *y);
222309124Sdim#endif
223314564Sdimstatic int vesa_bios_set_start(int x, int y);
224309124Sdimstatic int vesa_map_gen_mode_num(int type, int color, int mode);
225314564Sdimstatic int vesa_translate_flags(u_int16_t vflags);
226314564Sdimstatic int vesa_translate_mmodel(u_int8_t vmodel);
227314564Sdimstatic int vesa_get_bpscanline(struct vesa_mode *vmode);
228314564Sdimstatic int vesa_bios_init(void);
229314564Sdimstatic void vesa_clear_modes(video_info_t *info, int color);
230314564Sdim
231314564Sdim#if 0
232309124Sdimstatic int vesa_get_origin(video_adapter_t *adp, off_t *offset);
233314564Sdim#endif
234314564Sdim
235314564Sdim/* INT 10 BIOS calls */
236314564Sdimstatic int
237314564Sdimint10_set_mode(int mode)
238309124Sdim{
239314564Sdim	x86regs_t regs;
240314564Sdim
241314564Sdim	x86bios_init_regs(&regs);
242314564Sdim	regs.R_AL = mode;
243341825Sdim
244341825Sdim	x86bios_intr(&regs, 0x10);
245314564Sdim
246314564Sdim	return (0);
247314564Sdim}
248314564Sdim
249314564Sdimstatic int
250314564Sdimvesa_bios_post(void)
251314564Sdim{
252314564Sdim	x86regs_t regs;
253314564Sdim	devclass_t dc;
254314564Sdim	device_t *devs;
255309124Sdim	device_t dev;
256314564Sdim	int count, i, is_pci;
257314564Sdim
258314564Sdim	if (x86bios_get_orm(vesa_bios_offs) == NULL)
259309124Sdim		return (1);
260314564Sdim
261314564Sdim	dev = NULL;
262314564Sdim	is_pci = 0;
263314564Sdim
264314564Sdim	/* Find the matching PCI video controller. */
265314564Sdim	dc = devclass_find("vgapci");
266309124Sdim	if (dc != NULL && devclass_get_devices(dc, &devs, &count) == 0) {
267314564Sdim		for (i = 0; i < count; i++)
268314564Sdim			if (device_get_flags(devs[i]) != 0 &&
269314564Sdim			    x86bios_match_device(vesa_bios_offs, devs[i])) {
270314564Sdim				dev = devs[i];
271314564Sdim				is_pci = 1;
272314564Sdim				break;
273314564Sdim			}
274309124Sdim		free(devs, M_TEMP);
275314564Sdim	}
276314564Sdim
277314564Sdim	/* Try VGA if a PCI device is not found. */
278314564Sdim	if (dev == NULL) {
279360784Sdim		dc = devclass_find(VGA_DRIVER_NAME);
280360784Sdim		if (dc != NULL)
281360784Sdim			dev = devclass_get_device(dc, 0);
282360784Sdim	}
283309124Sdim
284314564Sdim	if (bootverbose)
285309124Sdim		printf("%s: calling BIOS POST\n",
286314564Sdim		    dev == NULL ? "VESA" : device_get_nameunit(dev));
287309124Sdim
288321369Sdim	x86bios_init_regs(&regs);
289309124Sdim	if (is_pci) {
290314564Sdim		regs.R_AH = pci_get_bus(dev);
291314564Sdim		regs.R_AL = (pci_get_slot(dev) << 3) |
292309124Sdim		    (pci_get_function(dev) & 0x07);
293314564Sdim	}
294314564Sdim	regs.R_DL = 0x80;
295314564Sdim	x86bios_call(&regs, X86BIOS_PHYSTOSEG(vesa_bios_offs + 3),
296314564Sdim	    X86BIOS_PHYSTOOFF(vesa_bios_offs + 3));
297314564Sdim
298314564Sdim	if (x86bios_get_intr(0x10) == 0)
299254721Semaste		return (1);
300309124Sdim
301314564Sdim	return (0);
302314564Sdim}
303314564Sdim
304314564Sdim/* VESA BIOS calls */
305314564Sdimstatic int
306314564Sdimvesa_bios_get_mode(int mode, struct vesa_mode *vmode, int flags)
307314564Sdim{
308314564Sdim	x86regs_t regs;
309314564Sdim	uint32_t offs;
310309124Sdim	void *buf;
311314564Sdim
312314564Sdim	buf = x86bios_alloc(&offs, sizeof(*vmode), flags);
313314564Sdim	if (buf == NULL)
314314564Sdim		return (1);
315314564Sdim
316254721Semaste	x86bios_init_regs(&regs);
317314564Sdim	regs.R_AX = 0x4f01;
318314564Sdim	regs.R_CX = mode;
319314564Sdim
320314564Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
321314564Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
322314564Sdim
323314564Sdim	x86bios_intr(&regs, 0x10);
324314564Sdim
325314564Sdim	if (regs.R_AX != 0x004f) {
326314564Sdim		x86bios_free(buf, sizeof(*vmode));
327314564Sdim		return (1);
328314564Sdim	}
329314564Sdim
330254721Semaste	bcopy(buf, vmode, sizeof(*vmode));
331314564Sdim	x86bios_free(buf, sizeof(*vmode));
332309124Sdim
333314564Sdim	return (0);
334314564Sdim}
335314564Sdim
336309124Sdimstatic int
337321369Sdimvesa_bios_set_mode(int mode)
338309124Sdim{
339314564Sdim	x86regs_t regs;
340254721Semaste
341314564Sdim	x86bios_init_regs(&regs);
342314564Sdim	regs.R_AX = 0x4f02;
343314564Sdim	regs.R_BX = mode;
344309124Sdim
345314564Sdim	x86bios_intr(&regs, 0x10);
346314564Sdim
347309124Sdim	return (regs.R_AX != 0x004f);
348314564Sdim}
349309124Sdim
350314564Sdim#if 0
351309124Sdimstatic int
352314564Sdimvesa_bios_get_dac(void)
353314564Sdim{
354314564Sdim	x86regs_t regs;
355321369Sdim
356321369Sdim	x86bios_init_regs(&regs);
357309124Sdim	regs.R_AX = 0x4f08;
358314564Sdim	regs.R_BL = 1;
359314564Sdim
360314564Sdim	x86bios_intr(&regs, 0x10);
361309124Sdim
362314564Sdim	if (regs.R_AX != 0x004f)
363314564Sdim		return (6);
364309124Sdim
365314564Sdim	return (regs.R_BH);
366309124Sdim}
367314564Sdim#endif
368309124Sdim
369314564Sdimstatic int
370314564Sdimvesa_bios_set_dac(int bits)
371314564Sdim{
372314564Sdim	x86regs_t regs;
373309124Sdim
374314564Sdim	x86bios_init_regs(&regs);
375314564Sdim	regs.R_AX = 0x4f08;
376309124Sdim	/* regs.R_BL = 0; */
377314564Sdim	regs.R_BH = bits;
378314564Sdim
379314564Sdim	x86bios_intr(&regs, 0x10);
380321369Sdim
381321369Sdim	if (regs.R_AX != 0x004f)
382309124Sdim		return (6);
383314564Sdim
384254721Semaste	return (regs.R_BH);
385314564Sdim}
386254721Semaste
387309124Sdimstatic int
388314564Sdimvesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
389314564Sdim{
390296417Sdim	x86regs_t regs;
391314564Sdim	int i;
392314564Sdim
393254721Semaste	x86bios_init_regs(&regs);
394314564Sdim	regs.R_AX = 0x4f09;
395314564Sdim	regs.R_BL = 1;
396254721Semaste	regs.R_CX = colors;
397254721Semaste	regs.R_DX = start;
398314564Sdim
399314564Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
400321369Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
401314564Sdim
402314564Sdim	bits = 8 - bits;
403314564Sdim	mtx_lock(&vesa_lock);
404314564Sdim	x86bios_intr(&regs, 0x10);
405314564Sdim	if (regs.R_AX != 0x004f) {
406254721Semaste		mtx_unlock(&vesa_lock);
407254721Semaste		return (1);
408314564Sdim	}
409254721Semaste	for (i = 0; i < colors; ++i) {
410314564Sdim		palette[i * 3] = vesa_palette[i * 4 + 2] << bits;
411314564Sdim		palette[i * 3 + 1] = vesa_palette[i * 4 + 1] << bits;
412254721Semaste		palette[i * 3 + 2] = vesa_palette[i * 4] << bits;
413314564Sdim	}
414314564Sdim	mtx_unlock(&vesa_lock);
415314564Sdim
416314564Sdim	return (0);
417314564Sdim}
418314564Sdim
419314564Sdimstatic int
420314564Sdimvesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
421314564Sdim			int bits)
422314564Sdim{
423321369Sdim	x86regs_t regs;
424314564Sdim	int i;
425314564Sdim
426314564Sdim	x86bios_init_regs(&regs);
427314564Sdim	regs.R_AX = 0x4f09;
428360784Sdim	regs.R_BL = 1;
429360784Sdim	regs.R_CX = colors;
430360784Sdim	regs.R_DX = start;
431360784Sdim
432254721Semaste	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
433309124Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
434314564Sdim
435309124Sdim	bits = 8 - bits;
436314564Sdim	mtx_lock(&vesa_lock);
437314564Sdim	x86bios_intr(&regs, 0x10);
438309124Sdim	if (regs.R_AX != 0x004f) {
439314564Sdim		mtx_unlock(&vesa_lock);
440314564Sdim		return (1);
441309124Sdim	}
442314564Sdim	for (i = 0; i < colors; ++i) {
443314564Sdim		r[i] = vesa_palette[i * 4 + 2] << bits;
444314564Sdim		g[i] = vesa_palette[i * 4 + 1] << bits;
445314564Sdim		b[i] = vesa_palette[i * 4] << bits;
446314564Sdim	}
447314564Sdim	mtx_unlock(&vesa_lock);
448309124Sdim
449321369Sdim	return (0);
450309124Sdim}
451314564Sdim
452314564Sdimstatic int
453314564Sdimvesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
454314564Sdim{
455314564Sdim	x86regs_t regs;
456314564Sdim	int i;
457309124Sdim
458314564Sdim	x86bios_init_regs(&regs);
459314564Sdim	regs.R_AX = 0x4f09;
460321369Sdim	/* regs.R_BL = 0; */
461314564Sdim	regs.R_CX = colors;
462309124Sdim	regs.R_DX = start;
463314564Sdim
464314564Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
465314564Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
466314564Sdim
467314564Sdim	bits = 8 - bits;
468314564Sdim	mtx_lock(&vesa_lock);
469309124Sdim	for (i = 0; i < colors; ++i) {
470314564Sdim		vesa_palette[i * 4] = palette[i * 3 + 2] >> bits;
471314564Sdim		vesa_palette[i * 4 + 1] = palette[i * 3 + 1] >> bits;
472309124Sdim		vesa_palette[i * 4 + 2] = palette[i * 3] >> bits;
473321369Sdim		vesa_palette[i * 4 + 3] = 0;
474314564Sdim	}
475309124Sdim	x86bios_intr(&regs, 0x10);
476314564Sdim	mtx_unlock(&vesa_lock);
477314564Sdim
478314564Sdim	return (regs.R_AX != 0x004f);
479314564Sdim}
480314564Sdim
481314564Sdimstatic int
482314564Sdimvesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
483314564Sdim			int bits)
484314564Sdim{
485314564Sdim	x86regs_t regs;
486314564Sdim	int i;
487314564Sdim
488314564Sdim	x86bios_init_regs(&regs);
489321369Sdim	regs.R_AX = 0x4f09;
490314564Sdim	/* regs.R_BL = 0; */
491309124Sdim	regs.R_CX = colors;
492314564Sdim	regs.R_DX = start;
493314564Sdim
494314564Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_palette_offs);
495314564Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(vesa_palette_offs);
496314564Sdim
497314564Sdim	bits = 8 - bits;
498314564Sdim	mtx_lock(&vesa_lock);
499314564Sdim	for (i = 0; i < colors; ++i) {
500321369Sdim		vesa_palette[i * 4] = b[i] >> bits;
501314564Sdim		vesa_palette[i * 4 + 1] = g[i] >> bits;
502314564Sdim		vesa_palette[i * 4 + 2] = r[i] >> bits;
503314564Sdim		vesa_palette[i * 4 + 3] = 0;
504314564Sdim	}
505314564Sdim	x86bios_intr(&regs, 0x10);
506314564Sdim	mtx_unlock(&vesa_lock);
507314564Sdim
508309124Sdim	return (regs.R_AX != 0x004f);
509314564Sdim}
510314564Sdim
511314564Sdimstatic ssize_t
512314564Sdimvesa_bios_state_buf_size(void)
513314564Sdim{
514254721Semaste	x86regs_t regs;
515309124Sdim
516314564Sdim	x86bios_init_regs(&regs);
517314564Sdim	regs.R_AX = 0x4f04;
518344779Sdim	/* regs.R_DL = STATE_SIZE; */
519314564Sdim	regs.R_CX = STATE_MOST;
520314564Sdim
521314564Sdim	x86bios_intr(&regs, 0x10);
522314564Sdim
523314564Sdim	if (regs.R_AX != 0x004f)
524314564Sdim		return (0);
525314564Sdim
526314564Sdim	return (regs.R_BX * 64);
527353358Sdim}
528353358Sdim
529314564Sdimstatic int
530314564Sdimvesa_bios_save_restore(int code, void *p)
531314564Sdim{
532309124Sdim	x86regs_t regs;
533360784Sdim
534360784Sdim	if (code != STATE_SAVE && code != STATE_LOAD)
535360784Sdim		return (1);
536360784Sdim
537360784Sdim	x86bios_init_regs(&regs);
538360784Sdim	regs.R_AX = 0x4f04;
539360784Sdim	regs.R_DL = code;
540309124Sdim	regs.R_CX = STATE_MOST;
541360784Sdim
542309124Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(vesa_state_buf_offs);
543321369Sdim	regs.R_BX = X86BIOS_PHYSTOOFF(vesa_state_buf_offs);
544314564Sdim
545309124Sdim	mtx_lock(&vesa_lock);
546314564Sdim	switch (code) {
547314564Sdim	case STATE_SAVE:
548314564Sdim		x86bios_intr(&regs, 0x10);
549314564Sdim		bcopy(vesa_state_buf, p, vesa_state_buf_size);
550309124Sdim		break;
551314564Sdim	case STATE_LOAD:
552296417Sdim		bcopy(p, vesa_state_buf, vesa_state_buf_size);
553353358Sdim		x86bios_intr(&regs, 0x10);
554353358Sdim		break;
555309124Sdim	}
556314564Sdim	mtx_unlock(&vesa_lock);
557314564Sdim
558314564Sdim	return (regs.R_AX != 0x004f);
559314564Sdim}
560314564Sdim
561314564Sdim#ifdef MODE_TABLE_BROKEN
562309124Sdimstatic int
563321369Sdimvesa_bios_get_line_length(void)
564309124Sdim{
565314564Sdim	x86regs_t regs;
566314564Sdim
567309124Sdim	x86bios_init_regs(&regs);
568314564Sdim	regs.R_AX = 0x4f06;
569314564Sdim	regs.R_BL = 1;
570314564Sdim
571314564Sdim	x86bios_intr(&regs, 0x10);
572314564Sdim
573314564Sdim	if (regs.R_AX != 0x004f)
574309124Sdim		return (-1);
575321369Sdim
576309124Sdim	return (regs.R_BX);
577314564Sdim}
578314564Sdim#endif
579309124Sdim
580314564Sdimstatic int
581314564Sdimvesa_bios_set_line_length(int pixel, int *bytes, int *lines)
582314564Sdim{
583314564Sdim	x86regs_t regs;
584314564Sdim
585254721Semaste	x86bios_init_regs(&regs);
586314564Sdim	regs.R_AX = 0x4f06;
587254721Semaste	/* regs.R_BL = 0; */
588314564Sdim	regs.R_CX = pixel;
589309124Sdim
590314564Sdim	x86bios_intr(&regs, 0x10);
591314564Sdim
592321369Sdim#if VESA_DEBUG > 1
593314564Sdim	printf("bx:%d, cx:%d, dx:%d\n", regs.R_BX, regs.R_CX, regs.R_DX);
594254721Semaste#endif
595314564Sdim	if (regs.R_AX != 0x004f)
596314564Sdim		return (-1);
597360784Sdim
598360784Sdim	if (bytes != NULL)
599360784Sdim		*bytes = regs.R_BX;
600360784Sdim	if (lines != NULL)
601314564Sdim		*lines = regs.R_DX;
602309124Sdim
603314564Sdim	return (0);
604314564Sdim}
605309124Sdim
606314564Sdim#if 0
607314564Sdimstatic int
608309124Sdimvesa_bios_get_start(int *x, int *y)
609314564Sdim{
610314564Sdim	x86regs_t regs;
611309124Sdim
612314564Sdim	x86bios_init_regs(&regs);
613314564Sdim	regs.R_AX = 0x4f07;
614314564Sdim	regs.R_BL = 1;
615314564Sdim
616314564Sdim	x86bios_intr(&regs, 0x10);
617314564Sdim
618309124Sdim	if (regs.R_AX != 0x004f)
619314564Sdim		return (-1);
620309124Sdim
621321369Sdim	*x = regs.R_CX;
622309124Sdim	*y = regs.R_DX;
623314564Sdim
624314564Sdim	return (0);
625309124Sdim}
626314564Sdim#endif
627314564Sdim
628314564Sdimstatic int
629314564Sdimvesa_bios_set_start(int x, int y)
630314564Sdim{
631309124Sdim	x86regs_t regs;
632314564Sdim
633309124Sdim	x86bios_init_regs(&regs);
634314564Sdim	regs.R_AX = 0x4f07;
635314564Sdim	regs.R_BL = 0x80;
636314564Sdim	regs.R_CX = x;
637314564Sdim	regs.R_DX = y;
638314564Sdim
639314564Sdim	x86bios_intr(&regs, 0x10);
640314564Sdim
641309124Sdim	return (regs.R_AX != 0x004f);
642321369Sdim}
643309124Sdim
644314564Sdim/* map a generic video mode to a known mode */
645314564Sdimstatic int
646309124Sdimvesa_map_gen_mode_num(int type, int color, int mode)
647314564Sdim{
648314564Sdim    static struct {
649314564Sdim	int from;
650314564Sdim	int to;
651314564Sdim    } mode_map[] = {
652314564Sdim	{ M_TEXT_132x25, M_VESA_C132x25 },
653314564Sdim	{ M_TEXT_132x43, M_VESA_C132x43 },
654309124Sdim	{ M_TEXT_132x50, M_VESA_C132x50 },
655321369Sdim	{ M_TEXT_132x60, M_VESA_C132x60 },
656309124Sdim    };
657314564Sdim    int i;
658309124Sdim
659314564Sdim    for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
660314564Sdim        if (mode_map[i].from == mode)
661314564Sdim            return (mode_map[i].to);
662314564Sdim    }
663314564Sdim    return (mode);
664314564Sdim}
665309124Sdim
666314564Sdimstatic int
667314564Sdimvesa_translate_flags(u_int16_t vflags)
668314564Sdim{
669254721Semaste	static struct {
670314564Sdim		u_int16_t mask;
671309124Sdim		int set;
672314564Sdim		int reset;
673314564Sdim	} ftable[] = {
674314564Sdim		{ V_MODECOLOR, V_INFO_COLOR, 0 },
675254721Semaste		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
676314564Sdim		{ V_MODELFB, V_INFO_LINEAR, 0 },
677314564Sdim		{ V_MODENONVGA, V_INFO_NONVGA, 0 },
678309124Sdim	};
679321369Sdim	int flags;
680309124Sdim	int i;
681314564Sdim
682309124Sdim	for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
683314564Sdim		flags |= (vflags & ftable[i].mask) ?
684314564Sdim			 ftable[i].set : ftable[i].reset;
685309124Sdim	}
686314564Sdim	return (flags);
687309124Sdim}
688314564Sdim
689309124Sdimstatic int
690314564Sdimvesa_translate_mmodel(u_int8_t vmodel)
691314564Sdim{
692314564Sdim	static struct {
693314564Sdim		u_int8_t vmodel;
694314564Sdim		int mmodel;
695309124Sdim	} mtable[] = {
696321369Sdim		{ V_MMTEXT,	V_INFO_MM_TEXT },
697321369Sdim		{ V_MMCGA,	V_INFO_MM_CGA },
698309124Sdim		{ V_MMHGC,	V_INFO_MM_HGC },
699314564Sdim		{ V_MMEGA,	V_INFO_MM_PLANAR },
700309124Sdim		{ V_MMPACKED,	V_INFO_MM_PACKED },
701314564Sdim		{ V_MMDIRCOLOR,	V_INFO_MM_DIRECT },
702309124Sdim	};
703314564Sdim	int i;
704314564Sdim
705314564Sdim	for (i = 0; mtable[i].mmodel >= 0; ++i) {
706309124Sdim		if (mtable[i].vmodel == vmodel)
707314564Sdim			return (mtable[i].mmodel);
708314564Sdim	}
709314564Sdim	return (V_INFO_MM_OTHER);
710314564Sdim}
711314564Sdim
712309124Sdimstatic int
713314564Sdimvesa_get_bpscanline(struct vesa_mode *vmode)
714314564Sdim{
715314564Sdim	int bpsl;
716314564Sdim
717309124Sdim	if ((vmode->v_modeattr & V_MODEGRAPHICS) != 0) {
718314564Sdim		/* Find the minimum length. */
719314564Sdim		switch (vmode->v_bpp / vmode->v_planes) {
720309124Sdim		case 1:
721314564Sdim			bpsl = vmode->v_width / 8;
722314564Sdim			break;
723314564Sdim		case 2:
724321369Sdim			bpsl = vmode->v_width / 4;
725321369Sdim			break;
726309124Sdim		case 4:
727314564Sdim			bpsl = vmode->v_width / 2;
728314564Sdim			break;
729254721Semaste		default:
730309124Sdim			bpsl = vmode->v_width * ((vmode->v_bpp + 7) / 8);
731314564Sdim			bpsl /= vmode->v_planes;
732314564Sdim			break;
733309124Sdim		}
734314564Sdim
735314564Sdim		/* Use VBE 3.0 information if it looks sane. */
736321369Sdim		if ((vmode->v_modeattr & V_MODELFB) != 0 &&
737309124Sdim		    vesa_adp_info->v_version >= 0x0300 &&
738314564Sdim		    vmode->v_linbpscanline > bpsl)
739254721Semaste			return (vmode->v_linbpscanline);
740314564Sdim
741314564Sdim		/* Return the minimum if the mode table looks absurd. */
742254721Semaste		if (vmode->v_bpscanline < bpsl)
743314564Sdim			return (bpsl);
744296417Sdim	}
745254721Semaste
746314564Sdim	return (vmode->v_bpscanline);
747314564Sdim}
748314564Sdim
749314564Sdim#define	VESA_MAXSTR		256
750314564Sdim
751254721Semaste#define	VESA_STRCPY(dst, src)	do {				\
752254721Semaste	char *str;						\
753321369Sdim	int i;							\
754314564Sdim	dst = malloc(VESA_MAXSTR, M_DEVBUF, M_WAITOK);		\
755314564Sdim	str = x86bios_offset(BIOS_SADDRTOLADDR(src));		\
756314564Sdim	for (i = 0; i < VESA_MAXSTR - 1 && str[i] != '\0'; i++)	\
757314564Sdim		dst[i] = str[i];				\
758314564Sdim	dst[i] = '\0';						\
759254721Semaste} while (0)
760254721Semaste
761314564Sdimstatic int
762254721Semastevesa_bios_init(void)
763314564Sdim{
764314564Sdim	struct vesa_mode vmode;
765314564Sdim	struct vesa_info *buf;
766314564Sdim	video_info_t *p;
767254721Semaste	x86regs_t regs;
768254721Semaste	size_t bsize;
769314564Sdim	size_t msize;
770314564Sdim	void *vmbuf;
771314564Sdim	uint8_t *vbios;
772314564Sdim	uint32_t offs;
773314564Sdim	uint16_t vers;
774314564Sdim	int is_via_cle266;
775309124Sdim	int modes;
776314564Sdim	int i;
777321369Sdim
778314564Sdim	if (vesa_init_done)
779314564Sdim		return (0);
780314564Sdim
781314564Sdim	has_vesa_bios = FALSE;
782314564Sdim	vesa_adp_info = NULL;
783314564Sdim	vesa_bios_offs = VESA_BIOS_OFFSET;
784309124Sdim	vesa_vmode_max = 0;
785314564Sdim	vesa_vmode[0].vi_mode = EOT;
786254721Semaste
787314564Sdim	/*
788309124Sdim	 * If the VBE real mode interrupt vector is not found, try BIOS POST.
789344779Sdim	 */
790344779Sdim	vesa_bios_int10 = x86bios_get_intr(0x10);
791344779Sdim	if (vesa_bios_int10 == 0) {
792344779Sdim		if (vesa_bios_post() != 0)
793344779Sdim			return (1);
794309124Sdim		vesa_bios_int10 = x86bios_get_intr(0x10);
795360784Sdim		if (vesa_bios_int10 == 0)
796360784Sdim			return (1);
797360784Sdim	}
798360784Sdim
799360784Sdim	/*
800296417Sdim	 * Shadow video ROM.
801360784Sdim	 */
802360784Sdim	offs = vesa_bios_int10;
803321369Sdim	if (vesa_shadow_rom) {
804314564Sdim		vbios = x86bios_get_orm(vesa_bios_offs);
805309124Sdim		if (vbios != NULL) {
806314564Sdim			vesa_bios_size = vbios[2] * 512;
807344779Sdim			if (((VESA_BIOS_OFFSET << 12) & 0xffff0000) ==
808314564Sdim			    (vesa_bios_int10 & 0xffff0000) &&
809314564Sdim			    vesa_bios_size > (vesa_bios_int10 & 0xffff)) {
810344779Sdim				vesa_bios = x86bios_alloc(&vesa_bios_offs,
811309124Sdim				    vesa_bios_size, M_WAITOK);
812314564Sdim				memcpy(vesa_bios, vbios, vesa_bios_size);
813314564Sdim				offs = ((vesa_bios_offs << 12) & 0xffff0000) +
814314564Sdim				    (vesa_bios_int10 & 0xffff);
815314564Sdim				x86bios_set_intr(0x10, offs);
816314564Sdim			}
817314564Sdim		}
818309124Sdim		if (vesa_bios == NULL)
819321369Sdim			printf("VESA: failed to shadow video ROM\n");
820309124Sdim	}
821314564Sdim	if (bootverbose)
822314564Sdim		printf("VESA: INT 0x10 vector 0x%04x:0x%04x\n",
823314564Sdim		    (offs >> 16) & 0xffff, offs & 0xffff);
824314564Sdim
825314564Sdim	x86bios_init_regs(&regs);
826314564Sdim	regs.R_AX = 0x4f00;
827314564Sdim
828314564Sdim	vmbuf = x86bios_alloc(&offs, sizeof(*buf), M_WAITOK);
829254721Semaste
830314564Sdim	regs.R_ES = X86BIOS_PHYSTOSEG(offs);
831309124Sdim	regs.R_DI = X86BIOS_PHYSTOOFF(offs);
832314564Sdim
833314564Sdim	bcopy("VBE2", vmbuf, 4);	/* try for VBE2 data */
834321369Sdim	x86bios_intr(&regs, 0x10);
835314564Sdim
836309124Sdim	if (regs.R_AX != 0x004f || bcmp("VESA", vmbuf, 4) != 0)
837314564Sdim		goto fail;
838309124Sdim
839314564Sdim	vesa_adp_info = buf = malloc(sizeof(*buf), M_DEVBUF, M_WAITOK);
840314564Sdim	bcopy(vmbuf, buf, sizeof(*buf));
841314564Sdim
842314564Sdim	if (bootverbose) {
843314564Sdim		printf("VESA: information block\n");
844309124Sdim		hexdump(buf, sizeof(*buf), NULL, HD_OMIT_CHARS);
845314564Sdim	}
846321369Sdim
847314564Sdim	vers = buf->v_version = le16toh(buf->v_version);
848309124Sdim	buf->v_oemstr = le32toh(buf->v_oemstr);
849314564Sdim	buf->v_flags = le32toh(buf->v_flags);
850309124Sdim	buf->v_modetable = le32toh(buf->v_modetable);
851314564Sdim	buf->v_memsize = le16toh(buf->v_memsize);
852314564Sdim	buf->v_revision = le16toh(buf->v_revision);
853314564Sdim	buf->v_venderstr = le32toh(buf->v_venderstr);
854314564Sdim	buf->v_prodstr = le32toh(buf->v_prodstr);
855314564Sdim	buf->v_revstr = le32toh(buf->v_revstr);
856309124Sdim
857314564Sdim	if (vers < 0x0102) {
858309124Sdim		printf("VESA: VBE version %d.%d is not supported; "
859314564Sdim		    "version 1.2 or later is required.\n",
860314564Sdim		    ((vers & 0xf000) >> 12) * 10 + ((vers & 0x0f00) >> 8),
861314564Sdim		    ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f));
862314564Sdim		goto fail;
863309124Sdim	}
864360784Sdim
865360784Sdim	VESA_STRCPY(vesa_oemstr, buf->v_oemstr);
866309124Sdim	if (vers >= 0x0200) {
867360784Sdim		VESA_STRCPY(vesa_venderstr, buf->v_venderstr);
868314564Sdim		VESA_STRCPY(vesa_prodstr, buf->v_prodstr);
869314564Sdim		VESA_STRCPY(vesa_revstr, buf->v_revstr);
870314564Sdim	}
871360784Sdim	is_via_cle266 = strncmp(vesa_oemstr, VESA_VIA_CLE266,
872314564Sdim	    sizeof(VESA_VIA_CLE266)) == 0;
873314564Sdim
874314564Sdim	if (buf->v_modetable == 0)
875360784Sdim		goto fail;
876309124Sdim
877314564Sdim	msize = (size_t)buf->v_memsize * 64 * 1024;
878314564Sdim
879314564Sdim	vesa_vmodetab = x86bios_offset(BIOS_SADDRTOLADDR(buf->v_modetable));
880314564Sdim
881314564Sdim	for (i = 0, modes = 0; (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) &&
882314564Sdim	    (vesa_vmodetab[i] != 0xffff); ++i) {
883309124Sdim		vesa_vmodetab[i] = le16toh(vesa_vmodetab[i]);
884341825Sdim		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_WAITOK))
885341825Sdim			continue;
886341825Sdim
887341825Sdim		vmode.v_modeattr = le16toh(vmode.v_modeattr);
888341825Sdim		vmode.v_wgran = le16toh(vmode.v_wgran);
889309124Sdim		vmode.v_wsize = le16toh(vmode.v_wsize);
890314564Sdim		vmode.v_waseg = le16toh(vmode.v_waseg);
891314564Sdim		vmode.v_wbseg = le16toh(vmode.v_wbseg);
892309124Sdim		vmode.v_posfunc = le32toh(vmode.v_posfunc);
893314564Sdim		vmode.v_bpscanline = le16toh(vmode.v_bpscanline);
894314564Sdim		vmode.v_width = le16toh(vmode.v_width);
895314564Sdim		vmode.v_height = le16toh(vmode.v_height);
896314564Sdim		vmode.v_lfb = le32toh(vmode.v_lfb);
897314564Sdim		vmode.v_offscreen = le32toh(vmode.v_offscreen);
898314564Sdim		vmode.v_offscreensize = le16toh(vmode.v_offscreensize);
899309124Sdim		vmode.v_linbpscanline = le16toh(vmode.v_linbpscanline);
900314564Sdim		vmode.v_maxpixelclock = le32toh(vmode.v_maxpixelclock);
901314564Sdim
902309124Sdim		/* reject unsupported modes */
903314564Sdim#if 0
904314564Sdim		if ((vmode.v_modeattr &
905314564Sdim		    (V_MODESUPP | V_MODEOPTINFO | V_MODENONVGA)) !=
906309124Sdim		    (V_MODESUPP | V_MODEOPTINFO))
907314564Sdim			continue;
908314564Sdim#else
909314564Sdim		if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
910254721Semaste#if VESA_DEBUG > 1
911314564Sdim			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
912314564Sdim			    " attr = %x\n",
913314564Sdim			    vmode.v_modeattr & V_MODEGRAPHICS ?
914314564Sdim			    "graphics" : "text",
915314564Sdim			    vmode.v_width, vmode.v_height, vmode.v_bpp,
916309124Sdim			    vmode.v_modeattr);
917314564Sdim#endif
918309124Sdim			continue;
919314564Sdim		}
920314564Sdim#endif
921309124Sdim
922314564Sdim		bsize = vesa_get_bpscanline(&vmode) * vmode.v_height;
923309124Sdim		if ((vmode.v_modeattr & V_MODEGRAPHICS) != 0)
924314564Sdim			bsize *= vmode.v_planes;
925314564Sdim
926314564Sdim		/* Does it have enough memory to support this mode? */
927314564Sdim		if (msize < bsize) {
928314564Sdim#if VESA_DEBUG > 1
929309124Sdim			printf("Rejecting VESA %s mode: %d x %d x %d bpp "
930314564Sdim			    " attr = %x, not enough memory\n",
931314564Sdim			    vmode.v_modeattr & V_MODEGRAPHICS ?
932309124Sdim			    "graphics" : "text",
933314564Sdim			    vmode.v_width, vmode.v_height, vmode.v_bpp,
934321369Sdim			    vmode.v_modeattr);
935314564Sdim#endif
936314564Sdim			continue;
937314564Sdim		}
938314564Sdim
939254721Semaste		/* expand the array if necessary */
940309124Sdim		if (modes >= vesa_vmode_max) {
941314564Sdim			vesa_vmode_max += MODE_TABLE_DELTA;
942314564Sdim			p = malloc(sizeof(*vesa_vmode) * (vesa_vmode_max + 1),
943314564Sdim			    M_DEVBUF, M_WAITOK);
944309124Sdim#if VESA_DEBUG > 1
945314564Sdim			printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
946314564Sdim			    modes, vesa_vmode_max);
947314564Sdim#endif
948254721Semaste			if (modes > 0) {
949314564Sdim				bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
950309124Sdim				free(vesa_vmode, M_DEVBUF);
951314564Sdim			}
952309124Sdim			vesa_vmode = p;
953321369Sdim		}
954309124Sdim
955314564Sdim#if VESA_DEBUG > 1
956309124Sdim		printf("Found VESA %s mode: %d x %d x %d bpp\n",
957314564Sdim		    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
958314564Sdim		    vmode.v_width, vmode.v_height, vmode.v_bpp);
959309124Sdim#endif
960314564Sdim		if (is_via_cle266) {
961309124Sdim		    if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
962314564Sdim			vmode.v_width &= 0xff;
963309124Sdim			vmode.v_waseg = 0xb8000 >> 4;
964314564Sdim		    }
965314564Sdim		}
966314564Sdim
967314564Sdim		/* copy some fields */
968314564Sdim		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
969309124Sdim		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
970321369Sdim		vesa_vmode[modes].vi_width = vmode.v_width;
971321369Sdim		vesa_vmode[modes].vi_height = vmode.v_height;
972309124Sdim		vesa_vmode[modes].vi_depth = vmode.v_bpp;
973314564Sdim		vesa_vmode[modes].vi_planes = vmode.v_planes;
974309124Sdim		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
975314564Sdim		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
976309124Sdim		vesa_vmode[modes].vi_window = (vm_offset_t)vmode.v_waseg << 4;
977314564Sdim		/* XXX window B */
978314564Sdim		vesa_vmode[modes].vi_window_size = vmode.v_wsize * 1024;
979314564Sdim		vesa_vmode[modes].vi_window_gran = vmode.v_wgran * 1024;
980309124Sdim		if (vmode.v_modeattr & V_MODELFB)
981314564Sdim			vesa_vmode[modes].vi_buffer = vmode.v_lfb;
982314564Sdim		vesa_vmode[modes].vi_buffer_size = bsize;
983314564Sdim		vesa_vmode[modes].vi_mem_model =
984314564Sdim		    vesa_translate_mmodel(vmode.v_memmodel);
985314564Sdim		switch (vesa_vmode[modes].vi_mem_model) {
986309124Sdim		case V_INFO_MM_DIRECT:
987314564Sdim			if ((vmode.v_modeattr & V_MODELFB) != 0 &&
988314564Sdim			    vers >= 0x0300) {
989314564Sdim				vesa_vmode[modes].vi_pixel_fields[0] =
990314564Sdim				    vmode.v_linredfieldpos;
991309124Sdim				vesa_vmode[modes].vi_pixel_fields[1] =
992314564Sdim				    vmode.v_lingreenfieldpos;
993314564Sdim				vesa_vmode[modes].vi_pixel_fields[2] =
994309124Sdim				    vmode.v_linbluefieldpos;
995314564Sdim				vesa_vmode[modes].vi_pixel_fields[3] =
996314564Sdim				    vmode.v_linresfieldpos;
997314564Sdim				vesa_vmode[modes].vi_pixel_fsizes[0] =
998321369Sdim				    vmode.v_linredmasksize;
999321369Sdim				vesa_vmode[modes].vi_pixel_fsizes[1] =
1000309124Sdim				    vmode.v_lingreenmasksize;
1001314564Sdim				vesa_vmode[modes].vi_pixel_fsizes[2] =
1002314564Sdim				    vmode.v_linbluemasksize;
1003254721Semaste				vesa_vmode[modes].vi_pixel_fsizes[3] =
1004309124Sdim				    vmode.v_linresmasksize;
1005314564Sdim			} else {
1006314564Sdim				vesa_vmode[modes].vi_pixel_fields[0] =
1007309124Sdim				    vmode.v_redfieldpos;
1008314564Sdim				vesa_vmode[modes].vi_pixel_fields[1] =
1009314564Sdim				    vmode.v_greenfieldpos;
1010321369Sdim				vesa_vmode[modes].vi_pixel_fields[2] =
1011309124Sdim				    vmode.v_bluefieldpos;
1012314564Sdim				vesa_vmode[modes].vi_pixel_fields[3] =
1013254721Semaste				    vmode.v_resfieldpos;
1014296417Sdim				vesa_vmode[modes].vi_pixel_fsizes[0] =
1015314564Sdim				    vmode.v_redmasksize;
1016314564Sdim				vesa_vmode[modes].vi_pixel_fsizes[1] =
1017314564Sdim				    vmode.v_greenmasksize;
1018314564Sdim				vesa_vmode[modes].vi_pixel_fsizes[2] =
1019254721Semaste				    vmode.v_bluemasksize;
1020314564Sdim				vesa_vmode[modes].vi_pixel_fsizes[3] =
1021314564Sdim				    vmode.v_resmasksize;
1022314564Sdim			}
1023309124Sdim			/* FALLTHROUGH */
1024314564Sdim		case V_INFO_MM_PACKED:
1025314564Sdim			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7) / 8;
1026314564Sdim			break;
1027254721Semaste		}
1028254721Semaste		vesa_vmode[modes].vi_flags =
1029314564Sdim		    vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
1030314564Sdim
1031314564Sdim		++modes;
1032314564Sdim	}
1033321369Sdim	vesa_vmode[modes].vi_mode = EOT;
1034314564Sdim
1035314564Sdim	if (bootverbose)
1036314564Sdim		printf("VESA: %d mode(s) found\n", modes);
1037314564Sdim
1038314564Sdim	has_vesa_bios = (modes > 0);
1039314564Sdim	if (!has_vesa_bios)
1040254721Semaste		goto fail;
1041254721Semaste
1042314564Sdim	x86bios_free(vmbuf, sizeof(*buf));
1043254721Semaste
1044314564Sdim	vesa_state_buf_size = vesa_bios_state_buf_size();
1045314564Sdim	vesa_palette = x86bios_alloc(&vesa_palette_offs,
1046314564Sdim	    VESA_PALETTE_SIZE + vesa_state_buf_size, M_WAITOK);
1047314564Sdim	if (vesa_state_buf_size > 0) {
1048314564Sdim		vesa_state_buf = vesa_palette + VESA_PALETTE_SIZE;
1049309124Sdim		vesa_state_buf_offs = vesa_palette_offs + VESA_PALETTE_SIZE;
1050314564Sdim	}
1051321369Sdim
1052314564Sdim	return (0);
1053254721Semaste
1054314564Sdimfail:
1055254721Semaste	if (vesa_bios != NULL) {
1056314564Sdim		x86bios_set_intr(0x10, vesa_bios_int10);
1057360784Sdim		vesa_bios_offs = VESA_BIOS_OFFSET;
1058360784Sdim		x86bios_free(vesa_bios, vesa_bios_size);
1059360784Sdim		vesa_bios = NULL;
1060360784Sdim	}
1061314564Sdim	if (vmbuf != NULL)
1062309124Sdim		x86bios_free(vmbuf, sizeof(buf));
1063314564Sdim	if (vesa_adp_info != NULL) {
1064254721Semaste		free(vesa_adp_info, M_DEVBUF);
1065314564Sdim		vesa_adp_info = NULL;
1066309124Sdim	}
1067314564Sdim	if (vesa_oemstr != NULL) {
1068309124Sdim		free(vesa_oemstr, M_DEVBUF);
1069314564Sdim		vesa_oemstr = NULL;
1070314564Sdim	}
1071309124Sdim	if (vesa_venderstr != NULL) {
1072314564Sdim		free(vesa_venderstr, M_DEVBUF);
1073314564Sdim		vesa_venderstr = NULL;
1074314564Sdim	}
1075314564Sdim	if (vesa_prodstr != NULL) {
1076314564Sdim		free(vesa_prodstr, M_DEVBUF);
1077314564Sdim		vesa_prodstr = NULL;
1078309124Sdim	}
1079314564Sdim	if (vesa_revstr != NULL) {
1080309124Sdim		free(vesa_revstr, M_DEVBUF);
1081314564Sdim		vesa_revstr = NULL;
1082314564Sdim	}
1083309124Sdim	return (1);
1084321369Sdim}
1085309124Sdim
1086314564Sdimstatic void
1087309124Sdimvesa_clear_modes(video_info_t *info, int color)
1088314564Sdim{
1089314564Sdim	while (info->vi_mode != EOT) {
1090314564Sdim		if ((info->vi_flags & V_INFO_COLOR) != color)
1091314564Sdim			info->vi_mode = NA;
1092314564Sdim		++info;
1093254721Semaste	}
1094314564Sdim}
1095309124Sdim
1096314564Sdim/* entry points */
1097314564Sdim
1098321369Sdimstatic int
1099314564Sdimvesa_configure(int flags)
1100254721Semaste{
1101314564Sdim	video_adapter_t *adp;
1102254721Semaste	int adapters;
1103314564Sdim	int error;
1104360784Sdim	int i;
1105360784Sdim
1106360784Sdim	if (vesa_init_done)
1107360784Sdim		return (0);
1108254721Semaste	if (flags & VIO_PROBE_ONLY)
1109309124Sdim		return (0);
1110314564Sdim
1111314564Sdim	/*
1112309124Sdim	 * If the VESA module has already been loaded, abort loading
1113314564Sdim	 * the module this time.
1114314564Sdim	 */
1115314564Sdim	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
1116309124Sdim		if (adp->va_flags & V_ADP_VESA)
1117321369Sdim			return (ENXIO);
1118254721Semaste		if (adp->va_type == KD_VGA)
1119314564Sdim			break;
1120309124Sdim	}
1121314564Sdim
1122314564Sdim	/*
1123309124Sdim	 * The VGA adapter is not found.  This is because either
1124314564Sdim	 * 1) the VGA driver has not been initialized, or 2) the VGA card
1125314564Sdim	 * is not present.  If 1) is the case, we shall defer
1126309124Sdim	 * initialization for now and try again later.
1127314564Sdim	 */
1128309124Sdim	if (adp == NULL) {
1129314564Sdim		vga_sub_configure = vesa_configure;
1130309124Sdim		return (ENODEV);
1131314564Sdim	}
1132314564Sdim
1133314564Sdim	/* count number of registered adapters */
1134321369Sdim	for (++i; vid_get_adapter(i) != NULL; ++i)
1135321369Sdim		;
1136309124Sdim	adapters = i;
1137314564Sdim
1138314564Sdim	/* call VESA BIOS */
1139254721Semaste	vesa_adp = adp;
1140309124Sdim	if (vesa_bios_init()) {
1141314564Sdim		vesa_adp = NULL;
1142314564Sdim		return (ENXIO);
1143296417Sdim	}
1144314564Sdim	vesa_adp->va_flags |= V_ADP_VESA;
1145314564Sdim
1146254721Semaste	/* remove conflicting modes if we have more than one adapter */
1147314564Sdim	if (adapters > 1) {
1148254721Semaste		vesa_clear_modes(vesa_vmode,
1149254721Semaste				 (vesa_adp->va_flags & V_ADP_COLOR) ?
1150321369Sdim				     V_INFO_COLOR : 0);
1151314564Sdim	}
1152314564Sdim
1153314564Sdim	if ((error = vesa_load_ioctl()) == 0) {
1154314564Sdim		prevvidsw = vidsw[vesa_adp->va_index];
1155314564Sdim		vidsw[vesa_adp->va_index] = &vesavidsw;
1156254721Semaste		vesa_init_done = TRUE;
1157254721Semaste	} else {
1158314564Sdim		vesa_adp = NULL;
1159254721Semaste		return (error);
1160314564Sdim	}
1161314564Sdim
1162314564Sdim	return (0);
1163314564Sdim}
1164314564Sdim
1165314564Sdim#if 0
1166314564Sdimstatic int
1167314564Sdimvesa_nop(void)
1168321369Sdim{
1169314564Sdim
1170314564Sdim	return (0);
1171314564Sdim}
1172314564Sdim#endif
1173314564Sdim
1174360784Sdimstatic int
1175360784Sdimvesa_error(void)
1176360784Sdim{
1177360784Sdim
1178254721Semaste	return (1);
1179309124Sdim}
1180314564Sdim
1181309124Sdimstatic int
1182314564Sdimvesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1183314564Sdim{
1184314564Sdim
1185314564Sdim	return ((*prevvidsw->probe)(unit, adpp, arg, flags));
1186314564Sdim}
1187314564Sdim
1188254721Semastestatic int
1189314564Sdimvesa_init(int unit, video_adapter_t *adp, int flags)
1190254721Semaste{
1191314564Sdim
1192314564Sdim	return ((*prevvidsw->init)(unit, adp, flags));
1193314564Sdim}
1194314564Sdim
1195314564Sdimstatic int
1196309124Sdimvesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1197314564Sdim{
1198309124Sdim	int i;
1199314564Sdim
1200314564Sdim	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
1201314564Sdim		return (0);
1202314564Sdim
1203314564Sdim	if (adp != vesa_adp)
1204309124Sdim		return (1);
1205314564Sdim
1206314564Sdim	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
1207314564Sdim				     vesa_adp->va_flags & V_ADP_COLOR, mode);
1208314564Sdim	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1209314564Sdim		if (vesa_vmode[i].vi_mode == NA)
1210314564Sdim			continue;
1211314564Sdim		if (vesa_vmode[i].vi_mode == mode) {
1212309124Sdim			*info = vesa_vmode[i];
1213353358Sdim			return (0);
1214353358Sdim		}
1215309124Sdim	}
1216321369Sdim	return (1);
1217309124Sdim}
1218314564Sdim
1219314564Sdimstatic int
1220309124Sdimvesa_query_mode(video_adapter_t *adp, video_info_t *info)
1221314564Sdim{
1222314564Sdim	int i;
1223314564Sdim
1224314564Sdim	if ((*prevvidsw->query_mode)(adp, info) == 0)
1225314564Sdim		return (0);
1226254721Semaste	if (adp != vesa_adp)
1227314564Sdim		return (ENODEV);
1228309124Sdim
1229314564Sdim	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
1230314564Sdim		if ((info->vi_width != 0)
1231321369Sdim		    && (info->vi_width != vesa_vmode[i].vi_width))
1232314564Sdim			continue;
1233309124Sdim		if ((info->vi_height != 0)
1234314564Sdim		    && (info->vi_height != vesa_vmode[i].vi_height))
1235254721Semaste			continue;
1236314564Sdim		if ((info->vi_cwidth != 0)
1237360784Sdim		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
1238360784Sdim			continue;
1239360784Sdim		if ((info->vi_cheight != 0)
1240360784Sdim		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
1241314564Sdim			continue;
1242309124Sdim		if ((info->vi_depth != 0)
1243321369Sdim		    && (info->vi_depth != vesa_vmode[i].vi_depth))
1244309124Sdim			continue;
1245314564Sdim		if ((info->vi_planes != 0)
1246309124Sdim		    && (info->vi_planes != vesa_vmode[i].vi_planes))
1247314564Sdim			continue;
1248314564Sdim		/* pixel format, memory model */
1249314564Sdim		if ((info->vi_flags != 0)
1250314564Sdim		    && (info->vi_flags != vesa_vmode[i].vi_flags))
1251314564Sdim			continue;
1252314564Sdim		*info = vesa_vmode[i];
1253309124Sdim		return (0);
1254314564Sdim	}
1255309124Sdim	return (ENODEV);
1256314564Sdim}
1257314564Sdim
1258309124Sdimstatic int
1259314564Sdimvesa_set_mode(video_adapter_t *adp, int mode)
1260314564Sdim{
1261314564Sdim	video_info_t info;
1262314564Sdim
1263314564Sdim	if (adp != vesa_adp)
1264314564Sdim		return ((*prevvidsw->set_mode)(adp, mode));
1265309124Sdim
1266314564Sdim	mode = vesa_map_gen_mode_num(adp->va_type,
1267314564Sdim				     adp->va_flags & V_ADP_COLOR, mode);
1268314564Sdim#if VESA_DEBUG > 0
1269314564Sdim	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1270309124Sdim		adp->va_mode, adp->va_mode, mode, mode);
1271314564Sdim#endif
1272314564Sdim	/*
1273314564Sdim	 * If the current mode is a VESA mode and the new mode is not,
1274309124Sdim	 * restore the state of the adapter first by setting one of the
1275314564Sdim	 * standard VGA mode, so that non-standard, extended SVGA registers
1276309124Sdim	 * are set to the state compatible with the standard VGA modes.
1277314564Sdim	 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1278314564Sdim	 * the new mode correctly.
1279314564Sdim	 */
1280309124Sdim	if (VESA_MODE(adp->va_mode)) {
1281314564Sdim		if (!VESA_MODE(mode) &&
1282314564Sdim		    (*prevvidsw->get_info)(adp, mode, &info) == 0) {
1283314564Sdim			if ((adp->va_flags & V_ADP_DAC8) != 0) {
1284314564Sdim				vesa_bios_set_dac(6);
1285254721Semaste				adp->va_flags &= ~V_ADP_DAC8;
1286314564Sdim			}
1287309124Sdim			int10_set_mode(adp->va_initial_bios_mode);
1288314564Sdim			if (adp->va_info.vi_flags & V_INFO_LINEAR)
1289314564Sdim				pmap_unmapdev(adp->va_buffer,
1290314564Sdim				    vesa_adp_info->v_memsize * 64 * 1024);
1291309124Sdim			/*
1292321369Sdim			 * Once (*prevvidsw->get_info)() succeeded,
1293309124Sdim			 * (*prevvidsw->set_mode)() below won't fail...
1294314564Sdim			 */
1295254721Semaste		}
1296314564Sdim	}
1297314564Sdim
1298309124Sdim	/* we may not need to handle this mode after all... */
1299314564Sdim	if (!VESA_MODE(mode) && (*prevvidsw->set_mode)(adp, mode) == 0)
1300314564Sdim		return (0);
1301309124Sdim
1302314564Sdim	/* is the new mode supported? */
1303309124Sdim	if (vesa_get_info(adp, mode, &info))
1304314564Sdim		return (1);
1305309124Sdim	/* assert(VESA_MODE(mode)); */
1306314564Sdim
1307314564Sdim#if VESA_DEBUG > 0
1308314564Sdim	printf("VESA: about to set a VESA mode...\n");
1309321369Sdim#endif
1310321369Sdim	/* don't use the linear frame buffer for text modes. XXX */
1311309124Sdim	if (!(info.vi_flags & V_INFO_GRAPHICS))
1312314564Sdim		info.vi_flags &= ~V_INFO_LINEAR;
1313314564Sdim
1314314564Sdim	if ((info.vi_flags & V_INFO_LINEAR) != 0)
1315309124Sdim		mode |= 0x4000;
1316314564Sdim	if (vesa_bios_set_mode(mode))
1317314564Sdim		return (1);
1318309124Sdim
1319314564Sdim	/* Palette format is reset by the above VBE function call. */
1320309124Sdim	adp->va_flags &= ~V_ADP_DAC8;
1321254721Semaste
1322314564Sdim	if ((vesa_adp_info->v_flags & V_DAC8) != 0 &&
1323314564Sdim	    (info.vi_flags & V_INFO_GRAPHICS) != 0 &&
1324254721Semaste	    vesa_bios_set_dac(8) > 6)
1325254721Semaste		adp->va_flags |= V_ADP_DAC8;
1326314564Sdim
1327321369Sdim	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1328314564Sdim		pmap_unmapdev(adp->va_buffer,
1329314564Sdim		    vesa_adp_info->v_memsize * 64 * 1024);
1330314564Sdim
1331314564Sdim#if VESA_DEBUG > 0
1332314564Sdim	printf("VESA: mode set!\n");
1333254721Semaste#endif
1334254721Semaste	vesa_adp->va_mode = mode & 0x1ff;	/* Mode number is 9-bit. */
1335314564Sdim	vesa_adp->va_flags &= ~V_ADP_COLOR;
1336314564Sdim	vesa_adp->va_flags |=
1337254721Semaste		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1338314564Sdim	vesa_adp->va_crtc_addr =
1339314564Sdim		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1340309124Sdim
1341314564Sdim	vesa_adp->va_line_width = info.vi_buffer_size / info.vi_height;
1342314564Sdim	if ((info.vi_flags & V_INFO_GRAPHICS) != 0)
1343254721Semaste		vesa_adp->va_line_width /= info.vi_planes;
1344254721Semaste
1345254721Semaste#ifdef MODE_TABLE_BROKEN
1346314564Sdim	/* If VBE function returns bigger bytes per scan line, use it. */
1347321369Sdim	{
1348314564Sdim		int bpsl = vesa_bios_get_line_length();
1349276479Sdim		if (bpsl > vesa_adp->va_line_width) {
1350314564Sdim			vesa_adp->va_line_width = bpsl;
1351314564Sdim			info.vi_buffer_size = bpsl * info.vi_height;
1352276479Sdim			if ((info.vi_flags & V_INFO_GRAPHICS) != 0)
1353314564Sdim				info.vi_buffer_size *= info.vi_planes;
1354276479Sdim		}
1355314564Sdim	}
1356314564Sdim#endif
1357314564Sdim
1358314564Sdim	if (info.vi_flags & V_INFO_LINEAR) {
1359276479Sdim#if VESA_DEBUG > 1
1360314564Sdim		printf("VESA: setting up LFB\n");
1361314564Sdim#endif
1362276479Sdim		vesa_adp->va_buffer =
1363314564Sdim		    (vm_offset_t)pmap_mapdev_attr(info.vi_buffer,
1364314564Sdim		    vesa_adp_info->v_memsize * 64 * 1024, PAT_WRITE_COMBINING);
1365314564Sdim		vesa_adp->va_window = vesa_adp->va_buffer;
1366314564Sdim		vesa_adp->va_window_size = info.vi_buffer_size / info.vi_planes;
1367276479Sdim		vesa_adp->va_window_gran = info.vi_buffer_size / info.vi_planes;
1368314564Sdim	} else {
1369314564Sdim		vesa_adp->va_buffer = 0;
1370276479Sdim		vesa_adp->va_window = (vm_offset_t)x86bios_offset(info.vi_window);
1371314564Sdim		vesa_adp->va_window_size = info.vi_window_size;
1372314564Sdim		vesa_adp->va_window_gran = info.vi_window_gran;
1373314564Sdim	}
1374276479Sdim	vesa_adp->va_buffer_size = info.vi_buffer_size;
1375314564Sdim	vesa_adp->va_window_orig = 0;
1376314564Sdim	vesa_adp->va_disp_start.x = 0;
1377360784Sdim	vesa_adp->va_disp_start.y = 0;
1378360784Sdim#if VESA_DEBUG > 0
1379314564Sdim	printf("vesa_set_mode(): vi_width:%d, line_width:%d\n",
1380314564Sdim	       info.vi_width, vesa_adp->va_line_width);
1381314564Sdim#endif
1382314564Sdim	bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1383314564Sdim
1384314564Sdim	/* move hardware cursor out of the way */
1385276479Sdim	(*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1386314564Sdim
1387276479Sdim	return (0);
1388314564Sdim}
1389254721Semaste
1390254721Semastestatic int
1391321369Sdimvesa_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1392314564Sdim	       u_char *data, int ch, int count)
1393314564Sdim{
1394314564Sdim
1395254721Semaste	return ((*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data,
1396314564Sdim	    ch, count));
1397314564Sdim}
1398314564Sdim
1399276479Sdimstatic int
1400314564Sdimvesa_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1401276479Sdim	       u_char *data, int ch, int count)
1402314564Sdim{
1403314564Sdim
1404314564Sdim	return ((*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data,
1405314564Sdim		ch, count));
1406276479Sdim}
1407314564Sdim
1408314564Sdimstatic int
1409314564Sdimvesa_show_font(video_adapter_t *adp, int page)
1410314564Sdim{
1411314564Sdim
1412314564Sdim	return ((*prevvidsw->show_font)(adp, page));
1413360784Sdim}
1414360784Sdim
1415360784Sdimstatic int
1416360784Sdimvesa_save_palette(video_adapter_t *adp, u_char *palette)
1417314564Sdim{
1418314564Sdim	int bits;
1419254721Semaste
1420276479Sdim	if (adp == vesa_adp && VESA_MODE(adp->va_mode)) {
1421314564Sdim		bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1422314564Sdim		if (vesa_bios_save_palette(0, 256, palette, bits) == 0)
1423314564Sdim			return (0);
1424276479Sdim	}
1425314564Sdim
1426314564Sdim	return ((*prevvidsw->save_palette)(adp, palette));
1427254721Semaste}
1428314564Sdim
1429276479Sdimstatic int
1430314564Sdimvesa_load_palette(video_adapter_t *adp, u_char *palette)
1431254721Semaste{
1432254721Semaste	int bits;
1433314564Sdim
1434314564Sdim	if (adp == vesa_adp && VESA_MODE(adp->va_mode)) {
1435314564Sdim		bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1436309124Sdim		if (vesa_bios_load_palette(0, 256, palette, bits) == 0)
1437314564Sdim			return (0);
1438314564Sdim	}
1439314564Sdim
1440254721Semaste	return ((*prevvidsw->load_palette)(adp, palette));
1441314564Sdim}
1442314564Sdim
1443314564Sdimstatic int
1444254721Semastevesa_set_border(video_adapter_t *adp, int color)
1445296417Sdim{
1446314564Sdim
1447314564Sdim	return ((*prevvidsw->set_border)(adp, color));
1448}
1449
1450static int
1451vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1452{
1453
1454	if (adp != vesa_adp)
1455		return ((*prevvidsw->save_state)(adp, p, size));
1456
1457	if (vesa_state_buf_size == 0)
1458		return (1);
1459	if (size == 0)
1460		return (offsetof(adp_state_t, regs) + vesa_state_buf_size);
1461	if (size < (offsetof(adp_state_t, regs) + vesa_state_buf_size))
1462		return (1);
1463
1464	((adp_state_t *)p)->sig = V_STATE_SIG;
1465	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1466	return (vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs));
1467}
1468
1469static int
1470vesa_load_state(video_adapter_t *adp, void *p)
1471{
1472	int mode;
1473
1474	if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1475		return ((*prevvidsw->load_state)(adp, p));
1476
1477	if (vesa_state_buf_size == 0)
1478		return (1);
1479
1480	/* Try BIOS POST to restore a sane state. */
1481	(void)vesa_bios_post();
1482	mode = adp->va_mode;
1483	(void)vesa_set_mode(adp, adp->va_initial_mode);
1484	if (mode != adp->va_initial_mode)
1485		(void)vesa_set_mode(adp, mode);
1486
1487	return (vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs));
1488}
1489
1490#if 0
1491static int
1492vesa_get_origin(video_adapter_t *adp, off_t *offset)
1493{
1494	x86regs_t regs;
1495
1496	x86bios_init_regs(&regs);
1497	regs.R_AX = 0x4f05;
1498	regs.R_BL = 0x10;
1499
1500	x86bios_intr(&regs, 0x10);
1501
1502	if (regs.R_AX != 0x004f)
1503		return (1);
1504	*offset = regs.DX * adp->va_window_gran;
1505
1506	return (0);
1507}
1508#endif
1509
1510static int
1511vesa_set_origin(video_adapter_t *adp, off_t offset)
1512{
1513	x86regs_t regs;
1514
1515	/*
1516	 * This function should return as quickly as possible to
1517	 * maintain good performance of the system. For this reason,
1518	 * error checking is kept minimal and let the VESA BIOS to
1519	 * detect error.
1520	 */
1521	if (adp != vesa_adp)
1522		return ((*prevvidsw->set_win_org)(adp, offset));
1523
1524	/* if this is a linear frame buffer, do nothing */
1525	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1526		return (0);
1527	/* XXX */
1528	if (adp->va_window_gran == 0)
1529		return (1);
1530
1531	x86bios_init_regs(&regs);
1532	regs.R_AX = 0x4f05;
1533	regs.R_DX = offset / adp->va_window_gran;
1534
1535	x86bios_intr(&regs, 0x10);
1536
1537	if (regs.R_AX != 0x004f)
1538		return (1);
1539
1540	x86bios_init_regs(&regs);
1541	regs.R_AX = 0x4f05;
1542	regs.R_BL = 1;
1543	regs.R_DX = offset / adp->va_window_gran;
1544	x86bios_intr(&regs, 0x10);
1545
1546	adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1547	return (0);			/* XXX */
1548}
1549
1550static int
1551vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1552{
1553
1554	return ((*prevvidsw->read_hw_cursor)(adp, col, row));
1555}
1556
1557static int
1558vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1559{
1560
1561	return ((*prevvidsw->set_hw_cursor)(adp, col, row));
1562}
1563
1564static int
1565vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1566			 int celsize, int blink)
1567{
1568
1569	return ((*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1570	    blink));
1571}
1572
1573static int
1574vesa_blank_display(video_adapter_t *adp, int mode)
1575{
1576
1577	/* XXX: use VESA DPMS */
1578	return ((*prevvidsw->blank_display)(adp, mode));
1579}
1580
1581static int
1582vesa_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
1583	  int prot, vm_memattr_t *memattr)
1584{
1585
1586#if VESA_DEBUG > 0
1587	printf("vesa_mmap(): window:0x%tx, buffer:0x%tx, offset:0x%jx\n",
1588	       adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1589#endif
1590
1591	if ((adp == vesa_adp) &&
1592	    (adp->va_info.vi_flags & V_INFO_LINEAR) != 0) {
1593		/* va_window_size == va_buffer_size/vi_planes */
1594		/* XXX: is this correct? */
1595		if (offset > adp->va_window_size - PAGE_SIZE)
1596			return (-1);
1597		*paddr = adp->va_info.vi_buffer + offset;
1598		return (0);
1599	}
1600	return ((*prevvidsw->mmap)(adp, offset, paddr, prot, memattr));
1601}
1602
1603static int
1604vesa_clear(video_adapter_t *adp)
1605{
1606
1607	return ((*prevvidsw->clear)(adp));
1608}
1609
1610static int
1611vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1612{
1613
1614	return ((*prevvidsw->fill_rect)(adp, val, x, y, cx, cy));
1615}
1616
1617static int
1618vesa_bitblt(video_adapter_t *adp,...)
1619{
1620
1621	/* FIXME */
1622	return (1);
1623}
1624
1625static int
1626get_palette(video_adapter_t *adp, int base, int count,
1627	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1628{
1629	u_char *r;
1630	u_char *g;
1631	u_char *b;
1632	int bits;
1633	int error;
1634
1635	if (base < 0 || base >= 256 || count < 0 || count > 256)
1636		return (1);
1637	if ((base + count) > 256)
1638		return (1);
1639	if (!VESA_MODE(adp->va_mode))
1640		return (1);
1641
1642	bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1643	r = malloc(count * 3, M_DEVBUF, M_WAITOK);
1644	g = r + count;
1645	b = g + count;
1646	error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1647	if (error == 0) {
1648		copyout(r, red, count);
1649		copyout(g, green, count);
1650		copyout(b, blue, count);
1651		if (trans != NULL) {
1652			bzero(r, count);
1653			copyout(r, trans, count);
1654		}
1655	}
1656	free(r, M_DEVBUF);
1657
1658	return (error);
1659}
1660
1661static int
1662set_palette(video_adapter_t *adp, int base, int count,
1663	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1664{
1665	u_char *r;
1666	u_char *g;
1667	u_char *b;
1668	int bits;
1669	int error;
1670
1671	if (base < 0 || base >= 256 || count < 0 || count > 256)
1672		return (1);
1673	if ((base + count) > 256)
1674		return (1);
1675	if (!VESA_MODE(adp->va_mode))
1676		return (1);
1677
1678	bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 8 : 6;
1679	r = malloc(count * 3, M_DEVBUF, M_WAITOK);
1680	g = r + count;
1681	b = g + count;
1682	copyin(red, r, count);
1683	copyin(green, g, count);
1684	copyin(blue, b, count);
1685
1686	error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1687	free(r, M_DEVBUF);
1688
1689	return (error);
1690}
1691
1692static int
1693vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1694{
1695	int bytes;
1696
1697	if (adp != vesa_adp)
1698		return ((*prevvidsw->ioctl)(adp, cmd, arg));
1699
1700	switch (cmd) {
1701	case FBIO_SETWINORG:	/* set frame buffer window origin */
1702		if (!VESA_MODE(adp->va_mode))
1703			return (*prevvidsw->ioctl)(adp, cmd, arg);
1704		return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1705
1706	case FBIO_SETDISPSTART:	/* set display start address */
1707		if (!VESA_MODE(adp->va_mode))
1708			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1709		if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1710					((video_display_start_t *)arg)->y))
1711			return (ENODEV);
1712		adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1713		adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1714		return (0);
1715
1716	case FBIO_SETLINEWIDTH:	/* set line length in pixel */
1717		if (!VESA_MODE(adp->va_mode))
1718			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1719		if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1720			return (ENODEV);
1721		adp->va_line_width = bytes;
1722#if VESA_DEBUG > 1
1723		printf("new line width:%d\n", adp->va_line_width);
1724#endif
1725		return (0);
1726
1727	case FBIO_GETPALETTE:	/* get color palette */
1728		if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1729				((video_color_palette_t *)arg)->count,
1730				((video_color_palette_t *)arg)->red,
1731				((video_color_palette_t *)arg)->green,
1732				((video_color_palette_t *)arg)->blue,
1733				((video_color_palette_t *)arg)->transparent))
1734			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1735		return (0);
1736
1737
1738	case FBIO_SETPALETTE:	/* set color palette */
1739		if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1740				((video_color_palette_t *)arg)->count,
1741				((video_color_palette_t *)arg)->red,
1742				((video_color_palette_t *)arg)->green,
1743				((video_color_palette_t *)arg)->blue,
1744				((video_color_palette_t *)arg)->transparent))
1745			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1746		return (0);
1747
1748	case FBIOGETCMAP:	/* get color palette */
1749		if (get_palette(adp, ((struct fbcmap *)arg)->index,
1750				((struct fbcmap *)arg)->count,
1751				((struct fbcmap *)arg)->red,
1752				((struct fbcmap *)arg)->green,
1753				((struct fbcmap *)arg)->blue, NULL))
1754			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1755		return (0);
1756
1757	case FBIOPUTCMAP:	/* set color palette */
1758		if (set_palette(adp, ((struct fbcmap *)arg)->index,
1759				((struct fbcmap *)arg)->count,
1760				((struct fbcmap *)arg)->red,
1761				((struct fbcmap *)arg)->green,
1762				((struct fbcmap *)arg)->blue, NULL))
1763			return ((*prevvidsw->ioctl)(adp, cmd, arg));
1764		return (0);
1765
1766	default:
1767		return ((*prevvidsw->ioctl)(adp, cmd, arg));
1768	}
1769}
1770
1771static int
1772vesa_diag(video_adapter_t *adp, int level)
1773{
1774	int error;
1775
1776	/* call the previous handler first */
1777	error = (*prevvidsw->diag)(adp, level);
1778	if (error)
1779		return (error);
1780
1781	if (adp != vesa_adp)
1782		return (1);
1783
1784	if (level <= 0)
1785		return (0);
1786
1787	return (0);
1788}
1789
1790static int
1791vesa_bios_info(int level)
1792{
1793#if VESA_DEBUG > 1
1794	struct vesa_mode vmode;
1795	int i;
1796#endif
1797	uint16_t vers;
1798
1799	vers = vesa_adp_info->v_version;
1800
1801	if (bootverbose) {
1802		/* general adapter information */
1803		printf(
1804	"VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1805		    (vers >> 12) * 10 + ((vers & 0x0f00) >> 8),
1806		    ((vers & 0x00f0) >> 4) * 10 + (vers & 0x000f),
1807		    vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1808		    vesa_vmodetab, vesa_adp_info->v_modetable);
1809
1810		/* OEM string */
1811		if (vesa_oemstr != NULL)
1812			printf("VESA: %s\n", vesa_oemstr);
1813	}
1814
1815	if (level <= 0)
1816		return (0);
1817
1818	if (vers >= 0x0200 && bootverbose) {
1819		/* vender name, product name, product revision */
1820		printf("VESA: %s %s %s\n",
1821			(vesa_venderstr != NULL) ? vesa_venderstr : "unknown",
1822			(vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1823			(vesa_revstr != NULL) ? vesa_revstr : "?");
1824	}
1825
1826#if VESA_DEBUG > 1
1827	/* mode information */
1828	for (i = 0;
1829		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1830		&& (vesa_vmodetab[i] != 0xffff); ++i) {
1831		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode, M_NOWAIT))
1832			continue;
1833
1834		/* print something for diagnostic purpose */
1835		printf("VESA: mode:0x%03x, flags:0x%04x",
1836		       vesa_vmodetab[i], vmode.v_modeattr);
1837		if (vmode.v_modeattr & V_MODEOPTINFO) {
1838			if (vmode.v_modeattr & V_MODEGRAPHICS) {
1839				printf(", G %dx%dx%d %d, ",
1840				       vmode.v_width, vmode.v_height,
1841				       vmode.v_bpp, vmode.v_planes);
1842			} else {
1843				printf(", T %dx%d, ",
1844				       vmode.v_width, vmode.v_height);
1845			}
1846			printf("font:%dx%d, ",
1847			       vmode.v_cwidth, vmode.v_cheight);
1848			printf("pages:%d, mem:%d",
1849			       vmode.v_ipages + 1, vmode.v_memmodel);
1850		}
1851		if (vmode.v_modeattr & V_MODELFB) {
1852			printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1853			       vmode.v_lfb, vmode.v_offscreen,
1854			       vmode.v_offscreensize*1024);
1855		}
1856		printf("\n");
1857		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1858		       vmode.v_waseg, vmode.v_waattr,
1859		       vmode.v_wbseg, vmode.v_wbattr);
1860		printf("size:%dk, gran:%dk\n",
1861		       vmode.v_wsize, vmode.v_wgran);
1862	}
1863#endif /* VESA_DEBUG > 1 */
1864
1865	return (0);
1866}
1867
1868/* module loading */
1869
1870static int
1871vesa_load(void)
1872{
1873	int error;
1874
1875	if (vesa_init_done)
1876		return (0);
1877
1878	mtx_init(&vesa_lock, "VESA lock", NULL, MTX_DEF);
1879
1880	/* locate a VGA adapter */
1881	vesa_adp = NULL;
1882	error = vesa_configure(0);
1883
1884	if (error == 0)
1885		vesa_bios_info(bootverbose);
1886
1887	return (error);
1888}
1889
1890static int
1891vesa_unload(void)
1892{
1893	u_char palette[256*3];
1894	int error;
1895
1896	/* if the adapter is currently in a VESA mode, don't unload */
1897	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1898		return (EBUSY);
1899	/*
1900	 * FIXME: if there is at least one vty which is in a VESA mode,
1901	 * we shouldn't be unloading! XXX
1902	 */
1903
1904	if ((error = vesa_unload_ioctl()) == 0) {
1905		if (vesa_adp != NULL) {
1906			if ((vesa_adp->va_flags & V_ADP_DAC8) != 0) {
1907				vesa_bios_save_palette(0, 256, palette, 8);
1908				vesa_bios_set_dac(6);
1909				vesa_adp->va_flags &= ~V_ADP_DAC8;
1910				vesa_bios_load_palette(0, 256, palette, 6);
1911			}
1912			vesa_adp->va_flags &= ~V_ADP_VESA;
1913			vidsw[vesa_adp->va_index] = prevvidsw;
1914		}
1915	}
1916
1917	if (vesa_bios != NULL) {
1918		x86bios_set_intr(0x10, vesa_bios_int10);
1919		x86bios_free(vesa_bios, vesa_bios_size);
1920	}
1921	if (vesa_adp_info != NULL)
1922		free(vesa_adp_info, M_DEVBUF);
1923	if (vesa_oemstr != NULL)
1924		free(vesa_oemstr, M_DEVBUF);
1925	if (vesa_venderstr != NULL)
1926		free(vesa_venderstr, M_DEVBUF);
1927	if (vesa_prodstr != NULL)
1928		free(vesa_prodstr, M_DEVBUF);
1929	if (vesa_revstr != NULL)
1930		free(vesa_revstr, M_DEVBUF);
1931	if (vesa_vmode != &vesa_vmode_empty)
1932		free(vesa_vmode, M_DEVBUF);
1933	if (vesa_palette != NULL)
1934		x86bios_free(vesa_palette,
1935		    VESA_PALETTE_SIZE + vesa_state_buf_size);
1936
1937	mtx_destroy(&vesa_lock);
1938
1939	return (error);
1940}
1941
1942static int
1943vesa_mod_event(module_t mod, int type, void *data)
1944{
1945
1946	switch (type) {
1947	case MOD_LOAD:
1948		return (vesa_load());
1949	case MOD_UNLOAD:
1950		return (vesa_unload());
1951	}
1952	return (EOPNOTSUPP);
1953}
1954
1955static moduledata_t vesa_mod = {
1956	"vesa",
1957	vesa_mod_event,
1958	NULL,
1959};
1960
1961DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1962MODULE_DEPEND(vesa, x86bios, 1, 1, 1);
1963
1964#endif	/* VGA_NO_MODE_CHANGE */
1965