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