disp.c 80.4 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright 2011 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 25 26 27 28
#include "disp.h"
#include "atom.h"
#include "core.h"
#include "head.h"
#include "wndw.h"
29
#include "handles.h"
30

31
#include <linux/dma-mapping.h>
32
#include <linux/hdmi.h>
33
#include <linux/component.h>
34
#include <linux/iopoll.h>
35

T
Thomas Zimmermann 已提交
36
#include <drm/display/drm_dp_helper.h>
37
#include <drm/display/drm_scdc_helper.h>
38
#include <drm/drm_atomic.h>
39
#include <drm/drm_atomic_helper.h>
S
Sam Ravnborg 已提交
40
#include <drm/drm_edid.h>
41
#include <drm/drm_fb_helper.h>
42
#include <drm/drm_probe_helper.h>
S
Sam Ravnborg 已提交
43
#include <drm/drm_vblank.h>
44

45 46
#include <nvif/push507c.h>

47 48 49 50
#include <nvif/class.h>
#include <nvif/cl0002.h>
#include <nvif/cl5070.h>
#include <nvif/event.h>
51
#include <nvif/if0014.h>
52
#include <nvif/timer.h>
53

54
#include <nvhw/class/cl507c.h>
55 56 57 58 59
#include <nvhw/class/cl507d.h>
#include <nvhw/class/cl837d.h>
#include <nvhw/class/cl887d.h>
#include <nvhw/class/cl907d.h>
#include <nvhw/class/cl917d.h>
60

61 62 63 64 65 66 67
#include "nouveau_drv.h"
#include "nouveau_dma.h"
#include "nouveau_gem.h"
#include "nouveau_connector.h"
#include "nouveau_encoder.h"
#include "nouveau_fence.h"
#include "nouveau_fbcon.h"
68

69 70
#include <subdev/bios/dp.h>

71 72 73
/******************************************************************************
 * EVO channel
 *****************************************************************************/
74 75

static int
76 77 78
nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
		 const s32 *oclass, u8 head, void *data, u32 size,
		 struct nv50_chan *chan)
79
{
80 81
	struct nvif_sclass *sclass;
	int ret, i, n;
82

83
	chan->device = device;
84

85 86 87
	ret = n = nvif_object_sclass_get(disp, &sclass);
	if (ret < 0)
		return ret;
88

89 90 91
	while (oclass[0]) {
		for (i = 0; i < n; i++) {
			if (sclass[i].oclass == oclass[0]) {
92 93 94
				ret = nvif_object_ctor(disp, "kmsChan", 0,
						       oclass[0], data, size,
						       &chan->user);
95 96 97 98 99
				if (ret == 0)
					nvif_object_map(&chan->user, NULL, 0);
				nvif_object_sclass_put(&sclass);
				return ret;
			}
100
		}
101
		oclass++;
102 103
	}

104 105
	nvif_object_sclass_put(&sclass);
	return -ENOSYS;
106 107
}

108
static void
109
nv50_chan_destroy(struct nv50_chan *chan)
110
{
111
	nvif_object_dtor(&chan->user);
112 113
}

114 115 116
/******************************************************************************
 * DMA EVO channel
 *****************************************************************************/
117

118 119
void
nv50_dmac_destroy(struct nv50_dmac *dmac)
120
{
121 122
	nvif_object_dtor(&dmac->vram);
	nvif_object_dtor(&dmac->sync);
123

124
	nv50_chan_destroy(&dmac->base);
125

126 127 128 129 130 131 132
	nvif_mem_dtor(&dmac->_push.mem);
}

static void
nv50_dmac_kick(struct nvif_push *push)
{
	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
133 134 135 136 137 138 139 140 141 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 178 179 180 181 182 183 184 185

	dmac->cur = push->cur - (u32 *)dmac->_push.mem.object.map.ptr;
	if (dmac->put != dmac->cur) {
		/* Push buffer fetches are not coherent with BAR1, we need to ensure
		 * writes have been flushed right through to VRAM before writing PUT.
		 */
		if (dmac->push->mem.type & NVIF_MEM_VRAM) {
			struct nvif_device *device = dmac->base.device;
			nvif_wr32(&device->object, 0x070000, 0x00000001);
			nvif_msec(device, 2000,
				if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
					break;
			);
		}

		NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
		dmac->put = dmac->cur;
	}

	push->bgn = push->cur;
}

static int
nv50_dmac_free(struct nv50_dmac *dmac)
{
	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
	if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
		return get - dmac->cur - 5;
	return dmac->max - dmac->cur;
}

static int
nv50_dmac_wind(struct nv50_dmac *dmac)
{
	/* Wait for GET to depart from the beginning of the push buffer to
	 * prevent writing PUT == GET, which would be ignored by HW.
	 */
	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
	if (get == 0) {
		/* Corner-case, HW idle, but non-committed work pending. */
		if (dmac->put == 0)
			nv50_dmac_kick(dmac->push);

		if (nvif_msec(dmac->base.device, 2000,
			if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
				break;
		) < 0)
			return -ETIMEDOUT;
	}

	PUSH_RSVD(dmac->push, PUSH_JUMP(dmac->push, 0));
	dmac->cur = 0;
	return 0;
186 187 188 189 190 191
}

static int
nv50_dmac_wait(struct nvif_push *push, u32 size)
{
	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	int free;

	if (WARN_ON(size > dmac->max))
		return -EINVAL;

	dmac->cur = push->cur - (u32 *)dmac->_push.mem.object.map.ptr;
	if (dmac->cur + size >= dmac->max) {
		int ret = nv50_dmac_wind(dmac);
		if (ret)
			return ret;

		push->cur = dmac->_push.mem.object.map.ptr;
		push->cur = push->cur + dmac->cur;
		nv50_dmac_kick(push);
	}

	if (nvif_msec(dmac->base.device, 2000,
		if ((free = nv50_dmac_free(dmac)) >= size)
			break;
	) < 0) {
		WARN_ON(1);
213
		return -ETIMEDOUT;
214
	}
215

216 217 218 219
	push->bgn = dmac->_push.mem.object.map.ptr;
	push->bgn = push->bgn + dmac->cur;
	push->cur = push->bgn;
	push->end = push->cur + free;
220
	return 0;
221 222
}

223 224 225 226
MODULE_PARM_DESC(kms_vram_pushbuf, "Place EVO/NVD push buffers in VRAM (default: auto)");
static int nv50_dmac_vram_pushbuf = -1;
module_param_named(kms_vram_pushbuf, nv50_dmac_vram_pushbuf, int, 0400);

227 228
int
nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
229
		 const s32 *oclass, u8 head, void *data, u32 size, s64 syncbuf,
230
		 struct nv50_dmac *dmac)
231
{
232
	struct nouveau_cli *cli = (void *)device->object.client;
233
	struct nvif_disp_chan_v0 *args = data;
234
	u8 type = NVIF_MEM_COHERENT;
235
	int ret;
236

237
	mutex_init(&dmac->lock);
238

239 240 241 242 243 244 245 246 247
	/* Pascal added support for 47-bit physical addresses, but some
	 * parts of EVO still only accept 40-bit PAs.
	 *
	 * To avoid issues on systems with large amounts of RAM, and on
	 * systems where an IOMMU maps pages at a high address, we need
	 * to allocate push buffers in VRAM instead.
	 *
	 * This appears to match NVIDIA's behaviour on Pascal.
	 */
248 249
	if ((nv50_dmac_vram_pushbuf > 0) ||
	    (nv50_dmac_vram_pushbuf < 0 && device->info.family == NV_DEVICE_INFO_V0_PASCAL))
250 251
		type |= NVIF_MEM_VRAM;

252
	ret = nvif_mem_ctor_map(&cli->mmu, "kmsChanPush", type, 0x1000,
253
				&dmac->_push.mem);
254 255
	if (ret)
		return ret;
256

257 258 259 260
	dmac->ptr = dmac->_push.mem.object.map.ptr;
	dmac->_push.wait = nv50_dmac_wait;
	dmac->_push.kick = nv50_dmac_kick;
	dmac->push = &dmac->_push;
261 262 263 264
	dmac->push->bgn = dmac->_push.mem.object.map.ptr;
	dmac->push->cur = dmac->push->bgn;
	dmac->push->end = dmac->push->bgn;
	dmac->max = 0x1000/4 - 1;
265

266 267 268 269 270 271
	/* EVO channels are affected by a HW bug where the last 12 DWORDs
	 * of the push buffer aren't able to be used safely.
	 */
	if (disp->oclass < GV100_DISP)
		dmac->max -= 12;

272
	args->pushbuf = nvif_handle(&dmac->_push.mem.object);
273

274 275 276
	ret = nv50_chan_create(device, disp, oclass, head, data, size,
			       &dmac->base);
	if (ret)
277 278
		return ret;

279
	if (syncbuf < 0)
280 281
		return 0;

282
	ret = nvif_object_ctor(&dmac->base.user, "kmsSyncCtxDma", NV50_DISP_HANDLE_SYNCBUF,
283
			       NV_DMA_IN_MEMORY,
284 285 286 287 288 289 290 291 292
			       &(struct nv_dma_v0) {
					.target = NV_DMA_V0_TARGET_VRAM,
					.access = NV_DMA_V0_ACCESS_RDWR,
					.start = syncbuf + 0x0000,
					.limit = syncbuf + 0x0fff,
			       }, sizeof(struct nv_dma_v0),
			       &dmac->sync);
	if (ret)
		return ret;
293

294
	ret = nvif_object_ctor(&dmac->base.user, "kmsVramCtxDma", NV50_DISP_HANDLE_VRAM,
295
			       NV_DMA_IN_MEMORY,
296 297 298 299 300 301 302
			       &(struct nv_dma_v0) {
					.target = NV_DMA_V0_TARGET_VRAM,
					.access = NV_DMA_V0_ACCESS_RDWR,
					.start = 0,
					.limit = device->info.ram_user - 1,
			       }, sizeof(struct nv_dma_v0),
			       &dmac->vram);
303
	if (ret)
304 305
		return ret;

306 307 308
	return ret;
}

309
/******************************************************************************
310
 * Output path helpers
311
 *****************************************************************************/
312 313 314 315 316 317 318 319
static void
nv50_outp_dump_caps(struct nouveau_drm *drm,
		    struct nouveau_encoder *outp)
{
	NV_DEBUG(drm, "%s caps: dp_interlace=%d\n",
		 outp->base.base.name, outp->caps.dp_interlace);
}

320 321 322 323 324 325 326 327 328 329 330 331 332
static void
nv50_outp_release(struct nouveau_encoder *nv_encoder)
{
	struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
	struct {
		struct nv50_disp_mthd_v1 base;
	} args = {
		.base.version = 1,
		.base.method = NV50_DISP_MTHD_V1_RELEASE,
		.base.hasht  = nv_encoder->dcb->hasht,
		.base.hashm  = nv_encoder->dcb->hashm,
	};

333
	nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
334 335 336 337 338
	nv_encoder->or = -1;
	nv_encoder->link = 0;
}

static int
339
nv50_outp_acquire(struct nouveau_encoder *nv_encoder, bool hda)
340 341 342 343 344 345 346 347 348 349 350
{
	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
	struct nv50_disp *disp = nv50_disp(drm->dev);
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_acquire_v0 info;
	} args = {
		.base.version = 1,
		.base.method = NV50_DISP_MTHD_V1_ACQUIRE,
		.base.hasht  = nv_encoder->dcb->hasht,
		.base.hashm  = nv_encoder->dcb->hashm,
351
		.info.hda = hda,
352 353 354
	};
	int ret;

355
	ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
356 357 358 359 360 361 362 363 364 365
	if (ret) {
		NV_ERROR(drm, "error acquiring output path: %d\n", ret);
		return ret;
	}

	nv_encoder->or = args.info.or;
	nv_encoder->link = args.info.link;
	return 0;
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
static int
nv50_outp_atomic_check_view(struct drm_encoder *encoder,
			    struct drm_crtc_state *crtc_state,
			    struct drm_connector_state *conn_state,
			    struct drm_display_mode *native_mode)
{
	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
	struct drm_display_mode *mode = &crtc_state->mode;
	struct drm_connector *connector = conn_state->connector;
	struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);

	NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
	asyc->scaler.full = false;
	if (!native_mode)
		return 0;

	if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
		switch (connector->connector_type) {
		case DRM_MODE_CONNECTOR_LVDS:
		case DRM_MODE_CONNECTOR_eDP:
387 388 389 390
			/* Don't force scaler for EDID modes with
			 * same size as the native one (e.g. different
			 * refresh rate)
			 */
391 392 393
			if (mode->hdisplay == native_mode->hdisplay &&
			    mode->vdisplay == native_mode->vdisplay &&
			    mode->type & DRM_MODE_TYPE_DRIVER)
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
				break;
			mode = native_mode;
			asyc->scaler.full = true;
			break;
		default:
			break;
		}
	} else {
		mode = native_mode;
	}

	if (!drm_mode_equal(adjusted_mode, mode)) {
		drm_mode_copy(adjusted_mode, mode);
		crtc_state->mode_changed = true;
	}

	return 0;
}

413 414 415 416
static int
nv50_outp_atomic_check(struct drm_encoder *encoder,
		       struct drm_crtc_state *crtc_state,
		       struct drm_connector_state *conn_state)
417
{
418 419 420 421 422 423 424 425 426 427 428 429 430 431
	struct drm_connector *connector = conn_state->connector;
	struct nouveau_connector *nv_connector = nouveau_connector(connector);
	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
	int ret;

	ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
					  nv_connector->native_mode);
	if (ret)
		return ret;

	if (crtc_state->mode_changed || crtc_state->connectors_changed)
		asyh->or.bpc = connector->display_info.bpc;

	return 0;
432 433
}

434
struct nouveau_connector *
435
nv50_outp_get_new_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
{
	struct drm_connector *connector;
	struct drm_connector_state *connector_state;
	struct drm_encoder *encoder = to_drm_encoder(outp);
	int i;

	for_each_new_connector_in_state(state, connector, connector_state, i) {
		if (connector_state->best_encoder == encoder)
			return nouveau_connector(connector);
	}

	return NULL;
}

struct nouveau_connector *
451
nv50_outp_get_old_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
452 453 454 455 456 457 458 459 460 461 462 463 464 465
{
	struct drm_connector *connector;
	struct drm_connector_state *connector_state;
	struct drm_encoder *encoder = to_drm_encoder(outp);
	int i;

	for_each_old_connector_in_state(state, connector, connector_state, i) {
		if (connector_state->best_encoder == encoder)
			return nouveau_connector(connector);
	}

	return NULL;
}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static struct nouveau_crtc *
nv50_outp_get_new_crtc(const struct drm_atomic_state *state, const struct nouveau_encoder *outp)
{
	struct drm_crtc *crtc;
	struct drm_crtc_state *crtc_state;
	const u32 mask = drm_encoder_mask(&outp->base.base);
	int i;

	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
		if (crtc_state->encoder_mask & mask)
			return nouveau_crtc(crtc);
	}

	return NULL;
}

482 483 484
/******************************************************************************
 * DAC
 *****************************************************************************/
485
static void
486
nv50_dac_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
487 488 489
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct nv50_core *core = nv50_disp(encoder->dev)->core;
490
	const u32 ctrl = NVDEF(NV507D, DAC_SET_CONTROL, OWNER, NONE);
491 492

	core->func->dac->ctrl(core, nv_encoder->or, ctrl, NULL);
493
	nv_encoder->crtc = NULL;
494
	nv50_outp_release(nv_encoder);
B
Ben Skeggs 已提交
495 496 497
}

static void
498
nv50_dac_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
B
Ben Skeggs 已提交
499 500
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
501 502 503
	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
	struct nv50_head_atom *asyh =
		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
504
	struct nv50_core *core = nv50_disp(encoder->dev)->core;
505 506 507 508 509 510 511 512 513 514 515 516 517
	u32 ctrl = 0;

	switch (nv_crtc->index) {
	case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
	case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
	case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
	case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
	default:
		WARN_ON(1);
		break;
	}

	ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
B
Ben Skeggs 已提交
518

519
	nv50_outp_acquire(nv_encoder, false);
520

521
	core->func->dac->ctrl(core, nv_encoder->or, ctrl, asyh);
522
	asyh->or.depth = 0;
B
Ben Skeggs 已提交
523

524
	nv_encoder->crtc = &nv_crtc->base;
B
Ben Skeggs 已提交
525 526
}

527
static enum drm_connector_status
528
nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
529
{
530
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
531
	u32 loadval;
532 533
	int ret;

534 535 536
	loadval = nouveau_drm(encoder->dev)->vbios.dactestval;
	if (loadval == 0)
		loadval = 340;
B
Ben Skeggs 已提交
537

538 539
	ret = nvif_outp_load_detect(&nv_encoder->outp, loadval);
	if (ret <= 0)
540
		return connector_status_disconnected;
B
Ben Skeggs 已提交
541

542
	return connector_status_connected;
543 544
}

545 546
static const struct drm_encoder_helper_funcs
nv50_dac_help = {
547
	.atomic_check = nv50_outp_atomic_check,
548 549
	.atomic_enable = nv50_dac_atomic_enable,
	.atomic_disable = nv50_dac_atomic_disable,
550
	.detect = nv50_dac_detect
B
Ben Skeggs 已提交
551 552
};

553 554 555
static void
nv50_dac_destroy(struct drm_encoder *encoder)
{
B
Ben Skeggs 已提交
556 557 558 559
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);

	nvif_outp_dtor(&nv_encoder->outp);

560 561 562 563 564 565
	drm_encoder_cleanup(encoder);
	kfree(encoder);
}

static const struct drm_encoder_funcs
nv50_dac_func = {
566
	.destroy = nv50_dac_destroy,
B
Ben Skeggs 已提交
567 568 569
};

static int
570
nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
B
Ben Skeggs 已提交
571
{
572
	struct nouveau_drm *drm = nouveau_drm(connector->dev);
B
Ben Skeggs 已提交
573
	struct nv50_disp *disp = nv50_disp(connector->dev);
574
	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
575
	struct nvkm_i2c_bus *bus;
B
Ben Skeggs 已提交
576 577
	struct nouveau_encoder *nv_encoder;
	struct drm_encoder *encoder;
578
	int type = DRM_MODE_ENCODER_DAC;
B
Ben Skeggs 已提交
579 580 581 582 583

	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
	if (!nv_encoder)
		return -ENOMEM;
	nv_encoder->dcb = dcbe;
584 585 586 587

	bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
	if (bus)
		nv_encoder->i2c = &bus->i2c;
B
Ben Skeggs 已提交
588 589 590 591

	encoder = to_drm_encoder(nv_encoder);
	encoder->possible_crtcs = dcbe->heads;
	encoder->possible_clones = 0;
592 593
	drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
			 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
594
	drm_encoder_helper_add(encoder, &nv50_dac_help);
B
Ben Skeggs 已提交
595

596
	drm_connector_attach_encoder(connector, encoder);
B
Ben Skeggs 已提交
597
	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
B
Ben Skeggs 已提交
598
}
599

600 601 602 603
/*
 * audio component binding for ELD notification
 */
static void
604 605
nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
				int dev_id)
606 607 608
{
	if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
		acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
609
						 port, dev_id);
610 611 612
}

static int
613
nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
614 615 616 617 618 619 620 621 622 623
			     bool *enabled, unsigned char *buf, int max_bytes)
{
	struct drm_device *drm_dev = dev_get_drvdata(kdev);
	struct nouveau_drm *drm = nouveau_drm(drm_dev);
	struct drm_encoder *encoder;
	struct nouveau_encoder *nv_encoder;
	struct nouveau_crtc *nv_crtc;
	int ret = 0;

	*enabled = false;
624

625 626
	mutex_lock(&drm->audio.lock);

627
	drm_for_each_encoder(encoder, drm->dev) {
628 629
		struct nouveau_connector *nv_connector = NULL;

630 631 632
		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
			continue; /* TODO */

633
		nv_encoder = nouveau_encoder(encoder);
634 635
		nv_connector = nouveau_connector(nv_encoder->audio.connector);
		nv_crtc = nouveau_crtc(nv_encoder->crtc);
636

637
		if (!nv_crtc || nv_encoder->or != port || nv_crtc->index != dev_id)
638 639
			continue;

640
		*enabled = nv_encoder->audio.enabled;
641 642 643 644 645 646 647
		if (*enabled) {
			ret = drm_eld_size(nv_connector->base.eld);
			memcpy(buf, nv_connector->base.eld,
			       min(max_bytes, ret));
		}
		break;
	}
648

649 650
	mutex_unlock(&drm->audio.lock);

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	return ret;
}

static const struct drm_audio_component_ops nv50_audio_component_ops = {
	.get_eld = nv50_audio_component_get_eld,
};

static int
nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
			  void *data)
{
	struct drm_device *drm_dev = dev_get_drvdata(kdev);
	struct nouveau_drm *drm = nouveau_drm(drm_dev);
	struct drm_audio_component *acomp = data;

	if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
		return -ENOMEM;

669
	drm_modeset_lock_all(drm_dev);
670 671 672
	acomp->ops = &nv50_audio_component_ops;
	acomp->dev = kdev;
	drm->audio.component = acomp;
673 674
	drm_modeset_unlock_all(drm_dev);
	return 0;
675 676 677 678 679 680 681 682 683 684
}

static void
nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
			    void *data)
{
	struct drm_device *drm_dev = dev_get_drvdata(kdev);
	struct nouveau_drm *drm = nouveau_drm(drm_dev);
	struct drm_audio_component *acomp = data;

685
	drm_modeset_lock_all(drm_dev);
686 687 688
	drm->audio.component = NULL;
	acomp->ops = NULL;
	acomp->dev = NULL;
689
	drm_modeset_unlock_all(drm_dev);
690 691 692 693 694 695 696 697 698 699
}

static const struct component_ops nv50_audio_component_bind_ops = {
	.bind   = nv50_audio_component_bind,
	.unbind = nv50_audio_component_unbind,
};

static void
nv50_audio_component_init(struct nouveau_drm *drm)
{
700 701 702 703 704
	if (component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
		return;

	drm->audio.component_registered = true;
	mutex_init(&drm->audio.lock);
705 706 707 708 709
}

static void
nv50_audio_component_fini(struct nouveau_drm *drm)
{
710 711 712 713 714 715
	if (!drm->audio.component_registered)
		return;

	component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
	drm->audio.component_registered = false;
	mutex_destroy(&drm->audio.lock);
716 717
}

718 719 720 721
/******************************************************************************
 * Audio
 *****************************************************************************/
static void
722 723
nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
{
724
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
725 726 727 728 729 730 731 732 733 734 735 736 737
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct nv50_disp *disp = nv50_disp(encoder->dev);
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_sor_hda_eld_v0 eld;
	} args = {
		.base.version = 1,
		.base.method  = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
		.base.hasht   = nv_encoder->dcb->hasht,
		.base.hashm   = (0xf0ff & nv_encoder->dcb->hashm) |
				(0x0100 << nv_crtc->index),
	};

738 739 740 741 742 743 744
	mutex_lock(&drm->audio.lock);
	if (nv_encoder->audio.enabled) {
		nv_encoder->audio.enabled = false;
		nv_encoder->audio.connector = NULL;
		nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
	}
	mutex_unlock(&drm->audio.lock);
745

746 747
	nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
					nv_crtc->index);
748 749 750
}

static void
751 752
nv50_audio_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
		  struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
753
		  struct drm_display_mode *mode)
754
{
755
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
756
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
757
	struct nv50_disp *disp = nv50_disp(encoder->dev);
758 759 760 761 762
	struct __packed {
		struct {
			struct nv50_disp_mthd_v1 mthd;
			struct nv50_disp_sor_hda_eld_v0 eld;
		} base;
763 764
		u8 data[sizeof(nv_connector->base.eld)];
	} args = {
765 766 767
		.base.mthd.version = 1,
		.base.mthd.method  = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
		.base.mthd.hasht   = nv_encoder->dcb->hasht,
B
Ben Skeggs 已提交
768 769
		.base.mthd.hashm   = (0xf0ff & nv_encoder->dcb->hashm) |
				     (0x0100 << nv_crtc->index),
770
	};
771 772 773 774

	if (!drm_detect_monitor_audio(nv_connector->edid))
		return;

775 776
	mutex_lock(&drm->audio.lock);

777
	memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
778

779
	nvif_mthd(&disp->disp->object, 0, &args,
780
		  sizeof(args.base) + drm_eld_size(args.data));
781 782 783 784
	nv_encoder->audio.enabled = true;
	nv_encoder->audio.connector = &nv_connector->base;

	mutex_unlock(&drm->audio.lock);
785

786 787
	nv50_audio_component_eld_notify(drm->audio.component, nv_encoder->or,
					nv_crtc->index);
788 789
}

790 791 792
/******************************************************************************
 * HDMI
 *****************************************************************************/
793
static void
794
nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
795 796
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
797
	struct nv50_disp *disp = nv50_disp(encoder->dev);
798 799
	struct {
		struct nv50_disp_mthd_v1 base;
800
		struct nv50_disp_sor_hdmi_pwr_v0 pwr;
801 802
	} args = {
		.base.version = 1,
803 804 805 806
		.base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
		.base.hasht  = nv_encoder->dcb->hasht,
		.base.hashm  = (0xf0ff & nv_encoder->dcb->hashm) |
			       (0x0100 << nv_crtc->index),
807
	};
808

809
	nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
810 811 812
}

static void
813 814
nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
		 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
815
		 struct drm_display_mode *mode)
816
{
817
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
818
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
819
	struct nv50_disp *disp = nv50_disp(encoder->dev);
820 821 822
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_sor_hdmi_pwr_v0 pwr;
823
		u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
824 825 826 827 828 829 830 831 832
	} args = {
		.base.version = 1,
		.base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
		.base.hasht  = nv_encoder->dcb->hasht,
		.base.hashm  = (0xf0ff & nv_encoder->dcb->hashm) |
			       (0x0100 << nv_crtc->index),
		.pwr.state = 1,
		.pwr.rekey = 56, /* binary driver, and tegra, constant */
	};
833
	struct drm_hdmi_info *hdmi;
834
	u32 max_ac_packet;
835 836
	union hdmi_infoframe avi_frame;
	union hdmi_infoframe vendor_frame;
837
	bool high_tmds_clock_ratio = false, scrambling = false;
838
	u8 config;
839 840
	int ret;
	int size;
841 842 843 844

	if (!drm_detect_hdmi_monitor(nv_connector->edid))
		return;

845 846
	hdmi = &nv_connector->base.display_info.hdmi;

847 848
	ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi,
						       &nv_connector->base, mode);
849
	if (!ret) {
850 851 852
		drm_hdmi_avi_infoframe_quant_range(&avi_frame.avi,
						   &nv_connector->base, mode,
						   HDMI_QUANTIZATION_RANGE_FULL);
853 854 855 856 857
		/* We have an AVI InfoFrame, populate it to the display */
		args.pwr.avi_infoframe_length
			= hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
	}

858 859
	ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
							  &nv_connector->base, mode);
860 861 862 863 864 865 866 867 868
	if (!ret) {
		/* We have a Vendor InfoFrame, populate it to the display */
		args.pwr.vendor_infoframe_length
			= hdmi_infoframe_pack(&vendor_frame,
					      args.infoframes
					      + args.pwr.avi_infoframe_length,
					      17);
	}

869
	max_ac_packet  = mode->htotal - mode->hdisplay;
870
	max_ac_packet -= args.pwr.rekey;
871
	max_ac_packet -= 18; /* constant from tegra */
872
	args.pwr.max_ac_packet = max_ac_packet / 32;
B
Ben Skeggs 已提交
873

874 875 876 877 878 879 880 881 882 883
	if (hdmi->scdc.scrambling.supported) {
		high_tmds_clock_ratio = mode->clock > 340000;
		scrambling = high_tmds_clock_ratio ||
			hdmi->scdc.scrambling.low_rates;
	}

	args.pwr.scdc =
		NV50_DISP_SOR_HDMI_PWR_V0_SCDC_SCRAMBLE * scrambling |
		NV50_DISP_SOR_HDMI_PWR_V0_SCDC_DIV_BY_4 * high_tmds_clock_ratio;

884 885 886 887
	size = sizeof(args.base)
		+ sizeof(args.pwr)
		+ args.pwr.avi_infoframe_length
		+ args.pwr.vendor_infoframe_length;
888
	nvif_mthd(&disp->disp->object, 0, &args, size);
889

890
	nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

	/* If SCDC is supported by the downstream monitor, update
	 * divider / scrambling settings to what we programmed above.
	 */
	if (!hdmi->scdc.scrambling.supported)
		return;

	ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &config);
	if (ret < 0) {
		NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
		return;
	}
	config &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
	config |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 * high_tmds_clock_ratio;
	config |= SCDC_SCRAMBLING_ENABLE * scrambling;
	ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, config);
	if (ret < 0)
		NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
			 config, ret);
910 911
}

912 913 914
/******************************************************************************
 * MST
 *****************************************************************************/
915 916 917 918 919 920 921 922 923 924 925
#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)

struct nv50_mstc {
	struct nv50_mstm *mstm;
	struct drm_dp_mst_port *port;
	struct drm_connector connector;

	struct drm_display_mode *native;
	struct edid *edid;
926 927
};

928 929 930
struct nv50_msto {
	struct drm_encoder encoder;

931
	/* head is statically assigned on msto creation */
932 933 934
	struct nv50_head *head;
	struct nv50_mstc *mstc;
	bool disabled;
935
	bool enabled;
936 937
};

938 939 940 941 942 943 944 945 946 947 948 949 950
struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
{
	struct nv50_msto *msto;

	if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
		return nouveau_encoder(encoder);

	msto = nv50_msto(encoder);
	if (!msto->mstc)
		return NULL;
	return msto->mstc->mstm->outp;
}

951
static void
952 953 954 955
nv50_msto_cleanup(struct drm_atomic_state *state,
		  struct drm_dp_mst_topology_state *mst_state,
		  struct drm_dp_mst_topology_mgr *mgr,
		  struct nv50_msto *msto)
956 957
{
	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
958 959
	struct drm_dp_mst_atomic_payload *payload =
		drm_atomic_get_mst_payload_state(mst_state, msto->mstc->port);
960

961
	NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
962

963 964 965 966 967 968 969
	if (msto->disabled) {
		msto->mstc = NULL;
		msto->disabled = false;
	} else if (msto->enabled) {
		drm_dp_add_payload_part2(mgr, state, payload);
		msto->enabled = false;
	}
970 971 972
}

static void
973 974 975 976
nv50_msto_prepare(struct drm_atomic_state *state,
		  struct drm_dp_mst_topology_state *mst_state,
		  struct drm_dp_mst_topology_mgr *mgr,
		  struct nv50_msto *msto)
977 978 979 980
{
	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
	struct nv50_mstc *mstc = msto->mstc;
	struct nv50_mstm *mstm = mstc->mstm;
981
	struct drm_dp_mst_atomic_payload *payload;
982 983 984 985 986 987 988 989 990 991 992 993
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
	} args = {
		.base.version = 1,
		.base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
		.base.hasht  = mstm->outp->dcb->hasht,
		.base.hashm  = (0xf0ff & mstm->outp->dcb->hashm) |
			       (0x0100 << msto->head->base.index),
	};

	NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

	payload = drm_atomic_get_mst_payload_state(mst_state, mstc->port);

	// TODO: Figure out if we want to do a better job of handling VCPI allocation failures here?
	if (msto->disabled) {
		drm_dp_remove_payload(mgr, mst_state, payload);
	} else {
		if (msto->enabled)
			drm_dp_add_payload_part1(mgr, mst_state, payload);

		args.vcpi.start_slot = payload->vc_start_slot;
		args.vcpi.num_slots = payload->time_slots;
		args.vcpi.pbn = payload->pbn;
		args.vcpi.aligned_pbn = payload->time_slots * mst_state->pbn_div;
1008 1009 1010 1011 1012 1013
	}

	NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
		  msto->encoder.name, msto->head->base.base.name,
		  args.vcpi.start_slot, args.vcpi.num_slots,
		  args.vcpi.pbn, args.vcpi.aligned_pbn);
1014

1015
	nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
1016 1017 1018 1019 1020 1021 1022
}

static int
nv50_msto_atomic_check(struct drm_encoder *encoder,
		       struct drm_crtc_state *crtc_state,
		       struct drm_connector_state *conn_state)
{
1023 1024
	struct drm_atomic_state *state = crtc_state->state;
	struct drm_connector *connector = conn_state->connector;
1025
	struct drm_dp_mst_topology_state *mst_state;
1026
	struct nv50_mstc *mstc = nv50_mstc(connector);
1027
	struct nv50_mstm *mstm = mstc->mstm;
1028
	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
1029
	int slots;
1030
	int ret;
1031

1032 1033 1034 1035
	ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
					  mstc->native);
	if (ret)
		return ret;
1036

1037
	if (!drm_atomic_crtc_needs_modeset(crtc_state))
1038
		return 0;
1039

1040 1041 1042 1043 1044 1045 1046 1047
	/*
	 * When restoring duplicated states, we need to make sure that the bw
	 * remains the same and avoid recalculating it, as the connector's bpc
	 * may have changed after the state was duplicated
	 */
	if (!state->duplicated) {
		const int clock = crtc_state->adjusted_mode.clock;

1048 1049 1050
		asyh->or.bpc = connector->display_info.bpc;
		asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
						    false);
1051
	}
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	mst_state = drm_atomic_get_mst_topology_state(state, &mstm->mgr);
	if (IS_ERR(mst_state))
		return PTR_ERR(mst_state);

	if (!mst_state->pbn_div) {
		struct nouveau_encoder *outp = mstc->mstm->outp;

		mst_state->pbn_div = drm_dp_get_vc_payload_bw(&mstm->mgr,
							      outp->dp.link_bw, outp->dp.link_nr);
	}

	slots = drm_dp_atomic_find_time_slots(state, &mstm->mgr, mstc->port, asyh->dp.pbn);
1065 1066 1067 1068 1069 1070
	if (slots < 0)
		return slots;

	asyh->dp.tu = slots;

	return 0;
1071 1072
}

1073 1074 1075 1076
static u8
nv50_dp_bpc_to_depth(unsigned int bpc)
{
	switch (bpc) {
1077 1078
	case  6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
	case  8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
1079
	case 10:
1080
	default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
1081 1082 1083
	}
}

1084
static void
1085
nv50_msto_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1086 1087
{
	struct nv50_msto *msto = nv50_msto(encoder);
1088 1089 1090
	struct nv50_head *head = msto->head;
	struct nv50_head_atom *asyh =
		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &head->base.base));
1091 1092 1093
	struct nv50_mstc *mstc = NULL;
	struct nv50_mstm *mstm = NULL;
	struct drm_connector *connector;
1094
	struct drm_connector_list_iter conn_iter;
1095
	u8 proto;
1096

1097 1098
	drm_connector_list_iter_begin(encoder->dev, &conn_iter);
	drm_for_each_connector_iter(connector, &conn_iter) {
1099 1100 1101 1102 1103 1104
		if (connector->state->best_encoder == &msto->encoder) {
			mstc = nv50_mstc(connector);
			mstm = mstc->mstm;
			break;
		}
	}
1105
	drm_connector_list_iter_end(&conn_iter);
1106 1107 1108 1109

	if (WARN_ON(!mstc))
		return;

1110
	if (!mstm->links++)
1111
		nv50_outp_acquire(mstm->outp, false /*XXX: MST audio.*/);
1112 1113

	if (mstm->outp->link & 1)
1114
		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1115
	else
1116
		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1117

1118 1119
	mstm->outp->update(mstm->outp, head->base.index, asyh, proto,
			   nv50_dp_bpc_to_depth(asyh->or.bpc));
1120 1121

	msto->mstc = mstc;
1122
	msto->enabled = true;
1123 1124 1125 1126
	mstm->modified = true;
}

static void
1127
nv50_msto_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1128 1129 1130 1131 1132 1133 1134
{
	struct nv50_msto *msto = nv50_msto(encoder);
	struct nv50_mstc *mstc = msto->mstc;
	struct nv50_mstm *mstm = mstc->mstm;

	mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
	mstm->modified = true;
1135 1136
	if (!--mstm->links)
		mstm->disabled = true;
1137 1138 1139 1140 1141
	msto->disabled = true;
}

static const struct drm_encoder_helper_funcs
nv50_msto_help = {
1142 1143
	.atomic_disable = nv50_msto_atomic_disable,
	.atomic_enable = nv50_msto_atomic_enable,
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	.atomic_check = nv50_msto_atomic_check,
};

static void
nv50_msto_destroy(struct drm_encoder *encoder)
{
	struct nv50_msto *msto = nv50_msto(encoder);
	drm_encoder_cleanup(&msto->encoder);
	kfree(msto);
}

static const struct drm_encoder_funcs
nv50_msto = {
	.destroy = nv50_msto_destroy,
};

1160 1161
static struct nv50_msto *
nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1162 1163 1164 1165
{
	struct nv50_msto *msto;
	int ret;

1166 1167 1168
	msto = kzalloc(sizeof(*msto), GFP_KERNEL);
	if (!msto)
		return ERR_PTR(-ENOMEM);
1169 1170

	ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1171
			       DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1172
	if (ret) {
1173 1174
		kfree(msto);
		return ERR_PTR(ret);
1175 1176 1177
	}

	drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1178 1179 1180
	msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
	msto->head = head;
	return msto;
1181 1182 1183 1184
}

static struct drm_encoder *
nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1185
			      struct drm_atomic_state *state)
1186
{
1187 1188
	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
											 connector);
1189
	struct nv50_mstc *mstc = nv50_mstc(connector);
1190
	struct drm_crtc *crtc = connector_state->crtc;
1191

1192 1193 1194 1195
	if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
		return NULL;

	return &nv50_head(crtc)->msto->encoder;
1196 1197 1198 1199 1200 1201
}

static enum drm_mode_status
nv50_mstc_mode_valid(struct drm_connector *connector,
		     struct drm_display_mode *mode)
{
1202 1203 1204 1205 1206 1207 1208 1209
	struct nv50_mstc *mstc = nv50_mstc(connector);
	struct nouveau_encoder *outp = mstc->mstm->outp;

	/* TODO: calculate the PBN from the dotclock and validate against the
	 * MSTB's max possible PBN
	 */

	return nv50_dp_mode_valid(connector, outp, mode, NULL);
1210 1211 1212 1213 1214 1215 1216 1217 1218
}

static int
nv50_mstc_get_modes(struct drm_connector *connector)
{
	struct nv50_mstc *mstc = nv50_mstc(connector);
	int ret = 0;

	mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1219
	drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1220
	if (mstc->edid)
1221 1222
		ret = drm_add_edid_modes(&mstc->connector, mstc->edid);

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
	/*
	 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
	 * to 8 to save bandwidth on the topology. In the future, we'll want
	 * to properly fix this by dynamically selecting the highest possible
	 * bpc that would fit in the topology
	 */
	if (connector->display_info.bpc)
		connector->display_info.bpc =
			clamp(connector->display_info.bpc, 6U, 8U);
	else
		connector->display_info.bpc = 8;
1234 1235 1236 1237 1238 1239 1240

	if (mstc->native)
		drm_mode_destroy(mstc->connector.dev, mstc->native);
	mstc->native = nouveau_conn_native_mode(&mstc->connector);
	return ret;
}

1241 1242
static int
nv50_mstc_atomic_check(struct drm_connector *connector,
1243
		       struct drm_atomic_state *state)
1244 1245 1246 1247
{
	struct nv50_mstc *mstc = nv50_mstc(connector);
	struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;

1248
	return drm_dp_atomic_release_time_slots(state, mgr, mstc->port);
1249 1250
}

1251 1252 1253
static int
nv50_mstc_detect(struct drm_connector *connector,
		 struct drm_modeset_acquire_ctx *ctx, bool force)
1254 1255
{
	struct nv50_mstc *mstc = nv50_mstc(connector);
1256 1257
	int ret;

1258
	if (drm_connector_is_unregistered(connector))
1259
		return connector_status_disconnected;
1260 1261

	ret = pm_runtime_get_sync(connector->dev->dev);
1262 1263
	if (ret < 0 && ret != -EACCES) {
		pm_runtime_put_autosuspend(connector->dev->dev);
1264
		return connector_status_disconnected;
1265
	}
1266

1267 1268
	ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
				     mstc->port);
1269 1270
	if (ret != connector_status_connected)
		goto out;
1271

1272
out:
1273 1274
	pm_runtime_mark_last_busy(connector->dev->dev);
	pm_runtime_put_autosuspend(connector->dev->dev);
1275
	return ret;
1276 1277
}

1278 1279 1280 1281 1282 1283 1284 1285 1286
static const struct drm_connector_helper_funcs
nv50_mstc_help = {
	.get_modes = nv50_mstc_get_modes,
	.mode_valid = nv50_mstc_mode_valid,
	.atomic_best_encoder = nv50_mstc_atomic_best_encoder,
	.atomic_check = nv50_mstc_atomic_check,
	.detect_ctx = nv50_mstc_detect,
};

1287 1288 1289 1290
static void
nv50_mstc_destroy(struct drm_connector *connector)
{
	struct nv50_mstc *mstc = nv50_mstc(connector);
1291

1292
	drm_connector_cleanup(&mstc->connector);
1293
	drm_dp_mst_put_port_malloc(mstc->port);
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	kfree(mstc);
}

static const struct drm_connector_funcs
nv50_mstc = {
	.reset = nouveau_conn_reset,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = nv50_mstc_destroy,
	.atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
	.atomic_destroy_state = nouveau_conn_atomic_destroy_state,
	.atomic_set_property = nouveau_conn_atomic_set_property,
	.atomic_get_property = nouveau_conn_atomic_get_property,
};

static int
nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
	      const char *path, struct nv50_mstc **pmstc)
{
	struct drm_device *dev = mstm->outp->base.base.dev;
1314
	struct drm_crtc *crtc;
1315
	struct nv50_mstc *mstc;
1316
	int ret;
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

	if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
		return -ENOMEM;
	mstc->mstm = mstm;
	mstc->port = port;

	ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
				 DRM_MODE_CONNECTOR_DisplayPort);
	if (ret) {
		kfree(*pmstc);
		*pmstc = NULL;
		return ret;
	}

	drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);

	mstc->connector.funcs->reset(&mstc->connector);
	nouveau_conn_attach_properties(&mstc->connector);

1336 1337 1338 1339 1340 1341 1342
	drm_for_each_crtc(crtc, dev) {
		if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
			continue;

		drm_connector_attach_encoder(&mstc->connector,
					     &nv50_head(crtc)->msto->encoder);
	}
1343 1344 1345

	drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
	drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1346
	drm_connector_set_path_property(&mstc->connector, path);
1347
	drm_dp_mst_get_port_malloc(port);
1348 1349 1350 1351
	return 0;
}

static void
1352 1353 1354
nv50_mstm_cleanup(struct drm_atomic_state *state,
		  struct drm_dp_mst_topology_state *mst_state,
		  struct nv50_mstm *mstm)
1355 1356 1357 1358 1359
{
	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
	struct drm_encoder *encoder;

	NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1360
	drm_dp_check_act_status(&mstm->mgr);
1361 1362 1363 1364 1365 1366

	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
			struct nv50_msto *msto = nv50_msto(encoder);
			struct nv50_mstc *mstc = msto->mstc;
			if (mstc && mstc->mstm == mstm)
1367
				nv50_msto_cleanup(state, mst_state, &mstm->mgr, msto);
1368 1369 1370 1371 1372 1373 1374
		}
	}

	mstm->modified = false;
}

static void
1375 1376 1377
nv50_mstm_prepare(struct drm_atomic_state *state,
		  struct drm_dp_mst_topology_state *mst_state,
		  struct nv50_mstm *mstm)
1378 1379 1380 1381 1382 1383
{
	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
	struct drm_encoder *encoder;

	NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);

1384
	/* Disable payloads first */
1385 1386 1387 1388
	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
			struct nv50_msto *msto = nv50_msto(encoder);
			struct nv50_mstc *mstc = msto->mstc;
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
			if (mstc && mstc->mstm == mstm && msto->disabled)
				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
		}
	}

	/* Add payloads for new heads, while also updating the start slots of any unmodified (but
	 * active) heads that may have had their VC slots shifted left after the previous step
	 */
	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
			struct nv50_msto *msto = nv50_msto(encoder);
			struct nv50_mstc *mstc = msto->mstc;
			if (mstc && mstc->mstm == mstm && !msto->disabled)
				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1403 1404
		}
	}
1405 1406 1407 1408 1409 1410

	if (mstm->disabled) {
		if (!mstm->links)
			nv50_outp_release(mstm->outp);
		mstm->disabled = false;
	}
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
}

static struct drm_connector *
nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
			struct drm_dp_mst_port *port, const char *path)
{
	struct nv50_mstm *mstm = nv50_mstm(mgr);
	struct nv50_mstc *mstc;
	int ret;

	ret = nv50_mstc_new(mstm, port, path, &mstc);
1422
	if (ret)
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
		return NULL;

	return &mstc->connector;
}

static const struct drm_dp_mst_topology_cbs
nv50_mstm = {
	.add_connector = nv50_mstm_add_connector,
};

1433 1434 1435 1436
bool
nv50_mstm_service(struct nouveau_drm *drm,
		  struct nouveau_connector *nv_connector,
		  struct nv50_mstm *mstm)
1437
{
1438 1439 1440
	struct drm_dp_aux *aux = &nv_connector->aux;
	bool handled = true, ret = true;
	int rc;
1441 1442 1443
	u8 esi[8] = {};

	while (handled) {
1444 1445 1446 1447
		rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
		if (rc != 8) {
			ret = false;
			break;
1448 1449 1450 1451 1452 1453
		}

		drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
		if (!handled)
			break;

1454 1455 1456 1457 1458 1459
		rc = drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1],
				       3);
		if (rc != 3) {
			ret = false;
			break;
		}
1460
	}
1461 1462 1463 1464 1465 1466

	if (!ret)
		NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
			 nv_connector->base.name, rc);

	return ret;
1467 1468 1469 1470 1471
}

void
nv50_mstm_remove(struct nv50_mstm *mstm)
{
1472 1473
	mstm->is_mst = false;
	drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1474 1475
}

1476
static int
1477
nv50_mstm_enable(struct nv50_mstm *mstm, int state)
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
{
	struct nouveau_encoder *outp = mstm->outp;
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_sor_dp_mst_link_v0 mst;
	} args = {
		.base.version = 1,
		.base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
		.base.hasht = outp->dcb->hasht,
		.base.hashm = outp->dcb->hashm,
		.mst.state = state,
	};
	struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
1491
	struct nvif_object *disp = &drm->display->disp.object;
1492 1493 1494 1495 1496

	return nvif_mthd(disp, 0, &args, sizeof(args));
}

int
1497
nv50_mstm_detect(struct nouveau_encoder *outp)
1498
{
1499
	struct nv50_mstm *mstm = outp->dp.mstm;
1500 1501
	struct drm_dp_aux *aux;
	int ret;
1502

1503
	if (!mstm || !mstm->can_mst)
1504 1505
		return 0;

1506 1507
	aux = mstm->mgr.aux;

1508 1509 1510 1511 1512 1513
	/* Clear any leftover MST state we didn't set ourselves by first
	 * disabling MST if it was already enabled
	 */
	ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
	if (ret < 0)
		return ret;
1514

1515 1516
	/* And start enabling */
	ret = nv50_mstm_enable(mstm, true);
1517
	if (ret)
1518
		return ret;
1519

1520 1521 1522 1523 1524
	ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
	if (ret) {
		nv50_mstm_enable(mstm, false);
		return ret;
	}
1525

1526 1527
	mstm->is_mst = true;
	return 1;
1528 1529
}

1530
static void
1531
nv50_mstm_fini(struct nouveau_encoder *outp)
1532
{
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
	struct nv50_mstm *mstm = outp->dp.mstm;

	if (!mstm)
		return;

	/* Don't change the MST state of this connector until we've finished
	 * resuming, since we can't safely grab hpd_irq_lock in our resume
	 * path to protect mstm->is_mst without potentially deadlocking
	 */
	mutex_lock(&outp->dp.hpd_irq_lock);
	mstm->suspended = true;
	mutex_unlock(&outp->dp.hpd_irq_lock);

	if (mstm->is_mst)
1547 1548 1549 1550
		drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
}

static void
1551
nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1552
{
1553 1554
	struct nv50_mstm *mstm = outp->dp.mstm;
	int ret = 0;
1555

1556
	if (!mstm)
1557 1558
		return;

1559 1560 1561 1562
	if (mstm->is_mst) {
		ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
		if (ret == -1)
			nv50_mstm_remove(mstm);
1563
	}
1564 1565 1566 1567 1568 1569 1570

	mutex_lock(&outp->dp.hpd_irq_lock);
	mstm->suspended = false;
	mutex_unlock(&outp->dp.hpd_irq_lock);

	if (ret == -1)
		drm_kms_helper_hotplug_event(mstm->mgr.dev);
1571 1572
}

1573 1574 1575 1576 1577
static void
nv50_mstm_del(struct nv50_mstm **pmstm)
{
	struct nv50_mstm *mstm = *pmstm;
	if (mstm) {
1578
		drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
		kfree(*pmstm);
		*pmstm = NULL;
	}
}

static int
nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
	      int conn_base_id, struct nv50_mstm **pmstm)
{
	const int max_payloads = hweight8(outp->dcb->heads);
	struct drm_device *dev = outp->base.base.dev;
	struct nv50_mstm *mstm;
1591
	int ret;
1592 1593 1594 1595

	if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
		return -ENOMEM;
	mstm->outp = outp;
1596
	mstm->mgr.cbs = &nv50_mstm;
1597

1598
	ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1599
					   max_payloads, conn_base_id);
1600 1601 1602 1603 1604 1605
	if (ret)
		return ret;

	return 0;
}

1606 1607 1608
/******************************************************************************
 * SOR
 *****************************************************************************/
1609
static void
1610
nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1611
		struct nv50_head_atom *asyh, u8 proto, u8 depth)
1612
{
1613
	struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1614
	struct nv50_core *core = disp->core;
1615

1616
	if (!asyh) {
1617
		nv_encoder->ctrl &= ~BIT(head);
1618
		if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1619 1620
			nv_encoder->ctrl = 0;
	} else {
1621
		nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1622
		nv_encoder->ctrl |= BIT(head);
1623
		asyh->or.depth = depth;
1624 1625
	}

1626
	core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
1627 1628
}

1629 1630 1631 1632 1633
/* TODO: Should we extend this to PWM-only backlights?
 * As well, should we add a DRM helper for waiting for the backlight to acknowledge
 * the panel backlight has been shut off? Intel doesn't seem to do this, and uses a
 * fixed time delay from the vbios…
 */
1634
static void
1635
nv50_sor_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1636 1637 1638
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1639
	struct nouveau_connector *nv_connector = nv50_outp_get_old_connector(state, nv_encoder);
1640 1641
#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
1642
	struct nouveau_backlight *backlight = nv_connector->backlight;
1643
#endif
1644
	struct drm_dp_aux *aux = &nv_connector->aux;
1645
	int ret;
1646
	u8 pwr;
1647

1648
#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1649 1650 1651 1652 1653 1654
	if (backlight && backlight->uses_dpcd) {
		ret = drm_edp_backlight_disable(aux, &backlight->edp_info);
		if (ret < 0)
			NV_ERROR(drm, "Failed to disable backlight on [CONNECTOR:%d:%s]: %d\n",
				 nv_connector->base.base.id, nv_connector->base.name, ret);
	}
1655
#endif
1656

1657
	if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
1658
		ret = drm_dp_dpcd_readb(aux, DP_SET_POWER, &pwr);
1659

1660 1661 1662 1663
		if (ret == 0) {
			pwr &= ~DP_SET_POWER_MASK;
			pwr |=  DP_SET_POWER_D3;
			drm_dp_dpcd_writeb(aux, DP_SET_POWER, pwr);
1664
		}
1665
	}
1666 1667 1668 1669 1670

	nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
	nv50_audio_disable(encoder, nv_crtc);
	nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
	nv50_outp_release(nv_encoder);
1671
	nv_encoder->crtc = NULL;
1672 1673
}

1674
static void
1675
nv50_sor_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1676
{
1677
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1678 1679 1680
	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
	struct nv50_head_atom *asyh =
		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1681
	struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1682 1683 1684 1685 1686 1687 1688 1689 1690
	struct {
		struct nv50_disp_mthd_v1 base;
		struct nv50_disp_sor_lvds_script_v0 lvds;
	} lvds = {
		.base.version = 1,
		.base.method  = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
		.base.hasht   = nv_encoder->dcb->hasht,
		.base.hashm   = nv_encoder->dcb->hashm,
	};
1691
	struct nv50_disp *disp = nv50_disp(encoder->dev);
1692
	struct drm_device *dev = encoder->dev;
1693
	struct nouveau_drm *drm = nouveau_drm(dev);
1694
	struct nouveau_connector *nv_connector;
1695 1696 1697
#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
	struct nouveau_backlight *backlight;
#endif
1698
	struct nvbios *bios = &drm->vbios;
1699
	bool hda = false;
1700 1701
	u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
	u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1702

1703
	nv_connector = nv50_outp_get_new_connector(state, nv_encoder);
1704
	nv_encoder->crtc = &nv_crtc->base;
1705 1706 1707 1708 1709 1710

	if ((disp->disp->object.oclass == GT214_DISP ||
	     disp->disp->object.oclass >= GF110_DISP) &&
	    drm_detect_monitor_audio(nv_connector->edid))
		hda = true;
	nv50_outp_acquire(nv_encoder, hda);
1711

1712
	switch (nv_encoder->dcb->type) {
1713
	case DCB_OUTPUT_TMDS:
1714
		if (nv_encoder->link & 1) {
1715
			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1716 1717 1718 1719 1720 1721 1722 1723 1724
			/* Only enable dual-link if:
			 *  - Need to (i.e. rate > 165MHz)
			 *  - DCB says we can
			 *  - Not an HDMI monitor, since there's no dual-link
			 *    on HDMI.
			 */
			if (mode->clock >= 165000 &&
			    nv_encoder->dcb->duallink_possible &&
			    !drm_detect_hdmi_monitor(nv_connector->edid))
1725
				proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1726
		} else {
1727
			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1728 1729
		}

1730
		nv50_hdmi_enable(&nv_encoder->base.base, nv_crtc, nv_connector, state, mode);
1731
		break;
1732
	case DCB_OUTPUT_LVDS:
1733
		proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1734

1735 1736
		if (bios->fp_no_ddc) {
			if (bios->fp.dual_link)
1737
				lvds.lvds.script |= 0x0100;
1738
			if (bios->fp.if_is_24bit)
1739
				lvds.lvds.script |= 0x0200;
1740
		} else {
1741
			if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1742
				if (((u8 *)nv_connector->edid)[121] == 2)
1743
					lvds.lvds.script |= 0x0100;
1744 1745
			} else
			if (mode->clock >= bios->fp.duallink_transition_clk) {
1746
				lvds.lvds.script |= 0x0100;
1747
			}
1748

1749
			if (lvds.lvds.script & 0x0100) {
1750
				if (bios->fp.strapless_is_24bit & 2)
1751
					lvds.lvds.script |= 0x0200;
1752 1753
			} else {
				if (bios->fp.strapless_is_24bit & 1)
1754
					lvds.lvds.script |= 0x0200;
1755 1756
			}

1757
			if (asyh->or.bpc == 8)
1758
				lvds.lvds.script |= 0x0200;
1759
		}
1760

1761
		nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
1762
		break;
1763
	case DCB_OUTPUT_DP:
1764
		depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1765

1766
		if (nv_encoder->link & 1)
1767
			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1768
		else
1769
			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1770

1771
		nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
1772 1773 1774 1775 1776 1777 1778 1779

#ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
		backlight = nv_connector->backlight;
		if (backlight && backlight->uses_dpcd)
			drm_edp_backlight_enable(&nv_connector->aux, &backlight->edp_info,
						 (u16)backlight->dev->props.brightness);
#endif

1780
		break;
1781
	default:
B
Ben Skeggs 已提交
1782
		BUG();
1783 1784
		break;
	}
1785

1786
	nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1787 1788
}

1789 1790
static const struct drm_encoder_helper_funcs
nv50_sor_help = {
1791
	.atomic_check = nv50_outp_atomic_check,
1792 1793
	.atomic_enable = nv50_sor_atomic_enable,
	.atomic_disable = nv50_sor_atomic_disable,
1794 1795
};

1796
static void
1797
nv50_sor_destroy(struct drm_encoder *encoder)
1798
{
1799
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
B
Ben Skeggs 已提交
1800 1801 1802

	nvif_outp_dtor(&nv_encoder->outp);

1803
	nv50_mstm_del(&nv_encoder->dp.mstm);
1804
	drm_encoder_cleanup(encoder);
1805 1806 1807 1808

	if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
		mutex_destroy(&nv_encoder->dp.hpd_irq_lock);

1809 1810 1811
	kfree(encoder);
}

1812 1813
static const struct drm_encoder_funcs
nv50_sor_func = {
1814
	.destroy = nv50_sor_destroy,
1815 1816
};

1817
bool nv50_has_mst(struct nouveau_drm *drm)
1818 1819 1820 1821 1822 1823 1824 1825 1826
{
	struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
	u32 data;
	u8 ver, hdr, cnt, len;

	data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
	return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
}

1827
static int
1828
nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1829
{
1830
	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1831
	struct nouveau_drm *drm = nouveau_drm(connector->dev);
1832
	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1833 1834
	struct nouveau_encoder *nv_encoder;
	struct drm_encoder *encoder;
1835
	struct nv50_disp *disp = nv50_disp(connector->dev);
1836
	int type, ret;
1837 1838 1839 1840 1841 1842 1843 1844 1845

	switch (dcbe->type) {
	case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
	case DCB_OUTPUT_TMDS:
	case DCB_OUTPUT_DP:
	default:
		type = DRM_MODE_ENCODER_TMDS;
		break;
	}
1846 1847 1848 1849 1850

	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
	if (!nv_encoder)
		return -ENOMEM;
	nv_encoder->dcb = dcbe;
1851
	nv_encoder->update = nv50_sor_update;
1852

1853 1854 1855
	encoder = to_drm_encoder(nv_encoder);
	encoder->possible_crtcs = dcbe->heads;
	encoder->possible_clones = 0;
1856 1857
	drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
			 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1858
	drm_encoder_helper_add(encoder, &nv50_sor_help);
1859

1860
	drm_connector_attach_encoder(connector, encoder);
1861

1862
	disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1863
	nv50_outp_dump_caps(drm, nv_encoder);
1864

1865 1866 1867
	if (dcbe->type == DCB_OUTPUT_DP) {
		struct nvkm_i2c_aux *aux =
			nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1868

1869 1870
		mutex_init(&nv_encoder->dp.hpd_irq_lock);

1871
		if (aux) {
1872
			if (disp->disp->object.oclass < GF110_DISP) {
1873 1874 1875 1876 1877 1878 1879 1880
				/* HW has no support for address-only
				 * transactions, so we're required to
				 * use custom I2C-over-AUX code.
				 */
				nv_encoder->i2c = &aux->i2c;
			} else {
				nv_encoder->i2c = &nv_connector->aux.ddc;
			}
1881 1882
			nv_encoder->aux = aux;
		}
1883

1884
		if (nv_connector->type != DCB_CONNECTOR_eDP &&
1885 1886 1887
		    nv50_has_mst(drm)) {
			ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
					    16, nv_connector->base.base.id,
1888 1889 1890 1891
					    &nv_encoder->dp.mstm);
			if (ret)
				return ret;
		}
1892 1893 1894 1895 1896 1897 1898
	} else {
		struct nvkm_i2c_bus *bus =
			nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
		if (bus)
			nv_encoder->i2c = &bus->i2c;
	}

B
Ben Skeggs 已提交
1899
	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
1900
}
1901

1902 1903 1904
/******************************************************************************
 * PIOR
 *****************************************************************************/
1905 1906 1907 1908
static int
nv50_pior_atomic_check(struct drm_encoder *encoder,
		       struct drm_crtc_state *crtc_state,
		       struct drm_connector_state *conn_state)
1909
{
1910 1911 1912 1913 1914
	int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
	if (ret)
		return ret;
	crtc_state->adjusted_mode.clock *= 2;
	return 0;
1915 1916 1917
}

static void
1918
nv50_pior_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1919
{
1920
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1921
	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1922
	const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1923 1924

	core->func->pior->ctrl(core, nv_encoder->or, ctrl, NULL);
1925
	nv_encoder->crtc = NULL;
1926
	nv50_outp_release(nv_encoder);
1927 1928 1929
}

static void
1930
nv50_pior_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1931 1932
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1933 1934 1935
	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
	struct nv50_head_atom *asyh =
		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1936
	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1937 1938 1939 1940 1941 1942 1943 1944 1945
	u32 ctrl = 0;

	switch (nv_crtc->index) {
	case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
	case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
	default:
		WARN_ON(1);
		break;
	}
1946

1947
	nv50_outp_acquire(nv_encoder, false);
1948

1949
	switch (asyh->or.bpc) {
1950 1951 1952 1953
	case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
	case  8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
	case  6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
	default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1954 1955 1956 1957 1958
	}

	switch (nv_encoder->dcb->type) {
	case DCB_OUTPUT_TMDS:
	case DCB_OUTPUT_DP:
1959
		ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1960 1961
		break;
	default:
B
Ben Skeggs 已提交
1962
		BUG();
1963 1964 1965
		break;
	}

1966
	core->func->pior->ctrl(core, nv_encoder->or, ctrl, asyh);
1967
	nv_encoder->crtc = &nv_crtc->base;
1968 1969
}

1970 1971
static const struct drm_encoder_helper_funcs
nv50_pior_help = {
1972
	.atomic_check = nv50_pior_atomic_check,
1973 1974
	.atomic_enable = nv50_pior_atomic_enable,
	.atomic_disable = nv50_pior_atomic_disable,
1975 1976
};

1977 1978 1979
static void
nv50_pior_destroy(struct drm_encoder *encoder)
{
B
Ben Skeggs 已提交
1980 1981 1982 1983
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);

	nvif_outp_dtor(&nv_encoder->outp);

1984 1985 1986 1987 1988 1989
	drm_encoder_cleanup(encoder);
	kfree(encoder);
}

static const struct drm_encoder_funcs
nv50_pior_func = {
1990 1991 1992 1993 1994 1995
	.destroy = nv50_pior_destroy,
};

static int
nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
{
1996 1997 1998
	struct drm_device *dev = connector->dev;
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nv50_disp *disp = nv50_disp(dev);
1999
	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
2000 2001 2002
	struct nvkm_i2c_bus *bus = NULL;
	struct nvkm_i2c_aux *aux = NULL;
	struct i2c_adapter *ddc;
2003 2004 2005 2006 2007 2008
	struct nouveau_encoder *nv_encoder;
	struct drm_encoder *encoder;
	int type;

	switch (dcbe->type) {
	case DCB_OUTPUT_TMDS:
2009 2010
		bus  = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
		ddc  = bus ? &bus->i2c : NULL;
2011 2012 2013
		type = DRM_MODE_ENCODER_TMDS;
		break;
	case DCB_OUTPUT_DP:
2014
		aux  = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
2015
		ddc  = aux ? &aux->i2c : NULL;
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
		type = DRM_MODE_ENCODER_TMDS;
		break;
	default:
		return -ENODEV;
	}

	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
	if (!nv_encoder)
		return -ENOMEM;
	nv_encoder->dcb = dcbe;
	nv_encoder->i2c = ddc;
2027
	nv_encoder->aux = aux;
2028 2029 2030 2031

	encoder = to_drm_encoder(nv_encoder);
	encoder->possible_crtcs = dcbe->heads;
	encoder->possible_clones = 0;
2032 2033
	drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
			 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
2034
	drm_encoder_helper_add(encoder, &nv50_pior_help);
2035

2036
	drm_connector_attach_encoder(connector, encoder);
2037 2038

	disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
2039
	nv50_outp_dump_caps(drm, nv_encoder);
2040

B
Ben Skeggs 已提交
2041
	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
2042 2043
}

2044 2045 2046 2047 2048
/******************************************************************************
 * Atomic
 *****************************************************************************/

static void
2049
nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
2050
{
2051 2052
	struct drm_dp_mst_topology_mgr *mgr;
	struct drm_dp_mst_topology_state *mst_state;
2053
	struct nouveau_drm *drm = nouveau_drm(state->dev);
2054
	struct nv50_disp *disp = nv50_disp(drm->dev);
2055
	struct nv50_core *core = disp->core;
2056
	struct nv50_mstm *mstm;
2057
	int i;
2058

2059
	NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
2060

2061 2062 2063 2064
	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
		mstm = nv50_mstm(mgr);
		if (mstm->modified)
			nv50_mstm_prepare(state, mst_state, mstm);
2065 2066
	}

2067 2068 2069 2070 2071
	core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
	core->func->update(core, interlock, true);
	if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
				       disp->core->chan.base.device))
		NV_ERROR(drm, "core notifier timeout\n");
2072

2073 2074 2075 2076
	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
		mstm = nv50_mstm(mgr);
		if (mstm->modified)
			nv50_mstm_cleanup(state, mst_state, mstm);
2077
	}
2078 2079
}

2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
static void
nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
{
	struct drm_plane_state *new_plane_state;
	struct drm_plane *plane;
	int i;

	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
		struct nv50_wndw *wndw = nv50_wndw(plane);
		if (interlock[wndw->interlock.type] & wndw->interlock.data) {
			if (wndw->func->update)
				wndw->func->update(wndw, interlock);
		}
	}
}

2096 2097 2098 2099
static void
nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
{
	struct drm_device *dev = state->dev;
2100
	struct drm_crtc_state *new_crtc_state, *old_crtc_state;
2101
	struct drm_crtc *crtc;
2102
	struct drm_plane_state *new_plane_state;
2103 2104 2105 2106
	struct drm_plane *plane;
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nv50_disp *disp = nv50_disp(dev);
	struct nv50_atom *atom = nv50_atom(state);
2107
	struct nv50_core *core = disp->core;
2108
	struct nv50_outp_atom *outp, *outt;
2109
	u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
2110
	int i;
2111
	bool flushed = false;
2112 2113

	NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2114
	nv50_crc_atomic_stop_reporting(state);
2115 2116
	drm_atomic_helper_wait_for_fences(dev, state, false);
	drm_atomic_helper_wait_for_dependencies(state);
2117
	drm_dp_mst_atomic_wait_for_dependencies(state);
2118
	drm_atomic_helper_update_legacy_modeset_state(dev, state);
2119
	drm_atomic_helper_calc_timestamping_constants(state);
2120 2121 2122 2123 2124

	if (atom->lock_core)
		mutex_lock(&disp->mutex);

	/* Disable head(s). */
2125
	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2126
		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2127 2128 2129 2130
		struct nv50_head *head = nv50_head(crtc);

		NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
			  asyh->clr.mask, asyh->set.mask);
2131 2132 2133

		if (old_crtc_state->active && !new_crtc_state->active) {
			pm_runtime_put_noidle(dev->dev);
2134
			drm_crtc_vblank_off(crtc);
2135
		}
2136 2137 2138

		if (asyh->clr.mask) {
			nv50_head_flush_clr(head, asyh, atom->flush_disable);
2139
			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2140 2141 2142 2143
		}
	}

	/* Disable plane(s). */
2144 2145
	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2146 2147 2148 2149 2150 2151 2152
		struct nv50_wndw *wndw = nv50_wndw(plane);

		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
			  asyw->clr.mask, asyw->set.mask);
		if (!asyw->clr.mask)
			continue;

2153
		nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
	}

	/* Disable output path(s). */
	list_for_each_entry(outp, &atom->outp, head) {
		const struct drm_encoder_helper_funcs *help;
		struct drm_encoder *encoder;

		encoder = outp->encoder;
		help = encoder->helper_private;

		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
			  outp->clr.mask, outp->set.mask);

		if (outp->clr.mask) {
2168
			help->atomic_disable(encoder, state);
2169
			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2170
			if (outp->flush_disable) {
2171 2172
				nv50_disp_atomic_commit_wndw(state, interlock);
				nv50_disp_atomic_commit_core(state, interlock);
2173
				memset(interlock, 0x00, sizeof(interlock));
2174 2175

				flushed = true;
2176 2177 2178 2179 2180
			}
		}
	}

	/* Flush disable. */
2181
	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2182
		if (atom->flush_disable) {
2183 2184
			nv50_disp_atomic_commit_wndw(state, interlock);
			nv50_disp_atomic_commit_core(state, interlock);
2185
			memset(interlock, 0x00, sizeof(interlock));
2186 2187

			flushed = true;
2188 2189 2190
		}
	}

2191 2192 2193
	if (flushed)
		nv50_crc_atomic_release_notifier_contexts(state);
	nv50_crc_atomic_init_notifier_contexts(state);
2194

2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
	/* Update output path(s). */
	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
		const struct drm_encoder_helper_funcs *help;
		struct drm_encoder *encoder;

		encoder = outp->encoder;
		help = encoder->helper_private;

		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
			  outp->set.mask, outp->clr.mask);

		if (outp->set.mask) {
2207
			help->atomic_enable(encoder, state);
2208
			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2209 2210 2211 2212 2213 2214 2215
		}

		list_del(&outp->head);
		kfree(outp);
	}

	/* Update head(s). */
2216
	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2217
		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2218 2219 2220 2221 2222 2223 2224
		struct nv50_head *head = nv50_head(crtc);

		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
			  asyh->set.mask, asyh->clr.mask);

		if (asyh->set.mask) {
			nv50_head_flush_set(head, asyh);
2225
			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2226 2227
		}

2228
		if (new_crtc_state->active) {
2229
			if (!old_crtc_state->active) {
2230
				drm_crtc_vblank_on(crtc);
2231 2232
				pm_runtime_get_noresume(dev->dev);
			}
2233
			if (new_crtc_state->event)
2234 2235
				drm_crtc_vblank_get(crtc);
		}
2236 2237
	}

2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
	/* Update window->head assignment.
	 *
	 * This has to happen in an update that's not interlocked with
	 * any window channels to avoid hitting HW error checks.
	 *
	 *TODO: Proper handling of window ownership (Turing apparently
	 *      supports non-fixed mappings).
	 */
	if (core->assign_windows) {
		core->func->wndw.owner(core);
2248
		nv50_disp_atomic_commit_core(state, interlock);
2249 2250 2251 2252
		core->assign_windows = false;
		interlock[NV50_DISP_INTERLOCK_CORE] = 0;
	}

2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
	/* Finish updating head(s)...
	 *
	 * NVD is rather picky about both where window assignments can change,
	 * *and* about certain core and window channel states matching.
	 *
	 * The EFI GOP driver on newer GPUs configures window channels with a
	 * different output format to what we do, and the core channel update
	 * in the assign_windows case above would result in a state mismatch.
	 *
	 * Delay some of the head update until after that point to workaround
	 * the issue.  This only affects the initial modeset.
	 *
	 * TODO: handle this better when adding flexible window mapping
	 */
	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
		struct nv50_head *head = nv50_head(crtc);

		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
			  asyh->set.mask, asyh->clr.mask);

		if (asyh->set.mask) {
			nv50_head_flush_set_wndw(head, asyh);
			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
		}
	}

2280
	/* Update plane(s). */
2281 2282
	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2283 2284 2285 2286 2287 2288 2289 2290
		struct nv50_wndw *wndw = nv50_wndw(plane);

		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
			  asyw->set.mask, asyw->clr.mask);
		if ( !asyw->set.mask &&
		    (!asyw->clr.mask || atom->flush_disable))
			continue;

2291
		nv50_wndw_flush_set(wndw, interlock, asyw);
2292 2293 2294
	}

	/* Flush update. */
2295
	nv50_disp_atomic_commit_wndw(state, interlock);
2296

2297 2298
	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
		if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2299 2300
		    interlock[NV50_DISP_INTERLOCK_OVLY] ||
		    interlock[NV50_DISP_INTERLOCK_WNDW] ||
2301
		    !atom->state.legacy_cursor_update)
2302
			nv50_disp_atomic_commit_core(state, interlock);
2303
		else
2304
			disp->core->func->update(disp->core, interlock, false);
2305 2306 2307 2308 2309 2310
	}

	if (atom->lock_core)
		mutex_unlock(&disp->mutex);

	/* Wait for HW to signal completion. */
2311 2312
	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2313 2314 2315 2316 2317 2318
		struct nv50_wndw *wndw = nv50_wndw(plane);
		int ret = nv50_wndw_wait_armed(wndw, asyw);
		if (ret)
			NV_ERROR(drm, "%s: timeout\n", plane->name);
	}

2319 2320
	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
		if (new_crtc_state->event) {
2321
			unsigned long flags;
2322
			/* Get correct count/ts if racing with vblank irq */
2323
			if (new_crtc_state->active)
2324
				drm_crtc_accurate_vblank_count(crtc);
2325
			spin_lock_irqsave(&crtc->dev->event_lock, flags);
2326
			drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2327
			spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2328

2329
			new_crtc_state->event = NULL;
2330
			if (new_crtc_state->active)
2331
				drm_crtc_vblank_put(crtc);
2332 2333 2334
		}
	}

2335
	nv50_crc_atomic_start_reporting(state);
2336 2337
	if (!flushed)
		nv50_crc_atomic_release_notifier_contexts(state);
2338

2339 2340 2341 2342
	drm_atomic_helper_commit_hw_done(state);
	drm_atomic_helper_cleanup_planes(dev, state);
	drm_atomic_helper_commit_cleanup_done(state);
	drm_atomic_state_put(state);
2343 2344 2345 2346

	/* Drop the RPM ref we got from nv50_disp_atomic_commit() */
	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
}

static void
nv50_disp_atomic_commit_work(struct work_struct *work)
{
	struct drm_atomic_state *state =
		container_of(work, typeof(*state), commit_work);
	nv50_disp_atomic_commit_tail(state);
}

static int
nv50_disp_atomic_commit(struct drm_device *dev,
			struct drm_atomic_state *state, bool nonblock)
{
2361
	struct drm_plane_state *new_plane_state;
2362 2363 2364 2365
	struct drm_plane *plane;
	int ret, i;

	ret = pm_runtime_get_sync(dev->dev);
2366 2367
	if (ret < 0 && ret != -EACCES) {
		pm_runtime_put_autosuspend(dev->dev);
2368
		return ret;
2369
	}
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383

	ret = drm_atomic_helper_setup_commit(state, nonblock);
	if (ret)
		goto done;

	INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);

	ret = drm_atomic_helper_prepare_planes(dev, state);
	if (ret)
		goto done;

	if (!nonblock) {
		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
		if (ret)
2384
			goto err_cleanup;
2385 2386
	}

2387 2388 2389 2390
	ret = drm_atomic_helper_swap_state(state, true);
	if (ret)
		goto err_cleanup;

2391 2392
	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2393
		struct nv50_wndw *wndw = nv50_wndw(plane);
2394

2395 2396
		if (asyw->set.image)
			nv50_wndw_ntfy_enable(wndw, asyw);
2397 2398 2399 2400
	}

	drm_atomic_state_get(state);

2401 2402 2403 2404 2405 2406
	/*
	 * Grab another RPM ref for the commit tail, which will release the
	 * ref when it's finished
	 */
	pm_runtime_get_noresume(dev->dev);

2407 2408 2409 2410 2411
	if (nonblock)
		queue_work(system_unbound_wq, &state->commit_work);
	else
		nv50_disp_atomic_commit_tail(state);

2412 2413 2414
err_cleanup:
	if (ret)
		drm_atomic_helper_cleanup_planes(dev, state);
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
done:
	pm_runtime_put_autosuspend(dev->dev);
	return ret;
}

static struct nv50_outp_atom *
nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
{
	struct nv50_outp_atom *outp;

	list_for_each_entry(outp, &atom->outp, head) {
		if (outp->encoder == encoder)
			return outp;
	}

	outp = kzalloc(sizeof(*outp), GFP_KERNEL);
	if (!outp)
		return ERR_PTR(-ENOMEM);

	list_add(&outp->head, &atom->outp);
	outp->encoder = encoder;
	return outp;
}

static int
nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2441
				struct drm_connector_state *old_connector_state)
2442
{
2443 2444
	struct drm_encoder *encoder = old_connector_state->best_encoder;
	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2445 2446 2447
	struct drm_crtc *crtc;
	struct nv50_outp_atom *outp;

2448
	if (!(crtc = old_connector_state->crtc))
2449 2450
		return 0;

2451 2452 2453
	old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
	if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
		outp = nv50_disp_outp_atomic_add(atom, encoder);
		if (IS_ERR(outp))
			return PTR_ERR(outp);

		if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
			outp->flush_disable = true;
			atom->flush_disable = true;
		}
		outp->clr.ctrl = true;
		atom->lock_core = true;
	}

	return 0;
}

static int
nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
				struct drm_connector_state *connector_state)
{
	struct drm_encoder *encoder = connector_state->best_encoder;
2474
	struct drm_crtc_state *new_crtc_state;
2475 2476 2477 2478 2479 2480
	struct drm_crtc *crtc;
	struct nv50_outp_atom *outp;

	if (!(crtc = connector_state->crtc))
		return 0;

2481 2482
	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
	if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
		outp = nv50_disp_outp_atomic_add(atom, encoder);
		if (IS_ERR(outp))
			return PTR_ERR(outp);

		outp->set.ctrl = true;
		atom->lock_core = true;
	}

	return 0;
}

static int
nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
{
	struct nv50_atom *atom = nv50_atom(state);
2498
	struct nv50_core *core = nv50_disp(dev)->core;
2499
	struct drm_connector_state *old_connector_state, *new_connector_state;
2500
	struct drm_connector *connector;
2501 2502
	struct drm_crtc_state *new_crtc_state;
	struct drm_crtc *crtc;
2503 2504
	struct nv50_head *head;
	struct nv50_head_atom *asyh;
2505 2506
	int ret, i;

2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
	if (core->assign_windows && core->func->head->static_wndw_map) {
		drm_for_each_crtc(crtc, dev) {
			new_crtc_state = drm_atomic_get_crtc_state(state,
								   crtc);
			if (IS_ERR(new_crtc_state))
				return PTR_ERR(new_crtc_state);

			head = nv50_head(crtc);
			asyh = nv50_head_atom(new_crtc_state);
			core->func->head->static_wndw_map(head, asyh);
		}
	}

2520 2521 2522 2523 2524 2525 2526 2527 2528
	/* We need to handle colour management on a per-plane basis. */
	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
		if (new_crtc_state->color_mgmt_changed) {
			ret = drm_atomic_add_affected_planes(state, crtc);
			if (ret)
				return ret;
		}
	}

2529 2530 2531 2532
	ret = drm_atomic_helper_check(dev, state);
	if (ret)
		return ret;

2533 2534
	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
		ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2535 2536 2537
		if (ret)
			return ret;

2538
		ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2539 2540 2541 2542
		if (ret)
			return ret;
	}

2543 2544 2545 2546
	ret = drm_dp_mst_atomic_check(state);
	if (ret)
		return ret;

2547 2548
	nv50_crc_atomic_check_outp(atom);

2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
	return 0;
}

static void
nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
{
	struct nv50_atom *atom = nv50_atom(state);
	struct nv50_outp_atom *outp, *outt;

	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
		list_del(&outp->head);
		kfree(outp);
	}

	drm_atomic_state_default_clear(state);
}

static void
nv50_disp_atomic_state_free(struct drm_atomic_state *state)
{
	struct nv50_atom *atom = nv50_atom(state);
	drm_atomic_state_default_release(&atom->state);
	kfree(atom);
}

static struct drm_atomic_state *
nv50_disp_atomic_state_alloc(struct drm_device *dev)
{
	struct nv50_atom *atom;
	if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
	    drm_atomic_state_init(dev, &atom->state) < 0) {
		kfree(atom);
		return NULL;
	}
	INIT_LIST_HEAD(&atom->outp);
	return &atom->state;
}

static const struct drm_mode_config_funcs
nv50_disp_func = {
	.fb_create = nouveau_user_framebuffer_create,
2590
	.output_poll_changed = nouveau_fbcon_output_poll_changed,
2591 2592 2593 2594 2595 2596 2597
	.atomic_check = nv50_disp_atomic_check,
	.atomic_commit = nv50_disp_atomic_commit,
	.atomic_state_alloc = nv50_disp_atomic_state_alloc,
	.atomic_state_clear = nv50_disp_atomic_state_clear,
	.atomic_state_free = nv50_disp_atomic_state_free,
};

2598 2599 2600 2601 2602
static const struct drm_mode_config_helper_funcs
nv50_disp_helper_func = {
	.atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
};

2603 2604 2605
/******************************************************************************
 * Init
 *****************************************************************************/
2606

2607
static void
2608
nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2609
{
2610
	struct nouveau_drm *drm = nouveau_drm(dev);
2611 2612 2613
	struct drm_encoder *encoder;

	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2614 2615
		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
			nv50_mstm_fini(nouveau_encoder(encoder));
2616
	}
2617 2618 2619

	if (!runtime)
		cancel_work_sync(&drm->hpd_work);
2620 2621
}

2622
static int
2623
nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2624
{
2625
	struct nv50_core *core = nv50_disp(dev)->core;
2626
	struct drm_encoder *encoder;
2627

2628 2629
	if (resume || runtime)
		core->func->init(core);
2630

2631 2632
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2633 2634
			struct nouveau_encoder *nv_encoder =
				nouveau_encoder(encoder);
2635
			nv50_mstm_init(nv_encoder, runtime);
2636 2637 2638
		}
	}

2639
	return 0;
2640 2641
}

2642
static void
2643
nv50_display_destroy(struct drm_device *dev)
2644
{
2645
	struct nv50_disp *disp = nv50_disp(dev);
2646

2647 2648
	nv50_audio_component_fini(nouveau_drm(dev));

2649
	nvif_object_unmap(&disp->caps);
2650
	nvif_object_dtor(&disp->caps);
2651
	nv50_core_del(&disp->core);
2652

2653
	nouveau_bo_unmap(disp->sync);
2654 2655
	if (disp->sync)
		nouveau_bo_unpin(disp->sync);
2656
	nouveau_bo_ref(NULL, &disp->sync);
2657

2658
	nouveau_display(dev)->priv = NULL;
2659 2660 2661 2662
	kfree(disp);
}

int
2663
nv50_display_create(struct drm_device *dev)
2664
{
2665
	struct nvif_device *device = &nouveau_drm(dev)->client.device;
2666 2667
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct dcb_table *dcb = &drm->vbios.dcb;
2668
	struct drm_connector *connector, *tmp;
2669
	struct nv50_disp *disp;
2670
	struct dcb_output *dcbe;
2671
	int crtcs, ret, i;
2672
	bool has_mst = nv50_has_mst(drm);
2673 2674 2675 2676

	disp = kzalloc(sizeof(*disp), GFP_KERNEL);
	if (!disp)
		return -ENOMEM;
2677

2678 2679
	mutex_init(&disp->mutex);

2680
	nouveau_display(dev)->priv = disp;
2681 2682 2683
	nouveau_display(dev)->dtor = nv50_display_destroy;
	nouveau_display(dev)->init = nv50_display_init;
	nouveau_display(dev)->fini = nv50_display_fini;
2684
	disp->disp = &nouveau_display(dev)->disp;
2685
	dev->mode_config.funcs = &nv50_disp_func;
2686
	dev->mode_config.helper_private = &nv50_disp_helper_func;
2687
	dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2688
	dev->mode_config.normalize_zpos = true;
2689

2690
	/* small shared memory area we use for notifiers and semaphores */
2691 2692
	ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
			     NOUVEAU_GEM_DOMAIN_VRAM,
2693
			     0, 0x0000, NULL, NULL, &disp->sync);
2694
	if (!ret) {
2695
		ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2696
		if (!ret) {
2697
			ret = nouveau_bo_map(disp->sync);
2698 2699 2700
			if (ret)
				nouveau_bo_unpin(disp->sync);
		}
2701 2702 2703 2704 2705 2706 2707 2708
		if (ret)
			nouveau_bo_ref(NULL, &disp->sync);
	}

	if (ret)
		goto out;

	/* allocate master evo channel */
2709
	ret = nv50_core_new(drm, &disp->core);
2710 2711 2712
	if (ret)
		goto out;

2713
	disp->core->func->init(disp->core);
2714 2715 2716 2717 2718
	if (disp->core->func->caps_init) {
		ret = disp->core->func->caps_init(drm, disp);
		if (ret)
			goto out;
	}
2719

2720 2721 2722 2723
	/* Assign the correct format modifiers */
	if (disp->disp->object.oclass >= TU102_DISP)
		nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
	else
2724
	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2725 2726 2727 2728
		nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
	else
		nouveau_display(dev)->format_modifiers = disp50xx_modifiers;

2729 2730 2731 2732 2733 2734 2735 2736 2737
	/* FIXME: 256x256 cursors are supported on Kepler, however unlike Maxwell and later
	 * generations Kepler requires that we use small pages (4K) for cursor scanout surfaces. The
	 * proper fix for this is to teach nouveau to migrate fbs being used for the cursor plane to
	 * small page allocations in prepare_fb(). When this is implemented, we should also force
	 * large pages (128K) for ovly fbs in order to fix Kepler ovlys.
	 * But until then, just limit cursors to 128x128 - which is small enough to avoid ever using
	 * large pages.
	 */
	if (disp->disp->object.oclass >= GM107_DISP) {
2738 2739
		dev->mode_config.cursor_width = 256;
		dev->mode_config.cursor_height = 256;
2740 2741 2742
	} else if (disp->disp->object.oclass >= GK104_DISP) {
		dev->mode_config.cursor_width = 128;
		dev->mode_config.cursor_height = 128;
2743 2744 2745 2746 2747
	} else {
		dev->mode_config.cursor_width = 64;
		dev->mode_config.cursor_height = 64;
	}

2748
	/* create crtc objects to represent the hw heads */
2749 2750 2751
	if (disp->disp->object.oclass >= GV100_DISP)
		crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
	else
2752
	if (disp->disp->object.oclass >= GF110_DISP)
2753
		crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2754
	else
2755
		crtcs = 0x3;
2756

2757
	for (i = 0; i < fls(crtcs); i++) {
2758 2759
		struct nv50_head *head;

2760 2761
		if (!(crtcs & (1 << i)))
			continue;
2762 2763 2764 2765

		head = nv50_head_create(dev, i);
		if (IS_ERR(head)) {
			ret = PTR_ERR(head);
2766
			goto out;
2767 2768 2769 2770 2771 2772 2773 2774 2775
		}

		if (has_mst) {
			head->msto = nv50_msto_new(dev, head, i);
			if (IS_ERR(head->msto)) {
				ret = PTR_ERR(head->msto);
				head->msto = NULL;
				goto out;
			}
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787

			/*
			 * FIXME: This is a hack to workaround the following
			 * issues:
			 *
			 * https://gitlab.gnome.org/GNOME/mutter/issues/759
			 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
			 *
			 * Once these issues are closed, this should be
			 * removed
			 */
			head->msto->encoder.possible_crtcs = crtcs;
2788
		}
2789 2790
	}

2791 2792
	/* create encoder/connector objects based on VBIOS DCB table */
	for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2793
		connector = nouveau_connector_create(dev, dcbe);
2794 2795 2796
		if (IS_ERR(connector))
			continue;

2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
		if (dcbe->location == DCB_LOC_ON_CHIP) {
			switch (dcbe->type) {
			case DCB_OUTPUT_TMDS:
			case DCB_OUTPUT_LVDS:
			case DCB_OUTPUT_DP:
				ret = nv50_sor_create(connector, dcbe);
				break;
			case DCB_OUTPUT_ANALOG:
				ret = nv50_dac_create(connector, dcbe);
				break;
			default:
				ret = -ENODEV;
				break;
			}
		} else {
			ret = nv50_pior_create(connector, dcbe);
2813 2814
		}

2815 2816 2817 2818
		if (ret) {
			NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
				     dcbe->location, dcbe->type,
				     ffs(dcbe->or) - 1, ret);
2819
			ret = 0;
2820 2821 2822 2823 2824
		}
	}

	/* cull any connectors we created that don't have an encoder */
	list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2825
		if (connector->possible_encoders)
2826 2827
			continue;

2828
		NV_WARN(drm, "%s has no encoders, removing\n",
2829
			connector->name);
2830 2831 2832
		connector->funcs->destroy(connector);
	}

2833 2834 2835
	/* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
	dev->vblank_disable_immediate = true;

2836 2837
	nv50_audio_component_init(drm);

2838 2839
out:
	if (ret)
2840
		nv50_display_destroy(dev);
2841 2842
	return ret;
}
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892

/******************************************************************************
 * Format modifiers
 *****************************************************************************/

/****************************************************************
 *            Log2(block height) ----------------------------+  *
 *            Page Kind ----------------------------------+  |  *
 *            Gob Height/Page Kind Generation ------+     |  |  *
 *                          Sector layout -------+  |     |  |  *
 *                          Compression ------+  |  |     |  |  */
const u64 disp50xx_modifiers[] = { /*         |  |  |     |  |  */
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};

/****************************************************************
 *            Log2(block height) ----------------------------+  *
 *            Page Kind ----------------------------------+  |  *
 *            Gob Height/Page Kind Generation ------+     |  |  *
 *                          Sector layout -------+  |     |  |  *
 *                          Compression ------+  |  |     |  |  */
const u64 disp90xx_modifiers[] = { /*         |  |  |     |  |  */
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
	DRM_FORMAT_MOD_LINEAR,
	DRM_FORMAT_MOD_INVALID
};