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