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