blob: 8eb108b28eb143a8a7b29f33831bf76c7e177fec [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2008 Maarten Maathuis.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27#include "drmP.h"
28#include "drm_mode.h"
29#include "drm_crtc_helper.h"
30
31#define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
32#include "nouveau_reg.h"
33#include "nouveau_drv.h"
34#include "nouveau_hw.h"
35#include "nouveau_encoder.h"
36#include "nouveau_crtc.h"
37#include "nouveau_fb.h"
38#include "nouveau_connector.h"
39#include "nv50_display.h"
40
Ben Skeggs549cd872011-10-06 11:51:45 +100041static int
42nv50_crtc_wait_complete(struct drm_crtc *crtc)
43{
44 struct drm_device *dev = crtc->dev;
45 struct drm_nouveau_private *dev_priv = dev->dev_private;
46 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
47 struct nv50_display *disp = nv50_display(dev);
48 struct nouveau_channel *evo = disp->master;
49 u64 start;
50 int ret;
51
52 ret = RING_SPACE(evo, 6);
53 if (ret)
54 return ret;
55 BEGIN_RING(evo, 0, 0x0084, 1);
56 OUT_RING (evo, 0x80000000);
57 BEGIN_RING(evo, 0, 0x0080, 1);
58 OUT_RING (evo, 0);
59 BEGIN_RING(evo, 0, 0x0084, 1);
60 OUT_RING (evo, 0x00000000);
61
62 nv_wo32(disp->ntfy, 0x000, 0x00000000);
63 FIRE_RING (evo);
64
65 start = ptimer->read(dev);
66 do {
67 if (nv_ro32(disp->ntfy, 0x000))
68 return 0;
69 } while (ptimer->read(dev) - start < 2000000000ULL);
70
71 return -EBUSY;
72}
73
Ben Skeggs6ee73862009-12-11 19:24:15 +100074static void
75nv50_crtc_lut_load(struct drm_crtc *crtc)
76{
77 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
78 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
79 int i;
80
Maarten Maathuisef2bb502009-12-13 16:53:12 +010081 NV_DEBUG_KMS(crtc->dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +100082
83 for (i = 0; i < 256; i++) {
84 writew(nv_crtc->lut.r[i] >> 2, lut + 8*i + 0);
85 writew(nv_crtc->lut.g[i] >> 2, lut + 8*i + 2);
86 writew(nv_crtc->lut.b[i] >> 2, lut + 8*i + 4);
87 }
88
89 if (nv_crtc->lut.depth == 30) {
90 writew(nv_crtc->lut.r[i - 1] >> 2, lut + 8*i + 0);
91 writew(nv_crtc->lut.g[i - 1] >> 2, lut + 8*i + 2);
92 writew(nv_crtc->lut.b[i - 1] >> 2, lut + 8*i + 4);
93 }
94}
95
96int
97nv50_crtc_blank(struct nouveau_crtc *nv_crtc, bool blanked)
98{
99 struct drm_device *dev = nv_crtc->base.dev;
100 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000101 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000102 int index = nv_crtc->index, ret;
103
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100104 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
105 NV_DEBUG_KMS(dev, "%s\n", blanked ? "blanked" : "unblanked");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106
107 if (blanked) {
108 nv_crtc->cursor.hide(nv_crtc, false);
109
110 ret = RING_SPACE(evo, dev_priv->chipset != 0x50 ? 7 : 5);
111 if (ret) {
112 NV_ERROR(dev, "no space while blanking crtc\n");
113 return ret;
114 }
115 BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
116 OUT_RING(evo, NV50_EVO_CRTC_CLUT_MODE_BLANK);
117 OUT_RING(evo, 0);
118 if (dev_priv->chipset != 0x50) {
119 BEGIN_RING(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
120 OUT_RING(evo, NV84_EVO_CRTC_CLUT_DMA_HANDLE_NONE);
121 }
122
123 BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
124 OUT_RING(evo, NV50_EVO_CRTC_FB_DMA_HANDLE_NONE);
125 } else {
126 if (nv_crtc->cursor.visible)
127 nv_crtc->cursor.show(nv_crtc, false);
128 else
129 nv_crtc->cursor.hide(nv_crtc, false);
130
131 ret = RING_SPACE(evo, dev_priv->chipset != 0x50 ? 10 : 8);
132 if (ret) {
133 NV_ERROR(dev, "no space while unblanking crtc\n");
134 return ret;
135 }
136 BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, CLUT_MODE), 2);
137 OUT_RING(evo, nv_crtc->lut.depth == 8 ?
138 NV50_EVO_CRTC_CLUT_MODE_OFF :
139 NV50_EVO_CRTC_CLUT_MODE_ON);
Ben Skeggs180cc302011-06-07 11:24:14 +1000140 OUT_RING(evo, nv_crtc->lut.nvbo->bo.offset >> 8);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141 if (dev_priv->chipset != 0x50) {
142 BEGIN_RING(evo, 0, NV84_EVO_CRTC(index, CLUT_DMA), 1);
143 OUT_RING(evo, NvEvoVRAM);
144 }
145
146 BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_OFFSET), 2);
147 OUT_RING(evo, nv_crtc->fb.offset >> 8);
148 OUT_RING(evo, 0);
149 BEGIN_RING(evo, 0, NV50_EVO_CRTC(index, FB_DMA), 1);
150 if (dev_priv->chipset != 0x50)
Ben Skeggs6d869512010-12-08 11:19:30 +1000151 if (nv_crtc->fb.tile_flags == 0x7a00 ||
152 nv_crtc->fb.tile_flags == 0xfe00)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000153 OUT_RING(evo, NvEvoFB32);
154 else
155 if (nv_crtc->fb.tile_flags == 0x7000)
156 OUT_RING(evo, NvEvoFB16);
157 else
Ben Skeggs6d869512010-12-08 11:19:30 +1000158 OUT_RING(evo, NvEvoVRAM_LP);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000159 else
Ben Skeggs6d869512010-12-08 11:19:30 +1000160 OUT_RING(evo, NvEvoVRAM_LP);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 }
162
163 nv_crtc->fb.blanked = blanked;
164 return 0;
165}
166
167static int
168nv50_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool on, bool update)
169{
170 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000171 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172 int ret;
173
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100174 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175
176 ret = RING_SPACE(evo, 2 + (update ? 2 : 0));
177 if (ret) {
178 NV_ERROR(dev, "no space while setting dither\n");
179 return ret;
180 }
181
182 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, DITHER_CTRL), 1);
183 if (on)
184 OUT_RING(evo, NV50_EVO_CRTC_DITHER_CTRL_ON);
185 else
186 OUT_RING(evo, NV50_EVO_CRTC_DITHER_CTRL_OFF);
187
188 if (update) {
189 BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
190 OUT_RING(evo, 0);
191 FIRE_RING(evo);
192 }
193
194 return 0;
195}
196
197struct nouveau_connector *
198nouveau_crtc_connector_get(struct nouveau_crtc *nv_crtc)
199{
200 struct drm_device *dev = nv_crtc->base.dev;
201 struct drm_connector *connector;
202 struct drm_crtc *crtc = to_drm_crtc(nv_crtc);
203
204 /* The safest approach is to find an encoder with the right crtc, that
205 * is also linked to a connector. */
206 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
207 if (connector->encoder)
208 if (connector->encoder->crtc == crtc)
209 return nouveau_connector(connector);
210 }
211
212 return NULL;
213}
214
215static int
216nv50_crtc_set_scale(struct nouveau_crtc *nv_crtc, int scaling_mode, bool update)
217{
218 struct nouveau_connector *nv_connector =
219 nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs549cd872011-10-06 11:51:45 +1000220 struct drm_crtc *crtc = &nv_crtc->base;
221 struct drm_device *dev = crtc->dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000222 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 struct drm_display_mode *native_mode = NULL;
Ben Skeggs549cd872011-10-06 11:51:45 +1000224 struct drm_display_mode *mode = &crtc->mode;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000225 uint32_t outX, outY, horiz, vert;
226 int ret;
227
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100228 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000229
230 switch (scaling_mode) {
231 case DRM_MODE_SCALE_NONE:
232 break;
233 default:
234 if (!nv_connector || !nv_connector->native_mode) {
235 NV_ERROR(dev, "No native mode, forcing panel scaling\n");
236 scaling_mode = DRM_MODE_SCALE_NONE;
237 } else {
238 native_mode = nv_connector->native_mode;
239 }
240 break;
241 }
242
243 switch (scaling_mode) {
244 case DRM_MODE_SCALE_ASPECT:
245 horiz = (native_mode->hdisplay << 19) / mode->hdisplay;
246 vert = (native_mode->vdisplay << 19) / mode->vdisplay;
247
248 if (vert > horiz) {
249 outX = (mode->hdisplay * horiz) >> 19;
250 outY = (mode->vdisplay * horiz) >> 19;
251 } else {
252 outX = (mode->hdisplay * vert) >> 19;
253 outY = (mode->vdisplay * vert) >> 19;
254 }
255 break;
256 case DRM_MODE_SCALE_FULLSCREEN:
257 outX = native_mode->hdisplay;
258 outY = native_mode->vdisplay;
259 break;
260 case DRM_MODE_SCALE_CENTER:
261 case DRM_MODE_SCALE_NONE:
262 default:
263 outX = mode->hdisplay;
264 outY = mode->vdisplay;
265 break;
266 }
267
Ben Skeggs549cd872011-10-06 11:51:45 +1000268 ret = RING_SPACE(evo, 5);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000269 if (ret)
270 return ret;
271
272 /* Got a better name for SCALER_ACTIVE? */
273 /* One day i've got to really figure out why this is needed. */
274 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CTRL), 1);
275 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) ||
276 (mode->flags & DRM_MODE_FLAG_INTERLACE) ||
277 mode->hdisplay != outX || mode->vdisplay != outY) {
278 OUT_RING(evo, NV50_EVO_CRTC_SCALE_CTRL_ACTIVE);
279 } else {
280 OUT_RING(evo, NV50_EVO_CRTC_SCALE_CTRL_INACTIVE);
281 }
282
283 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_RES1), 2);
284 OUT_RING(evo, outY << 16 | outX);
285 OUT_RING(evo, outY << 16 | outX);
286
287 if (update) {
Ben Skeggs549cd872011-10-06 11:51:45 +1000288 nv50_display_flip_stop(crtc);
289 nv50_crtc_wait_complete(crtc);
290 nv50_display_flip_next(crtc, crtc->fb, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000291 }
292
293 return 0;
294}
295
296int
297nv50_crtc_set_clock(struct drm_device *dev, int head, int pclk)
298{
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000299 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggse9ebb682010-04-28 14:07:06 +1000300 struct pll_lims pll;
Ben Skeggs5b321652010-09-24 09:17:02 +1000301 uint32_t reg1, reg2;
Ben Skeggse9ebb682010-04-28 14:07:06 +1000302 int ret, N1, M1, N2, M2, P;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303
Ben Skeggs5b321652010-09-24 09:17:02 +1000304 ret = get_pll_limits(dev, PLL_VPLL0 + head, &pll);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000305 if (ret)
306 return ret;
307
Ben Skeggse9ebb682010-04-28 14:07:06 +1000308 if (pll.vco2.maxfreq) {
309 ret = nv50_calc_pll(dev, &pll, pclk, &N1, &M1, &N2, &M2, &P);
310 if (ret <= 0)
311 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000312
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000313 NV_DEBUG(dev, "pclk %d out %d NM1 %d %d NM2 %d %d P %d\n",
Ben Skeggse9ebb682010-04-28 14:07:06 +1000314 pclk, ret, N1, M1, N2, M2, P);
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000315
Ben Skeggs5b321652010-09-24 09:17:02 +1000316 reg1 = nv_rd32(dev, pll.reg + 4) & 0xff00ff00;
317 reg2 = nv_rd32(dev, pll.reg + 8) & 0x8000ff00;
318 nv_wr32(dev, pll.reg + 0, 0x10000611);
319 nv_wr32(dev, pll.reg + 4, reg1 | (M1 << 16) | N1);
320 nv_wr32(dev, pll.reg + 8, reg2 | (P << 28) | (M2 << 16) | N2);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000321 } else
322 if (dev_priv->chipset < NV_C0) {
Ben Skeggs52eba8d2011-04-28 02:34:21 +1000323 ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
Ben Skeggse9ebb682010-04-28 14:07:06 +1000324 if (ret <= 0)
325 return 0;
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000326
Ben Skeggse9ebb682010-04-28 14:07:06 +1000327 NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
328 pclk, ret, N1, N2, M1, P);
329
Ben Skeggs5b321652010-09-24 09:17:02 +1000330 reg1 = nv_rd32(dev, pll.reg + 4) & 0xffc00000;
331 nv_wr32(dev, pll.reg + 0, 0x50000610);
332 nv_wr32(dev, pll.reg + 4, reg1 | (P << 16) | (M1 << 8) | N1);
333 nv_wr32(dev, pll.reg + 8, N2);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000334 } else {
Ben Skeggs52eba8d2011-04-28 02:34:21 +1000335 ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000336 if (ret <= 0)
337 return 0;
338
339 NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
340 pclk, ret, N1, N2, M1, P);
341
Ben Skeggs5b321652010-09-24 09:17:02 +1000342 nv_mask(dev, pll.reg + 0x0c, 0x00000000, 0x00000100);
343 nv_wr32(dev, pll.reg + 0x04, (P << 16) | (N1 << 8) | M1);
344 nv_wr32(dev, pll.reg + 0x10, N2 << 16);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000345 }
346
347 return 0;
348}
349
350static void
351nv50_crtc_destroy(struct drm_crtc *crtc)
352{
Marcin Slusarzdd19e442010-01-30 15:41:00 +0100353 struct drm_device *dev;
354 struct nouveau_crtc *nv_crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000355
356 if (!crtc)
357 return;
358
Marcin Slusarzdd19e442010-01-30 15:41:00 +0100359 dev = crtc->dev;
360 nv_crtc = nouveau_crtc(crtc);
361
362 NV_DEBUG_KMS(dev, "\n");
363
Ben Skeggs6ee73862009-12-11 19:24:15 +1000364 drm_crtc_cleanup(&nv_crtc->base);
365
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000366 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000367 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000368 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000369 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
370 kfree(nv_crtc->mode);
371 kfree(nv_crtc);
372}
373
374int
375nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
376 uint32_t buffer_handle, uint32_t width, uint32_t height)
377{
378 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000379 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
380 struct nouveau_bo *cursor = NULL;
381 struct drm_gem_object *gem;
382 int ret = 0, i;
383
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 if (!buffer_handle) {
385 nv_crtc->cursor.hide(nv_crtc, true);
386 return 0;
387 }
388
Marcin Slusarzb4fa9d02011-05-01 23:49:04 +0200389 if (width != 64 || height != 64)
390 return -EINVAL;
391
Ben Skeggs6ee73862009-12-11 19:24:15 +1000392 gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
393 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100394 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000395 cursor = nouveau_gem_object(gem);
396
397 ret = nouveau_bo_map(cursor);
398 if (ret)
399 goto out;
400
401 /* The simple will do for now. */
402 for (i = 0; i < 64 * 64; i++)
403 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, nouveau_bo_rd32(cursor, i));
404
405 nouveau_bo_unmap(cursor);
406
Ben Skeggs180cc302011-06-07 11:24:14 +1000407 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000408 nv_crtc->cursor.show(nv_crtc, true);
409
410out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000411 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000412 return ret;
413}
414
415int
416nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
417{
418 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
419
420 nv_crtc->cursor.set_pos(nv_crtc, x, y);
421 return 0;
422}
423
424static void
425nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
James Simmons72034252010-08-03 01:33:19 +0100426 uint32_t start, uint32_t size)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000427{
James Simmons72034252010-08-03 01:33:19 +0100428 int end = (start + size > 256) ? 256 : start + size, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000430
James Simmons72034252010-08-03 01:33:19 +0100431 for (i = start; i < end; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000432 nv_crtc->lut.r[i] = r[i];
433 nv_crtc->lut.g[i] = g[i];
434 nv_crtc->lut.b[i] = b[i];
435 }
436
437 /* We need to know the depth before we upload, but it's possible to
438 * get called before a framebuffer is bound. If this is the case,
439 * mark the lut values as dirty by setting depth==0, and it'll be
440 * uploaded on the first mode_set_base()
441 */
442 if (!nv_crtc->base.fb) {
443 nv_crtc->lut.depth = 0;
444 return;
445 }
446
447 nv50_crtc_lut_load(crtc);
448}
449
450static void
451nv50_crtc_save(struct drm_crtc *crtc)
452{
453 NV_ERROR(crtc->dev, "!!\n");
454}
455
456static void
457nv50_crtc_restore(struct drm_crtc *crtc)
458{
459 NV_ERROR(crtc->dev, "!!\n");
460}
461
462static const struct drm_crtc_funcs nv50_crtc_funcs = {
463 .save = nv50_crtc_save,
464 .restore = nv50_crtc_restore,
465 .cursor_set = nv50_crtc_cursor_set,
466 .cursor_move = nv50_crtc_cursor_move,
467 .gamma_set = nv50_crtc_gamma_set,
468 .set_config = drm_crtc_helper_set_config,
Francisco Jerez332b2422010-10-20 23:35:40 +0200469 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000470 .destroy = nv50_crtc_destroy,
471};
472
473static void
474nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
475{
476}
477
478static void
479nv50_crtc_prepare(struct drm_crtc *crtc)
480{
481 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
482 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000483
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100484 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000485
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000486 nv50_display_flip_stop(crtc);
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200487 drm_vblank_pre_modeset(dev, nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000488 nv50_crtc_blank(nv_crtc, true);
489}
490
491static void
492nv50_crtc_commit(struct drm_crtc *crtc)
493{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000495 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000496
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100497 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000498
499 nv50_crtc_blank(nv_crtc, false);
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200500 drm_vblank_post_modeset(dev, nv_crtc->index);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000501 nv50_crtc_wait_complete(crtc);
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000502 nv50_display_flip_next(crtc, crtc->fb, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000503}
504
505static bool
506nv50_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
507 struct drm_display_mode *adjusted_mode)
508{
509 return true;
510}
511
512static int
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500513nv50_crtc_do_mode_set_base(struct drm_crtc *crtc,
514 struct drm_framebuffer *passed_fb,
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000515 int x, int y, bool atomic)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000516{
517 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
518 struct drm_device *dev = nv_crtc->base.dev;
519 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000520 struct nouveau_channel *evo = nv50_display(dev)->master;
Emil Velikovffbc5592011-08-21 22:48:12 +0100521 struct drm_framebuffer *drm_fb;
522 struct nouveau_framebuffer *fb;
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000523 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000524
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100525 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000526
Emil Velikovffbc5592011-08-21 22:48:12 +0100527 /* no fb bound */
528 if (!atomic && !crtc->fb) {
529 NV_DEBUG_KMS(dev, "No FB bound\n");
530 return 0;
531 }
532
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500533 /* If atomic, we want to switch to the fb we were passed, so
534 * now we update pointers to do that. (We don't pin; just
535 * assume we're already pinned and update the base address.)
536 */
537 if (atomic) {
538 drm_fb = passed_fb;
539 fb = nouveau_framebuffer(passed_fb);
Emil Velikovf9ec8f62011-03-19 23:31:53 +0000540 } else {
Emil Velikovffbc5592011-08-21 22:48:12 +0100541 drm_fb = crtc->fb;
542 fb = nouveau_framebuffer(crtc->fb);
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500543 /* If not atomic, we can go ahead and pin, and unpin the
544 * old fb we were passed.
545 */
546 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
547 if (ret)
548 return ret;
549
550 if (passed_fb) {
551 struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
552 nouveau_bo_unpin(ofb->nvbo);
553 }
554 }
555
Ben Skeggs180cc302011-06-07 11:24:14 +1000556 nv_crtc->fb.offset = fb->nvbo->bo.offset;
Francisco Jerezf13b3262010-10-10 06:01:08 +0200557 nv_crtc->fb.tile_flags = nouveau_bo_tile_layout(fb->nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000558 nv_crtc->fb.cpp = drm_fb->bits_per_pixel / 8;
559 if (!nv_crtc->fb.blanked && dev_priv->chipset != 0x50) {
560 ret = RING_SPACE(evo, 2);
561 if (ret)
562 return ret;
563
564 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_DMA), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000565 OUT_RING (evo, fb->r_dma);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000566 }
567
568 ret = RING_SPACE(evo, 12);
569 if (ret)
570 return ret;
571
572 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_OFFSET), 5);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000573 OUT_RING (evo, nv_crtc->fb.offset >> 8);
574 OUT_RING (evo, 0);
575 OUT_RING (evo, (drm_fb->height << 16) | drm_fb->width);
576 OUT_RING (evo, fb->r_pitch);
577 OUT_RING (evo, fb->r_format);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000578
579 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLUT_MODE), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000580 OUT_RING (evo, fb->base.depth == 8 ?
581 NV50_EVO_CRTC_CLUT_MODE_OFF : NV50_EVO_CRTC_CLUT_MODE_ON);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582
583 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, COLOR_CTRL), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000584 OUT_RING (evo, NV50_EVO_CRTC_COLOR_CTRL_COLOR);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000585 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_POS), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000586 OUT_RING (evo, (y << 16) | x);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000587
588 if (nv_crtc->lut.depth != fb->base.depth) {
589 nv_crtc->lut.depth = fb->base.depth;
590 nv50_crtc_lut_load(crtc);
591 }
592
Ben Skeggs6ee73862009-12-11 19:24:15 +1000593 return 0;
594}
595
596static int
597nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
598 struct drm_display_mode *adjusted_mode, int x, int y,
599 struct drm_framebuffer *old_fb)
600{
601 struct drm_device *dev = crtc->dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000602 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000603 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
604 struct nouveau_connector *nv_connector = NULL;
605 uint32_t hsync_dur, vsync_dur, hsync_start_to_end, vsync_start_to_end;
606 uint32_t hunk1, vunk1, vunk2a, vunk2b;
607 int ret;
608
609 /* Find the connector attached to this CRTC */
610 nv_connector = nouveau_crtc_connector_get(nv_crtc);
611
612 *nv_crtc->mode = *adjusted_mode;
613
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100614 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615
616 hsync_dur = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
617 vsync_dur = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
618 hsync_start_to_end = adjusted_mode->htotal - adjusted_mode->hsync_start;
619 vsync_start_to_end = adjusted_mode->vtotal - adjusted_mode->vsync_start;
620 /* I can't give this a proper name, anyone else can? */
621 hunk1 = adjusted_mode->htotal -
622 adjusted_mode->hsync_start + adjusted_mode->hdisplay;
623 vunk1 = adjusted_mode->vtotal -
624 adjusted_mode->vsync_start + adjusted_mode->vdisplay;
625 /* Another strange value, this time only for interlaced adjusted_modes. */
626 vunk2a = 2 * adjusted_mode->vtotal -
627 adjusted_mode->vsync_start + adjusted_mode->vdisplay;
628 vunk2b = adjusted_mode->vtotal -
629 adjusted_mode->vsync_start + adjusted_mode->vtotal;
630
631 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
632 vsync_dur /= 2;
633 vsync_start_to_end /= 2;
634 vunk1 /= 2;
635 vunk2a /= 2;
636 vunk2b /= 2;
637 /* magic */
638 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN) {
639 vsync_start_to_end -= 1;
640 vunk1 -= 1;
641 vunk2a -= 1;
642 vunk2b -= 1;
643 }
644 }
645
646 ret = RING_SPACE(evo, 17);
647 if (ret)
648 return ret;
649
650 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLOCK), 2);
651 OUT_RING(evo, adjusted_mode->clock | 0x800000);
652 OUT_RING(evo, (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 0);
653
654 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, DISPLAY_START), 5);
655 OUT_RING(evo, 0);
656 OUT_RING(evo, (adjusted_mode->vtotal << 16) | adjusted_mode->htotal);
657 OUT_RING(evo, (vsync_dur - 1) << 16 | (hsync_dur - 1));
658 OUT_RING(evo, (vsync_start_to_end - 1) << 16 |
659 (hsync_start_to_end - 1));
660 OUT_RING(evo, (vunk1 - 1) << 16 | (hunk1 - 1));
661
662 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
663 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK0824), 1);
664 OUT_RING(evo, (vunk2b - 1) << 16 | (vunk2a - 1));
665 } else {
666 OUT_RING(evo, 0);
667 OUT_RING(evo, 0);
668 }
669
670 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK082C), 1);
671 OUT_RING(evo, 0);
672
673 /* This is the actual resolution of the mode. */
674 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, REAL_RES), 1);
675 OUT_RING(evo, (mode->vdisplay << 16) | mode->hdisplay);
676 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CENTER_OFFSET), 1);
677 OUT_RING(evo, NV50_EVO_CRTC_SCALE_CENTER_OFFSET_VAL(0, 0));
678
679 nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering, false);
680 nv_crtc->set_scale(nv_crtc, nv_connector->scaling_mode, false);
681
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000682 return nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000683}
684
685static int
686nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
687 struct drm_framebuffer *old_fb)
688{
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000689 int ret;
690
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000691 nv50_display_flip_stop(crtc);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000692 ret = nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
693 if (ret)
694 return ret;
695
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000696 ret = nv50_crtc_wait_complete(crtc);
697 if (ret)
698 return ret;
699
700 return nv50_display_flip_next(crtc, crtc->fb, NULL);
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500701}
702
703static int
704nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
705 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -0500706 int x, int y, enum mode_set_atomic state)
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500707{
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000708 int ret;
709
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000710 nv50_display_flip_stop(crtc);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000711 ret = nv50_crtc_do_mode_set_base(crtc, fb, x, y, true);
712 if (ret)
713 return ret;
714
715 return nv50_crtc_wait_complete(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716}
717
718static const struct drm_crtc_helper_funcs nv50_crtc_helper_funcs = {
719 .dpms = nv50_crtc_dpms,
720 .prepare = nv50_crtc_prepare,
721 .commit = nv50_crtc_commit,
722 .mode_fixup = nv50_crtc_mode_fixup,
723 .mode_set = nv50_crtc_mode_set,
724 .mode_set_base = nv50_crtc_mode_set_base,
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500725 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000726 .load_lut = nv50_crtc_lut_load,
727};
728
729int
730nv50_crtc_create(struct drm_device *dev, int index)
731{
732 struct nouveau_crtc *nv_crtc = NULL;
733 int ret, i;
734
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100735 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736
737 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
738 if (!nv_crtc)
739 return -ENOMEM;
740
741 nv_crtc->mode = kzalloc(sizeof(*nv_crtc->mode), GFP_KERNEL);
742 if (!nv_crtc->mode) {
743 kfree(nv_crtc);
744 return -ENOMEM;
745 }
746
747 /* Default CLUT parameters, will be activated on the hw upon
748 * first mode set.
749 */
750 for (i = 0; i < 256; i++) {
751 nv_crtc->lut.r[i] = i << 8;
752 nv_crtc->lut.g[i] = i << 8;
753 nv_crtc->lut.b[i] = i << 8;
754 }
755 nv_crtc->lut.depth = 0;
756
Ben Skeggs7375c952011-06-07 14:21:29 +1000757 ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsd550c412011-02-16 08:41:56 +1000758 0, 0x0000, &nv_crtc->lut.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 if (!ret) {
760 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
761 if (!ret)
762 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
763 if (ret)
764 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
765 }
766
767 if (ret) {
768 kfree(nv_crtc->mode);
769 kfree(nv_crtc);
770 return ret;
771 }
772
773 nv_crtc->index = index;
774
775 /* set function pointers */
776 nv_crtc->set_dither = nv50_crtc_set_dither;
777 nv_crtc->set_scale = nv50_crtc_set_scale;
778
779 drm_crtc_init(dev, &nv_crtc->base, &nv50_crtc_funcs);
780 drm_crtc_helper_add(&nv_crtc->base, &nv50_crtc_helper_funcs);
781 drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
782
Ben Skeggs7375c952011-06-07 14:21:29 +1000783 ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsd550c412011-02-16 08:41:56 +1000784 0, 0x0000, &nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000785 if (!ret) {
786 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
787 if (!ret)
788 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
789 if (ret)
790 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
791 }
792
793 nv50_cursor_init(nv_crtc);
794 return 0;
795}