nouveau: add runtime PM support (v0.9)
[deliverable/linux.git] / drivers / gpu / drm / nouveau / dispnv04 / crtc.c
CommitLineData
6ee73861
BS
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 */
5addcf0a 25#include <linux/pm_runtime.h>
6ee73861 26
760285e7
DH
27#include <drm/drmP.h>
28#include <drm/drm_crtc_helper.h>
6ee73861 29
77145f1c
BS
30#include "nouveau_drm.h"
31#include "nouveau_reg.h"
32#include "nouveau_bo.h"
33#include "nouveau_gem.h"
6ee73861
BS
34#include "nouveau_encoder.h"
35#include "nouveau_connector.h"
36#include "nouveau_crtc.h"
1a646342 37#include "hw.h"
6ee73861 38#include "nvreg.h"
a424d761 39#include "nouveau_fbcon.h"
1a646342 40#include "disp.h"
77145f1c
BS
41
42#include <subdev/bios/pll.h>
43#include <subdev/clock.h>
6ee73861
BS
44
45static int
46nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
47 struct drm_framebuffer *old_fb);
48
49static void
50crtc_wr_cio_state(struct drm_crtc *crtc, struct nv04_crtc_reg *crtcstate, int index)
51{
52 NVWriteVgaCrtc(crtc->dev, nouveau_crtc(crtc)->index, index,
53 crtcstate->CRTC[index]);
54}
55
56static void nv_crtc_set_digital_vibrance(struct drm_crtc *crtc, int level)
57{
58 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29
BS
59 struct drm_device *dev = crtc->dev;
60 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
6ee73861
BS
61
62 regp->CRTC[NV_CIO_CRE_CSB] = nv_crtc->saturation = level;
63 if (nv_crtc->saturation && nv_gf4_disp_arch(crtc->dev)) {
64 regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
65 regp->CRTC[NV_CIO_CRE_5B] = nv_crtc->saturation << 2;
66 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_5B);
67 }
68 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_CSB);
69}
70
71static void nv_crtc_set_image_sharpening(struct drm_crtc *crtc, int level)
72{
73 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29
BS
74 struct drm_device *dev = crtc->dev;
75 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
6ee73861
BS
76
77 nv_crtc->sharpness = level;
78 if (level < 0) /* blur is in hw range 0x3f -> 0x20 */
79 level += 0x40;
80 regp->ramdac_634 = level;
81 NVWriteRAMDAC(crtc->dev, nv_crtc->index, NV_PRAMDAC_634, regp->ramdac_634);
82}
83
84#define PLLSEL_VPLL1_MASK \
85 (NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_VPLL \
86 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK_RATIO_DB2)
87#define PLLSEL_VPLL2_MASK \
88 (NV_PRAMDAC_PLL_COEFF_SELECT_PLL_SOURCE_VPLL2 \
89 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK2_RATIO_DB2)
90#define PLLSEL_TV_MASK \
91 (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
92 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1 \
93 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
94 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
95
96/* NV4x 0x40.. pll notes:
97 * gpu pll: 0x4000 + 0x4004
98 * ?gpu? pll: 0x4008 + 0x400c
99 * vpll1: 0x4010 + 0x4014
100 * vpll2: 0x4018 + 0x401c
101 * mpll: 0x4020 + 0x4024
102 * mpll: 0x4038 + 0x403c
103 *
104 * the first register of each pair has some unknown details:
105 * bits 0-7: redirected values from elsewhere? (similar to PLL_SETUP_CONTROL?)
106 * bits 20-23: (mpll) something to do with post divider?
107 * bits 28-31: related to single stage mode? (bit 8/12)
108 */
109
110static void nv_crtc_calc_state_ext(struct drm_crtc *crtc, struct drm_display_mode * mode, int dot_clock)
111{
112 struct drm_device *dev = crtc->dev;
77145f1c
BS
113 struct nouveau_drm *drm = nouveau_drm(dev);
114 struct nouveau_bios *bios = nouveau_bios(drm->device);
115 struct nouveau_clock *clk = nouveau_clock(drm->device);
6ee73861 116 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29 117 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
6ee73861
BS
118 struct nv04_crtc_reg *regp = &state->crtc_reg[nv_crtc->index];
119 struct nouveau_pll_vals *pv = &regp->pllvals;
70790f4f 120 struct nvbios_pll pll_lim;
6ee73861 121
77145f1c
BS
122 if (nvbios_pll_parse(bios, nv_crtc->index ? PLL_VPLL1 : PLL_VPLL0,
123 &pll_lim))
6ee73861
BS
124 return;
125
126 /* NM2 == 0 is used to determine single stage mode on two stage plls */
127 pv->NM2 = 0;
128
129 /* for newer nv4x the blob uses only the first stage of the vpll below a
130 * certain clock. for a certain nv4b this is 150MHz. since the max
131 * output frequency of the first stage for this card is 300MHz, it is
132 * assumed the threshold is given by vco1 maxfreq/2
133 */
134 /* for early nv4x, specifically nv40 and *some* nv43 (devids 0 and 6,
135 * not 8, others unknown), the blob always uses both plls. no problem
136 * has yet been observed in allowing the use a single stage pll on all
137 * nv43 however. the behaviour of single stage use is untested on nv40
138 */
77145f1c 139 if (nv_device(drm->device)->chipset > 0x40 && dot_clock <= (pll_lim.vco1.max_freq / 2))
6ee73861
BS
140 memset(&pll_lim.vco2, 0, sizeof(pll_lim.vco2));
141
77145f1c
BS
142
143 if (!clk->pll_calc(clk, &pll_lim, dot_clock, pv))
6ee73861
BS
144 return;
145
146 state->pllsel &= PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK;
147
148 /* The blob uses this always, so let's do the same */
77145f1c 149 if (nv_device(drm->device)->card_type == NV_40)
6ee73861
BS
150 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_USE_VPLL2_TRUE;
151 /* again nv40 and some nv43 act more like nv3x as described above */
77145f1c 152 if (nv_device(drm->device)->chipset < 0x41)
6ee73861
BS
153 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_MPLL |
154 NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_NVPLL;
155 state->pllsel |= nv_crtc->index ? PLLSEL_VPLL2_MASK : PLLSEL_VPLL1_MASK;
156
157 if (pv->NM2)
77145f1c 158 NV_DEBUG(drm, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n",
6ee73861
BS
159 pv->N1, pv->N2, pv->M1, pv->M2, pv->log2P);
160 else
77145f1c 161 NV_DEBUG(drm, "vpll: n %d m %d log2p %d\n",
6ee73861
BS
162 pv->N1, pv->M1, pv->log2P);
163
164 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
165}
166
167static void
168nv_crtc_dpms(struct drm_crtc *crtc, int mode)
169{
170 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
171 struct drm_device *dev = crtc->dev;
77145f1c 172 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861
BS
173 unsigned char seq1 = 0, crtc17 = 0;
174 unsigned char crtc1A;
175
77145f1c 176 NV_DEBUG(drm, "Setting dpms mode %d on CRTC %d\n", mode,
6ee73861
BS
177 nv_crtc->index);
178
25985edc 179 if (nv_crtc->last_dpms == mode) /* Don't do unnecessary mode changes. */
6ee73861
BS
180 return;
181
182 nv_crtc->last_dpms = mode;
183
184 if (nv_two_heads(dev))
185 NVSetOwner(dev, nv_crtc->index);
186
187 /* nv4ref indicates these two RPC1 bits inhibit h/v sync */
188 crtc1A = NVReadVgaCrtc(dev, nv_crtc->index,
189 NV_CIO_CRE_RPC1_INDEX) & ~0xC0;
190 switch (mode) {
191 case DRM_MODE_DPMS_STANDBY:
192 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
193 seq1 = 0x20;
194 crtc17 = 0x80;
195 crtc1A |= 0x80;
196 break;
197 case DRM_MODE_DPMS_SUSPEND:
198 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
199 seq1 = 0x20;
200 crtc17 = 0x80;
201 crtc1A |= 0x40;
202 break;
203 case DRM_MODE_DPMS_OFF:
204 /* Screen: Off; HSync: Off, VSync: Off */
205 seq1 = 0x20;
206 crtc17 = 0x00;
207 crtc1A |= 0xC0;
208 break;
209 case DRM_MODE_DPMS_ON:
210 default:
211 /* Screen: On; HSync: On, VSync: On */
212 seq1 = 0x00;
213 crtc17 = 0x80;
214 break;
215 }
216
217 NVVgaSeqReset(dev, nv_crtc->index, true);
218 /* Each head has it's own sequencer, so we can turn it off when we want */
219 seq1 |= (NVReadVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX) & ~0x20);
220 NVWriteVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX, seq1);
221 crtc17 |= (NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX) & ~0x80);
222 mdelay(10);
223 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX, crtc17);
224 NVVgaSeqReset(dev, nv_crtc->index, false);
225
226 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RPC1_INDEX, crtc1A);
227}
228
229static bool
e811f5ae 230nv_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
6ee73861
BS
231 struct drm_display_mode *adjusted_mode)
232{
233 return true;
234}
235
236static void
237nv_crtc_mode_set_vga(struct drm_crtc *crtc, struct drm_display_mode *mode)
238{
239 struct drm_device *dev = crtc->dev;
6ee73861 240 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29 241 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
6ee73861
BS
242 struct drm_framebuffer *fb = crtc->fb;
243
244 /* Calculate our timings */
e5ec882c
FJ
245 int horizDisplay = (mode->crtc_hdisplay >> 3) - 1;
246 int horizStart = (mode->crtc_hsync_start >> 3) + 1;
247 int horizEnd = (mode->crtc_hsync_end >> 3) + 1;
6ee73861
BS
248 int horizTotal = (mode->crtc_htotal >> 3) - 5;
249 int horizBlankStart = (mode->crtc_hdisplay >> 3) - 1;
250 int horizBlankEnd = (mode->crtc_htotal >> 3) - 1;
251 int vertDisplay = mode->crtc_vdisplay - 1;
252 int vertStart = mode->crtc_vsync_start - 1;
253 int vertEnd = mode->crtc_vsync_end - 1;
254 int vertTotal = mode->crtc_vtotal - 2;
255 int vertBlankStart = mode->crtc_vdisplay - 1;
256 int vertBlankEnd = mode->crtc_vtotal - 1;
257
258 struct drm_encoder *encoder;
259 bool fp_output = false;
260
261 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
262 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
263
264 if (encoder->crtc == crtc &&
cb75d97e
BS
265 (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
266 nv_encoder->dcb->type == DCB_OUTPUT_TMDS))
6ee73861
BS
267 fp_output = true;
268 }
269
270 if (fp_output) {
271 vertStart = vertTotal - 3;
272 vertEnd = vertTotal - 2;
273 vertBlankStart = vertStart;
274 horizStart = horizTotal - 5;
275 horizEnd = horizTotal - 2;
276 horizBlankEnd = horizTotal + 4;
277#if 0
77145f1c 278 if (dev->overlayAdaptor && nv_device(drm->device)->card_type >= NV_10)
6ee73861
BS
279 /* This reportedly works around some video overlay bandwidth problems */
280 horizTotal += 2;
281#endif
282 }
283
284 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
285 vertTotal |= 1;
286
287#if 0
288 ErrorF("horizDisplay: 0x%X \n", horizDisplay);
289 ErrorF("horizStart: 0x%X \n", horizStart);
290 ErrorF("horizEnd: 0x%X \n", horizEnd);
291 ErrorF("horizTotal: 0x%X \n", horizTotal);
292 ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
293 ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
294 ErrorF("vertDisplay: 0x%X \n", vertDisplay);
295 ErrorF("vertStart: 0x%X \n", vertStart);
296 ErrorF("vertEnd: 0x%X \n", vertEnd);
297 ErrorF("vertTotal: 0x%X \n", vertTotal);
298 ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
299 ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
300#endif
301
302 /*
303 * compute correct Hsync & Vsync polarity
304 */
305 if ((mode->flags & (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC))
306 && (mode->flags & (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC))) {
307
308 regp->MiscOutReg = 0x23;
309 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
310 regp->MiscOutReg |= 0x40;
311 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
312 regp->MiscOutReg |= 0x80;
313 } else {
314 int vdisplay = mode->vdisplay;
315 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
316 vdisplay *= 2;
317 if (mode->vscan > 1)
318 vdisplay *= mode->vscan;
319 if (vdisplay < 400)
320 regp->MiscOutReg = 0xA3; /* +hsync -vsync */
321 else if (vdisplay < 480)
322 regp->MiscOutReg = 0x63; /* -hsync +vsync */
323 else if (vdisplay < 768)
324 regp->MiscOutReg = 0xE3; /* -hsync -vsync */
325 else
326 regp->MiscOutReg = 0x23; /* +hsync +vsync */
327 }
328
329 regp->MiscOutReg |= (mode->clock_index & 0x03) << 2;
330
331 /*
332 * Time Sequencer
333 */
334 regp->Sequencer[NV_VIO_SR_RESET_INDEX] = 0x00;
335 /* 0x20 disables the sequencer */
336 if (mode->flags & DRM_MODE_FLAG_CLKDIV2)
337 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x29;
338 else
339 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x21;
340 regp->Sequencer[NV_VIO_SR_PLANE_MASK_INDEX] = 0x0F;
341 regp->Sequencer[NV_VIO_SR_CHAR_MAP_INDEX] = 0x00;
342 regp->Sequencer[NV_VIO_SR_MEM_MODE_INDEX] = 0x0E;
343
344 /*
345 * CRTC
346 */
347 regp->CRTC[NV_CIO_CR_HDT_INDEX] = horizTotal;
348 regp->CRTC[NV_CIO_CR_HDE_INDEX] = horizDisplay;
349 regp->CRTC[NV_CIO_CR_HBS_INDEX] = horizBlankStart;
350 regp->CRTC[NV_CIO_CR_HBE_INDEX] = (1 << 7) |
351 XLATE(horizBlankEnd, 0, NV_CIO_CR_HBE_4_0);
352 regp->CRTC[NV_CIO_CR_HRS_INDEX] = horizStart;
353 regp->CRTC[NV_CIO_CR_HRE_INDEX] = XLATE(horizBlankEnd, 5, NV_CIO_CR_HRE_HBE_5) |
354 XLATE(horizEnd, 0, NV_CIO_CR_HRE_4_0);
355 regp->CRTC[NV_CIO_CR_VDT_INDEX] = vertTotal;
356 regp->CRTC[NV_CIO_CR_OVL_INDEX] = XLATE(vertStart, 9, NV_CIO_CR_OVL_VRS_9) |
357 XLATE(vertDisplay, 9, NV_CIO_CR_OVL_VDE_9) |
358 XLATE(vertTotal, 9, NV_CIO_CR_OVL_VDT_9) |
359 (1 << 4) |
360 XLATE(vertBlankStart, 8, NV_CIO_CR_OVL_VBS_8) |
361 XLATE(vertStart, 8, NV_CIO_CR_OVL_VRS_8) |
362 XLATE(vertDisplay, 8, NV_CIO_CR_OVL_VDE_8) |
363 XLATE(vertTotal, 8, NV_CIO_CR_OVL_VDT_8);
364 regp->CRTC[NV_CIO_CR_RSAL_INDEX] = 0x00;
365 regp->CRTC[NV_CIO_CR_CELL_HT_INDEX] = ((mode->flags & DRM_MODE_FLAG_DBLSCAN) ? MASK(NV_CIO_CR_CELL_HT_SCANDBL) : 0) |
366 1 << 6 |
367 XLATE(vertBlankStart, 9, NV_CIO_CR_CELL_HT_VBS_9);
368 regp->CRTC[NV_CIO_CR_CURS_ST_INDEX] = 0x00;
369 regp->CRTC[NV_CIO_CR_CURS_END_INDEX] = 0x00;
370 regp->CRTC[NV_CIO_CR_SA_HI_INDEX] = 0x00;
371 regp->CRTC[NV_CIO_CR_SA_LO_INDEX] = 0x00;
372 regp->CRTC[NV_CIO_CR_TCOFF_HI_INDEX] = 0x00;
373 regp->CRTC[NV_CIO_CR_TCOFF_LO_INDEX] = 0x00;
374 regp->CRTC[NV_CIO_CR_VRS_INDEX] = vertStart;
375 regp->CRTC[NV_CIO_CR_VRE_INDEX] = 1 << 5 | XLATE(vertEnd, 0, NV_CIO_CR_VRE_3_0);
376 regp->CRTC[NV_CIO_CR_VDE_INDEX] = vertDisplay;
377 /* framebuffer can be larger than crtc scanout area. */
01f2c773 378 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = fb->pitches[0] / 8;
6ee73861
BS
379 regp->CRTC[NV_CIO_CR_ULINE_INDEX] = 0x00;
380 regp->CRTC[NV_CIO_CR_VBS_INDEX] = vertBlankStart;
381 regp->CRTC[NV_CIO_CR_VBE_INDEX] = vertBlankEnd;
382 regp->CRTC[NV_CIO_CR_MODE_INDEX] = 0x43;
383 regp->CRTC[NV_CIO_CR_LCOMP_INDEX] = 0xff;
384
385 /*
386 * Some extended CRTC registers (they are not saved with the rest of the vga regs).
387 */
388
389 /* framebuffer can be larger than crtc scanout area. */
c1003d9c 390 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
01f2c773 391 XLATE(fb->pitches[0] / 8, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
c1003d9c 392 regp->CRTC[NV_CIO_CRE_42] =
01f2c773 393 XLATE(fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
6ee73861
BS
394 regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->crtc_hdisplay < 1280 ?
395 MASK(NV_CIO_CRE_RPC1_LARGE) : 0x00;
396 regp->CRTC[NV_CIO_CRE_LSR_INDEX] = XLATE(horizBlankEnd, 6, NV_CIO_CRE_LSR_HBE_6) |
397 XLATE(vertBlankStart, 10, NV_CIO_CRE_LSR_VBS_10) |
398 XLATE(vertStart, 10, NV_CIO_CRE_LSR_VRS_10) |
399 XLATE(vertDisplay, 10, NV_CIO_CRE_LSR_VDE_10) |
400 XLATE(vertTotal, 10, NV_CIO_CRE_LSR_VDT_10);
401 regp->CRTC[NV_CIO_CRE_HEB__INDEX] = XLATE(horizStart, 8, NV_CIO_CRE_HEB_HRS_8) |
402 XLATE(horizBlankStart, 8, NV_CIO_CRE_HEB_HBS_8) |
403 XLATE(horizDisplay, 8, NV_CIO_CRE_HEB_HDE_8) |
404 XLATE(horizTotal, 8, NV_CIO_CRE_HEB_HDT_8);
405 regp->CRTC[NV_CIO_CRE_EBR_INDEX] = XLATE(vertBlankStart, 11, NV_CIO_CRE_EBR_VBS_11) |
406 XLATE(vertStart, 11, NV_CIO_CRE_EBR_VRS_11) |
407 XLATE(vertDisplay, 11, NV_CIO_CRE_EBR_VDE_11) |
408 XLATE(vertTotal, 11, NV_CIO_CRE_EBR_VDT_11);
409
410 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
411 horizTotal = (horizTotal >> 1) & ~1;
412 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = horizTotal;
413 regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= XLATE(horizTotal, 8, NV_CIO_CRE_HEB_ILC_8);
414 } else
415 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff; /* interlace off */
416
417 /*
418 * Graphics Display Controller
419 */
420 regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
421 regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
422 regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
423 regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
424 regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
425 regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
426 regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
427 regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
428 regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
429
430 regp->Attribute[0] = 0x00; /* standard colormap translation */
431 regp->Attribute[1] = 0x01;
432 regp->Attribute[2] = 0x02;
433 regp->Attribute[3] = 0x03;
434 regp->Attribute[4] = 0x04;
435 regp->Attribute[5] = 0x05;
436 regp->Attribute[6] = 0x06;
437 regp->Attribute[7] = 0x07;
438 regp->Attribute[8] = 0x08;
439 regp->Attribute[9] = 0x09;
440 regp->Attribute[10] = 0x0A;
441 regp->Attribute[11] = 0x0B;
442 regp->Attribute[12] = 0x0C;
443 regp->Attribute[13] = 0x0D;
444 regp->Attribute[14] = 0x0E;
445 regp->Attribute[15] = 0x0F;
446 regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
447 /* Non-vga */
448 regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
449 regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
450 regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
451 regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
452}
453
454/**
455 * Sets up registers for the given mode/adjusted_mode pair.
456 *
457 * The clocks, CRTCs and outputs attached to this CRTC must be off.
458 *
459 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
460 * be easily turned on/off after this.
461 */
462static void
463nv_crtc_mode_set_regs(struct drm_crtc *crtc, struct drm_display_mode * mode)
464{
465 struct drm_device *dev = crtc->dev;
77145f1c 466 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 467 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29
BS
468 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
469 struct nv04_crtc_reg *savep = &nv04_display(dev)->saved_reg.crtc_reg[nv_crtc->index];
6ee73861
BS
470 struct drm_encoder *encoder;
471 bool lvds_output = false, tmds_output = false, tv_output = false,
472 off_chip_digital = false;
473
474 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
475 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
476 bool digital = false;
477
478 if (encoder->crtc != crtc)
479 continue;
480
cb75d97e 481 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS)
6ee73861 482 digital = lvds_output = true;
cb75d97e 483 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
6ee73861 484 tv_output = true;
cb75d97e 485 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
6ee73861
BS
486 digital = tmds_output = true;
487 if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP && digital)
488 off_chip_digital = true;
489 }
490
491 /* Registers not directly related to the (s)vga mode */
492
493 /* What is the meaning of this register? */
494 /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
495 regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
496
497 regp->crtc_eng_ctrl = 0;
498 /* Except for rare conditions I2C is enabled on the primary crtc */
499 if (nv_crtc->index == 0)
500 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_I2C;
501#if 0
502 /* Set overlay to desired crtc. */
503 if (dev->overlayAdaptor) {
504 NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(dev);
505 if (pPriv->overlayCRTC == nv_crtc->index)
506 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_OVERLAY;
507 }
508#endif
509
510 /* ADDRESS_SPACE_PNVM is the same as setting HCUR_ASI */
511 regp->cursor_cfg = NV_PCRTC_CURSOR_CONFIG_CUR_LINES_64 |
512 NV_PCRTC_CURSOR_CONFIG_CUR_PIXELS_64 |
513 NV_PCRTC_CURSOR_CONFIG_ADDRESS_SPACE_PNVM;
77145f1c 514 if (nv_device(drm->device)->chipset >= 0x11)
6ee73861
BS
515 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_CUR_BPP_32;
516 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
517 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_DOUBLE_SCAN_ENABLE;
518
519 /* Unblock some timings */
520 regp->CRTC[NV_CIO_CRE_53] = 0;
521 regp->CRTC[NV_CIO_CRE_54] = 0;
522
523 /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
524 if (lvds_output)
525 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
526 else if (tmds_output)
527 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
528 else
529 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
530
531 /* These values seem to vary */
532 /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
533 regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
534
535 nv_crtc_set_digital_vibrance(crtc, nv_crtc->saturation);
536
537 /* probably a scratch reg, but kept for cargo-cult purposes:
538 * bit0: crtc0?, head A
539 * bit6: lvds, head A
540 * bit7: (only in X), head A
541 */
542 if (nv_crtc->index == 0)
543 regp->CRTC[NV_CIO_CRE_4B] = savep->CRTC[NV_CIO_CRE_4B] | 0x80;
544
545 /* The blob seems to take the current value from crtc 0, add 4 to that
546 * and reuse the old value for crtc 1 */
017e6e29 547 regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] = nv04_display(dev)->saved_reg.crtc_reg[0].CRTC[NV_CIO_CRE_TVOUT_LATENCY];
6ee73861
BS
548 if (!nv_crtc->index)
549 regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] += 4;
550
551 /* the blob sometimes sets |= 0x10 (which is the same as setting |=
552 * 1 << 30 on 0x60.830), for no apparent reason */
553 regp->CRTC[NV_CIO_CRE_59] = off_chip_digital;
554
77145f1c 555 if (nv_device(drm->device)->card_type >= NV_30)
4a9f822f
FJ
556 regp->CRTC[0x9f] = off_chip_digital ? 0x11 : 0x1;
557
6ee73861
BS
558 regp->crtc_830 = mode->crtc_vdisplay - 3;
559 regp->crtc_834 = mode->crtc_vdisplay - 1;
560
77145f1c 561 if (nv_device(drm->device)->card_type == NV_40)
6ee73861
BS
562 /* This is what the blob does */
563 regp->crtc_850 = NVReadCRTC(dev, 0, NV_PCRTC_850);
564
77145f1c 565 if (nv_device(drm->device)->card_type >= NV_30)
6ee73861
BS
566 regp->gpio_ext = NVReadCRTC(dev, 0, NV_PCRTC_GPIO_EXT);
567
77145f1c 568 if (nv_device(drm->device)->card_type >= NV_10)
63f7fcfe
FJ
569 regp->crtc_cfg = NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC;
570 else
571 regp->crtc_cfg = NV04_PCRTC_CONFIG_START_ADDRESS_HSYNC;
6ee73861
BS
572
573 /* Some misc regs */
77145f1c 574 if (nv_device(drm->device)->card_type == NV_40) {
6ee73861
BS
575 regp->CRTC[NV_CIO_CRE_85] = 0xFF;
576 regp->CRTC[NV_CIO_CRE_86] = 0x1;
577 }
578
579 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (crtc->fb->depth + 1) / 8;
580 /* Enable slaved mode (called MODE_TV in nv4ref.h) */
581 if (lvds_output || tmds_output || tv_output)
582 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
583
584 /* Generic PRAMDAC regs */
585
77145f1c 586 if (nv_device(drm->device)->card_type >= NV_10)
6ee73861
BS
587 /* Only bit that bios and blob set. */
588 regp->nv10_cursync = (1 << 25);
589
590 regp->ramdac_gen_ctrl = NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
591 NV_PRAMDAC_GENERAL_CONTROL_VGA_STATE_SEL |
592 NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON;
593 if (crtc->fb->depth == 16)
594 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
77145f1c 595 if (nv_device(drm->device)->chipset >= 0x11)
6ee73861
BS
596 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_PIPE_LONG;
597
598 regp->ramdac_630 = 0; /* turn off green mode (tv test pattern?) */
599 regp->tv_setup = 0;
600
601 nv_crtc_set_image_sharpening(crtc, nv_crtc->sharpness);
602
603 /* Some values the blob sets */
604 regp->ramdac_8c0 = 0x100;
605 regp->ramdac_a20 = 0x0;
606 regp->ramdac_a24 = 0xfffff;
607 regp->ramdac_a34 = 0x1;
608}
609
610/**
611 * Sets up registers for the given mode/adjusted_mode pair.
612 *
613 * The clocks, CRTCs and outputs attached to this CRTC must be off.
614 *
615 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
616 * be easily turned on/off after this.
617 */
618static int
619nv_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
620 struct drm_display_mode *adjusted_mode,
621 int x, int y, struct drm_framebuffer *old_fb)
622{
623 struct drm_device *dev = crtc->dev;
624 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
77145f1c 625 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 626
77145f1c 627 NV_DEBUG(drm, "CTRC mode on CRTC %d:\n", nv_crtc->index);
6ee73861
BS
628 drm_mode_debug_printmodeline(adjusted_mode);
629
630 /* unlock must come after turning off FP_TG_CONTROL in output_prepare */
631 nv_lock_vga_crtc_shadow(dev, nv_crtc->index, -1);
632
633 nv_crtc_mode_set_vga(crtc, adjusted_mode);
634 /* calculated in nv04_dfp_prepare, nv40 needs it written before calculating PLLs */
77145f1c 635 if (nv_device(drm->device)->card_type == NV_40)
017e6e29 636 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, nv04_display(dev)->mode_reg.sel_clk);
6ee73861
BS
637 nv_crtc_mode_set_regs(crtc, adjusted_mode);
638 nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->clock);
639 return 0;
640}
641
642static void nv_crtc_save(struct drm_crtc *crtc)
643{
644 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29
BS
645 struct drm_device *dev = crtc->dev;
646 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
6ee73861 647 struct nv04_crtc_reg *crtc_state = &state->crtc_reg[nv_crtc->index];
017e6e29 648 struct nv04_mode_state *saved = &nv04_display(dev)->saved_reg;
6ee73861
BS
649 struct nv04_crtc_reg *crtc_saved = &saved->crtc_reg[nv_crtc->index];
650
651 if (nv_two_heads(crtc->dev))
652 NVSetOwner(crtc->dev, nv_crtc->index);
653
654 nouveau_hw_save_state(crtc->dev, nv_crtc->index, saved);
655
656 /* init some state to saved value */
657 state->sel_clk = saved->sel_clk & ~(0x5 << 16);
658 crtc_state->CRTC[NV_CIO_CRE_LCD__INDEX] = crtc_saved->CRTC[NV_CIO_CRE_LCD__INDEX];
659 state->pllsel = saved->pllsel & ~(PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK);
660 crtc_state->gpio_ext = crtc_saved->gpio_ext;
661}
662
663static void nv_crtc_restore(struct drm_crtc *crtc)
664{
665 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
017e6e29 666 struct drm_device *dev = crtc->dev;
6ee73861 667 int head = nv_crtc->index;
017e6e29 668 uint8_t saved_cr21 = nv04_display(dev)->saved_reg.crtc_reg[head].CRTC[NV_CIO_CRE_21];
6ee73861
BS
669
670 if (nv_two_heads(crtc->dev))
671 NVSetOwner(crtc->dev, head);
672
017e6e29 673 nouveau_hw_load_state(crtc->dev, head, &nv04_display(dev)->saved_reg);
6ee73861
BS
674 nv_lock_vga_crtc_shadow(crtc->dev, head, saved_cr21);
675
676 nv_crtc->last_dpms = NV_DPMS_CLEARED;
677}
678
679static void nv_crtc_prepare(struct drm_crtc *crtc)
680{
681 struct drm_device *dev = crtc->dev;
77145f1c 682 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861
BS
683 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
684 struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
685
686 if (nv_two_heads(dev))
687 NVSetOwner(dev, nv_crtc->index);
688
1c180fa5 689 drm_vblank_pre_modeset(dev, nv_crtc->index);
6ee73861
BS
690 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
691
692 NVBlankScreen(dev, nv_crtc->index, true);
693
25985edc 694 /* Some more preparation. */
6ee73861 695 NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
77145f1c 696 if (nv_device(drm->device)->card_type == NV_40) {
6ee73861
BS
697 uint32_t reg900 = NVReadRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900);
698 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900, reg900 & ~0x10000);
699 }
700}
701
702static void nv_crtc_commit(struct drm_crtc *crtc)
703{
704 struct drm_device *dev = crtc->dev;
705 struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
6ee73861
BS
706 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
707
017e6e29 708 nouveau_hw_load_state(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
6ee73861
BS
709 nv04_crtc_mode_set_base(crtc, crtc->x, crtc->y, NULL);
710
711#ifdef __BIG_ENDIAN
712 /* turn on LFB swapping */
713 {
714 uint8_t tmp = NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR);
715 tmp |= MASK(NV_CIO_CRE_RCR_ENDIAN_BIG);
716 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR, tmp);
717 }
718#endif
719
720 funcs->dpms(crtc, DRM_MODE_DPMS_ON);
1c180fa5 721 drm_vblank_post_modeset(dev, nv_crtc->index);
6ee73861
BS
722}
723
724static void nv_crtc_destroy(struct drm_crtc *crtc)
725{
726 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
727
6ee73861
BS
728 if (!nv_crtc)
729 return;
730
731 drm_crtc_cleanup(crtc);
732
9d59e8a1 733 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
04c8c210 734 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
6ee73861
BS
735 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
736 kfree(nv_crtc);
737}
738
739static void
740nv_crtc_gamma_load(struct drm_crtc *crtc)
741{
742 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
743 struct drm_device *dev = nv_crtc->base.dev;
6ee73861
BS
744 struct rgb { uint8_t r, g, b; } __attribute__((packed)) *rgbs;
745 int i;
746
017e6e29 747 rgbs = (struct rgb *)nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index].DAC;
6ee73861
BS
748 for (i = 0; i < 256; i++) {
749 rgbs[i].r = nv_crtc->lut.r[i] >> 8;
750 rgbs[i].g = nv_crtc->lut.g[i] >> 8;
751 rgbs[i].b = nv_crtc->lut.b[i] >> 8;
752 }
753
017e6e29 754 nouveau_hw_load_state_palette(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
6ee73861
BS
755}
756
757static void
7203425a
JS
758nv_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b, uint32_t start,
759 uint32_t size)
6ee73861 760{
7203425a 761 int end = (start + size > 256) ? 256 : start + size, i;
6ee73861 762 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
6ee73861 763
7203425a 764 for (i = start; i < end; i++) {
6ee73861
BS
765 nv_crtc->lut.r[i] = r[i];
766 nv_crtc->lut.g[i] = g[i];
767 nv_crtc->lut.b[i] = b[i];
768 }
769
770 /* We need to know the depth before we upload, but it's possible to
771 * get called before a framebuffer is bound. If this is the case,
772 * mark the lut values as dirty by setting depth==0, and it'll be
773 * uploaded on the first mode_set_base()
774 */
775 if (!nv_crtc->base.fb) {
776 nv_crtc->lut.depth = 0;
777 return;
778 }
779
780 nv_crtc_gamma_load(crtc);
781}
782
783static int
be64c2bb
CB
784nv04_crtc_do_mode_set_base(struct drm_crtc *crtc,
785 struct drm_framebuffer *passed_fb,
786 int x, int y, bool atomic)
6ee73861
BS
787{
788 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
789 struct drm_device *dev = crtc->dev;
77145f1c 790 struct nouveau_drm *drm = nouveau_drm(dev);
017e6e29 791 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
0e83bb4e
EV
792 struct drm_framebuffer *drm_fb;
793 struct nouveau_framebuffer *fb;
6ee73861
BS
794 int arb_burst, arb_lwm;
795 int ret;
796
77145f1c 797 NV_DEBUG(drm, "index %d\n", nv_crtc->index);
0e83bb4e
EV
798
799 /* no fb bound */
800 if (!atomic && !crtc->fb) {
77145f1c 801 NV_DEBUG(drm, "No FB bound\n");
0e83bb4e
EV
802 return 0;
803 }
804
805
be64c2bb
CB
806 /* If atomic, we want to switch to the fb we were passed, so
807 * now we update pointers to do that. (We don't pin; just
808 * assume we're already pinned and update the base address.)
809 */
810 if (atomic) {
811 drm_fb = passed_fb;
812 fb = nouveau_framebuffer(passed_fb);
f9ec8f6c 813 } else {
0e83bb4e
EV
814 drm_fb = crtc->fb;
815 fb = nouveau_framebuffer(crtc->fb);
be64c2bb
CB
816 /* If not atomic, we can go ahead and pin, and unpin the
817 * old fb we were passed.
818 */
819 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
820 if (ret)
821 return ret;
6ee73861 822
be64c2bb
CB
823 if (passed_fb) {
824 struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
825 nouveau_bo_unpin(ofb->nvbo);
826 }
6ee73861
BS
827 }
828
829 nv_crtc->fb.offset = fb->nvbo->bo.offset;
830
831 if (nv_crtc->lut.depth != drm_fb->depth) {
832 nv_crtc->lut.depth = drm_fb->depth;
833 nv_crtc_gamma_load(crtc);
834 }
835
836 /* Update the framebuffer format. */
837 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] &= ~3;
838 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (crtc->fb->depth + 1) / 8;
839 regp->ramdac_gen_ctrl &= ~NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
840 if (crtc->fb->depth == 16)
841 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
842 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_PIXEL_INDEX);
843 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_GENERAL_CONTROL,
844 regp->ramdac_gen_ctrl);
845
01f2c773 846 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = drm_fb->pitches[0] >> 3;
6ee73861 847 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
01f2c773 848 XLATE(drm_fb->pitches[0] >> 3, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
c1003d9c 849 regp->CRTC[NV_CIO_CRE_42] =
01f2c773 850 XLATE(drm_fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
6ee73861
BS
851 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_RPC0_INDEX);
852 crtc_wr_cio_state(crtc, regp, NV_CIO_CR_OFFSET_INDEX);
c1003d9c 853 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_42);
6ee73861
BS
854
855 /* Update the framebuffer location. */
856 regp->fb_start = nv_crtc->fb.offset & ~3;
01f2c773 857 regp->fb_start += (y * drm_fb->pitches[0]) + (x * drm_fb->bits_per_pixel / 8);
5794b5fd 858 nv_set_crtc_base(dev, nv_crtc->index, regp->fb_start);
6ee73861
BS
859
860 /* Update the arbitration parameters. */
861 nouveau_calc_arb(dev, crtc->mode.clock, drm_fb->bits_per_pixel,
862 &arb_burst, &arb_lwm);
863
864 regp->CRTC[NV_CIO_CRE_FF_INDEX] = arb_burst;
865 regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arb_lwm & 0xff;
866 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FF_INDEX);
867 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FFLWM__INDEX);
868
77145f1c 869 if (nv_device(drm->device)->card_type >= NV_20) {
6ee73861
BS
870 regp->CRTC[NV_CIO_CRE_47] = arb_lwm >> 8;
871 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_47);
872 }
873
874 return 0;
875}
876
be64c2bb
CB
877static int
878nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
879 struct drm_framebuffer *old_fb)
880{
881 return nv04_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
882}
883
884static int
885nv04_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
886 struct drm_framebuffer *fb,
21c74a8e 887 int x, int y, enum mode_set_atomic state)
be64c2bb 888{
77145f1c
BS
889 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
890 struct drm_device *dev = drm->dev;
a424d761 891
21c74a8e 892 if (state == ENTER_ATOMIC_MODE_SET)
a424d761
CB
893 nouveau_fbcon_save_disable_accel(dev);
894 else
895 nouveau_fbcon_restore_accel(dev);
896
be64c2bb
CB
897 return nv04_crtc_do_mode_set_base(crtc, fb, x, y, true);
898}
899
6ee73861
BS
900static void nv04_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
901 struct nouveau_bo *dst)
902{
903 int width = nv_cursor_width(dev);
904 uint32_t pixel;
905 int i, j;
906
907 for (i = 0; i < width; i++) {
908 for (j = 0; j < width; j++) {
909 pixel = nouveau_bo_rd32(src, i*64 + j);
910
911 nouveau_bo_wr16(dst, i*width + j, (pixel & 0x80000000) >> 16
912 | (pixel & 0xf80000) >> 9
913 | (pixel & 0xf800) >> 6
914 | (pixel & 0xf8) >> 3);
915 }
916 }
917}
918
919static void nv11_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
920 struct nouveau_bo *dst)
921{
922 uint32_t pixel;
923 int alpha, i;
924
925 /* nv11+ supports premultiplied (PM), or non-premultiplied (NPM) alpha
926 * cursors (though NPM in combination with fp dithering may not work on
927 * nv11, from "nv" driver history)
928 * NPM mode needs NV_PCRTC_CURSOR_CONFIG_ALPHA_BLEND set and is what the
929 * blob uses, however we get given PM cursors so we use PM mode
930 */
931 for (i = 0; i < 64 * 64; i++) {
932 pixel = nouveau_bo_rd32(src, i);
933
934 /* hw gets unhappy if alpha <= rgb values. for a PM image "less
935 * than" shouldn't happen; fix "equal to" case by adding one to
936 * alpha channel (slightly inaccurate, but so is attempting to
937 * get back to NPM images, due to limits of integer precision)
938 */
939 alpha = pixel >> 24;
940 if (alpha > 0 && alpha < 255)
941 pixel = (pixel & 0x00ffffff) | ((alpha + 1) << 24);
942
943#ifdef __BIG_ENDIAN
944 {
77145f1c 945 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 946
77145f1c 947 if (nv_device(drm->device)->chipset == 0x11) {
6ee73861
BS
948 pixel = ((pixel & 0x000000ff) << 24) |
949 ((pixel & 0x0000ff00) << 8) |
950 ((pixel & 0x00ff0000) >> 8) |
951 ((pixel & 0xff000000) >> 24);
952 }
953 }
954#endif
955
956 nouveau_bo_wr32(dst, i, pixel);
957 }
958}
959
960static int
961nv04_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
962 uint32_t buffer_handle, uint32_t width, uint32_t height)
963{
77145f1c
BS
964 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
965 struct drm_device *dev = drm->dev;
6ee73861
BS
966 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
967 struct nouveau_bo *cursor = NULL;
968 struct drm_gem_object *gem;
969 int ret = 0;
970
6ee73861
BS
971 if (!buffer_handle) {
972 nv_crtc->cursor.hide(nv_crtc, true);
973 return 0;
974 }
975
b4fa9d0f
MS
976 if (width != 64 || height != 64)
977 return -EINVAL;
978
6ee73861
BS
979 gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
980 if (!gem)
bf79cb91 981 return -ENOENT;
6ee73861
BS
982 cursor = nouveau_gem_object(gem);
983
984 ret = nouveau_bo_map(cursor);
985 if (ret)
986 goto out;
987
77145f1c 988 if (nv_device(drm->device)->chipset >= 0x11)
6ee73861
BS
989 nv11_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
990 else
991 nv04_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
992
993 nouveau_bo_unmap(cursor);
994 nv_crtc->cursor.offset = nv_crtc->cursor.nvbo->bo.offset;
995 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
996 nv_crtc->cursor.show(nv_crtc, true);
997out:
bc9025bd 998 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
999 return ret;
1000}
1001
1002static int
1003nv04_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1004{
1005 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1006
1007 nv_crtc->cursor.set_pos(nv_crtc, x, y);
1008 return 0;
1009}
1010
5addcf0a
DA
1011int
1012nouveau_crtc_set_config(struct drm_mode_set *set)
1013{
1014 struct drm_device *dev;
1015 struct nouveau_drm *drm;
1016 int ret;
1017 struct drm_crtc *crtc;
1018 bool active = false;
1019 if (!set || !set->crtc)
1020 return -EINVAL;
1021
1022 dev = set->crtc->dev;
1023
1024 /* get a pm reference here */
1025 ret = pm_runtime_get_sync(dev->dev);
1026 if (ret < 0)
1027 return ret;
1028
1029 ret = drm_crtc_helper_set_config(set);
1030
1031 drm = nouveau_drm(dev);
1032
1033 /* if we get here with no crtcs active then we can drop a reference */
1034 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1035 if (crtc->enabled)
1036 active = true;
1037 }
1038
1039 pm_runtime_mark_last_busy(dev->dev);
1040 /* if we have active crtcs and we don't have a power ref,
1041 take the current one */
1042 if (active && !drm->have_disp_power_ref) {
1043 drm->have_disp_power_ref = true;
1044 return ret;
1045 }
1046 /* if we have no active crtcs, then drop the power ref
1047 we got before */
1048 if (!active && drm->have_disp_power_ref) {
1049 pm_runtime_put_autosuspend(dev->dev);
1050 drm->have_disp_power_ref = false;
1051 }
1052 /* drop the power reference we got coming in here */
1053 pm_runtime_put_autosuspend(dev->dev);
1054 return ret;
1055}
1056
6ee73861
BS
1057static const struct drm_crtc_funcs nv04_crtc_funcs = {
1058 .save = nv_crtc_save,
1059 .restore = nv_crtc_restore,
1060 .cursor_set = nv04_crtc_cursor_set,
1061 .cursor_move = nv04_crtc_cursor_move,
1062 .gamma_set = nv_crtc_gamma_set,
5addcf0a 1063 .set_config = nouveau_crtc_set_config,
332b242f 1064 .page_flip = nouveau_crtc_page_flip,
6ee73861
BS
1065 .destroy = nv_crtc_destroy,
1066};
1067
1068static const struct drm_crtc_helper_funcs nv04_crtc_helper_funcs = {
1069 .dpms = nv_crtc_dpms,
1070 .prepare = nv_crtc_prepare,
1071 .commit = nv_crtc_commit,
1072 .mode_fixup = nv_crtc_mode_fixup,
1073 .mode_set = nv_crtc_mode_set,
1074 .mode_set_base = nv04_crtc_mode_set_base,
be64c2bb 1075 .mode_set_base_atomic = nv04_crtc_mode_set_base_atomic,
6ee73861
BS
1076 .load_lut = nv_crtc_gamma_load,
1077};
1078
1079int
1080nv04_crtc_create(struct drm_device *dev, int crtc_num)
1081{
1082 struct nouveau_crtc *nv_crtc;
1083 int ret, i;
1084
1085 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
1086 if (!nv_crtc)
1087 return -ENOMEM;
1088
1089 for (i = 0; i < 256; i++) {
1090 nv_crtc->lut.r[i] = i << 8;
1091 nv_crtc->lut.g[i] = i << 8;
1092 nv_crtc->lut.b[i] = i << 8;
1093 }
1094 nv_crtc->lut.depth = 0;
1095
1096 nv_crtc->index = crtc_num;
1097 nv_crtc->last_dpms = NV_DPMS_CLEARED;
1098
1099 drm_crtc_init(dev, &nv_crtc->base, &nv04_crtc_funcs);
1100 drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
1101 drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
1102
7375c95b 1103 ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
22b33e8e 1104 0, 0x0000, NULL, &nv_crtc->cursor.nvbo);
6ee73861
BS
1105 if (!ret) {
1106 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
04c8c210 1107 if (!ret) {
6ee73861 1108 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
04c8c210
MS
1109 if (ret)
1110 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1111 }
6ee73861
BS
1112 if (ret)
1113 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1114 }
1115
1116 nv04_cursor_init(nv_crtc);
1117
1118 return 0;
1119}
This page took 0.294045 seconds and 5 git commands to generate.