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