blob: 0c788ebe09ab55321a101a89b836159d2aa07e18 [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
Ben Skeggsbdb8c212011-11-12 01:30:24 +100038#define EVO_MASTER (0x00)
39#define EVO_SYNC(c) (0x01 + (c))
40#define EVO_CURS(c) (0x0d + (c))
41
Ben Skeggs26f6d882011-07-04 16:25:18 +100042struct nvd0_display {
43 struct nouveau_gpuobj *mem;
Ben Skeggs51beb422011-07-05 10:33:08 +100044 struct {
45 dma_addr_t handle;
46 u32 *ptr;
Ben Skeggsbdb8c212011-11-12 01:30:24 +100047 } evo[3];
Ben Skeggsf20ce962011-07-08 13:17:01 +100048
49 struct tasklet_struct tasklet;
Ben Skeggsee417792011-07-08 14:34:45 +100050 u32 modeset;
Ben Skeggs26f6d882011-07-04 16:25:18 +100051};
52
53static struct nvd0_display *
54nvd0_display(struct drm_device *dev)
55{
56 struct drm_nouveau_private *dev_priv = dev->dev_private;
57 return dev_priv->engine.display.priv;
58}
59
Ben Skeggsbdb8c212011-11-12 01:30:24 +100060static struct drm_crtc *
61nvd0_display_crtc_get(struct drm_encoder *encoder)
62{
63 return nouveau_encoder(encoder)->crtc;
64}
65
66/******************************************************************************
67 * EVO channel helpers
68 *****************************************************************************/
Ben Skeggs37b034a2011-07-08 14:43:19 +100069static inline int
Ben Skeggs51beb422011-07-05 10:33:08 +100070evo_icmd(struct drm_device *dev, int id, u32 mthd, u32 data)
71{
72 int ret = 0;
73 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000001);
74 nv_wr32(dev, 0x610704 + (id * 0x10), data);
75 nv_mask(dev, 0x610704 + (id * 0x10), 0x80000ffc, 0x80000000 | mthd);
76 if (!nv_wait(dev, 0x610704 + (id * 0x10), 0x80000000, 0x00000000))
77 ret = -EBUSY;
78 nv_mask(dev, 0x610700 + (id * 0x10), 0x00000001, 0x00000000);
79 return ret;
80}
81
82static u32 *
83evo_wait(struct drm_device *dev, int id, int nr)
84{
85 struct nvd0_display *disp = nvd0_display(dev);
86 u32 put = nv_rd32(dev, 0x640000 + (id * 0x1000)) / 4;
87
88 if (put + nr >= (PAGE_SIZE / 4)) {
89 disp->evo[id].ptr[put] = 0x20000000;
90
91 nv_wr32(dev, 0x640000 + (id * 0x1000), 0x00000000);
92 if (!nv_wait(dev, 0x640004 + (id * 0x1000), ~0, 0x00000000)) {
93 NV_ERROR(dev, "evo %d dma stalled\n", id);
94 return NULL;
95 }
96
97 put = 0;
98 }
99
Ben Skeggs27517dd2011-11-11 20:26:44 +1000100 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
101 NV_INFO(dev, "Evo%d: %p START\n", id, disp->evo[id].ptr + put);
102
Ben Skeggs51beb422011-07-05 10:33:08 +1000103 return disp->evo[id].ptr + put;
104}
105
106static void
107evo_kick(u32 *push, struct drm_device *dev, int id)
108{
109 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs27517dd2011-11-11 20:26:44 +1000110
111 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO) {
112 u32 curp = nv_rd32(dev, 0x640000 + (id * 0x1000)) >> 2;
113 u32 *cur = disp->evo[id].ptr + curp;
114
115 while (cur < push)
116 NV_INFO(dev, "Evo%d: 0x%08x\n", id, *cur++);
117 NV_INFO(dev, "Evo%d: %p KICK!\n", id, push);
118 }
119
Ben Skeggs51beb422011-07-05 10:33:08 +1000120 nv_wr32(dev, 0x640000 + (id * 0x1000), (push - disp->evo[id].ptr) << 2);
121}
122
123#define evo_mthd(p,m,s) *((p)++) = (((s) << 18) | (m))
124#define evo_data(p,d) *((p)++) = (d)
125
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000126static int
127evo_init_dma(struct drm_device *dev, int ch)
Ben Skeggs83fc0832011-07-05 13:08:40 +1000128{
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000129 struct nvd0_display *disp = nvd0_display(dev);
130 u32 flags;
131
132 flags = 0x00000000;
133 if (ch == EVO_MASTER)
134 flags |= 0x01000000;
135
136 nv_wr32(dev, 0x610494 + (ch * 0x0010), (disp->evo[ch].handle >> 8) | 3);
137 nv_wr32(dev, 0x610498 + (ch * 0x0010), 0x00010000);
138 nv_wr32(dev, 0x61049c + (ch * 0x0010), 0x00000001);
139 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
140 nv_wr32(dev, 0x640000 + (ch * 0x1000), 0x00000000);
141 nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000013 | flags);
142 if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000)) {
143 NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
144 nv_rd32(dev, 0x610490 + (ch * 0x0010)));
145 return -EBUSY;
146 }
147
148 nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
149 nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
150 return 0;
151}
152
153static void
154evo_fini_dma(struct drm_device *dev, int ch)
155{
156 if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000010))
157 return;
158
159 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000000);
160 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000003, 0x00000000);
161 nv_wait(dev, 0x610490 + (ch * 0x0010), 0x80000000, 0x00000000);
162 nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
163 nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
164}
165
Ben Skeggs4acd4292011-11-12 12:57:54 +1000166static inline void
167evo_piow(struct drm_device *dev, int ch, u16 mthd, u32 data)
168{
169 nv_wr32(dev, 0x640000 + (ch * 0x1000) + mthd, data);
170}
171
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000172static int
173evo_init_pio(struct drm_device *dev, int ch)
174{
175 nv_wr32(dev, 0x610490 + (ch * 0x0010), 0x00000001);
176 if (!nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00010000)) {
177 NV_ERROR(dev, "PDISP: ch%d 0x%08x\n", ch,
178 nv_rd32(dev, 0x610490 + (ch * 0x0010)));
179 return -EBUSY;
180 }
181
182 nv_mask(dev, 0x610090, (1 << ch), (1 << ch));
183 nv_mask(dev, 0x6100a0, (1 << ch), (1 << ch));
184 return 0;
185}
186
187static void
188evo_fini_pio(struct drm_device *dev, int ch)
189{
190 if (!(nv_rd32(dev, 0x610490 + (ch * 0x0010)) & 0x00000001))
191 return;
192
193 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000010, 0x00000010);
194 nv_mask(dev, 0x610490 + (ch * 0x0010), 0x00000001, 0x00000000);
195 nv_wait(dev, 0x610490 + (ch * 0x0010), 0x00010000, 0x00000000);
196 nv_mask(dev, 0x610090, (1 << ch), 0x00000000);
197 nv_mask(dev, 0x6100a0, (1 << ch), 0x00000000);
Ben Skeggs83fc0832011-07-05 13:08:40 +1000198}
199
Ben Skeggs26f6d882011-07-04 16:25:18 +1000200/******************************************************************************
Ben Skeggs438d99e2011-07-05 16:48:06 +1000201 * CRTC
202 *****************************************************************************/
203static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000204nvd0_crtc_set_dither(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000205{
206 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggsde691852011-10-17 12:23:41 +1000207 struct nouveau_connector *nv_connector;
208 struct drm_connector *connector;
209 u32 *push, mode = 0x00;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000210
Ben Skeggs488ff202011-10-17 10:38:10 +1000211 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggsde691852011-10-17 12:23:41 +1000212 connector = &nv_connector->base;
213 if (nv_connector->dithering_mode == DITHERING_MODE_AUTO) {
214 if (nv_crtc->base.fb->depth > connector->display_info.bpc * 3)
215 mode = DITHERING_MODE_DYNAMIC2X2;
216 } else {
217 mode = nv_connector->dithering_mode;
218 }
219
220 if (nv_connector->dithering_depth == DITHERING_DEPTH_AUTO) {
221 if (connector->display_info.bpc >= 8)
222 mode |= DITHERING_DEPTH_8BPC;
223 } else {
224 mode |= nv_connector->dithering_depth;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000225 }
226
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000227 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000228 if (push) {
229 evo_mthd(push, 0x0490 + (nv_crtc->index * 0x300), 1);
230 evo_data(push, mode);
231 if (update) {
232 evo_mthd(push, 0x0080, 1);
233 evo_data(push, 0x00000000);
234 }
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000235 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000236 }
237
238 return 0;
239}
240
241static int
Ben Skeggs488ff202011-10-17 10:38:10 +1000242nvd0_crtc_set_scale(struct nouveau_crtc *nv_crtc, bool update)
Ben Skeggs438d99e2011-07-05 16:48:06 +1000243{
Ben Skeggs92854622011-11-11 23:49:06 +1000244 struct drm_display_mode *omode, *umode = &nv_crtc->base.mode;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000245 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000246 struct nouveau_connector *nv_connector;
Ben Skeggs92854622011-11-11 23:49:06 +1000247 int mode = DRM_MODE_SCALE_NONE;
248 u32 oX, oY, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000249
Ben Skeggs92854622011-11-11 23:49:06 +1000250 /* start off at the resolution we programmed the crtc for, this
251 * effectively handles NONE/FULL scaling
252 */
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000253 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs92854622011-11-11 23:49:06 +1000254 if (nv_connector && nv_connector->native_mode)
255 mode = nv_connector->scaling_mode;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000256
Ben Skeggs92854622011-11-11 23:49:06 +1000257 if (mode != DRM_MODE_SCALE_NONE)
258 omode = nv_connector->native_mode;
259 else
260 omode = umode;
261
262 oX = omode->hdisplay;
263 oY = omode->vdisplay;
264 if (omode->flags & DRM_MODE_FLAG_DBLSCAN)
265 oY *= 2;
266
267 /* add overscan compensation if necessary, will keep the aspect
268 * ratio the same as the backend mode unless overridden by the
269 * user setting both hborder and vborder properties.
270 */
271 if (nv_connector && ( nv_connector->underscan == UNDERSCAN_ON ||
272 (nv_connector->underscan == UNDERSCAN_AUTO &&
273 nv_connector->edid &&
274 drm_detect_hdmi_monitor(nv_connector->edid)))) {
275 u32 bX = nv_connector->underscan_hborder;
276 u32 bY = nv_connector->underscan_vborder;
277 u32 aspect = (oY << 19) / oX;
278
279 if (bX) {
280 oX -= (bX * 2);
281 if (bY) oY -= (bY * 2);
282 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
283 } else {
284 oX -= (oX >> 4) + 32;
285 if (bY) oY -= (bY * 2);
286 else oY = ((oX * aspect) + (aspect / 2)) >> 19;
Ben Skeggsf3fdc522011-07-07 16:01:57 +1000287 }
288 }
Ben Skeggs438d99e2011-07-05 16:48:06 +1000289
Ben Skeggs92854622011-11-11 23:49:06 +1000290 /* handle CENTER/ASPECT scaling, taking into account the areas
291 * removed already for overscan compensation
292 */
293 switch (mode) {
294 case DRM_MODE_SCALE_CENTER:
295 oX = min((u32)umode->hdisplay, oX);
296 oY = min((u32)umode->vdisplay, oY);
297 /* fall-through */
298 case DRM_MODE_SCALE_ASPECT:
299 if (oY < oX) {
300 u32 aspect = (umode->hdisplay << 19) / umode->vdisplay;
301 oX = ((oY * aspect) + (aspect / 2)) >> 19;
302 } else {
303 u32 aspect = (umode->vdisplay << 19) / umode->hdisplay;
304 oY = ((oX * aspect) + (aspect / 2)) >> 19;
305 }
306 break;
307 default:
308 break;
309 }
310
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000311 push = evo_wait(dev, EVO_MASTER, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000312 if (push) {
313 evo_mthd(push, 0x04c0 + (nv_crtc->index * 0x300), 3);
Ben Skeggs92854622011-11-11 23:49:06 +1000314 evo_data(push, (oY << 16) | oX);
315 evo_data(push, (oY << 16) | oX);
316 evo_data(push, (oY << 16) | oX);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000317 evo_mthd(push, 0x0494 + (nv_crtc->index * 0x300), 1);
318 evo_data(push, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000319 evo_mthd(push, 0x04b8 + (nv_crtc->index * 0x300), 1);
Ben Skeggs92854622011-11-11 23:49:06 +1000320 evo_data(push, (umode->vdisplay << 16) | umode->hdisplay);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000321 if (update) {
322 evo_mthd(push, 0x0080, 1);
323 evo_data(push, 0x00000000);
324 }
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000325 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000326 }
327
328 return 0;
329}
330
331static int
332nvd0_crtc_set_image(struct nouveau_crtc *nv_crtc, struct drm_framebuffer *fb,
333 int x, int y, bool update)
334{
335 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(fb);
336 u32 *push;
337
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000338 push = evo_wait(fb->dev, EVO_MASTER, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000339 if (push) {
340 evo_mthd(push, 0x0460 + (nv_crtc->index * 0x300), 1);
341 evo_data(push, nvfb->nvbo->bo.offset >> 8);
342 evo_mthd(push, 0x0468 + (nv_crtc->index * 0x300), 4);
343 evo_data(push, (fb->height << 16) | fb->width);
344 evo_data(push, nvfb->r_pitch);
345 evo_data(push, nvfb->r_format);
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000346 evo_data(push, nvfb->r_dma);
Ben Skeggsc6f2f712011-07-08 12:11:58 +1000347 evo_mthd(push, 0x04b0 + (nv_crtc->index * 0x300), 1);
348 evo_data(push, (y << 16) | x);
Ben Skeggsa46232e2011-07-07 15:23:48 +1000349 if (update) {
350 evo_mthd(push, 0x0080, 1);
351 evo_data(push, 0x00000000);
352 }
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000353 evo_kick(push, fb->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000354 }
355
Ben Skeggsc0cc92a2011-07-06 11:40:45 +1000356 nv_crtc->fb.tile_flags = nvfb->r_dma;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000357 return 0;
358}
359
360static void
361nvd0_crtc_cursor_show(struct nouveau_crtc *nv_crtc, bool show, bool update)
362{
363 struct drm_device *dev = nv_crtc->base.dev;
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000364 u32 *push = evo_wait(dev, EVO_MASTER, 16);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000365 if (push) {
366 if (show) {
367 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 2);
368 evo_data(push, 0x85000000);
369 evo_data(push, nv_crtc->cursor.nvbo->bo.offset >> 8);
370 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000371 evo_data(push, NvEvoVRAM);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000372 } else {
373 evo_mthd(push, 0x0480 + (nv_crtc->index * 0x300), 1);
374 evo_data(push, 0x05000000);
375 evo_mthd(push, 0x048c + (nv_crtc->index * 0x300), 1);
376 evo_data(push, 0x00000000);
377 }
378
379 if (update) {
380 evo_mthd(push, 0x0080, 1);
381 evo_data(push, 0x00000000);
382 }
383
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000384 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000385 }
386}
387
388static void
389nvd0_crtc_dpms(struct drm_crtc *crtc, int mode)
390{
391}
392
393static void
394nvd0_crtc_prepare(struct drm_crtc *crtc)
395{
396 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
397 u32 *push;
398
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000399 push = evo_wait(crtc->dev, EVO_MASTER, 2);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000400 if (push) {
401 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
402 evo_data(push, 0x00000000);
403 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 1);
404 evo_data(push, 0x03000000);
405 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
406 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000407 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000408 }
409
410 nvd0_crtc_cursor_show(nv_crtc, false, false);
411}
412
413static void
414nvd0_crtc_commit(struct drm_crtc *crtc)
415{
416 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
417 u32 *push;
418
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000419 push = evo_wait(crtc->dev, EVO_MASTER, 32);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000420 if (push) {
421 evo_mthd(push, 0x0474 + (nv_crtc->index * 0x300), 1);
422 evo_data(push, nv_crtc->fb.tile_flags);
423 evo_mthd(push, 0x0440 + (nv_crtc->index * 0x300), 4);
424 evo_data(push, 0x83000000);
425 evo_data(push, nv_crtc->lut.nvbo->bo.offset >> 8);
426 evo_data(push, 0x00000000);
427 evo_data(push, 0x00000000);
428 evo_mthd(push, 0x045c + (nv_crtc->index * 0x300), 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +1000429 evo_data(push, NvEvoVRAM);
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000430 evo_mthd(push, 0x0430 + (nv_crtc->index * 0x300), 1);
431 evo_data(push, 0xffffff00);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000432 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000433 }
434
435 nvd0_crtc_cursor_show(nv_crtc, nv_crtc->cursor.visible, true);
436}
437
438static bool
439nvd0_crtc_mode_fixup(struct drm_crtc *crtc, struct drm_display_mode *mode,
440 struct drm_display_mode *adjusted_mode)
441{
442 return true;
443}
444
445static int
446nvd0_crtc_swap_fbs(struct drm_crtc *crtc, struct drm_framebuffer *old_fb)
447{
448 struct nouveau_framebuffer *nvfb = nouveau_framebuffer(crtc->fb);
449 int ret;
450
451 ret = nouveau_bo_pin(nvfb->nvbo, TTM_PL_FLAG_VRAM);
452 if (ret)
453 return ret;
454
455 if (old_fb) {
456 nvfb = nouveau_framebuffer(old_fb);
457 nouveau_bo_unpin(nvfb->nvbo);
458 }
459
460 return 0;
461}
462
463static int
464nvd0_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *umode,
465 struct drm_display_mode *mode, int x, int y,
466 struct drm_framebuffer *old_fb)
467{
468 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
469 struct nouveau_connector *nv_connector;
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000470 u32 ilace = (mode->flags & DRM_MODE_FLAG_INTERLACE) ? 2 : 1;
471 u32 vscan = (mode->flags & DRM_MODE_FLAG_DBLSCAN) ? 2 : 1;
472 u32 hactive, hsynce, hbackp, hfrontp, hblanke, hblanks;
473 u32 vactive, vsynce, vbackp, vfrontp, vblanke, vblanks;
474 u32 vblan2e = 0, vblan2s = 1;
475 u32 magic = 0x31ec6000;
Ben Skeggs629c1b92011-07-08 09:43:20 +1000476 u32 syncs, *push;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000477 int ret;
478
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000479 hactive = mode->htotal;
480 hsynce = mode->hsync_end - mode->hsync_start - 1;
481 hbackp = mode->htotal - mode->hsync_end;
482 hblanke = hsynce + hbackp;
483 hfrontp = mode->hsync_start - mode->hdisplay;
484 hblanks = mode->htotal - hfrontp - 1;
485
486 vactive = mode->vtotal * vscan / ilace;
487 vsynce = ((mode->vsync_end - mode->vsync_start) * vscan / ilace) - 1;
488 vbackp = (mode->vtotal - mode->vsync_end) * vscan / ilace;
489 vblanke = vsynce + vbackp;
490 vfrontp = (mode->vsync_start - mode->vdisplay) * vscan / ilace;
491 vblanks = vactive - vfrontp - 1;
492 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
493 vblan2e = vactive + vsynce + vbackp;
494 vblan2s = vblan2e + (mode->vdisplay * vscan / ilace);
495 vactive = (vactive * 2) + 1;
496 magic |= 0x00000001;
497 }
498
Ben Skeggs629c1b92011-07-08 09:43:20 +1000499 syncs = 0x00000001;
500 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
501 syncs |= 0x00000008;
502 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
503 syncs |= 0x00000010;
504
Ben Skeggs438d99e2011-07-05 16:48:06 +1000505 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
506 if (ret)
507 return ret;
508
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000509 push = evo_wait(crtc->dev, EVO_MASTER, 64);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000510 if (push) {
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000511 evo_mthd(push, 0x0410 + (nv_crtc->index * 0x300), 6);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000512 evo_data(push, 0x00000000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000513 evo_data(push, (vactive << 16) | hactive);
514 evo_data(push, ( vsynce << 16) | hsynce);
515 evo_data(push, (vblanke << 16) | hblanke);
516 evo_data(push, (vblanks << 16) | hblanks);
517 evo_data(push, (vblan2e << 16) | vblan2s);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000518 evo_mthd(push, 0x042c + (nv_crtc->index * 0x300), 1);
519 evo_data(push, 0x00000000); /* ??? */
520 evo_mthd(push, 0x0450 + (nv_crtc->index * 0x300), 3);
521 evo_data(push, mode->clock * 1000);
522 evo_data(push, 0x00200000); /* ??? */
523 evo_data(push, mode->clock * 1000);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000524 evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
Ben Skeggs629c1b92011-07-08 09:43:20 +1000525 evo_data(push, syncs);
Ben Skeggs2d1d8982011-11-11 23:39:22 +1000526 evo_data(push, magic);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000527 evo_kick(push, crtc->dev, EVO_MASTER);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000528 }
529
530 nv_connector = nouveau_crtc_connector_get(nv_crtc);
Ben Skeggs488ff202011-10-17 10:38:10 +1000531 nvd0_crtc_set_dither(nv_crtc, false);
532 nvd0_crtc_set_scale(nv_crtc, false);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000533 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, false);
534 return 0;
535}
536
537static int
538nvd0_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
539 struct drm_framebuffer *old_fb)
540{
541 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
542 int ret;
543
Ben Skeggs84e2ad82011-08-26 09:40:39 +1000544 if (!crtc->fb) {
545 NV_DEBUG_KMS(crtc->dev, "No FB bound\n");
546 return 0;
547 }
548
Ben Skeggs438d99e2011-07-05 16:48:06 +1000549 ret = nvd0_crtc_swap_fbs(crtc, old_fb);
550 if (ret)
551 return ret;
552
553 nvd0_crtc_set_image(nv_crtc, crtc->fb, x, y, true);
554 return 0;
555}
556
557static int
558nvd0_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
559 struct drm_framebuffer *fb, int x, int y,
560 enum mode_set_atomic state)
561{
562 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
563 nvd0_crtc_set_image(nv_crtc, fb, x, y, true);
564 return 0;
565}
566
567static void
568nvd0_crtc_lut_load(struct drm_crtc *crtc)
569{
570 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
571 void __iomem *lut = nvbo_kmap_obj_iovirtual(nv_crtc->lut.nvbo);
572 int i;
573
574 for (i = 0; i < 256; i++) {
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000575 writew(0x6000 + (nv_crtc->lut.r[i] >> 2), lut + (i * 0x20) + 0);
576 writew(0x6000 + (nv_crtc->lut.g[i] >> 2), lut + (i * 0x20) + 2);
577 writew(0x6000 + (nv_crtc->lut.b[i] >> 2), lut + (i * 0x20) + 4);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000578 }
579}
580
581static int
582nvd0_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
583 uint32_t handle, uint32_t width, uint32_t height)
584{
585 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
586 struct drm_device *dev = crtc->dev;
587 struct drm_gem_object *gem;
588 struct nouveau_bo *nvbo;
589 bool visible = (handle != 0);
590 int i, ret = 0;
591
592 if (visible) {
593 if (width != 64 || height != 64)
594 return -EINVAL;
595
596 gem = drm_gem_object_lookup(dev, file_priv, handle);
597 if (unlikely(!gem))
598 return -ENOENT;
599 nvbo = nouveau_gem_object(gem);
600
601 ret = nouveau_bo_map(nvbo);
602 if (ret == 0) {
603 for (i = 0; i < 64 * 64; i++) {
604 u32 v = nouveau_bo_rd32(nvbo, i);
605 nouveau_bo_wr32(nv_crtc->cursor.nvbo, i, v);
606 }
607 nouveau_bo_unmap(nvbo);
608 }
609
610 drm_gem_object_unreference_unlocked(gem);
611 }
612
613 if (visible != nv_crtc->cursor.visible) {
614 nvd0_crtc_cursor_show(nv_crtc, visible, true);
615 nv_crtc->cursor.visible = visible;
616 }
617
618 return ret;
619}
620
621static int
622nvd0_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
623{
624 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
Ben Skeggs4acd4292011-11-12 12:57:54 +1000625 int ch = EVO_CURS(nv_crtc->index);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000626
Ben Skeggs4acd4292011-11-12 12:57:54 +1000627 evo_piow(crtc->dev, ch, 0x0084, (y << 16) | x);
628 evo_piow(crtc->dev, ch, 0x0080, 0x00000000);
Ben Skeggs438d99e2011-07-05 16:48:06 +1000629 return 0;
630}
631
632static void
633nvd0_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
634 uint32_t start, uint32_t size)
635{
636 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
637 u32 end = max(start + size, (u32)256);
638 u32 i;
639
640 for (i = start; i < end; i++) {
641 nv_crtc->lut.r[i] = r[i];
642 nv_crtc->lut.g[i] = g[i];
643 nv_crtc->lut.b[i] = b[i];
644 }
645
646 nvd0_crtc_lut_load(crtc);
647}
648
649static void
650nvd0_crtc_destroy(struct drm_crtc *crtc)
651{
652 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
653 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
654 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
655 nouveau_bo_unmap(nv_crtc->lut.nvbo);
656 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
657 drm_crtc_cleanup(crtc);
658 kfree(crtc);
659}
660
661static const struct drm_crtc_helper_funcs nvd0_crtc_hfunc = {
662 .dpms = nvd0_crtc_dpms,
663 .prepare = nvd0_crtc_prepare,
664 .commit = nvd0_crtc_commit,
665 .mode_fixup = nvd0_crtc_mode_fixup,
666 .mode_set = nvd0_crtc_mode_set,
667 .mode_set_base = nvd0_crtc_mode_set_base,
668 .mode_set_base_atomic = nvd0_crtc_mode_set_base_atomic,
669 .load_lut = nvd0_crtc_lut_load,
670};
671
672static const struct drm_crtc_funcs nvd0_crtc_func = {
673 .cursor_set = nvd0_crtc_cursor_set,
674 .cursor_move = nvd0_crtc_cursor_move,
675 .gamma_set = nvd0_crtc_gamma_set,
676 .set_config = drm_crtc_helper_set_config,
677 .destroy = nvd0_crtc_destroy,
678};
679
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000680static void
681nvd0_cursor_set_pos(struct nouveau_crtc *nv_crtc, int x, int y)
682{
683}
684
685static void
686nvd0_cursor_set_offset(struct nouveau_crtc *nv_crtc, uint32_t offset)
687{
688}
689
Ben Skeggs438d99e2011-07-05 16:48:06 +1000690static int
691nvd0_crtc_create(struct drm_device *dev, int index)
692{
693 struct nouveau_crtc *nv_crtc;
694 struct drm_crtc *crtc;
695 int ret, i;
696
697 nv_crtc = kzalloc(sizeof(*nv_crtc), GFP_KERNEL);
698 if (!nv_crtc)
699 return -ENOMEM;
700
701 nv_crtc->index = index;
702 nv_crtc->set_dither = nvd0_crtc_set_dither;
703 nv_crtc->set_scale = nvd0_crtc_set_scale;
Ben Skeggsc20ab3e2011-08-25 14:09:43 +1000704 nv_crtc->cursor.set_offset = nvd0_cursor_set_offset;
705 nv_crtc->cursor.set_pos = nvd0_cursor_set_pos;
Ben Skeggs438d99e2011-07-05 16:48:06 +1000706 for (i = 0; i < 256; i++) {
707 nv_crtc->lut.r[i] = i << 8;
708 nv_crtc->lut.g[i] = i << 8;
709 nv_crtc->lut.b[i] = i << 8;
710 }
711
712 crtc = &nv_crtc->base;
713 drm_crtc_init(dev, crtc, &nvd0_crtc_func);
714 drm_crtc_helper_add(crtc, &nvd0_crtc_hfunc);
715 drm_mode_crtc_set_gamma_size(crtc, 256);
716
717 ret = nouveau_bo_new(dev, 64 * 64 * 4, 0x100, TTM_PL_FLAG_VRAM,
718 0, 0x0000, &nv_crtc->cursor.nvbo);
719 if (!ret) {
720 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
721 if (!ret)
722 ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
723 if (ret)
724 nouveau_bo_ref(NULL, &nv_crtc->cursor.nvbo);
725 }
726
727 if (ret)
728 goto out;
729
Ben Skeggs8ea0d4a2011-07-07 14:49:24 +1000730 ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
Ben Skeggs438d99e2011-07-05 16:48:06 +1000731 0, 0x0000, &nv_crtc->lut.nvbo);
732 if (!ret) {
733 ret = nouveau_bo_pin(nv_crtc->lut.nvbo, TTM_PL_FLAG_VRAM);
734 if (!ret)
735 ret = nouveau_bo_map(nv_crtc->lut.nvbo);
736 if (ret)
737 nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
738 }
739
740 if (ret)
741 goto out;
742
743 nvd0_crtc_lut_load(crtc);
744
745out:
746 if (ret)
747 nvd0_crtc_destroy(crtc);
748 return ret;
749}
750
751/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000752 * DAC
753 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000754static void
755nvd0_dac_dpms(struct drm_encoder *encoder, int mode)
756{
757 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
758 struct drm_device *dev = encoder->dev;
759 int or = nv_encoder->or;
760 u32 dpms_ctrl;
761
762 dpms_ctrl = 0x80000000;
763 if (mode == DRM_MODE_DPMS_STANDBY || mode == DRM_MODE_DPMS_OFF)
764 dpms_ctrl |= 0x00000001;
765 if (mode == DRM_MODE_DPMS_SUSPEND || mode == DRM_MODE_DPMS_OFF)
766 dpms_ctrl |= 0x00000004;
767
768 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
769 nv_mask(dev, 0x61a004 + (or * 0x0800), 0xc000007f, dpms_ctrl);
770 nv_wait(dev, 0x61a004 + (or * 0x0800), 0x80000000, 0x00000000);
771}
772
773static bool
774nvd0_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
775 struct drm_display_mode *adjusted_mode)
776{
777 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
778 struct nouveau_connector *nv_connector;
779
780 nv_connector = nouveau_encoder_connector_get(nv_encoder);
781 if (nv_connector && nv_connector->native_mode) {
782 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
783 int id = adjusted_mode->base.id;
784 *adjusted_mode = *nv_connector->native_mode;
785 adjusted_mode->base.id = id;
786 }
787 }
788
789 return true;
790}
791
792static void
793nvd0_dac_prepare(struct drm_encoder *encoder)
794{
795}
796
797static void
798nvd0_dac_commit(struct drm_encoder *encoder)
799{
800}
801
802static void
803nvd0_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
804 struct drm_display_mode *adjusted_mode)
805{
806 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
807 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
808 u32 *push;
809
810 nvd0_dac_dpms(encoder, DRM_MODE_DPMS_ON);
811
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000812 push = evo_wait(encoder->dev, EVO_MASTER, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000813 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +1000814 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 2);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000815 evo_data(push, 1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +1000816 evo_data(push, 0x00ff);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000817 evo_kick(push, encoder->dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000818 }
819
820 nv_encoder->crtc = encoder->crtc;
821}
822
823static void
824nvd0_dac_disconnect(struct drm_encoder *encoder)
825{
826 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
827 struct drm_device *dev = encoder->dev;
828 u32 *push;
829
830 if (nv_encoder->crtc) {
831 nvd0_crtc_prepare(nv_encoder->crtc);
832
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000833 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000834 if (push) {
835 evo_mthd(push, 0x0180 + (nv_encoder->or * 0x20), 1);
836 evo_data(push, 0x00000000);
837 evo_mthd(push, 0x0080, 1);
838 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +1000839 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000840 }
841
842 nv_encoder->crtc = NULL;
843 }
844}
845
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000846static enum drm_connector_status
847nvd0_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
848{
Ben Skeggsb6819932011-07-08 11:14:50 +1000849 enum drm_connector_status status = connector_status_disconnected;
850 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
851 struct drm_device *dev = encoder->dev;
852 int or = nv_encoder->or;
853 u32 load;
854
855 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00100000);
856 udelay(9500);
857 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x80000000);
858
859 load = nv_rd32(dev, 0x61a00c + (or * 0x800));
860 if ((load & 0x38000000) == 0x38000000)
861 status = connector_status_connected;
862
863 nv_wr32(dev, 0x61a00c + (or * 0x800), 0x00000000);
864 return status;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000865}
866
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000867static void
868nvd0_dac_destroy(struct drm_encoder *encoder)
869{
870 drm_encoder_cleanup(encoder);
871 kfree(encoder);
872}
873
874static const struct drm_encoder_helper_funcs nvd0_dac_hfunc = {
875 .dpms = nvd0_dac_dpms,
876 .mode_fixup = nvd0_dac_mode_fixup,
877 .prepare = nvd0_dac_prepare,
878 .commit = nvd0_dac_commit,
879 .mode_set = nvd0_dac_mode_set,
880 .disable = nvd0_dac_disconnect,
881 .get_crtc = nvd0_display_crtc_get,
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000882 .detect = nvd0_dac_detect
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000883};
884
885static const struct drm_encoder_funcs nvd0_dac_func = {
886 .destroy = nvd0_dac_destroy,
887};
888
889static int
890nvd0_dac_create(struct drm_connector *connector, struct dcb_entry *dcbe)
891{
892 struct drm_device *dev = connector->dev;
893 struct nouveau_encoder *nv_encoder;
894 struct drm_encoder *encoder;
895
896 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
897 if (!nv_encoder)
898 return -ENOMEM;
899 nv_encoder->dcb = dcbe;
900 nv_encoder->or = ffs(dcbe->or) - 1;
901
902 encoder = to_drm_encoder(nv_encoder);
903 encoder->possible_crtcs = dcbe->heads;
904 encoder->possible_clones = 0;
905 drm_encoder_init(dev, encoder, &nvd0_dac_func, DRM_MODE_ENCODER_DAC);
906 drm_encoder_helper_add(encoder, &nvd0_dac_hfunc);
907
908 drm_mode_connector_attach_encoder(connector, encoder);
909 return 0;
910}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000911
912/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +1000913 * Audio
914 *****************************************************************************/
915static void
916nvd0_audio_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
917{
918 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
919 struct nouveau_connector *nv_connector;
920 struct drm_device *dev = encoder->dev;
921 int i, or = nv_encoder->or * 0x30;
922
923 nv_connector = nouveau_encoder_connector_get(nv_encoder);
924 if (!drm_detect_monitor_audio(nv_connector->edid))
925 return;
926
927 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000001);
928
929 drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
930 if (nv_connector->base.eld[0]) {
931 u8 *eld = nv_connector->base.eld;
932
933 for (i = 0; i < eld[2] * 4; i++)
934 nv_wr32(dev, 0x10ec00 + or, (i << 8) | eld[i]);
935 for (i = eld[2] * 4; i < 0x60; i++)
936 nv_wr32(dev, 0x10ec00 + or, (i << 8) | 0x00);
937
938 nv_mask(dev, 0x10ec10 + or, 0x80000002, 0x80000002);
939 }
940}
941
942static void
943nvd0_audio_disconnect(struct drm_encoder *encoder)
944{
945 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
946 struct drm_device *dev = encoder->dev;
947 int or = nv_encoder->or * 0x30;
948
949 nv_mask(dev, 0x10ec10 + or, 0x80000003, 0x80000000);
950}
951
952/******************************************************************************
953 * HDMI
954 *****************************************************************************/
955static void
956nvd0_hdmi_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode)
957{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000958 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
959 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
960 struct nouveau_connector *nv_connector;
961 struct drm_device *dev = encoder->dev;
962 int head = nv_crtc->index * 0x800;
963 u32 rekey = 56; /* binary driver, and tegra constant */
964 u32 max_ac_packet;
965
966 nv_connector = nouveau_encoder_connector_get(nv_encoder);
967 if (!drm_detect_hdmi_monitor(nv_connector->edid))
968 return;
969
970 max_ac_packet = mode->htotal - mode->hdisplay;
971 max_ac_packet -= rekey;
972 max_ac_packet -= 18; /* constant from tegra */
973 max_ac_packet /= 32;
974
975 /* AVI InfoFrame */
976 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
977 nv_wr32(dev, 0x61671c + head, 0x000d0282);
978 nv_wr32(dev, 0x616720 + head, 0x0000006f);
979 nv_wr32(dev, 0x616724 + head, 0x00000000);
980 nv_wr32(dev, 0x616728 + head, 0x00000000);
981 nv_wr32(dev, 0x61672c + head, 0x00000000);
982 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000001);
983
984 /* ??? InfoFrame? */
985 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
986 nv_wr32(dev, 0x6167ac + head, 0x00000010);
987 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000001);
988
989 /* HDMI_CTRL */
990 nv_mask(dev, 0x616798 + head, 0x401f007f, 0x40000000 | rekey |
991 max_ac_packet << 16);
992
Ben Skeggs091e40c2011-11-11 20:46:00 +1000993 /* NFI, audio doesn't work without it though.. */
994 nv_mask(dev, 0x616548 + head, 0x00000070, 0x00000000);
995
Ben Skeggs78951d22011-11-11 18:13:13 +1000996 nvd0_audio_mode_set(encoder, mode);
997}
998
999static void
1000nvd0_hdmi_disconnect(struct drm_encoder *encoder)
1001{
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001002 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1003 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1004 struct drm_device *dev = encoder->dev;
1005 int head = nv_crtc->index * 0x800;
1006
Ben Skeggs78951d22011-11-11 18:13:13 +10001007 nvd0_audio_disconnect(encoder);
Ben Skeggs64d9cc02011-11-11 19:51:20 +10001008
1009 nv_mask(dev, 0x616798 + head, 0x40000000, 0x00000000);
1010 nv_mask(dev, 0x6167a4 + head, 0x00000001, 0x00000000);
1011 nv_mask(dev, 0x616714 + head, 0x00000001, 0x00000000);
Ben Skeggs78951d22011-11-11 18:13:13 +10001012}
1013
1014/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001015 * SOR
1016 *****************************************************************************/
Ben Skeggs83fc0832011-07-05 13:08:40 +10001017static void
1018nvd0_sor_dpms(struct drm_encoder *encoder, int mode)
1019{
1020 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1021 struct drm_device *dev = encoder->dev;
1022 struct drm_encoder *partner;
1023 int or = nv_encoder->or;
1024 u32 dpms_ctrl;
1025
1026 nv_encoder->last_dpms = mode;
1027
1028 list_for_each_entry(partner, &dev->mode_config.encoder_list, head) {
1029 struct nouveau_encoder *nv_partner = nouveau_encoder(partner);
1030
1031 if (partner->encoder_type != DRM_MODE_ENCODER_TMDS)
1032 continue;
1033
1034 if (nv_partner != nv_encoder &&
Ben Skeggs26cfa812011-11-17 09:10:02 +10001035 nv_partner->dcb->or == nv_encoder->dcb->or) {
Ben Skeggs83fc0832011-07-05 13:08:40 +10001036 if (nv_partner->last_dpms == DRM_MODE_DPMS_ON)
1037 return;
1038 break;
1039 }
1040 }
1041
1042 dpms_ctrl = (mode == DRM_MODE_DPMS_ON);
1043 dpms_ctrl |= 0x80000000;
1044
1045 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1046 nv_mask(dev, 0x61c004 + (or * 0x0800), 0x80000001, dpms_ctrl);
1047 nv_wait(dev, 0x61c004 + (or * 0x0800), 0x80000000, 0x00000000);
1048 nv_wait(dev, 0x61c030 + (or * 0x0800), 0x10000000, 0x00000000);
1049}
1050
1051static bool
1052nvd0_sor_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
1053 struct drm_display_mode *adjusted_mode)
1054{
1055 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1056 struct nouveau_connector *nv_connector;
1057
1058 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1059 if (nv_connector && nv_connector->native_mode) {
1060 if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
1061 int id = adjusted_mode->base.id;
1062 *adjusted_mode = *nv_connector->native_mode;
1063 adjusted_mode->base.id = id;
1064 }
1065 }
1066
1067 return true;
1068}
1069
1070static void
1071nvd0_sor_prepare(struct drm_encoder *encoder)
1072{
1073}
1074
1075static void
1076nvd0_sor_commit(struct drm_encoder *encoder)
1077{
1078}
1079
1080static void
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001081nvd0_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
1082 struct drm_display_mode *mode)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001083{
Ben Skeggs78951d22011-11-11 18:13:13 +10001084 struct drm_device *dev = encoder->dev;
1085 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001086 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1087 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001088 struct nouveau_connector *nv_connector;
1089 struct nvbios *bios = &dev_priv->vbios;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001090 u32 mode_ctrl = (1 << nv_crtc->index);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001091 u32 *push, or_config;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001092
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001093 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1094 switch (nv_encoder->dcb->type) {
1095 case OUTPUT_TMDS:
1096 if (nv_encoder->dcb->sorconf.link & 1) {
1097 if (mode->clock < 165000)
1098 mode_ctrl |= 0x00000100;
1099 else
1100 mode_ctrl |= 0x00000500;
1101 } else {
1102 mode_ctrl |= 0x00000200;
1103 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001104
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001105 or_config = (mode_ctrl & 0x00000f00) >> 8;
1106 if (mode->clock >= 165000)
1107 or_config |= 0x0100;
Ben Skeggs78951d22011-11-11 18:13:13 +10001108
1109 nvd0_hdmi_mode_set(encoder, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001110 break;
1111 case OUTPUT_LVDS:
1112 or_config = (mode_ctrl & 0x00000f00) >> 8;
1113 if (bios->fp_no_ddc) {
1114 if (bios->fp.dual_link)
1115 or_config |= 0x0100;
1116 if (bios->fp.if_is_24bit)
1117 or_config |= 0x0200;
1118 } else {
1119 if (nv_connector->dcb->type == DCB_CONNECTOR_LVDS_SPWG) {
1120 if (((u8 *)nv_connector->edid)[121] == 2)
1121 or_config |= 0x0100;
1122 } else
1123 if (mode->clock >= bios->fp.duallink_transition_clk) {
1124 or_config |= 0x0100;
1125 }
1126
1127 if (or_config & 0x0100) {
1128 if (bios->fp.strapless_is_24bit & 2)
1129 or_config |= 0x0200;
1130 } else {
1131 if (bios->fp.strapless_is_24bit & 1)
1132 or_config |= 0x0200;
1133 }
1134
1135 if (nv_connector->base.display_info.bpc == 8)
1136 or_config |= 0x0200;
1137
1138 }
1139 break;
1140 default:
1141 BUG_ON(1);
1142 break;
1143 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001144
Ben Skeggs83fc0832011-07-05 13:08:40 +10001145 nvd0_sor_dpms(encoder, DRM_MODE_DPMS_ON);
1146
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001147 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001148 if (push) {
Ben Skeggsff8ff502011-07-08 11:53:37 +10001149 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 2);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001150 evo_data(push, mode_ctrl);
Ben Skeggsff8ff502011-07-08 11:53:37 +10001151 evo_data(push, or_config);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001152 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001153 }
1154
1155 nv_encoder->crtc = encoder->crtc;
1156}
1157
1158static void
1159nvd0_sor_disconnect(struct drm_encoder *encoder)
1160{
1161 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1162 struct drm_device *dev = encoder->dev;
Ben Skeggs438d99e2011-07-05 16:48:06 +10001163 u32 *push;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001164
1165 if (nv_encoder->crtc) {
Ben Skeggs438d99e2011-07-05 16:48:06 +10001166 nvd0_crtc_prepare(nv_encoder->crtc);
1167
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001168 push = evo_wait(dev, EVO_MASTER, 4);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001169 if (push) {
1170 evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
1171 evo_data(push, 0x00000000);
1172 evo_mthd(push, 0x0080, 1);
1173 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001174 evo_kick(push, dev, EVO_MASTER);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001175 }
1176
Ben Skeggs78951d22011-11-11 18:13:13 +10001177 nvd0_hdmi_disconnect(encoder);
1178
Ben Skeggs83fc0832011-07-05 13:08:40 +10001179 nv_encoder->crtc = NULL;
1180 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1181 }
1182}
1183
1184static void
1185nvd0_sor_destroy(struct drm_encoder *encoder)
1186{
1187 drm_encoder_cleanup(encoder);
1188 kfree(encoder);
1189}
1190
1191static const struct drm_encoder_helper_funcs nvd0_sor_hfunc = {
1192 .dpms = nvd0_sor_dpms,
1193 .mode_fixup = nvd0_sor_mode_fixup,
1194 .prepare = nvd0_sor_prepare,
1195 .commit = nvd0_sor_commit,
1196 .mode_set = nvd0_sor_mode_set,
1197 .disable = nvd0_sor_disconnect,
1198 .get_crtc = nvd0_display_crtc_get,
1199};
1200
1201static const struct drm_encoder_funcs nvd0_sor_func = {
1202 .destroy = nvd0_sor_destroy,
1203};
1204
1205static int
1206nvd0_sor_create(struct drm_connector *connector, struct dcb_entry *dcbe)
1207{
1208 struct drm_device *dev = connector->dev;
1209 struct nouveau_encoder *nv_encoder;
1210 struct drm_encoder *encoder;
1211
1212 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1213 if (!nv_encoder)
1214 return -ENOMEM;
1215 nv_encoder->dcb = dcbe;
1216 nv_encoder->or = ffs(dcbe->or) - 1;
1217 nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
1218
1219 encoder = to_drm_encoder(nv_encoder);
1220 encoder->possible_crtcs = dcbe->heads;
1221 encoder->possible_clones = 0;
1222 drm_encoder_init(dev, encoder, &nvd0_sor_func, DRM_MODE_ENCODER_TMDS);
1223 drm_encoder_helper_add(encoder, &nvd0_sor_hfunc);
1224
1225 drm_mode_connector_attach_encoder(connector, encoder);
1226 return 0;
1227}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001228
1229/******************************************************************************
1230 * IRQ
1231 *****************************************************************************/
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001232static struct dcb_entry *
1233lookup_dcb(struct drm_device *dev, int id, u32 mc)
1234{
1235 struct drm_nouveau_private *dev_priv = dev->dev_private;
1236 int type, or, i;
1237
1238 if (id < 4) {
1239 type = OUTPUT_ANALOG;
1240 or = id;
1241 } else {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001242 switch (mc & 0x00000f00) {
1243 case 0x00000000: type = OUTPUT_LVDS; break;
1244 case 0x00000100: type = OUTPUT_TMDS; break;
1245 case 0x00000200: type = OUTPUT_TMDS; break;
1246 case 0x00000500: type = OUTPUT_TMDS; break;
1247 default:
Ben Skeggsee417792011-07-08 14:34:45 +10001248 NV_ERROR(dev, "PDISP: unknown SOR mc 0x%08x\n", mc);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001249 return NULL;
1250 }
1251
1252 or = id - 4;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001253 }
1254
1255 for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
1256 struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];
1257 if (dcb->type == type && (dcb->or & (1 << or)))
1258 return dcb;
1259 }
1260
Ben Skeggsee417792011-07-08 14:34:45 +10001261 NV_ERROR(dev, "PDISP: DCB for %d/0x%08x not found\n", id, mc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001262 return NULL;
1263}
1264
Ben Skeggs46005222011-07-05 11:01:13 +10001265static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001266nvd0_display_unk1_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001267{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001268 struct dcb_entry *dcb;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001269 int i;
1270
Ben Skeggsee417792011-07-08 14:34:45 +10001271 for (i = 0; mask && i < 8; i++) {
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001272 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
Ben Skeggsee417792011-07-08 14:34:45 +10001273 if (!(mcc & (1 << crtc)))
1274 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001275
Ben Skeggsee417792011-07-08 14:34:45 +10001276 dcb = lookup_dcb(dev, i, mcc);
1277 if (!dcb)
1278 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001279
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001280 nouveau_bios_run_display_table(dev, 0x0000, -1, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001281 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001282
Ben Skeggs270a5742011-07-05 14:16:05 +10001283 nv_wr32(dev, 0x6101d4, 0x00000000);
1284 nv_wr32(dev, 0x6109d4, 0x00000000);
1285 nv_wr32(dev, 0x6101d0, 0x80000000);
1286}
1287
1288static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001289nvd0_display_unk2_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001290{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001291 struct dcb_entry *dcb;
Ben Skeggs37b034a2011-07-08 14:43:19 +10001292 u32 or, tmp, pclk;
Ben Skeggsee417792011-07-08 14:34:45 +10001293 int i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001294
Ben Skeggsee417792011-07-08 14:34:45 +10001295 for (i = 0; mask && i < 8; i++) {
1296 u32 mcc = nv_rd32(dev, 0x640180 + (i * 0x20));
1297 if (!(mcc & (1 << crtc)))
1298 continue;
1299
1300 dcb = lookup_dcb(dev, i, mcc);
1301 if (!dcb)
1302 continue;
1303
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001304 nouveau_bios_run_display_table(dev, 0x0000, -2, dcb, crtc);
Ben Skeggsee417792011-07-08 14:34:45 +10001305 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001306
Ben Skeggsee417792011-07-08 14:34:45 +10001307 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
1308 if (mask & 0x00010000) {
1309 nv50_crtc_set_clock(dev, crtc, pclk);
1310 }
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001311
Ben Skeggsee417792011-07-08 14:34:45 +10001312 for (i = 0; mask && i < 8; i++) {
1313 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1314 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1315 if (!(mcp & (1 << crtc)))
1316 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001317
Ben Skeggsee417792011-07-08 14:34:45 +10001318 dcb = lookup_dcb(dev, i, mcp);
1319 if (!dcb)
1320 continue;
1321 or = ffs(dcb->or) - 1;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001322
Ben Skeggsee417792011-07-08 14:34:45 +10001323 nouveau_bios_run_display_table(dev, cfg, pclk, dcb, crtc);
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001324
Ben Skeggsee417792011-07-08 14:34:45 +10001325 nv_wr32(dev, 0x612200 + (crtc * 0x800), 0x00000000);
1326 switch (dcb->type) {
1327 case OUTPUT_ANALOG:
1328 nv_wr32(dev, 0x612280 + (or * 0x800), 0x00000000);
1329 break;
1330 case OUTPUT_TMDS:
1331 case OUTPUT_LVDS:
1332 if (cfg & 0x00000100)
1333 tmp = 0x00000101;
1334 else
1335 tmp = 0x00000000;
1336
1337 nv_mask(dev, 0x612300 + (or * 0x800), 0x00000707, tmp);
1338 break;
1339 default:
1340 break;
1341 }
1342
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001343 break;
1344 }
1345
Ben Skeggs270a5742011-07-05 14:16:05 +10001346 nv_wr32(dev, 0x6101d4, 0x00000000);
1347 nv_wr32(dev, 0x6109d4, 0x00000000);
1348 nv_wr32(dev, 0x6101d0, 0x80000000);
1349}
1350
1351static void
Ben Skeggs37b034a2011-07-08 14:43:19 +10001352nvd0_display_unk4_handler(struct drm_device *dev, u32 crtc, u32 mask)
Ben Skeggs270a5742011-07-05 14:16:05 +10001353{
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001354 struct dcb_entry *dcb;
Ben Skeggsee417792011-07-08 14:34:45 +10001355 int pclk, i;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001356
Ben Skeggsee417792011-07-08 14:34:45 +10001357 pclk = nv_rd32(dev, 0x660450 + (crtc * 0x300)) / 1000;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001358
Ben Skeggsee417792011-07-08 14:34:45 +10001359 for (i = 0; mask && i < 8; i++) {
1360 u32 mcp = nv_rd32(dev, 0x660180 + (i * 0x20));
1361 u32 cfg = nv_rd32(dev, 0x660184 + (i * 0x20));
1362 if (!(mcp & (1 << crtc)))
1363 continue;
Ben Skeggs3a89cd02011-07-07 10:47:10 +10001364
Ben Skeggsee417792011-07-08 14:34:45 +10001365 dcb = lookup_dcb(dev, i, mcp);
1366 if (!dcb)
1367 continue;
1368
1369 nouveau_bios_run_display_table(dev, cfg, -pclk, dcb, crtc);
1370 }
1371
Ben Skeggs270a5742011-07-05 14:16:05 +10001372 nv_wr32(dev, 0x6101d4, 0x00000000);
1373 nv_wr32(dev, 0x6109d4, 0x00000000);
1374 nv_wr32(dev, 0x6101d0, 0x80000000);
1375}
1376
1377static void
Ben Skeggsf20ce962011-07-08 13:17:01 +10001378nvd0_display_bh(unsigned long data)
1379{
1380 struct drm_device *dev = (struct drm_device *)data;
1381 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001382 u32 mask, crtc;
1383 int i;
1384
1385 if (drm_debug & (DRM_UT_DRIVER | DRM_UT_KMS)) {
1386 NV_INFO(dev, "PDISP: modeset req %d\n", disp->modeset);
1387 NV_INFO(dev, " STAT: 0x%08x 0x%08x 0x%08x\n",
1388 nv_rd32(dev, 0x6101d0),
1389 nv_rd32(dev, 0x6101d4), nv_rd32(dev, 0x6109d4));
1390 for (i = 0; i < 8; i++) {
1391 NV_INFO(dev, " %s%d: 0x%08x 0x%08x\n",
1392 i < 4 ? "DAC" : "SOR", i,
1393 nv_rd32(dev, 0x640180 + (i * 0x20)),
1394 nv_rd32(dev, 0x660180 + (i * 0x20)));
1395 }
1396 }
1397
1398 mask = nv_rd32(dev, 0x6101d4);
1399 crtc = 0;
1400 if (!mask) {
1401 mask = nv_rd32(dev, 0x6109d4);
1402 crtc = 1;
1403 }
Ben Skeggsf20ce962011-07-08 13:17:01 +10001404
Ben Skeggsee417792011-07-08 14:34:45 +10001405 if (disp->modeset & 0x00000001)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001406 nvd0_display_unk1_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001407 if (disp->modeset & 0x00000002)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001408 nvd0_display_unk2_handler(dev, crtc, mask);
Ben Skeggsee417792011-07-08 14:34:45 +10001409 if (disp->modeset & 0x00000004)
Ben Skeggs37b034a2011-07-08 14:43:19 +10001410 nvd0_display_unk4_handler(dev, crtc, mask);
Ben Skeggsf20ce962011-07-08 13:17:01 +10001411}
1412
1413static void
Ben Skeggs46005222011-07-05 11:01:13 +10001414nvd0_display_intr(struct drm_device *dev)
1415{
Ben Skeggsf20ce962011-07-08 13:17:01 +10001416 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001417 u32 intr = nv_rd32(dev, 0x610088);
1418
1419 if (intr & 0x00000002) {
1420 u32 stat = nv_rd32(dev, 0x61009c);
1421 int chid = ffs(stat) - 1;
1422 if (chid >= 0) {
1423 u32 mthd = nv_rd32(dev, 0x6101f0 + (chid * 12));
1424 u32 data = nv_rd32(dev, 0x6101f4 + (chid * 12));
1425 u32 unkn = nv_rd32(dev, 0x6101f8 + (chid * 12));
1426
1427 NV_INFO(dev, "EvoCh: chid %d mthd 0x%04x data 0x%08x "
1428 "0x%08x 0x%08x\n",
1429 chid, (mthd & 0x0000ffc), data, mthd, unkn);
1430 nv_wr32(dev, 0x61009c, (1 << chid));
1431 nv_wr32(dev, 0x6101f0 + (chid * 12), 0x90000000);
1432 }
1433
1434 intr &= ~0x00000002;
1435 }
1436
Ben Skeggs270a5742011-07-05 14:16:05 +10001437 if (intr & 0x00100000) {
1438 u32 stat = nv_rd32(dev, 0x6100ac);
1439
1440 if (stat & 0x00000007) {
Ben Skeggsee417792011-07-08 14:34:45 +10001441 disp->modeset = stat;
Ben Skeggsf20ce962011-07-08 13:17:01 +10001442 tasklet_schedule(&disp->tasklet);
Ben Skeggs270a5742011-07-05 14:16:05 +10001443
Ben Skeggsf20ce962011-07-08 13:17:01 +10001444 nv_wr32(dev, 0x6100ac, (stat & 0x00000007));
Ben Skeggs270a5742011-07-05 14:16:05 +10001445 stat &= ~0x00000007;
1446 }
1447
1448 if (stat) {
1449 NV_INFO(dev, "PDISP: unknown intr24 0x%08x\n", stat);
1450 nv_wr32(dev, 0x6100ac, stat);
1451 }
1452
1453 intr &= ~0x00100000;
1454 }
1455
Ben Skeggs46005222011-07-05 11:01:13 +10001456 if (intr & 0x01000000) {
1457 u32 stat = nv_rd32(dev, 0x6100bc);
1458 nv_wr32(dev, 0x6100bc, stat);
1459 intr &= ~0x01000000;
1460 }
1461
1462 if (intr & 0x02000000) {
1463 u32 stat = nv_rd32(dev, 0x6108bc);
1464 nv_wr32(dev, 0x6108bc, stat);
1465 intr &= ~0x02000000;
1466 }
1467
1468 if (intr)
1469 NV_INFO(dev, "PDISP: unknown intr 0x%08x\n", intr);
1470}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001471
1472/******************************************************************************
1473 * Init
1474 *****************************************************************************/
Ben Skeggs2a44e492011-11-09 11:36:33 +10001475void
Ben Skeggs26f6d882011-07-04 16:25:18 +10001476nvd0_display_fini(struct drm_device *dev)
1477{
1478 int i;
1479
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001480 /* fini cursors + syncs */
1481 for (i = 1; i >= 0; i--) {
1482 evo_fini_pio(dev, EVO_CURS(i));
1483 evo_fini_dma(dev, EVO_SYNC(i));
Ben Skeggs26f6d882011-07-04 16:25:18 +10001484 }
1485
1486 /* fini master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001487 evo_fini_dma(dev, EVO_MASTER);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001488}
1489
1490int
1491nvd0_display_init(struct drm_device *dev)
1492{
1493 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001494 int ret, i;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001495 u32 *push;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001496
1497 if (nv_rd32(dev, 0x6100ac) & 0x00000100) {
1498 nv_wr32(dev, 0x6100ac, 0x00000100);
1499 nv_mask(dev, 0x6194e8, 0x00000001, 0x00000000);
1500 if (!nv_wait(dev, 0x6194e8, 0x00000002, 0x00000000)) {
1501 NV_ERROR(dev, "PDISP: 0x6194e8 0x%08x\n",
1502 nv_rd32(dev, 0x6194e8));
1503 return -EBUSY;
1504 }
1505 }
1506
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001507 /* nfi what these are exactly, i do know that SOR_MODE_CTRL won't
1508 * work at all unless you do the SOR part below.
1509 */
1510 for (i = 0; i < 3; i++) {
1511 u32 dac = nv_rd32(dev, 0x61a000 + (i * 0x800));
1512 nv_wr32(dev, 0x6101c0 + (i * 0x800), dac);
1513 }
1514
1515 for (i = 0; i < 4; i++) {
1516 u32 sor = nv_rd32(dev, 0x61c000 + (i * 0x800));
1517 nv_wr32(dev, 0x6301c4 + (i * 0x800), sor);
1518 }
1519
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001520 for (i = 0; i < dev->mode_config.num_crtc; i++) {
Ben Skeggsa36f04c2011-07-06 14:39:23 +10001521 u32 crtc0 = nv_rd32(dev, 0x616104 + (i * 0x800));
1522 u32 crtc1 = nv_rd32(dev, 0x616108 + (i * 0x800));
1523 u32 crtc2 = nv_rd32(dev, 0x61610c + (i * 0x800));
1524 nv_wr32(dev, 0x6101b4 + (i * 0x800), crtc0);
1525 nv_wr32(dev, 0x6101b8 + (i * 0x800), crtc1);
1526 nv_wr32(dev, 0x6101bc + (i * 0x800), crtc2);
1527 }
1528
1529 /* point at our hash table / objects, enable interrupts */
Ben Skeggs26f6d882011-07-04 16:25:18 +10001530 nv_wr32(dev, 0x610010, (disp->mem->vinst >> 8) | 9);
Ben Skeggs270a5742011-07-05 14:16:05 +10001531 nv_mask(dev, 0x6100b0, 0x00000307, 0x00000307);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001532
1533 /* init master */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001534 ret = evo_init_dma(dev, EVO_MASTER);
1535 if (ret)
1536 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001537
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001538 /* init syncs + cursors */
1539 for (i = 0; i < dev->mode_config.num_crtc; i++) {
1540 if ((ret = evo_init_dma(dev, EVO_SYNC(i))) ||
1541 (ret = evo_init_pio(dev, EVO_CURS(i))))
1542 goto error;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001543 }
1544
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001545 push = evo_wait(dev, EVO_MASTER, 32);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001546 if (!push) {
1547 ret = -EBUSY;
1548 goto error;
1549 }
Ben Skeggsefd272a2011-07-05 11:58:58 +10001550 evo_mthd(push, 0x0088, 1);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001551 evo_data(push, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001552 evo_mthd(push, 0x0084, 1);
1553 evo_data(push, 0x00000000);
1554 evo_mthd(push, 0x0084, 1);
1555 evo_data(push, 0x80000000);
1556 evo_mthd(push, 0x008c, 1);
1557 evo_data(push, 0x00000000);
Ben Skeggs2eac77b2011-11-12 12:53:36 +10001558 evo_kick(push, dev, EVO_MASTER);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001559
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001560error:
1561 if (ret)
1562 nvd0_display_fini(dev);
1563 return ret;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001564}
1565
1566void
1567nvd0_display_destroy(struct drm_device *dev)
1568{
1569 struct drm_nouveau_private *dev_priv = dev->dev_private;
1570 struct nvd0_display *disp = nvd0_display(dev);
Ben Skeggs51beb422011-07-05 10:33:08 +10001571 struct pci_dev *pdev = dev->pdev;
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001572 int i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001573
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001574 for (i = 0; i < 3; i++) {
1575 pci_free_consistent(pdev, PAGE_SIZE, disp->evo[i].ptr,
1576 disp->evo[i].handle);
1577 }
1578
Ben Skeggs26f6d882011-07-04 16:25:18 +10001579 nouveau_gpuobj_ref(NULL, &disp->mem);
Ben Skeggs46005222011-07-05 11:01:13 +10001580 nouveau_irq_unregister(dev, 26);
Ben Skeggs51beb422011-07-05 10:33:08 +10001581
1582 dev_priv->engine.display.priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001583 kfree(disp);
1584}
1585
1586int
1587nvd0_display_create(struct drm_device *dev)
1588{
1589 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsefd272a2011-07-05 11:58:58 +10001590 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001591 struct dcb_table *dcb = &dev_priv->vbios.dcb;
1592 struct drm_connector *connector, *tmp;
Ben Skeggs51beb422011-07-05 10:33:08 +10001593 struct pci_dev *pdev = dev->pdev;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001594 struct nvd0_display *disp;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001595 struct dcb_entry *dcbe;
1596 int ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10001597
1598 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
1599 if (!disp)
1600 return -ENOMEM;
1601 dev_priv->engine.display.priv = disp;
1602
Ben Skeggs438d99e2011-07-05 16:48:06 +10001603 /* create crtc objects to represent the hw heads */
1604 for (i = 0; i < 2; i++) {
1605 ret = nvd0_crtc_create(dev, i);
1606 if (ret)
1607 goto out;
1608 }
1609
Ben Skeggs83fc0832011-07-05 13:08:40 +10001610 /* create encoder/connector objects based on VBIOS DCB table */
1611 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
1612 connector = nouveau_connector_create(dev, dcbe->connector);
1613 if (IS_ERR(connector))
1614 continue;
1615
1616 if (dcbe->location != DCB_LOC_ON_CHIP) {
1617 NV_WARN(dev, "skipping off-chip encoder %d/%d\n",
1618 dcbe->type, ffs(dcbe->or) - 1);
1619 continue;
1620 }
1621
1622 switch (dcbe->type) {
1623 case OUTPUT_TMDS:
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001624 case OUTPUT_LVDS:
Ben Skeggs83fc0832011-07-05 13:08:40 +10001625 nvd0_sor_create(connector, dcbe);
1626 break;
Ben Skeggs8eaa9662011-07-06 15:25:47 +10001627 case OUTPUT_ANALOG:
1628 nvd0_dac_create(connector, dcbe);
1629 break;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001630 default:
1631 NV_WARN(dev, "skipping unsupported encoder %d/%d\n",
1632 dcbe->type, ffs(dcbe->or) - 1);
1633 continue;
1634 }
1635 }
1636
1637 /* cull any connectors we created that don't have an encoder */
1638 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
1639 if (connector->encoder_ids[0])
1640 continue;
1641
1642 NV_WARN(dev, "%s has no encoders, removing\n",
1643 drm_get_connector_name(connector));
1644 connector->funcs->destroy(connector);
1645 }
1646
Ben Skeggs46005222011-07-05 11:01:13 +10001647 /* setup interrupt handling */
Ben Skeggsf20ce962011-07-08 13:17:01 +10001648 tasklet_init(&disp->tasklet, nvd0_display_bh, (unsigned long)dev);
Ben Skeggs46005222011-07-05 11:01:13 +10001649 nouveau_irq_register(dev, 26, nvd0_display_intr);
1650
Ben Skeggs51beb422011-07-05 10:33:08 +10001651 /* hash table and dma objects for the memory areas we care about */
Ben Skeggsefd272a2011-07-05 11:58:58 +10001652 ret = nouveau_gpuobj_new(dev, NULL, 0x4000, 0x10000,
1653 NVOBJ_FLAG_ZERO_ALLOC, &disp->mem);
Ben Skeggs26f6d882011-07-04 16:25:18 +10001654 if (ret)
1655 goto out;
1656
Ben Skeggsefd272a2011-07-05 11:58:58 +10001657 nv_wo32(disp->mem, 0x1000, 0x00000049);
1658 nv_wo32(disp->mem, 0x1004, (disp->mem->vinst + 0x2000) >> 8);
1659 nv_wo32(disp->mem, 0x1008, (disp->mem->vinst + 0x2fff) >> 8);
1660 nv_wo32(disp->mem, 0x100c, 0x00000000);
1661 nv_wo32(disp->mem, 0x1010, 0x00000000);
1662 nv_wo32(disp->mem, 0x1014, 0x00000000);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001663 nv_wo32(disp->mem, 0x0000, NvEvoSync);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001664 nv_wo32(disp->mem, 0x0004, (0x1000 << 9) | 0x00000001);
1665
Ben Skeggsc0cc92a2011-07-06 11:40:45 +10001666 nv_wo32(disp->mem, 0x1020, 0x00000049);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001667 nv_wo32(disp->mem, 0x1024, 0x00000000);
1668 nv_wo32(disp->mem, 0x1028, (dev_priv->vram_size - 1) >> 8);
1669 nv_wo32(disp->mem, 0x102c, 0x00000000);
1670 nv_wo32(disp->mem, 0x1030, 0x00000000);
1671 nv_wo32(disp->mem, 0x1034, 0x00000000);
Ben Skeggs37b034a2011-07-08 14:43:19 +10001672 nv_wo32(disp->mem, 0x0008, NvEvoVRAM);
Ben Skeggsefd272a2011-07-05 11:58:58 +10001673 nv_wo32(disp->mem, 0x000c, (0x1020 << 9) | 0x00000001);
1674
Ben Skeggsc0cc92a2011-07-06 11:40:45 +10001675 nv_wo32(disp->mem, 0x1040, 0x00000009);
1676 nv_wo32(disp->mem, 0x1044, 0x00000000);
1677 nv_wo32(disp->mem, 0x1048, (dev_priv->vram_size - 1) >> 8);
1678 nv_wo32(disp->mem, 0x104c, 0x00000000);
1679 nv_wo32(disp->mem, 0x1050, 0x00000000);
1680 nv_wo32(disp->mem, 0x1054, 0x00000000);
1681 nv_wo32(disp->mem, 0x0010, NvEvoVRAM_LP);
1682 nv_wo32(disp->mem, 0x0014, (0x1040 << 9) | 0x00000001);
1683
1684 nv_wo32(disp->mem, 0x1060, 0x0fe00009);
1685 nv_wo32(disp->mem, 0x1064, 0x00000000);
1686 nv_wo32(disp->mem, 0x1068, (dev_priv->vram_size - 1) >> 8);
1687 nv_wo32(disp->mem, 0x106c, 0x00000000);
1688 nv_wo32(disp->mem, 0x1070, 0x00000000);
1689 nv_wo32(disp->mem, 0x1074, 0x00000000);
1690 nv_wo32(disp->mem, 0x0018, NvEvoFB32);
1691 nv_wo32(disp->mem, 0x001c, (0x1060 << 9) | 0x00000001);
1692
Ben Skeggsefd272a2011-07-05 11:58:58 +10001693 pinstmem->flush(dev);
1694
Ben Skeggs51beb422011-07-05 10:33:08 +10001695 /* push buffers for evo channels */
Ben Skeggsbdb8c212011-11-12 01:30:24 +10001696 for (i = 0; i < 3; i++) {
1697 disp->evo[i].ptr = pci_alloc_consistent(pdev, PAGE_SIZE,
1698 &disp->evo[i].handle);
1699 if (!disp->evo[i].ptr) {
1700 ret = -ENOMEM;
1701 goto out;
1702 }
Ben Skeggs51beb422011-07-05 10:33:08 +10001703 }
1704
Ben Skeggs26f6d882011-07-04 16:25:18 +10001705out:
1706 if (ret)
1707 nvd0_display_destroy(dev);
1708 return ret;
1709}