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