1 /*
2  * Copyright © 2008 Intel Corporation
3  *             2014 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  */
25 
26 #include <drm/drm_atomic.h>
27 #include <drm/drm_atomic_helper.h>
28 #include <drm/drm_edid.h>
29 #include <drm/drm_probe_helper.h>
30 
31 #include "i915_drv.h"
32 #include "intel_atomic.h"
33 #include "intel_audio.h"
34 #include "intel_connector.h"
35 #include "intel_crtc.h"
36 #include "intel_ddi.h"
37 #include "intel_de.h"
38 #include "intel_display_types.h"
39 #include "intel_dp.h"
40 #include "intel_dp_hdcp.h"
41 #include "intel_dp_mst.h"
42 #include "intel_dpio_phy.h"
43 #include "intel_hdcp.h"
44 #include "intel_hotplug.h"
45 #include "skl_scaler.h"
46 
47 static int intel_dp_mst_compute_link_config(struct intel_encoder *encoder,
48 					    struct intel_crtc_state *crtc_state,
49 					    struct drm_connector_state *conn_state,
50 					    struct link_config_limits *limits)
51 {
52 	struct drm_atomic_state *state = crtc_state->uapi.state;
53 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
54 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
55 	struct intel_connector *connector =
56 		to_intel_connector(conn_state->connector);
57 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
58 	const struct drm_display_mode *adjusted_mode =
59 		&crtc_state->hw.adjusted_mode;
60 	bool constant_n = drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_CONSTANT_N);
61 	int bpp, slots = -EINVAL;
62 
63 	crtc_state->lane_count = limits->max_lane_count;
64 	crtc_state->port_clock = limits->max_rate;
65 
66 	for (bpp = limits->max_bpp; bpp >= limits->min_bpp; bpp -= 2 * 3) {
67 		crtc_state->pipe_bpp = bpp;
68 
69 		crtc_state->pbn = drm_dp_calc_pbn_mode(adjusted_mode->crtc_clock,
70 						       crtc_state->pipe_bpp,
71 						       false);
72 
73 		slots = drm_dp_atomic_find_vcpi_slots(state, &intel_dp->mst_mgr,
74 						      connector->port,
75 						      crtc_state->pbn,
76 						      drm_dp_get_vc_payload_bw(&intel_dp->mst_mgr,
77 									       crtc_state->port_clock,
78 									       crtc_state->lane_count));
79 		if (slots == -EDEADLK)
80 			return slots;
81 		if (slots >= 0)
82 			break;
83 	}
84 
85 	if (slots < 0) {
86 		drm_dbg_kms(&i915->drm, "failed finding vcpi slots:%d\n",
87 			    slots);
88 		return slots;
89 	}
90 
91 	intel_link_compute_m_n(crtc_state->pipe_bpp,
92 			       crtc_state->lane_count,
93 			       adjusted_mode->crtc_clock,
94 			       crtc_state->port_clock,
95 			       &crtc_state->dp_m_n,
96 			       constant_n, crtc_state->fec_enable);
97 	crtc_state->dp_m_n.tu = slots;
98 
99 	return 0;
100 }
101 
102 static int intel_dp_mst_update_slots(struct intel_encoder *encoder,
103 				     struct intel_crtc_state *crtc_state,
104 				     struct drm_connector_state *conn_state)
105 {
106 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
107 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
108 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
109 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
110 	struct drm_dp_mst_topology_state *topology_state;
111 	u8 link_coding_cap = intel_dp_is_uhbr(crtc_state) ?
112 		DP_CAP_ANSI_128B132B : DP_CAP_ANSI_8B10B;
113 
114 	topology_state = drm_atomic_get_mst_topology_state(conn_state->state, mgr);
115 	if (IS_ERR(topology_state)) {
116 		drm_dbg_kms(&i915->drm, "slot update failed\n");
117 		return PTR_ERR(topology_state);
118 	}
119 
120 	drm_dp_mst_update_slots(topology_state, link_coding_cap);
121 
122 	return 0;
123 }
124 
125 static int intel_dp_mst_compute_config(struct intel_encoder *encoder,
126 				       struct intel_crtc_state *pipe_config,
127 				       struct drm_connector_state *conn_state)
128 {
129 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
130 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
131 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
132 	struct intel_connector *connector =
133 		to_intel_connector(conn_state->connector);
134 	struct intel_digital_connector_state *intel_conn_state =
135 		to_intel_digital_connector_state(conn_state);
136 	const struct drm_display_mode *adjusted_mode =
137 		&pipe_config->hw.adjusted_mode;
138 	struct link_config_limits limits;
139 	int ret;
140 
141 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
142 		return -EINVAL;
143 
144 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
145 	pipe_config->has_pch_encoder = false;
146 
147 	if (intel_conn_state->force_audio == HDMI_AUDIO_AUTO)
148 		pipe_config->has_audio = connector->port->has_audio;
149 	else
150 		pipe_config->has_audio =
151 			intel_conn_state->force_audio == HDMI_AUDIO_ON;
152 
153 	/*
154 	 * for MST we always configure max link bw - the spec doesn't
155 	 * seem to suggest we should do otherwise.
156 	 */
157 	limits.min_rate =
158 	limits.max_rate = intel_dp_max_link_rate(intel_dp);
159 
160 	limits.min_lane_count =
161 	limits.max_lane_count = intel_dp_max_lane_count(intel_dp);
162 
163 	limits.min_bpp = intel_dp_min_bpp(pipe_config->output_format);
164 	/*
165 	 * FIXME: If all the streams can't fit into the link with
166 	 * their current pipe_bpp we should reduce pipe_bpp across
167 	 * the board until things start to fit. Until then we
168 	 * limit to <= 8bpc since that's what was hardcoded for all
169 	 * MST streams previously. This hack should be removed once
170 	 * we have the proper retry logic in place.
171 	 */
172 	limits.max_bpp = min(pipe_config->pipe_bpp, 24);
173 
174 	intel_dp_adjust_compliance_config(intel_dp, pipe_config, &limits);
175 
176 	ret = intel_dp_mst_compute_link_config(encoder, pipe_config,
177 					       conn_state, &limits);
178 	if (ret)
179 		return ret;
180 
181 	ret = intel_dp_mst_update_slots(encoder, pipe_config, conn_state);
182 	if (ret)
183 		return ret;
184 
185 	pipe_config->limited_color_range =
186 		intel_dp_limited_color_range(pipe_config, conn_state);
187 
188 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
189 		pipe_config->lane_lat_optim_mask =
190 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
191 
192 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
193 
194 	return 0;
195 }
196 
197 /*
198  * Iterate over all connectors and return a mask of
199  * all CPU transcoders streaming over the same DP link.
200  */
201 static unsigned int
202 intel_dp_mst_transcoder_mask(struct intel_atomic_state *state,
203 			     struct intel_dp *mst_port)
204 {
205 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
206 	const struct intel_digital_connector_state *conn_state;
207 	struct intel_connector *connector;
208 	u8 transcoders = 0;
209 	int i;
210 
211 	if (DISPLAY_VER(dev_priv) < 12)
212 		return 0;
213 
214 	for_each_new_intel_connector_in_state(state, connector, conn_state, i) {
215 		const struct intel_crtc_state *crtc_state;
216 		struct intel_crtc *crtc;
217 
218 		if (connector->mst_port != mst_port || !conn_state->base.crtc)
219 			continue;
220 
221 		crtc = to_intel_crtc(conn_state->base.crtc);
222 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
223 
224 		if (!crtc_state->hw.active)
225 			continue;
226 
227 		transcoders |= BIT(crtc_state->cpu_transcoder);
228 	}
229 
230 	return transcoders;
231 }
232 
233 static int intel_dp_mst_compute_config_late(struct intel_encoder *encoder,
234 					    struct intel_crtc_state *crtc_state,
235 					    struct drm_connector_state *conn_state)
236 {
237 	struct intel_atomic_state *state = to_intel_atomic_state(conn_state->state);
238 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
239 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
240 
241 	/* lowest numbered transcoder will be designated master */
242 	crtc_state->mst_master_transcoder =
243 		ffs(intel_dp_mst_transcoder_mask(state, intel_dp)) - 1;
244 
245 	return 0;
246 }
247 
248 /*
249  * If one of the connectors in a MST stream needs a modeset, mark all CRTCs
250  * that shares the same MST stream as mode changed,
251  * intel_modeset_pipe_config()+intel_crtc_check_fastset() will take care to do
252  * a fastset when possible.
253  */
254 static int
255 intel_dp_mst_atomic_master_trans_check(struct intel_connector *connector,
256 				       struct intel_atomic_state *state)
257 {
258 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
259 	struct drm_connector_list_iter connector_list_iter;
260 	struct intel_connector *connector_iter;
261 	int ret = 0;
262 
263 	if (DISPLAY_VER(dev_priv) < 12)
264 		return  0;
265 
266 	if (!intel_connector_needs_modeset(state, &connector->base))
267 		return 0;
268 
269 	drm_connector_list_iter_begin(&dev_priv->drm, &connector_list_iter);
270 	for_each_intel_connector_iter(connector_iter, &connector_list_iter) {
271 		struct intel_digital_connector_state *conn_iter_state;
272 		struct intel_crtc_state *crtc_state;
273 		struct intel_crtc *crtc;
274 
275 		if (connector_iter->mst_port != connector->mst_port ||
276 		    connector_iter == connector)
277 			continue;
278 
279 		conn_iter_state = intel_atomic_get_digital_connector_state(state,
280 									   connector_iter);
281 		if (IS_ERR(conn_iter_state)) {
282 			ret = PTR_ERR(conn_iter_state);
283 			break;
284 		}
285 
286 		if (!conn_iter_state->base.crtc)
287 			continue;
288 
289 		crtc = to_intel_crtc(conn_iter_state->base.crtc);
290 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
291 		if (IS_ERR(crtc_state)) {
292 			ret = PTR_ERR(crtc_state);
293 			break;
294 		}
295 
296 		ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
297 		if (ret)
298 			break;
299 		crtc_state->uapi.mode_changed = true;
300 	}
301 	drm_connector_list_iter_end(&connector_list_iter);
302 
303 	return ret;
304 }
305 
306 static int
307 intel_dp_mst_atomic_check(struct drm_connector *connector,
308 			  struct drm_atomic_state *_state)
309 {
310 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
311 	struct drm_connector_state *new_conn_state =
312 		drm_atomic_get_new_connector_state(&state->base, connector);
313 	struct drm_connector_state *old_conn_state =
314 		drm_atomic_get_old_connector_state(&state->base, connector);
315 	struct intel_connector *intel_connector =
316 		to_intel_connector(connector);
317 	struct drm_crtc *new_crtc = new_conn_state->crtc;
318 	struct drm_dp_mst_topology_mgr *mgr;
319 	int ret;
320 
321 	ret = intel_digital_connector_atomic_check(connector, &state->base);
322 	if (ret)
323 		return ret;
324 
325 	ret = intel_dp_mst_atomic_master_trans_check(intel_connector, state);
326 	if (ret)
327 		return ret;
328 
329 	if (!old_conn_state->crtc)
330 		return 0;
331 
332 	/* We only want to free VCPI if this state disables the CRTC on this
333 	 * connector
334 	 */
335 	if (new_crtc) {
336 		struct intel_crtc *crtc = to_intel_crtc(new_crtc);
337 		struct intel_crtc_state *crtc_state =
338 			intel_atomic_get_new_crtc_state(state, crtc);
339 
340 		if (!crtc_state ||
341 		    !drm_atomic_crtc_needs_modeset(&crtc_state->uapi) ||
342 		    crtc_state->uapi.enable)
343 			return 0;
344 	}
345 
346 	mgr = &enc_to_mst(to_intel_encoder(old_conn_state->best_encoder))->primary->dp.mst_mgr;
347 	ret = drm_dp_atomic_release_vcpi_slots(&state->base, mgr,
348 					       intel_connector->port);
349 
350 	return ret;
351 }
352 
353 static void clear_act_sent(struct intel_encoder *encoder,
354 			   const struct intel_crtc_state *crtc_state)
355 {
356 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
357 
358 	intel_de_write(i915, dp_tp_status_reg(encoder, crtc_state),
359 		       DP_TP_STATUS_ACT_SENT);
360 }
361 
362 static void wait_for_act_sent(struct intel_encoder *encoder,
363 			      const struct intel_crtc_state *crtc_state)
364 {
365 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
366 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
367 	struct intel_dp *intel_dp = &intel_mst->primary->dp;
368 
369 	if (intel_de_wait_for_set(i915, dp_tp_status_reg(encoder, crtc_state),
370 				  DP_TP_STATUS_ACT_SENT, 1))
371 		drm_err(&i915->drm, "Timed out waiting for ACT sent\n");
372 
373 	drm_dp_check_act_status(&intel_dp->mst_mgr);
374 }
375 
376 static void intel_mst_disable_dp(struct intel_atomic_state *state,
377 				 struct intel_encoder *encoder,
378 				 const struct intel_crtc_state *old_crtc_state,
379 				 const struct drm_connector_state *old_conn_state)
380 {
381 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
382 	struct intel_digital_port *dig_port = intel_mst->primary;
383 	struct intel_dp *intel_dp = &dig_port->dp;
384 	struct intel_connector *connector =
385 		to_intel_connector(old_conn_state->connector);
386 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
387 	int start_slot = intel_dp_is_uhbr(old_crtc_state) ? 0 : 1;
388 	int ret;
389 
390 	drm_dbg_kms(&i915->drm, "active links %d\n",
391 		    intel_dp->active_mst_links);
392 
393 	intel_hdcp_disable(intel_mst->connector);
394 
395 	drm_dp_mst_reset_vcpi_slots(&intel_dp->mst_mgr, connector->port);
396 
397 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr, start_slot);
398 	if (ret) {
399 		drm_dbg_kms(&i915->drm, "failed to update payload %d\n", ret);
400 	}
401 	if (old_crtc_state->has_audio)
402 		intel_audio_codec_disable(encoder,
403 					  old_crtc_state, old_conn_state);
404 }
405 
406 static void intel_mst_post_disable_dp(struct intel_atomic_state *state,
407 				      struct intel_encoder *encoder,
408 				      const struct intel_crtc_state *old_crtc_state,
409 				      const struct drm_connector_state *old_conn_state)
410 {
411 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
412 	struct intel_digital_port *dig_port = intel_mst->primary;
413 	struct intel_dp *intel_dp = &dig_port->dp;
414 	struct intel_connector *connector =
415 		to_intel_connector(old_conn_state->connector);
416 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
417 	bool last_mst_stream;
418 
419 	intel_dp->active_mst_links--;
420 	last_mst_stream = intel_dp->active_mst_links == 0;
421 	drm_WARN_ON(&dev_priv->drm,
422 		    DISPLAY_VER(dev_priv) >= 12 && last_mst_stream &&
423 		    !intel_dp_mst_is_master_trans(old_crtc_state));
424 
425 	intel_crtc_vblank_off(old_crtc_state);
426 
427 	intel_disable_transcoder(old_crtc_state);
428 
429 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
430 
431 	clear_act_sent(encoder, old_crtc_state);
432 
433 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(old_crtc_state->cpu_transcoder),
434 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC, 0);
435 
436 	wait_for_act_sent(encoder, old_crtc_state);
437 
438 	drm_dp_mst_deallocate_vcpi(&intel_dp->mst_mgr, connector->port);
439 
440 	intel_ddi_disable_transcoder_func(old_crtc_state);
441 
442 	if (DISPLAY_VER(dev_priv) >= 9)
443 		skl_scaler_disable(old_crtc_state);
444 	else
445 		ilk_pfit_disable(old_crtc_state);
446 
447 	/*
448 	 * Power down mst path before disabling the port, otherwise we end
449 	 * up getting interrupts from the sink upon detecting link loss.
450 	 */
451 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port,
452 				     false);
453 
454 	/*
455 	 * BSpec 4287: disable DIP after the transcoder is disabled and before
456 	 * the transcoder clock select is set to none.
457 	 */
458 	if (last_mst_stream)
459 		intel_dp_set_infoframes(&dig_port->base, false,
460 					old_crtc_state, NULL);
461 	/*
462 	 * From TGL spec: "If multi-stream slave transcoder: Configure
463 	 * Transcoder Clock Select to direct no clock to the transcoder"
464 	 *
465 	 * From older GENs spec: "Configure Transcoder Clock Select to direct
466 	 * no clock to the transcoder"
467 	 */
468 	if (DISPLAY_VER(dev_priv) < 12 || !last_mst_stream)
469 		intel_ddi_disable_pipe_clock(old_crtc_state);
470 
471 
472 	intel_mst->connector = NULL;
473 	if (last_mst_stream)
474 		dig_port->base.post_disable(state, &dig_port->base,
475 						  old_crtc_state, NULL);
476 
477 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
478 		    intel_dp->active_mst_links);
479 }
480 
481 static void intel_mst_pre_pll_enable_dp(struct intel_atomic_state *state,
482 					struct intel_encoder *encoder,
483 					const struct intel_crtc_state *pipe_config,
484 					const struct drm_connector_state *conn_state)
485 {
486 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
487 	struct intel_digital_port *dig_port = intel_mst->primary;
488 	struct intel_dp *intel_dp = &dig_port->dp;
489 
490 	if (intel_dp->active_mst_links == 0)
491 		dig_port->base.pre_pll_enable(state, &dig_port->base,
492 						    pipe_config, NULL);
493 }
494 
495 static void intel_mst_pre_enable_dp(struct intel_atomic_state *state,
496 				    struct intel_encoder *encoder,
497 				    const struct intel_crtc_state *pipe_config,
498 				    const struct drm_connector_state *conn_state)
499 {
500 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
501 	struct intel_digital_port *dig_port = intel_mst->primary;
502 	struct intel_dp *intel_dp = &dig_port->dp;
503 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
504 	struct intel_connector *connector =
505 		to_intel_connector(conn_state->connector);
506 	int start_slot = intel_dp_is_uhbr(pipe_config) ? 0 : 1;
507 	int ret;
508 	bool first_mst_stream;
509 
510 	/* MST encoders are bound to a crtc, not to a connector,
511 	 * force the mapping here for get_hw_state.
512 	 */
513 	connector->encoder = encoder;
514 	intel_mst->connector = connector;
515 	first_mst_stream = intel_dp->active_mst_links == 0;
516 	drm_WARN_ON(&dev_priv->drm,
517 		    DISPLAY_VER(dev_priv) >= 12 && first_mst_stream &&
518 		    !intel_dp_mst_is_master_trans(pipe_config));
519 
520 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
521 		    intel_dp->active_mst_links);
522 
523 	if (first_mst_stream)
524 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
525 
526 	drm_dp_send_power_updown_phy(&intel_dp->mst_mgr, connector->port, true);
527 
528 	if (first_mst_stream)
529 		dig_port->base.pre_enable(state, &dig_port->base,
530 						pipe_config, NULL);
531 
532 	ret = drm_dp_mst_allocate_vcpi(&intel_dp->mst_mgr,
533 				       connector->port,
534 				       pipe_config->pbn,
535 				       pipe_config->dp_m_n.tu);
536 	if (!ret)
537 		drm_err(&dev_priv->drm, "failed to allocate vcpi\n");
538 
539 	intel_dp->active_mst_links++;
540 
541 	ret = drm_dp_update_payload_part1(&intel_dp->mst_mgr, start_slot);
542 
543 	/*
544 	 * Before Gen 12 this is not done as part of
545 	 * dig_port->base.pre_enable() and should be done here. For
546 	 * Gen 12+ the step in which this should be done is different for the
547 	 * first MST stream, so it's done on the DDI for the first stream and
548 	 * here for the following ones.
549 	 */
550 	if (DISPLAY_VER(dev_priv) < 12 || !first_mst_stream)
551 		intel_ddi_enable_pipe_clock(encoder, pipe_config);
552 
553 	intel_ddi_set_dp_msa(pipe_config, conn_state);
554 }
555 
556 static void intel_mst_enable_dp(struct intel_atomic_state *state,
557 				struct intel_encoder *encoder,
558 				const struct intel_crtc_state *pipe_config,
559 				const struct drm_connector_state *conn_state)
560 {
561 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
562 	struct intel_digital_port *dig_port = intel_mst->primary;
563 	struct intel_dp *intel_dp = &dig_port->dp;
564 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
565 	enum transcoder trans = pipe_config->cpu_transcoder;
566 
567 	drm_WARN_ON(&dev_priv->drm, pipe_config->has_pch_encoder);
568 
569 	clear_act_sent(encoder, pipe_config);
570 
571 	if (intel_dp_is_uhbr(pipe_config)) {
572 		const struct drm_display_mode *adjusted_mode =
573 			&pipe_config->hw.adjusted_mode;
574 		u64 crtc_clock_hz = KHz(adjusted_mode->crtc_clock);
575 
576 		intel_de_write(dev_priv, TRANS_DP2_VFREQHIGH(pipe_config->cpu_transcoder),
577 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz >> 24));
578 		intel_de_write(dev_priv, TRANS_DP2_VFREQLOW(pipe_config->cpu_transcoder),
579 			       TRANS_DP2_VFREQ_PIXEL_CLOCK(crtc_clock_hz & 0xffffff));
580 	}
581 
582 	intel_ddi_enable_transcoder_func(encoder, pipe_config);
583 
584 	intel_de_rmw(dev_priv, TRANS_DDI_FUNC_CTL(trans), 0,
585 		     TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
586 
587 	drm_dbg_kms(&dev_priv->drm, "active links %d\n",
588 		    intel_dp->active_mst_links);
589 
590 	wait_for_act_sent(encoder, pipe_config);
591 
592 	drm_dp_update_payload_part2(&intel_dp->mst_mgr);
593 
594 	if (DISPLAY_VER(dev_priv) >= 12 && pipe_config->fec_enable)
595 		intel_de_rmw(dev_priv, CHICKEN_TRANS(trans), 0,
596 			     FECSTALL_DIS_DPTSTREAM_DPTTG);
597 
598 	intel_enable_transcoder(pipe_config);
599 
600 	intel_crtc_vblank_on(pipe_config);
601 
602 	if (pipe_config->has_audio)
603 		intel_audio_codec_enable(encoder, pipe_config, conn_state);
604 
605 	/* Enable hdcp if it's desired */
606 	if (conn_state->content_protection ==
607 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
608 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
609 				  pipe_config,
610 				  (u8)conn_state->hdcp_content_type);
611 }
612 
613 static bool intel_dp_mst_enc_get_hw_state(struct intel_encoder *encoder,
614 				      enum pipe *pipe)
615 {
616 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
617 	*pipe = intel_mst->pipe;
618 	if (intel_mst->connector)
619 		return true;
620 	return false;
621 }
622 
623 static void intel_dp_mst_enc_get_config(struct intel_encoder *encoder,
624 					struct intel_crtc_state *pipe_config)
625 {
626 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
627 	struct intel_digital_port *dig_port = intel_mst->primary;
628 
629 	dig_port->base.get_config(&dig_port->base, pipe_config);
630 }
631 
632 static bool intel_dp_mst_initial_fastset_check(struct intel_encoder *encoder,
633 					       struct intel_crtc_state *crtc_state)
634 {
635 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(encoder);
636 	struct intel_digital_port *dig_port = intel_mst->primary;
637 
638 	return intel_dp_initial_fastset_check(&dig_port->base, crtc_state);
639 }
640 
641 static int intel_dp_mst_get_ddc_modes(struct drm_connector *connector)
642 {
643 	struct intel_connector *intel_connector = to_intel_connector(connector);
644 	struct intel_dp *intel_dp = intel_connector->mst_port;
645 	struct edid *edid;
646 	int ret;
647 
648 	if (drm_connector_is_unregistered(connector))
649 		return intel_connector_update_modes(connector, NULL);
650 
651 	edid = drm_dp_mst_get_edid(connector, &intel_dp->mst_mgr, intel_connector->port);
652 	ret = intel_connector_update_modes(connector, edid);
653 	kfree(edid);
654 
655 	return ret;
656 }
657 
658 static int
659 intel_dp_mst_connector_late_register(struct drm_connector *connector)
660 {
661 	struct intel_connector *intel_connector = to_intel_connector(connector);
662 	int ret;
663 
664 	ret = drm_dp_mst_connector_late_register(connector,
665 						 intel_connector->port);
666 	if (ret < 0)
667 		return ret;
668 
669 	ret = intel_connector_register(connector);
670 	if (ret < 0)
671 		drm_dp_mst_connector_early_unregister(connector,
672 						      intel_connector->port);
673 
674 	return ret;
675 }
676 
677 static void
678 intel_dp_mst_connector_early_unregister(struct drm_connector *connector)
679 {
680 	struct intel_connector *intel_connector = to_intel_connector(connector);
681 
682 	intel_connector_unregister(connector);
683 	drm_dp_mst_connector_early_unregister(connector,
684 					      intel_connector->port);
685 }
686 
687 static const struct drm_connector_funcs intel_dp_mst_connector_funcs = {
688 	.fill_modes = drm_helper_probe_single_connector_modes,
689 	.atomic_get_property = intel_digital_connector_atomic_get_property,
690 	.atomic_set_property = intel_digital_connector_atomic_set_property,
691 	.late_register = intel_dp_mst_connector_late_register,
692 	.early_unregister = intel_dp_mst_connector_early_unregister,
693 	.destroy = intel_connector_destroy,
694 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
695 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
696 };
697 
698 static int intel_dp_mst_get_modes(struct drm_connector *connector)
699 {
700 	return intel_dp_mst_get_ddc_modes(connector);
701 }
702 
703 static int
704 intel_dp_mst_mode_valid_ctx(struct drm_connector *connector,
705 			    struct drm_display_mode *mode,
706 			    struct drm_modeset_acquire_ctx *ctx,
707 			    enum drm_mode_status *status)
708 {
709 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
710 	struct intel_connector *intel_connector = to_intel_connector(connector);
711 	struct intel_dp *intel_dp = intel_connector->mst_port;
712 	struct drm_dp_mst_topology_mgr *mgr = &intel_dp->mst_mgr;
713 	struct drm_dp_mst_port *port = intel_connector->port;
714 	const int min_bpp = 18;
715 	int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
716 	int max_rate, mode_rate, max_lanes, max_link_clock;
717 	int ret;
718 
719 	if (drm_connector_is_unregistered(connector)) {
720 		*status = MODE_ERROR;
721 		return 0;
722 	}
723 
724 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
725 		*status = MODE_NO_DBLESCAN;
726 		return 0;
727 	}
728 
729 	max_link_clock = intel_dp_max_link_rate(intel_dp);
730 	max_lanes = intel_dp_max_lane_count(intel_dp);
731 
732 	max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
733 	mode_rate = intel_dp_link_required(mode->clock, min_bpp);
734 
735 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
736 	if (ret)
737 		return ret;
738 
739 	if (mode_rate > max_rate || mode->clock > max_dotclk ||
740 	    drm_dp_calc_pbn_mode(mode->clock, min_bpp, false) > port->full_pbn) {
741 		*status = MODE_CLOCK_HIGH;
742 		return 0;
743 	}
744 
745 	if (mode->clock < 10000) {
746 		*status = MODE_CLOCK_LOW;
747 		return 0;
748 	}
749 
750 	if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
751 		*status = MODE_H_ILLEGAL;
752 		return 0;
753 	}
754 
755 	*status = intel_mode_valid_max_plane_size(dev_priv, mode, false);
756 	return 0;
757 }
758 
759 static struct drm_encoder *intel_mst_atomic_best_encoder(struct drm_connector *connector,
760 							 struct drm_atomic_state *state)
761 {
762 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
763 											 connector);
764 	struct intel_connector *intel_connector = to_intel_connector(connector);
765 	struct intel_dp *intel_dp = intel_connector->mst_port;
766 	struct intel_crtc *crtc = to_intel_crtc(connector_state->crtc);
767 
768 	return &intel_dp->mst_encoders[crtc->pipe]->base.base;
769 }
770 
771 static int
772 intel_dp_mst_detect(struct drm_connector *connector,
773 		    struct drm_modeset_acquire_ctx *ctx, bool force)
774 {
775 	struct drm_i915_private *i915 = to_i915(connector->dev);
776 	struct intel_connector *intel_connector = to_intel_connector(connector);
777 	struct intel_dp *intel_dp = intel_connector->mst_port;
778 
779 	if (!INTEL_DISPLAY_ENABLED(i915))
780 		return connector_status_disconnected;
781 
782 	if (drm_connector_is_unregistered(connector))
783 		return connector_status_disconnected;
784 
785 	return drm_dp_mst_detect_port(connector, ctx, &intel_dp->mst_mgr,
786 				      intel_connector->port);
787 }
788 
789 static const struct drm_connector_helper_funcs intel_dp_mst_connector_helper_funcs = {
790 	.get_modes = intel_dp_mst_get_modes,
791 	.mode_valid_ctx = intel_dp_mst_mode_valid_ctx,
792 	.atomic_best_encoder = intel_mst_atomic_best_encoder,
793 	.atomic_check = intel_dp_mst_atomic_check,
794 	.detect_ctx = intel_dp_mst_detect,
795 };
796 
797 static void intel_dp_mst_encoder_destroy(struct drm_encoder *encoder)
798 {
799 	struct intel_dp_mst_encoder *intel_mst = enc_to_mst(to_intel_encoder(encoder));
800 
801 	drm_encoder_cleanup(encoder);
802 	kfree(intel_mst);
803 }
804 
805 static const struct drm_encoder_funcs intel_dp_mst_enc_funcs = {
806 	.destroy = intel_dp_mst_encoder_destroy,
807 };
808 
809 static bool intel_dp_mst_get_hw_state(struct intel_connector *connector)
810 {
811 	if (intel_attached_encoder(connector) && connector->base.state->crtc) {
812 		enum pipe pipe;
813 		if (!intel_attached_encoder(connector)->get_hw_state(intel_attached_encoder(connector), &pipe))
814 			return false;
815 		return true;
816 	}
817 	return false;
818 }
819 
820 static struct drm_connector *intel_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *pathprop)
821 {
822 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
823 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
824 	struct drm_device *dev = dig_port->base.base.dev;
825 	struct drm_i915_private *dev_priv = to_i915(dev);
826 	struct intel_connector *intel_connector;
827 	struct drm_connector *connector;
828 	enum pipe pipe;
829 	int ret;
830 
831 	intel_connector = intel_connector_alloc();
832 	if (!intel_connector)
833 		return NULL;
834 
835 	intel_connector->get_hw_state = intel_dp_mst_get_hw_state;
836 	intel_connector->mst_port = intel_dp;
837 	intel_connector->port = port;
838 	drm_dp_mst_get_port_malloc(port);
839 
840 	connector = &intel_connector->base;
841 	ret = drm_connector_init(dev, connector, &intel_dp_mst_connector_funcs,
842 				 DRM_MODE_CONNECTOR_DisplayPort);
843 	if (ret) {
844 		intel_connector_free(intel_connector);
845 		return NULL;
846 	}
847 
848 	drm_connector_helper_add(connector, &intel_dp_mst_connector_helper_funcs);
849 
850 	for_each_pipe(dev_priv, pipe) {
851 		struct drm_encoder *enc =
852 			&intel_dp->mst_encoders[pipe]->base.base;
853 
854 		ret = drm_connector_attach_encoder(&intel_connector->base, enc);
855 		if (ret)
856 			goto err;
857 	}
858 
859 	drm_object_attach_property(&connector->base, dev->mode_config.path_property, 0);
860 	drm_object_attach_property(&connector->base, dev->mode_config.tile_property, 0);
861 
862 	ret = drm_connector_set_path_property(connector, pathprop);
863 	if (ret)
864 		goto err;
865 
866 	intel_attach_force_audio_property(connector);
867 	intel_attach_broadcast_rgb_property(connector);
868 
869 	ret = intel_dp_hdcp_init(dig_port, intel_connector);
870 	if (ret)
871 		drm_dbg_kms(&dev_priv->drm, "[%s:%d] HDCP MST init failed, skipping.\n",
872 			    connector->name, connector->base.id);
873 	/*
874 	 * Reuse the prop from the SST connector because we're
875 	 * not allowed to create new props after device registration.
876 	 */
877 	connector->max_bpc_property =
878 		intel_dp->attached_connector->base.max_bpc_property;
879 	if (connector->max_bpc_property)
880 		drm_connector_attach_max_bpc_property(connector, 6, 12);
881 
882 	return connector;
883 
884 err:
885 	drm_connector_cleanup(connector);
886 	return NULL;
887 }
888 
889 static void
890 intel_dp_mst_poll_hpd_irq(struct drm_dp_mst_topology_mgr *mgr)
891 {
892 	struct intel_dp *intel_dp = container_of(mgr, struct intel_dp, mst_mgr);
893 
894 	intel_hpd_trigger_irq(dp_to_dig_port(intel_dp));
895 }
896 
897 static const struct drm_dp_mst_topology_cbs mst_cbs = {
898 	.add_connector = intel_dp_add_mst_connector,
899 	.poll_hpd_irq = intel_dp_mst_poll_hpd_irq,
900 };
901 
902 static struct intel_dp_mst_encoder *
903 intel_dp_create_fake_mst_encoder(struct intel_digital_port *dig_port, enum pipe pipe)
904 {
905 	struct intel_dp_mst_encoder *intel_mst;
906 	struct intel_encoder *intel_encoder;
907 	struct drm_device *dev = dig_port->base.base.dev;
908 
909 	intel_mst = kzalloc(sizeof(*intel_mst), GFP_KERNEL);
910 
911 	if (!intel_mst)
912 		return NULL;
913 
914 	intel_mst->pipe = pipe;
915 	intel_encoder = &intel_mst->base;
916 	intel_mst->primary = dig_port;
917 
918 	drm_encoder_init(dev, &intel_encoder->base, &intel_dp_mst_enc_funcs,
919 			 DRM_MODE_ENCODER_DPMST, "DP-MST %c", pipe_name(pipe));
920 
921 	intel_encoder->type = INTEL_OUTPUT_DP_MST;
922 	intel_encoder->power_domain = dig_port->base.power_domain;
923 	intel_encoder->port = dig_port->base.port;
924 	intel_encoder->cloneable = 0;
925 	/*
926 	 * This is wrong, but broken userspace uses the intersection
927 	 * of possible_crtcs of all the encoders of a given connector
928 	 * to figure out which crtcs can drive said connector. What
929 	 * should be used instead is the union of possible_crtcs.
930 	 * To keep such userspace functioning we must misconfigure
931 	 * this to make sure the intersection is not empty :(
932 	 */
933 	intel_encoder->pipe_mask = ~0;
934 
935 	intel_encoder->compute_config = intel_dp_mst_compute_config;
936 	intel_encoder->compute_config_late = intel_dp_mst_compute_config_late;
937 	intel_encoder->disable = intel_mst_disable_dp;
938 	intel_encoder->post_disable = intel_mst_post_disable_dp;
939 	intel_encoder->update_pipe = intel_ddi_update_pipe;
940 	intel_encoder->pre_pll_enable = intel_mst_pre_pll_enable_dp;
941 	intel_encoder->pre_enable = intel_mst_pre_enable_dp;
942 	intel_encoder->enable = intel_mst_enable_dp;
943 	intel_encoder->get_hw_state = intel_dp_mst_enc_get_hw_state;
944 	intel_encoder->get_config = intel_dp_mst_enc_get_config;
945 	intel_encoder->initial_fastset_check = intel_dp_mst_initial_fastset_check;
946 
947 	return intel_mst;
948 
949 }
950 
951 static bool
952 intel_dp_create_fake_mst_encoders(struct intel_digital_port *dig_port)
953 {
954 	struct intel_dp *intel_dp = &dig_port->dp;
955 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
956 	enum pipe pipe;
957 
958 	for_each_pipe(dev_priv, pipe)
959 		intel_dp->mst_encoders[pipe] = intel_dp_create_fake_mst_encoder(dig_port, pipe);
960 	return true;
961 }
962 
963 int
964 intel_dp_mst_encoder_active_links(struct intel_digital_port *dig_port)
965 {
966 	return dig_port->dp.active_mst_links;
967 }
968 
969 int
970 intel_dp_mst_encoder_init(struct intel_digital_port *dig_port, int conn_base_id)
971 {
972 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
973 	struct intel_dp *intel_dp = &dig_port->dp;
974 	enum port port = dig_port->base.port;
975 	int ret;
976 	int max_source_rate =
977 		intel_dp->source_rates[intel_dp->num_source_rates - 1];
978 
979 	if (!HAS_DP_MST(i915) || intel_dp_is_edp(intel_dp))
980 		return 0;
981 
982 	if (DISPLAY_VER(i915) < 12 && port == PORT_A)
983 		return 0;
984 
985 	if (DISPLAY_VER(i915) < 11 && port == PORT_E)
986 		return 0;
987 
988 	intel_dp->mst_mgr.cbs = &mst_cbs;
989 
990 	/* create encoders */
991 	intel_dp_create_fake_mst_encoders(dig_port);
992 	ret = drm_dp_mst_topology_mgr_init(&intel_dp->mst_mgr, &i915->drm,
993 					   &intel_dp->aux, 16, 3,
994 					   dig_port->max_lanes,
995 					   max_source_rate,
996 					   conn_base_id);
997 	if (ret) {
998 		intel_dp->mst_mgr.cbs = NULL;
999 		return ret;
1000 	}
1001 
1002 	return 0;
1003 }
1004 
1005 bool intel_dp_mst_source_support(struct intel_dp *intel_dp)
1006 {
1007 	return intel_dp->mst_mgr.cbs;
1008 }
1009 
1010 void
1011 intel_dp_mst_encoder_cleanup(struct intel_digital_port *dig_port)
1012 {
1013 	struct intel_dp *intel_dp = &dig_port->dp;
1014 
1015 	if (!intel_dp_mst_source_support(intel_dp))
1016 		return;
1017 
1018 	drm_dp_mst_topology_mgr_destroy(&intel_dp->mst_mgr);
1019 	/* encoders will get killed by normal cleanup */
1020 
1021 	intel_dp->mst_mgr.cbs = NULL;
1022 }
1023 
1024 bool intel_dp_mst_is_master_trans(const struct intel_crtc_state *crtc_state)
1025 {
1026 	return crtc_state->mst_master_transcoder == crtc_state->cpu_transcoder;
1027 }
1028 
1029 bool intel_dp_mst_is_slave_trans(const struct intel_crtc_state *crtc_state)
1030 {
1031 	return crtc_state->mst_master_transcoder != INVALID_TRANSCODER &&
1032 	       crtc_state->mst_master_transcoder != crtc_state->cpu_transcoder;
1033 }
1034