vesa.c revision 43674
1133362Sobrien/*- 2234449Sobrien * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith 3133362Sobrien * All rights reserved. 4330569Sgordon * 5268515Sdelphij * Redistribution and use in source and binary forms, with or without 6268515Sdelphij * modification, are permitted provided that the following conditions 7133362Sobrien * are met: 8133362Sobrien * 1. Redistributions of source code must retain the above copyright 9195767Skensmith * notice, this list of conditions and the following disclaimer as 10152285Sru * the first lines of this file unmodified. 11152285Sru * 2. Redistributions in binary form must reproduce the above copyright 12133362Sobrien * notice, this list of conditions and the following disclaimer in the 13133362Sobrien * documentation and/or other materials provided with the distribution. 14191771Sobrien * 15330569Sgordon * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16191771Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17137887Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18133362Sobrien * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 19133370Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20133362Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21159769Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22330569Sgordon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23133362Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24208342Smarius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25201381Sed * 26186693Sobrien * $Id: vesa.c,v 1.15 1999/01/17 14:12:48 yokota Exp $ 27133362Sobrien */ 28186693Sobrien 29133362Sobrien#include "vga.h" 30133362Sobrien#include "opt_vga.h" 31268515Sdelphij#include "opt_vesa.h" 32268515Sdelphij#include "opt_vm86.h" 33268515Sdelphij#include "opt_fb.h" 34133362Sobrien 35133362Sobrien#ifdef VGA_NO_MODE_CHANGE 36265747Sdelphij#undef VESA 37133362Sobrien#endif 38133362Sobrien 39133362Sobrien#if (NVGA > 0 && defined(VESA) && defined(VM86)) || defined(KLD_MODULE) 40133362Sobrien 41133362Sobrien#include <sys/param.h> 42133362Sobrien#include <sys/systm.h> 43330569Sgordon#include <sys/kernel.h> 44330569Sgordon#include <sys/module.h> 45240377Sobrien#include <sys/malloc.h> 46133362Sobrien#include <vm/vm.h> 47136681Sru#include <vm/pmap.h> 48136681Sru 49133362Sobrien#include <machine/console.h> 50133362Sobrien#include <machine/md_var.h> 51133362Sobrien#include <machine/vm86.h> 52133362Sobrien#include <machine/pc/bios.h> 53133362Sobrien#include <machine/pc/vesa.h> 54133362Sobrien 55133362Sobrien#include <dev/fb/fbreg.h> 56133362Sobrien#include <dev/fb/vgareg.h> 57330569Sgordon 58330569Sgordon#ifndef __i386__ 59330569Sgordon#include <isa/isareg.h> 60330569Sgordon#else 61330569Sgordon#include <i386/isa/isa.h> 62330569Sgordon#endif 63133362Sobrien 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#if 0 80static void *vesa_state_buf = NULL; 81#endif 82 83/* VESA functions */ 84static int vesa_nop(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_mmap_t vesa_mmap; 103static vi_diag_t vesa_diag; 104 105static video_switch_t vesavidsw = { 106 vesa_probe, 107 vesa_init, 108 vesa_get_info, 109 vesa_query_mode, 110 vesa_set_mode, 111 vesa_save_font, 112 vesa_load_font, 113 vesa_show_font, 114 vesa_save_palette, 115 vesa_load_palette, 116 vesa_set_border, 117 vesa_save_state, 118 vesa_load_state, 119 vesa_set_origin, 120 vesa_read_hw_cursor, 121 vesa_set_hw_cursor, 122 vesa_set_hw_cursor_shape, 123 (vi_blank_display_t *)vesa_nop, 124 vesa_mmap, 125 vesa_diag, 126}; 127 128static video_switch_t *prevvidsw; 129 130/* VESA BIOS video modes */ 131#define VESA_MAXMODES 64 132#define EOT (-1) 133#define NA (-2) 134 135static video_info_t vesa_vmode[VESA_MAXMODES + 1] = { 136 { EOT, }, 137}; 138 139static int vesa_init_done = FALSE; 140static int has_vesa_bios = FALSE; 141static struct vesa_info *vesa_adp_info = NULL; 142static u_int16_t *vesa_vmodetab = NULL; 143static char *vesa_oemstr = NULL; 144static char *vesa_venderstr = NULL; 145static char *vesa_prodstr = NULL; 146static char *vesa_revstr = NULL; 147 148/* local macros and functions */ 149#define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff)) 150 151static int int10_set_mode(int mode); 152static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode); 153static int vesa_bios_set_mode(int mode); 154static int vesa_bios_set_dac(int bits); 155static int vesa_bios_save_palette(int start, int colors, u_char *palette, 156 int bits); 157static int vesa_bios_load_palette(int start, int colors, u_char *palette, 158 int bits); 159#define STATE_SIZE 0 160#define STATE_SAVE 1 161#define STATE_LOAD 2 162#define STATE_HW (1<<0) 163#define STATE_DATA (1<<1) 164#define STATE_DAC (1<<2) 165#define STATE_REG (1<<3) 166#define STATE_MOST (STATE_HW | STATE_DATA | STATE_REG) 167#define STATE_ALL (STATE_HW | STATE_DATA | STATE_DAC | STATE_REG) 168static int vesa_bios_state_buf_size(void); 169static int vesa_bios_save_restore(int code, void *p, size_t size); 170static int vesa_bios_get_line_length(void); 171static int vesa_map_gen_mode_num(int type, int color, int mode); 172static int vesa_translate_flags(u_int16_t vflags); 173static void *vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off, 174 u_char *buf); 175static int vesa_bios_init(void); 176static void vesa_clear_modes(video_info_t *info, int color); 177 178static void 179dump_buffer(u_char *buf, size_t len) 180{ 181 int i; 182 183 for(i = 0; i < len;) { 184 printf("%02x ", buf[i]); 185 if ((++i % 16) == 0) 186 printf("\n"); 187 } 188} 189 190/* INT 10 BIOS calls */ 191static int 192int10_set_mode(int mode) 193{ 194 struct vm86frame vmf; 195 196 bzero(&vmf, sizeof(vmf)); 197 vmf.vmf_eax = 0x0000 | mode; 198 vm86_intcall(0x10, &vmf); 199 return 0; 200} 201 202/* VESA BIOS calls */ 203static int 204vesa_bios_get_mode(int mode, struct vesa_mode *vmode) 205{ 206 struct vm86frame vmf; 207 u_char buf[256]; 208 int err; 209 210 bzero(&vmf, sizeof(vmf)); 211 bzero(buf, sizeof(buf)); 212 vmf.vmf_eax = 0x4f01; 213 vmf.vmf_ecx = mode; 214 err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf), 215 &vmf.vmf_es, &vmf.vmf_di); 216 if ((err != 0) || (vmf.vmf_eax != 0x4f)) 217 return 1; 218 bcopy(buf, vmode, sizeof(*vmode)); 219 return 0; 220} 221 222static int 223vesa_bios_set_mode(int mode) 224{ 225 struct vm86frame vmf; 226 int err; 227 228 bzero(&vmf, sizeof(vmf)); 229 vmf.vmf_eax = 0x4f02; 230 vmf.vmf_ebx = mode; 231 err = vm86_intcall(0x10, &vmf); 232 return ((err != 0) || (vmf.vmf_eax != 0x4f)); 233} 234 235static int 236vesa_bios_set_dac(int bits) 237{ 238 struct vm86frame vmf; 239 int err; 240 241 bzero(&vmf, sizeof(vmf)); 242 vmf.vmf_eax = 0x4f08; 243 vmf.vmf_ebx = (bits << 8); 244 err = vm86_intcall(0x10, &vmf); 245 if ((err != 0) || (vmf.vmf_eax != 0x4f)) 246 return 6; /* XXX */ 247 return ((vmf.vmf_ebx >> 8) & 0x00ff); 248} 249 250static int 251vesa_bios_save_palette(int start, int colors, u_char *palette, int bits) 252{ 253 struct vm86frame vmf; 254 u_char *p; 255 int err; 256 int i; 257 258 p = malloc(colors*4, M_DEVBUF, M_WAITOK); 259 260 bzero(&vmf, sizeof(vmf)); 261 vmf.vmf_eax = 0x4f09; 262 vmf.vmf_ebx = 1; /* get primary palette data */ 263 vmf.vmf_ecx = colors; 264 vmf.vmf_edx = start; 265 err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di); 266 if ((err != 0) || (vmf.vmf_eax != 0x4f)) { 267 free(p, M_DEVBUF); 268 return 1; 269 } 270 271 bits = 8 - bits; 272 for (i = 0; i < colors; ++i) { 273 palette[i*3] = p[i*4 + 2] << bits; 274 palette[i*3 + 1] = p[i*4 + 1] << bits; 275 palette[i*3 + 2] = p[i*4] << bits; 276 } 277 free(p, M_DEVBUF); 278 return 0; 279} 280 281static int 282vesa_bios_load_palette(int start, int colors, u_char *palette, int bits) 283{ 284 struct vm86frame vmf; 285 u_char *p; 286 int err; 287 int i; 288 289 p = malloc(colors*4, M_DEVBUF, M_WAITOK); 290 bits = 8 - bits; 291 for (i = 0; i < colors; ++i) { 292 p[i*4] = palette[i*3 + 2] >> bits; 293 p[i*4 + 1] = palette[i*3 + 1] >> bits; 294 p[i*4 + 2] = palette[i*3] >> bits; 295 p[i*4 + 3] = 0; 296 } 297 298 bzero(&vmf, sizeof(vmf)); 299 vmf.vmf_eax = 0x4f09; 300 vmf.vmf_ebx = 0; /* set primary palette data */ 301 vmf.vmf_ecx = colors; 302 vmf.vmf_edx = start; 303 err = vm86_datacall(0x10, &vmf, p, colors*4, &vmf.vmf_es, &vmf.vmf_di); 304 free(p, M_DEVBUF); 305 return ((err != 0) || (vmf.vmf_eax != 0x4f)); 306} 307 308static int 309vesa_bios_state_buf_size(void) 310{ 311 struct vm86frame vmf; 312 int err; 313 314 bzero(&vmf, sizeof(vmf)); 315 vmf.vmf_eax = 0x4f04; 316 vmf.vmf_ecx = STATE_MOST; 317 vmf.vmf_edx = STATE_SIZE; 318 err = vm86_intcall(0x10, &vmf); 319 if ((err != 0) || (vmf.vmf_eax != 0x4f)) 320 return 0; 321 return vmf.vmf_ebx*64; 322} 323 324static int 325vesa_bios_save_restore(int code, void *p, size_t size) 326{ 327 struct vm86frame vmf; 328 int err; 329 330 bzero(&vmf, sizeof(vmf)); 331 vmf.vmf_eax = 0x4f04; 332 vmf.vmf_ecx = STATE_MOST; 333 vmf.vmf_edx = code; /* STATE_SAVE/STATE_LOAD */ 334 err = vm86_datacall(0x10, &vmf, (char *)p, size, 335 &vmf.vmf_es, &vmf.vmf_bx); 336 return ((err != 0) || (vmf.vmf_eax != 0x4f)); 337} 338 339static int 340vesa_bios_get_line_length(void) 341{ 342 struct vm86frame vmf; 343 int err; 344 345 bzero(&vmf, sizeof(vmf)); 346 vmf.vmf_eax = 0x4f06; 347 vmf.vmf_ebx = 1; /* get scan line length */ 348 err = vm86_intcall(0x10, &vmf); 349 if ((err != 0) || (vmf.vmf_eax != 0x4f)) 350 return -1; 351 return vmf.vmf_bx; /* line length in bytes */ 352} 353 354/* map a generic video mode to a known mode */ 355static int 356vesa_map_gen_mode_num(int type, int color, int mode) 357{ 358 static struct { 359 int from; 360 int to; 361 } mode_map[] = { 362 { M_TEXT_132x25, M_VESA_C132x25 }, 363 { M_TEXT_132x43, M_VESA_C132x43 }, 364 { M_TEXT_132x50, M_VESA_C132x50 }, 365 { M_TEXT_132x60, M_VESA_C132x60 }, 366 }; 367 int i; 368 369 for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) { 370 if (mode_map[i].from == mode) 371 return mode_map[i].to; 372 } 373 return mode; 374} 375 376static int 377vesa_translate_flags(u_int16_t vflags) 378{ 379 static struct { 380 u_int16_t mask; 381 int set; 382 int reset; 383 } ftable[] = { 384 { V_MODECOLOR, V_INFO_COLOR, 0 }, 385 { V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 }, 386 { V_MODELFB, V_INFO_LINEAR, 0 }, 387 }; 388 int flags; 389 int i; 390 391 for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) { 392 flags |= (vflags & ftable[i].mask) ? 393 ftable[i].set : ftable[i].reset; 394 } 395 return flags; 396} 397 398static void 399*vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off, u_char *buf) 400{ 401 if (p == 0) 402 return NULL; 403 if (((p >> 16) == seg) && ((p & 0xffff) >= off)) 404 return (void *)(buf + ((p & 0xffff) - off)); 405 else { 406 p = BIOS_SADDRTOLADDR(p); 407 return (void *)BIOS_PADDRTOVADDR(p); 408 } 409} 410 411static int 412vesa_bios_init(void) 413{ 414 static u_char buf[512]; 415 struct vm86frame vmf; 416 struct vesa_mode vmode; 417 int modes; 418 int err; 419 int i; 420 421 if (vesa_init_done) 422 return 0; 423 424 has_vesa_bios = FALSE; 425 vesa_adp_info = NULL; 426 vesa_vmode[0].vi_mode = EOT; 427 428 bzero(&vmf, sizeof(vmf)); /* paranoia */ 429 bzero(buf, sizeof(buf)); 430 bcopy("VBE2", buf, 4); /* try for VBE2 data */ 431 vmf.vmf_eax = 0x4f00; 432 err = vm86_datacall(0x10, &vmf, (char *)buf, sizeof(buf), 433 &vmf.vmf_es, &vmf.vmf_di); 434 if ((err != 0) || (vmf.vmf_eax != 0x4f) || bcmp("VESA", buf, 4)) 435 return 1; 436 vesa_adp_info = (struct vesa_info *)buf; 437 if (bootverbose) { 438 printf("VESA: information block\n"); 439 dump_buffer(buf, 64); 440 } 441 if (vesa_adp_info->v_flags & V_NONVGA) 442 return 1; 443 444 /* fix string ptrs */ 445 vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr, 446 vmf.vmf_es, vmf.vmf_di, buf); 447 if (vesa_adp_info->v_version >= 0x0200) { 448 vesa_venderstr = 449 (char *)vesa_fix_ptr(vesa_adp_info->v_venderstr, 450 vmf.vmf_es, vmf.vmf_di, buf); 451 vesa_prodstr = 452 (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr, 453 vmf.vmf_es, vmf.vmf_di, buf); 454 vesa_revstr = 455 (char *)vesa_fix_ptr(vesa_adp_info->v_revstr, 456 vmf.vmf_es, vmf.vmf_di, buf); 457 } 458 459 /* obtain video mode information */ 460 vesa_vmode[0].vi_mode = EOT; 461 vesa_vmodetab = (u_int16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable, 462 vmf.vmf_es, vmf.vmf_di, buf); 463 if (vesa_vmodetab == NULL) 464 return 1; 465 for (i = 0, modes = 0; 466 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) 467 && (vesa_vmodetab[i] != 0xffff); ++i) { 468 if (modes >= VESA_MAXMODES) 469 break; 470 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode)) 471 continue; 472 473 /* reject unsupported modes */ 474#if 0 475 if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO 476 | V_MODENONVGA)) 477 != (V_MODESUPP | V_MODEOPTINFO)) 478 continue; 479#else 480 if ((vmode.v_modeattr & (V_MODEOPTINFO | V_MODENONVGA)) 481 != (V_MODEOPTINFO)) 482 continue; 483#endif 484 485 /* copy some fields */ 486 bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes])); 487 vesa_vmode[modes].vi_mode = vesa_vmodetab[i]; 488 vesa_vmode[modes].vi_width = vmode.v_width; 489 vesa_vmode[modes].vi_height = vmode.v_height; 490 vesa_vmode[modes].vi_depth = vmode.v_bpp; 491 vesa_vmode[modes].vi_planes = vmode.v_planes; 492 vesa_vmode[modes].vi_cwidth = vmode.v_cwidth; 493 vesa_vmode[modes].vi_cheight = vmode.v_cheight; 494 vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4; 495 /* XXX window B */ 496 vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024; 497 vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024; 498 vesa_vmode[modes].vi_buffer = vmode.v_lfb; 499 /* XXX */ 500 if (vmode.v_offscreen > vmode.v_lfb) 501 vesa_vmode[modes].vi_buffer_size 502 = vmode.v_offscreen - vmode.v_lfb; 503 else 504 vesa_vmode[modes].vi_buffer_size = vmode.v_offscreen; 505 /* pixel format, memory model... */ 506 507 vesa_vmode[modes].vi_flags 508 = vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA; 509 ++modes; 510 } 511 vesa_vmode[modes].vi_mode = EOT; 512 if (bootverbose) 513 printf("VESA: %d mode(s) found\n", modes); 514 515 has_vesa_bios = (modes > 0); 516 return (has_vesa_bios ? 0 : 1); 517} 518 519static void 520vesa_clear_modes(video_info_t *info, int color) 521{ 522 while (info->vi_mode != EOT) { 523 if ((info->vi_flags & V_INFO_COLOR) != color) 524 info->vi_mode = NA; 525 ++info; 526 } 527} 528 529/* entry points */ 530 531static int 532vesa_configure(int flags) 533{ 534 video_adapter_t *adp; 535 int adapters; 536 int error; 537 int i; 538 539 if (vesa_init_done) 540 return 0; 541 if (flags & VIO_PROBE_ONLY) 542 return 0; /* XXX */ 543 544 /* 545 * If the VESA module has already been loaded, abort loading 546 * the module this time. 547 */ 548 for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) { 549 if (adp->va_flags & V_ADP_VESA) 550 return ENXIO; 551 if (adp->va_type == KD_VGA) 552 break; 553 } 554 /* 555 * The VGA adapter is not found. This is because either 556 * 1) the VGA driver has not been initialized, or 2) the VGA card 557 * is not present. If 1) is the case, we shall defer 558 * initialization for now and try again later. 559 */ 560 if (adp == NULL) { 561 vga_sub_configure = vesa_configure; 562 return ENODEV; 563 } 564 565 /* count number of registered adapters */ 566 for (++i; vid_get_adapter(i) != NULL; ++i) 567 ; 568 adapters = i; 569 570 /* call VESA BIOS */ 571 vesa_adp = adp; 572 if (vesa_bios_init()) { 573 vesa_adp = NULL; 574 return ENXIO; 575 } 576 vesa_adp->va_flags |= V_ADP_VESA; 577 578 /* remove conflicting modes if we have more than one adapter */ 579 if (adapters > 1) { 580 vesa_clear_modes(vesa_vmode, 581 (vesa_adp->va_flags & V_ADP_COLOR) ? 582 V_INFO_COLOR : 0); 583 } 584 585 if ((error = vesa_load_ioctl()) == 0) { 586 prevvidsw = vidsw[vesa_adp->va_index]; 587 vidsw[vesa_adp->va_index] = &vesavidsw; 588 vesa_init_done = TRUE; 589 } else { 590 vesa_adp = NULL; 591 return error; 592 } 593 594 return 0; 595} 596 597static int 598vesa_nop(void) 599{ 600 return 0; 601} 602 603static int 604vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags) 605{ 606 return (*prevvidsw->probe)(unit, adpp, arg, flags); 607} 608 609static int 610vesa_init(int unit, video_adapter_t *adp, int flags) 611{ 612 return (*prevvidsw->init)(unit, adp, flags); 613} 614 615static int 616vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info) 617{ 618 int i; 619 620 if ((*prevvidsw->get_info)(adp, mode, info) == 0) 621 return 0; 622 623 if (adp != vesa_adp) 624 return 1; 625 626 mode = vesa_map_gen_mode_num(vesa_adp->va_type, 627 vesa_adp->va_flags & V_ADP_COLOR, mode); 628 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) { 629 if (vesa_vmode[i].vi_mode == NA) 630 continue; 631 if (vesa_vmode[i].vi_mode == mode) { 632 *info = vesa_vmode[i]; 633 return 0; 634 } 635 } 636 return 1; 637} 638 639static int 640vesa_query_mode(video_adapter_t *adp, video_info_t *info) 641{ 642 int i; 643 644 if ((i = (*prevvidsw->query_mode)(adp, info)) != -1) 645 return i; 646 if (adp != vesa_adp) 647 return -1; 648 649 for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) { 650 if ((info->vi_width != 0) 651 && (info->vi_width != vesa_vmode[i].vi_width)) 652 continue; 653 if ((info->vi_height != 0) 654 && (info->vi_height != vesa_vmode[i].vi_height)) 655 continue; 656 if ((info->vi_cwidth != 0) 657 && (info->vi_cwidth != vesa_vmode[i].vi_cwidth)) 658 continue; 659 if ((info->vi_cheight != 0) 660 && (info->vi_cheight != vesa_vmode[i].vi_cheight)) 661 continue; 662 if ((info->vi_depth != 0) 663 && (info->vi_depth != vesa_vmode[i].vi_depth)) 664 continue; 665 if ((info->vi_planes != 0) 666 && (info->vi_planes != vesa_vmode[i].vi_planes)) 667 continue; 668 /* pixel format, memory model */ 669 if ((info->vi_flags != 0) 670 && (info->vi_flags != vesa_vmode[i].vi_flags)) 671 continue; 672 return vesa_vmode[i].vi_mode; 673 } 674 return -1; 675} 676 677static int 678vesa_set_mode(video_adapter_t *adp, int mode) 679{ 680 video_info_t info; 681 size_t len; 682 683 if (adp != vesa_adp) 684 return (*prevvidsw->set_mode)(adp, mode); 685 686 mode = vesa_map_gen_mode_num(vesa_adp->va_type, 687 vesa_adp->va_flags & V_ADP_COLOR, mode); 688#if VESA_DEBUG > 0 689 printf("VESA: set_mode(): %d(%x) -> %d(%x)\n", 690 vesa_adp->va_mode, vesa_adp->va_mode, mode, mode); 691#endif 692 /* 693 * If the current mode is a VESA mode and the new mode is not, 694 * restore the state of the adapter first, so that non-standard, 695 * extended SVGA registers are set to the state compatible with 696 * the standard VGA modes. Otherwise (*prevvidsw->set_mode)() 697 * may not be able to set up the new mode correctly. 698 */ 699 if (VESA_MODE(vesa_adp->va_mode)) { 700 if ((*prevvidsw->get_info)(adp, mode, &info) == 0) { 701 int10_set_mode(vesa_adp->va_initial_bios_mode); 702#if 0 703 /* assert(vesa_state_buf != NULL); */ 704 if ((vesa_state_buf == NULL) 705 || vesa_load_state(adp, vesa_state_buf)) 706 return 1; 707 free(vesa_state_buf, M_DEVBUF); 708 vesa_state_buf = NULL; 709#if VESA_DEBUG > 0 710 printf("VESA: restored\n"); 711#endif 712#endif /* 0 */ 713 } 714 /* 715 * once (*prevvidsw->get_info)() succeeded, 716 * (*prevvidsw->set_mode)() below won't fail... 717 */ 718 } 719 720 /* we may not need to handle this mode after all... */ 721 if ((*prevvidsw->set_mode)(adp, mode) == 0) 722 return 0; 723 724 /* is the new mode supported? */ 725 if (vesa_get_info(adp, mode, &info)) 726 return 1; 727 /* assert(VESA_MODE(mode)); */ 728 729#if VESA_DEBUG > 0 730 printf("VESA: about to set a VESA mode...\n"); 731#endif 732 /* 733 * If the current mode is not a VESA mode, save the current state 734 * so that the adapter state can be restored later when a non-VESA 735 * mode is to be set up. See above. 736 */ 737#if 0 738 if (!VESA_MODE(vesa_adp->va_mode) && (vesa_state_buf == NULL)) { 739 len = vesa_save_state(adp, NULL, 0); 740 vesa_state_buf = malloc(len, M_DEVBUF, M_WAITOK); 741 if (vesa_save_state(adp, vesa_state_buf, len)) { 742#if VESA_DEBUG > 0 743 printf("VESA: state save failed! (len=%d)\n", len); 744#endif 745 free(vesa_state_buf, M_DEVBUF); 746 vesa_state_buf = NULL; 747 return 1; 748 } 749#if VESA_DEBUG > 0 750 printf("VESA: saved (len=%d)\n", len); 751 dump_buffer(vesa_state_buf, len); 752#endif 753 } 754#endif /* 0 */ 755 756 if (vesa_bios_set_mode(mode)) 757 return 1; 758 759#if VESA_DEBUG > 0 760 printf("VESA: mode set!\n"); 761#endif 762 vesa_adp->va_mode = mode; 763 vesa_adp->va_flags &= ~V_ADP_COLOR; 764 vesa_adp->va_flags |= 765 (info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0; 766 vesa_adp->va_crtc_addr = 767 (vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC; 768 vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window); 769 vesa_adp->va_window_size = info.vi_window_size; 770 vesa_adp->va_window_gran = info.vi_window_gran; 771 if (info.vi_buffer_size == 0) { 772 vesa_adp->va_buffer = 0; 773 vesa_adp->va_buffer_size = 0; 774 } else { 775 vesa_adp->va_buffer = BIOS_PADDRTOVADDR(info.vi_buffer); 776 vesa_adp->va_buffer_size = info.vi_buffer_size; 777 } 778 len = vesa_bios_get_line_length(); 779 if (len > 0) { 780 vesa_adp->va_line_width = len; 781 } else if (info.vi_flags & V_INFO_GRAPHICS) { 782 switch (info.vi_depth/info.vi_planes) { 783 case 1: 784 vesa_adp->va_line_width = info.vi_width/8; 785 break; 786 case 2: 787 vesa_adp->va_line_width = info.vi_width/4; 788 break; 789 case 4: 790 vesa_adp->va_line_width = info.vi_width/2; 791 break; 792 case 8: 793 default: /* shouldn't happen */ 794 vesa_adp->va_line_width = info.vi_width; 795 break; 796 } 797 } else { 798 vesa_adp->va_line_width = info.vi_width; 799 } 800#if VESA_DEBUG > 0 801 printf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n", 802 info.vi_width, len, vesa_adp->va_line_width); 803#endif 804 bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info)); 805 806 /* move hardware cursor out of the way */ 807 (*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1); 808 809 return 0; 810} 811 812static int 813vesa_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data, 814 int ch, int count) 815{ 816 return (*prevvidsw->save_font)(adp, page, fontsize, data, ch, count); 817} 818 819static int 820vesa_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data, 821 int ch, int count) 822{ 823 return (*prevvidsw->load_font)(adp, page, fontsize, data, ch, count); 824} 825 826static int 827vesa_show_font(video_adapter_t *adp, int page) 828{ 829 return (*prevvidsw->show_font)(adp, page); 830} 831 832static int 833vesa_save_palette(video_adapter_t *adp, u_char *palette) 834{ 835 int bits; 836 int error; 837 838 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8) 839 && ((bits = vesa_bios_set_dac(8)) > 6)) { 840 error = vesa_bios_save_palette(0, 256, palette, bits); 841 if (error == 0) 842 return 0; 843 vesa_bios_set_dac(6); 844 } 845 846 return (*prevvidsw->save_palette)(adp, palette); 847} 848 849static int 850vesa_load_palette(video_adapter_t *adp, u_char *palette) 851{ 852 int bits; 853 int error; 854 855 if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8) 856 && ((bits = vesa_bios_set_dac(8)) > 6)) { 857 error = vesa_bios_load_palette(0, 256, palette, bits); 858 if (error == 0) 859 return 0; 860 vesa_bios_set_dac(6); 861 } 862 863 return (*prevvidsw->load_palette)(adp, palette); 864} 865 866static int 867vesa_set_border(video_adapter_t *adp, int color) 868{ 869 return (*prevvidsw->set_border)(adp, color); 870} 871 872static int 873vesa_save_state(video_adapter_t *adp, void *p, size_t size) 874{ 875 if (adp != vesa_adp) 876 return (*prevvidsw->save_state)(adp, p, size); 877 878 if (vesa_state_buf_size == 0) 879 vesa_state_buf_size = vesa_bios_state_buf_size(); 880 if (size == 0) 881 return (sizeof(int) + vesa_state_buf_size); 882 else if (size < (sizeof(int) + vesa_state_buf_size)) 883 return 1; 884 885 ((adp_state_t *)p)->sig = V_STATE_SIG; 886 bzero(((adp_state_t *)p)->regs, vesa_state_buf_size); 887 return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs, 888 vesa_state_buf_size); 889} 890 891static int 892vesa_load_state(video_adapter_t *adp, void *p) 893{ 894 if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG)) 895 return (*prevvidsw->load_state)(adp, p); 896 897 return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs, 898 vesa_state_buf_size); 899} 900 901static int 902vesa_set_origin(video_adapter_t *adp, off_t offset) 903{ 904 struct vm86frame vmf; 905 int err; 906 907 /* 908 * This function should return as quickly as possible to 909 * maintain good performance of the system. For this reason, 910 * error checking is kept minimal and let the VESA BIOS to 911 * detect error. 912 */ 913 if (adp != vesa_adp) 914 return (*prevvidsw->set_win_org)(adp, offset); 915 916 if (vesa_adp->va_window_gran == 0) 917 return 1; 918 bzero(&vmf, sizeof(vmf)); 919 vmf.vmf_eax = 0x4f05; 920 vmf.vmf_ebx = 0; /* WINDOW_A, XXX */ 921 vmf.vmf_edx = offset/vesa_adp->va_window_gran; 922 err = vm86_intcall(0x10, &vmf); 923 if ((err != 0) || (vmf.vmf_eax != 0x4f)) 924 return 1; 925 bzero(&vmf, sizeof(vmf)); 926 vmf.vmf_eax = 0x4f05; 927 vmf.vmf_ebx = 1; /* WINDOW_B, XXX */ 928 vmf.vmf_edx = offset/vesa_adp->va_window_gran; 929 err = vm86_intcall(0x10, &vmf); 930 return 0; /* XXX */ 931} 932 933static int 934vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row) 935{ 936 return (*prevvidsw->read_hw_cursor)(adp, col, row); 937} 938 939static int 940vesa_set_hw_cursor(video_adapter_t *adp, int col, int row) 941{ 942 return (*prevvidsw->set_hw_cursor)(adp, col, row); 943} 944 945static int 946vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height, 947 int celsize, int blink) 948{ 949 return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize, 950 blink); 951} 952 953static int 954vesa_mmap(video_adapter_t *adp, vm_offset_t offset) 955{ 956 return (*prevvidsw->mmap)(adp, offset); 957} 958 959static int 960vesa_diag(video_adapter_t *adp, int level) 961{ 962#if VESA_DEBUG > 1 963 struct vesa_mode vmode; 964 int i; 965#endif 966 967 if (adp != vesa_adp) 968 return 1; 969 970#ifndef KLD_MODULE 971 /* call the previous handler first */ 972 (*prevvidsw->diag)(adp, level); 973#endif 974 975 /* general adapter information */ 976 printf("VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n", 977 ((vesa_adp_info->v_version & 0xf000) >> 12) * 10 978 + ((vesa_adp_info->v_version & 0x0f00) >> 8), 979 ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10 980 + (vesa_adp_info->v_version & 0x000f), 981 vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags, 982 vesa_vmodetab, vesa_adp_info->v_modetable); 983 /* OEM string */ 984 if (vesa_oemstr != NULL) 985 printf("VESA: %s\n", vesa_oemstr); 986 987 if (level <= 0) 988 return 0; 989 990 if (vesa_adp_info->v_version >= 0x0200) { 991 /* vendor name */ 992 if (vesa_venderstr != NULL) 993 printf("VESA: %s\n", vesa_venderstr); 994 /* product name */ 995 if (vesa_prodstr != NULL) 996 printf("VESA: %s\n", vesa_prodstr); 997 /* product revision */ 998 if (vesa_revstr != NULL) 999 printf("VESA: %s\n", vesa_revstr); 1000 } 1001 1002#if VESA_DEBUG > 1 1003 /* mode information */ 1004 for (i = 0; 1005 (i < (M_VESA_MODE_MAX - M_VESA_BASE + 1)) 1006 && (vesa_vmodetab[i] != 0xffff); ++i) { 1007 if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode)) 1008 continue; 1009 1010 /* print something for diagnostic purpose */ 1011 printf("VESA: mode:0x%03x, flags:0x%04x", 1012 vesa_vmodetab[i], vmode.v_modeattr); 1013 if (vmode.v_modeattr & V_MODEOPTINFO) { 1014 if (vmode.v_modeattr & V_MODEGRAPHICS) { 1015 printf(", G %dx%dx%d %d, ", 1016 vmode.v_width, vmode.v_height, 1017 vmode.v_bpp, vmode.v_planes); 1018 } else { 1019 printf(", T %dx%d, ", 1020 vmode.v_width, vmode.v_height); 1021 } 1022 printf("font:%dx%d", 1023 vmode.v_cwidth, vmode.v_cheight); 1024 } 1025 if (vmode.v_modeattr & V_MODELFB) { 1026 printf(", mem:%d, LFB:0x%x, off:0x%x", 1027 vmode.v_memmodel, vmode.v_lfb, 1028 vmode.v_offscreen); 1029 } 1030 printf("\n"); 1031 printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ", 1032 vmode.v_waseg, vmode.v_waattr, 1033 vmode.v_wbseg, vmode.v_wbattr); 1034 printf("size:%dk, gran:%dk\n", 1035 vmode.v_wsize, vmode.v_wgran); 1036 } 1037#endif 1038 1039 return 0; 1040} 1041 1042/* module loading */ 1043 1044static int 1045vesa_load(void) 1046{ 1047 int error; 1048 int s; 1049 1050 if (vesa_init_done) 1051 return 0; 1052 1053 /* locate a VGA adapter */ 1054 s = spltty(); 1055 vesa_adp = NULL; 1056 error = vesa_configure(0); 1057 splx(s); 1058 1059#ifdef KLD_MODULE 1060 if (error == 0) 1061 vesa_diag(vesa_adp, bootverbose); 1062#endif 1063 1064 return error; 1065} 1066 1067#ifdef KLD_MODULE 1068 1069static int 1070vesa_unload(void) 1071{ 1072 int error; 1073 int s; 1074 1075 /* if the adapter is currently in a VESA mode, don't unload */ 1076 if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode)) 1077 return EBUSY; 1078 /* 1079 * FIXME: if there is at least one vty which is in a VESA mode, 1080 * we shouldn't be unloading! XXX 1081 */ 1082 1083 s = spltty(); 1084 if ((error = vesa_unload_ioctl()) == 0) { 1085 if (vesa_adp != NULL) 1086 vesa_adp->va_flags &= ~V_ADP_VESA; 1087 vidsw[vesa_adp->va_index] = prevvidsw; 1088 } 1089 splx(s); 1090 1091 return error; 1092} 1093 1094static int 1095vesa_mod_event(module_t mod, int type, void *data) 1096{ 1097 switch (type) { 1098 case MOD_LOAD: 1099 return vesa_load(); 1100 case MOD_UNLOAD: 1101 return vesa_unload(); 1102 default: 1103 break; 1104 } 1105 return 0; 1106} 1107 1108static moduledata_t vesa_mod = { 1109 "vesa", 1110 vesa_mod_event, 1111 NULL, 1112}; 1113 1114DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 1115 1116#else /* KLD_MODULE */ 1117 1118SYSINIT(vesa, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, 1119 (void (*)(void *))vesa_load, NULL); 1120 1121#endif /* KLD_MODULE */ 1122 1123#endif /* (NVGA > 0 && VESA && VM86) || KLD_MODULE */ 1124