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