VirtualBox

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

Last change on this file since 55244 was 55236, checked in by vboxsync, 10 years ago

Additions/x11/vboxvideo: use constants for the maximum and minimum screen and frame-buffer sizes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.9 KB
Line 
1/* $Id: vboxvideo.c 55236 2015-04-14 10:02:04Z vboxsync $ */
2/** @file
3 *
4 * Linux Additions X11 graphics driver
5 */
6
7/*
8 * Copyright (C) 2006-2013 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 the X.Org VESA driver with the following copyrights:
20 *
21 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
22 * Copyright 2008 Red Hat, Inc.
23 * Copyright 2012 Red Hat, Inc.
24 *
25 * and the following permission notice (not all original sourse files include
26 * the last paragraph):
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 * David Dawes <[email protected]>
53 * Adam Jackson <[email protected]>
54 * Dave Airlie <[email protected]>
55 */
56
57#ifdef XORG_7X
58# include <stdlib.h>
59# include <string.h>
60#endif
61
62#include "xf86.h"
63#include "xf86_OSproc.h"
64#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
65# include "xf86Resources.h"
66#endif
67
68/* This was accepted upstream in X.Org Server 1.16 which bumped the video
69 * driver ABI to 17. */
70#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 17
71# define SET_HAVE_VT_PROPERTY
72#endif
73
74#ifndef PCIACCESS
75/* Drivers for PCI hardware need this */
76# include "xf86PciInfo.h"
77/* Drivers that need to access the PCI config space directly need this */
78# include "xf86Pci.h"
79#endif
80
81#include "fb.h"
82
83#include "vboxvideo.h"
84#include <VBox/VBoxGuest.h>
85#include "version-generated.h"
86#include "product-generated.h"
87#include <xf86.h>
88#include <misc.h>
89
90/* All drivers initialising the SW cursor need this */
91#include "mipointer.h"
92
93/* Colormap handling */
94#include "micmap.h"
95#include "xf86cmap.h"
96
97/* DPMS */
98/* #define DPMS_SERVER
99#include "extensions/dpms.h" */
100
101/* VGA hardware functions for setting and restoring text mode */
102#include "vgaHW.h"
103
104#ifdef VBOXVIDEO_13
105/* X.org 1.3+ mode setting */
106# define _HAVE_STRING_ARCH_strsep /* bits/string2.h, __strsep_1c. */
107# include "xf86Crtc.h"
108# include "xf86Modes.h"
109#endif
110
111/* For setting the root window property. */
112#include <X11/Xatom.h>
113#include "property.h"
114
115#ifdef VBOX_DRI
116# include "xf86drm.h"
117# include "xf86drmMode.h"
118#endif
119
120/* Mandatory functions */
121
122static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
123static void VBOXIdentify(int flags);
124#ifndef PCIACCESS
125static Bool VBOXProbe(DriverPtr drv, int flags);
126#else
127static Bool VBOXPciProbe(DriverPtr drv, int entity_num,
128 struct pci_device *dev, intptr_t match_data);
129#endif
130static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
131static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv);
132static Bool VBOXEnterVT(ScrnInfoPtr pScrn);
133static void VBOXLeaveVT(ScrnInfoPtr pScrn);
134static Bool VBOXCloseScreen(ScreenPtr pScreen);
135static Bool VBOXSaveScreen(ScreenPtr pScreen, int mode);
136static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode);
137static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y);
138static void VBOXFreeScreen(ScrnInfoPtr pScrn);
139static void VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
140 int flags);
141
142/* locally used functions */
143static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
144static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
145static void VBOXSaveMode(ScrnInfoPtr pScrn);
146static void VBOXRestoreMode(ScrnInfoPtr pScrn);
147
148static inline void VBOXSetRec(ScrnInfoPtr pScrn)
149{
150 if (!pScrn->driverPrivate)
151 {
152 VBOXPtr pVBox = (VBOXPtr)xnfcalloc(sizeof(VBOXRec), 1);
153 pScrn->driverPrivate = pVBox;
154#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
155 pVBox->fdACPIDevices = -1;
156#endif
157 }
158}
159
160enum GenericTypes
161{
162 CHIP_VBOX_GENERIC
163};
164
165#ifdef PCIACCESS
166static const struct pci_id_match vbox_device_match[] = {
167 {
168 VBOX_VENDORID, VBOX_DEVICEID, PCI_MATCH_ANY, PCI_MATCH_ANY,
169 0, 0, 0
170 },
171
172 { 0, 0, 0 },
173};
174#endif
175
176/* Supported chipsets */
177static SymTabRec VBOXChipsets[] =
178{
179 {VBOX_DEVICEID, "vbox"},
180 {-1, NULL}
181};
182
183static PciChipsets VBOXPCIchipsets[] = {
184 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
185 { -1, -1, RES_UNDEFINED },
186};
187
188/*
189 * This contains the functions needed by the server after loading the
190 * driver module. It must be supplied, and gets added the driver list by
191 * the Module Setup function in the dynamic case. In the static case a
192 * reference to this is compiled in, and this requires that the name of
193 * this DriverRec be an upper-case version of the driver name.
194 */
195
196#ifdef XORG_7X
197_X_EXPORT
198#endif
199DriverRec VBOXVIDEO = {
200 VBOX_VERSION,
201 VBOX_DRIVER_NAME,
202 VBOXIdentify,
203#ifdef PCIACCESS
204 NULL,
205#else
206 VBOXProbe,
207#endif
208 VBOXAvailableOptions,
209 NULL,
210 0,
211#ifdef XORG_7X
212 NULL,
213#endif
214#ifdef PCIACCESS
215 vbox_device_match,
216 VBOXPciProbe
217#endif
218};
219
220/* No options for now */
221static const OptionInfoRec VBOXOptions[] = {
222 { -1, NULL, OPTV_NONE, {0}, FALSE }
223};
224
225#ifndef XORG_7X
226/*
227 * List of symbols from other modules that this module references. This
228 * list is used to tell the loader that it is OK for symbols here to be
229 * unresolved providing that it hasn't been told that they haven't been
230 * told that they are essential via a call to xf86LoaderReqSymbols() or
231 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
232 * unresolved symbols that are not required.
233 */
234static const char *fbSymbols[] = {
235 "fbPictureInit",
236 "fbScreenInit",
237 NULL
238};
239
240static const char *shadowfbSymbols[] = {
241 "ShadowFBInit2",
242 NULL
243};
244
245static const char *ramdacSymbols[] = {
246 "xf86DestroyCursorInfoRec",
247 "xf86InitCursor",
248 "xf86CreateCursorInfoRec",
249 NULL
250};
251
252static const char *vgahwSymbols[] = {
253 "vgaHWFreeHWRec",
254 "vgaHWGetHWRec",
255 "vgaHWGetIOBase",
256 "vgaHWGetIndex",
257 "vgaHWRestore",
258 "vgaHWSave",
259 "vgaHWSetStdFuncs",
260 NULL
261};
262#endif /* !XORG_7X */
263
264#ifdef VBOXVIDEO_13
265/* X.org 1.3+ mode-setting support ******************************************/
266
267/* For descriptions of these functions and structures, see
268 hw/xfree86/modes/xf86Crtc.h and hw/xfree86/modes/xf86Modes.h in the
269 X.Org source tree. */
270
271static Bool vbox_config_resize(ScrnInfoPtr pScrn, int cw, int ch)
272{
273 VBOXPtr pVBox = VBOXGetRec(pScrn);
274 TRACE_LOG("width=%d, height=%d\n", cw, ch);
275 /* Save the size in case we need to re-set it later. */
276 pVBox->FBSize.cx = cw;
277 pVBox->FBSize.cy = ch;
278 /* Don't fiddle with the hardware if we are switched
279 * to a virtual terminal. */
280 if (!pScrn->vtSema) {
281 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
282 "We do not own the active VT, exiting.\n");
283 return TRUE;
284 }
285 return VBOXAdjustScreenPixmap(pScrn, cw, ch);
286}
287
288static const xf86CrtcConfigFuncsRec VBOXCrtcConfigFuncs = {
289 vbox_config_resize
290};
291
292static void
293vbox_crtc_dpms(xf86CrtcPtr crtc, int mode)
294{
295 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
296 unsigned cDisplay = (uintptr_t)crtc->driver_private;
297 bool fEnabled = (mode != DPMSModeOff);
298
299 TRACE_LOG("cDisplay=%u, mode=%i\n", cDisplay, mode);
300 pVBox->pScreens[cDisplay].fCrtcEnabled = fEnabled;
301 /* Don't fiddle with the hardware if we are switched
302 * to a virtual terminal. */
303 if (!crtc->scrn->vtSema) {
304 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
305 "We do not own the active VT, exiting.\n");
306 return;
307 }
308 if ( pVBox->pScreens[cDisplay].aScreenLocation.cx
309 && pVBox->pScreens[cDisplay].aScreenLocation.cy)
310 VBOXSetMode(crtc->scrn, cDisplay,
311 pVBox->pScreens[cDisplay].aScreenLocation.cx,
312 pVBox->pScreens[cDisplay].aScreenLocation.cy,
313 pVBox->pScreens[cDisplay].aScreenLocation.x,
314 pVBox->pScreens[cDisplay].aScreenLocation.y);
315}
316
317static Bool
318vbox_crtc_lock (xf86CrtcPtr crtc)
319{ (void) crtc; return FALSE; }
320
321
322/* We use this function to check whether the X server owns the active virtual
323 * terminal before attempting a mode switch, since the RandR extension isn't
324 * very dilligent here, which can mean crashes if we are unlucky. This is
325 * not the way it the function is intended - it is meant for reporting modes
326 * which the hardware can't handle. I hope that this won't confuse any clients
327 * connecting to us. */
328static Bool
329vbox_crtc_mode_fixup (xf86CrtcPtr crtc, DisplayModePtr mode,
330 DisplayModePtr adjusted_mode)
331{ (void) crtc; (void) mode; (void) adjusted_mode; return TRUE; }
332
333static void
334vbox_crtc_stub (xf86CrtcPtr crtc)
335{ (void) crtc; }
336
337static void
338vbox_crtc_mode_set (xf86CrtcPtr crtc, DisplayModePtr mode,
339 DisplayModePtr adjusted_mode, int x, int y)
340{
341 (void) mode;
342 VBOXPtr pVBox = VBOXGetRec(crtc->scrn);
343 unsigned cDisplay = (uintptr_t)crtc->driver_private;
344
345 TRACE_LOG("name=%s, HDisplay=%d, VDisplay=%d, x=%d, y=%d\n", adjusted_mode->name,
346 adjusted_mode->HDisplay, adjusted_mode->VDisplay, x, y);
347 pVBox->pScreens[cDisplay].fCrtcEnabled = true;
348 pVBox->pScreens[cDisplay].fOutputEnabled = true;
349 pVBox->pScreens[cDisplay].aScreenLocation.cx = adjusted_mode->HDisplay;
350 pVBox->pScreens[cDisplay].aScreenLocation.cy = adjusted_mode->VDisplay;
351 pVBox->pScreens[cDisplay].aScreenLocation.x = x;
352 pVBox->pScreens[cDisplay].aScreenLocation.y = y;
353 /* Don't fiddle with the hardware if we are switched
354 * to a virtual terminal. */
355 if (!crtc->scrn->vtSema)
356 {
357 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
358 "We do not own the active VT, exiting.\n");
359 return;
360 }
361 VBOXSetMode(crtc->scrn, cDisplay, adjusted_mode->HDisplay,
362 adjusted_mode->VDisplay, x, y);
363}
364
365static void
366vbox_crtc_gamma_set (xf86CrtcPtr crtc, CARD16 *red,
367 CARD16 *green, CARD16 *blue, int size)
368{ (void) crtc; (void) red; (void) green; (void) blue; (void) size; }
369
370static void *
371vbox_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
372{ (void) crtc; (void) width; (void) height; return NULL; }
373
374static const xf86CrtcFuncsRec VBOXCrtcFuncs = {
375 .dpms = vbox_crtc_dpms,
376 .save = NULL, /* These two are never called by the server. */
377 .restore = NULL,
378 .lock = vbox_crtc_lock,
379 .unlock = NULL, /* This will not be invoked if lock returns FALSE. */
380 .mode_fixup = vbox_crtc_mode_fixup,
381 .prepare = vbox_crtc_stub,
382 .mode_set = vbox_crtc_mode_set,
383 .commit = vbox_crtc_stub,
384 .gamma_set = vbox_crtc_gamma_set,
385 .shadow_allocate = vbox_crtc_shadow_allocate,
386 .shadow_create = NULL, /* These two should not be invoked if allocate
387 returns NULL. */
388 .shadow_destroy = NULL,
389 .set_cursor_colors = NULL, /* We are still using the old cursor API. */
390 .set_cursor_position = NULL,
391 .show_cursor = NULL,
392 .hide_cursor = NULL,
393 .load_cursor_argb = NULL,
394 .destroy = vbox_crtc_stub
395};
396
397static void
398vbox_output_stub (xf86OutputPtr output)
399{ (void) output; }
400
401static void
402vbox_output_dpms (xf86OutputPtr output, int mode)
403{
404 VBOXPtr pVBox = VBOXGetRec(output->scrn);
405 unsigned cDisplay = (uintptr_t)output->driver_private;
406 bool fEnabled = (mode == DPMSModeOn);
407
408 TRACE_LOG("cDisplay=%u, mode=%i\n", cDisplay, mode);
409 pVBox->pScreens[cDisplay].fOutputEnabled = fEnabled;
410 /* Don't fiddle with the hardware if we are switched
411 * to a virtual terminal. */
412 if (!output->scrn->vtSema) {
413 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
414 "We do not own the active VT, exiting.\n");
415 return;
416 }
417 if ( pVBox->pScreens[cDisplay].aScreenLocation.cx
418 && pVBox->pScreens[cDisplay].aScreenLocation.cy)
419 VBOXSetMode(output->scrn, cDisplay,
420 pVBox->pScreens[cDisplay].aScreenLocation.cx,
421 pVBox->pScreens[cDisplay].aScreenLocation.cy,
422 pVBox->pScreens[cDisplay].aScreenLocation.x,
423 pVBox->pScreens[cDisplay].aScreenLocation.y);
424}
425
426static int
427vbox_output_mode_valid (xf86OutputPtr output, DisplayModePtr mode)
428{
429 return MODE_OK;
430}
431
432static Bool
433vbox_output_mode_fixup (xf86OutputPtr output, DisplayModePtr mode,
434 DisplayModePtr adjusted_mode)
435{ (void) output; (void) mode; (void) adjusted_mode; return TRUE; }
436
437static void
438vbox_output_mode_set (xf86OutputPtr output, DisplayModePtr mode,
439 DisplayModePtr adjusted_mode)
440{ (void) output; (void) mode; (void) adjusted_mode; }
441
442/* A virtual monitor is always connected. */
443static xf86OutputStatus
444vbox_output_detect (xf86OutputPtr output)
445{
446 ScrnInfoPtr pScrn = output->scrn;
447 VBOXPtr pVBox = VBOXGetRec(pScrn);
448 uint32_t iScreen = (uintptr_t)output->driver_private;
449 return pVBox->pScreens[iScreen].afConnected
450 ? XF86OutputStatusConnected : XF86OutputStatusDisconnected;
451}
452
453static DisplayModePtr vbox_output_add_mode(VBOXPtr pVBox, DisplayModePtr *pModes, const char *pszName, int x, int y,
454 Bool isPreferred, Bool isUserDef)
455{
456 TRACE_LOG("pszName=%s, x=%d, y=%d\n", pszName ? pszName : "(null)", x, y);
457 DisplayModePtr pMode = xnfcalloc(1, sizeof(DisplayModeRec));
458 int cRefresh = 60;
459
460 pMode->status = MODE_OK;
461 /* We don't ask the host whether it likes user defined modes,
462 * as we assume that the user really wanted that mode. */
463 pMode->type = isUserDef ? M_T_USERDEF : M_T_BUILTIN;
464 if (isPreferred)
465 pMode->type |= M_T_PREFERRED;
466 /* Older versions of VBox only support screen widths which are a multiple
467 * of 8 */
468 if (pVBox->fAnyX)
469 pMode->HDisplay = x;
470 else
471 pMode->HDisplay = x & ~7;
472 pMode->HSyncStart = pMode->HDisplay + 2;
473 pMode->HSyncEnd = pMode->HDisplay + 4;
474 pMode->HTotal = pMode->HDisplay + 6;
475 pMode->VDisplay = y;
476 pMode->VSyncStart = pMode->VDisplay + 2;
477 pMode->VSyncEnd = pMode->VDisplay + 4;
478 pMode->VTotal = pMode->VDisplay + 6;
479 pMode->Clock = pMode->HTotal * pMode->VTotal * cRefresh / 1000; /* kHz */
480 if (NULL == pszName) {
481 xf86SetModeDefaultName(pMode);
482 } else {
483 pMode->name = xnfstrdup(pszName);
484 }
485 *pModes = xf86ModesAdd(*pModes, pMode);
486 return pMode;
487}
488
489static DisplayModePtr
490vbox_output_get_modes (xf86OutputPtr output)
491{
492 unsigned i, cIndex = 0;
493 DisplayModePtr pModes = NULL, pMode;
494 ScrnInfoPtr pScrn = output->scrn;
495 VBOXPtr pVBox = VBOXGetRec(pScrn);
496
497 TRACE_ENTRY();
498 uint32_t x, y, iScreen;
499 iScreen = (uintptr_t)output->driver_private;
500 VBoxUpdateSizeHints(pScrn);
501 pMode = vbox_output_add_mode(pVBox, &pModes, NULL, pVBox->pScreens[iScreen].aPreferredSize.cx,
502 pVBox->pScreens[iScreen].aPreferredSize.cy, TRUE, FALSE);
503 TRACE_EXIT();
504 return pModes;
505}
506
507static const xf86OutputFuncsRec VBOXOutputFuncs = {
508 .create_resources = vbox_output_stub,
509 .dpms = vbox_output_dpms,
510 .save = NULL, /* These two are never called by the server. */
511 .restore = NULL,
512 .mode_valid = vbox_output_mode_valid,
513 .mode_fixup = vbox_output_mode_fixup,
514 .prepare = vbox_output_stub,
515 .commit = vbox_output_stub,
516 .mode_set = vbox_output_mode_set,
517 .detect = vbox_output_detect,
518 .get_modes = vbox_output_get_modes,
519#ifdef RANDR_12_INTERFACE
520 .set_property = NULL,
521#endif
522 .destroy = vbox_output_stub
523};
524#endif /* VBOXVIDEO_13 */
525
526/* Module loader interface */
527static MODULESETUPPROTO(vboxSetup);
528
529static XF86ModuleVersionInfo vboxVersionRec =
530{
531 VBOX_DRIVER_NAME,
532 VBOX_VENDOR,
533 MODINFOSTRING1,
534 MODINFOSTRING2,
535#ifdef XORG_7X
536 XORG_VERSION_CURRENT,
537#else
538 XF86_VERSION_CURRENT,
539#endif
540 1, /* Module major version. Xorg-specific */
541 0, /* Module minor version. Xorg-specific */
542 1, /* Module patchlevel. Xorg-specific */
543 ABI_CLASS_VIDEODRV, /* This is a video driver */
544 ABI_VIDEODRV_VERSION,
545 MOD_CLASS_VIDEODRV,
546 {0, 0, 0, 0}
547};
548
549/*
550 * This data is accessed by the loader. The name must be the module name
551 * followed by "ModuleData".
552 */
553#ifdef XORG_7X
554_X_EXPORT
555#endif
556XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
557
558static pointer
559vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
560{
561 static Bool Initialised = FALSE;
562
563 if (!Initialised)
564 {
565 Initialised = TRUE;
566#ifdef PCIACCESS
567 xf86AddDriver(&VBOXVIDEO, Module, HaveDriverFuncs);
568#else
569 xf86AddDriver(&VBOXVIDEO, Module, 0);
570#endif
571#ifndef XORG_7X
572 LoaderRefSymLists(fbSymbols,
573 shadowfbSymbols,
574 ramdacSymbols,
575 vgahwSymbols,
576 NULL);
577#endif
578 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXVIDEO\" is %p\n",
579 (void *)&VBOXVIDEO);
580 return (pointer)TRUE;
581 }
582
583 if (ErrorMajor)
584 *ErrorMajor = LDR_ONCEONLY;
585 return (NULL);
586}
587
588
589static const OptionInfoRec *
590VBOXAvailableOptions(int chipid, int busid)
591{
592 return (VBOXOptions);
593}
594
595static void
596VBOXIdentify(int flags)
597{
598 xf86PrintChipsets(VBOX_NAME, "guest driver for VirtualBox", VBOXChipsets);
599}
600
601#ifndef XF86_SCRN_INTERFACE
602# define xf86ScreenToScrn(pScreen) xf86Screens[(pScreen)->myNum]
603# define xf86ScrnToScreen(pScrn) screenInfo.screens[(pScrn)->scrnIndex]
604# define SCRNINDEXAPI(pfn) pfn ## Index
605static Bool VBOXScreenInitIndex(int scrnIndex, ScreenPtr pScreen, int argc,
606 char **argv)
607{ return VBOXScreenInit(pScreen, argc, argv); }
608
609static Bool VBOXEnterVTIndex(int scrnIndex, int flags)
610{ (void) flags; return VBOXEnterVT(xf86Screens[scrnIndex]); }
611
612static void VBOXLeaveVTIndex(int scrnIndex, int flags)
613{ (void) flags; VBOXLeaveVT(xf86Screens[scrnIndex]); }
614
615static Bool VBOXCloseScreenIndex(int scrnIndex, ScreenPtr pScreen)
616{ (void) scrnIndex; return VBOXCloseScreen(pScreen); }
617
618static Bool VBOXSwitchModeIndex(int scrnIndex, DisplayModePtr pMode, int flags)
619{ (void) flags; return VBOXSwitchMode(xf86Screens[scrnIndex], pMode); }
620
621static void VBOXAdjustFrameIndex(int scrnIndex, int x, int y, int flags)
622{ (void) flags; VBOXAdjustFrame(xf86Screens[scrnIndex], x, y); }
623
624static void VBOXFreeScreenIndex(int scrnIndex, int flags)
625{ (void) flags; VBOXFreeScreen(xf86Screens[scrnIndex]); }
626# else
627# define SCRNINDEXAPI(pfn) pfn
628#endif /* XF86_SCRN_INTERFACE */
629
630static void setScreenFunctions(ScrnInfoPtr pScrn, xf86ProbeProc pfnProbe)
631{
632 pScrn->driverVersion = VBOX_VERSION;
633 pScrn->driverName = VBOX_DRIVER_NAME;
634 pScrn->name = VBOX_NAME;
635 pScrn->Probe = pfnProbe;
636 pScrn->PreInit = VBOXPreInit;
637 pScrn->ScreenInit = SCRNINDEXAPI(VBOXScreenInit);
638 pScrn->SwitchMode = SCRNINDEXAPI(VBOXSwitchMode);
639 pScrn->AdjustFrame = SCRNINDEXAPI(VBOXAdjustFrame);
640 pScrn->EnterVT = SCRNINDEXAPI(VBOXEnterVT);
641 pScrn->LeaveVT = SCRNINDEXAPI(VBOXLeaveVT);
642 pScrn->FreeScreen = SCRNINDEXAPI(VBOXFreeScreen);
643}
644
645/*
646 * One of these functions is called once, at the start of the first server
647 * generation to do a minimal probe for supported hardware.
648 */
649
650#ifdef PCIACCESS
651static Bool
652VBOXPciProbe(DriverPtr drv, int entity_num, struct pci_device *dev,
653 intptr_t match_data)
654{
655 ScrnInfoPtr pScrn;
656
657 TRACE_ENTRY();
658 pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, VBOXPCIchipsets,
659 NULL, NULL, NULL, NULL, NULL);
660 if (pScrn != NULL) {
661 VBOXPtr pVBox;
662
663 VBOXSetRec(pScrn);
664 pVBox = VBOXGetRec(pScrn);
665 if (!pVBox)
666 return FALSE;
667 setScreenFunctions(pScrn, NULL);
668 pVBox->pciInfo = dev;
669 }
670
671 TRACE_LOG("returning %s\n", BOOL_STR(pScrn != NULL));
672 return (pScrn != NULL);
673}
674#endif
675
676#ifndef PCIACCESS
677static Bool
678VBOXProbe(DriverPtr drv, int flags)
679{
680 Bool foundScreen = FALSE;
681 int numDevSections;
682 GDevPtr *devSections;
683
684 /*
685 * Find the config file Device sections that match this
686 * driver, and return if there are none.
687 */
688 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
689 &devSections)) <= 0)
690 return (FALSE);
691
692 /* PCI BUS */
693 if (xf86GetPciVideoInfo())
694 {
695 int numUsed;
696 int *usedChips;
697 int i;
698 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
699 VBOXChipsets, VBOXPCIchipsets,
700 devSections, numDevSections,
701 drv, &usedChips);
702 if (numUsed > 0)
703 {
704 if (flags & PROBE_DETECT)
705 foundScreen = TRUE;
706 else
707 for (i = 0; i < numUsed; i++)
708 {
709 ScrnInfoPtr pScrn = NULL;
710 /* Allocate a ScrnInfoRec */
711 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
712 VBOXPCIchipsets,NULL,
713 NULL,NULL,NULL,NULL)))
714 {
715 setScreenFunctions(pScrn, VBOXProbe);
716 foundScreen = TRUE;
717 }
718 }
719 free(usedChips);
720 }
721 }
722 free(devSections);
723 return (foundScreen);
724}
725#endif
726
727
728/*
729 * QUOTE from the XFree86 DESIGN document:
730 *
731 * The purpose of this function is to find out all the information
732 * required to determine if the configuration is usable, and to initialise
733 * those parts of the ScrnInfoRec that can be set once at the beginning of
734 * the first server generation.
735 *
736 * (...)
737 *
738 * This includes probing for video memory, clocks, ramdac, and all other
739 * HW info that is needed. It includes determining the depth/bpp/visual
740 * and related info. It includes validating and determining the set of
741 * video modes that will be used (and anything that is required to
742 * determine that).
743 *
744 * This information should be determined in the least intrusive way
745 * possible. The state of the HW must remain unchanged by this function.
746 * Although video memory (including MMIO) may be mapped within this
747 * function, it must be unmapped before returning.
748 *
749 * END QUOTE
750 */
751
752static Bool
753VBOXPreInit(ScrnInfoPtr pScrn, int flags)
754{
755 VBOXPtr pVBox;
756 Gamma gzeros = {0.0, 0.0, 0.0};
757 rgb rzeros = {0, 0, 0};
758 unsigned DispiId;
759
760 TRACE_ENTRY();
761 /* Are we really starting the server, or is this just a dummy run? */
762 if (flags & PROBE_DETECT)
763 return (FALSE);
764
765 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
766 "VirtualBox guest additions video driver version "
767 VBOX_VERSION_STRING "\n");
768
769 /* Get our private data from the ScrnInfoRec structure. */
770 VBOXSetRec(pScrn);
771 pVBox = VBOXGetRec(pScrn);
772 if (!pVBox)
773 return FALSE;
774
775 /* Entity information seems to mean bus information. */
776 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
777
778 /* The ramdac module is needed for the hardware cursor. */
779 if (!xf86LoadSubModule(pScrn, "ramdac"))
780 return FALSE;
781
782 /* The framebuffer module. */
783 if (!xf86LoadSubModule(pScrn, "fb"))
784 return (FALSE);
785
786 if (!xf86LoadSubModule(pScrn, "shadowfb"))
787 return FALSE;
788
789 if (!xf86LoadSubModule(pScrn, "vgahw"))
790 return FALSE;
791
792#ifdef VBOX_DRI_OLD
793 /* Load the dri module. */
794 if (!xf86LoadSubModule(pScrn, "dri"))
795 return FALSE;
796#else
797# ifdef VBOX_DRI
798 /* Load the dri module. */
799 if (!xf86LoadSubModule(pScrn, "dri2"))
800 return FALSE;
801# endif
802#endif
803
804#ifndef PCIACCESS
805 if (pVBox->pEnt->location.type != BUS_PCI)
806 return FALSE;
807
808 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
809 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
810 pVBox->pciInfo->device,
811 pVBox->pciInfo->func);
812#endif
813
814 /* Set up our ScrnInfoRec structure to describe our virtual
815 capabilities to X. */
816
817 pScrn->chipset = "vbox";
818 /** @note needed during colourmap initialisation */
819 pScrn->rgbBits = 8;
820
821 /* Let's create a nice, capable virtual monitor. */
822 pScrn->monitor = pScrn->confScreen->monitor;
823 pScrn->monitor->DDC = NULL;
824 pScrn->monitor->nHsync = 1;
825 pScrn->monitor->hsync[0].lo = 1;
826 pScrn->monitor->hsync[0].hi = 10000;
827 pScrn->monitor->nVrefresh = 1;
828 pScrn->monitor->vrefresh[0].lo = 1;
829 pScrn->monitor->vrefresh[0].hi = 100;
830
831 pScrn->progClock = TRUE;
832
833 /* Using the PCI information caused problems with non-powers-of-two
834 sized video RAM configurations */
835 pVBox->cbFBMax = VBoxVideoGetVRAMSize();
836 pScrn->videoRam = pVBox->cbFBMax / 1024;
837
838 /* Check if the chip restricts horizontal resolution or not. */
839 pVBox->fAnyX = VBoxVideoAnyWidthAllowed();
840
841 /* Set up clock information that will support all modes we need. */
842 pScrn->clockRanges = xnfcalloc(sizeof(ClockRange), 1);
843 pScrn->clockRanges->minClock = 1000;
844 pScrn->clockRanges->maxClock = 1000000000;
845 pScrn->clockRanges->clockIndex = -1;
846 pScrn->clockRanges->ClockMulFactor = 1;
847 pScrn->clockRanges->ClockDivFactor = 1;
848
849 if (!xf86SetDepthBpp(pScrn, 24, 0, 0, Support32bppFb))
850 return FALSE;
851 /* We only support 16 and 24 bits depth (i.e. 16 and 32bpp) */
852 if (pScrn->bitsPerPixel != 32 && pScrn->bitsPerPixel != 16)
853 {
854 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
855 "The VBox additions only support 16 and 32bpp graphics modes\n");
856 return FALSE;
857 }
858 xf86PrintDepthBpp(pScrn);
859 vboxAddModes(pScrn);
860
861#ifdef VBOXVIDEO_13
862 /* Work around a bug in the original X server modesetting code, which
863 * took the first valid values set to these two as maxima over the
864 * server lifetime. */
865 pScrn->virtualX = VBOX_VIDEO_MAX_VIRTUAL;
866 pScrn->virtualY = VBOX_VIDEO_MAX_VIRTUAL;
867#else
868 /* We don't validate with xf86ValidateModes and xf86PruneModes as we
869 * already know what we like and what we don't. */
870
871 pScrn->currentMode = pScrn->modes;
872
873 /* Set the right virtual resolution. */
874 pScrn->virtualX = pScrn->currentMode->HDisplay;
875 pScrn->virtualY = pScrn->currentMode->VDisplay;
876
877#endif /* !VBOXVIDEO_13 */
878
879 /* Needed before we initialise DRI. */
880 pVBox->cbLine = vboxLineLength(pScrn, pScrn->virtualX);
881 pScrn->displayWidth = vboxDisplayPitch(pScrn, pVBox->cbLine);
882
883 xf86PrintModes(pScrn);
884
885 /* VGA hardware initialisation */
886 if (!vgaHWGetHWRec(pScrn))
887 return FALSE;
888 /* Must be called before any VGA registers are saved or restored */
889 vgaHWSetStdFuncs(VGAHWPTR(pScrn));
890 vgaHWGetIOBase(VGAHWPTR(pScrn));
891
892 /* Colour weight - we always call this, since we are always in
893 truecolour. */
894 if (!xf86SetWeight(pScrn, rzeros, rzeros))
895 return (FALSE);
896
897 /* visual init */
898 if (!xf86SetDefaultVisual(pScrn, -1))
899 return (FALSE);
900
901 xf86SetGamma(pScrn, gzeros);
902
903 /* Set the DPI. Perhaps we should read this from the host? */
904 xf86SetDpi(pScrn, 96, 96);
905
906 if (pScrn->memPhysBase == 0) {
907#ifdef PCIACCESS
908 pScrn->memPhysBase = pVBox->pciInfo->regions[0].base_addr;
909#else
910 pScrn->memPhysBase = pVBox->pciInfo->memBase[0];
911#endif
912 pScrn->fbOffset = 0;
913 }
914
915 TRACE_EXIT();
916 return (TRUE);
917}
918
919/**
920 * Dummy function for setting the colour palette, which we actually never
921 * touch. However, the server still requires us to provide this.
922 */
923static void
924vboxLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
925 LOCO *colors, VisualPtr pVisual)
926{
927 (void)pScrn; (void) numColors; (void) indices; (void) colors;
928 (void)pVisual;
929}
930
931#define HAS_VT_ATOM_NAME "XFree86_has_VT"
932#define VBOXVIDEO_DRIVER_ATOM_NAME "VBOXVIDEO_DRIVER_IN_USE"
933/* The memory storing the initial value of the XFree86_has_VT root window
934 * property. This has to remain available until server start-up, so we just
935 * use a global. */
936static CARD32 InitialPropertyValue = 1;
937
938/** Initialise a flag property on the root window to say whether the server VT
939 * is currently the active one as some clients need to know this. */
940static void initialiseProperties(ScrnInfoPtr pScrn)
941{
942 Atom atom = -1;
943 CARD32 *PropertyValue = &InitialPropertyValue;
944#ifdef SET_HAVE_VT_PROPERTY
945 atom = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1, TRUE);
946 if (xf86RegisterRootWindowProperty(pScrn->scrnIndex, atom, XA_INTEGER,
947 32, 1, PropertyValue) != Success)
948 FatalError("vboxvideo: failed to register VT property\n");
949#endif /* SET_HAVE_VT_PROPERTY */
950 atom = MakeAtom(VBOXVIDEO_DRIVER_ATOM_NAME,
951 sizeof(VBOXVIDEO_DRIVER_ATOM_NAME) - 1, TRUE);
952 if (xf86RegisterRootWindowProperty(pScrn->scrnIndex, atom, XA_INTEGER,
953 32, 1, PropertyValue) != Success)
954 FatalError("vboxvideo: failed to register driver in use property\n");
955}
956
957#ifdef SET_HAVE_VT_PROPERTY
958/** Update a flag property on the root window to say whether the server VT
959 * is currently the active one as some clients need to know this. */
960static void updateHasVTProperty(ScrnInfoPtr pScrn, Bool hasVT)
961{
962 Atom property_name;
963 int32_t value = hasVT ? 1 : 0;
964 int i;
965
966 property_name = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1,
967 FALSE);
968 if (property_name == BAD_RESOURCE)
969 FatalError("Failed to retrieve \"HAS_VT\" atom\n");
970 if (ROOT_WINDOW(pScrn) == NULL)
971 return;
972 ChangeWindowProperty(ROOT_WINDOW(pScrn), property_name, XA_INTEGER, 32,
973 PropModeReplace, 1, &value, TRUE);
974}
975#endif /* SET_HAVE_VT_PROPERTY */
976
977/*
978 * QUOTE from the XFree86 DESIGN document:
979 *
980 * This is called at the start of each server generation.
981 *
982 * (...)
983 *
984 * Decide which operations need to be placed under resource access
985 * control. (...) Map any video memory or other memory regions. (...)
986 * Save the video card state. (...) Initialise the initial video
987 * mode.
988 *
989 * End QUOTE.
990 */
991static Bool VBOXScreenInit(ScreenPtr pScreen, int argc, char **argv)
992{
993 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
994 VBOXPtr pVBox = VBOXGetRec(pScrn);
995 VisualPtr visual;
996 unsigned flags;
997
998 TRACE_ENTRY();
999
1000 if (!VBOXMapVidMem(pScrn))
1001 return (FALSE);
1002
1003 /* save current video state */
1004 VBOXSaveMode(pScrn);
1005
1006 /* mi layer - reset the visual list (?)*/
1007 miClearVisualTypes();
1008 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
1009 pScrn->rgbBits, TrueColor))
1010 return (FALSE);
1011 if (!miSetPixmapDepths())
1012 return (FALSE);
1013
1014#ifdef VBOX_DRI
1015 pVBox->useDRI = VBOXDRIScreenInit(pScrn, pScreen, pVBox);
1016# ifndef VBOX_DRI_OLD /* DRI2 */
1017 if (pVBox->drmFD >= 0)
1018 /* Tell the kernel driver, if present, that we are taking over. */
1019 drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_DISABLE_HGSMI, NULL);
1020# endif
1021#endif
1022
1023 if (!fbScreenInit(pScreen, pVBox->base,
1024 pScrn->virtualX, pScrn->virtualY,
1025 pScrn->xDpi, pScrn->yDpi,
1026 pScrn->displayWidth, pScrn->bitsPerPixel))
1027 return (FALSE);
1028
1029 /* Fixup RGB ordering */
1030 /** @note the X server uses this even in true colour. */
1031 visual = pScreen->visuals + pScreen->numVisuals;
1032 while (--visual >= pScreen->visuals) {
1033 if ((visual->class | DynamicClass) == DirectColor) {
1034 visual->offsetRed = pScrn->offset.red;
1035 visual->offsetGreen = pScrn->offset.green;
1036 visual->offsetBlue = pScrn->offset.blue;
1037 visual->redMask = pScrn->mask.red;
1038 visual->greenMask = pScrn->mask.green;
1039 visual->blueMask = pScrn->mask.blue;
1040 }
1041 }
1042
1043 /* must be after RGB ordering fixed */
1044 fbPictureInit(pScreen, 0, 0);
1045
1046 xf86SetBlackWhitePixels(pScreen);
1047 pScrn->vtSema = TRUE;
1048
1049#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
1050 VBoxSetUpLinuxACPI(pScreen);
1051#endif
1052
1053 vbox_open (pScrn, pScreen, pVBox);
1054 vboxEnableVbva(pScrn);
1055 VBoxInitialiseSizeHints(pScrn);
1056
1057#ifdef VBOXVIDEO_13
1058 /* Initialise CRTC and output configuration for use with randr1.2. */
1059 xf86CrtcConfigInit(pScrn, &VBOXCrtcConfigFuncs);
1060
1061 {
1062 uint32_t i;
1063
1064 for (i = 0; i < pVBox->cScreens; ++i)
1065 {
1066 char szOutput[256];
1067
1068 /* Setup our virtual CRTCs. */
1069 pVBox->pScreens[i].paCrtcs = xf86CrtcCreate(pScrn, &VBOXCrtcFuncs);
1070 pVBox->pScreens[i].paCrtcs->driver_private = (void *)(uintptr_t)i;
1071
1072 /* Set up our virtual outputs. */
1073 snprintf(szOutput, sizeof(szOutput), "VGA-%u", i);
1074 pVBox->pScreens[i].paOutputs
1075 = xf86OutputCreate(pScrn, &VBOXOutputFuncs, szOutput);
1076
1077 /* We are not interested in the monitor section in the
1078 * configuration file. */
1079 xf86OutputUseScreenMonitor(pVBox->pScreens[i].paOutputs, FALSE);
1080 pVBox->pScreens[i].paOutputs->possible_crtcs = 1 << i;
1081 pVBox->pScreens[i].paOutputs->possible_clones = 0;
1082 pVBox->pScreens[i].paOutputs->driver_private = (void *)(uintptr_t)i;
1083 TRACE_LOG("Created crtc (%p) and output %s (%p)\n",
1084 (void *)pVBox->pScreens[i].paCrtcs, szOutput,
1085 (void *)pVBox->pScreens[i].paOutputs);
1086 }
1087 }
1088
1089 /* Set a sane minimum and maximum mode size to match what the hardware
1090 * supports. */
1091 xf86CrtcSetSizeRange(pScrn, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MIN_SIZE, VBOX_VIDEO_MAX_VIRTUAL, VBOX_VIDEO_MAX_VIRTUAL);
1092
1093 /* Now create our initial CRTC/output configuration. */
1094 if (!xf86InitialConfiguration(pScrn, TRUE)) {
1095 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial CRTC configuration failed!\n");
1096 return (FALSE);
1097 }
1098
1099 /* Initialise randr 1.2 mode-setting functions and set first mode.
1100 * Note that the mode won't be usable until the server has resized the
1101 * framebuffer to something reasonable. */
1102 if (!xf86CrtcScreenInit(pScreen)) {
1103 return FALSE;
1104 }
1105
1106 if (!xf86SetDesiredModes(pScrn)) {
1107 return FALSE;
1108 }
1109#else /* !VBOXVIDEO_13 */
1110 VBoxSetUpRandR11(pScreen);
1111 /* set first video mode */
1112 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1113 pScrn->currentMode->VDisplay, pScrn->frameX0,
1114 pScrn->frameY0))
1115 return FALSE;
1116 /* Save the size in case we need to re-set it later. */
1117 pVBox->FBSize.cx = pScrn->currentMode->HDisplay;
1118 pVBox->FBSize.cy = pScrn->currentMode->VDisplay;
1119 pVBox->pScreens[0].aScreenLocation.cx = pScrn->currentMode->HDisplay;
1120 pVBox->pScreens[0].aScreenLocation.cy = pScrn->currentMode->VDisplay;
1121 pVBox->pScreens[0].aScreenLocation.x = pScrn->frameX0;
1122 pVBox->pScreens[0].aScreenLocation.y = pScrn->frameY0;
1123#endif /* !VBOXVIDEO_13 */
1124
1125 /* software cursor */
1126 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1127
1128 /* colourmap code */
1129 if (!miCreateDefColormap(pScreen))
1130 return (FALSE);
1131
1132 if(!xf86HandleColormaps(pScreen, 256, 8, vboxLoadPalette, NULL, 0))
1133 return (FALSE);
1134
1135 pVBox->CloseScreen = pScreen->CloseScreen;
1136 pScreen->CloseScreen = SCRNINDEXAPI(VBOXCloseScreen);
1137#ifdef VBOXVIDEO_13
1138 pScreen->SaveScreen = xf86SaveScreen;
1139#else
1140 pScreen->SaveScreen = VBOXSaveScreen;
1141#endif
1142
1143#ifdef VBOXVIDEO_13
1144 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1145#else
1146 /* We probably do want to support power management - even if we just use
1147 a dummy function. */
1148 xf86DPMSInit(pScreen, VBOXDisplayPowerManagementSet, 0);
1149#endif
1150
1151 /* Report any unused options (only for the first generation) */
1152 if (serverGeneration == 1)
1153 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1154
1155 if (vbox_cursor_init(pScreen) != TRUE)
1156 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1157 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
1158
1159#ifdef VBOX_DRI_OLD
1160 if (pVBox->useDRI)
1161 pVBox->useDRI = VBOXDRIFinishScreenInit(pScreen);
1162#endif
1163
1164 initialiseProperties(pScrn);
1165
1166 return (TRUE);
1167}
1168
1169static Bool VBOXEnterVT(ScrnInfoPtr pScrn)
1170{
1171 VBOXPtr pVBox = VBOXGetRec(pScrn);
1172
1173 TRACE_ENTRY();
1174#ifdef VBOX_DRI_OLD
1175 if (pVBox->useDRI)
1176 DRIUnlock(xf86ScrnToScreen(pScrn));
1177#elif defined(VBOX_DRI) /* DRI2 */
1178 if (pVBox->drmFD >= 0)
1179 {
1180 /* Tell the kernel driver, if present, that we are taking over. */
1181 drmSetMaster(pVBox->drmFD);
1182 }
1183#endif
1184 vboxEnableVbva(pScrn);
1185 /* Re-assert this in case we had a change request while switched out. */
1186 if (pVBox->FBSize.cx && pVBox->FBSize.cy)
1187 VBOXAdjustScreenPixmap(pScrn, pVBox->FBSize.cx, pVBox->FBSize.cy);
1188#ifdef VBOXVIDEO_13
1189 if (!xf86SetDesiredModes(pScrn))
1190 return FALSE;
1191#else
1192 if (!VBOXSetMode(pScrn, 0, pScrn->currentMode->HDisplay,
1193 pScrn->currentMode->VDisplay, pScrn->frameX0,
1194 pScrn->frameY0))
1195 return FALSE;
1196#endif
1197#ifdef SET_HAVE_VT_PROPERTY
1198 updateHasVTProperty(pScrn, TRUE);
1199#endif
1200 return TRUE;
1201}
1202
1203static void VBOXLeaveVT(ScrnInfoPtr pScrn)
1204{
1205 VBOXPtr pVBox = VBOXGetRec(pScrn);
1206#ifdef VBOXVIDEO_13
1207 unsigned i;
1208#endif
1209
1210 TRACE_ENTRY();
1211#ifdef VBOXVIDEO_13
1212 for (i = 0; i < pVBox->cScreens; ++i)
1213 vbox_output_dpms(pVBox->pScreens[i].paOutputs, DPMSModeOff);
1214#endif
1215 vboxDisableVbva(pScrn);
1216 vboxClearVRAM(pScrn, 0, 0);
1217#ifdef VBOX_DRI_OLD
1218 if (pVBox->useDRI)
1219 DRILock(xf86ScrnToScreen(pScrn), 0);
1220#elif defined(VBOX_DRI) /* DRI2 */
1221 if (pVBox->drmFD >= 0)
1222 drmDropMaster(pVBox->drmFD);
1223#endif
1224 VBOXRestoreMode(pScrn);
1225#ifdef SET_HAVE_VT_PROPERTY
1226 updateHasVTProperty(pScrn, FALSE);
1227#endif
1228 TRACE_EXIT();
1229}
1230
1231static Bool VBOXCloseScreen(ScreenPtr pScreen)
1232{
1233 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1234 VBOXPtr pVBox = VBOXGetRec(pScrn);
1235#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1236 BOOL fRestore = TRUE;
1237#endif
1238 if (pScrn->vtSema)
1239 {
1240#ifdef VBOXVIDEO_13
1241 unsigned i;
1242
1243 for (i = 0; i < pVBox->cScreens; ++i)
1244 vbox_output_dpms(pVBox->pScreens[i].paOutputs, DPMSModeOff);
1245#endif
1246 vboxDisableVbva(pScrn);
1247 vboxClearVRAM(pScrn, 0, 0);
1248 }
1249#ifdef VBOX_DRI
1250# ifndef VBOX_DRI_OLD /* DRI2 */
1251 if ( pVBox->drmFD >= 0
1252 /* Tell the kernel driver, if present, that we are going away. */
1253 && drmIoctl(pVBox->drmFD, VBOXVIDEO_IOCTL_ENABLE_HGSMI, NULL) >= 0)
1254 fRestore = false;
1255# endif
1256 if (pVBox->useDRI)
1257 VBOXDRICloseScreen(pScreen, pVBox);
1258 pVBox->useDRI = false;
1259#endif
1260#if defined(VBOX_DRI) && !defined(VBOX_DRI_OLD) /* DRI2 */
1261 if (fRestore)
1262#endif
1263 if (pScrn->vtSema)
1264 VBOXRestoreMode(pScrn);
1265 if (pScrn->vtSema)
1266 VBOXUnmapVidMem(pScrn);
1267 pScrn->vtSema = FALSE;
1268
1269 /* Do additional bits which are separate for historical reasons */
1270 vbox_close(pScrn, pVBox);
1271
1272 pScreen->CloseScreen = pVBox->CloseScreen;
1273#if defined(VBOXVIDEO_13) && defined(RT_OS_LINUX)
1274 VBoxCleanUpLinuxACPI(pScreen);
1275#endif
1276#ifndef XF86_SCRN_INTERFACE
1277 return pScreen->CloseScreen(pScreen->myNum, pScreen);
1278#else
1279 return pScreen->CloseScreen(pScreen);
1280#endif
1281}
1282
1283static Bool VBOXSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr pMode)
1284{
1285 VBOXPtr pVBox;
1286 Bool rc;
1287
1288 TRACE_LOG("HDisplay=%d, VDisplay=%d\n", pMode->HDisplay, pMode->VDisplay);
1289#ifndef VBOXVIDEO_13
1290 pVBox = VBOXGetRec(pScrn);
1291 /* Save the size in case we need to re-set it later. */
1292 pVBox->FBSize.cx = pMode->HDisplay;
1293 pVBox->FBSize.cy = pMode->VDisplay;
1294 pVBox->pScreens[0].aScreenLocation.cx = pMode->HDisplay;
1295 pVBox->pScreens[0].aScreenLocation.cy = pMode->VDisplay;
1296 pVBox->pScreens[0].aScreenLocation.x = pScrn->frameX0;
1297 pVBox->pScreens[0].aScreenLocation.y = pScrn->frameY0;
1298#endif
1299 if (!pScrn->vtSema)
1300 {
1301 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1302 "We do not own the active VT, exiting.\n");
1303 return TRUE;
1304 }
1305#ifdef VBOXVIDEO_13
1306 rc = xf86SetSingleMode(pScrn, pMode, RR_Rotate_0);
1307#else
1308 VBOXAdjustScreenPixmap(pScrn, pMode->HDisplay, pMode->VDisplay);
1309 rc = VBOXSetMode(pScrn, 0, pMode->HDisplay, pMode->VDisplay,
1310 pScrn->frameX0, pScrn->frameY0);
1311#endif
1312 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1313 return rc;
1314}
1315
1316static void VBOXAdjustFrame(ScrnInfoPtr pScrn, int x, int y)
1317{
1318 VBOXPtr pVBox = VBOXGetRec(pScrn);
1319
1320 TRACE_ENTRY();
1321 pVBox->pScreens[0].aScreenLocation.x = x;
1322 pVBox->pScreens[0].aScreenLocation.y = y;
1323 /* Don't fiddle with the hardware if we are switched
1324 * to a virtual terminal. */
1325 if (!pScrn->vtSema)
1326 {
1327 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1328 "We do not own the active VT, exiting.\n");
1329 return;
1330 }
1331 VBOXSetMode(pScrn, 0, pVBox->pScreens[0].aScreenLocation.cx,
1332 pVBox->pScreens[0].aScreenLocation.cy, x, y);
1333 TRACE_EXIT();
1334}
1335
1336static void VBOXFreeScreen(ScrnInfoPtr pScrn)
1337{
1338 /* Destroy the VGA hardware record */
1339 vgaHWFreeHWRec(pScrn);
1340 /* And our private record */
1341 free(pScrn->driverPrivate);
1342 pScrn->driverPrivate = NULL;
1343}
1344
1345static Bool
1346VBOXMapVidMem(ScrnInfoPtr pScrn)
1347{
1348 VBOXPtr pVBox = VBOXGetRec(pScrn);
1349 Bool rc = TRUE;
1350
1351 TRACE_ENTRY();
1352 if (!pVBox->base)
1353 {
1354#ifdef PCIACCESS
1355 (void) pci_device_map_range(pVBox->pciInfo,
1356 pScrn->memPhysBase,
1357 pScrn->videoRam * 1024,
1358 PCI_DEV_MAP_FLAG_WRITABLE,
1359 & pVBox->base);
1360#else
1361 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
1362 VIDMEM_FRAMEBUFFER,
1363 pVBox->pciTag, pScrn->memPhysBase,
1364 (unsigned) pScrn->videoRam * 1024);
1365#endif
1366 if (!pVBox->base)
1367 rc = FALSE;
1368 }
1369 TRACE_LOG("returning %s\n", rc ? "TRUE" : "FALSE");
1370 return rc;
1371}
1372
1373static void
1374VBOXUnmapVidMem(ScrnInfoPtr pScrn)
1375{
1376 VBOXPtr pVBox = VBOXGetRec(pScrn);
1377
1378 TRACE_ENTRY();
1379 if (pVBox->base == NULL)
1380 return;
1381
1382#ifdef PCIACCESS
1383 (void) pci_device_unmap_range(pVBox->pciInfo,
1384 pVBox->base,
1385 pScrn->videoRam * 1024);
1386#else
1387 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
1388 (unsigned) pScrn->videoRam * 1024);
1389#endif
1390 pVBox->base = NULL;
1391 TRACE_EXIT();
1392}
1393
1394static Bool
1395VBOXSaveScreen(ScreenPtr pScreen, int mode)
1396{
1397 (void)pScreen; (void)mode;
1398 return TRUE;
1399}
1400
1401void
1402VBOXSaveMode(ScrnInfoPtr pScrn)
1403{
1404 VBOXPtr pVBox = VBOXGetRec(pScrn);
1405 vgaRegPtr vgaReg;
1406
1407 TRACE_ENTRY();
1408 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1409 vgaHWSave(pScrn, vgaReg, VGA_SR_ALL);
1410 pVBox->fSavedVBEMode = VBoxVideoGetModeRegisters(&pVBox->cSavedWidth,
1411 &pVBox->cSavedHeight,
1412 &pVBox->cSavedPitch,
1413 &pVBox->cSavedBPP,
1414 &pVBox->fSavedFlags);
1415}
1416
1417void
1418VBOXRestoreMode(ScrnInfoPtr pScrn)
1419{
1420 VBOXPtr pVBox = VBOXGetRec(pScrn);
1421 vgaRegPtr vgaReg;
1422#ifdef VBOX_DRI
1423 drmModeResPtr pRes;
1424#endif
1425
1426 TRACE_ENTRY();
1427#ifdef VBOX_DRI
1428 /* Do not try to re-set the VGA state if a mode-setting driver is loaded. */
1429 if ( pVBox->drmFD >= 0
1430 && LoaderSymbol("drmModeGetResources") != NULL
1431 && (pRes = drmModeGetResources(pVBox->drmFD)) != NULL)
1432 {
1433 drmModeFreeResources(pRes);
1434 return;
1435 }
1436#endif
1437 vgaReg = &VGAHWPTR(pScrn)->SavedReg;
1438 vgaHWRestore(pScrn, vgaReg, VGA_SR_ALL);
1439 if (pVBox->fSavedVBEMode)
1440 VBoxVideoSetModeRegisters(pVBox->cSavedWidth, pVBox->cSavedHeight,
1441 pVBox->cSavedPitch, pVBox->cSavedBPP,
1442 pVBox->fSavedFlags, 0, 0);
1443 else
1444 VBoxVideoDisableVBE();
1445}
1446
1447static void
1448VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
1449 int flags)
1450{
1451 (void)pScrn; (void)mode; (void) flags;
1452}
Note: See TracBrowser for help on using the repository browser.

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