2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991-1997 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
42 #include <sys/consio.h>
45 /* XXX Direct Color 24bits modes unsupported */
47 #define min(x, y) (((x) < (y)) ? (x) : (y))
48 #define max(x, y) (((x) > (y)) ? (x) : (y))
50 VGLBitmap *VGLDisplay;
51 video_info_t VGLModeInfo;
52 video_adapter_info_t VGLAdpInfo;
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;
79 signal(SIGUSR1, SIG_IGN);
81 if (VGLMem != MAP_FAILED) {
82 VGLClear(VGLDisplay, 0);
83 munmap(VGLMem, VGLAdpInfo.va_window_size);
86 if (VGLOldMode >= M_VESA_BASE)
87 ioctl(0, _IO('V', VGLOldMode - M_VESA_BASE), 0);
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);
96 ioctl(0, KDDISABIO, 0);
97 ioctl(0, KDSETMODE, KD_TEXT);
99 ioctl(0, VT_SETMODE, &smode);
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);
120 sigaddset(&mask, arg);
121 sigprocmask(SIG_UNBLOCK, &mask, NULL);
128 VGLSwitch(int arg __unused)
134 VGLSwitchPending = 1;
135 signal(SIGUSR1, VGLSwitch);
141 struct vt_mode smode;
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);
155 VGLSwitchPending = 0;
158 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
160 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */
161 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
163 VGLModeInfo.vi_mode = mode & 0x0ff;
164 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */
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))
171 VGLOldVInfo.size = sizeof(VGLOldVInfo);
172 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
175 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
176 if (VGLDisplay == NULL)
179 if (ioctl(0, KDENABIO, 0)) {
187 * vi_mem_model specifies the memory model of the current video mode
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)) {
198 VGLDisplay->Type = VIDBUF4;
199 VGLDisplay->PixelBytes = 1;
201 case V_INFO_MM_PACKED:
202 /* we can do only 256 color packed modes */
203 if (VGLModeInfo.vi_depth != 8) {
207 VGLDisplay->Type = VIDBUF8;
208 VGLDisplay->PixelBytes = 1;
211 VGLDisplay->Type = VIDBUF8X;
212 VGLDisplay->PixelBytes = 1;
214 case V_INFO_MM_DIRECT:
215 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
216 switch (VGLDisplay->PixelBytes) {
218 VGLDisplay->Type = VIDBUF16;
222 VGLDisplay->Type = VIDBUF24;
226 VGLDisplay->Type = VIDBUF32;
238 ioctl(0, VT_WAITACTIVE, 0);
239 ioctl(0, KDSETMODE, KD_GRAPHICS);
240 if (ioctl(0, mode, 0)) {
244 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */
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
259 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
260 * Use whichever is larger.
262 if (VGLAdpInfo.va_buffer_size != 0)
263 VGLBufSize = VGLAdpInfo.va_buffer_size;
265 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
266 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
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.
277 if (VGLModeInfo.vi_mode >= M_VESA_BASE)
278 VGLBufSize = 2*VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*
279 VGLModeInfo.vi_planes;
280 VGLBuf = malloc(VGLBufSize);
281 if (VGLBuf == NULL) {
287 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
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) {
294 VGLDisplay->Type = VIDBUF4S;
297 VGLDisplay->Type = VIDBUF8S;
300 VGLDisplay->Type = VIDBUF16S;
303 VGLDisplay->Type = VIDBUF24S;
306 VGLDisplay->Type = VIDBUF32S;
317 VGLDisplay->Xsize = VGLModeInfo.vi_width;
318 VGLDisplay->Ysize = VGLModeInfo.vi_height;
319 depth = VGLModeInfo.vi_depth;
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;
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) {
334 VGLDisplay->Bitmap = VGLMem;
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);
345 smode.mode = VT_PROCESS;
347 smode.relsig = SIGUSR1;
348 smode.acqsig = SIGUSR1;
349 smode.frsig = SIGINT;
350 if (ioctl(0, VT_SETMODE, &smode)) {
354 VGLTextSetFontFile((byte*)0);
355 VGLClear(VGLDisplay, 0);
362 if (VGLAbortPending) {
366 while (VGLSwitchPending) {
371 VGLSwitchPending = 0;
373 ioctl(0, KDENABIO, 0);
374 ioctl(0, KDSETMODE, KD_GRAPHICS);
375 ioctl(0, VGLMode, 0);
377 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
378 MAP_FILE | MAP_SHARED, 0, 0);
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;
388 VGLDisplay->Type = VIDBUF4;
390 /* shouldn't be happening */
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;
398 VGLDisplay->Type = VIDBUF8;
402 VGLDisplay->Type = VIDBUF8X;
404 case V_INFO_MM_DIRECT:
405 switch (VGLModeInfo.vi_pixel_size) {
407 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
408 VGLDisplay->Type = VIDBUF16S;
410 VGLDisplay->Type = VIDBUF16;
413 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
414 VGLDisplay->Type = VIDBUF24S;
416 VGLDisplay->Type = VIDBUF24;
419 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
420 VGLDisplay->Type = VIDBUF32S;
422 VGLDisplay->Type = VIDBUF32;
425 /* shouldn't be happening */
429 /* shouldn't be happening */
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) {
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;
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++) {
450 outb(0x3c5, 0x01<<i);
451 bcopy(&VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
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++) {
463 outb(0x3c5, 0x01<<i);
464 bcopy(&VGLBuf[i*VGLAdpInfo.va_window_size], VGLMem,
465 VGLAdpInfo.va_window_size);
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);
484 ioctl(0, VT_RELDISP, VT_ACKACQ);
487 switch (VGLDisplay->Type) {
489 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
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++) {
497 bcopy(VGLMem, &VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
505 * NOTE: the saved buffer is NOT in the MEMBUF format which
506 * the ordinary memory bitmap object is stored in. XXX
508 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
511 bcopy(VGLMem, &VGLBuf[i*VGLAdpInfo.va_window_size],
512 VGLAdpInfo.va_window_size);
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);
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();
546 VGLSetSegment(unsigned int offset)
548 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
549 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
550 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
552 return (offset%VGLAdpInfo.va_window_size);
556 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
560 if (VXsize < object->Xsize || VYsize < object->Ysize)
562 if (object->Type == MEMBUF)
564 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
566 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
567 depth = VGLModeInfo.vi_depth;
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;
577 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
578 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
585 VGLPanScreen(VGLBitmap *object, int x, int y)
587 video_display_start_t origin;
589 if (x < 0 || x + object->Xsize > object->VXsize
590 || y < 0 || y + object->Ysize > object->VYsize)
592 if (object->Type == MEMBUF)
596 if (ioctl(0, FBIO_SETDISPSTART, &origin))
602 fprintf(stderr, "new origin: (%d, %d)\n", x, y);