nv50_display.c 26.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2008 Maarten Maathuis.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

27 28 29
#include "nouveau_drm.h"
#include "nouveau_dma.h"

30 31 32 33
#include "nv50_display.h"
#include "nouveau_crtc.h"
#include "nouveau_encoder.h"
#include "nouveau_connector.h"
34
#include "nouveau_fbcon.h"
35
#include <drm/drm_crtc_helper.h>
36
#include "nouveau_fence.h"
37

38 39
#include <core/gpuobj.h>
#include <subdev/timer.h>
40

41
static void nv50_display_bh(unsigned long);
42

43 44 45
static inline int
nv50_sor_nr(struct drm_device *dev)
{
46
	struct nouveau_device *device = nouveau_dev(dev);
47

48 49 50
	if (device->chipset  < 0x90 ||
	    device->chipset == 0x92 ||
	    device->chipset == 0xa0)
51 52 53 54 55
		return 2;

	return 4;
}

56 57 58
u32
nv50_display_active_crtcs(struct drm_device *dev)
{
59
	struct nouveau_device *device = nouveau_dev(dev);
60 61 62
	u32 mask = 0;
	int i;

63 64 65
	if (device->chipset  < 0x90 ||
	    device->chipset == 0x92 ||
	    device->chipset == 0xa0) {
66
		for (i = 0; i < 2; i++)
67
			mask |= nv_rd32(device, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
68 69
	} else {
		for (i = 0; i < 4; i++)
70
			mask |= nv_rd32(device, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
71 72 73
	}

	for (i = 0; i < 3; i++)
74
		mask |= nv_rd32(device, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
75 76 77 78

	return mask & 3;
}

79 80 81 82 83 84 85 86 87 88 89
int
nv50_display_early_init(struct drm_device *dev)
{
	return 0;
}

void
nv50_display_late_takedown(struct drm_device *dev)
{
}

90 91 92 93 94 95 96 97 98
int
nv50_display_sync(struct drm_device *dev)
{
	struct nv50_display *disp = nv50_display(dev);
	struct nouveau_channel *evo = disp->master;
	int ret;

	ret = RING_SPACE(evo, 6);
	if (ret == 0) {
99
		BEGIN_NV04(evo, 0, 0x0084, 1);
100
		OUT_RING  (evo, 0x80000000);
101
		BEGIN_NV04(evo, 0, 0x0080, 1);
102
		OUT_RING  (evo, 0);
103
		BEGIN_NV04(evo, 0, 0x0084, 1);
104 105
		OUT_RING  (evo, 0x00000000);

106
		nv_wo32(disp->ramin, 0x2000, 0x00000000);
107 108
		FIRE_RING (evo);

109 110
		if (nv_wait_ne(disp->ramin, 0x2000, 0xffffffff, 0x00000000))
			return 0;
111 112
	}

113
	return 0;
114 115
}

116 117 118
int
nv50_display_init(struct drm_device *dev)
{
119 120
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_device *device = nouveau_dev(dev);
121
	struct nouveau_channel *evo;
122
	int ret, i;
123
	u32 val;
124

125
	nv_wr32(device, 0x00610184, nv_rd32(device, 0x00614004));
126

127 128 129 130 131 132
	/*
	 * I think the 0x006101XX range is some kind of main control area
	 * that enables things.
	 */
	/* CRTC? */
	for (i = 0; i < 2; i++) {
133 134 135 136 137 138 139 140
		val = nv_rd32(device, 0x00616100 + (i * 0x800));
		nv_wr32(device, 0x00610190 + (i * 0x10), val);
		val = nv_rd32(device, 0x00616104 + (i * 0x800));
		nv_wr32(device, 0x00610194 + (i * 0x10), val);
		val = nv_rd32(device, 0x00616108 + (i * 0x800));
		nv_wr32(device, 0x00610198 + (i * 0x10), val);
		val = nv_rd32(device, 0x0061610c + (i * 0x800));
		nv_wr32(device, 0x0061019c + (i * 0x10), val);
141
	}
142

143 144
	/* DAC */
	for (i = 0; i < 3; i++) {
145 146
		val = nv_rd32(device, 0x0061a000 + (i * 0x800));
		nv_wr32(device, 0x006101d0 + (i * 0x04), val);
147
	}
148

149
	/* SOR */
150
	for (i = 0; i < nv50_sor_nr(dev); i++) {
151 152
		val = nv_rd32(device, 0x0061c000 + (i * 0x800));
		nv_wr32(device, 0x006101e0 + (i * 0x04), val);
153
	}
154

155
	/* EXT */
156
	for (i = 0; i < 3; i++) {
157 158
		val = nv_rd32(device, 0x0061e000 + (i * 0x800));
		nv_wr32(device, 0x006101f0 + (i * 0x04), val);
159 160 161
	}

	for (i = 0; i < 3; i++) {
162
		nv_wr32(device, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
163
			NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
164
		nv_wr32(device, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
165 166 167 168 169
	}

	/* The precise purpose is unknown, i suspect it has something to do
	 * with text mode.
	 */
170 171 172 173 174 175 176
	if (nv_rd32(device, NV50_PDISPLAY_INTR_1) & 0x100) {
		nv_wr32(device, NV50_PDISPLAY_INTR_1, 0x100);
		nv_wr32(device, 0x006194e8, nv_rd32(device, 0x006194e8) & ~1);
		if (!nv_wait(device, 0x006194e8, 2, 0)) {
			NV_ERROR(drm, "timeout: (0x6194e8 & 2) != 0\n");
			NV_ERROR(drm, "0x6194e8 = 0x%08x\n",
						nv_rd32(device, 0x6194e8));
177 178 179 180
			return -EBUSY;
		}
	}

181
	for (i = 0; i < 2; i++) {
182 183
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
184
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
185 186 187
			NV_ERROR(drm, "timeout: CURSOR_CTRL2_STATUS == 0\n");
			NV_ERROR(drm, "CURSOR_CTRL2 = 0x%08x\n",
				 nv_rd32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
188 189 190
			return -EBUSY;
		}

191
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
192
			NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
193
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
194 195
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
196
			NV_ERROR(drm, "timeout: "
197
				      "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
198 199
			NV_ERROR(drm, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
				 nv_rd32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
200 201 202 203
			return -EBUSY;
		}
	}

204 205 206 207 208
	nv_wr32(device, NV50_PDISPLAY_PIO_CTRL, 0x00000000);
	nv_mask(device, NV50_PDISPLAY_INTR_0, 0x00000000, 0x00000000);
	nv_wr32(device, NV50_PDISPLAY_INTR_EN_0, 0x00000000);
	nv_mask(device, NV50_PDISPLAY_INTR_1, 0x00000000, 0x00000000);
	nv_wr32(device, NV50_PDISPLAY_INTR_EN_1,
209 210 211
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK10 |
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK20 |
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK40);
212

213
	ret = nv50_evo_init(dev);
214 215
	if (ret)
		return ret;
216
	evo = nv50_display(dev)->master;
217

218
	nv_wr32(device, NV50_PDISPLAY_OBJECTS, (nv50_display(dev)->ramin->addr >> 8) | 9);
219

220
	ret = RING_SPACE(evo, 3);
221 222
	if (ret)
		return ret;
223
	BEGIN_NV04(evo, 0, NV50_EVO_UNK84, 2);
224 225
	OUT_RING  (evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
	OUT_RING  (evo, NvEvoSync);
226

227
	return nv50_display_sync(dev);
228 229
}

230 231
void
nv50_display_fini(struct drm_device *dev)
232
{
233 234
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_device *device = nouveau_dev(dev);
235
	struct nv50_display *disp = nv50_display(dev);
236
	struct nouveau_channel *evo = disp->master;
237 238 239 240 241 242 243 244 245
	struct drm_crtc *drm_crtc;
	int ret, i;

	list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
		struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);

		nv50_crtc_blank(crtc, true);
	}

246
	ret = RING_SPACE(evo, 2);
247
	if (ret == 0) {
248
		BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
249
		OUT_RING(evo, 0);
250
	}
251
	FIRE_RING(evo);
252 253 254 255 256 257 258 259 260 261 262

	/* Almost like ack'ing a vblank interrupt, maybe in the spirit of
	 * cleaning up?
	 */
	list_for_each_entry(drm_crtc, &dev->mode_config.crtc_list, head) {
		struct nouveau_crtc *crtc = nouveau_crtc(drm_crtc);
		uint32_t mask = NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(crtc->index);

		if (!crtc->base.enabled)
			continue;

263 264 265
		nv_wr32(device, NV50_PDISPLAY_INTR_1, mask);
		if (!nv_wait(device, NV50_PDISPLAY_INTR_1, mask, mask)) {
			NV_ERROR(drm, "timeout: (0x610024 & 0x%08x) == "
266
				      "0x%08x\n", mask, mask);
267 268
			NV_ERROR(drm, "0x610024 = 0x%08x\n",
				 nv_rd32(device, NV50_PDISPLAY_INTR_1));
269 270 271
		}
	}

272
	for (i = 0; i < 2; i++) {
273 274
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0);
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
275
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
276 277 278
			NV_ERROR(drm, "timeout: CURSOR_CTRL2_STATUS == 0\n");
			NV_ERROR(drm, "CURSOR_CTRL2 = 0x%08x\n",
				 nv_rd32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
279 280 281
		}
	}

282
	nv50_evo_fini(dev);
283 284

	for (i = 0; i < 3; i++) {
285
		if (!nv_wait(device, NV50_PDISPLAY_SOR_DPMS_STATE(i),
286
			     NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
287 288 289
			NV_ERROR(drm, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
			NV_ERROR(drm, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
				  nv_rd32(device, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
290 291 292 293
		}
	}

	/* disable interrupts. */
294
	nv_wr32(device, NV50_PDISPLAY_INTR_EN_1, 0x00000000);
295 296
}

297 298
int
nv50_display_create(struct drm_device *dev)
299
{
300 301
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct dcb_table *dcb = &drm->vbios.dcb;
302
	struct drm_connector *connector, *ct;
303
	struct nv50_display *priv;
304
	int ret, i;
305

306 307 308
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
309 310 311 312 313

	nouveau_display(dev)->priv = priv;
	nouveau_display(dev)->dtor = nv50_display_destroy;
	nouveau_display(dev)->init = nv50_display_init;
	nouveau_display(dev)->fini = nv50_display_fini;
314

315
	/* Create CRTC objects */
316 317 318 319 320
	for (i = 0; i < 2; i++) {
		ret = nv50_crtc_create(dev, i);
		if (ret)
			return ret;
	}
321 322 323

	/* We setup the encoders from the BIOS table */
	for (i = 0 ; i < dcb->entries; i++) {
324
		struct dcb_output *entry = &dcb->entry[i];
325 326

		if (entry->location != DCB_LOC_ON_CHIP) {
327
			NV_WARN(drm, "Off-chip encoder %d/%d unsupported\n",
328 329 330 331
				entry->type, ffs(entry->or) - 1);
			continue;
		}

332 333 334 335
		connector = nouveau_connector_create(dev, entry->connector);
		if (IS_ERR(connector))
			continue;

336
		switch (entry->type) {
337 338 339
		case DCB_OUTPUT_TMDS:
		case DCB_OUTPUT_LVDS:
		case DCB_OUTPUT_DP:
340
			nv50_sor_create(connector, entry);
341
			break;
342
		case DCB_OUTPUT_ANALOG:
343
			nv50_dac_create(connector, entry);
344 345
			break;
		default:
346
			NV_WARN(drm, "DCB encoder %d unknown\n", entry->type);
347 348 349 350
			continue;
		}
	}

351 352 353
	list_for_each_entry_safe(connector, ct,
				 &dev->mode_config.connector_list, head) {
		if (!connector->encoder_ids[0]) {
354
			NV_WARN(drm, "%s has no encoders, removing\n",
355 356 357
				drm_get_connector_name(connector));
			connector->funcs->destroy(connector);
		}
358 359
	}

360
	tasklet_init(&priv->tasklet, nv50_display_bh, (unsigned long)dev);
361 362 363 364 365 366 367

	ret = nv50_evo_create(dev);
	if (ret) {
		nv50_display_destroy(dev);
		return ret;
	}

368 369 370
	return 0;
}

371 372
void
nv50_display_destroy(struct drm_device *dev)
373
{
374
	struct nv50_display *disp = nv50_display(dev);
375

376
	nv50_evo_destroy(dev);
377
	kfree(disp);
378 379
}

380 381 382 383 384 385
struct nouveau_bo *
nv50_display_crtc_sema(struct drm_device *dev, int crtc)
{
	return nv50_display(dev)->crtc[crtc].sem.bo;
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
void
nv50_display_flip_stop(struct drm_crtc *crtc)
{
	struct nv50_display *disp = nv50_display(crtc->dev);
	struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
	struct nv50_display_crtc *dispc = &disp->crtc[nv_crtc->index];
	struct nouveau_channel *evo = dispc->sync;
	int ret;

	ret = RING_SPACE(evo, 8);
	if (ret) {
		WARN_ON(1);
		return;
	}

401
	BEGIN_NV04(evo, 0, 0x0084, 1);
402
	OUT_RING  (evo, 0x00000000);
403
	BEGIN_NV04(evo, 0, 0x0094, 1);
404
	OUT_RING  (evo, 0x00000000);
405
	BEGIN_NV04(evo, 0, 0x00c0, 1);
406
	OUT_RING  (evo, 0x00000000);
407
	BEGIN_NV04(evo, 0, 0x0080, 1);
408 409 410 411 412 413 414 415
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);
}

int
nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
		       struct nouveau_channel *chan)
{
416
	struct nouveau_drm *drm = nouveau_drm(crtc->dev);
417 418 419 420 421 422 423
	struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
	struct nv50_display *disp = nv50_display(crtc->dev);
	struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
	struct nv50_display_crtc *dispc = &disp->crtc[nv_crtc->index];
	struct nouveau_channel *evo = dispc->sync;
	int ret;

424
	ret = RING_SPACE(evo, chan ? 25 : 27);
425 426 427 428 429 430 431 432 433 434 435
	if (unlikely(ret))
		return ret;

	/* synchronise with the rendering channel, if necessary */
	if (likely(chan)) {
		ret = RING_SPACE(chan, 10);
		if (ret) {
			WIND_RING(evo);
			return ret;
		}

436
		if (nv_device(drm->device)->chipset < 0xc0) {
437
			BEGIN_NV04(chan, 0, 0x0060, 2);
438 439
			OUT_RING  (chan, NvEvoSema0 + nv_crtc->index);
			OUT_RING  (chan, dispc->sem.offset);
440
			BEGIN_NV04(chan, 0, 0x006c, 1);
441
			OUT_RING  (chan, 0xf00d0000 | dispc->sem.value);
442
			BEGIN_NV04(chan, 0, 0x0064, 2);
443 444
			OUT_RING  (chan, dispc->sem.offset ^ 0x10);
			OUT_RING  (chan, 0x74b1e000);
445
			BEGIN_NV04(chan, 0, 0x0060, 1);
446
			if (nv_device(drm->device)->chipset < 0x84)
447 448
				OUT_RING  (chan, NvSema);
			else
449
				OUT_RING  (chan, chan->vram);
450
		} else {
451
			u64 offset = nvc0_fence_crtc(chan, nv_crtc->index);
452
			offset += dispc->sem.offset;
453
			BEGIN_NVC0(chan, 0, 0x0010, 4);
454 455 456 457
			OUT_RING  (chan, upper_32_bits(offset));
			OUT_RING  (chan, lower_32_bits(offset));
			OUT_RING  (chan, 0xf00d0000 | dispc->sem.value);
			OUT_RING  (chan, 0x1002);
458
			BEGIN_NVC0(chan, 0, 0x0010, 4);
459 460 461 462 463 464 465 466 467 468 469 470
			OUT_RING  (chan, upper_32_bits(offset));
			OUT_RING  (chan, lower_32_bits(offset ^ 0x10));
			OUT_RING  (chan, 0x74b1e000);
			OUT_RING  (chan, 0x1001);
		}
		FIRE_RING (chan);
	} else {
		nouveau_bo_wr32(dispc->sem.bo, dispc->sem.offset / 4,
				0xf00d0000 | dispc->sem.value);
	}

	/* queue the flip on the crtc's "display sync" channel */
471
	BEGIN_NV04(evo, 0, 0x0100, 1);
472
	OUT_RING  (evo, 0xfffe0000);
473
	if (chan) {
474
		BEGIN_NV04(evo, 0, 0x0084, 1);
475 476
		OUT_RING  (evo, 0x00000100);
	} else {
477
		BEGIN_NV04(evo, 0, 0x0084, 1);
478 479 480 481
		OUT_RING  (evo, 0x00000010);
		/* allows gamma somehow, PDISP will bitch at you if
		 * you don't wait for vblank before changing this..
		 */
482
		BEGIN_NV04(evo, 0, 0x00e0, 1);
483 484
		OUT_RING  (evo, 0x40000000);
	}
485
	BEGIN_NV04(evo, 0, 0x0088, 4);
486 487 488 489
	OUT_RING  (evo, dispc->sem.offset);
	OUT_RING  (evo, 0xf00d0000 | dispc->sem.value);
	OUT_RING  (evo, 0x74b1e000);
	OUT_RING  (evo, NvEvoSync);
490
	BEGIN_NV04(evo, 0, 0x00a0, 2);
491 492
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
493
	BEGIN_NV04(evo, 0, 0x00c0, 1);
494
	OUT_RING  (evo, nv_fb->r_dma);
495
	BEGIN_NV04(evo, 0, 0x0110, 2);
496 497
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
498
	BEGIN_NV04(evo, 0, 0x0800, 5);
499
	OUT_RING  (evo, nv_fb->nvbo->bo.offset >> 8);
500 501 502 503
	OUT_RING  (evo, 0);
	OUT_RING  (evo, (fb->height << 16) | fb->width);
	OUT_RING  (evo, nv_fb->r_pitch);
	OUT_RING  (evo, nv_fb->r_format);
504
	BEGIN_NV04(evo, 0, 0x0080, 1);
505 506 507 508 509 510 511 512
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);

	dispc->sem.offset ^= 0x10;
	dispc->sem.value++;
	return 0;
}

513
static u16
514
nv50_display_script_select(struct drm_device *dev, struct dcb_output *dcb,
515
			   u32 mc, int pxclk)
516
{
517
	struct nouveau_drm *drm = nouveau_drm(dev);
518 519
	struct nouveau_connector *nv_connector = NULL;
	struct drm_encoder *encoder;
520
	struct nvbios *bios = &drm->vbios;
521
	u32 script = 0, or;
522

523 524 525
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);

526
		if (nv_encoder->dcb != dcb)
527 528 529 530 531 532
			continue;

		nv_connector = nouveau_encoder_connector_get(nv_encoder);
		break;
	}

533 534
	or = ffs(dcb->or) - 1;
	switch (dcb->type) {
535
	case DCB_OUTPUT_LVDS:
536
		script = (mc >> 8) & 0xf;
537
		if (bios->fp_no_ddc) {
538 539 540 541 542
			if (bios->fp.dual_link)
				script |= 0x0100;
			if (bios->fp.if_is_24bit)
				script |= 0x0200;
		} else {
543 544
			/* determine number of lvds links */
			if (nv_connector && nv_connector->edid &&
545
			    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
546 547 548 549
				/* http://www.spwg.org */
				if (((u8 *)nv_connector->edid)[121] == 2)
					script |= 0x0100;
			} else
550 551
			if (pxclk >= bios->fp.duallink_transition_clk) {
				script |= 0x0100;
552 553 554 555
			}

			/* determine panel depth */
			if (script & 0x0100) {
556 557
				if (bios->fp.strapless_is_24bit & 2)
					script |= 0x0200;
558 559 560 561
			} else {
				if (bios->fp.strapless_is_24bit & 1)
					script |= 0x0200;
			}
562 563 564 565 566

			if (nv_connector && nv_connector->edid &&
			    (nv_connector->edid->revision >= 4) &&
			    (nv_connector->edid->input & 0x70) >= 0x20)
				script |= 0x0200;
567 568
		}
		break;
569
	case DCB_OUTPUT_TMDS:
570 571 572 573
		script = (mc >> 8) & 0xf;
		if (pxclk >= 165000)
			script |= 0x0100;
		break;
574
	case DCB_OUTPUT_DP:
575 576
		script = (mc >> 8) & 0xf;
		break;
577
	case DCB_OUTPUT_ANALOG:
578 579 580
		script = 0xff;
		break;
	default:
581
		NV_ERROR(drm, "modeset on unsupported output type!\n");
582 583 584 585 586 587 588 589 590
		break;
	}

	return script;
}

static void
nv50_display_unk10_handler(struct drm_device *dev)
{
591 592
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
593
	struct nv50_display *disp = nv50_display(dev);
594
	u32 unk30 = nv_rd32(device, 0x610030), mc;
595
	int i, crtc, or = 0, type = DCB_OUTPUT_ANY;
596

597
	NV_DEBUG(drm, "0x610030: 0x%08x\n", unk30);
598
	disp->irq.dcb = NULL;
599

600
	nv_wr32(device, 0x619494, nv_rd32(device, 0x619494) & ~8);
601

602 603 604 605 606 607 608 609 610 611 612
	/* Determine which CRTC we're dealing with, only 1 ever will be
	 * signalled at the same time with the current nouveau code.
	 */
	crtc = ffs((unk30 & 0x00000060) >> 5) - 1;
	if (crtc < 0)
		goto ack;

	/* Nothing needs to be done for the encoder */
	crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
	if (crtc < 0)
		goto ack;
613

614
	/* Find which encoder was connected to the CRTC */
615
	for (i = 0; type == DCB_OUTPUT_ANY && i < 3; i++) {
616 617
		mc = nv_rd32(device, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
		NV_DEBUG(drm, "DAC-%d mc: 0x%08x\n", i, mc);
618 619 620 621
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
622 623
		case 0: type = DCB_OUTPUT_ANALOG; break;
		case 1: type = DCB_OUTPUT_TV; break;
624
		default:
625
			NV_ERROR(drm, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
626 627 628 629 630 631
			goto ack;
		}

		or = i;
	}

632
	for (i = 0; type == DCB_OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
633 634 635 636
		if (nv_device(drm->device)->chipset  < 0x90 ||
		    nv_device(drm->device)->chipset == 0x92 ||
		    nv_device(drm->device)->chipset == 0xa0)
			mc = nv_rd32(device, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
637
		else
638
			mc = nv_rd32(device, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
639

640
		NV_DEBUG(drm, "SOR-%d mc: 0x%08x\n", i, mc);
641 642 643 644
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
645 646 647 648 649 650
		case 0: type = DCB_OUTPUT_LVDS; break;
		case 1: type = DCB_OUTPUT_TMDS; break;
		case 2: type = DCB_OUTPUT_TMDS; break;
		case 5: type = DCB_OUTPUT_TMDS; break;
		case 8: type = DCB_OUTPUT_DP; break;
		case 9: type = DCB_OUTPUT_DP; break;
651
		default:
652
			NV_ERROR(drm, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
653 654 655 656 657 658 659
			goto ack;
		}

		or = i;
	}

	/* There was no encoder to disable */
660
	if (type == DCB_OUTPUT_ANY)
661 662 663
		goto ack;

	/* Disable the encoder */
664 665
	for (i = 0; i < drm->vbios.dcb.entries; i++) {
		struct dcb_output *dcb = &drm->vbios.dcb.entry[i];
666 667

		if (dcb->type == type && (dcb->or & (1 << or))) {
668
			nouveau_bios_run_display_table(dev, 0, -1, dcb, -1);
669
			disp->irq.dcb = dcb;
670 671 672 673
			goto ack;
		}
	}

674
	NV_ERROR(drm, "no dcb for %d %d 0x%08x\n", or, type, mc);
675
ack:
676 677
	nv_wr32(device, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
	nv_wr32(device, 0x610030, 0x80000000);
678 679 680 681 682
}

static void
nv50_display_unk20_handler(struct drm_device *dev)
{
683 684
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
685
	struct nv50_display *disp = nv50_display(dev);
686
	u32 unk30 = nv_rd32(device, 0x610030), tmp, pclk, script, mc = 0;
687 688
	struct dcb_output *dcb;
	int i, crtc, or = 0, type = DCB_OUTPUT_ANY;
689

690
	NV_DEBUG(drm, "0x610030: 0x%08x\n", unk30);
691
	dcb = disp->irq.dcb;
692
	if (dcb) {
693
		nouveau_bios_run_display_table(dev, 0, -2, dcb, -1);
694
		disp->irq.dcb = NULL;
695 696 697 698 699
	}

	/* CRTC clock change requested? */
	crtc = ffs((unk30 & 0x00000600) >> 9) - 1;
	if (crtc >= 0) {
700
		pclk  = nv_rd32(device, NV50_PDISPLAY_CRTC_P(crtc, CLOCK));
701
		pclk &= 0x003fffff;
702 703
		if (pclk)
			nv50_crtc_set_clock(dev, crtc, pclk);
704

705
		tmp = nv_rd32(device, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc));
706
		tmp &= ~0x000000f;
707
		nv_wr32(device, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc), tmp);
708 709 710 711 712
	}

	/* Nothing needs to be done for the encoder */
	crtc = ffs((unk30 & 0x00000180) >> 7) - 1;
	if (crtc < 0)
713
		goto ack;
714
	pclk  = nv_rd32(device, NV50_PDISPLAY_CRTC_P(crtc, CLOCK)) & 0x003fffff;
715

716
	/* Find which encoder is connected to the CRTC */
717
	for (i = 0; type == DCB_OUTPUT_ANY && i < 3; i++) {
718 719
		mc = nv_rd32(device, NV50_PDISPLAY_DAC_MODE_CTRL_P(i));
		NV_DEBUG(drm, "DAC-%d mc: 0x%08x\n", i, mc);
720 721
		if (!(mc & (1 << crtc)))
			continue;
722

723
		switch ((mc & 0x00000f00) >> 8) {
724 725
		case 0: type = DCB_OUTPUT_ANALOG; break;
		case 1: type = DCB_OUTPUT_TV; break;
726
		default:
727
			NV_ERROR(drm, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
728 729 730 731 732 733
			goto ack;
		}

		or = i;
	}

734
	for (i = 0; type == DCB_OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
735 736 737 738
		if (nv_device(drm->device)->chipset  < 0x90 ||
		    nv_device(drm->device)->chipset == 0x92 ||
		    nv_device(drm->device)->chipset == 0xa0)
			mc = nv_rd32(device, NV50_PDISPLAY_SOR_MODE_CTRL_P(i));
739
		else
740
			mc = nv_rd32(device, NV90_PDISPLAY_SOR_MODE_CTRL_P(i));
741

742
		NV_DEBUG(drm, "SOR-%d mc: 0x%08x\n", i, mc);
743 744 745 746
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
747 748 749 750 751 752
		case 0: type = DCB_OUTPUT_LVDS; break;
		case 1: type = DCB_OUTPUT_TMDS; break;
		case 2: type = DCB_OUTPUT_TMDS; break;
		case 5: type = DCB_OUTPUT_TMDS; break;
		case 8: type = DCB_OUTPUT_DP; break;
		case 9: type = DCB_OUTPUT_DP; break;
753
		default:
754
			NV_ERROR(drm, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
755 756
			goto ack;
		}
757

758 759
		or = i;
	}
760

761
	if (type == DCB_OUTPUT_ANY)
762
		goto ack;
763

764
	/* Enable the encoder */
765 766
	for (i = 0; i < drm->vbios.dcb.entries; i++) {
		dcb = &drm->vbios.dcb.entry[i];
767 768 769
		if (dcb->type == type && (dcb->or & (1 << or)))
			break;
	}
770

771 772
	if (i == drm->vbios.dcb.entries) {
		NV_ERROR(drm, "no dcb for %d %d 0x%08x\n", or, type, mc);
773 774 775 776
		goto ack;
	}

	script = nv50_display_script_select(dev, dcb, mc, pclk);
777
	nouveau_bios_run_display_table(dev, script, pclk, dcb, -1);
778

779
	if (type == DCB_OUTPUT_DP) {
780 781
		int link = !(dcb->dpconf.sor.link & 1);
		if ((mc & 0x000f0000) == 0x00020000)
782
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 18);
783
		else
784
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 24);
785
	}
786

787
	if (dcb->type != DCB_OUTPUT_ANALOG) {
788
		tmp = nv_rd32(device, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
789 790 791
		tmp &= ~0x00000f0f;
		if (script & 0x0100)
			tmp |= 0x00000101;
792
		nv_wr32(device, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
793
	} else {
794
		nv_wr32(device, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
795 796
	}

797 798 799
	disp->irq.dcb = dcb;
	disp->irq.pclk = pclk;
	disp->irq.script = script;
800

801
ack:
802 803
	nv_wr32(device, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
	nv_wr32(device, 0x610030, 0x80000000);
804 805
}

806 807 808 809 810 811 812 813 814
/* If programming a TMDS output on a SOR that can also be configured for
 * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
 *
 * It looks like the VBIOS TMDS scripts make an attempt at this, however,
 * the VBIOS scripts on at least one board I have only switch it off on
 * link 0, causing a blank display if the output has previously been
 * programmed for DisplayPort.
 */
static void
815
nv50_display_unk40_dp_set_tmds(struct drm_device *dev, struct dcb_output *dcb)
816
{
817
	struct nouveau_device *device = nouveau_dev(dev);
818 819 820 821
	int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
	struct drm_encoder *encoder;
	u32 tmp;

822
	if (dcb->type != DCB_OUTPUT_TMDS)
823 824 825 826 827
		return;

	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);

828
		if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
829
		    nv_encoder->dcb->or & (1 << or)) {
830
			tmp  = nv_rd32(device, NV50_SOR_DP_CTRL(or, link));
831
			tmp &= ~NV50_SOR_DP_CTRL_ENABLED;
832
			nv_wr32(device, NV50_SOR_DP_CTRL(or, link), tmp);
833 834 835 836 837
			break;
		}
	}
}

838 839 840
static void
nv50_display_unk40_handler(struct drm_device *dev)
{
841 842
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
843
	struct nv50_display *disp = nv50_display(dev);
844
	struct dcb_output *dcb = disp->irq.dcb;
845
	u16 script = disp->irq.script;
846
	u32 unk30 = nv_rd32(device, 0x610030), pclk = disp->irq.pclk;
847

848
	NV_DEBUG(drm, "0x610030: 0x%08x\n", unk30);
849
	disp->irq.dcb = NULL;
850
	if (!dcb)
851 852
		goto ack;

853
	nouveau_bios_run_display_table(dev, script, -pclk, dcb, -1);
854 855
	nv50_display_unk40_dp_set_tmds(dev, dcb);

856
ack:
857 858 859
	nv_wr32(device, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
	nv_wr32(device, 0x610030, 0x80000000);
	nv_wr32(device, 0x619494, nv_rd32(device, 0x619494) | 8);
860 861
}

862 863
static void
nv50_display_bh(unsigned long data)
864
{
865
	struct drm_device *dev = (struct drm_device *)data;
866 867
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
868 869

	for (;;) {
870 871
		uint32_t intr0 = nv_rd32(device, NV50_PDISPLAY_INTR_0);
		uint32_t intr1 = nv_rd32(device, NV50_PDISPLAY_INTR_1);
872

873
		NV_DEBUG(drm, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
874 875 876 877 878 879 880 881 882 883 884 885 886

		if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK10)
			nv50_display_unk10_handler(dev);
		else
		if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK20)
			nv50_display_unk20_handler(dev);
		else
		if (intr1 & NV50_PDISPLAY_INTR_1_CLK_UNK40)
			nv50_display_unk40_handler(dev);
		else
			break;
	}

887
	nv_wr32(device, NV03_PMC_INTR_EN_0, 1);
888 889 890 891 892
}

static void
nv50_display_error_handler(struct drm_device *dev)
{
893 894 895
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
	u32 channels = (nv_rd32(device, NV50_PDISPLAY_INTR_0) & 0x001f0000) >> 16;
896 897
	u32 addr, data;
	int chid;
898

899 900 901
	for (chid = 0; chid < 5; chid++) {
		if (!(channels & (1 << chid)))
			continue;
902

903 904 905 906
		nv_wr32(device, NV50_PDISPLAY_INTR_0, 0x00010000 << chid);
		addr = nv_rd32(device, NV50_PDISPLAY_TRAPPED_ADDR(chid));
		data = nv_rd32(device, NV50_PDISPLAY_TRAPPED_DATA(chid));
		NV_ERROR(drm, "EvoCh %d Mthd 0x%04x Data 0x%08x "
907 908
			      "(0x%04x 0x%02x)\n", chid,
			 addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
909

910
		nv_wr32(device, NV50_PDISPLAY_TRAPPED_ADDR(chid), 0x90000000);
911
	}
912 913
}

914 915
void
nv50_display_intr(struct drm_device *dev)
916
{
917 918
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
919
	struct nv50_display *disp = nv50_display(dev);
920 921
	uint32_t delayed = 0;

922 923 924
	while (nv_rd32(device, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
		uint32_t intr0 = nv_rd32(device, NV50_PDISPLAY_INTR_0);
		uint32_t intr1 = nv_rd32(device, NV50_PDISPLAY_INTR_1);
925 926
		uint32_t clock;

927
		NV_DEBUG(drm, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
928 929 930 931

		if (!intr0 && !(intr1 & ~delayed))
			break;

932
		if (intr0 & 0x001f0000) {
933
			nv50_display_error_handler(dev);
934
			intr0 &= ~0x001f0000;
935 936 937 938
		}

		if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
			intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
939
			delayed |= NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
940 941 942 943 944 945
		}

		clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK20 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK40));
		if (clock) {
946
			nv_wr32(device, NV03_PMC_INTR_EN_0, 0);
947
			tasklet_schedule(&disp->tasklet);
948 949 950 951 952
			delayed |= clock;
			intr1 &= ~clock;
		}

		if (intr0) {
953 954
			NV_ERROR(drm, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
			nv_wr32(device, NV50_PDISPLAY_INTR_0, intr0);
955 956 957
		}

		if (intr1) {
958
			NV_ERROR(drm,
959
				 "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
960
			nv_wr32(device, NV50_PDISPLAY_INTR_1, intr1);
961 962 963
		}
	}
}