blob: f5d17bf954ff893290b537ad7dc9b8ae7edc40e3 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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 "drmP.h"
27#include "drm_crtc_helper.h"
28
29#include "nouveau_drv.h"
30#include "nouveau_encoder.h"
31#include "nouveau_connector.h"
32#include "nouveau_crtc.h"
33#include "nouveau_fb.h"
34#include "nouveau_hw.h"
35#include "nvreg.h"
Chris Balla424d762010-09-26 06:47:27 -050036#include "nouveau_fbcon.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037
38static int
39nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
40 struct drm_framebuffer *old_fb);
41
42static void
43crtc_wr_cio_state(struct drm_crtc *crtc, struct nv04_crtc_reg *crtcstate, int index)
44{
45 NVWriteVgaCrtc(crtc->dev, nouveau_crtc(crtc)->index, index,
46 crtcstate->CRTC[index]);
47}
48
49static void nv_crtc_set_digital_vibrance(struct drm_crtc *crtc, int level)
50{
51 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +100052 struct drm_device *dev = crtc->dev;
53 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +100054
55 regp->CRTC[NV_CIO_CRE_CSB] = nv_crtc->saturation = level;
56 if (nv_crtc->saturation && nv_gf4_disp_arch(crtc->dev)) {
57 regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
58 regp->CRTC[NV_CIO_CRE_5B] = nv_crtc->saturation << 2;
59 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_5B);
60 }
61 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_CSB);
62}
63
64static void nv_crtc_set_image_sharpening(struct drm_crtc *crtc, int level)
65{
66 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +100067 struct drm_device *dev = crtc->dev;
68 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +100069
70 nv_crtc->sharpness = level;
71 if (level < 0) /* blur is in hw range 0x3f -> 0x20 */
72 level += 0x40;
73 regp->ramdac_634 = level;
74 NVWriteRAMDAC(crtc->dev, nv_crtc->index, NV_PRAMDAC_634, regp->ramdac_634);
75}
76
77#define PLLSEL_VPLL1_MASK \
78 (NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_VPLL \
79 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK_RATIO_DB2)
80#define PLLSEL_VPLL2_MASK \
81 (NV_PRAMDAC_PLL_COEFF_SELECT_PLL_SOURCE_VPLL2 \
82 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK2_RATIO_DB2)
83#define PLLSEL_TV_MASK \
84 (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
85 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1 \
86 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
87 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
88
89/* NV4x 0x40.. pll notes:
90 * gpu pll: 0x4000 + 0x4004
91 * ?gpu? pll: 0x4008 + 0x400c
92 * vpll1: 0x4010 + 0x4014
93 * vpll2: 0x4018 + 0x401c
94 * mpll: 0x4020 + 0x4024
95 * mpll: 0x4038 + 0x403c
96 *
97 * the first register of each pair has some unknown details:
98 * bits 0-7: redirected values from elsewhere? (similar to PLL_SETUP_CONTROL?)
99 * bits 20-23: (mpll) something to do with post divider?
100 * bits 28-31: related to single stage mode? (bit 8/12)
101 */
102
103static void nv_crtc_calc_state_ext(struct drm_crtc *crtc, struct drm_display_mode * mode, int dot_clock)
104{
105 struct drm_device *dev = crtc->dev;
106 struct drm_nouveau_private *dev_priv = dev->dev_private;
107 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000108 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000109 struct nv04_crtc_reg *regp = &state->crtc_reg[nv_crtc->index];
110 struct nouveau_pll_vals *pv = &regp->pllvals;
Ben Skeggs70790f42012-07-10 17:26:46 +1000111 struct nvbios_pll pll_lim;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000112
Ben Skeggs855a95e2010-09-16 15:25:25 +1000113 if (get_pll_limits(dev, nv_crtc->index ? PLL_VPLL1 : PLL_VPLL0, &pll_lim))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114 return;
115
116 /* NM2 == 0 is used to determine single stage mode on two stage plls */
117 pv->NM2 = 0;
118
119 /* for newer nv4x the blob uses only the first stage of the vpll below a
120 * certain clock. for a certain nv4b this is 150MHz. since the max
121 * output frequency of the first stage for this card is 300MHz, it is
122 * assumed the threshold is given by vco1 maxfreq/2
123 */
124 /* for early nv4x, specifically nv40 and *some* nv43 (devids 0 and 6,
125 * not 8, others unknown), the blob always uses both plls. no problem
126 * has yet been observed in allowing the use a single stage pll on all
127 * nv43 however. the behaviour of single stage use is untested on nv40
128 */
Ben Skeggs70790f42012-07-10 17:26:46 +1000129 if (dev_priv->chipset > 0x40 && dot_clock <= (pll_lim.vco1.max_freq / 2))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000130 memset(&pll_lim.vco2, 0, sizeof(pll_lim.vco2));
131
132 if (!nouveau_calc_pll_mnp(dev, &pll_lim, dot_clock, pv))
133 return;
134
135 state->pllsel &= PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK;
136
137 /* The blob uses this always, so let's do the same */
138 if (dev_priv->card_type == NV_40)
139 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_USE_VPLL2_TRUE;
140 /* again nv40 and some nv43 act more like nv3x as described above */
141 if (dev_priv->chipset < 0x41)
142 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_MPLL |
143 NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_NVPLL;
144 state->pllsel |= nv_crtc->index ? PLLSEL_VPLL2_MASK : PLLSEL_VPLL1_MASK;
145
146 if (pv->NM2)
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100147 NV_DEBUG_KMS(dev, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 pv->N1, pv->N2, pv->M1, pv->M2, pv->log2P);
149 else
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100150 NV_DEBUG_KMS(dev, "vpll: n %d m %d log2p %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151 pv->N1, pv->M1, pv->log2P);
152
153 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
154}
155
156static void
157nv_crtc_dpms(struct drm_crtc *crtc, int mode)
158{
159 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
160 struct drm_device *dev = crtc->dev;
161 unsigned char seq1 = 0, crtc17 = 0;
162 unsigned char crtc1A;
163
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100164 NV_DEBUG_KMS(dev, "Setting dpms mode %d on CRTC %d\n", mode,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000165 nv_crtc->index);
166
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300167 if (nv_crtc->last_dpms == mode) /* Don't do unnecessary mode changes. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168 return;
169
170 nv_crtc->last_dpms = mode;
171
172 if (nv_two_heads(dev))
173 NVSetOwner(dev, nv_crtc->index);
174
175 /* nv4ref indicates these two RPC1 bits inhibit h/v sync */
176 crtc1A = NVReadVgaCrtc(dev, nv_crtc->index,
177 NV_CIO_CRE_RPC1_INDEX) & ~0xC0;
178 switch (mode) {
179 case DRM_MODE_DPMS_STANDBY:
180 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
181 seq1 = 0x20;
182 crtc17 = 0x80;
183 crtc1A |= 0x80;
184 break;
185 case DRM_MODE_DPMS_SUSPEND:
186 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
187 seq1 = 0x20;
188 crtc17 = 0x80;
189 crtc1A |= 0x40;
190 break;
191 case DRM_MODE_DPMS_OFF:
192 /* Screen: Off; HSync: Off, VSync: Off */
193 seq1 = 0x20;
194 crtc17 = 0x00;
195 crtc1A |= 0xC0;
196 break;
197 case DRM_MODE_DPMS_ON:
198 default:
199 /* Screen: On; HSync: On, VSync: On */
200 seq1 = 0x00;
201 crtc17 = 0x80;
202 break;
203 }
204
205 NVVgaSeqReset(dev, nv_crtc->index, true);
206 /* Each head has it's own sequencer, so we can turn it off when we want */
207 seq1 |= (NVReadVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX) & ~0x20);
208 NVWriteVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX, seq1);
209 crtc17 |= (NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX) & ~0x80);
210 mdelay(10);
211 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX, crtc17);
212 NVVgaSeqReset(dev, nv_crtc->index, false);
213
214 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RPC1_INDEX, crtc1A);
215}
216
217static bool
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200218nv_crtc_mode_fixup(struct drm_crtc *crtc, const struct drm_display_mode *mode,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219 struct drm_display_mode *adjusted_mode)
220{
221 return true;
222}
223
224static void
225nv_crtc_mode_set_vga(struct drm_crtc *crtc, struct drm_display_mode *mode)
226{
227 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000229 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000230 struct drm_framebuffer *fb = crtc->fb;
231
232 /* Calculate our timings */
Francisco Jereze5ec8822010-03-05 15:15:39 +0100233 int horizDisplay = (mode->crtc_hdisplay >> 3) - 1;
234 int horizStart = (mode->crtc_hsync_start >> 3) + 1;
235 int horizEnd = (mode->crtc_hsync_end >> 3) + 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000236 int horizTotal = (mode->crtc_htotal >> 3) - 5;
237 int horizBlankStart = (mode->crtc_hdisplay >> 3) - 1;
238 int horizBlankEnd = (mode->crtc_htotal >> 3) - 1;
239 int vertDisplay = mode->crtc_vdisplay - 1;
240 int vertStart = mode->crtc_vsync_start - 1;
241 int vertEnd = mode->crtc_vsync_end - 1;
242 int vertTotal = mode->crtc_vtotal - 2;
243 int vertBlankStart = mode->crtc_vdisplay - 1;
244 int vertBlankEnd = mode->crtc_vtotal - 1;
245
246 struct drm_encoder *encoder;
247 bool fp_output = false;
248
249 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
250 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
251
252 if (encoder->crtc == crtc &&
Ben Skeggscb75d972012-07-11 10:44:20 +1000253 (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
254 nv_encoder->dcb->type == DCB_OUTPUT_TMDS))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000255 fp_output = true;
256 }
257
258 if (fp_output) {
259 vertStart = vertTotal - 3;
260 vertEnd = vertTotal - 2;
261 vertBlankStart = vertStart;
262 horizStart = horizTotal - 5;
263 horizEnd = horizTotal - 2;
264 horizBlankEnd = horizTotal + 4;
265#if 0
266 if (dev->overlayAdaptor && dev_priv->card_type >= NV_10)
267 /* This reportedly works around some video overlay bandwidth problems */
268 horizTotal += 2;
269#endif
270 }
271
272 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
273 vertTotal |= 1;
274
275#if 0
276 ErrorF("horizDisplay: 0x%X \n", horizDisplay);
277 ErrorF("horizStart: 0x%X \n", horizStart);
278 ErrorF("horizEnd: 0x%X \n", horizEnd);
279 ErrorF("horizTotal: 0x%X \n", horizTotal);
280 ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
281 ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
282 ErrorF("vertDisplay: 0x%X \n", vertDisplay);
283 ErrorF("vertStart: 0x%X \n", vertStart);
284 ErrorF("vertEnd: 0x%X \n", vertEnd);
285 ErrorF("vertTotal: 0x%X \n", vertTotal);
286 ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
287 ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
288#endif
289
290 /*
291 * compute correct Hsync & Vsync polarity
292 */
293 if ((mode->flags & (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC))
294 && (mode->flags & (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC))) {
295
296 regp->MiscOutReg = 0x23;
297 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
298 regp->MiscOutReg |= 0x40;
299 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
300 regp->MiscOutReg |= 0x80;
301 } else {
302 int vdisplay = mode->vdisplay;
303 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
304 vdisplay *= 2;
305 if (mode->vscan > 1)
306 vdisplay *= mode->vscan;
307 if (vdisplay < 400)
308 regp->MiscOutReg = 0xA3; /* +hsync -vsync */
309 else if (vdisplay < 480)
310 regp->MiscOutReg = 0x63; /* -hsync +vsync */
311 else if (vdisplay < 768)
312 regp->MiscOutReg = 0xE3; /* -hsync -vsync */
313 else
314 regp->MiscOutReg = 0x23; /* +hsync +vsync */
315 }
316
317 regp->MiscOutReg |= (mode->clock_index & 0x03) << 2;
318
319 /*
320 * Time Sequencer
321 */
322 regp->Sequencer[NV_VIO_SR_RESET_INDEX] = 0x00;
323 /* 0x20 disables the sequencer */
324 if (mode->flags & DRM_MODE_FLAG_CLKDIV2)
325 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x29;
326 else
327 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x21;
328 regp->Sequencer[NV_VIO_SR_PLANE_MASK_INDEX] = 0x0F;
329 regp->Sequencer[NV_VIO_SR_CHAR_MAP_INDEX] = 0x00;
330 regp->Sequencer[NV_VIO_SR_MEM_MODE_INDEX] = 0x0E;
331
332 /*
333 * CRTC
334 */
335 regp->CRTC[NV_CIO_CR_HDT_INDEX] = horizTotal;
336 regp->CRTC[NV_CIO_CR_HDE_INDEX] = horizDisplay;
337 regp->CRTC[NV_CIO_CR_HBS_INDEX] = horizBlankStart;
338 regp->CRTC[NV_CIO_CR_HBE_INDEX] = (1 << 7) |
339 XLATE(horizBlankEnd, 0, NV_CIO_CR_HBE_4_0);
340 regp->CRTC[NV_CIO_CR_HRS_INDEX] = horizStart;
341 regp->CRTC[NV_CIO_CR_HRE_INDEX] = XLATE(horizBlankEnd, 5, NV_CIO_CR_HRE_HBE_5) |
342 XLATE(horizEnd, 0, NV_CIO_CR_HRE_4_0);
343 regp->CRTC[NV_CIO_CR_VDT_INDEX] = vertTotal;
344 regp->CRTC[NV_CIO_CR_OVL_INDEX] = XLATE(vertStart, 9, NV_CIO_CR_OVL_VRS_9) |
345 XLATE(vertDisplay, 9, NV_CIO_CR_OVL_VDE_9) |
346 XLATE(vertTotal, 9, NV_CIO_CR_OVL_VDT_9) |
347 (1 << 4) |
348 XLATE(vertBlankStart, 8, NV_CIO_CR_OVL_VBS_8) |
349 XLATE(vertStart, 8, NV_CIO_CR_OVL_VRS_8) |
350 XLATE(vertDisplay, 8, NV_CIO_CR_OVL_VDE_8) |
351 XLATE(vertTotal, 8, NV_CIO_CR_OVL_VDT_8);
352 regp->CRTC[NV_CIO_CR_RSAL_INDEX] = 0x00;
353 regp->CRTC[NV_CIO_CR_CELL_HT_INDEX] = ((mode->flags & DRM_MODE_FLAG_DBLSCAN) ? MASK(NV_CIO_CR_CELL_HT_SCANDBL) : 0) |
354 1 << 6 |
355 XLATE(vertBlankStart, 9, NV_CIO_CR_CELL_HT_VBS_9);
356 regp->CRTC[NV_CIO_CR_CURS_ST_INDEX] = 0x00;
357 regp->CRTC[NV_CIO_CR_CURS_END_INDEX] = 0x00;
358 regp->CRTC[NV_CIO_CR_SA_HI_INDEX] = 0x00;
359 regp->CRTC[NV_CIO_CR_SA_LO_INDEX] = 0x00;
360 regp->CRTC[NV_CIO_CR_TCOFF_HI_INDEX] = 0x00;
361 regp->CRTC[NV_CIO_CR_TCOFF_LO_INDEX] = 0x00;
362 regp->CRTC[NV_CIO_CR_VRS_INDEX] = vertStart;
363 regp->CRTC[NV_CIO_CR_VRE_INDEX] = 1 << 5 | XLATE(vertEnd, 0, NV_CIO_CR_VRE_3_0);
364 regp->CRTC[NV_CIO_CR_VDE_INDEX] = vertDisplay;
365 /* framebuffer can be larger than crtc scanout area. */
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200366 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = fb->pitches[0] / 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000367 regp->CRTC[NV_CIO_CR_ULINE_INDEX] = 0x00;
368 regp->CRTC[NV_CIO_CR_VBS_INDEX] = vertBlankStart;
369 regp->CRTC[NV_CIO_CR_VBE_INDEX] = vertBlankEnd;
370 regp->CRTC[NV_CIO_CR_MODE_INDEX] = 0x43;
371 regp->CRTC[NV_CIO_CR_LCOMP_INDEX] = 0xff;
372
373 /*
374 * Some extended CRTC registers (they are not saved with the rest of the vga regs).
375 */
376
377 /* framebuffer can be larger than crtc scanout area. */
Francisco Jerezc1003d92011-05-24 15:57:14 +0200378 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200379 XLATE(fb->pitches[0] / 8, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200380 regp->CRTC[NV_CIO_CRE_42] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200381 XLATE(fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000382 regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->crtc_hdisplay < 1280 ?
383 MASK(NV_CIO_CRE_RPC1_LARGE) : 0x00;
384 regp->CRTC[NV_CIO_CRE_LSR_INDEX] = XLATE(horizBlankEnd, 6, NV_CIO_CRE_LSR_HBE_6) |
385 XLATE(vertBlankStart, 10, NV_CIO_CRE_LSR_VBS_10) |
386 XLATE(vertStart, 10, NV_CIO_CRE_LSR_VRS_10) |
387 XLATE(vertDisplay, 10, NV_CIO_CRE_LSR_VDE_10) |
388 XLATE(vertTotal, 10, NV_CIO_CRE_LSR_VDT_10);
389 regp->CRTC[NV_CIO_CRE_HEB__INDEX] = XLATE(horizStart, 8, NV_CIO_CRE_HEB_HRS_8) |
390 XLATE(horizBlankStart, 8, NV_CIO_CRE_HEB_HBS_8) |
391 XLATE(horizDisplay, 8, NV_CIO_CRE_HEB_HDE_8) |
392 XLATE(horizTotal, 8, NV_CIO_CRE_HEB_HDT_8);
393 regp->CRTC[NV_CIO_CRE_EBR_INDEX] = XLATE(vertBlankStart, 11, NV_CIO_CRE_EBR_VBS_11) |
394 XLATE(vertStart, 11, NV_CIO_CRE_EBR_VRS_11) |
395 XLATE(vertDisplay, 11, NV_CIO_CRE_EBR_VDE_11) |
396 XLATE(vertTotal, 11, NV_CIO_CRE_EBR_VDT_11);
397
398 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
399 horizTotal = (horizTotal >> 1) & ~1;
400 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = horizTotal;
401 regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= XLATE(horizTotal, 8, NV_CIO_CRE_HEB_ILC_8);
402 } else
403 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff; /* interlace off */
404
405 /*
406 * Graphics Display Controller
407 */
408 regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
409 regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
410 regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
411 regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
412 regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
413 regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
414 regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
415 regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
416 regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
417
418 regp->Attribute[0] = 0x00; /* standard colormap translation */
419 regp->Attribute[1] = 0x01;
420 regp->Attribute[2] = 0x02;
421 regp->Attribute[3] = 0x03;
422 regp->Attribute[4] = 0x04;
423 regp->Attribute[5] = 0x05;
424 regp->Attribute[6] = 0x06;
425 regp->Attribute[7] = 0x07;
426 regp->Attribute[8] = 0x08;
427 regp->Attribute[9] = 0x09;
428 regp->Attribute[10] = 0x0A;
429 regp->Attribute[11] = 0x0B;
430 regp->Attribute[12] = 0x0C;
431 regp->Attribute[13] = 0x0D;
432 regp->Attribute[14] = 0x0E;
433 regp->Attribute[15] = 0x0F;
434 regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
435 /* Non-vga */
436 regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
437 regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
438 regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
439 regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
440}
441
442/**
443 * Sets up registers for the given mode/adjusted_mode pair.
444 *
445 * The clocks, CRTCs and outputs attached to this CRTC must be off.
446 *
447 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
448 * be easily turned on/off after this.
449 */
450static void
451nv_crtc_mode_set_regs(struct drm_crtc *crtc, struct drm_display_mode * mode)
452{
453 struct drm_device *dev = crtc->dev;
454 struct drm_nouveau_private *dev_priv = dev->dev_private;
455 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000456 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
457 struct nv04_crtc_reg *savep = &nv04_display(dev)->saved_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458 struct drm_encoder *encoder;
459 bool lvds_output = false, tmds_output = false, tv_output = false,
460 off_chip_digital = false;
461
462 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
463 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
464 bool digital = false;
465
466 if (encoder->crtc != crtc)
467 continue;
468
Ben Skeggscb75d972012-07-11 10:44:20 +1000469 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000470 digital = lvds_output = true;
Ben Skeggscb75d972012-07-11 10:44:20 +1000471 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472 tv_output = true;
Ben Skeggscb75d972012-07-11 10:44:20 +1000473 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000474 digital = tmds_output = true;
475 if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP && digital)
476 off_chip_digital = true;
477 }
478
479 /* Registers not directly related to the (s)vga mode */
480
481 /* What is the meaning of this register? */
482 /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
483 regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
484
485 regp->crtc_eng_ctrl = 0;
486 /* Except for rare conditions I2C is enabled on the primary crtc */
487 if (nv_crtc->index == 0)
488 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_I2C;
489#if 0
490 /* Set overlay to desired crtc. */
491 if (dev->overlayAdaptor) {
492 NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(dev);
493 if (pPriv->overlayCRTC == nv_crtc->index)
494 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_OVERLAY;
495 }
496#endif
497
498 /* ADDRESS_SPACE_PNVM is the same as setting HCUR_ASI */
499 regp->cursor_cfg = NV_PCRTC_CURSOR_CONFIG_CUR_LINES_64 |
500 NV_PCRTC_CURSOR_CONFIG_CUR_PIXELS_64 |
501 NV_PCRTC_CURSOR_CONFIG_ADDRESS_SPACE_PNVM;
502 if (dev_priv->chipset >= 0x11)
503 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_CUR_BPP_32;
504 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
505 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_DOUBLE_SCAN_ENABLE;
506
507 /* Unblock some timings */
508 regp->CRTC[NV_CIO_CRE_53] = 0;
509 regp->CRTC[NV_CIO_CRE_54] = 0;
510
511 /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
512 if (lvds_output)
513 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
514 else if (tmds_output)
515 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
516 else
517 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
518
519 /* These values seem to vary */
520 /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
521 regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
522
523 nv_crtc_set_digital_vibrance(crtc, nv_crtc->saturation);
524
525 /* probably a scratch reg, but kept for cargo-cult purposes:
526 * bit0: crtc0?, head A
527 * bit6: lvds, head A
528 * bit7: (only in X), head A
529 */
530 if (nv_crtc->index == 0)
531 regp->CRTC[NV_CIO_CRE_4B] = savep->CRTC[NV_CIO_CRE_4B] | 0x80;
532
533 /* The blob seems to take the current value from crtc 0, add 4 to that
534 * and reuse the old value for crtc 1 */
Ben Skeggs017e6e22012-07-18 10:00:50 +1000535 regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] = nv04_display(dev)->saved_reg.crtc_reg[0].CRTC[NV_CIO_CRE_TVOUT_LATENCY];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000536 if (!nv_crtc->index)
537 regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] += 4;
538
539 /* the blob sometimes sets |= 0x10 (which is the same as setting |=
540 * 1 << 30 on 0x60.830), for no apparent reason */
541 regp->CRTC[NV_CIO_CRE_59] = off_chip_digital;
542
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200543 if (dev_priv->card_type >= NV_30)
544 regp->CRTC[0x9f] = off_chip_digital ? 0x11 : 0x1;
545
Ben Skeggs6ee73862009-12-11 19:24:15 +1000546 regp->crtc_830 = mode->crtc_vdisplay - 3;
547 regp->crtc_834 = mode->crtc_vdisplay - 1;
548
549 if (dev_priv->card_type == NV_40)
550 /* This is what the blob does */
551 regp->crtc_850 = NVReadCRTC(dev, 0, NV_PCRTC_850);
552
553 if (dev_priv->card_type >= NV_30)
554 regp->gpio_ext = NVReadCRTC(dev, 0, NV_PCRTC_GPIO_EXT);
555
Francisco Jerez63f7fcf2010-10-22 04:31:02 +0200556 if (dev_priv->card_type >= NV_10)
557 regp->crtc_cfg = NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC;
558 else
559 regp->crtc_cfg = NV04_PCRTC_CONFIG_START_ADDRESS_HSYNC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560
561 /* Some misc regs */
562 if (dev_priv->card_type == NV_40) {
563 regp->CRTC[NV_CIO_CRE_85] = 0xFF;
564 regp->CRTC[NV_CIO_CRE_86] = 0x1;
565 }
566
567 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (crtc->fb->depth + 1) / 8;
568 /* Enable slaved mode (called MODE_TV in nv4ref.h) */
569 if (lvds_output || tmds_output || tv_output)
570 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
571
572 /* Generic PRAMDAC regs */
573
574 if (dev_priv->card_type >= NV_10)
575 /* Only bit that bios and blob set. */
576 regp->nv10_cursync = (1 << 25);
577
578 regp->ramdac_gen_ctrl = NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
579 NV_PRAMDAC_GENERAL_CONTROL_VGA_STATE_SEL |
580 NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON;
581 if (crtc->fb->depth == 16)
582 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
583 if (dev_priv->chipset >= 0x11)
584 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_PIPE_LONG;
585
586 regp->ramdac_630 = 0; /* turn off green mode (tv test pattern?) */
587 regp->tv_setup = 0;
588
589 nv_crtc_set_image_sharpening(crtc, nv_crtc->sharpness);
590
591 /* Some values the blob sets */
592 regp->ramdac_8c0 = 0x100;
593 regp->ramdac_a20 = 0x0;
594 regp->ramdac_a24 = 0xfffff;
595 regp->ramdac_a34 = 0x1;
596}
597
598/**
599 * Sets up registers for the given mode/adjusted_mode pair.
600 *
601 * The clocks, CRTCs and outputs attached to this CRTC must be off.
602 *
603 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
604 * be easily turned on/off after this.
605 */
606static int
607nv_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
608 struct drm_display_mode *adjusted_mode,
609 int x, int y, struct drm_framebuffer *old_fb)
610{
611 struct drm_device *dev = crtc->dev;
612 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
613 struct drm_nouveau_private *dev_priv = dev->dev_private;
614
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100615 NV_DEBUG_KMS(dev, "CTRC mode on CRTC %d:\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000616 drm_mode_debug_printmodeline(adjusted_mode);
617
618 /* unlock must come after turning off FP_TG_CONTROL in output_prepare */
619 nv_lock_vga_crtc_shadow(dev, nv_crtc->index, -1);
620
621 nv_crtc_mode_set_vga(crtc, adjusted_mode);
622 /* calculated in nv04_dfp_prepare, nv40 needs it written before calculating PLLs */
623 if (dev_priv->card_type == NV_40)
Ben Skeggs017e6e22012-07-18 10:00:50 +1000624 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, nv04_display(dev)->mode_reg.sel_clk);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000625 nv_crtc_mode_set_regs(crtc, adjusted_mode);
626 nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->clock);
627 return 0;
628}
629
630static void nv_crtc_save(struct drm_crtc *crtc)
631{
632 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000633 struct drm_device *dev = crtc->dev;
634 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000635 struct nv04_crtc_reg *crtc_state = &state->crtc_reg[nv_crtc->index];
Ben Skeggs017e6e22012-07-18 10:00:50 +1000636 struct nv04_mode_state *saved = &nv04_display(dev)->saved_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000637 struct nv04_crtc_reg *crtc_saved = &saved->crtc_reg[nv_crtc->index];
638
639 if (nv_two_heads(crtc->dev))
640 NVSetOwner(crtc->dev, nv_crtc->index);
641
642 nouveau_hw_save_state(crtc->dev, nv_crtc->index, saved);
643
644 /* init some state to saved value */
645 state->sel_clk = saved->sel_clk & ~(0x5 << 16);
646 crtc_state->CRTC[NV_CIO_CRE_LCD__INDEX] = crtc_saved->CRTC[NV_CIO_CRE_LCD__INDEX];
647 state->pllsel = saved->pllsel & ~(PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK);
648 crtc_state->gpio_ext = crtc_saved->gpio_ext;
649}
650
651static void nv_crtc_restore(struct drm_crtc *crtc)
652{
653 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000654 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000655 int head = nv_crtc->index;
Ben Skeggs017e6e22012-07-18 10:00:50 +1000656 uint8_t saved_cr21 = nv04_display(dev)->saved_reg.crtc_reg[head].CRTC[NV_CIO_CRE_21];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657
658 if (nv_two_heads(crtc->dev))
659 NVSetOwner(crtc->dev, head);
660
Ben Skeggs017e6e22012-07-18 10:00:50 +1000661 nouveau_hw_load_state(crtc->dev, head, &nv04_display(dev)->saved_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000662 nv_lock_vga_crtc_shadow(crtc->dev, head, saved_cr21);
663
664 nv_crtc->last_dpms = NV_DPMS_CLEARED;
665}
666
667static void nv_crtc_prepare(struct drm_crtc *crtc)
668{
669 struct drm_device *dev = crtc->dev;
670 struct drm_nouveau_private *dev_priv = dev->dev_private;
671 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
672 struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
673
674 if (nv_two_heads(dev))
675 NVSetOwner(dev, nv_crtc->index);
676
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200677 drm_vblank_pre_modeset(dev, nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000678 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
679
680 NVBlankScreen(dev, nv_crtc->index, true);
681
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300682 /* Some more preparation. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000683 NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
684 if (dev_priv->card_type == NV_40) {
685 uint32_t reg900 = NVReadRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900);
686 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900, reg900 & ~0x10000);
687 }
688}
689
690static void nv_crtc_commit(struct drm_crtc *crtc)
691{
692 struct drm_device *dev = crtc->dev;
693 struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
695
Ben Skeggs017e6e22012-07-18 10:00:50 +1000696 nouveau_hw_load_state(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697 nv04_crtc_mode_set_base(crtc, crtc->x, crtc->y, NULL);
698
699#ifdef __BIG_ENDIAN
700 /* turn on LFB swapping */
701 {
702 uint8_t tmp = NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR);
703 tmp |= MASK(NV_CIO_CRE_RCR_ENDIAN_BIG);
704 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR, tmp);
705 }
706#endif
707
708 funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200709 drm_vblank_post_modeset(dev, nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710}
711
712static void nv_crtc_destroy(struct drm_crtc *crtc)
713{
714 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
715
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100716 NV_DEBUG_KMS(crtc->dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000717
718 if (!nv_crtc)
719 return;
720
721 drm_crtc_cleanup(crtc);
722
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000723 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000724 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
725 kfree(nv_crtc);
726}
727
728static void
729nv_crtc_gamma_load(struct drm_crtc *crtc)
730{
731 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
732 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000733 struct rgb { uint8_t r, g, b; } __attribute__((packed)) *rgbs;
734 int i;
735
Ben Skeggs017e6e22012-07-18 10:00:50 +1000736 rgbs = (struct rgb *)nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index].DAC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 for (i = 0; i < 256; i++) {
738 rgbs[i].r = nv_crtc->lut.r[i] >> 8;
739 rgbs[i].g = nv_crtc->lut.g[i] >> 8;
740 rgbs[i].b = nv_crtc->lut.b[i] >> 8;
741 }
742
Ben Skeggs017e6e22012-07-18 10:00:50 +1000743 nouveau_hw_load_state_palette(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744}
745
746static void
James Simmons72034252010-08-03 01:33:19 +0100747nv_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b, uint32_t start,
748 uint32_t size)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749{
James Simmons72034252010-08-03 01:33:19 +0100750 int end = (start + size > 256) ? 256 : start + size, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000751 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000752
James Simmons72034252010-08-03 01:33:19 +0100753 for (i = start; i < end; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000754 nv_crtc->lut.r[i] = r[i];
755 nv_crtc->lut.g[i] = g[i];
756 nv_crtc->lut.b[i] = b[i];
757 }
758
759 /* We need to know the depth before we upload, but it's possible to
760 * get called before a framebuffer is bound. If this is the case,
761 * mark the lut values as dirty by setting depth==0, and it'll be
762 * uploaded on the first mode_set_base()
763 */
764 if (!nv_crtc->base.fb) {
765 nv_crtc->lut.depth = 0;
766 return;
767 }
768
769 nv_crtc_gamma_load(crtc);
770}
771
772static int
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500773nv04_crtc_do_mode_set_base(struct drm_crtc *crtc,
774 struct drm_framebuffer *passed_fb,
775 int x, int y, bool atomic)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776{
777 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
778 struct drm_device *dev = crtc->dev;
779 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs017e6e22012-07-18 10:00:50 +1000780 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Emil Velikov0e83bb42011-08-25 21:36:51 +0100781 struct drm_framebuffer *drm_fb;
782 struct nouveau_framebuffer *fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000783 int arb_burst, arb_lwm;
784 int ret;
785
Emil Velikov0e83bb42011-08-25 21:36:51 +0100786 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
787
788 /* no fb bound */
789 if (!atomic && !crtc->fb) {
790 NV_DEBUG_KMS(dev, "No FB bound\n");
791 return 0;
792 }
793
794
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500795 /* If atomic, we want to switch to the fb we were passed, so
796 * now we update pointers to do that. (We don't pin; just
797 * assume we're already pinned and update the base address.)
798 */
799 if (atomic) {
800 drm_fb = passed_fb;
801 fb = nouveau_framebuffer(passed_fb);
Emil Velikovf9ec8f62011-03-19 23:31:53 +0000802 } else {
Emil Velikov0e83bb42011-08-25 21:36:51 +0100803 drm_fb = crtc->fb;
804 fb = nouveau_framebuffer(crtc->fb);
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500805 /* If not atomic, we can go ahead and pin, and unpin the
806 * old fb we were passed.
807 */
808 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
809 if (ret)
810 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500812 if (passed_fb) {
813 struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
814 nouveau_bo_unpin(ofb->nvbo);
815 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 }
817
818 nv_crtc->fb.offset = fb->nvbo->bo.offset;
819
820 if (nv_crtc->lut.depth != drm_fb->depth) {
821 nv_crtc->lut.depth = drm_fb->depth;
822 nv_crtc_gamma_load(crtc);
823 }
824
825 /* Update the framebuffer format. */
826 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] &= ~3;
827 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (crtc->fb->depth + 1) / 8;
828 regp->ramdac_gen_ctrl &= ~NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
829 if (crtc->fb->depth == 16)
830 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
831 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_PIXEL_INDEX);
832 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_GENERAL_CONTROL,
833 regp->ramdac_gen_ctrl);
834
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200835 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = drm_fb->pitches[0] >> 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000836 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200837 XLATE(drm_fb->pitches[0] >> 3, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200838 regp->CRTC[NV_CIO_CRE_42] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200839 XLATE(drm_fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_RPC0_INDEX);
841 crtc_wr_cio_state(crtc, regp, NV_CIO_CR_OFFSET_INDEX);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200842 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_42);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000843
844 /* Update the framebuffer location. */
845 regp->fb_start = nv_crtc->fb.offset & ~3;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200846 regp->fb_start += (y * drm_fb->pitches[0]) + (x * drm_fb->bits_per_pixel / 8);
Francisco Jerez5794b5f2010-10-22 04:39:14 +0200847 nv_set_crtc_base(dev, nv_crtc->index, regp->fb_start);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000848
849 /* Update the arbitration parameters. */
850 nouveau_calc_arb(dev, crtc->mode.clock, drm_fb->bits_per_pixel,
851 &arb_burst, &arb_lwm);
852
853 regp->CRTC[NV_CIO_CRE_FF_INDEX] = arb_burst;
854 regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arb_lwm & 0xff;
855 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FF_INDEX);
856 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FFLWM__INDEX);
857
Francisco Jerez4295f182010-08-26 23:07:02 +0200858 if (dev_priv->card_type >= NV_20) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000859 regp->CRTC[NV_CIO_CRE_47] = arb_lwm >> 8;
860 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_47);
861 }
862
863 return 0;
864}
865
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500866static int
867nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
868 struct drm_framebuffer *old_fb)
869{
870 return nv04_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
871}
872
873static int
874nv04_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
875 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -0500876 int x, int y, enum mode_set_atomic state)
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500877{
Chris Balla424d762010-09-26 06:47:27 -0500878 struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
879 struct drm_device *dev = dev_priv->dev;
880
Jason Wessel21c74a82010-10-13 14:09:44 -0500881 if (state == ENTER_ATOMIC_MODE_SET)
Chris Balla424d762010-09-26 06:47:27 -0500882 nouveau_fbcon_save_disable_accel(dev);
883 else
884 nouveau_fbcon_restore_accel(dev);
885
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500886 return nv04_crtc_do_mode_set_base(crtc, fb, x, y, true);
887}
888
Ben Skeggs6ee73862009-12-11 19:24:15 +1000889static void nv04_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
890 struct nouveau_bo *dst)
891{
892 int width = nv_cursor_width(dev);
893 uint32_t pixel;
894 int i, j;
895
896 for (i = 0; i < width; i++) {
897 for (j = 0; j < width; j++) {
898 pixel = nouveau_bo_rd32(src, i*64 + j);
899
900 nouveau_bo_wr16(dst, i*width + j, (pixel & 0x80000000) >> 16
901 | (pixel & 0xf80000) >> 9
902 | (pixel & 0xf800) >> 6
903 | (pixel & 0xf8) >> 3);
904 }
905 }
906}
907
908static void nv11_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
909 struct nouveau_bo *dst)
910{
911 uint32_t pixel;
912 int alpha, i;
913
914 /* nv11+ supports premultiplied (PM), or non-premultiplied (NPM) alpha
915 * cursors (though NPM in combination with fp dithering may not work on
916 * nv11, from "nv" driver history)
917 * NPM mode needs NV_PCRTC_CURSOR_CONFIG_ALPHA_BLEND set and is what the
918 * blob uses, however we get given PM cursors so we use PM mode
919 */
920 for (i = 0; i < 64 * 64; i++) {
921 pixel = nouveau_bo_rd32(src, i);
922
923 /* hw gets unhappy if alpha <= rgb values. for a PM image "less
924 * than" shouldn't happen; fix "equal to" case by adding one to
925 * alpha channel (slightly inaccurate, but so is attempting to
926 * get back to NPM images, due to limits of integer precision)
927 */
928 alpha = pixel >> 24;
929 if (alpha > 0 && alpha < 255)
930 pixel = (pixel & 0x00ffffff) | ((alpha + 1) << 24);
931
932#ifdef __BIG_ENDIAN
933 {
934 struct drm_nouveau_private *dev_priv = dev->dev_private;
935
936 if (dev_priv->chipset == 0x11) {
937 pixel = ((pixel & 0x000000ff) << 24) |
938 ((pixel & 0x0000ff00) << 8) |
939 ((pixel & 0x00ff0000) >> 8) |
940 ((pixel & 0xff000000) >> 24);
941 }
942 }
943#endif
944
945 nouveau_bo_wr32(dst, i, pixel);
946 }
947}
948
949static int
950nv04_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
951 uint32_t buffer_handle, uint32_t width, uint32_t height)
952{
953 struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
954 struct drm_device *dev = dev_priv->dev;
955 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
956 struct nouveau_bo *cursor = NULL;
957 struct drm_gem_object *gem;
958 int ret = 0;
959
Ben Skeggs6ee73862009-12-11 19:24:15 +1000960 if (!buffer_handle) {
961 nv_crtc->cursor.hide(nv_crtc, true);
962 return 0;
963 }
964
Marcin Slusarzb4fa9d02011-05-01 23:49:04 +0200965 if (width != 64 || height != 64)
966 return -EINVAL;
967
Ben Skeggs6ee73862009-12-11 19:24:15 +1000968 gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
969 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100970 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000971 cursor = nouveau_gem_object(gem);
972
973 ret = nouveau_bo_map(cursor);
974 if (ret)
975 goto out;
976
977 if (dev_priv->chipset >= 0x11)
978 nv11_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
979 else
980 nv04_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
981
982 nouveau_bo_unmap(cursor);
983 nv_crtc->cursor.offset = nv_crtc->cursor.nvbo->bo.offset;
984 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
985 nv_crtc->cursor.show(nv_crtc, true);
986out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000987 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000988 return ret;
989}
990
991static int
992nv04_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
993{
994 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
995
996 nv_crtc->cursor.set_pos(nv_crtc, x, y);
997 return 0;
998}
999
1000static const struct drm_crtc_funcs nv04_crtc_funcs = {
1001 .save = nv_crtc_save,
1002 .restore = nv_crtc_restore,
1003 .cursor_set = nv04_crtc_cursor_set,
1004 .cursor_move = nv04_crtc_cursor_move,
1005 .gamma_set = nv_crtc_gamma_set,
1006 .set_config = drm_crtc_helper_set_config,
Francisco Jerez332b2422010-10-20 23:35:40 +02001007 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001008 .destroy = nv_crtc_destroy,
1009};
1010
1011static const struct drm_crtc_helper_funcs nv04_crtc_helper_funcs = {
1012 .dpms = nv_crtc_dpms,
1013 .prepare = nv_crtc_prepare,
1014 .commit = nv_crtc_commit,
1015 .mode_fixup = nv_crtc_mode_fixup,
1016 .mode_set = nv_crtc_mode_set,
1017 .mode_set_base = nv04_crtc_mode_set_base,
Chris Ballbe64c2bb2010-09-26 06:47:24 -05001018 .mode_set_base_atomic = nv04_crtc_mode_set_base_atomic,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001019 .load_lut = nv_crtc_gamma_load,
1020};
1021
1022int
1023nv04_crtc_create(struct drm_device *dev, int crtc_num)
1024{
1025 struct nouveau_crtc *nv_crtc;
1026 int ret, i;
1027
1028 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
1029 if (!nv_crtc)
1030 return -ENOMEM;
1031
1032 for (i = 0; i < 256; i++) {
1033 nv_crtc->lut.r[i] = i << 8;
1034 nv_crtc->lut.g[i] = i << 8;
1035 nv_crtc->lut.b[i] = i << 8;
1036 }
1037 nv_crtc->lut.depth = 0;
1038
1039 nv_crtc->index = crtc_num;
1040 nv_crtc->last_dpms = NV_DPMS_CLEARED;
1041
1042 drm_crtc_init(dev, &nv_crtc->base, &nv04_crtc_funcs);
1043 drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
1044 drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
1045
Ben Skeggs7375c952011-06-07 14:21:29 +10001046 ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
Dave Airlie22b33e82012-04-02 11:53:06 +01001047 0, 0x0000, NULL, &nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001048 if (!ret) {
1049 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
1050 if (!ret)
1051 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
1052 if (ret)
1053 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1054 }
1055
1056 nv04_cursor_init(nv_crtc);
1057
1058 return 0;
1059}
1060