blob: ab7b69c11d40fab09c710c9a3cde981aa9863990 [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 */
Dave Airlie5addcf02012-09-10 14:20:51 +100025#include <linux/pm_runtime.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100026
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010029#include <drm/drm_plane_helper.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100030
Ben Skeggs4dc28132016-05-20 09:22:55 +100031#include "nouveau_drv.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100032#include "nouveau_reg.h"
Ben Skeggs4dc28132016-05-20 09:22:55 +100033#include "nouveau_ttm.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100034#include "nouveau_bo.h"
35#include "nouveau_gem.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100036#include "nouveau_encoder.h"
37#include "nouveau_connector.h"
38#include "nouveau_crtc.h"
Ben Skeggs1a646342013-03-21 15:45:11 +100039#include "hw.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100040#include "nvreg.h"
Chris Balla424d762010-09-26 06:47:27 -050041#include "nouveau_fbcon.h"
Ben Skeggs1a646342013-03-21 15:45:11 +100042#include "disp.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100043
44#include <subdev/bios/pll.h>
Ben Skeggsf3867f42015-01-13 23:37:38 +100045#include <subdev/clk.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100046
47static int
48nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
49 struct drm_framebuffer *old_fb);
50
51static void
52crtc_wr_cio_state(struct drm_crtc *crtc, struct nv04_crtc_reg *crtcstate, int index)
53{
54 NVWriteVgaCrtc(crtc->dev, nouveau_crtc(crtc)->index, index,
55 crtcstate->CRTC[index]);
56}
57
58static void nv_crtc_set_digital_vibrance(struct drm_crtc *crtc, int level)
59{
60 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +100061 struct drm_device *dev = crtc->dev;
62 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +100063
64 regp->CRTC[NV_CIO_CRE_CSB] = nv_crtc->saturation = level;
65 if (nv_crtc->saturation && nv_gf4_disp_arch(crtc->dev)) {
66 regp->CRTC[NV_CIO_CRE_CSB] = 0x80;
67 regp->CRTC[NV_CIO_CRE_5B] = nv_crtc->saturation << 2;
68 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_5B);
69 }
70 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_CSB);
71}
72
73static void nv_crtc_set_image_sharpening(struct drm_crtc *crtc, int level)
74{
75 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +100076 struct drm_device *dev = crtc->dev;
77 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Ben Skeggs6ee73862009-12-11 19:24:15 +100078
79 nv_crtc->sharpness = level;
80 if (level < 0) /* blur is in hw range 0x3f -> 0x20 */
81 level += 0x40;
82 regp->ramdac_634 = level;
83 NVWriteRAMDAC(crtc->dev, nv_crtc->index, NV_PRAMDAC_634, regp->ramdac_634);
84}
85
86#define PLLSEL_VPLL1_MASK \
87 (NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_VPLL \
88 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK_RATIO_DB2)
89#define PLLSEL_VPLL2_MASK \
90 (NV_PRAMDAC_PLL_COEFF_SELECT_PLL_SOURCE_VPLL2 \
91 | NV_PRAMDAC_PLL_COEFF_SELECT_VCLK2_RATIO_DB2)
92#define PLLSEL_TV_MASK \
93 (NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK1 \
94 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK1 \
95 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_VSCLK2 \
96 | NV_PRAMDAC_PLL_COEFF_SELECT_TV_PCLK2)
97
98/* NV4x 0x40.. pll notes:
99 * gpu pll: 0x4000 + 0x4004
100 * ?gpu? pll: 0x4008 + 0x400c
101 * vpll1: 0x4010 + 0x4014
102 * vpll2: 0x4018 + 0x401c
103 * mpll: 0x4020 + 0x4024
104 * mpll: 0x4038 + 0x403c
105 *
106 * the first register of each pair has some unknown details:
107 * bits 0-7: redirected values from elsewhere? (similar to PLL_SETUP_CONTROL?)
108 * bits 20-23: (mpll) something to do with post divider?
109 * bits 28-31: related to single stage mode? (bit 8/12)
110 */
111
112static void nv_crtc_calc_state_ext(struct drm_crtc *crtc, struct drm_display_mode * mode, int dot_clock)
113{
114 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000115 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000116 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
117 struct nvkm_clk *clk = nvxx_clk(&drm->client.device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000118 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000119 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000120 struct nv04_crtc_reg *regp = &state->crtc_reg[nv_crtc->index];
Ben Skeggsbe83cd42015-01-14 15:36:34 +1000121 struct nvkm_pll_vals *pv = &regp->pllvals;
Ben Skeggs70790f42012-07-10 17:26:46 +1000122 struct nvbios_pll pll_lim;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123
Ben Skeggs77145f12012-07-31 16:16:21 +1000124 if (nvbios_pll_parse(bios, nv_crtc->index ? PLL_VPLL1 : PLL_VPLL0,
125 &pll_lim))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000126 return;
127
128 /* NM2 == 0 is used to determine single stage mode on two stage plls */
129 pv->NM2 = 0;
130
131 /* for newer nv4x the blob uses only the first stage of the vpll below a
132 * certain clock. for a certain nv4b this is 150MHz. since the max
133 * output frequency of the first stage for this card is 300MHz, it is
134 * assumed the threshold is given by vco1 maxfreq/2
135 */
136 /* for early nv4x, specifically nv40 and *some* nv43 (devids 0 and 6,
137 * not 8, others unknown), the blob always uses both plls. no problem
138 * has yet been observed in allowing the use a single stage pll on all
139 * nv43 however. the behaviour of single stage use is untested on nv40
140 */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000141 if (drm->client.device.info.chipset > 0x40 && dot_clock <= (pll_lim.vco1.max_freq / 2))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000142 memset(&pll_lim.vco2, 0, sizeof(pll_lim.vco2));
143
Ben Skeggs77145f12012-07-31 16:16:21 +1000144
145 if (!clk->pll_calc(clk, &pll_lim, dot_clock, pv))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 return;
147
148 state->pllsel &= PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK;
149
150 /* The blob uses this always, so let's do the same */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000151 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000152 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_USE_VPLL2_TRUE;
153 /* again nv40 and some nv43 act more like nv3x as described above */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000154 if (drm->client.device.info.chipset < 0x41)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155 state->pllsel |= NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_MPLL |
156 NV_PRAMDAC_PLL_COEFF_SELECT_SOURCE_PROG_NVPLL;
157 state->pllsel |= nv_crtc->index ? PLLSEL_VPLL2_MASK : PLLSEL_VPLL1_MASK;
158
159 if (pv->NM2)
Ben Skeggs77145f12012-07-31 16:16:21 +1000160 NV_DEBUG(drm, "vpll: n1 %d n2 %d m1 %d m2 %d log2p %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 pv->N1, pv->N2, pv->M1, pv->M2, pv->log2P);
162 else
Ben Skeggs77145f12012-07-31 16:16:21 +1000163 NV_DEBUG(drm, "vpll: n %d m %d log2p %d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000164 pv->N1, pv->M1, pv->log2P);
165
166 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
167}
168
169static void
170nv_crtc_dpms(struct drm_crtc *crtc, int mode)
171{
172 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
173 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000174 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175 unsigned char seq1 = 0, crtc17 = 0;
176 unsigned char crtc1A;
177
Ben Skeggs77145f12012-07-31 16:16:21 +1000178 NV_DEBUG(drm, "Setting dpms mode %d on CRTC %d\n", mode,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000179 nv_crtc->index);
180
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300181 if (nv_crtc->last_dpms == mode) /* Don't do unnecessary mode changes. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000182 return;
183
184 nv_crtc->last_dpms = mode;
185
186 if (nv_two_heads(dev))
187 NVSetOwner(dev, nv_crtc->index);
188
189 /* nv4ref indicates these two RPC1 bits inhibit h/v sync */
190 crtc1A = NVReadVgaCrtc(dev, nv_crtc->index,
191 NV_CIO_CRE_RPC1_INDEX) & ~0xC0;
192 switch (mode) {
193 case DRM_MODE_DPMS_STANDBY:
194 /* Screen: Off; HSync: Off, VSync: On -- Not Supported */
195 seq1 = 0x20;
196 crtc17 = 0x80;
197 crtc1A |= 0x80;
198 break;
199 case DRM_MODE_DPMS_SUSPEND:
200 /* Screen: Off; HSync: On, VSync: Off -- Not Supported */
201 seq1 = 0x20;
202 crtc17 = 0x80;
203 crtc1A |= 0x40;
204 break;
205 case DRM_MODE_DPMS_OFF:
206 /* Screen: Off; HSync: Off, VSync: Off */
207 seq1 = 0x20;
208 crtc17 = 0x00;
209 crtc1A |= 0xC0;
210 break;
211 case DRM_MODE_DPMS_ON:
212 default:
213 /* Screen: On; HSync: On, VSync: On */
214 seq1 = 0x00;
215 crtc17 = 0x80;
216 break;
217 }
218
219 NVVgaSeqReset(dev, nv_crtc->index, true);
220 /* Each head has it's own sequencer, so we can turn it off when we want */
221 seq1 |= (NVReadVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX) & ~0x20);
222 NVWriteVgaSeq(dev, nv_crtc->index, NV_VIO_SR_CLOCK_INDEX, seq1);
223 crtc17 |= (NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX) & ~0x80);
224 mdelay(10);
225 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CR_MODE_INDEX, crtc17);
226 NVVgaSeqReset(dev, nv_crtc->index, false);
227
228 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RPC1_INDEX, crtc1A);
229}
230
Ben Skeggs6ee73862009-12-11 19:24:15 +1000231static void
232nv_crtc_mode_set_vga(struct drm_crtc *crtc, struct drm_display_mode *mode)
233{
234 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000236 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Matt Roperf4510a22014-04-01 15:22:40 -0700237 struct drm_framebuffer *fb = crtc->primary->fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238
239 /* Calculate our timings */
Francisco Jereze5ec8822010-03-05 15:15:39 +0100240 int horizDisplay = (mode->crtc_hdisplay >> 3) - 1;
241 int horizStart = (mode->crtc_hsync_start >> 3) + 1;
242 int horizEnd = (mode->crtc_hsync_end >> 3) + 1;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000243 int horizTotal = (mode->crtc_htotal >> 3) - 5;
244 int horizBlankStart = (mode->crtc_hdisplay >> 3) - 1;
245 int horizBlankEnd = (mode->crtc_htotal >> 3) - 1;
246 int vertDisplay = mode->crtc_vdisplay - 1;
247 int vertStart = mode->crtc_vsync_start - 1;
248 int vertEnd = mode->crtc_vsync_end - 1;
249 int vertTotal = mode->crtc_vtotal - 2;
250 int vertBlankStart = mode->crtc_vdisplay - 1;
251 int vertBlankEnd = mode->crtc_vtotal - 1;
252
253 struct drm_encoder *encoder;
254 bool fp_output = false;
255
256 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
257 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
258
259 if (encoder->crtc == crtc &&
Ben Skeggscb75d972012-07-11 10:44:20 +1000260 (nv_encoder->dcb->type == DCB_OUTPUT_LVDS ||
261 nv_encoder->dcb->type == DCB_OUTPUT_TMDS))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000262 fp_output = true;
263 }
264
265 if (fp_output) {
266 vertStart = vertTotal - 3;
267 vertEnd = vertTotal - 2;
268 vertBlankStart = vertStart;
269 horizStart = horizTotal - 5;
270 horizEnd = horizTotal - 2;
271 horizBlankEnd = horizTotal + 4;
272#if 0
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000273 if (dev->overlayAdaptor && drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274 /* This reportedly works around some video overlay bandwidth problems */
275 horizTotal += 2;
276#endif
277 }
278
279 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
280 vertTotal |= 1;
281
282#if 0
283 ErrorF("horizDisplay: 0x%X \n", horizDisplay);
284 ErrorF("horizStart: 0x%X \n", horizStart);
285 ErrorF("horizEnd: 0x%X \n", horizEnd);
286 ErrorF("horizTotal: 0x%X \n", horizTotal);
287 ErrorF("horizBlankStart: 0x%X \n", horizBlankStart);
288 ErrorF("horizBlankEnd: 0x%X \n", horizBlankEnd);
289 ErrorF("vertDisplay: 0x%X \n", vertDisplay);
290 ErrorF("vertStart: 0x%X \n", vertStart);
291 ErrorF("vertEnd: 0x%X \n", vertEnd);
292 ErrorF("vertTotal: 0x%X \n", vertTotal);
293 ErrorF("vertBlankStart: 0x%X \n", vertBlankStart);
294 ErrorF("vertBlankEnd: 0x%X \n", vertBlankEnd);
295#endif
296
297 /*
298 * compute correct Hsync & Vsync polarity
299 */
300 if ((mode->flags & (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC))
301 && (mode->flags & (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC))) {
302
303 regp->MiscOutReg = 0x23;
304 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
305 regp->MiscOutReg |= 0x40;
306 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
307 regp->MiscOutReg |= 0x80;
308 } else {
309 int vdisplay = mode->vdisplay;
310 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
311 vdisplay *= 2;
312 if (mode->vscan > 1)
313 vdisplay *= mode->vscan;
314 if (vdisplay < 400)
315 regp->MiscOutReg = 0xA3; /* +hsync -vsync */
316 else if (vdisplay < 480)
317 regp->MiscOutReg = 0x63; /* -hsync +vsync */
318 else if (vdisplay < 768)
319 regp->MiscOutReg = 0xE3; /* -hsync -vsync */
320 else
321 regp->MiscOutReg = 0x23; /* +hsync +vsync */
322 }
323
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324 /*
325 * Time Sequencer
326 */
327 regp->Sequencer[NV_VIO_SR_RESET_INDEX] = 0x00;
328 /* 0x20 disables the sequencer */
329 if (mode->flags & DRM_MODE_FLAG_CLKDIV2)
330 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x29;
331 else
332 regp->Sequencer[NV_VIO_SR_CLOCK_INDEX] = 0x21;
333 regp->Sequencer[NV_VIO_SR_PLANE_MASK_INDEX] = 0x0F;
334 regp->Sequencer[NV_VIO_SR_CHAR_MAP_INDEX] = 0x00;
335 regp->Sequencer[NV_VIO_SR_MEM_MODE_INDEX] = 0x0E;
336
337 /*
338 * CRTC
339 */
340 regp->CRTC[NV_CIO_CR_HDT_INDEX] = horizTotal;
341 regp->CRTC[NV_CIO_CR_HDE_INDEX] = horizDisplay;
342 regp->CRTC[NV_CIO_CR_HBS_INDEX] = horizBlankStart;
343 regp->CRTC[NV_CIO_CR_HBE_INDEX] = (1 << 7) |
344 XLATE(horizBlankEnd, 0, NV_CIO_CR_HBE_4_0);
345 regp->CRTC[NV_CIO_CR_HRS_INDEX] = horizStart;
346 regp->CRTC[NV_CIO_CR_HRE_INDEX] = XLATE(horizBlankEnd, 5, NV_CIO_CR_HRE_HBE_5) |
347 XLATE(horizEnd, 0, NV_CIO_CR_HRE_4_0);
348 regp->CRTC[NV_CIO_CR_VDT_INDEX] = vertTotal;
349 regp->CRTC[NV_CIO_CR_OVL_INDEX] = XLATE(vertStart, 9, NV_CIO_CR_OVL_VRS_9) |
350 XLATE(vertDisplay, 9, NV_CIO_CR_OVL_VDE_9) |
351 XLATE(vertTotal, 9, NV_CIO_CR_OVL_VDT_9) |
352 (1 << 4) |
353 XLATE(vertBlankStart, 8, NV_CIO_CR_OVL_VBS_8) |
354 XLATE(vertStart, 8, NV_CIO_CR_OVL_VRS_8) |
355 XLATE(vertDisplay, 8, NV_CIO_CR_OVL_VDE_8) |
356 XLATE(vertTotal, 8, NV_CIO_CR_OVL_VDT_8);
357 regp->CRTC[NV_CIO_CR_RSAL_INDEX] = 0x00;
358 regp->CRTC[NV_CIO_CR_CELL_HT_INDEX] = ((mode->flags & DRM_MODE_FLAG_DBLSCAN) ? MASK(NV_CIO_CR_CELL_HT_SCANDBL) : 0) |
359 1 << 6 |
360 XLATE(vertBlankStart, 9, NV_CIO_CR_CELL_HT_VBS_9);
361 regp->CRTC[NV_CIO_CR_CURS_ST_INDEX] = 0x00;
362 regp->CRTC[NV_CIO_CR_CURS_END_INDEX] = 0x00;
363 regp->CRTC[NV_CIO_CR_SA_HI_INDEX] = 0x00;
364 regp->CRTC[NV_CIO_CR_SA_LO_INDEX] = 0x00;
365 regp->CRTC[NV_CIO_CR_TCOFF_HI_INDEX] = 0x00;
366 regp->CRTC[NV_CIO_CR_TCOFF_LO_INDEX] = 0x00;
367 regp->CRTC[NV_CIO_CR_VRS_INDEX] = vertStart;
368 regp->CRTC[NV_CIO_CR_VRE_INDEX] = 1 << 5 | XLATE(vertEnd, 0, NV_CIO_CR_VRE_3_0);
369 regp->CRTC[NV_CIO_CR_VDE_INDEX] = vertDisplay;
370 /* framebuffer can be larger than crtc scanout area. */
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200371 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = fb->pitches[0] / 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000372 regp->CRTC[NV_CIO_CR_ULINE_INDEX] = 0x00;
373 regp->CRTC[NV_CIO_CR_VBS_INDEX] = vertBlankStart;
374 regp->CRTC[NV_CIO_CR_VBE_INDEX] = vertBlankEnd;
375 regp->CRTC[NV_CIO_CR_MODE_INDEX] = 0x43;
376 regp->CRTC[NV_CIO_CR_LCOMP_INDEX] = 0xff;
377
378 /*
379 * Some extended CRTC registers (they are not saved with the rest of the vga regs).
380 */
381
382 /* framebuffer can be larger than crtc scanout area. */
Francisco Jerezc1003d92011-05-24 15:57:14 +0200383 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200384 XLATE(fb->pitches[0] / 8, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200385 regp->CRTC[NV_CIO_CRE_42] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200386 XLATE(fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000387 regp->CRTC[NV_CIO_CRE_RPC1_INDEX] = mode->crtc_hdisplay < 1280 ?
388 MASK(NV_CIO_CRE_RPC1_LARGE) : 0x00;
389 regp->CRTC[NV_CIO_CRE_LSR_INDEX] = XLATE(horizBlankEnd, 6, NV_CIO_CRE_LSR_HBE_6) |
390 XLATE(vertBlankStart, 10, NV_CIO_CRE_LSR_VBS_10) |
391 XLATE(vertStart, 10, NV_CIO_CRE_LSR_VRS_10) |
392 XLATE(vertDisplay, 10, NV_CIO_CRE_LSR_VDE_10) |
393 XLATE(vertTotal, 10, NV_CIO_CRE_LSR_VDT_10);
394 regp->CRTC[NV_CIO_CRE_HEB__INDEX] = XLATE(horizStart, 8, NV_CIO_CRE_HEB_HRS_8) |
395 XLATE(horizBlankStart, 8, NV_CIO_CRE_HEB_HBS_8) |
396 XLATE(horizDisplay, 8, NV_CIO_CRE_HEB_HDE_8) |
397 XLATE(horizTotal, 8, NV_CIO_CRE_HEB_HDT_8);
398 regp->CRTC[NV_CIO_CRE_EBR_INDEX] = XLATE(vertBlankStart, 11, NV_CIO_CRE_EBR_VBS_11) |
399 XLATE(vertStart, 11, NV_CIO_CRE_EBR_VRS_11) |
400 XLATE(vertDisplay, 11, NV_CIO_CRE_EBR_VDE_11) |
401 XLATE(vertTotal, 11, NV_CIO_CRE_EBR_VDT_11);
402
403 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
404 horizTotal = (horizTotal >> 1) & ~1;
405 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = horizTotal;
406 regp->CRTC[NV_CIO_CRE_HEB__INDEX] |= XLATE(horizTotal, 8, NV_CIO_CRE_HEB_ILC_8);
407 } else
408 regp->CRTC[NV_CIO_CRE_ILACE__INDEX] = 0xff; /* interlace off */
409
410 /*
411 * Graphics Display Controller
412 */
413 regp->Graphics[NV_VIO_GX_SR_INDEX] = 0x00;
414 regp->Graphics[NV_VIO_GX_SREN_INDEX] = 0x00;
415 regp->Graphics[NV_VIO_GX_CCOMP_INDEX] = 0x00;
416 regp->Graphics[NV_VIO_GX_ROP_INDEX] = 0x00;
417 regp->Graphics[NV_VIO_GX_READ_MAP_INDEX] = 0x00;
418 regp->Graphics[NV_VIO_GX_MODE_INDEX] = 0x40; /* 256 color mode */
419 regp->Graphics[NV_VIO_GX_MISC_INDEX] = 0x05; /* map 64k mem + graphic mode */
420 regp->Graphics[NV_VIO_GX_DONT_CARE_INDEX] = 0x0F;
421 regp->Graphics[NV_VIO_GX_BIT_MASK_INDEX] = 0xFF;
422
423 regp->Attribute[0] = 0x00; /* standard colormap translation */
424 regp->Attribute[1] = 0x01;
425 regp->Attribute[2] = 0x02;
426 regp->Attribute[3] = 0x03;
427 regp->Attribute[4] = 0x04;
428 regp->Attribute[5] = 0x05;
429 regp->Attribute[6] = 0x06;
430 regp->Attribute[7] = 0x07;
431 regp->Attribute[8] = 0x08;
432 regp->Attribute[9] = 0x09;
433 regp->Attribute[10] = 0x0A;
434 regp->Attribute[11] = 0x0B;
435 regp->Attribute[12] = 0x0C;
436 regp->Attribute[13] = 0x0D;
437 regp->Attribute[14] = 0x0E;
438 regp->Attribute[15] = 0x0F;
439 regp->Attribute[NV_CIO_AR_MODE_INDEX] = 0x01; /* Enable graphic mode */
440 /* Non-vga */
441 regp->Attribute[NV_CIO_AR_OSCAN_INDEX] = 0x00;
442 regp->Attribute[NV_CIO_AR_PLANE_INDEX] = 0x0F; /* enable all color planes */
443 regp->Attribute[NV_CIO_AR_HPP_INDEX] = 0x00;
444 regp->Attribute[NV_CIO_AR_CSEL_INDEX] = 0x00;
445}
446
447/**
448 * Sets up registers for the given mode/adjusted_mode pair.
449 *
450 * The clocks, CRTCs and outputs attached to this CRTC must be off.
451 *
452 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
453 * be easily turned on/off after this.
454 */
455static void
456nv_crtc_mode_set_regs(struct drm_crtc *crtc, struct drm_display_mode * mode)
457{
458 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000459 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000460 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000461 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
462 struct nv04_crtc_reg *savep = &nv04_display(dev)->saved_reg.crtc_reg[nv_crtc->index];
Ville Syrjäläd92df862016-11-18 21:52:47 +0200463 const struct drm_framebuffer *fb = crtc->primary->fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000464 struct drm_encoder *encoder;
465 bool lvds_output = false, tmds_output = false, tv_output = false,
466 off_chip_digital = false;
467
468 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
469 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
470 bool digital = false;
471
472 if (encoder->crtc != crtc)
473 continue;
474
Ben Skeggscb75d972012-07-11 10:44:20 +1000475 if (nv_encoder->dcb->type == DCB_OUTPUT_LVDS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476 digital = lvds_output = true;
Ben Skeggscb75d972012-07-11 10:44:20 +1000477 if (nv_encoder->dcb->type == DCB_OUTPUT_TV)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000478 tv_output = true;
Ben Skeggscb75d972012-07-11 10:44:20 +1000479 if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000480 digital = tmds_output = true;
481 if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP && digital)
482 off_chip_digital = true;
483 }
484
485 /* Registers not directly related to the (s)vga mode */
486
487 /* What is the meaning of this register? */
488 /* A few popular values are 0x18, 0x1c, 0x38, 0x3c */
489 regp->CRTC[NV_CIO_CRE_ENH_INDEX] = savep->CRTC[NV_CIO_CRE_ENH_INDEX] & ~(1<<5);
490
491 regp->crtc_eng_ctrl = 0;
492 /* Except for rare conditions I2C is enabled on the primary crtc */
493 if (nv_crtc->index == 0)
494 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_I2C;
495#if 0
496 /* Set overlay to desired crtc. */
497 if (dev->overlayAdaptor) {
498 NVPortPrivPtr pPriv = GET_OVERLAY_PRIVATE(dev);
499 if (pPriv->overlayCRTC == nv_crtc->index)
500 regp->crtc_eng_ctrl |= NV_CRTC_FSEL_OVERLAY;
501 }
502#endif
503
504 /* ADDRESS_SPACE_PNVM is the same as setting HCUR_ASI */
505 regp->cursor_cfg = NV_PCRTC_CURSOR_CONFIG_CUR_LINES_64 |
506 NV_PCRTC_CURSOR_CONFIG_CUR_PIXELS_64 |
507 NV_PCRTC_CURSOR_CONFIG_ADDRESS_SPACE_PNVM;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000508 if (drm->client.device.info.chipset >= 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000509 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_CUR_BPP_32;
510 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
511 regp->cursor_cfg |= NV_PCRTC_CURSOR_CONFIG_DOUBLE_SCAN_ENABLE;
512
513 /* Unblock some timings */
514 regp->CRTC[NV_CIO_CRE_53] = 0;
515 regp->CRTC[NV_CIO_CRE_54] = 0;
516
517 /* 0x00 is disabled, 0x11 is lvds, 0x22 crt and 0x88 tmds */
518 if (lvds_output)
519 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x11;
520 else if (tmds_output)
521 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x88;
522 else
523 regp->CRTC[NV_CIO_CRE_SCRATCH3__INDEX] = 0x22;
524
525 /* These values seem to vary */
526 /* This register seems to be used by the bios to make certain decisions on some G70 cards? */
527 regp->CRTC[NV_CIO_CRE_SCRATCH4__INDEX] = savep->CRTC[NV_CIO_CRE_SCRATCH4__INDEX];
528
529 nv_crtc_set_digital_vibrance(crtc, nv_crtc->saturation);
530
531 /* probably a scratch reg, but kept for cargo-cult purposes:
532 * bit0: crtc0?, head A
533 * bit6: lvds, head A
534 * bit7: (only in X), head A
535 */
536 if (nv_crtc->index == 0)
537 regp->CRTC[NV_CIO_CRE_4B] = savep->CRTC[NV_CIO_CRE_4B] | 0x80;
538
539 /* The blob seems to take the current value from crtc 0, add 4 to that
540 * and reuse the old value for crtc 1 */
Ben Skeggs017e6e22012-07-18 10:00:50 +1000541 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 +1000542 if (!nv_crtc->index)
543 regp->CRTC[NV_CIO_CRE_TVOUT_LATENCY] += 4;
544
545 /* the blob sometimes sets |= 0x10 (which is the same as setting |=
546 * 1 << 30 on 0x60.830), for no apparent reason */
547 regp->CRTC[NV_CIO_CRE_59] = off_chip_digital;
548
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000549 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
Francisco Jerez4a9f8222010-07-20 16:48:08 +0200550 regp->CRTC[0x9f] = off_chip_digital ? 0x11 : 0x1;
551
Ben Skeggs6ee73862009-12-11 19:24:15 +1000552 regp->crtc_830 = mode->crtc_vdisplay - 3;
553 regp->crtc_834 = mode->crtc_vdisplay - 1;
554
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000555 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000556 /* This is what the blob does */
557 regp->crtc_850 = NVReadCRTC(dev, 0, NV_PCRTC_850);
558
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000559 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 regp->gpio_ext = NVReadCRTC(dev, 0, NV_PCRTC_GPIO_EXT);
561
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000562 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
Francisco Jerez63f7fcf2010-10-22 04:31:02 +0200563 regp->crtc_cfg = NV10_PCRTC_CONFIG_START_ADDRESS_HSYNC;
564 else
565 regp->crtc_cfg = NV04_PCRTC_CONFIG_START_ADDRESS_HSYNC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000566
567 /* Some misc regs */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000568 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000569 regp->CRTC[NV_CIO_CRE_85] = 0xFF;
570 regp->CRTC[NV_CIO_CRE_86] = 0x1;
571 }
572
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200573 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] = (fb->format->depth + 1) / 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000574 /* Enable slaved mode (called MODE_TV in nv4ref.h) */
575 if (lvds_output || tmds_output || tv_output)
576 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (1 << 7);
577
578 /* Generic PRAMDAC regs */
579
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000580 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000581 /* Only bit that bios and blob set. */
582 regp->nv10_cursync = (1 << 25);
583
584 regp->ramdac_gen_ctrl = NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
585 NV_PRAMDAC_GENERAL_CONTROL_VGA_STATE_SEL |
586 NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON;
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200587 if (fb->format->depth == 16)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000588 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000589 if (drm->client.device.info.chipset >= 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000590 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_PIPE_LONG;
591
592 regp->ramdac_630 = 0; /* turn off green mode (tv test pattern?) */
593 regp->tv_setup = 0;
594
595 nv_crtc_set_image_sharpening(crtc, nv_crtc->sharpness);
596
597 /* Some values the blob sets */
598 regp->ramdac_8c0 = 0x100;
599 regp->ramdac_a20 = 0x0;
600 regp->ramdac_a24 = 0xfffff;
601 regp->ramdac_a34 = 0x1;
602}
603
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000604static int
605nv_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
606{
607 struct nv04_display *disp = nv04_display(crtc->dev);
Matt Roperf4510a22014-04-01 15:22:40 -0700608 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000609 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
610 int ret;
611
Ben Skeggsad76b3f2014-11-10 11:24:27 +1000612 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM, false);
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000613 if (ret == 0) {
614 if (disp->image[nv_crtc->index])
615 nouveau_bo_unpin(disp->image[nv_crtc->index]);
616 nouveau_bo_ref(nvfb->nvbo, &disp->image[nv_crtc->index]);
617 }
618
619 return ret;
620}
621
Ben Skeggs6ee73862009-12-11 19:24:15 +1000622/**
623 * Sets up registers for the given mode/adjusted_mode pair.
624 *
625 * The clocks, CRTCs and outputs attached to this CRTC must be off.
626 *
627 * This shouldn't enable any clocks, CRTCs, or outputs, but they should
628 * be easily turned on/off after this.
629 */
630static int
631nv_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
632 struct drm_display_mode *adjusted_mode,
633 int x, int y, struct drm_framebuffer *old_fb)
634{
635 struct drm_device *dev = crtc->dev;
636 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs77145f12012-07-31 16:16:21 +1000637 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000638 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000639
Ben Skeggs77145f12012-07-31 16:16:21 +1000640 NV_DEBUG(drm, "CTRC mode on CRTC %d:\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000641 drm_mode_debug_printmodeline(adjusted_mode);
642
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000643 ret = nv_crtc_swap_fbs(crtc, old_fb);
644 if (ret)
645 return ret;
646
Ben Skeggs6ee73862009-12-11 19:24:15 +1000647 /* unlock must come after turning off FP_TG_CONTROL in output_prepare */
648 nv_lock_vga_crtc_shadow(dev, nv_crtc->index, -1);
649
650 nv_crtc_mode_set_vga(crtc, adjusted_mode);
651 /* calculated in nv04_dfp_prepare, nv40 needs it written before calculating PLLs */
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000652 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
Ben Skeggs017e6e22012-07-18 10:00:50 +1000653 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, nv04_display(dev)->mode_reg.sel_clk);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000654 nv_crtc_mode_set_regs(crtc, adjusted_mode);
655 nv_crtc_calc_state_ext(crtc, mode, adjusted_mode->clock);
656 return 0;
657}
658
659static void nv_crtc_save(struct drm_crtc *crtc)
660{
661 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000662 struct drm_device *dev = crtc->dev;
663 struct nv04_mode_state *state = &nv04_display(dev)->mode_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 struct nv04_crtc_reg *crtc_state = &state->crtc_reg[nv_crtc->index];
Ben Skeggs017e6e22012-07-18 10:00:50 +1000665 struct nv04_mode_state *saved = &nv04_display(dev)->saved_reg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000666 struct nv04_crtc_reg *crtc_saved = &saved->crtc_reg[nv_crtc->index];
667
668 if (nv_two_heads(crtc->dev))
669 NVSetOwner(crtc->dev, nv_crtc->index);
670
671 nouveau_hw_save_state(crtc->dev, nv_crtc->index, saved);
672
673 /* init some state to saved value */
674 state->sel_clk = saved->sel_clk & ~(0x5 << 16);
675 crtc_state->CRTC[NV_CIO_CRE_LCD__INDEX] = crtc_saved->CRTC[NV_CIO_CRE_LCD__INDEX];
676 state->pllsel = saved->pllsel & ~(PLLSEL_VPLL1_MASK | PLLSEL_VPLL2_MASK | PLLSEL_TV_MASK);
677 crtc_state->gpio_ext = crtc_saved->gpio_ext;
678}
679
680static void nv_crtc_restore(struct drm_crtc *crtc)
681{
682 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000683 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000684 int head = nv_crtc->index;
Ben Skeggs017e6e22012-07-18 10:00:50 +1000685 uint8_t saved_cr21 = nv04_display(dev)->saved_reg.crtc_reg[head].CRTC[NV_CIO_CRE_21];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000686
687 if (nv_two_heads(crtc->dev))
688 NVSetOwner(crtc->dev, head);
689
Ben Skeggs017e6e22012-07-18 10:00:50 +1000690 nouveau_hw_load_state(crtc->dev, head, &nv04_display(dev)->saved_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691 nv_lock_vga_crtc_shadow(crtc->dev, head, saved_cr21);
692
693 nv_crtc->last_dpms = NV_DPMS_CLEARED;
694}
695
696static void nv_crtc_prepare(struct drm_crtc *crtc)
697{
698 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000699 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000700 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Jani Nikulad58ded72015-03-11 11:51:04 +0200701 const struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000702
703 if (nv_two_heads(dev))
704 NVSetOwner(dev, nv_crtc->index);
705
Daniel Vetter9bc6db0d2016-11-15 22:58:19 +0100706 drm_crtc_vblank_off(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000707 funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
708
709 NVBlankScreen(dev, nv_crtc->index, true);
710
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300711 /* Some more preparation. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000712 NVWriteCRTC(dev, nv_crtc->index, NV_PCRTC_CONFIG, NV_PCRTC_CONFIG_START_ADDRESS_NON_VGA);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000713 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 uint32_t reg900 = NVReadRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900);
715 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_900, reg900 & ~0x10000);
716 }
717}
718
719static void nv_crtc_commit(struct drm_crtc *crtc)
720{
721 struct drm_device *dev = crtc->dev;
Jani Nikulad58ded72015-03-11 11:51:04 +0200722 const struct drm_crtc_helper_funcs *funcs = crtc->helper_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000723 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
724
Ben Skeggs017e6e22012-07-18 10:00:50 +1000725 nouveau_hw_load_state(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000726 nv04_crtc_mode_set_base(crtc, crtc->x, crtc->y, NULL);
727
728#ifdef __BIG_ENDIAN
729 /* turn on LFB swapping */
730 {
731 uint8_t tmp = NVReadVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR);
732 tmp |= MASK(NV_CIO_CRE_RCR_ENDIAN_BIG);
733 NVWriteVgaCrtc(dev, nv_crtc->index, NV_CIO_CRE_RCR, tmp);
734 }
735#endif
736
737 funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Daniel Vetter9bc6db0d2016-11-15 22:58:19 +0100738 drm_crtc_vblank_on(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739}
740
741static void nv_crtc_destroy(struct drm_crtc *crtc)
742{
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000743 struct nv04_display *disp = nv04_display(crtc->dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
745
Ben Skeggs6ee73862009-12-11 19:24:15 +1000746 if (!nv_crtc)
747 return;
748
749 drm_crtc_cleanup(crtc);
750
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000751 if (disp->image[nv_crtc->index])
752 nouveau_bo_unpin(disp->image[nv_crtc->index]);
753 nouveau_bo_ref(NULL, &disp->image[nv_crtc->index]);
754
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000755 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +0100756 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
758 kfree(nv_crtc);
759}
760
761static void
762nv_crtc_gamma_load(struct drm_crtc *crtc)
763{
764 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
765 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000766 struct rgb { uint8_t r, g, b; } __attribute__((packed)) *rgbs;
767 int i;
768
Ben Skeggs017e6e22012-07-18 10:00:50 +1000769 rgbs = (struct rgb *)nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index].DAC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000770 for (i = 0; i < 256; i++) {
771 rgbs[i].r = nv_crtc->lut.r[i] >> 8;
772 rgbs[i].g = nv_crtc->lut.g[i] >> 8;
773 rgbs[i].b = nv_crtc->lut.b[i] >> 8;
774 }
775
Ben Skeggs017e6e22012-07-18 10:00:50 +1000776 nouveau_hw_load_state_palette(dev, nv_crtc->index, &nv04_display(dev)->mode_reg);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000777}
778
779static void
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000780nv_crtc_disable(struct drm_crtc *crtc)
781{
782 struct nv04_display *disp = nv04_display(crtc->dev);
783 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
784 if (disp->image[nv_crtc->index])
785 nouveau_bo_unpin(disp->image[nv_crtc->index]);
786 nouveau_bo_ref(NULL, &disp->image[nv_crtc->index]);
787}
788
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200789static int
790nv_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
James Simmons72034252010-08-03 01:33:19 +0100791 uint32_t size)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000792{
793 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200794 int i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200796 for (i = 0; i < size; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 nv_crtc->lut.r[i] = r[i];
798 nv_crtc->lut.g[i] = g[i];
799 nv_crtc->lut.b[i] = b[i];
800 }
801
802 /* We need to know the depth before we upload, but it's possible to
803 * get called before a framebuffer is bound. If this is the case,
804 * mark the lut values as dirty by setting depth==0, and it'll be
805 * uploaded on the first mode_set_base()
806 */
Matt Roperf4510a22014-04-01 15:22:40 -0700807 if (!nv_crtc->base.primary->fb) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000808 nv_crtc->lut.depth = 0;
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200809 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000810 }
811
812 nv_crtc_gamma_load(crtc);
Maarten Lankhorst7ea77282016-06-07 12:49:30 +0200813
814 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000815}
816
817static int
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500818nv04_crtc_do_mode_set_base(struct drm_crtc *crtc,
819 struct drm_framebuffer *passed_fb,
820 int x, int y, bool atomic)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000821{
822 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
823 struct drm_device *dev = crtc->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +1000824 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs017e6e22012-07-18 10:00:50 +1000825 struct nv04_crtc_reg *regp = &nv04_display(dev)->mode_reg.crtc_reg[nv_crtc->index];
Emil Velikov0e83bb42011-08-25 21:36:51 +0100826 struct drm_framebuffer *drm_fb;
827 struct nouveau_framebuffer *fb;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000828 int arb_burst, arb_lwm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000829
Ben Skeggs77145f12012-07-31 16:16:21 +1000830 NV_DEBUG(drm, "index %d\n", nv_crtc->index);
Emil Velikov0e83bb42011-08-25 21:36:51 +0100831
832 /* no fb bound */
Matt Roperf4510a22014-04-01 15:22:40 -0700833 if (!atomic && !crtc->primary->fb) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000834 NV_DEBUG(drm, "No FB bound\n");
Emil Velikov0e83bb42011-08-25 21:36:51 +0100835 return 0;
836 }
837
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500838 /* If atomic, we want to switch to the fb we were passed, so
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000839 * now we update pointers to do that.
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500840 */
841 if (atomic) {
842 drm_fb = passed_fb;
843 fb = nouveau_framebuffer(passed_fb);
Emil Velikovf9ec8f62011-03-19 23:31:53 +0000844 } else {
Matt Roperf4510a22014-04-01 15:22:40 -0700845 drm_fb = crtc->primary->fb;
846 fb = nouveau_framebuffer(crtc->primary->fb);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 }
848
849 nv_crtc->fb.offset = fb->nvbo->bo.offset;
850
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200851 if (nv_crtc->lut.depth != drm_fb->format->depth) {
852 nv_crtc->lut.depth = drm_fb->format->depth;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000853 nv_crtc_gamma_load(crtc);
854 }
855
856 /* Update the framebuffer format. */
857 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] &= ~3;
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200858 regp->CRTC[NV_CIO_CRE_PIXEL_INDEX] |= (drm_fb->format->depth + 1) / 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000859 regp->ramdac_gen_ctrl &= ~NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200860 if (drm_fb->format->depth == 16)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000861 regp->ramdac_gen_ctrl |= NV_PRAMDAC_GENERAL_CONTROL_ALT_MODE_SEL;
862 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_PIXEL_INDEX);
863 NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_GENERAL_CONTROL,
864 regp->ramdac_gen_ctrl);
865
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200866 regp->CRTC[NV_CIO_CR_OFFSET_INDEX] = drm_fb->pitches[0] >> 3;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000867 regp->CRTC[NV_CIO_CRE_RPC0_INDEX] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200868 XLATE(drm_fb->pitches[0] >> 3, 8, NV_CIO_CRE_RPC0_OFFSET_10_8);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200869 regp->CRTC[NV_CIO_CRE_42] =
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200870 XLATE(drm_fb->pitches[0] / 8, 11, NV_CIO_CRE_42_OFFSET_11);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_RPC0_INDEX);
872 crtc_wr_cio_state(crtc, regp, NV_CIO_CR_OFFSET_INDEX);
Francisco Jerezc1003d92011-05-24 15:57:14 +0200873 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_42);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000874
875 /* Update the framebuffer location. */
876 regp->fb_start = nv_crtc->fb.offset & ~3;
Ville Syrjälä272725c2016-12-14 23:32:20 +0200877 regp->fb_start += (y * drm_fb->pitches[0]) + (x * drm_fb->format->cpp[0]);
Francisco Jerez5794b5f2010-10-22 04:39:14 +0200878 nv_set_crtc_base(dev, nv_crtc->index, regp->fb_start);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000879
880 /* Update the arbitration parameters. */
Ville Syrjälä272725c2016-12-14 23:32:20 +0200881 nouveau_calc_arb(dev, crtc->mode.clock, drm_fb->format->cpp[0] * 8,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000882 &arb_burst, &arb_lwm);
883
884 regp->CRTC[NV_CIO_CRE_FF_INDEX] = arb_burst;
885 regp->CRTC[NV_CIO_CRE_FFLWM__INDEX] = arb_lwm & 0xff;
886 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FF_INDEX);
887 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_FFLWM__INDEX);
888
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000889 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_KELVIN) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000890 regp->CRTC[NV_CIO_CRE_47] = arb_lwm >> 8;
891 crtc_wr_cio_state(crtc, regp, NV_CIO_CRE_47);
892 }
893
894 return 0;
895}
896
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500897static int
898nv04_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
899 struct drm_framebuffer *old_fb)
900{
Ben Skeggs78ae0ad2013-08-21 11:30:36 +1000901 int ret = nv_crtc_swap_fbs(crtc, old_fb);
902 if (ret)
903 return ret;
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500904 return nv04_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
905}
906
907static int
908nv04_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
909 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -0500910 int x, int y, enum mode_set_atomic state)
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500911{
Ben Skeggs77145f12012-07-31 16:16:21 +1000912 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
913 struct drm_device *dev = drm->dev;
Chris Balla424d762010-09-26 06:47:27 -0500914
Jason Wessel21c74a82010-10-13 14:09:44 -0500915 if (state == ENTER_ATOMIC_MODE_SET)
Ben Skeggs4b5098f2014-06-28 20:44:07 +1000916 nouveau_fbcon_accel_save_disable(dev);
Chris Balla424d762010-09-26 06:47:27 -0500917 else
Ben Skeggs4b5098f2014-06-28 20:44:07 +1000918 nouveau_fbcon_accel_restore(dev);
Chris Balla424d762010-09-26 06:47:27 -0500919
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500920 return nv04_crtc_do_mode_set_base(crtc, fb, x, y, true);
921}
922
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923static void nv04_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
924 struct nouveau_bo *dst)
925{
926 int width = nv_cursor_width(dev);
927 uint32_t pixel;
928 int i, j;
929
930 for (i = 0; i < width; i++) {
931 for (j = 0; j < width; j++) {
932 pixel = nouveau_bo_rd32(src, i*64 + j);
933
934 nouveau_bo_wr16(dst, i*width + j, (pixel & 0x80000000) >> 16
935 | (pixel & 0xf80000) >> 9
936 | (pixel & 0xf800) >> 6
937 | (pixel & 0xf8) >> 3);
938 }
939 }
940}
941
942static void nv11_cursor_upload(struct drm_device *dev, struct nouveau_bo *src,
943 struct nouveau_bo *dst)
944{
945 uint32_t pixel;
946 int alpha, i;
947
948 /* nv11+ supports premultiplied (PM), or non-premultiplied (NPM) alpha
949 * cursors (though NPM in combination with fp dithering may not work on
950 * nv11, from "nv" driver history)
951 * NPM mode needs NV_PCRTC_CURSOR_CONFIG_ALPHA_BLEND set and is what the
952 * blob uses, however we get given PM cursors so we use PM mode
953 */
954 for (i = 0; i < 64 * 64; i++) {
955 pixel = nouveau_bo_rd32(src, i);
956
957 /* hw gets unhappy if alpha <= rgb values. for a PM image "less
958 * than" shouldn't happen; fix "equal to" case by adding one to
959 * alpha channel (slightly inaccurate, but so is attempting to
960 * get back to NPM images, due to limits of integer precision)
961 */
962 alpha = pixel >> 24;
963 if (alpha > 0 && alpha < 255)
964 pixel = (pixel & 0x00ffffff) | ((alpha + 1) << 24);
965
966#ifdef __BIG_ENDIAN
967 {
Ben Skeggs77145f12012-07-31 16:16:21 +1000968 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000969
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000970 if (drm->client.device.info.chipset == 0x11) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000971 pixel = ((pixel & 0x000000ff) << 24) |
972 ((pixel & 0x0000ff00) << 8) |
973 ((pixel & 0x00ff0000) >> 8) |
974 ((pixel & 0xff000000) >> 24);
975 }
976 }
977#endif
978
979 nouveau_bo_wr32(dst, i, pixel);
980 }
981}
982
983static int
984nv04_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
985 uint32_t buffer_handle, uint32_t width, uint32_t height)
986{
Ben Skeggs77145f12012-07-31 16:16:21 +1000987 struct nouveau_drm *drm = nouveau_drm(crtc->dev);
988 struct drm_device *dev = drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000989 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
990 struct nouveau_bo *cursor = NULL;
991 struct drm_gem_object *gem;
992 int ret = 0;
993
Ben Skeggs6ee73862009-12-11 19:24:15 +1000994 if (!buffer_handle) {
995 nv_crtc->cursor.hide(nv_crtc, true);
996 return 0;
997 }
998
Marcin Slusarzb4fa9d02011-05-01 23:49:04 +0200999 if (width != 64 || height != 64)
1000 return -EINVAL;
1001
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001002 gem = drm_gem_object_lookup(file_priv, buffer_handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001003 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001004 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001005 cursor = nouveau_gem_object(gem);
1006
1007 ret = nouveau_bo_map(cursor);
1008 if (ret)
1009 goto out;
1010
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001011 if (drm->client.device.info.chipset >= 0x11)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001012 nv11_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
1013 else
1014 nv04_cursor_upload(dev, cursor, nv_crtc->cursor.nvbo);
1015
1016 nouveau_bo_unmap(cursor);
1017 nv_crtc->cursor.offset = nv_crtc->cursor.nvbo->bo.offset;
1018 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.offset);
1019 nv_crtc->cursor.show(nv_crtc, true);
1020out:
Luca Barbieribc9025b2010-02-09 05:49:12 +00001021 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001022 return ret;
1023}
1024
1025static int
1026nv04_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1027{
1028 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
1029
1030 nv_crtc->cursor.set_pos(nv_crtc, x, y);
1031 return 0;
1032}
1033
Dave Airlie5addcf02012-09-10 14:20:51 +10001034int
1035nouveau_crtc_set_config(struct drm_mode_set *set)
1036{
1037 struct drm_device *dev;
1038 struct nouveau_drm *drm;
1039 int ret;
1040 struct drm_crtc *crtc;
1041 bool active = false;
1042 if (!set || !set->crtc)
1043 return -EINVAL;
1044
1045 dev = set->crtc->dev;
1046
1047 /* get a pm reference here */
1048 ret = pm_runtime_get_sync(dev->dev);
Alexandre Courbotb6c42852014-02-12 14:00:59 +09001049 if (ret < 0 && ret != -EACCES)
Dave Airlie5addcf02012-09-10 14:20:51 +10001050 return ret;
1051
1052 ret = drm_crtc_helper_set_config(set);
1053
1054 drm = nouveau_drm(dev);
1055
1056 /* if we get here with no crtcs active then we can drop a reference */
1057 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1058 if (crtc->enabled)
1059 active = true;
1060 }
1061
1062 pm_runtime_mark_last_busy(dev->dev);
1063 /* if we have active crtcs and we don't have a power ref,
1064 take the current one */
1065 if (active && !drm->have_disp_power_ref) {
1066 drm->have_disp_power_ref = true;
1067 return ret;
1068 }
1069 /* if we have no active crtcs, then drop the power ref
1070 we got before */
1071 if (!active && drm->have_disp_power_ref) {
1072 pm_runtime_put_autosuspend(dev->dev);
1073 drm->have_disp_power_ref = false;
1074 }
1075 /* drop the power reference we got coming in here */
1076 pm_runtime_put_autosuspend(dev->dev);
1077 return ret;
1078}
1079
Ben Skeggs6ee73862009-12-11 19:24:15 +10001080static const struct drm_crtc_funcs nv04_crtc_funcs = {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001081 .cursor_set = nv04_crtc_cursor_set,
1082 .cursor_move = nv04_crtc_cursor_move,
1083 .gamma_set = nv_crtc_gamma_set,
Dave Airlie5addcf02012-09-10 14:20:51 +10001084 .set_config = nouveau_crtc_set_config,
Francisco Jerez332b2422010-10-20 23:35:40 +02001085 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001086 .destroy = nv_crtc_destroy,
1087};
1088
1089static const struct drm_crtc_helper_funcs nv04_crtc_helper_funcs = {
1090 .dpms = nv_crtc_dpms,
1091 .prepare = nv_crtc_prepare,
1092 .commit = nv_crtc_commit,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001093 .mode_set = nv_crtc_mode_set,
1094 .mode_set_base = nv04_crtc_mode_set_base,
Chris Ballbe64c2bb2010-09-26 06:47:24 -05001095 .mode_set_base_atomic = nv04_crtc_mode_set_base_atomic,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001096 .load_lut = nv_crtc_gamma_load,
Ben Skeggs78ae0ad2013-08-21 11:30:36 +10001097 .disable = nv_crtc_disable,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001098};
1099
1100int
1101nv04_crtc_create(struct drm_device *dev, int crtc_num)
1102{
1103 struct nouveau_crtc *nv_crtc;
1104 int ret, i;
1105
1106 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
1107 if (!nv_crtc)
1108 return -ENOMEM;
1109
1110 for (i = 0; i < 256; i++) {
1111 nv_crtc->lut.r[i] = i << 8;
1112 nv_crtc->lut.g[i] = i << 8;
1113 nv_crtc->lut.b[i] = i << 8;
1114 }
1115 nv_crtc->lut.depth = 0;
1116
1117 nv_crtc->index = crtc_num;
1118 nv_crtc->last_dpms = NV_DPMS_CLEARED;
1119
Daniel Vetter2c3d7712015-12-04 17:13:38 +01001120 nv_crtc->save = nv_crtc_save;
1121 nv_crtc->restore = nv_crtc_restore;
1122
Ben Skeggs6ee73862009-12-11 19:24:15 +10001123 drm_crtc_init(dev, &nv_crtc->base, &nv04_crtc_funcs);
1124 drm_crtc_helper_add(&nv_crtc->base, &nv04_crtc_helper_funcs);
1125 drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
1126
Ben Skeggsbab7cc12016-05-24 17:26:48 +10001127 ret = nouveau_bo_new(&nouveau_drm(dev)->client, 64*64*4, 0x100,
1128 TTM_PL_FLAG_VRAM, 0, 0x0000, NULL, NULL,
1129 &nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001130 if (!ret) {
Ben Skeggsad76b3f2014-11-10 11:24:27 +10001131 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, false);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001132 if (!ret) {
Ben Skeggs6ee73862009-12-11 19:24:15 +10001133 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01001134 if (ret)
1135 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
1136 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001137 if (ret)
1138 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
1139 }
1140
1141 nv04_cursor_init(nv_crtc);
1142
1143 return 0;
1144}