xref: /linux/drivers/gpu/drm/i915/display/intel_dp_mst.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
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_fixed.h>
30 #include <drm/drm_probe_helper.h>
31 
32 #include "i915_drv.h"
33 #include "i915_reg.h"
34 #include "intel_atomic.h"
35 #include "intel_audio.h"
36 #include "intel_connector.h"
37 #include "intel_crtc.h"
38 #include "intel_ddi.h"
39 #include "intel_de.h"
40 #include "intel_display_driver.h"
41 #include "intel_display_types.h"
42 #include "intel_dp.h"
43 #include "intel_dp_hdcp.h"
44 #include "intel_dp_mst.h"
45 #include "intel_dp_tunnel.h"
46 #include "intel_dpio_phy.h"
47 #include "intel_hdcp.h"
48 #include "intel_hotplug.h"
49 #include "intel_link_bw.h"
50 #include "intel_psr.h"
51 #include "intel_vdsc.h"
52 #include "skl_scaler.h"
53 
54 static int intel_dp_mst_check_constraints(struct drm_i915_private *i915, int bpp,
55 					  const struct drm_display_mode *adjusted_mode,
56 					  struct intel_crtc_state *crtc_state,
57 					  bool dsc)
58 {
59 	if (intel_dp_is_uhbr(crtc_state) && DISPLAY_VER(i915) < 14 && dsc) {
60 		int output_bpp = bpp;
61 		/* DisplayPort 2 128b/132b, bits per lane is always 32 */
62 		int symbol_clock = crtc_state->port_clock / 32;
63 
64 		if (output_bpp * adjusted_mode->crtc_clock >=
65 		    symbol_clock * 72) {
66 			drm_dbg_kms(&i915->drm, "UHBR check failed(required bw %d available %d)\n",
67 				    output_bpp * adjusted_mode->crtc_clock, symbol_clock * 72);
68 			return -EINVAL;
69 		}
70 	}
71 
72 	return 0;
73 }
74 
75 static int intel_dp_mst_bw_overhead(const struct intel_crtc_state *crtc_state,
76 				    const struct intel_connector *connector,
77 				    bool ssc, bool dsc, int bpp_x16)
78 {
79 	const struct drm_display_mode *adjusted_mode =
80 		&crtc_state->hw.adjusted_mode;
81 	unsigned long flags = DRM_DP_BW_OVERHEAD_MST;
82 	int dsc_slice_count = 0;
83 	int overhead;
84 
85 	flags |= intel_dp_is_uhbr(crtc_state) ? DRM_DP_BW_OVERHEAD_UHBR : 0;
86 	flags |= ssc ? DRM_DP_BW_OVERHEAD_SSC_REF_CLK : 0;
87 	flags |= crtc_state->fec_enable ? DRM_DP_BW_OVERHEAD_FEC : 0;
88 
89 	if (dsc) {
90 		flags |= DRM_DP_BW_OVERHEAD_DSC;
91 		dsc_slice_count = intel_dp_dsc_get_slice_count(connector,
92 							       adjusted_mode->clock,
93 							       adjusted_mode->hdisplay,
94 							       crtc_state->bigjoiner_pipes);
95 	}
96 
97 	overhead = drm_dp_bw_overhead(crtc_state->lane_count,
98 				      adjusted_mode->hdisplay,
99 				      dsc_slice_count,
100 				      bpp_x16,
101 				      flags);
102 
103 	/*
104 	 * TODO: clarify whether a minimum required by the fixed FEC overhead
105 	 * in the bspec audio programming sequence is required here.
106 	 */
107 	return max(overhead, intel_dp_bw_fec_overhead(crtc_state->fec_enable));
108 }
109 
110 static void intel_dp_mst_compute_m_n(const struct intel_crtc_state *crtc_state,
111 				     const struct intel_connector *connector,
112 				     int overhead,
113 				     int bpp_x16,
114 				     struct intel_link_m_n *m_n)
115 {
116 	const struct drm_display_mode *adjusted_mode =
117 		&crtc_state->hw.adjusted_mode;
118 
119 	/* TODO: Check WA 14013163432 to set data M/N for full BW utilization. */
120 	intel_link_compute_m_n(bpp_x16, crtc_state->lane_count,
121 			       adjusted_mode->crtc_clock,
122 			       crtc_state->port_clock,
123 			       overhead,
124 			       m_n);
125 
126 	m_n->tu = DIV_ROUND_UP_ULL(mul_u32_u32(m_n->data_m, 64), m_n->data_n);
127 }
128 
129 static int intel_dp_mst_calc_pbn(int pixel_clock, int bpp_x16, int bw_overhead)
130 {
131 	int effective_data_rate =
132 		intel_dp_effective_data_rate(pixel_clock, bpp_x16, bw_overhead);
133 
134 	/*
135 	 * TODO: Use drm_dp_calc_pbn_mode() instead, once it's converted
136 	 * to calculate PBN with the BW overhead passed to it.
137 	 */
138 	return DIV_ROUND_UP(effective_data_rate * 64, 54 * 1000);
139 }
140 
141 static int intel_dp_mst_find_vcpi_slots_for_bpp(struct intel_encoder *encoder,
142 						struct intel_crtc_state *crtc_state,
143 						int max_bpp,
144 						int min_bpp,
145 						struct link_config_limits *limits,
146 						struct drm_connector_state *conn_state,
147 						int step,
148 						bool dsc)
149 {
150 	struct drm_atomic_state *state = crtc_state->uapi.state;
151 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
152 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
153 	struct drm_dp_mst_topology_state *mst_state;
154 	struct intel_connector *connector =
155 		to_intel_connector(conn_state->connector);
156 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
157 	const struct drm_display_mode *adjusted_mode =
158 		&crtc_state->hw.adjusted_mode;
159 	int bpp, slots = -EINVAL;
160 	int ret = 0;
161 
162 	mst_state = drm_atomic_get_mst_topology_state(state, &intel_dp->mst_mgr);
163 	if (IS_ERR(mst_state))
164 		return PTR_ERR(mst_state);
165 
166 	crtc_state->lane_count = limits->max_lane_count;
167 	crtc_state->port_clock = limits->max_rate;
168 
169 	if (dsc) {
170 		if (!intel_dp_supports_fec(intel_dp, connector, crtc_state))
171 			return -EINVAL;
172 
173 		crtc_state->fec_enable = !intel_dp_is_uhbr(crtc_state);
174 	}
175 
176 	mst_state->pbn_div = drm_dp_get_vc_payload_bw(&intel_dp->mst_mgr,
177 						      crtc_state->port_clock,
178 						      crtc_state->lane_count);
179 
180 	drm_dbg_kms(&i915->drm, "Looking for slots in range min bpp %d max bpp %d\n",
181 		    min_bpp, max_bpp);
182 
183 	for (bpp = max_bpp; bpp >= min_bpp; bpp -= step) {
184 		int local_bw_overhead;
185 		int remote_bw_overhead;
186 		int link_bpp_x16;
187 		int remote_tu;
188 
189 		drm_dbg_kms(&i915->drm, "Trying bpp %d\n", bpp);
190 
191 		ret = intel_dp_mst_check_constraints(i915, bpp, adjusted_mode, crtc_state, dsc);
192 		if (ret)
193 			continue;
194 
195 		link_bpp_x16 = to_bpp_x16(dsc ? bpp :
196 					  intel_dp_output_bpp(crtc_state->output_format, bpp));
197 
198 		local_bw_overhead = intel_dp_mst_bw_overhead(crtc_state, connector,
199 							     false, dsc, link_bpp_x16);
200 		remote_bw_overhead = intel_dp_mst_bw_overhead(crtc_state, connector,
201 							      true, dsc, link_bpp_x16);
202 
203 		intel_dp_mst_compute_m_n(crtc_state, connector,
204 					 local_bw_overhead,
205 					 link_bpp_x16,
206 					 &crtc_state->dp_m_n);
207 
208 		/*
209 		 * The TU size programmed to the HW determines which slots in
210 		 * an MTP frame are used for this stream, which needs to match
211 		 * the payload size programmed to the first downstream branch
212 		 * device's payload table.
213 		 *
214 		 * Note that atm the payload's PBN value DRM core sends via
215 		 * the ALLOCATE_PAYLOAD side-band message matches the payload
216 		 * size (which it calculates from the PBN value) it programs
217 		 * to the first branch device's payload table. The allocation
218 		 * in the payload table could be reduced though (to
219 		 * crtc_state->dp_m_n.tu), provided that the driver doesn't
220 		 * enable SSC on the corresponding link.
221 		 */
222 		crtc_state->pbn = intel_dp_mst_calc_pbn(adjusted_mode->crtc_clock,
223 							link_bpp_x16,
224 							remote_bw_overhead);
225 
226 		remote_tu = DIV_ROUND_UP(dfixed_const(crtc_state->pbn), mst_state->pbn_div.full);
227 
228 		drm_WARN_ON(&i915->drm, remote_tu < crtc_state->dp_m_n.tu);
229 		crtc_state->dp_m_n.tu = remote_tu;
230 
231 		slots = drm_dp_atomic_find_time_slots(state, &intel_dp->mst_mgr,
232 						      connector->port,
233 						      crtc_state->pbn);
234 		if (slots == -EDEADLK)
235 			return slots;
236 
237 		if (slots >= 0) {
238 			drm_WARN_ON(&i915->drm, slots != crtc_state->dp_m_n.tu);
239 
240 			break;
241 		}
242 	}
243 
244 	/* We failed to find a proper bpp/timeslots, return error */
245 	if (ret)
246 		slots = ret;
247 
248 	if (slots < 0) {
249 		drm_dbg_kms(&i915->drm, "failed finding vcpi slots:%d\n",
250 			    slots);
251 	} else {
252 		if (!dsc)
253 			crtc_state->pipe_bpp = bpp;
254 		else
255 			crtc_state->dsc.compressed_bpp_x16 = to_bpp_x16(bpp);
256 		drm_dbg_kms(&i915->drm, "Got %d slots for pipe bpp %d dsc %d\n", slots, bpp, dsc);
257 	}
258 
259 	return slots;
260 }
261 
262 static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
263 					    struct intel_crtc_state *crtc_state,
264 					    struct drm_connector_state *conn_state,
265 					    struct link_config_limits *limits)
266 {
267 	int slots = -EINVAL;
268 
269 	/*
270 	 * FIXME: allocate the BW according to link_bpp, which in the case of
271 	 * YUV420 is only half of the pipe bpp value.
272 	 */
273 	slots = intel_dp_mst_find_vcpi_slots_for_bpp(encoder, crtc_state,
274 						     to_bpp_int(limits->link.max_bpp_x16),
275 						     to_bpp_int(limits->link.min_bpp_x16),
276 						     limits,
277 						     conn_state, 2 * 3, false);
278 
279 	if (slots < 0)
280 		return slots;
281 
282 	return 0;
283 }
284 
285 static int intel_dp_dsc_mst_compute_link_config(struct intel_encoder *encoder,
286 						struct intel_crtc_state *crtc_state,
287 						struct drm_connector_state *conn_state,
288 						struct link_config_limits *limits)
289 {
290 	struct intel_connector *connector =
291 		to_intel_connector(conn_state->connector);
292 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
293 	int slots = -EINVAL;
294 	int i, num_bpc;
295 	u8 dsc_bpc[3] = {};
296 	int min_bpp, max_bpp, sink_min_bpp, sink_max_bpp;
297 	u8 dsc_max_bpc;
298 	int min_compressed_bpp, max_compressed_bpp;
299 
300 	/* Max DSC Input BPC for ICL is 10 and for TGL+ is 12 */
301 	if (DISPLAY_VER(i915) >= 12)
302 		dsc_max_bpc = min_t(u8, 12, conn_state->max_requested_bpc);
303 	else
304 		dsc_max_bpc = min_t(u8, 10, conn_state->max_requested_bpc);
305 
306 	max_bpp = min_t(u8, dsc_max_bpc * 3, limits->pipe.max_bpp);
307 	min_bpp = limits->pipe.min_bpp;
308 
309 	num_bpc = drm_dp_dsc_sink_supported_input_bpcs(connector->dp.dsc_dpcd,
310 						       dsc_bpc);
311 
312 	drm_dbg_kms(&i915->drm, "DSC Source supported min bpp %d max bpp %d\n",
313 		    min_bpp, max_bpp);
314 
315 	sink_max_bpp = dsc_bpc[0] * 3;
316 	sink_min_bpp = sink_max_bpp;
317 
318 	for (i = 1; i < num_bpc; i++) {
319 		if (sink_min_bpp > dsc_bpc[i] * 3)
320 			sink_min_bpp = dsc_bpc[i] * 3;
321 		if (sink_max_bpp < dsc_bpc[i] * 3)
322 			sink_max_bpp = dsc_bpc[i] * 3;
323 	}
324 
325 	drm_dbg_kms(&i915->drm, "DSC Sink supported min bpp %d max bpp %d\n",
326 		    sink_min_bpp, sink_max_bpp);
327 
328 	if (min_bpp < sink_min_bpp)
329 		min_bpp = sink_min_bpp;
330 
331 	if (max_bpp > sink_max_bpp)
332 		max_bpp = sink_max_bpp;
333 
334 	max_compressed_bpp = intel_dp_dsc_sink_max_compressed_bpp(connector,
335 								  crtc_state,
336 								  max_bpp / 3);
337 	max_compressed_bpp = min(max_compressed_bpp,
338 				 to_bpp_int(limits->link.max_bpp_x16));
339 
340 	min_compressed_bpp = intel_dp_dsc_sink_min_compressed_bpp(crtc_state);
341 	min_compressed_bpp = max(min_compressed_bpp,
342 				 to_bpp_int_roundup(limits->link.min_bpp_x16));
343 
344 	drm_dbg_kms(&i915->drm, "DSC Sink supported compressed min bpp %d compressed max bpp %d\n",
345 		    min_compressed_bpp, max_compressed_bpp);
346 
347 	/* Align compressed bpps according to our own constraints */
348 	max_compressed_bpp = intel_dp_dsc_nearest_valid_bpp(i915, max_compressed_bpp,
349 							    crtc_state->pipe_bpp);
350 	min_compressed_bpp = intel_dp_dsc_nearest_valid_bpp(i915, min_compressed_bpp,
351 							    crtc_state->pipe_bpp);
352 
353 	slots = intel_dp_mst_find_vcpi_slots_for_bpp(encoder, crtc_state, max_compressed_bpp,
354 						     min_compressed_bpp, limits,
355 						     conn_state, 1, true);
356 
357 	if (slots < 0)
358 		return slots;
359 
360 	return 0;
361 }
362 static int intel_dp_mst_update_slots(struct intel_encoder *encoder,
363 				     struct intel_crtc_state *crtc_state,
364 				     struct drm_connector_state *conn_state)
365 {
366 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
367 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
368 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
369 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
370 	struct drm_dp_mst_topology_state *topology_state;
371 	u8 link_coding_cap = intel_dp_is_uhbr(crtc_state) ?
372 		DP_CAP_ANSI_128B132B : DP_CAP_ANSI_8B10B;
373 
374 	topology_state = drm_atomic_get_mst_topology_state(conn_state->state, mgr);
375 	if (IS_ERR(topology_state)) {
376 		drm_dbg_kms(&i915->drm, "slot update failed\n");
377 		return PTR_ERR(topology_state);
378 	}
379 
380 	drm_dp_mst_update_slots(topology_state, link_coding_cap);
381 
382 	return 0;
383 }
384 
385 static bool
386 intel_dp_mst_dsc_source_support(const struct intel_crtc_state *crtc_state)
387 {
388 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
389 
390 	/*
391 	 * FIXME: Enabling DSC on ICL results in blank screen and FIFO pipe /
392 	 * transcoder underruns, re-enable DSC after fixing this issue.
393 	 */
394 	return DISPLAY_VER(i915) >= 12 && intel_dsc_source_support(crtc_state);
395 }
396 
397 static int mode_hblank_period_ns(const struct drm_display_mode *mode)
398 {
399 	return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(mode->htotal - mode->hdisplay,
400 						 NSEC_PER_SEC / 1000),
401 				     mode->crtc_clock);
402 }
403 
404 static bool
405 hblank_expansion_quirk_needs_dsc(const struct intel_connector *connector,
406 				 const struct intel_crtc_state *crtc_state)
407 {
408 	const struct drm_display_mode *adjusted_mode =
409 		&crtc_state->hw.adjusted_mode;
410 
411 	if (!connector->dp.dsc_hblank_expansion_quirk)
412 		return false;
413 
414 	if (mode_hblank_period_ns(adjusted_mode) > 300)
415 		return false;
416 
417 	return true;
418 }
419 
420 static bool
421 adjust_limits_for_dsc_hblank_expansion_quirk(const struct intel_connector *connector,
422 					     const struct intel_crtc_state *crtc_state,
423 					     struct link_config_limits *limits,
424 					     bool dsc)
425 {
426 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
427 	const struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
428 	int min_bpp_x16 = limits->link.min_bpp_x16;
429 
430 	if (!hblank_expansion_quirk_needs_dsc(connector, crtc_state))
431 		return true;
432 
433 	if (!dsc) {
434 		if (intel_dp_mst_dsc_source_support(crtc_state)) {
435 			drm_dbg_kms(&i915->drm,
436 				    "[CRTC:%d:%s][CONNECTOR:%d:%s] DSC needed by hblank expansion quirk\n",
437 				    crtc->base.base.id, crtc->base.name,
438 				    connector->base.base.id, connector->base.name);
439 			return false;
440 		}
441 
442 		drm_dbg_kms(&i915->drm,
443 			    "[CRTC:%d:%s][CONNECTOR:%d:%s] Increasing link min bpp to 24 due to hblank expansion quirk\n",
444 			    crtc->base.base.id, crtc->base.name,
445 			    connector->base.base.id, connector->base.name);
446 
447 		if (limits->link.max_bpp_x16 < to_bpp_x16(24))
448 			return false;
449 
450 		limits->link.min_bpp_x16 = to_bpp_x16(24);
451 
452 		return true;
453 	}
454 
455 	drm_WARN_ON(&i915->drm, limits->min_rate != limits->max_rate);
456 
457 	if (limits->max_rate < 540000)
458 		min_bpp_x16 = to_bpp_x16(13);
459 	else if (limits->max_rate < 810000)
460 		min_bpp_x16 = to_bpp_x16(10);
461 
462 	if (limits->link.min_bpp_x16 >= min_bpp_x16)
463 		return true;
464 
465 	drm_dbg_kms(&i915->drm,
466 		    "[CRTC:%d:%s][CONNECTOR:%d:%s] Increasing link min bpp to " BPP_X16_FMT " in DSC mode due to hblank expansion quirk\n",
467 		    crtc->base.base.id, crtc->base.name,
468 		    connector->base.base.id, connector->base.name,
469 		    BPP_X16_ARGS(min_bpp_x16));
470 
471 	if (limits->link.max_bpp_x16 < min_bpp_x16)
472 		return false;
473 
474 	limits->link.min_bpp_x16 = min_bpp_x16;
475 
476 	return true;
477 }
478 
479 static bool
480 intel_dp_mst_compute_config_limits(struct intel_dp *intel_dp,
481 				   const struct intel_connector *connector,
482 				   struct intel_crtc_state *crtc_state,
483 				   bool dsc,
484 				   struct link_config_limits *limits)
485 {
486 	/*
487 	 * for MST we always configure max link bw - the spec doesn't
488 	 * seem to suggest we should do otherwise.
489 	 */
490 	limits->min_rate = limits->max_rate =
491 		intel_dp_max_link_rate(intel_dp);
492 
493 	limits->min_lane_count = limits->max_lane_count =
494 		intel_dp_max_lane_count(intel_dp);
495 
496 	limits->pipe.min_bpp = intel_dp_min_bpp(crtc_state->output_format);
497 	/*
498 	 * FIXME: If all the streams can't fit into the link with
499 	 * their current pipe_bpp we should reduce pipe_bpp across
500 	 * the board until things start to fit. Until then we
501 	 * limit to <= 8bpc since that's what was hardcoded for all
502 	 * MST streams previously. This hack should be removed once
503 	 * we have the proper retry logic in place.
504 	 */
505 	limits->pipe.max_bpp = min(crtc_state->pipe_bpp, 24);
506 
507 	intel_dp_adjust_compliance_config(intel_dp, crtc_state, limits);
508 
509 	if (!intel_dp_compute_config_link_bpp_limits(intel_dp,
510 						     crtc_state,
511 						     dsc,
512 						     limits))
513 		return false;
514 
515 	return adjust_limits_for_dsc_hblank_expansion_quirk(connector,
516 							    crtc_state,
517 							    limits,
518 							    dsc);
519 }
520 
521 static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
522 				       struct intel_crtc_state *pipe_config,
523 				       struct drm_connector_state *conn_state)
524 {
525 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
526 	struct intel_atomic_state *state = to_intel_atomic_state(conn_state->state);
527 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
528 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
529 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
530 	struct intel_connector *connector =
531 		to_intel_connector(conn_state->connector);
532 	const struct drm_display_mode *adjusted_mode =
533 		&pipe_config->hw.adjusted_mode;
534 	struct link_config_limits limits;
535 	bool dsc_needed, joiner_needs_dsc;
536 	int ret = 0;
537 
538 	if (pipe_config->fec_enable &&
539 	    !intel_dp_supports_fec(intel_dp, connector, pipe_config))
540 		return -EINVAL;
541 
542 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
543 		return -EINVAL;
544 
545 	if (intel_dp_need_bigjoiner(intel_dp, connector,
546 				    adjusted_mode->crtc_hdisplay,
547 				    adjusted_mode->crtc_clock))
548 		pipe_config->bigjoiner_pipes = GENMASK(crtc->pipe + 1, crtc->pipe);
549 
550 	pipe_config->sink_format = INTEL_OUTPUT_FORMAT_RGB;
551 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
552 	pipe_config->has_pch_encoder = false;
553 
554 	joiner_needs_dsc = intel_dp_joiner_needs_dsc(dev_priv, pipe_config->bigjoiner_pipes);
555 
556 	dsc_needed = joiner_needs_dsc || intel_dp->force_dsc_en ||
557 		     !intel_dp_mst_compute_config_limits(intel_dp,
558 							 connector,
559 							 pipe_config,
560 							 false,
561 							 &limits);
562 
563 	if (!dsc_needed) {
564 		ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
565 						       conn_state, &limits);
566 
567 		if (ret == -EDEADLK)
568 			return ret;
569 
570 		if (ret)
571 			dsc_needed = true;
572 	}
573 
574 	/* enable compression if the mode doesn't fit available BW */
575 	if (dsc_needed) {
576 		drm_dbg_kms(&dev_priv->drm, "Try DSC (fallback=%s, joiner=%s, force=%s)\n",
577 			    str_yes_no(ret), str_yes_no(joiner_needs_dsc),
578 			    str_yes_no(intel_dp->force_dsc_en));
579 
580 		if (!intel_dp_mst_dsc_source_support(pipe_config))
581 			return -EINVAL;
582 
583 		if (!intel_dp_mst_compute_config_limits(intel_dp,
584 							connector,
585 							pipe_config,
586 							true,
587 							&limits))
588 			return -EINVAL;
589 
590 		/*
591 		 * FIXME: As bpc is hardcoded to 8, as mentioned above,
592 		 * WARN and ignore the debug flag force_dsc_bpc for now.
593 		 */
594 		drm_WARN(&dev_priv->drm, intel_dp->force_dsc_bpc, "Cannot Force BPC for MST\n");
595 		/*
596 		 * Try to get at least some timeslots and then see, if
597 		 * we can fit there with DSC.
598 		 */
599 		drm_dbg_kms(&dev_priv->drm, "Trying to find VCPI slots in DSC mode\n");
600 
601 		ret = intel_dp_dsc_mst_compute_link_config(encoder, pipe_config,
602 							   conn_state, &limits);
603 		if (ret < 0)
604 			return ret;
605 
606 		ret = intel_dp_dsc_compute_config(intel_dp, pipe_config,
607 						  conn_state, &limits,
608 						  pipe_config->dp_m_n.tu, false);
609 	}
610 
611 	if (ret)
612 		return ret;
613 
614 	ret = intel_dp_mst_update_slots(encoder, pipe_config, conn_state);
615 	if (ret)
616 		return ret;
617 
618 	pipe_config->limited_color_range =
619 		intel_dp_limited_color_range(pipe_config, conn_state);
620 
621 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
622 		pipe_config->lane_lat_optim_mask =
623 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
624 
625 	intel_dp_audio_compute_config(encoder, pipe_config, conn_state);
626 
627 	intel_ddi_compute_min_voltage_level(pipe_config);
628 
629 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
630 
631 	return intel_dp_tunnel_atomic_compute_stream_bw(state, intel_dp, connector,
632 							pipe_config);
633 }
634 
635 /*
636  * Iterate over all connectors and return a mask of
637  * all CPU transcoders streaming over the same DP link.
638  */
639 static unsigned int
640 intel_dp_mst_transcoder_mask(struct intel_atomic_state *state,
641 			     struct intel_dp *mst_port)
642 {
643 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
644 	const struct intel_digital_connector_state *conn_state;
645 	struct intel_connector *connector;
646 	u8 transcoders = 0;
647 	int i;
648 
649 	if (DISPLAY_VER(dev_priv) < 12)
650 		return 0;
651 
652 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
653 		const struct intel_crtc_state *crtc_state;
654 		struct intel_crtc *crtc;
655 
656 		if (connector->mst_port != mst_port || !conn_state->base.crtc)
657 			continue;
658 
659 		crtc = to_intel_crtc(conn_state->base.crtc);
660 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
661 
662 		if (!crtc_state->hw.active)
663 			continue;
664 
665 		transcoders |= BIT(crtc_state->cpu_transcoder);
666 	}
667 
668 	return transcoders;
669 }
670 
671 static u8 get_pipes_downstream_of_mst_port(struct intel_atomic_state *state,
672 					   struct drm_dp_mst_topology_mgr *mst_mgr,
673 					   struct drm_dp_mst_port *parent_port)
674 {
675 	const struct intel_digital_connector_state *conn_state;
676 	struct intel_connector *connector;
677 	u8 mask = 0;
678 	int i;
679 
680 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
681 		if (!conn_state->base.crtc)
682 			continue;
683 
684 		if (&connector->mst_port->mst_mgr != mst_mgr)
685 			continue;
686 
687 		if (connector->port != parent_port &&
688 		    !drm_dp_mst_port_downstream_of_parent(mst_mgr,
689 							  connector->port,
690 							  parent_port))
691 			continue;
692 
693 		mask |= BIT(to_intel_crtc(conn_state->base.crtc)->pipe);
694 	}
695 
696 	return mask;
697 }
698 
699 static int intel_dp_mst_check_fec_change(struct intel_atomic_state *state,
700 					 struct drm_dp_mst_topology_mgr *mst_mgr,
701 					 struct intel_link_bw_limits *limits)
702 {
703 	struct drm_i915_private *i915 = to_i915(state->base.dev);
704 	struct intel_crtc *crtc;
705 	u8 mst_pipe_mask;
706 	u8 fec_pipe_mask = 0;
707 	int ret;
708 
709 	mst_pipe_mask = get_pipes_downstream_of_mst_port(state, mst_mgr, NULL);
710 
711 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, mst_pipe_mask) {
712 		struct intel_crtc_state *crtc_state =
713 			intel_atomic_get_new_crtc_state(state, crtc);
714 
715 		/* Atomic connector check should've added all the MST CRTCs. */
716 		if (drm_WARN_ON(&i915->drm, !crtc_state))
717 			return -EINVAL;
718 
719 		if (crtc_state->fec_enable)
720 			fec_pipe_mask |= BIT(crtc->pipe);
721 	}
722 
723 	if (!fec_pipe_mask || mst_pipe_mask == fec_pipe_mask)
724 		return 0;
725 
726 	limits->force_fec_pipes |= mst_pipe_mask;
727 
728 	ret = intel_modeset_pipes_in_mask_early(state, "MST FEC",
729 						mst_pipe_mask);
730 
731 	return ret ? : -EAGAIN;
732 }
733 
734 static int intel_dp_mst_check_bw(struct intel_atomic_state *state,
735 				 struct drm_dp_mst_topology_mgr *mst_mgr,
736 				 struct drm_dp_mst_topology_state *mst_state,
737 				 struct intel_link_bw_limits *limits)
738 {
739 	struct drm_dp_mst_port *mst_port;
740 	u8 mst_port_pipes;
741 	int ret;
742 
743 	ret = drm_dp_mst_atomic_check_mgr(&state->base, mst_mgr, mst_state, &mst_port);
744 	if (ret != -ENOSPC)
745 		return ret;
746 
747 	mst_port_pipes = get_pipes_downstream_of_mst_port(state, mst_mgr, mst_port);
748 
749 	ret = intel_link_bw_reduce_bpp(state, limits,
750 				       mst_port_pipes, "MST link BW");
751 
752 	return ret ? : -EAGAIN;
753 }
754 
755 /**
756  * intel_dp_mst_atomic_check_link - check all modeset MST link configuration
757  * @state: intel atomic state
758  * @limits: link BW limits
759  *
760  * Check the link configuration for all modeset MST outputs. If the
761  * configuration is invalid @limits will be updated if possible to
762  * reduce the total BW, after which the configuration for all CRTCs in
763  * @state must be recomputed with the updated @limits.
764  *
765  * Returns:
766  *   - 0 if the confugration is valid
767  *   - %-EAGAIN, if the configuration is invalid and @limits got updated
768  *     with fallback values with which the configuration of all CRTCs in
769  *     @state must be recomputed
770  *   - Other negative error, if the configuration is invalid without a
771  *     fallback possibility, or the check failed for another reason
772  */
773 int intel_dp_mst_atomic_check_link(struct intel_atomic_state *state,
774 				   struct intel_link_bw_limits *limits)
775 {
776 	struct drm_dp_mst_topology_mgr *mgr;
777 	struct drm_dp_mst_topology_state *mst_state;
778 	int ret;
779 	int i;
780 
781 	for_each_new_mst_mgr_in_state(&state->base, mgr, mst_state, i) {
782 		ret = intel_dp_mst_check_fec_change(state, mgr, limits);
783 		if (ret)
784 			return ret;
785 
786 		ret = intel_dp_mst_check_bw(state, mgr, mst_state,
787 					    limits);
788 		if (ret)
789 			return ret;
790 	}
791 
792 	return 0;
793 }
794 
795 static int intel_dp_mst_compute_config_late(struct intel_encoder *encoder,
796 					    struct intel_crtc_state *crtc_state,
797 					    struct drm_connector_state *conn_state)
798 {
799 	struct intel_atomic_state *state = to_intel_atomic_state(conn_state->state);
800 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
801 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
802 
803 	/* lowest numbered transcoder will be designated master */
804 	crtc_state->mst_master_transcoder =
805 		ffs(intel_dp_mst_transcoder_mask(state, intel_dp)) - 1;
806 
807 	return 0;
808 }
809 
810 /*
811  * If one of the connectors in a MST stream needs a modeset, mark all CRTCs
812  * that shares the same MST stream as mode changed,
813  * intel_modeset_pipe_config()+intel_crtc_check_fastset() will take care to do
814  * a fastset when possible.
815  *
816  * On TGL+ this is required since each stream go through a master transcoder,
817  * so if the master transcoder needs modeset, all other streams in the
818  * topology need a modeset. All platforms need to add the atomic state
819  * for all streams in the topology, since a modeset on one may require
820  * changing the MST link BW usage of the others, which in turn needs a
821  * recomputation of the corresponding CRTC states.
822  */
823 static int
824 intel_dp_mst_atomic_topology_check(struct intel_connector *connector,
825 				   struct intel_atomic_state *state)
826 {
827 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
828 	struct drm_connector_list_iter connector_list_iter;
829 	struct intel_connector *connector_iter;
830 	int ret = 0;
831 
832 	if (!intel_connector_needs_modeset(state, &connector->base))
833 		return 0;
834 
835 	drm_connector_list_iter_begin(&dev_priv->drm, &connector_list_iter);
836 	for_each_intel_connector_iter(connector_iter, &connector_list_iter) {
837 		struct intel_digital_connector_state *conn_iter_state;
838 		struct intel_crtc_state *crtc_state;
839 		struct intel_crtc *crtc;
840 
841 		if (connector_iter->mst_port != connector->mst_port ||
842 		    connector_iter == connector)
843 			continue;
844 
845 		conn_iter_state = intel_atomic_get_digital_connector_state(state,
846 									   connector_iter);
847 		if (IS_ERR(conn_iter_state)) {
848 			ret = PTR_ERR(conn_iter_state);
849 			break;
850 		}
851 
852 		if (!conn_iter_state->base.crtc)
853 			continue;
854 
855 		crtc = to_intel_crtc(conn_iter_state->base.crtc);
856 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
857 		if (IS_ERR(crtc_state)) {
858 			ret = PTR_ERR(crtc_state);
859 			break;
860 		}
861 
862 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
863 		if (ret)
864 			break;
865 		crtc_state->uapi.mode_changed = true;
866 	}
867 	drm_connector_list_iter_end(&connector_list_iter);
868 
869 	return ret;
870 }
871 
872 static int
873 intel_dp_mst_atomic_check(struct drm_connector *connector,
874 			  struct drm_atomic_state *_state)
875 {
876 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
877 	struct intel_connector *intel_connector =
878 		to_intel_connector(connector);
879 	int ret;
880 
881 	ret = intel_digital_connector_atomic_check(connector, &state->base);
882 	if (ret)
883 		return ret;
884 
885 	ret = intel_dp_mst_atomic_topology_check(intel_connector, state);
886 	if (ret)
887 		return ret;
888 
889 	if (intel_connector_needs_modeset(state, connector)) {
890 		ret = intel_dp_tunnel_atomic_check_state(state,
891 							 intel_connector->mst_port,
892 							 intel_connector);
893 		if (ret)
894 			return ret;
895 	}
896 
897 	return drm_dp_atomic_release_time_slots(&state->base,
898 						&intel_connector->mst_port->mst_mgr,
899 						intel_connector->port);
900 }
901 
902 static void clear_act_sent(struct intel_encoder *encoder,
903 			   const struct intel_crtc_state *crtc_state)
904 {
905 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
906 
907 	intel_de_write(i915, dp_tp_status_reg(encoder, crtc_state),
908 		       DP_TP_STATUS_ACT_SENT);
909 }
910 
911 static void wait_for_act_sent(struct intel_encoder *encoder,
912 			      const struct intel_crtc_state *crtc_state)
913 {
914 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
915 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
916 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
917 
918 	if (intel_de_wait_for_set(i915, dp_tp_status_reg(encoder, crtc_state),
919 				  DP_TP_STATUS_ACT_SENT, 1))
920 		drm_err(&i915->drm, "Timed out waiting for ACT sent\n");
921 
922 	drm_dp_check_act_status(&intel_dp->mst_mgr);
923 }
924 
925 static void intel_mst_disable_dp(struct intel_atomic_state *state,
926 				 struct intel_encoder *encoder,
927 				 const struct intel_crtc_state *old_crtc_state,
928 				 const struct drm_connector_state *old_conn_state)
929 {
930 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
931 	struct intel_digital_port *dig_port = intel_mst->primary;
932 	struct intel_dp *intel_dp = &dig_port->dp;
933 	struct intel_connector *connector =
934 		to_intel_connector(old_conn_state->connector);
935 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
936 
937 	drm_dbg_kms(&i915->drm, "active links %d\n",
938 		    intel_dp->active_mst_links);
939 
940 	intel_hdcp_disable(intel_mst->connector);
941 
942 	intel_dp_sink_disable_decompression(state, connector, old_crtc_state);
943 }
944 
945 static void intel_mst_post_disable_dp(struct intel_atomic_state *state,
946 				      struct intel_encoder *encoder,
947 				      const struct intel_crtc_state *old_crtc_state,
948 				      const struct drm_connector_state *old_conn_state)
949 {
950 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
951 	struct intel_digital_port *dig_port = intel_mst->primary;
952 	struct intel_dp *intel_dp = &dig_port->dp;
953 	struct intel_connector *connector =
954 		to_intel_connector(old_conn_state->connector);
955 	struct drm_dp_mst_topology_state *old_mst_state =
956 		drm_atomic_get_old_mst_topology_state(&state->base, &intel_dp->mst_mgr);
957 	struct drm_dp_mst_topology_state *new_mst_state =
958 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
959 	const struct drm_dp_mst_atomic_payload *old_payload =
960 		drm_atomic_get_mst_payload_state(old_mst_state, connector->port);
961 	struct drm_dp_mst_atomic_payload *new_payload =
962 		drm_atomic_get_mst_payload_state(new_mst_state, connector->port);
963 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
964 	struct intel_crtc *pipe_crtc;
965 	bool last_mst_stream;
966 
967 	intel_dp->active_mst_links--;
968 	last_mst_stream = intel_dp->active_mst_links == 0;
969 	drm_WARN_ON(&dev_priv->drm,
970 		    DISPLAY_VER(dev_priv) >= 12 && last_mst_stream &&
971 		    !intel_dp_mst_is_master_trans(old_crtc_state));
972 
973 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, pipe_crtc,
974 					 intel_crtc_joined_pipe_mask(old_crtc_state)) {
975 		const struct intel_crtc_state *old_pipe_crtc_state =
976 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
977 
978 		intel_crtc_vblank_off(old_pipe_crtc_state);
979 	}
980 
981 	intel_disable_transcoder(old_crtc_state);
982 
983 	drm_dp_remove_payload_part1(&intel_dp->mst_mgr, new_mst_state, new_payload);
984 
985 	clear_act_sent(encoder, old_crtc_state);
986 
987 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(old_crtc_state->cpu_transcoder),
988 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC, 0);
989 
990 	wait_for_act_sent(encoder, old_crtc_state);
991 
992 	drm_dp_remove_payload_part2(&intel_dp->mst_mgr, new_mst_state,
993 				    old_payload, new_payload);
994 
995 	intel_ddi_disable_transcoder_func(old_crtc_state);
996 
997 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, pipe_crtc,
998 					 intel_crtc_joined_pipe_mask(old_crtc_state)) {
999 		const struct intel_crtc_state *old_pipe_crtc_state =
1000 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
1001 
1002 		intel_dsc_disable(old_pipe_crtc_state);
1003 
1004 		if (DISPLAY_VER(dev_priv) >= 9)
1005 			skl_scaler_disable(old_pipe_crtc_state);
1006 		else
1007 			ilk_pfit_disable(old_pipe_crtc_state);
1008 	}
1009 
1010 	/*
1011 	 * Power down mst path before disabling the port, otherwise we end
1012 	 * up getting interrupts from the sink upon detecting link loss.
1013 	 */
1014 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
1015 				     false);
1016 
1017 	/*
1018 	 * BSpec 4287: disable DIP after the transcoder is disabled and before
1019 	 * the transcoder clock select is set to none.
1020 	 */
1021 	intel_dp_set_infoframes(&dig_port->base, false,
1022 				old_crtc_state, NULL);
1023 	/*
1024 	 * From TGL spec: "If multi-stream slave transcoder: Configure
1025 	 * Transcoder Clock Select to direct no clock to the transcoder"
1026 	 *
1027 	 * From older GENs spec: "Configure Transcoder Clock Select to direct
1028 	 * no clock to the transcoder"
1029 	 */
1030 	if (DISPLAY_VER(dev_priv) < 12 || !last_mst_stream)
1031 		intel_ddi_disable_transcoder_clock(old_crtc_state);
1032 
1033 
1034 	intel_mst->connector = NULL;
1035 	if (last_mst_stream)
1036 		dig_port->base.post_disable(state, &dig_port->base,
1037 						  old_crtc_state, NULL);
1038 
1039 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
1040 		    intel_dp->active_mst_links);
1041 }
1042 
1043 static void intel_mst_post_pll_disable_dp(struct intel_atomic_state *state,
1044 					  struct intel_encoder *encoder,
1045 					  const struct intel_crtc_state *old_crtc_state,
1046 					  const struct drm_connector_state *old_conn_state)
1047 {
1048 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1049 	struct intel_digital_port *dig_port = intel_mst->primary;
1050 	struct intel_dp *intel_dp = &dig_port->dp;
1051 
1052 	if (intel_dp->active_mst_links == 0 &&
1053 	    dig_port->base.post_pll_disable)
1054 		dig_port->base.post_pll_disable(state, encoder, old_crtc_state, old_conn_state);
1055 }
1056 
1057 static void intel_mst_pre_pll_enable_dp(struct intel_atomic_state *state,
1058 					struct intel_encoder *encoder,
1059 					const struct intel_crtc_state *pipe_config,
1060 					const struct drm_connector_state *conn_state)
1061 {
1062 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1063 	struct intel_digital_port *dig_port = intel_mst->primary;
1064 	struct intel_dp *intel_dp = &dig_port->dp;
1065 
1066 	if (intel_dp->active_mst_links == 0)
1067 		dig_port->base.pre_pll_enable(state, &dig_port->base,
1068 						    pipe_config, NULL);
1069 	else
1070 		/*
1071 		 * The port PLL state needs to get updated for secondary
1072 		 * streams as for the primary stream.
1073 		 */
1074 		intel_ddi_update_active_dpll(state, &dig_port->base,
1075 					     to_intel_crtc(pipe_config->uapi.crtc));
1076 }
1077 
1078 static void intel_mst_pre_enable_dp(struct intel_atomic_state *state,
1079 				    struct intel_encoder *encoder,
1080 				    const struct intel_crtc_state *pipe_config,
1081 				    const struct drm_connector_state *conn_state)
1082 {
1083 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1084 	struct intel_digital_port *dig_port = intel_mst->primary;
1085 	struct intel_dp *intel_dp = &dig_port->dp;
1086 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1087 	struct intel_connector *connector =
1088 		to_intel_connector(conn_state->connector);
1089 	struct drm_dp_mst_topology_state *mst_state =
1090 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
1091 	int ret;
1092 	bool first_mst_stream;
1093 
1094 	/* MST encoders are bound to a crtc, not to a connector,
1095 	 * force the mapping here for get_hw_state.
1096 	 */
1097 	connector->encoder = encoder;
1098 	intel_mst->connector = connector;
1099 	first_mst_stream = intel_dp->active_mst_links == 0;
1100 	drm_WARN_ON(&dev_priv->drm,
1101 		    DISPLAY_VER(dev_priv) >= 12 && first_mst_stream &&
1102 		    !intel_dp_mst_is_master_trans(pipe_config));
1103 
1104 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
1105 		    intel_dp->active_mst_links);
1106 
1107 	if (first_mst_stream)
1108 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
1109 
1110 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
1111 
1112 	intel_dp_sink_enable_decompression(state, connector, pipe_config);
1113 
1114 	if (first_mst_stream)
1115 		dig_port->base.pre_enable(state, &dig_port->base,
1116 						pipe_config, NULL);
1117 
1118 	intel_dp->active_mst_links++;
1119 
1120 	ret = drm_dp_add_payload_part1(&intel_dp->mst_mgr, mst_state,
1121 				       drm_atomic_get_mst_payload_state(mst_state, connector->port));
1122 	if (ret < 0)
1123 		drm_dbg_kms(&dev_priv->drm, "Failed to create MST payload for %s: %d\n",
1124 			    connector->base.name, ret);
1125 
1126 	/*
1127 	 * Before Gen 12 this is not done as part of
1128 	 * dig_port->base.pre_enable() and should be done here. For
1129 	 * Gen 12+ the step in which this should be done is different for the
1130 	 * first MST stream, so it's done on the DDI for the first stream and
1131 	 * here for the following ones.
1132 	 */
1133 	if (DISPLAY_VER(dev_priv) < 12 || !first_mst_stream)
1134 		intel_ddi_enable_transcoder_clock(encoder, pipe_config);
1135 
1136 	intel_dsc_dp_pps_write(&dig_port->base, pipe_config);
1137 	intel_ddi_set_dp_msa(pipe_config, conn_state);
1138 }
1139 
1140 static void enable_bs_jitter_was(const struct intel_crtc_state *crtc_state)
1141 {
1142 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1143 	u32 clear = 0;
1144 	u32 set = 0;
1145 
1146 	if (!IS_ALDERLAKE_P(i915))
1147 		return;
1148 
1149 	if (!IS_DISPLAY_STEP(i915, STEP_D0, STEP_FOREVER))
1150 		return;
1151 
1152 	/* Wa_14013163432:adlp */
1153 	if (crtc_state->fec_enable || intel_dp_is_uhbr(crtc_state))
1154 		set |= DP_MST_FEC_BS_JITTER_WA(crtc_state->cpu_transcoder);
1155 
1156 	/* Wa_14014143976:adlp */
1157 	if (IS_DISPLAY_STEP(i915, STEP_E0, STEP_FOREVER)) {
1158 		if (intel_dp_is_uhbr(crtc_state))
1159 			set |= DP_MST_SHORT_HBLANK_WA(crtc_state->cpu_transcoder);
1160 		else if (crtc_state->fec_enable)
1161 			clear |= DP_MST_SHORT_HBLANK_WA(crtc_state->cpu_transcoder);
1162 
1163 		if (crtc_state->fec_enable || intel_dp_is_uhbr(crtc_state))
1164 			set |= DP_MST_DPT_DPTP_ALIGN_WA(crtc_state->cpu_transcoder);
1165 	}
1166 
1167 	if (!clear && !set)
1168 		return;
1169 
1170 	intel_de_rmw(i915, CHICKEN_MISC_3, clear, set);
1171 }
1172 
1173 static void intel_mst_enable_dp(struct intel_atomic_state *state,
1174 				struct intel_encoder *encoder,
1175 				const struct intel_crtc_state *pipe_config,
1176 				const struct drm_connector_state *conn_state)
1177 {
1178 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1179 	struct intel_digital_port *dig_port = intel_mst->primary;
1180 	struct intel_dp *intel_dp = &dig_port->dp;
1181 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1182 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1183 	struct drm_dp_mst_topology_state *mst_state =
1184 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst_mgr);
1185 	enum transcoder trans = pipe_config->cpu_transcoder;
1186 	bool first_mst_stream = intel_dp->active_mst_links == 1;
1187 	struct intel_crtc *pipe_crtc;
1188 
1189 	drm_WARN_ON(&dev_priv->drm, pipe_config->has_pch_encoder);
1190 
1191 	if (intel_dp_is_uhbr(pipe_config)) {
1192 		const struct drm_display_mode *adjusted_mode =
1193 			&pipe_config->hw.adjusted_mode;
1194 		u64 crtc_clock_hz = KHz(adjusted_mode->crtc_clock);
1195 
1196 		intel_de_write(dev_priv, TRANS_DP2_VFREQHIGH(pipe_config->cpu_transcoder),
1197 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz >> 24));
1198 		intel_de_write(dev_priv, TRANS_DP2_VFREQLOW(pipe_config->cpu_transcoder),
1199 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz & 0xffffff));
1200 	}
1201 
1202 	enable_bs_jitter_was(pipe_config);
1203 
1204 	intel_ddi_enable_transcoder_func(encoder, pipe_config);
1205 
1206 	clear_act_sent(encoder, pipe_config);
1207 
1208 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(trans), 0,
1209 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
1210 
1211 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
1212 		    intel_dp->active_mst_links);
1213 
1214 	wait_for_act_sent(encoder, pipe_config);
1215 
1216 	if (first_mst_stream)
1217 		intel_ddi_wait_for_fec_status(encoder, pipe_config, true);
1218 
1219 	drm_dp_add_payload_part2(&intel_dp->mst_mgr, &state->base,
1220 				 drm_atomic_get_mst_payload_state(mst_state, connector->port));
1221 
1222 	if (DISPLAY_VER(dev_priv) >= 12)
1223 		intel_de_rmw(dev_priv, hsw_chicken_trans_reg(dev_priv, trans),
1224 			     FECSTALL_DIS_DPTSTREAM_DPTTG,
1225 			     pipe_config->fec_enable ? FECSTALL_DIS_DPTSTREAM_DPTTG : 0);
1226 
1227 	intel_audio_sdp_split_update(pipe_config);
1228 
1229 	intel_enable_transcoder(pipe_config);
1230 
1231 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1232 						 intel_crtc_joined_pipe_mask(pipe_config)) {
1233 		const struct intel_crtc_state *pipe_crtc_state =
1234 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1235 
1236 		intel_crtc_vblank_on(pipe_crtc_state);
1237 	}
1238 
1239 	intel_hdcp_enable(state, encoder, pipe_config, conn_state);
1240 }
1241 
1242 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
1243 				      enum pipe *pipe)
1244 {
1245 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1246 	*pipe = intel_mst->pipe;
1247 	if (intel_mst->connector)
1248 		return true;
1249 	return false;
1250 }
1251 
1252 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
1253 					struct intel_crtc_state *pipe_config)
1254 {
1255 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1256 	struct intel_digital_port *dig_port = intel_mst->primary;
1257 
1258 	dig_port->base.get_config(&dig_port->base, pipe_config);
1259 }
1260 
1261 static bool intel_dp_mst_initial_fastset_check(struct intel_encoder *encoder,
1262 					       struct intel_crtc_state *crtc_state)
1263 {
1264 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1265 	struct intel_digital_port *dig_port = intel_mst->primary;
1266 
1267 	return intel_dp_initial_fastset_check(&dig_port->base, crtc_state);
1268 }
1269 
1270 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
1271 {
1272 	struct intel_connector *intel_connector = to_intel_connector(connector);
1273 	struct drm_i915_private *i915 = to_i915(intel_connector->base.dev);
1274 	struct intel_dp *intel_dp = intel_connector->mst_port;
1275 	const struct drm_edid *drm_edid;
1276 	int ret;
1277 
1278 	if (drm_connector_is_unregistered(connector))
1279 		return intel_connector_update_modes(connector, NULL);
1280 
1281 	if (!intel_display_driver_check_access(i915))
1282 		return drm_edid_connector_add_modes(connector);
1283 
1284 	drm_edid = drm_dp_mst_edid_read(connector, &intel_dp->mst_mgr, intel_connector->port);
1285 
1286 	ret = intel_connector_update_modes(connector, drm_edid);
1287 
1288 	drm_edid_free(drm_edid);
1289 
1290 	return ret;
1291 }
1292 
1293 static int
1294 intel_dp_mst_connector_late_register(struct drm_connector *connector)
1295 {
1296 	struct intel_connector *intel_connector = to_intel_connector(connector);
1297 	int ret;
1298 
1299 	ret = drm_dp_mst_connector_late_register(connector,
1300 						 intel_connector->port);
1301 	if (ret < 0)
1302 		return ret;
1303 
1304 	ret = intel_connector_register(connector);
1305 	if (ret < 0)
1306 		drm_dp_mst_connector_early_unregister(connector,
1307 						      intel_connector->port);
1308 
1309 	return ret;
1310 }
1311 
1312 static void
1313 intel_dp_mst_connector_early_unregister(struct drm_connector *connector)
1314 {
1315 	struct intel_connector *intel_connector = to_intel_connector(connector);
1316 
1317 	intel_connector_unregister(connector);
1318 	drm_dp_mst_connector_early_unregister(connector,
1319 					      intel_connector->port);
1320 }
1321 
1322 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
1323 	.fill_modes = drm_helper_probe_single_connector_modes,
1324 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1325 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1326 	.late_register = intel_dp_mst_connector_late_register,
1327 	.early_unregister = intel_dp_mst_connector_early_unregister,
1328 	.destroy = intel_connector_destroy,
1329 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1330 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1331 };
1332 
1333 static int intel_dp_mst_get_modes(struct drm_connector *connector)
1334 {
1335 	return intel_dp_mst_get_ddc_modes(connector);
1336 }
1337 
1338 static int
1339 intel_dp_mst_mode_valid_ctx(struct drm_connector *connector,
1340 			    struct drm_display_mode *mode,
1341 			    struct drm_modeset_acquire_ctx *ctx,
1342 			    enum drm_mode_status *status)
1343 {
1344 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
1345 	struct intel_connector *intel_connector = to_intel_connector(connector);
1346 	struct intel_dp *intel_dp = intel_connector->mst_port;
1347 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
1348 	struct drm_dp_mst_port *port = intel_connector->port;
1349 	const int min_bpp = 18;
1350 	int max_dotclk = to_i915(connector->dev)->display.cdclk.max_dotclk_freq;
1351 	int max_rate, mode_rate, max_lanes, max_link_clock;
1352 	int ret;
1353 	bool dsc = false, bigjoiner = false;
1354 	u16 dsc_max_compressed_bpp = 0;
1355 	u8 dsc_slice_count = 0;
1356 	int target_clock = mode->clock;
1357 
1358 	if (drm_connector_is_unregistered(connector)) {
1359 		*status = MODE_ERROR;
1360 		return 0;
1361 	}
1362 
1363 	*status = intel_cpu_transcoder_mode_valid(dev_priv, mode);
1364 	if (*status != MODE_OK)
1365 		return 0;
1366 
1367 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1368 		*status = MODE_H_ILLEGAL;
1369 		return 0;
1370 	}
1371 
1372 	if (mode->clock < 10000) {
1373 		*status = MODE_CLOCK_LOW;
1374 		return 0;
1375 	}
1376 
1377 	max_link_clock = intel_dp_max_link_rate(intel_dp);
1378 	max_lanes = intel_dp_max_lane_count(intel_dp);
1379 
1380 	max_rate = intel_dp_max_link_data_rate(intel_dp,
1381 					       max_link_clock, max_lanes);
1382 	mode_rate = intel_dp_link_required(mode->clock, min_bpp);
1383 
1384 	/*
1385 	 * TODO:
1386 	 * - Also check if compression would allow for the mode
1387 	 * - Calculate the overhead using drm_dp_bw_overhead() /
1388 	 *   drm_dp_bw_channel_coding_efficiency(), similarly to the
1389 	 *   compute config code, as drm_dp_calc_pbn_mode() doesn't
1390 	 *   account with all the overheads.
1391 	 * - Check here and during compute config the BW reported by
1392 	 *   DFP_Link_Available_Payload_Bandwidth_Number (or the
1393 	 *   corresponding link capabilities of the sink) in case the
1394 	 *   stream is uncompressed for it by the last branch device.
1395 	 */
1396 	if (intel_dp_need_bigjoiner(intel_dp, intel_connector,
1397 				    mode->hdisplay, target_clock)) {
1398 		bigjoiner = true;
1399 		max_dotclk *= 2;
1400 	}
1401 
1402 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
1403 	if (ret)
1404 		return ret;
1405 
1406 	if (mode_rate > max_rate || mode->clock > max_dotclk ||
1407 	    drm_dp_calc_pbn_mode(mode->clock, min_bpp << 4) > port->full_pbn) {
1408 		*status = MODE_CLOCK_HIGH;
1409 		return 0;
1410 	}
1411 
1412 	if (HAS_DSC_MST(dev_priv) &&
1413 	    drm_dp_sink_supports_dsc(intel_connector->dp.dsc_dpcd)) {
1414 		/*
1415 		 * TBD pass the connector BPC,
1416 		 * for now U8_MAX so that max BPC on that platform would be picked
1417 		 */
1418 		int pipe_bpp = intel_dp_dsc_compute_max_bpp(intel_connector, U8_MAX);
1419 
1420 		if (drm_dp_sink_supports_fec(intel_connector->dp.fec_capability)) {
1421 			dsc_max_compressed_bpp =
1422 				intel_dp_dsc_get_max_compressed_bpp(dev_priv,
1423 								    max_link_clock,
1424 								    max_lanes,
1425 								    target_clock,
1426 								    mode->hdisplay,
1427 								    bigjoiner,
1428 								    INTEL_OUTPUT_FORMAT_RGB,
1429 								    pipe_bpp, 64);
1430 			dsc_slice_count =
1431 				intel_dp_dsc_get_slice_count(intel_connector,
1432 							     target_clock,
1433 							     mode->hdisplay,
1434 							     bigjoiner);
1435 		}
1436 
1437 		dsc = dsc_max_compressed_bpp && dsc_slice_count;
1438 	}
1439 
1440 	if (intel_dp_joiner_needs_dsc(dev_priv, bigjoiner) && !dsc) {
1441 		*status = MODE_CLOCK_HIGH;
1442 		return 0;
1443 	}
1444 
1445 	if (mode_rate > max_rate && !dsc) {
1446 		*status = MODE_CLOCK_HIGH;
1447 		return 0;
1448 	}
1449 
1450 	*status = intel_mode_valid_max_plane_size(dev_priv, mode, bigjoiner);
1451 	return 0;
1452 }
1453 
1454 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
1455 							 struct drm_atomic_state *state)
1456 {
1457 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
1458 											 connector);
1459 	struct intel_connector *intel_connector = to_intel_connector(connector);
1460 	struct intel_dp *intel_dp = intel_connector->mst_port;
1461 	struct intel_crtc *crtc = to_intel_crtc(connector_state->crtc);
1462 
1463 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
1464 }
1465 
1466 static int
1467 intel_dp_mst_detect(struct drm_connector *connector,
1468 		    struct drm_modeset_acquire_ctx *ctx, bool force)
1469 {
1470 	struct drm_i915_private *i915 = to_i915(connector->dev);
1471 	struct intel_connector *intel_connector = to_intel_connector(connector);
1472 	struct intel_dp *intel_dp = intel_connector->mst_port;
1473 
1474 	if (!intel_display_device_enabled(i915))
1475 		return connector_status_disconnected;
1476 
1477 	if (drm_connector_is_unregistered(connector))
1478 		return connector_status_disconnected;
1479 
1480 	if (!intel_display_driver_check_access(i915))
1481 		return connector->status;
1482 
1483 	return drm_dp_mst_detect_port(connector, ctx, &intel_dp->mst_mgr,
1484 				      intel_connector->port);
1485 }
1486 
1487 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
1488 	.get_modes = intel_dp_mst_get_modes,
1489 	.mode_valid_ctx = intel_dp_mst_mode_valid_ctx,
1490 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
1491 	.atomic_check = intel_dp_mst_atomic_check,
1492 	.detect_ctx = intel_dp_mst_detect,
1493 };
1494 
1495 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
1496 {
1497 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(to_intel_encoder(encoder));
1498 
1499 	drm_encoder_cleanup(encoder);
1500 	kfree(intel_mst);
1501 }
1502 
1503 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
1504 	.destroy = intel_dp_mst_encoder_destroy,
1505 };
1506 
1507 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
1508 {
1509 	if (intel_attached_encoder(connector) && connector->base.state->crtc) {
1510 		enum pipe pipe;
1511 		if (!intel_attached_encoder(connector)->get_hw_state(intel_attached_encoder(connector), &pipe))
1512 			return false;
1513 		return true;
1514 	}
1515 	return false;
1516 }
1517 
1518 static int intel_dp_mst_add_properties(struct intel_dp *intel_dp,
1519 				       struct drm_connector *connector,
1520 				       const char *pathprop)
1521 {
1522 	struct drm_i915_private *i915 = to_i915(connector->dev);
1523 
1524 	drm_object_attach_property(&connector->base,
1525 				   i915->drm.mode_config.path_property, 0);
1526 	drm_object_attach_property(&connector->base,
1527 				   i915->drm.mode_config.tile_property, 0);
1528 
1529 	intel_attach_force_audio_property(connector);
1530 	intel_attach_broadcast_rgb_property(connector);
1531 
1532 	/*
1533 	 * Reuse the prop from the SST connector because we're
1534 	 * not allowed to create new props after device registration.
1535 	 */
1536 	connector->max_bpc_property =
1537 		intel_dp->attached_connector->base.max_bpc_property;
1538 	if (connector->max_bpc_property)
1539 		drm_connector_attach_max_bpc_property(connector, 6, 12);
1540 
1541 	return drm_connector_set_path_property(connector, pathprop);
1542 }
1543 
1544 static void
1545 intel_dp_mst_read_decompression_port_dsc_caps(struct intel_dp *intel_dp,
1546 					      struct intel_connector *connector)
1547 {
1548 	u8 dpcd_caps[DP_RECEIVER_CAP_SIZE];
1549 
1550 	if (!connector->dp.dsc_decompression_aux)
1551 		return;
1552 
1553 	if (drm_dp_read_dpcd_caps(connector->dp.dsc_decompression_aux, dpcd_caps) < 0)
1554 		return;
1555 
1556 	intel_dp_get_dsc_sink_cap(dpcd_caps[DP_DPCD_REV], connector);
1557 }
1558 
1559 static bool detect_dsc_hblank_expansion_quirk(const struct intel_connector *connector)
1560 {
1561 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1562 	struct drm_dp_desc desc;
1563 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
1564 
1565 	if (!connector->dp.dsc_decompression_aux)
1566 		return false;
1567 
1568 	if (drm_dp_read_desc(connector->dp.dsc_decompression_aux,
1569 			     &desc, true) < 0)
1570 		return false;
1571 
1572 	if (!drm_dp_has_quirk(&desc,
1573 			      DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC))
1574 		return false;
1575 
1576 	if (drm_dp_read_dpcd_caps(connector->dp.dsc_decompression_aux, dpcd) < 0)
1577 		return false;
1578 
1579 	if (!(dpcd[DP_RECEIVE_PORT_0_CAP_0] & DP_HBLANK_EXPANSION_CAPABLE))
1580 		return false;
1581 
1582 	drm_dbg_kms(&i915->drm,
1583 		    "[CONNECTOR:%d:%s] DSC HBLANK expansion quirk detected\n",
1584 		    connector->base.base.id, connector->base.name);
1585 
1586 	return true;
1587 }
1588 
1589 static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr,
1590 							struct drm_dp_mst_port *port,
1591 							const char *pathprop)
1592 {
1593 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
1594 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1595 	struct drm_device *dev = dig_port->base.base.dev;
1596 	struct drm_i915_private *dev_priv = to_i915(dev);
1597 	struct intel_connector *intel_connector;
1598 	struct drm_connector *connector;
1599 	enum pipe pipe;
1600 	int ret;
1601 
1602 	intel_connector = intel_connector_alloc();
1603 	if (!intel_connector)
1604 		return NULL;
1605 
1606 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
1607 	intel_connector->sync_state = intel_dp_connector_sync_state;
1608 	intel_connector->mst_port = intel_dp;
1609 	intel_connector->port = port;
1610 	drm_dp_mst_get_port_malloc(port);
1611 
1612 	intel_dp_init_modeset_retry_work(intel_connector);
1613 
1614 	intel_connector->dp.dsc_decompression_aux = drm_dp_mst_dsc_aux_for_port(port);
1615 	intel_dp_mst_read_decompression_port_dsc_caps(intel_dp, intel_connector);
1616 	intel_connector->dp.dsc_hblank_expansion_quirk =
1617 		detect_dsc_hblank_expansion_quirk(intel_connector);
1618 
1619 	connector = &intel_connector->base;
1620 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
1621 				 DRM_MODE_CONNECTOR_DisplayPort);
1622 	if (ret) {
1623 		drm_dp_mst_put_port_malloc(port);
1624 		intel_connector_free(intel_connector);
1625 		return NULL;
1626 	}
1627 
1628 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
1629 
1630 	for_each_pipe(dev_priv, pipe) {
1631 		struct drm_encoder *enc =
1632 			&intel_dp->mst_encoders[pipe]->base.base;
1633 
1634 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
1635 		if (ret)
1636 			goto err;
1637 	}
1638 
1639 	ret = intel_dp_mst_add_properties(intel_dp, connector, pathprop);
1640 	if (ret)
1641 		goto err;
1642 
1643 	ret = intel_dp_hdcp_init(dig_port, intel_connector);
1644 	if (ret)
1645 		drm_dbg_kms(&dev_priv->drm, "[%s:%d] HDCP MST init failed, skipping.\n",
1646 			    connector->name, connector->base.id);
1647 
1648 	return connector;
1649 
1650 err:
1651 	drm_connector_cleanup(connector);
1652 	return NULL;
1653 }
1654 
1655 static void
1656 intel_dp_mst_poll_hpd_irq(struct drm_dp_mst_topology_mgr *mgr)
1657 {
1658 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
1659 
1660 	intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
1661 }
1662 
1663 static const struct drm_dp_mst_topology_cbs mst_cbs = {
1664 	.add_connector = intel_dp_add_mst_connector,
1665 	.poll_hpd_irq = intel_dp_mst_poll_hpd_irq,
1666 };
1667 
1668 static struct intel_dp_mst_encoder *
1669 intel_dp_create_fake_mst_encoder(struct intel_digital_port *dig_port, enum pipe pipe)
1670 {
1671 	struct intel_dp_mst_encoder *intel_mst;
1672 	struct intel_encoder *intel_encoder;
1673 	struct drm_device *dev = dig_port->base.base.dev;
1674 
1675 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
1676 
1677 	if (!intel_mst)
1678 		return NULL;
1679 
1680 	intel_mst->pipe = pipe;
1681 	intel_encoder = &intel_mst->base;
1682 	intel_mst->primary = dig_port;
1683 
1684 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
1685 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
1686 
1687 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
1688 	intel_encoder->power_domain = dig_port->base.power_domain;
1689 	intel_encoder->port = dig_port->base.port;
1690 	intel_encoder->cloneable = 0;
1691 	/*
1692 	 * This is wrong, but broken userspace uses the intersection
1693 	 * of possible_crtcs of all the encoders of a given connector
1694 	 * to figure out which crtcs can drive said connector. What
1695 	 * should be used instead is the union of possible_crtcs.
1696 	 * To keep such userspace functioning we must misconfigure
1697 	 * this to make sure the intersection is not empty :(
1698 	 */
1699 	intel_encoder->pipe_mask = ~0;
1700 
1701 	intel_encoder->compute_config = intel_dp_mst_compute_config;
1702 	intel_encoder->compute_config_late = intel_dp_mst_compute_config_late;
1703 	intel_encoder->disable = intel_mst_disable_dp;
1704 	intel_encoder->post_disable = intel_mst_post_disable_dp;
1705 	intel_encoder->post_pll_disable = intel_mst_post_pll_disable_dp;
1706 	intel_encoder->update_pipe = intel_ddi_update_pipe;
1707 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
1708 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
1709 	intel_encoder->enable = intel_mst_enable_dp;
1710 	intel_encoder->audio_enable = intel_audio_codec_enable;
1711 	intel_encoder->audio_disable = intel_audio_codec_disable;
1712 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
1713 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
1714 	intel_encoder->initial_fastset_check = intel_dp_mst_initial_fastset_check;
1715 
1716 	return intel_mst;
1717 
1718 }
1719 
1720 static bool
1721 intel_dp_create_fake_mst_encoders(struct intel_digital_port *dig_port)
1722 {
1723 	struct intel_dp *intel_dp = &dig_port->dp;
1724 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
1725 	enum pipe pipe;
1726 
1727 	for_each_pipe(dev_priv, pipe)
1728 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(dig_port, pipe);
1729 	return true;
1730 }
1731 
1732 int
1733 intel_dp_mst_encoder_active_links(struct intel_digital_port *dig_port)
1734 {
1735 	return dig_port->dp.active_mst_links;
1736 }
1737 
1738 int
1739 intel_dp_mst_encoder_init(struct intel_digital_port *dig_port, int conn_base_id)
1740 {
1741 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1742 	struct intel_dp *intel_dp = &dig_port->dp;
1743 	enum port port = dig_port->base.port;
1744 	int ret;
1745 
1746 	if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
1747 		return 0;
1748 
1749 	if (DISPLAY_VER(i915) < 12 && port == PORT_A)
1750 		return 0;
1751 
1752 	if (DISPLAY_VER(i915) < 11 && port == PORT_E)
1753 		return 0;
1754 
1755 	intel_dp->mst_mgr.cbs = &mst_cbs;
1756 
1757 	/* create encoders */
1758 	intel_dp_create_fake_mst_encoders(dig_port);
1759 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
1760 					   &intel_dp->aux, 16, 3, conn_base_id);
1761 	if (ret) {
1762 		intel_dp->mst_mgr.cbs = NULL;
1763 		return ret;
1764 	}
1765 
1766 	return 0;
1767 }
1768 
1769 bool intel_dp_mst_source_support(struct intel_dp *intel_dp)
1770 {
1771 	return intel_dp->mst_mgr.cbs;
1772 }
1773 
1774 void
1775 intel_dp_mst_encoder_cleanup(struct intel_digital_port *dig_port)
1776 {
1777 	struct intel_dp *intel_dp = &dig_port->dp;
1778 
1779 	if (!intel_dp_mst_source_support(intel_dp))
1780 		return;
1781 
1782 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
1783 	/* encoders will get killed by normal cleanup */
1784 
1785 	intel_dp->mst_mgr.cbs = NULL;
1786 }
1787 
1788 bool intel_dp_mst_is_master_trans(const struct intel_crtc_state *crtc_state)
1789 {
1790 	return crtc_state->mst_master_transcoder == crtc_state->cpu_transcoder;
1791 }
1792 
1793 bool intel_dp_mst_is_slave_trans(const struct intel_crtc_state *crtc_state)
1794 {
1795 	return crtc_state->mst_master_transcoder != INVALID_TRANSCODER &&
1796 	       crtc_state->mst_master_transcoder != crtc_state->cpu_transcoder;
1797 }
1798 
1799 /**
1800  * intel_dp_mst_add_topology_state_for_connector - add MST topology state for a connector
1801  * @state: atomic state
1802  * @connector: connector to add the state for
1803  * @crtc: the CRTC @connector is attached to
1804  *
1805  * Add the MST topology state for @connector to @state.
1806  *
1807  * Returns 0 on success, negative error code on failure.
1808  */
1809 static int
1810 intel_dp_mst_add_topology_state_for_connector(struct intel_atomic_state *state,
1811 					      struct intel_connector *connector,
1812 					      struct intel_crtc *crtc)
1813 {
1814 	struct drm_dp_mst_topology_state *mst_state;
1815 
1816 	if (!connector->mst_port)
1817 		return 0;
1818 
1819 	mst_state = drm_atomic_get_mst_topology_state(&state->base,
1820 						      &connector->mst_port->mst_mgr);
1821 	if (IS_ERR(mst_state))
1822 		return PTR_ERR(mst_state);
1823 
1824 	mst_state->pending_crtc_mask |= drm_crtc_mask(&crtc->base);
1825 
1826 	return 0;
1827 }
1828 
1829 /**
1830  * intel_dp_mst_add_topology_state_for_crtc - add MST topology state for a CRTC
1831  * @state: atomic state
1832  * @crtc: CRTC to add the state for
1833  *
1834  * Add the MST topology state for @crtc to @state.
1835  *
1836  * Returns 0 on success, negative error code on failure.
1837  */
1838 int intel_dp_mst_add_topology_state_for_crtc(struct intel_atomic_state *state,
1839 					     struct intel_crtc *crtc)
1840 {
1841 	struct drm_connector *_connector;
1842 	struct drm_connector_state *conn_state;
1843 	int i;
1844 
1845 	for_each_new_connector_in_state(&state->base, _connector, conn_state, i) {
1846 		struct intel_connector *connector = to_intel_connector(_connector);
1847 		int ret;
1848 
1849 		if (conn_state->crtc != &crtc->base)
1850 			continue;
1851 
1852 		ret = intel_dp_mst_add_topology_state_for_connector(state, connector, crtc);
1853 		if (ret)
1854 			return ret;
1855 	}
1856 
1857 	return 0;
1858 }
1859 
1860 static struct intel_connector *
1861 get_connector_in_state_for_crtc(struct intel_atomic_state *state,
1862 				const struct intel_crtc *crtc)
1863 {
1864 	struct drm_connector_state *old_conn_state;
1865 	struct drm_connector_state *new_conn_state;
1866 	struct drm_connector *_connector;
1867 	int i;
1868 
1869 	for_each_oldnew_connector_in_state(&state->base, _connector,
1870 					   old_conn_state, new_conn_state, i) {
1871 		struct intel_connector *connector =
1872 			to_intel_connector(_connector);
1873 
1874 		if (old_conn_state->crtc == &crtc->base ||
1875 		    new_conn_state->crtc == &crtc->base)
1876 			return connector;
1877 	}
1878 
1879 	return NULL;
1880 }
1881 
1882 /**
1883  * intel_dp_mst_crtc_needs_modeset - check if changes in topology need to modeset the given CRTC
1884  * @state: atomic state
1885  * @crtc: CRTC for which to check the modeset requirement
1886  *
1887  * Check if any change in a MST topology requires a forced modeset on @crtc in
1888  * this topology. One such change is enabling/disabling the DSC decompression
1889  * state in the first branch device's UFP DPCD as required by one CRTC, while
1890  * the other @crtc in the same topology is still active, requiring a full modeset
1891  * on @crtc.
1892  */
1893 bool intel_dp_mst_crtc_needs_modeset(struct intel_atomic_state *state,
1894 				     struct intel_crtc *crtc)
1895 {
1896 	const struct intel_connector *crtc_connector;
1897 	const struct drm_connector_state *conn_state;
1898 	const struct drm_connector *_connector;
1899 	int i;
1900 
1901 	if (!intel_crtc_has_type(intel_atomic_get_new_crtc_state(state, crtc),
1902 				 INTEL_OUTPUT_DP_MST))
1903 		return false;
1904 
1905 	crtc_connector = get_connector_in_state_for_crtc(state, crtc);
1906 
1907 	if (!crtc_connector)
1908 		/* None of the connectors in the topology needs modeset */
1909 		return false;
1910 
1911 	for_each_new_connector_in_state(&state->base, _connector, conn_state, i) {
1912 		const struct intel_connector *connector =
1913 			to_intel_connector(_connector);
1914 		const struct intel_crtc_state *new_crtc_state;
1915 		const struct intel_crtc_state *old_crtc_state;
1916 		struct intel_crtc *crtc_iter;
1917 
1918 		if (connector->mst_port != crtc_connector->mst_port ||
1919 		    !conn_state->crtc)
1920 			continue;
1921 
1922 		crtc_iter = to_intel_crtc(conn_state->crtc);
1923 
1924 		new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc_iter);
1925 		old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc_iter);
1926 
1927 		if (!intel_crtc_needs_modeset(new_crtc_state))
1928 			continue;
1929 
1930 		if (old_crtc_state->dsc.compression_enable ==
1931 		    new_crtc_state->dsc.compression_enable)
1932 			continue;
1933 		/*
1934 		 * Toggling the decompression flag because of this stream in
1935 		 * the first downstream branch device's UFP DPCD may reset the
1936 		 * whole branch device. To avoid the reset while other streams
1937 		 * are also active modeset the whole MST topology in this
1938 		 * case.
1939 		 */
1940 		if (connector->dp.dsc_decompression_aux ==
1941 		    &connector->mst_port->aux)
1942 			return true;
1943 	}
1944 
1945 	return false;
1946 }
1947