Neither overlay or blit adaptor work on G8x.
[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 "nv_include.h"
31
32 #include "xf86int10.h"
33
34 #include "xf86drm.h"
35
36 extern DisplayModePtr xf86ModesAdd(DisplayModePtr Modes, DisplayModePtr Additions);
37
38 /*
39  * Forward definitions for the functions that make up the driver.
40  */
41 /* Mandatory functions */
42 static const OptionInfoRec * NVAvailableOptions(int chipid, int busid);
43 static void    NVIdentify(int flags);
44 #ifndef XSERVER_LIBPCIACCESS
45 static Bool    NVProbe(DriverPtr drv, int flags);
46 #endif /* XSERVER_LIBPCIACCESS */
47 static Bool    NVPreInit(ScrnInfoPtr pScrn, int flags);
48 static Bool    NVScreenInit(int Index, ScreenPtr pScreen, int argc,
49                             char **argv);
50 static Bool    NVEnterVT(int scrnIndex, int flags);
51 static void    NVLeaveVT(int scrnIndex, int flags);
52 static Bool    NVCloseScreen(int scrnIndex, ScreenPtr pScreen);
53 static Bool    NVSaveScreen(ScreenPtr pScreen, int mode);
54
55 /* Optional functions */
56 static void    NVFreeScreen(int scrnIndex, int flags);
57 static ModeStatus NVValidMode(int scrnIndex, DisplayModePtr mode,
58                               Bool verbose, int flags);
59 #ifdef RANDR
60 static Bool    NVDriverFunc(ScrnInfoPtr pScrnInfo, xorgDriverFuncOp op,
61                               pointer data);
62 #endif
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 static Bool
391 NVGetScrnInfoRec(PciChipsets *chips, int chip)
392 {
393     ScrnInfoPtr pScrn;
394
395     pScrn = xf86ConfigPciEntity(NULL, 0, chip,
396                                 chips, NULL, NULL, NULL,
397                                 NULL, NULL);
398
399     if(!pScrn) return FALSE;
400
401     pScrn->driverVersion    = NV_VERSION;
402     pScrn->driverName       = NV_DRIVER_NAME;
403     pScrn->name             = NV_NAME;
404
405 #ifndef XSERVER_LIBPCIACCESS
406         pScrn->Probe = NVProbe;
407 #else
408         pScrn->Probe = NULL;
409 #endif
410     pScrn->PreInit          = NVPreInit;
411     pScrn->ScreenInit       = NVScreenInit;
412     pScrn->SwitchMode       = NVSwitchMode;
413     pScrn->AdjustFrame      = NVAdjustFrame;
414     pScrn->EnterVT          = NVEnterVT;
415     pScrn->LeaveVT          = NVLeaveVT;
416     pScrn->FreeScreen       = NVFreeScreen;
417     pScrn->ValidMode        = NVValidMode;
418
419     return TRUE;
420 }
421
422 /* This returns architecture in hexdecimal, so NV40 is 0x40 */
423 static char NVGetArchitecture (volatile CARD32 *regs)
424 {
425         char architecture = 0;
426
427         /* We're dealing with >=NV10 */
428         if ((regs[0] & 0x0f000000) > 0 ) {
429                 /* Bit 27-20 contain the architecture in hex */
430                 architecture = (regs[0] & 0xff00000) >> 20;
431         /* NV04 or NV05 */
432         } else if ((regs[0] & 0xff00fff0) == 0x20004000) {
433                 architecture = 0x04;
434         }
435
436         return architecture;
437 }
438
439 /* Reading the pci_id from the card registers is the most reliable way */
440 static CARD32 NVGetPCIID (volatile CARD32 *regs)
441 {
442         CARD32 pci_id;
443
444         char architecture = NVGetArchitecture(regs);
445
446         /* Dealing with an unknown or unsupported card */
447         if (architecture == 0) {
448                 return 0;
449         }
450
451         if (architecture >= 0x04 && architecture <= 0x4F) {
452                 pci_id = regs[0x1800/4];
453         } else if (architecture >= 0x50 && architecture <= 0x5F) {
454                 pci_id = regs[0x88000/4];
455         } else {
456                 return 0;
457         }
458
459         /* A pci-id can be inverted, we must correct this */
460         if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA) {
461                 pci_id = (PCI_VENDOR_NVIDIA << 16) | (pci_id >> 16);
462         } else if ((pci_id & 0xffff) == PCI_VENDOR_NVIDIA_SGS) {
463                 pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | (pci_id >> 16);
464         /* Checking endian issues */
465         } else {
466                 /* PCI_VENDOR_NVIDIA = 0x10DE */
467                 if ((pci_id & (0xffff << 16)) == (0xDE10 << 16)) { /* wrong endian */
468                         pci_id = (PCI_VENDOR_NVIDIA << 16) | ((pci_id << 8) & 0x0000ff00) |
469                                 ((pci_id >> 8) & 0x000000ff);
470                 /* PCI_VENDOR_NVIDIA_SGS = 0x12D2 */
471                 } else if ((pci_id & (0xffff << 16)) == (0xD212 << 16)) { /* wrong endian */
472                         pci_id = (PCI_VENDOR_NVIDIA_SGS << 16) | ((pci_id << 8) & 0x0000ff00) |
473                                 ((pci_id >> 8) & 0x000000ff);
474                 }
475         }
476
477         return pci_id;
478 }
479
480 #ifdef XSERVER_LIBPCIACCESS
481
482 static Bool NVPciProbe (        DriverPtr               drv,
483                                 int                     entity_num,
484                                 struct pci_device       *dev,
485                                 intptr_t                match_data      )
486 {
487         ScrnInfoPtr pScrn = NULL;
488
489         volatile CARD32 *regs = NULL;
490
491         /* Temporary mapping to discover the architecture */
492         pci_device_map_memory_range(dev, PCI_DEV_MEM_BASE(dev, 0), 0x90000, FALSE, &regs);
493
494         /* Bit 27-20 contain the architecture in hex */
495         char architecture = (regs[0] & 0xff00000) >> 20;
496
497         CARD32 pci_id = NVGetPCIID(regs);
498
499         pci_device_unmap_memory_range(dev, regs, 0x90000);
500
501         /* Currently NV04 up to NV83 is supported */
502         /* For safety the fictional NV8F is used */
503         if (architecture >= 0x04 && architecture <= 0x8F) {
504
505                 /* At this stage the pci_id should be ok, so we generate this to avoid list duplication */
506                 const PciChipsets NVChipsets[] = {
507                         { pci_id, pci_id, RES_SHARED_VGA },
508                         { -1, -1, RES_UNDEFINED }
509                 };
510
511                 pScrn = xf86ConfigPciEntity(pScrn, 0, entity_num, NVChipsets, 
512                                                 NULL, NULL, NULL, NULL, NULL);
513
514                 if (pScrn != NULL) {
515                         pScrn->driverVersion    = NV_VERSION;
516                         pScrn->driverName       = NV_DRIVER_NAME;
517                         pScrn->name             = NV_NAME;
518
519                         pScrn->Probe            = NULL;
520                         pScrn->PreInit          = NVPreInit;
521                         pScrn->ScreenInit       = NVScreenInit;
522                         pScrn->SwitchMode       = NVSwitchMode;
523                         pScrn->AdjustFrame      = NVAdjustFrame;
524                         pScrn->EnterVT          = NVEnterVT;
525                         pScrn->LeaveVT          = NVLeaveVT;
526                         pScrn->FreeScreen       = NVFreeScreen;
527                         pScrn->ValidMode        = NVValidMode;
528
529                         return TRUE;
530                 }
531         }
532
533         return FALSE;
534 }
535
536 #endif /* XSERVER_LIBPCIACCESS */
537
538 #define MAX_CHIPS MAXSCREENS
539
540 #ifndef XSERVER_LIBPCIACCESS
541 /* Mandatory */
542 static Bool
543 NVProbe(DriverPtr drv, int flags)
544 {
545         int i;
546         GDevPtr *devSections;
547         int *usedChips;
548         SymTabRec NVChipsets[MAX_CHIPS + 1];
549         PciChipsets NVPciChipsets[MAX_CHIPS + 1];
550         pciVideoPtr *ppPci;
551         int numDevSections;
552         int numUsed;
553         Bool foundScreen = FALSE;
554
555         if ((numDevSections = xf86MatchDevice(NV_DRIVER_NAME, &devSections)) <= 0) 
556                 return FALSE;  /* no matching device section */
557
558         if (!(ppPci = xf86GetPciVideoInfo())) 
559                 return FALSE;  /* no PCI cards found */
560
561         numUsed = 0;
562
563         /* Create the NVChipsets and NVPciChipsets from found devices */
564         while (*ppPci && (numUsed < MAX_CHIPS)) {
565                 if (((*ppPci)->vendor == PCI_VENDOR_NVIDIA_SGS) || 
566                         ((*ppPci)->vendor == PCI_VENDOR_NVIDIA)) 
567                 {
568                         volatile CARD32 *regs;
569                         CARD32 pcicmd;
570
571                         PCI_DEV_READ_LONG(*ppPci, PCI_CMD_STAT_REG, &pcicmd);
572                         /* Enable reading memory? */
573                         PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
574
575                         regs = xf86MapPciMem(-1, VIDMEM_MMIO, PCI_DEV_TAG(*ppPci), PCI_DEV_MEM_BASE(*ppPci, 0), 0x90000);
576                         int pciid = NVGetPCIID(regs);
577
578                         char architecture = NVGetArchitecture(regs);
579                         char name[25];
580                         sprintf(name, "NVIDIA NV%02X", architecture);
581                         /* NV04 upto NV83 is supported, NV8F is fictive limit */
582                         if (architecture >= 0x04 && architecture <= 0x8F) {
583                                 NVChipsets[numUsed].token = pciid;
584                                 NVChipsets[numUsed].name = name;
585                                 NVPciChipsets[numUsed].numChipset = pciid; 
586                                 NVPciChipsets[numUsed].PCIid = pciid;
587                                 NVPciChipsets[numUsed].resList = RES_SHARED_VGA;
588                                 numUsed++;
589                         }
590                         xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
591
592                         /* Reset previous state */
593                         PCI_DEV_WRITE_LONG(*ppPci, PCI_CMD_STAT_REG, pcicmd);
594                 }
595                 ppPci++;
596         }
597
598         /* terminate the list */
599         NVChipsets[numUsed].token = -1;
600         NVChipsets[numUsed].name = NULL; 
601         NVPciChipsets[numUsed].numChipset = -1;
602         NVPciChipsets[numUsed].PCIid = -1;
603         NVPciChipsets[numUsed].resList = RES_UNDEFINED;
604
605         numUsed = xf86MatchPciInstances(NV_NAME, 0, NVChipsets, NVPciChipsets,
606                                         devSections, numDevSections, drv,
607                                         &usedChips);
608
609         if (numUsed <= 0) {
610                 return FALSE;
611         }
612
613         if (flags & PROBE_DETECT) {
614                 foundScreen = TRUE;
615         } else {
616                 for (i = 0; i < numUsed; i++) {
617                         pciVideoPtr pPci;
618
619                         pPci = xf86GetPciInfoForEntity(usedChips[i]);
620                         if (NVGetScrnInfoRec(NVPciChipsets, usedChips[i])) {
621                                 foundScreen = TRUE;
622                         }
623                 }
624         }
625
626         xfree(devSections);
627         xfree(usedChips);
628
629         return foundScreen;
630 }
631 #endif /* XSERVER_LIBPCIACCESS */
632
633 /*
634  * This function is needed by the XF86VideMode extension which is used by
635  * the current pre-randr clients. The API covers only one screen, but
636  * implementing the latest modesetting framework like done in the Intel
637  * driver is more than a few lines of patch, the randr-1.2 branch in its
638  * current form cannot the mode switching in a perfect way right now.
639  *
640  * As there are effors to bring modesetting into the kernel, controlled
641  * thru the drm module, of which nouveu currently requires its own version,
642  * one could even try to go one step further and try to bring the nouveau
643  * modesetting into the nouveau kernel module.c (as a first step which does
644  * not require a kernel patch), which would increase the chances that the
645  * text console is properly restored after X dies as the kernel can simply
646  * restore the text console when the process which has changed modes thru
647  * /dev/drm has been disconnected from the device.
648  *
649  * The current implementation simply tries to set each crtc to the mode
650  * for which the application asks for, hoping that one of them gives a
651  * usable monitor display (no error handling implemented), and sets
652  * the viewport of each crtc to (0,0), which means essentially clone
653  * mode with all monitors which managed to switch to the mode showing
654  * top left area of the framebuffer memory if the application's window
655  * is there. This is essentially what the Intel driver did in earlyer
656  * versions. To restore a LeftOf/RightOf layout, you two randr calls
657  * seem to be neccessary, one which sets the reversed layout, followed
658  * by one which sets the desired layout:
659  *
660  * xrandr --output Digital-1 --left-of Digital-0
661  * xrandr --output Digital-0 --left-of Digital-1
662  *
663  * FIXME: This could be fixed by getting the current viewports for the
664  * CRTCs and use these during mode settings, or (preferably) by getting
665  * the current screen layout and adapting the new viewports so that
666  * a new, continuos screen layout with the same monitor arrangement,
667  * but in the new mode is set up.
668  */
669 Bool
670 NVSwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
671 {
672     ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
673     NVPtr pNv = NVPTR(pScrn);
674     Bool ret = TRUE;
675
676     if (pNv->randr12_enable) {
677         NVFBLayout *pLayout = &pNv->CurrentLayout;
678
679         if (pLayout->mode != mode) {
680                 /* This needs to be fixed with error handling */
681                 NVSetMode(pScrn, mode);
682                 pLayout->mode = mode;
683         }
684
685         pLayout->mode = mode;
686         return ret;
687     } else {
688         return NVModeInit(xf86Screens[scrnIndex], mode);
689     }
690 }
691
692 /*
693  * This function is used to initialize the Start Address - the first
694  * displayed location in the video memory.
695  */
696 /* Usually mandatory */
697 void 
698 NVAdjustFrame(int scrnIndex, int x, int y, int flags)
699 {
700     ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
701     int startAddr;
702     NVPtr pNv = NVPTR(pScrn);
703     NVFBLayout *pLayout = &pNv->CurrentLayout;
704
705     if (pNv->randr12_enable) {
706         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
707         int startAddr;
708         xf86CrtcPtr crtc = config->output[config->compat_output]->crtc;
709         
710         if (crtc && crtc->enabled) {
711             NVCrtcSetBase(crtc, x, y);
712         }
713     } else {
714         startAddr = (((y*pLayout->displayWidth)+x)*(pLayout->bitsPerPixel/8));
715         startAddr += pNv->FB->offset;
716         NVSetStartAddress(pNv, startAddr);
717     }
718 }
719
720 void
721 NVResetCrtcConfig(ScrnInfoPtr pScrn, int set)
722 {
723         xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
724         NVPtr pNv = NVPTR(pScrn);
725         int i;
726         CARD32 val = 0;
727
728         for (i = 0; i < config->num_crtc; i++) {
729                 xf86CrtcPtr crtc = config->crtc[i];
730                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
731
732                 if (set) {
733                         NVCrtcRegPtr regp;
734
735                         regp = &pNv->ModeReg.crtc_reg[nv_crtc->crtc];
736                         val = regp->head;
737                 }
738
739                 nvWriteCRTC(pNv, nv_crtc->crtc, NV_CRTC_FSEL, val);
740         }
741 }
742
743 static Bool
744 NV50AcquireDisplay(ScrnInfoPtr pScrn)
745 {
746         if (!NV50DispInit(pScrn))
747                 return FALSE;
748         if (!NV50CursorAcquire(pScrn))
749                 return FALSE;
750         xf86SetDesiredModes(pScrn);
751
752         return TRUE;
753 }
754
755 static Bool
756 NV50ReleaseDisplay(ScrnInfoPtr pScrn)
757 {
758         NV50CursorRelease(pScrn);
759         NV50DispShutdown(pScrn);
760
761         return TRUE;
762 }
763
764 /*
765  * This is called when VT switching back to the X server.  Its job is
766  * to reinitialise the video mode.
767  *
768  * We may wish to unmap video/MMIO memory too.
769  */
770
771 /* Mandatory */
772 static Bool
773 NVEnterVT(int scrnIndex, int flags)
774 {
775     ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
776     NVPtr pNv = NVPTR(pScrn);
777     
778     if (pNv->randr12_enable) { 
779         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
780         pScrn->vtSema = TRUE;
781
782         if (pNv->Architecture == NV_ARCH_50) {
783                 if (!NV50AcquireDisplay(pScrn))
784                         return FALSE;
785                 return TRUE;
786         }
787
788         /* Save the current state */
789         if (pNv->SaveGeneration != serverGeneration) {
790                 pNv->SaveGeneration = serverGeneration;
791                 NVSave(pScrn);
792         }
793
794         NVResetCrtcConfig(pScrn, 0);
795         if (!xf86SetDesiredModes(pScrn))
796                 return FALSE;
797         NVResetCrtcConfig(pScrn, 1);
798
799     } else {
800         if (!NVModeInit(pScrn, pScrn->currentMode))
801             return FALSE;
802
803     }
804     NVAdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
805     if(pNv->overlayAdaptor)
806         NVResetVideo(pScrn);
807     return TRUE;
808     
809 }
810
811 /*
812  * This is called when VT switching away from the X server.  Its job is
813  * to restore the previous (text) mode.
814  *
815  * We may wish to remap video/MMIO memory too.
816  */
817
818 /* Mandatory */
819 static void
820 NVLeaveVT(int scrnIndex, int flags)
821 {
822     ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
823     NVPtr pNv = NVPTR(pScrn);
824
825     if (pNv->Architecture == NV_ARCH_50) {
826         NV50ReleaseDisplay(pScrn);
827         return;
828     }
829     NVSync(pScrn);
830     NVRestore(pScrn);
831     if (!pNv->randr12_enable)
832         NVLockUnlock(pNv, 1);
833 }
834
835
836
837 static void 
838 NVBlockHandler (
839     int i, 
840     pointer blockData, 
841     pointer pTimeout,
842     pointer pReadmask
843 )
844 {
845     ScreenPtr     pScreen = screenInfo.screens[i];
846     ScrnInfoPtr   pScrnInfo = xf86Screens[i];
847     NVPtr         pNv = NVPTR(pScrnInfo);
848
849     if (pNv->DMAKickoffCallback)
850         (*pNv->DMAKickoffCallback)(pNv);
851     
852     pScreen->BlockHandler = pNv->BlockHandler;
853     (*pScreen->BlockHandler) (i, blockData, pTimeout, pReadmask);
854     pScreen->BlockHandler = NVBlockHandler;
855
856     if (pNv->VideoTimerCallback) 
857         (*pNv->VideoTimerCallback)(pScrnInfo, currentTime.milliseconds);
858
859 }
860
861
862 /*
863  * This is called at the end of each server generation.  It restores the
864  * original (text) mode.  It should also unmap the video memory, and free
865  * any per-generation data allocated by the driver.  It should finish
866  * by unwrapping and calling the saved CloseScreen function.
867  */
868
869 /* Mandatory */
870 static Bool
871 NVCloseScreen(int scrnIndex, ScreenPtr pScreen)
872 {
873     ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
874     NVPtr pNv = NVPTR(pScrn);
875
876     if (pScrn->vtSema) {
877         pScrn->vtSema = FALSE;
878         if (pNv->Architecture == NV_ARCH_50) {
879             NV50ReleaseDisplay(pScrn);
880         } else {
881             NVSync(pScrn);
882             NVRestore(pScrn);
883             if (!pNv->randr12_enable)
884                 NVLockUnlock(pNv, 1);
885         }
886     }
887
888     NVUnmapMem(pScrn);
889     vgaHWUnmapMem(pScrn);
890     if (pNv->CursorInfoRec)
891         xf86DestroyCursorInfoRec(pNv->CursorInfoRec);
892     if (pNv->ShadowPtr)
893         xfree(pNv->ShadowPtr);
894     if (pNv->overlayAdaptor)
895         xfree(pNv->overlayAdaptor);
896     if (pNv->blitAdaptor)
897         xfree(pNv->blitAdaptor);
898
899     pScrn->vtSema = FALSE;
900     pScreen->CloseScreen = pNv->CloseScreen;
901     pScreen->BlockHandler = pNv->BlockHandler;
902     return (*pScreen->CloseScreen)(scrnIndex, pScreen);
903 }
904
905 /* Free up any persistent data structures */
906
907 /* Optional */
908 static void
909 NVFreeScreen(int scrnIndex, int flags)
910 {
911     /*
912      * This only gets called when a screen is being deleted.  It does not
913      * get called routinely at the end of a server generation.
914      */
915     if (xf86LoaderCheckSymbol("vgaHWFreeHWRec"))
916         vgaHWFreeHWRec(xf86Screens[scrnIndex]);
917     NVFreeRec(xf86Screens[scrnIndex]);
918 }
919
920
921 /* Checks if a mode is suitable for the selected chipset. */
922
923 /* Optional */
924 static ModeStatus
925 NVValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
926 {
927     NVPtr pNv = NVPTR(xf86Screens[scrnIndex]);
928
929     if(pNv->fpWidth && pNv->fpHeight)
930       if((pNv->fpWidth < mode->HDisplay) || (pNv->fpHeight < mode->VDisplay))
931         return (MODE_PANEL);
932
933     return (MODE_OK);
934 }
935
936 static void
937 nvProbeDDC(ScrnInfoPtr pScrn, int index)
938 {
939     vbeInfoPtr pVbe;
940
941     if (xf86LoadSubModule(pScrn, "vbe")) {
942         pVbe = VBEInit(NULL,index);
943         ConfiguredMonitor = vbeDoEDID(pVbe, NULL);
944         vbeFree(pVbe);
945     }
946 }
947
948
949 Bool NVI2CInit(ScrnInfoPtr pScrn)
950 {
951     char *mod = "i2c";
952
953     if (xf86LoadSubModule(pScrn, mod)) {
954         xf86LoaderReqSymLists(i2cSymbols,NULL);
955
956         mod = "ddc";
957         if(xf86LoadSubModule(pScrn, mod)) {
958             xf86LoaderReqSymLists(ddcSymbols, NULL);
959             return NVDACi2cInit(pScrn);
960         } 
961     }
962
963     xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
964               "Couldn't load %s module.  DDC probing can't be done\n", mod);
965
966     return FALSE;
967 }
968
969 static Bool NVPreInitDRI(ScrnInfoPtr pScrn)
970 {
971         NVPtr pNv = NVPTR(pScrn);
972
973         if (!NVDRIGetVersion(pScrn))
974                 return FALSE;
975
976         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
977                 "[dri] Found DRI library version %d.%d.%d and kernel"
978                 " module version %d.%d.%d\n",
979                 pNv->pLibDRMVersion->version_major,
980                 pNv->pLibDRMVersion->version_minor,
981                 pNv->pLibDRMVersion->version_patchlevel,
982                 pNv->pKernelDRMVersion->version_major,
983                 pNv->pKernelDRMVersion->version_minor,
984                 pNv->pKernelDRMVersion->version_patchlevel);
985
986         return TRUE;
987 }
988
989
990 static Bool
991 nv_xf86crtc_resize(ScrnInfoPtr scrn, int width, int height)
992 {
993         scrn->virtualX = width;
994         scrn->virtualY = height;
995         return TRUE;
996 }
997
998 static const xf86CrtcConfigFuncsRec nv_xf86crtc_config_funcs = {
999         nv_xf86crtc_resize
1000 };
1001
1002 #define NVPreInitFail(fmt, args...) do {                                    \
1003         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "%d: "fmt, __LINE__, ##args); \
1004         if (pNv->pInt10)                                                    \
1005                 xf86FreeInt10(pNv->pInt10);                                 \
1006         NVFreeRec(pScrn);                                                   \
1007         return FALSE;                                                       \
1008 } while(0)
1009
1010 /* Mandatory */
1011 Bool
1012 NVPreInit(ScrnInfoPtr pScrn, int flags)
1013 {
1014     xf86CrtcConfigPtr xf86_config;
1015     NVPtr pNv;
1016     MessageType from;
1017     int i, max_width, max_height;
1018     ClockRangePtr clockRanges;
1019     const char *s;
1020     int config_mon_rates = FALSE;
1021     int num_crtc;
1022
1023     if (flags & PROBE_DETECT) {
1024         EntityInfoPtr pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1025
1026         if (!pEnt)
1027             return FALSE;
1028
1029         i = pEnt->index;
1030         xfree(pEnt);
1031
1032         nvProbeDDC(pScrn, i);
1033         return TRUE;
1034     }
1035
1036     /*
1037      * Note: This function is only called once at server startup, and
1038      * not at the start of each server generation.  This means that
1039      * only things that are persistent across server generations can
1040      * be initialised here.  xf86Screens[] is (pScrn is a pointer to one
1041      * of these).  Privates allocated using xf86AllocateScrnInfoPrivateIndex()  
1042      * are too, and should be used for data that must persist across
1043      * server generations.
1044      *
1045      * Per-generation data should be allocated with
1046      * AllocateScreenPrivateIndex() from the ScreenInit() function.
1047      */
1048
1049     /* Check the number of entities, and fail if it isn't one. */
1050     if (pScrn->numEntities != 1)
1051         return FALSE;
1052
1053     /* Allocate the NVRec driverPrivate */
1054     if (!NVGetRec(pScrn)) {
1055         return FALSE;
1056     }
1057     pNv = NVPTR(pScrn);
1058
1059     /* Get the entity, and make sure it is PCI. */
1060     pNv->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
1061     if (pNv->pEnt->location.type != BUS_PCI)
1062         return FALSE;
1063  
1064     /* Find the PCI info for this screen */
1065     pNv->PciInfo = xf86GetPciInfoForEntity(pNv->pEnt->index);
1066 #ifndef XSERVER_LIBPCIACCESS
1067     pNv->PciTag = pciTag(pNv->PciInfo->bus, pNv->PciInfo->device,
1068                           pNv->PciInfo->func);
1069 #endif /* XSERVER_LIBPCIACCESS */
1070
1071     pNv->Primary = xf86IsPrimaryPci(pNv->PciInfo);
1072
1073     /* Initialize the card through int10 interface if needed */
1074     if (xf86LoadSubModule(pScrn, "int10")) {
1075         xf86LoaderReqSymLists(int10Symbols, NULL);
1076 #if !defined(__alpha__) && !defined(__powerpc__)
1077         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initializing int10\n");
1078         pNv->pInt10 = xf86InitInt10(pNv->pEnt->index);
1079 #endif
1080     }
1081    
1082     xf86SetOperatingState(resVgaIo, pNv->pEnt->index, ResUnusedOpr);
1083     xf86SetOperatingState(resVgaMem, pNv->pEnt->index, ResDisableOpr);
1084
1085     /* Set pScrn->monitor */
1086     pScrn->monitor = pScrn->confScreen->monitor;
1087
1088         volatile CARD32 *regs = NULL;
1089 #ifdef XSERVER_LIBPCIACCESS
1090         pci_device_map_memory_range(pNv->PciInfo, PCI_DEV_MEM_BASE(pNv->PciInfo, 0), 0x90000, FALSE, &regs);
1091         pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1092         pNv->NVArch = NVGetArchitecture(regs);
1093         pci_device_unmap_memory_range(pNv->PciInfo, regs, 0x90000);
1094 #else
1095         CARD32 pcicmd;
1096         PCI_DEV_READ_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, &pcicmd);
1097         /* Enable reading memory? */
1098         PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd | PCI_CMD_MEM_ENABLE);
1099         regs = xf86MapPciMem(-1, VIDMEM_MMIO, pNv->PciTag, PCI_DEV_MEM_BASE(pNv->PciInfo, 0), 0x90000);
1100         pNv->Chipset = NVGetPCIID(regs) & 0xffff;
1101         pNv->NVArch = NVGetArchitecture(regs);
1102         xf86UnMapVidMem(-1, (pointer)regs, 0x90000);
1103         /* Reset previous state */
1104         PCI_DEV_WRITE_LONG(pNv->PciInfo, PCI_CMD_STAT_REG, pcicmd);
1105 #endif /* XSERVER_LIBPCIACCESS */
1106
1107         pScrn->chipset = malloc(sizeof(char) * 25);
1108         sprintf(pScrn->chipset, "NVIDIA NV%02X", pNv->NVArch);
1109
1110         if(!pScrn->chipset) {
1111                 pScrn->chipset = "Unknown NVIDIA";
1112         }
1113
1114         /*
1115         * This shouldn't happen because such problems should be caught in
1116         * NVProbe(), but check it just in case.
1117         */
1118         if (pScrn->chipset == NULL)
1119                 NVPreInitFail("ChipID 0x%04X is not recognised\n", pNv->Chipset);
1120
1121         if (pNv->NVArch < 0x04)
1122                 NVPreInitFail("Chipset \"%s\" is not recognised\n", pScrn->chipset);
1123
1124         xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "Chipset: \"%s\"\n", pScrn->chipset);
1125
1126         /* The highest architecture currently supported is NV5x */
1127         if (pNv->NVArch >= 0x50) {
1128                 pNv->Architecture =  NV_ARCH_50;
1129         } else if (pNv->NVArch >= 0x40) {
1130                 pNv->Architecture =  NV_ARCH_40;
1131         } else if (pNv->NVArch >= 0x30) {
1132                 pNv->Architecture = NV_ARCH_30;
1133         } else if (pNv->NVArch >= 0x20) {
1134                 pNv->Architecture = NV_ARCH_20;
1135         } else if (pNv->NVArch >= 0x10) {
1136                 pNv->Architecture = NV_ARCH_10;
1137         } else if (pNv->NVArch >= 0x04) {
1138                 pNv->Architecture = NV_ARCH_04;
1139         /*  The lowest architecture currently supported is NV04 */
1140         } else {
1141                 return FALSE;
1142         }
1143
1144     /*
1145      * The first thing we should figure out is the depth, bpp, etc.
1146      */
1147
1148     if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support32bppFb)) {
1149         NVPreInitFail("\n");
1150     } else {
1151         /* Check that the returned depth is one we support */
1152         switch (pScrn->depth) {
1153             case 8:
1154             case 15:
1155             case 16:
1156             case 24:
1157                 /* OK */
1158                 break;
1159             default:
1160                 NVPreInitFail("Given depth (%d) is not supported by this driver\n",
1161                               pScrn->depth);
1162         }
1163     }
1164     xf86PrintDepthBpp(pScrn);
1165
1166     /* Get the depth24 pixmap format */
1167     if (pScrn->depth == 24 && pix24bpp == 0)
1168         pix24bpp = xf86GetBppFromDepth(pScrn, 24);
1169
1170     /*
1171      * This must happen after pScrn->display has been set because
1172      * xf86SetWeight references it.
1173      */
1174     if (pScrn->depth > 8) {
1175         /* The defaults are OK for us */
1176         rgb zeros = {0, 0, 0};
1177
1178         if (!xf86SetWeight(pScrn, zeros, zeros)) {
1179             NVPreInitFail("\n");
1180         }
1181     }
1182
1183     if (!xf86SetDefaultVisual(pScrn, -1)) {
1184         NVPreInitFail("\n");
1185     } else {
1186         /* We don't currently support DirectColor at > 8bpp */
1187         if (pScrn->depth > 8 && (pScrn->defaultVisual != TrueColor)) {
1188             NVPreInitFail("Given default visual"
1189                        " (%s) is not supported at depth %d\n",
1190                        xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
1191             
1192         }
1193     }
1194
1195     /* The vgahw module should be loaded here when needed */
1196     if (!xf86LoadSubModule(pScrn, "vgahw")) {
1197         NVPreInitFail("\n");
1198     }
1199     
1200     xf86LoaderReqSymLists(vgahwSymbols, NULL);
1201
1202     /*
1203      * Allocate a vgaHWRec
1204      */
1205     if (!vgaHWGetHWRec(pScrn)) {
1206         NVPreInitFail("\n");
1207     }
1208     
1209     /* We use a programmable clock */
1210     pScrn->progClock = TRUE;
1211
1212     /* Collect all of the relevant option flags (fill in pScrn->options) */
1213     xf86CollectOptions(pScrn, NULL);
1214
1215     /* Process the options */
1216     if (!(pNv->Options = xalloc(sizeof(NVOptions))))
1217         return FALSE;
1218     memcpy(pNv->Options, NVOptions, sizeof(NVOptions));
1219     xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, pNv->Options);
1220
1221     /* Set the bits per RGB for 8bpp mode */
1222     if (pScrn->depth == 8)
1223         pScrn->rgbBits = 8;
1224
1225     from = X_DEFAULT;
1226
1227     if (pNv->Architecture == NV_ARCH_50) {
1228             pNv->randr12_enable = TRUE;
1229     } else {
1230         pNv->randr12_enable = FALSE;
1231         if (xf86ReturnOptValBool(pNv->Options, OPTION_RANDR12, FALSE)) {
1232             pNv->randr12_enable = TRUE;
1233         }
1234     }
1235     xf86DrvMsg(pScrn->scrnIndex, from, "Randr1.2 support %sabled\n", pNv->randr12_enable ? "en" : "dis");
1236
1237     pNv->HWCursor = TRUE;
1238     /*
1239      * The preferred method is to use the "hw cursor" option as a tri-state
1240      * option, with the default set above.
1241      */
1242     if (xf86GetOptValBool(pNv->Options, OPTION_HW_CURSOR, &pNv->HWCursor)) {
1243         from = X_CONFIG;
1244     }
1245     /* For compatibility, accept this too (as an override) */
1246     if (xf86ReturnOptValBool(pNv->Options, OPTION_SW_CURSOR, FALSE)) {
1247         from = X_CONFIG;
1248         pNv->HWCursor = FALSE;
1249     }
1250     xf86DrvMsg(pScrn->scrnIndex, from, "Using %s cursor\n",
1251                 pNv->HWCursor ? "HW" : "SW");
1252
1253     pNv->FpScale = TRUE;
1254     if (xf86GetOptValBool(pNv->Options, OPTION_FP_SCALE, &pNv->FpScale)) {
1255         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Flat panel scaling %s\n",
1256                    pNv->FpScale ? "on" : "off");
1257     }
1258     if (xf86ReturnOptValBool(pNv->Options, OPTION_NOACCEL, FALSE)) {
1259         pNv->NoAccel = TRUE;
1260         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Acceleration disabled\n");
1261     }
1262     if (xf86ReturnOptValBool(pNv->Options, OPTION_SHADOW_FB, FALSE)) {
1263         pNv->ShadowFB = TRUE;
1264         pNv->NoAccel = TRUE;
1265         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1266                 "Using \"Shadow Framebuffer\" - acceleration disabled\n");
1267     }
1268     
1269     pNv->Rotate = 0;
1270     pNv->RandRRotation = FALSE;
1271     if ((s = xf86GetOptValString(pNv->Options, OPTION_ROTATE))) {
1272       if(!xf86NameCmp(s, "CW")) {
1273         pNv->ShadowFB = TRUE;
1274         pNv->NoAccel = TRUE;
1275         pNv->HWCursor = FALSE;
1276         pNv->Rotate = 1;
1277         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1278                 "Rotating screen clockwise - acceleration disabled\n");
1279       } else
1280       if(!xf86NameCmp(s, "CCW")) {
1281         pNv->ShadowFB = TRUE;
1282         pNv->NoAccel = TRUE;
1283         pNv->HWCursor = FALSE;
1284         pNv->Rotate = -1;
1285         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1286                 "Rotating screen counter clockwise - acceleration disabled\n");
1287       } else
1288       if(!xf86NameCmp(s, "RandR")) {
1289 #ifdef RANDR
1290         pNv->ShadowFB = TRUE;
1291         pNv->NoAccel = TRUE;
1292         pNv->HWCursor = FALSE;
1293         pNv->RandRRotation = TRUE;
1294         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
1295                 "Using RandR rotation - acceleration disabled\n");
1296 #else
1297         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
1298                 "This driver was not compiled with support for the Resize and "
1299                 "Rotate extension.  Cannot honor 'Option \"Rotate\" "
1300                 "\"RandR\"'.\n");
1301 #endif
1302       } else {
1303         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1304                 "\"%s\" is not a valid value for Option \"Rotate\"\n", s);
1305         xf86DrvMsg(pScrn->scrnIndex, X_INFO, 
1306                 "Valid options are \"CW\", \"CCW\", and \"RandR\"\n");
1307       }
1308     }
1309
1310     if(xf86GetOptValInteger(pNv->Options, OPTION_VIDEO_KEY, &(pNv->videoKey))) {
1311         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "video key set to 0x%x\n",
1312                                 pNv->videoKey);
1313     } else {
1314         pNv->videoKey =  (1 << pScrn->offset.red) | 
1315                           (1 << pScrn->offset.green) |
1316         (((pScrn->mask.blue >> pScrn->offset.blue) - 1) << pScrn->offset.blue); 
1317     }
1318
1319     if (xf86GetOptValBool(pNv->Options, OPTION_FLAT_PANEL, &(pNv->FlatPanel))) {
1320         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "forcing %s usage\n",
1321                    pNv->FlatPanel ? "DFP" : "CRTC");
1322     } else {
1323         pNv->FlatPanel = -1;   /* autodetect later */
1324     }
1325
1326     pNv->FPDither = FALSE;
1327     if (xf86GetOptValBool(pNv->Options, OPTION_FP_DITHER, &(pNv->FPDither))) 
1328         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "enabling flat panel dither\n");
1329
1330     if (xf86GetOptValInteger(pNv->Options, OPTION_CRTC_NUMBER,
1331                              &pNv->CRTCnumber)) 
1332     {
1333         if((pNv->CRTCnumber < 0) || (pNv->CRTCnumber > 1)) {
1334            pNv->CRTCnumber = -1;
1335            xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, 
1336                       "Invalid CRTC number.  Must be 0 or 1\n");
1337         }
1338     } else {
1339         pNv->CRTCnumber = -1; /* autodetect later */
1340     }
1341
1342
1343     if (xf86GetOptValInteger(pNv->Options, OPTION_FP_TWEAK, 
1344                              &pNv->PanelTweak))
1345     {
1346         pNv->usePanelTweak = TRUE;
1347     } else {
1348         pNv->usePanelTweak = FALSE;
1349     }
1350     
1351     if (pNv->pEnt->device->MemBase != 0) {
1352         /* Require that the config file value matches one of the PCI values. */
1353         if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->MemBase)) {
1354             NVPreInitFail(
1355                 "MemBase 0x%08lX doesn't match any PCI base register.\n",
1356                 pNv->pEnt->device->MemBase);
1357         }
1358         pNv->VRAMPhysical = pNv->pEnt->device->MemBase;
1359         from = X_CONFIG;
1360     } else {
1361         if (PCI_DEV_MEM_BASE(pNv->PciInfo, 1) != 0) {
1362             pNv->VRAMPhysical = PCI_DEV_MEM_BASE(pNv->PciInfo, 1) & 0xff800000;
1363             from = X_PROBED;
1364         } else {
1365             NVPreInitFail("No valid FB address in PCI config space\n");
1366             return FALSE;
1367         }
1368     }
1369     xf86DrvMsg(pScrn->scrnIndex, from, "Linear framebuffer at 0x%lX\n",
1370                (unsigned long)pNv->VRAMPhysical);
1371
1372     if (pNv->pEnt->device->IOBase != 0) {
1373         /* Require that the config file value matches one of the PCI values. */
1374         if (!xf86CheckPciMemBase(pNv->PciInfo, pNv->pEnt->device->IOBase)) {
1375             NVPreInitFail("IOBase 0x%08lX doesn't match any PCI base register.\n",
1376                           pNv->pEnt->device->IOBase);
1377         }
1378         pNv->IOAddress = pNv->pEnt->device->IOBase;
1379         from = X_CONFIG;
1380     } else {
1381         if (PCI_DEV_MEM_BASE(pNv->PciInfo, 0) != 0) {
1382             pNv->IOAddress = PCI_DEV_MEM_BASE(pNv->PciInfo, 0) & 0xffffc000;
1383             from = X_PROBED;
1384         } else {
1385             NVPreInitFail("No valid MMIO address in PCI config space\n");
1386         }
1387     }
1388     xf86DrvMsg(pScrn->scrnIndex, from, "MMIO registers at 0x%lX\n",
1389                (unsigned long)pNv->IOAddress);
1390      
1391         if (xf86RegisterResources(pNv->pEnt->index, NULL, ResExclusive)) {
1392                 NVPreInitFail("xf86RegisterResources() found resource conflicts\n");
1393     }
1394
1395     pNv->alphaCursor = (pNv->Architecture >= NV_ARCH_10) &&
1396                        ((pNv->Chipset & 0x0ff0) != CHIPSET_NV10);
1397
1398     if (pNv->randr12_enable) {
1399         /* Allocate an xf86CrtcConfig */
1400         xf86CrtcConfigInit(pScrn, &nv_xf86crtc_config_funcs);
1401         xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1402         
1403         max_width = 16384;
1404         xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, 2048);
1405     }
1406
1407     if (NVPreInitDRI(pScrn) == FALSE) {
1408         NVPreInitFail("\n");
1409     }
1410
1411     if (!pNv->randr12_enable) {
1412         if ((pScrn->monitor->nHsync == 0) && 
1413             (pScrn->monitor->nVrefresh == 0))
1414             config_mon_rates = FALSE;
1415         else
1416             config_mon_rates = TRUE;
1417     }
1418
1419     NVCommonSetup(pScrn);
1420
1421     if (pNv->randr12_enable) {
1422         if (pNv->Architecture < NV_ARCH_50) {
1423             NVI2CInit(pScrn);
1424             
1425             num_crtc = pNv->twoHeads ? 2 : 1;
1426             for (i = 0; i < num_crtc; i++) {
1427                 nv_crtc_init(pScrn, i);
1428             }
1429             
1430             NvSetupOutputs(pScrn);
1431         } else {
1432             if (!NV50DispPreInit(pScrn))
1433                 NVPreInitFail("\n");
1434             if (!NV50CreateOutputs(pScrn))
1435                 NVPreInitFail("\n");
1436             NV50DispCreateCrtcs(pScrn);
1437         }
1438
1439         if (!xf86InitialConfiguration(pScrn, FALSE))
1440             NVPreInitFail("No valid modes.\n");
1441     }
1442
1443     pScrn->videoRam = pNv->RamAmountKBytes;
1444     xf86DrvMsg(pScrn->scrnIndex, X_PROBED, "VideoRAM: %d kBytes\n",
1445                pScrn->videoRam);
1446         
1447     pNv->VRAMPhysicalSize = pScrn->videoRam * 1024;
1448
1449     /*
1450      * If the driver can do gamma correction, it should call xf86SetGamma()
1451      * here.
1452      */
1453
1454     {
1455         Gamma zeros = {0.0, 0.0, 0.0};
1456
1457         if (!xf86SetGamma(pScrn, zeros)) {
1458             NVPreInitFail("\n");
1459         }
1460     }
1461
1462     /*
1463      * Setup the ClockRanges, which describe what clock ranges are available,
1464      * and what sort of modes they can be used for.
1465      */
1466
1467     clockRanges = xnfcalloc(sizeof(ClockRange), 1);
1468     clockRanges->next = NULL;
1469     clockRanges->minClock = pNv->MinVClockFreqKHz;
1470     clockRanges->maxClock = pNv->MaxVClockFreqKHz;
1471     clockRanges->clockIndex = -1;               /* programmable */
1472     clockRanges->doubleScanAllowed = TRUE;
1473     if((pNv->Architecture == NV_ARCH_20) ||
1474          ((pNv->Architecture == NV_ARCH_10) && 
1475            ((pNv->Chipset & 0x0ff0) != CHIPSET_NV10) &&
1476            ((pNv->Chipset & 0x0ff0) != CHIPSET_NV15)))
1477     {
1478        /* HW is broken */
1479        clockRanges->interlaceAllowed = FALSE;
1480     } else {
1481        clockRanges->interlaceAllowed = TRUE;
1482     }
1483
1484     if(pNv->FlatPanel == 1) {
1485        clockRanges->interlaceAllowed = FALSE;
1486        clockRanges->doubleScanAllowed = FALSE;
1487     }
1488
1489     if(pNv->Architecture < NV_ARCH_10) {
1490        max_width = (pScrn->bitsPerPixel > 16) ? 2032 : 2048;
1491        max_height = 2048;
1492     } else {
1493        max_width = (pScrn->bitsPerPixel > 16) ? 4080 : 4096;
1494        max_height = 4096;
1495     }
1496
1497 #ifdef M_T_DRIVER
1498     /* If DFP, add a modeline corresponding to its panel size */
1499     if (pNv->FlatPanel && !pNv->Television && pNv->fpWidth && pNv->fpHeight) {
1500         DisplayModePtr Mode;
1501
1502         Mode = xnfcalloc(1, sizeof(DisplayModeRec));
1503         Mode = xf86CVTMode(pNv->fpWidth, pNv->fpHeight, 60.00, TRUE, FALSE);
1504         Mode->type = M_T_DRIVER;
1505         pScrn->monitor->Modes = xf86ModesAdd(pScrn->monitor->Modes, Mode);
1506
1507         if (!config_mon_rates) {
1508             if (!Mode->HSync)
1509                 Mode->HSync = ((float) Mode->Clock ) / ((float) Mode->HTotal);
1510             if (!Mode->VRefresh)
1511                 Mode->VRefresh = (1000.0 * ((float) Mode->Clock)) /
1512                     ((float) (Mode->HTotal * Mode->VTotal));
1513
1514             if (Mode->HSync < pScrn->monitor->hsync[0].lo)
1515                 pScrn->monitor->hsync[0].lo = Mode->HSync;
1516             if (Mode->HSync > pScrn->monitor->hsync[0].hi)
1517                 pScrn->monitor->hsync[0].hi = Mode->HSync;
1518             if (Mode->VRefresh < pScrn->monitor->vrefresh[0].lo)
1519                 pScrn->monitor->vrefresh[0].lo = Mode->VRefresh;
1520             if (Mode->VRefresh > pScrn->monitor->vrefresh[0].hi)
1521                 pScrn->monitor->vrefresh[0].hi = Mode->VRefresh;
1522
1523             pScrn->monitor->nHsync = 1;
1524             pScrn->monitor->nVrefresh = 1;
1525         }
1526     }
1527 #endif
1528
1529     /*
1530      * xf86ValidateModes will check that the mode HTotal and VTotal values
1531      * don't exceed the chipset's limit if pScrn->maxHValue and
1532      * pScrn->maxVValue are set.  Since our NVValidMode() already takes
1533      * care of this, we don't worry about setting them here.
1534      */
1535     i = xf86ValidateModes(pScrn, pScrn->monitor->Modes,
1536                           pScrn->display->modes, clockRanges,
1537                           NULL, 256, max_width,
1538                           512, 128, max_height,
1539                           pScrn->display->virtualX,
1540                           pScrn->display->virtualY,
1541                           pNv->VRAMPhysicalSize / 2,
1542                           LOOKUP_BEST_REFRESH);
1543
1544     if (i == -1) {
1545         NVPreInitFail("\n");
1546     }
1547
1548     /* Prune the modes marked as invalid */
1549     xf86PruneDriverModes(pScrn);
1550
1551     if (i == 0 || pScrn->modes == NULL) {
1552         NVPreInitFail("No valid modes found\n");
1553     }
1554
1555     /*
1556      * Set the CRTC parameters for all of the modes based on the type
1557      * of mode, and the chipset's interlace requirements.
1558      *
1559      * Calling this is required if the mode->Crtc* values are used by the
1560      * driver and if the driver doesn't provide code to set them.  They
1561      * are not pre-initialised at all.
1562      */
1563     xf86SetCrtcForModes(pScrn, 0);
1564
1565     /* Set the current mode to the first in the list */
1566     pScrn->currentMode = pScrn->modes;
1567
1568     /* Print the list of modes being used */
1569     xf86PrintModes(pScrn);
1570
1571     /* Set display resolution */
1572     xf86SetDpi(pScrn, 0, 0);
1573
1574
1575     /*
1576      * XXX This should be taken into account in some way in the mode valdation
1577      * section.
1578      */
1579
1580     if (xf86LoadSubModule(pScrn, "fb") == NULL) {
1581         NVPreInitFail("\n");
1582     }
1583
1584     xf86LoaderReqSymLists(fbSymbols, NULL);
1585     
1586     /* Load EXA if needed */
1587     if (!pNv->NoAccel) {
1588         if (!xf86LoadSubModule(pScrn, "exa")) {
1589             NVPreInitFail("\n");
1590         }
1591         xf86LoaderReqSymLists(exaSymbols, NULL);
1592     }
1593
1594     /* Load ramdac if needed */
1595     if (pNv->HWCursor) {
1596         if (!xf86LoadSubModule(pScrn, "ramdac")) {
1597             NVPreInitFail("\n");
1598         }
1599         xf86LoaderReqSymLists(ramdacSymbols, NULL);
1600     }
1601
1602     /* Load shadowfb if needed */
1603     if (pNv->ShadowFB) {
1604         if (!xf86LoadSubModule(pScrn, "shadowfb")) {
1605             NVPreInitFail("\n");
1606         }
1607         xf86LoaderReqSymLists(shadowSymbols, NULL);
1608     }
1609
1610     pNv->CurrentLayout.bitsPerPixel = pScrn->bitsPerPixel;
1611     pNv->CurrentLayout.depth = pScrn->depth;
1612     pNv->CurrentLayout.displayWidth = pScrn->displayWidth;
1613     pNv->CurrentLayout.weight.red = pScrn->weight.red;
1614     pNv->CurrentLayout.weight.green = pScrn->weight.green;
1615     pNv->CurrentLayout.weight.blue = pScrn->weight.blue;
1616     pNv->CurrentLayout.mode = pScrn->currentMode;
1617
1618     xf86FreeInt10(pNv->pInt10);
1619
1620     pNv->pInt10 = NULL;
1621     return TRUE;
1622 }
1623
1624
1625 /*
1626  * Map the framebuffer and MMIO memory.
1627  */
1628
1629 static Bool
1630 NVMapMem(ScrnInfoPtr pScrn)
1631 {
1632         NVPtr pNv = NVPTR(pScrn);
1633
1634         pNv->FB = NVAllocateMemory(pNv, NOUVEAU_MEM_FB, pNv->VRAMPhysicalSize/2);
1635         if (!pNv->FB) {
1636                 ErrorF("Failed to allocate memory for framebuffer!\n");
1637                 return FALSE;
1638         }
1639         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1640                    "Allocated %dMiB VRAM for framebuffer + offscreen pixmaps\n",
1641                    (unsigned int)(pNv->FB->size >> 20));
1642
1643         /*XXX: have to get these after we've allocated something, otherwise
1644          *     they're uninitialised in the DRM!
1645          */
1646         pNv->VRAMSize     = NVDRMGetParam(pNv, NOUVEAU_GETPARAM_FB_SIZE);
1647         pNv->VRAMPhysical = NVDRMGetParam(pNv, NOUVEAU_GETPARAM_FB_PHYSICAL);
1648         pNv->AGPSize      = NVDRMGetParam(pNv, NOUVEAU_GETPARAM_AGP_SIZE);
1649         pNv->AGPPhysical  = NVDRMGetParam(pNv, NOUVEAU_GETPARAM_AGP_PHYSICAL);
1650         if ( ! pNv->AGPSize ) /*if no AGP*/
1651                 /*use PCI*/
1652                 pNv->SGPhysical  = NVDRMGetParam(pNv, NOUVEAU_GETPARAM_PCI_PHYSICAL);
1653
1654         int gart_scratch_size;
1655
1656         if (pNv->AGPSize) {
1657                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1658                            "AGPGART: %dMiB available\n",
1659                            (unsigned int)(pNv->AGPSize >> 20));
1660
1661                 if (pNv->AGPSize > (16*1024*1024))
1662                         gart_scratch_size = 16*1024*1024;
1663                 else
1664                         gart_scratch_size = pNv->AGPSize;
1665
1666                 }
1667         else {
1668
1669                 gart_scratch_size = (4 << 20) - (1 << 18) ;
1670                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1671                            "GART: PCI DMA - using %dKiB\n", gart_scratch_size >> 10);
1672                 
1673         }
1674
1675         /*The DRM allocates AGP memory, PCI as a fallback */
1676         pNv->GARTScratch = NVAllocateMemory(pNv, NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI_ACCEPTABLE,
1677                                                         gart_scratch_size);
1678         if (!pNv->GARTScratch) {
1679                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1680                            "Unable to allocate GART memory\n");
1681         } else {
1682                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1683                            "GART: mapped %dMiB at %p, offset is %d\n",
1684                            (unsigned int)(pNv->GARTScratch->size >> 20),
1685                            pNv->GARTScratch->map, pNv->GARTScratch->offset);
1686         }
1687
1688
1689         pNv->Cursor = NVAllocateMemory(pNv, NOUVEAU_MEM_FB, 64*1024);
1690         if (!pNv->Cursor) {
1691                 ErrorF("Failed to allocate memory for hardware cursor\n");
1692                 return FALSE;
1693         }
1694
1695         pNv->ScratchBuffer = NVAllocateMemory(pNv, NOUVEAU_MEM_FB,
1696                         pNv->Architecture <NV_ARCH_10 ? 8192 : 16384);
1697         if (!pNv->ScratchBuffer) {
1698                 ErrorF("Failed to allocate memory for scratch buffer\n");
1699                 return FALSE;
1700         }
1701
1702         if (pNv->Architecture >= NV_ARCH_50) {
1703                 pNv->CLUT = NVAllocateMemory(pNv, NOUVEAU_MEM_FB, 0x1000);
1704                 if (!pNv->CLUT) {
1705                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1706                                    "Failed to allocate memory for CLUT\n");
1707                         return FALSE;
1708                 }
1709         }
1710
1711         return TRUE;
1712 }
1713
1714 /*
1715  * Unmap the framebuffer and MMIO memory.
1716  */
1717
1718 static Bool
1719 NVUnmapMem(ScrnInfoPtr pScrn)
1720 {
1721         NVPtr pNv = NVPTR(pScrn);
1722
1723         NVFreeMemory(pNv, pNv->FB);
1724         NVFreeMemory(pNv, pNv->ScratchBuffer);
1725         NVFreeMemory(pNv, pNv->Cursor);
1726
1727     return TRUE;
1728 }
1729
1730
1731 /*
1732  * Initialise a new mode. 
1733  */
1734
1735 static Bool
1736 NVModeInit(ScrnInfoPtr pScrn, DisplayModePtr mode)
1737 {
1738     vgaHWPtr hwp = VGAHWPTR(pScrn);
1739     vgaRegPtr vgaReg;
1740     NVPtr pNv = NVPTR(pScrn);
1741     NVRegPtr nvReg;
1742
1743     /* Initialise the ModeReg values */
1744     if (!vgaHWInit(pScrn, mode))
1745         return FALSE;
1746     pScrn->vtSema = TRUE;
1747
1748     vgaReg = &hwp->ModeReg;
1749     nvReg = &pNv->ModeReg;
1750
1751     if(!NVDACInit(pScrn, mode))
1752         return FALSE;
1753
1754     NVLockUnlock(pNv, 0);
1755     if(pNv->twoHeads) {
1756         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, nvReg->crtcOwner);
1757         NVLockUnlock(pNv, 0);
1758     }
1759
1760     /* Program the registers */
1761     vgaHWProtect(pScrn, TRUE);
1762
1763     NVDACRestore(pScrn, vgaReg, nvReg, FALSE);
1764
1765 #if X_BYTE_ORDER == X_BIG_ENDIAN
1766     /* turn on LFB swapping */
1767     {
1768         unsigned char tmp;
1769
1770         tmp = nvReadVGA(pNv, NV_VGA_CRTCX_SWAPPING);
1771         tmp |= (1 << 7);
1772         nvWriteVGA(pNv, NV_VGA_CRTCX_SWAPPING, tmp);
1773     }
1774 #endif
1775
1776     if (!pNv->NoAccel)
1777             NVResetGraphics(pScrn);
1778
1779     vgaHWProtect(pScrn, FALSE);
1780
1781     pNv->CurrentLayout.mode = mode;
1782
1783     return TRUE;
1784 }
1785
1786 /*
1787  * Restore the initial (text) mode.
1788  */
1789 static void 
1790 NVRestore(ScrnInfoPtr pScrn)
1791 {
1792         vgaHWPtr hwp = VGAHWPTR(pScrn);
1793         vgaRegPtr vgaReg = &hwp->SavedReg;
1794         NVPtr pNv = NVPTR(pScrn);
1795         NVRegPtr nvReg = &pNv->SavedReg;
1796
1797         if (pNv->randr12_enable) {
1798                 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1799                 int i;
1800                 int vgaflags = VGA_SR_CMAP | VGA_SR_MODE;
1801
1802                 for (i = 0; i < xf86_config->num_crtc; i++) {
1803                         NVCrtcLockUnlock(xf86_config->crtc[i], 0);
1804                 }
1805
1806                 for (i = 0; i < xf86_config->num_crtc; i++) {
1807                         xf86_config->crtc[i]->funcs->restore(xf86_config->crtc[i]);
1808                 }
1809
1810                 for (i = 0; i < xf86_config->num_output; i++) {
1811                         xf86_config->output[i]->funcs->restore(xf86_config->
1812                                                                output[i]);
1813                 }
1814
1815 #ifndef __powerpc__
1816                 vgaflags |= VGA_SR_FONTS;
1817 #endif
1818                 vgaHWRestore(pScrn, vgaReg, vgaflags);
1819                 vgaHWLock(hwp);
1820
1821                 for (i = 0; i < xf86_config->num_crtc; i++) {
1822                         NVCrtcLockUnlock(xf86_config->crtc[i], 1);
1823                 }
1824         } else {
1825                 NVLockUnlock(pNv, 0);
1826
1827                 if(pNv->twoHeads) {
1828                         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->CRTCnumber * 0x3);
1829                         NVLockUnlock(pNv, 0);
1830                 }
1831
1832                 /* Only restore text mode fonts/text for the primary card */
1833                 vgaHWProtect(pScrn, TRUE);
1834                 NVDACRestore(pScrn, vgaReg, nvReg, pNv->Primary);
1835                 if(pNv->twoHeads) {
1836                         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
1837                 }
1838                 vgaHWProtect(pScrn, FALSE);
1839         }
1840 }
1841
1842
1843 #define DEPTH_SHIFT(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
1844 #define MAKE_INDEX(in, w) (DEPTH_SHIFT(in, w) * 3)
1845
1846 static void
1847 NVLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1848               LOCO * colors, VisualPtr pVisual)
1849 {
1850         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1851         int c;
1852         NVPtr pNv = NVPTR(pScrn);
1853         int i, index;
1854
1855         for (c = 0; c < xf86_config->num_crtc; c++) {
1856                 xf86CrtcPtr crtc = xf86_config->crtc[c];
1857                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1858                 NVCrtcRegPtr regp;
1859
1860                 regp = &pNv->ModeReg.crtc_reg[nv_crtc->crtc];
1861
1862                 if (crtc->enabled == 0)
1863                         continue;
1864
1865                 switch (pNv->CurrentLayout.depth) {
1866                 case 15:
1867                         for (i = 0; i < numColors; i++) {
1868                                 index = indices[i];
1869                                 regp->DAC[MAKE_INDEX(index, 5) + 0] =
1870                                     colors[index].red;
1871                                 regp->DAC[MAKE_INDEX(index, 5) + 1] =
1872                                     colors[index].green;
1873                                 regp->DAC[MAKE_INDEX(index, 5) + 2] =
1874                                     colors[index].blue;
1875                         }
1876                         break;
1877                 case 16:
1878                         for (i = 0; i < numColors; i++) {
1879                                 index = indices[i];
1880                                 regp->DAC[MAKE_INDEX(index, 6) + 1] =
1881                                     colors[index].green;
1882                                 if (index < 32) {
1883                                         regp->DAC[MAKE_INDEX(index, 5) +
1884                                                   0] = colors[index].red;
1885                                         regp->DAC[MAKE_INDEX(index, 5) +
1886                                                   2] = colors[index].blue;
1887                                 }
1888                         }
1889                         break;
1890                 default:
1891                         for (i = 0; i < numColors; i++) {
1892                                 index = indices[i];
1893                                 regp->DAC[index * 3] = colors[index].red;
1894                                 regp->DAC[(index * 3) + 1] =
1895                                     colors[index].green;
1896                                 regp->DAC[(index * 3) + 2] =
1897                                     colors[index].blue;
1898                         }
1899                         break;
1900                 }
1901
1902                 NVCrtcLoadPalette(crtc);
1903         }
1904 }
1905
1906 //#define DEPTH_SHIFT(val, w) ((val << (8 - w)) | (val >> ((w << 1) - 8)))
1907 #define COLOR(c) (unsigned int)(0x3fff * ((c)/255.0))
1908 static void
1909 NV50LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
1910                 LOCO * colors, VisualPtr pVisual)
1911 {
1912         NVPtr pNv = NVPTR(pScrn);
1913         int i, index;
1914         volatile struct {
1915                 unsigned short red, green, blue, unused;
1916         } *lut = (void *) pNv->CLUT->map;
1917
1918         switch (pScrn->depth) {
1919         case 15:
1920                 for (i = 0; i < numColors; i++) {
1921                         index = indices[i];
1922                         lut[DEPTH_SHIFT(index, 5)].red =
1923                             COLOR(colors[index].red);
1924                         lut[DEPTH_SHIFT(index, 5)].green =
1925                             COLOR(colors[index].green);
1926                         lut[DEPTH_SHIFT(index, 5)].blue =
1927                             COLOR(colors[index].blue);
1928                 }
1929                 break;
1930         case 16:
1931                 for (i = 0; i < numColors; i++) {
1932                         index = indices[i];
1933                         lut[DEPTH_SHIFT(index, 6)].green =
1934                             COLOR(colors[index].green);
1935                         if (index < 32) {
1936                                 lut[DEPTH_SHIFT(index, 5)].red =
1937                                     COLOR(colors[index].red);
1938                                 lut[DEPTH_SHIFT(index, 5)].blue =
1939                                     COLOR(colors[index].blue);
1940                         }
1941                 }
1942                 break;
1943         default:
1944                 for (i = 0; i < numColors; i++) {
1945                         index = indices[i];
1946                         lut[index].red = COLOR(colors[index].red);
1947                         lut[index].green = COLOR(colors[index].green);
1948                         lut[index].blue = COLOR(colors[index].blue);
1949                 }
1950                 break;
1951         }
1952 }
1953
1954
1955 static void NVBacklightEnable(NVPtr pNv,  Bool on)
1956 {
1957     /* This is done differently on each laptop.  Here we
1958        define the ones we know for sure. */
1959
1960 #if defined(__powerpc__)
1961     if((pNv->Chipset == 0x10DE0179) || 
1962        (pNv->Chipset == 0x10DE0189) || 
1963        (pNv->Chipset == 0x10DE0329))
1964     {
1965        /* NV17,18,34 Apple iMac, iBook, PowerBook */
1966       CARD32 tmp_pmc, tmp_pcrt;
1967       tmp_pmc = nvReadMC(pNv, 0x10F0) & 0x7FFFFFFF;
1968       tmp_pcrt = nvReadCRTC0(pNv, NV_CRTC_081C) & 0xFFFFFFFC;
1969       if(on) {
1970           tmp_pmc |= (1 << 31);
1971           tmp_pcrt |= 0x1;
1972       }
1973       nvWriteMC(pNv, 0x10F0, tmp_pmc);
1974       nvWriteCRTC0(pNv, NV_CRTC_081C, tmp_pcrt);
1975     }
1976 #endif
1977     
1978     if(pNv->LVDS) {
1979        if(pNv->twoHeads && ((pNv->Chipset & 0x0ff0) != CHIPSET_NV11)) {
1980            nvWriteMC(pNv, 0x130C, on ? 3 : 7);
1981        }
1982     } else {
1983        CARD32 fpcontrol;
1984
1985        fpcontrol = nvReadCurRAMDAC(pNv, 0x848) & 0xCfffffCC;
1986
1987        /* cut the TMDS output */
1988        if(on) fpcontrol |= pNv->fpSyncs;
1989        else fpcontrol |= 0x20000022;
1990
1991        nvWriteCurRAMDAC(pNv, 0x0848, fpcontrol);
1992     }
1993 }
1994
1995 static void
1996 NVDPMSSetLCD(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
1997 {
1998   NVPtr pNv = NVPTR(pScrn);
1999
2000   if (!pScrn->vtSema) return;
2001
2002   vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2003
2004   switch (PowerManagementMode) {
2005   case DPMSModeStandby:  /* HSync: Off, VSync: On */
2006   case DPMSModeSuspend:  /* HSync: On, VSync: Off */
2007   case DPMSModeOff:      /* HSync: Off, VSync: Off */
2008     NVBacklightEnable(pNv, 0);
2009     break;
2010   case DPMSModeOn:       /* HSync: On, VSync: On */
2011     NVBacklightEnable(pNv, 1);
2012   default:
2013     break;
2014   }
2015 }
2016
2017
2018 static void
2019 NVDPMSSet(ScrnInfoPtr pScrn, int PowerManagementMode, int flags)
2020 {
2021   unsigned char crtc1A;
2022   vgaHWPtr hwp = VGAHWPTR(pScrn);
2023
2024   if (!pScrn->vtSema) return;
2025
2026   crtc1A = hwp->readCrtc(hwp, 0x1A) & ~0xC0;
2027
2028   switch (PowerManagementMode) {
2029   case DPMSModeStandby:  /* HSync: Off, VSync: On */
2030     crtc1A |= 0x80;
2031     break;
2032   case DPMSModeSuspend:  /* HSync: On, VSync: Off */
2033     crtc1A |= 0x40;
2034     break;
2035   case DPMSModeOff:      /* HSync: Off, VSync: Off */
2036     crtc1A |= 0xC0;
2037     break;
2038   case DPMSModeOn:       /* HSync: On, VSync: On */
2039   default:
2040     break;
2041   }
2042
2043   /* vgaHWDPMSSet will merely cut the dac output */
2044   vgaHWDPMSSet(pScrn, PowerManagementMode, flags);
2045
2046   hwp->writeCrtc(hwp, 0x1A, crtc1A);
2047 }
2048
2049
2050 /* Mandatory */
2051
2052 /* This gets called at the start of each server generation */
2053
2054 static Bool
2055 NVScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
2056 {
2057     ScrnInfoPtr pScrn;
2058     vgaHWPtr hwp;
2059     NVPtr pNv;
2060     int ret;
2061     VisualPtr visual;
2062     unsigned char *FBStart;
2063     int width, height, displayWidth, shadowHeight;
2064
2065     /* 
2066      * First get the ScrnInfoRec
2067      */
2068     pScrn = xf86Screens[pScreen->myNum];
2069
2070     hwp = VGAHWPTR(pScrn);
2071     pNv = NVPTR(pScrn);
2072
2073     /* Map the VGA memory when the primary video */
2074     if (pNv->Primary) {
2075         hwp->MapSize = 0x10000;
2076         if (!vgaHWMapMem(pScrn))
2077             return FALSE;
2078     }
2079     
2080     /* First init DRI/DRM */
2081     if (!NVDRIScreenInit(pScrn))
2082         return FALSE;
2083     
2084     ret = drmCommandNone(pNv->drm_fd, DRM_NOUVEAU_CARD_INIT);
2085     if (ret) {
2086         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2087                    "Error initialising the nouveau kernel module: %d\n",
2088                    ret);
2089         return FALSE;
2090     }
2091     
2092     /* Allocate and map memory areas we need */
2093     if (!NVMapMem(pScrn))
2094         return FALSE;
2095     
2096     if (!pNv->NoAccel) {
2097         /* Init DRM - Alloc FIFO */
2098         if (!NVInitDma(pScrn))
2099             return FALSE;
2100         
2101         /* setup graphics objects */
2102         if (!NVAccelCommonInit(pScrn))
2103             return FALSE;
2104     }
2105     
2106     if (!pNv->randr12_enable) {
2107         /* Save the current state */
2108         NVSave(pScrn);
2109         /* Initialise the first mode */
2110         if (!NVModeInit(pScrn, pScrn->currentMode))
2111             return FALSE;
2112
2113         /* Darken the screen for aesthetic reasons and set the viewport */
2114         
2115         NVSaveScreen(pScreen, SCREEN_SAVER_ON);
2116         pScrn->AdjustFrame(scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
2117         
2118     } else {
2119         pScrn->memPhysBase = pNv->VRAMPhysical;
2120         pScrn->fbOffset = 0;
2121         
2122         if (!NVEnterVT(scrnIndex, 0))
2123             return FALSE;
2124     }
2125
2126
2127     /*
2128      * The next step is to setup the screen's visuals, and initialise the
2129      * framebuffer code.  In cases where the framebuffer's default
2130      * choices for things like visual layouts and bits per RGB are OK,
2131      * this may be as simple as calling the framebuffer's ScreenInit()
2132      * function.  If not, the visuals will need to be setup before calling
2133      * a fb ScreenInit() function and fixed up after.
2134      *
2135      * For most PC hardware at depths >= 8, the defaults that fb uses
2136      * are not appropriate.  In this driver, we fixup the visuals after.
2137      */
2138
2139     /*
2140      * Reset the visual list.
2141      */
2142     miClearVisualTypes();
2143
2144     /* Setup the visuals we support. */
2145
2146     if (!miSetVisualTypes(pScrn->depth, 
2147                           miGetDefaultVisualMask(pScrn->depth), 8,
2148                           pScrn->defaultVisual))
2149           return FALSE;
2150     if (!miSetPixmapDepths ()) return FALSE;
2151
2152     /*
2153      * Call the framebuffer layer's ScreenInit function, and fill in other
2154      * pScreen fields.
2155      */
2156
2157     width = pScrn->virtualX;
2158     height = pScrn->virtualY;
2159     displayWidth = pScrn->displayWidth;
2160
2161
2162     if(pNv->Rotate) {
2163         height = pScrn->virtualX;
2164         width = pScrn->virtualY;
2165     }
2166
2167     /* If RandR rotation is enabled, leave enough space in the
2168      * framebuffer for us to rotate the screen dimensions without
2169      * changing the pitch.
2170      */
2171     if(pNv->RandRRotation)
2172         shadowHeight = max(width, height);
2173     else
2174         shadowHeight = height;
2175
2176     if(pNv->ShadowFB) {
2177         pNv->ShadowPitch = BitmapBytePad(pScrn->bitsPerPixel * width);
2178         pNv->ShadowPtr = xalloc(pNv->ShadowPitch * shadowHeight);
2179         displayWidth = pNv->ShadowPitch / (pScrn->bitsPerPixel >> 3);
2180         FBStart = pNv->ShadowPtr;
2181     } else {
2182         pNv->ShadowPtr = NULL;
2183         FBStart = pNv->FB->map;
2184     }
2185
2186     switch (pScrn->bitsPerPixel) {
2187         case 8:
2188         case 16:
2189         case 32:
2190             ret = fbScreenInit(pScreen, FBStart, width, height,
2191                                pScrn->xDpi, pScrn->yDpi,
2192                                displayWidth, pScrn->bitsPerPixel);
2193             break;
2194         default:
2195             xf86DrvMsg(scrnIndex, X_ERROR,
2196                        "Internal error: invalid bpp (%d) in NVScreenInit\n",
2197                        pScrn->bitsPerPixel);
2198             ret = FALSE;
2199             break;
2200     }
2201     if (!ret)
2202         return FALSE;
2203
2204     if (pScrn->bitsPerPixel > 8) {
2205         /* Fixup RGB ordering */
2206         visual = pScreen->visuals + pScreen->numVisuals;
2207         while (--visual >= pScreen->visuals) {
2208             if ((visual->class | DynamicClass) == DirectColor) {
2209                 visual->offsetRed = pScrn->offset.red;
2210                 visual->offsetGreen = pScrn->offset.green;
2211                 visual->offsetBlue = pScrn->offset.blue;
2212                 visual->redMask = pScrn->mask.red;
2213                 visual->greenMask = pScrn->mask.green;
2214                 visual->blueMask = pScrn->mask.blue;
2215             }
2216         }
2217     }
2218
2219     fbPictureInit (pScreen, 0, 0);
2220     
2221     xf86SetBlackWhitePixels(pScreen);
2222
2223     if (!pNv->NoAccel) {
2224             NVExaInit(pScreen);
2225             NVResetGraphics(pScrn);
2226     }
2227     
2228     miInitializeBackingStore(pScreen);
2229     xf86SetBackingStore(pScreen);
2230     xf86SetSilkenMouse(pScreen);
2231
2232     /* Finish DRI init */
2233     NVDRIFinishScreenInit(pScrn);
2234
2235     /* Initialize software cursor.  
2236         Must precede creation of the default colormap */
2237     miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
2238
2239     /* Initialize HW cursor layer. 
2240         Must follow software cursor initialization*/
2241     if (pNv->HWCursor) { 
2242         if (pNv->Architecture < NV_ARCH_50)
2243             ret = NVCursorInit(pScreen);
2244         else
2245             ret = NV50CursorInit(pScreen);
2246
2247         if (ret != TRUE) {
2248             xf86DrvMsg(pScrn->scrnIndex, X_ERROR, 
2249                 "Hardware cursor initialization failed\n");
2250             pNv->HWCursor = FALSE;
2251         }
2252     }
2253
2254     /* Initialise default colourmap */
2255     if (!miCreateDefColormap(pScreen))
2256         return FALSE;
2257
2258     /* Initialize colormap layer.  
2259        Must follow initialization of the default colormap */
2260     if (!pNv->randr12_enable) {
2261         if(!xf86HandleColormaps(pScreen, 256, 8, NVDACLoadPalette,
2262                                 NULL, CMAP_RELOAD_ON_MODE_SWITCH | CMAP_PALETTED_TRUECOLOR))
2263             return FALSE;
2264     } else {
2265         if (pNv->Architecture < NV_ARCH_50) {
2266             if (!xf86HandleColormaps(pScreen, 256, 8, NVLoadPalette,
2267                                      NULL,
2268                                      CMAP_RELOAD_ON_MODE_SWITCH |
2269                                      CMAP_PALETTED_TRUECOLOR))
2270                 return FALSE;
2271         } else {
2272             if (!xf86HandleColormaps(pScreen, 256, 8, NV50LoadPalette,
2273                                      NULL, CMAP_PALETTED_TRUECOLOR))
2274                 return FALSE;
2275         }
2276     }
2277
2278     if (pNv->randr12_enable) {
2279         xf86DPMSInit(pScreen, xf86DPMSSet, 0);
2280         
2281         if (!xf86CrtcScreenInit(pScreen))
2282             return FALSE;
2283
2284         pNv->PointerMoved = pScrn->PointerMoved;
2285         pScrn->PointerMoved = NVPointerMoved;
2286     }
2287
2288     if(pNv->ShadowFB) {
2289         RefreshAreaFuncPtr refreshArea = NVRefreshArea;
2290
2291         if(pNv->Rotate || pNv->RandRRotation) {
2292            pNv->PointerMoved = pScrn->PointerMoved;
2293            if(pNv->Rotate)
2294                pScrn->PointerMoved = NVPointerMoved;
2295
2296            switch(pScrn->bitsPerPixel) {
2297                case 8:  refreshArea = NVRefreshArea8;   break;
2298                case 16: refreshArea = NVRefreshArea16;  break;
2299                case 32: refreshArea = NVRefreshArea32;  break;
2300            }
2301            if(!pNv->RandRRotation) {
2302                xf86DisableRandR();
2303                xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2304                           "Driver rotation enabled, RandR disabled\n");
2305            }
2306         }
2307
2308         ShadowFBInit(pScreen, refreshArea);
2309     }
2310
2311     if (!pNv->randr12_enable) {
2312         if(pNv->FlatPanel)
2313             xf86DPMSInit(pScreen, NVDPMSSetLCD, 0);
2314         else
2315             xf86DPMSInit(pScreen, NVDPMSSet, 0);
2316     }
2317
2318     pScrn->memPhysBase = pNv->VRAMPhysical;
2319     pScrn->fbOffset = 0;
2320
2321     if(pNv->Rotate == 0 && !pNv->RandRRotation)
2322        NVInitVideo(pScreen);
2323
2324     pScreen->SaveScreen = NVSaveScreen;
2325
2326     /* Wrap the current CloseScreen function */
2327     pNv->CloseScreen = pScreen->CloseScreen;
2328     pScreen->CloseScreen = NVCloseScreen;
2329
2330     pNv->BlockHandler = pScreen->BlockHandler;
2331     pScreen->BlockHandler = NVBlockHandler;
2332
2333 #ifdef RANDR
2334     /* Install our DriverFunc.  We have to do it this way instead of using the
2335      * HaveDriverFuncs argument to xf86AddDriver, because InitOutput clobbers
2336      * pScrn->DriverFunc */
2337     if (!pNv->randr12_enable)
2338         pScrn->DriverFunc = NVDriverFunc;
2339 #endif
2340
2341     /* Report any unused options (only for the first generation) */
2342     if (serverGeneration == 1) {
2343         xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
2344     }
2345     return TRUE;
2346 }
2347
2348 static Bool
2349 NVSaveScreen(ScreenPtr pScreen, int mode)
2350 {
2351     ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
2352     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2353     NVPtr pNv = NVPTR(pScrn);
2354     int i;
2355     Bool on = xf86IsUnblank(mode);
2356     
2357     if (pNv->randr12_enable) {
2358         if (pScrn->vtSema) {
2359             for (i = 0; i < xf86_config->num_crtc; i++) {
2360                 
2361                 if (xf86_config->crtc[i]->enabled) {
2362                     NVCrtcBlankScreen(xf86_config->crtc[i],
2363                                       on);
2364                 }
2365             }
2366             
2367         }
2368         return TRUE;
2369     } else
2370         return vgaHWSaveScreen(pScreen, mode);
2371 }
2372
2373 static void
2374 NVSave(ScrnInfoPtr pScrn)
2375 {
2376     NVPtr pNv = NVPTR(pScrn);
2377     NVRegPtr nvReg = &pNv->SavedReg;
2378     vgaHWPtr pVga = VGAHWPTR(pScrn);
2379     vgaRegPtr vgaReg = &pVga->SavedReg;
2380     int i;
2381
2382     if (pNv->randr12_enable) {
2383         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
2384         int vgaflags = VGA_SR_CMAP | VGA_SR_MODE;
2385
2386         for (i = 0; i < xf86_config->num_crtc; i++) {
2387                 xf86_config->crtc[i]->funcs->save(xf86_config->crtc[i]);
2388         }
2389
2390         for (i = 0; i < xf86_config->num_output; i++) {
2391                 xf86_config->output[i]->funcs->save(xf86_config->
2392                                                     output[i]);
2393         }
2394
2395         vgaHWUnlock(pVga);
2396 #ifndef __powerpc__
2397         vgaflags |= VGA_SR_FONTS;
2398 #endif
2399         vgaHWSave(pScrn, vgaReg, vgaflags);
2400     } else {
2401         NVLockUnlock(pNv, 0);
2402         if(pNv->twoHeads) {
2403             nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->CRTCnumber * 0x3);
2404             NVLockUnlock(pNv, 0);
2405         }
2406
2407         NVDACSave(pScrn, vgaReg, nvReg, pNv->Primary);
2408     }
2409 }
2410
2411 #ifdef RANDR
2412 static Bool
2413 NVRandRGetInfo(ScrnInfoPtr pScrn, Rotation *rotations)
2414 {
2415     NVPtr pNv = NVPTR(pScrn);
2416
2417     if(pNv->RandRRotation)
2418        *rotations = RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_270;
2419     else
2420        *rotations = RR_Rotate_0;
2421
2422     return TRUE;
2423 }
2424
2425 static Bool
2426 NVRandRSetConfig(ScrnInfoPtr pScrn, xorgRRConfig *config)
2427 {
2428     NVPtr pNv = NVPTR(pScrn);
2429
2430     switch(config->rotation) {
2431         case RR_Rotate_0:
2432             pNv->Rotate = 0;
2433             pScrn->PointerMoved = pNv->PointerMoved;
2434             break;
2435
2436         case RR_Rotate_90:
2437             pNv->Rotate = -1;
2438             pScrn->PointerMoved = NVPointerMoved;
2439             break;
2440
2441         case RR_Rotate_270:
2442             pNv->Rotate = 1;
2443             pScrn->PointerMoved = NVPointerMoved;
2444             break;
2445
2446         default:
2447             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2448                     "Unexpected rotation in NVRandRSetConfig!\n");
2449             pNv->Rotate = 0;
2450             pScrn->PointerMoved = pNv->PointerMoved;
2451             return FALSE;
2452     }
2453
2454     return TRUE;
2455 }
2456
2457 static Bool
2458 NVDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op, pointer data)
2459 {
2460     switch(op) {
2461        case RR_GET_INFO:
2462           return NVRandRGetInfo(pScrn, (Rotation*)data);
2463        case RR_SET_CONFIG:
2464           return NVRandRSetConfig(pScrn, (xorgRRConfig*)data);
2465        default:
2466           return FALSE;
2467     }
2468
2469     return FALSE;
2470 }
2471 #endif