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