nv50_display.c 27.5 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
#define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
28 29 30 31 32
#include "nv50_display.h"
#include "nouveau_crtc.h"
#include "nouveau_encoder.h"
#include "nouveau_connector.h"
#include "nouveau_fb.h"
33
#include "nouveau_fbcon.h"
34
#include "nouveau_ramht.h"
35 36
#include "drm_crtc_helper.h"

37
static void nv50_display_isr(struct drm_device *);
38
static void nv50_display_bh(unsigned long);
39

40 41 42 43 44 45 46 47 48 49 50 51 52
static inline int
nv50_sor_nr(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;

	if (dev_priv->chipset  < 0x90 ||
	    dev_priv->chipset == 0x92 ||
	    dev_priv->chipset == 0xa0)
		return 2;

	return 4;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
u32
nv50_display_active_crtcs(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	u32 mask = 0;
	int i;

	if (dev_priv->chipset  < 0x90 ||
	    dev_priv->chipset == 0x92 ||
	    dev_priv->chipset == 0xa0) {
		for (i = 0; i < 2; i++)
			mask |= nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
	} else {
		for (i = 0; i < 4; i++)
			mask |= nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));
	}

	for (i = 0; i < 3; i++)
		mask |= nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));

	return mask & 3;
}

76 77 78 79 80 81 82 83 84
static int
evo_icmd(struct drm_device *dev, int ch, u32 mthd, u32 data)
{
	int ret = 0;
	nv_mask(dev, 0x610300 + (ch * 0x08), 0x00000001, 0x00000001);
	nv_wr32(dev, 0x610304 + (ch * 0x08), data);
	nv_wr32(dev, 0x610300 + (ch * 0x08), 0x80000001 | mthd);
	if (!nv_wait(dev, 0x610300 + (ch * 0x08), 0x80000000, 0x00000000))
		ret = -EBUSY;
85 86
	if (ret || (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO))
		NV_INFO(dev, "EvoPIO: %d 0x%04x 0x%08x\n", ch, mthd, data);
87 88 89 90
	nv_mask(dev, 0x610300 + (ch * 0x08), 0x00000001, 0x00000000);
	return ret;
}

91 92 93
int
nv50_display_early_init(struct drm_device *dev)
{
94
	u32 ctrl = nv_rd32(dev, 0x610200);
95
	int i;
96

97 98 99 100
	/* check if master evo channel is already active, a good a sign as any
	 * that the display engine is in a weird state (hibernate/kexec), if
	 * it is, do our best to reset the display engine...
	 */
101 102
	if ((ctrl & 0x00000003) == 0x00000003) {
		NV_INFO(dev, "PDISP: EVO(0) 0x%08x, resetting...\n", ctrl);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

		/* deactivate both heads first, PDISP will disappear forever
		 * (well, until you power cycle) on some boards as soon as
		 * PMC_ENABLE is hit unless they are..
		 */
		for (i = 0; i < 2; i++) {
			evo_icmd(dev, 0, 0x0880 + (i * 0x400), 0x05000000);
			evo_icmd(dev, 0, 0x089c + (i * 0x400), 0);
			evo_icmd(dev, 0, 0x0840 + (i * 0x400), 0);
			evo_icmd(dev, 0, 0x0844 + (i * 0x400), 0);
			evo_icmd(dev, 0, 0x085c + (i * 0x400), 0);
			evo_icmd(dev, 0, 0x0874 + (i * 0x400), 0);
		}
		evo_icmd(dev, 0, 0x0080, 0);

		/* reset PDISP */
		nv_mask(dev, 0x000200, 0x40000000, 0x00000000);
		nv_mask(dev, 0x000200, 0x40000000, 0x40000000);
	}
122

123 124 125 126 127 128 129 130
	return 0;
}

void
nv50_display_late_takedown(struct drm_device *dev)
{
}

131 132 133 134 135 136 137 138 139 140 141 142
int
nv50_display_sync(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
	struct nv50_display *disp = nv50_display(dev);
	struct nouveau_channel *evo = disp->master;
	u64 start;
	int ret;

	ret = RING_SPACE(evo, 6);
	if (ret == 0) {
143
		BEGIN_NV04(evo, 0, 0x0084, 1);
144
		OUT_RING  (evo, 0x80000000);
145
		BEGIN_NV04(evo, 0, 0x0080, 1);
146
		OUT_RING  (evo, 0);
147
		BEGIN_NV04(evo, 0, 0x0084, 1);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		OUT_RING  (evo, 0x00000000);

		nv_wo32(disp->ntfy, 0x000, 0x00000000);
		FIRE_RING (evo);

		start = ptimer->read(dev);
		do {
			if (nv_ro32(disp->ntfy, 0x000))
				return 0;
		} while (ptimer->read(dev) - start < 2000000000ULL);
	}

	return -EBUSY;
}

163 164 165
int
nv50_display_init(struct drm_device *dev)
{
166
	struct nouveau_channel *evo;
167
	int ret, i;
168
	u32 val;
169

170
	NV_DEBUG_KMS(dev, "\n");
171 172

	nv_wr32(dev, 0x00610184, nv_rd32(dev, 0x00614004));
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/*
	 * I think the 0x006101XX range is some kind of main control area
	 * that enables things.
	 */
	/* CRTC? */
	for (i = 0; i < 2; i++) {
		val = nv_rd32(dev, 0x00616100 + (i * 0x800));
		nv_wr32(dev, 0x00610190 + (i * 0x10), val);
		val = nv_rd32(dev, 0x00616104 + (i * 0x800));
		nv_wr32(dev, 0x00610194 + (i * 0x10), val);
		val = nv_rd32(dev, 0x00616108 + (i * 0x800));
		nv_wr32(dev, 0x00610198 + (i * 0x10), val);
		val = nv_rd32(dev, 0x0061610c + (i * 0x800));
		nv_wr32(dev, 0x0061019c + (i * 0x10), val);
	}
189

190 191 192 193 194
	/* DAC */
	for (i = 0; i < 3; i++) {
		val = nv_rd32(dev, 0x0061a000 + (i * 0x800));
		nv_wr32(dev, 0x006101d0 + (i * 0x04), val);
	}
195

196
	/* SOR */
197
	for (i = 0; i < nv50_sor_nr(dev); i++) {
198 199 200
		val = nv_rd32(dev, 0x0061c000 + (i * 0x800));
		nv_wr32(dev, 0x006101e0 + (i * 0x04), val);
	}
201

202
	/* EXT */
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	for (i = 0; i < 3; i++) {
		val = nv_rd32(dev, 0x0061e000 + (i * 0x800));
		nv_wr32(dev, 0x006101f0 + (i * 0x04), val);
	}

	for (i = 0; i < 3; i++) {
		nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(i), 0x00550000 |
			NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
		nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(i), 0x00000001);
	}

	/* The precise purpose is unknown, i suspect it has something to do
	 * with text mode.
	 */
	if (nv_rd32(dev, NV50_PDISPLAY_INTR_1) & 0x100) {
		nv_wr32(dev, NV50_PDISPLAY_INTR_1, 0x100);
		nv_wr32(dev, 0x006194e8, nv_rd32(dev, 0x006194e8) & ~1);
220
		if (!nv_wait(dev, 0x006194e8, 2, 0)) {
221 222 223 224 225 226 227
			NV_ERROR(dev, "timeout: (0x6194e8 & 2) != 0\n");
			NV_ERROR(dev, "0x6194e8 = 0x%08x\n",
						nv_rd32(dev, 0x6194e8));
			return -EBUSY;
		}
	}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	for (i = 0; i < 2; i++) {
		nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0x2000);
		if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
			NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
			NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
				 nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
			return -EBUSY;
		}

		nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
			NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_ON);
		if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS,
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS_ACTIVE)) {
			NV_ERROR(dev, "timeout: "
				      "CURSOR_CTRL2_STATUS_ACTIVE(%d)\n", i);
			NV_ERROR(dev, "CURSOR_CTRL2(%d) = 0x%08x\n", i,
				 nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
			return -EBUSY;
		}
	}

	nv_wr32(dev, NV50_PDISPLAY_PIO_CTRL, 0x00000000);
	nv_mask(dev, NV50_PDISPLAY_INTR_0, 0x00000000, 0x00000000);
253
	nv_wr32(dev, NV50_PDISPLAY_INTR_EN_0, 0x00000000);
254
	nv_mask(dev, NV50_PDISPLAY_INTR_1, 0x00000000, 0x00000000);
255 256 257 258
	nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1,
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK10 |
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK20 |
		     NV50_PDISPLAY_INTR_EN_1_CLK_UNK40);
259

260
	ret = nv50_evo_init(dev);
261 262
	if (ret)
		return ret;
263
	evo = nv50_display(dev)->master;
264

265
	nv_wr32(dev, NV50_PDISPLAY_OBJECTS, (evo->ramin->vinst >> 8) | 9);
266

267
	ret = RING_SPACE(evo, 3);
268 269
	if (ret)
		return ret;
270
	BEGIN_NV04(evo, 0, NV50_EVO_UNK84, 2);
271 272
	OUT_RING  (evo, NV50_EVO_UNK84_NOTIFY_DISABLED);
	OUT_RING  (evo, NvEvoSync);
273

274
	return nv50_display_sync(dev);
275 276
}

277 278
void
nv50_display_fini(struct drm_device *dev)
279
{
280
	struct nv50_display *disp = nv50_display(dev);
281
	struct nouveau_channel *evo = disp->master;
282 283 284
	struct drm_crtc *drm_crtc;
	int ret, i;

285
	NV_DEBUG_KMS(dev, "\n");
286 287 288 289 290 291 292

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

293
	ret = RING_SPACE(evo, 2);
294
	if (ret == 0) {
295
		BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
296
		OUT_RING(evo, 0);
297
	}
298
	FIRE_RING(evo);
299 300 301 302 303 304 305 306 307 308 309 310

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

		nv_wr32(dev, NV50_PDISPLAY_INTR_1, mask);
311
		if (!nv_wait(dev, NV50_PDISPLAY_INTR_1, mask, mask)) {
312 313 314 315 316 317 318
			NV_ERROR(dev, "timeout: (0x610024 & 0x%08x) == "
				      "0x%08x\n", mask, mask);
			NV_ERROR(dev, "0x610024 = 0x%08x\n",
				 nv_rd32(dev, NV50_PDISPLAY_INTR_1));
		}
	}

319 320 321 322 323 324 325 326 327 328
	for (i = 0; i < 2; i++) {
		nv_wr32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i), 0);
		if (!nv_wait(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i),
			     NV50_PDISPLAY_CURSOR_CURSOR_CTRL2_STATUS, 0)) {
			NV_ERROR(dev, "timeout: CURSOR_CTRL2_STATUS == 0\n");
			NV_ERROR(dev, "CURSOR_CTRL2 = 0x%08x\n",
				 nv_rd32(dev, NV50_PDISPLAY_CURSOR_CURSOR_CTRL2(i)));
		}
	}

329
	nv50_evo_fini(dev);
330 331

	for (i = 0; i < 3; i++) {
332
		if (!nv_wait(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i),
333 334 335 336 337 338 339 340
			     NV50_PDISPLAY_SOR_DPMS_STATE_WAIT, 0)) {
			NV_ERROR(dev, "timeout: SOR_DPMS_STATE_WAIT(%d) == 0\n", i);
			NV_ERROR(dev, "SOR_DPMS_STATE(%d) = 0x%08x\n", i,
				  nv_rd32(dev, NV50_PDISPLAY_SOR_DPMS_STATE(i)));
		}
	}

	/* disable interrupts. */
341
	nv_wr32(dev, NV50_PDISPLAY_INTR_EN_1, 0x00000000);
342 343
}

344 345
int
nv50_display_create(struct drm_device *dev)
346 347
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
348
	struct dcb_table *dcb = &dev_priv->vbios.dcb;
349
	struct drm_connector *connector, *ct;
350
	struct nv50_display *priv;
351
	int ret, i;
352

353
	NV_DEBUG_KMS(dev, "\n");
354

355 356 357 358 359
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	dev_priv->engine.display.priv = priv;

360
	/* Create CRTC objects */
361 362 363 364 365
	for (i = 0; i < 2; i++) {
		ret = nv50_crtc_create(dev, i);
		if (ret)
			return ret;
	}
366 367 368 369 370 371 372 373 374 375 376

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

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

377 378 379 380
		connector = nouveau_connector_create(dev, entry->connector);
		if (IS_ERR(connector))
			continue;

381 382 383 384
		switch (entry->type) {
		case OUTPUT_TMDS:
		case OUTPUT_LVDS:
		case OUTPUT_DP:
385
			nv50_sor_create(connector, entry);
386 387
			break;
		case OUTPUT_ANALOG:
388
			nv50_dac_create(connector, entry);
389 390 391 392 393 394 395
			break;
		default:
			NV_WARN(dev, "DCB encoder %d unknown\n", entry->type);
			continue;
		}
	}

396 397 398 399 400 401 402
	list_for_each_entry_safe(connector, ct,
				 &dev->mode_config.connector_list, head) {
		if (!connector->encoder_ids[0]) {
			NV_WARN(dev, "%s has no encoders, removing\n",
				drm_get_connector_name(connector));
			connector->funcs->destroy(connector);
		}
403 404
	}

405
	tasklet_init(&priv->tasklet, nv50_display_bh, (unsigned long)dev);
406
	nouveau_irq_register(dev, 26, nv50_display_isr);
407 408 409 410 411 412 413

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

414 415 416
	return 0;
}

417 418
void
nv50_display_destroy(struct drm_device *dev)
419
{
420
	struct nv50_display *disp = nv50_display(dev);
421

422
	NV_DEBUG_KMS(dev, "\n");
423

424
	nv50_evo_destroy(dev);
425
	nouveau_irq_unregister(dev, 26);
426
	kfree(disp);
427 428
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
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;
	}

444
	BEGIN_NV04(evo, 0, 0x0084, 1);
445
	OUT_RING  (evo, 0x00000000);
446
	BEGIN_NV04(evo, 0, 0x0094, 1);
447
	OUT_RING  (evo, 0x00000000);
448
	BEGIN_NV04(evo, 0, 0x00c0, 1);
449
	OUT_RING  (evo, 0x00000000);
450
	BEGIN_NV04(evo, 0, 0x0080, 1);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);
}

int
nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
		       struct nouveau_channel *chan)
{
	struct drm_nouveau_private *dev_priv = crtc->dev->dev_private;
	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;

467
	ret = RING_SPACE(evo, chan ? 25 : 27);
468 469 470 471 472 473 474 475 476 477 478 479
	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;
		}

		if (dev_priv->chipset < 0xc0) {
480
			BEGIN_NV04(chan, 0, 0x0060, 2);
481 482
			OUT_RING  (chan, NvEvoSema0 + nv_crtc->index);
			OUT_RING  (chan, dispc->sem.offset);
483
			BEGIN_NV04(chan, 0, 0x006c, 1);
484
			OUT_RING  (chan, 0xf00d0000 | dispc->sem.value);
485
			BEGIN_NV04(chan, 0, 0x0064, 2);
486 487
			OUT_RING  (chan, dispc->sem.offset ^ 0x10);
			OUT_RING  (chan, 0x74b1e000);
488
			BEGIN_NV04(chan, 0, 0x0060, 1);
489 490 491 492 493
			if (dev_priv->chipset < 0x84)
				OUT_RING  (chan, NvSema);
			else
				OUT_RING  (chan, chan->vram_handle);
		} else {
494 495
			u64 offset = chan->dispc_vma[nv_crtc->index].offset;
			offset += dispc->sem.offset;
496
			BEGIN_NVC0(chan, 0, 0x0010, 4);
497 498 499 500
			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);
501
			BEGIN_NVC0(chan, 0, 0x0010, 4);
502 503 504 505 506 507 508 509 510 511 512 513
			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 */
514
	BEGIN_NV04(evo, 0, 0x0100, 1);
515
	OUT_RING  (evo, 0xfffe0000);
516
	if (chan) {
517
		BEGIN_NV04(evo, 0, 0x0084, 1);
518 519
		OUT_RING  (evo, 0x00000100);
	} else {
520
		BEGIN_NV04(evo, 0, 0x0084, 1);
521 522 523 524
		OUT_RING  (evo, 0x00000010);
		/* allows gamma somehow, PDISP will bitch at you if
		 * you don't wait for vblank before changing this..
		 */
525
		BEGIN_NV04(evo, 0, 0x00e0, 1);
526 527
		OUT_RING  (evo, 0x40000000);
	}
528
	BEGIN_NV04(evo, 0, 0x0088, 4);
529 530 531 532
	OUT_RING  (evo, dispc->sem.offset);
	OUT_RING  (evo, 0xf00d0000 | dispc->sem.value);
	OUT_RING  (evo, 0x74b1e000);
	OUT_RING  (evo, NvEvoSync);
533
	BEGIN_NV04(evo, 0, 0x00a0, 2);
534 535
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
536
	BEGIN_NV04(evo, 0, 0x00c0, 1);
537
	OUT_RING  (evo, nv_fb->r_dma);
538
	BEGIN_NV04(evo, 0, 0x0110, 2);
539 540
	OUT_RING  (evo, 0x00000000);
	OUT_RING  (evo, 0x00000000);
541
	BEGIN_NV04(evo, 0, 0x0800, 5);
542
	OUT_RING  (evo, nv_fb->nvbo->bo.offset >> 8);
543 544 545 546
	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);
547
	BEGIN_NV04(evo, 0, 0x0080, 1);
548 549 550 551 552 553 554 555
	OUT_RING  (evo, 0x00000000);
	FIRE_RING (evo);

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

556 557 558
static u16
nv50_display_script_select(struct drm_device *dev, struct dcb_entry *dcb,
			   u32 mc, int pxclk)
559 560
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
561 562
	struct nouveau_connector *nv_connector = NULL;
	struct drm_encoder *encoder;
563
	struct nvbios *bios = &dev_priv->vbios;
564
	u32 script = 0, or;
565

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

569
		if (nv_encoder->dcb != dcb)
570 571 572 573 574 575
			continue;

		nv_connector = nouveau_encoder_connector_get(nv_encoder);
		break;
	}

576 577
	or = ffs(dcb->or) - 1;
	switch (dcb->type) {
578 579
	case OUTPUT_LVDS:
		script = (mc >> 8) & 0xf;
580
		if (bios->fp_no_ddc) {
581 582 583 584 585
			if (bios->fp.dual_link)
				script |= 0x0100;
			if (bios->fp.if_is_24bit)
				script |= 0x0200;
		} else {
586 587
			/* determine number of lvds links */
			if (nv_connector && nv_connector->edid &&
588
			    nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
589 590 591 592
				/* http://www.spwg.org */
				if (((u8 *)nv_connector->edid)[121] == 2)
					script |= 0x0100;
			} else
593 594
			if (pxclk >= bios->fp.duallink_transition_clk) {
				script |= 0x0100;
595 596 597 598
			}

			/* determine panel depth */
			if (script & 0x0100) {
599 600
				if (bios->fp.strapless_is_24bit & 2)
					script |= 0x0200;
601 602 603 604
			} else {
				if (bios->fp.strapless_is_24bit & 1)
					script |= 0x0200;
			}
605 606 607 608 609

			if (nv_connector && nv_connector->edid &&
			    (nv_connector->edid->revision >= 4) &&
			    (nv_connector->edid->input & 0x70) >= 0x20)
				script |= 0x0200;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		}

		if (nouveau_uscript_lvds >= 0) {
			NV_INFO(dev, "override script 0x%04x with 0x%04x "
				     "for output LVDS-%d\n", script,
				     nouveau_uscript_lvds, or);
			script = nouveau_uscript_lvds;
		}
		break;
	case OUTPUT_TMDS:
		script = (mc >> 8) & 0xf;
		if (pxclk >= 165000)
			script |= 0x0100;

		if (nouveau_uscript_tmds >= 0) {
			NV_INFO(dev, "override script 0x%04x with 0x%04x "
				     "for output TMDS-%d\n", script,
				     nouveau_uscript_tmds, or);
			script = nouveau_uscript_tmds;
		}
		break;
	case OUTPUT_DP:
		script = (mc >> 8) & 0xf;
		break;
	case OUTPUT_ANALOG:
		script = 0xff;
		break;
	default:
		NV_ERROR(dev, "modeset on unsupported output type!\n");
		break;
	}

	return script;
}

static void
nv50_display_vblank_crtc_handler(struct drm_device *dev, int crtc)
{
648
	nouveau_software_vblank(dev, crtc);
649
	drm_handle_vblank(dev, crtc);
650 651 652 653 654 655 656 657 658 659 660
}

static void
nv50_display_vblank_handler(struct drm_device *dev, uint32_t intr)
{
	if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_0)
		nv50_display_vblank_crtc_handler(dev, 0);

	if (intr & NV50_PDISPLAY_INTR_1_VBLANK_CRTC_1)
		nv50_display_vblank_crtc_handler(dev, 1);

661
	nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_VBLANK_CRTC);
662 663 664 665 666
}

static void
nv50_display_unk10_handler(struct drm_device *dev)
{
667
	struct drm_nouveau_private *dev_priv = dev->dev_private;
668
	struct nv50_display *disp = nv50_display(dev);
669
	u32 unk30 = nv_rd32(dev, 0x610030), mc;
670
	int i, crtc, or = 0, type = OUTPUT_ANY;
671

672
	NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
673
	disp->irq.dcb = NULL;
674 675 676

	nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) & ~8);

677 678 679 680 681 682 683 684 685 686 687
	/* 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;
688

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	/* Find which encoder was connected to the CRTC */
	for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
		mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_C(i));
		NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
		case 0: type = OUTPUT_ANALOG; break;
		case 1: type = OUTPUT_TV; break;
		default:
			NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
			goto ack;
		}

		or = i;
	}

707
	for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		if (dev_priv->chipset  < 0x90 ||
		    dev_priv->chipset == 0x92 ||
		    dev_priv->chipset == 0xa0)
			mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_C(i));
		else
			mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_C(i));

		NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
		case 0: type = OUTPUT_LVDS; break;
		case 1: type = OUTPUT_TMDS; break;
		case 2: type = OUTPUT_TMDS; break;
		case 5: type = OUTPUT_TMDS; break;
		case 8: type = OUTPUT_DP; break;
		case 9: type = OUTPUT_DP; break;
		default:
			NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
			goto ack;
		}

		or = i;
	}

	/* There was no encoder to disable */
	if (type == OUTPUT_ANY)
		goto ack;

	/* Disable the encoder */
	for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
		struct dcb_entry *dcb = &dev_priv->vbios.dcb.entry[i];

		if (dcb->type == type && (dcb->or & (1 << or))) {
743
			nouveau_bios_run_display_table(dev, 0, -1, dcb, -1);
744
			disp->irq.dcb = dcb;
745 746 747 748 749
			goto ack;
		}
	}

	NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
750 751 752 753 754 755 756 757
ack:
	nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK10);
	nv_wr32(dev, 0x610030, 0x80000000);
}

static void
nv50_display_unk20_handler(struct drm_device *dev)
{
758
	struct drm_nouveau_private *dev_priv = dev->dev_private;
759
	struct nv50_display *disp = nv50_display(dev);
760
	u32 unk30 = nv_rd32(dev, 0x610030), tmp, pclk, script, mc = 0;
761
	struct dcb_entry *dcb;
762
	int i, crtc, or = 0, type = OUTPUT_ANY;
763

764
	NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
765
	dcb = disp->irq.dcb;
766
	if (dcb) {
767
		nouveau_bios_run_display_table(dev, 0, -2, dcb, -1);
768
		disp->irq.dcb = NULL;
769 770 771 772 773 774 775
	}

	/* CRTC clock change requested? */
	crtc = ffs((unk30 & 0x00000600) >> 9) - 1;
	if (crtc >= 0) {
		pclk  = nv_rd32(dev, NV50_PDISPLAY_CRTC_P(crtc, CLOCK));
		pclk &= 0x003fffff;
776 777
		if (pclk)
			nv50_crtc_set_clock(dev, crtc, pclk);
778 779 780 781 782 783 784 785 786

		tmp = nv_rd32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc));
		tmp &= ~0x000000f;
		nv_wr32(dev, NV50_PDISPLAY_CRTC_CLK_CTRL2(crtc), tmp);
	}

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

790 791 792 793 794 795
	/* Find which encoder is connected to the CRTC */
	for (i = 0; type == OUTPUT_ANY && i < 3; i++) {
		mc = nv_rd32(dev, NV50_PDISPLAY_DAC_MODE_CTRL_P(i));
		NV_DEBUG_KMS(dev, "DAC-%d mc: 0x%08x\n", i, mc);
		if (!(mc & (1 << crtc)))
			continue;
796

797 798 799 800 801 802 803 804 805 806 807
		switch ((mc & 0x00000f00) >> 8) {
		case 0: type = OUTPUT_ANALOG; break;
		case 1: type = OUTPUT_TV; break;
		default:
			NV_ERROR(dev, "invalid mc, DAC-%d: 0x%08x\n", i, mc);
			goto ack;
		}

		or = i;
	}

808
	for (i = 0; type == OUTPUT_ANY && i < nv50_sor_nr(dev); i++) {
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
		if (dev_priv->chipset  < 0x90 ||
		    dev_priv->chipset == 0x92 ||
		    dev_priv->chipset == 0xa0)
			mc = nv_rd32(dev, NV50_PDISPLAY_SOR_MODE_CTRL_P(i));
		else
			mc = nv_rd32(dev, NV90_PDISPLAY_SOR_MODE_CTRL_P(i));

		NV_DEBUG_KMS(dev, "SOR-%d mc: 0x%08x\n", i, mc);
		if (!(mc & (1 << crtc)))
			continue;

		switch ((mc & 0x00000f00) >> 8) {
		case 0: type = OUTPUT_LVDS; break;
		case 1: type = OUTPUT_TMDS; break;
		case 2: type = OUTPUT_TMDS; break;
		case 5: type = OUTPUT_TMDS; break;
		case 8: type = OUTPUT_DP; break;
		case 9: type = OUTPUT_DP; break;
		default:
			NV_ERROR(dev, "invalid mc, SOR-%d: 0x%08x\n", i, mc);
			goto ack;
		}
831

832 833
		or = i;
	}
834

835 836
	if (type == OUTPUT_ANY)
		goto ack;
837

838 839 840 841 842 843
	/* Enable the encoder */
	for (i = 0; i < dev_priv->vbios.dcb.entries; i++) {
		dcb = &dev_priv->vbios.dcb.entry[i];
		if (dcb->type == type && (dcb->or & (1 << or)))
			break;
	}
844

845 846 847 848 849 850
	if (i == dev_priv->vbios.dcb.entries) {
		NV_ERROR(dev, "no dcb for %d %d 0x%08x\n", or, type, mc);
		goto ack;
	}

	script = nv50_display_script_select(dev, dcb, mc, pclk);
851
	nouveau_bios_run_display_table(dev, script, pclk, dcb, -1);
852

853 854 855
	if (type == OUTPUT_DP) {
		int link = !(dcb->dpconf.sor.link & 1);
		if ((mc & 0x000f0000) == 0x00020000)
856
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 18);
857
		else
858
			nv50_sor_dp_calc_tu(dev, or, link, pclk, 24);
859
	}
860 861

	if (dcb->type != OUTPUT_ANALOG) {
862 863 864 865 866 867 868 869 870
		tmp = nv_rd32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or));
		tmp &= ~0x00000f0f;
		if (script & 0x0100)
			tmp |= 0x00000101;
		nv_wr32(dev, NV50_PDISPLAY_SOR_CLK_CTRL2(or), tmp);
	} else {
		nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL2(or), 0);
	}

871 872 873
	disp->irq.dcb = dcb;
	disp->irq.pclk = pclk;
	disp->irq.script = script;
874

875 876 877 878 879
ack:
	nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK20);
	nv_wr32(dev, 0x610030, 0x80000000);
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
/* 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
nv50_display_unk40_dp_set_tmds(struct drm_device *dev, struct dcb_entry *dcb)
{
	int or = ffs(dcb->or) - 1, link = !(dcb->dpconf.sor.link & 1);
	struct drm_encoder *encoder;
	u32 tmp;

	if (dcb->type != OUTPUT_TMDS)
		return;

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

		if (nv_encoder->dcb->type == OUTPUT_DP &&
		    nv_encoder->dcb->or & (1 << or)) {
			tmp  = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
			tmp &= ~NV50_SOR_DP_CTRL_ENABLED;
			nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
			break;
		}
	}
}

911 912 913
static void
nv50_display_unk40_handler(struct drm_device *dev)
{
914 915 916 917
	struct nv50_display *disp = nv50_display(dev);
	struct dcb_entry *dcb = disp->irq.dcb;
	u16 script = disp->irq.script;
	u32 unk30 = nv_rd32(dev, 0x610030), pclk = disp->irq.pclk;
918

919
	NV_DEBUG_KMS(dev, "0x610030: 0x%08x\n", unk30);
920
	disp->irq.dcb = NULL;
921
	if (!dcb)
922 923
		goto ack;

924
	nouveau_bios_run_display_table(dev, script, -pclk, dcb, -1);
925 926
	nv50_display_unk40_dp_set_tmds(dev, dcb);

927 928 929 930 931 932
ack:
	nv_wr32(dev, NV50_PDISPLAY_INTR_1, NV50_PDISPLAY_INTR_1_CLK_UNK40);
	nv_wr32(dev, 0x610030, 0x80000000);
	nv_wr32(dev, 0x619494, nv_rd32(dev, 0x619494) | 8);
}

933 934
static void
nv50_display_bh(unsigned long data)
935
{
936
	struct drm_device *dev = (struct drm_device *)data;
937 938 939 940 941

	for (;;) {
		uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
		uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);

942
		NV_DEBUG_KMS(dev, "PDISPLAY_INTR_BH 0x%08x 0x%08x\n", intr0, intr1);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

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

	nv_wr32(dev, NV03_PMC_INTR_EN_0, 1);
}

static void
nv50_display_error_handler(struct drm_device *dev)
{
962 963 964
	u32 channels = (nv_rd32(dev, NV50_PDISPLAY_INTR_0) & 0x001f0000) >> 16;
	u32 addr, data;
	int chid;
965

966 967 968
	for (chid = 0; chid < 5; chid++) {
		if (!(channels & (1 << chid)))
			continue;
969

970 971 972 973 974 975
		nv_wr32(dev, NV50_PDISPLAY_INTR_0, 0x00010000 << chid);
		addr = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid));
		data = nv_rd32(dev, NV50_PDISPLAY_TRAPPED_DATA(chid));
		NV_ERROR(dev, "EvoCh %d Mthd 0x%04x Data 0x%08x "
			      "(0x%04x 0x%02x)\n", chid,
			 addr & 0xffc, data, addr >> 16, (addr >> 12) & 0xf);
976

977 978
		nv_wr32(dev, NV50_PDISPLAY_TRAPPED_ADDR(chid), 0x90000000);
	}
979 980
}

981 982
static void
nv50_display_isr(struct drm_device *dev)
983
{
984
	struct nv50_display *disp = nv50_display(dev);
985 986 987 988 989 990 991
	uint32_t delayed = 0;

	while (nv_rd32(dev, NV50_PMC_INTR_0) & NV50_PMC_INTR_0_DISPLAY) {
		uint32_t intr0 = nv_rd32(dev, NV50_PDISPLAY_INTR_0);
		uint32_t intr1 = nv_rd32(dev, NV50_PDISPLAY_INTR_1);
		uint32_t clock;

992
		NV_DEBUG_KMS(dev, "PDISPLAY_INTR 0x%08x 0x%08x\n", intr0, intr1);
993 994 995 996

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

997
		if (intr0 & 0x001f0000) {
998
			nv50_display_error_handler(dev);
999
			intr0 &= ~0x001f0000;
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		}

		if (intr1 & NV50_PDISPLAY_INTR_1_VBLANK_CRTC) {
			nv50_display_vblank_handler(dev, intr1);
			intr1 &= ~NV50_PDISPLAY_INTR_1_VBLANK_CRTC;
		}

		clock = (intr1 & (NV50_PDISPLAY_INTR_1_CLK_UNK10 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK20 |
				  NV50_PDISPLAY_INTR_1_CLK_UNK40));
		if (clock) {
			nv_wr32(dev, NV03_PMC_INTR_EN_0, 0);
1012
			tasklet_schedule(&disp->tasklet);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
			delayed |= clock;
			intr1 &= ~clock;
		}

		if (intr0) {
			NV_ERROR(dev, "unknown PDISPLAY_INTR_0: 0x%08x\n", intr0);
			nv_wr32(dev, NV50_PDISPLAY_INTR_0, intr0);
		}

		if (intr1) {
			NV_ERROR(dev,
				 "unknown PDISPLAY_INTR_1: 0x%08x\n", intr1);
			nv_wr32(dev, NV50_PDISPLAY_INTR_1, intr1);
		}
	}
}