VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxvideo/vboxvideo_13.c@ 28372

Last change on this file since 28372 was 28372, checked in by vboxsync, 15 years ago

X11 additions: branding

  • Property svn:eol-style set to native
File size: 42.8 KB
Line 
1/** @file
2 *
3 * Linux Additions X11 graphics driver
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 * --------------------------------------------------------------------
21 *
22 * This code is based on:
23 *
24 * X11 VESA driver
25 *
26 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
27 *
28 * Permission is hereby granted, free of charge, to any person obtaining a
29 * copy of this software and associated documentation files (the "Software"),
30 * to deal in the Software without restriction, including without limitation
31 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
32 * and/or sell copies of the Software, and to permit persons to whom the
33 * Software is furnished to do so, subject to the following conditions:
34 *
35 * The above copyright notice and this permission notice shall be included in
36 * all copies or substantial portions of the Software.
37 *
38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
41 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
43 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SOFTWARE.
45 *
46 * Except as contained in this notice, the name of Conectiva Linux shall
47 * not be used in advertising or otherwise to promote the sale, use or other
48 * dealings in this Software without prior written authorization from
49 * Conectiva Linux.
50 *
51 * Authors: Paulo César Pereira de Andrade <[email protected]>
52 */
53
54#include "xorg-server.h"
55#include "vboxvideo.h"
56#include "version-generated.h"
57#include "product-generated.h"
58#include <xf86.h>
59
60/* All drivers initialising the SW cursor need this */
61#include "mipointer.h"
62
63/* All drivers implementing backing store need this */
64#include "mibstore.h"
65
66/* Colormap handling */
67#include "micmap.h"
68#include "xf86cmap.h"
69
70/* DPMS */
71/* #define DPMS_SERVER
72#include "extensions/dpms.h" */
73
74/* VGA hardware functions for setting and restoring text mode */
75#include "vgaHW.h"
76
77/* X.org 1.3+ mode setting */
78#include "xf86Crtc.h"
79#include "xf86Modes.h"
80
81/* Mandatory functions */
82
83static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
84static void VBOXIdentify(int flags);
85#ifndef PCIACCESS
86static Bool VBOXProbe(DriverPtr drv, int flags);
87#else
88static Bool VBOXPciProbe(DriverPtr drv, int entity_num,
89 struct pci_device *dev, intptr_t match_data);
90#endif
91static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
92static Bool VBOXScreenInit(int Index, ScreenPtr pScreen, int argc,
93 char **argv);
94static Bool VBOXEnterVT(int scrnIndex, int flags);
95static void VBOXLeaveVT(int scrnIndex, int flags);
96static Bool VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen);
97static Bool VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags);
98static ModeStatus VBOXValidMode(int scrn, DisplayModePtr p, Bool flag, int pass);
99static Bool VBOXSetMode(ScrnInfoPtr pScrn, DisplayModePtr pMode);
100static void VBOXAdjustFrame(int scrnIndex, int x, int y, int flags);
101static void VBOXFreeScreen(int scrnIndex, int flags);
102static void VBOXFreeRec(ScrnInfoPtr pScrn);
103
104/* locally used functions */
105static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
106static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
107static Bool VBOXSaveRestore(ScrnInfoPtr pScrn,
108 vbeSaveRestoreFunction function);
109
110enum GenericTypes
111{
112 CHIP_VBOX_GENERIC
113};
114
115#ifdef PCIACCESS
116static const struct pci_id_match vbox_device_match[] = {
117 {
118 VBOX_VENDORID, VBOX_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
119 0, 0, 0
120 },
121
122 { 0, 0, 0 },
123};
124#endif
125
126/* Supported chipsets */
127static SymTabRec VBOXChipsets[] =
128{
129 {VBOX_DEVICEID, "vbox"},
130 {-1, NULL}
131};
132
133static PciChipsets VBOXPCIchipsets[] = {
134 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
135 { -1, -1, RES_UNDEFINED },
136};
137
138/*
139 * This contains the functions needed by the server after loading the
140 * driver module. It must be supplied, and gets added the driver list by
141 * the Module Setup funtion in the dynamic case. In the static case a
142 * reference to this is compiled in, and this requires that the name of
143 * this DriverRec be an upper-case version of the driver name.
144 */
145
146_X_EXPORT DriverRec VBOXVIDEO = {
147 VBOX_VERSION,
148 VBOX_DRIVER_NAME,
149 VBOXIdentify,
150#ifdef PCIACCESS
151 NULL,
152#else
153 VBOXProbe,
154#endif
155 VBOXAvailableOptions,
156 NULL,
157 0,
158 NULL,
159
160#ifdef PCIACCESS
161 vbox_device_match,
162 VBOXPciProbe
163#endif
164};
165
166/* No options for now */
167static const OptionInfoRec VBOXOptions[] = {
168 { -1, NULL, OPTV_NONE, {0}, FALSE }
169};
170
171static VBOXPtr
172VBOXGetRec(ScrnInfoPtr pScrn)
173{
174 if (!pScrn->driverPrivate) {
175 pScrn->driverPrivate = xcalloc(sizeof(VBOXRec), 1);
176 }
177
178 return ((VBOXPtr)pScrn->driverPrivate);
179}
180
181static void
182VBOXFreeRec(ScrnInfoPtr pScrn)
183{
184 VBOXPtr pVBox = VBOXGetRec(pScrn);
185 xfree(pVBox->savedPal);
186 xfree(pVBox->fonts);
187 xfree(pScrn->driverPrivate);
188 pScrn->driverPrivate = NULL;
189}
190
191/* X.org 1.3+ mode-setting support ******************************************/
192
193/* For descriptions of these functions and structures, see
194 hw/xfree86/modes/xf86Crtc.h and hw/xfree86/modes/xf86Modes.h in the
195 X.Org source tree. */
196
197static Bool
198VBOXCrtcResize(ScrnInfoPtr scrn, int width, int height)
199{
200 int bpp = scrn->bitsPerPixel;
201 ScreenPtr pScreen = scrn->pScreen;
202 PixmapPtr pPixmap = NULL;
203 VBOXPtr pVBox = VBOXGetRec(scrn);
204 Bool rc = TRUE;
205
206 TRACE_LOG("width=%d, height=%d\n", width, height);
207 /* We only support horizontal resolutions which are a multiple of 8.
208 * Round up if necessary. */
209 width = (width + 7) & ~7;
210 if (width * height * bpp / 8 >= scrn->videoRam * 1024)
211 {
212 xf86DrvMsg(scrn->scrnIndex, X_ERROR,
213 "Unable to set up a virtual screen size of %dx%d with %d Kb of video memory. Please increase the video memory size.\n",
214 width, height, scrn->videoRam);
215 rc = FALSE;
216 }
217 if (rc) {
218 pPixmap = pScreen->GetScreenPixmap(pScreen);
219 if (NULL == pPixmap) {
220 xf86DrvMsg(scrn->scrnIndex, X_ERROR,
221 "Failed to get the screen pixmap.\n");
222 rc = FALSE;
223 }
224 }
225 if (rc) {
226 if (
227 !pScreen->ModifyPixmapHeader(pPixmap, width, height,
228 scrn->depth, bpp, width * bpp / 8,
229 pVBox->base)
230 ) {
231 xf86DrvMsg(scrn->scrnIndex, X_ERROR,
232 "Failed to set up the screen pixmap.\n");
233 rc = FALSE;
234 }
235 }
236 if (rc) {
237 scrn->virtualX = width;
238 scrn->virtualY = height;
239 scrn->displayWidth = width;
240#ifdef VBOX_DRI
241 if (pVBox->useDRI)
242 VBOXDRIUpdateStride(scrn, pVBox);
243#endif
244 /* Write the new values to the hardware */
245 rc = xf86SetDesiredModes(scrn);
246 }
247 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
248 return rc;
249}
250
251static const xf86CrtcConfigFuncsRec VBOXCrtcConfigFuncs = {
252 VBOXCrtcResize
253};
254
255static void
256vbox_crtc_dpms(xf86CrtcPtr crtc, int mode)
257{ (void) crtc; (void) mode; }
258
259static Bool
260vbox_crtc_lock (xf86CrtcPtr crtc)
261{ (void) crtc; return FALSE; }
262
263static Bool
264vbox_crtc_mode_fixup (xf86CrtcPtr crtc, DisplayModePtr mode,
265 DisplayModePtr adjusted_mode)
266{
267 ScrnInfoPtr pScrn = crtc->scrn;
268 int xRes = adjusted_mode->HDisplay;
269
270 (void) mode;
271 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d\n", adjusted_mode->name,
272 adjusted_mode->HDisplay, adjusted_mode->VDisplay);
273 /* We only support horizontal resolutions which are a multiple of 8. Round down if
274 necessary. */
275 if (xRes % 8 != 0)
276 {
277 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
278 "VirtualBox only supports screen widths which are a multiple of 8. Rounding down from %d to %d\n",
279 xRes, xRes - (xRes % 8));
280 adjusted_mode->HDisplay = xRes - (xRes % 8);
281 }
282 return TRUE;
283}
284
285static void
286vbox_crtc_stub (xf86CrtcPtr crtc)
287{ (void) crtc; }
288
289static void
290vbox_crtc_mode_set (xf86CrtcPtr crtc, DisplayModePtr mode,
291 DisplayModePtr adjusted_mode, int x, int y)
292{
293 (void) mode;
294 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d, x=%d, y=%d\n", adjusted_mode->name,
295 adjusted_mode->HDisplay, adjusted_mode->VDisplay, x, y);
296 VBOXSetMode(crtc->scrn, adjusted_mode);
297 VBOXAdjustFrame(crtc->scrn->scrnIndex, x, y, 0);
298 /* Don't remember any modes set while we are seamless, as they are
299 * just temporary. */
300 if (!vboxGuestIsSeamless(crtc->scrn))
301 vboxSaveVideoMode(crtc->scrn, adjusted_mode->HDisplay,
302 adjusted_mode->VDisplay, crtc->scrn->bitsPerPixel);
303}
304
305static void
306vbox_crtc_gamma_set (xf86CrtcPtr crtc, CARD16 *red,
307 CARD16 *green, CARD16 *blue, int size)
308{ (void) crtc; (void) red; (void) green; (void) blue; (void) size; }
309
310static void *
311vbox_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
312{ (void) crtc; (void) width; (void) height; return NULL; }
313
314static const xf86CrtcFuncsRec VBOXCrtcFuncs = {
315 .dpms = vbox_crtc_dpms,
316 .save = NULL, /* These two are never called by the server. */
317 .restore = NULL,
318 .lock = vbox_crtc_lock,
319 .unlock = NULL, /* This will not be invoked if lock returns FALSE. */
320 .mode_fixup = vbox_crtc_mode_fixup,
321 .prepare = vbox_crtc_stub,
322 .mode_set = vbox_crtc_mode_set,
323 .commit = vbox_crtc_stub,
324 .gamma_set = vbox_crtc_gamma_set,
325 .shadow_allocate = vbox_crtc_shadow_allocate,
326 .shadow_create = NULL, /* These two should not be invoked if allocate
327 returns NULL. */
328 .shadow_destroy = NULL,
329 .set_cursor_colors = NULL, /* We are still using the old cursor API. */
330 .set_cursor_position = NULL,
331 .show_cursor = NULL,
332 .hide_cursor = NULL,
333 .load_cursor_argb = NULL,
334 .destroy = vbox_crtc_stub
335};
336
337static void
338vbox_output_stub (xf86OutputPtr output)
339{ (void) output; }
340
341static void
342vbox_output_dpms (xf86OutputPtr output, int mode)
343{ (void) output; (void) mode; }
344
345static int
346vbox_output_mode_valid (xf86OutputPtr output, DisplayModePtr mode)
347{
348 ScrnInfoPtr pScrn = output->scrn;
349 int rc = MODE_OK;
350 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", mode->HDisplay, mode->VDisplay);
351 /* We always like modes specified by the user in the configuration
352 * file and modes requested by the host, as doing otherwise is likely to
353 * annoy people. */
354 if ( !(mode->type & M_T_USERDEF)
355 && !(mode->type & M_T_PREFERRED)
356 && vbox_device_available(VBOXGetRec(pScrn))
357 && !vboxHostLikesVideoMode(pScrn, mode->HDisplay, mode->VDisplay,
358 pScrn->bitsPerPixel)
359 )
360 rc = MODE_BAD;
361 TRACE_LOG("returning %s\n", MODE_OK == rc ? "MODE_OK" : "MODE_BAD");
362 return rc;
363}
364
365static Bool
366vbox_output_mode_fixup (xf86OutputPtr output, DisplayModePtr mode,
367 DisplayModePtr adjusted_mode)
368{ (void) output; (void) mode; (void) adjusted_mode; return TRUE; }
369
370static void
371vbox_output_mode_set (xf86OutputPtr output, DisplayModePtr mode,
372 DisplayModePtr adjusted_mode)
373{ (void) output; (void) mode; (void) adjusted_mode; }
374
375/* A virtual monitor is always connected. */
376static xf86OutputStatus
377vbox_output_detect (xf86OutputPtr output)
378{
379 (void) output;
380 return XF86OutputStatusConnected;
381}
382
383static void
384vbox_output_add_mode (DisplayModePtr *pModes, const char *pszName, int x, int y,
385 Bool isPreferred, Bool isUserDef)
386{
387 TRACE_LOG("pszName=%s, x=%d, y=%d\n", pszName, x, y);
388 DisplayModePtr pMode = xnfcalloc(1, sizeof(DisplayModeRec));
389
390 pMode->status = MODE_OK;
391 /* We don't ask the host whether it likes user defined modes,
392 * as we assume that the user really wanted that mode. */
393 pMode->type = isUserDef ? M_T_USERDEF : M_T_BUILTIN;
394 if (isPreferred)
395 pMode->type |= M_T_PREFERRED;
396 /* VBox only supports screen widths which are a multiple of 8 */
397 pMode->HDisplay = (x + 7) & ~7;
398 pMode->HSyncStart = pMode->HDisplay + 2;
399 pMode->HSyncEnd = pMode->HDisplay + 4;
400 pMode->HTotal = pMode->HDisplay + 6;
401 pMode->VDisplay = y;
402 pMode->VSyncStart = pMode->VDisplay + 2;
403 pMode->VSyncEnd = pMode->VDisplay + 4;
404 pMode->VTotal = pMode->VDisplay + 6;
405 pMode->Clock = pMode->HTotal * pMode->VTotal * 60 / 1000; /* kHz */
406 if (NULL == pszName) {
407 xf86SetModeDefaultName(pMode);
408 } else {
409 pMode->name = xnfstrdup(pszName);
410 }
411 *pModes = xf86ModesAdd(*pModes, pMode);
412}
413
414static DisplayModePtr
415vbox_output_get_modes (xf86OutputPtr output)
416{
417 bool rc;
418 unsigned i;
419 DisplayModePtr pModes = NULL;
420 ScrnInfoPtr pScrn = output->scrn;
421 VBOXPtr pVBox = VBOXGetRec(pScrn);
422
423 TRACE_ENTRY();
424 if (vbox_device_available(pVBox))
425 {
426 uint32_t x, y, bpp, iScreen;
427 rc = vboxGetDisplayChangeRequest(pScrn, &x, &y, &bpp, &iScreen);
428 /* @todo - check the display number once we support multiple displays. */
429 /* If we don't find a display request, see if we have a saved hint
430 * from a previous session. */
431 if (!rc || (0 == x) || (0 == y))
432 rc = vboxRetrieveVideoMode(pScrn, &x, &y, &bpp);
433 if (rc && (0 != x) && (0 != y)) {
434 /* We prefer a slightly smaller size to a slightly larger one */
435 x -= (x % 8);
436 vbox_output_add_mode(&pModes, NULL, x, y, TRUE, FALSE);
437 }
438 }
439 /* Also report any modes the user may have requested in the xorg.conf
440 * configuration file. */
441 for (i = 0; pScrn->display->modes[i] != NULL; i++)
442 {
443 int x, y;
444 if (2 == sscanf(pScrn->display->modes[i], "%dx%d", &x, &y))
445 vbox_output_add_mode(&pModes, pScrn->display->modes[i], x, y,
446 FALSE, TRUE);
447 }
448 TRACE_EXIT();
449 return pModes;
450}
451
452#ifdef RANDR_12_INTERFACE
453/* We don't yet have mutable properties, whatever they are. */
454static Bool
455vbox_output_set_property(xf86OutputPtr output, Atom property,
456 RRPropertyValuePtr value)
457{ (void) output; (void) property; (void) value; return FALSE; }
458#endif
459
460static const xf86OutputFuncsRec VBOXOutputFuncs = {
461 .create_resources = vbox_output_stub,
462 .dpms = vbox_output_dpms,
463 .save = NULL, /* These two are never called by the server. */
464 .restore = NULL,
465 .mode_valid = vbox_output_mode_valid,
466 .mode_fixup = vbox_output_mode_fixup,
467 .prepare = vbox_output_stub,
468 .commit = vbox_output_stub,
469 .mode_set = vbox_output_mode_set,
470 .detect = vbox_output_detect,
471 .get_modes = vbox_output_get_modes,
472#ifdef RANDR_12_INTERFACE
473 .set_property = vbox_output_set_property,
474#endif
475 .destroy = vbox_output_stub
476};
477
478#ifdef XFree86LOADER
479/* Module loader interface */
480static MODULESETUPPROTO(vboxSetup);
481
482static XF86ModuleVersionInfo vboxVersionRec =
483{
484 VBOX_DRIVER_NAME,
485 VBOX_VENDOR,
486 MODINFOSTRING1,
487 MODINFOSTRING2,
488 XORG_VERSION_CURRENT,
489 1, /* Module major version. Xorg-specific */
490 0, /* Module minor version. Xorg-specific */
491 1, /* Module patchlevel. Xorg-specific */
492 ABI_CLASS_VIDEODRV, /* This is a video driver */
493 ABI_VIDEODRV_VERSION,
494 MOD_CLASS_VIDEODRV,
495 {0, 0, 0, 0}
496};
497
498/*
499 * This data is accessed by the loader. The name must be the module name
500 * followed by "ModuleData".
501 */
502_X_EXPORT XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
503
504static pointer
505vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
506{
507 static Bool Initialised = FALSE;
508
509 if (!Initialised)
510 {
511 Initialised = TRUE;
512#ifdef PCIACCESS
513 xf86AddDriver(&VBOXVIDEO, Module, HaveDriverFuncs);
514#else
515 xf86AddDriver(&VBOXVIDEO, Module, 0);
516#endif
517 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXVIDEO\" is %p\n",
518 (void *)&VBOXVIDEO);
519 return (pointer)TRUE;
520 }
521
522 if (ErrorMajor)
523 *ErrorMajor = LDR_ONCEONLY;
524 return (NULL);
525}
526
527#endif /* XFree86Loader defined */
528
529static const OptionInfoRec *
530VBOXAvailableOptions(int chipid, int busid)
531{
532 return (VBOXOptions);
533}
534
535static void
536VBOXIdentify(int flags)
537{
538 xf86PrintChipsets(VBOX_NAME, "guest driver for VirtualBox", VBOXChipsets);
539}
540
541/*
542 * This function is called once, at the start of the first server generation to
543 * do a minimal probe for supported hardware.
544 */
545
546#ifdef PCIACCESS
547static Bool
548VBOXPciProbe(DriverPtr drv, int entity_num, struct pci_device *dev,
549 intptr_t match_data)
550{
551 ScrnInfoPtr pScrn;
552
553 TRACE_ENTRY();
554 pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, VBOXPCIchipsets,
555 NULL, NULL, NULL, NULL, NULL);
556 if (pScrn != NULL) {
557 VBOXPtr pVBox = VBOXGetRec(pScrn);
558
559 pScrn->driverVersion = VBOX_VERSION;
560 pScrn->driverName = VBOX_DRIVER_NAME;
561 pScrn->name = VBOX_NAME;
562 pScrn->Probe = NULL;
563 pScrn->PreInit = VBOXPreInit;
564 pScrn->ScreenInit = VBOXScreenInit;
565 pScrn->SwitchMode = VBOXSwitchMode;
566 pScrn->ValidMode = VBOXValidMode;
567 pScrn->AdjustFrame = VBOXAdjustFrame;
568 pScrn->EnterVT = VBOXEnterVT;
569 pScrn->LeaveVT = VBOXLeaveVT;
570 pScrn->FreeScreen = VBOXFreeScreen;
571
572 pVBox->pciInfo = dev;
573 }
574
575 TRACE_LOG("returning %s\n", BOOL_STR(pScrn != NULL));
576 return (pScrn != NULL);
577}
578#endif
579
580#ifndef PCIACCESS
581static Bool
582VBOXProbe(DriverPtr drv, int flags)
583{
584 Bool foundScreen = FALSE;
585 int numDevSections;
586 GDevPtr *devSections;
587
588 /*
589 * Find the config file Device sections that match this
590 * driver, and return if there are none.
591 */
592 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
593 &devSections)) <= 0)
594 return (FALSE);
595
596 /* PCI BUS */
597 if (xf86GetPciVideoInfo()) {
598 int numUsed;
599 int *usedChips;
600 int i;
601 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
602 VBOXChipsets, VBOXPCIchipsets,
603 devSections, numDevSections,
604 drv, &usedChips);
605 if (numUsed > 0) {
606 if (flags & PROBE_DETECT)
607 foundScreen = TRUE;
608 else {
609 for (i = 0; i < numUsed; i++) {
610 ScrnInfoPtr pScrn = NULL;
611 /* Allocate a ScrnInfoRec */
612 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
613 VBOXPCIchipsets,NULL,
614 NULL,NULL,NULL,NULL))) {
615 pScrn->driverVersion = VBOX_VERSION;
616 pScrn->driverName = VBOX_DRIVER_NAME;
617 pScrn->name = VBOX_NAME;
618 pScrn->Probe = VBOXProbe;
619 pScrn->PreInit = VBOXPreInit;
620 pScrn->ScreenInit = VBOXScreenInit;
621 pScrn->SwitchMode = VBOXSwitchMode;
622 pScrn->ValidMode = VBOXValidMode;
623 pScrn->AdjustFrame = VBOXAdjustFrame;
624 pScrn->EnterVT = VBOXEnterVT;
625 pScrn->LeaveVT = VBOXLeaveVT;
626 pScrn->FreeScreen = VBOXFreeScreen;
627 foundScreen = TRUE;
628 }
629 }
630 }
631 xfree(usedChips);
632 }
633 }
634
635 xfree(devSections);
636
637 return (foundScreen);
638}
639#endif
640
641/*
642 * QUOTE from the XFree86 DESIGN document:
643 *
644 * The purpose of this function is to find out all the information
645 * required to determine if the configuration is usable, and to initialise
646 * those parts of the ScrnInfoRec that can be set once at the beginning of
647 * the first server generation.
648 *
649 * (...)
650 *
651 * This includes probing for video memory, clocks, ramdac, and all other
652 * HW info that is needed. It includes determining the depth/bpp/visual
653 * and related info. It includes validating and determining the set of
654 * video modes that will be used (and anything that is required to
655 * determine that).
656 *
657 * This information should be determined in the least intrusive way
658 * possible. The state of the HW must remain unchanged by this function.
659 * Although video memory (including MMIO) may be mapped within this
660 * function, it must be unmapped before returning.
661 *
662 * END QUOTE
663 */
664
665static Bool
666VBOXPreInit(ScrnInfoPtr pScrn, int flags)
667{
668 VBOXPtr pVBox;
669 Gamma gzeros = {0.0, 0.0, 0.0};
670 rgb rzeros = {0, 0, 0};
671 xf86OutputPtr output;
672
673 /* Are we really starting the server, or is this just a dummy run? */
674 if (flags & PROBE_DETECT)
675 return (FALSE);
676
677 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
678 "VirtualBox guest additions video driver version "
679 VBOX_VERSION_STRING "\n");
680
681 /* Get our private data from the ScrnInfoRec structure. */
682 pVBox = VBOXGetRec(pScrn);
683
684 /* Initialise the guest library */
685 vbox_init(pScrn->scrnIndex, pVBox);
686
687 /* Entity information seems to mean bus information. */
688 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
689
690 /* We need the vbe module because we use VBE code to save and restore
691 text mode, in order to keep our code simple. */
692 if (!xf86LoadSubModule(pScrn, "vbe"))
693 return (FALSE);
694
695 if ((pVBox->pVbe = VBEExtendedInit(NULL, pVBox->pEnt->index,
696 SET_BIOS_SCRATCH
697 | RESTORE_BIOS_SCRATCH)) == NULL)
698 return (FALSE);
699
700#ifndef PCIACCESS
701 if (pVBox->pEnt->location.type != BUS_PCI)
702 return FALSE;
703
704 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
705 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
706 pVBox->pciInfo->device,
707 pVBox->pciInfo->func);
708#endif
709
710 /* The ramdac module is needed for the hardware cursor. */
711 if (!xf86LoadSubModule(pScrn, "ramdac"))
712 return FALSE;
713
714 /* The framebuffer module. */
715 if (xf86LoadSubModule(pScrn, "fb") == NULL)
716 return (FALSE);
717
718 if (!xf86LoadSubModule(pScrn, "shadowfb"))
719 return FALSE;
720
721 if (!xf86LoadSubModule(pScrn, "vgahw"))
722 return FALSE;
723
724 /* Set up our ScrnInfoRec structure to describe our virtual
725 capabilities to X. */
726
727 pScrn->chipset = "vbox";
728
729 /* This *is* still needed, at least for server version 1.3 */
730 pScrn->monitor = pScrn->confScreen->monitor;
731
732 pScrn->progClock = TRUE;
733 pScrn->rgbBits = 8;
734
735 /* Using the PCI information caused problems with non-powers-of-two
736 sized video RAM configurations */
737 pScrn->videoRam = inl(VBE_DISPI_IOPORT_DATA) / 1024;
738
739 /* Query the host for the preferred colour depth */
740 {
741 uint32_t cx, cy, iDisplay, cBits = 24;
742
743 if (vbox_device_available(pVBox))
744 {
745 /* We only support 16 and 24 bits depth (i.e. 16 and 32bpp) */
746 if ( vboxGetDisplayChangeRequest(pScrn, &cx, &cy, &cBits,
747 &iDisplay)
748 && (cBits != 16)
749 )
750 cBits = 24;
751 }
752 if (!xf86SetDepthBpp(pScrn, cBits, 0, 0, Support32bppFb))
753 return FALSE;
754 }
755 if (pScrn->bitsPerPixel != 32 && pScrn->bitsPerPixel != 16)
756 {
757 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
758 "The VBox additions only support 16 and 32bpp graphics modes\n");
759 return FALSE;
760 }
761 xf86PrintDepthBpp(pScrn);
762
763 /* options */
764 xf86CollectOptions(pScrn, NULL);
765 if (!(pVBox->Options = xalloc(sizeof(VBOXOptions))))
766 return FALSE;
767 memcpy(pVBox->Options, VBOXOptions, sizeof(VBOXOptions));
768 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pVBox->Options);
769
770 /* Work around a bug in the original X server modesetting code, which
771 * took the first valid values set to these two as maxima over the
772 * server lifetime. */
773 pScrn->virtualX = 32000;
774 pScrn->virtualY = 32000;
775
776 /* Initialise CRTC and output configuration for use with randr1.2. */
777 xf86CrtcConfigInit(pScrn, &VBOXCrtcConfigFuncs);
778
779 /* Setup our single virtual CRTC. */
780 xf86CrtcCreate(pScrn, &VBOXCrtcFuncs);
781
782 /* Set up our single virtual output. */
783 output = xf86OutputCreate(pScrn, &VBOXOutputFuncs, "VBOX1");
784
785 /* Set a sane minimum and maximum mode size */
786 xf86CrtcSetSizeRange(pScrn, 64, 64, 32000, 32000);
787
788 /* We are not interested in the monitor section in the configuration file. */
789 xf86OutputUseScreenMonitor(output, FALSE);
790 output->possible_crtcs = 1;
791 output->possible_clones = 0;
792
793 /* Now create our initial CRTC/output configuration. */
794 if (!xf86InitialConfiguration(pScrn, TRUE)) {
795 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial CRTC configuration failed!\n");
796 return (FALSE);
797 }
798
799 /* Colour weight - we always call this, since we are always in
800 truecolour. */
801 if (!xf86SetWeight(pScrn, rzeros, rzeros))
802 return (FALSE);
803
804 /* visual init */
805 if (!xf86SetDefaultVisual(pScrn, -1))
806 return (FALSE);
807
808 xf86SetGamma(pScrn, gzeros);
809
810 /* Set a default display resolution. */
811 xf86SetDpi(pScrn, 96, 96);
812
813 /* Framebuffer-related setup */
814 pScrn->bitmapBitOrder = BITMAP_BIT_ORDER;
815
816 /* VGA hardware initialisation */
817 if (!vgaHWGetHWRec(pScrn))
818 return FALSE;
819
820#ifdef VBOX_DRI
821 /* Load the dri module. */
822 if (!xf86LoadSubModule(pScrn, "dri"))
823 return FALSE;
824#endif
825 return (TRUE);
826}
827
828/**
829 * This function hooks into the chain that is called when framebuffer access
830 * is allowed or disallowed by a call to EnableDisableFBAccess in the server.
831 * In other words, it observes when the server wishes access to the
832 * framebuffer to be enabled and when it should be disabled. We need to know
833 * this because we disable access ourselves during mode switches (presumably
834 * the server should do this but it doesn't) and want to know whether to
835 * restore it or not afterwards.
836 */
837static void
838vboxEnableDisableFBAccess(int scrnIndex, Bool enable)
839{
840 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
841 VBOXPtr pVBox = VBOXGetRec(pScrn);
842
843 TRACE_LOG("enable=%s\n", enable ? "TRUE" : "FALSE");
844 pVBox->accessEnabled = enable;
845 pVBox->EnableDisableFBAccess(scrnIndex, enable);
846 TRACE_EXIT();
847}
848
849/*
850 * QUOTE from the XFree86 DESIGN document:
851 *
852 * This is called at the start of each server generation.
853 *
854 * (...)
855 *
856 * Decide which operations need to be placed under resource access
857 * control. (...) Map any video memory or other memory regions. (...)
858 * Save the video card state. (...) Initialise the initial video
859 * mode.
860 *
861 * End QUOTE.Initialise the initial video mode.
862 */
863static Bool
864VBOXScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
865{
866 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
867 VBOXPtr pVBox = VBOXGetRec(pScrn);
868 VisualPtr visual;
869 unsigned flags;
870
871 if (pVBox->mapPhys == 0) {
872#ifdef PCIACCESS
873 pVBox->mapPhys = pVBox->pciInfo->regions[0].base_addr;
874#else
875 pVBox->mapPhys = pVBox->pciInfo->memBase[0];
876#endif
877/* pVBox->mapSize = 1 << pVBox->pciInfo->size[0]; */
878 /* Using the PCI information caused problems with
879 non-powers-of-two sized video RAM configurations */
880 pVBox->mapSize = inl(VBE_DISPI_IOPORT_DATA);
881 pVBox->mapOff = 0;
882 }
883
884 if (!VBOXMapVidMem(pScrn))
885 return (FALSE);
886
887 /* save current video state */
888 VBOXSaveRestore(pScrn, MODE_SAVE);
889 pVBox->savedPal = VBESetGetPaletteData(pVBox->pVbe, FALSE, 0, 256,
890 NULL, FALSE, FALSE);
891
892 /* mi layer - reset the visual list (?)*/
893 miClearVisualTypes();
894 if (!xf86SetDefaultVisual(pScrn, -1))
895 return (FALSE);
896 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
897 pScrn->rgbBits, TrueColor))
898 return (FALSE);
899 if (!miSetPixmapDepths())
900 return (FALSE);
901
902 /* Needed before we initialise DRI. */
903 pScrn->virtualX = (pScrn->virtualX + 7) & ~7;
904 pScrn->displayWidth = pScrn->virtualX;
905
906#ifdef VBOX_DRI
907 pVBox->useDRI = VBOXDRIScreenInit(scrnIndex, pScreen, pVBox);
908#endif
909
910 /* I checked in the sources, and XFree86 4.2 does seem to support
911 this function for 32bpp. */
912 if (!fbScreenInit(pScreen, pVBox->base,
913 pScrn->virtualX, pScrn->virtualY,
914 pScrn->xDpi, pScrn->yDpi,
915 pScrn->displayWidth, pScrn->bitsPerPixel))
916 return (FALSE);
917
918 /* Fixup RGB ordering */
919 visual = pScreen->visuals + pScreen->numVisuals;
920 while (--visual >= pScreen->visuals) {
921 if ((visual->class | DynamicClass) == DirectColor) {
922 visual->offsetRed = pScrn->offset.red;
923 visual->offsetGreen = pScrn->offset.green;
924 visual->offsetBlue = pScrn->offset.blue;
925 visual->redMask = pScrn->mask.red;
926 visual->greenMask = pScrn->mask.green;
927 visual->blueMask = pScrn->mask.blue;
928 }
929 }
930
931 /* must be after RGB ordering fixed */
932 fbPictureInit(pScreen, 0, 0);
933
934 xf86SetBlackWhitePixels(pScreen);
935 miInitializeBackingStore(pScreen);
936 xf86SetBackingStore(pScreen);
937
938 /* We need to keep track of whether we are currently switched to a virtual
939 * terminal to know whether a mode set operation is currently safe to do.
940 */
941 pVBox->vtSwitch = FALSE;
942 /* Initialise DGA. The cast is unfortunately correct - it gets cast back
943 to (unsigned char *) later. */
944 xf86DiDGAInit(pScreen, (unsigned long) pVBox->base);
945
946 /* Initialise randr 1.2 mode-setting functions and set first mode. */
947 if (!xf86CrtcScreenInit(pScreen)) {
948 return FALSE;
949 }
950
951 if (!xf86SetDesiredModes(pScrn)) {
952 return FALSE;
953 }
954
955 /* software cursor */
956 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
957
958 /* colourmap code */
959 if (!miCreateDefColormap(pScreen))
960 return (FALSE);
961
962 flags = CMAP_RELOAD_ON_MODE_SWITCH;
963
964 if(!vgaHWHandleColormaps(pScreen))
965 return (FALSE);
966
967 /* Hook our observer function ito the chain which is called when
968 * framebuffer access is enabled or disabled in the server, and
969 * assume an initial state of enabled. */
970 pVBox->accessEnabled = TRUE;
971 pVBox->EnableDisableFBAccess = pScrn->EnableDisableFBAccess;
972 pScrn->EnableDisableFBAccess = vboxEnableDisableFBAccess;
973
974 pVBox->CloseScreen = pScreen->CloseScreen;
975 pScreen->CloseScreen = VBOXCloseScreen;
976 pScreen->SaveScreen = xf86SaveScreen;
977
978 /* We probably do want to support power management - even if we just use
979 a dummy function. */
980 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
981
982 /* Report any unused options (only for the first generation) */
983 if (serverGeneration == 1)
984 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
985
986 if (vbox_device_available(pVBox) && vbox_open (pScrn, pScreen, pVBox)) {
987 if (vbox_cursor_init(pScreen) != TRUE)
988 xf86DrvMsg(scrnIndex, X_ERROR,
989 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
990 if (vboxEnableVbva(pScrn) == TRUE)
991 xf86DrvMsg(scrnIndex, X_INFO,
992 "The VBox video extensions are now enabled.\n");
993 vboxEnableGraphicsCap(pVBox);
994 }
995
996#ifdef VBOX_DRI
997 if (pVBox->useDRI)
998 pVBox->useDRI = VBOXDRIFinishScreenInit(pScreen);
999#endif
1000 return (TRUE);
1001}
1002
1003static Bool
1004VBOXEnterVT(int scrnIndex, int flags)
1005{
1006 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1007 VBOXPtr pVBox = VBOXGetRec(pScrn);
1008 bool rc;
1009
1010 TRACE_ENTRY();
1011 pVBox->vtSwitch = FALSE;
1012#ifdef VBOX_DRI
1013 if (pVBox->useDRI)
1014 DRIUnlock(screenInfo.screens[scrnIndex]);
1015#endif
1016 rc = xf86SetDesiredModes(pScrn);
1017 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1018 return rc;
1019}
1020
1021static void
1022VBOXLeaveVT(int scrnIndex, int flags)
1023{
1024 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1025 VBOXPtr pVBox = VBOXGetRec(pScrn);
1026
1027 TRACE_ENTRY();
1028 pVBox->vtSwitch = TRUE;
1029 VBOXSaveRestore(pScrn, MODE_RESTORE);
1030 if (vbox_device_available(pVBox))
1031 {
1032 if (pVBox->useVbva == TRUE)
1033 vboxDisableVbva(pScrn);
1034 vboxDisableGraphicsCap(pVBox);
1035 }
1036#ifdef VBOX_DRI
1037 if (pVBox->useDRI)
1038 DRILock(screenInfo.screens[scrnIndex], 0);
1039#endif
1040 TRACE_EXIT();
1041}
1042
1043static Bool
1044VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen)
1045{
1046 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1047 VBOXPtr pVBox = VBOXGetRec(pScrn);
1048
1049#ifdef VBOX_DRI
1050 if (pVBox->useDRI)
1051 VBOXDRICloseScreen(pScreen, pVBox);
1052 pVBox->useDRI = false;
1053#endif
1054
1055 if (vbox_device_available(pVBox))
1056 {
1057 if (TRUE == pVBox->useVbva)
1058 vboxDisableVbva(pScrn);
1059 vboxDisableGraphicsCap(pVBox);
1060 }
1061 if (pScrn->vtSema) {
1062 VBOXSaveRestore(xf86Screens[scrnIndex], MODE_RESTORE);
1063 if (pVBox->savedPal)
1064 VBESetGetPaletteData(pVBox->pVbe, TRUE, 0, 256,
1065 pVBox->savedPal, FALSE, TRUE);
1066 VBOXUnmapVidMem(pScrn);
1067 }
1068 pScrn->vtSema = FALSE;
1069
1070 /* Destroy the VGA hardware record */
1071 vgaHWFreeHWRec(pScrn);
1072
1073 /* Remove our observer functions from the X server call chains. */
1074 pScrn->EnableDisableFBAccess = pVBox->EnableDisableFBAccess;
1075 pScreen->CloseScreen = pVBox->CloseScreen;
1076 return pScreen->CloseScreen(scrnIndex, pScreen);
1077}
1078
1079/**
1080 * Quoted from "How to add an (S)VGA driver to XFree86"
1081 * (http://www.xfree86.org/3.3.6/VGADriver.html):
1082 *
1083 * The ValidMode() function is required. It is used to check for any
1084 * chipset-dependent reasons why a graphics mode might not be valid. It gets
1085 * called by higher levels of the code after the Probe() stage. In many cases
1086 * no special checking will be required and this function will simply return
1087 * TRUE always.
1088 *
1089 * Note: we check here that our generated video modes fulfil the X server's
1090 * criteria for the monitor, since this can otherwise cause problems in
1091 * randr 1.2.
1092 */
1093static ModeStatus
1094VBOXValidMode(int scrn, DisplayModePtr p, Bool flag, int pass)
1095{
1096 static int warned = 0;
1097 ScrnInfoPtr pScrn = xf86Screens[scrn];
1098 MonPtr mon = pScrn->monitor;
1099 ModeStatus ret = MODE_BAD;
1100 DisplayModePtr mode;
1101 float v;
1102
1103 TRACE_LOG("HDisplay=%d, VDisplay=%d, flag=%s, pass=%d\n",
1104 p->HDisplay, p->VDisplay, flag ? "TRUE" : "FALSE", pass);
1105 if (pass != MODECHECK_FINAL) {
1106 if (!warned) {
1107 xf86DrvMsg(scrn, X_WARNING, "VBOXValidMode called unexpectedly\n");
1108 warned = 1;
1109 }
1110 }
1111#if 0
1112 /*
1113 * First off, if this isn't a mode we handed to the server (ie,
1114 * M_T_BUILTIN), then we reject it out of hand.
1115 */
1116 if (!(p->type & M_T_BUILTIN))
1117 return MODE_NOMODE;
1118#endif
1119 /*
1120 * Finally, walk through the vsync rates 1Hz at a time looking for a mode
1121 * that will fit. This is assuredly a terrible way to do this, but
1122 * there's no obvious method for computing a mode of a given size that
1123 * will pass xf86CheckModeForMonitor.
1124 */
1125 for (v = mon->vrefresh[0].lo; v <= mon->vrefresh[0].hi; v++) {
1126 mode = xf86CVTMode(p->HDisplay, p->VDisplay, v, 0, 0);
1127 ret = xf86CheckModeForMonitor(mode, mon);
1128 xfree(mode);
1129 if (ret == MODE_OK)
1130 break;
1131 }
1132
1133 if (ret != MODE_OK)
1134 {
1135 xf86DrvMsg(scrn, X_WARNING, "Graphics mode %s rejected by the X server\n", p->name);
1136 }
1137 TRACE_LOG("returning %d\n", ret);
1138 return ret;
1139}
1140
1141static Bool
1142VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags)
1143{
1144 ScrnInfoPtr pScrn;
1145 VBOXPtr pVBox;
1146 Bool rc;
1147
1148 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", pMode->HDisplay, pMode->VDisplay);
1149 pScrn = xf86Screens[scrnIndex]; /* Why does X have three ways of refering to the screen? */
1150 pVBox = VBOXGetRec(pScrn);
1151 /* We want to disable access to the framebuffer before switching mode.
1152 * After doing the switch, we allow access if it was allowed before. */
1153 if (pVBox->accessEnabled)
1154 pVBox->EnableDisableFBAccess(scrnIndex, FALSE);
1155 rc = xf86SetSingleMode(pScrn, pMode, 0);
1156 if (pVBox->accessEnabled)
1157 pVBox->EnableDisableFBAccess(scrnIndex, TRUE);
1158 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1159 return rc;
1160}
1161
1162/* Set a graphics mode. Poke the required values into registers, enable
1163 guest-host acceleration functions and tell the host we support advanced
1164 graphics functions. */
1165static Bool
1166VBOXSetMode(ScrnInfoPtr pScrn, DisplayModePtr pMode)
1167{
1168 VBOXPtr pVBox;
1169 Bool rc = TRUE;
1170
1171 int bpp = pScrn->depth == 24 ? 32 : 16;
1172 TRACE_LOG("HDisplay=%d, VDisplay=%d, displayWidth=%d\n",
1173 pMode->HDisplay, pMode->VDisplay, pScrn->displayWidth);
1174 pVBox = VBOXGetRec(pScrn);
1175 /* Don't fiddle with the hardware if we are switched
1176 * to a virtual terminal. */
1177 if (!pVBox->vtSwitch)
1178 {
1179 if ( vbox_device_available(pVBox)
1180 && (TRUE == pVBox->useVbva)
1181 && (vboxDisableVbva(pScrn) != TRUE)
1182 ) /* This would be bad. */
1183 rc = FALSE;
1184 if (rc)
1185 {
1186 /* Disable linear framebuffer mode before making changes to the resolution. */
1187 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
1188 outw(VBE_DISPI_IOPORT_DATA, VBE_DISPI_DISABLED);
1189 /* Unlike the resolution, the depth is fixed for a given screen
1190 for the lifetime of the X session. */
1191 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BPP);
1192 outw(VBE_DISPI_IOPORT_DATA, bpp);
1193 /* HDisplay and VDisplay are actually monitor information about
1194 the display part of the scanlines. */
1195 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_XRES);
1196 outw(VBE_DISPI_IOPORT_DATA, pMode->HDisplay);
1197 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_YRES);
1198 outw(VBE_DISPI_IOPORT_DATA, pMode->VDisplay);
1199 /* Set the virtual resolution. We are still using VESA to control
1200 the virtual offset. */
1201 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_WIDTH);
1202 outw(VBE_DISPI_IOPORT_DATA, pScrn->displayWidth);
1203 /* Enable linear framebuffer mode. */
1204 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
1205 outw(VBE_DISPI_IOPORT_DATA,
1206 VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
1207 /* Enable acceleration and tell the host we support graphics */
1208 if (vbox_device_available(pVBox))
1209 {
1210 if ((TRUE == pVBox->useVbva) && (vboxEnableVbva(pScrn) != TRUE))
1211 /* Bad but not fatal */
1212 pVBox->useVbva = FALSE;
1213 vboxEnableGraphicsCap(pVBox);
1214 }
1215 }
1216 }
1217 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1218 return rc;
1219}
1220
1221static void
1222VBOXAdjustFrame(int scrnIndex, int x, int y, int flags)
1223{
1224 VBOXPtr pVBox = VBOXGetRec(xf86Screens[scrnIndex]);
1225 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1226
1227 TRACE_ENTRY();
1228 /* Don't fiddle with the hardware if we are switched
1229 * to a virtual terminal. */
1230 if (!pVBox->vtSwitch) {
1231 pVBox->viewportX = x;
1232 pVBox->viewportY = y;
1233 /* If VBVA is enabled the graphics card will not notice the change. */
1234 if (pVBox->useVbva == TRUE)
1235 vboxDisableVbva(pScrn);
1236 VBESetDisplayStart(pVBox->pVbe, x, y, TRUE);
1237 if (pVBox->useVbva == TRUE)
1238 vboxEnableVbva(pScrn);
1239 }
1240 TRACE_EXIT();
1241}
1242
1243static void
1244VBOXFreeScreen(int scrnIndex, int flags)
1245{
1246 VBOXFreeRec(xf86Screens[scrnIndex]);
1247}
1248
1249static Bool
1250VBOXMapVidMem(ScrnInfoPtr pScrn)
1251{
1252 VBOXPtr pVBox = VBOXGetRec(pScrn);
1253 Bool rc = TRUE;
1254
1255 TRACE_ENTRY();
1256 if (!pVBox->base)
1257 {
1258 pScrn->memPhysBase = pVBox->mapPhys;
1259 pScrn->fbOffset = pVBox->mapOff;
1260
1261#ifdef PCIACCESS
1262 (void) pci_device_map_range(pVBox->pciInfo,
1263 pScrn->memPhysBase,
1264 pVBox->mapSize,
1265 PCI_DEV_MAP_FLAG_WRITABLE,
1266 & pVBox->base);
1267#else
1268 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
1269 VIDMEM_FRAMEBUFFER,
1270 pVBox->pciTag, pVBox->mapPhys,
1271 (unsigned) pVBox->mapSize);
1272#endif
1273 if (pVBox->base)
1274 {
1275 /* We need this for saving/restoring textmode */
1276 VGAHWPTR(pScrn)->IOBase = pScrn->domainIOBase;
1277 rc = vgaHWMapMem(pScrn);
1278 }
1279 else
1280 rc = FALSE;
1281 }
1282 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1283 return rc;
1284}
1285
1286static void
1287VBOXUnmapVidMem(ScrnInfoPtr pScrn)
1288{
1289 VBOXPtr pVBox = VBOXGetRec(pScrn);
1290
1291 TRACE_ENTRY();
1292 if (pVBox->base == NULL)
1293 return;
1294
1295#ifdef PCIACCESS
1296 (void) pci_device_unmap_range(pVBox->pciInfo,
1297 pVBox->base,
1298 pVBox->mapSize);
1299#else
1300 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
1301 (unsigned) pVBox->mapSize);
1302#endif
1303 vgaHWUnmapMem(pScrn);
1304 pVBox->base = NULL;
1305 TRACE_EXIT();
1306}
1307
1308
1309Bool
1310VBOXSaveRestore(ScrnInfoPtr pScrn, vbeSaveRestoreFunction function)
1311{
1312 VBOXPtr pVBox;
1313 Bool rc = TRUE;
1314
1315 TRACE_ENTRY();
1316 if (MODE_QUERY < 0 || function > MODE_RESTORE)
1317 rc = FALSE;
1318
1319 if (rc)
1320 {
1321 pVBox = VBOXGetRec(pScrn);
1322
1323 /* Query amount of memory to save state */
1324 if (function == MODE_QUERY ||
1325 (function == MODE_SAVE && pVBox->state == NULL))
1326 {
1327
1328 /* Make sure we save at least this information in case of failure */
1329 (void)VBEGetVBEMode(pVBox->pVbe, &pVBox->stateMode);
1330 vgaHWSaveFonts(pScrn, &pVBox->vgaRegs);
1331
1332 if (!VBESaveRestore(pVBox->pVbe,function,(pointer)&pVBox->state,
1333 &pVBox->stateSize,&pVBox->statePage)
1334 )
1335 rc = FALSE;
1336 }
1337 }
1338 if (rc)
1339 {
1340 /* Save/Restore Super VGA state */
1341 if (function != MODE_QUERY) {
1342
1343 if (function == MODE_RESTORE)
1344 memcpy(pVBox->state, pVBox->pstate,
1345 (unsigned) pVBox->stateSize);
1346
1347 if ( (rc = VBESaveRestore(pVBox->pVbe,function,
1348 (pointer)&pVBox->state,
1349 &pVBox->stateSize,&pVBox->statePage)
1350 )
1351 && (function == MODE_SAVE)
1352 )
1353 {
1354 /* don't rely on the memory not being touched */
1355 if (pVBox->pstate == NULL)
1356 pVBox->pstate = xalloc(pVBox->stateSize);
1357 memcpy(pVBox->pstate, pVBox->state,
1358 (unsigned) pVBox->stateSize);
1359 }
1360
1361 if (function == MODE_RESTORE)
1362 {
1363 VBESetVBEMode(pVBox->pVbe, pVBox->stateMode, NULL);
1364 vgaHWRestoreFonts(pScrn, &pVBox->vgaRegs);
1365 }
1366 }
1367 }
1368 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1369 return rc;
1370}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette