blob: e426a9b9865a4f512902c67cdc1e2422ddc1cda6 [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{
Ben Skeggsb29caa52011-10-06 13:29:05 +1000218 struct nouveau_connector *nv_connector;
Ben Skeggs549cd872011-10-06 11:51:45 +1000219 struct drm_crtc *crtc = &nv_crtc->base;
220 struct drm_device *dev = crtc->dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000221 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs549cd872011-10-06 11:51:45 +1000222 struct drm_display_mode *mode = &crtc->mode;
Ben Skeggsb29caa52011-10-06 13:29:05 +1000223 u32 ctrl = 0, oX, oY;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224 int ret;
225
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100226 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000227
Ben Skeggsb29caa52011-10-06 13:29:05 +1000228 nv_connector = nouveau_crtc_connector_get(nv_crtc);
229 if (!nv_connector || !nv_connector->native_mode) {
230 NV_ERROR(dev, "no native mode, forcing panel scaling\n");
231 scaling_mode = DRM_MODE_SCALE_NONE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000232 }
233
Ben Skeggsb29caa52011-10-06 13:29:05 +1000234 /* start off at the resolution we programmed the crtc for, this
235 * effectively handles NONE/FULL scaling
236 */
237 if (scaling_mode != DRM_MODE_SCALE_NONE) {
238 oX = nv_connector->native_mode->hdisplay;
239 oY = nv_connector->native_mode->vdisplay;
240 } else {
241 oX = mode->hdisplay;
242 oY = mode->vdisplay;
243 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244
Ben Skeggsb29caa52011-10-06 13:29:05 +1000245 /* add overscan compensation if necessary, will keep the aspect
246 * ratio the same as the backend mode unless overridden by the
247 * user setting both hborder and vborder properties.
248 */
249 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
250 (nv_connector->underscan == UNDERSCAN_AUTO &&
251 nv_connector->edid &&
252 drm_detect_hdmi_monitor(nv_connector->edid)))) {
253 u32 bX = nv_connector->underscan_hborder;
254 u32 bY = nv_connector->underscan_vborder;
255 u32 aspect = (oY << 19) / oX;
256
257 if (bX) {
258 oX -= (bX * 2);
259 if (bY) oY -= (bY * 2);
260 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000261 } else {
Ben Skeggsb29caa52011-10-06 13:29:05 +1000262 oX -= (oX >> 4) + 32;
263 if (bY) oY -= (bY * 2);
264 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 }
Ben Skeggsb29caa52011-10-06 13:29:05 +1000266 }
267
268 /* handle CENTER/ASPECT scaling, taking into account the areas
269 * removed already for overscan compensation
270 */
271 switch (scaling_mode) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272 case DRM_MODE_SCALE_CENTER:
Ben Skeggsb29caa52011-10-06 13:29:05 +1000273 oX = min((u32)mode->hdisplay, oX);
274 oY = min((u32)mode->vdisplay, oY);
275 /* fall-through */
276 case DRM_MODE_SCALE_ASPECT:
277 if (oY < oX) {
278 u32 aspect = (mode->hdisplay << 19) / mode->vdisplay;
279 oX = ((oY * aspect) + (aspect / 2)) >> 19;
280 } else {
281 u32 aspect = (mode->vdisplay << 19) / mode->hdisplay;
282 oY = ((oX * aspect) + (aspect / 2)) >> 19;
283 }
284 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000285 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286 break;
287 }
288
Ben Skeggsb29caa52011-10-06 13:29:05 +1000289 if (mode->hdisplay != oX || mode->vdisplay != oY ||
290 mode->flags & DRM_MODE_FLAG_INTERLACE ||
291 mode->flags & DRM_MODE_FLAG_DBLSCAN)
292 ctrl |= NV50_EVO_CRTC_SCALE_CTRL_ACTIVE;
293
Ben Skeggs549cd872011-10-06 11:51:45 +1000294 ret = RING_SPACE(evo, 5);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000295 if (ret)
296 return ret;
297
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CTRL), 1);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000299 OUT_RING (evo, ctrl);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000300 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_RES1), 2);
Ben Skeggsb29caa52011-10-06 13:29:05 +1000301 OUT_RING (evo, oY << 16 | oX);
302 OUT_RING (evo, oY << 16 | oX);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303
304 if (update) {
Ben Skeggs549cd872011-10-06 11:51:45 +1000305 nv50_display_flip_stop(crtc);
306 nv50_crtc_wait_complete(crtc);
307 nv50_display_flip_next(crtc, crtc->fb, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000308 }
309
310 return 0;
311}
312
313int
314nv50_crtc_set_clock(struct drm_device *dev, int head, int pclk)
315{
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000316 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggse9ebb682010-04-28 14:07:06 +1000317 struct pll_lims pll;
Ben Skeggs5b321652010-09-24 09:17:02 +1000318 uint32_t reg1, reg2;
Ben Skeggse9ebb682010-04-28 14:07:06 +1000319 int ret, N1, M1, N2, M2, P;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000320
Ben Skeggs5b321652010-09-24 09:17:02 +1000321 ret = get_pll_limits(dev, PLL_VPLL0 + head, &pll);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322 if (ret)
323 return ret;
324
Ben Skeggse9ebb682010-04-28 14:07:06 +1000325 if (pll.vco2.maxfreq) {
326 ret = nv50_calc_pll(dev, &pll, pclk, &N1, &M1, &N2, &M2, &P);
327 if (ret <= 0)
328 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000330 NV_DEBUG(dev, "pclk %d out %d NM1 %d %d NM2 %d %d P %d\n",
Ben Skeggse9ebb682010-04-28 14:07:06 +1000331 pclk, ret, N1, M1, N2, M2, P);
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000332
Ben Skeggs5b321652010-09-24 09:17:02 +1000333 reg1 = nv_rd32(dev, pll.reg + 4) & 0xff00ff00;
334 reg2 = nv_rd32(dev, pll.reg + 8) & 0x8000ff00;
335 nv_wr32(dev, pll.reg + 0, 0x10000611);
336 nv_wr32(dev, pll.reg + 4, reg1 | (M1 << 16) | N1);
337 nv_wr32(dev, pll.reg + 8, reg2 | (P << 28) | (M2 << 16) | N2);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000338 } else
339 if (dev_priv->chipset < NV_C0) {
Ben Skeggs52eba8d2011-04-28 02:34:21 +1000340 ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
Ben Skeggse9ebb682010-04-28 14:07:06 +1000341 if (ret <= 0)
342 return 0;
Ben Skeggs17b96cc2010-04-23 03:53:42 +1000343
Ben Skeggse9ebb682010-04-28 14:07:06 +1000344 NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
345 pclk, ret, N1, N2, M1, P);
346
Ben Skeggs5b321652010-09-24 09:17:02 +1000347 reg1 = nv_rd32(dev, pll.reg + 4) & 0xffc00000;
348 nv_wr32(dev, pll.reg + 0, 0x50000610);
349 nv_wr32(dev, pll.reg + 4, reg1 | (P << 16) | (M1 << 8) | N1);
350 nv_wr32(dev, pll.reg + 8, N2);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000351 } else {
Ben Skeggs52eba8d2011-04-28 02:34:21 +1000352 ret = nva3_calc_pll(dev, &pll, pclk, &N1, &N2, &M1, &P);
Ben Skeggs1ac7b522010-08-04 22:08:03 +1000353 if (ret <= 0)
354 return 0;
355
356 NV_DEBUG(dev, "pclk %d out %d N %d fN 0x%04x M %d P %d\n",
357 pclk, ret, N1, N2, M1, P);
358
Ben Skeggs5b321652010-09-24 09:17:02 +1000359 nv_mask(dev, pll.reg + 0x0c, 0x00000000, 0x00000100);
360 nv_wr32(dev, pll.reg + 0x04, (P << 16) | (N1 << 8) | M1);
361 nv_wr32(dev, pll.reg + 0x10, N2 << 16);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000362 }
363
364 return 0;
365}
366
367static void
368nv50_crtc_destroy(struct drm_crtc *crtc)
369{
Marcin Slusarzdd19e442010-01-30 15:41:00 +0100370 struct drm_device *dev;
371 struct nouveau_crtc *nv_crtc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000372
373 if (!crtc)
374 return;
375
Marcin Slusarzdd19e442010-01-30 15:41:00 +0100376 dev = crtc->dev;
377 nv_crtc = nouveau_crtc(crtc);
378
379 NV_DEBUG_KMS(dev, "\n");
380
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381 drm_crtc_cleanup(&nv_crtc->base);
382
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000383 nouveau_bo_unmap(nv_crtc->lut.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000385 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000386 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
387 kfree(nv_crtc->mode);
388 kfree(nv_crtc);
389}
390
391int
392nv50_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
393 uint32_t buffer_handle, uint32_t width, uint32_t height)
394{
395 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000396 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
397 struct nouveau_bo *cursor = NULL;
398 struct drm_gem_object *gem;
399 int ret = 0, i;
400
Ben Skeggs6ee73862009-12-11 19:24:15 +1000401 if (!buffer_handle) {
402 nv_crtc->cursor.hide(nv_crtc, true);
403 return 0;
404 }
405
Marcin Slusarzb4fa9d02011-05-01 23:49:04 +0200406 if (width != 64 || height != 64)
407 return -EINVAL;
408
Ben Skeggs6ee73862009-12-11 19:24:15 +1000409 gem = drm_gem_object_lookup(dev, file_priv, buffer_handle);
410 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100411 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000412 cursor = nouveau_gem_object(gem);
413
414 ret = nouveau_bo_map(cursor);
415 if (ret)
416 goto out;
417
418 /* The simple will do for now. */
419 for (i = 0; i < 64 * 64; i++)
420 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, nouveau_bo_rd32(cursor, i));
421
422 nouveau_bo_unmap(cursor);
423
Ben Skeggs180cc302011-06-07 11:24:14 +1000424 nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000425 nv_crtc->cursor.show(nv_crtc, true);
426
427out:
Luca Barbieribc9025b2010-02-09 05:49:12 +0000428 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 return ret;
430}
431
432int
433nv50_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
434{
435 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
436
437 nv_crtc->cursor.set_pos(nv_crtc, x, y);
438 return 0;
439}
440
441static void
442nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
James Simmons72034252010-08-03 01:33:19 +0100443 uint32_t start, uint32_t size)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000444{
James Simmons72034252010-08-03 01:33:19 +0100445 int end = (start + size > 256) ? 256 : start + size, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000446 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000447
James Simmons72034252010-08-03 01:33:19 +0100448 for (i = start; i < end; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000449 nv_crtc->lut.r[i] = r[i];
450 nv_crtc->lut.g[i] = g[i];
451 nv_crtc->lut.b[i] = b[i];
452 }
453
454 /* We need to know the depth before we upload, but it's possible to
455 * get called before a framebuffer is bound. If this is the case,
456 * mark the lut values as dirty by setting depth==0, and it'll be
457 * uploaded on the first mode_set_base()
458 */
459 if (!nv_crtc->base.fb) {
460 nv_crtc->lut.depth = 0;
461 return;
462 }
463
464 nv50_crtc_lut_load(crtc);
465}
466
467static void
468nv50_crtc_save(struct drm_crtc *crtc)
469{
470 NV_ERROR(crtc->dev, "!!\n");
471}
472
473static void
474nv50_crtc_restore(struct drm_crtc *crtc)
475{
476 NV_ERROR(crtc->dev, "!!\n");
477}
478
479static const struct drm_crtc_funcs nv50_crtc_funcs = {
480 .save = nv50_crtc_save,
481 .restore = nv50_crtc_restore,
482 .cursor_set = nv50_crtc_cursor_set,
483 .cursor_move = nv50_crtc_cursor_move,
484 .gamma_set = nv50_crtc_gamma_set,
485 .set_config = drm_crtc_helper_set_config,
Francisco Jerez332b2422010-10-20 23:35:40 +0200486 .page_flip = nouveau_crtc_page_flip,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000487 .destroy = nv50_crtc_destroy,
488};
489
490static void
491nv50_crtc_dpms(struct drm_crtc *crtc, int mode)
492{
493}
494
495static void
496nv50_crtc_prepare(struct drm_crtc *crtc)
497{
498 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
499 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000500
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100501 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000502
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000503 nv50_display_flip_stop(crtc);
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200504 drm_vblank_pre_modeset(dev, nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000505 nv50_crtc_blank(nv_crtc, true);
506}
507
508static void
509nv50_crtc_commit(struct drm_crtc *crtc)
510{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000511 struct drm_device *dev = crtc->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000512 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000513
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100514 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000515
516 nv50_crtc_blank(nv_crtc, false);
Francisco Jerez1c180fa2010-10-25 03:30:34 +0200517 drm_vblank_post_modeset(dev, nv_crtc->index);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000518 nv50_crtc_wait_complete(crtc);
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000519 nv50_display_flip_next(crtc, crtc->fb, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000520}
521
522static bool
523nv50_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
524 struct drm_display_mode *adjusted_mode)
525{
526 return true;
527}
528
529static int
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500530nv50_crtc_do_mode_set_base(struct drm_crtc *crtc,
531 struct drm_framebuffer *passed_fb,
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000532 int x, int y, bool atomic)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000533{
534 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
535 struct drm_device *dev = nv_crtc->base.dev;
536 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000537 struct nouveau_channel *evo = nv50_display(dev)->master;
Emil Velikovffbc5592011-08-21 22:48:12 +0100538 struct drm_framebuffer *drm_fb;
539 struct nouveau_framebuffer *fb;
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000540 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000541
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100542 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543
Emil Velikovffbc5592011-08-21 22:48:12 +0100544 /* no fb bound */
545 if (!atomic && !crtc->fb) {
546 NV_DEBUG_KMS(dev, "No FB bound\n");
547 return 0;
548 }
549
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500550 /* If atomic, we want to switch to the fb we were passed, so
551 * now we update pointers to do that. (We don't pin; just
552 * assume we're already pinned and update the base address.)
553 */
554 if (atomic) {
555 drm_fb = passed_fb;
556 fb = nouveau_framebuffer(passed_fb);
Emil Velikovf9ec8f62011-03-19 23:31:53 +0000557 } else {
Emil Velikovffbc5592011-08-21 22:48:12 +0100558 drm_fb = crtc->fb;
559 fb = nouveau_framebuffer(crtc->fb);
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500560 /* If not atomic, we can go ahead and pin, and unpin the
561 * old fb we were passed.
562 */
563 ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM);
564 if (ret)
565 return ret;
566
567 if (passed_fb) {
568 struct nouveau_framebuffer *ofb = nouveau_framebuffer(passed_fb);
569 nouveau_bo_unpin(ofb->nvbo);
570 }
571 }
572
Ben Skeggs180cc302011-06-07 11:24:14 +1000573 nv_crtc->fb.offset = fb->nvbo->bo.offset;
Francisco Jerezf13b3262010-10-10 06:01:08 +0200574 nv_crtc->fb.tile_flags = nouveau_bo_tile_layout(fb->nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000575 nv_crtc->fb.cpp = drm_fb->bits_per_pixel / 8;
576 if (!nv_crtc->fb.blanked && dev_priv->chipset != 0x50) {
577 ret = RING_SPACE(evo, 2);
578 if (ret)
579 return ret;
580
581 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_DMA), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000582 OUT_RING (evo, fb->r_dma);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000583 }
584
585 ret = RING_SPACE(evo, 12);
586 if (ret)
587 return ret;
588
589 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_OFFSET), 5);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000590 OUT_RING (evo, nv_crtc->fb.offset >> 8);
591 OUT_RING (evo, 0);
592 OUT_RING (evo, (drm_fb->height << 16) | drm_fb->width);
593 OUT_RING (evo, fb->r_pitch);
594 OUT_RING (evo, fb->r_format);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000595
596 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLUT_MODE), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000597 OUT_RING (evo, fb->base.depth == 8 ?
598 NV50_EVO_CRTC_CLUT_MODE_OFF : NV50_EVO_CRTC_CLUT_MODE_ON);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000599
600 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, COLOR_CTRL), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000601 OUT_RING (evo, NV50_EVO_CRTC_COLOR_CTRL_COLOR);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000602 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, FB_POS), 1);
Ben Skeggs45c4e0a2011-02-09 11:57:45 +1000603 OUT_RING (evo, (y << 16) | x);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000604
605 if (nv_crtc->lut.depth != fb->base.depth) {
606 nv_crtc->lut.depth = fb->base.depth;
607 nv50_crtc_lut_load(crtc);
608 }
609
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 return 0;
611}
612
613static int
614nv50_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
615 struct drm_display_mode *adjusted_mode, int x, int y,
616 struct drm_framebuffer *old_fb)
617{
618 struct drm_device *dev = crtc->dev;
Ben Skeggs59c0f572011-02-01 10:24:41 +1000619 struct nouveau_channel *evo = nv50_display(dev)->master;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
621 struct nouveau_connector *nv_connector = NULL;
622 uint32_t hsync_dur, vsync_dur, hsync_start_to_end, vsync_start_to_end;
623 uint32_t hunk1, vunk1, vunk2a, vunk2b;
624 int ret;
625
626 /* Find the connector attached to this CRTC */
627 nv_connector = nouveau_crtc_connector_get(nv_crtc);
628
629 *nv_crtc->mode = *adjusted_mode;
630
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100631 NV_DEBUG_KMS(dev, "index %d\n", nv_crtc->index);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000632
633 hsync_dur = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
634 vsync_dur = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
635 hsync_start_to_end = adjusted_mode->htotal - adjusted_mode->hsync_start;
636 vsync_start_to_end = adjusted_mode->vtotal - adjusted_mode->vsync_start;
637 /* I can't give this a proper name, anyone else can? */
638 hunk1 = adjusted_mode->htotal -
639 adjusted_mode->hsync_start + adjusted_mode->hdisplay;
640 vunk1 = adjusted_mode->vtotal -
641 adjusted_mode->vsync_start + adjusted_mode->vdisplay;
642 /* Another strange value, this time only for interlaced adjusted_modes. */
643 vunk2a = 2 * adjusted_mode->vtotal -
644 adjusted_mode->vsync_start + adjusted_mode->vdisplay;
645 vunk2b = adjusted_mode->vtotal -
646 adjusted_mode->vsync_start + adjusted_mode->vtotal;
647
648 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
649 vsync_dur /= 2;
650 vsync_start_to_end /= 2;
651 vunk1 /= 2;
652 vunk2a /= 2;
653 vunk2b /= 2;
654 /* magic */
655 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN) {
656 vsync_start_to_end -= 1;
657 vunk1 -= 1;
658 vunk2a -= 1;
659 vunk2b -= 1;
660 }
661 }
662
663 ret = RING_SPACE(evo, 17);
664 if (ret)
665 return ret;
666
667 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, CLOCK), 2);
668 OUT_RING(evo, adjusted_mode->clock | 0x800000);
669 OUT_RING(evo, (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 0);
670
671 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, DISPLAY_START), 5);
672 OUT_RING(evo, 0);
673 OUT_RING(evo, (adjusted_mode->vtotal << 16) | adjusted_mode->htotal);
674 OUT_RING(evo, (vsync_dur - 1) << 16 | (hsync_dur - 1));
675 OUT_RING(evo, (vsync_start_to_end - 1) << 16 |
676 (hsync_start_to_end - 1));
677 OUT_RING(evo, (vunk1 - 1) << 16 | (hunk1 - 1));
678
679 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
680 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK0824), 1);
681 OUT_RING(evo, (vunk2b - 1) << 16 | (vunk2a - 1));
682 } else {
683 OUT_RING(evo, 0);
684 OUT_RING(evo, 0);
685 }
686
687 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, UNK082C), 1);
688 OUT_RING(evo, 0);
689
690 /* This is the actual resolution of the mode. */
691 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, REAL_RES), 1);
692 OUT_RING(evo, (mode->vdisplay << 16) | mode->hdisplay);
693 BEGIN_RING(evo, 0, NV50_EVO_CRTC(nv_crtc->index, SCALE_CENTER_OFFSET), 1);
694 OUT_RING(evo, NV50_EVO_CRTC_SCALE_CENTER_OFFSET_VAL(0, 0));
695
696 nv_crtc->set_dither(nv_crtc, nv_connector->use_dithering, false);
697 nv_crtc->set_scale(nv_crtc, nv_connector->scaling_mode, false);
698
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000699 return nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000700}
701
702static int
703nv50_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
704 struct drm_framebuffer *old_fb)
705{
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000706 int ret;
707
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000708 nv50_display_flip_stop(crtc);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000709 ret = nv50_crtc_do_mode_set_base(crtc, old_fb, x, y, false);
710 if (ret)
711 return ret;
712
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000713 ret = nv50_crtc_wait_complete(crtc);
714 if (ret)
715 return ret;
716
717 return nv50_display_flip_next(crtc, crtc->fb, NULL);
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500718}
719
720static int
721nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
722 struct drm_framebuffer *fb,
Jason Wessel21c74a82010-10-13 14:09:44 -0500723 int x, int y, enum mode_set_atomic state)
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500724{
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000725 int ret;
726
Ben Skeggs1d3fac02011-02-07 14:18:37 +1000727 nv50_display_flip_stop(crtc);
Ben Skeggs60f60bf2011-02-03 15:46:14 +1000728 ret = nv50_crtc_do_mode_set_base(crtc, fb, x, y, true);
729 if (ret)
730 return ret;
731
732 return nv50_crtc_wait_complete(crtc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000733}
734
735static const struct drm_crtc_helper_funcs nv50_crtc_helper_funcs = {
736 .dpms = nv50_crtc_dpms,
737 .prepare = nv50_crtc_prepare,
738 .commit = nv50_crtc_commit,
739 .mode_fixup = nv50_crtc_mode_fixup,
740 .mode_set = nv50_crtc_mode_set,
741 .mode_set_base = nv50_crtc_mode_set_base,
Chris Ballbe64c2bb2010-09-26 06:47:24 -0500742 .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000743 .load_lut = nv50_crtc_lut_load,
744};
745
746int
747nv50_crtc_create(struct drm_device *dev, int index)
748{
749 struct nouveau_crtc *nv_crtc = NULL;
750 int ret, i;
751
Maarten Maathuisef2bb502009-12-13 16:53:12 +0100752 NV_DEBUG_KMS(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753
754 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
755 if (!nv_crtc)
756 return -ENOMEM;
757
758 nv_crtc->mode = kzalloc(sizeof(*nv_crtc->mode), GFP_KERNEL);
759 if (!nv_crtc->mode) {
760 kfree(nv_crtc);
761 return -ENOMEM;
762 }
763
764 /* Default CLUT parameters, will be activated on the hw upon
765 * first mode set.
766 */
767 for (i = 0; i < 256; i++) {
768 nv_crtc->lut.r[i] = i << 8;
769 nv_crtc->lut.g[i] = i << 8;
770 nv_crtc->lut.b[i] = i << 8;
771 }
772 nv_crtc->lut.depth = 0;
773
Ben Skeggs7375c952011-06-07 14:21:29 +1000774 ret = nouveau_bo_new(dev, 4096, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsd550c412011-02-16 08:41:56 +1000775 0, 0x0000, &nv_crtc->lut.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776 if (!ret) {
777 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
778 if (!ret)
779 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
780 if (ret)
781 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
782 }
783
784 if (ret) {
785 kfree(nv_crtc->mode);
786 kfree(nv_crtc);
787 return ret;
788 }
789
790 nv_crtc->index = index;
791
792 /* set function pointers */
793 nv_crtc->set_dither = nv50_crtc_set_dither;
794 nv_crtc->set_scale = nv50_crtc_set_scale;
795
796 drm_crtc_init(dev, &nv_crtc->base, &nv50_crtc_funcs);
797 drm_crtc_helper_add(&nv_crtc->base, &nv50_crtc_helper_funcs);
798 drm_mode_crtc_set_gamma_size(&nv_crtc->base, 256);
799
Ben Skeggs7375c952011-06-07 14:21:29 +1000800 ret = nouveau_bo_new(dev, 64*64*4, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggsd550c412011-02-16 08:41:56 +1000801 0, 0x0000, &nv_crtc->cursor.nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802 if (!ret) {
803 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
804 if (!ret)
805 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
806 if (ret)
807 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
808 }
809
810 nv50_cursor_init(nv_crtc);
811 return 0;
812}