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