VirtualBox

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

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

bugref:8087: Additions/x11: support non-root X server: revert r105407: do not start the display part of VBoxClient at all when not using the user space X.Org vboxvideo driver: all guest systems capable of running the kernel driver should already have equivalent functionality.

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