Really get the libz fix into libstand as well.
[dragonfly.git] / sys / dev / video / i386 / vesa / vesa.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/i386/isa/vesa.c,v 1.32.2.1 2002/08/13 02:42:33 rwatson Exp $
052b6ffa 27 * $DragonFly: src/sys/dev/video/i386/vesa/vesa.c,v 1.10 2005/06/10 23:25:09 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_vga.h"
5d556382 31#include "opt_vesa.h"
984263bc
MD
32
33#ifndef VGA_NO_MODE_CHANGE
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/malloc.h>
40#include <sys/fbio.h>
052b6ffa 41#include <sys/thread2.h>
984263bc
MD
42
43#include <vm/vm.h>
44#include <vm/vm_extern.h>
45#include <vm/vm_kern.h>
46#include <vm/pmap.h>
47
48#include <machine/md_var.h>
49#include <machine/vm86.h>
50#include <machine/pc/bios.h>
51#include <machine/pc/vesa.h>
52
583d9351
MD
53#include <dev/video/fb/fbreg.h>
54#include <dev/video/fb/vgareg.h>
984263bc
MD
55
56#ifndef __i386__
1f2de5d4 57#include <bus/isa/isareg.h>
984263bc 58#else
583d9351 59#include <bus/isa/i386/isa.h>
984263bc
MD
60#endif
61
5d556382
MD
62#define VESA_VIA_CLE266 "VIA CLE266\r\n"
63
984263bc
MD
64#ifndef VESA_DEBUG
65#define VESA_DEBUG 0
66#endif
67
68/* VESA video adapter state buffer stub */
69struct adp_state {
70 int sig;
71#define V_STATE_SIG 0x61736576
72 u_char regs[1];
73};
74typedef struct adp_state adp_state_t;
75
76/* VESA video adapter */
77static video_adapter_t *vesa_adp = NULL;
78static int vesa_state_buf_size = 0;
79
80/* VESA functions */
81#if 0
82static int vesa_nop(void);
83#endif
84static int vesa_error(void);
85static vi_probe_t vesa_probe;
86static vi_init_t vesa_init;
87static vi_get_info_t vesa_get_info;
88static vi_query_mode_t vesa_query_mode;
89static vi_set_mode_t vesa_set_mode;
90static vi_save_font_t vesa_save_font;
91static vi_load_font_t vesa_load_font;
92static vi_show_font_t vesa_show_font;
93static vi_save_palette_t vesa_save_palette;
94static vi_load_palette_t vesa_load_palette;
95static vi_set_border_t vesa_set_border;
96static vi_save_state_t vesa_save_state;
97static vi_load_state_t vesa_load_state;
98static vi_set_win_org_t vesa_set_origin;
99static vi_read_hw_cursor_t vesa_read_hw_cursor;
100static vi_set_hw_cursor_t vesa_set_hw_cursor;
101static vi_set_hw_cursor_shape_t vesa_set_hw_cursor_shape;
102static vi_blank_display_t vesa_blank_display;
103static vi_mmap_t vesa_mmap;
104static vi_ioctl_t vesa_ioctl;
105static vi_clear_t vesa_clear;
106static vi_fill_rect_t vesa_fill_rect;
107static vi_bitblt_t vesa_bitblt;
108static vi_diag_t vesa_diag;
109static int vesa_bios_info(int level);
110static struct vm86context vesa_vmcontext;
111
112static video_switch_t vesavidsw = {
113 vesa_probe,
114 vesa_init,
115 vesa_get_info,
116 vesa_query_mode,
117 vesa_set_mode,
118 vesa_save_font,
119 vesa_load_font,
120 vesa_show_font,
121 vesa_save_palette,
122 vesa_load_palette,
123 vesa_set_border,
124 vesa_save_state,
125 vesa_load_state,
126 vesa_set_origin,
127 vesa_read_hw_cursor,
128 vesa_set_hw_cursor,
129 vesa_set_hw_cursor_shape,
130 vesa_blank_display,
131 vesa_mmap,
132 vesa_ioctl,
133 vesa_clear,
134 vesa_fill_rect,
135 vesa_bitblt,
136 vesa_error,
137 vesa_error,
138 vesa_diag,
139};
140
141static video_switch_t *prevvidsw;
142
143/* VESA BIOS video modes */
144#define VESA_MAXMODES 64
145#define EOT (-1)
146#define NA (-2)
147
148#define MODE_TABLE_DELTA 8
149
150static int vesa_vmode_max = 0;
151static video_info_t vesa_vmode_empty = { EOT };
152static video_info_t *vesa_vmode = &vesa_vmode_empty;
153
154static int vesa_init_done = FALSE;
155static int has_vesa_bios = FALSE;
156static struct vesa_info *vesa_adp_info = NULL;
2e458ca3 157static uint16_t *vesa_vmodetab = NULL;
984263bc 158static char *vesa_oemstr = NULL;
2e46b2a9 159static char *vesa_vendorstr = NULL;
984263bc
MD
160static char *vesa_prodstr = NULL;
161static char *vesa_revstr = NULL;
162
163/* local macros and functions */
164#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
165
166static int int10_set_mode(int mode);
167static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
168static int vesa_bios_set_mode(int mode);
169static int vesa_bios_get_dac(void);
170static int vesa_bios_set_dac(int bits);
171static int vesa_bios_save_palette(int start, int colors, u_char *palette,
172 int bits);
173static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
174 u_char *b, int bits);
736a05b5 175static int vesa_bios_load_palette(int start, int colors, const u_char *palette,
984263bc
MD
176 int bits);
177#ifdef notyet
178static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
179 u_char *b, int bits);
180#endif
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)
190static int vesa_bios_state_buf_size(void);
191static int vesa_bios_save_restore(int code, void *p, size_t size);
192static int vesa_bios_get_line_length(void);
193static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
194#if 0
195static int vesa_bios_get_start(int *x, int *y);
196#endif
197static int vesa_bios_set_start(int x, int y);
198static int vesa_map_gen_mode_num(int type, int color, int mode);
2e458ca3
SW
199static int vesa_translate_flags(uint16_t vflags);
200static int vesa_translate_mmodel(uint8_t vmodel);
201static void *vesa_fix_ptr(uint32_t p, uint16_t seg, uint16_t off,
984263bc
MD
202 u_char *buf);
203static int vesa_bios_init(void);
204static void vesa_clear_modes(video_info_t *info, int color);
205static vm_offset_t vesa_map_buffer(u_int paddr, size_t size);
206static void vesa_unmap_buffer(vm_offset_t vaddr, size_t size);
207
208#if 0
209static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
210#endif
211
212static void
213dump_buffer(u_char *buf, size_t len)
214{
2e458ca3 215 int i;
984263bc 216
2e458ca3
SW
217 for (i = 0; i < len;) {
218 printf("%02x ", buf[i]);
219 if ((++i % 16) == 0)
220 printf("\n");
221 }
984263bc
MD
222}
223
224/* INT 10 BIOS calls */
225static int
226int10_set_mode(int mode)
227{
228 struct vm86frame vmf;
229
230 bzero(&vmf, sizeof(vmf));
231 vmf.vmf_eax = 0x0000 | mode;
232 vm86_intcall(0x10, &vmf);
233 return 0;
234}
235
236/* VESA BIOS calls */
237static int
238vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
239{
240 struct vm86frame vmf;
241 u_char *buf;
242 int err;
243
244 bzero(&vmf, sizeof(vmf));
245 vmf.vmf_eax = 0x4f01;
246 vmf.vmf_ecx = mode;
247 buf = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
248 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
249
250 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
251 if ((err != 0) || (vmf.vmf_ax != 0x4f))
252 return 1;
253 bcopy(buf, vmode, sizeof(*vmode));
254 return 0;
255}
256
257static int
258vesa_bios_set_mode(int mode)
259{
260 struct vm86frame vmf;
261 int err;
262
263 bzero(&vmf, sizeof(vmf));
264 vmf.vmf_eax = 0x4f02;
265 vmf.vmf_ebx = mode;
266 err = vm86_intcall(0x10, &vmf);
267 return ((err != 0) || (vmf.vmf_ax != 0x4f));
268}
269
270static int
271vesa_bios_get_dac(void)
272{
273 struct vm86frame vmf;
274 int err;
275
276 bzero(&vmf, sizeof(vmf));
277 vmf.vmf_eax = 0x4f08;
278 vmf.vmf_ebx = 1; /* get DAC width */
279 err = vm86_intcall(0x10, &vmf);
280 if ((err != 0) || (vmf.vmf_ax != 0x4f))
281 return 6; /* XXX */
282 return ((vmf.vmf_ebx >> 8) & 0x00ff);
283}
284
285static int
286vesa_bios_set_dac(int bits)
287{
288 struct vm86frame vmf;
289 int err;
290
291 bzero(&vmf, sizeof(vmf));
292 vmf.vmf_eax = 0x4f08;
293 vmf.vmf_ebx = (bits << 8);
294 err = vm86_intcall(0x10, &vmf);
295 if ((err != 0) || (vmf.vmf_ax != 0x4f))
296 return 6; /* XXX */
297 return ((vmf.vmf_ebx >> 8) & 0x00ff);
298}
299
300static int
301vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
302{
303 struct vm86frame vmf;
304 u_char *p;
305 int err;
306 int i;
307
308 bzero(&vmf, sizeof(vmf));
309 vmf.vmf_eax = 0x4f09;
310 vmf.vmf_ebx = 1; /* get primary palette data */
311 vmf.vmf_ecx = colors;
312 vmf.vmf_edx = start;
313 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
314 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
315
316 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
317 if ((err != 0) || (vmf.vmf_ax != 0x4f))
318 return 1;
319
320 bits = 8 - bits;
321 for (i = 0; i < colors; ++i) {
322 palette[i*3] = p[i*4 + 2] << bits;
323 palette[i*3 + 1] = p[i*4 + 1] << bits;
324 palette[i*3 + 2] = p[i*4] << bits;
325 }
326 return 0;
327}
328
329static int
330vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
331 int bits)
332{
333 struct vm86frame vmf;
334 u_char *p;
335 int err;
336 int i;
337
338 bzero(&vmf, sizeof(vmf));
339 vmf.vmf_eax = 0x4f09;
340 vmf.vmf_ebx = 1; /* get primary palette data */
341 vmf.vmf_ecx = colors;
342 vmf.vmf_edx = start;
343 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
344 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
345
346 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
347 if ((err != 0) || (vmf.vmf_ax != 0x4f))
348 return 1;
349
350 bits = 8 - bits;
351 for (i = 0; i < colors; ++i) {
352 r[i] = p[i*4 + 2] << bits;
353 g[i] = p[i*4 + 1] << bits;
354 b[i] = p[i*4] << bits;
355 }
356 return 0;
357}
358
359static int
736a05b5 360vesa_bios_load_palette(int start, int colors, const u_char *palette, int bits)
984263bc
MD
361{
362 struct vm86frame vmf;
363 u_char *p;
364 int err;
365 int i;
366
367 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
368 bits = 8 - bits;
369 for (i = 0; i < colors; ++i) {
370 p[i*4] = palette[i*3 + 2] >> bits;
371 p[i*4 + 1] = palette[i*3 + 1] >> bits;
372 p[i*4 + 2] = palette[i*3] >> bits;
373 p[i*4 + 3] = 0;
374 }
375
376 bzero(&vmf, sizeof(vmf));
377 vmf.vmf_eax = 0x4f09;
378 vmf.vmf_ebx = 0; /* set primary palette data */
379 vmf.vmf_ecx = colors;
380 vmf.vmf_edx = start;
381 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
382
383 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
384 return ((err != 0) || (vmf.vmf_ax != 0x4f));
385}
386
387#ifdef notyet
388static int
389vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
390 int bits)
391{
392 struct vm86frame vmf;
393 u_char *p;
394 int err;
395 int i;
396
397 p = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
398 bits = 8 - bits;
399 for (i = 0; i < colors; ++i) {
400 p[i*4] = b[i] >> bits;
401 p[i*4 + 1] = g[i] >> bits;
402 p[i*4 + 2] = r[i] >> bits;
403 p[i*4 + 3] = 0;
404 }
405
406 bzero(&vmf, sizeof(vmf));
407 vmf.vmf_eax = 0x4f09;
408 vmf.vmf_ebx = 0; /* set primary palette data */
409 vmf.vmf_ecx = colors;
410 vmf.vmf_edx = start;
411 vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
412
413 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
414 return ((err != 0) || (vmf.vmf_ax != 0x4f));
415}
416#endif
417
418static int
419vesa_bios_state_buf_size(void)
420{
421 struct vm86frame vmf;
422 int err;
423
424 bzero(&vmf, sizeof(vmf));
425 vmf.vmf_eax = 0x4f04;
426 vmf.vmf_ecx = STATE_MOST;
427 vmf.vmf_edx = STATE_SIZE;
428 err = vm86_intcall(0x10, &vmf);
429 if ((err != 0) || (vmf.vmf_ax != 0x4f))
430 return 0;
431 return vmf.vmf_bx*64;
432}
433
434static int
435vesa_bios_save_restore(int code, void *p, size_t size)
436{
437 struct vm86frame vmf;
438 u_char *buf;
439 int err;
440
441 bzero(&vmf, sizeof(vmf));
442 vmf.vmf_eax = 0x4f04;
443 vmf.vmf_ecx = STATE_MOST;
444 vmf.vmf_edx = code; /* STATE_SAVE/STATE_LOAD */
445 buf = (u_char *)vm86_getpage(&vesa_vmcontext, 1);
446 vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
447 bcopy(p, buf, size);
448
449 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
450 return ((err != 0) || (vmf.vmf_ax != 0x4f));
451}
452
453static int
454vesa_bios_get_line_length(void)
455{
456 struct vm86frame vmf;
457 int err;
458
459 bzero(&vmf, sizeof(vmf));
460 vmf.vmf_eax = 0x4f06;
461 vmf.vmf_ebx = 1; /* get scan line length */
462 err = vm86_intcall(0x10, &vmf);
463 if ((err != 0) || (vmf.vmf_ax != 0x4f))
464 return -1;
465 return vmf.vmf_bx; /* line length in bytes */
466}
467
468static int
469vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
470{
471 struct vm86frame vmf;
472 int err;
473
474 bzero(&vmf, sizeof(vmf));
475 vmf.vmf_eax = 0x4f06;
476 vmf.vmf_ebx = 0; /* set scan line length in pixel */
477 vmf.vmf_ecx = pixel;
478 err = vm86_intcall(0x10, &vmf);
479#if VESA_DEBUG > 1
480 printf("bx:%d, cx:%d, dx:%d\n", vmf.vmf_bx, vmf.vmf_cx, vmf.vmf_dx);
481#endif
482 if ((err != 0) || (vmf.vmf_ax != 0x4f))
483 return 1;
484 if (bytes)
485 *bytes = vmf.vmf_bx;
486 if (lines)
487 *lines = vmf.vmf_dx;
488 return 0;
489}
490
491#if 0
492static int
493vesa_bios_get_start(int *x, int *y)
494{
495 struct vm86frame vmf;
496 int err;
497
498 bzero(&vmf, sizeof(vmf));
499 vmf.vmf_eax = 0x4f07;
500 vmf.vmf_ebx = 1; /* get display start */
501 err = vm86_intcall(0x10, &vmf);
502 if ((err != 0) || (vmf.vmf_ax != 0x4f))
503 return 1;
504 *x = vmf.vmf_cx;
505 *y = vmf.vmf_dx;
506 return 0;
507}
508#endif
509
510static int
511vesa_bios_set_start(int x, int y)
512{
513 struct vm86frame vmf;
514 int err;
515
516 bzero(&vmf, sizeof(vmf));
517 vmf.vmf_eax = 0x4f07;
518 vmf.vmf_ebx = 0x80; /* set display start */
519 vmf.vmf_edx = y;
520 vmf.vmf_ecx = x;
521 err = vm86_intcall(0x10, &vmf);
522 return ((err != 0) || (vmf.vmf_ax != 0x4f));
523}
524
525/* map a generic video mode to a known mode */
526static int
527vesa_map_gen_mode_num(int type, int color, int mode)
528{
2e458ca3
SW
529 static struct {
530 int from;
531 int to;
532 } mode_map[] = {
533 { M_TEXT_132x25, M_VESA_C132x25 },
534 { M_TEXT_132x43, M_VESA_C132x43 },
535 { M_TEXT_132x50, M_VESA_C132x50 },
536 { M_TEXT_132x60, M_VESA_C132x60 },
537 };
538 int i;
539
540 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
541 if (mode_map[i].from == mode)
542 return mode_map[i].to;
543 }
544 return mode;
984263bc
MD
545}
546
547static int
2e458ca3 548vesa_translate_flags(uint16_t vflags)
984263bc
MD
549{
550 static struct {
2e458ca3 551 uint16_t mask;
984263bc
MD
552 int set;
553 int reset;
554 } ftable[] = {
555 { V_MODECOLOR, V_INFO_COLOR, 0 },
556 { V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
557 { V_MODELFB, V_INFO_LINEAR, 0 },
558 };
559 int flags;
560 int i;
561
562 for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
563 flags |= (vflags & ftable[i].mask) ?
564 ftable[i].set : ftable[i].reset;
565 }
566 return flags;
567}
568
569static int
2e458ca3 570vesa_translate_mmodel(uint8_t vmodel)
984263bc
MD
571{
572 static struct {
2e458ca3 573 uint8_t vmodel;
984263bc
MD
574 int mmodel;
575 } mtable[] = {
576 { V_MMTEXT, V_INFO_MM_TEXT },
577 { V_MMCGA, V_INFO_MM_CGA },
578 { V_MMHGC, V_INFO_MM_HGC },
579 { V_MMEGA, V_INFO_MM_PLANAR },
580 { V_MMPACKED, V_INFO_MM_PACKED },
581 { V_MMDIRCOLOR, V_INFO_MM_DIRECT },
582 };
583 int i;
584
585 for (i = 0; mtable[i].mmodel >= 0; ++i) {
586 if (mtable[i].vmodel == vmodel)
587 return mtable[i].mmodel;
588 }
589 return V_INFO_MM_OTHER;
590}
591
2e458ca3
SW
592static void *
593vesa_fix_ptr(uint32_t p, uint16_t seg, uint16_t off, u_char *buf)
984263bc
MD
594{
595 if (p == 0)
596 return NULL;
597 if (((p >> 16) == seg) && ((p & 0xffff) >= off))
598 return (void *)(buf + ((p & 0xffff) - off));
599 else {
600 p = BIOS_SADDRTOLADDR(p);
601 return (void *)BIOS_PADDRTOVADDR(p);
602 }
603}
604
605static int
606vesa_bios_init(void)
607{
608 static u_char buf[512];
609 struct vm86frame vmf;
610 struct vesa_mode vmode;
611 video_info_t *p;
612 u_char *vmbuf;
5d556382 613 int is_via_cle266;
984263bc
MD
614 int modes;
615 int err;
616 int i;
617
618 if (vesa_init_done)
619 return 0;
620
621 has_vesa_bios = FALSE;
622 vesa_adp_info = NULL;
623 vesa_vmode_max = 0;
624 vesa_vmode[0].vi_mode = EOT;
625
626 vmbuf = (u_char *)vm86_addpage(&vesa_vmcontext, 1, 0);
627 bzero(&vmf, sizeof(vmf)); /* paranoia */
628 bcopy("VBE2", vmbuf, 4); /* try for VBE2 data */
629 vmf.vmf_eax = 0x4f00;
630 vm86_getptr(&vesa_vmcontext, (vm_offset_t)vmbuf, &vmf.vmf_es, &vmf.vmf_di);
631
632 err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
633 if ((err != 0) || (vmf.vmf_ax != 0x4f) || bcmp("VESA", vmbuf, 4))
634 return 1;
635 bcopy(vmbuf, buf, sizeof(buf));
636 vesa_adp_info = (struct vesa_info *)buf;
637 if (bootverbose) {
638 printf("VESA: information block\n");
639 dump_buffer(buf, 64);
640 }
641 if (vesa_adp_info->v_flags & V_NONVGA)
642 return 1;
643 if (vesa_adp_info->v_version < 0x0102) {
644 printf("VESA: VBE version %d.%d is not supported; "
645 "version 1.2 or later is required.\n",
646 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
647 + ((vesa_adp_info->v_version & 0x0f00) >> 8),
648 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
649 + (vesa_adp_info->v_version & 0x000f));
650 return 1;
651 }
652
653 /* fix string ptrs */
654 vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr,
655 vmf.vmf_es, vmf.vmf_di, buf);
5d556382
MD
656 is_via_cle266 = strcmp(vesa_oemstr, VESA_VIA_CLE266) == 0;
657
984263bc 658 if (vesa_adp_info->v_version >= 0x0200) {
2e46b2a9
SW
659 vesa_vendorstr =
660 (char *)vesa_fix_ptr(vesa_adp_info->v_vendorstr,
984263bc
MD
661 vmf.vmf_es, vmf.vmf_di, buf);
662 vesa_prodstr =
663 (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr,
664 vmf.vmf_es, vmf.vmf_di, buf);
665 vesa_revstr =
666 (char *)vesa_fix_ptr(vesa_adp_info->v_revstr,
667 vmf.vmf_es, vmf.vmf_di, buf);
668 }
669
670 /* obtain video mode information */
2e458ca3 671 vesa_vmodetab = (uint16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable,
984263bc
MD
672 vmf.vmf_es, vmf.vmf_di, buf);
673 if (vesa_vmodetab == NULL)
674 return 1;
675 for (i = 0, modes = 0;
676 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
677 && (vesa_vmodetab[i] != 0xffff); ++i) {
678 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
679 continue;
680
681 /* reject unsupported modes */
682#if 0
683 if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
684 | V_MODENONVGA))
685 != (V_MODESUPP | V_MODEOPTINFO))
686 continue;
687#else
5d556382
MD
688 if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
689#if VESA_DEBUG > 1
690 printf("Rejecting VESA %s mode: %d x %d x %d bpp attr = %x\n",
691 vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
692 vmode.v_width, vmode.v_height, vmode.v_bpp,
693 vmode.v_modeattr);
694#endif
984263bc 695 continue;
5d556382 696 }
984263bc
MD
697#endif
698
699 /* expand the array if necessary */
700 if (modes >= vesa_vmode_max) {
701 vesa_vmode_max += MODE_TABLE_DELTA;
702 p = malloc(sizeof(*vesa_vmode)*(vesa_vmode_max + 1),
703 M_DEVBUF, M_WAITOK);
704#if VESA_DEBUG > 1
705 printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
706 modes, vesa_vmode_max);
707#endif
708 if (modes > 0) {
709 bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
710 free(vesa_vmode, M_DEVBUF);
711 }
712 vesa_vmode = p;
713 }
714
5d556382
MD
715#if VESA_DEBUG > 1
716 printf("Found VESA %s mode: %d x %d x %d bpp\n",
717 vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
718 vmode.v_width, vmode.v_height, vmode.v_bpp);
719#endif
720 if (is_via_cle266) {
2e458ca3
SW
721 if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
722 vmode.v_width &= 0xff;
723 vmode.v_waseg = 0xb8000 >> 4;
724 }
5d556382
MD
725 }
726
984263bc
MD
727 /* copy some fields */
728 bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
729 vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
730 vesa_vmode[modes].vi_width = vmode.v_width;
731 vesa_vmode[modes].vi_height = vmode.v_height;
732 vesa_vmode[modes].vi_depth = vmode.v_bpp;
733 vesa_vmode[modes].vi_planes = vmode.v_planes;
734 vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
735 vesa_vmode[modes].vi_cheight = vmode.v_cheight;
736 vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
737 /* XXX window B */
738 vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
739 vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
740 if (vmode.v_modeattr & V_MODELFB)
741 vesa_vmode[modes].vi_buffer = vmode.v_lfb;
742 else
743 vesa_vmode[modes].vi_buffer = 0;
744 /* XXX */
745 vesa_vmode[modes].vi_buffer_size
746 = vesa_adp_info->v_memsize*64*1024;
747#if 0
748 if (vmode.v_offscreen > vmode.v_lfb)
749 vesa_vmode[modes].vi_buffer_size
750 = vmode.v_offscreen + vmode.v_offscreensize*1024
751 - vmode.v_lfb;
752 else
753 vesa_vmode[modes].vi_buffer_size
754 = vmode.v_offscreen + vmode.v_offscreensize*1024
755#endif
756 vesa_vmode[modes].vi_mem_model
757 = vesa_translate_mmodel(vmode.v_memmodel);
758 vesa_vmode[modes].vi_pixel_fields[0] = 0;
759 vesa_vmode[modes].vi_pixel_fields[1] = 0;
760 vesa_vmode[modes].vi_pixel_fields[2] = 0;
761 vesa_vmode[modes].vi_pixel_fields[3] = 0;
762 vesa_vmode[modes].vi_pixel_fsizes[0] = 0;
763 vesa_vmode[modes].vi_pixel_fsizes[1] = 0;
764 vesa_vmode[modes].vi_pixel_fsizes[2] = 0;
765 vesa_vmode[modes].vi_pixel_fsizes[3] = 0;
766 if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_PACKED) {
767 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
768 } else if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_DIRECT) {
769 vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
770 vesa_vmode[modes].vi_pixel_fields[0]
771 = vmode.v_redfieldpos;
772 vesa_vmode[modes].vi_pixel_fields[1]
773 = vmode.v_greenfieldpos;
774 vesa_vmode[modes].vi_pixel_fields[2]
775 = vmode.v_bluefieldpos;
776 vesa_vmode[modes].vi_pixel_fields[3]
777 = vmode.v_resfieldpos;
778 vesa_vmode[modes].vi_pixel_fsizes[0]
779 = vmode.v_redmasksize;
780 vesa_vmode[modes].vi_pixel_fsizes[1]
781 = vmode.v_greenmasksize;
782 vesa_vmode[modes].vi_pixel_fsizes[2]
783 = vmode.v_bluemasksize;
784 vesa_vmode[modes].vi_pixel_fsizes[3]
785 = vmode.v_resmasksize;
786 } else {
787 vesa_vmode[modes].vi_pixel_size = 0;
788 }
789
790 vesa_vmode[modes].vi_flags
791 = vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
792 ++modes;
793 }
794 vesa_vmode[modes].vi_mode = EOT;
795 if (bootverbose)
796 printf("VESA: %d mode(s) found\n", modes);
797
798 has_vesa_bios = (modes > 0);
799 return (has_vesa_bios ? 0 : 1);
800}
801
802static void
803vesa_clear_modes(video_info_t *info, int color)
804{
805 while (info->vi_mode != EOT) {
806 if ((info->vi_flags & V_INFO_COLOR) != color)
807 info->vi_mode = NA;
808 ++info;
809 }
810}
811
812static vm_offset_t
813vesa_map_buffer(u_int paddr, size_t size)
814{
815 vm_offset_t vaddr;
816 u_int off;
817
818 off = paddr - trunc_page(paddr);
819 vaddr = (vm_offset_t)pmap_mapdev(paddr - off, size + off);
820#if VESA_DEBUG > 1
821 printf("vesa_map_buffer: paddr:%x vaddr:%x size:%x off:%x\n",
822 paddr, vaddr, size, off);
823#endif
824 return (vaddr + off);
825}
826
827static void
828vesa_unmap_buffer(vm_offset_t vaddr, size_t size)
829{
830#if VESA_DEBUG > 1
831 printf("vesa_unmap_buffer: vaddr:%x size:%x\n", vaddr, size);
832#endif
833 kmem_free(kernel_map, vaddr, size);
834}
835
836/* entry points */
837
838static int
839vesa_configure(int flags)
840{
841 video_adapter_t *adp;
842 int adapters;
843 int error;
844 int i;
845
846 if (vesa_init_done)
847 return 0;
848 if (flags & VIO_PROBE_ONLY)
849 return 0; /* XXX */
850
851 /*
852 * If the VESA module has already been loaded, abort loading
853 * the module this time.
854 */
855 for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
856 if (adp->va_flags & V_ADP_VESA)
857 return ENXIO;
858 if (adp->va_type == KD_VGA)
859 break;
860 }
861 /*
862 * The VGA adapter is not found. This is because either
863 * 1) the VGA driver has not been initialized, or 2) the VGA card
864 * is not present. If 1) is the case, we shall defer
865 * initialization for now and try again later.
866 */
867 if (adp == NULL) {
868 vga_sub_configure = vesa_configure;
869 return ENODEV;
870 }
871
872 /* count number of registered adapters */
873 for (++i; vid_get_adapter(i) != NULL; ++i)
874 ;
875 adapters = i;
876
877 /* call VESA BIOS */
878 vesa_adp = adp;
879 if (vesa_bios_init()) {
880 vesa_adp = NULL;
881 return ENXIO;
882 }
883 vesa_adp->va_flags |= V_ADP_VESA;
884
885 /* remove conflicting modes if we have more than one adapter */
886 if (adapters > 1) {
887 vesa_clear_modes(vesa_vmode,
888 (vesa_adp->va_flags & V_ADP_COLOR) ?
889 V_INFO_COLOR : 0);
890 }
891
892 if ((error = vesa_load_ioctl()) == 0) {
893 prevvidsw = vidsw[vesa_adp->va_index];
894 vidsw[vesa_adp->va_index] = &vesavidsw;
895 vesa_init_done = TRUE;
896 } else {
897 vesa_adp = NULL;
898 return error;
899 }
900
901 return 0;
902}
903
904#if 0
905static int
906vesa_nop(void)
907{
908 return 0;
909}
910#endif
911
912static int
913vesa_error(void)
914{
915 return 1;
916}
917
918static int
919vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
920{
921 return (*prevvidsw->probe)(unit, adpp, arg, flags);
922}
923
924static int
925vesa_init(int unit, video_adapter_t *adp, int flags)
926{
927 return (*prevvidsw->init)(unit, adp, flags);
928}
929
930static int
931vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
932{
933 int i;
934
935 if ((*prevvidsw->get_info)(adp, mode, info) == 0)
936 return 0;
937
938 if (adp != vesa_adp)
939 return 1;
940
941 mode = vesa_map_gen_mode_num(vesa_adp->va_type,
942 vesa_adp->va_flags & V_ADP_COLOR, mode);
943 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
944 if (vesa_vmode[i].vi_mode == NA)
945 continue;
946 if (vesa_vmode[i].vi_mode == mode) {
947 *info = vesa_vmode[i];
948 return 0;
949 }
950 }
951 return 1;
952}
953
954static int
955vesa_query_mode(video_adapter_t *adp, video_info_t *info)
956{
957 int i;
958
959 if ((*prevvidsw->query_mode)(adp, info) == 0)
960 return 0;
961 if (adp != vesa_adp)
962 return ENODEV;
963
964 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
965 if ((info->vi_width != 0)
966 && (info->vi_width != vesa_vmode[i].vi_width))
967 continue;
968 if ((info->vi_height != 0)
969 && (info->vi_height != vesa_vmode[i].vi_height))
970 continue;
971 if ((info->vi_cwidth != 0)
972 && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
973 continue;
974 if ((info->vi_cheight != 0)
975 && (info->vi_cheight != vesa_vmode[i].vi_cheight))
976 continue;
977 if ((info->vi_depth != 0)
978 && (info->vi_depth != vesa_vmode[i].vi_depth))
979 continue;
980 if ((info->vi_planes != 0)
981 && (info->vi_planes != vesa_vmode[i].vi_planes))
982 continue;
983 /* pixel format, memory model */
984 if ((info->vi_flags != 0)
985 && (info->vi_flags != vesa_vmode[i].vi_flags))
986 continue;
987 *info = vesa_vmode[i];
988 return 0;
989 }
990 return ENODEV;
991}
992
993static int
994vesa_set_mode(video_adapter_t *adp, int mode)
995{
996 video_info_t info;
997 int len;
998
999 if (adp != vesa_adp)
1000 return (*prevvidsw->set_mode)(adp, mode);
1001
1002 mode = vesa_map_gen_mode_num(adp->va_type,
1003 adp->va_flags & V_ADP_COLOR, mode);
1004#if VESA_DEBUG > 0
1005 printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1006 adp->va_mode, adp->va_mode, mode, mode);
1007#endif
1008 /*
1009 * If the current mode is a VESA mode and the new mode is not,
1010 * restore the state of the adapter first by setting one of the
1011 * standard VGA mode, so that non-standard, extended SVGA registers
1012 * are set to the state compatible with the standard VGA modes.
1013 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1014 * the new mode correctly.
1015 */
1016 if (VESA_MODE(adp->va_mode)) {
1017 if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
1018 int10_set_mode(adp->va_initial_bios_mode);
1019 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1020 vesa_unmap_buffer(adp->va_buffer,
1021 vesa_adp_info->v_memsize*64*1024);
1022 /*
1023 * Once (*prevvidsw->get_info)() succeeded,
1024 * (*prevvidsw->set_mode)() below won't fail...
1025 */
1026 }
1027 }
1028
1029 /* we may not need to handle this mode after all... */
1030 if ((*prevvidsw->set_mode)(adp, mode) == 0)
1031 return 0;
1032
1033 /* is the new mode supported? */
1034 if (vesa_get_info(adp, mode, &info))
1035 return 1;
1036 /* assert(VESA_MODE(mode)); */
1037
1038#if VESA_DEBUG > 0
1039 printf("VESA: about to set a VESA mode...\n");
1040#endif
1041 /* don't use the linear frame buffer for text modes. XXX */
1042 if (!(info.vi_flags & V_INFO_GRAPHICS))
1043 info.vi_flags &= ~V_INFO_LINEAR;
1044
1045 if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
1046 return 1;
1047
1048 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1049 vesa_unmap_buffer(adp->va_buffer,
1050 vesa_adp_info->v_memsize*64*1024);
1051
1052#if VESA_DEBUG > 0
1053 printf("VESA: mode set!\n");
1054#endif
1055 vesa_adp->va_mode = mode;
1056 vesa_adp->va_flags &= ~V_ADP_COLOR;
1057 vesa_adp->va_flags |=
1058 (info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1059 vesa_adp->va_crtc_addr =
1060 (vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1061 if (info.vi_flags & V_INFO_LINEAR) {
1062#if VESA_DEBUG > 1
1063 printf("VESA: setting up LFB\n");
1064#endif
1065 vesa_adp->va_buffer =
1066 vesa_map_buffer(info.vi_buffer,
1067 vesa_adp_info->v_memsize*64*1024);
1068 vesa_adp->va_buffer_size = info.vi_buffer_size;
1069 vesa_adp->va_window = vesa_adp->va_buffer;
1070 vesa_adp->va_window_size = info.vi_buffer_size/info.vi_planes;
1071 vesa_adp->va_window_gran = info.vi_buffer_size/info.vi_planes;
1072 } else {
1073 vesa_adp->va_buffer = 0;
1074 vesa_adp->va_buffer_size = info.vi_buffer_size;
1075 vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
1076 vesa_adp->va_window_size = info.vi_window_size;
1077 vesa_adp->va_window_gran = info.vi_window_gran;
1078 }
1079 vesa_adp->va_window_orig = 0;
1080 len = vesa_bios_get_line_length();
1081 if (len > 0) {
1082 vesa_adp->va_line_width = len;
1083 } else if (info.vi_flags & V_INFO_GRAPHICS) {
1084 switch (info.vi_depth/info.vi_planes) {
1085 case 1:
1086 vesa_adp->va_line_width = info.vi_width/8;
1087 break;
1088 case 2:
1089 vesa_adp->va_line_width = info.vi_width/4;
1090 break;
1091 case 4:
1092 vesa_adp->va_line_width = info.vi_width/2;
1093 break;
1094 case 8:
1095 default: /* shouldn't happen */
1096 vesa_adp->va_line_width = info.vi_width;
1097 break;
1098 }
1099 } else {
1100 vesa_adp->va_line_width = info.vi_width;
1101 }
1102 vesa_adp->va_disp_start.x = 0;
1103 vesa_adp->va_disp_start.y = 0;
1104#if VESA_DEBUG > 0
1105 printf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n",
1106 info.vi_width, len, vesa_adp->va_line_width);
1107#endif
1108 bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1109
1110 /* move hardware cursor out of the way */
1111 (*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1112
1113 return 0;
1114}
1115
1116static int
1117vesa_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1118 int ch, int count)
1119{
1120 return (*prevvidsw->save_font)(adp, page, fontsize, data, ch, count);
1121}
1122
1123static int
1124vesa_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1125 int ch, int count)
1126{
1127 return (*prevvidsw->load_font)(adp, page, fontsize, data, ch, count);
1128}
1129
1130static int
1131vesa_show_font(video_adapter_t *adp, int page)
1132{
1133 return (*prevvidsw->show_font)(adp, page);
1134}
1135
1136static int
1137vesa_save_palette(video_adapter_t *adp, u_char *palette)
1138{
1139 int bits;
1140 int error;
1141
1142 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1143 && VESA_MODE(adp->va_mode)) {
1144 bits = vesa_bios_get_dac();
1145 error = vesa_bios_save_palette(0, 256, palette, bits);
1146 if (error == 0)
1147 return 0;
1148 if (bits != 6)
1149 return error;
1150 }
1151
1152 return (*prevvidsw->save_palette)(adp, palette);
1153}
1154
1155static int
736a05b5 1156vesa_load_palette(video_adapter_t *adp, const u_char *palette)
984263bc
MD
1157{
1158#if notyet
1159 int bits;
1160 int error;
1161
1162 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1163 && VESA_MODE(adp->va_mode) && ((bits = vesa_bios_set_dac(8)) > 6)) {
1164 error = vesa_bios_load_palette(0, 256, palette, bits);
1165 if (error == 0)
1166 return 0;
1167 if (vesa_bios_set_dac(6) != 6)
1168 return 1;
1169 }
1170#endif /* notyet */
1171
1172 return (*prevvidsw->load_palette)(adp, palette);
1173}
1174
1175static int
1176vesa_set_border(video_adapter_t *adp, int color)
1177{
1178 return (*prevvidsw->set_border)(adp, color);
1179}
1180
1181static int
1182vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1183{
1184 if (adp != vesa_adp)
1185 return (*prevvidsw->save_state)(adp, p, size);
1186
1187 if (vesa_state_buf_size == 0)
1188 vesa_state_buf_size = vesa_bios_state_buf_size();
1189 if (size == 0)
1190 return (sizeof(int) + vesa_state_buf_size);
1191 else if (size < (sizeof(int) + vesa_state_buf_size))
1192 return 1;
1193
1194 ((adp_state_t *)p)->sig = V_STATE_SIG;
1195 bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1196 return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
1197 vesa_state_buf_size);
1198}
1199
1200static int
1201vesa_load_state(video_adapter_t *adp, void *p)
1202{
1203 if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1204 return (*prevvidsw->load_state)(adp, p);
1205
1206 return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
1207 vesa_state_buf_size);
1208}
1209
1210#if 0
1211static int
1212vesa_get_origin(video_adapter_t *adp, off_t *offset)
1213{
1214 struct vm86frame vmf;
1215 int err;
1216
1217 bzero(&vmf, sizeof(vmf));
1218 vmf.vmf_eax = 0x4f05;
1219 vmf.vmf_ebx = 0x10; /* WINDOW_A, XXX */
1220 err = vm86_intcall(0x10, &vmf);
1221 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1222 return 1;
1223 *offset = vmf.vmf_dx*adp->va_window_gran;
1224 return 0;
1225}
1226#endif
1227
1228static int
1229vesa_set_origin(video_adapter_t *adp, off_t offset)
1230{
1231 struct vm86frame vmf;
1232 int err;
1233
1234 /*
1235 * This function should return as quickly as possible to
1236 * maintain good performance of the system. For this reason,
1237 * error checking is kept minimal and let the VESA BIOS to
1238 * detect error.
1239 */
1240 if (adp != vesa_adp)
1241 return (*prevvidsw->set_win_org)(adp, offset);
1242
1243 /* if this is a linear frame buffer, do nothing */
1244 if (adp->va_info.vi_flags & V_INFO_LINEAR)
1245 return 0;
1246 /* XXX */
1247 if (adp->va_window_gran == 0)
1248 return 1;
1249
1250 bzero(&vmf, sizeof(vmf));
1251 vmf.vmf_eax = 0x4f05;
1252 vmf.vmf_ebx = 0; /* WINDOW_A, XXX */
1253 vmf.vmf_edx = offset/adp->va_window_gran;
1254 err = vm86_intcall(0x10, &vmf);
1255 if ((err != 0) || (vmf.vmf_ax != 0x4f))
1256 return 1;
1257 bzero(&vmf, sizeof(vmf));
1258 vmf.vmf_eax = 0x4f05;
1259 vmf.vmf_ebx = 1; /* WINDOW_B, XXX */
1260 vmf.vmf_edx = offset/adp->va_window_gran;
1261 err = vm86_intcall(0x10, &vmf);
1262 adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1263 return 0; /* XXX */
1264}
1265
1266static int
1267vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1268{
1269 return (*prevvidsw->read_hw_cursor)(adp, col, row);
1270}
1271
1272static int
1273vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1274{
1275 return (*prevvidsw->set_hw_cursor)(adp, col, row);
1276}
1277
1278static int
1279vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1280 int celsize, int blink)
1281{
1282 return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1283 blink);
1284}
1285
1286static int
1287vesa_blank_display(video_adapter_t *adp, int mode)
1288{
1289 /* XXX: use VESA DPMS */
1290 return (*prevvidsw->blank_display)(adp, mode);
1291}
1292
1293static int
1294vesa_mmap(video_adapter_t *adp, vm_offset_t offset, int prot)
1295{
1296#if VESA_DEBUG > 0
1297 printf("vesa_mmap(): window:0x%x, buffer:0x%x, offset:0x%x\n",
1298 adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1299#endif
1300
1301 if ((adp == vesa_adp) && (adp->va_info.vi_flags & V_INFO_LINEAR)) {
1302 /* va_window_size == va_buffer_size/vi_planes */
1303 /* XXX: is this correct? */
1304 if (offset > adp->va_window_size - PAGE_SIZE)
1305 return -1;
1306#ifdef __i386__
1307 return i386_btop(adp->va_info.vi_buffer + offset);
984263bc
MD
1308#endif
1309 } else {
1310 return (*prevvidsw->mmap)(adp, offset, prot);
1311 }
1312}
1313
1314static int
1315vesa_clear(video_adapter_t *adp)
1316{
1317 return (*prevvidsw->clear)(adp);
1318}
1319
1320static int
1321vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1322{
1323 return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
1324}
1325
1326static int
1327vesa_bitblt(video_adapter_t *adp,...)
1328{
1329 /* FIXME */
1330 return 1;
1331}
1332
1333static int
1334get_palette(video_adapter_t *adp, int base, int count,
1335 u_char *red, u_char *green, u_char *blue, u_char *trans)
1336{
1337 u_char *r;
1338 u_char *g;
1339 u_char *b;
1340 int bits;
1341 int error;
1342
1343 if ((base < 0) || (base >= 256) || (count < 0) || (count > 256))
1344 return 1;
5d556382 1345 if ((base + count) > 256)
984263bc
MD
1346 return 1;
1347 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode))
1348 return 1;
1349
1350 bits = vesa_bios_get_dac();
1351 if (bits <= 6)
1352 return 1;
1353
1354 r = malloc(count*3, M_DEVBUF, M_WAITOK);
1355 g = r + count;
1356 b = g + count;
1357 error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1358 if (error == 0) {
1359 copyout(r, red, count);
1360 copyout(g, green, count);
1361 copyout(b, blue, count);
1362 if (trans != NULL) {
1363 bzero(r, count);
1364 copyout(r, trans, count);
1365 }
1366 }
1367 free(r, M_DEVBUF);
1368
1369 /* if error && bits != 6 at this point, we are in in trouble... XXX */
1370 return error;
1371}
1372
1373static int
1374set_palette(video_adapter_t *adp, int base, int count,
1375 u_char *red, u_char *green, u_char *blue, u_char *trans)
1376{
1377 return 1;
1378#if notyet
1379 u_char *r;
1380 u_char *g;
1381 u_char *b;
1382 int bits;
1383 int error;
1384
1385 if ((base < 0) || (base >= 256) || (base + count > 256))
1386 return 1;
1387 if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode)
1388 || ((bits = vesa_bios_set_dac(8)) <= 6))
1389 return 1;
1390
1391 r = malloc(count*3, M_DEVBUF, M_WAITOK);
1392 g = r + count;
1393 b = g + count;
1394 copyin(red, r, count);
1395 copyin(green, g, count);
1396 copyin(blue, b, count);
1397
1398 error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1399 free(r, M_DEVBUF);
1400 if (error == 0)
1401 return 0;
1402
1403 /* if the following call fails, we are in trouble... XXX */
1404 vesa_bios_set_dac(6);
1405 return 1;
1406#endif /* notyet */
1407}
1408
1409static int
1410vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1411{
1412 int bytes;
1413
1414 if (adp != vesa_adp)
1415 return (*prevvidsw->ioctl)(adp, cmd, arg);
1416
1417 switch (cmd) {
1418 case FBIO_SETWINORG: /* set frame buffer window origin */
1419 if (!VESA_MODE(adp->va_mode))
1420 return (*prevvidsw->ioctl)(adp, cmd, arg);
1421 return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1422
1423 case FBIO_SETDISPSTART: /* set display start address */
1424 if (!VESA_MODE(adp->va_mode))
1425 return (*prevvidsw->ioctl)(adp, cmd, arg);
1426 if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1427 ((video_display_start_t *)arg)->y))
1428 return ENODEV;
1429 adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1430 adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1431 return 0;
1432
1433 case FBIO_SETLINEWIDTH: /* set line length in pixel */
1434 if (!VESA_MODE(adp->va_mode))
1435 return (*prevvidsw->ioctl)(adp, cmd, arg);
1436 if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1437 return ENODEV;
1438 adp->va_line_width = bytes;
1439#if VESA_DEBUG > 1
1440 printf("new line width:%d\n", adp->va_line_width);
1441#endif
1442 return 0;
1443
1444 case FBIO_GETPALETTE: /* get color palette */
1445 if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1446 ((video_color_palette_t *)arg)->count,
1447 ((video_color_palette_t *)arg)->red,
1448 ((video_color_palette_t *)arg)->green,
1449 ((video_color_palette_t *)arg)->blue,
1450 ((video_color_palette_t *)arg)->transparent))
1451 return (*prevvidsw->ioctl)(adp, cmd, arg);
1452 return 0;
1453
1454
1455 case FBIO_SETPALETTE: /* set color palette */
1456 if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1457 ((video_color_palette_t *)arg)->count,
1458 ((video_color_palette_t *)arg)->red,
1459 ((video_color_palette_t *)arg)->green,
1460 ((video_color_palette_t *)arg)->blue,
1461 ((video_color_palette_t *)arg)->transparent))
1462 return (*prevvidsw->ioctl)(adp, cmd, arg);
1463 return 0;
1464
1465 case FBIOGETCMAP: /* get color palette */
1466 if (get_palette(adp, ((struct fbcmap *)arg)->index,
1467 ((struct fbcmap *)arg)->count,
1468 ((struct fbcmap *)arg)->red,
1469 ((struct fbcmap *)arg)->green,
1470 ((struct fbcmap *)arg)->blue, NULL))
1471 return (*prevvidsw->ioctl)(adp, cmd, arg);
1472 return 0;
1473
1474 case FBIOPUTCMAP: /* set color palette */
1475 if (set_palette(adp, ((struct fbcmap *)arg)->index,
1476 ((struct fbcmap *)arg)->count,
1477 ((struct fbcmap *)arg)->red,
1478 ((struct fbcmap *)arg)->green,
1479 ((struct fbcmap *)arg)->blue, NULL))
1480 return (*prevvidsw->ioctl)(adp, cmd, arg);
1481 return 0;
1482
1483 default:
1484 return (*prevvidsw->ioctl)(adp, cmd, arg);
1485 }
1486}
1487
1488static int
1489vesa_diag(video_adapter_t *adp, int level)
1490{
1491 int error;
1492
1493 /* call the previous handler first */
1494 error = (*prevvidsw->diag)(adp, level);
1495 if (error)
1496 return error;
1497
1498 if (adp != vesa_adp)
1499 return 1;
1500
1501 if (level <= 0)
1502 return 0;
1503
1504 return 0;
1505}
1506
1507static int
1508vesa_bios_info(int level)
1509{
1510#if VESA_DEBUG > 1
1511 struct vesa_mode vmode;
1512 int i;
1513#endif
1514
5d556382
MD
1515 if (bootverbose) {
1516 /* general adapter information */
1517 printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1518 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10 +
1519 ((vesa_adp_info->v_version & 0x0f00) >> 8),
1520 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10 +
1521 (vesa_adp_info->v_version & 0x000f),
1522 vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1523 vesa_vmodetab, vesa_adp_info->v_modetable);
1524
1525 /* OEM string */
1526 if (vesa_oemstr != NULL)
1527 printf("VESA: %s\n", vesa_oemstr);
1528 }
984263bc
MD
1529
1530 if (level <= 0)
1531 return 0;
1532
5d556382 1533 if (vesa_adp_info->v_version >= 0x0200 && bootverbose) {
2e46b2a9 1534 /* vendor name, product name, product revision */
984263bc 1535 printf("VESA: %s %s %s\n",
2e46b2a9 1536 (vesa_vendorstr != NULL) ? vesa_vendorstr : "unknown",
984263bc
MD
1537 (vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1538 (vesa_revstr != NULL) ? vesa_revstr : "?");
1539 }
1540
1541#if VESA_DEBUG > 1
1542 /* mode information */
1543 for (i = 0;
1544 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1545 && (vesa_vmodetab[i] != 0xffff); ++i) {
1546 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
1547 continue;
1548
1549 /* print something for diagnostic purpose */
1550 printf("VESA: mode:0x%03x, flags:0x%04x",
1551 vesa_vmodetab[i], vmode.v_modeattr);
1552 if (vmode.v_modeattr & V_MODEOPTINFO) {
1553 if (vmode.v_modeattr & V_MODEGRAPHICS) {
1554 printf(", G %dx%dx%d %d, ",
1555 vmode.v_width, vmode.v_height,
1556 vmode.v_bpp, vmode.v_planes);
1557 } else {
1558 printf(", T %dx%d, ",
1559 vmode.v_width, vmode.v_height);
1560 }
1561 printf("font:%dx%d, ",
1562 vmode.v_cwidth, vmode.v_cheight);
1563 printf("pages:%d, mem:%d",
1564 vmode.v_ipages + 1, vmode.v_memmodel);
1565 }
1566 if (vmode.v_modeattr & V_MODELFB) {
1567 printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1568 vmode.v_lfb, vmode.v_offscreen,
1569 vmode.v_offscreensize*1024);
1570 }
1571 printf("\n");
1572 printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1573 vmode.v_waseg, vmode.v_waattr,
1574 vmode.v_wbseg, vmode.v_wbattr);
1575 printf("size:%dk, gran:%dk\n",
1576 vmode.v_wsize, vmode.v_wgran);
1577 }
1578#endif /* VESA_DEBUG > 1 */
1579
1580 return 0;
1581}
1582
1583/* module loading */
1584
1585static int
1586vesa_load(void)
1587{
1588 int error;
984263bc
MD
1589
1590 if (vesa_init_done)
1591 return 0;
1592
1593 /* locate a VGA adapter */
052b6ffa 1594 crit_enter();
984263bc
MD
1595 vesa_adp = NULL;
1596 error = vesa_configure(0);
052b6ffa 1597 crit_exit();
984263bc
MD
1598
1599 if (error == 0)
1600 vesa_bios_info(bootverbose);
1601
1602 return error;
1603}
1604
1605static int
1606vesa_unload(void)
1607{
1608 u_char palette[256*3];
1609 int error;
1610 int bits;
984263bc
MD
1611
1612 /* if the adapter is currently in a VESA mode, don't unload */
1613 if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1614 return EBUSY;
1615 /*
1616 * FIXME: if there is at least one vty which is in a VESA mode,
1617 * we shouldn't be unloading! XXX
1618 */
1619
052b6ffa 1620 crit_enter();
984263bc
MD
1621 if ((error = vesa_unload_ioctl()) == 0) {
1622 if (vesa_adp != NULL) {
1623 if (vesa_adp_info->v_flags & V_DAC8) {
1624 bits = vesa_bios_get_dac();
1625 if (bits > 6) {
1626 vesa_bios_save_palette(0, 256,
1627 palette, bits);
1628 vesa_bios_set_dac(6);
1629 vesa_bios_load_palette(0, 256,
1630 palette, 6);
1631 }
1632 }
1633 vesa_adp->va_flags &= ~V_ADP_VESA;
1634 vidsw[vesa_adp->va_index] = prevvidsw;
1635 }
1636 }
052b6ffa 1637 crit_exit();
984263bc
MD
1638
1639 return error;
1640}
1641
1642static int
1643vesa_mod_event(module_t mod, int type, void *data)
1644{
1645 switch (type) {
1646 case MOD_LOAD:
1647 return vesa_load();
1648 case MOD_UNLOAD:
1649 return vesa_unload();
1650 default:
1651 break;
1652 }
1653 return 0;
1654}
1655
1656static moduledata_t vesa_mod = {
1657 "vesa",
1658 vesa_mod_event,
1659 NULL,
1660};
1661
1662DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1663
1664#endif /* VGA_NO_MODE_CHANGE */