nv50.c 53.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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
 */

25
#include <core/object.h>
26
#include <core/client.h>
27 28
#include <core/parent.h>
#include <core/handle.h>
29
#include <core/enum.h>
30 31
#include <nvif/unpack.h>
#include <nvif/class.h>
32
#include <nvif/event.h>
33

34 35 36 37 38
#include <subdev/bios.h>
#include <subdev/bios/dcb.h>
#include <subdev/bios/disp.h>
#include <subdev/bios/init.h>
#include <subdev/bios/pll.h>
39
#include <subdev/devinit.h>
40
#include <subdev/timer.h>
41
#include <subdev/fb.h>
42

43 44 45
#include "nv50.h"

/*******************************************************************************
46
 * EVO channel base class
47 48
 ******************************************************************************/

49
static int
50 51
nv50_disp_chan_create_(struct nouveau_object *parent,
		       struct nouveau_object *engine,
52
		       struct nouveau_oclass *oclass, int head,
53 54
		       int length, void **pobject)
{
55
	const struct nv50_disp_chan_impl *impl = (void *)oclass->ofuncs;
56 57
	struct nv50_disp_base *base = (void *)parent;
	struct nv50_disp_chan *chan;
58
	int chid = impl->chid + head;
59 60 61 62 63 64 65 66 67 68 69 70 71
	int ret;

	if (base->chan & (1 << chid))
		return -EBUSY;
	base->chan |= (1 << chid);

	ret = nouveau_namedb_create_(parent, engine, oclass, 0, NULL,
				     (1ULL << NVDEV_ENGINE_DMAOBJ),
				     length, pobject);
	chan = *pobject;
	if (ret)
		return ret;
	chan->chid = chid;
72 73 74

	nv_parent(chan)->object_attach = impl->attach;
	nv_parent(chan)->object_detach = impl->detach;
75 76 77
	return 0;
}

78
static void
79 80 81 82 83 84 85
nv50_disp_chan_destroy(struct nv50_disp_chan *chan)
{
	struct nv50_disp_base *base = (void *)nv_object(chan)->parent;
	base->chan &= ~(1 << chan->chid);
	nouveau_namedb_destroy(&chan->base);
}

86 87 88 89 90
static void
nv50_disp_chan_uevent_fini(struct nvkm_event *event, int type, int index)
{
	struct nv50_disp_priv *priv = container_of(event, typeof(*priv), uevent);
	nv_mask(priv, 0x610028, 0x00000001 << index, 0x00000000 << index);
91
	nv_wr32(priv, 0x610020, 0x00000001 << index);
92 93 94 95 96 97
}

static void
nv50_disp_chan_uevent_init(struct nvkm_event *event, int types, int index)
{
	struct nv50_disp_priv *priv = container_of(event, typeof(*priv), uevent);
98
	nv_wr32(priv, 0x610020, 0x00000001 << index);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	nv_mask(priv, 0x610028, 0x00000001 << index, 0x00000001 << index);
}

void
nv50_disp_chan_uevent_send(struct nv50_disp_priv *priv, int chid)
{
	struct nvif_notify_uevent_rep {
	} rep;

	nvkm_event_send(&priv->uevent, 1, chid, &rep, sizeof(rep));
}

int
nv50_disp_chan_uevent_ctor(struct nouveau_object *object, void *data, u32 size,
			   struct nvkm_notify *notify)
{
	struct nv50_disp_dmac *dmac = (void *)object;
	union {
		struct nvif_notify_uevent_req none;
	} *args = data;
	int ret;

	if (nvif_unvers(args->none)) {
		notify->size  = sizeof(struct nvif_notify_uevent_rep);
		notify->types = 1;
		notify->index = dmac->base.chid;
		return 0;
	}

	return ret;
}

const struct nvkm_event_func
nv50_disp_chan_uevent = {
	.ctor = nv50_disp_chan_uevent_ctor,
	.init = nv50_disp_chan_uevent_init,
	.fini = nv50_disp_chan_uevent_fini,
};

int
nv50_disp_chan_ntfy(struct nouveau_object *object, u32 type,
		    struct nvkm_event **pevent)
{
	struct nv50_disp_priv *priv = (void *)object->engine;
	switch (type) {
	case NV50_DISP_CORE_CHANNEL_DMA_V0_NTFY_UEVENT:
		*pevent = &priv->uevent;
		return 0;
	default:
		break;
	}
	return -EINVAL;
}

153 154 155 156 157 158 159 160 161 162
int
nv50_disp_chan_map(struct nouveau_object *object, u64 *addr, u32 *size)
{
	struct nv50_disp_chan *chan = (void *)object;
	*addr = nv_device_resource_start(nv_device(object), 0) +
		0x640000 + (chan->chid * 0x1000);
	*size = 0x001000;
	return 0;
}

163
u32
164 165
nv50_disp_chan_rd32(struct nouveau_object *object, u64 addr)
{
166 167 168
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_chan *chan = (void *)object;
	return nv_rd32(priv, 0x640000 + (chan->chid * 0x1000) + addr);
169 170
}

171
void
172 173
nv50_disp_chan_wr32(struct nouveau_object *object, u64 addr, u32 data)
{
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_chan *chan = (void *)object;
	nv_wr32(priv, 0x640000 + (chan->chid * 0x1000) + addr, data);
}

/*******************************************************************************
 * EVO DMA channel base class
 ******************************************************************************/

static int
nv50_disp_dmac_object_attach(struct nouveau_object *parent,
			     struct nouveau_object *object, u32 name)
{
	struct nv50_disp_base *base = (void *)parent->parent;
	struct nv50_disp_chan *chan = (void *)parent;
	u32 addr = nv_gpuobj(object)->node->offset;
	u32 chid = chan->chid;
	u32 data = (chid << 28) | (addr << 10) | chid;
	return nouveau_ramht_insert(base->ramht, chid, name, data);
}

static void
nv50_disp_dmac_object_detach(struct nouveau_object *parent, int cookie)
{
	struct nv50_disp_base *base = (void *)parent->parent;
	nouveau_ramht_remove(base->ramht, cookie);
}

202
static int
203 204
nv50_disp_dmac_create_(struct nouveau_object *parent,
		       struct nouveau_object *engine,
205
		       struct nouveau_oclass *oclass, u32 pushbuf, int head,
206 207 208 209 210
		       int length, void **pobject)
{
	struct nv50_disp_dmac *dmac;
	int ret;

211
	ret = nv50_disp_chan_create_(parent, engine, oclass, head,
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
				     length, pobject);
	dmac = *pobject;
	if (ret)
		return ret;

	dmac->pushdma = (void *)nouveau_handle_ref(parent, pushbuf);
	if (!dmac->pushdma)
		return -ENOENT;

	switch (nv_mclass(dmac->pushdma)) {
	case 0x0002:
	case 0x003d:
		if (dmac->pushdma->limit - dmac->pushdma->start != 0xfff)
			return -EINVAL;

		switch (dmac->pushdma->target) {
		case NV_MEM_TARGET_VRAM:
			dmac->push = 0x00000000 | dmac->pushdma->start >> 8;
			break;
231 232 233
		case NV_MEM_TARGET_PCI_NOSNOOP:
			dmac->push = 0x00000003 | dmac->pushdma->start >> 8;
			break;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		default:
			return -EINVAL;
		}
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

void
nv50_disp_dmac_dtor(struct nouveau_object *object)
{
	struct nv50_disp_dmac *dmac = (void *)object;
	nouveau_object_ref(NULL, (struct nouveau_object **)&dmac->pushdma);
	nv50_disp_chan_destroy(&dmac->base);
}

static int
nv50_disp_dmac_init(struct nouveau_object *object)
{
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_dmac *dmac = (void *)object;
	int chid = dmac->base.chid;
	int ret;

	ret = nv50_disp_chan_init(&dmac->base);
	if (ret)
		return ret;

	/* enable error reporting */
266
	nv_mask(priv, 0x610028, 0x00010000 << chid, 0x00010000 << chid);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	/* initialise channel for dma command submission */
	nv_wr32(priv, 0x610204 + (chid * 0x0010), dmac->push);
	nv_wr32(priv, 0x610208 + (chid * 0x0010), 0x00010000);
	nv_wr32(priv, 0x61020c + (chid * 0x0010), chid);
	nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00000010, 0x00000010);
	nv_wr32(priv, 0x640000 + (chid * 0x1000), 0x00000000);
	nv_wr32(priv, 0x610200 + (chid * 0x0010), 0x00000013);

	/* wait for it to go inactive */
	if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x80000000, 0x00000000)) {
		nv_error(dmac, "init timeout, 0x%08x\n",
			 nv_rd32(priv, 0x610200 + (chid * 0x10)));
		return -EBUSY;
	}

	return 0;
}

static int
nv50_disp_dmac_fini(struct nouveau_object *object, bool suspend)
{
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_dmac *dmac = (void *)object;
	int chid = dmac->base.chid;

	/* deactivate channel */
	nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00001010, 0x00001000);
	nv_mask(priv, 0x610200 + (chid * 0x0010), 0x00000003, 0x00000000);
	if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x001e0000, 0x00000000)) {
		nv_error(dmac, "fini timeout, 0x%08x\n",
			 nv_rd32(priv, 0x610200 + (chid * 0x10)));
		if (suspend)
			return -EBUSY;
	}

303
	/* disable error reporting and completion notifications */
304 305 306
	nv_mask(priv, 0x610028, 0x00010001 << chid, 0x00000000 << chid);

	return nv50_disp_chan_fini(&dmac->base, suspend);
307 308 309 310 311 312
}

/*******************************************************************************
 * EVO master channel object
 ******************************************************************************/

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
static void
nv50_disp_mthd_list(struct nv50_disp_priv *priv, int debug, u32 base, int c,
		    const struct nv50_disp_mthd_list *list, int inst)
{
	struct nouveau_object *disp = nv_object(priv);
	int i;

	for (i = 0; list->data[i].mthd; i++) {
		if (list->data[i].addr) {
			u32 next = nv_rd32(priv, list->data[i].addr + base + 0);
			u32 prev = nv_rd32(priv, list->data[i].addr + base + c);
			u32 mthd = list->data[i].mthd + (list->mthd * inst);
			const char *name = list->data[i].name;
			char mods[16];

			if (prev != next)
				snprintf(mods, sizeof(mods), "-> 0x%08x", next);
			else
				snprintf(mods, sizeof(mods), "%13c", ' ');

			nv_printk_(disp, debug, "\t0x%04x: 0x%08x %s%s%s\n",
				   mthd, prev, mods, name ? " // " : "",
				   name ? name : "");
		}
	}
}

void
nv50_disp_mthd_chan(struct nv50_disp_priv *priv, int debug, int head,
		    const struct nv50_disp_mthd_chan *chan)
{
	struct nouveau_object *disp = nv_object(priv);
	const struct nv50_disp_impl *impl = (void *)disp->oclass;
	const struct nv50_disp_mthd_list *list;
	int i, j;

	if (debug > nv_subdev(priv)->debug)
		return;

	for (i = 0; (list = chan->data[i].mthd) != NULL; i++) {
		u32 base = head * chan->addr;
		for (j = 0; j < chan->data[i].nr; j++, base += list->addr) {
			const char *cname = chan->name;
			const char *sname = "";
			char cname_[16], sname_[16];

			if (chan->addr) {
				snprintf(cname_, sizeof(cname_), "%s %d",
					 chan->name, head);
				cname = cname_;
			}

			if (chan->data[i].nr > 1) {
				snprintf(sname_, sizeof(sname_), " - %s %d",
					 chan->data[i].name, j);
				sname = sname_;
			}

			nv_printk_(disp, debug, "%s%s:\n", cname, sname);
			nv50_disp_mthd_list(priv, debug, base, impl->mthd.prev,
					    list, j);
		}
	}
}

const struct nv50_disp_mthd_list
379
nv50_disp_core_mthd_base = {
380 381 382 383 384 385 386 387 388 389 390 391
	.mthd = 0x0000,
	.addr = 0x000000,
	.data = {
		{ 0x0080, 0x000000 },
		{ 0x0084, 0x610bb8 },
		{ 0x0088, 0x610b9c },
		{ 0x008c, 0x000000 },
		{}
	}
};

static const struct nv50_disp_mthd_list
392
nv50_disp_core_mthd_dac = {
393 394 395 396 397 398 399 400 401 402 403
	.mthd = 0x0080,
	.addr = 0x000008,
	.data = {
		{ 0x0400, 0x610b58 },
		{ 0x0404, 0x610bdc },
		{ 0x0420, 0x610828 },
		{}
	}
};

const struct nv50_disp_mthd_list
404
nv50_disp_core_mthd_sor = {
405 406 407 408 409 410 411 412 413
	.mthd = 0x0040,
	.addr = 0x000008,
	.data = {
		{ 0x0600, 0x610b70 },
		{}
	}
};

const struct nv50_disp_mthd_list
414
nv50_disp_core_mthd_pior = {
415 416 417 418 419 420 421 422 423
	.mthd = 0x0040,
	.addr = 0x000008,
	.data = {
		{ 0x0700, 0x610b80 },
		{}
	}
};

static const struct nv50_disp_mthd_list
424
nv50_disp_core_mthd_head = {
425 426 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
	.mthd = 0x0400,
	.addr = 0x000540,
	.data = {
		{ 0x0800, 0x610ad8 },
		{ 0x0804, 0x610ad0 },
		{ 0x0808, 0x610a48 },
		{ 0x080c, 0x610a78 },
		{ 0x0810, 0x610ac0 },
		{ 0x0814, 0x610af8 },
		{ 0x0818, 0x610b00 },
		{ 0x081c, 0x610ae8 },
		{ 0x0820, 0x610af0 },
		{ 0x0824, 0x610b08 },
		{ 0x0828, 0x610b10 },
		{ 0x082c, 0x610a68 },
		{ 0x0830, 0x610a60 },
		{ 0x0834, 0x000000 },
		{ 0x0838, 0x610a40 },
		{ 0x0840, 0x610a24 },
		{ 0x0844, 0x610a2c },
		{ 0x0848, 0x610aa8 },
		{ 0x084c, 0x610ab0 },
		{ 0x0860, 0x610a84 },
		{ 0x0864, 0x610a90 },
		{ 0x0868, 0x610b18 },
		{ 0x086c, 0x610b20 },
		{ 0x0870, 0x610ac8 },
		{ 0x0874, 0x610a38 },
		{ 0x0880, 0x610a58 },
		{ 0x0884, 0x610a9c },
		{ 0x08a0, 0x610a70 },
		{ 0x08a4, 0x610a50 },
		{ 0x08a8, 0x610ae0 },
		{ 0x08c0, 0x610b28 },
		{ 0x08c4, 0x610b30 },
		{ 0x08c8, 0x610b40 },
		{ 0x08d4, 0x610b38 },
		{ 0x08d8, 0x610b48 },
		{ 0x08dc, 0x610b50 },
		{ 0x0900, 0x610a18 },
		{ 0x0904, 0x610ab8 },
		{}
	}
};

static const struct nv50_disp_mthd_chan
471
nv50_disp_core_mthd_chan = {
472 473 474
	.name = "Core",
	.addr = 0x000000,
	.data = {
475 476 477 478 479
		{ "Global", 1, &nv50_disp_core_mthd_base },
		{    "DAC", 3, &nv50_disp_core_mthd_dac  },
		{    "SOR", 2, &nv50_disp_core_mthd_sor  },
		{   "PIOR", 3, &nv50_disp_core_mthd_pior },
		{   "HEAD", 2, &nv50_disp_core_mthd_head },
480 481 482 483
		{}
	}
};

484
int
485
nv50_disp_core_ctor(struct nouveau_object *parent,
486 487 488 489
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
{
490 491 492
	union {
		struct nv50_disp_core_channel_dma_v0 v0;
	} *args = data;
493
	struct nv50_disp_dmac *mast;
494 495
	int ret;

496 497 498 499 500 501 502
	nv_ioctl(parent, "create disp core channel dma size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create disp core channel dma vers %d "
				 "pushbuf %08x\n",
			 args->v0.version, args->v0.pushbuf);
	} else
		return ret;
503

504
	ret = nv50_disp_dmac_create_(parent, engine, oclass, args->v0.pushbuf,
505 506
				     0, sizeof(*mast), (void **)&mast);
	*pobject = nv_object(mast);
507 508 509 510 511 512 513
	if (ret)
		return ret;

	return 0;
}

static int
514
nv50_disp_core_init(struct nouveau_object *object)
515
{
516 517
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_dmac *mast = (void *)object;
518 519
	int ret;

520
	ret = nv50_disp_chan_init(&mast->base);
521 522 523
	if (ret)
		return ret;

524
	/* enable error reporting */
525
	nv_mask(priv, 0x610028, 0x00010000, 0x00010000);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

	/* attempt to unstick channel from some unknown state */
	if ((nv_rd32(priv, 0x610200) & 0x009f0000) == 0x00020000)
		nv_mask(priv, 0x610200, 0x00800000, 0x00800000);
	if ((nv_rd32(priv, 0x610200) & 0x003f0000) == 0x00030000)
		nv_mask(priv, 0x610200, 0x00600000, 0x00600000);

	/* initialise channel for dma command submission */
	nv_wr32(priv, 0x610204, mast->push);
	nv_wr32(priv, 0x610208, 0x00010000);
	nv_wr32(priv, 0x61020c, 0x00000000);
	nv_mask(priv, 0x610200, 0x00000010, 0x00000010);
	nv_wr32(priv, 0x640000, 0x00000000);
	nv_wr32(priv, 0x610200, 0x01000013);

	/* wait for it to go inactive */
	if (!nv_wait(priv, 0x610200, 0x80000000, 0x00000000)) {
		nv_error(mast, "init: 0x%08x\n", nv_rd32(priv, 0x610200));
		return -EBUSY;
	}

547 548 549 550
	return 0;
}

static int
551
nv50_disp_core_fini(struct nouveau_object *object, bool suspend)
552
{
553 554 555 556 557 558 559 560 561 562 563 564
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_dmac *mast = (void *)object;

	/* deactivate channel */
	nv_mask(priv, 0x610200, 0x00000010, 0x00000000);
	nv_mask(priv, 0x610200, 0x00000003, 0x00000000);
	if (!nv_wait(priv, 0x610200, 0x001e0000, 0x00000000)) {
		nv_error(mast, "fini: 0x%08x\n", nv_rd32(priv, 0x610200));
		if (suspend)
			return -EBUSY;
	}

565
	/* disable error reporting and completion notifications */
566 567 568
	nv_mask(priv, 0x610028, 0x00010001, 0x00000000);

	return nv50_disp_chan_fini(&mast->base, suspend);
569 570
}

571
struct nv50_disp_chan_impl
572 573
nv50_disp_core_ofuncs = {
	.base.ctor = nv50_disp_core_ctor,
574
	.base.dtor = nv50_disp_dmac_dtor,
575 576
	.base.init = nv50_disp_core_init,
	.base.fini = nv50_disp_core_fini,
577
	.base.map  = nv50_disp_chan_map,
578
	.base.ntfy = nv50_disp_chan_ntfy,
579 580 581 582 583
	.base.rd32 = nv50_disp_chan_rd32,
	.base.wr32 = nv50_disp_chan_wr32,
	.chid = 0,
	.attach = nv50_disp_dmac_object_attach,
	.detach = nv50_disp_dmac_object_detach,
584 585 586
};

/*******************************************************************************
587
 * EVO sync channel objects
588 589
 ******************************************************************************/

590
static const struct nv50_disp_mthd_list
591
nv50_disp_base_mthd_base = {
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	.mthd = 0x0000,
	.addr = 0x000000,
	.data = {
		{ 0x0080, 0x000000 },
		{ 0x0084, 0x0008c4 },
		{ 0x0088, 0x0008d0 },
		{ 0x008c, 0x0008dc },
		{ 0x0090, 0x0008e4 },
		{ 0x0094, 0x610884 },
		{ 0x00a0, 0x6108a0 },
		{ 0x00a4, 0x610878 },
		{ 0x00c0, 0x61086c },
		{ 0x00e0, 0x610858 },
		{ 0x00e4, 0x610860 },
		{ 0x00e8, 0x6108ac },
		{ 0x00ec, 0x6108b4 },
		{ 0x0100, 0x610894 },
		{ 0x0110, 0x6108bc },
		{ 0x0114, 0x61088c },
		{}
	}
};

const struct nv50_disp_mthd_list
616
nv50_disp_base_mthd_image = {
617 618 619 620 621 622 623 624 625 626 627 628 629
	.mthd = 0x0400,
	.addr = 0x000000,
	.data = {
		{ 0x0800, 0x6108f0 },
		{ 0x0804, 0x6108fc },
		{ 0x0808, 0x61090c },
		{ 0x080c, 0x610914 },
		{ 0x0810, 0x610904 },
		{}
	}
};

static const struct nv50_disp_mthd_chan
630
nv50_disp_base_mthd_chan = {
631 632 633
	.name = "Base",
	.addr = 0x000540,
	.data = {
634 635
		{ "Global", 1, &nv50_disp_base_mthd_base },
		{  "Image", 2, &nv50_disp_base_mthd_image },
636 637 638 639
		{}
	}
};

640
int
641
nv50_disp_base_ctor(struct nouveau_object *parent,
642 643 644 645
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
{
646 647 648
	union {
		struct nv50_disp_base_channel_dma_v0 v0;
	} *args = data;
649
	struct nv50_disp_priv *priv = (void *)engine;
650
	struct nv50_disp_dmac *dmac;
651 652
	int ret;

653 654 655 656 657 658 659 660 661
	nv_ioctl(parent, "create disp base channel dma size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create disp base channel dma vers %d "
				 "pushbuf %08x head %d\n",
			 args->v0.version, args->v0.pushbuf, args->v0.head);
		if (args->v0.head > priv->head.nr)
			return -EINVAL;
	} else
		return ret;
662

663 664 665
	ret = nv50_disp_dmac_create_(parent, engine, oclass, args->v0.pushbuf,
				     args->v0.head, sizeof(*dmac),
				     (void **)&dmac);
666
	*pobject = nv_object(dmac);
667 668 669 670 671 672
	if (ret)
		return ret;

	return 0;
}

673
struct nv50_disp_chan_impl
674 675
nv50_disp_base_ofuncs = {
	.base.ctor = nv50_disp_base_ctor,
676 677 678
	.base.dtor = nv50_disp_dmac_dtor,
	.base.init = nv50_disp_dmac_init,
	.base.fini = nv50_disp_dmac_fini,
679
	.base.ntfy = nv50_disp_chan_ntfy,
680
	.base.map  = nv50_disp_chan_map,
681 682 683 684 685
	.base.rd32 = nv50_disp_chan_rd32,
	.base.wr32 = nv50_disp_chan_wr32,
	.chid = 1,
	.attach = nv50_disp_dmac_object_attach,
	.detach = nv50_disp_dmac_object_detach,
686 687 688 689 690
};

/*******************************************************************************
 * EVO overlay channel objects
 ******************************************************************************/
691

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
const struct nv50_disp_mthd_list
nv50_disp_ovly_mthd_base = {
	.mthd = 0x0000,
	.addr = 0x000000,
	.data = {
		{ 0x0080, 0x000000 },
		{ 0x0084, 0x0009a0 },
		{ 0x0088, 0x0009c0 },
		{ 0x008c, 0x0009c8 },
		{ 0x0090, 0x6109b4 },
		{ 0x0094, 0x610970 },
		{ 0x00a0, 0x610998 },
		{ 0x00a4, 0x610964 },
		{ 0x00c0, 0x610958 },
		{ 0x00e0, 0x6109a8 },
		{ 0x00e4, 0x6109d0 },
		{ 0x00e8, 0x6109d8 },
		{ 0x0100, 0x61094c },
		{ 0x0104, 0x610984 },
		{ 0x0108, 0x61098c },
		{ 0x0800, 0x6109f8 },
		{ 0x0808, 0x610a08 },
		{ 0x080c, 0x610a10 },
		{ 0x0810, 0x610a00 },
		{}
	}
};

static const struct nv50_disp_mthd_chan
nv50_disp_ovly_mthd_chan = {
	.name = "Overlay",
	.addr = 0x000540,
	.data = {
		{ "Global", 1, &nv50_disp_ovly_mthd_base },
		{}
	}
};

730
int
731 732 733 734
nv50_disp_ovly_ctor(struct nouveau_object *parent,
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
735
{
736 737 738
	union {
		struct nv50_disp_overlay_channel_dma_v0 v0;
	} *args = data;
739
	struct nv50_disp_priv *priv = (void *)engine;
740
	struct nv50_disp_dmac *dmac;
741 742
	int ret;

743 744 745 746 747 748 749 750 751
	nv_ioctl(parent, "create disp overlay channel dma size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create disp overlay channel dma vers %d "
				 "pushbuf %08x head %d\n",
			 args->v0.version, args->v0.pushbuf, args->v0.head);
		if (args->v0.head > priv->head.nr)
			return -EINVAL;
	} else
		return ret;
752

753 754 755
	ret = nv50_disp_dmac_create_(parent, engine, oclass, args->v0.pushbuf,
				     args->v0.head, sizeof(*dmac),
				     (void **)&dmac);
756
	*pobject = nv_object(dmac);
757 758 759 760 761 762
	if (ret)
		return ret;

	return 0;
}

763
struct nv50_disp_chan_impl
764
nv50_disp_ovly_ofuncs = {
765 766 767 768
	.base.ctor = nv50_disp_ovly_ctor,
	.base.dtor = nv50_disp_dmac_dtor,
	.base.init = nv50_disp_dmac_init,
	.base.fini = nv50_disp_dmac_fini,
769
	.base.ntfy = nv50_disp_chan_ntfy,
770
	.base.map  = nv50_disp_chan_map,
771 772 773 774 775
	.base.rd32 = nv50_disp_chan_rd32,
	.base.wr32 = nv50_disp_chan_wr32,
	.chid = 3,
	.attach = nv50_disp_dmac_object_attach,
	.detach = nv50_disp_dmac_object_detach,
776 777 778
};

/*******************************************************************************
779
 * EVO PIO channel base class
780 781 782
 ******************************************************************************/

static int
783 784
nv50_disp_pioc_create_(struct nouveau_object *parent,
		       struct nouveau_object *engine,
785
		       struct nouveau_oclass *oclass, int head,
786
		       int length, void **pobject)
787
{
788
	return nv50_disp_chan_create_(parent, engine, oclass, head,
789 790 791
				      length, pobject);
}

792
void
793 794 795 796 797 798 799 800 801 802 803 804
nv50_disp_pioc_dtor(struct nouveau_object *object)
{
	struct nv50_disp_pioc *pioc = (void *)object;
	nv50_disp_chan_destroy(&pioc->base);
}

static int
nv50_disp_pioc_init(struct nouveau_object *object)
{
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_pioc *pioc = (void *)object;
	int chid = pioc->base.chid;
805 806
	int ret;

807
	ret = nv50_disp_chan_init(&pioc->base);
808 809 810
	if (ret)
		return ret;

811 812 813 814 815 816 817 818 819 820 821 822 823 824
	nv_wr32(priv, 0x610200 + (chid * 0x10), 0x00002000);
	if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00000000, 0x00000000)) {
		nv_error(pioc, "timeout0: 0x%08x\n",
			 nv_rd32(priv, 0x610200 + (chid * 0x10)));
		return -EBUSY;
	}

	nv_wr32(priv, 0x610200 + (chid * 0x10), 0x00000001);
	if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00030000, 0x00010000)) {
		nv_error(pioc, "timeout1: 0x%08x\n",
			 nv_rd32(priv, 0x610200 + (chid * 0x10)));
		return -EBUSY;
	}

825 826 827
	return 0;
}

828 829
static int
nv50_disp_pioc_fini(struct nouveau_object *object, bool suspend)
830
{
831 832 833 834 835 836 837 838 839 840 841 842 843
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nv50_disp_pioc *pioc = (void *)object;
	int chid = pioc->base.chid;

	nv_mask(priv, 0x610200 + (chid * 0x10), 0x00000001, 0x00000000);
	if (!nv_wait(priv, 0x610200 + (chid * 0x10), 0x00030000, 0x00000000)) {
		nv_error(pioc, "timeout: 0x%08x\n",
			 nv_rd32(priv, 0x610200 + (chid * 0x10)));
		if (suspend)
			return -EBUSY;
	}

	return nv50_disp_chan_fini(&pioc->base, suspend);
844 845
}

846 847 848 849
/*******************************************************************************
 * EVO immediate overlay channel objects
 ******************************************************************************/

850
int
851 852 853 854
nv50_disp_oimm_ctor(struct nouveau_object *parent,
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
855
{
856 857 858
	union {
		struct nv50_disp_overlay_v0 v0;
	} *args = data;
859
	struct nv50_disp_priv *priv = (void *)engine;
860
	struct nv50_disp_pioc *pioc;
861 862
	int ret;

863 864 865 866 867 868 869 870
	nv_ioctl(parent, "create disp overlay size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create disp overlay vers %d head %d\n",
			 args->v0.version, args->v0.head);
		if (args->v0.head > priv->head.nr)
			return -EINVAL;
	} else
		return ret;
871

872
	ret = nv50_disp_pioc_create_(parent, engine, oclass, args->v0.head,
873 874
				     sizeof(*pioc), (void **)&pioc);
	*pobject = nv_object(pioc);
875 876 877 878 879 880
	if (ret)
		return ret;

	return 0;
}

881
struct nv50_disp_chan_impl
882
nv50_disp_oimm_ofuncs = {
883 884 885 886
	.base.ctor = nv50_disp_oimm_ctor,
	.base.dtor = nv50_disp_pioc_dtor,
	.base.init = nv50_disp_pioc_init,
	.base.fini = nv50_disp_pioc_fini,
887
	.base.ntfy = nv50_disp_chan_ntfy,
888
	.base.map  = nv50_disp_chan_map,
889 890 891
	.base.rd32 = nv50_disp_chan_rd32,
	.base.wr32 = nv50_disp_chan_wr32,
	.chid = 5,
892 893 894 895 896 897
};

/*******************************************************************************
 * EVO cursor channel objects
 ******************************************************************************/

898
int
899 900 901 902
nv50_disp_curs_ctor(struct nouveau_object *parent,
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
903
{
904 905 906
	union {
		struct nv50_disp_cursor_v0 v0;
	} *args = data;
907
	struct nv50_disp_priv *priv = (void *)engine;
908 909 910
	struct nv50_disp_pioc *pioc;
	int ret;

911 912 913 914 915 916 917 918
	nv_ioctl(parent, "create disp cursor size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(parent, "create disp cursor vers %d head %d\n",
			 args->v0.version, args->v0.head);
		if (args->v0.head > priv->head.nr)
			return -EINVAL;
	} else
		return ret;
919

920
	ret = nv50_disp_pioc_create_(parent, engine, oclass, args->v0.head,
921 922 923 924 925 926
				     sizeof(*pioc), (void **)&pioc);
	*pobject = nv_object(pioc);
	if (ret)
		return ret;

	return 0;
927 928
}

929
struct nv50_disp_chan_impl
930
nv50_disp_curs_ofuncs = {
931 932 933 934
	.base.ctor = nv50_disp_curs_ctor,
	.base.dtor = nv50_disp_pioc_dtor,
	.base.init = nv50_disp_pioc_init,
	.base.fini = nv50_disp_pioc_fini,
935
	.base.ntfy = nv50_disp_chan_ntfy,
936
	.base.map  = nv50_disp_chan_map,
937 938 939
	.base.rd32 = nv50_disp_chan_rd32,
	.base.wr32 = nv50_disp_chan_wr32,
	.chid = 7,
940 941 942 943 944 945
};

/*******************************************************************************
 * Base display object
 ******************************************************************************/

946
int
947
nv50_disp_main_scanoutpos(NV50_DISP_MTHD_V0)
948
{
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	const u32 blanke = nv_rd32(priv, 0x610aec + (head * 0x540));
	const u32 blanks = nv_rd32(priv, 0x610af4 + (head * 0x540));
	const u32 total  = nv_rd32(priv, 0x610afc + (head * 0x540));
	union {
		struct nv04_disp_scanoutpos_v0 v0;
	} *args = data;
	int ret;

	nv_ioctl(object, "disp scanoutpos size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, false)) {
		nv_ioctl(object, "disp scanoutpos vers %d\n", args->v0.version);
		args->v0.vblanke = (blanke & 0xffff0000) >> 16;
		args->v0.hblanke = (blanke & 0x0000ffff);
		args->v0.vblanks = (blanks & 0xffff0000) >> 16;
		args->v0.hblanks = (blanks & 0x0000ffff);
		args->v0.vtotal  = ( total & 0xffff0000) >> 16;
		args->v0.htotal  = ( total & 0x0000ffff);
		args->v0.time[0] = ktime_to_ns(ktime_get());
		args->v0.vline = /* vline read locks hline */
			nv_rd32(priv, 0x616340 + (head * 0x800)) & 0xffff;
		args->v0.time[1] = ktime_to_ns(ktime_get());
		args->v0.hline =
			nv_rd32(priv, 0x616344 + (head * 0x800)) & 0xffff;
	} else
		return ret;
974 975 976 977

	return 0;
}

978
int
979
nv50_disp_main_mthd(struct nouveau_object *object, u32 mthd,
980 981
		    void *data, u32 size)
{
982
	const struct nv50_disp_impl *impl = (void *)nv_oclass(object->engine);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	union {
		struct nv50_disp_mthd_v0 v0;
		struct nv50_disp_mthd_v1 v1;
	} *args = data;
	struct nv50_disp_priv *priv = (void *)object->engine;
	struct nvkm_output *outp = NULL;
	struct nvkm_output *temp;
	u16 type, mask = 0;
	int head, ret;

	if (mthd != NV50_DISP_MTHD)
		return -EINVAL;

	nv_ioctl(object, "disp mthd size %d\n", size);
	if (nvif_unpack(args->v0, 0, 0, true)) {
		nv_ioctl(object, "disp mthd vers %d mthd %02x head %d\n",
			 args->v0.version, args->v0.method, args->v0.head);
		mthd = args->v0.method;
		head = args->v0.head;
	} else
	if (nvif_unpack(args->v1, 1, 1, true)) {
		nv_ioctl(object, "disp mthd vers %d mthd %02x "
				 "type %04x mask %04x\n",
			 args->v1.version, args->v1.method,
			 args->v1.hasht, args->v1.hashm);
		mthd = args->v1.method;
		type = args->v1.hasht;
		mask = args->v1.hashm;
		head = ffs((mask >> 8) & 0x0f) - 1;
	} else
		return ret;

	if (head < 0 || head >= priv->head.nr)
		return -ENXIO;

	if (mask) {
		list_for_each_entry(temp, &priv->base.outp, head) {
			if ((temp->info.hasht         == type) &&
			    (temp->info.hashm & mask) == mask) {
				outp = temp;
				break;
			}
		}
		if (outp == NULL)
			return -ENXIO;
	}

	switch (mthd) {
1031 1032
	case NV50_DISP_SCANOUTPOS:
		return impl->head.scanoutpos(object, priv, data, size, head);
1033 1034 1035 1036 1037 1038 1039
	default:
		break;
	}

	switch (mthd * !!outp) {
	case NV50_DISP_MTHD_V1_DAC_PWR:
		return priv->dac.power(object, priv, data, size, head, outp);
1040 1041
	case NV50_DISP_MTHD_V1_DAC_LOAD:
		return priv->dac.sense(object, priv, data, size, head, outp);
1042 1043
	case NV50_DISP_MTHD_V1_SOR_PWR:
		return priv->sor.power(object, priv, data, size, head, outp);
1044 1045 1046 1047
	case NV50_DISP_MTHD_V1_SOR_HDA_ELD:
		if (!priv->sor.hda_eld)
			return -ENODEV;
		return priv->sor.hda_eld(object, priv, data, size, head, outp);
1048 1049 1050 1051
	case NV50_DISP_MTHD_V1_SOR_HDMI_PWR:
		if (!priv->sor.hdmi)
			return -ENODEV;
		return priv->sor.hdmi(object, priv, data, size, head, outp);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	case NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT: {
		union {
			struct nv50_disp_sor_lvds_script_v0 v0;
		} *args = data;
		nv_ioctl(object, "disp sor lvds script size %d\n", size);
		if (nvif_unpack(args->v0, 0, 0, false)) {
			nv_ioctl(object, "disp sor lvds script "
					 "vers %d name %04x\n",
				 args->v0.version, args->v0.script);
			priv->sor.lvdsconf = args->v0.script;
			return 0;
		} else
			return ret;
	}
		break;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	case NV50_DISP_MTHD_V1_SOR_DP_PWR: {
		struct nvkm_output_dp *outpdp = (void *)outp;
		union {
			struct nv50_disp_sor_dp_pwr_v0 v0;
		} *args = data;
		nv_ioctl(object, "disp sor dp pwr size %d\n", size);
		if (nvif_unpack(args->v0, 0, 0, false)) {
			nv_ioctl(object, "disp sor dp pwr vers %d state %d\n",
				 args->v0.version, args->v0.state);
			if (args->v0.state == 0) {
				nvkm_notify_put(&outpdp->irq);
				((struct nvkm_output_dp_impl *)nv_oclass(outp))
					->lnk_pwr(outpdp, 0);
				atomic_set(&outpdp->lt.done, 0);
				return 0;
			} else
			if (args->v0.state != 0) {
				nvkm_output_dp_train(&outpdp->base, 0, true);
				return 0;
			}
		} else
			return ret;
	}
		break;
1091 1092 1093 1094
	case NV50_DISP_MTHD_V1_PIOR_PWR:
		if (!priv->pior.power)
			return -ENODEV;
		return priv->pior.power(object, priv, data, size, head, outp);
1095 1096 1097 1098 1099 1100 1101
	default:
		break;
	}

	return -EINVAL;
}

1102
int
1103
nv50_disp_main_ctor(struct nouveau_object *parent,
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
{
	struct nv50_disp_priv *priv = (void *)engine;
	struct nv50_disp_base *base;
	int ret;

	ret = nouveau_parent_create(parent, engine, oclass, 0,
				    priv->sclass, 0, &base);
	*pobject = nv_object(base);
	if (ret)
		return ret;

1118 1119
	return nouveau_ramht_new(nv_object(base), nv_object(base), 0x1000, 0,
				&base->ramht);
1120 1121
}

1122
void
1123
nv50_disp_main_dtor(struct nouveau_object *object)
1124 1125
{
	struct nv50_disp_base *base = (void *)object;
1126
	nouveau_ramht_ref(NULL, &base->ramht);
1127 1128 1129 1130
	nouveau_parent_destroy(&base->base);
}

static int
1131
nv50_disp_main_init(struct nouveau_object *object)
1132
{
B
Ben Skeggs 已提交
1133
	struct nv50_disp_priv *priv = (void *)object->engine;
1134
	struct nv50_disp_base *base = (void *)object;
B
Ben Skeggs 已提交
1135 1136
	int ret, i;
	u32 tmp;
1137 1138 1139 1140 1141

	ret = nouveau_parent_init(&base->base);
	if (ret)
		return ret;

B
Ben Skeggs 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	/* The below segments of code copying values from one register to
	 * another appear to inform EVO of the display capabilities or
	 * something similar.  NFI what the 0x614004 caps are for..
	 */
	tmp = nv_rd32(priv, 0x614004);
	nv_wr32(priv, 0x610184, tmp);

	/* ... CRTC caps */
	for (i = 0; i < priv->head.nr; i++) {
		tmp = nv_rd32(priv, 0x616100 + (i * 0x800));
		nv_wr32(priv, 0x610190 + (i * 0x10), tmp);
		tmp = nv_rd32(priv, 0x616104 + (i * 0x800));
		nv_wr32(priv, 0x610194 + (i * 0x10), tmp);
		tmp = nv_rd32(priv, 0x616108 + (i * 0x800));
		nv_wr32(priv, 0x610198 + (i * 0x10), tmp);
		tmp = nv_rd32(priv, 0x61610c + (i * 0x800));
		nv_wr32(priv, 0x61019c + (i * 0x10), tmp);
	}

	/* ... DAC caps */
	for (i = 0; i < priv->dac.nr; i++) {
		tmp = nv_rd32(priv, 0x61a000 + (i * 0x800));
		nv_wr32(priv, 0x6101d0 + (i * 0x04), tmp);
	}

	/* ... SOR caps */
	for (i = 0; i < priv->sor.nr; i++) {
		tmp = nv_rd32(priv, 0x61c000 + (i * 0x800));
		nv_wr32(priv, 0x6101e0 + (i * 0x04), tmp);
	}

1173
	/* ... PIOR caps */
1174
	for (i = 0; i < priv->pior.nr; i++) {
B
Ben Skeggs 已提交
1175 1176 1177 1178
		tmp = nv_rd32(priv, 0x61e000 + (i * 0x800));
		nv_wr32(priv, 0x6101f0 + (i * 0x04), tmp);
	}

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	/* steal display away from vbios, or something like that */
	if (nv_rd32(priv, 0x610024) & 0x00000100) {
		nv_wr32(priv, 0x610024, 0x00000100);
		nv_mask(priv, 0x6194e8, 0x00000001, 0x00000000);
		if (!nv_wait(priv, 0x6194e8, 0x00000002, 0x00000000)) {
			nv_error(priv, "timeout acquiring display\n");
			return -EBUSY;
		}
	}

	/* point at display engine memory area (hash table, objects) */
1190
	nv_wr32(priv, 0x610010, (nv_gpuobj(base->ramht)->addr >> 8) | 9);
1191 1192

	/* enable supervisor interrupts, disable everything else */
1193 1194
	nv_wr32(priv, 0x61002c, 0x00000370);
	nv_wr32(priv, 0x610028, 0x00000000);
1195 1196 1197 1198
	return 0;
}

static int
1199
nv50_disp_main_fini(struct nouveau_object *object, bool suspend)
1200
{
1201
	struct nv50_disp_priv *priv = (void *)object->engine;
1202
	struct nv50_disp_base *base = (void *)object;
1203 1204 1205 1206 1207

	/* disable all interrupts */
	nv_wr32(priv, 0x610024, 0x00000000);
	nv_wr32(priv, 0x610020, 0x00000000);

1208 1209 1210 1211
	return nouveau_parent_fini(&base->base, suspend);
}

struct nouveau_ofuncs
1212 1213 1214 1215 1216 1217
nv50_disp_main_ofuncs = {
	.ctor = nv50_disp_main_ctor,
	.dtor = nv50_disp_main_dtor,
	.init = nv50_disp_main_init,
	.fini = nv50_disp_main_fini,
	.mthd = nv50_disp_main_mthd,
1218
	.ntfy = nouveau_disp_ntfy,
1219 1220 1221
};

static struct nouveau_oclass
1222 1223
nv50_disp_main_oclass[] = {
	{ NV50_DISP, &nv50_disp_main_ofuncs },
1224
	{}
1225 1226 1227 1228
};

static struct nouveau_oclass
nv50_disp_sclass[] = {
1229 1230
	{ NV50_DISP_CORE_CHANNEL_DMA, &nv50_disp_core_ofuncs.base },
	{ NV50_DISP_BASE_CHANNEL_DMA, &nv50_disp_base_ofuncs.base },
1231 1232 1233
	{ NV50_DISP_OVERLAY_CHANNEL_DMA, &nv50_disp_ovly_ofuncs.base },
	{ NV50_DISP_OVERLAY, &nv50_disp_oimm_ofuncs.base },
	{ NV50_DISP_CURSOR, &nv50_disp_curs_ofuncs.base },
1234
	{}
1235 1236
};

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
/*******************************************************************************
 * Display context, tracks instmem allocation and prevents more than one
 * client using the display hardware at any time.
 ******************************************************************************/

static int
nv50_disp_data_ctor(struct nouveau_object *parent,
		    struct nouveau_object *engine,
		    struct nouveau_oclass *oclass, void *data, u32 size,
		    struct nouveau_object **pobject)
{
1248
	struct nv50_disp_priv *priv = (void *)engine;
1249
	struct nouveau_engctx *ectx;
1250
	int ret = -EBUSY;
1251

1252
	/* no context needed for channel objects... */
1253
	if (nv_mclass(parent) != NV_DEVICE) {
1254 1255
		atomic_inc(&parent->refcount);
		*pobject = parent;
1256
		return 1;
1257
	}
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	/* allocate display hardware to client */
	mutex_lock(&nv_subdev(priv)->mutex);
	if (list_empty(&nv_engine(priv)->contexts)) {
		ret = nouveau_engctx_create(parent, engine, oclass, NULL,
					    0x10000, 0x10000,
					    NVOBJ_FLAG_HEAP, &ectx);
		*pobject = nv_object(ectx);
	}
	mutex_unlock(&nv_subdev(priv)->mutex);
	return ret;
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
}

struct nouveau_oclass
nv50_disp_cclass = {
	.handle = NV_ENGCTX(DISP, 0x50),
	.ofuncs = &(struct nouveau_ofuncs) {
		.ctor = nv50_disp_data_ctor,
		.dtor = _nouveau_engctx_dtor,
		.init = _nouveau_engctx_init,
		.fini = _nouveau_engctx_fini,
		.rd32 = _nouveau_engctx_rd32,
		.wr32 = _nouveau_engctx_wr32,
	},
};

/*******************************************************************************
 * Display engine implementation
 ******************************************************************************/

1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
static void
nv50_disp_vblank_fini(struct nvkm_event *event, int type, int head)
{
	struct nouveau_disp *disp = container_of(event, typeof(*disp), vblank);
	nv_mask(disp, 0x61002c, (4 << head), 0);
}

static void
nv50_disp_vblank_init(struct nvkm_event *event, int type, int head)
{
	struct nouveau_disp *disp = container_of(event, typeof(*disp), vblank);
	nv_mask(disp, 0x61002c, (4 << head), (4 << head));
}

const struct nvkm_event_func
nv50_disp_vblank_func = {
	.ctor = nouveau_disp_vblank_ctor,
	.init = nv50_disp_vblank_init,
	.fini = nv50_disp_vblank_fini,
};

1309 1310 1311 1312 1313 1314 1315 1316
static const struct nouveau_enum
nv50_disp_intr_error_type[] = {
	{ 3, "ILLEGAL_MTHD" },
	{ 4, "INVALID_VALUE" },
	{ 5, "INVALID_STATE" },
	{ 7, "INVALID_HANDLE" },
	{}
};
1317

1318 1319 1320 1321 1322
static const struct nouveau_enum
nv50_disp_intr_error_code[] = {
	{ 0x00, "" },
	{}
};
1323

1324 1325 1326
static void
nv50_disp_intr_error(struct nv50_disp_priv *priv, int chid)
{
1327
	struct nv50_disp_impl *impl = (void *)nv_object(priv)->oclass;
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
	u32 data = nv_rd32(priv, 0x610084 + (chid * 0x08));
	u32 addr = nv_rd32(priv, 0x610080 + (chid * 0x08));
	u32 code = (addr & 0x00ff0000) >> 16;
	u32 type = (addr & 0x00007000) >> 12;
	u32 mthd = (addr & 0x00000ffc);
	const struct nouveau_enum *ec, *et;
	char ecunk[6], etunk[6];

	et = nouveau_enum_find(nv50_disp_intr_error_type, type);
	if (!et)
		snprintf(etunk, sizeof(etunk), "UNK%02X", type);

	ec = nouveau_enum_find(nv50_disp_intr_error_code, code);
	if (!ec)
		snprintf(ecunk, sizeof(ecunk), "UNK%02X", code);

	nv_error(priv, "%s [%s] chid %d mthd 0x%04x data 0x%08x\n",
		 et ? et->name : etunk, ec ? ec->name : ecunk,
		 chid, mthd, data);

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	if (chid == 0) {
		switch (mthd) {
		case 0x0080:
			nv50_disp_mthd_chan(priv, NV_DBG_ERROR, chid - 0,
					    impl->mthd.core);
			break;
		default:
			break;
		}
	} else
	if (chid <= 2) {
		switch (mthd) {
		case 0x0080:
			nv50_disp_mthd_chan(priv, NV_DBG_ERROR, chid - 1,
					    impl->mthd.base);
			break;
		default:
			break;
		}
	} else
	if (chid <= 4) {
		switch (mthd) {
		case 0x0080:
			nv50_disp_mthd_chan(priv, NV_DBG_ERROR, chid - 3,
					    impl->mthd.ovly);
			break;
		default:
			break;
		}
	}

1379 1380
	nv_wr32(priv, 0x610020, 0x00010000 << chid);
	nv_wr32(priv, 0x610080 + (chid * 0x08), 0x90000000);
1381 1382
}

1383 1384 1385
static struct nvkm_output *
exec_lookup(struct nv50_disp_priv *priv, int head, int or, u32 ctrl,
	    u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
1386 1387 1388
	    struct nvbios_outp *info)
{
	struct nouveau_bios *bios = nouveau_bios(priv);
1389 1390
	struct nvkm_output *outp;
	u16 mask, type;
1391

1392
	if (or < 4) {
1393 1394
		type = DCB_OUTPUT_ANALOG;
		mask = 0;
1395
	} else
1396
	if (or < 8) {
1397 1398 1399 1400 1401 1402 1403 1404 1405
		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:
			nv_error(priv, "unknown SOR mc 0x%08x\n", ctrl);
1406
			return NULL;
1407
		}
1408
		or  -= 4;
1409
	} else {
1410
		or   = or - 8;
1411 1412 1413
		type = 0x0010;
		mask = 0;
		switch (ctrl & 0x00000f00) {
1414
		case 0x00000000: type |= priv->pior.type[or]; break;
1415 1416
		default:
			nv_error(priv, "unknown PIOR mc 0x%08x\n", ctrl);
1417
			return NULL;
1418
		}
1419 1420 1421
	}

	mask  = 0x00c0 & (mask << 6);
1422
	mask |= 0x0001 << or;
1423 1424
	mask |= 0x0100 << head;

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	list_for_each_entry(outp, &priv->base.outp, head) {
		if ((outp->info.hasht & 0xff) == type &&
		    (outp->info.hashm & mask) == mask) {
			*data = nvbios_outp_match(bios, outp->info.hasht,
							outp->info.hashm,
						  ver, hdr, cnt, len, info);
			if (!*data)
				return NULL;
			return outp;
		}
	}
1436

1437
	return NULL;
1438 1439
}

1440
static struct nvkm_output *
1441 1442 1443
exec_script(struct nv50_disp_priv *priv, int head, int id)
{
	struct nouveau_bios *bios = nouveau_bios(priv);
1444
	struct nvkm_output *outp;
1445 1446
	struct nvbios_outp info;
	u8  ver, hdr, cnt, len;
1447
	u32 data, ctrl = 0;
1448
	u32 reg;
1449 1450
	int i;

1451
	/* DAC */
1452
	for (i = 0; !(ctrl & (1 << head)) && i < priv->dac.nr; i++)
1453 1454
		ctrl = nv_rd32(priv, 0x610b5c + (i * 8));

1455
	/* SOR */
1456 1457 1458 1459
	if (!(ctrl & (1 << head))) {
		if (nv_device(priv)->chipset  < 0x90 ||
		    nv_device(priv)->chipset == 0x92 ||
		    nv_device(priv)->chipset == 0xa0) {
1460
			reg = 0x610b74;
1461
		} else {
1462
			reg = 0x610798;
1463
		}
1464 1465 1466
		for (i = 0; !(ctrl & (1 << head)) && i < priv->sor.nr; i++)
			ctrl = nv_rd32(priv, reg + (i * 8));
		i += 4;
1467 1468
	}

1469 1470
	/* PIOR */
	if (!(ctrl & (1 << head))) {
1471
		for (i = 0; !(ctrl & (1 << head)) && i < priv->pior.nr; i++)
1472 1473 1474 1475
			ctrl = nv_rd32(priv, 0x610b84 + (i * 8));
		i += 8;
	}

1476
	if (!(ctrl & (1 << head)))
1477
		return NULL;
1478
	i--;
1479

1480 1481
	outp = exec_lookup(priv, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
	if (outp) {
1482 1483 1484 1485
		struct nvbios_init init = {
			.subdev = nv_subdev(priv),
			.bios = bios,
			.offset = info.script[id],
1486
			.outp = &outp->info,
1487 1488 1489 1490
			.crtc = head,
			.execute = 1,
		};

1491
		nvbios_exec(&init);
1492 1493
	}

1494
	return outp;
1495 1496
}

1497 1498
static struct nvkm_output *
exec_clkcmp(struct nv50_disp_priv *priv, int head, int id, u32 pclk, u32 *conf)
1499 1500
{
	struct nouveau_bios *bios = nouveau_bios(priv);
1501
	struct nvkm_output *outp;
1502 1503 1504
	struct nvbios_outp info1;
	struct nvbios_ocfg info2;
	u8  ver, hdr, cnt, len;
1505
	u32 data, ctrl = 0;
1506
	u32 reg;
1507 1508
	int i;

1509
	/* DAC */
1510
	for (i = 0; !(ctrl & (1 << head)) && i < priv->dac.nr; i++)
1511 1512
		ctrl = nv_rd32(priv, 0x610b58 + (i * 8));

1513
	/* SOR */
1514 1515 1516 1517
	if (!(ctrl & (1 << head))) {
		if (nv_device(priv)->chipset  < 0x90 ||
		    nv_device(priv)->chipset == 0x92 ||
		    nv_device(priv)->chipset == 0xa0) {
1518
			reg = 0x610b70;
1519
		} else {
1520
			reg = 0x610794;
1521
		}
1522 1523 1524
		for (i = 0; !(ctrl & (1 << head)) && i < priv->sor.nr; i++)
			ctrl = nv_rd32(priv, reg + (i * 8));
		i += 4;
1525 1526
	}

1527 1528
	/* PIOR */
	if (!(ctrl & (1 << head))) {
1529
		for (i = 0; !(ctrl & (1 << head)) && i < priv->pior.nr; i++)
1530 1531 1532 1533
			ctrl = nv_rd32(priv, 0x610b80 + (i * 8));
		i += 8;
	}

1534
	if (!(ctrl & (1 << head)))
1535
		return NULL;
1536
	i--;
1537

1538
	outp = exec_lookup(priv, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
1539
	if (!outp)
1540
		return NULL;
1541

1542 1543
	if (outp->info.location == 0) {
		switch (outp->info.type) {
1544
		case DCB_OUTPUT_TMDS:
1545
			*conf = (ctrl & 0x00000f00) >> 8;
1546
			if (pclk >= 165000)
1547
				*conf |= 0x0100;
1548 1549
			break;
		case DCB_OUTPUT_LVDS:
1550
			*conf = priv->sor.lvdsconf;
1551 1552
			break;
		case DCB_OUTPUT_DP:
1553
			*conf = (ctrl & 0x00000f00) >> 8;
1554 1555 1556
			break;
		case DCB_OUTPUT_ANALOG:
		default:
1557
			*conf = 0x00ff;
1558 1559 1560
			break;
		}
	} else {
1561
		*conf = (ctrl & 0x00000f00) >> 8;
1562
		pclk = pclk / 2;
1563 1564
	}

1565
	data = nvbios_ocfg_match(bios, data, *conf, &ver, &hdr, &cnt, &len, &info2);
1566
	if (data && id < 0xff) {
1567 1568 1569 1570 1571 1572
		data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
		if (data) {
			struct nvbios_init init = {
				.subdev = nv_subdev(priv),
				.bios = bios,
				.offset = data,
1573
				.outp = &outp->info,
1574 1575 1576 1577
				.crtc = head,
				.execute = 1,
			};

1578
			nvbios_exec(&init);
1579 1580 1581
		}
	}

1582
	return outp;
1583 1584 1585
}

static void
1586
nv50_disp_intr_unk10_0(struct nv50_disp_priv *priv, int head)
1587
{
1588 1589
	exec_script(priv, head, 1);
}
1590

1591 1592 1593
static void
nv50_disp_intr_unk20_0(struct nv50_disp_priv *priv, int head)
{
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	struct nvkm_output *outp = exec_script(priv, head, 2);

	/* 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 = (void *)outp;
		struct nvbios_init init = {
			.subdev = nv_subdev(priv),
			.bios = nouveau_bios(priv),
			.outp = &outp->info,
			.crtc = head,
			.offset = outpdp->info.script[4],
			.execute = 1,
		};

		nvbios_exec(&init);
		atomic_set(&outpdp->lt.done, 0);
	}
1623 1624 1625
}

static void
1626 1627
nv50_disp_intr_unk20_1(struct nv50_disp_priv *priv, int head)
{
1628
	struct nouveau_devinit *devinit = nouveau_devinit(priv);
1629 1630
	u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
	if (pclk)
1631
		devinit->pll_set(devinit, PLL_VPLL0 + head, pclk);
1632 1633 1634
}

static void
1635
nv50_disp_intr_unk20_2_dp(struct nv50_disp_priv *priv, int head,
1636
			  struct dcb_output *outp, u32 pclk)
1637 1638 1639 1640 1641 1642 1643
{
	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;
	const u32 ctrl = nv_rd32(priv, 0x610794 + (or * 8));
	const u32 symbol = 100000;
1644 1645 1646 1647
	const s32 vactive = nv_rd32(priv, 0x610af8 + (head * 0x540)) & 0xffff;
	const s32 vblanke = nv_rd32(priv, 0x610ae8 + (head * 0x540)) & 0xffff;
	const s32 vblanks = nv_rd32(priv, 0x610af0 + (head * 0x540)) & 0xffff;
	u32 dpctrl = nv_rd32(priv, 0x61c10c + loff);
1648 1649 1650 1651 1652
	u32 clksor = nv_rd32(priv, 0x614300 + soff);
	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 已提交
1653
	u32 link_nr, link_bw, bits;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
	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);
	nv_mask(priv, 0x61c1e8 + soff, 0x0000ffff, value);

	/* 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;
	nv_mask(priv, 0x61c1ec + soff, 0x00ffffff, value);

	/* watermark / activesym */
1674 1675 1676 1677
	if      ((ctrl & 0xf0000) == 0x60000) bits = 30;
	else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
	else                                  bits = 18;

1678 1679 1680 1681
	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 已提交
1682
	do_div(link_ratio, link_bw);
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

	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) {
		nv_error(priv, "unable to find suitable dp config\n");
		return;
	}

	/* XXX close to vbios numbers, but not right */
	unk  = (symbol - link_ratio) * bestTU;
	unk *= link_ratio;
B
Ben Skeggs 已提交
1743 1744
	do_div(unk, symbol);
	do_div(unk, symbol);
1745 1746 1747 1748 1749 1750 1751 1752 1753
	unk += 6;

	nv_mask(priv, 0x61c10c + loff, 0x000001fc, bestTU << 2);
	nv_mask(priv, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
						   bestVTUf << 16 |
						   bestVTUi << 8 | unk);
}

static void
1754
nv50_disp_intr_unk20_2(struct nv50_disp_priv *priv, int head)
1755
{
1756
	struct nvkm_output *outp;
1757 1758 1759
	u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
	u32 hval, hreg = 0x614200 + (head * 0x800);
	u32 oval, oreg;
1760
	u32 mask, conf;
1761

1762 1763 1764
	outp = exec_clkcmp(priv, head, 0xff, pclk, &conf);
	if (!outp)
		return;
1765

1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
	/* 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).
	 */
1784
	if (outp->info.type == DCB_OUTPUT_DP) {
1785
		u32 soff = (ffs(outp->info.or) - 1) * 0x08;
1786 1787 1788 1789 1790 1791 1792 1793 1794
		u32 ctrl, datarate;

		if (outp->info.location == 0) {
			ctrl = nv_rd32(priv, 0x610794 + soff);
			soff = 1;
		} else {
			ctrl = nv_rd32(priv, 0x610b80 + soff);
			soff = 2;
		}
1795 1796

		switch ((ctrl & 0x000f0000) >> 16) {
1797 1798
		case 6: datarate = pclk * 30; break;
		case 5: datarate = pclk * 24; break;
1799 1800
		case 2:
		default:
1801
			datarate = pclk * 18;
1802
			break;
1803 1804
		}

1805 1806
		if (nvkm_output_dp_train(outp, datarate / soff, true))
			ERR("link not trained before attach\n");
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
	}

	exec_clkcmp(priv, head, 0, pclk, &conf);

	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)
1819
			nv50_disp_intr_unk20_2_dp(priv, head, &outp->info, pclk);
1820 1821 1822 1823 1824 1825 1826 1827 1828
		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;
1829
	}
1830 1831 1832

	nv_mask(priv, hreg, 0x0000000f, hval);
	nv_mask(priv, oreg, mask, oval);
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
}

/* 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
1844
nv50_disp_intr_unk40_0_tmds(struct nv50_disp_priv *priv, struct dcb_output *outp)
1845 1846 1847 1848 1849 1850
{
	struct nouveau_bios *bios = nouveau_bios(priv);
	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;
1851
	struct dcb_output match;
1852 1853
	u8  ver, hdr;

1854
	if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
1855 1856 1857 1858
		nv_mask(priv, 0x61c10c + loff, 0x00000001, 0x00000000);
}

static void
1859
nv50_disp_intr_unk40_0(struct nv50_disp_priv *priv, int head)
1860
{
1861
	struct nvkm_output *outp;
1862
	u32 pclk = nv_rd32(priv, 0x610ad0 + (head * 0x540)) & 0x3fffff;
1863
	u32 conf;
1864

1865 1866 1867 1868 1869 1870
	outp = exec_clkcmp(priv, head, 1, pclk, &conf);
	if (!outp)
		return;

	if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
		nv50_disp_intr_unk40_0_tmds(priv, &outp->info);
1871 1872
}

1873 1874
void
nv50_disp_intr_supervisor(struct work_struct *work)
1875
{
1876 1877
	struct nv50_disp_priv *priv =
		container_of(work, struct nv50_disp_priv, supervisor);
1878
	struct nv50_disp_impl *impl = (void *)nv_object(priv)->oclass;
1879
	u32 super = nv_rd32(priv, 0x610030);
1880
	int head;
1881

1882
	nv_debug(priv, "supervisor 0x%08x 0x%08x\n", priv->super, super);
1883

1884
	if (priv->super & 0x00000010) {
1885
		nv50_disp_mthd_chan(priv, NV_DBG_DEBUG, 0, impl->mthd.core);
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
		for (head = 0; head < priv->head.nr; head++) {
			if (!(super & (0x00000020 << head)))
				continue;
			if (!(super & (0x00000080 << head)))
				continue;
			nv50_disp_intr_unk10_0(priv, head);
		}
	} else
	if (priv->super & 0x00000020) {
		for (head = 0; head < priv->head.nr; head++) {
			if (!(super & (0x00000080 << head)))
				continue;
			nv50_disp_intr_unk20_0(priv, head);
		}
		for (head = 0; head < priv->head.nr; head++) {
			if (!(super & (0x00000200 << head)))
				continue;
			nv50_disp_intr_unk20_1(priv, head);
		}
		for (head = 0; head < priv->head.nr; head++) {
			if (!(super & (0x00000080 << head)))
				continue;
			nv50_disp_intr_unk20_2(priv, head);
		}
	} else
	if (priv->super & 0x00000040) {
		for (head = 0; head < priv->head.nr; head++) {
			if (!(super & (0x00000080 << head)))
				continue;
			nv50_disp_intr_unk40_0(priv, head);
		}
	}

	nv_wr32(priv, 0x610030, 0x80000000);
1920 1921
}

1922
void
1923 1924 1925
nv50_disp_intr(struct nouveau_subdev *subdev)
{
	struct nv50_disp_priv *priv = (void *)subdev;
1926 1927
	u32 intr0 = nv_rd32(priv, 0x610020);
	u32 intr1 = nv_rd32(priv, 0x610024);
1928

1929 1930 1931 1932
	while (intr0 & 0x001f0000) {
		u32 chid = __ffs(intr0 & 0x001f0000) - 16;
		nv50_disp_intr_error(priv, chid);
		intr0 &= ~(0x00010000 << chid);
1933 1934
	}

1935 1936 1937 1938 1939 1940
	while (intr0 & 0x0000001f) {
		u32 chid = __ffs(intr0 & 0x0000001f);
		nv50_disp_chan_uevent_send(priv, chid);
		intr0 &= ~(0x00000001 << chid);
	}

1941
	if (intr1 & 0x00000004) {
1942
		nouveau_disp_vblank(&priv->base, 0);
1943
		nv_wr32(priv, 0x610024, 0x00000004);
1944
		intr1 &= ~0x00000004;
1945 1946
	}

1947
	if (intr1 & 0x00000008) {
1948
		nouveau_disp_vblank(&priv->base, 1);
1949
		nv_wr32(priv, 0x610024, 0x00000008);
1950
		intr1 &= ~0x00000008;
1951 1952
	}

1953
	if (intr1 & 0x00000070) {
1954 1955 1956
		priv->super = (intr1 & 0x00000070);
		schedule_work(&priv->supervisor);
		nv_wr32(priv, 0x610024, priv->super);
1957 1958
		intr1 &= ~0x00000070;
	}
1959 1960 1961 1962
}

static int
nv50_disp_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
1963 1964
	       struct nouveau_oclass *oclass, void *data, u32 size,
	       struct nouveau_object **pobject)
1965 1966 1967 1968
{
	struct nv50_disp_priv *priv;
	int ret;

1969
	ret = nouveau_disp_create(parent, engine, oclass, 2, "PDISP",
1970 1971 1972 1973 1974
				  "display", &priv);
	*pobject = nv_object(priv);
	if (ret)
		return ret;

1975 1976 1977 1978
	ret = nvkm_event_init(&nv50_disp_chan_uevent, 1, 9, &priv->uevent);
	if (ret)
		return ret;

1979
	nv_engine(priv)->sclass = nv50_disp_main_oclass;
1980
	nv_engine(priv)->cclass = &nv50_disp_cclass;
1981
	nv_subdev(priv)->intr = nv50_disp_intr;
1982
	INIT_WORK(&priv->supervisor, nv50_disp_intr_supervisor);
1983 1984 1985 1986
	priv->sclass = nv50_disp_sclass;
	priv->head.nr = 2;
	priv->dac.nr = 3;
	priv->sor.nr = 2;
1987
	priv->pior.nr = 3;
1988
	priv->dac.power = nv50_dac_power;
1989
	priv->dac.sense = nv50_dac_sense;
1990
	priv->sor.power = nv50_sor_power;
1991
	priv->pior.power = nv50_pior_power;
1992 1993 1994
	return 0;
}

1995 1996 1997 1998 1999 2000
struct nouveau_oclass *
nv50_disp_outp_sclass[] = {
	&nv50_pior_dp_impl.base.base,
	NULL
};

2001 2002 2003 2004
struct nouveau_oclass *
nv50_disp_oclass = &(struct nv50_disp_impl) {
	.base.base.handle = NV_ENGINE(DISP, 0x50),
	.base.base.ofuncs = &(struct nouveau_ofuncs) {
2005 2006 2007 2008 2009
		.ctor = nv50_disp_ctor,
		.dtor = _nouveau_disp_dtor,
		.init = _nouveau_disp_init,
		.fini = _nouveau_disp_fini,
	},
2010
	.base.vblank = &nv50_disp_vblank_func,
2011
	.base.outp =  nv50_disp_outp_sclass,
2012 2013
	.mthd.core = &nv50_disp_core_mthd_chan,
	.mthd.base = &nv50_disp_base_mthd_chan,
2014 2015
	.mthd.ovly = &nv50_disp_ovly_mthd_chan,
	.mthd.prev = 0x000004,
2016
	.head.scanoutpos = nv50_disp_main_scanoutpos,
2017
}.base.base;