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