VirtualBox

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

Last change on this file since 49839 was 49639, checked in by vboxsync, 11 years ago

Additions/x11/vboxvideo: fix virtual consoles if KMS is not present.

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