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