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