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