Don't allocate the whole AGP gart as a scratch buffer, since we still need to allocat...
[nouveau] / src / nv_driver.c
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 $ */
3 /*
4  * Copyright 1996-1997  David J. McKay
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * SOFTWARE.
23  */
24
25 /* Hacked together from mga driver and 3.3.4 NVIDIA driver by Jarno Paananen
26    <jpaana@s2.org> */
27
28 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/nv/nv_driver.c,v 1.144 2006/06/16 00:19:32 mvojkovi Exp $ */
29
30 #include <stdio.h>
31
32 #include "nv_include.h"
33
34 #include "xf86int10.h"
35
36 #include "xf86drm.h"
37
38 extern DisplayModePtr xf86ModesAdd(DisplayModePtr Modes, DisplayModePtr Additions);
39
40 /*
41  * Forward definitions for the functions that make up the driver.
42  */
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,
51                             char **argv);
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);
56
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,
62                               pointer data);
63
64 /* Internally used functions */
65
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);
71
72 #ifdef XSERVER_LIBPCIACCESS
73
74 #define NOUVEAU_PCI_DEVICE(_vendor_id, _device_id) \
75         { (_vendor_id), (_device_id), PCI_MATCH_ANY, PCI_MATCH_ANY, 0x00030000, 0x00ffffff, 0 }
76
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),
80         { 0, 0, 0 },
81 };
82
83 static Bool NVPciProbe (        DriverPtr               drv,
84                                 int                     entity_num,
85                                 struct pci_device       *dev,
86                                 intptr_t                match_data      );
87
88 #endif /* XSERVER_LIBPCIACCESS */
89
90 /*
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.
96  */
97
98 _X_EXPORT DriverRec NV = {
99         NV_VERSION,
100         NV_DRIVER_NAME,
101         NVIdentify,
102 #ifdef XSERVER_LIBPCIACCESS
103         NULL,
104 #else
105         NVProbe,
106 #endif /* XSERVER_LIBPCIACCESS */
107         NVAvailableOptions,
108         NULL,
109         0,
110         NULL,
111 #ifdef XSERVER_LIBPCIACCESS
112         nouveau_device_match,
113         NVPciProbe
114 #endif /* XSERVER_LIBPCIACCESS */
115 };
116
117 struct NvFamily
118 {
119   char *name;
120   char *chipset;
121 };
122
123 static struct NvFamily NVKnownFamilies[] =
124 {
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" },
136   { NULL, NULL}
137 };
138
139 /*
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.
146  */
147
148 static const char *vgahwSymbols[] = {
149     "vgaHWUnmapMem",
150     "vgaHWDPMSSet",
151     "vgaHWFreeHWRec",
152     "vgaHWGetHWRec",
153     "vgaHWGetIndex",
154     "vgaHWInit",
155     "vgaHWMapMem",
156     "vgaHWProtect",
157     "vgaHWRestore",
158     "vgaHWSave",
159     "vgaHWSaveScreen",
160     NULL
161 };
162
163 static const char *fbSymbols[] = {
164     "fbPictureInit",
165     "fbScreenInit",
166     NULL
167 };
168
169 static const char *exaSymbols[] = {
170     "exaDriverInit",
171     "exaOffscreenInit",
172     NULL
173 };
174
175 static const char *ramdacSymbols[] = {
176     "xf86CreateCursorInfoRec",
177     "xf86DestroyCursorInfoRec",
178     "xf86InitCursor",
179     NULL
180 };
181
182 static const char *ddcSymbols[] = {
183     "xf86PrintEDID",
184     "xf86DoEDID_DDC2",
185     "xf86SetDDCproperties",
186     NULL
187 };
188
189 static const char *vbeSymbols[] = {
190     "VBEInit",
191     "vbeFree",
192     "vbeDoEDID",
193     NULL
194 };
195
196 static const char *i2cSymbols[] = {
197     "xf86CreateI2CBusRec",
198     "xf86I2CBusInit",
199     NULL
200 };
201
202 static const char *shadowSymbols[] = {
203     "ShadowFBInit",
204     NULL
205 };
206
207 static const char *int10Symbols[] = {
208     "xf86FreeInt10",
209     "xf86InitInt10",
210     NULL
211 };
212
213 const char *drmSymbols[] = {
214     "drmOpen", 
215     "drmAddBufs",
216     "drmAddMap",
217     "drmAgpAcquire",
218     "drmAgpVersionMajor",
219     "drmAgpVersionMinor",
220     "drmAgpAlloc",
221     "drmAgpBind",
222     "drmAgpEnable",
223     "drmAgpFree",
224     "drmAgpRelease",
225     "drmAgpUnbind",
226     "drmAuthMagic",
227     "drmCommandNone",
228     "drmCommandWrite",
229     "drmCommandWriteRead",
230     "drmCreateContext",
231     "drmCtlInstHandler",
232     "drmCtlUninstHandler",
233     "drmDestroyContext",
234     "drmFreeVersion",
235     "drmGetInterruptFromBusID",
236     "drmGetLibVersion",
237     "drmGetVersion",
238     NULL
239 };
240
241 const char *driSymbols[] = {
242     "DRICloseScreen",
243     "DRICreateInfoRec",
244     "DRIDestroyInfoRec",
245     "DRIFinishScreenInit",
246     "DRIGetSAREAPrivate",
247     "DRILock",
248     "DRIQueryVersion",
249     "DRIScreenInit",
250     "DRIUnlock",
251     "GlxSetVisualConfigs",
252     "DRICreatePCIBusID",
253     NULL
254 };
255
256
257 static MODULESETUPPROTO(nouveauSetup);
258
259 static XF86ModuleVersionInfo nouveauVersRec =
260 {
261     "nouveau",
262     MODULEVENDORSTRING,
263     MODINFOSTRING1,
264     MODINFOSTRING2,
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,
269     MOD_CLASS_VIDEODRV,
270     {0,0,0,0}
271 };
272
273 _X_EXPORT XF86ModuleData nouveauModuleData = { &nouveauVersRec, nouveauSetup, NULL };
274
275
276 /*
277  * This is intentionally screen-independent.  It indicates the binding
278  * choice made in the first PreInit.
279  */
280 static int pix24bpp = 0;
281
282 static Bool
283 NVGetRec(ScrnInfoPtr pScrn)
284 {
285     /*
286      * Allocate an NVRec, and hook it into pScrn->driverPrivate.
287      * pScrn->driverPrivate is initialised to NULL, so we can check if
288      * the allocation has already been done.
289      */
290     if (pScrn->driverPrivate != NULL)
291         return TRUE;
292
293     pScrn->driverPrivate = xnfcalloc(sizeof(NVRec), 1);
294     /* Initialise it */
295
296     return TRUE;
297 }
298
299 static void
300 NVFreeRec(ScrnInfoPtr pScrn)
301 {
302     if (pScrn->driverPrivate == NULL)
303         return;
304     xfree(pScrn->driverPrivate);
305     pScrn->driverPrivate = NULL;
306 }
307
308
309 static pointer
310 nouveauSetup(pointer module, pointer opts, int *errmaj, int *errmin)
311 {
312         static Bool setupDone = FALSE;
313
314         /* This module should be loaded only once, but check to be sure. */
315
316         if (!setupDone) {
317                 setupDone = TRUE;
318                 /* The 1 here is needed to turn off a backwards compatibility mode */
319                 /* Otherwise NVPciProbe() is not called */
320                 xf86AddDriver(&NV, module, 1);
321
322                 /*
323                  * Modules that this driver always requires may be loaded here
324                  * by calling LoadSubModule().
325                  */
326                 /*
327                  * Tell the loader about symbols from other modules that this module
328                  * might refer to.
329                  */
330                 LoaderRefSymLists(vgahwSymbols, exaSymbols, fbSymbols,
331 #ifdef XF86DRI
332                                 drmSymbols, 
333 #endif
334                                 ramdacSymbols, shadowSymbols,
335                                 i2cSymbols, ddcSymbols, vbeSymbols,
336                                 int10Symbols, NULL);
337
338                 /*
339                  * The return value must be non-NULL on success even though there
340                  * is no TearDownProc.
341                  */
342                 return (pointer)1;
343         } else {
344                 if (errmaj) *errmaj = LDR_ONCEONLY;
345                 return NULL;
346         }
347 }
348
349 static const OptionInfoRec *
350 NVAvailableOptions(int chipid, int busid)
351 {
352     return NVOptions;
353 }
354
355 /* Mandatory */
356 static void
357 NVIdentify(int flags)
358 {
359     struct NvFamily *family;
360     size_t maxLen=0;
361
362     xf86DrvMsg(0, X_INFO, NV_NAME " driver " NV_DRIVER_DATE "\n");
363     xf86DrvMsg(0, X_INFO, NV_NAME " driver for NVIDIA chipset families :\n");
364
365     /* maximum length for alignment */
366     family = NVKnownFamilies;
367     while(family->name && family->chipset)
368     {
369         maxLen = max(maxLen, strlen(family->name));
370         family++;
371     }
372
373     /* display */
374     family = NVKnownFamilies;
375     while(family->name && family->chipset)
376     {
377         size_t len = strlen(family->name);
378         xf86ErrorF("\t%s", family->name);
379         while(len<maxLen+1)
380         {
381             xf86ErrorF(" ");
382             len++;
383         }
384         xf86ErrorF("(%s)\n", family->chipset);
385         family++;
386     }
387 }
388
389
390 #ifndef XSERVER_LIBPCIACCESS
391 static Bool
392 NVGetScrnInfoRec(PciChipsets *chips, int chip)
393 {
394     ScrnInfoPtr pScrn;
395
396     pScrn = xf86ConfigPciEntity(NULL, 0, chip,
397                                 chips, NULL, NULL, NULL,
398                                 NULL, NULL);
399
400     if(!pScrn) return FALSE;
401
402     pScrn->driverVersion    = NV_VERSION;
403     pScrn->driverName       = NV_DRIVER_NAME;
404     pScrn->name             = NV_NAME;
405
406     pScrn->Probe = NVProbe;
407     pScrn->PreInit          = NVPreInit;
408     pScrn->ScreenInit       = NVScreenInit;
409     pScrn->SwitchMode       = NVSwitchMode;
410     pScrn->AdjustFrame      = NVAdjustFrame;
411     pScrn->EnterVT          = NVEnterVT;
412     pScrn->LeaveVT          = NVLeaveVT;
413     pScrn->FreeScreen       = NVFreeScreen;
414     pScrn->ValidMode        = NVValidMode;
415
416     return TRUE;
417 }
418 #endif
419
420 /* This returns architecture in hexdecimal, so NV40 is 0x40 */
421 static int NVGetArchitecture (volatile CARD32 *regs)
422 {
423         int architecture = 0;
424
425         /* We're dealing with >=NV10 */
426         if ((regs[0] & 0x0f000000) > 0 ) {
427                 /* Bit 27-20 contain the architecture in hex */
428                 architecture = (regs[0] & 0xff00000) >> 20;
429         /* NV04 or NV05 */
430         } else if ((regs[0] & 0xff00fff0) == 0x20004000) {
431                 architecture = 0x04;
432         }
433
434         return architecture;
435 }
436
437 /* Reading the pci_id from the card registers is the most reliable way */
438 static CARD32 NVGetPCIID (volatile CARD32 *regs)
439 {
440         CARD32 pci_id;
441
442         int architecture = NVGetArchitecture(regs);
443
444         /* Dealing with an unknown or unsupported card */
445         if (architecture == 0) {
446                 return 0;
447         }
448
449         if (architecture >= 0x40)
450                 pci_id = regs[0x88000/4];
451         else
452                 pci_id = regs[0x1800/4];
453
454         /* A pci-id can be inverted, we must correct this */
455         if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA) {
456                 pci_id = (PCI_VENDOR_NVIDIA << 16) | (pci_id >> 16);
457         } else if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA_SGS) {
458                 pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | (pci_id >> 16);
459         /* Checking endian issues */
460         } else {
461                 /* PCI_VENDOR_NVIDIA = 0x10DE */
462                 if ((pci_id & (0xffff << 16)) == (0xDE10 << 16)) { /* wrong endian */
463                         pci_id = (PCI_VENDOR_NVIDIA << 16) | ((pci_id << 8) & 0x0000ff00) |
464                                 ((pci_id >> 8) & 0x000000ff);
465                 /* PCI_VENDOR_NVIDIA_SGS = 0x12D2 */
466                 } else if ((pci_id & (0xffff << 16)) == (0xD212 << 16)) { /* wrong endian */
467                         pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | ((pci_id << 8) & 0x0000ff00) |
468                                 ((pci_id >> 8) & 0x000000ff);
469                 }
470         }
471
472         return pci_id;
473 }
474
475 #ifdef XSERVER_LIBPCIACCESS
476
477 static Bool NVPciProbe (        DriverPtr               drv,
478                                 int                     entity_num,
479                                 struct pci_device       *dev,
480                                 intptr_t                match_data      )
481 {
482         ScrnInfoPtr pScrn = NULL;
483
484         volatile uint32_t *regs = NULL;
485
486         /* Temporary mapping to discover the architecture */
487         pci_device_map_range(dev, PCI_DEV_MEM_BASE(dev, 0), 0x90000, 0,
488                              (void *) &regs);
489
490         uint8_t architecture = NVGetArchitecture(regs);
491
492         CARD32 pci_id = NVGetPCIID(regs);
493
494         pci_device_unmap_range(dev, (void *) regs, 0x90000);
495
496         /* Currently NV04 up to NV83 is supported */
497         /* For safety the fictional NV8F is used */
498         if (architecture >= 0x04 && architecture <= 0x8F) {
499
500                 /* At this stage the pci_id should be ok, so we generate this
501                  * to avoid list duplication */
502                 /* AGP bridge chips need their bridge chip id to be detected */
503                 PciChipsets NVChipsets[] = {
504                         { pci_id, PCI_DEV_PCI_ID(dev), RES_SHARED_VGA },
505                         { -1, -1, RES_UNDEFINED }
506                 };
507
508                 pScrn = xf86ConfigPciEntity(pScrn, 0, entity_num, NVChipsets, 
509                                                 NULL, NULL, NULL, NULL, NULL);
510
511                 if (pScrn != NULL) {
512                         pScrn->driverVersion    = NV_VERSION;
513                         pScrn->driverName       = NV_DRIVER_NAME;
514                         pScrn->name             = NV_NAME;
515
516                         pScrn->Probe            = NULL;
517                         pScrn->PreInit          = NVPreInit;
518                         pScrn->ScreenInit       = NVScreenInit;
519                         pScrn->SwitchMode       = NVSwitchMode;
520                         pScrn->AdjustFrame      = NVAdjustFrame;
521                         pScrn->EnterVT          = NVEnterVT;
522                         pScrn->LeaveVT          = NVLeaveVT;
523                         pScrn->FreeScreen       = NVFreeScreen;
524                         pScrn->ValidMode        = NVValidMode;
525
526                         return TRUE;
527                 }
528         }
529
530         return FALSE;
531 }
532
533 #endif /* XSERVER_LIBPCIACCESS */
534
535 #define MAX_CHIPS MAXSCREENS
536
537 #ifndef XSERVER_LIBPCIACCESS
538 /* Mandatory */
539 static Bool
540 NVProbe(DriverPtr drv, int flags)
541 {
542         int i;
543         GDevPtr *devSections;
544         int *usedChips;
545         SymTabRec NVChipsets[MAX_CHIPS + 1];
546         PciChipsets NVPciChipsets[MAX_CHIPS + 1];
547         pciVideoPtr *ppPci;
548         int numDevSections;
549         int numUsed;
550         Bool foundScreen = FALSE;
551
552         if ((numDevSections = xf86MatchDevice(NV_DRIVER_NAME, &devSections)) <= 0) 
553                 return FALSE;  /* no matching device section */
554
555         if (!(ppPci = xf86GetPciVideoInfo())) 
556                 return FALSE;  /* no PCI cards found */
557
558         numUsed = 0;
559
560         /* Create the NVChipsets and NVPciChipsets from found devices */
561         while (*ppPci && (numUsed < MAX_CHIPS)) {
562                 if (((*ppPci)->vendor == PCI_VENDOR_NVIDIA_SGS) || 
563                         ((*ppPci)->vendor == PCI_VENDOR_NVIDIA)) 
564                 {
565                         volatile CARD32 *regs;
566                         CARD32 pcicmd;
567
568                         PCI_DEV_READ_LONG(*ppPci, PCI_CMD_STAT_REG, &pcicmd);
569                         /* Enable reading memory? */
570                         PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
571
572                         regs = xf86MapPciMem(-1, VIDMEM_MMIO, PCI_DEV_TAG(*ppPci), PCI_DEV_MEM_BASE(*ppPci, 0), 0x90000);
573                         int pciid = NVGetPCIID(regs);
574
575                         int architecture = NVGetArchitecture(regs);
576                         char name[25];
577                         sprintf(name, "NVIDIA NV%02X", architecture);
578                         /* NV04 upto NV83 is supported, NV8F is fictive limit */
579                         if (architecture >= 0x04 && architecture <= 0x8F) {
580                                 NVChipsets[numUsed].token = pciid;
581                                 NVChipsets[numUsed].name = name;
582                                 NVPciChipsets[numUsed].numChipset = pciid;
583                                 /* AGP bridge chips need their bridge chip id to be detected */
584                                 NVPciChipsets[numUsed].PCIid = PCI_DEV_PCI_ID(*ppPci);
585                                 NVPciChipsets[numUsed].resList = RES_SHARED_VGA;
586                                 numUsed++;
587                         }
588                         xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
589
590                         /* Reset previous state */
591                         PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd);
592                 }
593                 ppPci++;
594         }
595
596         /* terminate the list */
597         NVChipsets[numUsed].token = -1;
598         NVChipsets[numUsed].name = NULL; 
599         NVPciChipsets[numUsed].numChipset = -1;
600         NVPciChipsets[numUsed].PCIid = -1;
601         NVPciChipsets[numUsed].resList = RES_UNDEFINED;
602
603         numUsed = xf86MatchPciInstances(NV_NAME, 0, NVChipsets, NVPciChipsets,
604                                         devSections, numDevSections, drv,
605                                         &usedChips);
606
607         if (numUsed <= 0) {
608                 return FALSE;
609         }
610
611         if (flags & PROBE_DETECT) {
612                 foundScreen = TRUE;
613         } else {
614                 for (i = 0; i < numUsed; i++) {
615                         pciVideoPtr pPci;
616
617                         pPci = xf86GetPciInfoForEntity(usedChips[i]);
618                         if (NVGetScrnInfoRec(NVPciChipsets, usedChips[i])) {
619                                 foundScreen = TRUE;
620                         }
621                 }
622         }
623
624         xfree(devSections);
625         xfree(usedChips);
626
627         return foundScreen;
628 }
629 #endif /* XSERVER_LIBPCIACCESS */
630
631 Bool
632 NVSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
633 {
634         ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
635         NVPtr pNv = NVPTR(pScrn);
636
637         if (pNv->randr12_enable) {
638                 /* No rotation support for the moment */
639                 return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
640         }
641
642         return NVModeInit(xf86Screens[scrnIndex], mode);
643 }
644
645 /*
646  * This function is used to initialize the Start Address - the first
647  * displayed location in the video memory.
648  */
649 /* Usually mandatory */
650 void 
651 NVAdjustFrame(int scrnIndex, int x, int y, int flags)
652 {
653         ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
654         NVPtr pNv = NVPTR(pScrn);
655
656         if (pNv->randr12_enable) {
657                 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
658                 xf86CrtcPtr crtc = config->output[config->compat_output]->crtc;
659
660                 if (crtc && crtc->enabled) {
661                         NVCrtcSetBase(crtc, x, y, FALSE);
662                 }
663         } else {
664                 int startAddr;
665                 NVFBLayout *pLayout = &pNv->CurrentLayout;
666                 startAddr = (((y*pLayout->displayWidth)+x)*(pLayout->bitsPerPixel/8));
667                 startAddr += pNv->FB->offset;
668                 NVSetStartAddress(pNv, startAddr);
669         }
670 }
671
672 static Bool
673 NV50AcquireDisplay(ScrnInfoPtr pScrn)
674 {
675         if (!NV50DispInit(pScrn))
676                 return FALSE;
677         if (!NV50CursorAcquire(pScrn))
678                 return FALSE;
679         xf86SetDesiredModes(pScrn);
680
681         return TRUE;
682 }
683
684 static Bool
685 NV50ReleaseDisplay(ScrnInfoPtr pScrn)
686 {
687         NV50CursorRelease(pScrn);
688         NV50DispShutdown(pScrn);
689         return TRUE;
690 }
691
692 /*
693  * This is called when VT switching back to the X server.  Its job is
694  * to reinitialise the video mode.
695  *
696  * We may wish to unmap video/MMIO memory too.
697  */
698
699 /* Mandatory */
700 static Bool
701 NVEnterVT(int scrnIndex, int flags)
702 {
703         ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
704         NVPtr pNv = NVPTR(pScrn);
705
706         if (pNv->randr12_enable) {
707                 ErrorF("NVEnterVT is called\n");
708                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
709                 int i;
710                 pScrn->vtSema = TRUE;
711
712                 /* bit28: transmit text mode on both heads? */
713                 if (pNv->twoHeads) {
714                         uint32_t debug1 = nvReadMC(pNv, NV_PBUS_DEBUG_1);
715                         nvWriteMC(pNv, NV_PBUS_DEBUG_1, debug1 & ~(1 << 28));
716                 }
717
718                 if (pNv->Architecture == NV_ARCH_50) {
719                         if (!NV50AcquireDisplay(pScrn))
720                                 return FALSE;
721                         return TRUE;
722                 }
723
724                 /* Save the current state */
725                 if (pNv->SaveGeneration != serverGeneration) {
726                         pNv->SaveGeneration = serverGeneration;
727                         NVSave(pScrn);
728                 }
729
730                 for (i = 0; i < xf86_config->num_crtc; i++) {
731                         NVCrtcLockUnlock(xf86_config->crtc[i], 0);
732                 }
733
734                 /* Reassign outputs so disabled outputs don't get stuck on the wrong crtc */
735                 for (i = 0; i < xf86_config->num_output; i++) {
736                         xf86OutputPtr output = xf86_config->output[i];
737                         NVOutputPrivatePtr nv_output = output->driver_private;
738                         if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS) {
739                                 uint8_t tmds_reg4;
740
741                                 /* Disable any crosswired tmds, to avoid picking up a signal on a disabled output */
742                                 /* Example: TMDS1 crosswired to CRTC0 (by bios) reassigned to CRTC1 in xorg, disabled. */
743                                 /* But the bios reinits it to CRTC0 when going back to VT. */
744                                 /* Because it's disabled, it doesn't get a mode set, still it picks up the signal from CRTC0 (which is another output) */
745                                 /* A legitimately crosswired output will get set properly during mode set */
746                                 if ((tmds_reg4 = NVReadTMDS(pNv, nv_output->preferred_output, 0x4)) & (1 << 3)) {
747                                         NVWriteTMDS(pNv, nv_output->preferred_output, 0x4, tmds_reg4 & ~(1 << 3));
748                                 }
749                         }
750                 }
751
752                 if (!xf86SetDesiredModes(pScrn))
753                         return FALSE;
754         } else {
755                 if (!NVModeInit(pScrn, pScrn->currentMode))
756                         return FALSE;
757
758                 NVAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
759         }
760
761         if (pNv->overlayAdaptor && pNv->Architecture != NV_ARCH_04)
762                 NV10WriteOverlayParameters(pScrn);
763         return TRUE;
764 }
765
766 /*
767  * This is called when VT switching away from the X server.  Its job is
768  * to restore the previous (text) mode.
769  *
770  * We may wish to remap video/MMIO memory too.
771  */
772
773 /* Mandatory */
774 static void
775 NVLeaveVT(int scrnIndex, int flags)
776 {
777         ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
778         NVPtr pNv = NVPTR(pScrn);
779         if (pNv->randr12_enable)
780                 ErrorF("NVLeaveVT is called\n");
781
782         if (pNv->Architecture == NV_ARCH_50) {
783                 NV50ReleaseDisplay(pScrn);
784                 return;
785         }
786         NVSync(pScrn);
787         NVRestore(pScrn);
788         if (!pNv->randr12_enable)
789                 NVLockUnlock(pNv, 1);
790 }
791
792
793
794 static void 
795 NVBlockHandler (
796     int i, 
797     pointer blockData, 
798     pointer pTimeout,
799     pointer pReadmask
800 )
801 {
802     ScreenPtr     pScreen = screenInfo.screens[i];
803     ScrnInfoPtr   pScrnInfo = xf86Screens[i];
804     NVPtr         pNv = NVPTR(pScrnInfo);
805
806     FIRE_RING();
807
808     pScreen->BlockHandler = pNv->BlockHandler;
809     (*pScreen->BlockHandler) (i, blockData, pTimeout, pReadmask);
810     pScreen->BlockHandler = NVBlockHandler;
811
812     if (pNv->VideoTimerCallback) 
813         (*pNv->VideoTimerCallback)(pScrnInfo, currentTime.milliseconds);
814
815 }
816
817
818 /*
819  * This is called at the end of each server generation.  It restores the
820  * original (text) mode.  It should also unmap the video memory, and free
821  * any per-generation data allocated by the driver.  It should finish
822  * by unwrapping and calling the saved CloseScreen function.
823  */
824
825 /* Mandatory */
826 static Bool
827 NVCloseScreen(int scrnIndex, ScreenPtr pScreen)
828 {
829         ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
830         NVPtr pNv = NVPTR(pScrn);
831
832         if (pScrn->vtSema) {
833                 pScrn->vtSema = FALSE;
834                 if (pNv->Architecture == NV_ARCH_50) {
835                         NV50ReleaseDisplay(pScrn);
836                 } else {
837                         if (pNv->randr12_enable)
838                                 ErrorF("NVCloseScreen is called\n");
839                         NVSync(pScrn);
840                         NVRestore(pScrn);
841                         if (!pNv->randr12_enable)
842                                 NVLockUnlock(pNv, 1);
843                 }
844         }
845
846         NVUnmapMem(pScrn);
847         vgaHWUnmapMem(pScrn);
848         if (pNv->CursorInfoRec)
849                 xf86DestroyCursorInfoRec(pNv->CursorInfoRec);
850         if (pNv->ShadowPtr)
851                 xfree(pNv->ShadowPtr);
852         if (pNv->overlayAdaptor)
853                 xfree(pNv->overlayAdaptor);
854         if (pNv->blitAdaptor)
855                 xfree(pNv->blitAdaptor);
856
857         pScrn->vtSema = FALSE;
858         pScreen->CloseScreen = pNv->CloseScreen;
859         pScreen->BlockHandler = pNv->BlockHandler;
860         return (*pScreen->CloseScreen)(scrnIndex, pScreen);
861 }
862
863 /* Free up any persistent data structures */
864
865 /* Optional */
866 static void
867 NVFreeScreen(int scrnIndex, int flags)
868 {
869     /*
870      * This only gets called when a screen is being deleted.  It does not
871      * get called routinely at the end of a server generation.
872      */
873     if (xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
874         vgaHWFreeHWRec(xf86Screens[scrnIndex]);
875     NVFreeRec(xf86Screens[scrnIndex]);
876 }
877
878
879 /* Checks if a mode is suitable for the selected chipset. */
880
881 /* Optional */
882 static ModeStatus
883 NVValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
884 {
885     NVPtr pNv = NVPTR(xf86Screens[scrnIndex]);
886
887     if(pNv->fpWidth && pNv->fpHeight)
888       if((pNv->fpWidth < mode->HDisplay) || (pNv->fpHeight < mode->VDisplay))
889         return (MODE_PANEL);
890
891     return (MODE_OK);
892 }
893
894 static void
895 nvProbeDDC(ScrnInfoPtr pScrn, int index)
896 {
897     vbeInfoPtr pVbe;
898
899     if (xf86LoadSubModule(pScrn, "vbe")) {
900         pVbe = VBEInit(NULL,index);
901         ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
902         vbeFree(pVbe);
903     }
904 }
905
906 Bool NVI2CInit(ScrnInfoPtr pScrn)
907 {
908         NVPtr pNv = NVPTR(pScrn);
909         char *mod = "i2c";
910
911         if (xf86LoadSubModule(pScrn, mod)) {
912                 xf86LoaderReqSymLists(i2cSymbols,NULL);
913
914                 mod = "ddc";
915                 if(xf86LoadSubModule(pScrn, mod)) {
916                         xf86LoaderReqSymLists(ddcSymbols, NULL);
917                         /* randr-1.2 clients have their DDC's initialized elsewhere */
918                         if (pNv->randr12_enable) {
919                                 return TRUE;
920                         } else {
921                                 return NVDACi2cInit(pScrn);
922                         }
923                 } 
924         }
925
926         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
927                 "Couldn't load %s module.  DDC probing can't be done\n", mod);
928
929         return FALSE;
930 }
931
932 static Bool NVPreInitDRI(ScrnInfoPtr pScrn)
933 {
934         NVPtr pNv = NVPTR(pScrn);
935
936         if (!NVDRIGetVersion(pScrn))
937                 return FALSE;
938
939         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
940                 "[dri] Found DRI library version %d.%d.%d and kernel"
941                 " module version %d.%d.%d\n",
942                 pNv->pLibDRMVersion->version_major,
943                 pNv->pLibDRMVersion->version_minor,
944                 pNv->pLibDRMVersion->version_patchlevel,
945                 pNv->pKernelDRMVersion->version_major,
946                 pNv->pKernelDRMVersion->version_minor,
947                 pNv->pKernelDRMVersion->version_patchlevel);
948
949         return TRUE;
950 }
951
952 static Bool
953 nv_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
954 {
955         ErrorF("nv_xf86crtc_resize is called with %dx%d resolution\n", width, height);
956         pScrn->virtualX = width;
957         pScrn->virtualY = height;
958         return TRUE;
959 }
960
961 static const xf86CrtcConfigFuncsRec nv_xf86crtc_config_funcs = {
962         nv_xf86crtc_resize
963 };
964
965 /* This is taken from the haiku driver */
966 /* We must accept crtc pitch constrains */
967 /* A hardware bug on some hardware requires twice the pitch */
968 static CARD8 NVGetCRTCMask(ScrnInfoPtr pScrn, CARD8 bpp)
969 {
970         CARD8 mask = 0;
971         switch(bpp) {
972                 case 8:
973                         mask = 0xf; /* 0x7 */
974                         break;
975                 case 15:
976                         mask = 0x7; /* 0x3 */
977                         break;
978                 case 16:
979                         mask = 0x7; /* 0x3 */
980                         break;
981                 case 24:
982                         mask = 0xf; /* 0x7 */
983                         break;
984                 case 32:
985                         mask = 0x3; /* 0x1 */
986                         break;
987                 default:
988                         ErrorF("Unkown color format\n");
989                         break;
990         }
991
992         return mask;
993 }
994
995 /* This is taken from the haiku driver */
996 static CARD8 NVGetAccelerationMask(ScrnInfoPtr pScrn, CARD8 bpp)
997 {
998         NVPtr pNv = NVPTR(pScrn);
999         CARD8 mask = 0;
1000         /* Identical for NV04 */
1001         if (pNv->Architecture == NV_ARCH_04) {
1002                 return NVGetCRTCMask(pScrn, bpp);
1003         } else {
1004                 switch(bpp) {
1005                         case 8:
1006                                 mask = 0x3f;
1007                                 break;
1008                         case 15:
1009                                 mask = 0x1f;
1010                                 break;
1011                         case 16:
1012                                 mask = 0x1f;
1013                                 break;
1014                         case 24:
1015                                 mask = 0x3f;
1016                                 break;
1017                         case 32:
1018                                 mask = 0x0f;
1019                                 break;
1020                         default:
1021                                 ErrorF("Unkown color format\n");
1022                                 break;
1023                 }
1024         }
1025
1026         return mask;
1027 }
1028
1029 static CARD32 NVGetVideoPitch(ScrnInfoPtr pScrn, CARD8 bpp)
1030 {
1031         NVPtr pNv = NVPTR(pScrn);
1032         CARD8 crtc_mask, accel_mask = 0;
1033         crtc_mask = NVGetCRTCMask(pScrn, bpp);
1034         if (!pNv->NoAccel) {
1035                 accel_mask = NVGetAccelerationMask(pScrn, bpp);
1036         }
1037
1038         /* adhere to the largest granularity imposed */
1039         if (accel_mask > crtc_mask) {
1040                 return (pScrn->virtualX + accel_mask) & ~accel_mask;
1041         } else {
1042                 return (pScrn->virtualX + crtc_mask) & ~crtc_mask;
1043         }
1044 }
1045
1046 #define NVPreInitFail(fmt, args...) do {                                    \
1047         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%d: "fmt, __LINE__, ##args); \
1048         if (pNv->pInt10)                                                    \
1049                 xf86FreeInt10(pNv->pInt10);                                 \
1050         NVFreeRec(pScrn);                                                   \
1051         return FALSE;                                                       \
1052 } while(0)
1053
1054 /* Mandatory */
1055 Bool
1056 NVPreInit(ScrnInfoPtr pScrn, int flags)
1057 {
1058         xf86CrtcConfigPtr xf86_config;
1059         NVPtr pNv;
1060         MessageType from;
1061         int i, max_width, max_height;
1062         ClockRangePtr clockRanges;
1063         const char *s;
1064         int config_mon_rates = FALSE;
1065         int num_crtc;
1066
1067         if (flags & PROBE_DETECT) {
1068                 EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1069
1070                 if (!pEnt)
1071                         return FALSE;
1072
1073                 i = pEnt->index;
1074                 xfree(pEnt);
1075
1076                 nvProbeDDC(pScrn, i);
1077                 return TRUE;
1078         }
1079
1080         /*
1081          * Note: This function is only called once at server startup, and
1082          * not at the start of each server generation.  This means that
1083          * only things that are persistent across server generations can
1084          * be initialised here.  xf86Screens[] is (pScrn is a pointer to one
1085          * of these).  Privates allocated using xf86AllocateScrnInfoPrivateIndex()  
1086          * are too, and should be used for data that must persist across
1087          * server generations.
1088          *
1089          * Per-generation data should be allocated with
1090          * AllocateScreenPrivateIndex() from the ScreenInit() function.
1091          */
1092
1093         /* Check the number of entities, and fail if it isn't one. */
1094         if (pScrn->numEntities != 1)
1095                 return FALSE;
1096
1097         /* Allocate the NVRec driverPrivate */
1098         if (!NVGetRec(pScrn)) {
1099                 return FALSE;
1100         }
1101         pNv = NVPTR(pScrn);
1102
1103         /* Get the entity, and make sure it is PCI. */
1104         pNv->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1105         if (pNv->pEnt->location.type != BUS_PCI)
1106                 return FALSE;
1107  
1108         /* Find the PCI info for this screen */
1109         pNv->PciInfo = xf86GetPciInfoForEntity(pNv->pEnt->index);
1110 #ifndef XSERVER_LIBPCIACCESS
1111         pNv->PciTag = pciTag(pNv->PciInfo->bus, pNv->PciInfo->device,
1112                                 pNv->PciInfo->func);
1113 #endif /* XSERVER_LIBPCIACCESS */
1114
1115         pNv->Primary = xf86IsPrimaryPci(pNv->PciInfo);
1116
1117         /* Initialize the card through int10 interface if needed */
1118         if (xf86LoadSubModule(pScrn, "int10")) {
1119                 xf86LoaderReqSymLists(int10Symbols, NULL);
1120 #if !defined(__alpha__) && !defined(__powerpc__)
1121                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initializing int10\n");
1122                 pNv->pInt10 = xf86InitInt10(pNv->pEnt->index);
1123 #endif
1124         }
1125
1126         xf86SetOperatingState(resVgaIo, pNv->pEnt->index, ResUnusedOpr);
1127         xf86SetOperatingState(resVgaMem, pNv->pEnt->index, ResDisableOpr);
1128
1129         /* Set pScrn->monitor */
1130         pScrn->monitor = pScrn->confScreen->monitor;
1131
1132         volatile uint32_t *regs = NULL;
1133 #ifdef XSERVER_LIBPCIACCESS
1134         pci_device_map_range(pNv->PciInfo, PCI_DEV_MEM_BASE(pNv->PciInfo, 0),
1135                              0x90000, 0, (void *)&regs);
1136         pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1137         pNv->NVArch = NVGetArchitecture(regs);
1138         pci_device_unmap_range(pNv->PciInfo, (void *) regs, 0x90000);
1139 #else
1140         CARD32 pcicmd;
1141         PCI_DEV_READ_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, &pcicmd);
1142         /* Enable reading memory? */
1143         PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
1144         regs = xf86MapPciMem(-1, VIDMEM_MMIO, pNv->PciTag, PCI_DEV_MEM_BASE(pNv->PciInfo, 0), 0x90000);
1145         pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1146         pNv->NVArch = NVGetArchitecture(regs);
1147         xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
1148         /* Reset previous state */
1149         PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd);
1150 #endif /* XSERVER_LIBPCIACCESS */
1151
1152         pScrn->chipset = malloc(sizeof(char) * 25);
1153         sprintf(pScrn->chipset, "NVIDIA NV%02X", pNv->NVArch);
1154
1155         if(!pScrn->chipset) {
1156                 pScrn->chipset = "Unknown NVIDIA";
1157         }
1158
1159         /*
1160         * This shouldn't happen because such problems should be caught in
1161         * NVProbe(), but check it just in case.
1162         */
1163         if (pScrn->chipset == NULL)
1164                 NVPreInitFail("ChipID 0x%04X is not recognised\n", pNv->Chipset);
1165
1166         if (pNv->NVArch < 0x04)
1167                 NVPreInitFail("Chipset \"%s\" is not recognised\n", pScrn->chipset);
1168
1169         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Chipset: \"%s\"\n", pScrn->chipset);
1170
1171         /* The highest architecture currently supported is NV5x */
1172         if (pNv->NVArch >= 0x80) {
1173                 pNv->Architecture =  NV_ARCH_50;
1174         } else if (pNv->NVArch >= 0x60) {
1175                 pNv->Architecture =  NV_ARCH_40;
1176         } else if (pNv->NVArch >= 0x50) {
1177                 pNv->Architecture =  NV_ARCH_50;
1178         } else if (pNv->NVArch >= 0x40) {
1179                 pNv->Architecture =  NV_ARCH_40;
1180         } else if (pNv->NVArch >= 0x30) {
1181                 pNv->Architecture = NV_ARCH_30;
1182         } else if (pNv->NVArch >= 0x20) {
1183                 pNv->Architecture = NV_ARCH_20;
1184         } else if (pNv->NVArch >= 0x10) {
1185                 pNv->Architecture = NV_ARCH_10;
1186         } else if (pNv->NVArch >= 0x04) {
1187                 pNv->Architecture = NV_ARCH_04;
1188         /*  The lowest architecture currently supported is NV04 */
1189         } else {
1190                 return FALSE;
1191         }
1192
1193         /*
1194          * The first thing we should figure out is the depth, bpp, etc.
1195          */
1196
1197         if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support32bppFb)) {
1198                 NVPreInitFail("\n");
1199         } else {
1200                 /* Check that the returned depth is one we support */
1201                 switch (pScrn->depth) {
1202                         case 8:
1203                         case 15:
1204                         case 16:
1205                         case 24:
1206                                 /* OK */
1207                                 break;
1208                         default:
1209                                 NVPreInitFail("Given depth (%d) is not supported by this driver\n",
1210                                         pScrn->depth);
1211                 }
1212         }
1213         xf86PrintDepthBpp(pScrn);
1214
1215         /* Get the depth24 pixmap format */
1216         if (pScrn->depth == 24 && pix24bpp == 0)
1217                 pix24bpp = xf86GetBppFromDepth(pScrn, 24);
1218
1219         /*
1220          * This must happen after pScrn->display has been set because
1221          * xf86SetWeight references it.
1222          */
1223         if (pScrn->depth > 8) {
1224                 /* The defaults are OK for us */
1225                 rgb zeros = {0, 0, 0};
1226
1227                 if (!xf86SetWeight(pScrn, zeros, zeros)) {
1228                         NVPreInitFail("\n");
1229                 }
1230         }
1231
1232         if (!xf86SetDefaultVisual(pScrn, -1)) {
1233                 NVPreInitFail("\n");
1234         } else {
1235                 /* We don't currently support DirectColor at > 8bpp */
1236                 if (pScrn->depth > 8 && (pScrn->defaultVisual != TrueColor)) {
1237                         NVPreInitFail("Given default visual"
1238                                 " (%s) is not supported at depth %d\n",
1239                                 xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
1240                 }
1241         }
1242
1243         /* The vgahw module should be loaded here when needed */
1244         if (!xf86LoadSubModule(pScrn, "vgahw")) {
1245                 NVPreInitFail("\n");
1246         }
1247
1248         xf86LoaderReqSymLists(vgahwSymbols, NULL);
1249
1250         /*
1251          * Allocate a vgaHWRec
1252          */
1253         if (!vgaHWGetHWRec(pScrn)) {
1254                 NVPreInitFail("\n");
1255         }
1256
1257         /* We use a programmable clock */
1258         pScrn->progClock = TRUE;
1259
1260         /* Collect all of the relevant option flags (fill in pScrn->options) */
1261         xf86CollectOptions(pScrn, NULL);
1262
1263         /* Process the options */
1264         if (!(pNv->Options = xalloc(sizeof(NVOptions))))
1265                 return FALSE;
1266         memcpy(pNv->Options, NVOptions, sizeof(NVOptions));
1267         xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pNv->Options);
1268
1269         /* Set the bits per RGB for 8bpp mode */
1270         if (pScrn->depth == 8)
1271                 pScrn->rgbBits = 8;
1272
1273         from = X_DEFAULT;
1274
1275         pNv->new_restore = FALSE;
1276
1277         if (pNv->Architecture == NV_ARCH_50) {
1278                 pNv->randr12_enable = TRUE;
1279         } else {
1280                 pNv->randr12_enable = FALSE;
1281                 if (xf86ReturnOptValBool(pNv->Options, OPTION_RANDR12, FALSE)) {
1282                         pNv->randr12_enable = TRUE;
1283                 }
1284         }
1285         xf86DrvMsg(pScrn->scrnIndex, from, "Randr1.2 support %sabled\n", pNv->randr12_enable ? "en" : "dis");
1286
1287         if (pNv->randr12_enable) {
1288                 if (xf86ReturnOptValBool(pNv->Options, OPTION_NEW_RESTORE, FALSE)) {
1289                         pNv->new_restore = TRUE;
1290                 }
1291                 xf86DrvMsg(pScrn->scrnIndex, from, "New (experimental) restore support %sabled\n", pNv->new_restore ? "en" : "dis");
1292         }
1293
1294         pNv->HWCursor = TRUE;
1295         /*
1296          * The preferred method is to use the "hw cursor" option as a tri-state
1297          * option, with the default set above.
1298          */
1299         if (xf86GetOptValBool(pNv->Options, OPTION_HW_CURSOR, &pNv->HWCursor)) {
1300                 from = X_CONFIG;
1301         }
1302         /* For compatibility, accept this too (as an override) */
1303         if (xf86ReturnOptValBool(pNv->Options, OPTION_SW_CURSOR, FALSE)) {
1304                 from = X_CONFIG;
1305                 pNv->HWCursor = FALSE;
1306         }
1307         xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
1308                 pNv->HWCursor ? "HW" : "SW");
1309
1310         pNv->FpScale = TRUE;
1311
1312         if (xf86GetOptValBool(pNv->Options, OPTION_FP_SCALE, &pNv->FpScale)) {
1313                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Flat panel scaling %s\n",
1314                         pNv->FpScale ? "on" : "off");
1315         }
1316         if (xf86ReturnOptValBool(pNv->Options, OPTION_NOACCEL, FALSE)) {
1317                 pNv->NoAccel = TRUE;
1318                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
1319         }
1320         if (xf86ReturnOptValBool(pNv->Options, OPTION_SHADOW_FB, FALSE)) {
1321                 pNv->ShadowFB = TRUE;
1322                 pNv->NoAccel = TRUE;
1323                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1324                         "Using \"Shadow Framebuffer\" - acceleration disabled\n");
1325         }
1326
1327         pNv->Rotate = 0;
1328         pNv->RandRRotation = FALSE;
1329         /*
1330          * Rotation with a randr-1.2 driver happens at a different level, so ignore these options.
1331          */
1332         if ((s = xf86GetOptValString(pNv->Options, OPTION_ROTATE)) && !pNv->randr12_enable) {
1333                 if(!xf86NameCmp(s, "CW")) {
1334                         pNv->ShadowFB = TRUE;
1335                         pNv->NoAccel = TRUE;
1336                         pNv->HWCursor = FALSE;
1337                         pNv->Rotate = 1;
1338                         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1339                                 "Rotating screen clockwise - acceleration disabled\n");
1340                 } else if(!xf86NameCmp(s, "CCW")) {
1341                         pNv->ShadowFB = TRUE;
1342                         pNv->NoAccel = TRUE;
1343                         pNv->HWCursor = FALSE;
1344                         pNv->Rotate = -1;
1345                         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1346                                 "Rotating screen counter clockwise - acceleration disabled\n");
1347                 } else if(!xf86NameCmp(s, "RandR")) {
1348                         pNv->ShadowFB = TRUE;
1349                         pNv->NoAccel = TRUE;
1350                         pNv->HWCursor = FALSE;
1351                         pNv->RandRRotation = TRUE;
1352                         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1353                                 "Using RandR rotation - acceleration disabled\n");
1354                 } else {
1355                         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1356                                 "\"%s\" is not a valid value for Option \"Rotate\"\n", s);
1357                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, 
1358                                 "Valid options are \"CW\", \"CCW\", and \"RandR\"\n");
1359                 }
1360         }
1361
1362         if(xf86GetOptValInteger(pNv->Options, OPTION_VIDEO_KEY, &(pNv->videoKey))) {
1363                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "video key set to 0x%x\n",
1364                                         pNv->videoKey);
1365         } else {
1366                 pNv->videoKey =  (1 << pScrn->offset.red) | 
1367                                         (1 << pScrn->offset.green) |
1368                 (((pScrn->mask.blue >> pScrn->offset.blue) - 1) << pScrn->offset.blue);
1369         }
1370
1371         /* Things happen on a per output basis for a randr-1.2 driver. */
1372         if (xf86GetOptValBool(pNv->Options, OPTION_FLAT_PANEL, &(pNv->FlatPanel)) && !pNv->randr12_enable) {
1373                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "forcing %s usage\n",
1374                         pNv->FlatPanel ? "DFP" : "CRTC");
1375         } else {
1376                 pNv->FlatPanel = -1; /* autodetect later */
1377         }
1378
1379         pNv->FPDither = FALSE;
1380         if (xf86GetOptValBool(pNv->Options, OPTION_FP_DITHER, &(pNv->FPDither))) 
1381                 xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "enabling flat panel dither\n");
1382
1383         if (xf86GetOptValInteger(pNv->Options, OPTION_FP_TWEAK, 
1384                                                 &pNv->PanelTweak)) {
1385                 pNv->usePanelTweak = TRUE;
1386         } else {
1387                 pNv->usePanelTweak = FALSE;
1388         }
1389
1390         if (pNv->pEnt->device->MemBase != 0) {
1391                 /* Require that the config file value matches one of the PCI values. */
1392                 if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->MemBase)) {
1393                         NVPreInitFail(
1394                                 "MemBase 0x%08lX doesn't match any PCI base register.\n",
1395                                 pNv->pEnt->device->MemBase);
1396                 }
1397                 pNv->VRAMPhysical = pNv->pEnt->device->MemBase;
1398                 from = X_CONFIG;
1399         } else {
1400                 if (PCI_DEV_MEM_BASE(pNv->PciInfo, 1) != 0) {
1401                         pNv->VRAMPhysical = PCI_DEV_MEM_BASE(pNv->PciInfo, 1) & 0xff800000;
1402                         from = X_PROBED;
1403                 } else {
1404                         NVPreInitFail("No valid FB address in PCI config space\n");
1405                         return FALSE;
1406                 }
1407         }
1408         xf86DrvMsg(pScrn->scrnIndex, from, "Linear framebuffer at 0x%lX\n",
1409                 (unsigned long)pNv->VRAMPhysical);
1410
1411         if (pNv->pEnt->device->IOBase != 0) {
1412                 /* Require that the config file value matches one of the PCI values. */
1413                 if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->IOBase)) {
1414                         NVPreInitFail("IOBase 0x%08lX doesn't match any PCI base register.\n",
1415                                 pNv->pEnt->device->IOBase);
1416                 }
1417                 pNv->IOAddress = pNv->pEnt->device->IOBase;
1418                 from = X_CONFIG;
1419         } else {
1420                 if (PCI_DEV_MEM_BASE(pNv->PciInfo, 0) != 0) {
1421                         pNv->IOAddress = PCI_DEV_MEM_BASE(pNv->PciInfo, 0) & 0xffffc000;
1422                         from = X_PROBED;
1423                 } else {
1424                         NVPreInitFail("No valid MMIO address in PCI config space\n");
1425                 }
1426         }
1427         xf86DrvMsg(pScrn->scrnIndex, from, "MMIO registers at 0x%lX\n",
1428                 (unsigned long)pNv->IOAddress);
1429
1430         if (xf86RegisterResources(pNv->pEnt->index, NULL, ResExclusive)) {
1431                 NVPreInitFail("xf86RegisterResources() found resource conflicts\n");
1432         }
1433
1434         pNv->alphaCursor = (pNv->NVArch >= 0x11);
1435
1436         if(pNv->Architecture < NV_ARCH_10) {
1437                 max_width = (pScrn->bitsPerPixel > 16) ? 2032 : 2048;
1438                 max_height = 2048;
1439         } else {
1440                 max_width = (pScrn->bitsPerPixel > 16) ? 4080 : 4096;
1441                 max_height = 4096;
1442         }
1443
1444         if (pNv->randr12_enable) {
1445                 /* Allocate an xf86CrtcConfig */
1446                 xf86CrtcConfigInit(pScrn, &nv_xf86crtc_config_funcs);
1447                 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1448
1449                 xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
1450
1451                 /* Set this in case no output ever does. */
1452                 if (pNv->Architecture >= NV_ARCH_30) {
1453                         pNv->restricted_mode = FALSE;
1454                 } else { /* real flexibility starts at the NV3x cards */
1455                         pNv->restricted_mode = TRUE;
1456                 }
1457         }
1458
1459         if (NVPreInitDRI(pScrn) == FALSE) {
1460                 NVPreInitFail("\n");
1461         }
1462
1463         if (!pNv->randr12_enable) {
1464                 if ((pScrn->monitor->nHsync == 0) && 
1465                         (pScrn->monitor->nVrefresh == 0)) {
1466
1467                         config_mon_rates = FALSE;
1468                 } else {
1469                         config_mon_rates = TRUE;
1470                 }
1471         }
1472
1473         NVCommonSetup(pScrn);
1474
1475         if (pNv->randr12_enable) {
1476                 if (pNv->Architecture < NV_ARCH_50) {
1477                         NVI2CInit(pScrn);
1478
1479                         num_crtc = pNv->twoHeads ? 2 : 1;
1480                         for (i = 0; i < num_crtc; i++) {
1481                                 nv_crtc_init(pScrn, i);
1482                         }
1483
1484                         NvSetupOutputs(pScrn);
1485                 } else {
1486                         if (!NV50DispPreInit(pScrn))
1487                                 NVPreInitFail("\n");
1488                         if (!NV50CreateOutputs(pScrn))
1489                                 NVPreInitFail("\n");
1490                         NV50DispCreateCrtcs(pScrn);
1491                 }
1492
1493                 if (!xf86InitialConfiguration(pScrn, FALSE))
1494                         NVPreInitFail("No valid modes.\n");
1495         }
1496
1497         pScrn->videoRam = pNv->RamAmountKBytes;
1498         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "VideoRAM: %d kBytes\n",
1499                 pScrn->videoRam);
1500
1501         pNv->VRAMPhysicalSize = pScrn->videoRam * 1024;
1502
1503         /*
1504          * If the driver can do gamma correction, it should call xf86SetGamma()
1505          * here.
1506          */
1507
1508         {
1509                 Gamma zeros = {0.0, 0.0, 0.0};
1510
1511                 if (!xf86SetGamma(pScrn, zeros)) {
1512                         NVPreInitFail("\n");
1513                 }
1514         }
1515
1516         /*
1517          * Setup the ClockRanges, which describe what clock ranges are available,
1518          * and what sort of modes they can be used for.
1519          */
1520
1521         clockRanges = xnfcalloc(sizeof(ClockRange), 1);
1522         clockRanges->next = NULL;
1523         clockRanges->minClock = pNv->MinVClockFreqKHz;
1524         clockRanges->maxClock = pNv->MaxVClockFreqKHz;
1525         clockRanges->clockIndex = -1;           /* programmable */
1526         clockRanges->doubleScanAllowed = TRUE;
1527         if ((pNv->Architecture == NV_ARCH_20) ||
1528                 ((pNv->Architecture == NV_ARCH_10) && 
1529                 ((pNv->Chipset & 0x0ff0) != CHIPSET_NV10) &&
1530                 ((pNv->Chipset & 0x0ff0) != CHIPSET_NV15))) {
1531                 /* HW is broken */
1532                 clockRanges->interlaceAllowed = FALSE;
1533         } else {
1534                 clockRanges->interlaceAllowed = TRUE;
1535         }
1536
1537         if(pNv->FlatPanel == 1) {
1538                 clockRanges->interlaceAllowed = FALSE;
1539                 clockRanges->doubleScanAllowed = FALSE;
1540         }
1541
1542 #ifdef M_T_DRIVER
1543         /* If DFP, add a modeline corresponding to its panel size */
1544         if (pNv->FlatPanel && !pNv->Television && pNv->fpWidth && pNv->fpHeight) {
1545                 DisplayModePtr Mode;
1546
1547                 Mode = xnfcalloc(1, sizeof(DisplayModeRec));
1548                 Mode = xf86CVTMode(pNv->fpWidth, pNv->fpHeight, 60.00, TRUE, FALSE);
1549                 Mode->type = M_T_DRIVER;
1550                 pScrn->monitor->Modes = xf86ModesAdd(pScrn->monitor->Modes, Mode);
1551
1552                 if (!config_mon_rates) {
1553                         if (!Mode->HSync)
1554                                 Mode->HSync = ((float) Mode->Clock ) / ((float) Mode->HTotal);
1555                         if (!Mode->VRefresh)
1556                                 Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
1557                                                         ((float) (Mode->HTotal * Mode->VTotal));
1558
1559                         if (Mode->HSync < pScrn->monitor->hsync[0].lo)
1560                                 pScrn->monitor->hsync[0].lo = Mode->HSync;
1561                         if (Mode->HSync > pScrn->monitor->hsync[0].hi)
1562                                 pScrn->monitor->hsync[0].hi = Mode->HSync;
1563                         if (Mode->VRefresh < pScrn->monitor->vrefresh[0].lo)
1564                                 pScrn->monitor->vrefresh[0].lo = Mode->VRefresh;
1565                         if (Mode->VRefresh > pScrn->monitor->vrefresh[0].hi)
1566                                 pScrn->monitor->vrefresh[0].hi = Mode->VRefresh;
1567
1568                         pScrn->monitor->nHsync = 1;
1569                         pScrn->monitor->nVrefresh = 1;
1570                 }
1571         }
1572 #endif
1573
1574         if (pNv->randr12_enable) {
1575                 pScrn->displayWidth = NVGetVideoPitch(pScrn, pScrn->depth);
1576         } else {
1577                 /*
1578                  * xf86ValidateModes will check that the mode HTotal and VTotal values
1579                  * don't exceed the chipset's limit if pScrn->maxHValue and
1580                  * pScrn->maxVValue are set.  Since our NVValidMode() already takes
1581                  * care of this, we don't worry about setting them here.
1582                  */
1583                 i = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
1584                                         pScrn->display->modes, clockRanges,
1585                                         NULL, 256, max_width,
1586                                         512, 128, max_height,
1587                                         pScrn->display->virtualX,
1588                                         pScrn->display->virtualY,
1589                                         pNv->VRAMPhysicalSize / 2,
1590                                         LOOKUP_BEST_REFRESH);
1591
1592                 if (i == -1) {
1593                         NVPreInitFail("\n");
1594                 }
1595
1596                 /* Prune the modes marked as invalid */
1597                 xf86PruneDriverModes(pScrn);
1598
1599                 /*
1600                  * Set the CRTC parameters for all of the modes based on the type
1601                  * of mode, and the chipset's interlace requirements.
1602                  *
1603                  * Calling this is required if the mode->Crtc* values are used by the
1604                  * driver and if the driver doesn't provide code to set them.  They
1605                  * are not pre-initialised at all.
1606                  */
1607                 xf86SetCrtcForModes(pScrn, 0);
1608         }
1609
1610         if (pScrn->modes == NULL) {
1611                 NVPreInitFail("No valid modes found\n");
1612         }
1613
1614         /* Set the current mode to the first in the list */
1615         pScrn->currentMode = pScrn->modes;
1616
1617         /* Print the list of modes being used */
1618         xf86PrintModes(pScrn);
1619
1620         /* Set display resolution */
1621         xf86SetDpi(pScrn, 0, 0);
1622
1623
1624         /*
1625          * XXX This should be taken into account in some way in the mode valdation
1626          * section.
1627          */
1628
1629         if (xf86LoadSubModule(pScrn, "fb") == NULL) {
1630                 NVPreInitFail("\n");
1631         }
1632
1633         xf86LoaderReqSymLists(fbSymbols, NULL);
1634
1635         /* Load EXA if needed */
1636         if (!pNv->NoAccel) {
1637                 if (!xf86LoadSubModule(pScrn, "exa")) {
1638                         NVPreInitFail("\n");
1639                 }
1640                 xf86LoaderReqSymLists(exaSymbols, NULL);
1641         }
1642
1643         /* Load ramdac if needed */
1644         if (pNv->HWCursor) {
1645                 if (!xf86LoadSubModule(pScrn, "ramdac")) {
1646                         NVPreInitFail("\n");
1647                 }
1648                 xf86LoaderReqSymLists(ramdacSymbols, NULL);
1649         }
1650
1651         /* Load shadowfb if needed */
1652         if (pNv->ShadowFB) {
1653                 if (!xf86LoadSubModule(pScrn, "shadowfb")) {
1654                         NVPreInitFail("\n");
1655                 }
1656                 xf86LoaderReqSymLists(shadowSymbols, NULL);
1657         }
1658
1659         pNv->CurrentLayout.bitsPerPixel = pScrn->bitsPerPixel;
1660         pNv->CurrentLayout.depth = pScrn->depth;
1661         pNv->CurrentLayout.displayWidth = pScrn->displayWidth;
1662         pNv->CurrentLayout.weight.red = pScrn->weight.red;
1663         pNv->CurrentLayout.weight.green = pScrn->weight.green;
1664         pNv->CurrentLayout.weight.blue = pScrn->weight.blue;
1665         pNv->CurrentLayout.mode = pScrn->currentMode;
1666
1667         xf86FreeInt10(pNv->pInt10);
1668
1669         pNv->pInt10 = NULL;
1670         return TRUE;
1671 }
1672
1673
1674 /*
1675  * Map the framebuffer and MMIO memory.
1676  */
1677
1678 static Bool
1679 NVMapMem(ScrnInfoPtr pScrn)
1680 {
1681         NVPtr pNv = NVPTR(pScrn);
1682         int gart_scratch_size;
1683         uint64_t res;
1684
1685         nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_FB_SIZE, &res);
1686         pNv->VRAMSize=res;
1687         nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_FB_PHYSICAL, &res);
1688         pNv->VRAMPhysical=res;
1689         nouveau_device_get_param(pNv->dev, NOUVEAU_GETPARAM_AGP_SIZE, &res);
1690         pNv->AGPSize=res;
1691
1692 #if !NOUVEAU_EXA_PIXMAPS
1693         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
1694                 0, pNv->VRAMPhysicalSize / 2, &pNv->FB)) {
1695                         ErrorF("Failed to allocate memory for framebuffer!\n");
1696                         return FALSE;
1697         }
1698         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1699                 "Allocated %dMiB VRAM for framebuffer + offscreen pixmaps\n",
1700                 (unsigned int)(pNv->FB->size >> 20));
1701 #endif
1702
1703         if (pNv->AGPSize) {
1704                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1705                            "AGPGART: %dMiB available\n",
1706                            (unsigned int)(pNv->AGPSize >> 20));
1707                 if (pNv->AGPSize > (16*1024*1024))
1708                         gart_scratch_size = 16*1024*1024;
1709                 else
1710                         /* always leave 512kb for other things like the fifos */
1711                         gart_scratch_size = pNv->AGPSize - 512*1024;
1712         } else {
1713                 gart_scratch_size = (4 << 20) - (1 << 18) ;
1714                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1715                            "GART: PCI DMA - using %dKiB\n",
1716                            gart_scratch_size >> 10);
1717         }
1718
1719 #ifndef __powerpc__
1720         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_GART | NOUVEAU_BO_PIN, 0,
1721                            gart_scratch_size, &pNv->GART)) {
1722                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1723                            "Unable to allocate GART memory\n");
1724         }
1725 #endif
1726         if (pNv->GART) {
1727                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1728                            "GART: Allocated %dMiB as a scratch buffer\n",
1729                            (unsigned int)(pNv->GART->size >> 20));
1730         }
1731
1732         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN, 0,
1733                            64 * 1024, &pNv->Cursor)) {
1734                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1735                            "Failed to allocate memory for hardware cursor\n");
1736                 return FALSE;
1737         }
1738
1739         if (pNv->randr12_enable) {
1740                 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN, 0,
1741                         64 * 1024, &pNv->Cursor2)) {
1742                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1743                                 "Failed to allocate memory for hardware cursor\n");
1744                         return FALSE;
1745                 }
1746         }
1747
1748         if (pNv->Architecture >= NV_ARCH_50) {
1749                 if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
1750                                    0, 0x1000, &pNv->CLUT)) {
1751                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1752                                    "Failed to allocate memory for CLUT\n");
1753                         return FALSE;
1754                 }
1755         }
1756
1757         if ((pNv->FB && nouveau_bo_map(pNv->FB, NOUVEAU_BO_RDWR)) ||
1758             (pNv->GART && nouveau_bo_map(pNv->GART, NOUVEAU_BO_RDWR)) ||
1759             (pNv->CLUT && nouveau_bo_map(pNv->CLUT, NOUVEAU_BO_RDWR)) ||
1760             nouveau_bo_map(pNv->Cursor, NOUVEAU_BO_RDWR) ||
1761             (pNv->randr12_enable && nouveau_bo_map(pNv->Cursor2, NOUVEAU_BO_RDWR))) {
1762                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1763                            "Failed to map pinned buffers\n");
1764                 return FALSE;
1765         }
1766
1767         return TRUE;
1768 }
1769
1770 /*
1771  * Unmap the framebuffer and MMIO memory.
1772  */
1773
1774 static Bool
1775 NVUnmapMem(ScrnInfoPtr pScrn)
1776 {
1777         NVPtr pNv = NVPTR(pScrn);
1778
1779         nouveau_bo_del(&pNv->FB);
1780         nouveau_bo_del(&pNv->GART);
1781         nouveau_bo_del(&pNv->Cursor);
1782         if (pNv->randr12_enable) {
1783                 nouveau_bo_del(&pNv->Cursor2);
1784         }
1785         nouveau_bo_del(&pNv->CLUT);
1786
1787         return TRUE;
1788 }
1789
1790
1791 /*
1792  * Initialise a new mode. 
1793  */
1794
1795 static Bool
1796 NVModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
1797 {
1798     vgaHWPtr hwp = VGAHWPTR(pScrn);
1799     vgaRegPtr vgaReg;
1800     NVPtr pNv = NVPTR(pScrn);
1801     NVRegPtr nvReg;
1802
1803     /* Initialise the ModeReg values */
1804     if (!vgaHWInit(pScrn, mode))
1805         return FALSE;
1806     pScrn->vtSema = TRUE;
1807
1808     vgaReg = &hwp->ModeReg;
1809     nvReg = &pNv->ModeReg;
1810
1811     if(!NVDACInit(pScrn, mode))
1812         return FALSE;
1813
1814     NVLockUnlock(pNv, 0);
1815     if(pNv->twoHeads) {
1816         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, nvReg->crtcOwner);
1817         NVLockUnlock(pNv, 0);
1818     }
1819
1820     /* Program the registers */
1821     vgaHWProtect(pScrn, TRUE);
1822
1823     NVDACRestore(pScrn, vgaReg, nvReg, FALSE);
1824
1825 #if X_BYTE_ORDER == X_BIG_ENDIAN
1826     /* turn on LFB swapping */
1827     {
1828         unsigned char tmp;
1829
1830         tmp = nvReadVGA(pNv, NV_VGA_CRTCX_SWAPPING);
1831         tmp |= (1 << 7);
1832         nvWriteVGA(pNv, NV_VGA_CRTCX_SWAPPING, tmp);
1833     }
1834 #endif
1835
1836     if (!pNv->NoAccel)
1837             NVResetGraphics(pScrn);
1838
1839     vgaHWProtect(pScrn, FALSE);
1840
1841     pNv->CurrentLayout.mode = mode;
1842
1843     return TRUE;
1844 }
1845
1846 #define NV_MODE_PRIVATE_ID 0x4F37ED65
1847 #define NV_MODE_PRIVATE_SIZE 2
1848
1849 /* 
1850  * Match a private mode flag in a special function.
1851  * I don't want ugly casting all over the code.
1852  */
1853 Bool
1854 NVMatchModePrivate(DisplayModePtr mode, uint32_t flags)
1855 {
1856         if (!mode)
1857                 return FALSE;
1858         if (!mode->Private)
1859                 return FALSE;
1860         if (mode->PrivSize != NV_MODE_PRIVATE_SIZE)
1861                 return FALSE;
1862         if (mode->Private[0] != NV_MODE_PRIVATE_ID)
1863                 return FALSE;
1864
1865         if (mode->Private[1] & flags)
1866                 return TRUE;
1867
1868         return FALSE;
1869 }
1870
1871 static void
1872 NVRestoreConsole(xf86OutputPtr output, DisplayModePtr mode)
1873 {
1874         if (!output->crtc)
1875                 return;
1876
1877         xf86CrtcPtr crtc = output->crtc;
1878         Bool need_unlock;
1879
1880         if (!crtc->enabled)
1881                 return;
1882
1883         xf86SetModeCrtc(mode, INTERLACE_HALVE_V);
1884         DisplayModePtr adjusted_mode = xf86DuplicateMode(mode);
1885
1886         /* Sequence mimics a normal modeset. */
1887         output->funcs->dpms(output, DPMSModeOff);
1888         crtc->funcs->dpms(crtc, DPMSModeOff);
1889         need_unlock = crtc->funcs->lock(crtc);
1890         output->funcs->mode_fixup(output, mode, adjusted_mode);
1891         crtc->funcs->mode_fixup(crtc, mode, adjusted_mode);
1892         output->funcs->prepare(output);
1893         crtc->funcs->prepare(crtc);
1894         /* Always use offset (0,0). */
1895         crtc->funcs->mode_set(crtc, mode, adjusted_mode, 0, 0);
1896         output->funcs->mode_set(output, mode, adjusted_mode);
1897         crtc->funcs->commit(crtc);
1898         output->funcs->commit(output);
1899         if (need_unlock)
1900                 crtc->funcs->unlock(crtc);
1901         /* Always turn on outputs afterwards. */
1902         output->funcs->dpms(output, DPMSModeOn);
1903         crtc->funcs->dpms(crtc, DPMSModeOn);
1904
1905         /* Free mode. */
1906         xfree(adjusted_mode);
1907 }
1908
1909 #define MODEPREFIX(name) NULL, NULL, name, 0,M_T_DRIVER
1910 #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
1911
1912 /* hblankstart: 648, hblankend: 792, vblankstart: 407, vblankend: 442 for 640x400 */
1913 static DisplayModeRec VGAModes[2] = {
1914         { MODEPREFIX("640x400"),    28320, /*25175,*/ 640,  680,  776,  800, 0,  400,  412,  414,  449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 640x400 */
1915         { MODEPREFIX("720x400"),    28320,  720,  738,  846,  900, 0,  400,  412,  414,  449, 0, V_NHSYNC | V_PVSYNC, MODESUFFIX }, /* 720x400@70Hz */
1916 };
1917
1918 /*
1919  * Restore the initial (text) mode.
1920  */
1921 static void 
1922 NVRestore(ScrnInfoPtr pScrn)
1923 {
1924         vgaHWPtr hwp = VGAHWPTR(pScrn);
1925         vgaRegPtr vgaReg = &hwp->SavedReg;
1926         NVPtr pNv = NVPTR(pScrn);
1927         NVRegPtr nvReg = &pNv->SavedReg;
1928
1929         if (pNv->randr12_enable) {
1930                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1931                 RIVA_HW_STATE *state = &pNv->ModeReg;
1932                 int i;
1933
1934                 /* Let's wipe some state regs */
1935                 state->vpll1_a = 0;
1936                 state->vpll1_b = 0;
1937                 state->vpll2_a = 0;
1938                 state->vpll2_b = 0;
1939                 state->reg594 = 0;
1940                 state->reg580 = 0;
1941                 state->pllsel = 0;
1942                 state->sel_clk = 0;
1943                 state->crosswired = FALSE;
1944
1945                 if (pNv->new_restore) { /* new style restore. */
1946                         for (i = 0; i < xf86_config->num_crtc; i++) {
1947                                 NVCrtcLockUnlock(xf86_config->crtc[i], 0);
1948                         }
1949
1950                         /* Reset some values according to stored console value, to avoid confusion later on. */
1951                         /* Otherwise we end up with corrupted terminals. */
1952                         for (i = 0; i < xf86_config->num_crtc; i++) {
1953                                 NVCrtcPrivatePtr nv_crtc = xf86_config->crtc[i]->driver_private;
1954                                 RIVA_HW_STATE *state = &pNv->SavedReg;
1955                                 NVCrtcRegPtr savep = &state->crtc_reg[nv_crtc->head];
1956                                 uint8_t pixelDepth = pNv->console_mode[nv_crtc->head].depth/8;
1957                                 /* restore PIXEL value */
1958                                 uint32_t pixel = NVReadVgaCrtc(xf86_config->crtc[i], NV_VGA_CRTCX_PIXEL) & ~(0xF);
1959                                 pixel |= (pixelDepth > 2) ? 3 : pixelDepth;
1960                                 NVWriteVgaCrtc(xf86_config->crtc[i], NV_VGA_CRTCX_PIXEL, pixel);
1961                                 /* restore HDisplay and VDisplay */
1962                                 NVWriteVgaCrtc(xf86_config->crtc[i], NV_VGA_CRTCX_HDISPE, (pNv->console_mode[nv_crtc->head].x_res)/8 - 1);
1963                                 NVWriteVgaCrtc(xf86_config->crtc[i], NV_VGA_CRTCX_VDISPE, (pNv->console_mode[nv_crtc->head].y_res) - 1);
1964                                 /* restore CR52 */
1965                                 NVWriteVgaCrtc(xf86_config->crtc[i], NV_VGA_CRTCX_52, pNv->misc_info.crtc_reg_52[nv_crtc->head]);
1966                                 /* restore crtc base */
1967                                 NVCrtcWriteCRTC(xf86_config->crtc[i], NV_CRTC_START, pNv->console_mode[nv_crtc->head].fb_start);
1968                                 /* Restore general control */
1969                                 NVCrtcWriteRAMDAC(xf86_config->crtc[i], NV_RAMDAC_GENERAL_CONTROL, pNv->misc_info.ramdac_general_control[nv_crtc->head]);
1970                                 /* Restore CR5758 */
1971                                 if (pNv->NVArch >= 0x17 && pNv->twoHeads)
1972                                         for (i = 0; i < 0x10; i++)
1973                                                 NVWriteVGACR5758(pNv, nv_crtc->head, i, savep->CR58[i]);
1974                         }
1975
1976                         /* Restore outputs when enabled. */
1977                         for (i = 0; i < xf86_config->num_output; i++) {
1978                                 xf86OutputPtr output = xf86_config->output[i];
1979                                 if (!xf86_config->output[i]->crtc) /* not enabled? */
1980                                         continue;
1981
1982                                 NVOutputPrivatePtr nv_output = output->driver_private;
1983                                 Bool is_fp = FALSE;
1984                                 DisplayModePtr mode = NULL;
1985                                 DisplayModePtr good_mode = NULL;
1986                                 NVConsoleMode *console = &pNv->console_mode[i];
1987                                 DisplayModePtr modes = output->probed_modes;
1988                                 if (!modes) /* no modes means no restore */
1989                                         continue;
1990
1991                                 if (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)
1992                                         is_fp = TRUE;
1993
1994                                 if (console->vga_mode) {
1995                                         /* We support 640x400 and 720x400 vga modes. */
1996                                         if (console->x_res == 720)
1997                                                 good_mode = &VGAModes[1];
1998                                         else
1999                                                 good_mode = &VGAModes[0];
2000                                         if (!good_mode) /* No suitable mode found. */
2001                                                 continue;
2002                                 } else {
2003                                         NVCrtcPrivatePtr nv_crtc = output->crtc->driver_private;
2004                                         uint32_t old_clock = nv_get_clock_from_crtc(pScrn, &pNv->SavedReg, nv_crtc->head);
2005                                         uint32_t clock_diff = 0xFFFFFFFF;
2006                                         for (mode = modes; mode != NULL; mode = mode->next) {
2007                                                 /* We only have the first 8 bits of y_res - 1. */
2008                                                 /* And it's sometimes bogus. */
2009                                                 if (is_fp || !console->enabled) { /* digital outputs are run at their native clock */
2010                                                         if (mode->HDisplay == console->x_res) {
2011                                                                 if (!good_mode) /* Pick any match, in case we don't find a 60.0 Hz mode. */
2012                                                                         good_mode = mode;
2013                                                                 /* Pick a 60.0 Hz mode if there is one. */
2014                                                                 if (mode->VRefresh > 59.95 && mode->VRefresh < 60.05) {
2015                                                                         good_mode = mode;
2016                                                                         break;
2017                                                                 }
2018                                                         }
2019                                                 } else {
2020                                                         if (mode->HDisplay == console->x_res) {
2021                                                                 int temp_diff = mode->Clock - old_clock;
2022                                                                 if (temp_diff < 0)
2023                                                                         temp_diff *= -1;
2024                                                                 if (temp_diff < clock_diff) { /* converge on the closest mode */
2025                                                                         clock_diff = temp_diff;
2026                                                                         good_mode = mode;
2027                                                                 }
2028                                                         }
2029                                                 }
2030                                         }
2031                                         if (!good_mode) /* No suitable mode found. */
2032                                                 continue;
2033                                 }
2034
2035                                 mode = xf86DuplicateMode(good_mode);
2036
2037                                 INT32 *nv_mode = xnfcalloc(sizeof(INT32)*NV_MODE_PRIVATE_SIZE, 1);
2038
2039                                 /* A semi-unique identifier to avoid using other privates. */
2040                                 nv_mode[0] = NV_MODE_PRIVATE_ID;
2041
2042                                 if (console->vga_mode)
2043                                         nv_mode[1] |= NV_MODE_VGA;
2044
2045                                 nv_mode[1] |= NV_MODE_CONSOLE;
2046
2047                                 mode->Private = nv_mode;
2048                                 mode->PrivSize = NV_MODE_PRIVATE_SIZE;
2049
2050                                 uint8_t scale_backup = nv_output->scaling_mode;
2051                                 if (nv_output->type == OUTPUT_LVDS || nv_output->type == OUTPUT_TMDS)
2052                                         nv_output->scaling_mode = SCALE_FULLSCREEN;
2053
2054                                 NVRestoreConsole(output, mode);
2055
2056                                 /* Restore value, so we reenter X properly. */
2057                                 nv_output->scaling_mode = scale_backup;
2058
2059                                 xfree(mode->Private);
2060                                 xfree(mode);
2061                         }
2062
2063                         /* Force hide the cursor. */
2064                         for (i = 0; i < xf86_config->num_crtc; i++) {
2065                                 xf86_config->crtc[i]->funcs->hide_cursor(xf86_config->crtc[i]);
2066                         }
2067
2068                         /* Lock the crtc's. */
2069                         for (i = 0; i < xf86_config->num_crtc; i++) {
2070                                 NVCrtcLockUnlock(xf86_config->crtc[i], 1);
2071                         }
2072
2073                         /* Let's clean our slate once again, so we always rewrite vpll's upon returning to X. */
2074                         state->vpll1_a = 0;
2075                         state->vpll1_b = 0;
2076                         state->vpll2_a = 0;
2077                         state->vpll2_b = 0;
2078                         state->reg594 = 0;
2079                         state->reg580 = 0;
2080                         state->pllsel = 0;
2081                         state->sel_clk = 0;
2082                         state->crosswired = FALSE;
2083                 } else {
2084                         for (i = 0; i < xf86_config->num_crtc; i++) {
2085                                 NVCrtcLockUnlock(xf86_config->crtc[i], 0);
2086                         }
2087
2088                         /* Some aspects of an output needs to be restore before the crtc. */
2089                         /* In my case this has to do with the mode that i get at very low resolutions. */
2090                         /* If i do this at the end, it will not be restored properly */
2091                         for (i = 0; i < xf86_config->num_output; i++) {
2092                                 NVOutputPrivatePtr nv_output2 = xf86_config->output[i]->driver_private;
2093                                 NVOutputRegPtr regp = &nvReg->dac_reg[nv_output2->preferred_output];
2094                                 Bool crosswired = regp->TMDS[0x4] & (1 << 3);
2095                                 /* Let's guess the bios state ;-) */
2096                                 if (nv_output2->type == OUTPUT_TMDS)
2097                                         ErrorF("Restoring TMDS timings, before restoring anything else\n");
2098                                 if (nv_output2->type == OUTPUT_LVDS)
2099                                         ErrorF("Restoring LVDS timings, before restoring anything else\n");
2100                                 if (nv_output2->type == OUTPUT_TMDS || nv_output2->type == OUTPUT_LVDS) {
2101                                         uint32_t clock = nv_calc_tmds_clock_from_pll(xf86_config->output[i]);
2102                                         nv_set_tmds_registers(xf86_config->output[i], clock, TRUE, crosswired);
2103                                 }
2104                         }
2105
2106                         /* This needs to happen before the crtc restore happens. */
2107                         for (i = 0; i < xf86_config->num_output; i++) {
2108                                 NVOutputPrivatePtr nv_output = xf86_config->output[i]->driver_private;
2109                                 /* Select the default output resource for consistent restore. */
2110                                 if (ffs(pNv->dcb_table.entry[nv_output->dcb_entry].or) & OUTPUT_1) {
2111                                         nv_output->output_resource = 1;
2112                                 } else {
2113                                         nv_output->output_resource = 0;
2114                                 }
2115                         }
2116
2117                         for (i = 0; i < xf86_config->num_crtc; i++) {
2118                                 NVCrtcPrivatePtr nv_crtc = xf86_config->crtc[i]->driver_private;
2119                                 /* Restore this, so it doesn't mess with restore. */
2120                                 pNv->fp_regs_owner[nv_crtc->head] = nv_crtc->head;
2121                         }
2122
2123                         for (i = 0; i < xf86_config->num_crtc; i++) {
2124                                 xf86_config->crtc[i]->funcs->restore(xf86_config->crtc[i]);
2125                         }
2126
2127                         for (i = 0; i < xf86_config->num_output; i++) {
2128                                 xf86_config->output[i]->funcs->restore(xf86_config->
2129                                                                        output[i]);
2130                         }
2131
2132                         for (i = 0; i < xf86_config->num_crtc; i++) {
2133                                 NVCrtcLockUnlock(xf86_config->crtc[i], 1);
2134                         }
2135                 }
2136
2137                 /* bit28: transmit text mode on both heads? */
2138                 if (pNv->twoHeads) {
2139                         uint32_t debug1 = nvReadMC(pNv, NV_PBUS_DEBUG_1);
2140                         nvWriteMC(pNv, NV_PBUS_DEBUG_1, debug1 | (1 << 28));
2141                 }
2142         } else {
2143                 NVLockUnlock(pNv, 0);
2144
2145                 if(pNv->twoHeads) {
2146                         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->crtc_active[1] * 0x3);
2147                         NVLockUnlock(pNv, 0);
2148                 }
2149
2150                 /* Only restore text mode fonts/text for the primary card */
2151                 vgaHWProtect(pScrn, TRUE);
2152                 NVDACRestore(pScrn, vgaReg, nvReg, pNv->Primary);
2153                 vgaHWProtect(pScrn, FALSE);
2154         }
2155
2156         if (pNv->twoHeads && !pNv->new_restore) {
2157                 NVLockUnlock(pNv, 0);
2158                 ErrorF("Restoring CRTC_OWNER to %d\n", pNv->vtOWNER);
2159                 NVWriteVGA(pNv, 0, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
2160                 NVLockUnlock(pNv, 1);
2161         }
2162 }
2163
2164 static void
2165 NVLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
2166               LOCO * colors, VisualPtr pVisual)
2167 {
2168         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2169         int c;
2170         int i, j, index;
2171         CARD16 lut_r[256], lut_g[256], lut_b[256];
2172
2173         for (c = 0; c < xf86_config->num_crtc; c++) {
2174                 xf86CrtcPtr crtc = xf86_config->crtc[c];
2175
2176                 if (crtc->enabled == 0)
2177                         continue;
2178
2179                 /* code borrowed from intel driver */
2180                 switch (pScrn->depth) {
2181                 case 15:
2182                         for (i = 0; i < numColors; i++) {
2183                                 index = indices[i];
2184                                 for (j = 0; j < 8; j++) {
2185                                         lut_r[index * 8 + j] = colors[index].red << 8;
2186                                         lut_g[index * 8 + j] = colors[index].green << 8;
2187                                         lut_b[index * 8 + j] = colors[index].blue << 8;
2188                                 }
2189                         }
2190                 case 16:
2191                         for (i = 0; i < numColors; i++) {
2192                                 index = indices[i];
2193
2194                                 if (i <= 31) {
2195                                         for (j = 0; j < 8; j++) {
2196                                                 lut_r[index * 8 + j] = colors[index].red << 8;
2197                                                 lut_b[index * 8 + j] = colors[index].blue << 8;
2198                                         }
2199                                 }
2200
2201                                 for (j = 0; j < 4; j++) {
2202                                         lut_g[index * 4 + j] = colors[index].green << 8;
2203                                 }
2204                         }
2205                 default:
2206                         for (i = 0; i < numColors; i++) {
2207                                 index = indices[i];
2208                                 lut_r[index] = colors[index].red << 8;
2209                                 lut_g[index] = colors[index].green << 8;
2210                                 lut_b[index] = colors[index].blue << 8;
2211                         }
2212                         break;
2213                 }
2214
2215                 /* Make the change through RandR */
2216                 RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
2217         }
2218 }
2219
2220 #define DEPTH_SHIFT(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
2221 #define COLOR(c) (unsigned int)(0x3fff * ((c)/255.0))
2222 static void
2223 NV50LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
2224                 LOCO * colors, VisualPtr pVisual)
2225 {
2226         NVPtr pNv = NVPTR(pScrn);
2227         int i, index;
2228         volatile struct {
2229                 unsigned short red, green, blue, unused;
2230         } *lut = (void *) pNv->CLUT->map;
2231
2232         switch (pScrn->depth) {
2233         case 15:
2234                 for (i = 0; i < numColors; i++) {
2235                         index = indices[i];
2236                         lut[DEPTH_SHIFT(index, 5)].red =
2237                             COLOR(colors[index].red);
2238                         lut[DEPTH_SHIFT(index, 5)].green =
2239                             COLOR(colors[index].green);
2240                         lut[DEPTH_SHIFT(index, 5)].blue =
2241                             COLOR(colors[index].blue);
2242                 }
2243                 break;
2244         case 16:
2245                 for (i = 0; i < numColors; i++) {
2246                         index = indices[i];
2247                         lut[DEPTH_SHIFT(index, 6)].green =
2248                             COLOR(colors[index].green);
2249                         if (index < 32) {
2250                                 lut[DEPTH_SHIFT(index, 5)].red =
2251                                     COLOR(colors[index].red);
2252                                 lut[DEPTH_SHIFT(index, 5)].blue =
2253                                     COLOR(colors[index].blue);
2254                         }
2255                 }
2256                 break;
2257         default:
2258                 for (i = 0; i < numColors; i++) {
2259                         index = indices[i];
2260                         lut[index].red = COLOR(colors[index].red);
2261                         lut[index].green = COLOR(colors[index].green);
2262                         lut[index].blue = COLOR(colors[index].blue);
2263                 }
2264                 break;
2265         }
2266 }
2267
2268
2269 static void NVBacklightEnable(NVPtr pNv,  Bool on)
2270 {
2271     /* This is done differently on each laptop.  Here we
2272        define the ones we know for sure. */
2273
2274 #if defined(__powerpc__)
2275     if((pNv->Chipset == 0x10DE0179) || 
2276        (pNv->Chipset == 0x10DE0189) || 
2277        (pNv->Chipset == 0x10DE0329))
2278     {
2279        /* NV17,18,34 Apple iMac, iBook, PowerBook */
2280       CARD32 tmp_pmc, tmp_pcrt;
2281       tmp_pmc = nvReadMC(pNv, NV_PBUS_DUALHEAD_CTL) & 0x7FFFFFFF;
2282       tmp_pcrt = nvReadCRTC0(pNv, NV_PCRTC_GPIO_EXT) & 0xFFFFFFFC;
2283       if(on) {
2284           tmp_pmc |= (1 << 31);
2285           tmp_pcrt |= 0x1;
2286       }
2287       nvWriteMC(pNv, NV_PBUS_DUALHEAD_CTL, tmp_pmc);
2288       nvWriteCRTC0(pNv, NV_PCRTC_GPIO_EXT, tmp_pcrt);
2289     }
2290 #endif
2291     
2292     if(pNv->LVDS) {
2293        if(pNv->twoHeads && ((pNv->Chipset & 0x0ff0) != CHIPSET_NV11)) {
2294            nvWriteMC(pNv, 0x130C, on ? 3 : 7);
2295        }
2296     } else {
2297        CARD32 fpcontrol;
2298
2299        fpcontrol = nvReadCurRAMDAC(pNv, NV_RAMDAC_FP_CONTROL) & 0xCfffffCC;
2300
2301        /* cut the TMDS output */
2302        if(on) fpcontrol |= pNv->fpSyncs;
2303        else fpcontrol |= 0x20000022;
2304
2305        nvWriteCurRAMDAC(pNv, NV_RAMDAC_FP_CONTROL, fpcontrol);
2306     }
2307 }
2308
2309 static void
2310 NVDPMSSetLCD(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
2311 {
2312   NVPtr pNv = NVPTR(pScrn);
2313
2314   if (!pScrn->vtSema) return;
2315
2316   vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2317
2318   switch (PowerManagementMode) {
2319   case DPMSModeStandby:  /* HSync: Off, VSync: On */
2320   case DPMSModeSuspend:  /* HSync: On, VSync: Off */
2321   case DPMSModeOff:      /* HSync: Off, VSync: Off */
2322     NVBacklightEnable(pNv, 0);
2323     break;
2324   case DPMSModeOn:       /* HSync: On, VSync: On */
2325     NVBacklightEnable(pNv, 1);
2326   default:
2327     break;
2328   }
2329 }
2330
2331
2332 static void
2333 NVDPMSSet(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
2334 {
2335   unsigned char crtc1A;
2336   vgaHWPtr hwp = VGAHWPTR(pScrn);
2337
2338   if (!pScrn->vtSema) return;
2339
2340   crtc1A = hwp->readCrtc(hwp, 0x1A) & ~0xC0;
2341
2342   switch (PowerManagementMode) {
2343   case DPMSModeStandby:  /* HSync: Off, VSync: On */
2344     crtc1A |= 0x80;
2345     break;
2346   case DPMSModeSuspend:  /* HSync: On, VSync: Off */
2347     crtc1A |= 0x40;
2348     break;
2349   case DPMSModeOff:      /* HSync: Off, VSync: Off */
2350     crtc1A |= 0xC0;
2351     break;
2352   case DPMSModeOn:       /* HSync: On, VSync: On */
2353   default:
2354     break;
2355   }
2356
2357   /* vgaHWDPMSSet will merely cut the dac output */
2358   vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2359
2360   hwp->writeCrtc(hwp, 0x1A, crtc1A);
2361 }
2362
2363
2364 /* Mandatory */
2365
2366 /* This gets called at the start of each server generation */
2367
2368 static Bool
2369 NVScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
2370 {
2371         ScrnInfoPtr pScrn;
2372         vgaHWPtr hwp;
2373         NVPtr pNv;
2374         int ret;
2375         VisualPtr visual;
2376         unsigned char *FBStart;
2377         int width, height, displayWidth, shadowHeight, i;
2378
2379         /* 
2380          * First get the ScrnInfoRec
2381          */
2382         pScrn = xf86Screens[pScreen->myNum];
2383
2384         hwp = VGAHWPTR(pScrn);
2385         pNv = NVPTR(pScrn);
2386
2387         /* Map the VGA memory when the primary video */
2388         if (pNv->Primary) {
2389                 hwp->MapSize = 0x10000;
2390                 if (!vgaHWMapMem(pScrn))
2391                         return FALSE;
2392         }
2393
2394         /* First init DRI/DRM */
2395         if (!NVDRIScreenInit(pScrn))
2396                 return FALSE;
2397
2398         /* Allocate and map memory areas we need */
2399         if (!NVMapMem(pScrn))
2400                 return FALSE;
2401
2402         if (!pNv->NoAccel) {
2403                 /* Init DRM - Alloc FIFO */
2404                 if (!NVInitDma(pScrn))
2405                         return FALSE;
2406
2407                 /* setup graphics objects */
2408                 if (!NVAccelCommonInit(pScrn))
2409                         return FALSE;
2410         }
2411
2412 #if NOUVEAU_EXA_PIXMAPS
2413         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
2414                         0, NOUVEAU_ALIGN(pScrn->virtualX, 64) * NOUVEAU_ALIGN(pScrn->virtualY, 64) *
2415                         (pScrn->bitsPerPixel >> 3), &pNv->FB)) {
2416                 ErrorF("Failed to allocate memory for screen pixmap.\n");
2417                 return FALSE;
2418         }
2419 #endif
2420
2421         if (!pNv->randr12_enable) {
2422                 /* Save the current state */
2423                 NVSave(pScrn);
2424                 /* Initialise the first mode */
2425                 if (!NVModeInit(pScrn, pScrn->currentMode))
2426                         return FALSE;
2427
2428                 /* Darken the screen for aesthetic reasons and set the viewport */
2429                 NVSaveScreen(pScreen, SCREEN_SAVER_ON);
2430                 pScrn->AdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
2431         } else {
2432                 pScrn->memPhysBase = pNv->VRAMPhysical;
2433                 pScrn->fbOffset = 0;
2434
2435                 /* Gather some misc info before the randr stuff kicks in */
2436                 if (pNv->Architecture >= NV_ARCH_10) {
2437                         pNv->misc_info.crtc_reg_52[0] = NVReadVGA(pNv, 0, NV_VGA_CRTCX_52);
2438                         pNv->misc_info.crtc_reg_52[1] = NVReadVGA(pNv, 1, NV_VGA_CRTCX_52);
2439                 }
2440                 if (pNv->Architecture == NV_ARCH_40) {
2441                         pNv->misc_info.ramdac_0_reg_580 = NVReadRAMDAC(pNv, 0, NV_RAMDAC_580);
2442                         pNv->misc_info.reg_c040 = nvReadMC(pNv, 0xc040);
2443                 }
2444                 pNv->misc_info.ramdac_general_control[0] = NVReadRAMDAC(pNv, 0, NV_RAMDAC_GENERAL_CONTROL);
2445                 pNv->misc_info.ramdac_general_control[1] = NVReadRAMDAC(pNv, 1, NV_RAMDAC_GENERAL_CONTROL);
2446                 pNv->misc_info.ramdac_0_pllsel = NVReadRAMDAC(pNv, 0, NV_RAMDAC_PLL_SELECT);
2447                 pNv->misc_info.sel_clk = NVReadRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK);
2448                 if (pNv->twoHeads) {
2449                         pNv->misc_info.output[0] = NVReadRAMDAC(pNv, 0, NV_RAMDAC_OUTPUT);
2450                         pNv->misc_info.output[1] = NVReadRAMDAC(pNv, 1, NV_RAMDAC_OUTPUT);
2451                 }
2452
2453                 for (i = 0; i <= pNv->twoHeads; i++) {
2454                         if (NVReadVGA(pNv, i, NV_VGA_CRTCX_PIXEL) & 0xf) { /* framebuffer mode */
2455                                 pNv->console_mode[i].vga_mode = FALSE;
2456                                 uint8_t var = NVReadVGA(pNv, i, NV_VGA_CRTCX_PIXEL) & 0xf;
2457                                 Bool filled = (NVReadRAMDAC(pNv, i, NV_RAMDAC_GENERAL_CONTROL) & 0x1000);
2458                                 switch (var){
2459                                         case 3:
2460                                                 if (filled)
2461                                                         pNv->console_mode[i].depth = 32;
2462                                                 else
2463                                                         pNv->console_mode[i].depth = 24;
2464                                                 /* This is pitch related. */
2465                                                 pNv->console_mode[i].bpp = 32;
2466                                                 break;
2467                                         case 2:
2468                                                 if (filled)
2469                                                         pNv->console_mode[i].depth = 16;
2470                                                 else
2471                                                         pNv->console_mode[i].depth = 15;
2472                                                 /* This is pitch related. */
2473                                                 pNv->console_mode[i].bpp = 16;
2474                                                 break;
2475                                         case 1:
2476                                                 /* 8bit mode is always filled? */
2477                                                 pNv->console_mode[i].depth = 8;
2478                                                 /* This is pitch related. */
2479                                                 pNv->console_mode[i].bpp = 8;
2480                                         default:
2481                                                 break;
2482                                 }
2483                         } else { /* vga mode */
2484                                 pNv->console_mode[i].vga_mode = TRUE;
2485                                 pNv->console_mode[i].bpp = 4;
2486                                 pNv->console_mode[i].depth = 4;
2487                         }
2488
2489                         pNv->console_mode[i].x_res = (NVReadVGA(pNv, i, NV_VGA_CRTCX_HDISPE) + 1) * 8;
2490                         pNv->console_mode[i].y_res = (NVReadVGA(pNv, i, NV_VGA_CRTCX_VDISPE) + 1); /* NV_VGA_CRTCX_VDISPE only contains the lower 8 bits. */
2491
2492                         pNv->console_mode[i].fb_start = NVReadCRTC(pNv, i, NV_CRTC_START);
2493
2494                         pNv->console_mode[i].enabled = FALSE;
2495
2496                         ErrorF("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);
2497                 }
2498
2499                 /* Check if crtc's were enabled. */
2500                 if (pNv->misc_info.ramdac_0_pllsel & NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL) {
2501                         pNv->console_mode[0].enabled = TRUE;
2502                         ErrorF("CRTC 0 was enabled.\n");
2503                 }
2504
2505                 if (pNv->misc_info.ramdac_0_pllsel & NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2) {
2506                         pNv->console_mode[1].enabled = TRUE;
2507                         ErrorF("CRTC 1 was enabled.\n");
2508                 }
2509
2510                 if (!NVEnterVT(scrnIndex, 0))
2511                         return FALSE;
2512                 NVSaveScreen(pScreen, SCREEN_SAVER_ON);
2513         }
2514
2515
2516         /*
2517          * The next step is to setup the screen's visuals, and initialise the
2518          * framebuffer code.  In cases where the framebuffer's default
2519          * choices for things like visual layouts and bits per RGB are OK,
2520          * this may be as simple as calling the framebuffer's ScreenInit()
2521          * function.  If not, the visuals will need to be setup before calling
2522          * a fb ScreenInit() function and fixed up after.
2523          *
2524          * For most PC hardware at depths >= 8, the defaults that fb uses
2525          * are not appropriate.  In this driver, we fixup the visuals after.
2526          */
2527
2528         /*
2529          * Reset the visual list.
2530          */
2531         miClearVisualTypes();
2532
2533         /* Setup the visuals we support. */
2534
2535         if (!miSetVisualTypes(pScrn->depth, 
2536                                 miGetDefaultVisualMask(pScrn->depth), 8,
2537                                 pScrn->defaultVisual))
2538                 return FALSE;
2539         if (!miSetPixmapDepths ())
2540                 return FALSE;
2541
2542         /*
2543          * Call the framebuffer layer's ScreenInit function, and fill in other
2544          * pScreen fields.
2545          */
2546
2547         width = pScrn->virtualX;
2548         height = pScrn->virtualY;
2549         displayWidth = pScrn->displayWidth;
2550
2551         if(pNv->Rotate) {
2552                 height = pScrn->virtualX;
2553                 width = pScrn->virtualY;
2554         }
2555
2556         /* If RandR rotation is enabled, leave enough space in the
2557          * framebuffer for us to rotate the screen dimensions without
2558          * changing the pitch.
2559          */
2560         if(pNv->RandRRotation) {
2561                 shadowHeight = max(width, height);
2562         } else {
2563                 shadowHeight = height;
2564         }
2565
2566         if (pNv->ShadowFB) {
2567                 pNv->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * width);
2568                 pNv->ShadowPtr = xalloc(pNv->ShadowPitch * shadowHeight);
2569                 displayWidth = pNv->ShadowPitch / (pScrn->bitsPerPixel >> 3);
2570                 FBStart = pNv->ShadowPtr;
2571         } else {
2572                 pNv->ShadowPtr = NULL;
2573                 FBStart = pNv->FB->map;
2574         }
2575
2576         switch (pScrn->bitsPerPixel) {
2577                 case 8:
2578                 case 16:
2579                 case 32:
2580                         ret = fbScreenInit(pScreen, FBStart, width, height,
2581                                 pScrn->xDpi, pScrn->yDpi,
2582                                 displayWidth, pScrn->bitsPerPixel);
2583                         break;
2584                 default:
2585                         xf86DrvMsg(scrnIndex, X_ERROR,
2586                                 "Internal error: invalid bpp (%d) in NVScreenInit\n",
2587                                 pScrn->bitsPerPixel);
2588                         ret = FALSE;
2589                         break;
2590         }
2591         if (!ret)
2592                 return FALSE;
2593
2594         if (pScrn->bitsPerPixel > 8) {
2595                 /* Fixup RGB ordering */
2596                 visual = pScreen->visuals + pScreen->numVisuals;
2597                 while (--visual >= pScreen->visuals) {
2598                         if ((visual->class | DynamicClass) == DirectColor) {
2599                                 visual->offsetRed = pScrn->offset.red;
2600                                 visual->offsetGreen = pScrn->offset.green;
2601                                 visual->offsetBlue = pScrn->offset.blue;
2602                                 visual->redMask = pScrn->mask.red;
2603                                 visual->greenMask = pScrn->mask.green;
2604                                 visual->blueMask = pScrn->mask.blue;
2605                         }
2606                 }
2607         }
2608
2609         fbPictureInit (pScreen, 0, 0);
2610
2611         xf86SetBlackWhitePixels(pScreen);
2612
2613         if (!pNv->NoAccel) {
2614                 if (!NVExaInit(pScreen))
2615                         return FALSE;
2616                 NVResetGraphics(pScrn);
2617         } else if (pNv->VRAMPhysicalSize / 2 < NOUVEAU_ALIGN(pScrn->virtualX, 64) * NOUVEAU_ALIGN(pScrn->virtualY, 64) * (pScrn->bitsPerPixel >> 3)) {
2618                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "The virtual screen size's resolution is too big for the video RAM framebuffer at this colour depth.\n");
2619                 return FALSE;
2620         }
2621
2622
2623         miInitializeBackingStore(pScreen);
2624         xf86SetBackingStore(pScreen);
2625         xf86SetSilkenMouse(pScreen);
2626
2627         /* Finish DRI init */
2628         NVDRIFinishScreenInit(pScrn);
2629
2630         /* 
2631          * Initialize software cursor.
2632          * Must precede creation of the default colormap.
2633          */
2634         miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
2635
2636         /*
2637          * Initialize HW cursor layer. 
2638          * Must follow software cursor initialization.
2639          */
2640         if (pNv->HWCursor) { 
2641                 if (pNv->Architecture < NV_ARCH_50 && !pNv->randr12_enable)
2642                         ret = NVCursorInit(pScreen);
2643                 else if (pNv->Architecture < NV_ARCH_50 && pNv->randr12_enable)
2644                         ret = NVCursorInitRandr12(pScreen);
2645                 else
2646                         ret = NV50CursorInit(pScreen);
2647
2648                 if (ret != TRUE) {
2649                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 
2650                                 "Hardware cursor initialization failed\n");
2651                         pNv->HWCursor = FALSE;
2652                 }
2653         }
2654
2655         if (pNv->randr12_enable) {
2656                 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
2657
2658                 if (!xf86CrtcScreenInit(pScreen))
2659                         return FALSE;
2660
2661                 pNv->PointerMoved = pScrn->PointerMoved;
2662                 pScrn->PointerMoved = NVPointerMoved;
2663         }
2664
2665         /* Initialise default colourmap */
2666         if (!miCreateDefColormap(pScreen))
2667                 return FALSE;
2668
2669         /*
2670          * Initialize colormap layer.
2671          * Must follow initialization of the default colormap 
2672          */
2673         if (!pNv->randr12_enable) {
2674                 if(!xf86HandleColormaps(pScreen, 256, 8, NVDACLoadPalette,
2675                                 NULL, CMAP_RELOAD_ON_MODE_SWITCH | CMAP_PALETTED_TRUECOLOR))
2676                 return FALSE;
2677         } else {
2678                 if (pNv->Architecture < NV_ARCH_50) {
2679                         if (!xf86HandleColormaps(pScreen, 256, 8, NVLoadPalette,
2680                                                 NULL,
2681                                                 CMAP_RELOAD_ON_MODE_SWITCH |
2682                                                 CMAP_PALETTED_TRUECOLOR))
2683                         return FALSE;
2684                 } else {
2685                         if (!xf86HandleColormaps(pScreen, 256, 8, NV50LoadPalette,
2686                                                 NULL, CMAP_PALETTED_TRUECOLOR))
2687                         return FALSE;
2688                 }
2689         }
2690
2691         if(pNv->ShadowFB) {
2692                 RefreshAreaFuncPtr refreshArea = NVRefreshArea;
2693
2694                 if (pNv->Rotate || pNv->RandRRotation) {
2695                         pNv->PointerMoved = pScrn->PointerMoved;
2696                         if (pNv->Rotate)
2697                                 pScrn->PointerMoved = NVPointerMoved;
2698
2699                         switch(pScrn->bitsPerPixel) {
2700                                 case 8: refreshArea = NVRefreshArea8;   break;
2701                                 case 16:        refreshArea = NVRefreshArea16;  break;
2702                                 case 32:        refreshArea = NVRefreshArea32;  break;
2703                         }
2704                         if(!pNv->RandRRotation) {
2705                                 xf86DisableRandR();
2706                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2707                                         "Driver rotation enabled, RandR disabled\n");
2708                         }
2709                 }
2710
2711                 ShadowFBInit(pScreen, refreshArea);
2712         }
2713
2714         if (!pNv->randr12_enable) {
2715                 if(pNv->FlatPanel) {
2716                         xf86DPMSInit(pScreen, NVDPMSSetLCD, 0);
2717                 } else {
2718                         xf86DPMSInit(pScreen, NVDPMSSet, 0);
2719                 }
2720         }
2721
2722         pScrn->memPhysBase = pNv->VRAMPhysical;
2723         pScrn->fbOffset = 0;
2724
2725         if (pNv->Rotate == 0 && !pNv->RandRRotation)
2726                 NVInitVideo(pScreen);
2727
2728         pScreen->SaveScreen = NVSaveScreen;
2729
2730         /* Wrap the current CloseScreen function */
2731         pNv->CloseScreen = pScreen->CloseScreen;
2732         pScreen->CloseScreen = NVCloseScreen;
2733
2734         pNv->BlockHandler = pScreen->BlockHandler;
2735         pScreen->BlockHandler = NVBlockHandler;
2736
2737         /* Install our DriverFunc.  We have to do it this way instead of using the
2738          * HaveDriverFuncs argument to xf86AddDriver, because InitOutput clobbers
2739          * pScrn->DriverFunc 
2740          */
2741         if (!pNv->randr12_enable)
2742                 pScrn->DriverFunc = NVDriverFunc;
2743
2744         /* Report any unused options (only for the first generation) */
2745         if (serverGeneration == 1) {
2746                 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
2747         }
2748
2749         return TRUE;
2750 }
2751
2752 static Bool
2753 NVSaveScreen(ScreenPtr pScreen, int mode)
2754 {
2755     ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
2756     NVPtr pNv = NVPTR(pScrn);
2757     int i;
2758     Bool on = xf86IsUnblank(mode);
2759     
2760     if (pNv->randr12_enable) {
2761         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2762         if (pScrn->vtSema && pNv->Architecture < NV_ARCH_50) {
2763             for (i = 0; i < xf86_config->num_crtc; i++) {
2764                 
2765                 if (xf86_config->crtc[i]->enabled) {
2766                     NVCrtcBlankScreen(xf86_config->crtc[i],
2767                                       on);
2768                 }
2769             }
2770             
2771         }
2772         return TRUE;
2773     }
2774
2775         return vgaHWSaveScreen(pScreen, mode);
2776 }
2777
2778 static void
2779 NVSave(ScrnInfoPtr pScrn)
2780 {
2781         NVPtr pNv = NVPTR(pScrn);
2782         NVRegPtr nvReg = &pNv->SavedReg;
2783
2784         if (pNv->randr12_enable) {
2785                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2786                 int i;
2787
2788                 for (i = 0; i < xf86_config->num_crtc; i++) {
2789                         xf86_config->crtc[i]->funcs->save(xf86_config->crtc[i]);
2790                 }
2791
2792                 for (i = 0; i < xf86_config->num_output; i++) {
2793                         xf86_config->output[i]->funcs->save(xf86_config->
2794                                                             output[i]);
2795                 }
2796         } else {
2797                 vgaHWPtr pVga = VGAHWPTR(pScrn);
2798                 vgaRegPtr vgaReg = &pVga->SavedReg;
2799                 NVLockUnlock(pNv, 0);
2800                 if (pNv->twoHeads) {
2801                         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->crtc_active[1] * 0x3);
2802                         NVLockUnlock(pNv, 0);
2803                 }
2804
2805                 NVDACSave(pScrn, vgaReg, nvReg, pNv->Primary);
2806         }
2807 }
2808
2809 static Bool
2810 NVRandRGetInfo(ScrnInfoPtr pScrn, Rotation *rotations)
2811 {
2812     NVPtr pNv = NVPTR(pScrn);
2813
2814     if(pNv->RandRRotation)
2815        *rotations = RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_270;
2816     else
2817        *rotations = RR_Rotate_0;
2818
2819     return TRUE;
2820 }
2821
2822 static Bool
2823 NVRandRSetConfig(ScrnInfoPtr pScrn, xorgRRConfig *config)
2824 {
2825     NVPtr pNv = NVPTR(pScrn);
2826
2827     switch(config->rotation) {
2828         case RR_Rotate_0:
2829             pNv->Rotate = 0;
2830             pScrn->PointerMoved = pNv->PointerMoved;
2831             break;
2832
2833         case RR_Rotate_90:
2834             pNv->Rotate = -1;
2835             pScrn->PointerMoved = NVPointerMoved;
2836             break;
2837
2838         case RR_Rotate_270:
2839             pNv->Rotate = 1;
2840             pScrn->PointerMoved = NVPointerMoved;
2841             break;
2842
2843         default:
2844             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2845                     "Unexpected rotation in NVRandRSetConfig!\n");
2846             pNv->Rotate = 0;
2847             pScrn->PointerMoved = pNv->PointerMoved;
2848             return FALSE;
2849     }
2850
2851     return TRUE;
2852 }
2853
2854 static Bool
2855 NVDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op, pointer data)
2856 {
2857     switch(op) {
2858        case RR_GET_INFO:
2859           return NVRandRGetInfo(pScrn, (Rotation*)data);
2860        case RR_SET_CONFIG:
2861           return NVRandRSetConfig(pScrn, (xorgRRConfig*)data);
2862        default:
2863           return FALSE;
2864     }
2865
2866     return FALSE;
2867 }