nv50.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
/*
 * Copyright 2012 Red Hat Inc.
 *
 * 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 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 HOLDER(S) OR AUTHOR(S) 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.
 *
 * Authors: Ben Skeggs
 */
24
#include "nv50.h"
25
#include "rootnv50.h"
26

27
#include <core/client.h>
28
#include <core/enum.h>
29
#include <core/gpuobj.h>
30 31 32 33
#include <subdev/bios.h>
#include <subdev/bios/disp.h>
#include <subdev/bios/init.h>
#include <subdev/bios/pll.h>
34
#include <subdev/devinit.h>
35
#include <subdev/timer.h>
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static const struct nvkm_disp_oclass *
nv50_disp_root_(struct nvkm_disp *base)
{
	return nv50_disp(base)->func->root;
}

static int
nv50_disp_outp_internal_crt_(struct nvkm_disp *base, int index,
			     struct dcb_output *dcb, struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	return disp->func->outp.internal.crt(base, index, dcb, poutp);
}

static int
nv50_disp_outp_internal_tmds_(struct nvkm_disp *base, int index,
			      struct dcb_output *dcb,
			      struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	return disp->func->outp.internal.tmds(base, index, dcb, poutp);
}

static int
nv50_disp_outp_internal_lvds_(struct nvkm_disp *base, int index,
			      struct dcb_output *dcb,
			      struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	return disp->func->outp.internal.lvds(base, index, dcb, poutp);
}

static int
nv50_disp_outp_internal_dp_(struct nvkm_disp *base, int index,
			    struct dcb_output *dcb, struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	if (disp->func->outp.internal.dp)
		return disp->func->outp.internal.dp(base, index, dcb, poutp);
	return -ENODEV;
}

static int
nv50_disp_outp_external_tmds_(struct nvkm_disp *base, int index,
			      struct dcb_output *dcb,
			      struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	if (disp->func->outp.external.tmds)
		return disp->func->outp.external.tmds(base, index, dcb, poutp);
	return -ENODEV;
}

static int
nv50_disp_outp_external_dp_(struct nvkm_disp *base, int index,
			    struct dcb_output *dcb, struct nvkm_output **poutp)
{
	struct nv50_disp *disp = nv50_disp(base);
	if (disp->func->outp.external.dp)
		return disp->func->outp.external.dp(base, index, dcb, poutp);
	return -ENODEV;
}

100
static void
101
nv50_disp_vblank_fini_(struct nvkm_disp *base, int head)
102
{
103 104
	struct nv50_disp *disp = nv50_disp(base);
	disp->func->head.vblank_fini(disp, head);
105 106 107
}

static void
108
nv50_disp_vblank_init_(struct nvkm_disp *base, int head)
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	struct nv50_disp *disp = nv50_disp(base);
	disp->func->head.vblank_init(disp, head);
}

static void
nv50_disp_intr_(struct nvkm_disp *base)
{
	struct nv50_disp *disp = nv50_disp(base);
	disp->func->intr(disp);
}

static void *
nv50_disp_dtor_(struct nvkm_disp *base)
{
	struct nv50_disp *disp = nv50_disp(base);
	nvkm_event_fini(&disp->uevent);
	return disp;
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141
static const struct nvkm_disp_func
nv50_disp_ = {
	.dtor = nv50_disp_dtor_,
	.intr = nv50_disp_intr_,
	.root = nv50_disp_root_,
	.outp.internal.crt = nv50_disp_outp_internal_crt_,
	.outp.internal.tmds = nv50_disp_outp_internal_tmds_,
	.outp.internal.lvds = nv50_disp_outp_internal_lvds_,
	.outp.internal.dp = nv50_disp_outp_internal_dp_,
	.outp.external.tmds = nv50_disp_outp_external_tmds_,
	.outp.external.dp = nv50_disp_outp_external_dp_,
	.head.vblank_init = nv50_disp_vblank_init_,
	.head.vblank_fini = nv50_disp_vblank_fini_,
142 143
};

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
int
nv50_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
	       int index, int heads, struct nvkm_disp **pdisp)
{
	struct nv50_disp *disp;
	int ret;

	if (!(disp = kzalloc(sizeof(*disp), GFP_KERNEL)))
		return -ENOMEM;
	INIT_WORK(&disp->supervisor, func->super);
	disp->func = func;
	*pdisp = &disp->base;

	ret = nvkm_disp_ctor(&nv50_disp_, device, index, heads, &disp->base);
	if (ret)
		return ret;

	return nvkm_event_init(func->uevent, 1, 1 + (heads * 4), &disp->uevent);
}

void
nv50_disp_vblank_fini(struct nv50_disp *disp, int head)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	nvkm_mask(device, 0x61002c, (4 << head), 0);
}

void
nv50_disp_vblank_init(struct nv50_disp *disp, int head)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	nvkm_mask(device, 0x61002c, (4 << head), (4 << head));
}

178
static const struct nvkm_enum
179 180 181 182 183 184 185
nv50_disp_intr_error_type[] = {
	{ 3, "ILLEGAL_MTHD" },
	{ 4, "INVALID_VALUE" },
	{ 5, "INVALID_STATE" },
	{ 7, "INVALID_HANDLE" },
	{}
};
186

187
static const struct nvkm_enum
188 189 190 191
nv50_disp_intr_error_code[] = {
	{ 0x00, "" },
	{}
};
192

193
static void
B
Ben Skeggs 已提交
194
nv50_disp_intr_error(struct nv50_disp *disp, int chid)
195
{
196 197
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
198 199
	u32 data = nvkm_rd32(device, 0x610084 + (chid * 0x08));
	u32 addr = nvkm_rd32(device, 0x610080 + (chid * 0x08));
200 201 202
	u32 code = (addr & 0x00ff0000) >> 16;
	u32 type = (addr & 0x00007000) >> 12;
	u32 mthd = (addr & 0x00000ffc);
203
	const struct nvkm_enum *ec, *et;
204

205 206
	et = nvkm_enum_find(nv50_disp_intr_error_type, type);
	ec = nvkm_enum_find(nv50_disp_intr_error_code, code);
207

208 209 210 211
	nvkm_error(subdev,
		   "ERROR %d [%s] %02x [%s] chid %d mthd %04x data %08x\n",
		   type, et ? et->name : "", code, ec ? ec->name : "",
		   chid, mthd, data);
212

213
	if (chid < ARRAY_SIZE(disp->chan)) {
214 215
		switch (mthd) {
		case 0x0080:
216
			nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
217 218 219 220 221 222
			break;
		default:
			break;
		}
	}

223 224
	nvkm_wr32(device, 0x610020, 0x00010000 << chid);
	nvkm_wr32(device, 0x610080 + (chid * 0x08), 0x90000000);
225 226
}

227
static struct nvkm_output *
B
Ben Skeggs 已提交
228
exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
229
	    u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
230 231
	    struct nvbios_outp *info)
{
232 233
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_bios *bios = subdev->device->bios;
234 235
	struct nvkm_output *outp;
	u16 mask, type;
236

237
	if (or < 4) {
238 239
		type = DCB_OUTPUT_ANALOG;
		mask = 0;
240
	} else
241
	if (or < 8) {
242 243 244 245 246 247 248 249
		switch (ctrl & 0x00000f00) {
		case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
		case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
		case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
		case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
		case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
		case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
		default:
250
			nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
251
			return NULL;
252
		}
253
		or  -= 4;
254
	} else {
255
		or   = or - 8;
256 257 258
		type = 0x0010;
		mask = 0;
		switch (ctrl & 0x00000f00) {
B
Ben Skeggs 已提交
259
		case 0x00000000: type |= disp->pior.type[or]; break;
260
		default:
261
			nvkm_error(subdev, "unknown PIOR mc %08x\n", ctrl);
262
			return NULL;
263
		}
264 265 266
	}

	mask  = 0x00c0 & (mask << 6);
267
	mask |= 0x0001 << or;
268 269
	mask |= 0x0100 << head;

B
Ben Skeggs 已提交
270
	list_for_each_entry(outp, &disp->base.outp, head) {
271 272
		if ((outp->info.hasht & 0xff) == type &&
		    (outp->info.hashm & mask) == mask) {
273
			*data = nvbios_outp_match(bios, outp->info.hasht, mask,
274 275 276 277 278 279
						  ver, hdr, cnt, len, info);
			if (!*data)
				return NULL;
			return outp;
		}
	}
280

281
	return NULL;
282 283
}

284
static struct nvkm_output *
B
Ben Skeggs 已提交
285
exec_script(struct nv50_disp *disp, int head, int id)
286
{
287 288
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
289
	struct nvkm_bios *bios = device->bios;
290
	struct nvkm_output *outp;
291 292
	struct nvbios_outp info;
	u8  ver, hdr, cnt, len;
293
	u32 data, ctrl = 0;
294
	u32 reg;
295 296
	int i;

297
	/* DAC */
298
	for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
299
		ctrl = nvkm_rd32(device, 0x610b5c + (i * 8));
300

301
	/* SOR */
302
	if (!(ctrl & (1 << head))) {
303 304 305
		if (device->chipset  < 0x90 ||
		    device->chipset == 0x92 ||
		    device->chipset == 0xa0) {
306
			reg = 0x610b74;
307
		} else {
308
			reg = 0x610798;
309
		}
310
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
311
			ctrl = nvkm_rd32(device, reg + (i * 8));
312
		i += 4;
313 314
	}

315 316
	/* PIOR */
	if (!(ctrl & (1 << head))) {
317
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
318
			ctrl = nvkm_rd32(device, 0x610b84 + (i * 8));
319 320 321
		i += 8;
	}

322
	if (!(ctrl & (1 << head)))
323
		return NULL;
324
	i--;
325

B
Ben Skeggs 已提交
326
	outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
327
	if (outp) {
328
		struct nvbios_init init = {
329
			.subdev = subdev,
330 331
			.bios = bios,
			.offset = info.script[id],
332
			.outp = &outp->info,
333 334 335 336
			.crtc = head,
			.execute = 1,
		};

337
		nvbios_exec(&init);
338 339
	}

340
	return outp;
341 342
}

343
static struct nvkm_output *
B
Ben Skeggs 已提交
344
exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
345
{
346 347
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
348
	struct nvkm_bios *bios = device->bios;
349
	struct nvkm_output *outp;
350 351 352
	struct nvbios_outp info1;
	struct nvbios_ocfg info2;
	u8  ver, hdr, cnt, len;
353
	u32 data, ctrl = 0;
354
	u32 reg;
355 356
	int i;

357
	/* DAC */
358
	for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
359
		ctrl = nvkm_rd32(device, 0x610b58 + (i * 8));
360

361
	/* SOR */
362
	if (!(ctrl & (1 << head))) {
363 364 365
		if (device->chipset  < 0x90 ||
		    device->chipset == 0x92 ||
		    device->chipset == 0xa0) {
366
			reg = 0x610b70;
367
		} else {
368
			reg = 0x610794;
369
		}
370
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
371
			ctrl = nvkm_rd32(device, reg + (i * 8));
372
		i += 4;
373 374
	}

375 376
	/* PIOR */
	if (!(ctrl & (1 << head))) {
377
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
378
			ctrl = nvkm_rd32(device, 0x610b80 + (i * 8));
379 380 381
		i += 8;
	}

382
	if (!(ctrl & (1 << head)))
383
		return NULL;
384
	i--;
385

B
Ben Skeggs 已提交
386
	outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
387
	if (!outp)
388
		return NULL;
389

390
	*conf = (ctrl & 0x00000f00) >> 8;
391 392
	if (outp->info.location == 0) {
		switch (outp->info.type) {
393
		case DCB_OUTPUT_TMDS:
394
			if (*conf == 5)
395
				*conf |= 0x0100;
396 397
			break;
		case DCB_OUTPUT_LVDS:
398
			*conf |= disp->sor.lvdsconf;
399 400 401 402 403
			break;
		default:
			break;
		}
	} else {
404
		*conf = (ctrl & 0x00000f00) >> 8;
405
		pclk = pclk / 2;
406 407
	}

408 409
	data = nvbios_ocfg_match(bios, data, *conf & 0xff, *conf >> 8,
				 &ver, &hdr, &cnt, &len, &info2);
410
	if (data && id < 0xff) {
411 412 413
		data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
		if (data) {
			struct nvbios_init init = {
414
				.subdev = subdev,
415 416
				.bios = bios,
				.offset = data,
417
				.outp = &outp->info,
418 419 420 421
				.crtc = head,
				.execute = 1,
			};

422
			nvbios_exec(&init);
423 424 425
		}
	}

426
	return outp;
427 428
}

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static bool
nv50_disp_dptmds_war(struct nvkm_device *device)
{
	switch (device->chipset) {
	case 0x94:
	case 0x96:
	case 0x98:
	case 0xaa:
	case 0xac:
		return true;
	default:
		break;
	}
	return false;
}

static bool
nv50_disp_dptmds_war_needed(struct nv50_disp *disp, struct dcb_output *outp)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	const u32 soff = __ffs(outp->or) * 0x800;
	if (nv50_disp_dptmds_war(device) && outp->type == DCB_OUTPUT_TMDS) {
		switch (nvkm_rd32(device, 0x614300 + soff) & 0x00030000) {
		case 0x00000000:
		case 0x00030000:
			return true;
		default:
			break;
		}
	}
	return false;

}

static void
nv50_disp_dptmds_war_2(struct nv50_disp *disp, struct dcb_output *outp)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	const u32 soff = __ffs(outp->or) * 0x800;

	if (!nv50_disp_dptmds_war_needed(disp, outp))
		return;

	nvkm_mask(device, 0x00e840, 0x80000000, 0x80000000);
	nvkm_mask(device, 0x614300 + soff, 0x03000000, 0x03000000);
	nvkm_mask(device, 0x61c10c + soff, 0x00000001, 0x00000001);

	nvkm_mask(device, 0x61c00c + soff, 0x0f000000, 0x00000000);
	nvkm_mask(device, 0x61c008 + soff, 0xff000000, 0x14000000);
	nvkm_usec(device, 400, NVKM_DELAY);
	nvkm_mask(device, 0x61c008 + soff, 0xff000000, 0x00000000);
	nvkm_mask(device, 0x61c00c + soff, 0x0f000000, 0x01000000);

	if (nvkm_rd32(device, 0x61c004 + soff) & 0x00000001) {
		u32 seqctl = nvkm_rd32(device, 0x61c030 + soff);
		u32  pu_pc = seqctl & 0x0000000f;
		nvkm_wr32(device, 0x61c040 + soff + pu_pc * 4, 0x1f008000);
	}
}

static void
nv50_disp_dptmds_war_3(struct nv50_disp *disp, struct dcb_output *outp)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	const u32 soff = __ffs(outp->or) * 0x800;
	u32 sorpwr;

	if (!nv50_disp_dptmds_war_needed(disp, outp))
		return;

	sorpwr = nvkm_rd32(device, 0x61c004 + soff);
	if (sorpwr & 0x00000001) {
		u32 seqctl = nvkm_rd32(device, 0x61c030 + soff);
		u32  pd_pc = (seqctl & 0x00000f00) >> 8;
		u32  pu_pc =  seqctl & 0x0000000f;

		nvkm_wr32(device, 0x61c040 + soff + pd_pc * 4, 0x1f008000);

		nvkm_msec(device, 2000,
			if (!(nvkm_rd32(device, 0x61c030 + soff) & 0x10000000))
				break;
		);
		nvkm_mask(device, 0x61c004 + soff, 0x80000001, 0x80000000);
		nvkm_msec(device, 2000,
			if (!(nvkm_rd32(device, 0x61c030 + soff) & 0x10000000))
				break;
		);

		nvkm_wr32(device, 0x61c040 + soff + pd_pc * 4, 0x00002000);
		nvkm_wr32(device, 0x61c040 + soff + pu_pc * 4, 0x1f000000);
	}

	nvkm_mask(device, 0x61c10c + soff, 0x00000001, 0x00000000);
	nvkm_mask(device, 0x614300 + soff, 0x03000000, 0x00000000);

	if (sorpwr & 0x00000001) {
		nvkm_mask(device, 0x61c004 + soff, 0x80000001, 0x80000001);
	}
}

static void
nv50_disp_update_sppll1(struct nv50_disp *disp)
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
	bool used = false;
	int sor;

	if (!nv50_disp_dptmds_war(device))
		return;

	for (sor = 0; sor < disp->func->sor.nr; sor++) {
		u32 clksor = nvkm_rd32(device, 0x614300 + (sor * 0x800));
		switch (clksor & 0x03000000) {
		case 0x02000000:
		case 0x03000000:
			used = true;
			break;
		default:
			break;
		}
	}

	if (used)
		return;

	nvkm_mask(device, 0x00e840, 0x80000000, 0x00000000);
}

557
static void
B
Ben Skeggs 已提交
558
nv50_disp_intr_unk10_0(struct nv50_disp *disp, int head)
559
{
B
Ben Skeggs 已提交
560
	exec_script(disp, head, 1);
561
}
562

563
static void
B
Ben Skeggs 已提交
564
nv50_disp_intr_unk20_0(struct nv50_disp *disp, int head)
565
{
566
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
B
Ben Skeggs 已提交
567
	struct nvkm_output *outp = exec_script(disp, head, 2);
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

	/* the binary driver does this outside of the supervisor handling
	 * (after the third supervisor from a detach).  we (currently?)
	 * allow both detach/attach to happen in the same set of
	 * supervisor interrupts, so it would make sense to execute this
	 * (full power down?) script after all the detach phases of the
	 * supervisor handling.  like with training if needed from the
	 * second supervisor, nvidia doesn't do this, so who knows if it's
	 * entirely safe, but it does appear to work..
	 *
	 * without this script being run, on some configurations i've
	 * seen, switching from DP to TMDS on a DP connector may result
	 * in a blank screen (SOR_PWR off/on can restore it)
	 */
	if (outp && outp->info.type == DCB_OUTPUT_DP) {
583
		struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
584
		struct nvbios_init init = {
585 586
			.subdev = subdev,
			.bios = subdev->device->bios,
587 588 589 590 591 592 593 594 595
			.outp = &outp->info,
			.crtc = head,
			.offset = outpdp->info.script[4],
			.execute = 1,
		};

		nvbios_exec(&init);
		atomic_set(&outpdp->lt.done, 0);
	}
596 597 598
}

static void
B
Ben Skeggs 已提交
599
nv50_disp_intr_unk20_1(struct nv50_disp *disp, int head)
600
{
601 602 603
	struct nvkm_device *device = disp->base.engine.subdev.device;
	struct nvkm_devinit *devinit = device->devinit;
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
604
	if (pclk)
605
		nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
606 607 608
}

static void
B
Ben Skeggs 已提交
609
nv50_disp_intr_unk20_2_dp(struct nv50_disp *disp, int head,
610
			  struct dcb_output *outp, u32 pclk)
611
{
612 613
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
614 615 616 617
	const int link = !(outp->sorconf.link & 1);
	const int   or = ffs(outp->or) - 1;
	const u32 soff = (  or * 0x800);
	const u32 loff = (link * 0x080) + soff;
618
	const u32 ctrl = nvkm_rd32(device, 0x610794 + (or * 8));
619
	const u32 symbol = 100000;
620 621 622 623 624
	const s32 vactive = nvkm_rd32(device, 0x610af8 + (head * 0x540)) & 0xffff;
	const s32 vblanke = nvkm_rd32(device, 0x610ae8 + (head * 0x540)) & 0xffff;
	const s32 vblanks = nvkm_rd32(device, 0x610af0 + (head * 0x540)) & 0xffff;
	u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
	u32 clksor = nvkm_rd32(device, 0x614300 + soff);
625 626 627 628
	int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
	int TU, VTUi, VTUf, VTUa;
	u64 link_data_rate, link_ratio, unk;
	u32 best_diff = 64 * symbol;
B
Ben Skeggs 已提交
629
	u32 link_nr, link_bw, bits;
630 631 632 633 634 635 636 637 638 639
	u64 value;

	link_bw = (clksor & 0x000c0000) ? 270000 : 162000;
	link_nr = hweight32(dpctrl & 0x000f0000);

	/* symbols/hblank - algorithm taken from comments in tegra driver */
	value = vblanke + vactive - vblanks - 7;
	value = value * link_bw;
	do_div(value, pclk);
	value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
640
	nvkm_mask(device, 0x61c1e8 + soff, 0x0000ffff, value);
641 642 643 644 645 646

	/* symbols/vblank - algorithm taken from comments in tegra driver */
	value = vblanks - vblanke - 25;
	value = value * link_bw;
	do_div(value, pclk);
	value = value - ((36 / link_nr) + 3) - 1;
647
	nvkm_mask(device, 0x61c1ec + soff, 0x00ffffff, value);
648 649

	/* watermark / activesym */
650 651 652 653
	if      ((ctrl & 0xf0000) == 0x60000) bits = 30;
	else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
	else                                  bits = 18;

654 655 656 657
	link_data_rate = (pclk * bits / 8) / link_nr;

	/* calculate ratio of packed data rate to link symbol rate */
	link_ratio = link_data_rate * symbol;
B
Ben Skeggs 已提交
658
	do_div(link_ratio, link_bw);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

	for (TU = 64; TU >= 32; TU--) {
		/* calculate average number of valid symbols in each TU */
		u32 tu_valid = link_ratio * TU;
		u32 calc, diff;

		/* find a hw representation for the fraction.. */
		VTUi = tu_valid / symbol;
		calc = VTUi * symbol;
		diff = tu_valid - calc;
		if (diff) {
			if (diff >= (symbol / 2)) {
				VTUf = symbol / (symbol - diff);
				if (symbol - (VTUf * diff))
					VTUf++;

				if (VTUf <= 15) {
					VTUa  = 1;
					calc += symbol - (symbol / VTUf);
				} else {
					VTUa  = 0;
					VTUf  = 1;
					calc += symbol;
				}
			} else {
				VTUa  = 0;
				VTUf  = min((int)(symbol / diff), 15);
				calc += symbol / VTUf;
			}

			diff = calc - tu_valid;
		} else {
			/* no remainder, but the hw doesn't like the fractional
			 * part to be zero.  decrement the integer part and
			 * have the fraction add a whole symbol back
			 */
			VTUa = 0;
			VTUf = 1;
			VTUi--;
		}

		if (diff < best_diff) {
			best_diff = diff;
			bestTU = TU;
			bestVTUa = VTUa;
			bestVTUf = VTUf;
			bestVTUi = VTUi;
			if (diff == 0)
				break;
		}
	}

	if (!bestTU) {
712
		nvkm_error(subdev, "unable to find suitable dp config\n");
713 714 715 716 717 718
		return;
	}

	/* XXX close to vbios numbers, but not right */
	unk  = (symbol - link_ratio) * bestTU;
	unk *= link_ratio;
B
Ben Skeggs 已提交
719 720
	do_div(unk, symbol);
	do_div(unk, symbol);
721 722
	unk += 6;

723 724
	nvkm_mask(device, 0x61c10c + loff, 0x000001fc, bestTU << 2);
	nvkm_mask(device, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
725 726 727 728 729
						   bestVTUf << 16 |
						   bestVTUi << 8 | unk);
}

static void
B
Ben Skeggs 已提交
730
nv50_disp_intr_unk20_2(struct nv50_disp *disp, int head)
731
{
732
	struct nvkm_device *device = disp->base.engine.subdev.device;
733
	struct nvkm_output *outp;
734
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
735 736
	u32 hval, hreg = 0x614200 + (head * 0x800);
	u32 oval, oreg;
737
	u32 mask, conf;
738

B
Ben Skeggs 已提交
739
	outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
740 741
	if (!outp)
		return;
742

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	/* we allow both encoder attach and detach operations to occur
	 * within a single supervisor (ie. modeset) sequence.  the
	 * encoder detach scripts quite often switch off power to the
	 * lanes, which requires the link to be re-trained.
	 *
	 * this is not generally an issue as the sink "must" (heh)
	 * signal an irq when it's lost sync so the driver can
	 * re-train.
	 *
	 * however, on some boards, if one does not configure at least
	 * the gpu side of the link *before* attaching, then various
	 * things can go horribly wrong (PDISP disappearing from mmio,
	 * third supervisor never happens, etc).
	 *
	 * the solution is simply to retrain here, if necessary.  last
	 * i checked, the binary driver userspace does not appear to
	 * trigger this situation (it forces an UPDATE between steps).
	 */
761
	if (outp->info.type == DCB_OUTPUT_DP) {
762
		u32 soff = (ffs(outp->info.or) - 1) * 0x08;
763 764 765
		u32 ctrl, datarate;

		if (outp->info.location == 0) {
766
			ctrl = nvkm_rd32(device, 0x610794 + soff);
767 768
			soff = 1;
		} else {
769
			ctrl = nvkm_rd32(device, 0x610b80 + soff);
770 771
			soff = 2;
		}
772 773

		switch ((ctrl & 0x000f0000) >> 16) {
774 775
		case 6: datarate = pclk * 30; break;
		case 5: datarate = pclk * 24; break;
776 777
		case 2:
		default:
778
			datarate = pclk * 18;
779
			break;
780 781
		}

782
		if (nvkm_output_dp_train(outp, datarate / soff, true))
783
			OUTP_ERR(outp, "link not trained before attach");
784 785
	}

B
Ben Skeggs 已提交
786
	exec_clkcmp(disp, head, 0, pclk, &conf);
787 788 789 790 791 792 793 794 795

	if (!outp->info.location && outp->info.type == DCB_OUTPUT_ANALOG) {
		oreg = 0x614280 + (ffs(outp->info.or) - 1) * 0x800;
		oval = 0x00000000;
		hval = 0x00000000;
		mask = 0xffffffff;
	} else
	if (!outp->info.location) {
		if (outp->info.type == DCB_OUTPUT_DP)
B
Ben Skeggs 已提交
796
			nv50_disp_intr_unk20_2_dp(disp, head, &outp->info, pclk);
797 798 799 800 801 802 803 804 805
		oreg = 0x614300 + (ffs(outp->info.or) - 1) * 0x800;
		oval = (conf & 0x0100) ? 0x00000101 : 0x00000000;
		hval = 0x00000000;
		mask = 0x00000707;
	} else {
		oreg = 0x614380 + (ffs(outp->info.or) - 1) * 0x800;
		oval = 0x00000001;
		hval = 0x00000001;
		mask = 0x00000707;
806
	}
807

808 809
	nvkm_mask(device, hreg, 0x0000000f, hval);
	nvkm_mask(device, oreg, mask, oval);
810 811

	nv50_disp_dptmds_war_2(disp, &outp->info);
812 813 814 815 816 817 818 819 820 821 822
}

/* 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
B
Ben Skeggs 已提交
823
nv50_disp_intr_unk40_0_tmds(struct nv50_disp *disp,
824
			    struct dcb_output *outp)
825
{
826 827
	struct nvkm_device *device = disp->base.engine.subdev.device;
	struct nvkm_bios *bios = device->bios;
828 829 830 831
	const int link = !(outp->sorconf.link & 1);
	const int   or = ffs(outp->or) - 1;
	const u32 loff = (or * 0x800) + (link * 0x80);
	const u16 mask = (outp->sorconf.link << 6) | outp->or;
832
	struct dcb_output match;
833 834
	u8  ver, hdr;

835
	if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
836
		nvkm_mask(device, 0x61c10c + loff, 0x00000001, 0x00000000);
837 838 839
}

static void
B
Ben Skeggs 已提交
840
nv50_disp_intr_unk40_0(struct nv50_disp *disp, int head)
841
{
842
	struct nvkm_device *device = disp->base.engine.subdev.device;
843
	struct nvkm_output *outp;
844
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
845
	u32 conf;
846

B
Ben Skeggs 已提交
847
	outp = exec_clkcmp(disp, head, 1, pclk, &conf);
848 849 850 851
	if (!outp)
		return;

	if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
B
Ben Skeggs 已提交
852
		nv50_disp_intr_unk40_0_tmds(disp, &outp->info);
853
	nv50_disp_dptmds_war_3(disp, &outp->info);
854 855
}

856 857
void
nv50_disp_intr_supervisor(struct work_struct *work)
858
{
B
Ben Skeggs 已提交
859 860
	struct nv50_disp *disp =
		container_of(work, struct nv50_disp, supervisor);
861 862
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
863
	u32 super = nvkm_rd32(device, 0x610030);
864
	int head;
865

866
	nvkm_debug(subdev, "supervisor %08x %08x\n", disp->super, super);
867

B
Ben Skeggs 已提交
868
	if (disp->super & 0x00000010) {
869
		nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
870
		for (head = 0; head < disp->base.head.nr; head++) {
871 872 873 874
			if (!(super & (0x00000020 << head)))
				continue;
			if (!(super & (0x00000080 << head)))
				continue;
B
Ben Skeggs 已提交
875
			nv50_disp_intr_unk10_0(disp, head);
876 877
		}
	} else
B
Ben Skeggs 已提交
878
	if (disp->super & 0x00000020) {
879
		for (head = 0; head < disp->base.head.nr; head++) {
880 881
			if (!(super & (0x00000080 << head)))
				continue;
B
Ben Skeggs 已提交
882
			nv50_disp_intr_unk20_0(disp, head);
883
		}
884
		for (head = 0; head < disp->base.head.nr; head++) {
885 886
			if (!(super & (0x00000200 << head)))
				continue;
B
Ben Skeggs 已提交
887
			nv50_disp_intr_unk20_1(disp, head);
888
		}
889
		for (head = 0; head < disp->base.head.nr; head++) {
890 891
			if (!(super & (0x00000080 << head)))
				continue;
B
Ben Skeggs 已提交
892
			nv50_disp_intr_unk20_2(disp, head);
893 894
		}
	} else
B
Ben Skeggs 已提交
895
	if (disp->super & 0x00000040) {
896
		for (head = 0; head < disp->base.head.nr; head++) {
897 898
			if (!(super & (0x00000080 << head)))
				continue;
B
Ben Skeggs 已提交
899
			nv50_disp_intr_unk40_0(disp, head);
900
		}
901
		nv50_disp_update_sppll1(disp);
902 903
	}

904
	nvkm_wr32(device, 0x610030, 0x80000000);
905 906
}

907
void
908
nv50_disp_intr(struct nv50_disp *disp)
909
{
910 911 912
	struct nvkm_device *device = disp->base.engine.subdev.device;
	u32 intr0 = nvkm_rd32(device, 0x610020);
	u32 intr1 = nvkm_rd32(device, 0x610024);
913

914 915
	while (intr0 & 0x001f0000) {
		u32 chid = __ffs(intr0 & 0x001f0000) - 16;
B
Ben Skeggs 已提交
916
		nv50_disp_intr_error(disp, chid);
917
		intr0 &= ~(0x00010000 << chid);
918 919
	}

920 921
	while (intr0 & 0x0000001f) {
		u32 chid = __ffs(intr0 & 0x0000001f);
B
Ben Skeggs 已提交
922
		nv50_disp_chan_uevent_send(disp, chid);
923 924 925
		intr0 &= ~(0x00000001 << chid);
	}

926
	if (intr1 & 0x00000004) {
B
Ben Skeggs 已提交
927
		nvkm_disp_vblank(&disp->base, 0);
928
		nvkm_wr32(device, 0x610024, 0x00000004);
929 930
	}

931
	if (intr1 & 0x00000008) {
B
Ben Skeggs 已提交
932
		nvkm_disp_vblank(&disp->base, 1);
933
		nvkm_wr32(device, 0x610024, 0x00000008);
934 935
	}

936
	if (intr1 & 0x00000070) {
B
Ben Skeggs 已提交
937 938
		disp->super = (intr1 & 0x00000070);
		schedule_work(&disp->supervisor);
939
		nvkm_wr32(device, 0x610024, disp->super);
940
	}
941 942
}

943
static const struct nv50_disp_func
944
nv50_disp = {
945 946 947
	.intr = nv50_disp_intr,
	.uevent = &nv50_disp_chan_uevent,
	.super = nv50_disp_intr_supervisor,
948
	.root = &nv50_disp_root_oclass,
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	.head.vblank_init = nv50_disp_vblank_init,
	.head.vblank_fini = nv50_disp_vblank_fini,
	.head.scanoutpos = nv50_disp_root_scanoutpos,
	.outp.internal.crt = nv50_dac_output_new,
	.outp.internal.tmds = nv50_sor_output_new,
	.outp.internal.lvds = nv50_sor_output_new,
	.outp.external.tmds = nv50_pior_output_new,
	.outp.external.dp = nv50_pior_dp_new,
	.dac.nr = 3,
	.dac.power = nv50_dac_power,
	.dac.sense = nv50_dac_sense,
	.sor.nr = 2,
	.sor.power = nv50_sor_power,
	.pior.nr = 3,
	.pior.power = nv50_pior_power,
964 965
};

966 967
int
nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
968
{
969
	return nv50_disp_new_(&nv50_disp, device, index, 2, pdisp);
970
}