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