VirtualBox

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

Last change on this file since 35881 was 35881, checked in by vboxsync, 14 years ago

Additions/x11/vboxvideo: clean up unneeded symbols

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.2 KB
Line 
1/* $Id: vboxvideo.c 35881 2011-02-07 22:19:31Z vboxsync $ */
2/** @file
3 *
4 * Linux Additions X11 graphics driver
5 */
6
7/*
8 * Copyright (C) 2006-2010 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * --------------------------------------------------------------------
18 *
19 * This code is based on:
20 *
21 * X11 VESA driver
22 *
23 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a
26 * copy of this software and associated documentation files (the "Software"),
27 * to deal in the Software without restriction, including without limitation
28 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
29 * and/or sell copies of the Software, and to permit persons to whom the
30 * Software is furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
39 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
40 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 * SOFTWARE.
42 *
43 * Except as contained in this notice, the name of Conectiva Linux shall
44 * not be used in advertising or otherwise to promote the sale, use or other
45 * dealings in this Software without prior written authorization from
46 * Conectiva Linux.
47 *
48 * Authors: Paulo César Pereira de Andrade <[email protected]>
49 */
50
51#ifdef XORG_7X
52# include "xorg-server.h"
53# include <string.h>
54#endif
55#include "vboxvideo.h"
56#include <iprt/asm-math.h>
57#include "version-generated.h"
58#include "product-generated.h"
59#include <xf86.h>
60#include <misc.h>
61
62/* All drivers initialising the SW cursor need this */
63#include "mipointer.h"
64
65/* All drivers implementing backing store need this */
66#include "mibstore.h"
67
68/* Colormap handling */
69#include "micmap.h"
70#include "xf86cmap.h"
71
72/* DPMS */
73/* #define DPMS_SERVER
74#include "extensions/dpms.h" */
75
76/* VGA hardware functions for setting and restoring text mode */
77#include "vgaHW.h"
78
79#ifdef VBOXVIDEO_13
80/* X.org 1.3+ mode setting */
81# define _HAVE_STRING_ARCH_strsep /* bits/string2.h, __strsep_1c. */
82# include "xf86Crtc.h"
83# include "xf86Modes.h"
84# include <X11/Xatom.h>
85#endif
86
87/* Mandatory functions */
88
89static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
90static void VBOXIdentify(int flags);
91#ifndef PCIACCESS
92static Bool VBOXProbe(DriverPtr drv, int flags);
93#else
94static Bool VBOXPciProbe(DriverPtr drv, int entity_num,
95 struct pci_device *dev, intptr_t match_data);
96#endif
97static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
98static Bool VBOXScreenInit(int Index, ScreenPtr pScreen, int argc,
99 char **argv);
100static Bool VBOXEnterVT(int scrnIndex, int flags);
101static void VBOXLeaveVT(int scrnIndex, int flags);
102static Bool VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen);
103static Bool VBOXSaveScreen(ScreenPtr pScreen, int mode);
104static Bool VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags);
105static Bool VBOXSetMode(ScrnInfoPtr pScrn, unsigned cDisplay, unsigned cWidth,
106 unsigned cHeight, int x, int y);
107static void VBOXAdjustFrame(int scrnIndex, int x, int y, int flags);
108static void VBOXFreeScreen(int scrnIndex, int flags);
109static void VBOXFreeRec(ScrnInfoPtr pScrn);
110static void VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
111 int flags);
112
113/* locally used functions */
114static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
115static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
116static Bool VBOXSaveRestore(ScrnInfoPtr pScrn,
117 vbeSaveRestoreFunction function);
118static Bool VBOXAdjustScreenPixmap(ScrnInfoPtr pScrn, int width, int height);
119
120enum GenericTypes
121{
122 CHIP_VBOX_GENERIC
123};
124
125#ifdef PCIACCESS
126static const struct pci_id_match vbox_device_match[] = {
127 {
128 VBOX_VENDORID, VBOX_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
129 0, 0, 0
130 },
131
132 { 0, 0, 0 },
133};
134#endif
135
136/* Supported chipsets */
137static SymTabRec VBOXChipsets[] =
138{
139 {VBOX_DEVICEID, "vbox"},
140 {-1, NULL}
141};
142
143static PciChipsets VBOXPCIchipsets[] = {
144 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
145 { -1, -1, RES_UNDEFINED },
146};
147
148/*
149 * This contains the functions needed by the server after loading the
150 * driver module. It must be supplied, and gets added the driver list by
151 * the Module Setup function in the dynamic case. In the static case a
152 * reference to this is compiled in, and this requires that the name of
153 * this DriverRec be an upper-case version of the driver name.
154 */
155
156#ifdef XORG_7X
157_X_EXPORT
158#endif
159DriverRec VBOXVIDEO = {
160 VBOX_VERSION,
161 VBOX_DRIVER_NAME,
162 VBOXIdentify,
163#ifdef PCIACCESS
164 NULL,
165#else
166 VBOXProbe,
167#endif
168 VBOXAvailableOptions,
169 NULL,
170 0,
171#ifdef XORG_7X
172 NULL,
173#endif
174#ifdef PCIACCESS
175 vbox_device_match,
176 VBOXPciProbe
177#endif
178};
179
180/* No options for now */
181static const OptionInfoRec VBOXOptions[] = {
182 { -1, NULL, OPTV_NONE, {0}, FALSE }
183};
184
185#ifndef XORG_7X
186/*
187 * List of symbols from other modules that this module references. This
188 * list is used to tell the loader that it is OK for symbols here to be
189 * unresolved providing that it hasn't been told that they haven't been
190 * told that they are essential via a call to xf86LoaderReqSymbols() or
191 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
192 * unresolved symbols that are not required.
193 */
194static const char *fbSymbols[] = {
195 "fbPictureInit",
196 "fbScreenInit",
197 NULL
198};
199
200static const char *shadowfbSymbols[] = {
201 "ShadowFBInit2",
202 NULL
203};
204
205static const char *vbeSymbols[] = {
206 "VBEExtendedInit",
207 "VBEGetVBEMode",
208 "VBESaveRestore",
209 "VBESetVBEMode",
210 NULL
211};
212
213static const char *ramdacSymbols[] = {
214 "xf86InitCursor",
215 "xf86CreateCursorInfoRec",
216 NULL
217};
218
219static const char *vgahwSymbols[] = {
220 "vgaHWGetHWRec",
221 "vgaHWGetIndex",
222 "vgaHWFreeHWRec",
223 "vgaHWMapMem",
224 "vgaHWUnmapMem",
225 "vgaHWSaveFonts",
226 "vgaHWRestoreFonts",
227 NULL
228};
229#endif /* !XORG_7X */
230
231static VBOXPtr
232VBOXGetRec(ScrnInfoPtr pScrn)
233{
234 if (!pScrn->driverPrivate)
235 {
236 pScrn->driverPrivate = calloc(sizeof(VBOXRec), 1);
237 }
238
239 return ((VBOXPtr)pScrn->driverPrivate);
240}
241
242static void
243VBOXFreeRec(ScrnInfoPtr pScrn)
244{
245 VBOXPtr pVBox = VBOXGetRec(pScrn);
246 free(pVBox->savedPal);
247 free(pVBox->fonts);
248 free(pScrn->driverPrivate);
249 pScrn->driverPrivate = NULL;
250}
251
252#ifdef VBOXVIDEO_13
253/* X.org 1.3+ mode-setting support ******************************************/
254
255/* For descriptions of these functions and structures, see
256 hw/xfree86/modes/xf86Crtc.h and hw/xfree86/modes/xf86Modes.h in the
257 X.Org source tree. */
258
259static const xf86CrtcConfigFuncsRec VBOXCrtcConfigFuncs = {
260 VBOXAdjustScreenPixmap
261};
262
263static void
264vbox_crtc_dpms(xf86CrtcPtr crtc, int mode)
265{
266 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
267 unsigned cDisplay = (uintptr_t)crtc->driver_private;
268 TRACE_LOG("cDisplay=%u, mode=%i\n", cDisplay, mode);
269 pVBox->afDisabled[cDisplay] = (mode != DPMSModeOn);
270 if ( pVBox->aScreenLocation[cDisplay].cx
271 && pVBox->aScreenLocation[cDisplay].cy)
272 VBOXSetMode(crtc->scrn, cDisplay,
273 pVBox->aScreenLocation[cDisplay].cx,
274 pVBox->aScreenLocation[cDisplay].cy,
275 pVBox->aScreenLocation[cDisplay].x,
276 pVBox->aScreenLocation[cDisplay].y);
277}
278
279static Bool
280vbox_crtc_lock (xf86CrtcPtr crtc)
281{ (void) crtc; return FALSE; }
282
283static Bool
284vbox_crtc_mode_fixup (xf86CrtcPtr crtc, DisplayModePtr mode,
285 DisplayModePtr adjusted_mode)
286{ (void) crtc; (void) mode; (void) adjusted_mode; return TRUE; }
287
288static void
289vbox_crtc_stub (xf86CrtcPtr crtc)
290{ (void) crtc; }
291
292static void
293vbox_crtc_mode_set (xf86CrtcPtr crtc, DisplayModePtr mode,
294 DisplayModePtr adjusted_mode, int x, int y)
295{
296 (void) mode;
297 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
298 unsigned cDisplay = (uintptr_t)crtc->driver_private;
299
300 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d, x=%d, y=%d\n", adjusted_mode->name,
301 adjusted_mode->HDisplay, adjusted_mode->VDisplay, x, y);
302 pVBox->afDisabled[cDisplay] = false;
303 VBOXSetMode(crtc->scrn, cDisplay, adjusted_mode->HDisplay,
304 adjusted_mode->VDisplay, x, y);
305 /* Don't remember any modes set while we are seamless, as they are
306 * just temporary. */
307 if (!vboxGuestIsSeamless(crtc->scrn))
308 vboxSaveVideoMode(crtc->scrn, adjusted_mode->HDisplay,
309 adjusted_mode->VDisplay, crtc->scrn->bitsPerPixel);
310}
311
312static void
313vbox_crtc_gamma_set (xf86CrtcPtr crtc, CARD16 *red,
314 CARD16 *green, CARD16 *blue, int size)
315{ (void) crtc; (void) red; (void) green; (void) blue; (void) size; }
316
317static void *
318vbox_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
319{ (void) crtc; (void) width; (void) height; return NULL; }
320
321static const xf86CrtcFuncsRec VBOXCrtcFuncs = {
322 .dpms = vbox_crtc_dpms,
323 .save = NULL, /* These two are never called by the server. */
324 .restore = NULL,
325 .lock = vbox_crtc_lock,
326 .unlock = NULL, /* This will not be invoked if lock returns FALSE. */
327 .mode_fixup = vbox_crtc_mode_fixup,
328 .prepare = vbox_crtc_stub,
329 .mode_set = vbox_crtc_mode_set,
330 .commit = vbox_crtc_stub,
331 .gamma_set = vbox_crtc_gamma_set,
332 .shadow_allocate = vbox_crtc_shadow_allocate,
333 .shadow_create = NULL, /* These two should not be invoked if allocate
334 returns NULL. */
335 .shadow_destroy = NULL,
336 .set_cursor_colors = NULL, /* We are still using the old cursor API. */
337 .set_cursor_position = NULL,
338 .show_cursor = NULL,
339 .hide_cursor = NULL,
340 .load_cursor_argb = NULL,
341 .destroy = vbox_crtc_stub
342};
343
344static void
345vbox_output_stub (xf86OutputPtr output)
346{ (void) output; }
347
348static void
349vbox_output_dpms (xf86OutputPtr output, int mode)
350{ (void) output; (void) mode; }
351
352static int
353vbox_output_mode_valid (xf86OutputPtr output, DisplayModePtr mode)
354{
355 ScrnInfoPtr pScrn = output->scrn;
356 int rc = MODE_OK;
357 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", mode->HDisplay, mode->VDisplay);
358 /* We always like modes specified by the user in the configuration
359 * file and modes requested by the host, as doing otherwise is likely to
360 * annoy people. */
361 if ( !(mode->type & M_T_USERDEF)
362 && !(mode->type & M_T_PREFERRED)
363 && vbox_device_available(VBOXGetRec(pScrn))
364 && !vboxHostLikesVideoMode(pScrn, mode->HDisplay, mode->VDisplay,
365 pScrn->bitsPerPixel)
366 )
367 rc = MODE_BAD;
368 TRACE_LOG("returning %s\n", MODE_OK == rc ? "MODE_OK" : "MODE_BAD");
369 return rc;
370}
371
372static Bool
373vbox_output_mode_fixup (xf86OutputPtr output, DisplayModePtr mode,
374 DisplayModePtr adjusted_mode)
375{ (void) output; (void) mode; (void) adjusted_mode; return TRUE; }
376
377static void
378vbox_output_mode_set (xf86OutputPtr output, DisplayModePtr mode,
379 DisplayModePtr adjusted_mode)
380{ (void) output; (void) mode; (void) adjusted_mode; }
381
382/* A virtual monitor is always connected. */
383static xf86OutputStatus
384vbox_output_detect (xf86OutputPtr output)
385{
386 (void) output;
387 return XF86OutputStatusConnected;
388}
389
390static DisplayModePtr
391vbox_output_add_mode (VBOXPtr pVBox, DisplayModePtr *pModes,
392 const char *pszName, int x, int y,
393 Bool isPreferred, Bool isUserDef)
394{
395 TRACE_LOG("pszName=%s, x=%d, y=%d\n", pszName, x, y);
396 DisplayModePtr pMode = xnfcalloc(1, sizeof(DisplayModeRec));
397
398 pMode->status = MODE_OK;
399 /* We don't ask the host whether it likes user defined modes,
400 * as we assume that the user really wanted that mode. */
401 pMode->type = isUserDef ? M_T_USERDEF : M_T_BUILTIN;
402 if (isPreferred)
403 pMode->type |= M_T_PREFERRED;
404 /* Older versions of VBox only support screen widths which are a multiple
405 * of 8 */
406 if (pVBox->fAnyX)
407 pMode->HDisplay = x;
408 else
409 pMode->HDisplay = x & ~7;
410 pMode->HSyncStart = pMode->HDisplay + 2;
411 pMode->HSyncEnd = pMode->HDisplay + 4;
412 pMode->HTotal = pMode->HDisplay + 6;
413 pMode->VDisplay = y;
414 pMode->VSyncStart = pMode->VDisplay + 2;
415 pMode->VSyncEnd = pMode->VDisplay + 4;
416 pMode->VTotal = pMode->VDisplay + 6;
417 pMode->Clock = pMode->HTotal * pMode->VTotal * 60 / 1000; /* kHz */
418 if (NULL == pszName) {
419 xf86SetModeDefaultName(pMode);
420 } else {
421 pMode->name = xnfstrdup(pszName);
422 }
423 *pModes = xf86ModesAdd(*pModes, pMode);
424 return pMode;
425}
426
427static DisplayModePtr
428vbox_output_get_modes (xf86OutputPtr output)
429{
430 unsigned i, cIndex = 0;
431 DisplayModePtr pModes = NULL, pMode;
432 ScrnInfoPtr pScrn = output->scrn;
433 VBOXPtr pVBox = VBOXGetRec(pScrn);
434
435 TRACE_ENTRY();
436 uint32_t x, y, bpp, iScreen;
437 iScreen = (uintptr_t)output->driver_private;
438 vboxGetPreferredMode(pScrn, iScreen, &x, &y, &bpp);
439 pMode = vbox_output_add_mode(pVBox, &pModes, NULL, x, y, TRUE, FALSE);
440 VBOXEDIDSet(output, pMode);
441 /* Add standard modes supported by the host */
442 for ( ; ; )
443 {
444 cIndex = vboxNextStandardMode(pScrn, cIndex, &x, &y, NULL);
445 if (cIndex == 0)
446 break;
447 vbox_output_add_mode(pVBox, &pModes, NULL, x, y, FALSE, FALSE);
448 }
449
450 /* Also report any modes the user may have requested in the xorg.conf
451 * configuration file. */
452 for (i = 0; pScrn->display->modes[i] != NULL; i++)
453 {
454 if (2 == sscanf(pScrn->display->modes[i], "%ux%u", &x, &y))
455 vbox_output_add_mode(pVBox, &pModes, pScrn->display->modes[i], x, y,
456 FALSE, TRUE);
457 }
458 TRACE_EXIT();
459 return pModes;
460}
461
462#ifdef RANDR_12_INTERFACE
463static Atom
464vboxAtomVBoxMode(void)
465{
466 return MakeAtom("VBOX_MODE", sizeof("VBOX_MODE") - 1, TRUE);
467}
468
469static Atom
470vboxAtomEDID(void)
471{
472 return MakeAtom("EDID", sizeof("EDID") - 1, TRUE);
473}
474
475/** We use this for receiving information from clients for the purpose of
476 * dynamic resizing, and later possibly other things too.
477 */
478static Bool
479vbox_output_set_property(xf86OutputPtr output, Atom property,
480 RRPropertyValuePtr value)
481{
482 ScrnInfoPtr pScrn = output->scrn;
483 VBOXPtr pVBox = VBOXGetRec(pScrn);
484 TRACE_LOG("property=%d, value->type=%d, value->format=%d, value->size=%ld\n",
485 (int)property, (int)value->type, value->format, value->size);
486 if (property == vboxAtomVBoxMode())
487 {
488 uint32_t cDisplay = (uintptr_t)output->driver_private;
489 char sz[256] = { 0 };
490 int w, h;
491
492 if ( value->type != XA_STRING
493 || (unsigned) value->size > (sizeof(sz) - 1))
494 return FALSE;
495 strncpy(sz, value->data, value->size);
496 TRACE_LOG("screen=%u, property value=%s\n", cDisplay, sz);
497 if (sscanf(sz, "%dx%d", &w, &h) != 2)
498 return FALSE;
499 pVBox->aPreferredSize[cDisplay].cx = w;
500 pVBox->aPreferredSize[cDisplay].cy = h;
501 return TRUE;
502 }
503 if (property == vboxAtomEDID())
504 return TRUE;
505 return FALSE;
506}
507#endif
508
509static const xf86OutputFuncsRec VBOXOutputFuncs = {
510 .create_resources = vbox_output_stub,
511 .dpms = vbox_output_dpms,
512 .save = NULL, /* These two are never called by the server. */
513 .restore = NULL,
514 .mode_valid = vbox_output_mode_valid,
515 .mode_fixup = vbox_output_mode_fixup,
516 .prepare = vbox_output_stub,
517 .commit = vbox_output_stub,
518 .mode_set = vbox_output_mode_set,
519 .detect = vbox_output_detect,
520 .get_modes = vbox_output_get_modes,
521#ifdef RANDR_12_INTERFACE
522 .set_property = vbox_output_set_property,
523#endif
524 .destroy = vbox_output_stub
525};
526#endif /* VBOXVIDEO_13 */
527
528#ifdef XFree86LOADER
529/* Module loader interface */
530static MODULESETUPPROTO(vboxSetup);
531
532static XF86ModuleVersionInfo vboxVersionRec =
533{
534 VBOX_DRIVER_NAME,
535 VBOX_VENDOR,
536 MODINFOSTRING1,
537 MODINFOSTRING2,
538#ifdef XORG_7X
539 XORG_VERSION_CURRENT,
540#else
541 XF86_VERSION_CURRENT,
542#endif
543 1, /* Module major version. Xorg-specific */
544 0, /* Module minor version. Xorg-specific */
545 1, /* Module patchlevel. Xorg-specific */
546 ABI_CLASS_VIDEODRV, /* This is a video driver */
547 ABI_VIDEODRV_VERSION,
548 MOD_CLASS_VIDEODRV,
549 {0, 0, 0, 0}
550};
551
552/*
553 * This data is accessed by the loader. The name must be the module name
554 * followed by "ModuleData".
555 */
556#ifdef XORG_7X
557_X_EXPORT
558#endif
559XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
560
561static pointer
562vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
563{
564 static Bool Initialised = FALSE;
565
566 if (!Initialised)
567 {
568 Initialised = TRUE;
569#ifdef PCIACCESS
570 xf86AddDriver(&VBOXVIDEO, Module, HaveDriverFuncs);
571#else
572 xf86AddDriver(&VBOXVIDEO, Module, 0);
573#endif
574#ifndef XORG_7X
575 LoaderRefSymLists(fbSymbols,
576 shadowfbSymbols,
577 vbeSymbols,
578 ramdacSymbols,
579 vgahwSymbols,
580 NULL);
581#endif
582 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXVIDEO\" is %p\n",
583 (void *)&VBOXVIDEO);
584 return (pointer)TRUE;
585 }
586
587 if (ErrorMajor)
588 *ErrorMajor = LDR_ONCEONLY;
589 return (NULL);
590}
591
592#endif /* XFree86Loader defined */
593
594static const OptionInfoRec *
595VBOXAvailableOptions(int chipid, int busid)
596{
597 return (VBOXOptions);
598}
599
600static void
601VBOXIdentify(int flags)
602{
603 xf86PrintChipsets(VBOX_NAME, "guest driver for VirtualBox", VBOXChipsets);
604}
605
606/*
607 * This function is called once, at the start of the first server generation to
608 * do a minimal probe for supported hardware.
609 */
610
611#ifdef PCIACCESS
612static Bool
613VBOXPciProbe(DriverPtr drv, int entity_num, struct pci_device *dev,
614 intptr_t match_data)
615{
616 ScrnInfoPtr pScrn;
617
618 TRACE_ENTRY();
619 pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, VBOXPCIchipsets,
620 NULL, NULL, NULL, NULL, NULL);
621 if (pScrn != NULL) {
622 VBOXPtr pVBox = VBOXGetRec(pScrn);
623
624 pScrn->driverVersion = VBOX_VERSION;
625 pScrn->driverName = VBOX_DRIVER_NAME;
626 pScrn->name = VBOX_NAME;
627 pScrn->Probe = NULL;
628 pScrn->PreInit = VBOXPreInit;
629 pScrn->ScreenInit = VBOXScreenInit;
630 pScrn->SwitchMode = VBOXSwitchMode;
631 pScrn->AdjustFrame = VBOXAdjustFrame;
632 pScrn->EnterVT = VBOXEnterVT;
633 pScrn->LeaveVT = VBOXLeaveVT;
634 pScrn->FreeScreen = VBOXFreeScreen;
635
636 pVBox->pciInfo = dev;
637 }
638
639 TRACE_LOG("returning %s\n", BOOL_STR(pScrn != NULL));
640 return (pScrn != NULL);
641}
642#endif
643
644#ifndef PCIACCESS
645static Bool
646VBOXProbe(DriverPtr drv, int flags)
647{
648 Bool foundScreen = FALSE;
649 int numDevSections;
650 GDevPtr *devSections;
651
652 /*
653 * Find the config file Device sections that match this
654 * driver, and return if there are none.
655 */
656 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
657 &devSections)) <= 0)
658 return (FALSE);
659
660 /* PCI BUS */
661 if (xf86GetPciVideoInfo()) {
662 int numUsed;
663 int *usedChips;
664 int i;
665 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
666 VBOXChipsets, VBOXPCIchipsets,
667 devSections, numDevSections,
668 drv, &usedChips);
669 if (numUsed > 0) {
670 if (flags & PROBE_DETECT)
671 foundScreen = TRUE;
672 else {
673 for (i = 0; i < numUsed; i++) {
674 ScrnInfoPtr pScrn = NULL;
675 /* Allocate a ScrnInfoRec */
676 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
677 VBOXPCIchipsets,NULL,
678 NULL,NULL,NULL,NULL))) {
679 pScrn->driverVersion = VBOX_VERSION;
680 pScrn->driverName = VBOX_DRIVER_NAME;
681 pScrn->name = VBOX_NAME;
682 pScrn->Probe = VBOXProbe;
683 pScrn->PreInit = VBOXPreInit;
684 pScrn->ScreenInit = VBOXScreenInit;
685 pScrn->SwitchMode = VBOXSwitchMode;
686 pScrn->AdjustFrame = VBOXAdjustFrame;
687 pScrn->EnterVT = VBOXEnterVT;
688 pScrn->LeaveVT = VBOXLeaveVT;
689 pScrn->FreeScreen = VBOXFreeScreen;
690 foundScreen = TRUE;
691 }
692 }
693 }
694 free(usedChips);
695 }
696 }
697
698 free(devSections);
699
700 return (foundScreen);
701}
702#endif
703
704/**
705 * This function hooks into the chain that is called when framebuffer access
706 * is allowed or disallowed by a call to EnableDisableFBAccess in the server.
707 * In other words, it observes when the server wishes access to the
708 * framebuffer to be enabled and when it should be disabled. We need to know
709 * this because we disable access ourselves during mode switches (presumably
710 * the server should do this but it doesn't) and want to know whether to
711 * restore it or not afterwards.
712 */
713static void
714vboxEnableDisableFBAccess(int scrnIndex, Bool enable)
715{
716 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
717 VBOXPtr pVBox = VBOXGetRec(pScrn);
718
719 TRACE_LOG("enable=%s\n", enable ? "TRUE" : "FALSE");
720 pVBox->accessEnabled = enable;
721 pVBox->EnableDisableFBAccess(scrnIndex, enable);
722 TRACE_EXIT();
723}
724
725/** Calculate the BPP from the screen depth */
726static uint16_t
727vboxBPP(ScrnInfoPtr pScrn)
728{
729 return pScrn->depth == 24 ? 32 : 16;
730}
731
732/** Calculate the scan line length for a display width */
733static int32_t
734vboxLineLength(ScrnInfoPtr pScrn, int32_t cDisplayWidth)
735{
736 uint64_t cbLine = ((uint64_t)cDisplayWidth * vboxBPP(pScrn) / 8 + 3) & ~3;
737 return cbLine < INT32_MAX ? cbLine : INT32_MAX;
738}
739
740/** Calculate the display pitch from the scan line length */
741static int32_t
742vboxDisplayPitch(ScrnInfoPtr pScrn, int32_t cbLine)
743{
744 return ASMDivU64ByU32RetU32((uint64_t)cbLine * 8, vboxBPP(pScrn));
745}
746
747/*
748 * QUOTE from the XFree86 DESIGN document:
749 *
750 * The purpose of this function is to find out all the information
751 * required to determine if the configuration is usable, and to initialise
752 * those parts of the ScrnInfoRec that can be set once at the beginning of
753 * the first server generation.
754 *
755 * (...)
756 *
757 * This includes probing for video memory, clocks, ramdac, and all other
758 * HW info that is needed. It includes determining the depth/bpp/visual
759 * and related info. It includes validating and determining the set of
760 * video modes that will be used (and anything that is required to
761 * determine that).
762 *
763 * This information should be determined in the least intrusive way
764 * possible. The state of the HW must remain unchanged by this function.
765 * Although video memory (including MMIO) may be mapped within this
766 * function, it must be unmapped before returning.
767 *
768 * END QUOTE
769 */
770
771static Bool
772VBOXPreInit(ScrnInfoPtr pScrn, int flags)
773{
774 VBOXPtr pVBox;
775 Gamma gzeros = {0.0, 0.0, 0.0};
776 rgb rzeros = {0, 0, 0};
777 unsigned DispiId;
778
779 TRACE_ENTRY();
780 /* Are we really starting the server, or is this just a dummy run? */
781 if (flags & PROBE_DETECT)
782 return (FALSE);
783
784 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
785 "VirtualBox guest additions video driver version "
786 VBOX_VERSION_STRING "\n");
787
788 /* Get our private data from the ScrnInfoRec structure. */
789 pVBox = VBOXGetRec(pScrn);
790
791 /* Initialise the guest library */
792 vbox_init(pScrn->scrnIndex, pVBox);
793
794 /* Entity information seems to mean bus information. */
795 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
796
797 /* The ramdac module is needed for the hardware cursor. */
798 if (!xf86LoadSubModule(pScrn, "ramdac"))
799 return FALSE;
800
801 /* We need the vbe module because we use VBE code to save and restore
802 text mode, in order to keep our code simple. */
803 if (!xf86LoadSubModule(pScrn, "vbe"))
804 return (FALSE);
805
806 /* The framebuffer module. */
807 if (xf86LoadSubModule(pScrn, "fb") == NULL)
808 return (FALSE);
809
810 if (!xf86LoadSubModule(pScrn, "shadowfb"))
811 return FALSE;
812
813 if (!xf86LoadSubModule(pScrn, "vgahw"))
814 return FALSE;
815
816#ifdef VBOX_DRI
817 /* Load the dri module. */
818 if (!xf86LoadSubModule(pScrn, "dri"))
819 return FALSE;
820#endif
821
822#ifndef PCIACCESS
823 if (pVBox->pEnt->location.type != BUS_PCI)
824 return FALSE;
825
826 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
827 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
828 pVBox->pciInfo->device,
829 pVBox->pciInfo->func);
830#endif
831
832 /* Set up our ScrnInfoRec structure to describe our virtual
833 capabilities to X. */
834
835 pScrn->chipset = "vbox";
836 pScrn->rgbBits = 8;
837
838 /* Let's create a nice, capable virtual monitor.
839 * This *is* still needed, at least for server version 1.3 */
840 pScrn->monitor = pScrn->confScreen->monitor;
841 pScrn->monitor->DDC = NULL;
842 pScrn->monitor->nHsync = 1;
843 pScrn->monitor->hsync[0].lo = 1;
844 pScrn->monitor->hsync[0].hi = 10000;
845 pScrn->monitor->nVrefresh = 1;
846 pScrn->monitor->vrefresh[0].lo = 1;
847 pScrn->monitor->vrefresh[0].hi = 100;
848
849 pScrn->progClock = TRUE;
850
851 /* Using the PCI information caused problems with non-powers-of-two
852 sized video RAM configurations */
853 pVBox->cbFBMax = inl(VBE_DISPI_IOPORT_DATA);
854 pScrn->videoRam = pVBox->cbFBMax / 1024;
855
856 /* Check if the chip restricts horizontal resolution or not. */
857 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ID);
858 outw(VBE_DISPI_IOPORT_DATA, VBE_DISPI_ID_ANYX);
859 DispiId = inw(VBE_DISPI_IOPORT_DATA);
860 if (DispiId == VBE_DISPI_ID_ANYX)
861 pVBox->fAnyX = TRUE;
862 else
863 pVBox->fAnyX = FALSE;
864
865 /* Set up clock information that will support all modes we need. */
866 pScrn->clockRanges = xnfcalloc(sizeof(ClockRange), 1);
867 pScrn->clockRanges->minClock = 1000;
868 pScrn->clockRanges->maxClock = 1000000000;
869 pScrn->clockRanges->clockIndex = -1;
870 pScrn->clockRanges->ClockMulFactor = 1;
871 pScrn->clockRanges->ClockDivFactor = 1;
872
873 /* Query the host for the preferred colour depth */
874 {
875 uint32_t cx = 0, cy = 0, cBits = 0;
876
877 vboxGetPreferredMode(pScrn, 0, &cx, &cy, &cBits);
878 /* We only support 16 and 24 bits depth (i.e. 16 and 32bpp) */
879 if (cBits != 16)
880 cBits = 24;
881 if (!xf86SetDepthBpp(pScrn, cBits, 0, 0, Support32bppFb))
882 return FALSE;
883 vboxAddModes(pScrn, cx, cy);
884 }
885 if (pScrn->bitsPerPixel != 32 && pScrn->bitsPerPixel != 16)
886 {
887 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
888 "The VBox additions only support 16 and 32bpp graphics modes\n");
889 return FALSE;
890 }
891 xf86PrintDepthBpp(pScrn);
892
893#ifdef VBOXVIDEO_13
894 /* Work around a bug in the original X server modesetting code, which
895 * took the first valid values set to these two as maxima over the
896 * server lifetime. */
897 pScrn->virtualX = 32000;
898 pScrn->virtualY = 32000;
899#else
900 /* We don't validate with xf86ValidateModes and xf86PruneModes as we
901 * already know what we like and what we don't. */
902
903 pScrn->currentMode = pScrn->modes;
904
905 /* Set the right virtual resolution. */
906 pScrn->virtualX = pScrn->currentMode->HDisplay;
907 pScrn->virtualY = pScrn->currentMode->VDisplay;
908
909#endif /* !VBOXVIDEO_13 */
910
911 /* Needed before we initialise DRI. */
912 pVBox->cbLine = vboxLineLength(pScrn, pScrn->virtualX);
913 pScrn->displayWidth = vboxDisplayPitch(pScrn, pVBox->cbLine);
914
915 xf86PrintModes(pScrn);
916
917 /* Colour weight - we always call this, since we are always in
918 truecolour. */
919 if (!xf86SetWeight(pScrn, rzeros, rzeros))
920 return (FALSE);
921
922 /* visual init */
923 if (!xf86SetDefaultVisual(pScrn, -1))
924 return (FALSE);
925
926 xf86SetGamma(pScrn, gzeros);
927
928 /* Set the DPI. Perhaps we should read this from the host? */
929 xf86SetDpi(pScrn, 96, 96);
930
931 /* Framebuffer-related setup */
932 pScrn->bitmapBitOrder = BITMAP_BIT_ORDER;
933
934 TRACE_EXIT();
935 return (TRUE);
936}
937
938/**
939 * Dummy function for setting the colour palette, which we actually never
940 * touch. However, the server still requires us to provide this.
941 */
942static void
943vboxLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
944 LOCO *colors, VisualPtr pVisual)
945{
946 (void)pScrn; (void) numColors; (void) indices; (void) colors;
947 (void)pVisual;
948}
949
950/*
951 * QUOTE from the XFree86 DESIGN document:
952 *
953 * This is called at the start of each server generation.
954 *
955 * (...)
956 *
957 * Decide which operations need to be placed under resource access
958 * control. (...) Map any video memory or other memory regions. (...)
959 * Save the video card state. (...) Initialise the initial video
960 * mode.
961 *
962 * End QUOTE.
963 */
964static Bool
965VBOXScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
966{
967 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
968 VBOXPtr pVBox = VBOXGetRec(pScrn);
969 VisualPtr visual;
970 unsigned flags;
971
972 TRACE_ENTRY();
973
974 /* VGA hardware initialisation */
975 if (!vgaHWGetHWRec(pScrn))
976 return FALSE;
977
978 /* We make use of the X11 VBE code to save and restore text mode, in
979 order to keep our code simple. */
980 if ((pVBox->pVbe = VBEExtendedInit(NULL, pVBox->pEnt->index,
981 SET_BIOS_SCRATCH
982 | RESTORE_BIOS_SCRATCH)) == NULL)
983 return (FALSE);
984
985 if (pScrn->memPhysBase == 0) {
986#ifdef PCIACCESS
987 pScrn->memPhysBase = pVBox->pciInfo->regions[0].base_addr;
988#else
989 pScrn->memPhysBase = pVBox->pciInfo->memBase[0];
990#endif
991 pScrn->fbOffset = 0;
992 }
993
994 if (!VBOXMapVidMem(pScrn))
995 return (FALSE);
996
997 /* save current video state */
998 VBOXSaveRestore(pScrn, MODE_SAVE);
999
1000 /* mi layer - reset the visual list (?)*/
1001 miClearVisualTypes();
1002 if (!xf86SetDefaultVisual(pScrn, -1))
1003 return (FALSE);
1004 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
1005 pScrn->rgbBits, TrueColor))
1006 return (FALSE);
1007 if (!miSetPixmapDepths())
1008 return (FALSE);
1009
1010#ifdef VBOX_DRI
1011 pVBox->useDRI = VBOXDRIScreenInit(scrnIndex, pScreen, pVBox);
1012#endif
1013
1014 /* I checked in the sources, and XFree86 4.2 does seem to support
1015 this function for 32bpp. */
1016 if (!fbScreenInit(pScreen, pVBox->base,
1017 pScrn->virtualX, pScrn->virtualY,
1018 pScrn->xDpi, pScrn->yDpi,
1019 pScrn->displayWidth, pScrn->bitsPerPixel))
1020 return (FALSE);
1021
1022 /* Fixup RGB ordering */
1023 visual = pScreen->visuals + pScreen->numVisuals;
1024 while (--visual >= pScreen->visuals) {
1025 if ((visual->class | DynamicClass) == DirectColor) {
1026 visual->offsetRed = pScrn->offset.red;
1027 visual->offsetGreen = pScrn->offset.green;
1028 visual->offsetBlue = pScrn->offset.blue;
1029 visual->redMask = pScrn->mask.red;
1030 visual->greenMask = pScrn->mask.green;
1031 visual->blueMask = pScrn->mask.blue;
1032 }
1033 }
1034
1035 /* must be after RGB ordering fixed */
1036 fbPictureInit(pScreen, 0, 0);
1037
1038 xf86SetBlackWhitePixels(pScreen);
1039 miInitializeBackingStore(pScreen);
1040 xf86SetBackingStore(pScreen);
1041
1042 /* We need to keep track of whether we are currently switched to a virtual
1043 * terminal to know whether a mode set operation is currently safe to do.
1044 */
1045 pVBox->vtSwitch = FALSE;
1046
1047 if (vbox_open (pScrn, pScreen, pVBox)) {
1048 vboxEnableVbva(pScrn);
1049 vboxEnableGraphicsCap(pVBox);
1050 }
1051
1052#ifdef VBOXVIDEO_13
1053 /* Initialise CRTC and output configuration for use with randr1.2. */
1054 xf86CrtcConfigInit(pScrn, &VBOXCrtcConfigFuncs);
1055
1056 {
1057 uint32_t i;
1058
1059 for (i = 0; i < pVBox->cScreens; ++i)
1060 {
1061 char szOutput[256];
1062
1063 /* Setup our virtual CRTCs. */
1064 pVBox->paCrtcs[i] = xf86CrtcCreate(pScrn, &VBOXCrtcFuncs);
1065 pVBox->paCrtcs[i]->driver_private = (void *)(uintptr_t)i;
1066
1067 /* Set up our virtual outputs. */
1068 snprintf(szOutput, sizeof(szOutput), "VBOX%u", i);
1069 pVBox->paOutputs[i] = xf86OutputCreate(pScrn, &VBOXOutputFuncs,
1070 szOutput);
1071
1072 /* We are not interested in the monitor section in the
1073 * configuration file. */
1074 xf86OutputUseScreenMonitor(pVBox->paOutputs[i], FALSE);
1075 pVBox->paOutputs[i]->possible_crtcs = 1 << i;
1076 pVBox->paOutputs[i]->possible_clones = 0;
1077 pVBox->paOutputs[i]->driver_private = (void *)(uintptr_t)i;
1078 TRACE_LOG("Created crtc (%p) and output %s (%p)\n",
1079 (void *)pVBox->paCrtcs[i], szOutput,
1080 (void *)pVBox->paOutputs[i]);
1081 }
1082 }
1083
1084 /* Set a sane minimum and maximum mode size */
1085 xf86CrtcSetSizeRange(pScrn, 64, 64, 32000, 32000);
1086
1087 /* Now create our initial CRTC/output configuration. */
1088 if (!xf86InitialConfiguration(pScrn, TRUE)) {
1089 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial CRTC configuration failed!\n");
1090 return (FALSE);
1091 }
1092
1093 /* Initialise randr 1.2 mode-setting functions and set first mode.
1094 * Note that the mode won't be usable until the server has resized the
1095 * framebuffer to something reasonable. */
1096 if (!xf86CrtcScreenInit(pScreen)) {
1097 return FALSE;
1098 }
1099
1100 /* Create our VBOX_MODE display properties. */
1101 {
1102 uint32_t i;
1103
1104 for (i = 0; i < pVBox->cScreens; ++i)
1105 {
1106 char csz[] = "0x0";
1107 RRChangeOutputProperty(pVBox->paOutputs[i]->randr_output,
1108 vboxAtomVBoxMode(), XA_STRING, 8,
1109 PropModeReplace, sizeof(csz), csz, TRUE,
1110 FALSE);
1111
1112 }
1113 }
1114
1115 if (!xf86SetDesiredModes(pScrn)) {
1116 return FALSE;
1117 }
1118#else /* !VBOXVIDEO_13 */
1119 /* set first video mode */
1120 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1121 pScrn->currentMode->VDisplay, pScrn->frameX0,
1122 pScrn->frameY0))
1123 return FALSE;
1124 /* And make sure that a non-current dynamic mode is at the front of the
1125 * list */
1126 vboxWriteHostModes(pScrn, pScrn->currentMode);
1127#endif /* !VBOXVIDEO_13 */
1128
1129 /* software cursor */
1130 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1131
1132 /* colourmap code */
1133 if (!miCreateDefColormap(pScreen))
1134 return (FALSE);
1135
1136 if(!xf86HandleColormaps(pScreen, 256, 8, vboxLoadPalette, NULL, 0))
1137 return (FALSE);
1138
1139 /* Hook our observer function ito the chain which is called when
1140 * framebuffer access is enabled or disabled in the server, and
1141 * assume an initial state of enabled. */
1142 pVBox->accessEnabled = TRUE;
1143 pVBox->EnableDisableFBAccess = pScrn->EnableDisableFBAccess;
1144 pScrn->EnableDisableFBAccess = vboxEnableDisableFBAccess;
1145
1146 pVBox->CloseScreen = pScreen->CloseScreen;
1147 pScreen->CloseScreen = VBOXCloseScreen;
1148#ifdef VBOXVIDEO_13
1149 pScreen->SaveScreen = xf86SaveScreen;
1150#else
1151 pScreen->SaveScreen = VBOXSaveScreen;
1152#endif
1153
1154#ifdef VBOXVIDEO_13
1155 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1156#else
1157 /* We probably do want to support power management - even if we just use
1158 a dummy function. */
1159 xf86DPMSInit(pScreen, VBOXDisplayPowerManagementSet, 0);
1160#endif
1161
1162 /* Report any unused options (only for the first generation) */
1163 if (serverGeneration == 1)
1164 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1165
1166 if (vbox_cursor_init(pScreen) != TRUE)
1167 xf86DrvMsg(scrnIndex, X_ERROR,
1168 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
1169
1170#ifdef VBOX_DRI
1171 if (pVBox->useDRI)
1172 pVBox->useDRI = VBOXDRIFinishScreenInit(pScreen);
1173#endif
1174 return (TRUE);
1175}
1176
1177/** Clear the virtual framebuffer in VRAM. Optionally also clear up to the
1178 * size of a new framebuffer. Framebuffer sizes larger than available VRAM
1179 * be treated as zero and passed over. */
1180static void
1181vboxClearVRAM(ScrnInfoPtr pScrn, int32_t cNewX, int32_t cNewY)
1182{
1183 VBOXPtr pVBox = VBOXGetRec(pScrn);
1184 uint64_t cbOldFB, cbNewFB;
1185
1186 cbOldFB = pVBox->cbLine * pScrn->virtualX;
1187 cbNewFB = vboxLineLength(pScrn, cNewX) * cNewY;
1188 if (cbOldFB > (uint64_t)pVBox->cbFBMax)
1189 cbOldFB = 0;
1190 if (cbNewFB > (uint64_t)pVBox->cbFBMax)
1191 cbNewFB = 0;
1192 memset(pVBox->base, 0, max(cbOldFB, cbNewFB));
1193}
1194
1195static Bool
1196VBOXEnterVT(int scrnIndex, int flags)
1197{
1198 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1199 VBOXPtr pVBox = VBOXGetRec(pScrn);
1200
1201 TRACE_ENTRY();
1202 vboxClearVRAM(pScrn, 0, 0);
1203 if (pVBox->fHaveHGSMI)
1204 vboxEnableVbva(pScrn);
1205 pVBox->vtSwitch = FALSE;
1206#ifdef VBOX_DRI
1207 if (pVBox->useDRI)
1208 DRIUnlock(screenInfo.screens[scrnIndex]);
1209#endif
1210#ifdef VBOXVIDEO_13
1211 if (!xf86SetDesiredModes(pScrn))
1212 return FALSE;
1213#else
1214 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1215 pScrn->currentMode->VDisplay, pScrn->frameX0,
1216 pScrn->frameY0))
1217 return FALSE;
1218#endif
1219 return TRUE;
1220}
1221
1222static void
1223VBOXLeaveVT(int scrnIndex, int flags)
1224{
1225 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1226 VBOXPtr pVBox = VBOXGetRec(pScrn);
1227
1228 TRACE_ENTRY();
1229 pVBox->vtSwitch = TRUE;
1230 if (pVBox->fHaveHGSMI)
1231 vboxDisableVbva(pScrn);
1232 vboxClearVRAM(pScrn, 0, 0);
1233 VBOXSaveRestore(pScrn, MODE_RESTORE);
1234 vboxDisableGraphicsCap(pVBox);
1235#ifdef VBOX_DRI
1236 if (pVBox->useDRI)
1237 DRILock(screenInfo.screens[scrnIndex], 0);
1238#endif
1239 TRACE_EXIT();
1240}
1241
1242static Bool
1243VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen)
1244{
1245 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1246 VBOXPtr pVBox = VBOXGetRec(pScrn);
1247
1248 if (pVBox->fHaveHGSMI)
1249 vboxDisableVbva(pScrn);
1250 vboxDisableGraphicsCap(pVBox);
1251 vboxClearVRAM(pScrn, 0, 0);
1252#ifdef VBOX_DRI
1253 if (pVBox->useDRI)
1254 VBOXDRICloseScreen(pScreen, pVBox);
1255 pVBox->useDRI = false;
1256#endif
1257
1258 if (pScrn->vtSema) {
1259 VBOXSaveRestore(xf86Screens[scrnIndex], MODE_RESTORE);
1260 VBOXUnmapVidMem(pScrn);
1261 }
1262 pScrn->vtSema = FALSE;
1263
1264 /* Destroy the VGA hardware record */
1265 vgaHWFreeHWRec(pScrn);
1266
1267 /* And do additional bits which are separate for historical reasons */
1268 vbox_close(pScrn, pVBox);
1269
1270 /* Remove our observer functions from the X server call chains. */
1271 pScrn->EnableDisableFBAccess = pVBox->EnableDisableFBAccess;
1272 pScreen->CloseScreen = pVBox->CloseScreen;
1273 return pScreen->CloseScreen(scrnIndex, pScreen);
1274}
1275
1276static Bool
1277VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags)
1278{
1279 ScrnInfoPtr pScrn;
1280 VBOXPtr pVBox;
1281 Bool rc;
1282
1283 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", pMode->HDisplay, pMode->VDisplay);
1284 pScrn = xf86Screens[scrnIndex]; /* Why does X have three ways of referring to the screen? */
1285 pVBox = VBOXGetRec(pScrn);
1286 /* We want to disable access to the framebuffer before switching mode.
1287 * After doing the switch, we allow access if it was allowed before. */
1288 if (pVBox->accessEnabled)
1289 pVBox->EnableDisableFBAccess(scrnIndex, FALSE);
1290#ifdef VBOXVIDEO_13
1291 rc = xf86SetSingleMode(pScrn, pMode, 0);
1292#else
1293 VBOXAdjustScreenPixmap(pScrn, pMode->HDisplay, pMode->VDisplay);
1294 rc = VBOXSetMode(pScrn, 0, pMode->HDisplay, pMode->VDisplay,
1295 pScrn->frameX0, pScrn->frameY0);
1296 if (rc)
1297 {
1298 vboxWriteHostModes(pScrn, pMode);
1299 xf86PrintModes(pScrn);
1300 }
1301 if (rc && !vboxGuestIsSeamless(pScrn))
1302 vboxSaveVideoMode(pScrn, pMode->HDisplay, pMode->VDisplay,
1303 pScrn->bitsPerPixel);
1304#endif
1305 if (pVBox->accessEnabled)
1306 pVBox->EnableDisableFBAccess(scrnIndex, TRUE);
1307 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1308 return rc;
1309}
1310
1311/** Set a graphics mode. Poke any required values into registers, do an HGSMI
1312 * mode set and tell the host we support advanced graphics functions. This
1313 * procedure is complicated by the fact that X.Org can implicitly disable a
1314 * screen by resizing the virtual framebuffer so that the screen is no longer
1315 * inside it. We have to spot and handle this.
1316 */
1317static Bool
1318VBOXSetMode(ScrnInfoPtr pScrn, unsigned cDisplay, unsigned cWidth,
1319 unsigned cHeight, int x, int y)
1320{
1321 VBOXPtr pVBox = VBOXGetRec(pScrn);
1322 Bool rc = TRUE, fActive = !pVBox->afDisabled[cDisplay];
1323 uint32_t offStart, cwReal = cWidth;
1324
1325 TRACE_LOG("cDisplay=%u, cWidth=%u, cHeight=%u, x=%d, y=%d, displayWidth=%d\n",
1326 cDisplay, cWidth, cHeight, x, y, pScrn->displayWidth);
1327 pVBox->aScreenLocation[cDisplay].cx = cWidth;
1328 pVBox->aScreenLocation[cDisplay].cy = cHeight;
1329 pVBox->aScreenLocation[cDisplay].x = x;
1330 pVBox->aScreenLocation[cDisplay].y = y;
1331 offStart = y * pVBox->cbLine + x * vboxBPP(pScrn) / 8;
1332 /* Deactivate the screen if the mode - specifically the virtual width - is
1333 * too large for VRAM as we sometimes have to do this - see comments in
1334 * VBOXPreInit. */
1335 if ( offStart + pVBox->cbLine * cHeight > pVBox->cbFBMax
1336 || pVBox->cbLine * pScrn->virtualY > pVBox->cbFBMax)
1337 fActive = FALSE;
1338 /* Deactivate the screen if it is outside of the virtual framebuffer and
1339 * clamp it to lie inside if it is partly outside. */
1340 if (x >= pScrn->displayWidth || x + (int) cWidth <= 0)
1341 fActive = FALSE;
1342 else
1343 cwReal = RT_MIN((int) cWidth, pScrn->displayWidth - x);
1344 TRACE_LOG("pVBox->afDisabled[cDisplay]=%d, fActive=%d\n",
1345 (int)pVBox->afDisabled[cDisplay], (int)fActive);
1346 /* Don't fiddle with the hardware if we are switched
1347 * to a virtual terminal. */
1348 if (!pVBox->vtSwitch && fActive)
1349 {
1350 if (cDisplay == 0)
1351 VBoxVideoSetModeRegisters(cwReal, cHeight, pScrn->displayWidth,
1352 vboxBPP(pScrn), x, y);
1353 /* Tell the host we support graphics */
1354 if (vbox_device_available(pVBox))
1355 vboxEnableGraphicsCap(pVBox);
1356 }
1357 if ( (pVBox->fHaveHGSMI)
1358 && !pVBox->vtSwitch)
1359 VBoxHGSMIProcessDisplayInfo(&pVBox->guestCtx, cDisplay, x, y,
1360 offStart, pVBox->cbLine, cwReal, cHeight,
1361 vboxBPP(pScrn),
1362 VBVA_SCREEN_F_ACTIVE
1363 | (fActive ? 0: VBVA_SCREEN_F_DISABLED));
1364 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1365 return rc;
1366}
1367
1368/** Resize the virtual framebuffer. After resizing we reset all modes
1369 * (X.Org 1.3+) to adjust them to the new framebuffer.
1370 */
1371static Bool VBOXAdjustScreenPixmap(ScrnInfoPtr pScrn, int width, int height)
1372{
1373 ScreenPtr pScreen = pScrn->pScreen;
1374 PixmapPtr pPixmap = pScreen->GetScreenPixmap(pScreen);
1375 VBOXPtr pVBox = VBOXGetRec(pScrn);
1376 uint64_t cbLine = vboxLineLength(pScrn, width);
1377
1378 TRACE_LOG("width=%d, height=%d\n", width, height);
1379 if (!pPixmap) {
1380 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1381 "Failed to get the screen pixmap.\n");
1382 return FALSE;
1383 }
1384 if (cbLine > UINT32_MAX || cbLine * height >= pVBox->cbFBMax)
1385 {
1386 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1387 "Unable to set up a virtual screen size of %dx%d with %lu of %d Kb of video memory available. Please increase the video memory size.\n",
1388 width, height, pVBox->cbFBMax / 1024, pScrn->videoRam);
1389 return FALSE;
1390 }
1391 pScreen->ModifyPixmapHeader(pPixmap, width, height,
1392 pScrn->depth, vboxBPP(pScrn), cbLine,
1393 pVBox->base);
1394 vboxClearVRAM(pScrn, width, height);
1395 pScrn->virtualX = width;
1396 pScrn->virtualY = height;
1397 pScrn->displayWidth = vboxDisplayPitch(pScrn, cbLine);
1398 pVBox->cbLine = cbLine;
1399#ifdef VBOX_DRI
1400 if (pVBox->useDRI)
1401 VBOXDRIUpdateStride(pScrn, pVBox);
1402#endif
1403#ifdef VBOXVIDEO_13
1404 /* Write the new values to the hardware */
1405 {
1406 unsigned i;
1407 for (i = 0; i < pVBox->cScreens; ++i)
1408 VBOXSetMode(pScrn, i, pVBox->aScreenLocation[i].cx,
1409 pVBox->aScreenLocation[i].cy,
1410 pVBox->aScreenLocation[i].x,
1411 pVBox->aScreenLocation[i].y);
1412 }
1413#endif
1414 return TRUE;
1415}
1416
1417static void
1418VBOXAdjustFrame(int scrnIndex, int x, int y, int flags)
1419{
1420 VBOXPtr pVBox = VBOXGetRec(xf86Screens[scrnIndex]);
1421 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
1422
1423 TRACE_ENTRY();
1424 /* Don't fiddle with the hardware if we are switched
1425 * to a virtual terminal. */
1426 VBOXSetMode(pScrn, 0, pVBox->aScreenLocation[0].cx,
1427 pVBox->aScreenLocation[0].cy, x, y);
1428 TRACE_EXIT();
1429}
1430
1431static void
1432VBOXFreeScreen(int scrnIndex, int flags)
1433{
1434 VBOXFreeRec(xf86Screens[scrnIndex]);
1435}
1436
1437static Bool
1438VBOXMapVidMem(ScrnInfoPtr pScrn)
1439{
1440 VBOXPtr pVBox = VBOXGetRec(pScrn);
1441 Bool rc = TRUE;
1442
1443 TRACE_ENTRY();
1444 if (!pVBox->base)
1445 {
1446#ifdef PCIACCESS
1447 (void) pci_device_map_range(pVBox->pciInfo,
1448 pScrn->memPhysBase,
1449 pScrn->videoRam * 1024,
1450 PCI_DEV_MAP_FLAG_WRITABLE,
1451 & pVBox->base);
1452#else
1453 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
1454 VIDMEM_FRAMEBUFFER,
1455 pVBox->pciTag, pScrn->memPhysBase,
1456 (unsigned) pScrn->videoRam * 1024);
1457#endif
1458 if (pVBox->base)
1459 {
1460 /* We need this for saving/restoring textmode */
1461 VGAHWPTR(pScrn)->IOBase = pScrn->domainIOBase;
1462 rc = vgaHWMapMem(pScrn);
1463 }
1464 else
1465 rc = FALSE;
1466 }
1467 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1468 return rc;
1469}
1470
1471static void
1472VBOXUnmapVidMem(ScrnInfoPtr pScrn)
1473{
1474 VBOXPtr pVBox = VBOXGetRec(pScrn);
1475
1476 TRACE_ENTRY();
1477 if (pVBox->base == NULL)
1478 return;
1479
1480#ifdef PCIACCESS
1481 (void) pci_device_unmap_range(pVBox->pciInfo,
1482 pVBox->base,
1483 pScrn->videoRam * 1024);
1484#else
1485 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
1486 (unsigned) pScrn->videoRam * 1024);
1487#endif
1488 vgaHWUnmapMem(pScrn);
1489 pVBox->base = NULL;
1490 TRACE_EXIT();
1491}
1492
1493static Bool
1494VBOXSaveScreen(ScreenPtr pScreen, int mode)
1495{
1496 (void)pScreen; (void)mode;
1497 return TRUE;
1498}
1499
1500Bool
1501VBOXSaveRestore(ScrnInfoPtr pScrn, vbeSaveRestoreFunction function)
1502{
1503 VBOXPtr pVBox;
1504 Bool rc = TRUE;
1505
1506 TRACE_ENTRY();
1507 if (MODE_QUERY < 0 || function > MODE_RESTORE)
1508 rc = FALSE;
1509
1510 if (rc)
1511 {
1512 pVBox = VBOXGetRec(pScrn);
1513
1514 /* Query amount of memory to save state */
1515 if (function == MODE_QUERY ||
1516 (function == MODE_SAVE && pVBox->state == NULL))
1517 {
1518
1519 /* Make sure we save at least this information in case of failure */
1520 (void)VBEGetVBEMode(pVBox->pVbe, &pVBox->stateMode);
1521 vgaHWSaveFonts(pScrn, &pVBox->vgaRegs);
1522
1523 if (!VBESaveRestore(pVBox->pVbe,function,(pointer)&pVBox->state,
1524 &pVBox->stateSize,&pVBox->statePage)
1525 )
1526 rc = FALSE;
1527 }
1528 }
1529 if (rc)
1530 {
1531 /* Save/Restore Super VGA state */
1532 if (function != MODE_QUERY) {
1533
1534 if (function == MODE_RESTORE)
1535 memcpy(pVBox->state, pVBox->pstate,
1536 (unsigned) pVBox->stateSize);
1537
1538 if ( (rc = VBESaveRestore(pVBox->pVbe,function,
1539 (pointer)&pVBox->state,
1540 &pVBox->stateSize,&pVBox->statePage)
1541 )
1542 && (function == MODE_SAVE)
1543 )
1544 {
1545 /* don't rely on the memory not being touched */
1546 if (pVBox->pstate == NULL)
1547 pVBox->pstate = malloc(pVBox->stateSize);
1548 memcpy(pVBox->pstate, pVBox->state,
1549 (unsigned) pVBox->stateSize);
1550 }
1551
1552 if (function == MODE_RESTORE)
1553 {
1554 VBESetVBEMode(pVBox->pVbe, pVBox->stateMode, NULL);
1555 vgaHWRestoreFonts(pScrn, &pVBox->vgaRegs);
1556 }
1557 }
1558 }
1559 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1560 return rc;
1561}
1562
1563static void
1564VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
1565 int flags)
1566{
1567 (void)pScrn; (void)mode; (void) flags;
1568}
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