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