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/dma-mapping.h>
32 #include <linux/hdmi.h>
33 #include <linux/component.h>
34 #include <linux/iopoll.h>
35 
36 #include <drm/display/drm_dp_helper.h>
37 #include <drm/display/drm_scdc_helper.h>
38 #include <drm/drm_atomic.h>
39 #include <drm/drm_atomic_helper.h>
40 #include <drm/drm_edid.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_vblank.h>
44 
45 #include <nvif/push507c.h>
46 
47 #include <nvif/class.h>
48 #include <nvif/cl0002.h>
49 #include <nvif/event.h>
50 #include <nvif/if0012.h>
51 #include <nvif/if0014.h>
52 #include <nvif/timer.h>
53 
54 #include <nvhw/class/cl507c.h>
55 #include <nvhw/class/cl507d.h>
56 #include <nvhw/class/cl837d.h>
57 #include <nvhw/class/cl887d.h>
58 #include <nvhw/class/cl907d.h>
59 #include <nvhw/class/cl917d.h>
60 
61 #include "nouveau_drv.h"
62 #include "nouveau_dma.h"
63 #include "nouveau_gem.h"
64 #include "nouveau_connector.h"
65 #include "nouveau_encoder.h"
66 #include "nouveau_fence.h"
67 #include "nv50_display.h"
68 
69 #include <subdev/bios/dp.h>
70 
71 /******************************************************************************
72  * EVO channel
73  *****************************************************************************/
74 
75 static int
76 nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
77 		 const s32 *oclass, u8 head, void *data, u32 size,
78 		 struct nv50_chan *chan)
79 {
80 	struct nvif_sclass *sclass;
81 	int ret, i, n;
82 
83 	chan->device = device;
84 
85 	ret = n = nvif_object_sclass_get(disp, &sclass);
86 	if (ret < 0)
87 		return ret;
88 
89 	while (oclass[0]) {
90 		for (i = 0; i < n; i++) {
91 			if (sclass[i].oclass == oclass[0]) {
92 				ret = nvif_object_ctor(disp, "kmsChan", 0,
93 						       oclass[0], data, size,
94 						       &chan->user);
95 				if (ret == 0)
96 					nvif_object_map(&chan->user, NULL, 0);
97 				nvif_object_sclass_put(&sclass);
98 				return ret;
99 			}
100 		}
101 		oclass++;
102 	}
103 
104 	nvif_object_sclass_put(&sclass);
105 	return -ENOSYS;
106 }
107 
108 static void
109 nv50_chan_destroy(struct nv50_chan *chan)
110 {
111 	nvif_object_dtor(&chan->user);
112 }
113 
114 /******************************************************************************
115  * DMA EVO channel
116  *****************************************************************************/
117 
118 void
119 nv50_dmac_destroy(struct nv50_dmac *dmac)
120 {
121 	nvif_object_dtor(&dmac->vram);
122 	nvif_object_dtor(&dmac->sync);
123 
124 	nv50_chan_destroy(&dmac->base);
125 
126 	nvif_mem_dtor(&dmac->_push.mem);
127 }
128 
129 static void
130 nv50_dmac_kick(struct nvif_push *push)
131 {
132 	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
133 
134 	dmac->cur = push->cur - (u32 __iomem *)dmac->_push.mem.object.map.ptr;
135 	if (dmac->put != dmac->cur) {
136 		/* Push buffer fetches are not coherent with BAR1, we need to ensure
137 		 * writes have been flushed right through to VRAM before writing PUT.
138 		 */
139 		if (dmac->push->mem.type & NVIF_MEM_VRAM) {
140 			struct nvif_device *device = dmac->base.device;
141 			nvif_wr32(&device->object, 0x070000, 0x00000001);
142 			nvif_msec(device, 2000,
143 				if (!(nvif_rd32(&device->object, 0x070000) & 0x00000002))
144 					break;
145 			);
146 		}
147 
148 		NVIF_WV32(&dmac->base.user, NV507C, PUT, PTR, dmac->cur);
149 		dmac->put = dmac->cur;
150 	}
151 
152 	push->bgn = push->cur;
153 }
154 
155 static int
156 nv50_dmac_free(struct nv50_dmac *dmac)
157 {
158 	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
159 	if (get > dmac->cur) /* NVIDIA stay 5 away from GET, do the same. */
160 		return get - dmac->cur - 5;
161 	return dmac->max - dmac->cur;
162 }
163 
164 static int
165 nv50_dmac_wind(struct nv50_dmac *dmac)
166 {
167 	/* Wait for GET to depart from the beginning of the push buffer to
168 	 * prevent writing PUT == GET, which would be ignored by HW.
169 	 */
170 	u32 get = NVIF_RV32(&dmac->base.user, NV507C, GET, PTR);
171 	if (get == 0) {
172 		/* Corner-case, HW idle, but non-committed work pending. */
173 		if (dmac->put == 0)
174 			nv50_dmac_kick(dmac->push);
175 
176 		if (nvif_msec(dmac->base.device, 2000,
177 			if (NVIF_TV32(&dmac->base.user, NV507C, GET, PTR, >, 0))
178 				break;
179 		) < 0)
180 			return -ETIMEDOUT;
181 	}
182 
183 	PUSH_RSVD(dmac->push, PUSH_JUMP(dmac->push, 0));
184 	dmac->cur = 0;
185 	return 0;
186 }
187 
188 static int
189 nv50_dmac_wait(struct nvif_push *push, u32 size)
190 {
191 	struct nv50_dmac *dmac = container_of(push, typeof(*dmac), _push);
192 	int free;
193 
194 	if (WARN_ON(size > dmac->max))
195 		return -EINVAL;
196 
197 	dmac->cur = push->cur - (u32 __iomem *)dmac->_push.mem.object.map.ptr;
198 	if (dmac->cur + size >= dmac->max) {
199 		int ret = nv50_dmac_wind(dmac);
200 		if (ret)
201 			return ret;
202 
203 		push->cur = dmac->_push.mem.object.map.ptr;
204 		push->cur = push->cur + dmac->cur;
205 		nv50_dmac_kick(push);
206 	}
207 
208 	if (nvif_msec(dmac->base.device, 2000,
209 		if ((free = nv50_dmac_free(dmac)) >= size)
210 			break;
211 	) < 0) {
212 		WARN_ON(1);
213 		return -ETIMEDOUT;
214 	}
215 
216 	push->bgn = dmac->_push.mem.object.map.ptr;
217 	push->bgn = push->bgn + dmac->cur;
218 	push->cur = push->bgn;
219 	push->end = push->cur + free;
220 	return 0;
221 }
222 
223 MODULE_PARM_DESC(kms_vram_pushbuf, "Place EVO/NVD push buffers in VRAM (default: auto)");
224 static int nv50_dmac_vram_pushbuf = -1;
225 module_param_named(kms_vram_pushbuf, nv50_dmac_vram_pushbuf, int, 0400);
226 
227 int
228 nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
229 		 const s32 *oclass, u8 head, void *data, u32 size, s64 syncbuf,
230 		 struct nv50_dmac *dmac)
231 {
232 	struct nouveau_cli *cli = (void *)device->object.client;
233 	struct nvif_disp_chan_v0 *args = data;
234 	u8 type = NVIF_MEM_COHERENT;
235 	int ret;
236 
237 	mutex_init(&dmac->lock);
238 
239 	/* Pascal added support for 47-bit physical addresses, but some
240 	 * parts of EVO still only accept 40-bit PAs.
241 	 *
242 	 * To avoid issues on systems with large amounts of RAM, and on
243 	 * systems where an IOMMU maps pages at a high address, we need
244 	 * to allocate push buffers in VRAM instead.
245 	 *
246 	 * This appears to match NVIDIA's behaviour on Pascal.
247 	 */
248 	if ((nv50_dmac_vram_pushbuf > 0) ||
249 	    (nv50_dmac_vram_pushbuf < 0 && device->info.family == NV_DEVICE_INFO_V0_PASCAL))
250 		type |= NVIF_MEM_VRAM;
251 
252 	ret = nvif_mem_ctor_map(&cli->mmu, "kmsChanPush", type, 0x1000,
253 				&dmac->_push.mem);
254 	if (ret)
255 		return ret;
256 
257 	dmac->ptr = dmac->_push.mem.object.map.ptr;
258 	dmac->_push.wait = nv50_dmac_wait;
259 	dmac->_push.kick = nv50_dmac_kick;
260 	dmac->push = &dmac->_push;
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 	nvif_outp_release(&nv_encoder->outp);
481 }
482 
483 static void
484 nv50_dac_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
485 {
486 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
487 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
488 	struct nv50_head_atom *asyh =
489 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
490 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
491 	u32 ctrl = 0;
492 
493 	switch (nv_crtc->index) {
494 	case 0: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD0); break;
495 	case 1: ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, OWNER, HEAD1); break;
496 	case 2: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD2); break;
497 	case 3: ctrl |= NVDEF(NV907D, DAC_SET_CONTROL, OWNER_MASK, HEAD3); break;
498 	default:
499 		WARN_ON(1);
500 		break;
501 	}
502 
503 	ctrl |= NVDEF(NV507D, DAC_SET_CONTROL, PROTOCOL, RGB_CRT);
504 
505 	nvif_outp_acquire_rgb_crt(&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 drm_connector *connector, struct dcb_output *dcbe)
557 {
558 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
559 	struct nv50_disp *disp = nv50_disp(connector->dev);
560 	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
561 	struct nvkm_i2c_bus *bus;
562 	struct nouveau_encoder *nv_encoder;
563 	struct drm_encoder *encoder;
564 	int type = DRM_MODE_ENCODER_DAC;
565 
566 	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
567 	if (!nv_encoder)
568 		return -ENOMEM;
569 	nv_encoder->dcb = dcbe;
570 
571 	bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
572 	if (bus)
573 		nv_encoder->i2c = &bus->i2c;
574 
575 	encoder = to_drm_encoder(nv_encoder);
576 	encoder->possible_crtcs = dcbe->heads;
577 	encoder->possible_clones = 0;
578 	drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
579 			 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
580 	drm_encoder_helper_add(encoder, &nv50_dac_help);
581 
582 	drm_connector_attach_encoder(connector, encoder);
583 	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
584 }
585 
586 /*
587  * audio component binding for ELD notification
588  */
589 static void
590 nv50_audio_component_eld_notify(struct drm_audio_component *acomp, int port,
591 				int dev_id)
592 {
593 	if (acomp && acomp->audio_ops && acomp->audio_ops->pin_eld_notify)
594 		acomp->audio_ops->pin_eld_notify(acomp->audio_ops->audio_ptr,
595 						 port, dev_id);
596 }
597 
598 static int
599 nv50_audio_component_get_eld(struct device *kdev, int port, int dev_id,
600 			     bool *enabled, unsigned char *buf, int max_bytes)
601 {
602 	struct drm_device *drm_dev = dev_get_drvdata(kdev);
603 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
604 	struct drm_encoder *encoder;
605 	struct nouveau_encoder *nv_encoder;
606 	struct nouveau_crtc *nv_crtc;
607 	int ret = 0;
608 
609 	*enabled = false;
610 
611 	mutex_lock(&drm->audio.lock);
612 
613 	drm_for_each_encoder(encoder, drm->dev) {
614 		struct nouveau_connector *nv_connector = NULL;
615 
616 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST)
617 			continue; /* TODO */
618 
619 		nv_encoder = nouveau_encoder(encoder);
620 		nv_connector = nouveau_connector(nv_encoder->audio.connector);
621 		nv_crtc = nouveau_crtc(nv_encoder->crtc);
622 
623 		if (!nv_crtc || nv_encoder->outp.or.id != port || nv_crtc->index != dev_id)
624 			continue;
625 
626 		*enabled = nv_encoder->audio.enabled;
627 		if (*enabled) {
628 			ret = drm_eld_size(nv_connector->base.eld);
629 			memcpy(buf, nv_connector->base.eld,
630 			       min(max_bytes, ret));
631 		}
632 		break;
633 	}
634 
635 	mutex_unlock(&drm->audio.lock);
636 
637 	return ret;
638 }
639 
640 static const struct drm_audio_component_ops nv50_audio_component_ops = {
641 	.get_eld = nv50_audio_component_get_eld,
642 };
643 
644 static int
645 nv50_audio_component_bind(struct device *kdev, struct device *hda_kdev,
646 			  void *data)
647 {
648 	struct drm_device *drm_dev = dev_get_drvdata(kdev);
649 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
650 	struct drm_audio_component *acomp = data;
651 
652 	if (WARN_ON(!device_link_add(hda_kdev, kdev, DL_FLAG_STATELESS)))
653 		return -ENOMEM;
654 
655 	drm_modeset_lock_all(drm_dev);
656 	acomp->ops = &nv50_audio_component_ops;
657 	acomp->dev = kdev;
658 	drm->audio.component = acomp;
659 	drm_modeset_unlock_all(drm_dev);
660 	return 0;
661 }
662 
663 static void
664 nv50_audio_component_unbind(struct device *kdev, struct device *hda_kdev,
665 			    void *data)
666 {
667 	struct drm_device *drm_dev = dev_get_drvdata(kdev);
668 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
669 	struct drm_audio_component *acomp = data;
670 
671 	drm_modeset_lock_all(drm_dev);
672 	drm->audio.component = NULL;
673 	acomp->ops = NULL;
674 	acomp->dev = NULL;
675 	drm_modeset_unlock_all(drm_dev);
676 }
677 
678 static const struct component_ops nv50_audio_component_bind_ops = {
679 	.bind   = nv50_audio_component_bind,
680 	.unbind = nv50_audio_component_unbind,
681 };
682 
683 static void
684 nv50_audio_component_init(struct nouveau_drm *drm)
685 {
686 	if (component_add(drm->dev->dev, &nv50_audio_component_bind_ops))
687 		return;
688 
689 	drm->audio.component_registered = true;
690 	mutex_init(&drm->audio.lock);
691 }
692 
693 static void
694 nv50_audio_component_fini(struct nouveau_drm *drm)
695 {
696 	if (!drm->audio.component_registered)
697 		return;
698 
699 	component_del(drm->dev->dev, &nv50_audio_component_bind_ops);
700 	drm->audio.component_registered = false;
701 	mutex_destroy(&drm->audio.lock);
702 }
703 
704 /******************************************************************************
705  * Audio
706  *****************************************************************************/
707 static bool
708 nv50_audio_supported(struct drm_encoder *encoder)
709 {
710 	struct nv50_disp *disp = nv50_disp(encoder->dev);
711 
712 	if (disp->disp->object.oclass <= GT200_DISP ||
713 	    disp->disp->object.oclass == GT206_DISP)
714 		return false;
715 
716 	return true;
717 }
718 
719 static void
720 nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
721 {
722 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
723 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
724 	struct nvif_outp *outp = &nv_encoder->outp;
725 
726 	if (!nv50_audio_supported(encoder))
727 		return;
728 
729 	mutex_lock(&drm->audio.lock);
730 	if (nv_encoder->audio.enabled) {
731 		nv_encoder->audio.enabled = false;
732 		nv_encoder->audio.connector = NULL;
733 		nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, NULL, 0);
734 	}
735 	mutex_unlock(&drm->audio.lock);
736 
737 	nv50_audio_component_eld_notify(drm->audio.component, outp->or.id, nv_crtc->index);
738 }
739 
740 static void
741 nv50_audio_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
742 		  struct nouveau_connector *nv_connector, struct drm_atomic_state *state,
743 		  struct drm_display_mode *mode)
744 {
745 	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
746 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
747 	struct nvif_outp *outp = &nv_encoder->outp;
748 
749 	if (!nv50_audio_supported(encoder) || !drm_detect_monitor_audio(nv_connector->edid))
750 		return;
751 
752 	mutex_lock(&drm->audio.lock);
753 
754 	nvif_outp_hda_eld(&nv_encoder->outp, nv_crtc->index, nv_connector->base.eld,
755 			  drm_eld_size(nv_connector->base.eld));
756 	nv_encoder->audio.enabled = true;
757 	nv_encoder->audio.connector = &nv_connector->base;
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 	u8 scdc = 0;
778 	u32 max_ac_packet;
779 	struct {
780 		struct nvif_outp_infoframe_v0 infoframe;
781 		u8 data[17];
782 	} args = { 0 };
783 	int ret, size;
784 
785 	max_ac_packet  = mode->htotal - mode->hdisplay;
786 	max_ac_packet -= rekey;
787 	max_ac_packet -= 18; /* constant from tegra */
788 	max_ac_packet /= 32;
789 
790 	if (hdmi->scdc.scrambling.supported) {
791 		const bool high_tmds_clock_ratio = mode->clock > 340000;
792 
793 		ret = drm_scdc_readb(nv_encoder->i2c, SCDC_TMDS_CONFIG, &scdc);
794 		if (ret < 0) {
795 			NV_ERROR(drm, "Failure to read SCDC_TMDS_CONFIG: %d\n", ret);
796 			return;
797 		}
798 
799 		scdc &= ~(SCDC_TMDS_BIT_CLOCK_RATIO_BY_40 | SCDC_SCRAMBLING_ENABLE);
800 		if (high_tmds_clock_ratio || hdmi->scdc.scrambling.low_rates)
801 			scdc |= SCDC_SCRAMBLING_ENABLE;
802 		if (high_tmds_clock_ratio)
803 			scdc |= SCDC_TMDS_BIT_CLOCK_RATIO_BY_40;
804 
805 		ret = drm_scdc_writeb(nv_encoder->i2c, SCDC_TMDS_CONFIG, scdc);
806 		if (ret < 0)
807 			NV_ERROR(drm, "Failure to write SCDC_TMDS_CONFIG = 0x%02x: %d\n",
808 				 scdc, ret);
809 	}
810 
811 	ret = nvif_outp_acquire_tmds(&nv_encoder->outp, nv_crtc->index, true,
812 				     max_ac_packet, rekey, scdc, hda);
813 	if (ret)
814 		return;
815 
816 	/* AVI InfoFrame. */
817 	args.infoframe.version = 0;
818 	args.infoframe.head = nv_crtc->index;
819 
820 	if (!drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi, &nv_connector->base, mode)) {
821 		drm_hdmi_avi_infoframe_quant_range(&infoframe.avi, &nv_connector->base, mode,
822 						   HDMI_QUANTIZATION_RANGE_FULL);
823 
824 		size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
825 	} else {
826 		size = 0;
827 	}
828 
829 	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, &args.infoframe, size);
830 
831 	/* Vendor InfoFrame. */
832 	memset(&args.data, 0, sizeof(args.data));
833 	if (!drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
834 							 &nv_connector->base, mode))
835 		size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
836 	else
837 		size = 0;
838 
839 	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, &args.infoframe, size);
840 
841 	nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
842 }
843 
844 /******************************************************************************
845  * MST
846  *****************************************************************************/
847 #define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
848 #define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
849 #define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
850 
851 struct nv50_mstc {
852 	struct nv50_mstm *mstm;
853 	struct drm_dp_mst_port *port;
854 	struct drm_connector connector;
855 
856 	struct drm_display_mode *native;
857 	struct edid *edid;
858 };
859 
860 struct nv50_msto {
861 	struct drm_encoder encoder;
862 
863 	/* head is statically assigned on msto creation */
864 	struct nv50_head *head;
865 	struct nv50_mstc *mstc;
866 	bool disabled;
867 	bool enabled;
868 };
869 
870 struct nouveau_encoder *nv50_real_outp(struct drm_encoder *encoder)
871 {
872 	struct nv50_msto *msto;
873 
874 	if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
875 		return nouveau_encoder(encoder);
876 
877 	msto = nv50_msto(encoder);
878 	if (!msto->mstc)
879 		return NULL;
880 	return msto->mstc->mstm->outp;
881 }
882 
883 static void
884 nv50_msto_cleanup(struct drm_atomic_state *state,
885 		  struct drm_dp_mst_topology_state *mst_state,
886 		  struct drm_dp_mst_topology_mgr *mgr,
887 		  struct nv50_msto *msto)
888 {
889 	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
890 	struct drm_dp_mst_atomic_payload *payload =
891 		drm_atomic_get_mst_payload_state(mst_state, msto->mstc->port);
892 
893 	NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
894 
895 	if (msto->disabled) {
896 		msto->mstc = NULL;
897 		msto->disabled = false;
898 	} else if (msto->enabled) {
899 		drm_dp_add_payload_part2(mgr, state, payload);
900 		msto->enabled = false;
901 	}
902 }
903 
904 static void
905 nv50_msto_prepare(struct drm_atomic_state *state,
906 		  struct drm_dp_mst_topology_state *mst_state,
907 		  struct drm_dp_mst_topology_mgr *mgr,
908 		  struct nv50_msto *msto)
909 {
910 	struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
911 	struct nv50_mstc *mstc = msto->mstc;
912 	struct nv50_mstm *mstm = mstc->mstm;
913 	struct drm_dp_mst_atomic_payload *payload;
914 
915 	NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
916 
917 	payload = drm_atomic_get_mst_payload_state(mst_state, mstc->port);
918 
919 	// TODO: Figure out if we want to do a better job of handling VCPI allocation failures here?
920 	if (msto->disabled) {
921 		drm_dp_remove_payload(mgr, mst_state, payload, payload);
922 
923 		nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index, 0, 0, 0, 0);
924 	} else {
925 		if (msto->enabled)
926 			drm_dp_add_payload_part1(mgr, mst_state, payload);
927 
928 		nvif_outp_dp_mst_vcpi(&mstm->outp->outp, msto->head->base.index,
929 				      payload->vc_start_slot, payload->time_slots,
930 				      payload->pbn, payload->time_slots * mst_state->pbn_div);
931 	}
932 }
933 
934 static int
935 nv50_msto_atomic_check(struct drm_encoder *encoder,
936 		       struct drm_crtc_state *crtc_state,
937 		       struct drm_connector_state *conn_state)
938 {
939 	struct drm_atomic_state *state = crtc_state->state;
940 	struct drm_connector *connector = conn_state->connector;
941 	struct drm_dp_mst_topology_state *mst_state;
942 	struct nv50_mstc *mstc = nv50_mstc(connector);
943 	struct nv50_mstm *mstm = mstc->mstm;
944 	struct nv50_head_atom *asyh = nv50_head_atom(crtc_state);
945 	int slots;
946 	int ret;
947 
948 	ret = nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
949 					  mstc->native);
950 	if (ret)
951 		return ret;
952 
953 	if (!drm_atomic_crtc_needs_modeset(crtc_state))
954 		return 0;
955 
956 	/*
957 	 * When restoring duplicated states, we need to make sure that the bw
958 	 * remains the same and avoid recalculating it, as the connector's bpc
959 	 * may have changed after the state was duplicated
960 	 */
961 	if (!state->duplicated) {
962 		const int clock = crtc_state->adjusted_mode.clock;
963 
964 		asyh->or.bpc = connector->display_info.bpc;
965 		asyh->dp.pbn = drm_dp_calc_pbn_mode(clock, asyh->or.bpc * 3,
966 						    false);
967 	}
968 
969 	mst_state = drm_atomic_get_mst_topology_state(state, &mstm->mgr);
970 	if (IS_ERR(mst_state))
971 		return PTR_ERR(mst_state);
972 
973 	if (!mst_state->pbn_div) {
974 		struct nouveau_encoder *outp = mstc->mstm->outp;
975 
976 		mst_state->pbn_div = drm_dp_get_vc_payload_bw(&mstm->mgr,
977 							      outp->dp.link_bw, outp->dp.link_nr);
978 	}
979 
980 	slots = drm_dp_atomic_find_time_slots(state, &mstm->mgr, mstc->port, asyh->dp.pbn);
981 	if (slots < 0)
982 		return slots;
983 
984 	asyh->dp.tu = slots;
985 
986 	return 0;
987 }
988 
989 static u8
990 nv50_dp_bpc_to_depth(unsigned int bpc)
991 {
992 	switch (bpc) {
993 	case  6: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444;
994 	case  8: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444;
995 	case 10:
996 	default: return NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444;
997 	}
998 }
999 
1000 static void
1001 nv50_msto_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1002 {
1003 	struct nv50_msto *msto = nv50_msto(encoder);
1004 	struct nv50_head *head = msto->head;
1005 	struct nv50_head_atom *asyh =
1006 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &head->base.base));
1007 	struct nv50_mstc *mstc = NULL;
1008 	struct nv50_mstm *mstm = NULL;
1009 	struct drm_connector *connector;
1010 	struct drm_connector_list_iter conn_iter;
1011 	u8 proto;
1012 
1013 	drm_connector_list_iter_begin(encoder->dev, &conn_iter);
1014 	drm_for_each_connector_iter(connector, &conn_iter) {
1015 		if (connector->state->best_encoder == &msto->encoder) {
1016 			mstc = nv50_mstc(connector);
1017 			mstm = mstc->mstm;
1018 			break;
1019 		}
1020 	}
1021 	drm_connector_list_iter_end(&conn_iter);
1022 
1023 	if (WARN_ON(!mstc))
1024 		return;
1025 
1026 	if (!mstm->links++) {
1027 		/*XXX: MST audio. */
1028 		nvif_outp_acquire_dp(&mstm->outp->outp, mstm->outp->dp.dpcd, 0, 0, false, true);
1029 	}
1030 
1031 	if (mstm->outp->outp.or.link & 1)
1032 		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1033 	else
1034 		proto = NV917D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1035 
1036 	mstm->outp->update(mstm->outp, head->base.index, asyh, proto,
1037 			   nv50_dp_bpc_to_depth(asyh->or.bpc));
1038 
1039 	msto->mstc = mstc;
1040 	msto->enabled = true;
1041 	mstm->modified = true;
1042 }
1043 
1044 static void
1045 nv50_msto_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1046 {
1047 	struct nv50_msto *msto = nv50_msto(encoder);
1048 	struct nv50_mstc *mstc = msto->mstc;
1049 	struct nv50_mstm *mstm = mstc->mstm;
1050 
1051 	mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
1052 	mstm->modified = true;
1053 	if (!--mstm->links)
1054 		mstm->disabled = true;
1055 	msto->disabled = true;
1056 }
1057 
1058 static const struct drm_encoder_helper_funcs
1059 nv50_msto_help = {
1060 	.atomic_disable = nv50_msto_atomic_disable,
1061 	.atomic_enable = nv50_msto_atomic_enable,
1062 	.atomic_check = nv50_msto_atomic_check,
1063 };
1064 
1065 static void
1066 nv50_msto_destroy(struct drm_encoder *encoder)
1067 {
1068 	struct nv50_msto *msto = nv50_msto(encoder);
1069 	drm_encoder_cleanup(&msto->encoder);
1070 	kfree(msto);
1071 }
1072 
1073 static const struct drm_encoder_funcs
1074 nv50_msto = {
1075 	.destroy = nv50_msto_destroy,
1076 };
1077 
1078 static struct nv50_msto *
1079 nv50_msto_new(struct drm_device *dev, struct nv50_head *head, int id)
1080 {
1081 	struct nv50_msto *msto;
1082 	int ret;
1083 
1084 	msto = kzalloc(sizeof(*msto), GFP_KERNEL);
1085 	if (!msto)
1086 		return ERR_PTR(-ENOMEM);
1087 
1088 	ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
1089 			       DRM_MODE_ENCODER_DPMST, "mst-%d", id);
1090 	if (ret) {
1091 		kfree(msto);
1092 		return ERR_PTR(ret);
1093 	}
1094 
1095 	drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
1096 	msto->encoder.possible_crtcs = drm_crtc_mask(&head->base.base);
1097 	msto->head = head;
1098 	return msto;
1099 }
1100 
1101 static struct drm_encoder *
1102 nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
1103 			      struct drm_atomic_state *state)
1104 {
1105 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1106 											 connector);
1107 	struct nv50_mstc *mstc = nv50_mstc(connector);
1108 	struct drm_crtc *crtc = connector_state->crtc;
1109 
1110 	if (!(mstc->mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1111 		return NULL;
1112 
1113 	return &nv50_head(crtc)->msto->encoder;
1114 }
1115 
1116 static enum drm_mode_status
1117 nv50_mstc_mode_valid(struct drm_connector *connector,
1118 		     struct drm_display_mode *mode)
1119 {
1120 	struct nv50_mstc *mstc = nv50_mstc(connector);
1121 	struct nouveau_encoder *outp = mstc->mstm->outp;
1122 
1123 	/* TODO: calculate the PBN from the dotclock and validate against the
1124 	 * MSTB's max possible PBN
1125 	 */
1126 
1127 	return nv50_dp_mode_valid(connector, outp, mode, NULL);
1128 }
1129 
1130 static int
1131 nv50_mstc_get_modes(struct drm_connector *connector)
1132 {
1133 	struct nv50_mstc *mstc = nv50_mstc(connector);
1134 	int ret = 0;
1135 
1136 	mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
1137 	drm_connector_update_edid_property(&mstc->connector, mstc->edid);
1138 	if (mstc->edid)
1139 		ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
1140 
1141 	/*
1142 	 * XXX: Since we don't use HDR in userspace quite yet, limit the bpc
1143 	 * to 8 to save bandwidth on the topology. In the future, we'll want
1144 	 * to properly fix this by dynamically selecting the highest possible
1145 	 * bpc that would fit in the topology
1146 	 */
1147 	if (connector->display_info.bpc)
1148 		connector->display_info.bpc =
1149 			clamp(connector->display_info.bpc, 6U, 8U);
1150 	else
1151 		connector->display_info.bpc = 8;
1152 
1153 	if (mstc->native)
1154 		drm_mode_destroy(mstc->connector.dev, mstc->native);
1155 	mstc->native = nouveau_conn_native_mode(&mstc->connector);
1156 	return ret;
1157 }
1158 
1159 static int
1160 nv50_mstc_atomic_check(struct drm_connector *connector,
1161 		       struct drm_atomic_state *state)
1162 {
1163 	struct nv50_mstc *mstc = nv50_mstc(connector);
1164 	struct drm_dp_mst_topology_mgr *mgr = &mstc->mstm->mgr;
1165 
1166 	return drm_dp_atomic_release_time_slots(state, mgr, mstc->port);
1167 }
1168 
1169 static int
1170 nv50_mstc_detect(struct drm_connector *connector,
1171 		 struct drm_modeset_acquire_ctx *ctx, bool force)
1172 {
1173 	struct nv50_mstc *mstc = nv50_mstc(connector);
1174 	int ret;
1175 
1176 	if (drm_connector_is_unregistered(connector))
1177 		return connector_status_disconnected;
1178 
1179 	ret = pm_runtime_get_sync(connector->dev->dev);
1180 	if (ret < 0 && ret != -EACCES) {
1181 		pm_runtime_put_autosuspend(connector->dev->dev);
1182 		return connector_status_disconnected;
1183 	}
1184 
1185 	ret = drm_dp_mst_detect_port(connector, ctx, mstc->port->mgr,
1186 				     mstc->port);
1187 	if (ret != connector_status_connected)
1188 		goto out;
1189 
1190 out:
1191 	pm_runtime_mark_last_busy(connector->dev->dev);
1192 	pm_runtime_put_autosuspend(connector->dev->dev);
1193 	return ret;
1194 }
1195 
1196 static const struct drm_connector_helper_funcs
1197 nv50_mstc_help = {
1198 	.get_modes = nv50_mstc_get_modes,
1199 	.mode_valid = nv50_mstc_mode_valid,
1200 	.atomic_best_encoder = nv50_mstc_atomic_best_encoder,
1201 	.atomic_check = nv50_mstc_atomic_check,
1202 	.detect_ctx = nv50_mstc_detect,
1203 };
1204 
1205 static void
1206 nv50_mstc_destroy(struct drm_connector *connector)
1207 {
1208 	struct nv50_mstc *mstc = nv50_mstc(connector);
1209 
1210 	drm_connector_cleanup(&mstc->connector);
1211 	drm_dp_mst_put_port_malloc(mstc->port);
1212 
1213 	kfree(mstc);
1214 }
1215 
1216 static const struct drm_connector_funcs
1217 nv50_mstc = {
1218 	.reset = nouveau_conn_reset,
1219 	.fill_modes = drm_helper_probe_single_connector_modes,
1220 	.destroy = nv50_mstc_destroy,
1221 	.atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
1222 	.atomic_destroy_state = nouveau_conn_atomic_destroy_state,
1223 	.atomic_set_property = nouveau_conn_atomic_set_property,
1224 	.atomic_get_property = nouveau_conn_atomic_get_property,
1225 };
1226 
1227 static int
1228 nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
1229 	      const char *path, struct nv50_mstc **pmstc)
1230 {
1231 	struct drm_device *dev = mstm->outp->base.base.dev;
1232 	struct drm_crtc *crtc;
1233 	struct nv50_mstc *mstc;
1234 	int ret;
1235 
1236 	if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
1237 		return -ENOMEM;
1238 	mstc->mstm = mstm;
1239 	mstc->port = port;
1240 
1241 	ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
1242 				 DRM_MODE_CONNECTOR_DisplayPort);
1243 	if (ret) {
1244 		kfree(*pmstc);
1245 		*pmstc = NULL;
1246 		return ret;
1247 	}
1248 
1249 	drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
1250 
1251 	mstc->connector.funcs->reset(&mstc->connector);
1252 	nouveau_conn_attach_properties(&mstc->connector);
1253 
1254 	drm_for_each_crtc(crtc, dev) {
1255 		if (!(mstm->outp->dcb->heads & drm_crtc_mask(crtc)))
1256 			continue;
1257 
1258 		drm_connector_attach_encoder(&mstc->connector,
1259 					     &nv50_head(crtc)->msto->encoder);
1260 	}
1261 
1262 	drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
1263 	drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
1264 	drm_connector_set_path_property(&mstc->connector, path);
1265 	drm_dp_mst_get_port_malloc(port);
1266 	return 0;
1267 }
1268 
1269 static void
1270 nv50_mstm_cleanup(struct drm_atomic_state *state,
1271 		  struct drm_dp_mst_topology_state *mst_state,
1272 		  struct nv50_mstm *mstm)
1273 {
1274 	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1275 	struct drm_encoder *encoder;
1276 
1277 	NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
1278 	drm_dp_check_act_status(&mstm->mgr);
1279 
1280 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1281 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1282 			struct nv50_msto *msto = nv50_msto(encoder);
1283 			struct nv50_mstc *mstc = msto->mstc;
1284 			if (mstc && mstc->mstm == mstm)
1285 				nv50_msto_cleanup(state, mst_state, &mstm->mgr, msto);
1286 		}
1287 	}
1288 
1289 	mstm->modified = false;
1290 }
1291 
1292 static void
1293 nv50_mstm_prepare(struct drm_atomic_state *state,
1294 		  struct drm_dp_mst_topology_state *mst_state,
1295 		  struct nv50_mstm *mstm)
1296 {
1297 	struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
1298 	struct drm_encoder *encoder;
1299 
1300 	NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
1301 
1302 	/* Disable payloads first */
1303 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1304 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1305 			struct nv50_msto *msto = nv50_msto(encoder);
1306 			struct nv50_mstc *mstc = msto->mstc;
1307 			if (mstc && mstc->mstm == mstm && msto->disabled)
1308 				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1309 		}
1310 	}
1311 
1312 	/* Add payloads for new heads, while also updating the start slots of any unmodified (but
1313 	 * active) heads that may have had their VC slots shifted left after the previous step
1314 	 */
1315 	drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
1316 		if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1317 			struct nv50_msto *msto = nv50_msto(encoder);
1318 			struct nv50_mstc *mstc = msto->mstc;
1319 			if (mstc && mstc->mstm == mstm && !msto->disabled)
1320 				nv50_msto_prepare(state, mst_state, &mstm->mgr, msto);
1321 		}
1322 	}
1323 
1324 	if (mstm->disabled) {
1325 		if (!mstm->links)
1326 			nvif_outp_release(&mstm->outp->outp);
1327 		mstm->disabled = false;
1328 	}
1329 }
1330 
1331 static struct drm_connector *
1332 nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1333 			struct drm_dp_mst_port *port, const char *path)
1334 {
1335 	struct nv50_mstm *mstm = nv50_mstm(mgr);
1336 	struct nv50_mstc *mstc;
1337 	int ret;
1338 
1339 	ret = nv50_mstc_new(mstm, port, path, &mstc);
1340 	if (ret)
1341 		return NULL;
1342 
1343 	return &mstc->connector;
1344 }
1345 
1346 static const struct drm_dp_mst_topology_cbs
1347 nv50_mstm = {
1348 	.add_connector = nv50_mstm_add_connector,
1349 };
1350 
1351 bool
1352 nv50_mstm_service(struct nouveau_drm *drm,
1353 		  struct nouveau_connector *nv_connector,
1354 		  struct nv50_mstm *mstm)
1355 {
1356 	struct drm_dp_aux *aux = &nv_connector->aux;
1357 	bool handled = true, ret = true;
1358 	int rc;
1359 	u8 esi[8] = {};
1360 
1361 	while (handled) {
1362 		rc = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1363 		if (rc != 8) {
1364 			ret = false;
1365 			break;
1366 		}
1367 
1368 		drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1369 		if (!handled)
1370 			break;
1371 
1372 		rc = drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1],
1373 				       3);
1374 		if (rc != 3) {
1375 			ret = false;
1376 			break;
1377 		}
1378 	}
1379 
1380 	if (!ret)
1381 		NV_DEBUG(drm, "Failed to handle ESI on %s: %d\n",
1382 			 nv_connector->base.name, rc);
1383 
1384 	return ret;
1385 }
1386 
1387 void
1388 nv50_mstm_remove(struct nv50_mstm *mstm)
1389 {
1390 	mstm->is_mst = false;
1391 	drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1392 }
1393 
1394 int
1395 nv50_mstm_detect(struct nouveau_encoder *outp)
1396 {
1397 	struct nv50_mstm *mstm = outp->dp.mstm;
1398 	struct drm_dp_aux *aux;
1399 	int ret;
1400 
1401 	if (!mstm || !mstm->can_mst)
1402 		return 0;
1403 
1404 	aux = mstm->mgr.aux;
1405 
1406 	/* Clear any leftover MST state we didn't set ourselves by first
1407 	 * disabling MST if it was already enabled
1408 	 */
1409 	ret = drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
1410 	if (ret < 0)
1411 		return ret;
1412 
1413 	/* And start enabling */
1414 	ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, true);
1415 	if (ret)
1416 		return ret;
1417 
1418 	mstm->is_mst = true;
1419 	return 1;
1420 }
1421 
1422 static void
1423 nv50_mstm_fini(struct nouveau_encoder *outp)
1424 {
1425 	struct nv50_mstm *mstm = outp->dp.mstm;
1426 
1427 	if (!mstm)
1428 		return;
1429 
1430 	/* Don't change the MST state of this connector until we've finished
1431 	 * resuming, since we can't safely grab hpd_irq_lock in our resume
1432 	 * path to protect mstm->is_mst without potentially deadlocking
1433 	 */
1434 	mutex_lock(&outp->dp.hpd_irq_lock);
1435 	mstm->suspended = true;
1436 	mutex_unlock(&outp->dp.hpd_irq_lock);
1437 
1438 	if (mstm->is_mst)
1439 		drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1440 }
1441 
1442 static void
1443 nv50_mstm_init(struct nouveau_encoder *outp, bool runtime)
1444 {
1445 	struct nv50_mstm *mstm = outp->dp.mstm;
1446 	int ret = 0;
1447 
1448 	if (!mstm)
1449 		return;
1450 
1451 	if (mstm->is_mst) {
1452 		ret = drm_dp_mst_topology_mgr_resume(&mstm->mgr, !runtime);
1453 		if (ret == -1)
1454 			nv50_mstm_remove(mstm);
1455 	}
1456 
1457 	mutex_lock(&outp->dp.hpd_irq_lock);
1458 	mstm->suspended = false;
1459 	mutex_unlock(&outp->dp.hpd_irq_lock);
1460 
1461 	if (ret == -1)
1462 		drm_kms_helper_hotplug_event(mstm->mgr.dev);
1463 }
1464 
1465 static void
1466 nv50_mstm_del(struct nv50_mstm **pmstm)
1467 {
1468 	struct nv50_mstm *mstm = *pmstm;
1469 	if (mstm) {
1470 		drm_dp_mst_topology_mgr_destroy(&mstm->mgr);
1471 		kfree(*pmstm);
1472 		*pmstm = NULL;
1473 	}
1474 }
1475 
1476 static int
1477 nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1478 	      int conn_base_id, struct nv50_mstm **pmstm)
1479 {
1480 	const int max_payloads = hweight8(outp->dcb->heads);
1481 	struct drm_device *dev = outp->base.base.dev;
1482 	struct nv50_mstm *mstm;
1483 	int ret;
1484 
1485 	if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1486 		return -ENOMEM;
1487 	mstm->outp = outp;
1488 	mstm->mgr.cbs = &nv50_mstm;
1489 
1490 	ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
1491 					   max_payloads, conn_base_id);
1492 	if (ret)
1493 		return ret;
1494 
1495 	return 0;
1496 }
1497 
1498 /******************************************************************************
1499  * SOR
1500  *****************************************************************************/
1501 static void
1502 nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
1503 		struct nv50_head_atom *asyh, u8 proto, u8 depth)
1504 {
1505 	struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
1506 	struct nv50_core *core = disp->core;
1507 
1508 	if (!asyh) {
1509 		nv_encoder->ctrl &= ~BIT(head);
1510 		if (NVDEF_TEST(nv_encoder->ctrl, NV507D, SOR_SET_CONTROL, OWNER, ==, NONE))
1511 			nv_encoder->ctrl = 0;
1512 	} else {
1513 		nv_encoder->ctrl |= NVVAL(NV507D, SOR_SET_CONTROL, PROTOCOL, proto);
1514 		nv_encoder->ctrl |= BIT(head);
1515 		asyh->or.depth = depth;
1516 	}
1517 
1518 	core->func->sor->ctrl(core, nv_encoder->outp.or.id, nv_encoder->ctrl, asyh);
1519 }
1520 
1521 /* TODO: Should we extend this to PWM-only backlights?
1522  * As well, should we add a DRM helper for waiting for the backlight to acknowledge
1523  * the panel backlight has been shut off? Intel doesn't seem to do this, and uses a
1524  * fixed time delay from the vbios…
1525  */
1526 static void
1527 nv50_sor_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1528 {
1529 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1530 	struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
1531 	struct nouveau_connector *nv_connector = nv50_outp_get_old_connector(state, nv_encoder);
1532 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1533 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
1534 	struct nouveau_backlight *backlight = nv_connector->backlight;
1535 #endif
1536 	struct drm_dp_aux *aux = &nv_connector->aux;
1537 	int ret;
1538 	u8 pwr;
1539 
1540 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1541 	if (backlight && backlight->uses_dpcd) {
1542 		ret = drm_edp_backlight_disable(aux, &backlight->edp_info);
1543 		if (ret < 0)
1544 			NV_ERROR(drm, "Failed to disable backlight on [CONNECTOR:%d:%s]: %d\n",
1545 				 nv_connector->base.base.id, nv_connector->base.name, ret);
1546 	}
1547 #endif
1548 
1549 	if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
1550 		ret = drm_dp_dpcd_readb(aux, DP_SET_POWER, &pwr);
1551 
1552 		if (ret == 0) {
1553 			pwr &= ~DP_SET_POWER_MASK;
1554 			pwr |=  DP_SET_POWER_D3;
1555 			drm_dp_dpcd_writeb(aux, DP_SET_POWER, pwr);
1556 		}
1557 	}
1558 
1559 	nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
1560 	nv50_audio_disable(encoder, nv_crtc);
1561 	nvif_outp_release(&nv_encoder->outp);
1562 	nv_encoder->crtc = NULL;
1563 }
1564 
1565 static void
1566 nv50_sor_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1567 {
1568 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1569 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1570 	struct nv50_head_atom *asyh =
1571 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1572 	struct drm_display_mode *mode = &asyh->state.adjusted_mode;
1573 	struct nv50_disp *disp = nv50_disp(encoder->dev);
1574 	struct nvif_outp *outp = &nv_encoder->outp;
1575 	struct drm_device *dev = encoder->dev;
1576 	struct nouveau_drm *drm = nouveau_drm(dev);
1577 	struct nouveau_connector *nv_connector;
1578 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1579 	struct nouveau_backlight *backlight;
1580 #endif
1581 	struct nvbios *bios = &drm->vbios;
1582 	bool lvds_dual = false, lvds_8bpc = false, hda = false;
1583 	u8 proto = NV507D_SOR_SET_CONTROL_PROTOCOL_CUSTOM;
1584 	u8 depth = NV837D_SOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT;
1585 
1586 	nv_connector = nv50_outp_get_new_connector(state, nv_encoder);
1587 	nv_encoder->crtc = &nv_crtc->base;
1588 
1589 	if ((disp->disp->object.oclass == GT214_DISP ||
1590 	     disp->disp->object.oclass >= GF110_DISP) &&
1591 	    drm_detect_monitor_audio(nv_connector->edid))
1592 		hda = true;
1593 
1594 	switch (nv_encoder->dcb->type) {
1595 	case DCB_OUTPUT_TMDS:
1596 		if (disp->disp->object.oclass == NV50_DISP ||
1597 		    !drm_detect_hdmi_monitor(nv_connector->edid))
1598 			nvif_outp_acquire_tmds(outp, nv_crtc->index, false, 0, 0, 0, false);
1599 		else
1600 			nv50_hdmi_enable(encoder, nv_crtc, nv_connector, state, mode, hda);
1601 
1602 		if (nv_encoder->outp.or.link & 1) {
1603 			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_A;
1604 			/* Only enable dual-link if:
1605 			 *  - Need to (i.e. rate > 165MHz)
1606 			 *  - DCB says we can
1607 			 *  - Not an HDMI monitor, since there's no dual-link
1608 			 *    on HDMI.
1609 			 */
1610 			if (mode->clock >= 165000 &&
1611 			    nv_encoder->dcb->duallink_possible &&
1612 			    !drm_detect_hdmi_monitor(nv_connector->edid))
1613 				proto = NV507D_SOR_SET_CONTROL_PROTOCOL_DUAL_TMDS;
1614 		} else {
1615 			proto = NV507D_SOR_SET_CONTROL_PROTOCOL_SINGLE_TMDS_B;
1616 		}
1617 		break;
1618 	case DCB_OUTPUT_LVDS:
1619 		proto = NV507D_SOR_SET_CONTROL_PROTOCOL_LVDS_CUSTOM;
1620 
1621 		if (bios->fp_no_ddc) {
1622 			lvds_dual = bios->fp.dual_link;
1623 			lvds_8bpc = bios->fp.if_is_24bit;
1624 		} else {
1625 			if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
1626 				if (((u8 *)nv_connector->edid)[121] == 2)
1627 					lvds_dual = true;
1628 			} else
1629 			if (mode->clock >= bios->fp.duallink_transition_clk) {
1630 				lvds_dual = true;
1631 			}
1632 
1633 			if (lvds_dual) {
1634 				if (bios->fp.strapless_is_24bit & 2)
1635 					lvds_8bpc = true;
1636 			} else {
1637 				if (bios->fp.strapless_is_24bit & 1)
1638 					lvds_8bpc = true;
1639 			}
1640 
1641 			if (asyh->or.bpc == 8)
1642 				lvds_8bpc = true;
1643 		}
1644 
1645 		nvif_outp_acquire_lvds(&nv_encoder->outp, lvds_dual, lvds_8bpc);
1646 		break;
1647 	case DCB_OUTPUT_DP:
1648 		nvif_outp_acquire_dp(&nv_encoder->outp, nv_encoder->dp.dpcd, 0, 0, hda, false);
1649 		depth = nv50_dp_bpc_to_depth(asyh->or.bpc);
1650 
1651 		if (nv_encoder->outp.or.link & 1)
1652 			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_A;
1653 		else
1654 			proto = NV887D_SOR_SET_CONTROL_PROTOCOL_DP_B;
1655 
1656 		nv50_audio_enable(encoder, nv_crtc, nv_connector, state, mode);
1657 
1658 #ifdef CONFIG_DRM_NOUVEAU_BACKLIGHT
1659 		backlight = nv_connector->backlight;
1660 		if (backlight && backlight->uses_dpcd)
1661 			drm_edp_backlight_enable(&nv_connector->aux, &backlight->edp_info,
1662 						 (u16)backlight->dev->props.brightness);
1663 #endif
1664 
1665 		break;
1666 	default:
1667 		BUG();
1668 		break;
1669 	}
1670 
1671 	nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
1672 }
1673 
1674 static const struct drm_encoder_helper_funcs
1675 nv50_sor_help = {
1676 	.atomic_check = nv50_outp_atomic_check,
1677 	.atomic_enable = nv50_sor_atomic_enable,
1678 	.atomic_disable = nv50_sor_atomic_disable,
1679 };
1680 
1681 static void
1682 nv50_sor_destroy(struct drm_encoder *encoder)
1683 {
1684 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1685 
1686 	nvif_outp_dtor(&nv_encoder->outp);
1687 
1688 	nv50_mstm_del(&nv_encoder->dp.mstm);
1689 	drm_encoder_cleanup(encoder);
1690 
1691 	if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
1692 		mutex_destroy(&nv_encoder->dp.hpd_irq_lock);
1693 
1694 	kfree(encoder);
1695 }
1696 
1697 static const struct drm_encoder_funcs
1698 nv50_sor_func = {
1699 	.destroy = nv50_sor_destroy,
1700 };
1701 
1702 bool nv50_has_mst(struct nouveau_drm *drm)
1703 {
1704 	struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
1705 	u32 data;
1706 	u8 ver, hdr, cnt, len;
1707 
1708 	data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len);
1709 	return data && ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04);
1710 }
1711 
1712 static int
1713 nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
1714 {
1715 	struct nouveau_connector *nv_connector = nouveau_connector(connector);
1716 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
1717 	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1718 	struct nouveau_encoder *nv_encoder;
1719 	struct drm_encoder *encoder;
1720 	struct nv50_disp *disp = nv50_disp(connector->dev);
1721 	int type, ret;
1722 
1723 	switch (dcbe->type) {
1724 	case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1725 	case DCB_OUTPUT_TMDS:
1726 	case DCB_OUTPUT_DP:
1727 	default:
1728 		type = DRM_MODE_ENCODER_TMDS;
1729 		break;
1730 	}
1731 
1732 	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1733 	if (!nv_encoder)
1734 		return -ENOMEM;
1735 	nv_encoder->dcb = dcbe;
1736 	nv_encoder->update = nv50_sor_update;
1737 
1738 	encoder = to_drm_encoder(nv_encoder);
1739 	encoder->possible_crtcs = dcbe->heads;
1740 	encoder->possible_clones = 0;
1741 	drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1742 			 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
1743 	drm_encoder_helper_add(encoder, &nv50_sor_help);
1744 
1745 	drm_connector_attach_encoder(connector, encoder);
1746 
1747 	disp->core->func->sor->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1748 	nv50_outp_dump_caps(drm, nv_encoder);
1749 
1750 	if (dcbe->type == DCB_OUTPUT_DP) {
1751 		struct nvkm_i2c_aux *aux =
1752 			nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1753 
1754 		mutex_init(&nv_encoder->dp.hpd_irq_lock);
1755 
1756 		if (aux) {
1757 			if (disp->disp->object.oclass < GF110_DISP) {
1758 				/* HW has no support for address-only
1759 				 * transactions, so we're required to
1760 				 * use custom I2C-over-AUX code.
1761 				 */
1762 				nv_encoder->i2c = &aux->i2c;
1763 			} else {
1764 				nv_encoder->i2c = &nv_connector->aux.ddc;
1765 			}
1766 			nv_encoder->aux = aux;
1767 		}
1768 
1769 		if (nv_connector->type != DCB_CONNECTOR_eDP &&
1770 		    nv50_has_mst(drm)) {
1771 			ret = nv50_mstm_new(nv_encoder, &nv_connector->aux,
1772 					    16, nv_connector->base.base.id,
1773 					    &nv_encoder->dp.mstm);
1774 			if (ret)
1775 				return ret;
1776 		}
1777 	} else {
1778 		struct nvkm_i2c_bus *bus =
1779 			nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1780 		if (bus)
1781 			nv_encoder->i2c = &bus->i2c;
1782 	}
1783 
1784 	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
1785 }
1786 
1787 /******************************************************************************
1788  * PIOR
1789  *****************************************************************************/
1790 static int
1791 nv50_pior_atomic_check(struct drm_encoder *encoder,
1792 		       struct drm_crtc_state *crtc_state,
1793 		       struct drm_connector_state *conn_state)
1794 {
1795 	int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1796 	if (ret)
1797 		return ret;
1798 	crtc_state->adjusted_mode.clock *= 2;
1799 	return 0;
1800 }
1801 
1802 static void
1803 nv50_pior_atomic_disable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1804 {
1805 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1806 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1807 	const u32 ctrl = NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, NONE);
1808 
1809 	core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, NULL);
1810 	nv_encoder->crtc = NULL;
1811 	nvif_outp_release(&nv_encoder->outp);
1812 }
1813 
1814 static void
1815 nv50_pior_atomic_enable(struct drm_encoder *encoder, struct drm_atomic_state *state)
1816 {
1817 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1818 	struct nouveau_crtc *nv_crtc = nv50_outp_get_new_crtc(state, nv_encoder);
1819 	struct nv50_head_atom *asyh =
1820 		nv50_head_atom(drm_atomic_get_new_crtc_state(state, &nv_crtc->base));
1821 	struct nv50_core *core = nv50_disp(encoder->dev)->core;
1822 	u32 ctrl = 0;
1823 
1824 	switch (nv_crtc->index) {
1825 	case 0: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD0); break;
1826 	case 1: ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, OWNER, HEAD1); break;
1827 	default:
1828 		WARN_ON(1);
1829 		break;
1830 	}
1831 
1832 	switch (asyh->or.bpc) {
1833 	case 10: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_30_444; break;
1834 	case  8: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_24_444; break;
1835 	case  6: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_BPP_18_444; break;
1836 	default: asyh->or.depth = NV837D_PIOR_SET_CONTROL_PIXEL_DEPTH_DEFAULT; break;
1837 	}
1838 
1839 	switch (nv_encoder->dcb->type) {
1840 	case DCB_OUTPUT_TMDS:
1841 		ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1842 		nvif_outp_acquire_tmds(&nv_encoder->outp, false, false, 0, 0, 0, false);
1843 		break;
1844 	case DCB_OUTPUT_DP:
1845 		ctrl |= NVDEF(NV507D, PIOR_SET_CONTROL, PROTOCOL, EXT_TMDS_ENC);
1846 		nvif_outp_acquire_dp(&nv_encoder->outp, nv_encoder->dp.dpcd, 0, 0, false, false);
1847 		break;
1848 	default:
1849 		BUG();
1850 		break;
1851 	}
1852 
1853 	core->func->pior->ctrl(core, nv_encoder->outp.or.id, ctrl, asyh);
1854 	nv_encoder->crtc = &nv_crtc->base;
1855 }
1856 
1857 static const struct drm_encoder_helper_funcs
1858 nv50_pior_help = {
1859 	.atomic_check = nv50_pior_atomic_check,
1860 	.atomic_enable = nv50_pior_atomic_enable,
1861 	.atomic_disable = nv50_pior_atomic_disable,
1862 };
1863 
1864 static void
1865 nv50_pior_destroy(struct drm_encoder *encoder)
1866 {
1867 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1868 
1869 	nvif_outp_dtor(&nv_encoder->outp);
1870 
1871 	drm_encoder_cleanup(encoder);
1872 	kfree(encoder);
1873 }
1874 
1875 static const struct drm_encoder_funcs
1876 nv50_pior_func = {
1877 	.destroy = nv50_pior_destroy,
1878 };
1879 
1880 static int
1881 nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1882 {
1883 	struct drm_device *dev = connector->dev;
1884 	struct nouveau_drm *drm = nouveau_drm(dev);
1885 	struct nv50_disp *disp = nv50_disp(dev);
1886 	struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
1887 	struct nvkm_i2c_bus *bus = NULL;
1888 	struct nvkm_i2c_aux *aux = NULL;
1889 	struct i2c_adapter *ddc;
1890 	struct nouveau_encoder *nv_encoder;
1891 	struct drm_encoder *encoder;
1892 	int type;
1893 
1894 	switch (dcbe->type) {
1895 	case DCB_OUTPUT_TMDS:
1896 		bus  = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1897 		ddc  = bus ? &bus->i2c : NULL;
1898 		type = DRM_MODE_ENCODER_TMDS;
1899 		break;
1900 	case DCB_OUTPUT_DP:
1901 		aux  = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
1902 		ddc  = aux ? &aux->i2c : NULL;
1903 		type = DRM_MODE_ENCODER_TMDS;
1904 		break;
1905 	default:
1906 		return -ENODEV;
1907 	}
1908 
1909 	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1910 	if (!nv_encoder)
1911 		return -ENOMEM;
1912 	nv_encoder->dcb = dcbe;
1913 	nv_encoder->i2c = ddc;
1914 	nv_encoder->aux = aux;
1915 
1916 	encoder = to_drm_encoder(nv_encoder);
1917 	encoder->possible_crtcs = dcbe->heads;
1918 	encoder->possible_clones = 0;
1919 	drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1920 			 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
1921 	drm_encoder_helper_add(encoder, &nv50_pior_help);
1922 
1923 	drm_connector_attach_encoder(connector, encoder);
1924 
1925 	disp->core->func->pior->get_caps(disp, nv_encoder, ffs(dcbe->or) - 1);
1926 	nv50_outp_dump_caps(drm, nv_encoder);
1927 
1928 	return nvif_outp_ctor(disp->disp, nv_encoder->base.base.name, dcbe->id, &nv_encoder->outp);
1929 }
1930 
1931 /******************************************************************************
1932  * Atomic
1933  *****************************************************************************/
1934 
1935 static void
1936 nv50_disp_atomic_commit_core(struct drm_atomic_state *state, u32 *interlock)
1937 {
1938 	struct drm_dp_mst_topology_mgr *mgr;
1939 	struct drm_dp_mst_topology_state *mst_state;
1940 	struct nouveau_drm *drm = nouveau_drm(state->dev);
1941 	struct nv50_disp *disp = nv50_disp(drm->dev);
1942 	struct nv50_core *core = disp->core;
1943 	struct nv50_mstm *mstm;
1944 	int i;
1945 
1946 	NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
1947 
1948 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
1949 		mstm = nv50_mstm(mgr);
1950 		if (mstm->modified)
1951 			nv50_mstm_prepare(state, mst_state, mstm);
1952 	}
1953 
1954 	core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1955 	core->func->update(core, interlock, true);
1956 	if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1957 				       disp->core->chan.base.device))
1958 		NV_ERROR(drm, "core notifier timeout\n");
1959 
1960 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
1961 		mstm = nv50_mstm(mgr);
1962 		if (mstm->modified)
1963 			nv50_mstm_cleanup(state, mst_state, mstm);
1964 	}
1965 }
1966 
1967 static void
1968 nv50_disp_atomic_commit_wndw(struct drm_atomic_state *state, u32 *interlock)
1969 {
1970 	struct drm_plane_state *new_plane_state;
1971 	struct drm_plane *plane;
1972 	int i;
1973 
1974 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1975 		struct nv50_wndw *wndw = nv50_wndw(plane);
1976 		if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1977 			if (wndw->func->update)
1978 				wndw->func->update(wndw, interlock);
1979 		}
1980 	}
1981 }
1982 
1983 static void
1984 nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1985 {
1986 	struct drm_device *dev = state->dev;
1987 	struct drm_crtc_state *new_crtc_state, *old_crtc_state;
1988 	struct drm_crtc *crtc;
1989 	struct drm_plane_state *new_plane_state;
1990 	struct drm_plane *plane;
1991 	struct nouveau_drm *drm = nouveau_drm(dev);
1992 	struct nv50_disp *disp = nv50_disp(dev);
1993 	struct nv50_atom *atom = nv50_atom(state);
1994 	struct nv50_core *core = disp->core;
1995 	struct nv50_outp_atom *outp, *outt;
1996 	u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
1997 	int i;
1998 	bool flushed = false;
1999 
2000 	NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
2001 	nv50_crc_atomic_stop_reporting(state);
2002 	drm_atomic_helper_wait_for_fences(dev, state, false);
2003 	drm_atomic_helper_wait_for_dependencies(state);
2004 	drm_dp_mst_atomic_wait_for_dependencies(state);
2005 	drm_atomic_helper_update_legacy_modeset_state(dev, state);
2006 	drm_atomic_helper_calc_timestamping_constants(state);
2007 
2008 	if (atom->lock_core)
2009 		mutex_lock(&disp->mutex);
2010 
2011 	/* Disable head(s). */
2012 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2013 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2014 		struct nv50_head *head = nv50_head(crtc);
2015 
2016 		NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
2017 			  asyh->clr.mask, asyh->set.mask);
2018 
2019 		if (old_crtc_state->active && !new_crtc_state->active) {
2020 			pm_runtime_put_noidle(dev->dev);
2021 			drm_crtc_vblank_off(crtc);
2022 		}
2023 
2024 		if (asyh->clr.mask) {
2025 			nv50_head_flush_clr(head, asyh, atom->flush_disable);
2026 			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2027 		}
2028 	}
2029 
2030 	/* Disable plane(s). */
2031 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2032 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2033 		struct nv50_wndw *wndw = nv50_wndw(plane);
2034 
2035 		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
2036 			  asyw->clr.mask, asyw->set.mask);
2037 		if (!asyw->clr.mask)
2038 			continue;
2039 
2040 		nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
2041 	}
2042 
2043 	/* Disable output path(s). */
2044 	list_for_each_entry(outp, &atom->outp, head) {
2045 		const struct drm_encoder_helper_funcs *help;
2046 		struct drm_encoder *encoder;
2047 
2048 		encoder = outp->encoder;
2049 		help = encoder->helper_private;
2050 
2051 		NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
2052 			  outp->clr.mask, outp->set.mask);
2053 
2054 		if (outp->clr.mask) {
2055 			help->atomic_disable(encoder, state);
2056 			interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
2057 			if (outp->flush_disable) {
2058 				nv50_disp_atomic_commit_wndw(state, interlock);
2059 				nv50_disp_atomic_commit_core(state, interlock);
2060 				memset(interlock, 0x00, sizeof(interlock));
2061 
2062 				flushed = true;
2063 			}
2064 		}
2065 	}
2066 
2067 	/* Flush disable. */
2068 	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2069 		if (atom->flush_disable) {
2070 			nv50_disp_atomic_commit_wndw(state, interlock);
2071 			nv50_disp_atomic_commit_core(state, interlock);
2072 			memset(interlock, 0x00, sizeof(interlock));
2073 
2074 			flushed = true;
2075 		}
2076 	}
2077 
2078 	if (flushed)
2079 		nv50_crc_atomic_release_notifier_contexts(state);
2080 	nv50_crc_atomic_init_notifier_contexts(state);
2081 
2082 	/* Update output path(s). */
2083 	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2084 		const struct drm_encoder_helper_funcs *help;
2085 		struct drm_encoder *encoder;
2086 
2087 		encoder = outp->encoder;
2088 		help = encoder->helper_private;
2089 
2090 		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
2091 			  outp->set.mask, outp->clr.mask);
2092 
2093 		if (outp->set.mask) {
2094 			help->atomic_enable(encoder, state);
2095 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2096 		}
2097 
2098 		list_del(&outp->head);
2099 		kfree(outp);
2100 	}
2101 
2102 	/* Update head(s). */
2103 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2104 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2105 		struct nv50_head *head = nv50_head(crtc);
2106 
2107 		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2108 			  asyh->set.mask, asyh->clr.mask);
2109 
2110 		if (asyh->set.mask) {
2111 			nv50_head_flush_set(head, asyh);
2112 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2113 		}
2114 
2115 		if (new_crtc_state->active) {
2116 			if (!old_crtc_state->active) {
2117 				drm_crtc_vblank_on(crtc);
2118 				pm_runtime_get_noresume(dev->dev);
2119 			}
2120 			if (new_crtc_state->event)
2121 				drm_crtc_vblank_get(crtc);
2122 		}
2123 	}
2124 
2125 	/* Update window->head assignment.
2126 	 *
2127 	 * This has to happen in an update that's not interlocked with
2128 	 * any window channels to avoid hitting HW error checks.
2129 	 *
2130 	 *TODO: Proper handling of window ownership (Turing apparently
2131 	 *      supports non-fixed mappings).
2132 	 */
2133 	if (core->assign_windows) {
2134 		core->func->wndw.owner(core);
2135 		nv50_disp_atomic_commit_core(state, interlock);
2136 		core->assign_windows = false;
2137 		interlock[NV50_DISP_INTERLOCK_CORE] = 0;
2138 	}
2139 
2140 	/* Finish updating head(s)...
2141 	 *
2142 	 * NVD is rather picky about both where window assignments can change,
2143 	 * *and* about certain core and window channel states matching.
2144 	 *
2145 	 * The EFI GOP driver on newer GPUs configures window channels with a
2146 	 * different output format to what we do, and the core channel update
2147 	 * in the assign_windows case above would result in a state mismatch.
2148 	 *
2149 	 * Delay some of the head update until after that point to workaround
2150 	 * the issue.  This only affects the initial modeset.
2151 	 *
2152 	 * TODO: handle this better when adding flexible window mapping
2153 	 */
2154 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2155 		struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
2156 		struct nv50_head *head = nv50_head(crtc);
2157 
2158 		NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
2159 			  asyh->set.mask, asyh->clr.mask);
2160 
2161 		if (asyh->set.mask) {
2162 			nv50_head_flush_set_wndw(head, asyh);
2163 			interlock[NV50_DISP_INTERLOCK_CORE] = 1;
2164 		}
2165 	}
2166 
2167 	/* Update plane(s). */
2168 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2169 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2170 		struct nv50_wndw *wndw = nv50_wndw(plane);
2171 
2172 		NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
2173 			  asyw->set.mask, asyw->clr.mask);
2174 		if ( !asyw->set.mask &&
2175 		    (!asyw->clr.mask || atom->flush_disable))
2176 			continue;
2177 
2178 		nv50_wndw_flush_set(wndw, interlock, asyw);
2179 	}
2180 
2181 	/* Flush update. */
2182 	nv50_disp_atomic_commit_wndw(state, interlock);
2183 
2184 	if (interlock[NV50_DISP_INTERLOCK_CORE]) {
2185 		if (interlock[NV50_DISP_INTERLOCK_BASE] ||
2186 		    interlock[NV50_DISP_INTERLOCK_OVLY] ||
2187 		    interlock[NV50_DISP_INTERLOCK_WNDW] ||
2188 		    !atom->state.legacy_cursor_update)
2189 			nv50_disp_atomic_commit_core(state, interlock);
2190 		else
2191 			disp->core->func->update(disp->core, interlock, false);
2192 	}
2193 
2194 	if (atom->lock_core)
2195 		mutex_unlock(&disp->mutex);
2196 
2197 	/* Wait for HW to signal completion. */
2198 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2199 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2200 		struct nv50_wndw *wndw = nv50_wndw(plane);
2201 		int ret = nv50_wndw_wait_armed(wndw, asyw);
2202 		if (ret)
2203 			NV_ERROR(drm, "%s: timeout\n", plane->name);
2204 	}
2205 
2206 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2207 		if (new_crtc_state->event) {
2208 			unsigned long flags;
2209 			/* Get correct count/ts if racing with vblank irq */
2210 			if (new_crtc_state->active)
2211 				drm_crtc_accurate_vblank_count(crtc);
2212 			spin_lock_irqsave(&crtc->dev->event_lock, flags);
2213 			drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
2214 			spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
2215 
2216 			new_crtc_state->event = NULL;
2217 			if (new_crtc_state->active)
2218 				drm_crtc_vblank_put(crtc);
2219 		}
2220 	}
2221 
2222 	nv50_crc_atomic_start_reporting(state);
2223 	if (!flushed)
2224 		nv50_crc_atomic_release_notifier_contexts(state);
2225 
2226 	drm_atomic_helper_commit_hw_done(state);
2227 	drm_atomic_helper_cleanup_planes(dev, state);
2228 	drm_atomic_helper_commit_cleanup_done(state);
2229 	drm_atomic_state_put(state);
2230 
2231 	/* Drop the RPM ref we got from nv50_disp_atomic_commit() */
2232 	pm_runtime_mark_last_busy(dev->dev);
2233 	pm_runtime_put_autosuspend(dev->dev);
2234 }
2235 
2236 static void
2237 nv50_disp_atomic_commit_work(struct work_struct *work)
2238 {
2239 	struct drm_atomic_state *state =
2240 		container_of(work, typeof(*state), commit_work);
2241 	nv50_disp_atomic_commit_tail(state);
2242 }
2243 
2244 static int
2245 nv50_disp_atomic_commit(struct drm_device *dev,
2246 			struct drm_atomic_state *state, bool nonblock)
2247 {
2248 	struct drm_plane_state *new_plane_state;
2249 	struct drm_plane *plane;
2250 	int ret, i;
2251 
2252 	ret = pm_runtime_get_sync(dev->dev);
2253 	if (ret < 0 && ret != -EACCES) {
2254 		pm_runtime_put_autosuspend(dev->dev);
2255 		return ret;
2256 	}
2257 
2258 	ret = drm_atomic_helper_setup_commit(state, nonblock);
2259 	if (ret)
2260 		goto done;
2261 
2262 	INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
2263 
2264 	ret = drm_atomic_helper_prepare_planes(dev, state);
2265 	if (ret)
2266 		goto done;
2267 
2268 	if (!nonblock) {
2269 		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
2270 		if (ret)
2271 			goto err_cleanup;
2272 	}
2273 
2274 	ret = drm_atomic_helper_swap_state(state, true);
2275 	if (ret)
2276 		goto err_cleanup;
2277 
2278 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
2279 		struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
2280 		struct nv50_wndw *wndw = nv50_wndw(plane);
2281 
2282 		if (asyw->set.image)
2283 			nv50_wndw_ntfy_enable(wndw, asyw);
2284 	}
2285 
2286 	drm_atomic_state_get(state);
2287 
2288 	/*
2289 	 * Grab another RPM ref for the commit tail, which will release the
2290 	 * ref when it's finished
2291 	 */
2292 	pm_runtime_get_noresume(dev->dev);
2293 
2294 	if (nonblock)
2295 		queue_work(system_unbound_wq, &state->commit_work);
2296 	else
2297 		nv50_disp_atomic_commit_tail(state);
2298 
2299 err_cleanup:
2300 	if (ret)
2301 		drm_atomic_helper_cleanup_planes(dev, state);
2302 done:
2303 	pm_runtime_put_autosuspend(dev->dev);
2304 	return ret;
2305 }
2306 
2307 static struct nv50_outp_atom *
2308 nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
2309 {
2310 	struct nv50_outp_atom *outp;
2311 
2312 	list_for_each_entry(outp, &atom->outp, head) {
2313 		if (outp->encoder == encoder)
2314 			return outp;
2315 	}
2316 
2317 	outp = kzalloc(sizeof(*outp), GFP_KERNEL);
2318 	if (!outp)
2319 		return ERR_PTR(-ENOMEM);
2320 
2321 	list_add(&outp->head, &atom->outp);
2322 	outp->encoder = encoder;
2323 	return outp;
2324 }
2325 
2326 static int
2327 nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
2328 				struct drm_connector_state *old_connector_state)
2329 {
2330 	struct drm_encoder *encoder = old_connector_state->best_encoder;
2331 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2332 	struct drm_crtc *crtc;
2333 	struct nv50_outp_atom *outp;
2334 
2335 	if (!(crtc = old_connector_state->crtc))
2336 		return 0;
2337 
2338 	old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
2339 	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2340 	if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2341 		outp = nv50_disp_outp_atomic_add(atom, encoder);
2342 		if (IS_ERR(outp))
2343 			return PTR_ERR(outp);
2344 
2345 		if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
2346 			outp->flush_disable = true;
2347 			atom->flush_disable = true;
2348 		}
2349 		outp->clr.ctrl = true;
2350 		atom->lock_core = true;
2351 	}
2352 
2353 	return 0;
2354 }
2355 
2356 static int
2357 nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
2358 				struct drm_connector_state *connector_state)
2359 {
2360 	struct drm_encoder *encoder = connector_state->best_encoder;
2361 	struct drm_crtc_state *new_crtc_state;
2362 	struct drm_crtc *crtc;
2363 	struct nv50_outp_atom *outp;
2364 
2365 	if (!(crtc = connector_state->crtc))
2366 		return 0;
2367 
2368 	new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
2369 	if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
2370 		outp = nv50_disp_outp_atomic_add(atom, encoder);
2371 		if (IS_ERR(outp))
2372 			return PTR_ERR(outp);
2373 
2374 		outp->set.ctrl = true;
2375 		atom->lock_core = true;
2376 	}
2377 
2378 	return 0;
2379 }
2380 
2381 static int
2382 nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
2383 {
2384 	struct nv50_atom *atom = nv50_atom(state);
2385 	struct nv50_core *core = nv50_disp(dev)->core;
2386 	struct drm_connector_state *old_connector_state, *new_connector_state;
2387 	struct drm_connector *connector;
2388 	struct drm_crtc_state *new_crtc_state;
2389 	struct drm_crtc *crtc;
2390 	struct nv50_head *head;
2391 	struct nv50_head_atom *asyh;
2392 	int ret, i;
2393 
2394 	if (core->assign_windows && core->func->head->static_wndw_map) {
2395 		drm_for_each_crtc(crtc, dev) {
2396 			new_crtc_state = drm_atomic_get_crtc_state(state,
2397 								   crtc);
2398 			if (IS_ERR(new_crtc_state))
2399 				return PTR_ERR(new_crtc_state);
2400 
2401 			head = nv50_head(crtc);
2402 			asyh = nv50_head_atom(new_crtc_state);
2403 			core->func->head->static_wndw_map(head, asyh);
2404 		}
2405 	}
2406 
2407 	/* We need to handle colour management on a per-plane basis. */
2408 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2409 		if (new_crtc_state->color_mgmt_changed) {
2410 			ret = drm_atomic_add_affected_planes(state, crtc);
2411 			if (ret)
2412 				return ret;
2413 		}
2414 	}
2415 
2416 	ret = drm_atomic_helper_check(dev, state);
2417 	if (ret)
2418 		return ret;
2419 
2420 	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
2421 		ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
2422 		if (ret)
2423 			return ret;
2424 
2425 		ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
2426 		if (ret)
2427 			return ret;
2428 	}
2429 
2430 	ret = drm_dp_mst_atomic_check(state);
2431 	if (ret)
2432 		return ret;
2433 
2434 	nv50_crc_atomic_check_outp(atom);
2435 
2436 	return 0;
2437 }
2438 
2439 static void
2440 nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2441 {
2442 	struct nv50_atom *atom = nv50_atom(state);
2443 	struct nv50_outp_atom *outp, *outt;
2444 
2445 	list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2446 		list_del(&outp->head);
2447 		kfree(outp);
2448 	}
2449 
2450 	drm_atomic_state_default_clear(state);
2451 }
2452 
2453 static void
2454 nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2455 {
2456 	struct nv50_atom *atom = nv50_atom(state);
2457 	drm_atomic_state_default_release(&atom->state);
2458 	kfree(atom);
2459 }
2460 
2461 static struct drm_atomic_state *
2462 nv50_disp_atomic_state_alloc(struct drm_device *dev)
2463 {
2464 	struct nv50_atom *atom;
2465 	if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2466 	    drm_atomic_state_init(dev, &atom->state) < 0) {
2467 		kfree(atom);
2468 		return NULL;
2469 	}
2470 	INIT_LIST_HEAD(&atom->outp);
2471 	return &atom->state;
2472 }
2473 
2474 static const struct drm_mode_config_funcs
2475 nv50_disp_func = {
2476 	.fb_create = nouveau_user_framebuffer_create,
2477 	.output_poll_changed = drm_fb_helper_output_poll_changed,
2478 	.atomic_check = nv50_disp_atomic_check,
2479 	.atomic_commit = nv50_disp_atomic_commit,
2480 	.atomic_state_alloc = nv50_disp_atomic_state_alloc,
2481 	.atomic_state_clear = nv50_disp_atomic_state_clear,
2482 	.atomic_state_free = nv50_disp_atomic_state_free,
2483 };
2484 
2485 static const struct drm_mode_config_helper_funcs
2486 nv50_disp_helper_func = {
2487 	.atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
2488 };
2489 
2490 /******************************************************************************
2491  * Init
2492  *****************************************************************************/
2493 
2494 static void
2495 nv50_display_fini(struct drm_device *dev, bool runtime, bool suspend)
2496 {
2497 	struct nouveau_drm *drm = nouveau_drm(dev);
2498 	struct drm_encoder *encoder;
2499 
2500 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2501 		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST)
2502 			nv50_mstm_fini(nouveau_encoder(encoder));
2503 	}
2504 
2505 	if (!runtime)
2506 		cancel_work_sync(&drm->hpd_work);
2507 }
2508 
2509 static int
2510 nv50_display_init(struct drm_device *dev, bool resume, bool runtime)
2511 {
2512 	struct nv50_core *core = nv50_disp(dev)->core;
2513 	struct drm_encoder *encoder;
2514 
2515 	if (resume || runtime)
2516 		core->func->init(core);
2517 
2518 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2519 		if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2520 			struct nouveau_encoder *nv_encoder =
2521 				nouveau_encoder(encoder);
2522 			nv50_mstm_init(nv_encoder, runtime);
2523 		}
2524 	}
2525 
2526 	return 0;
2527 }
2528 
2529 static void
2530 nv50_display_destroy(struct drm_device *dev)
2531 {
2532 	struct nv50_disp *disp = nv50_disp(dev);
2533 
2534 	nv50_audio_component_fini(nouveau_drm(dev));
2535 
2536 	nvif_object_unmap(&disp->caps);
2537 	nvif_object_dtor(&disp->caps);
2538 	nv50_core_del(&disp->core);
2539 
2540 	nouveau_bo_unmap(disp->sync);
2541 	if (disp->sync)
2542 		nouveau_bo_unpin(disp->sync);
2543 	nouveau_bo_ref(NULL, &disp->sync);
2544 
2545 	nouveau_display(dev)->priv = NULL;
2546 	kfree(disp);
2547 }
2548 
2549 int
2550 nv50_display_create(struct drm_device *dev)
2551 {
2552 	struct nvif_device *device = &nouveau_drm(dev)->client.device;
2553 	struct nouveau_drm *drm = nouveau_drm(dev);
2554 	struct dcb_table *dcb = &drm->vbios.dcb;
2555 	struct drm_connector *connector, *tmp;
2556 	struct nv50_disp *disp;
2557 	struct dcb_output *dcbe;
2558 	int crtcs, ret, i;
2559 	bool has_mst = nv50_has_mst(drm);
2560 
2561 	disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2562 	if (!disp)
2563 		return -ENOMEM;
2564 
2565 	mutex_init(&disp->mutex);
2566 
2567 	nouveau_display(dev)->priv = disp;
2568 	nouveau_display(dev)->dtor = nv50_display_destroy;
2569 	nouveau_display(dev)->init = nv50_display_init;
2570 	nouveau_display(dev)->fini = nv50_display_fini;
2571 	disp->disp = &nouveau_display(dev)->disp;
2572 	dev->mode_config.funcs = &nv50_disp_func;
2573 	dev->mode_config.helper_private = &nv50_disp_helper_func;
2574 	dev->mode_config.quirk_addfb_prefer_xbgr_30bpp = true;
2575 	dev->mode_config.normalize_zpos = true;
2576 
2577 	/* small shared memory area we use for notifiers and semaphores */
2578 	ret = nouveau_bo_new(&drm->client, 4096, 0x1000,
2579 			     NOUVEAU_GEM_DOMAIN_VRAM,
2580 			     0, 0x0000, NULL, NULL, &disp->sync);
2581 	if (!ret) {
2582 		ret = nouveau_bo_pin(disp->sync, NOUVEAU_GEM_DOMAIN_VRAM, true);
2583 		if (!ret) {
2584 			ret = nouveau_bo_map(disp->sync);
2585 			if (ret)
2586 				nouveau_bo_unpin(disp->sync);
2587 		}
2588 		if (ret)
2589 			nouveau_bo_ref(NULL, &disp->sync);
2590 	}
2591 
2592 	if (ret)
2593 		goto out;
2594 
2595 	/* allocate master evo channel */
2596 	ret = nv50_core_new(drm, &disp->core);
2597 	if (ret)
2598 		goto out;
2599 
2600 	disp->core->func->init(disp->core);
2601 	if (disp->core->func->caps_init) {
2602 		ret = disp->core->func->caps_init(drm, disp);
2603 		if (ret)
2604 			goto out;
2605 	}
2606 
2607 	/* Assign the correct format modifiers */
2608 	if (disp->disp->object.oclass >= TU102_DISP)
2609 		nouveau_display(dev)->format_modifiers = wndwc57e_modifiers;
2610 	else
2611 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_FERMI)
2612 		nouveau_display(dev)->format_modifiers = disp90xx_modifiers;
2613 	else
2614 		nouveau_display(dev)->format_modifiers = disp50xx_modifiers;
2615 
2616 	/* FIXME: 256x256 cursors are supported on Kepler, however unlike Maxwell and later
2617 	 * generations Kepler requires that we use small pages (4K) for cursor scanout surfaces. The
2618 	 * proper fix for this is to teach nouveau to migrate fbs being used for the cursor plane to
2619 	 * small page allocations in prepare_fb(). When this is implemented, we should also force
2620 	 * large pages (128K) for ovly fbs in order to fix Kepler ovlys.
2621 	 * But until then, just limit cursors to 128x128 - which is small enough to avoid ever using
2622 	 * large pages.
2623 	 */
2624 	if (disp->disp->object.oclass >= GM107_DISP) {
2625 		dev->mode_config.cursor_width = 256;
2626 		dev->mode_config.cursor_height = 256;
2627 	} else if (disp->disp->object.oclass >= GK104_DISP) {
2628 		dev->mode_config.cursor_width = 128;
2629 		dev->mode_config.cursor_height = 128;
2630 	} else {
2631 		dev->mode_config.cursor_width = 64;
2632 		dev->mode_config.cursor_height = 64;
2633 	}
2634 
2635 	/* create crtc objects to represent the hw heads */
2636 	if (disp->disp->object.oclass >= GV100_DISP)
2637 		crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2638 	else
2639 	if (disp->disp->object.oclass >= GF110_DISP)
2640 		crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
2641 	else
2642 		crtcs = 0x3;
2643 
2644 	for (i = 0; i < fls(crtcs); i++) {
2645 		struct nv50_head *head;
2646 
2647 		if (!(crtcs & (1 << i)))
2648 			continue;
2649 
2650 		head = nv50_head_create(dev, i);
2651 		if (IS_ERR(head)) {
2652 			ret = PTR_ERR(head);
2653 			goto out;
2654 		}
2655 
2656 		if (has_mst) {
2657 			head->msto = nv50_msto_new(dev, head, i);
2658 			if (IS_ERR(head->msto)) {
2659 				ret = PTR_ERR(head->msto);
2660 				head->msto = NULL;
2661 				goto out;
2662 			}
2663 
2664 			/*
2665 			 * FIXME: This is a hack to workaround the following
2666 			 * issues:
2667 			 *
2668 			 * https://gitlab.gnome.org/GNOME/mutter/issues/759
2669 			 * https://gitlab.freedesktop.org/xorg/xserver/merge_requests/277
2670 			 *
2671 			 * Once these issues are closed, this should be
2672 			 * removed
2673 			 */
2674 			head->msto->encoder.possible_crtcs = crtcs;
2675 		}
2676 	}
2677 
2678 	/* create encoder/connector objects based on VBIOS DCB table */
2679 	for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2680 		connector = nouveau_connector_create(dev, dcbe);
2681 		if (IS_ERR(connector))
2682 			continue;
2683 
2684 		if (dcbe->location == DCB_LOC_ON_CHIP) {
2685 			switch (dcbe->type) {
2686 			case DCB_OUTPUT_TMDS:
2687 			case DCB_OUTPUT_LVDS:
2688 			case DCB_OUTPUT_DP:
2689 				ret = nv50_sor_create(connector, dcbe);
2690 				break;
2691 			case DCB_OUTPUT_ANALOG:
2692 				ret = nv50_dac_create(connector, dcbe);
2693 				break;
2694 			default:
2695 				ret = -ENODEV;
2696 				break;
2697 			}
2698 		} else {
2699 			ret = nv50_pior_create(connector, dcbe);
2700 		}
2701 
2702 		if (ret) {
2703 			NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2704 				     dcbe->location, dcbe->type,
2705 				     ffs(dcbe->or) - 1, ret);
2706 			ret = 0;
2707 		}
2708 	}
2709 
2710 	/* cull any connectors we created that don't have an encoder */
2711 	list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2712 		if (connector->possible_encoders)
2713 			continue;
2714 
2715 		NV_WARN(drm, "%s has no encoders, removing\n",
2716 			connector->name);
2717 		connector->funcs->destroy(connector);
2718 	}
2719 
2720 	/* Disable vblank irqs aggressively for power-saving, safe on nv50+ */
2721 	dev->vblank_disable_immediate = true;
2722 
2723 	nv50_audio_component_init(drm);
2724 
2725 out:
2726 	if (ret)
2727 		nv50_display_destroy(dev);
2728 	return ret;
2729 }
2730 
2731 /******************************************************************************
2732  * Format modifiers
2733  *****************************************************************************/
2734 
2735 /****************************************************************
2736  *            Log2(block height) ----------------------------+  *
2737  *            Page Kind ----------------------------------+  |  *
2738  *            Gob Height/Page Kind Generation ------+     |  |  *
2739  *                          Sector layout -------+  |     |  |  *
2740  *                          Compression ------+  |  |     |  |  */
2741 const u64 disp50xx_modifiers[] = { /*         |  |  |     |  |  */
2742 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 0),
2743 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 1),
2744 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 2),
2745 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 3),
2746 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 4),
2747 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x7a, 5),
2748 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 0),
2749 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 1),
2750 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 2),
2751 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 3),
2752 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 4),
2753 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x78, 5),
2754 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 0),
2755 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 1),
2756 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 2),
2757 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 3),
2758 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 4),
2759 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 1, 0x70, 5),
2760 	DRM_FORMAT_MOD_LINEAR,
2761 	DRM_FORMAT_MOD_INVALID
2762 };
2763 
2764 /****************************************************************
2765  *            Log2(block height) ----------------------------+  *
2766  *            Page Kind ----------------------------------+  |  *
2767  *            Gob Height/Page Kind Generation ------+     |  |  *
2768  *                          Sector layout -------+  |     |  |  *
2769  *                          Compression ------+  |  |     |  |  */
2770 const u64 disp90xx_modifiers[] = { /*         |  |  |     |  |  */
2771 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 0),
2772 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 1),
2773 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 2),
2774 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 3),
2775 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 4),
2776 	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 0, 0xfe, 5),
2777 	DRM_FORMAT_MOD_LINEAR,
2778 	DRM_FORMAT_MOD_INVALID
2779 };
2780