Use symbolic define values where known
[nouveau] / src / nv_crtc.c
1 /*
2  * Copyright 1993-2003 NVIDIA, Corporation
3  * Copyright 2006 Dave Airlie
4  * Copyright 2007 Maarten Maathuis
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #include "nv_include.h"
27
28 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
29 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
30 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
31 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
32 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
33 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
34 static void nv_crtc_load_state_palette(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
35 static void nv_crtc_save_state_palette(xf86CrtcPtr crtc, RIVA_HW_STATE *state);
36
37 static uint32_t NVCrtcReadCRTC(xf86CrtcPtr crtc, uint32_t reg)
38 {
39         ScrnInfoPtr pScrn = crtc->scrn;
40         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
41         NVPtr pNv = NVPTR(pScrn);
42
43         return NVReadCRTC(pNv, nv_crtc->head, reg);
44 }
45
46 static void NVCrtcWriteCRTC(xf86CrtcPtr crtc, uint32_t reg, uint32_t val)
47 {
48         ScrnInfoPtr pScrn = crtc->scrn;
49         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
50         NVPtr pNv = NVPTR(pScrn);
51
52         NVWriteCRTC(pNv, nv_crtc->head, reg, val);
53 }
54
55 static uint32_t NVCrtcReadRAMDAC(xf86CrtcPtr crtc, uint32_t reg)
56 {
57         ScrnInfoPtr pScrn = crtc->scrn;
58         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
59         NVPtr pNv = NVPTR(pScrn);
60
61         return NVReadRAMDAC(pNv, nv_crtc->head, reg);
62 }
63
64 static void NVCrtcWriteRAMDAC(xf86CrtcPtr crtc, uint32_t reg, uint32_t val)
65 {
66         ScrnInfoPtr pScrn = crtc->scrn;
67         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
68         NVPtr pNv = NVPTR(pScrn);
69
70         NVWriteRAMDAC(pNv, nv_crtc->head, reg, val);
71 }
72
73 void NVCrtcLockUnlock(xf86CrtcPtr crtc, bool lock)
74 {
75         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
76         NVPtr pNv = NVPTR(crtc->scrn);
77
78         if (pNv->twoHeads)
79                 NVSetOwner(pNv, nv_crtc->head);
80         NVLockVgaCrtc(pNv, nv_crtc->head, lock);
81 }
82
83 /* Even though they are not yet used, i'm adding some notes about some of the 0x4000 regs */
84 /* They are only valid for NV4x, appearantly reordered for NV5x */
85 /* gpu pll: 0x4000 + 0x4004
86  * unknown pll: 0x4008 + 0x400c
87  * vpll1: 0x4010 + 0x4014
88  * vpll2: 0x4018 + 0x401c
89  * unknown pll: 0x4020 + 0x4024
90  * unknown pll: 0x4038 + 0x403c
91  * Some of the unknown's are probably memory pll's.
92  * The vpll's use two set's of multipliers and dividers. I refer to them as a and b.
93  * 1 and 2 refer to the registers of each pair. There is only one post divider.
94  * Logic: clock = reference_clock * ((n(a) * n(b))/(m(a) * m(b))) >> p
95  * 1) bit 0-7: familiar values, but redirected from were? (similar to PLL_SETUP_CONTROL)
96  *     bit8: A switch that turns of the second divider and multiplier off.
97  *     bit12: Also a switch, i haven't seen it yet.
98  *     bit16-19: p-divider
99  *     but 28-31: Something related to the mode that is used (see bit8).
100  * 2) bit0-7: m-divider (a)
101  *     bit8-15: n-multiplier (a)
102  *     bit16-23: m-divider (b)
103  *     bit24-31: n-multiplier (b)
104  */
105
106 /* Modifying the gpu pll for example requires:
107  * - Disable value 0x333 (inverse AND mask) on the 0xc040 register.
108  * This is not needed for the vpll's which have their own bits.
109  */
110
111 static void nv_crtc_save_state_pll(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
112 {
113         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
114         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
115         NVPtr pNv = NVPTR(crtc->scrn);
116
117         if (nv_crtc->head) {
118                 regp->vpll_a = NVReadRAMDAC(pNv, 0, NV_RAMDAC_VPLL2);
119                 if (pNv->twoStagePLL)
120                         regp->vpll_b = NVReadRAMDAC(pNv, 0, NV_RAMDAC_VPLL2_B);
121         } else {
122                 regp->vpll_a = NVReadRAMDAC(pNv, 0, NV_RAMDAC_VPLL);
123                 if (pNv->twoStagePLL)
124                         regp->vpll_b = NVReadRAMDAC(pNv, 0, NV_RAMDAC_VPLL_B);
125         }
126         if (pNv->twoHeads)
127                 state->sel_clk = NVReadRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK);
128         state->pllsel = NVReadRAMDAC(pNv, 0, NV_RAMDAC_PLL_SELECT);
129         if (pNv->Architecture == NV_ARCH_40)
130                 state->reg580 = NVReadRAMDAC(pNv, 0, NV_RAMDAC_580);
131 }
132
133 static void nv_crtc_load_state_pll(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
134 {
135         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
136         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
137         ScrnInfoPtr pScrn = crtc->scrn;
138         NVPtr pNv = NVPTR(pScrn);
139         uint32_t savedc040 = 0;
140
141         /* This sequence is important, the NV28 is very sensitive in this area. */
142         /* Keep pllsel last and sel_clk first. */
143         if (pNv->twoHeads)
144                 NVWriteRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK, state->sel_clk);
145
146         if (pNv->Architecture == NV_ARCH_40) {
147                 savedc040 = nvReadMC(pNv, 0xc040);
148
149                 /* for vpll1 change bits 16 and 17 are disabled */
150                 /* for vpll2 change bits 18 and 19 are disabled */
151                 nvWriteMC(pNv, 0xc040, savedc040 & ~(3 << (16 + nv_crtc->head * 2)));
152         }
153
154         if (nv_crtc->head) {
155                 NVWriteRAMDAC(pNv, 0, NV_RAMDAC_VPLL2, regp->vpll_a);
156                 if (pNv->twoStagePLL)
157                         NVWriteRAMDAC(pNv, 0, NV_RAMDAC_VPLL2_B, regp->vpll_b);
158         } else {
159                 NVWriteRAMDAC(pNv, 0, NV_RAMDAC_VPLL, regp->vpll_a);
160                 if (pNv->twoStagePLL)
161                         NVWriteRAMDAC(pNv, 0, NV_RAMDAC_VPLL_B, regp->vpll_b);
162         }
163
164         if (pNv->Architecture == NV_ARCH_40) {
165                 NVWriteRAMDAC(pNv, 0, NV_RAMDAC_580, state->reg580);
166
167                 /* We need to wait a while */
168                 usleep(5000);
169                 nvWriteMC(pNv, 0xc040, savedc040);
170         }
171
172         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Writing NV_RAMDAC_PLL_SELECT %08X\n", state->pllsel);
173         NVWriteRAMDAC(pNv, 0, NV_RAMDAC_PLL_SELECT, state->pllsel);
174 }
175
176 static void nv_crtc_cursor_set(xf86CrtcPtr crtc)
177 {
178         NVPtr pNv = NVPTR(crtc->scrn);
179         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
180         uint32_t cursor_start;
181         uint8_t *CRTC = pNv->ModeReg.crtc_reg[nv_crtc->head].CRTC;
182
183         if (pNv->Architecture == NV_ARCH_04)
184                 cursor_start = 0x5E00 << 2;
185         else
186                 cursor_start = nv_crtc->head ? pNv->Cursor2->offset : pNv->Cursor->offset;
187
188         CRTC[NV_CIO_CRE_HCUR_ADDR0_INDEX] = cursor_start >> 17;
189         if (pNv->Architecture != NV_ARCH_04)
190                 CRTC[NV_CIO_CRE_HCUR_ADDR0_INDEX] |= NV_CIO_CRE_HCUR_ASI;
191         CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX] = (cursor_start >> 11) << 2;
192         if (crtc->mode.Flags & V_DBLSCAN)
193                 CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX] |= NV_CIO_CRE_HCUR_ADDR1_CUR_DBL;
194         CRTC[NV_CIO_CRE_HCUR_ADDR2_INDEX] = cursor_start >> 24;
195
196         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR0_INDEX, CRTC[NV_CIO_CRE_HCUR_ADDR0_INDEX]);
197         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR1_INDEX, CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX]);
198         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR2_INDEX, CRTC[NV_CIO_CRE_HCUR_ADDR2_INDEX]);
199         if (pNv->Architecture == NV_ARCH_40)
200                 nv_fix_nv40_hw_cursor(pNv, nv_crtc->head);
201 }
202
203 static void nv_crtc_calc_state_ext(xf86CrtcPtr crtc, DisplayModePtr mode, int dot_clock)
204 {
205         ScrnInfoPtr pScrn = crtc->scrn;
206         NVPtr pNv = NVPTR(pScrn);
207         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
208         RIVA_HW_STATE *state = &pNv->ModeReg;
209         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
210         struct pll_lims pll_lim;
211         int NM1 = 0xbeef, NM2 = 0, log2P = 0, VClk = 0;
212         uint32_t g70_pll_special_bits = 0;
213         bool nv4x_single_stage_pll_mode = false;
214         uint8_t arbitration0;
215         uint16_t arbitration1;
216
217         if (!get_pll_limits(pScrn, nv_crtc->head ? VPLL2 : VPLL1, &pll_lim))
218                 return;
219
220         if (pNv->twoStagePLL || pNv->NVArch == 0x30 || pNv->NVArch == 0x35) {
221                 if (dot_clock < pll_lim.vco1.maxfreq && pNv->NVArch > 0x40) { /* use a single VCO */
222                         nv4x_single_stage_pll_mode = true;
223                         /* Turn the second set of divider and multiplier off */
224                         /* Bogus data, the same nvidia uses */
225                         NM2 = 0x11f;
226                         VClk = getMNP_single(pScrn, &pll_lim, dot_clock, &NM1, &log2P);
227                 } else
228                         VClk = getMNP_double(pScrn, &pll_lim, dot_clock, &NM1, &NM2, &log2P);
229         } else
230                 VClk = getMNP_single(pScrn, &pll_lim, dot_clock, &NM1, &log2P);
231
232         /* Are these all the (relevant) G70 cards? */
233         if (pNv->NVArch == 0x4B || pNv->NVArch == 0x46 || pNv->NVArch == 0x47 || pNv->NVArch == 0x49) {
234                 /* This is a big guess, but should be reasonable until we can narrow it down. */
235                 /* What exactly are the purpose of the upper 2 bits of pll_a and pll_b? */
236                 if (nv4x_single_stage_pll_mode)
237                         g70_pll_special_bits = 0x1;
238                 else
239                         g70_pll_special_bits = 0x3;
240         }
241
242         if (pNv->NVArch == 0x30 || pNv->NVArch == 0x35)
243                 /* See nvregisters.xml for details. */
244                 regp->vpll_a = (NM2 & (0x18 << 8)) << 13 | (NM2 & (0x7 << 8)) << 11 | log2P << 16 | NV30_RAMDAC_ENABLE_VCO2 | (NM2 & 7) << 4 | NM1;
245         else
246                 regp->vpll_a = g70_pll_special_bits << 30 | log2P << 16 | NM1;
247         regp->vpll_b = NV31_RAMDAC_ENABLE_VCO2 | NM2;
248
249         if (nv4x_single_stage_pll_mode) {
250                 if (nv_crtc->head == 0)
251                         state->reg580 |= NV_RAMDAC_580_VPLL1_ACTIVE;
252                 else
253                         state->reg580 |= NV_RAMDAC_580_VPLL2_ACTIVE;
254         } else {
255                 if (nv_crtc->head == 0)
256                         state->reg580 &= ~NV_RAMDAC_580_VPLL1_ACTIVE;
257                 else
258                         state->reg580 &= ~NV_RAMDAC_580_VPLL2_ACTIVE;
259         }
260
261         /* The NV40 seems to have more similarities to NV3x than other NV4x */
262         if (pNv->NVArch < 0x41)
263                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_NVPLL |
264                                  NV_RAMDAC_PLL_SELECT_PLL_SOURCE_MPLL;
265         /* The blob uses this always, so let's do the same */
266         if (pNv->Architecture == NV_ARCH_40)
267                 state->pllsel |= NV_RAMDAC_PLL_SELECT_USE_VPLL2_TRUE;
268
269         if (nv_crtc->head == 1) {
270                 if (!nv4x_single_stage_pll_mode)
271                         state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
272                 else
273                         state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK2_RATIO_DB2;
274                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL2;
275         } else {
276                 if (!nv4x_single_stage_pll_mode)
277                         state->pllsel |= NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
278                 else
279                         state->pllsel &= ~NV_RAMDAC_PLL_SELECT_VCLK_RATIO_DB2;
280                 state->pllsel |= NV_RAMDAC_PLL_SELECT_PLL_SOURCE_VPLL;
281         }
282
283         if ((!pNv->twoStagePLL && pNv->NVArch != 0x30 && pNv->NVArch != 0x35) || nv4x_single_stage_pll_mode)
284                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "vpll: n %d m %d log2p %d\n", NM1 >> 8, NM1 & 0xff, log2P);
285         else
286                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n", NM1 >> 8, NM2 >> 8, NM1 & 0xff, NM2 & 0xff, log2P);
287
288         if (pNv->Architecture < NV_ARCH_30)
289                 nv4_10UpdateArbitrationSettings(pScrn, VClk, pScrn->bitsPerPixel, &arbitration0, &arbitration1);
290         else if ((pNv->Chipset & 0xfff0) == CHIPSET_C51 ||
291                  (pNv->Chipset & 0xfff0) == CHIPSET_C512) {
292                 arbitration0 = 128;
293                 arbitration1 = 0x0480;
294         } else
295                 nv30UpdateArbitrationSettings(&arbitration0, &arbitration1);
296
297         regp->CRTC[NV_CIO_CRE_FF_INDEX] = arbitration0;
298         regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arbitration1 & 0xff;
299         if (pNv->Architecture >= NV_ARCH_30)
300                 regp->CRTC[NV_CIO_CRE_47] = arbitration1 >> 8;
301
302         nv_crtc_cursor_set(crtc);
303 }
304
305 static void
306 nv_crtc_dpms(xf86CrtcPtr crtc, int mode)
307 {
308         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
309         ScrnInfoPtr pScrn = crtc->scrn;
310         NVPtr pNv = NVPTR(pScrn);
311         unsigned char seq1 = 0, crtc17 = 0;
312         unsigned char crtc1A;
313
314         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Setting dpms mode %d on CRTC %d\n", mode, nv_crtc->head);
315
316         if (nv_crtc->last_dpms == mode) /* Don't do unnecesary mode changes. */
317                 return;
318
319         nv_crtc->last_dpms = mode;
320
321         if (pNv->twoHeads)
322                 NVSetOwner(pNv, nv_crtc->head);
323
324         crtc1A = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC1_INDEX) & ~0xC0;
325         switch(mode) {
326                 case DPMSModeStandby:
327                 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
328                 seq1 = 0x20;
329                 crtc17 = 0x80;
330                 crtc1A |= 0x80;
331                 break;
332         case DPMSModeSuspend:
333                 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
334                 seq1 = 0x20;
335                 crtc17 = 0x80;
336                 crtc1A |= 0x40;
337                 break;
338         case DPMSModeOff:
339                 /* Screen: Off; HSync: Off, VSync: Off */
340                 seq1 = 0x20;
341                 crtc17 = 0x00;
342                 crtc1A |= 0xC0;
343                 break;
344         case DPMSModeOn:
345         default:
346                 /* Screen: On; HSync: On, VSync: On */
347                 seq1 = 0x00;
348                 crtc17 = 0x80;
349                 break;
350         }
351
352         NVVgaSeqReset(pNv, nv_crtc->head, true);
353         /* Each head has it's own sequencer, so we can turn it off when we want */
354         seq1 |= (NVReadVgaSeq(pNv, nv_crtc->head, NV_VIO_SR_CLOCK_INDEX) & ~0x20);
355         NVWriteVgaSeq(pNv, nv_crtc->head, NV_VIO_SR_CLOCK_INDEX, seq1);
356         crtc17 |= (NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CR_MODE_INDEX) & ~0x80);
357         usleep(10000);
358         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CR_MODE_INDEX, crtc17);
359         NVVgaSeqReset(pNv, nv_crtc->head, false);
360
361         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC1_INDEX, crtc1A);
362 }
363
364 static Bool
365 nv_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode,
366                      DisplayModePtr adjusted_mode)
367 {
368         return TRUE;
369 }
370
371 static void
372 nv_crtc_mode_set_vga(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
373 {
374         ScrnInfoPtr pScrn = crtc->scrn;
375         NVPtr pNv = NVPTR(pScrn);
376         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
377         NVCrtcRegPtr regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
378
379         /* Calculate our timings */
380         int horizDisplay        = (mode->CrtcHDisplay >> 3)     - 1;
381         int horizStart          = (mode->CrtcHSyncStart >> 3)   - 1;
382         int horizEnd            = (mode->CrtcHSyncEnd >> 3)     - 1;
383         int horizTotal          = (mode->CrtcHTotal >> 3)               - 5;
384         int horizBlankStart     = (mode->CrtcHDisplay >> 3)             - 1;
385         int horizBlankEnd       = (mode->CrtcHTotal >> 3)               - 1;
386         int vertDisplay         = mode->CrtcVDisplay                    - 1;
387         int vertStart           = mode->CrtcVSyncStart          - 1;
388         int vertEnd             = mode->CrtcVSyncEnd                    - 1;
389         int vertTotal           = mode->CrtcVTotal                      - 2;
390         int vertBlankStart      = mode->CrtcVDisplay                    - 1;
391         int vertBlankEnd        = mode->CrtcVTotal                      - 1;
392
393         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
394         bool fp_output = false;
395         int i;
396
397         for (i = 0; i < xf86_config->num_output; i++) {
398                 xf86OutputPtr output = xf86_config->output[i];
399                 struct nouveau_encoder *nv_encoder = to_nouveau_encoder(output);
400
401                 if (output->crtc == crtc && (nv_encoder->dcb->type == OUTPUT_LVDS ||
402                                              nv_encoder->dcb->type == OUTPUT_TMDS))
403                         fp_output = true;
404         }
405
406         if (fp_output) {
407                 vertStart = vertTotal - 3;  
408                 vertEnd = vertTotal - 2;
409                 vertBlankStart = vertStart;
410                 horizStart = horizTotal - 5;
411                 horizEnd = horizTotal - 2;
412                 horizBlankEnd = horizTotal + 4;
413                 if (pNv->overlayAdaptor && pNv->Architecture >= NV_ARCH_10)
414                         /* This reportedly works around some video overlay bandwidth problems */
415                         horizTotal += 2;
416         }
417
418         if (mode->Flags & V_INTERLACE) 
419                 vertTotal |= 1;
420
421 #if 0
422         ErrorF("horizDisplay: 0x%X \n", horizDisplay);
423         ErrorF("horizStart: 0x%X \n", horizStart);
424         ErrorF("horizEnd: 0x%X \n", horizEnd);
425         ErrorF("horizTotal: 0x%X \n", horizTotal);
426         ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
427         ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
428         ErrorF("vertDisplay: 0x%X \n", vertDisplay);
429         ErrorF("vertStart: 0x%X \n", vertStart);
430         ErrorF("vertEnd: 0x%X \n", vertEnd);
431         ErrorF("vertTotal: 0x%X \n", vertTotal);
432         ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
433         ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
434 #endif
435
436         /*
437         * compute correct Hsync & Vsync polarity 
438         */
439         if ((mode->Flags & (V_PHSYNC | V_NHSYNC))
440                 && (mode->Flags & (V_PVSYNC | V_NVSYNC))) {
441
442                 regp->MiscOutReg = 0x23;
443                 if (mode->Flags & V_NHSYNC) regp->MiscOutReg |= 0x40;
444                 if (mode->Flags & V_NVSYNC) regp->MiscOutReg |= 0x80;
445         } else {
446                 int VDisplay = mode->VDisplay;
447                 if (mode->Flags & V_DBLSCAN)
448                         VDisplay *= 2;
449                 if (mode->VScan > 1)
450                         VDisplay *= mode->VScan;
451                 if (VDisplay < 400)
452                         regp->MiscOutReg = 0xA3;                /* +hsync -vsync */
453                 else if (VDisplay < 480)
454                         regp->MiscOutReg = 0x63;                /* -hsync +vsync */
455                 else if (VDisplay < 768)
456                         regp->MiscOutReg = 0xE3;                /* -hsync -vsync */
457                 else
458                         regp->MiscOutReg = 0x23;                /* +hsync +vsync */
459         }
460
461         regp->MiscOutReg |= (mode->ClockIndex & 0x03) << 2;
462
463         /*
464         * Time Sequencer
465         */
466         regp->Sequencer[NV_VIO_SR_RESET_INDEX] = 0x00;
467         /* 0x20 disables the sequencer */
468         if (mode->Flags & V_CLKDIV2)
469                 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x29;
470         else
471                 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x21;
472         regp->Sequencer[NV_VIO_SR_PLANE_MASK_INDEX] = 0x0F;
473         regp->Sequencer[NV_VIO_SR_CHAR_MAP_INDEX] = 0x00;
474         regp->Sequencer[NV_VIO_SR_MEM_MODE_INDEX] = 0x0E;
475
476         /*
477         * CRTC Controller
478         */
479         regp->CRTC[NV_CIO_CR_HDT_INDEX]  = Set8Bits(horizTotal);
480         regp->CRTC[NV_CIO_CR_HDE_INDEX]  = Set8Bits(horizDisplay);
481         regp->CRTC[NV_CIO_CR_HBS_INDEX]  = Set8Bits(horizBlankStart);
482         regp->CRTC[NV_CIO_CR_HBE_INDEX]  = SetBitField(horizBlankEnd,4:0,4:0)
483                                 | SetBit(7);
484         regp->CRTC[NV_CIO_CR_HRS_INDEX]  = Set8Bits(horizStart);
485         regp->CRTC[NV_CIO_CR_HRE_INDEX]  = SetBitField(horizBlankEnd,5:5,7:7)
486                                 | SetBitField(horizEnd,4:0,4:0);
487         regp->CRTC[NV_CIO_CR_VDT_INDEX]  = SetBitField(vertTotal,7:0,7:0);
488         regp->CRTC[NV_CIO_CR_OVL_INDEX]  = SetBitField(vertTotal,8:8,0:0)
489                                 | SetBitField(vertDisplay,8:8,1:1)
490                                 | SetBitField(vertStart,8:8,2:2)
491                                 | SetBitField(vertBlankStart,8:8,3:3)
492                                 | SetBit(4)
493                                 | SetBitField(vertTotal,9:9,5:5)
494                                 | SetBitField(vertDisplay,9:9,6:6)
495                                 | SetBitField(vertStart,9:9,7:7);
496         regp->CRTC[NV_CIO_CR_RSAL_INDEX]  = 0x00;
497         regp->CRTC[NV_CIO_CR_CELL_HT_INDEX]  = SetBitField(vertBlankStart,9:9,5:5)
498                                 | SetBit(6)
499                                 | (mode->Flags & V_DBLSCAN) * NV_CIO_CR_CELL_HT_SCANDBL;
500         regp->CRTC[NV_CIO_CR_CURS_ST_INDEX] = 0x00;
501         regp->CRTC[NV_CIO_CR_CURS_END_INDEX] = 0x00;
502         regp->CRTC[NV_CIO_CR_SA_HI_INDEX] = 0x00;
503         regp->CRTC[NV_CIO_CR_SA_LO_INDEX] = 0x00;
504         regp->CRTC[NV_CIO_CR_TCOFF_HI_INDEX] = 0x00;
505         regp->CRTC[NV_CIO_CR_TCOFF_LO_INDEX] = 0x00;
506         regp->CRTC[NV_CIO_CR_VRS_INDEX] = Set8Bits(vertStart);
507         /* What is the meaning of bit5, it is empty in the vga spec. */
508         regp->CRTC[NV_CIO_CR_VRE_INDEX] = SetBitField(vertEnd,3:0,3:0) | SetBit(5);
509         regp->CRTC[NV_CIO_CR_VDE_INDEX] = Set8Bits(vertDisplay);
510         /* framebuffer can be larger than crtc scanout area. */
511         regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = pScrn->displayWidth / 8 * pScrn->bitsPerPixel / 8;
512         regp->CRTC[NV_CIO_CR_ULINE_INDEX] = 0x00;
513         regp->CRTC[NV_CIO_CR_VBS_INDEX] = Set8Bits(vertBlankStart);
514         regp->CRTC[NV_CIO_CR_VBE_INDEX] = Set8Bits(vertBlankEnd);
515         regp->CRTC[NV_CIO_CR_MODE_INDEX] = 0x43;
516         regp->CRTC[NV_CIO_CR_LCOMP_INDEX] = 0xff;
517
518         /* 
519          * Some extended CRTC registers (they are not saved with the rest of the vga regs).
520          */
521
522         /* framebuffer can be larger than crtc scanout area. */
523         regp->CRTC[NV_CIO_CRE_RPC0_INDEX] = ((pScrn->displayWidth / 8 * pScrn->bitsPerPixel / 8) & 0x700) >> 3;
524         regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->CrtcHDisplay < 1280 ? 0x04 : 0x00;
525         regp->CRTC[NV_CIO_CRE_LSR_INDEX] = SetBitField(horizBlankEnd,6:6,4:4)
526                                 | SetBitField(vertBlankStart,10:10,3:3)
527                                 | SetBitField(vertStart,10:10,2:2)
528                                 | SetBitField(vertDisplay,10:10,1:1)
529                                 | SetBitField(vertTotal,10:10,0:0);
530
531         regp->CRTC[NV_CIO_CRE_HEB__INDEX] = SetBitField(horizTotal,8:8,0:0)
532                                 | SetBitField(horizDisplay,8:8,1:1)
533                                 | SetBitField(horizBlankStart,8:8,2:2)
534                                 | SetBitField(horizStart,8:8,3:3);
535
536         regp->CRTC[NV_CIO_CRE_EBR_INDEX] = SetBitField(vertTotal,11:11,0:0)
537                                 | SetBitField(vertDisplay,11:11,2:2)
538                                 | SetBitField(vertStart,11:11,4:4)
539                                 | SetBitField(vertBlankStart,11:11,6:6);
540
541         if(mode->Flags & V_INTERLACE) {
542                 horizTotal = (horizTotal >> 1) & ~1;
543                 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = Set8Bits(horizTotal);
544                 regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= SetBitField(horizTotal,8:8,4:4);
545         } else
546                 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff;  /* interlace off */
547
548         /*
549         * Graphics Display Controller
550         */
551         regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
552         regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
553         regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
554         regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
555         regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
556         regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
557         regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
558         regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
559         regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
560
561         regp->Attribute[0]  = 0x00; /* standard colormap translation */
562         regp->Attribute[1]  = 0x01;
563         regp->Attribute[2]  = 0x02;
564         regp->Attribute[3]  = 0x03;
565         regp->Attribute[4]  = 0x04;
566         regp->Attribute[5]  = 0x05;
567         regp->Attribute[6]  = 0x06;
568         regp->Attribute[7]  = 0x07;
569         regp->Attribute[8]  = 0x08;
570         regp->Attribute[9]  = 0x09;
571         regp->Attribute[10] = 0x0A;
572         regp->Attribute[11] = 0x0B;
573         regp->Attribute[12] = 0x0C;
574         regp->Attribute[13] = 0x0D;
575         regp->Attribute[14] = 0x0E;
576         regp->Attribute[15] = 0x0F;
577         regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
578         /* Non-vga */
579         regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
580         regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
581         regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
582         regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
583 }
584
585 /**
586  * Sets up registers for the given mode/adjusted_mode pair.
587  *
588  * The clocks, CRTCs and outputs attached to this CRTC must be off.
589  *
590  * This shouldn't enable any clocks, CRTCs, or outputs, but they should
591  * be easily turned on/off after this.
592  */
593 static void
594 nv_crtc_mode_set_regs(xf86CrtcPtr crtc, DisplayModePtr mode)
595 {
596         ScrnInfoPtr pScrn = crtc->scrn;
597         NVPtr pNv = NVPTR(pScrn);
598         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
599         NVCrtcRegPtr regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
600         NVCrtcRegPtr savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
601         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
602         bool lvds_output = false, tmds_output = false;
603         int i;
604
605         for (i = 0; i < xf86_config->num_output; i++) {
606                 xf86OutputPtr output = xf86_config->output[i];
607                 struct nouveau_encoder *nv_encoder = to_nouveau_encoder(output);
608
609                 if (output->crtc == crtc && nv_encoder->dcb->type == OUTPUT_LVDS)
610                         lvds_output = true;
611                 if (output->crtc == crtc && nv_encoder->dcb->type == OUTPUT_TMDS)
612                         tmds_output = true;
613         }
614
615         /* Registers not directly related to the (s)vga mode */
616
617         /* bit2 = 0 -> fine pitched crtc granularity */
618         /* The rest disables double buffering on CRTC access */
619         regp->CRTC[NV_CIO_CRE_21] = 0xfa;
620
621         /* the blob sometimes sets |= 0x10 (which is the same as setting |=
622          * 1 << 30 on 0x60.830), for no apparent reason */
623         regp->CRTC[NV_CIO_CRE_59] = 0x0;
624         if (tmds_output && pNv->Architecture < NV_ARCH_40)
625                 regp->CRTC[NV_CIO_CRE_59] |= 0x1;
626
627         /* What is the meaning of this register? */
628         /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */ 
629         regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
630
631         regp->head = 0;
632         /* Except for rare conditions I2C is enabled on the primary crtc */
633         if (nv_crtc->head == 0)
634                 regp->head |= NV_CRTC_FSEL_I2C;
635         /* Set overlay to desired crtc. */
636         if (pNv->overlayAdaptor) {
637                 NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(pNv);
638                 if (pPriv->overlayCRTC == nv_crtc->head)
639                         regp->head |= NV_CRTC_FSEL_OVERLAY;
640         }
641
642         /* This is not what nv does, but it is what the blob does (for nv4x at least) */
643         /* This fixes my cursor corruption issue */
644         regp->cursorConfig = 0x0;
645         if(mode->Flags & V_DBLSCAN)
646                 regp->cursorConfig |= NV_CRTC_CURSOR_CONFIG_DOUBLE_SCAN;
647         if (pNv->alphaCursor) {
648                 regp->cursorConfig |= NV_CRTC_CURSOR_CONFIG_32BPP |
649                                       NV_CRTC_CURSOR_CONFIG_64PIXELS |
650                                       NV_CRTC_CURSOR_CONFIG_64LINES |
651                                       NV_CRTC_CURSOR_CONFIG_ALPHA_BLEND;
652         } else
653                 regp->cursorConfig |= NV_CRTC_CURSOR_CONFIG_32LINES;
654
655         /* Unblock some timings */
656         regp->CRTC[NV_CIO_CRE_53] = 0;
657         regp->CRTC[NV_CIO_CRE_54] = 0;
658
659         /* What is the purpose of this register? */
660         /* 0x14 may be disabled? */
661         regp->CRTC[NV_CIO_CR_ARX_INDEX] = 0x20;
662
663         /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
664         if (lvds_output)
665                 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
666         else if (tmds_output)
667                 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
668         else
669                 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
670
671         /* These values seem to vary */
672         /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
673         regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
674
675         regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
676
677         /* What does this do?:
678          * bit0: crtc0
679          * bit6: lvds
680          * bit7: (only in X)
681          */
682         if (nv_crtc->head == 0)
683                 regp->CRTC[NV_CIO_CRE_4B] = 0x81;
684         else 
685                 regp->CRTC[NV_CIO_CRE_4B] = 0x80;
686
687         if (lvds_output)
688                 regp->CRTC[NV_CIO_CRE_4B] |= 0x40;
689
690         /* The blob seems to take the current value from crtc 0, add 4 to that
691          * and reuse the old value for crtc 1 */
692         regp->CRTC[NV_CIO_CRE_52] = pNv->SavedReg.crtc_reg[0].CRTC[NV_CIO_CRE_52];
693         if (!nv_crtc->head)
694                 regp->CRTC[NV_CIO_CRE_52] += 4;
695
696         regp->unk830 = mode->CrtcVDisplay - 3;
697         regp->unk834 = mode->CrtcVDisplay - 1;
698
699         if (pNv->twoHeads)
700                 /* This is what the blob does */
701                 regp->unk850 = NVReadCRTC(pNv, 0, NV_CRTC_0850);
702
703         /* Never ever modify gpio, unless you know very well what you're doing */
704         regp->gpio = NVReadCRTC(pNv, 0, NV_CRTC_GPIO);
705
706         if (pNv->twoHeads)
707                 regp->gpio_ext = NVReadCRTC(pNv, 0, NV_CRTC_GPIO_EXT);
708
709         regp->config = NV_PCRTC_CONFIG_START_ADDRESS_HSYNC;
710
711         /* Some misc regs */
712         if (pNv->Architecture == NV_ARCH_40) {
713                 regp->CRTC[NV_CIO_CRE_85] = 0xFF;
714                 regp->CRTC[NV_CIO_CRE_86] = 0x1;
715         }
716
717         regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (pScrn->depth + 1) / 8;
718         /* Enable slaved mode */
719         if (lvds_output || tmds_output)
720                 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
721
722         /* Generic PRAMDAC regs */
723
724         if (pNv->Architecture >= NV_ARCH_10)
725                 /* Only bit that bios and blob set. */
726                 regp->nv10_cursync = (1 << 25);
727
728         switch (pScrn->depth) {
729                 case 24:
730                 case 15:
731                         regp->general = 0x00100130;
732                         break;
733                 case 16:
734                 default:
735                         regp->general = 0x00101130;
736                         break;
737         }
738         if (pNv->alphaCursor)
739                 /* PIPE_LONG mode, something to do with the size of the cursor? */
740                 regp->general |= 1 << 29;
741
742         regp->unk_630 = 0; /* turn off green mode (tv test pattern?) */
743
744         /* Some values the blob sets */
745         regp->unk_a20 = 0x0;
746         regp->unk_a24 = 0xfffff;
747         regp->unk_a34 = 0x1;
748 }
749
750 /* this could be set in nv_output, but would require some rework of load/save */
751 static void
752 nv_crtc_mode_set_fp_regs(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
753 {
754         ScrnInfoPtr pScrn = crtc->scrn;
755         NVPtr pNv = NVPTR(pScrn);
756         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
757         NVCrtcRegPtr regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
758         NVCrtcRegPtr savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
759         struct nouveau_encoder *nv_encoder = NULL;
760         xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
761         bool is_fp = false;
762         bool is_lvds = false;
763         uint32_t mode_ratio, panel_ratio;
764         int i;
765
766         for (i = 0; i < xf86_config->num_output; i++) {
767                 xf86OutputPtr output = xf86_config->output[i];
768                 /* assuming one fp output per crtc seems ok */
769                 nv_encoder = to_nouveau_encoder(output);
770
771                 if (output->crtc == crtc && nv_encoder->dcb->type == OUTPUT_LVDS)
772                         is_lvds = true;
773                 if (is_lvds || (output->crtc == crtc && nv_encoder->dcb->type == OUTPUT_TMDS)) {
774                         is_fp = true;
775                         break;
776                 }
777         }
778         if (!is_fp)
779                 return;
780
781         regp->fp_horiz_regs[REG_DISP_END] = adjusted_mode->HDisplay - 1;
782         regp->fp_horiz_regs[REG_DISP_TOTAL] = adjusted_mode->HTotal - 1;
783         if ((adjusted_mode->HSyncStart - adjusted_mode->HDisplay) >= pNv->VBIOS.digital_min_front_porch)
784                 regp->fp_horiz_regs[REG_DISP_CRTC] = adjusted_mode->HDisplay;
785         else
786                 regp->fp_horiz_regs[REG_DISP_CRTC] = adjusted_mode->HSyncStart - pNv->VBIOS.digital_min_front_porch - 1;
787         regp->fp_horiz_regs[REG_DISP_SYNC_START] = adjusted_mode->HSyncStart - 1;
788         regp->fp_horiz_regs[REG_DISP_SYNC_END] = adjusted_mode->HSyncEnd - 1;
789         regp->fp_horiz_regs[REG_DISP_VALID_START] = adjusted_mode->HSkew;
790         regp->fp_horiz_regs[REG_DISP_VALID_END] = adjusted_mode->HDisplay - 1;
791
792         regp->fp_vert_regs[REG_DISP_END] = adjusted_mode->VDisplay - 1;
793         regp->fp_vert_regs[REG_DISP_TOTAL] = adjusted_mode->VTotal - 1;
794         regp->fp_vert_regs[REG_DISP_CRTC] = adjusted_mode->VTotal - 5 - 1;
795         regp->fp_vert_regs[REG_DISP_SYNC_START] = adjusted_mode->VSyncStart - 1;
796         regp->fp_vert_regs[REG_DISP_SYNC_END] = adjusted_mode->VSyncEnd - 1;
797         regp->fp_vert_regs[REG_DISP_VALID_START] = 0;
798         regp->fp_vert_regs[REG_DISP_VALID_END] = adjusted_mode->VDisplay - 1;
799
800         /*
801         * bit0: positive vsync
802         * bit4: positive hsync
803         * bit8: enable center mode
804         * bit9: enable native mode
805         * bit24: 12/24 bit interface (12bit=on, 24bit=off)
806         * bit26: a bit sometimes seen on some g70 cards
807         * bit28: fp display enable bit
808         * bit31: set for dual link LVDS
809         */
810
811         regp->fp_control = (savep->fp_control & 0x04100000) |
812                            NV_RAMDAC_FP_CONTROL_DISPEN_POS;
813
814         /* Deal with vsync/hsync polarity */
815         /* LVDS screens do set this, but modes with +ve syncs are very rare */
816         if (adjusted_mode->Flags & V_PVSYNC)
817                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_VSYNC_POS;
818         if (adjusted_mode->Flags & V_PHSYNC)
819                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_HSYNC_POS;
820
821         if (nv_encoder->scaling_mode == SCALE_PANEL ||
822             nv_encoder->scaling_mode == SCALE_NOSCALE) /* panel needs to scale */
823                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_CENTER;
824         /* This is also true for panel scaling, so we must put the panel scale check first */
825         else if (mode->HDisplay == adjusted_mode->HDisplay &&
826                  mode->VDisplay == adjusted_mode->VDisplay) /* native mode */
827                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_NATIVE;
828         else /* gpu needs to scale */
829                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_MODE_SCALE;
830
831         if (nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT_0) & NV_PEXTDEV_BOOT_0_STRAP_FP_IFACE_12BIT)
832                 regp->fp_control |= NV_RAMDAC_FP_CONTROL_WIDTH_12;
833
834         if (is_lvds && pNv->VBIOS.fp.dual_link)
835                 regp->fp_control |= (8 << 28);
836
837         /* Use the generic value, and enable x-scaling, y-scaling, and the TMDS enable bit */
838         regp->debug_0 = 0x01101191;
839         /* We want automatic scaling */
840         regp->debug_1 = 0;
841         /* This can override HTOTAL and VTOTAL */
842         regp->debug_2 = 0;
843
844         /* Use 20.12 fixed point format to avoid floats */
845         mode_ratio = (1 << 12) * mode->HDisplay / mode->VDisplay;
846         panel_ratio = (1 << 12) * adjusted_mode->HDisplay / adjusted_mode->VDisplay;
847         /* if ratios are equal, SCALE_ASPECT will automatically (and correctly)
848          * get treated the same as SCALE_FULLSCREEN */
849         if (nv_encoder->scaling_mode == SCALE_ASPECT && mode_ratio != panel_ratio) {
850                 uint32_t diff, scale;
851
852                 if (mode_ratio < panel_ratio) {
853                         /* vertical needs to expand to glass size (automatic)
854                          * horizontal needs to be scaled at vertical scale factor
855                          * to maintain aspect */
856         
857                         scale = (1 << 12) * mode->VDisplay / adjusted_mode->VDisplay;
858                         regp->debug_1 = 1 << 12 | ((scale >> 1) & 0xfff);
859
860                         /* restrict area of screen used, horizontally */
861                         diff = adjusted_mode->HDisplay -
862                                adjusted_mode->VDisplay * mode_ratio / (1 << 12);
863                         regp->fp_horiz_regs[REG_DISP_VALID_START] += diff / 2;
864                         regp->fp_horiz_regs[REG_DISP_VALID_END] -= diff / 2;
865                 }
866
867                 if (mode_ratio > panel_ratio) {
868                         /* horizontal needs to expand to glass size (automatic)
869                          * vertical needs to be scaled at horizontal scale factor
870                          * to maintain aspect */
871
872                         scale = (1 << 12) * mode->HDisplay / adjusted_mode->HDisplay;
873                         regp->debug_1 = 1 << 28 | ((scale >> 1) & 0xfff) << 16;
874                         
875                         /* restrict area of screen used, vertically */
876                         diff = adjusted_mode->VDisplay -
877                                (1 << 12) * adjusted_mode->HDisplay / mode_ratio;
878                         regp->fp_vert_regs[REG_DISP_VALID_START] += diff / 2;
879                         regp->fp_vert_regs[REG_DISP_VALID_END] -= diff / 2;
880                 }
881         }
882
883         /* Flatpanel support needs at least a NV10 */
884         if (pNv->twoHeads) {
885                 /* Output property. */
886                 if (nv_encoder && nv_encoder->dithering) {
887                         if (pNv->NVArch == 0x11)
888                                 regp->dither = savep->dither | 0x00010000;
889                         else {
890                                 int i;
891                                 regp->dither = savep->dither | 0x00000001;
892                                 for (i = 0; i < 3; i++) {
893                                         regp->dither_regs[i] = 0xe4e4e4e4;
894                                         regp->dither_regs[i + 3] = 0x44444444;
895                                 }
896                         }
897                 } else {
898                         if (pNv->NVArch != 0x11) {
899                                 /* reset them */
900                                 int i;
901                                 for (i = 0; i < 3; i++) {
902                                         regp->dither_regs[i] = savep->dither_regs[i];
903                                         regp->dither_regs[i + 3] = savep->dither_regs[i + 3];
904                                 }
905                         }
906                         regp->dither = savep->dither;
907                 }
908         } else
909                 regp->dither = savep->dither;
910 }
911
912 /**
913  * Sets up registers for the given mode/adjusted_mode pair.
914  *
915  * The clocks, CRTCs and outputs attached to this CRTC must be off.
916  *
917  * This shouldn't enable any clocks, CRTCs, or outputs, but they should
918  * be easily turned on/off after this.
919  */
920 static void
921 nv_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode,
922                  DisplayModePtr adjusted_mode,
923                  int x, int y)
924 {
925         ScrnInfoPtr pScrn = crtc->scrn;
926         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
927         NVPtr pNv = NVPTR(pScrn);
928
929         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "CTRC mode on CRTC %d:\n", nv_crtc->head);
930         xf86PrintModeline(pScrn->scrnIndex, mode);
931         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Output mode on CRTC %d:\n", nv_crtc->head);
932         xf86PrintModeline(pScrn->scrnIndex, adjusted_mode);
933
934         if (pNv->twoHeads)
935                 NVSetOwner(pNv, nv_crtc->head);
936
937         nv_crtc_mode_set_vga(crtc, mode, adjusted_mode);
938
939         /* calculated in output_prepare, nv40 needs it written before calculating PLLs */
940         if (pNv->Architecture == NV_ARCH_40)
941                 NVWriteRAMDAC(pNv, 0, NV_RAMDAC_SEL_CLK, pNv->ModeReg.sel_clk);
942         nv_crtc_mode_set_regs(crtc, mode);
943         nv_crtc_mode_set_fp_regs(crtc, mode, adjusted_mode);
944         nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->Clock);
945
946         NVVgaProtect(pNv, nv_crtc->head, true);
947         nv_crtc_load_state_ramdac(crtc, &pNv->ModeReg);
948         nv_crtc_load_state_ext(crtc, &pNv->ModeReg);
949         nv_crtc_load_state_palette(crtc, &pNv->ModeReg);
950         nv_crtc_load_state_vga(crtc, &pNv->ModeReg);
951         nv_crtc_load_state_pll(crtc, &pNv->ModeReg);
952
953         NVVgaProtect(pNv, nv_crtc->head, false);
954
955         NVCrtcSetBase(crtc, x, y);
956
957 #if X_BYTE_ORDER == X_BIG_ENDIAN
958         /* turn on LFB swapping */
959         {
960                 uint8_t tmp = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RCR);
961                 tmp |= NV_CIO_CRE_RCR_ENDIAN_BIG;
962                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RCR, tmp);
963         }
964 #endif
965 }
966
967 static void nv_crtc_save(xf86CrtcPtr crtc)
968 {
969         ScrnInfoPtr pScrn = crtc->scrn;
970         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
971         NVPtr pNv = NVPTR(pScrn);
972
973         /* We just came back from terminal, so unlock */
974         NVCrtcLockUnlock(crtc, false);
975
976         nv_crtc_save_state_ramdac(crtc, &pNv->SavedReg);
977         nv_crtc_save_state_vga(crtc, &pNv->SavedReg);
978         nv_crtc_save_state_palette(crtc, &pNv->SavedReg);
979         nv_crtc_save_state_ext(crtc, &pNv->SavedReg);
980         nv_crtc_save_state_pll(crtc, &pNv->SavedReg);
981
982         /* init some state to saved value */
983         pNv->ModeReg.reg580 = pNv->SavedReg.reg580;
984         pNv->ModeReg.sel_clk = pNv->SavedReg.sel_clk & ~(0x5 << 16);
985         pNv->ModeReg.crtc_reg[nv_crtc->head].CRTC[NV_CIO_CRE_LCD__INDEX] = pNv->SavedReg.crtc_reg[nv_crtc->head].CRTC[NV_CIO_CRE_LCD__INDEX];
986 }
987
988 static void nv_crtc_restore(xf86CrtcPtr crtc)
989 {
990         ScrnInfoPtr pScrn = crtc->scrn;
991         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
992         NVPtr pNv = NVPTR(pScrn);
993         RIVA_HW_STATE *state;
994         NVCrtcRegPtr savep;
995
996         state = &pNv->SavedReg;
997         savep = &pNv->SavedReg.crtc_reg[nv_crtc->head];
998
999         /* Just to be safe */
1000         NVCrtcLockUnlock(crtc, false);
1001
1002         NVVgaProtect(pNv, nv_crtc->head, true);
1003         nv_crtc_load_state_ramdac(crtc, &pNv->SavedReg);
1004         nv_crtc_load_state_ext(crtc, &pNv->SavedReg);
1005         nv_crtc_load_state_palette(crtc, &pNv->SavedReg);
1006         nv_crtc_load_state_vga(crtc, &pNv->SavedReg);
1007         nv_crtc_load_state_pll(crtc, &pNv->SavedReg);
1008         NVVgaProtect(pNv, nv_crtc->head, false);
1009
1010         nv_crtc->last_dpms = NV_DPMS_CLEARED;
1011 }
1012
1013 static void nv_crtc_prepare(xf86CrtcPtr crtc)
1014 {
1015         ScrnInfoPtr pScrn = crtc->scrn;
1016         NVPtr pNv = NVPTR(pScrn);
1017         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1018
1019         /* Just in case */
1020         NVCrtcLockUnlock(crtc, 0);
1021
1022         crtc->funcs->dpms(crtc, DPMSModeOff);
1023
1024         /* Sync the engine before adjust mode */
1025         if (pNv->EXADriverPtr) {
1026                 exaMarkSync(pScrn->pScreen);
1027                 exaWaitSync(pScrn->pScreen);
1028         }
1029
1030         NVBlankScreen(pNv, nv_crtc->head, true);
1031
1032         /* Some more preperation. */
1033         NVCrtcWriteCRTC(crtc, NV_CRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
1034         if (pNv->Architecture == NV_ARCH_40) {
1035                 uint32_t reg900 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_900);
1036                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_900, reg900 & ~0x10000);
1037         }
1038 }
1039
1040 static void nv_crtc_commit(xf86CrtcPtr crtc)
1041 {
1042         crtc->funcs->dpms (crtc, DPMSModeOn);
1043
1044         if (crtc->scrn->pScreen != NULL) {
1045                 NVPtr pNv = NVPTR(crtc->scrn);
1046
1047                 xf86_reload_cursors (crtc->scrn->pScreen);
1048                 if (!pNv->alphaCursor) {
1049                         /* this works round the fact that xf86_reload_cursors
1050                          * will quite happily show the hw cursor when it knows
1051                          * the hardware can't do alpha, and the current cursor
1052                          * has an alpha channel
1053                          */
1054                         xf86ForceHWCursor(crtc->scrn->pScreen, 1);
1055                         xf86ForceHWCursor(crtc->scrn->pScreen, 0);
1056                 }
1057         }
1058 }
1059
1060 static void nv_crtc_destroy(xf86CrtcPtr crtc)
1061 {
1062         xfree(to_nouveau_crtc(crtc));
1063 }
1064
1065 static Bool nv_crtc_lock(xf86CrtcPtr crtc)
1066 {
1067         return FALSE;
1068 }
1069
1070 static void nv_crtc_unlock(xf86CrtcPtr crtc)
1071 {
1072 }
1073
1074 static void
1075 nv_crtc_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
1076                                         int size)
1077 {
1078         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1079         ScrnInfoPtr pScrn = crtc->scrn;
1080         NVPtr pNv = NVPTR(pScrn);
1081         NVCrtcRegPtr regp = &pNv->ModeReg.crtc_reg[nv_crtc->head];
1082         int i, j;
1083
1084         switch (pScrn->depth) {
1085         case 15:
1086                 /* R5G5B5 */
1087                 /* We've got 5 bit (32 values) colors and 256 registers for each color */
1088                 for (i = 0; i < 32; i++)
1089                         for (j = 0; j < 8; j++) {
1090                                 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1091                                 regp->DAC[(i*8 + j) * 3 + 1] = green[i] >> 8;
1092                                 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1093                         }
1094                 break;
1095         case 16:
1096                 /* R5G6B5 */
1097                 /* First deal with the 5 bit colors */
1098                 for (i = 0; i < 32; i++)
1099                         for (j = 0; j < 8; j++) {
1100                                 regp->DAC[(i*8 + j) * 3 + 0] = red[i] >> 8;
1101                                 regp->DAC[(i*8 + j) * 3 + 2] = blue[i] >> 8;
1102                         }
1103                 /* Now deal with the 6 bit color */
1104                 for (i = 0; i < 64; i++)
1105                         for (j = 0; j < 4; j++)
1106                                 regp->DAC[(i*4 + j) * 3 + 1] = green[i] >> 8;
1107                 break;
1108         default:
1109                 /* R8G8B8 */
1110                 for (i = 0; i < 256; i++) {
1111                         regp->DAC[i * 3] = red[i] >> 8;
1112                         regp->DAC[(i * 3) + 1] = green[i] >> 8;
1113                         regp->DAC[(i * 3) + 2] = blue[i] >> 8;
1114                 }
1115                 break;
1116         }
1117
1118         nv_crtc_load_state_palette(crtc, &pNv->ModeReg);
1119 }
1120
1121 /**
1122  * Allocates memory for a locked-in-framebuffer shadow of the given
1123  * width and height for this CRTC's rotated shadow framebuffer.
1124  */
1125  
1126 static void *
1127 nv_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
1128 {
1129         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1130         ScrnInfoPtr pScrn = crtc->scrn;
1131 #if !NOUVEAU_EXA_PIXMAPS
1132         ScreenPtr pScreen = pScrn->pScreen;
1133 #endif /* !NOUVEAU_EXA_PIXMAPS */
1134         NVPtr pNv = NVPTR(pScrn);
1135         void *offset;
1136
1137         unsigned long rotate_pitch;
1138         int size, align = 64;
1139
1140         rotate_pitch = pScrn->displayWidth * (pScrn->bitsPerPixel/8);
1141         size = rotate_pitch * height;
1142
1143         assert(nv_crtc->shadow == NULL);
1144 #if NOUVEAU_EXA_PIXMAPS
1145         if (nouveau_bo_new(pNv->dev, NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN,
1146                         align, size, &nv_crtc->shadow)) {
1147                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Failed to allocate memory for shadow buffer!\n");
1148                 return NULL;
1149         }
1150
1151         if (nv_crtc->shadow && nouveau_bo_map(nv_crtc->shadow, NOUVEAU_BO_RDWR)) {
1152                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1153                                 "Failed to map shadow buffer.\n");
1154                 return NULL;
1155         }
1156
1157         offset = nv_crtc->shadow->map;
1158 #else
1159         nv_crtc->shadow = exaOffscreenAlloc(pScreen, size, align, TRUE, NULL, NULL);
1160         if (nv_crtc->shadow == NULL) {
1161                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1162                         "Couldn't allocate shadow memory for rotated CRTC.\n");
1163                 return NULL;
1164         }
1165         offset = pNv->FB->map + nv_crtc->shadow->offset;
1166 #endif /* NOUVEAU_EXA_PIXMAPS */
1167
1168         return offset;
1169 }
1170
1171 /**
1172  * Creates a pixmap for this CRTC's rotated shadow framebuffer.
1173  */
1174 static PixmapPtr
1175 nv_crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
1176 {
1177         ScrnInfoPtr pScrn = crtc->scrn;
1178 #if NOUVEAU_EXA_PIXMAPS
1179         ScreenPtr pScreen = pScrn->pScreen;
1180         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1181 #endif /* NOUVEAU_EXA_PIXMAPS */
1182         unsigned long rotate_pitch;
1183         PixmapPtr rotate_pixmap;
1184 #if NOUVEAU_EXA_PIXMAPS
1185         struct nouveau_pixmap *nvpix;
1186 #endif /* NOUVEAU_EXA_PIXMAPS */
1187
1188         if (!data)
1189                 data = crtc->funcs->shadow_allocate (crtc, width, height);
1190
1191         rotate_pitch = pScrn->displayWidth * (pScrn->bitsPerPixel/8);
1192
1193 #if NOUVEAU_EXA_PIXMAPS
1194         /* Create a dummy pixmap, to get a private that will be accepted by the system.*/
1195         rotate_pixmap = pScreen->CreatePixmap(pScreen, 
1196                                                                 0, /* width */
1197                                                                 0, /* height */
1198         #ifdef CREATE_PIXMAP_USAGE_SCRATCH /* there seems to have been no api bump */
1199                                                                 pScrn->depth,
1200                                                                 0);
1201         #else
1202                                                                 pScrn->depth);
1203         #endif /* CREATE_PIXMAP_USAGE_SCRATCH */
1204 #else
1205         rotate_pixmap = GetScratchPixmapHeader(pScrn->pScreen,
1206                                                                 width, height,
1207                                                                 pScrn->depth,
1208                                                                 pScrn->bitsPerPixel,
1209                                                                 rotate_pitch,
1210                                                                 data);
1211 #endif /* NOUVEAU_EXA_PIXMAPS */
1212
1213         if (rotate_pixmap == NULL) {
1214                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1215                         "Couldn't allocate shadow pixmap for rotated CRTC\n");
1216         }
1217
1218 #if NOUVEAU_EXA_PIXMAPS
1219         nvpix = exaGetPixmapDriverPrivate(rotate_pixmap);
1220         if (!nvpix) {
1221                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No initial shadow private available for rotation.\n");
1222         } else {
1223                 nvpix->bo = nv_crtc->shadow;
1224                 nvpix->mapped = TRUE;
1225         }
1226
1227         /* Modify the pixmap to actually be the one we need. */
1228         pScreen->ModifyPixmapHeader(rotate_pixmap,
1229                                         width,
1230                                         height,
1231                                         pScrn->depth,
1232                                         pScrn->bitsPerPixel,
1233                                         rotate_pitch,
1234                                         data);
1235
1236         nvpix = exaGetPixmapDriverPrivate(rotate_pixmap);
1237         if (!nvpix || !nvpix->bo)
1238                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No final shadow private available for rotation.\n");
1239 #endif /* NOUVEAU_EXA_PIXMAPS */
1240
1241         return rotate_pixmap;
1242 }
1243
1244 static void
1245 nv_crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
1246 {
1247         ScrnInfoPtr pScrn = crtc->scrn;
1248         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1249         ScreenPtr pScreen = pScrn->pScreen;
1250
1251         if (rotate_pixmap) { /* This should also unmap the buffer object if relevant. */
1252                 pScreen->DestroyPixmap(rotate_pixmap);
1253         }
1254
1255 #if !NOUVEAU_EXA_PIXMAPS
1256         if (data && nv_crtc->shadow) {
1257                 exaOffscreenFree(pScreen, nv_crtc->shadow);
1258         }
1259 #endif /* !NOUVEAU_EXA_PIXMAPS */
1260
1261         nv_crtc->shadow = NULL;
1262 }
1263
1264 static const xf86CrtcFuncsRec nv_crtc_funcs = {
1265         .dpms = nv_crtc_dpms,
1266         .save = nv_crtc_save,
1267         .restore = nv_crtc_restore,
1268         .mode_fixup = nv_crtc_mode_fixup,
1269         .mode_set = nv_crtc_mode_set,
1270         .prepare = nv_crtc_prepare,
1271         .commit = nv_crtc_commit,
1272         .destroy = nv_crtc_destroy,
1273         .lock = nv_crtc_lock,
1274         .unlock = nv_crtc_unlock,
1275         .set_cursor_colors = NULL, /* Alpha cursors do not need this */
1276         .set_cursor_position = nv_crtc_set_cursor_position,
1277         .show_cursor = nv_crtc_show_cursor,
1278         .hide_cursor = nv_crtc_hide_cursor,
1279         .load_cursor_argb = nv_crtc_load_cursor_argb,
1280         .gamma_set = nv_crtc_gamma_set,
1281         .shadow_create = nv_crtc_shadow_create,
1282         .shadow_allocate = nv_crtc_shadow_allocate,
1283         .shadow_destroy = nv_crtc_shadow_destroy,
1284 };
1285
1286 void
1287 nv_crtc_init(ScrnInfoPtr pScrn, int crtc_num)
1288 {
1289         NVPtr pNv = NVPTR(pScrn);
1290         static xf86CrtcFuncsRec crtcfuncs;
1291         xf86CrtcPtr crtc;
1292         struct nouveau_crtc *nv_crtc;
1293         NVCrtcRegPtr regp = &pNv->ModeReg.crtc_reg[crtc_num];
1294         int i;
1295
1296         crtcfuncs = nv_crtc_funcs;
1297
1298         /* NV04-NV10 doesn't support alpha cursors */
1299         if (pNv->NVArch < 0x11) {
1300                 crtcfuncs.set_cursor_colors = nv_crtc_set_cursor_colors;
1301                 crtcfuncs.load_cursor_image = nv_crtc_load_cursor_image;
1302                 crtcfuncs.load_cursor_argb = NULL;
1303         }
1304         if (pNv->NoAccel) {
1305                 crtcfuncs.shadow_create = NULL;
1306                 crtcfuncs.shadow_allocate = NULL;
1307                 crtcfuncs.shadow_destroy = NULL;
1308         }
1309         
1310         if (!(crtc = xf86CrtcCreate(pScrn, &crtcfuncs)))
1311                 return;
1312
1313         if (!(nv_crtc = xcalloc(1, sizeof (struct nouveau_crtc)))) {
1314                 xf86CrtcDestroy(crtc);
1315                 return;
1316         }
1317
1318         nv_crtc->head = crtc_num;
1319         nv_crtc->last_dpms = NV_DPMS_CLEARED;
1320
1321         crtc->driver_private = nv_crtc;
1322
1323         /* Initialise the default LUT table. */
1324         for (i = 0; i < 256; i++) {
1325                 regp->DAC[i*3] = i;
1326                 regp->DAC[(i*3)+1] = i;
1327                 regp->DAC[(i*3)+2] = i;
1328         }
1329
1330         NVCrtcLockUnlock(crtc, false);
1331 }
1332
1333 static void nv_crtc_load_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1334 {
1335         ScrnInfoPtr pScrn = crtc->scrn;
1336         NVPtr pNv = NVPTR(pScrn);
1337         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1338         int i;
1339         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
1340
1341         NVWritePRMVIO(pNv, nv_crtc->head, NV_PRMVIO_MISC__WRITE, regp->MiscOutReg);
1342
1343         for (i = 0; i < 5; i++)
1344                 NVWriteVgaSeq(pNv, nv_crtc->head, i, regp->Sequencer[i]);
1345
1346         /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17] */
1347         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CR_VRE_INDEX, regp->CRTC[NV_CIO_CR_VRE_INDEX] & ~0x80);
1348
1349         for (i = 0; i < 25; i++)
1350                 NVWriteVgaCrtc(pNv, nv_crtc->head, i, regp->CRTC[i]);
1351
1352         for (i = 0; i < 9; i++)
1353                 NVWriteVgaGr(pNv, nv_crtc->head, i, regp->Graphics[i]);
1354
1355         NVSetEnablePalette(pNv, nv_crtc->head, true);
1356         for (i = 0; i < 21; i++)
1357                 NVWriteVgaAttr(pNv, nv_crtc->head, i, regp->Attribute[i]);
1358
1359         NVSetEnablePalette(pNv, nv_crtc->head, false);
1360 }
1361
1362 static void nv_crtc_load_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1363 {
1364         ScrnInfoPtr pScrn = crtc->scrn;
1365         NVPtr pNv = NVPTR(pScrn);    
1366         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1367         NVCrtcRegPtr regp;
1368         int i;
1369
1370         regp = &state->crtc_reg[nv_crtc->head];
1371
1372         if (pNv->Architecture >= NV_ARCH_10) {
1373                 if (pNv->twoHeads)
1374                         /* setting FSEL *must* come before CIO_CRE_LCD, as writing CIO_CRE_LCD sets some
1375                          * bits (16 & 17) in FSEL that should not be overwritten by writing FSEL */
1376                         NVCrtcWriteCRTC(crtc, NV_CRTC_FSEL, regp->head);
1377
1378                 nvWriteVIDEO(pNv, NV_PVIDEO_STOP, 1);
1379                 nvWriteVIDEO(pNv, NV_PVIDEO_INTR_EN, 0);
1380                 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(0), 0);
1381                 nvWriteVIDEO(pNv, NV_PVIDEO_OFFSET_BUFF(1), 0);
1382                 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(0), pNv->VRAMPhysicalSize - 1);
1383                 nvWriteVIDEO(pNv, NV_PVIDEO_LIMIT(1), pNv->VRAMPhysicalSize - 1);
1384                 nvWriteVIDEO(pNv, NV_PVIDEO_UVPLANE_LIMIT(0), pNv->VRAMPhysicalSize - 1);
1385                 nvWriteVIDEO(pNv, NV_PVIDEO_UVPLANE_LIMIT(1), pNv->VRAMPhysicalSize - 1);
1386                 nvWriteMC(pNv, NV_PBUS_POWERCTRL_2, 0);
1387
1388                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_21, regp->CRTC[NV_CIO_CRE_21]);
1389                 NVCrtcWriteCRTC(crtc, NV_CRTC_CURSOR_CONFIG, regp->cursorConfig);
1390                 NVCrtcWriteCRTC(crtc, NV_CRTC_0830, regp->unk830);
1391                 NVCrtcWriteCRTC(crtc, NV_CRTC_0834, regp->unk834);
1392                 if (pNv->Architecture == NV_ARCH_40) {
1393                         NVCrtcWriteCRTC(crtc, NV_CRTC_0850, regp->unk850);
1394                         NVCrtcWriteCRTC(crtc, NV_CRTC_GPIO_EXT, regp->gpio_ext);
1395                 }
1396
1397                 if (pNv->Architecture == NV_ARCH_40) {
1398                         uint32_t reg900 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_900);
1399                         if (regp->config == NV_PCRTC_CONFIG_START_ADDRESS_HSYNC)
1400                                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_900, reg900 | 0x10000);
1401                         else
1402                                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_900, reg900 & ~0x10000);
1403                 }
1404         }
1405
1406         NVCrtcWriteCRTC(crtc, NV_CRTC_CONFIG, regp->config);
1407         NVCrtcWriteCRTC(crtc, NV_CRTC_GPIO, regp->gpio);
1408
1409         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC0_INDEX, regp->CRTC[NV_CIO_CRE_RPC0_INDEX]);
1410         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC1_INDEX, regp->CRTC[NV_CIO_CRE_RPC1_INDEX]);
1411         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_LSR_INDEX, regp->CRTC[NV_CIO_CRE_LSR_INDEX]);
1412         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_PIXEL_INDEX, regp->CRTC[NV_CIO_CRE_PIXEL_INDEX]);
1413         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_LCD__INDEX, regp->CRTC[NV_CIO_CRE_LCD__INDEX]);
1414         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HEB__INDEX, regp->CRTC[NV_CIO_CRE_HEB__INDEX]);
1415         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_ENH_INDEX, regp->CRTC[NV_CIO_CRE_ENH_INDEX]);
1416         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_FF_INDEX, regp->CRTC[NV_CIO_CRE_FF_INDEX]);
1417         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_FFLWM__INDEX, regp->CRTC[NV_CIO_CRE_FFLWM__INDEX]);
1418         if (pNv->Architecture >= NV_ARCH_30)
1419                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_47, regp->CRTC[NV_CIO_CRE_47]);
1420
1421         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR0_INDEX, regp->CRTC[NV_CIO_CRE_HCUR_ADDR0_INDEX]);
1422         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR1_INDEX, regp->CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX]);
1423         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR2_INDEX, regp->CRTC[NV_CIO_CRE_HCUR_ADDR2_INDEX]);
1424         if (pNv->Architecture == NV_ARCH_40)
1425                 nv_fix_nv40_hw_cursor(pNv, nv_crtc->head);
1426         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_ILACE__INDEX, regp->CRTC[NV_CIO_CRE_ILACE__INDEX]);
1427
1428         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CR_ARX_INDEX, regp->CRTC[NV_CIO_CR_ARX_INDEX]);
1429         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_SCRATCH3__INDEX, regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX]);
1430         NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_SCRATCH4__INDEX, regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX]);
1431         if (pNv->Architecture >= NV_ARCH_10) {
1432                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_EBR_INDEX, regp->CRTC[NV_CIO_CRE_EBR_INDEX]);
1433                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_CSB, regp->CRTC[NV_CIO_CRE_CSB]);
1434                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_4B, regp->CRTC[NV_CIO_CRE_4B]);
1435                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_52, regp->CRTC[NV_CIO_CRE_52]);
1436         }
1437         /* NV11 and NV20 stop at 0x52. */
1438         if (pNv->NVArch >= 0x17 && pNv->twoHeads) {
1439                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_53, regp->CRTC[NV_CIO_CRE_53]);
1440                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_54, regp->CRTC[NV_CIO_CRE_54]);
1441
1442                 for (i = 0; i < 0x10; i++)
1443                         NVWriteVgaCrtc5758(pNv, nv_crtc->head, i, regp->CR58[i]);
1444                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_59, regp->CRTC[NV_CIO_CRE_59]);
1445
1446                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_85, regp->CRTC[NV_CIO_CRE_85]);
1447                 NVWriteVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_86, regp->CRTC[NV_CIO_CRE_86]);
1448         }
1449
1450         NVCrtcWriteCRTC(crtc, NV_CRTC_START, regp->fb_start);
1451
1452         /* Setting 1 on this value gives you interrupts for every vblank period. */
1453         NVCrtcWriteCRTC(crtc, NV_CRTC_INTR_EN_0, 0);
1454         NVCrtcWriteCRTC(crtc, NV_CRTC_INTR_0, NV_CRTC_INTR_VBLANK);
1455 }
1456
1457 static void nv_crtc_save_state_vga(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1458 {
1459         ScrnInfoPtr pScrn = crtc->scrn;
1460         NVPtr pNv = NVPTR(pScrn);
1461         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1462         int i;
1463         NVCrtcRegPtr regp = &state->crtc_reg[nv_crtc->head];
1464
1465         regp->MiscOutReg = NVReadPRMVIO(pNv, nv_crtc->head, NV_PRMVIO_MISC__READ);
1466
1467         for (i = 0; i < 25; i++)
1468                 regp->CRTC[i] = NVReadVgaCrtc(pNv, nv_crtc->head, i);
1469
1470         NVSetEnablePalette(pNv, nv_crtc->head, true);
1471         for (i = 0; i < 21; i++)
1472                 regp->Attribute[i] = NVReadVgaAttr(pNv, nv_crtc->head, i);
1473         NVSetEnablePalette(pNv, nv_crtc->head, false);
1474
1475         for (i = 0; i < 9; i++)
1476                 regp->Graphics[i] = NVReadVgaGr(pNv, nv_crtc->head, i);
1477
1478         for (i = 0; i < 5; i++)
1479                 regp->Sequencer[i] = NVReadVgaSeq(pNv, nv_crtc->head, i);
1480 }
1481
1482 static void nv_crtc_save_state_ext(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1483 {
1484         ScrnInfoPtr pScrn = crtc->scrn;
1485         NVPtr pNv = NVPTR(pScrn);
1486         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1487         NVCrtcRegPtr regp;
1488         int i;
1489
1490         regp = &state->crtc_reg[nv_crtc->head];
1491
1492         regp->CRTC[NV_CIO_CRE_LCD__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_LCD__INDEX);
1493         regp->CRTC[NV_CIO_CRE_RPC0_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC0_INDEX);
1494         regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_RPC1_INDEX);
1495         regp->CRTC[NV_CIO_CRE_LSR_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_LSR_INDEX);
1496         regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_PIXEL_INDEX);
1497         regp->CRTC[NV_CIO_CRE_HEB__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HEB__INDEX);
1498         regp->CRTC[NV_CIO_CRE_ENH_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_ENH_INDEX);
1499
1500         regp->CRTC[NV_CIO_CRE_FF_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_FF_INDEX);
1501         regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_FFLWM__INDEX);
1502         regp->CRTC[NV_CIO_CRE_21] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_21);
1503         if (pNv->Architecture >= NV_ARCH_30)
1504                 regp->CRTC[NV_CIO_CRE_47] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_47);
1505         regp->CRTC[NV_CIO_CRE_HCUR_ADDR0_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR0_INDEX);
1506         regp->CRTC[NV_CIO_CRE_HCUR_ADDR1_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR1_INDEX);
1507         regp->CRTC[NV_CIO_CRE_HCUR_ADDR2_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_HCUR_ADDR2_INDEX);
1508         regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_ILACE__INDEX);
1509
1510         if (pNv->Architecture >= NV_ARCH_10) {
1511                 regp->unk830 = NVCrtcReadCRTC(crtc, NV_CRTC_0830);
1512                 regp->unk834 = NVCrtcReadCRTC(crtc, NV_CRTC_0834);
1513                 if (pNv->Architecture == NV_ARCH_40) {
1514                         regp->unk850 = NVCrtcReadCRTC(crtc, NV_CRTC_0850);
1515                         regp->gpio_ext = NVCrtcReadCRTC(crtc, NV_CRTC_GPIO_EXT);
1516                 }
1517                 if (pNv->twoHeads) {
1518                         regp->head = NVCrtcReadCRTC(crtc, NV_CRTC_FSEL);
1519                         regp->crtcOwner = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_44);
1520                 }
1521                 regp->cursorConfig = NVCrtcReadCRTC(crtc, NV_CRTC_CURSOR_CONFIG);
1522         }
1523
1524         regp->gpio = NVCrtcReadCRTC(crtc, NV_CRTC_GPIO);
1525         regp->config = NVCrtcReadCRTC(crtc, NV_CRTC_CONFIG);
1526
1527         regp->CRTC[NV_CIO_CR_ARX_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CR_ARX_INDEX);
1528         regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_SCRATCH3__INDEX);
1529         regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_SCRATCH4__INDEX);
1530         if (pNv->Architecture >= NV_ARCH_10) {
1531                 regp->CRTC[NV_CIO_CRE_EBR_INDEX] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_EBR_INDEX);
1532                 regp->CRTC[NV_CIO_CRE_CSB] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_CSB);
1533                 regp->CRTC[NV_CIO_CRE_4B] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_4B);
1534                 regp->CRTC[NV_CIO_CRE_52] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_52);
1535         }
1536         /* NV11 and NV20 don't have this, they stop at 0x52. */
1537         if (pNv->NVArch >= 0x17 && pNv->twoHeads) {
1538                 for (i = 0; i < 0x10; i++)
1539                         regp->CR58[i] = NVReadVgaCrtc5758(pNv, nv_crtc->head, i);
1540
1541                 regp->CRTC[NV_CIO_CRE_59] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_59);
1542                 regp->CRTC[NV_CIO_CRE_53] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_53);
1543                 regp->CRTC[NV_CIO_CRE_54] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_54);
1544
1545                 regp->CRTC[NV_CIO_CRE_85] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_85);
1546                 regp->CRTC[NV_CIO_CRE_86] = NVReadVgaCrtc(pNv, nv_crtc->head, NV_CIO_CRE_86);
1547         }
1548
1549         regp->fb_start = NVCrtcReadCRTC(crtc, NV_CRTC_START);
1550 }
1551
1552 static void nv_crtc_save_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1553 {
1554         ScrnInfoPtr pScrn = crtc->scrn;
1555         NVPtr pNv = NVPTR(pScrn);    
1556         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1557         NVCrtcRegPtr regp;
1558         int i;
1559
1560         regp = &state->crtc_reg[nv_crtc->head];
1561
1562         regp->general = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_GENERAL_CONTROL);
1563
1564         if (pNv->twoHeads) {
1565                 if (pNv->NVArch >= 0x17)
1566                         regp->unk_630 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_630);
1567                 regp->fp_control        = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_CONTROL);
1568                 regp->debug_0   = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_0);
1569                 regp->debug_1   = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_1);
1570                 regp->debug_2   = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_2);
1571
1572                 regp->unk_a20 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_A20);
1573                 regp->unk_a24 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_A24);
1574                 regp->unk_a34 = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_A34);
1575         }
1576
1577         if (pNv->NVArch == 0x11) {
1578                 regp->dither = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_DITHER_NV11);
1579         } else if (pNv->twoHeads) {
1580                 regp->dither = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_DITHER);
1581                 for (i = 0; i < 3; i++) {
1582                         regp->dither_regs[i] = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_850 + i * 4);
1583                         regp->dither_regs[i + 3] = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_FP_85C + i * 4);
1584                 }
1585         }
1586         if (pNv->Architecture >= NV_ARCH_10)
1587                 regp->nv10_cursync = NVCrtcReadRAMDAC(crtc, NV_RAMDAC_NV10_CURSYNC);
1588
1589         /* The regs below are 0 for non-flatpanels, so you can load and save them */
1590
1591         for (i = 0; i < 7; i++) {
1592                 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
1593                 regp->fp_horiz_regs[i] = NVCrtcReadRAMDAC(crtc, ramdac_reg);
1594         }
1595
1596         for (i = 0; i < 7; i++) {
1597                 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
1598                 regp->fp_vert_regs[i] = NVCrtcReadRAMDAC(crtc, ramdac_reg);
1599         }
1600 }
1601
1602 static void nv_crtc_load_state_ramdac(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1603 {
1604         ScrnInfoPtr pScrn = crtc->scrn;
1605         NVPtr pNv = NVPTR(pScrn);    
1606         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1607         NVCrtcRegPtr regp;
1608         int i;
1609
1610         regp = &state->crtc_reg[nv_crtc->head];
1611
1612         NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_GENERAL_CONTROL, regp->general);
1613
1614         if (pNv->twoHeads) {
1615                 if (pNv->NVArch >= 0x17)
1616                         NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_630, regp->unk_630);
1617                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_CONTROL, regp->fp_control);
1618                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_0, regp->debug_0);
1619                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_1, regp->debug_1);
1620                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_DEBUG_2, regp->debug_2);
1621                 if (pNv->NVArch == 0x30) { /* For unknown purposes. */
1622                         uint32_t reg890 = NVCrtcReadRAMDAC(crtc, NV30_RAMDAC_890);
1623                         NVCrtcWriteRAMDAC(crtc, NV30_RAMDAC_89C, reg890);
1624                 }
1625
1626                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_A20, regp->unk_a20);
1627                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_A24, regp->unk_a24);
1628                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_A34, regp->unk_a34);
1629         }
1630
1631         if (pNv->NVArch == 0x11)
1632                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_DITHER_NV11, regp->dither);
1633         else if (pNv->twoHeads) {
1634                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_DITHER, regp->dither);
1635                 for (i = 0; i < 3; i++) {
1636                         NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_850 + i * 4, regp->dither_regs[i]);
1637                         NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_FP_85C + i * 4, regp->dither_regs[i + 3]);
1638                 }
1639         }
1640         if (pNv->Architecture >= NV_ARCH_10)
1641                 NVCrtcWriteRAMDAC(crtc, NV_RAMDAC_NV10_CURSYNC, regp->nv10_cursync);
1642
1643         /* The regs below are 0 for non-flatpanels, so you can load and save them */
1644
1645         for (i = 0; i < 7; i++) {
1646                 uint32_t ramdac_reg = NV_RAMDAC_FP_HDISP_END + (i * 4);
1647                 NVCrtcWriteRAMDAC(crtc, ramdac_reg, regp->fp_horiz_regs[i]);
1648         }
1649
1650         for (i = 0; i < 7; i++) {
1651                 uint32_t ramdac_reg = NV_RAMDAC_FP_VDISP_END + (i * 4);
1652                 NVCrtcWriteRAMDAC(crtc, ramdac_reg, regp->fp_vert_regs[i]);
1653         }
1654 }
1655
1656 void NVCrtcSetBase(xf86CrtcPtr crtc, int x, int y)
1657 {
1658         ScrnInfoPtr pScrn = crtc->scrn;
1659         NVPtr pNv = NVPTR(pScrn);    
1660         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1661         uint32_t start = (y * pScrn->displayWidth + x) * pScrn->bitsPerPixel / 8;
1662
1663         if (crtc->rotatedData != NULL) /* we do not exist on the real framebuffer */
1664 #if NOUVEAU_EXA_PIXMAPS
1665                 start = nv_crtc->shadow->offset;
1666 #else
1667                 start = pNv->FB->offset + nv_crtc->shadow->offset; /* We do exist relative to the framebuffer */
1668 #endif
1669         else
1670                 start += pNv->FB->offset;
1671
1672         /* 30 bits addresses in 32 bits according to haiku */
1673         start &= ~3;
1674         pNv->ModeReg.crtc_reg[nv_crtc->head].fb_start = start;
1675         NVCrtcWriteCRTC(crtc, NV_CRTC_START, start);
1676
1677         crtc->x = x;
1678         crtc->y = y;
1679 }
1680
1681 static void nv_crtc_save_state_palette(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1682 {
1683         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1684         NVPtr pNv = NVPTR(crtc->scrn);
1685         int head_offset = nv_crtc->head * NV_PRMDIO_SIZE, i;
1686
1687         VGA_WR08(pNv->REGS, NV_PRMDIO_PIXEL_MASK + head_offset, NV_PRMDIO_PIXEL_MASK_MASK);
1688         VGA_WR08(pNv->REGS, NV_PRMDIO_READ_MODE_ADDRESS + head_offset, 0x0);
1689
1690         for (i = 0; i < 768; i++) {
1691                 state->crtc_reg[nv_crtc->head].DAC[i] = NV_RD08(pNv->REGS, NV_PRMDIO_PALETTE_DATA + head_offset);
1692                 DDXMMIOH("nv_crtc_save_state_palette: head %d reg 0x%04x data 0x%02x\n", nv_crtc->head, NV_PRMDIO_PALETTE_DATA + head_offset, state->crtc_reg[nv_crtc->head].DAC[i]);
1693         }
1694
1695         NVSetEnablePalette(pNv, nv_crtc->head, false);
1696 }
1697 static void nv_crtc_load_state_palette(xf86CrtcPtr crtc, RIVA_HW_STATE *state)
1698 {
1699         struct nouveau_crtc *nv_crtc = to_nouveau_crtc(crtc);
1700         NVPtr pNv = NVPTR(crtc->scrn);
1701         int head_offset = nv_crtc->head * NV_PRMDIO_SIZE, i;
1702
1703         VGA_WR08(pNv->REGS, NV_PRMDIO_PIXEL_MASK + head_offset, NV_PRMDIO_PIXEL_MASK_MASK);
1704         VGA_WR08(pNv->REGS, NV_PRMDIO_WRITE_MODE_ADDRESS + head_offset, 0x0);
1705
1706         for (i = 0; i < 768; i++) {
1707                 DDXMMIOH("nv_crtc_load_state_palette: head %d reg 0x%04x data 0x%02x\n", nv_crtc->head, NV_PRMDIO_PALETTE_DATA + head_offset, state->crtc_reg[nv_crtc->head].DAC[i]);
1708                 NV_WR08(pNv->REGS, NV_PRMDIO_PALETTE_DATA + head_offset, state->crtc_reg[nv_crtc->head].DAC[i]);
1709         }
1710
1711         NVSetEnablePalette(pNv, nv_crtc->head, false);
1712 }