1 /* $XdotOrg: driver/xf86-video-nv/src/nv_driver.c,v 1.21 2006/01/24 16:45:29 aplattner Exp $ */
2 /* $XConsortium: nv_driver.c /main/3 1996/10/28 05:13:37 kaleb $ */
4 * Copyright 1996-1997 David J. McKay
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * DAVID J. MCKAY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 /* Hacked together from mga driver and 3.3.4 NVIDIA driver by Jarno Paananen
28 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/nv/nv_driver.c,v 1.144 2006/06/16 00:19:32 mvojkovi Exp $ */
32 #include "nv_include.h"
34 #include "xf86int10.h"
38 extern DisplayModePtr xf86ModesAdd(DisplayModePtr Modes, DisplayModePtr Additions);
41 * Forward definitions for the functions that make up the driver.
43 /* Mandatory functions */
44 static const OptionInfoRec * NVAvailableOptions(int chipid, int busid);
45 static void NVIdentify(int flags);
46 #ifndef XSERVER_LIBPCIACCESS
47 static Bool NVProbe(DriverPtr drv, int flags);
48 #endif /* XSERVER_LIBPCIACCESS */
49 static Bool NVPreInit(ScrnInfoPtr pScrn, int flags);
50 static Bool NVScreenInit(int Index, ScreenPtr pScreen, int argc,
52 static Bool NVEnterVT(int scrnIndex, int flags);
53 static void NVLeaveVT(int scrnIndex, int flags);
54 static Bool NVCloseScreen(int scrnIndex, ScreenPtr pScreen);
55 static Bool NVSaveScreen(ScreenPtr pScreen, int mode);
57 /* Optional functions */
58 static void NVFreeScreen(int scrnIndex, int flags);
59 static ModeStatus NVValidMode(int scrnIndex, DisplayModePtr mode,
60 Bool verbose, int flags);
61 static Bool NVDriverFunc(ScrnInfoPtr pScrnInfo, xorgDriverFuncOp op,
64 /* Internally used functions */
66 static Bool NVMapMem(ScrnInfoPtr pScrn);
67 static Bool NVUnmapMem(ScrnInfoPtr pScrn);
68 static void NVSave(ScrnInfoPtr pScrn);
69 static void NVRestore(ScrnInfoPtr pScrn);
70 static Bool NVModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode);
72 #ifdef XSERVER_LIBPCIACCESS
74 #define NOUVEAU_PCI_DEVICE(_vendor_id, _device_id) \
75 { (_vendor_id), (_device_id), PCI_MATCH_ANY, PCI_MATCH_ANY, 0x00030000, 0x00ffffff, 0 }
77 static const struct pci_id_match nouveau_device_match[] = {
78 NOUVEAU_PCI_DEVICE(PCI_VENDOR_NVIDIA, PCI_MATCH_ANY),
79 NOUVEAU_PCI_DEVICE(PCI_VENDOR_NVIDIA_SGS, PCI_MATCH_ANY),
83 static Bool NVPciProbe ( DriverPtr drv,
85 struct pci_device *dev,
86 intptr_t match_data );
88 #endif /* XSERVER_LIBPCIACCESS */
91 * This contains the functions needed by the server after loading the
92 * driver module. It must be supplied, and gets added the driver list by
93 * the Module Setup funtion in the dynamic case. In the static case a
94 * reference to this is compiled in, and this requires that the name of
95 * this DriverRec be an upper-case version of the driver name.
98 _X_EXPORT DriverRec NV = {
102 #ifdef XSERVER_LIBPCIACCESS
106 #endif /* XSERVER_LIBPCIACCESS */
111 #ifdef XSERVER_LIBPCIACCESS
112 nouveau_device_match,
114 #endif /* XSERVER_LIBPCIACCESS */
123 static struct NvFamily NVKnownFamilies[] =
125 { "RIVA TNT", "NV04" },
126 { "RIVA TNT2", "NV05" },
127 { "GeForce 256", "NV10" },
128 { "GeForce 2", "NV11, NV15" },
129 { "GeForce 4MX", "NV17, NV18" },
130 { "GeForce 3", "NV20" },
131 { "GeForce 4Ti", "NV25, NV28" },
132 { "GeForce FX", "NV3x" },
133 { "GeForce 6", "NV4x" },
134 { "GeForce 7", "G7x" },
135 { "GeForce 8", "G8x" },
140 * List of symbols from other modules that this module references. This
141 * list is used to tell the loader that it is OK for symbols here to be
142 * unresolved providing that it hasn't been told that they haven't been
143 * told that they are essential via a call to xf86LoaderReqSymbols() or
144 * xf86LoaderReqSymLists(). The purpose is this is to avoid warnings about
145 * unresolved symbols that are not required.
148 static const char *vgahwSymbols[] = {
163 static const char *fbSymbols[] = {
169 static const char *exaSymbols[] = {
175 static const char *ramdacSymbols[] = {
176 "xf86CreateCursorInfoRec",
177 "xf86DestroyCursorInfoRec",
182 static const char *ddcSymbols[] = {
185 "xf86SetDDCproperties",
189 static const char *vbeSymbols[] = {
196 static const char *i2cSymbols[] = {
197 "xf86CreateI2CBusRec",
202 static const char *shadowSymbols[] = {
207 static const char *int10Symbols[] = {
213 const char *drmSymbols[] = {
218 "drmAgpVersionMajor",
219 "drmAgpVersionMinor",
229 "drmCommandWriteRead",
232 "drmCtlUninstHandler",
235 "drmGetInterruptFromBusID",
241 const char *driSymbols[] = {
245 "DRIFinishScreenInit",
246 "DRIGetSAREAPrivate",
251 "GlxSetVisualConfigs",
257 static MODULESETUPPROTO(nouveauSetup);
259 static XF86ModuleVersionInfo nouveauVersRec =
265 XORG_VERSION_CURRENT,
266 NV_MAJOR_VERSION, NV_MINOR_VERSION, NV_PATCHLEVEL,
267 ABI_CLASS_VIDEODRV, /* This is a video driver */
268 ABI_VIDEODRV_VERSION,
273 _X_EXPORT XF86ModuleData nouveauModuleData = { &nouveauVersRec, nouveauSetup, NULL };
276 NVGetRec(ScrnInfoPtr pScrn)
279 * Allocate an NVRec, and hook it into pScrn->driverPrivate.
280 * pScrn->driverPrivate is initialised to NULL, so we can check if
281 * the allocation has already been done.
283 if (pScrn->driverPrivate != NULL)
286 pScrn->driverPrivate = xnfcalloc(sizeof(NVRec), 1);
293 NVFreeRec(ScrnInfoPtr pScrn)
295 if (pScrn->driverPrivate == NULL)
297 xfree(pScrn->driverPrivate);
298 pScrn->driverPrivate = NULL;
303 nouveauSetup(pointer module, pointer opts, int *errmaj, int *errmin)
305 static Bool setupDone = FALSE;
307 /* This module should be loaded only once, but check to be sure. */
311 /* The 1 here is needed to turn off a backwards compatibility mode */
312 /* Otherwise NVPciProbe() is not called */
313 xf86AddDriver(&NV, module, 1);
316 * Modules that this driver always requires may be loaded here
317 * by calling LoadSubModule().
320 * Tell the loader about symbols from other modules that this module
323 LoaderRefSymLists(vgahwSymbols, exaSymbols, fbSymbols,
327 ramdacSymbols, shadowSymbols,
328 i2cSymbols, ddcSymbols, vbeSymbols,
332 * The return value must be non-NULL on success even though there
333 * is no TearDownProc.
337 if (errmaj) *errmaj = LDR_ONCEONLY;
342 static const OptionInfoRec *
343 NVAvailableOptions(int chipid, int busid)
350 NVIdentify(int flags)
352 struct NvFamily *family;
355 xf86DrvMsg(0, X_INFO, NV_NAME " driver " NV_DRIVER_DATE "\n");
356 xf86DrvMsg(0, X_INFO, NV_NAME " driver for NVIDIA chipset families :\n");
358 /* maximum length for alignment */
359 family = NVKnownFamilies;
360 while(family->name && family->chipset)
362 maxLen = max(maxLen, strlen(family->name));
367 family = NVKnownFamilies;
368 while(family->name && family->chipset)
370 size_t len = strlen(family->name);
371 xf86ErrorF("\t%s", family->name);
377 xf86ErrorF("(%s)\n", family->chipset);
383 #ifndef XSERVER_LIBPCIACCESS
385 NVGetScrnInfoRec(PciChipsets *chips, int chip)
389 pScrn = xf86ConfigPciEntity(NULL, 0, chip,
390 chips, NULL, NULL, NULL,
393 if(!pScrn) return FALSE;
395 pScrn->driverVersion = NV_VERSION;
396 pScrn->driverName = NV_DRIVER_NAME;
397 pScrn->name = NV_NAME;
399 pScrn->Probe = NVProbe;
400 pScrn->PreInit = NVPreInit;
401 pScrn->ScreenInit = NVScreenInit;
402 pScrn->SwitchMode = NVSwitchMode;
403 pScrn->AdjustFrame = NVAdjustFrame;
404 pScrn->EnterVT = NVEnterVT;
405 pScrn->LeaveVT = NVLeaveVT;
406 pScrn->FreeScreen = NVFreeScreen;
407 pScrn->ValidMode = NVValidMode;
413 /* This returns architecture in hexdecimal, so NV40 is 0x40 */
414 static int NVGetArchitecture (volatile CARD32 *regs)
416 int architecture = 0;
418 /* We're dealing with >=NV10 */
419 if ((regs[0] & 0x0f000000) > 0 ) {
420 /* Bit 27-20 contain the architecture in hex */
421 architecture = (regs[0] & 0xff00000) >> 20;
423 } else if ((regs[0] & 0xff00fff0) == 0x20004000) {
430 /* Reading the pci_id from the card registers is the most reliable way */
431 static CARD32 NVGetPCIID (volatile CARD32 *regs)
435 int architecture = NVGetArchitecture(regs);
437 /* Dealing with an unknown or unsupported card */
438 if (architecture == 0) {
442 if (architecture >= 0x40)
443 pci_id = regs[0x88000/4];
445 pci_id = regs[0x1800/4];
447 /* A pci-id can be inverted, we must correct this */
448 if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA) {
449 pci_id = (PCI_VENDOR_NVIDIA << 16) | (pci_id >> 16);
450 } else if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA_SGS) {
451 pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | (pci_id >> 16);
452 /* Checking endian issues */
454 /* PCI_VENDOR_NVIDIA = 0x10DE */
455 if ((pci_id & (0xffff << 16)) == (0xDE10 << 16)) { /* wrong endian */
456 pci_id = (PCI_VENDOR_NVIDIA << 16) | ((pci_id << 8) & 0x0000ff00) |
457 ((pci_id >> 8) & 0x000000ff);
458 /* PCI_VENDOR_NVIDIA_SGS = 0x12D2 */
459 } else if ((pci_id & (0xffff << 16)) == (0xD212 << 16)) { /* wrong endian */
460 pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | ((pci_id << 8) & 0x0000ff00) |
461 ((pci_id >> 8) & 0x000000ff);
468 #ifdef XSERVER_LIBPCIACCESS
470 static Bool NVPciProbe ( DriverPtr drv,
472 struct pci_device *dev,
473 intptr_t match_data )
475 ScrnInfoPtr pScrn = NULL;
477 volatile uint32_t *regs = NULL;
479 /* Temporary mapping to discover the architecture */
480 pci_device_map_range(dev, PCI_DEV_MEM_BASE(dev, 0), 0x90000, 0,
483 uint8_t architecture = NVGetArchitecture(regs);
485 CARD32 pci_id = NVGetPCIID(regs);
487 pci_device_unmap_range(dev, (void *) regs, 0x90000);
489 /* Currently NV04 up to NV83 is supported */
490 /* For safety the fictional NV8F is used */
491 if (architecture >= 0x04 && architecture <= 0x8F) {
493 /* At this stage the pci_id should be ok, so we generate this
494 * to avoid list duplication */
495 /* AGP bridge chips need their bridge chip id to be detected */
496 PciChipsets NVChipsets[] = {
497 { pci_id, PCI_DEV_PCI_ID(dev), RES_SHARED_VGA },
498 { -1, -1, RES_UNDEFINED }
501 pScrn = xf86ConfigPciEntity(pScrn, 0, entity_num, NVChipsets,
502 NULL, NULL, NULL, NULL, NULL);
505 pScrn->driverVersion = NV_VERSION;
506 pScrn->driverName = NV_DRIVER_NAME;
507 pScrn->name = NV_NAME;
510 pScrn->PreInit = NVPreInit;
511 pScrn->ScreenInit = NVScreenInit;
512 pScrn->SwitchMode = NVSwitchMode;
513 pScrn->AdjustFrame = NVAdjustFrame;
514 pScrn->EnterVT = NVEnterVT;
515 pScrn->LeaveVT = NVLeaveVT;
516 pScrn->FreeScreen = NVFreeScreen;
517 pScrn->ValidMode = NVValidMode;
526 #endif /* XSERVER_LIBPCIACCESS */
528 #define MAX_CHIPS MAXSCREENS
530 #ifndef XSERVER_LIBPCIACCESS
533 NVProbe(DriverPtr drv, int flags)
536 GDevPtr *devSections;
538 SymTabRec NVChipsets[MAX_CHIPS + 1];
539 PciChipsets NVPciChipsets[MAX_CHIPS + 1];
543 Bool foundScreen = FALSE;
545 if ((numDevSections = xf86MatchDevice(NV_DRIVER_NAME, &devSections)) <= 0)
546 return FALSE; /* no matching device section */
548 if (!(ppPci = xf86GetPciVideoInfo()))
549 return FALSE; /* no PCI cards found */
553 /* Create the NVChipsets and NVPciChipsets from found devices */
554 while (*ppPci && (numUsed < MAX_CHIPS)) {
555 if (((*ppPci)->vendor == PCI_VENDOR_NVIDIA_SGS) ||
556 ((*ppPci)->vendor == PCI_VENDOR_NVIDIA))
558 volatile CARD32 *regs;
561 PCI_DEV_READ_LONG(*ppPci, PCI_CMD_STAT_REG, &pcicmd);
562 /* Enable reading memory? */
563 PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
565 regs = xf86MapPciMem(-1, VIDMEM_MMIO, PCI_DEV_TAG(*ppPci), PCI_DEV_MEM_BASE(*ppPci, 0), 0x90000);
566 int pciid = NVGetPCIID(regs);
568 int architecture = NVGetArchitecture(regs);
570 sprintf(name, "NVIDIA NV%02X", architecture);
571 /* NV04 upto NV83 is supported, NV8F is fictive limit */
572 if (architecture >= 0x04 && architecture <= 0x8F) {
573 NVChipsets[numUsed].token = pciid;
574 NVChipsets[numUsed].name = name;
575 NVPciChipsets[numUsed].numChipset = pciid;
576 /* AGP bridge chips need their bridge chip id to be detected */
577 NVPciChipsets[numUsed].PCIid = PCI_DEV_PCI_ID(*ppPci);
578 NVPciChipsets[numUsed].resList = RES_SHARED_VGA;
581 xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
583 /* Reset previous state */
584 PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd);
589 /* terminate the list */
590 NVChipsets[numUsed].token = -1;
591 NVChipsets[numUsed].name = NULL;
592 NVPciChipsets[numUsed].numChipset = -1;
593 NVPciChipsets[numUsed].PCIid = -1;
594 NVPciChipsets[numUsed].resList = RES_UNDEFINED;
596 numUsed = xf86MatchPciInstances(NV_NAME, 0, NVChipsets, NVPciChipsets,
597 devSections, numDevSections, drv,
604 if (flags & PROBE_DETECT) {
607 for (i = 0; i < numUsed; i++) {
610 pPci = xf86GetPciInfoForEntity(usedChips[i]);
611 if (NVGetScrnInfoRec(NVPciChipsets, usedChips[i])) {
622 #endif /* XSERVER_LIBPCIACCESS */
625 NVSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
627 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
628 NVPtr pNv = NVPTR(pScrn);
630 if (pNv->randr12_enable) {
631 /* No rotation support for the moment */
632 return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
635 return NVModeInit(xf86Screens[scrnIndex], mode);
639 * This function is used to initialize the Start Address - the first
640 * displayed location in the video memory.
642 /* Usually mandatory */
644 NVAdjustFrame(int scrnIndex, int x, int y, int flags)
646 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
647 NVPtr pNv = NVPTR(pScrn);
649 if (pNv->randr12_enable) {
650 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
651 xf86CrtcPtr crtc = config->output[config->compat_output]->crtc;
653 if (crtc && crtc->enabled) {
654 NVCrtcSetBase(crtc, x, y, FALSE);
658 NVFBLayout *pLayout = &pNv->CurrentLayout;
659 startAddr = (((y*pLayout->displayWidth)+x)*(pLayout->bitsPerPixel/8));
660 startAddr += pNv->FB->offset;
661 NVSetStartAddress(pNv, startAddr);
666 NV50AcquireDisplay(ScrnInfoPtr pScrn)
668 if (!NV50DispInit(pScrn))
670 if (!NV50CursorAcquire(pScrn))
672 xf86SetDesiredModes(pScrn);
678 NV50ReleaseDisplay(ScrnInfoPtr pScrn)
680 NV50CursorRelease(pScrn);
681 NV50DispShutdown(pScrn);
686 * This is called when VT switching back to the X server. Its job is
687 * to reinitialise the video mode.
689 * We may wish to unmap video/MMIO memory too.
694 NVEnterVT(int scrnIndex, int flags)
696 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
697 NVPtr pNv = NVPTR(pScrn);
699 if (pNv->randr12_enable) {
700 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NVEnterVT is called.\n");
701 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
703 pScrn->vtSema = TRUE;
705 if (pNv->Architecture == NV_ARCH_50) {
706 if (!NV50AcquireDisplay(pScrn))
711 /* Save the current state */
712 if (pNv->SaveGeneration != serverGeneration) {
713 pNv->SaveGeneration = serverGeneration;
717 for (i = 0; i < xf86_config->num_crtc; i++) {
718 NVCrtcLockUnlock(xf86_config->crtc[i], 0);
721 /* Reassign outputs so disabled outputs don't get stuck on the wrong crtc */
722 for (i = 0; i < xf86_config->num_output; i++) {
723 xf86OutputPtr output = xf86_config->output[i];
724 NVOutputPrivatePtr nv_output = output->driver_private;
725 if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
728 /* Disable any crosswired tmds, to avoid picking up a signal on a disabled output */
729 /* Example: TMDS1 crosswired to CRTC0 (by bios) reassigned to CRTC1 in xorg, disabled. */
730 /* But the bios reinits it to CRTC0 when going back to VT. */
731 /* Because it's disabled, it doesn't get a mode set, still it picks up the signal from CRTC0 (which is another output) */
732 /* A legitimately crosswired output will get set properly during mode set */
733 if ((tmds_reg4 = NVReadTMDS(pNv, nv_output->preferred_output, 0x4)) & (1 << 3)) {
734 NVWriteTMDS(pNv, nv_output->preferred_output, 0x4, tmds_reg4 & ~(1 << 3));
739 if (!xf86SetDesiredModes(pScrn))
742 if (!NVModeInit(pScrn, pScrn->currentMode))
745 NVAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
748 if (pNv->overlayAdaptor && pNv->Architecture != NV_ARCH_04)
749 NV10WriteOverlayParameters(pScrn);
754 * This is called when VT switching away from the X server. Its job is
755 * to restore the previous (text) mode.
757 * We may wish to remap video/MMIO memory too.
762 NVLeaveVT(int scrnIndex, int flags)
764 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
765 NVPtr pNv = NVPTR(pScrn);
766 if (pNv->randr12_enable)
767 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NVLeaveVT is called.\n");
769 if (pNv->Architecture == NV_ARCH_50) {
770 NV50ReleaseDisplay(pScrn);
775 if (!pNv->randr12_enable)
776 NVLockUnlock(pScrn, 1);
789 ScreenPtr pScreen = screenInfo.screens[i];
790 ScrnInfoPtr pScrnInfo = xf86Screens[i];
791 NVPtr pNv = NVPTR(pScrnInfo);
796 pScreen->BlockHandler = pNv->BlockHandler;
797 (*pScreen->BlockHandler) (i, blockData, pTimeout, pReadmask);
798 pScreen->BlockHandler = NVBlockHandler;
800 if (pNv->VideoTimerCallback)
801 (*pNv->VideoTimerCallback)(pScrnInfo, currentTime.milliseconds);
807 * This is called at the end of each server generation. It restores the
808 * original (text) mode. It should also unmap the video memory, and free
809 * any per-generation data allocated by the driver. It should finish
810 * by unwrapping and calling the saved CloseScreen function.
815 NVCloseScreen(int scrnIndex, ScreenPtr pScreen)
817 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
818 NVPtr pNv = NVPTR(pScrn);
821 pScrn->vtSema = FALSE;
822 if (pNv->Architecture == NV_ARCH_50) {
823 NV50ReleaseDisplay(pScrn);
825 if (pNv->randr12_enable)
826 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NVCloseScreen is called.\n");
829 if (!pNv->randr12_enable)
830 NVLockUnlock(pScrn, 1);
835 vgaHWUnmapMem(pScrn);
836 if (pNv->CursorInfoRec)
837 xf86DestroyCursorInfoRec(pNv->CursorInfoRec);
839 xfree(pNv->ShadowPtr);
840 if (pNv->overlayAdaptor)
841 xfree(pNv->overlayAdaptor);
842 if (pNv->blitAdaptor)
843 xfree(pNv->blitAdaptor);
845 pScrn->vtSema = FALSE;
846 pScreen->CloseScreen = pNv->CloseScreen;
847 pScreen->BlockHandler = pNv->BlockHandler;
848 return (*pScreen->CloseScreen)(scrnIndex, pScreen);
851 /* Free up any persistent data structures */
855 NVFreeScreen(int scrnIndex, int flags)
858 * This only gets called when a screen is being deleted. It does not
859 * get called routinely at the end of a server generation.
861 if (xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
862 vgaHWFreeHWRec(xf86Screens[scrnIndex]);
863 NVFreeRec(xf86Screens[scrnIndex]);
867 /* Checks if a mode is suitable for the selected chipset. */
871 NVValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
873 NVPtr pNv = NVPTR(xf86Screens[scrnIndex]);
875 if(pNv->fpWidth && pNv->fpHeight)
876 if((pNv->fpWidth < mode->HDisplay) || (pNv->fpHeight < mode->VDisplay))
882 Bool NVI2CInit(ScrnInfoPtr pScrn)
884 NVPtr pNv = NVPTR(pScrn);
886 if (xf86LoadSubModule(pScrn, "i2c") && xf86LoadSubModule(pScrn, "ddc")) {
887 xf86LoaderReqSymLists(i2cSymbols,NULL);
888 xf86LoaderReqSymLists(ddcSymbols, NULL);
890 /* randr-1.2 clients have their DDCs initialized elsewhere */
891 if (!pNv->randr12_enable)
892 return NVDACi2cInit(pScrn);
895 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
896 "Couldn't load i2c and ddc modules. DDC probing can't be done\n");
900 static Bool NVPreInitDRI(ScrnInfoPtr pScrn)
902 NVPtr pNv = NVPTR(pScrn);
904 if (!NVDRIGetVersion(pScrn))
907 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
908 "[dri] Found DRI library version %d.%d.%d and kernel"
909 " module version %d.%d.%d\n",
910 pNv->pLibDRMVersion->version_major,
911 pNv->pLibDRMVersion->version_minor,
912 pNv->pLibDRMVersion->version_patchlevel,
913 pNv->pKernelDRMVersion->version_major,
914 pNv->pKernelDRMVersion->version_minor,
915 pNv->pKernelDRMVersion->version_patchlevel);
921 nv_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
923 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "nv_xf86crtc_resize is called with %dx%d resolution.\n", width, height);
924 pScrn->virtualX = width;
925 pScrn->virtualY = height;
929 static const xf86CrtcConfigFuncsRec nv_xf86crtc_config_funcs = {
933 /* This is taken from the haiku driver */
934 /* We must accept crtc pitch constrains */
935 /* A hardware bug on some hardware requires twice the pitch */
936 static CARD8 NVGetCRTCMask(ScrnInfoPtr pScrn, CARD8 bpp)
941 mask = 0xf; /* 0x7 */
944 mask = 0x7; /* 0x3 */
947 mask = 0x7; /* 0x3 */
950 mask = 0xf; /* 0x7 */
953 mask = 0x3; /* 0x1 */
956 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unkown color format.\n");
963 /* This is taken from the haiku driver */
964 static CARD8 NVGetAccelerationMask(ScrnInfoPtr pScrn, CARD8 bpp)
966 NVPtr pNv = NVPTR(pScrn);
968 /* Identical for NV04 */
969 if (pNv->Architecture == NV_ARCH_04) {
970 return NVGetCRTCMask(pScrn, bpp);
989 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unkown color format.\n");
997 static CARD32 NVGetVideoPitch(ScrnInfoPtr pScrn, CARD8 bpp)
999 NVPtr pNv = NVPTR(pScrn);
1000 CARD8 crtc_mask, accel_mask = 0;
1001 crtc_mask = NVGetCRTCMask(pScrn, bpp);
1002 if (!pNv->NoAccel) {
1003 accel_mask = NVGetAccelerationMask(pScrn, bpp);
1006 /* adhere to the largest granularity imposed */
1007 if (accel_mask > crtc_mask) {
1008 return (pScrn->virtualX + accel_mask) & ~accel_mask;
1010 return (pScrn->virtualX + crtc_mask) & ~crtc_mask;
1014 #define NVPreInitFail(fmt, args...) do { \
1015 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%d: "fmt, __LINE__, ##args); \
1017 xf86FreeInt10(pNv->pInt10); \
1024 NVPreInit(ScrnInfoPtr pScrn, int flags)
1026 xf86CrtcConfigPtr xf86_config;
1029 int i, max_width, max_height;
1030 ClockRangePtr clockRanges;
1032 int config_mon_rates = FALSE;
1035 if (flags & PROBE_DETECT) {
1036 EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1044 if (xf86LoadSubModule(pScrn, "vbe")) {
1045 vbeInfoPtr pVbe = VBEInit(NULL, i);
1046 ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
1054 * Note: This function is only called once at server startup, and
1055 * not at the start of each server generation. This means that
1056 * only things that are persistent across server generations can
1057 * be initialised here. xf86Screens[] is (pScrn is a pointer to one
1058 * of these). Privates allocated using xf86AllocateScrnInfoPrivateIndex()
1059 * are too, and should be used for data that must persist across
1060 * server generations.
1062 * Per-generation data should be allocated with
1063 * AllocateScreenPrivateIndex() from the ScreenInit() function.
1066 /* Check the number of entities, and fail if it isn't one. */
1067 if (pScrn->numEntities != 1)
1070 /* Allocate the NVRec driverPrivate */
1071 if (!NVGetRec(pScrn)) {
1076 /* Get the entity, and make sure it is PCI. */
1077 pNv->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1078 if (pNv->pEnt->location.type != BUS_PCI)
1081 /* Find the PCI info for this screen */
1082 pNv->PciInfo = xf86GetPciInfoForEntity(pNv->pEnt->index);
1083 #ifndef XSERVER_LIBPCIACCESS
1084 pNv->PciTag = pciTag(pNv->PciInfo->bus, pNv->PciInfo->device,
1085 pNv->PciInfo->func);
1086 #endif /* XSERVER_LIBPCIACCESS */
1088 pNv->Primary = xf86IsPrimaryPci(pNv->PciInfo);
1090 /* Initialize the card through int10 interface if needed */
1091 if (xf86LoadSubModule(pScrn, "int10")) {
1092 xf86LoaderReqSymLists(int10Symbols, NULL);
1093 #if !defined(__alpha__) && !defined(__powerpc__)
1094 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initializing int10\n");
1095 pNv->pInt10 = xf86InitInt10(pNv->pEnt->index);
1099 xf86SetOperatingState(resVgaIo, pNv->pEnt->index, ResUnusedOpr);
1100 xf86SetOperatingState(resVgaMem, pNv->pEnt->index, ResDisableOpr);
1102 /* Set pScrn->monitor */
1103 pScrn->monitor = pScrn->confScreen->monitor;
1105 volatile uint32_t *regs = NULL;
1106 #ifdef XSERVER_LIBPCIACCESS
1107 pci_device_map_range(pNv->PciInfo, PCI_DEV_MEM_BASE(pNv->PciInfo, 0),
1108 0x90000, 0, (void *)®s);
1109 pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1110 pNv->NVArch = NVGetArchitecture(regs);
1111 pci_device_unmap_range(pNv->PciInfo, (void *) regs, 0x90000);
1114 PCI_DEV_READ_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, &pcicmd);
1115 /* Enable reading memory? */
1116 PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
1117 regs = xf86MapPciMem(-1, VIDMEM_MMIO, pNv->PciTag, PCI_DEV_MEM_BASE(pNv->PciInfo, 0), 0x90000);
1118 pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1119 pNv->NVArch = NVGetArchitecture(regs);
1120 xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
1121 /* Reset previous state */
1122 PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd);
1123 #endif /* XSERVER_LIBPCIACCESS */
1125 pScrn->chipset = malloc(sizeof(char) * 25);
1126 sprintf(pScrn->chipset, "NVIDIA NV%02X", pNv->NVArch);
1128 if(!pScrn->chipset) {
1129 pScrn->chipset = "Unknown NVIDIA";
1133 * This shouldn't happen because such problems should be caught in
1134 * NVProbe(), but check it just in case.
1136 if (pScrn->chipset == NULL)
1137 NVPreInitFail("ChipID 0x%04X is not recognised\n", pNv->Chipset);
1139 if (pNv->NVArch < 0x04)
1140 NVPreInitFail("Chipset \"%s\" is not recognised\n", pScrn->chipset);
1142 xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Chipset: \"%s\"\n", pScrn->chipset);
1144 /* The highest architecture currently supported is NV5x */
1145 if (pNv->NVArch >= 0x80) {
1146 pNv->Architecture = NV_ARCH_50;
1147 } else if (pNv->NVArch >= 0x60) {
1148 pNv->Architecture = NV_ARCH_40;
1149 } else if (pNv->NVArch >= 0x50) {
1150 pNv->Architecture = NV_ARCH_50;
1151 } else if (pNv->NVArch >= 0x40) {
1152 pNv->Architecture = NV_ARCH_40;
1153 } else if (pNv->NVArch >= 0x30) {
1154 pNv->Architecture = NV_ARCH_30;
1155 } else if (pNv->NVArch >= 0x20) {
1156 pNv->Architecture = NV_ARCH_20;
1157 } else if (pNv->NVArch >= 0x10) {
1158 pNv->Architecture = NV_ARCH_10;
1159 } else if (pNv->NVArch >= 0x04) {
1160 pNv->Architecture = NV_ARCH_04;
1161 /* The lowest architecture currently supported is NV04 */
1167 * The first thing we should figure out is the depth, bpp, etc.
1170 if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support32bppFb)) {
1171 NVPreInitFail("\n");
1173 /* Check that the returned depth is one we support */
1174 switch (pScrn->depth) {
1182 NVPreInitFail("Given depth (%d) is not supported by this driver\n",
1186 xf86PrintDepthBpp(pScrn);
1189 * This must happen after pScrn->display has been set because
1190 * xf86SetWeight references it.
1192 if (pScrn->depth > 8) {
1193 /* The defaults are OK for us */
1194 rgb zeros = {0, 0, 0};
1196 if (!xf86SetWeight(pScrn, zeros, zeros)) {
1197 NVPreInitFail("\n");
1201 if (!xf86SetDefaultVisual(pScrn, -1)) {
1202 NVPreInitFail("\n");
1204 /* We don't currently support DirectColor at > 8bpp */
1205 if (pScrn->depth > 8 && (pScrn->defaultVisual != TrueColor)) {
1206 NVPreInitFail("Given default visual"
1207 " (%s) is not supported at depth %d\n",
1208 xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
1212 /* The vgahw module should be loaded here when needed */
1213 if (!xf86LoadSubModule(pScrn, "vgahw")) {
1214 NVPreInitFail("\n");
1217 xf86LoaderReqSymLists(vgahwSymbols, NULL);
1220 * Allocate a vgaHWRec
1222 if (!vgaHWGetHWRec(pScrn)) {
1223 NVPreInitFail("\n");
1226 /* We use a programmable clock */
1227 pScrn->progClock = TRUE;
1229 /* Collect all of the relevant option flags (fill in pScrn->options) */
1230 xf86CollectOptions(pScrn, NULL);
1232 /* Process the options */
1233 if (!(pNv->Options = xalloc(sizeof(NVOptions))))
1235 memcpy(pNv->Options, NVOptions, sizeof(NVOptions));
1236 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pNv->Options);
1238 /* Set the bits per RGB for 8bpp mode */
1239 if (pScrn->depth == 8)
1244 if (pNv->Architecture == NV_ARCH_50) {
1245 pNv->randr12_enable = TRUE;
1247 pNv->randr12_enable = FALSE;
1248 if (xf86ReturnOptValBool(pNv->Options, OPTION_RANDR12, FALSE)) {
1249 pNv->randr12_enable = TRUE;
1252 xf86DrvMsg(pScrn->scrnIndex, from, "Randr1.2 support %sabled\n", pNv->randr12_enable ? "en" : "dis");
1254 pNv->new_restore = FALSE;
1256 if (pNv->randr12_enable) {
1257 if (xf86ReturnOptValBool(pNv->Options, OPTION_NEW_RESTORE, FALSE)) {
1258 pNv->new_restore = TRUE;
1260 xf86DrvMsg(pScrn->scrnIndex, from, "New (experimental) restore support %sabled\n", pNv->new_restore ? "en" : "dis");
1263 pNv->HWCursor = TRUE;
1265 * The preferred method is to use the "hw cursor" option as a tri-state
1266 * option, with the default set above.
1268 if (xf86GetOptValBool(pNv->Options, OPTION_HW_CURSOR, &pNv->HWCursor)) {
1271 /* For compatibility, accept this too (as an override) */
1272 if (xf86ReturnOptValBool(pNv->Options, OPTION_SW_CURSOR, FALSE)) {
1274 pNv->HWCursor = FALSE;
1276 xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
1277 pNv->HWCursor ? "HW" : "SW");
1279 pNv->FpScale = TRUE;
1281 if (xf86GetOptValBool(pNv->Options, OPTION_FP_SCALE, &pNv->FpScale)) {
1282 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Flat panel scaling %s\n",
1283 pNv->FpScale ? "on" : "off");
1285 if (xf86ReturnOptValBool(pNv->Options, OPTION_NOACCEL, FALSE)) {
1286 pNv->NoAccel = TRUE;
1287 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
1289 if (xf86ReturnOptValBool(pNv->Options, OPTION_SHADOW_FB, FALSE)) {
1290 pNv->ShadowFB = TRUE;
1291 pNv->NoAccel = TRUE;
1292 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1293 "Using \"Shadow Framebuffer\" - acceleration disabled\n");
1297 pNv->RandRRotation = FALSE;
1299 * Rotation with a randr-1.2 driver happens at a different level, so ignore these options.
1301 if ((s = xf86GetOptValString(pNv->Options, OPTION_ROTATE)) && !pNv->randr12_enable) {
1302 if(!xf86NameCmp(s, "CW")) {
1303 pNv->ShadowFB = TRUE;
1304 pNv->NoAccel = TRUE;
1305 pNv->HWCursor = FALSE;
1307 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1308 "Rotating screen clockwise - acceleration disabled\n");
1309 } else if(!xf86NameCmp(s, "CCW")) {
1310 pNv->ShadowFB = TRUE;
1311 pNv->NoAccel = TRUE;
1312 pNv->HWCursor = FALSE;
1314 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1315 "Rotating screen counter clockwise - acceleration disabled\n");
1316 } else if(!xf86NameCmp(s, "RandR")) {
1317 pNv->ShadowFB = TRUE;
1318 pNv->NoAccel = TRUE;
1319 pNv->HWCursor = FALSE;
1320 pNv->RandRRotation = TRUE;
1321 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1322 "Using RandR rotation - acceleration disabled\n");
1324 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1325 "\"%s\" is not a valid value for Option \"Rotate\"\n", s);
1326 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1327 "Valid options are \"CW\", \"CCW\", and \"RandR\"\n");
1331 if(xf86GetOptValInteger(pNv->Options, OPTION_VIDEO_KEY, &(pNv->videoKey))) {
1332 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "video key set to 0x%x\n",
1335 pNv->videoKey = (1 << pScrn->offset.red) |
1336 (1 << pScrn->offset.green) |
1337 (((pScrn->mask.blue >> pScrn->offset.blue) - 1) << pScrn->offset.blue);
1340 /* Things happen on a per output basis for a randr-1.2 driver. */
1341 if (xf86GetOptValBool(pNv->Options, OPTION_FLAT_PANEL, &(pNv->FlatPanel)) && !pNv->randr12_enable) {
1342 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "forcing %s usage\n",
1343 pNv->FlatPanel ? "DFP" : "CRTC");
1345 pNv->FlatPanel = -1; /* autodetect later */
1348 pNv->FPDither = FALSE;
1349 if (xf86GetOptValBool(pNv->Options, OPTION_FP_DITHER, &(pNv->FPDither)))
1350 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "enabling flat panel dither\n");
1352 if (xf86GetOptValInteger(pNv->Options, OPTION_FP_TWEAK,
1353 &pNv->PanelTweak)) {
1354 pNv->usePanelTweak = TRUE;
1356 pNv->usePanelTweak = FALSE;
1359 if (pNv->pEnt->device->MemBase != 0) {
1360 /* Require that the config file value matches one of the PCI values. */
1361 if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->MemBase)) {
1363 "MemBase 0x%08lX doesn't match any PCI base register.\n",
1364 pNv->pEnt->device->MemBase);
1366 pNv->VRAMPhysical = pNv->pEnt->device->MemBase;
1369 if (PCI_DEV_MEM_BASE(pNv->PciInfo, 1) != 0) {
1370 pNv->VRAMPhysical = PCI_DEV_MEM_BASE(pNv->PciInfo, 1) & 0xff800000;
1373 NVPreInitFail("No valid FB address in PCI config space\n");
1377 xf86DrvMsg(pScrn->scrnIndex, from, "Linear framebuffer at 0x%lX\n",
1378 (unsigned long)pNv->VRAMPhysical);
1380 if (pNv->pEnt->device->IOBase != 0) {
1381 /* Require that the config file value matches one of the PCI values. */
1382 if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->IOBase)) {
1383 NVPreInitFail("IOBase 0x%08lX doesn't match any PCI base register.\n",
1384 pNv->pEnt->device->IOBase);
1386 pNv->IOAddress = pNv->pEnt->device->IOBase;
1389 if (PCI_DEV_MEM_BASE(pNv->PciInfo, 0) != 0) {
1390 pNv->IOAddress = PCI_DEV_MEM_BASE(pNv->PciInfo, 0) & 0xffffc000;
1393 NVPreInitFail("No valid MMIO address in PCI config space\n");
1396 xf86DrvMsg(pScrn->scrnIndex, from, "MMIO registers at 0x%lX\n",
1397 (unsigned long)pNv->IOAddress);
1399 if (xf86RegisterResources(pNv->pEnt->index, NULL, ResExclusive)) {
1400 NVPreInitFail("xf86RegisterResources() found resource conflicts\n");
1403 pNv->alphaCursor = (pNv->NVArch >= 0x11);
1405 if(pNv->Architecture < NV_ARCH_10) {
1406 max_width = (pScrn->bitsPerPixel > 16) ? 2032 : 2048;
1409 max_width = (pScrn->bitsPerPixel > 16) ? 4080 : 4096;
1413 if (pNv->randr12_enable) {
1414 /* Allocate an xf86CrtcConfig */
1415 xf86CrtcConfigInit(pScrn, &nv_xf86crtc_config_funcs);
1416 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1418 xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
1421 if (NVPreInitDRI(pScrn) == FALSE) {
1422 NVPreInitFail("\n");
1425 if (!pNv->randr12_enable) {
1426 if ((pScrn->monitor->nHsync == 0) &&
1427 (pScrn->monitor->nVrefresh == 0)) {
1429 config_mon_rates = FALSE;
1431 config_mon_rates = TRUE;
1435 NVCommonSetup(pScrn);
1437 if (pNv->randr12_enable) {
1438 if (pNv->Architecture < NV_ARCH_50) {
1441 num_crtc = pNv->twoHeads ? 2 : 1;
1442 for (i = 0; i < num_crtc; i++) {
1443 nv_crtc_init(pScrn, i);
1446 NvSetupOutputs(pScrn);
1448 if (!NV50DispPreInit(pScrn))
1449 NVPreInitFail("\n");
1450 if (!NV50CreateOutputs(pScrn))
1451 NVPreInitFail("\n");
1452 NV50DispCreateCrtcs(pScrn);
1455 if (!xf86InitialConfiguration(pScrn, FALSE))
1456 NVPreInitFail("No valid modes.\n");
1459 pScrn->videoRam = pNv->RamAmountKBytes;
1460 xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "VideoRAM: %d kBytes\n",
1463 pNv->VRAMPhysicalSize = pScrn->videoRam * 1024;
1466 * If the driver can do gamma correction, it should call xf86SetGamma()
1471 Gamma zeros = {0.0, 0.0, 0.0};
1473 if (!xf86SetGamma(pScrn, zeros)) {
1474 NVPreInitFail("\n");
1479 * Setup the ClockRanges, which describe what clock ranges are available,
1480 * and what sort of modes they can be used for.
1483 clockRanges = xnfcalloc(sizeof(ClockRange), 1);
1484 clockRanges->next = NULL;
1485 clockRanges->minClock = pNv->MinVClockFreqKHz;
1486 clockRanges->maxClock = pNv->MaxVClockFreqKHz;
1487 clockRanges->clockIndex = -1; /* programmable */
1488 clockRanges->doubleScanAllowed = TRUE;
1489 if ((pNv->Architecture == NV_ARCH_20) ||
1490 ((pNv->Architecture == NV_ARCH_10) &&
1491 ((pNv->Chipset & 0x0ff0) != CHIPSET_NV10) &&
1492 ((pNv->Chipset & 0x0ff0) != CHIPSET_NV15))) {
1494 clockRanges->interlaceAllowed = FALSE;
1496 clockRanges->interlaceAllowed = TRUE;
1499 if(pNv->FlatPanel == 1) {
1500 clockRanges->interlaceAllowed = FALSE;
1501 clockRanges->doubleScanAllowed = FALSE;
1505 /* If DFP, add a modeline corresponding to its panel size */
1506 if (pNv->FlatPanel && !pNv->Television && pNv->fpWidth && pNv->fpHeight) {
1507 DisplayModePtr Mode;
1509 Mode = xnfcalloc(1, sizeof(DisplayModeRec));
1510 Mode = xf86CVTMode(pNv->fpWidth, pNv->fpHeight, 60.00, TRUE, FALSE);
1511 Mode->type = M_T_DRIVER;
1512 pScrn->monitor->Modes = xf86ModesAdd(pScrn->monitor->Modes, Mode);
1514 if (!config_mon_rates) {
1516 Mode->HSync = ((float) Mode->Clock ) / ((float) Mode->HTotal);
1517 if (!Mode->VRefresh)
1518 Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
1519 ((float) (Mode->HTotal * Mode->VTotal));
1521 if (Mode->HSync < pScrn->monitor->hsync[0].lo)
1522 pScrn->monitor->hsync[0].lo = Mode->HSync;
1523 if (Mode->HSync > pScrn->monitor->hsync[0].hi)
1524 pScrn->monitor->hsync[0].hi = Mode->HSync;
1525 if (Mode->VRefresh < pScrn->monitor->vrefresh[0].lo)
1526 pScrn->monitor->vrefresh[0].lo = Mode->VRefresh;
1527 if (Mode->VRefresh > pScrn->monitor->vrefresh[0].hi)
1528 pScrn->monitor->vrefresh[0].hi = Mode->VRefresh;
1530 pScrn->monitor->nHsync = 1;
1531 pScrn->monitor->nVrefresh = 1;
1536 if (pNv->randr12_enable) {
1537 pScrn->displayWidth = NVGetVideoPitch(pScrn, pScrn->depth);
1540 * xf86ValidateModes will check that the mode HTotal and VTotal values
1541 * don't exceed the chipset's limit if pScrn->maxHValue and
1542 * pScrn->maxVValue are set. Since our NVValidMode() already takes
1543 * care of this, we don't worry about setting them here.
1545 i = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
1546 pScrn->display->modes, clockRanges,
1547 NULL, 256, max_width,
1548 512, 128, max_height,
1549 pScrn->display->virtualX,
1550 pScrn->display->virtualY,
1551 pNv->VRAMPhysicalSize / 2,
1552 LOOKUP_BEST_REFRESH);
1555 NVPreInitFail("\n");
1558 /* Prune the modes marked as invalid */
1559 xf86PruneDriverModes(pScrn);
1562 * Set the CRTC parameters for all of the modes based on the type
1563 * of mode, and the chipset's interlace requirements.
1565 * Calling this is required if the mode->Crtc* values are used by the
1566 * driver and if the driver doesn't provide code to set them. They
1567 * are not pre-initialised at all.
1569 xf86SetCrtcForModes(pScrn, 0);
1572 if (pScrn->modes == NULL) {
1573 NVPreInitFail("No valid modes found\n");
1576 /* Set the current mode to the first in the list */
1577 pScrn->currentMode = pScrn->modes;
1579 /* Print the list of modes being used */
1580 xf86PrintModes(pScrn);
1582 /* Set display resolution */
1583 xf86SetDpi(pScrn, 0, 0);
1587 * XXX This should be taken into account in some way in the mode valdation
1591 if (xf86LoadSubModule(pScrn, "fb") == NULL) {
1592 NVPreInitFail("\n");
1595 xf86LoaderReqSymLists(fbSymbols, NULL);
1597 /* Load EXA if needed */
1598 if (!pNv->NoAccel) {
1599 if (!xf86LoadSubModule(pScrn, "exa")) {
1600 NVPreInitFail("\n");
1602 xf86LoaderReqSymLists(exaSymbols, NULL);
1605 /* Load ramdac if needed */
1606 if (pNv->HWCursor) {
1607 if (!xf86LoadSubModule(pScrn, "ramdac")) {
1608 NVPreInitFail("\n");
1610 xf86LoaderReqSymLists(ramdacSymbols, NULL);
1613 /* Load shadowfb if needed */
1614 if (pNv->ShadowFB) {
1615 if (!xf86LoadSubModule(pScrn, "shadowfb")) {
1616 NVPreInitFail("\n");
1618 xf86LoaderReqSymLists(shadowSymbols, NULL);
1621 pNv->CurrentLayout.bitsPerPixel = pScrn->bitsPerPixel;
1622 pNv->CurrentLayout.depth = pScrn->depth;
1623 pNv->CurrentLayout.displayWidth = pScrn->displayWidth;
1624 pNv->CurrentLayout.weight.red = pScrn->weight.red;
1625 pNv->CurrentLayout.weight.green = pScrn->weight.green;
1626 pNv->CurrentLayout.weight.blue = pScrn->weight.blue;
1627 pNv->CurrentLayout.mode = pScrn->currentMode;
1629 xf86FreeInt10(pNv->pInt10);
1637 * Map the framebuffer and MMIO memory.
1641 NVMapMem(ScrnInfoPtr pScrn)
1643 NVPtr pNv = NVPTR(pScrn);
1644 int gart_scratch_size;
1647 nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_FB_SIZE, &res);
1649 nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_FB_PHYSICAL, &res);
1650 pNv->VRAMPhysical=res;
1651 nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_AGP_SIZE, &res);
1654 #if !NOUVEAU_EXA_PIXMAPS
1655 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
1656 0, pNv->VRAMPhysicalSize / 2, &pNv->FB)) {
1657 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Failed to allocate memory for framebuffer!\n");
1660 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1661 "Allocated %dMiB VRAM for framebuffer + offscreen pixmaps\n",
1662 (unsigned int)(pNv->FB->size >> 20));
1666 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1667 "AGPGART: %dMiB available\n",
1668 (unsigned int)(pNv->AGPSize >> 20));
1669 if (pNv->AGPSize > (16*1024*1024))
1670 gart_scratch_size = 16*1024*1024;
1672 /* always leave 512kb for other things like the fifos */
1673 gart_scratch_size = pNv->AGPSize - 512*1024;
1675 gart_scratch_size = (4 << 20) - (1 << 18) ;
1676 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1677 "GART: PCI DMA - using %dKiB\n",
1678 gart_scratch_size >> 10);
1681 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_GART | NOUVEAU_BO_PIN, 0,
1682 gart_scratch_size, &pNv->GART)) {
1683 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1684 "Unable to allocate GART memory\n");
1687 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1688 "GART: Allocated %dMiB as a scratch buffer\n",
1689 (unsigned int)(pNv->GART->size >> 20));
1692 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN, 0,
1693 64 * 1024, &pNv->Cursor)) {
1694 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1695 "Failed to allocate memory for hardware cursor\n");
1699 if (pNv->randr12_enable) {
1700 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN, 0,
1701 64 * 1024, &pNv->Cursor2)) {
1702 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1703 "Failed to allocate memory for hardware cursor\n");
1708 if (pNv->Architecture >= NV_ARCH_50) {
1709 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
1710 0, 0x1000, &pNv->CLUT)) {
1711 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1712 "Failed to allocate memory for CLUT\n");
1717 if ((pNv->FB && nouveau_bo_map(pNv->FB, NOUVEAU_BO_RDWR)) ||
1718 (pNv->GART && nouveau_bo_map(pNv->GART, NOUVEAU_BO_RDWR)) ||
1719 (pNv->CLUT && nouveau_bo_map(pNv->CLUT, NOUVEAU_BO_RDWR)) ||
1720 nouveau_bo_map(pNv->Cursor, NOUVEAU_BO_RDWR) ||
1721 (pNv->randr12_enable && nouveau_bo_map(pNv->Cursor2, NOUVEAU_BO_RDWR))) {
1722 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1723 "Failed to map pinned buffers\n");
1731 * Unmap the framebuffer and MMIO memory.
1735 NVUnmapMem(ScrnInfoPtr pScrn)
1737 NVPtr pNv = NVPTR(pScrn);
1739 nouveau_bo_del(&pNv->FB);
1740 nouveau_bo_del(&pNv->GART);
1741 nouveau_bo_del(&pNv->Cursor);
1742 if (pNv->randr12_enable) {
1743 nouveau_bo_del(&pNv->Cursor2);
1745 nouveau_bo_del(&pNv->CLUT);
1752 * Initialise a new mode.
1756 NVModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
1758 vgaHWPtr hwp = VGAHWPTR(pScrn);
1760 NVPtr pNv = NVPTR(pScrn);
1763 /* Initialise the ModeReg values */
1764 if (!vgaHWInit(pScrn, mode))
1766 pScrn->vtSema = TRUE;
1768 vgaReg = &hwp->ModeReg;
1769 nvReg = &pNv->ModeReg;
1771 if(!NVDACInit(pScrn, mode))
1774 NVLockUnlock(pScrn, 0);
1776 nvWriteCurVGA(pNv, NV_VGA_CRTCX_OWNER, nvReg->crtcOwner);
1777 NVLockUnlock(pScrn, 0);
1780 /* Program the registers */
1781 vgaHWProtect(pScrn, TRUE);
1783 NVDACRestore(pScrn, vgaReg, nvReg, FALSE);
1785 #if X_BYTE_ORDER == X_BIG_ENDIAN
1786 /* turn on LFB swapping */
1790 tmp = nvReadCurVGA(pNv, NV_VGA_CRTCX_SWAPPING);
1792 nvWriteCurVGA(pNv, NV_VGA_CRTCX_SWAPPING, tmp);
1797 NVAccelCommonInit(pScrn);
1799 vgaHWProtect(pScrn, FALSE);
1801 pNv->CurrentLayout.mode = mode;
1806 #define NV_MODE_PRIVATE_ID 0x4F37ED65
1807 #define NV_MODE_PRIVATE_SIZE 2
1810 * Match a private mode flag in a special function.
1811 * I don't want ugly casting all over the code.
1814 NVMatchModePrivate(DisplayModePtr mode, uint32_t flags)
1820 if (mode->PrivSize != NV_MODE_PRIVATE_SIZE)
1822 if (mode->Private[0] != NV_MODE_PRIVATE_ID)
1825 if (mode->Private[1] & flags)
1832 NVRestoreConsole(xf86OutputPtr output, DisplayModePtr mode)
1837 xf86CrtcPtr crtc = output->crtc;
1843 xf86SetModeCrtc(mode, INTERLACE_HALVE_V);
1844 DisplayModePtr adjusted_mode = xf86DuplicateMode(mode);
1846 /* Sequence mimics a normal modeset. */
1847 output->funcs->dpms(output, DPMSModeOff);
1848 crtc->funcs->dpms(crtc, DPMSModeOff);
1849 need_unlock = crtc->funcs->lock(crtc);
1850 output->funcs->mode_fixup(output, mode, adjusted_mode);
1851 crtc->funcs->mode_fixup(crtc, mode, adjusted_mode);
1852 output->funcs->prepare(output);
1853 crtc->funcs->prepare(crtc);
1854 /* Always use offset (0,0). */
1855 crtc->funcs->mode_set(crtc, mode, adjusted_mode, 0, 0);
1856 output->funcs->mode_set(output, mode, adjusted_mode);
1857 crtc->funcs->commit(crtc);
1858 output->funcs->commit(output);
1860 crtc->funcs->unlock(crtc);
1861 /* Always turn on outputs afterwards. */
1862 output->funcs->dpms(output, DPMSModeOn);
1863 crtc->funcs->dpms(crtc, DPMSModeOn);
1866 xfree(adjusted_mode);
1869 #define MODEPREFIX(name) NULL, NULL, name, 0,M_T_DRIVER
1870 #define MODESUFFIX 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,FALSE,FALSE,0,NULL,0,0.0,0.0
1872 /* hblankstart: 648, hblankend: 792, vblankstart: 407, vblankend: 442 for 640x400 */
1873 static DisplayModeRec VGAModes[2] = {
1874 { MODEPREFIX("640x400"), 28320, /*25175,*/ 640, 680, 776, 800, 0, 400, 412, 414, 449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 640x400 */
1875 { MODEPREFIX("720x400"), 28320, 720, 738, 846, 900, 0, 400, 412, 414, 449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 720x400@70Hz */
1879 * Restore the initial (text) mode.
1882 NVRestore(ScrnInfoPtr pScrn)
1884 vgaHWPtr hwp = VGAHWPTR(pScrn);
1885 vgaRegPtr vgaReg = &hwp->SavedReg;
1886 NVPtr pNv = NVPTR(pScrn);
1887 NVRegPtr nvReg = &pNv->SavedReg;
1889 if (pNv->randr12_enable) {
1890 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1891 RIVA_HW_STATE *state = &pNv->ModeReg;
1894 /* Let's wipe some state regs */
1903 state->crosswired = FALSE;
1905 if (pNv->new_restore) { /* new style restore. */
1906 for (i = 0; i < xf86_config->num_crtc; i++) {
1907 NVCrtcLockUnlock(xf86_config->crtc[i], 0);
1910 /* Reset some values according to stored console value, to avoid confusion later on. */
1911 /* Otherwise we end up with corrupted terminals. */
1912 for (i = 0; i < xf86_config->num_crtc; i++) {
1913 NVCrtcPrivatePtr nv_crtc = xf86_config->crtc[i]->driver_private;
1914 RIVA_HW_STATE *state = &pNv->SavedReg;
1915 NVCrtcRegPtr savep = &state->crtc_reg[nv_crtc->head];
1916 uint8_t pixelDepth = pNv->console_mode[nv_crtc->head].depth/8;
1917 /* restore PIXEL value */
1918 uint32_t pixel = NVReadVgaCrtc(pNv, nv_crtc->head, NV_VGA_CRTCX_PIXEL) & ~(0xF);
1919 pixel |= (pixelDepth > 2) ? 3 : pixelDepth;
1920 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_VGA_CRTCX_PIXEL, pixel);
1921 /* restore HDisplay and VDisplay */
1922 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_VGA_CRTCX_HDISPE, (pNv->console_mode[nv_crtc->head].x_res)/8 - 1);
1923 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_VGA_CRTCX_VDISPE, (pNv->console_mode[nv_crtc->head].y_res) - 1);
1925 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_VGA_CRTCX_52, pNv->misc_info.crtc_reg_52[nv_crtc->head]);
1926 /* restore crtc base */
1927 NVCrtcWriteCRTC(xf86_config->crtc[i], NV_CRTC_START, pNv->console_mode[nv_crtc->head].fb_start);
1928 /* Restore general control */
1929 NVCrtcWriteRAMDAC(xf86_config->crtc[i], NV_RAMDAC_GENERAL_CONTROL, pNv->misc_info.ramdac_general_control[nv_crtc->head]);
1930 /* Restore CR5758 */
1931 if (pNv->NVArch >= 0x17 && pNv->twoHeads)
1932 for (i = 0; i < 0x10; i++)
1933 NVWriteVgaCrtc5758(pNv, nv_crtc->head, i, savep->CR58[i]);
1936 /* Restore outputs when enabled. */
1937 for (i = 0; i < xf86_config->num_output; i++) {
1938 xf86OutputPtr output = xf86_config->output[i];
1939 if (!xf86_config->output[i]->crtc) /* not enabled? */
1942 NVOutputPrivatePtr nv_output = output->driver_private;
1944 DisplayModePtr mode = NULL;
1945 DisplayModePtr good_mode = NULL;
1946 NVConsoleMode *console = &pNv->console_mode[i];
1947 DisplayModePtr modes = output->probed_modes;
1948 if (!modes) /* no modes means no restore */
1951 if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)
1954 if (console->vga_mode) {
1955 /* We support 640x400 and 720x400 vga modes. */
1956 if (console->x_res == 720)
1957 good_mode = &VGAModes[1];
1959 good_mode = &VGAModes[0];
1960 if (!good_mode) /* No suitable mode found. */
1963 NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
1964 uint32_t old_clock = nv_get_clock_from_crtc(pScrn, &pNv->SavedReg, nv_crtc->head);
1965 uint32_t clock_diff = 0xFFFFFFFF;
1966 for (mode = modes; mode != NULL; mode = mode->next) {
1967 /* We only have the first 8 bits of y_res - 1. */
1968 /* And it's sometimes bogus. */
1969 if (is_fp || !console->enabled) { /* digital outputs are run at their native clock */
1970 if (mode->HDisplay == console->x_res) {
1971 if (!good_mode) /* Pick any match, in case we don't find a 60.0 Hz mode. */
1973 /* Pick a 60.0 Hz mode if there is one. */
1974 if (mode->VRefresh > 59.95 && mode->VRefresh < 60.05) {
1980 if (mode->HDisplay == console->x_res) {
1981 int temp_diff = mode->Clock - old_clock;
1984 if (temp_diff < clock_diff) { /* converge on the closest mode */
1985 clock_diff = temp_diff;
1991 if (!good_mode) /* No suitable mode found. */
1995 mode = xf86DuplicateMode(good_mode);
1997 INT32 *nv_mode = xnfcalloc(sizeof(INT32)*NV_MODE_PRIVATE_SIZE, 1);
1999 /* A semi-unique identifier to avoid using other privates. */
2000 nv_mode[0] = NV_MODE_PRIVATE_ID;
2002 if (console->vga_mode)
2003 nv_mode[1] |= NV_MODE_VGA;
2005 nv_mode[1] |= NV_MODE_CONSOLE;
2007 mode->Private = nv_mode;
2008 mode->PrivSize = NV_MODE_PRIVATE_SIZE;
2010 uint8_t scale_backup = nv_output->scaling_mode;
2011 if (nv_output->type == OUTPUT_LVDS || nv_output->type == OUTPUT_TMDS)
2012 nv_output->scaling_mode = SCALE_FULLSCREEN;
2014 NVRestoreConsole(output, mode);
2016 /* Restore value, so we reenter X properly. */
2017 nv_output->scaling_mode = scale_backup;
2019 xfree(mode->Private);
2023 /* Force hide the cursor. */
2024 for (i = 0; i < xf86_config->num_crtc; i++) {
2025 xf86_config->crtc[i]->funcs->hide_cursor(xf86_config->crtc[i]);
2028 /* Lock the crtc's. */
2029 for (i = 0; i < xf86_config->num_crtc; i++) {
2030 NVCrtcLockUnlock(xf86_config->crtc[i], 1);
2033 /* Let's clean our slate once again, so we always rewrite vpll's upon returning to X. */
2042 state->crosswired = FALSE;
2044 for (i = 0; i < xf86_config->num_crtc; i++) {
2045 NVCrtcLockUnlock(xf86_config->crtc[i], 0);
2048 /* Some aspects of an output needs to be restore before the crtc. */
2049 /* In my case this has to do with the mode that i get at very low resolutions. */
2050 /* If i do this at the end, it will not be restored properly */
2051 for (i = 0; i < xf86_config->num_output; i++) {
2052 NVOutputPrivatePtr nv_output2 = xf86_config->output[i]->driver_private;
2053 NVOutputRegPtr regp = &nvReg->dac_reg[nv_output2->preferred_output];
2054 Bool crosswired = regp->TMDS[0x4] & (1 << 3);
2055 /* Let's guess the bios state ;-) */
2056 if (nv_output2->type == OUTPUT_TMDS)
2057 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Restoring TMDS timings, before restoring anything else.\n");
2058 if (nv_output2->type == OUTPUT_LVDS)
2059 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Restoring LVDS timings, before restoring anything else.\n");
2060 if (nv_output2->type == OUTPUT_TMDS || nv_output2->type == OUTPUT_LVDS) {
2061 uint32_t clock = nv_calc_tmds_clock_from_pll(xf86_config->output[i]);
2062 nv_set_tmds_registers(xf86_config->output[i], clock, TRUE, crosswired);
2066 /* This needs to happen before the crtc restore happens. */
2067 for (i = 0; i < xf86_config->num_output; i++) {
2068 NVOutputPrivatePtr nv_output = xf86_config->output[i]->driver_private;
2069 /* Select the default output resource for consistent restore. */
2070 if (ffs(pNv->dcb_table.entry[nv_output->dcb_entry].or) & OUTPUT_1) {
2071 nv_output->output_resource = 1;
2073 nv_output->output_resource = 0;
2077 for (i = 0; i < xf86_config->num_crtc; i++) {
2078 NVCrtcPrivatePtr nv_crtc = xf86_config->crtc[i]->driver_private;
2079 /* Restore this, so it doesn't mess with restore. */
2080 pNv->fp_regs_owner[nv_crtc->head] = nv_crtc->head;
2083 for (i = 0; i < xf86_config->num_crtc; i++) {
2084 xf86_config->crtc[i]->funcs->restore(xf86_config->crtc[i]);
2087 for (i = 0; i < xf86_config->num_output; i++) {
2088 xf86_config->output[i]->funcs->restore(xf86_config->
2092 for (i = 0; i < xf86_config->num_crtc; i++) {
2093 NVCrtcLockUnlock(xf86_config->crtc[i], 1);
2097 NVLockUnlock(pScrn, 0);
2100 nvWriteCurVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->crtc_active[1] * 0x3);
2101 NVLockUnlock(pScrn, 0);
2104 /* Only restore text mode fonts/text for the primary card */
2105 vgaHWProtect(pScrn, TRUE);
2106 NVDACRestore(pScrn, vgaReg, nvReg, pNv->Primary);
2107 vgaHWProtect(pScrn, FALSE);
2110 if (pNv->twoHeads && !pNv->new_restore) {
2111 NVSetOwner(pScrn, 0); /* move to head A to set owner */
2112 NVLockVgaCrtc(pNv, 0, false);
2113 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Restoring CRTC_OWNER to %d.\n", pNv->vtOWNER);
2114 NVWriteVGA(pNv, 0, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
2115 NVLockVgaCrtc(pNv, 0, true);
2120 NVLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
2121 LOCO * colors, VisualPtr pVisual)
2123 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2126 CARD16 lut_r[256], lut_g[256], lut_b[256];
2128 for (c = 0; c < xf86_config->num_crtc; c++) {
2129 xf86CrtcPtr crtc = xf86_config->crtc[c];
2131 if (crtc->enabled == 0)
2134 /* code borrowed from intel driver */
2135 switch (pScrn->depth) {
2137 for (i = 0; i < numColors; i++) {
2139 for (j = 0; j < 8; j++) {
2140 lut_r[index * 8 + j] = colors[index].red << 8;
2141 lut_g[index * 8 + j] = colors[index].green << 8;
2142 lut_b[index * 8 + j] = colors[index].blue << 8;
2146 for (i = 0; i < numColors; i++) {
2150 for (j = 0; j < 8; j++) {
2151 lut_r[index * 8 + j] = colors[index].red << 8;
2152 lut_b[index * 8 + j] = colors[index].blue << 8;
2156 for (j = 0; j < 4; j++) {
2157 lut_g[index * 4 + j] = colors[index].green << 8;
2161 for (i = 0; i < numColors; i++) {
2163 lut_r[index] = colors[index].red << 8;
2164 lut_g[index] = colors[index].green << 8;
2165 lut_b[index] = colors[index].blue << 8;
2170 /* Make the change through RandR */
2171 RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
2175 #define DEPTH_SHIFT(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
2176 #define COLOR(c) (unsigned int)(0x3fff * ((c)/255.0))
2178 NV50LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
2179 LOCO * colors, VisualPtr pVisual)
2181 NVPtr pNv = NVPTR(pScrn);
2184 unsigned short red, green, blue, unused;
2185 } *lut = (void *) pNv->CLUT->map;
2187 switch (pScrn->depth) {
2189 for (i = 0; i < numColors; i++) {
2191 lut[DEPTH_SHIFT(index, 5)].red =
2192 COLOR(colors[index].red);
2193 lut[DEPTH_SHIFT(index, 5)].green =
2194 COLOR(colors[index].green);
2195 lut[DEPTH_SHIFT(index, 5)].blue =
2196 COLOR(colors[index].blue);
2200 for (i = 0; i < numColors; i++) {
2202 lut[DEPTH_SHIFT(index, 6)].green =
2203 COLOR(colors[index].green);
2205 lut[DEPTH_SHIFT(index, 5)].red =
2206 COLOR(colors[index].red);
2207 lut[DEPTH_SHIFT(index, 5)].blue =
2208 COLOR(colors[index].blue);
2213 for (i = 0; i < numColors; i++) {
2215 lut[index].red = COLOR(colors[index].red);
2216 lut[index].green = COLOR(colors[index].green);
2217 lut[index].blue = COLOR(colors[index].blue);
2224 static void NVBacklightEnable(NVPtr pNv, Bool on)
2226 /* This is done differently on each laptop. Here we
2227 define the ones we know for sure. */
2229 #if defined(__powerpc__)
2230 if((pNv->Chipset == 0x10DE0179) ||
2231 (pNv->Chipset == 0x10DE0189) ||
2232 (pNv->Chipset == 0x10DE0329))
2234 /* NV17,18,34 Apple iMac, iBook, PowerBook */
2235 CARD32 tmp_pmc, tmp_pcrt;
2236 tmp_pmc = nvReadMC(pNv, NV_PBUS_DEBUG_DUALHEAD_CTL) & 0x7FFFFFFF;
2237 tmp_pcrt = NVReadCRTC(pNv, 0, NV_CRTC_GPIO_EXT) & 0xFFFFFFFC;
2239 tmp_pmc |= (1 << 31);
2242 nvWriteMC(pNv, NV_PBUS_DEBUG_DUALHEAD_CTL, tmp_pmc);
2243 NVWriteCRTC(pNv, 0, NV_CRTC_GPIO_EXT, tmp_pcrt);
2248 if(pNv->twoHeads && ((pNv->Chipset & 0x0ff0) != CHIPSET_NV11)) {
2249 nvWriteMC(pNv, 0x130C, on ? 3 : 7);
2254 fpcontrol = nvReadCurRAMDAC(pNv, NV_RAMDAC_FP_CONTROL) & 0xCfffffCC;
2256 /* cut the TMDS output */
2257 if(on) fpcontrol |= pNv->fpSyncs;
2258 else fpcontrol |= 0x20000022;
2260 nvWriteCurRAMDAC(pNv, NV_RAMDAC_FP_CONTROL, fpcontrol);
2265 NVDPMSSetLCD(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
2267 NVPtr pNv = NVPTR(pScrn);
2269 if (!pScrn->vtSema) return;
2271 vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2273 switch (PowerManagementMode) {
2274 case DPMSModeStandby: /* HSync: Off, VSync: On */
2275 case DPMSModeSuspend: /* HSync: On, VSync: Off */
2276 case DPMSModeOff: /* HSync: Off, VSync: Off */
2277 NVBacklightEnable(pNv, 0);
2279 case DPMSModeOn: /* HSync: On, VSync: On */
2280 NVBacklightEnable(pNv, 1);
2288 NVDPMSSet(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
2290 unsigned char crtc1A;
2291 vgaHWPtr hwp = VGAHWPTR(pScrn);
2293 if (!pScrn->vtSema) return;
2295 crtc1A = hwp->readCrtc(hwp, 0x1A) & ~0xC0;
2297 switch (PowerManagementMode) {
2298 case DPMSModeStandby: /* HSync: Off, VSync: On */
2301 case DPMSModeSuspend: /* HSync: On, VSync: Off */
2304 case DPMSModeOff: /* HSync: Off, VSync: Off */
2307 case DPMSModeOn: /* HSync: On, VSync: On */
2312 /* vgaHWDPMSSet will merely cut the dac output */
2313 vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2315 hwp->writeCrtc(hwp, 0x1A, crtc1A);
2321 /* This gets called at the start of each server generation */
2324 NVScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
2331 unsigned char *FBStart;
2332 int width, height, displayWidth, shadowHeight, i;
2335 * First get the ScrnInfoRec
2337 pScrn = xf86Screens[pScreen->myNum];
2339 hwp = VGAHWPTR(pScrn);
2342 /* Map the VGA memory when the primary video */
2344 hwp->MapSize = 0x10000;
2345 if (!vgaHWMapMem(pScrn))
2349 /* First init DRI/DRM */
2350 if (!NVDRIScreenInit(pScrn))
2353 /* Allocate and map memory areas we need */
2354 if (!NVMapMem(pScrn))
2357 if (!pNv->NoAccel) {
2358 /* Init DRM - Alloc FIFO */
2359 if (!NVInitDma(pScrn))
2362 /* setup graphics objects */
2363 if (!NVAccelCommonInit(pScrn))
2367 #if NOUVEAU_EXA_PIXMAPS
2368 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
2369 0, NOUVEAU_ALIGN(pScrn->virtualX, 64) * NOUVEAU_ALIGN(pScrn->virtualY, 64) *
2370 (pScrn->bitsPerPixel >> 3), &pNv->FB)) {
2371 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Failed to allocate memory for screen pixmap.\n");
2376 if (!pNv->randr12_enable) {
2377 /* Save the current state */
2379 /* Initialise the first mode */
2380 if (!NVModeInit(pScrn, pScrn->currentMode))
2383 /* Darken the screen for aesthetic reasons and set the viewport */
2384 NVSaveScreen(pScreen, SCREEN_SAVER_ON);
2385 pScrn->AdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
2387 pScrn->memPhysBase = pNv->VRAMPhysical;
2388 pScrn->fbOffset = 0;
2390 /* Gather some misc info before the randr stuff kicks in */
2391 if (pNv->Architecture >= NV_ARCH_10) {
2392 pNv->misc_info.crtc_reg_52[0] = NVReadVGA(pNv, 0, NV_VGA_CRTCX_52);
2393 pNv->misc_info.crtc_reg_52[1] = NVReadVGA(pNv, 1, NV_VGA_CRTCX_52);
2395 if (pNv->Architecture == NV_ARCH_40) {
2396 pNv->misc_info.ramdac_0_reg_580 = NVReadRAMDAC(pNv, 0, NV_RAMDAC_580);
2397 pNv->misc_info.reg_c040 = nvReadMC(pNv, 0xc040);
2399 pNv->misc_info.ramdac_general_control[0] = NVReadRAMDAC(pNv, 0, NV_RAMDAC_GENERAL_CONTROL);
2400 pNv->misc_info.ramdac_general_control[1] = NVReadRAMDAC(pNv, 1, NV_RAMDAC_GENERAL_CONTROL);
2401 pNv->misc_info.ramdac_0_pllsel = NVReadRAMDAC(pNv, 0, NV_RAMDAC_PLL_SELECT);
2402 pNv->misc_info.sel_clk = NVReadRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK);
2403 if (pNv->twoHeads) {
2404 pNv->misc_info.output[0] = NVReadRAMDAC(pNv, 0, NV_RAMDAC_OUTPUT);
2405 pNv->misc_info.output[1] = NVReadRAMDAC(pNv, 1, NV_RAMDAC_OUTPUT);
2408 for (i = 0; i <= pNv->twoHeads; i++) {
2409 if (NVReadVGA(pNv, i, NV_VGA_CRTCX_PIXEL) & 0xf) { /* framebuffer mode */
2410 pNv->console_mode[i].vga_mode = FALSE;
2411 uint8_t var = NVReadVGA(pNv, i, NV_VGA_CRTCX_PIXEL) & 0xf;
2412 Bool filled = (NVReadRAMDAC(pNv, i, NV_RAMDAC_GENERAL_CONTROL) & 0x1000);
2416 pNv->console_mode[i].depth = 32;
2418 pNv->console_mode[i].depth = 24;
2419 /* This is pitch related. */
2420 pNv->console_mode[i].bpp = 32;
2424 pNv->console_mode[i].depth = 16;
2426 pNv->console_mode[i].depth = 15;
2427 /* This is pitch related. */
2428 pNv->console_mode[i].bpp = 16;
2431 /* 8bit mode is always filled? */
2432 pNv->console_mode[i].depth = 8;
2433 /* This is pitch related. */
2434 pNv->console_mode[i].bpp = 8;
2438 } else { /* vga mode */
2439 pNv->console_mode[i].vga_mode = TRUE;
2440 pNv->console_mode[i].bpp = 4;
2441 pNv->console_mode[i].depth = 4;
2444 pNv->console_mode[i].x_res = (NVReadVGA(pNv, i, NV_VGA_CRTCX_HDISPE) + 1) * 8;
2445 pNv->console_mode[i].y_res = (NVReadVGA(pNv, i, NV_VGA_CRTCX_VDISPE) + 1); /* NV_VGA_CRTCX_VDISPE only contains the lower 8 bits. */
2447 pNv->console_mode[i].fb_start = NVReadCRTC(pNv, i, NV_CRTC_START);
2449 pNv->console_mode[i].enabled = FALSE;
2451 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CRTC %d: Console mode: %dx%d depth: %d bpp: %d crtc_start: 0x%X.\n", i, pNv->console_mode[i].x_res, pNv->console_mode[i].y_res, pNv->console_mode[i].depth, pNv->console_mode[i].bpp, pNv->console_mode[i].fb_start);
2454 /* Check if crtc's were enabled. */
2455 if (pNv->misc_info.ramdac_0_pllsel & NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL) {
2456 pNv->console_mode[0].enabled = TRUE;
2457 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CRTC 0 was enabled.\n");
2460 if (pNv->misc_info.ramdac_0_pllsel & NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2) {
2461 pNv->console_mode[1].enabled = TRUE;
2462 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CRTC 1 was enabled.\n");
2465 if (!NVEnterVT(scrnIndex, 0))
2467 NVSaveScreen(pScreen, SCREEN_SAVER_ON);
2472 * The next step is to setup the screen's visuals, and initialise the
2473 * framebuffer code. In cases where the framebuffer's default
2474 * choices for things like visual layouts and bits per RGB are OK,
2475 * this may be as simple as calling the framebuffer's ScreenInit()
2476 * function. If not, the visuals will need to be setup before calling
2477 * a fb ScreenInit() function and fixed up after.
2479 * For most PC hardware at depths >= 8, the defaults that fb uses
2480 * are not appropriate. In this driver, we fixup the visuals after.
2484 * Reset the visual list.
2486 miClearVisualTypes();
2488 /* Setup the visuals we support. */
2490 if (!miSetVisualTypes(pScrn->depth,
2491 miGetDefaultVisualMask(pScrn->depth), 8,
2492 pScrn->defaultVisual))
2494 if (!miSetPixmapDepths ())
2498 * Call the framebuffer layer's ScreenInit function, and fill in other
2502 width = pScrn->virtualX;
2503 height = pScrn->virtualY;
2504 displayWidth = pScrn->displayWidth;
2507 height = pScrn->virtualX;
2508 width = pScrn->virtualY;
2511 /* If RandR rotation is enabled, leave enough space in the
2512 * framebuffer for us to rotate the screen dimensions without
2513 * changing the pitch.
2515 if(pNv->RandRRotation) {
2516 shadowHeight = max(width, height);
2518 shadowHeight = height;
2521 if (pNv->ShadowFB) {
2522 pNv->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * width);
2523 pNv->ShadowPtr = xalloc(pNv->ShadowPitch * shadowHeight);
2524 displayWidth = pNv->ShadowPitch / (pScrn->bitsPerPixel >> 3);
2525 FBStart = pNv->ShadowPtr;
2527 pNv->ShadowPtr = NULL;
2528 FBStart = pNv->FB->map;
2531 switch (pScrn->bitsPerPixel) {
2535 ret = fbScreenInit(pScreen, FBStart, width, height,
2536 pScrn->xDpi, pScrn->yDpi,
2537 displayWidth, pScrn->bitsPerPixel);
2540 xf86DrvMsg(scrnIndex, X_ERROR,
2541 "Internal error: invalid bpp (%d) in NVScreenInit\n",
2542 pScrn->bitsPerPixel);
2549 if (pScrn->bitsPerPixel > 8) {
2550 /* Fixup RGB ordering */
2551 visual = pScreen->visuals + pScreen->numVisuals;
2552 while (--visual >= pScreen->visuals) {
2553 if ((visual->class | DynamicClass) == DirectColor) {
2554 visual->offsetRed = pScrn->offset.red;
2555 visual->offsetGreen = pScrn->offset.green;
2556 visual->offsetBlue = pScrn->offset.blue;
2557 visual->redMask = pScrn->mask.red;
2558 visual->greenMask = pScrn->mask.green;
2559 visual->blueMask = pScrn->mask.blue;
2564 fbPictureInit (pScreen, 0, 0);
2566 xf86SetBlackWhitePixels(pScreen);
2568 if (!pNv->NoAccel) {
2569 if (!NVExaInit(pScreen))
2571 NVAccelCommonInit(pScrn);
2572 } else if (pNv->VRAMPhysicalSize / 2 < NOUVEAU_ALIGN(pScrn->virtualX, 64) * NOUVEAU_ALIGN(pScrn->virtualY, 64) * (pScrn->bitsPerPixel >> 3)) {
2573 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "The virtual screen size's resolution is too big for the video RAM framebuffer at this colour depth.\n");
2578 miInitializeBackingStore(pScreen);
2579 xf86SetBackingStore(pScreen);
2580 xf86SetSilkenMouse(pScreen);
2582 /* Finish DRI init */
2583 NVDRIFinishScreenInit(pScrn);
2586 * Initialize software cursor.
2587 * Must precede creation of the default colormap.
2589 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
2592 * Initialize HW cursor layer.
2593 * Must follow software cursor initialization.
2595 if (pNv->HWCursor) {
2596 if (pNv->Architecture < NV_ARCH_50 && !pNv->randr12_enable)
2597 ret = NVCursorInit(pScreen);
2598 else if (pNv->Architecture < NV_ARCH_50 && pNv->randr12_enable)
2599 ret = NVCursorInitRandr12(pScreen);
2601 ret = NV50CursorInit(pScreen);
2604 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2605 "Hardware cursor initialization failed\n");
2606 pNv->HWCursor = FALSE;
2610 if (pNv->randr12_enable) {
2611 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
2613 if (!xf86CrtcScreenInit(pScreen))
2616 pNv->PointerMoved = pScrn->PointerMoved;
2617 pScrn->PointerMoved = NVPointerMoved;
2620 /* Initialise default colourmap */
2621 if (!miCreateDefColormap(pScreen))
2625 * Initialize colormap layer.
2626 * Must follow initialization of the default colormap
2628 if (!pNv->randr12_enable) {
2629 if(!xf86HandleColormaps(pScreen, 256, 8, NVDACLoadPalette,
2630 NULL, CMAP_RELOAD_ON_MODE_SWITCH | CMAP_PALETTED_TRUECOLOR))
2633 if (pNv->Architecture < NV_ARCH_50) {
2634 if (!xf86HandleColormaps(pScreen, 256, 8, NVLoadPalette,
2636 CMAP_RELOAD_ON_MODE_SWITCH |
2637 CMAP_PALETTED_TRUECOLOR))
2640 if (!xf86HandleColormaps(pScreen, 256, 8, NV50LoadPalette,
2641 NULL, CMAP_PALETTED_TRUECOLOR))
2647 RefreshAreaFuncPtr refreshArea = NVRefreshArea;
2649 if (pNv->Rotate || pNv->RandRRotation) {
2650 pNv->PointerMoved = pScrn->PointerMoved;
2652 pScrn->PointerMoved = NVPointerMoved;
2654 switch(pScrn->bitsPerPixel) {
2655 case 8: refreshArea = NVRefreshArea8; break;
2656 case 16: refreshArea = NVRefreshArea16; break;
2657 case 32: refreshArea = NVRefreshArea32; break;
2659 if(!pNv->RandRRotation) {
2661 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2662 "Driver rotation enabled, RandR disabled\n");
2666 ShadowFBInit(pScreen, refreshArea);
2669 if (!pNv->randr12_enable) {
2670 if(pNv->FlatPanel) {
2671 xf86DPMSInit(pScreen, NVDPMSSetLCD, 0);
2673 xf86DPMSInit(pScreen, NVDPMSSet, 0);
2677 pScrn->memPhysBase = pNv->VRAMPhysical;
2678 pScrn->fbOffset = 0;
2680 if (pNv->Rotate == 0 && !pNv->RandRRotation)
2681 NVInitVideo(pScreen);
2683 pScreen->SaveScreen = NVSaveScreen;
2685 /* Wrap the current CloseScreen function */
2686 pNv->CloseScreen = pScreen->CloseScreen;
2687 pScreen->CloseScreen = NVCloseScreen;
2689 pNv->BlockHandler = pScreen->BlockHandler;
2690 pScreen->BlockHandler = NVBlockHandler;
2692 /* Install our DriverFunc. We have to do it this way instead of using the
2693 * HaveDriverFuncs argument to xf86AddDriver, because InitOutput clobbers
2696 if (!pNv->randr12_enable)
2697 pScrn->DriverFunc = NVDriverFunc;
2699 /* Report any unused options (only for the first generation) */
2700 if (serverGeneration == 1) {
2701 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
2708 NVSaveScreen(ScreenPtr pScreen, int mode)
2710 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
2711 NVPtr pNv = NVPTR(pScrn);
2713 Bool on = xf86IsUnblank(mode);
2715 if (pNv->randr12_enable) {
2716 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2717 if (pScrn->vtSema && pNv->Architecture < NV_ARCH_50) {
2718 for (i = 0; i < xf86_config->num_crtc; i++) {
2720 if (xf86_config->crtc[i]->enabled) {
2721 NVCrtcPrivatePtr nv_crtc = xf86_config->crtc[i]->driver_private;
2722 NVBlankScreen(pScrn, nv_crtc->head, !on);
2730 return vgaHWSaveScreen(pScreen, mode);
2734 NVSave(ScrnInfoPtr pScrn)
2736 NVPtr pNv = NVPTR(pScrn);
2737 NVRegPtr nvReg = &pNv->SavedReg;
2739 if (pNv->randr12_enable) {
2740 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2743 for (i = 0; i < xf86_config->num_crtc; i++) {
2744 xf86_config->crtc[i]->funcs->save(xf86_config->crtc[i]);
2747 for (i = 0; i < xf86_config->num_output; i++) {
2748 xf86_config->output[i]->funcs->save(xf86_config->
2752 vgaHWPtr pVga = VGAHWPTR(pScrn);
2753 vgaRegPtr vgaReg = &pVga->SavedReg;
2754 NVLockUnlock(pScrn, 0);
2755 if (pNv->twoHeads) {
2756 nvWriteCurVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->crtc_active[1] * 0x3);
2757 NVLockUnlock(pScrn, 0);
2760 NVDACSave(pScrn, vgaReg, nvReg, pNv->Primary);
2765 NVRandRGetInfo(ScrnInfoPtr pScrn, Rotation *rotations)
2767 NVPtr pNv = NVPTR(pScrn);
2769 if(pNv->RandRRotation)
2770 *rotations = RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_270;
2772 *rotations = RR_Rotate_0;
2778 NVRandRSetConfig(ScrnInfoPtr pScrn, xorgRRConfig *config)
2780 NVPtr pNv = NVPTR(pScrn);
2782 switch(config->rotation) {
2785 pScrn->PointerMoved = pNv->PointerMoved;
2790 pScrn->PointerMoved = NVPointerMoved;
2795 pScrn->PointerMoved = NVPointerMoved;
2799 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2800 "Unexpected rotation in NVRandRSetConfig!\n");
2802 pScrn->PointerMoved = pNv->PointerMoved;
2810 NVDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op, pointer data)
2814 return NVRandRGetInfo(pScrn, (Rotation*)data);
2816 return NVRandRSetConfig(pScrn, (xorgRRConfig*)data);