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