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