1 /*
2  * Copyright © 2008 Intel Corporation
3  *             2014 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  */
25 
26 #include <drm/drm_atomic.h>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_edid.h>
29 #include <drm/drm_probe_helper.h>
30 
31 #include "i915_drv.h"
32 #include "i915_reg.h"
33 #include "intel_atomic.h"
34 #include "intel_audio.h"
35 #include "intel_connector.h"
36 #include "intel_crtc.h"
37 #include "intel_ddi.h"
38 #include "intel_de.h"
39 #include "intel_display_types.h"
40 #include "intel_dp.h"
41 #include "intel_dp_hdcp.h"
42 #include "intel_dp_mst.h"
43 #include "intel_dpio_phy.h"
44 #include "intel_hdcp.h"
45 #include "intel_hotplug.h"
46 #include "skl_scaler.h"
47 
48 static int intel_dp_mst_check_constraints(struct drm_i915_private *i915, int bpp,
49 					  const struct drm_display_mode *adjusted_mode,
50 					  struct intel_crtc_state *crtc_state,
51 					  bool dsc)
52 {
53 	if (intel_dp_is_uhbr(crtc_state) && DISPLAY_VER(i915) <= 13 && dsc) {
54 		int output_bpp = bpp;
55 		/* DisplayPort 2 128b/132b, bits per lane is always 32 */
56 		int symbol_clock = crtc_state->port_clock / 32;
57 
58 		if (output_bpp * adjusted_mode->crtc_clock >=
59 		    symbol_clock * 72) {
60 			drm_dbg_kms(&i915->drm, "UHBR check failed(required bw %d available %d)\n",
61 				    output_bpp * adjusted_mode->crtc_clock, symbol_clock * 72);
62 			return -EINVAL;
63 		}
64 	}
65 
66 	return 0;
67 }
68 
69 static int intel_dp_mst_find_vcpi_slots_for_bpp(struct intel_encoder *encoder,
70 						struct intel_crtc_state *crtc_state,
71 						int max_bpp,
72 						int min_bpp,
73 						struct link_config_limits *limits,
74 						struct drm_connector_state *conn_state,
75 						int step,
76 						bool dsc)
77 {
78 	struct drm_atomic_state *state = crtc_state->uapi.state;
79 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
80 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
81 	struct drm_dp_mst_topology_state *mst_state;
82 	struct intel_connector *connector =
83 		to_intel_connector(conn_state->connector);
84 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
85 	const struct drm_display_mode *adjusted_mode =
86 		&crtc_state->hw.adjusted_mode;
87 	int bpp, slots = -EINVAL;
88 	int ret = 0;
89 
90 	mst_state = drm_atomic_get_mst_topology_state(state, &intel_dp->mst_mgr);
91 	if (IS_ERR(mst_state))
92 		return PTR_ERR(mst_state);
93 
94 	crtc_state->lane_count = limits->max_lane_count;
95 	crtc_state->port_clock = limits->max_rate;
96 
97 	// TODO: Handle pbn_div changes by adding a new MST helper
98 	if (!mst_state->pbn_div) {
99 		mst_state->pbn_div = drm_dp_get_vc_payload_bw(&intel_dp->mst_mgr,
100 							      crtc_state->port_clock,
101 							      crtc_state->lane_count);
102 	}
103 
104 	for (bpp = max_bpp; bpp >= min_bpp; bpp -= step) {
105 		drm_dbg_kms(&i915->drm, "Trying bpp %d\n", bpp);
106 
107 		ret = intel_dp_mst_check_constraints(i915, bpp, adjusted_mode, crtc_state, dsc);
108 		if (ret)
109 			continue;
110 
111 		crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
112 						       dsc ? bpp << 4 : bpp,
113 						       dsc);
114 
115 		slots = drm_dp_atomic_find_time_slots(state, &intel_dp->mst_mgr,
116 						      connector->port,
117 						      crtc_state->pbn);
118 		if (slots == -EDEADLK)
119 			return slots;
120 
121 		if (slots >= 0) {
122 			ret = drm_dp_mst_atomic_check(state);
123 			/*
124 			 * If we got slots >= 0 and we can fit those based on check
125 			 * then we can exit the loop. Otherwise keep trying.
126 			 */
127 			if (!ret)
128 				break;
129 		}
130 	}
131 
132 	/* We failed to find a proper bpp/timeslots, return error */
133 	if (ret)
134 		slots = ret;
135 
136 	if (slots < 0) {
137 		drm_dbg_kms(&i915->drm, "failed finding vcpi slots:%d\n",
138 			    slots);
139 	} else {
140 		if (!dsc)
141 			crtc_state->pipe_bpp = bpp;
142 		else
143 			crtc_state->dsc.compressed_bpp = bpp;
144 		drm_dbg_kms(&i915->drm, "Got %d slots for pipe bpp %d dsc %d\n", slots, bpp, dsc);
145 	}
146 
147 	return slots;
148 }
149 
150 static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
151 					    struct intel_crtc_state *crtc_state,
152 					    struct drm_connector_state *conn_state,
153 					    struct link_config_limits *limits)
154 {
155 	const struct drm_display_mode *adjusted_mode =
156 		&crtc_state->hw.adjusted_mode;
157 	int slots = -EINVAL;
158 
159 	slots = intel_dp_mst_find_vcpi_slots_for_bpp(encoder, crtc_state, limits->max_bpp,
160 						     limits->min_bpp, limits,
161 						     conn_state, 2 * 3, false);
162 
163 	if (slots < 0)
164 		return slots;
165 
166 	intel_link_compute_m_n(crtc_state->pipe_bpp,
167 			       crtc_state->lane_count,
168 			       adjusted_mode->crtc_clock,
169 			       crtc_state->port_clock,
170 			       &crtc_state->dp_m_n,
171 			       crtc_state->fec_enable);
172 	crtc_state->dp_m_n.tu = slots;
173 
174 	return 0;
175 }
176 
177 static int intel_dp_dsc_mst_compute_link_config(struct intel_encoder *encoder,
178 						struct intel_crtc_state *crtc_state,
179 						struct drm_connector_state *conn_state,
180 						struct link_config_limits *limits)
181 {
182 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
183 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
184 	struct intel_connector *connector =
185 		to_intel_connector(conn_state->connector);
186 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
187 	const struct drm_display_mode *adjusted_mode =
188 		&crtc_state->hw.adjusted_mode;
189 	int slots = -EINVAL;
190 	int i, num_bpc;
191 	u8 dsc_bpc[3] = {0};
192 	int min_bpp, max_bpp, sink_min_bpp, sink_max_bpp;
193 	u8 dsc_max_bpc;
194 	bool need_timeslot_recalc = false;
195 	u32 last_compressed_bpp;
196 
197 	/* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
198 	if (DISPLAY_VER(i915) >= 12)
199 		dsc_max_bpc = min_t(u8, 12, conn_state->max_requested_bpc);
200 	else
201 		dsc_max_bpc = min_t(u8, 10, conn_state->max_requested_bpc);
202 
203 	max_bpp = min_t(u8, dsc_max_bpc * 3, limits->max_bpp);
204 	min_bpp = limits->min_bpp;
205 
206 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(intel_dp->dsc_dpcd,
207 						       dsc_bpc);
208 
209 	drm_dbg_kms(&i915->drm, "DSC Source supported min bpp %d max bpp %d\n",
210 		    min_bpp, max_bpp);
211 
212 	sink_max_bpp = dsc_bpc[0] * 3;
213 	sink_min_bpp = sink_max_bpp;
214 
215 	for (i = 1; i < num_bpc; i++) {
216 		if (sink_min_bpp > dsc_bpc[i] * 3)
217 			sink_min_bpp = dsc_bpc[i] * 3;
218 		if (sink_max_bpp < dsc_bpc[i] * 3)
219 			sink_max_bpp = dsc_bpc[i] * 3;
220 	}
221 
222 	drm_dbg_kms(&i915->drm, "DSC Sink supported min bpp %d max bpp %d\n",
223 		    sink_min_bpp, sink_max_bpp);
224 
225 	if (min_bpp < sink_min_bpp)
226 		min_bpp = sink_min_bpp;
227 
228 	if (max_bpp > sink_max_bpp)
229 		max_bpp = sink_max_bpp;
230 
231 	slots = intel_dp_mst_find_vcpi_slots_for_bpp(encoder, crtc_state, max_bpp,
232 						     min_bpp, limits,
233 						     conn_state, 2 * 3, true);
234 
235 	if (slots < 0)
236 		return slots;
237 
238 	last_compressed_bpp = crtc_state->dsc.compressed_bpp;
239 
240 	crtc_state->dsc.compressed_bpp = intel_dp_dsc_nearest_valid_bpp(i915,
241 									last_compressed_bpp,
242 									crtc_state->pipe_bpp);
243 
244 	if (crtc_state->dsc.compressed_bpp != last_compressed_bpp)
245 		need_timeslot_recalc = true;
246 
247 	/*
248 	 * Apparently some MST hubs dislike if vcpi slots are not matching precisely
249 	 * the actual compressed bpp we use.
250 	 */
251 	if (need_timeslot_recalc) {
252 		slots = intel_dp_mst_find_vcpi_slots_for_bpp(encoder, crtc_state,
253 							     crtc_state->dsc.compressed_bpp,
254 							     crtc_state->dsc.compressed_bpp,
255 							     limits, conn_state, 2 * 3, true);
256 		if (slots < 0)
257 			return slots;
258 	}
259 
260 	intel_link_compute_m_n(crtc_state->dsc.compressed_bpp,
261 			       crtc_state->lane_count,
262 			       adjusted_mode->crtc_clock,
263 			       crtc_state->port_clock,
264 			       &crtc_state->dp_m_n,
265 			       crtc_state->fec_enable);
266 	crtc_state->dp_m_n.tu = slots;
267 
268 	return 0;
269 }
270 static int intel_dp_mst_update_slots(struct intel_encoder *encoder,
271 				     struct intel_crtc_state *crtc_state,
272 				     struct drm_connector_state *conn_state)
273 {
274 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
275 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
276 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
277 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
278 	struct drm_dp_mst_topology_state *topology_state;
279 	u8 link_coding_cap = intel_dp_is_uhbr(crtc_state) ?
280 		DP_CAP_ANSI_128B132B : DP_CAP_ANSI_8B10B;
281 
282 	topology_state = drm_atomic_get_mst_topology_state(conn_state->state, mgr);
283 	if (IS_ERR(topology_state)) {
284 		drm_dbg_kms(&i915->drm, "slot update failed\n");
285 		return PTR_ERR(topology_state);
286 	}
287 
288 	drm_dp_mst_update_slots(topology_state, link_coding_cap);
289 
290 	return 0;
291 }
292 
293 static bool intel_dp_mst_has_audio(const struct drm_connector_state *conn_state)
294 {
295 	const struct intel_digital_connector_state *intel_conn_state =
296 		to_intel_digital_connector_state(conn_state);
297 	struct intel_connector *connector =
298 		to_intel_connector(conn_state->connector);
299 
300 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
301 		return connector->base.display_info.has_audio;
302 	else
303 		return intel_conn_state->force_audio == HDMI_AUDIO_ON;
304 }
305 
306 static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
307 				       struct intel_crtc_state *pipe_config,
308 				       struct drm_connector_state *conn_state)
309 {
310 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
311 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
312 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
313 	const struct drm_display_mode *adjusted_mode =
314 		&pipe_config->hw.adjusted_mode;
315 	struct link_config_limits limits;
316 	int ret;
317 
318 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
319 		return -EINVAL;
320 
321 	pipe_config->sink_format = INTEL_OUTPUT_FORMAT_RGB;
322 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
323 	pipe_config->has_pch_encoder = false;
324 
325 	pipe_config->has_audio =
326 		intel_dp_mst_has_audio(conn_state) &&
327 		intel_audio_compute_config(encoder, pipe_config, conn_state);
328 
329 	/*
330 	 * for MST we always configure max link bw - the spec doesn't
331 	 * seem to suggest we should do otherwise.
332 	 */
333 	limits.min_rate =
334 	limits.max_rate = intel_dp_max_link_rate(intel_dp);
335 
336 	limits.min_lane_count =
337 	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
338 
339 	limits.min_bpp = intel_dp_min_bpp(pipe_config->output_format);
340 	/*
341 	 * FIXME: If all the streams can't fit into the link with
342 	 * their current pipe_bpp we should reduce pipe_bpp across
343 	 * the board until things start to fit. Until then we
344 	 * limit to <= 8bpc since that's what was hardcoded for all
345 	 * MST streams previously. This hack should be removed once
346 	 * we have the proper retry logic in place.
347 	 */
348 	limits.max_bpp = min(pipe_config->pipe_bpp, 24);
349 
350 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
351 
352 	ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
353 					       conn_state, &limits);
354 
355 	if (ret == -EDEADLK)
356 		return ret;
357 
358 	/* enable compression if the mode doesn't fit available BW */
359 	drm_dbg_kms(&dev_priv->drm, "Force DSC en = %d\n", intel_dp->force_dsc_en);
360 	if (ret || intel_dp->force_dsc_en) {
361 		/*
362 		 * Try to get at least some timeslots and then see, if
363 		 * we can fit there with DSC.
364 		 */
365 		drm_dbg_kms(&dev_priv->drm, "Trying to find VCPI slots in DSC mode\n");
366 
367 		ret = intel_dp_dsc_mst_compute_link_config(encoder, pipe_config,
368 							   conn_state, &limits);
369 		if (ret < 0)
370 			return ret;
371 
372 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
373 						  conn_state, &limits,
374 						  pipe_config->dp_m_n.tu, false);
375 	}
376 
377 	if (ret)
378 		return ret;
379 
380 	ret = intel_dp_mst_update_slots(encoder, pipe_config, conn_state);
381 	if (ret)
382 		return ret;
383 
384 	pipe_config->limited_color_range =
385 		intel_dp_limited_color_range(pipe_config, conn_state);
386 
387 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
388 		pipe_config->lane_lat_optim_mask =
389 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
390 
391 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
392 
393 	return 0;
394 }
395 
396 /*
397  * Iterate over all connectors and return a mask of
398  * all CPU transcoders streaming over the same DP link.
399  */
400 static unsigned int
401 intel_dp_mst_transcoder_mask(struct intel_atomic_state *state,
402 			     struct intel_dp *mst_port)
403 {
404 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
405 	const struct intel_digital_connector_state *conn_state;
406 	struct intel_connector *connector;
407 	u8 transcoders = 0;
408 	int i;
409 
410 	if (DISPLAY_VER(dev_priv) < 12)
411 		return 0;
412 
413 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
414 		const struct intel_crtc_state *crtc_state;
415 		struct intel_crtc *crtc;
416 
417 		if (connector->mst_port != mst_port || !conn_state->base.crtc)
418 			continue;
419 
420 		crtc = to_intel_crtc(conn_state->base.crtc);
421 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
422 
423 		if (!crtc_state->hw.active)
424 			continue;
425 
426 		transcoders |= BIT(crtc_state->cpu_transcoder);
427 	}
428 
429 	return transcoders;
430 }
431 
432 static int intel_dp_mst_compute_config_late(struct intel_encoder *encoder,
433 					    struct intel_crtc_state *crtc_state,
434 					    struct drm_connector_state *conn_state)
435 {
436 	struct intel_atomic_state *state = to_intel_atomic_state(conn_state->state);
437 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
438 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
439 
440 	/* lowest numbered transcoder will be designated master */
441 	crtc_state->mst_master_transcoder =
442 		ffs(intel_dp_mst_transcoder_mask(state, intel_dp)) - 1;
443 
444 	return 0;
445 }
446 
447 /*
448  * If one of the connectors in a MST stream needs a modeset, mark all CRTCs
449  * that shares the same MST stream as mode changed,
450  * intel_modeset_pipe_config()+intel_crtc_check_fastset() will take care to do
451  * a fastset when possible.
452  */
453 static int
454 intel_dp_mst_atomic_master_trans_check(struct intel_connector *connector,
455 				       struct intel_atomic_state *state)
456 {
457 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
458 	struct drm_connector_list_iter connector_list_iter;
459 	struct intel_connector *connector_iter;
460 	int ret = 0;
461 
462 	if (DISPLAY_VER(dev_priv) < 12)
463 		return  0;
464 
465 	if (!intel_connector_needs_modeset(state, &connector->base))
466 		return 0;
467 
468 	drm_connector_list_iter_begin(&dev_priv->drm, &connector_list_iter);
469 	for_each_intel_connector_iter(connector_iter, &connector_list_iter) {
470 		struct intel_digital_connector_state *conn_iter_state;
471 		struct intel_crtc_state *crtc_state;
472 		struct intel_crtc *crtc;
473 
474 		if (connector_iter->mst_port != connector->mst_port ||
475 		    connector_iter == connector)
476 			continue;
477 
478 		conn_iter_state = intel_atomic_get_digital_connector_state(state,
479 									   connector_iter);
480 		if (IS_ERR(conn_iter_state)) {
481 			ret = PTR_ERR(conn_iter_state);
482 			break;
483 		}
484 
485 		if (!conn_iter_state->base.crtc)
486 			continue;
487 
488 		crtc = to_intel_crtc(conn_iter_state->base.crtc);
489 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
490 		if (IS_ERR(crtc_state)) {
491 			ret = PTR_ERR(crtc_state);
492 			break;
493 		}
494 
495 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
496 		if (ret)
497 			break;
498 		crtc_state->uapi.mode_changed = true;
499 	}
500 	drm_connector_list_iter_end(&connector_list_iter);
501 
502 	return ret;
503 }
504 
505 static int
506 intel_dp_mst_atomic_check(struct drm_connector *connector,
507 			  struct drm_atomic_state *_state)
508 {
509 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
510 	struct intel_connector *intel_connector =
511 		to_intel_connector(connector);
512 	int ret;
513 
514 	ret = intel_digital_connector_atomic_check(connector, &state->base);
515 	if (ret)
516 		return ret;
517 
518 	ret = intel_dp_mst_atomic_master_trans_check(intel_connector, state);
519 	if (ret)
520 		return ret;
521 
522 	return drm_dp_atomic_release_time_slots(&state->base,
523 						&intel_connector->mst_port->mst_mgr,
524 						intel_connector->port);
525 }
526 
527 static void clear_act_sent(struct intel_encoder *encoder,
528 			   const struct intel_crtc_state *crtc_state)
529 {
530 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
531 
532 	intel_de_write(i915, dp_tp_status_reg(encoder, crtc_state),
533 		       DP_TP_STATUS_ACT_SENT);
534 }
535 
536 static void wait_for_act_sent(struct intel_encoder *encoder,
537 			      const struct intel_crtc_state *crtc_state)
538 {
539 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
540 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
541 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
542 
543 	if (intel_de_wait_for_set(i915, dp_tp_status_reg(encoder, crtc_state),
544 				  DP_TP_STATUS_ACT_SENT, 1))
545 		drm_err(&i915->drm, "Timed out waiting for ACT sent\n");
546 
547 	drm_dp_check_act_status(&intel_dp->mst_mgr);
548 }
549 
550 static void intel_mst_disable_dp(struct intel_atomic_state *state,
551 				 struct intel_encoder *encoder,
552 				 const struct intel_crtc_state *old_crtc_state,
553 				 const struct drm_connector_state *old_conn_state)
554 {
555 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
556 	struct intel_digital_port *dig_port = intel_mst->primary;
557 	struct intel_dp *intel_dp = &dig_port->dp;
558 	struct intel_connector *connector =
559 		to_intel_connector(old_conn_state->connector);
560 	struct drm_dp_mst_topology_state *old_mst_state =
561 		drm_atomic_get_old_mst_topology_state(&state->base, &intel_dp->mst_mgr);
562 	struct drm_dp_mst_topology_state *new_mst_state =
563 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
564 	const struct drm_dp_mst_atomic_payload *old_payload =
565 		drm_atomic_get_mst_payload_state(old_mst_state, connector->port);
566 	struct drm_dp_mst_atomic_payload *new_payload =
567 		drm_atomic_get_mst_payload_state(new_mst_state, connector->port);
568 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
569 
570 	drm_dbg_kms(&i915->drm, "active links %d\n",
571 		    intel_dp->active_mst_links);
572 
573 	intel_hdcp_disable(intel_mst->connector);
574 
575 	drm_dp_remove_payload(&intel_dp->mst_mgr, new_mst_state,
576 			      old_payload, new_payload);
577 
578 	intel_audio_codec_disable(encoder, old_crtc_state, old_conn_state);
579 }
580 
581 static void intel_mst_post_disable_dp(struct intel_atomic_state *state,
582 				      struct intel_encoder *encoder,
583 				      const struct intel_crtc_state *old_crtc_state,
584 				      const struct drm_connector_state *old_conn_state)
585 {
586 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
587 	struct intel_digital_port *dig_port = intel_mst->primary;
588 	struct intel_dp *intel_dp = &dig_port->dp;
589 	struct intel_connector *connector =
590 		to_intel_connector(old_conn_state->connector);
591 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
592 	bool last_mst_stream;
593 
594 	intel_dp->active_mst_links--;
595 	last_mst_stream = intel_dp->active_mst_links == 0;
596 	drm_WARN_ON(&dev_priv->drm,
597 		    DISPLAY_VER(dev_priv) >= 12 && last_mst_stream &&
598 		    !intel_dp_mst_is_master_trans(old_crtc_state));
599 
600 	intel_crtc_vblank_off(old_crtc_state);
601 
602 	intel_disable_transcoder(old_crtc_state);
603 
604 	clear_act_sent(encoder, old_crtc_state);
605 
606 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(old_crtc_state->cpu_transcoder),
607 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC, 0);
608 
609 	wait_for_act_sent(encoder, old_crtc_state);
610 
611 	intel_ddi_disable_transcoder_func(old_crtc_state);
612 
613 	if (DISPLAY_VER(dev_priv) >= 9)
614 		skl_scaler_disable(old_crtc_state);
615 	else
616 		ilk_pfit_disable(old_crtc_state);
617 
618 	/*
619 	 * Power down mst path before disabling the port, otherwise we end
620 	 * up getting interrupts from the sink upon detecting link loss.
621 	 */
622 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
623 				     false);
624 
625 	/*
626 	 * BSpec 4287: disable DIP after the transcoder is disabled and before
627 	 * the transcoder clock select is set to none.
628 	 */
629 	if (last_mst_stream)
630 		intel_dp_set_infoframes(&dig_port->base, false,
631 					old_crtc_state, NULL);
632 	/*
633 	 * From TGL spec: "If multi-stream slave transcoder: Configure
634 	 * Transcoder Clock Select to direct no clock to the transcoder"
635 	 *
636 	 * From older GENs spec: "Configure Transcoder Clock Select to direct
637 	 * no clock to the transcoder"
638 	 */
639 	if (DISPLAY_VER(dev_priv) < 12 || !last_mst_stream)
640 		intel_ddi_disable_transcoder_clock(old_crtc_state);
641 
642 
643 	intel_mst->connector = NULL;
644 	if (last_mst_stream)
645 		dig_port->base.post_disable(state, &dig_port->base,
646 						  old_crtc_state, NULL);
647 
648 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
649 		    intel_dp->active_mst_links);
650 }
651 
652 static void intel_mst_post_pll_disable_dp(struct intel_atomic_state *state,
653 					  struct intel_encoder *encoder,
654 					  const struct intel_crtc_state *old_crtc_state,
655 					  const struct drm_connector_state *old_conn_state)
656 {
657 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
658 	struct intel_digital_port *dig_port = intel_mst->primary;
659 	struct intel_dp *intel_dp = &dig_port->dp;
660 
661 	if (intel_dp->active_mst_links == 0 &&
662 	    dig_port->base.post_pll_disable)
663 		dig_port->base.post_pll_disable(state, encoder, old_crtc_state, old_conn_state);
664 }
665 
666 static void intel_mst_pre_pll_enable_dp(struct intel_atomic_state *state,
667 					struct intel_encoder *encoder,
668 					const struct intel_crtc_state *pipe_config,
669 					const struct drm_connector_state *conn_state)
670 {
671 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
672 	struct intel_digital_port *dig_port = intel_mst->primary;
673 	struct intel_dp *intel_dp = &dig_port->dp;
674 
675 	if (intel_dp->active_mst_links == 0)
676 		dig_port->base.pre_pll_enable(state, &dig_port->base,
677 						    pipe_config, NULL);
678 	else
679 		/*
680 		 * The port PLL state needs to get updated for secondary
681 		 * streams as for the primary stream.
682 		 */
683 		intel_ddi_update_active_dpll(state, &dig_port->base,
684 					     to_intel_crtc(pipe_config->uapi.crtc));
685 }
686 
687 static void intel_mst_pre_enable_dp(struct intel_atomic_state *state,
688 				    struct intel_encoder *encoder,
689 				    const struct intel_crtc_state *pipe_config,
690 				    const struct drm_connector_state *conn_state)
691 {
692 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
693 	struct intel_digital_port *dig_port = intel_mst->primary;
694 	struct intel_dp *intel_dp = &dig_port->dp;
695 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
696 	struct intel_connector *connector =
697 		to_intel_connector(conn_state->connector);
698 	struct drm_dp_mst_topology_state *mst_state =
699 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
700 	int ret;
701 	bool first_mst_stream;
702 
703 	/* MST encoders are bound to a crtc, not to a connector,
704 	 * force the mapping here for get_hw_state.
705 	 */
706 	connector->encoder = encoder;
707 	intel_mst->connector = connector;
708 	first_mst_stream = intel_dp->active_mst_links == 0;
709 	drm_WARN_ON(&dev_priv->drm,
710 		    DISPLAY_VER(dev_priv) >= 12 && first_mst_stream &&
711 		    !intel_dp_mst_is_master_trans(pipe_config));
712 
713 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
714 		    intel_dp->active_mst_links);
715 
716 	if (first_mst_stream)
717 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
718 
719 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
720 
721 	if (first_mst_stream)
722 		dig_port->base.pre_enable(state, &dig_port->base,
723 						pipe_config, NULL);
724 
725 	intel_dp->active_mst_links++;
726 
727 	ret = drm_dp_add_payload_part1(&intel_dp->mst_mgr, mst_state,
728 				       drm_atomic_get_mst_payload_state(mst_state, connector->port));
729 	if (ret < 0)
730 		drm_err(&dev_priv->drm, "Failed to create MST payload for %s: %d\n",
731 			connector->base.name, ret);
732 
733 	/*
734 	 * Before Gen 12 this is not done as part of
735 	 * dig_port->base.pre_enable() and should be done here. For
736 	 * Gen 12+ the step in which this should be done is different for the
737 	 * first MST stream, so it's done on the DDI for the first stream and
738 	 * here for the following ones.
739 	 */
740 	if (DISPLAY_VER(dev_priv) < 12 || !first_mst_stream)
741 		intel_ddi_enable_transcoder_clock(encoder, pipe_config);
742 
743 	intel_ddi_set_dp_msa(pipe_config, conn_state);
744 }
745 
746 static void intel_mst_enable_dp(struct intel_atomic_state *state,
747 				struct intel_encoder *encoder,
748 				const struct intel_crtc_state *pipe_config,
749 				const struct drm_connector_state *conn_state)
750 {
751 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
752 	struct intel_digital_port *dig_port = intel_mst->primary;
753 	struct intel_dp *intel_dp = &dig_port->dp;
754 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
755 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
756 	struct drm_dp_mst_topology_state *mst_state =
757 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
758 	enum transcoder trans = pipe_config->cpu_transcoder;
759 
760 	drm_WARN_ON(&dev_priv->drm, pipe_config->has_pch_encoder);
761 
762 	clear_act_sent(encoder, pipe_config);
763 
764 	if (intel_dp_is_uhbr(pipe_config)) {
765 		const struct drm_display_mode *adjusted_mode =
766 			&pipe_config->hw.adjusted_mode;
767 		u64 crtc_clock_hz = KHz(adjusted_mode->crtc_clock);
768 
769 		intel_de_write(dev_priv, TRANS_DP2_VFREQHIGH(pipe_config->cpu_transcoder),
770 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz >> 24));
771 		intel_de_write(dev_priv, TRANS_DP2_VFREQLOW(pipe_config->cpu_transcoder),
772 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz & 0xffffff));
773 	}
774 
775 	intel_ddi_enable_transcoder_func(encoder, pipe_config);
776 
777 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(trans), 0,
778 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
779 
780 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
781 		    intel_dp->active_mst_links);
782 
783 	wait_for_act_sent(encoder, pipe_config);
784 
785 	drm_dp_add_payload_part2(&intel_dp->mst_mgr, &state->base,
786 				 drm_atomic_get_mst_payload_state(mst_state, connector->port));
787 
788 	if (DISPLAY_VER(dev_priv) >= 14 && pipe_config->fec_enable)
789 		intel_de_rmw(dev_priv, MTL_CHICKEN_TRANS(trans), 0,
790 			     FECSTALL_DIS_DPTSTREAM_DPTTG);
791 	else if (DISPLAY_VER(dev_priv) >= 12 && pipe_config->fec_enable)
792 		intel_de_rmw(dev_priv, CHICKEN_TRANS(trans), 0,
793 			     FECSTALL_DIS_DPTSTREAM_DPTTG);
794 
795 	intel_enable_transcoder(pipe_config);
796 
797 	intel_crtc_vblank_on(pipe_config);
798 
799 	intel_audio_codec_enable(encoder, pipe_config, conn_state);
800 
801 	/* Enable hdcp if it's desired */
802 	if (conn_state->content_protection ==
803 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
804 		intel_hdcp_enable(state, encoder, pipe_config, conn_state);
805 }
806 
807 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
808 				      enum pipe *pipe)
809 {
810 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
811 	*pipe = intel_mst->pipe;
812 	if (intel_mst->connector)
813 		return true;
814 	return false;
815 }
816 
817 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
818 					struct intel_crtc_state *pipe_config)
819 {
820 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
821 	struct intel_digital_port *dig_port = intel_mst->primary;
822 
823 	dig_port->base.get_config(&dig_port->base, pipe_config);
824 }
825 
826 static bool intel_dp_mst_initial_fastset_check(struct intel_encoder *encoder,
827 					       struct intel_crtc_state *crtc_state)
828 {
829 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
830 	struct intel_digital_port *dig_port = intel_mst->primary;
831 
832 	return intel_dp_initial_fastset_check(&dig_port->base, crtc_state);
833 }
834 
835 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
836 {
837 	struct intel_connector *intel_connector = to_intel_connector(connector);
838 	struct intel_dp *intel_dp = intel_connector->mst_port;
839 	const struct drm_edid *drm_edid;
840 	int ret;
841 
842 	if (drm_connector_is_unregistered(connector))
843 		return intel_connector_update_modes(connector, NULL);
844 
845 	drm_edid = drm_dp_mst_edid_read(connector, &intel_dp->mst_mgr, intel_connector->port);
846 
847 	ret = intel_connector_update_modes(connector, drm_edid);
848 
849 	drm_edid_free(drm_edid);
850 
851 	return ret;
852 }
853 
854 static int
855 intel_dp_mst_connector_late_register(struct drm_connector *connector)
856 {
857 	struct intel_connector *intel_connector = to_intel_connector(connector);
858 	int ret;
859 
860 	ret = drm_dp_mst_connector_late_register(connector,
861 						 intel_connector->port);
862 	if (ret < 0)
863 		return ret;
864 
865 	ret = intel_connector_register(connector);
866 	if (ret < 0)
867 		drm_dp_mst_connector_early_unregister(connector,
868 						      intel_connector->port);
869 
870 	return ret;
871 }
872 
873 static void
874 intel_dp_mst_connector_early_unregister(struct drm_connector *connector)
875 {
876 	struct intel_connector *intel_connector = to_intel_connector(connector);
877 
878 	intel_connector_unregister(connector);
879 	drm_dp_mst_connector_early_unregister(connector,
880 					      intel_connector->port);
881 }
882 
883 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
884 	.fill_modes = drm_helper_probe_single_connector_modes,
885 	.atomic_get_property = intel_digital_connector_atomic_get_property,
886 	.atomic_set_property = intel_digital_connector_atomic_set_property,
887 	.late_register = intel_dp_mst_connector_late_register,
888 	.early_unregister = intel_dp_mst_connector_early_unregister,
889 	.destroy = intel_connector_destroy,
890 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
891 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
892 };
893 
894 static int intel_dp_mst_get_modes(struct drm_connector *connector)
895 {
896 	return intel_dp_mst_get_ddc_modes(connector);
897 }
898 
899 static int
900 intel_dp_mst_mode_valid_ctx(struct drm_connector *connector,
901 			    struct drm_display_mode *mode,
902 			    struct drm_modeset_acquire_ctx *ctx,
903 			    enum drm_mode_status *status)
904 {
905 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
906 	struct intel_connector *intel_connector = to_intel_connector(connector);
907 	struct intel_dp *intel_dp = intel_connector->mst_port;
908 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
909 	struct drm_dp_mst_port *port = intel_connector->port;
910 	const int min_bpp = 18;
911 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
912 	int max_rate, mode_rate, max_lanes, max_link_clock;
913 	int ret;
914 	bool dsc = false, bigjoiner = false;
915 	u16 dsc_max_output_bpp = 0;
916 	u8 dsc_slice_count = 0;
917 	int target_clock = mode->clock;
918 
919 	if (drm_connector_is_unregistered(connector)) {
920 		*status = MODE_ERROR;
921 		return 0;
922 	}
923 
924 	*status = intel_cpu_transcoder_mode_valid(dev_priv, mode);
925 	if (*status != MODE_OK)
926 		return 0;
927 
928 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
929 		*status = MODE_NO_DBLESCAN;
930 		return 0;
931 	}
932 
933 	max_link_clock = intel_dp_max_link_rate(intel_dp);
934 	max_lanes = intel_dp_max_lane_count(intel_dp);
935 
936 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
937 	mode_rate = intel_dp_link_required(mode->clock, min_bpp);
938 
939 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
940 	if (ret)
941 		return ret;
942 
943 	if (mode_rate > max_rate || mode->clock > max_dotclk ||
944 	    drm_dp_calc_pbn_mode(mode->clock, min_bpp, false) > port->full_pbn) {
945 		*status = MODE_CLOCK_HIGH;
946 		return 0;
947 	}
948 
949 	if (mode->clock < 10000) {
950 		*status = MODE_CLOCK_LOW;
951 		return 0;
952 	}
953 
954 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
955 		*status = MODE_H_ILLEGAL;
956 		return 0;
957 	}
958 
959 	if (intel_dp_need_bigjoiner(intel_dp, mode->hdisplay, target_clock)) {
960 		bigjoiner = true;
961 		max_dotclk *= 2;
962 
963 		/* TODO: add support for bigjoiner */
964 		*status = MODE_CLOCK_HIGH;
965 		return 0;
966 	}
967 
968 	if (DISPLAY_VER(dev_priv) >= 10 &&
969 	    drm_dp_sink_supports_dsc(intel_dp->dsc_dpcd)) {
970 		/*
971 		 * TBD pass the connector BPC,
972 		 * for now U8_MAX so that max BPC on that platform would be picked
973 		 */
974 		int pipe_bpp = intel_dp_dsc_compute_bpp(intel_dp, U8_MAX);
975 
976 		if (drm_dp_sink_supports_fec(intel_dp->fec_capable)) {
977 			dsc_max_output_bpp =
978 				intel_dp_dsc_get_output_bpp(dev_priv,
979 							    max_link_clock,
980 							    max_lanes,
981 							    target_clock,
982 							    mode->hdisplay,
983 							    bigjoiner,
984 							    pipe_bpp, 64) >> 4;
985 			dsc_slice_count =
986 				intel_dp_dsc_get_slice_count(intel_dp,
987 							     target_clock,
988 							     mode->hdisplay,
989 							     bigjoiner);
990 		}
991 
992 		dsc = dsc_max_output_bpp && dsc_slice_count;
993 	}
994 
995 	/*
996 	 * Big joiner configuration needs DSC for TGL which is not true for
997 	 * XE_LPD where uncompressed joiner is supported.
998 	 */
999 	if (DISPLAY_VER(dev_priv) < 13 && bigjoiner && !dsc) {
1000 		*status = MODE_CLOCK_HIGH;
1001 		return 0;
1002 	}
1003 
1004 	if (mode_rate > max_rate && !dsc) {
1005 		*status = MODE_CLOCK_HIGH;
1006 		return 0;
1007 	}
1008 
1009 	*status = intel_mode_valid_max_plane_size(dev_priv, mode, false);
1010 	return 0;
1011 }
1012 
1013 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
1014 							 struct drm_atomic_state *state)
1015 {
1016 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1017 											 connector);
1018 	struct intel_connector *intel_connector = to_intel_connector(connector);
1019 	struct intel_dp *intel_dp = intel_connector->mst_port;
1020 	struct intel_crtc *crtc = to_intel_crtc(connector_state->crtc);
1021 
1022 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
1023 }
1024 
1025 static int
1026 intel_dp_mst_detect(struct drm_connector *connector,
1027 		    struct drm_modeset_acquire_ctx *ctx, bool force)
1028 {
1029 	struct drm_i915_private *i915 = to_i915(connector->dev);
1030 	struct intel_connector *intel_connector = to_intel_connector(connector);
1031 	struct intel_dp *intel_dp = intel_connector->mst_port;
1032 
1033 	if (!INTEL_DISPLAY_ENABLED(i915))
1034 		return connector_status_disconnected;
1035 
1036 	if (drm_connector_is_unregistered(connector))
1037 		return connector_status_disconnected;
1038 
1039 	return drm_dp_mst_detect_port(connector, ctx, &intel_dp->mst_mgr,
1040 				      intel_connector->port);
1041 }
1042 
1043 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
1044 	.get_modes = intel_dp_mst_get_modes,
1045 	.mode_valid_ctx = intel_dp_mst_mode_valid_ctx,
1046 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
1047 	.atomic_check = intel_dp_mst_atomic_check,
1048 	.detect_ctx = intel_dp_mst_detect,
1049 };
1050 
1051 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
1052 {
1053 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(to_intel_encoder(encoder));
1054 
1055 	drm_encoder_cleanup(encoder);
1056 	kfree(intel_mst);
1057 }
1058 
1059 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
1060 	.destroy = intel_dp_mst_encoder_destroy,
1061 };
1062 
1063 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
1064 {
1065 	if (intel_attached_encoder(connector) && connector->base.state->crtc) {
1066 		enum pipe pipe;
1067 		if (!intel_attached_encoder(connector)->get_hw_state(intel_attached_encoder(connector), &pipe))
1068 			return false;
1069 		return true;
1070 	}
1071 	return false;
1072 }
1073 
1074 static int intel_dp_mst_add_properties(struct intel_dp *intel_dp,
1075 				       struct drm_connector *connector,
1076 				       const char *pathprop)
1077 {
1078 	struct drm_i915_private *i915 = to_i915(connector->dev);
1079 
1080 	drm_object_attach_property(&connector->base,
1081 				   i915->drm.mode_config.path_property, 0);
1082 	drm_object_attach_property(&connector->base,
1083 				   i915->drm.mode_config.tile_property, 0);
1084 
1085 	intel_attach_force_audio_property(connector);
1086 	intel_attach_broadcast_rgb_property(connector);
1087 
1088 	/*
1089 	 * Reuse the prop from the SST connector because we're
1090 	 * not allowed to create new props after device registration.
1091 	 */
1092 	connector->max_bpc_property =
1093 		intel_dp->attached_connector->base.max_bpc_property;
1094 	if (connector->max_bpc_property)
1095 		drm_connector_attach_max_bpc_property(connector, 6, 12);
1096 
1097 	return drm_connector_set_path_property(connector, pathprop);
1098 }
1099 
1100 static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
1101 							struct drm_dp_mst_port *port,
1102 							const char *pathprop)
1103 {
1104 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
1105 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1106 	struct drm_device *dev = dig_port->base.base.dev;
1107 	struct drm_i915_private *dev_priv = to_i915(dev);
1108 	struct intel_connector *intel_connector;
1109 	struct drm_connector *connector;
1110 	enum pipe pipe;
1111 	int ret;
1112 
1113 	intel_connector = intel_connector_alloc();
1114 	if (!intel_connector)
1115 		return NULL;
1116 
1117 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
1118 	intel_connector->mst_port = intel_dp;
1119 	intel_connector->port = port;
1120 	drm_dp_mst_get_port_malloc(port);
1121 
1122 	connector = &intel_connector->base;
1123 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
1124 				 DRM_MODE_CONNECTOR_DisplayPort);
1125 	if (ret) {
1126 		drm_dp_mst_put_port_malloc(port);
1127 		intel_connector_free(intel_connector);
1128 		return NULL;
1129 	}
1130 
1131 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
1132 
1133 	for_each_pipe(dev_priv, pipe) {
1134 		struct drm_encoder *enc =
1135 			&intel_dp->mst_encoders[pipe]->base.base;
1136 
1137 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
1138 		if (ret)
1139 			goto err;
1140 	}
1141 
1142 	ret = intel_dp_mst_add_properties(intel_dp, connector, pathprop);
1143 	if (ret)
1144 		goto err;
1145 
1146 	ret = intel_dp_hdcp_init(dig_port, intel_connector);
1147 	if (ret)
1148 		drm_dbg_kms(&dev_priv->drm, "[%s:%d] HDCP MST init failed, skipping.\n",
1149 			    connector->name, connector->base.id);
1150 
1151 	return connector;
1152 
1153 err:
1154 	drm_connector_cleanup(connector);
1155 	return NULL;
1156 }
1157 
1158 static void
1159 intel_dp_mst_poll_hpd_irq(struct drm_dp_mst_topology_mgr *mgr)
1160 {
1161 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
1162 
1163 	intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
1164 }
1165 
1166 static const struct drm_dp_mst_topology_cbs mst_cbs = {
1167 	.add_connector = intel_dp_add_mst_connector,
1168 	.poll_hpd_irq = intel_dp_mst_poll_hpd_irq,
1169 };
1170 
1171 static struct intel_dp_mst_encoder *
1172 intel_dp_create_fake_mst_encoder(struct intel_digital_port *dig_port, enum pipe pipe)
1173 {
1174 	struct intel_dp_mst_encoder *intel_mst;
1175 	struct intel_encoder *intel_encoder;
1176 	struct drm_device *dev = dig_port->base.base.dev;
1177 
1178 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
1179 
1180 	if (!intel_mst)
1181 		return NULL;
1182 
1183 	intel_mst->pipe = pipe;
1184 	intel_encoder = &intel_mst->base;
1185 	intel_mst->primary = dig_port;
1186 
1187 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
1188 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
1189 
1190 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
1191 	intel_encoder->power_domain = dig_port->base.power_domain;
1192 	intel_encoder->port = dig_port->base.port;
1193 	intel_encoder->cloneable = 0;
1194 	/*
1195 	 * This is wrong, but broken userspace uses the intersection
1196 	 * of possible_crtcs of all the encoders of a given connector
1197 	 * to figure out which crtcs can drive said connector. What
1198 	 * should be used instead is the union of possible_crtcs.
1199 	 * To keep such userspace functioning we must misconfigure
1200 	 * this to make sure the intersection is not empty :(
1201 	 */
1202 	intel_encoder->pipe_mask = ~0;
1203 
1204 	intel_encoder->compute_config = intel_dp_mst_compute_config;
1205 	intel_encoder->compute_config_late = intel_dp_mst_compute_config_late;
1206 	intel_encoder->disable = intel_mst_disable_dp;
1207 	intel_encoder->post_disable = intel_mst_post_disable_dp;
1208 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
1209 	intel_encoder->update_pipe = intel_ddi_update_pipe;
1210 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
1211 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
1212 	intel_encoder->enable = intel_mst_enable_dp;
1213 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
1214 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
1215 	intel_encoder->initial_fastset_check = intel_dp_mst_initial_fastset_check;
1216 
1217 	return intel_mst;
1218 
1219 }
1220 
1221 static bool
1222 intel_dp_create_fake_mst_encoders(struct intel_digital_port *dig_port)
1223 {
1224 	struct intel_dp *intel_dp = &dig_port->dp;
1225 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1226 	enum pipe pipe;
1227 
1228 	for_each_pipe(dev_priv, pipe)
1229 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(dig_port, pipe);
1230 	return true;
1231 }
1232 
1233 int
1234 intel_dp_mst_encoder_active_links(struct intel_digital_port *dig_port)
1235 {
1236 	return dig_port->dp.active_mst_links;
1237 }
1238 
1239 int
1240 intel_dp_mst_encoder_init(struct intel_digital_port *dig_port, int conn_base_id)
1241 {
1242 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1243 	struct intel_dp *intel_dp = &dig_port->dp;
1244 	enum port port = dig_port->base.port;
1245 	int ret;
1246 
1247 	if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
1248 		return 0;
1249 
1250 	if (DISPLAY_VER(i915) < 12 && port == PORT_A)
1251 		return 0;
1252 
1253 	if (DISPLAY_VER(i915) < 11 && port == PORT_E)
1254 		return 0;
1255 
1256 	intel_dp->mst_mgr.cbs = &mst_cbs;
1257 
1258 	/* create encoders */
1259 	intel_dp_create_fake_mst_encoders(dig_port);
1260 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
1261 					   &intel_dp->aux, 16, 3, conn_base_id);
1262 	if (ret) {
1263 		intel_dp->mst_mgr.cbs = NULL;
1264 		return ret;
1265 	}
1266 
1267 	return 0;
1268 }
1269 
1270 bool intel_dp_mst_source_support(struct intel_dp *intel_dp)
1271 {
1272 	return intel_dp->mst_mgr.cbs;
1273 }
1274 
1275 void
1276 intel_dp_mst_encoder_cleanup(struct intel_digital_port *dig_port)
1277 {
1278 	struct intel_dp *intel_dp = &dig_port->dp;
1279 
1280 	if (!intel_dp_mst_source_support(intel_dp))
1281 		return;
1282 
1283 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
1284 	/* encoders will get killed by normal cleanup */
1285 
1286 	intel_dp->mst_mgr.cbs = NULL;
1287 }
1288 
1289 bool intel_dp_mst_is_master_trans(const struct intel_crtc_state *crtc_state)
1290 {
1291 	return crtc_state->mst_master_transcoder == crtc_state->cpu_transcoder;
1292 }
1293 
1294 bool intel_dp_mst_is_slave_trans(const struct intel_crtc_state *crtc_state)
1295 {
1296 	return crtc_state->mst_master_transcoder != INVALID_TRANSCODER &&
1297 	       crtc_state->mst_master_transcoder != crtc_state->cpu_transcoder;
1298 }
1299 
1300 /**
1301  * intel_dp_mst_add_topology_state_for_connector - add MST topology state for a connector
1302  * @state: atomic state
1303  * @connector: connector to add the state for
1304  * @crtc: the CRTC @connector is attached to
1305  *
1306  * Add the MST topology state for @connector to @state.
1307  *
1308  * Returns 0 on success, negative error code on failure.
1309  */
1310 static int
1311 intel_dp_mst_add_topology_state_for_connector(struct intel_atomic_state *state,
1312 					      struct intel_connector *connector,
1313 					      struct intel_crtc *crtc)
1314 {
1315 	struct drm_dp_mst_topology_state *mst_state;
1316 
1317 	if (!connector->mst_port)
1318 		return 0;
1319 
1320 	mst_state = drm_atomic_get_mst_topology_state(&state->base,
1321 						      &connector->mst_port->mst_mgr);
1322 	if (IS_ERR(mst_state))
1323 		return PTR_ERR(mst_state);
1324 
1325 	mst_state->pending_crtc_mask |= drm_crtc_mask(&crtc->base);
1326 
1327 	return 0;
1328 }
1329 
1330 /**
1331  * intel_dp_mst_add_topology_state_for_crtc - add MST topology state for a CRTC
1332  * @state: atomic state
1333  * @crtc: CRTC to add the state for
1334  *
1335  * Add the MST topology state for @crtc to @state.
1336  *
1337  * Returns 0 on success, negative error code on failure.
1338  */
1339 int intel_dp_mst_add_topology_state_for_crtc(struct intel_atomic_state *state,
1340 					     struct intel_crtc *crtc)
1341 {
1342 	struct drm_connector *_connector;
1343 	struct drm_connector_state *conn_state;
1344 	int i;
1345 
1346 	for_each_new_connector_in_state(&state->base, _connector, conn_state, i) {
1347 		struct intel_connector *connector = to_intel_connector(_connector);
1348 		int ret;
1349 
1350 		if (conn_state->crtc != &crtc->base)
1351 			continue;
1352 
1353 		ret = intel_dp_mst_add_topology_state_for_connector(state, connector, crtc);
1354 		if (ret)
1355 			return ret;
1356 	}
1357 
1358 	return 0;
1359 }
1360