nv50.c 23.1 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 "head.h"
26
#include "ior.h"
27
#include "rootnv50.h"
28

29
#include <core/client.h>
30
#include <core/enum.h>
31
#include <core/gpuobj.h>
32 33 34 35
#include <subdev/bios.h>
#include <subdev/bios/disp.h>
#include <subdev/bios/init.h>
#include <subdev/bios/pll.h>
36
#include <subdev/devinit.h>
37
#include <subdev/timer.h>
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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;
}

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);
114 115
	if (disp->wq)
		destroy_workqueue(disp->wq);
116
	return disp;
117 118
}

119 120 121 122 123 124 125 126 127 128 129
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_,
130 131
};

132 133 134 135 136
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;
137
	int ret, i;
138 139 140 141 142 143

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

144
	ret = nvkm_disp_ctor(&nv50_disp_, device, index, &disp->base);
145 146 147
	if (ret)
		return ret;

148 149 150 151 152
	disp->wq = create_singlethread_workqueue("nvkm-disp");
	if (!disp->wq)
		return -ENOMEM;
	INIT_WORK(&disp->supervisor, func->super);

153 154 155 156 157 158
	for (i = 0; func->head.new && i < heads; i++) {
		ret = func->head.new(&disp->base, i);
		if (ret)
			return ret;
	}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	for (i = 0; func->dac.new && i < func->dac.nr; i++) {
		ret = func->dac.new(&disp->base, i);
		if (ret)
			return ret;
	}

	for (i = 0; func->pior.new && i < func->pior.nr; i++) {
		ret = func->pior.new(&disp->base, i);
		if (ret)
			return ret;
	}

	for (i = 0; func->sor.new && i < func->sor.nr; i++) {
		ret = func->sor.new(&disp->base, i);
		if (ret)
			return ret;
	}

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

180
static struct nvkm_output *
B
Ben Skeggs 已提交
181
exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
182
	    u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
183 184
	    struct nvbios_outp *info)
{
185 186
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_bios *bios = subdev->device->bios;
187 188
	struct nvkm_output *outp;
	u16 mask, type;
189

190
	if (or < 4) {
191 192
		type = DCB_OUTPUT_ANALOG;
		mask = 0;
193
	} else
194
	if (or < 8) {
195 196 197 198 199 200 201 202
		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:
203
			nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
204
			return NULL;
205
		}
206
		or  -= 4;
207
	} else {
208
		or   = or - 8;
209 210 211
		type = 0x0010;
		mask = 0;
		switch (ctrl & 0x00000f00) {
B
Ben Skeggs 已提交
212
		case 0x00000000: type |= disp->pior.type[or]; break;
213
		default:
214
			nvkm_error(subdev, "unknown PIOR mc %08x\n", ctrl);
215
			return NULL;
216
		}
217 218 219
	}

	mask  = 0x00c0 & (mask << 6);
220
	mask |= 0x0001 << or;
221 222
	mask |= 0x0100 << head;

B
Ben Skeggs 已提交
223
	list_for_each_entry(outp, &disp->base.outp, head) {
224 225
		if ((outp->info.hasht & 0xff) == type &&
		    (outp->info.hashm & mask) == mask) {
226
			*data = nvbios_outp_match(bios, outp->info.hasht, mask,
227 228 229 230 231 232
						  ver, hdr, cnt, len, info);
			if (!*data)
				return NULL;
			return outp;
		}
	}
233

234
	return NULL;
235 236
}

237
static struct nvkm_output *
B
Ben Skeggs 已提交
238
exec_script(struct nv50_disp *disp, int head, int id)
239
{
240 241
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
242
	struct nvkm_bios *bios = device->bios;
243
	struct nvkm_output *outp;
244 245
	struct nvbios_outp info;
	u8  ver, hdr, cnt, len;
246
	u32 data, ctrl = 0;
247
	u32 reg;
248 249
	int i;

250
	/* DAC */
251
	for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
252
		ctrl = nvkm_rd32(device, 0x610b5c + (i * 8));
253

254
	/* SOR */
255
	if (!(ctrl & (1 << head))) {
256 257 258
		if (device->chipset  < 0x90 ||
		    device->chipset == 0x92 ||
		    device->chipset == 0xa0) {
259
			reg = 0x610b74;
260
		} else {
261
			reg = 0x610798;
262
		}
263
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
264
			ctrl = nvkm_rd32(device, reg + (i * 8));
265
		i += 4;
266 267
	}

268 269
	/* PIOR */
	if (!(ctrl & (1 << head))) {
270
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
271
			ctrl = nvkm_rd32(device, 0x610b84 + (i * 8));
272 273 274
		i += 8;
	}

275
	if (!(ctrl & (1 << head)))
276
		return NULL;
277
	i--;
278

B
Ben Skeggs 已提交
279
	outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
280
	if (outp) {
281
		struct nvbios_init init = {
282
			.subdev = subdev,
283 284
			.bios = bios,
			.offset = info.script[id],
285
			.outp = &outp->info,
286 287 288 289
			.crtc = head,
			.execute = 1,
		};

290
		nvbios_exec(&init);
291 292
	}

293
	return outp;
294 295
}

296
static struct nvkm_output *
B
Ben Skeggs 已提交
297
exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
298
{
299 300
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
301
	struct nvkm_bios *bios = device->bios;
302
	struct nvkm_output *outp;
303 304 305
	struct nvbios_outp info1;
	struct nvbios_ocfg info2;
	u8  ver, hdr, cnt, len;
306
	u32 data, ctrl = 0;
307
	u32 reg;
308 309
	int i;

310
	/* DAC */
311
	for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
312
		ctrl = nvkm_rd32(device, 0x610b58 + (i * 8));
313

314
	/* SOR */
315
	if (!(ctrl & (1 << head))) {
316 317 318
		if (device->chipset  < 0x90 ||
		    device->chipset == 0x92 ||
		    device->chipset == 0xa0) {
319
			reg = 0x610b70;
320
		} else {
321
			reg = 0x610794;
322
		}
323
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
324
			ctrl = nvkm_rd32(device, reg + (i * 8));
325
		i += 4;
326 327
	}

328 329
	/* PIOR */
	if (!(ctrl & (1 << head))) {
330
		for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
331
			ctrl = nvkm_rd32(device, 0x610b80 + (i * 8));
332 333 334
		i += 8;
	}

335
	if (!(ctrl & (1 << head)))
336
		return NULL;
337
	i--;
338

B
Ben Skeggs 已提交
339
	outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
340
	if (!outp)
341
		return NULL;
342

343
	*conf = (ctrl & 0x00000f00) >> 8;
344 345
	if (outp->info.location == 0) {
		switch (outp->info.type) {
346
		case DCB_OUTPUT_TMDS:
347
			if (*conf == 5)
348
				*conf |= 0x0100;
349 350
			break;
		case DCB_OUTPUT_LVDS:
351
			*conf |= disp->sor.lvdsconf;
352 353 354 355 356
			break;
		default:
			break;
		}
	} else {
357
		*conf = (ctrl & 0x00000f00) >> 8;
358
		pclk = pclk / 2;
359 360
	}

361 362
	data = nvbios_ocfg_match(bios, data, *conf & 0xff, *conf >> 8,
				 &ver, &hdr, &cnt, &len, &info2);
363
	if (data && id < 0xff) {
364 365 366
		data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
		if (data) {
			struct nvbios_init init = {
367
				.subdev = subdev,
368 369
				.bios = bios,
				.offset = data,
370
				.outp = &outp->info,
371 372 373 374
				.crtc = head,
				.execute = 1,
			};

375
			nvbios_exec(&init);
376 377 378
		}
	}

379
	return outp;
380 381
}

382 383 384 385 386 387 388 389
/* 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.
 */
390
static void
391 392
nv50_disp_intr_unk40_0_tmds(struct nv50_disp *disp,
			    struct dcb_output *outp)
393 394
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
395 396 397 398 399 400 401
	struct nvkm_bios *bios = device->bios;
	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;
	struct dcb_output match;
	u8  ver, hdr;
402

403 404
	if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
		nvkm_mask(device, 0x61c10c + loff, 0x00000001, 0x00000000);
405 406 407
}

static void
408
nv50_disp_intr_unk40_0(struct nv50_disp *disp, int head)
409 410
{
	struct nvkm_device *device = disp->base.engine.subdev.device;
411 412 413
	struct nvkm_output *outp;
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
	u32 conf;
414

415 416
	outp = exec_clkcmp(disp, head, 1, pclk, &conf);
	if (!outp)
417 418
		return;

419 420 421
	if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
		nv50_disp_intr_unk40_0_tmds(disp, &outp->info);
	nv50_disp_dptmds_war_3(disp, &outp->info);
422 423 424
}

static void
B
Ben Skeggs 已提交
425
nv50_disp_intr_unk20_2_dp(struct nv50_disp *disp, int head,
426
			  struct dcb_output *outp, u32 pclk)
427
{
428 429
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
430 431 432 433
	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;
434
	const u32 ctrl = nvkm_rd32(device, 0x610794 + (or * 8));
435
	const u32 symbol = 100000;
436 437 438 439 440
	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);
441 442 443 444
	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 已提交
445
	u32 link_nr, link_bw, bits;
446 447 448 449 450 451 452 453 454 455
	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);
456
	nvkm_mask(device, 0x61c1e8 + soff, 0x0000ffff, value);
457 458 459 460 461 462

	/* 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;
463
	nvkm_mask(device, 0x61c1ec + soff, 0x00ffffff, value);
464 465

	/* watermark / activesym */
466 467 468 469
	if      ((ctrl & 0xf0000) == 0x60000) bits = 30;
	else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
	else                                  bits = 18;

470 471 472 473
	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 已提交
474
	do_div(link_ratio, link_bw);
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

	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) {
528
		nvkm_error(subdev, "unable to find suitable dp config\n");
529 530 531 532 533 534
		return;
	}

	/* XXX close to vbios numbers, but not right */
	unk  = (symbol - link_ratio) * bestTU;
	unk *= link_ratio;
B
Ben Skeggs 已提交
535 536
	do_div(unk, symbol);
	do_div(unk, symbol);
537 538
	unk += 6;

539 540
	nvkm_mask(device, 0x61c10c + loff, 0x000001fc, bestTU << 2);
	nvkm_mask(device, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
541 542 543 544 545
						   bestVTUf << 16 |
						   bestVTUi << 8 | unk);
}

static void
B
Ben Skeggs 已提交
546
nv50_disp_intr_unk20_2(struct nv50_disp *disp, int head)
547
{
548
	struct nvkm_device *device = disp->base.engine.subdev.device;
549
	struct nvkm_output *outp;
550
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
551 552
	u32 hval, hreg = 0x614200 + (head * 0x800);
	u32 oval, oreg;
553
	u32 mask, conf;
554

B
Ben Skeggs 已提交
555
	outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
556 557
	if (!outp)
		return;
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	/* 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).
	 */
577
	if (outp->info.type == DCB_OUTPUT_DP) {
578
		u32 soff = (ffs(outp->info.or) - 1) * 0x08;
579 580 581
		u32 ctrl, datarate;

		if (outp->info.location == 0) {
582
			ctrl = nvkm_rd32(device, 0x610794 + soff);
583 584
			soff = 1;
		} else {
585
			ctrl = nvkm_rd32(device, 0x610b80 + soff);
586 587
			soff = 2;
		}
588 589

		switch ((ctrl & 0x000f0000) >> 16) {
590 591
		case 6: datarate = pclk * 30; break;
		case 5: datarate = pclk * 24; break;
592 593
		case 2:
		default:
594
			datarate = pclk * 18;
595
			break;
596 597
		}

598
		if (nvkm_output_dp_train(outp, datarate / soff))
599
			OUTP_ERR(outp, "link not trained before attach");
600 601
	}

B
Ben Skeggs 已提交
602
	exec_clkcmp(disp, head, 0, pclk, &conf);
603 604 605 606 607 608 609 610 611

	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 已提交
612
			nv50_disp_intr_unk20_2_dp(disp, head, &outp->info, pclk);
613 614 615 616 617 618 619 620 621
		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;
622
	}
623

624 625
	nvkm_mask(device, hreg, 0x0000000f, hval);
	nvkm_mask(device, oreg, mask, oval);
626 627

	nv50_disp_dptmds_war_2(disp, &outp->info);
628 629 630
}

static void
631
nv50_disp_intr_unk20_1(struct nv50_disp *disp, int head)
632
{
633
	struct nvkm_device *device = disp->base.engine.subdev.device;
634 635 636 637
	struct nvkm_devinit *devinit = device->devinit;
	u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
	if (pclk)
		nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
638 639 640
}

static void
641
nv50_disp_intr_unk20_0(struct nv50_disp *disp, int head)
642
{
643 644
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_output *outp = exec_script(disp, head, 2);
645

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	/* 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) {
		struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
		struct nvbios_init init = {
			.subdev = subdev,
			.bios = subdev->device->bios,
			.outp = &outp->info,
			.crtc = head,
			.offset = outpdp->info.script[4],
			.execute = 1,
		};
669

670
		atomic_set(&outpdp->lt.done, 0);
671
		nvbios_exec(&init);
672 673 674 675 676 677 678
	}
}

static void
nv50_disp_intr_unk10_0(struct nv50_disp *disp, int head)
{
	exec_script(disp, head, 1);
679 680
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
void
nv50_disp_super_1(struct nv50_disp *disp)
{
	struct nvkm_head *head;
	struct nvkm_ior *ior;

	list_for_each_entry(head, &disp->base.head, head) {
		head->func->state(head, &head->arm);
		head->func->state(head, &head->asy);
	}

	list_for_each_entry(ior, &disp->base.ior, head) {
		ior->func->state(ior, &ior->arm);
		ior->func->state(ior, &ior->asy);
	}
}

698
void
699
nv50_disp_super(struct work_struct *work)
700
{
B
Ben Skeggs 已提交
701 702
	struct nv50_disp *disp =
		container_of(work, struct nv50_disp, supervisor);
703 704
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
705
	struct nvkm_head *head;
706
	u32 super = nvkm_rd32(device, 0x610030);
707

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

B
Ben Skeggs 已提交
710
	if (disp->super & 0x00000010) {
711
		nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
712
		nv50_disp_super_1(disp);
713 714
		list_for_each_entry(head, &disp->base.head, head) {
			if (!(super & (0x00000020 << head->id)))
715
				continue;
716
			if (!(super & (0x00000080 << head->id)))
717
				continue;
718
			nv50_disp_intr_unk10_0(disp, head->id);
719 720
		}
	} else
B
Ben Skeggs 已提交
721
	if (disp->super & 0x00000020) {
722 723
		list_for_each_entry(head, &disp->base.head, head) {
			if (!(super & (0x00000080 << head->id)))
724
				continue;
725
			nv50_disp_intr_unk20_0(disp, head->id);
726
		}
727 728
		list_for_each_entry(head, &disp->base.head, head) {
			if (!(super & (0x00000200 << head->id)))
729
				continue;
730
			nv50_disp_intr_unk20_1(disp, head->id);
731
		}
732 733
		list_for_each_entry(head, &disp->base.head, head) {
			if (!(super & (0x00000080 << head->id)))
734
				continue;
735
			nv50_disp_intr_unk20_2(disp, head->id);
736 737
		}
	} else
B
Ben Skeggs 已提交
738
	if (disp->super & 0x00000040) {
739 740
		list_for_each_entry(head, &disp->base.head, head) {
			if (!(super & (0x00000080 << head->id)))
741
				continue;
742
			nv50_disp_intr_unk40_0(disp, head->id);
743
		}
744
		nv50_disp_update_sppll1(disp);
745 746
	}

747
	nvkm_wr32(device, 0x610030, 0x80000000);
748 749
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
static const struct nvkm_enum
nv50_disp_intr_error_type[] = {
	{ 3, "ILLEGAL_MTHD" },
	{ 4, "INVALID_VALUE" },
	{ 5, "INVALID_STATE" },
	{ 7, "INVALID_HANDLE" },
	{}
};

static const struct nvkm_enum
nv50_disp_intr_error_code[] = {
	{ 0x00, "" },
	{}
};

static void
nv50_disp_intr_error(struct nv50_disp *disp, int chid)
{
	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
	struct nvkm_device *device = subdev->device;
	u32 data = nvkm_rd32(device, 0x610084 + (chid * 0x08));
	u32 addr = nvkm_rd32(device, 0x610080 + (chid * 0x08));
	u32 code = (addr & 0x00ff0000) >> 16;
	u32 type = (addr & 0x00007000) >> 12;
	u32 mthd = (addr & 0x00000ffc);
	const struct nvkm_enum *ec, *et;

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

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

	if (chid < ARRAY_SIZE(disp->chan)) {
		switch (mthd) {
		case 0x0080:
			nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
			break;
		default:
			break;
		}
	}

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

799
void
800
nv50_disp_intr(struct nv50_disp *disp)
801
{
802 803 804
	struct nvkm_device *device = disp->base.engine.subdev.device;
	u32 intr0 = nvkm_rd32(device, 0x610020);
	u32 intr1 = nvkm_rd32(device, 0x610024);
805

806 807
	while (intr0 & 0x001f0000) {
		u32 chid = __ffs(intr0 & 0x001f0000) - 16;
B
Ben Skeggs 已提交
808
		nv50_disp_intr_error(disp, chid);
809
		intr0 &= ~(0x00010000 << chid);
810 811
	}

812 813
	while (intr0 & 0x0000001f) {
		u32 chid = __ffs(intr0 & 0x0000001f);
B
Ben Skeggs 已提交
814
		nv50_disp_chan_uevent_send(disp, chid);
815 816 817
		intr0 &= ~(0x00000001 << chid);
	}

818
	if (intr1 & 0x00000004) {
B
Ben Skeggs 已提交
819
		nvkm_disp_vblank(&disp->base, 0);
820
		nvkm_wr32(device, 0x610024, 0x00000004);
821 822
	}

823
	if (intr1 & 0x00000008) {
B
Ben Skeggs 已提交
824
		nvkm_disp_vblank(&disp->base, 1);
825
		nvkm_wr32(device, 0x610024, 0x00000008);
826 827
	}

828
	if (intr1 & 0x00000070) {
B
Ben Skeggs 已提交
829
		disp->super = (intr1 & 0x00000070);
830
		queue_work(disp->wq, &disp->supervisor);
831
		nvkm_wr32(device, 0x610024, disp->super);
832
	}
833 834
}

835
static const struct nv50_disp_func
836
nv50_disp = {
837 838
	.intr = nv50_disp_intr,
	.uevent = &nv50_disp_chan_uevent,
839
	.super = nv50_disp_super,
840
	.root = &nv50_disp_root_oclass,
841
	.head.new = nv50_head_new,
842 843 844 845 846
	.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,
847
	.dac = { .nr = 3, .new = nv50_dac_new },
848 849
	.sor = { .nr = 2, .new = nv50_sor_new },
	.pior = { .nr = 3, .new = nv50_pior_new },
850 851
};

852 853
int
nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
854
{
855
	return nv50_disp_new_(&nv50_disp, device, index, 2, pdisp);
856
}