VirtualBox

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

Last change on this file since 55390 was 55384, checked in by vboxsync, 10 years ago

Additions/x11/vboxvideo: enable new mode hint handling code.

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