VirtualBox

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

Last change on this file since 60133 was 60133, checked in by vboxsync, 9 years ago

bugref:8288: Additions/x11: rework VBoxClient video mode hint handling: remove explicit ACPI code and left-over Vbgl calls in the user space graphics driver. We probe for screen changes on every X server wake-up, and the X server wakes up on ACPI events even without our handler.

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