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