blob: 7c2defdcc8f3a58044e33201fc188e9489580428 [file] [log] [blame]
Ben Skeggs26f6d882011-07-04 16:25:18 +10001/*
2 * Copyright 2011 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
Ben Skeggs51beb422011-07-05 10:33:08 +100025#include <linux/dma-mapping.h>
Ben Skeggs83fc0832011-07-05 13:08:40 +100026
Ben Skeggs26f6d882011-07-04 16:25:18 +100027#include "drmP.h"
Ben Skeggs83fc0832011-07-05 13:08:40 +100028#include "drm_crtc_helper.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100029
30#include "nouveau_drv.h"
31#include "nouveau_connector.h"
32#include "nouveau_encoder.h"
33#include "nouveau_crtc.h"
Ben Skeggs37b034a2011-07-08 14:43:19 +100034#include "nouveau_dma.h"
Ben Skeggs438d99e2011-07-05 16:48:06 +100035#include "nouveau_fb.h"
Ben Skeggs3a89cd02011-07-07 10:47:10 +100036#include "nv50_display.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100037
38struct nvd0_display {
39 struct nouveau_gpuobj *mem;
Ben Skeggs51beb422011-07-05 10:33:08 +100040 struct {
41 dma_addr_t handle;
42 u32 *ptr;
43 } evo[1];
Ben Skeggsf20ce962011-07-08 13:17:01 +100044
45 struct tasklet_struct tasklet;
Ben Skeggsee417792011-07-08 14:34:45 +100046 u32 modeset;
Ben Skeggs26f6d882011-07-04 16:25:18 +100047};
48
49static struct nvd0_display *
50nvd0_display(struct drm_device *dev)
51{
52 struct drm_nouveau_private *dev_priv = dev->dev_private;
53 return dev_priv->engine.display.priv;
54}
55
Ben Skeggs37b034a2011-07-08 14:43:19 +100056static inline int
Ben Skeggs51beb422011-07-05 10:33:08 +100057evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
58{
59 int ret = 0;
60 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
61 nv_wr32(dev, 0x610704 + (id * 0x10), data);
62 nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
63 if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
64 ret = -EBUSY;
65 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
66 return ret;
67}
68
69static u32 *
70evo_wait(struct drm_device *dev, int id, int nr)
71{
72 struct nvd0_display *disp = nvd0_display(dev);
73 u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
74
75 if (put + nr >= (PAGE_SIZE / 4)) {
76 disp->evo[id].ptr[put] = 0x20000000;
77
78 nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
79 if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
80 NV_ERROR(dev, "evo %d dma stalled\n", id);
81 return NULL;
82 }
83
84 put = 0;
85 }
86
Ben Skeggs27517dd2011-11-11 20:26:44 +100087 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
88 NV_INFO(dev, "Evo%d: %p START\n", id, disp->evo[id].ptr + put);
89
Ben Skeggs51beb422011-07-05 10:33:08 +100090 return disp->evo[id].ptr + put;
91}
92
93static void
94evo_kick(u32 *push, struct drm_device *dev, int id)
95{
96 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs27517dd2011-11-11 20:26:44 +100097
98 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO) {
99 u32 curp = nv_rd32(dev, 0x640000 + (id * 0x1000)) >> 2;
100 u32 *cur = disp->evo[id].ptr + curp;
101
102 while (cur < push)
103 NV_INFO(dev, "Evo%d: 0x%08x\n", id, *cur++);
104 NV_INFO(dev, "Evo%d: %p KICK!\n", id, push);
105 }
106
Ben Skeggs51beb422011-07-05 10:33:08 +1000107 nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
108}
109
110#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
111#define evo_data(p,d) *((p)++) = (d)
112
Ben Skeggs83fc0832011-07-05 13:08:40 +1000113static struct drm_crtc *
114nvd0_display_crtc_get(struct drm_encoder *encoder)
115{
116 return nouveau_encoder(encoder)->crtc;
117}
118
Ben Skeggs26f6d882011-07-04 16:25:18 +1000119/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000120 * CRTC
121 *****************************************************************************/
122static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000123nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000124{
125 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggsde691852011-10-17 12:23:41 +1000126 struct nouveau_connector *nv_connector;
127 struct drm_connector *connector;
128 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000129
Ben Skeggs488ff202011-10-17 10:38:10 +1000130 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000131 connector = &nv_connector->base;
132 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
133 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
134 mode = DITHERING_MODE_DYNAMIC2X2;
135 } else {
136 mode = nv_connector->dithering_mode;
137 }
138
139 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
140 if (connector->display_info.bpc >= 8)
141 mode |= DITHERING_DEPTH_8BPC;
142 } else {
143 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000144 }
145
146 push = evo_wait(dev, 0, 4);
147 if (push) {
148 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x300), 1);
149 evo_data(push, mode);
150 if (update) {
151 evo_mthd(push, 0x0080, 1);
152 evo_data(push, 0x00000000);
153 }
154 evo_kick(push, dev, 0);
155 }
156
157 return 0;
158}
159
160static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000161nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000162{
163 struct drm_display_mode *mode = &nv_crtc->base.mode;
164 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000165 struct nouveau_connector *nv_connector;
166 u32 *push, outX, outY;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000167
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000168 outX = mode->hdisplay;
169 outY = mode->vdisplay;
170
171 nv_connector = nouveau_crtc_connector_get(nv_crtc);
172 if (nv_connector && nv_connector->native_mode) {
173 struct drm_display_mode *native = nv_connector->native_mode;
174 u32 xratio = (native->hdisplay << 19) / mode->hdisplay;
175 u32 yratio = (native->vdisplay << 19) / mode->vdisplay;
176
Ben Skeggs488ff202011-10-17 10:38:10 +1000177 switch (nv_connector->scaling_mode) {
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000178 case DRM_MODE_SCALE_ASPECT:
179 if (xratio > yratio) {
180 outX = (mode->hdisplay * yratio) >> 19;
181 outY = (mode->vdisplay * yratio) >> 19;
182 } else {
183 outX = (mode->hdisplay * xratio) >> 19;
184 outY = (mode->vdisplay * xratio) >> 19;
185 }
186 break;
187 case DRM_MODE_SCALE_FULLSCREEN:
188 outX = native->hdisplay;
189 outY = native->vdisplay;
190 break;
191 default:
192 break;
193 }
194 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000195
196 push = evo_wait(dev, 0, 16);
197 if (push) {
198 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000199 evo_data(push, (outY << 16) | outX);
200 evo_data(push, (outY << 16) | outX);
201 evo_data(push, (outY << 16) | outX);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000202 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
203 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000204 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
205 evo_data(push, (mode->vdisplay << 16) | mode->hdisplay);
206 if (update) {
207 evo_mthd(push, 0x0080, 1);
208 evo_data(push, 0x00000000);
209 }
210 evo_kick(push, dev, 0);
211 }
212
213 return 0;
214}
215
216static int
217nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
218 int x, int y, bool update)
219{
220 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
221 u32 *push;
222
Ben Skeggs438d99e2011-07-05 16:48:06 +1000223 push = evo_wait(fb->dev, 0, 16);
224 if (push) {
225 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
226 evo_data(push, nvfb->nvbo->bo.offset >> 8);
227 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
228 evo_data(push, (fb->height << 16) | fb->width);
229 evo_data(push, nvfb->r_pitch);
230 evo_data(push, nvfb->r_format);
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000231 evo_data(push, nvfb->r_dma);
Ben Skeggsc6f2f712011-07-08 12:11:58 +1000232 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
233 evo_data(push, (y << 16) | x);
Ben Skeggsa46232e2011-07-07 15:23:48 +1000234 if (update) {
235 evo_mthd(push, 0x0080, 1);
236 evo_data(push, 0x00000000);
237 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000238 evo_kick(push, fb->dev, 0);
239 }
240
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000241 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000242 return 0;
243}
244
245static void
246nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
247{
248 struct drm_device *dev = nv_crtc->base.dev;
249 u32 *push = evo_wait(dev, 0, 16);
250 if (push) {
251 if (show) {
252 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
253 evo_data(push, 0x85000000);
254 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
255 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000256 evo_data(push, NvEvoVRAM);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000257 } else {
258 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
259 evo_data(push, 0x05000000);
260 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
261 evo_data(push, 0x00000000);
262 }
263
264 if (update) {
265 evo_mthd(push, 0x0080, 1);
266 evo_data(push, 0x00000000);
267 }
268
269 evo_kick(push, dev, 0);
270 }
271}
272
273static void
274nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
275{
276}
277
278static void
279nvd0_crtc_prepare(struct drm_crtc *crtc)
280{
281 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
282 u32 *push;
283
284 push = evo_wait(crtc->dev, 0, 2);
285 if (push) {
286 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
287 evo_data(push, 0x00000000);
288 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
289 evo_data(push, 0x03000000);
290 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
291 evo_data(push, 0x00000000);
292 evo_kick(push, crtc->dev, 0);
293 }
294
295 nvd0_crtc_cursor_show(nv_crtc, false, false);
296}
297
298static void
299nvd0_crtc_commit(struct drm_crtc *crtc)
300{
301 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
302 u32 *push;
303
304 push = evo_wait(crtc->dev, 0, 32);
305 if (push) {
306 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
307 evo_data(push, nv_crtc->fb.tile_flags);
308 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
309 evo_data(push, 0x83000000);
310 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
311 evo_data(push, 0x00000000);
312 evo_data(push, 0x00000000);
313 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000314 evo_data(push, NvEvoVRAM);
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000315 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
316 evo_data(push, 0xffffff00);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000317 evo_kick(push, crtc->dev, 0);
318 }
319
320 nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
321}
322
323static bool
324nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
325 struct drm_display_mode *adjusted_mode)
326{
327 return true;
328}
329
330static int
331nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
332{
333 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
334 int ret;
335
336 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
337 if (ret)
338 return ret;
339
340 if (old_fb) {
341 nvfb = nouveau_framebuffer(old_fb);
342 nouveau_bo_unpin(nvfb->nvbo);
343 }
344
345 return 0;
346}
347
348static int
349nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
350 struct drm_display_mode *mode, int x, int y,
351 struct drm_framebuffer *old_fb)
352{
353 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
354 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000355 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
356 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
357 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
358 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
359 u32 vblan2e = 0, vblan2s = 1;
360 u32 magic = 0x31ec6000;
Ben Skeggs629c1b92011-07-08 09:43:20 +1000361 u32 syncs, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000362 int ret;
363
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000364 hactive = mode->htotal;
365 hsynce = mode->hsync_end - mode->hsync_start - 1;
366 hbackp = mode->htotal - mode->hsync_end;
367 hblanke = hsynce + hbackp;
368 hfrontp = mode->hsync_start - mode->hdisplay;
369 hblanks = mode->htotal - hfrontp - 1;
370
371 vactive = mode->vtotal * vscan / ilace;
372 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
373 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
374 vblanke = vsynce + vbackp;
375 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
376 vblanks = vactive - vfrontp - 1;
377 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
378 vblan2e = vactive + vsynce + vbackp;
379 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
380 vactive = (vactive * 2) + 1;
381 magic |= 0x00000001;
382 }
383
Ben Skeggs629c1b92011-07-08 09:43:20 +1000384 syncs = 0x00000001;
385 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
386 syncs |= 0x00000008;
387 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
388 syncs |= 0x00000010;
389
Ben Skeggs438d99e2011-07-05 16:48:06 +1000390 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
391 if (ret)
392 return ret;
393
394 push = evo_wait(crtc->dev, 0, 64);
395 if (push) {
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000396 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000397 evo_data(push, 0x00000000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000398 evo_data(push, (vactive << 16) | hactive);
399 evo_data(push, ( vsynce << 16) | hsynce);
400 evo_data(push, (vblanke << 16) | hblanke);
401 evo_data(push, (vblanks << 16) | hblanks);
402 evo_data(push, (vblan2e << 16) | vblan2s);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000403 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
404 evo_data(push, 0x00000000); /* ??? */
405 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
406 evo_data(push, mode->clock * 1000);
407 evo_data(push, 0x00200000); /* ??? */
408 evo_data(push, mode->clock * 1000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000409 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000410 evo_data(push, syncs);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000411 evo_data(push, magic);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000412 evo_kick(push, crtc->dev, 0);
413 }
414
415 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs488ff202011-10-17 10:38:10 +1000416 nvd0_crtc_set_dither(nv_crtc, false);
417 nvd0_crtc_set_scale(nv_crtc, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000418 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
419 return 0;
420}
421
422static int
423nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
424 struct drm_framebuffer *old_fb)
425{
426 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
427 int ret;
428
Ben Skeggs84e2ad82011-08-26 09:40:39 +1000429 if (!crtc->fb) {
430 NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
431 return 0;
432 }
433
Ben Skeggs438d99e2011-07-05 16:48:06 +1000434 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
435 if (ret)
436 return ret;
437
438 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
439 return 0;
440}
441
442static int
443nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
444 struct drm_framebuffer *fb, int x, int y,
445 enum mode_set_atomic state)
446{
447 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
448 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
449 return 0;
450}
451
452static void
453nvd0_crtc_lut_load(struct drm_crtc *crtc)
454{
455 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
456 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
457 int i;
458
459 for (i = 0; i < 256; i++) {
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000460 writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
461 writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
462 writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000463 }
464}
465
466static int
467nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
468 uint32_t handle, uint32_t width, uint32_t height)
469{
470 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
471 struct drm_device *dev = crtc->dev;
472 struct drm_gem_object *gem;
473 struct nouveau_bo *nvbo;
474 bool visible = (handle != 0);
475 int i, ret = 0;
476
477 if (visible) {
478 if (width != 64 || height != 64)
479 return -EINVAL;
480
481 gem = drm_gem_object_lookup(dev, file_priv, handle);
482 if (unlikely(!gem))
483 return -ENOENT;
484 nvbo = nouveau_gem_object(gem);
485
486 ret = nouveau_bo_map(nvbo);
487 if (ret == 0) {
488 for (i = 0; i < 64 * 64; i++) {
489 u32 v = nouveau_bo_rd32(nvbo, i);
490 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
491 }
492 nouveau_bo_unmap(nvbo);
493 }
494
495 drm_gem_object_unreference_unlocked(gem);
496 }
497
498 if (visible != nv_crtc->cursor.visible) {
499 nvd0_crtc_cursor_show(nv_crtc, visible, true);
500 nv_crtc->cursor.visible = visible;
501 }
502
503 return ret;
504}
505
506static int
507nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
508{
509 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
510 const u32 data = (y << 16) | x;
511
512 nv_wr32(crtc->dev, 0x64d084 + (nv_crtc->index * 0x1000), data);
513 nv_wr32(crtc->dev, 0x64d080 + (nv_crtc->index * 0x1000), 0x00000000);
514 return 0;
515}
516
517static void
518nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
519 uint32_t start, uint32_t size)
520{
521 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
522 u32 end = max(start + size, (u32)256);
523 u32 i;
524
525 for (i = start; i < end; i++) {
526 nv_crtc->lut.r[i] = r[i];
527 nv_crtc->lut.g[i] = g[i];
528 nv_crtc->lut.b[i] = b[i];
529 }
530
531 nvd0_crtc_lut_load(crtc);
532}
533
534static void
535nvd0_crtc_destroy(struct drm_crtc *crtc)
536{
537 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
538 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
539 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
540 nouveau_bo_unmap(nv_crtc->lut.nvbo);
541 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
542 drm_crtc_cleanup(crtc);
543 kfree(crtc);
544}
545
546static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
547 .dpms = nvd0_crtc_dpms,
548 .prepare = nvd0_crtc_prepare,
549 .commit = nvd0_crtc_commit,
550 .mode_fixup = nvd0_crtc_mode_fixup,
551 .mode_set = nvd0_crtc_mode_set,
552 .mode_set_base = nvd0_crtc_mode_set_base,
553 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
554 .load_lut = nvd0_crtc_lut_load,
555};
556
557static const struct drm_crtc_funcs nvd0_crtc_func = {
558 .cursor_set = nvd0_crtc_cursor_set,
559 .cursor_move = nvd0_crtc_cursor_move,
560 .gamma_set = nvd0_crtc_gamma_set,
561 .set_config = drm_crtc_helper_set_config,
562 .destroy = nvd0_crtc_destroy,
563};
564
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000565static void
566nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
567{
568}
569
570static void
571nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
572{
573}
574
Ben Skeggs438d99e2011-07-05 16:48:06 +1000575static int
576nvd0_crtc_create(struct drm_device *dev, int index)
577{
578 struct nouveau_crtc *nv_crtc;
579 struct drm_crtc *crtc;
580 int ret, i;
581
582 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
583 if (!nv_crtc)
584 return -ENOMEM;
585
586 nv_crtc->index = index;
587 nv_crtc->set_dither = nvd0_crtc_set_dither;
588 nv_crtc->set_scale = nvd0_crtc_set_scale;
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000589 nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
590 nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000591 for (i = 0; i < 256; i++) {
592 nv_crtc->lut.r[i] = i << 8;
593 nv_crtc->lut.g[i] = i << 8;
594 nv_crtc->lut.b[i] = i << 8;
595 }
596
597 crtc = &nv_crtc->base;
598 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
599 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
600 drm_mode_crtc_set_gamma_size(crtc, 256);
601
602 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
603 0, 0x0000, &nv_crtc->cursor.nvbo);
604 if (!ret) {
605 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
606 if (!ret)
607 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
608 if (ret)
609 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
610 }
611
612 if (ret)
613 goto out;
614
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000615 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000616 0, 0x0000, &nv_crtc->lut.nvbo);
617 if (!ret) {
618 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
619 if (!ret)
620 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
621 if (ret)
622 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
623 }
624
625 if (ret)
626 goto out;
627
628 nvd0_crtc_lut_load(crtc);
629
630out:
631 if (ret)
632 nvd0_crtc_destroy(crtc);
633 return ret;
634}
635
636/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000637 * DAC
638 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000639static void
640nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
641{
642 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
643 struct drm_device *dev = encoder->dev;
644 int or = nv_encoder->or;
645 u32 dpms_ctrl;
646
647 dpms_ctrl = 0x80000000;
648 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
649 dpms_ctrl |= 0x00000001;
650 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
651 dpms_ctrl |= 0x00000004;
652
653 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
654 nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
655 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
656}
657
658static bool
659nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
660 struct drm_display_mode *adjusted_mode)
661{
662 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
663 struct nouveau_connector *nv_connector;
664
665 nv_connector = nouveau_encoder_connector_get(nv_encoder);
666 if (nv_connector && nv_connector->native_mode) {
667 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
668 int id = adjusted_mode->base.id;
669 *adjusted_mode = *nv_connector->native_mode;
670 adjusted_mode->base.id = id;
671 }
672 }
673
674 return true;
675}
676
677static void
678nvd0_dac_prepare(struct drm_encoder *encoder)
679{
680}
681
682static void
683nvd0_dac_commit(struct drm_encoder *encoder)
684{
685}
686
687static void
688nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
689 struct drm_display_mode *adjusted_mode)
690{
691 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
692 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
693 u32 *push;
694
695 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
696
Ben Skeggsff8ff502011-07-08 11:53:37 +1000697 push = evo_wait(encoder->dev, 0, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000698 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +1000699 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 2);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000700 evo_data(push, 1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +1000701 evo_data(push, 0x00ff);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000702 evo_kick(push, encoder->dev, 0);
703 }
704
705 nv_encoder->crtc = encoder->crtc;
706}
707
708static void
709nvd0_dac_disconnect(struct drm_encoder *encoder)
710{
711 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
712 struct drm_device *dev = encoder->dev;
713 u32 *push;
714
715 if (nv_encoder->crtc) {
716 nvd0_crtc_prepare(nv_encoder->crtc);
717
718 push = evo_wait(dev, 0, 4);
719 if (push) {
720 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
721 evo_data(push, 0x00000000);
722 evo_mthd(push, 0x0080, 1);
723 evo_data(push, 0x00000000);
724 evo_kick(push, dev, 0);
725 }
726
727 nv_encoder->crtc = NULL;
728 }
729}
730
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000731static enum drm_connector_status
732nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
733{
Ben Skeggsb6819932011-07-08 11:14:50 +1000734 enum drm_connector_status status = connector_status_disconnected;
735 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
736 struct drm_device *dev = encoder->dev;
737 int or = nv_encoder->or;
738 u32 load;
739
740 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
741 udelay(9500);
742 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
743
744 load = nv_rd32(dev, 0x61a00c + (or * 0x800));
745 if ((load & 0x38000000) == 0x38000000)
746 status = connector_status_connected;
747
748 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
749 return status;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000750}
751
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000752static void
753nvd0_dac_destroy(struct drm_encoder *encoder)
754{
755 drm_encoder_cleanup(encoder);
756 kfree(encoder);
757}
758
759static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
760 .dpms = nvd0_dac_dpms,
761 .mode_fixup = nvd0_dac_mode_fixup,
762 .prepare = nvd0_dac_prepare,
763 .commit = nvd0_dac_commit,
764 .mode_set = nvd0_dac_mode_set,
765 .disable = nvd0_dac_disconnect,
766 .get_crtc = nvd0_display_crtc_get,
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000767 .detect = nvd0_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000768};
769
770static const struct drm_encoder_funcs nvd0_dac_func = {
771 .destroy = nvd0_dac_destroy,
772};
773
774static int
775nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
776{
777 struct drm_device *dev = connector->dev;
778 struct nouveau_encoder *nv_encoder;
779 struct drm_encoder *encoder;
780
781 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
782 if (!nv_encoder)
783 return -ENOMEM;
784 nv_encoder->dcb = dcbe;
785 nv_encoder->or = ffs(dcbe->or) - 1;
786
787 encoder = to_drm_encoder(nv_encoder);
788 encoder->possible_crtcs = dcbe->heads;
789 encoder->possible_clones = 0;
790 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
791 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
792
793 drm_mode_connector_attach_encoder(connector, encoder);
794 return 0;
795}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000796
797/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +1000798 * Audio
799 *****************************************************************************/
800static void
801nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
802{
803 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
804 struct nouveau_connector *nv_connector;
805 struct drm_device *dev = encoder->dev;
806 int i, or = nv_encoder->or * 0x30;
807
808 nv_connector = nouveau_encoder_connector_get(nv_encoder);
809 if (!drm_detect_monitor_audio(nv_connector->edid))
810 return;
811
812 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000001);
813
814 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
815 if (nv_connector->base.eld[0]) {
816 u8 *eld = nv_connector->base.eld;
817
818 for (i = 0; i < eld[2] * 4; i++)
819 nv_wr32(dev, 0x10ec00 + or, (i << 8) | eld[i]);
820 for (i = eld[2] * 4; i < 0x60; i++)
821 nv_wr32(dev, 0x10ec00 + or, (i << 8) | 0x00);
822
823 nv_mask(dev, 0x10ec10 + or, 0x80000002, 0x80000002);
824 }
825}
826
827static void
828nvd0_audio_disconnect(struct drm_encoder *encoder)
829{
830 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
831 struct drm_device *dev = encoder->dev;
832 int or = nv_encoder->or * 0x30;
833
834 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000000);
835}
836
837/******************************************************************************
838 * HDMI
839 *****************************************************************************/
840static void
841nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
842{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000843 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
844 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
845 struct nouveau_connector *nv_connector;
846 struct drm_device *dev = encoder->dev;
847 int head = nv_crtc->index * 0x800;
848 u32 rekey = 56; /* binary driver, and tegra constant */
849 u32 max_ac_packet;
850
851 nv_connector = nouveau_encoder_connector_get(nv_encoder);
852 if (!drm_detect_hdmi_monitor(nv_connector->edid))
853 return;
854
855 max_ac_packet = mode->htotal - mode->hdisplay;
856 max_ac_packet -= rekey;
857 max_ac_packet -= 18; /* constant from tegra */
858 max_ac_packet /= 32;
859
860 /* AVI InfoFrame */
861 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
862 nv_wr32(dev, 0x61671c + head, 0x000d0282);
863 nv_wr32(dev, 0x616720 + head, 0x0000006f);
864 nv_wr32(dev, 0x616724 + head, 0x00000000);
865 nv_wr32(dev, 0x616728 + head, 0x00000000);
866 nv_wr32(dev, 0x61672c + head, 0x00000000);
867 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000001);
868
869 /* ??? InfoFrame? */
870 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
871 nv_wr32(dev, 0x6167ac + head, 0x00000010);
872 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000001);
873
874 /* HDMI_CTRL */
875 nv_mask(dev, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
876 max_ac_packet << 16);
877
Ben Skeggs091e40c2011-11-11 20:46:00 +1000878 /* NFI, audio doesn't work without it though.. */
879 nv_mask(dev, 0x616548 + head, 0x00000070, 0x00000000);
880
Ben Skeggs78951d22011-11-11 18:13:13 +1000881 nvd0_audio_mode_set(encoder, mode);
882}
883
884static void
885nvd0_hdmi_disconnect(struct drm_encoder *encoder)
886{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000887 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
888 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
889 struct drm_device *dev = encoder->dev;
890 int head = nv_crtc->index * 0x800;
891
Ben Skeggs78951d22011-11-11 18:13:13 +1000892 nvd0_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000893
894 nv_mask(dev, 0x616798 + head, 0x40000000, 0x00000000);
895 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
896 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +1000897}
898
899/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000900 * SOR
901 *****************************************************************************/
Ben Skeggs83fc0832011-07-05 13:08:40 +1000902static void
903nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
904{
905 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
906 struct drm_device *dev = encoder->dev;
907 struct drm_encoder *partner;
908 int or = nv_encoder->or;
909 u32 dpms_ctrl;
910
911 nv_encoder->last_dpms = mode;
912
913 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
914 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
915
916 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
917 continue;
918
919 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +1000920 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +1000921 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
922 return;
923 break;
924 }
925 }
926
927 dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
928 dpms_ctrl |= 0x80000000;
929
930 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
931 nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
932 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
933 nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
934}
935
936static bool
937nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
938 struct drm_display_mode *adjusted_mode)
939{
940 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
941 struct nouveau_connector *nv_connector;
942
943 nv_connector = nouveau_encoder_connector_get(nv_encoder);
944 if (nv_connector && nv_connector->native_mode) {
945 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
946 int id = adjusted_mode->base.id;
947 *adjusted_mode = *nv_connector->native_mode;
948 adjusted_mode->base.id = id;
949 }
950 }
951
952 return true;
953}
954
955static void
956nvd0_sor_prepare(struct drm_encoder *encoder)
957{
958}
959
960static void
961nvd0_sor_commit(struct drm_encoder *encoder)
962{
963}
964
965static void
Ben Skeggs3b6d83d12011-07-08 12:52:14 +1000966nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
967 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +1000968{
Ben Skeggs78951d22011-11-11 18:13:13 +1000969 struct drm_device *dev = encoder->dev;
970 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs83fc0832011-07-05 13:08:40 +1000971 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
972 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +1000973 struct nouveau_connector *nv_connector;
974 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs83fc0832011-07-05 13:08:40 +1000975 u32 mode_ctrl = (1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +1000976 u32 *push, or_config;
Ben Skeggs83fc0832011-07-05 13:08:40 +1000977
Ben Skeggs3b6d83d12011-07-08 12:52:14 +1000978 nv_connector = nouveau_encoder_connector_get(nv_encoder);
979 switch (nv_encoder->dcb->type) {
980 case OUTPUT_TMDS:
981 if (nv_encoder->dcb->sorconf.link & 1) {
982 if (mode->clock < 165000)
983 mode_ctrl |= 0x00000100;
984 else
985 mode_ctrl |= 0x00000500;
986 } else {
987 mode_ctrl |= 0x00000200;
988 }
Ben Skeggs83fc0832011-07-05 13:08:40 +1000989
Ben Skeggs3b6d83d12011-07-08 12:52:14 +1000990 or_config = (mode_ctrl & 0x00000f00) >> 8;
991 if (mode->clock >= 165000)
992 or_config |= 0x0100;
Ben Skeggs78951d22011-11-11 18:13:13 +1000993
994 nvd0_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +1000995 break;
996 case OUTPUT_LVDS:
997 or_config = (mode_ctrl & 0x00000f00) >> 8;
998 if (bios->fp_no_ddc) {
999 if (bios->fp.dual_link)
1000 or_config |= 0x0100;
1001 if (bios->fp.if_is_24bit)
1002 or_config |= 0x0200;
1003 } else {
1004 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS_SPWG) {
1005 if (((u8 *)nv_connector->edid)[121] == 2)
1006 or_config |= 0x0100;
1007 } else
1008 if (mode->clock >= bios->fp.duallink_transition_clk) {
1009 or_config |= 0x0100;
1010 }
1011
1012 if (or_config & 0x0100) {
1013 if (bios->fp.strapless_is_24bit & 2)
1014 or_config |= 0x0200;
1015 } else {
1016 if (bios->fp.strapless_is_24bit & 1)
1017 or_config |= 0x0200;
1018 }
1019
1020 if (nv_connector->base.display_info.bpc == 8)
1021 or_config |= 0x0200;
1022
1023 }
1024 break;
1025 default:
1026 BUG_ON(1);
1027 break;
1028 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001029
Ben Skeggs83fc0832011-07-05 13:08:40 +10001030 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
1031
Ben Skeggs78951d22011-11-11 18:13:13 +10001032 push = evo_wait(dev, 0, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001033 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +10001034 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 2);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001035 evo_data(push, mode_ctrl);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001036 evo_data(push, or_config);
Ben Skeggs78951d22011-11-11 18:13:13 +10001037 evo_kick(push, dev, 0);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001038 }
1039
1040 nv_encoder->crtc = encoder->crtc;
1041}
1042
1043static void
1044nvd0_sor_disconnect(struct drm_encoder *encoder)
1045{
1046 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1047 struct drm_device *dev = encoder->dev;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001048 u32 *push;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001049
1050 if (nv_encoder->crtc) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001051 nvd0_crtc_prepare(nv_encoder->crtc);
1052
1053 push = evo_wait(dev, 0, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001054 if (push) {
1055 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1056 evo_data(push, 0x00000000);
1057 evo_mthd(push, 0x0080, 1);
1058 evo_data(push, 0x00000000);
1059 evo_kick(push, dev, 0);
1060 }
1061
Ben Skeggs78951d22011-11-11 18:13:13 +10001062 nvd0_hdmi_disconnect(encoder);
1063
Ben Skeggs83fc0832011-07-05 13:08:40 +10001064 nv_encoder->crtc = NULL;
1065 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1066 }
1067}
1068
1069static void
1070nvd0_sor_destroy(struct drm_encoder *encoder)
1071{
1072 drm_encoder_cleanup(encoder);
1073 kfree(encoder);
1074}
1075
1076static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
1077 .dpms = nvd0_sor_dpms,
1078 .mode_fixup = nvd0_sor_mode_fixup,
1079 .prepare = nvd0_sor_prepare,
1080 .commit = nvd0_sor_commit,
1081 .mode_set = nvd0_sor_mode_set,
1082 .disable = nvd0_sor_disconnect,
1083 .get_crtc = nvd0_display_crtc_get,
1084};
1085
1086static const struct drm_encoder_funcs nvd0_sor_func = {
1087 .destroy = nvd0_sor_destroy,
1088};
1089
1090static int
1091nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1092{
1093 struct drm_device *dev = connector->dev;
1094 struct nouveau_encoder *nv_encoder;
1095 struct drm_encoder *encoder;
1096
1097 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1098 if (!nv_encoder)
1099 return -ENOMEM;
1100 nv_encoder->dcb = dcbe;
1101 nv_encoder->or = ffs(dcbe->or) - 1;
1102 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1103
1104 encoder = to_drm_encoder(nv_encoder);
1105 encoder->possible_crtcs = dcbe->heads;
1106 encoder->possible_clones = 0;
1107 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
1108 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
1109
1110 drm_mode_connector_attach_encoder(connector, encoder);
1111 return 0;
1112}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001113
1114/******************************************************************************
1115 * IRQ
1116 *****************************************************************************/
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001117static struct dcb_entry *
1118lookup_dcb(struct drm_device *dev, int id, u32 mc)
1119{
1120 struct drm_nouveau_private *dev_priv = dev->dev_private;
1121 int type, or, i;
1122
1123 if (id < 4) {
1124 type = OUTPUT_ANALOG;
1125 or = id;
1126 } else {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001127 switch (mc & 0x00000f00) {
1128 case 0x00000000: type = OUTPUT_LVDS; break;
1129 case 0x00000100: type = OUTPUT_TMDS; break;
1130 case 0x00000200: type = OUTPUT_TMDS; break;
1131 case 0x00000500: type = OUTPUT_TMDS; break;
1132 default:
Ben Skeggsee417792011-07-08 14:34:45 +10001133 NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001134 return NULL;
1135 }
1136
1137 or = id - 4;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001138 }
1139
1140 for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
1141 struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
1142 if (dcb->type == type && (dcb->or & (1 << or)))
1143 return dcb;
1144 }
1145
Ben Skeggsee417792011-07-08 14:34:45 +10001146 NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001147 return NULL;
1148}
1149
Ben Skeggs46005222011-07-05 11:01:13 +10001150static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001151nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001152{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001153 struct dcb_entry *dcb;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001154 int i;
1155
Ben Skeggsee417792011-07-08 14:34:45 +10001156 for (i = 0; mask && i < 8; i++) {
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001157 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
Ben Skeggsee417792011-07-08 14:34:45 +10001158 if (!(mcc & (1 << crtc)))
1159 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001160
Ben Skeggsee417792011-07-08 14:34:45 +10001161 dcb = lookup_dcb(dev, i, mcc);
1162 if (!dcb)
1163 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001164
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001165 nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001166 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001167
Ben Skeggs270a5742011-07-05 14:16:05 +10001168 nv_wr32(dev, 0x6101d4, 0x00000000);
1169 nv_wr32(dev, 0x6109d4, 0x00000000);
1170 nv_wr32(dev, 0x6101d0, 0x80000000);
1171}
1172
1173static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001174nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001175{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001176 struct dcb_entry *dcb;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001177 u32 or, tmp, pclk;
Ben Skeggsee417792011-07-08 14:34:45 +10001178 int i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001179
Ben Skeggsee417792011-07-08 14:34:45 +10001180 for (i = 0; mask && i < 8; i++) {
1181 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
1182 if (!(mcc & (1 << crtc)))
1183 continue;
1184
1185 dcb = lookup_dcb(dev, i, mcc);
1186 if (!dcb)
1187 continue;
1188
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001189 nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001190 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001191
Ben Skeggsee417792011-07-08 14:34:45 +10001192 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
1193 if (mask & 0x00010000) {
1194 nv50_crtc_set_clock(dev, crtc, pclk);
1195 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001196
Ben Skeggsee417792011-07-08 14:34:45 +10001197 for (i = 0; mask && i < 8; i++) {
1198 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1199 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1200 if (!(mcp & (1 << crtc)))
1201 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001202
Ben Skeggsee417792011-07-08 14:34:45 +10001203 dcb = lookup_dcb(dev, i, mcp);
1204 if (!dcb)
1205 continue;
1206 or = ffs(dcb->or) - 1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001207
Ben Skeggsee417792011-07-08 14:34:45 +10001208 nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001209
Ben Skeggsee417792011-07-08 14:34:45 +10001210 nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
1211 switch (dcb->type) {
1212 case OUTPUT_ANALOG:
1213 nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
1214 break;
1215 case OUTPUT_TMDS:
1216 case OUTPUT_LVDS:
1217 if (cfg & 0x00000100)
1218 tmp = 0x00000101;
1219 else
1220 tmp = 0x00000000;
1221
1222 nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
1223 break;
1224 default:
1225 break;
1226 }
1227
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001228 break;
1229 }
1230
Ben Skeggs270a5742011-07-05 14:16:05 +10001231 nv_wr32(dev, 0x6101d4, 0x00000000);
1232 nv_wr32(dev, 0x6109d4, 0x00000000);
1233 nv_wr32(dev, 0x6101d0, 0x80000000);
1234}
1235
1236static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001237nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001238{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001239 struct dcb_entry *dcb;
Ben Skeggsee417792011-07-08 14:34:45 +10001240 int pclk, i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001241
Ben Skeggsee417792011-07-08 14:34:45 +10001242 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001243
Ben Skeggsee417792011-07-08 14:34:45 +10001244 for (i = 0; mask && i < 8; i++) {
1245 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1246 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1247 if (!(mcp & (1 << crtc)))
1248 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001249
Ben Skeggsee417792011-07-08 14:34:45 +10001250 dcb = lookup_dcb(dev, i, mcp);
1251 if (!dcb)
1252 continue;
1253
1254 nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
1255 }
1256
Ben Skeggs270a5742011-07-05 14:16:05 +10001257 nv_wr32(dev, 0x6101d4, 0x00000000);
1258 nv_wr32(dev, 0x6109d4, 0x00000000);
1259 nv_wr32(dev, 0x6101d0, 0x80000000);
1260}
1261
1262static void
Ben Skeggsf20ce962011-07-08 13:17:01 +10001263nvd0_display_bh(unsigned long data)
1264{
1265 struct drm_device *dev = (struct drm_device *)data;
1266 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001267 u32 mask, crtc;
1268 int i;
1269
1270 if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
1271 NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
1272 NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
1273 nv_rd32(dev, 0x6101d0),
1274 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
1275 for (i = 0; i < 8; i++) {
1276 NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
1277 i < 4 ? "DAC" : "SOR", i,
1278 nv_rd32(dev, 0x640180 + (i * 0x20)),
1279 nv_rd32(dev, 0x660180 + (i * 0x20)));
1280 }
1281 }
1282
1283 mask = nv_rd32(dev, 0x6101d4);
1284 crtc = 0;
1285 if (!mask) {
1286 mask = nv_rd32(dev, 0x6109d4);
1287 crtc = 1;
1288 }
Ben Skeggsf20ce962011-07-08 13:17:01 +10001289
Ben Skeggsee417792011-07-08 14:34:45 +10001290 if (disp->modeset & 0x00000001)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001291 nvd0_display_unk1_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001292 if (disp->modeset & 0x00000002)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001293 nvd0_display_unk2_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001294 if (disp->modeset & 0x00000004)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001295 nvd0_display_unk4_handler(dev, crtc, mask);
Ben Skeggsf20ce962011-07-08 13:17:01 +10001296}
1297
1298static void
Ben Skeggs46005222011-07-05 11:01:13 +10001299nvd0_display_intr(struct drm_device *dev)
1300{
Ben Skeggsf20ce962011-07-08 13:17:01 +10001301 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001302 u32 intr = nv_rd32(dev, 0x610088);
1303
1304 if (intr & 0x00000002) {
1305 u32 stat = nv_rd32(dev, 0x61009c);
1306 int chid = ffs(stat) - 1;
1307 if (chid >= 0) {
1308 u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
1309 u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
1310 u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
1311
1312 NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
1313 "0x%08x 0x%08x\n",
1314 chid, (mthd & 0x0000ffc), data, mthd, unkn);
1315 nv_wr32(dev, 0x61009c, (1 << chid));
1316 nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
1317 }
1318
1319 intr &= ~0x00000002;
1320 }
1321
Ben Skeggs270a5742011-07-05 14:16:05 +10001322 if (intr & 0x00100000) {
1323 u32 stat = nv_rd32(dev, 0x6100ac);
1324
1325 if (stat & 0x00000007) {
Ben Skeggsee417792011-07-08 14:34:45 +10001326 disp->modeset = stat;
Ben Skeggsf20ce962011-07-08 13:17:01 +10001327 tasklet_schedule(&disp->tasklet);
Ben Skeggs270a5742011-07-05 14:16:05 +10001328
Ben Skeggsf20ce962011-07-08 13:17:01 +10001329 nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
Ben Skeggs270a5742011-07-05 14:16:05 +10001330 stat &= ~0x00000007;
1331 }
1332
1333 if (stat) {
1334 NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
1335 nv_wr32(dev, 0x6100ac, stat);
1336 }
1337
1338 intr &= ~0x00100000;
1339 }
1340
Ben Skeggs46005222011-07-05 11:01:13 +10001341 if (intr & 0x01000000) {
1342 u32 stat = nv_rd32(dev, 0x6100bc);
1343 nv_wr32(dev, 0x6100bc, stat);
1344 intr &= ~0x01000000;
1345 }
1346
1347 if (intr & 0x02000000) {
1348 u32 stat = nv_rd32(dev, 0x6108bc);
1349 nv_wr32(dev, 0x6108bc, stat);
1350 intr &= ~0x02000000;
1351 }
1352
1353 if (intr)
1354 NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
1355}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001356
1357/******************************************************************************
1358 * Init
1359 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10001360void
Ben Skeggs26f6d882011-07-04 16:25:18 +10001361nvd0_display_fini(struct drm_device *dev)
1362{
1363 int i;
1364
1365 /* fini cursors */
1366 for (i = 14; i >= 13; i--) {
1367 if (!(nv_rd32(dev, 0x610490 + (i * 0x10)) & 0x00000001))
1368 continue;
1369
1370 nv_mask(dev, 0x610490 + (i * 0x10), 0x00000001, 0x00000000);
1371 nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00000000);
1372 nv_mask(dev, 0x610090, 1 << i, 0x00000000);
1373 nv_mask(dev, 0x6100a0, 1 << i, 0x00000000);
1374 }
1375
1376 /* fini master */
1377 if (nv_rd32(dev, 0x610490) & 0x00000010) {
1378 nv_mask(dev, 0x610490, 0x00000010, 0x00000000);
1379 nv_mask(dev, 0x610490, 0x00000003, 0x00000000);
1380 nv_wait(dev, 0x610490, 0x80000000, 0x00000000);
1381 nv_mask(dev, 0x610090, 0x00000001, 0x00000000);
1382 nv_mask(dev, 0x6100a0, 0x00000001, 0x00000000);
1383 }
1384}
1385
1386int
1387nvd0_display_init(struct drm_device *dev)
1388{
1389 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001390 u32 *push;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001391 int i;
1392
1393 if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
1394 nv_wr32(dev, 0x6100ac, 0x00000100);
1395 nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
1396 if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
1397 NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
1398 nv_rd32(dev, 0x6194e8));
1399 return -EBUSY;
1400 }
1401 }
1402
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001403 /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
1404 * work at all unless you do the SOR part below.
1405 */
1406 for (i = 0; i < 3; i++) {
1407 u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
1408 nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
1409 }
1410
1411 for (i = 0; i < 4; i++) {
1412 u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
1413 nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
1414 }
1415
1416 for (i = 0; i < 2; i++) {
1417 u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
1418 u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
1419 u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
1420 nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
1421 nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
1422 nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
1423 }
1424
1425 /* point at our hash table / objects, enable interrupts */
Ben Skeggs26f6d882011-07-04 16:25:18 +10001426 nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
Ben Skeggs270a5742011-07-05 14:16:05 +10001427 nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001428
1429 /* init master */
Ben Skeggs51beb422011-07-05 10:33:08 +10001430 nv_wr32(dev, 0x610494, (disp->evo[0].handle >> 8) | 3);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001431 nv_wr32(dev, 0x610498, 0x00010000);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001432 nv_wr32(dev, 0x61049c, 0x00000001);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001433 nv_mask(dev, 0x610490, 0x00000010, 0x00000010);
1434 nv_wr32(dev, 0x640000, 0x00000000);
1435 nv_wr32(dev, 0x610490, 0x01000013);
1436 if (!nv_wait(dev, 0x610490, 0x80000000, 0x00000000)) {
1437 NV_ERROR(dev, "PDISP: master 0x%08x\n",
1438 nv_rd32(dev, 0x610490));
1439 return -EBUSY;
1440 }
1441 nv_mask(dev, 0x610090, 0x00000001, 0x00000001);
1442 nv_mask(dev, 0x6100a0, 0x00000001, 0x00000001);
1443
1444 /* init cursors */
1445 for (i = 13; i <= 14; i++) {
1446 nv_wr32(dev, 0x610490 + (i * 0x10), 0x00000001);
1447 if (!nv_wait(dev, 0x610490 + (i * 0x10), 0x00010000, 0x00010000)) {
1448 NV_ERROR(dev, "PDISP: curs%d 0x%08x\n", i,
1449 nv_rd32(dev, 0x610490 + (i * 0x10)));
1450 return -EBUSY;
1451 }
1452
1453 nv_mask(dev, 0x610090, 1 << i, 1 << i);
1454 nv_mask(dev, 0x6100a0, 1 << i, 1 << i);
1455 }
1456
Ben Skeggsefd272a2011-07-05 11:58:58 +10001457 push = evo_wait(dev, 0, 32);
1458 if (!push)
1459 return -EBUSY;
1460 evo_mthd(push, 0x0088, 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001461 evo_data(push, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001462 evo_mthd(push, 0x0084, 1);
1463 evo_data(push, 0x00000000);
1464 evo_mthd(push, 0x0084, 1);
1465 evo_data(push, 0x80000000);
1466 evo_mthd(push, 0x008c, 1);
1467 evo_data(push, 0x00000000);
1468 evo_kick(push, dev, 0);
1469
Ben Skeggs26f6d882011-07-04 16:25:18 +10001470 return 0;
1471}
1472
1473void
1474nvd0_display_destroy(struct drm_device *dev)
1475{
1476 struct drm_nouveau_private *dev_priv = dev->dev_private;
1477 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs51beb422011-07-05 10:33:08 +10001478 struct pci_dev *pdev = dev->pdev;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001479
Ben Skeggs51beb422011-07-05 10:33:08 +10001480 pci_free_consistent(pdev, PAGE_SIZE, disp->evo[0].ptr, disp->evo[0].handle);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001481 nouveau_gpuobj_ref(NULL, &disp->mem);
Ben Skeggs46005222011-07-05 11:01:13 +10001482 nouveau_irq_unregister(dev, 26);
Ben Skeggs51beb422011-07-05 10:33:08 +10001483
1484 dev_priv->engine.display.priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001485 kfree(disp);
1486}
1487
1488int
1489nvd0_display_create(struct drm_device *dev)
1490{
1491 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001492 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001493 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1494 struct drm_connector *connector, *tmp;
Ben Skeggs51beb422011-07-05 10:33:08 +10001495 struct pci_dev *pdev = dev->pdev;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001496 struct nvd0_display *disp;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001497 struct dcb_entry *dcbe;
1498 int ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001499
1500 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1501 if (!disp)
1502 return -ENOMEM;
1503 dev_priv->engine.display.priv = disp;
1504
Ben Skeggs438d99e2011-07-05 16:48:06 +10001505 /* create crtc objects to represent the hw heads */
1506 for (i = 0; i < 2; i++) {
1507 ret = nvd0_crtc_create(dev, i);
1508 if (ret)
1509 goto out;
1510 }
1511
Ben Skeggs83fc0832011-07-05 13:08:40 +10001512 /* create encoder/connector objects based on VBIOS DCB table */
1513 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1514 connector = nouveau_connector_create(dev, dcbe->connector);
1515 if (IS_ERR(connector))
1516 continue;
1517
1518 if (dcbe->location != DCB_LOC_ON_CHIP) {
1519 NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
1520 dcbe->type, ffs(dcbe->or) - 1);
1521 continue;
1522 }
1523
1524 switch (dcbe->type) {
1525 case OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001526 case OUTPUT_LVDS:
Ben Skeggs83fc0832011-07-05 13:08:40 +10001527 nvd0_sor_create(connector, dcbe);
1528 break;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001529 case OUTPUT_ANALOG:
1530 nvd0_dac_create(connector, dcbe);
1531 break;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001532 default:
1533 NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
1534 dcbe->type, ffs(dcbe->or) - 1);
1535 continue;
1536 }
1537 }
1538
1539 /* cull any connectors we created that don't have an encoder */
1540 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
1541 if (connector->encoder_ids[0])
1542 continue;
1543
1544 NV_WARN(dev, "%s has no encoders, removing\n",
1545 drm_get_connector_name(connector));
1546 connector->funcs->destroy(connector);
1547 }
1548
Ben Skeggs46005222011-07-05 11:01:13 +10001549 /* setup interrupt handling */
Ben Skeggsf20ce962011-07-08 13:17:01 +10001550 tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001551 nouveau_irq_register(dev, 26, nvd0_display_intr);
1552
Ben Skeggs51beb422011-07-05 10:33:08 +10001553 /* hash table and dma objects for the memory areas we care about */
Ben Skeggsefd272a2011-07-05 11:58:58 +10001554 ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
1555 NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001556 if (ret)
1557 goto out;
1558
Ben Skeggsefd272a2011-07-05 11:58:58 +10001559 nv_wo32(disp->mem, 0x1000, 0x00000049);
1560 nv_wo32(disp->mem, 0x1004, (disp->mem->vinst + 0x2000) >> 8);
1561 nv_wo32(disp->mem, 0x1008, (disp->mem->vinst + 0x2fff) >> 8);
1562 nv_wo32(disp->mem, 0x100c, 0x00000000);
1563 nv_wo32(disp->mem, 0x1010, 0x00000000);
1564 nv_wo32(disp->mem, 0x1014, 0x00000000);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001565 nv_wo32(disp->mem, 0x0000, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001566 nv_wo32(disp->mem, 0x0004, (0x1000 << 9) | 0x00000001);
1567
Ben Skeggsc0cc92a2011-07-06 11:40:45 +10001568 nv_wo32(disp->mem, 0x1020, 0x00000049);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001569 nv_wo32(disp->mem, 0x1024, 0x00000000);
1570 nv_wo32(disp->mem, 0x1028, (dev_priv->vram_size - 1) >> 8);
1571 nv_wo32(disp->mem, 0x102c, 0x00000000);
1572 nv_wo32(disp->mem, 0x1030, 0x00000000);
1573 nv_wo32(disp->mem, 0x1034, 0x00000000);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001574 nv_wo32(disp->mem, 0x0008, NvEvoVRAM);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001575 nv_wo32(disp->mem, 0x000c, (0x1020 << 9) | 0x00000001);
1576
Ben Skeggsc0cc92a2011-07-06 11:40:45 +10001577 nv_wo32(disp->mem, 0x1040, 0x00000009);
1578 nv_wo32(disp->mem, 0x1044, 0x00000000);
1579 nv_wo32(disp->mem, 0x1048, (dev_priv->vram_size - 1) >> 8);
1580 nv_wo32(disp->mem, 0x104c, 0x00000000);
1581 nv_wo32(disp->mem, 0x1050, 0x00000000);
1582 nv_wo32(disp->mem, 0x1054, 0x00000000);
1583 nv_wo32(disp->mem, 0x0010, NvEvoVRAM_LP);
1584 nv_wo32(disp->mem, 0x0014, (0x1040 << 9) | 0x00000001);
1585
1586 nv_wo32(disp->mem, 0x1060, 0x0fe00009);
1587 nv_wo32(disp->mem, 0x1064, 0x00000000);
1588 nv_wo32(disp->mem, 0x1068, (dev_priv->vram_size - 1) >> 8);
1589 nv_wo32(disp->mem, 0x106c, 0x00000000);
1590 nv_wo32(disp->mem, 0x1070, 0x00000000);
1591 nv_wo32(disp->mem, 0x1074, 0x00000000);
1592 nv_wo32(disp->mem, 0x0018, NvEvoFB32);
1593 nv_wo32(disp->mem, 0x001c, (0x1060 << 9) | 0x00000001);
1594
Ben Skeggsefd272a2011-07-05 11:58:58 +10001595 pinstmem->flush(dev);
1596
Ben Skeggs51beb422011-07-05 10:33:08 +10001597 /* push buffers for evo channels */
1598 disp->evo[0].ptr =
1599 pci_alloc_consistent(pdev, PAGE_SIZE, &disp->evo[0].handle);
1600 if (!disp->evo[0].ptr) {
1601 ret = -ENOMEM;
1602 goto out;
1603 }
1604
Ben Skeggs26f6d882011-07-04 16:25:18 +10001605out:
1606 if (ret)
1607 nvd0_display_destroy(dev);
1608 return ret;
1609}