randr12: Small fix for NV3x, more will be needed.
[nouveau] / src / nv_crtc.c
1 /*
2  * Copyright 2006 Dave Airlie
3  * Copyright 2007 Maarten Maathuis
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * 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  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * this code uses ideas taken from the NVIDIA nv driver - the nvidia license
26  * decleration is at the bottom of this file as it is rather ugly 
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include "xf86.h"
35 #include "os.h"
36 #include "mibank.h"
37 #include "globals.h"
38 #include "xf86.h"
39 #include "xf86Priv.h"
40 #include "xf86DDC.h"
41 #include "mipointer.h"
42 #include "windowstr.h"
43 #include <randrstr.h>
44 #include <X11/extensions/render.h>
45
46 #include "xf86Crtc.h"
47 #include "nv_include.h"
48
49 #include "vgaHW.h"
50
51 #define CRTC_INDEX 0x3d4
52 #define CRTC_DATA 0x3d5
53 #define CRTC_IN_STAT_1 0x3da
54
55 #define WHITE_VALUE 0x3F
56 #define BLACK_VALUE 0x00
57 #define OVERSCAN_VALUE 0x01
58
59 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
60 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state, Bool override);
61 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
62 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
63 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
64 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
65
66 static uint8_t NVReadPVIO(xf86CrtcPtr crtc, uint32_t address)
67 {
68         ScrnInfoPtr pScrn = crtc->scrn;
69         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
70         NVPtr pNv = NVPTR(pScrn);
71
72         /* Only NV4x have two pvio ranges */
73         if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
74                 return NV_RD08(pNv->PVIO1, address);
75         } else {
76                 return NV_RD08(pNv->PVIO0, address);
77         }
78 }
79
80 static void NVWritePVIO(xf86CrtcPtr crtc, uint32_t address, uint8_t value)
81 {
82         ScrnInfoPtr pScrn = crtc->scrn;
83         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
84         NVPtr pNv = NVPTR(pScrn);
85
86         /* Only NV4x have two pvio ranges */
87         if (nv_crtc->head == 1 && pNv->Architecture == NV_ARCH_40) {
88                 NV_WR08(pNv->PVIO1, address, value);
89         } else {
90                 NV_WR08(pNv->PVIO0, address, value);
91         }
92 }
93
94 static void NVWriteMiscOut(xf86CrtcPtr crtc, uint8_t value)
95 {
96         NVWritePVIO(crtc, VGA_MISC_OUT_W, value);
97 }
98
99 static uint8_t NVReadMiscOut(xf86CrtcPtr crtc)
100 {
101         return NVReadPVIO(crtc, VGA_MISC_OUT_R);
102 }
103
104 void NVWriteVGA(NVPtr pNv, int head, uint8_t index, uint8_t value)
105 {
106         volatile uint8_t *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
107
108 #ifdef NOUVEAU_MODESET_TRACE
109         ErrorF("NVWriteVGA: idx %d data 0x%x head %d\n", index, value, head);
110 #endif
111
112         NV_WR08(pCRTCReg, CRTC_INDEX, index);
113         NV_WR08(pCRTCReg, CRTC_DATA, value);
114 }
115
116 uint8_t NVReadVGA(NVPtr pNv, int head, uint8_t index)
117 {
118         volatile uint8_t *pCRTCReg = head ? pNv->PCIO1 : pNv->PCIO0;
119
120         NV_WR08(pCRTCReg, CRTC_INDEX, index);
121         return NV_RD08(pCRTCReg, CRTC_DATA);
122 }
123
124 /* CR57 and CR58 are a fun pair of regs. CR57 provides an index (0-0xf) for CR58
125  * I suspect they in fact do nothing, but are merely a way to carry useful
126  * per-head variables around
127  *
128  * Known uses:
129  * CR57         CR58
130  * 0x00         index to the appropriate dcb entry (or 7f for inactive)
131  * 0x02         dcb entry's "or" value (or 00 for inactive)
132  * 0x03         bit0 set for dual link (LVDS, possibly elsewhere too)
133  * 0x08 or 0x09 pxclk in MHz
134  * 0x0f         laptop panel info -     low nibble for PEXTDEV_BOOT strap
135  *                                      high nibble for xlat strap value
136  */
137
138 void NVWriteVGACR5758(NVPtr pNv, int head, uint8_t index, uint8_t value)
139 {
140         NVWriteVGA(pNv, head, 0x57, index);
141         NVWriteVGA(pNv, head, 0x58, value);
142 }
143
144 uint8_t NVReadVGACR5758(NVPtr pNv, int head, uint8_t index)
145 {
146         NVWriteVGA(pNv, head, 0x57, index);
147         return NVReadVGA(pNv, head, 0x58);
148 }
149
150 void NVWriteVgaCrtc(xf86CrtcPtr crtc, uint8_t index, uint8_t value)
151 {
152         ScrnInfoPtr pScrn = crtc->scrn;
153         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
154         NVPtr pNv = NVPTR(pScrn);
155
156         NVWriteVGA(pNv, nv_crtc->head, index, value);
157 }
158
159 uint8_t NVReadVgaCrtc(xf86CrtcPtr crtc, uint8_t index)
160 {
161         ScrnInfoPtr pScrn = crtc->scrn;
162         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
163         NVPtr pNv = NVPTR(pScrn);
164
165         return NVReadVGA(pNv, nv_crtc->head, index);
166 }
167
168 static void NVWriteVgaSeq(xf86CrtcPtr crtc, uint8_t index, uint8_t value)
169 {
170         NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
171         NVWritePVIO(crtc, VGA_SEQ_DATA, value);
172 }
173
174 static uint8_t NVReadVgaSeq(xf86CrtcPtr crtc, uint8_t index)
175 {
176         NVWritePVIO(crtc, VGA_SEQ_INDEX, index);
177         return NVReadPVIO(crtc, VGA_SEQ_DATA);
178 }
179
180 static void NVWriteVgaGr(xf86CrtcPtr crtc, uint8_t index, uint8_t value)
181 {
182         NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
183         NVWritePVIO(crtc, VGA_GRAPH_DATA, value);
184 }
185
186 static uint8_t NVReadVgaGr(xf86CrtcPtr crtc, uint8_t index)
187 {
188         NVWritePVIO(crtc, VGA_GRAPH_INDEX, index);
189         return NVReadPVIO(crtc, VGA_GRAPH_DATA);
190
191
192
193 static void NVWriteVgaAttr(xf86CrtcPtr crtc, uint8_t index, uint8_t value)
194 {
195   ScrnInfoPtr pScrn = crtc->scrn;
196   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
197   NVPtr pNv = NVPTR(pScrn);
198   volatile uint8_t *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
199
200   NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
201   if (nv_crtc->paletteEnabled)
202     index &= ~0x20;
203   else
204     index |= 0x20;
205   NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
206   NV_WR08(pCRTCReg, VGA_ATTR_DATA_W, value);
207 }
208
209 static uint8_t NVReadVgaAttr(xf86CrtcPtr crtc, uint8_t index)
210 {
211   ScrnInfoPtr pScrn = crtc->scrn;
212   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
213   NVPtr pNv = NVPTR(pScrn);
214   volatile uint8_t *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
215
216   NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
217   if (nv_crtc->paletteEnabled)
218     index &= ~0x20;
219   else
220     index |= 0x20;
221   NV_WR08(pCRTCReg, VGA_ATTR_INDEX, index);
222   return NV_RD08(pCRTCReg, VGA_ATTR_DATA_R);
223 }
224
225 void NVCrtcSetOwner(xf86CrtcPtr crtc)
226 {
227         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
228         ScrnInfoPtr pScrn = crtc->scrn;
229         NVPtr pNv = NVPTR(pScrn);
230         /* Non standard beheaviour required by NV11 */
231         if (pNv) {
232                 uint8_t owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
233                 ErrorF("pre-Owner: 0x%X\n", owner);
234                 if (owner == 0x04) {
235                         uint32_t pbus84 = nvReadMC(pNv, 0x1084);
236                         ErrorF("pbus84: 0x%X\n", pbus84);
237                         pbus84 &= ~(1<<28);
238                         ErrorF("pbus84: 0x%X\n", pbus84);
239                         nvWriteMC(pNv, 0x1084, pbus84);
240                 }
241                 /* The blob never writes owner to pcio1, so should we */
242                 if (pNv->NVArch == 0x11) {
243                         NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, 0xff);
244                 }
245                 NVWriteVGA0(pNv, NV_VGA_CRTCX_OWNER, nv_crtc->head * 0x3);
246                 owner = NVReadVGA0(pNv, NV_VGA_CRTCX_OWNER);
247                 ErrorF("post-Owner: 0x%X\n", owner);
248         } else {
249                 ErrorF("pNv pointer is NULL\n");
250         }
251 }
252
253 static void
254 NVEnablePalette(xf86CrtcPtr crtc)
255 {
256   ScrnInfoPtr pScrn = crtc->scrn;
257   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
258   NVPtr pNv = NVPTR(pScrn);
259   volatile uint8_t *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
260
261   NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
262   NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0);
263   nv_crtc->paletteEnabled = TRUE;
264 }
265
266 static void
267 NVDisablePalette(xf86CrtcPtr crtc)
268 {
269   ScrnInfoPtr pScrn = crtc->scrn;
270   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
271   NVPtr pNv = NVPTR(pScrn);
272   volatile uint8_t *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
273
274   NV_RD08(pCRTCReg, CRTC_IN_STAT_1);
275   NV_WR08(pCRTCReg, VGA_ATTR_INDEX, 0x20);
276   nv_crtc->paletteEnabled = FALSE;
277 }
278
279 static void NVWriteVgaReg(xf86CrtcPtr crtc, uint32_t reg, uint8_t value)
280 {
281  ScrnInfoPtr pScrn = crtc->scrn;
282   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
283   NVPtr pNv = NVPTR(pScrn);
284   volatile uint8_t *pCRTCReg = nv_crtc->head ? pNv->PCIO1 : pNv->PCIO0;
285
286   NV_WR08(pCRTCReg, reg, value);
287 }
288
289 /* perform a sequencer reset */
290 static void NVVgaSeqReset(xf86CrtcPtr crtc, Bool start)
291 {
292   if (start)
293     NVWriteVgaSeq(crtc, 0x00, 0x1);
294   else
295     NVWriteVgaSeq(crtc, 0x00, 0x3);
296
297 }
298 static void NVVgaProtect(xf86CrtcPtr crtc, Bool on)
299 {
300         uint8_t tmp;
301
302         if (on) {
303                 tmp = NVReadVgaSeq(crtc, 0x1);
304                 NVVgaSeqReset(crtc, TRUE);
305                 NVWriteVgaSeq(crtc, 0x01, tmp | 0x20);
306
307                 NVEnablePalette(crtc);
308         } else {
309                 /*
310                  * Reenable sequencer, then turn on screen.
311                  */
312                 tmp = NVReadVgaSeq(crtc, 0x1);
313                 NVWriteVgaSeq(crtc, 0x01, tmp & ~0x20); /* reenable display */
314                 NVVgaSeqReset(crtc, FALSE);
315
316                 NVDisablePalette(crtc);
317         }
318 }
319
320 void NVCrtcLockUnlock(xf86CrtcPtr crtc, Bool Lock)
321 {
322         uint8_t cr11;
323
324         NVCrtcSetOwner(crtc);
325
326         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LOCK, Lock ? 0x99 : 0x57);
327         cr11 = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE);
328         if (Lock) cr11 |= 0x80;
329         else cr11 &= ~0x80;
330         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_VSYNCE, cr11);
331 }
332
333 xf86OutputPtr 
334 NVGetOutputFromCRTC(xf86CrtcPtr crtc)
335 {
336         ScrnInfoPtr pScrn = crtc->scrn;
337         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
338         int i;
339         for (i = 0; i < xf86_config->num_output; i++) {
340                 xf86OutputPtr output = xf86_config->output[i];
341
342                 if (output->crtc == crtc) {
343                         return output;
344                 }
345         }
346
347         return NULL;
348 }
349
350 xf86CrtcPtr
351 nv_find_crtc_by_index(ScrnInfoPtr pScrn, int index)
352 {
353         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
354         int i;
355
356         for (i = 0; i < xf86_config->num_crtc; i++) {
357                 xf86CrtcPtr crtc = xf86_config->crtc[i];
358                 NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
359                 if (nv_crtc->head == index)
360                         return crtc;
361         }
362
363         return NULL;
364 }
365
366 /*
367  * Calculate the Video Clock parameters for the PLL.
368  */
369 /* Code taken from NVClock, with permission of the author (being a GPL->MIT code transfer). */
370
371 static void
372 CalculateVClkNV4x_SingleVCO(NVPtr pNv, struct pll_lims *pll_lim, uint32_t clockIn, uint32_t *n1_best, uint32_t *m1_best, uint32_t *p_best)
373 {
374         uint32_t clock, M, N, P;
375         uint32_t delta, bestDelta, minM, maxM, minN, maxN, maxP;
376         uint32_t minVCOInputFreq, minVCOFreq, maxVCOFreq;
377         uint32_t VCOFreq;
378         uint32_t refClk = pNv->CrystalFreqKHz;
379         bestDelta = clockIn;
380
381         /* bios clocks are in MHz, we use KHz */
382         minVCOInputFreq = pll_lim->vco1.min_inputfreq*1000;
383         minVCOFreq = pll_lim->vco1.minfreq*1000;
384         maxVCOFreq = pll_lim->vco1.maxfreq*1000;
385         minM = pll_lim->vco1.min_m;
386         maxM = pll_lim->vco1.max_m;
387         minN = pll_lim->vco1.min_n;
388         maxN = pll_lim->vco1.max_n;
389
390         maxP = 6;
391
392         /* The optimal frequency for the PLL to work at is somewhere in the center of its range.
393         /  Choose a post divider in such a way to achieve this.
394         /  The G8x nv driver does something similar but they they derive a minP and maxP. That
395         /  doesn't seem required as you get so many matching clocks that you don't enter a second
396         /  iteration for P. (The minP / maxP values in the nv driver only differ at most 1, so it is for
397         /  some rare corner cases.
398         */
399         for (P=0, VCOFreq=maxVCOFreq/2; clockIn<=VCOFreq && P <= maxP; P++)
400         {
401                 VCOFreq /= 2;
402         }
403
404         /* Calculate the m and n values. There are a lot of values which give the same speed;
405         /  We choose the speed for which the difference with the request speed is as small as possible.
406         */
407         for (M=minM; M<=maxM; M++)
408         {
409                 /* The VCO has a minimum input frequency */
410                 if ((refClk/M) < minVCOInputFreq)
411                         break;
412
413                 for (N=minN; N<=maxN; N++)
414                 {
415                         /* Calculate the frequency generated by VCO1 */
416                         clock = (int)(refClk * N / (float)M);
417
418                         /* Verify if the clock lies within the output limits of VCO1 */
419                         if (clock < minVCOFreq)
420                                 continue;
421                         else if (clock > maxVCOFreq) /* It is no use to continue as the clock will only become higher */
422                                 break;
423
424                         clock >>= P;
425                         delta = abs((int)(clockIn - clock));
426                         /* When the difference is 0 or less than .5% accept the speed */
427                         if (((delta == 0) || ((float)delta/(float)clockIn <= 0.005)))
428                         {
429                                 *m1_best = M;
430                                 *n1_best = N;
431                                 *p_best = P;
432                                 return;
433                         }
434
435                         /* When the new difference is smaller than the old one, use this one */
436                         if (delta < bestDelta)
437                         {
438                                 bestDelta = delta;
439                                 *m1_best = M;
440                                 *n1_best = N;
441                                 *p_best = P;
442                         }
443                 }
444         }
445 }
446
447 static void
448 CalculateVClkNV4x_DoubleVCO(NVPtr pNv, struct pll_lims *pll_lim, uint32_t clockIn, uint32_t *n1_best, uint32_t *n2_best, uint32_t *m1_best, uint32_t *m2_best, uint32_t *p_best)
449 {
450         uint32_t clock1, clock2, M, M2, N, N2, P;
451         uint32_t delta, bestDelta, minM, minM2, maxM, maxM2, minN, minN2, maxN, maxN2, maxP;
452         uint32_t minVCOInputFreq, minVCO2InputFreq, maxVCO2InputFreq, minVCOFreq, minVCO2Freq, maxVCOFreq, maxVCO2Freq;
453         uint32_t VCO2Freq, maxClock;
454         uint32_t refClk = pNv->CrystalFreqKHz;
455         bestDelta = clockIn;
456
457         /* bios clocks are in MHz, we use KHz */
458         minVCOInputFreq = pll_lim->vco1.min_inputfreq*1000;
459         minVCOFreq = pll_lim->vco1.minfreq*1000;
460         maxVCOFreq = pll_lim->vco1.maxfreq*1000;
461         minM = pll_lim->vco1.min_m;
462         maxM = pll_lim->vco1.max_m;
463         minN = pll_lim->vco1.min_n;
464         maxN = pll_lim->vco1.max_n;
465
466         minVCO2InputFreq = pll_lim->vco2.min_inputfreq*1000;
467         maxVCO2InputFreq = pll_lim->vco2.max_inputfreq*1000;
468         minVCO2Freq = pll_lim->vco2.minfreq*1000;
469         maxVCO2Freq = pll_lim->vco2.maxfreq*1000;
470         minM2 = pll_lim->vco2.min_m;
471         maxM2 = pll_lim->vco2.max_m;
472         minN2 = pll_lim->vco2.min_n;
473         maxN2 = pll_lim->vco2.max_n;
474
475         maxP = 6;
476
477         maxClock = maxVCO2Freq;
478         /* If the requested clock is behind the bios limits, try it anyway */
479         if (clockIn > maxVCO2Freq)
480                 maxClock = clockIn + clockIn/200; /* Add a .5% margin */
481
482         /* The optimal frequency for the PLL to work at is somewhere in the center of its range.
483         /  Choose a post divider in such a way to achieve this.
484         /  The G8x nv driver does something similar but they they derive a minP and maxP. That
485         /  doesn't seem required as you get so many matching clocks that you don't enter a second
486         /  iteration for P. (The minP / maxP values in the nv driver only differ at most 1, so it is for
487         /  some rare corner cases.
488         */
489         for (P=0, VCO2Freq=maxClock/2; clockIn<=VCO2Freq && P <= maxP; P++)
490         {
491                 VCO2Freq /= 2;
492         }
493
494         /* The PLLs on Geforce6/7 hardware can operate in a single stage made with only 1 VCO
495         /  and a cascade mode of two VCOs. This second mode is in general used for relatively high
496         /  frequencies. The loop below calculates the divider and multiplier ratios for the cascade
497         /  mode. The code takes into account limits defined in the video bios.
498         */
499         for (M=minM; M<=maxM; M++)
500         {
501                 /* The VCO has a minimum input frequency */
502                 if ((refClk/M) < minVCOInputFreq)
503                         break;
504
505                 for (N=minN; N<=maxN; N++)
506                 {
507                         /* Calculate the frequency generated by VCO1 */
508                         clock1 = (int)(refClk * N / (float)M);
509                         /* Verify if the clock lies within the output limits of VCO1 */
510                         if ( (clock1 < minVCOFreq) )
511                                 continue;
512                         else if (clock1 > maxVCOFreq) /* For future N, the clock will only increase so stop; xorg nv continues but that is useless */
513                                 break;
514
515                         for (M2=minM2; M2<=maxM2; M2++)
516                         {
517                                 /* The clock fed to the second VCO needs to lie within a certain input range */
518                                 if (clock1 / M2 < minVCO2InputFreq)
519                                         break;
520                                 else if (clock1 / M2 > maxVCO2InputFreq)
521                                         continue;
522
523                                 N2 = (int)((float)((clockIn << P) * M * M2) / (float)(refClk * N)+.5);
524                                 if( (N2 < minN2) || (N2 > maxN2) )
525                                         continue;
526
527                                 /* The clock before being fed to the post-divider needs to lie within a certain range.
528                                 /  Further there are some limits on N2/M2.
529                                 */
530                                 clock2 = (int)((float)(N*N2)/(M*M2) * refClk);
531                                 if( (clock2 < minVCO2Freq) || (clock2 > maxClock))// || ((N2 / M2) < 4) || ((N2 / M2) > 10) )
532                                         continue;
533
534                                 /* The post-divider delays the 'high' clock to create a low clock if requested.
535                                 /  This post-divider exists because the VCOs can only generate frequencies within
536                                 /  a limited frequency range. This range has been tuned to lie around half of its max
537                                 /  input frequency. It tries to calculate all clocks (including lower ones) around this
538                                 /  'center' frequency.
539                                 */
540                                 clock2 >>= P;
541                                 delta = abs((int)(clockIn - clock2));
542
543                                 /* When the difference is 0 or less than .5% accept the speed */
544                                 if (((delta == 0) || ((float)delta/(float)clockIn <= 0.005)))
545                                 {
546                                         *m1_best = M;
547                                         *m2_best = M2;
548                                         *n1_best = N;
549                                         *n2_best = N2;
550                                         *p_best = P;
551                                         return;
552                                 }
553
554                                 /* When the new difference is smaller than the old one, use this one */
555                                 if (delta < bestDelta)
556                                 {
557                                         bestDelta = delta;
558                                         *m1_best = M;
559                                         *m2_best = M2;
560                                         *n1_best = N;
561                                         *n2_best = N2;
562                                         *p_best = P;
563                                 }
564                         }
565                 }
566         }
567 }
568
569 /* BIG NOTE: modifying vpll1 and vpll2 does not work, what bit is the switch to allow it? */
570
571 /* Even though they are not yet used, i'm adding some notes about some of the 0x4000 regs */
572 /* They are only valid for NV4x, appearantly reordered for NV5x */
573 /* gpu pll: 0x4000 + 0x4004
574  * unknown pll: 0x4008 + 0x400c
575  * vpll1: 0x4010 + 0x4014
576  * vpll2: 0x4018 + 0x401c
577  * unknown pll: 0x4020 + 0x4024
578  * unknown pll: 0x4038 + 0x403c
579  * Some of the unknown's are probably memory pll's.
580  * The vpll's use two set's of multipliers and dividers. I refer to them as a and b.
581  * 1 and 2 refer to the registers of each pair. There is only one post divider.
582  * Logic: clock = reference_clock * ((n(a) * n(b))/(m(a) * m(b))) >> p
583  * 1) bit 0-7: familiar values, but redirected from were? (similar to PLL_SETUP_CONTROL)
584  *     bit8: A switch that turns of the second divider and multiplier off.
585  *     bit12: Also a switch, i haven't seen it yet.
586  *     bit16-19: p-divider
587  *     but 28-31: Something related to the mode that is used (see bit8).
588  * 2) bit0-7: m-divider (a)
589  *     bit8-15: n-multiplier (a)
590  *     bit16-23: m-divider (b)
591  *     bit24-31: n-multiplier (b)
592  */
593
594 /* Modifying the gpu pll for example requires:
595  * - Disable value 0x333 (inverse AND mask) on the 0xc040 register.
596  * This is not needed for the vpll's which have their own bits.
597  */
598
599 static void
600 CalculateVClkNV4x(
601         ScrnInfoPtr pScrn,
602         uint32_t requested_clock,
603         uint32_t *given_clock,
604         uint32_t *pll_a,
605         uint32_t *pll_b,
606         uint32_t *reg580,
607         Bool    *db1_ratio,
608         Bool primary
609 )
610 {
611         NVPtr pNv = NVPTR(pScrn);
612         struct pll_lims pll_lim;
613         /* We have 2 mulitpliers, 2 dividers and one post divider */
614         /* Note that p is only 3 bits */
615         uint32_t m1_best = 0, m2_best = 0, n1_best = 0, n2_best = 0, p_best = 0;
616         uint32_t special_bits = 0;
617
618         if (primary) {
619                 if (!get_pll_limits(pScrn, VPLL1, &pll_lim))
620                         return;
621         } else
622                 if (!get_pll_limits(pScrn, VPLL2, &pll_lim))
623                         return;
624
625         if (requested_clock < pll_lim.vco1.maxfreq*1000 && pNv->NVArch > 0x40) { /* single VCO */
626                 *db1_ratio = TRUE;
627                 /* Turn the second set of divider and multiplier off */
628                 /* Bogus data, the same nvidia uses */
629                 n2_best = 1;
630                 m2_best = 31;
631                 CalculateVClkNV4x_SingleVCO(pNv, &pll_lim, requested_clock, &n1_best, &m1_best, &p_best);
632         } else { /* dual VCO */
633                 *db1_ratio = FALSE;
634                 CalculateVClkNV4x_DoubleVCO(pNv, &pll_lim, requested_clock, &n1_best, &n2_best, &m1_best, &m2_best, &p_best);
635         }
636
637         /* Are this all (relevant) G70 cards? */
638         if (pNv->NVArch == 0x4B || pNv->NVArch == 0x46 || pNv->NVArch == 0x47 || pNv->NVArch == 0x49) {
639                 /* This is a big guess, but should be reasonable until we can narrow it down. */
640                 if (*db1_ratio) {
641                         special_bits = 0x1;
642                 } else {
643                         special_bits = 0x3;
644                 }
645         }
646
647         /* What exactly are the purpose of the upper 2 bits of pll_a and pll_b? */
648         *pll_a = (special_bits << 30) | (p_best << 16) | (n1_best << 8) | (m1_best << 0);
649         /* This VCO2 bit is an educated guess, but it needs to stay on for NV4x. */
650         *pll_b = NV31_RAMDAC_ENABLE_VCO2 | (n2_best << 8) | (m2_best << 0);
651
652         if (*db1_ratio) {
653                 if (primary) {
654                         *reg580 |= NV_RAMDAC_580_VPLL1_ACTIVE;
655                 } else {
656                         *reg580 |= NV_RAMDAC_580_VPLL2_ACTIVE;
657                 }
658         } else {
659                 if (primary) {
660                         *reg580 &= ~NV_RAMDAC_580_VPLL1_ACTIVE;
661                 } else {
662                         *reg580 &= ~NV_RAMDAC_580_VPLL2_ACTIVE;
663                 }
664         }
665
666         if (*db1_ratio) {
667                 ErrorF("vpll: n1 %d m1 %d p %d db1_ratio %d\n", n1_best, m1_best, p_best, *db1_ratio);
668         } else {
669                 ErrorF("vpll: n1 %d n2 %d m1 %d m2 %d p %d db1_ratio %d\n", n1_best, n2_best, m1_best, m2_best, p_best, *db1_ratio);
670         }
671 }
672
673 static void nv40_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
674 {
675         state->vpll1_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL);
676         state->vpll1_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL_B);
677         state->vpll2_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2);
678         state->vpll2_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2_B);
679         state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
680         state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
681         state->reg580 = nvReadRAMDAC0(pNv, NV_RAMDAC_580);
682         state->reg594 = nvReadRAMDAC0(pNv, NV_RAMDAC_594);
683 }
684
685 static void nv40_crtc_load_state_pll(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
686 {
687         ScrnInfoPtr pScrn = crtc->scrn;
688         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
689         NVPtr pNv = NVPTR(pScrn);
690         uint32_t fp_debug_0[2];
691         uint32_t index[2];
692         fp_debug_0[0] = nvReadRAMDAC(pNv, 0, NV_RAMDAC_FP_DEBUG_0);
693         fp_debug_0[1] = nvReadRAMDAC(pNv, 1, NV_RAMDAC_FP_DEBUG_0);
694
695         uint32_t reg_c040_old = nvReadMC(pNv, 0xc040);
696
697         /* The TMDS_PLL switch is on the actual ramdac */
698         if (state->crosswired) {
699                 index[0] = 1;
700                 index[1] = 0;
701                 ErrorF("Crosswired pll state load\n");
702         } else {
703                 index[0] = 0;
704                 index[1] = 1;
705         }
706
707         if (state->vpll2_b && state->vpll_changed[1]) {
708                 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0,
709                         fp_debug_0[index[1]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
710
711                 /* Wait for the situation to stabilise */
712                 usleep(5000);
713
714                 uint32_t reg_c040 = pNv->misc_info.reg_c040;
715                 /* for vpll2 change bits 18 and 19 are disabled */
716                 reg_c040 &= ~(0x3 << 18);
717                 nvWriteMC(pNv, 0xc040, reg_c040);
718
719                 ErrorF("writing vpll2_a %08X\n", state->vpll2_a);
720                 ErrorF("writing vpll2_b %08X\n", state->vpll2_b);
721
722                 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2, state->vpll2_a);
723                 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2_B, state->vpll2_b);
724
725                 ErrorF("writing pllsel %08X\n", state->pllsel);
726                 /* Don't turn vpll1 off. */
727                 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
728
729                 nvWriteRAMDAC0(pNv, NV_RAMDAC_580, state->reg580);
730                 ErrorF("writing reg580 %08X\n", state->reg580);
731
732                 /* We need to wait a while */
733                 usleep(5000);
734                 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
735
736                 nvWriteRAMDAC(pNv, index[1], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[1]]);
737
738                 /* Wait for the situation to stabilise */
739                 usleep(5000);
740         }
741
742         if (state->vpll1_b && state->vpll_changed[0]) {
743                 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0,
744                         fp_debug_0[index[0]] | NV_RAMDAC_FP_DEBUG_0_PWRDOWN_TMDS_PLL);
745
746                 /* Wait for the situation to stabilise */
747                 usleep(5000);
748
749                 uint32_t reg_c040 = pNv->misc_info.reg_c040;
750                 /* for vpll2 change bits 16 and 17 are disabled */
751                 reg_c040 &= ~(0x3 << 16);
752                 nvWriteMC(pNv, 0xc040, reg_c040);
753
754                 ErrorF("writing vpll1_a %08X\n", state->vpll1_a);
755                 ErrorF("writing vpll1_b %08X\n", state->vpll1_b);
756
757                 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL, state->vpll1_a);
758                 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL_B, state->vpll1_b);
759
760                 ErrorF("writing pllsel %08X\n", state->pllsel);
761                 nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
762
763                 nvWriteRAMDAC0(pNv, NV_RAMDAC_580, state->reg580);
764                 ErrorF("writing reg580 %08X\n", state->reg580);
765
766                 /* We need to wait a while */
767                 usleep(5000);
768                 nvWriteMC(pNv, 0xc040, pNv->misc_info.reg_c040);
769
770                 nvWriteRAMDAC(pNv, index[0], NV_RAMDAC_FP_DEBUG_0, fp_debug_0[index[0]]);
771
772                 /* Wait for the situation to stabilise */
773                 usleep(5000);
774         }
775
776         /* Let's be sure not to wake up any crtc's from dpms. */
777         /* But we do want to keep our newly set crtc awake. */
778         if (nv_crtc->head == 1) {
779                 nvWriteMC(pNv, 0xc040, reg_c040_old | (pNv->misc_info.reg_c040 & (0x3 << 18)));
780         } else {
781                 nvWriteMC(pNv, 0xc040, reg_c040_old | (pNv->misc_info.reg_c040 & (0x3 << 16)));
782         }
783
784         ErrorF("writing sel_clk %08X\n", state->sel_clk);
785         nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
786
787         ErrorF("writing reg594 %08X\n", state->reg594);
788         nvWriteRAMDAC0(pNv, NV_RAMDAC_594, state->reg594);
789
790         /* All clocks have been set at this point. */
791         state->vpll_changed[0] = FALSE;
792         state->vpll_changed[1] = FALSE;
793 }
794
795 static void nv_crtc_save_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
796 {
797         state->vpll1_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL);
798         if (pNv->twoHeads) {
799                 state->vpll2_a = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2);
800         }
801         if (pNv->twoStagePLL && pNv->NVArch != 0x30) {
802                 state->vpll1_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL_B);
803                 state->vpll2_b = nvReadRAMDAC0(pNv, NV_RAMDAC_VPLL2_B);
804         }
805         state->pllsel = nvReadRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT);
806         state->sel_clk = nvReadRAMDAC0(pNv, NV_RAMDAC_SEL_CLK);
807 }
808
809
810 static void nv_crtc_load_state_pll(NVPtr pNv, RIVA_HW_STATE *state)
811 {
812         /* This sequence is important, the NV28 is very sensitive in this area. */
813         /* Keep pllsel last and sel_clk first. */
814         ErrorF("writing sel_clk %08X\n", state->sel_clk);
815         nvWriteRAMDAC0(pNv, NV_RAMDAC_SEL_CLK, state->sel_clk);
816
817         if (state->vpll2_a && state->vpll_changed[1]) {
818                 if (pNv->twoHeads) {
819                         ErrorF("writing vpll2_a %08X\n", state->vpll2_a);
820                         nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2, state->vpll2_a);
821                 }
822                 if (pNv->twoStagePLL && pNv->NVArch != 0x30) {
823                         ErrorF("writing vpll2_b %08X\n", state->vpll2_b);
824                         nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL2_B, state->vpll2_b);
825                 }
826         }
827
828         if (state->vpll1_a && state->vpll_changed[0]) {
829                 ErrorF("writing vpll1_a %08X\n", state->vpll1_a);
830                 nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL, state->vpll1_a);
831                 if (pNv->twoStagePLL && pNv->NVArch != 0x30) {
832                         ErrorF("writing vpll1_b %08X\n", state->vpll1_b);
833                         nvWriteRAMDAC0(pNv, NV_RAMDAC_VPLL_B, state->vpll1_b);
834                 }
835         }
836
837         ErrorF("writing pllsel %08X\n", state->pllsel);
838         nvWriteRAMDAC0(pNv, NV_RAMDAC_PLL_SELECT, state->pllsel);
839
840         /* All clocks have been set at this point. */
841         state->vpll_changed[0] = FALSE;
842         state->vpll_changed[1] = FALSE;
843 }
844
845 #define IS_NV44P (pNv->NVArch >= 0x44 ? 1 : 0)
846 #define SEL_CLK_OFFSET (nv_get_sel_clk_offset(pNv->NVArch, nv_output->bus))
847
848 #define WIPE_OTHER_CLOCKS(_sel_clk, _head, _bus) (nv_wipe_other_clocks(_sel_clk, pNv->NVArch, _head, _bus))
849
850 /*
851  * Calculate extended mode parameters (SVGA) and save in a 
852  * mode state structure.
853  * State is not specific to a single crtc, but shared.
854  */
855 void nv_crtc_calc_state_ext(
856         xf86CrtcPtr     crtc,
857         int                     bpp,
858         int                     DisplayWidth, /* Does this change after setting the mode? */
859         int                     CrtcHDisplay,
860         int                     CrtcVDisplay,
861         int                     dotClock,
862         int                     flags 
863 )
864 {
865         ScrnInfoPtr pScrn = crtc->scrn;
866         uint32_t pixelDepth, VClk = 0;
867         uint32_t CursorStart;
868         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
869         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
870         NVCrtcRegPtr regp;
871         NVPtr pNv = NVPTR(pScrn);
872         RIVA_HW_STATE *state;
873         int num_crtc_enabled, i;
874         uint32_t old_clock_a = 0, old_clock_b = 0;
875
876         state = &pNv->ModeReg;
877
878         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
879
880         xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
881         NVOutputPrivatePtr nv_output = NULL;
882         if (output) {
883                 nv_output = output->driver_private;
884         }
885
886         /* Store old clock. */
887         if (nv_crtc->head == 1) {
888                 old_clock_a = state->vpll2_a;
889                 old_clock_b = state->vpll2_b;
890         } else {
891                 old_clock_a = state->vpll1_a;
892                 old_clock_b = state->vpll1_b;
893         }
894
895         /*
896          * Extended RIVA registers.
897          */
898         pixelDepth = (bpp + 1)/8;
899         if (pNv->Architecture == NV_ARCH_40) {
900                 /* Does register 0x580 already have a value? */
901                 if (!state->reg580) {
902                         state->reg580 = pNv->misc_info.ramdac_0_reg_580;
903                 }
904                 if (nv_crtc->head == 1) {
905                         CalculateVClkNV4x(pScrn, dotClock, &VClk, &state->vpll2_a, &state->vpll2_b, &state->reg580, &state->db1_ratio[1], FALSE);
906                 } else {
907                         CalculateVClkNV4x(pScrn, dotClock, &VClk, &state->vpll1_a, &state->vpll1_b, &state->reg580, &state->db1_ratio[0], TRUE);
908                 }
909         } else if (pNv->twoStagePLL) {
910                 struct pll_lims pll_lim;
911                 int NM1, NM2, log2P;
912                 get_pll_limits(pScrn, 0, &pll_lim);
913                 VClk = getMNP_double(pScrn, &pll_lim, dotClock, &NM1, &NM2, &log2P);
914                 if (pNv->NVArch == 0x30) {
915                         /* See nvregisters.xml for details. */
916                         state->pll = log2P << 16 | NM1 | (NM2 & 7) << 4 | ((NM2 >> 8) & 7) << 19 | ((NM2 >> 11) & 3) << 24 | NV30_RAMDAC_ENABLE_VCO2;
917                 } else {
918                         state->pll = log2P << 16 | NM1;
919                         state->pllB = NV31_RAMDAC_ENABLE_VCO2 | NM2;
920                 }
921         } else {
922                 int NM, log2P;
923                 VClk = getMNP_single(pScrn, dotClock, &NM, &log2P);
924                 state->pll = log2P << 16 | NM;
925         }
926
927         if (pNv->Architecture < NV_ARCH_40) {
928                 if (nv_crtc->head == 1) {
929                         state->vpll2_a = state->pll;
930                         state->vpll2_b = state->pllB;
931                 } else {
932                         state->vpll1_a = state->pll;
933                         state->vpll1_b = state->pllB;
934                 }
935         }
936
937         if (nv_crtc->head == 1) {
938                 state->vpll_changed[1] = ((state->vpll2_a == old_clock_a) && (state->vpll2_b == old_clock_b)) ? FALSE : TRUE;
939         } else {
940                 state->vpll_changed[0] = ((state->vpll1_a == old_clock_a) && (state->vpll1_b == old_clock_b)) ? FALSE : TRUE;
941         }
942
943         switch (pNv->Architecture) {
944         case NV_ARCH_04:
945                 nv4UpdateArbitrationSettings(VClk, 
946                                                 pixelDepth * 8, 
947                                                 &(state->arbitration0),
948                                                 &(state->arbitration1),
949                                                 pNv);
950                 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x00;
951                 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = 0xbC;
952                 if (flags & V_DBLSCAN)
953                         regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
954                 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = 0x00000000;
955                 state->pllsel   |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2 | NV_RAMDAC_PLL_SELECT_PLL_SOURCE_ALL; 
956                 state->config   = 0x00001114;
957                 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
958                 break;
959         case NV_ARCH_10:
960         case NV_ARCH_20:
961         case NV_ARCH_30:
962         default:
963                 if (((pNv->Chipset & 0xfff0) == CHIPSET_C51) ||
964                         ((pNv->Chipset & 0xfff0) == CHIPSET_C512)) {
965                         state->arbitration0 = 128; 
966                         state->arbitration1 = 0x0480; 
967                 } else if (((pNv->Chipset & 0xffff) == CHIPSET_NFORCE) ||
968                         ((pNv->Chipset & 0xffff) == CHIPSET_NFORCE2)) {
969                         nForceUpdateArbitrationSettings(VClk,
970                                                 pixelDepth * 8,
971                                                 &(state->arbitration0),
972                                                 &(state->arbitration1),
973                                                 pNv);
974                 } else if (pNv->Architecture < NV_ARCH_30) {
975                         nv10UpdateArbitrationSettings(VClk, 
976                                                 pixelDepth * 8, 
977                                                 &(state->arbitration0),
978                                                 &(state->arbitration1),
979                                                 pNv);
980                 } else {
981                         nv30UpdateArbitrationSettings(pNv,
982                                                 &(state->arbitration0),
983                                                 &(state->arbitration1));
984                 }
985
986                 if (nv_crtc->head == 1) {
987                         CursorStart = pNv->Cursor2->offset;
988                 } else {
989                         CursorStart = pNv->Cursor->offset;
990                 }
991
992                 regp->CRTC[NV_VGA_CRTCX_CURCTL0] = 0x80 | (CursorStart >> 17);
993                 regp->CRTC[NV_VGA_CRTCX_CURCTL1] = (CursorStart >> 11) << 2;
994                 regp->CRTC[NV_VGA_CRTCX_CURCTL2] = CursorStart >> 24;
995
996                 if (flags & V_DBLSCAN) 
997                         regp->CRTC[NV_VGA_CRTCX_CURCTL1] |= 2;
998
999                 state->config   = nvReadFB(pNv, NV_PFB_CFG0);
1000                 regp->CRTC[NV_VGA_CRTCX_REPAINT1] = CrtcHDisplay < 1280 ? 0x04 : 0x00;
1001                 break;
1002         }
1003
1004         /* okay do we have 2 CRTCs running ? */
1005         num_crtc_enabled = 0;
1006         for (i = 0; i < xf86_config->num_crtc; i++) {
1007                 if (xf86_config->crtc[i]->enabled) {
1008                         num_crtc_enabled++;
1009                 }
1010         }
1011
1012         ErrorF("There are %d CRTC's enabled\n", num_crtc_enabled);
1013
1014         /* The main stuff seems to be valid for NV3x also. */
1015         if (pNv->Architecture >= NV_ARCH_30) {
1016                 /* This register is only used on the primary ramdac */
1017                 /* This seems to be needed to select the proper clocks, otherwise bad things happen */
1018
1019                 if (!state->sel_clk)
1020                         state->sel_clk = pNv->misc_info.sel_clk & ~(0xf << 16);
1021
1022                 if (output && (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)) {
1023                         /* Only wipe when are a relevant (digital) output. */
1024                         state->sel_clk &= ~(0xf << 16);
1025                         Bool crossed_clocks = nv_output->preferred_output ^ nv_crtc->head;
1026                         /* Even with two dvi, this should not conflict. */
1027                         if (crossed_clocks) {
1028                                 state->sel_clk |= (0x1 << 16);
1029                         } else {
1030                                 state->sel_clk |= (0x4 << 16);
1031                         }
1032                 }
1033
1034                 /* Some cards, specifically dual dvi/lvds cards set another bitrange.
1035                  * I suspect inverse beheaviour to the normal bitrange, but i am not a 100% certain about this.
1036                  * This is all based on default settings found in mmio-traces.
1037                  * The blob never changes these, as it doesn't run unusual output configurations.
1038                  * It seems to prefer situations that avoid changing these bits (for a good reason?).
1039                  * I still don't know the purpose of value 2, it's similar to 4, but what exactly does it do?
1040                  */
1041
1042                 /* Some extra info:
1043                  * nv30:
1044                  *      bit 0           NVClk spread spectrum on/off
1045                  *      bit 2           MemClk spread spectrum on/off
1046                  *      bit 4           PixClk1 spread spectrum on/off
1047                  *      bit 6           PixClk2 spread spectrum on/off
1048
1049                  *      nv40:
1050                  *      what causes setting of bits not obvious but:
1051                  *      bits 4&5                relate to headA
1052                  *      bits 6&7                relate to headB
1053                 */
1054                 /* Only let digital outputs mess with this, otherwise strange output routings may mess it up. */
1055                 if (output && (nv_output->type == OUTPUT_TMDS || nv_output->type == OUTPUT_LVDS)) {
1056                         if (pNv->Architecture == NV_ARCH_40) {
1057                                 for (i = 0; i < 4; i++) {
1058                                         uint32_t var = (state->sel_clk & (0xf << 4*i)) >> 4*i;
1059                                         if (var == 0x1 || var == 0x4) {
1060                                                 state->sel_clk &= ~(0xf << 4*i);
1061                                                 Bool crossed_clocks = nv_output->preferred_output ^ nv_crtc->head;
1062                                                 if (crossed_clocks) {
1063                                                         state->sel_clk |= (0x4 << 4*i);
1064                                                 } else {
1065                                                         state->sel_clk |= (0x1 << 4*i);
1066                                                 }
1067                                                 break; /* This should only occur once. */
1068                                         }
1069                                 }
1070                         /* Based on NV31M. */
1071                         } else if (pNv->Architecture == NV_ARCH_30) {
1072                                 for (i = 0; i < 4; i++) {
1073                                         uint32_t var = (state->sel_clk & (0xf << 4*i)) >> 4*i;
1074                                         if (var == 0x4 || var == 0x5) {
1075                                                 state->sel_clk &= ~(0xf << 4*i);
1076                                                 Bool crossed_clocks = nv_output->preferred_output ^ nv_crtc->head;
1077                                                 if (crossed_clocks) {
1078                                                         state->sel_clk |= (0x4 << 4*i);
1079                                                 } else {
1080                                                         state->sel_clk |= (0x5 << 4*i);
1081                                                 }
1082                                                 break; /* This should only occur once. */
1083                                         }
1084                                 }
1085                         }
1086                 }
1087
1088                 /* Are we crosswired? */
1089                 if (output && nv_crtc->head != nv_output->preferred_output) {
1090                         state->crosswired = TRUE;
1091                 } else {
1092                         state->crosswired = FALSE;
1093                 }
1094
1095                 if (nv_crtc->head == 1) {
1096                         if (state->db1_ratio[1])
1097                                 ErrorF("We are a lover of the DB1 VCLK ratio\n");
1098                 } else if (nv_crtc->head == 0) {
1099                         if (state->db1_ratio[0])
1100                                 ErrorF("We are a lover of the DB1 VCLK ratio\n");
1101                 }
1102         } else {
1103                 /* Do NV1x/NV2x cards need anything in sel_clk? */
1104                 state->sel_clk = 0x0;
1105                 state->crosswired = FALSE;
1106         }
1107
1108         /* The NV40 seems to have more similarities to NV3x than other cards. */
1109         if (pNv->NVArch < 0x41) {
1110                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_NVPLL;
1111                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_MPLL;
1112         }
1113
1114         if (nv_crtc->head == 1) {
1115                 if (!state->db1_ratio[1]) {
1116                         state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
1117                 } else {
1118                         state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
1119                 }
1120                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2;
1121         } else {
1122                 if (!state->db1_ratio[0]) {
1123                         state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
1124                 } else {
1125                         state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
1126                 }
1127                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL;
1128         }
1129
1130         /* The blob uses this always, so let's do the same */
1131         if (pNv->Architecture == NV_ARCH_40) {
1132                 state->pllsel |= NV_RAMDAC_PLL_SELECT_USE_VPLL2_TRUE;
1133         }
1134
1135         /* The primary output resource doesn't seem to care */
1136         if (output && pNv->Architecture == NV_ARCH_40 && nv_output->output_resource == 1) { /* This is the "output" */
1137                 /* non-zero values are for analog, don't know about tv-out and the likes */
1138                 if (output && nv_output->type != OUTPUT_ANALOG) {
1139                         state->reg594 = 0x0;
1140                 } else if (output) {
1141                         /* Are we a flexible output? */
1142                         if (ffs(pNv->dcb_table.entry[nv_output->dcb_entry].or) & OUTPUT_0) {
1143                                 state->reg594 = 0x1;
1144                                 pNv->restricted_mode = FALSE;
1145                         } else {
1146                                 state->reg594 = 0x0;
1147                                 pNv->restricted_mode = TRUE;
1148                         }
1149
1150                         /* More values exist, but they seem related to the 3rd dac (tv-out?) somehow */
1151                         /* bit 16-19 are bits that are set on some G70 cards */
1152                         /* Those bits are also set to the 3rd OUTPUT register */
1153                         if (nv_crtc->head == 1) {
1154                                 state->reg594 |= 0x100;
1155                         }
1156                 }
1157         }
1158
1159         regp->CRTC[NV_VGA_CRTCX_FIFO0] = state->arbitration0;
1160         regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = state->arbitration1 & 0xff;
1161         if (pNv->Architecture >= NV_ARCH_30) {
1162                 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = state->arbitration1 >> 8;
1163         }
1164
1165         regp->CRTC[NV_VGA_CRTCX_REPAINT0] = (((DisplayWidth/8) * pixelDepth) & 0x700) >> 3;
1166         regp->CRTC[NV_VGA_CRTCX_PIXEL] = (pixelDepth > 2) ? 3 : pixelDepth;
1167 }
1168
1169 static void
1170 nv_crtc_dpms(xf86CrtcPtr crtc, int mode)
1171 {
1172         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1173
1174         ErrorF("nv_crtc_dpms is called for CRTC %d with mode %d\n", nv_crtc->head, mode);
1175
1176         if (nv_crtc->last_dpms == mode) /* Don't do unnecesary mode changes. */
1177                 return;
1178
1179         nv_crtc->last_dpms = mode;
1180
1181         ScrnInfoPtr pScrn = crtc->scrn;
1182         NVPtr pNv = NVPTR(pScrn);
1183         unsigned char seq1 = 0, crtc17 = 0;
1184         unsigned char crtc1A;
1185
1186         NVCrtcSetOwner(crtc);
1187
1188         crtc1A = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1) & ~0xC0;
1189         switch(mode) {
1190                 case DPMSModeStandby:
1191                 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
1192                 seq1 = 0x20;
1193                 crtc17 = 0x80;
1194                 crtc1A |= 0x80;
1195                 break;
1196         case DPMSModeSuspend:
1197                 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
1198                 seq1 = 0x20;
1199                 crtc17 = 0x80;
1200                 crtc1A |= 0x40;
1201                 break;
1202         case DPMSModeOff:
1203                 /* Screen: Off; HSync: Off, VSync: Off */
1204                 seq1 = 0x20;
1205                 crtc17 = 0x00;
1206                 crtc1A |= 0xC0;
1207                 break;
1208         case DPMSModeOn:
1209         default:
1210                 /* Screen: On; HSync: On, VSync: On */
1211                 seq1 = 0x00;
1212                 crtc17 = 0x80;
1213                 break;
1214         }
1215
1216         NVVgaSeqReset(crtc, TRUE);
1217         /* Each head has it's own sequencer, so we can turn it off when we want */
1218         seq1 |= (NVReadVgaSeq(crtc, 0x01) & ~0x20);
1219         NVWriteVgaSeq(crtc, 0x1, seq1);
1220         crtc17 |= (NVReadVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL) & ~0x80);
1221         usleep(10000);
1222         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_MODECTL, crtc17);
1223         NVVgaSeqReset(crtc, FALSE);
1224
1225         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, crtc1A);
1226
1227         /* We can completely disable a vpll if the crtc is off. */
1228         if (pNv->Architecture == NV_ARCH_40) {
1229                 uint32_t reg_c040_old = nvReadMC(pNv, 0xc040);
1230                 if (mode == DPMSModeOn) {
1231                         nvWriteMC(pNv, 0xc040, reg_c040_old | (pNv->misc_info.reg_c040 & (0x3 << (16 + 2*nv_crtc->head))));
1232                 } else {
1233                         nvWriteMC(pNv, 0xc040, reg_c040_old & ~(pNv->misc_info.reg_c040 & (0x3 << (16 + 2*nv_crtc->head))));
1234                 }
1235         }
1236
1237         /* I hope this is the right place */
1238         if (crtc->enabled && mode == DPMSModeOn) {
1239                 pNv->crtc_active[nv_crtc->head] = TRUE;
1240         } else {
1241                 pNv->crtc_active[nv_crtc->head] = FALSE;
1242         }
1243 }
1244
1245 static Bool
1246 nv_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode,
1247                      DisplayModePtr adjusted_mode)
1248 {
1249         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1250         ErrorF("nv_crtc_mode_fixup is called for CRTC %d\n", nv_crtc->head);
1251
1252         return TRUE;
1253 }
1254
1255 static void
1256 nv_crtc_mode_set_vga(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1257 {
1258         ScrnInfoPtr pScrn = crtc->scrn;
1259         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1260         NVCrtcRegPtr regp;
1261         NVPtr pNv = NVPTR(pScrn);
1262         NVFBLayout *pLayout = &pNv->CurrentLayout;
1263         int depth = pScrn->depth;
1264
1265         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1266
1267         /* Calculate our timings */
1268         int horizDisplay        = (mode->CrtcHDisplay >> 3)     - 1;
1269         int horizStart          = (mode->CrtcHSyncStart >> 3)   - 1;
1270         int horizEnd            = (mode->CrtcHSyncEnd >> 3)     - 1;
1271         int horizTotal          = (mode->CrtcHTotal >> 3)               - 5;
1272         int horizBlankStart     = (mode->CrtcHDisplay >> 3)             - 1;
1273         int horizBlankEnd       = (mode->CrtcHTotal >> 3)               - 1;
1274         int vertDisplay         = mode->CrtcVDisplay                    - 1;
1275         int vertStart           = mode->CrtcVSyncStart          - 1;
1276         int vertEnd             = mode->CrtcVSyncEnd                    - 1;
1277         int vertTotal           = mode->CrtcVTotal                      - 2;
1278         int vertBlankStart      = mode->CrtcVDisplay                    - 1;
1279         int vertBlankEnd        = mode->CrtcVTotal                      - 1;
1280
1281         Bool is_fp = FALSE;
1282
1283         xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1284         NVOutputPrivatePtr nv_output = NULL;
1285         if (output) {
1286                 nv_output = output->driver_private;
1287
1288                 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS))
1289                         is_fp = TRUE;
1290         }
1291
1292         ErrorF("Mode clock: %d\n", mode->Clock);
1293         ErrorF("Adjusted mode clock: %d\n", adjusted_mode->Clock);
1294
1295         /* Reverted to what nv did, because that works for all resolutions on flatpanels */
1296         if (is_fp) {
1297                 vertStart = vertTotal - 3;  
1298                 vertEnd = vertTotal - 2;
1299                 vertBlankStart = vertStart;
1300                 horizStart = horizTotal - 5;
1301                 horizEnd = horizTotal - 2;
1302                 horizBlankEnd = horizTotal + 4;
1303                 if (pNv->overlayAdaptor) {
1304                         /* This reportedly works around Xv some overlay bandwidth problems*/
1305                         horizTotal += 2;
1306                 }
1307         }
1308
1309         if(mode->Flags & V_INTERLACE) 
1310                 vertTotal |= 1;
1311
1312         ErrorF("horizDisplay: 0x%X \n", horizDisplay);
1313         ErrorF("horizStart: 0x%X \n", horizStart);
1314         ErrorF("horizEnd: 0x%X \n", horizEnd);
1315         ErrorF("horizTotal: 0x%X \n", horizTotal);
1316         ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
1317         ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
1318         ErrorF("vertDisplay: 0x%X \n", vertDisplay);
1319         ErrorF("vertStart: 0x%X \n", vertStart);
1320         ErrorF("vertEnd: 0x%X \n", vertEnd);
1321         ErrorF("vertTotal: 0x%X \n", vertTotal);
1322         ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
1323         ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
1324
1325         /*
1326         * compute correct Hsync & Vsync polarity 
1327         */
1328         if ((mode->Flags & (V_PHSYNC | V_NHSYNC))
1329                 && (mode->Flags & (V_PVSYNC | V_NVSYNC))) {
1330
1331                 regp->MiscOutReg = 0x23;
1332                 if (mode->Flags & V_NHSYNC) regp->MiscOutReg |= 0x40;
1333                 if (mode->Flags & V_NVSYNC) regp->MiscOutReg |= 0x80;
1334         } else {
1335                 int VDisplay = mode->VDisplay;
1336                 if (mode->Flags & V_DBLSCAN)
1337                         VDisplay *= 2;
1338                 if (mode->VScan > 1)
1339                         VDisplay *= mode->VScan;
1340                 if (VDisplay < 400) {
1341                         regp->MiscOutReg = 0xA3;                /* +hsync -vsync */
1342                 } else if (VDisplay < 480) {
1343                         regp->MiscOutReg = 0x63;                /* -hsync +vsync */
1344                 } else if (VDisplay < 768) {
1345                         regp->MiscOutReg = 0xE3;                /* -hsync -vsync */
1346                 } else {
1347                         regp->MiscOutReg = 0x23;                /* +hsync +vsync */
1348                 }
1349         }
1350
1351         regp->MiscOutReg |= (mode->ClockIndex & 0x03) << 2;
1352
1353         /*
1354         * Time Sequencer
1355         */
1356         if (depth == 4) {
1357                 regp->Sequencer[0] = 0x02;
1358         } else {
1359                 regp->Sequencer[0] = 0x00;
1360         }
1361         /* 0x20 disables the sequencer */
1362         if (mode->Flags & V_CLKDIV2) {
1363                 regp->Sequencer[1] = 0x29;
1364         } else {
1365                 regp->Sequencer[1] = 0x21;
1366         }
1367         if (depth == 1) {
1368                 regp->Sequencer[2] = 1 << BIT_PLANE;
1369         } else {
1370                 regp->Sequencer[2] = 0x0F;
1371                 regp->Sequencer[3] = 0x00;                     /* Font select */
1372         }
1373         if (depth < 8) {
1374                 regp->Sequencer[4] = 0x06;                             /* Misc */
1375         } else {
1376                 regp->Sequencer[4] = 0x0E;                             /* Misc */
1377         }
1378
1379         /*
1380         * CRTC Controller
1381         */
1382         regp->CRTC[NV_VGA_CRTCX_HTOTAL]  = Set8Bits(horizTotal);
1383         regp->CRTC[NV_VGA_CRTCX_HDISPE]  = Set8Bits(horizDisplay);
1384         regp->CRTC[NV_VGA_CRTCX_HBLANKS]  = Set8Bits(horizBlankStart);
1385         regp->CRTC[NV_VGA_CRTCX_HBLANKE]  = SetBitField(horizBlankEnd,4:0,4:0) 
1386                                 | SetBit(7);
1387         regp->CRTC[NV_VGA_CRTCX_HSYNCS]  = Set8Bits(horizStart);
1388         regp->CRTC[NV_VGA_CRTCX_HSYNCE]  = SetBitField(horizBlankEnd,5:5,7:7)
1389                                 | SetBitField(horizEnd,4:0,4:0);
1390         regp->CRTC[NV_VGA_CRTCX_VTOTAL]  = SetBitField(vertTotal,7:0,7:0);
1391         regp->CRTC[NV_VGA_CRTCX_OVERFLOW]  = SetBitField(vertTotal,8:8,0:0)
1392                                 | SetBitField(vertDisplay,8:8,1:1)
1393                                 | SetBitField(vertStart,8:8,2:2)
1394                                 | SetBitField(vertBlankStart,8:8,3:3)
1395                                 | SetBit(4)
1396                                 | SetBitField(vertTotal,9:9,5:5)
1397                                 | SetBitField(vertDisplay,9:9,6:6)
1398                                 | SetBitField(vertStart,9:9,7:7);
1399         regp->CRTC[NV_VGA_CRTCX_PRROWSCN]  = 0x00;
1400         regp->CRTC[NV_VGA_CRTCX_MAXSCLIN]  = SetBitField(vertBlankStart,9:9,5:5)
1401                                 | SetBit(6)
1402                                 | ((mode->Flags & V_DBLSCAN) ? 0x80 : 0x00);
1403         regp->CRTC[NV_VGA_CRTCX_VGACURCTRL] = 0x00;
1404         regp->CRTC[0xb] = 0x00;
1405         regp->CRTC[NV_VGA_CRTCX_FBSTADDH] = 0x00;
1406         regp->CRTC[NV_VGA_CRTCX_FBSTADDL] = 0x00;
1407         regp->CRTC[0xe] = 0x00;
1408         regp->CRTC[0xf] = 0x00;
1409         regp->CRTC[NV_VGA_CRTCX_VSYNCS] = Set8Bits(vertStart);
1410         regp->CRTC[NV_VGA_CRTCX_VSYNCE] = SetBitField(vertEnd,3:0,3:0) | SetBit(5);
1411         regp->CRTC[NV_VGA_CRTCX_VDISPE] = Set8Bits(vertDisplay);
1412         regp->CRTC[0x14] = 0x00;
1413         regp->CRTC[NV_VGA_CRTCX_PITCHL] = ((pScrn->displayWidth/8)*(pLayout->bitsPerPixel/8));
1414         regp->CRTC[NV_VGA_CRTCX_VBLANKS] = Set8Bits(vertBlankStart);
1415         regp->CRTC[NV_VGA_CRTCX_VBLANKE] = Set8Bits(vertBlankEnd);
1416         /* 0x80 enables the sequencer, we don't want that */
1417         if (depth < 8) {
1418                 regp->CRTC[NV_VGA_CRTCX_MODECTL] = 0xE3 & ~0x80;
1419         } else {
1420                 regp->CRTC[NV_VGA_CRTCX_MODECTL] = 0xC3 & ~0x80;
1421         }
1422         regp->CRTC[NV_VGA_CRTCX_LINECOMP] = 0xff;
1423
1424         /* 
1425          * Some extended CRTC registers (they are not saved with the rest of the vga regs).
1426          */
1427
1428         regp->CRTC[NV_VGA_CRTCX_LSR] = SetBitField(horizBlankEnd,6:6,4:4)
1429                                 | SetBitField(vertBlankStart,10:10,3:3)
1430                                 | SetBitField(vertStart,10:10,2:2)
1431                                 | SetBitField(vertDisplay,10:10,1:1)
1432                                 | SetBitField(vertTotal,10:10,0:0);
1433
1434         regp->CRTC[NV_VGA_CRTCX_HEB] = SetBitField(horizTotal,8:8,0:0) 
1435                                 | SetBitField(horizDisplay,8:8,1:1)
1436                                 | SetBitField(horizBlankStart,8:8,2:2)
1437                                 | SetBitField(horizStart,8:8,3:3);
1438
1439         regp->CRTC[NV_VGA_CRTCX_EXTRA] = SetBitField(vertTotal,11:11,0:0)
1440                                 | SetBitField(vertDisplay,11:11,2:2)
1441                                 | SetBitField(vertStart,11:11,4:4)
1442                                 | SetBitField(vertBlankStart,11:11,6:6);
1443
1444         if(mode->Flags & V_INTERLACE) {
1445                 horizTotal = (horizTotal >> 1) & ~1;
1446                 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = Set8Bits(horizTotal);
1447                 regp->CRTC[NV_VGA_CRTCX_HEB] |= SetBitField(horizTotal,8:8,4:4);
1448         } else {
1449                 regp->CRTC[NV_VGA_CRTCX_INTERLACE] = 0xff;  /* interlace off */
1450         }
1451
1452         /*
1453         * Theory resumes here....
1454         */
1455
1456         /*
1457         * Graphics Display Controller
1458         */
1459         regp->Graphics[0] = 0x00;
1460         regp->Graphics[1] = 0x00;
1461         regp->Graphics[2] = 0x00;
1462         regp->Graphics[3] = 0x00;
1463         if (depth == 1) {
1464                 regp->Graphics[4] = BIT_PLANE;
1465                 regp->Graphics[5] = 0x00;
1466         } else {
1467                 regp->Graphics[4] = 0x00;
1468                 if (depth == 4) {
1469                         regp->Graphics[5] = 0x02;
1470                 } else {
1471                         regp->Graphics[5] = 0x40;
1472                 }
1473         }
1474         regp->Graphics[6] = 0x05;   /* only map 64k VGA memory !!!! */
1475         regp->Graphics[7] = 0x0F;
1476         regp->Graphics[8] = 0xFF;
1477
1478         /* I ditched the mono stuff */
1479         regp->Attribute[0]  = 0x00; /* standard colormap translation */
1480         regp->Attribute[1]  = 0x01;
1481         regp->Attribute[2]  = 0x02;
1482         regp->Attribute[3]  = 0x03;
1483         regp->Attribute[4]  = 0x04;
1484         regp->Attribute[5]  = 0x05;
1485         regp->Attribute[6]  = 0x06;
1486         regp->Attribute[7]  = 0x07;
1487         regp->Attribute[8]  = 0x08;
1488         regp->Attribute[9]  = 0x09;
1489         regp->Attribute[10] = 0x0A;
1490         regp->Attribute[11] = 0x0B;
1491         regp->Attribute[12] = 0x0C;
1492         regp->Attribute[13] = 0x0D;
1493         regp->Attribute[14] = 0x0E;
1494         regp->Attribute[15] = 0x0F;
1495         /* These two below are non-vga */
1496         regp->Attribute[16] = 0x01;
1497         regp->Attribute[17] = 0x00;
1498         regp->Attribute[18] = 0x0F;
1499         regp->Attribute[19] = 0x00;
1500         regp->Attribute[20] = 0x00;
1501 }
1502
1503 #define MAX_H_VALUE(i) ((0x1ff + i) << 3)
1504 #define MAX_V_VALUE(i) ((0xfff + i) << 0)
1505
1506 /**
1507  * Sets up registers for the given mode/adjusted_mode pair.
1508  *
1509  * The clocks, CRTCs and outputs attached to this CRTC must be off.
1510  *
1511  * This shouldn't enable any clocks, CRTCs, or outputs, but they should
1512  * be easily turned on/off after this.
1513  */
1514 static void
1515 nv_crtc_mode_set_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1516 {
1517         ScrnInfoPtr pScrn = crtc->scrn;
1518         NVPtr pNv = NVPTR(pScrn);
1519         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1520         NVFBLayout *pLayout = &pNv->CurrentLayout;
1521         NVCrtcRegPtr regp, savep;
1522         unsigned int i;
1523         Bool is_fp = FALSE;
1524         Bool is_lvds = FALSE;
1525
1526         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];    
1527         savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
1528
1529         xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1530         NVOutputPrivatePtr nv_output = NULL;
1531         if (output) {
1532                 nv_output = output->driver_private;
1533
1534                 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS))
1535                         is_fp = TRUE;
1536
1537                 if (nv_output->type == OUTPUT_LVDS)
1538                         is_lvds = TRUE;
1539         }
1540
1541         /* Registers not directly related to the (s)vga mode */
1542
1543         /* bit2 = 0 -> fine pitched crtc granularity */
1544         /* The rest disables double buffering on CRTC access */
1545         regp->CRTC[NV_VGA_CRTCX_BUFFER] = 0xfa;
1546
1547         if (savep->CRTC[NV_VGA_CRTCX_LCD] <= 0xb) {
1548                 /* Common values are 0x0, 0x3, 0x8, 0xb, see logic below */
1549                 if (nv_crtc->head == 0) {
1550                         regp->CRTC[NV_VGA_CRTCX_LCD] = (1 << 3);
1551                 }
1552
1553                 if (is_fp) {
1554                         regp->CRTC[NV_VGA_CRTCX_LCD] |= (1 << 0) | (1 << 1);
1555                 }
1556         } else {
1557                 /* Let's keep any abnormal value there may be, like 0x54 or 0x79 */
1558                 regp->CRTC[NV_VGA_CRTCX_LCD] = savep->CRTC[NV_VGA_CRTCX_LCD];
1559         }
1560
1561         /* Sometimes 0x10 is used, what is this? */
1562         regp->CRTC[NV_VGA_CRTCX_59] = 0x0;
1563         /* Some kind of tmds switch for older cards */
1564         if (pNv->Architecture < NV_ARCH_40) {
1565                 regp->CRTC[NV_VGA_CRTCX_59] |= 0x1;
1566         }
1567
1568         /*
1569         * Initialize DAC palette.
1570         * Will only be written when depth != 8.
1571         */
1572         for (i = 0; i < 256; i++) {
1573                 regp->DAC[i*3] = i;
1574                 regp->DAC[(i*3)+1] = i;
1575                 regp->DAC[(i*3)+2] = i;
1576         }
1577
1578         /*
1579         * Calculate the extended registers.
1580         */
1581
1582         if(pLayout->depth < 24) {
1583                 i = pLayout->depth;
1584         } else {
1585                 i = 32;
1586         }
1587
1588         /* What is the meaning of this register? */
1589         /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */ 
1590         regp->CRTC[NV_VGA_CRTCX_FIFO1] = savep->CRTC[NV_VGA_CRTCX_FIFO1] & ~(1<<5);
1591
1592         /* NV40's don't set FPP units, unless in special conditions (then they set both) */
1593         /* But what are those special conditions? */
1594         if (pNv->Architecture <= NV_ARCH_30) {
1595                 if (is_fp) {
1596                         if(nv_crtc->head == 1) {
1597                                 regp->head |= NV_CRTC_FSEL_FPP1;
1598                         } else if (pNv->twoHeads) {
1599                                 regp->head |= NV_CRTC_FSEL_FPP2;
1600                         }
1601                 }
1602         } else {
1603                 /* Most G70 cards have FPP2 set on the secondary CRTC. */
1604                 if (nv_crtc->head == 1 && pNv->NVArch > 0x44) {
1605                         regp->head |= NV_CRTC_FSEL_FPP2;
1606                 }
1607         }
1608
1609         /* Except for rare conditions I2C is enabled on the primary crtc */
1610         if (nv_crtc->head == 0) {
1611                 if (pNv->overlayAdaptor) {
1612                         regp->head |= NV_CRTC_FSEL_OVERLAY;
1613                 }
1614                 regp->head |= NV_CRTC_FSEL_I2C;
1615         }
1616
1617         /* This is not what nv does, but it is what the blob does (for nv4x at least) */
1618         /* This fixes my cursor corruption issue */
1619         regp->cursorConfig = 0x0;
1620         if(mode->Flags & V_DBLSCAN)
1621                 regp->cursorConfig |= (1 << 4);
1622         if (pNv->alphaCursor) {
1623                 /* bit28 means we go into alpha blend mode and not rely on the current ROP */
1624                 regp->cursorConfig |= 0x14011000;
1625         } else {
1626                 regp->cursorConfig |= 0x02000000;
1627         }
1628
1629         /* Unblock some timings */
1630         regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = 0;
1631         regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = 0;
1632
1633         /* What is the purpose of this register? */
1634         /* 0x14 may be disabled? */
1635         regp->CRTC[NV_VGA_CRTCX_26] = 0x20;
1636
1637         /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
1638         if (is_lvds) {
1639                 regp->CRTC[NV_VGA_CRTCX_3B] = 0x11;
1640         } else if (is_fp) {
1641                 regp->CRTC[NV_VGA_CRTCX_3B] = 0x88;
1642         } else {
1643                 regp->CRTC[NV_VGA_CRTCX_3B] = 0x22;
1644         }
1645
1646         /* These values seem to vary */
1647         /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
1648         regp->CRTC[NV_VGA_CRTCX_3C] = savep->CRTC[NV_VGA_CRTCX_3C];
1649
1650         /* 0x80 seems to be used very often, if not always */
1651         regp->CRTC[NV_VGA_CRTCX_45] = 0x80;
1652
1653         /* Some cards have 0x41 instead of 0x1 (for crtc 0), it doesn't hurt to just use the old value. */
1654         regp->CRTC[NV_VGA_CRTCX_4B] = savep->CRTC[NV_VGA_CRTCX_4B];
1655
1656         if (is_fp)
1657                 regp->CRTC[NV_VGA_CRTCX_4B] |= 0x80;
1658
1659         /* The blob seems to take the current value from crtc 0, add 4 to that and reuse the old value for crtc 1*/
1660         if (nv_crtc->head == 1) {
1661                 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52;
1662         } else {
1663                 regp->CRTC[NV_VGA_CRTCX_52] = pNv->misc_info.crtc_0_reg_52 + 4;
1664         }
1665
1666         /* The exact purpose of this register is unknown, but we copy value from crtc0 */
1667         regp->unk81c = nvReadCRTC0(pNv, NV_CRTC_081C);
1668
1669         regp->unk830 = mode->CrtcVDisplay - 3;
1670         regp->unk834 = mode->CrtcVDisplay - 1;
1671
1672         /* This is what the blob does */
1673         regp->unk850 = nvReadCRTC(pNv, 0, NV_CRTC_0850);
1674
1675         /* Never ever modify gpio, unless you know very well what you're doing */
1676         regp->gpio = nvReadCRTC(pNv, 0, NV_CRTC_GPIO);
1677
1678         /* Switch to non-vga mode (the so called HSYNC mode) */
1679         regp->config = 0x2;
1680
1681         /* Some misc regs */
1682         regp->CRTC[NV_VGA_CRTCX_43] = 0x1;
1683         if (pNv->Architecture == NV_ARCH_40) {
1684                 regp->CRTC[NV_VGA_CRTCX_85] = 0xFF;
1685                 regp->CRTC[NV_VGA_CRTCX_86] = 0x1;
1686         }
1687
1688         /*
1689          * Calculate the state that is common to all crtc's (stored in the state struct).
1690          */
1691         ErrorF("crtc %d %d %d\n", nv_crtc->head, mode->CrtcHDisplay, pScrn->displayWidth);
1692         nv_crtc_calc_state_ext(crtc,
1693                                 i,
1694                                 pScrn->displayWidth,
1695                                 mode->CrtcHDisplay,
1696                                 mode->CrtcVDisplay,
1697                                 adjusted_mode->Clock,
1698                                 mode->Flags);
1699
1700         /* Enable slaved mode */
1701         if (is_fp) {
1702                 regp->CRTC[NV_VGA_CRTCX_PIXEL] |= (1 << 7);
1703         }
1704 }
1705
1706 static void
1707 nv_crtc_mode_set_ramdac_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
1708 {
1709         ScrnInfoPtr pScrn = crtc->scrn;
1710         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
1711         NVCrtcRegPtr regp, savep;
1712         NVPtr pNv = NVPTR(pScrn);
1713         NVFBLayout *pLayout = &pNv->CurrentLayout;
1714         Bool is_fp = FALSE;
1715         Bool is_lvds = FALSE;
1716         float aspect_ratio, panel_ratio;
1717         uint32_t h_scale, v_scale;
1718
1719         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1720         savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
1721
1722         xf86OutputPtr output = NVGetOutputFromCRTC(crtc);
1723         NVOutputPrivatePtr nv_output = NULL;
1724         if (output) {
1725                 nv_output = output->driver_private;
1726
1727                 if ((nv_output->type == OUTPUT_LVDS) || (nv_output->type == OUTPUT_TMDS))
1728                         is_fp = TRUE;
1729
1730                 if (nv_output->type == OUTPUT_LVDS)
1731                         is_lvds = TRUE;
1732         }
1733
1734         if (is_fp) {
1735                 regp->fp_horiz_regs[REG_DISP_END] = adjusted_mode->HDisplay - 1;
1736                 regp->fp_horiz_regs[REG_DISP_TOTAL] = adjusted_mode->HTotal - 1;
1737                 /* This is what the blob does. */
1738                 regp->fp_horiz_regs[REG_DISP_CRTC] = adjusted_mode->HSyncStart - 75 - 1;
1739                 regp->fp_horiz_regs[REG_DISP_SYNC_START] = adjusted_mode->HSyncStart - 1;
1740                 regp->fp_horiz_regs[REG_DISP_SYNC_END] = adjusted_mode->HSyncEnd - 1;
1741                 regp->fp_horiz_regs[REG_DISP_VALID_START] = adjusted_mode->HSkew;
1742                 regp->fp_horiz_regs[REG_DISP_VALID_END] = adjusted_mode->HDisplay - 1;
1743
1744                 regp->fp_vert_regs[REG_DISP_END] = adjusted_mode->VDisplay - 1;
1745                 regp->fp_vert_regs[REG_DISP_TOTAL] = adjusted_mode->VTotal - 1;
1746                 /* This is what the blob does. */
1747                 regp->fp_vert_regs[REG_DISP_CRTC] = adjusted_mode->VTotal - 5 - 1;
1748                 regp->fp_vert_regs[REG_DISP_SYNC_START] = adjusted_mode->VSyncStart - 1;
1749                 regp->fp_vert_regs[REG_DISP_SYNC_END] = adjusted_mode->VSyncEnd - 1;
1750                 regp->fp_vert_regs[REG_DISP_VALID_START] = 0;
1751                 regp->fp_vert_regs[REG_DISP_VALID_END] = adjusted_mode->VDisplay - 1;
1752
1753                 /* Quirks, maybe move them somewere else? */
1754                 if (is_lvds) {
1755                         switch(pNv->NVArch) {
1756                                 case 0x46: /* 7300GO */
1757                                         /* Only native mode needed, is there some logic to this? */
1758                                         if (mode->HDisplay == 1280 && mode->VDisplay == 800) {
1759                                                 regp->fp_horiz_regs[REG_DISP_CRTC] = 0x4c6;
1760                                         }
1761                                         break;
1762                                 default:
1763                                         break;
1764                         }
1765                 }
1766
1767                 ErrorF("Horizontal:\n");
1768                 ErrorF("REG_DISP_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_END]);
1769                 ErrorF("REG_DISP_TOTAL: 0x%X\n", regp->fp_horiz_regs[REG_DISP_TOTAL]);
1770                 ErrorF("REG_DISP_CRTC: 0x%X\n", regp->fp_horiz_regs[REG_DISP_CRTC]);
1771                 ErrorF("REG_DISP_SYNC_START: 0x%X\n", regp->fp_horiz_regs[REG_DISP_SYNC_START]);
1772                 ErrorF("REG_DISP_SYNC_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_SYNC_END]);
1773                 ErrorF("REG_DISP_VALID_START: 0x%X\n", regp->fp_horiz_regs[REG_DISP_VALID_START]);
1774                 ErrorF("REG_DISP_VALID_END: 0x%X\n", regp->fp_horiz_regs[REG_DISP_VALID_END]);
1775
1776                 ErrorF("Vertical:\n");
1777                 ErrorF("REG_DISP_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_END]);
1778                 ErrorF("REG_DISP_TOTAL: 0x%X\n", regp->fp_vert_regs[REG_DISP_TOTAL]);
1779                 ErrorF("REG_DISP_CRTC: 0x%X\n", regp->fp_vert_regs[REG_DISP_CRTC]);
1780                 ErrorF("REG_DISP_SYNC_START: 0x%X\n", regp->fp_vert_regs[REG_DISP_SYNC_START]);
1781                 ErrorF("REG_DISP_SYNC_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_SYNC_END]);
1782                 ErrorF("REG_DISP_VALID_START: 0x%X\n", regp->fp_vert_regs[REG_DISP_VALID_START]);
1783                 ErrorF("REG_DISP_VALID_END: 0x%X\n", regp->fp_vert_regs[REG_DISP_VALID_END]);
1784         }
1785
1786         /*
1787         * bit0: positive vsync
1788         * bit4: positive hsync
1789         * bit8: enable center mode
1790         * bit9: enable native mode
1791         * bit26: a bit sometimes seen on some g70 cards
1792         * bit31: set for dual link LVDS
1793         * nv10reg contains a few more things, but i don't quite get what it all means.
1794         */
1795
1796         if (pNv->Architecture >= NV_ARCH_30) {
1797                 regp->fp_control = 0x01100000;
1798         } else {
1799                 regp->fp_control = 0x00000000;
1800         }
1801
1802         if (is_fp) {
1803                 regp->fp_control |= (1 << 28);
1804         } else {
1805                 regp->fp_control |= (2 << 28);
1806                 if (pNv->Architecture < NV_ARCH_30)
1807                         regp->fp_control |= (1 << 24);
1808         }
1809
1810         /* Some 7300GO cards get a quad view if this bit is set, even though they are duallink. */
1811         /* This was seen on 2 cards. */
1812         if (is_lvds && pNv->VBIOS.fp.dual_link && pNv->NVArch != 0x46) {
1813                 regp->fp_control |= (8 << 28);
1814         }
1815
1816         /* If the special bit exists, it exists on both ramdac's */
1817         regp->fp_control |= nvReadRAMDAC0(pNv, NV_RAMDAC_FP_CONTROL) & (1 << 26);
1818
1819         if (is_fp) {
1820                 if (nv_output->scaling_mode == SCALE_PANEL) { /* panel needs to scale */
1821                         regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_CENTER;
1822                 /* This is also true for panel scaling, so we must put the panel scale check first */
1823                 } else if (mode->Clock == adjusted_mode->Clock) { /* native mode */
1824                         regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_NATIVE;
1825                 } else { /* gpu needs to scale */
1826                         regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_SCALE;
1827                 }
1828         }
1829
1830         /* Deal with vsync/hsync polarity */
1831         /* LVDS screens don't set this. */
1832         if (is_fp && !is_lvds) {
1833                 if (adjusted_mode->Flags & V_PVSYNC) {
1834                         regp->fp_control |= NV_RAMDAC_FP_CONTROL_VSYNC_POS;
1835                 }
1836
1837                 if (adjusted_mode->Flags & V_PHSYNC) {
1838                         regp->fp_control |= NV_RAMDAC_FP_CONTROL_HSYNC_POS;
1839                 }
1840         } else if (!is_lvds) {
1841                 /* The blob doesn't always do this, but often */
1842                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_VSYNC_DISABLE;
1843                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_HSYNC_DISABLE;
1844         }
1845
1846         if (is_fp) {
1847                 ErrorF("Pre-panel scaling\n");
1848                 ErrorF("panel-size:%dx%d\n", nv_output->fpWidth, nv_output->fpHeight);
1849                 panel_ratio = (nv_output->fpWidth)/(float)(nv_output->fpHeight);
1850                 ErrorF("panel_ratio=%f\n", panel_ratio);
1851                 aspect_ratio = (mode->HDisplay)/(float)(mode->VDisplay);
1852                 ErrorF("aspect_ratio=%f\n", aspect_ratio);
1853                 /* Scale factors is the so called 20.12 format, taken from Haiku */
1854                 h_scale = ((1 << 12) * mode->HDisplay)/nv_output->fpWidth;
1855                 v_scale = ((1 << 12) * mode->VDisplay)/nv_output->fpHeight;
1856                 ErrorF("h_scale=%d\n", h_scale);
1857                 ErrorF("v_scale=%d\n", v_scale);
1858
1859                 /* This can override HTOTAL and VTOTAL */
1860                 regp->debug_2 = 0;
1861
1862                 /* We want automatic scaling */
1863                 regp->debug_1 = 0;
1864
1865                 regp->fp_hvalid_start = 0;
1866                 regp->fp_hvalid_end = (nv_output->fpWidth - 1);
1867
1868                 regp->fp_vvalid_start = 0;
1869                 regp->fp_vvalid_end = (nv_output->fpHeight - 1);
1870
1871                 /* 0 = panel scaling */
1872                 if (nv_output->scaling_mode == SCALE_PANEL) {
1873                         ErrorF("Flat panel is doing the scaling.\n");
1874                 } else {
1875                         ErrorF("GPU is doing the scaling.\n");
1876
1877                         if (nv_output->scaling_mode == SCALE_ASPECT) {
1878                                 /* GPU scaling happens automaticly at a ratio of 1.33 */
1879                                 /* A 1280x1024 panel has a ratio of 1.25, we don't want to scale that at 4:3 resolutions */
1880                                 if (h_scale != (1 << 12) && (panel_ratio > (aspect_ratio + 0.10))) {
1881                                         uint32_t diff;
1882
1883                                         ErrorF("Scaling resolution on a widescreen panel\n");
1884
1885                                         /* Scaling in both directions needs to the same */
1886                                         h_scale = v_scale;
1887
1888                                         /* Set a new horizontal scale factor and enable testmode (bit12) */
1889                                         regp->debug_1 = ((h_scale >> 1) & 0xfff) | (1 << 12);
1890
1891                                         diff = nv_output->fpWidth - (((1 << 12) * mode->HDisplay)/h_scale);
1892                                         regp->fp_hvalid_start = diff/2;
1893                                         regp->fp_hvalid_end = nv_output->fpWidth - (diff/2) - 1;
1894                                 }
1895
1896                                 /* Same scaling, just for panels with aspect ratio's smaller than 1 */
1897                                 if (v_scale != (1 << 12) && (panel_ratio < (aspect_ratio - 0.10))) {
1898                                         uint32_t diff;
1899
1900                                         ErrorF("Scaling resolution on a portrait panel\n");
1901
1902                                         /* Scaling in both directions needs to the same */
1903                                         v_scale = h_scale;
1904
1905                                         /* Set a new vertical scale factor and enable testmode (bit28) */
1906                                         regp->debug_1 = (((v_scale >> 1) & 0xfff) << 16) | (1 << (12 + 16));
1907
1908                                         diff = nv_output->fpHeight - (((1 << 12) * mode->VDisplay)/v_scale);
1909                                         regp->fp_vvalid_start = diff/2;
1910                                         regp->fp_vvalid_end = nv_output->fpHeight - (diff/2) - 1;
1911                                 }
1912                         }
1913                 }
1914
1915                 ErrorF("Post-panel scaling\n");
1916         }
1917
1918         if (pNv->Architecture >= NV_ARCH_10) {
1919                 /* Bios and blob don't seem to do anything (else) */
1920                 regp->nv10_cursync = (1<<25);
1921         }
1922
1923         /* These are the common blob values, minus a few fp specific bit's */
1924         /* Let's keep the TMDS pll and fpclock running in all situations */
1925         regp->debug_0 = 0x1101100;
1926
1927         if (is_fp && nv_output->scaling_mode != SCALE_NOSCALE) {
1928                 regp->debug_0 |= NV_RAMDAC_FP_DEBUG_0_XSCALE_ENABLED;
1929                 regp->debug_0 |= NV_RAMDAC_FP_DEBUG_0_YSCALE_ENABLED;
1930         } else if (is_fp) { /* no_scale mode, so we must center it */
1931                 uint32_t diff;
1932
1933                 diff = nv_output->fpWidth - mode->HDisplay;
1934                 regp->fp_hvalid_start = diff/2;
1935                 regp->fp_hvalid_end = (nv_output->fpWidth - diff/2 - 1);
1936
1937                 diff = nv_output->fpHeight - mode->VDisplay;
1938                 regp->fp_vvalid_start = diff/2;
1939                 regp->fp_vvalid_end = (nv_output->fpHeight - diff/2 - 1);
1940         }
1941
1942         /* Is this crtc bound or output bound? */
1943         /* Does the bios TMDS script try to change this sometimes? */
1944         if (is_fp) {
1945                 /* I am not completely certain, but seems to be set only for dfp's */
1946                 regp->debug_0 |= NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED;
1947         }
1948
1949         if (output)
1950                 ErrorF("output %d debug_0 %08X\n", nv_output->output_resource, regp->debug_0);
1951
1952         /* Flatpanel support needs at least a NV10 */
1953         if (pNv->twoHeads) {
1954                 /* The blob does this differently. */
1955                 /* TODO: Find out what precisely and why. */
1956                 /* Let's not destroy any bits that were already present. */
1957                 if (pNv->FPDither || (is_lvds && pNv->VBIOS.fp.if_is_18bit)) {
1958                         if (pNv->NVArch == 0x11) {
1959                                 regp->dither = savep->dither | 0x00010000;
1960                         } else {
1961                                 regp->dither = savep->dither | 0x00000001;
1962                         }
1963                 } else {
1964                         regp->dither = savep->dither;
1965                 }
1966         }
1967
1968         /* Kindly borrowed from haiku driver */
1969         /* bit4 and bit5 activate indirect mode trough color palette */
1970         switch (pLayout->depth) {
1971                 case 32:
1972                 case 16:
1973                         regp->general = 0x00101130;
1974                         break;
1975                 case 24:
1976                 case 15:
1977                         regp->general = 0x00100130;
1978                         break;
1979                 case 8:
1980                 default:
1981                         regp->general = 0x00101100;
1982                         break;
1983         }
1984
1985         if (pNv->alphaCursor) {
1986                 /* PIPE_LONG mode, something to do with the size of the cursor? */
1987                 regp->general |= (1<<29);
1988         }
1989
1990         /* Some values the blob sets */
1991         /* This may apply to the real ramdac that is being used (for crosswired situations) */
1992         /* Nevertheless, it's unlikely to cause many problems, since the values are equal for both */
1993         regp->unk_a20 = 0x0;
1994         regp->unk_a24 = 0xfffff;
1995         regp->unk_a34 = 0x1;
1996 }
1997
1998 /**
1999  * Sets up registers for the given mode/adjusted_mode pair.
2000  *
2001  * The clocks, CRTCs and outputs attached to this CRTC must be off.
2002  *
2003  * This shouldn't enable any clocks, CRTCs, or outputs, but they should
2004  * be easily turned on/off after this.
2005  */
2006 static void
2007 nv_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode,
2008                  DisplayModePtr adjusted_mode,
2009                  int x, int y)
2010 {
2011         ScrnInfoPtr pScrn = crtc->scrn;
2012         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2013         NVPtr pNv = NVPTR(pScrn);
2014         NVFBLayout *pLayout = &pNv->CurrentLayout;
2015
2016         ErrorF("nv_crtc_mode_set is called for CRTC %d\n", nv_crtc->head);
2017
2018         xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Mode on CRTC %d\n", nv_crtc->head);
2019         xf86PrintModeline(pScrn->scrnIndex, mode);
2020         NVCrtcSetOwner(crtc);
2021
2022         nv_crtc_mode_set_vga(crtc, mode, adjusted_mode);
2023         nv_crtc_mode_set_regs(crtc, mode, adjusted_mode);
2024         nv_crtc_mode_set_ramdac_regs(crtc, mode, adjusted_mode);
2025
2026         NVVgaProtect(crtc, TRUE);
2027         nv_crtc_load_state_ramdac(crtc, &pNv->ModeReg);
2028         nv_crtc_load_state_ext(crtc, &pNv->ModeReg, FALSE);
2029         if (pLayout->depth != 8)
2030                 NVCrtcLoadPalette(crtc);
2031         nv_crtc_load_state_vga(crtc, &pNv->ModeReg);
2032         if (pNv->Architecture == NV_ARCH_40) {
2033                 nv40_crtc_load_state_pll(crtc, &pNv->ModeReg);
2034         } else {
2035                 nv_crtc_load_state_pll(pNv, &pNv->ModeReg);
2036         }
2037
2038         NVVgaProtect(crtc, FALSE);
2039
2040         NVCrtcSetBase(crtc, x, y);
2041
2042 #if X_BYTE_ORDER == X_BIG_ENDIAN
2043         /* turn on LFB swapping */
2044         {
2045                 unsigned char tmp;
2046
2047                 tmp = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING);
2048                 tmp |= (1 << 7);
2049                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_SWAPPING, tmp);
2050         }
2051 #endif
2052 }
2053
2054 /* This functions generates data that is not saved, but still is needed. */
2055 void nv_crtc_restore_generate(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2056 {
2057         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2058         ScrnInfoPtr pScrn = crtc->scrn;
2059         NVPtr pNv = NVPTR(pScrn);
2060         int i;
2061         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
2062
2063         /* It's a good idea to also save a default palette on shutdown. */
2064         for (i = 0; i < 256; i++) {
2065                 regp->DAC[i*3] = i;
2066                 regp->DAC[(i*3)+1] = i;
2067                 regp->DAC[(i*3)+2] = i;
2068         }
2069
2070         /* Noticed that reading this variable is problematic on one card. */
2071         if (pNv->NVArch == 0x11)
2072                 state->sel_clk = 0x0;
2073 }
2074
2075 void nv_crtc_save(xf86CrtcPtr crtc)
2076 {
2077         ScrnInfoPtr pScrn = crtc->scrn;
2078         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2079         NVPtr pNv = NVPTR(pScrn);
2080
2081         ErrorF("nv_crtc_save is called for CRTC %d\n", nv_crtc->head);
2082
2083         /* We just came back from terminal, so unlock */
2084         NVCrtcLockUnlock(crtc, FALSE);
2085
2086         NVCrtcSetOwner(crtc);
2087         nv_crtc_save_state_ramdac(crtc, &pNv->SavedReg);
2088         nv_crtc_save_state_vga(crtc, &pNv->SavedReg);
2089         nv_crtc_save_state_ext(crtc, &pNv->SavedReg);
2090         if (pNv->Architecture == NV_ARCH_40) {
2091                 nv40_crtc_save_state_pll(pNv, &pNv->SavedReg);
2092         } else {
2093                 nv_crtc_save_state_pll(pNv, &pNv->SavedReg);
2094         }
2095 }
2096
2097 void nv_crtc_restore(xf86CrtcPtr crtc)
2098 {
2099         ScrnInfoPtr pScrn = crtc->scrn;
2100         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2101         NVPtr pNv = NVPTR(pScrn);
2102         RIVA_HW_STATE *state;
2103         NVCrtcRegPtr savep;
2104
2105         state = &pNv->SavedReg;
2106         savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
2107
2108         ErrorF("nv_crtc_restore is called for CRTC %d\n", nv_crtc->head);
2109
2110         NVCrtcSetOwner(crtc);
2111
2112         /* Just to be safe */
2113         NVCrtcLockUnlock(crtc, FALSE);
2114
2115         NVVgaProtect(crtc, TRUE);
2116         nv_crtc_restore_generate(crtc, &pNv->SavedReg);
2117         nv_crtc_load_state_ramdac(crtc, &pNv->SavedReg);
2118         nv_crtc_load_state_ext(crtc, &pNv->SavedReg, TRUE);
2119         if (savep->general & 0x30) /* Palette mode */
2120                 NVCrtcLoadPalette(crtc);
2121         nv_crtc_load_state_vga(crtc, &pNv->SavedReg);
2122
2123         /* Force restoring pll's. */
2124         state->vpll_changed[0] = TRUE;
2125         state->vpll_changed[1] = TRUE;
2126
2127         if (pNv->Architecture == NV_ARCH_40) {
2128                 nv40_crtc_load_state_pll(crtc, &pNv->SavedReg);
2129         } else {
2130                 nv_crtc_load_state_pll(pNv, &pNv->SavedReg);
2131         }
2132         nvWriteVGA(pNv, NV_VGA_CRTCX_OWNER, pNv->vtOWNER);
2133         NVVgaProtect(crtc, FALSE);
2134
2135         nv_crtc->last_dpms = NV_DPMS_CLEARED;
2136 }
2137
2138 void
2139 NVResetCrtcConfig(xf86CrtcPtr crtc, Bool set)
2140 {
2141         ScrnInfoPtr pScrn = crtc->scrn;
2142         NVPtr pNv = NVPTR(pScrn);
2143         uint32_t val = 0;
2144
2145         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2146
2147         if (set) {
2148                 NVCrtcRegPtr regp;
2149
2150                 regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
2151                 val = regp->head;
2152         }
2153
2154         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL, val);
2155 }
2156
2157 void nv_crtc_prepare(xf86CrtcPtr crtc)
2158 {
2159         ScrnInfoPtr pScrn = crtc->scrn;
2160         NVPtr pNv = NVPTR(pScrn);
2161         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2162
2163         ErrorF("nv_crtc_prepare is called for CRTC %d\n", nv_crtc->head);
2164
2165         /* Just in case */
2166         NVCrtcLockUnlock(crtc, 0);
2167
2168         NVResetCrtcConfig(crtc, FALSE);
2169
2170         crtc->funcs->dpms(crtc, DPMSModeOff);
2171
2172         /* Sync the engine before adjust mode */
2173         if (pNv->EXADriverPtr) {
2174                 exaMarkSync(pScrn->pScreen);
2175                 exaWaitSync(pScrn->pScreen);
2176         }
2177
2178         NVCrtcBlankScreen(crtc, FALSE); /* Blank screen */
2179
2180         /* Some more preperation. */
2181         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_CONFIG, 0x1); /* Go to non-vga mode/out of enhanced mode */
2182         if (pNv->Architecture == NV_ARCH_40) {
2183                 uint32_t reg900 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_900);
2184                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_900, reg900 & ~0x10000);
2185         }
2186 }
2187
2188 void nv_crtc_commit(xf86CrtcPtr crtc)
2189 {
2190         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2191         ErrorF("nv_crtc_commit for CRTC %d\n", nv_crtc->head);
2192
2193         crtc->funcs->dpms (crtc, DPMSModeOn);
2194
2195         if (crtc->scrn->pScreen != NULL)
2196                 xf86_reload_cursors (crtc->scrn->pScreen);
2197
2198         NVResetCrtcConfig(crtc, TRUE);
2199 }
2200
2201 static Bool nv_crtc_lock(xf86CrtcPtr crtc)
2202 {
2203         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2204         ErrorF("nv_crtc_lock is called for CRTC %d\n", nv_crtc->head);
2205
2206         return FALSE;
2207 }
2208
2209 static void nv_crtc_unlock(xf86CrtcPtr crtc)
2210 {
2211         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2212         ErrorF("nv_crtc_unlock is called for CRTC %d\n", nv_crtc->head);
2213 }
2214
2215 static void
2216 nv_crtc_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
2217                                         int size)
2218 {
2219         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2220         ScrnInfoPtr pScrn = crtc->scrn;
2221         NVPtr pNv = NVPTR(pScrn);
2222         int i, j;
2223
2224         NVCrtcRegPtr regp;
2225         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
2226
2227         switch (pNv->CurrentLayout.depth) {
2228         case 15:
2229                 /* R5G5B5 */
2230                 /* We've got 5 bit (32 values) colors and 256 registers for each color */
2231                 for (i = 0; i < 32; i++) {
2232                         for (j = 0; j < 8; j++) {
2233                                 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
2234                                 regp->DAC[(i*8 + j) * 3 + 1] = green[i] >> 8;
2235                                 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
2236                         }
2237                 }
2238                 break;
2239         case 16:
2240                 /* R5G6B5 */
2241                 /* First deal with the 5 bit colors */
2242                 for (i = 0; i < 32; i++) {
2243                         for (j = 0; j < 8; j++) {
2244                                 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
2245                                 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
2246                         }
2247                 }
2248                 /* Now deal with the 6 bit color */
2249                 for (i = 0; i < 64; i++) {
2250                         for (j = 0; j < 4; j++) {
2251                                 regp->DAC[(i*4 + j) * 3 + 1] = green[i] >> 8;
2252                         }
2253                 }
2254                 break;
2255         default:
2256                 /* R8G8B8 */
2257                 for (i = 0; i < 256; i++) {
2258                         regp->DAC[i * 3] = red[i] >> 8;
2259                         regp->DAC[(i * 3) + 1] = green[i] >> 8;
2260                         regp->DAC[(i * 3) + 2] = blue[i] >> 8;
2261                 }
2262                 break;
2263         }
2264
2265         NVCrtcLoadPalette(crtc);
2266 }
2267
2268 /**
2269  * Allocates memory for a locked-in-framebuffer shadow of the given
2270  * width and height for this CRTC's rotated shadow framebuffer.
2271  */
2272  
2273 static void *
2274 nv_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
2275 {
2276         ErrorF("nv_crtc_shadow_allocate is called\n");
2277         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2278         ScrnInfoPtr pScrn = crtc->scrn;
2279 #if !NOUVEAU_EXA_PIXMAPS
2280         ScreenPtr pScreen = pScrn->pScreen;
2281 #endif /* !NOUVEAU_EXA_PIXMAPS */
2282         NVPtr pNv = NVPTR(pScrn);
2283         void *offset;
2284
2285         unsigned long rotate_pitch;
2286         int size, align = 64;
2287
2288         rotate_pitch = pScrn->displayWidth * (pScrn->bitsPerPixel/8);
2289         size = rotate_pitch * height;
2290
2291         assert(nv_crtc->shadow == NULL);
2292 #if NOUVEAU_EXA_PIXMAPS
2293         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
2294                         align, size, &nv_crtc->shadow)) {
2295                 ErrorF("Failed to allocate memory for shadow buffer!\n");
2296                 return NULL;
2297         }
2298
2299         if (nv_crtc->shadow && nouveau_bo_map(nv_crtc->shadow, NOUVEAU_BO_RDWR)) {
2300                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2301                                 "Failed to map shadow buffer.\n");
2302                 return NULL;
2303         }
2304
2305         offset = nv_crtc->shadow->map;
2306 #else
2307         nv_crtc->shadow = exaOffscreenAlloc(pScreen, size, align, TRUE, NULL, NULL);
2308         if (nv_crtc->shadow == NULL) {
2309                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2310                         "Couldn't allocate shadow memory for rotated CRTC\n");
2311                 return NULL;
2312         }
2313         offset = pNv->FB->map + nv_crtc->shadow->offset;
2314 #endif /* NOUVEAU_EXA_PIXMAPS */
2315
2316         return offset;
2317 }
2318
2319 /**
2320  * Creates a pixmap for this CRTC's rotated shadow framebuffer.
2321  */
2322 static PixmapPtr
2323 nv_crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
2324 {
2325         ErrorF("nv_crtc_shadow_create is called\n");
2326         ScrnInfoPtr pScrn = crtc->scrn;
2327 #if NOUVEAU_EXA_PIXMAPS
2328         ScreenPtr pScreen = pScrn->pScreen;
2329         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2330 #endif /* NOUVEAU_EXA_PIXMAPS */
2331         unsigned long rotate_pitch;
2332         PixmapPtr rotate_pixmap;
2333 #if NOUVEAU_EXA_PIXMAPS
2334         struct nouveau_pixmap *nvpix;
2335 #endif /* NOUVEAU_EXA_PIXMAPS */
2336
2337         if (!data)
2338                 data = crtc->funcs->shadow_allocate (crtc, width, height);
2339
2340         rotate_pitch = pScrn->displayWidth * (pScrn->bitsPerPixel/8);
2341
2342 #if NOUVEAU_EXA_PIXMAPS
2343         /* Create a dummy pixmap, to get a private that will be accepted by the system.*/
2344         rotate_pixmap = pScreen->CreatePixmap(pScreen, 
2345                                                                 0, /* width */
2346                                                                 0, /* height */
2347         #ifdef CREATE_PIXMAP_USAGE_SCRATCH /* there seems to have been no api bump */
2348                                                                 pScrn->depth,
2349                                                                 0);
2350         #else
2351                                                                 pScrn->depth);
2352         #endif /* CREATE_PIXMAP_USAGE_SCRATCH */
2353 #else
2354         rotate_pixmap = GetScratchPixmapHeader(pScrn->pScreen,
2355                                                                 width, height,
2356                                                                 pScrn->depth,
2357                                                                 pScrn->bitsPerPixel,
2358                                                                 rotate_pitch,
2359                                                                 data);
2360 #endif /* NOUVEAU_EXA_PIXMAPS */
2361
2362         if (rotate_pixmap == NULL) {
2363                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2364                         "Couldn't allocate shadow pixmap for rotated CRTC\n");
2365         }
2366
2367 #if NOUVEAU_EXA_PIXMAPS
2368         nvpix = exaGetPixmapDriverPrivate(rotate_pixmap);
2369         if (!nvpix) {
2370                 ErrorF("No shadow private, stage 1\n");
2371         } else {
2372                 nvpix->bo = nv_crtc->shadow;
2373                 nvpix->mapped = TRUE;
2374         }
2375
2376         /* Modify the pixmap to actually be the one we need. */
2377         pScreen->ModifyPixmapHeader(rotate_pixmap,
2378                                         width,
2379                                         height,
2380                                         pScrn->depth,
2381                                         pScrn->bitsPerPixel,
2382                                         rotate_pitch,
2383                                         data);
2384
2385         nvpix = exaGetPixmapDriverPrivate(rotate_pixmap);
2386         if (!nvpix || !nvpix->bo)
2387                 ErrorF("No shadow private, stage 2\n");
2388 #endif /* NOUVEAU_EXA_PIXMAPS */
2389
2390         return rotate_pixmap;
2391 }
2392
2393 static void
2394 nv_crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
2395 {
2396         ErrorF("nv_crtc_shadow_destroy is called\n");
2397         ScrnInfoPtr pScrn = crtc->scrn;
2398         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2399         ScreenPtr pScreen = pScrn->pScreen;
2400
2401         if (rotate_pixmap) { /* This should also unmap the buffer object if relevant. */
2402                 pScreen->DestroyPixmap(rotate_pixmap);
2403         }
2404
2405 #if !NOUVEAU_EXA_PIXMAPS
2406         if (data && nv_crtc->shadow) {
2407                 exaOffscreenFree(pScreen, nv_crtc->shadow);
2408         }
2409 #endif /* !NOUVEAU_EXA_PIXMAPS */
2410
2411         nv_crtc->shadow = NULL;
2412 }
2413
2414 /* NV04-NV10 doesn't support alpha cursors */
2415 static const xf86CrtcFuncsRec nv_crtc_funcs = {
2416         .dpms = nv_crtc_dpms,
2417         .save = nv_crtc_save, /* XXX */
2418         .restore = nv_crtc_restore, /* XXX */
2419         .mode_fixup = nv_crtc_mode_fixup,
2420         .mode_set = nv_crtc_mode_set,
2421         .prepare = nv_crtc_prepare,
2422         .commit = nv_crtc_commit,
2423         .destroy = NULL, /* XXX */
2424         .lock = nv_crtc_lock,
2425         .unlock = nv_crtc_unlock,
2426         .set_cursor_colors = nv_crtc_set_cursor_colors,
2427         .set_cursor_position = nv_crtc_set_cursor_position,
2428         .show_cursor = nv_crtc_show_cursor,
2429         .hide_cursor = nv_crtc_hide_cursor,
2430         .load_cursor_image = nv_crtc_load_cursor_image,
2431         .gamma_set = nv_crtc_gamma_set,
2432         .shadow_create = nv_crtc_shadow_create,
2433         .shadow_allocate = nv_crtc_shadow_allocate,
2434         .shadow_destroy = nv_crtc_shadow_destroy,
2435 };
2436
2437 /* NV11 and up has support for alpha cursors. */ 
2438 /* Due to different maximum sizes we cannot allow it to use normal cursors */
2439 static const xf86CrtcFuncsRec nv11_crtc_funcs = {
2440         .dpms = nv_crtc_dpms,
2441         .save = nv_crtc_save, /* XXX */
2442         .restore = nv_crtc_restore, /* XXX */
2443         .mode_fixup = nv_crtc_mode_fixup,
2444         .mode_set = nv_crtc_mode_set,
2445         .prepare = nv_crtc_prepare,
2446         .commit = nv_crtc_commit,
2447         .destroy = NULL, /* XXX */
2448         .lock = nv_crtc_lock,
2449         .unlock = nv_crtc_unlock,
2450         .set_cursor_colors = NULL, /* Alpha cursors do not need this */
2451         .set_cursor_position = nv_crtc_set_cursor_position,
2452         .show_cursor = nv_crtc_show_cursor,
2453         .hide_cursor = nv_crtc_hide_cursor,
2454         .load_cursor_argb = nv_crtc_load_cursor_argb,
2455         .gamma_set = nv_crtc_gamma_set,
2456         .shadow_create = nv_crtc_shadow_create,
2457         .shadow_allocate = nv_crtc_shadow_allocate,
2458         .shadow_destroy = nv_crtc_shadow_destroy,
2459 };
2460
2461
2462 void
2463 nv_crtc_init(ScrnInfoPtr pScrn, int crtc_num)
2464 {
2465         NVPtr pNv = NVPTR(pScrn);
2466         xf86CrtcPtr crtc;
2467         NVCrtcPrivatePtr nv_crtc;
2468
2469         if (pNv->NVArch >= 0x11) {
2470                 crtc = xf86CrtcCreate (pScrn, &nv11_crtc_funcs);
2471         } else {
2472                 crtc = xf86CrtcCreate (pScrn, &nv_crtc_funcs);
2473         }
2474         if (crtc == NULL)
2475                 return;
2476
2477         nv_crtc = xnfcalloc (sizeof (NVCrtcPrivateRec), 1);
2478         nv_crtc->head = crtc_num;
2479         nv_crtc->last_dpms = NV_DPMS_CLEARED;
2480
2481         crtc->driver_private = nv_crtc;
2482
2483         NVCrtcLockUnlock(crtc, FALSE);
2484 }
2485
2486 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2487 {
2488         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2489         int i;
2490         NVCrtcRegPtr regp;
2491
2492         regp = &state->crtc_reg[nv_crtc->head];
2493
2494         NVWriteMiscOut(crtc, regp->MiscOutReg);
2495
2496         for (i = 1; i < 5; i++)
2497                 NVWriteVgaSeq(crtc, i, regp->Sequencer[i]);
2498
2499         /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
2500         NVWriteVgaCrtc(crtc, 17, regp->CRTC[17] & ~0x80);
2501
2502         for (i = 0; i < 25; i++)
2503                 NVWriteVgaCrtc(crtc, i, regp->CRTC[i]);
2504
2505         for (i = 0; i < 9; i++)
2506                 NVWriteVgaGr(crtc, i, regp->Graphics[i]);
2507
2508         NVEnablePalette(crtc);
2509         for (i = 0; i < 21; i++)
2510                 NVWriteVgaAttr(crtc, i, regp->Attribute[i]);
2511
2512         NVDisablePalette(crtc);
2513 }
2514
2515 static void nv_crtc_fix_nv40_hw_cursor(xf86CrtcPtr crtc)
2516 {
2517         /* TODO - implement this properly */
2518         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2519         ScrnInfoPtr pScrn = crtc->scrn;
2520         NVPtr pNv = NVPTR(pScrn);
2521
2522         if (pNv->Architecture == NV_ARCH_40) {  /* HW bug */
2523                 volatile uint32_t curpos = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_CURSOR_POS);
2524                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_CURSOR_POS, curpos);
2525         }
2526 }
2527 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state, Bool override)
2528 {
2529         ScrnInfoPtr pScrn = crtc->scrn;
2530         NVPtr pNv = NVPTR(pScrn);    
2531         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2532         NVCrtcRegPtr regp;
2533         int i;
2534
2535         regp = &state->crtc_reg[nv_crtc->head];
2536
2537         /* If we ever get down to pre-nv10 cards, then we must reinstate some limits. */
2538         nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 1);
2539         nvWriteVIDEO(pNv, NV_PVIDEO_INTR_EN, 0);
2540         nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(0), 0);
2541         nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(1), 0);
2542         nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(0), pNv->VRAMPhysicalSize - 1);
2543         nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(1), pNv->VRAMPhysicalSize - 1);
2544         nvWriteMC(pNv, 0x1588, 0);
2545
2546         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER, regp->CRTC[NV_VGA_CRTCX_BUFFER]);
2547         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG, regp->cursorConfig);
2548         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO, regp->gpio);
2549         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0830, regp->unk830);
2550         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0834, regp->unk834);
2551         if (pNv->Architecture == NV_ARCH_40) {
2552                 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_0850, regp->unk850);
2553                 nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_081C, regp->unk81c);
2554         }
2555
2556         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_CONFIG, regp->config);
2557         if (pNv->Architecture == NV_ARCH_40) {
2558                 uint32_t reg900 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_900);
2559                 if (regp->config == 0x2) { /* enhanced "horizontal only" non-vga mode */
2560                         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_900, reg900 | 0x10000);
2561                 } else {
2562                         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_900, reg900 & ~0x10000);
2563                 }
2564         }
2565
2566         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0, regp->CRTC[NV_VGA_CRTCX_REPAINT0]);
2567         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1, regp->CRTC[NV_VGA_CRTCX_REPAINT1]);
2568         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LSR, regp->CRTC[NV_VGA_CRTCX_LSR]);
2569         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL, regp->CRTC[NV_VGA_CRTCX_PIXEL]);
2570         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_LCD, regp->CRTC[NV_VGA_CRTCX_LCD]);
2571         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_HEB, regp->CRTC[NV_VGA_CRTCX_HEB]);
2572         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1, regp->CRTC[NV_VGA_CRTCX_FIFO1]);
2573         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0, regp->CRTC[NV_VGA_CRTCX_FIFO0]);
2574         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM]);
2575         if (pNv->Architecture >= NV_ARCH_30) {
2576                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30, regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30]);
2577         }
2578
2579         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0, regp->CRTC[NV_VGA_CRTCX_CURCTL0]);
2580         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1, regp->CRTC[NV_VGA_CRTCX_CURCTL1]);
2581         nv_crtc_fix_nv40_hw_cursor(crtc);
2582         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2, regp->CRTC[NV_VGA_CRTCX_CURCTL2]);
2583         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE, regp->CRTC[NV_VGA_CRTCX_INTERLACE]);
2584
2585         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_26, regp->CRTC[NV_VGA_CRTCX_26]);
2586         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3B, regp->CRTC[NV_VGA_CRTCX_3B]);
2587         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_3C, regp->CRTC[NV_VGA_CRTCX_3C]);
2588         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA, regp->CRTC[NV_VGA_CRTCX_EXTRA]);
2589         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_43, regp->CRTC[NV_VGA_CRTCX_43]);
2590         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_45, regp->CRTC[NV_VGA_CRTCX_45]);
2591         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_4B, regp->CRTC[NV_VGA_CRTCX_4B]);
2592         NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_52, regp->CRTC[NV_VGA_CRTCX_52]);
2593         /* NV11 and NV20 stop at 0x52. */
2594         if (pNv->NVArch >= 0x17 && pNv->twoHeads) {
2595                 if (override)
2596                         for (i = 0; i < 0x10; i++)
2597                                 NVWriteVGACR5758(pNv, nv_crtc->head, i, regp->CR58[i]);
2598
2599                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING, regp->CRTC[NV_VGA_CRTCX_FP_HTIMING]);
2600                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING, regp->CRTC[NV_VGA_CRTCX_FP_VTIMING]);
2601
2602                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_59, regp->CRTC[NV_VGA_CRTCX_59]);
2603
2604                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_85, regp->CRTC[NV_VGA_CRTCX_85]);
2605                 NVWriteVgaCrtc(crtc, NV_VGA_CRTCX_86, regp->CRTC[NV_VGA_CRTCX_86]);
2606         }
2607
2608         /* Setting 1 on this value gives you interrupts for every vblank period. */
2609         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_EN_0, 0);
2610         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_INTR_0, NV_CRTC_INTR_VBLANK);
2611
2612         pNv->CurrentState = state;
2613 }
2614
2615 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2616 {
2617         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2618         int i;
2619         NVCrtcRegPtr regp;
2620
2621         regp = &state->crtc_reg[nv_crtc->head];
2622
2623         regp->MiscOutReg = NVReadMiscOut(crtc);
2624
2625         for (i = 0; i < 25; i++)
2626                 regp->CRTC[i] = NVReadVgaCrtc(crtc, i);
2627
2628         NVEnablePalette(crtc);
2629         for (i = 0; i < 21; i++)
2630                 regp->Attribute[i] = NVReadVgaAttr(crtc, i);
2631         NVDisablePalette(crtc);
2632
2633         for (i = 0; i < 9; i++)
2634                 regp->Graphics[i] = NVReadVgaGr(crtc, i);
2635
2636         for (i = 1; i < 5; i++)
2637                 regp->Sequencer[i] = NVReadVgaSeq(crtc, i);
2638   
2639 }
2640
2641 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2642 {
2643         ScrnInfoPtr pScrn = crtc->scrn;
2644         NVPtr pNv = NVPTR(pScrn);    
2645         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2646         NVCrtcRegPtr regp;
2647         int i;
2648
2649         regp = &state->crtc_reg[nv_crtc->head];
2650
2651         /* If we ever get down to pre-nv10 cards, then we must reinstate some limits. */
2652         regp->CRTC[NV_VGA_CRTCX_LCD] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LCD);
2653         regp->CRTC[NV_VGA_CRTCX_REPAINT0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT0);
2654         regp->CRTC[NV_VGA_CRTCX_REPAINT1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_REPAINT1);
2655         regp->CRTC[NV_VGA_CRTCX_LSR] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_LSR);
2656         regp->CRTC[NV_VGA_CRTCX_PIXEL] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_PIXEL);
2657         regp->CRTC[NV_VGA_CRTCX_HEB] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_HEB);
2658         regp->CRTC[NV_VGA_CRTCX_FIFO1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO1);
2659
2660         regp->CRTC[NV_VGA_CRTCX_FIFO0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO0);
2661         regp->CRTC[NV_VGA_CRTCX_FIFO_LWM] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM);
2662         regp->CRTC[NV_VGA_CRTCX_BUFFER] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_BUFFER);
2663         if (pNv->Architecture >= NV_ARCH_30) {
2664                 regp->CRTC[NV_VGA_CRTCX_FIFO_LWM_NV30] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FIFO_LWM_NV30);
2665         }
2666         regp->CRTC[NV_VGA_CRTCX_CURCTL0] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL0);
2667         regp->CRTC[NV_VGA_CRTCX_CURCTL1] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL1);
2668         regp->CRTC[NV_VGA_CRTCX_CURCTL2] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_CURCTL2);
2669         regp->CRTC[NV_VGA_CRTCX_INTERLACE] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_INTERLACE);
2670
2671         regp->gpio = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_GPIO);
2672         regp->unk830 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0830);
2673         regp->unk834 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0834);
2674         if (pNv->Architecture == NV_ARCH_40) {
2675                 regp->unk850 = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_0850);
2676                 regp->unk81c = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_081C);
2677         }
2678
2679         regp->config = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_CONFIG);
2680
2681         regp->head = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_FSEL);
2682         regp->crtcOwner = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_OWNER);
2683         regp->CRTC[NV_VGA_CRTCX_EXTRA] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_EXTRA);
2684
2685         regp->cursorConfig = nvReadCRTC(pNv, nv_crtc->head, NV_CRTC_CURSOR_CONFIG);
2686
2687         regp->CRTC[NV_VGA_CRTCX_26] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_26);
2688         regp->CRTC[NV_VGA_CRTCX_3B] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3B);
2689         regp->CRTC[NV_VGA_CRTCX_3C] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_3C);
2690         regp->CRTC[NV_VGA_CRTCX_43] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_43);
2691         regp->CRTC[NV_VGA_CRTCX_45] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_45);
2692         regp->CRTC[NV_VGA_CRTCX_4B] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_4B);
2693         regp->CRTC[NV_VGA_CRTCX_52] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_52);
2694         /* NV11 and NV20 don't have this, they stop at 0x52. */
2695         if (pNv->NVArch >= 0x17 && pNv->twoHeads) {
2696                 for (i = 0; i < 0x10; i++)
2697                         regp->CR58[i] = NVReadVGACR5758(pNv, nv_crtc->head, i);
2698
2699                 regp->CRTC[NV_VGA_CRTCX_59] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_59);
2700                 regp->CRTC[NV_VGA_CRTCX_FP_HTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_HTIMING);
2701                 regp->CRTC[NV_VGA_CRTCX_FP_VTIMING] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_FP_VTIMING);
2702
2703                 regp->CRTC[NV_VGA_CRTCX_85] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_85);
2704                 regp->CRTC[NV_VGA_CRTCX_86] = NVReadVgaCrtc(crtc, NV_VGA_CRTCX_86);
2705         }
2706 }
2707
2708 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2709 {
2710         ScrnInfoPtr pScrn = crtc->scrn;
2711         NVPtr pNv = NVPTR(pScrn);    
2712         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2713         NVCrtcRegPtr regp;
2714         int i;
2715
2716         regp = &state->crtc_reg[nv_crtc->head];
2717
2718         regp->general = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_GENERAL_CONTROL);
2719
2720         regp->fp_control        = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL);
2721         regp->debug_0   = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_0);
2722         regp->debug_1   = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_1);
2723         regp->debug_2   = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_2);
2724
2725         regp->unk_a20 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A20);
2726         regp->unk_a24 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A24);
2727         regp->unk_a34 = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A34);
2728
2729         if (pNv->NVArch == 0x11) {
2730                 regp->dither = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_DITHER_NV11);
2731         } else if (pNv->twoHeads) {
2732                 regp->dither = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DITHER);
2733         }
2734         regp->nv10_cursync = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_NV10_CURSYNC);
2735
2736         /* The regs below are 0 for non-flatpanels, so you can load and save them */
2737
2738         for (i = 0; i < 7; i++) {
2739                 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
2740                 regp->fp_horiz_regs[i] = nvReadRAMDAC(pNv, nv_crtc->head, ramdac_reg);
2741         }
2742
2743         for (i = 0; i < 7; i++) {
2744                 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
2745                 regp->fp_vert_regs[i] = nvReadRAMDAC(pNv, nv_crtc->head, ramdac_reg);
2746         }
2747
2748         regp->fp_hvalid_start = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_START);
2749         regp->fp_hvalid_end = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_END);
2750         regp->fp_vvalid_start = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_START);
2751         regp->fp_vvalid_end = nvReadRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_END);
2752 }
2753
2754 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
2755 {
2756         ScrnInfoPtr pScrn = crtc->scrn;
2757         NVPtr pNv = NVPTR(pScrn);    
2758         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2759         NVCrtcRegPtr regp;
2760         int i;
2761
2762         regp = &state->crtc_reg[nv_crtc->head];
2763
2764         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_GENERAL_CONTROL, regp->general);
2765
2766         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_CONTROL, regp->fp_control);
2767         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_0, regp->debug_0);
2768         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_1, regp->debug_1);
2769         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DEBUG_2, regp->debug_2);
2770         if (pNv->NVArch == 0x30) { /* For unknown purposes. */
2771                 uint32_t reg890 = nvReadRAMDAC(pNv, nv_crtc->head, NV30_RAMDAC_890);
2772                 nvWriteRAMDAC(pNv, nv_crtc->head, NV30_RAMDAC_89C, reg890);
2773         }
2774
2775         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A20, regp->unk_a20);
2776         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A24, regp->unk_a24);
2777         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_A34, regp->unk_a34);
2778
2779         if (pNv->NVArch == 0x11) {
2780                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_DITHER_NV11, regp->dither);
2781         } else if (pNv->twoHeads) {
2782                 nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_DITHER, regp->dither);
2783         }
2784         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_NV10_CURSYNC, regp->nv10_cursync);
2785
2786         /* The regs below are 0 for non-flatpanels, so you can load and save them */
2787
2788         for (i = 0; i < 7; i++) {
2789                 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
2790                 nvWriteRAMDAC(pNv, nv_crtc->head, ramdac_reg, regp->fp_horiz_regs[i]);
2791         }
2792
2793         for (i = 0; i < 7; i++) {
2794                 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
2795                 nvWriteRAMDAC(pNv, nv_crtc->head, ramdac_reg, regp->fp_vert_regs[i]);
2796         }
2797
2798         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_START, regp->fp_hvalid_start);
2799         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_HVALID_END, regp->fp_hvalid_end);
2800         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_START, regp->fp_vvalid_start);
2801         nvWriteRAMDAC(pNv, nv_crtc->head, NV_RAMDAC_FP_VVALID_END, regp->fp_vvalid_end);
2802 }
2803
2804 void
2805 NVCrtcSetBase (xf86CrtcPtr crtc, int x, int y)
2806 {
2807         ScrnInfoPtr pScrn = crtc->scrn;
2808         NVPtr pNv = NVPTR(pScrn);    
2809         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2810         NVFBLayout *pLayout = &pNv->CurrentLayout;
2811         uint32_t start = 0;
2812
2813         ErrorF("NVCrtcSetBase: x: %d y: %d\n", x, y);
2814
2815         start += ((y * pScrn->displayWidth + x) * (pLayout->bitsPerPixel/8));
2816         if (crtc->rotatedData != NULL) { /* we do not exist on the real framebuffer */
2817 #if NOUVEAU_EXA_PIXMAPS
2818                 start = nv_crtc->shadow->offset;
2819 #else
2820                 start = pNv->FB->offset + nv_crtc->shadow->offset; /* We do exist relative to the framebuffer */
2821 #endif
2822         } else {
2823                 start += pNv->FB->offset;
2824         }
2825
2826         /* 30 bits addresses in 32 bits according to haiku */
2827         nvWriteCRTC(pNv, nv_crtc->head, NV_CRTC_START, start & 0xfffffffc);
2828
2829         /* set NV4/NV10 byte adress: (bit0 - 1) */
2830         NVWriteVgaAttr(crtc, 0x13, (start & 0x3) << 1);
2831
2832         crtc->x = x;
2833         crtc->y = y;
2834 }
2835
2836 static void NVCrtcWriteDacMask(xf86CrtcPtr crtc, uint8_t value)
2837 {
2838   ScrnInfoPtr pScrn = crtc->scrn;
2839   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2840   NVPtr pNv = NVPTR(pScrn);
2841   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2842
2843   NV_WR08(pDACReg, VGA_DAC_MASK, value);
2844 }
2845
2846 static uint8_t NVCrtcReadDacMask(xf86CrtcPtr crtc)
2847 {
2848   ScrnInfoPtr pScrn = crtc->scrn;
2849   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2850   NVPtr pNv = NVPTR(pScrn);
2851   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2852   
2853   return NV_RD08(pDACReg, VGA_DAC_MASK);
2854 }
2855
2856 static void NVCrtcWriteDacReadAddr(xf86CrtcPtr crtc, uint8_t value)
2857 {
2858   ScrnInfoPtr pScrn = crtc->scrn;
2859   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2860   NVPtr pNv = NVPTR(pScrn);
2861   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2862
2863   NV_WR08(pDACReg, VGA_DAC_READ_ADDR, value);
2864 }
2865
2866 static void NVCrtcWriteDacWriteAddr(xf86CrtcPtr crtc, uint8_t value)
2867 {
2868   ScrnInfoPtr pScrn = crtc->scrn;
2869   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2870   NVPtr pNv = NVPTR(pScrn);
2871   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2872
2873   NV_WR08(pDACReg, VGA_DAC_WRITE_ADDR, value);
2874 }
2875
2876 static void NVCrtcWriteDacData(xf86CrtcPtr crtc, uint8_t value)
2877 {
2878   ScrnInfoPtr pScrn = crtc->scrn;
2879   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2880   NVPtr pNv = NVPTR(pScrn);
2881   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2882
2883   NV_WR08(pDACReg, VGA_DAC_DATA, value);
2884 }
2885
2886 static uint8_t NVCrtcReadDacData(xf86CrtcPtr crtc, uint8_t value)
2887 {
2888   ScrnInfoPtr pScrn = crtc->scrn;
2889   NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2890   NVPtr pNv = NVPTR(pScrn);
2891   volatile uint8_t *pDACReg = nv_crtc->head ? pNv->PDIO1 : pNv->PDIO0;
2892
2893   return NV_RD08(pDACReg, VGA_DAC_DATA);
2894 }
2895
2896 void NVCrtcLoadPalette(xf86CrtcPtr crtc)
2897 {
2898         int i;
2899         NVCrtcPrivatePtr nv_crtc = crtc->driver_private;
2900         NVCrtcRegPtr regp;
2901         ScrnInfoPtr pScrn = crtc->scrn;
2902         NVPtr pNv = NVPTR(pScrn);
2903
2904         regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
2905
2906         NVCrtcSetOwner(crtc);
2907         NVCrtcWriteDacMask(crtc, 0xff);
2908         NVCrtcWriteDacWriteAddr(crtc, 0x00);
2909
2910         for (i = 0; i<768; i++) {
2911                 NVCrtcWriteDacData(crtc, regp->DAC[i]);
2912         }
2913         NVDisablePalette(crtc);
2914 }
2915
2916 /* on = unblank */
2917 void NVCrtcBlankScreen(xf86CrtcPtr crtc, Bool on)
2918 {
2919         unsigned char scrn;
2920
2921         NVCrtcSetOwner(crtc);
2922
2923         scrn = NVReadVgaSeq(crtc, 0x01);
2924         if (on) {
2925                 scrn &= ~0x20;
2926         } else {
2927                 scrn |= 0x20;
2928         }
2929
2930         NVVgaSeqReset(crtc, TRUE);
2931         NVWriteVgaSeq(crtc, 0x01, scrn);
2932         NVVgaSeqReset(crtc, FALSE);
2933 }
2934
2935 /*************************************************************************** \
2936 |*                                                                           *|
2937 |*       Copyright 1993-2003 NVIDIA, Corporation.  All rights reserved.      *|
2938 |*                                                                           *|
2939 |*     NOTICE TO USER:   The source code  is copyrighted under  U.S. and     *|
2940 |*     international laws.  Users and possessors of this source code are     *|
2941 |*     hereby granted a nonexclusive,  royalty-free copyright license to     *|
2942 |*     use this code in individual and commercial software.                  *|
2943 |*                                                                           *|
2944 |*     Any use of this source code must include,  in the user documenta-     *|
2945 |*     tion and  internal comments to the code,  notices to the end user     *|
2946 |*     as follows:                                                           *|
2947 |*                                                                           *|
2948 |*       Copyright 1993-1999 NVIDIA, Corporation.  All rights reserved.      *|
2949 |*                                                                           *|
2950 |*     NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY     *|
2951 |*     OF  THIS SOURCE  CODE  FOR ANY PURPOSE.  IT IS  PROVIDED  "AS IS"     *|
2952 |*     WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  NVIDIA, CORPOR-     *|
2953 |*     ATION DISCLAIMS ALL WARRANTIES  WITH REGARD  TO THIS SOURCE CODE,     *|
2954 |*     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE-     *|
2955 |*     MENT,  AND FITNESS  FOR A PARTICULAR PURPOSE.   IN NO EVENT SHALL     *|
2956 |*     NVIDIA, CORPORATION  BE LIABLE FOR ANY SPECIAL,  INDIRECT,  INCI-     *|
2957 |*     DENTAL, OR CONSEQUENTIAL DAMAGES,  OR ANY DAMAGES  WHATSOEVER RE-     *|
2958 |*     SULTING FROM LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION     *|
2959 |*     OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF     *|
2960 |*     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.     *|
2961 |*                                                                           *|
2962 |*     U.S. Government  End  Users.   This source code  is a "commercial     *|
2963 |*     item,"  as that  term is  defined at  48 C.F.R. 2.101 (OCT 1995),     *|
2964 |*     consisting  of "commercial  computer  software"  and  "commercial     *|
2965 |*     computer  software  documentation,"  as such  terms  are  used in     *|
2966 |*     48 C.F.R. 12.212 (SEPT 1995)  and is provided to the U.S. Govern-     *|
2967 |*     ment only as  a commercial end item.   Consistent with  48 C.F.R.     *|
2968 |*     12.212 and  48 C.F.R. 227.7202-1 through  227.7202-4 (JUNE 1995),     *|
2969 |*     all U.S. Government End Users  acquire the source code  with only     *|
2970 |*     those rights set forth herein.                                        *|
2971 |*                                                                           *|
2972  \***************************************************************************/