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