xref: /linux/drivers/gpu/drm/nouveau/dispnv50/disp.c (revision a4871e6201c46c8e1d04308265b4b4c5753c8209)
1 /*
2  * Copyright 2011 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 #include "disp.h"
25 #include "atom.h"
26 #include "core.h"
27 #include "head.h"
28 #include "wndw.h"
29 #include "handles.h"
30 
31 #include <linux/backlight.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/hdmi.h>
34 #include <linux/component.h>
35 #include <linux/iopoll.h>
36 
37 #include <drm/display/drm_dp_helper.h>
38 #include <drm/display/drm_scdc_helper.h>
39 #include <drm/drm_atomic.h>
40 #include <drm/drm_atomic_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_eld.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_fixed.h>
45 #include <drm/drm_probe_helper.h>
46 #include <drm/drm_vblank.h>
47 
48 #include <nvif/push507c.h>
49 
50 #include <nvif/class.h>
51 #include <nvif/cl0002.h>
52 #include <nvif/event.h>
53 #include <nvif/if0012.h>
54 #include <nvif/if0014.h>
55 #include <nvif/timer.h>
56 
57 #include <nvhw/class/cl507c.h>
58 #include <nvhw/class/cl507d.h>
59 #include <nvhw/class/cl837d.h>
60 #include <nvhw/class/cl887d.h>
61 #include <nvhw/class/cl907d.h>
62 #include <nvhw/class/cl917d.h>
63 
64 #include "nouveau_drv.h"
65 #include "nouveau_dma.h"
66 #include "nouveau_gem.h"
67 #include "nouveau_connector.h"
68 #include "nouveau_encoder.h"
69 #include "nouveau_fence.h"
70 #include "nv50_display.h"
71 
72 /******************************************************************************
73  * EVO channel
74  *****************************************************************************/
75 
76 static int
77 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
78 		 const s32 *oclass, u8 head, void *data, u32 size,
79 		 struct nv50_chan *chan)
80 {
81 	struct nvif_sclass *sclass;
82 	int ret, i, n;
83 
84 	chan->device = device;
85 
86 	ret = n = nvif_object_sclass_get(disp, &sclass);
87 	if (ret < 0)
88 		return ret;
89 
90 	while (oclass[0]) {
91 		for (i = 0; i < n; i++) {
92 			if (sclass[i].oclass == oclass[0]) {
93 				ret = nvif_object_ctor(disp, "kmsChan", 0,
94 						       oclass[0], data, size,
95 						       &chan->user);
96 				if (ret == 0) {
97 					ret = nvif_object_map(&chan->user, NULL, 0);
98 					if (ret)
99 						nvif_object_dtor(&chan->user);
100 				}
101 				nvif_object_sclass_put(&sclass);
102 				return ret;
103 			}
104 		}
105 		oclass++;
106 	}
107 
108 	nvif_object_sclass_put(&sclass);
109 	return -ENOSYS;
110 }
111 
112 static void
113 nv50_chan_destroy(struct nv50_chan *chan)
114 {
115 	nvif_object_dtor(&chan->user);
116 }
117 
118 /******************************************************************************
119  * DMA EVO channel
120  *****************************************************************************/
121 
122 void
123 nv50_dmac_destroy(struct nv50_dmac *dmac)
124 {
125 	nvif_object_dtor(&dmac->vram);
126 	nvif_object_dtor(&dmac->sync);
127 
128 	nv50_chan_destroy(&dmac->base);
129 
130 	nvif_mem_dtor(&dmac->push.mem);
131 }
132 
133 static void
134 nv50_dmac_kick(struct nvif_push *push)
135 {
136 	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), push);
137 
138 	dmac->cur = push->cur - (u32 __iomem *)dmac->push.mem.object.map.ptr;
139 	if (dmac->put != dmac->cur) {
140 		/* Push buffer fetches are not coherent with BAR1, we need to ensure
141 		 * writes have been flushed right through to VRAM before writing PUT.
142 		 */
143 		if (dmac->push.mem.type & NVIF_MEM_VRAM) {
144 			struct nvif_device *device = dmac->base.device;
145 			nvif_wr32(&device->object, 0x070000, 0x00000001);
146 			nvif_msec(device, 2000,
147 				if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
148 					break;
149 			);
150 		}
151 
152 		NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
153 		dmac->put = dmac->cur;
154 	}
155 
156 	push->bgn = push->cur;
157 }
158 
159 static int
160 nv50_dmac_free(struct nv50_dmac *dmac)
161 {
162 	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
163 	if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
164 		return get - dmac->cur - 5;
165 	return dmac->max - dmac->cur;
166 }
167 
168 static int
169 nv50_dmac_wind(struct nv50_dmac *dmac)
170 {
171 	/* Wait for GET to depart from the beginning of the push buffer to
172 	 * prevent writing PUT == GET, which would be ignored by HW.
173 	 */
174 	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
175 	if (get == 0) {
176 		/* Corner-case, HW idle, but non-committed work pending. */
177 		if (dmac->put == 0)
178 			nv50_dmac_kick(&dmac->push);
179 
180 		if (nvif_msec(dmac->base.device, 2000,
181 			if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
182 				break;
183 		) < 0)
184 			return -ETIMEDOUT;
185 	}
186 
187 	PUSH_RSVD(&dmac->push, PUSH_JUMP(&dmac->push, 0));
188 	dmac->cur = 0;
189 	return 0;
190 }
191 
192 static int
193 nv50_dmac_wait(struct nvif_push *push, u32 size)
194 {
195 	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), push);
196 	int free;
197 
198 	if (WARN_ON(size > dmac->max))
199 		return -EINVAL;
200 
201 	dmac->cur = push->cur - (u32 __iomem *)dmac->push.mem.object.map.ptr;
202 	if (dmac->cur + size >= dmac->max) {
203 		int ret = nv50_dmac_wind(dmac);
204 		if (ret)
205 			return ret;
206 
207 		push->cur = dmac->push.mem.object.map.ptr;
208 		push->cur = push->cur + dmac->cur;
209 		nv50_dmac_kick(push);
210 	}
211 
212 	if (nvif_msec(dmac->base.device, 2000,
213 		if ((free = nv50_dmac_free(dmac)) >= size)
214 			break;
215 	) < 0) {
216 		WARN_ON(1);
217 		return -ETIMEDOUT;
218 	}
219 
220 	push->bgn = dmac->push.mem.object.map.ptr;
221 	push->bgn = push->bgn + dmac->cur;
222 	push->cur = push->bgn;
223 	push->end = push->cur + free;
224 	return 0;
225 }
226 
227 MODULE_PARM_DESC(kms_vram_pushbuf, "Place EVO/NVD push buffers in VRAM (default: auto)");
228 static int nv50_dmac_vram_pushbuf = -1;
229 module_param_named(kms_vram_pushbuf, nv50_dmac_vram_pushbuf, int, 0400);
230 
231 int
232 nv50_dmac_create(struct nouveau_drm *drm,
233 		 const s32 *oclass, u8 head, void *data, u32 size, s64 syncbuf,
234 		 struct nv50_dmac *dmac)
235 {
236 	struct nvif_device *device = &drm->device;
237 	struct nvif_object *disp = &drm->display->disp.object;
238 	struct nvif_disp_chan_v0 *args = data;
239 	u8 type = NVIF_MEM_COHERENT;
240 	int ret;
241 
242 	/* Pascal added support for 47-bit physical addresses, but some
243 	 * parts of EVO still only accept 40-bit PAs.
244 	 *
245 	 * To avoid issues on systems with large amounts of RAM, and on
246 	 * systems where an IOMMU maps pages at a high address, we need
247 	 * to allocate push buffers in VRAM instead.
248 	 *
249 	 * This appears to match NVIDIA's behaviour on Pascal.
250 	 */
251 	if ((nv50_dmac_vram_pushbuf > 0) ||
252 	    (nv50_dmac_vram_pushbuf < 0 && device->info.family == NV_DEVICE_INFO_V0_PASCAL))
253 		type |= NVIF_MEM_VRAM;
254 
255 	ret = nvif_mem_ctor_map(&drm->mmu, "kmsChanPush", type, 0x1000, &dmac->push.mem);
256 	if (ret)
257 		return ret;
258 
259 	dmac->push.wait = nv50_dmac_wait;
260 	dmac->push.kick = nv50_dmac_kick;
261 	dmac->push.bgn = dmac->push.mem.object.map.ptr;
262 	dmac->push.cur = dmac->push.bgn;
263 	dmac->push.end = dmac->push.bgn;
264 	dmac->max = 0x1000/4 - 1;
265 
266 	/* EVO channels are affected by a HW bug where the last 12 DWORDs
267 	 * of the push buffer aren't able to be used safely.
268 	 */
269 	if (disp->oclass < GV100_DISP)
270 		dmac->max -= 12;
271 
272 	args->pushbuf = nvif_handle(&dmac->push.mem.object);
273 
274 	ret = nv50_chan_create(device, disp, oclass, head, data, size,
275 			       &dmac->base);
276 	if (ret)
277 		return ret;
278 
279 	if (syncbuf < 0)
280 		return 0;
281 
282 	ret = nvif_object_ctor(&dmac->base.user, "kmsSyncCtxDma", NV50_DISP_HANDLE_SYNCBUF,
283 			       NV_DMA_IN_MEMORY,
284 			       &(struct nv_dma_v0) {
285 					.target = NV_DMA_V0_TARGET_VRAM,
286 					.access = NV_DMA_V0_ACCESS_RDWR,
287 					.start = syncbuf + 0x0000,
288 					.limit = syncbuf + 0x0fff,
289 			       }, sizeof(struct nv_dma_v0),
290 			       &dmac->sync);
291 	if (ret)
292 		return ret;
293 
294 	ret = nvif_object_ctor(&dmac->base.user, "kmsVramCtxDma", NV50_DISP_HANDLE_VRAM,
295 			       NV_DMA_IN_MEMORY,
296 			       &(struct nv_dma_v0) {
297 					.target = NV_DMA_V0_TARGET_VRAM,
298 					.access = NV_DMA_V0_ACCESS_RDWR,
299 					.start = 0,
300 					.limit = device->info.ram_user - 1,
301 			       }, sizeof(struct nv_dma_v0),
302 			       &dmac->vram);
303 	if (ret)
304 		return ret;
305 
306 	return ret;
307 }
308 
309 /******************************************************************************
310  * Output path helpers
311  *****************************************************************************/
312 static void
313 nv50_outp_dump_caps(struct nouveau_drm *drm,
314 		    struct nouveau_encoder *outp)
315 {
316 	NV_DEBUG(drm, "%s caps: dp_interlace=%d\n",
317 		 outp->base.base.name, outp->caps.dp_interlace);
318 }
319 
320 static int
321 nv50_outp_atomic_check_view(struct drm_encoder *encoder,
322 			    struct drm_crtc_state *crtc_state,
323 			    struct drm_connector_state *conn_state,
324 			    struct drm_display_mode *native_mode)
325 {
326 	struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
327 	struct drm_display_mode *mode = &crtc_state->mode;
328 	struct drm_connector *connector = conn_state->connector;
329 	struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
330 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
331 
332 	NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
333 	asyc->scaler.full = false;
334 	if (!native_mode)
335 		return 0;
336 
337 	if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
338 		switch (connector->connector_type) {
339 		case DRM_MODE_CONNECTOR_LVDS:
340 		case DRM_MODE_CONNECTOR_eDP:
341 			/* Don't force scaler for EDID modes with
342 			 * same size as the native one (e.g. different
343 			 * refresh rate)
344 			 */
345 			if (mode->hdisplay == native_mode->hdisplay &&
346 			    mode->vdisplay == native_mode->vdisplay &&
347 			    mode->type & DRM_MODE_TYPE_DRIVER)
348 				break;
349 			mode = native_mode;
350 			asyc->scaler.full = true;
351 			break;
352 		default:
353 			break;
354 		}
355 	} else {
356 		mode = native_mode;
357 	}
358 
359 	if (!drm_mode_equal(adjusted_mode, mode)) {
360 		drm_mode_copy(adjusted_mode, mode);
361 		crtc_state->mode_changed = true;
362 	}
363 
364 	return 0;
365 }
366 
367 static void
368 nv50_outp_atomic_fix_depth(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state)
369 {
370 	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
371 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
372 	struct drm_display_mode *mode = &asyh->state.adjusted_mode;
373 	unsigned int max_rate, mode_rate;
374 
375 	switch (nv_encoder->dcb->type) {
376 	case DCB_OUTPUT_DP:
377 		max_rate = nv_encoder->dp.link_nr * nv_encoder->dp.link_bw;
378 
379 		/* we don't support more than 10 anyway */
380 		asyh->or.bpc = min_t(u8, asyh->or.bpc, 10);
381 
382 		/* reduce the bpc until it works out */
383 		while (asyh->or.bpc > 6) {
384 			mode_rate = DIV_ROUND_UP(mode->clock * asyh->or.bpc * 3, 8);
385 			if (mode_rate <= max_rate)
386 				break;
387 
388 			asyh->or.bpc -= 2;
389 		}
390 		break;
391 	default:
392 		break;
393 	}
394 }
395 
396 static int
397 nv50_outp_atomic_check(struct drm_encoder *encoder,
398 		       struct drm_crtc_state *crtc_state,
399 		       struct drm_connector_state *conn_state)
400 {
401 	struct drm_connector *connector = conn_state->connector;
402 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
403 	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
404 	int ret;
405 
406 	ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
407 					  nv_connector->native_mode);
408 	if (ret)
409 		return ret;
410 
411 	if (crtc_state->mode_changed || crtc_state->connectors_changed)
412 		asyh->or.bpc = connector->display_info.bpc;
413 
414 	/* We might have to reduce the bpc */
415 	nv50_outp_atomic_fix_depth(encoder, crtc_state);
416 
417 	return 0;
418 }
419 
420 struct nouveau_connector *
421 nv50_outp_get_new_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
422 {
423 	struct drm_connector *connector;
424 	struct drm_connector_state *connector_state;
425 	struct drm_encoder *encoder = to_drm_encoder(outp);
426 	int i;
427 
428 	for_each_new_connector_in_state(state, connector, connector_state, i) {
429 		if (connector_state->best_encoder == encoder)
430 			return nouveau_connector(connector);
431 	}
432 
433 	return NULL;
434 }
435 
436 struct nouveau_connector *
437 nv50_outp_get_old_connector(struct drm_atomic_state *state, struct nouveau_encoder *outp)
438 {
439 	struct drm_connector *connector;
440 	struct drm_connector_state *connector_state;
441 	struct drm_encoder *encoder = to_drm_encoder(outp);
442 	int i;
443 
444 	for_each_old_connector_in_state(state, connector, connector_state, i) {
445 		if (connector_state->best_encoder == encoder)
446 			return nouveau_connector(connector);
447 	}
448 
449 	return NULL;
450 }
451 
452 static struct nouveau_crtc *
453 nv50_outp_get_new_crtc(const struct drm_atomic_state *state, const struct nouveau_encoder *outp)
454 {
455 	struct drm_crtc *crtc;
456 	struct drm_crtc_state *crtc_state;
457 	const u32 mask = drm_encoder_mask(&outp->base.base);
458 	int i;
459 
460 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
461 		if (crtc_state->encoder_mask & mask)
462 			return nouveau_crtc(crtc);
463 	}
464 
465 	return NULL;
466 }
467 
468 /******************************************************************************
469  * DAC
470  *****************************************************************************/
471 static void
472 nv50_dac_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
473 {
474 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
475 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
476 	const u32 ctrl = NVDEF(NV507D, DAC_SET_CONTROL, OWNER, NONE);
477 
478 	core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
479 	nv_encoder->crtc = NULL;
480 }
481 
482 static void
483 nv50_dac_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
484 {
485 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
486 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
487 	struct nv50_head_atom *asyh =
488 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
489 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
490 	u32 ctrl = 0;
491 
492 	switch (nv_crtc->index) {
493 	case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
494 	case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
495 	case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
496 	case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
497 	default:
498 		WARN_ON(1);
499 		break;
500 	}
501 
502 	ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
503 
504 	if (!nvif_outp_acquired(&nv_encoder->outp))
505 		nvif_outp_acquire_dac(&nv_encoder->outp);
506 
507 	core->func->dac->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
508 	asyh->or.depth = 0;
509 
510 	nv_encoder->crtc = &nv_crtc->base;
511 }
512 
513 static enum drm_connector_status
514 nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
515 {
516 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
517 	u32 loadval;
518 	int ret;
519 
520 	loadval = nouveau_drm(encoder->dev)->vbios.dactestval;
521 	if (loadval == 0)
522 		loadval = 340;
523 
524 	ret = nvif_outp_load_detect(&nv_encoder->outp, loadval);
525 	if (ret <= 0)
526 		return connector_status_disconnected;
527 
528 	return connector_status_connected;
529 }
530 
531 static const struct drm_encoder_helper_funcs
532 nv50_dac_help = {
533 	.atomic_check = nv50_outp_atomic_check,
534 	.atomic_enable = nv50_dac_atomic_enable,
535 	.atomic_disable = nv50_dac_atomic_disable,
536 	.detect = nv50_dac_detect
537 };
538 
539 static void
540 nv50_dac_destroy(struct drm_encoder *encoder)
541 {
542 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
543 
544 	nvif_outp_dtor(&nv_encoder->outp);
545 
546 	drm_encoder_cleanup(encoder);
547 	kfree(encoder);
548 }
549 
550 static const struct drm_encoder_funcs
551 nv50_dac_func = {
552 	.destroy = nv50_dac_destroy,
553 };
554 
555 static int
556 nv50_dac_create(struct nouveau_encoder *nv_encoder)
557 {
558 	struct drm_connector *connector = &nv_encoder->conn->base;
559 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
560 	struct nvkm_i2c *i2c = nvxx_i2c(drm);
561 	struct nvkm_i2c_bus *bus;
562 	struct drm_encoder *encoder;
563 	struct dcb_output *dcbe = nv_encoder->dcb;
564 	int type = DRM_MODE_ENCODER_DAC;
565 
566 	bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
567 	if (bus)
568 		nv_encoder->i2c = &bus->i2c;
569 
570 	encoder = to_drm_encoder(nv_encoder);
571 	drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
572 			 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
573 	drm_encoder_helper_add(encoder, &nv50_dac_help);
574 
575 	drm_connector_attach_encoder(connector, encoder);
576 	return 0;
577 }
578 
579 /*
580  * audio component binding for ELD notification
581  */
582 static void
583 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
584 				int dev_id)
585 {
586 	if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
587 		acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
588 						 port, dev_id);
589 }
590 
591 static int
592 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
593 			     bool *enabled, unsigned char *buf, int max_bytes)
594 {
595 	struct nouveau_drm *drm = dev_get_drvdata(kdev);
596 	struct drm_encoder *encoder;
597 	struct nouveau_encoder *nv_encoder;
598 	struct nouveau_crtc *nv_crtc;
599 	int ret = 0;
600 
601 	*enabled = false;
602 
603 	mutex_lock(&drm->audio.lock);
604 
605 	drm_for_each_encoder(encoder, drm->dev) {
606 		struct nouveau_connector *nv_connector = NULL;
607 
608 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
609 			continue; /* TODO */
610 
611 		nv_encoder = nouveau_encoder(encoder);
612 		nv_connector = nv_encoder->conn;
613 		nv_crtc = nouveau_crtc(nv_encoder->crtc);
614 
615 		if (!nv_crtc || nv_encoder->outp.or.id != port || nv_crtc->index != dev_id)
616 			continue;
617 
618 		*enabled = nv_encoder->audio.enabled;
619 		if (*enabled) {
620 			ret = drm_eld_size(nv_connector->base.eld);
621 			memcpy(buf, nv_connector->base.eld,
622 			       min(max_bytes, ret));
623 		}
624 		break;
625 	}
626 
627 	mutex_unlock(&drm->audio.lock);
628 
629 	return ret;
630 }
631 
632 static const struct drm_audio_component_ops nv50_audio_component_ops = {
633 	.get_eld = nv50_audio_component_get_eld,
634 };
635 
636 static int
637 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
638 			  void *data)
639 {
640 	struct nouveau_drm *drm = dev_get_drvdata(kdev);
641 	struct drm_audio_component *acomp = data;
642 
643 	if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
644 		return -ENOMEM;
645 
646 	drm_modeset_lock_all(drm->dev);
647 	acomp->ops = &nv50_audio_component_ops;
648 	acomp->dev = kdev;
649 	drm->audio.component = acomp;
650 	drm_modeset_unlock_all(drm->dev);
651 	return 0;
652 }
653 
654 static void
655 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
656 			    void *data)
657 {
658 	struct nouveau_drm *drm = dev_get_drvdata(kdev);
659 	struct drm_audio_component *acomp = data;
660 
661 	drm_modeset_lock_all(drm->dev);
662 	drm->audio.component = NULL;
663 	acomp->ops = NULL;
664 	acomp->dev = NULL;
665 	drm_modeset_unlock_all(drm->dev);
666 }
667 
668 static const struct component_ops nv50_audio_component_bind_ops = {
669 	.bind   = nv50_audio_component_bind,
670 	.unbind = nv50_audio_component_unbind,
671 };
672 
673 static void
674 nv50_audio_component_init(struct nouveau_drm *drm)
675 {
676 	if (component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
677 		return;
678 
679 	drm->audio.component_registered = true;
680 	mutex_init(&drm->audio.lock);
681 }
682 
683 static void
684 nv50_audio_component_fini(struct nouveau_drm *drm)
685 {
686 	if (!drm->audio.component_registered)
687 		return;
688 
689 	component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
690 	drm->audio.component_registered = false;
691 	mutex_destroy(&drm->audio.lock);
692 }
693 
694 /******************************************************************************
695  * Audio
696  *****************************************************************************/
697 static bool
698 nv50_audio_supported(struct drm_encoder *encoder)
699 {
700 	struct nv50_disp *disp = nv50_disp(encoder->dev);
701 
702 	if (disp->disp->object.oclass <= GT200_DISP ||
703 	    disp->disp->object.oclass == GT206_DISP)
704 		return false;
705 
706 	if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
707 		struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
708 
709 		switch (nv_encoder->dcb->type) {
710 		case DCB_OUTPUT_TMDS:
711 		case DCB_OUTPUT_DP:
712 			break;
713 		default:
714 			return false;
715 		}
716 	}
717 
718 	return true;
719 }
720 
721 static void
722 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
723 {
724 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
725 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
726 	struct nvif_outp *outp = &nv_encoder->outp;
727 
728 	if (!nv50_audio_supported(encoder))
729 		return;
730 
731 	mutex_lock(&drm->audio.lock);
732 	if (nv_encoder->audio.enabled) {
733 		nv_encoder->audio.enabled = false;
734 		nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, NULL, 0);
735 	}
736 	mutex_unlock(&drm->audio.lock);
737 
738 	nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
739 }
740 
741 static void
742 nv50_audio_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
743 		  struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
744 		  struct drm_display_mode *mode)
745 {
746 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
747 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
748 	struct nvif_outp *outp = &nv_encoder->outp;
749 
750 	if (!nv50_audio_supported(encoder) || !nv_connector->base.display_info.has_audio)
751 		return;
752 
753 	mutex_lock(&drm->audio.lock);
754 
755 	nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, nv_connector->base.eld,
756 			  drm_eld_size(nv_connector->base.eld));
757 	nv_encoder->audio.enabled = true;
758 
759 	mutex_unlock(&drm->audio.lock);
760 
761 	nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
762 }
763 
764 /******************************************************************************
765  * HDMI
766  *****************************************************************************/
767 static void
768 nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
769 		 struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
770 		 struct drm_display_mode *mode, bool hda)
771 {
772 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
773 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
774 	struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
775 	union hdmi_infoframe infoframe = { 0 };
776 	const u8 rekey = 56; /* binary driver, and tegra, constant */
777 	u32 max_ac_packet;
778 	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, 17);
779 	const u8 data_len = __member_size(args->data);
780 	int ret, size;
781 
782 	max_ac_packet  = mode->htotal - mode->hdisplay;
783 	max_ac_packet -= rekey;
784 	max_ac_packet -= 18; /* constant from tegra */
785 	max_ac_packet /= 32;
786 
787 	if (nv_encoder->i2c && hdmi->scdc.scrambling.supported) {
788 		const bool high_tmds_clock_ratio = mode->clock > 340000;
789 		u8 scdc;
790 
791 		ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &scdc);
792 		if (ret < 0) {
793 			NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
794 			return;
795 		}
796 
797 		scdc &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
798 		if (high_tmds_clock_ratio || hdmi->scdc.scrambling.low_rates)
799 			scdc |= SCDC_SCRAMBLING_ENABLE;
800 		if (high_tmds_clock_ratio)
801 			scdc |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
802 
803 		ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, scdc);
804 		if (ret < 0)
805 			NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
806 				 scdc, ret);
807 	}
808 
809 	ret = nvif_outp_hdmi(&nv_encoder->outp, nv_crtc->index, true, max_ac_packet, rekey,
810 			     mode->clock, hdmi->scdc.supported, hdmi->scdc.scrambling.supported,
811 			     hdmi->scdc.scrambling.low_rates);
812 	if (ret)
813 		return;
814 
815 	/* AVI InfoFrame. */
816 	args->version = 0;
817 	args->head = nv_crtc->index;
818 
819 	if (!drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi, &nv_connector->base, mode)) {
820 		drm_hdmi_avi_infoframe_quant_range(&infoframe.avi, &nv_connector->base, mode,
821 						   HDMI_QUANTIZATION_RANGE_FULL);
822 
823 		size = hdmi_infoframe_pack(&infoframe, args->data, data_len);
824 	} else {
825 		size = 0;
826 	}
827 
828 	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, args, size);
829 
830 	/* Vendor InfoFrame. */
831 	memset(args->data, 0, data_len);
832 	if (!drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
833 							 &nv_connector->base, mode))
834 		size = hdmi_infoframe_pack(&infoframe, args->data, data_len);
835 	else
836 		size = 0;
837 
838 	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, args, size);
839 
840 	nv_encoder->hdmi.enabled = true;
841 }
842 
843 /******************************************************************************
844  * MST
845  *****************************************************************************/
846 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
847 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
848 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
849 
850 struct nv50_mstc {
851 	struct nv50_mstm *mstm;
852 	struct drm_dp_mst_port *port;
853 	struct drm_connector connector;
854 
855 	struct drm_display_mode *native;
856 	struct edid *edid;
857 };
858 
859 struct nv50_msto {
860 	struct drm_encoder encoder;
861 
862 	/* head is statically assigned on msto creation */
863 	struct nv50_head *head;
864 	struct nv50_mstc *mstc;
865 	bool disabled;
866 	bool enabled;
867 
868 	u32 display_id;
869 };
870 
871 struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
872 {
873 	struct nv50_msto *msto;
874 
875 	if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
876 		return nouveau_encoder(encoder);
877 
878 	msto = nv50_msto(encoder);
879 	if (!msto->mstc)
880 		return NULL;
881 	return msto->mstc->mstm->outp;
882 }
883 
884 static void
885 nv50_msto_cleanup(struct drm_atomic_state *state,
886 		  struct drm_dp_mst_topology_state *new_mst_state,
887 		  struct drm_dp_mst_topology_mgr *mgr,
888 		  struct nv50_msto *msto)
889 {
890 	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
891 	struct drm_dp_mst_atomic_payload *new_payload =
892 		drm_atomic_get_mst_payload_state(new_mst_state, msto->mstc->port);
893 	struct drm_dp_mst_topology_state *old_mst_state =
894 		drm_atomic_get_old_mst_topology_state(state, mgr);
895 	const struct drm_dp_mst_atomic_payload *old_payload =
896 		drm_atomic_get_mst_payload_state(old_mst_state, msto->mstc->port);
897 	struct nv50_mstc *mstc = msto->mstc;
898 	struct nv50_mstm *mstm = mstc->mstm;
899 
900 	NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
901 
902 	if (msto->disabled) {
903 		if (msto->head->func->display_id) {
904 			nvif_outp_dp_mst_id_put(&mstm->outp->outp, msto->display_id);
905 			msto->display_id = 0;
906 		}
907 
908 		msto->mstc = NULL;
909 		msto->disabled = false;
910 		drm_dp_remove_payload_part2(mgr, new_mst_state, old_payload, new_payload);
911 	} else if (msto->enabled) {
912 		drm_dp_add_payload_part2(mgr, new_payload);
913 		msto->enabled = false;
914 	}
915 }
916 
917 static void
918 nv50_msto_prepare(struct drm_atomic_state *state,
919 		  struct drm_dp_mst_topology_state *mst_state,
920 		  struct drm_dp_mst_topology_mgr *mgr,
921 		  struct nv50_msto *msto)
922 {
923 	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
924 	struct nv50_mstc *mstc = msto->mstc;
925 	struct nv50_mstm *mstm = mstc->mstm;
926 	struct drm_dp_mst_atomic_payload *payload;
927 	int ret = 0;
928 
929 	NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
930 
931 	payload = drm_atomic_get_mst_payload_state(mst_state, mstc->port);
932 
933 	if (msto->disabled) {
934 		drm_dp_remove_payload_part1(mgr, mst_state, payload);
935 		nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
936 		ret = 1;
937 	} else {
938 		if (msto->enabled)
939 			ret = drm_dp_add_payload_part1(mgr, mst_state, payload);
940 	}
941 
942 	if (ret == 0) {
943 		nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index,
944 				      payload->vc_start_slot, payload->time_slots,
945 				      payload->pbn,
946 				      payload->time_slots * dfixed_trunc(mst_state->pbn_div));
947 	} else {
948 		nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
949 	}
950 }
951 
952 static int
953 nv50_msto_atomic_check(struct drm_encoder *encoder,
954 		       struct drm_crtc_state *crtc_state,
955 		       struct drm_connector_state *conn_state)
956 {
957 	struct drm_atomic_state *state = crtc_state->state;
958 	struct drm_connector *connector = conn_state->connector;
959 	struct drm_dp_mst_topology_state *mst_state;
960 	struct nv50_mstc *mstc = nv50_mstc(connector);
961 	struct nv50_mstm *mstm = mstc->mstm;
962 	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
963 	int slots;
964 	int ret;
965 
966 	ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
967 					  mstc->native);
968 	if (ret)
969 		return ret;
970 
971 	if (!drm_atomic_crtc_needs_modeset(crtc_state))
972 		return 0;
973 
974 	/*
975 	 * When restoring duplicated states, we need to make sure that the bw
976 	 * remains the same and avoid recalculating it, as the connector's bpc
977 	 * may have changed after the state was duplicated
978 	 */
979 	if (!state->duplicated) {
980 		const int clock = crtc_state->adjusted_mode.clock;
981 
982 		asyh->or.bpc = connector->display_info.bpc;
983 		asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3 << 4);
984 	}
985 
986 	mst_state = drm_atomic_get_mst_topology_state(state, &mstm->mgr);
987 	if (IS_ERR(mst_state))
988 		return PTR_ERR(mst_state);
989 
990 	if (!mst_state->pbn_div.full) {
991 		struct nouveau_encoder *outp = mstc->mstm->outp;
992 
993 		mst_state->pbn_div = drm_dp_get_vc_payload_bw(outp->dp.link_bw, outp->dp.link_nr);
994 	}
995 
996 	slots = drm_dp_atomic_find_time_slots(state, &mstm->mgr, mstc->port, asyh->dp.pbn);
997 	if (slots < 0)
998 		return slots;
999 
1000 	asyh->dp.tu = slots;
1001 
1002 	return 0;
1003 }
1004 
1005 static u8
1006 nv50_dp_bpc_to_depth(unsigned int bpc)
1007 {
1008 	switch (bpc) {
1009 	case  6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
1010 	case  8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
1011 	case 10:
1012 	default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
1013 	}
1014 }
1015 
1016 static void
1017 nv50_msto_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1018 {
1019 	struct nv50_msto *msto = nv50_msto(encoder);
1020 	struct nv50_head *head = msto->head;
1021 	struct nv50_head_atom *asyh =
1022 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &head->base.base));
1023 	struct nv50_mstc *mstc = NULL;
1024 	struct nv50_mstm *mstm = NULL;
1025 	struct drm_connector *connector;
1026 	struct drm_connector_list_iter conn_iter;
1027 	u8 proto;
1028 
1029 	drm_connector_list_iter_begin(encoder->dev, &conn_iter);
1030 	drm_for_each_connector_iter(connector, &conn_iter) {
1031 		if (connector->state->best_encoder == &msto->encoder) {
1032 			mstc = nv50_mstc(connector);
1033 			mstm = mstc->mstm;
1034 			break;
1035 		}
1036 	}
1037 	drm_connector_list_iter_end(&conn_iter);
1038 
1039 	if (WARN_ON(!mstc))
1040 		return;
1041 
1042 	if (!mstm->links++) {
1043 		nvif_outp_acquire_sor(&mstm->outp->outp, false /*TODO: MST audio... */);
1044 		nouveau_dp_train(mstm->outp, true, 0, 0);
1045 	}
1046 
1047 	if (head->func->display_id) {
1048 		if (!WARN_ON(nvif_outp_dp_mst_id_get(&mstm->outp->outp, &msto->display_id)))
1049 			head->func->display_id(head, msto->display_id);
1050 	}
1051 
1052 	if (mstm->outp->outp.or.link & 1)
1053 		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1054 	else
1055 		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1056 
1057 	mstm->outp->update(mstm->outp, head->base.index, asyh, proto,
1058 			   nv50_dp_bpc_to_depth(asyh->or.bpc));
1059 
1060 	msto->mstc = mstc;
1061 	msto->enabled = true;
1062 	mstm->modified = true;
1063 }
1064 
1065 static void
1066 nv50_msto_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1067 {
1068 	struct nv50_msto *msto = nv50_msto(encoder);
1069 	struct nv50_mstc *mstc = msto->mstc;
1070 	struct nv50_mstm *mstm = mstc->mstm;
1071 
1072 	if (msto->head->func->display_id)
1073 		msto->head->func->display_id(msto->head, 0);
1074 
1075 	mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
1076 	mstm->modified = true;
1077 	if (!--mstm->links)
1078 		mstm->disabled = true;
1079 	msto->disabled = true;
1080 }
1081 
1082 static const struct drm_encoder_helper_funcs
1083 nv50_msto_help = {
1084 	.atomic_disable = nv50_msto_atomic_disable,
1085 	.atomic_enable = nv50_msto_atomic_enable,
1086 	.atomic_check = nv50_msto_atomic_check,
1087 };
1088 
1089 static void
1090 nv50_msto_destroy(struct drm_encoder *encoder)
1091 {
1092 	struct nv50_msto *msto = nv50_msto(encoder);
1093 	drm_encoder_cleanup(&msto->encoder);
1094 	kfree(msto);
1095 }
1096 
1097 static const struct drm_encoder_funcs
1098 nv50_msto = {
1099 	.destroy = nv50_msto_destroy,
1100 };
1101 
1102 static struct nv50_msto *
1103 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1104 {
1105 	struct nv50_msto *msto;
1106 	int ret;
1107 
1108 	msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1109 	if (!msto)
1110 		return ERR_PTR(-ENOMEM);
1111 
1112 	ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1113 			       DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1114 	if (ret) {
1115 		kfree(msto);
1116 		return ERR_PTR(ret);
1117 	}
1118 
1119 	drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1120 	msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1121 	msto->head = head;
1122 	return msto;
1123 }
1124 
1125 static struct drm_encoder *
1126 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1127 			      struct drm_atomic_state *state)
1128 {
1129 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1130 											 connector);
1131 	struct nv50_mstc *mstc = nv50_mstc(connector);
1132 	struct drm_crtc *crtc = connector_state->crtc;
1133 
1134 	if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1135 		return NULL;
1136 
1137 	return &nv50_head(crtc)->msto->encoder;
1138 }
1139 
1140 static enum drm_mode_status
1141 nv50_mstc_mode_valid(struct drm_connector *connector,
1142 		     const struct drm_display_mode *mode)
1143 {
1144 	struct nv50_mstc *mstc = nv50_mstc(connector);
1145 	struct nouveau_encoder *outp = mstc->mstm->outp;
1146 
1147 	/* TODO: calculate the PBN from the dotclock and validate against the
1148 	 * MSTB's max possible PBN
1149 	 */
1150 
1151 	return nv50_dp_mode_valid(outp, mode, NULL);
1152 }
1153 
1154 static int
1155 nv50_mstc_get_modes(struct drm_connector *connector)
1156 {
1157 	struct nv50_mstc *mstc = nv50_mstc(connector);
1158 	int ret = 0;
1159 
1160 	mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1161 	drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1162 	if (mstc->edid)
1163 		ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1164 
1165 	/*
1166 	 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1167 	 * to 8 to save bandwidth on the topology. In the future, we'll want
1168 	 * to properly fix this by dynamically selecting the highest possible
1169 	 * bpc that would fit in the topology
1170 	 */
1171 	if (connector->display_info.bpc)
1172 		connector->display_info.bpc =
1173 			clamp(connector->display_info.bpc, 6U, 8U);
1174 	else
1175 		connector->display_info.bpc = 8;
1176 
1177 	if (mstc->native)
1178 		drm_mode_destroy(mstc->connector.dev, mstc->native);
1179 	mstc->native = nouveau_conn_native_mode(&mstc->connector);
1180 	return ret;
1181 }
1182 
1183 static int
1184 nv50_mstc_atomic_check(struct drm_connector *connector,
1185 		       struct drm_atomic_state *state)
1186 {
1187 	struct nv50_mstc *mstc = nv50_mstc(connector);
1188 	struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1189 
1190 	return drm_dp_atomic_release_time_slots(state, mgr, mstc->port);
1191 }
1192 
1193 static int
1194 nv50_mstc_detect(struct drm_connector *connector,
1195 		 struct drm_modeset_acquire_ctx *ctx, bool force)
1196 {
1197 	struct nv50_mstc *mstc = nv50_mstc(connector);
1198 	int ret;
1199 
1200 	if (drm_connector_is_unregistered(connector))
1201 		return connector_status_disconnected;
1202 
1203 	ret = pm_runtime_get_sync(connector->dev->dev);
1204 	if (ret < 0 && ret != -EACCES) {
1205 		pm_runtime_put_autosuspend(connector->dev->dev);
1206 		return connector_status_disconnected;
1207 	}
1208 
1209 	ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1210 				     mstc->port);
1211 	if (ret != connector_status_connected)
1212 		goto out;
1213 
1214 out:
1215 	pm_runtime_mark_last_busy(connector->dev->dev);
1216 	pm_runtime_put_autosuspend(connector->dev->dev);
1217 	return ret;
1218 }
1219 
1220 static const struct drm_connector_helper_funcs
1221 nv50_mstc_help = {
1222 	.get_modes = nv50_mstc_get_modes,
1223 	.mode_valid = nv50_mstc_mode_valid,
1224 	.atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1225 	.atomic_check = nv50_mstc_atomic_check,
1226 	.detect_ctx = nv50_mstc_detect,
1227 };
1228 
1229 static void
1230 nv50_mstc_destroy(struct drm_connector *connector)
1231 {
1232 	struct nv50_mstc *mstc = nv50_mstc(connector);
1233 
1234 	drm_connector_cleanup(&mstc->connector);
1235 	drm_dp_mst_put_port_malloc(mstc->port);
1236 
1237 	kfree(mstc);
1238 }
1239 
1240 static const struct drm_connector_funcs
1241 nv50_mstc = {
1242 	.reset = nouveau_conn_reset,
1243 	.fill_modes = drm_helper_probe_single_connector_modes,
1244 	.destroy = nv50_mstc_destroy,
1245 	.atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1246 	.atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1247 	.atomic_set_property = nouveau_conn_atomic_set_property,
1248 	.atomic_get_property = nouveau_conn_atomic_get_property,
1249 };
1250 
1251 static int
1252 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1253 	      const char *path, struct nv50_mstc **pmstc)
1254 {
1255 	struct drm_device *dev = mstm->outp->base.base.dev;
1256 	struct drm_crtc *crtc;
1257 	struct nv50_mstc *mstc;
1258 	int ret;
1259 
1260 	if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1261 		return -ENOMEM;
1262 	mstc->mstm = mstm;
1263 	mstc->port = port;
1264 
1265 	ret = drm_connector_dynamic_init(dev, &mstc->connector, &nv50_mstc,
1266 					 DRM_MODE_CONNECTOR_DisplayPort, NULL);
1267 	if (ret) {
1268 		kfree(*pmstc);
1269 		*pmstc = NULL;
1270 		return ret;
1271 	}
1272 
1273 	drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1274 
1275 	mstc->connector.funcs->reset(&mstc->connector);
1276 	nouveau_conn_attach_properties(&mstc->connector);
1277 
1278 	drm_for_each_crtc(crtc, dev) {
1279 		if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1280 			continue;
1281 
1282 		drm_connector_attach_encoder(&mstc->connector,
1283 					     &nv50_head(crtc)->msto->encoder);
1284 	}
1285 
1286 	drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1287 	drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1288 	drm_connector_set_path_property(&mstc->connector, path);
1289 	drm_dp_mst_get_port_malloc(port);
1290 	return 0;
1291 }
1292 
1293 static void
1294 nv50_mstm_cleanup(struct drm_atomic_state *state,
1295 		  struct drm_dp_mst_topology_state *mst_state,
1296 		  struct nv50_mstm *mstm)
1297 {
1298 	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1299 	struct drm_encoder *encoder;
1300 
1301 	NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1302 	drm_dp_check_act_status(&mstm->mgr);
1303 
1304 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1305 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1306 			struct nv50_msto *msto = nv50_msto(encoder);
1307 			struct nv50_mstc *mstc = msto->mstc;
1308 			if (mstc && mstc->mstm == mstm)
1309 				nv50_msto_cleanup(state, mst_state, &mstm->mgr, msto);
1310 		}
1311 	}
1312 
1313 	if (mstm->disabled) {
1314 		nouveau_dp_power_down(mstm->outp);
1315 		nvif_outp_release(&mstm->outp->outp);
1316 		mstm->disabled = false;
1317 	}
1318 
1319 	mstm->modified = false;
1320 }
1321 
1322 static void
1323 nv50_mstm_prepare(struct drm_atomic_state *state,
1324 		  struct drm_dp_mst_topology_state *mst_state,
1325 		  struct nv50_mstm *mstm)
1326 {
1327 	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1328 	struct drm_encoder *encoder;
1329 
1330 	NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1331 
1332 	/* Disable payloads first */
1333 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1334 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1335 			struct nv50_msto *msto = nv50_msto(encoder);
1336 			struct nv50_mstc *mstc = msto->mstc;
1337 			if (mstc && mstc->mstm == mstm && msto->disabled)
1338 				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1339 		}
1340 	}
1341 
1342 	/* Add payloads for new heads, while also updating the start slots of any unmodified (but
1343 	 * active) heads that may have had their VC slots shifted left after the previous step
1344 	 */
1345 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1346 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1347 			struct nv50_msto *msto = nv50_msto(encoder);
1348 			struct nv50_mstc *mstc = msto->mstc;
1349 			if (mstc && mstc->mstm == mstm && !msto->disabled)
1350 				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1351 		}
1352 	}
1353 }
1354 
1355 static struct drm_connector *
1356 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1357 			struct drm_dp_mst_port *port, const char *path)
1358 {
1359 	struct nv50_mstm *mstm = nv50_mstm(mgr);
1360 	struct nv50_mstc *mstc;
1361 	int ret;
1362 
1363 	ret = nv50_mstc_new(mstm, port, path, &mstc);
1364 	if (ret)
1365 		return NULL;
1366 
1367 	return &mstc->connector;
1368 }
1369 
1370 static const struct drm_dp_mst_topology_cbs
1371 nv50_mstm = {
1372 	.add_connector = nv50_mstm_add_connector,
1373 };
1374 
1375 bool
1376 nv50_mstm_service(struct nouveau_drm *drm,
1377 		  struct nouveau_connector *nv_connector,
1378 		  struct nv50_mstm *mstm)
1379 {
1380 	struct drm_dp_aux *aux = &nv_connector->aux;
1381 	bool handled = true, ret = true;
1382 	int rc;
1383 	u8 esi[8] = {};
1384 
1385 	while (handled) {
1386 		u8 ack[8] = {};
1387 
1388 		rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1389 		if (rc != 8) {
1390 			ret = false;
1391 			break;
1392 		}
1393 
1394 		drm_dp_mst_hpd_irq_handle_event(&mstm->mgr, esi, ack, &handled);
1395 		if (!handled)
1396 			break;
1397 
1398 		rc = drm_dp_dpcd_writeb(aux, DP_SINK_COUNT_ESI + 1, ack[1]);
1399 
1400 		if (rc != 1) {
1401 			ret = false;
1402 			break;
1403 		}
1404 
1405 		drm_dp_mst_hpd_irq_send_new_request(&mstm->mgr);
1406 	}
1407 
1408 	if (!ret)
1409 		NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
1410 			 nv_connector->base.name, rc);
1411 
1412 	return ret;
1413 }
1414 
1415 void
1416 nv50_mstm_remove(struct nv50_mstm *mstm)
1417 {
1418 	mstm->is_mst = false;
1419 	drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1420 }
1421 
1422 int
1423 nv50_mstm_detect(struct nouveau_encoder *outp)
1424 {
1425 	struct nv50_mstm *mstm = outp->dp.mstm;
1426 	struct drm_dp_aux *aux;
1427 	int ret;
1428 
1429 	if (!mstm || !mstm->can_mst)
1430 		return 0;
1431 
1432 	aux = mstm->mgr.aux;
1433 
1434 	/* Clear any leftover MST state we didn't set ourselves by first
1435 	 * disabling MST if it was already enabled
1436 	 */
1437 	ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1438 	if (ret < 0)
1439 		return ret;
1440 
1441 	/* And start enabling */
1442 	ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
1443 	if (ret)
1444 		return ret;
1445 
1446 	mstm->is_mst = true;
1447 	return 1;
1448 }
1449 
1450 static void
1451 nv50_mstm_fini(struct nouveau_encoder *outp)
1452 {
1453 	struct nv50_mstm *mstm = outp->dp.mstm;
1454 
1455 	if (!mstm)
1456 		return;
1457 
1458 	/* Don't change the MST state of this connector until we've finished
1459 	 * resuming, since we can't safely grab hpd_irq_lock in our resume
1460 	 * path to protect mstm->is_mst without potentially deadlocking
1461 	 */
1462 	mutex_lock(&outp->dp.hpd_irq_lock);
1463 	mstm->suspended = true;
1464 	mutex_unlock(&outp->dp.hpd_irq_lock);
1465 
1466 	if (mstm->is_mst)
1467 		drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1468 }
1469 
1470 static void
1471 nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1472 {
1473 	struct nv50_mstm *mstm = outp->dp.mstm;
1474 	int ret = 0;
1475 
1476 	if (!mstm)
1477 		return;
1478 
1479 	if (mstm->is_mst) {
1480 		ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1481 		if (ret == -1)
1482 			nv50_mstm_remove(mstm);
1483 	}
1484 
1485 	mutex_lock(&outp->dp.hpd_irq_lock);
1486 	mstm->suspended = false;
1487 	mutex_unlock(&outp->dp.hpd_irq_lock);
1488 
1489 	if (ret == -1)
1490 		drm_kms_helper_hotplug_event(mstm->mgr.dev);
1491 }
1492 
1493 static void
1494 nv50_mstm_del(struct nv50_mstm **pmstm)
1495 {
1496 	struct nv50_mstm *mstm = *pmstm;
1497 	if (mstm) {
1498 		drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1499 		kfree(*pmstm);
1500 		*pmstm = NULL;
1501 	}
1502 }
1503 
1504 static int
1505 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1506 	      int conn_base_id, struct nv50_mstm **pmstm)
1507 {
1508 	const int max_payloads = hweight8(outp->dcb->heads);
1509 	struct drm_device *dev = outp->base.base.dev;
1510 	struct nv50_mstm *mstm;
1511 	int ret;
1512 
1513 	if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1514 		return -ENOMEM;
1515 	mstm->outp = outp;
1516 	mstm->mgr.cbs = &nv50_mstm;
1517 
1518 	ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1519 					   max_payloads, conn_base_id);
1520 	if (ret)
1521 		return ret;
1522 
1523 	return 0;
1524 }
1525 
1526 /******************************************************************************
1527  * SOR
1528  *****************************************************************************/
1529 static void
1530 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1531 		struct nv50_head_atom *asyh, u8 proto, u8 depth)
1532 {
1533 	struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1534 	struct nv50_core *core = disp->core;
1535 
1536 	if (!asyh) {
1537 		nv_encoder->ctrl &= ~BIT(head);
1538 		if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1539 			nv_encoder->ctrl = 0;
1540 	} else {
1541 		nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1542 		nv_encoder->ctrl |= BIT(head);
1543 		asyh->or.depth = depth;
1544 	}
1545 
1546 	core->func->sor->ctrl(core, nv_encoder->outp.or.id, nv_encoder->ctrl, asyh);
1547 }
1548 
1549 /* TODO: Should we extend this to PWM-only backlights?
1550  * As well, should we add a DRM helper for waiting for the backlight to acknowledge
1551  * the panel backlight has been shut off? Intel doesn't seem to do this, and uses a
1552  * fixed time delay from the vbios…
1553  */
1554 static void
1555 nv50_sor_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1556 {
1557 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1558 	struct nv50_head *head = nv50_head(nv_encoder->crtc);
1559 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1560 	struct nouveau_connector *nv_connector = nv50_outp_get_old_connector(state, nv_encoder);
1561 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
1562 	struct nouveau_backlight *backlight = nv_connector->backlight;
1563 	struct drm_dp_aux *aux = &nv_connector->aux;
1564 	int ret;
1565 
1566 	if (backlight && backlight->uses_dpcd) {
1567 		ret = drm_edp_backlight_disable(aux, &backlight->edp_info);
1568 		if (ret < 0)
1569 			NV_ERROR(drm, "Failed to disable backlight on [CONNECTOR:%d:%s]: %d\n",
1570 				 nv_connector->base.base.id, nv_connector->base.name, ret);
1571 	}
1572 #endif
1573 
1574 	if (nv_encoder->dcb->type == DCB_OUTPUT_TMDS && nv_encoder->hdmi.enabled) {
1575 		nvif_outp_hdmi(&nv_encoder->outp, head->base.index,
1576 			       false, 0, 0, 0, false, false, false);
1577 		nv_encoder->hdmi.enabled = false;
1578 	}
1579 
1580 	if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1581 		nouveau_dp_power_down(nv_encoder);
1582 
1583 	if (head->func->display_id)
1584 		head->func->display_id(head, 0);
1585 
1586 	nv_encoder->update(nv_encoder, head->base.index, NULL, 0, 0);
1587 	nv50_audio_disable(encoder, &head->base);
1588 	nv_encoder->crtc = NULL;
1589 }
1590 
1591 // common/inc/displayport/displayport.h
1592 #define DP_CONFIG_WATERMARK_ADJUST                   2
1593 #define DP_CONFIG_WATERMARK_LIMIT                   20
1594 #define DP_CONFIG_INCREASED_WATERMARK_ADJUST         8
1595 #define DP_CONFIG_INCREASED_WATERMARK_LIMIT         22
1596 
1597 static bool
1598 nv50_sor_dp_watermark_sst(struct nouveau_encoder *outp,
1599 			  struct nv50_head *head, struct nv50_head_atom *asyh)
1600 {
1601 	bool enhancedFraming = outp->dp.dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP;
1602 	u64 minRate = outp->dp.link_bw * 1000;
1603 	unsigned tuSize = 64;
1604 	unsigned waterMark;
1605 	unsigned hBlankSym;
1606 	unsigned vBlankSym;
1607 	unsigned watermarkAdjust = DP_CONFIG_WATERMARK_ADJUST;
1608 	unsigned watermarkMinimum = DP_CONFIG_WATERMARK_LIMIT;
1609 	// depth is multiplied by 16 in case of DSC enable
1610 	s32 hblank_symbols;
1611 	// number of link clocks per line.
1612 	int vblank_symbols	  = 0;
1613 	bool bEnableDsc = false;
1614 	unsigned surfaceWidth = asyh->mode.h.blanks - asyh->mode.h.blanke;
1615 	unsigned rasterWidth = asyh->mode.h.active;
1616 	unsigned depth = asyh->or.bpc * 3;
1617 	unsigned DSC_FACTOR = bEnableDsc ? 16 : 1;
1618 	u64 pixelClockHz = asyh->mode.clock * 1000;
1619 	u64 PrecisionFactor = 100000, ratioF, watermarkF;
1620 	u32 numLanesPerLink = outp->dp.link_nr;
1621 	u32 numSymbolsPerLine;
1622 	u32 BlankingBits;
1623 	u32 surfaceWidthPerLink;
1624 	u32 PixelSteeringBits;
1625 	u64 NumBlankingLinkClocks;
1626 	u32 MinHBlank;
1627 
1628 	if (outp->outp.info.dp.increased_wm) {
1629 		watermarkAdjust = DP_CONFIG_INCREASED_WATERMARK_ADJUST;
1630 		watermarkMinimum = DP_CONFIG_INCREASED_WATERMARK_LIMIT;
1631 	}
1632 
1633 	if ((pixelClockHz * depth) >= (8 * minRate * outp->dp.link_nr * DSC_FACTOR))
1634 	{
1635 		return false;
1636 	}
1637 
1638 	//
1639 	// For DSC, if (pclk * bpp) < (1/64 * orclk * 8 * lanes) then some TU may end up with
1640 	// 0 active symbols. This may cause HW hang. Bug 200379426
1641 	//
1642 	if ((bEnableDsc) &&
1643 	    ((pixelClockHz * depth) < div_u64(8 * minRate * outp->dp.link_nr * DSC_FACTOR, 64)))
1644 	{
1645 		return false;
1646 	}
1647 
1648 	//
1649 	//  Perform the SST calculation.
1650 	//	For auto mode the watermark calculation does not need to track accumulated error the
1651 	//	formulas for manual mode will not work.  So below calculation was extracted from the DTB.
1652 	//
1653 	ratioF = div_u64((u64)pixelClockHz * depth * PrecisionFactor, DSC_FACTOR);
1654 
1655 	ratioF = div_u64(ratioF, 8 * (u64) minRate * outp->dp.link_nr);
1656 
1657 	if (PrecisionFactor < ratioF) // Assert if we will end up with a negative number in below
1658 		return false;
1659 
1660 	watermarkF = div_u64(ratioF * tuSize * (PrecisionFactor - ratioF), PrecisionFactor);
1661 	waterMark = (unsigned)(watermarkAdjust + (div_u64(2 * div_u64(depth * PrecisionFactor, 8 * numLanesPerLink * DSC_FACTOR) + watermarkF, PrecisionFactor)));
1662 
1663 	//
1664 	//  Bounds check the watermark
1665 	//
1666 	numSymbolsPerLine = div_u64(surfaceWidth * depth, 8 * outp->dp.link_nr * DSC_FACTOR);
1667 
1668 	if (WARN_ON(waterMark > 39 || waterMark > numSymbolsPerLine))
1669 		return false;
1670 
1671 	//
1672 	//  Clamp the low side
1673 	//
1674 	if (waterMark < watermarkMinimum)
1675 		waterMark = watermarkMinimum;
1676 
1677 	//Bits to send BS/BE/Extra symbols due to pixel padding
1678 	//Also accounts for enhanced framing.
1679 	BlankingBits = 3*8*numLanesPerLink + (enhancedFraming ? 3*8*numLanesPerLink : 0);
1680 
1681 	//VBID/MVID/MAUD sent 4 times all the time
1682 	BlankingBits += 3*8*4;
1683 
1684 	surfaceWidthPerLink = surfaceWidth;
1685 
1686 	//Extra bits sent due to pixel steering
1687 	u32 remain;
1688 	div_u64_rem(surfaceWidthPerLink, numLanesPerLink, &remain);
1689 	PixelSteeringBits = remain ? div_u64((numLanesPerLink - remain) * depth, DSC_FACTOR) : 0;
1690 
1691 	BlankingBits += PixelSteeringBits;
1692 	NumBlankingLinkClocks = div_u64((u64)BlankingBits * PrecisionFactor, (8 * numLanesPerLink));
1693 	MinHBlank = (u32)(div_u64(div_u64(NumBlankingLinkClocks * pixelClockHz, minRate), PrecisionFactor));
1694 	MinHBlank += 12;
1695 
1696 	if (WARN_ON(MinHBlank > rasterWidth - surfaceWidth))
1697 		return false;
1698 
1699 	// Bug 702290 - Active Width should be greater than 60
1700 	if (WARN_ON(surfaceWidth <= 60))
1701 		return false;
1702 
1703 
1704 	hblank_symbols = (s32)(div_u64((u64)(rasterWidth - surfaceWidth - MinHBlank) * minRate, pixelClockHz));
1705 
1706 	//reduce HBlank Symbols to account for secondary data packet
1707 	hblank_symbols -= 1; //Stuffer latency to send BS
1708 	hblank_symbols -= 3; //SPKT latency to send data to stuffer
1709 
1710 	hblank_symbols -= numLanesPerLink == 1 ? 9  : numLanesPerLink == 2 ? 6 : 3;
1711 
1712 	hBlankSym = (hblank_symbols < 0) ? 0 : hblank_symbols;
1713 
1714 	// Refer to dev_disp.ref for more information.
1715 	// # symbols/vblank = ((SetRasterBlankEnd.X + SetRasterSize.Width - SetRasterBlankStart.X - 40) * link_clk / pclk) - Y - 1;
1716 	// where Y = (# lanes == 4) 12 : (# lanes == 2) ? 21 : 39
1717 	if (surfaceWidth < 40)
1718 	{
1719 		vblank_symbols = 0;
1720 	}
1721 	else
1722 	{
1723 		vblank_symbols = (s32)((div_u64((u64)(surfaceWidth - 40) * minRate, pixelClockHz))) - 1;
1724 
1725 		vblank_symbols -= numLanesPerLink == 1 ? 39  : numLanesPerLink == 2 ? 21 : 12;
1726 	}
1727 
1728 	vBlankSym = (vblank_symbols < 0) ? 0 : vblank_symbols;
1729 
1730 	return nvif_outp_dp_sst(&outp->outp, head->base.index, waterMark, hBlankSym, vBlankSym);
1731 }
1732 
1733 static void
1734 nv50_sor_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1735 {
1736 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1737 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1738 	struct nv50_head_atom *asyh =
1739 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1740 	struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1741 	struct nv50_disp *disp = nv50_disp(encoder->dev);
1742 	struct nv50_head *head = nv50_head(&nv_crtc->base);
1743 	struct nvif_outp *outp = &nv_encoder->outp;
1744 	struct drm_device *dev = encoder->dev;
1745 	struct nouveau_drm *drm = nouveau_drm(dev);
1746 	struct nouveau_connector *nv_connector;
1747 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1748 	struct nouveau_backlight *backlight;
1749 #endif
1750 	struct nvbios *bios = &drm->vbios;
1751 	bool lvds_dual = false, lvds_8bpc = false, hda = false;
1752 	u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
1753 	u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1754 
1755 	nv_connector = nv50_outp_get_new_connector(state, nv_encoder);
1756 	nv_encoder->crtc = &nv_crtc->base;
1757 
1758 	if ((disp->disp->object.oclass == GT214_DISP ||
1759 	     disp->disp->object.oclass >= GF110_DISP) &&
1760 	    nv_encoder->dcb->type != DCB_OUTPUT_LVDS &&
1761 	    nv_connector->base.display_info.has_audio)
1762 		hda = true;
1763 
1764 	if (!nvif_outp_acquired(outp))
1765 		nvif_outp_acquire_sor(outp, hda);
1766 
1767 	switch (nv_encoder->dcb->type) {
1768 	case DCB_OUTPUT_TMDS:
1769 		if (disp->disp->object.oclass != NV50_DISP &&
1770 		    nv_connector->base.display_info.is_hdmi)
1771 			nv50_hdmi_enable(encoder, nv_crtc, nv_connector, state, mode, hda);
1772 
1773 		if (nv_encoder->outp.or.link & 1) {
1774 			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1775 			/* Only enable dual-link if:
1776 			 *  - Need to (i.e. rate > 165MHz)
1777 			 *  - DCB says we can
1778 			 *  - Not an HDMI monitor, since there's no dual-link
1779 			 *    on HDMI.
1780 			 */
1781 			if (mode->clock >= 165000 &&
1782 			    nv_encoder->dcb->duallink_possible &&
1783 			    !nv_connector->base.display_info.is_hdmi)
1784 				proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1785 		} else {
1786 			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1787 		}
1788 		break;
1789 	case DCB_OUTPUT_LVDS:
1790 		proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1791 
1792 		if (bios->fp_no_ddc) {
1793 			lvds_dual = bios->fp.dual_link;
1794 			lvds_8bpc = bios->fp.if_is_24bit;
1795 		} else {
1796 			if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1797 				if (((u8 *)nv_connector->edid)[121] == 2)
1798 					lvds_dual = true;
1799 			} else
1800 			if (mode->clock >= bios->fp.duallink_transition_clk) {
1801 				lvds_dual = true;
1802 			}
1803 
1804 			if (lvds_dual) {
1805 				if (bios->fp.strapless_is_24bit & 2)
1806 					lvds_8bpc = true;
1807 			} else {
1808 				if (bios->fp.strapless_is_24bit & 1)
1809 					lvds_8bpc = true;
1810 			}
1811 
1812 			if (asyh->or.bpc == 8)
1813 				lvds_8bpc = true;
1814 		}
1815 
1816 		nvif_outp_lvds(&nv_encoder->outp, lvds_dual, lvds_8bpc);
1817 		break;
1818 	case DCB_OUTPUT_DP:
1819 		nouveau_dp_train(nv_encoder, false, mode->clock, asyh->or.bpc);
1820 		nv50_sor_dp_watermark_sst(nv_encoder, head, asyh);
1821 		depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1822 
1823 		if (nv_encoder->outp.or.link & 1)
1824 			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1825 		else
1826 			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1827 
1828 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1829 		backlight = nv_connector->backlight;
1830 		if (backlight && backlight->uses_dpcd)
1831 			drm_edp_backlight_enable(&nv_connector->aux, &backlight->edp_info,
1832 						 (u16)backlight->dev->props.brightness);
1833 #endif
1834 
1835 		break;
1836 	default:
1837 		BUG();
1838 		break;
1839 	}
1840 
1841 	if (head->func->display_id)
1842 		head->func->display_id(head, BIT(nv_encoder->outp.id));
1843 
1844 	nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1845 }
1846 
1847 static const struct drm_encoder_helper_funcs
1848 nv50_sor_help = {
1849 	.atomic_check = nv50_outp_atomic_check,
1850 	.atomic_enable = nv50_sor_atomic_enable,
1851 	.atomic_disable = nv50_sor_atomic_disable,
1852 };
1853 
1854 static void
1855 nv50_sor_destroy(struct drm_encoder *encoder)
1856 {
1857 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1858 
1859 	nv50_mstm_del(&nv_encoder->dp.mstm);
1860 	drm_encoder_cleanup(encoder);
1861 
1862 	if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1863 		mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1864 
1865 	nvif_outp_dtor(&nv_encoder->outp);
1866 	kfree(encoder);
1867 }
1868 
1869 static const struct drm_encoder_funcs
1870 nv50_sor_func = {
1871 	.destroy = nv50_sor_destroy,
1872 };
1873 
1874 static int
1875 nv50_sor_create(struct nouveau_encoder *nv_encoder)
1876 {
1877 	struct drm_connector *connector = &nv_encoder->conn->base;
1878 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1879 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
1880 	struct nvkm_i2c *i2c = nvxx_i2c(drm);
1881 	struct drm_encoder *encoder;
1882 	struct dcb_output *dcbe = nv_encoder->dcb;
1883 	struct nv50_disp *disp = nv50_disp(connector->dev);
1884 	int type, ret;
1885 
1886 	switch (dcbe->type) {
1887 	case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1888 	case DCB_OUTPUT_TMDS:
1889 	case DCB_OUTPUT_DP:
1890 	default:
1891 		type = DRM_MODE_ENCODER_TMDS;
1892 		break;
1893 	}
1894 
1895 	nv_encoder->update = nv50_sor_update;
1896 
1897 	encoder = to_drm_encoder(nv_encoder);
1898 	drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1899 			 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1900 	drm_encoder_helper_add(encoder, &nv50_sor_help);
1901 
1902 	drm_connector_attach_encoder(connector, encoder);
1903 
1904 	disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1905 	nv50_outp_dump_caps(drm, nv_encoder);
1906 
1907 	if (dcbe->type == DCB_OUTPUT_DP) {
1908 		mutex_init(&nv_encoder->dp.hpd_irq_lock);
1909 
1910 		if (disp->disp->object.oclass < GF110_DISP) {
1911 			/* HW has no support for address-only
1912 			 * transactions, so we're required to
1913 			 * use custom I2C-over-AUX code.
1914 			 */
1915 			struct nvkm_i2c_aux *aux;
1916 
1917 			aux = nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1918 			if (!aux)
1919 				return -EINVAL;
1920 
1921 			nv_encoder->i2c = &aux->i2c;
1922 		} else {
1923 			nv_encoder->i2c = &nv_connector->aux.ddc;
1924 		}
1925 
1926 		if (nv_connector->type != DCB_CONNECTOR_eDP && nv_encoder->outp.info.dp.mst) {
1927 			ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1928 					    16, nv_connector->base.base.id,
1929 					    &nv_encoder->dp.mstm);
1930 			if (ret)
1931 				return ret;
1932 		}
1933 	} else
1934 	if (nv_encoder->outp.info.ddc != NVIF_OUTP_DDC_INVALID) {
1935 		struct nvkm_i2c_bus *bus =
1936 			nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1937 		if (bus)
1938 			nv_encoder->i2c = &bus->i2c;
1939 	}
1940 
1941 	return 0;
1942 }
1943 
1944 /******************************************************************************
1945  * PIOR
1946  *****************************************************************************/
1947 static int
1948 nv50_pior_atomic_check(struct drm_encoder *encoder,
1949 		       struct drm_crtc_state *crtc_state,
1950 		       struct drm_connector_state *conn_state)
1951 {
1952 	int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1953 	if (ret)
1954 		return ret;
1955 	crtc_state->adjusted_mode.clock *= 2;
1956 	return 0;
1957 }
1958 
1959 static void
1960 nv50_pior_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1961 {
1962 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1963 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1964 	const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1965 
1966 	core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
1967 	nv_encoder->crtc = NULL;
1968 }
1969 
1970 static void
1971 nv50_pior_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1972 {
1973 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1974 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1975 	struct nv50_head_atom *asyh =
1976 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1977 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1978 	u32 ctrl = 0;
1979 
1980 	switch (nv_crtc->index) {
1981 	case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
1982 	case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
1983 	default:
1984 		WARN_ON(1);
1985 		break;
1986 	}
1987 
1988 	switch (asyh->or.bpc) {
1989 	case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
1990 	case  8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
1991 	case  6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
1992 	default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1993 	}
1994 
1995 	if (!nvif_outp_acquired(&nv_encoder->outp))
1996 		nvif_outp_acquire_pior(&nv_encoder->outp);
1997 
1998 	switch (nv_encoder->dcb->type) {
1999 	case DCB_OUTPUT_TMDS:
2000 		ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
2001 		break;
2002 	case DCB_OUTPUT_DP:
2003 		ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
2004 		nouveau_dp_train(nv_encoder, false, asyh->state.adjusted_mode.clock, 6);
2005 		break;
2006 	default:
2007 		BUG();
2008 		break;
2009 	}
2010 
2011 	core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
2012 	nv_encoder->crtc = &nv_crtc->base;
2013 }
2014 
2015 static const struct drm_encoder_helper_funcs
2016 nv50_pior_help = {
2017 	.atomic_check = nv50_pior_atomic_check,
2018 	.atomic_enable = nv50_pior_atomic_enable,
2019 	.atomic_disable = nv50_pior_atomic_disable,
2020 };
2021 
2022 static void
2023 nv50_pior_destroy(struct drm_encoder *encoder)
2024 {
2025 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
2026 
2027 	nvif_outp_dtor(&nv_encoder->outp);
2028 
2029 	drm_encoder_cleanup(encoder);
2030 
2031 	mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
2032 	kfree(encoder);
2033 }
2034 
2035 static const struct drm_encoder_funcs
2036 nv50_pior_func = {
2037 	.destroy = nv50_pior_destroy,
2038 };
2039 
2040 static int
2041 nv50_pior_create(struct nouveau_encoder *nv_encoder)
2042 {
2043 	struct drm_connector *connector = &nv_encoder->conn->base;
2044 	struct drm_device *dev = connector->dev;
2045 	struct nouveau_drm *drm = nouveau_drm(dev);
2046 	struct nv50_disp *disp = nv50_disp(dev);
2047 	struct nvkm_i2c *i2c = nvxx_i2c(drm);
2048 	struct nvkm_i2c_bus *bus = NULL;
2049 	struct nvkm_i2c_aux *aux = NULL;
2050 	struct i2c_adapter *ddc;
2051 	struct drm_encoder *encoder;
2052 	struct dcb_output *dcbe = nv_encoder->dcb;
2053 	int type;
2054 
2055 	switch (dcbe->type) {
2056 	case DCB_OUTPUT_TMDS:
2057 		bus  = nvkm_i2c_bus_find(i2c, nv_encoder->outp.info.ddc);
2058 		ddc  = bus ? &bus->i2c : NULL;
2059 		type = DRM_MODE_ENCODER_TMDS;
2060 		break;
2061 	case DCB_OUTPUT_DP:
2062 		aux  = nvkm_i2c_aux_find(i2c, nv_encoder->outp.info.dp.aux);
2063 		ddc  = aux ? &aux->i2c : NULL;
2064 		type = DRM_MODE_ENCODER_TMDS;
2065 		break;
2066 	default:
2067 		return -ENODEV;
2068 	}
2069 
2070 	nv_encoder->i2c = ddc;
2071 
2072 	mutex_init(&nv_encoder->dp.hpd_irq_lock);
2073 
2074 	encoder = to_drm_encoder(nv_encoder);
2075 	drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
2076 			 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
2077 	drm_encoder_helper_add(encoder, &nv50_pior_help);
2078 
2079 	drm_connector_attach_encoder(connector, encoder);
2080 
2081 	disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
2082 	nv50_outp_dump_caps(drm, nv_encoder);
2083 
2084 	return 0;
2085 }
2086 
2087 /******************************************************************************
2088  * Atomic
2089  *****************************************************************************/
2090 
2091 static void
2092 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
2093 {
2094 	struct drm_dp_mst_topology_mgr *mgr;
2095 	struct drm_dp_mst_topology_state *mst_state;
2096 	struct nouveau_drm *drm = nouveau_drm(state->dev);
2097 	struct nv50_disp *disp = nv50_disp(drm->dev);
2098 	struct nv50_atom *atom = nv50_atom(state);
2099 	struct nv50_core *core = disp->core;
2100 	struct nv50_outp_atom *outp;
2101 	struct nv50_mstm *mstm;
2102 	int i;
2103 
2104 	NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
2105 
2106 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
2107 		mstm = nv50_mstm(mgr);
2108 		if (mstm->modified)
2109 			nv50_mstm_prepare(state, mst_state, mstm);
2110 	}
2111 
2112 	core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
2113 	core->func->update(core, interlock, true);
2114 	if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
2115 				       disp->core->chan.base.device))
2116 		NV_ERROR(drm, "core notifier timeout\n");
2117 
2118 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
2119 		mstm = nv50_mstm(mgr);
2120 		if (mstm->modified)
2121 			nv50_mstm_cleanup(state, mst_state, mstm);
2122 	}
2123 
2124 	list_for_each_entry(outp, &atom->outp, head) {
2125 		if (outp->encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2126 			struct nouveau_encoder *nv_encoder = nouveau_encoder(outp->encoder);
2127 
2128 			if (outp->enabled) {
2129 				nv50_audio_enable(outp->encoder, nouveau_crtc(nv_encoder->crtc),
2130 						  nv_encoder->conn, NULL, NULL);
2131 				outp->enabled = outp->disabled = false;
2132 			} else {
2133 				if (outp->disabled) {
2134 					nvif_outp_release(&nv_encoder->outp);
2135 					outp->disabled = false;
2136 				}
2137 			}
2138 		}
2139 	}
2140 }
2141 
2142 static void
2143 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
2144 {
2145 	struct drm_plane_state *new_plane_state;
2146 	struct drm_plane *plane;
2147 	int i;
2148 
2149 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2150 		struct nv50_wndw *wndw = nv50_wndw(plane);
2151 		if (interlock[wndw->interlock.type] & wndw->interlock.data) {
2152 			if (wndw->func->update)
2153 				wndw->func->update(wndw, interlock);
2154 		}
2155 	}
2156 }
2157 
2158 static void
2159 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
2160 {
2161 	struct drm_device *dev = state->dev;
2162 	struct drm_crtc_state *new_crtc_state, *old_crtc_state;
2163 	struct drm_crtc *crtc;
2164 	struct drm_plane_state *new_plane_state;
2165 	struct drm_plane *plane;
2166 	struct nouveau_drm *drm = nouveau_drm(dev);
2167 	struct nv50_disp *disp = nv50_disp(dev);
2168 	struct nv50_atom *atom = nv50_atom(state);
2169 	struct nv50_core *core = disp->core;
2170 	struct nv50_outp_atom *outp, *outt;
2171 	u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
2172 	int i;
2173 	bool flushed = false;
2174 
2175 	NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2176 	nv50_crc_atomic_stop_reporting(state);
2177 	drm_atomic_helper_wait_for_fences(dev, state, false);
2178 	drm_atomic_helper_wait_for_dependencies(state);
2179 	drm_dp_mst_atomic_wait_for_dependencies(state);
2180 	drm_atomic_helper_update_legacy_modeset_state(dev, state);
2181 	drm_atomic_helper_calc_timestamping_constants(state);
2182 
2183 	if (atom->lock_core)
2184 		mutex_lock(&disp->mutex);
2185 
2186 	/* Disable head(s). */
2187 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2188 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2189 		struct nv50_head *head = nv50_head(crtc);
2190 
2191 		NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
2192 			  asyh->clr.mask, asyh->set.mask);
2193 
2194 		if (old_crtc_state->active && !new_crtc_state->active) {
2195 			pm_runtime_put_noidle(dev->dev);
2196 			drm_crtc_vblank_off(crtc);
2197 		}
2198 
2199 		if (asyh->clr.mask) {
2200 			nv50_head_flush_clr(head, asyh, atom->flush_disable);
2201 			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2202 		}
2203 	}
2204 
2205 	/* Disable plane(s). */
2206 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2207 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2208 		struct nv50_wndw *wndw = nv50_wndw(plane);
2209 
2210 		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
2211 			  asyw->clr.mask, asyw->set.mask);
2212 		if (!asyw->clr.mask)
2213 			continue;
2214 
2215 		nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2216 	}
2217 
2218 	/* Disable output path(s). */
2219 	list_for_each_entry(outp, &atom->outp, head) {
2220 		const struct drm_encoder_helper_funcs *help;
2221 		struct drm_encoder *encoder;
2222 
2223 		encoder = outp->encoder;
2224 		help = encoder->helper_private;
2225 
2226 		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2227 			  outp->clr.mask, outp->set.mask);
2228 
2229 		if (outp->clr.mask) {
2230 			help->atomic_disable(encoder, state);
2231 			outp->disabled = true;
2232 			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2233 		}
2234 	}
2235 
2236 	/* Flush disable. */
2237 	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2238 		if (atom->flush_disable) {
2239 			nv50_disp_atomic_commit_wndw(state, interlock);
2240 			nv50_disp_atomic_commit_core(state, interlock);
2241 			memset(interlock, 0x00, sizeof(interlock));
2242 
2243 			flushed = true;
2244 		}
2245 	}
2246 
2247 	if (flushed)
2248 		nv50_crc_atomic_release_notifier_contexts(state);
2249 	nv50_crc_atomic_init_notifier_contexts(state);
2250 
2251 	/* Update output path(s). */
2252 	list_for_each_entry(outp, &atom->outp, head) {
2253 		const struct drm_encoder_helper_funcs *help;
2254 		struct drm_encoder *encoder;
2255 
2256 		encoder = outp->encoder;
2257 		help = encoder->helper_private;
2258 
2259 		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2260 			  outp->set.mask, outp->clr.mask);
2261 
2262 		if (outp->set.mask) {
2263 			help->atomic_enable(encoder, state);
2264 			outp->enabled = true;
2265 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2266 		}
2267 	}
2268 
2269 	/* Update head(s). */
2270 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2271 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2272 		struct nv50_head *head = nv50_head(crtc);
2273 
2274 		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2275 			  asyh->set.mask, asyh->clr.mask);
2276 
2277 		if (asyh->set.mask) {
2278 			nv50_head_flush_set(head, asyh);
2279 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2280 		}
2281 
2282 		if (new_crtc_state->active) {
2283 			if (!old_crtc_state->active) {
2284 				drm_crtc_vblank_on(crtc);
2285 				pm_runtime_get_noresume(dev->dev);
2286 			}
2287 			if (new_crtc_state->event)
2288 				drm_crtc_vblank_get(crtc);
2289 		}
2290 	}
2291 
2292 	/* Update window->head assignment.
2293 	 *
2294 	 * This has to happen in an update that's not interlocked with
2295 	 * any window channels to avoid hitting HW error checks.
2296 	 *
2297 	 *TODO: Proper handling of window ownership (Turing apparently
2298 	 *      supports non-fixed mappings).
2299 	 */
2300 	if (core->assign_windows) {
2301 		core->func->wndw.owner(core);
2302 		nv50_disp_atomic_commit_core(state, interlock);
2303 		core->assign_windows = false;
2304 		interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2305 	}
2306 
2307 	/* Finish updating head(s)...
2308 	 *
2309 	 * NVD is rather picky about both where window assignments can change,
2310 	 * *and* about certain core and window channel states matching.
2311 	 *
2312 	 * The EFI GOP driver on newer GPUs configures window channels with a
2313 	 * different output format to what we do, and the core channel update
2314 	 * in the assign_windows case above would result in a state mismatch.
2315 	 *
2316 	 * Delay some of the head update until after that point to workaround
2317 	 * the issue.  This only affects the initial modeset.
2318 	 *
2319 	 * TODO: handle this better when adding flexible window mapping
2320 	 */
2321 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2322 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2323 		struct nv50_head *head = nv50_head(crtc);
2324 
2325 		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2326 			  asyh->set.mask, asyh->clr.mask);
2327 
2328 		if (asyh->set.mask) {
2329 			nv50_head_flush_set_wndw(head, asyh);
2330 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2331 		}
2332 	}
2333 
2334 	/* Update plane(s). */
2335 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2336 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2337 		struct nv50_wndw *wndw = nv50_wndw(plane);
2338 
2339 		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2340 			  asyw->set.mask, asyw->clr.mask);
2341 		if ( !asyw->set.mask &&
2342 		    (!asyw->clr.mask || atom->flush_disable))
2343 			continue;
2344 
2345 		nv50_wndw_flush_set(wndw, interlock, asyw);
2346 	}
2347 
2348 	/* Flush update. */
2349 	nv50_disp_atomic_commit_wndw(state, interlock);
2350 
2351 	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2352 		if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2353 		    interlock[NV50_DISP_INTERLOCK_OVLY] ||
2354 		    interlock[NV50_DISP_INTERLOCK_WNDW] ||
2355 		    !atom->state.legacy_cursor_update)
2356 			nv50_disp_atomic_commit_core(state, interlock);
2357 		else
2358 			disp->core->func->update(disp->core, interlock, false);
2359 	}
2360 
2361 	if (atom->lock_core)
2362 		mutex_unlock(&disp->mutex);
2363 
2364 	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2365 		list_del(&outp->head);
2366 		kfree(outp);
2367 	}
2368 
2369 	/* Wait for HW to signal completion. */
2370 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2371 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2372 		struct nv50_wndw *wndw = nv50_wndw(plane);
2373 		int ret = nv50_wndw_wait_armed(wndw, asyw);
2374 		if (ret)
2375 			NV_ERROR(drm, "%s: timeout\n", plane->name);
2376 	}
2377 
2378 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2379 		if (new_crtc_state->event) {
2380 			unsigned long flags;
2381 			/* Get correct count/ts if racing with vblank irq */
2382 			if (new_crtc_state->active)
2383 				drm_crtc_accurate_vblank_count(crtc);
2384 			spin_lock_irqsave(&crtc->dev->event_lock, flags);
2385 			drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2386 			spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2387 
2388 			new_crtc_state->event = NULL;
2389 			if (new_crtc_state->active)
2390 				drm_crtc_vblank_put(crtc);
2391 		}
2392 	}
2393 
2394 	nv50_crc_atomic_start_reporting(state);
2395 	if (!flushed)
2396 		nv50_crc_atomic_release_notifier_contexts(state);
2397 
2398 	drm_atomic_helper_commit_hw_done(state);
2399 	drm_atomic_helper_cleanup_planes(dev, state);
2400 	drm_atomic_helper_commit_cleanup_done(state);
2401 	drm_atomic_state_put(state);
2402 
2403 	/* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2404 	pm_runtime_mark_last_busy(dev->dev);
2405 	pm_runtime_put_autosuspend(dev->dev);
2406 }
2407 
2408 static void
2409 nv50_disp_atomic_commit_work(struct work_struct *work)
2410 {
2411 	struct drm_atomic_state *state =
2412 		container_of(work, typeof(*state), commit_work);
2413 	nv50_disp_atomic_commit_tail(state);
2414 }
2415 
2416 static int
2417 nv50_disp_atomic_commit(struct drm_device *dev,
2418 			struct drm_atomic_state *state, bool nonblock)
2419 {
2420 	struct drm_plane_state *new_plane_state;
2421 	struct drm_plane *plane;
2422 	int ret, i;
2423 
2424 	ret = pm_runtime_get_sync(dev->dev);
2425 	if (ret < 0 && ret != -EACCES) {
2426 		pm_runtime_put_autosuspend(dev->dev);
2427 		return ret;
2428 	}
2429 
2430 	ret = drm_atomic_helper_setup_commit(state, nonblock);
2431 	if (ret)
2432 		goto done;
2433 
2434 	INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2435 
2436 	ret = drm_atomic_helper_prepare_planes(dev, state);
2437 	if (ret)
2438 		goto done;
2439 
2440 	if (!nonblock) {
2441 		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2442 		if (ret)
2443 			goto err_cleanup;
2444 	}
2445 
2446 	ret = drm_atomic_helper_swap_state(state, true);
2447 	if (ret)
2448 		goto err_cleanup;
2449 
2450 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2451 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2452 		struct nv50_wndw *wndw = nv50_wndw(plane);
2453 
2454 		if (asyw->set.image)
2455 			nv50_wndw_ntfy_enable(wndw, asyw);
2456 	}
2457 
2458 	drm_atomic_state_get(state);
2459 
2460 	/*
2461 	 * Grab another RPM ref for the commit tail, which will release the
2462 	 * ref when it's finished
2463 	 */
2464 	pm_runtime_get_noresume(dev->dev);
2465 
2466 	if (nonblock)
2467 		queue_work(system_unbound_wq, &state->commit_work);
2468 	else
2469 		nv50_disp_atomic_commit_tail(state);
2470 
2471 err_cleanup:
2472 	if (ret)
2473 		drm_atomic_helper_unprepare_planes(dev, state);
2474 done:
2475 	pm_runtime_put_autosuspend(dev->dev);
2476 	return ret;
2477 }
2478 
2479 static struct nv50_outp_atom *
2480 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2481 {
2482 	struct nv50_outp_atom *outp;
2483 
2484 	list_for_each_entry(outp, &atom->outp, head) {
2485 		if (outp->encoder == encoder)
2486 			return outp;
2487 	}
2488 
2489 	outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2490 	if (!outp)
2491 		return ERR_PTR(-ENOMEM);
2492 
2493 	list_add(&outp->head, &atom->outp);
2494 	outp->encoder = encoder;
2495 	return outp;
2496 }
2497 
2498 static int
2499 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2500 				struct drm_connector_state *old_connector_state)
2501 {
2502 	struct drm_encoder *encoder = old_connector_state->best_encoder;
2503 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2504 	struct drm_crtc *crtc;
2505 	struct nv50_outp_atom *outp;
2506 
2507 	if (!(crtc = old_connector_state->crtc))
2508 		return 0;
2509 
2510 	old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2511 	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2512 	if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2513 		outp = nv50_disp_outp_atomic_add(atom, encoder);
2514 		if (IS_ERR(outp))
2515 			return PTR_ERR(outp);
2516 
2517 		if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST ||
2518 		    nouveau_encoder(outp->encoder)->dcb->type == DCB_OUTPUT_DP)
2519 			atom->flush_disable = true;
2520 		outp->clr.ctrl = true;
2521 		atom->lock_core = true;
2522 	}
2523 
2524 	return 0;
2525 }
2526 
2527 static int
2528 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2529 				struct drm_connector_state *connector_state)
2530 {
2531 	struct drm_encoder *encoder = connector_state->best_encoder;
2532 	struct drm_crtc_state *new_crtc_state;
2533 	struct drm_crtc *crtc;
2534 	struct nv50_outp_atom *outp;
2535 
2536 	if (!(crtc = connector_state->crtc))
2537 		return 0;
2538 
2539 	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2540 	if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2541 		outp = nv50_disp_outp_atomic_add(atom, encoder);
2542 		if (IS_ERR(outp))
2543 			return PTR_ERR(outp);
2544 
2545 		outp->set.ctrl = true;
2546 		atom->lock_core = true;
2547 	}
2548 
2549 	return 0;
2550 }
2551 
2552 static int
2553 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2554 {
2555 	struct nv50_atom *atom = nv50_atom(state);
2556 	struct nv50_core *core = nv50_disp(dev)->core;
2557 	struct drm_connector_state *old_connector_state, *new_connector_state;
2558 	struct drm_connector *connector;
2559 	struct drm_crtc_state *new_crtc_state;
2560 	struct drm_crtc *crtc;
2561 	struct nv50_head *head;
2562 	struct nv50_head_atom *asyh;
2563 	int ret, i;
2564 
2565 	if (core->assign_windows && core->func->head->static_wndw_map) {
2566 		drm_for_each_crtc(crtc, dev) {
2567 			new_crtc_state = drm_atomic_get_crtc_state(state,
2568 								   crtc);
2569 			if (IS_ERR(new_crtc_state))
2570 				return PTR_ERR(new_crtc_state);
2571 
2572 			head = nv50_head(crtc);
2573 			asyh = nv50_head_atom(new_crtc_state);
2574 			core->func->head->static_wndw_map(head, asyh);
2575 		}
2576 	}
2577 
2578 	/* We need to handle colour management on a per-plane basis. */
2579 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2580 		if (new_crtc_state->color_mgmt_changed) {
2581 			ret = drm_atomic_add_affected_planes(state, crtc);
2582 			if (ret)
2583 				return ret;
2584 		}
2585 	}
2586 
2587 	ret = drm_atomic_helper_check(dev, state);
2588 	if (ret)
2589 		return ret;
2590 
2591 	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2592 		ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2593 		if (ret)
2594 			return ret;
2595 
2596 		ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2597 		if (ret)
2598 			return ret;
2599 	}
2600 
2601 	ret = drm_dp_mst_atomic_check(state);
2602 	if (ret)
2603 		return ret;
2604 
2605 	nv50_crc_atomic_check_outp(atom);
2606 
2607 	return 0;
2608 }
2609 
2610 static void
2611 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2612 {
2613 	struct nv50_atom *atom = nv50_atom(state);
2614 	struct nv50_outp_atom *outp, *outt;
2615 
2616 	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2617 		list_del(&outp->head);
2618 		kfree(outp);
2619 	}
2620 
2621 	drm_atomic_state_default_clear(state);
2622 }
2623 
2624 static void
2625 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2626 {
2627 	struct nv50_atom *atom = nv50_atom(state);
2628 	drm_atomic_state_default_release(&atom->state);
2629 	kfree(atom);
2630 }
2631 
2632 static struct drm_atomic_state *
2633 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2634 {
2635 	struct nv50_atom *atom;
2636 	if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2637 	    drm_atomic_state_init(dev, &atom->state) < 0) {
2638 		kfree(atom);
2639 		return NULL;
2640 	}
2641 	INIT_LIST_HEAD(&atom->outp);
2642 	return &atom->state;
2643 }
2644 
2645 static const struct drm_mode_config_funcs
2646 nv50_disp_func = {
2647 	.fb_create = nouveau_user_framebuffer_create,
2648 	.atomic_check = nv50_disp_atomic_check,
2649 	.atomic_commit = nv50_disp_atomic_commit,
2650 	.atomic_state_alloc = nv50_disp_atomic_state_alloc,
2651 	.atomic_state_clear = nv50_disp_atomic_state_clear,
2652 	.atomic_state_free = nv50_disp_atomic_state_free,
2653 };
2654 
2655 static const struct drm_mode_config_helper_funcs
2656 nv50_disp_helper_func = {
2657 	.atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
2658 };
2659 
2660 /******************************************************************************
2661  * Init
2662  *****************************************************************************/
2663 
2664 static void
2665 nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2666 {
2667 	struct nouveau_drm *drm = nouveau_drm(dev);
2668 	struct drm_encoder *encoder;
2669 
2670 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2671 		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
2672 			nv50_mstm_fini(nouveau_encoder(encoder));
2673 	}
2674 
2675 	if (!runtime && !drm->headless)
2676 		cancel_work_sync(&drm->hpd_work);
2677 }
2678 
2679 static inline void
2680 nv50_display_read_hw_or_state(struct drm_device *dev, struct nv50_disp *disp,
2681 			      struct nouveau_encoder *outp)
2682 {
2683 	struct drm_crtc *crtc;
2684 	struct drm_connector_list_iter conn_iter;
2685 	struct drm_connector *conn;
2686 	struct nv50_head_atom *armh;
2687 	const u32 encoder_mask = drm_encoder_mask(&outp->base.base);
2688 	bool found_conn = false, found_head = false;
2689 	u8 proto;
2690 	int head_idx;
2691 	int ret;
2692 
2693 	switch (outp->dcb->type) {
2694 	case DCB_OUTPUT_TMDS:
2695 		ret = nvif_outp_inherit_tmds(&outp->outp, &proto);
2696 		break;
2697 	case DCB_OUTPUT_DP:
2698 		ret = nvif_outp_inherit_dp(&outp->outp, &proto);
2699 		break;
2700 	case DCB_OUTPUT_LVDS:
2701 		ret = nvif_outp_inherit_lvds(&outp->outp, &proto);
2702 		break;
2703 	case DCB_OUTPUT_ANALOG:
2704 		ret = nvif_outp_inherit_rgb_crt(&outp->outp, &proto);
2705 		break;
2706 	default:
2707 		drm_dbg_kms(dev, "Readback for %s not implemented yet, skipping\n",
2708 			    outp->base.base.name);
2709 		drm_WARN_ON(dev, true);
2710 		return;
2711 	}
2712 
2713 	if (ret < 0)
2714 		return;
2715 
2716 	head_idx = ret;
2717 
2718 	drm_for_each_crtc(crtc, dev) {
2719 		if (crtc->index != head_idx)
2720 			continue;
2721 
2722 		armh = nv50_head_atom(crtc->state);
2723 		found_head = true;
2724 		break;
2725 	}
2726 	if (drm_WARN_ON(dev, !found_head))
2727 		return;
2728 
2729 	/* Figure out which connector is being used by this encoder */
2730 	drm_connector_list_iter_begin(dev, &conn_iter);
2731 	nouveau_for_each_non_mst_connector_iter(conn, &conn_iter) {
2732 		if (nouveau_connector(conn)->index == outp->dcb->connector) {
2733 			found_conn = true;
2734 			break;
2735 		}
2736 	}
2737 	drm_connector_list_iter_end(&conn_iter);
2738 	if (drm_WARN_ON(dev, !found_conn))
2739 		return;
2740 
2741 	armh->state.encoder_mask = encoder_mask;
2742 	armh->state.connector_mask = drm_connector_mask(conn);
2743 	armh->state.active = true;
2744 	armh->state.enable = true;
2745 	pm_runtime_get_noresume(dev->dev);
2746 
2747 	outp->crtc = crtc;
2748 	outp->ctrl = NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto) | BIT(crtc->index);
2749 
2750 	drm_connector_get(conn);
2751 	conn->state->crtc = crtc;
2752 	conn->state->best_encoder = &outp->base.base;
2753 }
2754 
2755 /* Read back the currently programmed display state */
2756 static void
2757 nv50_display_read_hw_state(struct nouveau_drm *drm)
2758 {
2759 	struct drm_device *dev = drm->dev;
2760 	struct drm_encoder *encoder;
2761 	struct drm_modeset_acquire_ctx ctx;
2762 	struct nv50_disp *disp = nv50_disp(dev);
2763 	int ret;
2764 
2765 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
2766 
2767 	drm_for_each_encoder(encoder, dev) {
2768 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
2769 			continue;
2770 
2771 		nv50_display_read_hw_or_state(dev, disp, nouveau_encoder(encoder));
2772 	}
2773 
2774 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
2775 }
2776 
2777 static int
2778 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2779 {
2780 	struct nv50_core *core = nv50_disp(dev)->core;
2781 	struct drm_encoder *encoder;
2782 
2783 	if (resume || runtime)
2784 		core->func->init(core);
2785 
2786 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2787 		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2788 			struct nouveau_encoder *nv_encoder =
2789 				nouveau_encoder(encoder);
2790 			nv50_mstm_init(nv_encoder, runtime);
2791 		}
2792 	}
2793 
2794 	if (!resume)
2795 		nv50_display_read_hw_state(nouveau_drm(dev));
2796 
2797 	return 0;
2798 }
2799 
2800 static void
2801 nv50_display_destroy(struct drm_device *dev)
2802 {
2803 	struct nv50_disp *disp = nv50_disp(dev);
2804 
2805 	nv50_audio_component_fini(nouveau_drm(dev));
2806 
2807 	nvif_object_unmap(&disp->caps);
2808 	nvif_object_dtor(&disp->caps);
2809 	nv50_core_del(&disp->core);
2810 
2811 	nouveau_bo_unmap(disp->sync);
2812 	if (disp->sync)
2813 		nouveau_bo_unpin(disp->sync);
2814 	nouveau_bo_fini(disp->sync);
2815 
2816 	nouveau_display(dev)->priv = NULL;
2817 	kfree(disp);
2818 }
2819 
2820 int
2821 nv50_display_create(struct drm_device *dev)
2822 {
2823 	struct nouveau_drm *drm = nouveau_drm(dev);
2824 	struct drm_connector *connector, *tmp;
2825 	struct nv50_disp *disp;
2826 	int ret, i;
2827 	bool has_mst = false;
2828 
2829 	disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2830 	if (!disp)
2831 		return -ENOMEM;
2832 
2833 	mutex_init(&disp->mutex);
2834 
2835 	nouveau_display(dev)->priv = disp;
2836 	nouveau_display(dev)->dtor = nv50_display_destroy;
2837 	nouveau_display(dev)->init = nv50_display_init;
2838 	nouveau_display(dev)->fini = nv50_display_fini;
2839 	disp->disp = &nouveau_display(dev)->disp;
2840 	dev->mode_config.funcs = &nv50_disp_func;
2841 	dev->mode_config.helper_private = &nv50_disp_helper_func;
2842 	dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2843 	dev->mode_config.normalize_zpos = true;
2844 
2845 	/* small shared memory area we use for notifiers and semaphores */
2846 	ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
2847 			     NOUVEAU_GEM_DOMAIN_VRAM,
2848 			     0, 0x0000, NULL, NULL, &disp->sync);
2849 	if (!ret) {
2850 		ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2851 		if (!ret) {
2852 			ret = nouveau_bo_map(disp->sync);
2853 			if (ret)
2854 				nouveau_bo_unpin(disp->sync);
2855 		}
2856 		if (ret)
2857 			nouveau_bo_fini(disp->sync);
2858 	}
2859 
2860 	if (ret)
2861 		goto out;
2862 
2863 	/* allocate master evo channel */
2864 	ret = nv50_core_new(drm, &disp->core);
2865 	if (ret)
2866 		goto out;
2867 
2868 	disp->core->func->init(disp->core);
2869 	if (disp->core->func->caps_init) {
2870 		ret = disp->core->func->caps_init(drm, disp);
2871 		if (ret)
2872 			goto out;
2873 	}
2874 
2875 	/* Assign the correct format modifiers */
2876 	if (disp->disp->object.oclass >= TU102_DISP)
2877 		nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2878 	else
2879 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2880 		nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2881 	else
2882 		nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2883 
2884 	/* FIXME: 256x256 cursors are supported on Kepler, however unlike Maxwell and later
2885 	 * generations Kepler requires that we use small pages (4K) for cursor scanout surfaces. The
2886 	 * proper fix for this is to teach nouveau to migrate fbs being used for the cursor plane to
2887 	 * small page allocations in prepare_fb(). When this is implemented, we should also force
2888 	 * large pages (128K) for ovly fbs in order to fix Kepler ovlys.
2889 	 * But until then, just limit cursors to 128x128 - which is small enough to avoid ever using
2890 	 * large pages.
2891 	 */
2892 	if (disp->disp->object.oclass >= GM107_DISP) {
2893 		dev->mode_config.cursor_width = 256;
2894 		dev->mode_config.cursor_height = 256;
2895 	} else if (disp->disp->object.oclass >= GK104_DISP) {
2896 		dev->mode_config.cursor_width = 128;
2897 		dev->mode_config.cursor_height = 128;
2898 	} else {
2899 		dev->mode_config.cursor_width = 64;
2900 		dev->mode_config.cursor_height = 64;
2901 	}
2902 
2903 	/* create encoder/connector objects based on VBIOS DCB table */
2904 	for_each_set_bit(i, &disp->disp->outp_mask, sizeof(disp->disp->outp_mask) * 8) {
2905 		struct nouveau_encoder *outp;
2906 
2907 		outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2908 		if (!outp)
2909 			break;
2910 
2911 		ret = nvif_outp_ctor(disp->disp, "kmsOutp", i, &outp->outp);
2912 		if (ret) {
2913 			kfree(outp);
2914 			continue;
2915 		}
2916 
2917 		connector = nouveau_connector_create(dev, outp->outp.info.conn);
2918 		if (IS_ERR(connector)) {
2919 			nvif_outp_dtor(&outp->outp);
2920 			kfree(outp);
2921 			continue;
2922 		}
2923 
2924 		outp->base.base.possible_crtcs = outp->outp.info.heads;
2925 		outp->base.base.possible_clones = 0;
2926 		outp->conn = nouveau_connector(connector);
2927 
2928 		outp->dcb = kzalloc(sizeof(*outp->dcb), GFP_KERNEL);
2929 		if (!outp->dcb)
2930 			break;
2931 
2932 		switch (outp->outp.info.proto) {
2933 		case NVIF_OUTP_RGB_CRT:
2934 			outp->dcb->type = DCB_OUTPUT_ANALOG;
2935 			outp->dcb->crtconf.maxfreq = outp->outp.info.rgb_crt.freq_max;
2936 			break;
2937 		case NVIF_OUTP_TMDS:
2938 			outp->dcb->type = DCB_OUTPUT_TMDS;
2939 			outp->dcb->duallink_possible = outp->outp.info.tmds.dual;
2940 			break;
2941 		case NVIF_OUTP_LVDS:
2942 			outp->dcb->type = DCB_OUTPUT_LVDS;
2943 			outp->dcb->lvdsconf.use_acpi_for_edid = outp->outp.info.lvds.acpi_edid;
2944 			break;
2945 		case NVIF_OUTP_DP:
2946 			outp->dcb->type = DCB_OUTPUT_DP;
2947 			outp->dcb->dpconf.link_nr = outp->outp.info.dp.link_nr;
2948 			outp->dcb->dpconf.link_bw = outp->outp.info.dp.link_bw;
2949 			if (outp->outp.info.dp.mst)
2950 				has_mst = true;
2951 			break;
2952 		default:
2953 			WARN_ON(1);
2954 			continue;
2955 		}
2956 
2957 		outp->dcb->heads = outp->outp.info.heads;
2958 		outp->dcb->connector = outp->outp.info.conn;
2959 		outp->dcb->i2c_index = outp->outp.info.ddc;
2960 
2961 		switch (outp->outp.info.type) {
2962 		case NVIF_OUTP_DAC : ret = nv50_dac_create(outp); break;
2963 		case NVIF_OUTP_SOR : ret = nv50_sor_create(outp); break;
2964 		case NVIF_OUTP_PIOR: ret = nv50_pior_create(outp); break;
2965 		default:
2966 			WARN_ON(1);
2967 			continue;
2968 		}
2969 
2970 		if (ret) {
2971 			NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2972 				i, outp->outp.info.type, outp->outp.info.proto, ret);
2973 		}
2974 	}
2975 
2976 	/* cull any connectors we created that don't have an encoder */
2977 	list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2978 		if (connector->possible_encoders)
2979 			continue;
2980 
2981 		NV_WARN(drm, "%s has no encoders, removing\n",
2982 			connector->name);
2983 		connector->funcs->destroy(connector);
2984 	}
2985 
2986 	/* create crtc objects to represent the hw heads */
2987 	for_each_set_bit(i, &disp->disp->head_mask, sizeof(disp->disp->head_mask) * 8) {
2988 		struct nv50_head *head;
2989 
2990 		head = nv50_head_create(dev, i);
2991 		if (IS_ERR(head)) {
2992 			ret = PTR_ERR(head);
2993 			goto out;
2994 		}
2995 
2996 		if (has_mst) {
2997 			head->msto = nv50_msto_new(dev, head, i);
2998 			if (IS_ERR(head->msto)) {
2999 				ret = PTR_ERR(head->msto);
3000 				head->msto = NULL;
3001 				goto out;
3002 			}
3003 
3004 			/*
3005 			 * FIXME: This is a hack to workaround the following
3006 			 * issues:
3007 			 *
3008 			 * https://gitlab.gnome.org/GNOME/mutter/issues/759
3009 			 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
3010 			 *
3011 			 * Once these issues are closed, this should be
3012 			 * removed
3013 			 */
3014 			head->msto->encoder.possible_crtcs = disp->disp->head_mask;
3015 		}
3016 	}
3017 
3018 	/* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
3019 	dev->vblank_disable_immediate = true;
3020 
3021 	nv50_audio_component_init(drm);
3022 
3023 out:
3024 	if (ret)
3025 		nv50_display_destroy(dev);
3026 	return ret;
3027 }
3028 
3029 /******************************************************************************
3030  * Format modifiers
3031  *****************************************************************************/
3032 
3033 /****************************************************************
3034  *            Log2(block height) ----------------------------+  *
3035  *            Page Kind ----------------------------------+  |  *
3036  *            Gob Height/Page Kind Generation ------+     |  |  *
3037  *                          Sector layout -------+  |     |  |  *
3038  *                          Compression ------+  |  |     |  |  */
3039 const u64 disp50xx_modifiers[] = { /*         |  |  |     |  |  */
3040 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
3041 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
3042 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
3043 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
3044 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
3045 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
3046 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
3047 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
3048 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
3049 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
3050 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
3051 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
3052 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
3053 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
3054 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
3055 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
3056 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
3057 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
3058 	DRM_FORMAT_MOD_LINEAR,
3059 	DRM_FORMAT_MOD_INVALID
3060 };
3061 
3062 /****************************************************************
3063  *            Log2(block height) ----------------------------+  *
3064  *            Page Kind ----------------------------------+  |  *
3065  *            Gob Height/Page Kind Generation ------+     |  |  *
3066  *                          Sector layout -------+  |     |  |  *
3067  *                          Compression ------+  |  |     |  |  */
3068 const u64 disp90xx_modifiers[] = { /*         |  |  |     |  |  */
3069 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
3070 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
3071 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
3072 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
3073 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
3074 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
3075 	DRM_FORMAT_MOD_LINEAR,
3076 	DRM_FORMAT_MOD_INVALID
3077 };
3078