VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/xgraphics/vboxvideo.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

File size: 41.9 KB
Line 
1/** @file
2 *
3 * Linux Additions X11 graphics driver
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 *
21 * --------------------------------------------------------------------
22 *
23 * This code is based on:
24 *
25 * X11 VESA driver
26 *
27 * Copyright (c) 2000 by Conectiva S.A. (http://www.conectiva.com)
28 *
29 * Permission is hereby granted, free of charge, to any person obtaining a
30 * copy of this software and associated documentation files (the "Software"),
31 * to deal in the Software without restriction, including without limitation
32 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
33 * and/or sell copies of the Software, and to permit persons to whom the
34 * Software is furnished to do so, subject to the following conditions:
35 *
36 * The above copyright notice and this permission notice shall be included in
37 * all copies or substantial portions of the Software.
38 *
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
42 * CONECTIVA LINUX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
43 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
44 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45 * SOFTWARE.
46 *
47 * Except as contained in this notice, the name of Conectiva Linux shall
48 * not be used in advertising or otherwise to promote the sale, use or other
49 * dealings in this Software without prior written authorization from
50 * Conectiva Linux.
51 *
52 * Authors: Paulo C�ar Pereira de Andrade <[email protected]>
53 */
54
55#define DEBUG_VERB 2
56
57#ifdef XORG_7X
58# ifdef XFree86LOADER
59# include "xorg-server.h"
60# else
61# ifdef HAVE_CONFIG_H
62# include "config.h"
63# endif
64# endif
65#endif
66#include "vboxvideo.h"
67
68/* All drivers initialising the SW cursor need this */
69#include "mipointer.h"
70
71/* All drivers implementing backing store need this */
72#include "mibstore.h"
73
74/* Colormap handling */
75#include "micmap.h"
76#include "xf86cmap.h"
77
78/* DPMS */
79/* #define DPMS_SERVER
80#include "extensions/dpms.h" */
81
82/* Mandatory functions */
83
84static const OptionInfoRec * VBOXAvailableOptions(int chipid, int busid);
85static void VBOXIdentify(int flags);
86static Bool VBOXProbe(DriverPtr drv, int flags);
87static Bool VBOXPreInit(ScrnInfoPtr pScrn, int flags);
88static Bool VBOXScreenInit(int Index, ScreenPtr pScreen, int argc,
89 char **argv);
90static Bool VBOXEnterVT(int scrnIndex, int flags);
91static void VBOXLeaveVT(int scrnIndex, int flags);
92static Bool VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen);
93static Bool VBOXSaveScreen(ScreenPtr pScreen, int mode);
94static Bool VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags);
95static Bool VBOXSetMode(ScrnInfoPtr pScrn, DisplayModePtr pMode);
96static void VBOXAdjustFrame(int scrnIndex, int x, int y, int flags);
97static void VBOXFreeScreen(int scrnIndex, int flags);
98static void VBOXFreeRec(ScrnInfoPtr pScrn);
99static void VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
100 int flags);
101
102/* locally used functions */
103static Bool VBOXMapVidMem(ScrnInfoPtr pScrn);
104static void VBOXUnmapVidMem(ScrnInfoPtr pScrn);
105static void VBOXLoadPalette(ScrnInfoPtr pScrn, int numColors,
106 int *indices,
107 LOCO *colors, VisualPtr pVisual);
108static void SaveFonts(ScrnInfoPtr pScrn);
109static void RestoreFonts(ScrnInfoPtr pScrn);
110static Bool VBOXSaveRestore(ScrnInfoPtr pScrn,
111 vbeSaveRestoreFunction function);
112
113/* Initialise DGA */
114
115static Bool VBOXDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen);
116
117/*
118 * This contains the functions needed by the server after loading the
119 * driver module. It must be supplied, and gets added the driver list by
120 * the Module Setup funtion in the dynamic case. In the static case a
121 * reference to this is compiled in, and this requires that the name of
122 * this DriverRec be an upper-case version of the driver name.
123 */
124
125#ifdef XORG_7X
126_X_EXPORT DriverRec VBOXDRV = {
127#else
128DriverRec VBOXDRV = {
129#endif
130 VBOX_VERSION,
131 VBOX_DRIVER_NAME,
132 VBOXIdentify,
133 VBOXProbe,
134 VBOXAvailableOptions,
135 NULL,
136#ifdef XORG_7X
137 0,
138 NULL
139#else
140 0
141#endif
142};
143
144/* Supported chipsets */
145static SymTabRec VBOXChipsets[] =
146{
147 {VBOX_VESA_DEVICEID, "vbox"},
148 {-1, NULL}
149};
150
151static PciChipsets VBOXPCIchipsets[] = {
152 { VBOX_DEVICEID, VBOX_DEVICEID, RES_SHARED_VGA },
153 { -1, -1, RES_UNDEFINED },
154};
155
156typedef enum {
157 OPTION_SHADOW_FB
158#ifdef XORG_7X
159 ,
160 OPTION_DFLT_REFRESH,
161 OPTION_MODESET_CLEAR_SCREEN
162#endif
163} VBOXOpts;
164
165/* No options for now */
166static const OptionInfoRec VBOXOptions[] = {
167 { -1, NULL, OPTV_NONE, {0}, FALSE }
168};
169
170/*
171 * List of symbols from other modules that this module references. This
172 * list is used to tell the loader that it is OK for symbols here to be
173 * unresolved providing that it hasn't been told that they haven't been
174 * told that they are essential via a call to xf86LoaderReqSymbols() or
175 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
176 * unresolved symbols that are not required.
177 */
178static const char *fbSymbols[] = {
179 "fbPictureInit",
180 "fbScreenInit",
181 NULL
182};
183
184static const char *shadowfbSymbols[] = {
185 "ShadowFBInit2",
186 NULL
187};
188
189static const char *vbeSymbols[] = {
190#ifdef XORG_7X
191 "VBEExtendedInit",
192 "VBEFindSupportedDepths",
193#else /* !XORG_7X */
194 "VBEFreeModeInfo",
195#endif /* XORG_7X */
196 "VBEGetModeInfo",
197 "VBEGetVBEInfo",
198 "VBEGetVBEMode",
199#ifdef XORG_7X
200 "VBEPrintModes",
201#else /* !XORG_7X */
202 "VBEInit",
203#endif /* XORG_7X */
204 "VBESaveRestore",
205 "VBESetDisplayStart",
206 "VBESetGetDACPaletteFormat",
207 "VBESetGetLogicalScanlineLength",
208 "VBESetGetPaletteData",
209#ifdef XORG_7X
210 "VBESetModeNames",
211 "VBESetModeParameters",
212#endif
213 "VBESetVBEMode",
214#ifdef XORG_7X
215 "VBEValidateModes",
216#endif
217 "vbeDoEDID",
218 "vbeFree",
219 NULL
220};
221
222static const char *ramdacSymbols[] = {
223 "xf86InitCursor",
224 "xf86CreateCursorInfoRec",
225 NULL
226};
227
228#ifdef XFree86LOADER
229/* Module loader interface */
230static MODULESETUPPROTO(vboxSetup);
231
232static XF86ModuleVersionInfo vboxVersionRec =
233{
234 VBOX_DRIVER_NAME,
235 "InnoTek Systemberatung GmbH",
236 MODINFOSTRING1,
237 MODINFOSTRING2,
238#ifdef XORG_7X
239 XORG_VERSION_CURRENT,
240#else /* !XORG_7X */
241 XF86_VERSION_CURRENT,
242#endif /* XORG_7X */
243 VBOX_MAJOR_VERSION, VBOX_MINOR_VERSION, VBOX_PATCHLEVEL,
244 ABI_CLASS_VIDEODRV, /* This is a video driver */
245 ABI_VIDEODRV_VERSION,
246 MOD_CLASS_VIDEODRV,
247 {0, 0, 0, 0}
248};
249
250/*
251 * This data is accessed by the loader. The name must be the module name
252 * followed by "ModuleData".
253 */
254#ifdef XORG_7X
255_X_EXPORT XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
256#else /* !XORG_7X */
257XF86ModuleData vboxvideoModuleData = { &vboxVersionRec, vboxSetup, NULL };
258#endif /* XORG_7X */
259
260static pointer
261vboxSetup(pointer Module, pointer Options, int *ErrorMajor, int *ErrorMinor)
262{
263 static Bool Initialised = FALSE;
264
265 if (!Initialised)
266 {
267 Initialised = TRUE;
268 xf86AddDriver(&VBOXDRV, Module, 0);
269 LoaderRefSymLists(fbSymbols,
270 shadowfbSymbols,
271 vbeSymbols,
272 ramdacSymbols,
273 NULL);
274 return (pointer)TRUE;
275 }
276
277 if (ErrorMajor)
278 *ErrorMajor = LDR_ONCEONLY;
279 return (NULL);
280}
281
282#endif /* XFree86Loader defined */
283
284static const OptionInfoRec *
285VBOXAvailableOptions(int chipid, int busid)
286{
287 return (VBOXOptions);
288}
289
290static void
291VBOXIdentify(int flags)
292{
293 xf86PrintChipsets(VBOX_NAME, "guest driver for VBox", VBOXChipsets);
294}
295
296/*
297 * This function is called once, at the start of the first server generation to
298 * do a minimal probe for supported hardware.
299 */
300
301static Bool
302VBOXProbe(DriverPtr drv, int flags)
303{
304 Bool foundScreen = FALSE;
305 int numDevSections, numUsed;
306 GDevPtr *devSections;
307 int *usedChips;
308 int i;
309
310 /*
311 * Find the config file Device sections that match this
312 * driver, and return if there are none.
313 */
314 if ((numDevSections = xf86MatchDevice(VBOX_NAME,
315 &devSections)) <= 0)
316 return (FALSE);
317
318 /* PCI BUS */
319 if (xf86GetPciVideoInfo()) {
320 numUsed = xf86MatchPciInstances(VBOX_NAME, VBOX_VENDORID,
321 VBOXChipsets, VBOXPCIchipsets,
322 devSections, numDevSections,
323 drv, &usedChips);
324 if (numUsed > 0) {
325 if (flags & PROBE_DETECT)
326 foundScreen = TRUE;
327 else {
328 for (i = 0; i < numUsed; i++) {
329 ScrnInfoPtr pScrn = NULL;
330 /* Allocate a ScrnInfoRec */
331 if ((pScrn = xf86ConfigPciEntity(pScrn,0,usedChips[i],
332 VBOXPCIchipsets,NULL,
333 NULL,NULL,NULL,NULL))) {
334 pScrn->driverVersion = VBOX_VERSION;
335 pScrn->driverName = VBOX_DRIVER_NAME;
336 pScrn->name = VBOX_NAME;
337 pScrn->Probe = VBOXProbe;
338 pScrn->PreInit = VBOXPreInit;
339 pScrn->ScreenInit = VBOXScreenInit;
340 pScrn->SwitchMode = VBOXSwitchMode;
341 pScrn->AdjustFrame = VBOXAdjustFrame;
342 pScrn->EnterVT = VBOXEnterVT;
343 pScrn->LeaveVT = VBOXLeaveVT;
344 pScrn->FreeScreen = VBOXFreeScreen;
345 foundScreen = TRUE;
346 }
347 }
348 }
349 xfree(usedChips);
350 }
351 }
352 xfree(devSections);
353
354 return (foundScreen);
355}
356
357static VBOXPtr
358VBOXGetRec(ScrnInfoPtr pScrn)
359{
360 if (!pScrn->driverPrivate)
361 pScrn->driverPrivate = xcalloc(sizeof(VBOXRec), 1);
362
363 return ((VBOXPtr)pScrn->driverPrivate);
364}
365
366static void
367VBOXFreeRec(ScrnInfoPtr pScrn)
368{
369 VBOXPtr pVBox = VBOXGetRec(pScrn);
370 xfree(pVBox->vbeInfo);
371 xfree(pVBox->savedPal);
372 xfree(pVBox->fonts);
373 xfree(pScrn->driverPrivate);
374 pScrn->driverPrivate = NULL;
375}
376
377/*
378 * QUOTE from the XFree86 DESIGN document:
379 *
380 * The purpose of this function is to find out all the information
381 * required to determine if the configuration is usable, and to initialise
382 * those parts of the ScrnInfoRec that can be set once at the beginning of
383 * the first server generation.
384 *
385 * (...)
386 *
387 * This includes probing for video memory, clocks, ramdac, and all other
388 * HW info that is needed. It includes determining the depth/bpp/visual
389 * and related info. It includes validating and determining the set of
390 * video modes that will be used (and anything that is required to
391 * determine that).
392 *
393 * This information should be determined in the least intrusive way
394 * possible. The state of the HW must remain unchanged by this function.
395 * Although video memory (including MMIO) may be mapped within this
396 * function, it must be unmapped before returning.
397 *
398 * END QUOTE
399 */
400
401static Bool
402VBOXPreInit(ScrnInfoPtr pScrn, int flags)
403{
404 VBOXPtr pVBox;
405 Gamma gzeros = {0.0, 0.0, 0.0};
406 rgb rzeros = {0, 0, 0};
407 ClockRange *clockRanges;
408 int i;
409
410 /* Are we really starting the server, or is this just a dummy run? */
411 if (flags & PROBE_DETECT)
412 return (FALSE);
413
414 /* Get our private data from the ScrnInfoRec structure. */
415 pVBox = VBOXGetRec(pScrn);
416
417 /* Entity information seems to mean bus information. */
418 pVBox->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
419 if (pVBox->pEnt->location.type != BUS_PCI)
420 return FALSE;
421
422 /* The ramdac module is needed for the hardware cursor. */
423 if (!xf86LoadSubModule(pScrn, "ramdac"))
424 return FALSE;
425 xf86LoaderReqSymLists(ramdacSymbols, NULL);
426
427 /* We need the vbe module because we use VBE code to save and restore
428 text mode, in order to keep our code simple. */
429 if (!xf86LoadSubModule(pScrn, "vbe"))
430 return (FALSE);
431 xf86LoaderReqSymLists(vbeSymbols, NULL);
432
433 /* The framebuffer module. */
434 if (xf86LoadSubModule(pScrn, "fb") == NULL)
435 return (FALSE);
436 xf86LoaderReqSymLists(fbSymbols, NULL);
437
438 if (!xf86LoadSubModule(pScrn, "shadowfb"))
439 return FALSE;
440 xf86LoaderReqSymLists(shadowfbSymbols, NULL);
441
442 pVBox->pciInfo = xf86GetPciInfoForEntity(pVBox->pEnt->index);
443 pVBox->pciTag = pciTag(pVBox->pciInfo->bus,
444 pVBox->pciInfo->device,
445 pVBox->pciInfo->func);
446
447 /* Set up our ScrnInfoRec structure to describe our virtual
448 capabilities to X. */
449
450 pScrn->rgbBits = 8;
451
452 /* Let's create a nice, capable virtual monitor. */
453 pScrn->monitor = pScrn->confScreen->monitor;
454 pScrn->monitor->DDC = NULL;
455 pScrn->monitor->nHsync = 1;
456 pScrn->monitor->hsync[0].lo = 1;
457 pScrn->monitor->hsync[0].hi = 10000;
458 pScrn->monitor->nVrefresh = 1;
459 pScrn->monitor->vrefresh[0].lo = 1;
460 pScrn->monitor->vrefresh[0].hi = 100;
461
462 pScrn->chipset = "vbox";
463 pScrn->progClock = TRUE;
464
465 /* Determine the size of the VBox video RAM from PCI data*/
466#if 0
467 pScrn->videoRam = 1 << pVBox->pciInfo->size[0];
468#endif
469 /* Using the PCI information caused problems with non-powers-of-two
470 sized video RAM configurations */
471 pScrn->videoRam = inl(VBE_DISPI_IOPORT_DATA) / 1024;
472
473 /* Set up clock information that will support all modes we need. */
474 clockRanges = xnfcalloc(sizeof(ClockRange), 1);
475 clockRanges->next = NULL;
476 clockRanges->minClock = 1000;
477 clockRanges->maxClock = 1000000000;
478 clockRanges->clockIndex = -1;
479 clockRanges->ClockMulFactor = 1;
480 clockRanges->ClockDivFactor = 1;
481
482 /* This function asks X to choose a depth and bpp based on the
483 config file and the command line, and gives a default in
484 case none is specified. Note that we only support 32bpp, not
485 24bpp. After spending ages looking through the XFree86 4.2
486 source code however, I realised that it automatically uses
487 32bpp for depth 24 unless you explicitly add a "24 24"
488 format to its internal list. */
489 if (!xf86SetDepthBpp(pScrn, pScrn->videoRam >= 2048 ? 24 : 16, 0, 0,
490 Support32bppFb))
491 return FALSE;
492 if (pScrn->depth != 24 && pScrn->depth != 16)
493 {
494 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
495 "The VBox additions only support 16 and 32bpp graphics modes\n");
496 return FALSE;
497 }
498 xf86PrintDepthBpp(pScrn);
499
500 /* Colour weight - we always call this, since we are always in
501 truecolour. */
502 if (!xf86SetWeight(pScrn, rzeros, rzeros))
503 return (FALSE);
504
505 /* visual init */
506 if (!xf86SetDefaultVisual(pScrn, -1))
507 return (FALSE);
508
509 xf86SetGamma(pScrn, gzeros);
510
511 /* Filter out video modes not supported by the virtual hardware
512 we described. All modes used by the Windows additions should
513 work fine. */
514 i = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
515 pScrn->display->modes,
516 clockRanges, NULL, 0, 6400, 1, 0, 1440,
517 pScrn->display->virtualX,
518 pScrn->display->virtualY,
519 pScrn->videoRam, LOOKUP_BEST_REFRESH);
520
521 if (i <= 0) {
522 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No usable graphics modes found.\n");
523 return (FALSE);
524 }
525 xf86PruneDriverModes(pScrn);
526
527 pScrn->currentMode = pScrn->modes;
528 pScrn->displayWidth = pScrn->virtualX;
529
530 xf86PrintModes(pScrn);
531
532 /* Set display resolution */
533 xf86SetDpi(pScrn, 0, 0);
534
535 if (pScrn->modes == NULL) {
536 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No graphics modes available\n");
537 return (FALSE);
538 }
539
540 /* options */
541 xf86CollectOptions(pScrn, NULL);
542 if (!(pVBox->Options = xalloc(sizeof(VBOXOptions))))
543 return FALSE;
544 memcpy(pVBox->Options, VBOXOptions, sizeof(VBOXOptions));
545 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pVBox->Options);
546
547 /* Framebuffer-related setup */
548 pScrn->bitmapBitOrder = BITMAP_BIT_ORDER;
549 return (TRUE);
550}
551
552/*
553 * QUOTE from the XFree86 DESIGN document:
554 *
555 * This is called at the start of each server generation.
556 *
557 * (...)
558 *
559 * Decide which operations need to be placed under resource access
560 * control. (...) Map any video memory or other memory regions. (...)
561 * Save the video card state. (...) Initialise the initial video
562 * mode.
563 *
564 * End QUOTE.Initialise the initial video mode.
565 */
566static Bool
567VBOXScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
568{
569 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
570 VBOXPtr pVBox = VBOXGetRec(pScrn);
571 VisualPtr visual;
572 unsigned flags;
573
574 /* We make use of the X11 VBE code to save and restore text mode, in
575 order to keep our code simple. */
576#ifdef XORG_7X
577 if ((pVBox->pVbe = VBEExtendedInit(NULL, pVBox->pEnt->index,
578 SET_BIOS_SCRATCH
579 | RESTORE_BIOS_SCRATCH)) == NULL)
580#else
581 if ((pVBox->pVbe = VBEInit(NULL, pVBox->pEnt->index)) == NULL)
582#endif
583 return (FALSE);
584
585 if (pVBox->mapPhys == 0) {
586 pVBox->mapPhys = pVBox->pciInfo->memBase[0];
587/* pVBox->mapSize = 1 << pVBox->pciInfo->size[0]; */
588 /* Using the PCI information caused problems with
589 non-powers-of-two sized video RAM configurations */
590 pVBox->mapSize = inl(VBE_DISPI_IOPORT_DATA);
591 pVBox->mapOff = 0;
592 }
593
594 if (!VBOXMapVidMem(pScrn))
595 return (FALSE);
596
597 /* save current video state */
598 VBOXSaveRestore(pScrn, MODE_SAVE);
599 pVBox->savedPal = VBESetGetPaletteData(pVBox->pVbe, FALSE, 0, 256,
600 NULL, FALSE, FALSE);
601
602 /* set first video mode */
603 if (!VBOXSetMode(pScrn, pScrn->currentMode))
604 return FALSE;
605
606#ifdef XORG_7X
607 /* set the viewport */
608 VBOXAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
609
610 /* Blank the screen for aesthetic reasons. */
611 VBOXSaveScreen(pScreen, SCREEN_SAVER_ON);
612#endif
613
614 /* mi layer - reset the visual list (?)*/
615 miClearVisualTypes();
616 if (!xf86SetDefaultVisual(pScrn, -1))
617 return (FALSE);
618 if (!miSetVisualTypes(pScrn->depth, TrueColorMask,
619 pScrn->rgbBits, TrueColor))
620 return (FALSE);
621 if (!miSetPixmapDepths())
622 return (FALSE);
623
624 /* I checked in the sources, and XFree86 4.2 does seem to support
625 this function for 32bpp. */
626 if (!fbScreenInit(pScreen, pVBox->base,
627 pScrn->virtualX, pScrn->virtualY,
628 pScrn->xDpi, pScrn->yDpi,
629 pScrn->displayWidth, pScrn->bitsPerPixel))
630 return (FALSE);
631
632 /* Fixup RGB ordering */
633 visual = pScreen->visuals + pScreen->numVisuals;
634 while (--visual >= pScreen->visuals) {
635 if ((visual->class | DynamicClass) == DirectColor) {
636 visual->offsetRed = pScrn->offset.red;
637 visual->offsetGreen = pScrn->offset.green;
638 visual->offsetBlue = pScrn->offset.blue;
639 visual->redMask = pScrn->mask.red;
640 visual->greenMask = pScrn->mask.green;
641 visual->blueMask = pScrn->mask.blue;
642 }
643 }
644
645 /* must be after RGB ordering fixed */
646 fbPictureInit(pScreen, 0, 0);
647
648 VBOXDGAInit(pScrn, pScreen);
649
650 xf86SetBlackWhitePixels(pScreen);
651 miInitializeBackingStore(pScreen);
652 xf86SetBackingStore(pScreen);
653
654 /* software cursor */
655 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
656
657 /* colourmap code - apparently, we need this even in Truecolour */
658 if (!miCreateDefColormap(pScreen))
659 return (FALSE);
660
661 flags = CMAP_RELOAD_ON_MODE_SWITCH;
662
663 if(!xf86HandleColormaps(pScreen, 256,
664 pVBox->vbeInfo->Capabilities[0] & 0x01 ? 8 : 6,
665 VBOXLoadPalette, NULL, flags))
666 return (FALSE);
667
668 pVBox->CloseScreen = pScreen->CloseScreen;
669 pScreen->CloseScreen = VBOXCloseScreen;
670 pScreen->SaveScreen = VBOXSaveScreen;
671
672 /* However, we probably do want to support power management -
673 even if we just use a dummy function. */
674 xf86DPMSInit(pScreen, VBOXDisplayPowerManagementSet, 0);
675
676 /* Report any unused options (only for the first generation) */
677 if (serverGeneration == 1)
678 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
679
680 pVBox->vbox_fd = -1;
681 if (vbox_open (pScrn, pScreen, pVBox)) {
682 if (vbox_cursor_init(pScreen) != TRUE)
683 xf86DrvMsg(scrnIndex, X_ERROR,
684 "Unable to start the VirtualBox mouse pointer integration with the host system.\n");
685 if (vboxEnableVbva(pScrn) == TRUE)
686 xf86DrvMsg(scrnIndex, X_INFO,
687 "The VBox video extensions are now enabled.\n");
688 } else
689 xf86DrvMsg(scrnIndex, X_ERROR, "Failed to open the VBox system device - make sure that the VirtualBox guest additions are properly installed. If you are not sure, try reinstalling them.\n");
690 return (TRUE);
691}
692
693static Bool
694VBOXEnterVT(int scrnIndex, int flags)
695{
696 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
697 VBOXPtr pVBox = VBOXGetRec(pScrn);
698
699 if (!VBOXSetMode(pScrn, pScrn->currentMode))
700 return FALSE;
701#ifdef XORG_7X
702 VBOXAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
703#endif /* XORG_7X defined */
704 if (pVBox->useVbva == TRUE)
705 vboxEnableVbva(pScrn);
706 return TRUE;
707}
708
709static void
710VBOXLeaveVT(int scrnIndex, int flags)
711{
712 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
713 VBOXPtr pVBox = VBOXGetRec(pScrn);
714
715 VBOXSaveRestore(pScrn, MODE_RESTORE);
716 if (pVBox->useVbva == TRUE)
717 vboxDisableVbva(pScrn);
718}
719
720static Bool
721VBOXCloseScreen(int scrnIndex, ScreenPtr pScreen)
722{
723 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
724 VBOXPtr pVBox = VBOXGetRec(pScrn);
725
726 if (pVBox->useVbva == TRUE)
727 vboxDisableVbva(pScrn);
728 if (pScrn->vtSema) {
729 VBOXSaveRestore(xf86Screens[scrnIndex], MODE_RESTORE);
730#ifdef XORG_7X
731 if (pVBox->savedPal)
732 VBESetGetPaletteData(pVBox->pVbe, TRUE, 0, 256,
733 pVBox->savedPal, FALSE, TRUE);
734#else
735 VBESetGetPaletteData(pVBox->pVbe, TRUE, 0, 256,
736 pVBox->savedPal, FALSE, TRUE);
737#endif
738 VBOXUnmapVidMem(pScrn);
739 }
740 if (pVBox->pDGAMode) {
741 xfree(pVBox->pDGAMode);
742 pVBox->pDGAMode = NULL;
743 pVBox->nDGAMode = 0;
744 }
745 pScrn->vtSema = FALSE;
746
747 pScreen->CloseScreen = pVBox->CloseScreen;
748 return pScreen->CloseScreen(scrnIndex, pScreen);
749}
750
751static Bool
752VBOXSwitchMode(int scrnIndex, DisplayModePtr pMode, int flags)
753{
754 ScrnInfoPtr pScrn;
755 VBOXPtr pVBox;
756
757 pScrn = xf86Screens[scrnIndex]; /* Why does X have three ways of refering to the screen? */
758 pVBox = VBOXGetRec(pScrn);
759 if (pVBox->useVbva == TRUE)
760 if (vboxDisableVbva(pScrn) != TRUE) /* This would be bad. */
761 return FALSE;
762 if (VBOXSetMode(pScrn, pMode) != TRUE)
763 return FALSE;
764 if (pVBox->useVbva == TRUE)
765 if (vboxEnableVbva(pScrn) != TRUE) /* Bad but not fatal */
766 pVBox->useVbva = FALSE;
767 return TRUE;
768}
769
770/* Set a graphics mode */
771static Bool
772VBOXSetMode(ScrnInfoPtr pScrn, DisplayModePtr pMode)
773{
774 VBOXPtr pVBox;
775
776 int bpp = pScrn->depth == 24 ? 32 : 16;
777 int xRes = pMode->HDisplay;
778 if (pScrn->virtualX * pScrn->virtualY * bpp / 8
779 >= pScrn->videoRam * 1024)
780 {
781 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
782 "Unable to set up a virtual screen size of %dx%d with %d Kb of video memory. Please increase the video memory size.\n",
783 pScrn->virtualX, pScrn->virtualY, pScrn->videoRam);
784 return FALSE;
785 }
786 /* We only support horizontal resolutions which are a multiple of 8. Round down if
787 necessary. */
788 if (xRes % 8 != 0)
789 {
790 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
791 "VirtualBox only supports screen widths which are a multiple of 8. Rounding down from %d to %d\n",
792 xRes, xRes - (xRes % 8));
793 xRes = xRes - (xRes % 8);
794 }
795 pVBox = VBOXGetRec(pScrn);
796 pScrn->vtSema = TRUE;
797 /* Disable linear framebuffer mode before making changes to the resolution. */
798 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
799 outw(VBE_DISPI_IOPORT_DATA,
800 VBE_DISPI_DISABLED);
801 /* Unlike the resolution, the depth is fixed for a given screen
802 for the lifetime of the X session. */
803 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_BPP);
804 outw(VBE_DISPI_IOPORT_DATA, bpp);
805 /* HDisplay and VDisplay are actually monitor information about
806 the display part of the scanlines. */
807 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_XRES);
808 outw(VBE_DISPI_IOPORT_DATA, xRes);
809 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_YRES);
810 outw(VBE_DISPI_IOPORT_DATA, pMode->VDisplay);
811 /* Enable linear framebuffer mode. */
812 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_ENABLE);
813 outw(VBE_DISPI_IOPORT_DATA,
814 VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
815 /* Set the virtual resolution. We are still using VESA to control
816 the virtual offset. */
817 outw(VBE_DISPI_IOPORT_INDEX, VBE_DISPI_INDEX_VIRT_WIDTH);
818 outw(VBE_DISPI_IOPORT_DATA, pScrn->displayWidth);
819 return (TRUE);
820}
821
822static void
823VBOXAdjustFrame(int scrnIndex, int x, int y, int flags)
824{
825 VBOXPtr pVBox = VBOXGetRec(xf86Screens[scrnIndex]);
826
827 VBESetDisplayStart(pVBox->pVbe, x, y, TRUE);
828}
829
830static void
831VBOXFreeScreen(int scrnIndex, int flags)
832{
833 VBOXFreeRec(xf86Screens[scrnIndex]);
834}
835
836static Bool
837VBOXMapVidMem(ScrnInfoPtr pScrn)
838{
839 VBOXPtr pVBox = VBOXGetRec(pScrn);
840
841 if (pVBox->base != NULL)
842 return (TRUE);
843
844 pScrn->memPhysBase = pVBox->mapPhys;
845 pScrn->fbOffset = pVBox->mapOff;
846
847 pVBox->base = xf86MapPciMem(pScrn->scrnIndex,
848 VIDMEM_FRAMEBUFFER,
849 pVBox->pciTag, pVBox->mapPhys,
850 (unsigned) pVBox->mapSize);
851
852 if (pVBox->base) {
853 pScrn->memPhysBase = pVBox->mapPhys;
854# ifdef XORG_7X
855 pVBox->VGAbase = xf86MapDomainMemory(pScrn->scrnIndex, 0,
856 pVBox->pciTag,
857 0xa0000, 0x10000);
858# else
859 pVBox->VGAbase = xf86MapVidMem(pScrn->scrnIndex, 0,
860 0xa0000, 0x10000);
861# endif
862 }
863#ifdef XORG_7X
864 /* We need this for saving/restoring textmode */
865 pVBox->ioBase = pScrn->domainIOBase;
866#endif
867
868 return (pVBox->base != NULL);
869}
870
871static void
872VBOXUnmapVidMem(ScrnInfoPtr pScrn)
873{
874 VBOXPtr pVBox = VBOXGetRec(pScrn);
875
876 if (pVBox->base == NULL)
877 return;
878
879 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->base,
880 (unsigned) pVBox->mapSize);
881 xf86UnMapVidMem(pScrn->scrnIndex, pVBox->VGAbase, 0x10000);
882 pVBox->base = NULL;
883}
884
885static void
886VBOXLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
887 LOCO *colors, VisualPtr pVisual)
888{
889#ifdef XORG_7X
890 VBOXPtr pVBox = VBOXGetRec(pScrn);
891 int i, idx;
892#endif
893#ifdef XORG_7X
894#define VBOXDACDelay() \
895 do { \
896 (void)inb(pVBox->ioBase + VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET); \
897 (void)inb(pVBox->ioBase + VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET); \
898 } while (0)
899#else
900#define WriteDacWriteAddr(value) outb(VGA_DAC_WRITE_ADDR, value)
901#define WriteDacData(value) outb(VGA_DAC_DATA, value);
902#undef DACDelay
903#define DACDelay() \
904 do { \
905 unsigned char temp = inb(VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET); \
906 temp = inb(VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET); \
907 } while (0)
908 int i, idx;
909#endif
910
911 for (i = 0; i < numColors; i++) {
912 idx = indices[i];
913#ifdef XORG_7X
914 outb(pVBox->ioBase + VGA_DAC_WRITE_ADDR, idx);
915 VBOXDACDelay();
916 outb(pVBox->ioBase + VGA_DAC_DATA, colors[idx].red);
917 VBOXDACDelay();
918 outb(pVBox->ioBase + VGA_DAC_DATA, colors[idx].green);
919 VBOXDACDelay();
920 outb(pVBox->ioBase + VGA_DAC_DATA, colors[idx].blue);
921 VBOXDACDelay();
922#else
923 WriteDacWriteAddr(idx);
924 DACDelay();
925 WriteDacData(colors[idx].red);
926 DACDelay();
927 WriteDacData(colors[idx].green);
928 DACDelay();
929 WriteDacData(colors[idx].blue);
930 DACDelay();
931#endif
932 }
933}
934
935/*
936 * Just adapted from the std* functions in vgaHW.c
937 */
938static void
939#ifdef XORG_7X
940WriteAttr(VBOXPtr pVBox, int index, int value)
941{
942 (void) inb(pVBox->ioBase + VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET);
943
944 index |= 0x20;
945 outb(pVBox->ioBase + VGA_ATTR_INDEX, index);
946 outb(pVBox->ioBase + VGA_ATTR_DATA_W, value);
947#else
948WriteAttr(int index, int value)
949{
950 CARD8 tmp;
951
952 tmp = inb(VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET);
953
954 index |= 0x20;
955 outb(VGA_ATTR_INDEX, index);
956 outb(VGA_ATTR_DATA_W, value);
957#endif
958}
959
960static int
961#ifdef XORG_7X
962ReadAttr(VBOXPtr pVBox, int index)
963{
964 (void) inb(pVBox->ioBase + VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET);
965
966 index |= 0x20;
967 outb(pVBox->ioBase + VGA_ATTR_INDEX, index);
968 return (inb(pVBox->ioBase + VGA_ATTR_DATA_R));
969#else
970ReadAttr(int index)
971{
972 CARD8 tmp;
973
974 tmp = inb(VGA_IOBASE_COLOR + VGA_IN_STAT_1_OFFSET);
975
976 index |= 0x20;
977 outb(VGA_ATTR_INDEX, index);
978 return (inb(VGA_ATTR_DATA_R));
979#endif
980}
981
982#ifdef XORG_7X
983#define WriteMiscOut(value) outb(pVBox->ioBase + VGA_MISC_OUT_W, value)
984#define ReadMiscOut() inb(pVBox->ioBase + VGA_MISC_OUT_R)
985#define WriteSeq(index, value) \
986 outb(pVBox->ioBase + VGA_SEQ_INDEX, (index));\
987 outb(pVBox->ioBase + VGA_SEQ_DATA, value)
988#else
989#define WriteMiscOut(value) outb(VGA_MISC_OUT_W, value)
990#define ReadMiscOut() inb(VGA_MISC_OUT_R)
991#define WriteSeq(index, value) \
992 outb(VGA_SEQ_INDEX, (index));\
993 outb(VGA_SEQ_DATA, value)
994#endif
995
996static int
997#ifdef XORG_7X
998ReadSeq(VBOXPtr pVBox, int index)
999{
1000 outb(pVBox->ioBase + VGA_SEQ_INDEX, index);
1001
1002 return (inb(pVBox->ioBase + VGA_SEQ_DATA));
1003#else
1004ReadSeq(int index)
1005{
1006 outb(VGA_SEQ_INDEX, index);
1007
1008 return (inb(VGA_SEQ_DATA));
1009#endif
1010}
1011
1012#ifdef XORG_7X
1013#define WriteGr(index, value) \
1014 outb(pVBox->ioBase + VGA_GRAPH_INDEX, index); \
1015 outb(pVBox->ioBase + VGA_GRAPH_DATA, value)
1016#else
1017#define WriteGr(index, value) outb(VGA_GRAPH_INDEX, index);\
1018 outb(VGA_GRAPH_DATA, value)
1019#endif
1020
1021static int
1022#ifdef XORG_7X
1023ReadGr(VBOXPtr pVBox, int index)
1024{
1025 outb(pVBox->ioBase + VGA_GRAPH_INDEX, index);
1026
1027 return (inb(pVBox->ioBase + VGA_GRAPH_DATA));
1028#else
1029ReadGr(int index)
1030{
1031 outb(VGA_GRAPH_INDEX, index);
1032
1033 return (inb(VGA_GRAPH_DATA));
1034#endif
1035}
1036
1037#ifdef XORG_7X
1038#define WriteCrtc(index, value) \
1039 outb(pVBox->ioBase + (VGA_IOBASE_COLOR + VGA_CRTC_INDEX_OFFSET), index); \
1040 outb(pVBox->ioBase + (VGA_IOBASE_COLOR + VGA_CRTC_DATA_OFFSET), value)
1041#else
1042#define WriteCrtc(index, value) outb(VGA_CRTC_INDEX_OFFSET, index);\
1043 outb(VGA_CRTC_DATA_OFFSET, value)
1044#endif
1045
1046static void
1047#ifdef XORG_7X
1048SeqReset(VBOXPtr pVBox, Bool start)
1049{
1050 if (start) {
1051 WriteSeq(0x00, 0x01); /* Synchronous Reset */
1052 }
1053 else {
1054 WriteSeq(0x00, 0x03); /* End Reset */
1055 }
1056#else
1057SeqReset(Bool start)
1058{
1059 if (start) {
1060 WriteSeq(0x00, 0x01); /* Synchronous Reset */
1061 }
1062 else {
1063 WriteSeq(0x00, 0x03); /* End Reset */
1064 }
1065#endif
1066}
1067
1068static void
1069SaveFonts(ScrnInfoPtr pScrn)
1070{
1071 VBOXPtr pVBox = VBOXGetRec(pScrn);
1072 unsigned char miscOut, attr10, gr4, gr5, gr6, seq2, seq4, scrn;
1073
1074 if (pVBox->fonts != NULL)
1075 return;
1076
1077 /* If in graphics mode, don't save anything */
1078#ifdef XORG_7X
1079 attr10 = ReadAttr(pVBox, 0x10);
1080#else
1081 attr10 = ReadAttr(0x10);
1082#endif
1083 if (attr10 & 0x01)
1084 return;
1085
1086 pVBox->fonts = xalloc(16384);
1087
1088 /* save the registers that are needed here */
1089 miscOut = ReadMiscOut();
1090#ifdef XORG_7X
1091 gr4 = ReadGr(pVBox, 0x04);
1092 gr5 = ReadGr(pVBox, 0x05);
1093 gr6 = ReadGr(pVBox, 0x06);
1094 seq2 = ReadSeq(pVBox, 0x02);
1095 seq4 = ReadSeq(pVBox, 0x04);
1096#else
1097 gr4 = ReadGr(0x04);
1098 gr5 = ReadGr(0x05);
1099 gr6 = ReadGr(0x06);
1100 seq2 = ReadSeq(0x02);
1101 seq4 = ReadSeq(0x04);
1102#endif
1103
1104 /* Force into colour mode */
1105 WriteMiscOut(miscOut | 0x01);
1106
1107#ifdef XORG_7X
1108 scrn = ReadSeq(pVBox, 0x01) | 0x20;
1109 SeqReset(pVBox, TRUE);
1110#else
1111 scrn = ReadSeq(0x01) | 0x20;
1112 SeqReset(TRUE);
1113#endif
1114 WriteSeq(0x01, scrn);
1115#ifdef XORG_7X
1116 SeqReset(pVBox, FALSE);
1117#else
1118 SeqReset(FALSE);
1119#endif
1120
1121#ifdef XORG_7X
1122 WriteAttr(pVBox, 0x10, 0x01); /* graphics mode */
1123#else
1124 WriteAttr(0x10, 0x01); /* graphics mode */
1125#endif
1126
1127 /*font1 */
1128 WriteSeq(0x02, 0x04); /* write to plane 2 */
1129 WriteSeq(0x04, 0x06); /* enable plane graphics */
1130 WriteGr(0x04, 0x02); /* read plane 2 */
1131 WriteGr(0x05, 0x00); /* write mode 0, read mode 0 */
1132 WriteGr(0x06, 0x05); /* set graphics */
1133 slowbcopy_frombus(pVBox->VGAbase, pVBox->fonts, 8192);
1134
1135 /* font2 */
1136 WriteSeq(0x02, 0x08); /* write to plane 3 */
1137 WriteSeq(0x04, 0x06); /* enable plane graphics */
1138 WriteGr(0x04, 0x03); /* read plane 3 */
1139 WriteGr(0x05, 0x00); /* write mode 0, read mode 0 */
1140 WriteGr(0x06, 0x05); /* set graphics */
1141 slowbcopy_frombus(pVBox->VGAbase, pVBox->fonts + 8192, 8192);
1142
1143#ifdef XORG_7X
1144 scrn = ReadSeq(pVBox, 0x01) & ~0x20;
1145 SeqReset(pVBox, TRUE);
1146#else
1147 scrn = ReadSeq(0x01) & ~0x20;
1148 SeqReset(TRUE);
1149#endif
1150 WriteSeq(0x01, scrn);
1151#ifdef XORG_7X
1152 SeqReset(pVBox, FALSE);
1153#else
1154 SeqReset(FALSE);
1155#endif
1156
1157 /* Restore clobbered registers */
1158#ifdef XORG_7X
1159 WriteAttr(pVBox, 0x10, attr10);
1160#else
1161 WriteAttr(0x10, attr10);
1162#endif
1163 WriteSeq(0x02, seq2);
1164 WriteSeq(0x04, seq4);
1165 WriteGr(0x04, gr4);
1166 WriteGr(0x05, gr5);
1167 WriteGr(0x06, gr6);
1168 WriteMiscOut(miscOut);
1169}
1170
1171static void
1172RestoreFonts(ScrnInfoPtr pScrn)
1173{
1174 VBOXPtr pVBox = VBOXGetRec(pScrn);
1175 unsigned char miscOut, attr10, gr1, gr3, gr4, gr5, gr6, gr8, seq2, seq4, scrn;
1176
1177 if (pVBox->fonts == NULL)
1178 return;
1179
1180 /* save the registers that are needed here */
1181 miscOut = ReadMiscOut();
1182#ifdef XORG_7X
1183 attr10 = ReadAttr(pVBox, 0x10);
1184 gr1 = ReadGr(pVBox, 0x01);
1185 gr3 = ReadGr(pVBox, 0x03);
1186 gr4 = ReadGr(pVBox, 0x04);
1187 gr5 = ReadGr(pVBox, 0x05);
1188 gr6 = ReadGr(pVBox, 0x06);
1189 gr8 = ReadGr(pVBox, 0x08);
1190 seq2 = ReadSeq(pVBox, 0x02);
1191 seq4 = ReadSeq(pVBox, 0x04);
1192#else
1193 attr10 = ReadAttr(0x10);
1194 gr1 = ReadGr(0x01);
1195 gr3 = ReadGr(0x03);
1196 gr4 = ReadGr(0x04);
1197 gr5 = ReadGr(0x05);
1198 gr6 = ReadGr(0x06);
1199 gr8 = ReadGr(0x08);
1200 seq2 = ReadSeq(0x02);
1201 seq4 = ReadSeq(0x04);
1202#endif
1203
1204 /* Force into colour mode */
1205 WriteMiscOut(miscOut | 0x01);
1206
1207#ifdef XORG_7X
1208 scrn = ReadSeq(pVBox, 0x01) & ~0x20;
1209 SeqReset(pVBox, TRUE);
1210#else
1211 scrn = ReadSeq(0x01) | 0x20;
1212 SeqReset(TRUE);
1213#endif
1214 WriteSeq(0x01, scrn);
1215#ifdef XORG_7X
1216 SeqReset(pVBox, FALSE);
1217#else
1218 SeqReset(FALSE);
1219#endif
1220
1221#ifdef XORG_7X
1222 WriteAttr(pVBox, 0x10, 0x01); /* graphics mode */
1223#else
1224 WriteAttr(0x10, 0x01); /* graphics mode */
1225#endif
1226 if (pScrn->depth == 4) {
1227 /* GJA */
1228 WriteGr(0x03, 0x00); /* don't rotate, write unmodified */
1229 WriteGr(0x08, 0xFF); /* write all bits in a byte */
1230 WriteGr(0x01, 0x00); /* all planes come from CPU */
1231 }
1232
1233 WriteSeq(0x02, 0x04); /* write to plane 2 */
1234 WriteSeq(0x04, 0x06); /* enable plane graphics */
1235 WriteGr(0x04, 0x02); /* read plane 2 */
1236 WriteGr(0x05, 0x00); /* write mode 0, read mode 0 */
1237 WriteGr(0x06, 0x05); /* set graphics */
1238 slowbcopy_tobus(pVBox->fonts, pVBox->VGAbase, 8192);
1239
1240 WriteSeq(0x02, 0x08); /* write to plane 3 */
1241 WriteSeq(0x04, 0x06); /* enable plane graphics */
1242 WriteGr(0x04, 0x03); /* read plane 3 */
1243 WriteGr(0x05, 0x00); /* write mode 0, read mode 0 */
1244 WriteGr(0x06, 0x05); /* set graphics */
1245 slowbcopy_tobus(pVBox->fonts + 8192, pVBox->VGAbase, 8192);
1246
1247#ifdef XORG_7X
1248 scrn = ReadSeq(pVBox, 0x01) & ~0x20;
1249 SeqReset(pVBox, TRUE);
1250#else
1251 scrn = ReadSeq(0x01) & ~0x20;
1252 SeqReset(TRUE);
1253#endif
1254 WriteSeq(0x01, scrn);
1255#ifdef XORG_7X
1256 SeqReset(pVBox, FALSE);
1257#else
1258 SeqReset(FALSE);
1259#endif
1260
1261 /* restore the registers that were changed */
1262 WriteMiscOut(miscOut);
1263#ifdef XORG_7X
1264 WriteAttr(pVBox, 0x10, attr10);
1265#else
1266 WriteAttr(0x10, attr10);
1267#endif
1268 WriteGr(0x01, gr1);
1269 WriteGr(0x03, gr3);
1270 WriteGr(0x04, gr4);
1271 WriteGr(0x05, gr5);
1272 WriteGr(0x06, gr6);
1273 WriteGr(0x08, gr8);
1274 WriteSeq(0x02, seq2);
1275 WriteSeq(0x04, seq4);
1276}
1277
1278static Bool
1279VBOXSaveScreen(ScreenPtr pScreen, int mode)
1280{
1281 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
1282#ifdef XORG_7X
1283 VBOXPtr pVBox = VBOXGetRec(pScrn);
1284#endif
1285 Bool on = xf86IsUnblank(mode);
1286
1287 if (on)
1288 SetTimeSinceLastInputEvent();
1289
1290 if (pScrn->vtSema) {
1291#ifdef XORG_7X
1292 unsigned char scrn = ReadSeq(pVBox, 0x01);
1293#else
1294 unsigned char scrn = ReadSeq(0x01);
1295#endif
1296
1297 if (on)
1298 scrn &= ~0x20;
1299 else
1300 scrn |= 0x20;
1301#ifdef XORG_7X
1302 SeqReset(pVBox, TRUE);
1303 WriteSeq(0x01, scrn);
1304 SeqReset(pVBox, FALSE);
1305#else
1306 SeqReset(TRUE);
1307 WriteSeq(0x01, scrn);
1308 SeqReset(FALSE);
1309#endif
1310 }
1311
1312 return (TRUE);
1313}
1314
1315Bool
1316VBOXSaveRestore(ScrnInfoPtr pScrn, vbeSaveRestoreFunction function)
1317{
1318 VBOXPtr pVBox;
1319
1320 if (MODE_QUERY < 0 || function > MODE_RESTORE)
1321 return (FALSE);
1322
1323 pVBox = VBOXGetRec(pScrn);
1324
1325
1326 /* Query amount of memory to save state */
1327 if (function == MODE_QUERY ||
1328 (function == MODE_SAVE && pVBox->state == NULL))
1329 {
1330
1331 /* Make sure we save at least this information in case of failure */
1332 (void)VBEGetVBEMode(pVBox->pVbe, &pVBox->stateMode);
1333 SaveFonts(pScrn);
1334
1335 if (!VBESaveRestore(pVBox->pVbe,function,(pointer)&pVBox->state,
1336 &pVBox->stateSize,&pVBox->statePage))
1337 return FALSE;
1338 }
1339
1340 /* Save/Restore Super VGA state */
1341 if (function != MODE_QUERY) {
1342 Bool retval = TRUE;
1343
1344 if (function == MODE_RESTORE)
1345 memcpy(pVBox->state, pVBox->pstate,
1346 (unsigned) pVBox->stateSize);
1347
1348 if ((retval = VBESaveRestore(pVBox->pVbe,function,
1349 (pointer)&pVBox->state,
1350 &pVBox->stateSize,&pVBox->statePage))
1351 && function == MODE_SAVE)
1352 {
1353 /* don't rely on the memory not being touched */
1354 if (pVBox->pstate == NULL)
1355 pVBox->pstate = xalloc(pVBox->stateSize);
1356 memcpy(pVBox->pstate, pVBox->state,
1357 (unsigned) pVBox->stateSize);
1358 }
1359
1360 if (function == MODE_RESTORE)
1361 {
1362 VBESetVBEMode(pVBox->pVbe, pVBox->stateMode, NULL);
1363 RestoreFonts(pScrn);
1364 }
1365
1366 if (!retval)
1367 return (FALSE);
1368
1369 }
1370
1371 return (TRUE);
1372}
1373
1374static void
1375VBOXDisplayPowerManagementSet(ScrnInfoPtr pScrn, int mode,
1376 int flags)
1377{
1378 /* VBox is always power efficient... */
1379}
1380
1381
1382
1383
1384/***********************************************************************
1385 * DGA stuff
1386 ***********************************************************************/
1387static Bool VBOXDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1388 unsigned char **ApertureBase,
1389 int *ApertureSize, int *ApertureOffset,
1390 int *flags);
1391static Bool VBOXDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode);
1392static void VBOXDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags);
1393
1394static Bool
1395VBOXDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
1396 unsigned char **ApertureBase, int *ApertureSize,
1397 int *ApertureOffset, int *flags)
1398{
1399 VBOXPtr pVBox = VBOXGetRec(pScrn);
1400
1401 *DeviceName = NULL; /* No special device */
1402 *ApertureBase = (unsigned char *)(long)(pVBox->mapPhys);
1403 *ApertureSize = pVBox->mapSize;
1404 *ApertureOffset = pVBox->mapOff;
1405 *flags = DGA_NEED_ROOT;
1406
1407 return (TRUE);
1408}
1409
1410static Bool
1411VBOXDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
1412{
1413 DisplayModePtr pMode;
1414 int scrnIdx = pScrn->pScreen->myNum;
1415 int frameX0, frameY0;
1416
1417 if (pDGAMode) {
1418 pMode = pDGAMode->mode;
1419 frameX0 = frameY0 = 0;
1420 }
1421 else {
1422 if (!(pMode = pScrn->currentMode))
1423 return (TRUE);
1424
1425 frameX0 = pScrn->frameX0;
1426 frameY0 = pScrn->frameY0;
1427 }
1428
1429 if (!(*pScrn->SwitchMode)(scrnIdx, pMode, 0))
1430 return (FALSE);
1431 (*pScrn->AdjustFrame)(scrnIdx, frameX0, frameY0, 0);
1432
1433 return (TRUE);
1434}
1435
1436static void
1437VBOXDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
1438{
1439 (*pScrn->AdjustFrame)(pScrn->pScreen->myNum, x, y, flags);
1440}
1441
1442static int
1443VBOXDGAGetViewport(ScrnInfoPtr pScrn)
1444{
1445 return (0);
1446}
1447
1448static DGAFunctionRec VBOXDGAFunctions =
1449{
1450 VBOXDGAOpenFramebuffer,
1451 NULL, /* CloseFramebuffer */
1452 VBOXDGASetMode,
1453 VBOXDGASetViewport,
1454 VBOXDGAGetViewport,
1455 NULL, /* Sync */
1456 NULL, /* FillRect */
1457 NULL, /* BlitRect */
1458 NULL, /* BlitTransRect */
1459};
1460
1461static void
1462VBOXDGAAddModes(ScrnInfoPtr pScrn)
1463{
1464 VBOXPtr pVBox = VBOXGetRec(pScrn);
1465 DisplayModePtr pMode = pScrn->modes;
1466 DGAModePtr pDGAMode;
1467
1468 do {
1469 pDGAMode = xrealloc(pVBox->pDGAMode,
1470 (pVBox->nDGAMode + 1) * sizeof(DGAModeRec));
1471 if (!pDGAMode)
1472 break;
1473
1474 pVBox->pDGAMode = pDGAMode;
1475 pDGAMode += pVBox->nDGAMode;
1476 (void)memset(pDGAMode, 0, sizeof(DGAModeRec));
1477
1478 ++pVBox->nDGAMode;
1479 pDGAMode->mode = pMode;
1480 pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
1481 pDGAMode->byteOrder = pScrn->imageByteOrder;
1482 pDGAMode->depth = pScrn->depth;
1483 pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
1484 pDGAMode->red_mask = pScrn->mask.red;
1485 pDGAMode->green_mask = pScrn->mask.green;
1486 pDGAMode->blue_mask = pScrn->mask.blue;
1487 pDGAMode->visualClass = TrueColor;
1488 pDGAMode->xViewportStep = 1;
1489 pDGAMode->yViewportStep = 1;
1490 pDGAMode->viewportWidth = pMode->HDisplay;
1491 pDGAMode->viewportHeight = pMode->VDisplay;
1492
1493 pDGAMode->bytesPerScanline = pVBox->maxBytesPerScanline;
1494 pDGAMode->imageWidth = pMode->HDisplay;
1495 pDGAMode->imageHeight = pMode->VDisplay;
1496 pDGAMode->pixmapWidth = pDGAMode->imageWidth;
1497 pDGAMode->pixmapHeight = pDGAMode->imageHeight;
1498 pDGAMode->maxViewportX = pScrn->virtualX -
1499 pDGAMode->viewportWidth;
1500 pDGAMode->maxViewportY = pScrn->virtualY -
1501 pDGAMode->viewportHeight;
1502
1503 pDGAMode->address = pVBox->base;
1504
1505 pMode = pMode->next;
1506 } while (pMode != pScrn->modes);
1507}
1508
1509static Bool
1510VBOXDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
1511{
1512 VBOXPtr pVBox = VBOXGetRec(pScrn);
1513
1514 if (!pVBox->nDGAMode)
1515 VBOXDGAAddModes(pScrn);
1516
1517 return (DGAInit(pScreen, &VBOXDGAFunctions,
1518 pVBox->pDGAMode, pVBox->nDGAMode));
1519}
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