xref: /linux/drivers/gpu/drm/i915/display/intel_dp_mst.c (revision 6812ce4e4379ffc99c52401ec28f0d7ffbc36206)
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 	for_each_joiner_candidate(connector, adjusted_mode, num_joined_pipes) {
769 		if (num_joined_pipes > 1)
770 			pipe_config->joiner_pipes = GENMASK(crtc->pipe + num_joined_pipes - 1,
771 							    crtc->pipe);
772 
773 		ret = mst_stream_compute_link_for_joined_pipes(encoder,
774 							       pipe_config,
775 							       conn_state,
776 							       num_joined_pipes);
777 		if (ret == 0 || ret == -EDEADLK)
778 			break;
779 	}
780 
781 	if (ret)
782 		return ret;
783 
784 	pipe_config->limited_color_range =
785 		intel_dp_limited_color_range(pipe_config, conn_state);
786 
787 	if (display->platform.geminilake || display->platform.broxton)
788 		pipe_config->lane_lat_optim_mask =
789 			bxt_dpio_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
790 
791 	intel_vrr_compute_config(pipe_config, conn_state);
792 
793 	intel_dp_audio_compute_config(encoder, pipe_config, conn_state);
794 
795 	intel_ddi_compute_min_voltage_level(pipe_config);
796 
797 	intel_psr_compute_config(intel_dp, pipe_config, conn_state);
798 
799 	return intel_dp_tunnel_atomic_compute_stream_bw(state, intel_dp, connector,
800 							pipe_config);
801 }
802 
803 /*
804  * Iterate over all connectors and return a mask of
805  * all CPU transcoders streaming over the same DP link.
806  */
807 static unsigned int
intel_dp_mst_transcoder_mask(struct intel_atomic_state * state,struct intel_dp * mst_port)808 intel_dp_mst_transcoder_mask(struct intel_atomic_state *state,
809 			     struct intel_dp *mst_port)
810 {
811 	struct intel_display *display = to_intel_display(state);
812 	const struct intel_digital_connector_state *conn_state;
813 	struct intel_connector *connector;
814 	u16 transcoders = 0;
815 	int i;
816 
817 	if (DISPLAY_VER(display) < 12)
818 		return 0;
819 
820 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
821 		const struct intel_crtc_state *crtc_state;
822 		struct intel_crtc *crtc;
823 
824 		if (connector->mst.dp != mst_port || !conn_state->base.crtc)
825 			continue;
826 
827 		crtc = to_intel_crtc(conn_state->base.crtc);
828 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
829 
830 		if (!crtc_state->hw.active)
831 			continue;
832 
833 		transcoders |= BIT(crtc_state->cpu_transcoder);
834 	}
835 
836 	return transcoders;
837 }
838 
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)839 static u8 get_pipes_downstream_of_mst_port(struct intel_atomic_state *state,
840 					   struct drm_dp_mst_topology_mgr *mst_mgr,
841 					   struct drm_dp_mst_port *parent_port)
842 {
843 	const struct intel_digital_connector_state *conn_state;
844 	struct intel_connector *connector;
845 	u8 mask = 0;
846 	int i;
847 
848 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
849 		if (!conn_state->base.crtc)
850 			continue;
851 
852 		if (&connector->mst.dp->mst.mgr != mst_mgr)
853 			continue;
854 
855 		if (parent_port &&
856 		    connector->mst.port != parent_port &&
857 		    !drm_dp_mst_port_downstream_of_parent(mst_mgr,
858 							  connector->mst.port,
859 							  parent_port))
860 			continue;
861 
862 		mask |= BIT(to_intel_crtc(conn_state->base.crtc)->pipe);
863 	}
864 
865 	return mask;
866 }
867 
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)868 static int intel_dp_mst_check_dsc_change(struct intel_atomic_state *state,
869 					 struct drm_dp_mst_topology_mgr *mst_mgr,
870 					 struct intel_link_bw_limits *limits)
871 {
872 	struct intel_display *display = to_intel_display(state);
873 	struct intel_crtc *crtc;
874 	u8 mst_pipe_mask;
875 	u8 dsc_pipe_mask = 0;
876 	int ret;
877 
878 	mst_pipe_mask = get_pipes_downstream_of_mst_port(state, mst_mgr, NULL);
879 
880 	for_each_intel_crtc_in_pipe_mask(display, crtc, mst_pipe_mask) {
881 		struct intel_crtc_state *crtc_state =
882 			intel_atomic_get_new_crtc_state(state, crtc);
883 
884 		/* Atomic connector check should've added all the MST CRTCs. */
885 		if (drm_WARN_ON(display->drm, !crtc_state))
886 			return -EINVAL;
887 
888 		if (intel_dsc_enabled_on_link(crtc_state))
889 			dsc_pipe_mask |= BIT(crtc->pipe);
890 	}
891 
892 	if (!dsc_pipe_mask || mst_pipe_mask == dsc_pipe_mask)
893 		return 0;
894 
895 	limits->link_dsc_pipes |= mst_pipe_mask;
896 
897 	ret = intel_modeset_pipes_in_mask_early(state, "MST DSC",
898 						mst_pipe_mask);
899 
900 	return ret ? : -EAGAIN;
901 }
902 
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)903 static int intel_dp_mst_check_bw(struct intel_atomic_state *state,
904 				 struct drm_dp_mst_topology_mgr *mst_mgr,
905 				 struct drm_dp_mst_topology_state *mst_state,
906 				 struct intel_link_bw_limits *limits)
907 {
908 	struct drm_dp_mst_port *mst_port;
909 	u8 mst_port_pipes;
910 	int ret;
911 
912 	ret = drm_dp_mst_atomic_check_mgr(&state->base, mst_mgr, mst_state, &mst_port);
913 	if (ret != -ENOSPC)
914 		return ret;
915 
916 	mst_port_pipes = get_pipes_downstream_of_mst_port(state, mst_mgr, mst_port);
917 
918 	ret = intel_link_bw_reduce_bpp(state, limits,
919 				       mst_port_pipes, "MST link BW");
920 
921 	return ret ? : -EAGAIN;
922 }
923 
924 /**
925  * intel_dp_mst_atomic_check_link - check all modeset MST link configuration
926  * @state: intel atomic state
927  * @limits: link BW limits
928  *
929  * Check the link configuration for all modeset MST outputs. If the
930  * configuration is invalid @limits will be updated if possible to
931  * reduce the total BW, after which the configuration for all CRTCs in
932  * @state must be recomputed with the updated @limits.
933  *
934  * Returns:
935  *   - 0 if the configuration is valid
936  *   - %-EAGAIN, if the configuration is invalid and @limits got updated
937  *     with fallback values with which the configuration of all CRTCs in
938  *     @state must be recomputed
939  *   - Other negative error, if the configuration is invalid without a
940  *     fallback possibility, or the check failed for another reason
941  */
intel_dp_mst_atomic_check_link(struct intel_atomic_state * state,struct intel_link_bw_limits * limits)942 int intel_dp_mst_atomic_check_link(struct intel_atomic_state *state,
943 				   struct intel_link_bw_limits *limits)
944 {
945 	struct drm_dp_mst_topology_mgr *mgr;
946 	struct drm_dp_mst_topology_state *mst_state;
947 	int ret;
948 	int i;
949 
950 	for_each_new_mst_mgr_in_state(&state->base, mgr, mst_state, i) {
951 		ret = intel_dp_mst_check_dsc_change(state, mgr, limits);
952 		if (ret)
953 			return ret;
954 
955 		ret = intel_dp_mst_check_bw(state, mgr, mst_state,
956 					    limits);
957 		if (ret)
958 			return ret;
959 	}
960 
961 	return 0;
962 }
963 
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)964 static int mst_stream_compute_config_late(struct intel_atomic_state *state,
965 					  struct intel_encoder *encoder,
966 					  struct intel_crtc_state *crtc_state,
967 					  struct drm_connector_state *conn_state)
968 {
969 	struct intel_dp *intel_dp = to_primary_dp(encoder);
970 
971 	/* lowest numbered transcoder will be designated master */
972 	crtc_state->mst_master_transcoder =
973 		ffs(intel_dp_mst_transcoder_mask(state, intel_dp)) - 1;
974 
975 	return 0;
976 }
977 
978 /*
979  * If one of the connectors in a MST stream needs a modeset, mark all CRTCs
980  * that shares the same MST stream as mode changed,
981  * intel_modeset_pipe_config()+intel_crtc_check_fastset() will take care to do
982  * a fastset when possible.
983  *
984  * On TGL+ this is required since each stream go through a master transcoder,
985  * so if the master transcoder needs modeset, all other streams in the
986  * topology need a modeset. All platforms need to add the atomic state
987  * for all streams in the topology, since a modeset on one may require
988  * changing the MST link BW usage of the others, which in turn needs a
989  * recomputation of the corresponding CRTC states.
990  */
991 static int
mst_connector_atomic_topology_check(struct intel_connector * connector,struct intel_atomic_state * state)992 mst_connector_atomic_topology_check(struct intel_connector *connector,
993 				    struct intel_atomic_state *state)
994 {
995 	struct intel_display *display = to_intel_display(connector);
996 	struct drm_connector_list_iter connector_list_iter;
997 	struct intel_connector *connector_iter;
998 	int ret = 0;
999 
1000 	if (!intel_connector_needs_modeset(state, &connector->base))
1001 		return 0;
1002 
1003 	drm_connector_list_iter_begin(display->drm, &connector_list_iter);
1004 	for_each_intel_connector_iter(connector_iter, &connector_list_iter) {
1005 		struct intel_digital_connector_state *conn_iter_state;
1006 		struct intel_crtc_state *crtc_state;
1007 		struct intel_crtc *crtc;
1008 
1009 		if (connector_iter->mst.dp != connector->mst.dp ||
1010 		    connector_iter == connector)
1011 			continue;
1012 
1013 		conn_iter_state = intel_atomic_get_digital_connector_state(state,
1014 									   connector_iter);
1015 		if (IS_ERR(conn_iter_state)) {
1016 			ret = PTR_ERR(conn_iter_state);
1017 			break;
1018 		}
1019 
1020 		if (!conn_iter_state->base.crtc)
1021 			continue;
1022 
1023 		crtc = to_intel_crtc(conn_iter_state->base.crtc);
1024 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
1025 		if (IS_ERR(crtc_state)) {
1026 			ret = PTR_ERR(crtc_state);
1027 			break;
1028 		}
1029 
1030 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
1031 		if (ret)
1032 			break;
1033 		crtc_state->uapi.mode_changed = true;
1034 	}
1035 	drm_connector_list_iter_end(&connector_list_iter);
1036 
1037 	return ret;
1038 }
1039 
1040 static int
mst_connector_atomic_check(struct drm_connector * _connector,struct drm_atomic_commit * _state)1041 mst_connector_atomic_check(struct drm_connector *_connector,
1042 			   struct drm_atomic_commit *_state)
1043 {
1044 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
1045 	struct intel_connector *connector = to_intel_connector(_connector);
1046 	int ret;
1047 
1048 	ret = intel_digital_connector_atomic_check(&connector->base, &state->base);
1049 	if (ret)
1050 		return ret;
1051 
1052 	ret = mst_connector_atomic_topology_check(connector, state);
1053 	if (ret)
1054 		return ret;
1055 
1056 	if (intel_connector_needs_modeset(state, &connector->base)) {
1057 		ret = intel_dp_tunnel_atomic_check_state(state,
1058 							 connector->mst.dp,
1059 							 connector);
1060 		if (ret)
1061 			return ret;
1062 	}
1063 
1064 	return drm_dp_atomic_release_time_slots(&state->base,
1065 						&connector->mst.dp->mst.mgr,
1066 						connector->mst.port);
1067 }
1068 
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)1069 static void mst_stream_disable(struct intel_atomic_state *state,
1070 			       struct intel_encoder *encoder,
1071 			       const struct intel_crtc_state *old_crtc_state,
1072 			       const struct drm_connector_state *old_conn_state)
1073 {
1074 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1075 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1076 	struct intel_connector *connector =
1077 		to_intel_connector(old_conn_state->connector);
1078 
1079 	if (intel_dp_mst_active_streams(intel_dp) == 1)
1080 		intel_dp->link.active = false;
1081 
1082 	intel_hdcp_disable(intel_mst->connector);
1083 
1084 	intel_dp_sink_disable_decompression(state, connector, old_crtc_state);
1085 }
1086 
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)1087 static void mst_stream_post_disable(struct intel_atomic_state *state,
1088 				    struct intel_encoder *encoder,
1089 				    const struct intel_crtc_state *old_crtc_state,
1090 				    const struct drm_connector_state *old_conn_state)
1091 {
1092 	struct intel_display *display = to_intel_display(encoder);
1093 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1094 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1095 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1096 	struct intel_connector *connector =
1097 		to_intel_connector(old_conn_state->connector);
1098 	struct drm_dp_mst_topology_state *old_mst_state =
1099 		drm_atomic_get_old_mst_topology_state(&state->base, &intel_dp->mst.mgr);
1100 	struct drm_dp_mst_topology_state *new_mst_state =
1101 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst.mgr);
1102 	const struct drm_dp_mst_atomic_payload *old_payload =
1103 		drm_atomic_get_mst_payload_state(old_mst_state, connector->mst.port);
1104 	struct drm_dp_mst_atomic_payload *new_payload =
1105 		drm_atomic_get_mst_payload_state(new_mst_state, connector->mst.port);
1106 	struct intel_crtc *pipe_crtc;
1107 	bool last_mst_stream;
1108 
1109 	last_mst_stream = intel_dp_mst_dec_active_streams(intel_dp);
1110 
1111 	drm_WARN_ON(display->drm, DISPLAY_VER(display) >= 12 && last_mst_stream &&
1112 		    !intel_dp_mst_is_master_trans(old_crtc_state));
1113 
1114 	for_each_pipe_crtc_modeset_disable(display, pipe_crtc, old_crtc_state) {
1115 		const struct intel_crtc_state *old_pipe_crtc_state =
1116 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
1117 
1118 		intel_crtc_vblank_off(old_pipe_crtc_state);
1119 	}
1120 
1121 	intel_disable_transcoder(old_crtc_state);
1122 
1123 	drm_dp_remove_payload_part1(&intel_dp->mst.mgr, new_mst_state, new_payload);
1124 
1125 	intel_ddi_clear_act_sent(encoder, old_crtc_state);
1126 
1127 	intel_de_rmw(display,
1128 		     TRANS_DDI_FUNC_CTL(display, old_crtc_state->cpu_transcoder),
1129 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC, 0);
1130 
1131 	intel_ddi_wait_for_act_sent(encoder, old_crtc_state);
1132 	drm_dp_check_act_status(&intel_dp->mst.mgr);
1133 
1134 	drm_dp_remove_payload_part2(&intel_dp->mst.mgr, new_mst_state,
1135 				    old_payload, new_payload);
1136 
1137 	intel_vrr_transcoder_disable(old_crtc_state);
1138 
1139 	intel_ddi_disable_transcoder_func(old_crtc_state);
1140 
1141 	for_each_pipe_crtc_modeset_disable(display, pipe_crtc, old_crtc_state) {
1142 		const struct intel_crtc_state *old_pipe_crtc_state =
1143 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
1144 
1145 		intel_dsc_disable(old_pipe_crtc_state);
1146 
1147 		if (DISPLAY_VER(display) >= 9)
1148 			skl_scaler_disable(old_pipe_crtc_state);
1149 		else
1150 			ilk_pfit_disable(old_pipe_crtc_state);
1151 	}
1152 
1153 	/*
1154 	 * Power down mst path before disabling the port, otherwise we end
1155 	 * up getting interrupts from the sink upon detecting link loss.
1156 	 */
1157 	drm_dp_send_power_updown_phy(&intel_dp->mst.mgr, connector->mst.port,
1158 				     false);
1159 
1160 	/*
1161 	 * BSpec 4287: disable DIP after the transcoder is disabled and before
1162 	 * the transcoder clock select is set to none.
1163 	 */
1164 	intel_dp_set_infoframes(primary_encoder, false, old_crtc_state, NULL);
1165 	/*
1166 	 * From TGL spec: "If multi-stream slave transcoder: Configure
1167 	 * Transcoder Clock Select to direct no clock to the transcoder"
1168 	 *
1169 	 * From older GENs spec: "Configure Transcoder Clock Select to direct
1170 	 * no clock to the transcoder"
1171 	 */
1172 	if (DISPLAY_VER(display) < 12 || !last_mst_stream)
1173 		intel_ddi_disable_transcoder_clock(old_crtc_state);
1174 
1175 
1176 	intel_mst->connector = NULL;
1177 	if (last_mst_stream)
1178 		primary_encoder->post_disable(state, primary_encoder,
1179 					      old_crtc_state, NULL);
1180 
1181 }
1182 
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)1183 static void mst_stream_post_pll_disable(struct intel_atomic_state *state,
1184 					struct intel_encoder *encoder,
1185 					const struct intel_crtc_state *old_crtc_state,
1186 					const struct drm_connector_state *old_conn_state)
1187 {
1188 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1189 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1190 
1191 	if (intel_dp_mst_active_streams(intel_dp) == 0 &&
1192 	    primary_encoder->post_pll_disable)
1193 		primary_encoder->post_pll_disable(state, primary_encoder, old_crtc_state, old_conn_state);
1194 }
1195 
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)1196 static void mst_stream_pre_pll_enable(struct intel_atomic_state *state,
1197 				      struct intel_encoder *encoder,
1198 				      const struct intel_crtc_state *pipe_config,
1199 				      const struct drm_connector_state *conn_state)
1200 {
1201 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1202 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1203 
1204 	if (intel_dp_mst_active_streams(intel_dp) == 0)
1205 		primary_encoder->pre_pll_enable(state, primary_encoder,
1206 						pipe_config, NULL);
1207 	else
1208 		/*
1209 		 * The port PLL state needs to get updated for secondary
1210 		 * streams as for the primary stream.
1211 		 */
1212 		intel_ddi_update_active_dpll(state, primary_encoder,
1213 					     to_intel_crtc(pipe_config->uapi.crtc));
1214 }
1215 
intel_mst_probed_link_params_valid(struct intel_dp * intel_dp,int link_rate,int lane_count)1216 static bool intel_mst_probed_link_params_valid(struct intel_dp *intel_dp,
1217 					       int link_rate, int lane_count)
1218 {
1219 	return intel_dp->link.mst_probed_rate == link_rate &&
1220 		intel_dp->link.mst_probed_lane_count == lane_count;
1221 }
1222 
intel_mst_set_probed_link_params(struct intel_dp * intel_dp,int link_rate,int lane_count)1223 static void intel_mst_set_probed_link_params(struct intel_dp *intel_dp,
1224 					     int link_rate, int lane_count)
1225 {
1226 	intel_dp->link.mst_probed_rate = link_rate;
1227 	intel_dp->link.mst_probed_lane_count = lane_count;
1228 }
1229 
intel_mst_reprobe_topology(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)1230 static void intel_mst_reprobe_topology(struct intel_dp *intel_dp,
1231 				       const struct intel_crtc_state *crtc_state)
1232 {
1233 	if (intel_mst_probed_link_params_valid(intel_dp,
1234 					       crtc_state->port_clock, crtc_state->lane_count))
1235 		return;
1236 
1237 	drm_dp_mst_topology_queue_probe(&intel_dp->mst.mgr);
1238 
1239 	intel_mst_set_probed_link_params(intel_dp,
1240 					 crtc_state->port_clock, crtc_state->lane_count);
1241 }
1242 
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)1243 static void mst_stream_pre_enable(struct intel_atomic_state *state,
1244 				  struct intel_encoder *encoder,
1245 				  const struct intel_crtc_state *pipe_config,
1246 				  const struct drm_connector_state *conn_state)
1247 {
1248 	struct intel_display *display = to_intel_display(state);
1249 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1250 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1251 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1252 	struct intel_connector *connector =
1253 		to_intel_connector(conn_state->connector);
1254 	struct drm_dp_mst_topology_state *mst_state =
1255 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst.mgr);
1256 	int ret;
1257 	bool first_mst_stream;
1258 
1259 	/* MST encoders are bound to a crtc, not to a connector,
1260 	 * force the mapping here for get_hw_state.
1261 	 */
1262 	connector->encoder = encoder;
1263 	intel_mst->connector = connector;
1264 
1265 	first_mst_stream = intel_dp_mst_inc_active_streams(intel_dp);
1266 	drm_WARN_ON(display->drm, DISPLAY_VER(display) >= 12 && first_mst_stream &&
1267 		    !intel_dp_mst_is_master_trans(pipe_config));
1268 
1269 	if (first_mst_stream)
1270 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
1271 
1272 	drm_dp_send_power_updown_phy(&intel_dp->mst.mgr, connector->mst.port, true);
1273 
1274 	intel_dp_sink_enable_decompression(state, connector, pipe_config);
1275 
1276 	if (first_mst_stream) {
1277 		primary_encoder->pre_enable(state, primary_encoder,
1278 					    pipe_config, NULL);
1279 
1280 		intel_mst_reprobe_topology(intel_dp, pipe_config);
1281 	}
1282 
1283 	ret = drm_dp_add_payload_part1(&intel_dp->mst.mgr, mst_state,
1284 				       drm_atomic_get_mst_payload_state(mst_state, connector->mst.port));
1285 	if (ret < 0)
1286 		intel_dp_queue_modeset_retry_for_link(state, primary_encoder, pipe_config);
1287 
1288 	/*
1289 	 * Before Gen 12 this is not done as part of
1290 	 * primary_encoder->pre_enable() and should be done here. For
1291 	 * Gen 12+ the step in which this should be done is different for the
1292 	 * first MST stream, so it's done on the DDI for the first stream and
1293 	 * here for the following ones.
1294 	 */
1295 	if (DISPLAY_VER(display) < 12 || !first_mst_stream)
1296 		intel_ddi_enable_transcoder_clock(encoder, pipe_config);
1297 
1298 	if (DISPLAY_VER(display) >= 13 && !first_mst_stream)
1299 		intel_ddi_config_transcoder_func(encoder, pipe_config);
1300 
1301 	intel_dsc_dp_pps_write(primary_encoder, pipe_config);
1302 	intel_ddi_set_dp_msa(pipe_config, conn_state);
1303 }
1304 
enable_bs_jitter_was(const struct intel_crtc_state * crtc_state)1305 static void enable_bs_jitter_was(const struct intel_crtc_state *crtc_state)
1306 {
1307 	struct intel_display *display = to_intel_display(crtc_state);
1308 	u32 clear = 0;
1309 	u32 set = 0;
1310 
1311 	if (!display->platform.alderlake_p)
1312 		return;
1313 
1314 	if (!IS_DISPLAY_STEP(display, STEP_D0, STEP_FOREVER))
1315 		return;
1316 
1317 	/* Wa_14013163432:adlp */
1318 	if (crtc_state->fec_enable || intel_dp_is_uhbr(crtc_state))
1319 		set |= DP_MST_FEC_BS_JITTER_WA(crtc_state->cpu_transcoder);
1320 
1321 	/* Wa_14014143976:adlp */
1322 	if (intel_display_wa(display, INTEL_DISPLAY_WA_14014143976)) {
1323 		if (intel_dp_is_uhbr(crtc_state))
1324 			set |= DP_MST_SHORT_HBLANK_WA(crtc_state->cpu_transcoder);
1325 		else if (crtc_state->fec_enable)
1326 			clear |= DP_MST_SHORT_HBLANK_WA(crtc_state->cpu_transcoder);
1327 
1328 		if (crtc_state->fec_enable || intel_dp_is_uhbr(crtc_state))
1329 			set |= DP_MST_DPT_DPTP_ALIGN_WA(crtc_state->cpu_transcoder);
1330 	}
1331 
1332 	if (!clear && !set)
1333 		return;
1334 
1335 	intel_de_rmw(display, CHICKEN_MISC_3, clear, set);
1336 }
1337 
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)1338 static void mst_stream_enable(struct intel_atomic_state *state,
1339 			      struct intel_encoder *encoder,
1340 			      const struct intel_crtc_state *pipe_config,
1341 			      const struct drm_connector_state *conn_state)
1342 {
1343 	struct intel_display *display = to_intel_display(encoder);
1344 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1345 	struct intel_dp *intel_dp = to_primary_dp(encoder);
1346 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1347 	struct drm_dp_mst_topology_state *mst_state =
1348 		drm_atomic_get_new_mst_topology_state(&state->base, &intel_dp->mst.mgr);
1349 	enum transcoder trans = pipe_config->cpu_transcoder;
1350 	bool first_mst_stream = intel_dp_mst_active_streams(intel_dp) == 1;
1351 	struct intel_crtc *pipe_crtc;
1352 	int ret;
1353 
1354 	drm_WARN_ON(display->drm, pipe_config->has_pch_encoder);
1355 
1356 	if (intel_dp_is_uhbr(pipe_config)) {
1357 		const struct drm_display_mode *adjusted_mode =
1358 			&pipe_config->hw.adjusted_mode;
1359 		u64 crtc_clock_hz = KHz(adjusted_mode->crtc_clock);
1360 
1361 		intel_de_write(display, TRANS_DP2_VFREQHIGH(pipe_config->cpu_transcoder),
1362 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz >> 24));
1363 		intel_de_write(display, TRANS_DP2_VFREQLOW(pipe_config->cpu_transcoder),
1364 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz & 0xffffff));
1365 	}
1366 
1367 	enable_bs_jitter_was(pipe_config);
1368 
1369 	intel_ddi_enable_transcoder_func(encoder, pipe_config);
1370 
1371 	intel_vrr_transcoder_enable(pipe_config);
1372 
1373 	intel_ddi_clear_act_sent(encoder, pipe_config);
1374 
1375 	intel_de_rmw(display, TRANS_DDI_FUNC_CTL(display, trans), 0,
1376 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
1377 
1378 	intel_ddi_wait_for_act_sent(encoder, pipe_config);
1379 	drm_dp_check_act_status(&intel_dp->mst.mgr);
1380 
1381 	if (first_mst_stream)
1382 		intel_ddi_wait_for_fec_status(encoder, pipe_config, true);
1383 
1384 	ret = drm_dp_add_payload_part2(&intel_dp->mst.mgr,
1385 				       drm_atomic_get_mst_payload_state(mst_state,
1386 									connector->mst.port));
1387 	if (ret < 0)
1388 		intel_dp_queue_modeset_retry_for_link(state, primary_encoder, pipe_config);
1389 
1390 	if (DISPLAY_VER(display) >= 12)
1391 		intel_de_rmw(display, CHICKEN_TRANS(display, trans),
1392 			     FECSTALL_DIS_DPTSTREAM_DPTTG,
1393 			     pipe_config->fec_enable ? FECSTALL_DIS_DPTSTREAM_DPTTG : 0);
1394 
1395 	intel_enable_transcoder(pipe_config);
1396 
1397 	for_each_pipe_crtc_modeset_enable(display, pipe_crtc, pipe_config) {
1398 		const struct intel_crtc_state *pipe_crtc_state =
1399 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1400 
1401 		intel_crtc_vblank_on(pipe_crtc_state);
1402 	}
1403 
1404 	intel_hdcp_enable(state, encoder, pipe_config, conn_state);
1405 }
1406 
mst_stream_get_hw_state(struct intel_encoder * encoder,enum pipe * pipe)1407 static bool mst_stream_get_hw_state(struct intel_encoder *encoder,
1408 				    enum pipe *pipe)
1409 {
1410 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
1411 	*pipe = intel_mst->pipe;
1412 	if (intel_mst->connector)
1413 		return true;
1414 	return false;
1415 }
1416 
mst_stream_get_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config)1417 static void mst_stream_get_config(struct intel_encoder *encoder,
1418 				  struct intel_crtc_state *pipe_config)
1419 {
1420 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1421 
1422 	primary_encoder->get_config(primary_encoder, pipe_config);
1423 }
1424 
mst_stream_initial_fastset_check(struct intel_encoder * encoder,struct intel_crtc_state * crtc_state)1425 static bool mst_stream_initial_fastset_check(struct intel_encoder *encoder,
1426 					     struct intel_crtc_state *crtc_state)
1427 {
1428 	struct intel_encoder *primary_encoder = to_primary_encoder(encoder);
1429 
1430 	return intel_dp_initial_fastset_check(primary_encoder, crtc_state);
1431 }
1432 
mst_connector_get_ddc_modes(struct drm_connector * _connector)1433 static int mst_connector_get_ddc_modes(struct drm_connector *_connector)
1434 {
1435 	struct intel_connector *connector = to_intel_connector(_connector);
1436 	struct intel_display *display = to_intel_display(connector);
1437 	struct intel_dp *intel_dp = connector->mst.dp;
1438 	const struct drm_edid *drm_edid;
1439 	int ret;
1440 
1441 	if (drm_connector_is_unregistered(&connector->base))
1442 		return intel_connector_update_modes(&connector->base, NULL);
1443 
1444 	if (!intel_display_driver_check_access(display))
1445 		return drm_edid_connector_add_modes(&connector->base);
1446 
1447 	drm_edid = drm_dp_mst_edid_read(&connector->base, &intel_dp->mst.mgr, connector->mst.port);
1448 
1449 	ret = intel_connector_update_modes(&connector->base, drm_edid);
1450 
1451 	drm_edid_free(drm_edid);
1452 
1453 	if (intel_dp_tunnel_uhbr_lanes_wa_setup(intel_dp)) {
1454 		intel_dp_flush_connector_commits(connector);
1455 		intel_dp_tunnel_uhbr_lanes_wa_apply(intel_dp);
1456 	}
1457 
1458 	return ret;
1459 }
1460 
1461 static int
mst_connector_late_register(struct drm_connector * _connector)1462 mst_connector_late_register(struct drm_connector *_connector)
1463 {
1464 	struct intel_connector *connector = to_intel_connector(_connector);
1465 	int ret;
1466 
1467 	ret = drm_dp_mst_connector_late_register(&connector->base, connector->mst.port);
1468 	if (ret < 0)
1469 		return ret;
1470 
1471 	ret = intel_connector_register(&connector->base);
1472 	if (ret < 0)
1473 		drm_dp_mst_connector_early_unregister(&connector->base, connector->mst.port);
1474 
1475 	return ret;
1476 }
1477 
1478 static void
mst_connector_early_unregister(struct drm_connector * _connector)1479 mst_connector_early_unregister(struct drm_connector *_connector)
1480 {
1481 	struct intel_connector *connector = to_intel_connector(_connector);
1482 
1483 	intel_connector_unregister(&connector->base);
1484 	drm_dp_mst_connector_early_unregister(&connector->base, connector->mst.port);
1485 }
1486 
1487 static const struct drm_connector_funcs mst_connector_funcs = {
1488 	.fill_modes = drm_helper_probe_single_connector_modes,
1489 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1490 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1491 	.late_register = mst_connector_late_register,
1492 	.early_unregister = mst_connector_early_unregister,
1493 	.destroy = intel_connector_destroy,
1494 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1495 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1496 };
1497 
mst_connector_get_modes(struct drm_connector * _connector)1498 static int mst_connector_get_modes(struct drm_connector *_connector)
1499 {
1500 	struct intel_connector *connector = to_intel_connector(_connector);
1501 
1502 	return mst_connector_get_ddc_modes(&connector->base);
1503 }
1504 
1505 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)1506 mst_connector_mode_valid_ctx(struct drm_connector *_connector,
1507 			     const struct drm_display_mode *mode,
1508 			     struct drm_modeset_acquire_ctx *ctx,
1509 			     enum drm_mode_status *status)
1510 {
1511 	struct intel_connector *connector = to_intel_connector(_connector);
1512 	struct intel_display *display = to_intel_display(connector);
1513 	struct intel_dp *intel_dp = connector->mst.dp;
1514 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst.mgr;
1515 	struct drm_dp_mst_port *port = connector->mst.port;
1516 	int max_rate, mode_rate, max_lanes, max_link_clock;
1517 	unsigned long bw_overhead_flags =
1518 		DRM_DP_BW_OVERHEAD_MST | DRM_DP_BW_OVERHEAD_SSC_REF_CLK;
1519 	int min_link_bpp_x16 = fxp_q4_from_int(18);
1520 	struct intel_dp_link_config max_bw_config;
1521 	static bool supports_dsc;
1522 	int ret;
1523 	bool dsc = false;
1524 	int target_clock = mode->clock;
1525 	int num_joined_pipes;
1526 
1527 	if (drm_connector_is_unregistered(&connector->base)) {
1528 		*status = MODE_ERROR;
1529 		return 0;
1530 	}
1531 
1532 	*status = intel_cpu_transcoder_mode_valid(display, mode);
1533 	if (*status != MODE_OK)
1534 		return 0;
1535 
1536 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
1537 		*status = MODE_H_ILLEGAL;
1538 		return 0;
1539 	}
1540 
1541 	if (mode->clock < 10000) {
1542 		*status = MODE_CLOCK_LOW;
1543 		return 0;
1544 	}
1545 
1546 	supports_dsc = intel_dp_has_dsc(connector) &&
1547 		       drm_dp_sink_supports_fec(connector->dp.fec_capability);
1548 
1549 	if (supports_dsc && connector->mst.port->passthrough_aux)
1550 		min_link_bpp_x16 = intel_dp_compute_min_compressed_bpp_x16(connector,
1551 									   INTEL_OUTPUT_FORMAT_RGB);
1552 
1553 	intel_dp_link_caps_get_max_bw_config(intel_dp->link.caps, &max_bw_config);
1554 	max_link_clock = max_bw_config.rate;
1555 	max_lanes = max_bw_config.lane_count;
1556 
1557 	max_rate = intel_dp_max_link_data_rate(intel_dp,
1558 					       max_link_clock, max_lanes);
1559 	mode_rate = intel_dp_link_required(max_link_clock, max_lanes,
1560 					   mode->clock, mode->hdisplay,
1561 					   min_link_bpp_x16,
1562 					   bw_overhead_flags);
1563 
1564 	/*
1565 	 * TODO:
1566 	 * - Also check if compression would allow for the mode
1567 	 *   in non-passthrough mode, i.e. the last branch device
1568 	 *   decompressing the stream. This makes a difference only if
1569 	 *   the BW on the link between the last branch device and the
1570 	 *   sink is higher than the BW on the whole MST path from the
1571 	 *   source to the last branch device. Relying on the extra BW
1572 	 *   this provides also requires the
1573 	 *   DFP_Link_Available_Payload_Bandwidth_Number described below.
1574 	 * - Calculate the overhead using drm_dp_bw_overhead() /
1575 	 *   drm_dp_bw_channel_coding_efficiency(), similarly to the
1576 	 *   compute config code, as drm_dp_calc_pbn_mode() doesn't
1577 	 *   account with all the overheads.
1578 	 * - Check here and during compute config the BW reported by
1579 	 *   DFP_Link_Available_Payload_Bandwidth_Number (or the
1580 	 *   corresponding link capabilities of the sink) in case the
1581 	 *   stream is uncompressed for it by the last branch device.
1582 	 */
1583 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
1584 	if (ret)
1585 		return ret;
1586 
1587 	if (mode_rate > max_rate ||
1588 	    drm_dp_calc_pbn_mode(mode->clock, min_link_bpp_x16) > port->full_pbn) {
1589 		*status = MODE_CLOCK_HIGH;
1590 		return 0;
1591 	}
1592 
1593 	*status = MODE_CLOCK_HIGH;
1594 	for_each_joiner_candidate(connector, mode, num_joined_pipes) {
1595 		int dsc_slice_count = 0;
1596 
1597 		if (supports_dsc) {
1598 			/*
1599 			 * TBD pass the connector BPC,
1600 			 * for now U8_MAX so that max BPC on that platform would be picked
1601 			 */
1602 			int pipe_bpp = intel_dp_dsc_compute_max_bpp(connector, U8_MAX);
1603 
1604 			dsc_slice_count = intel_dp_dsc_get_slice_count(connector,
1605 								       mode->clock,
1606 								       mode->hdisplay,
1607 								       num_joined_pipes);
1608 
1609 			if (!drm_dp_is_uhbr_rate(max_link_clock))
1610 				bw_overhead_flags |= DRM_DP_BW_OVERHEAD_FEC;
1611 
1612 			dsc = intel_dp_mode_valid_with_dsc(connector,
1613 							   max_link_clock, max_lanes,
1614 							   target_clock, mode->hdisplay,
1615 							   num_joined_pipes,
1616 							   INTEL_OUTPUT_FORMAT_RGB, pipe_bpp,
1617 							   bw_overhead_flags);
1618 		}
1619 
1620 		if (intel_dp_joiner_needs_dsc(display, num_joined_pipes) && !dsc) {
1621 			*status = MODE_CLOCK_HIGH;
1622 			continue;
1623 		}
1624 
1625 		if (mode_rate > max_rate && !dsc) {
1626 			*status = MODE_CLOCK_HIGH;
1627 			continue;
1628 		}
1629 
1630 		*status = intel_mode_valid_max_plane_size(display, mode, num_joined_pipes);
1631 
1632 		if (*status != MODE_OK)
1633 			continue;
1634 
1635 		if (!dsc)
1636 			dsc_slice_count = 0;
1637 
1638 		if (!intel_dp_dotclk_valid(display,
1639 					   mode->clock,
1640 					   mode->htotal,
1641 					   dsc_slice_count,
1642 					   num_joined_pipes)) {
1643 			*status = MODE_CLOCK_HIGH;
1644 			continue;
1645 		}
1646 
1647 		break;
1648 	}
1649 
1650 	return 0;
1651 }
1652 
1653 static struct drm_encoder *
mst_connector_atomic_best_encoder(struct drm_connector * _connector,struct drm_atomic_commit * state)1654 mst_connector_atomic_best_encoder(struct drm_connector *_connector,
1655 				  struct drm_atomic_commit *state)
1656 {
1657 	struct intel_connector *connector = to_intel_connector(_connector);
1658 	struct drm_connector_state *connector_state =
1659 		drm_atomic_get_new_connector_state(state, &connector->base);
1660 	struct intel_dp *intel_dp = connector->mst.dp;
1661 	struct intel_crtc *crtc = to_intel_crtc(connector_state->crtc);
1662 
1663 	return &intel_dp->mst.stream_encoders[crtc->pipe]->base.base;
1664 }
1665 
1666 static int
mst_connector_detect_ctx(struct drm_connector * _connector,struct drm_modeset_acquire_ctx * ctx,bool force)1667 mst_connector_detect_ctx(struct drm_connector *_connector,
1668 			 struct drm_modeset_acquire_ctx *ctx, bool force)
1669 {
1670 	struct intel_connector *connector = to_intel_connector(_connector);
1671 	struct intel_display *display = to_intel_display(connector);
1672 	struct intel_dp *intel_dp = connector->mst.dp;
1673 
1674 	if (!intel_display_device_enabled(display))
1675 		return connector_status_disconnected;
1676 
1677 	if (drm_connector_is_unregistered(&connector->base))
1678 		return connector_status_disconnected;
1679 
1680 	if (!intel_display_driver_check_access(display))
1681 		return connector->base.status;
1682 
1683 	intel_dp_flush_connector_commits(connector);
1684 
1685 	return drm_dp_mst_detect_port(&connector->base, ctx, &intel_dp->mst.mgr,
1686 				      connector->mst.port);
1687 }
1688 
1689 static const struct drm_connector_helper_funcs mst_connector_helper_funcs = {
1690 	.get_modes = mst_connector_get_modes,
1691 	.mode_valid_ctx = mst_connector_mode_valid_ctx,
1692 	.atomic_best_encoder = mst_connector_atomic_best_encoder,
1693 	.atomic_check = mst_connector_atomic_check,
1694 	.detect_ctx = mst_connector_detect_ctx,
1695 };
1696 
mst_stream_encoder_destroy(struct drm_encoder * encoder)1697 static void mst_stream_encoder_destroy(struct drm_encoder *encoder)
1698 {
1699 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(to_intel_encoder(encoder));
1700 
1701 	drm_encoder_cleanup(encoder);
1702 	kfree(intel_mst);
1703 }
1704 
1705 static const struct drm_encoder_funcs mst_stream_encoder_funcs = {
1706 	.destroy = mst_stream_encoder_destroy,
1707 };
1708 
mst_connector_get_hw_state(struct intel_connector * connector)1709 static bool mst_connector_get_hw_state(struct intel_connector *connector)
1710 {
1711 	/* This is the MST stream encoder set in ->pre_enable, if any */
1712 	struct intel_encoder *encoder = intel_attached_encoder(connector);
1713 	enum pipe pipe;
1714 
1715 	if (!encoder || !connector->base.state->crtc)
1716 		return false;
1717 
1718 	return encoder->get_hw_state(encoder, &pipe);
1719 }
1720 
mst_topology_add_connector_properties(struct intel_dp * intel_dp,struct drm_connector * _connector,const char * pathprop)1721 static int mst_topology_add_connector_properties(struct intel_dp *intel_dp,
1722 						 struct drm_connector *_connector,
1723 						 const char *pathprop)
1724 {
1725 	struct intel_display *display = to_intel_display(intel_dp);
1726 	struct intel_connector *connector = to_intel_connector(_connector);
1727 
1728 	drm_object_attach_property(&connector->base.base,
1729 				   display->drm->mode_config.path_property, 0);
1730 	drm_object_attach_property(&connector->base.base,
1731 				   display->drm->mode_config.tile_property, 0);
1732 
1733 	intel_attach_force_audio_property(&connector->base);
1734 	intel_attach_broadcast_rgb_property(&connector->base);
1735 
1736 	/*
1737 	 * Reuse the prop from the SST connector because we're
1738 	 * not allowed to create new props after device registration.
1739 	 */
1740 	connector->base.max_bpc_property =
1741 		intel_dp->attached_connector->base.max_bpc_property;
1742 	if (connector->base.max_bpc_property)
1743 		drm_connector_attach_max_bpc_property(&connector->base, 6, 12);
1744 
1745 	return drm_connector_set_path_property(&connector->base, pathprop);
1746 }
1747 
1748 static void
intel_dp_mst_read_decompression_port_dsc_caps(struct intel_dp * intel_dp,struct intel_connector * connector)1749 intel_dp_mst_read_decompression_port_dsc_caps(struct intel_dp *intel_dp,
1750 					      struct intel_connector *connector)
1751 {
1752 	u8 dpcd_caps[DP_RECEIVER_CAP_SIZE];
1753 	struct drm_dp_desc desc;
1754 
1755 	if (!connector->dp.dsc_decompression_aux)
1756 		return;
1757 
1758 	if (drm_dp_read_dpcd_caps(connector->dp.dsc_decompression_aux, dpcd_caps) < 0)
1759 		return;
1760 
1761 	if (drm_dp_read_desc(connector->dp.dsc_decompression_aux, &desc,
1762 			     drm_dp_is_branch(dpcd_caps)) < 0)
1763 		return;
1764 
1765 	intel_dp_get_dsc_sink_cap(dpcd_caps[DP_DPCD_REV],
1766 				  &desc, drm_dp_is_branch(dpcd_caps),
1767 				  connector);
1768 }
1769 
detect_dsc_hblank_expansion_quirk(const struct intel_connector * connector)1770 static bool detect_dsc_hblank_expansion_quirk(const struct intel_connector *connector)
1771 {
1772 	struct intel_display *display = to_intel_display(connector);
1773 	struct drm_dp_aux *aux = connector->dp.dsc_decompression_aux;
1774 	struct drm_dp_desc desc;
1775 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
1776 
1777 	if (!aux)
1778 		return false;
1779 
1780 	/*
1781 	 * A logical port's OUI (at least for affected sinks) is all 0, so
1782 	 * instead of that the parent port's OUI is used for identification.
1783 	 */
1784 	if (drm_dp_mst_port_is_logical(connector->mst.port)) {
1785 		aux = drm_dp_mst_aux_for_parent(connector->mst.port);
1786 		if (!aux)
1787 			aux = &connector->mst.dp->aux;
1788 	}
1789 
1790 	if (drm_dp_read_dpcd_caps(aux, dpcd) < 0)
1791 		return false;
1792 
1793 	if (drm_dp_read_desc(aux, &desc, drm_dp_is_branch(dpcd)) < 0)
1794 		return false;
1795 
1796 	if (!drm_dp_has_quirk(&desc,
1797 			      DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC))
1798 		return false;
1799 
1800 	/*
1801 	 * UHBR (MST sink) devices requiring this quirk don't advertise the
1802 	 * HBLANK expansion support. Presuming that they perform HBLANK
1803 	 * expansion internally, or are affected by this issue on modes with a
1804 	 * short HBLANK for other reasons.
1805 	 */
1806 	if (!drm_dp_128b132b_supported(dpcd) &&
1807 	    !(dpcd[DP_RECEIVE_PORT_0_CAP_0] & DP_HBLANK_EXPANSION_CAPABLE))
1808 		return false;
1809 
1810 	drm_dbg_kms(display->drm,
1811 		    "[CONNECTOR:%d:%s] DSC HBLANK expansion quirk detected\n",
1812 		    connector->base.base.id, connector->base.name);
1813 
1814 	return true;
1815 }
1816 
1817 static struct drm_connector *
mst_topology_add_connector(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,const char * pathprop)1818 mst_topology_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1819 			   struct drm_dp_mst_port *port,
1820 			   const char *pathprop)
1821 {
1822 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst.mgr);
1823 	struct intel_display *display = to_intel_display(intel_dp);
1824 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1825 	struct intel_connector *connector;
1826 	enum pipe pipe;
1827 	int ret;
1828 
1829 	connector = intel_connector_alloc();
1830 	if (!connector)
1831 		return NULL;
1832 
1833 	connector->get_hw_state = mst_connector_get_hw_state;
1834 	connector->sync_state = intel_dp_connector_sync_state;
1835 	connector->mst.dp = intel_dp;
1836 	connector->mst.port = port;
1837 	drm_dp_mst_get_port_malloc(port);
1838 
1839 	ret = drm_connector_dynamic_init(display->drm, &connector->base, &mst_connector_funcs,
1840 					 DRM_MODE_CONNECTOR_DisplayPort, NULL);
1841 	if (ret)
1842 		goto err_put_port;
1843 
1844 	connector->dp.dsc_decompression_aux = drm_dp_mst_dsc_aux_for_port(port);
1845 	intel_dp_mst_read_decompression_port_dsc_caps(intel_dp, connector);
1846 	connector->dp.dsc_hblank_expansion_quirk =
1847 		detect_dsc_hblank_expansion_quirk(connector);
1848 
1849 	drm_connector_helper_add(&connector->base, &mst_connector_helper_funcs);
1850 
1851 	for_each_pipe(display, pipe) {
1852 		struct drm_encoder *enc =
1853 			&intel_dp->mst.stream_encoders[pipe]->base.base;
1854 
1855 		ret = drm_connector_attach_encoder(&connector->base, enc);
1856 		if (ret)
1857 			goto err_cleanup_connector;
1858 	}
1859 
1860 	ret = mst_topology_add_connector_properties(intel_dp, &connector->base, pathprop);
1861 	if (ret)
1862 		goto err_cleanup_connector;
1863 
1864 	ret = intel_dp_hdcp_init(dig_port, connector);
1865 	if (ret)
1866 		drm_dbg_kms(display->drm, "[%s:%d] HDCP MST init failed, skipping.\n",
1867 			    connector->base.name, connector->base.base.id);
1868 
1869 	return &connector->base;
1870 
1871 err_cleanup_connector:
1872 	drm_connector_cleanup(&connector->base);
1873 err_put_port:
1874 	drm_dp_mst_put_port_malloc(port);
1875 	intel_connector_free(connector);
1876 
1877 	return NULL;
1878 }
1879 
1880 static void
mst_topology_poll_hpd_irq(struct drm_dp_mst_topology_mgr * mgr)1881 mst_topology_poll_hpd_irq(struct drm_dp_mst_topology_mgr *mgr)
1882 {
1883 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst.mgr);
1884 
1885 	intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
1886 }
1887 
1888 static const struct drm_dp_mst_topology_cbs mst_topology_cbs = {
1889 	.add_connector = mst_topology_add_connector,
1890 	.poll_hpd_irq = mst_topology_poll_hpd_irq,
1891 };
1892 
1893 /* Create a fake encoder for an individual MST stream */
1894 static struct intel_dp_mst_encoder *
mst_stream_encoder_create(struct intel_digital_port * dig_port,enum pipe pipe)1895 mst_stream_encoder_create(struct intel_digital_port *dig_port, enum pipe pipe)
1896 {
1897 	struct intel_display *display = to_intel_display(dig_port);
1898 	struct intel_encoder *primary_encoder = &dig_port->base;
1899 	struct intel_dp_mst_encoder *intel_mst;
1900 	struct intel_encoder *encoder;
1901 
1902 	intel_mst = kzalloc_obj(*intel_mst);
1903 
1904 	if (!intel_mst)
1905 		return NULL;
1906 
1907 	intel_mst->pipe = pipe;
1908 	encoder = &intel_mst->base;
1909 	intel_mst->primary = dig_port;
1910 
1911 	drm_encoder_init(display->drm, &encoder->base, &mst_stream_encoder_funcs,
1912 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
1913 
1914 	encoder->type = INTEL_OUTPUT_DP_MST;
1915 	encoder->power_domain = primary_encoder->power_domain;
1916 	encoder->port = primary_encoder->port;
1917 	encoder->cloneable = 0;
1918 	/*
1919 	 * This is wrong, but broken userspace uses the intersection
1920 	 * of possible_crtcs of all the encoders of a given connector
1921 	 * to figure out which crtcs can drive said connector. What
1922 	 * should be used instead is the union of possible_crtcs.
1923 	 * To keep such userspace functioning we must misconfigure
1924 	 * this to make sure the intersection is not empty :(
1925 	 */
1926 	encoder->pipe_mask = ~0;
1927 
1928 	encoder->compute_config = mst_stream_compute_config;
1929 	encoder->compute_config_late = mst_stream_compute_config_late;
1930 	encoder->disable = mst_stream_disable;
1931 	encoder->post_disable = mst_stream_post_disable;
1932 	encoder->post_pll_disable = mst_stream_post_pll_disable;
1933 	encoder->update_pipe = intel_ddi_update_pipe;
1934 	encoder->pre_pll_enable = mst_stream_pre_pll_enable;
1935 	encoder->pre_enable = mst_stream_pre_enable;
1936 	encoder->enable = mst_stream_enable;
1937 	encoder->audio_enable = intel_audio_codec_enable;
1938 	encoder->audio_disable = intel_audio_codec_disable;
1939 	encoder->get_hw_state = mst_stream_get_hw_state;
1940 	encoder->get_config = mst_stream_get_config;
1941 	encoder->initial_fastset_check = mst_stream_initial_fastset_check;
1942 
1943 	return intel_mst;
1944 
1945 }
1946 
1947 /* Create the fake encoders for MST streams */
1948 static bool
mst_stream_encoders_create(struct intel_digital_port * dig_port)1949 mst_stream_encoders_create(struct intel_digital_port *dig_port)
1950 {
1951 	struct intel_display *display = to_intel_display(dig_port);
1952 	struct intel_dp *intel_dp = &dig_port->dp;
1953 	enum pipe pipe;
1954 
1955 	for_each_pipe(display, pipe)
1956 		intel_dp->mst.stream_encoders[pipe] = mst_stream_encoder_create(dig_port, pipe);
1957 	return true;
1958 }
1959 
1960 int
intel_dp_mst_encoder_init(struct intel_digital_port * dig_port,int conn_base_id)1961 intel_dp_mst_encoder_init(struct intel_digital_port *dig_port, int conn_base_id)
1962 {
1963 	struct intel_display *display = to_intel_display(dig_port);
1964 	struct intel_dp *intel_dp = &dig_port->dp;
1965 	enum port port = dig_port->base.port;
1966 	int ret;
1967 
1968 	if (!HAS_DP_MST(display) || intel_dp_is_edp(intel_dp))
1969 		return 0;
1970 
1971 	if (DISPLAY_VER(display) < 12 && port == PORT_A)
1972 		return 0;
1973 
1974 	if (DISPLAY_VER(display) < 11 && port == PORT_E)
1975 		return 0;
1976 
1977 	intel_dp->mst.mgr.cbs = &mst_topology_cbs;
1978 
1979 	/* create encoders */
1980 	mst_stream_encoders_create(dig_port);
1981 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst.mgr, display->drm,
1982 					   &intel_dp->aux, 16,
1983 					   INTEL_NUM_PIPES(display), conn_base_id);
1984 	if (ret) {
1985 		intel_dp->mst.mgr.cbs = NULL;
1986 		return ret;
1987 	}
1988 
1989 	return 0;
1990 }
1991 
intel_dp_mst_source_support(struct intel_dp * intel_dp)1992 bool intel_dp_mst_source_support(struct intel_dp *intel_dp)
1993 {
1994 	return intel_dp->mst.mgr.cbs;
1995 }
1996 
1997 void
intel_dp_mst_encoder_cleanup(struct intel_digital_port * dig_port)1998 intel_dp_mst_encoder_cleanup(struct intel_digital_port *dig_port)
1999 {
2000 	struct intel_dp *intel_dp = &dig_port->dp;
2001 
2002 	if (!intel_dp_mst_source_support(intel_dp))
2003 		return;
2004 
2005 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst.mgr);
2006 	/* encoders will get killed by normal cleanup */
2007 
2008 	intel_dp->mst.mgr.cbs = NULL;
2009 }
2010 
intel_dp_mst_is_master_trans(const struct intel_crtc_state * crtc_state)2011 bool intel_dp_mst_is_master_trans(const struct intel_crtc_state *crtc_state)
2012 {
2013 	return crtc_state->mst_master_transcoder == crtc_state->cpu_transcoder;
2014 }
2015 
intel_dp_mst_is_slave_trans(const struct intel_crtc_state * crtc_state)2016 bool intel_dp_mst_is_slave_trans(const struct intel_crtc_state *crtc_state)
2017 {
2018 	return crtc_state->mst_master_transcoder != INVALID_TRANSCODER &&
2019 	       crtc_state->mst_master_transcoder != crtc_state->cpu_transcoder;
2020 }
2021 
2022 /**
2023  * intel_dp_mst_add_topology_state_for_connector - add MST topology state for a connector
2024  * @state: atomic state
2025  * @connector: connector to add the state for
2026  * @crtc: the CRTC @connector is attached to
2027  *
2028  * Add the MST topology state for @connector to @state.
2029  *
2030  * Returns 0 on success, negative error code on failure.
2031  */
2032 static int
intel_dp_mst_add_topology_state_for_connector(struct intel_atomic_state * state,struct intel_connector * connector,struct intel_crtc * crtc)2033 intel_dp_mst_add_topology_state_for_connector(struct intel_atomic_state *state,
2034 					      struct intel_connector *connector,
2035 					      struct intel_crtc *crtc)
2036 {
2037 	struct drm_dp_mst_topology_state *mst_state;
2038 
2039 	if (!connector->mst.dp)
2040 		return 0;
2041 
2042 	mst_state = drm_atomic_get_mst_topology_state(&state->base,
2043 						      &connector->mst.dp->mst.mgr);
2044 	if (IS_ERR(mst_state))
2045 		return PTR_ERR(mst_state);
2046 
2047 	mst_state->pending_crtc_mask |= drm_crtc_mask(&crtc->base);
2048 
2049 	return 0;
2050 }
2051 
2052 /**
2053  * intel_dp_mst_add_topology_state_for_crtc - add MST topology state for a CRTC
2054  * @state: atomic state
2055  * @crtc: CRTC to add the state for
2056  *
2057  * Add the MST topology state for @crtc to @state.
2058  *
2059  * Returns 0 on success, negative error code on failure.
2060  */
intel_dp_mst_add_topology_state_for_crtc(struct intel_atomic_state * state,struct intel_crtc * crtc)2061 int intel_dp_mst_add_topology_state_for_crtc(struct intel_atomic_state *state,
2062 					     struct intel_crtc *crtc)
2063 {
2064 	struct drm_connector *_connector;
2065 	struct drm_connector_state *conn_state;
2066 	int i;
2067 
2068 	for_each_new_connector_in_state(&state->base, _connector, conn_state, i) {
2069 		struct intel_connector *connector = to_intel_connector(_connector);
2070 		int ret;
2071 
2072 		if (conn_state->crtc != &crtc->base)
2073 			continue;
2074 
2075 		ret = intel_dp_mst_add_topology_state_for_connector(state, connector, crtc);
2076 		if (ret)
2077 			return ret;
2078 	}
2079 
2080 	return 0;
2081 }
2082 
2083 static struct intel_connector *
get_connector_in_state_for_crtc(struct intel_atomic_state * state,const struct intel_crtc * crtc)2084 get_connector_in_state_for_crtc(struct intel_atomic_state *state,
2085 				const struct intel_crtc *crtc)
2086 {
2087 	struct drm_connector_state *old_conn_state;
2088 	struct drm_connector_state *new_conn_state;
2089 	struct drm_connector *_connector;
2090 	int i;
2091 
2092 	for_each_oldnew_connector_in_state(&state->base, _connector,
2093 					   old_conn_state, new_conn_state, i) {
2094 		struct intel_connector *connector =
2095 			to_intel_connector(_connector);
2096 
2097 		if (old_conn_state->crtc == &crtc->base ||
2098 		    new_conn_state->crtc == &crtc->base)
2099 			return connector;
2100 	}
2101 
2102 	return NULL;
2103 }
2104 
2105 /**
2106  * intel_dp_mst_crtc_needs_modeset - check if changes in topology need to modeset the given CRTC
2107  * @state: atomic state
2108  * @crtc: CRTC for which to check the modeset requirement
2109  *
2110  * Check if any change in a MST topology requires a forced modeset on @crtc in
2111  * this topology. One such change is enabling/disabling the DSC decompression
2112  * state in the first branch device's UFP DPCD as required by one CRTC, while
2113  * the other @crtc in the same topology is still active, requiring a full modeset
2114  * on @crtc.
2115  */
intel_dp_mst_crtc_needs_modeset(struct intel_atomic_state * state,struct intel_crtc * crtc)2116 bool intel_dp_mst_crtc_needs_modeset(struct intel_atomic_state *state,
2117 				     struct intel_crtc *crtc)
2118 {
2119 	const struct intel_connector *crtc_connector;
2120 	const struct drm_connector_state *conn_state;
2121 	const struct drm_connector *_connector;
2122 	int i;
2123 
2124 	if (!intel_crtc_has_type(intel_atomic_get_new_crtc_state(state, crtc),
2125 				 INTEL_OUTPUT_DP_MST))
2126 		return false;
2127 
2128 	crtc_connector = get_connector_in_state_for_crtc(state, crtc);
2129 
2130 	if (!crtc_connector)
2131 		/* None of the connectors in the topology needs modeset */
2132 		return false;
2133 
2134 	for_each_new_connector_in_state(&state->base, _connector, conn_state, i) {
2135 		const struct intel_connector *connector =
2136 			to_intel_connector(_connector);
2137 		const struct intel_crtc_state *new_crtc_state;
2138 		const struct intel_crtc_state *old_crtc_state;
2139 		struct intel_crtc *crtc_iter;
2140 
2141 		if (connector->mst.dp != crtc_connector->mst.dp ||
2142 		    !conn_state->crtc)
2143 			continue;
2144 
2145 		crtc_iter = to_intel_crtc(conn_state->crtc);
2146 
2147 		new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc_iter);
2148 		old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc_iter);
2149 
2150 		if (!intel_crtc_needs_modeset(new_crtc_state))
2151 			continue;
2152 
2153 		if (old_crtc_state->dsc.compression_enable ==
2154 		    new_crtc_state->dsc.compression_enable)
2155 			continue;
2156 		/*
2157 		 * Toggling the decompression flag because of this stream in
2158 		 * the first downstream branch device's UFP DPCD may reset the
2159 		 * whole branch device. To avoid the reset while other streams
2160 		 * are also active modeset the whole MST topology in this
2161 		 * case.
2162 		 */
2163 		if (connector->dp.dsc_decompression_aux ==
2164 		    &connector->mst.dp->aux)
2165 			return true;
2166 	}
2167 
2168 	return false;
2169 }
2170 
intel_dp_mst_stream_disconnected(struct intel_atomic_state * state,const struct intel_crtc * crtc)2171 bool intel_dp_mst_stream_disconnected(struct intel_atomic_state *state,
2172 				      const struct intel_crtc *crtc)
2173 {
2174 	struct intel_connector *connector;
2175 
2176 	connector = get_connector_in_state_for_crtc(state, crtc);
2177 	if (!connector)
2178 		return false;
2179 
2180 	if (!connector->mst.dp)
2181 		return false;
2182 
2183 	if (!connector->mst.dp->mst.mgr.mst_state)
2184 		return true;
2185 
2186 	if (drm_connector_is_unregistered(&connector->base))
2187 		return true;
2188 
2189 	return false;
2190 }
2191 
2192 /**
2193  * intel_dp_mst_prepare_probe - Prepare an MST link for topology probing
2194  * @intel_dp: DP port object
2195  *
2196  * Prepare an MST link for topology probing, programming the target
2197  * link parameters to DPCD. This step is a requirement of the enumeration
2198  * of path resources during probing.
2199  */
intel_dp_mst_prepare_probe(struct intel_dp * intel_dp)2200 void intel_dp_mst_prepare_probe(struct intel_dp *intel_dp)
2201 {
2202 	struct intel_dp_link_config max_bw_config;
2203 	int link_rate;
2204 	int lane_count;
2205 	u8 rate_select;
2206 	u8 link_bw;
2207 
2208 	if (intel_dp->link.active)
2209 		return;
2210 
2211 	intel_dp_link_caps_get_max_bw_config(intel_dp->link.caps, &max_bw_config);
2212 	link_rate = max_bw_config.rate;
2213 	lane_count = max_bw_config.lane_count;
2214 
2215 	if (intel_mst_probed_link_params_valid(intel_dp, link_rate, lane_count))
2216 		return;
2217 
2218 	intel_dp_compute_rate(intel_dp, link_rate, &link_bw, &rate_select);
2219 
2220 	intel_dp_link_training_set_mode(intel_dp, link_rate, false, false);
2221 	intel_dp_link_training_set_bw(intel_dp, link_bw, rate_select, lane_count,
2222 				      drm_dp_enhanced_frame_cap(intel_dp->dpcd), false);
2223 
2224 	intel_mst_set_probed_link_params(intel_dp, link_rate, lane_count);
2225 }
2226 
2227 /*
2228  * intel_dp_mst_verify_dpcd_state - verify the MST SW enabled state wrt. the DPCD
2229  * @intel_dp: DP port object
2230  *
2231  * Verify if @intel_dp's MST enabled SW state matches the corresponding DPCD
2232  * state. A long HPD pulse - not long enough to be detected as a disconnected
2233  * state - could've reset the DPCD state, which requires tearing
2234  * down/recreating the MST topology.
2235  *
2236  * Returns %true if the SW MST enabled and DPCD states match, %false
2237  * otherwise.
2238  */
intel_dp_mst_verify_dpcd_state(struct intel_dp * intel_dp)2239 bool intel_dp_mst_verify_dpcd_state(struct intel_dp *intel_dp)
2240 {
2241 	struct intel_display *display = to_intel_display(intel_dp);
2242 	struct intel_connector *connector = intel_dp->attached_connector;
2243 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2244 	struct intel_encoder *encoder = &dig_port->base;
2245 	int ret;
2246 	u8 val;
2247 
2248 	if (!intel_dp->is_mst)
2249 		return true;
2250 
2251 	ret = drm_dp_dpcd_readb(intel_dp->mst.mgr.aux, DP_MSTM_CTRL, &val);
2252 
2253 	/* Adjust the expected register value for SST + SideBand. */
2254 	if (ret < 0 || val != (DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC)) {
2255 		drm_dbg_kms(display->drm,
2256 			    "[CONNECTOR:%d:%s][ENCODER:%d:%s] MST mode got reset, removing topology (ret=%d, ctrl=0x%02x)\n",
2257 			    connector->base.base.id, connector->base.name,
2258 			    encoder->base.base.id, encoder->base.name,
2259 			    ret, val);
2260 
2261 		return false;
2262 	}
2263 
2264 	return true;
2265 }
2266