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 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
97 ioctl(0, KDDISABIO, 0);
98 ioctl(0, KDSETMODE, KD_TEXT);
100 ioctl(0, VT_SETMODE, &smode);
115 signal(SIGINT, SIG_IGN);
116 signal(SIGTERM, SIG_IGN);
117 signal(SIGUSR2, SIG_IGN);
118 if (arg == SIGBUS || arg == SIGSEGV) {
119 signal(arg, SIG_DFL);
121 sigaddset(&mask, arg);
122 sigprocmask(SIG_UNBLOCK, &mask, NULL);
129 VGLSwitch(int arg __unused)
135 VGLSwitchPending = 1;
136 signal(SIGUSR1, VGLSwitch);
142 struct vt_mode smode;
148 signal(SIGUSR1, VGLSwitch);
149 signal(SIGINT, VGLAbort);
150 signal(SIGTERM, VGLAbort);
151 signal(SIGSEGV, VGLAbort);
152 signal(SIGBUS, VGLAbort);
153 signal(SIGUSR2, SIG_IGN);
156 VGLSwitchPending = 0;
159 if (ioctl(0, CONS_GET, &VGLOldMode) || ioctl(0, CONS_CURRENT, &adptype))
161 if (IOCGROUP(mode) == 'V') /* XXX: this is ugly */
162 VGLModeInfo.vi_mode = (mode & 0x0ff) + M_VESA_BASE;
164 VGLModeInfo.vi_mode = mode & 0x0ff;
165 if (ioctl(0, CONS_MODEINFO, &VGLModeInfo)) /* FBIO_MODEINFO */
168 /* Save info for old mode to restore font size if old mode is graphics. */
169 VGLOldModeInfo.vi_mode = VGLOldMode;
170 if (ioctl(0, CONS_MODEINFO, &VGLOldModeInfo))
172 VGLOldVInfo.size = sizeof(VGLOldVInfo);
173 if (ioctl(0, CONS_GETINFO, &VGLOldVInfo))
176 VGLDisplay = (VGLBitmap *)malloc(sizeof(VGLBitmap));
177 if (VGLDisplay == NULL)
180 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT && ioctl(0, KDENABIO, 0)) {
188 * vi_mem_model specifies the memory model of the current video mode
191 switch (VGLModeInfo.vi_mem_model) {
192 case V_INFO_MM_PLANAR:
193 /* we can handle EGA/VGA planner modes only */
194 if (VGLModeInfo.vi_depth != 4 || VGLModeInfo.vi_planes != 4
195 || (adptype != KD_EGA && adptype != KD_VGA)) {
199 VGLDisplay->Type = VIDBUF4;
200 VGLDisplay->PixelBytes = 1;
202 case V_INFO_MM_PACKED:
203 /* we can do only 256 color packed modes */
204 if (VGLModeInfo.vi_depth != 8) {
208 VGLDisplay->Type = VIDBUF8;
209 VGLDisplay->PixelBytes = 1;
212 VGLDisplay->Type = VIDBUF8X;
213 VGLDisplay->PixelBytes = 1;
215 case V_INFO_MM_DIRECT:
216 VGLDisplay->PixelBytes = VGLModeInfo.vi_pixel_size;
217 switch (VGLDisplay->PixelBytes) {
219 VGLDisplay->Type = VIDBUF16;
223 VGLDisplay->Type = VIDBUF24;
227 VGLDisplay->Type = VIDBUF32;
239 ioctl(0, VT_WAITACTIVE, 0);
240 ioctl(0, KDSETMODE, KD_GRAPHICS);
241 if (ioctl(0, mode, 0)) {
245 if (ioctl(0, CONS_ADPINFO, &VGLAdpInfo)) { /* FBIO_ADPINFO */
251 * Calculate the shadow screen buffer size. In -CURRENT, va_buffer_size
252 * always holds the entire frame buffer size, wheather it's in the linear
253 * mode or windowed mode.
254 * VGLBufSize = VGLAdpInfo.va_buffer_size;
255 * In -STABLE, va_buffer_size holds the frame buffer size, only if
256 * the linear frame buffer mode is supported. Otherwise the field is zero.
257 * We shall calculate the minimal size in this case:
258 * VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*VGLModeInfo.vi_planes
260 * VGLAdpInfo.va_window_size*VGLModeInfo.vi_planes;
261 * Use whichever is larger.
263 if (VGLAdpInfo.va_buffer_size != 0)
264 VGLBufSize = VGLAdpInfo.va_buffer_size;
266 VGLBufSize = max(VGLAdpInfo.va_line_width*VGLModeInfo.vi_height,
267 VGLAdpInfo.va_window_size)*VGLModeInfo.vi_planes;
269 * The above is for old -CURRENT. Current -CURRENT since r203535 and/or
270 * r248799 restricts va_buffer_size to the displayed size in VESA modes to
271 * avoid wasting kva for mapping unused parts of the frame buffer. But all
272 * parts were usable here. Applying the same restriction to user mappings
273 * makes our virtualization useless and breaks our panning, but large frame
274 * buffers are also difficult for us to manage (clearing and switching may
275 * be too slow, and malloc() may fail). Restrict ourselves similarly to
276 * get the same efficiency and bugs for all kernels.
278 if (VGLModeInfo.vi_mode >= M_VESA_BASE)
279 VGLBufSize = VGLAdpInfo.va_line_width*VGLModeInfo.vi_height*
280 VGLModeInfo.vi_planes;
281 VGLBuf = malloc(VGLBufSize);
282 if (VGLBuf == NULL) {
288 fprintf(stderr, "VGLBufSize:0x%x\n", VGLBufSize);
291 /* see if we are in the windowed buffer mode or in the linear buffer mode */
292 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size) {
293 switch (VGLDisplay->Type) {
295 VGLDisplay->Type = VIDBUF4S;
298 VGLDisplay->Type = VIDBUF8S;
301 VGLDisplay->Type = VIDBUF16S;
304 VGLDisplay->Type = VIDBUF24S;
307 VGLDisplay->Type = VIDBUF32S;
318 VGLDisplay->Xsize = VGLModeInfo.vi_width;
319 VGLDisplay->Ysize = VGLModeInfo.vi_height;
320 depth = VGLModeInfo.vi_depth;
323 VGLDisplay->VXsize = VGLAdpInfo.va_line_width
324 *8/(depth/VGLModeInfo.vi_planes);
325 VGLDisplay->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
326 VGLDisplay->Xorigin = 0;
327 VGLDisplay->Yorigin = 0;
329 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
330 MAP_FILE | MAP_SHARED, 0, 0);
331 if (VGLMem == MAP_FAILED) {
335 VGLDisplay->Bitmap = VGLMem;
340 fprintf(stderr, "va_line_width:%d\n", VGLAdpInfo.va_line_width);
341 fprintf(stderr, "VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
342 VGLDisplay->Xsize, VGLDisplay->Ysize,
343 VGLDisplay->VXsize, VGLDisplay->VYsize);
346 smode.mode = VT_PROCESS;
348 smode.relsig = SIGUSR1;
349 smode.acqsig = SIGUSR1;
350 smode.frsig = SIGINT;
351 if (ioctl(0, VT_SETMODE, &smode)) {
355 VGLTextSetFontFile((byte*)0);
356 VGLClear(VGLDisplay, 0);
363 if (VGLAbortPending) {
367 while (VGLSwitchPending) {
372 VGLSwitchPending = 0;
374 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
375 ioctl(0, KDENABIO, 0);
376 ioctl(0, KDSETMODE, KD_GRAPHICS);
377 ioctl(0, VGLMode, 0);
379 VGLMem = (byte*)mmap(0, VGLAdpInfo.va_window_size, PROT_READ|PROT_WRITE,
380 MAP_FILE | MAP_SHARED, 0, 0);
382 /* XXX: what if mmap() has failed! */
383 VGLDisplay->Type = VIDBUF8; /* XXX */
384 switch (VGLModeInfo.vi_mem_model) {
385 case V_INFO_MM_PLANAR:
386 if (VGLModeInfo.vi_depth == 4 && VGLModeInfo.vi_planes == 4) {
387 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
388 VGLDisplay->Type = VIDBUF4S;
390 VGLDisplay->Type = VIDBUF4;
392 /* shouldn't be happening */
395 case V_INFO_MM_PACKED:
396 if (VGLModeInfo.vi_depth == 8) {
397 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
398 VGLDisplay->Type = VIDBUF8S;
400 VGLDisplay->Type = VIDBUF8;
404 VGLDisplay->Type = VIDBUF8X;
406 case V_INFO_MM_DIRECT:
407 switch (VGLModeInfo.vi_pixel_size) {
409 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
410 VGLDisplay->Type = VIDBUF16S;
412 VGLDisplay->Type = VIDBUF16;
415 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
416 VGLDisplay->Type = VIDBUF24S;
418 VGLDisplay->Type = VIDBUF24;
421 if (VGLBufSize/VGLModeInfo.vi_planes > VGLAdpInfo.va_window_size)
422 VGLDisplay->Type = VIDBUF32S;
424 VGLDisplay->Type = VIDBUF32;
427 /* shouldn't be happening */
431 /* shouldn't be happening */
435 VGLDisplay->Bitmap = VGLMem;
436 VGLDisplay->Xsize = VGLModeInfo.vi_width;
437 VGLDisplay->Ysize = VGLModeInfo.vi_height;
438 VGLSetVScreenSize(VGLDisplay, VGLDisplay->VXsize, VGLDisplay->VYsize);
442 VGLPanScreen(VGLDisplay, VGLDisplay->Xorigin, VGLDisplay->Yorigin);
443 switch (VGLDisplay->Type) {
446 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
447 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
448 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
450 VGLSetSegment(offset);
451 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
452 VGLAdpInfo.va_window_size);
453 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
455 outb(0x3c5, 0x01<<i);
456 bcopy(&VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
464 outb(0x3ce, 0x01); outb(0x3cf, 0x00); /* set/reset enable */
465 outb(0x3ce, 0x08); outb(0x3cf, 0xff); /* bit mask */
466 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
468 outb(0x3c5, 0x01<<i);
469 bcopy(&VGLBuf[i*VGLAdpInfo.va_window_size], VGLMem,
470 VGLAdpInfo.va_window_size);
481 for (offset = 0; offset < VGLBufSize; offset += len) {
482 VGLSetSegment(offset);
483 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
484 bcopy(&VGLBuf[offset], VGLMem, len);
489 ioctl(0, VT_RELDISP, VT_ACKACQ);
492 switch (VGLDisplay->Type) {
494 for (offset = 0; offset < VGLBufSize/VGLModeInfo.vi_planes;
496 VGLSetSegment(offset);
497 len = min(VGLBufSize/VGLModeInfo.vi_planes - offset,
498 VGLAdpInfo.va_window_size);
499 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
502 bcopy(VGLMem, &VGLBuf[i*VGLBufSize/VGLModeInfo.vi_planes + offset],
510 * NOTE: the saved buffer is NOT in the MEMBUF format which
511 * the ordinary memory bitmap object is stored in. XXX
513 for (i = 0; i < VGLModeInfo.vi_planes; i++) {
516 bcopy(VGLMem, &VGLBuf[i*VGLAdpInfo.va_window_size],
517 VGLAdpInfo.va_window_size);
528 for (offset = 0; offset < VGLBufSize; offset += len) {
529 VGLSetSegment(offset);
530 len = min(VGLBufSize - offset, VGLAdpInfo.va_window_size);
531 bcopy(VGLMem, &VGLBuf[offset], len);
536 munmap(VGLDisplay->Bitmap, VGLAdpInfo.va_window_size);
537 ioctl(0, VGLOldMode, 0);
538 ioctl(0, KDSETMODE, KD_TEXT);
539 if (VGLModeInfo.vi_mem_model != V_INFO_MM_DIRECT)
540 ioctl(0, KDDISABIO, 0);
541 ioctl(0, VT_RELDISP, VT_TRUE);
542 VGLDisplay->Bitmap = VGLBuf;
543 VGLDisplay->Type = MEMBUF;
544 VGLDisplay->Xsize = VGLDisplay->VXsize;
545 VGLDisplay->Ysize = VGLDisplay->VYsize;
546 while (!VGLOnDisplay) pause();
552 VGLSetSegment(unsigned int offset)
554 if (offset/VGLAdpInfo.va_window_size != VGLCurWindow) {
555 ioctl(0, CONS_SETWINORG, offset); /* FBIO_SETWINORG */
556 VGLCurWindow = offset/VGLAdpInfo.va_window_size;
558 return (offset%VGLAdpInfo.va_window_size);
562 VGLSetVScreenSize(VGLBitmap *object, int VXsize, int VYsize)
566 if (VXsize < object->Xsize || VYsize < object->Ysize)
568 if (object->Type == MEMBUF)
570 if (ioctl(0, FBIO_SETLINEWIDTH, &VXsize))
572 ioctl(0, CONS_ADPINFO, &VGLAdpInfo); /* FBIO_ADPINFO */
573 depth = VGLModeInfo.vi_depth;
576 object->VXsize = VGLAdpInfo.va_line_width
577 *8/(depth/VGLModeInfo.vi_planes);
578 object->VYsize = VGLBufSize/VGLModeInfo.vi_planes/VGLAdpInfo.va_line_width;
579 if (VYsize < object->VYsize)
580 object->VYsize = VYsize;
583 fprintf(stderr, "new size: VGLXsize:%d, Ysize:%d, VXsize:%d, VYsize:%d\n",
584 object->Xsize, object->Ysize, object->VXsize, object->VYsize);
591 VGLPanScreen(VGLBitmap *object, int x, int y)
593 video_display_start_t origin;
595 if (x < 0 || x + object->Xsize > object->VXsize
596 || y < 0 || y + object->Ysize > object->VYsize)
598 if (object->Type == MEMBUF)
602 if (ioctl(0, FBIO_SETDISPSTART, &origin))
608 fprintf(stderr, "new origin: (%d, %d)\n", x, y);