1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991-1997 Søren Schmidt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <signal.h> 35 #include <stdio.h> 36 #include <sys/types.h> 37 #include <sys/file.h> 38 #include <sys/ioctl.h> 39 #include <sys/mman.h> 40 #include <sys/fbio.h> 41 #include <sys/kbio.h> 42 #include <sys/consio.h> 43 #include "vgl.h" 44 45 /* XXX Direct Color 24bits modes unsupported */ 46 47 #define min(x, y) (((x) < (y)) ? (x) : (y)) 48 #define max(x, y) (((x) > (y)) ? (x) : (y)) 49 50 VGLBitmap *VGLDisplay; 51 video_info_t VGLModeInfo; 52 video_adapter_info_t VGLAdpInfo; 53 byte *VGLBuf; 54 55 static int VGLMode; 56 static int VGLOldMode; 57 static size_t VGLBufSize; 58 static byte *VGLMem = MAP_FAILED; 59 static int VGLSwitchPending; 60 static int VGLAbortPending; 61 static int VGLOnDisplay; 62 static unsigned int VGLCurWindow; 63 static int VGLInitDone = 0; 64 static video_info_t VGLOldModeInfo; 65 static vid_info_t VGLOldVInfo; 66 67 void 68 VGLEnd() 69 { 70 struct vt_mode smode; 71 int size[3]; 72 73 if (!VGLInitDone) 74 return; 75 VGLInitDone = 0; 76 VGLSwitchPending = 0; 77 VGLAbortPending = 0; 78 79 signal(SIGUSR1, SIG_IGN); 80 81 if (VGLMem != MAP_FAILED) { 82 VGLClear(VGLDisplay, 0); 83 munmap(VGLMem, VGLAdpInfo.va_window_size); 84 } 85 86 if (VGLOldMode >= M_VESA_BASE) 87 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0); 88 else 89 ioctl(0, _IO('S', VGLOldMode), 0); 90 if (VGLOldModeInfo.vi_flags & V_INFO_GRAPHICS) { 91 size[0] = VGLOldVInfo.mv_csz; 92 size[1] = VGLOldVInfo.mv_rsz; 93 size[2] = VGLOldVInfo.font_size;; 94 ioctl(0, KDRASTER, size); 95 } 96 ioctl(0, KDDISABIO, 0); 97 ioctl(0, KDSETMODE, KD_TEXT); 98 smode.mode = VT_AUTO; 99 ioctl(0, VT_SETMODE, &smode); 100 if (VGLBuf) 101 free(VGLBuf); 102 VGLBuf = NULL; 103 free(VGLDisplay); 104 VGLDisplay = NULL; 105 VGLKeyboardEnd(); 106 } 107 108 static void 109 VGLAbort(int arg) 110 { 111 sigset_t mask; 112 113 VGLAbortPending = 1; 114 signal(SIGINT, SIG_IGN); 115 signal(SIGTERM, SIG_IGN); 116 signal(SIGUSR2, SIG_IGN); 117 if (arg == SIGBUS || arg == SIGSEGV) { 118 signal(arg, SIG_DFL); 119 sigemptyset(&mask); 120 sigaddset(&mask, arg); 121 sigprocmask(SIG_UNBLOCK, &mask, NULL); 122 VGLEnd(); 123 kill(getpid(), arg); 124 } 125 } 126 127 static void 128 VGLSwitch(int arg __unused) 129 { 130 if (!VGLOnDisplay) 131 VGLOnDisplay = 1; 132 else 133 VGLOnDisplay = 0; 134 VGLSwitchPending = 1; 135 signal(SIGUSR1, VGLSwitch); 136 } 137 138 int 139 VGLInit(int mode) 140 { 141 struct vt_mode smode; 142 int adptype, depth; 143 144 if (VGLInitDone) 145 return -1; 146 147 signal(SIGUSR1, VGLSwitch); 148 signal(SIGINT, VGLAbort); 149 signal(SIGTERM, VGLAbort); 150 signal(SIGSEGV, VGLAbort); 151 signal(SIGBUS, VGLAbort); 152 signal(SIGUSR2, SIG_IGN); 153 154 VGLOnDisplay = 1; 155 VGLSwitchPending = 0; 156 VGLAbortPending = 0; 157 158 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype)) 159 return -1; 160 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */ 161 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE; 162 else 163 VGLModeInfo.vi_mode = mode & 0x0ff; 164 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */ 165 return -1; 166 167 /* Save info for old mode to restore font size if old mode is graphics. */ 168 VGLOldModeInfo.vi_mode = VGLOldMode; 169 if (ioctl(0, CONS_MODEINFO, &VGLOldModeInfo)) 170 return -1; 171 VGLOldVInfo.size = sizeof(VGLOldVInfo); 172 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo)) 173 return -1; 174 175 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap)); 176 if (VGLDisplay == NULL) 177 return -2; 178 179 if (ioctl(0, KDENABIO, 0)) { 180 free(VGLDisplay); 181 return -3; 182 } 183 184 VGLInitDone = 1; 185 186 /* 187 * vi_mem_model specifies the memory model of the current video mode 188 * in -CURRENT. 189 */ 190 switch (VGLModeInfo.vi_mem_model) { 191 case V_INFO_MM_PLANAR: 192 /* we can handle EGA/VGA planner modes only */ 193 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4 194 || (adptype != KD_EGA && adptype != KD_VGA)) { 195 VGLEnd(); 196 return -4; 197 } 198 VGLDisplay->Type = VIDBUF4; 199 VGLDisplay->PixelBytes = 1; 200 break; 201 case V_INFO_MM_PACKED: 202 /* we can do only 256 color packed modes */ 203 if (VGLModeInfo.vi_depth != 8) { 204 VGLEnd(); 205 return -4; 206 } 207 VGLDisplay->Type = VIDBUF8; 208 VGLDisplay->PixelBytes = 1; 209 break; 210 case V_INFO_MM_VGAX: 211 VGLDisplay->Type = VIDBUF8X; 212 VGLDisplay->PixelBytes = 1; 213 break; 214 case V_INFO_MM_DIRECT: 215 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size; 216 switch (VGLDisplay->PixelBytes) { 217 case 2: 218 VGLDisplay->Type = VIDBUF16; 219 break; 220 #if notyet 221 case 3: 222 VGLDisplay->Type = VIDBUF24; 223 break; 224 #endif 225 case 4: 226 VGLDisplay->Type = VIDBUF32; 227 break; 228 default: 229 VGLEnd(); 230 return -4; 231 } 232 break; 233 default: 234 VGLEnd(); 235 return -4; 236 } 237 238 ioctl(0, VT_WAITACTIVE, 0); 239 ioctl(0, KDSETMODE, KD_GRAPHICS); 240 if (ioctl(0, mode, 0)) { 241 VGLEnd(); 242 return -5; 243 } 244 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */ 245 VGLEnd(); 246 return -6; 247 } 248 249 /* 250 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size 251 * always holds the entire frame buffer size, wheather it's in the linear 252 * mode or windowed mode. 253 * VGLBufSize = VGLAdpInfo.va_buffer_size; 254 * In -STABLE, va_buffer_size holds the frame buffer size, only if 255 * the linear frame buffer mode is supported. Otherwise the field is zero. 256 * We shall calculate the minimal size in this case: 257 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes 258 * or 259 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes; 260 * Use whichever is larger. 261 */ 262 if (VGLAdpInfo.va_buffer_size != 0) 263 VGLBufSize = VGLAdpInfo.va_buffer_size; 264 else 265 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height, 266 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes; 267 /* 268 * The above is for old -CURRENT. Current -CURRENT since r203535 and/or 269 * r248799 restricts va_buffer_size to the displayed size in VESA modes to 270 * avoid wasting kva for mapping unused parts of the frame buffer. But all 271 * parts were usable here. Applying the same restriction to user mappings 272 * makes our virtualization useless and breaks our panning, but large frame 273 * buffers are also difficult for us to manage (clearing and switching may 274 * be too slow, and malloc() may fail). Restrict ourselves similarly to 275 * get the same efficiency and bugs for all kernels. 276 */ 277 if (VGLModeInfo.vi_mode >= M_VESA_BASE) 278 VGLBufSize = VGLAdpInfo.va_line_width*VGLModeInfo.vi_height* 279 VGLModeInfo.vi_planes; 280 VGLBuf = malloc(VGLBufSize); 281 if (VGLBuf == NULL) { 282 VGLEnd(); 283 return -7; 284 } 285 286 #ifdef LIBVGL_DEBUG 287 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize); 288 #endif 289 290 /* see if we are in the windowed buffer mode or in the linear buffer mode */ 291 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) { 292 switch (VGLDisplay->Type) { 293 case VIDBUF4: 294 VGLDisplay->Type = VIDBUF4S; 295 break; 296 case VIDBUF8: 297 VGLDisplay->Type = VIDBUF8S; 298 break; 299 case VIDBUF16: 300 VGLDisplay->Type = VIDBUF16S; 301 break; 302 case VIDBUF24: 303 VGLDisplay->Type = VIDBUF24S; 304 break; 305 case VIDBUF32: 306 VGLDisplay->Type = VIDBUF32S; 307 break; 308 default: 309 VGLEnd(); 310 return -8; 311 } 312 } 313 314 VGLMode = mode; 315 VGLCurWindow = 0; 316 317 VGLDisplay->Xsize = VGLModeInfo.vi_width; 318 VGLDisplay->Ysize = VGLModeInfo.vi_height; 319 depth = VGLModeInfo.vi_depth; 320 if (depth == 15) 321 depth = 16; 322 VGLDisplay->VXsize = VGLAdpInfo.va_line_width 323 *8/(depth/VGLModeInfo.vi_planes); 324 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width; 325 VGLDisplay->Xorigin = 0; 326 VGLDisplay->Yorigin = 0; 327 328 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE, 329 MAP_FILE | MAP_SHARED, 0, 0); 330 if (VGLMem == MAP_FAILED) { 331 VGLEnd(); 332 return -7; 333 } 334 VGLDisplay->Bitmap = VGLMem; 335 336 VGLSavePalette(); 337 338 #ifdef LIBVGL_DEBUG 339 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width); 340 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n", 341 VGLDisplay->Xsize, VGLDisplay->Ysize, 342 VGLDisplay->VXsize, VGLDisplay->VYsize); 343 #endif 344 345 smode.mode = VT_PROCESS; 346 smode.waitv = 0; 347 smode.relsig = SIGUSR1; 348 smode.acqsig = SIGUSR1; 349 smode.frsig = SIGINT; 350 if (ioctl(0, VT_SETMODE, &smode)) { 351 VGLEnd(); 352 return -9; 353 } 354 VGLTextSetFontFile((byte*)0); 355 VGLClear(VGLDisplay, 0); 356 return 0; 357 } 358 359 void 360 VGLCheckSwitch() 361 { 362 if (VGLAbortPending) { 363 VGLEnd(); 364 exit(0); 365 } 366 while (VGLSwitchPending) { 367 unsigned int offset; 368 unsigned int len; 369 int i; 370 371 VGLSwitchPending = 0; 372 if (VGLOnDisplay) { 373 ioctl(0, KDENABIO, 0); 374 ioctl(0, KDSETMODE, KD_GRAPHICS); 375 ioctl(0, VGLMode, 0); 376 VGLCurWindow = 0; 377 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE, 378 MAP_FILE | MAP_SHARED, 0, 0); 379 380 /* XXX: what if mmap() has failed! */ 381 VGLDisplay->Type = VIDBUF8; /* XXX */ 382 switch (VGLModeInfo.vi_mem_model) { 383 case V_INFO_MM_PLANAR: 384 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) { 385 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 386 VGLDisplay->Type = VIDBUF4S; 387 else 388 VGLDisplay->Type = VIDBUF4; 389 } else { 390 /* shouldn't be happening */ 391 } 392 break; 393 case V_INFO_MM_PACKED: 394 if (VGLModeInfo.vi_depth == 8) { 395 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 396 VGLDisplay->Type = VIDBUF8S; 397 else 398 VGLDisplay->Type = VIDBUF8; 399 } 400 break; 401 case V_INFO_MM_VGAX: 402 VGLDisplay->Type = VIDBUF8X; 403 break; 404 case V_INFO_MM_DIRECT: 405 switch (VGLModeInfo.vi_pixel_size) { 406 case 2: 407 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 408 VGLDisplay->Type = VIDBUF16S; 409 else 410 VGLDisplay->Type = VIDBUF16; 411 break; 412 case 3: 413 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 414 VGLDisplay->Type = VIDBUF24S; 415 else 416 VGLDisplay->Type = VIDBUF24; 417 break; 418 case 4: 419 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) 420 VGLDisplay->Type = VIDBUF32S; 421 else 422 VGLDisplay->Type = VIDBUF32; 423 break; 424 default: 425 /* shouldn't be happening */ 426 break; 427 } 428 default: 429 /* shouldn't be happening */ 430 break; 431 } 432 433 VGLDisplay->Bitmap = VGLMem; 434 VGLDisplay->Xsize = VGLModeInfo.vi_width; 435 VGLDisplay->Ysize = VGLModeInfo.vi_height; 436 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize); 437 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin); 438 switch (VGLDisplay->Type) { 439 case VIDBUF4S: 440 outb(0x3c6, 0xff); 441 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */ 442 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */ 443 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes; 444 offset += len) { 445 VGLSetSegment(offset); 446 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset, 447 VGLAdpInfo.va_window_size); 448 for (i = 0; i < VGLModeInfo.vi_planes; i++) { 449 outb(0x3c4, 0x02); 450 outb(0x3c5, 0x01<<i); 451 bcopy(&VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset], 452 VGLMem, len); 453 } 454 } 455 break; 456 case VIDBUF4: 457 case VIDBUF8X: 458 outb(0x3c6, 0xff); 459 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */ 460 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */ 461 for (i = 0; i < VGLModeInfo.vi_planes; i++) { 462 outb(0x3c4, 0x02); 463 outb(0x3c5, 0x01<<i); 464 bcopy(&VGLBuf[i*VGLAdpInfo.va_window_size], VGLMem, 465 VGLAdpInfo.va_window_size); 466 } 467 break; 468 case VIDBUF8: 469 case VIDBUF8S: 470 case VIDBUF16: 471 case VIDBUF16S: 472 case VIDBUF24: 473 case VIDBUF24S: 474 case VIDBUF32: 475 case VIDBUF32S: 476 for (offset = 0; offset < VGLBufSize; offset += len) { 477 VGLSetSegment(offset); 478 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size); 479 bcopy(&VGLBuf[offset], VGLMem, len); 480 } 481 break; 482 } 483 VGLRestorePalette(); 484 ioctl(0, VT_RELDISP, VT_ACKACQ); 485 } 486 else { 487 switch (VGLDisplay->Type) { 488 case VIDBUF4S: 489 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes; 490 offset += len) { 491 VGLSetSegment(offset); 492 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset, 493 VGLAdpInfo.va_window_size); 494 for (i = 0; i < VGLModeInfo.vi_planes; i++) { 495 outb(0x3ce, 0x04); 496 outb(0x3cf, i); 497 bcopy(VGLMem, &VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset], 498 len); 499 } 500 } 501 break; 502 case VIDBUF4: 503 case VIDBUF8X: 504 /* 505 * NOTE: the saved buffer is NOT in the MEMBUF format which 506 * the ordinary memory bitmap object is stored in. XXX 507 */ 508 for (i = 0; i < VGLModeInfo.vi_planes; i++) { 509 outb(0x3ce, 0x04); 510 outb(0x3cf, i); 511 bcopy(VGLMem, &VGLBuf[i*VGLAdpInfo.va_window_size], 512 VGLAdpInfo.va_window_size); 513 } 514 break; 515 case VIDBUF8: 516 case VIDBUF8S: 517 case VIDBUF16: 518 case VIDBUF16S: 519 case VIDBUF24: 520 case VIDBUF24S: 521 case VIDBUF32: 522 case VIDBUF32S: 523 for (offset = 0; offset < VGLBufSize; offset += len) { 524 VGLSetSegment(offset); 525 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size); 526 bcopy(VGLMem, &VGLBuf[offset], len); 527 } 528 break; 529 } 530 VGLMem = MAP_FAILED; 531 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size); 532 ioctl(0, VGLOldMode, 0); 533 ioctl(0, KDSETMODE, KD_TEXT); 534 ioctl(0, KDDISABIO, 0); 535 ioctl(0, VT_RELDISP, VT_TRUE); 536 VGLDisplay->Bitmap = VGLBuf; 537 VGLDisplay->Type = MEMBUF; 538 VGLDisplay->Xsize = VGLDisplay->VXsize; 539 VGLDisplay->Ysize = VGLDisplay->VYsize; 540 while (!VGLOnDisplay) pause(); 541 } 542 } 543 } 544 545 int 546 VGLSetSegment(unsigned int offset) 547 { 548 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) { 549 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */ 550 VGLCurWindow = offset/VGLAdpInfo.va_window_size; 551 } 552 return (offset%VGLAdpInfo.va_window_size); 553 } 554 555 int 556 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize) 557 { 558 int depth; 559 560 if (VXsize < object->Xsize || VYsize < object->Ysize) 561 return -1; 562 if (object->Type == MEMBUF) 563 return -1; 564 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize)) 565 return -1; 566 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */ 567 depth = VGLModeInfo.vi_depth; 568 if (depth == 15) 569 depth = 16; 570 object->VXsize = VGLAdpInfo.va_line_width 571 *8/(depth/VGLModeInfo.vi_planes); 572 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width; 573 if (VYsize < object->VYsize) 574 object->VYsize = VYsize; 575 576 #ifdef LIBVGL_DEBUG 577 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n", 578 object->Xsize, object->Ysize, object->VXsize, object->VYsize); 579 #endif 580 581 return 0; 582 } 583 584 int 585 VGLPanScreen(VGLBitmap *object, int x, int y) 586 { 587 video_display_start_t origin; 588 589 if (x < 0 || x + object->Xsize > object->VXsize 590 || y < 0 || y + object->Ysize > object->VYsize) 591 return -1; 592 if (object->Type == MEMBUF) 593 return 0; 594 origin.x = x; 595 origin.y = y; 596 if (ioctl(0, FBIO_SETDISPSTART, &origin)) 597 return -1; 598 object->Xorigin = x; 599 object->Yorigin = y; 600 601 #ifdef LIBVGL_DEBUG 602 fprintf(stderr, "new origin: (%d, %d)\n", x, y); 603 #endif 604 605 return 0; 606 } 607