nv50_display.c 24.4 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
#include <core/gpuobj.h>
39 40
#include <core/class.h>

41
#include <subdev/timer.h>
42

43
static void nv50_display_bh(unsigned long);
44

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

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

	return 4;
}

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

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

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

	return mask & 3;
}

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

void
nv50_display_late_takedown(struct drm_device *dev)
{
}

92 93 94 95 96 97 98 99 100
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) {
101
		BEGIN_NV04(evo, 0, 0x0084, 1);
102
		OUT_RING  (evo, 0x80000000);
103
		BEGIN_NV04(evo, 0, 0x0080, 1);
104
		OUT_RING  (evo, 0);
105
		BEGIN_NV04(evo, 0, 0x0084, 1);
106 107
		OUT_RING  (evo, 0x00000000);

108
		nv_wo32(disp->ramin, 0x2000, 0x00000000);
109 110
		FIRE_RING (evo);

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

115
	return 0;
116 117
}

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

	for (i = 0; i < 3; i++) {
127
		nv_wr32(device, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
128
			NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
129
		nv_wr32(device, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
130 131
	}

132
	for (i = 0; i < 2; i++) {
133 134
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
135
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
136 137 138
			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)));
139 140 141
			return -EBUSY;
		}

142
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
143
			NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
144
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
145 146
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
147
			NV_ERROR(drm, "timeout: "
148
				      "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
149 150
			NV_ERROR(drm, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
				 nv_rd32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
151 152 153 154
			return -EBUSY;
		}
	}

155
	ret = nv50_evo_init(dev);
156 157
	if (ret)
		return ret;
158
	evo = nv50_display(dev)->master;
159

160
	ret = RING_SPACE(evo, 3);
161 162
	if (ret)
		return ret;
163
	BEGIN_NV04(evo, 0, NV50_EVO_UNK84, 2);
164 165
	OUT_RING  (evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
	OUT_RING  (evo, NvEvoSync);
166

167
	return nv50_display_sync(dev);
168 169
}

170 171
void
nv50_display_fini(struct drm_device *dev)
172
{
173 174
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_device *device = nouveau_dev(dev);
175
	struct nv50_display *disp = nv50_display(dev);
176
	struct nouveau_channel *evo = disp->master;
177 178 179 180 181 182 183 184 185
	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);
	}

186
	ret = RING_SPACE(evo, 2);
187
	if (ret == 0) {
188
		BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
189
		OUT_RING(evo, 0);
190
	}
191
	FIRE_RING(evo);
192 193 194 195 196 197 198 199 200 201 202

	/* 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;

203 204 205
		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) == "
206
				      "0x%08x\n", mask, mask);
207 208
			NV_ERROR(drm, "0x610024 = 0x%08x\n",
				 nv_rd32(device, NV50_PDISPLAY_INTR_1));
209 210 211
		}
	}

212
	for (i = 0; i < 2; i++) {
213 214
		nv_wr32(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0);
		if (!nv_wait(device, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
215
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
216 217 218
			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)));
219 220 221
		}
	}

222
	nv50_evo_fini(dev);
223 224

	for (i = 0; i < 3; i++) {
225
		if (!nv_wait(device, NV50_PDISPLAY_SOR_DPMS_STATE(i),
226
			     NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
227 228 229
			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)));
230 231 232 233
		}
	}
}

234 235
int
nv50_display_create(struct drm_device *dev)
236
{
237 238 239 240 241 242 243
	static const u16 oclass[] = {
		NVA3_DISP_CLASS,
		NV94_DISP_CLASS,
		NVA0_DISP_CLASS,
		NV84_DISP_CLASS,
		NV50_DISP_CLASS,
	};
244 245
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct dcb_table *dcb = &drm->vbios.dcb;
246
	struct drm_connector *connector, *ct;
247
	struct nv50_display *priv;
248
	int ret, i;
249

250 251 252
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
253 254 255 256 257

	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;
258

259 260 261 262 263 264 265 266 267 268 269
	/* attempt to allocate a supported evo display class */
	ret = -ENODEV;
	for (i = 0; ret && i < ARRAY_SIZE(oclass); i++) {
		ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
					 0xd1500000, oclass[i], NULL, 0,
					 &priv->core);
	}

	if (ret)
		return ret;

270
	/* Create CRTC objects */
271 272 273 274 275
	for (i = 0; i < 2; i++) {
		ret = nv50_crtc_create(dev, i);
		if (ret)
			return ret;
	}
276 277 278

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

		if (entry->location != DCB_LOC_ON_CHIP) {
282
			NV_WARN(drm, "Off-chip encoder %d/%d unsupported\n",
283 284 285 286
				entry->type, ffs(entry->or) - 1);
			continue;
		}

287 288 289 290
		connector = nouveau_connector_create(dev, entry->connector);
		if (IS_ERR(connector))
			continue;

291
		switch (entry->type) {
292 293 294
		case DCB_OUTPUT_TMDS:
		case DCB_OUTPUT_LVDS:
		case DCB_OUTPUT_DP:
295
			nv50_sor_create(connector, entry);
296
			break;
297
		case DCB_OUTPUT_ANALOG:
298
			nv50_dac_create(connector, entry);
299 300
			break;
		default:
301
			NV_WARN(drm, "DCB encoder %d unknown\n", entry->type);
302 303 304 305
			continue;
		}
	}

306 307 308
	list_for_each_entry_safe(connector, ct,
				 &dev->mode_config.connector_list, head) {
		if (!connector->encoder_ids[0]) {
309
			NV_WARN(drm, "%s has no encoders, removing\n",
310 311 312
				drm_get_connector_name(connector));
			connector->funcs->destroy(connector);
		}
313 314
	}

315
	tasklet_init(&priv->tasklet, nv50_display_bh, (unsigned long)dev);
316 317 318 319 320 321 322

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

323 324 325
	return 0;
}

326 327
void
nv50_display_destroy(struct drm_device *dev)
328
{
329
	struct nv50_display *disp = nv50_display(dev);
330

331
	nv50_evo_destroy(dev);
332
	kfree(disp);
333 334
}

335 336 337 338 339 340
struct nouveau_bo *
nv50_display_crtc_sema(struct drm_device *dev, int crtc)
{
	return nv50_display(dev)->crtc[crtc].sem.bo;
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
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;
	}

356
	BEGIN_NV04(evo, 0, 0x0084, 1);
357
	OUT_RING  (evo, 0x00000000);
358
	BEGIN_NV04(evo, 0, 0x0094, 1);
359
	OUT_RING  (evo, 0x00000000);
360
	BEGIN_NV04(evo, 0, 0x00c0, 1);
361
	OUT_RING  (evo, 0x00000000);
362
	BEGIN_NV04(evo, 0, 0x0080, 1);
363 364 365 366 367 368 369 370
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);
}

int
nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
		       struct nouveau_channel *chan)
{
371
	struct nouveau_drm *drm = nouveau_drm(crtc->dev);
372 373 374 375 376 377 378
	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;

379
	ret = RING_SPACE(evo, chan ? 25 : 27);
380 381 382 383 384 385 386 387 388 389 390
	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;
		}

391
		if (nv_device(drm->device)->chipset < 0xc0) {
392
			BEGIN_NV04(chan, 0, 0x0060, 2);
393 394
			OUT_RING  (chan, NvEvoSema0 + nv_crtc->index);
			OUT_RING  (chan, dispc->sem.offset);
395
			BEGIN_NV04(chan, 0, 0x006c, 1);
396
			OUT_RING  (chan, 0xf00d0000 | dispc->sem.value);
397
			BEGIN_NV04(chan, 0, 0x0064, 2);
398 399
			OUT_RING  (chan, dispc->sem.offset ^ 0x10);
			OUT_RING  (chan, 0x74b1e000);
400
			BEGIN_NV04(chan, 0, 0x0060, 1);
401
			if (nv_device(drm->device)->chipset < 0x84)
402 403
				OUT_RING  (chan, NvSema);
			else
404
				OUT_RING  (chan, chan->vram);
405
		} else {
406
			u64 offset = nvc0_fence_crtc(chan, nv_crtc->index);
407
			offset += dispc->sem.offset;
408
			BEGIN_NVC0(chan, 0, 0x0010, 4);
409 410 411 412
			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);
413
			BEGIN_NVC0(chan, 0, 0x0010, 4);
414 415 416 417 418 419 420 421 422 423 424 425
			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 */
426
	BEGIN_NV04(evo, 0, 0x0100, 1);
427
	OUT_RING  (evo, 0xfffe0000);
428
	if (chan) {
429
		BEGIN_NV04(evo, 0, 0x0084, 1);
430 431
		OUT_RING  (evo, 0x00000100);
	} else {
432
		BEGIN_NV04(evo, 0, 0x0084, 1);
433 434 435 436
		OUT_RING  (evo, 0x00000010);
		/* allows gamma somehow, PDISP will bitch at you if
		 * you don't wait for vblank before changing this..
		 */
437
		BEGIN_NV04(evo, 0, 0x00e0, 1);
438 439
		OUT_RING  (evo, 0x40000000);
	}
440
	BEGIN_NV04(evo, 0, 0x0088, 4);
441 442 443 444
	OUT_RING  (evo, dispc->sem.offset);
	OUT_RING  (evo, 0xf00d0000 | dispc->sem.value);
	OUT_RING  (evo, 0x74b1e000);
	OUT_RING  (evo, NvEvoSync);
445
	BEGIN_NV04(evo, 0, 0x00a0, 2);
446 447
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
448
	BEGIN_NV04(evo, 0, 0x00c0, 1);
449
	OUT_RING  (evo, nv_fb->r_dma);
450
	BEGIN_NV04(evo, 0, 0x0110, 2);
451 452
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
453
	BEGIN_NV04(evo, 0, 0x0800, 5);
454
	OUT_RING  (evo, nv_fb->nvbo->bo.offset >> 8);
455 456 457 458
	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);
459
	BEGIN_NV04(evo, 0, 0x0080, 1);
460 461 462 463 464 465 466 467
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);

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

468
static u16
469
nv50_display_script_select(struct drm_device *dev, struct dcb_output *dcb,
470
			   u32 mc, int pxclk)
471
{
472
	struct nouveau_drm *drm = nouveau_drm(dev);
473 474
	struct nouveau_connector *nv_connector = NULL;
	struct drm_encoder *encoder;
475
	struct nvbios *bios = &drm->vbios;
476
	u32 script = 0, or;
477

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

481
		if (nv_encoder->dcb != dcb)
482 483 484 485 486 487
			continue;

		nv_connector = nouveau_encoder_connector_get(nv_encoder);
		break;
	}

488 489
	or = ffs(dcb->or) - 1;
	switch (dcb->type) {
490
	case DCB_OUTPUT_LVDS:
491
		script = (mc >> 8) & 0xf;
492
		if (bios->fp_no_ddc) {
493 494 495 496 497
			if (bios->fp.dual_link)
				script |= 0x0100;
			if (bios->fp.if_is_24bit)
				script |= 0x0200;
		} else {
498 499
			/* determine number of lvds links */
			if (nv_connector && nv_connector->edid &&
500
			    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
501 502 503 504
				/* http://www.spwg.org */
				if (((u8 *)nv_connector->edid)[121] == 2)
					script |= 0x0100;
			} else
505 506
			if (pxclk >= bios->fp.duallink_transition_clk) {
				script |= 0x0100;
507 508 509 510
			}

			/* determine panel depth */
			if (script & 0x0100) {
511 512
				if (bios->fp.strapless_is_24bit & 2)
					script |= 0x0200;
513 514 515 516
			} else {
				if (bios->fp.strapless_is_24bit & 1)
					script |= 0x0200;
			}
517 518 519 520 521

			if (nv_connector && nv_connector->edid &&
			    (nv_connector->edid->revision >= 4) &&
			    (nv_connector->edid->input & 0x70) >= 0x20)
				script |= 0x0200;
522 523
		}
		break;
524
	case DCB_OUTPUT_TMDS:
525 526 527 528
		script = (mc >> 8) & 0xf;
		if (pxclk >= 165000)
			script |= 0x0100;
		break;
529
	case DCB_OUTPUT_DP:
530 531
		script = (mc >> 8) & 0xf;
		break;
532
	case DCB_OUTPUT_ANALOG:
533 534 535
		script = 0xff;
		break;
	default:
536
		NV_ERROR(drm, "modeset on unsupported output type!\n");
537 538 539 540 541 542 543 544 545
		break;
	}

	return script;
}

static void
nv50_display_unk10_handler(struct drm_device *dev)
{
546 547
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
548
	struct nv50_display *disp = nv50_display(dev);
549
	u32 unk30 = nv_rd32(device, 0x610030), mc;
550
	int i, crtc, or = 0, type = DCB_OUTPUT_ANY;
551

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

555
	nv_wr32(device, 0x619494, nv_rd32(device, 0x619494) & ~8);
556

557 558 559 560 561 562 563 564 565 566 567
	/* 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;
568

569
	/* Find which encoder was connected to the CRTC */
570
	for (i = 0; type == DCB_OUTPUT_ANY && i < 3; i++) {
571 572
		mc = nv_rd32(device, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
		NV_DEBUG(drm, "DAC-%d mc: 0x%08x\n", i, mc);
573 574 575 576
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
577 578
		case 0: type = DCB_OUTPUT_ANALOG; break;
		case 1: type = DCB_OUTPUT_TV; break;
579
		default:
580
			NV_ERROR(drm, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
581 582 583 584 585 586
			goto ack;
		}

		or = i;
	}

587
	for (i = 0; type == DCB_OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
588 589 590 591
		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));
592
		else
593
			mc = nv_rd32(device, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
594

595
		NV_DEBUG(drm, "SOR-%d mc: 0x%08x\n", i, mc);
596 597 598 599
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
600 601 602 603 604 605
		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;
606
		default:
607
			NV_ERROR(drm, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
608 609 610 611 612 613 614
			goto ack;
		}

		or = i;
	}

	/* There was no encoder to disable */
615
	if (type == DCB_OUTPUT_ANY)
616 617 618
		goto ack;

	/* Disable the encoder */
619 620
	for (i = 0; i < drm->vbios.dcb.entries; i++) {
		struct dcb_output *dcb = &drm->vbios.dcb.entry[i];
621 622

		if (dcb->type == type && (dcb->or & (1 << or))) {
623
			nouveau_bios_run_display_table(dev, 0, -1, dcb, -1);
624
			disp->irq.dcb = dcb;
625 626 627 628
			goto ack;
		}
	}

629
	NV_ERROR(drm, "no dcb for %d %d 0x%08x\n", or, type, mc);
630
ack:
631 632
	nv_wr32(device, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
	nv_wr32(device, 0x610030, 0x80000000);
633 634 635 636 637
}

static void
nv50_display_unk20_handler(struct drm_device *dev)
{
638 639
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
640
	struct nv50_display *disp = nv50_display(dev);
641
	u32 unk30 = nv_rd32(device, 0x610030), tmp, pclk, script, mc = 0;
642 643
	struct dcb_output *dcb;
	int i, crtc, or = 0, type = DCB_OUTPUT_ANY;
644

645
	NV_DEBUG(drm, "0x610030: 0x%08x\n", unk30);
646
	dcb = disp->irq.dcb;
647
	if (dcb) {
648
		nouveau_bios_run_display_table(dev, 0, -2, dcb, -1);
649
		disp->irq.dcb = NULL;
650 651 652 653 654
	}

	/* CRTC clock change requested? */
	crtc = ffs((unk30 & 0x00000600) >> 9) - 1;
	if (crtc >= 0) {
655
		pclk  = nv_rd32(device, NV50_PDISPLAY_CRTC_P(crtc, CLOCK));
656
		pclk &= 0x003fffff;
657 658
		if (pclk)
			nv50_crtc_set_clock(dev, crtc, pclk);
659

660
		tmp = nv_rd32(device, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc));
661
		tmp &= ~0x000000f;
662
		nv_wr32(device, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc), tmp);
663 664 665 666 667
	}

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

671
	/* Find which encoder is connected to the CRTC */
672
	for (i = 0; type == DCB_OUTPUT_ANY && i < 3; i++) {
673 674
		mc = nv_rd32(device, NV50_PDISPLAY_DAC_MODE_CTRL_P(i));
		NV_DEBUG(drm, "DAC-%d mc: 0x%08x\n", i, mc);
675 676
		if (!(mc & (1 << crtc)))
			continue;
677

678
		switch ((mc & 0x00000f00) >> 8) {
679 680
		case 0: type = DCB_OUTPUT_ANALOG; break;
		case 1: type = DCB_OUTPUT_TV; break;
681
		default:
682
			NV_ERROR(drm, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
683 684 685 686 687 688
			goto ack;
		}

		or = i;
	}

689
	for (i = 0; type == DCB_OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
690 691 692 693
		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));
694
		else
695
			mc = nv_rd32(device, NV90_PDISPLAY_SOR_MODE_CTRL_P(i));
696

697
		NV_DEBUG(drm, "SOR-%d mc: 0x%08x\n", i, mc);
698 699 700 701
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
702 703 704 705 706 707
		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;
708
		default:
709
			NV_ERROR(drm, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
710 711
			goto ack;
		}
712

713 714
		or = i;
	}
715

716
	if (type == DCB_OUTPUT_ANY)
717
		goto ack;
718

719
	/* Enable the encoder */
720 721
	for (i = 0; i < drm->vbios.dcb.entries; i++) {
		dcb = &drm->vbios.dcb.entry[i];
722 723 724
		if (dcb->type == type && (dcb->or & (1 << or)))
			break;
	}
725

726 727
	if (i == drm->vbios.dcb.entries) {
		NV_ERROR(drm, "no dcb for %d %d 0x%08x\n", or, type, mc);
728 729 730 731
		goto ack;
	}

	script = nv50_display_script_select(dev, dcb, mc, pclk);
732
	nouveau_bios_run_display_table(dev, script, pclk, dcb, -1);
733

734
	if (type == DCB_OUTPUT_DP) {
735 736
		int link = !(dcb->dpconf.sor.link & 1);
		if ((mc & 0x000f0000) == 0x00020000)
737
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 18);
738
		else
739
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 24);
740
	}
741

742
	if (dcb->type != DCB_OUTPUT_ANALOG) {
743
		tmp = nv_rd32(device, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
744 745 746
		tmp &= ~0x00000f0f;
		if (script & 0x0100)
			tmp |= 0x00000101;
747
		nv_wr32(device, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
748
	} else {
749
		nv_wr32(device, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
750 751
	}

752 753 754
	disp->irq.dcb = dcb;
	disp->irq.pclk = pclk;
	disp->irq.script = script;
755

756
ack:
757 758
	nv_wr32(device, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
	nv_wr32(device, 0x610030, 0x80000000);
759 760
}

761 762 763 764 765 766 767 768 769
/* 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
770
nv50_display_unk40_dp_set_tmds(struct drm_device *dev, struct dcb_output *dcb)
771
{
772
	struct nouveau_device *device = nouveau_dev(dev);
773 774 775 776
	int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
	struct drm_encoder *encoder;
	u32 tmp;

777
	if (dcb->type != DCB_OUTPUT_TMDS)
778 779 780 781 782
		return;

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

783
		if (nv_encoder->dcb->type == DCB_OUTPUT_DP &&
784
		    nv_encoder->dcb->or & (1 << or)) {
785
			tmp  = nv_rd32(device, NV50_SOR_DP_CTRL(or, link));
786
			tmp &= ~NV50_SOR_DP_CTRL_ENABLED;
787
			nv_wr32(device, NV50_SOR_DP_CTRL(or, link), tmp);
788 789 790 791 792
			break;
		}
	}
}

793 794 795
static void
nv50_display_unk40_handler(struct drm_device *dev)
{
796 797
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
798
	struct nv50_display *disp = nv50_display(dev);
799
	struct dcb_output *dcb = disp->irq.dcb;
800
	u16 script = disp->irq.script;
801
	u32 unk30 = nv_rd32(device, 0x610030), pclk = disp->irq.pclk;
802

803
	NV_DEBUG(drm, "0x610030: 0x%08x\n", unk30);
804
	disp->irq.dcb = NULL;
805
	if (!dcb)
806 807
		goto ack;

808
	nouveau_bios_run_display_table(dev, script, -pclk, dcb, -1);
809 810
	nv50_display_unk40_dp_set_tmds(dev, dcb);

811
ack:
812 813 814
	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);
815 816
}

817 818
static void
nv50_display_bh(unsigned long data)
819
{
820
	struct drm_device *dev = (struct drm_device *)data;
821 822
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
823 824

	for (;;) {
825 826
		uint32_t intr0 = nv_rd32(device, NV50_PDISPLAY_INTR_0);
		uint32_t intr1 = nv_rd32(device, NV50_PDISPLAY_INTR_1);
827

828
		NV_DEBUG(drm, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
829 830 831 832 833 834 835 836 837 838 839 840 841

		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;
	}

842
	nv_wr32(device, NV03_PMC_INTR_EN_0, 1);
843 844 845 846 847
}

static void
nv50_display_error_handler(struct drm_device *dev)
{
848 849 850
	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;
851 852
	u32 addr, data;
	int chid;
853

854 855 856
	for (chid = 0; chid < 5; chid++) {
		if (!(channels & (1 << chid)))
			continue;
857

858 859 860 861
		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 "
862 863
			      "(0x%04x 0x%02x)\n", chid,
			 addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
864

865
		nv_wr32(device, NV50_PDISPLAY_TRAPPED_ADDR(chid), 0x90000000);
866
	}
867 868
}

869 870
void
nv50_display_intr(struct drm_device *dev)
871
{
872 873
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
874
	struct nv50_display *disp = nv50_display(dev);
875 876
	uint32_t delayed = 0;

877 878 879
	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);
880 881
		uint32_t clock;

882
		NV_DEBUG(drm, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
883 884 885 886

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

887
		if (intr0 & 0x001f0000) {
888
			nv50_display_error_handler(dev);
889
			intr0 &= ~0x001f0000;
890 891 892 893
		}

		if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
			intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
894
			delayed |= NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
895 896 897 898 899 900
		}

		clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK20 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK40));
		if (clock) {
901
			nv_wr32(device, NV03_PMC_INTR_EN_0, 0);
902
			tasklet_schedule(&disp->tasklet);
903 904 905 906 907
			delayed |= clock;
			intr1 &= ~clock;
		}

		if (intr0) {
908 909
			NV_ERROR(drm, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
			nv_wr32(device, NV50_PDISPLAY_INTR_0, intr0);
910 911 912
		}

		if (intr1) {
913
			NV_ERROR(drm,
914
				 "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
915
			nv_wr32(device, NV50_PDISPLAY_INTR_1, intr1);
916 917 918
		}
	}
}