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